Skip to content

Commit

Permalink
apps.ipfix: use lib.poptrie
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneia committed Apr 25, 2022
1 parent c23857c commit 1e94811
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/apps/ipfix/maps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ local ffi = require("ffi")
local lib = require("core.lib")
local ctable = require("lib.ctable")
local ethernet = require("lib.protocol.ethernet")
local lpm = require("lib.lpm.lpm4_248").LPM4_248
local ipv4 = require("lib.protocol.ipv4")
local poptrie = require("lib.poptrie")
local logger = require("lib.logger")

-- Map MAC addresses to peer AS number
Expand Down Expand Up @@ -73,14 +74,15 @@ end
-- used by the Geo2Lite database provided by MaxMind:
-- http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip
local function make_pfx_to_as_map(name)
local table = lpm:new({ keybits = 31 })
-- Assign AS 0 to addresses not covered by the map
table:add_string("0.0.0.0/0", 0)
local table = poptrie.new{direct_pointing=true}
for line in assert(io.lines(name)) do
if not line:match("^network") then
local pfx, asn = line:match("([^,]*),(%d+),")
assert(pfx and asn, "Prefix-to-AS map: invalid line: "..line)
table:add_string(pfx, tonumber(asn))
local cidr, asn = line:match("([^,]*),(%d+),")
asn = tonumber(asn)
assert(cidr and asn and asn == bit.band(asn, 0xffff),
"Prefix-to-AS map: invalid line: "..line)
local pfx, len = ipv4:pton_cidr(cidr)
table:add(pfx, len, asn)
end
end
table:build()
Expand Down
4 changes: 2 additions & 2 deletions src/apps/ipfix/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -569,14 +569,14 @@ local function v4_extended_extract (self, pkt, timestamp, entry)
extended_extract(self, pkt, md, timestamp, entry, extract_v4_addr)

local pfx_to_as = self.maps.pfx_to_as
local asn = pfx_to_as.map:search_bytes(entry.key.sourceIPv4Address)
local asn = pfx_to_as.map:lookup32(entry.key.sourceIPv4Address)
if asn ~= 0 then
entry.value.bgpSourceAsNumber = asn
elseif can_log(pfx_to_as.logger) then
pfx_to_as.logger:log("missing AS for source "
..ipv4:ntop(entry.key.sourceIPv4Address))
end
local asn = pfx_to_as.map:search_bytes(entry.key.destinationIPv4Address)
local asn = pfx_to_as.map:lookup32(entry.key.destinationIPv4Address)
if asn ~= 0 then
entry.value.bgpDestinationAsNumber = asn
elseif can_log(pfx_to_as.logger) then
Expand Down

0 comments on commit 1e94811

Please sign in to comment.