chiark / gitweb /
udp: Use <bsd/sys/queue.h> for notify lists
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Sep 2014 12:24:11 +0000 (13:24 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 6 Oct 2014 16:53:13 +0000 (17:53 +0100)
This makes the code clearer, shorter and more typesafe.

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

index 7d1edb0737d6709714973f9b438a66bddd5a08c5..629851b2ac183234cf00ba8ca773c1e730183b2d 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -19,6 +19,8 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
+#include <bsd/sys/queue.h>
+
 #define MAX_PEER_ADDRS 5
 /* send at most this many copies; honour at most that many addresses */
 
 #define MAX_PEER_ADDRS 5
 /* send at most this many copies; honour at most that many addresses */
 
diff --git a/udp.c b/udp.c
index 80299ac37f450f0ec21fc3f49d70defb01405a9a..8ae5671bd64a38a16914997ffe035f65e0b4d26f 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -30,11 +30,12 @@ static comm_request_notify_fn request_notify;
 static comm_release_notify_fn release_notify;
 static comm_sendmsg_fn udp_sendmsg;
 
 static comm_release_notify_fn release_notify;
 static comm_sendmsg_fn udp_sendmsg;
 
-struct notify_list {
+struct comm_notify_entry {
     comm_notify_fn *fn;
     void *state;
     comm_notify_fn *fn;
     void *state;
-    struct notify_list *next;
+    LIST_ENTRY(comm_notify_entry) entry;
 };
 };
+LIST_HEAD(comm_notify_list, comm_notify_entry) notify;
 
 #define MAX_SOCKETS 3 /* 2 ought to do really */
 
 
 #define MAX_SOCKETS 3 /* 2 ought to do really */
 
@@ -51,7 +52,7 @@ struct udp {
     struct udpsock socks[MAX_SOCKETS];
     string_t authbind;
     struct buffer_if *rbuf;
     struct udpsock socks[MAX_SOCKETS];
     string_t authbind;
     struct buffer_if *rbuf;
-    struct notify_list *notify;
+    struct comm_notify_list notify;
     bool_t use_proxy;
     union iaddr proxy;
 };
     bool_t use_proxy;
     union iaddr proxy;
 };
@@ -103,7 +104,7 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
     struct udp *st=state;
     union iaddr from;
     socklen_t fromlen;
     struct udp *st=state;
     union iaddr from;
     socklen_t fromlen;
-    struct notify_list *n;
+    struct comm_notify_entry *n;
     bool_t done;
     int rv;
     int i;
     bool_t done;
     int rv;
     int i;
@@ -144,7 +145,7 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
                ca.ia=from;
                ca.ix=i;
                done=False;
                ca.ia=from;
                ca.ix=i;
                done=False;
-               for (n=st->notify; n; n=n->next) {
+               LIST_FOREACH(n, &st->notify, entry) {
                    if (n->fn(n->state, st->rbuf, &ca)) {
                        done=True;
                        break;
                    if (n->fn(n->state, st->rbuf, &ca)) {
                        done=True;
                        break;
@@ -174,32 +175,24 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
 {
     struct udp *st=commst;
 static void request_notify(void *commst, void *nst, comm_notify_fn *fn)
 {
     struct udp *st=commst;
-    struct notify_list *n;
+    struct comm_notify_entry *n;
     
     n=safe_malloc(sizeof(*n),"request_notify");
     n->fn=fn;
     n->state=nst;
     
     n=safe_malloc(sizeof(*n),"request_notify");
     n->fn=fn;
     n->state=nst;
-    n->next=st->notify;
-    st->notify=n;
+    LIST_INSERT_HEAD(&st->notify, n, entry);
 }
 
 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
 {
     struct udp *st=commst;
 }
 
 static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
 {
     struct udp *st=commst;
-    struct notify_list *n, **p, *t;
+    struct comm_notify_entry *n, *t;
 
     /* XXX untested */
 
     /* XXX untested */
-    p=&st->notify;
-    for (n=st->notify; n; )
-    {
+    LIST_FOREACH_SAFE(n, &st->notify, entry, t) {
        if (n->state==nst && n->fn==fn) {
        if (n->state==nst && n->fn==fn) {
-           t=n;
-           *p=n->next;
-           n=n->next;
-           free(t);
-       } else {
-           p=&n->next;
-           n=n->next;
+           LIST_REMOVE(n, entry);
+           free(n);
        }
     }
 }
        }
     }
 }
@@ -363,6 +356,7 @@ static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
     st->ops.sendmsg=udp_sendmsg;
     st->ops.addr_to_string=addr_to_string;
     st->use_proxy=False;
     st->ops.sendmsg=udp_sendmsg;
     st->ops.addr_to_string=addr_to_string;
     st->use_proxy=False;
+    LIST_INIT(&st->notify);
 
     item=list_elem(args,0);
     if (!item || item->type!=t_dict) {
 
     item=list_elem(args,0);
     if (!item || item->type!=t_dict) {