Advertisement
priore

Get list of all interfaces on the iPhone - iPad Device

Aug 26th, 2014
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3. #include <netdb.h>
  4. #include <sys/types.h>
  5. #include <ifaddrs.h>
  6. #include <net/if.h>
  7.  
  8. - (NSArray *)localIPAddresses
  9. {
  10.     NSMutableArray *ipAddresses = [NSMutableArray array] ;
  11.    
  12.     struct ifaddrs *allInterfaces;
  13.    
  14.     // Get list of all interfaces on the local machine:
  15.     if (getifaddrs(&allInterfaces) == 0) {
  16.         struct ifaddrs *interface;
  17.        
  18.         // For each interface ...
  19.         for (interface = allInterfaces; interface != NULL; interface = interface->ifa_next) {
  20.             unsigned int flags = interface->ifa_flags;
  21.             struct sockaddr *addr = interface->ifa_addr;
  22.            
  23.             // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
  24.             if ((flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING)) {
  25.                 if (addr->sa_family == AF_INET || addr->sa_family == AF_INET6) {
  26.                    
  27.                     // Convert interface address to a human readable string:
  28.                     char host[NI_MAXHOST];
  29.                     getnameinfo(addr, addr->sa_len, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
  30.                    
  31.                     [ipAddresses addObject:[[NSString alloc] initWithUTF8String:host]];
  32.                 }
  33.             }
  34.         }
  35.        
  36.         freeifaddrs(allInterfaces);
  37.     }
  38.    
  39.     return ipAddresses;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement