Various minor tweaks to debugging and related things.

Fixed major memory handling error in neighbour structure allocation.
This commit is contained in:
gardners 2012-01-09 16:28:44 +10:30
parent 52b80069da
commit 23ee957169
5 changed files with 58 additions and 24 deletions

View File

@ -872,3 +872,9 @@ int rhizome_server_get_fds(struct pollfd *fds,int *fdcount,int fdmax);
int rhizome_server_poll(); int rhizome_server_poll();
#include "nacl.h" #include "nacl.h"
#define DEBUG_OVERLAYINTERFACES 4
#define DEBUG_PACKETXFER 1
#define DEBUG_VERBOSE 2
#define DEBUG_VERBOSE_IO 8

View File

@ -137,7 +137,7 @@ int overlayServerMode()
waittime.tv_usec=(ms%1000)*1000; waittime.tv_usec=(ms%1000)*1000;
waittime.tv_sec=ms/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); int r=select(maxfd+1,&read_fds,NULL,NULL,&waittime);
if (r<0) { if (r<0) {
/* select had a problem */ /* select had a problem */
@ -150,11 +150,12 @@ int overlayServerMode()
} else { } else {
/* No data before tick occurred, so do nothing. /* No data before tick occurred, so do nothing.
Well, for now let's just check anyway. */ 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(); overlay_rx_messages();
} }
/* Check if we need to trigger any ticks on any interfaces */ /* Check if we need to trigger any ticks on any interfaces */
overlay_check_ticks(); overlay_check_ticks();
sleep(1);
} }
return 0; 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 */ /* Great, we have the address, so we can get on with things */
break; break;
case OA_PLEASEEXPLAIN: 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; break;
case OA_UNSUPPORTED: case OA_UNSUPPORTED:
default: default:
@ -228,10 +231,18 @@ int overlay_frame_process(int interface,overlay_frame *f)
fprintf(stderr,"\n"); 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) { if (f->destination_address_status==OA_RESOLVED) {
for(i=0;i<SID_SIZE;i++) if (f->destination[i]!=0xff) break; for(i=0;i<SID_SIZE;i++) if (f->destination[i]!=0xff) break;
if (i==SID_SIZE) { ultimatelyForMe=1; broadcast=1; } if (i==SID_SIZE) { ultimatelyForMe=1; broadcast=1; }
if (overlay_address_is_local(f->destination)) ultimatelyForMe=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;
} }
} }

View File

@ -184,8 +184,6 @@ int overlay_interface_init_socket(int interface,struct sockaddr_in src_addr,stru
I(netmask)=netmask; I(netmask)=netmask;
I(fileP)=0; I(fileP)=0;
overlay_sequence_start_time=time(0);
I(fd)=socket(PF_INET,SOCK_DGRAM,0); I(fd)=socket(PF_INET,SOCK_DGRAM,0);
if (I(fd)<0) { if (I(fd)<0) {
return WHY("Could not create UDP socket for interface"); 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; case OVERLAY_INTERFACE_WIFI: I(tick_ms)=500; break;
} }
if (!overlay_sequence_start_time) overlay_sequence_start_time=time(0);
if (name[0]=='>') { if (name[0]=='>') {
I(fileP)=1; I(fileP)=1;
I(fd) = open(&name[1],O_APPEND|O_NONBLOCK|O_RDWR); I(fd) = open(&name[1],O_APPEND|O_NONBLOCK|O_RDWR);
@ -290,16 +290,16 @@ int overlay_rx_messages()
/* Read from dummy interface file */ /* Read from dummy interface file */
long long length=lseek(overlay_interfaces[i].fd,0,SEEK_END); long long length=lseek(overlay_interfaces[i].fd,0,SEEK_END);
lseek(overlay_interfaces[i].fd,overlay_interfaces[i].offset,SEEK_SET); 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); 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; overlay_interfaces[i].offset+=2048;
plen=2048-128; plen=2048-128;
bzero(&transaction_id[0],8); bzero(&transaction_id[0],8);
bzero(&src_addr,sizeof(src_addr)); bzero(&src_addr,sizeof(src_addr));
if ((packet[0]==0x01)&&!(packet[1]|packet[2]|packet[3])) 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 (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 WHY("Invalid packet version in dummy interface");
} }
else { c[i]=0; count--; } else { c[i]=0; count--; }
@ -309,7 +309,7 @@ int overlay_rx_messages()
&src_addr,&addrlen); &src_addr,&addrlen);
if (plen<0) { c[i]=0; count--; } else { if (plen<0) { c[i]=0; count--; } else {
/* We have a frame from this interface */ /* 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"); 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)) if (!overlay_broadcast_ensemble(i,NULL,e->bytes,e->length))
{ {
overlay_update_sequence_number(); overlay_update_sequence_number();
fprintf(stderr,"Successfully transmitted tick frame #%d on interface #%d (%d bytes)\n", fprintf(stderr,"Successfully transmitted tick frame #%lld on interface #%d (%d bytes)\n",
overlay_sequence_number,i,e->length); (long long)overlay_sequence_number,i,e->length);
/* De-queue the passengers who were aboard. */ /* De-queue the passengers who were aboard. */
int j,q; int j,q;
for(q=0;q<OQ_MAX;q++) for(q=0;q<OQ_MAX;q++)
@ -682,7 +682,7 @@ int overlay_check_ticks()
/* Only tick live interfaces */ /* Only tick live interfaces */
if (overlay_interfaces[i].observed>0) if (overlay_interfaces[i].observed>0)
{ {
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); overlay_interfaces[i].tick_ms,overlay_interfaces[i].last_tick_ms);
if (now>=overlay_interfaces[i].last_tick_ms+overlay_interfaces[i].tick_ms) if (now>=overlay_interfaces[i].last_tick_ms+overlay_interfaces[i].tick_ms)
{ {
@ -692,7 +692,7 @@ int overlay_check_ticks()
} }
} }
else 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; return 0;
@ -709,11 +709,11 @@ long long overlay_time_until_next_tick()
now=tv.tv_sec*1000LL+tv.tv_usec/1000; now=tv.tv_sec*1000LL+tv.tv_usec/1000;
int i; 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;i<overlay_interface_count;i++) for(i=0;i<overlay_interface_count;i++)
if (overlay_interfaces[i].observed>0) if (overlay_interfaces[i].observed>0)
{ {
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); 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; long long thistick=(overlay_interfaces[i].last_tick_ms+overlay_interfaces[i].tick_ms)-now;

View File

@ -292,7 +292,7 @@ int overlay_route_init(int mb_ram)
overlay_nodes=calloc(sizeof(overlay_node*),bin_count); overlay_nodes=calloc(sizeof(overlay_node*),bin_count);
if (!overlay_nodes) return WHY("calloc() failed."); 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) { if (!overlay_neighbours) {
free(overlay_nodes); free(overlay_nodes);
return WHY("calloc() failed."); 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 */ /* Mask out extranous bits to return only a valid bin number */
bin&=(overlay_bin_count-1); bin&=(overlay_bin_count-1);
if (debug>3) { if (debug>3) {
int zeroes=0;
fprintf(stderr,"The following address resolves to bin #%d\n",bin); fprintf(stderr,"The following address resolves to bin #%d\n",bin);
for(i=0;i<SID_SIZE;i++) fprintf(stderr,"%02x",sid[i]); for(i=0;i<SID_SIZE;i++) { fprintf(stderr,"%02x",sid[i]); if (!sid[i]) zeroes++; }
fprintf(stderr,"\n"); fprintf(stderr,"\n");
if (zeroes>8) {
fprintf(stderr,"Looks like corrupt memory or packet to me!\n");
}
} }
return bin; return bin;
} }
@ -586,6 +590,7 @@ int overlay_route_make_neighbour(overlay_node *n)
} }
bzero(&overlay_neighbours[n->neighbour_id],sizeof(overlay_neighbour)); bzero(&overlay_neighbours[n->neighbour_id],sizeof(overlay_neighbour));
overlay_neighbours[n->neighbour_id].node=n; overlay_neighbours[n->neighbour_id].node=n;
return 0; 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"); if (!n->neighbour_id) if (overlay_route_make_neighbour(n)) return WHY("overlay_route_make_neighbour() failed");
/* Get neighbour structure */ /* 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]; overlay_neighbour *neh=&overlay_neighbours[n->neighbour_id];
int obs_index=neh->most_recent_observation_id; int obs_index=neh->most_recent_observation_id;
@ -732,14 +741,18 @@ int overlay_route_recalc_node_metrics(overlay_node *n,long long now)
if (n->neighbour_id) if (n->neighbour_id)
{ {
/* Node is also a direct neighbour, so check score that way */ /* 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; int i;
for(i=0;i<overlay_interface_count;i++) for(i=0;i<overlay_interface_count;i++)
{
if (overlay_neighbours[n->neighbour_id].scores[i]>best_score) if (overlay_neighbours[n->neighbour_id].scores[i]>best_score)
{ {
best_score=overlay_neighbours[n->neighbour_id].scores[i]; best_score=overlay_neighbours[n->neighbour_id].scores[i];
best_observation=-1; best_observation=-1;
} }
} }
}
/* Think about scheduling this node's score for readvertising if its score /* Think about scheduling this node's score for readvertising if its score
has changed a lot? has changed a lot?
@ -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 /* 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) */ 50KB/12 hours which is the minimum traffic charge rate on an expensive BGAN satellite link) */
if (interval<3600000) { 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); overlay_interfaces[n->observations[i].sender_interface].name);
/* sender_interface is unsigned, so a single-sided test is sufficient for bounds checking */ /* sender_interface is unsigned, so a single-sided test is sufficient for bounds checking */
if (n->observations[i].sender_interface<OVERLAY_MAX_INTERFACES) if (n->observations[i].sender_interface<OVERLAY_MAX_INTERFACES)
@ -1098,8 +1111,9 @@ int overlay_route_tick()
while(n--) while(n--)
{ {
int slot; int slot;
for(slot=0;slot<overlay_bin_size;slot++) for(slot=0;slot<overlay_bin_size;slot++) {
overlay_route_tick_node(overlay_route_tick_next_node_bin_id,slot,start_time); overlay_route_tick_node(overlay_route_tick_next_node_bin_id,slot,start_time);
}
overlay_route_tick_next_node_bin_id++; overlay_route_tick_next_node_bin_id++;
if (overlay_route_tick_next_node_bin_id>=overlay_bin_count) overlay_route_tick_next_node_bin_id=0; if (overlay_route_tick_next_node_bin_id>=overlay_bin_count) overlay_route_tick_next_node_bin_id=0;
} }

View File

@ -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) 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."); return WHY("Not implemented.");
} }