iphone - 如何确定 iPhone 上的默认网关?

标签 iphone objective-c

我需要在 iPhone 应用程序中获取默认网关的 IP 地址。我找不到合适的 API。有人知道 iPhone 是否会公开此信息吗?

最佳答案

我已经解决了这个问题,虽然我不知道这个解决方案是否有效(不会被苹果拒绝)

首先我会给你我用来构建我的解决方案的资源:

http://code.google.com/p/doubango/source/browse/trunk/thirdparties/iphone/include/net/route.h?r=348

http://code.google.com/p/touchcode/source/browse/Experimental/TouchHTTPD/Externals/libnatpmp-20071213/getgateway.c?r=bc90b03205cbebb0633a7b5b203c2c6d5cb860dc

如果您认为 apple 会允许这种代码,那么得到一些反馈会很棒。

获取网关.h

#ifndef __GETGATEWAY_H__
#define __GETGATEWAY_H__

/* getdefaultgateway() :
 * return value :
 *    0 : success
 *   -1 : failure    */
int getdefaultgateway(in_addr_t * addr);

#endif

获取网关.c

#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

关于iphone - 如何确定 iPhone 上的默认网关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2300149/

相关文章:

ios - 测试文件是否存在的快速方法是什么?

iphone - 通过 UIViewController 以编程方式实现 UIView

IOS Objective C 祖 parent 作为委托(delegate)

ios - iOS 9 上存在 UITextField 和 Pixate Freestyle 崩溃

iPhone - 使用 AVAudioRecorder 显示音频记录反馈

iphone - 获取 Facebook 帐户详细信息

ios - cellForItemAtIndexPath , sizeForItemAtIndexPath , insetForSectionAtIndex 没有被调用 iOS

objective-c - 何时设置 Audio Session 属性

iphone - "Legality"在 UIWebView/Phonegap 中使用 web 资源和 appcache

iphone - 如何从 Objective C 中的域名中获取 IP 地址的域名和 IP 地址?