1/*	$OpenBSD: moptrace.c,v 1.14 2023/03/08 04:43:14 guenther 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/*
28 * moptrace - MOP Trace Utility
29 *
30 * Usage:	moptrace [-3 | -4] [-ad] interface
31 */
32
33#include "os.h"
34#include "common/common.h"
35#include "common/mopdef.h"
36#include "common/device.h"
37#include "common/print.h"
38#include "common/pf.h"
39#include "common/dl.h"
40#include "common/rc.h"
41#include "common/get.h"
42
43/*
44 * The list of all interfaces that are being listened to.
45 * "selects" on the descriptors in this list.
46 */
47struct if_info *iflist;
48
49void   Usage(void);
50void   mopProcess(struct if_info *, u_char *);
51
52int     AllFlag = 0;		/* listen on "all" interfaces  */
53int     DebugFlag = 0;		/* print debugging messages    */
54int	Not3Flag = 0;		/* Ignore MOP V3 messages      */
55int	Not4Flag = 0;		/* Ignore MOP V4 messages      */
56int	promisc = 1;		/* Need promisc mode           */
57extern char *__progname;
58
59int
60main(int argc, char *argv[])
61{
62	int     op;
63	char   *interface;
64
65	/* All error reporting is done through syslogs. */
66	openlog(__progname, LOG_PID | LOG_CONS, LOG_DAEMON);
67
68	opterr = 0;
69	while ((op = getopt(argc, argv, "34ad")) != -1) {
70		switch (op) {
71		case '3':
72			Not3Flag = 1;
73			break;
74		case '4':
75			Not4Flag = 1;
76			break;
77		case 'a':
78			AllFlag = 1;
79			break;
80		case 'd':
81			DebugFlag++;
82			break;
83		default:
84			Usage();
85			/* NOTREACHED */
86		}
87	}
88
89	interface = argv[optind++];
90
91	if ((AllFlag && interface) ||
92	    (!AllFlag && interface == 0) ||
93	    (Not3Flag && Not4Flag))
94		Usage();
95
96	if (AllFlag)
97 		deviceInitAll();
98	else
99		deviceInitOne(interface);
100
101	Loop();
102	/* NOTREACHED */
103}
104
105void
106Usage()
107{
108	fprintf(stderr, "usage: %s [-3 | -4] [-ad] interface\n", __progname);
109	exit(1);
110}
111
112/*
113 * Process incoming packages.
114 */
115void
116mopProcess(struct if_info *ii, u_char *pkt)
117{
118	int	 trans;
119
120	/* We don't known which transport, Guess! */
121
122	trans = mopGetTrans(pkt, 0);
123
124	/* Ok, return if we don't want this message */
125
126	if ((trans == TRANS_ETHER) && Not3Flag) return;
127	if ((trans == TRANS_8023) && Not4Flag)	return;
128
129	fprintf(stdout, "Interface    : %s", ii->if_name);
130	mopPrintHeader(stdout, pkt, trans);
131	mopPrintMopHeader(stdout, pkt, trans);
132
133	mopDumpDL(stdout, pkt, trans);
134	mopDumpRC(stdout, pkt, trans);
135
136	fprintf(stdout, "\n");
137	fflush(stdout);
138}
139