Skip to content

Commit 5befca5

Browse files
committed
Factor common RAW AF_PACKET socket pattern
Move recurring raw socket recieve and send pattern in a common place. Signed-off-by: Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com>
1 parent 73d7175 commit 5befca5

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

examples/lldpd.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
--
55

66
-- Daemon to send LLDP frames on given interface
7-
local socket = require("socket")
7+
local raw = require("socket.raw")
88
local linux = require("linux")
99
local thread = require("thread")
1010

@@ -36,8 +36,7 @@ local config = {
3636
local ethertype = string.pack(">H", ETH_P_LLDP)
3737

3838
local function get_src_mac(ifindex)
39-
local rx <close> = socket.new(socket.af.PACKET, socket.sock.RAW, ETH_P_ALL)
40-
rx:bind(string.pack(">H", ETH_P_ALL), ifindex)
39+
local rx <close> = raw.bind(ETH_P_ALL, ifindex)
4140
local frame = rx:receive(2048)
4241
return frame:sub(7, 12)
4342
end
@@ -77,11 +76,11 @@ end
7776
local ifindex = linux.ifindex(config.interface)
7877

7978
local src_mac = get_src_mac(ifindex)
79+
print(string.format("lldpd got src mac = %s", src_mac))
8080
local lldp_frame = build_lldp_frame(src_mac)
8181

8282
local function worker()
83-
local tx <close> = socket.new(socket.af.PACKET, socket.sock.RAW, ETH_P_LLDP)
84-
tx:bind(ifindex)
83+
local tx <close> = raw.bind(ETH_P_LLDP, ifindex)
8584

8685
while (not shouldstop()) do
8786
tx:send(lldp_frame)

examples/tap.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@
44
--
55

66
local device = require("device")
7-
local socket = require("socket")
7+
local raw = require("socket.raw")
88
local linux = require("linux")
99

10-
local PACKET = socket.af.PACKET
11-
local RAW = socket.sock.RAW
12-
local ETH_P_ALL = 0x0003
1310
local MTU = 1500
1411

1512
local s = linux.stat
1613
local tap = {name = "tap", mode = s.IRUGO}
1714

18-
local socket = socket.new(PACKET, RAW, ETH_P_ALL)
19-
socket:bind(string.pack(">I2", ETH_P_ALL))
15+
local socket = raw.bind()
2016

2117
function tap:read()
2218
local frame = socket:receive(MTU)

lib/socket/raw.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--
2+
-- SPDX-FileCopyrightText: (c) 2025 Ashwani Kumar Kamal <ashwanikamal.im421@gmail.com>
3+
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
4+
--
5+
6+
---
7+
-- RAW AF_PACKET socket operations.
8+
-- This module provides a higher-level abstraction over the `socket` module.
9+
--
10+
-- @module socket.raw
11+
-- @see socket
12+
--
13+
local socket = require("socket")
14+
15+
local af = socket.af
16+
local sock = socket.sock
17+
18+
local ETH_P_ALL = 0x0003
19+
20+
local raw = {}
21+
22+
local function new(method)
23+
raw[method] = function (proto, ifindex)
24+
local proto = proto or ETH_P_ALL
25+
local ifindex = ifindex or 0
26+
local s = socket.new(af.PACKET, sock.RAW, proto)
27+
s:bind(string.pack(">H", proto), ifindex)
28+
return s
29+
end
30+
end
31+
32+
---
33+
-- Creates and binds a raw packet socket for receiving and sending frames.
34+
-- @param proto (number) EtherType (defaults to ETH_P_ALL).
35+
-- @param ifindex (number) Interface index.
36+
-- @return A new raw packet socket bound for RX.
37+
-- @raise Error if socket.new() or socket.bind() fail.
38+
-- @see socket.new
39+
-- @see socket.bind
40+
new("bind")
41+
42+
return raw
43+

0 commit comments

Comments
 (0)