* Added -6 flag to prefer TCP over IPv6, if available.
authorUrban Wallasch <urban.wallasch@freenet.de>
Fri, 1 Nov 2019 20:02:31 +0000 (21:02 +0100)
committerUrban Wallasch <urban.wallasch@freenet.de>
Fri, 1 Nov 2019 20:02:31 +0000 (21:02 +0100)
telehttpd.c

index 27298e058b364d83a165134beaca2d2047969bf4..965846c981680037c1b00c8c22e43a85c9660ccc 100644 (file)
@@ -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] );