Skip to content

Commit 4c7cc53

Browse files
sneaky-potatolneto
authored andcommitted
Fix bind signature in raw.lua pattern
Separate call signatures for raw socket bind call - rx socket (protocol is required) - tx socket (interface index is required) Signed-off-by: Ashwani Kumar Kamal <[email protected]>
1 parent 72cc7be commit 4c7cc53

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

config.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ file = {
3737
'./lib/luarcu.c',
3838
'./lib/luasocket.c',
3939
'./lib/socket/inet.lua',
40+
'./lib/socket/raw.lua',
4041
'./lib/socket/unix.lua',
4142
'./lib/luasyscall.c',
4243
'./lib/syscall/table.lua',

examples/lldpd.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- SPDX-FileCopyrightText: (c) 2025 Ashwani Kumar Kamal <[email protected]>
2+
-- SPDX-FileCopyrightText: (c) 2025-2026 Ashwani Kumar Kamal <[email protected]>
33
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
44
--
55

@@ -33,10 +33,10 @@ local config = {
3333
},
3434
}
3535

36-
local ethertype = string.pack(">H", ETH_P_LLDP)
36+
local ethertype = string.pack(">I2", ETH_P_LLDP)
3737

38-
local function get_src_mac(ifindex)
39-
local rx <close> = raw.bind(ETH_P_ALL, ifindex)
38+
local function get_src_mac()
39+
local rx <close> = raw.bind(ETH_P_ALL)
4040
local frame = rx:receive(2048)
4141
return frame:sub(7, 12)
4242
end
@@ -45,13 +45,13 @@ local function tlv(t, payload, subtype)
4545
if subtype then
4646
payload = string.char(subtype) .. payload
4747
end
48-
return string.pack(">H", (t << 9) | #payload) .. payload
48+
return string.pack(">I2", (t << 9) | #payload) .. payload
4949
end
5050

5151
local function build_lldp_frame(chassis_id)
5252
local port_id = config.interface
53-
local ttl = string.pack(">H", config.system.ttl)
54-
local capabilities = string.pack(">HH", config.system.capabilities, config.system.capabilities_enabled)
53+
local ttl = string.pack(">I2", config.system.ttl)
54+
local capabilities = string.pack(">I2I2", config.system.capabilities, config.system.capabilities_enabled)
5555

5656
local pdu = {
5757
-- Ethernet header
@@ -75,7 +75,7 @@ end
7575

7676
local ifindex = linux.ifindex(config.interface)
7777

78-
local src_mac = get_src_mac(ifindex)
78+
local src_mac = get_src_mac()
7979
local lldp_frame = build_lldp_frame(src_mac)
8080

8181
local function worker()

lib/luasocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static int luasocket_receive(lua_State *L)
232232
* if not binding to a specific protocol via other means (e.g. `socket.new`'s protocol argument for `AF_PACKET` might set this).
233233
* - If `addr` is a string: It's a packed string directly representing parts of the `sockaddr_ll` structure
234234
* (specifically, the fields after `sll_family`, like `sll_protocol`).
235-
* Example from `tap.lua` for binding to `ETH_P_ALL` (0x0003): `sock:bind(string.pack(">H", 0x0003))`.
235+
* Example from `tap.lua` for binding to `ETH_P_ALL` (0x0003): `sock:bind(string.pack(">I2", 0x0003))`.
236236
* - Other families: A packed string representing the family-specific address structure.
237237
* @tparam[opt] integer port The local port number (required and used only if the family is `AF_INET`).
238238
* @treturn nil

lib/socket/raw.lua

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- SPDX-FileCopyrightText: (c) 2025 Ashwani Kumar Kamal <[email protected]>
2+
-- SPDX-FileCopyrightText: (c) 2026 Ashwani Kumar Kamal <[email protected]>
33
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
44
--
55

@@ -20,18 +20,21 @@ local ETH_P_ALL = 0x0003
2020
local raw = {}
2121

2222
---
23-
-- Creates and binds a raw packet socket for receiving and sending frames.
23+
-- Creates and binds a raw packet socket for receiving frames.
2424
-- @param proto (number) EtherType (defaults to ETH_P_ALL).
25-
-- @param ifindex (number) Interface index.
26-
-- @return A new raw packet socket bound for RX.
25+
-- @param ifindex (number) [optional] Interface index (defaults to listen all interfaces).
26+
-- @return A new raw packet socket bound for proto and ifindex.
2727
-- @raise Error if socket.new() or socket.bind() fail.
28+
-- @usage
29+
-- local rx <close> = raw.bind(0x0003)
30+
-- local tx <close> = raw.bind(0x88cc, ifindex)
2831
-- @see socket.new
2932
-- @see socket.bind
30-
function raw.new(proto, ifindex)
33+
function raw.bind(proto, ifindex)
3134
local proto = proto or ETH_P_ALL
32-
local ifindex = ifindex or 0
3335
local s = socket.new(af.PACKET, sock.RAW, proto)
34-
s:bind(string.pack(">H", proto), ifindex)
36+
local param = ifindex and ifindex or string.pack(">I2", proto)
37+
s:bind(param)
3538
return s
3639
end
3740

0 commit comments

Comments
 (0)