server/: Split peer and admin initialization into smaller pieces.
[tripe] / server / peer.c
index a8099e4..27ac4a7 100644 (file)
@@ -29,7 +29,7 @@
 
 /*----- Global state ------------------------------------------------------*/
 
-sel_file udpsock[NADDRFAM];
+udpsocket udpsock[NADDRFAM];
 
 /*----- Static variables --------------------------------------------------*/
 
@@ -527,9 +527,14 @@ buf *p_txstart(peer *p, unsigned msg)
 int p_txaddr(const addr *a, const void *p, size_t sz)
 {
   socklen_t sasz = addrsz(a);
+  int i;
 
+  if ((i = afix(a->sa.sa_family)) < 0) {
+    a_warn("PEER", "?ADDR", a, "disabled-address-family", A_END);
+    return (-1);
+  }
   IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet", p, sz); )
-  if (sendto(sock.fd, p, sz, 0, &a->sa, sasz) < 0) {
+  if (sendto(udpsock[i].sf.fd, p, sz, 0, &a->sa, sasz) < 0) {
     a_warn("PEER", "?ADDR", a, "socket-write-error", "?ERRNO", A_END);
     return (-1);
   }
@@ -557,7 +562,7 @@ static int p_dotxend(peer *p)
   }
   IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
                                 BBASE(&p->b), BLEN(&p->b)); )
-  if (sendto(udpsock[p->afix].fd, BBASE(&p->b), BLEN(&p->b),
+  if (sendto(udpsock[p->afix].sf.fd, BBASE(&p->b), BLEN(&p->b),
             0, &p->spec.sa.sa, sasz) < 0) {
     a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END);
     return (0);
@@ -811,7 +816,7 @@ void p_setifname(peer *p, const char *name)
 
 const addr *p_addr(peer *p) { return (&p->spec.sa); }
 
-/* --- @p_init@ --- *
+/* --- @p_bind@ --- *
  *
  * Arguments:  @struct addrinfo *ailist@ = addresses to bind to
  *
@@ -820,7 +825,7 @@ const addr *p_addr(peer *p) { return (&p->spec.sa); }
  * Use:                Initializes the peer system; creates the socket.
  */
 
-void p_init(struct addrinfo *ailist)
+void p_bind(struct addrinfo *ailist)
 {
   int fd;
   int len = PKBUFSZ;
@@ -831,11 +836,11 @@ void p_init(struct addrinfo *ailist)
   addr a;
   socklen_t sz;
 
-  for (i = 0; i < NADDRFAM; i++) udpsock[i].fd = -1;
+  for (i = 0; i < NADDRFAM; i++) udpsock[i].sf.fd = -1;
 
   for (ai = ailist; ai; ai = ai->ai_next) {
     if ((i = afix(ai->ai_family)) < 0) continue;
-    if (udpsock[i].fd != -1) continue;
+    if (udpsock[i].sf.fd != -1) continue;
 
     /* --- Note on socket buffer sizes --- *
      *
@@ -863,38 +868,36 @@ void p_init(struct addrinfo *ailist)
          strerror(errno));
     }
     fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
-    sel_initfile(&sel, &udpsock[i], fd, SEL_READ, p_read, 0);
-    sel_addfile(&udpsock[i]);
+    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);
     }
   }
 
-  sym_create(&byname);
-  am_create(&byaddr);
 }
 
-/* --- @p_port@ --- *
+/* --- @p_init@ --- *
  *
- * Arguments:  @int i@ = address family index to retrieve
+ * Arguments:  ---
+ *
+ * Returns:    ---
  *
- * Returns:    Port number used for socket.
+ * Use:                Initializes the peer system.
  */
 
-unsigned p_port(int i)
+void p_init(void)
 {
-  addr a;
-  socklen_t sz = sizeof(addr);
-
-  if (getsockname(udpsock[i].fd, &a.sa, &sz))
-    die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
-  return (getport(&a));
+  sym_create(&byname);
+  am_create(&byaddr);
 }
 
 /* --- @p_keepalive@ --- *