| 1 |
|
| |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #define LPM_MAX_INSTANCES 1024 |
| 7 |
|
| |
| 8 |
|
| |
| 9 |
|
| #include <stdio.h> |
| 10 |
|
| #include <stdlib.h> |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include <stdbool.h> |
| 13 |
|
| #include <arpa/inet.h> |
| 14 |
|
| |
| 15 |
|
| #define BUFFSIZE 60 |
| 16 |
|
| #define ALOCSIZE 100000 |
| 17 |
|
| |
| 18 |
|
| #define HV_STORE_NV(r,k,v) (void)hv_store(r, k, strlen(k), newSVnv(v), 0) |
| 19 |
|
| |
| 20 |
|
| const int lastAlocIndex = ALOCSIZE - 1; |
| 21 |
|
| |
| 22 |
|
| /// Structure of Node in Trie |
| 23 |
|
| typedef struct TrieNode{ |
| 24 |
|
| struct TrieNode *pTN0; |
| 25 |
|
| struct TrieNode *pTN1; |
| 26 |
|
| SV * Value; |
| 27 |
|
| bool hasValue; |
| 28 |
|
| } TTrieNode; |
| 29 |
|
| |
| 30 |
|
| /* linked list of TTrieNode(s) */ |
| 31 |
|
| typedef struct TrieNodeList { |
| 32 |
|
| struct TrieNode *pNode; |
| 33 |
|
| struct TrieNodeList *pNext; |
| 34 |
|
| int Depth; |
| 35 |
|
| int AfType; |
| 36 |
|
| char *Prefix; |
| 37 |
|
| } TTrieNodeList; |
| 38 |
|
| |
| 39 |
|
| /// Structure of alokated chunks of Trie Nodes |
| 40 |
|
| typedef struct AlocTrieNodes{ |
| 41 |
|
| struct TrieNode TrieNodes[ALOCSIZE]; |
| 42 |
|
| struct AlocTrieNodes *pNextATN; |
| 43 |
|
| } TAlocTrieNodes; |
| 44 |
|
| |
| 45 |
|
| |
| 46 |
|
| /* instance data */ |
| 47 |
|
| typedef struct lpm_instance_s |
| 48 |
|
| { |
| 49 |
|
| TTrieNode *pTrieIPV4; |
| 50 |
|
| TTrieNode *pTrieIPV6; |
| 51 |
|
| } lpm_instance_t; |
| 52 |
|
| |
| 53 |
|
| |
| 54 |
|
| /* array of initalized instances */ |
| 55 |
|
| lpm_instance_t *lpm_instances[LPM_MAX_INSTANCES] = { NULL }; |
| 56 |
|
| |
| 57 |
|
| |
| 58 |
|
| /// function Prototypes |
| 59 |
|
| |
| 60 |
|
| void addPrefixToTrie(unsigned char *prefix, unsigned char prefixLen, SV * Value, TTrieNode **ppTrie); |
| 61 |
|
| TTrieNode *createTrieNode(); |
| 62 |
|
| void freeAlocTrieNodes(TAlocTrieNodes *pATN); |
| 63 |
|
| TTrieNode *lookupAddress(unsigned char *address, int addrLen, TTrieNode *pTN); |
| 64 |
|
| TTrieNode **lookupInTrie(unsigned char *prefix, unsigned char *byte, unsigned char *bit, unsigned char *makeMatches, TTrieNode **ppTN, bool *root); |
| 65 |
|
| TTrieNode *myMalloc(); |
| 66 |
|
| int listTrieNode(TTrieNode *pTN, TTrieNodeList **plTN, const int AfType, int *depth, unsigned char *prefix); |
| 67 |
|
| |
| 68 |
|
| |
| 69 |
|
| /// Alocation speeding variables |
| 70 |
|
| TAlocTrieNodes *pAlocated = NULL; |
| 71 |
|
| TAlocTrieNodes *pActual = NULL; |
| 72 |
|
| struct TrieNode *pActualTN = NULL; |
| 73 |
|
| struct TrieNode *pLastTN = NULL; |
| 74 |
|
| |
| 75 |
|
| /// IPV4 Trie |
| 76 |
|
| TTrieNode *pTrieIPV4 = NULL; |
| 77 |
|
| |
| 78 |
|
| /// IPV6 Trie |
| 79 |
|
| TTrieNode *pTrieIPV6 = NULL; |
| 80 |
|
| |
| 81 |
|
| /* clean tree and decrement reference count */ |
| 82 |
2311966 |
| void freeTrieNode(TTrieNode *pTN) { |
| 83 |
2311966 |
▶ | if (pTN != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pTN != NULL) { |
1155977 |
1155989 |
2311966 |
|
| 84 |
1155977 |
▶ | if (pTN->hasValue) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pTN->hasValue) { |
458938 |
697039 |
1155977 |
|
| 85 |
458938 |
| pTN->hasValue = false; |
| 86 |
458938 |
| SvREFCNT_dec(pTN->Value); |
| 87 |
|
| } |
| 88 |
1155977 |
| freeTrieNode(pTN->pTN0); |
| 89 |
1155977 |
| freeTrieNode(pTN->pTN1); |
| 90 |
1155977 |
| free(pTN); |
| 91 |
|
| } |
| 92 |
2311966 |
| } |
| 93 |
|
| |
| 94 |
|
| /* count the number of nodes in subtree */ |
| 95 |
2310524 |
| void countTrieNode(TTrieNode *pTN, int * totalNodes, int * valueNodes, int * trieBytes, int * dataBytes) { |
| 96 |
2310524 |
▶ | if (pTN != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pTN != NULL) { |
1155261 |
1155263 |
2310524 |
|
| 97 |
1155261 |
▶ | if (pTN->hasValue) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pTN->hasValue) { |
458895 |
696366 |
1155261 |
|
| 98 |
458895 |
| (*valueNodes)++; |
| 99 |
458895 |
| (*dataBytes) += 0; |
| 100 |
|
| } |
| 101 |
1155261 |
| countTrieNode(pTN->pTN0, totalNodes, valueNodes, trieBytes, dataBytes); |
| 102 |
1155261 |
| countTrieNode(pTN->pTN1, totalNodes, valueNodes, trieBytes, dataBytes); |
| 103 |
1155261 |
| (*totalNodes)++; |
| 104 |
1155261 |
| (*trieBytes) += sizeof(struct TrieNode); |
| 105 |
|
| } |
| 106 |
2310524 |
| } |
| 107 |
|
| |
| 108 |
|
| /* convert trie node into linked list */ |
| 109 |
995 |
| int listTrieNode(TTrieNode *pTN, TTrieNodeList **plTN, const int AfType, int *depth, unsigned char *prefix) { |
| 110 |
|
| TTrieNodeList *ptmp; |
| 111 |
|
| int allocsize; |
| 112 |
|
| |
| 113 |
995 |
▶ | if (pTN != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pTN != NULL) { |
495 |
500 |
995 |
|
| 114 |
495 |
▶ | if (pTN->hasValue) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pTN->hasValue) { |
33 |
462 |
495 |
|
| 115 |
33 |
| ptmp = malloc(sizeof(TTrieNodeList)); |
| 116 |
33 |
▶ | if (ptmp == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptmp == NULL) { |
0 |
33 |
33 |
|
| 117 |
0 |
| return 0; |
| 118 |
|
| } |
| 119 |
|
| /* get prefix len */ |
| 120 |
33 |
| allocsize = (((*depth) - 1) / 8) + 1; |
| 121 |
|
| |
| 122 |
33 |
| ptmp->pNext = *plTN; |
| 123 |
33 |
| ptmp->pNode = pTN; |
| 124 |
33 |
| ptmp->Depth = *depth; |
| 125 |
33 |
| ptmp->AfType = AfType; |
| 126 |
33 |
| ptmp->Prefix = malloc(allocsize); |
| 127 |
33 |
▶ | if (ptmp->Prefix == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptmp->Prefix == NULL) { |
0 |
33 |
33 |
|
| 128 |
0 |
| return 0; |
| 129 |
|
| } |
| 130 |
33 |
| memcpy(ptmp->Prefix, prefix, allocsize); |
| 131 |
|
| |
| 132 |
33 |
| *plTN = ptmp; |
| 133 |
|
| } |
| 134 |
495 |
| (*depth)++; |
| 135 |
|
| |
| 136 |
495 |
▶ | if (listTrieNode(pTN->pTN0, plTN, AfType, depth, prefix) == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (listTrieNode(pTN->pTN0, plTN, AfType, depth, prefix) == 0) return 0; |
0 |
495 |
495 |
|
| 137 |
|
| |
| 138 |
495 |
| prefix[((*depth) - 1 ) / 8] |= 0x80 >> ((((*depth) - 1) % 8) ); |
| 139 |
495 |
▶ | if (listTrieNode(pTN->pTN1, plTN, AfType, depth, prefix) == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (listTrieNode(pTN->pTN1, plTN, AfType, depth, prefix) == 0) return 0; |
0 |
495 |
495 |
|
| 140 |
495 |
| prefix[((*depth) - 1 ) / 8] &= 0xFF7F >> ((((*depth) - 1) % 8) ); |
| 141 |
|
| |
| 142 |
495 |
| (*depth)--; |
| 143 |
|
| |
| 144 |
|
| } |
| 145 |
995 |
| return 1; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
|
| /** |
| 149 |
|
| * addPrefixToTrie adds prefix to Trie |
| 150 |
|
| * prefix - prefix that will be added to Trie |
| 151 |
|
| * prefixLen - prefix length |
| 152 |
|
| * ASNum - number of AS |
| 153 |
|
| * ppTrie - pointer at pointer of desired Trie(IPV4 or IPV6) |
| 154 |
|
| * returns error if fails |
| 155 |
|
| */ |
| 156 |
458948 |
| void addPrefixToTrie(unsigned char *prefix, unsigned char prefixLen, SV * Value, TTrieNode **ppTrie){ |
| 157 |
458948 |
| unsigned char byte = 0; |
| 158 |
458948 |
| unsigned char bit = 128; |
| 159 |
|
| |
| 160 |
458948 |
| unsigned char makeMatches = prefixLen; |
| 161 |
458948 |
| bool root = false; |
| 162 |
458948 |
| TTrieNode *pFound = (*ppTrie); |
| 163 |
458948 |
| TTrieNode **ppTN = lookupInTrie(prefix, &byte, &bit, &makeMatches, &pFound, &root); |
| 164 |
458948 |
▶ | if(root){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(root){ |
12 |
458936 |
458948 |
|
| 165 |
12 |
| ppTN = ppTrie; |
| 166 |
|
| } |
| 167 |
458948 |
▶ | if(ppTN != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(ppTN != NULL){ |
456209 |
2739 |
458948 |
|
| 168 |
1155977 |
▶ | while(makeMatches){ |
Branch: 1 branch
| Branch | True | False | Total |
| while(makeMatches){ |
699768 |
456209 |
1155977 |
|
| 169 |
699768 |
| (*ppTN) = createTrieNode(); |
| 170 |
699768 |
| unsigned char unmasked = (prefix[byte] & bit); |
| 171 |
|
| |
| 172 |
699768 |
▶ | if(unmasked){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(unmasked){ |
140391 |
559377 |
699768 |
|
| 173 |
140391 |
| ppTN = &((*ppTN)->pTN1); |
| 174 |
|
| } |
| 175 |
|
| else{ |
| 176 |
559377 |
| ppTN = &((*ppTN)->pTN0); |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
699768 |
| makeMatches--; |
| 180 |
699768 |
| bit = (bit >> 1); |
| 181 |
699768 |
▶ | if(!bit){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!bit){ |
177435 |
522333 |
699768 |
|
| 182 |
177435 |
| byte++; |
| 183 |
177435 |
| bit = 128; |
| 184 |
|
| } |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
456209 |
| (*ppTN) = createTrieNode(); |
| 188 |
456209 |
| (*ppTN)->Value = Value; |
| 189 |
456209 |
| (*ppTN)->hasValue = true; |
| 190 |
456209 |
| SvREFCNT_inc(Value); |
| 191 |
|
| } |
| 192 |
|
| else{ |
| 193 |
2739 |
▶ | if(!pFound->hasValue){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!pFound->hasValue){ |
2729 |
10 |
2739 |
|
| 194 |
2729 |
| pFound->Value = Value; |
| 195 |
2729 |
| pFound->hasValue = true; |
| 196 |
2729 |
| SvREFCNT_inc(Value); |
| 197 |
|
| } |
| 198 |
|
| } |
| 199 |
458948 |
| } |
| 200 |
|
| |
| 201 |
|
| /** |
| 202 |
|
| * createTrieNode creates Node of Trie and sets him implicitly up |
| 203 |
|
| * returns NULL or created Node of Trie |
| 204 |
|
| */ |
| 205 |
1155977 |
| TTrieNode *createTrieNode(){ |
| 206 |
|
| //TTrieNode *pTN = myMalloc(sizeof(struct TrieNode)); |
| 207 |
1155977 |
| TTrieNode *pTN = malloc(sizeof(struct TrieNode)); |
| 208 |
1155977 |
▶ | if(pTN == NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pTN == NULL){ |
0 |
1155977 |
1155977 |
|
| 209 |
0 |
| fprintf(stderr, "pTN malloc error."); |
| 210 |
0 |
| return NULL; |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| // initialize |
| 214 |
1155977 |
| pTN->pTN0 = NULL; |
| 215 |
1155977 |
| pTN->pTN1 = NULL; |
| 216 |
1155977 |
| pTN->hasValue = false; |
| 217 |
|
| |
| 218 |
1155977 |
| return pTN; |
| 219 |
|
| } |
| 220 |
|
| |
| 221 |
|
| |
| 222 |
|
| /** |
| 223 |
|
| * freeAlocTrieNodes |
| 224 |
|
| * pATN - pointer at structure to be freed |
| 225 |
|
| */ |
| 226 |
0 |
| void freeAlocTrieNodes(TAlocTrieNodes *pATN){ |
| 227 |
0 |
▶ | if(pATN->pNextATN != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pATN->pNextATN != NULL){ |
0 |
0 |
0 |
|
| 228 |
0 |
| freeAlocTrieNodes(pATN->pNextATN); |
| 229 |
|
| } |
| 230 |
0 |
| free(pATN); |
| 231 |
0 |
| } |
| 232 |
|
| |
| 233 |
|
| /** |
| 234 |
|
| * lookupAddressIPv6 |
| 235 |
|
| * address - field with address |
| 236 |
|
| * addrLen - length of IP adress in bits (32 for IPv4, 128 for IPv6) |
| 237 |
|
| * returns NULL when NOT match adress to any prefix in Trie |
| 238 |
|
| */ |
| 239 |
9000313 |
| TTrieNode *lookupAddress(unsigned char *address, int addrLen, TTrieNode *pTN){ |
| 240 |
9000313 |
| unsigned char byte = 0; |
| 241 |
9000313 |
| unsigned char bit = 128; // most significant bit |
| 242 |
|
| // TTrieNode *pTN = pTrieIPV6; |
| 243 |
9000313 |
| TTrieNode *pTNValue = NULL; |
| 244 |
|
| |
| 245 |
9000313 |
▶ | if (pTN == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pTN == NULL) { |
19 |
9000294 |
9000313 |
|
| 246 |
19 |
| return NULL; |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
9000294 |
| unsigned char addrPassed = 0; |
| 250 |
131840728 |
▶ | while(addrPassed++ <= addrLen){ |
Branch: 1 branch
| Branch | True | False | Total |
| while(addrPassed++ <= addrLen){ |
131840728 |
0 |
131840728 |
|
| 251 |
131840728 |
| unsigned char unmasked = (address[byte] & bit); |
| 252 |
131840728 |
| bit = (bit >> 1); |
| 253 |
131840728 |
▶ | if(!bit){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!bit){ |
13428984 |
118411744 |
131840728 |
|
| 254 |
13428984 |
| byte++; |
| 255 |
13428984 |
| bit = 128; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
|
| // pTN with ASNum is desired |
| 259 |
131840728 |
▶ | if(pTN->hasValue){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pTN->hasValue){ |
16632470 |
115208258 |
131840728 |
|
| 260 |
16632470 |
| pTNValue = pTN; |
| 261 |
|
| } |
| 262 |
|
| |
| 263 |
131840728 |
▶ | if(unmasked){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(unmasked){ |
51878780 |
79961948 |
131840728 |
|
| 264 |
51878780 |
▶ | if(pTN->pTN1 != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pTN->pTN1 != NULL){ |
47162624 |
4716156 |
51878780 |
|
| 265 |
47162624 |
| pTN = pTN->pTN1; |
| 266 |
|
| } |
| 267 |
|
| else{ |
| 268 |
4716156 |
| return pTNValue; |
| 269 |
|
| } |
| 270 |
|
| } |
| 271 |
|
| else{ |
| 272 |
79961948 |
▶ | if(pTN->pTN0 != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pTN->pTN0 != NULL){ |
75677810 |
4284138 |
79961948 |
|
| 273 |
75677810 |
| pTN = pTN->pTN0; |
| 274 |
|
| } |
| 275 |
|
| else{ |
| 276 |
4284138 |
| return pTNValue; |
| 277 |
|
| } |
| 278 |
|
| } |
| 279 |
|
| } |
| 280 |
0 |
| return pTNValue; |
| 281 |
|
| } |
| 282 |
|
| |
| 283 |
|
| /** |
| 284 |
|
| * lookupInTrie |
| 285 |
|
| * prefix - holds the prefix that is searched during Trie building |
| 286 |
|
| * byte - byte of prefix |
| 287 |
|
| * bit - bit of prefix byte |
| 288 |
|
| * makeMatches - input and output,indicates prefixLen that can be used |
| 289 |
|
| * ppTN - input and output, determines which Node of Trie was examined last during function proccess, at start holds the root of Trie |
| 290 |
|
| * root - return flag, true if root of Trie must be build first |
| 291 |
|
| * returns NULL when match current prefix during Trie building |
| 292 |
|
| */ |
| 293 |
458948 |
| TTrieNode **lookupInTrie(unsigned char *prefix, unsigned char *byte, unsigned char *bit, unsigned char *makeMatches, TTrieNode **ppTN, bool *root){ |
| 294 |
458948 |
▶ | if((*ppTN) == NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if((*ppTN) == NULL){ |
12 |
458936 |
458948 |
|
| 295 |
12 |
| (*root) = true; |
| 296 |
12 |
| return ppTN; |
| 297 |
|
| } |
| 298 |
|
| |
| 299 |
9834995 |
▶ | while((*makeMatches)){ |
Branch: 1 branch
| Branch | True | False | Total |
| while((*makeMatches)){ |
9832256 |
2739 |
9834995 |
|
| 300 |
9832256 |
| unsigned char unmasked = (prefix[(*byte)] & (*bit)); |
| 301 |
9832256 |
| (*makeMatches)--; |
| 302 |
9832256 |
| (*bit) = ((*bit) >> 1); |
| 303 |
9832256 |
▶ | if(!(*bit)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(*bit)){ |
1013876 |
8818380 |
9832256 |
|
| 304 |
1013876 |
| (*byte)++; |
| 305 |
1013876 |
| (*bit) = 128; |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
9832256 |
▶ | if(unmasked){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(unmasked){ |
4985472 |
4846784 |
9832256 |
|
| 309 |
4985472 |
▶ | if((*ppTN)->pTN1 != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if((*ppTN)->pTN1 != NULL){ |
4580053 |
405419 |
4985472 |
|
| 310 |
4580053 |
| (*ppTN) = (*ppTN)->pTN1; |
| 311 |
|
| } |
| 312 |
|
| else{ |
| 313 |
405419 |
| return &((*ppTN)->pTN1); |
| 314 |
|
| } |
| 315 |
|
| } |
| 316 |
|
| else{ |
| 317 |
4846784 |
▶ | if((*ppTN)->pTN0 != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if((*ppTN)->pTN0 != NULL){ |
4796006 |
50778 |
4846784 |
|
| 318 |
4796006 |
| (*ppTN) = (*ppTN)->pTN0; |
| 319 |
|
| } |
| 320 |
|
| else{ |
| 321 |
50778 |
| return &((*ppTN)->pTN0); |
| 322 |
|
| } |
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
2739 |
| (*root) = false; |
| 327 |
2739 |
| return NULL; |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
|
| /** |
| 331 |
|
| * myMalloc |
| 332 |
|
| * encapsulates real malloc function but call it less times |
| 333 |
|
| * and that is why it could save some presious time |
| 334 |
|
| */ |
| 335 |
0 |
| TTrieNode *myMalloc(){ |
| 336 |
0 |
▶ | if(pAlocated == NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pAlocated == NULL){ |
0 |
0 |
0 |
|
| 337 |
0 |
| pAlocated = malloc(sizeof(struct AlocTrieNodes)); |
| 338 |
0 |
▶ | if(pAlocated == NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pAlocated == NULL){ |
0 |
0 |
0 |
|
| 339 |
0 |
| return NULL; |
| 340 |
|
| } |
| 341 |
|
| |
| 342 |
0 |
| pAlocated->pNextATN = NULL; |
| 343 |
0 |
| pActual = pAlocated; |
| 344 |
0 |
| pActualTN = &pActual->TrieNodes[0]; |
| 345 |
0 |
| pLastTN = &pActual->TrieNodes[lastAlocIndex]; |
| 346 |
|
| } |
| 347 |
|
| |
| 348 |
|
| // Save to return it later |
| 349 |
0 |
| TTrieNode *pReturn = pActualTN; |
| 350 |
|
| |
| 351 |
0 |
▶ | if(pActualTN == pLastTN){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pActualTN == pLastTN){ |
0 |
0 |
0 |
|
| 352 |
0 |
| pActual->pNextATN = malloc(sizeof(struct AlocTrieNodes)); |
| 353 |
0 |
| pActual = pActual->pNextATN; |
| 354 |
0 |
▶ | if(pActual == NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(pActual == NULL){ |
0 |
0 |
0 |
|
| 355 |
0 |
| return NULL; |
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
0 |
| pActual->pNextATN = NULL; |
| 359 |
0 |
| pActualTN = &pActual->TrieNodes[0]; |
| 360 |
0 |
| pLastTN = &pActual->TrieNodes[lastAlocIndex]; |
| 361 |
|
| } |
| 362 |
|
| else{ |
| 363 |
0 |
| pActualTN++; |
| 364 |
|
| } |
| 365 |
|
| |
| 366 |
0 |
| return pReturn; |
| 367 |
|
| } |
| 368 |
|
| |
| 369 |
|
| |
| 370 |
|
| /********************************************************************/
|
| 371 |
|
| /* PERL INTERFACE */
|
| 372 |
|
| /********************************************************************/
|
| 373 |
|
| |
| 374 |
26 |
| int lpm_init(void) { |
| 375 |
26 |
| int handle = 1; |
| 376 |
|
| lpm_instance_t *instance; |
| 377 |
|
| //int i; |
| 378 |
|
| |
| 379 |
|
| /* find the first free handler and assign to array of open handlers/instances */ |
| 380 |
32 |
▶ | while (lpm_instances[handle] != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lpm_instances[handle] != NULL) { |
6 |
26 |
32 |
|
| 381 |
6 |
| handle++; |
| 382 |
6 |
▶ | if (handle >= LPM_MAX_INSTANCES - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (handle >= LPM_MAX_INSTANCES - 1) { |
0 |
6 |
6 |
|
| 383 |
0 |
| croak("No free handles available, max instances %d reached", LPM_MAX_INSTANCES); |
| 384 |
|
| return 0; |
| 385 |
|
| } |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
26 |
| instance = malloc(sizeof(lpm_instance_t)); |
| 389 |
26 |
▶ | if (instance == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance == NULL) { |
0 |
26 |
26 |
|
| 390 |
0 |
| croak("can not allocate memory for instance"); |
| 391 |
|
| return 0; |
| 392 |
|
| } |
| 393 |
|
| |
| 394 |
26 |
| memset(instance, 0, sizeof(lpm_instance_t)); |
| 395 |
|
| |
| 396 |
26 |
| instance->pTrieIPV4 = NULL; |
| 397 |
26 |
| instance->pTrieIPV6 = NULL; |
| 398 |
|
| |
| 399 |
26 |
| lpm_instances[handle] = instance; |
| 400 |
|
| |
| 401 |
26 |
| return handle; |
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
458954 |
| int lpm_add_raw(int handle, SV * svprefix, IV prefix_len, SV *value) { |
| 405 |
458954 |
| lpm_instance_t *instance = lpm_instances[handle]; |
| 406 |
|
| STRLEN len; |
| 407 |
|
| char * prefix; |
| 408 |
|
| |
| 409 |
458954 |
▶ | if (instance == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance == NULL ) { |
0 |
458954 |
458954 |
|
| 410 |
0 |
| croak("handler %d not initialized", handle); |
| 411 |
|
| return 0; |
| 412 |
|
| } |
| 413 |
|
| |
| 414 |
458954 |
| prefix = SvPV(svprefix, len); |
| 415 |
|
| |
| 416 |
458954 |
▶ | if ((len == 4 || len == 16) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((len == 4 || len == 16) |
11958 |
446996 |
458954 |
| if ((len == 4 || len == 16) |
11958 |
0 |
11958 |
|
| 417 |
458954 |
▶ | && (prefix_len < 0 || (STRLEN)prefix_len > len * 8)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (prefix_len < 0 || (STRLEN)prefix_len > len * 8)) { |
458954 |
0 |
458954 |
| && (prefix_len < 0 || (STRLEN)prefix_len > len * 8)) { |
6 |
458948 |
458954 |
|
| 418 |
6 |
| croak("Net::IP::LPM: prefix length /%d out of range (max /%d)", |
| 419 |
|
| prefix_len, (int)(len * 8)); |
| 420 |
|
| return 0; |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
458948 |
▶ | if (len == 4){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 4){ |
446992 |
11956 |
458948 |
|
| 424 |
446992 |
| addPrefixToTrie((void *)prefix, (int) prefix_len, value, &instance->pTrieIPV4); |
| 425 |
|
| } |
| 426 |
11956 |
▶ | else if (len == 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (len == 16) { |
11956 |
0 |
11956 |
|
| 427 |
11956 |
| addPrefixToTrie((void *)prefix, (int) prefix_len, value, &instance->pTrieIPV6); |
| 428 |
|
| } |
| 429 |
|
| else{ // Corrupted input file |
| 430 |
0 |
| croak("Cannot add prefix %s", prefix); |
| 431 |
|
| return 0; |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
|
| /* included code */ |
| 435 |
458948 |
| return 1; |
| 436 |
|
| } |
| 437 |
|
| |
| 438 |
18000245 |
| SV *lpm_lookup_raw(int handle, SV *svaddr) { |
| 439 |
18000245 |
| lpm_instance_t *instance = lpm_instances[handle]; |
| 440 |
|
| //SV *out = NULL; |
| 441 |
|
| //char buf[BUFFSIZE]; |
| 442 |
|
| //char *buf = NULL; |
| 443 |
|
| char *addr; |
| 444 |
|
| STRLEN len; |
| 445 |
|
| |
| 446 |
18000245 |
▶ | if (instance == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance == NULL ) { |
0 |
18000245 |
18000245 |
|
| 447 |
0 |
| croak("handler %d not initialized", handle); |
| 448 |
|
| return NULL; |
| 449 |
|
| } |
| 450 |
|
| |
| 451 |
18000245 |
| TTrieNode *pTN = NULL; |
| 452 |
18000245 |
| addr = SvPV(svaddr, len); |
| 453 |
|
| |
| 454 |
18000245 |
▶ | if(len == 4){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(len == 4){ |
9000204 |
9000041 |
18000245 |
|
| 455 |
9000204 |
| pTN = lookupAddress((void *)addr, 32, instance->pTrieIPV4); |
| 456 |
|
| } |
| 457 |
9000041 |
▶ | else if(len == 16){ // IPV6 |
Branch: 1 branch
| Branch | True | False | Total |
| else if(len == 16){ // IPV6 |
109 |
8999932 |
9000041 |
|
| 458 |
109 |
| pTN = lookupAddress((void *)addr, 128, instance->pTrieIPV6); |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
18000245 |
▶ | if ( pTN == NULL ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( pTN == NULL ){ |
8999995 |
9000250 |
18000245 |
|
| 462 |
8999995 |
| return &PL_sv_undef; |
| 463 |
|
| } else { |
| 464 |
9000250 |
| SvREFCNT_inc(pTN->Value); |
| 465 |
9000250 |
| return pTN->Value; |
| 466 |
|
| } |
| 467 |
|
| } |
| 468 |
|
| |
| 469 |
1 |
| SV * lpm_info(int handle) { |
| 470 |
1 |
| lpm_instance_t *instance = lpm_instances[handle]; |
| 471 |
|
| int totalNodes, valueNodes, trieBytes, dataBytes; |
| 472 |
|
| HV * res; |
| 473 |
|
| |
| 474 |
1 |
▶ | if (instance == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance == NULL ) { |
0 |
1 |
1 |
|
| 475 |
0 |
| croak("handler %d not initialized", handle); |
| 476 |
|
| return &PL_sv_undef; |
| 477 |
|
| } |
| 478 |
|
| |
| 479 |
1 |
| res = (HV *)sv_2mortal((SV *)newHV()); |
| 480 |
|
| |
| 481 |
1 |
| totalNodes = 0; |
| 482 |
1 |
| valueNodes = 0; |
| 483 |
1 |
| trieBytes = 0; |
| 484 |
1 |
| dataBytes = 0; |
| 485 |
1 |
| countTrieNode(instance->pTrieIPV4, &totalNodes, &valueNodes, &trieBytes, &dataBytes); |
| 486 |
1 |
| HV_STORE_NV(res, "ipv4_nodes_total", totalNodes); |
| 487 |
1 |
| HV_STORE_NV(res, "ipv4_nodes_value", valueNodes); |
| 488 |
1 |
| HV_STORE_NV(res, "ipv4_trie_bytes", trieBytes); |
| 489 |
|
| // HV_STORE_NV(res, "ipv4_data_bytes", dataBytes); |
| 490 |
|
| |
| 491 |
1 |
| totalNodes = 0; |
| 492 |
1 |
| valueNodes = 0; |
| 493 |
1 |
| trieBytes = 0; |
| 494 |
1 |
| dataBytes = 0; |
| 495 |
1 |
| countTrieNode(instance->pTrieIPV6, &totalNodes, &valueNodes, &trieBytes, &dataBytes); |
| 496 |
1 |
| HV_STORE_NV(res, "ipv6_nodes_total", totalNodes); |
| 497 |
1 |
| HV_STORE_NV(res, "ipv6_nodes_value", valueNodes); |
| 498 |
1 |
| HV_STORE_NV(res, "ipv6_trie_bytes", trieBytes); |
| 499 |
|
| // HV_STORE_NV(res, "ipv6_data_bytes", dataBytes); |
| 500 |
|
| |
| 501 |
1 |
| return newRV((SV *)res); |
| 502 |
|
| |
| 503 |
|
| } |
| 504 |
|
| |
| 505 |
3 |
| SV * lpm_dump(int handle) { |
| 506 |
3 |
| lpm_instance_t *instance = lpm_instances[handle]; |
| 507 |
|
| TTrieNodeList *ptmp; |
| 508 |
3 |
| TTrieNodeList *plist = NULL; |
| 509 |
|
| HV * res; |
| 510 |
|
| unsigned char prefix[BUFFSIZE]; |
| 511 |
|
| int depth; |
| 512 |
|
| |
| 513 |
3 |
▶ | if (instance == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance == NULL ) { |
0 |
3 |
3 |
|
| 514 |
0 |
| croak("handler %d not initialized", handle); |
| 515 |
|
| return &PL_sv_undef; |
| 516 |
|
| } |
| 517 |
|
| |
| 518 |
3 |
| res = (HV *)sv_2mortal((SV *)newHV()); |
| 519 |
|
| |
| 520 |
3 |
▶ | if (instance->pTrieIPV4 != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance->pTrieIPV4 != NULL) { |
3 |
0 |
3 |
|
| 521 |
3 |
| depth = 0; |
| 522 |
3 |
| memset(&prefix, 0x0, sizeof(prefix)); |
| 523 |
3 |
| listTrieNode(instance->pTrieIPV4, &plist, AF_INET, &depth, prefix); |
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
3 |
▶ | if (instance->pTrieIPV6 != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance->pTrieIPV6 != NULL) { |
2 |
1 |
3 |
|
| 527 |
2 |
| depth = 0; |
| 528 |
2 |
| memset(&prefix, 0x0, sizeof(prefix)); |
| 529 |
2 |
| listTrieNode(instance->pTrieIPV6, &plist, AF_INET6, &depth, prefix); |
| 530 |
|
| } |
| 531 |
|
| |
| 532 |
|
| |
| 533 |
36 |
▶ | while (plist != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (plist != NULL) { |
33 |
3 |
36 |
|
| 534 |
|
| STRLEN len; |
| 535 |
|
| char buf[BUFFSIZE]; |
| 536 |
|
| char buf2[BUFFSIZE]; |
| 537 |
|
| int allocsize; |
| 538 |
|
| |
| 539 |
33 |
| allocsize = ((plist->Depth - 1) / 8) + 1; |
| 540 |
|
| |
| 541 |
33 |
| memset(prefix, 0x0, BUFFSIZE - 1); |
| 542 |
33 |
| memcpy(prefix, plist->Prefix, allocsize); |
| 543 |
|
| |
| 544 |
33 |
| inet_ntop(plist->AfType, prefix, buf, BUFFSIZE); |
| 545 |
|
| |
| 546 |
33 |
| sprintf(buf2, "%s/%d", buf, plist->Depth); |
| 547 |
|
| |
| 548 |
33 |
| SvREFCNT_inc(plist->pNode->Value); |
| 549 |
33 |
| hv_store(res, buf2, strlen(buf2), plist->pNode->Value, 0); |
| 550 |
|
| |
| 551 |
33 |
| ptmp = plist; |
| 552 |
33 |
| plist = plist->pNext; |
| 553 |
33 |
| free(ptmp); |
| 554 |
|
| } |
| 555 |
|
| |
| 556 |
3 |
| return newRV((SV *)res); |
| 557 |
|
| } |
| 558 |
|
| |
| 559 |
26 |
| void lpm_finish(int handle) { |
| 560 |
26 |
| lpm_instance_t *instance = lpm_instances[handle]; |
| 561 |
|
| |
| 562 |
26 |
▶ | if (instance == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance == NULL ) { |
0 |
26 |
26 |
|
| 563 |
0 |
| croak("handler %d not initialized", handle); |
| 564 |
|
| return; |
| 565 |
|
| } |
| 566 |
|
| |
| 567 |
26 |
▶ | if (instance->pTrieIPV4 != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance->pTrieIPV4 != NULL) { |
7 |
19 |
26 |
|
| 568 |
7 |
| freeTrieNode(instance->pTrieIPV4); |
| 569 |
7 |
| instance->pTrieIPV4 = NULL; |
| 570 |
|
| } |
| 571 |
|
| |
| 572 |
26 |
▶ | if (instance->pTrieIPV6 != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance->pTrieIPV6 != NULL) { |
5 |
21 |
26 |
|
| 573 |
5 |
| freeTrieNode(instance->pTrieIPV6); |
| 574 |
5 |
| instance->pTrieIPV6 = NULL; |
| 575 |
|
| } |
| 576 |
26 |
| return; |
| 577 |
|
| } |
| 578 |
|
| |
| 579 |
|
| |
| 580 |
26 |
| void lpm_destroy(int handle) { |
| 581 |
26 |
| lpm_instance_t *instance = lpm_instances[handle]; |
| 582 |
|
| |
| 583 |
26 |
▶ | if (instance == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (instance == NULL ) { |
0 |
26 |
26 |
|
| 584 |
0 |
| croak("handler %d not initialized", handle); |
| 585 |
|
| return; |
| 586 |
|
| } |
| 587 |
|
| |
| 588 |
|
| // freeAlocTrieNodes(instance->pTrieIPV4); |
| 589 |
|
| // freeAlocTrieNodes(instance->pTrieIPV6); |
| 590 |
|
| |
| 591 |
26 |
| lpm_finish(handle); |
| 592 |
26 |
| free(instance); |
| 593 |
26 |
| lpm_instances[handle] = NULL; |
| 594 |
|
| |
| 595 |
26 |
| return; |
| 596 |
|
| } |