如何获得iPhone上的WIFI网关地址?

我需要得到我连接到iPhone的WiFinetworking的网关地址。 任何人都知道如何得到这个?

只是为了澄清,我正在寻找这个屏幕的信息:

在这里输入图像描述

谢谢。

这对我工作,但我需要得到我的项目中的route.h副本

我的一般理解是,这个代码查询和检索和路由表,并使用其条目来确定默认路由aka网关ip

/* $Id: getgateway.c,v 1.6 2007/12/13 14:46:06 nanard Exp $ */ /* libnatpmp * Copyright (c) 2007, Thomas BERNARD <miniupnp@free.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <stdio.h> #include <netinet/in.h> #include <stdlib.h> #include <sys/sysctl.h> #include "getgateway.h" #include "route.h" #include <net/if.h> #include <string.h> #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)) 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; } #endif 

http://opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/net/route.h添加到您的项目;route.h文件

创buildgetgateway.h

 int getdefaultgateway(in_addr_t * addr); 

创buildgetgateway.c

 #include <stdio.h> #include <netinet/in.h> #include <stdlib.h> #include <sys/sysctl.h> #include "getgateway.h" #include "TargetConditionals.h" #if TARGET_IPHONE_SIMULATOR #include <net/route.h> #define TypeEN "en1" #else #include "route.h" #define TypeEN "en0" #endif #include <net/if.h> #include <string.h> #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)) 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(TypeEN,ifName)==0){ *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr; r = 0; } } } } free(buf); } return r; } #endif 

通过下面的代码片断,这个例子在模拟器和devise上将会很好的工作。

 #include "TargetConditionals.h" #if TARGET_IPHONE_SIMULATOR #include <net/route.h> #define TypeEN "en1" #else #include "route.h" #define TypeEN "en0" #endif 

在Objective C项目中使用此代码

 #import "getgateway.h" #import <arpa/inet.h> - (NSString *)getGatewayIP { NSString *ipString = nil; struct in_addr gatewayaddr; int r = getdefaultgateway(&(gatewayaddr.s_addr)); if(r >= 0) { ipString = [NSString stringWithFormat: @"%s",inet_ntoa(gatewayaddr)]; NSLog(@"default gateway : %@", ipString ); } else { NSLog(@"getdefaultgateway() failed"); } return ipString; } 

在与Apple联系后,SDK不提供简单的方法。 如果有的话,困难的方法是深入挖掘系统或使用traceroute。

我提交了一个错误报告,也许他们会在将来添加它。

看一下Objective-C的答案:如何获取路由器地址?

也许只是切线相关,但看到这篇博客文章中列出的技术: http : //blog.zachwaugh.com/post/309927273/programmatically-retrieving-ip-address-of-iphone

它获得了Wi-Fi接口的IP地址,可能是寻找另一个解决scheme的另一个起点。

 - (NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // retrieve the current interfaces - returns 0 on success success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked list of interfaces temp_addr = interfaces; while(temp_addr != NULL) { if(temp_addr->ifa_addr->sa_family == AF_INET) { // Check if interface is en0 which is the wifi connection on the iPhone if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // Get NSString from C String address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); return address; }