chiark / gitweb /
server/admin.h: Consolidate address construction during resolution.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 16 Sep 2017 16:06:41 +0000 (17:06 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 28 Jun 2018 23:26:40 +0000 (00:26 +0100)
Previously, setting up the socket address was kind of scattered
throughout the resolver code: the address family was set up front; the
port number stashed a bit later; and then the address plugged in once
the resolution job finished.

Instead, keep the port number separate once we've worked out what it is,
and build the entire socket address in one go at each site (once in the
background-resolver callback, and once for parsing a numerical address).

server/admin.c
server/tripe.h

index 8cb44a3c711fbb3611c0be662a939f898e606f13..c5fed8b0781cdfa7a0bb986f05e09893f6b0b26b 100644 (file)
@@ -1028,7 +1028,9 @@ static void a_resolved(struct hostent *h, void *v)
     r->func(r, ARES_FAIL);
   } else {
     T( trace(T_ADMIN, "admin: resop %s ok", BGTAG(r)); )
+    r->sa.sin.sin_family = AF_INET;
     memcpy(&r->sa.sin.sin_addr, h->h_addr, sizeof(struct in_addr));
+    r->sa.sin.sin_port = htons(r->port);
     r->func(r, ARES_OK);
   }
   sel_rmtimer(&r->t);
@@ -1130,7 +1132,7 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
     a_fail(a, "invalid-port", "%lu", pt, A_END);
     goto fail;
   }
-  r->sa.sin.sin_port = htons(pt);
+  r->port = pt;
 
   /* --- Report backgrounding --- *
    *
@@ -1147,6 +1149,8 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
 
   if (inet_aton(av[i], &r->sa.sin.sin_addr)) {
     T( trace(T_ADMIN, "admin: resop %s done the easy way", BGTAG(r)); )
+    r->sa.sin.sin_family = AF_INET;
+    r->sa.sin.sin_port = htons(r->port);
     func(r, ARES_OK);
     xfree(r->addr);
     a_bgrelease(&r->bg);
index 0eae8dc5bbea304ac9246d5b2dc8f7702be62758..baae9078077aaafefdad79accf0577bb22fcd717 100644 (file)
@@ -692,6 +692,7 @@ typedef struct admin_resop {
   bres_client r;                       /* Background resolver task */
   sel_timer t;                         /* Timer for resolver */
   addr sa;                             /* Socket address */
+  unsigned port;                       /* Port number chosen */
   size_t sasz;                         /* Socket address size */
   void (*func)(struct admin_resop *, int); /* Handler */
 } admin_resop;