I have been looking on StackOverflow but I have not yet found a way to get the Wifi router IP address in iOS programatically. Is this possible? If so, how would I go about doing it?
Asked
Active
Viewed 3,600 times
1
-
http://stackoverflow.com/questions/30748480/swift-get-devices-ip-address – Shades Feb 28 '16 at 02:05
-
does that give the wifi's router number or the phone's unique ip address? – Jared Feb 28 '16 at 02:15
1 Answers
2
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <string.h>
#include <arpa/inet.h>
#if TARGET_IPHONE_SIMULATOR
#include <net/route.h>
#else
#include "route.h"
#endif
#define CTL_NET 4 /* network, see socket.h */
#if defined(BSD) || defined(__APPLE__)
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
static int getdefaultgateway(in_addr_t * addr)
{
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
NET_RT_FLAGS, RTF_GATEWAY};
size_t l;
char * buf, * p;
struct rt_msghdr * rt;
struct sockaddr * sa;
struct sockaddr * sa_tab[RTAX_MAX];
int i;
int r = -1;
if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
return -1;
}
if(l>0) {
buf = malloc(l);
if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
return -1;
}
for(p=buf; p<buf+l; p+=rt->rtm_msglen) {
rt = (struct rt_msghdr *)p;
sa = (struct sockaddr *)(rt + 1);
for(i=0; i<RTAX_MAX; i++) {
if(rt->rtm_addrs & (1 << i)) {
sa_tab[i] = sa;
sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len));
} else {
sa_tab[i] = NULL;
}
}
if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY))
&& sa_tab[RTAX_DST]->sa_family == AF_INET
&& sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) {
if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) {
char ifName[128];
if_indextoname(rt->rtm_index,ifName);
if(strcmp("en0",ifName)==0){
*addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
r = 0;
}
}
}
}
free(buf);
}
return r;
}
static NSString* routerIP()
{
struct in_addr gatewayaddr;
int r = getdefaultgateway(&(gatewayaddr.s_addr));
if (r >= 0) {
return [NSString stringWithFormat: @"%s",inet_ntoa(gatewayaddr)];
}
return nil;
}
#endif
extected from https://stackoverflow.com/a/9173849/4069848
UPDATE: conditional compilation for route.h
UPDATE: if you want to get the gateway of one specific interface, just change the interface name on this line:
if(strcmp("en0",ifName)==0){
-
1
-
@Jared I have tested on a new empty xcode project and running fine on simulator – Danilo Feb 28 '16 at 03:05
-
-
1update: the conditional compilation for router.h. There's nothing to import, I put the code in a .h with only foundation.h – Danilo Feb 28 '16 at 15:25
-
-
-
1Dont forget copy /usr/include/net/route.h to your project, if you encounter the error "route.h" not found – Victor Choy Mar 21 '18 at 07:05
-
Does copying route.h into my project have any licensing implications? – AdeleGoldberg Mar 09 '20 at 15:16
-
1@AdeleGoldberg the .h file is just a declaration of the functions to use, every time the code is compiled, the latest function prototypes declared in .h file will be included in the files using them, avoiding potentially disastrous errors, all compilers work like this and it is a predefined file made available by the development environment for developers. – Danilo Mar 10 '20 at 20:18
-