nfrb 0.0.1.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,83 @@
1
+ /*
2
+ * Copyright (c) 2009, Peter Haag
3
+ * Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice,
10
+ * this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ * * Neither the name of SWITCH nor the names of its contributors may be
15
+ * used to endorse or promote products derived from this software without
16
+ * specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ * $Author: haag $
31
+ *
32
+ * $Id: nfx.h 48 2010-01-02 08:06:27Z haag $
33
+ *
34
+ * $LastChangedRevision: 48 $
35
+ *
36
+ */
37
+
38
+ #ifndef _NFX_H
39
+ #define _NFX_H 1
40
+
41
+ // MAX_EXTENSION_MAPS must be a power of 2
42
+ #define MAX_EXTENSION_MAPS 65536
43
+ #define EXTENSION_MAP_MASK (MAX_EXTENSION_MAPS-1)
44
+
45
+ typedef struct extension_descriptor_s {
46
+ uint16_t id; // id number
47
+ uint16_t size; // number of bytes
48
+ uint32_t user_index; // index specified by the user to enable this extension
49
+ uint32_t enabled; // extension is enabled or not
50
+ char *description;
51
+ } extension_descriptor_t;
52
+
53
+ typedef struct extension_info_s {
54
+ extension_map_t *map;
55
+ uint32_t ref_count;
56
+ master_record_t master_record;
57
+ } extension_info_t;
58
+
59
+
60
+ typedef struct extension_map_list_s {
61
+ extension_info_t *slot[MAX_EXTENSION_MAPS];
62
+ extension_info_t *page[MAX_EXTENSION_MAPS];
63
+ uint32_t next_free; // next free index in extension page
64
+ int32_t max_used; // max used slot index, -1 if empty
65
+ } extension_map_list_t;
66
+
67
+ void InitExtensionMaps(extension_map_list_t *extension_map_list );
68
+
69
+ void FreeExtensionMaps(extension_map_list_t *extension_map_list);
70
+
71
+ void PackExtensionMapList(extension_map_list_t *extension_map_list);
72
+
73
+ int Insert_Extension_Map(extension_map_list_t *extension_map_list, extension_map_t *map);
74
+
75
+ void SetupExtensionDescriptors(char *options);
76
+
77
+ void PrintExtensionMap(extension_map_t *map);
78
+
79
+ int VerifyExtensionMap(extension_map_t *map);
80
+
81
+ void DumpExMaps(char *filename);
82
+
83
+ #endif //_NFX_H
@@ -0,0 +1,517 @@
1
+ /*
2
+ * Copyright (c) 2009, Peter Haag
3
+ * Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice,
10
+ * this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ * * Neither the name of SWITCH nor the names of its contributors may be
15
+ * used to endorse or promote products derived from this software without
16
+ * specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ * $Author: haag $
31
+ *
32
+ * $Id: util.c 39 2009-11-25 08:11:15Z haag $
33
+ *
34
+ * $LastChangedRevision: 39 $
35
+ *
36
+ */
37
+
38
+ #include "config.h"
39
+
40
+ #include <stdio.h>
41
+ #include <unistd.h>
42
+ #include <stdlib.h>
43
+ #include <time.h>
44
+ #include <string.h>
45
+ #include <errno.h>
46
+ #include <sys/types.h>
47
+
48
+ #ifndef SYSLOG_NAMES
49
+ #define SYSLOG_NAMES 1
50
+ #endif
51
+ #include <syslog.h>
52
+ #include <stdarg.h>
53
+
54
+ #ifdef HAVE_STDINT_H
55
+ #include <stdint.h>
56
+ #endif
57
+
58
+ #include "util.h"
59
+
60
+ /* Global vars */
61
+
62
+ extern char *CurrentIdent;
63
+
64
+ enum { NONE, LESS, MORE };
65
+
66
+ /* Function prototypes */
67
+ static int check_number(char *s, int len);
68
+
69
+ static int ParseTime(char *s, time_t *t_start);
70
+
71
+ static uint32_t twin_first, twin_last;
72
+ static int use_syslog = 0;
73
+
74
+ #ifdef sun
75
+ struct _code {
76
+ char *c_name;
77
+ int c_val;
78
+ } facilitynames[] = {
79
+ { "auth", LOG_AUTH },
80
+ { "cron", LOG_CRON },
81
+ { "daemon", LOG_DAEMON },
82
+ { "kern", LOG_KERN },
83
+ { "lpr", LOG_LPR },
84
+ { "mail", LOG_MAIL },
85
+ { "news", LOG_NEWS },
86
+ { "security", LOG_AUTH }, /* DEPRECATED */
87
+ { "syslog", LOG_SYSLOG },
88
+ { "user", LOG_USER },
89
+ { "uucp", LOG_UUCP },
90
+ { "local0", LOG_LOCAL0 },
91
+ { "local1", LOG_LOCAL1 },
92
+ { "local2", LOG_LOCAL2 },
93
+ { "local3", LOG_LOCAL3 },
94
+ { "local4", LOG_LOCAL4 },
95
+ { "local5", LOG_LOCAL5 },
96
+ { "local6", LOG_LOCAL6 },
97
+ { "local7", LOG_LOCAL7 },
98
+ { NULL, -1 }
99
+ };
100
+ #endif
101
+
102
+ /* Functions */
103
+
104
+ /*
105
+ ** usleep(3) implemented with select.
106
+ */
107
+
108
+ void xsleep(long sec) {
109
+ struct timeval tv;
110
+
111
+ tv.tv_sec = sec;
112
+ tv.tv_usec = 0;
113
+
114
+ select(0, NULL, NULL, NULL, &tv);
115
+ }
116
+
117
+ int InitLog(char *name, char *facility) {
118
+ int i;
119
+ char *logname;
120
+
121
+ if ( !facility || strlen(facility) > 32 ) {
122
+ fprintf(stderr, "Invalid syslog facility name '%s'!\n", facility);
123
+ return 0;
124
+ }
125
+
126
+
127
+ i = 0;
128
+ while ( facilitynames[i].c_name && strcasecmp(facilitynames[i].c_name, facility ) != 0 ) {
129
+ i++;
130
+ }
131
+
132
+ if ( facilitynames[i].c_name == NULL ) {
133
+ fprintf(stderr, "Invalid syslog facility name '%s'!\n", facility);
134
+ return 0;
135
+ }
136
+
137
+ if ( (logname = strrchr(name , '/')) != 0 ) {
138
+ logname++;
139
+ } else {
140
+ logname = name;
141
+ }
142
+ openlog(logname, LOG_CONS|LOG_PID, facilitynames[i].c_val);
143
+ use_syslog = 1;
144
+
145
+ return 1;
146
+
147
+ } // End of InitLog
148
+
149
+ /*
150
+ * some modules are needed for daemon code as well as normal stdio code
151
+ * therefore a generic LogError is defined, which maps in this case
152
+ * to stderr
153
+ */
154
+ void LogError(char *format, ...) {
155
+ va_list var_args;
156
+ char string[512];
157
+
158
+ if ( use_syslog ) {
159
+ va_start(var_args, format);
160
+ vsnprintf(string, 511, format, var_args);
161
+ va_end(var_args);
162
+ syslog(LOG_ERR, "%s", string);
163
+ } else {
164
+ va_start(var_args, format);
165
+ vfprintf(stderr, format, var_args);
166
+ va_end(var_args);
167
+ }
168
+
169
+ } // End of LogError
170
+
171
+ void LogInfo(char *format, ...) {
172
+ va_list var_args;
173
+ char string[512];
174
+
175
+ if ( use_syslog ) {
176
+ va_start(var_args, format);
177
+ vsnprintf(string, 511, format, var_args);
178
+ va_end(var_args);
179
+ syslog(LOG_INFO, "%s", string);
180
+ } else {
181
+ va_start(var_args, format);
182
+ vfprintf(stderr, format, var_args);
183
+ va_end(var_args);
184
+ }
185
+
186
+ } // End of LogInfo
187
+
188
+ static int check_number(char *s, int len) {
189
+ int i;
190
+ int l = strlen(s);
191
+
192
+ for ( i=0; i<l; i++ ) {
193
+ if ( s[i] < '0' || s[i] > '9' ) {
194
+ LogError("Time format error at '%s': unexpected character: '%c'.\n", s, s[i]);
195
+ return 0;
196
+ }
197
+ }
198
+
199
+ if ( l != len ) {
200
+ LogError("Time format error: '%s' unexpected.\n", s);
201
+ return 0;
202
+ }
203
+ return 1;
204
+
205
+ } // End of check_number
206
+
207
+ static int ParseTime(char *s, time_t *t_start ) {
208
+ struct tm ts;
209
+ int i;
210
+ char *p, *q;
211
+
212
+
213
+ /* A time string may look like:
214
+ * yyyy/MM/dd.hh:mm:ss
215
+ */
216
+
217
+ memset((void *)&ts, 0, sizeof(ts));
218
+ ts.tm_isdst = -1;
219
+
220
+ p = s;
221
+
222
+ // parse year
223
+ q = strchr(p, '/');
224
+ if ( q ) {
225
+ *q++ = 0;
226
+ }
227
+ if ( !check_number(p,4) )
228
+ return 0;
229
+ i = atoi(p);
230
+ if ( i > 2038 || i < 1970 ) {
231
+ LogError("Year out of range: '%i'\n", i);
232
+ *t_start = 0;
233
+ return 0;
234
+ }
235
+ ts.tm_year = i - 1900;
236
+ if ( !q ) {
237
+ ts.tm_mday = 1;
238
+ *t_start = mktime(&ts);
239
+ return 1;
240
+ }
241
+
242
+ // parse month
243
+ p = q;
244
+ q = strchr(p, '/');
245
+ if ( q )
246
+ *q++ = 0;
247
+ if ( !check_number(p,2) )
248
+ return 0;
249
+ i = atoi(p);
250
+ if ( i < 1 || i > 12 ) {
251
+ LogError("Month out of range: '%i'\n", i);
252
+ *t_start = 0;
253
+ return 0;
254
+ }
255
+ ts.tm_mon = i - 1;
256
+ if ( !q ) {
257
+ ts.tm_mday = 1;
258
+ *t_start = mktime(&ts);
259
+ return 1;
260
+ }
261
+
262
+ // Parse day
263
+ p = q;
264
+ q = strchr(p, '.');
265
+ if ( q )
266
+ *q++ = 0;
267
+ if ( !check_number(p,2) )
268
+ return 0;
269
+ i = atoi(p);
270
+ if ( i < 1 || i > 31 ) {
271
+ LogError("Day out of range: '%i'\n", i);
272
+ *t_start = 0;
273
+ return 0;
274
+ }
275
+ ts.tm_mday = i;
276
+ if ( !q ) {
277
+ *t_start = mktime(&ts);
278
+ return 1;
279
+ }
280
+
281
+ // Parse hour
282
+ p = q;
283
+ q = strchr(p, ':');
284
+ if ( q )
285
+ *q++ = 0;
286
+ if ( !check_number(p,2) )
287
+ return 0;
288
+ i = atoi(p);
289
+ if ( i < 0 || i > 23 ) {
290
+ LogError("Hour out of range: '%i'\n", i);
291
+ *t_start = 0;
292
+ return 0;
293
+ }
294
+ ts.tm_hour = i;
295
+ if ( !q ) {
296
+ *t_start = mktime(&ts);
297
+ return 1;
298
+ }
299
+
300
+ // Parse minute
301
+ p = q;
302
+ q = strchr(p, ':');
303
+ if ( q )
304
+ *q++ = 0;
305
+ if ( !check_number(p,2) )
306
+ return 0;
307
+ i = atoi(p);
308
+ if ( i < 0 || i > 59 ) {
309
+ LogError("Minute out of range: '%i'\n", i);
310
+ *t_start = 0;
311
+ return 0;
312
+ }
313
+ ts.tm_min = i;
314
+ if ( !q ) {
315
+ *t_start = mktime(&ts);
316
+ return 1;
317
+ }
318
+
319
+ // Parse second
320
+ p = q;
321
+ if ( !check_number(p,2) )
322
+ return 0;
323
+ i = atoi(p);
324
+ if ( i < 0 || i > 59 ) {
325
+ LogError("Seconds out of range: '%i'\n", i);
326
+ *t_start = 0;
327
+ return 0;
328
+ }
329
+ ts.tm_sec = i;
330
+ *t_start = mktime(&ts);
331
+ return 1;
332
+
333
+ } // End of ParseTime
334
+
335
+
336
+ int ScanTimeFrame(char *tstring, time_t *t_start, time_t *t_end) {
337
+ char *p;
338
+
339
+ if ( !tstring ) {
340
+ fprintf(stderr,"Time Window format error '%s'\n", tstring);
341
+ return 0;
342
+ }
343
+
344
+ // check for delta time window
345
+ if ( tstring[0] == '-' || tstring[0] == '+' ) {
346
+ if ( !twin_first || !twin_last ) {
347
+ fprintf(stderr,"Time Window error: No time slot information available\n");
348
+ return 0;
349
+ }
350
+
351
+ if ( tstring[0] == '-' ) {
352
+ *t_start = twin_last + atoi(tstring);
353
+ *t_end = twin_last;
354
+ return 1;
355
+ }
356
+
357
+ if ( tstring[0] == '+' ) {
358
+ *t_start = twin_first;
359
+ *t_end = twin_first + atoi(tstring);
360
+ return 1;
361
+ }
362
+ }
363
+
364
+ if ( strlen(tstring) < 4 ) {
365
+ fprintf(stderr,"Time Window format error '%s'\n", tstring);
366
+ return 0;
367
+ }
368
+ if ( (p = strchr(tstring, '-') ) == NULL ) {
369
+ ParseTime(tstring, t_start);
370
+ *t_end = 0xFFFFFFFF;
371
+ } else {
372
+ *p++ = 0;
373
+ ParseTime(tstring, t_start);
374
+ ParseTime(p, t_end);
375
+ }
376
+
377
+ return *t_start == 0 || *t_end == 0 ? 0 : 1;
378
+
379
+ } // End of ScanTimeFrame
380
+
381
+ char *TimeString(time_t start, time_t end) {
382
+ static char datestr[255];
383
+ char t1[64], t2[64];
384
+ struct tm *tbuff;
385
+
386
+ if ( start ) {
387
+ tbuff = localtime(&start);
388
+ if ( !tbuff ) {
389
+ perror("Error time convert");
390
+ exit(250);
391
+ }
392
+ strftime(t1, 63, "%Y-%m-%d %H:%M:%S", tbuff);
393
+
394
+ tbuff = localtime(&end);
395
+ if ( !tbuff ) {
396
+ perror("Error time convert");
397
+ exit(250);
398
+ }
399
+ strftime(t2, 63, "%Y-%m-%d %H:%M:%S", tbuff);
400
+
401
+ snprintf(datestr, 254, "%s - %s", t1, t2);
402
+ } else {
403
+ snprintf(datestr, 254, "Time Window unknown");
404
+ }
405
+ datestr[254] = 0;
406
+ return datestr;
407
+ }
408
+
409
+ char *UNIX2ISO(time_t t) {
410
+ struct tm *when;
411
+ static char timestring[16];
412
+
413
+ when = localtime(&t);
414
+ when->tm_isdst = -1;
415
+ snprintf(timestring, 15, "%i%02i%02i%02i%02i",
416
+ when->tm_year + 1900, when->tm_mon + 1, when->tm_mday, when->tm_hour, when->tm_min);
417
+ timestring[15] = '\0';
418
+
419
+ return timestring;
420
+
421
+ } // End of UNIX2ISO
422
+
423
+ time_t ISO2UNIX(char *timestring) {
424
+ char c, *p;
425
+ struct tm when;
426
+ time_t t;
427
+
428
+ // let localtime fill in all default fields such as summer time, TZ etc.
429
+ t = time(NULL);
430
+ localtime_r(&t, &when);
431
+ when.tm_sec = 0;
432
+ when.tm_wday = 0;
433
+ when.tm_yday = 0;
434
+ when.tm_isdst = -1;
435
+
436
+ if ( strlen(timestring) != 12 ) {
437
+ LogError( "Wrong time format '%s'\n", timestring);
438
+ return 0;
439
+ }
440
+ // 2006 05 05 12 00
441
+ // year
442
+ p = timestring;
443
+ c = p[4];
444
+ p[4] = '\0';
445
+ when.tm_year = atoi(p) - 1900;
446
+ p[4] = c;
447
+
448
+ // month
449
+ p += 4;
450
+ c = p[2];
451
+ p[2] = '\0';
452
+ when.tm_mon = atoi(p) - 1;
453
+ p[2] = c;
454
+
455
+ // day
456
+ p += 2;
457
+ c = p[2];
458
+ p[2] = '\0';
459
+ when.tm_mday = atoi(p);
460
+ p[2] = c;
461
+
462
+ // hour
463
+ p += 2;
464
+ c = p[2];
465
+ p[2] = '\0';
466
+ when.tm_hour = atoi(p);
467
+ p[2] = c;
468
+
469
+ // minute
470
+ p += 2;
471
+ when.tm_min = atoi(p);
472
+
473
+ t = mktime(&when);
474
+ if ( t == -1 ) {
475
+ LogError( "Failed to convert string '%s'\n", timestring);
476
+ return 0;
477
+ } else {
478
+ // printf("%s %s", timestring, ctime(&t));
479
+ return t;
480
+ }
481
+
482
+ } // End of ISO2UNIX
483
+
484
+
485
+ void InitStringlist(stringlist_t *list, int block_size) {
486
+
487
+ list->list = NULL;
488
+ list->num_strings = 0;
489
+ list->max_index = 0;
490
+ list->block_size = block_size;
491
+
492
+ } // End of InitStringlist
493
+
494
+ void InsertString(stringlist_t *list, char *string) {
495
+
496
+ if ( !list->list ) {
497
+ list->max_index = list->block_size;
498
+ list->num_strings = 0;
499
+ list->list = (char **)malloc(list->max_index * sizeof(char *));
500
+ if ( !list->list ) {
501
+ LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno) );
502
+ exit(250);
503
+ }
504
+ }
505
+ list->list[list->num_strings++] = string ? strdup(string) : NULL;
506
+
507
+ if ( list->num_strings == list->max_index ) {
508
+ list->max_index += list->block_size;
509
+ list->list = (char **)realloc(list->list, list->max_index * sizeof(char *));
510
+ if ( !list->list ) {
511
+ LogError("realloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno) );
512
+ exit(250);
513
+ }
514
+ }
515
+
516
+ } // End of InsertString
517
+