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