qlibs:(socket_if)
π qlibs:(socket_if) β bestie's guide to interface names & scope_ids π
okay so real talk, this is one of those little pages from the qlibs manual (section 3, if we're being specific π) and honestly it's short but it matters. it's all about mapping between an interface name and a scope_id, and the two lil functions that do it for you. let's go β¨
πΈ NAME πΈ
socket_if β grab the scope_id for an interface name, or the other way around, no cap π
π SYNTAX π
#include "socket_if.h"
uint32 socket_getifidx(const char *ifname);
const char *socket_getifname(uint32 scope_id);
two functions, one header, super clean. we love a tidy include π
β¨ DESCRIPTION β¨
socket_getifidxβ returns thescope_idof an interface namedifname, and like, typically that's something like''eth0''. yep, that's the whole vibe π«Άsocket_getifnameβ goes the other direction: it takes thescope_idthat the operating system assigned, and retrieves the interface name (ifname) from it. it's giving reverse lookup main character energy π
π¦ INTERFACE_NAME VERSUS SCOPE_ID π¦
so here's the tea β for IPv6 LLU addresses, you gotta hand over an extra ifname (that's the interface name) alongside the IPv6 address, looking like this:
fe80::1%eth0
but the operating systems? they'd rather use scope_id as the index for ifname. classic OS behaviour honestly π
now for global IPv6 and ULA addresses, ifname can just be set to 0. and since IPv4 addresses on any interface are always unique (periodt π―), you can simply use 0 for all cases. easy.
π BACKGROUND π
Qlib's socket routines give you an easy API to set up TCP or UDP connections over IPv4 or IPv6 networks β both, bestie, we don't pick favourites π. and together with Qlib's IP address parsing capabilities, there's a whole set of high-level socket routines that let you handle IPv4/IPv6 in a common way. iconic behaviour, truly π
π SOCKET FILES π
here's the whole squad of socket files, each doing their one job and doing it well:
socket_bind.cβ bind to, or reuse, the local IPv4/IPv6 address and port for a socket connection πsocket_connect.cβ attempts to set up a TCP or UDP client connection π€socket_info.cβ get the local/remote IPv4/IPv6 address of a socket πsocket_recv.cβ set up a receiving IPv4/IPv6 connection π₯socket_send.cβ send a UDP datagram over an IPv4 or IPv6 connection π€socket_setup.cβ listen to and accept an IPv4/IPv6 TCP socket connection πsocket_tcp.cβ create a non-blocking TCP stream socket πsocket_udp.cβ create a non-blocking UDP datagram socket π¨
π§ USAGE π§
most of the files above include their IPv4 and IPv6 counterparts together, plus a combined usage that needs a scope_id on top β or just simply 0 π«‘. IPv4 addresses usually get converted, upon reading, into IPv4-mapped IPv6 addresses using Qlib's IP address parsing functions. and the IPv4 and IPv6 socket calls β if required β need to be told apart by the calling routines testing this:
ip6_isv4mapped(ip)
otherwise? the unified IPv6/IPv4 versions get used. simple as that π
ngl this is the part where you double-check which flavour you're actually dealing with before you ship it β the page is very clear that the calling routines do the distinguishing, not the library doing it behind your back π
β SEE ALSO β
socket_bind(3), socket_connect(3), socket_info(3), socket_recv(3), socket_send(3), socket_setup(3), socket_tcp(3), socket_udp(3) β go read them all, they're the friend group π«Άβ¨
and that's the whole page!! π this was qlibs:(socket_if), section 3 of the manual, signing off β mwah, go forth and resolve your interfaces, bestie ππ¦π
π¦ where this page comes from
- alpine 3.24: fehqlibs-doc 31-r0 Β· Public-Domain
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms