Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:29:42+00:00 2026-06-16T03:29:42+00:00

I am currently working on an IPv6 class and use inet_pton to retrieve the

  • 0

I am currently working on an IPv6 class and use inet_pton to retrieve the actual binary representation of the IP from a string i.e.:

    AdressV6::AdressV6(const String & _ip)
    {
        int result = inet_pton(AF_INET6, _ip.c_str(), &(m_nativeAdress));

        if(result <= 0)
            //throw...

        //How can I retrieve the sope ID from that?
    }

Is there a common way to do that? Do you just manually parse the string and look for the “%” that does not sound very bullet proof 🙁

Thank you!

I tried manual parsing for now which seems to work. Still, if there is a better way please let me know:

        //retrieve scope ID
        uint32 scopeId = 0;
        size_t pos = _ip.find("%");
        if(pos != String::npos)
        {
            String theId = _ip.substr(pos+1);
            scopeId = atoi(theId.c_str());
        }
        m_scopeId = scopeId;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:29:43+00:00Added an answer on June 16, 2026 at 3:29 am

    On BSD and BSD based systems (this includes MacOS X for example), the scope ID is embedded into the address itself for link local addresses as the second 16 bit word. Please refer to the FreeBSD Handbook and search for "8.1.1.3 Scope Index" (without the quotes).

    So assuming that intf1 has scope ID 1 and intf2 has scope ID 2, inet_pton() will convert the strings as follows on these platforms:

    "fe80::1234%intf1" -> fe80:1::1234
    "fe80::1234%intf2" -> fe80:2::1234
    "fe80::1234"       -> fe80::1234
    

    The last address is simply unscoped and thus cannot be really used for sending out data.

    Please note that this is non-standard; inet_pton() does not work that way on Linux or Windows based systems. However, I think even on Linux and Windows based systems, inet_pton() allows a scope ID at the end, it will simply ignore it, though.

    For non-link-local address, this trick doesn’t work, of course, yet those addresses are usually not scoped. They can be scoped, but usually every interface has an own, unique interface IPv6 address, based on its interface identifier (even if you use DHCPv6, in which case it has a DHCP address assigned by the DHCP server, as well as the auto generated IPv6 interface address, unless this auto generation has been forbidden).

    The struct sockaddr_in6 structure has a field for the scope ID but the RFC that defines this field (RFC 2553 – Section 3.3) does not really give much detail how this field is to be interpreted. It only says:

    The mapping of sin6_scope_id to an interface or set of interfaces is
    left to implementation and future specifications on the subject of
    site identifiers.

    So this field is entirely implementation specific.

    If you want this field to be filled in correctly, and your code should be as cross-platform as possible, you should use getaddrinfo():

    struct addrinfo hints;
    struct addrinfo * result;
    
    memset(&hints, 0, sizeof(hints));
    // AI_NUMERICHOST prevents usage of DNS servers,
    // it tells getaddrinfo that the input string is a numeric IP address.
    hints.flags = AI_NUMERICHOST;
    if (getaddrinfo("fe80::1234%intf1", NULL, &hints, &result) == 0) {
        // result->ai_addr claims to be a pointer to struct sockaddr,
        // in fact it will be a pointer to a struct sockaddr_in6 in our case.
        struct sockaddr_in6 * so = (struct sockaddr_in6 *)result->ai_addr;
    
        // It will be prefilled like this:
        //
        // so->sin6_family   ==> AF_INET6;
        // so->sin6_port     ==> 0
        // so->sin6_flowinfo ==> 0
        // so->sin6_addr     ==> fe80::1234
        // so->sin6_scope_id ==> "intf1" as scope ID
        
        // Do something with that sockaddr,
        // e.g. set a port number and connect a socket to that address.
    
        freeaddrinfo(result);
    }
    

    One extra tip: If you want to use the returned getaddrinfo() for a server socket (a socket that you want to bind locally and then call accept() on it), you should also set the passive flag:

    hints.flags = AI_NUMERICHOST | AI_PASSIVE;
    

    Not that it will play a role in most case but that is the correct way of using getaddrinfo().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently working through a Teach Yourself WPF tutorial. Usually I can mentally convert from
I'm currently working on a Perl script to gather data from the QuakeLive website.
Were currently working on a script that reads the current users information from out
Currently working on a project to upgrade our Goole Maps API from v2 to
Currently working on a Swing application and I need to use a ListSelectionListener to
im currently working with jQuery ajax and i get some unexpected error. I use
Am currently working on a web application which receives the encoded text from the
I am currently working on a project that uses Manager.createPlayer(InputStream is, String mimeType) to
Currently working with converting SQLException error messages into messages that are more useful for
Currently working with the following package structure: /package __init__.py final.py /write __init__.py write.py /data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.