SHARE
TWEET

How to get router IP address in Objective-C

priore Feb 27th, 2016 (edited) 559 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <netinet/in.h>
  3. #include <stdlib.h>
  4. #include <sys/sysctl.h>
  5. #include <net/if.h>
  6. #include <string.h>
  7. #include <arpa/inet.h>
  8.  
  9. #if TARGET_IPHONE_SIMULATOR
  10. #include <net/route.h>
  11. #else
  12. #include "route.h"
  13. #endif
  14.  
  15. #define CTL_NET         4               /* network, see socket.h */
  16.  
  17. #if defined(BSD) || defined(__APPLE__)
  18.  
  19. #define ROUNDUP(a) \
  20. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  21.  
  22. static int getdefaultgateway(in_addr_t * addr)
  23. {
  24.     int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
  25.         NET_RT_FLAGS, RTF_GATEWAY};
  26.     size_t l;
  27.     char * buf, * p;
  28.     struct rt_msghdr * rt;
  29.     struct sockaddr * sa;
  30.     struct sockaddr * sa_tab[RTAX_MAX];
  31.     int i;
  32.     int r = -1;
  33.     if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
  34.         return -1;
  35.     }
  36.     if(l>0) {
  37.         buf = malloc(l);
  38.         if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
  39.             return -1;
  40.         }
  41.         for(p=buf; p<buf+l; p+=rt->rtm_msglen) {
  42.             rt = (struct rt_msghdr *)p;
  43.             sa = (struct sockaddr *)(rt + 1);
  44.             for(i=0; i<RTAX_MAX; i++) {
  45.                 if(rt->rtm_addrs & (1 << i)) {
  46.                     sa_tab[i] = sa;
  47.                     sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len));
  48.                 } else {
  49.                     sa_tab[i] = NULL;
  50.                 }
  51.             }
  52.  
  53.             if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY))
  54.                && sa_tab[RTAX_DST]->sa_family == AF_INET
  55.                && sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) {
  56.  
  57.  
  58.                 if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) {
  59.                     char ifName[128];
  60.                     if_indextoname(rt->rtm_index,ifName);
  61.  
  62.                     if(strcmp("en0",ifName)==0){
  63.  
  64.                         *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
  65.                         r = 0;
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         free(buf);
  71.     }
  72.     return r;
  73. }
  74.  
  75. static NSString* routerIP()
  76. {
  77.     struct in_addr gatewayaddr;
  78.     int r = getdefaultgateway(&(gatewayaddr.s_addr));
  79.     if (r >= 0) {
  80.         return [NSString stringWithFormat: @"%s",inet_ntoa(gatewayaddr)];
  81.     }
  82.  
  83.     return nil;
  84. }
  85.  
  86. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top