Fix bind signature in raw.lua pattern#364
Conversation
|
If we want to do this in a single new method, we can pass whatever arguments we get to the -- Arguments are forwarded verbatim to socket:bind().
--
-- Examples:
-- raw.new(string.pack(">H", ETH_P_ALL), ifindex) RX-style bind
-- raw.new(ifindex) TX-style bind
-- raw.new() unbound socket
--
function raw.new(...)
local s = socket.new(af.PACKET, sock.RAW)
local argc = select("#", ...)
if argc > 0 then
s:bind(...)
end
return s
end
return raw |
lib/socket/raw.lua
Outdated
| s:bind(string.pack(">H", proto), ifindex) | ||
| return s | ||
| local s = socket.new(af.PACKET, sock.RAW, proto) | ||
| s:bind(string.pack(">H", proto), ifindex) |
There was a problem hiding this comment.
Wouldn't this work for sending as well? Are you sure?
There was a problem hiding this comment.
I tried to use this form to send frames over raw socket
local function worker()
local tx <close> = socket.new(socket.af.PACKET, socket.sock.RAW, ETH_P_LLDP)
tx:bind(string.pack(">H", ETH_P_LLDP), ifindex)
while (not shouldstop()) do
tx:send(lldp_frame)
print(string.format("[lldpd] frame sent on ifindex=%d (%d bytes)", ifindex, #lldp_frame))
linux.schedule(config.tx_interval_ms)
end
print("[lldpd] worker stopped")
end
return workergot this error
[ 495.463722] luathread: [000000004887f053] ENXIO
The socket starts working when called with bind(ifindex)
There was a problem hiding this comment.
I believe this branch executes when bind is called with string parameter.
So it blindly copies 2 bytes (ETH_P_LLDP) into sockaddr_storage.__data
Everything else is uninitialized garbage. (This explains ENXIO no such device error)
Summary
bind(ifindex)binds on a specific interface (this cannot be used to capture traffic because protocol is never set in this case)bind(string.pack(">H", proto))binds on all interfaces (0) (should be used when receiving frames)- anything else does not make sense
There was a problem hiding this comment.
We were using ">I2" instead ">H" on tap.. why did you change it?
There was a problem hiding this comment.
I don't think it's correct as short might vary depending on arch.. I would fix both the code and the doc
lib/socket/raw.lua
Outdated
| -- @see socket.new | ||
| -- @see socket.bind | ||
| function raw.tx(proto, ifindex) | ||
| local s = socket.new(af.PACKET, sock.RAW, proto) |
There was a problem hiding this comment.
I would create a "new" function just to avoid repeat this line.. not fond of tx/Rx but couldn't figure better names.. it's up to you.. I can merge it
There was a problem hiding this comment.
What about bind and connect as the names for the methods?
There was a problem hiding this comment.
I think it's better from the API client's perspective.. though it sounds a bit misleading to bind() on connect(), I will leave this decision to you ;-)
There was a problem hiding this comment.
I think a single bind call will suffice, we can call bind(ifindex) if ifindex is given else the usual listening socket bind(string.pack(">I2", proto))
lneto
left a comment
There was a problem hiding this comment.
Ah.. you need to add this to config.ld right?
66c7e1e to
9bf99e5
Compare
|
@lneto from the For AF_PACKET
For a listening raw socket bound to an interface, we would need something equivalent to: sll_protocol = htons(ETH_P_ALL);
sll_ifindex = ifindex;Currently there’s no way to express this combination from Lua. Is this by design? If not, would it make sense to open an issue to track proper AF_PACKET bind support for listening sockets? |
lneto
left a comment
There was a problem hiding this comment.
it's looking good to me, just a small comment.. btw, can you take the opportunity and bump the copyright year of those files as well? thanks
9bf99e5 to
106d8dd
Compare
examples/lldpd.lua
Outdated
| @@ -1,5 +1,5 @@ | |||
| -- | |||
| -- SPDX-FileCopyrightText: (c) 2025 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> | |||
| -- SPDX-FileCopyrightText: (c) 2026 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> | |||
There was a problem hiding this comment.
| -- SPDX-FileCopyrightText: (c) 2026 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> | |
| -- SPDX-FileCopyrightText: (c) 2025-2026 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com> |
right?
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 <ashwanikamal.im421@gmail.com>
106d8dd to
f4a6a25
Compare
We need separate call signatures for
Related PR
#360