MoonGen
 All Files Functions Variables Pages
ip4.lua File Reference

Internet protocol (v4) utility. More...

Go to the source code of this file.

Functions

function ip4Addr get ()
 Retrieve the IPv4 address. More...
 
function ip4Addr set (ip)
 Set the IPv4 address. More...
 
function ip4Addr setString (ip)
 Set the IPv4 address. More...
 
function ip4Addr getString ()
 Retrieve the string representation of the IPv4 address. More...
 
function ip4Addr __eq (lhs, rhs)
 Test equality of two IPv4 addresses. More...
 
function ip4Addr __add (lhs, rhs)
 Add a number to an IPv4 address. More...
 
function ip4Addr add (val)
 Add a number to an IPv4 address in-place. More...
 
function ip4Addr __sub (val)
 Subtract a number from an IPv4 address. More...
 
function ip4Header setVersion (int)
 Set the version. More...
 
function ip4Header getVersion ()
 Retrieve the version. More...
 
function ip4Header getVersionString ()
 Retrieve the version. More...
 
function ip4Header setHeaderLength (int)
 Set the header length. More...
 
function ip4Header getHeaderLength ()
 Retrieve the header length. More...
 
function ip4Header getHeaderLengthString ()
 Retrieve the header length. More...
 
function ip4Header setTOS (int)
 Set the type of service (TOS). More...
 
function ip4Header getTOS ()
 Retrieve the type of service. More...
 
function ip4Header getTOSString ()
 Retrieve the type of service. More...
 
function ip4Header setLength (int)
 Set the total length. More...
 
function ip4Header getLength ()
 Retrieve the length. More...
 
function ip4Header getLengthString ()
 Retrieve the length. More...
 
function ip4Header setID (int)
 Set the identification. More...
 
function ip4Header getID ()
 Retrieve the identification. More...
 
function ip4Header getIDString ()
 Retrieve the identification. More...
 
function ip4Header setFlags (int)
 Set the flags. More...
 
function ip4Header getFlags ()
 Retrieve the flags. More...
 
function ip4Header getFlagsString ()
 Retrieve the flags. More...
 
function ip4Header setFragment (int)
 Set the fragment. More...
 
function ip4Header getFragment ()
 Retrieve the fragment. More...
 
function ip4Header getFragmentString ()
 Retrieve the fragemt. More...
 
function ip4Header setTTL (int)
 Set the time-to-live (TTL). More...
 
function ip4Header getTTL ()
 Retrieve the time-to-live. More...
 
function ip4Header getTTLString ()
 Retrieve the time-to-live. More...
 
function ip4Header setProtocol (int)
 Set the next layer protocol. More...
 
function ip4Header getProtocol ()
 Retrieve the next layer protocol. More...
 
function ip4Header getProtocolString ()
 Retrieve the next layer protocol. More...
 
function ip4Header setChecksum (int)
 Set the checksum. More...
 
function ip4Header getChecksum ()
 Retrieve the checksum. More...
 
function ip4Header getChecksumString ()
 Retrieve the checksum. More...
 
function ip4Header calculateChecksum ()
 Calculate and set the checksum. More...
 
function ip4Header setDst (int)
 Set the destination address. More...
 
function ip4Header getDst ()
 Retrieve the destination IP address. More...
 
function ip4Header setSrc (int)
 Set the source address. More...
 
function ip4Header getSrc ()
 Retrieve the source IP address. More...
 
function ip4Header setDstString (str)
 Set the destination address. More...
 
function ip4Header getDstString ()
 Retrieve the destination IP address. More...
 
function ip4Header setSrcString (str)
 Set the source address. More...
 
function ip4Header getSrcString ()
 Retrieve the source IP address. More...
 
function ip4Header fill (args, pre)
 Set all members of the ip header. More...
 
function ip4Header get (pre)
 Retrieve the values of all members. More...
 
function ip4Header resolveNextHeader ()
 Resolve which header comes after this one (in a packet). More...
 
function ip4Header setDefaultNamedArgs (pre, namedArgs, nextHeader, accumulatedLength)
 Change the default values for namedArguments (for fill/get). More...
 

Variables

local ip = {}
 IP4 protocol constants.
 
ip PROTO_ICMP = 0x01
 Protocol field value for Icmp.
 
ip PROTO_TCP = 0x06
 Protocol field value for Tcp.
 
ip PROTO_UDP = 0x11
 Protocol field value for Udp.
 
local ip4Addr = {}
 Module for ip4_address struct (see headers.lua).
 
local ip4Header = {}
 Module for ip4_header struct (see headers.lua).
 
pkt getIP4Packet = packetCreate("eth", "ip4")
 Cast the packet to an IP4 packet.
 
pkt getIPPacket = function(self, ip4)
 Cast the packet to either an IP4 (nil/true) or IP6 (false) packet, depending on the passed boolean.
 

Detailed Description

Internet protocol (v4) utility.

Utility functions for the ip4_address and ip4_header structs defined in headers.lua .
Includes:

  • IP4 constants
  • IP4 address utility
  • IP4 header utility
  • Definition of IP4 packets

Definition in file ip4.lua.

Function Documentation

function ip4Addr __add ( lhs  ,
rhs   
)

Add a number to an IPv4 address.

Max. 32 bit, commutative.

Parameters
lhsAddress in 'union ip4_address' format.
rhsNumber to add (32 bit integer).
Returns
Resulting address in uint32 format.
function ip4Addr __eq ( lhs  ,
rhs   
)

Test equality of two IPv4 addresses.

Parameters
lhsAddress in 'union ip4_address' format.
rhsAddress in 'union ip4_address' format.
Returns
true if equal, false otherwise.
function ip4Addr __sub ( val  )

Subtract a number from an IPv4 address.

Max. 32 bit.

Parameters
valNumber to substract (32 bit integer)
Returns
Resulting address in uint32 format.
function ip4Addr add ( val  )

Add a number to an IPv4 address in-place.

Max. 32 bit.

Parameters
valNumber to add (32 bit integer).
Examples:
l3-tcp-syn-flood.lua.
function ip4Header calculateChecksum ( )

Calculate and set the checksum.

If possible use checksum offloading instead.

See Also
pkt:offloadUdpChecksum
function ip4Header fill ( args  ,
pre   
)

Set all members of the ip header.

Per default, all members are set to default values specified in the respective set function. Optional named arguments can be used to set a member to a user-provided value.

Parameters
argsTable of named arguments. Available arguments: Version, HeaderLength, TOS, Length, ID, Flags, Fragment, TTL, Protocol, Checksum, Src, Dst
preprefix for namedArgs. Default 'ip4'.
fill() --- only default values
fill{ ipSrc="1.1.1.1", ipTTL=100 } --- all members are set to default values with the exception of ipSrc and ipTTL
function ip4Addr get ( )

Retrieve the IPv4 address.

Returns
Address in uint32 format.
function ip4Header get ( pre  )

Retrieve the values of all members.

Parameters
preprefix for namedArgs. Default 'ip4'.
Returns
Table of named arguments. For a list of arguments see "See also".
See Also
ip4Header:fill
function ip4Header getChecksum ( )

Retrieve the checksum.

Returns
Checksum as 16 bit integer.
function ip4Header getChecksumString ( )

Retrieve the checksum.

Returns
Checksum as string.
function ip4Header getDst ( )

Retrieve the destination IP address.

Returns
Address in 'union ip4_address' format.
function ip4Header getDstString ( )

Retrieve the destination IP address.

Returns
Address in string format.
function ip4Header getFlags ( )

Retrieve the flags.

Returns
Flags as 3 bit integer.
function ip4Header getFlagsString ( )

Retrieve the flags.

Returns
Flags as string.
function ip4Header getFragment ( )

Retrieve the fragment.

Returns
Fragment as 13 bit integer.
function ip4Header getFragmentString ( )

Retrieve the fragemt.

Returns
Fragment as string.
function ip4Header getHeaderLength ( )

Retrieve the header length.

Returns
Header length as 4 bit integer.
function ip4Header getHeaderLengthString ( )

Retrieve the header length.

Returns
Header length as string.
function ip4Header getID ( )

Retrieve the identification.

Returns
ID as 16 bit integer.
function ip4Header getIDString ( )

Retrieve the identification.

Returns
ID as string.
function ip4Header getLength ( )

Retrieve the length.

Returns
Length as 16 bit integer.
function ip4Header getLengthString ( )

Retrieve the length.

Returns
Length as string.
function ip4Header getProtocol ( )

Retrieve the next layer protocol.

Returns
Next layer protocol as 8 bit integer.
function ip4Header getProtocolString ( )

Retrieve the next layer protocol.

Returns
Next layer protocol as string.
function ip4Header getSrc ( )

Retrieve the source IP address.

Returns
Address in 'union ip4_address' format.
function ip4Header getSrcString ( )

Retrieve the source IP address.

Returns
Address in string format.
function ip4Addr getString ( )

Retrieve the string representation of the IPv4 address.

Retrieve the values of all members.

Returns
Address in string format.
Values in string format.
function ip4Header getTOS ( )

Retrieve the type of service.

Returns
TOS as 8 bit integer.
function ip4Header getTOSString ( )

Retrieve the type of service.

Returns
TOS as string.
function ip4Header getTTL ( )

Retrieve the time-to-live.

Returns
TTL as 8 bit integer.
function ip4Header getTTLString ( )

Retrieve the time-to-live.

Returns
TTL as string.
function ip4Header getVersion ( )

Retrieve the version.

Returns
Version as 4 bit integer.
function ip4Header getVersionString ( )

Retrieve the version.

Returns
Version as string.
function ip4Header resolveNextHeader ( )

Resolve which header comes after this one (in a packet).

For instance: in tcp/udp based on the ports. This function must exist and is only used when get/dump is executed on an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)

Returns
String next header (e.g. 'udp', 'icmp', nil)
function ip4Addr set ( ip  )

Set the IPv4 address.

Parameters
ipAddress in uint32 format.
function ip4Header setChecksum ( int  )

Set the checksum.

Parameters
intChecksum of the ip header as 16 bit integer.
See Also
ip4Header:calculateChecksum
pkt:offloadUdpChecksum
function ip4Header setDefaultNamedArgs ( pre  ,
namedArgs  ,
nextHeader  ,
accumulatedLength   
)

Change the default values for namedArguments (for fill/get).

This can be used to for instance calculate a length value based on the total packet length. See proto/ip4.setDefaultNamedArgs as an example. This function must exist and is only used by packet.fill.

Parameters
preThe prefix used for the namedArgs, e.g. 'ip4'
namedArgsTable of named arguments (see See Also)
nextHeaderThe header following after this header in a packet
accumulatedLengthThe so far accumulated length for previous headers in a packet
Returns
Table of namedArgs
See Also
ip4Header:fill
function ip4Header setDst ( int  )

Set the destination address.

Parameters
intAddress in 'union ip4_address' format.
function ip4Header setDstString ( str  )

Set the destination address.

Parameters
strAddress in string format.
function ip4Header setFlags ( int  )

Set the flags.

Bits: [ reserved (must be 0) | don't fragment | more fragments ]

Parameters
intFlags of the ip header as 3 bit integer
function ip4Header setFragment ( int  )

Set the fragment.

Parameters
intFragment of the ip header as 13 bit integer.
function ip4Header setHeaderLength ( int  )

Set the header length.

Parameters
intLength of the ip header (in multiple of 32 bits) as 4 bit integer. Should always be '5'.
function ip4Header setID ( int  )

Set the identification.

Parameters
intID of the ip header as 16 bit integer.
function ip4Header setLength ( int  )

Set the total length.

Parameters
intLength of the packet excluding layer 2. 16 bit integer.
function ip4Header setProtocol ( int  )

Set the next layer protocol.

Parameters
intNext layer protocol of the ip header as 8 bit integer.
function ip4Header setSrc ( int  )

Set the source address.

Parameters
intAddress in 'union ip4_address' format.
function ip4Header setSrcString ( str  )

Set the source address.

Parameters
strAddress in string format.
function ip4Addr setString ( ip  )

Set the IPv4 address.

Parameters
ipAddress in string format.
function ip4Header setTOS ( int  )

Set the type of service (TOS).

Parameters
intTOS of the ip header as 8 bit integer.
function ip4Header setTTL ( int  )

Set the time-to-live (TTL).

Parameters
intTTL of the ip header as 8 bit integer.
function ip4Header setVersion ( int  )

Set the version.

Parameters
intIP header version as 4 bit integer. Should always be '4'.