2 * Copyright (c) 1996, 2000 Hellmuth Michaelis. All rights reserved.
4 * Copyright (c) 1996 Gary Jennejohn. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 * 4. Altered versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software and/or documentation.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 *---------------------------------------------------------------------------*
35 * trace.c - print traces of D (B) channel activity for isdn4bsd
36 * -------------------------------------------------------------
38 * $Id: trace.c,v 1.19 2000/08/28 07:06:42 hm Exp $
40 * $FreeBSD: src/usr.sbin/i4b/isdntrace/trace.c,v 1.8.2.1 2001/08/01 17:45:08 obrien Exp $
41 * $DragonFly: src/usr.sbin/i4b/isdntrace/trace.c,v 1.3 2005/11/25 00:58:52 swildner Exp $
43 * last edit-date: [Mon Aug 28 09:03:46 2000]
45 *---------------------------------------------------------------------------*/
49 unsigned char buf[BSIZE];
70 int enable_trace = TRACE_D_RX | TRACE_D_TX;
72 static char outfilename[MAXPATHLEN];
73 static char routfilename[MAXPATHLEN];
74 static char BPfilename[MAXPATHLEN];
75 static char rBPfilename[MAXPATHLEN];
77 static struct stat fst;
79 static void dumpbuf( int n, unsigned char *buf, i4b_trace_hdr_t *hdr, int raw );
80 static int switch_driver( int value, int rx, int tx );
81 static void usage( void );
82 static void exit_hdl( void );
83 static void reopenfiles( int );
84 void add_datetime(char *filename, char *rfilename);
86 /*---------------------------------------------------------------------------*
88 *---------------------------------------------------------------------------*/
93 fprintf(stderr,"isdntrace - i4b package ISDN trace facility for passive cards (%02d.%02d.%d)\n", VERSION, REL, STEP);
94 fprintf(stderr,"usage: isdntrace -a -b -d -f <file> -h -i -l -n <val> -o -p <file> -r -u <unit>\n");
95 fprintf(stderr," -x -B -F -P -R <unit> -T <unit>\n");
96 fprintf(stderr," -a analyzer mode ................................... (default off)\n");
97 fprintf(stderr," -b switch B channel trace on ....................... (default off)\n");
98 fprintf(stderr," -d switch D channel trace off ....................... (default on)\n");
99 fprintf(stderr," -f <file> write output to file filename ............ (default %s0)\n", TRACE_FILE_NAME);
100 fprintf(stderr," -h don't print header for each message ............. (default off)\n");
101 fprintf(stderr," -i print I.430 (layer 1) INFO signals .............. (default off)\n");
102 fprintf(stderr," -l don't decode low layer Q.921 messages ........... (default off)\n");
103 fprintf(stderr," -n <val> process packet if it is longer than <val> octetts . (default 0)\n");
104 fprintf(stderr," -o don't write output to a file .................... (default off)\n");
105 fprintf(stderr," -p <file> specify filename for -B and -P ........ (default %s0)\n", BIN_FILE_NAME);
106 fprintf(stderr," -r don't print raw hex/ASCII dump of protocol ...... (default off)\n");
107 fprintf(stderr," -u <unit> specify controller unit number ............... (default unit 0)\n");
108 fprintf(stderr," -x show packets with unknown protocol discriminator (default off)\n");
109 fprintf(stderr," -B write binary trace data to file filename ........ (default off)\n");
110 fprintf(stderr," -F with -P and -p: wait for more data at EOF ....... (default off)\n");
111 fprintf(stderr," -P playback from binary trace data file ............ (default off)\n");
112 fprintf(stderr," -R <unit> analyze Rx controller unit number (for -a) ... (default unit %d)\n", RxUDEF);
113 fprintf(stderr," -T <unit> analyze Tx controller unit number (for -a) ... (default unit %d)\n", TxUDEF);
114 fprintf(stderr,"\n");
118 /*---------------------------------------------------------------------------*
120 *---------------------------------------------------------------------------*/
122 main(int argc, char *argv[])
134 char *outfile = TRACE_FILE_NAME;
135 char *binfile = BIN_FILE_NAME;
140 i4b_trace_hdr_t *ithp = NULL;
142 static struct stat fstnew;
144 b = &buf[sizeof(i4b_trace_hdr_t)];
146 while( (c = getopt(argc, argv, "abdf:hiln:op:ru:xBFPR:T:")) != -1)
155 enable_trace |= (TRACE_B_RX | TRACE_B_TX);
159 enable_trace &= (~(TRACE_D_TX | TRACE_D_RX));
180 enable_trace |= TRACE_I;
199 if(unit < 0 || unit >= MAX_CONTROLLERS)
221 if(Rx < 0 || Rx >= MAX_CONTROLLERS)
227 if(Tx < 0 || Tx >= MAX_CONTROLLERS)
238 if(enable_trace == 0)
249 sprintf(BPfilename, "%s", binfile);
251 sprintf(BPfilename, "%s%d", BIN_FILE_NAME, unit);
253 add_datetime(BPfilename, rBPfilename);
255 if((BP = fopen(rBPfilename, "w")) == NULL)
259 sprintf(buffer, "Error opening file [%s]", rBPfilename);
264 if((setvbuf(BP, NULL, _IONBF, 0)) != 0)
268 sprintf(buffer, "Error setting file [%s] to unbuffered", rBPfilename);
277 sprintf(BPfilename, "%s", binfile);
279 sprintf(BPfilename, "%s%d", BIN_FILE_NAME, unit);
281 strcpy(rBPfilename, BPfilename);
283 if((BP = fopen(BPfilename, "r")) == NULL)
287 sprintf(buffer, "Error opening file [%s]", BPfilename);
293 if(fstat(fileno(BP), &fst))
296 sprintf(buffer, "Error fstat file [%s]", BPfilename);
304 sprintf(devicename, "%s%d", I4BTRC_DEVICE, unit);
306 if((f = open(devicename, O_RDWR)) < 0)
310 sprintf(buffer, "Error opening trace device [%s]", devicename);
319 sprintf(outfilename, "%s%d", TRACE_FILE_NAME, unit);
321 strcpy(outfilename, outfile);
323 add_datetime(outfilename, routfilename);
325 if((Fout = fopen(routfilename, "w")) == NULL)
329 sprintf(buffer, "Error opening file [%s]", routfilename);
334 if((setvbuf(Fout, NULL, _IONBF, 0)) != 0)
338 sprintf(buffer, "Error setting file [%s] to unbuffered", routfilename);
344 if((setvbuf(stdout, NULL, _IOLBF, 0)) != 0)
348 sprintf(buffer, "Error setting stdout to line-buffered");
355 if((switch_driver(enable_trace, Rx, Tx)) == -1)
361 signal(SIGHUP, SIG_IGN); /* ignore hangup signal */
362 signal(SIGUSR1, reopenfiles); /* rotate logfile(s) */
368 sprintf(headerbuf, "\n==== isdnanalyze controller rx #%d - tx #%d ==== started %s",
373 sprintf(headerbuf, "\n=========== isdntrace controller #%d =========== started %s",
377 printf("%s", headerbuf);
380 fprintf(Fout, "%s", headerbuf);
386 n = read(f, buf, BSIZE);
390 if((fwrite(buf, 1, n, BP)) != n)
393 sprintf(buffer, "Error writing file [%s]", rBPfilename);
399 n -= sizeof(i4b_trace_hdr_t);
404 if((fread(buf, 1, sizeof(i4b_trace_hdr_t), BP)) != sizeof(i4b_trace_hdr_t))
413 sprintf(buffer, "Error reading hdr from file [%s]", rBPfilename);
421 if(stat(rBPfilename, &fstnew) != -1)
423 if((fst.st_ino != fstnew.st_ino) ||
424 (fstnew.st_nlink == 0))
426 if((BP = freopen(rBPfilename, "r", BP)) == NULL)
429 sprintf(buffer, "Error reopening file [%s]", rBPfilename);
433 stat(rBPfilename, &fst);
440 printf("\nEnd of playback input file reached.\n");
447 sprintf(buffer, "Error reading hdr from file [%s]", rBPfilename);
453 ithp = (i4b_trace_hdr_t *)buf;
454 l = ithp->length - sizeof(i4b_trace_hdr_t);
456 if((n = fread(buf+sizeof(i4b_trace_hdr_t), 1, l , BP)) != l)
459 sprintf(buffer, "Error reading data from file [%s]", rBPfilename);
466 if((n > 0) && (n > noct))
468 dumpbuf(n, b, (i4b_trace_hdr_t *)buf, raw);
473 /*---------------------------------------------------------------------------*
474 * format header into static buffer, return buffer address
475 *---------------------------------------------------------------------------*/
477 fmt_hdr(i4b_trace_hdr_t *hdr, int frm_len)
480 static char hbuf[256];
483 s = localtime((time_t *)&(hdr->time.tv_sec));
485 if(hdr->type == TRC_CH_I) /* Layer 1 INFO's */
487 sprintf(hbuf,"\n-- %s - unit:%d ---------------- time:%2.2d.%2.2d %2.2d:%2.2d:%2.2d.%06u ",
488 ((hdr->dir) ? "NT->TE" : "TE->NT"),
495 (u_int32_t)hdr->time.tv_usec);
501 sprintf(hbuf,"\n-- %s - unit:%d - frame:%6.6u - time:%2.2d.%2.2d %2.2d:%2.2d:%2.2d.%06u - length:%d (%d) ",
502 ((hdr->dir) ? "NT->TE" : "TE->NT"),
510 (u_int32_t)hdr->time.tv_usec,
516 sprintf(hbuf,"\n-- %s - unit:%d - frame:%6.6u - time:%2.2d.%2.2d %2.2d:%2.2d:%2.2d.%06u - length:%d ",
517 ((hdr->dir) ? "NT->TE" : "TE->NT"),
525 (u_int32_t)hdr->time.tv_usec,
530 for(i=strlen(hbuf); i <= NCOLS;)
539 /*---------------------------------------------------------------------------*
540 * decode protocol and output to file(s)
541 *---------------------------------------------------------------------------*/
543 dumpbuf(int n, unsigned char *buf, i4b_trace_hdr_t *hdr, int raw)
545 static char l1buf[128];
546 static unsigned char l2buf[32000];
547 static unsigned char l3buf[32000];
559 case TRC_CH_I: /* Layer 1 INFO's */
561 /* on playback, don't display layer 1 if -i ! */
562 if(!(enable_trace & TRACE_I))
570 sprintf((pbuf+strlen(pbuf)),"I430: INFO0 (No Signal)\n");
574 sprintf((pbuf+strlen(pbuf)),"I430: INFO1 (Activation Request, Priority = 8, from TE)\n");
578 sprintf((pbuf+strlen(pbuf)),"I430: INFO1 (Activation Request, Priority = 10, from TE)\n");
582 sprintf((pbuf+strlen(pbuf)),"I430: INFO2 (Pending Activation, from NT)\n");
586 sprintf((pbuf+strlen(pbuf)),"I430: INFO3 (Synchronized, from TE)\n");
590 sprintf((pbuf+strlen(pbuf)),"I430: INFO4 (Activated, Priority = 8/9, from NT)\n");
594 sprintf((pbuf+strlen(pbuf)),"I430: INFO4 (Activated, Priority = 10/11, from NT)\n");
598 sprintf((pbuf+strlen(pbuf)),"I430: ERROR, invalid INFO value 0x%x!\n", buf[0]);
603 case TRC_CH_D: /* D-channel data */
605 cnt = decode_lapd(l2buf, n, buf, hdr->dir, raw, print_q921);
616 decode_1tr6(l3buf, n, cnt, buf, raw);
620 decode_q931(l3buf, n, cnt, buf, raw);
631 decode_unknownl3(l3buf, n, cnt, buf, raw);
638 default: /* B-channel data */
642 for (i = 0; i < n; i += 16)
644 sprintf((pbuf+strlen(pbuf)),"B%d:%.3x ", hdr->type, i);
646 for (j = 0; j < 16; j++)
648 sprintf((pbuf+strlen(pbuf)),"%02x ", buf[i + j]);
650 sprintf((pbuf+strlen(pbuf))," ");
652 sprintf((pbuf+strlen(pbuf))," ");
654 for (j = 0; j < 16 && i + j < n; j++)
655 if (isprint(buf[i + j]))
656 sprintf((pbuf+strlen(pbuf)),"%c", buf[i + j]);
658 sprintf((pbuf+strlen(pbuf)),".");
660 sprintf((pbuf+strlen(pbuf)),"\n");
665 if(header && ((l1buf[0] != '\0' || l2buf[0] != '\0') || (l3buf[0] != 0)))
668 p = fmt_hdr(hdr, nsave);
671 fprintf(Fout, "%s", p);
678 fprintf(Fout, "%s", l1buf);
685 fprintf(Fout, "%s", l2buf);
692 fprintf(Fout, "%s", l3buf);
696 /*---------------------------------------------------------------------------*
697 * exit handler function to be called at program exit
698 *---------------------------------------------------------------------------*/
703 switch_driver(TRACE_OFF, Rx, Tx);
706 /*---------------------------------------------------------------------------*
707 * switch driver debugging output on/off
708 *---------------------------------------------------------------------------*/
710 switch_driver(int value, int rx, int tx)
717 if(ioctl(f, I4B_TRC_SET, &v) < 0)
719 sprintf(buffer, "Error ioctl I4B_TRC_SET, val = %d", v);
726 if(value == TRACE_OFF)
728 if(ioctl(f, I4B_TRC_RESETA, &v) < 0)
730 sprintf(buffer, "Error ioctl I4B_TRC_RESETA - ");
737 i4b_trace_setupa_t tsa;
744 if(ioctl(f, I4B_TRC_SETA, &tsa) < 0)
746 sprintf(buffer, "Error ioctl I4B_TRC_SETA, val = %d", v);
755 /*---------------------------------------------------------------------------*
756 * reopen files to support rotating logfile(s) on SIGUSR1
758 * based on an idea from Ripley (ripley@nostromo.in-berlin.de)
760 * close file and reopen it for append. this will be a nop
761 * if the previously opened file hasn't moved but will open
762 * a new one otherwise, thus enabling a rotation...
764 *---------------------------------------------------------------------------*/
766 reopenfiles(int dummy)
772 add_datetime(outfilename, routfilename);
774 if((Fout = fopen(routfilename, "a")) == NULL)
778 sprintf(buffer, "Error re-opening file [%s]", routfilename);
783 if((setvbuf(Fout, NULL, _IONBF, 0)) != 0)
787 sprintf(buffer, "Error re-setting file [%s] to unbuffered", routfilename);
798 add_datetime(BPfilename, rBPfilename);
800 if((BP = fopen(rBPfilename, "a")) == NULL)
804 sprintf(buffer, "Error re-opening file [%s]", rBPfilename);
809 if((setvbuf(BP, NULL, _IONBF, 0)) != 0)
813 sprintf(buffer, "Error re-setting file [%s] to unbuffered", rBPfilename);
821 add_datetime(char *filename, char *rfilename)
828 tmp = localtime(&timeb);
830 sprintf(rfilename, "%s-", filename);
832 strftime(rfilename+strlen(rfilename), MAXPATHLEN-strlen(rfilename)-1,
833 "%Y%m%d-%H%M%S", tmp);
835 if((fx = fopen(rfilename, "r")) != NULL)
842 tmp = localtime(&timeb);
844 sprintf(rfilename, "%s-", filename);
846 strftime(rfilename+strlen(rfilename), MAXPATHLEN-strlen(rfilename)-1,
847 "%Y%m%d-%H%M%S", tmp);