undns 0.4.0a

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,103 @@
1
+ /*
2
+ * Copyright (c) 2002
3
+ * Neil Spring and the University of Washington.
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
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * 3. The name of the author(s) may not be used to endorse or promote
15
+ * products derived from this software without specific prior
16
+ * written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
22
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
30
+ #ifndef _hashtable_h
31
+ #define _hashtable_h
32
+
33
+ #include "nscommon.h"
34
+
35
+ NS_BEGIN_DECLS
36
+
37
+ struct hashtable_struct;
38
+ typedef struct hashtable_struct *hashtable;
39
+
40
+ void ht_delete(/*@only@*/ hashtable deleteme, boolean show_progress);
41
+
42
+ typedef unsigned int (*hash_cb)(const void *key);
43
+ typedef boolean (*isequal_cb)(const void *key1, const void *key2);
44
+ typedef void (*delete_cb)(void *key);
45
+ hashtable ht_new(unsigned int size,
46
+ hash_cb hash,
47
+ isequal_cb isequal,
48
+ /*@null@*/ delete_cb free_keyval);
49
+ void ht_insert(hashtable ht, /*@owned@*/const void *keyval);
50
+ /* true if found and removed */
51
+ boolean ht_remove(hashtable ht, const void *key);
52
+ typedef boolean (*ht_delete_if_cb)(void *keyval, void *user);
53
+ unsigned int ht_delete_if(hashtable ht,
54
+ ht_delete_if_cb callback,
55
+ /*@null@*/ void *user);
56
+ void ht_free_entry(hashtable ht, const void *keyval);
57
+ /*@dependent@*/ /*@null@*/
58
+ void *ht_lookup(hashtable ht, const void *key);
59
+
60
+ typedef void * (*ht_constructor_cb)(const void *key);
61
+ /*@dependent@*/
62
+ void *ht_lookup_nofail(hashtable ht,
63
+ const void *key,
64
+ ht_constructor_cb constructor);
65
+
66
+ /* returns true if it should continue to be called */
67
+ typedef boolean (*ht_iterator_cb)(const void *keyval, void *user);
68
+ boolean ht_iterate(hashtable ht,
69
+ ht_iterator_cb callback,
70
+ void *user);
71
+
72
+ typedef boolean (*ht_iterator_pair_cb)(const void *keyval, const void *keyval2);
73
+ boolean ht_iterate_pairs(hashtable ht, unsigned int nThreads, unsigned int chunkSize,
74
+ ht_iterator_pair_cb callback);
75
+
76
+ /* not-const version of the above; differs only in type. */
77
+ typedef boolean (*ht_iterator_cb_nc)(void *keyval, void *user);
78
+ boolean ht_iterate_nc(hashtable ht,
79
+ ht_iterator_cb_nc callback,
80
+ void *user);
81
+
82
+ unsigned long ht_count(hashtable ht);
83
+
84
+ void ht_occupancyjgr(const hashtable ht, const char *fname);
85
+
86
+ /* works if you're storing ints, or if the key is
87
+ already generated and the first int in the structure */
88
+ unsigned int hash_8byte(const void *k);
89
+ boolean isequal_8byte(const void *ia, const void *ib);
90
+ unsigned int hash_k_int(const int *k);
91
+ boolean isequal_k_int(const int *ia, const int *ib);
92
+ struct in_addr;
93
+ unsigned int hash_k_saddr(const struct in_addr *k);
94
+ boolean isequal_k_saddr(const struct in_addr *ia, const struct in_addr *ib);
95
+ unsigned int hash_k_uint(const unsigned int *k);
96
+ boolean isequal_k_uint(const unsigned int *ia, const unsigned int *ib);
97
+ unsigned int hash_int(const void *k);
98
+ boolean isequal_int(const void *ia, const void *ib);
99
+ unsigned int hash_ushort(const void *k);
100
+ boolean isequal_ushort(const void *ia, const void *ib);
101
+
102
+ NS_END_DECLS
103
+ #endif
@@ -0,0 +1,3 @@
1
+
2
+ /* placeholder */
3
+ #include <ruby.h>
@@ -0,0 +1,207 @@
1
+ /*
2
+ * Copyright (c) 2002
3
+ * Neil Spring and the University of Washington.
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
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * 3. The name of the author(s) may not be used to endorse or promote
15
+ * products derived from this software without specific prior
16
+ * written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
22
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
30
+ #ifndef _NS_COMMON_H
31
+ #define _NS_COMMON_H
32
+
33
+ #ifdef HAVE_CONFIG_H
34
+ #include <config.h>
35
+ #endif
36
+
37
+ #ifdef HAVE_SYS_TIME_H
38
+ #include <sys/time.h>
39
+ #endif
40
+
41
+ #ifdef __cplusplus
42
+ # define NS_BEGIN_DECLS extern "C" {
43
+ # define NS_END_DECLS }
44
+ #else
45
+ # define NS_BEGIN_DECLS /* empty */
46
+ # define NS_END_DECLS /* empty */
47
+ #endif
48
+
49
+ NS_BEGIN_DECLS
50
+
51
+ #ifdef __LCLINT__
52
+ #define UNUSED(x) /*@unused@*/ ##x
53
+ typedef unsigned char u_int8_t;
54
+ typedef unsigned short u_int16_t;
55
+ typedef unsigned int u_int32_t;
56
+ typedef unsigned long long u_int64_t;
57
+ #else
58
+ #define UNUSED(x) x __attribute__((unused))
59
+ #endif
60
+
61
+ #ifdef __LCLINT__
62
+ #define __STDC__
63
+ char *rindex(const char *s, char c); /* not really true; it's an int */
64
+ /*@dependent@*/char *index(const char *s, char c); /* not really true; it's an int */
65
+ char *strdup(const char *s); /* probably a good annotation here */
66
+ /*@printflike@*/
67
+ int /*@alt void@*/ snprintf(/*@out@*/char *s, size_t x, const char *fmt, ...);
68
+ #endif
69
+
70
+ typedef unsigned char boolean;
71
+
72
+ #ifndef FALSE
73
+ #define FALSE 0
74
+ #endif
75
+ #ifndef TRUE
76
+ #define TRUE (!FALSE)
77
+ #endif
78
+
79
+ #ifdef __LCLINT__
80
+ /* lclint doesn't do typeof */
81
+ #define min(x,y) ((x > y) ? x : y)
82
+ #define max(x,y) ((x > y) ? x : y)
83
+ #else
84
+ /* from linux/kernel.h */
85
+ #define min(x,y) ({ \
86
+ const typeof(x) _xi = (x); \
87
+ const typeof(y) _yi = (y); \
88
+ (void) (&_xi == &_yi); \
89
+ _xi < _yi ? _xi : _yi; })
90
+ #define max(x,y) ({ \
91
+ const typeof(x) _xa = (x); \
92
+ const typeof(y) _ya = (y); \
93
+ (void) (&_xa == &_ya); \
94
+ _xa > _ya ? _xa : _ya; })
95
+ #endif
96
+
97
+ #define DEBUG_PRINT(x...) do { } while (0)
98
+ // #define DEBUG_PRINT(x) fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, x);
99
+ #define DEBUG_PRINTL(x) fprintf(stderr, "%s:%d: %s", __FILE__, __LINE__, x);
100
+
101
+ #ifdef HAVE_STDINT_H
102
+ /* a linux-y thing */
103
+ #include <stdint.h>
104
+ #endif
105
+ #ifdef HAVE_SYS_TYPES_H
106
+ #include <sys/types.h>
107
+ #endif
108
+ /* kludgy fix for solaris. cygwin suggests u_int64_t is more portable,
109
+ solaris suggests the opposite. */
110
+ typedef u_int64_t macaddress;
111
+
112
+ #ifdef VERBOSE
113
+ #define VERBAL printf
114
+ #else
115
+ #define VERBAL(X...) do { if(want_debug) fprintf(stderr, X); } while(0);
116
+
117
+ #if defined(__linux__) && !defined(__cplusplus)
118
+ /*@unused@*/
119
+ static __inline void nullie(/*@unused@*/ const char *fmt __attribute__ ((unused)), ...) {
120
+ return;
121
+ }
122
+ #else
123
+ #define nullie(x...) do {} while(0)
124
+ #endif
125
+
126
+ #endif
127
+
128
+ #include<stdlib.h>
129
+ #include<stdio.h>
130
+ /*@unused@*/
131
+ static __inline /*@out@*/ void *malloc_ordie(size_t len) {
132
+ void *ret = malloc(len);
133
+ if(ret == NULL) {
134
+ fprintf(stderr, "unable to allocate %d bytes\n", (int)len);
135
+ abort();
136
+ }
137
+ return(ret);
138
+ }
139
+
140
+ #include <string.h>
141
+ /*@unused@*/
142
+ static __inline char *strdup_ordie(const char *str) /*@*/ {
143
+ char *ret = strdup(str);
144
+ if(ret == NULL) {
145
+ fprintf(stderr, "unable to duplicate %s\n", str);
146
+ abort();
147
+ }
148
+ return(ret);
149
+ }
150
+
151
+ #define NEWPTR(type, name) type *name = (type *)malloc_ordie(sizeof(type))
152
+ #define NEWPTRX(type, name) type *name = (type *)malloc_ordie(sizeof(type))
153
+ #define NEWPTRZ(type, name) type *name = (type *)memset(malloc_ordie(sizeof(type)), 0, sizeof(type))
154
+
155
+ /* means nscommon must be included after sys/time */
156
+ #ifndef timersub
157
+ # define timersub(a, b, result) \
158
+ do { \
159
+ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
160
+ (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;\
161
+ if ((result)->tv_usec < 0) { \
162
+ --(result)->tv_sec; \
163
+ (result)->tv_usec += 1000000; \
164
+ } \
165
+ } while (0)
166
+ #endif
167
+
168
+ #ifdef HAVE_CHECK_H
169
+ #include <check.h>
170
+ #ifdef HAVE__ck_assert_msg
171
+ /* newer check library, still seems to lack the obvious function. */
172
+ #define fail_expect_int(result,expect,msg) do { \
173
+ char buf[255]; \
174
+ int r = result; \
175
+ sprintf(buf, "%s (got %d, expected %d)", msg, (r), (expect)); \
176
+ _ck_assert_msg((r)==(expect),__FILE__,__LINE__,buf, NULL); \
177
+ }while(0)
178
+ #define fail_expect_voidp(result,expect,msg) do { \
179
+ char buf[255]; \
180
+ void *r = result; \
181
+ sprintf(buf, "%s (got %p, expected %p)", msg, (r), (expect)); \
182
+ _ck_assert_msg((r)==((void *)expect),__FILE__,__LINE__,buf, NULL); \
183
+ } while(0)
184
+ #else
185
+ #define fail_expect_int(result,expect,msg) do { \
186
+ char buf[255]; \
187
+ int r = result; \
188
+ sprintf(buf, "%s (got %d, expected %d)", msg, (r), (expect)); \
189
+ _fail_unless((r)==(expect),__FILE__,__LINE__,buf); \
190
+ } while(0)
191
+ #define fail_expect_voidp(result,expect,msg) do { \
192
+ char buf[255]; \
193
+ void *r = result; \
194
+ sprintf(buf, "%s (got %p, expected %p)", msg, (r), (expect)); \
195
+ _fail_unless((r)==((void *)expect),__FILE__,__LINE__,buf); \
196
+ } while(0)
197
+
198
+ #endif
199
+ #endif
200
+
201
+
202
+ NS_END_DECLS
203
+
204
+ #endif
205
+ /*
206
+ vi:ts=2
207
+ */
@@ -0,0 +1,134 @@
1
+ /* (c) neil spring 2002 */
2
+ #ifdef HAVE_CONFIG_H
3
+ #include "config.h"
4
+ #endif
5
+ #include <sys/types.h>
6
+ #include <sys/socket.h>
7
+ #include <netinet/in.h>
8
+ #include <arpa/inet.h>
9
+ #include <assert.h>
10
+ #include <stdlib.h>
11
+ #include <errno.h>
12
+ #include <string.h>
13
+ #include "nscommon.h"
14
+ #include "originAS.h"
15
+ #include "radix.h"
16
+ #include "exception.h"
17
+ #include "ruleset.h" /* fopen_path_r */
18
+
19
+ static struct patricia_table *origins;
20
+ static in_addr_t addr_from_octets(unsigned int o1, unsigned int o2,
21
+ unsigned int o3, unsigned int o4){
22
+ return (htonl((o1<<24) + (o2<<16) + (o3<<8) + o4));
23
+ }
24
+
25
+ void
26
+ origins_init(const char *origins_dat) {
27
+ char linebuf[60];
28
+ // FILE *in = popen("./show_origins.pl | sort -u", "r");
29
+ const char *filename = (origins_dat != NULL) ? origins_dat : "origins.dat";
30
+ FILE *in = fopen_path_r(filename);
31
+ if(in == NULL) {
32
+ char buf[255];
33
+ sprintf(buf, "can't open origins.dat(%s): %s", filename, strerror(errno));
34
+ throw_void(buf);
35
+ }
36
+ origins = patricia_new();
37
+ while(!feof(in) && fgets(linebuf, sizeof(linebuf), in) != NULL) {
38
+ unsigned int o1, o2, o3, o4, len, as;
39
+ if(sscanf(linebuf, "%u.%u.%u.%u/%u %u",
40
+ &o1, &o2, &o3, &o4, &len, &as) == 6) {
41
+ patricia_insert(origins, addr_from_octets(o1,o2,o3,o4), len, (void *)as);
42
+ } else if(sscanf(linebuf, "%u.%u.%u.%u/%u {%u",
43
+ &o1, &o2, &o3, &o4, &len, &as) == 6) {
44
+ /* pick the first one from a multiple-origins line */
45
+ patricia_insert(origins, addr_from_octets(o1,o2,o3,o4), len, (void *)as);
46
+ } else {
47
+ /* this has never forced me to actually address this issue, it just clouds
48
+ up the output file.
49
+ printf("originAS: recoverable error parsing '%s' in origins.dat\n", linebuf);
50
+ */
51
+ }
52
+ }
53
+ fclose(in);
54
+ }
55
+
56
+ unsigned short origin_for_address(in_addr_t a) {
57
+ if ( origins == NULL ) {
58
+ origins_init(NULL);
59
+ }
60
+ if ( origins != NULL ) {
61
+ return((unsigned short)(unsigned int)patricia_lookup(origins, a));
62
+ } else {
63
+ fprintf(stderr, "Origins failed to initialize\n"); /* shouldn't get here. */
64
+ return(0);
65
+ }
66
+ }
67
+
68
+ unsigned short origin_for_address_str(const char *addrstring) {
69
+ struct in_addr a;
70
+ if(inet_aton(addrstring, &a)) {
71
+ return(origin_for_address(a.s_addr));
72
+ }
73
+ return 0;
74
+ }
75
+
76
+ boolean origin_prefix_for_address(in_addr_t a, /*@out@*/ unsigned short *as,
77
+ /*@out@*/ in_addr_t *addr, /*@out@*/ unsigned char *prefixlen) {
78
+ if ( origins == NULL ) {
79
+ origins_init(NULL);
80
+ }
81
+ if ( origins != NULL ) {
82
+ void *data;
83
+ // unsigned short oas;
84
+ boolean ret = patricia_lookup_all(origins, a, addr, prefixlen, &data);
85
+ // printf("a=%x *addr=%x, *prefixlen=%d\n", a, *addr, *prefixlen);
86
+ *as = (unsigned short)(unsigned int)data;
87
+ // oas = ((unsigned short)(unsigned int)patricia_lookup(origins, a));
88
+ // assert(oas == *as);
89
+ return(ret);
90
+ } else {
91
+ fprintf(stderr, "Origins failed to initialize\n"); /* shouldn't get here. */
92
+ return(FALSE);
93
+ }
94
+ }
95
+
96
+
97
+ void
98
+ origins_regression_test(const char *origins_dat) {
99
+ FILE *in;
100
+ char linebuf[60];
101
+ origins_init(origins_dat);
102
+ in = fopen_path_r(origins_dat);
103
+ while(!feof(in) && fgets(linebuf, sizeof(linebuf), in) != NULL) {
104
+ unsigned int o1, o2, o3, o4, len, as;
105
+ if(sscanf(linebuf, "%u.%u.%u.%u/%u %u",
106
+ &o1, &o2, &o3, &o4, &len, &as) == 6) {
107
+ if(len < 32 && o1 < 256 && o2 < 256 && o3 < 256 && o4 < 256) {
108
+ struct patricia_entry *pe =
109
+ radix_resolve_route(origins->routes,addr_from_octets(o1,o2,o3,o4+1));
110
+ if((unsigned int)pe->data != as && pe->position <= len) {
111
+ /* not the same and not a more specific */
112
+ fprintf(stderr, "failed to verify %u == %u on line:\n%s",
113
+ as, (unsigned int) pe->data, linebuf);
114
+ if(pe->prefixlen > len) {
115
+ fprintf(stderr, " -- because a more specific was matched. (%d > %d)\n", pe->prefixlen, len);
116
+ } else {
117
+ #if !defined(CHECK_ORIGINS_SHOULD_FAIL) || CHECK_ORIGINS_SHOULD_FAIL == 0
118
+ abort();
119
+ #else
120
+ exit(EXIT_SUCCESS);
121
+ #endif
122
+ }
123
+ }
124
+ // unsigned int stored_as = (unsigned int) patricia_lookup(origins, addr_from_octets(o1,o2,o3,o4+1));
125
+ // if( stored_as != as) {
126
+ // fprintf(stderr, "failed to verify %u == %u on line:\n%s",
127
+ // as, stored_as, linebuf);
128
+ // abort();
129
+ // }
130
+ }
131
+ }
132
+ }
133
+ fclose(in);
134
+ }