[PATCH 5/6] site: Clearer processing for name-addressed packets
Ian Jackson
ijackson at chiark.greenend.org.uk
Wed Jul 17 14:36:11 BST 2013
Ian Jackson writes ("[PATCH 5/6] site: Clearer processing for name-addressed packets"):
> We are going to introduce a new packet which (like MSG1) is addressed
> by name rather than the site id. To make this easier:
The diff here is inflated by a reindent.
Here is a copy of it prepared with "diff -b":
diff --git a/NOTES b/NOTES
index 09c083e..477104f 100644
--- a/NOTES
+++ b/NOTES
@@ -202,6 +202,9 @@ Messages:
1) A->B: *,iA,msg1,A,B,protorange-A,nA
+i* must be encoded as 0. (However, it is permitted for a site to use
+zero as its "index" for another site.)
+
2) B->A: iA,iB,msg2,B,A,chosen-protocol,nB,nA
(The order of B and A reverses in alternate messages so that the same
diff --git a/site.c b/site.c
index 5c73533..aecd64d 100644
--- a/site.c
+++ b/site.c
@@ -253,9 +253,9 @@ struct site {
after this time, initiate a new
key exchange */
- uint8_t *setupsig; /* Expected signature of incoming MSG1 packets */
- int32_t setupsiglen; /* Allows us to discard packets quickly if
- they are not for us */
+ uint8_t *namedsig; /* Names section of MSG1 packets */
+ int32_t namedsiglen; /* Allows us to discard name-addressed packets
+ quickly if they are not for us */
bool_t setup_priority; /* Do we have precedence if both sites emit
message 1 simultaneously? */
uint32_t log_events;
@@ -1220,6 +1220,18 @@ static void site_outgoing(void *sst, struct buffer_if *buf)
initiate_key_setup(st,"outgoing packet");
}
+static bool_t named_for_us(struct site *st, struct buffer_if *buf,
+ int extralen)
+ /* For packets which are identified by the local and remote names.
+ * If it has our name and our peer's name in it it's for us. */
+{
+ if (buf->size<(st->namedsiglen+12+extralen))
+ return False;
+ if (memcmp(buf->start+12,st->namedsig,st->namedsiglen))
+ return False;
+ return True;
+}
+
/* 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,
@@ -1230,14 +1242,11 @@ static bool_t site_incoming(void *sst, struct buffer_if *buf,
if (buf->size < 12) return False;
+ uint32_t msgtype=ntohl(get_uint32(buf->start+8));
uint32_t dest=ntohl(*(uint32_t *)buf->start);
- if (dest==0) {
- /* It could be for any site - it should have LABEL_MSG1 and
- might have our name and our peer's name in it */
- if (buf->size<(st->setupsiglen+8+NONCELEN)) return False;
- if (memcmp(buf->start+8,st->setupsig,st->setupsiglen)==0) {
- /* It's addressed to us. Decide what to do about it. */
+ if (msgtype==LABEL_MSG1 && named_for_us(st,buf,NONCELEN)) {
+ /* It's a MSG1 addressed to us. Decide what to do about it. */
dump_packet(st,buf,source,True);
if (st->state==SITE_RUN || st->state==SITE_RESOLVE ||
st->state==SITE_WAIT) {
@@ -1279,11 +1288,8 @@ static bool_t site_incoming(void *sst, struct buffer_if *buf,
BUF_FREE(buf);
return True;
}
- return False; /* Not for us. */
- }
if (dest==st->index) {
/* Explicitly addressed to us */
- uint32_t msgtype=ntohl(get_uint32(buf->start+8));
if (msgtype!=LABEL_MSG0) dump_packet(st,buf,source,True);
switch (msgtype) {
case LABEL_NAK:
@@ -1532,13 +1538,12 @@ static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
/* The information we expect to see in incoming messages of type 1 */
/* fixme: lots of unchecked overflows here, but the results are only
corrupted packets rather than undefined behaviour */
- st->setupsiglen=strlen(st->remotename)+strlen(st->localname)+8;
- st->setupsig=safe_malloc(st->setupsiglen,"site_apply");
- put_uint32(st->setupsig+0,LABEL_MSG1);
- put_uint16(st->setupsig+4,strlen(st->remotename));
- memcpy(&st->setupsig[6],st->remotename,strlen(st->remotename));
- put_uint16(st->setupsig+(6+strlen(st->remotename)),strlen(st->localname));
- memcpy(&st->setupsig[8+strlen(st->remotename)],st->localname,
+ st->namedsiglen=strlen(st->remotename)+strlen(st->localname)+4;
+ st->namedsig=safe_malloc(st->namedsiglen,"site_apply");
+ put_uint16(st->namedsig+0,strlen(st->remotename));
+ memcpy(&st->namedsig[2],st->remotename,strlen(st->remotename));
+ put_uint16(st->namedsig+(2+strlen(st->remotename)),strlen(st->localname));
+ memcpy(&st->namedsig[4+strlen(st->remotename)],st->localname,
strlen(st->localname));
st->setup_priority=(strcmp(st->localname,st->remotename)>0);
More information about the sgo-software-discuss
mailing list