1/*	$NetBSD: mopd.c,v 1.13 2009/11/17 18:58:07 drochner 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 *
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: mopd.c,v 1.13 2009/11/17 18:58:07 drochner Exp $");
30#endif
31
32/*
33 * mopd - MOP Dump/Load Daemon
34 *
35 * Usage:	mopd -a [ -d -f -v ] [ -3 | -4 ]
36 *		mopd [ -d -f -v ] [ -3 | -4 ] interface
37 */
38
39#include "os.h"
40#include "cmp.h"
41#include "common.h"
42#include "device.h"
43#include "dl.h"
44#include "get.h"
45#include "mopdef.h"
46#include "pf.h"
47#include "print.h"
48#include "process.h"
49#include "rc.h"
50
51#include <util.h>
52
53/*
54 * The list of all interfaces that are being listened to.
55 * "selects" on the descriptors in this list.
56 */
57struct if_info *iflist;
58
59__dead static void	Usage(void);
60void	mopProcess(struct if_info *, u_char *);
61
62int     AllFlag = 0;		/* listen on "all" interfaces */
63int     DebugFlag = 0;		/* print debugging messages   */
64int	ForegroundFlag = 0;	/* run in foreground          */
65int	VersionFlag = 0;	/* print version              */
66int	Not3Flag = 0;		/* Not MOP V3 messages.       */
67int	Not4Flag = 0;		/* Not MOP V4 messages.       */
68int	promisc = 1;		/* Need promisc mode    */
69const char *MopdDir = MOP_FILE_PATH;  /* Path to mop directory  */
70
71int
72main(int argc, char  **argv)
73{
74	int	c, pid;
75
76	extern char version[];
77
78	while ((c = getopt(argc, argv, "34adfs:v")) != -1) {
79		switch (c) {
80			case '3':
81				Not3Flag++;
82				break;
83			case '4':
84				Not4Flag++;
85				break;
86			case 'a':
87				AllFlag++;
88				break;
89			case 'd':
90				DebugFlag++;
91				break;
92			case 'f':
93				ForegroundFlag++;
94				break;
95			case 's':
96				MopdDir = optarg;
97				break;
98			case 'v':
99				VersionFlag++;
100				break;
101			default:
102				Usage();
103				/* NOTREACHED */
104		}
105	}
106	argc -= optind;
107	argv += optind;
108
109	if (VersionFlag) {
110		fprintf(stdout,"%s: version %s\n", getprogname(), version);
111		exit(0);
112	}
113
114	if ((AllFlag && argc != 0) || (!AllFlag && argc == 0) ||
115	    (Not3Flag && Not4Flag))
116		Usage();
117
118	/* All error reporting is done through syslogs. */
119	openlog("mopd", LOG_PID, LOG_DAEMON);
120
121	if ((!ForegroundFlag) && DebugFlag)
122		fprintf(stdout,
123		    "%s: not running as daemon, -d given.\n", getprogname());
124
125	if ((!ForegroundFlag) && (!DebugFlag)) {
126		pid = fork();
127		if (pid > 0)
128			/* Parent exits, leaving child in background. */
129			exit(0);
130		else
131			if (pid == -1) {
132				syslog(LOG_ERR, "cannot fork");
133				exit(0);
134			}
135
136		/* Fade into the background */
137		daemon(0, 0);
138		pidfile(NULL);
139	}
140
141	syslog(LOG_INFO, "%s %s started.", getprogname(), version);
142
143	if (AllFlag)
144 		deviceInitAll();
145	else {
146		while (argc--)
147			deviceInitOne(*argv++);
148	}
149
150	Loop();
151	/* NOTREACHED */
152	return (0);
153}
154
155static void
156Usage(void)
157{
158	(void) fprintf(stderr, "usage: %s -a [ -d -f -v ] [ -3 | -4 ]\n",
159	    getprogname());
160	(void) fprintf(stderr, "       %s [ -d -f -v ] [ -3 | -4 ]\n",
161	    getprogname());
162	(void) fprintf(stderr, "           interface [...]\n");
163	exit(1);
164}
165
166/*
167 * Process incomming packages.
168 */
169void
170mopProcess(struct if_info *ii, u_char *pkt)
171{
172	const u_char	*dst, *src;
173	u_short  ptype;
174	int	 idx, trans, len;
175
176	/* We don't known with transport, Guess! */
177
178	trans = mopGetTrans(pkt, 0);
179
180	/* Ok, return if we don't wan't this message */
181
182	if ((trans == TRANS_ETHER) && Not3Flag) return;
183	if ((trans == TRANS_8023) && Not4Flag)	return;
184
185	idx = 0;
186	mopGetHeader(pkt, &idx, &dst, &src, &ptype, &len, trans);
187
188	/*
189	 * Ignore our own transmissions
190	 *
191	 */
192	if (mopCmpEAddr(ii->eaddr,src) == 0)
193		return;
194
195	switch(ptype) {
196	case MOP_K_PROTO_DL:
197		mopProcessDL(stdout, ii, pkt, &idx, dst, src, trans, len);
198		break;
199	case MOP_K_PROTO_RC:
200		mopProcessRC(stdout, ii, pkt, &idx, dst, src, trans, len);
201		break;
202	default:
203		break;
204	}
205}
206