MRT.xs

filter: Statementstmt 0.0% 0 / 310 Branchbran 0.0% 0 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 414 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Net/MRT.pm »
1 /*
2 * MRT.xs
3 * $Id$
4 *
5 * Copyright (C) 2013 MaxiM Basunov <maxim.basunov@gmail.com>
6 * All rights reserved.
7 *
8 * This program is free software; you may redistribute it and/or
9 * modify it under the same terms as Perl itself.
10 */
11  
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15 // #include "poll.h"
16 #ifdef I_UNISTD
17 # include <unistd.h>
18 #endif
19 #if defined(I_FCNTL) || defined(HAS_FCNTL)
20 # include <fcntl.h>
21 #endif
22  
23 //#include "ppport.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <arpa/inet.h>
29 #include "mrttypes.h"
30  
31 // Windows XP workaround for inet_ntop
32 // TODO: Windows Vista/7 can use InetNtop
33 // TODO: http://vinsworldcom.blogspot.com/2012/08/ipv6-in-perl-on-windows_20.html
34 #ifdef WIN32
35 #include "inet_ntop.c"
36 #endif
37  
38 // String buffer size
39 #define SBUFF 200
40  
41 // Global variables
42 SV* USE_RFC4760;
43  
44 // Function to decode single MRT message and compose HV contents
45 0 void mrt_decode(HV* const rt, Off_t const msgpos, MRT_MESSAGE* const mh)
46 {
47 0 char sbuff[SBUFF] = {}; // Char buffer for SNPRINTF
48 0 char* pPos = (char*)&mh->message; // Current read position
49 0 int AF = 0; // Address family from message
50 uint32_t iSeq; // Sequence number from MRT
51 struct sockaddr_in6 sa6;
52 0 memset(&sa6, 0, sizeof(sa6));
53 char cIpAddress[INET6_ADDRSTRLEN]; // Buffer for INET_PTON
54 0 int iRemainingLen = mh->length; // Self-check for buffer remaining length
55  
56 // Some temporary variables
57 AV* avTmpAv;
58 uint8_t iTmpU8;
59 uint16_t iTmpU16;
60 uint32_t iTmpU32;
61  
62 # ifdef _DEBUG_
63 printf("mrt_decode_single(): Input type: %d subtype %d data len: %d\n", mh->type, mh->subtype, mh->length);
64 # endif
65 0 ▶ switch (mh->type) {
Branch: 1 branch
BranchTrueFalseTotal
switch (mh->type) { 0 0 0
66 0 case MT_TABLE_DUMP_V2:
67 0 switch (mh->subtype) {
68 0 case MST_TD2_PEER_INDEX_TABLE:
69 //PEER_INDEX_TABLE
70 //4 Collector BGP ID - ID of this collector
71 //2 view name length (can be =0)
72 //V View Name (optional)
73 //2 Peer count
74 //V Peers (index start with 0)
75 //
76 //PEER:
77 //1 PEer type 000000AI. A=2 - AS 32 bit; IP=1 - IPv6
78 //4 Peer BGP ID
79 //IP Peer IP address
80 //AS Peer AS number
81 // Decode Collector BGP ID
82 0 memset(&sa6, 0, sizeof(sa6));
83 0 mrt_copy_next(&pPos, &sa6, 4, &iRemainingLen);
84 0 inet_ntop(AF_INET, &sa6, cIpAddress, INET6_ADDRSTRLEN);
85 0 hv_stores(rt, "collector_bgp_id", newSVpv(cIpAddress, 0));
86  
87 // Decode View name
88 0 mrt_copy_next(&pPos, &iTmpU16, 2, &iRemainingLen);
89 0 iTmpU16 = ntohs(iTmpU16);
90 0 ▶ if (iTmpU16 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (iTmpU16 > 0) { 0 0 0
91 0 SV* svViewName = newSVpv(pPos, iTmpU16);
92 0 SvUTF8_on(svViewName);
93 0 hv_stores(rt, "view_name", svViewName);
94 0 pPos += iTmpU16;
95 0 iRemainingLen -= iTmpU16;
96 } else {
97 0 hv_stores(rt, "view_name", &PL_sv_undef);
98 }
99  
100 // Decode amount of peers
101 uint16_t iPeerCount;
102 0 mrt_copy_next(&pPos, &iPeerCount, 2, &iRemainingLen);
103 0 iPeerCount = htons(iPeerCount);
104  
105 // Store peers container
106 0 avTmpAv = newAV();
107 0 hv_stores(rt, "peers", newRV_noinc((SV *)avTmpAv));
108  
109 // Decode peers
110 0 ▶ while (iPeerCount > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (iPeerCount > 0) 0 0 0
111 {
112 0 iPeerCount--;
113 // Append new onePeer container
114 0 HV* hvOnePeer = newHV();
115 0 av_push(avTmpAv, newRV_noinc((SV *)hvOnePeer));
116  
117 // Decode Peer's Type
118 PEER_TYPE ptPT;
119 0 mrt_copy_next(&pPos, &ptPT, 1, &iRemainingLen);
120  
121 // Decode Peer's BGP ID
122 0 memset(&sa6, 0, sizeof(sa6));
123 0 mrt_copy_next(&pPos, &sa6, 4, &iRemainingLen);
124 0 inet_ntop(AF_INET, &sa6, cIpAddress, INET6_ADDRSTRLEN);
125 0 hv_stores(hvOnePeer, "bgp_id", newSVpv(cIpAddress, 0));
126  
127 // Decode Peer's IP address
128 0 memset(&sa6, 0, sizeof(sa6));
129 0 ▶ mrt_copy_next(&pPos, &sa6, (!ptPT.ipv6 ? 4 : 16), &iRemainingLen);
Branch: 1 branch
BranchTrueFalseTotal
mrt_copy_next(&pPos, &sa6, (!ptPT.ipv6 ? 4 : 16), &iRemainingLen); 0 0 0
130 0 ▶ inet_ntop((!ptPT.ipv6 ? AF_INET : AF_INET6), &sa6, cIpAddress, INET6_ADDRSTRLEN);
Branch: 1 branch
BranchTrueFalseTotal
inet_ntop((!ptPT.ipv6 ? AF_INET : AF_INET6), &sa6, cIpAddress, INET6_ADDRSTRLEN); 0 0 0
131 0 hv_stores(hvOnePeer, "peer_ip", newSVpv(cIpAddress, 0));
132  
133 // Decode Peer's AS number
134 0 ▶ if (!ptPT.as32)
Branch: 1 branch
BranchTrueFalseTotal
if (!ptPT.as32) 0 0 0
135 {
136 0 mrt_copy_next(&pPos, &iTmpU16, 2, &iRemainingLen);
137 0 iTmpU16 = ntohs(iTmpU16);
138 0 hv_stores(hvOnePeer, "as", newSVuv(iTmpU16));
139 } else {
140 0 mrt_copy_next(&pPos, &iTmpU32, 4, &iRemainingLen);
141 0 iTmpU32 = ntohl(iTmpU32);
142 0 hv_stores(hvOnePeer, "as", newSVuv(iTmpU32));
143 }
144 } // while (iPeerCount > 0)
145 0 break; // MST_TD2_PEER_INDEX_TABLE
146 // Try to decode MULTICAST/ANYCAST
147 //case MST_TD2_RIB_IPV6_MULTICAST:
148 0 case MST_TD2_RIB_IPV6_UNICAST:
149 0 AF = AF_INET6;
150 //case MST_TD2_RIB_IPV4_MULTICAST:
151 0 case MST_TD2_RIB_IPV4_UNICAST:
152 0 ▶ if (AF == 0) AF = AF_INET; // Address Family also set for IPV6 messages
Branch: 1 branch
BranchTrueFalseTotal
if (AF == 0) AF = AF_INET; // Address Family also set for IPV6 messages 0 0 0
153  
154 // Decode Sequence
155 0 mrt_copy_next(&pPos, &iSeq, 4, &iRemainingLen);
156 0 iSeq = ntohl(iSeq);
157 0 hv_stores(rt, "sequence", newSVuv(iSeq));
158  
159 // Decode Prefix Bits
160 uint8_t iPrefixBits;
161 0 mrt_copy_next(&pPos, &iPrefixBits, 1, &iRemainingLen);
162 0 hv_stores(rt, "bits", newSVuv(iPrefixBits));
163  
164 // Decode Prefix
165 0 memset(&sa6, 0, sizeof(sa6));
166 0 ▶ if (iPrefixBits > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (iPrefixBits > 0) 0 0 0
167 0 mrt_copy_next(&pPos, &sa6, (int)ceil((double)iPrefixBits/8), &iRemainingLen);
168 0 inet_ntop(AF, &sa6, cIpAddress, INET6_ADDRSTRLEN);
169 0 hv_stores(rt, "prefix", newSVpv(cIpAddress, 0));
170  
171 // Decode count of entries
172 uint16_t iEntries;
173 0 mrt_copy_next(&pPos, &iEntries, 2, &iRemainingLen);
174 0 iEntries = ntohs(iEntries);
175 # ifdef _DEBUG_
176 printf("mrt_decode_single(): Decoded prefix %s/%d\n", cIpAddress, iPrefixBits);
177 printf("mrt_decode_single(): Decode have %d entries\n", iEntries);
178 # endif
179  
180 // Prepare entres
181 0 AV* avEntries = newAV();
182 0 hv_stores(rt, "entries", newRV_noinc((SV *)avEntries));
183 // Loop each entry
184  
185 0 ▶ while (iEntries > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (iEntries > 0) 0 0 0
186 {
187 0 iEntries--;
188 0 AV* avNextHop = NULL; // NEXT_HOP container
189 0 AV* avAsPath = NULL; // AS_PATH container
190  
191 # ifdef _DEBUG_
192 printf("mrt_decode_single(): %d entries remaining\n", iEntries);
193 # endif
194 // Prepare Entry HashRef
195  
196 0 HV* hvEntry = newHV();
197 0 av_push(avEntries, newRV_noinc((SV *)hvEntry));
198  
199 // Decode one entry
200 uint16_t iPeer;
201 0 mrt_copy_next(&pPos, &iPeer, 2, &iRemainingLen);
202 0 iPeer = ntohs(iPeer);
203 0 hv_stores(hvEntry, "peer_index", newSVuv(iPeer));
204  
205 // Decode Originated Time
206 int32_t orig_time;
207 0 mrt_copy_next(&pPos, &orig_time, 4, &iRemainingLen);
208 0 orig_time = ntohl(orig_time);
209 0 hv_stores(hvEntry, "originated_time", newSViv(orig_time));
210  
211 // Store length of BGP attributes
212 uint16_t iBgpAttributesLen;
213 int iBgpAttributesRemainLen;
214 0 mrt_copy_next(&pPos, &iBgpAttributesLen, 2, &iRemainingLen);
215 0 iBgpAttributesRemainLen = iBgpAttributesLen = ntohs(iBgpAttributesLen);
216  
217 // Store pointer to BGP attributes
218 0 char* pBgpAttributes = pPos;
219 0 ▶ if (iRemainingLen < iBgpAttributesLen)
Branch: 1 branch
BranchTrueFalseTotal
if (iRemainingLen < iBgpAttributesLen) 0 0 0
220 0 croak("Attempt to read %d bytes while buffer contain only %d", iBgpAttributesLen, iRemainingLen);
221 0 pPos = pPos + iBgpAttributesLen; // Skip pPos to next entry
222 0 iRemainingLen -= iBgpAttributesLen; // Decrease main remaining length
223  
224 // Scan each BGP attribute
225 0 ▶ while (iBgpAttributesRemainLen > 0) // pPos points to next entry
Branch: 1 branch
BranchTrueFalseTotal
while (iBgpAttributesRemainLen > 0) // pPos points to next entry 0 0 0
226 {
227 // Parse each attribute
228 uint8_t attribute_flags;
229 0 mrt_copy_next(&pBgpAttributes, &attribute_flags, 1, &iBgpAttributesRemainLen);
230 uint8_t attribute_code;
231 0 mrt_copy_next(&pBgpAttributes, &attribute_code, 1, &iBgpAttributesRemainLen);
232 // Check for Extended Length and read length
233 0 uint16_t iAttributeLen = 0;
234 int iAttributeRemainLen;
235 0 ▶ if (attribute_flags & 0x10) {
Branch: 1 branch
BranchTrueFalseTotal
if (attribute_flags & 0x10) { 0 0 0
236 0 mrt_copy_next(&pBgpAttributes, &iAttributeLen, 2, &iBgpAttributesRemainLen);
237 0 iAttributeLen = ntohs(iAttributeLen);
238 } else {
239 uint8_t att_len_8;
240 0 mrt_copy_next(&pBgpAttributes, &att_len_8, 1, &iBgpAttributesRemainLen);
241 0 iAttributeLen = att_len_8;
242 }
243 0 iAttributeRemainLen = iAttributeLen;
244 # ifdef _DEBUG_
245 printf("mrt_decode_single(): Decoding attribute code %d (len %d)\n", attribute_code, iAttributeLen);
246 # endif
247  
248 // Decode attributes
249 0 switch (attribute_code)
250 {
251 // 1 ORIGIN [RFC4271]
252 0 case 1:
253 0 mrt_copy_next(&pBgpAttributes, &iTmpU8, 1, &iBgpAttributesRemainLen);
254 0 hv_stores(hvEntry, "ORIGIN", newSVuv(iTmpU8));
255 0 break;
256 // 2 AS_PATH [RFC4271]
257 0 case 2:
258 // Check for absent NEXT_HOP array and create it
259 0 ▶ if (avAsPath == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (avAsPath == NULL) 0 0 0
260 {
261 0 avAsPath = newAV();
262 0 hv_stores(hvEntry, "AS_PATH", newRV_noinc((SV *)avAsPath));
263 }
264 0 ▶ while (iAttributeRemainLen > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (iAttributeRemainLen > 0) 0 0 0
265 {
266 // Read next AS_PATH subtype
267 0 iAttributeRemainLen -= 2;
268 uint8_t iPathType;
269 uint8_t iPathCount;
270 0 mrt_copy_next(&pBgpAttributes, &iPathType, 1, &iBgpAttributesRemainLen);
271 0 mrt_copy_next(&pBgpAttributes, &iPathCount, 1, &iBgpAttributesRemainLen);
272  
273 uint32_t iAsPathEntry;
274 // Decode AS_SET & AS_SEQUENCE
275 0 AV* avTmpAv2 = NULL;
276 0 ▶ if (iPathType == 1) // Compose subarray in case of AS_SET
Branch: 1 branch
BranchTrueFalseTotal
if (iPathType == 1) // Compose subarray in case of AS_SET 0 0 0
277 {
278 0 avTmpAv2 = newAV();
279 0 av_push(avAsPath, newRV_noinc((SV *)avTmpAv2));
280 }
281 0 ▶ while (iPathCount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (iPathCount > 0) { 0 0 0
282 0 iPathCount--;
283 0 iAttributeRemainLen -= 4; // NOTE: RIPE RIS hold 4-byte ASn in AS_PATH
284 0 mrt_copy_next(&pBgpAttributes, &iAsPathEntry, 4, &iBgpAttributesRemainLen);
285 0 iAsPathEntry = ntohl(iAsPathEntry);
286 0 ▶ av_push(((iPathType == 1)? avTmpAv2 : avAsPath), newSVuv(iAsPathEntry));
Branch: 1 branch
BranchTrueFalseTotal
av_push(((iPathType == 1)? avTmpAv2 : avAsPath), newSVuv(iAsPathEntry)); 0 0 0
287 }
288 } // end while (iAttributeRemainLen > 0)
289 0 break; // 2 AS_PATH [RFC4271]
290 // 3 NEXT_HOP [RFC4271]
291 0 case 3:
292 // Check for absent NEXT_HOP array and create it
293 0 ▶ if (avNextHop == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (avNextHop == NULL) 0 0 0
294 {
295 0 avNextHop = newAV();
296 0 hv_stores(hvEntry, "NEXT_HOP", newRV_noinc((SV *)avNextHop));
297 }
298  
299 0 mrt_copy_next(&pBgpAttributes, &iTmpU32, 4, &iBgpAttributesRemainLen);
300 0 inet_ntop(AF_INET, &iTmpU32, cIpAddress, INET6_ADDRSTRLEN);
301 0 av_push(avNextHop, newSVpv(cIpAddress, 0));
302 0 break;// 3 NEXT_HOP [RFC4271]
303 // 4 MULTI_EXIT_DISC [RFC4271]
304 0 case 4:
305 0 mrt_copy_next(&pBgpAttributes, &iTmpU32, 4, &iBgpAttributesRemainLen);
306 0 iTmpU32 = ntohl(iTmpU32);
307 0 hv_stores(hvEntry, "MULTI_EXIT_DISC", newSVuv(iTmpU32));
308 0 break;// 4 MULTI_EXIT_DISC [RFC4271]
309 // 5 LOCAL_PREF [RFC4271]
310 0 case 5:
311 0 mrt_copy_next(&pBgpAttributes, &iTmpU32, 4, &iBgpAttributesRemainLen);
312 0 iTmpU32 = ntohl(iTmpU32);
313 0 hv_stores(hvEntry, "LOCAL_PREF", newSVuv(iTmpU32));
314 0 break;// 5 LOCAL_PREF [RFC4271]
315 // 6 ATOMIC_AGGREGATE [RFC4271]
316 0 case 6:
317 0 hv_stores(hvEntry, "ATOMIC_AGGREGATE", &PL_sv_undef);
318 0 break;// 6 ATOMIC_AGGREGATE [RFC4271]
319 // 7 AGGREGATOR [RFC4271]
320 0 case 7:
321 0 mrt_copy_next(&pBgpAttributes, &iTmpU32, 4, &iBgpAttributesRemainLen);
322 0 iTmpU32 = ntohl(iTmpU32);
323 0 hv_stores(hvEntry, "AGGREGATOR_AS", newSVuv(iTmpU32));
324 0 mrt_copy_next(&pBgpAttributes, &iTmpU32, 4, &iBgpAttributesRemainLen);
325 0 inet_ntop(AF_INET, &iTmpU32, cIpAddress, INET6_ADDRSTRLEN);
326 0 hv_stores(hvEntry, "AGGREGATOR_BGPID", newSVpv(cIpAddress, 0));
327 0 break;// 7 AGGREGATOR [RFC4271]
328 // 8 COMMUNITY [RFC1997]
329 0 case 8:
330 0 avTmpAv = (AV *)sv_2mortal((SV *)newAV());
331 0 hv_stores(hvEntry, "COMMUNITY", newRV_inc((SV *)avTmpAv));
332  
333 0 ▶ while (iAttributeRemainLen > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (iAttributeRemainLen > 0) 0 0 0
334 {
335 // Read and decode community
336 0 iAttributeRemainLen -= 4;
337 uint16_t c1, c2;
338 0 mrt_copy_next(&pBgpAttributes, &c1, 2, &iBgpAttributesRemainLen);
339 0 c1 = ntohs(c1);
340 0 mrt_copy_next(&pBgpAttributes, &c2, 2, &iBgpAttributesRemainLen);
341 0 c2 = ntohs(c2);
342 0 snprintf(sbuff, SBUFF, "%d:%d", c1, c2);
343 0 av_push(avTmpAv, newSVpv(sbuff, 0));
344 } // end while (iAttributeRemainLen > 0)
345 0 break;
346 // 14 MP_REACH_NLRI http://tools.ietf.org/html/rfc6396#section-4.3.4
347 0 case 14:
348 // Check for absent NEXT_HOP array and create it
349 0 ▶ if (avNextHop == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (avNextHop == NULL) 0 0 0
350 {
351 0 avNextHop = newAV();
352 0 hv_stores(hvEntry, "NEXT_HOP", newRV_noinc((SV *)avNextHop));
353 }
354 0 ▶ if (SvOK(USE_RFC4760) && SvIV(USE_RFC4760) == 1)
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(USE_RFC4760) && SvIV(USE_RFC4760) == 1) 0 0 0
if (SvOK(USE_RFC4760) && SvIV(USE_RFC4760) == 1) 0 0 0
355 0 {
356 // read AFI
357 uint16_t AFI;
358 0 mrt_copy_next(&pBgpAttributes, &iTmpU16, 2, &iBgpAttributesRemainLen);
359 0 AFI = ntohs(iTmpU16);
360 // read/skip SAFI
361 0 mrt_copy_next(&pBgpAttributes, &iTmpU8, 1, &iBgpAttributesRemainLen);
362 // read LEN
363 0 mrt_copy_next(&pBgpAttributes, &iTmpU8, 1, &iBgpAttributesRemainLen);
364 0 iAttributeRemainLen -= 4;
365 // validate LEN & decode IP
366 0 bool bSkip = false;
367 0 ▶ if (AFI == 1 && iTmpU8 == 4)
Branch: 2 branches
BranchTrueFalseTotal
if (AFI == 1 && iTmpU8 == 4) 0 0 0
if (AFI == 1 && iTmpU8 == 4) 0 0 0
368 {
369 // Read and decode IPv4
370 0 memset(&sa6, 0, sizeof(sa6));
371 0 mrt_copy_next(&pBgpAttributes, &sa6, iTmpU8, &iBgpAttributesRemainLen);
372 0 iAttributeRemainLen -= iTmpU8;
373 0 inet_ntop(AF_INET, &sa6, cIpAddress, INET6_ADDRSTRLEN);
374 0 av_push(avNextHop, newSVpv(cIpAddress, 0));
375 0 ▶ } else if (AFI == 2 && (iTmpU8 == 16 || iTmpU8 == 32)) // AFI=IPv6
Branch: 3 branches
BranchTrueFalseTotal
} else if (AFI == 2 && (iTmpU8 == 16 || iTmpU8 == 32)) // AFI=IPv6 0 0 0
} else if (AFI == 2 && (iTmpU8 == 16 || iTmpU8 == 32)) // AFI=IPv6 0 0 0
} else if (AFI == 2 && (iTmpU8 == 16 || iTmpU8 == 32)) // AFI=IPv6 0 0 0
376 {
377 // Read and decode IPv6
378 0 memset(&sa6, 0, sizeof(sa6));
379 0 mrt_copy_next(&pBgpAttributes, &sa6, 16, &iBgpAttributesRemainLen);
380 0 iAttributeRemainLen -= 16;
381 0 inet_ntop(AF_INET6, &sa6, cIpAddress, INET6_ADDRSTRLEN);
382 0 av_push(avNextHop, newSVpv(cIpAddress, 0));
383 0 ▶ if (iTmpU8 == 32) // Case for combined Global and Link Local IPv6
Branch: 1 branch
BranchTrueFalseTotal
if (iTmpU8 == 32) // Case for combined Global and Link Local IPv6 0 0 0
384 {
385 0 memset(&sa6, 0, sizeof(sa6));
386 0 mrt_copy_next(&pBgpAttributes, &sa6, 16, &iBgpAttributesRemainLen);
387 0 iAttributeRemainLen -= 16;
388 0 inet_ntop(AF_INET6, &sa6, cIpAddress, INET6_ADDRSTRLEN);
389 0 av_push(avNextHop, newSVpv(cIpAddress, 0));
390 }
391 } else {
392 0 snprintf(sbuff, SBUFF, "Unsupported/invalid AFI %d len %d", AFI, iTmpU8);
393 0 hv_stores(hvEntry, "MP_REACH_NLRI", newSVpv(sbuff, 0));
394 // Skip to end of attribute
395 0 bSkip = true;
396 }
397 0 ▶ if (!bSkip) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bSkip) { 0 0 0
398 // Process MP_REACH_NLRI
399 0 mrt_copy_next(&pBgpAttributes, &iTmpU8, 1, &iBgpAttributesRemainLen); // Skip reserved byte
400 0 iAttributeRemainLen--;
401  
402 0 avTmpAv = (AV *)sv_2mortal((SV *)newAV());
403 0 hv_stores(hvEntry, "MP_REACH_NLRI", newRV_inc((SV *)avTmpAv));
404  
405 0 ▶ while (iAttributeRemainLen > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (iAttributeRemainLen > 0) 0 0 0
406 {
407 // Read and decode NLRI
408 // Decode Prefix Bits
409 uint8_t iBits;
410 0 mrt_copy_next(&pBgpAttributes, &iBits, 1, &iBgpAttributesRemainLen);
411 0 iAttributeRemainLen--;
412 0 hv_stores(rt, "bits", newSVuv(iPrefixBits));
413  
414 // Decode Prefix
415 0 memset(&sa6, 0, sizeof(sa6));
416 0 ▶ if (iPrefixBits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (iPrefixBits > 0) { 0 0 0
417 0 mrt_copy_next(&pBgpAttributes, &sa6, (int)ceil((double)iBits/8), &iBgpAttributesRemainLen);
418 0 iAttributeRemainLen -= (int)ceil((double)iBits/8);
419 }
420 0 ▶ inet_ntop((AFI == 1 ? AF_INET : AF_INET6), &sa6, cIpAddress, INET6_ADDRSTRLEN);
Branch: 1 branch
BranchTrueFalseTotal
inet_ntop((AFI == 1 ? AF_INET : AF_INET6), &sa6, cIpAddress, INET6_ADDRSTRLEN); 0 0 0
421  
422 0 snprintf(sbuff, SBUFF, "%s/%d", cIpAddress, iBits);
423 0 av_push(avTmpAv, newSVpv(sbuff, 0));
424 } // end while (iAttributeRemainLen > 0)
425 }
426 0 pBgpAttributes += iAttributeRemainLen;
427 0 iBgpAttributesRemainLen -= iAttributeRemainLen;
428 0 ▶ } else if (SvOK(USE_RFC4760) && SvIV(USE_RFC4760) == -1) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvOK(USE_RFC4760) && SvIV(USE_RFC4760) == -1) { 0 0 0
} else if (SvOK(USE_RFC4760) && SvIV(USE_RFC4760) == -1) { 0 0 0
429 0 pBgpAttributes += iAttributeLen;
430 0 iBgpAttributesRemainLen -= iAttributeLen;
431 } else {
432 // Use RFC 6396
433 0 ▶ iTmpU8 = (AF == AF_INET ? 4 : 16); // Store size of IP address for AF
Branch: 1 branch
BranchTrueFalseTotal
iTmpU8 = (AF == AF_INET ? 4 : 16); // Store size of IP address for AF 0 0 0
434 // Read size of MRT MP_REACH_NLRI
435 0 mrt_copy_next(&pBgpAttributes, &iAttributeRemainLen, 1, &iBgpAttributesRemainLen);
436 # ifdef _DEBUG_
437 printf("mrt_decode_single(): Attribute 14 have %u bytes for AF. Remain %d bytes\n", iTmpU8, iAttributeRemainLen);
438 # endif
439 0 ▶ while (iAttributeRemainLen > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (iAttributeRemainLen > 0) 0 0 0
440 {
441 0 iAttributeRemainLen -= iTmpU8;
442 0 memset(&sa6, 0, sizeof(sa6));
443 0 mrt_copy_next(&pBgpAttributes, &sa6, iTmpU8, &iBgpAttributesRemainLen);
444 0 inet_ntop(AF, &sa6, cIpAddress, INET6_ADDRSTRLEN);
445 0 av_push(avNextHop, newSVpv(cIpAddress, 0));
446 }
447 } // end else if RFC4760
448 0 break;// 14 MP_REACH_NLRI [RFC4760]
449 0 default:
450 0 hv_store(hvEntry, sbuff, strlen(sbuff), &PL_sv_undef, 0);
451 0 pBgpAttributes += iAttributeLen;
452 0 iBgpAttributesRemainLen -= iAttributeLen;
453 } // switch (attribute_code)
454 } // iBgpAttributesRemainLen > 0
455 } // while (iEntries > 0)
456  
457 0 break; // subtype = IPv4/6 UNICAST/MULTICAST
458 0 default:
459 0 snprintf(sbuff, SBUFF, "Unsupported MRT type %d subtype %d in message at %lli", mh->type, mh->subtype, (intmax_t)msgpos);
460 0 hv_stores(rt, "error", newSVpv(sbuff, 0));
461 } // switch subtype
462 0 break; // MT_TABLE_DUMP_V2
463 0 default:
464 0 snprintf(sbuff, SBUFF, "Unsupported MRT type %d in message at %lli", mh->type, (intmax_t)msgpos);
465 0 hv_stores(rt, "error", newSVpv(sbuff, 0));
466 } // switch message type
467 0 return;
468 }
469  
470 MODULE = Net::MRT PACKAGE = Net::MRT
471  
472 void
473 mrt_read_next(f)
474 PerlIO * f;
475 PROTOTYPE: *
476 PPCODE:
477 0 # Definitions
478 Off_t msgpos = PerlIO_tell(f); // Store message position & check for proper handle
479 int sz;
480 0 MRT_MESSAGE mh;
481 char sbuff[SBUFF] = {};
482 HV* rt;
483 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
484 0 if (msgpos == -1)
485 0 croak("Invalid filehandle passed to mrt_read_next");
486 0 ▶ sz = PerlIO_read(f, &mh, 12);
Branch: 1 branch
BranchTrueFalseTotal
sz = PerlIO_read(f, &mh, 12); 0 0 0
487 if (sz == 0)
488 0 {
489 0 # No data to read
490 ST(0) = &PL_sv_undef;
491 0 XSRETURN(1);
492 0 } else {
493 0 # Network to host for MH
494 0 mh.timestamp = ntohl(mh.timestamp);
495 mh.type = ntohs(mh.type);
496 0 mh.subtype = ntohs(mh.subtype);
497 0 mh.length = ntohl(mh.length);
498 0  
499 0 # Create resulting HASHREF
500 rt = newHV();
501 0 ▶ hv_stores(rt, "timestamp", newSVuv(mh.timestamp));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(rt, "timestamp", newSVuv(mh.timestamp)); 0 0 0
502 hv_stores(rt, "type", newSVuv(mh.type));
503 0 hv_stores(rt, "subtype", newSVuv(mh.subtype));
504 0  
505 0 ▶ # Decode header
Branch: 1 branch
BranchTrueFalseTotal
# Decode header 0 0 0
506 0 # Check for length to be less than buffer
507 0 if (mh.length > BUFFER_SIZE)
508 0 {
509 0 snprintf(sbuff, SBUFF, "Message length too big at %lli", (intmax_t)msgpos);
510 hv_stores(rt, "error", newSVpv(sbuff, 0));
511 while (mh.length > 0) {
512 0 ▶ auto remainder = (BUFFER_SIZE < mh.length ? BUFFER_SIZE : mh.length);
Branch: 1 branch
BranchTrueFalseTotal
auto remainder = (BUFFER_SIZE < mh.length ? BUFFER_SIZE : mh.length); 0 0 0
513 0 printf("remaining: %d to do: %d %d\n", mh.length, remainder, BUFFER_SIZE); fflush(stdout);
514 0 ▶ sz = PerlIO_read(f, &mh.message, remainder);
Branch: 2 branches
BranchTrueFalseTotal
sz = PerlIO_read(f, &mh.message, remainder); 0 0 0
sz = PerlIO_read(f, &mh.message, remainder); 0 0 0
515 0 mh.length -= remainder;
516 }
517 0 } else {
518 # Try to read message
519 0 if (mh.length > 0)
520 0 sz = PerlIO_read(f, &mh.message, mh.length);
521 if ((mh.length > 0) && (sz != mh.length))
522 croak("Unable to read %d bytes in message at pos %lli", mh.length, (intmax_t)msgpos);
523  
524 # Try to decode
525 mrt_decode(rt, msgpos, &mh);
526 }
527 ST(0) = sv_2mortal((SV*)newRV_noinc((SV*)rt));
528 XSRETURN(1);
529 }
530  
531 void
532 mrt_get_next(f)
533 PerlIO * f;
534 PROTOTYPE: *
535 PPCODE:
536 0 # Definitions
537 Off_t msgpos = PerlIO_tell(f); // Store message position & check for proper handle
538 int sz;
539 0 MRT_MESSAGE mh;
540 char sbuff[SBUFF] = {};
541 HV* rt;
542 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
543 0 if (msgpos == -1)
544 0 croak("Invalid filehandle passed to mrt_read_next");
545 0 ▶ sz = PerlIO_read(f, &mh, 12);
Branch: 1 branch
BranchTrueFalseTotal
sz = PerlIO_read(f, &mh, 12); 0 0 0
546 if (sz == 0)
547 0 {
548 0 # No data to read
549 ST(0) = &PL_sv_undef;
550 0 XSRETURN(1);
551 0 } else {
552 0 # Network to host for MH
553 0 mh.timestamp = ntohl(mh.timestamp);
554 mh.type = ntohs(mh.type);
555 0 mh.subtype = ntohs(mh.subtype);
556 0 mh.length = ntohl(mh.length);
557 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
558 # Decode header
559 0 # Check for length to be less than buffer
560 0 ▶ ST(0) = newSVuv(mh.type);
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = newSVuv(mh.type); 0 0 0
561 0 ST(1) = newSVuv(mh.subtype);
562 0 if (mh.length > BUFFER_SIZE)
563 0 {
564 0 snprintf(sbuff, SBUFF, "Message length too big at %lli", (intmax_t)msgpos);
565 while (mh.length > 0) {
566 0 auto remainder = (BUFFER_SIZE < mh.length ? BUFFER_SIZE : mh.length);
567 0 printf("remaining: %d to do: %d %d\n", mh.length, remainder, BUFFER_SIZE); fflush(stdout);
568 sz = PerlIO_read(f, &mh.message, remainder);
569 0 ▶ mh.length -= remainder;
Branch: 1 branch
BranchTrueFalseTotal
mh.length -= remainder; 0 0 0
570 0 }
571 0 ▶ ST(2) = newSVuv(-1);
Branch: 2 branches
BranchTrueFalseTotal
ST(2) = newSVuv(-1); 0 0 0
ST(2) = newSVuv(-1); 0 0 0
572 0 ST(3) = newSVpv(sbuff, strlen(sbuff));
573 } else {
574 0 # Try to read message
575 0 if (mh.length > 0)
576 sz = PerlIO_read(f, &mh.message, mh.length);
577 0 if ((mh.length > 0) && (sz != mh.length))
578 croak("Unable to read %d bytes in message at pos %lli", mh.length, (intmax_t)msgpos);
579  
580 ST(2) = newSVuv(mh.length);
581 ST(3) = newSVpv(mh.message, mh.length);
582 }
583 XSRETURN(4);
584 }
585  
586 HV*
587 mrt_decode_single(type, subtype, message)
588 uint16_t type;
589 uint16_t subtype;
590 SV* message;
591 CODE:
592 MRT_MESSAGE mh;
593 char* msg;
594  
595 // Prepare returning variable(s)
596 0 RETVAL = newHV();
597  
598 // Prepare intermediate variables
599 0 mh.timestamp = 0;
600 0 mh.type = type;
601 0 mh.subtype = subtype;
602 0 msg = (char*)SvPV(message, mh.length);
603 0 memcpy(&mh.message, msg, mh.length);
604  
605 // Perform checks
606 0 ▶ if (mh.length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (mh.length == 0) 0 0 0
607 0 croak("I don't know how to decode a message without contents");
608 0 ▶ if (mh.length > BUFFER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (mh.length > BUFFER_SIZE) 0 0 0
609 0 croak("Unable to process message larger than %d bytes", BUFFER_SIZE);
610  
611 0 mrt_decode(RETVAL, 0, &mh);
612 0 sv_2mortal((SV*)newRV_noinc((SV*)RETVAL));
613 OUTPUT:
614 RETVAL
615  
616 # void mrt_decode(HV* const rt, Off_t const msgpos, MRT_MESSAGE* const mh)
617  
618 BOOT:
619 0 USE_RFC4760 = get_sv("Net::MRT::USE_RFC4760", GV_ADDMULTI);
620 //sv_setiv(USE_RFC4760, 1); // Change default behavior
↑ Summary blib/lib/Net/MRT.pm »