diff --git a/libremap-agent/files/etc/uci-defaults/80_libremap-agent b/libremap-agent/files/etc/uci-defaults/80_libremap-agent index 10efa85..950ad83 100755 --- a/libremap-agent/files/etc/uci-defaults/80_libremap-agent +++ b/libremap-agent/files/etc/uci-defaults/80_libremap-agent @@ -4,7 +4,6 @@ create_conf() { cat > /etc/config/libremap < - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -]]-- - -local libremap = require 'luci.libremap' -local util = require 'luci.libremap.util' -local utl = require 'luci.util' -local json = require 'luci.json' - -sys = require 'luci.sys' - --- insert babel info into doc -function insert(doc) - -- init fields in doc (if not yet present) - doc.aliases = doc.aliases or {} - doc.links = doc.links or {} - - --todo: make that cleaner - --todo: get /var/log/babeld.log from babel configuration - --todo: need better aliases: local is the mac and remote the neigbour's id in babel... that won't do to map links (or add the aliases like they do in olsr?) - local output = utl.exec('LINESBEFORE=$(wc -l /var/log/babeld.log | cut -d\' \' -f1); /etc/init.d/babeld status; LINESAFTER=$(wc -l /var/log/babeld.log | cut -d\' \' -f1);tail -`expr $LINESAFTER - $LINESBEFORE - 1` /var/log/babeld.log') - local links = utl.exec('echo \''..output..'\' | grep Neighbour | cut -d\' \' -f2') - local myid = utl.exec('echo \''..output..'\' | grep \'My id\' | cut -d\' \' -f3') - local myid2 = string.match(myid,"[%w+%p*]+") - local jsonstring = json.encode(links) - for l in string.gmatch(links,"[%w+%p*]+") do - doc.links[#doc.links+1] = { - type = 'babel', - alias_remote = l, - alias_local = myid2 - } - end -end - -return { - insert = insert -} diff --git a/libremap-agent/luasrc/libremap/plugins/babeld.lua b/libremap-agent/luasrc/libremap/plugins/babeld.lua new file mode 100644 index 0000000..401e8fb --- /dev/null +++ b/libremap-agent/luasrc/libremap/plugins/babeld.lua @@ -0,0 +1,123 @@ +--! LibreMap Babeld Plugin +--! Copyright (C) 2018 Gioacchino Mazzurco +--! +--! This program is free software: you can redistribute it and/or modify +--! it under the terms of the GNU Affero General Public License as +--! published by the Free Software Foundation, either version 3 of the +--! License, or (at your option) any later version. +--! +--! This program is distributed in the hope that it will be useful, +--! but WITHOUT ANY WARRANTY; without even the implied warranty of +--! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +--! GNU Affero General Public License for more details. +--! +--! You should have received a copy of the GNU Affero General Public License +--! along with this program. If not, see . + + +local libuci = require("uci") +local nixio = require("nixio") + +local function split(string, sep) + local ret = {} + for token in string.gmatch(string, "[^"..sep.."]+") do + table.insert(ret, token) + end + return ret +end + +local function insert(doc) + doc.aliases = doc.aliases or {} + doc.links = doc.links or {} + doc.attributes = doc.attributes or {} + + function mLog(mType, mMessage) + nixio.syslog(mType, mMessage) + doc.debug = doc.debug or {} + table.insert(doc.debug, mType.." "..mMessage) + end + + local bblMonitorPort = -1 + function savePort(s) + if type(s.local_port) ~= "nil" then + bblMonitorPort = tonumber(s.local_port) + end + end + local uci = libuci:cursor() + uci:foreach("babeld", "general", savePort) + uci = nil + + if not (bblMonitorPort >= 0 and bblMonitorPort <= 65535) then + mLog("err", "Could not determine Babeld monitoring local-port") + return + end + + local ncStdOut = io.popen("echo dump | nc ::1 " .. bblMonitorPort) + + local exptectedBblCtrlProto = "BABEL 1.0" + local bblCtrlProto = ncStdOut:read() + if bblCtrlProto ~= exptectedBblCtrlProto then + mLog( "err", + "Babeld control protocol mismatch expected: " .. + exptectedBblCtrlProto .. " got: " .. bblCtrlProto ) + return + end + + local bblVersion = ncStdOut:read() + if not bblVersion then + mLog("err", "could not determine babeld version") + return + end + doc.attributes.babel_version = split(bblVersion, " ")[2] + + ncStdOut:read() -- discard hostname line + + local mId = split(ncStdOut:read(), " ") + if mId[1] ~= "my-id" or not mId[2] then + mLog("err", "Cannot parse Babeld router id") + return + end + table.insert( doc.aliases, {type="babeld", alias=mId[2]} ) + + if(ncStdOut:read() ~= "ok") then + mLog("err", "Cannot parse Babeld 'ok' line") + end + + local ifaceIp6Map = {} + local mLine = ncStdOut:read() + while mLine do + local mTable = split(mLine, " ") + + if mTable[1] == "add" then + if mTable[2] == "interface" and mTable[5] == "true" then + table.insert( doc.aliases, { type="babel", alias=mTable[7] } ) + table.insert( doc.aliases, { type="babel", alias=mTable[9] } ) + ifaceIp6Map[mTable[3]] = mTable[7] + else + if mTable[2] == "neighbour" then + local mLink = { + type = "babel", + alias_local = ifaceIp6Map[mTable[7]], + alias_remote = mTable[5], + quality = 255/mTable[17], + attributes = { + l2_interface = mTable[7], + reach = mTable[9], + ureach = mTable[11], + rxcost = mTable[13], + txcost = mTable[15], + cost = mTable[17] + } + } + table.insert(doc.links, mLink) + end + end + else + if mTable[1] == "ok" then return end + end + + mLine = ncStdOut:read() + end +end + +return { insert = insert }