1 /*        $NetBSD: moptrace.c,v 1.13 2020/04/22 23:57:56 joerg Exp $  */
2 
3 /*
4  * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "port.h"
28 #ifndef lint
29 __RCSID("$NetBSD: moptrace.c,v 1.13 2020/04/22 23:57:56 joerg Exp $");
30 #endif
31 
32 /*
33  * moptrace - MOP Trace Utility
34  *
35  * Usage: moptrace -a [ -d ] [ -3 | -4 ]
36  *                  moptrace [ -d ] [ -3 | -4 ] interface
37  */
38 
39 #include "os.h"
40 #include "common.h"
41 #include "device.h"
42 #include "dl.h"
43 #include "get.h"
44 #include "mopdef.h"
45 #include "pf.h"
46 #include "print.h"
47 #include "rc.h"
48 #include "log.h"
49 
50 /*
51  * The list of all interfaces that are being listened to.
52  * "selects" on the descriptors in this list.
53  */
54 extern struct if_info *iflist;
55 
56 __dead static void  Usage(void);
57 void      mopProcess(struct if_info *, u_char *);
58 
59 int     AllFlag = 0;                    /* listen on "all" interfaces  */
60 int     DebugFlag = 0;                  /* print debugging messages    */
61 int       Not3Flag = 0;                 /* Ignore MOP V3 messages      */
62 int       Not4Flag = 0;                 /* Ignore MOP V4 messages      */
63 int       promisc = 1;                  /* Need promisc mode           */
64 
65 int
main(int argc,char ** argv)66 main(int argc, char  **argv)
67 {
68           int     op;
69           char   *interface;
70 
71           mopInteractive = 1;
72           opterr = 0;
73           while ((op = getopt(argc, argv, "34ad")) != -1) {
74                     switch (op) {
75                     case '3':
76                               Not3Flag++;
77                               break;
78                     case '4':
79                               Not4Flag++;
80                               break;
81                     case 'a':
82                               AllFlag++;
83                               break;
84                     case 'd':
85                               DebugFlag++;
86                               break;
87                     default:
88                               Usage();
89                               /* NOTREACHED */
90                     }
91           }
92 
93           interface = argv[optind++];
94 
95           if ((AllFlag && interface) ||
96               (!AllFlag && interface == 0) ||
97               (Not3Flag && Not4Flag))
98                     Usage();
99 
100           if (AllFlag)
101                     deviceInitAll();
102           else
103                     deviceInitOne(interface);
104 
105           Loop();
106           /* NOTREACHED */
107           return (0);
108 }
109 
110 static void
Usage(void)111 Usage(void)
112 {
113           (void) fprintf(stderr, "usage: %s -a [ -d ] [ -3 | -4 ]\n",
114                            getprogname());
115           (void) fprintf(stderr, "       %s [ -d ] [ -3 | -4 ] interface\n",
116                            getprogname());
117           exit(1);
118 }
119 
120 /*
121  * Process incoming packages.
122  */
123 void
mopProcess(struct if_info * ii,u_char * pkt)124 mopProcess(struct if_info *ii, u_char *pkt)
125 {
126           int        trans;
127 
128           /* We don't known which transport, Guess! */
129 
130           trans = mopGetTrans(pkt, 0);
131 
132           /* Ok, return if we don't want this message */
133 
134           if ((trans == TRANS_ETHER) && Not3Flag) return;
135           if ((trans == TRANS_8023) && Not4Flag)  return;
136 
137           mopPrintHeader(stdout, pkt, trans);
138           mopPrintMopHeader(stdout, pkt, trans);
139 
140           mopDumpDL(stdout, pkt, trans);
141           mopDumpRC(stdout, pkt, trans);
142 
143           fprintf(stdout, "\n");
144           fflush(stdout);
145 }
146