Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.37 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Migrating to a new way of identifying devices
  2. #include <sys/socket.h>
  3. #include <sys/sysctl.h>
  4. #include <net/if.h>
  5. #include <net/if_dl.h>
  6.  
  7. - (NSString *)getMacAddress
  8. {
  9.   int                 mgmtInfoBase[6];
  10.   char                *msgBuffer = NULL;
  11.   size_t              length;
  12.   unsigned char       macAddress[6];
  13.   struct if_msghdr    *interfaceMsgStruct;
  14.   struct sockaddr_dl  *socketStruct;
  15.   NSString            *errorFlag = NULL;
  16.  
  17.   // Setup the management Information Base (mib)
  18.   mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
  19.   mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
  20.   mgmtInfoBase[2] = 0;
  21.   mgmtInfoBase[3] = AF_LINK;        // Request link layer information
  22.   mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces
  23.  
  24.   // With all configured interfaces requested, get handle index
  25.   if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
  26.     errorFlag = @"if_nametoindex failure";
  27.   else
  28.   {
  29.     // Get the size of the data available (store in len)
  30.     if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
  31.       errorFlag = @"sysctl mgmtInfoBase failure";
  32.     else
  33.     {
  34.       // Alloc memory based on above call
  35.       if ((msgBuffer = malloc(length)) == NULL)
  36.         errorFlag = @"buffer allocation failure";
  37.       else
  38.       {
  39.         // Get system information, store in buffer
  40.         if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
  41.           errorFlag = @"sysctl msgBuffer failure";
  42.       }
  43.     }
  44.   }
  45.  
  46.   // Befor going any further...
  47.   if (errorFlag != NULL)
  48.   {
  49.     NSLog(@"Error: %@", errorFlag);
  50.     return errorFlag;
  51.   }
  52.  
  53.   // Map msgbuffer to interface message structure
  54.   interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
  55.  
  56.   // Map to link-level socket structure
  57.   socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
  58.  
  59.   // Copy link layer address data in socket structure to an array
  60.   memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
  61.  
  62.   // Read from char array into a string object, into traditional Mac address format
  63.   NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
  64.                                 macAddress[0], macAddress[1], macAddress[2],
  65.                                 macAddress[3], macAddress[4], macAddress[5]];
  66.   NSLog(@"Mac Address: %@", macAddressString);
  67.  
  68.   // Release the buffer memory
  69.   free(msgBuffer);
  70.  
  71.   return macAddressString;
  72. }