DEV Community

Fomalhaut Weisszwerg
Fomalhaut Weisszwerg

Posted on

4 1

[C/C++] How to get scope_id of IPv6 Link local Address on Linux.

A well-known way is to use getaddrinfo() such like:

#include <arpa/inet.h>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <netdb.h>
#include <string>
#include <sys/socket.h>
#include <sysexits.h>

int main(void)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_protocol = IPPROTO_UDP;

    struct addrinfo* result_addrinfo = nullptr;
    auto result = getaddrinfo("fe80::dabb:c1ff:fe0a:17f2%enp39s0", std::to_string(12345).c_str(), &hints, &result_addrinfo);
    if( result != 0 ){
        std::cout << "`getaddrinfo()` failed: " << gai_strerror(errno) << std::endl;
        return result;
    }

    for(
        struct addrinfo* info = result_addrinfo;
        info != nullptr;
        info = info->ai_next
    ){
        char ipv6addr_human_readable_form[INET6_ADDRSTRLEN] = {0, };
        inet_ntop(info->ai_family, &((struct sockaddr_in6*)(info->ai_addr))->sin6_addr, ipv6addr_human_readable_form, info->ai_addrlen);
        auto ipv6_scope_id = ((struct sockaddr_in6*)(info->ai_addr))->sin6_scope_id;

        std::cout << "IPv6 address = " << ipv6addr_human_readable_form << std::endl;
        std::cout << "Scope-ID = " << std::to_string(ipv6_scope_id) << std::endl;
    }

    freeaddrinfo(result_addrinfo);
    return EX_OK;
}
Enter fullscreen mode Exit fullscreen mode

Of course, the Scope-ID can also be obtained as previously. But is there an easier and shorter?

Solution using if_nametoindex()

Fortunately, Linux provides a function called if_nametoindex(). It returns the index number of the network interface, which is currently equal to the Scope-ID.

#include <net/if.h>
#include <iostream>
#include <sysexits.h>

int main(void)
{
    auto scope_id = if_nametoindex("enp39s0");
    std::cout << scope_id << std::endl;

    return EX_OK;
}
Enter fullscreen mode Exit fullscreen mode

Now, the code became much shorter and easier. Also, there is no need to care to free memory!

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay