Objective-C:如何获取路由器地址? [英] Objective-C : How to fetch the router address?

查看:264
本文介绍了Objective-C:如何获取路由器地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以这种方式获取路由器地址。

I tried to fetch the router address this way.

- (NSString *) routerIp {

  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 //ifa_addr
          address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];
        }
      }

      temp_addr = temp_addr->ifa_next;
    }
  }

  // Free memory
  freeifaddrs(interfaces);

  return address;
}

路由器地址总是看起来像xxx.xxx.255.255但它应该是看起来像xxx.xxx.0.1或者这样......

The router address always looks like xxx.xxx.255.255 but it is supposed to look like xxx.xxx.0.1 or something this way...

有什么办法可以获得有效地址吗?

Is there anything to do to get the valid address?

感谢您的帮助!

推荐答案

我一直在寻找获取IP地址的方法默认网关我自己。我将专注于此 - 我不需要获取默认路由器的MAC地址,因此这只能部分回答OP的问题。

I've been looking for a way to get the IP address of the default gateway myself. I'm going to focus just on this - I didn't need to get the MAC address of the default router, so this only partially answers the OP's question.

I不喜欢解析netstat输出的想法。此外,您无法根据计算机上的接口所具有的IP地址获取默认路由。与您的相同范围内的任何IP地址都可以是默认网关 - 唯一的规则是您的IP和默认网关都必须属于同一子网。

I didn't like the idea of parsing the netstat output. Also, you can't get a default route based on the IP address an interface on your machine has. Any IP address within the same range as yours can be a default gateway - the only rule is that both your IP and the default gateway need to be part of the same subnet.

我想要确保的另一件事是获取默认网关的IP地址,即使我的本地接口有多个分配了IP地址(例如,我同时连接到Wifi和有线以太网,两个接口都已启动,其上有IP地址)。对于我的应用程序,默认路由通过哪个接口设置无关紧要(它可以是en0,en1,ppp0或其他任何东西)。

One more thing I wanted to ensure is to get an IP address of the default gateway even if more than one of my local interfaces has an IP address assigned to it (e.g. I'm connected to the Wifi and to the wired Ethernet at the same time, and both interfaces are up, with an IP address on them). For my application, it doesn't matter which interface the default route is set via (it can be en0, en1, ppp0 or anything else at all).

我认为获得该信息的最佳方式(也是大多数类似苹果的方式)是使用系统配置框架 - 此线程中上面帖子中的链接指向我那方向,但没有真正详细说明如何使用它,我没有发现它上面的Apple文档非常有用(至少考虑到我的技能水平)。

I think the best (and most Apple-like) way to get that info is to use System Configuration Framework - the links in the post above in this thread pointed me in that direction, but didn't really give much detail as to how to use it, and I didn't find the Apple documentation on it very useful (at least taking into consideration my skill level).

请注意我在Objective-C方面不是很有经验 - 我正在编写一个我自己需要的应用程序(我找不到的应用程序) ),对于那个应用程序,我只需要默认网关的IP地址。

Please mind I'm not very experienced in Objective-C - I'm in the middle of writing an app that I need myself (and which I couldn't find), and for that app I just need the IP address of the default gateway.

所以 - 这就是我在我的应用程序中所做的,它似乎工作正常(加上比我到目前为止找到的大多数其他解决方案更短更简单:

So - here's what I'm doing in my app, and it seems to be working fine (plus is much shorter and simpler than most other solutions I found so far:

- (NSString *)defaultRouter {

    SCDynamicStoreRef ds = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myapp"), NULL, NULL);
    CFDictionaryRef dr = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/IPv4"));
    CFStringRef router = CFDictionaryGetValue(dr, CFSTR("Router"));
    NSString *routerString = [NSString stringWithString:(__bridge NSString *)router];
    CFRelease(dr);
    CFRelease(ds);

    return routerString;
}

请注意,在我的应用程序中,上面是更大的一部分方法(我实际上没有一个defaultRouter:方法)。另外,为了简洁起见,我省略了一些检查(比如[routerString length]等等)。

Please note that in my app, the above is part of a larger method (I don't actually have a defaultRouter: method in there). Also, I've omitted some of the checks (like [routerString length] and so on) for brevity.

希望有人觉得这很有用。拜托也可以随意修复上面代码中可能出现的任何错误 - 我还是新手!

Hopefully someone finds this useful. Please also feel free to fix any errors I might have in the code above - I'm still a newbie!

PS。当我检查'scutil'的输出时,我知道要查找什么,它使用系统配置框架本身:

PS. I got an idea of what to look for when I checked the output of 'scutil', which uses the System Configuration Framework itself:

MacBook:~$ scutil 
> show State:/Network/Global/IPv4
<dictionary> {
  PrimaryInterface : en1
  PrimaryService : <service_id>
  Router : <ipv4_address>
}
> show State:/Network/Global/IPv6
<dictionary> {
  PrimaryInterface : en1
  PrimaryService : <service_id>
  Router : <ipv6_address>
}

这篇关于Objective-C:如何获取路由器地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆