mopprobe.c revision 1.4 1 /* $NetBSD: mopprobe.c,v 1.4 1997/04/17 21:09:27 christos Exp $ */
2
3 /*
4 * Copyright (c) 1993-96 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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Mats O Jansson.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef LINT
33 static char rcsid[] = "$NetBSD: mopprobe.c,v 1.4 1997/04/17 21:09:27 christos Exp $";
34 #endif
35
36 /*
37 * mopprobe - MOP Probe Utility
38 *
39 * Usage: mopprobe -a [ -3 | -4 ]
40 * mopprobe [ -3 | -4 ] interface
41 */
42
43 #include "os.h"
44 #include "common/common.h"
45 #include "common/mopdef.h"
46 #include "common/device.h"
47 #include "common/print.h"
48 #include "common/get.h"
49 #include "common/cmp.h"
50 #include "common/pf.h"
51 #include "common/nmadef.h"
52
53 /*
54 * The list of all interfaces that are being listened to. rarp_loop()
55 * "selects" on the descriptors in this list.
56 */
57 struct if_info *iflist;
58
59 #ifdef NO__P
60 void Loop (/* void */);
61 void Usage (/* void */);
62 void mopProcess (/* struct if_info *, u_char * */);
63 #else
64 void Loop __P((void));
65 void Usage __P((void));
66 void mopProcess __P((struct if_info *, u_char *));
67 #endif
68
69 int AllFlag = 0; /* listen on "all" interfaces */
70 int DebugFlag = 0; /* print debugging messages */
71 int Not3Flag = 0; /* Not MOP V3 messages */
72 int Not4Flag = 0; /* Not MOP V4 messages */
73 int oflag = 0; /* print only once */
74 int promisc = 1; /* Need promisc mode */
75 char *Program;
76
77 void
78 main(argc, argv)
79 int argc;
80 char **argv;
81 {
82 int op;
83 char *interface;
84
85 extern int optind, opterr;
86
87 if ((Program = strrchr(argv[0], '/')))
88 Program++;
89 else
90 Program = argv[0];
91 if (*Program == '-')
92 Program++;
93
94 /* All error reporting is done through syslogs. */
95 openlog(Program, LOG_PID | LOG_CONS, LOG_DAEMON);
96
97 opterr = 0;
98 while ((op = getopt(argc, argv, "ado")) != EOF) {
99 switch (op) {
100 case '3':
101 Not3Flag++;
102 break;
103 case '4':
104 Not4Flag++;
105 break;
106 case 'a':
107 AllFlag++;
108 break;
109 case 'd':
110 DebugFlag++;
111 break;
112 case 'o':
113 oflag++;
114 break;
115
116 default:
117 Usage();
118 /* NOTREACHED */
119 }
120 }
121 interface = argv[optind++];
122
123 if ((AllFlag && interface) ||
124 (!AllFlag && interface == 0) ||
125 (Not3Flag && Not4Flag))
126 Usage();
127
128 if (AllFlag)
129 deviceInitAll();
130 else
131 deviceInitOne(interface);
132
133 Loop();
134 }
135
136 void
137 Usage()
138 {
139 (void) fprintf(stderr, "usage: %s -a [ -3 | -4 ]\n",Program);
140 (void) fprintf(stderr, " %s [ -3 | -4 ] interface\n",Program);
141 exit(1);
142 }
143
144 /*
145 * Process incomming packages.
146 */
147 void
148 mopProcess(ii, pkt)
149 struct if_info *ii;
150 u_char *pkt;
151 {
152 u_char *dst, *src, *p, mopcode, tmpc, ilen;
153 u_short *ptype, moplen, tmps, itype, len;
154 int index, i, device, trans;
155
156 dst = pkt;
157 src = pkt+6;
158 ptype = (u_short *)(pkt+12);
159 index = 0;
160
161 if (*ptype < 1600) {
162 len = *ptype;
163 trans = TRANS_8023;
164 ptype = (u_short *)(pkt+20);
165 p = pkt+22;
166 if (Not4Flag) return;
167 } else {
168 len = 0;
169 trans = TRANS_ETHER;
170 p = pkt+14;
171 if (Not3Flag) return;
172 }
173
174 /* Ignore our own messages */
175
176 if (mopCmpEAddr(ii->eaddr,src) == 0) {
177 return;
178 }
179
180 /* Just check multicast */
181
182 if (mopCmpEAddr(rc_mcst,dst) != 0) {
183 return;
184 }
185
186 switch (trans) {
187 case TRANS_8023:
188 moplen = len;
189 break;
190 default:
191 moplen = mopGetShort(pkt,&index);
192 }
193 mopcode = mopGetChar(p,&index);
194
195 /* Just process System Information */
196
197 if (mopcode != MOP_K_CODE_SID) {
198 return;
199 }
200
201 tmpc = mopGetChar(pkt,&index); /* Reserved */
202 tmps = mopGetShort(pkt,&index); /* Receipt # */
203
204 device = 0; /* Unknown Device */
205
206 itype = mopGetShort(pkt,&index);
207
208 while (index < (int)(moplen + 2)) {
209 ilen = mopGetChar(pkt,&index);
210 switch (itype) {
211 case 0:
212 tmpc = mopGetChar(pkt,&index);
213 index = index + tmpc;
214 break;
215 case MOP_K_INFO_VER:
216 index = index + 3;
217 break;
218 case MOP_K_INFO_MFCT:
219 index = index + 2;
220 break;
221 case MOP_K_INFO_CNU:
222 index = index + 6;
223 break;
224 case MOP_K_INFO_RTM:
225 index = index + 2;
226 break;
227 case MOP_K_INFO_CSZ:
228 index = index + 2;
229 break;
230 case MOP_K_INFO_RSZ:
231 index = index + 2;
232 break;
233 case MOP_K_INFO_HWA:
234 index = index + 6;
235 break;
236 case MOP_K_INFO_TIME:
237 index = index + 10;
238 break;
239 case MOP_K_INFO_SOFD:
240 device = mopGetChar(pkt,&index);
241 break;
242 case MOP_K_INFO_SFID:
243 tmpc = mopGetChar(pkt,&index);
244 if ((index > 0) && (index < 17))
245 index = index + tmpc;
246 break;
247 case MOP_K_INFO_PRTY:
248 index = index + 1;
249 break;
250 case MOP_K_INFO_DLTY:
251 index = index + 1;
252 break;
253 case MOP_K_INFO_DLBSZ:
254 index = index + 2;
255 break;
256 default:
257 if (((device = NMA_C_SOFD_LCS) || /* DECserver 100 */
258 (device = NMA_C_SOFD_DS2) || /* DECserver 200 */
259 (device = NMA_C_SOFD_DP2) || /* DECserver 250 */
260 (device = NMA_C_SOFD_DS3)) && /* DECserver 300 */
261 ((itype > 101) && (itype < 107)))
262 {
263 switch (itype) {
264 case 102:
265 index = index + ilen;
266 break;
267 case 103:
268 index = index + ilen;
269 break;
270 case 104:
271 index = index + 2;
272 break;
273 case 105:
274 (void)fprintf(stdout,"%x:%x:%x:%x:%x:%x\t",
275 src[0],src[1],src[2],src[3],src[4],src[5]);
276 for (i = 0; i < ilen; i++) {
277 (void)fprintf(stdout, "%c",pkt[index+i]);
278 }
279 index = index + ilen;
280 (void)fprintf(stdout, "\n");
281 break;
282 case 106:
283 index = index + ilen;
284 break;
285 };
286 } else {
287 index = index + ilen;
288 };
289 }
290 itype = mopGetShort(pkt,&index);
291 }
292
293 }
294
295