2012-05-01 50 views
6

我試圖在我的iPad上獲得ARP條目,如here在iPhone/iPad上獲取ARP表

當編譯代碼在我的iPad上運行(所以不是模擬器)時,我正在丟失標題錯誤消息。您可以通過將頭文件複製到本地項目來解決這些問題,如post中所述。

問題在於行

SDL =(結構sockaddr_dl *)(SIN + 1);

在這段代碼

-(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; 
    } 




} 

它提供了以下錯誤消息:

算術上指針不完全型 '結構sockaddr_inarp *'

當編譯iPad模擬器的代碼一切正常。

有沒有人有一個想法如何解決這個問題? 一個類似的問題(但沒有解決)被要求here

+0

hi @Richard: - Iam獲取nflag作爲未聲明的標識符,我需要包含哪些文件/導入 – Bonnie

+0

@Richard:我是'objective-C'的新手,我想獲得連接到「熱點」的客戶端的「IP地址」。我找到了'ARP.c'和'ARP.h'。但如何使用它來獲取'ARP表'中的'IP地址'? – Martin

+0

不好意思,你把代碼放在.m文件中直接編譯? – Wun

回答

2

進口<netinet/if_ether.h>後,你應該編輯和更改線路

#include <net/if_arp.h> 

#include "if_arp.h" 

,然後導入<網/ if_arp.h >在您的項目以及。這應該可以解決這個錯誤。

反正你需要導入編譯您發佈的代碼的標題是:

#include "if_ether.h" 
#include "route.h" 
#include "if_arp.h" 
#include "if_dl.h" 

希望這有助於=)

編輯:

您需要「添加文件到工程」 ,而不是簡單地用#import或#include導入它。 您可以從以下鏈接找到上述文件: Files under "netinet" Files under "net"

+0

首先,我在頭文件中找不到條目。在我的系統上有多個版本。我的錯。在導入並修改了正確的之後,它就起作用了! :-) 非常感謝! –

+1

對於不知道在哪裏可以找到這些文件的用戶,它們位於/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/usr/include/當然,5.1將來可能會改變,但這些文件的位置可能不會。 –