[PATCH 3/7] comm, site: pass a new "struct comm_addr" rather than sockaddr_in

Ian Jackson ijackson at chiark.greenend.org.uk
Thu Jul 14 01:22:38 BST 2011


This abstracts away the fact that the peer address is a sockaddr_in;
instead, most of the code in site.c now only handles the peer address
as an opaque structure.

We put the  struct comm_if*  in the address structure too, as this will
be useful later.  Amongst other things, doing this arranges that the
comm client knows which comm is notifying about an incoming packet.
Previously the client was expected to "just know" because the only
actual client in secnet is site which currently only deals with one
comm.

Also make the relevant arguments const-correct.

Contains consequential changes the signatures of many functions but no
intentional functional change.

Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
---
 secnet.h |   16 ++++++++++++++--
 site.c   |   29 +++++++++++++++--------------
 udp.c    |   15 ++++++++++-----
 3 files changed, 39 insertions(+), 21 deletions(-)

diff --git a/secnet.h b/secnet.h
index 49afd32..809b3a7 100644
--- a/secnet.h
+++ b/secnet.h
@@ -303,16 +303,28 @@ struct rsaprivkey_if {
 
 /* COMM interface */
 
+struct comm_addr {
+    /* This struct is pure data; in particular comm's clients may
+       freely copy it. */
+    /* Everyone is also guaranteed that all padding is set to zero, ie
+       that comm_addrs referring to semantically identical peers will
+       compare equal with memcmp.  Anyone who constructs a comm_addr
+       must start by memsetting it with FILLZERO, or some
+       equivalent. */
+    struct comm_if *comm;
+    struct sockaddr_in sin;
+};
+
 /* Return True if the packet was processed, and shouldn't be passed to
    any other potential receivers. */
 typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
-			    struct sockaddr_in *source);
+			      const struct comm_addr *source);
 typedef void comm_request_notify_fn(void *commst, void *nst,
 				    comm_notify_fn *fn);
 typedef void comm_release_notify_fn(void *commst, void *nst,
 				    comm_notify_fn *fn);
 typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
-			       struct sockaddr_in *dest);
+			       const struct comm_addr *dest);
 struct comm_if {
     void *st;
     int32_t min_start_pad;
diff --git a/site.c b/site.c
index 006d9b4..18ae85d 100644
--- a/site.c
+++ b/site.c
@@ -174,7 +174,7 @@ struct site {
     bool_t current_valid;
     uint64_t current_key_timeout; /* End of life of current key */
     uint64_t renegotiate_key_time; /* When we can negotiate a new key */
-    struct sockaddr_in peer; /* Current address of peer */
+    struct comm_addr peer; /* Current address of peer */
     bool_t peer_valid; /* Peer address becomes invalid when key times out,
 			  but only if we have a DNS name for our peer */
 
@@ -185,7 +185,7 @@ struct site {
        timeout before we can listen for another setup packet); perhaps
        we should keep a list of 'bad' sources for setup packets. */
     uint32_t setup_session_id;
-    struct sockaddr_in setup_peer;
+    struct comm_addr setup_peer;
     uint8_t localN[NONCELEN]; /* Nonces for key exchange */
     uint8_t remoteN[NONCELEN];
     struct buffer_if buffer; /* Current outgoing key exchange packet */
@@ -374,7 +374,7 @@ static bool_t generate_msg1(struct site *st)
 }
 
 static bool_t process_msg1(struct site *st, struct buffer_if *msg1,
-			   struct sockaddr_in *src)
+			   const struct comm_addr *src)
 {
     struct msg m;
 
@@ -397,7 +397,7 @@ static bool_t generate_msg2(struct site *st)
 }
 
 static bool_t process_msg2(struct site *st, struct buffer_if *msg2,
-			   struct sockaddr_in *src)
+			   const struct comm_addr *src)
 {
     struct msg m;
     cstring_t err;
@@ -421,7 +421,7 @@ static bool_t generate_msg3(struct site *st)
 }
 
 static bool_t process_msg3(struct site *st, struct buffer_if *msg3,
-			   struct sockaddr_in *src)
+			   const struct comm_addr *src)
 {
     struct msg m;
     uint8_t *hash;
@@ -472,7 +472,7 @@ static bool_t generate_msg4(struct site *st)
 }
 
 static bool_t process_msg4(struct site *st, struct buffer_if *msg4,
-			   struct sockaddr_in *src)
+			   const struct comm_addr *src)
 {
     struct msg m;
     uint8_t *hash;
@@ -552,7 +552,7 @@ static bool_t generate_msg5(struct site *st)
 }
 
 static bool_t process_msg5(struct site *st, struct buffer_if *msg5,
-			   struct sockaddr_in *src)
+			   const struct comm_addr *src)
 {
     struct msg0 m;
     cstring_t transform_err;
@@ -601,7 +601,7 @@ static bool_t generate_msg6(struct site *st)
 }
 
 static bool_t process_msg6(struct site *st, struct buffer_if *msg6,
-			   struct sockaddr_in *src)
+			   const struct comm_addr *src)
 {
     struct msg0 m;
     cstring_t transform_err;
@@ -629,7 +629,7 @@ static bool_t process_msg6(struct site *st, struct buffer_if *msg6,
 }
 
 static bool_t process_msg0(struct site *st, struct buffer_if *msg0,
-			   struct sockaddr_in *src)
+			   const struct comm_addr *src)
 {
     struct msg0 m;
     cstring_t transform_err;
@@ -670,7 +670,7 @@ static bool_t process_msg0(struct site *st, struct buffer_if *msg0,
 }
 
 static void dump_packet(struct site *st, struct buffer_if *buf,
-			struct sockaddr_in *addr, bool_t incoming)
+			const struct comm_addr *addr, bool_t incoming)
 {
     uint32_t dest=ntohl(*(uint32_t *)buf->start);
     uint32_t source=ntohl(*(uint32_t *)(buf->start+4));
@@ -713,9 +713,10 @@ static void site_resolve_callback(void *sst, struct in_addr *address)
     }
     if (address) {
 	memset(&st->setup_peer,0,sizeof(st->setup_peer));
-	st->setup_peer.sin_family=AF_INET;
-	st->setup_peer.sin_port=htons(st->remoteport);
-	st->setup_peer.sin_addr=*address;
+	st->setup_peer.comm=st->comm;
+	st->setup_peer.sin.sin_family=AF_INET;
+	st->setup_peer.sin.sin_port=htons(st->remoteport);
+	st->setup_peer.sin.sin_addr=*address;
 	enter_new_state(st,SITE_SENTMSG1);
     } else {
 	/* Resolution failed */
@@ -1025,7 +1026,7 @@ static void site_outgoing(void *sst, struct buffer_if *buf)
 /* This function is called by the communication device to deliver
    packets from our peers. */
 static bool_t site_incoming(void *sst, struct buffer_if *buf,
-			    struct sockaddr_in *source)
+			    const struct comm_addr *source)
 {
     struct site *st=sst;
     uint32_t dest=ntohl(*(uint32_t *)buf->start);
diff --git a/udp.c b/udp.c
index 945d1d0..8ec8581 100644
--- a/udp.c
+++ b/udp.c
@@ -71,6 +71,7 @@ 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");
@@ -95,7 +96,11 @@ static void udp_afterpoll(void *state, struct pollfd *fds, int nfds)
 		}
 		done=False;
 		for (n=st->notify; n; n=n->next) {
-		    if (n->fn(n->state, st->rbuf, &from)) {
+		    struct comm_addr ca;
+		    FILLZERO(ca);
+		    ca.comm=&st->ops;
+		    ca.sin=from;
+		    if (n->fn(n->state, st->rbuf, &ca)) {
 			done=True;
 			break;
 		    }
@@ -156,21 +161,21 @@ static void release_notify(void *commst, void *nst, comm_notify_fn *fn)
 }
 
 static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
-			  struct sockaddr_in *dest)
+			  const struct comm_addr *dest)
 {
     struct udp *st=commst;
     uint8_t *sa;
 
     if (st->use_proxy) {
 	sa=buf->start-8;
-	memcpy(sa,&dest->sin_addr,4);
+	memcpy(sa,&dest->sin.sin_addr,4);
 	memset(sa+4,0,4);
-	memcpy(sa+6,&dest->sin_port,2);
+	memcpy(sa+6,&dest->sin.sin_port,2);
 	sendto(st->fd,sa,buf->size+8,0,(struct sockaddr *)&st->proxy,
 	       sizeof(st->proxy));
     } else {
 	sendto(st->fd, buf->start, buf->size, 0,
-	       (struct sockaddr *)dest, sizeof(*dest));
+	       (struct sockaddr *)&dest->sin, sizeof(dest->sin));
     }
 
     return True;
-- 
1.5.6.5




More information about the sgo-software-discuss mailing list