ioctl(sock, SIOCGIFHWADDR, &ifr) fails with 1.7

Corinna Vinschen corinna-cygwin@cygwin.com
Fri Jun 12 14:26:00 GMT 2009


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/



More information about the Cygwin mailing list