Rewrite many fprintf(stderr,...) to DEBUGF(...)

This commit is contained in:
Andrew Bettison 2012-07-27 11:29:27 +09:30
parent 62d851f97c
commit bb5d658779
3 changed files with 60 additions and 51 deletions

View File

@ -48,25 +48,25 @@ int readRoutingTable(struct in_addr peers[],int *peer_count,int peer_max){
unsigned long d, g, m; unsigned long d, g, m;
int flgs, ref, use, metric, mtu, win, ir; int flgs, ref, use, metric, mtu, win, ir;
if (debug&DEBUG_PEERS) fprintf(stderr,"Reading routing table\n"); if (debug&DEBUG_PEERS) DEBUG("Reading routing table");
FILE *fp = fopen("/proc/net/route","r"); FILE *fp = fopen("/proc/net/route","r");
if (!fp) return -1; if (!fp) return -1;
if (debug&DEBUG_PEERS) fprintf(stderr,"Skipping line\n"); if (debug&DEBUG_PEERS) DEBUG("Skipping line");
if (fscanf(fp, "%*[^\n]\n") < 0) if (fscanf(fp, "%*[^\n]\n") < 0)
goto ERROR; goto ERROR;
while(1){ while(1){
int r; int r;
if (debug&DEBUG_PEERS) fprintf(stderr,"Reading next route\n"); if (debug&DEBUG_PEERS) DEBUG("Reading next route");
r = fscanf(fp, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n", r = fscanf(fp, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n",
devname, &d, &g, &flgs, &ref, &use, &metric, &m, devname, &d, &g, &flgs, &ref, &use, &metric, &m,
&mtu, &win, &ir); &mtu, &win, &ir);
if (r != 11) { if (r != 11) {
if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */ if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */
if (debug&DEBUG_PEERS) fprintf(stderr,"eof\n"); if (debug&DEBUG_PEERS) DEBUG("eof");
break; break;
} }
ERROR: ERROR:
@ -75,18 +75,18 @@ int readRoutingTable(struct in_addr peers[],int *peer_count,int peer_max){
} }
if (!(flgs & RTF_UP)) { /* Skip interfaces that are down. */ if (!(flgs & RTF_UP)) { /* Skip interfaces that are down. */
if (debug&DEBUG_PEERS) fprintf(stderr,"Skipping down interface %s\n",devname); if (debug&DEBUG_PEERS) DEBUGF("Skipping down interface %s",devname);
continue; continue;
} }
if (m!=0xFFFFFFFF){ if (m!=0xFFFFFFFF){
/* Netmask indicates a network, so calculate broadcast address */ /* Netmask indicates a network, so calculate broadcast address */
d=(d&m)|(0xffffffff^m); d=(d&m)|(0xffffffff^m);
if (debug&DEBUG_PEERS) fprintf(stderr,"Adding broadcast address %08lx\n",d); if (debug&DEBUG_PEERS) DEBUGF("Adding broadcast address %08lx",d);
} }
if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=d; if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=d;
if (debug&DEBUG_PEERS) fprintf(stderr,"Found peer %08lx from routing table\n",d); if (debug&DEBUG_PEERS) DEBUGF("Found peer %08lx from routing table",d);
} }
fclose(fp); fclose(fp);
return 0; return 0;
@ -96,12 +96,12 @@ int readArpTable(struct in_addr peers[],int *peer_count,int peer_max){
unsigned long d; unsigned long d;
int q1,q2,q3,q4; int q1,q2,q3,q4;
if (debug&DEBUG_PEERS) fprintf(stderr,"Reading ARP table\n"); if (debug&DEBUG_PEERS) DEBUG("Reading ARP table");
FILE *fp = fopen("/proc/net/arp","r"); FILE *fp = fopen("/proc/net/arp","r");
if (!fp) return -1; if (!fp) return -1;
if (debug&DEBUG_PEERS) fprintf(stderr,"Skipping line\n"); if (debug&DEBUG_PEERS) DEBUG("Skipping line");
if (fscanf(fp, "%*[^\n]\n") < 0) if (fscanf(fp, "%*[^\n]\n") < 0)
goto ERROR; goto ERROR;
@ -109,7 +109,7 @@ int readArpTable(struct in_addr peers[],int *peer_count,int peer_max){
int r; int r;
r = fscanf(fp, "%d.%d.%d.%d%*[^\n]\n", r = fscanf(fp, "%d.%d.%d.%d%*[^\n]\n",
&q1,&q2,&q3,&q4); &q1,&q2,&q3,&q4);
if (debug&DEBUG_PEERS) fprintf(stderr,"Reading next arp entry (r=%d, %d.%d.%d.%d)\n",r,q1,q2,q3,q4); if (debug&DEBUG_PEERS) DEBUGF("Reading next arp entry (r=%d, %d.%d.%d.%d)",r,q1,q2,q3,q4);
d = (q1&0xff) d = (q1&0xff)
+((q2&0xff)<<8) +((q2&0xff)<<8)
@ -118,7 +118,7 @@ int readArpTable(struct in_addr peers[],int *peer_count,int peer_max){
if (r != 4) { if (r != 4) {
if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */ if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */
if (debug&DEBUG_PEERS) fprintf(stderr,"eof\n"); if (debug&DEBUG_PEERS) DEBUG("eof");
break; break;
} }
ERROR: ERROR:
@ -127,7 +127,7 @@ int readArpTable(struct in_addr peers[],int *peer_count,int peer_max){
} }
if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=d; if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=d;
if (debug&DEBUG_PEERS) fprintf(stderr,"Found peer %08lx from ARP table\n",d); if (debug&DEBUG_PEERS) DEBUGF("Found peer %08lx from ARP table",d);
} }
fclose(fp); fclose(fp);
return 0; return 0;
@ -143,41 +143,48 @@ int readBatmanPeerFile(char *file_path,struct in_addr peers[],int *peer_count,in
f=fopen(file_path,"r"); f=fopen(file_path,"r");
if (!f) { if (!f) {
fprintf(stderr,"Failed to open peer list file `%s'\n",file_path); WHY_perror("fopen");
return -1; return WHYF("Failed to open peer list file `%s'",file_path);
} }
if (fread(&offset,sizeof(offset),1,f)!=1) { if (fread(&offset,sizeof(offset),1,f)!=1) {
fprintf(stderr,"Failed to read peer list offset from `%s'\n",file_path); WHY_perror("fread");
fclose(f); return -1; } fclose(f);
return WHYF("Failed to read peer list offset from `%s'",file_path);
}
offset=ntohl(offset); offset=ntohl(offset);
if (fseek(f,offset,SEEK_SET)) { if (fseek(f,offset,SEEK_SET)) {
fprintf(stderr,"Failed to seek to peer list offset 0x%x in `%s'\n",offset,file_path); WHY_perror("fseek");
fclose(f); return -1; } fclose(f);
return WHYF("Failed to seek to peer list offset 0x%x in `%s'",offset,file_path);
}
if (fread(&timestamp,sizeof(timestamp),1,f)!=1) { if (fread(&timestamp,sizeof(timestamp),1,f)!=1) {
fprintf(stderr,"Failed to read peer list timestamp from `%s'\n",file_path); WHY_perror("fread");
fclose(f); return -1; } fclose(f);
return WHYF("Failed to read peer list timestamp from `%s'",file_path);
}
timestamp=ntohl(timestamp); timestamp=ntohl(timestamp);
if (timestamp<(time(0)-3)) { if (timestamp<(time(0)-3)) {
if (debug&DEBUG_PEERS) fprintf(stderr,"Ignoring stale BATMAN peer list (%d seconds old)\n",(int)(time(0)-timestamp)); if (debug&DEBUG_PEERS)
DEBUGF("Ignoring stale BATMAN peer list (%d seconds old)",(int)(time(0)-timestamp));
fclose(f); fclose(f);
return -1; return -1;
} }
while(fread(&p,sizeof(p),1,f)==1) while(fread(&p,sizeof(p),1,f)==1) {
{ struct in_addr i;
struct in_addr i; if (!p.addr_len) break;
if (!p.addr_len) break; union { char c[4]; uint32_t ui32; } *u = (void*)&p.addr[0];
union { char c[4]; uint32_t ui32; } *u = (void*)&p.addr[0]; i.s_addr = u->ui32;
i.s_addr = u->ui32; if (*peer_count<peer_max) peers[(*peer_count)++]=i;
if (*peer_count<peer_max) peers[(*peer_count)++]=i; if (debug&DEBUG_PEERS) DEBUGF("Found BATMAN peer '%s'",inet_ntoa(i));
if (debug&DEBUG_PEERS) fprintf(stderr,"Found BATMAN peer '%s'\n",inet_ntoa(i)); }
}
fclose(f); if (fclose(f) == EOF)
WHY_perror("fclose");
return 0; return 0;
} }
@ -192,7 +199,6 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
int ofs=0; int ofs=0;
int bytes=0; int bytes=0;
struct pollfd fds; struct pollfd fds;
char cmd[30];
int notDone=1; int notDone=1;
int res; int res;
int tries=0; int tries=0;
@ -210,10 +216,13 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
if (connect(sock,(struct sockaddr*)&socket_address,sizeof(socket_address))<0) if (connect(sock,(struct sockaddr*)&socket_address,sizeof(socket_address))<0)
return WHY("connect() to BATMAN socket failed."); return WHY("connect() to BATMAN socket failed.");
memset(&cmd[0],0,30); char cmd[30];
snprintf(cmd,30,"d:%c",1); snprintf(cmd, sizeof cmd, "d:%c", 1);
if (write(sock,cmd,30)!=30) cmd[sizeof cmd - 1] = '\0';
{ close(sock); return WHY("write() command failed to BATMAN socket."); } if (write(sock,cmd,30) != 30) {
close(sock);
return WHY("write() command failed to BATMAN socket.");
}
fds.fd=sock; fds.fd=sock;
fds.events=POLLIN; fds.events=POLLIN;
@ -224,7 +233,7 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
{ {
switch (poll(&fds,1,1500)) { switch (poll(&fds,1,1500)) {
case 1: /* Excellent - we have a response */ break; case 1: /* Excellent - we have a response */ break;
case 0: if (debug&DEBUG_PEERS) fprintf(stderr,"BATMAN did not respond to peer enquiry.\n"); case 0: if (debug&DEBUG_PEERS) DEBUGF("BATMAN did not respond to peer enquiry");
close(sock); close(sock);
if (tries++<=3) goto askagain; if (tries++<=3) goto askagain;
return WHY("No response from BATMAN."); return WHY("No response from BATMAN.");
@ -241,7 +250,7 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
/* Got a partial response, then a dead line. /* Got a partial response, then a dead line.
Should probably ask again unless we have tried too many times. Should probably ask again unless we have tried too many times.
*/ */
if (debug&DEBUG_PEERS) fprintf(stderr,"Trying again after cold drop.\n"); if (debug&DEBUG_PEERS) DEBUGF("Trying again after cold drop");
close(sock); close(sock);
bytes=0; bytes=0;
if (tries++<=3) goto askagain; if (tries++<=3) goto askagain;
@ -250,7 +259,7 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
return WHY("failed to read() from BATMAN socket."); return WHY("failed to read() from BATMAN socket.");
} }
if (!res) return 0; if (!res) return 0;
if (debug&DEBUG_PEERS) fprintf(stderr,"BATMAN has responded with %d bytes.\n",res); if (debug&DEBUG_PEERS) DEBUGF("BATMAN has responded with %d bytes",res);
if (debug&DEBUG_PEERS) dump("BATMAN says",&buf[bytes],res); if (debug&DEBUG_PEERS) dump("BATMAN says",&buf[bytes],res);
@ -267,7 +276,7 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
*/ */
if (buf[bytes+res-4]!='E') { if (buf[bytes+res-4]!='E') {
/* no end marker, so try adding record to the end. */ /* no end marker, so try adding record to the end. */
if (debug&DEBUG_PEERS) fprintf(stderr,"Data has no end marker, accumulating.\n"); if (debug&DEBUG_PEERS) DEBUGF("Data has no end marker, accumulating");
bytes+=res; bytes+=res;
goto getmore; goto getmore;
} }
@ -278,7 +287,7 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
while(ofs<bytes) while(ofs<bytes)
{ {
if(debug&DEBUG_PEERS) fprintf(stderr,"New line @ %d\n",ofs); if(debug&DEBUG_PEERS) DEBUGF("New line @ %d",ofs);
/* Check for IP address of peers */ /* Check for IP address of peers */
if (isdigit(buf[ofs])) if (isdigit(buf[ofs]))
{ {
@ -287,7 +296,7 @@ int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,i
if (buf[i+ofs]==' ') { if (buf[i+ofs]==' ') {
buf[i+ofs]=0; buf[i+ofs]=0;
if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=inet_addr((char *)&buf[ofs]); if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=inet_addr((char *)&buf[ofs]);
if (debug&DEBUG_PEERS) fprintf(stderr,"Found BATMAN peer '%s'\n",&buf[ofs]); if (debug&DEBUG_PEERS) DEBUGF("Found BATMAN peer '%s'",&buf[ofs]);
buf[ofs+i]=' '; buf[ofs+i]=' ';
break; break;
} }

View File

@ -294,7 +294,7 @@ int packetGetID(unsigned char *packet,int len,char *did,char *sid)
case 0: /* DID */ case 0: /* DID */
ofs++; ofs++;
if (extractDid(packet,&ofs,did)) return WHY("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); if (debug&DEBUG_PACKETFORMATS) DEBUGF("Decoded DID as %s", did);
return 0; return 0;
break; break;
case 1: /* SID */ case 1: /* SID */
@ -333,8 +333,8 @@ int isFieldZeroP(unsigned char *packet,int start,int count)
} }
if (debug&DEBUG_PACKETFORMATS) { if (debug&DEBUG_PACKETFORMATS) {
if (mod) fprintf(stderr,"Field [%d,%d) is non-zero (mod=0x%02x)\n",start,start+count,mod); if (mod) DEBUGF("Field [%d,%d) is non-zero (mod=0x%02x)",start,start+count,mod);
else fprintf(stderr,"Field [%d,%d) is zero\n",start,start+count); else DEBUGF("Field [%d,%d) is zero",start,start+count);
} }
if (mod) return 0; else return 1; if (mod) return 0; else return 1;
@ -346,8 +346,7 @@ int safeZeroField(unsigned char *packet,int start,int count)
int i; int i;
if (debug&DEBUG_PACKETFORMATS) if (debug&DEBUG_PACKETFORMATS)
fprintf(stderr,"Known plain-text counter-measure: safe-zeroing [%d,%d)\n", DEBUGF("Known plain-text counter-measure: safe-zeroing [%d,%d)", start,start+count);
start,start+count);
for(i=start;i<(start+count-1);i++) for(i=start;i<(start+count-1);i++)
{ {

View File

@ -30,7 +30,8 @@ int process_packet(unsigned char *packet, size_t len,
did[0]=0; sid[0]=0; did[0]=0; sid[0]=0;
/* Get DID or SID */ /* Get DID or SID */
if (packetGetID(packet,len,did,sid)) return WHY("Could not parse DID or SID"); if (packetGetID(packet,len,did,sid) == -1)
return WHY("Could not parse DID or SID");
/* Check for PIN */ /* Check for PIN */
if (!isFieldZeroP(packet,OFS_PINFIELD,16)) if (!isFieldZeroP(packet,OFS_PINFIELD,16))
@ -38,7 +39,7 @@ int process_packet(unsigned char *packet, size_t len,
/* Authentication has been attempted. /* Authentication has been attempted.
If it is incorrect, then we need to return with ACTION_DECLINED If it is incorrect, then we need to return with ACTION_DECLINED
*/ */
if (debug&DEBUG_SECURITY) fprintf(stderr,"A PIN has been supplied.\n"); if (debug&DEBUG_SECURITY) DEBUG("A PIN has been supplied");
/* Can only authenticate by SID, not DID (since DIDs are ambiguous) */ /* Can only authenticate by SID, not DID (since DIDs are ambiguous) */
if (packet[OFS_SIDDIDFIELD]!=1) return WHY("You can only authenticate against a SID"); if (packet[OFS_SIDDIDFIELD]!=1) return WHY("You can only authenticate against a SID");
@ -50,7 +51,7 @@ int process_packet(unsigned char *packet, size_t len,
{ {
/* No attempt at authentication was made */ /* No attempt at authentication was made */
//authenticatedP=0; //authenticatedP=0;
if (debug&DEBUG_SECURITY) fprintf(stderr,"No PIN was supplied.\n"); if (debug&DEBUG_SECURITY) DEBUG("No PIN was supplied");
} }
if (serverMode) return processRequest(packet,len,sender,sender_len,transaction_id, if (serverMode) return processRequest(packet,len,sender,sender_len,transaction_id,