EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
Functions
net.c File Reference

File containing the implementation of the network utilities. More...

#include <stdbool.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include "allocs.h"
#include "net.h"
#include "os.h"
Include dependency graph for net.c:

Functions

bool validate_ipv4_string (const char *ip)
 Checks whether a string denotes a IPv4 address. More...
 
int ip_2_nbo (const char *ip, const char *subnet_mask, in_addr_t *addr)
 IP string to struct in_addr_t converter. More...
 
int ip4_2_buf (const char *ip, uint8_t buf[static IP_ALEN])
 IP string to buffer. More...
 
const char * bit32_2_ip (uint32_t addr, char ip[static OS_INET_ADDRSTRLEN])
 Convert a 32 bit number IP to an IP string. More...
 
const char * inaddr4_2_ip (const struct in_addr *addr, char ip[static OS_INET_ADDRSTRLEN])
 Convert the in_addr encoded IP4 address to an IP string. More...
 
const char * inaddr6_2_ip (const struct in6_addr *addr, char ip[static OS_INET6_ADDRSTRLEN])
 Convert the in6_addr encoded IP6 address to an IP string. More...
 
uint8_t get_short_subnet (const char *subnet_mask)
 Convert from a string subnet mask to a short integer version. More...
 
int get_ip_host (const char *ip, const char *subnet_mask, uint32_t *host)
 Get the host identifier from an IP address string. More...
 
int disable_pmtu_discovery (int sock)
 Disable the PMTU discovery for sockets. More...
 
int edge_hwaddr_aton2 (const char *txt, uint8_t *addr)
 Convert ASCII string to MAC address (in any known format) More...
 

Detailed Description

File containing the implementation of the network utilities.

Author
Alexandru Mereacre
Date
2022

Function Documentation

◆ bit32_2_ip()

const char* bit32_2_ip ( uint32_t  addr,
char  ip[static OS_INET_ADDRSTRLEN] 
)

Convert a 32 bit number IP to an IP string.

Parameters
addrThe IP in 32 bit format
[out]ipThe output buffer to store the IP. Must be at least OS_INET_ADDRSTRLEN chars long.
Returns
Pointer to the returned IP (same as ip on success)
Return values
NULLon error (see errno).

◆ disable_pmtu_discovery()

int disable_pmtu_discovery ( int  sock)

Disable the PMTU discovery for sockets.

Parameters
[in]sockThe socket descriptor
Returns
0 on success, -1 on failure

◆ edge_hwaddr_aton2()

int edge_hwaddr_aton2 ( const char *  txt,
uint8_t *  addr 
)

Convert ASCII string to MAC address (in any known format)

Authors
Jouni Malinen j@w1..nosp@m.fi and wpa_supplicant contributors
Date
2003-2019
See also
https://w1.fi/wpa_supplicant/devel/common_8h.html#ab7ec8839c8e817e241c52ad7a0299be3
Parameters
[in]txtMAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
[out]addrBuffer for the MAC address (ETHER_ADDR_LEN = 6 bytes)
Returns
int Characters used (> 0) on success, -1 on failure

◆ get_ip_host()

int get_ip_host ( const char *  ip,
const char *  subnet_mask,
uint32_t *  host 
)

Get the host identifier from an IP address string.

Parameters
ipThe IP address string
subnet_maskThe subnet mask string
[out]hostThe returned host indentifier
Returns
0 on success, -1 on failure

◆ get_short_subnet()

uint8_t get_short_subnet ( const char *  subnet_mask)

Convert from a string subnet mask to a short integer version.

Parameters
subnet_maskThe subnet mask string
Returns
The short integer version subnet mask

◆ inaddr4_2_ip()

const char* inaddr4_2_ip ( const struct in_addr *  addr,
char  ip[static OS_INET_ADDRSTRLEN] 
)

Convert the in_addr encoded IP4 address to an IP string.

Parameters
addrThe in_addr encoded IP
[out]ipThe output buffer to store the IP. Must be at least OS_INET_ADDRSTRLEN chars long.
Returns
Pointer to the returned IP (same as ip on success)
Return values
NULLon error (see errno).

◆ inaddr6_2_ip()

const char* inaddr6_2_ip ( const struct in6_addr *  addr,
char  ip[static OS_INET6_ADDRSTRLEN] 
)

Convert the in6_addr encoded IP6 address to an IP string.

Parameters
addrThe in6_addr encoded IP
[out]ipThe output buffer to store the IP. Must be at least OS_INET6_ADDRSTRLEN chars long.
Returns
Pointer to the returned IP (same as ip on success)
Return values
NULLon error (see errno).

◆ ip4_2_buf()

int ip4_2_buf ( const char *  ip,
uint8_t  buf[static IP_ALEN] 
)

IP string to buffer.

Parameters
ipThe IP address string
[out]bufThe output buffer of size IP_ALEN
Returns
0 on success, -1 on failure

◆ ip_2_nbo()

int ip_2_nbo ( const char *  ip,
const char *  subnetMask,
in_addr_t *  addr 
)

IP string to struct in_addr_t converter.

Parameters
ipThe IP address string
subnetMaskThe IP address subnet mask
[out]addrThe output struct in_addr_t value
Returns
0 on success, -1 on failure

◆ validate_ipv4_string()

bool validate_ipv4_string ( const char *  ip)

Checks whether a string denotes a IPv4 address.

Parameters
ipThe IP in fromat x.y.z.q
Returns
true if the string is an IP, false otherwise