Greenbone Vulnerability Management Libraries 22.10.0
networking.c File Reference

Implementation of GVM Networking related API. More...

#include "networking.h"
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

Go to the source code of this file.

Data Structures

struct  route_entry
 

Macros

#define G_LOG_DOMAIN   "libgvm base"
 GLib log domain.
 
#define memdup   g_memdup
 

Typedefs

typedef struct route_entry route_entry_t
 

Functions

int gvm_source_iface_init (const char *iface)
 Initializes the source network interface name and related information.
 
int gvm_source_iface_is_set (void)
 Check if global_source global_source_iface is set.
 
int gvm_source_set_socket (int socket, int port, int family)
 Binds a socket to use the global source address.
 
void gvm_source_addr (void *addr)
 Gives the source IPv4 address.
 
void gvm_source_addr6 (void *addr6)
 Gives the source IPv6 address.
 
void gvm_source_addr_as_addr6 (struct in6_addr *addr6)
 Gives the source IPv4 mapped as an IPv6 address. eg. 192.168.20.10 would map to ::ffff:192.168.20.10.
 
char * gvm_source_addr_str (void)
 Gives the source IPv4 address in string format.
 
char * gvm_source_addr6_str (void)
 Gives the source IPv6 address in string format.
 
void ipv4_as_ipv6 (const struct in_addr *ip4, struct in6_addr *ip6)
 Maps an IPv4 address as an IPv6 address. eg. 192.168.10.20 would map to ::ffff:192.168.10.20.
 
void addr6_to_str (const struct in6_addr *addr6, char *str)
 Stringifies an IP address.
 
char * addr6_as_str (const struct in6_addr *addr6)
 Stringifies an IP address.
 
void sockaddr_as_str (const struct sockaddr_storage *addr, char *str)
 Convert an IP address to string format.
 
GSList * gvm_resolve_list (const char *name)
 Returns a list of addresses that a hostname resolves to.
 
int gvm_resolve (const char *name, void *dst, int family)
 Resolves a hostname to an IPv4 or IPv6 address.
 
int gvm_resolve_as_addr6 (const char *name, struct in6_addr *ip6)
 Resolves a hostname to an IPv4-mapped IPv6 or IPv6 address.
 
int validate_port_range (const char *port_range)
 Validate a port range string.
 
array_tport_range_ranges (const char *port_range)
 Create a range array from a port_range string.
 
int port_in_port_ranges (int pnum, port_protocol_t ptype, array_t *pranges)
 Checks if a port num is in port ranges array.
 
int ipv6_is_enabled (void)
 Checks if IPv6 support is enabled.
 
static gboolean ip_islocalhost (struct sockaddr_storage *storage)
 Determine if IP is localhost.
 
static GSList * get_routes (void)
 Get the entries of /proc/net/route as list of route_entry structs.
 
gchar * gvm_routethrough (struct sockaddr_storage *storage_dest, struct sockaddr_storage *storage_source)
 Get Interface which should be used for routing to destination addr.
 
static int get_connected_udp_sock (struct sockaddr_storage *target_addr)
 Get a connected UDP socket.
 
static int get_sock_addr (int sockfd, struct sockaddr_storage *sock_addr)
 Get address from socket.
 
static char * get_ifname_from_ifaddr (struct sockaddr_storage *target_addr)
 Get iface name of iface matching the given interface address.
 
char * gvm_get_outgoing_iface (struct sockaddr_storage *target_addr)
 Get the outgoing interface name for a given destination addr.
 

Variables

char global_source_iface [IFNAMSIZ] = {'\0'}
 
struct in_addr global_source_addr = {.s_addr = 0}
 
struct in6_addr global_source_addr6 = {.s6_addr32 = {0, 0, 0, 0}}
 

Detailed Description

Implementation of GVM Networking related API.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm base"

GLib log domain.

◆ memdup

#define memdup   g_memdup

Typedef Documentation

◆ route_entry_t

typedef struct route_entry route_entry_t

Function Documentation

◆ addr6_as_str()

char * addr6_as_str ( const struct in6_addr * addr6)

Stringifies an IP address.

Parameters
[in]addr6IP address.
Returns
IP as string. NULL otherwise.

◆ addr6_to_str()

void addr6_to_str ( const struct in6_addr * addr6,
char * str )

Stringifies an IP address.

Parameters
[in]addr6IP address.
[out]strBuffer to output IP.

◆ get_connected_udp_sock()

static int get_connected_udp_sock ( struct sockaddr_storage * target_addr)
static

Get a connected UDP socket.

Parameters
target_addrHolds addr for connect call
Returns
Socket number or -1 on error.

◆ get_ifname_from_ifaddr()

static char * get_ifname_from_ifaddr ( struct sockaddr_storage * target_addr)
static

Get iface name of iface matching the given interface address.

Parameters
[in]target_addrAddress of interface.
Returns
Interface name of matching interface which to be freed by the caller. Null if no interface found or error.

◆ get_routes()

static GSList * get_routes ( void )
static

Get the entries of /proc/net/route as list of route_entry structs.

Returns
GSList of route_entry structs. NULL if no routes found or Error.

◆ get_sock_addr()

static int get_sock_addr ( int sockfd,
struct sockaddr_storage * sock_addr )
static

Get address from socket.

Parameters
[in]sockfdSocket from which to get the address.
[out]sock_addrLocation to write address into.
Returns
0 on success, -1 on error.

◆ gvm_get_outgoing_iface()

char * gvm_get_outgoing_iface ( struct sockaddr_storage * target_addr)

Get the outgoing interface name for a given destination addr.

A UDP socket is connected and its address retrieved. The address is the address of the interface of the outgoing interface. Its is determined by the kernel. We then search the list of interfaces for this address to determine the interface name. This method has the downside that if two interfaces with same addr are UP, a wrong interface might be returned because we can only retrieve the interface addr which was chosen by the kernel and nothing else (like e.g. interface number).

Parameters
[in]target_addrDestination address.
Returns
Name of outgoing interface which has to be freed by caller. NULL if no interface found or Error.

◆ gvm_resolve()

int gvm_resolve ( const char * name,
void * dst,
int family )

Resolves a hostname to an IPv4 or IPv6 address.

Parameters
[in]nameHostname to resolve.
[out]dstBuffer to store resolved address. Size must be at least 4 bytes for AF_INET and 16 bytes for AF_INET6.
[in]familyEither AF_INET or AF_INET6.
Returns
-1 if error, 0 otherwise.

◆ gvm_resolve_as_addr6()

int gvm_resolve_as_addr6 ( const char * name,
struct in6_addr * ip6 )

Resolves a hostname to an IPv4-mapped IPv6 or IPv6 address.

Parameters
[in]nameHostname to resolve.
[out]ip6Buffer to store resolved address.
Returns
-1 if error, 0 otherwise.

◆ gvm_resolve_list()

GSList * gvm_resolve_list ( const char * name)

Returns a list of addresses that a hostname resolves to.

Parameters
[in]nameHostname to resolve.
Returns
List of addresses, NULL otherwise.

◆ gvm_routethrough()

gchar * gvm_routethrough ( struct sockaddr_storage * storage_dest,
struct sockaddr_storage * storage_source )

Get Interface which should be used for routing to destination addr.

This function should be used sparingly as it parses /proc/net/route for every call.

Parameters
[in]storage_destDestination address.
[out]storage_sourceSource address. Is set to either address of the interface we use or global source address if set. Only gets filled if storage_source != NULL.
Returns
Interface name of interface used for routing to destination address. NULL if no interface found or Error.

◆ gvm_source_addr()

void gvm_source_addr ( void * addr)

Gives the source IPv4 address.

Parameters
[out]addrBuffer of at least 4 bytes.

◆ gvm_source_addr6()

void gvm_source_addr6 ( void * addr6)

Gives the source IPv6 address.

Parameters
[out]addr6Buffer of at least 16 bytes.

◆ gvm_source_addr6_str()

char * gvm_source_addr6_str ( void )

Gives the source IPv6 address in string format.

Returns
Source IPv6 string. Free with g_free().

◆ gvm_source_addr_as_addr6()

void gvm_source_addr_as_addr6 ( struct in6_addr * addr6)

Gives the source IPv4 mapped as an IPv6 address. eg. 192.168.20.10 would map to ::ffff:192.168.20.10.

Parameters
[out]addr6Buffer of at least 16 bytes.

◆ gvm_source_addr_str()

char * gvm_source_addr_str ( void )

Gives the source IPv4 address in string format.

Returns
Source IPv4 string. Free with g_free().

◆ gvm_source_iface_init()

int gvm_source_iface_init ( const char * iface)

Initializes the source network interface name and related information.

Parameters
[in]ifaceName of network interface to use as source interface.
Returns
0 if success. If error, return 1 and reset source values to default.

◆ gvm_source_iface_is_set()

int gvm_source_iface_is_set ( void )

Check if global_source global_source_iface is set.

Returns
1 if set, 0 otherwise.

◆ gvm_source_set_socket()

int gvm_source_set_socket ( int socket,
int port,
int family )

Binds a socket to use the global source address.

Parameters
[in]socketSocket to set source address for.
[in]portNetwork port for socket.
[in]familyFamily of socket. AF_INET or AF_INET6.
Returns
0 if success, -1 if error.

◆ ip_islocalhost()

static gboolean ip_islocalhost ( struct sockaddr_storage * storage)
static

Determine if IP is localhost.

Returns
True if IP is localhost, else false.

◆ ipv4_as_ipv6()

void ipv4_as_ipv6 ( const struct in_addr * ip4,
struct in6_addr * ip6 )

Maps an IPv4 address as an IPv6 address. eg. 192.168.10.20 would map to ::ffff:192.168.10.20.

Parameters
[in]ip4IPv4 address to map.
[out]ip6Buffer to store the IPv6 address.

◆ ipv6_is_enabled()

int ipv6_is_enabled ( void )

Checks if IPv6 support is enabled.

Returns
1 if IPv6 is enabled, 0 if disabled.

◆ port_in_port_ranges()

int port_in_port_ranges ( int pnum,
port_protocol_t ptype,
array_t * pranges )

Checks if a port num is in port ranges array.

Parameters
[in]pnumPort number.
[in]ptypePort type.
[in]prangesArray of port ranges.
Returns
1 if port in port ranges, 0 otherwise.

◆ port_range_ranges()

array_t * port_range_ranges ( const char * port_range)

Create a range array from a port_range string.

Parameters
[in]port_rangeValid port_range string.
Returns
Range array or NULL if port_range invalid or NULL.

◆ sockaddr_as_str()

void sockaddr_as_str ( const struct sockaddr_storage * addr,
char * str )

Convert an IP address to string format.

Parameters
[in]addrAddress to convert.
[out]strBuffer of INET6_ADDRSTRLEN size.

◆ validate_port_range()

int validate_port_range ( const char * port_range)

Validate a port range string.

Accepts ranges in form of "103,U:200-1024,3000-4000,T:3-4,U:7".

Parameters
[in]port_rangeA port range.
Returns
0 success, 1 failed.

Variable Documentation

◆ global_source_addr

struct in_addr global_source_addr = {.s_addr = 0}

◆ global_source_addr6

struct in6_addr global_source_addr6 = {.s6_addr32 = {0, 0, 0, 0}}

◆ global_source_iface

char global_source_iface[IFNAMSIZ] = {'\0'}