how to get mac address

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