Allow to use IPv4 addr with --bindhost
authorRobert Swiecki <robert@swiecki.net>
Mon, 19 Jun 2017 20:35:57 +0000 (22:35 +0200)
committerRobert Swiecki <robert@swiecki.net>
Mon, 19 Jun 2017 20:35:57 +0000 (22:35 +0200)
cmdline.c
net.c

index dc43d2e85f928907c668d70501c99a008fc9d856..c5e777e5b092773166a83376321f9eac78e66571 100644 (file)
--- a/cmdline.c
+++ b/cmdline.c
@@ -74,7 +74,7 @@ struct custom_option custom_opts[] = {
     {{"hostname", required_argument, NULL, 'H'}, "UTS name (hostname) of the jail (default: 'NSJAIL')"},
     {{"cwd", required_argument, NULL, 'D'}, "Directory in the namespace the process will run (default: '/')"},
     {{"port", required_argument, NULL, 'p'}, "TCP port to bind to (enables MODE_LISTEN_TCP) (default: 0)"},
-    {{"bindhost", required_argument, NULL, 0x604}, "IP address port to bind to (only in [MODE_LISTEN_TCP]), '::ffff:127.0.0.1' for locahost (default: '::')"},
+    {{"bindhost", required_argument, NULL, 0x604}, "IP address to bind the port to (only in [MODE_LISTEN_TCP]), (default: '::')"},
     {{"max_conns_per_ip", required_argument, NULL, 'i'}, "Maximum number of connections per one IP (only in [MODE_LISTEN_TCP]), (default: 0 (unlimited))"},
     {{"log", required_argument, NULL, 'l'}, "Log file (default: use log_fd)"},
     {{"log_fd", required_argument, NULL, 'L'}, "Log FD (default: 2)"},
diff --git a/net.c b/net.c
index b7bd3d5bfba47757add5b00dd1bf05a37b30ffea..08a110a045947931d78acc5545916091340bfab6 100644 (file)
--- a/net.c
+++ b/net.c
@@ -195,9 +195,17 @@ int netGetRecvSocket(const char *bindhost, int port)
                     port);
        }
 
+       char bindaddr[128];
+       snprintf(bindaddr, sizeof(bindaddr), "%s", bindhost);
+       struct in_addr in4a;
+       if (inet_pton(AF_INET, bindaddr, &in4a) == 1) {
+               snprintf(bindaddr, sizeof(bindaddr), "::ffff:%s", bindhost);
+       }
+
        struct in6_addr in6a;
-       if (inet_pton(AF_INET6, bindhost, &in6a) != 1) {
-               PLOG_E("Couldn't convert '%s' into AF_INET6 address", bindhost);
+       if (inet_pton(AF_INET6, bindaddr, &in6a) != 1) {
+               PLOG_E("Couldn't convert '%s' (orig:'%s') into AF_INET6 address", bindaddr,
+                      bindhost);
                return -1;
        }
 
@@ -220,7 +228,7 @@ int netGetRecvSocket(const char *bindhost, int port)
        };
        if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
                close(sockfd);
-               PLOG_E("bind(host:[%s], port:%d)", bindhost, port);
+               PLOG_E("bind(host:[%s (orig:'%s')], port:%d)", bindaddr, bindhost, port);
                return -1;
        }
        if (listen(sockfd, SOMAXCONN) == -1) {