From 00b9fb35b75f97ce1be30e43e2b1f441640a1ae3 Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Fri, 1 Nov 2019 21:02:31 +0100 Subject: [PATCH] * Added -6 flag to prefer TCP over IPv6, if available. --- telehttpd.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/telehttpd.c b/telehttpd.c index 27298e0..965846c 100644 --- a/telehttpd.c +++ b/telehttpd.c @@ -36,6 +36,7 @@ struct { char *origin; char *idxfile; bool retry; + bool prefv6; } cfg = { "8837", "*", @@ -43,6 +44,7 @@ struct { "http://localhost", "index.html", false, + false }; static volatile struct telemetry_state_t *telemetry; @@ -332,7 +334,9 @@ static int serve_http(void) { pthread_t tid; pthread_attr_t attr; - ss = tcp_open_server( cfg.iface, cfg.port ); + ss = cfg.prefv6 ? tcp6_open_server( cfg.iface, cfg.port ) : -1; + if ( ss < 0 ) + ss = tcp_open_server( cfg.iface, cfg.port ); if ( ss < 0 ) { char ebuf[200]; net_strerror( ss, ebuf, sizeof ebuf ); @@ -379,6 +383,7 @@ static void usage( const char *me ) " Options:\n" " -b str bind interface; default: * (all)\n" " -h str host name; default: localhost\n" + " -6 prefer TCP over IPv6, if available\n" " -i file file to serve as index file; default: index.html\n" " -o str origin; default: http://localhost\n" " -p N TCP listen port; default: 8837\n" @@ -388,7 +393,7 @@ static void usage( const char *me ) static int config( int argc, char *argv[] ) { int opt; - const char *ostr = "+rb:h:i:o:p:"; + const char *ostr = "+6rb:h:i:o:p:"; while ( -1 != ( opt = getopt( argc, argv, ostr ) ) ) { switch ( opt ) { @@ -410,6 +415,9 @@ static int config( int argc, char *argv[] ) { case 'r': cfg.retry = true; break; + case '6': + cfg.prefv6 = true; + break; case ':': EPRINT( "Missing argument for option '%c'\n", optopt ); usage( argv[0] ); -- 2.30.2