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