1 ------------------------------------------------------------------------
3 --- @brief User datagram protocol (UDP) utility.
4 --- Utility functions for udp_header struct
5 --- defined in \ref headers.lua . \
n
8 --- - Udp header utility
9 --- - Definition of Udp packets
10 ------------------------------------------------------------------------
13 local
pkt = require "packet"
18 local ntoh, hton = ntoh, hton
19 local ntoh16, hton16 = ntoh16, hton16
20 local
bor,
band,
bnot, rshift, lshift= bit.bor, bit.band, bit.bnot, bit.rshift, bit.lshift
21 local istype =
ffi.istype
22 local format =
string.format
25 ---------------------------------------------------------------------------
27 ---------------------------------------------------------------------------
29 --- Udp protocol constants
32 -- TODO move well-known ports somewhere
else, those are not only Udp
34 --- Well known port
for Ptp
event message
35 udp.PORT_PTP_EVENTS = 319
36 --- Well known port
for Ptp general message
37 udp.PORT_PTP_GENERAL_MESSAGES = 320
40 ---------------------------------------------------------------------------
42 ---------------------------------------------------------------------------
44 --- Module
for udp_header
struct (see \ref headers.lua).
48 --- Set the source port.
49 --- @param
int Source port of the
udp header as 16 bit integer.
52 self.src = hton16(
int)
55 --- Retrieve the UDP source port.
56 --- @return Port as 16 bit integer.
58 return hton16(self.src)
61 --- Retrieve the UDP source port.
62 --- @return Port as
string.
67 --- Set the destination port.
68 --- @param
int Destination port of the
udp header as 16 bit integer.
71 self.dst = hton16(
int)
74 --- Retrieve the UDP destination port.
75 --- @return Port as 16 bit integer.
77 return hton16(self.dst)
80 --- Retrieve the UDP destination port.
81 --- @return Port as
string.
87 --- @param
int Length of the
udp header plus payload (excluding l2 and l3). 16 bit integer.
89 int =
int or 28 -- with ethernet + IPv4 header -> 64B
90 self.len = hton16(
int)
93 --- Retrieve the length.
94 --- @return Length as 16 bit integer.
96 return hton16(self.len)
99 --- Retrieve the length.
100 --- @return Length as
string.
106 --- @param
int Checksum of the
udp header as 16 bit integer.
109 self.cs = hton16(
int)
118 --- @return Checksum as 16 bit integer.
120 return hton16(self.cs)
124 --- @return Checksum as
string.
129 --- Set all members of the
udp header.
130 --- Per default, all members are
set to default values specified in the respective
set function.
131 --- Optional named arguments can be used to
set a member to a user-provided value.
132 --- @param args Table of named arguments. Available arguments: udpSrc, udpDst, udpLength, udpChecksum
133 --- @param pre prefix for namedArgs. Default '
udp'.
135 ---
fill() --- only default values
136 ---
fill{ udpSrc=44566, ip6Length=101 } --- all members are
set to
default values with the exception of udpSrc
148 --- Retrieve the values of all members.
149 --- @param pre prefix for namedArgs. Default '
udp'.
150 --- @return Table of named arguments. For a list of arguments see "See also".
164 --- Retrieve the values of all members.
165 --- @return Values in
string format.
171 -- Maps headers to respective (well knwon) port.
172 -- This list should be extended whenever a
new protocol is added to 'UDP constants'.
173 local mapNamePort = {
174 ptp = {
udp.PORT_PTP_EVENTS,
udp.PORT_PTP_GENERAL_MESSAGES },
177 --- Resolve which header comes after
this one (in a packet).
178 --- For instance: in tcp/
udp based on the ports.
179 --- This
function must exist and is only used when
get/
dump is executed on
180 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
181 --- @
return String next header (e.g.
'udp',
'icmp', nil)
184 for name, _port in pairs(mapNamePort) do
185 if type(_port) == "table" then
186 for _, p in pairs(_port) do
191 elseif port == _port then
198 --- Change the default values for namedArguments (for
fill/
get).
199 --- This can be used to for instance calculate a length value based on the total packet length.
201 --- This function must exist and is only used by packet.
fill.
202 --- @param pre The prefix used for the namedArgs, e.g. '
udp'
203 --- @param namedArgs Table of named arguments (see See Also)
204 --- @param nextHeader The header following after this header in a packet
205 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
206 --- @return Table of namedArgs
210 if not namedArgs[pre .. "Length"] and namedArgs["pktLength"] then
211 namedArgs[pre .. "Length"] = namedArgs["pktLength"] - accumulatedLength
215 if not namedArgs[pre .. "Dst"] then
216 for name, _port in pairs(mapNamePort) do
217 if nextHeader == name then
218 namedArgs[pre .. "Dst"] = type(_port) == "table" and _port[1] or _port
226 ----------------------------------------------------------------------------------
228 ----------------------------------------------------------------------------------
230 --- Cast the packet to an Udp (IP4) packet
232 --- Cast the packet to an Udp (IP6) packet
234 --- Cast the packet to an Udp packet, either using IP4 (nil/true) or IP6 (false), depending on the passed
boolean.
236 ip4 = ip4 == nil or ip4
245 ------------------------------------------------------------------------
247 ------------------------------------------------------------------------
function udpHeader getDstPort()
Retrieve the UDP destination port.
pkt getUdpPacket
Cast the packet to an Udp packet, either using IP4 (nil/true) or IP6 (false), depending on the passed...
local ffi
low-level dpdk wrapper
function udpHeader setChecksum(int)
Set the checksum.
pkt getUdp6Packet
Cast the packet to an Udp (IP6) packet.
function checksum(data, len)
Calculate a 16 bit checksum.
function udpHeader getChecksumString()
Retrieve the checksum.
function udpHeader fill(args, pre)
Set all members of the udp header.
function packetCreate(...)
Create struct and functions for a new packet.
function udpHeader getString()
Retrieve the values of all members.
function mod band(mask1, mask2, result)
Bitwise and.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
function udpHeader getLengthString()
Retrieve the length.
function ipsecICV set(icv)
Set the IPsec ICV.
function udpHeader getChecksum()
Retrieve the checksum.
function udpHeader getDstPortString()
Retrieve the UDP destination port.
local udp
Udp protocol constants.
function mod bor(mask1, mask2, result)
Bitwise or.
function udpHeader getLength()
Retrieve the length.
local udpHeader
Module for udp_header struct (see headers.lua).
function udpHeader setSrcPort(int)
Set the source port.
function udpHeader setLength(int)
Set the length.
local ptp
Ptp protocol constants.
function udpHeader setDstPort(int)
Set the destination port.
function udpHeader getSrcPortString()
Retrieve the UDP source port.
local eth
Ethernet protocol constants.
local ip6
IP6 protocol constants.
function udpHeader resolveNextHeader()
Resolve which header comes after this one (in a packet).
pkt getUdp4Packet
Cast the packet to an Udp (IP4) packet.
function udpHeader calculateChecksum(len)
Calculate the checksum.
local pkt
Module for packets (rte_mbuf)
function mod bnot(mask, result)
Bitwise not.
n
Create a new array of memory buffers (initialized to nil).
local ip4Header
Module for ip4_header struct (see headers.lua).
function udpHeader getSrcPort()
Retrieve the UDP source port.
function udpHeader get(pre)
Retrieve the values of all members.
function udpHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get).