[ English | 简体中文 ]
Network Utility Library (netlib) API
The openvela network utility library (netlib_*) provides a series of helper functions that simplify BSD socket operations, covering IPv4/IPv6 address management, routing, ARP, MAC addresses, MTU, firewall (iptables/ip6tables), network connectivity checks, and more.
Header: #include <netutils/netlib.h>
openvela Implementation Notes
- Purpose: Convenience wrappers on top of the BSD socket API, hiding low-level details such as
ioctl+SIOCGIF* - Coverage:
- IPv4/IPv6 addresses, gateways, subnet masks, DNS, routes
- MAC address read/write, interface up/down, MTU setting
- VLAN management, ARP table operations
- iptables/ip6tables operations
- Network connectivity checks (
ping/ HTTP / interface reachability) - URL parsing utilities
- Configuration dependencies: Requires
CONFIG_NETUTILS_NETLIBto be enabled; some sub-interfaces additionally require the corresponding module configuration (such asCONFIG_NET_ARP,CONFIG_NET_IPv6, etc.) - Error handling: Most interfaces return
0orOKon success, orERROR(-1) witherrnoset on failure
Network Utility Library
Header: #include <netutils/netlib.h>
netlib provides network configuration utility functions, including interface address setting, route management, ARP operations, and more.
IPv4 Address Management
netlib_get_ipv4addr
int netlib_get_ipv4addr(const char *ifname, struct in_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrUsed to store the IP address.
netlib_set_ipv4addr
int netlib_set_ipv4addr(const char *ifname, const struct in_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrAddress to set.
netlib_set_dripv4addr
int netlib_set_dripv4addr(const char *ifname, const struct in_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrAddress to set.
netlib_get_dripv4addr
int netlib_get_dripv4addr(const char *ifname, struct in_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrUsed to store the default route address.
netlib_set_ipv4netmask
int netlib_set_ipv4netmask(const char *ifname, const struct in_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrAddress to set.
netlib_get_ipv4netmask
int netlib_get_ipv4netmask(const char *ifname, struct in_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrUsed to store the subnet mask.
netlib_ipv4adaptor
int netlib_ipv4adaptor(in_addr_t destipaddr, in_addr_t *srcipaddr);
Parameters:
destipaddrTarget IPv4 address.srcipaddrUsed to store the adapter address.
netlib_read_ipv4route
ssize_t netlib_read_ipv4route(FILE *stream, struct netlib_ipv4_route_s *route);
Parameters:
fdFile descriptor for the procfs IPv4 routing table.routeUsed to store the next routing entry.
netlib_ipv4router
int netlib_ipv4router(const struct in_addr *destipaddr, struct in_addr *router);
Parameters:
destipaddrTarget IP address.routerUsed to store the IP address of the gateway router.
netlib_obtain_ipv4addr
int netlib_obtain_ipv4addr(const char *ifname);
Parameters:
ifnameNetwork interface name.
netlib_set_ipv4dnsaddr
int netlib_set_ipv4dnsaddr(const struct in_addr *inaddr);
Parameters:
inaddrAddress to set.
IPv6 Address Management
netlib_add_ipv6addr
int netlib_add_ipv6addr(const char *ifname, const struct in6_addr *addr, uint8_t preflen);
Parameters:
ifnameNetwork interface name.ipaddrAddress to add.preflenPrefix length (in bits).
netlib_del_ipv6addr
int netlib_del_ipv6addr(const char *ifname, const struct in6_addr *addr, uint8_t preflen);
Parameters:
ifnameNetwork interface name.ipaddrAddress to delete.preflenPrefix length (in bits).
netlib_get_ipv6addr
int netlib_get_ipv6addr(const char *ifname, struct in6_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrUsed to store the IP address.
netlib_set_ipv6addr
int netlib_set_ipv6addr(const char *ifname, const struct in6_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrAddress to set.
netlib_set_dripv6addr
int netlib_set_dripv6addr(const char *ifname, const struct in6_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrAddress to set.
netlib_set_ipv6netmask
int netlib_set_ipv6netmask(const char *ifname, const struct in6_addr *addr);
Parameters:
ifnameNetwork interface name.ipaddrAddress to set.
netlib_ipv6adaptor
int netlib_ipv6adaptor(const struct in6_addr *destipaddr, struct in6_addr *srcipaddr);
Parameters:
destipaddrTarget IP address.srcipaddrUsed to store the adapter address.
netlib_ipv6netmask2prefix
uint8_t netlib_ipv6netmask2prefix(const uint16_t *mask);
Parameters:
maskSubnet mask.
netlib_prefix2ipv6netmask
void netlib_prefix2ipv6netmask(uint8_t preflen, struct in6_addr *netmask);
Parameters:
preflenPrefix length (in bits).netmaskUsed to store the subnet mask.
netlib_read_ipv6route
ssize_t netlib_read_ipv6route(FILE *stream, struct netlib_ipv6_route_s *route);
Parameters:
fdRouting entry.routeUsed to store the next routing entry.
netlib_ipv6router
int netlib_ipv6router(const struct in6_addr *destipaddr, struct in6_addr *router);
Parameters:
destipaddrTarget IP address.routerUsed to store the IP address of the gateway router.
netlib_obtain_ipv6addr
int netlib_obtain_ipv6addr(const char *ifname);
Parameters:
ifnameNetwork interface name.
netlib_set_ipv6dnsaddr
int netlib_set_ipv6dnsaddr(const struct in6_addr *inaddr);
Parameters:
inaddrAddress to set.
Interface Management
netlib_setmacaddr
int netlib_setmacaddr(const char *ifname, const uint8_t *macaddr);
Parameters:
ifnameNetwork interface name.macaddrMAC address.
netlib_getmacaddr
int netlib_getmacaddr(const char *ifname, uint8_t *macaddr);
Parameters:
ifnameNetwork interface name.macaddrUsed to store the MAC address.
netlib_getessid
int netlib_getessid(const char *ifname, char *essid, size_t idlen);
Parameters:
ifnameNetwork interface name.essidUsed to store the result.idlenESSID buffer size.
netlib_setessid
int netlib_setessid(const char *ifname, const char *essid);
Parameters:
ifnameNetwork interface name.essidESSID (network name).
netlib_getifstatus
int netlib_getifstatus(const char *ifname, uint8_t *flags);
Parameters:
ifnameNetwork interface name.flagsInterface flags.
netlib_ifup
int netlib_ifup(const char *ifname);
Parameters:
ifnameNetwork interface name.
netlib_ifdown
int netlib_ifdown(const char *ifname);
Parameters:
ifnameNetwork interface name.
netlib_set_mtu
int netlib_set_mtu(const char *ifname, int mtu);
Parameters:
ifnameNetwork interface name.mtuMaximum Transmission Unit (MTU).
Returns:
:
netlib_getifstatistics
int netlib_getifstatistics(const char *ifname, struct netdev_statistics_s *stat);
Parameters:
ifnameNetwork interface name.statUsed to store device statistics.
netlib_check_ifconflict
int netlib_check_ifconflict(const char *ifname);
Parameters:
ifnameNetwork interface name.
Route Management
netlib_get_route
ssize_t netlib_get_route(struct rtentry *rtelist, unsigned int nentries, sa_family_t family);
Parameters:
rtelistUsed to store the device list.nentriesArray capacity (entry count).familyAddress family. See AF_* definitions in.
ARP Management
netlib_del_arpmapping
int netlib_del_arpmapping(const struct sockaddr_in *inaddr, const char *ifname);
Parameters:
inaddrIPv4 address.ifnameNetwork interface name.
netlib_get_arpmapping
int netlib_get_arpmapping(const struct sockaddr_in *inaddr, uint8_t *macaddr, const char *ifname);
Parameters:
inaddrIPv4 address.macaddrUsed to store the corresponding Ethernet MAC address.ifnameNetwork interface name.
netlib_set_arpmapping
int netlib_set_arpmapping(const struct sockaddr_in *inaddr, const uint8_t *macaddr, const char *ifname);
Parameters:
inaddrIPv4 address.macaddrMAC address.ifnameNetwork interface name.
netlib_get_arptable
ssize_t netlib_get_arptable(struct arpreq *arptab, unsigned int nentries);
Parameters:
arptabUsed to store a copy of the ARP table.nentriesArray capacity (entry count).
netlib_ifarp
int netlib_ifarp(const char *ifname);
Parameters:
ifnameNetwork interface name.
netlib_ifnoarp
int netlib_ifnoarp(const char *ifname);
Parameters:
ifnameNetwork interface name.
DNS Management
netlib_clear_dnsaddr
void netlib_clear_dnsaddr(void);
VLAN Management
netlib_add_vlan
int netlib_add_vlan(const char *ifname, int vlanid, int prio);
Parameters:
ifnameNetwork interface name.vlanidVLAN identifier.prioDefault VLAN priority (PCP).
netlib_del_vlan
int netlib_del_vlan(const char *vlanif);
iptables
netlib_ipt_commit
int netlib_ipt_commit(const struct ipt_replace *repl);
Parameters:
replConfiguration to commit.
netlib_ipt_flush
int netlib_ipt_flush(const char *table, enum nf_inet_hooks hook);
Parameters:
tableTable name.hookHook point.
netlib_ipt_policy
int netlib_ipt_policy(const char *table, enum nf_inet_hooks hook, int verdict);
Parameters:
tablePolicy.hookHook point.verdictVerdict value.
netlib_ipt_append
int netlib_ipt_append(struct ipt_replace **repl, const struct ipt_entry *entry, enum nf_inet_hooks hook);
Parameters:
replConfiguration to commit.entryRule entry to append.hookHook point.
netlib_ipt_insert
int netlib_ipt_insert(struct ipt_replace **repl, const struct ipt_entry *entry, enum nf_inet_hooks hook, int rulenum);
Parameters:
replConfiguration to commit.entryRule entry to insert.hookHook point.rulenumRule number.
netlib_ipt_delete
int netlib_ipt_delete(struct ipt_replace *repl, const struct ipt_entry *entry, enum nf_inet_hooks hook, int rulenum);
Parameters:
replConfiguration to commit.entryRule entry to delete.hookHook point.rulenumRule number.
netlib_ipt_fillifname
int netlib_ipt_fillifname(struct ipt_entry *entry, const char *inifname, const char *outifname);
Parameters:
entryRule entry to fill.inifnameInput device name;NULLmeans unchanged.outifnameOutput device name;NULLmeans unchanged.
netlib_ip6t_commit
int netlib_ip6t_commit(const struct ip6t_replace *repl);
Parameters:
replConfiguration to commit.
netlib_ip6t_flush
int netlib_ip6t_flush(const char *table, enum nf_inet_hooks hook);
Parameters:
tableTable name.hookHook point.
netlib_ip6t_policy
int netlib_ip6t_policy(const char *table, enum nf_inet_hooks hook, int verdict);
Parameters:
tablePolicy.hookHook point.verdictVerdict value.
netlib_ip6t_append
int netlib_ip6t_append(struct ip6t_replace **repl, const struct ip6t_entry *entry, enum nf_inet_hooks hook);
Parameters:
replConfiguration to commit.entryRule entry to append.hookHook point.
netlib_ip6t_insert
int netlib_ip6t_insert(struct ip6t_replace **repl, const struct ip6t_entry *entry, enum nf_inet_hooks hook, int rulenum);
Parameters:
replConfiguration to commit.entryRule entry to insert.hookHook point.rulenumRule number.
netlib_ip6t_delete
int netlib_ip6t_delete(struct ip6t_replace *repl, const struct ip6t_entry *entry, enum nf_inet_hooks hook, int rulenum);
Parameters:
replConfiguration to commit.entryRule entry to delete.hookHook point.rulenumRule number.
netlib_ip6t_fillifname
int netlib_ip6t_fillifname(struct ip6t_entry *entry, const char *inifname, const char *outifname);
Parameters:
entryRule entry to fill.inifnameInput device name;NULLmeans unchanged.outifnameOutput device name;NULLmeans unchanged.
Connectivity Checks
netlib_check_ipconnectivity
int netlib_check_ipconnectivity(const char *ip, int timeout, int retry);
Parameters:
ipIPv4 address to check.timeoutTimeout.retryRetry count.
netlib_check_ifconnectivity
int netlib_check_ifconnectivity(const char *ifname, int timeout, int retry);
Parameters:
ifnameNetwork interface name.timeoutTimeout.retryRetry count.
URL Parsing
netlib_parsehttpurl
int netlib_parsehttpurl(const char *url, uint16_t *port, char *hostname, int hostlen, char *filename, int namelen);
Parameters:
urlHTTP-related parameter.portPointer to auint16_tused to store the parsed port number.hostnameBuffer used to store the result.hostlenBuffer size.filenameBuffer used to store the result.namelenBuffer size.
netlib_parseurl
int netlib_parseurl(const char *str, struct url_s *url);
netlib_check_httpconnectivity
int netlib_check_httpconnectivity(const char *host, const char *getmsg, int port, int expect_code);
Parameters:
hostRemote host address.getmsgHTTP-related parameter.portPort number.expect_codeHTTP-related parameter.
Others
netlib_get_devices
ssize_t netlib_get_devices(struct netlib_device_s *devlist, unsigned int nentries, sa_family_t family);
Parameters:
devlistUsed to store the device list.nentriesArray capacity (entry count).familyAddress family. See AF_* definitions in.
netlib_seteaddr
int netlib_seteaddr(const char *ifname, const uint8_t *eaddr);
Parameters:
ifnameNetwork interface name.eaddrNew address.
netlib_getpanid
int netlib_getpanid(const char *ifname, uint8_t *panid);
Parameters:
ifnameNetwork interface name.panidUsed to store the current PAN ID.
netlib_getproperties
int netlib_getproperties(const char *ifname, struct pktradio_properties_s *properties);
Parameters:
ifnameNetwork interface name.nodeaddUsed to store the node address.
netlib_setnodeaddr
int netlib_setnodeaddr(const char *ifname, const struct pktradio_addr_s *nodeaddr);
Parameters:
ifnameNetwork interface name.nodeaddNew address.
netlib_getnodnodeaddr
int netlib_getnodnodeaddr(const char *ifname, struct pktradio_addr_s *nodeaddr);
Parameters:
ifnameNetwork interface name.nodeaddUsed to store the node address.
netlib_get_nbtable
ssize_t netlib_get_nbtable(struct neighbor_entry_s *nbtab, unsigned int nentries);
Parameters:
nbtabUsed to store a copy of the neighbor table.nentriesArray capacity (entry count).
netlib_icmpv6_autoconfiguration
int netlib_icmpv6_autoconfiguration(const char *ifname);
Parameters:
ifnameNetwork interface name.
netlib_parse_conntrack
int netlib_parse_conntrack(const struct nlmsghdr *nlh, size_t len, struct netlib_conntrack_s *ct);
Parameters:
nlhNetlink message to parse.ctConnection tracking entry.
netlib_get_conntrack
int netlib_get_conntrack(sa_family_t family, netlib_conntrack_cb_t cb);
Parameters:
familyAddress family, used to filter conntrack entries.cbConnection tracking entry.
netlib_listenon
int netlib_listenon(uint16_t portno);
Parameters:
portnoPort number.
netlib_server
void netlib_server(uint16_t portno, pthread_startroutine_t handler, int stacksize);
Parameters:
portnoPort number.handlerTask entry function.stacksizeStack size.
netlib_get_iobinfo
int netlib_get_iobinfo(struct iob_stats_s *iob);
Parameters:
iobIOB information structure.
netlib_ipv4addrconv
bool netlib_ipv4addrconv(const char *addrstr, uint8_t *addr);
Convert an IPv4 address string (e.g., "192.168.1.1") to a 4-byte binary array.
Parameters:
addrstrIPv4 address string.addrOutput buffer (4 bytes).
Returns:
Returns true on successful conversion, or false if the format is invalid.
netlib_ethaddrconv
bool netlib_ethaddrconv(const char *hwstr, uint8_t *hw);
Convert an Ethernet MAC address string (e.g., "aa:bb:cc:dd:ee:ff") to a 6-byte binary array.
Parameters:
hwstrMAC address string.hwOutput buffer (6 bytes).
Returns:
Returns true on successful conversion, or false if the format is invalid.
netlib_saddrconv
bool netlib_saddrconv(const char *hwstr, uint8_t *hw);
Convert an IEEE 802.15.4 short address (2 bytes) string to binary form.
Parameters:
hwstrAddress string.hwOutput buffer (2 bytes).
Returns:
Returns true on successful conversion, or false if the format is invalid.
netlib_eaddrconv
bool netlib_eaddrconv(const char *hwstr, uint8_t *hw);
Convert an IEEE 802.15.4 extended address (8 bytes) string to binary form.
Parameters:
hwstrAddress string.hwOutput buffer (8 bytes).
Returns:
Returns true on successful conversion, or false if the format is invalid.
netlib_nodeaddrconv
bool netlib_nodeaddrconv(const char *addrstr,
struct pktradio_addr_s *nodeaddr);
Convert a pktradio node address string to a pktradio_addr_s structure.
Parameters:
addrstrNode address string.nodeaddrOutput structure pointer.
Returns:
Returns true on successful conversion, or false if the format is invalid.