server/: Record port number with UDP socket, rather than querying.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 12 May 2018 09:44:14 +0000 (10:44 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 25 Jan 2019 12:10:31 +0000 (12:10 +0000)
The old `p_port' function queried getsockname(2) at runtime, and bailed
in the (very unlikely) event that it failed.  Avoid this situation by
recording the port number when the socket is set up and just using the
recorded value when it's needed.

This was the only remaining abnormal exit after the server has started.

server/admin.c
server/peer.c
server/tripe.h

index 24f8397..85e8c23 100644 (file)
@@ -1879,7 +1879,7 @@ static void acmd_port(admin *a, unsigned ac, char *av[])
       if (udpsock[i].sf.fd >= 0) goto found;
     abort();
   }
-  a_info(a, "%u", p_port(i), A_END);
+  a_info(a, "%u", udpsock[i].port, A_END);
   a_ok(a);
 }
 
index d415567..377dcc5 100644 (file)
@@ -871,13 +871,15 @@ void p_init(struct addrinfo *ailist)
     sel_initfile(&sel, &udpsock[i].sf, fd, SEL_READ, p_read, 0);
     sel_addfile(&udpsock[i].sf);
     T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
-    if (!port) {
+    if (port)
+      udpsock[i].port = port;
+    else {
       sz = sizeof(a);
       if (getsockname(fd, &a.sa, &sz)) {
        die(EXIT_FAILURE, "failed to read local socket address: %s",
            strerror(errno));
       }
-      lastport = getport(&a);
+      udpsock[i].port = lastport = getport(&a);
     }
   }
 
@@ -885,23 +887,6 @@ void p_init(struct addrinfo *ailist)
   am_create(&byaddr);
 }
 
-/* --- @p_port@ --- *
- *
- * Arguments:  @int i@ = address family index to retrieve
- *
- * Returns:    Port number used for socket.
- */
-
-unsigned p_port(int i)
-{
-  addr a;
-  socklen_t sz = sizeof(addr);
-
-  if (getsockname(udpsock[i].sf.fd, &a.sa, &sz))
-    die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
-  return (getport(&a));
-}
-
 /* --- @p_keepalive@ --- *
  *
  * Arguments:  @struct timeval *now@ = the current time
index a083b71..1effb95 100644 (file)
@@ -678,6 +678,7 @@ typedef struct peer_iter { sym_iter i; } peer_iter;
 
 typedef struct udpsocket {
   sel_file sf;                         /* Selector for the socket */
+  unsigned port;                       /* Chosen port number */
 } udpsocket;
 
 typedef struct ping {
@@ -1649,15 +1650,6 @@ extern const addr *p_addr(peer */*p*/);
 
 extern void p_init(struct addrinfo */*ailist*/);
 
-/* --- @p_port@ --- *
- *
- * Arguments:  @int i@ = address family index to retrieve
- *
- * Returns:    Port number used for socket.
- */
-
-extern unsigned p_port(int /*i*/);
-
 /* --- @p_create@ --- *
  *
  * Arguments:  @peerspec *spec@ = information about this peer