Finding ip address for eth0?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Finding ip address for eth0?

Ian Piumarta
 
On Aug 26, 2009, at 3:15 PM, Andreas Raab wrote:

> David T. Lewis wrote:
>>   #!/bin/sh
>
> Wow, without sed? I'm impressed ;-) And you know what, that's just  
> what I'll do. Thanks for the snippet.

If you change your mind, your primitive could evolve trivially from  
the following.

On Darwin this picks up inet4 and inet6 interface addresses.  
SIOCGIFCONF seems to be broken on Linux and it only picks up inet4  
addresses regardless of the address family of the socket.

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifndef s6_addr16
# define s6_addr16 __u6_addr.__u6_addr16
#endif

int main(int argc, char **argv)
{
   struct ifreq *ifr;
   struct ifconf ifc;
   char buf[32768];
   int s= socket(AF_INET, SOCK_DGRAM, 0);
   if (-1 == s) return -1;
   ifc.ifc_len= sizeof(buf);
   ifc.ifc_buf= buf;
   if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
     perror("SIOCGIFCONF");
     goto die;
   }
   for (ifr= ifc.ifc_req;  (char *)ifr < (char *)ifc.ifc_req +  
ifc.ifc_len;) {
     switch (ifr->ifr_addr.sa_family) {
     case AF_INET: {
       struct sockaddr_in *sin= (struct sockaddr_in *)&ifr->ifr_addr;
       printf("%s\t%s\n", ifr->ifr_name,
             inet_ntoa(sin->sin_addr));
       break;
     }
     case AF_INET6: {
       struct sockaddr_in6 *sin6= (struct sockaddr_in6 *)&ifr->ifr_addr;
       struct in6_addr *in6= &sin6->sin6_addr;
       printf("%s\t%x:%x:%x:%x:%x:%x:%x:%x\n", ifr->ifr_name,
             in6->s6_addr16[0], in6->s6_addr16[1], in6->s6_addr16[2], in6-
 >s6_addr16[3],
             in6->s6_addr16[4], in6->s6_addr16[5], in6->s6_addr16[6], in6-
 >s6_addr16[7]);
       break;
     }
     default:
       break;
     }
#  if defined(linux)
     ++ifr;
#  else
     ifr= (struct ifreq *)((char *)ifr + sizeof(ifr->ifr_name) + ifr-
 >ifr_addr.sa_len);
#  endif
   }
  die:
   close(s);
   return 0;
}

Reply | Threaded
Open this post in threaded view
|

Re: Finding ip address for eth0?

Bert Freudenberg
 

On 27.08.2009, at 01:10, Ian Piumarta wrote:

> On Aug 26, 2009, at 3:15 PM, Andreas Raab wrote:
>
>> David T. Lewis wrote:
>>>  #!/bin/sh
>>
>> Wow, without sed? I'm impressed ;-) And you know what, that's just  
>> what I'll do. Thanks for the snippet.
>
> If you change your mind, your primitive could evolve trivially from  
> the following.


So the extended SocketPlugin cannot list all ip addresses under Linux?  
On John's VM it does I thought ...

- Bert -


Reply | Threaded
Open this post in threaded view
|

Re: Finding ip address for eth0?

johnmci
 
yes, but OS-X is not Linux...

Also I'm not sure it's correct
http://bugs.squeak.org/view.php?id=7392

On 26-Aug-09, at 4:30 PM, Bert Freudenberg wrote:

>
> On 27.08.2009, at 01:10, Ian Piumarta wrote:
>
>> On Aug 26, 2009, at 3:15 PM, Andreas Raab wrote:
>>
>>> David T. Lewis wrote:
>>>> #!/bin/sh
>>>
>>> Wow, without sed? I'm impressed ;-) And you know what, that's just  
>>> what I'll do. Thanks for the snippet.
>>
>> If you change your mind, your primitive could evolve trivially from  
>> the following.
>
>
> So the extended SocketPlugin cannot list all ip addresses under  
> Linux? On John's VM it does I thought ...
>
> - Bert -
>
>

--
=
=
=
========================================================================
John M. McIntosh <[hidden email]>   Twitter:  
squeaker68882
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
=
=
=
========================================================================




Reply | Threaded
Open this post in threaded view
|

Re: Finding ip address for eth0?

David T. Lewis
 
On Wed, Aug 26, 2009 at 04:35:54PM -0700, John M McIntosh wrote:

>
> On 26-Aug-09, at 4:30 PM, Bert Freudenberg wrote:
> >
> >So the extended SocketPlugin cannot list all ip addresses under  
> >Linux? On John's VM it does I thought ...
>
> yes, but OS-X is not Linux...
>
> Also I'm not sure it's correct
> http://bugs.squeak.org/view.php?id=7392

I'm fairly sure that this is a bug in some versions of the libuuid
library that apparently shows up in some Linux distributions. It's
not a bug in anything that you wrote for the plugin. I have a hunch
that it might be possible to "fix" it by static linking to a non-buggy
version of libuuid, but aside from that it's not anything that can
be resolved in the plugin.

Dave

12