diff --git a/mphlr.h b/mphlr.h index 04b625bf..f7e5719b 100644 --- a/mphlr.h +++ b/mphlr.h @@ -872,3 +872,9 @@ int rhizome_server_get_fds(struct pollfd *fds,int *fdcount,int fdmax); int rhizome_server_poll(); #include "nacl.h" + + +#define DEBUG_OVERLAYINTERFACES 4 +#define DEBUG_PACKETXFER 1 +#define DEBUG_VERBOSE 2 +#define DEBUG_VERBOSE_IO 8 diff --git a/overlay.c b/overlay.c index 2394bd3c..fff10790 100644 --- a/overlay.c +++ b/overlay.c @@ -137,7 +137,7 @@ int overlayServerMode() waittime.tv_usec=(ms%1000)*1000; waittime.tv_sec=ms/1000; - if (debug&4) fprintf(stderr,"Waiting via select() for up to %lldms\n",ms); + if (debug&DEBUG_VERBOSE_IO) fprintf(stderr,"Waiting via select() for up to %lldms\n",ms); int r=select(maxfd+1,&read_fds,NULL,NULL,&waittime); if (r<0) { /* select had a problem */ @@ -150,11 +150,12 @@ int overlayServerMode() } else { /* No data before tick occurred, so do nothing. Well, for now let's just check anyway. */ - if (debug&4) fprintf(stderr,"select() timeout.\n"); + if (debug&DEBUG_VERBOSE_IO) fprintf(stderr,"select() timeout.\n"); overlay_rx_messages(); } /* Check if we need to trigger any ticks on any interfaces */ overlay_check_ticks(); + sleep(1); } return 0; @@ -190,6 +191,8 @@ int overlay_frame_process(int interface,overlay_frame *f) /* Great, we have the address, so we can get on with things */ break; case OA_PLEASEEXPLAIN: + return WHY("Address cannot be resolved -- aborting packet processing.\n"); + /* XXX Should send a please explain to get this address resolved. */ break; case OA_UNSUPPORTED: default: @@ -228,10 +231,18 @@ int overlay_frame_process(int interface,overlay_frame *f) fprintf(stderr,"\n"); } + if (f->source_address_status!=OA_RESOLVED) { + if (debug>1) WHY("Source address could not be resolved, so dropping frame."); + return -1; + } + if (f->destination_address_status==OA_RESOLVED) { for(i=0;idestination[i]!=0xff) break; if (i==SID_SIZE) { ultimatelyForMe=1; broadcast=1; } if (overlay_address_is_local(f->destination)) ultimatelyForMe=1; + } else { + if (debug>1) WHY("Destination address could not be resolved, so dropping frame."); + return -1; } } diff --git a/overlay_interface.c b/overlay_interface.c index 90338b81..bf0ce99a 100644 --- a/overlay_interface.c +++ b/overlay_interface.c @@ -184,8 +184,6 @@ int overlay_interface_init_socket(int interface,struct sockaddr_in src_addr,stru I(netmask)=netmask; I(fileP)=0; - overlay_sequence_start_time=time(0); - I(fd)=socket(PF_INET,SOCK_DGRAM,0); if (I(fd)<0) { return WHY("Could not create UDP socket for interface"); @@ -240,6 +238,8 @@ int overlay_interface_init(char *name,struct sockaddr_in src_addr,struct sockadd case OVERLAY_INTERFACE_WIFI: I(tick_ms)=500; break; } + if (!overlay_sequence_start_time) overlay_sequence_start_time=time(0); + if (name[0]=='>') { I(fileP)=1; I(fd) = open(&name[1],O_APPEND|O_NONBLOCK|O_RDWR); @@ -290,16 +290,16 @@ int overlay_rx_messages() /* Read from dummy interface file */ long long length=lseek(overlay_interfaces[i].fd,0,SEEK_END); lseek(overlay_interfaces[i].fd,overlay_interfaces[i].offset,SEEK_SET); - if (debug&4) fprintf(stderr,"Reading from interface log at offset %d, end of file at %lld.\n", + if (debug&DEBUG_OVERLAYINTERFACES) fprintf(stderr,"Reading from interface #%d log at offset %d, end of file at %lld.\n",i, overlay_interfaces[i].offset,length); - if (read(overlay_interfaces[i].fd,packet,2048)==2048) + if (read(overlay_interfaces[i].fd,&packet[0],2048)==2048) { overlay_interfaces[i].offset+=2048; plen=2048-128; bzero(&transaction_id[0],8); bzero(&src_addr,sizeof(src_addr)); - if ((packet[0]==0x01)&&!(packet[1]|packet[2]|packet[3])) - { if (packetOk(i,&packet[128],plen,transaction_id,&src_addr,addrlen,1)) WHY("Malformed or unsupported packet from dummy interface (packetOK() failed)"); } + if ((packet[0]==0x01)&&!(packet[1]|packet[2]|packet[3])) { + { if (packetOk(i,&packet[128],plen,transaction_id,&src_addr,addrlen,1)) WHY("Malformed or unsupported packet from dummy interface (packetOK() failed)"); } } else WHY("Invalid packet version in dummy interface"); } else { c[i]=0; count--; } @@ -309,7 +309,7 @@ int overlay_rx_messages() &src_addr,&addrlen); if (plen<0) { c[i]=0; count--; } else { /* We have a frame from this interface */ - if (debug&4)fprintf(stderr,"Received %d bytes on interface #%d\n",plen,i); + if (debug&DEBUG_OVERLAYINTERFACES)fprintf(stderr,"Received %d bytes on interface #%d\n",plen,i); if (packetOk(i,packet,plen,NULL,&src_addr,addrlen,1)) WHY("Malformed packet"); } @@ -622,8 +622,8 @@ int overlay_tick_interface(int i, long long now) if (!overlay_broadcast_ensemble(i,NULL,e->bytes,e->length)) { overlay_update_sequence_number(); - fprintf(stderr,"Successfully transmitted tick frame #%d on interface #%d (%d bytes)\n", - overlay_sequence_number,i,e->length); + fprintf(stderr,"Successfully transmitted tick frame #%lld on interface #%d (%d bytes)\n", + (long long)overlay_sequence_number,i,e->length); /* De-queue the passengers who were aboard. */ int j,q; for(q=0;q0) { - if (debug&4)fprintf(stderr,"Interface %s ticks every %dms, last at %lld.\n",overlay_interfaces[i].name, + if (debug&DEBUG_VERBOSE_IO)fprintf(stderr,"Interface %s ticks every %dms, last at %lld.\n",overlay_interfaces[i].name, overlay_interfaces[i].tick_ms,overlay_interfaces[i].last_tick_ms); if (now>=overlay_interfaces[i].last_tick_ms+overlay_interfaces[i].tick_ms) { @@ -692,7 +692,7 @@ int overlay_check_ticks() } } else - if (debug&4)fprintf(stderr,"Interface %s is awol.\n",overlay_interfaces[i].name); + if (debug&DEBUG_VERBOSE_IO)fprintf(stderr,"Interface %s is awol.\n",overlay_interfaces[i].name); } return 0; @@ -709,11 +709,11 @@ long long overlay_time_until_next_tick() now=tv.tv_sec*1000LL+tv.tv_usec/1000; int i; - if (debug&4)fprintf(stderr,"Tick-check on %d interfaces at %lldms\n",overlay_interface_count,now); + if (debug&DEBUG_VERBOSE_IO)fprintf(stderr,"Tick-check on %d interfaces at %lldms\n",overlay_interface_count,now); for(i=0;i0) { - if (debug&4) fprintf(stderr,"Interface %s ticks every %dms, last at T-%lldms.\n",overlay_interfaces[i].name, + if (debug&DEBUG_VERBOSE_IO) fprintf(stderr,"Interface %s ticks every %dms, last at T-%lldms.\n",overlay_interfaces[i].name, overlay_interfaces[i].tick_ms,now-overlay_interfaces[i].last_tick_ms); long long thistick=(overlay_interfaces[i].last_tick_ms+overlay_interfaces[i].tick_ms)-now; diff --git a/overlay_route.c b/overlay_route.c index 662ebf44..7cc798a1 100644 --- a/overlay_route.c +++ b/overlay_route.c @@ -292,7 +292,7 @@ int overlay_route_init(int mb_ram) overlay_nodes=calloc(sizeof(overlay_node*),bin_count); if (!overlay_nodes) return WHY("calloc() failed."); - overlay_neighbours=calloc(sizeof(overlay_neighbour*),1024*mb_ram); + overlay_neighbours=calloc(sizeof(overlay_neighbour),1024*mb_ram); if (!overlay_neighbours) { free(overlay_nodes); return WHY("calloc() failed."); @@ -406,9 +406,13 @@ unsigned int overlay_route_hash_sid(unsigned char *sid) /* Mask out extranous bits to return only a valid bin number */ bin&=(overlay_bin_count-1); if (debug>3) { + int zeroes=0; fprintf(stderr,"The following address resolves to bin #%d\n",bin); - for(i=0;i8) { + fprintf(stderr,"Looks like corrupt memory or packet to me!\n"); + } } return bin; } @@ -586,6 +590,7 @@ int overlay_route_make_neighbour(overlay_node *n) } bzero(&overlay_neighbours[n->neighbour_id],sizeof(overlay_neighbour)); overlay_neighbours[n->neighbour_id].node=n; + return 0; } @@ -618,6 +623,10 @@ int overlay_route_i_can_hear(unsigned char *who,int sender_interface,unsigned in if (!n->neighbour_id) if (overlay_route_make_neighbour(n)) return WHY("overlay_route_make_neighbour() failed"); /* Get neighbour structure */ + if (n->neighbour_id<0||n->neighbour_id>overlay_max_neighbours) + { WHY("n->neighbour_id set to illegal value"); + return -1; + } overlay_neighbour *neh=&overlay_neighbours[n->neighbour_id]; int obs_index=neh->most_recent_observation_id; @@ -732,13 +741,17 @@ int overlay_route_recalc_node_metrics(overlay_node *n,long long now) if (n->neighbour_id) { /* Node is also a direct neighbour, so check score that way */ + if (n->neighbour_id>overlay_max_neighbours||n->neighbour_id<0) + return WHY("n->neighbour_id is invalid."); int i; for(i=0;ineighbour_id].scores[i]>best_score) - { - best_score=overlay_neighbours[n->neighbour_id].scores[i]; - best_observation=-1; - } + { + if (overlay_neighbours[n->neighbour_id].scores[i]>best_score) + { + best_score=overlay_neighbours[n->neighbour_id].scores[i]; + best_observation=-1; + } + } } /* Think about scheduling this node's score for readvertising if its score @@ -791,7 +804,7 @@ int overlay_route_recalc_neighbour_metrics(overlay_neighbour *n,long long now) /* Support interface tick speeds down to 1 per hour (well and truly slow enough to do 50KB/12 hours which is the minimum traffic charge rate on an expensive BGAN satellite link) */ if (interval<3600000) { - if (debug>3) fprintf(stderr,"adding %dms (interface %d '%s')\n",interval,n->observations[i].sender_interface, + if (debug&DEBUG_VERBOSE_IO) fprintf(stderr,"adding %dms (interface %d '%s')\n",interval,n->observations[i].sender_interface, overlay_interfaces[n->observations[i].sender_interface].name); /* sender_interface is unsigned, so a single-sided test is sufficient for bounds checking */ if (n->observations[i].sender_interface=overlay_bin_count) overlay_route_tick_next_node_bin_id=0; } diff --git a/rhizome.c b/rhizome.c index 416d2b75..1c12faab 100644 --- a/rhizome.c +++ b/rhizome.c @@ -1488,5 +1488,8 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e) int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now) { + if (!f) return -1; + fprintf(stderr,"rhizome f->bytecount=%d\n",f->bytecount); + return WHY("Not implemented."); }