| 1 |
|
| #ifdef __cplusplus |
| 2 |
|
| extern "C" { |
| 3 |
|
| #endif |
| 4 |
|
| #include "EXTERN.h" |
| 5 |
|
| #include "perl.h" |
| 6 |
|
| #include "XSUB.h" |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include <unistd.h> |
| 10 |
|
| #include <sys/socket.h> |
| 11 |
|
| #include <netinet/in.h> |
| 12 |
|
| #include <arpa/inet.h> |
| 13 |
|
| |
| 14 |
|
| #ifdef __cplusplus |
| 15 |
|
| } |
| 16 |
|
| #endif |
| 17 |
|
| |
| 18 |
|
| // for debug fixme |
| 19 |
|
| //#define DEBUG 1 |
| 20 |
|
| |
| 21 |
|
| #ifdef DEBUG |
| 22 |
|
| # define DIAG(fmt, ...) PerlIO_printf(PerlIO_stderr(), fmt, __VA_ARGS__); |
| 23 |
|
| # define DUMPTREE(pt) { CIDR_TRIE *xt = pt; xt = xt->child[10]; xt = xt->child[0]; xt = xt->child[0]; xt = xt->child[1]; DIAG(" DUMPTREE %s\n", xt->name); } |
| 24 |
|
| #else |
| 25 |
|
| # define DIAG(fmt, ...) |
| 26 |
|
| # define DUMPTREE(pt, ...) |
| 27 |
|
| #endif |
| 28 |
|
| |
| 29 |
|
| #define CIDR_TABLE_BITS 8 |
| 30 |
|
| #define CIDR_TABLE_SIZE (1 << CIDR_TABLE_BITS) |
| 31 |
|
| |
| 32 |
|
| typedef struct CIDR_TRIE { |
| 33 |
|
| const char *name; |
| 34 |
|
| uint32_t bits; |
| 35 |
|
| struct CIDR_TRIE *child[CIDR_TABLE_SIZE]; |
| 36 |
|
| } CIDR_TRIE; |
| 37 |
|
| |
| 38 |
6 |
| static int itonetmask(int n, uint32_t *netmask) |
| 39 |
|
| { |
| 40 |
|
| uint32_t m; |
| 41 |
|
| |
| 42 |
6 |
▶ | if (n < 0 || 32 < n) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0 || 32 < n) return 0; |
6 |
0 |
6 |
| if (n < 0 || 32 < n) return 0; |
0 |
6 |
6 |
|
| 43 |
|
| |
| 44 |
6 |
| m = 1UL << (32 - n); |
| 45 |
6 |
| --m; |
| 46 |
6 |
| *netmask = ~m; |
| 47 |
6 |
| return 1; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
262175 |
| static int is_leaf(const CIDR_TRIE *pt) { |
| 51 |
262175 |
| return pt->child[0] == pt; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
14 |
| static CIDR_TRIE *new_trie_node() { |
| 55 |
|
| int i; |
| 56 |
|
| CIDR_TRIE *pt; |
| 57 |
|
| |
| 58 |
14 |
| Newxz(pt, 1, CIDR_TRIE); |
| 59 |
|
| |
| 60 |
14 |
| pt->name = 0; |
| 61 |
14 |
| pt->bits = 0; |
| 62 |
3598 |
▶ | for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
3584 |
14 |
3598 |
|
| 63 |
3584 |
| pt->child[i] = pt; |
| 64 |
14 |
| return pt; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
0 |
| static void init_root(CIDR_TRIE *trie_root) |
| 68 |
|
| { |
| 69 |
|
| int i; |
| 70 |
|
| CIDR_TRIE *nullnode; |
| 71 |
|
| |
| 72 |
0 |
| nullnode = new_trie_node(); |
| 73 |
0 |
▶ | for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
0 |
0 |
0 |
|
| 74 |
0 |
| trie_root->child[i] = nullnode; |
| 75 |
0 |
| } |
| 76 |
|
| |
| 77 |
6 |
| static CIDR_TRIE* digg_trie(CIDR_TRIE *child) { |
| 78 |
|
| int i; |
| 79 |
6 |
| CIDR_TRIE *parent = new_trie_node(); |
| 80 |
|
| |
| 81 |
1542 |
▶ | for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
1536 |
6 |
1542 |
|
| 82 |
1536 |
| parent->child[i] = child; |
| 83 |
6 |
| return parent; |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
1024 |
| static int update_leaf(CIDR_TRIE *pt, CIDR_TRIE *leaf) |
| 87 |
|
| { |
| 88 |
1024 |
| int used = 0; |
| 89 |
|
| int i; |
| 90 |
|
| |
| 91 |
263168 |
▶ | for (i = 0; i < CIDR_TABLE_SIZE; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < CIDR_TABLE_SIZE; ++i) { |
262144 |
1024 |
263168 |
|
| 92 |
262144 |
| CIDR_TRIE *next = pt->child[i]; |
| 93 |
262144 |
▶ | if (is_leaf(next)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_leaf(next)) { |
261376 |
768 |
262144 |
|
| 94 |
261376 |
▶ | if (next->bits < leaf->bits) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (next->bits < leaf->bits) { |
1003 |
260373 |
261376 |
|
| 95 |
1003 |
| pt->child[i] = leaf; |
| 96 |
1003 |
| used = 1; |
| 97 |
|
| } |
| 98 |
|
| } |
| 99 |
|
| else { |
| 100 |
768 |
| used |= update_leaf(next, leaf); |
| 101 |
|
| } |
| 102 |
|
| } |
| 103 |
1024 |
| return used; |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| |
| 107 |
|
| MODULE = Net::IP::Match::Trie PACKAGE = Net::IP::Match::Trie |
| 108 |
|
| |
| 109 |
|
| SV * |
| 110 |
|
| _initialize(self) |
| 111 |
|
| SV *self; |
| 112 |
|
| PREINIT: |
| 113 |
|
| CIDR_TRIE *root, *nullnode; |
| 114 |
|
| int i; |
| 115 |
|
| CODE: |
| 116 |
1 |
| root = new_trie_node(); |
| 117 |
1 |
| nullnode = new_trie_node(); |
| 118 |
257 |
▶ | for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < CIDR_TABLE_SIZE; ++i) |
256 |
1 |
257 |
|
| 119 |
256 |
| root->child[i] = nullnode; |
| 120 |
1 |
| sv_magic(SvRV(self), NULL, PERL_MAGIC_ext, NULL, 0); |
| 121 |
1 |
| mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj = (void *)root; |
| 122 |
|
| |
| 123 |
|
| void |
| 124 |
|
| _add(self, name, network, netmask) |
| 125 |
|
| SV *self; |
| 126 |
|
| SV *name; |
| 127 |
|
| SV *network; |
| 128 |
|
| SV *netmask; |
| 129 |
|
| PREINIT: |
| 130 |
|
| CIDR_TRIE *pt, *p_leaf; |
| 131 |
|
| size_t len; |
| 132 |
|
| in_addr_t addr; |
| 133 |
|
| uint32_t nm; |
| 134 |
|
| CODE: |
| 135 |
|
| DIAG(">>%s %s %s/%d\n", "add", SvPV_nolen(name), SvPV_nolen(network), SvIV(netmask)); |
| 136 |
|
| DUMPTREE((CIDR_TRIE *)mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj); |
| 137 |
6 |
▶ | if (!inet_aton(SvPV_nolen(network), (struct in_addr *)&addr)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!inet_aton(SvPV_nolen(network), (struct in_addr *)&addr)) { |
0 |
6 |
6 |
|
| 138 |
0 |
| warn("inet_aton: failed"); |
| 139 |
|
| } |
| 140 |
6 |
| len = SvIV(netmask); |
| 141 |
6 |
▶ | if (!itonetmask(len,&nm)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!itonetmask(len,&nm)) { |
0 |
6 |
6 |
|
| 142 |
0 |
| warn("itonetmask: failed"); |
| 143 |
|
| } |
| 144 |
|
| DIAG(" nm :%08X\n", nm); |
| 145 |
6 |
| addr = ntohl(addr); |
| 146 |
|
| DIAG(" addr:%08X\n", addr); |
| 147 |
6 |
| addr &= nm; |
| 148 |
|
| DIAG(" addr&nm:%08X\n", addr); |
| 149 |
|
| |
| 150 |
6 |
| pt = (CIDR_TRIE *)mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj; |
| 151 |
|
| |
| 152 |
6 |
| p_leaf = new_trie_node(); |
| 153 |
6 |
| p_leaf->name = SvPV_nolen(newSVsv(name)); |
| 154 |
6 |
| p_leaf->bits = len; |
| 155 |
|
| |
| 156 |
16 |
▶ | while (len > CIDR_TABLE_BITS) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len > CIDR_TABLE_BITS) { |
10 |
6 |
16 |
|
| 157 |
10 |
| int b = addr >> (32 - CIDR_TABLE_BITS); |
| 158 |
10 |
| CIDR_TRIE *next = pt->child[b]; |
| 159 |
10 |
▶ | if (is_leaf(next)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_leaf(next)) { |
6 |
4 |
10 |
|
| 160 |
6 |
| pt->child[b] = next = digg_trie(next); |
| 161 |
|
| } |
| 162 |
10 |
| pt = next; |
| 163 |
10 |
| addr <<= CIDR_TABLE_BITS; |
| 164 |
10 |
| len -= CIDR_TABLE_BITS; |
| 165 |
|
| } |
| 166 |
|
| { |
| 167 |
|
| int i; |
| 168 |
6 |
| const int bmin = addr >> (32 - CIDR_TABLE_BITS); |
| 169 |
6 |
| const int bmax = bmin + (1 << (CIDR_TABLE_BITS - len)); |
| 170 |
6 |
| int used = 0; // delete p_leaf if it is not used. |
| 171 |
27 |
▶ | for (i = bmin; i < bmax; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = bmin; i < bmax; ++i) { |
21 |
6 |
27 |
|
| 172 |
21 |
| CIDR_TRIE *target = pt->child[i]; |
| 173 |
21 |
▶ | if (is_leaf(target)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_leaf(target)) { |
20 |
1 |
21 |
|
| 174 |
20 |
▶ | if (target->bits < p_leaf->bits) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (target->bits < p_leaf->bits) { |
20 |
0 |
20 |
|
| 175 |
20 |
| pt->child[i] = p_leaf; |
| 176 |
20 |
| used = 1; |
| 177 |
|
| } |
| 178 |
|
| } |
| 179 |
|
| else { |
| 180 |
|
| int j; |
| 181 |
257 |
▶ | for (j = 0; j < CIDR_TABLE_SIZE; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < CIDR_TABLE_SIZE; ++j) { |
256 |
1 |
257 |
|
| 182 |
256 |
| used |= update_leaf(target, p_leaf); |
| 183 |
|
| } |
| 184 |
|
| } |
| 185 |
|
| } |
| 186 |
|
| } |
| 187 |
|
| DUMPTREE((CIDR_TRIE *)mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj); |
| 188 |
|
| |
| 189 |
|
| SV * |
| 190 |
|
| match_ip(self, ip) |
| 191 |
|
| SV *self; |
| 192 |
|
| SV *ip; |
| 193 |
|
| PREINIT: |
| 194 |
|
| CIDR_TRIE *pt; |
| 195 |
|
| in_addr_t addr; |
| 196 |
|
| uint8_t *octed; |
| 197 |
|
| CODE: |
| 198 |
|
| DIAG(">>%s %s\n", "match_ip", SvPV_nolen(ip)); |
| 199 |
10 |
| pt = (CIDR_TRIE *)mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj; |
| 200 |
10 |
▶ | if (!inet_aton(SvPV_nolen(ip), (struct in_addr *)&addr)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!inet_aton(SvPV_nolen(ip), (struct in_addr *)&addr)) { |
1 |
9 |
10 |
|
| 201 |
1 |
| warn("inet_aton: failed"); |
| 202 |
1 |
| XSRETURN_EMPTY; |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
|
| DIAG(" addr:%08X\n", addr); |
| 206 |
9 |
| octed = (uint8_t *)&addr; |
| 207 |
|
| #if CIDR_TABLE_BITS == 8 |
| 208 |
9 |
| pt = pt->child[*octed++]; |
| 209 |
9 |
| pt = pt->child[*octed++]; |
| 210 |
9 |
| pt = pt->child[*octed++]; |
| 211 |
9 |
| pt = pt->child[*octed++]; |
| 212 |
|
| #elif CIDR_TABLE_BITS == 16 |
| 213 |
|
| pt = pt->child[octed[0] * 256 + octed[1]]; |
| 214 |
|
| pt = pt->child[octed[2] * 256 + octed[3]]; |
| 215 |
|
| #else |
| 216 |
|
| #error CIDR_TABLE_BITS must be 8 or 16. |
| 217 |
|
| #endif |
| 218 |
|
| DIAG(" name:%s\n", pt->name); |
| 219 |
9 |
▶ | RETVAL = newSVpv(pt->name != NULL ? pt->name : "", 0); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = newSVpv(pt->name != NULL ? pt->name : "", 0); |
6 |
3 |
9 |
|
| 220 |
|
| OUTPUT: |
| 221 |
|
| RETVAL |
| 222 |
|
| |
| 223 |
|
| void |
| 224 |
|
| DESTROY(self) |
| 225 |
|
| SV *self; |
| 226 |
|
| PREINIT: |
| 227 |
|
| CIDR_TRIE *root; |
| 228 |
|
| CODE: |
| 229 |
|
| DIAG(">>%s\n", "DESTROY"); |
| 230 |
1 |
| root = (CIDR_TRIE *)mg_find(SvRV(self), PERL_MAGIC_ext)->mg_obj; |
| 231 |
|
| /* todo: free all node of tree */ |
| 232 |
1 |
| Safefree(root); |