How can I determine the default gateway on iPhone?

后端 未结 5 725
栀梦
栀梦 2020-12-03 09:31

I need to obtain the IP address of the default gateway in an iPhone application. I cannot find the proper API. Anyone know if iPhone exposes this information?

5条回答
  •  一个人的身影
    2020-12-03 09:57

    for those who need human readable IP, i modified my getgateway.c as follow:

    add to includes:

    #include 
    

    inside the last if statement, between *addr and r=0;

    if(strcmp("en0",ifName)==0){
    
    *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
    
    char str[INET_ADDRSTRLEN];
    inet_ntop(AF_INET, &(((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr), str, INET_ADDRSTRLEN); // supports IPv6
    printf("IP: %s\n", str); // prints "192.0.2.33"
    
    r = 0;
    }
    

提交回复
热议问题