|
Greenbone Vulnerability Management Libraries 22.32.0
|
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_t * | port_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. | |
| static void | free_routes (GSList *routes) |
| Free the list of routes. | |
| 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}} |
Implementation of GVM Networking related API.
Definition in file networking.c.
| #define G_LOG_DOMAIN "libgvm base" |
GLib log domain.
Definition at line 35 of file networking.c.
| #define memdup g_memdup |
Definition at line 40 of file networking.c.
Referenced by gvm_resolve_list().
| typedef struct route_entry route_entry_t |
Definition at line 862 of file networking.c.
| char * addr6_as_str | ( | const struct in6_addr * | addr6 | ) |
Stringifies an IP address.
| [in] | addr6 | IP address. |
Definition at line 279 of file networking.c.
References addr6_to_str().

| void addr6_to_str | ( | const struct in6_addr * | addr6, |
| char * | str ) |
Stringifies an IP address.
| [in] | addr6 | IP address. |
| [out] | str | Buffer to output IP. |
Definition at line 261 of file networking.c.
Referenced by addr6_as_str(), and host_name_verify().

|
static |
Free the list of routes.
| GSList | of route_entry structs, or NULL. |
Definition at line 987 of file networking.c.
Referenced by Ensure(), and gvm_routethrough().

|
static |
Get a connected UDP socket.
| target_addr | Holds addr for connect call |
Definition at line 1140 of file networking.c.
Referenced by gvm_get_outgoing_iface().

|
static |
Get iface name of iface matching the given interface address.
| [in] | target_addr | Address of interface. |
Definition at line 1229 of file networking.c.
Referenced by gvm_get_outgoing_iface().

|
static |
Get the entries of /proc/net/route as list of route_entry structs.
Definition at line 878 of file networking.c.
References route_entry::dest, route_entry::interface, and route_entry::mask.
Referenced by Ensure(), gvm_routethough(), and gvm_routethrough().

|
static |
Get address from socket.
| [in] | sockfd | Socket from which to get the address. |
| [out] | sock_addr | Location to write address into. |
Definition at line 1193 of file networking.c.
Referenced by 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).
| [in] | target_addr | Destination address. |
Definition at line 1298 of file networking.c.
References get_connected_udp_sock(), get_ifname_from_ifaddr(), and get_sock_addr().

| int gvm_resolve | ( | const char * | name, |
| void * | dst, | ||
| int | family ) |
Resolves a hostname to an IPv4 or IPv6 address.
| [in] | name | Hostname to resolve. |
| [out] | dst | Buffer to store resolved address. Size must be at least 4 bytes for AF_INET and 16 bytes for AF_INET6. |
| [in] | family | Either AF_INET or AF_INET6. |
Definition at line 389 of file networking.c.
References ipv4_as_ipv6().
Referenced by gvm_host_resolve(), and 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.
| [in] | name | Hostname to resolve. |
| [out] | ip6 | Buffer to store resolved address. |
Definition at line 443 of file networking.c.
References gvm_resolve().

| GSList * gvm_resolve_list | ( | const char * | name | ) |
Returns a list of addresses that a hostname resolves to.
| [in] | name | Hostname to resolve. |
Definition at line 339 of file networking.c.
References ipv4_as_ipv6(), and memdup.
Referenced by gvm_hosts_resolve(), and host_name_verify().


| 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.
| [in] | storage_dest | Destination address. |
| [out] | storage_source | Source address. Is set to either address of the interface we use or global source address if set. Only gets filled if storage_source != NULL. |
Definition at line 1018 of file networking.c.
References free_routes(), get_routes(), gvm_source_addr(), and ip_islocalhost().
Referenced by Ensure().


| void gvm_source_addr | ( | void * | addr | ) |
Gives the source IPv4 address.
| [out] | addr | Buffer of at least 4 bytes. |
Definition at line 174 of file networking.c.
References global_source_addr.
Referenced by Ensure(), gvm_routethrough(), gvm_source_set_socket(), and main().

| void gvm_source_addr6 | ( | void * | addr6 | ) |
Gives the source IPv6 address.
| [out] | addr6 | Buffer of at least 16 bytes. |
Definition at line 186 of file networking.c.
References global_source_addr6.
Referenced by gvm_source_set_socket().

| char * gvm_source_addr6_str | ( | void | ) |
Gives the source IPv6 address in string format.
Definition at line 225 of file networking.c.
References global_source_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.
| [out] | addr6 | Buffer of at least 16 bytes. |
Definition at line 199 of file networking.c.
References global_source_addr, and ipv4_as_ipv6().

| char * gvm_source_addr_str | ( | void | ) |
Gives the source IPv4 address in string format.
Definition at line 211 of file networking.c.
References global_source_addr.
| int gvm_source_iface_init | ( | const char * | iface | ) |
Initializes the source network interface name and related information.
| [in] | iface | Name of network interface to use as source interface. |
Definition at line 64 of file networking.c.
References global_source_addr, global_source_addr6, and global_source_iface.
Referenced by Ensure().

| int gvm_source_iface_is_set | ( | void | ) |
Check if global_source global_source_iface is set.
Definition at line 121 of file networking.c.
References global_source_iface.
| int gvm_source_set_socket | ( | int | socket, |
| int | port, | ||
| int | family ) |
Binds a socket to use the global source address.
| [in] | socket | Socket to set source address for. |
| [in] | port | Network port for socket. |
| [in] | family | Family of socket. AF_INET or AF_INET6. |
Definition at line 136 of file networking.c.
References gvm_source_addr(), and gvm_source_addr6().

|
static |
Determine if IP is localhost.
Definition at line 775 of file networking.c.
Referenced by Ensure(), gvm_routethough(), and gvm_routethrough().

| 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.
| [in] | ip4 | IPv4 address to map. |
| [out] | ip6 | Buffer to store the IPv6 address. |
Definition at line 243 of file networking.c.
Referenced by gvm_host_get_addr6(), gvm_resolve(), gvm_resolve_list(), and gvm_source_addr_as_addr6().

| int ipv6_is_enabled | ( | void | ) |
Checks if IPv6 support is enabled.
Definition at line 752 of file networking.c.
| int port_in_port_ranges | ( | int | pnum, |
| port_protocol_t | ptype, | ||
| array_t * | pranges ) |
Checks if a port num is in port ranges array.
| [in] | pnum | Port number. |
| [in] | ptype | Port type. |
| [in] | pranges | Array of port ranges. |
Definition at line 728 of file networking.c.
References range::start, and range::type.
Referenced by Ensure(), and main().

| array_t * port_range_ranges | ( | const char * | port_range | ) |
Create a range array from a port_range string.
| [in] | port_range | Valid port_range string. |
Definition at line 601 of file networking.c.
References array_add(), range::end, range::exclude, make_array(), PORT_PROTOCOL_TCP, PORT_PROTOCOL_UDP, range::start, range::type, and validate_port_range().
Referenced by Ensure(), Ensure(), and main().


| void sockaddr_as_str | ( | const struct sockaddr_storage * | addr, |
| char * | str ) |
Convert an IP address to string format.
| [in] | addr | Address to convert. |
| [out] | str | Buffer of INET6_ADDRSTRLEN size. |
Definition at line 298 of file networking.c.
| 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".
| [in] | port_range | A port range. |
Definition at line 460 of file networking.c.
Referenced by Ensure(), Ensure(), Ensure(), main(), and port_range_ranges().

| struct in_addr global_source_addr = {.s_addr = 0} |
Definition at line 49 of file networking.c.
Referenced by gvm_source_addr(), gvm_source_addr_as_addr6(), gvm_source_addr_str(), and gvm_source_iface_init().
| struct in6_addr global_source_addr6 = {.s6_addr32 = {0, 0, 0, 0}} |
Definition at line 52 of file networking.c.
Referenced by gvm_source_addr6(), gvm_source_addr6_str(), and gvm_source_iface_init().
| char global_source_iface[IFNAMSIZ] = {'\0'} |
Definition at line 46 of file networking.c.
Referenced by gvm_source_iface_init(), and gvm_source_iface_is_set().