[PATCH 17/31] udp: Break out udp_make_socket

Ian Jackson ijackson at chiark.greenend.org.uk
Sat Sep 20 01:32:12 BST 2014


Make this into a function by itself and adjust its arguments so that
when we support multiple sockets (for multiple addresses so that we
can have multiple AFs) we can just call it for each one.

No functional change.

Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
---
 udp.c |   31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/udp.c b/udp.c
index b731dc6..1056003 100644
--- a/udp.c
+++ b/udp.c
@@ -203,25 +203,22 @@ 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,
+			    const union iaddr *addr)
 {
-    struct udp *st=sst;
-    union iaddr addr;
-
-    st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
-    if (st->fd<0) {
+    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);
     }
-    if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
+    if (fcntl(us->fd, F_SETFD, FD_CLOEXEC)==-1) {
 	fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
 		     st->loc.file,st->loc.line);
     }
 
-    addr=st->addr;
     if (st->authbind) {
 	pid_t c;
 	int status;
@@ -234,10 +231,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",
@@ -247,7 +244,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);
 	}
@@ -264,7 +261,7 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
 		  st->loc.line, WEXITSTATUS(status));
 	}
     } else {
-	if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
+	if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) {
 	    fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
 	}
     }
@@ -272,6 +269,12 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
     register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
 }
 
+static void udp_phase_hook(void *sst, uint32_t new_phase)
+{
+    struct udp *st=sst;
+    udp_make_socket(st,st,&st->addr);
+}
+
 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
 			 list_t *args)
 {
-- 
1.7.10.4




More information about the sgo-software-discuss mailing list