NLINLINE(3) | Library Functions Manual | NLINLINE(3) |
nlinline_if_nametoindex, nlinline_linksetupdown, nlinline_ipaddr_add, nlinline_ipaddr_del, nlinline_iproute_add, nlinline_iproute_del, nlinline_iplink_add, nlinline_iplink_del, nlinline_linksetaddr, nlinline_linkgetaddr - configure network interfaces
#include <nlinline.h>
int nlinline_if_nametoindex(const char *ifname);
int nlinline_linksetupdown(unsigned int ifindex, int updown);
int nlinline_ipaddr_add(int family, void *addr, int prefixlen, unsigned int ifindex);
int nlinline_ipaddr_del(int family, void *addr, int prefixlen, unsigned int ifindex);
int nlinline_iproute_add(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex);
int nlinline_iproute_del(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex);
int nlinline_iplink_add(const char *ifname, unsigned int ifindex, const char *type, const char *data);
int nlinline_iplink_del(const char *ifname, unsigned int ifindex);
int nlinline_linksetaddr(unsigned int ifindex, void *macaddr);
int nlinline_linkgetaddr(unsigned int ifindex, void *macaddr);
int nlinline_linksetmtu(unsigned int ifindex, unsigned int mtu);
NLINLINE (netlink inline) is a library of inline functions providing C programmers with very handy functions to configure network stacks. NLINLINE is entirely implemented in a header file, nlinline.h.
IP addresses are void * arguments, any sequence of 4 or 16 bytes (in network byte order) is a legal IPv4 or IPv6 address respectively.
nlinline functions do not add dependencies at run-time. This is useful for security critical applications (like PAM modules) These inline functions use netlink only, they do not depend on the obsolete netdevice (ioctl) API. Only the code of referenced inline functions enters in the object and executable code.
nlinline_if_nametoindex returns the interface index or -1 if an error occurred (in which case, errno is set appropriately)
All the other functions return zero in case of success. On error, -1 is returned, and errno is set appropriately.
(nlinline_iplink_add can return the (positive) ifindex of the newly created link when the argument ifindex is -1 and the stack supports this feature.)
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <nlinline.h> int main(int argc, char *argv[]) {
uint8_t ipv4addr[] = {192,168,2,2};
uint8_t ipv4gw[] = {192,168,2,1};
uint8_t ipv6addr[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x02};
uint8_t ipv6gw[16] = {0x20, 0x01, 0x07, 0x60, [15] = 0x01};
int ifindex = nlinline_if_nametoindex(argv[1]);
if (ifindex > 0)
printf("%d\n", ifindex);
else {
perror("nametoindex");
return 1;
}
if (nlinline_linksetupdown(ifindex, 1) < 0)
perror("link up");
if (nlinline_ipaddr_add(AF_INET, ipv4addr, 24, ifindex) < 0)
perror("addr ipv4");
if (nlinline_iproute_add(AF_INET, NULL, 0, ipv4gw, 0) < 0)
perror("addr ipv6");
if (nlinline_ipaddr_add(AF_INET6, ipv6addr, 64, ifindex) < 0)
perror("route ipv4");
if (nlinline_iproute_add(AF_INET6, NULL, 0, ipv6gw, 0) < 0)
perror("route ipv6");
return 0; }
This program takes the name of an interface from the command line. It turns that interface up and sets the interface IPv4 and IPv6 addresses and default routes.
VirtualSquare. Project leader: Renzo Davoli
December 2020 | VirtualSquare |