Code improvements while debugging 'dnaprotocol' tests

Use ssize_t and size_t where required by system calls
More stringent checking of errors from system calls
Log the offset when writing to dummy interface file
Clean up many WHY/INFO/DEBUG statements
Convert many fprintf(stderr,...) to DEBUGF()
This commit is contained in:
Andrew Bettison 2012-07-03 15:36:51 +09:30
parent 8020ea3b74
commit 78a8aaca16
12 changed files with 173 additions and 179 deletions

View File

@ -278,7 +278,6 @@ int getReplyPackets(int method,int peer,int batchP,struct response_set *response
int timeout_secs;
int timeout_usecs;
int to=timeout;
int len;
if (debug&DEBUG_DNARESPONSES) DEBUGF("getReplyPackets(policy=%d)",method);
@ -291,7 +290,7 @@ int getReplyPackets(int method,int peer,int batchP,struct response_set *response
while(1) {
unsigned char buffer[16384];
struct sockaddr sender;
socklen_t recvaddrlen=sizeof(struct sockaddr);
socklen_t recvaddrlen = sizeof(struct sockaddr);
struct pollfd fds;
if (recvaddr) bzero((void *)recvaddr,sizeof(struct sockaddr));
@ -309,16 +308,16 @@ int getReplyPackets(int method,int peer,int batchP,struct response_set *response
int ttl=-1;
if (!recvaddr) recvaddr=&reply_recvaddr;
len=recvwithttl(sock,buffer,sizeof(buffer),&ttl,recvaddr,&recvaddrlen);
if (len<=0) return WHY("Unable to receive packet.");
ssize_t len = recvwithttl(sock,buffer,sizeof(buffer),&ttl,recvaddr,&recvaddrlen);
if (len == -1)
return WHY("Unable to receive packet.");
if (recvaddr) {
client_port=((struct sockaddr_in *)recvaddr)->sin_port;
client_addr=((struct sockaddr_in *)recvaddr)->sin_addr;
if (debug&DEBUG_DNARESPONSES) DEBUGF("Received reply from %s (len=%d)",inet_ntoa(client_addr),len);
if (debug&DEBUG_DNARESPONSES) dump("recvaddr",(unsigned char *)&sender,recvaddrlen);
if (debug&DEBUG_DNARESPONSES) dump("packet",(unsigned char *)buffer,len);
if (debug&DEBUG_DNARESPONSES) DEBUGF("Received reply from %s (len=%lld)", inet_ntoa(client_addr), (long long) len);
if (debug&DEBUG_DNARESPONSES) dump("recvaddr", (unsigned char *)&sender, recvaddrlen);
if (debug&DEBUG_DNARESPONSES) dump("packet", (unsigned char *)buffer, len);
}
if (dropPacketP(len)) {

5
log.c
View File

@ -109,14 +109,15 @@ const char *trimbuildpath(const char *path)
return &path[lastsep];
}
int dump(char *name, unsigned char *addr, int len)
int dump(char *name, unsigned char *addr, size_t len)
{
char buf[100];
int i,j;
size_t i;
DEBUGF("Dump of %s", name);
for(i = 0; i < len; i += 16) {
strbuf b = strbuf_local(buf, sizeof buf);
strbuf_sprintf(b, " %04x :", i);
int j;
for (j = 0; j < 16 && i + j < len; j++)
strbuf_sprintf(b, " %02x", addr[i + j]);
for (; j < 16; j++)

View File

@ -182,8 +182,7 @@ int overlay_route_saw_advertisements(int i,overlay_frame *f, long long now)
overlay_node *sender=overlay_route_find_node(f->source,SID_SIZE,0);
int sender_score=sender->best_link_score;
if (debug&DEBUG_OVERLAYROUTEMONITOR)
fprintf(stderr,"score to reach %s is %d\n",
overlay_render_sid(f->source),sender_score);
DEBUGF("score to reach %s is %d", overlay_render_sid(f->source),sender_score);
while(ofs<f->payload->length)
{
@ -215,7 +214,7 @@ int overlay_route_saw_advertisements(int i,overlay_frame *f, long long now)
score,gateways_en_route);
} else if (r==OA_PLEASEEXPLAIN) {
/* Unresolved address -- ask someone to resolve it for us. */
#warning WHY("Dispatch PLEASEEXPLAIN not implemented");
WARN("Dispatch PLEASEEXPLAIN not implemented");
}
}

View File

@ -331,29 +331,22 @@ void overlay_interface_poll(struct sched_ent *alarm)
unsigned char packet[16384];
struct sockaddr src_addr;
unsigned int addrlen=sizeof(src_addr);
socklen_t addrlen = sizeof(src_addr);
/* Read only one UDP packet per call to share resources more fairly, and also
enable stats to accurately count packets received */
int recvttl=1;
plen=recvwithttl(alarm->poll.fd,packet,sizeof(packet),
&recvttl,&src_addr,&addrlen);
if (plen<1) {
return;
} else {
plen = recvwithttl(alarm->poll.fd,packet, sizeof(packet), &recvttl, &src_addr, &addrlen);
if (plen != -1) {
/* We have a frame from this interface */
if (debug&DEBUG_PACKETRX) {
serval_packetvisualise(open_logging(),"Read from real interface",
packet,plen);
}
if (debug&DEBUG_PACKETRX)
serval_packetvisualise(open_logging(),"Read from real interface", packet,plen);
if (debug&DEBUG_OVERLAYINTERFACES) DEBUGF("Received %d bytes on interface %s",plen,interface->name);
if (packetOk(interface,packet,plen,NULL,recvttl,&src_addr,addrlen,1)) {
WHY("Malformed packet");
serval_packetvisualise(open_logging(), "Malformed packet", packet,plen);
}
}
return;
}
void overlay_dummy_poll(struct sched_ent *alarm)
@ -366,7 +359,7 @@ void overlay_dummy_poll(struct sched_ent *alarm)
unsigned char packet[16384];
int plen=0;
struct sockaddr src_addr;
unsigned int addrlen=sizeof(src_addr);
size_t addrlen = sizeof(src_addr);
unsigned char transaction_id[8];
/* Read from dummy interface file */
@ -380,30 +373,33 @@ void overlay_dummy_poll(struct sched_ent *alarm)
{
lseek(alarm->poll.fd,interface->offset,SEEK_SET);
if (debug&DEBUG_OVERLAYINTERFACES)
DEBUGF("Reading from interface %s log at offset %d, end of file at %lld",interface->name,
interface->offset,length);
if (read(alarm->poll.fd,&packet[0],2048)==2048)
{
interface->offset+=2048;
plen=2048-128;
plen=packet[110]+(packet[111]<<8);
if (plen>(2048-128)) plen=-1;
if (debug&DEBUG_PACKETRX) {
serval_packetvisualise(open_logging(),
"Read from dummy interface",
&packet[128],plen);
}
DEBUGF("Read interface %s (size=%lld) at offset=%d",interface->name, length, interface->offset);
ssize_t nread = read(alarm->poll.fd,&packet[0],2048);
if (nread == -1)
WHY_perror("read");
else {
interface->offset += nread;
if (nread == 2048) {
plen = packet[110]+(packet[111]<<8);
if (plen > nread - 128)
plen = -1;
if (debug&DEBUG_PACKETRX)
serval_packetvisualise(open_logging(), "Read from dummy interface", &packet[128], plen);
bzero(&transaction_id[0],8);
bzero(&src_addr,sizeof(src_addr));
if ((plen>=0)&&(packet[0]==0x01)&&!(packet[1]|packet[2]|packet[3])) {
{ if (packetOk(interface,&packet[128],plen,transaction_id,
-1 /* fake TTL */,
&src_addr,addrlen,1))
WHY("Malformed or unsupported packet from dummy interface (packetOK() failed)"); } }
else WHY("Invalid packet version in dummy interface");
if (plen >= 4) {
if (packet[0] == 0x01 && packet[1] == 0 && packet[2] == 0 && packet[3] == 0) {
if (packetOk(interface,&packet[128],plen,transaction_id, -1 /* fake TTL */, &src_addr,addrlen,1) == -1)
WARN("Unsupported packet from dummy interface");
} else {
WARNF("Unsupported packet version from dummy interface: %02x %02x %02x %02x", packet[0], packet[1], packet[2], packet[3]);
}
} else {
WARNF("Invalid packet from dummy interface: plen=%lld", (long long) plen);
}
}
else {
if (debug&DEBUG_IO) DEBUG("Read NOTHING from dummy interface");
else
WARNF("Read %lld bytes from dummy interface", nread);
}
}
@ -443,18 +439,20 @@ int overlay_broadcast_ensemble(int interface_number,
serval_packetvisualise(open_logging(),NULL,bytes,len);
}
overlay_interface *interface = &overlay_interfaces[interface_number];
memset(&s, '\0', sizeof(struct sockaddr_in));
if (recipientaddr) {
bcopy(recipientaddr,&s,sizeof(struct sockaddr_in));
}
else {
s = overlay_interfaces[interface_number].broadcast_address;
s = interface->broadcast_address;
s.sin_family = AF_INET;
if (debug&DEBUG_PACKETTX) DEBUGF("Port=%d",overlay_interfaces[interface_number].port);
s.sin_port = htons( overlay_interfaces[interface_number].port );
if (debug&DEBUG_PACKETTX) DEBUGF("Port=%d",interface->port);
s.sin_port = htons(interface->port);
}
if (overlay_interfaces[interface_number].fileP)
if (interface->fileP)
{
char buf[2048];
bzero(&buf[0],128);
@ -492,26 +490,29 @@ int overlay_broadcast_ensemble(int interface_number,
bzero(&buf[128+len],2048-(128+len));
bcopy(bytes,&buf[128],len);
if (write(overlay_interfaces[interface_number].alarm.poll.fd,buf,2048)!=2048)
{
WHY_perror("write");
return WHY("write() failed");
}
else
return 0;
/* This lseek() is unneccessary because the dummy file is opened in O_APPEND mode. It's
only purpose is to find out the offset to print in the DEBUG statement. It is vulnerable
to a race condition with other processes appending to the same file. */
off_t fsize = lseek(interface->alarm.poll.fd, (off_t) 0, SEEK_END);
if (fsize == -1)
return WHY_perror("lseek");
interface->offset = fsize;
if (debug&DEBUG_OVERLAYINTERFACES)
DEBUGF("Write to interface %s at offset=%d", interface->name, interface->offset);
ssize_t nwrite = write(interface->alarm.poll.fd, buf, 2048);
if (nwrite == -1)
return WHY_perror("write");
interface->offset += nwrite;
if (nwrite != 2048)
return WHYF("only wrote %lld of %lld bytes", nwrite, 2048);
return 0;
}
else
{
if(sendto(overlay_interfaces[interface_number].alarm.poll.fd,
if(sendto(interface->alarm.poll.fd,
bytes, len, 0, (struct sockaddr *)&s, sizeof(struct sockaddr_in)) != len)
{
/* Failed to send */
WHY_perror("sendto(c)");
return WHY("sendto() failed");
}
else
/* Sent okay */
return 0;
return WHY_perror("sendto(c)");
return 0;
}
}
@ -520,10 +521,9 @@ int overlay_broadcast_ensemble(int interface_number,
int overlay_sendto(struct sockaddr_in *recipientaddr,unsigned char *bytes,int len)
{
if (debug&DEBUG_PACKETTX) DEBUGF("Sending %d bytes",len);
if(overlay_broadcast_ensemble(overlay_last_interface_number,recipientaddr,bytes,len))
if (overlay_broadcast_ensemble(overlay_last_interface_number,recipientaddr,bytes,len) == -1)
return -1;
else
return len;
return len;
}
/* Register the interface, or update the existing interface registration */
@ -871,7 +871,7 @@ int overlay_tick_interface(int i, long long now)
service code, which we setup earlier. */
if (debug&DEBUG_OVERLAYINTERFACES)
DEBUGF("Sending %d byte tick packet",e->length);
if (!overlay_broadcast_ensemble(i,NULL,e->bytes,e->length))
if (overlay_broadcast_ensemble(i,NULL,e->bytes,e->length) != -1)
{
overlay_update_sequence_number();
if (debug&DEBUG_OVERLAYINTERFACES)

View File

@ -972,8 +972,7 @@ void overlay_mdp_poll(struct sched_ent *alarm)
ttl=-1;
bzero((void *)recvaddrbuffer,sizeof(recvaddrbuffer));
int len = recvwithttl(alarm->poll.fd,buffer,sizeof(buffer),&ttl,
recvaddr,&recvaddrlen);
ssize_t len = recvwithttl(alarm->poll.fd,buffer,sizeof(buffer),&ttl, recvaddr, &recvaddrlen);
recvaddr_un=(struct sockaddr_un *)recvaddr;
if (len>0) {
@ -1283,8 +1282,7 @@ int overlay_mdp_recv(overlay_mdp_frame *mdp,int *ttl)
/* Check if reply available */
SET_NONBLOCKING(mdp_client_socket);
int len = recvwithttl(mdp_client_socket,(unsigned char *)mdp,
sizeof(overlay_mdp_frame),ttl,recvaddr,&recvaddrlen);
ssize_t len = recvwithttl(mdp_client_socket,(unsigned char *)mdp, sizeof(overlay_mdp_frame),ttl,recvaddr,&recvaddrlen);
SET_BLOCKING(mdp_client_socket);
recvaddr_un=(struct sockaddr_un *)recvaddr;
@ -1307,7 +1305,7 @@ int overlay_mdp_recv(overlay_mdp_frame *mdp,int *ttl)
int expected_len = overlay_mdp_relevant_bytes(mdp);
if (len < expected_len){
return WHYF("Expected packet length of %d, received only %d bytes", expected_len, len);
return WHYF("Expected packet length of %d, received only %lld bytes", expected_len, (long long) len);
}
/* Valid packet received */
return 0;

View File

@ -26,9 +26,9 @@ struct sockaddr_in loopback = {
.sin_addr.s_addr=0x0100007f
};
int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet,int len,
int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, size_t len,
unsigned char *transaction_id,int recvttl,
struct sockaddr *recvaddr,int recvaddrlen,int parseP)
struct sockaddr *recvaddr, size_t recvaddrlen, int parseP)
{
/*
Overlay packets are ensembles contain one or more frames each of which
@ -270,7 +270,7 @@ int overlay_add_selfannouncement(int interface,overlay_buffer *b)
}
int send_prefix = ++overlay_interfaces[interface].ticks_since_sent_full_address < ticks_per_full_address;
if (!send_prefix)
overlay_interfaces[interface].ticks_since_sent_full_address = 0;
overlay_interfaces[interface].ticks_since_sent_full_address = 0;
/* A TTL for this frame.
XXX - BATMAN uses various TTLs, but I think that it may just be better to have all TTL=1,

View File

@ -288,7 +288,7 @@ int overlay_payload_enqueue(int q,overlay_frame *p,int forceBroadcastP)
if (debug&DEBUG_OVERLAYINTERFACES)
WHYF("Sending %d byte voice packet",b->length);
nextinterface:
if (!overlay_broadcast_ensemble(interface,NULL,b->bytes,b->length))
if (overlay_broadcast_ensemble(interface,NULL,b->bytes,b->length) != -1)
{
overlay_update_sequence_number();
if (debug&DEBUG_OVERLAYINTERFACES)

View File

@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "serval.h"
#include "strbuf.h"
/*
Here we implement the actual routing algorithm which is heavily based on BATMAN.
@ -252,7 +253,7 @@ int overlay_route_init(int mb_ram)
/* Try to catch one observed behaviour when memory corruption has occurred. */
if (overlay_route_initP) {
fprintf(stderr,"ERROR: overlay_route_init() multiply called.\n");
WHY("multiply called");
sleep(3600);
}
overlay_route_initP=1;
@ -262,7 +263,7 @@ int overlay_route_init(int mb_ram)
srandomdev();
/* Generate hash ordering function */
fprintf(stderr,"Generating byte-order for hash function:");
strbuf b = strbuf_alloca(12 * 32);
for(i=0;i<32;i++) {
j=0;
overlay_route_hash_order[i]=random()&31;
@ -270,9 +271,9 @@ int overlay_route_init(int mb_ram)
overlay_route_hash_order[i]=random()&31;
for(j=0;j<i;j++) if (overlay_route_hash_order[i]==overlay_route_hash_order[j]) break;
}
fprintf(stderr," %d",overlay_route_hash_order[i]);
strbuf_sprintf(b, " %d", overlay_route_hash_order[i]);
}
fprintf(stderr,"\n");
DEBUGF("Generating byte-order for hash function:%s", strbuf_str(b));
overlay_route_hash_bytes=16;
int associativity=4;
@ -293,7 +294,7 @@ int overlay_route_init(int mb_ram)
{
space=(sizeof(overlay_neighbour*)*1024LL*mb_ram)+sizeof(overlay_node)*bin_count*associativity*1LL;
int percent=100LL*space/(mb_ram*1048576LL);
fprintf(stderr,"Using %d%% of %dMB RAM allows for %d bins with %d-way associativity and %d direct neighbours.\n",
INFOF("Using %d%% of %dMB RAM allows for %d bins with %d-way associativity and %d direct neighbours",
percent,mb_ram,bin_count,associativity,1024*mb_ram);
}
@ -322,17 +323,17 @@ int overlay_route_init(int mb_ram)
overlay_max_neighbours=1024*mb_ram;
overlay_bin_count=bin_count;
overlay_bin_size=associativity;
fprintf(stderr,"Node (%dbins) and neighbour tables allocated.\n",bin_count);
INFOF("Node (%dbins) and neighbour tables allocated",bin_count);
/* Work out number of bytes required to represent the bin number.
Used for calculation of sid hash */
overlay_bin_bytes=1;
while(bin_count&0xffffff00) {
fprintf(stderr,"bin_count=0x%08x, overlay_bin_bytes=%d\n",bin_count,overlay_bin_bytes);
INFOF("bin_count=0x%08x, overlay_bin_bytes=%d",bin_count,overlay_bin_bytes);
overlay_bin_bytes++;
bin_count=bin_count>>8;
}
fprintf(stderr,"bin_count=0x%08x, overlay_bin_bytes=%d\n",bin_count,overlay_bin_bytes);
INFOF("bin_count=0x%08x, overlay_bin_bytes=%d",bin_count,overlay_bin_bytes);
return 0;
}
@ -394,11 +395,11 @@ int overlay_get_nexthop(unsigned char *d,unsigned char *nexthop,int *nexthoplen,
if (neh->scores[*interface]<1) {
if (debug&DEBUG_OVERLAYROUTING) {
*interface=-1;
WHYF("No open path to %s\n",overlay_render_sid(neh->node->sid));
DEBUGF("No open path to %s",overlay_render_sid(neh->node->sid));
}
return -1;
}
if (0) printf("nexthop is %s\n",overlay_render_sid(nexthop));
if (0) DEBUGF("nexthop is %s",overlay_render_sid(nexthop));
return 0;
} else {
/* Is not a direct neighbour.
@ -452,13 +453,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&DEBUG_OVERLAYROUTING) {
DEBUGF("Address %s resolves to bin #%d", alloca_tohex_sid(sid), bin);
int zeroes=0;
fprintf(stderr,"The following address resolves to bin #%d\n",bin);
for(i=0;i<SID_SIZE;i++) { fprintf(stderr,"%02x",sid[i]); if (!sid[i]) zeroes++; }
fprintf(stderr,"\n");
if (zeroes>8) {
fprintf(stderr,"Looks like corrupt memory or packet to me!\n");
}
for (i=0;i<SID_SIZE;i++)
if (!sid[i])
zeroes++;
if (zeroes>8)
DEBUG("Looks like corrupt memory or packet!");
}
return bin;
}
@ -574,7 +575,7 @@ int overlay_route_ack_selfannounce(overlay_frame *f,
out->ttl=2;
out->isBroadcast=1;
if (debug&DEBUG_OVERLAYROUTING)
WHY("Broadcasting ack to selfannounce for hithero unroutable node");
DEBUG("Broadcasting ack to selfannounce for hithero unroutable node");
} else out->isBroadcast=0;
}
@ -696,7 +697,7 @@ int overlay_route_i_can_hear_node(unsigned char *who,int sender_interface,
unsigned int s1,unsigned int s2,
long long now)
{
if (0) WHYF("I can hear node %s (but I really only care who can hear me)",
if (0) DEBUGF("I can hear node %s (but I really only care who can hear me)",
overlay_render_sid(who));
return 0;
}
@ -713,7 +714,7 @@ int overlay_route_node_can_hear_me(unsigned char *who,int sender_interface,
/* Ignore traffic from ourselves. */
if (overlay_address_is_local(who))
{
WHY("I can hear myself. How odd");
DEBUGF("I can hear myself. How odd");
return 0;
}
@ -741,7 +742,7 @@ int overlay_route_node_can_hear_me(unsigned char *who,int sender_interface,
{
if (neh->observations[obs_index].sender_interface==sender_interface)
{
if (0) WHYF("merging observation in slot #%d",obs_index);
if (0) DEBUGF("merging observation in slot #%d",obs_index);
if (!neh->observations[obs_index].s1)
neh->observations[obs_index].s1=neh->observations[obs_index].s2;
neh->observations[obs_index].s2=s2;
@ -759,7 +760,7 @@ int overlay_route_node_can_hear_me(unsigned char *who,int sender_interface,
/* Replace oldest observation with this one */
obs_index=neh->most_recent_observation_id+1;
if (obs_index>=OVERLAY_MAX_OBSERVATIONS) obs_index=0;
if (0) WHYF("storing observation in slot #%d",obs_index);
if (0) DEBUGF("storing observation in slot #%d",obs_index);
neh->observations[obs_index].valid=0;
neh->observations[obs_index].time_ms=now;
neh->observations[obs_index].s1=s1;
@ -779,16 +780,6 @@ int overlay_route_node_can_hear_me(unsigned char *who,int sender_interface,
return 0;
}
int overlay_print_address(FILE *f,char *prefix,unsigned char *s,char *suffix)
{
int i;
fprintf(f,"%s",prefix);
for(i=0;i<SID_SIZE;i++) fprintf(f,"%02x",s[i]);
fprintf(f,"%s",suffix);
return 0;
}
int overlay_route_saw_selfannounce(overlay_frame *f,long long now)
{
if (overlay_address_is_local(f->source)) return 0;
@ -806,7 +797,7 @@ int overlay_route_saw_selfannounce(overlay_frame *f,long long now)
/* Ignore self announcements from ourselves */
if (overlay_address_is_local(f->source)) {
if(0) WHY("Ignoring selfannouncement from myself");
if(0) DEBUG("Ignoring selfannouncement from myself");
return 0;
}
@ -814,7 +805,7 @@ int overlay_route_saw_selfannounce(overlay_frame *f,long long now)
s2=ntohl(*((int*)&f->payload->bytes[4]));
sender_interface=f->payload->bytes[8];
if (debug&DEBUG_OVERLAYROUTING) {
fprintf(stderr,"Received self-announcement for sequence range [%08x,%08x] from interface %d\n",s1,s2,sender_interface);
DEBUGF("Received self-announcement for sequence range [%08x,%08x] from interface %d",s1,s2,sender_interface);
dump("Payload",&f->payload->bytes[0],f->payload->length);
}
@ -915,7 +906,7 @@ int overlay_route_recalc_neighbour_metrics(overlay_neighbour *n,long long now)
/* Only update every half-second */
if ((now-n->last_metric_update)<500) {
if (0) WHYF("refusing to update metric too often (last at %lldms, now=%lldms)",
if (0) DEBUGF("refusing to update metric too often (last at %lldms, now=%lldms)",
n->last_metric_update,now);
return 0;
}
@ -944,8 +935,7 @@ int overlay_route_recalc_neighbour_metrics(overlay_neighbour *n,long long now)
/* Check the observation age, and ignore if too old */
int obs_age=now-n->observations[i].time_ms;
if (0) WHYF("tallying obs: %dms old, %dms long",
obs_age,interval);
if (0) DEBUGF("tallying obs: %dms old, %dms long", obs_age,interval);
if (obs_age>200000) continue;
/* Ignore very large intervals (>1hour) as being likely to be erroneous.
@ -957,7 +947,7 @@ int overlay_route_recalc_neighbour_metrics(overlay_neighbour *n,long long now)
*/
if (interval<3600000) {
if (debug&DEBUG_VERBOSE_IO)
fprintf(stderr,"adding %dms (interface %d '%s')\n",
DEBUGF("adding %dms (interface %d '%s')",
interval,n->observations[i].sender_interface,
overlay_interfaces[n->observations[i].sender_interface].name);
@ -971,7 +961,7 @@ int overlay_route_recalc_neighbour_metrics(overlay_neighbour *n,long long now)
else
{
WHY("Invalid interface ID in observation");
fprintf(stderr,"XXXXXXX adding %dms (interface %d)\n",interval,n->observations[i].sender_interface);
DEBUGF("XXXXXXX adding %dms (interface %d)",interval,n->observations[i].sender_interface);
}
}
@ -1005,7 +995,7 @@ int overlay_route_recalc_neighbour_metrics(overlay_neighbour *n,long long now)
if (score<0) score=0;
n->scores[i]=score;
if ((debug&DEBUG_OVERLAYROUTING)&&score) fprintf(stderr,"Neighbour score on interface #%d = %d (observations for %dms)\n",i,score,ms_observed_200sec[i]);
if ((debug&DEBUG_OVERLAYROUTING)&&score) DEBUGF("Neighbour score on interface #%d = %d (observations for %dms)",i,score,ms_observed_200sec[i]);
}
return 0;
@ -1061,9 +1051,9 @@ char *overlay_render_sid_prefix(unsigned char *sid,int l)
*/
int overlay_route_saw_selfannounce_ack(overlay_frame *f,long long now)
{
if (0) WHYF("processing selfannounce ack (payload length=%d)",f->payload->length);
if (0) DEBUGF("processing selfannounce ack (payload length=%d)",f->payload->length);
if (!overlay_neighbours) {
if (0) WHY("no neighbours, so returning immediately");
if (0) DEBUG("no neighbours, so returning immediately");
return 0;
}
@ -1077,7 +1067,7 @@ int overlay_route_saw_selfannounce_ack(overlay_frame *f,long long now)
// Call something like the following for each link
if (f->source_address_status==OA_RESOLVED&&
f->destination_address_status==OA_RESOLVED) {
if (0) WHYF("f->source=%s, f->destination=%s",
if (0) DEBUGF("f->source=%s, f->destination=%s",
overlay_render_sid(f->source),overlay_render_sid(f->destination));
overlay_route_record_link(now,f->source,f->source,iface,s1,s2,
0 /* no associated score */,
@ -1096,7 +1086,7 @@ int overlay_route_record_link(long long now,unsigned char *to,
{
int i,slot=-1;
if (0) WHYF("to=%s, via=%s, iface=%d, s1=%d, s2=%d",
if (0) DEBUGF("to=%s, via=%s, iface=%d, s1=%d, s2=%d",
overlay_render_sid(to),overlay_render_sid(via),
sender_interface,s1,s2);
@ -1105,28 +1095,27 @@ int overlay_route_record_link(long long now,unsigned char *to,
/* Don't record routes to ourselves */
if (overlay_address_is_local(to)) {
if (0) WHYF("Ignoring self announce ack addressed to me (%s).",
if (0) DEBUGF("Ignoring self announce ack addressed to me (%s).",
overlay_render_sid(to));
return 0;
}
else if (0) WHYF("Recording link to %s",overlay_render_sid(to));
else if (0) DEBUGF("Recording link to %s",overlay_render_sid(to));
for(i=0;i<SID_SIZE;i++) if (to[i]!=via[i]) break;
if (i==SID_SIZE)
{
/* It's a neighbour observation */
if (0) WHYF("%s is my neighbour",overlay_render_sid(to));
if (0) DEBUGF("%s is my neighbour",overlay_render_sid(to));
overlay_route_node_can_hear_me(to,sender_interface,s1,s2,now);
}
if (!score) {
if (0) WHY("non-scoring report, so done");
if (0) DEBUG("non-scoring report, so done");
return 0;
}
fprintf(stderr,"route_record_link(0x%llx,%s*,",
now,overlay_render_sid_prefix(to,7));
fprintf(stderr,"%s*,0x%08x-0x%08x,%d)\n",
DEBUGF("route_record_link(0x%llx,%s*,%s*,0x%08x-0x%08x,%d)",
now,overlay_render_sid_prefix(to,7),
overlay_render_sid_prefix(via,7),s1,s2,score);
overlay_node *n=overlay_route_find_node(to,SID_SIZE,1 /* create node if missing */);
@ -1190,8 +1179,9 @@ int overlay_route_dump()
{
int bin,slot,o,n,i;
long long now=overlay_gettime_ms();
strbuf b = strbuf_alloca(8192);
fprintf(stderr,"\nOverlay Local Identities\n------------------------\n");
strbuf_sprintf(b,"Overlay Local Identities\n------------------------\n");
int cn,in,kp;
for(cn=0;cn<keyring->context_count;cn++)
for(in=0;in<keyring->contexts[cn]->identity_count;in++)
@ -1200,33 +1190,36 @@ int overlay_route_dump()
==KEYTYPE_CRYPTOBOX)
{
for(i=0;i<SID_SIZE;i++)
fprintf(stderr,"%02x",keyring->contexts[cn]->identities[in]
strbuf_sprintf(b,"%02x",keyring->contexts[cn]->identities[in]
->keypairs[kp]->public_key[i]);
fprintf(stderr,"\n");
strbuf_sprintf(b,"\n");
}
DEBUG(strbuf_str(b));
fprintf(stderr,"\nOverlay Neighbour Table\n------------------------\n");
strbuf_reset(b);
strbuf_sprintf(b,"\nOverlay Neighbour Table\n------------------------\n");
for(n=0;n<overlay_neighbour_count;n++)
if (overlay_neighbours[n].node)
{
fprintf(stderr," %s* : %lldms ago :",
strbuf_sprintf(b," %s* : %lldms ago :",
overlay_render_sid_prefix(overlay_neighbours[n].node->sid,7),
(now-overlay_neighbours[n].last_observation_time_ms));
for(i=0;i<OVERLAY_MAX_INTERFACES;i++)
if (overlay_neighbours[n].scores[i])
fprintf(stderr," %d(via #%d)",
strbuf_sprintf(b," %d(via #%d)",
overlay_neighbours[n].scores[i],i);
fprintf(stderr,"\n");
strbuf_sprintf(b,"\n");
}
DEBUG(strbuf_str(b));
fprintf(stderr,"Overlay Mesh Route Table\n------------------------\n");
strbuf_reset(b);
strbuf_sprintf(b,"Overlay Mesh Route Table\n------------------------\n");
for(bin=0;bin<overlay_bin_count;bin++)
for(slot=0;slot<overlay_bin_size;slot++)
{
if (!overlay_nodes[bin][slot].sid[0]) continue;
fprintf(stderr," %s* : %d :",overlay_render_sid_prefix(overlay_nodes[bin][slot].sid,7),
strbuf_sprintf(b," %s* : %d :",overlay_render_sid_prefix(overlay_nodes[bin][slot].sid,7),
overlay_nodes[bin][slot].best_link_score);
for(o=0;o<OVERLAY_MAX_OBSERVATIONS;o++)
{
@ -1234,13 +1227,14 @@ int overlay_route_dump()
{
overlay_node_observation *ob=&overlay_nodes[bin][slot].observations[o];
if (ob->corrected_score)
fprintf(stderr," %d/%d via %s*",
strbuf_sprintf(b," %d/%d via %s*",
ob->corrected_score,ob->gateways_en_route,
overlay_render_sid_prefix(ob->sender_prefix,7));
}
}
fprintf(stderr,"\n");
strbuf_sprintf(b,"\n");
}
DEBUG(strbuf_str(b));
return 0;
}
@ -1266,7 +1260,7 @@ void overlay_route_tick(struct sched_ent *alarm)
long long start_time=overlay_gettime_ms();
if (debug&DEBUG_OVERLAYROUTING)
DEBUGF("Neighbours: %d@%d, Nodes: %d@%d\n",
DEBUGF("Neighbours: %d@%d, Nodes: %d@%d",
overlay_route_tick_neighbour_bundle_size,overlay_route_tick_next_neighbour_id,
overlay_route_tick_node_bundle_size,overlay_route_tick_next_node_bin_id);
@ -1325,7 +1319,7 @@ void overlay_route_tick(struct sched_ent *alarm)
if (ticks>5000) ticks=5000;
int interval=5000/ticks;
if (debug&DEBUG_OVERLAYROUTING) fprintf(stderr,"route tick interval = %dms (%d ticks per 5sec, neigh=%lldms, node=%lldms)\n",interval,ticks,neighbour_time,node_time);
if (debug&DEBUG_OVERLAYROUTING) DEBUGF("route tick interval = %dms (%d ticks per 5sec, neigh=%lldms, node=%lldms)",interval,ticks,neighbour_time,node_time);
/* Update callback interval based on how much work we have to do */
alarm->alarm = overlay_gettime_ms()+interval;
@ -1366,7 +1360,7 @@ int overlay_route_node_info(overlay_mdp_frame *mdp,
long long now=overlay_gettime_ms();
if (0)
WHYF("Looking for node %s* (prefix len=0x%x)",
DEBUGF("Looking for node %s* (prefix len=0x%x)",
overlay_render_sid_prefix(mdp->nodeinfo.sid,
mdp->nodeinfo.sid_prefix_length),
mdp->nodeinfo.sid_prefix_length

View File

@ -19,8 +19,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "serval.h"
int process_packet(unsigned char *packet,int len,
int recvttl,struct sockaddr *sender,int sender_len)
int process_packet(unsigned char *packet, size_t len,
int recvttl,struct sockaddr *sender, size_t sender_len)
{
//int authenticatedP=0;
char did[128];
@ -59,9 +59,9 @@ int process_packet(unsigned char *packet,int len,
return 0;
}
int packetOk(struct overlay_interface *interface, unsigned char *packet,int len,
int packetOk(struct overlay_interface *interface, unsigned char *packet, size_t len,
unsigned char *transaction_id,int ttl,
struct sockaddr *recvaddr,int recvaddrlen,int parseP)
struct sockaddr *recvaddr, size_t recvaddrlen,int parseP)
{
if (len<HEADERFIELDS_LEN) return WHY("Packet is too short");
@ -87,7 +87,7 @@ int packetOk(struct overlay_interface *interface, unsigned char *packet,int len,
int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id,
int recvttl,
struct sockaddr *recvaddr,int recvaddrlen,int parseP)
struct sockaddr *recvaddr, size_t recvaddrlen, int parseP)
{
/* Make sure that the packet is meant for us, and is not mal-formed */
int version;

View File

@ -668,8 +668,7 @@ int64_t confValueGetInt64Range(const char *var, int64_t defaultValue, int64_t ra
void confSetDebugFlags();
int confParseBoolean(const char *text, const char *option_name);
int recvwithttl(int sock,unsigned char *buffer,int bufferlen,int *ttl,
struct sockaddr *recvaddr,unsigned int *recvaddrlen);
ssize_t recvwithttl(int sock, unsigned char *buffer, size_t bufferlen, int *ttl, struct sockaddr *recvaddr, socklen_t *recvaddrlen);
int is_xsubstring(const char *text, int len);
int is_xstring(const char *text, int len);
@ -699,11 +698,10 @@ void serverCleanUp();
int isTransactionInCache(unsigned char *transaction_id);
void insertTransactionInCache(unsigned char *transaction_id);
int packetOk(struct overlay_interface *interface,unsigned char *packet,int len,
int packetOk(struct overlay_interface *interface,unsigned char *packet, size_t len,
unsigned char *transaction_id, int recvttl,
struct sockaddr *recvaddr,int recvaddrlen,int parseP);
int process_packet(unsigned char *packet,int len,
int recvttl,struct sockaddr *sender,int sender_len);
struct sockaddr *recvaddr, size_t recvaddrlen,int parseP);
int process_packet(unsigned char *packet, size_t len, int recvttl,struct sockaddr *sender, size_t sender_len);
int packetMakeHeader(unsigned char *packet,int packet_maxlen,int *packet_len,unsigned char *transaction_id,int cryptoflags);
int packetSetDid(unsigned char *packet,int packet_maxlen,int *packet_len,char *did);
int packetSetSidFromId(unsigned char *packet,int packet_maxlen,int *packet_len,
@ -743,7 +741,7 @@ int hlrGetVariable(unsigned char *hlr,int hofs,int varid,int varinstance,
unsigned char *value,int *len);
int dumpResponses(struct response_set *responses);
int eraseLastResponse(struct response_set *responses);
int dropPacketP(int packet_len);
int dropPacketP(size_t packet_len);
int clearResponses(struct response_set *responses);
int responseFromPeerP(struct response_set *responses,int peerId);
int responseFromPeer(struct response_set *responses,int peerId);
@ -759,10 +757,10 @@ int openHlrFile(char *backing_file,int size);
int runCommand(char *cmd);
int asteriskObtainGateway(char *requestor_sid,char *did,char *uri_out);
int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id,
int recvttl,struct sockaddr *recvaddr,int recvaddrlen,int parseP);
int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet,int len,
int recvttl,struct sockaddr *recvaddr, size_t recvaddrlen,int parseP);
int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, size_t len,
unsigned char *transaction_id,int recvttl,
struct sockaddr *recvaddr,int recvaddrlen,int parseP);
struct sockaddr *recvaddr, size_t recvaddrlen,int parseP);
int prepareGateway(char *gatewayspec);
int packetSendRequest(int method,unsigned char *packet,int packet_len,int batchP,
unsigned char *transaction_id,struct sockaddr *recvaddr,
@ -786,7 +784,7 @@ void logMessage(int level, const char *file, unsigned int line, const char *func
void vlogMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, va_list);
unsigned int debugFlagMask(const char *flagname);
char *catv(const char *data, char *buf, size_t len);
int dump(char *name,unsigned char *addr,int len);
int dump(char *name, unsigned char *addr, size_t len);
#define alloca_tohex(buf,len) tohex((char *)alloca((len)*2+1), (buf), (len))
#define alloca_tohex_sid(sid) alloca_tohex((sid), SID_SIZE)

View File

@ -49,8 +49,8 @@ void signal_handler(int signal);
int getKeyring(char *s);
int createServerSocket();
int recvwithttl(int sock,unsigned char *buffer,int bufferlen,int *ttl,
struct sockaddr *recvaddr,unsigned int *recvaddrlen)
ssize_t recvwithttl(int sock,unsigned char *buffer, size_t bufferlen,int *ttl,
struct sockaddr *recvaddr, socklen_t *recvaddrlen)
{
struct msghdr msg;
struct iovec iov[1];
@ -71,12 +71,13 @@ int recvwithttl(int sock,unsigned char *buffer,int bufferlen,int *ttl,
msg.msg_controllen = sizeof(struct cmsghdr)*16;
msg.msg_flags = 0;
int len = recvmsg(sock,&msg,0);
ssize_t len = recvmsg(sock,&msg,0);
if (len == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
return WHY_perror("recvmsg");
if (0&&debug&DEBUG_PACKETRX) {
DEBUGF("recvmsg returned %d bytes (flags=%d,msg_controllen=%d)",
len,msg.msg_flags,msg.msg_controllen);
dump("received data",buffer,len);
DEBUGF("recvmsg returned %lld (flags=%d, msg_controllen=%d)", (long long) len, msg.msg_flags, msg.msg_controllen);
dump("received data", buffer, len);
}
struct cmsghdr *cmsg;
@ -199,8 +200,8 @@ int server(char *backing_file)
return -1;
FILE *f=fopen(filename,"w");
if (!f) {
WHYF("Could not write to PID file %s", filename);
WHY_perror("fopen");
WHYF("Could not write to PID file %s", filename);
return -1;
}
server_getpid = getpid();
@ -250,8 +251,10 @@ int server_create_stopfile()
if (!FORM_SERVAL_INSTANCE_PATH(stopfile, STOPFILE_NAME))
return -1;
FILE *f;
if ((f = fopen(stopfile, "w")) == NULL)
return WHYF("Could not create stopfile '%s': %s [errno=%d]", stopfile, strerror(errno), errno);
if ((f = fopen(stopfile, "w")) == NULL) {
WHY_perror("fopen");
return WHYF("Could not create stopfile '%s'", stopfile);
}
fclose(f);
return 0;
}
@ -264,7 +267,8 @@ int server_remove_stopfile()
if (unlink(stopfile) == -1) {
if (errno == ENOENT)
return 0;
return WHYF("Could not unlink stopfile '%s': %s [errno=%d]", stopfile, strerror(errno), errno);
WHY_perror("unlink");
return WHYF("Could not unlink stopfile '%s'", stopfile);
}
return 1;
}
@ -279,7 +283,8 @@ int server_check_stopfile()
return 1;
if (r == -1 && errno == ENOENT)
return 0;
WHYF("Access check for stopfile '%s' failed: %s [errno=%d]", stopfile, strerror(errno), errno);
WHY_perror("access");
WHYF("Cannot access stopfile '%s'", stopfile);
return -1;
}
@ -410,7 +415,7 @@ void signal_handler(int signal)
{
char buf[80];
signame(buf, sizeof(buf), signal);
WHYF("Caught %s", buf);
INFOF("Caught %s", buf);
switch (signal) {
case SIGQUIT:
serverCleanUp();
@ -426,7 +431,7 @@ void signal_handler(int signal)
return;
}
/* oops - caught a bad signal -- exec() ourselves fresh */
WHY("Respawning");
INFO("Respawning");
if (sock>-1) close(sock);
int i;
for(i=0;i<overlay_interface_count;i++)
@ -709,7 +714,6 @@ int processRequest(unsigned char *packet,int len,
}
break;
default:
WHY("Asked to perform unsupported action");
if (debug&DEBUG_PACKETFORMATS) DEBUGF("Asked to perform unsipported action at Packet offset = 0x%x",pofs);
if (debug&DEBUG_PACKETFORMATS) dump("Packet",packet,len);
return WHY("Asked to perform unsupported action.");
@ -782,8 +786,8 @@ int createServerSocket()
sock=socket(PF_INET,SOCK_DGRAM,0);
if (sock<0) {
WHY("Could not create UDP socket.");
WHY_perror("socket");
WHY("Could not create UDP socket.");
exit(-3);
}
@ -804,8 +808,8 @@ int createServerSocket()
bind_addr.sin_port = htons( PORT_DNA );
bind_addr.sin_addr.s_addr = htonl( INADDR_ANY );
if(bind(sock,(struct sockaddr *)&bind_addr,sizeof(bind_addr))) {
WHYF("MP HLR server could not bind to UDP port %d", PORT_DNA);
WHY_perror("bind");
WHYF("MP HLR server could not bind to UDP port %d", PORT_DNA);
exit(-3);
}
return 0;

View File

@ -25,9 +25,10 @@ double simulatedBER=0;
We use this function to simulate a lossy link so that we can easily bench-test the
retransmission protocols.
*/
int dropPacketP(int packet_len)
int dropPacketP(size_t packet_len)
{
int i,b;
size_t i;
int b;
long berThreshold=0x7fffffff*simulatedBER;