John Andersson
John Andersson

Reputation: 21

ping and arp for ios development

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.

thank you for your concern!

Upvotes: 2

Views: 2138

Answers (2)

Mojo66
Mojo66

Reputation: 1107

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

Upvotes: 0

l0gg3r
l0gg3r

Reputation: 8954

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




    }

Upvotes: 5

Related Questions