udp: Break out udp_make_socket
[secnet] / udp.c
diff --git a/udp.c b/udp.c
index 4216922..33a8707 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -83,7 +83,6 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
 
     if (nfds && (fds->revents & POLLIN)) {
        do {
-           FILLZERO(from);
            fromlen=sizeof(from);
            BUF_ASSERT_FREE(st->rbuf);
            BUF_ALLOC(st->rbuf,"udp_afterpoll");
@@ -110,7 +109,6 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
                    memcpy(&from.sin.sin_port,buf_unprepend(st->rbuf,2),2);
                }
                struct comm_addr ca;
-               FILLZERO(ca);
                ca.comm=&st->ops;
                ca.ia=from;
                done=False;
@@ -202,22 +200,19 @@ static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
     return True;
 }
 
-static void udp_phase_hook(void *sst, uint32_t new_phase)
+static void udp_make_socket(struct udp *st, struct udp *us)
 {
-    struct udp *st=sst;
-    union iaddr addr;
-
-    st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
-    if (st->fd<0) {
+    const union iaddr *addr=&us->addr;
+    us->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    if (us->fd<0) {
        fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
     }
-    if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
+    if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1) {
        fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
                     st->loc.file,st->loc.line);
     }
-    setcloexec(st->fd);
+    setcloexec(us->fd);
 
-    addr=st->addr;
     if (st->authbind) {
        pid_t c;
        int status;
@@ -230,10 +225,10 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
        }
        if (c==0) {
            char *argv[4], addrstr[9], portstr[5];
-           switch (addr.sa.sa_family) {
+           switch (addr->sa.sa_family) {
            case AF_INET:
-               sprintf(addrstr,"%08lX",(long)addr.sin.sin_addr.s_addr);
-               sprintf(portstr,"%04X",addr.sin.sin_port);
+               sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
+               sprintf(portstr,"%04X",addr->sin.sin_port);
                break;
            default:
                fatal("udp (%s:%d): unsupported address family for authbind",
@@ -243,7 +238,7 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
            argv[1]=addrstr;
            argv[2]=portstr;
            argv[3]=NULL;
-           dup2(st->fd,0);
+           dup2(us->fd,0);
            execvp(st->authbind,argv);
            _exit(255);
        }
@@ -260,11 +255,16 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
                  st->loc.line, WEXITSTATUS(status));
        }
     } else {
-       if (bind(st->fd, &addr.sa, iaddr_socklen(&addr))!=0) {
+       if (bind(st->fd, &addr->sa, iaddr_socklen(addr))!=0) {
            fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
        }
     }
+}
 
+static void udp_phase_hook(void *sst, uint32_t new_phase)
+{
+    struct udp *st=sst;
+    udp_make_socket(st,st);
     register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
 }
 
@@ -288,7 +288,6 @@ static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
     st->ops.release_notify=release_notify;
     st->ops.sendmsg=udp_sendmsg;
     st->ops.addr_to_string=addr_to_string;
-    FILLZERO(st->addr);
     st->use_proxy=False;
 
     i=list_elem(args,0);
@@ -306,7 +305,6 @@ static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
     l=dict_lookup(d,"proxy");
     if (l) {
        st->use_proxy=True;
-       FILLZERO(st->proxy);
        st->proxy.sa.sa_family=AF_INET;
        i=list_elem(l,0);
        if (!i || i->type!=t_string) {