bmpStation is server of BGP monitoring protocol.
bmpStation start making
@@ -0,0 +1,572 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | + | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | +#if !defined(BMP_STATION_H__) | |
29 | +# include <bmpStation.h> | |
30 | +#endif | |
31 | +#if !defined(BMP_STATION_INT_H__) | |
32 | +# include <bmpStationInt.h> | |
33 | +#endif | |
34 | +#if !defined(SELFLIB_H__) | |
35 | +# include <selfLib.h> | |
36 | +#endif | |
37 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
38 | +# include <sys/types.h> | |
39 | +#endif | |
40 | +#if !defined(_SYS_SELECT_H) && !defined(_SYS_SELECT_H_) | |
41 | +# include <sys/select.h> | |
42 | +#endif | |
43 | +#if !defined(_SYS_WAIT_H) && !defined(_SYS_WAIT_H_) | |
44 | +# include <sys/wait.h> | |
45 | +#endif | |
46 | +#if !defined(_SYS_SOCKET_H) && !defined(_SYS_SOCKET_H_) | |
47 | +# include <sys/socket.h> | |
48 | +#endif | |
49 | +#if !defined(_STDIO_H) && !defined(_STDIO_H_) | |
50 | +# include <stdio.h> | |
51 | +#endif | |
52 | +#if !defined(_UNISTD_H) && !defined(_UNISTD_H_) | |
53 | +# include <unistd.h> | |
54 | +#endif | |
55 | +#if !defined(_STDLIB_H) && !defined(_STDLIB_H_) | |
56 | +# include <stdlib.h> | |
57 | +#endif | |
58 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
59 | +# include <string.h> | |
60 | +#endif | |
61 | +#if !defined(_SIGNAL_H) && !defined(_SIGNAL_H_) | |
62 | +# include <signal.h> | |
63 | +#endif | |
64 | +#if !defined(_ERRNO_H) && !defined(_ERRNO_H_) | |
65 | +# include <errno.h> | |
66 | +#endif | |
67 | + | |
68 | +/******************************************************************************* | |
69 | + * variables ******************************************************************/ | |
70 | +static int tsoc; /* TCP Listen socket ***************************/ | |
71 | + | |
72 | +/******************************************************************************* | |
73 | + * functions ******************************************************************/ | |
74 | +static int opt_proc (int, char **); | |
75 | +static void usage (); | |
76 | +static void hangup (int); | |
77 | +static void wait_child (int); | |
78 | + | |
79 | +/*+==========================================================================+** | |
80 | + *| hang up | |
81 | + *+==========================================================================+*/ | |
82 | +static void hangup (int pid) | |
83 | +{ | |
84 | + close (tsoc); | |
85 | + | |
86 | + exit (0); | |
87 | +} | |
88 | + | |
89 | + | |
90 | +/*%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%** | |
91 | + *: main process :** | |
92 | + *%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,%*/ | |
93 | +int main (int argc, char **argv) | |
94 | +{ | |
95 | + /***************************************************************************** | |
96 | + * Variables ****************************************************************/ | |
97 | + pid_t pid; | |
98 | + /* select */ | |
99 | + struct timeval itime, utime; | |
100 | + int fdw , ret; | |
101 | + fd_set iset , uset ; /* iset is initialize, uset for select */ | |
102 | + /* socket */ | |
103 | + int asoc; /* accept socket */ | |
104 | + /***************************************************************************** | |
105 | + * option process ***********************************************************/ | |
106 | + if (!opt_proc (argc, argv)) {goto JUMP_OPT;} | |
107 | + | |
108 | + if (FLGISSET(lastParam.flags, OPTFLAGDAEMONIZE)) { | |
109 | + if (daemon (0, 0)<0) { | |
110 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
111 | + writeLog (syslogFile, prgName, -1, "Fault daemonize"); | |
112 | + } | |
113 | + goto JUMP_OPT; | |
114 | + } | |
115 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
116 | + writeLog (syslogFile, prgName, -1, "daemonize"); | |
117 | + } | |
118 | + } | |
119 | + | |
120 | + /*=Signal setup=============================================================*/ | |
121 | + if (signal (SIGHUP , hangup )==SIG_ERR) { | |
122 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) {writeLog (syslogFile, prgName, -1, "Failure signal setup: SIGHUP.");} | |
123 | + goto JUMP_OPT; | |
124 | + } | |
125 | + else if (signal (SIGINT , hangup )==SIG_ERR) { | |
126 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) {writeLog (syslogFile, prgName, -1, "Failure signal setup: SIGINT.");} | |
127 | + goto JUMP_OPT; | |
128 | + } | |
129 | + else if (signal (SIGTERM, hangup )==SIG_ERR) { | |
130 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) {writeLog (syslogFile, prgName, -1, "Failure signal setup: SIGTERM.");} | |
131 | + goto JUMP_OPT; | |
132 | + } | |
133 | + else if (signal (SIGCHLD, wait_child)==SIG_ERR) { | |
134 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) {writeLog (syslogFile, prgName, -1, "Failure signal setup: SIGCHLD.");} | |
135 | + goto JUMP_OPT; | |
136 | + } | |
137 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
138 | + writeLog (syslogFile, prgName, -1, "signal setup"); | |
139 | + } | |
140 | + /***************************************************************************** | |
141 | + * Socket Listen ************************************************************/ | |
142 | + /*=Socket open==============================================================*/ | |
143 | + if ((tsoc=lsocket (lastParam.host, lastParam.ports, SOCK_STREAM, 0))<0) { | |
144 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
145 | + writeLog (syslogFile, prgName, -1, "fault listen socket open."); | |
146 | + } | |
147 | + goto JUMP_OPT; | |
148 | + } | |
149 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
150 | + writeLog (syslogFile, prgName, -1, "listen socket open."); | |
151 | + } | |
152 | + /*=Select setup=============================================================*/ | |
153 | + fdw = 0; | |
154 | + FD_ZERO(&iset); | |
155 | + FD_SET(tsoc, &iset); fdw = tsoc; | |
156 | + fdw++; | |
157 | + (void) memset (&itime, 0, sizeof (struct timeval)); | |
158 | + itime = d2tval (10); | |
159 | + | |
160 | + /*=TCP Listen===============================================================*/ | |
161 | + while (1) { | |
162 | + utime = itime; uset = iset; | |
163 | + ret = select (fdw, &uset, NULL, NULL, &utime); | |
164 | + if (ret< 0) { | |
165 | + switch (errno) { | |
166 | + case EBADF: break; | |
167 | + case EINTR: continue; break; | |
168 | + case EINVAL: break; | |
169 | + case ENOMEM: break; | |
170 | + } | |
171 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
172 | + writeLog (syslogFile, prgName, -1, "select error."); | |
173 | + } | |
174 | + goto ERR_CLOSE_SOC; | |
175 | + } | |
176 | + else if (ret==0) {continue ;} | |
177 | + else if (FD_ISSET(tsoc, &uset)) { | |
178 | + /*=main BMP proc========================================================*/ | |
179 | + if ((asoc=accept (tsoc, NULL, NULL))<0) { | |
180 | + switch (errno) { | |
181 | + case EMFILE: | |
182 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
183 | + writeLog (syslogFile, prgName, -1, "limit tcp session."); | |
184 | + } | |
185 | + case EINTR: | |
186 | + continue; | |
187 | + break; | |
188 | + case ENOTSOCK: case EOPNOTSUPP: case EPROTO: case EINVAL: | |
189 | + case EFAULT: case EBADF: case EWOULDBLOCK: | |
190 | + case ECONNABORTED: case ENOBUFS: case ENOMEM: | |
191 | + default: | |
192 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
193 | + writeLog (syslogFile, prgName, -1, "Listen socket is error."); | |
194 | + } | |
195 | + goto ERR_CLOSE_SOC; | |
196 | + } | |
197 | + } | |
198 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
199 | + writeLog (syslogFile, prgName, asoc, "TCP session is open."); | |
200 | + } | |
201 | + | |
202 | + pid = fork (); | |
203 | + if (pid<0) { | |
204 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
205 | + writeLog (syslogFile, prgName, asoc, "Fault new process(fork)."); | |
206 | + } | |
207 | + close (asoc); | |
208 | + goto ERR_CLOSE_SOC; | |
209 | + } | |
210 | + else if (pid==0) { | |
211 | + /* main process */ | |
212 | + if (!bmpRead (asoc)) { | |
213 | + close (asoc); | |
214 | + _exit(1); | |
215 | + } | |
216 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
217 | + writeLog (syslogFile, prgName, asoc, "Close session."); | |
218 | + } | |
219 | + close (asoc); | |
220 | + _exit (0); | |
221 | + } | |
222 | + close (asoc); | |
223 | + } | |
224 | + } | |
225 | + /*=Finish Process===========================================================*/ | |
226 | + close (tsoc); | |
227 | + | |
228 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
229 | + writeLog (syslogFile, prgName, -1, "Close processes."); | |
230 | + } | |
231 | + | |
232 | + return 0; | |
233 | + | |
234 | + ERR_CLOSE_SOC: | |
235 | + close (tsoc); | |
236 | + JUMP_OPT: | |
237 | + return 1; | |
238 | +} | |
239 | + | |
240 | +/******************************************************************************* | |
241 | + * wait child *****************************************************************/ | |
242 | +static void wait_child (int pid) | |
243 | +{ | |
244 | + pid_t _cpid; | |
245 | +#if 0 | |
246 | + int id; | |
247 | +#endif | |
248 | + | |
249 | + _cpid = wait (NULL); | |
250 | +#if 0 | |
251 | + if (FLGISSET(loglevel, LOGLVLSTATES2)) { | |
252 | + if (id<0) {/* error proc */} | |
253 | + else {/* error proc */} | |
254 | + } | |
255 | +#endif | |
256 | + while (SIG_ERR==signal (SIGCHLD, wait_child)) { | |
257 | + /* error proc */ | |
258 | + } | |
259 | + | |
260 | +} | |
261 | + | |
262 | +/******************************************************************************* | |
263 | + * usage **********************************************************************/ | |
264 | +static void usage () | |
265 | +{ | |
266 | + char spaPad[MAXPATHLEN]; /* space padding */ | |
267 | + (void) memset (spaPad, 0 , sizeof (spaPad)); | |
268 | + (void) memset (spaPad, ' ', (strlen (prgName)<sizeof (spaPad)?strlen (prgName):sizeof (spaPad)-1)); | |
269 | + /*=Usage main===============================================================*/ | |
270 | + fprintf (stdout, "%s [\033[1m-H\033[0m] [\033[1m-d\033[0m|\033[1m-D\033[0m] ", prgName); | |
271 | + fprintf (stdout, " [\033[1m-c\033[0m|\033[1m-C\033[0m] [\033[1m-e\033[0m|\033[1m-E\033[0m] [\033[1m-n\033[0m|\033[1m-N \033[4mListen address\033[0m\033[0m]\n"); | |
272 | + fprintf (stdout, "%s [\033[1m-P \033[4mListen Port\033[0m\033[0m] [\033[1m-W \033[4mWork dirctory\033[0m\033[0m]", spaPad); | |
273 | + fprintf (stdout, " [\033[1m-A \033[4mAccept TCP Number\033[0m\033[0m]\n"); | |
274 | + fprintf (stdout, "%s -H : help(this display)\n", spaPad); | |
275 | + fprintf (stdout, "%s -[D |d]: (set|unset) debug mode\n", spaPad); | |
276 | + fprintf (stdout, "%s -[C |c]: (set|unset) debug mode\n", spaPad); | |
277 | + fprintf (stdout, "%s -[E |e]: (set|unset) damonized mode\n", spaPad); | |
278 | + fprintf (stdout, "%s -[N \033[4mListen address\033[0m\033[0m|n]: (set|unset) Listen address\n", spaPad); | |
279 | + fprintf (stdout, "%s -[P \033[4mListen Port\033[0m\033[0m ]: set Listen Port\n", spaPad); | |
280 | + fprintf (stdout, "%s -W \033[4mWork Directory\033[0m\033[0m : set Work directory\n", spaPad); | |
281 | + fprintf (stdout, "%s -A \033[4mAccept TCP Number\033[0m\033[0m : set Accept TCP Number\n", spaPad); | |
282 | + | |
283 | + exit (1); | |
284 | +} | |
285 | + | |
286 | +/******************************************************************************* | |
287 | + * option processes ***********************************************************/ | |
288 | +static int opt_proc (int argc, char **argv) | |
289 | +{ | |
290 | + int ch, rtn; | |
291 | + char uhdir[MAXPATHLEN]; /* user home directory */ | |
292 | + char curdir[MAXPATHLEN]; /* current directory */ | |
293 | + char *ppname; | |
294 | + mode_t wdirmode; | |
295 | + | |
296 | + ppname = prgName = *argv; | |
297 | + while ((ppname=strstr (ppname, "/"))!=NULL) { | |
298 | + ppname++; | |
299 | + if (prgName == (ppname-1)) {prgName = ppname;} | |
300 | + else if (*(ppname - 2) != 92) {prgName = ppname;} | |
301 | + } | |
302 | + /***************************************************************************** | |
303 | + * initialize ***************************************************************/ | |
304 | + wdirmode = 0755; | |
305 | + (void) memset ((char *)uhdir , 0, sizeof (uhdir )); | |
306 | + (void) memset ((char *)curdir , 0, sizeof (curdir )); | |
307 | + (void) memset ((char *)&defParam , 0, sizeof (defParam )); | |
308 | + (void) memset ((char *)&cfgParam , 0, sizeof (cfgParam )); | |
309 | + (void) memset ((char *)&argParam , 0, sizeof (argParam )); | |
310 | + (void) memset ((char *)&lastParam, 0, sizeof (lastParam)); | |
311 | + ruid = getuid (); /* real user */ | |
312 | + euid = geteuid (); /* exec user */ | |
313 | + if (getcwd (curdir, sizeof (curdir))==NULL) { | |
314 | + fprintf (stderr, "Fault current dir\n"); | |
315 | + return 0; | |
316 | + } | |
317 | + | |
318 | + /***************************************************************************** | |
319 | + * set default **************************************************************/ | |
320 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGLPORT); | |
321 | + strncpy (defParam.ports, DEFLPORT, sizeof (((optionParam *)NULL)->ports)-1); | |
322 | + snprintf (defParam.ports, sizeof (((optionParam *)NULL)->ports)-1, "%s", DEFLPORT); | |
323 | + | |
324 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGLHOST); | |
325 | + (void) memset (defParam.host, 0, sizeof (((optionParam *)NULL)->host)); | |
326 | + snprintf (defParam.host, sizeof (((optionParam *)NULL)->host)-1, "%s", DEFLHOST); | |
327 | + | |
328 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGDEBUG); FLGCLR(defParam.flags, OPTFLAGDEBUG); | |
329 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGCHROOT); FLGCLR(defParam.flags, OPTFLAGCHROOT); | |
330 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGTCPACCEPTNUM); | |
331 | + defParam.acceptNum = DEFTCPACCEPTNUM; | |
332 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGLOGLEVEL); | |
333 | + defParam.loglevel = DEFLOGLVL; | |
334 | + | |
335 | + GetHomeDir (ruid, uhdir, sizeof (uhdir)); | |
336 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGWORKDIR); | |
337 | + snprintf (defParam.workdir, sizeof (((optionParam *)NULL)->workdir)-1, "%s/%s", uhdir, DEFWORKDIR); | |
338 | + | |
339 | + | |
340 | + /***************************************************************************** | |
341 | + * argument process *********************************************************/ | |
342 | + while ((ch = getopt (argc, argv, "A:cCdDeEHl:nN:P:uU:W:"))!=-1) { | |
343 | + switch (ch) { | |
344 | + case 'l': /* set log level */ | |
345 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGLOGLEVEL)) break; | |
346 | + FLGSET(defParam.enableConfigFlag, OPTENABLECFLAGLOGLEVEL); | |
347 | + defParam.loglevel = atoi (optarg); | |
348 | + break; | |
349 | + case 'D': /* set Debug */ | |
350 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGDEBUG)) break; | |
351 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGDEBUG); | |
352 | + FLGSET(argParam.flags , OPTFLAGDEBUG); | |
353 | + break; | |
354 | + case 'd': /* unset Debug */ | |
355 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGDEBUG)) break; | |
356 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGDEBUG); | |
357 | + FLGCLR(argParam.flags , OPTFLAGDEBUG); | |
358 | + break; | |
359 | + case 'C': /* set Chroot */ | |
360 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGCHROOT)) break; | |
361 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGCHROOT); | |
362 | + FLGSET(argParam.flags , OPTFLAGCHROOT); | |
363 | + break; | |
364 | + case 'c': /* unset Chroot */ | |
365 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGCHROOT)) break; | |
366 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGCHROOT); | |
367 | + FLGCLR(argParam.flags , OPTFLAGCHROOT); | |
368 | + break; | |
369 | + case 'N': /* set Hostname */ | |
370 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGLHOST)) break; | |
371 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGLHOST); | |
372 | + strncpy (argParam.host, optarg, sizeof (((optionParam *)NULL)->host)-1); | |
373 | + break; | |
374 | + case 'n': /* unset Hostname */ | |
375 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGLHOST)) break; | |
376 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGLHOST); | |
377 | + (void) memset (argParam.host, 0, sizeof (((optionParam *)NULL)->host)); | |
378 | + break; | |
379 | + case 'P': /* set Service(Port Number) */ | |
380 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGLPORT)) break; | |
381 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGLPORT); | |
382 | + strncpy (argParam.ports, optarg, sizeof (((optionParam *)NULL)->ports)-1); | |
383 | + break; | |
384 | + case 'W': /* set workdir */ | |
385 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGWORKDIR)) break; | |
386 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGWORKDIR); | |
387 | + if (*optarg == '/') { | |
388 | + strncpy (argParam.workdir, optarg, sizeof (((optionParam *)NULL)->workdir)-1); | |
389 | + } else { | |
390 | + snprintf (argParam.workdir, sizeof (((optionParam *)NULL)->workdir)-1, "%s/%s", curdir, optarg); | |
391 | + } | |
392 | + break; | |
393 | + case 'E': /* set daemonize */ | |
394 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE)) break; | |
395 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE); | |
396 | + FLGSET(argParam.flags , OPTFLAGDAEMONIZE); | |
397 | + break; | |
398 | + case 'e': /* unset daemonize */ | |
399 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE)) break; | |
400 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE); | |
401 | + FLGCLR(argParam.flags , OPTFLAGDAEMONIZE); | |
402 | + break; | |
403 | + case 'A': /* set TCP Accept Number */ | |
404 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGTCPACCEPTNUM)) break; | |
405 | + FLGSET(argParam.enableConfigFlag, OPTENABLECFLAGTCPACCEPTNUM); | |
406 | + argParam.acceptNum = atoi (optarg); | |
407 | + break; | |
408 | + case 'H': /* Help */ | |
409 | + default: | |
410 | + usage (); | |
411 | + return 0; | |
412 | + break; | |
413 | + } | |
414 | + } | |
415 | + argc -= optind; | |
416 | + argv += optind; | |
417 | + | |
418 | + /***************************************************************************** | |
419 | + * Last Option **************************************************************/ | |
420 | + /*=debug Option=*/ | |
421 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGDEBUG)) { | |
422 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGDEBUG); | |
423 | + lastParam.flags |= argParam.flags & OPTFLAGDEBUG; | |
424 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGDEBUG)) { | |
425 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGDEBUG); | |
426 | + lastParam.flags |= cfgParam.flags & OPTFLAGDEBUG; | |
427 | + } else { | |
428 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGDEBUG; | |
429 | + lastParam.flags |= defParam.flags & OPTFLAGDEBUG; | |
430 | + } | |
431 | + /*=chroot Option=*/ | |
432 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGCHROOT)) { | |
433 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGCHROOT); | |
434 | + lastParam.flags |= argParam.flags & OPTFLAGCHROOT; | |
435 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGCHROOT)) { | |
436 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGCHROOT); | |
437 | + lastParam.flags |= cfgParam.flags & OPTFLAGCHROOT; | |
438 | + } else { | |
439 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGCHROOT; | |
440 | + lastParam.flags |= defParam.flags & OPTFLAGCHROOT; | |
441 | + } | |
442 | + /*=daemonize Option=*/ | |
443 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE)) { | |
444 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE); | |
445 | + lastParam.flags |= argParam.flags & OPTFLAGDAEMONIZE; | |
446 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE)) { | |
447 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGDAEMONIZE); | |
448 | + lastParam.flags |= cfgParam.flags & OPTFLAGDAEMONIZE; | |
449 | + } else { | |
450 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGDAEMONIZE; | |
451 | + lastParam.flags |= defParam.flags & OPTFLAGDAEMONIZE; | |
452 | + } | |
453 | + /*=workdir Option=*/ | |
454 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGWORKDIR)) { | |
455 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGWORKDIR); | |
456 | + strncpy (lastParam.workdir, argParam.workdir, sizeof (((optionParam *)NULL)->workdir)-1); | |
457 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGWORKDIR)) { | |
458 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGWORKDIR); | |
459 | + strncpy (lastParam.workdir, cfgParam.workdir, sizeof (((optionParam *)NULL)->workdir)-1); | |
460 | + } else { | |
461 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGWORKDIR; | |
462 | + strncpy (lastParam.workdir, defParam.workdir, sizeof (((optionParam *)NULL)->workdir)-1); | |
463 | + } | |
464 | + /*=host Option=*/ | |
465 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGLHOST)) { | |
466 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGLHOST); | |
467 | + strncpy (lastParam.host, argParam.host, sizeof (((optionParam *)NULL)->host)-1); | |
468 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGLHOST)) { | |
469 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGLHOST); | |
470 | + strncpy (lastParam.host, cfgParam.host, sizeof (((optionParam *)NULL)->host)-1); | |
471 | + } else { | |
472 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGLHOST; | |
473 | + strncpy (lastParam.host, defParam.host, sizeof (((optionParam *)NULL)->host)-1); | |
474 | + } | |
475 | + /*=port Option=*/ | |
476 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGLPORT)) { | |
477 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGLPORT); | |
478 | + strncpy (lastParam.ports, argParam.ports, sizeof (((optionParam *)NULL)->ports)-1); | |
479 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGLPORT)) { | |
480 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGLPORT); | |
481 | + strncpy (lastParam.ports, cfgParam.ports, sizeof (((optionParam *)NULL)->ports)-1); | |
482 | + } else { | |
483 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGLPORT; | |
484 | + strncpy (lastParam.ports, defParam.ports, sizeof (((optionParam *)NULL)->ports)-1); | |
485 | + } | |
486 | + /*=accept number Option=*/ | |
487 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGTCPACCEPTNUM)) { | |
488 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGTCPACCEPTNUM); | |
489 | + lastParam.acceptNum = argParam.acceptNum; | |
490 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGLPORT)) { | |
491 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGTCPACCEPTNUM); | |
492 | + lastParam.acceptNum = cfgParam.acceptNum; | |
493 | + } else { | |
494 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGTCPACCEPTNUM; | |
495 | + lastParam.acceptNum = defParam.acceptNum; | |
496 | + } | |
497 | + /*=log level Option=*/ | |
498 | + if (FLGISSET(argParam.enableConfigFlag, OPTENABLECFLAGLOGLEVEL)) { | |
499 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGLOGLEVEL); | |
500 | + lastParam.loglevel = argParam.loglevel; | |
501 | + } else if (FLGISSET(cfgParam.enableConfigFlag, OPTENABLECFLAGLOGLEVEL)) { | |
502 | + FLGSET(lastParam.enableConfigFlag, OPTENABLECFLAGLOGLEVEL); | |
503 | + lastParam.loglevel = cfgParam.loglevel; | |
504 | + } else { | |
505 | + lastParam.enableConfigFlag |= defParam.enableConfigFlag & OPTENABLECFLAGLOGLEVEL; | |
506 | + lastParam.loglevel = defParam.loglevel; | |
507 | + } | |
508 | + | |
509 | + /***************************************************************************** | |
510 | + * Setup ********************************************************************/ | |
511 | + /*=Chuser===================================================================*/ | |
512 | + /*=Create workdir===========================================================*/ | |
513 | + if ((rtn=chkdir (lastParam.workdir, wdirmode))<0) { | |
514 | + fprintf (stderr, "Can't access the workdir: %s", lastParam.workdir); | |
515 | + return 0; | |
516 | + } | |
517 | + else if (rtn==1) {} | |
518 | + else if ((rtn=mkdirs (lastParam.workdir, wdirmode))<0) { | |
519 | + fprintf (stderr, "Can't create the workdir: %s", lastParam.workdir); | |
520 | + return 0; | |
521 | + } | |
522 | + else if (rtn==0) { | |
523 | + fprintf (stderr, "Can't create the workdir: %s", lastParam.workdir); | |
524 | + return 0; | |
525 | + } | |
526 | + /*=Chroot workdir===========================================================*/ | |
527 | + if (FLGISSET(lastParam.flags, OPTFLAGCHROOT)) { | |
528 | + if (chroot (lastParam.workdir)<0) { | |
529 | + fprintf (stderr, "Can't chroot to workdir: %s", lastParam.workdir); | |
530 | + return 0; | |
531 | + } | |
532 | + else if (chdir ("/")<0) { | |
533 | + fprintf (stderr, "Can't change to workdir: %s", lastParam.workdir); | |
534 | + return 0; | |
535 | + } | |
536 | + snprintf (lastParam.workdir, sizeof (((optionParam *)NULL)->workdir)-1, "/"); | |
537 | + } | |
538 | + else if (chdir (lastParam.workdir)<0) { | |
539 | + fprintf (stderr, "Can't change to workdir: %s", lastParam.workdir); | |
540 | + return 0; | |
541 | + } | |
542 | + /*=System log===============================================================*/ | |
543 | + (void) memset (syslogFile , 0, sizeof (syslogFile )); | |
544 | + (void) memset (statisticsFile, 0, sizeof (statisticsFile)); | |
545 | + (void) memset (peerEventLog , 0, sizeof (peerEventLog )); | |
546 | + snprintf (syslogFile , sizeof (syslogFile )-1, "%s/%s", lastParam.workdir, DEFSYSLOG ); | |
547 | + snprintf (statisticsFile, sizeof (statisticsFile)-1, "%s/%s", lastParam.workdir, DEFSTATISTICSLOG); | |
548 | + snprintf (peerEventLog , sizeof (peerEventLog )-1, "%s/%s", lastParam.workdir, DEFEVENTLOG ); | |
549 | + | |
550 | + if (FLGISSET(lastParam.flags, OPTFLAGDEBUG)) { | |
551 | + printf ("real user = %d\n", ruid); | |
552 | + printf ("exec user = %d\n", euid); | |
553 | + printf ("debug : %d\n", FLGISSET(lastParam.flags, OPTFLAGDEBUG)); | |
554 | + printf ("chroot : %d\n", FLGISSET(lastParam.flags, OPTFLAGCHROOT)); | |
555 | + printf ("daemon : %d\n", FLGISSET(lastParam.flags, OPTFLAGDAEMONIZE)); | |
556 | + printf ("host : %s\n", lastParam.host); | |
557 | + printf ("ports : %s\n", lastParam.ports); | |
558 | + printf ("accept#: %d\n", lastParam.acceptNum); | |
559 | + printf ("workdir: %s\n", lastParam.workdir); | |
560 | + printf ("log : %s\n", syslogFile); | |
561 | + printf ("stati : %s\n", statisticsFile); | |
562 | + printf ("event : %s\n", peerEventLog); | |
563 | + } | |
564 | + | |
565 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
566 | + writeLog (syslogFile, prgName, -1, "initialize"); | |
567 | + } | |
568 | + | |
569 | + return 1; | |
570 | +} | |
571 | + | |
572 | + |
@@ -0,0 +1,109 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | +#if !defined(BMP_STATION_INT_H__) | |
26 | +#define BMP_STATION_INT_H__ 1 | |
27 | +/******************************************************************************* | |
28 | + * include ********************************************************************/ | |
29 | +#if defined(HAVE_CONFIG_H) | |
30 | +# include <config.h> | |
31 | +#endif | |
32 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
33 | +# include <sys/types.h> | |
34 | +#endif | |
35 | +#if !defined(_SYS_PARAM_H) && !defined(_SYS_PARAM_H_) | |
36 | +# include <sys/param.h> | |
37 | +#endif | |
38 | +#if !defined(_NETDB_H) && !defined(_NETDB_H_) | |
39 | +# include <netdb.h> | |
40 | +#endif | |
41 | +/******************************************************************************* | |
42 | + * Define *********************************************************************/ | |
43 | +#define LOGSTRMAX 512 | |
44 | +#define DEFLHOST "" | |
45 | +#define DEFLPORT "11019" | |
46 | +#define DEFTCPACCEPTNUM 0 | |
47 | +#define DEFLOGLVL 0x8 | |
48 | +#define DEFWORKDIR ".bmpStation" | |
49 | +#define DEFSYSLOG "system.log" | |
50 | +#define DEFSTATISTICSLOG "statistics.log" | |
51 | +#define DEFEVENTLOG "peer_event.log" | |
52 | +/*#define DEFBGPMONLOG "%H-%L_bgp.log"*/ | |
53 | + | |
54 | +/******************************************************************************* | |
55 | + * Macro **********************************************************************/ | |
56 | +#define FLGSET(a,b) (a|=b) | |
57 | +#define FLGISSET(a,b) ((a&b)==0?0:1) | |
58 | +#define FLGTGL(a,b) (a^=b) | |
59 | +#define FLGCLR(a,b) (a^=(a&b)) | |
60 | + | |
61 | +/******************************************************************************* | |
62 | + * structure & typedef ********************************************************/ | |
63 | +/*=option structure===========================================================*/ | |
64 | +typedef struct { | |
65 | + u_int64_t enableConfigFlag; /* active config flag */ | |
66 | +#define OPTENABLECFLAGDEBUG 0x0000000000000001 | |
67 | +#define OPTENABLECFLAGCHROOT 0x0000000000000002 | |
68 | +#define OPTENABLECFLAGWORKDIR 0x0000000000000004 | |
69 | +#define OPTENABLECFLAGLHOST 0x0000000000000008 | |
70 | +#define OPTENABLECFLAGLPORT 0x0000000000000010 | |
71 | +#define OPTENABLECFLAGTCPACCEPTNUM 0x0000000000000020 | |
72 | +#define OPTENABLECFLAGLOGLEVEL 0x0000000000000040 | |
73 | +#define OPTENABLECFLAGDAEMONIZE 0x0000000000000080 | |
74 | + u_int32_t flags; /* flag option */ | |
75 | +#define OPTFLAGDEBUG 0x00000001 | |
76 | +#define OPTFLAGCHROOT 0x00000002 | |
77 | +#define OPTFLAGDAEMONIZE 0x00000004 | |
78 | + char workdir[MAXPATHLEN]; /* work directory */ | |
79 | + char host[NI_MAXHOST]; /* listen address */ | |
80 | + char ports[NI_MAXSERV]; /* listen service */ | |
81 | + int acceptNum; /* TCP accept Num */ | |
82 | + u_int32_t loglevel; /* log level */ | |
83 | +#define LOGLBL_DEBUG 0x000001 | |
84 | +#define LOGLBL_PACKET 0x000002 | |
85 | +#define LOGLBL_NOTIFICATION 0x000004 | |
86 | +#define LOGLBL_STATS 0x000008 | |
87 | +} optionParam; | |
88 | + | |
89 | +/******************************************************************************* | |
90 | + * variables ******************************************************************/ | |
91 | +/*=System=====================================================================*/ | |
92 | +char *prgName; /* arg program name ***********/ | |
93 | +char pname[32]; /* program name ***************/ | |
94 | +uid_t ruid; /* Real user id ***************/ | |
95 | +uid_t euid; /* Exec user id ***************/ | |
96 | +char syslogFile[MAXPATHLEN]; /* Proc system log ************/ | |
97 | +char statisticsFile[MAXPATHLEN]; /* Statistics log *************/ | |
98 | +char peerEventLog[MAXPATHLEN]; /* Peer event log *************/ | |
99 | +/*=Options====================================================================*/ | |
100 | +optionParam defParam; | |
101 | +optionParam cfgParam; | |
102 | +optionParam argParam; | |
103 | +optionParam lastParam; | |
104 | +/******************************************************************************* | |
105 | + * functions ******************************************************************/ | |
106 | +int bmpRead (int); | |
107 | +void writeSystemLog (const u_int32_t, const char *, const char *, ...); | |
108 | + | |
109 | +#endif |
@@ -0,0 +1,112 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | + | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | +#if !defined(BMP_STATION_H__) | |
29 | +# include <bmpStation.h> | |
30 | +#endif | |
31 | +#if !defined(BMP_STATION_INT_H__) | |
32 | +# include <bmpStationInt.h> | |
33 | +#endif | |
34 | +#if !defined(SELFLIB_H__) | |
35 | +# include <selfLib.h> | |
36 | +#endif | |
37 | +#if !defined(_STDIO_H) && !defined(_STDIO_H_) | |
38 | +# include <stdio.h> | |
39 | +#endif | |
40 | +#if !defined(_STDARG_H) && !defined(_STDARG_H_) | |
41 | +# include <stdarg.h> | |
42 | +#endif | |
43 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
44 | +# include <string.h> | |
45 | +#endif | |
46 | +#if !defined(_STDLIB_H_) && !defined(_STDLIB_H) | |
47 | +# include <stdlib.h> | |
48 | +#endif | |
49 | +#if !defined(_SYS_TYPES_H_) && !defined(_SYS_TYPES_H) | |
50 | +# include <sys/types.h> | |
51 | +#endif | |
52 | +#if !defined(_SYS_STAT_H_) && !defined(_SYS_STAT_H) | |
53 | +# include <sys/stat.h> | |
54 | +#endif | |
55 | + | |
56 | + | |
57 | +/*%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%** | |
58 | + *: write Log :** | |
59 | + *%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,%*/ | |
60 | + | |
61 | + | |
62 | +/******************************************************************************* | |
63 | + * syslog *********************************************************************/ | |
64 | +void writeSystemLog (const u_int32_t lbl, const char *remote, const char *fmt, ...) | |
65 | +{ | |
66 | + FILE *wfd; | |
67 | + char strdate[30]; | |
68 | + char *strLog; | |
69 | + char *jmp, *ser; | |
70 | + va_list ap; | |
71 | + mode_t mode; | |
72 | + | |
73 | + if (!FLGISSET(lastParam.loglevel, lbl)) {return ;} | |
74 | + /*=Get Infomation===========================================================*/ | |
75 | + if (!dateLog(strdate , sizeof (strdate ))) {goto end_proc;} | |
76 | + else if ((strLog=(char *)imalloc (LOGSTRMAX))==NULL) {goto end_proc;} | |
77 | + /*=Get Log==================================================================*/ | |
78 | + va_start(ap, fmt); | |
79 | + if (vsnprintf (strLog, LOGSTRMAX-1, fmt, ap)<0 ) {goto fin_va;} | |
80 | + va_end(ap); | |
81 | + | |
82 | + /*=Open File================================================================*/ | |
83 | + mode = umask (022); | |
84 | + if ((wfd=fopen (syslogFile, "a"))==NULL) {goto free_data;} | |
85 | + umask (mode); | |
86 | + | |
87 | + /*=write Log================================================================*/ | |
88 | + ser = strLog; | |
89 | + for (jmp=strstr (ser, "\n"); ; jmp=strstr (ser, "\n")) { | |
90 | + if (jmp==NULL ) {} | |
91 | + else if (*jmp=='\n') {*jmp = 0x00;} | |
92 | + if (remote==NULL) {fprintf (wfd, "%s %s(%d): %s\n" , strdate, prgName, getpid (), strLog);} | |
93 | + else {fprintf (wfd, "%s %s(%d): remote:%s %s\n", strdate, prgName, getpid (), remote, strLog);} | |
94 | + if (jmp==NULL) {break;} | |
95 | + ser = jmp + 1; | |
96 | + } | |
97 | + fclose (wfd); | |
98 | + | |
99 | + return; | |
100 | + | |
101 | + free_data2: | |
102 | + umask (mode); | |
103 | + free (strLog); | |
104 | + return ; | |
105 | + | |
106 | + fin_va: | |
107 | + va_end(ap); | |
108 | + free_data: | |
109 | + free (strLog); | |
110 | + end_proc: | |
111 | + return ; | |
112 | +} |
@@ -0,0 +1,13 @@ | ||
1 | +bin_PROGRAMS = bmpStation | |
2 | +bmpStation_SOURCES = bmpStationInt.h bmpStation.c bmpRead.c bmpStationLib.c | |
3 | +bmpStation_SOURCES += | |
4 | +bmpStation_SOURCES += | |
5 | +bmpStation_SOURCES += | |
6 | +bmpStation_CFLAGS = -O2 -Wall -I../include -L../lib -I../libbmp -L../libbmp -I../libbgp -L../libbgp | |
7 | +bmpStation_CFLAGS += | |
8 | +bmpStation_CFLAGS += -fno-strict-aliasing | |
9 | +bmpStation_CFLAGS += | |
10 | +bmpStation_LDADD = -lbmp -lbgp -lself | |
11 | +bmpStation_LDADD += | |
12 | +bmpStation_LDFLAGS = | |
13 | +bmpStation_LDFLAGS += |
@@ -0,0 +1,889 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | + | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | +#if !defined(BMP_STATION_H__) | |
29 | +# include <bmpStation.h> | |
30 | +#endif | |
31 | +#if !defined(BMP_STATION_INT_H__) | |
32 | +# include <bmpStationInt.h> | |
33 | +#endif | |
34 | +#if !defined(SELFLIB_H__) | |
35 | +# include <selfLib.h> | |
36 | +#endif | |
37 | +#if !defined(BMP_H__) | |
38 | +# include <bmp.h> | |
39 | +#endif | |
40 | +#if !defined(BGP_H__) | |
41 | +# include <bgp.h> | |
42 | +#endif | |
43 | +#if !defined(_ARPA_INET_H) && !defined(_ARPA_INET_H_) | |
44 | +# include <arpa/inet.h> | |
45 | +#endif | |
46 | +#if !defined(_NETINET_IN_H) && !defined(_NETINET_IN_H_) | |
47 | +# include <netinet/in.h> | |
48 | +#endif | |
49 | +#if !defined(_NETINET_TCP_H) && !defined(_NETINET_TCP_H_) | |
50 | +# include <netinet/tcp.h> | |
51 | +#endif | |
52 | +#if !defined(_SYS_SOCKET_H) && !defined(_SYS_SOCKET_H_) | |
53 | +# include <sys/socket.h> | |
54 | +#endif | |
55 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
56 | +# include <sys/types.h> | |
57 | +#endif | |
58 | +#if !defined(_SYS_TIME_H) && !defined(_SYS_TIME_H_) | |
59 | +# include <sys/time.h> | |
60 | +#endif | |
61 | +#if !defined(_SYS_FILE_H) && !defined(_SYS_FILE_H_) | |
62 | +# include <sys/file.h> | |
63 | +#endif | |
64 | +#if !defined(_SYS_SELECT_H) && !defined(_SYS_SELECT_H_) | |
65 | +# include <sys/select.h> | |
66 | +#endif | |
67 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
68 | +# include <string.h> | |
69 | +#endif | |
70 | +#if !defined(_UNISTD_H) && !defined(_UNISTD_H_) | |
71 | +# include <unistd.h> | |
72 | +#endif | |
73 | +#if !defined(_STDLIB_H) && !defined(_STDLIB_H_) | |
74 | +# include <stdlib.h> | |
75 | +#endif | |
76 | +#if !defined(_FCNTL_H) && !defined(_FCNTL_H_) | |
77 | +# include <fcntl.h> | |
78 | +#endif | |
79 | +#if !defined(_ERRNO_H) && !defined(_ERRNO_H_) | |
80 | +# include <errno.h> | |
81 | +#endif | |
82 | +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) | |
83 | +#if !defined(_SYS_ENDIAN_H) && !defined(_SYS_ENDIAN_H_) | |
84 | +# include <sys/endian.h> | |
85 | +#endif | |
86 | +#else | |
87 | +#if !defined(_ENDIAN_H) && !defined(_ENDIAN_H_) | |
88 | +# include <endian.h> | |
89 | +#endif | |
90 | +#endif | |
91 | + | |
92 | +#if 1 | |
93 | +#if !defined(_STDIO_H) && !defined(_STDIO_H_) | |
94 | +# include <stdio.h> | |
95 | +#endif | |
96 | +#endif | |
97 | + | |
98 | + | |
99 | +/******************************************************************************* | |
100 | + * Global variables ***********************************************************/ | |
101 | +static u_int8_t *pmsgStore; /* recv messages Data store */ | |
102 | +static ssize_t imsgStoreSize; /* pmsgStore size */ | |
103 | +static u_int8_t *pmsgBondStore; /* recv messages bondig Data store */ | |
104 | +static u_int8_t *pmsgBondLast; /* pmsgBond 1st nodata point */ | |
105 | +static u_int8_t *pmsgBondRead; /* pmsgBond read point */ | |
106 | +static ssize_t iBondStoreSize; /* pmsgBondStore size */ | |
107 | +static int soc4log; | |
108 | +/*=Peer infomation============================================================*/ | |
109 | +#define NEW 1 | |
110 | +#if NEW | |
111 | +char *pStrRemote = NULL; | |
112 | +#endif | |
113 | + | |
114 | +/******************************************************************************* | |
115 | + * functions ******************************************************************/ | |
116 | +static int getBMPLogInfo (BMPLogInfoSet *, u_int8_t *, ssize_t); | |
117 | + | |
118 | + | |
119 | +/* old */ | |
120 | +static void HEXoutput (u_int8_t *, int, int); /* debug code */ | |
121 | +static void debugBMPinfo (BMPLogInfoSet *); /* debug code */ | |
122 | +static int BMPRecvData (int); | |
123 | +#if 1 | |
124 | +static int BMPv1Data (BMPLogInfoSet *, u_int8_t *, ssize_t); | |
125 | +static int BMPv3Data (BMPLogInfoSet *, u_int8_t *, ssize_t); | |
126 | +#else | |
127 | +static int BMPv1Data (u_int8_t *, ssize_t); | |
128 | +static int BMPv3Data (u_int8_t *, ssize_t); | |
129 | +#endif | |
130 | + | |
131 | +static int BMPInitiation (u_int8_t *, ssize_t); | |
132 | +static int BMPTermination (u_int8_t *, ssize_t); | |
133 | +static int BMPRouteMonitoring (BMPLogInfoSet *, u_int8_t *, ssize_t); | |
134 | +static int BMPRouteMonitoringLog (BMPLogInfoSet *, char *, u_int8_t *, ssize_t); | |
135 | +static int BMPStatisticsData (BMPLogInfoSet *, u_int8_t *, ssize_t); | |
136 | +static int BMPPeerDown (BMPLogInfoSet *, u_int8_t *, ssize_t, int); | |
137 | +static int BMPPeerUp (BMPLogInfoSet *, u_int8_t *, ssize_t, int); | |
138 | + | |
139 | +static int getBMPPeerAddr (BMPLogInfoSet *, char *, const int); | |
140 | + | |
141 | +/******************************************************************************* | |
142 | + * main ***********************************************************************/ | |
143 | +int bmpRead (int soc) | |
144 | +{ | |
145 | + int res; | |
146 | + /* socket */ | |
147 | + int maxseg; /* TCP MSS size */ | |
148 | + int optlen; /* maxseg size */ | |
149 | + /* select */ | |
150 | + struct timeval itime, utime; | |
151 | + int fdw , ret; | |
152 | + fd_set iset , uset ; /* iset is initialize, uset for select */ | |
153 | + | |
154 | + /*=initialize===============================================================*/ | |
155 | + /* get Remote information */ | |
156 | + if ((pStrRemote = imalloc (NI_MAXHOST))==NULL) { | |
157 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
158 | + writeLog (syslogFile, prgName, soc, "Fault memory allocate for remote hostname."); | |
159 | + } | |
160 | + } | |
161 | + else if (!getRemoteHost (soc, pStrRemote, NI_MAXHOST)) { | |
162 | + if (FLGISSET(lastParam.loglevel, LOGLBL_STATS)) { | |
163 | + writeLog (syslogFile, prgName, soc, "Fault get remote hostname."); | |
164 | + } | |
165 | + goto free_remote_name; | |
166 | + } | |
167 | +#if 1 | |
168 | + soc4log = soc; | |
169 | +#endif | |
170 | + /* get TCP MSS size */ | |
171 | + optlen=sizeof(maxseg); | |
172 | + if (getsockopt(soc,IPPROTO_TCP,TCP_MAXSEG, &maxseg, (socklen_t *)&optlen)<0) { | |
173 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault get TCP MSS."); | |
174 | + goto free_remote_name; | |
175 | + } | |
176 | + /* memory allocate imsgStore */ | |
177 | + imsgStoreSize = maxseg; | |
178 | + if ((pmsgStore = malloc (imsgStoreSize))==NULL) { | |
179 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault memory allocate for message store."); | |
180 | + goto free_remote_name; | |
181 | + } | |
182 | + /* memory allocate imsgBondStore */ | |
183 | + iBondStoreSize = LIBBMP_MAXHEADERLEN + LIBBGP_MAXLENGTH + maxseg; | |
184 | + if ((pmsgBondStore = imalloc (iBondStoreSize))==NULL) { | |
185 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault memory allocate for message bond store."); | |
186 | + free (pmsgStore); | |
187 | + goto free_remote_name; | |
188 | + } | |
189 | + pmsgBondRead = pmsgBondLast = pmsgBondStore; | |
190 | + /* select setup */ | |
191 | + fdw = 0; | |
192 | + FD_ZERO(&iset); | |
193 | + FD_SET(soc, &iset); fdw = soc; | |
194 | + fdw++; | |
195 | + (void) memset (&itime, 0, sizeof (struct timeval)); | |
196 | + itime = d2tval (10); | |
197 | + | |
198 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Co-processes initialized."); | |
199 | + /*=Read BMP data start======================================================*/ | |
200 | + while (1) { | |
201 | + /*=setup==================================================================*/ | |
202 | + utime = itime; uset = iset; | |
203 | + ret = select (fdw, &uset, NULL, NULL, &utime); | |
204 | + if (ret< 0) { | |
205 | + switch (errno) { | |
206 | + case EINTR: continue; break; | |
207 | + case EBADF: break; | |
208 | + case EINVAL: break; | |
209 | + case ENOMEM: break; | |
210 | + } | |
211 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "select error."); | |
212 | + goto free_memory; | |
213 | + break; | |
214 | + } | |
215 | + else if (ret==0) { | |
216 | + continue ; | |
217 | + } | |
218 | + else if (FD_ISSET(soc, &uset)) { | |
219 | + res = BMPRecvData (soc); | |
220 | + if (res==1) {} | |
221 | + else if (res==0) {break;} | |
222 | + else if (res <0) {goto free_memory;} | |
223 | + } | |
224 | + } | |
225 | + /*=Read BMP data end========================================================*/ | |
226 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Termination."); | |
227 | + | |
228 | + return 1; | |
229 | + | |
230 | + free_memory: | |
231 | + free (pmsgStore); | |
232 | + free (pmsgBondStore); | |
233 | + | |
234 | + free_remote_name: | |
235 | + free (pStrRemote); | |
236 | + return 0; | |
237 | +} | |
238 | + | |
239 | +/******************************************************************************* | |
240 | + * Recv BMP data **************************************************************/ | |
241 | +static int BMPRecvData (int soc) | |
242 | +{ | |
243 | + int readLen; | |
244 | + int res; | |
245 | + int iSize; | |
246 | + /* BMP Log */ | |
247 | + BMPLogInfoSet BMPLogInfo; | |
248 | + struct timeval localTime; | |
249 | + | |
250 | + (void) memset (pmsgStore, 0, imsgStoreSize); | |
251 | + /*=Read data================================================================*/ | |
252 | + readLen = recv (soc, pmsgStore, imsgStoreSize, MSG_DONTWAIT); | |
253 | + if (readLen<0) { | |
254 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP data recv error."); | |
255 | + return -1; | |
256 | + } | |
257 | + else if (readLen > (iBondStoreSize-(pmsgBondLast - pmsgBondStore))) { | |
258 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP data bonding store is not enough area."); | |
259 | + return -1; | |
260 | + } | |
261 | + /*=Data Bonding=============================================================*/ | |
262 | + (void) memcpy (pmsgBondLast, pmsgStore, readLen); pmsgBondLast += readLen; | |
263 | + pmsgBondRead = pmsgBondStore; | |
264 | + do { | |
265 | + /*=BMP data size check====================================================*/ | |
266 | + iSize = bmpMessageSizeCheck (pmsgBondRead, (ssize_t)(pmsgBondLast-pmsgBondRead)); | |
267 | + if (iSize==0) {break;} | |
268 | + else if (iSize <0) {return -1;} | |
269 | + /*=BMP Log Information====================================================*/ | |
270 | + (void) memset ((void *)&BMPLogInfo, 0, sizeof (BMPLogInfoSet)); | |
271 | + (void) memset ((char *)&localTime , 0, sizeof (struct timeval)); | |
272 | + if (gettimeofday (&localTime, NULL)<0) { | |
273 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "can't get time."); | |
274 | + return -1; | |
275 | + } | |
276 | + BMPLogInfo.lcl_tv_sec = htobe32 (localTime.tv_sec); | |
277 | + BMPLogInfo.lcl_tv_usec = htobe32 (localTime.tv_usec); | |
278 | +#if 0 | |
279 | + getBMPLogInfo (&BMPLogInfo, pmsgBondRead, iSize); | |
280 | +#endif | |
281 | + | |
282 | + /*=get BMP data===========================================================*/ | |
283 | + switch (*(pmsgBondRead+0)) { | |
284 | + case 1: res = BMPv1Data (&BMPLogInfo, pmsgBondRead, iSize); break; | |
285 | + case 2: res = BMPv1Data (&BMPLogInfo, pmsgBondRead, iSize); break; | |
286 | + case 3: | |
287 | + res = BMPv3Data (&BMPLogInfo, pmsgBondRead, iSize); | |
288 | + break; | |
289 | + default: | |
290 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "recv can't understand data."); | |
291 | + return -1; | |
292 | + break; | |
293 | + } | |
294 | + if (res==0) {return 0;} | |
295 | + else if (res <0) {return -1;} | |
296 | +#if 1 | |
297 | + printf ("iSize: %d\n", iSize); | |
298 | + printf ("res : %d\n", res); | |
299 | +#endif | |
300 | + pmsgBondRead += iSize; | |
301 | + } while (pmsgBondRead<pmsgBondLast); | |
302 | + (void) memcpy (pmsgBondStore, pmsgBondRead, (pmsgBondLast-pmsgBondRead)); | |
303 | + pmsgBondLast = pmsgBondStore + (pmsgBondLast - pmsgBondRead); | |
304 | + | |
305 | + return 1; | |
306 | +} | |
307 | +/*%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%** | |
308 | + *: get BMP data each version :** | |
309 | + *%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,%*/ | |
310 | +/******************************************************************************* | |
311 | + * get BMP log infomation *****************************************************/ | |
312 | +static int getBMPLogInfo (BMPLogInfoSet *BMPLogInfo, u_int8_t *rdata, ssize_t dlen) | |
313 | +{ | |
314 | + return 1; | |
315 | +} | |
316 | + | |
317 | + | |
318 | + | |
319 | +/******************************************************************************* | |
320 | + * BMP v1 data ****************************************************************/ | |
321 | +static int BMPv1Data (BMPLogInfoSet *BMPLogInfo, u_int8_t *rdata, ssize_t dlen) | |
322 | +{ | |
323 | + /*=Variables================================================================*/ | |
324 | + int rsize; | |
325 | + int res = 0; | |
326 | + /* BMP */ | |
327 | + u_int8_t *msgData; | |
328 | + BMPv1Header *BMPHeader; | |
329 | + | |
330 | + /*=Init=====================================================================*/ | |
331 | + rsize = sizeof (BMPv1Header); | |
332 | + if (dlen<rsize) {return 0;} | |
333 | + BMPHeader = (BMPv1Header *)rdata; | |
334 | + msgData = (u_int8_t *)(BMPHeader+1); | |
335 | + /*=Format check=============================================================*/ | |
336 | + /* message type check */ | |
337 | + if (!bmpMessageTypeCheck (BMPHeader->version, BMPHeader->msgType)) { | |
338 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "recv can't understand the BMP message type."); | |
339 | + return -1; | |
340 | + } | |
341 | + else if (!bmpPeerTypeCheck (BMPHeader->version, BMPHeader->peerType)) { | |
342 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "recv can't understand the BMP peer type."); | |
343 | + return -1; | |
344 | + } | |
345 | + | |
346 | + /*=Recv Header==============================================================*/ | |
347 | + BMPLogInfo->version = BMPHeader->version; | |
348 | + BMPLogInfo->msgType = BMPHeader->msgType; | |
349 | + BMPLogInfo->peerType = BMPHeader->peerType; | |
350 | + BMPLogInfo->peerFlags = BMPHeader->peerFlags; | |
351 | + memcpy (&BMPLogInfo->peerDist, BMPHeader->peerDist, sizeof (((BMPLogInfoSet *)NULL)->peerDist)); | |
352 | + memcpy (&BMPLogInfo->peerAddr, BMPHeader->peerAddr, sizeof (((BMPLogInfoSet *)NULL)->peerAddr)); | |
353 | + BMPLogInfo->peerAS = BMPHeader->peerAS; | |
354 | + BMPLogInfo->peerBgpID = BMPHeader->peerBgpID; | |
355 | + BMPLogInfo->rtr_tv_sec = BMPHeader->tv_sec; | |
356 | + BMPLogInfo->rtr_tv_usec = BMPHeader->tv_usec; | |
357 | + BMPLogInfo->msgLen = htobe32 (sizeof (BMPLogInfoSet)); | |
358 | +#if 1 | |
359 | + HEXoutput (pmsgBondRead, dlen, 256); | |
360 | + debugBMPinfo (BMPLogInfo); | |
361 | +#endif | |
362 | + | |
363 | + /*=Data Output==============================================================*/ | |
364 | + switch (BMPHeader->msgType) { | |
365 | + case BMPV1MSGTYPE_ROUTEMON: | |
366 | + res = BMPRouteMonitoring (BMPLogInfo, msgData, dlen - sizeof (BMPv1Header)); | |
367 | + break; | |
368 | + case BMPV1MSGTYPE_STATISTICS: | |
369 | + res = BMPStatisticsData (BMPLogInfo, msgData, dlen - sizeof (BMPv1Header)); | |
370 | + break; | |
371 | + case BMPV1MSGTYPE_PEERDOWN: | |
372 | + res = BMPPeerDown (BMPLogInfo, msgData, dlen - sizeof (BMPv1Header), BMPHeader->version); | |
373 | + break; | |
374 | + case BMPV1MSGTYPE_PEERUP: | |
375 | + res = BMPPeerUp (BMPLogInfo, msgData, dlen - sizeof (BMPv1Header), BMPHeader->version); | |
376 | + break; | |
377 | + } | |
378 | + if (res <0) {return -1;} | |
379 | + else if (res==0) {return 1;} | |
380 | + rsize+=res; | |
381 | + | |
382 | + return 1; | |
383 | +} | |
384 | + | |
385 | +/******************************************************************************* | |
386 | + * BMP v3 data ****************************************************************/ | |
387 | +static int BMPv3Data (BMPLogInfoSet *BMPLogInfo, u_int8_t *rdata, ssize_t dlen) | |
388 | +{ | |
389 | + /*=Variables================================================================*/ | |
390 | + int rsize, hlen, msgLen; | |
391 | + int res = 0; | |
392 | + /* BMP */ | |
393 | + u_int8_t *msgData; | |
394 | + BMPv3Header *BMPCommonHeader; | |
395 | + BMPv3PerPeerHeader *BMPHeader; | |
396 | + | |
397 | + /*=Init=====================================================================*/ | |
398 | +#if 0 | |
399 | + HEXoutput (rdata, dlen, 256); | |
400 | +#endif | |
401 | + hlen = rsize = sizeof (BMPv3Header); | |
402 | + BMPCommonHeader = (BMPv3Header *)rdata; | |
403 | + msgData = (u_int8_t *)(BMPCommonHeader+1); | |
404 | + msgLen = be32toh (BMPCommonHeader->msgLen); | |
405 | + /*=Format check=============================================================*/ | |
406 | +#if 0 | |
407 | + HEXoutput (pmsgBondRead, dlen, 256); | |
408 | + printf ("version : 0x%02x : 0x%08x : %d\n" , BMPCommonHeader->version , &BMPCommonHeader->version , sizeof (BMPCommonHeader->version)); | |
409 | + printf ("msgLen : %d : 0x%08x : %d, 0x%08x\n" , be32toh (BMPCommonHeader->msgLen) , &BMPCommonHeader->msgLen , sizeof (BMPCommonHeader->msgLen ), BMPCommonHeader->msgLen); | |
410 | + printf ("msgType : 0x%02x : 0x%08x : %d\n" , BMPCommonHeader->msgType , &BMPCommonHeader->msgType , sizeof (BMPCommonHeader->msgType)); | |
411 | +#endif | |
412 | + /* message type check */ | |
413 | + if (!bmpMessageTypeCheck (BMPCommonHeader->version, BMPCommonHeader->msgType)) { | |
414 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "recv can't understand the BMP message type."); | |
415 | + return -1; | |
416 | + } | |
417 | + switch (BMPCommonHeader->msgType) { | |
418 | + case BMPV3MSGTYPE_INIT: | |
419 | + res = BMPInitiation ((u_int8_t *)(BMPCommonHeader+1), msgLen - hlen); | |
420 | + if (res <0) { | |
421 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "fault BMP initiation."); | |
422 | + return -1; | |
423 | + } | |
424 | + else if (res==0) {return 1;} | |
425 | + return 0; | |
426 | + case BMPV3MSGTYPE_TERMINATE: | |
427 | + if (!BMPTermination ((u_int8_t *)(BMPCommonHeader+1), msgLen - hlen)) { | |
428 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "fault BMP termination."); | |
429 | + return -1; | |
430 | + } | |
431 | + return 0; | |
432 | + break; | |
433 | + default: | |
434 | + break; | |
435 | + } | |
436 | + | |
437 | + hlen += sizeof (BMPv3PerPeerHeader); | |
438 | + BMPHeader = (BMPv3PerPeerHeader *)msgData; | |
439 | + msgData = (u_int8_t *)(BMPHeader+1); | |
440 | + /* peer type check */ | |
441 | + if (!bmpPeerTypeCheck (BMPCommonHeader->version, BMPHeader->peerType)) { | |
442 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "recv can't understand the BMP peer type."); | |
443 | + return -1; | |
444 | + } | |
445 | + | |
446 | + /*=Recv Header==============================================================*/ | |
447 | + BMPLogInfo->version = BMPCommonHeader->version; | |
448 | + BMPLogInfo->msgLen = htobe32 (sizeof (BMPLogInfoSet)); | |
449 | + BMPLogInfo->msgType = BMPCommonHeader->msgType; | |
450 | + BMPLogInfo->peerType = BMPHeader->peerType; | |
451 | + BMPLogInfo->peerFlags = BMPHeader->peerFlags; | |
452 | + memcpy (&BMPLogInfo->peerDist, BMPHeader->peerDist, sizeof (((BMPLogInfoSet *)NULL)->peerDist)); | |
453 | + memcpy (&BMPLogInfo->peerAddr, BMPHeader->peerAddr, sizeof (((BMPLogInfoSet *)NULL)->peerAddr)); | |
454 | + BMPLogInfo->peerAS = BMPHeader->peerAS; | |
455 | + BMPLogInfo->peerBgpID = BMPHeader->peerBgpID; | |
456 | + BMPLogInfo->rtr_tv_sec = BMPHeader->tv_sec; | |
457 | + BMPLogInfo->rtr_tv_usec = BMPHeader->tv_usec; | |
458 | + | |
459 | +#if 1 | |
460 | + HEXoutput (pmsgBondRead, dlen, 256); | |
461 | + debugBMPinfo (BMPLogInfo); | |
462 | +#endif | |
463 | + | |
464 | + /*=Data Output==============================================================*/ | |
465 | + res = -1; | |
466 | + switch (BMPCommonHeader->msgType) { | |
467 | + case BMPV3MSGTYPE_ROUTEMON: | |
468 | + res = BMPRouteMonitoring (BMPLogInfo, msgData, be32toh (BMPCommonHeader->msgLen)-hlen); | |
469 | + break; | |
470 | + case BMPV3MSGTYPE_STATISTICS: | |
471 | + res = BMPStatisticsData (BMPLogInfo, msgData, be32toh (BMPCommonHeader->msgLen)-hlen); | |
472 | + break; | |
473 | + case BMPV3MSGTYPE_PEERDOWN: | |
474 | + res = BMPPeerDown (BMPLogInfo, msgData, be32toh (BMPCommonHeader->msgLen)-hlen, BMPCommonHeader->version); | |
475 | + break; | |
476 | + case BMPV3MSGTYPE_PEERUP: | |
477 | + res = BMPPeerUp (BMPLogInfo, msgData, be32toh (BMPCommonHeader->msgLen)-hlen, BMPCommonHeader->version); | |
478 | + break; | |
479 | + } | |
480 | + if (res <0) {return -1;} | |
481 | + else if (res==0) {return 1;} | |
482 | + | |
483 | + return 1; | |
484 | +} | |
485 | + | |
486 | +/*%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%** | |
487 | + *: get BMP message :** | |
488 | + *%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,%*/ | |
489 | +/******************************************************************************* | |
490 | + * Route Monitoring ***********************************************************/ | |
491 | +static int BMPRouteMonitoring (BMPLogInfoSet *BMPLogInfo, u_int8_t *rdata, ssize_t dlen) | |
492 | +{ | |
493 | + int rsize; | |
494 | + BGPCommonHeader *BGPHeader; | |
495 | + u_int8_t *msgData; | |
496 | + char hostAddr[MAXPATHLEN]; | |
497 | + | |
498 | + rsize = sizeof (BGPCommonHeader); | |
499 | + if (dlen<rsize) {return 0;} | |
500 | + /*=Init=====================================================================*/ | |
501 | + BGPHeader = (BGPCommonHeader *)rdata; | |
502 | + msgData = (u_int8_t *)(BGPHeader+1); | |
503 | + rsize = be16toh(BGPHeader->length); | |
504 | +#if 0 | |
505 | + printf ("rsize(BGP headder): %d, 0x%04x\n", (int)rsize, rsize); | |
506 | + printf ("dlen : %d\n", (int)dlen); | |
507 | + HEXoutput (BGPHeader, rsize, 256); | |
508 | +#endif | |
509 | + if (dlen < rsize) {return 0;} | |
510 | + BMPLogInfo->msgLen = htobe32 (be32toh (BMPLogInfo->msgLen) + rsize); | |
511 | +#if 0 | |
512 | + printf ("pmsgBondStore : 0x%016x\n", (int)pmsgBondStore); | |
513 | + printf ("BGPCommonHeader: 0x%016x\n", (int)BGPHeader); | |
514 | + printf ("BGPLength : 0x%d\n" , (int)be16toh(BGPHeader->length)); | |
515 | + HEXoutput (rdata, (int)be16toh(BGPHeader->length), 256); | |
516 | +#endif | |
517 | + /*=BGP Data Save============================================================*/ | |
518 | + getRemoteHost (soc4log, hostAddr, sizeof (hostAddr)); | |
519 | + if (!BMPRouteMonitoringLog (BMPLogInfo, hostAddr, rdata, rsize)) { | |
520 | + return -1; | |
521 | + } | |
522 | + | |
523 | + return rsize; | |
524 | +} | |
525 | + | |
526 | + | |
527 | +/******************************************************************************* | |
528 | + * Write Route monitor messages ***********************************************/ | |
529 | +static int BMPRouteMonitoringLog (BMPLogInfoSet *BMPLogInfo, char *host, u_int8_t *rdata, ssize_t len) | |
530 | +{ | |
531 | + int wfd; | |
532 | + int wlen, tlen; | |
533 | + char routeMonitor[MAXPATHLEN]; | |
534 | + (void) memset (routeMonitor, 0, sizeof (routeMonitor)); | |
535 | + snprintf (routeMonitor, sizeof (routeMonitor)-1, "%s/%s.log", lastParam.workdir, host); | |
536 | + | |
537 | + if ((wfd=open (routeMonitor, O_WRONLY|O_CREAT, 0644))<0) {return 0;} | |
538 | + else if (flock (wfd, LOCK_EX)<0) {close (wfd); return 0;} | |
539 | + else if (lseek(wfd, 0, SEEK_END)<0) {close (wfd); return 0;} | |
540 | +#if 0 | |
541 | + printf ("BMP LogInfo(bef): 0x%016x: tlen: %d\n", (int)BMPLogInfo, tlen); | |
542 | + HEXoutput (BMPLogInfo, sizeof (BMPLogInfoSet), 256); | |
543 | +#endif | |
544 | + wlen = tlen = write (wfd, (u_int8_t *)BMPLogInfo, sizeof (BMPLogInfoSet)); | |
545 | +#if 0 | |
546 | + printf ("BMP LogInfo(aft): 0x%016x: tlen: %d\n", (int)BMPLogInfo, tlen); | |
547 | + HEXoutput (BMPLogInfo, sizeof (BMPLogInfoSet), 256); | |
548 | +#endif | |
549 | + if (tlen != sizeof (BMPLogInfoSet)) { | |
550 | + flock (wfd, LOCK_UN); | |
551 | + close (wfd); | |
552 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault write BGP Monitor Data(BMP Info): %s", routeMonitor); | |
553 | + return 0; | |
554 | + } | |
555 | + tlen = write (wfd, rdata, len); | |
556 | +#if 0 | |
557 | + printf ("BGP data : 0x%016x: tlen: %d\n", (int)rdata, tlen); | |
558 | + HEXoutput (rdata, len, 256); | |
559 | +#endif | |
560 | + wlen += tlen; | |
561 | + if (tlen != len) { | |
562 | + flock (wfd, LOCK_UN); | |
563 | + close (wfd); | |
564 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault write BGP Monitor Data(BMP Data): %s", routeMonitor); | |
565 | + return 0; | |
566 | + } else if (wlen != be32toh (BMPLogInfo->msgLen)) { | |
567 | + flock (wfd, LOCK_UN); | |
568 | + close (wfd); | |
569 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault write BGP Monitor Data(Fault Data length): %s", routeMonitor); | |
570 | + } | |
571 | + | |
572 | + flock (wfd, LOCK_UN); | |
573 | + close (wfd); | |
574 | + | |
575 | + return 1; | |
576 | +} | |
577 | + | |
578 | +/******************************************************************************* | |
579 | + * Stats Reports **************************************************************/ | |
580 | +static int BMPStatisticsData (BMPLogInfoSet *BMPLogInfo, u_int8_t *rdata, ssize_t dlen) | |
581 | +{ | |
582 | + int rsize, i, num; | |
583 | + u_int8_t *ptmp; | |
584 | + BMPMsgStatsNum *StatsNum; | |
585 | + BMPMsgStatsHeader *StatsHeader; | |
586 | + BMPMsgStatsData *StatsData; | |
587 | + char peerAddr[MAXPATHLEN]; | |
588 | + rsize = sizeof (BMPMsgStatsNum); | |
589 | + if (dlen<rsize) {return 0;} | |
590 | + /*=Init=====================================================================*/ | |
591 | + StatsNum = (BMPMsgStatsNum *)rdata; | |
592 | + num = be32toh (*StatsNum); | |
593 | + for (i=0; i<num; i++) { | |
594 | + ptmp = rdata + rsize; | |
595 | + rsize += sizeof (BMPMsgStatsHeader); | |
596 | + if (dlen<rsize) {return 0;} | |
597 | + StatsHeader = (BMPMsgStatsHeader *)ptmp; | |
598 | + rsize += be16toh (StatsHeader->statLen); | |
599 | + if (dlen<rsize) {return 0;} | |
600 | + } | |
601 | + | |
602 | +#if 0 | |
603 | + printf ("BMP statistics data\n"); | |
604 | + printf ("rsize(BGP headder): %d\n", (int)rsize); | |
605 | + printf ("dlen : %d\n", (int)dlen); | |
606 | + HEXoutput (rdata, dlen, 256); | |
607 | +#endif | |
608 | + getBMPPeerAddr (BMPLogInfo, peerAddr, sizeof (peerAddr)); | |
609 | + ptmp = (u_int8_t *)(StatsNum+1); | |
610 | + for (i=0; i<num; i++) { | |
611 | + StatsHeader = (BMPMsgStatsHeader *)ptmp; | |
612 | + StatsData = (BMPMsgStatsData *)(StatsHeader+1); | |
613 | + ptmp = (u_int8_t *)StatsData; | |
614 | + ptmp += be16toh (StatsHeader->statLen); | |
615 | + switch (be16toh (StatsHeader->statType)) { | |
616 | + case BMPSTATSTYPE_REJECT_BY_INBOUND_POLICY: | |
617 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of prefixes rejected by inbound policy: %u", peerAddr, (int)be32toh (StatsData->count32)); | |
618 | + break; | |
619 | + case BMPSTATSTYPE_DUPLICATE_ADVERTISEMENTS: | |
620 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of (known) duplicate prefix: %u", peerAddr, (int)be32toh (StatsData->count32)); | |
621 | + break; | |
622 | + case BMPSTATSTYPE_DUPLICATE_WITHDRAWS: | |
623 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of (known) duplicate withdraws: %u", peerAddr, (int)be32toh (StatsData->count32)); | |
624 | + break; | |
625 | + case BMPSTATSTYPE_UPDATES_INVALIDATED_CLUSTER: | |
626 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of updates invalidated due to CLUSTER_LIST loop: %u", peerAddr, (int)be32toh (StatsData->count32)); | |
627 | + break; | |
628 | + case BMPSTATSTYPE_UPDATES_INVALIDATED_AS_PATH: | |
629 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of updates invalidated due to AS_PATH loop: %u", peerAddr, (int)be32toh (StatsData->count32)); | |
630 | + break; | |
631 | + case BMPSTATSTYPE_UPDATES_INVALIDATED_ORIGIN: | |
632 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of updates invalidated due to ORIGINATOR_ID: %u", peerAddr, (int)be32toh (StatsData->count32)); | |
633 | + break; | |
634 | + case BMPSTATSTYPE_UPDATES_INVALIDATED_AS_CONF: | |
635 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of updates invalidated due to AS_CONFED loop: %u", peerAddr, (int)be32toh (StatsData->count32)); | |
636 | + break; | |
637 | + case BMPSTATSTYPE_ROUTES_ADJ_RIBs_IN: | |
638 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of routes in Adj-RIBs-In: %lu", peerAddr, (int)be64toh (StatsData->count64)); | |
639 | + break; | |
640 | + case BMPSTATSTYPE_ROUTES_LOC_RIB: | |
641 | + writeLog (statisticsFile, prgName, soc4log, "Adj-RIB-in: %s : # of routes in Loc-RIB: %lu", peerAddr, (int)be64toh (StatsData->count64)); | |
642 | + break; | |
643 | + } | |
644 | + } | |
645 | + BMPLogInfo->msgLen += rsize; | |
646 | + | |
647 | + return rsize; | |
648 | +} | |
649 | + | |
650 | +/******************************************************************************* | |
651 | + * BGP Peer Down **************************************************************/ | |
652 | +static int BMPPeerDown (BMPLogInfoSet *BMPLogInfo, u_int8_t *rdata, ssize_t dlen, int version) | |
653 | +{ | |
654 | + int rsize; | |
655 | + BMPMsgPeerDown *BMPPeerDownHeader; | |
656 | + u_int8_t *msgData; | |
657 | + BGPCommonHeader *BGPHeader; | |
658 | + char peerAddr[MAXPATHLEN]; | |
659 | + rsize = sizeof (BMPMsgPeerDown); | |
660 | + if (dlen<rsize) {return 0;} | |
661 | + /*=Init=====================================================================*/ | |
662 | + BMPPeerDownHeader = (BMPMsgPeerDown *)rdata; | |
663 | + getBMPPeerAddr (BMPLogInfo, peerAddr, sizeof (peerAddr)); | |
664 | + if (BMPPeerDownHeader->reason==4) { | |
665 | + writeLog (peerEventLog, prgName, soc4log, "BGP Peer Down: %s", peerAddr); | |
666 | + BMPLogInfo->msgLen += rsize; | |
667 | + return rsize; | |
668 | + } | |
669 | + BGPHeader = (BGPCommonHeader *)(BMPPeerDownHeader+1); | |
670 | + rsize += (int)be16toh (BGPHeader->length); | |
671 | + if (dlen<rsize) {return 0;} | |
672 | + else if (BGPHeader->type!=BGPMSGTYPE_NOTIFICATION) { /* sikp no target data */ | |
673 | + BMPLogInfo->msgLen += rsize; | |
674 | + return rsize; | |
675 | + } | |
676 | + writeLog (peerEventLog, prgName, soc4log, "BGP Peer Down: %s", peerAddr); | |
677 | + | |
678 | + return rsize; | |
679 | +} | |
680 | + | |
681 | +/******************************************************************************* | |
682 | + * BGP Peer Up ****************************************************************/ | |
683 | +static int BMPPeerUp (BMPLogInfoSet *BMPLogInfo, u_int8_t *rdata, ssize_t dlen, int version) | |
684 | +{ | |
685 | + int rsize; | |
686 | + BGPCommonHeader *BGPHeader; | |
687 | + BMPMsgPeerUp *BMPPeerUpHeader; | |
688 | + u_int8_t *msgData; | |
689 | + char peerAddr[MAXPATHLEN]; | |
690 | + | |
691 | + getBMPPeerAddr (BMPLogInfo, peerAddr, sizeof (peerAddr)); | |
692 | + if (BMPLogInfo->version==2) { | |
693 | + rsize = sizeof (BGPCommonHeader); | |
694 | + if (dlen<rsize) {return 0;} | |
695 | + /*=Init===================================================================*/ | |
696 | + getBMPPeerAddr (BMPLogInfo, peerAddr, sizeof (peerAddr)); | |
697 | + BGPHeader = (BGPCommonHeader *)rdata; | |
698 | + msgData = (u_int8_t *)(BGPHeader+1); | |
699 | + if (dlen < be16toh(BGPHeader->length)) {return 0;} | |
700 | + rsize = be16toh(BGPHeader->length); | |
701 | + if (BGPHeader->type!=BGPMSGTYPE_OPEN) {return rsize;} | |
702 | + writeLog (peerEventLog, prgName, soc4log, "BGP Peer Up: %s", peerAddr); | |
703 | + BMPLogInfo->msgLen += rsize; | |
704 | + return rsize; | |
705 | + } else if (BMPLogInfo->version==3) { | |
706 | +#if 0 | |
707 | + printf ("rsize(BGP headder): %d\n", (int)rsize); | |
708 | + printf ("dlen : %d\n", (int)dlen); | |
709 | + HEXoutput (rdata, dlen, 256); | |
710 | +#endif | |
711 | + BMPPeerUpHeader = (BMPMsgPeerUp *)rdata; | |
712 | + writeLog (peerEventLog, prgName, soc4log, "BGP Peer Up: %s", peerAddr); | |
713 | + return dlen; | |
714 | + } | |
715 | + | |
716 | + return -1; | |
717 | +} | |
718 | + | |
719 | +/******************************************************************************* | |
720 | + * BMP Initiation Messages ****************************************************/ | |
721 | +static int BMPInitiation (u_int8_t *rdata, ssize_t dlen) | |
722 | +{ | |
723 | + int psize; | |
724 | + u_int8_t *ptmp; | |
725 | + u_int8_t *pdata; | |
726 | + int pdlen; | |
727 | + BMPMsgInit *BMPInitMessage; | |
728 | + | |
729 | + /*=Init=====================================================================*/ | |
730 | + ptmp = rdata; | |
731 | + psize = dlen; | |
732 | +#if 0 | |
733 | + printf ("BMP Initiaion\n"); | |
734 | + HEXoutput (rdata, dlen, 256); | |
735 | +#endif | |
736 | + | |
737 | + /*=Main proc================================================================*/ | |
738 | + do { | |
739 | + BMPInitMessage = (BMPMsgInit *)ptmp; | |
740 | + pdlen = be16toh (BMPInitMessage->infoLen); | |
741 | + ptmp += pdlen + sizeof (BMPMsgInit);; | |
742 | + psize -= pdlen + sizeof (BMPMsgInit);; | |
743 | + if ((pdata=malloc (pdlen+1))==NULL) { | |
744 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault memory allocate for BMP initiaion."); | |
745 | + return -1; | |
746 | + } | |
747 | +#if 0 | |
748 | + printf ("type : %d\n", be16toh (BMPInitMessage->infoType)); | |
749 | +#endif | |
750 | + (void) memset (pdata, 0, pdlen+1); | |
751 | + (void) memcpy (pdata, (BMPInitMessage+1), pdlen); | |
752 | + switch (be16toh (BMPInitMessage->infoType)) { | |
753 | + case BMPINITMSG_STRING: | |
754 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP Initiaion: string\n %s", pdata); | |
755 | + break; | |
756 | + case BMPINITMSG_SYSDESCR: | |
757 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP Initiaion: sysDescr\n %s", pdata); | |
758 | + break; | |
759 | + case BMPINITMSG_SYSNAME: | |
760 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP Initiaion: sysName\n %s", pdata); | |
761 | + break; | |
762 | + default: | |
763 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP Initiaion: can't understand messages\n"); | |
764 | + free (pdata); | |
765 | + return -1; | |
766 | + break; | |
767 | + } | |
768 | + free (pdata); | |
769 | + } while (psize>0); | |
770 | + if (psize<0) { | |
771 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP initiation messages length error."); | |
772 | + return -1; | |
773 | + } | |
774 | + | |
775 | + return dlen; | |
776 | +} | |
777 | + | |
778 | +/******************************************************************************* | |
779 | + * BMP Termination Messages ***************************************************/ | |
780 | +static int BMPTermination (u_int8_t *rdata, ssize_t dlen) | |
781 | +{ | |
782 | + int psize; | |
783 | + u_int8_t *ptmp; | |
784 | + u_int8_t *pdata; | |
785 | + int pdlen; | |
786 | + BMPMsgTerminate *BMPTermMessage; | |
787 | + | |
788 | + /*=Init=====================================================================*/ | |
789 | + ptmp = rdata; | |
790 | + psize = dlen; | |
791 | +#if 0 | |
792 | + printf ("BMP Termination\n"); | |
793 | + HEXoutput (rdata, dlen, 256); | |
794 | + printf ("psize: %d\n", psize); | |
795 | +#endif | |
796 | + | |
797 | + /*=Main proc================================================================*/ | |
798 | + do { | |
799 | + BMPTermMessage = (BMPMsgTerminate *)ptmp; | |
800 | + pdlen = be16toh (BMPTermMessage->infoLen); | |
801 | + ptmp += pdlen + sizeof (BMPMsgTerminate); | |
802 | + psize -= pdlen + sizeof (BMPMsgTerminate); | |
803 | + printf ("pdlen: %d\n", pdlen); | |
804 | + printf ("psize: %d\n\n", psize); | |
805 | + if ((pdata=malloc (pdlen+1))==NULL) { | |
806 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "Fault memory allocate for BMP Termination."); | |
807 | + return 0; | |
808 | + } | |
809 | +#if 0 | |
810 | + printf ("type : %d\n", be16toh (BMPTermMessage->infoType)); | |
811 | +#endif | |
812 | + (void) memset (pdata, 0, pdlen+1); | |
813 | + (void) memcpy (pdata, (BMPTermMessage+1), pdlen); | |
814 | + switch (be16toh (BMPTermMessage->infoType)) { | |
815 | + case BMPTERMMSG_STRING: | |
816 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP Termination: string\n %s", pdata); | |
817 | + break; | |
818 | + case BMPTERMMSG_REASON: | |
819 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP Termination: reason: %d", be16toh (*((u_int16_t *)pdata))); | |
820 | + break; | |
821 | + } | |
822 | + free (pdata); | |
823 | + } while (psize>0); | |
824 | + if (psize<0) { | |
825 | + writeSystemLog (LOGLBL_STATS, pStrRemote, "BMP Term messages length error."); | |
826 | + return 0; | |
827 | + } | |
828 | + | |
829 | + return 1; | |
830 | +} | |
831 | + | |
832 | + | |
833 | +/******************************************************************************* | |
834 | + * GET BMP peer address *******************************************************/ | |
835 | +static int getBMPPeerAddr (BMPLogInfoSet *BMPLogInfo, char *pstr, const int size) | |
836 | +{ | |
837 | + struct in_addr *sin_addr; | |
838 | + struct in6_addr *sin6_addr; | |
839 | + | |
840 | + (void) memset (pstr, 0, size); | |
841 | + sin_addr = (struct in_addr *)&BMPLogInfo->peerAddr[3]; | |
842 | + sin6_addr = (struct in6_addr *)&BMPLogInfo->peerAddr; | |
843 | + if (FLGISSET(BMPLogInfo->peerFlags, BMPPEERFLG_VFLG_GET)) { | |
844 | + inet_ntop (AF_INET6, sin6_addr, pstr, size); | |
845 | + } else { | |
846 | + inet_ntop (AF_INET, sin_addr, pstr, size); | |
847 | + } | |
848 | + | |
849 | + return 1; | |
850 | +} | |
851 | + | |
852 | +/*%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%** | |
853 | + *: DEBUG CODE :** | |
854 | + *%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,%*/ | |
855 | +/******************************************************************************* | |
856 | + * BMP Information output *****************************************************/ | |
857 | +static void debugBMPinfo (BMPLogInfoSet *BMPLogInfo) | |
858 | +{ | |
859 | + printf ("version : 0x%02x\n" , BMPLogInfo->version ); | |
860 | + printf ("msgLen : %d \n" , be32toh (BMPLogInfo->msgLen) ); | |
861 | + printf ("msgType : 0x%02x\n" , BMPLogInfo->msgType ); | |
862 | + printf ("peerType : 0x%02x\n" , BMPLogInfo->peerType ); | |
863 | + printf ("peerFlags: 0x%02x\n" , BMPLogInfo->peerFlags ); | |
864 | + printf ("peerDist : \n" ); | |
865 | + printf ("peerAddr : \n" ); | |
866 | + printf ("peerAS : %d \n" , (int)be32toh (BMPLogInfo->peerAS)); | |
867 | + printf ("BGP ID : %u \n" , be32toh (BMPLogInfo->peerBgpID) ); | |
868 | + printf ("tv_sec : %u \n" , be32toh (BMPLogInfo->rtr_tv_sec) ); | |
869 | + printf ("tv_usec : %06u \n" , be32toh (BMPLogInfo->rtr_tv_usec)); | |
870 | +} | |
871 | + | |
872 | +/******************************************************************************* | |
873 | + * HEX Data output ************************************************************/ | |
874 | +static void HEXoutput (u_int8_t *rdata, int len, int max) | |
875 | +{ | |
876 | + int i; | |
877 | + int outLen; | |
878 | + int counter = -16; | |
879 | + outLen = (len<max?len:max); | |
880 | + | |
881 | + printf ("len: %d, output: %d\n", len, outLen); | |
882 | + for (i=0; i<outLen; i++) { | |
883 | + if ((i%16)==0) {counter += 16; printf ("0x%04X:", counter);} | |
884 | + if ((i%2)==0) {printf (" ");} | |
885 | + printf ("%02x", rdata[i]); | |
886 | + if ((i%16)==15) {printf ("\n");} | |
887 | + } | |
888 | + printf ("\n"); | |
889 | +} |
@@ -0,0 +1,100 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | +#if !defined(BMP_STATION_H__) | |
26 | +#define BMP_STATION_H__ 1 | |
27 | +/******************************************************************************* | |
28 | + * include ********************************************************************/ | |
29 | +#if defined(HAVE_CONFIG_H) | |
30 | +# include <config.h> | |
31 | +#endif | |
32 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
33 | +# include <sys/types.h> | |
34 | +#endif | |
35 | + | |
36 | +/******************************************************************************* | |
37 | + * Define *********************************************************************/ | |
38 | + | |
39 | +/******************************************************************************* | |
40 | + * Macro **********************************************************************/ | |
41 | + | |
42 | +/******************************************************************************* | |
43 | + * structure & typedef ********************************************************/ | |
44 | +/*+==========================================================================+** | |
45 | + *| BMP Station logging |** | |
46 | + *+==========================================================================+*/ | |
47 | +/*=BMP logging binary=========================================================** | |
48 | + * save data is big endian | |
49 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
50 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
51 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
52 | + * | Version | msg type | peer type | peer flags | | |
53 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
54 | + * | msg length | | |
55 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
56 | + * | Peer Distinguisher (8 Byte) | | |
57 | + * | | | |
58 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
59 | + * | Peer Address (16 Byte) | | |
60 | + * | | | |
61 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
62 | + * | Peer AS | | |
63 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
64 | + * | Peer BGP ID | | |
65 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
66 | + * | Router Timestamp (seconds) | | |
67 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
68 | + * | Router Timestamp (microseconds) | | |
69 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
70 | + * | Local Timestamp (seconds) | | |
71 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
72 | + * | Local Timestamp (microseconds) | | |
73 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
74 | + * | messages data | | |
75 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
76 | + ******************************************************************************/ | |
77 | +typedef struct { | |
78 | + u_int8_t version; | |
79 | + u_int8_t msgType; | |
80 | + u_int8_t peerType; | |
81 | + u_int8_t peerFlags; | |
82 | + u_int32_t msgLen; | |
83 | + u_int32_t peerDist[2]; | |
84 | + u_int32_t peerAddr[4]; | |
85 | + u_int32_t peerAS; | |
86 | + u_int32_t peerBgpID; | |
87 | + u_int32_t rtr_tv_sec; | |
88 | + u_int32_t rtr_tv_usec; | |
89 | + u_int32_t lcl_tv_sec; | |
90 | + u_int32_t lcl_tv_usec; | |
91 | +} __attribute__((__packed__)) BMPLogInfoSet; | |
92 | + | |
93 | +/******************************************************************************* | |
94 | + * variables ******************************************************************/ | |
95 | + | |
96 | +/******************************************************************************* | |
97 | + * functions ******************************************************************/ | |
98 | + | |
99 | + | |
100 | +#endif |
@@ -0,0 +1,72 @@ | ||
1 | +/* -*- c -*- */ | |
2 | +/*- | |
3 | + * The MIT License | |
4 | + * | |
5 | + * Copyright (c) 2014 Yuki SAKAI | |
6 | + * All Rights Reserved. | |
7 | + * | |
8 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
9 | + * copy of this software and associated documentation files (the "Software"), | |
10 | + * to deal in the Software without restriction, including without limitation | |
11 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | + * and/or sell copies of the Software, and to permit persons to whom the | |
13 | + * Software is furnished to do so, subject to the following conditions: | |
14 | + * | |
15 | + * The above copyright notice and this permission notice shall be included in | |
16 | + * all copies or substantial portions of the Software. | |
17 | + * | |
18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
24 | + * IN THE SOFTWARE. | |
25 | + */ | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | + | |
29 | +/******************************************************************************* | |
30 | + * Personal system library | |
31 | + ******************************************************************************/ | |
32 | +#ifndef SELFLIB_H__ | |
33 | +#define SELFLIB_H__ __DATE__ | |
34 | +/******************************************************************************* | |
35 | + * Include ********************************************************************/ | |
36 | +#if !defined(_SYS_TYPES_H_) && !defined(_SYS_TYPES_H) | |
37 | +# include <sys/types.h> | |
38 | +#endif | |
39 | +#if !defined(_SYS_STAT_H) && !defined(_SYS_STAT_H_) | |
40 | +# include <sys/stat.h> | |
41 | +#endif | |
42 | +#if !defined(_NETDB_H) && !defined(_NETDB_H_) | |
43 | +# include <netdb.h> | |
44 | +#endif | |
45 | + | |
46 | +/******************************************************************************* | |
47 | + * Functions ******************************************************************/ | |
48 | +/*=memory=====================================================================*/ | |
49 | +void *imalloc (size_t); | |
50 | +/*=Network====================================================================*/ | |
51 | +int lsocket (const char *, const char *, int, int); | |
52 | +int getRemoteHost (const int, char *, const int); | |
53 | +/*=Time=======================================================================*/ | |
54 | +struct timeval d2tval (double); | |
55 | +/*=pwd========================================================================*/ | |
56 | +int GetHomeDir (uid_t, char *, ssize_t); | |
57 | +/*=path=======================================================================*/ | |
58 | +ssize_t getLastPathBlock (const char *, char *, ssize_t); | |
59 | +int chkdir (const char *, mode_t); | |
60 | +int mkdirs (const char *, mode_t); | |
61 | +ssize_t rmPathLastBlock (char *); | |
62 | +ssize_t rmPathLastSlash (char *); | |
63 | +ssize_t packPathDupSlash (char *); | |
64 | +/*=log========================================================================*/ | |
65 | +int dateLog (char *, const int); | |
66 | +void writeLog (const char *, const char *, const int, const char *, ...); | |
67 | +void writeSystemLog (const u_int32_t, const char *, const char *, ...); | |
68 | + | |
69 | + | |
70 | + | |
71 | +#endif | |
72 | + |
@@ -0,0 +1,25 @@ | ||
1 | +/* include/config.h.in. Generated from configure.in by autoheader. */ | |
2 | + | |
3 | +/* Define to 1 if your C compiler doesn't accept -c and -o together. */ | |
4 | +#undef NO_MINUS_C_MINUS_O | |
5 | + | |
6 | +/* Name of package */ | |
7 | +#undef PACKAGE | |
8 | + | |
9 | +/* Define to the address where bug reports for this package should be sent. */ | |
10 | +#undef PACKAGE_BUGREPORT | |
11 | + | |
12 | +/* Define to the full name of this package. */ | |
13 | +#undef PACKAGE_NAME | |
14 | + | |
15 | +/* Define to the full name and version of this package. */ | |
16 | +#undef PACKAGE_STRING | |
17 | + | |
18 | +/* Define to the one symbol short name of this package. */ | |
19 | +#undef PACKAGE_TARNAME | |
20 | + | |
21 | +/* Define to the version of this package. */ | |
22 | +#undef PACKAGE_VERSION | |
23 | + | |
24 | +/* Version number of package */ | |
25 | +#undef VERSION |
@@ -0,0 +1 @@ | ||
1 | +Yuki SAKAI |
@@ -0,0 +1,58 @@ | ||
1 | +# -*- Autoconf -*- | |
2 | +# Process this file with autoconf to produce a configure script. | |
3 | + | |
4 | +AC_PREREQ([2.63]) | |
5 | +AC_INIT(bmpStation, 0.1.0, y.sakai@arteria-net.com) | |
6 | +AC_CONFIG_SRCDIR([bmpStation/bmpStationInt.h]) | |
7 | +AC_CONFIG_HEADERS([include/config.h]) | |
8 | +AC_PROG_RANLIB | |
9 | + | |
10 | +# Checks for programs. | |
11 | +AC_PROG_CC | |
12 | +AM_PROG_CC_C_O | |
13 | +AM_INIT_AUTOMAKE | |
14 | + | |
15 | +################################################################################ | |
16 | +# Default LIBS/Include | |
17 | +saved_LIBS="$LIBS" | |
18 | +saved_CFLAGS="$CFLAGS" | |
19 | +if test -d "/usr/local" ; then | |
20 | + LIBS="$saved_LIBS -L/usr/local/lib" ; saved_LIBS="$LIBS" | |
21 | + CFLAGS="$saved_CFLAGS -I/usr/local/include"; saved_CFLAGS="$CFLAGS" | |
22 | +fi | |
23 | +if test -d "/usr/pkg" ; then | |
24 | + LIBS="$saved_LIBS -L/usr/pkg/lib" ; saved_LIBS="$LIBS" | |
25 | + CFLAGS="$saved_CFLAGS -I/usr/pkg/include"; saved_CFLAGS="$CFLAGS" | |
26 | +fi | |
27 | + | |
28 | + | |
29 | +################################################################################ | |
30 | +# Checks for Options(enable) | |
31 | +dnl AC_ARG_ENABLE(debug, | |
32 | +dnl [ --enable-debug turn on debugging [[default=no]]], | |
33 | +dnl [\ | |
34 | +dnl case "${enableval}" in | |
35 | +dnl yes) enable_debug=yes ;; | |
36 | +dnl no) enable_debug=no ;; | |
37 | +dnl *) AC_MSG_ERROR(bad value for --enable-debug) ;; | |
38 | +dnl esac], | |
39 | +dnl enable_debug=no) | |
40 | +dnl if test x"${enable_debug}" = x"yes"; then | |
41 | +dnl AC_DEFINE(DEBUG, 1, [Define to 1 if you want to debug]) | |
42 | +dnl fi | |
43 | + | |
44 | +################################################################################ | |
45 | +# Enable and With process | |
46 | +dnl if test x"$with_gmp" != x"yes" ; then | |
47 | +dnl LIBS="$saved_LIBS -L$with_gmp/lib" ; saved_LIBS="$LIBS" | |
48 | +dnl CFLAGS="$saved_CFLAGS -I$with_gmp/include"; saved_CFLAGS="$CFLAGS" | |
49 | +dnl fi | |
50 | + | |
51 | +CPPFLAGS="$CFLAGS" | |
52 | +################################################################################ | |
53 | +AC_CONFIG_FILES([Makefile | |
54 | + libbgp/Makefile | |
55 | + libbmp/Makefile | |
56 | + lib/Makefile | |
57 | + bmpStation/Makefile]) | |
58 | +AC_OUTPUT |
@@ -0,0 +1,2 @@ | ||
1 | +version 0.1: 2014/10/27: 1st release | |
2 | + |
@@ -0,0 +1 @@ | ||
1 | +NEED libbgpdump | |
\ No newline at end of file |
@@ -0,0 +1,11 @@ | ||
1 | +noinst_LIBRARIES = libbgp.a | |
2 | +include_HEADERS = bgp.h | |
3 | +libbgp_a_SOURCES = bgp.h | |
4 | +libbgp_a_SOURCES += | |
5 | +libbgp_a_SOURCES += | |
6 | +libbgp_a_SOURCES += | |
7 | +libbgp_a_SOURCES += | |
8 | +libbgp_a_CFLAGS = -Wall -O2 | |
9 | +libbgp_a_CFLAGS += -I./ | |
10 | +libbgp_a_CFLAGS += | |
11 | +libbgp_a_CFLAGS += |
@@ -0,0 +1,166 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | +#if !defined(BGP_H__) | |
26 | +#define BGP_H__ 1 | |
27 | + | |
28 | +/******************************************************************************* | |
29 | + * include ********************************************************************/ | |
30 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
31 | +# include <sys/types.h> | |
32 | +#endif | |
33 | +#if !defined(_SYS_TIME_H) && !defined(_SYS_TIME_H_) | |
34 | +# include <sys/time.h> | |
35 | +#endif | |
36 | + | |
37 | +/******************************************************************************* | |
38 | + * Define *********************************************************************/ | |
39 | +#define LIBBGP_MAXLENGTH 4096 | |
40 | +#define LIBBGP_MAXMSGLEN 4096 | |
41 | +/*+==========================================================================+** | |
42 | + *| BGP Messeages Info |** | |
43 | + *+==========================================================================+*/ | |
44 | +/*=Messages type==============================================================*/ | |
45 | +#define BGP_MSGTYPE_OPEN 1 | |
46 | +#define BGP_MSGTYPE_UPDATE 2 | |
47 | +#define BGP_MSGTYPE_NOTIFICATION 3 | |
48 | +#define BGP_MSGTYPE_KEEPALIVE 4 | |
49 | +/* OLD */ | |
50 | +#define BGPMSGTYPE_OPEN 1 | |
51 | +#define BGPMSGTYPE_UPDATE 2 | |
52 | +#define BGPMSGTYPE_NOTIFICATION 3 | |
53 | +#define BGPMSGTYPE_KEEPALIVE 4 | |
54 | +/*=BGP Update path attribute flag=============================================*/ | |
55 | +#define BGP_UPDATE_PATHATTR_FLG_OPTIONAL 0x80 | |
56 | +#define BGP_UPDATE_PATHATTR_FLG_TRANSITIVE 0x40 | |
57 | +#define BGP_UPDATE_PATHATTR_FLG_PARTIAL 0x20 | |
58 | +/* extend flag is 0, next(3rd) octet is length. | |
59 | + * extend flag is 1, 3rd and 4th octet is length | |
60 | + */ | |
61 | +#define BGP_UPDATE_PATHATTR_FLG_EXTEND_LENGTH 0x10 | |
62 | +/*=BGP Update path attribute==================================================*/ | |
63 | +#define BGP_UPDATE_PATHATTR_TYPE_ORIGIN 1 | |
64 | +#define BGP_UPDATE_PATHATTR_TYPE_AS_PATH 2 | |
65 | +#define BGP_UPDATE_PATHATTR_TYPE_NEXT_HOP 3 | |
66 | +#define BGP_UPDATE_PATHATTR_TYPE_MED 4 | |
67 | +#define BGP_UPDATE_PATHATTR_TYPE_LOCAL_PREF 5 | |
68 | +#define BGP_UPDATE_PATHATTR_TYPE_ATOMIC_AGG 6 | |
69 | +#define BGP_UPDATE_PATHATTR_TYPE_AGGREGATOR 7 | |
70 | +#define BGP_UPDATE_PATHATTR_TYPE_COMMUNITY 8 | |
71 | +#define BGP_UPDATE_PATHATTR_TYPE_ORIGINATOR 9 | |
72 | +#define BGP_UPDATE_PATHATTR_TYPE_CLUSTER_LIST 10 | |
73 | + | |
74 | + | |
75 | +/******************************************************************************* | |
76 | + * Macro **********************************************************************/ | |
77 | + | |
78 | +/******************************************************************************* | |
79 | + * structure & typedef ********************************************************/ | |
80 | +/*+==========================================================================+** | |
81 | + *| BGP Messages Header |** | |
82 | + *+==========================================================================+*/ | |
83 | +/* BGP header common | |
84 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
85 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
86 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
87 | + * | Marker (16 byte) | | |
88 | + * | | | |
89 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
90 | + * | Length | msg Type | | |
91 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
92 | + ******************************************************************************/ | |
93 | +typedef struct { | |
94 | + u_int32_t Maker[4]; | |
95 | + u_int16_t length; | |
96 | + u_int8_t type; | |
97 | +} __attribute__((__packed__)) BGPCommonHeader; | |
98 | + | |
99 | +/* Open Messages | |
100 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
101 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
102 | + * +-+-+-+-+-+-+-+-+ | |
103 | + * | Version | | |
104 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
105 | + * | MY AS(sender) | | |
106 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
107 | + * | Hold Time | | |
108 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
109 | + * | BGP Identifier | | |
110 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
111 | + * | OPt Parm Len | | |
112 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
113 | + * | OPtional parameter (variable) | | |
114 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
115 | + ******************************************************************************/ | |
116 | +typedef struct { | |
117 | + u_int8_t version; | |
118 | + u_int16_t asn; | |
119 | + u_int16_t holdtime; | |
120 | + u_int32_t BGPid; | |
121 | + u_int8_t optLen; | |
122 | +} __attribute__((__packed__)) BGPOpenMessage; | |
123 | + | |
124 | + | |
125 | +/* BGP Update Messages | |
126 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
127 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
128 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
129 | + * | Withdrawn Routes Length | Withdrawn Routes (variable) | | |
130 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
131 | + * | Total path attribute length | path attribute (variable) | | |
132 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
133 | + * | NLRI (variable) | | |
134 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
135 | + ******************************************************************************/ | |
136 | +typedef u_int16_t BGPUpdateLength; /* for withdrawn routes or total path attribute length */ | |
137 | +/* BGP update path attributes | |
138 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
139 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
140 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
141 | + * | Attr. flags | Attr Type | length(1 or 2 Byte) | | |
142 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
143 | + ******************************************************************************/ | |
144 | +typedef struct { | |
145 | + u_int8_t attrFlag; | |
146 | + u_int8_t attrType; | |
147 | +} __attribute__((__packed__)) BGPUpdatePathAttr; | |
148 | + | |
149 | +#if 0 | |
150 | +typedef u_int8_t BGPUpdatePathAttr_Origin_Unit; | |
151 | +typedef u_int8_t BGPUpdatePathAttr_AsPath_Type; | |
152 | +typedef u_int16_t BGPUpdatePathAttr_AsPath_Unit; | |
153 | + | |
154 | +#define BGP_UPDATE_PATHATTR_TYPE_ORIGIN 1 | |
155 | +#define BGP_UPDATE_PATHATTR_TYPE_AS_PATH 2 | |
156 | +#define BGP_UPDATE_PATHATTR_TYPE_NEXT_HOP 3 | |
157 | +#define BGP_UPDATE_PATHATTR_TYPE_MED 4 | |
158 | +#define BGP_UPDATE_PATHATTR_TYPE_LOCAL_PREF 5 | |
159 | +#define BGP_UPDATE_PATHATTR_TYPE_ATOMIC_AGG 6 | |
160 | +#define BGP_UPDATE_PATHATTR_TYPE_AGGREGATOR 7 | |
161 | +#define BGP_UPDATE_PATHATTR_TYPE_COMMUNITY 8 | |
162 | +#define BGP_UPDATE_PATHATTR_TYPE_ORIGINATOR 9 | |
163 | +#define BGP_UPDATE_PATHATTR_TYPE_CLUSTER_LIST 10 | |
164 | +#endif | |
165 | + | |
166 | +#endif |
@@ -0,0 +1,221 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | + | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | +#if !defined(BMP_H__) | |
29 | +# include <bmp.h> | |
30 | +#endif | |
31 | +#if !defined(BGP_H__) | |
32 | +# include <bgp.h> | |
33 | +#endif | |
34 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
35 | +# include <sys/types.h> | |
36 | +#endif | |
37 | +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) | |
38 | +#if !defined(_SYS_ENDIAN_H) && !defined(_SYS_ENDIAN_H_) | |
39 | +# include <sys/endian.h> | |
40 | +#endif | |
41 | +#else | |
42 | +#if !defined(_ENDIAN_H) && !defined(_ENDIAN_H_) | |
43 | +# include <endian.h> | |
44 | +#endif | |
45 | +#endif | |
46 | + | |
47 | +/*+==========================================================================+** | |
48 | + *: check BMP messages size | |
49 | + *: arg: | |
50 | + *: rdata : bmp messages start point address. | |
51 | + *: maxDataLength: stored bmp messages max data size. | |
52 | + *: return: | |
53 | + *: plus value : BMP messages size. | |
54 | + *: zero : data is not enough size for bmp messages. | |
55 | + *: minus value: error | |
56 | + *+==========================================================================+*/ | |
57 | +ssize_t bmpMessageSizeCheck (u_int8_t *rdata, const ssize_t maxDataLength) | |
58 | +{ | |
59 | + ssize_t iHeaderLen; | |
60 | + ssize_t iMsgLen; | |
61 | + BMPv1Header *pV1Header; | |
62 | + BMPv3Header *pV3Header; | |
63 | + /*==========================================================================*/ | |
64 | + if (maxDataLength<1) {return 0;} | |
65 | + else if (*rdata == 1 || *rdata == 2) {iHeaderLen = sizeof (BMPv1Header);} | |
66 | + else if (*rdata == 3 ) {iHeaderLen = sizeof (BMPv3Header);} | |
67 | + else {return -1;} | |
68 | + /*==========================================================================*/ | |
69 | + if (maxDataLength<iHeaderLen) {return 0;} | |
70 | + else if (*rdata == 3) { | |
71 | + pV3Header = (BMPv3Header *)rdata; | |
72 | + iMsgLen = be16toh (pV3Header->msgLen); | |
73 | + } | |
74 | + else { | |
75 | + pV1Header = (BMPv1Header *)rdata; | |
76 | + switch (pV1Header->msgType) { | |
77 | + case BMP_MSGTYPE_ROUTEMON: | |
78 | + iMsgLen = bmpRouteMonSize ((u_int8_t *)(pV1Header+1), maxDataLength - iHeaderLen); | |
79 | + break; | |
80 | + case BMP_MSGTYPE_STATISTICS: | |
81 | + iMsgLen = bmpStatisticsSize ((u_int8_t *)(pV1Header+1), maxDataLength - iHeaderLen); | |
82 | + break; | |
83 | + case BMP_MSGTYPE_PEERDOWN: | |
84 | + iMsgLen = bmpPeerDownSize ((u_int8_t *)(pV1Header+1), maxDataLength - iHeaderLen); | |
85 | + break; | |
86 | + case BMP_MSGTYPE_PEERUP: | |
87 | + iMsgLen = bmpPeerUpSize ((u_int8_t *)(pV1Header+1), maxDataLength - iHeaderLen, pV1Header->version); | |
88 | + break; | |
89 | + default: return -1; | |
90 | + } | |
91 | + if (iMsgLen==0) {return 0;} | |
92 | + else if (iMsgLen <0) {return -1;} | |
93 | + iMsgLen += sizeof (BMPv1Header); | |
94 | + } | |
95 | + /*==========================================================================*/ | |
96 | + if (maxDataLength<iMsgLen) {return 0;} | |
97 | + return iMsgLen; | |
98 | +} | |
99 | + | |
100 | +/*+==========================================================================+** | |
101 | + *: check BMP PeerDown messages size | |
102 | + *: arg: | |
103 | + *: rdata : bmp peer down messages start point address. | |
104 | + *: maxDataLength: stored messages max data size. | |
105 | + *: return: | |
106 | + *: plus value : BMP peer down messages size. | |
107 | + *: zero : data is not enough size for bmp messages. | |
108 | + *: minus value: error | |
109 | + *+==========================================================================+*/ | |
110 | +ssize_t bmpPeerDownSize (u_int8_t *rdata, const ssize_t maxDataLength) | |
111 | +{ | |
112 | + int iSize; | |
113 | + BMPMsgPeerDown *pBMPPeerDownHeader; | |
114 | + BGPCommonHeader *pBGPHeader; | |
115 | + iSize = sizeof (BMPMsgPeerDown); | |
116 | + /*==========================================================================*/ | |
117 | + if (maxDataLength<iSize) {return 0;} | |
118 | + pBMPPeerDownHeader = (BMPMsgPeerDown *)rdata; | |
119 | + if (pBMPPeerDownHeader->reason==BMP_PEER_DOWN_REMOTE_NO_NOTIFICATION) {return iSize;} | |
120 | + else if (pBMPPeerDownHeader->reason==BMP_PEER_DOWN_LOCAL_NOTIFICATION ) {} | |
121 | + else if (pBMPPeerDownHeader->reason==BMP_PEER_DOWN_LOCAL_NO_NOTIFICATION ) {} | |
122 | + else if (pBMPPeerDownHeader->reason==BMP_PEER_DOWN_REMOTE_NOTIFICATION ) {} | |
123 | + else {return -1;} | |
124 | + /*==========================================================================*/ | |
125 | + pBGPHeader = (BGPCommonHeader *)(pBMPPeerDownHeader+1); | |
126 | + if (maxDataLength<(iSize+sizeof (BGPCommonHeader))) {return 0;} | |
127 | + iSize += be16toh (pBGPHeader->length); | |
128 | + if (maxDataLength<iSize) {return 0;} | |
129 | + return iSize; | |
130 | +} | |
131 | + | |
132 | +/*+==========================================================================+** | |
133 | + *: check BMP Peer Up messages size | |
134 | + *: arg: | |
135 | + *: rdata : bmp peer up messages start point address. | |
136 | + *: maxDataLength: stored messages max data size. | |
137 | + *: return: | |
138 | + *: plus value : BMP peer up messages size. | |
139 | + *: zero : data is not enough size for bmp messages. | |
140 | + *: minus value: error | |
141 | + *+==========================================================================+*/ | |
142 | +ssize_t bmpPeerUpSize (u_int8_t *rdata, const ssize_t maxDataLength, const u_int8_t ver) | |
143 | +{ | |
144 | + int iSize; | |
145 | + BGPCommonHeader *pBGPHeader; | |
146 | + if (ver==2) {} | |
147 | + else if (ver==3) {} | |
148 | + else {return -1;} | |
149 | + /*==========================================================================*/ | |
150 | + if (ver==2) { | |
151 | + pBGPHeader = (BGPCommonHeader *)rdata; | |
152 | + if (maxDataLength<sizeof (BGPCommonHeader)) {return 0;} | |
153 | + iSize = be16toh (pBGPHeader->length); | |
154 | + } | |
155 | + else if (ver==3) { | |
156 | + /* unknown */ | |
157 | + iSize = maxDataLength; | |
158 | + } | |
159 | + if (maxDataLength<iSize) {return 0;} | |
160 | + return iSize; | |
161 | +} | |
162 | + | |
163 | +/*+==========================================================================+** | |
164 | + *: check BMP Statistics messages size | |
165 | + *: arg: | |
166 | + *: rdata : bmp statistics messages start point address. | |
167 | + *: maxDataLength: stored messages max data size. | |
168 | + *: return: | |
169 | + *: plus value : BMP statistics messages size. | |
170 | + *: zero : data is not enough size for bmp messages. | |
171 | + *: minus value: error | |
172 | + *+==========================================================================+*/ | |
173 | +ssize_t bmpStatisticsSize (u_int8_t *rdata, const ssize_t maxDataLength) | |
174 | +{ | |
175 | + int i; | |
176 | + int iSize, iNum;; | |
177 | + u_int8_t *pTmp; | |
178 | + BMPMsgStatsNum *pStatsNum; | |
179 | + BMPMsgStatsHeader *pStatsHeader; | |
180 | + | |
181 | + iSize = sizeof (BMPMsgStatsNum); | |
182 | + /*==========================================================================*/ | |
183 | + if (maxDataLength<iSize) {return 0;} | |
184 | + pStatsNum = (BMPMsgStatsNum *)rdata; | |
185 | + iNum = be32toh (*pStatsNum); | |
186 | + for (i=0; i<iNum; i++) { | |
187 | + pTmp = rdata + iSize; | |
188 | + pStatsHeader = (BMPMsgStatsHeader *)pTmp; | |
189 | + /*========================================================================*/ | |
190 | + iSize += sizeof (BMPMsgStatsHeader); | |
191 | + if (maxDataLength<iSize) {return 0;} | |
192 | + /*========================================================================*/ | |
193 | + iSize += be16toh (pStatsHeader->statLen); | |
194 | + if (maxDataLength<iSize) {return 0;} | |
195 | + } | |
196 | + return iSize; | |
197 | +} | |
198 | + | |
199 | +/*+==========================================================================+** | |
200 | + *: check BMP route monitoring messages size | |
201 | + *: arg: | |
202 | + *: rdata : bmp route monitoring messages start point address. | |
203 | + *: maxDataLength: stored messages max data size. | |
204 | + *: return: | |
205 | + *: plus value : BMP route monitoring messages size. | |
206 | + *: zero : data is not enough size for bmp messages. | |
207 | + *: minus value: error | |
208 | + *+==========================================================================+*/ | |
209 | +ssize_t bmpRouteMonSize (u_int8_t *rdata, const ssize_t maxDataLength) | |
210 | +{ | |
211 | + int iSize; | |
212 | + BGPCommonHeader *pBGPHeader; | |
213 | + | |
214 | + iSize = sizeof (BGPCommonHeader); | |
215 | + /*==========================================================================*/ | |
216 | + if (maxDataLength<iSize) {return 0;} | |
217 | + pBGPHeader = (BGPCommonHeader *)rdata; | |
218 | + iSize = be16toh(pBGPHeader->length); | |
219 | + if (maxDataLength<iSize) {return 0;} | |
220 | + return iSize; | |
221 | +} |
@@ -0,0 +1,11 @@ | ||
1 | +noinst_LIBRARIES = libbmp.a | |
2 | +include_HEADERS = bmp.h | |
3 | +libbmp_a_SOURCES = bmp.h bmpSizeCheck.c bmpDataCheck.c | |
4 | +libbmp_a_SOURCES += | |
5 | +libbmp_a_SOURCES += | |
6 | +libbmp_a_SOURCES += | |
7 | +libbmp_a_SOURCES += | |
8 | +libbmp_a_CFLAGS = -Wall -O2 | |
9 | +libbmp_a_CFLAGS += -I./ -I../libbgp | |
10 | +libbmp_a_CFLAGS += | |
11 | +libbmp_a_CFLAGS += |
@@ -0,0 +1,56 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | + | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | +#if !defined(BMP_H__) | |
29 | +# include <bmp.h> | |
30 | +#endif | |
31 | +#if !defined(BGP_H__) | |
32 | +# include <bgp.h> | |
33 | +#endif | |
34 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
35 | +# include <sys/types.h> | |
36 | +#endif | |
37 | +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) | |
38 | +#if !defined(_SYS_ENDIAN_H) && !defined(_SYS_ENDIAN_H_) | |
39 | +# include <sys/endian.h> | |
40 | +#endif | |
41 | +#else | |
42 | +#if !defined(_ENDIAN_H) && !defined(_ENDIAN_H_) | |
43 | +# include <endian.h> | |
44 | +#endif | |
45 | +#endif | |
46 | + | |
47 | +/*+==========================================================================+** | |
48 | + *: check BMP messages size | |
49 | + *: arg: | |
50 | + *: rdata : bmp messages start point address. | |
51 | + *: maxDataLength: stored bmp messages max data size. | |
52 | + *: return: | |
53 | + *: plus value : BMP messages size. | |
54 | + *: zero : data is not enough size for bmp messages. | |
55 | + *: minus value: error | |
56 | + *+==========================================================================+*/ |
@@ -0,0 +1,341 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | +#if !defined(BMP_H__) | |
26 | +#define BMP_H__ 1 | |
27 | + | |
28 | +/******************************************************************************* | |
29 | + * include ********************************************************************/ | |
30 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
31 | +# include <sys/types.h> | |
32 | +#endif | |
33 | +#if !defined(_SYS_TIME_H) && !defined(_SYS_TIME_H_) | |
34 | +# include <sys/time.h> | |
35 | +#endif | |
36 | + | |
37 | +/******************************************************************************* | |
38 | + * Define *********************************************************************/ | |
39 | +/*=Parameter==================================================================*/ | |
40 | +#define LIBBMP_MAXHEADERLEN 100 /* 2 * (Header(6B)+PerPeerHeader(42B)+Buffer(2B)) */ | |
41 | +/*+==========================================================================+** | |
42 | + *| BMP Messeages Info |** | |
43 | + *+==========================================================================+*/ | |
44 | +#define BMP_VERSION_V1 1 | |
45 | +#define BMP_VERSION_V2 2 | |
46 | +#define BMP_VERSION_V3 3 | |
47 | +/*=Messages type==============================================================*/ | |
48 | +#define BMP_MSGTYPE_ROUTEMON 0 | |
49 | +#define BMP_MSGTYPE_STATISTICS 1 | |
50 | +#define BMP_MSGTYPE_PEERDOWN 2 | |
51 | +#define BMP_MSGTYPE_PEERUP 3 | |
52 | +#define BMP_MSGTYPE_INIT 4 | |
53 | +#define BMP_MSGTYPE_TERMINATE 5 | |
54 | +/*=Peer Type==================================================================*/ | |
55 | +#define BMP_PEERTYPE_GLOBAL 0 | |
56 | +#define BMP_PEERTYPE_L3VPN 1 | |
57 | +/*=Peer Flags=================================================================*/ | |
58 | +#define BMP_PEERFLG_VFLG_GET 0x80 | |
59 | +#define BMP_PEERFLG_VFLG_IPv6 0x80 | |
60 | +#define BMP_PEERFLG_VFLG_IPv4 0x00 | |
61 | +#define BMP_PEERFLG_LFLG_GET 0x40 | |
62 | +/* old */ | |
63 | +#define BMPPEERFLG_VFLG_GET 0x80 | |
64 | +#define BMPPEERFLG_LFLG_GET 0x40 | |
65 | +/*=Initiation=================================================================*/ | |
66 | +#define BMP_INITMSG_STRING 0 | |
67 | +#define BMP_INITMSG_SYSDESCR 1 | |
68 | +#define BMP_INITMSG_SYSNAME 2 | |
69 | +/* old */ | |
70 | +#define BMPINITMSG_STRING 0 | |
71 | +#define BMPINITMSG_SYSDESCR 1 | |
72 | +#define BMPINITMSG_SYSNAME 2 | |
73 | +/*=Termination================================================================*/ | |
74 | +#define BMP_TERMMSG_STRING 0 | |
75 | +#define BMP_TERMMSG_REASON 1 | |
76 | +#define BMP_TERM_REASON_ADMIN_CLOSE 0 | |
77 | +#define BMP_TERM_REASON_UNSPECIFIED 1 | |
78 | +#define BMP_TERM_REASON_OUT_OF_RESOURCE 2 | |
79 | +#define BMP_TERM_REASON_REDUNDANT 3 | |
80 | +/* old */ | |
81 | +#define BMPTERMMSG_STRING 0 | |
82 | +#define BMPTERMMSG_REASON 1 | |
83 | +/*=Statistics=================================================================*/ | |
84 | +#define BMP_STATS_TYPE_REJECT_BY_INBOUND_POLICY 0x00 /* Number of prefixes rejected by inbound policy. */ | |
85 | +#define BMP_STATS_TYPE_DUPLICATE_ADVERTISEMENTS 0x01 /* Number of (known) duplicate prefix. */ | |
86 | +#define BMP_STATS_TYPE_DUPLICATE_WITHDRAWS 0x02 /* Number of (known) duplicate withdraws. */ | |
87 | +#define BMP_STATS_TYPE_UPDATES_INVALIDATED_CLUSTER 0x03 /* Number of updates invalidated due to CLUSTER_LIST loop. */ | |
88 | +#define BMP_STATS_TYPE_UPDATES_INVALIDATED_AS_PATH 0x04 /* Number of updates invalidated due to AS_PATH loop. */ | |
89 | +#define BMP_STATS_TYPE_UPDATES_INVALIDATED_ORIGIN 0x05 /* Number of updates invalidated due to ORIGINATOR_ID. */ | |
90 | +#define BMP_STATS_TYPE_UPDATES_INVALIDATED_AS_CONF 0x06 /* Number of updates invalidated due to AS_CONFED loop. */ | |
91 | +#define BMP_STATS_TYPE_ROUTES_ADJ_RIBs_IN 0x07 /* Number of routes in Adj-RIBs-In. */ | |
92 | +#define BMP_STATS_TYPE_ROUTES_LOC_RIB 0x08 /* Number of routes in Loc-RIB. */ | |
93 | +/* old */ | |
94 | +#define BMPSTATSTYPE_REJECT_BY_INBOUND_POLICY 0x00 /* Number of prefixes rejected by inbound policy. */ | |
95 | +#define BMPSTATSTYPE_DUPLICATE_ADVERTISEMENTS 0x01 /* Number of (known) duplicate prefix. */ | |
96 | +#define BMPSTATSTYPE_DUPLICATE_WITHDRAWS 0x02 /* Number of (known) duplicate withdraws. */ | |
97 | +#define BMPSTATSTYPE_UPDATES_INVALIDATED_CLUSTER 0x03 /* Number of updates invalidated due to CLUSTER_LIST loop. */ | |
98 | +#define BMPSTATSTYPE_UPDATES_INVALIDATED_AS_PATH 0x04 /* Number of updates invalidated due to AS_PATH loop. */ | |
99 | +#define BMPSTATSTYPE_UPDATES_INVALIDATED_ORIGIN 0x05 /* Number of updates invalidated due to ORIGINATOR_ID. */ | |
100 | +#define BMPSTATSTYPE_UPDATES_INVALIDATED_AS_CONF 0x06 /* Number of updates invalidated due to AS_CONFED loop. */ | |
101 | +#define BMPSTATSTYPE_ROUTES_ADJ_RIBs_IN 0x07 /* Number of routes in Adj-RIBs-In. */ | |
102 | +#define BMPSTATSTYPE_ROUTES_LOC_RIB 0x08 /* Number of routes in Loc-RIB. */ | |
103 | +/*=Peer Down==================================================================*/ | |
104 | +#define BMP_PEER_DOWN_LOCAL_NOTIFICATION 1 | |
105 | +#define BMP_PEER_DOWN_LOCAL_NO_NOTIFICATION 2 | |
106 | +#define BMP_PEER_DOWN_REMOTE_NOTIFICATION 3 | |
107 | +#define BMP_PEER_DOWN_REMOTE_NO_NOTIFICATION 4 | |
108 | +/* old */ | |
109 | +#define BMPPEERDOWN_LOCALCLOSE 1 | |
110 | +#define BMPPEERDOWN_ | |
111 | + | |
112 | + | |
113 | + | |
114 | +/******************************************************************************* | |
115 | + * Macro **********************************************************************/ | |
116 | + | |
117 | +/******************************************************************************* | |
118 | + * structure & typedef ********************************************************/ | |
119 | +/*+==========================================================================+** | |
120 | + *| BMP Messages Header |** | |
121 | + *+==========================================================================+*/ | |
122 | +/* Version: 1,2 (Header) | |
123 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
124 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
125 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
126 | + * | Version | msg type | peer type | peer flags | | |
127 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
128 | + * | Peer Distinguisher (8 Byte) | | |
129 | + * | | | |
130 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
131 | + * | Peer Address (16 Byte) | | |
132 | + * | | | |
133 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
134 | + * | Peer AS | | |
135 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
136 | + * | Peer BGP ID | | |
137 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
138 | + * | Timestamp (seconds) | | |
139 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
140 | + * | Timestamp (microseconds) | | |
141 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
142 | + * | messages(variables: Routemon, statistics, peer down, peer up) | | |
143 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
144 | + ******************************************************************************/ | |
145 | +typedef struct { | |
146 | + u_int8_t version; | |
147 | + u_int8_t msgType; | |
148 | + u_int8_t peerType; | |
149 | + u_int8_t peerFlags; | |
150 | + u_int32_t peerDist[2]; | |
151 | + u_int32_t peerAddr[4]; | |
152 | + u_int32_t peerAS; | |
153 | + u_int32_t peerBgpID; | |
154 | + /* which user "struct timeval"? "u_int32_t"? */ | |
155 | + u_int32_t tv_sec; | |
156 | + u_int32_t tv_usec; | |
157 | +} __attribute__((__packed__)) BMPv1Header; | |
158 | +#define BMPV1MSGTYPE_ROUTEMON 0 | |
159 | +#define BMPV1MSGTYPE_STATISTICS 1 | |
160 | +#define BMPV1MSGTYPE_PEERDOWN 2 | |
161 | +#define BMPV1MSGTYPE_PEERUP 3 | |
162 | +#define BMPV1PEERTYPE_GLOBAL 0 | |
163 | +#define BMPV1PEERTYPE_L3VPN 1 | |
164 | +#define BMPV1PEERFLG_VFLG_IPv6 0x80 | |
165 | +#define BMPV1PEERFLG_VFLG_IPv4 0x00 | |
166 | + | |
167 | +/* Version: 3 (Header) | |
168 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
169 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
170 | + * +-+-+-+-+-+-+-+-+ | |
171 | + * | Version | | |
172 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
173 | + * | msg length | | |
174 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
175 | + * | msg type | | |
176 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
177 | + * | Initiation, Termination, PerPeerHeader(variables) | | |
178 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
179 | + ******************************************************************************/ | |
180 | +/* Version: 3 */ | |
181 | +typedef struct { | |
182 | + u_int8_t version; | |
183 | + u_int32_t msgLen; | |
184 | + u_int8_t msgType; | |
185 | +} __attribute__((__packed__)) BMPv3Header; | |
186 | +#define BMPV3MSGTYPE_ROUTEMON 0 | |
187 | +#define BMPV3MSGTYPE_STATISTICS 1 | |
188 | +#define BMPV3MSGTYPE_PEERDOWN 2 | |
189 | +#define BMPV3MSGTYPE_PEERUP 3 | |
190 | +#define BMPV3MSGTYPE_INIT 4 | |
191 | +#define BMPV3MSGTYPE_TERMINATE 5 | |
192 | + | |
193 | +/* Version: 3 (Per Peer Header) | |
194 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
195 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
196 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
197 | + * | peer type | peer flags | | |
198 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
199 | + * | Peer Distinguisher (8 Byte) | | |
200 | + * | | | |
201 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
202 | + * | Peer Address (16 Byte) | | |
203 | + * | | | |
204 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
205 | + * | Peer AS | | |
206 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
207 | + * | Peer BGP ID | | |
208 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
209 | + * | Timestamp (seconds) | | |
210 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
211 | + * | Timestamp (microseconds) | | |
212 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
213 | + * | messages(variables: Routemon, statistics, peer down, peer up) | | |
214 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
215 | + ******************************************************************************/ | |
216 | +typedef struct { | |
217 | + u_int8_t peerType; | |
218 | + u_int8_t peerFlags; | |
219 | + u_int32_t peerDist[2]; | |
220 | + u_int32_t peerAddr[4]; | |
221 | + u_int32_t peerAS; | |
222 | + u_int32_t peerBgpID; | |
223 | + /* which user "struct timeval"? "u_int32_t"? */ | |
224 | + u_int32_t tv_sec; | |
225 | + u_int32_t tv_usec; | |
226 | +} __attribute__((__packed__)) BMPv3PerPeerHeader; | |
227 | +#define BMPV3PEERTYPE_GLOBAL 0 | |
228 | +#define BMPV3PEERTYPE_L3VPN 1 | |
229 | +#define BMPV3PEERFLG_VFLG_IPv6 0x80 | |
230 | +#define BMPV3PEERFLG_VFLG_IPv4 0x00 | |
231 | + | |
232 | + | |
233 | +/*+==========================================================================+** | |
234 | + *| BMP Messages |** | |
235 | + *+==========================================================================+*/ | |
236 | +/*=Initiation Message=========================================================** | |
237 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
238 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
239 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
240 | + * | Information type | Information length | | |
241 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
242 | + * | Information (variables) | | |
243 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
244 | + ******************************************************************************/ | |
245 | +typedef struct { | |
246 | + u_int16_t infoType; | |
247 | + u_int16_t infoLen; | |
248 | +} __attribute__((__packed__)) BMPMsgInit; | |
249 | + | |
250 | +/*=Termination Message========================================================** | |
251 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
252 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
253 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
254 | + * | Information type | Information length | | |
255 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
256 | + * | Information (variables) | | |
257 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
258 | + ******************************************************************************/ | |
259 | +typedef struct { | |
260 | + u_int16_t infoType; | |
261 | + u_int16_t infoLen; | |
262 | +} __attribute__((__packed__)) BMPMsgTerminate; | |
263 | + | |
264 | +/*=Statistics Reports=========================================================** | |
265 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
266 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
267 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
268 | + * | number of Statistics item | | |
269 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
270 | + * | statistics messages(variables) | | |
271 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
272 | + * | |
273 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
274 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
275 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
276 | + * | Statistics type | Statistics length | | |
277 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
278 | + * | statistics data(variables) | | |
279 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
280 | + ******************************************************************************/ | |
281 | +typedef u_int32_t BMPMsgStatsNum; | |
282 | +typedef struct { | |
283 | + u_int16_t statType; | |
284 | + u_int16_t statLen; | |
285 | +} __attribute__((__packed__)) BMPMsgStatsHeader; | |
286 | +typedef union { | |
287 | + u_int32_t count32; | |
288 | + u_int64_t count64; | |
289 | +} BMPMsgStatsData; | |
290 | + | |
291 | +/*=Peer Down Notification=====================================================** | |
292 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
293 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
294 | + * +-+-+-+-+-+-+-+-+ | |
295 | + * | reason | | |
296 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
297 | + * | statistics messages(variables) | | |
298 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
299 | + ******************************************************************************/ | |
300 | +typedef struct { | |
301 | + u_int8_t reason; | |
302 | +} __attribute__((__packed__)) BMPMsgPeerDown; | |
303 | + | |
304 | +/*=Peer Up Notification=======================================================** | |
305 | + * 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | |
306 | + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
307 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
308 | + * | Local Address (16 Byte) | | |
309 | + * | | | |
310 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
311 | + * | Local Port | Remote Port | | |
312 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
313 | + * | Send OPEN Messages(variables) | | |
314 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
315 | + * | Received OPEN Messages(variables) | | |
316 | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
317 | + * | |
318 | + * version 2's peer up message is BGP Open messages | |
319 | + ******************************************************************************/ | |
320 | +typedef struct { | |
321 | + u_int32_t localAddr[4]; | |
322 | + u_int16_t localPort; | |
323 | + u_int16_t peerPort; | |
324 | +} __attribute__((__packed__)) BMPMsgPeerUp; | |
325 | + | |
326 | +/******************************************************************************* | |
327 | + * functions ******************************************************************/ | |
328 | +/*=bmpSizeCheck===============================================================*/ | |
329 | +ssize_t bmpMessageSizeCheck (u_int8_t *, const ssize_t); | |
330 | +ssize_t bmpPeerDownSize (u_int8_t *, const ssize_t); | |
331 | +ssize_t bmpPeerUpSize (u_int8_t *, const ssize_t, const u_int8_t); | |
332 | +ssize_t bmpStatisticsSize (u_int8_t *, const ssize_t); | |
333 | +ssize_t bmpRouteMonSize (u_int8_t *, const ssize_t); | |
334 | +/*=bmpDataCheck===============================================================*/ | |
335 | +int bmpMessageTypeCheck (const u_int8_t, const u_int8_t); | |
336 | +int bmpPeerTypeCheck (const u_int8_t, const u_int8_t); | |
337 | + | |
338 | +#endif | |
339 | + | |
340 | + | |
341 | + |
@@ -0,0 +1,96 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | + | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | +#if !defined(BMP_H__) | |
29 | +# include <bmp.h> | |
30 | +#endif | |
31 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
32 | +# include <sys/types.h> | |
33 | +#endif | |
34 | +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) | |
35 | +#if !defined(_SYS_ENDIAN_H) && !defined(_SYS_ENDIAN_H_) | |
36 | +# include <sys/endian.h> | |
37 | +#endif | |
38 | +#else | |
39 | +#if !defined(_ENDIAN_H) && !defined(_ENDIAN_H_) | |
40 | +# include <endian.h> | |
41 | +#endif | |
42 | +#endif | |
43 | + | |
44 | +/*+==========================================================================+** | |
45 | + *: check BMP messages type check | |
46 | + *: arg: | |
47 | + *: version : bmp Version | |
48 | + *: msgType : bmp Message type code | |
49 | + *: return: | |
50 | + *: 1: O.K. | |
51 | + *: 0: Bad | |
52 | + *+==========================================================================+*/ | |
53 | +int bmpMessageTypeCheck (const u_int8_t version, const u_int8_t msgType) | |
54 | +{ | |
55 | + switch (msgType) { | |
56 | + case BMP_MSGTYPE_ROUTEMON: | |
57 | + case BMP_MSGTYPE_STATISTICS: | |
58 | + case BMP_MSGTYPE_PEERDOWN: | |
59 | + return 1; | |
60 | + break; | |
61 | + case BMP_MSGTYPE_PEERUP: | |
62 | + if (version==BMP_VERSION_V2 || | |
63 | + version==BMP_VERSION_V3) {return 1;} | |
64 | + break; | |
65 | + case BMP_MSGTYPE_INIT: | |
66 | + case BMP_MSGTYPE_TERMINATE: | |
67 | + if (version==BMP_VERSION_V3) {return 1;} | |
68 | + break; | |
69 | + default: | |
70 | + break; | |
71 | + } | |
72 | + return 0; | |
73 | +} | |
74 | + | |
75 | +/*+==========================================================================+** | |
76 | + *: check BMP peer type check | |
77 | + *: arg: | |
78 | + *: version : bmp Version | |
79 | + *: peerType: bmp Peer type code | |
80 | + *: return: | |
81 | + *: 1: O.K. | |
82 | + *: 0: Bad | |
83 | + *+==========================================================================+*/ | |
84 | +int bmpPeerTypeCheck (const u_int8_t version, const u_int8_t peerType) | |
85 | +{ | |
86 | + switch (peerType) { | |
87 | + case BMP_PEERTYPE_GLOBAL: | |
88 | + case BMP_PEERTYPE_L3VPN: | |
89 | + return 1; | |
90 | + break; | |
91 | + default: | |
92 | + break; | |
93 | + } | |
94 | + return 0; | |
95 | +} | |
96 | + |
@@ -0,0 +1,365 @@ | ||
1 | +Installation Instructions | |
2 | +************************* | |
3 | + | |
4 | +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, | |
5 | +2006, 2007, 2008, 2009 Free Software Foundation, Inc. | |
6 | + | |
7 | + Copying and distribution of this file, with or without modification, | |
8 | +are permitted in any medium without royalty provided the copyright | |
9 | +notice and this notice are preserved. This file is offered as-is, | |
10 | +without warranty of any kind. | |
11 | + | |
12 | +Basic Installation | |
13 | +================== | |
14 | + | |
15 | + Briefly, the shell commands `./configure; make; make install' should | |
16 | +configure, build, and install this package. The following | |
17 | +more-detailed instructions are generic; see the `README' file for | |
18 | +instructions specific to this package. Some packages provide this | |
19 | +`INSTALL' file but do not implement all of the features documented | |
20 | +below. The lack of an optional feature in a given package is not | |
21 | +necessarily a bug. More recommendations for GNU packages can be found | |
22 | +in *note Makefile Conventions: (standards)Makefile Conventions. | |
23 | + | |
24 | + The `configure' shell script attempts to guess correct values for | |
25 | +various system-dependent variables used during compilation. It uses | |
26 | +those values to create a `Makefile' in each directory of the package. | |
27 | +It may also create one or more `.h' files containing system-dependent | |
28 | +definitions. Finally, it creates a shell script `config.status' that | |
29 | +you can run in the future to recreate the current configuration, and a | |
30 | +file `config.log' containing compiler output (useful mainly for | |
31 | +debugging `configure'). | |
32 | + | |
33 | + It can also use an optional file (typically called `config.cache' | |
34 | +and enabled with `--cache-file=config.cache' or simply `-C') that saves | |
35 | +the results of its tests to speed up reconfiguring. Caching is | |
36 | +disabled by default to prevent problems with accidental use of stale | |
37 | +cache files. | |
38 | + | |
39 | + If you need to do unusual things to compile the package, please try | |
40 | +to figure out how `configure' could check whether to do them, and mail | |
41 | +diffs or instructions to the address given in the `README' so they can | |
42 | +be considered for the next release. If you are using the cache, and at | |
43 | +some point `config.cache' contains results you don't want to keep, you | |
44 | +may remove or edit it. | |
45 | + | |
46 | + The file `configure.ac' (or `configure.in') is used to create | |
47 | +`configure' by a program called `autoconf'. You need `configure.ac' if | |
48 | +you want to change it or regenerate `configure' using a newer version | |
49 | +of `autoconf'. | |
50 | + | |
51 | + The simplest way to compile this package is: | |
52 | + | |
53 | + 1. `cd' to the directory containing the package's source code and type | |
54 | + `./configure' to configure the package for your system. | |
55 | + | |
56 | + Running `configure' might take a while. While running, it prints | |
57 | + some messages telling which features it is checking for. | |
58 | + | |
59 | + 2. Type `make' to compile the package. | |
60 | + | |
61 | + 3. Optionally, type `make check' to run any self-tests that come with | |
62 | + the package, generally using the just-built uninstalled binaries. | |
63 | + | |
64 | + 4. Type `make install' to install the programs and any data files and | |
65 | + documentation. When installing into a prefix owned by root, it is | |
66 | + recommended that the package be configured and built as a regular | |
67 | + user, and only the `make install' phase executed with root | |
68 | + privileges. | |
69 | + | |
70 | + 5. Optionally, type `make installcheck' to repeat any self-tests, but | |
71 | + this time using the binaries in their final installed location. | |
72 | + This target does not install anything. Running this target as a | |
73 | + regular user, particularly if the prior `make install' required | |
74 | + root privileges, verifies that the installation completed | |
75 | + correctly. | |
76 | + | |
77 | + 6. You can remove the program binaries and object files from the | |
78 | + source code directory by typing `make clean'. To also remove the | |
79 | + files that `configure' created (so you can compile the package for | |
80 | + a different kind of computer), type `make distclean'. There is | |
81 | + also a `make maintainer-clean' target, but that is intended mainly | |
82 | + for the package's developers. If you use it, you may have to get | |
83 | + all sorts of other programs in order to regenerate files that came | |
84 | + with the distribution. | |
85 | + | |
86 | + 7. Often, you can also type `make uninstall' to remove the installed | |
87 | + files again. In practice, not all packages have tested that | |
88 | + uninstallation works correctly, even though it is required by the | |
89 | + GNU Coding Standards. | |
90 | + | |
91 | + 8. Some packages, particularly those that use Automake, provide `make | |
92 | + distcheck', which can by used by developers to test that all other | |
93 | + targets like `make install' and `make uninstall' work correctly. | |
94 | + This target is generally not run by end users. | |
95 | + | |
96 | +Compilers and Options | |
97 | +===================== | |
98 | + | |
99 | + Some systems require unusual options for compilation or linking that | |
100 | +the `configure' script does not know about. Run `./configure --help' | |
101 | +for details on some of the pertinent environment variables. | |
102 | + | |
103 | + You can give `configure' initial values for configuration parameters | |
104 | +by setting variables in the command line or in the environment. Here | |
105 | +is an example: | |
106 | + | |
107 | + ./configure CC=c99 CFLAGS=-g LIBS=-lposix | |
108 | + | |
109 | + *Note Defining Variables::, for more details. | |
110 | + | |
111 | +Compiling For Multiple Architectures | |
112 | +==================================== | |
113 | + | |
114 | + You can compile the package for more than one kind of computer at the | |
115 | +same time, by placing the object files for each architecture in their | |
116 | +own directory. To do this, you can use GNU `make'. `cd' to the | |
117 | +directory where you want the object files and executables to go and run | |
118 | +the `configure' script. `configure' automatically checks for the | |
119 | +source code in the directory that `configure' is in and in `..'. This | |
120 | +is known as a "VPATH" build. | |
121 | + | |
122 | + With a non-GNU `make', it is safer to compile the package for one | |
123 | +architecture at a time in the source code directory. After you have | |
124 | +installed the package for one architecture, use `make distclean' before | |
125 | +reconfiguring for another architecture. | |
126 | + | |
127 | + On MacOS X 10.5 and later systems, you can create libraries and | |
128 | +executables that work on multiple system types--known as "fat" or | |
129 | +"universal" binaries--by specifying multiple `-arch' options to the | |
130 | +compiler but only a single `-arch' option to the preprocessor. Like | |
131 | +this: | |
132 | + | |
133 | + ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ | |
134 | + CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ | |
135 | + CPP="gcc -E" CXXCPP="g++ -E" | |
136 | + | |
137 | + This is not guaranteed to produce working output in all cases, you | |
138 | +may have to build one architecture at a time and combine the results | |
139 | +using the `lipo' tool if you have problems. | |
140 | + | |
141 | +Installation Names | |
142 | +================== | |
143 | + | |
144 | + By default, `make install' installs the package's commands under | |
145 | +`/usr/local/bin', include files under `/usr/local/include', etc. You | |
146 | +can specify an installation prefix other than `/usr/local' by giving | |
147 | +`configure' the option `--prefix=PREFIX', where PREFIX must be an | |
148 | +absolute file name. | |
149 | + | |
150 | + You can specify separate installation prefixes for | |
151 | +architecture-specific files and architecture-independent files. If you | |
152 | +pass the option `--exec-prefix=PREFIX' to `configure', the package uses | |
153 | +PREFIX as the prefix for installing programs and libraries. | |
154 | +Documentation and other data files still use the regular prefix. | |
155 | + | |
156 | + In addition, if you use an unusual directory layout you can give | |
157 | +options like `--bindir=DIR' to specify different values for particular | |
158 | +kinds of files. Run `configure --help' for a list of the directories | |
159 | +you can set and what kinds of files go in them. In general, the | |
160 | +default for these options is expressed in terms of `${prefix}', so that | |
161 | +specifying just `--prefix' will affect all of the other directory | |
162 | +specifications that were not explicitly provided. | |
163 | + | |
164 | + The most portable way to affect installation locations is to pass the | |
165 | +correct locations to `configure'; however, many packages provide one or | |
166 | +both of the following shortcuts of passing variable assignments to the | |
167 | +`make install' command line to change installation locations without | |
168 | +having to reconfigure or recompile. | |
169 | + | |
170 | + The first method involves providing an override variable for each | |
171 | +affected directory. For example, `make install | |
172 | +prefix=/alternate/directory' will choose an alternate location for all | |
173 | +directory configuration variables that were expressed in terms of | |
174 | +`${prefix}'. Any directories that were specified during `configure', | |
175 | +but not in terms of `${prefix}', must each be overridden at install | |
176 | +time for the entire installation to be relocated. The approach of | |
177 | +makefile variable overrides for each directory variable is required by | |
178 | +the GNU Coding Standards, and ideally causes no recompilation. | |
179 | +However, some platforms have known limitations with the semantics of | |
180 | +shared libraries that end up requiring recompilation when using this | |
181 | +method, particularly noticeable in packages that use GNU Libtool. | |
182 | + | |
183 | + The second method involves providing the `DESTDIR' variable. For | |
184 | +example, `make install DESTDIR=/alternate/directory' will prepend | |
185 | +`/alternate/directory' before all installation names. The approach of | |
186 | +`DESTDIR' overrides is not required by the GNU Coding Standards, and | |
187 | +does not work on platforms that have drive letters. On the other hand, | |
188 | +it does better at avoiding recompilation issues, and works well even | |
189 | +when some directory options were not specified in terms of `${prefix}' | |
190 | +at `configure' time. | |
191 | + | |
192 | +Optional Features | |
193 | +================= | |
194 | + | |
195 | + If the package supports it, you can cause programs to be installed | |
196 | +with an extra prefix or suffix on their names by giving `configure' the | |
197 | +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. | |
198 | + | |
199 | + Some packages pay attention to `--enable-FEATURE' options to | |
200 | +`configure', where FEATURE indicates an optional part of the package. | |
201 | +They may also pay attention to `--with-PACKAGE' options, where PACKAGE | |
202 | +is something like `gnu-as' or `x' (for the X Window System). The | |
203 | +`README' should mention any `--enable-' and `--with-' options that the | |
204 | +package recognizes. | |
205 | + | |
206 | + For packages that use the X Window System, `configure' can usually | |
207 | +find the X include and library files automatically, but if it doesn't, | |
208 | +you can use the `configure' options `--x-includes=DIR' and | |
209 | +`--x-libraries=DIR' to specify their locations. | |
210 | + | |
211 | + Some packages offer the ability to configure how verbose the | |
212 | +execution of `make' will be. For these packages, running `./configure | |
213 | +--enable-silent-rules' sets the default to minimal output, which can be | |
214 | +overridden with `make V=1'; while running `./configure | |
215 | +--disable-silent-rules' sets the default to verbose, which can be | |
216 | +overridden with `make V=0'. | |
217 | + | |
218 | +Particular systems | |
219 | +================== | |
220 | + | |
221 | + On HP-UX, the default C compiler is not ANSI C compatible. If GNU | |
222 | +CC is not installed, it is recommended to use the following options in | |
223 | +order to use an ANSI C compiler: | |
224 | + | |
225 | + ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" | |
226 | + | |
227 | +and if that doesn't work, install pre-built binaries of GCC for HP-UX. | |
228 | + | |
229 | + On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot | |
230 | +parse its `<wchar.h>' header file. The option `-nodtk' can be used as | |
231 | +a workaround. If GNU CC is not installed, it is therefore recommended | |
232 | +to try | |
233 | + | |
234 | + ./configure CC="cc" | |
235 | + | |
236 | +and if that doesn't work, try | |
237 | + | |
238 | + ./configure CC="cc -nodtk" | |
239 | + | |
240 | + On Solaris, don't put `/usr/ucb' early in your `PATH'. This | |
241 | +directory contains several dysfunctional programs; working variants of | |
242 | +these programs are available in `/usr/bin'. So, if you need `/usr/ucb' | |
243 | +in your `PATH', put it _after_ `/usr/bin'. | |
244 | + | |
245 | + On Haiku, software installed for all users goes in `/boot/common', | |
246 | +not `/usr/local'. It is recommended to use the following options: | |
247 | + | |
248 | + ./configure --prefix=/boot/common | |
249 | + | |
250 | +Specifying the System Type | |
251 | +========================== | |
252 | + | |
253 | + There may be some features `configure' cannot figure out | |
254 | +automatically, but needs to determine by the type of machine the package | |
255 | +will run on. Usually, assuming the package is built to be run on the | |
256 | +_same_ architectures, `configure' can figure that out, but if it prints | |
257 | +a message saying it cannot guess the machine type, give it the | |
258 | +`--build=TYPE' option. TYPE can either be a short name for the system | |
259 | +type, such as `sun4', or a canonical name which has the form: | |
260 | + | |
261 | + CPU-COMPANY-SYSTEM | |
262 | + | |
263 | +where SYSTEM can have one of these forms: | |
264 | + | |
265 | + OS | |
266 | + KERNEL-OS | |
267 | + | |
268 | + See the file `config.sub' for the possible values of each field. If | |
269 | +`config.sub' isn't included in this package, then this package doesn't | |
270 | +need to know the machine type. | |
271 | + | |
272 | + If you are _building_ compiler tools for cross-compiling, you should | |
273 | +use the option `--target=TYPE' to select the type of system they will | |
274 | +produce code for. | |
275 | + | |
276 | + If you want to _use_ a cross compiler, that generates code for a | |
277 | +platform different from the build platform, you should specify the | |
278 | +"host" platform (i.e., that on which the generated programs will | |
279 | +eventually be run) with `--host=TYPE'. | |
280 | + | |
281 | +Sharing Defaults | |
282 | +================ | |
283 | + | |
284 | + If you want to set default values for `configure' scripts to share, | |
285 | +you can create a site shell script called `config.site' that gives | |
286 | +default values for variables like `CC', `cache_file', and `prefix'. | |
287 | +`configure' looks for `PREFIX/share/config.site' if it exists, then | |
288 | +`PREFIX/etc/config.site' if it exists. Or, you can set the | |
289 | +`CONFIG_SITE' environment variable to the location of the site script. | |
290 | +A warning: not all `configure' scripts look for a site script. | |
291 | + | |
292 | +Defining Variables | |
293 | +================== | |
294 | + | |
295 | + Variables not defined in a site shell script can be set in the | |
296 | +environment passed to `configure'. However, some packages may run | |
297 | +configure again during the build, and the customized values of these | |
298 | +variables may be lost. In order to avoid this problem, you should set | |
299 | +them in the `configure' command line, using `VAR=value'. For example: | |
300 | + | |
301 | + ./configure CC=/usr/local2/bin/gcc | |
302 | + | |
303 | +causes the specified `gcc' to be used as the C compiler (unless it is | |
304 | +overridden in the site shell script). | |
305 | + | |
306 | +Unfortunately, this technique does not work for `CONFIG_SHELL' due to | |
307 | +an Autoconf bug. Until the bug is fixed you can use this workaround: | |
308 | + | |
309 | + CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash | |
310 | + | |
311 | +`configure' Invocation | |
312 | +====================== | |
313 | + | |
314 | + `configure' recognizes the following options to control how it | |
315 | +operates. | |
316 | + | |
317 | +`--help' | |
318 | +`-h' | |
319 | + Print a summary of all of the options to `configure', and exit. | |
320 | + | |
321 | +`--help=short' | |
322 | +`--help=recursive' | |
323 | + Print a summary of the options unique to this package's | |
324 | + `configure', and exit. The `short' variant lists options used | |
325 | + only in the top level, while the `recursive' variant lists options | |
326 | + also present in any nested packages. | |
327 | + | |
328 | +`--version' | |
329 | +`-V' | |
330 | + Print the version of Autoconf used to generate the `configure' | |
331 | + script, and exit. | |
332 | + | |
333 | +`--cache-file=FILE' | |
334 | + Enable the cache: use and save the results of the tests in FILE, | |
335 | + traditionally `config.cache'. FILE defaults to `/dev/null' to | |
336 | + disable caching. | |
337 | + | |
338 | +`--config-cache' | |
339 | +`-C' | |
340 | + Alias for `--cache-file=config.cache'. | |
341 | + | |
342 | +`--quiet' | |
343 | +`--silent' | |
344 | +`-q' | |
345 | + Do not print messages saying which checks are being made. To | |
346 | + suppress all normal output, redirect it to `/dev/null' (any error | |
347 | + messages will still be shown). | |
348 | + | |
349 | +`--srcdir=DIR' | |
350 | + Look for the package's source code in directory DIR. Usually | |
351 | + `configure' can determine that directory automatically. | |
352 | + | |
353 | +`--prefix=DIR' | |
354 | + Use DIR as the installation prefix. *note Installation Names:: | |
355 | + for more details, including other options available for fine-tuning | |
356 | + the installation locations. | |
357 | + | |
358 | +`--no-create' | |
359 | +`-n' | |
360 | + Run the configure checks, but stop before creating any output | |
361 | + files. | |
362 | + | |
363 | +`configure' also accepts some other, not widely useful, options. Run | |
364 | +`configure --help' for more details. | |
365 | + |
@@ -0,0 +1,113 @@ | ||
1 | +/* -*- c -*- */ | |
2 | +/* | |
3 | + * The MIT License | |
4 | + * | |
5 | + * Copyright (c) 2014 Yuki SAKAI | |
6 | + * All Rights Reserved. | |
7 | + * | |
8 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
9 | + * copy of this software and associated documentation files (the "Software"), | |
10 | + * to deal in the Software without restriction, including without limitation | |
11 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | + * and/or sell copies of the Software, and to permit persons to whom the | |
13 | + * Software is furnished to do so, subject to the following conditions: | |
14 | + * | |
15 | + * The above copyright notice and this permission notice shall be included in | |
16 | + * all copies or substantial portions of the Software. | |
17 | + * | |
18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
24 | + * IN THE SOFTWARE. | |
25 | + */ | |
26 | + | |
27 | +/******************************************************************************* | |
28 | + * include ********************************************************************/ | |
29 | +#if !defined(SELFLIB_H__) | |
30 | +# include <selfLib.h> | |
31 | +#endif | |
32 | +#if !defined(_SYS_SOCKET_H) && !defined(_SYS_SOCKET_H_) | |
33 | +# include <sys/socket.h> | |
34 | +#endif | |
35 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
36 | +# include <sys/types.h> | |
37 | +#endif | |
38 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
39 | +# include <string.h> | |
40 | +#endif | |
41 | +#if !defined(_NETDB_H) && !defined(_NETDB_H_) | |
42 | +# include <netdb.h> | |
43 | +#endif | |
44 | +#if !defined(_UNISTD_H) && !defined(_UNISTD_H_) | |
45 | +# include <unistd.h> | |
46 | +#endif | |
47 | +#if !defined(_ARPA_INET_H_) && !defined(_ARPA_INET_H) | |
48 | +# include <arpa/inet.h> | |
49 | +#endif | |
50 | +#if !defined(_NETINET_IN_H) && !defined(_NETINET_IN_H_) | |
51 | +# include <netinet/in.h> | |
52 | +#endif | |
53 | + | |
54 | + | |
55 | +/******************************************************************************* | |
56 | + * Listen socket functions (for IPv4 and IPv6 on TCP and UDP) *****************/ | |
57 | +int lsocket (const char *hostname, const char *port, int stype, int accept) | |
58 | +{ | |
59 | + struct addrinfo soc_info, *psinfo; | |
60 | + int soc = -1, ret = -1; | |
61 | + long on = 1; | |
62 | + | |
63 | + /***************************************************************************** | |
64 | + * Init Information *********************************************************/ | |
65 | + (void) memset ((char *)&soc_info, 0, sizeof (struct addrinfo)); | |
66 | + if (hostname==(void *)0) {soc_info.ai_flags = AI_PASSIVE;} | |
67 | + else if (strlen (hostname)<=0) {hostname = (void *)0; soc_info.ai_flags = AI_PASSIVE;} | |
68 | + soc_info.ai_family = AF_UNSPEC; | |
69 | + soc_info.ai_socktype = stype; | |
70 | + /*=Socket Setup=============================================================*/ | |
71 | + if (getaddrinfo (hostname, port, &soc_info, &psinfo)!=0) {ret = -1; goto err_ret; } | |
72 | + else if ((soc=socket (psinfo->ai_family, psinfo->ai_socktype, psinfo->ai_protocol))<0) {ret = -2; goto err_ret; } | |
73 | + else if (bind (soc, psinfo->ai_addr, psinfo->ai_addrlen)<0) {ret = -3; goto err_close;} | |
74 | + else if (stype==SOCK_DGRAM) {goto true_fin;} | |
75 | + else if (listen (soc, accept)<0) {ret = -4; goto err_close;} | |
76 | + true_fin: | |
77 | + setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
78 | + freeaddrinfo (psinfo); | |
79 | + return soc; | |
80 | + | |
81 | + /*=Error Process============================================================*/ | |
82 | + err_close: | |
83 | + close (soc); | |
84 | + err_ret: | |
85 | + freeaddrinfo (psinfo); | |
86 | + return ret; | |
87 | +} | |
88 | + | |
89 | +/******************************************************************************* | |
90 | + * Get Remote Host address ****************************************************/ | |
91 | +int getRemoteHost (const int soc, char *pstr, const int size) | |
92 | +{ | |
93 | + struct sockaddr_storage hostAddr; | |
94 | + socklen_t namelen; | |
95 | + struct sockaddr_in *ppF; | |
96 | + struct sockaddr_in6 *ppS; | |
97 | + (void) memset (pstr , 0, size); | |
98 | + (void) memset (&hostAddr, 0, sizeof (struct sockaddr_storage)); | |
99 | + /*=Get IP address ==========================================================*/ | |
100 | + namelen = sizeof (struct sockaddr_storage); | |
101 | + if (getpeername (soc, (struct sockaddr *)&hostAddr, &namelen)<0) {return 0;} | |
102 | + if (hostAddr.ss_family == AF_INET) { | |
103 | + ppF = (struct sockaddr_in *)&hostAddr; | |
104 | + inet_ntop (AF_INET, &ppF->sin_addr, pstr, size); | |
105 | + } | |
106 | + else if (hostAddr.ss_family == AF_INET6) { | |
107 | + ppS = (struct sockaddr_in6 *)&hostAddr; | |
108 | + inet_ntop (AF_INET6, &ppS->sin6_addr, pstr, size); | |
109 | + } | |
110 | + else {return 0;} | |
111 | + | |
112 | + return 1; | |
113 | +} |
@@ -0,0 +1,51 @@ | ||
1 | +/* -*- c -*- */ | |
2 | +/* | |
3 | + * The MIT License | |
4 | + * | |
5 | + * Copyright (c) 2014 Yuki SAKAI | |
6 | + * All Rights Reserved. | |
7 | + * | |
8 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
9 | + * copy of this software and associated documentation files (the "Software"), | |
10 | + * to deal in the Software without restriction, including without limitation | |
11 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | + * and/or sell copies of the Software, and to permit persons to whom the | |
13 | + * Software is furnished to do so, subject to the following conditions: | |
14 | + * | |
15 | + * The above copyright notice and this permission notice shall be included in | |
16 | + * all copies or substantial portions of the Software. | |
17 | + * | |
18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
24 | + * IN THE SOFTWARE. | |
25 | + */ | |
26 | + | |
27 | +/******************************************************************************* | |
28 | + * Include ********************************************************************/ | |
29 | +#if !defined(SELFLIB_H__) | |
30 | +# include <selfLib.h> | |
31 | +#endif | |
32 | +#if !defined(_SYS_TYPES_H_) && !defined(_SYS_TYPES_H) | |
33 | +# include <sys/types.h> | |
34 | +#endif | |
35 | +#if !defined(_PWD_H) && !defined(_PWD_H_) | |
36 | +# include <pwd.h> | |
37 | +#endif | |
38 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
39 | +# include <string.h> | |
40 | +#endif | |
41 | + | |
42 | +/******************************************************************************* | |
43 | + * Get Homedir From UID *******************************************************/ | |
44 | +int GetHomeDir (uid_t uid, char *buf, ssize_t blen) | |
45 | +{ | |
46 | + struct passwd *pwd; | |
47 | + if ((pwd=getpwuid (uid))==(void *)0) {endpwent (); return -1;} | |
48 | + strncpy (buf, pwd->pw_dir, (blen-1)); | |
49 | + endpwent (); | |
50 | + return 1; | |
51 | +} |
@@ -0,0 +1,55 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | +#if !defined(__BMP_STATION_H__) | |
26 | +#define __BMP_STATION_H__ 1 | |
27 | + | |
28 | +/******************************************************************************* | |
29 | + * include ********************************************************************/ | |
30 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
31 | +# include <sys/types.h> | |
32 | +#endif | |
33 | +#if !defined(_SYS_TIME_H) && !defined(_SYS_TIME_H_) | |
34 | +# include <sys/time.h> | |
35 | +#endif | |
36 | + | |
37 | +/******************************************************************************* | |
38 | + * Define *********************************************************************/ | |
39 | + | |
40 | +/******************************************************************************* | |
41 | + * Macro **********************************************************************/ | |
42 | + | |
43 | +/******************************************************************************* | |
44 | + * structure & typedef ********************************************************/ | |
45 | + | |
46 | +/******************************************************************************* | |
47 | + * variables ******************************************************************/ | |
48 | +/******************************************************************************* | |
49 | + * functions ******************************************************************/ | |
50 | +int BMPRecvData (int, u_int8_t **, ssize_t *); | |
51 | + | |
52 | +#endif | |
53 | + | |
54 | + | |
55 | + |
@@ -0,0 +1,172 @@ | ||
1 | +/* -*- c -*- */ | |
2 | +/* | |
3 | + * The MIT License | |
4 | + * | |
5 | + * Copyright (c) 2014 Yuki SAKAI | |
6 | + * All Rights Reserved. | |
7 | + * | |
8 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
9 | + * copy of this software and associated documentation files (the "Software"), | |
10 | + * to deal in the Software without restriction, including without limitation | |
11 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | + * and/or sell copies of the Software, and to permit persons to whom the | |
13 | + * Software is furnished to do so, subject to the following conditions: | |
14 | + * | |
15 | + * The above copyright notice and this permission notice shall be included in | |
16 | + * all copies or substantial portions of the Software. | |
17 | + * | |
18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
24 | + * IN THE SOFTWARE. | |
25 | + */ | |
26 | + | |
27 | +/******************************************************************************* | |
28 | + * Include ********************************************************************/ | |
29 | +#if !defined(SELFLIB_H__) | |
30 | +# include <selfLib.h> | |
31 | +#endif | |
32 | +#if !defined(_SYS_PARAM_H) && !defined(_SYS_PARAM_H_) | |
33 | +# include <sys/param.h> | |
34 | +#endif | |
35 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
36 | +# include <string.h> | |
37 | +#endif | |
38 | +#if !defined(_SYS_STAT_H) && !defined(_SYS_STAT_H_) | |
39 | +# include <sys/stat.h> | |
40 | +#endif | |
41 | +#if !defined(_ERRNO_H) && !defined(_ERRNO_H_) | |
42 | +# include <errno.h> | |
43 | +#endif | |
44 | + | |
45 | +/******************************************************************************* | |
46 | + * Get Last Block**************************************************************/ | |
47 | +ssize_t getLastPathBlock (const char *dpath, char *lpath, ssize_t llen) | |
48 | +{ | |
49 | + const char *sp, *fp, *tp; | |
50 | + /*=Search===================================================================*/ | |
51 | + sp = tp = dpath; | |
52 | + for (fp=strstr (tp, "/"); fp!=NULL; fp=strstr (tp, "/")) { | |
53 | + if (tp==fp ) {tp = fp + 1; sp = tp;} | |
54 | + else if (*(fp-1)=='\\') {tp = fp + 1;} | |
55 | + else {tp = fp + 1; sp = tp;} | |
56 | + } | |
57 | + /*=Copy=====================================================================*/ | |
58 | + return strncpy (lpath, sp, llen-1); | |
59 | +} | |
60 | + | |
61 | +/******************************************************************************* | |
62 | + * check directory path *******************************************************/ | |
63 | +int chkdir (const char *path, mode_t mode) | |
64 | +{ | |
65 | + mode_t nomod; | |
66 | + struct stat dstat; | |
67 | + nomod = (~ mode) & 0777; | |
68 | + /*=Check Parent directory===================================================*/ | |
69 | + errno = 0; | |
70 | + if (stat (path, &dstat)<0) { /* no path process ****************************/ | |
71 | + switch (errno) { | |
72 | + case EACCES: case EFAULT: case ELOOP: case ENAMETOOLONG: case ENOMEM: case ENOTDIR: | |
73 | + return -1; break; | |
74 | + } | |
75 | + return 0; | |
76 | + } | |
77 | + else if (!S_ISDIR(dstat.st_mode)) {return -1;} /* Directory check ****/ | |
78 | + else if ((dstat.st_mode&nomod)) {return -1;} /* permission check ***/ | |
79 | + | |
80 | + return 1; | |
81 | +} | |
82 | + | |
83 | +/******************************************************************************* | |
84 | + * Create directory ***********************************************************/ | |
85 | +int mkdirs (const char *path, mode_t mode) | |
86 | +{ | |
87 | + struct stat dstat; | |
88 | + char updir[MAXPATHLEN], *pupd; | |
89 | + const char *ppath; | |
90 | + /*=Init=====================================================================*/ | |
91 | + (void) memset ((char *)&dstat, 0, sizeof (struct stat)); | |
92 | + (void) memset ((char *)updir , 0, sizeof (updir )); | |
93 | + /*=Get Parent directory=====================================================*/ | |
94 | + for (pupd=updir, ppath=path; *ppath!=0; pupd++, ppath++) {*pupd = *ppath;} | |
95 | + rmPathLastBlock (updir); | |
96 | + | |
97 | + /*=Check Parent directory===================================================*/ | |
98 | + if (stat (updir, &dstat)<0) { /* no path process ****************************/ | |
99 | + switch (errno) { | |
100 | + case EACCES: case EFAULT: case ELOOP: case ENAMETOOLONG: case ENOMEM: case ENOTDIR: | |
101 | + return 0; break; | |
102 | + } | |
103 | + return mkdirs (updir, mode); | |
104 | + } | |
105 | + /*=Mkdir====================================================================*/ | |
106 | + errno = 0; | |
107 | + if (mkdir (path, mode)<0) { | |
108 | + switch (errno) { | |
109 | + case EACCES: case EFAULT: case ELOOP: case ENAMETOOLONG: case ENOMEM: case ENOSPC: case ENOTDIR: case EPERM: case EROFS: | |
110 | + return -1; break; | |
111 | + } | |
112 | + } | |
113 | + | |
114 | + return 1; | |
115 | +} | |
116 | + | |
117 | +/******************************************************************************* | |
118 | + * remove Last Block***********************************************************/ | |
119 | +ssize_t rmPathLastBlock (char *dpath) | |
120 | +{ | |
121 | + char *sp, *fp, *tp; | |
122 | + | |
123 | + rmPathLastSlash (dpath); | |
124 | + /*=Search===================================================================*/ | |
125 | + sp = tp = dpath; | |
126 | + for (fp=strstr (tp, "/"); fp!=NULL; fp=strstr (tp, "/")) { | |
127 | + if (tp==fp ) {tp = fp + 1; sp = tp;} | |
128 | + else if (*(fp-1)=='\\') {tp = fp + 1;} | |
129 | + else {tp = fp + 1; sp = tp;} | |
130 | + } | |
131 | + /*=Copy=====================================================================*/ | |
132 | + *sp = 0; | |
133 | + if (strcpy (dpath, "/")!=0) {rmPathLastSlash (dpath);} | |
134 | + return strlen (dpath); | |
135 | +} | |
136 | + | |
137 | +/******************************************************************************* | |
138 | + * Remove Path Last slash *****************************************************/ | |
139 | +ssize_t rmPathLastSlash (char *dpath) | |
140 | +{ | |
141 | + int len, i; | |
142 | + packPathDupSlash (dpath); | |
143 | + /*=Remove Last '/'==========================================================*/ | |
144 | + len = strlen (dpath); | |
145 | + if (len==0) {return -1;} | |
146 | + for (i=len-1; 0<=i; i--) { | |
147 | + if (i!=0) { | |
148 | + if (*(dpath+i)!='/') {break;} | |
149 | + else if (0<i && *(dpath+i-1)=='\\') {break;} | |
150 | + else {*(dpath+i) = 0x00;} | |
151 | + } | |
152 | + } | |
153 | + | |
154 | + return strlen (dpath); | |
155 | +} | |
156 | + | |
157 | +/******************************************************************************* | |
158 | + * Pack Duplicate Slash *******************************************************/ | |
159 | +ssize_t packPathDupSlash (char *dpath) | |
160 | +{ | |
161 | + char *pntA, *pntB; | |
162 | + int find, skip; | |
163 | + for (pntA=pntB=dpath,find=skip=0;;) { | |
164 | + if (skip==1 ) {skip = 0;} | |
165 | + else if (*pntB=='\\' ) {skip = 1; find = 0;} | |
166 | + else if (*pntB=='/' && find==0) {find = 1;} | |
167 | + else if (*pntB=='/' && find==1) {pntB++; continue;} | |
168 | + else {skip = find = 0;} | |
169 | + if ((*pntA++ = *pntB++)==0) {break;} | |
170 | + } | |
171 | + return strlen (dpath); | |
172 | +} |
@@ -0,0 +1,54 @@ | ||
1 | +/* -*- c -*- */ | |
2 | +/*- | |
3 | + * The MIT License | |
4 | + * | |
5 | + * Copyright (c) 2014 Yuki SAKAI | |
6 | + * All Rights Reserved. | |
7 | + * | |
8 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
9 | + * copy of this software and associated documentation files (the "Software"), | |
10 | + * to deal in the Software without restriction, including without limitation | |
11 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | + * and/or sell copies of the Software, and to permit persons to whom the | |
13 | + * Software is furnished to do so, subject to the following conditions: | |
14 | + * | |
15 | + * The above copyright notice and this permission notice shall be included in | |
16 | + * all copies or substantial portions of the Software. | |
17 | + * | |
18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
24 | + * IN THE SOFTWARE. | |
25 | + */ | |
26 | + | |
27 | +/******************************************************************************* | |
28 | + * include ********************************************************************/ | |
29 | +#if !defined(SELFLIB_H__) | |
30 | +# include <selfLib.h> | |
31 | +#endif | |
32 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
33 | +# include <string.h> | |
34 | +#endif | |
35 | +#if defined(__NetBSD__) || defined(__OpenBSD__) | |
36 | +#if !defined(_TIME_H) && !defined(_TIME_H_) | |
37 | +# include <time.h> | |
38 | +#endif | |
39 | +#elif defined(__linux__) | |
40 | +#if !defined(_SYS_TIME_H) && !defined(_SYS_TIME_H_) | |
41 | +# include <sys/time.h> | |
42 | +#endif | |
43 | +#endif | |
44 | + | |
45 | +/******************************************************************************* | |
46 | + * Transration from double to timeval *****************************************/ | |
47 | +struct timeval d2tval (double td) | |
48 | +{ | |
49 | + struct timeval tv; | |
50 | + (void) memset ((char *)&tv, 0, sizeof (struct timeval)); | |
51 | + tv.tv_sec = (long)td; | |
52 | + tv.tv_usec = (long)((td-(double)tv.tv_sec)*1e6); | |
53 | + return tv; | |
54 | +} |
@@ -0,0 +1,204 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | + | |
26 | +/******************************************************************************* | |
27 | + * include ********************************************************************/ | |
28 | +#if !defined(SELFLIB_H__) | |
29 | +# include <selfLib.h> | |
30 | +#endif | |
31 | +#if !defined(_SYS_PARAM_H) && !defined(_SYS_PARAM_H_) | |
32 | +# include <sys/param.h> | |
33 | +#endif | |
34 | +#if !defined(_ARPA_INET_H) && !defined(_ARPA_INET_H_) | |
35 | +# include <arpa/inet.h> | |
36 | +#endif | |
37 | +#if !defined(_NETINET_IN_H) && !defined(_NETINET_IN_H_) | |
38 | +# include <netinet/in.h> | |
39 | +#endif | |
40 | +#if !defined(_SYS_SOCKET_H) && !defined(_SYS_SOCKET_H_) | |
41 | +# include <sys/socket.h> | |
42 | +#endif | |
43 | +#if !defined(_STDIO_H) && !defined(_STDIO_H_) | |
44 | +# include <stdio.h> | |
45 | +#endif | |
46 | +#if !defined(_STDARG_H) && !defined(_STDARG_H_) | |
47 | +# include <stdarg.h> | |
48 | +#endif | |
49 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
50 | +# include <string.h> | |
51 | +#endif | |
52 | +#if !defined(_STDLIB_H_) && !defined(_STDLIB_H) | |
53 | +# include <stdlib.h> | |
54 | +#endif | |
55 | +#if !defined(_UNISTD_H) && !defined(_UNISTD_H_) | |
56 | +# include <unistd.h> | |
57 | +#endif | |
58 | +#if !defined(_TIME_H) && !defined(_TIME_H_) | |
59 | +# include <time.h> | |
60 | +#endif | |
61 | +#if !defined(_SYS_TYPES_H_) && !defined(_SYS_TYPES_H) | |
62 | +# include <sys/types.h> | |
63 | +#endif | |
64 | +#if !defined(_SYS_STAT_H_) && !defined(_SYS_STAT_H) | |
65 | +# include <sys/stat.h> | |
66 | +#endif | |
67 | + | |
68 | + | |
69 | +/******************************************************************************* | |
70 | + * define *********************************************************************/ | |
71 | +#define _YLOGMAXDATE 30 | |
72 | +#define _YLOGMAXIP 64 | |
73 | +#define _YLOGMAXLOG 512 | |
74 | + | |
75 | + | |
76 | +/******************************************************************************* | |
77 | + * socket *********************************************************************/ | |
78 | +static void netWriteLog (FILE *w, const char *d, const char *p, const char *r, const char *s) | |
79 | +{ | |
80 | + fprintf (w, "%s %s: Peer %s: %s\n", d, p, r, s); | |
81 | +} | |
82 | + | |
83 | +/******************************************************************************* | |
84 | + * no socket ******************************************************************/ | |
85 | +static void nonetWriteLog (FILE *w, const char *d, const char *p, const char *r, const char *s) | |
86 | +{ | |
87 | + fprintf (w, "%s %s: %s\n", d, p, s); | |
88 | +} | |
89 | + | |
90 | +/******************************************************************************* | |
91 | + * Date for log ***************************************************************/ | |
92 | +int dateLog (char *date, const int size) | |
93 | +{ | |
94 | + time_t tv; | |
95 | + struct tm *tvt; | |
96 | + tv = time (NULL); | |
97 | + tvt = localtime (&tv); | |
98 | + (void) memset (date, 0, size); | |
99 | + strftime (date, size-1, "%Y/%m/%d %H:%M:%S(%Z)", tvt); | |
100 | + | |
101 | + return 1; | |
102 | +} | |
103 | + | |
104 | +/******************************************************************************* | |
105 | + * Process ********************************************************************/ | |
106 | +static int processLog (const char *pgn, char *pstr, const int size) | |
107 | +{ | |
108 | + (void) memset (pstr, 0, size); | |
109 | + snprintf (pstr, size, "%s(%d)", pgn, (int)getpid ()); | |
110 | + | |
111 | + return 1; | |
112 | +} | |
113 | + | |
114 | +/******************************************************************************* | |
115 | + * Remote *********************************************************************/ | |
116 | +static int remoteLog (const int soc, char *pstr, const int size) | |
117 | +{ | |
118 | + struct sockaddr_storage peerAddr; | |
119 | + socklen_t namelen; | |
120 | + struct sockaddr_in *ppF; | |
121 | + struct sockaddr_in6 *ppS; | |
122 | + int port; | |
123 | + char strRemote[_YLOGMAXIP]; | |
124 | + (void) memset (strRemote, 0, sizeof (strRemote)); | |
125 | + (void) memset (pstr , 0, size); | |
126 | + (void) memset (&peerAddr, 0, sizeof (struct sockaddr_storage)); | |
127 | + /*=Get IP address ==========================================================*/ | |
128 | + namelen = sizeof (struct sockaddr_storage); | |
129 | + if (getpeername (soc, (struct sockaddr *)&peerAddr, &namelen)<0) {return 0;} | |
130 | + if (peerAddr.ss_family == AF_INET) { | |
131 | + ppF = (struct sockaddr_in *)&peerAddr; | |
132 | + port = ntohs (ppF->sin_port); | |
133 | + inet_ntop (AF_INET, &ppF->sin_addr, strRemote, sizeof (strRemote)); | |
134 | + } | |
135 | + else if (peerAddr.ss_family == AF_INET6) { | |
136 | + ppS = (struct sockaddr_in6 *)&peerAddr; | |
137 | + port = ntohs (ppS->sin6_port); | |
138 | + inet_ntop (AF_INET6, &ppS->sin6_addr, strRemote, sizeof (strRemote)); | |
139 | + } | |
140 | + else {return 0;} | |
141 | + snprintf (pstr, size, "%s(%d)", strRemote, port); | |
142 | + | |
143 | + return 1; | |
144 | +} | |
145 | + | |
146 | +/******************************************************************************* | |
147 | + * write syslog ***************************************************************/ | |
148 | +void writeLog (const char *path, const char *prog, const int fd, const char *fmt, ...) | |
149 | +{ | |
150 | + FILE *wfd; | |
151 | + char strdate[_YLOGMAXDATE]; | |
152 | + char strPrs[MAXPATHLEN]; | |
153 | + char strRemote[NI_MAXHOST]; | |
154 | + char *jmp, *ser; | |
155 | + char *strLog; | |
156 | + va_list ap; | |
157 | + void (*wfunc) (FILE *, const char*, const char*, const char*, const char*); | |
158 | + mode_t mode; | |
159 | + | |
160 | + if (fd<0) {wfunc = nonetWriteLog;} | |
161 | + else {wfunc = netWriteLog;} | |
162 | + /*=Get Infomation===========================================================*/ | |
163 | + if (!processLog (prog, strPrs , sizeof (strPrs ))) {goto end_proc;} | |
164 | + else if (!dateLog (strdate , sizeof (strdate ))) {goto end_proc;} | |
165 | + else if (fd<0) {} | |
166 | + else if (!remoteLog (fd , strRemote, sizeof (strRemote))) {goto end_proc;} | |
167 | + /*=Get Log==================================================================*/ | |
168 | + va_start(ap, fmt); | |
169 | + if ((strLog=(char *)malloc (_YLOGMAXLOG))==NULL) {goto fin_va;} | |
170 | + else if (vsnprintf (strLog, _YLOGMAXLOG, fmt, ap)<0 ) {goto free_data;} | |
171 | + va_end(ap); | |
172 | + /*=Open File================================================================*/ | |
173 | + mode = umask (022); | |
174 | + if (strcmp (path, "stdout")==0) {wfd = stdout;} | |
175 | + else if (strcmp (path, "stderr")==0) {wfd = stderr;} | |
176 | + else if ((wfd=fopen (path, "a"))==NULL) {goto free_data2;} | |
177 | + | |
178 | + /*=write Log================================================================*/ | |
179 | + ser = strLog; | |
180 | + for (jmp=strstr (ser, "\n"); ; jmp=strstr (ser, "\n")) { | |
181 | + if (jmp==NULL ) {} | |
182 | + else if (*jmp=='\n') {*jmp = 0x00;} | |
183 | + wfunc (wfd, strdate, strPrs, strRemote, ser); | |
184 | + if (jmp==NULL) {break;} | |
185 | + ser = jmp + 1; | |
186 | + } | |
187 | + | |
188 | + if (strcmp (path, "stdout")==0) {} | |
189 | + else if (strcmp (path, "stderr")==0) {} | |
190 | + else {fclose (wfd);} | |
191 | + | |
192 | + free_data2: | |
193 | + umask (mode); | |
194 | + free (strLog); | |
195 | + return ; | |
196 | + | |
197 | + | |
198 | + free_data: | |
199 | + free (strLog); | |
200 | + fin_va: | |
201 | + va_end(ap); | |
202 | + end_proc: | |
203 | + return ; | |
204 | +} |
@@ -0,0 +1,18 @@ | ||
1 | +noinst_LIBRARIES = libbmpStation.a libself.a | |
2 | +include_HEADERS = ../include/bmpStation.h ../include/selfLib.h | |
3 | +libself_a_SOURCES =../include/selfLib.h memory.c network.c path.c time.c pwd.c log.c | |
4 | +libself_a_SOURCES += | |
5 | +libself_a_SOURCES += | |
6 | +libself_a_CFLAGS = -Wall -O2 | |
7 | +libself_a_CFLAGS += -I../include -I../libbgp -L../libbgp -I../libbmp -L../libbmp | |
8 | +libself_a_CFLAGS += | |
9 | +libself_a_CFLAGS += | |
10 | +libbmpStation_a_SOURCES = ../bmpStation.h | |
11 | +libbmpStation_a_SOURCES += | |
12 | +libbmpStation_a_SOURCES += | |
13 | +libbmpStation_a_SOURCES += | |
14 | +libbmpStation_a_SOURCES += | |
15 | +libbmpStation_a_CFLAGS = -Wall -O2 | |
16 | +libbmpStation_a_CFLAGS += -I../include -I../libbgp -L../libbgp -I../libbmp -L../libbmp | |
17 | +libbmpStation_a_CFLAGS += | |
18 | +libbmpStation_a_CFLAGS += |
@@ -0,0 +1,49 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright (c) 2014 Yuki SAKAI | |
5 | + * All Rights Reserved. | |
6 | + * | |
7 | + * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | + * copy of this software and associated documentation files (the "Software"), | |
9 | + * to deal in the Software without restriction, including without limitation | |
10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | + * and/or sell copies of the Software, and to permit persons to whom the | |
12 | + * Software is furnished to do so, subject to the following conditions: | |
13 | + * | |
14 | + * The above copyright notice and this permission notice shall be included in | |
15 | + * all copies or substantial portions of the Software. | |
16 | + * | |
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
23 | + * IN THE SOFTWARE. | |
24 | + */ | |
25 | +/******************************************************************************* | |
26 | + * include ********************************************************************/ | |
27 | +#if !defined(SELFLIB_H__) | |
28 | +# include <selfLib.h> | |
29 | +#endif | |
30 | +#if !defined(_SYS_TYPES_H) && !defined(_SYS_TYPES_H_) | |
31 | +# include <sys/types.h> | |
32 | +#endif | |
33 | +#if !defined(_STRING_H) && !defined(_STRING_H_) | |
34 | +# include <string.h> | |
35 | +#endif | |
36 | + | |
37 | +/*%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%** | |
38 | + *: memory allocation :** | |
39 | + *%,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,%*/ | |
40 | +/******************************************************************************* | |
41 | + * memory allocation and initialize *******************************************/ | |
42 | +void *imalloc (size_t size) | |
43 | +{ | |
44 | + void *pData; | |
45 | + | |
46 | + if ((pData = malloc (size))==NULL) {return NULL;} | |
47 | + (void) memset (pData, 0, size); | |
48 | + return pData; | |
49 | +} |
@@ -0,0 +1,21 @@ | ||
1 | +The MIT License (MIT) | |
2 | + | |
3 | +Copyright (c) 2014 Yuki SAKAI | |
4 | + | |
5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | +of this software and associated documentation files (the "Software"), to deal | |
7 | +in the Software without restriction, including without limitation the rights | |
8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | +copies of the Software, and to permit persons to whom the Software is | |
10 | +furnished to do so, subject to the following conditions: | |
11 | + | |
12 | +The above copyright notice and this permission notice shall be included in | |
13 | +all copies or substantial portions of the Software. | |
14 | + | |
15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | +THE SOFTWARE. |
@@ -0,0 +1 @@ | ||
1 | +SUBDIRS = libbmp libbgp lib bmpStation |