3

I'm really hoping, that someone explains it to me. I'm writing an app that uses it's device mac address, and this code perfectly works on the simulator, but does not work on a device.

I got this code from the question Get router mac (without system call for ARP) in Objective-C

#include <stdio.h>

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <net/if_types.h>

char*  getMacAddress(char* macAddress, char* ifName) 
{
   int  success;
   struct ifaddrs *addrs;
   struct ifaddrs *cursor;
   const struct sockaddr_dl *dlAddr;
   const unsigned char* base;
   int i;

   success = getifaddrs(&addrs) == 0;
   if (success) 
   {
       cursor = addrs;
       while (cursor != 0) 
       {
           const struct sockaddr_dl *socAddr = 
           (const struct sockaddr_dl *)cursor->ifa_addr;
           _Bool afLinkFamily = (cursor->ifa_addr->sa_family == AF_LINK);
           /* Ethernet CSMA/CD */
           _Bool sdlIFTEther = (socAddr->sdl_type == IFT_ETHER);

           if ((afLinkFamily) && 
                sdlIFTEther &&
                strcmp(ifName,  cursor->ifa_name) == 0) 
           {
               dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
               base = 
                   (const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen];
               strcpy(macAddress, ""); 
               for (i = 0; i < dlAddr->sdl_alen; i++) 
               {
                   if (i != 0) 
                   {
                       strcat(macAddress, ":");
                   }
                   char partialAddr[3];
                   sprintf(partialAddr, "%02X", base[i]);
                   strcat(macAddress, partialAddr);

               }
           }
           cursor = cursor->ifa_next;
       }

       freeifaddrs(addrs);
   }    
   return macAddress;
}

an error it gives me:

'net/if_types.h' file not found

so my question is why is this happening, what is the difference between running on simulator and device? Thank you in advance.

Community
  • 1
  • 1
orsi
  • 159
  • 10

1 Answers1

1

The header file is simply missing in the iOS device SDK. Apple doesn't want you to use it, for whatever reason. If you just want the code to work, you can try to extract the required definition:

#define IFT_ETHER   0x6     /* Ethernet CSMACD */

and remove line

#include <net/if_types.h>

completely. Although this breaks the compatibility with platforms where this constant may be defined to some other value.

kuba
  • 7,329
  • 1
  • 36
  • 41
  • *"... although this breaks the compatibility with platforms where this constant may be defined to some other value"* - Well, grepping in `/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk` shows its not defined at all. So he's between a rock and a hard place. He might as well just iterate through possible values (like 0 through 10) and wait until he gets a good response back. Once he gets a good response, then check the interface properties to ensure the media is preferred. – jww Jan 09 '15 at 22:08