Linux getting all network interface names

You could check which entries from getifaddrs belong to the AF_PACKET family. On my system that seems to list all interfaces:

struct ifaddrs *addrs,*tmp;

getifaddrs(&addrs);
tmp = addrs;

while (tmp)
{
    if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET)
        printf("%s\n", tmp->ifa_name);

    tmp = tmp->ifa_next;
}

freeifaddrs(addrs);

getifaddrs() will only return your interfaces addresses, not the interfaces themselves.

What if any of your interface has no address, or no address of the requested family, as suggested with the 'AF_PACKET' one ?

Here, an example where I’ve got a tunnel interface (with an OpenVPN connexion), and where I’m listing all entries from getifaddrs() for each of my network interfaces:

[0] 1: lo                address family: 17 (AF_PACKET) b4:11:00:00:00:01
                         address family: 2 (AF_INET)    address: <127.0.0.1>
                         address family: 10 (AF_INET6)  address: <::1>
[...]

[5] 10: tun0             address family: 2 (AF_INET)    address: <172.16.0.14>
[EOF]

Bam. No AF_PACKET on the "tun0" interface, but it DOES exist on the system.

You should, instead, use if_nameindex() syscall, which does exactly what you want. In other words, with no arguments, it returns a list of all interfaces on your system:

#include <net/if.h>
#include <stdio.h>

int main (void)
{
    struct if_nameindex *if_nidxs, *intf;

    if_nidxs = if_nameindex();
    if ( if_nidxs != NULL )
    {
        for (intf = if_nidxs; intf->if_index != 0 || intf->if_name != NULL; intf++)
        {
            printf("%s\n", intf->if_name);
        }

        if_freenameindex(if_nidxs);
    }

    return 0;
}

And voilà.