public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Marco Atzeri <marco.atzeri@gmail.com>
To: cygwin@cygwin.com
Subject: Re: Failed to get interface list while using ioctl (WSAIoctl) compiled by cygwin
Date: Fri, 12 Aug 2022 14:22:39 +0200	[thread overview]
Message-ID: <1804efbc-25a7-8e31-1a2a-980bbca36510@gmail.com> (raw)
In-Reply-To: <40af4ac5.6664.18291ee6a7f.Coremail.malongfei1230@163.com>

[-- Attachment #1: Type: text/plain, Size: 834 bytes --]

On 12.08.2022 14:01, dragon wrote:
> Hi,
> 
>      Thans for your attention.
> 
>      I had a problem doing porting the network part。I want to get a list of network devices that can be implemented on Linux with an ioctl interface. A set of ioctl interfaces is also provided on Windows called WSAIoctl with cmd SIO_GET_INTERFACE_LIST。The WSAIoctl interface is also encapsulated in cygwin and placed in /usr/include/w32api/, libs place in /usr/lib/w32api/ws2_32 .

mixing Posix and W32 API is seldom a good idea.

The attached code provides the full list of Network interface and
some details as

internal_name:  {E9617ED6-8C03-4C53-AA3F-4FD6DBC596D5}
  flags:         AF_INET6 up multicast
  address:       fe80::9cbb:5d97:6ef:565%22
  friendly_name: LAN-Verbindung* 2

feel free to modify it for your purpose

Regards
Marco






[-- Attachment #2: get-interface.c --]
[-- Type: text/plain, Size: 2969 bytes --]

/* #define _GNU_SOURCE  * To get defns of NI_MAXSERV and NI_MAXHOST */
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>


void print_flags(unsigned int flags)
{
        const char* sep = "", *sp = " ";
        if (flags & IFF_UP) {
                printf("%sup", sep);
                sep = sp;
        }
        if (flags & IFF_BROADCAST) {
                printf("%sbroadcast", sep);
                sep = sp;
        }
        if (flags & IFF_LOOPBACK) {
                printf("%sloopback", sep);
                sep = sp;
        }
        if (flags & IFF_NOTRAILERS) {
                printf("%snotrailers", sep);
                sep = sp;
        }
        if (flags & IFF_RUNNING) {
                printf("%srunning", sep);
                sep = sp;
        }
        if (flags & IFF_PROMISC) {
                printf("%spromisc", sep);
                sep = sp;
        }
        if (flags & IFF_MULTICAST) {
                printf("%smulticast", sep);
                sep = sp;
        }
}

int main(int argc, char *argv[])
{
    struct ifaddrs *ifaddr, *ifa;
    int family, s, n;
    char host[NI_MAXHOST];

    struct ifreq ifr;
    struct ifreq_frndlyname iff;
    struct ifreq_frndlyname * iffp;
    
    struct ifaddrs_hwdata * ifhwdata;

    if (getifaddrs(&ifaddr) == -1) {
        perror("getifaddrs");
        exit(EXIT_FAILURE);
    }

    /* Walk through linked list, maintaining head pointer so we
       can free list later */

    for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
        if (ifa->ifa_addr == NULL)
            continue;

        family = ifa->ifa_addr->sa_family;

        /* Display interface name and family (including symbolic
           form of the latter for the common families) */

        printf("internal_name: \t%s\n",ifa->ifa_name);

	printf(" flags: \t%s ",
               (family == AF_INET) ? "AF_INET " :
               (family == AF_INET6) ? "AF_INET6" : "unknown ");

        print_flags(ifa->ifa_flags);
	printf("\n");

        /* For an AF_INET* interface address, display the address */

        if (family == AF_INET || family == AF_INET6) {
            s = getnameinfo(ifa->ifa_addr,
                    (family == AF_INET) ? sizeof(struct sockaddr_in) :
                                          sizeof(struct sockaddr_in6),
                    host, NI_MAXHOST,
                    NULL, 0, NI_NUMERICHOST);
            if (s != 0) {
                printf("getnameinfo() failed: %s\n", gai_strerror(s));
                exit(EXIT_FAILURE);
            }

            printf(" address:\t%s\n", host);
            ifhwdata=ifa->ifa_data;
            iffp=&(ifhwdata->ifa_frndlyname);
            printf(" friendly_name:\t%s\n\n", iffp->ifrf_friendlyname);

        }
    }

    freeifaddrs(ifaddr);
    exit(EXIT_SUCCESS);
}

      reply	other threads:[~2022-08-12 12:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-12 12:01 dragon
2022-08-12 12:22 ` Marco Atzeri [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1804efbc-25a7-8e31-1a2a-980bbca36510@gmail.com \
    --to=marco.atzeri@gmail.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).