15
A
回答
2
检查回来的苹果后,SDK不提供一个简单的方法来做到这一点。如果有的话,困难的方法是深入挖掘系统或使用traceroute。
我提交了一个错误报告,也许他们会在未来添加它。
0
看看答案Objective-C : How to fetch the router address?
也许只有切向相关,但看到这个博客帖子描述的技术:http://blog.zachwaugh.com/post/309927273/programmatically-retrieving-ip-address-of-iphone
它获得了Wi-Fi接口的IP地址,并可能被一个又一个跳板,把寻找另一种解决方案....
11
这对我的作品,但我需要得到route.h的拷贝在我的项目
我一般的理解是,这个代码查询,检索和路由表 并使用它的条目来确定默认又名网关IP路由
/* $Id: getgateway.c,v 1.6 2007/12/13 14:46:06 nanard Exp $ */
/* libnatpmp
* Copyright (c) 2007, Thomas BERNARD <[email protected]>
*
* 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
12
添加到您的项目route.h文件从http://opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/net/route.h
创建getgateway.h
int getdefaultgateway(in_addr_t * addr);
创建getgateway.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
可以通过下面的片段中,这个例子将是在模拟器和谋善的工作。
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
#include <net/route.h>
#define TypeEN "en1"
#else
#include "route.h"
#define TypeEN "en0"
#endif
使用此代码在您的目标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;
}
相关问题
- 1. 如何获取WiFi网络接口的MAC地址?
- 2. C#获得iPad/iPhone MAC/Wifi地址
- 3. 在LUA中获取网关地址esp8266
- 4. 在C/C++中获取网关地址
- 5. 如何从网络上的IP地址获取MAC地址?
- 6. 子网划分时如何获取网关地址?
- 7. 如何在android中获取WIFI接口的MAC地址?
- 8. 如何在Windows应用商店获取WiFi MAC地址应用
- 9. 如何在Android Marshmallow中获取缺少的Wifi MAC地址?
- 10. 获取我设备的wifi mac地址?
- 11. 如何在LINUX中获取目标地址的默认网关?
- 12. 获取蓝牙MAC上的iPhone地址
- 13. 从默认网关获取mac地址?
- 14. 在Android模拟器上获取WIFI网络中的计算机IP地址
- 15. 获取android以太网MAC地址(不是wifi接口)
- 16. 如何仅获取IP地址/子网
- 17. 在网址上获取%3F
- 18. 获取有关WiFi网络的信息
- 19. 使用bash在Ubuntu或Debian上获取IP地址信息(网关和子网)
- 20. 如何在API网关中获取请求网址?
- 21. 在Android上检测wifi的IP地址?
- 22. iPhone:您如何获得iPhone上所有地址簿的名称?
- 23. 从网址获取IP地址
- 24. 如何将套接字绑定到Android上的本地wifi网络地址?
- 25. 如何获取RTSP网址?
- 26. 如何获取IP地址?
- 27. 如何获取IP地址?
- 28. 如何获取动态谷歌地图网址(有静态地图网址)
- 29. iPhone - 如何找到连接的WiFi接入点的MAC地址?
- 30. 如何从iPhone的在线网址获取音频播放
一种技术,虽然也许有点特殊,在这里列出http://stackoverflow.com/questions/2300149/how-can-i -determine-the-default-gateway-on-iphone – 2011-02-02 08:31:29