Expunge setReason()

Replace with WHY() or WHYF() everywhere

Improve logMessage() to handle file/line/function printing internally

Mainly so that log messages from manifest alloc/free in rhizome_bundle.c
can make it into the Android log
This commit is contained in:
Andrew Bettison 2012-05-24 17:11:55 +09:30
parent 3b00110c49
commit 0ec3feaa29
13 changed files with 156 additions and 162 deletions

View File

@ -71,7 +71,7 @@ int readRoutingTable(struct in_addr peers[],int *peer_count,int peer_max){
}
ERROR:
fclose(fp);
return setReason("Unable to parse routing table");
return WHY("Unable to parse routing table");
}
if (!(flgs & RTF_UP)) { /* Skip interfaces that are down. */
@ -123,7 +123,7 @@ int readArpTable(struct in_addr peers[],int *peer_count,int peer_max){
}
ERROR:
fclose(fp);
return setReason("Unable to parse arp table");
return WHY("Unable to parse arp table");
}
if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=d;
@ -203,17 +203,17 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
sock=socket(AF_LOCAL,SOCK_STREAM,0);
memset(&socket_address,0,sizeof(struct sockaddr_un));
socket_address.sun_family=AF_LOCAL;
if (strlen(socket_path)>256) return setReason("BATMAN socket path too long");
if (strlen(socket_path)>256) return WHY("BATMAN socket path too long");
strcpy(socket_address.sun_path,socket_path);
/* Connect the socket */
if (connect(sock,(struct sockaddr*)&socket_address,sizeof(socket_address))<0)
return setReason("connect() to BATMAN socket failed.");
return WHY("connect() to BATMAN socket failed.");
memset(&cmd[0],0,30);
snprintf(cmd,30,"d:%c",1);
if (write(sock,cmd,30)!=30)
{ close(sock); return setReason("write() command failed to BATMAN socket."); }
{ close(sock); return WHY("write() command failed to BATMAN socket."); }
fds.fd=sock;
fds.events=POLLIN;
@ -227,10 +227,10 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
case 0: if (debug&DEBUG_PEERS) fprintf(stderr,"BATMAN did not respond to peer enquiry.\n");
close(sock);
if (tries++<=3) goto askagain;
return setReason("No response from BATMAN.");
return WHY("No response from BATMAN.");
default: /* some sort of error, e.g., lost connection */
close(sock);
return setReason("poll() of BATMAN socket failed.");
return WHY("poll() of BATMAN socket failed.");
}
res=read(sock,&buf[bytes],16383-bytes); close(sock);
@ -245,9 +245,9 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
close(sock);
bytes=0;
if (tries++<=3) goto askagain;
else return setReason("failed to read() from BATMAN socket (too many tries).");
else return WHY("failed to read() from BATMAN socket (too many tries).");
}
return setReason("failed to read() from BATMAN socket.");
return WHY("failed to read() from BATMAN socket.");
}
if (!res) return 0;
if (debug&DEBUG_PEERS) fprintf(stderr,"BATMAN has responded with %d bytes.\n",res);

View File

@ -27,17 +27,17 @@ int packetSetMySid(char *sid)
/* Set SID identity if not the first SID in the HLR */
return setReason("Not implemented");
return WHY("Not implemented");
}
int packetGetPrivateKeyForSid()
{
return setReason("Not implemented");
return WHY("Not implemented");
}
int packetClearPrivateKeys()
{
return setReason("Not implemented");
return WHY("Not implemented");
}
int packetDecipher(unsigned char *packet,int len,int cipher)
@ -62,7 +62,7 @@ int packetDecipher(unsigned char *packet,int len,int cipher)
/* encrypt and sign, i.e., crypto_box() */
return 0;
default:
return setReason("Unknown packet cipher");
return WHY("Unknown packet cipher");
}
}
@ -73,7 +73,7 @@ int packetEncipher(unsigned char *packet,int maxlen,int *len,int cryptoflags)
if (cryptoflags)
{
return setReason("Unknown packet cipher");
return WHY("Unknown packet cipher");
}
else return 0; /* plain text */
}

View File

@ -274,7 +274,7 @@ int requestNewHLR(char *did,char *pin,char *sid,
return -1;
}
return setReason("Request creation of new HLR not implemented");
return WHY("Request creation of new HLR not implemented");
}
/* Some data types can end in @ if they require the address of the sender to be appended for correct local interpretation */
@ -366,7 +366,7 @@ int getReplyPackets(int method,int peer,int batchP,struct response_set *response
if (!recvaddr) recvaddr=&reply_recvaddr;
len=recvwithttl(sock,buffer,sizeof(buffer),&ttl,recvaddr,&recvaddrlen);
if (len<=0) return setReason("Unable to receive packet.");
if (len<=0) return WHY("Unable to receive packet.");
if (recvaddr) {
client_port=((struct sockaddr_in *)recvaddr)->sin_port;
@ -384,7 +384,7 @@ int getReplyPackets(int method,int peer,int batchP,struct response_set *response
if (!packetOk(-1,buffer,len,transaction_id,ttl,recvaddr,recvaddrlen,0)) {
/* Packet passes tests - extract responses and append them to the end of the response list */
if (extractResponses(client_addr,buffer,len,responses))
return setReason("Problem extracting response fields from reply packets");
return WHY("Problem extracting response fields from reply packets");
if (method==REQ_SERIAL||method==REQ_FIRSTREPLY) {
if (!batchP) return 0;
/* In batch mode we need ACTION_DONE to mark end of transmission.
@ -406,7 +406,7 @@ int getReplyPackets(int method,int peer,int batchP,struct response_set *response
if (debug&DEBUG_DNARESPONSES) printf("Waiting for more packets, since called with policy %d\n",method);
}
} else {
if (debug&(DEBUG_PACKETRX|DEBUG_DNARESPONSES)) setReason("Ignoring invalid packet");
if (debug&(DEBUG_PACKETRX|DEBUG_DNARESPONSES)) WHY("Ignoring invalid packet");
}
}
}
@ -446,7 +446,7 @@ int writeItem(char *sid,int var_id,int instance,unsigned char *value,
flags|((o>value_start)?SET_FRAGMENT:0),recvttl,NULL))
{
if (debug&DEBUG_DNAVARS) fprintf(stderr," - writing installment failed\n");
return setReason("Failure during multi-packet write of long-value");
return WHY("Failure during multi-packet write of long-value");
}
}
printf("OK:%s\n",sid);
@ -538,7 +538,7 @@ int peerAddress(char *did,char *sid,int flags)
}
else {
if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"%s() failed at line %d\n",__FUNCTION__,__LINE__);
return setReason("You must request items by DID or SID, not neither, nor both");
return WHY("You must request items by DID or SID, not neither, nor both");
}
@ -620,7 +620,7 @@ int requestItem(char *did,char *sid,char *item,int instance,
}
else {
if (debug&DEBUG_DNAREQUESTS) fprintf(stderr,"requestItem() failed at line %d\n",__LINE__);
return setReason("You must request items by DID or SID, not neither, nor both");
return WHY("You must request items by DID or SID, not neither, nor both");
}
@ -752,7 +752,7 @@ int requestItem(char *did,char *sid,char *item,int instance,
}
if (packetSetSid(packet,8000,&packet_len,sid)) {
if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"requestItem() failed at line %d\n",__LINE__);
return setReason("SID went mouldy during multi-packet get");
return WHY("SID went mouldy during multi-packet get");
}
}

View File

@ -200,7 +200,7 @@ static void complainCommandLine(const char *prefix, int argc, const char *const
strbuf_puts(b, arg);
}
}
setReason("%s%s%s", prefix, strbuf_str(b), strbuf_overrun(b) ? "..." : "");
WHYF("%s%s%s", prefix, strbuf_str(b), strbuf_overrun(b) ? "..." : "");
}
/* args[] excludes command name (unless hardlinks are used to use first words
@ -250,9 +250,9 @@ int parseCommandLine(int argc, const char *const *args)
that the call is ambiguous. */
if (cli_call>=0) ambiguous++;
if (ambiguous==1) {
setReason("Ambiguous command line call:");
WHY("Ambiguous command line call:");
complainCommandLine(" ", argc, args);
setReason("Matches the following known command line calls:");
WHY("Matches the following known command line calls:");
complainCommandLine(" ", argc, command_line_options[cli_call].words);
}
if (ambiguous) {
@ -266,7 +266,7 @@ int parseCommandLine(int argc, const char *const *args)
if (ambiguous) return -1;
/* Complain if we found no matching calls */
if (cli_call<0) {
setReason("Unknown command line call:");
WHY("Unknown command line call:");
complainCommandLine(" ", argc, args);
return cli_usage();
}
@ -1369,9 +1369,9 @@ int app_keyring_add(int argc, const char *const *argv, struct command_line_optio
if (!k)
return -1;
if (keyring_create_identity(k,k->contexts[0],(char *)pin)==NULL)
return setReason("Could not create new identity (keyring_create_identity() failed)");
return WHY("Could not create new identity (keyring_create_identity() failed)");
if (keyring_commit(k))
return setReason("Could not write new identity (keyring_commit() failed)");
return WHY("Could not write new identity (keyring_commit() failed)");
keyring_free(k);
return 0;
}

View File

@ -40,7 +40,7 @@ int extractDid(unsigned char *packet,int *ofs,char *did)
}
if (highP) highP=0; else { (*ofs)++; highP=1; }
}
if (d>63) return setReason("DID too long");
if (d>63) return WHY("DID too long");
did[d]=0;
return 0;
@ -65,7 +65,7 @@ int stowDid(unsigned char *packet,int *ofs,char *did)
case '#': nybl=0xb; break;
case '+': nybl=0xc; break;
default:
setReason("Illegal digits in DID number");
WHY("Illegal digits in DID number");
return -1;
}
if (highP) { packet[*ofs]=nybl<<4; highP=0; }
@ -77,7 +77,7 @@ int stowDid(unsigned char *packet,int *ofs,char *did)
}
if (d>=DID_MAXSIZE)
{
setReason("DID number too long");
WHY("DID number too long");
return -1;
}
/* Append end of number code, filling the whole byte for fast and easy comparison */
@ -168,7 +168,7 @@ int hexvalue(unsigned char c)
if (c>='0'&&c<='9') return c-'0';
if (c>='A'&&c<='F') return c-'A'+10;
if (c>='a'&&c<='f') return c-'a'+10;
return setReason("Invalid hex digit in SID");
return WHY("Invalid hex digit in SID");
}
int packetGetID(unsigned char *packet,int len,char *did,char *sid)
@ -179,22 +179,22 @@ int packetGetID(unsigned char *packet,int len,char *did,char *sid)
{
case 0: /* DID */
ofs++;
if (extractDid(packet,&ofs,did)) return setReason("Could not decode DID");
if (extractDid(packet,&ofs,did)) return WHY("Could not decode DID");
if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"Decoded DID as %s\n",did);
return 0;
break;
case 1: /* SID */
ofs++;
if (len<(OFS_SIDDIDFIELD+SID_SIZE)) return setReason("Packet too short");
if (extractSid(packet,&ofs,sid)) return setReason("Could not decode SID");
if (len<(OFS_SIDDIDFIELD+SID_SIZE)) return WHY("Packet too short");
if (extractSid(packet,&ofs,sid)) return WHY("Could not decode SID");
return 0;
break;
default: /* no idea */
return setReason("Unknown ID key");
return WHY("Unknown ID key");
break;
}
return setReason("Impossible event #1 just occurred");
return WHY("Impossible event #1 just occurred");
}
/*

16
dna.c
View File

@ -257,7 +257,7 @@ int parseAssignment(unsigned char *text,int *var_id,unsigned char *value,int *va
int tlen=strlen((char *)text);
if (tlen>3072) {
return setReason("Variable assignment string is too long, use =@file to read value from a file");
return WHY("Variable assignment string is too long, use =@file to read value from a file");
}
/* Identify which variable */
@ -267,7 +267,7 @@ int parseAssignment(unsigned char *text,int *var_id,unsigned char *value,int *va
/* Go through known keyring variables */
if (!strcasecmp((char *)text,"did")) *var_id=KEYTYPE_DID;
if (*var_id==-1) return setReason("Illegal variable name in assignment");
if (*var_id==-1) return WHY("Illegal variable name in assignment");
i++;
switch(text[i])
@ -276,10 +276,10 @@ int parseAssignment(unsigned char *text,int *var_id,unsigned char *value,int *va
i++;
while(i<tlen) {
int b=hexvalue(text[i++])<<4;
if (i>=tlen) return setReason("Variable value has an odd number of hex digits.");
if (i>=tlen) return WHY("Variable value has an odd number of hex digits.");
b|=hexvalue(text[i++]);
if (b<0) return setReason("That doesn't look like hex to me");
if (vlen>=max_len) return setReason("Variable hex value too long");
if (b<0) return WHY("That doesn't look like hex to me");
if (vlen>=max_len) return WHY("Variable hex value too long");
value[vlen++]=b;
}
*value_len=vlen;
@ -291,10 +291,10 @@ int parseAssignment(unsigned char *text,int *var_id,unsigned char *value,int *va
int flen;
fseek(f,0,SEEK_END);
flen=ftell(f);
if (flen>max_len) return setReason("Variable value from file too long");
if (flen>max_len) return WHY("Variable value from file too long");
fseek(f,0,SEEK_SET);
vlen=fread(value,1,flen,f);
if (vlen!=flen) return setReason("Could not read all of file");
if (vlen!=flen) return WHY("Could not read all of file");
fclose(f);
*value_len=vlen;
return 0;
@ -302,7 +302,7 @@ int parseAssignment(unsigned char *text,int *var_id,unsigned char *value,int *va
break;
default: /* literal string */
vlen=strlen((char *)&text[i]);
if (vlen>max_len) return setReason("Variable value too long");
if (vlen>max_len) return WHY("Variable value too long");
bcopy(&text[i],value,vlen);
*value_len=vlen;
return 0;

25
log.c
View File

@ -27,19 +27,19 @@ int debug = 0;
#include <android/log.h>
#endif
void logMessage(int level, char *fmt, ...)
void logMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vlogMessage(level, fmt, ap);
vlogMessage(level, file, line, function, fmt, ap);
va_end(ap);
}
void vlogMessage(int level, char *fmt, va_list ap)
void vlogMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, va_list ap)
{
char buf[8192];
vsnprintf(buf, sizeof buf, fmt, ap);
buf[sizeof buf - 1] = '\0';
strbuf b = strbuf_alloca(8192);
strbuf_sprintf(b, "%s:%u:%s() ", file ? trimbuildpath(file) : "NULL", line, function ? function : "NULL");
strbuf_vsprintf(b, fmt, ap);
#ifdef ANDROID
int alevel = ANDROID_LOG_UNKNOWN;
switch (level) {
@ -49,7 +49,7 @@ void vlogMessage(int level, char *fmt, va_list ap)
case LOG_LEVEL_WARN: alevel = ANDROID_LOG_WARN; break;
case LOG_LEVEL_DEBUG: alevel = ANDROID_LOG_DEBUG; break;
}
__android_log_print(alevel, "servald", "%s", buf);
__android_log_print(alevel, "servald", "%s", strbuf_str(b));
#endif
const char *levelstr = "UNKNOWN";
switch (level) {
@ -59,7 +59,7 @@ void vlogMessage(int level, char *fmt, va_list ap)
case LOG_LEVEL_WARN: levelstr = "WARN"; break;
case LOG_LEVEL_DEBUG: levelstr = "DEBUG"; break;
}
fprintf(stderr, "%s: %s\n", levelstr, buf);
fprintf(stderr, "%s: %s\n", levelstr, strbuf_str(b));
}
const char *trimbuildpath(const char *path)
@ -76,15 +76,6 @@ const char *trimbuildpath(const char *path)
return &path[lastsep];
}
int setReason(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vlogMessage(LOG_LEVEL_ERROR, fmt, ap);
va_end(ap);
return -1;
}
int dump(char *name, unsigned char *addr, int len)
{
int i,j;

View File

@ -30,7 +30,7 @@ int process_packet(unsigned char *packet,int len,
did[0]=0; sid[0]=0;
/* Get DID or SID */
if (packetGetID(packet,len,did,sid)) return setReason("Could not parse DID or SID");
if (packetGetID(packet,len,did,sid)) return WHY("Could not parse DID or SID");
/* Check for PIN */
if (!isFieldZeroP(packet,OFS_PINFIELD,16))
@ -41,10 +41,10 @@ int process_packet(unsigned char *packet,int len,
if (debug&DEBUG_SECURITY) fprintf(stderr,"A PIN has been supplied.\n");
/* Can only authenticate by SID, not DID (since DIDs are ambiguous) */
if (packet[OFS_SIDDIDFIELD]!=1) return setReason("You can only authenticate against a SID");
if (packet[OFS_SIDDIDFIELD]!=1) return WHY("You can only authenticate against a SID");
/* XXX check authentication */
return setReason("Authentication not yet supported");
return WHY("Authentication not yet supported");
}
else
{
@ -63,7 +63,7 @@ int packetOk(int interface,unsigned char *packet,int len,
unsigned char *transaction_id,int ttl,
struct sockaddr *recvaddr,int recvaddrlen,int parseP)
{
if (len<HEADERFIELDS_LEN) return setReason("Packet is too short");
if (len<HEADERFIELDS_LEN) return WHY("Packet is too short");
if (packet[0]==0x41&&packet[1]==0x10)
{
@ -79,10 +79,10 @@ int packetOk(int interface,unsigned char *packet,int len,
}
else
/* We ignore overlay mesh packets in simple server mode, which is indicated by interface==-1 */
return setReason("Ignoring overlay mesh packet");
return WHY("Ignoring overlay mesh packet");
}
return setReason("Packet type not recognised.");
return WHY("Packet type not recognised.");
}
int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id,
@ -98,13 +98,13 @@ int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id,
version=(packet[2]<<8)|packet[3];
length=(packet[4]<<8)|packet[5];
cipher=(packet[6]<<8)|packet[7];
if (version!=1) return setReason("Unknown packet format version");
if (cipher!=0) return setReason("Unknown packet cipher");
if (length!=len) return setReason("Packet length incorrect");
if (version!=1) return WHY("Unknown packet format version");
if (cipher!=0) return WHY("Unknown packet cipher");
if (length!=len) return WHY("Packet length incorrect");
if (cipher)
if (packetDecipher(packet,len,cipher))
return setReason("Could not decipher packet");
return WHY("Could not decipher packet");
/* Make sure the transaction ID matches */
if (transaction_id)
@ -112,7 +112,7 @@ int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id,
int i;
for(i=0;i<TRANSID_SIZE;i++)
if (packet[OFS_TRANSIDFIELD+i]!=transaction_id[i])
return setReason("transaction ID mismatch");
return WHY("transaction ID mismatch");
}
/* Unrotate the payload */
@ -199,7 +199,7 @@ int packetSetSid(unsigned char *packet,int packet_maxlen,int *packet_len,char *s
if (strlen(sid)!=64) {
if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"Invalid SID: [%s] - should be 64 hex digits\n",sid);
return setReason("SID must consist of 64 hex digits");
return WHY("SID must consist of 64 hex digits");
}
packet[ofs++]=0x01; /* SID */
@ -317,18 +317,18 @@ int packetAddVariableRequest(unsigned char *packet,int packet_maxlen,int *packet
/* Sanity check the request */
if (!vars[itemId].name) {
if (debug&(DEBUG_DNAVARS|DEBUG_PACKETFORMATS)) fprintf(stderr,"`%s' is not a known HLR variable.\n",item);
return setReason("Requested unknown HLR variable");
return WHY("Requested unknown HLR variable");
}
itemId=vars[itemId].id;
if (instance<-1) return setReason("Asked for illegal variable value instance (<-1)");
if (instance>0xfe) return setReason("Asked for illegal variable value instance (>0xfe)");
if (instance<-1) return WHY("Asked for illegal variable value instance (<-1)");
if (instance>0xfe) return WHY("Asked for illegal variable value instance (>0xfe)");
if ((instance!=-1)&&(itemId<0x80)&&instance) {
return setReason("Asked for secondary value of single-value variable for read");
return WHY("Asked for secondary value of single-value variable for read");
}
if (start_offset<0||start_offset>0xffff) return setReason("Asked for illegal variable value starting offset");
if (start_offset<0||start_offset>0xffff) return WHY("Asked for illegal variable value starting offset");
if (bytes<0||(start_offset+bytes)>0xffff) {
if (debug&(DEBUG_PACKETFORMATS|DEBUG_DNAVARS)) fprintf(stderr,"Asked for %d bytes at offset %d\n",bytes,start_offset);
return setReason("Asked for illegal variable value ending offset");
return WHY("Asked for illegal variable value ending offset");
}
/* Add request to the packet */
@ -360,12 +360,12 @@ int packetAddVariableWrite(unsigned char *packet,int packet_maxlen,
/* Sanity check */
if (itemId&0x80) {
if (instance<0) return setReason("Asked for illegal variable value instance (<0)");
if (instance>0xfe) return setReason("Asked for illegal variable value instance (>0xfe)");
if (instance<0) return WHY("Asked for illegal variable value instance (<0)");
if (instance>0xfe) return WHY("Asked for illegal variable value instance (>0xfe)");
}
if ((itemId<0x80)&&instance&&(instance!=-1)) return setReason("Asked for secondary value of single-value variable for write");
if (start_offset<0||start_offset>0xffff) return setReason("Asked for illegal variable value starting offset");
if (max_offset<0||max_offset>0xffff) return setReason("Asked for illegal variable value ending offset");
if ((itemId<0x80)&&instance&&(instance!=-1)) return WHY("Asked for secondary value of single-value variable for write");
if (start_offset<0||start_offset>0xffff) return WHY("Asked for illegal variable value starting offset");
if (max_offset<0||max_offset>0xffff) return WHY("Asked for illegal variable value ending offset");
/* Add request to the packet */
CHECK_PACKET_LEN(1+1+((itemId&0x80)?1:0)+2+2+1);
@ -394,7 +394,7 @@ int extractRequest(unsigned char *packet,int *packet_ofs,int packet_len,
int *start_offset,int *bytes,int *flags)
{
if (*packet_ofs<0||(*packet_ofs)+6>=packet_len)
return setReason("mal-formed request packet (packet too short/bad offset)");
return WHY("mal-formed request packet (packet too short/bad offset)");
*itemId=packet[(*packet_ofs)++];
@ -413,7 +413,7 @@ int extractRequest(unsigned char *packet,int *packet_ofs,int packet_len,
if (*packet_ofs<0||(*packet_ofs)+(*bytes)>=packet_len)
{
if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"Packet offset is %d, length is %d, and asked for %d bytes.\n",*packet_ofs,packet_len,*bytes);
return setReason("mal-formed request packet (too short for claimed data)");
return WHY("mal-formed request packet (too short for claimed data)");
}
bcopy(&packet[*packet_ofs],value,*bytes);
@ -433,7 +433,7 @@ int extractResponses(struct in_addr sender,unsigned char *buffer,int len,struct
{
/* XXX should allocate responses from a temporary and bounded slab of memory */
struct response *r=calloc(sizeof(struct response),1);
if (!r) exit(setReason("calloc() failed."));
if (!r) exit(WHY("calloc() failed."));
r->code=buffer[ofs];
r->sender=sender;
@ -491,7 +491,7 @@ int extractResponses(struct in_addr sender,unsigned char *buffer,int len,struct
free(r);
if (debug&(DEBUG_DNARESPONSES|DEBUG_PACKETFORMATS)) fprintf(stderr,"Encountered unimplemented response code 0x%02x @ 0x%x\n",buffer[ofs],ofs);
fixResponses(responses);
return setReason("Encountered unimplemented response type");
return WHY("Encountered unimplemented response type");
}
ofs++;
if (r->response_len) {
@ -499,7 +499,7 @@ int extractResponses(struct in_addr sender,unsigned char *buffer,int len,struct
unsigned char *rr;
if (r->response) rr=r->response; else rr=&buffer[ofs];
r->response=malloc(r->response_len+1);
if (!r->response) exit(setReason("malloc() failed."));
if (!r->response) exit(WHY("malloc() failed."));
bcopy(&rr[0],r->response,r->response_len);
r->response[r->response_len]=0;
ofs+=r->response_len;
@ -541,7 +541,7 @@ int packageVariableSegment(unsigned char *data,int *dlen,
int bytes;
int dlen_in=*dlen;
if ((buffer_size-(*dlen))<8) return setReason("Insufficient buffer space for packageVariableSegment()");
if ((buffer_size-(*dlen))<8) return WHY("Insufficient buffer space for packageVariableSegment()");
/* Figure out how many bytes we need to package */
bytes=buffer_size-(*dlen)-8;
@ -580,7 +580,7 @@ int packageVariableSegment(unsigned char *data,int *dlen,
int unpackageVariableSegment(unsigned char *data,int dlen,int flags,struct response *r)
{
r->response_len=0;
if (dlen<7) return setReason("unpackageVariableSegment() fed insufficient data");
if (dlen<7) return WHY("unpackageVariableSegment() fed insufficient data");
r->var_id=data[r->response_len++];
if (r->var_id&0x80) r->var_instance=data[r->response_len++]; else r->var_instance=0;
@ -601,7 +601,7 @@ int unpackageVariableSegment(unsigned char *data,int dlen,int flags,struct respo
if (flags!=WITHOUTDATA)
if (r->response_len>dlen)
return setReason("unpackageVariableSegment() fed insufficient or corrupt data");
return WHY("unpackageVariableSegment() fed insufficient or corrupt data");
return 0;
}

View File

@ -32,10 +32,10 @@ int additionalPeer(char *peer)
{
struct in_addr pa;
if (nom_peer_count>255) return setReason("Too many peers. You can only nominate 255 peers in this version.");
if (nom_peer_count>255) return WHY("Too many peers. You can only nominate 255 peers in this version.");
pa.s_addr=inet_addr(peer);
if (pa.s_addr==INADDR_NONE) return setReason("Invalid peer address specified.");
if (pa.s_addr==INADDR_NONE) return WHY("Invalid peer address specified.");
nominated_peers[nom_peer_count++]=pa;
return 0;

View File

@ -287,8 +287,7 @@ const char *manifest_free_sourcefiles[MAX_RHIZOME_MANIFESTS];
const char *manifest_free_functions[MAX_RHIZOME_MANIFESTS];
int manifest_free_lines[MAX_RHIZOME_MANIFESTS];
rhizome_manifest *_rhizome_new_manifest(const char *filename,const char *funcname,
int line)
rhizome_manifest *_rhizome_new_manifest(const char *filename, const char *funcname, int line)
{
if (manifest_first_free<0) {
/* Setup structures */
@ -309,15 +308,15 @@ rhizome_manifest *_rhizome_new_manifest(const char *filename,const char *funcnam
if (manifest_first_free>=MAX_RHIZOME_MANIFESTS)
{
int i;
fprintf(stderr,"%s:%d:%s() call to rhizome_new_manifest() could not be serviced.\n (no free manifest records, this probably indicates a memory leak.)\n",
filename,line,funcname);
fprintf(stderr," Manifest Slot# | Last allocated by\n");
logMessage(LOG_LEVEL_ERROR, filename, line, funcname, "%s(): no free manifest records, this probably indicates a memory leak", __FUNCTION__);
WHYF(" Manifest Slot# | Last allocated by");
for(i=0;i<MAX_RHIZOME_MANIFESTS;i++) {
fprintf(stderr," %-14d | %s:%d in %s()\n",
WHYF(" %-14d | %s:%d in %s()",
i,
manifest_alloc_sourcefiles[i],
manifest_alloc_lines[i],
manifest_alloc_functions[i]);
manifest_alloc_functions[i]
);
}
return NULL;
}
@ -353,19 +352,21 @@ void _rhizome_manifest_free(const char *sourcefile,const char *funcname,int line
int mid=m->manifest_record_number;
if (m!=&manifests[mid]) {
fprintf(stderr,"%s:%d:%s() called rhizome_manifest_free() and asked to free"
" manifest %p, which claims to be manifest slot #%d (%p), but isn't.\n",
sourcefile,line,funcname,m,mid,&manifests[mid]);
logMessage(LOG_LEVEL_ERROR, sourcefile, line, funcname,
"%s(): asked to free manifest %p, which claims to be manifest slot #%d (%p), but isn't",
__FUNCTION__, m, mid, &manifests[mid]
);
exit(-1);
}
if (manifest_free[mid]) {
fprintf(stderr,"%s:%d:%s() called rhizome_manifest_free() and asked to free"
" manifest slot #%d (%p), which has already been freed at %s:%d:%s().\n",
sourcefile,line,funcname,mid,m,
logMessage(LOG_LEVEL_ERROR, sourcefile, line, funcname,
"%s(): asked to free manifest slot #%d (%p), which was already freed at %s:%d:%s()",
__FUNCTION__, mid, m,
manifest_free_sourcefiles[mid],
manifest_free_lines[mid],
manifest_free_functions[mid]);
manifest_free_functions[mid]
);
exit(-1);
}
@ -471,12 +472,12 @@ int rhizome_manifest_add_group(rhizome_manifest *m,char *groupid)
return WHY("Not implemented.");
}
int rhizome_manifest_dump(rhizome_manifest *m,char *msg)
int rhizome_manifest_dump(rhizome_manifest *m, const char *msg)
{
int i;
fprintf(stderr,"Dumping manifest %s:\n",msg);
WHYF("Dumping manifest %s:", msg);
for(i=0;i<m->var_count;i++)
fprintf(stderr,"[%s]=[%s]\n",m->vars[i],m->values[i]);
WHYF("[%s]=[%s]\n", m->vars[i], m->values[i]);
return 0;
}

View File

@ -137,7 +137,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
*/
if (debug&DEBUG_RHIZOME) {
#warning WHY("Group handling not completely thought out here yet.");
#warning DEBUG("Group handling not completely thought out here yet.");
}
/* Get number of bundles available if required */
@ -147,7 +147,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
if (bundles_available==-1||(bundle_offset[1]>=bundles_available))
bundle_offset[1]=0;
if(0)
WHYF("%d bundles in database (%d %d), slots=%d.",bundles_available,
DEBUGF("%d bundles in database (%d %d), slots=%d.",bundles_available,
bundle_offset[0],bundle_offset[1],slots);
sqlite3_stmt *statement=NULL;
@ -196,7 +196,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
int blob_bytes=sqlite3_blob_bytes(blob);
if (pass&&(blob_bytes!=RHIZOME_BAR_BYTES)) {
if (debug&DEBUG_RHIZOME)
WHY("Found a BAR that is the wrong size - ignoring");
DEBUG("Found a BAR that is the wrong size - ignoring");
sqlite3_blob_close(blob); blob=NULL;
continue;
}
@ -204,7 +204,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
/* Only include manifests that are <=1KB inline.
Longer ones are only advertised by BAR */
if (blob_bytes>1024) {
WHY("blob>1k - ignoring");
WARN("blob>1k - ignoring");
sqlite3_blob_close(blob); blob=NULL;
continue;
}
@ -215,12 +215,12 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
int overhead=0;
int frameFull=0;
if (!pass) overhead=2;
if (0) WHYF("e=%p, e->bytes=%p,e->length=%d, e->allocSize=%d",
if (0) DEBUGF("e=%p, e->bytes=%p,e->length=%d, e->allocSize=%d",
e,e->bytes,e->length,e->allocSize);
if (ob_makespace(e,overhead+2+blob_bytes)) {
if (0&&debug&DEBUG_RHIZOME)
WHYF("Stopped cramming %s into Rhizome advertisement frame.",
DEBUGF("Stopped cramming %s into Rhizome advertisement frame.",
pass?"BARs":"manifests");
frameFull=1;
}
@ -231,7 +231,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
ob_setbyte(e,e->length,(blob_bytes>>8)&0xff);
ob_setbyte(e,e->length+1,(blob_bytes>>0)&0xff);
if (0&&debug&DEBUG_RHIZOME)
WHYF("length bytes written at offset 0x%x",e->length);
DEBUGF("length bytes written at offset 0x%x",e->length);
}
if (frameFull) {
sqlite3_blob_close(blob); blob=NULL;
@ -246,12 +246,12 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
!=SQLITE_OK) {
if (!pass) {
if (0) {
WHY(" Manifest:");
DEBUG(" Manifest:");
int i;
for(i=0;i<blob_bytes;i++) WHYF(" %c",e->bytes[e->length+overhead+i]);
for(i=0;i<blob_bytes;i++) DEBUGF(" %c",e->bytes[e->length+overhead+i]);
}
}
if (debug&DEBUG_RHIZOME) WHY("Couldn't read from blob");
if (debug&DEBUG_RHIZOME) DEBUG("Couldn't read from blob");
sqlite3_blob_close(blob); blob=NULL;
dump("buffer (225)",(unsigned char *)e,sizeof(*e));
@ -285,7 +285,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
if (blob) sqlite3_blob_close(blob); blob=NULL;
if (statement) sqlite3_finalize(statement); statement=NULL;
if (0&&debug&DEBUG_RHIZOME) WHYF("Appended %d rhizome advertisements to packet using %d bytes.",bundles_advertised,bytes_used);
if (0&&debug&DEBUG_RHIZOME) DEBUGF("Appended %d rhizome advertisements to packet using %d bytes.",bundles_advertised,bytes_used);
int rfs_value=1+8+1+1+1+bytes_used;
if (rfs_value<0xfa)
ob_setbyte(e,rfs_offset,rfs_value);
@ -305,8 +305,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;
if (debug&DEBUG_RHIZOME) {
WHYF("rhizome f->bytecount=%d",f->payload->length);
if (debug&DEBUG_RHIZOMESYNC) {
DEBUGF("rhizome f->bytecount=%d",f->payload->length);
// dump("payload",f->payload->bytes,f->payload->length);
}
@ -352,7 +352,7 @@ int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now)
if (!i) {
/* ignore the announcement, but don't ignore other people
offering the same manifest */
WHY("Ignoring manifest announcment with no signature");
WARN("Ignoring manifest announcment with no signature");
rhizome_manifest_free(m);
return 0;
}
@ -360,7 +360,7 @@ int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now)
if (rhizome_ignore_manifest_check(m,(struct sockaddr_in *)f->recvaddr))
{
/* Ignoring manifest that has caused us problems recently */
if (0) WHYF("Ignoring manifest with errors: %s",
if (0) WARNF("Ignoring manifest with errors: %s",
rhizome_manifest_get(m,"id",NULL,0));
}
else if (m&&(!m->errors))
@ -369,15 +369,15 @@ int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now)
if (rhizome_manifest_version_cache_lookup(m)) {
/* We already have this version or newer */
if (debug&DEBUG_RHIZOMESYNC) {
WHYF("manifest id=%s, version=%lld",
DEBUGF("manifest id=%s, version=%lld",
rhizome_manifest_get(m,"id",NULL,0),
rhizome_manifest_get_ll(m,"version"));
WHY("We already have that manifest or newer.");
DEBUG("We already have that manifest or newer.");
}
importManifest=0;
} else {
if (debug&DEBUG_RHIZOMESYNC) {
WHYF("manifest id=%s, version=%lld is new to us.",
DEBUGF("manifest id=%s, version=%lld is new to us.",
rhizome_manifest_get(m,"id",NULL,0),
rhizome_manifest_get_ll(m,"version"));
}
@ -388,7 +388,7 @@ int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now)
}
else
{
if (debug&DEBUG_RHIZOME) WHY("Unverified manifest has errors - so not processing any further.");
if (debug&DEBUG_RHIZOME) DEBUG("Unverified manifest has errors - so not processing any further.");
/* Don't waste any time on this manifest in future attempts for at least
a minute. */
rhizome_queue_ignore_manifest(m,(struct sockaddr_in*)f->recvaddr,60000);
@ -403,15 +403,15 @@ int overlay_rhizome_saw_advertisements(int i,overlay_frame *f, long long now)
m=rhizome_read_manifest_file((char *)&f->payload->bytes[ofs],
manifest_length,RHIZOME_VERIFY);
if (m->errors) {
if (debug&DEBUG_RHIZOME) WHYF("Verifying manifest %s revealed errors -- not storing.", manifest_id);
if (debug&DEBUG_RHIZOME) DEBUGF("Verifying manifest %s revealed errors -- not storing.", manifest_id);
rhizome_queue_ignore_manifest(m,(struct sockaddr_in*)f->recvaddr,60000);
rhizome_manifest_free(m);
} else {
if (debug&DEBUG_RHIZOME) WHYF("Verifying manifest %s revealed no errors -- will try to store.", manifest_id);
if (debug&DEBUG_RHIZOME) DEBUGF("Verifying manifest %s revealed no errors -- will try to store.", manifest_id);
/* Add manifest to import queue. We need to know originating IPv4 address
so that we can transfer by HTTP. */
if (0) WHY("Suggesting fetching of a bundle");
if (0) DEBUG("Suggesting fetching of a bundle");
rhizome_suggest_queue_manifest_import(m,(struct sockaddr_in *)f->recvaddr);
}
}

View File

@ -388,7 +388,7 @@ extern struct mphlr_variable vars[];
extern int hexdigit[16];
/* Make sure we have space to put bytes of the packet as we go along */
#define CHECK_PACKET_LEN(B) {if (((*packet_len)+(B))>=packet_maxlen) { setReason("Packet composition ran out of space."); return -1; } }
#define CHECK_PACKET_LEN(B) {if (((*packet_len)+(B))>=packet_maxlen) { return WHY("Packet composition ran out of space."); } }
extern int sock;
@ -758,32 +758,33 @@ extern overlay_txqueue overlay_tx[OQ_MAX];
#define LOG_LEVEL_ERROR (3)
#define LOG_LEVEL_FATAL (4)
int setReason(char *fmt, ...);
void logMessage(int level, char *fmt, ...);
void vlogMessage(int level, char *fmt, va_list);
void logMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, ...);
void vlogMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, va_list);
long long debugFlagMask(const char *flagname);
char *catv(const char *data, char *buf, size_t len);
int dump(char *name,unsigned char *addr,int len);
const char *trimbuildpath(const char *s);
#define FATALF(F,...) do { logMessage(LOG_LEVEL_FATAL, "%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); exit(-1); } while(1)
#define LOGF(L,F,...) logMessage(L, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define FATALF(F,...) do { logMessage(LOG_LEVEL_FATAL, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__); exit(-1); } while (1)
#define FATAL(X) FATALF("%s", (X))
#define FATAL_perror(X) FATALF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define WHY(X) setReason("%s:%d:%s() %s",trimbuildpath(__FILE__),__LINE__,__FUNCTION__,X)
#define WHYNULL(X) (setReason("%s:%d:%s() %s",trimbuildpath(__FILE__),__LINE__,__FUNCTION__,X), NULL)
#define WHYF(F,...) setReason("%s:%d:%s() " F,trimbuildpath(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WHY_perror(X) setReason("%s:%d:%s() %s: %s [errno=%d]", trimbuildpath(__FILE__), __LINE__, __FUNCTION__, X, strerror(errno), errno)
#define WHYF(F,...) (LOGF(LOG_LEVEL_ERROR, F, ##__VA_ARGS__), -1)
#define WHY(X) WHYF("%s", (X))
#define WHYNULL(X) (LOGF(LOG_LEVEL_ERROR, "%s", X), NULL)
#define WHY_perror(X) WHYF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define WARNF(F,...) logMessage(LOG_LEVEL_WARN, "%s:%d:%s() " F, trimbuildpath(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WARNF(F,...) logMessage(LOG_LEVEL_WARN, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define WARN(X) WARNF("%s", (X))
#define WARN_perror(X) WARNF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define INFOF(F,...) logMessage(LOG_LEVEL_INFO, "%s:%d:%s() " F,trimbuildpath( __FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define INFOF(F,...) logMessage(LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define INFO(X) INFOF("%s", (X))
#define DEBUGF(F,...) logMessage(LOG_LEVEL_DEBUG, "%s:%d:%s() " F, trimbuildpath(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define DEBUGF(F,...) logMessage(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define DEBUG(X) DEBUGF("%s", (X))
#define DEBUG_perror(X) DEBUGF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define D DEBUG("D")

View File

@ -125,14 +125,15 @@ int server_pid()
{
const char *instancepath = serval_instancepath();
struct stat st;
if (stat(instancepath, &st) == -1)
return setReason(
"Instance path '%s' non existant or not accessable: %s [errno=%d]"
if (stat(instancepath, &st) == -1) {
WHY_perror("stat");
return WHYF("Instance path '%s' non existant or not accessable"
" (Set SERVALINSTANCE_PATH to specify an alternate location)",
instancepath, strerror(errno), errno
instancepath
);
}
if ((st.st_mode & S_IFMT) != S_IFDIR)
return setReason("Instance path '%s' is not a directory", instancepath);
return WHYF("Instance path '%s' is not a directory", instancepath);
char filename[1024];
if (!FORM_SERVAL_INSTANCE_PATH(filename, PIDFILE_NAME))
return -1;
@ -526,7 +527,7 @@ int processRequest(unsigned char *packet,int len,
}
break;
case ACTION_SET:
setReason("You can only set keyring variables locally");
WHY("You can only set keyring variables locally");
return respondSimple(NULL,ACTION_ERROR,
(unsigned char *)"Would be insecure",
0,transaction_id,recvttl,
@ -625,7 +626,7 @@ int processRequest(unsigned char *packet,int len,
dlen=0;
if (packageVariableSegment(data,&dlen,&r,offset,
MAX_DATA_BYTES+16))
return setReason("packageVariableSegment() failed.");
return WHY("packageVariableSegment() failed.");
responding_id = keyring->contexts[cn]->identities[in];
/* Remember that we need to send this new packet */
@ -690,7 +691,7 @@ int processRequest(unsigned char *packet,int len,
fake.response=uri;
if (packageVariableSegment(data,&dlen,&fake,offset,MAX_DATA_BYTES+16))
return setReason("packageVariableSegment() of gateway URI failed.");
return WHY("packageVariableSegment() of gateway URI failed.");
WHY("Gateway claims to be 1st identity, when it should probably have its own identity");
respondSimple(keyring->contexts[0]->identities[0],
@ -707,7 +708,7 @@ int processRequest(unsigned char *packet,int len,
}
break;
default:
setReason("Asked to perform unsupported action");
WHY("Asked to perform unsupported action");
if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"Asked to perform unsipported action at Packet offset = 0x%x\n",pofs);
if (debug&DEBUG_PACKETFORMATS) dump("Packet",packet,len);
return WHY("Asked to perform unsupported action.");
@ -751,10 +752,10 @@ int respondSimple(keyring_identity *id,
return WHY("packetMakeHeader() failed.");
if (id)
{ if (packetSetSidFromId(packet,8000,packet_len,id))
return setReason("invalid SID in reply"); }
return WHY("invalid SID in reply"); }
else
{ if (packetSetDid(packet,8000,packet_len,""))
return setReason("Could not set empty DID in reply"); }
return WHY("Could not set empty DID in reply"); }
CHECK_PACKET_LEN(1+1+action_len);
packet[(*packet_len)++]=action;
@ -863,14 +864,14 @@ int simpleServerMode()
if (debug&DEBUG_PACKETRX) dump("recvaddr",(unsigned char *)&recvaddr,recvaddrlen);
if (debug&DEBUG_PACKETRX) dump("packet",(unsigned char *)buffer,len);
if (dropPacketP(len)) {
if (debug&DEBUG_SIMULATION) fprintf(stderr,"Simulation mode: Dropped packet due to simulated link parameters.\n");
if (debug&DEBUG_SIMULATION) DEBUG("Simulation mode: Dropped packet due to simulated link parameters.");
continue;
}
/* Simple server mode doesn't really use interface numbers, so lie and say interface -1 */
if (packetOk(-1,buffer,len,NULL,ttl,&recvaddr,recvaddrlen,1)) {
if (debug&DEBUG_PACKETFORMATS) setReason("Ignoring invalid packet");
if (debug&DEBUG_PACKETFORMATS) DEBUG("Ignoring invalid packet");
}
if (debug&DEBUG_PACKETRX) fprintf(stderr,"Finished processing packet, waiting for next one.\n");
if (debug&DEBUG_PACKETRX) DEBUG("Finished processing packet, waiting for next one.");
}
}
return 0;