Professional Web Applications Themes

SIOCGIFADDR results in ENXIO - Sun Solaris

I'm trying to write a little routine find an ethernet address (non-localhost) as an identifier for a client/server routine. The gethostname() and gethostbyname() routine works great on Solaris, but wasn't working for Linux (which always returned localhost) and I chanced on this other method. I haven't tried it yet in Linux, but can't understand why it's not working under Solaris as well... can anyone see what I'm doing wrong? (yes, I know that this isn't an authoritative list of ethernet adapters... I'm going quick and dirty until I get SOME results...) errno is always ENXIO (No such device or address) ...

  1. #1

    Default SIOCGIFADDR results in ENXIO



    I'm trying to write a little routine find an ethernet address
    (non-localhost) as an identifier for a client/server routine. The
    gethostname() and gethostbyname() routine works great on Solaris, but wasn't
    working for Linux (which always returned localhost) and I chanced on this
    other method. I haven't tried it yet in Linux, but can't understand why
    it's not working under Solaris as well... can anyone see what I'm doing
    wrong? (yes, I know that this isn't an authoritative list of ethernet
    adapters... I'm going quick and dirty until I get SOME results...)

    errno is always ENXIO (No such device or address)

    ------------------------------------

    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/tcp.h>
    #include <stropts.h>
    #include <signal.h>
    #include <net/if.h>
    #include <sys/ioctl.h>
    #include <sys/sockio.h>
    #include <arpa/inet.h>

    #if defined(SOLARIS)
    #define ETHERNET_LIST 4
    char *AdapterList[] = {
    "le",
    "hme",
    "eth0",
    "eth1"
    };
    #elif defined(LINUX)
    #define ETHERNET_LIST 4
    char *AdapterList[] = {
    "eth0",
    "eth1",
    "le0",
    "le1"
    };
    #endif


    void testcode()
    {
    int i;
    SOCKET tmpsock;
    struct ifreq ifr;
    struct sockaddr_in *paddr;
    unsigned char tmpaddr[32];


    tmpaddr[0] = 0;
    i = 0;
    bzero(&ifr, sizeof(ifr));

    tmpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);

    while(tmpaddr[0] == 0 && i < ETHERNET_LIST) {
    strcpy(ifr.ifr_name, AdapterList[i]);
    if(ioctl(tmpsock, SIOCGIFADDR , &ifr) == 0) { // success
    paddr = (struct sockaddr_in*)&(ifr.ifr_addr);
    if(strcmp( inet_ntoa(paddr->sin_addr), "127.0.0.1") != 0)
    sprintf(tmpaddr, "%s", inet_ntoa(paddr->sin_addr) );
    }
    else
    Winlib_PrintErrno(errno, ifr.ifr_name, 0, TRUE) ;

    i++;
    }
    closesocket(tmpsock);
    }


    Brent Guest

  2. #2

    Default Re: SIOCGIFADDR results in ENXIO

    In article <supernews.com>,
    Brent Burkholder <com> wrote: 

    The entries in AdapterList are wrong. There are no adapters named "hme" or
    "le", they're "hme0", "hme1", etc. and "le0", le1", etc.

    BTW, rather than hard-coding ETHERNET_LIST (which is error-prone if you
    forget to update it when adding to AdapterList), you can compute it as
    sizeof(AdapterList)/SizeOf(*AdapterList).
     

    --
    Barry Margolin, com
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Guest

  3. #3

    Default Re: SIOCGIFADDR results in ENXIO

    Brent Burkholder wrote: 

    you probably mean IP address, not Ethernet address.

    try using le0 and hme0. ENXIO is a good hint. Anyways, have you thought
    about what your client/server logic should be if there are multiple IP
    addresses on the host you are trying to get an IP for? also isn't
    hardcoding the device names another limitation?

    John Guest

  4. #4

    Default Re: SIOCGIFADDR results in ENXIO

    > you probably mean IP address, not Ethernet address. 


    I'd used le0 and hme0 initially then started tinkering to try to get a
    different response. :)

    Yes, hardcoding the device names is a real limitation that I'm not pleased
    with. If Linux didn't insist on returning 127.0.0.1 when I do
    gethostbyname() then I wouldn't be using this routine at all... this routine
    basically is attempting to set up a last-ditch fallback IP in case my normal
    procedure didn't work.

    Yes, if there are multiples addresses I'll need to update the client address
    after actually making the connection I think. That would return the IP
    address of the adapter actually using the socket to talk to the server,
    correct? (ergo, the return address SHOULD work unless being blocked?) For
    what I'm currently using the routine for it's not all that important, but
    may possibly be in the future.

    Does anyone have a better routine for accomplishing this that works on
    Solaris/Linux?




    Brent Guest

  5. #5

    Default Re: SIOCGIFADDR results in ENXIO

    In article <supernews.com>,
    "Brent Burkholder" <com> writes: 
    >
    >
    > I'd used le0 and hme0 initially then started tinkering to try to get a
    > different response. :)
    >
    > Yes, hardcoding the device names is a real limitation that I'm not pleased
    > with. If Linux didn't insist on returning 127.0.0.1 when I do
    > gethostbyname() then I wouldn't be using this routine at all... this routine
    > basically is attempting to set up a last-ditch fallback IP in case my normal
    > procedure didn't work.[/ref]
    [...]

    You can get a list of the interfaces from the system. Here's an example.
    There are actually two different sets of ioctls for this stuff,
    SIOCGIFNUM/SIOCGIFCONF, and SIOCGLIFNUM/SIOCGLIFCONF. The first pair
    are older and can't handle IPv6 addresses, the second pair are newer and
    can (and also use different data structures; note there may be an error
    in the if_tcp(7p) man page where it shows a struct ifreq pointer within
    struct lifconf - that should really be a struct lifreq pointer (header file
    is correct)), but may not be available on older systems.

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/sockio.h>
    #include <net/if.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    int print_ifnames()
    {
    int sockfd,x;
    struct lifnum ln;
    struct lifconf lc;

    if ((sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) == -1) {
    perror("islocal(): can't open UDP socket");
    return -1;
    }

    /* use AF_INET for IPv4 only or AF_INET6 for IPv6 only */
    /* note: if using AF_UNSPEC, some interface names may appear twice,
    once for IPv4 and once for IPv6 */
    ln.lifn_family=AF_UNSPEC;
    ln.lifn_flags=ln.lifn_count=0;

    if (ioctl(sockfd,SIOCGLIFNUM,&ln) == -1) {
    perror("islocal(): SIOCGLIFNUM ioctl failed");
    close(sockfd);
    return -1;
    }

    lc.lifc_family=AF_UNSPEC;
    lc.lifc_flags=0;
    lc.lifc_len=ln.lifn_count * sizeof(struct lifreq);
    if ((lc.lifc_req=(struct lifreq *) malloc(lc.lifc_len)) == NULL ) {
    perror("islocal(): malloc() failed");
    close(sockfd);
    return -1;
    }

    if (ioctl(sockfd,SIOCGLIFCONF,&lc) == -1) {
    perror("islocal(): SIOCGLIFCONF ioctl failed");
    close(sockfd);
    free(lc.lifc_req);
    return -1;
    }
    for (x=0;x<ln.lifn_count;x++)
    printf("%.*s\n",LIFNAMSIZ,lc.lifc_req[x].lifr_name);
    close(sockfd);
    free(lc.lifc_req);

    return 0;
    }


    --
    mailto:smart.net http://www.smart.net/~rlhamil
    Richard Guest

Similar Threads

  1. formatting find results in results window or in savedfile
    By InQuistve in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 0
    Last Post: January 23rd, 03:19 PM
  2. Replies: 2
    Last Post: August 9th, 09:41 PM
  3. Query results don't display properly in results table.IGNORE PREVIOUS
    By JoyRose in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: March 24th, 07:28 PM
  4. Query results don't display properly in results table.
    By JoyRose in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: March 24th, 07:17 PM
  5. Replies: 3
    Last Post: July 17th, 11:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139