2

Hello I'm an ios developer, and I'm trying to programming network socket program. First I'm trying to find a way for get arp table, and icmp action such as a ping. I found many of good network scanners at apple appstores, but I really don't know where I should start from.

All that I'm worrying about is appstore rejection.

  • can I use system() function for ios devices?
  • I know I can't use raw socket programming, how can I handle icmp and arp action without raw socket programming?

thank you for your concern!

2 Answers2

5

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

This example should be good for starting. (See https://github.com/mongizaidi/LAN-Scan/blob/master/LAN%20Scan/SimplePing.m for pinging)

Note:
You can't get your device's MAC address, but you can resolve another device Mac address.
Here is code to resolve Mac address of host (This will not be rejected by Apple)

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




    }
l0gg3r
  • 8,864
  • 3
  • 26
  • 46
  • 1
    Thank you so much! still I'm looking for arp packets for get mac addresses. – John Andersson Sep 30 '14 at 12:30
  • getting mac address is restricted since iOS7, it should return you only zeros. so if there will be some workaround for getting mac address it's 100% rejection. – l0gg3r Sep 30 '14 at 13:01
  • If you want arp to get hosts mac address, i will edit my answer – l0gg3r Sep 30 '14 at 13:06
  • 1
    Thank you so much for your answer! I'll try to apply on my device, I hope it's gonna work and fine with apple's appstore guideline. Thank you! – John Andersson Sep 30 '14 at 15:38
  • Guys, Im getting error, "Arithmetic on a pointer to an incomplete type 'struct_sockaddr_inarp'". Sorry I am absolutely noob in this. Can you help me here? – Pankaj Gaikar Jun 08 '16 at 10:40
0

I've cleaned up @logg3r's answer:

#import <sys/sysctl.h>
#import <net/route.h>
#import <netinet/if_ether.h>
#import <net/if_dl.h>

+ (NSString *) ip2mac: (in_addr_t) addr {
    size_t size;
    char *lim, *buf, *next;
    struct rt_msghdr *rtm;
    struct sockaddr_inarp *sin;
    struct sockaddr_dl *sdl;
    
    static int  mib[6] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_LLINFO};
    
    if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) return nil;
    if ((buf = malloc(size)) == NULL)  return nil;
    if (sysctl(mib, 6, buf, &size, NULL, 0) < 0) return nil;
    
    lim = buf + size;
    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 == sin->sin_addr.s_addr && sdl->sdl_alen) {
            return [NSString stringWithUTF8String:link_ntoa(sdl)];
        }
    }
    return nil;
}
Mojo66
  • 1,109
  • 12
  • 21