#include #include #include #include #include #include #include #include #include "tlmptcpdump.h" static void samples_printhex(const void *packet, int len) { for (int i=0; i len) jend = len; int j; for (j=i; j= ' ' && c < 128){ printf ("%c",c); }else{ printf ("."); } } printf ("\n"); } printf ("\n"); } // #Specbeg: samples / tcpdump /* This shows how to use the tcpdump component to retrieve statistics and content of various TCP sessions. */ static void sample_test1(const char *device,const char *filter) { (device,filter); printf ("newsession: %08x:%u -> %08x:%u (nbsession %d)\n" ,session.saddr,session.sport ,session.daddr,session.dport ,getnbsession()); printf ("endsession: %08x:%u -> %08x:%u (nbsession %d)\n" ,session.saddr,session.sport ,session.daddr,session.dport ,getnbsession()); printf ("packet: %08x:%u %s %08x:%u\n\t[rec %u/%Lu/%ld.%ld snd %u/%Lu/%ld.%ld]\n" ,session.saddr,session.sport ,from ? "<-" : "->" ,session.daddr,session.dport ,stats.from.nbpk,stats.from.size ,stats.from.stamp.tv_sec,stats.from.stamp.tv_usec ,stats.to.nbpk,stats.to.size ,stats.to.stamp.tv_sec,stats.to.stamp.tv_usec); samples_printhex (packet,len); } // #Specend: // #Specbeg: samples / tcpdump / data statistics /* Using the class TCPINFO, we see how we can attach a TCPINFO object to a TCP session to collect per-session information. In this case we are counting the eol characters sent/received. */ class TCPINFO: public ARRAY_OBJ{ public: int nbrec; int nbsent; TCPINFO(){ nbrec = 0; nbsent = 0; } }; static void sample_countlines(const char *device,const char *filter) { (device,filter); data = new TCPINFO; TCPINFO *info = (TCPINFO*)data; printf ("endsession: %08x:%u -> %08x:%u -- newlines: received %d sent %d\n" ,session.saddr,session.sport ,session.daddr,session.dport ,info->nbrec,info->nbsent); TCPINFO *info = (TCPINFO*)data; const char *pt = (const char*)packet; int nl = 0; for (int i=0; inbsent += nl; }else{ info->nbrec += nl; } } // #Specend: // #Specbeg: samples / tcpdump / selecting session /* Here we show how to discard some session and how to handle already started sessions. To test, we use the sample_client and sample_server twice. */ static void sample_discard(const char *device,const char *filter) { (device,filter); if (session.dport == 8000){ printf ("newsession: %08x:%u -> %08x:%u\n" ,session.saddr,session.sport ,session.daddr,session.dport); }else if (session.sport == 8000){ printf ("newsession: %08x:%u -> %08x:%u\n" ,session.daddr,session.dport ,session.saddr,session.sport); reverse = true; }else{ discard = true; } printf ("endsession: %08x:%u -> %08x:%u (%s)\n" ,session.saddr,session.sport ,session.daddr,session.dport ,clientending ? "Client ended" : "Server ended"); printf ("packet: %08x:%u %s %08x:%u\n\t[rec %u/%Lu/%ld.%ld (%lu) snd %u/%Lu/%ld.%ld (%lu)]\n" ,session.saddr,session.sport ,from ? "<-" : "->" ,session.daddr,session.dport ,stats.from.nbpk,stats.from.size ,stats.from.stamp.tv_sec,stats.from.stamp.tv_usec ,stats.from.seq ,stats.to.nbpk,stats.to.size ,stats.to.stamp.tv_sec,stats.to.stamp.tv_usec ,stats.to.seq); } // #Specend: /* The following test cases are there to test the performance of the tcpdump component to handle many session in parallel. sample_server is simply an echo server. */ static void sample_server (const char *port) { (port,60); sendf ("%s\n",line); o.loop(); } static void sample_client ( const char *server, // Connect to this servere const char *port, // On this port int nbloop, // Send that many messages int waitt) // wait that many micro between each { int count; int waitt; glocal.count = nbloop; glocal.waitt = waitt; (server,port,60); send ("one line\n"); // fprintf (stderr,"oneline %s\n",line); set_timeout (0,glocal.waitt); if (glocal.count == 0){ end = true; }else{ glocal.count--; send ("one line\n"); } } int main (int argc, char *argv[]) { int ret = (argc,argv,"tlmptcpdump"); if (strcmp(argv[0],"hexdump")==0){ // Simple tcpdump + hex const char *device = argc > 1 ? argv[1] : NULL; const char *filter = argc > 2 ? argv[2] : NULL; sample_test1(device,filter); }else if (strcmp(argv[0],"countlines")==0){ // Simple tcpdump + hex const char *device = argc > 1 ? argv[1] : NULL; const char *filter = argc > 2 ? argv[2] : NULL; sample_countlines(device,filter); }else if (strcmp(argv[0],"discard")==0){ // Simple tcpdump + hex const char *device = argc > 1 ? argv[1] : NULL; const char *filter = argc > 2 ? argv[2] : NULL; sample_discard(device,filter); }else if (strcmp(argv[0],"server")==0){ const char *port = argc > 1 ? argv[1] : "8000"; sample_server (port); }else if (strcmp(argv[0],"client")==0){ const char *server = argc > 1 ? argv[1] : "localhost"; const char *port = argc > 2 ? argv[2] : "8000"; int nbloop = argc > 3 ? atoi(argv[3]) : 1000; int waitt = argc > 4 ? atoi(argv[4]) : 500000; int nbproc = argc > 5 ? atoi(argv[5]) : 1; fprintf (stderr,"sample_client %s %s %d %d %d\n",server,port ,nbloop,waitt,nbproc); for (int i=0; i }