【问题标题】:How to get router IP address in Objective C? [duplicate]如何在Objective C中获取路由器IP地址? [复制]
【发布时间】:2016-02-28 01:41:38
【问题描述】:

我一直在查看 StackOverflow,但我还没有找到一种以编程方式在 iOS 中获取 Wifi 路由器 IP 地址的方法。这可能吗?如果是这样,我将如何去做?

【问题讨论】:

标签: ios objective-c wifi


【解决方案1】:
#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

来自https://stackoverflow.com/a/9173849/4069848

UPDATE:route.h 的条件编译

UPDATE:如果你想得到一个特定接口的网关,只需在这一行更改接口名称:

if(strcmp("en0",ifName)==0){

【讨论】:

  • 它说 net/route.h not found?
  • @Jared 我已经在一个新的空 xcode 项目上进行了测试,并且在模拟器上运行良好
  • 更新:router.h 的条件编译。没有什么要导入的,我把代码放在一个只有foundation.h的.h中
  • 如果遇到错误“route.h”未找到,请不要忘记将 /usr/include/net/route.h 复制到您的项目中
  • @AdeleGoldberg .h 文件只是要使用的函数的声明,每次编译代码时,.h 文件中声明的最新函数原型将包含在使用它们的文件中,避免潜在的灾难性错误,所有编译器都是这样工作的,它是开发环境为开发人员提供的预定义文件。
猜你喜欢
  • 2011-01-07
  • 2015-03-10
  • 2021-08-14
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 2011-01-02
相关资源
最近更新 更多