Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

野指针了啊 #2

Open
buzai opened this issue Jun 20, 2016 · 0 comments
Open

野指针了啊 #2

buzai opened this issue Jun 20, 2016 · 0 comments

Comments

@buzai
Copy link

buzai commented Jun 20, 2016

  • (NSString *) gateway2{
    int mib[6] = {CTL_NET,
    PF_ROUTE,//其实就是af_route的define
    0, AF_INET,
    NET_RT_FLAGS, RTF_GATEWAY};
    size_t len;
    NSAssert(sysctl(mib,sizeof(mib)/sizeof(int), 0, &len, 0, 0) >= 0, @"取长度出错");
    // NSLog(@"sysctl buf长度%zd",len);
    char *buf = calloc(len, 1);
    NSAssert(sysctl(mib,sizeof(mib)/sizeof(int), buf, &len, 0, 0) >= 0, @"取长度出错");
    // NSLog(@"实际内容长度%zd",len);
    struct rt_msghdr *rtptr = (struct rt_msghdr *)buf;//routeptr, 后面解析数据就一样的了
    struct sockaddr *sa = (struct sockaddr *)(rtptr + 1);
    struct sockaddr_in *gateway = (struct sockaddr_in *)((char *)sa + sa->sa_len);//第一个是RTAX_DST, 我们只要网关
    free(rtptr);
    // NSLog(@"%s",inet_ntoa(gateway->sin_addr));
    return [NSString stringWithUTF8String:inet_ntoa(gateway->sin_addr)];
    }

    return [NSString stringWithUTF8String:inet_ntoa(gateway->sin_addr)];
    返回的时候报错了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant