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