Detect IPv4/IPv6 prefixes conform to BGP Additional Path but NOT conform to standard BGP.. This is an adapted version of wireshark's detect_add_path_prefix46 https://github.com/wireshark/wireshark/blob/ed9e958a2ed506220fdab320738f1f96a3c2ffbb/epan/dissectors/packet-bgp.c#L2905
(s, max_bit_length)
| 187 | |
| 188 | |
| 189 | def detect_add_path_prefix46(s, max_bit_length): |
| 190 | """ |
| 191 | Detect IPv4/IPv6 prefixes conform to BGP Additional Path but NOT conform |
| 192 | to standard BGP.. |
| 193 | |
| 194 | This is an adapted version of wireshark's detect_add_path_prefix46 |
| 195 | https://github.com/wireshark/wireshark/blob/ed9e958a2ed506220fdab320738f1f96a3c2ffbb/epan/dissectors/packet-bgp.c#L2905 |
| 196 | Kudos to them ! |
| 197 | """ |
| 198 | # Must be compatible with BGP Additional Path |
| 199 | i = 0 |
| 200 | while i + 4 < len(s): |
| 201 | i += 4 |
| 202 | prefix_len = orb(s[i]) |
| 203 | if prefix_len > max_bit_length: |
| 204 | return False |
| 205 | addr_len = (prefix_len + 7) // 8 |
| 206 | i += 1 + addr_len |
| 207 | if i > len(s): |
| 208 | return False |
| 209 | if prefix_len % 8: |
| 210 | if orb(s[i - 1]) & (0xFF >> (prefix_len % 8)): |
| 211 | return False |
| 212 | # Must NOT be compatible with standard BGP |
| 213 | i = 0 |
| 214 | while i + 4 < len(s): |
| 215 | prefix_len = orb(s[i]) |
| 216 | if prefix_len == 0 and len(s) > 1: |
| 217 | return True |
| 218 | if prefix_len > max_bit_length: |
| 219 | return True |
| 220 | addr_len = (prefix_len + 7) // 8 |
| 221 | i += 1 + addr_len |
| 222 | if i > len(s): |
| 223 | return True |
| 224 | if prefix_len % 8: |
| 225 | if orb(s[i - 1]) & (0xFF >> (prefix_len % 8)): |
| 226 | return True |
| 227 | return False |
| 228 | |
| 229 | |
| 230 | class BGPNLRI_IPv4(Packet): |