File tree Expand file tree Collapse file tree 3 files changed +49
-11
lines changed
Expand file tree Collapse file tree 3 files changed +49
-11
lines changed Original file line number Diff line number Diff line change 44--
55
66-- Daemon to send LLDP frames on given interface
7- local socket = require (" socket" )
7+ local raw = require (" socket.raw " )
88local linux = require (" linux" )
99local thread = require (" thread" )
1010
@@ -36,8 +36,7 @@ local config = {
3636local ethertype = string.pack (" >H" , ETH_P_LLDP )
3737
3838local 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 )
4342end
7776local ifindex = linux .ifindex (config .interface )
7877
7978local src_mac = get_src_mac (ifindex )
79+ print (string.format (" lldpd got src mac = %s" , src_mac ))
8080local lldp_frame = build_lldp_frame (src_mac )
8181
8282local 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 )
Original file line number Diff line number Diff line change 44--
55
66local device = require (" device" )
7- local socket = require (" socket" )
7+ local raw = require (" socket.raw " )
88local linux = require (" linux" )
99
10- local PACKET = socket .af .PACKET
11- local RAW = socket .sock .RAW
12- local ETH_P_ALL = 0x0003
1310local MTU = 1500
1411
1512local s = linux .stat
1613local 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
2117function tap :read ()
2218 local frame = socket :receive (MTU )
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments