noip.c: Factor out the non-implicit-binding parts of `do_implicit_bind'.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 6 Jun 2018 19:22:04 +0000 (20:22 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 6 Jun 2018 19:22:04 +0000 (20:22 +0100)
The function's other responsibilities are taken over by a new function
`fixup_client_socket'.  When I did this, I anticipated that I was going
to have to make the latter rather more complicated, but it turns out
that it's not actually that far off.

No functional change, though the new function takes a different approach
to producing the same effective control flow.

noip.c

diff --git a/noip.c b/noip.c
index 1d40850..fd86cb3 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -963,43 +963,67 @@ static void dump_impbind_list(void)
  * SA.  Assign it a local address so that getpeername(2) does something
  * useful.
  */
-static int do_implicit_bind(int sk, const struct sockaddr **sa,
-                           socklen_t *len, struct sockaddr_un *sun)
+static int do_implicit_bind(int sk, const struct sockaddr *sa)
 {
   address addr;
-  socklen_t mylen = sizeof(*sun);
+  struct sockaddr_un sun;
   const impbind *i;
   Dpid;
 
-  if (acl_allows_p(connect_real, *sa)) {
-    if (fixup_real_ip_socket(sk, (*sa)->sa_family, 0)) return (-1);
-  } else {
-    if (real_getsockname(sk, SA(sun), &mylen) < 0) return (-1);
-    if (sun->sun_family == AF_UNIX) {
-      if (mylen < sizeof(*sun)) ((char *)sun)[mylen] = 0;
-      if (!sun->sun_path[0]) {
-       D( fprintf(stderr, "noip(%d): checking impbind list...\n", pid); )
-       for (i = impbinds; i; i = i->next) {
-         D( dump_impbind(i); )
-         if ((*sa)->sa_family == i->af &&
-             sockaddr_in_range_p(*sa, &i->minaddr, &i->maxaddr)) {
-           D( fprintf(stderr, "noip(%d): match!\n", pid); )
-           addr.sa.sa_family = (*sa)->sa_family;
-           ipaddr_to_sockaddr(&addr.sa, &i->bindaddr);
-           goto found;
-         }
-       }
-       D( fprintf(stderr, "noip(%d): no match; using wildcard\n", pid); )
-       wildcard_address((*sa)->sa_family, &addr.sa);
-      found:
-       encode_inet_addr(sun, &addr.sa, ENCF_FRESH);
-       if (real_bind(sk, SA(sun), SUN_LEN(sun))) return (-1);
-      }
-      encode_inet_addr(sun, *sa, 0);
-      *sa = SA(sun);
-      *len = SUN_LEN(sun);
+  D( fprintf(stderr, "noip(%d): checking impbind list...\n", pid); )
+  for (i = impbinds; i; i = i->next) {
+    D( dump_impbind(i); )
+    if (sa->sa_family == i->af &&
+       sockaddr_in_range_p(sa, &i->minaddr, &i->maxaddr)) {
+      D( fprintf(stderr, "noip(%d): match!\n", pid); )
+      addr.sa.sa_family = sa->sa_family;
+      ipaddr_to_sockaddr(&addr.sa, &i->bindaddr);
+      goto found;
     }
   }
+  D( fprintf(stderr, "noip(%d): no match; using wildcard\n", pid); )
+  wildcard_address(sa->sa_family, &addr.sa);
+found:
+  encode_inet_addr(&sun, &addr.sa, ENCF_FRESH);
+  if (real_bind(sk, SA(&sun), SUN_LEN(&sun))) return (-1);
+  return (0);
+}
+
+/* The socket SK is about to communicate with the remote address *SA.  Ensure
+ * that the socket has a local address, and adjust *SA to refer to the real
+ * remote endpoint.
+ *
+ * If we need to translate the remote address, then the Unix-domain endpoint
+ * address will end in *SUN, and *SA will be adjusted to point to it.
+ */
+static int fixup_client_socket(int sk, const struct sockaddr **sa_r,
+                              socklen_t *len_r, struct sockaddr_un *sun)
+{
+  socklen_t mylen = sizeof(*sun);
+  const struct sockaddr *sa = *sa_r;
+
+  /* If we're allowed to talk to a real remote endpoint, then fix things up
+   * as necessary and proceed.
+   */
+  if (acl_allows_p(connect_real, sa)) {
+    if (fixup_real_ip_socket(sk, (*sa_r)->sa_family, 0)) return (-1);
+    return (0);
+  }
+
+  /* If this isn't a Unix-domain socket then there's nothing to do. */
+  if (real_getsockname(sk, SA(sun), &mylen) < 0) return (-1);
+  if (sun->sun_family != AF_UNIX) return (0);
+  if (mylen < sizeof(*sun)) ((char *)sun)[mylen] = 0;
+
+  /* Speaking of which, if we don't have a local address, then we should
+   * arrange one now.
+   */
+  if (!sun->sun_path[0] && do_implicit_bind(sk, sa)) return (-1);
+
+  /* And then come up with a remote address. */
+  encode_inet_addr(sun, sa, 0);
+  *sa_r = SA(sun);
+  *len_r = SUN_LEN(sun);
   return (0);
 }
 
@@ -1584,7 +1608,7 @@ int connect(int sk, const struct sockaddr *sa, socklen_t len)
   } else {
     D( fprintf(stderr, " -> checking...\n"); )
     PRESERVING_ERRNO({
-      do_implicit_bind(sk, &sa, &len, &sun);
+      fixup_client_socket(sk, &sa, &len, &sun);
     });
     D( fprintf(stderr, "noip(%d): CONNECT ...", pid); )
     rc = real_connect(sk, sa, len);
@@ -1617,7 +1641,7 @@ ssize_t sendto(int sk, const void *buf, size_t len, int flags,
   else {
     D( fprintf(stderr, " -> checking...\n"); )
     PRESERVING_ERRNO({
-      do_implicit_bind(sk, &to, &tolen, &sun);
+      fixup_client_socket(sk, &to, &tolen, &sun);
     });
     D( fprintf(stderr, "noip(%d): SENDTO ...", pid); )
   }
@@ -1677,7 +1701,7 @@ ssize_t sendmsg(int sk, const struct msghdr *msg, int flags)
     D( fprintf(stderr, " -> checking...\n"); )
     PRESERVING_ERRNO({
       mymsg = *msg;
-      do_implicit_bind(sk, &sa, &mymsg.msg_namelen, &sun);
+      fixup_client_socket(sk, &sa, &mymsg.msg_namelen, &sun);
       mymsg.msg_name = SA(sa);
       msg = &mymsg;
     });