The API is defined in the header file sysroot-*-kos/include/kos_net.h from the KasperskyOS SDK.
The API lets you do the following:
Information about API functions is provided in the table below.
To get a list of network interfaces that are available in the system, call the get_ifaces_addrs() function. This function uses the pif parameter to return a linked list of ifaddrs structures, each of which contains the following data on the network interface: name, flags, IP address, and subnet mask. The ifaddrs structure is defined in the header file sysroot-*-kos/include/ifaddrs.h.
To send information about available network interfaces to standard output, use the list_network_ifaces() function. This function does not have any parameters. The standard output displays the following data about each network interface: name, type, activity status and address of the network interface with its subnet mask.
Example output of the list_network_ifaces() function:
Name Type Running Address/mask
lo0 inet Yes ::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
lo0 inet Yes fe80:1::1/ffff:ffff:ffff:ffff::
lo0 inet Yes 127.0.0.1/255.0.0.0
en0 mac Yes 52:58:00:12:34:bb
en0 inet Yes 10.0.2.15/255.255.255.
en0 inet Yes fe80:2::5058:ff:fe12:34bb/ffff:ffff:ffff:ffff::
To convert the textual representation of an IPv6 address into binary format, use the ip6_parse_addr() function. This function uses the ip_str input parameter to receive the textual representation of an IPv6 address in accordance with paragraphs 2.2 and 2.3 of the RFC 4291 specification in the following format:
<ipv6_address>[/<prefix_size>]
The function uses the dst_mask output parameter to return the pointer to the sockaddr_in6 structure, which contains the prefix mask in the sin6_addr field. The function uses the dst_ip output parameter to return the pointer to the other sockaddr_in6 structure, which contains the IPv6 address in the sin6_addr field. The sockaddr_in6 structure is defined in the header file sysroot-*-kos/include/strict/posix/netinet/in.6.
Use the wait_for_iface() function to wait for the appearance of a network interface with the necessary parameters in the list of available network interfaces. This function uses the name input parameter to receive the name of the network interface for which the function is waiting. If this parameter is not specified (NULL), the function will wait for the appearance of any network interface except those whose names begin with lo*. This function uses the wait_flags input parameter to receive the flags that determine the wait parameters – availability of IP addresses and default gateways. These flags can be combined. The timeout for waiting for the necessary network interface (in seconds) is defined in the timeout parameter.
To configure the available network interfaces using IPv4 and IPv6 addressing, the API provides the following two functions: configure_net_iface() and configure_net_iface6(). To simplify configuration of network interfaces, there are macros that you can use as input parameters for these functions (see the table below).
Macros of the configure_net_iface() and configure_net_iface6() functions
Macro |
Description |
Default value |
Use in |
Use in |
|---|---|---|---|---|
|
Name of the network interface used for the connection |
|
|
|
|
IP address assigned to the network interface |
|
|
|
|
Subnet mask |
|
|
|
|
Default gateway |
|
|
|
|
Maximum size of a transmitted packet, in bytes (Maximum Transmission Unit, MTU) |
|
|
|
|
Timeout (in seconds) |
|
|
|
Information about API functions
kos_net.h functions
Function |
Information about the function |
|---|---|
|
Purpose Gets a list of network interfaces available in the system. Parameters
Returned values Returns |
|
Purpose Configures a network interface using IPv4 addressing. The Parameters
Returned values Returns |
|
Purpose Converts a textual representation of an IPv6 address into binary format, and (optionally) generates a prefix mask based on the specified prefix size. Parameters [in]
Returned values Returns |
|
Purpose Configures a network interface using IPv6 addressing. Parameters
Returned values Returns |
|
Purpose Gets a list of network interfaces that are available in the system and sends data on each network interface to standard output. Parameters N/A Returned values Returns |
|
Purpose Waits for the appearance of an available network interface that meets the requirements defined by the wait flags. Parameters
Returned values Returns |
Example use of API functions:
#include <kos_net.h>
int main(int argc, char *argv[])
{
// Wait 60 seconds for the network interface named en0 to appear in the list of available network interfaces
if (!wait_for_iface(DEFAULT_INTERFACE, IWF_EXISTS, DEFAULT_TIMEOUT))
{
perror("Network interface does not exist");
return EXIT_FAILURE;
}en0
// Configure network interface en0 with the default values
if (!configure_net_iface(DEFAULT_INTERFACE, DEFAULT_ADDR, DEFAULT_MASK, DEFAULT_GATEWAY, DEFAULT_MTU))
{
perror("Can't configure network");
return EXIT_FAILURE;
}
}
Page top