From 73de79ef42d69526bb1f5f4e976c746d96ad99a0 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Wed, 11 Oct 2000 12:00:00 -0700 Subject: [PATCH] Import sersniff 0.0.4 --- HISTORY | 7 ++++ README | 34 ++++++++++------ TODO | 3 ++ disp_basic.c | 8 +++- disp_basic.h | 2 +- sersniff.c | 110 +++++++++++++++++++++++++++++++-------------------- 6 files changed, 106 insertions(+), 58 deletions(-) diff --git a/HISTORY b/HISTORY index 7c2e15c..bc687ba 100644 --- a/HISTORY +++ b/HISTORY @@ -16,3 +16,10 @@ First public release. * Some socket tweaks (Cornelius Cook) * Started breaking out display code in preparation for ncurses version. * Added check for closed sockets. + +0.0.4 - 11th October 2000 + +* Changed -i & -o to cope with either serial ports or TCP ports. +* Added option to not initialize the serial ports (Glen W. Mabey) +* Added format option. (Glen W. Mabey) +* Added port names. (Glen W. Mabey) diff --git a/README b/README index a9821be..0d996c3 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -sersniff 0.0.3 - A program to tunnel between 2 serial ports and show +sersniff 0.0.4 - A program to tunnel between 2 serial ports and show what's happening. Copyright 1999 Jonathan McDowell for Project Purple. http://www.earth.li/projectpurple/progs/sersniff.html @@ -28,25 +28,21 @@ someday, maybe not. Maybe someone else will and will send me a patch. :) Command line options: -sersniff [-h] [-i DEVICE | -l PORT] [-o DEVICE | -c HOST:PORT] [-b BAUD] [-s] +sersniff [-h] [-i DEVICE ] [-o DEVICE] [-b BAUD] [-s] [-n] [-w USECS] -h Displays some command line option help. -i IN_DEV - Set the device to use for Port1, if you're using serial ports. - Default of /dev/ttyS0 - --l PORT - Set the port to listen on, if you want Port1 to be a TCP socket. + Set the device to use for Port1. Default of /dev/ttyS0. If you want + to listen on a TCP port then use the format :port. If there's a / in + the string then it will always be treated as a device rather than a + port to listen on. -o OUT_DEV - Set the device to use for Port2, if you're using serial ports. - Default of /dev/ttyS1 - --c HOST:PORT - Specify the host and port to connect to if Port2 is to be a TCP - socket. + Set the device to use for Port2. Default of /dev/ttyS1. If you want + to connect to a TCP port then use the format host:port. Again if + there's a / in the string then it will always be treated as a device. -b BAUD Specify baud rate for serial connections. Defaults to 19200. @@ -55,6 +51,18 @@ sersniff [-h] [-i DEVICE | -l PORT] [-o DEVICE | -c HOST:PORT] [-b BAUD] [-s] Make sersniff not copy data between the two ports - useful if a Y cable is being used for serial port sniffing for example. +-n + Don't do any port configuration. + +-w USECS + How many microsecs to wait before reporting a delay. + +-x + Show hex characters instead of normal ASCII characters. + +-f + Specify the format string that should be used to print hex characters. + TCP/serial things: diff --git a/TODO b/TODO index 51f3902..58fda00 100644 --- a/TODO +++ b/TODO @@ -9,3 +9,6 @@ * Add filtering rules capability. Perhaps look at some regex libraries? + +* PTYs? Pipes? So we can look like a serial port to programs that expect to + talk to one. diff --git a/disp_basic.c b/disp_basic.c index 39f4e36..d1c5015 100644 --- a/disp_basic.c +++ b/disp_basic.c @@ -52,7 +52,7 @@ void disp_outputstatus(char *string) /* Output a string from the port. */ void disp_outputstr(int port, char *string, - long usec_threshold, long usec_waited) + long usec_threshold, long usec_waited, char *name1, char *name2) { if (usec_waited>usec_threshold) { /* report how long we waited between the last port */ @@ -66,7 +66,11 @@ void disp_outputstr(int port, char *string, if (last!=port) { /* If we didn't just send a CR, we need to now */ if (wrap!=WRAPINIT) printf("\n"); - printf("\nPort%d:\t", port); + //printf("\nPort%d:\t", port); + if (port == 1) + printf("\n%s:\t",name1); + else + printf("\n%s:\t",name2); last=port; wrap=WRAPINIT; } else if (wrap==WRAPINIT) { diff --git a/disp_basic.h b/disp_basic.h index cd6062b..a5653c1 100644 --- a/disp_basic.h +++ b/disp_basic.h @@ -37,5 +37,5 @@ void disp_outputstatus(char *string); /* Output a string from the port. */ void disp_outputstr(int port, char *string, - long usec_threshold, long usec_waited); + long usec_threshold, long usec_waited, char *name1, char *name2); diff --git a/sersniff.c b/sersniff.c index dd28d34..52db00d 100644 --- a/sersniff.c +++ b/sersniff.c @@ -25,7 +25,7 @@ 27Nov1999 - Cook: added select, timer & changed output look */ -#define VERSION "0.0.3" +#define VERSION "0.0.4" #include #include @@ -45,7 +45,7 @@ char * speed_str[] = { "300", "1200", "2400", "4800", "9600", "19200", "38400", speed_t speed_num[] = { B300, B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B0 }; -int openport(const char *device, speed_t baud) +int openport(const char *device, speed_t baud, int setup) { int filedes; struct termios serparams; @@ -57,19 +57,21 @@ int openport(const char *device, speed_t baud) exit(1); } - bzero(&serparams, sizeof(serparams)); + if (setup) { + bzero(&serparams, sizeof(serparams)); - serparams.c_cflag=baud | CLOCAL | CS8 | CREAD; + serparams.c_cflag=baud | CLOCAL | CS8 | CREAD; - if (tcflush(filedes, TCIFLUSH)) { - fprintf(stderr,"%s: ",device); - perror("tcflush"); - exit(1); - } - if (tcsetattr(filedes, TCSANOW, &serparams)) { - fprintf(stderr,"%s: ",device); - perror("tcsetattr"); - exit(1); + if (tcflush(filedes, TCIFLUSH)) { + fprintf(stderr,"%s: ",device); + perror("tcflush"); + exit(1); + } + if (tcsetattr(filedes, TCSANOW, &serparams)) { + fprintf(stderr,"%s: ",device); + perror("tcsetattr"); + exit(1); + } } return filedes; @@ -85,7 +87,7 @@ int closeport(int filedes) this returns the string for the character passed to it It could be expanded in the future to maybe to string recognitions? */ -char *chardecide(unsigned char c, int alpha) { +char *chardecide(unsigned char c, int alpha, char *format) { static char result[256]; /* everyone should take up 5 characters */ @@ -109,21 +111,21 @@ char *chardecide(unsigned char c, int alpha) { snprintf(result,256,"%c",c); } } else { - snprintf(result,256,"0x%02hX ",c); + snprintf(result,256,format,c); } return result; } void outputchar(unsigned char c, int port, int alpha, - long usec_threshold, long usec_waited) + long usec_threshold, long usec_waited, char *name1, char *name2, char *format) { char *todisplay; - todisplay=chardecide(c,alpha); - disp_outputstr(port, todisplay, usec_threshold, usec_waited); + todisplay=chardecide(c,alpha,format); + disp_outputstr(port, todisplay, usec_threshold, usec_waited, name1, name2); } -void mainloop(int port1, int port2, int silent, int alpha, long usec_threshold) +void mainloop(int port1, int port2, int silent, int alpha, long usec_threshold, char *name1, char *name2, char *format) { unsigned char c1, c2; int last; @@ -174,7 +176,7 @@ void mainloop(int port1, int port2, int silent, int alpha, long usec_threshold) if (FD_ISSET(port1, &rfds)) { for (rc=read(port1, &c1, 1); rc>0; rc=read(port1, &c1, 1) ) { - outputchar(c1,1,alpha,usec_threshold,timediff); + outputchar(c1,1,alpha,usec_threshold,timediff,name1,name2,format); timediff=0; if (!silent) write(port2,&c1,1); } @@ -191,7 +193,7 @@ void mainloop(int port1, int port2, int silent, int alpha, long usec_threshold) if (FD_ISSET(port2, &rfds)) { for (rc=read(port2, &c2, 1); rc>0; rc=read(port2, &c2, 1) ) { - outputchar(c2,2,alpha,usec_threshold,timediff); + outputchar(c2,2,alpha,usec_threshold,timediff,name1,name2,format); timediff=0; if (!silent) write(port1,&c2,1); } @@ -232,11 +234,15 @@ Usage: sersniff [-h] [-i DEV | -l PORT] [-o DEV | -c HOST:PORT] [-b BAUD] [-w USEC] -h This help -x Show hex characters instead of alpha --i DEVICE Port 1 device (defaults to /dev/ttyS0) --l PORT Port 1 port for TCP --o DEVICE Port 2 device (defaults to /dev/ttyS1) --c HOST:PORT Port 2 host & port to connect to +-f PRINTF_OPTS printf style options for printing hex characters + when '-x' switch is given (default \"<%%02hX>\") +-i DEVICE Port 1 device (defaults to /dev/ttyS0). Use host:port for + TCP. +-1 PORT1_NAME Port 1 name to be printed (defaults to 'Port1') +-o DEVICE Port 2 device (defaults to /dev/ttyS1). Use :port for TCP. +-2 PORT2_NAME Port 2 name to be printed (defaults to 'Port2') -b BAUD Baud rate (Defaults to 19200) +-n No port configuration (do not set BAUD or change settings) -w USECS How many microsecs to wait before reporting a delay (default is %d) -s Silent - don't pass data from port1 <=> port2, @@ -251,6 +257,8 @@ int main(int argc, char *argv[]) int port1, port2; char *dev1=NULL; char *dev2=NULL; + char *name1=NULL; + char *name2=NULL; int listenport=0; int connectport; char *connecthost=NULL, *tmpchr=NULL; @@ -258,8 +266,10 @@ int main(int argc, char *argv[]) speed_t baud=B0; int silent=0; long usec_threshold=USEC; + int setup_port=1; + char *format=NULL; - while ((optret=getopt(argc,argv,"hxsi:l:o:c:b:w:"))!=EOF) { + while ((optret=getopt(argc,argv,"hxsni:l:o:c:b:w:1:2:f:"))!=EOF) { switch (optret) { case '?': case 'h': case ':': usage(); @@ -270,22 +280,23 @@ int main(int argc, char *argv[]) usec_threshold=atoi(optarg); break; case 'i': - dev1=strdup(optarg); + if ((tmpchr=strchr(optarg, ':'))!=NULL && + (strchr(optarg, '/')==NULL)) { + *tmpchr='\0'; + listenport=atoi(++tmpchr); + } else { + dev1=strdup(optarg); + } break; case 'o': - dev2=strdup(optarg); - break; - case 'l': - listenport=atoi(optarg); - break; - case 'c': - if ((tmpchr=strchr(optarg, ':'))==NULL) { - printf("Must specify -c option with host:port\n"); - exit(1); + if ((tmpchr=strchr(optarg, ':'))!=NULL && + (strchr(optarg, '/')==NULL)) { + *tmpchr='\0'; + connectport=atoi(++tmpchr); + connecthost=strdup(optarg); + } else { + dev2=strdup(optarg); } - *tmpchr='\0'; - connectport=atoi(++tmpchr); - connecthost=strdup(optarg); break; case 'x': show_alpha=0; @@ -305,24 +316,39 @@ int main(int argc, char *argv[]) exit(1); } break; + case 'n': + setup_port=0; + break; + case '1': + name1=strdup(optarg); + break; + case '2': + name2=strdup(optarg); + break; + case 'f': + format=strdup(optarg); + break; } } /* Default settings */ if (!dev1 && !listenport) dev1=strdup("/dev/ttyS0"); + if (!name1 && !listenport) name1=strdup("Port1"); if (!dev2 && !connecthost) dev2=strdup("/dev/ttyS1"); + if (!name2 && !connecthost) name2=strdup("Port2"); if (baud==B0) baud=B19200; + if (!format) format=strdup("0x%02hX"); disp_init(); if (dev1) { - port1=openport(dev1, baud); + port1=openport(dev1, baud, setup_port); } else { disp_outputstatus("Waiting for connection to TCP port."); port1=listensock(listenport); } if (dev2) { - port2=openport(dev2, baud); + port2=openport(dev2, baud, setup_port); } else { disp_outputstatus("Connecting to TCP port."); port2=opensock(connecthost, connectport); @@ -333,7 +359,7 @@ int main(int argc, char *argv[]) exit(1); } - mainloop(port1, port2, silent, show_alpha, usec_threshold); + mainloop(port1, port2, silent, show_alpha, usec_threshold, name1, name2, format); /* Clean up */ if (dev1) free(dev1); -- 2.39.5