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

  • Home
  • SEARCH
  • 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 3211810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:46:22+00:00 2026-05-17T14:46:22+00:00

I need some help with finding network interfaces based on a provided IP address.

  • 0

I need some help with finding network interfaces based on a provided
IP address. I need a cross-platform way to reliably find the local
interface that has the given address in both IPv4 and IPv6 formats. I
created the attached program to take an IP address as a string and
search through the results of getifaddrs.

The reason is that I eventually want to pass this in_addr or
in6_addr structure to the IP_MULTICAST_IF ioctl on a socket for
specifying which interface should be used to send a multicast message.
I want to check that it is a known IP before proceeding.

While it seems to work fine on my Linux machine and on a MacBook, it
fails on my lab’s server. (Oh my, this eventually has to work on
Windows too..) Here’s an example run on Linux:

$ ifconfig wlan0 | grep inet6
          inet6 addr: fe80::1e4b:d6ff:fe6e:f846/64 Scope:Link

$ ifconfig wlan0 | grep inet
          inet addr:192.168.0.13  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::1e4b:d6ff:fe6e:f846/64 Scope:Link

$ ./findif 192.168.0.13

found: wlan0

$ ./findif fe80::1e4b:d6ff:fe6e:f846

name: lo
ifa:      00000000000000000000000000000001
provided: fe800000000000001e4bd6fffe6ef846

name: wlan0
ifa:      fe800000000000001e4bd6fffe6ef846
provided: fe800000000000001e4bd6fffe6ef846

found: wlan0

So far so good. I get similar results on a MacBook running OS X 10.6.
However, when I do something similar on our server, which is an older
PPC-based OS X machine running 10.4.11, I get the following:

$ ifconfig en0 | grep inet
  inet6 fe80::20d:XXXX:XXXX:XXXX%en0 prefixlen 64 scopeid 0x4 
  inet 132.XXX.XX.XX netmask 0xffffff00 broadcast 132.XXX.XX.255

$ ./findif 132.XXX.XX.XX

found: en0

$ ./findif fe80::XXX:XXXX:XXXX:XXXX

name: lo0
ifa:      00000000000000000000000000000001
provided: fe800000000000000XXXXXXXXXXXXXXX

name: lo0
ifa:      fe800001000000000000000000000001
provided: fe800000000000000XXXXXXXXXXXXXXX

name: en0
ifa:      fe800004000000000XXXXXXXXXXXXXXX
provided: fe800000000000000XXXXXXXXXXXXXXX

name: en1
ifa:      fe800005000000000YYYYYYYYYYYYYYY
provided: fe800000000000000YYYYYYYYYYYYYYY

Apologies for X’ing out some of the address digits, I thought it best
not to expose real IPs for a live server here. Rest assured that
wherever you see X or Y it is a matching hex digit.

Anyways, notice that the IPv6 returned for en0 has exactly 1 bit that
is different than reported by ifconfig. Can anyone tell me why that
is, and how I should adjust my code? Is a simple memcmp not the
correct way to compare IPv6 addresses?

Thanks for any suggestions.

Here is the code for findif.c:

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <string.h>

void printipv6(const void* ipv6)
{
    int i;
    for (i=0; i<16; i++)
        printf("%02x", ((unsigned char*)ipv6)[i]);
}

int find_iface(const char *ip)
{
    union {
        struct in_addr addr;
        struct in6_addr addr6;
    } a;

    int rc = inet_pton(AF_INET6, ip, &a.addr6);
 int fam = AF_INET6;
    if (rc==0) {
        rc = inet_pton(AF_INET, ip, &a.addr);
  fam = AF_INET;
 }
    if (rc < 0) {
        perror("inet_pton");
        return 1;
    }

    struct ifaddrs *ifa, *ifa_list;
    if (getifaddrs(&ifa_list)==-1) {
        perror("getifaddrs");
        return 1;
    }
    ifa = ifa_list;

    int i=0;
    while (ifa) {
        if (ifa->ifa_addr) {
            if (ifa->ifa_addr->sa_family == AF_INET && fam == AF_INET)
            {
                if (memcmp(&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr,
                           &a.addr, sizeof(struct in_addr))==0) {
                    printf("\nfound: %s\n", ifa->ifa_name);
                    break;
                }
            }
            else if (ifa->ifa_addr->sa_family == AF_INET6 && fam == AF_INET6)
            {
                struct in6_addr *p = &((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr;
                printf("\nname: %s\n", ifa->ifa_name);
                printf("ifa:      "); printipv6(p);        printf("\n");
                printf("provided: "); printipv6(&a.addr6); printf("\n");
                if (memcmp(p, &a.addr6, sizeof(struct in6_addr))==0) {
                    printf("\nfound: %s\n", ifa->ifa_name);
                    break;
                }
            }
        }
        ifa = ifa->ifa_next;
        i++;
    }
    freeifaddrs(ifa_list);
    return 0;
}

int main(int argc, char *argv[])
{
    if (argc > 1)
        return find_iface(argv[1]);
    return 0;
}
  • 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-05-17T14:46:23+00:00Added an answer on May 17, 2026 at 2:46 pm

    Your code looks correct – IPv6 link-local addresses are supposed to have 54 zero bits following the first 10 bits, so the addresses being returned by getifaddrs() just seem wrong. It looks like that version of OS X might be incorrectly munging the scope id into the address.

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

Sidebar

Related Questions

I need some help finding a jQuery plugin which will allow me to display
I need some help from the shell-script gurus out there. I have a .txt
I need some help regarding algorithm for randomness. So Problem is. There are 50
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with
I need some help ... I'm a bit (read total) n00b when it comes
Hi I need some help with the following scenario in php. I have a
I am getting a little confused and need some help please. Take these two
I am new to all the anonymous features and need some help. I have
I'm working with jQuery for the first time and need some help. I have

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.