chiark / gitweb /
comm: Formalise interface to udp sockets
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Sep 2014 23:51:58 +0000 (00:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 8 Oct 2014 17:25:17 +0000 (18:25 +0100)
Have the poll registration done by the udpcommon/udpsocks code, rather
than by udp.c.  This means we can abolish the two wrapper functions,
but we do need an extra pointer in a udpsocks to find the udpcommon.

No overall functional change.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
comm-common.h
udp.c

index ba49254f47493dc48b75de30b91c226d48120443..44bb6c3d5fe080b7c3d471d497bbb874f44b7941 100644 (file)
@@ -70,6 +70,8 @@ struct udpsock {
 struct udpsocks {
     int n_socks;
     struct udpsock socks[UDP_MAX_SOCKETS];
 struct udpsocks {
     int n_socks;
     struct udpsock socks[UDP_MAX_SOCKETS];
+    /* private for udp_socks_* */
+    struct udpcommon *uc; /* link to parent, for cfg, notify list, etc. */
 };
 
 struct udpcommon {
 };
 
 struct udpcommon {
@@ -80,12 +82,8 @@ struct udpcommon {
     union iaddr proxy;
 };
 
     union iaddr proxy;
 };
 
-int udp_socks_beforepoll(struct udpsocks *s,
-                        struct pollfd *fds, int *nfds_io,
-                        int *timeout_io);
-
-void udp_socks_afterpoll(struct udpcommon *u, struct udpsocks *s,
-                        struct pollfd *fds, int nfds);
+void udp_make_socket(struct udpcommon *uc, struct udpsock *us);
+void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks);
 
 #define UDP_APPLY_STANDARD(st,uc,desc)                                 \
     (uc)->use_proxy=False;                                             \
 
 #define UDP_APPLY_STANDARD(st,uc,desc)                                 \
     (uc)->use_proxy=False;                                             \
diff --git a/udp.c b/udp.c
index 9101e497a5097bb760214c830507b69ed9ca34c7..8780c7e9f22f29db72d6918e38752c3dedfec176 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -25,8 +25,6 @@
 #include "magic.h"
 #include "comm-common.h"
 
 #include "magic.h"
 #include "comm-common.h"
 
-static beforepoll_fn udp_beforepoll;
-static afterpoll_fn udp_afterpoll;
 static comm_sendmsg_fn udp_sendmsg;
 
 struct udp {
 static comm_sendmsg_fn udp_sendmsg;
 
 struct udp {
@@ -61,10 +59,10 @@ static const char *udp_addr_to_string(void *commst, const struct comm_addr *ca)
     return sbuf;
 }
 
     return sbuf;
 }
 
-int udp_socks_beforepoll(struct udpsocks *socks,
-                        struct pollfd *fds, int *nfds_io,
-                        int *timeout_io)
+static int udp_socks_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
+                               int *timeout_io)
 {
 {
+    struct udpsocks *socks=state;
     int i;
     BEFOREPOLL_WANT_FDS(socks->n_socks);
     for (i=0; i<socks->n_socks; i++) {
     int i;
     BEFOREPOLL_WANT_FDS(socks->n_socks);
     for (i=0; i<socks->n_socks; i++) {
@@ -74,16 +72,10 @@ int udp_socks_beforepoll(struct udpsocks *socks,
     return 0;
 }
 
     return 0;
 }
 
-static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
-                         int *timeout_io)
-{
-    struct udp *st=state;
-    return udp_socks_beforepoll(&st->socks,fds,nfds_io,timeout_io);
-}
-
-void udp_socks_afterpoll(struct udpcommon *uc, struct udpsocks *socks,
-                        struct pollfd *fds, int nfds)
+static void udp_socks_afterpoll(void *state, struct pollfd *fds, int nfds)
 {
 {
+    struct udpsocks *socks=state;
+    struct udpcommon *uc=socks->uc;
     union iaddr from;
     socklen_t fromlen;
     bool_t done;
     union iaddr from;
     socklen_t fromlen;
     bool_t done;
@@ -149,12 +141,6 @@ void udp_socks_afterpoll(struct udpcommon *uc, struct udpsocks *socks,
     }
 }
 
     }
 }
 
-static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
-{
-    struct udp *st=state;
-    return udp_socks_afterpoll(&st->uc,&st->socks,fds,nfds);
-}
-
 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
                          const struct comm_addr *dest)
 {
 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
                          const struct comm_addr *dest)
 {
@@ -197,10 +183,9 @@ static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
     return True;
 }
 
     return True;
 }
 
-static void udp_make_socket(struct udp *st, struct udpsock *us)
+void udp_make_socket(struct udpcommon *uc, struct udpsock *us)
 {
     const union iaddr *addr=&us->addr;
 {
     const union iaddr *addr=&us->addr;
-    struct udpcommon *uc=&st->uc;
     struct commcommon *cc=&uc->cc;
 
     us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
     struct commcommon *cc=&uc->cc;
 
     us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
@@ -286,15 +271,22 @@ static void udp_make_socket(struct udp *st, struct udpsock *us)
     }
 }
 
     }
 }
 
+void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks)
+{
+    socks->uc=uc;
+    register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
+}
+
 static void udp_phase_hook(void *sst, uint32_t new_phase)
 {
     struct udp *st=sst;
     struct udpsocks *socks=&st->socks;
 static void udp_phase_hook(void *sst, uint32_t new_phase)
 {
     struct udp *st=sst;
     struct udpsocks *socks=&st->socks;
+    struct udpcommon *uc=&st->uc;
     int i;
     for (i=0; i<socks->n_socks; i++)
     int i;
     for (i=0; i<socks->n_socks; i++)
-       udp_make_socket(st,&socks->socks[i]);
+       udp_make_socket(uc,&socks->socks[i]);
 
 
-    register_for_poll(st,udp_beforepoll,udp_afterpoll,"udp");
+    udp_socks_register(uc,socks);
 }
 
 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
 }
 
 static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,