Skip to content

Commit

Permalink
Merge pull request #1055 from javierbrk/batmaninfo
Browse files Browse the repository at this point in the history
links information according to batman protocol
  • Loading branch information
G10h4ck authored Mar 12, 2024
2 parents 7b7fcd7 + 30298c7 commit 069ac1a
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/shared-state-bat_links_info/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright (C) 2023 Javier Jorge <[email protected]>
# Copyright (c) 2023 Instituto Nacional de Tecnología Industrial
# Copyright (C) 2023 Asociación Civil Altermundi <[email protected]>
# This is free software, licensed under the GNU Affero General Public License v3.
#

include ../../libremesh.mk

define Package/$(PKG_NAME)
SECTION:=lime
CATEGORY:=LibreMesh
TITLE:=Batman protocol links information module for shared-state
MAINTAINER:= Javier <[email protected]>
DEPENDS:=+lua +luci-lib-jsonc +ubus-lime-utils \
+lime-system +batctl-default shared-state-async
PKGARCH:=all
endef

define Package/$(PKG_NAME)/description
Syncronize batman protocol links information beween nodes.
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

unique_append()
{
grep -qF "$1" "$2" || echo "$1" >> "$2"
}

uci set shared-state.bat_links_info=dataType
uci set shared-state.bat_links_info.name='bat_links_info'
uci set shared-state.bat_links_info.scope='community'
uci set shared-state.bat_links_info.ttl='2400'
uci set shared-state.bat_links_info.update_interval='30'
uci commit shared-state

unique_append \
'*/30 * * * * ((sleep $(($RANDOM % 120)); shared-state-publish_bat_links_info &> /dev/null)&)' \
/etc/crontabs/root
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/lua

--! LibreMesh
--! Copyright (C) 2023 Javier Jorge <[email protected]>
--! Copyright (C) 2023 Asociación Civil Altermundi <[email protected]>
--!
--! 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 <http://www.gnu.org/licenses/>.

local JSON = require("luci.jsonc")
local utils = require('lime.utils')
local network = require ("lime.network")

local hostname = utils.hostname()

function get_bat_links_info()
local bat_neighbors_obj={}
local bat_originators_obj={}
local bat_originators = utils.unsafe_shell("batctl oj")
bat_originators_obj = JSON.parse(bat_originators)


local bat_neighbors = utils.unsafe_shell("batctl nj")
bat_neighbors = string.gsub(bat_neighbors,"neigh_address","dst_mac")
bat_neighbors = string.gsub(bat_neighbors,"hard_ifname","iface")
bat_neighbors_obj = JSON.parse(bat_neighbors)

for key,neight_value in pairs (bat_neighbors_obj) do
local macparts = network.get_mac(neight_value.iface)
local src_macaddr = table.concat(macparts,":")
neight_value.hard_ifindex=nil
neight_value.src_mac=src_macaddr
for key,originator_value in pairs (bat_originators_obj) do
if originator_value.hard_ifname == neight_value.iface and
originator_value.neigh_address== originator_value.orig_address and
originator_value.neigh_address== neight_value.dst_mac then
-- Batman "transmit link quality" (tq) is a byte that describes
-- the probability of a successful transmission towards a
-- neighbor node
neight_value.tq = originator_value.tq
end
end
end
return bat_neighbors_obj
end

local result = { [hostname] = get_bat_links_info() }
io.popen("shared-state-async insert bat_links_info", "w"):write(JSON.stringify(result))
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
local utils = require "lime.utils"
local network = require ("lime.network")
local iwinfo = require "iwinfo"



describe('Tests bat_links_info #bat_links_info', function ()

local oj_output = [[
[
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:95:39:46:28:95",
"last_seen_msecs": 1820,
"neigh_address": "02:ab:46:da:4e:aa",
"tq": 225
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:95:39:46:28:95",
"last_seen_msecs": 1820,
"neigh_address": "02:58:47:da:4e:aa",
"tq": 0
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:95:39:46:28:95",
"best": true,
"last_seen_msecs": 1820,
"neigh_address": "02:ab:46:46:28:95",
"tq": 255
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:95:39:46:28:95",
"last_seen_msecs": 1820,
"neigh_address": "02:58:47:46:28:95",
"tq": 253
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:58:47:da:4e:aa",
"last_seen_msecs": 0,
"neigh_address": "02:58:47:46:28:95",
"tq": 193
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:58:47:da:4e:aa",
"best": true,
"last_seen_msecs": 0,
"neigh_address": "02:58:47:da:4e:aa",
"tq": 111
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:95:39:da:4e:aa",
"last_seen_msecs": 10,
"neigh_address": "02:ab:46:46:28:95",
"tq": 221
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:95:39:da:4e:aa",
"last_seen_msecs": 10,
"neigh_address": "02:58:47:46:28:95",
"tq": 0
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:95:39:da:4e:aa",
"best": true,
"last_seen_msecs": 10,
"neigh_address": "02:ab:46:da:4e:aa",
"tq": 255
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:95:39:da:4e:aa",
"last_seen_msecs": 10,
"neigh_address": "02:58:47:da:4e:aa",
"tq": 254
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:58:47:46:28:95",
"last_seen_msecs": 140,
"neigh_address": "02:58:47:da:4e:aa",
"tq": 198
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"orig_address": "02:58:47:46:28:95",
"best": true,
"last_seen_msecs": 140,
"neigh_address": "02:58:47:46:28:95",
"tq": 222
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:ab:46:46:28:95",
"last_seen_msecs": 1420,
"neigh_address": "02:ab:46:da:4e:aa",
"tq": 198
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:ab:46:46:28:95",
"best": true,
"last_seen_msecs": 1420,
"neigh_address": "02:ab:46:46:28:95",
"tq": 444
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:ab:46:da:4e:aa",
"last_seen_msecs": 1680,
"neigh_address": "02:ab:46:46:28:95",
"tq": 195
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"orig_address": "02:ab:46:da:4e:aa",
"best": true,
"last_seen_msecs": 1680,
"neigh_address": "02:ab:46:da:4e:aa",
"tq": 333
}
]
]]
local nj_output = [[
[
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"last_seen_msecs": 1040,
"neigh_address": "02:58:47:da:4e:aa"
},
{
"hard_ifindex": 26,
"hard_ifname": "wlan0-mesh_250",
"last_seen_msecs": 1250,
"neigh_address": "02:58:47:46:28:95"
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"last_seen_msecs": 640,
"neigh_address": "02:ab:46:da:4e:aa"
},
{
"hard_ifindex": 28,
"hard_ifname": "wlan1-mesh_250",
"last_seen_msecs": 450,
"neigh_address": "02:ab:46:46:28:95"
}
]
]]

stub(utils, "unsafe_shell", function (cmd)
if cmd == "batctl nj" then
return nj_output
elseif cmd == "batctl oj" then
return oj_output
end
return ""
end)

stub(network, "get_mac", function (iface)
if string.match(iface, "wlan0") then
return iwinfo.mocks.wlan0_mesh_mac
end
return iwinfo.mocks.wlan1_mesh_mac
end)

package.path = package.path .. ";packages/shared-state-bat_links_info/files/usr/bin/?;;"
require ("shared-state-publish_bat_links_info")

it('a simple test to get node info and assert requiered fields are present', function()
local links_info = {}
links_info = get_bat_links_info()
assert.are.equal(table.concat(iwinfo.mocks.wlan0_mesh_mac,":"), links_info[1].src_mac)
assert.are.equal(table.concat(iwinfo.mocks.wlan1_mesh_mac,":"), links_info[4].src_mac)
assert.are.equal('02:58:47:da:4e:aa', links_info[1].dst_mac)
assert.are.equal(1040, links_info[1].last_seen_msecs)
assert.are.equal("wlan0-mesh_250", links_info[1].iface)
assert.are.equal(111, links_info[1].tq)
assert.are.equal(222, links_info[2].tq)
assert.are.equal(333, links_info[3].tq)
assert.are.equal(444, links_info[4].tq)
end)
end)

0 comments on commit 069ac1a

Please sign in to comment.