Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

objective c - Getting ARP table on iPhone/iPad

I am trying to get the ARP entries on my iPad like here.

When compiling the code to run on my iPad (so not the simulator) I am getting missing header error messages. You can resolve them by copying the header files into you project locally as mentioned in this post.

The problem lies in the line

sdl = (struct sockaddr_dl *)(sin + 1);

in this piece of code:

-(NSString*) ip2mac: (char*) ip 
{ 

    int expire_time, flags, export_only, doing_proxy, found_entry; 

    NSString *mAddr = nil; 
    u_long addr = inet_addr(ip); 
    int mib[6]; 
    size_t needed; 
    char *host, *lim, *buf, *next; 
    struct rt_msghdr *rtm; 
    struct sockaddr_inarp *sin; 
    struct sockaddr_dl *sdl; 
    extern int h_errno; 
    struct hostent *hp; 

    mib[0] = CTL_NET; 
    mib[1] = PF_ROUTE; 
    mib[2] = 0; 
    mib[3] = AF_INET; 
    mib[4] = NET_RT_FLAGS; 
    mib[5] = RTF_LLINFO; 
    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 
        err(1, "route-sysctl-estimate"); 
    if ((buf = malloc(needed)) == NULL) 
        err(1, "malloc"); 
    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 
        err(1, "actual retrieval of routing table"); 


    lim = buf + needed; 
    for (next = buf; next < lim; next += rtm->rtm_msglen) { 
        rtm = (struct rt_msghdr *)next; 
        sin = (struct sockaddr_inarp *)(rtm + 1); 
        sdl = (struct sockaddr_dl *)(sin + 1); 
        if (addr) { 
            if (addr != sin->sin_addr.s_addr) 
                continue; 
            found_entry = 1; 
        } 
        if (nflag == 0) 
            hp = gethostbyaddr((caddr_t)&(sin->sin_addr), 
                               sizeof sin->sin_addr, AF_INET); 
        else 
            hp = 0; 
        if (hp) 
            host = hp->h_name; 
        else { 
            host = "?"; 
            if (h_errno == TRY_AGAIN) 
                nflag = 1; 
        } 



        if (sdl->sdl_alen) { 

            u_char *cp = LLADDR(sdl); 

            mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]]; 


        //  ether_print((u_char *)LLADDR(sdl)); 
        } 
        else 

            mAddr = nil; 



    } 


    if (found_entry == 0) { 
        return nil; 
    } else { 
        return mAddr; 
    } 




} 

It gives the following error message:

Arithmetic on pointer to incomplete type 'struct sockaddr_inarp*'

When you compile the code for the iPad simulator everything runs fine.

Does anyone have an idea how to solve this? A similar question (but not solved) is asked here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#include <net/ethernet.h>

instead of messing with the original headers. More info here: Implicit declaration of function 'ether_ntoa' is invalid in C99


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...