Skip to content

Conversation

@sneaky-potato
Copy link
Contributor

Move recurring raw socket receive and send pattern in a common place.

Fixes #358

@lneto lneto requested review from carloslack and jperon December 31, 2025 16:56
Copy link
Contributor

@lneto lneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use the very same approach we use on unix.lua; we can leverage the new() function we have there to better modularize our code here..

could you also apply this new lib on the examples that should use it on this PR?

Copy link
Contributor

@lneto lneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, please use --fixup on your commits or squash it.. let's keep our history as clean as possible

@sneaky-potato sneaky-potato force-pushed the raw-socket-pattern branch 3 times, most recently from 5befca5 to d32f704 Compare January 3, 2026 10:49
Comment on lines 22 to 40
local function new(method)
raw[method] = function (proto, ifindex)
local proto = proto or ETH_P_ALL
local ifindex = ifindex or 0
local s = socket.new(af.PACKET, sock.RAW, proto)
s:bind(string.pack(">H", proto), ifindex)
return s
end
end

---
-- Creates and binds a raw packet socket for receiving and sending frames.
-- @param proto (number) EtherType (defaults to ETH_P_ALL).
-- @param ifindex (number) Interface index.
-- @return A new raw packet socket bound for RX.
-- @raise Error if socket.new() or socket.bind() fail.
-- @see socket.new
-- @see socket.bind
new("bind")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local function new(method)
raw[method] = function (proto, ifindex)
local proto = proto or ETH_P_ALL
local ifindex = ifindex or 0
local s = socket.new(af.PACKET, sock.RAW, proto)
s:bind(string.pack(">H", proto), ifindex)
return s
end
end
---
-- Creates and binds a raw packet socket for receiving and sending frames.
-- @param proto (number) EtherType (defaults to ETH_P_ALL).
-- @param ifindex (number) Interface index.
-- @return A new raw packet socket bound for RX.
-- @raise Error if socket.new() or socket.bind() fail.
-- @see socket.new
-- @see socket.bind
new("bind")
---
-- Creates and binds a raw packet socket for receiving and sending frames.
-- @param proto (number) EtherType (defaults to ETH_P_ALL).
-- @param ifindex (number) Interface index.
-- @return A new raw packet socket bound for RX.
-- @raise Error if socket.new() or socket.bind() fail.
-- @see socket.new
-- @see socket.bind
function bind.new(proto, ifindex)
local proto = proto or ETH_P_ALL
local ifindex = ifindex or 0
local s = socket.new(af.PACKET, sock.RAW, proto)
s:bind(string.pack(">H", proto), ifindex)
return s
end

if we will have only one method for creating a new raw socket, I'd keep that simpler and avoid the factory..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in latest push

Move recurring raw socket recieve and send
pattern in a common place.

Signed-off-by: Ashwani Kumar Kamal <[email protected]>
@lneto lneto merged commit 72cc7be into luainkernel:master Jan 3, 2026
1 check passed
@sneaky-potato
Copy link
Contributor Author

Hi @lneto
Sorry I missed the calling signature, examples lldpd.lua and tap.lua do not work because of that

Also I believe we need to call bind() differently when using both proto, ifindex vs only ifindex. I will create a quick pull request with working examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Factor common AF_PACKET RAW socket pattern

2 participants