Re: ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-18 Thread Matthias Andree

Am 15.06.2009, 11:22 Uhr, schrieb Frédéric Bron frederic.b...@m4x.org:


To fix your application, call either

 struct ifconf ifc;
 ifc.ifc_len = sizeof (struct ifreq) * 32;
 ifc.ifc_buf = malloc (ifc.ifc_len);
 if (ioctl (fd, SIOCGIFCONF, ifc))
   /* Resize ifc_buf and retry */
 else
   {
     struct ifreq *ifr = ifc.ifc_req;
     struct ifreq ifr2;
     for (int i = 0; i  ifc.ifc_len; i += sizeof (struct ifreq), ++ifr)
       if (!ioctl (fd, SIOCGIFADDR, ifr2))
         /* Print result for that interface */
   }


Thanks, this works half! No need of ifr2, ifr is enough.
I saw the name change: 1.5 gives eth0, eth1, eth2, lo and 1.7 gives
{821C54BE-...}...


Careful - this isn't portable to newer BSDs, as there ifc_len may exceed  
sizeof(struct ifreq).


getifaddrs() is probably the more portable solution here.

--
Matthias Andree

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-16 Thread Frédéric Bron
 I applied a patch to Cygwin which also reports the IPv4 addresses of
 disconnected interfaces, fetching the info from the registry.  It's
 a pity that Windows doesn't correctly report these addresses in the
 official API.

 This won't work for IPv6 and IPv6-only interfaces.  I didn't find a
 generic way to list IPv6 addresses except for using the official API.
 Since Windows Vista the IPv6 address information isn't stored in the
 registry at all, at least not in a publically available, easy to read
 place.

Thank you, can I test it? Shall I just update my installation?
Frédéric

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-16 Thread Corinna Vinschen
On Jun 16 11:56, Fr?d?ric Bron wrote:
  I applied a patch to Cygwin which also reports the IPv4 addresses of
  disconnected interfaces, fetching the info from the registry.  It's
  a pity that Windows doesn't correctly report these addresses in the
  official API.
 
  This won't work for IPv6 and IPv6-only interfaces.  I didn't find a
  generic way to list IPv6 addresses except for using the official API.
  Since Windows Vista the IPv6 address information isn't stored in the
  registry at all, at least not in a publically available, easy to read
  place.
 
 Thank you, can I test it? Shall I just update my installation?

There's no new release yet.  The absence of an announcement speaks
volumes.

The next developer snapshot or the next release will contain this
change, whatever comes first.  In the meantime you can build the
Cygwin DLL from CVS (http://cygwin.com/cvs.html).


Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-15 Thread Frédéric Bron
 To fix your application, call either

  struct ifconf ifc;
  ifc.ifc_len = sizeof (struct ifreq) * 32;
  ifc.ifc_buf = malloc (ifc.ifc_len);
  if (ioctl (fd, SIOCGIFCONF, ifc))
    /* Resize ifc_buf and retry */
  else
    {
      struct ifreq *ifr = ifc.ifc_req;
      struct ifreq ifr2;
      for (int i = 0; i  ifc.ifc_len; i += sizeof (struct ifreq), ++ifr)
        if (!ioctl (fd, SIOCGIFADDR, ifr2))
          /* Print result for that interface */
    }

Thanks, this works half! No need of ifr2, ifr is enough.
I saw the name change: 1.5 gives eth0, eth1, eth2, lo and 1.7 gives
{821C54BE-...}...

However, with that code, I get all network adapters with cygwin 1.5
but only active adpaters with 1.7 (with IP adress != 0).
For example if I unplug the ethernet wire, the ip of eth0 becomes
0.0.0.0 with 1.5 and I don't see it anymore with 1.7.

How can I get all interfaces with 1.7?

Frédéric

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-15 Thread Corinna Vinschen
On Jun 15 11:22, Fr?d?ric Bron wrote:
  To fix your application, call either
 
   struct ifconf ifc;
   ifc.ifc_len = sizeof (struct ifreq) * 32;
   ifc.ifc_buf = malloc (ifc.ifc_len);
   if (ioctl (fd, SIOCGIFCONF, ifc))
     /* Resize ifc_buf and retry */
   else
     {
       struct ifreq *ifr = ifc.ifc_req;
       struct ifreq ifr2;
       for (int i = 0; i  ifc.ifc_len; i += sizeof (struct ifreq), ++ifr)
         if (!ioctl (fd, SIOCGIFADDR, ifr2))
           /* Print result for that interface */
     }
 
 Thanks, this works half! No need of ifr2, ifr is enough.
 I saw the name change: 1.5 gives eth0, eth1, eth2, lo and 1.7 gives
 {821C54BE-...}...
 
 However, with that code, I get all network adapters with cygwin 1.5
 but only active adpaters with 1.7 (with IP adress != 0).
 For example if I unplug the ethernet wire, the ip of eth0 becomes
 0.0.0.0 with 1.5 and I don't see it anymore with 1.7.
 
 How can I get all interfaces with 1.7?

I just debugged this and the answer is, right now you can't.  I'm
going to fix that at one point, but I have other stuff to do first.


Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-15 Thread Corinna Vinschen
On Jun 15 13:42, Corinna Vinschen wrote:
 On Jun 15 11:22, Fr?d?ric Bron wrote:
   To fix your application, call either
  
    struct ifconf ifc;
    ifc.ifc_len = sizeof (struct ifreq) * 32;
    ifc.ifc_buf = malloc (ifc.ifc_len);
    if (ioctl (fd, SIOCGIFCONF, ifc))
      /* Resize ifc_buf and retry */
    else
      {
        struct ifreq *ifr = ifc.ifc_req;
        struct ifreq ifr2;
        for (int i = 0; i  ifc.ifc_len; i += sizeof (struct ifreq), ++ifr)
          if (!ioctl (fd, SIOCGIFADDR, ifr2))
            /* Print result for that interface */
      }
  
  Thanks, this works half! No need of ifr2, ifr is enough.
  I saw the name change: 1.5 gives eth0, eth1, eth2, lo and 1.7 gives
  {821C54BE-...}...
  
  However, with that code, I get all network adapters with cygwin 1.5
  but only active adpaters with 1.7 (with IP adress != 0).
  For example if I unplug the ethernet wire, the ip of eth0 becomes
  0.0.0.0 with 1.5 and I don't see it anymore with 1.7.
  
  How can I get all interfaces with 1.7?
 
 I just debugged this and the answer is, right now you can't.  I'm
 going to fix that at one point, but I have other stuff to do first.

I applied a patch to Cygwin which also reports the IPv4 addresses of
disconnected interfaces, fetching the info from the registry.  It's
a pity that Windows doesn't correctly report these addresses in the
official API.

This won't work for IPv6 and IPv6-only interfaces.  I didn't find a
generic way to list IPv6 addresses except for using the official API.
Since Windows Vista the IPv6 address information isn't stored in the
registry at all, at least not in a publically available, easy to read
place.


Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-12 Thread Frédéric Bron
I used the following program to obtain mac address and ip of network
adpaters. It works fine with 1.5 but not with 1.7.

Output with 1.5:
sock=3
trying eth0
mac: 00.1f.3c.57.XX.XX, ip: XXX.XXX.XXX.XXX
trying eth1
mac: 00.1d.09.df.XX.XX, ip: XXX.XXX.XXX.XXX
trying eth2
mac: 08.00.27.00.XX.XX, ip: XXX.XXX.XXX.XXX
trying eth3
...

Output with 1.7:
sock=3
trying eth0
trying eth1
trying eth2
trying eth3
...

Any clue?

Frédéric

#include iostream
#include iomanip
#include netdb.h
#include sys/ioctl.h
#include net/if.h

void read_hwaddr(const struct ifreq ifr) {
std::cout  mac:  ;
const struct sockaddr hwaddr=ifr.ifr_hwaddr ;
for (size_t k=0 ; k6 ; ++k) {
if (k) std::cout  '.' ;
unsigned int v=static_castunsigned char(hwaddr.sa_data[k]) ;
std::cout  std::hex  std::setw(2) 
std::setfill('0')  static_castint(v) ;
}
}

void read_ip(const struct ifreq ifr) {
std::cout  ip:  ;
const struct sockaddr addr=ifr.ifr_addr ;
for (size_t k=0 ; k4 ; ++k) {
unsigned int v=static_castunsigned char(addr.sa_data[k+2]) ;
if (k) std::cout  '.' ;
std::cout  std::dec  static_castint(v) ;
}
}

int main() {
std::string eth(eth) ;
struct protoent *proto=NULL ;
if (proto=getprotobyname(tcp)) {
int sock=socket(PF_INET, SOCK_STREAM, proto-p_proto) ;
std::cout  sock=  sock  '\n' ;
struct ifreq ifr ;
for (char k='0' ; k='9' ; ++k) {
std::cout  trying   (eth+k)  '\n' ;
std::memset(ifr, 0, sizeof(struct ifreq)) ;
std::strcpy(ifr.ifr_name, (eth+k).c_str()) ;
// MAC address
if (ioctl(sock, SIOCGIFHWADDR, ifr)!=0) continue ;
read_hwaddr(ifr) ;
std::cout  ,  ;
// IP address
if (ioctl(sock, SIOCGIFADDR, ifr)==0) read_ip(ifr) ;
std::cout  '\n' ;
}
close(sock) ;
}
return 0 ;
}

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: ioctl(sock, SIOCGIFHWADDR, ifr) fails with 1.7

2009-06-12 Thread Corinna Vinschen
On Jun 12 14:30, Fr?d?ric Bron wrote:
 I used the following program to obtain mac address and ip of network
 adpaters. It works fine with 1.5 but not with 1.7.
 
 Output with 1.5:
 sock=3
 trying eth0
 mac: 00.1f.3c.57.XX.XX, ip: XXX.XXX.XXX.XXX
 trying eth1
 mac: 00.1d.09.df.XX.XX, ip: XXX.XXX.XXX.XXX
 trying eth2
 mac: 08.00.27.00.XX.XX, ip: XXX.XXX.XXX.XXX
 trying eth3
 ...
 
 Output with 1.7:
 sock=3
 trying eth0
 trying eth1
 trying eth2
 trying eth3
 ...
 
 Any clue?

Yes.  Your example application is wrongly assuming that the interfaces
are called eth0, eth1, ...

The problem is that this approach to use our own naming convention
for the Windows interfaces wasn't feasible anymore, when you're trying
to support IPv6, multicast and all that stuff.  I really thought long
and hard about this but I couldn't find a satisfing renaming approach
*and* could guarantee that the device names were unique over the entire
runtime, over multiple processes.

Therefore, with Windows XP and later, our device name is the unique
Windows device name.  This is a GUID string as, for instance,
{126B4C29-7A0E-41D1-81B4-C706C8F40128}. Yes, I know. this looks very
ugly, but it works.  I thought about using the interface name which you
see when you're in the Network Connections window, typically names
like Local Area Connection.  This is the so-called FriendlyName of
an interface.  However, the name can be empty, and it is not unique.

To fix your application, call either

  struct ifconf ifc;
  ifc.ifc_len = sizeof (struct ifreq) * 32;
  ifc.ifc_buf = malloc (ifc.ifc_len);
  if (ioctl (fd, SIOCGIFCONF, ifc))
/* Resize ifc_buf and retry */
  else
{
  struct ifreq *ifr = ifc.ifc_req;
  struct ifreq ifr2;
  for (int i = 0; i  ifc.ifc_len; i += sizeof (struct ifreq), ++ifr)
if (!ioctl (fd, SIOCGIFADDR, ifr2))
  /* Print result for that interface */
}

or, use the new getifaddrs() interface.  See
http://www.kernel.org/doc/man-pages/online/pages/man3/getifaddrs.3.html


Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/