lpm_lib.c

filter: Statementstmt 84.5% 218 / 258 Branchbran 75.0% 87 / 116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.5% 305 / 374 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« LPM.c ↑ Summary
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 2311519 void freeTrieNode(TTrieNode *pTN) {
83 2311519 ▶ if (pTN != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pTN != NULL) { 1155756 1155763 2311519
84 1155756 ▶ if (pTN->hasValue) {
Branch: 1 branch
BranchTrueFalseTotal
if (pTN->hasValue) { 458930 696826 1155756
85 458930 pTN->hasValue = false;
86 458930 SvREFCNT_dec(pTN->Value);
87 }
88 1155756 freeTrieNode(pTN->pTN0);
89 1155756 freeTrieNode(pTN->pTN1);
90 1155756 free(pTN);
91 }
92 2311519 }
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
BranchTrueFalseTotal
if (pTN != NULL) { 1155261 1155263 2310524
97 1155261 ▶ if (pTN->hasValue) {
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
if (pTN != NULL) { 495 500 995
114 495 ▶ if (pTN->hasValue) {
Branch: 1 branch
BranchTrueFalseTotal
if (pTN->hasValue) { 33 462 495
115 33 ptmp = malloc(sizeof(TTrieNodeList));
116 33 ▶ if (ptmp == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 458940 void addPrefixToTrie(unsigned char *prefix, unsigned char prefixLen, SV * Value, TTrieNode **ppTrie){
157 458940 unsigned char byte = 0;
158 458940 unsigned char bit = 128;
159  
160 458940 unsigned char makeMatches = prefixLen;
161 458940 bool root = false;
162 458940 TTrieNode *pFound = (*ppTrie);
163 458940 TTrieNode **ppTN = lookupInTrie(prefix, &byte, &bit, &makeMatches, &pFound, &root);
164 458940 ▶ if(root){
Branch: 1 branch
BranchTrueFalseTotal
if(root){ 7 458933 458940
165 7 ppTN = ppTrie;
166 }
167 458940 ▶ if(ppTN != NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(ppTN != NULL){ 456201 2739 458940
168 1155756 ▶ while(makeMatches){
Branch: 1 branch
BranchTrueFalseTotal
while(makeMatches){ 699555 456201 1155756
169 699555 (*ppTN) = createTrieNode();
170 699555 unsigned char unmasked = (prefix[byte] & bit);
171  
172 699555 ▶ if(unmasked){
Branch: 1 branch
BranchTrueFalseTotal
if(unmasked){ 140367 559188 699555
173 140367 ppTN = &((*ppTN)->pTN1);
174 }
175 else{
176 559188 ppTN = &((*ppTN)->pTN0);
177 }
178  
179 699555 makeMatches--;
180 699555 bit = (bit >> 1);
181 699555 ▶ if(!bit){
Branch: 1 branch
BranchTrueFalseTotal
if(!bit){ 177408 522147 699555
182 177408 byte++;
183 177408 bit = 128;
184 }
185 }
186  
187 456201 (*ppTN) = createTrieNode();
188 456201 (*ppTN)->Value = Value;
189 456201 (*ppTN)->hasValue = true;
190 456201 SvREFCNT_inc(Value);
191 }
192 else{
193 2739 ▶ if(!pFound->hasValue){
Branch: 1 branch
BranchTrueFalseTotal
if(!pFound->hasValue){ 2729 10 2739
194 2729 pFound->Value = Value;
195 2729 pFound->hasValue = true;
196 2729 SvREFCNT_inc(Value);
197 }
198 }
199 458940 }
200  
201 /**
202 * createTrieNode creates Node of Trie and sets him implicitly up
203 * returns NULL or created Node of Trie
204 */
205 1155756 TTrieNode *createTrieNode(){
206 //TTrieNode *pTN = myMalloc(sizeof(struct TrieNode));
207 1155756 TTrieNode *pTN = malloc(sizeof(struct TrieNode));
208 1155756 ▶ if(pTN == NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(pTN == NULL){ 0 1155756 1155756
209 0 fprintf(stderr, "pTN malloc error.");
210 0 return NULL;
211 }
212  
213 // initialize
214 1155756 pTN->pTN0 = NULL;
215 1155756 pTN->pTN1 = NULL;
216 1155756 pTN->hasValue = false;
217  
218 1155756 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
BranchTrueFalseTotal
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 9000286 TTrieNode *lookupAddress(unsigned char *address, int addrLen, TTrieNode *pTN){
240 9000286 unsigned char byte = 0;
241 9000286 unsigned char bit = 128; // most significant bit
242 // TTrieNode *pTN = pTrieIPV6;
243 9000286 TTrieNode *pTNValue = NULL;
244  
245 9000286 ▶ if (pTN == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pTN == NULL) { 2 9000284 9000286
246 2 return NULL;
247 }
248  
249 9000284 unsigned char addrPassed = 0;
250 131840331 ▶ while(addrPassed++ <= addrLen){
Branch: 1 branch
BranchTrueFalseTotal
while(addrPassed++ <= addrLen){ 131840331 0 131840331
251 131840331 unsigned char unmasked = (address[byte] & bit);
252 131840331 bit = (bit >> 1);
253 131840331 ▶ if(!bit){
Branch: 1 branch
BranchTrueFalseTotal
if(!bit){ 13428938 118411393 131840331
254 13428938 byte++;
255 13428938 bit = 128;
256 }
257  
258 // pTN with ASNum is desired
259 131840331 ▶ if(pTN->hasValue){
Branch: 1 branch
BranchTrueFalseTotal
if(pTN->hasValue){ 16632460 115207871 131840331
260 16632460 pTNValue = pTN;
261 }
262  
263 131840331 ▶ if(unmasked){
Branch: 1 branch
BranchTrueFalseTotal
if(unmasked){ 51878736 79961595 131840331
264 51878736 ▶ if(pTN->pTN1 != NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(pTN->pTN1 != NULL){ 47162583 4716153 51878736
265 47162583 pTN = pTN->pTN1;
266 }
267 else{
268 4716153 return pTNValue;
269 }
270 }
271 else{
272 79961595 ▶ if(pTN->pTN0 != NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(pTN->pTN0 != NULL){ 75677464 4284131 79961595
273 75677464 pTN = pTN->pTN0;
274 }
275 else{
276 4284131 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 458940 TTrieNode **lookupInTrie(unsigned char *prefix, unsigned char *byte, unsigned char *bit, unsigned char *makeMatches, TTrieNode **ppTN, bool *root){
294 458940 ▶ if((*ppTN) == NULL){
Branch: 1 branch
BranchTrueFalseTotal
if((*ppTN) == NULL){ 7 458933 458940
295 7 (*root) = true;
296 7 return ppTN;
297 }
298  
299 9834952 ▶ while((*makeMatches)){
Branch: 1 branch
BranchTrueFalseTotal
while((*makeMatches)){ 9832213 2739 9834952
300 9832213 unsigned char unmasked = (prefix[(*byte)] & (*bit));
301 9832213 (*makeMatches)--;
302 9832213 (*bit) = ((*bit) >> 1);
303 9832213 ▶ if(!(*bit)){
Branch: 1 branch
BranchTrueFalseTotal
if(!(*bit)){ 1013871 8818342 9832213
304 1013871 (*byte)++;
305 1013871 (*bit) = 128;
306 }
307  
308 9832213 ▶ if(unmasked){
Branch: 1 branch
BranchTrueFalseTotal
if(unmasked){ 4985460 4846753 9832213
309 4985460 ▶ if((*ppTN)->pTN1 != NULL){
Branch: 1 branch
BranchTrueFalseTotal
if((*ppTN)->pTN1 != NULL){ 4580042 405418 4985460
310 4580042 (*ppTN) = (*ppTN)->pTN1;
311 }
312 else{
313 405418 return &((*ppTN)->pTN1);
314 }
315 }
316 else{
317 4846753 ▶ if((*ppTN)->pTN0 != NULL){
Branch: 1 branch
BranchTrueFalseTotal
if((*ppTN)->pTN0 != NULL){ 4795977 50776 4846753
318 4795977 (*ppTN) = (*ppTN)->pTN0;
319 }
320 else{
321 50776 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
BranchTrueFalseTotal
if(pAlocated == NULL){ 0 0 0
337 0 pAlocated = malloc(sizeof(struct AlocTrieNodes));
338 0 ▶ if(pAlocated == NULL){
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 5 int lpm_init(void) {
375 5 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 11 ▶ while (lpm_instances[handle] != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (lpm_instances[handle] != NULL) { 6 5 11
381 6 handle++;
382 6 ▶ if (handle >= LPM_MAX_INSTANCES - 1) {
Branch: 1 branch
BranchTrueFalseTotal
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 5 instance = malloc(sizeof(lpm_instance_t));
389 5 ▶ if (instance == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (instance == NULL) { 0 5 5
390 0 croak("can not allocate memory for instance");
391 return 0;
392 }
393  
394 5 memset(instance, 0, sizeof(lpm_instance_t));
395  
396 5 instance->pTrieIPV4 = NULL;
397 5 instance->pTrieIPV6 = NULL;
398  
399 5 lpm_instances[handle] = instance;
400  
401 5 return handle;
402 }
403  
404 458940 int lpm_add_raw(int handle, SV * svprefix, int prefix_len, SV *value) {
405 458940 lpm_instance_t *instance = lpm_instances[handle];
406 STRLEN len;
407 char * prefix;
408  
409 458940 ▶ if (instance == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if (instance == NULL ) { 0 458940 458940
410 0 croak("handler %d not initialized", handle);
411 return 0;
412 }
413  
414 458940 prefix = SvPV(svprefix, len);
415  
416 458940 ▶ if ((len == 4 || len == 16)
Branch: 2 branches
BranchTrueFalseTotal
if ((len == 4 || len == 16) 11953 446987 458940
if ((len == 4 || len == 16) 11953 0 11953
417 458940 ▶ && (prefix_len < 0 || (STRLEN)prefix_len > len * 8)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (prefix_len < 0 || (STRLEN)prefix_len > len * 8)) { 458940 0 458940
&& (prefix_len < 0 || (STRLEN)prefix_len > len * 8)) { 0 458940 458940
418 0 croak("Net::IP::LPM: prefix length /%d out of range (max /%d)",
419 prefix_len, (int)(len * 8));
420 return 0;
421 }
422  
423 458940 ▶ if (len == 4){
Branch: 1 branch
BranchTrueFalseTotal
if (len == 4){ 446987 11953 458940
424 446987 addPrefixToTrie((void *)prefix, prefix_len, value, &instance->pTrieIPV4);
425 }
426 11953 ▶ else if (len == 16) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len == 16) { 11953 0 11953
427 11953 addPrefixToTrie((void *)prefix, 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 458940 return 1;
436 }
437  
438 18000218 SV *lpm_lookup_raw(int handle, SV *svaddr) {
439 18000218 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 18000218 ▶ if (instance == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if (instance == NULL ) { 0 18000218 18000218
447 0 croak("handler %d not initialized", handle);
448 return NULL;
449 }
450  
451 18000218 TTrieNode *pTN = NULL;
452 18000218 addr = SvPV(svaddr, len);
453  
454 18000218 ▶ if(len == 4){
Branch: 1 branch
BranchTrueFalseTotal
if(len == 4){ 9000183 9000035 18000218
455 9000183 pTN = lookupAddress((void *)addr, 32, instance->pTrieIPV4);
456 }
457 9000035 ▶ else if(len == 16){ // IPV6
Branch: 1 branch
BranchTrueFalseTotal
else if(len == 16){ // IPV6 103 8999932 9000035
458 103 pTN = lookupAddress((void *)addr, 128, instance->pTrieIPV6);
459 }
460  
461 18000218 ▶ if ( pTN == NULL ){
Branch: 1 branch
BranchTrueFalseTotal
if ( pTN == NULL ){ 8999976 9000242 18000218
462 8999976 return &PL_sv_undef;
463 } else {
464 9000242 SvREFCNT_inc(pTN->Value);
465 9000242 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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 5 void lpm_finish(int handle) {
560 5 lpm_instance_t *instance = lpm_instances[handle];
561  
562 5 ▶ if (instance == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if (instance == NULL ) { 0 5 5
563 0 croak("handler %d not initialized", handle);
564 return;
565 }
566  
567 5 ▶ if (instance->pTrieIPV4 != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (instance->pTrieIPV4 != NULL) { 4 1 5
568 4 freeTrieNode(instance->pTrieIPV4);
569 4 instance->pTrieIPV4 = NULL;
570 }
571  
572 5 ▶ if (instance->pTrieIPV6 != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (instance->pTrieIPV6 != NULL) { 3 2 5
573 3 freeTrieNode(instance->pTrieIPV6);
574 3 instance->pTrieIPV6 = NULL;
575 }
576 5 return;
577 }
578  
579  
580 5 void lpm_destroy(int handle) {
581 5 lpm_instance_t *instance = lpm_instances[handle];
582  
583 5 ▶ if (instance == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if (instance == NULL ) { 0 5 5
584 0 croak("handler %d not initialized", handle);
585 return;
586 }
587  
588 // freeAlocTrieNodes(instance->pTrieIPV4);
589 // freeAlocTrieNodes(instance->pTrieIPV6);
590  
591 5 lpm_finish(handle);
592 5 free(instance);
593 5 lpm_instances[handle] = NULL;
594  
595 5 return;
596 }
« LPM.c ↑ Summary