]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/statd/sm-notify.c
sm-notify: Use getaddrinfo(3) to create bind address in smn_create_socket()
[nfs-utils.git] / utils / statd / sm-notify.c
index 462ba7987fef96036ba79f7662694787e37fac76..259db09b5c211e7af0edb7268a87ef3ccaff80b0 100644 (file)
@@ -49,11 +49,12 @@ struct nsm_host {
 
 static char            nsm_hostname[256];
 static int             nsm_state;
+static int             nsm_family = AF_INET;
 static int             opt_debug = 0;
 static _Bool           opt_update_state = true;
 static unsigned int    opt_max_retry = 15 * 60;
-static char *          opt_srcaddr = 0;
-static uint16_t                opt_srcport = 0;
+static char *          opt_srcaddr = NULL;
+static char *          opt_srcport = NULL;
 
 static void            notify(const int sock);
 static int             notify_host(int, struct nsm_host *);
@@ -141,6 +142,132 @@ smn_get_host(const char *hostname,
        return 1;
 }
 
+#ifdef IPV6_SUPPORTED
+static int smn_socket(void)
+{
+       int sock;
+
+       /*
+        * Use an AF_INET socket if IPv6 is disabled on the
+        * local system.
+        */
+       sock = socket(AF_INET6, SOCK_DGRAM, 0);
+       if (sock == -1) {
+               if (errno != EAFNOSUPPORT) {
+                       xlog(L_ERROR, "Failed to create RPC socket: %m");
+                       return -1;
+               }
+               sock = socket(AF_INET, SOCK_DGRAM, 0);
+               if (sock < 0) {
+                       xlog(L_ERROR, "Failed to create RPC socket: %m");
+                       return -1;
+               }
+       } else
+               nsm_family = AF_INET6;
+
+       if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
+               xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
+               goto out_close;
+       }
+
+       /*
+        * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4.  However,
+        * since sm-notify open-codes all of its RPC support, it can
+        * use a single socket and let the local network stack provide
+        * the correct mapping between address families automatically.
+        * This is the same thing that is done in the kernel.
+        */
+       if (nsm_family == AF_INET6) {
+               const int zero = 0;
+               socklen_t zerolen = (socklen_t)sizeof(zero);
+
+               if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
+                                       (char *)&zero, zerolen) == -1) {
+                       xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
+                       goto out_close;
+               }
+       }
+
+       return sock;
+
+out_close:
+       (void)close(sock);
+       return -1;
+}
+#else  /* !IPV6_SUPPORTED */
+static int smn_socket(void)
+{
+       int sock;
+
+       sock = socket(AF_INET, SOCK_DGRAM, 0);
+       if (sock == -1) {
+               xlog(L_ERROR, "Failed to create RPC socket: %m");
+               return -1;
+       }
+
+       if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
+               xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
+               (void)close(sock);
+               return -1;
+       }
+
+       return sock;
+}
+#endif /* !IPV6_SUPPORTED */
+
+/*
+ * If admin specified a source address or srcport, then convert those
+ * to a sockaddr and return it.   Otherwise, return an ANYADDR address.
+ */
+__attribute_malloc__
+static struct addrinfo *
+smn_bind_address(const char *srcaddr, const char *srcport)
+{
+       struct addrinfo *ai = NULL;
+       struct addrinfo hint = {
+               .ai_flags       = AI_NUMERICSERV,
+               .ai_family      = nsm_family,
+               .ai_protocol    = (int)IPPROTO_UDP,
+       };
+       int error;
+
+       if (srcaddr == NULL)
+               hint.ai_flags |= AI_PASSIVE;
+
+       if (srcport == NULL)
+               error = getaddrinfo(srcaddr, "", &hint, &ai);
+       else
+               error = getaddrinfo(srcaddr, srcport, &hint, &ai);
+       if (error != 0) {
+               xlog(L_ERROR,
+                       "Invalid bind address or port for RPC socket: %s",
+                               gai_strerror(error));
+               return NULL;
+       }
+
+       return ai;
+}
+
+#ifdef HAVE_LIBTIRPC
+static int
+smn_bindresvport(int sock, struct sockaddr *sap)
+{
+       return bindresvport_sa(sock, sap);
+}
+
+#else  /* !HAVE_LIBTIRPC */
+static int
+smn_bindresvport(int sock, struct sockaddr *sap)
+{
+       if (sap->sa_family != AF_INET) {
+               errno = EAFNOSUPPORT;
+               return -1;
+       }
+
+       return bindresvport(sock, (struct sockaddr_in *)(char *)sap);
+}
+#endif /* !HAVE_LIBTIRPC */
+
 /*
  * Prepare a socket for sending RPC requests
  *
@@ -148,62 +275,53 @@ smn_get_host(const char *hostname,
  * an error occurs.
  */
 static int
-smn_create_socket(const char *srcaddr, const uint16_t srcport)
+smn_create_socket(const char *srcaddr, const char *srcport)
 {
-       struct sockaddr_storage address;
-       struct sockaddr *local_addr = (struct sockaddr *)&address;
        int sock, retry_cnt = 0;
+       struct addrinfo *ai;
 
 retry:
-       sock = socket(AF_INET, SOCK_DGRAM, 0);
-       if (sock < 0) {
-               xlog(L_ERROR, "Failed to create RPC socket: %m");
+       sock = smn_socket();
+       if (sock == -1)
                return -1;
-       }
-       fcntl(sock, F_SETFL, O_NONBLOCK);
-
-       memset(&address, 0, sizeof(address));
-       local_addr->sa_family = AF_INET;        /* Default to IPv4 */
-
-       /* Bind source IP if provided on command line */
-       if (srcaddr) {
-               struct addrinfo *ai = smn_lookup(srcaddr);
-               if (!ai) {
-                       xlog(L_ERROR,
-                               "Not a valid hostname or address: \"%s\"",
-                               srcaddr);
-                       (void)close(sock);
-                       return -1;
-               }
 
-               /* We know it's IPv4 at this point */
-               memcpy(local_addr, ai->ai_addr, ai->ai_addrlen);
-
-               freeaddrinfo(ai);
+       ai = smn_bind_address(srcaddr, srcport);
+       if (ai == NULL) {
+               (void)close(sock);
+               return -1;
        }
 
        /* Use source port if provided on the command line,
         * otherwise use bindresvport */
        if (srcport) {
-               nfs_set_port(local_addr, srcport);
-               if (bind(sock, local_addr, sizeof(struct sockaddr_in)) < 0) {
+               if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
                        xlog(L_ERROR, "Failed to bind RPC socket: %m");
+                       freeaddrinfo(ai);
                        (void)close(sock);
                        return -1;
                }
        } else {
                struct servent *se;
-               struct sockaddr_in *sin = (struct sockaddr_in *)local_addr;
-               (void) bindresvport(sock, sin);
+
+               if (smn_bindresvport(sock, ai->ai_addr) == -1) {
+                       xlog(L_ERROR,
+                               "bindresvport on RPC socket failed: %m");
+                       freeaddrinfo(ai);
+                       (void)close(sock);
+                       return -1;
+               }
+
                /* try to avoid known ports */
-               se = getservbyport(sin->sin_port, "udp");
-               if (se && retry_cnt < 100) {
+               se = getservbyport((int)nfs_get_port(ai->ai_addr), "udp");
+               if (se != NULL && retry_cnt < 100) {
                        retry_cnt++;
-                       close(sock);
+                       freeaddrinfo(ai);
+                       (void)close(sock);
                        goto retry;
                }
        }
 
+       freeaddrinfo(ai);
        return sock;
 }
 
@@ -234,7 +352,7 @@ main(int argc, char **argv)
                        opt_update_state = false;
                        break;
                case 'p':
-                       opt_srcport = atoi(optarg);
+                       opt_srcport = optarg;
                        break;
                case 'v':
                        opt_srcaddr = optarg;