2014-09-30 75 views
2

你好我是一個ios開發者,我正在嘗試編程網絡套接字程序。 首先,我試圖找到一種方法來獲取arp表和icmp動作,如ping。 我在蘋果應用商店發現了許多優秀的網絡掃描儀,但我真的不知道從哪裏開始。ping和arp for ios development

所有我擔心的是appstore拒絕。

  • 我可以使用ios設備的system()函數嗎?
  • 我知道我不能使用原始套接字編程,如何在沒有原始套接字編程的情況下處理icmp和arp動作?

謝謝您的關注!

回答

4

https://github.com/mongizaidi/LAN-Scan

這個例子應該是不錯的開始。 (請參閱https://github.com/mongizaidi/LAN-Scan/blob/master/LAN%20Scan/SimplePing.m for pinging)

注意:
您無法獲取設備的MAC地址,但可以解析其他設備的Mac地址。
這裏是代碼,以解決主機的MAC地址(這會不會被蘋果拒絕)

#include <sys/param.h> 
#include <sys/file.h> 
#include <sys/socket.h> 
#include <sys/sysctl.h> 

#include <net/if.h> 
#include <net/if_dl.h> 
#include "if_types.h" 
#include "route.h" 
#include "if_ether.h" 
#include <netinet/in.h> 


#include <arpa/inet.h> 

#include <err.h> 
#include <errno.h> 
#include <netdb.h> 

#include <paths.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 

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




    } 
+1

太謝謝你了!仍然我正在尋找獲取MAC地址的ARP數據包。 – 2014-09-30 12:30:00

+0

自iOS7起,獲取mac地址受到限制,它應該只返回零。所以如果將有一些解決方法獲取MAC地址,它是100%的拒絕。 – l0gg3r 2014-09-30 13:01:53

+0

如果你想ARP獲取主機的MAC地址,我會編輯我的答案 – l0gg3r 2014-09-30 13:06:39