chiark / gitweb /
ipv6: introduce union iaddr
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 26 Feb 2014 15:57:21 +0000 (15:57 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Sep 2014 17:48:11 +0000 (18:48 +0100)
Replace many occurrences of sockaddr_in by a new union, iaddr.

Everywhere that fills in an address has been modified to look into the
subfields of iaddr.  But there is not yet any support for a union
iaddr to contain anything other than a sockaddr_in.  This will be
added gradually in forthcoming patches, starting at consumers and
working back.

Additionally, a couple of places that specified a port and address as
a uint16_t and uint32_t have been converted.

We have changed only transport addresses - that is, addresses on the
public network.  VPN addresses remain IPv4 only.

We provide a few helper functions for manipulating union iaddr, such
as iaddr_to_string (which replaces saddr_to_string).

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

index 5360966a056e348dc11cc543cc8432e78ae58964..f7cd373050fc9f8f3cb17becff2e94fe77af0721 100644 (file)
@@ -44,9 +44,9 @@ static bool_t resolve_request(void *sst, cstring_t name,
        struct comm_addr ca;
        FILLZERO(ca);
        ca.comm=comm;
        struct comm_addr ca;
        FILLZERO(ca);
        ca.comm=comm;
-       ca.sin.sin_family=AF_INET;
-       ca.sin.sin_port=htons(port);
-       if (inet_aton(trimmed,&ca.sin.sin_addr))
+       ca.ia.sin.sin_family=AF_INET;
+       ca.ia.sin.sin_port=htons(port);
+       if (inet_aton(trimmed,&ca.ia.sin.sin_addr))
            cb(cst,&ca,1);
        else
            cb(cst,0,0);
            cb(cst,&ca,1);
        else
            cb(cst,0,0);
@@ -112,10 +112,10 @@ static void resolver_afterpoll(void *sst, struct pollfd *fds, int nfds)
                    /* copy fields individually so we leave holes zeroed: */
                    switch (ra->addr.sa.sa_family) {
                    case AF_INET:
                    /* copy fields individually so we leave holes zeroed: */
                    switch (ra->addr.sa.sa_family) {
                    case AF_INET:
-                       assert(ra->len == sizeof(ca->sin));
-                       ca->sin.sin_family=ra->addr.inet.sin_family;
-                       ca->sin.sin_addr=  ra->addr.inet.sin_addr;
-                       ca->sin.sin_port=  htons(q->port);
+                       assert(ra->len == sizeof(ca->ia.sin));
+                       ca->ia.sin.sin_family=ra->addr.inet.sin_family;
+                       ca->ia.sin.sin_addr=  ra->addr.inet.sin_addr;
+                       ca->ia.sin.sin_port=  htons(q->port);
                        wslot++;
                        break;
                    default:
                        wslot++;
                        break;
                    default:
index 2efd11dda5aadb1aa4ea75edbce144c0a37ab22e..1c88e3a0c744ce5251a6085c38fdad56a189ec89 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -30,6 +30,11 @@ typedef const char *cstring_t;
 #define True  (_Bool)1
 typedef _Bool bool_t;
 
 #define True  (_Bool)1
 typedef _Bool bool_t;
 
+union iaddr {
+    struct sockaddr sa;
+    struct sockaddr_in sin;
+};
+
 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
                                         __FILE__,__LINE__); } } while(0)
 
 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
                                         __FILE__,__LINE__); } } while(0)
 
@@ -333,7 +338,7 @@ struct comm_addr {
        must start by memsetting it with FILLZERO, or some
        equivalent. */
     struct comm_if *comm;
        must start by memsetting it with FILLZERO, or some
        equivalent. */
     struct comm_if *comm;
-    struct sockaddr_in sin;
+    union iaddr ia;
 };
 
 /* Return True if the packet was processed, and shouldn't be passed to
 };
 
 /* Return True if the packet was processed, and shouldn't be passed to
diff --git a/udp.c b/udp.c
index bb82026c144d0df1f56f9f52e9401dd2fae91691..33971a8129da6f4a454213e4b49d4a6903cb1f7e 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -40,37 +40,21 @@ struct udp {
     closure_t cl;
     struct comm_if ops;
     struct cloc loc;
     closure_t cl;
     struct comm_if ops;
     struct cloc loc;
-    uint32_t addr;
-    uint16_t port;
+    union iaddr addr;
     int fd;
     string_t authbind;
     struct buffer_if *rbuf;
     struct notify_list *notify;
     bool_t use_proxy;
     int fd;
     string_t authbind;
     struct buffer_if *rbuf;
     struct notify_list *notify;
     bool_t use_proxy;
-    struct sockaddr_in proxy;
+    union iaddr proxy;
 };
 
 };
 
-static const char *saddr_to_string(const struct sockaddr_in *sin) {
-    static char bufs[2][100];
-    static int b;
-
-    b ^= 1;
-    snprintf(bufs[b], sizeof(bufs[b]), "[%s]:%d",
-            inet_ntoa(sin->sin_addr),
-            ntohs(sin->sin_port));
-    return bufs[b];
-}
-
 static const char *addr_to_string(void *commst, const struct comm_addr *ca) {
     struct udp *st=commst;
     static char sbuf[100];
 
 static const char *addr_to_string(void *commst, const struct comm_addr *ca) {
     struct udp *st=commst;
     static char sbuf[100];
 
-    struct sockaddr_in la;
-    la.sin_addr.s_addr=htonl(st->addr);
-    la.sin_port=htons(st->port);
-
     snprintf(sbuf, sizeof(sbuf), "udp:%s-%s",
     snprintf(sbuf, sizeof(sbuf), "udp:%s-%s",
-            saddr_to_string(&la), saddr_to_string(&ca->sin));
+            iaddr_to_string(&st->addr), iaddr_to_string(&ca->ia));
     return sbuf;
 }
 
     return sbuf;
 }
 
@@ -91,7 +75,7 @@ static int udp_beforepoll(void *state, struct pollfd *fds, int *nfds_io,
 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
 {
     struct udp *st=state;
 static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
 {
     struct udp *st=state;
-    struct sockaddr_in from;
+    union iaddr from;
     socklen_t fromlen;
     struct notify_list *n;
     bool_t done;
     socklen_t fromlen;
     struct notify_list *n;
     bool_t done;
@@ -113,21 +97,22 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
                    /* Check that the packet came from our poxy server;
                       we shouldn't be contacted directly by anybody else
                       (since they can trivially forge source addresses) */
                    /* Check that the packet came from our poxy server;
                       we shouldn't be contacted directly by anybody else
                       (since they can trivially forge source addresses) */
-                   if (memcmp(&from.sin_addr,&st->proxy.sin_addr,4)!=0 ||
-                       memcmp(&from.sin_port,&st->proxy.sin_port,2)!=0) {
+                   if (!iaddr_equal(&from,&st->proxy)) {
                        Message(M_INFO,"udp: received packet that's not "
                                "from the proxy\n");
                        BUF_FREE(st->rbuf);
                        continue;
                    }
                        Message(M_INFO,"udp: received packet that's not "
                                "from the proxy\n");
                        BUF_FREE(st->rbuf);
                        continue;
                    }
-                   memcpy(&from.sin_addr,buf_unprepend(st->rbuf,4),4);
+                   /* proxy protocol supports ipv4 transport only */
+                   from.sa.sa_family=AF_INET;
+                   memcpy(&from.sin.sin_addr,buf_unprepend(st->rbuf,4),4);
                    buf_unprepend(st->rbuf,2);
                    buf_unprepend(st->rbuf,2);
-                   memcpy(&from.sin_port,buf_unprepend(st->rbuf,2),2);
+                   memcpy(&from.sin.sin_port,buf_unprepend(st->rbuf,2),2);
                }
                struct comm_addr ca;
                FILLZERO(ca);
                ca.comm=&st->ops;
                }
                struct comm_addr ca;
                FILLZERO(ca);
                ca.comm=&st->ops;
-               ca.sin=from;
+               ca.ia=from;
                done=False;
                for (n=st->notify; n; n=n->next) {
                    if (n->fn(n->state, st->rbuf, &ca)) {
                done=False;
                for (n=st->notify; n; n=n->next) {
                    if (n->fn(n->state, st->rbuf, &ca)) {
@@ -197,15 +182,21 @@ static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
 
     if (st->use_proxy) {
        sa=buf_prepend(buf,8);
 
     if (st->use_proxy) {
        sa=buf_prepend(buf,8);
-       memcpy(sa,&dest->sin.sin_addr,4);
+       if (dest->ia.sa.sa_family != AF_INET) {
+           Message(M_INFO,
+               "udp: proxy means dropping outgoing non-IPv4 packet to %s\n",
+                   iaddr_to_string(&dest->ia));
+           return False;
+       }
+       memcpy(sa,&dest->ia.sin.sin_addr,4);
        memset(sa+4,0,4);
        memset(sa+4,0,4);
-       memcpy(sa+6,&dest->sin.sin_port,2);
+       memcpy(sa+6,&dest->ia.sin.sin_port,2);
        sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
               sizeof(st->proxy));
        buf_unprepend(buf,8);
     } else {
        sendto(st->fd, buf->start, buf->size, 0,
        sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
               sizeof(st->proxy));
        buf_unprepend(buf,8);
     } else {
        sendto(st->fd, buf->start, buf->size, 0,
-              (struct sockaddr *)&dest->sin, sizeof(dest->sin));
+              &dest->ia.sa, iaddr_socklen(&dest->ia));
     }
 
     return True;
     }
 
     return True;
@@ -214,7 +205,7 @@ static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
 static void udp_phase_hook(void *sst, uint32_t new_phase)
 {
     struct udp *st=sst;
 static void udp_phase_hook(void *sst, uint32_t new_phase)
 {
     struct udp *st=sst;
-    struct sockaddr_in addr;
+    union iaddr addr;
 
     st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
     if (st->fd<0) {
 
     st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
     if (st->fd<0) {
@@ -226,10 +217,7 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
     }
     setcloexec(st->fd);
 
     }
     setcloexec(st->fd);
 
-    FILLZERO(addr);
-    addr.sin_family=AF_INET;
-    addr.sin_addr.s_addr=htonl(st->addr);
-    addr.sin_port=htons(st->port);
+    addr=st->addr;
     if (st->authbind) {
        pid_t c;
        int status;
     if (st->authbind) {
        pid_t c;
        int status;
@@ -242,8 +230,15 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
        }
        if (c==0) {
            char *argv[4], addrstr[9], portstr[5];
        }
        if (c==0) {
            char *argv[4], addrstr[9], portstr[5];
-           sprintf(addrstr,"%08lX",(long)addr.sin_addr.s_addr);
-           sprintf(portstr,"%04X",addr.sin_port);
+           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);
+               break;
+           default:
+               fatal("udp (%s:%d): unsupported address family for authbind",
+                     st->loc.file,st->loc.line);
+           }
            argv[0]=st->authbind;
            argv[1]=addrstr;
            argv[2]=portstr;
            argv[0]=st->authbind;
            argv[1]=addrstr;
            argv[2]=portstr;
@@ -293,7 +288,7 @@ 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;
     st->ops.release_notify=release_notify;
     st->ops.sendmsg=udp_sendmsg;
     st->ops.addr_to_string=addr_to_string;
-    st->port=0;
+    FILLZERO(st->addr);
     st->use_proxy=False;
 
     i=list_elem(args,0);
     st->use_proxy=False;
 
     i=list_elem(args,0);
@@ -302,27 +297,28 @@ static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context,
     }
     d=i->data.dict;
 
     }
     d=i->data.dict;
 
+    st->addr.sa.sa_family=AF_INET;
     j=dict_find_item(d,"address",False,"udp",st->loc);
     j=dict_find_item(d,"address",False,"udp",st->loc);
-    st->addr=j?string_item_to_ipaddr(j, "udp"):INADDR_ANY;
-    st->port=dict_read_number(d,"port",True,"udp",st->loc,0);
+    st->addr.sin.sin_addr.s_addr=j?string_item_to_ipaddr(j, "udp"):INADDR_ANY;
+    st->addr.sin.sin_port=dict_read_number(d,"port",True,"udp",st->loc,0);
     st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
     st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
     l=dict_lookup(d,"proxy");
     if (l) {
        st->use_proxy=True;
        FILLZERO(st->proxy);
     st->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,"udp",st->loc);
     st->authbind=dict_read_string(d,"authbind",False,"udp",st->loc);
     l=dict_lookup(d,"proxy");
     if (l) {
        st->use_proxy=True;
        FILLZERO(st->proxy);
-       st->proxy.sin_family=AF_INET;
+       st->proxy.sa.sa_family=AF_INET;
        i=list_elem(l,0);
        if (!i || i->type!=t_string) {
            cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
        }
        a=string_item_to_ipaddr(i,"proxy");
        i=list_elem(l,0);
        if (!i || i->type!=t_string) {
            cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
        }
        a=string_item_to_ipaddr(i,"proxy");
-       st->proxy.sin_addr.s_addr=htonl(a);
+       st->proxy.sin.sin_addr.s_addr=htonl(a);
        i=list_elem(l,1);
        if (!i || i->type!=t_number) {
            cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
        }
        i=list_elem(l,1);
        if (!i || i->type!=t_number) {
            cfgfatal(st->loc,"udp","proxy must supply ""addr"",port\n");
        }
-       st->proxy.sin_port=htons(i->data.number);
+       st->proxy.sin.sin_port=htons(i->data.number);
     }
 
     update_max_start_pad(&comm_max_start_pad, st->use_proxy ? 8 : 0);
     }
 
     update_max_start_pad(&comm_max_start_pad, st->use_proxy ? 8 : 0);
diff --git a/util.c b/util.c
index 53f5bdffffd7c0aa1835037259c007cad77613b3..f8ebf25a3e500b35a55f8d5f9ffd0a06efaddcd7 100644 (file)
--- a/util.c
+++ b/util.c
@@ -477,3 +477,39 @@ extern void slilog_part(struct log_if *lf, int priority, const char *message, ..
     vslilog_part(lf,priority,message,ap);
     va_end(ap);
 }
     vslilog_part(lf,priority,message,ap);
     va_end(ap);
 }
+
+const char *iaddr_to_string(const union iaddr *ia)
+{
+    static char bufs[2][100];
+    static int b;
+
+    b ^= 1;
+
+    assert(ia->sa.sa_family == AF_INET);
+
+    snprintf(bufs[b], sizeof(bufs[b]), "[%s]:%d",
+            inet_ntoa(ia->sin.sin_addr),
+            ntohs(ia->sin.sin_port));
+    return bufs[b];
+}
+
+bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib)
+{
+    if (ia->sa.sa_family != ib->sa.sa_family)
+       return 0;
+    switch (ia->sa.sa_family) {
+    case AF_INET:
+       return ia->sin.sin_addr.s_addr == ib->sin.sin_addr.s_addr
+           && ia->sin.sin_port        == ib->sin.sin_port;
+    default:
+       abort();
+    }
+}
+
+int iaddr_socklen(const union iaddr *ia)
+{
+    switch (ia->sa.sa_family) {
+    case AF_INET:  return sizeof(ia->sin);
+    default:       abort();
+    }
+}
diff --git a/util.h b/util.h
index 5866e5784481376e9c2897ecabdb16b5c1f9c818..836b53efe55c77fea0cfd5ac7f0ab5331c7690d3 100644 (file)
--- a/util.h
+++ b/util.h
@@ -56,6 +56,10 @@ extern void send_nak(const struct comm_addr *dest, uint32_t our_index,
 
 extern int consttime_memeq(const void *s1, const void *s2, size_t n);
 
 
 extern int consttime_memeq(const void *s1, const void *s2, size_t n);
 
+const char *iaddr_to_string(const union iaddr *ia);
+bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib);
+int iaddr_socklen(const union iaddr *ia);
+
 #define MINMAX(ae,be,op) ({                    \
        typeof((ae)) a=(ae);                    \
        typeof((be)) b=(be);                    \
 #define MINMAX(ae,be,op) ({                    \
        typeof((ae)) a=(ae);                    \
        typeof((be)) b=(be);                    \