forked from mstpd/mstpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
353 lines (291 loc) · 8.63 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*****************************************************************************
Copyright (c) 2006 EMC Corporation.
Copyright (c) 2011 Factor-SPE
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
The full GNU General Public License is included in this distribution in the
file called LICENSE.
Authors: Srinivas Aji <[email protected]>
Authors: Vitalii Demianets <[email protected]>
******************************************************************************/
/* #define MISC_TEST_FUNCS */
#include <config.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include "epoll_loop.h"
#include "bridge_ctl.h"
#include "netif_utils.h"
#include "packet.h"
#include "log.h"
#include "mstp.h"
#include "ctl_socket_server.h"
#include "driver.h"
#include "bridge_track.h"
#define APP_NAME "mstpd"
static int print_to_syslog = 0;
int log_level = LOG_LEVEL_DEFAULT;
#ifdef MISC_TEST_FUNCS
static bool test_ports_trees_mesh(void);
#endif /* MISC_TEST_FUNCS */
volatile bool quit = false;
static void handle_signal(int sig)
{
quit = true;
}
static void handle_sigusr1(int sig)
{
log_level = LOG_LEVEL_DEBUG;
}
int signal_init(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_signal;
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sa.sa_handler = handle_sigusr1;
sigaction(SIGUSR1, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
return 0;
}
static int sanity_check(void)
{
bridge_identifier_t BridgeIdentifier;
mst_configuration_identifier_t MST_ConfigurationIdentifier;
TST(sizeof(BridgeIdentifier) == 8, -1);
TST(sizeof(BridgeIdentifier.u) == 8, -1);
TST(sizeof(BridgeIdentifier.s) == 8, -1);
TST(sizeof(BridgeIdentifier.s.priority) == 2, -1);
TST(sizeof(BridgeIdentifier.s.mac_address) == 6, -1);
TST(sizeof(MST_ConfigurationIdentifier) == 51, -1);
TST(sizeof(MST_ConfigurationIdentifier.a) == 51, -1);
TST(sizeof(MST_ConfigurationIdentifier.s) == 51, -1);
#ifdef HMAC_MDS_TEST_FUNCTIONS
TST(MD5TestSuite(), -1);
#endif /* HMAC_MDS_TEST_FUNCTIONS */
#ifdef MISC_TEST_FUNCS
TST(test_ports_trees_mesh(), -1);
#endif /* MISC_TEST_FUNCS */
INFO("Sanity checks succeeded");
return 0;
}
int main(int argc, char *argv[])
{
int c;
int daemonize = 1;
while((c = getopt(argc, argv, "Vdsv:")) != -1)
{
switch (c)
{
case 'd':
daemonize = 0;
break;
case 's':
print_to_syslog = 1;
break;
case 'v':
{
char *end;
long l;
l = strtoul(optarg, &end, 0);
if(*optarg == 0 || *end != 0 || l > LOG_LEVEL_MAX)
{
ERROR("Invalid loglevel %s", optarg);
exit(1);
}
log_level = l;
break;
}
case 'V':
printf(PACKAGE_VERSION "\n");
return 0;
default:
return -1;
}
}
if(daemonize)
print_to_syslog = 1;
if(print_to_syslog)
openlog(APP_NAME, LOG_PID, LOG_DAEMON);
INFO("Version - " PACKAGE_VERSION "\n");
if (sanity_check() < 0)
return EXIT_FAILURE;
if(daemonize)
{
FILE *f = fopen(MSTPD_PID_FILE, "w");
if(!f)
{
ERROR("can't open "MSTPD_PID_FILE);
return -1;
}
if(daemon(0, 0))
{
ERROR("can't daemonize");
fclose(f);
return -1;
}
fprintf(f, "%d", getpid());
fclose(f);
}
TST(signal_init() == 0, -1);
TST(driver_mstp_init() == 0, -1);
TST(init_epoll() == 0, -1);
TST(ctl_socket_init() == 0, -1);
TST(packet_sock_init() == 0, -1);
TST(netsock_init() == 0, -1);
TST(init_bridge_ops() == 0, -1);
c = epoll_main_loop(&quit);
bridge_track_fini();
ctl_socket_cleanup();
driver_mstp_fini();
return c;
}
/*********************** Logging *********************/
#include <stdarg.h>
#include <time.h>
void vDprintf(int level, const char *fmt, va_list ap)
{
if(level > log_level)
return;
if(!print_to_syslog)
{
char logbuf[256];
logbuf[255] = 0;
time_t clock;
struct tm *local_tm;
time(&clock);
local_tm = localtime(&clock);
int l = strftime(logbuf, sizeof(logbuf) - 1, "%F %T ", local_tm);
vsnprintf(logbuf + l, sizeof(logbuf) - l - 1, fmt, ap);
printf("%s\n", logbuf);
}
else
{
vsyslog((level <= LOG_LEVEL_INFO) ? LOG_INFO : LOG_DEBUG, fmt, ap);
}
}
void Dprintf(int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vDprintf(level, fmt, ap);
va_end(ap);
}
/*********************** Testing *********************/
#ifdef MISC_TEST_FUNCS
#include <string.h>
#include <asm/byteorder.h>
static void printout_mesh(bridge_t *br)
{
tree_t *tree;
port_t *prt;
per_tree_port_t *ptp;
printf("Ports:\n");
list_for_each_entry(prt, &br->ports, br_list)
{
printf(" %s(%03hX)", prt->sysdeps.name, __be16_to_cpu(prt->port_number));
list_for_each_entry(ptp, &prt->trees, port_list)
printf("->%03hX", __be16_to_cpu(ptp->MSTID));
printf("\n");
}
printf("\nTrees:\n");
list_for_each_entry(tree, &br->trees, bridge_list)
{
printf(" %03hX", __be16_to_cpu(tree->MSTID));
list_for_each_entry(ptp, &tree->ports, tree_list)
printf("->%s", ptp->port->sysdeps.name);
printf("\n");
}
printf("\n");
}
static bool test_ports_trees_mesh(void)
{
bridge_t *br = calloc(1, sizeof(*br));
if(!br)
return false;
strcpy(br->sysdeps.name, "BR_TEST");
br->sysdeps.macaddr[5] = 0xED;
if(!MSTP_IN_bridge_create(br, br->sysdeps.macaddr))
{
free(br);
return false;
}
port_t *prt[5];
int i;
for(i = 0; i < 5; ++i)
{
if(!(prt[i] = calloc(1, sizeof(port_t))))
return false;
prt[i]->bridge = br;
}
if(!MSTP_IN_create_msti(br, 0xF91))
{
error_exit:
MSTP_IN_delete_bridge(br);
free(br);
return false;
}
if(!MSTP_IN_create_msti(br, 0xE10))
goto error_exit;
strcpy(prt[0]->sysdeps.name, "PRT_10C");
if(!MSTP_IN_port_create_and_add_tail(prt[0], 0x10C))
goto error_exit;
strcpy(prt[1]->sysdeps.name, "PRT_001");
if(!MSTP_IN_port_create_and_add_tail(prt[1], 0x001))
goto error_exit;
strcpy(prt[2]->sysdeps.name, "PRT_C01");
if(!MSTP_IN_port_create_and_add_tail(prt[2], 0xC01))
goto error_exit;
if(!MSTP_IN_create_msti(br, 0xE12))
goto error_exit;
if(!MSTP_IN_create_msti(br, 0x001))
goto error_exit;
strcpy(prt[3]->sysdeps.name, "PRT_002");
if(!MSTP_IN_port_create_and_add_tail(prt[3], 0x002))
goto error_exit;
strcpy(prt[4]->sysdeps.name, "PRT_003");
if(!MSTP_IN_port_create_and_add_tail(prt[4], 0x003))
goto error_exit;
if(!MSTP_IN_create_msti(br, 0x005))
goto error_exit;
printout_mesh(br);
MSTP_IN_delete_port(prt[1]);
if(!MSTP_IN_delete_msti(br, 0xE12))
goto error_exit;
MSTP_IN_delete_port(prt[3]);
if(!MSTP_IN_delete_msti(br, 0x005))
goto error_exit;
printout_mesh(br);
if(!MSTP_IN_create_msti(br, 0x102))
goto error_exit;
strcpy(prt[1]->sysdeps.name, "PRT_504");
if(!MSTP_IN_port_create_and_add_tail(prt[1], 0x504))
goto error_exit;
if(!MSTP_IN_create_msti(br, 0x105))
goto error_exit;
strcpy(prt[3]->sysdeps.name, "PRT_777");
if(!MSTP_IN_port_create_and_add_tail(prt[3], 0x777))
goto error_exit;
printout_mesh(br);
MSTP_IN_delete_bridge(br);
free(br);
return true;
}
#endif /* MISC_TEST_FUNCS */