xref: /netbsd/usr.sbin/mopd/mopd/mopd.c (revision bf9ec67e)
1 /*	$NetBSD: mopd.c,v 1.8 2001/02/19 23:22:45 cgd 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.8 2001/02/19 23:22:45 cgd 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 
76 int
77 main(argc, argv)
78 	int     argc;
79 	char  **argv;
80 {
81 	int	c, pid;
82 	char   *interface;
83 
84 	extern char version[];
85 
86 	while ((c = getopt(argc, argv, "34adfv")) != -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 'v':
104 				VersionFlag++;
105 				break;
106 			default:
107 				Usage();
108 				/* NOTREACHED */
109 		}
110 
111 	if (VersionFlag) {
112 		fprintf(stdout,"%s: version %s\n", getprogname(), version);
113 		exit(0);
114 	}
115 
116 	interface = argv[optind++];
117 
118 	if ((AllFlag && interface) ||
119 	    (!AllFlag && interface == 0) ||
120 	    (argc > optind) ||
121 	    (Not3Flag && Not4Flag))
122 		Usage();
123 
124 	/* All error reporting is done through syslogs. */
125 	openlog("mopd", LOG_PID, LOG_DAEMON);
126 
127 	if ((!ForegroundFlag) && DebugFlag)
128 		fprintf(stdout,
129 		    "%s: not running as daemon, -d given.\n", getprogname());
130 
131 	if ((!ForegroundFlag) && (!DebugFlag)) {
132 		pid = fork();
133 		if (pid > 0)
134 			/* Parent exits, leaving child in background. */
135 			exit(0);
136 		else
137 			if (pid == -1) {
138 				syslog(LOG_ERR, "cannot fork");
139 				exit(0);
140 			}
141 
142 		/* Fade into the background */
143 		daemon(0, 0);
144 		pidfile(NULL);
145 	}
146 
147 	syslog(LOG_INFO, "%s %s started.", getprogname(), version);
148 
149 	if (AllFlag)
150  		deviceInitAll();
151 	else
152 		deviceInitOne(interface);
153 
154 	Loop();
155 	/* NOTREACHED */
156 	return (0);
157 }
158 
159 void
160 Usage()
161 {
162 	(void) fprintf(stderr, "usage: %s -a [ -d -f -v ] [ -3 | -4 ]\n",
163 	    getprogname());
164 	(void) fprintf(stderr, "       %s [ -d -f -v ] [ -3 | -4 ] interface\n",
165 	    getprogname());
166 	exit(1);
167 }
168 
169 /*
170  * Process incomming packages.
171  */
172 void
173 mopProcess(ii, pkt)
174 	struct if_info *ii;
175 	u_char *pkt;
176 {
177 	u_char	*dst, *src;
178 	u_short  ptype;
179 	int	 index, trans, len;
180 
181 	/* We don't known with transport, Guess! */
182 
183 	trans = mopGetTrans(pkt, 0);
184 
185 	/* Ok, return if we don't wan't this message */
186 
187 	if ((trans == TRANS_ETHER) && Not3Flag) return;
188 	if ((trans == TRANS_8023) && Not4Flag)	return;
189 
190 	index = 0;
191 	mopGetHeader(pkt, &index, &dst, &src, &ptype, &len, trans);
192 
193 	/*
194 	 * Ignore our own transmissions
195 	 *
196 	 */
197 	if (mopCmpEAddr(ii->eaddr,src) == 0)
198 		return;
199 
200 	switch(ptype) {
201 	case MOP_K_PROTO_DL:
202 		mopProcessDL(stdout, ii, pkt, &index, dst, src, trans, len);
203 		break;
204 	case MOP_K_PROTO_RC:
205 		mopProcessRC(stdout, ii, pkt, &index, dst, src, trans, len);
206 		break;
207 	default:
208 		break;
209 	}
210 }
211