]> the.earth.li Git - sersniff.git/blobdiff - sersniff.c
Import sersniff 0.0.4
[sersniff.git] / sersniff.c
index dd28d3407b5f0ec1c1b294abae857239c256dcda..52db00de84eb9cfaf757bd25c5dc0d0a5048b8f4 100644 (file)
@@ -25,7 +25,7 @@
        27Nov1999  - Cook: added select, timer & changed output look
 */
 
-#define VERSION "0.0.3"
+#define VERSION "0.0.4"
 
 #include <stdio.h>
 #include <string.h>
@@ -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);