![]() |
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).
|
#include <net/ethernet.h>#include <linux/udp.h>#include <linux/ip.h>#include <arpa/inet.h>#include <stdio.h>#include <stdbool.h>#include <string.h>Go to the source code of this file.
Data Structures | |
| struct | ipaddrs |
| Structure to store a couple of source and destinaion IPv4 addresses. More... | |
Macros | |
| #define | MAC_FILE_PATH_SIZE 23 |
| #define | MAC_ADDR_SIZE 6 |
| #define | ERR_WLAN_NOIF 0 |
| #define | ERR_WLAN_SOCK -1 |
| #define | ERR_WLAN_GETIFADDRS -2 |
| #define | ERR_WLAN_INDEX -3 |
| #define | ERR_WLAN_GETSRCMAC -4 |
| #define | ERR_WLAN_GETIFINDEX -5 |
| #define | ERR_WLAN_GETSRCIP -6 |
| #define | ERR_IPHEAD_SOCK -10 |
| #define | ERR_IPHEAD_NOSRCADDR -11 |
| #define | ERR_VIFPRINTER_SOCK -20 |
| #define | ERR_VIFPRINTER_GETIFADDRS -21 |
| #define | WLANLOOKUP_WLAN 0 |
| #define | WLANLOOKUP_NONWLAN 1 |
| #define | MAC_NULL 0x00 |
| #define | MAC_BROADCAST 0x01 |
| #define | MAC_UNICAST 0x02 |
| #define | MAC_MULTICAST 0x03 |
| #define | MAC_ZERO 0x04 |
| #define | ETHERTYPE_GEONET 0x8947 |
| #define | ETHERTYPE_WSMP 0x88DC |
| #define | WLANLOOKUP_LOOPBACK -1 |
| #define | BASIC_IHL 5 |
| #define | IPV4 4 |
| #define | BASIC_UDP_TTL 64 |
| #define | UDPHEADERLEN 8 |
| #define | FLAG_NOFRAG_MASK (1<<6) |
| #define | FLAG_RESERVED_MASK (1<<7) |
| #define | FLAG_MOREFRAG_MASK (1<<5) |
| #define | CSUM_IP 0x00 |
| #define | CSUM_UDP 0x01 |
| #define | CSUM_UDPIP 0x80 |
| #define | PRI_MAC "%02x:%02x:%02x:%02x:%02x:%02x" |
| #define | MAC_PRINTER(mac_array) mac_array[0], mac_array[1], mac_array[2], mac_array[3], mac_array[4], mac_array[5] |
| #define | SCN_MAC "%x:%x:%x:%x:%x:%x%*c" |
| #define | MAC_SCANNER(mac_array) &mac_array[0], &mac_array[1], &mac_array[2], &mac_array[3], &mac_array[4], &mac_array[5] |
| #define | UDP_PACKET_SIZE(data) sizeof(struct udphdr)+sizeof(data) |
| #define | IP_UDP_PACKET_SIZE(data) sizeof(struct iphdr)+sizeof(struct udphdr)+sizeof(data) |
| #define | ETH_IP_UDP_PACKET_SIZE(data) sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct udphdr)+sizeof(data) |
| #define | UDP_PACKET_SIZE_S(size) sizeof(struct udphdr)+size |
| #define | IP_UDP_PACKET_SIZE_S(size) sizeof(struct iphdr)+sizeof(struct udphdr)+size |
| #define | ETH_IP_UDP_PACKET_SIZE_S(size) sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct udphdr)+size |
| #define | BYTE_TYPE |
Typedefs | |
| typedef uint8_t | byte_t |
| typedef uint8_t * | macaddr_t |
| typedef unsigned short | ethertype_t |
| typedef int | rawsockerr_t |
| typedef unsigned char | csumt_t |
| typedef __sum16 | csum16_t |
Enumerations | |
| enum | protocol_t { UNSET_P, UDP, AMQP_0_9, AMQP_1_0 } |
| Protocol type enumerator. More... | |
Functions | |
| rawsockerr_t | wlanLookup (char *devname, int *ifindex, macaddr_t mac, struct in_addr *srcIP, int index, int mode) |
| Automatically look for available WLAN, non-WLAN or loopback interfaces. More... | |
| rawsockerr_t | vifPrinter (FILE *stream) |
| Print information about available interfaces. More... | |
| macaddr_t | prepareMacAddrT () |
| Prepare a macaddr_t variable. More... | |
| unsigned int | macAddrTypeGet (macaddr_t mac) |
| Get the MAC address type. More... | |
| void | freeMacAddrT (macaddr_t mac) |
| Free a macaddr_t variable. More... | |
| void | rs_printerror (FILE *stream, rawsockerr_t code) |
| Print more detailed error messages. More... | |
| void | display_packet (const char *text, byte_t *packet, unsigned int len) |
| Display packet in hexadecimal form. More... | |
| void | display_packetc (const char *text, byte_t *packet, unsigned int len) |
| Display packet in character form. More... | |
| uint64_t | hton64 (uint64_t hostu64) |
| Convert a 64-bit unsigned value between host and network byte order. More... | |
| uint64_t | ntoh64 (uint64_t netu64) |
| Convert a 64-bit unsigned value between network and host byte order. More... | |
| void | etherheadPopulateB (struct ether_header *etherHeader, macaddr_t mac, ethertype_t type) |
| Populate broadcast Ethernet header (variant of etherheadPopulate()) More... | |
| void | etherheadPopulate (struct ether_header *etherHeader, macaddr_t macsrc, macaddr_t macdst, ethertype_t type) |
| Populate Ethernet header. More... | |
| size_t | etherEncapsulate (byte_t *packet, struct ether_header *header, byte_t *sdu, size_t sdusize) |
| Combine Ethernet SDU and PCI. More... | |
| void | getSrcMAC (struct ether_header *etherHeader, macaddr_t macsrc) |
| Retrieve source MAC address field from Ethernet header. More... | |
| rawsockerr_t | IP4headPopulateB (struct iphdr *IPhead, char *devname, unsigned char tos, unsigned short frag_offset, unsigned char ttl, unsigned char protocol, unsigned int flags, struct ipaddrs *addrs) |
| Populate broadcast IP version 4 header (variant of IP4headPopulate()) More... | |
| rawsockerr_t | IP4headPopulateS (struct iphdr *IPhead, char *devname, struct in_addr destIP, unsigned char tos, unsigned short frag_offset, unsigned char ttl, unsigned char protocol, unsigned int flags, struct ipaddrs *addrs) |
| Populate IP version 4 header with struct in_addr addresses (variant of IP4headPopulate()) More... | |
| rawsockerr_t | IP4headPopulate (struct iphdr *IPhead, char *devname, char *destIP, unsigned char tos, unsigned short frag_offset, unsigned char ttl, unsigned char protocol, unsigned int flags, struct ipaddrs *addrs) |
| Populate IP version 4 header. More... | |
| void | IP4headAddID (struct iphdr *IPhead, unsigned short id) |
| Add ID to a given IPv4 header. More... | |
| void | IP4headAddTotLen (struct iphdr *IPhead, unsigned short len) |
| Add Total Length to a given IPv4 header. More... | |
| size_t | IP4Encapsulate (byte_t *packet, struct iphdr *header, byte_t *sdu, size_t sdusize) |
| Combine IPv4 SDU and PCI. More... | |
| void | UDPheadPopulate (struct udphdr *UDPhead, unsigned short sourceport, unsigned short destport) |
| Populate UDP header. More... | |
| size_t | UDPencapsulate (byte_t *packet, struct udphdr *header, byte_t *data, size_t payloadsize, struct ipaddrs addrs) |
| Combine UDP payload and header. More... | |
| byte_t * | UDPgetpacketpointers (byte_t *pktbuf, struct ether_header **etherHeader, struct iphdr **IPheader, struct udphdr **UDPheader) |
| Get pointers to headers and payload in UDP packet buffer. More... | |
| unsigned short | UDPgetpayloadsize (struct udphdr *UDPheader) |
| Get UDP payload size, given a UDP header. More... | |
| bool | validateEthCsum (byte_t *packet, csum16_t csum, csum16_t *combinedcsum, csumt_t type, void *args) |
| Validate the checksum of a raw "Ethernet" packet, i.e. of any packet containing a struct ether_header as first bytes. More... | |
| void | test_injectIPCsumError (byte_t *IPpacket) |
| Test function: inject a checksum error in an IP packet. More... | |
| void | test_injectUDPCsumError (byte_t *UDPpacket) |
| Test function: inject a checksum error in an UDP packet. More... | |
This file represents the main header file of the Rawsock library.
This file, represeting the main header file of the Rawsock library, should be included in your project everytime you want to use the library to simplify and enhance the use of Linux raw sockets. It supports, as of now, IPv4 and UDP.
Definition in file rawsock.h.
| #define BASIC_IHL 5 |
| #define BASIC_UDP_TTL 64 |
| #define CSUM_IP 0x00 |
Simple validateEthCsum() checksum type (csum_t): compute IPv4 checksum by specifying CSUM_IP as type.
| #define CSUM_UDP 0x01 |
Simple validateEthCsum() checksum type (csum_t): compute UDP checksum by specifying CSUM_UDP as type.
| #define CSUM_UDPIP 0x80 |
Combined validateEthCsum() checksum type (csum_t): compute IPv4 and UDP checksums by specifying CSUM_UDPIP as type.
| #define ERR_IPHEAD_NOSRCADDR -11 |
IP4headPopulate*() error definition: unable to retrieve current device IP address.
| #define ERR_IPHEAD_SOCK -10 |
IP4headPopulate*() error definition: internal socket creation error.
| #define ERR_VIFPRINTER_GETIFADDRS -21 |
vifPrinter() error definition: getifaddrs() (to obtain interfaces list) error.
| #define ERR_VIFPRINTER_SOCK -20 |
vifPrinter() error definition: socket creation error.
| #define ERR_WLAN_GETIFADDRS -2 |
wlanLookup() error definition: getifaddrs() (to obtain interfaces list) error.
| #define ERR_WLAN_GETIFINDEX -5 |
wlanLookup() error definition: unable to get source interface index (if requested).
| #define ERR_WLAN_GETSRCIP -6 |
wlanLookup() error definition: unable to get source interface index (if requested).
| #define ERR_WLAN_GETSRCMAC -4 |
wlanLookup() error definition: unable to get source MAC address (if requested).
| #define ERR_WLAN_INDEX -3 |
wlanLookup() error definition: wrong index specified.
| #define ERR_WLAN_NOIF 0 |
wlanLookup() error definition: no WLAN interfaces found.
| #define ERR_WLAN_SOCK -1 |
wlanLookup() error definition: socket creation error.
| #define ETH_IP_UDP_PACKET_SIZE | ( | data | ) | sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct udphdr)+sizeof(data) |
| #define ETH_IP_UDP_PACKET_SIZE_S | ( | size | ) | sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct udphdr)+size |
| #define ETHERTYPE_GEONET 0x8947 |
| #define ETHERTYPE_WSMP 0x88DC |
| #define FLAG_MOREFRAG_MASK (1<<5) |
IPv4 flags mask: Mask to set the More Fragments (MF) IPv4 flag, in the flags argument of IP4headPopulate*()
| #define FLAG_NOFRAG_MASK (1<<6) |
IPv4 flags mask: Mask to set the Don't Fragment (DF) IPv4 flag, in the flags argument of IP4headPopulate*()
| #define FLAG_RESERVED_MASK (1<<7) |
IPv4 flags mask: Mask to set the Reserved IPv4 flag, in the flags argument of IP4headPopulate*()
| #define IP_UDP_PACKET_SIZE | ( | data | ) | sizeof(struct iphdr)+sizeof(struct udphdr)+sizeof(data) |
| #define IP_UDP_PACKET_SIZE_S | ( | size | ) | sizeof(struct iphdr)+sizeof(struct udphdr)+size |
| #define IPV4 4 |
| #define MAC_ADDR_SIZE 6 |
| #define MAC_BROADCAST 0x01 |
MAC type definition (managed by prepareMacAddrT(), macAddrTypeGet() and freeMacAddrT()): broadcast (FF:FF:FF:FF:FF:FF) MAC address.
| #define MAC_MULTICAST 0x03 |
MAC type definition (managed by prepareMacAddrT(), macAddrTypeGet() and freeMacAddrT()): multicast (01:xx:xx:xx:xx:xx) MAC address.
| #define MAC_NULL 0x00 |
MAC type definition (managed by prepareMacAddrT(), macAddrTypeGet() and freeMacAddrT()): NULL MAC-address (NULL pointer returned).
| #define MAC_PRINTER | ( | mac_array | ) | mac_array[0], mac_array[1], mac_array[2], mac_array[3], mac_array[4], mac_array[5] |
MAC_PRINTER(address-variable) should be used in combination with PRI_MAC to specify the variable containing the MAC address. For instance, if addr is a variable of type macaddr_t, it is possible to print the corresponding address with printf("Address: " PRI_MAC "\\n",MAC_PRINTER(addr)).
| #define MAC_SCANNER | ( | mac_array | ) | &mac_array[0], &mac_array[1], &mac_array[2], &mac_array[3], &mac_array[4], &mac_array[5] |
MAC_SCANNER(address-variable) should be used in combination with SCN_MAC to specify the variable containing the MAC address (without &, as it is already added by MAC_SCANNER). For instance, if addr is an allocated variable of type macaddr_t, it is possible to store an address inside addr with scanf(SCN_MAC,MAC_SCANNER(addr)).
| #define MAC_UNICAST 0x02 |
MAC type definition (managed by prepareMacAddrT(), macAddrTypeGet() and freeMacAddrT()): unicast MAC address.
| #define MAC_ZERO 0x04 |
MAC type definition (managed by prepareMacAddrT(), macAddrTypeGet() and freeMacAddrT()): all-zero (00:00:00:00:00:00) MAC address (no MAC is set).
| #define PRI_MAC "%02x:%02x:%02x:%02x:%02x:%02x" |
Useful macro specifier for printing MAC addresses inside the printf() familty of functions. PRI_MAC works as a single specifier for the whole address, like PRIuxx in inttypes.h for printing xx bits integers, but without the leading %. See also the strictly related MAC_PRINTER macro.
| #define SCN_MAC "%x:%x:%x:%x:%x:%x%*c" |
Useful macro specifier for reading MAC addresses inside the scanf() familty of functions. SCN_MAC works as a single specifier for the whole address, like SCNuxx in inttypes.h for reading xx bits integers, but without the leading %. See also the strictly related MAC_SCANNER macro.
| #define UDP_PACKET_SIZE | ( | data | ) | sizeof(struct udphdr)+sizeof(data) |
| #define UDP_PACKET_SIZE_S | ( | size | ) | sizeof(struct udphdr)+size |
| #define UDPHEADERLEN 8 |
| #define WLANLOOKUP_LOOPBACK -1 |
wlanLookup() special index value definition: use WLANLOOKUP_LOOPBACK to search for loopback IF instead of WLAN IF.
| #define WLANLOOKUP_NONWLAN 1 |
wlanLookup() mode definition: look for non-wireless interfaces only.
| #define WLANLOOKUP_WLAN 0 |
wlanLookup() mode definition: look for wireless interfaces only.
| typedef uint8_t byte_t |
| typedef __sum16 csum16_t |
| typedef unsigned char csumt_t |
Custom type to store checksum types to be passed to validateEthCsum().
| typedef unsigned short ethertype_t |
| typedef uint8_t* macaddr_t |
Custom type to store a MAC address. It is better and should be managed with the provided prepareMacAddrT(), macAddrTypeGet() and freeMacAddrT() functions, to avoid messing up with pointer.
| typedef int rawsockerr_t |
| enum protocol_t |
Protocol type enumerator.
Protocol type enumerator, useful to manage more than one protocol on a single program. As more protocols will be supported, this enum will be updated accordingly.
| Enumerator | |
|---|---|
| UNSET_P |
Unspecified protocol type |
| UDP |
UDP over IPv4 |
| AMQP_0_9 |
AMQP 0.9 (RabbitMQ) - not yet supported by the library |
| AMQP_1_0 |
AMQP 1.0 (ActiveMQ) - not yet supported by the library |
| void display_packet | ( | const char * | text, |
| byte_t * | packet, | ||
| unsigned int | len | ||
| ) |
Display packet in hexadecimal form.
This function can be used to display a packet content in hexadecimal form, given the pointer to a buffer of bytes and its length (len).
text can be used to specify an additional text that will be printer before printing the actual packet content.
| [in] | text | Additional text preceding the packet content |
| [in] | packet | Buffer containing the whole packet to be printed |
| [in] | len | Length, in bytes, of the buffer to be printed (i.e. number of elements of the bytes buffer to be printed) |
| void display_packetc | ( | const char * | text, |
| byte_t * | packet, | ||
| unsigned int | len | ||
| ) |
Display packet in character form.
This function can be used to display a packet content in character form, given the pointer to a buffer of bytes and its length (len).
text can be used to specify an additional text that will be printer before printing the actual packet content.
| [in] | text | Additional text preceding the packet content |
| [in] | packet | Buffer containing the whole packet to be printed |
| [in] | len | Length, in bytes, of the buffer to be printed (i.e. number of elements of the bytes buffer to be printed) |
| size_t etherEncapsulate | ( | byte_t * | packet, |
| struct ether_header * | header, | ||
| byte_t * | sdu, | ||
| size_t | sdusize | ||
| ) |
Combine Ethernet SDU and PCI.
This function combines an Ethernet SDU and PCI, the latter in the form of a struct ether_header Linux structure.
The user shall specify a buffer in which the full packet will be put, the ethernet header, the SDU (byte_t *sdu) and its size in bytes.
| [out] | packet | Packet buffer (should be already allocated) that will contain the full packet (SDU+PCI). |
| [in] | header | Ethernet header, as struct ether_header. Should be filled in with etherheadPopulate*() before being passed to this function. |
| [in] | sdu | Buffer containing the SDU. |
| [in] | sdusize | Size, in bytes, of the SDU. |
| void etherheadPopulate | ( | struct ether_header * | etherHeader, |
| macaddr_t | macsrc, | ||
| macaddr_t | macdst, | ||
| ethertype_t | type | ||
| ) |
Populate Ethernet header.
This function can be used to populate an ethernet header (struct ether_header).
A variant, etherheadPopulateB(), addressed specifically to broadcast transmissions, exists.
The user shall specify the pointer to an already existing ether_header structure to be filled in, the source and destination MAC addresses and the EtherType (either one type in net/ethernet.h or one type in rawsock.h).
| [out] | etherHeader | Pointer to the Ethernet header structure, used in raw sockets (for instance, to send data over a ETH_P_ALL socket). |
| [in] | macsrc | macaddr_t variable containing the source MAC address. |
| [in] | macdst | macaddr_t variable containing the destination MAC address. |
| [in] | type | ethertype_t variable containing the EtherType. |
| void etherheadPopulateB | ( | struct ether_header * | etherHeader, |
| macaddr_t | mac, | ||
| ethertype_t | type | ||
| ) |
Populate broadcast Ethernet header (variant of etherheadPopulate())
This function can be used to populate an ethernet header (struct ether_header) for broadcast transmissions (i.e. a broadcast MAC address is automatically inserted).
The user shall specify the pointer to an already existing ether_header structure to be filled in, the source MAC and the EtherType (either one type in net/ethernet.h or one type in rawsock.h).
| [in,out] | etherHeader | Pointer to the Ethernet header structure, used in raw sockets (for instance, to send data over a ETH_P_ALL socket). |
| [in] | mac | macaddr_t variable containing the source MAC address. |
| [in] | type | ethertype_t variable containing the EtherType. |
| void freeMacAddrT | ( | macaddr_t | mac | ) |
| void getSrcMAC | ( | struct ether_header * | etherHeader, |
| macaddr_t | macsrc | ||
| ) |
Retrieve source MAC address field from Ethernet header.
Simple function to retrieve the source MAC address field from a struct ether_header, and copy it inside the macsrc variable.
| [in] | etherHeader | struct ether_header, already filled in or extracted from a received packet. |
| [out] | macsrc | Source MAC address, filled in by the function. |
| uint64_t hton64 | ( | uint64_t | hostu64 | ) |
Convert a 64-bit unsigned value between host and network byte order.
This function works like htons() and htonl(), but with 64-bit unsigned integers (i.e. uint64_t).
| [in] | hostu64 | Host byte order integer to be converted into network byte order |
Combine IPv4 SDU and PCI.
This function combines an IPv4 SDU and PCI, the latter in the form of a struct iphdr Linux structure.
The user shall specify a buffer in which the full packet will be put, the IPv4 header, the SDU (byte_t *sdu) and its size in bytes.
| [out] | packet | Packet buffer (should be already allocated) that will contain the full IPv4 packet (SDU+PCI). |
| [in] | header | IPv4 header, as struct iphdr. Should be filled in with IP4headPopulate*() before being passed to this function. |
| [in] | sdu | Buffer containing the SDU. |
| [in] | sdusize | Size, in bytes, of the SDU. |
| void IP4headAddID | ( | struct iphdr * | IPhead, |
| unsigned short | id | ||
| ) |
Add ID to a given IPv4 header.
Since it can be useful, after preparing a certain header, to update quite often the IPv4 ID (depending on the application and on the use case, though), this function allows to perform the aformentioned operation, inserting a given id inside an already populated IPv4 header (IPhead).
0, a call to this function is required to set a specific ID inside an IPv4 header, before sending any packet over raw sockets.| [in,out] | IPhead | IPv4 header structure (struct iphdr) in which the ID field has to be set. |
| [in] | id | 16-bit IP identification value |
| void IP4headAddTotLen | ( | struct iphdr * | IPhead, |
| unsigned short | len | ||
| ) |
Add Total Length to a given IPv4 header.
This function can be used to force a certain length of the entire packet inside the IPv4 header Total Length field.
| [in,out] | IPhead | IPv4 header structure (struct iphdr) in which the Total Length field has to be set. |
| [in] | len | Length, in bytes, to be inserted inside the IPv4 header. |
| rawsockerr_t IP4headPopulate | ( | struct iphdr * | IPhead, |
| char * | devname, | ||
| char * | destIP, | ||
| unsigned char | tos, | ||
| unsigned short | frag_offset, | ||
| unsigned char | ttl, | ||
| unsigned char | protocol, | ||
| unsigned int | flags, | ||
| struct ipaddrs * | addrs | ||
| ) |
Populate IP version 4 header.
This function can be used to populate an Ipv4 header (struct iphdr).
Two variants exist:
The user shall specify an already existing IP header structure to be filled in, the interface name inside devname (for instance wlan0), the destination IP address as a human-readable string (e.g. "192.168.1.180"), the TOS value, the fragment offset value, the TTL, the protocol and the IP header flags (reserved, DF, MF).
If struct ipaddrs *addrs is non-NULL, this function, other than returning possible errors (ERR_IPHEAD_SOCK or ERR_IPHEAD_NOSRCADDR) as return value, fills the structure pointed by addrs with the destination and source IP addresses.
| [in,out] | IPhead | Pointer to the IPv4 header structure, used in raw sockets. |
| [in] | devname | Name of the interface which will be used to send the packet (it is used to automatically retrieve the source IP address) |
| [in] | destIP | Destination IP address, a string containing the address in a human-readable format. |
| [in] | tos | Type of Service (ToS) field. |
| [in] | frag_offset | IPv4 fragment offset field. |
| [in] | ttl | Time To Live (TTL) field. |
| [in] | protocol | Higher layer protocol field. |
| [in] | flags | IPv4 flags (reserved, DF, MF): should be filled in using the macros: FLAG_NOFRAG_MASK, FLAG_RESERVED_MASK and FLAG_MOREFRAG_MASK. |
| [out] | addrs | Pointer to the ipaddrs structure to be filled in with the destination and source IP addresses, or NULL if no structure has to be filled in. |
| rawsockerr_t IP4headPopulateB | ( | struct iphdr * | IPhead, |
| char * | devname, | ||
| unsigned char | tos, | ||
| unsigned short | frag_offset, | ||
| unsigned char | ttl, | ||
| unsigned char | protocol, | ||
| unsigned int | flags, | ||
| struct ipaddrs * | addrs | ||
| ) |
Populate broadcast IP version 4 header (variant of IP4headPopulate())
This function can be used to populate an IPv4 header (struct iphdr) for broadcast transmissions.
The user shall specify an already existing IP header structure to be filled in, the interface name inside devname (for instance wlan0), the TOS value, the fragment offset value, the TTL, the protocol and the IP header flags (reserved, DF, MF).
A broadcast destination IP address is automatically inserted (i.e. 255.255.255.255).
If struct ipaddrs *addrs is non-NULL, this function, other than returning possible errors (ERR_IPHEAD_SOCK or ERR_IPHEAD_NOSRCADDR) as return value, fills the structure pointed by addrs with the destination and source IP addresses.
| [in,out] | IPhead | Pointer to the IPv4 header structure, used in raw sockets. |
| [in] | devname | Name of the interface which will be used to send the packet (it is used to automatically retrieve the source IP address) |
| [in] | tos | Type of Service (ToS) field. |
| [in] | frag_offset | IPv4 fragment offset field. |
| [in] | ttl | Time To Live (TTL) field. |
| [in] | protocol | Higher layer protocol field. |
| [in] | flags | IPv4 flags (reserved, DF, MF): should be filled in using the macros: FLAG_NOFRAG_MASK, FLAG_RESERVED_MASK and FLAG_MOREFRAG_MASK. |
| [out] | addrs | Pointer to the ipaddrs structure to be filled in with the destination and source IP addresses, or NULL if no structure has to be filled in. |
| rawsockerr_t IP4headPopulateS | ( | struct iphdr * | IPhead, |
| char * | devname, | ||
| struct in_addr | destIP, | ||
| unsigned char | tos, | ||
| unsigned short | frag_offset, | ||
| unsigned char | ttl, | ||
| unsigned char | protocol, | ||
| unsigned int | flags, | ||
| struct ipaddrs * | addrs | ||
| ) |
Populate IP version 4 header with struct in_addr addresses (variant of IP4headPopulate())
This function can be used to populate an IPv4 header (struct iphdr).
The user shall specify an already existing IP header structure to be filled in, the interface name inside devname (for instance wlan0), the destination IP address, stored inside a struct in_addr, the TOS value, the fragment offset value, the TTL, the protocol and the IP header flags (reserved, DF, MF).
If struct ipaddrs *addrs is non-NULL, this function, other than returning possible errors (ERR_IPHEAD_SOCK or ERR_IPHEAD_NOSRCADDR) as return value, fills the structure pointed by addrs with the destination and source IP addresses.
| [in,out] | IPhead | Pointer to the IPv4 header structure, used in raw sockets. |
| [in] | devname | Name of the interface which will be used to send the packet (it is used to automatically retrieve the source IP address) |
| [in] | destIP | Destination IP address, stored inside the s_addr field of the specified struct in_addr. |
| [in] | tos | Type of Service (ToS) field. |
| [in] | frag_offset | IPv4 fragment offset field. |
| [in] | ttl | Time To Live (TTL) field. |
| [in] | protocol | Higher layer protocol field. |
| [in] | flags | IPv4 flags (reserved, DF, MF): should be filled in using the macros: FLAG_NOFRAG_MASK, FLAG_RESERVED_MASK and FLAG_MOREFRAG_MASK. |
| [out] | addrs | Pointer to the ipaddrs structure to be filled in with the destination and source IP addresses, or NULL if no structure has to be filled in. |
| unsigned int macAddrTypeGet | ( | macaddr_t | mac | ) |
Get the MAC address type.
Starting from a MAC address type, its type is returned (unicast, multicast, broadcast).
if(macAddrTypeGet(mac)!=MAC_NULL).| [in] | mac | A previously allocated macaddr_t variable/pointer. |
| uint64_t ntoh64 | ( | uint64_t | netu64 | ) |
Convert a 64-bit unsigned value between network and host byte order.
This function works like ntohs() and ntohl(), but with 64-bit unsigned integers (i.e. uint64_t).
| [in] | netu64 | Network byte order integer to be converted into host byte order |
| macaddr_t prepareMacAddrT | ( | ) |
Prepare a macaddr_t variable.
Allocates a six element byte array to store any MAC address, using the macaddr_t custom type.
| void rs_printerror | ( | FILE * | stream, |
| rawsockerr_t | code | ||
| ) |
Print more detailed error messages.
Given a stream (e.g. stdout or stderr) and the error code, as a rawsockerr_t type, more detailed information is printed on the selected stream.
This function essentialy works like perror(), but instead of describing the current value of errno, it describes what is stored inside code.
It can be useful to print any error that may occur after a call to wlanLookup() or other compatible library functions.
| [in] | stream | FILE stream to be used to print the requested error (it can be a file, stdout or stderr). |
| [in] | code | rawsockerr_t integer variable containing the error code (returned by another library function). |
| void test_injectIPCsumError | ( | byte_t * | IPpacket | ) |
Test function: inject a checksum error in an IP packet.
This function can be used as a test function to specifically force a checksum error inside an already formed IP packet.
The pointer to the full IP packet shall be passed (IP header+payload).
| [in] | IPpacket | Pointer to a byte_t buffer containing the full IP packet. |
| void test_injectUDPCsumError | ( | byte_t * | UDPpacket | ) |
Test function: inject a checksum error in an UDP packet.
This function can be used as a test function to specifically force a checksum error inside an already formed UDP packet.
The pointer to the full UDP packet shall be passed (UDP header+payload - not including neither the Ethernet header nor the IPv4 one)
| [in] | UDPpacket | Pointer to a byte_t buffer containing the full UDP packet. |
| size_t UDPencapsulate | ( | byte_t * | packet, |
| struct udphdr * | header, | ||
| byte_t * | data, | ||
| size_t | payloadsize, | ||
| struct ipaddrs | addrs | ||
| ) |
Combine UDP payload and header.
This function combines a UDP payload and header, the latter in the form of a struct udphdr Linux structure.
The user shall specify a buffer in which the full packet will be put, the UDP header, the payload (byte_t *data), its size in bytes and a struct ipaddrs containing the source and destination IP addresses. In particular, the latter is needed to properly compute the UDP checksum.
| [out] | packet | Packet buffer (should be already allocated) that will contain the full IPv4 packet (SDU+PCI). |
| [in] | header | IPv4 header, as struct iphdr. Should be filled in with IP4headPopulate*() before being passed to this function. |
| [in] | data | Buffer containing the data payload. |
| [in] | payloadsize | Size, in bytes, of the payload. |
| byte_t* UDPgetpacketpointers | ( | byte_t * | pktbuf, |
| struct ether_header ** | etherHeader, | ||
| struct iphdr ** | IPheader, | ||
| struct udphdr ** | UDPheader | ||
| ) |
Get pointers to headers and payload in UDP packet buffer.
This function can be used to obtain, given a certain buffer containing a full UDP packet, the pointers to the headers (i.e. to the struct ether_header, struct iphdr and struct udphdr structure respectively) and payload sections
Example of call:
payload=UDPgetpacketpointers(packet,ðerHeader,&IPheader,&udpHeader);
with:
struct ether_header *etherHeader; struct iphdr *IPheader; struct udphdr *udpHeader; byte_t *payload;
If any argument is NULL, no pointer will be returned for that argument.
| [in] | pktbuf | Packet buffer, containing a full valid UDP packet (the checksum can be wrong, "valid" means here "that is really UDP"), including struct ether_header. |
| [out] | etherHeader | This pointer will be written by the function, storing the pointer to the ethernet header inside the pktbuf packet buffer; as it is written by the function, a pointer to the pointer shall be passed to UDPgetpacketpointers() (see the example above) |
| [out] | IPheader | This pointer will be written by the function, storing the pointer to the IPv4 header inside the pktbuf packet buffer. |
| [out] | UDPheader | This pointer will be written by the function, storing the pointer to the UDP header inside the pktbuf packet buffer. |
| unsigned short UDPgetpayloadsize | ( | struct udphdr * | UDPheader | ) |
Get UDP payload size, given a UDP header.
This function can be used to obtain the UDP payload size as unsigned short (int), given a UDP header pointer.
It basically extracts the length field and substracts the standard header length.
| [in] | UDPheader | Pointer to a struct udphdr, containing the UDP header. |
| void UDPheadPopulate | ( | struct udphdr * | UDPhead, |
| unsigned short | sourceport, | ||
| unsigned short | destport | ||
| ) |
Populate UDP header.
This function can be used to populate a UDP header (struct udphdr).
The user shall specify an already existing UDP header structure, the source port and the destination port.
| [in,out] | UDPhead | Pointer to the UDP header structure, used in raw sockets. |
| [in] | sourceport | Source port (16 bit unsigned value) |
| [in] | destport | Destination port (16 bit unsigned value) |
| bool validateEthCsum | ( | byte_t * | packet, |
| csum16_t | csum, | ||
| csum16_t * | combinedcsum, | ||
| csumt_t | type, | ||
| void * | args | ||
| ) |
Validate the checksum of a raw "Ethernet" packet, i.e. of any packet containing a struct ether_header as first bytes.
This function can be used to valide the checksum of any Ethernet raw packet, which can be, for instance, received through a raw socket.
A more detailed description is presented below, in the "Parameters" section.
| [in] | packet | Pointer to the full packet buffer for which the checksum has to be validated. |
| [in] | csum | Checksum value to be checked against the newly computed value, from packet (see also csum16_t) |
| [in] | combinedcsum | This parameter is a pointer to a checksum value. It should be NULL for non combined checksum types, otherwise it should contain the pointer to the value of the second checksum to be checked (the ordering between csum and _*combinedcsum_ is the same as the one in the checksum type constant - for instance: CSUM_UDPIP requires csum to be related to UDP and _*combinedcsum_ to IPv4). If NULL is specified for any combined type, the function will always return false. |
| [in] | type | Checksum protocol (see also csum16_t): various protocols can be specified within the same function, in order to keep it easier to be extended as newer protocols will be implemented inside the library. The supported protocols are defined inside proper "Checksum protocols" constants in rawsock.h, starting with CSUM_ (e.g. CSUM_IP). Both simple protocols and combined ones are supported: in the first case, only the checksum related to the specified protocol is checked (csum16_t csum), in the second case, two checksums, contained inside the same packet, are checked (csum16_t csum and csum16_t *combinedcsum). This can be useful, for instance, to check both the UDP and IP checksums all at once; in case a combined mode is selected _*combinedcsum_ shall be non-NULL, otherwise the function will always return false. |
| [in] | args | Additional protocol-specific arguments: these are typically dependant on the specified protocol. For instance, using IPv4, they are not needed and NULL can be passed; instead, when using UDP (or UDP+IP), they shall contain the pointer to a single value which is the payload length (this may avoid computing it multiple times, outside and inside this function, when a variable containing it is already available). Everytime an additional argument is needed and it is not passed (i.e. NULL args), the function always returns false. |
| rawsockerr_t vifPrinter | ( | FILE * | stream | ) |
Print information about available interfaces.
This function can be used to automatically print some information about the interfaces which are up and available in the system.
The **"Interface internal index"** value can be used as a reference for the index value to be inserted in wlanLookup(), as last argument, when more than one wireless interface is available on the system.
| [out] | stream | File stream to print to (a file, stdout or stderr) |
getifaddrs() | rawsockerr_t wlanLookup | ( | char * | devname, |
| int * | ifindex, | ||
| macaddr_t | mac, | ||
| struct in_addr * | srcIP, | ||
| int | index, | ||
| int | mode | ||
| ) |
Automatically look for available WLAN, non-WLAN or loopback interfaces.
This function can be used to automatically look for available and ready WLAN or non-WLAN (depending on mode) interfaces in the system.
From version 0.2.0 it is also possible to specify WLANLOOKUP_LOOPBACK as index to look for the first available lookback interface, instead of WLAN/non-WLAN ones.
When only one interface is available and 0 is specified as index, that interface name is returned inside devname. Then, if the other three arguments are not NULL, the interface index, the corresponding source MAC address (if available) and the corresponding source IP address (if available) are respectively returned.
If more than one interface is present, the number of available interfaces of the specified type (WLAN/non-WLAN) is returned by the function and index is used to point to a specific interface (for instance index=1 can be used to point to a possible wlan1 interface when mode is WLANLOOKUP_WLAN).
To print the available indeces, the user can use vifPrinter().
| [out] | devname | Name of the WLAN interface. |
| [out] | ifindex | Interface index corresponding to devname (filled in only if non-NULL). |
| [out] | mac | Interface (source) MAC address (filled in only if non-NULL / non-MAC_NULL). |
| [out] | srcIP | Interface (source) IPv4 address (filled in only if non-NULL and returned inside a struct in_addr, which should be available in the calling module). |
| [in] | index | Integer index used to point to a specific interface of the specified type (i.e. using the specified mode), when more than one is available (or equal to WLANLOOKUP_LOOPBACK to look for the first available loopback interface). |
| [in] | mode | Operating mode: WLANLOOKUP_WLAN to look for available WLAN interfaces only, WLANLOOKUP_NONWLAN to look for available non-WLAN/Ethernet interfaces only |
getifaddrs()