diff --git a/commandline.c b/commandline.c index f687f7ed..aec1e425 100644 --- a/commandline.c +++ b/commandline.c @@ -38,16 +38,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "strbuf.h" int cli_usage() { - fprintf(stderr,"\nServal Mesh version .\n"); - fprintf(stderr,"Usage:\n"); + printf("Serval Mesh version .\n"); + printf("Usage:\n"); int i,j; - for(i=0;command_line_options[i].function;i++) - { - for(j=0;command_line_options[i].words[j];j++) - fprintf(stderr," %s",command_line_options[i].words[j]); - fprintf(stderr,"\n %s\n",command_line_options[i].description); - } - return -1; + for(i=0;command_line_options[i].function;i++) { + for(j=0;command_line_options[i].words[j];j++) + printf(" %s",command_line_options[i].words[j]); + printf("\n %s\n",command_line_options[i].description); + } + return 0; } /* Remember the name by which this program was invoked. @@ -183,24 +182,17 @@ JNIEXPORT jint JNICALL Java_org_servalproject_servald_ServalD_rawCommand(JNIEnv #endif /* HAVE_JNI_H */ -static void complainCommandLine(const char *prefix, int argc, const char *const *argv) -{ - WHY_argv(prefix, argc, argv); -} - /* The argc and argv arguments must be passed verbatim from main(argc, argv), so argv[0] is path to executable. */ int parseCommandLine(const char *argv0, int argc, const char *const *args) { - int i; int ambiguous=0; int cli_call=-1; - fd_clearstats(); - IN(); exec_argv0 = argv0; + int i; for(i=0;command_line_options[i].function;i++) { int j; @@ -242,13 +234,12 @@ int parseCommandLine(const char *argv0, int argc, const char *const *args) if (cli_call>=0) ambiguous++; if (ambiguous==1) { WHY("Ambiguous command line call:"); - complainCommandLine(" ", argc, args); + WHY_argv(" ", argc, args); WHY("Matches the following known command line calls:"); - complainCommandLine(" ", argc, command_line_options[cli_call].words); - } - if (ambiguous) { - complainCommandLine(" ", argc, command_line_options[i].words); + WHY_argv(" ", argc, command_line_options[cli_call].words); } + if (ambiguous) + WHY_argv(" ", argc, command_line_options[i].words); cli_call=i; } } @@ -257,9 +248,12 @@ int parseCommandLine(const char *argv0, int argc, const char *const *args) if (ambiguous) return -1; /* Complain if we found no matching calls */ if (cli_call<0) { - WHY("Unknown command line call:"); - complainCommandLine(" ", argc, args); - return cli_usage(); + if (argc) { + WHY("Unknown command line call:"); + WHY_argv(" ", argc, args); + } + INFO("Use \"help\" command to see a list of valid commands"); + return -1; } /* Otherwise, make call */ @@ -897,10 +891,10 @@ int app_mdp_ping(int argc, const char *const *argv, struct command_line_option * rx_stddev=sqrtf(rx_stddev); /* XXX Report final statistics before going */ - fprintf(stderr,"--- %s ping statistics ---\n", alloca_tohex_sid(ping_sid)); - fprintf(stderr,"%lld packets transmitted, %lld packets received, %3.1f%% packet loss\n", + printf("--- %s ping statistics ---\n", alloca_tohex_sid(ping_sid)); + printf("%lld packets transmitted, %lld packets received, %3.1f%% packet loss\n", tx_count,rx_count,tx_count?(tx_count-rx_count)*100.0/tx_count:0); - fprintf(stderr,"round-trip min/avg/max/stddev%s = %lld/%.3f/%lld/%.3f ms\n", + printf("round-trip min/avg/max/stddev%s = %lld/%.3f/%lld/%.3f ms\n", (samplesname); if (packetOk(interface,packet,plen,NULL,recvttl,&src_addr,addrlen,1)) { WHY("Malformed packet"); // Do we really want to attempt to parse it again? - //serval_packetvisualise(open_logging(), "Malformed packet", packet,plen); + //DEBUG_packet_visualise("Malformed packet", packet,plen); } } @@ -437,7 +437,7 @@ void overlay_dummy_poll(struct sched_ent *alarm) if (plen > nread - 128) plen = -1; if (debug&DEBUG_PACKETRX) - serval_packetvisualise(open_logging(), "Read from dummy interface", &packet[128], plen); + DEBUG_packet_visualise("Read from dummy interface", &packet[128], plen); bzero(&transaction_id[0],8); bzero(&src_addr,sizeof(src_addr)); if (plen >= 4) { @@ -475,7 +475,7 @@ int overlay_broadcast_ensemble(int interface_number, if (debug&DEBUG_PACKETTX) { DEBUGF("Sending this packet via interface #%d",interface_number); - serval_packetvisualise(open_logging(),NULL,bytes,len); + DEBUG_packet_visualise(NULL,bytes,len); } overlay_interface *interface = &overlay_interfaces[interface_number]; @@ -1029,3 +1029,20 @@ long long parse_quantity(char *q) return WHY("Could not parse quantity"); } } + +void logServalPacket(int level, const char *file, unsigned int line, const char *function, const char *message, const unsigned char *packet, size_t len) +{ + char *buffer; + size_t size; + FILE *m = open_memstream(&buffer, &size); + if (m == NULL) { + WHY_perror("open_memstream"); + return; + } + if (serval_packetvisualise(m, message, packet, len) == -1) + WHY("serval_packetvisualise() failed"); + if (fclose(m) == EOF) + WHY_perror("fclose"); + else + logString(level, file, line, function, buffer); +} diff --git a/overlay_payload.c b/overlay_payload.c index 57bc3583..f14be10f 100644 --- a/overlay_payload.c +++ b/overlay_payload.c @@ -34,20 +34,20 @@ int op_append_type(overlay_buffer *headers,overlay_frame *p) { case OF_TYPE_FLAG_NORMAL: c[0]=p->type|p->modifiers; - if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"type resolves to %02x\n",c[0]); + if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x",c[0]); if (ob_append_bytes(headers,c,1)) return -1; break; case OF_TYPE_FLAG_E12: c[0]=(p->type&OF_MODIFIER_BITS)|OF_TYPE_EXTENDED12; c[1]=(p->type>>4)&0xff; - if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"type resolves to %02x%02x\n",c[0],c[1]); + if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x%02x",c[0],c[1]); if (ob_append_bytes(headers,c,2)) return -1; break; case OF_TYPE_FLAG_E20: c[0]=(p->type&OF_MODIFIER_BITS)|OF_TYPE_EXTENDED20; c[1]=(p->type>>4)&0xff; c[2]=(p->type>>12)&0xff; - if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"type resolves to %02x%02x%02x\n",c[0],c[1],c[2]); + if (debug&DEBUG_PACKETFORMATS) DEBUGF("type resolves to %02x%02x%02x",c[0],c[1],c[2]); if (ob_append_bytes(headers,c,3)) return -1; break; default: @@ -194,21 +194,16 @@ overlay_buffer *overlay_payload_unpackage(overlay_frame *b) { int dump_queue(char *msg,int q) { overlay_txqueue *qq=&overlay_tx[q]; - printf("Contents of TX queue #%d (%s):\n",q,msg); - printf(" length=%d, maxLength=%d\n",qq->length,qq->maxLength); + DEBUGF("Contents of TX queue #%d (%s):",q,msg); + DEBUGF(" length=%d, maxLength=%d",qq->length,qq->maxLength); struct overlay_frame *f=qq->first,*l=qq->last; - - printf(" head of queue = %p, tail of queue = %p\n", - f,l); - + DEBUGF(" head of queue = %p, tail of queue = %p", f, l); struct overlay_frame *n=f; int count=0; - while(n) { - printf(" queue entry #%d : prev=%p, next=%p\n", - count,n->prev,n->next); - if (n==n->next) { - printf(" ERROR: loop in queue\n"); + DEBUGF(" queue entry #%d : prev=%p, next=%p", count,n->prev,n->next); + if (n==n->next) { + WHY(" ERROR: loop in queue"); return -1; } n=n->next; @@ -218,18 +213,13 @@ int dump_queue(char *msg,int q) int dump_payload(overlay_frame *p,char *message) { - fflush(stdout); - fprintf(stderr, - "+++++\nFrame from %s to %s of type 0x%02x %s:\n", + DEBUGF( "+++++\nFrame from %s to %s of type 0x%02x %s:", alloca_tohex_sid(p->source), alloca_tohex_sid(p->destination),p->type, message?message:""); - fprintf(stderr," next hop is %s\n",alloca_tohex_sid(p->nexthop)); - fflush(stderr); - if (p->payload) dump("payload contents", - &p->payload->bytes[0],p->payload->length); - fflush(stdout); fflush(stderr); - fprintf(stderr,"-----\n"); + DEBUGF(" next hop is %s",alloca_tohex_sid(p->nexthop)); + if (p->payload) + dump("payload contents", &p->payload->bytes[0],p->payload->length); return 0; } diff --git a/packetformats.c b/packetformats.c index 4252c802..0fe8d3be 100644 --- a/packetformats.c +++ b/packetformats.c @@ -126,8 +126,10 @@ int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id, bcopy(&temp[0],&packet[HEADERFIELDS_LEN],payloadRotation); } - if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"Packet passes sanity checks and is ready for decoding.\n"); - if (debug&DEBUG_PACKETFORMATS) dump("unrotated packet",packet,len); + if (debug&DEBUG_PACKETFORMATS) { + DEBUG("Packet passes sanity checks and is ready for decoding"); + dump("unrotated packet",packet,len); + } if (parseP) return process_packet(packet,len,recvttl,recvaddr,recvaddrlen); else return 0; } @@ -253,10 +255,11 @@ int packetFinalise(unsigned char *packet,int packet_maxlen,int recvttl, payloadRotation=(*packet_len)-HEADERFIELDS_LEN; if (payloadRotation>0xff) payloadRotation=0xff; payloadRotation=random()%payloadRotation; - if (debug&DEBUG_SECURITY) - fprintf(stderr,"Known Plaintext counter-measure: rotating packet payload by 0x%02x bytes.\n", + if (debug&DEBUG_SECURITY) { + DEBUGF("Known Plaintext counter-measure: rotating packet payload by 0x%02x bytes", payloadRotation); - if (debug&DEBUG_SECURITY) dump("unrotated packet",packet,*packet_len); + dump("unrotated packet",packet,*packet_len); + } /* Now rotate the payload */ { @@ -297,11 +300,11 @@ int extractRequest(unsigned char *packet,int *packet_ofs,int packet_len, *bytes|=packet[(*packet_ofs)++]; *flags=packet[(*packet_ofs)++]; - if (debug&DEBUG_PACKETFORMATS) printf("Write flags = 0x%02x\n",*flags); + if (debug&DEBUG_PACKETFORMATS) DEBUGF("Write flags = 0x%02x",*flags); 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); + if (debug&DEBUG_PACKETFORMATS) DEBUGF("Packet offset is %d, length is %d, and asked for %d bytes",*packet_ofs,packet_len,*bytes); return WHY("mal-formed request packet (too short for claimed data)"); } @@ -332,7 +335,7 @@ int extractResponses(struct in_addr sender,unsigned char *buffer,int len,struct switch(buffer[ofs]) { case ACTION_EOT: - if (debug&DEBUG_DNARESPONSES) fprintf(stderr,"Reached response packet EOT.\n"); + if (debug&DEBUG_DNARESPONSES) DEBUGF("Reached response packet EOT"); case ACTION_DECLINED: case ACTION_OKAY: case ACTION_CREATEHLR: r->response_len=0; break; @@ -378,7 +381,7 @@ int extractResponses(struct in_addr sender,unsigned char *buffer,int len,struct case ACTION_XFER: default: free(r); - if (debug&(DEBUG_DNARESPONSES|DEBUG_PACKETFORMATS)) fprintf(stderr,"Encountered unimplemented response code 0x%02x @ 0x%x\n",buffer[ofs],ofs); + if (debug&(DEBUG_DNARESPONSES|DEBUG_PACKETFORMATS)) DEBUGF("Encountered unimplemented response code 0x%02x @ 0x%x",buffer[ofs],ofs); fixResponses(responses); return WHY("Encountered unimplemented response type"); } @@ -403,7 +406,7 @@ int extractResponses(struct in_addr sender,unsigned char *buffer,int len,struct if (r->peer_id>peer_count) r->peer_id=-1; /* Link new response into chain */ - if (debug&DEBUG_DNARESPONSES) printf("Linking response into response set.\n"); + if (debug&DEBUG_DNARESPONSES) DEBUGF("Linking response into response set"); r->prev=responses->last_response; if (responses->last_response) responses->last_response->next=r; @@ -436,7 +439,7 @@ int packageVariableSegment(unsigned char *data,int *dlen, bytes=buffer_size-(*dlen)-8; if ((h->value_len-offset)value_len-offset; if (bytes<0) bytes=0; - if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"Packaging %d bytes of variable\n",bytes); + if (debug&DEBUG_PACKETFORMATS) DEBUGF("Packaging %d bytes of variable",bytes); /* Describe variable */ @@ -455,7 +458,7 @@ int packageVariableSegment(unsigned char *data,int *dlen, /* Number of bytes in this segment */ data[(*dlen)++]=(bytes>>8)&0xff; data[(*dlen)++]=bytes&0xff; - if (debug&DEBUG_PACKETFORMATS) fprintf(stderr,"Packaging %d bytes\n",bytes); + if (debug&DEBUG_PACKETFORMATS) DEBUGF("Packaging %d bytes",bytes); /* Package the variable value itself (or part thereof) */ bcopy(&h->response[offset],&data[*dlen],bytes); diff --git a/peers.c b/peers.c index 2fbadcfe..7984f9b0 100644 --- a/peers.c +++ b/peers.c @@ -60,7 +60,7 @@ int getBroadcastAddresses(struct in_addr peers[],int *peer_count,int peer_max){ size_t bytesRead; struct nlmsghdr *hdr; - if (debug&DEBUG_PEERS) fprintf(stderr,"Reading broadcast addresses (linux style)\n"); + if (debug&DEBUG_PEERS) DEBUG("Reading broadcast addresses (linux style)"); netsock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); @@ -110,7 +110,7 @@ int getBroadcastAddresses(struct in_addr peers[],int *peer_count,int peer_max){ struct ifaddrs *ifaddr,*ifa; int family; - if (debug&DEBUG_PEERS) fprintf(stderr,"Reading broadcast addresses (posix style)\n"); + if (debug&DEBUG_PEERS) DEBUG("Reading broadcast addresses (posix style)"); if (getifaddrs(&ifaddr) == -1) { return WHY_perror("getifaddr"); @@ -134,7 +134,7 @@ int getBroadcastAddresses(struct in_addr peers[],int *peer_count,int peer_max){ } } #else - if (debug&DEBUG_PEERS) fprintf(stderr,"Don't know how to read broadcast addresses :(\n"); + if (debug&DEBUG_PEERS) DEBUG("Don't know how to read broadcast addresses :("); #endif #endif return 0; @@ -200,27 +200,27 @@ int sendToPeers(unsigned char *packet,int packet_len,int method,int peerId,struc { peer_addr.sin_addr=peers[i]; - if (debug&(DEBUG_PACKETTX|DEBUG_PEERS)) fprintf(stderr,"Sending packet to peer #%d\n",i); + if (debug&(DEBUG_PACKETTX|DEBUG_PEERS)) DEBUGF("Sending packet to peer #%d",i); ret=sendto(sock,packet,packet_len,0,(struct sockaddr *)&peer_addr,sizeof(peer_addr)); if (retmanifestdata[(*ofs)+1+32],&sigBuf[96],32); /* Get public key of signatory */ bcopy(&m->manifestdata[(*ofs)+1+64],&publicKey[0],crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES); - unsigned long long mlen=0; - int r=crypto_sign_edwards25519sha512batch_open(verifyBuf,&mlen,&sigBuf[0],128, - publicKey); - fflush(stdout); fflush(stderr); + int r=crypto_sign_edwards25519sha512batch_open(verifyBuf,&mlen,&sigBuf[0],128, publicKey); #endif if (r) { (*ofs)+=len; diff --git a/serval.h b/serval.h index 6a489c3c..ee6b0188 100644 --- a/serval.h +++ b/serval.h @@ -660,6 +660,10 @@ int overlay_interface_init_socket(int i); long long overlay_time_until_next_tick(); int overlay_rx_messages(); +void logServalPacket(int level, const char *file, unsigned int line, const char *function, const char *message, const unsigned char *packet, size_t len); + +#define DEBUG_packet_visualise(M,P,N) logServalPacket(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, (M), (P), (N)) + int overlay_add_selfannouncement(); int overlay_frame_package_fmt1(overlay_frame *p,overlay_buffer *b); int overlay_interface_args(const char *arg); @@ -797,7 +801,7 @@ int overlay_saw_mdp_containing_frame(overlay_frame *f,long long now); #include "nacl.h" -int serval_packetvisualise(FILE *f,char *message,unsigned char *packet,int plen); +int serval_packetvisualise(FILE *f, const char *message, const unsigned char *packet, size_t len); int overlay_broadcast_drop_check(unsigned char *a); int overlay_address_is_broadcast(unsigned char *a); diff --git a/serval_packetvisualise.c b/serval_packetvisualise.c index 37d06a0b..e8a1ffcc 100644 --- a/serval_packetvisualise.c +++ b/serval_packetvisualise.c @@ -1,3 +1,23 @@ +/* +Serval Mesh +Copyright (C) 2010-2012 Paul Gardner-Stephen +Copyright (C) 2010-2012 Serval Project Pty Limited +Copyright (C) 2012 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ #include #include #include @@ -26,7 +46,6 @@ int main(int argc,char **argv) int i; len=random()%8192; for(i=0;i"); } else fprintf(f," "); @@ -139,7 +158,7 @@ int serval_packetvisualise_renderaddress(FILE *f,unsigned char *packet,int *ofs, } -int isOverlayPacket(FILE *f,unsigned char *packet,int *ofs,int len) +int isOverlayPacket(FILE *f, const unsigned char *packet, size_t *ofs, size_t len) { if (packet[(*ofs)]!=0x4f) return 0; if (packet[(*ofs)+1]!=0x10) return 0; @@ -195,7 +214,7 @@ int isOverlayPacket(FILE *f,unsigned char *packet,int *ofs,int len) /* Assuming that there is no compression or crypto, we just use the plain body of the frame. */ - unsigned char *frame=&packet[*ofs]; + const unsigned char *frame=&packet[*ofs]; int frame_len=rfs; next_frame_ofs=(*ofs)+rfs; @@ -544,13 +563,12 @@ int isOverlayPacket(FILE *f,unsigned char *packet,int *ofs,int len) return 1; } -int isDNAPacket(FILE *f,unsigned char *packet,int *ofs,int len) +int isDNAPacket(FILE *f, const unsigned char *packet, size_t *ofs, size_t len) { return 0; } - -int serval_packetvisualise(FILE *f,char *message,unsigned char *packet,int len) +int serval_packetvisualise(FILE *f, const char *message, const unsigned char *packet, size_t len) { if (f == NULL) return -1; @@ -570,19 +588,18 @@ int serval_packetvisualise(FILE *f,char *message,unsigned char *packet,int len) fprintf(f,"\n"); } - int ofs=0; + size_t ofs=0; fprintf(f," Packet Structure:\n"); - if (isOverlayPacket(f,packet,&ofs,len)) - { } + ; else if (isDNAPacket(f,packet,&ofs,len)) - { } + ; else { /* Unknown packet type. */ } if (ofs=ofs) { diff --git a/sqlite-amalgamation-3070900/encode.c b/sqlite-amalgamation-3070900/encode.c index 07aa091d..5bc9079d 100644 --- a/sqlite-amalgamation-3070900/encode.c +++ b/sqlite-amalgamation-3070900/encode.c @@ -247,9 +247,7 @@ int main(int argc, char **argv){ } printf(" OK\n"); } - fprintf(stderr,"Finished. Total encoding: %d->%d bytes\n", - nByteIn, nByteOut); - fprintf(stderr,"Avg size increase: %.3f%%\n", - (nByteOut-nByteIn)*100.0/(double)nByteIn); + printf("Finished. Total encoding: %d->%d bytes\n", nByteIn, nByteOut); + printf("Avg size increase: %.3f%%\n", (nByteOut-nByteIn)*100.0/(double)nByteIn); } #endif /* ENCODER_TEST */