Rawsock library  v0.3.4
Library to make the use of raw sockets easier, under Linux. Currently supporting IPv4, UDP and a custom latency measurement protocol (LaMP - supported both in raw and non-raw sockets).
rawsock_lamp.h File Reference
#include "rawsock.h"
#include <linux/if_packet.h>
#include <sys/time.h>

Go to the source code of this file.

Data Structures

struct  lamphdr
 Main LaMP packet header structure. More...
 

Macros

#define PROTO_LAMP   0xAA
 
#define PROTO_LAMP_CTRL_MASK   0xA0
 
#define CTRL_PINGLIKE_REQ   0xA0
 
#define CTRL_PINGLIKE_REPLY   0xA1
 
#define CTRL_PINGLIKE_ENDREQ   0xA2
 
#define CTRL_PINGLIKE_ENDREPLY   0xA3
 
#define CTRL_UNIDIR_CONTINUE   0xA4
 
#define CTRL_UNIDIR_STOP   0xA5
 
#define CTRL_UNIDIR_REPORT   0xA6
 
#define CTRL_ACK   0xA7
 
#define CTRL_CONN_INIT   0xA8
 
#define CTRL_PINGLIKE_REQ_TLESS   0xA9
 
#define CTRL_PINGLIKE_REPLY_TLESS   0xAA
 
#define CTRL_PINGLIKE_ENDREQ_TLESS   0xAB
 
#define CTRL_PINGLIKE_ENDREPLY_TLESS   0xAC
 
#define CTRL_FOLLOWUP_CTRL   0xAD
 
#define CTRL_FOLLOWUP_DATA   0xAE
 
#define FOLLOWUP_REQUEST   0x0000
 
#define FOLLOWUP_DENY   0x0100
 
#define FOLLOWUP_ACCEPT   0x0200
 
#define FOLLOWUP_UNKNOWN   0xFFFF
 
#define FOLLOWUP_REQUEST_T_APP   0x0000
 
#define FOLLOWUP_REQUEST_T_KRN_RX   0x0001
 
#define FOLLOWUP_REQUEST_T_KRN   0x0002
 
#define FOLLOWUP_REQUEST_T_HW   0x0003
 
#define IS_FOLLOWUP_REQUEST(lenortype_field)   ((lenortype_field & 0xFF00)==0x0000)
 
#define IS_CTRL_PINGLIKE_REQ(ctrl)   (ctrl==CTRL_PINGLIKE_REQ || ctrl==CTRL_PINGLIKE_REQ_TLESS)
 
#define IS_CTRL_PINGLIKE_REPLY(ctrl)   (ctrl==CTRL_PINGLIKE_REPLY || ctrl==CTRL_PINGLIKE_REPLY_TLESS)
 
#define IS_CTRL_PINGLIKE_ENDREQ(ctrl)   (ctrl==CTRL_PINGLIKE_ENDREQ || ctrl==CTRL_PINGLIKE_ENDREQ_TLESS)
 
#define IS_CTRL_PINGLIKE_ENDREPLY(ctrl)   (ctrl==CTRL_PINGLIKE_ENDREPLY || ctrl==CTRL_PINGLIKE_ENDREPLY_TLESS)
 
#define INIT_PINGLIKE_INDEX   0x0001
 
#define INIT_UNIDIR_INDEX   0x0002
 
#define IS_INIT_INDEX_VALID(idx)   (idx == INIT_PINGLIKE_INDEX || idx == INIT_UNIDIR_INDEX)
 
#define IS_INIT(ctrl)   (ctrl == CTRL_CONN_INIT)
 
#define IS_FOLLOWUP_CTRL(ctrl)   (ctrl == CTRL_FOLLOWUP_CTRL)
 
#define IS_FOLLOWUP_CTRL_REQ_TYPE_VALID(idx)   (idx == FOLLOWUP_REQUEST_T_APP || idx == FOLLOWUP_REQUEST_T_KRN_RX || idx == FOLLOWUP_REQUEST_T_KRN || idx == FOLLOWUP_REQUEST_T_HW || (idx >= 0x00F0 && idx<= 0x00FF))
 
#define IS_FOLLOWUP_CTRL_TYPE_VALID(idx)   (idx == FOLLOWUP_REQUEST || idx == FOLLOWUP_DENY || idx == FOLLOWUP_ACCEPT || IS_FOLLOWUP_CTRL_REQ_TYPE_VALID(idx))
 
#define IS_UNIDIR(ctrl)   (ctrl == CTRL_UNIDIR_CONTINUE || ctrl == CTRL_UNIDIR_STOP)
 
#define IS_PINGLIKE(ctrl)   (ctrl == CTRL_PINGLIKE_REQ || ctrl == CTRL_PINGLIKE_REPLY || ctrl == CTRL_PINGLIKE_ENDREQ || ctrl == CTRL_PINGLIKE_ENDREPLY || ctrl == CTRL_PINGLIKE_REQ_TLESS || ctrl == CTRL_PINGLIKE_REPLY_TLESS || ctrl == CTRL_PINGLIKE_ENDREQ_TLESS || ctrl == CTRL_PINGLIKE_ENDREPLY_TLESS)
 
#define IS_LAMP(reserved, ctrl)   (reserved==PROTO_LAMP && (ctrl & PROTO_LAMP_CTRL_MASK)==PROTO_LAMP_CTRL_MASK)
 
#define ETHERTYPE_LAMP   ETH_P_802_EX1
 
#define MAX_LAMP_LEN   (65535)
 
#define LAMP_HDR_PAYLOAD_SIZE_STR(data)   sizeof(struct lamphdr)+strlen(data)
 
#define LAMP_HDR_PAYLOAD_SIZE(size)   sizeof(struct lamphdr)+size
 
#define LAMP_HDR_SIZE()   sizeof(struct lamphdr)
 
#define TYPE_TO_CTRL(field)   (field | PROTO_LAMP_CTRL_MASK)
 
#define CTRL_TO_TYPE(field)   (field & 0x0F)
 

Enumerations

enum  lamptype_t {
  PINGLIKE_REQ, PINGLIKE_REPLY, PINGLIKE_ENDREQ, PINGLIKE_ENDREPLY,
  UNIDIR_CONTINUE, UNIDIR_STOP, REPORT, ACK,
  INIT, PINGLIKE_REQ_TLESS, PINGLIKE_REPLY_TLESS, PINGLIKE_ENDREQ_TLESS,
  PINGLIKE_ENDREPLY_TLESS, FOLLOWUP_CTRL, FOLLOWUP_DATA
}
 LaMP type enumerator. More...
 
enum  endflag_t { FLG_CONTINUE, FLG_STOP, FLG_NONE }
 End flag type. More...
 

Functions

void lampHeadPopulate (struct lamphdr *lampHeader, unsigned char ctrl, unsigned short id, unsigned short seq)
 Populate a LaMP header. More...
 
void lampHeadSetTimestamp (struct lamphdr *lampHeader, struct timeval *tStampPtr)
 Set the timestamp inside a LaMP Header. More...
 
void lampEncapsulate (byte_t *packet, struct lamphdr *lampHeader, byte_t *data, size_t payloadsize)
 Combine LaMP payload and header. More...
 
void lampSetUnidirStop (struct lamphdr *lampHeader)
 Set the control field of a LaMP header to "Unidirectional stop". More...
 
void lampSetPinglikeEndreq (struct lamphdr *lampHeader)
 Set the control field of a LaMP header to "Ping-like (bidirectional) end request". More...
 
void lampSetPinglikeEndreqTless (struct lamphdr *lampHeader)
 Set the control field of a LaMP header to "Ping-like (bidirectional) end request (timestampless)". More...
 
void lampSetPinglikeEndreqAll (struct lamphdr *lampHeader)
 Set the control field of a LaMP header to "Ping-like (bidirectional) end request". More...
 
void lampHeadSetConnType (struct lamphdr *initLampHeader, uint16_t mode_index)
 Set the INIT type field ("length or packet type") inside a LaMP header. More...
 
void lampHeadSetFollowupCtrlType (struct lamphdr *followupLampHeader, uint16_t followup_type)
 Set the FOLLOWUP_CTRL type field ("length or packet type") inside a LaMP header. More...
 
void lampHeadIncreaseSeq (struct lamphdr *inpacket_headerptr)
 Increase the sequence number inside a LaMP header. More...
 
int rawLampSend (int descriptor, struct sockaddr_ll addrll, struct lamphdr *inpacket_headerptr, byte_t *ethernetpacket, size_t finalpacketsize, endflag_t end_flag, protocol_t llprot)
 Send LaMP packet over a raw socket, automatically setting some fields such as the timestamp (when needed) More...
 
void lampHeadGetData (byte_t *lampPacket, lamptype_t *type, unsigned short *id, unsigned short *seq, unsigned short *len, struct timeval *timestamp, byte_t *payload)
 Extract relevant data from a LaMP packet. More...
 
byte_tlampGetPacketPointers (byte_t *pktbuf, struct lamphdr **lampHeader)
 Get pointers to header and payload in a LaMP packet. More...
 

Detailed Description

Extra Latency Measurement Protocol (custom protocol) support, developed in Politecnico di Torino.

This file represents an additional module of the Rawsock library, adding support for a custom L7 protocol, which can be encapsulated inside any packet and transported using any protocol, providing a tool to measure latency between wireless (and wired) devices. We decided to call it LaMP (Latency Measurement Protocol).

This file uses rawsock.h, but it is not included within the main module, allowing the user to include the Rawsock library without forcing him/her to include also the LaMP module, if he/she does not need it.

The version number of this module is set to be the same as the main Rawsock library version number.

Version
0.3.4
Date
2020-04-24

Definition in file rawsock_lamp.h.

Macro Definition Documentation

#define CTRL_ACK   0xA7

LaMP type (full control field): Generic Acknowledgment (user can specify an additional custom payload, but it is not suggested to do so)

Definition at line 34 of file rawsock_lamp.h.

#define CTRL_CONN_INIT   0xA8

LaMP type (full control field): Connection INIT packet (initial handshake INIT packet)

Definition at line 35 of file rawsock_lamp.h.

#define CTRL_FOLLOWUP_CTRL   0xAD

LaMP type (full control field): Follow up control message (request, deny or accept)

Definition at line 40 of file rawsock_lamp.h.

#define CTRL_FOLLOWUP_DATA   0xAE

LaMP type (full control field): Follow up data message (time delta message)

Definition at line 41 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_ENDREPLY   0xA3

LaMP type (full control field): Ping-like (bidirectional) end reply

Definition at line 30 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_ENDREPLY_TLESS   0xAC

LaMP type (full control field): Ping-like (bidirectional) end reply - timestampless

Definition at line 39 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_ENDREQ   0xA2

LaMP type (full control field): Ping-like (bidirectional) end request

Definition at line 29 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_ENDREQ_TLESS   0xAB

LaMP type (full control field): Ping-like (bidirectional) end request - timestampless

Definition at line 38 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_REPLY   0xA1

LaMP type (full control field): Ping-like (bidirectional) reply

Definition at line 28 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_REPLY_TLESS   0xAA

LaMP type (full control field): Ping-like (bidirectional) reply - timestampless

Definition at line 37 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_REQ   0xA0

LaMP type (full control field): Ping-like (bidirectional) request

Definition at line 27 of file rawsock_lamp.h.

#define CTRL_PINGLIKE_REQ_TLESS   0xA9

LaMP type (full control field): Ping-like (bidirectional) request - timestampless

Definition at line 36 of file rawsock_lamp.h.

#define CTRL_TO_TYPE (   field)    (field & 0x0F)

Conversion macro: Macro to convert a full control field (ctrl) value (0xA + 4 type bits) to the corresponding lamptype_t value (4 bits).

Definition at line 84 of file rawsock_lamp.h.

#define CTRL_UNIDIR_CONTINUE   0xA4

LaMP type (full control field): Unidirectional continue packet (there will be more packets after the current one)

Definition at line 31 of file rawsock_lamp.h.

#define CTRL_UNIDIR_REPORT   0xA6

LaMP type (full control field): Report packet (payload should contain user-defined data to be transmitted containing latency related statistics)

Definition at line 33 of file rawsock_lamp.h.

#define CTRL_UNIDIR_STOP   0xA5

LaMP type (full control field): Unidirectional stop packet (last packet of the current session)

Definition at line 32 of file rawsock_lamp.h.

#define ETHERTYPE_LAMP   ETH_P_802_EX1

Local Experimental Ethertype should be used if LaMP is encapsulated directly inside a 802.11/Ethernet packet. You can use ETHERTYPE_LAMP for the sake of clarity (but ETH_P_802_EX1 is perfectly fine too).

Definition at line 76 of file rawsock_lamp.h.

#define FOLLOWUP_ACCEPT   0x0200

LaMP follow-up control type: follow-up accept

Definition at line 46 of file rawsock_lamp.h.

#define FOLLOWUP_DENY   0x0100

LaMP follow-up control type: follow-up deny

Definition at line 45 of file rawsock_lamp.h.

#define FOLLOWUP_REQUEST   0x0000

LaMP follow-up control type: follow-up request (default request type: 0x00)

Definition at line 44 of file rawsock_lamp.h.

#define FOLLOWUP_REQUEST_T_APP   0x0000

LaMP follow-up request type: application timestamps

Definition at line 50 of file rawsock_lamp.h.

#define FOLLOWUP_REQUEST_T_HW   0x0003

LaMP follow-up request type: hardware timestamps

Definition at line 53 of file rawsock_lamp.h.

#define FOLLOWUP_REQUEST_T_KRN   0x0002

LaMP follow-up request type: kernel timestamps

Definition at line 52 of file rawsock_lamp.h.

#define FOLLOWUP_REQUEST_T_KRN_RX   0x0001

LaMP follow-up request type: kernel rx timestamps

Definition at line 51 of file rawsock_lamp.h.

#define FOLLOWUP_UNKNOWN   0xFFFF

LaMP follow-up control type: bad or unknown request

Definition at line 47 of file rawsock_lamp.h.

#define INIT_PINGLIKE_INDEX   0x0001

INIT type field value: ping-like.

Definition at line 64 of file rawsock_lamp.h.

#define INIT_UNIDIR_INDEX   0x0002

INIT type field value: undirectional.

Definition at line 65 of file rawsock_lamp.h.

#define IS_CTRL_PINGLIKE_ENDREPLY (   ctrl)    (ctrl==CTRL_PINGLIKE_ENDREPLY || ctrl==CTRL_PINGLIKE_ENDREPLY_TLESS)

Multi-type LaMP type check macro: checks if the control field of a LaMP header, specified as ctrl, corresponds to any kind of ping-like end reply

Definition at line 62 of file rawsock_lamp.h.

#define IS_CTRL_PINGLIKE_ENDREQ (   ctrl)    (ctrl==CTRL_PINGLIKE_ENDREQ || ctrl==CTRL_PINGLIKE_ENDREQ_TLESS)

Multi-type LaMP type check macro: checks if the control field of a LaMP header, specified as ctrl, corresponds to any kind of ping-like end request

Definition at line 61 of file rawsock_lamp.h.

#define IS_CTRL_PINGLIKE_REPLY (   ctrl)    (ctrl==CTRL_PINGLIKE_REPLY || ctrl==CTRL_PINGLIKE_REPLY_TLESS)

Multi-type LaMP type check macro: checks if the control field of a LaMP header, specified as ctrl, corresponds to any kind of ping-like reply

Definition at line 60 of file rawsock_lamp.h.

#define IS_CTRL_PINGLIKE_REQ (   ctrl)    (ctrl==CTRL_PINGLIKE_REQ || ctrl==CTRL_PINGLIKE_REQ_TLESS)

Multi-type LaMP type check macro: checks if the control field of a LaMP header, specified as ctrl, corresponds to any kind of ping-like request

Definition at line 59 of file rawsock_lamp.h.

#define IS_FOLLOWUP_CTRL (   ctrl)    (ctrl == CTRL_FOLLOWUP_CTRL)

LaMP Test macro: checks if the given control field value (specified as ctrl) is corresponding to "Follow-up control".

Definition at line 69 of file rawsock_lamp.h.

#define IS_FOLLOWUP_CTRL_REQ_TYPE_VALID (   idx)    (idx == FOLLOWUP_REQUEST_T_APP || idx == FOLLOWUP_REQUEST_T_KRN_RX || idx == FOLLOWUP_REQUEST_T_KRN || idx == FOLLOWUP_REQUEST_T_HW || (idx >= 0x00F0 && idx<= 0x00FF))

LaMP Test macro: checks, given the full 16-bits long "payload length or packet type" field, whether it contains a valid follow-up request type or not (user defined values are included).

Definition at line 70 of file rawsock_lamp.h.

#define IS_FOLLOWUP_CTRL_TYPE_VALID (   idx)    (idx == FOLLOWUP_REQUEST || idx == FOLLOWUP_DENY || idx == FOLLOWUP_ACCEPT || IS_FOLLOWUP_CTRL_REQ_TYPE_VALID(idx))

LaMP Test macro: checks whether the given FOLLOWUP_CTRL type field value (which can be extracted, for instance, from a received LaMP header and specified as idx) is valid or not.

Definition at line 71 of file rawsock_lamp.h.

#define IS_FOLLOWUP_REQUEST (   lenortype_field)    ((lenortype_field & 0xFF00)==0x0000)

LaMP Test macro: it checks, given a "payload length or packet type" field of a FOLLOWUP_CTRL packet, specified as 'lenortype_field', if it corresponds to a valid FOLLOWUP_REQUEST or not, no matter the request type (i.e. it checks if the first byte is set to 0x00)

Definition at line 56 of file rawsock_lamp.h.

#define IS_INIT (   ctrl)    (ctrl == CTRL_CONN_INIT)

LaMP Test macro: checks if the given control field value (specified as ctrl) is corresponding to "Connection INIT".

Definition at line 68 of file rawsock_lamp.h.

#define IS_INIT_INDEX_VALID (   idx)    (idx == INIT_PINGLIKE_INDEX || idx == INIT_UNIDIR_INDEX)

LaMP Test macro: checks whether the given INIT type field value (which can be extracted, for instance, from a received LaMP header and specified as idx) is valid or not.

Definition at line 67 of file rawsock_lamp.h.

#define IS_LAMP (   reserved,
  ctrl 
)    (reserved==PROTO_LAMP && (ctrl & PROTO_LAMP_CTRL_MASK)==PROTO_LAMP_CTRL_MASK)

LaMP Test macro: important macro: you can use this to check if a received packet is really encapsulating LaMP, after trying to extract the reserved (reserved) and control (ctrl) fields from it (threating the first bytes as if they were a LaMP header).

Definition at line 74 of file rawsock_lamp.h.

#define IS_PINGLIKE (   ctrl)    (ctrl == CTRL_PINGLIKE_REQ || ctrl == CTRL_PINGLIKE_REPLY || ctrl == CTRL_PINGLIKE_ENDREQ || ctrl == CTRL_PINGLIKE_ENDREPLY || ctrl == CTRL_PINGLIKE_REQ_TLESS || ctrl == CTRL_PINGLIKE_REPLY_TLESS || ctrl == CTRL_PINGLIKE_ENDREQ_TLESS || ctrl == CTRL_PINGLIKE_ENDREPLY_TLESS)

LaMP Test macro: checks, though the specified (as ctrl) control field value, if the current packet is ping-like.

Definition at line 73 of file rawsock_lamp.h.

#define IS_UNIDIR (   ctrl)    (ctrl == CTRL_UNIDIR_CONTINUE || ctrl == CTRL_UNIDIR_STOP)

LaMP Test macro: checks, though the specified (as ctrl) control field value, if the current packet is undirectional.

Definition at line 72 of file rawsock_lamp.h.

#define LAMP_HDR_PAYLOAD_SIZE (   size)    sizeof(struct lamphdr)+size

Size definition: given size, in bytes, the LaMP packet size (LaMP header + payload) containing a payload with the specified size is calculated and returned in bytes. This macro is similar to UDP_PACKET_SIZE_S, IP_UDP_PACKET_SIZE_S and ETH_IP_UDP_PACKET_SIZE_S, defined in rawsock.h

Definition at line 80 of file rawsock_lamp.h.

#define LAMP_HDR_PAYLOAD_SIZE_STR (   data)    sizeof(struct lamphdr)+strlen(data)

Size definition: given data, as a string, the LaMP packet size (header + payload) containing the specified string is calculated and returned in bytes.

Definition at line 79 of file rawsock_lamp.h.

#define LAMP_HDR_SIZE ( )    sizeof(struct lamphdr)

Size definition: this macro returns the size of a LaMP header. Since the underlying struct lamphdr should be already "packed by design", this macro should be equivant to a call to ´sizeof(struct lamphdr)´, but a bit more compact.

Definition at line 81 of file rawsock_lamp.h.

#define MAX_LAMP_LEN   (65535)

**LaMP size definition: maximum payload size a LaMP packet can bear.

Definition at line 77 of file rawsock_lamp.h.

#define PROTO_LAMP   0xAA

LaMP reserved field value: the reserved field of LaMP must always be checked against this constant, as it represents the value that every LaMP packet should contain inside the reserved field. It allows to distinguish LaMP packets with respect to non-LaMP payloads.

Definition at line 24 of file rawsock_lamp.h.

#define PROTO_LAMP_CTRL_MASK   0xA0

LaMP control reserved field mask: every LaMP packet should contain 0xA as MSB of the control field, as extension of the reserved field itself. You can use this mask to check if the MSB is really 0xA (as rquired by LaMP); however, it is highly suggested to use

Definition at line 25 of file rawsock_lamp.h.

#define TYPE_TO_CTRL (   field)    (field | PROTO_LAMP_CTRL_MASK)

Conversion macro: Macro to convert a lamptype_t value (specified as field, 4 bits) to the corresponding full control field (ctrl) value (0xA + 4 type bits).

Definition at line 83 of file rawsock_lamp.h.

Enumeration Type Documentation

enum endflag_t

End flag type.

This enumerator can be used to tell rawLampSend() is the current packet whether the last one of the current session or not.

If it is FLG_STOP, the current packet will be threated as last one, and its type will be forced to "ping-like (bidirectional) end request" or "ping-like (bidirectional) end request - timestampless" or "unidirectional stop" (depending on the current session type).

If it is FLG_CONTINUE the current packet will not be threated as the last one, and its type won't be changed (it remains as specified before, for example by means of lampHeadPopulate().

FLG_NONE can be used in all the cases in which the user does not need to specify if the current packet is the last one or not (e.g. ACK and INIT packets).

Please see rawLampSend(), as the existence of this enumerator is directly linked to this function.

Enumerator
FLG_CONTINUE 

Flag CONTINUE: this is not the last packet of the session

FLG_STOP 

Flag STOP: this is the last packet of the session

FLG_NONE 

Flag NONE: the user does not want/need to specify any flag to rawLampSend()

Definition at line 132 of file rawsock_lamp.h.

enum lamptype_t

LaMP type enumerator.

LaMP type enumerator: it can be used to manage the different types of LaMP packets without having to deal with the upper 4 bits of the control header field, which are always equal to 0xA.

The conversion from a control field value to the corresponding lamptype_t value, and vice versa, can be obtained through TYPE_TO_CTRL and CTRL_TO_TYPE.

Enumerator
PINGLIKE_REQ 

Ping-like (bidirectional) request

PINGLIKE_REPLY 

Ping-like (bidirectional) reply

PINGLIKE_ENDREQ 

Ping-like (bidirectional) end request

PINGLIKE_ENDREPLY 

Ping-like (bidirectional) end reply

UNIDIR_CONTINUE 

Unidirectional continue packet (there will be more packets after the current one)

UNIDIR_STOP 

Unidirectional stop packet (last packet of the current session)

REPORT 

Report packet (payload should contain user-defined data to be transmitted containing latency related statistics)

ACK 

Generic Acknowledgment (user can specify an additional custom payload, but it is not suggested to do so)

INIT 

Connection INIT packet (initial handshake INIT packet)

PINGLIKE_REQ_TLESS 

Ping-like (bidirectional) request - timestampless

PINGLIKE_REPLY_TLESS 

Ping-like (bidirectional) reply - timestampless

PINGLIKE_ENDREQ_TLESS 

Ping-like (bidirectional) end request - timestampless

PINGLIKE_ENDREPLY_TLESS 

Ping-like (bidirectional) end reply - timestampless

FOLLOWUP_CTRL 

Follow-up control message

FOLLOWUP_DATA 

Follow-up data message

Definition at line 99 of file rawsock_lamp.h.

Function Documentation

void lampEncapsulate ( byte_t packet,
struct lamphdr lampHeader,
byte_t data,
size_t  payloadsize 
)

Combine LaMP payload and header.

This function combines a LaMP (optional) payload and header, the latter in the form of a struct lamphdr structure.

It can be used if an additional payload have to be inserted inside a LaMP packet. If the user does not want to insert any payload, he can directly send the LaMP header structure as payload of any other protocol, encapsulating LaMP, without the need of calling lampEncapsulate().

The user shall specify a buffer in which the full packet will be put, the LaMP header, the payload (byte_t *data) and its size in bytes.

Warning
The packet buffer should be already allocated, and it should be big enough to contain both the header and the payload. It is possible to use the macros defined in rawsock_lamp.h, such as LAMP_HDR_PAYLOAD_SIZE(LaMP_payload_size), to find the required size starting from an higher layer payload size and then call a malloc() with that size to allocate the memory for packet.
Note
The "length" field of the LaMP header is automatically set.
Parameters
[out]packetPacket buffer (should be already allocated) that will contain the full LaMP packet (LaMP header + payload)
[in]lampHeaderLaMP header, as struct lamphdr. Should be filled in with lampHeadPopulate() before being passed to this function.
[in]dataBuffer containing the payload.
[in]payloadsizeSize, in bytes, of the payload.
Returns
None.

Definition at line 246 of file rawsock_lamp.c.

byte_t* lampGetPacketPointers ( byte_t pktbuf,
struct lamphdr **  lampHeader 
)

Get pointers to header and payload in a LaMP packet.

This function can be used to obtain, given a certain buffer containing a full LaMP packet, including its payload, the pointers to the header (i.e. to the struct lamphdr and payload sections

Example of call:

payload=lampGetPacketPointers(packet,&lampHeader);

with:

struct lamphdr *lampHeader;
byte_t *payload;
Warning
No memory is allocated by this function! It will return pointers inside the original pktbuf buffer, by doing the proper arithmetics.
Never use this function on a LaMP packet without a payload. It will work, but the returned payload pointer will point to a memory area which is just after the one storing the struct lamphdr, and that, if no payload was received, is not belonging to the application callling lampGetPacketPointers(), possibly causing a segmentation fault or an undefined behaviour when trying to access or write it.
Parameters
[in]pktbufPacket buffer, containing a full valid LaMP packet (the checksum or even the reserved field can be wrong, "valid" means here "that can be LaMP").
[out]lampHeaderThis pointer will be written by the function, storing the pointer to the LaMP header inside the pktbuf packet buffer.
Returns
The pointer to the payload (of type byte_t) is returned by the function. If pktbuf is a valid pointer, it should never happen that the returned pointer is NULL.

Definition at line 383 of file rawsock_lamp.c.

void lampHeadGetData ( byte_t lampPacket,
lamptype_t type,
unsigned short *  id,
unsigned short *  seq,
unsigned short *  len,
struct timeval *  timestamp,
byte_t payload 
)

Extract relevant data from a LaMP packet.

This function can be used to extract all the relevant data, already converted to host byte order, from a LaMP packet.

The function takes as input the pointer to the LaMP packet (i.e. to a memory area where a struct lamphdr, plus the payload, is stored).

Parameters
[in]lampPacketPointer to the LaMP packet buffer (with type byte_t).
[out]idIdentification field (LaMP ID) value.
[out]seqCurrent sequence number inside the header.
[out]lenValue stored inside the "length or INIT type" field.
[out]timestampstruct timeval which is filled using the timestamp stored inside the LaMP header.
[out]payloadIf this pointer is non-NULL, it should be related to a memory area big enough to contain a possible LaMP payload. Then, the function will copy the payload contained inside the LaMP packet buffer to that memory area. Il the length field is 0 (or NULL is specified), no copy operation will be performed.
Returns
None.

Definition at line 339 of file rawsock_lamp.c.

void lampHeadIncreaseSeq ( struct lamphdr inpacket_headerptr)

Increase the sequence number inside a LaMP header.

This function can be used to cyclically increase (up to 65535 and then back to 0) the sequence number inside an existing LaMP header of an existing LaMP packet.

The function takes as input the pointer to a LaMP header (i.e. to a memory area where a struct lamphdr is stored).

Parameters
[in,out]inpacket_headerptrPointer to the LaMP header structure, in which the sequence number will be increased by 1.
Returns
None.

Definition at line 137 of file rawsock_lamp.c.

void lampHeadPopulate ( struct lamphdr lampHeader,
unsigned char  ctrl,
unsigned short  id,
unsigned short  seq 
)

Populate a LaMP header.

This function can be used to populate a LaMP header (struct lamphdr).

The user shall specify an already existing LaMP header structure to be filled in, the control field value (the full one), as ctrl, the identification value and the sequence number of the current packet.

Note
Like all the other functions inside the Rawsock library, it already takes care of byte ordering.
In order to increase the sequence number inside an existing LaMP header, it is not necessary to repeatedly call this function. Use, instead, lampHeadIncreaseSeq().
Warning
Important: the timestamp is initialized to 0. However, you should set a timestamp for any non timestampless or control packet (i.e. ACK and INIT). This can be achieved manually thanks to lampHeadSetTimestamp() (to be used for transmissions over normal sockets) or automatically when calling rawLampSend() (to be used for transmissions over raw sockets). It is always advised to call, in the manual case, lampHeadSetTimestamp() as the last operation before sending the data.
Parameters
[in,out]lampHeaderPointer to the LaMP header structure.
[in]ctrlFull control field value (including the first 4 bits equal to 0xA) to be placed inside the header.
[in]idIdentification field value.
[in]seq(Initial) sequence number to be put inside the LaMP header.
Returns
None.

Definition at line 34 of file rawsock_lamp.c.

void lampHeadSetConnType ( struct lamphdr initLampHeader,
uint16_t  mode_index 
)

Set the INIT type field ("length or packet type") inside a LaMP header.

This function can be used to set the "length or packet type" field inside an already populated LaMP header.

As it can be used to set an INIT type, this function has an effect only if the headers carries an "INIT" packet type and a valid index (see INIT_PINGLIKE_INDEX and INIT_UNIDIR_INDEX) is specified, otherwise it has no effect.

Note
Like all the other functions inside the Rawsock library, it already takes care of byte ordering.
Parameters
[in,out]initLampHeaderPointer to the LaMP header structure, already populated by lampHeadPopulate().
[in]mode_indexINIT type index to be used (see INIT_PINGLIKE_INDEX and INIT_UNIDIR_INDEX)
Returns
None.

Definition at line 61 of file rawsock_lamp.c.

void lampHeadSetFollowupCtrlType ( struct lamphdr followupLampHeader,
uint16_t  followup_type 
)

Set the FOLLOWUP_CTRL type field ("length or packet type") inside a LaMP header.

This function can be used to set the "length or packet type" field inside an already populated LaMP header.

As it can be used to set a FOLLOWUP_CTRL type, this function has an effect only if the headers carries a "FOLLOWUP_CTRL" packet type and a valid type is specified, otherwise it has no effect.

Note
Like all the other functions inside the Rawsock library, it already takes care of byte ordering.
Parameters
[in,out]followupLampHeaderPointer to the LaMP header structure, already populated by lampHeadPopulate().
[in]followup_typeFOLLOWUP_CTRL type to be used (see for instance FOLLOWUP_REQUEST)
Returns
None.

Definition at line 83 of file rawsock_lamp.c.

void lampHeadSetTimestamp ( struct lamphdr lampHeader,
struct timeval *  tStampPtr 
)

Set the timestamp inside a LaMP Header.

This function can be used to set the timestamp (seconds and microseconds fields) inside the specified LaMP header.

If tStampPtr is NULL, the timestamp is set to the time instant in which this function was called and started its execution. If it is not NULL, it will be set using the values stored inside the specified struct timeval.

A realtime clock is used (i.e. the one used for gettimeofday()).

This function has no effect if the packet is timestampless (i.e. if the control field is indicating a "TLESS" type)

Note
Like all the other functions inside the Rawsock library, it already takes care of byte ordering.
Parameters
[in,out]lampHeaderPointer to the LaMP header structure.
[in]tStampPtrPointer to a struct timeval to store a custom timestamp, or NULL to use the current time
Returns
None.

Definition at line 109 of file rawsock_lamp.c.

void lampSetPinglikeEndreq ( struct lamphdr lampHeader)

Set the control field of a LaMP header to "Ping-like (bidirectional) end request".

This function can be used to set the the control field inside an existing LaMP header of an existing LaMP packet, to "Ping-like (bidirectional) end request".

The function takes as input the pointer to a LaMP header (i.e. to a memory area where a struct lamphdr is stored).

Parameters
[in,out]lampHeaderPointer to the LaMP header structure.
Returns
None.

Definition at line 176 of file rawsock_lamp.c.

void lampSetPinglikeEndreqAll ( struct lamphdr lampHeader)

Set the control field of a LaMP header to "Ping-like (bidirectional) end request".

This function can be used to set the the control field inside an existing LaMP header of an existing LaMP packet, to "Ping-like (bidirectional) end request".

This function will look at the already existing control field and act accordingly:

  1. If the control field corresponds to CTRL_PINGLIKE_REQ, CTRL_PINGLIKE_ENDREQ is set
  2. If the control field corresponds to CTRL_PINGLIKE_REQ_TLESS, CTRL_PINGLIKE_ENDREQ_TLESS is set
  3. Otherwise, the control field remains unmodified.

The function takes as input the pointer to a LaMP header (i.e. to a memory area where a struct lamphdr is stored).

Parameters
[in,out]lampHeaderPointer to the LaMP header structure.
Returns
None.

Definition at line 214 of file rawsock_lamp.c.

void lampSetPinglikeEndreqTless ( struct lamphdr lampHeader)

Set the control field of a LaMP header to "Ping-like (bidirectional) end request (timestampless)".

This function can be used to set the the control field inside an existing LaMP header of an existing LaMP packet, to "Ping-like (bidirectional) end request (timestampless)".

The function takes as input the pointer to a LaMP header (i.e. to a memory area where a struct lamphdr is stored).

Parameters
[in,out]lampHeaderPointer to the LaMP header structure.
Returns
None.

Definition at line 192 of file rawsock_lamp.c.

void lampSetUnidirStop ( struct lamphdr lampHeader)

Set the control field of a LaMP header to "Unidirectional stop".

This function can be used to set the the control field inside an existing LaMP header of an existing LaMP packet, to "Unidirectional stop".

The function takes as input the pointer to a LaMP header (i.e. to a memory area where a struct lamphdr is stored).

Parameters
[in,out]lampHeaderPointer to the LaMP header structure.
Returns
None.

Definition at line 160 of file rawsock_lamp.c.

int rawLampSend ( int  descriptor,
struct sockaddr_ll  addrll,
struct lamphdr inpacket_headerptr,
byte_t ethernetpacket,
size_t  finalpacketsize,
endflag_t  end_flag,
protocol_t  llprot 
)

Send LaMP packet over a raw socket, automatically setting some fields such as the timestamp (when needed)

This function can be used to send a LaMP packet over a raw socket, automating some operations such as the insertion of the timestamp, which is performed as the last possible operation before sending.

It replaces any call to sendto().

This function allows the user to specify a certain protocol, which will be used to properly place the timestamp, when needed, inside the full packet. All the protocols defined inside protocol_t are supported by this function.

Warning
This function can only be used when sending LaMP data over raw sockets. That's why the pointer to the full packet to be sent is called ethernetpacket.
Parameters
[in]descriptorSocket descriptor related to the raw socket to be used to send the packet.
[in]addrllSocket address structure (struct sockaddr_ll, i.e. the same structure you would pass to a call to sendto()).
[in]inpacket_headerptrPointer to the LaMP header inside the full packet, passed as ethernetpacket (a future improvement will remove the necessity of passing this pointer).
[in]ethernetpacketPointer to the buffer storing the whole packet to be sent (i.e. the same buffer you would pass to a call to sendto()).
[in]finalpacketsizeSize of the whole packet (i.e. the same size you would pass to a call to sendto()).
[in]end_flagEnd flag value: see endflag_t.
[in]llprotProtocol type, using the protocol_t definition inside rawsock.h.
Returns
It returns 1 if the packet was successfully sent, 0 otherwise.

Definition at line 276 of file rawsock_lamp.c.