xref: /netbsd/usr.sbin/mopd/moptrace/moptrace.c (revision 6550d01e)
1 /*	$NetBSD: moptrace.c,v 1.10 2009/10/20 00:51:14 snj 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 <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: moptrace.c,v 1.10 2009/10/20 00:51:14 snj 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 struct if_info *iflist;
55 
56 void	Usage __P((void));
57 int	main __P((int, char **));
58 void	mopProcess __P((struct if_info *, u_char *));
59 
60 int     AllFlag = 0;		/* listen on "all" interfaces  */
61 int     DebugFlag = 0;		/* print debugging messages    */
62 int	Not3Flag = 0;		/* Ignore MOP V3 messages      */
63 int	Not4Flag = 0;		/* Ignore MOP V4 messages      */
64 int	promisc = 1;		/* Need promisc mode           */
65 
66 int
67 main(argc, argv)
68 	int     argc;
69 	char  **argv;
70 {
71 	int     op;
72 	char   *interface;
73 
74 	mopInteractive = 1;
75 	opterr = 0;
76 	while ((op = getopt(argc, argv, "34ad")) != -1) {
77 		switch (op) {
78 		case '3':
79 			Not3Flag++;
80 			break;
81 		case '4':
82 			Not4Flag++;
83 			break;
84 		case 'a':
85 			AllFlag++;
86 			break;
87 		case 'd':
88 			DebugFlag++;
89 			break;
90 		default:
91 			Usage();
92 			/* NOTREACHED */
93 		}
94 	}
95 
96 	interface = argv[optind++];
97 
98 	if ((AllFlag && interface) ||
99 	    (!AllFlag && interface == 0) ||
100 	    (Not3Flag && Not4Flag))
101 		Usage();
102 
103 	if (AllFlag)
104  		deviceInitAll();
105 	else
106 		deviceInitOne(interface);
107 
108 	Loop();
109 	/* NOTREACHED */
110 	return (0);
111 }
112 
113 void
114 Usage()
115 {
116 	(void) fprintf(stderr, "usage: %s -a [ -d ] [ -3 | -4 ]\n",
117 		       getprogname());
118 	(void) fprintf(stderr, "       %s [ -d ] [ -3 | -4 ] interface\n",
119 		       getprogname());
120 	exit(1);
121 }
122 
123 /*
124  * Process incoming packages.
125  */
126 void
127 mopProcess(ii, pkt)
128 	struct if_info *ii;
129 	u_char *pkt;
130 {
131 	int	 trans;
132 
133 	/* We don't known which transport, Guess! */
134 
135 	trans = mopGetTrans(pkt, 0);
136 
137 	/* Ok, return if we don't want this message */
138 
139 	if ((trans == TRANS_ETHER) && Not3Flag) return;
140 	if ((trans == TRANS_8023) && Not4Flag)	return;
141 
142 	mopPrintHeader(stdout, pkt, trans);
143 	mopPrintMopHeader(stdout, pkt, trans);
144 
145 	mopDumpDL(stdout, pkt, trans);
146 	mopDumpRC(stdout, pkt, trans);
147 
148 	fprintf(stdout, "\n");
149 	fflush(stdout);
150 }
151