NLINLINE(3)
π NLINLINE β the nlinline girlies' guide π
okay so bestie, we're in section 3 of the Library Functions Manual and the star of the show is NLINLINE, a whole library of functions that let you configure network interfaces. yes, in C. yes, it slays. π¦
β¨ NAME β¨
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 β they literally configure network interfaces. that's the whole vibe. π
πΈ SYNOPSIS πΈ
first things first, you gotta pull in the header:
#include <nlinline.h>
and then, the girlies themselves:
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);
π DESCRIPTION π
NLINLINE (short for netlink inline, iykyk) is a library of inline functions that hands C programmers some extremely handy little tools for configuring network stacks. and the best part? it's entirely implemented in a header file, nlinline.h. one file. no drama. no cap. π
π§ the functions, one by one
nlinline_if_nametoindexβ bestie, this one just gives you the index of the network interface whose name isifname. that's it, that's the function.nlinline_linksetupdownβ turns the interfaceifindexup (updown == 1) or down (updown == 0). flip the switch, main character energy. π₯nlinline_ipaddr_addβ adds an IP address to the interfaceifindex. it supports IPv4 (family == AF_INET) and IPv6 (family == AF_INET6). both baddies welcome. β¨nlinline_ipaddr_delβ removes the IP address from the interfaceifindex. same deal, it supports IPv4 (family == AF_INET) and IPv6 (family == AF_INET6). we love a symmetric API. πnlinline_iproute_addβ adds a static route to thedst_addr/dst_prefixlennetwork through the gatewaygw_addr. ifdst_addr == NULLit adds a default route. and heads up:ifindexmust be specified whengw_addris an IPv6 link local address. periodt.nlinline_iproute_delβ removes the static route to thedst_addr/dst_prefixlennetwork through the gatewaygw_addr. bye girl πnlinline_iplink_addβ adds a new link of typetype, namedifname. the value ofdatadepends on the type of link and can beNULL. a default interface name is assigned ifname == NULL. and the link gets created with a given index whenifindexis positive. slay.nlinline_iplink_delβ removes a link. the link to be deleted can be identified by its name (ifname) or by its index (ifindex). eitherifindexcan be zero orifnamecan beNULL. you can even use bothifindexandifnameto identify the link β but an error may occur if the parameters are inconsistent. so like, don't be messy. πnlinline_linksetaddrβ this function sets the mac address of the interfaceifindex. (yes, the original really does say "This functions". we love her.)nlinline_linkgetaddrβ this function gets the mac address of the interfaceifindex. get you a function that can do both. πnlinline_linksetmtuβ this function sets the MTU (Maximum Transfer Unit) of the interfaceifindex. big packets, bigger dreams. π
π«Ά the important lore
IP addresses are void * arguments β any sequence of 4 or 16 bytes (in network byte order) is a legal IPv4 or IPv6 address respectively. that's the rule, no exceptions, bestie.
nlinline functions do not add dependencies at run-time. this is highkey useful for security critical applications (like PAM modules). these inline functions use netlink only β they do not depend on the obsolete netdevice (ioctl) API. and only the code of referenced inline functions enters into the object and executable code. so efficient. so clean. we're obsessed. π
β RETURN VALUE β
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.
and a lil plot twist for you: (nlinline_iplink_addcan return the (positive) ifindex of the newly created link when the argumentifindexis -1 and the stack supports this feature.) π
π EXAMPLE π
here's a whole little program, bestie. look at her go:
#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;
}
so what does she do? 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. iconic behaviour, honestly. π β¨
π AUTHOR π
VirtualSquare. Project leader: Renzo Davoli. legends only. π
and that's the whole page, bestie β brought to you by VirtualSquare, dated December 2020, living in section 3 of the manual. go configure some network stacks, you've got this. mwah π«Άπ
π¦ where this page comes from
- debian bookworm: nlinline-dev 0.2.2-1 Β· LGPL-2.1-or-later
- debian trixie: nlinline-dev 0.2.2-1 Β· LGPL-2.1-or-later
- ubuntu jammy: nlinline-dev 0.2.2-1 Β· LGPL-2.1-or-later
- ubuntu noble: nlinline-dev 0.2.2-1 Β· LGPL-2.1-or-later
- ubuntu resolute: nlinline-dev 0.2.2-1build1 Β· LGPL-2.1-or-later
- ubuntu stonking: nlinline-dev 0.2.2-1build1 Β· LGPL-2.1-or-later
- Copyright (C) 2019 VirtualSquare. Project Leader: Renzo Davoli
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms