How do I query the ARP table on iPhone?
Asked Answered
D

3

10

I am new on iPhone development, and I want to integrate wake-on-lan into my application without squeezing my users to enter the computers MAC address when the IP is already known. I googled for about some hours, took the source code of an ARP tool, but I don't know how to manage this on iPhone.

Daynadays answered 13/2, 2010 at 16:6 Comment(0)
D
21

Since nobody has answered my question... here is the answer ;)

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




    }

from apples arp.c working on iphone

Copy the following:

- "if_types.h"
- "route.h"
- "if_ether.h"

headers from mac includes into your project folder and add to classes

Daynadays answered 16/2, 2010 at 0:26 Comment(6)
Thanks for the tip. I only copied the "route.h" header, since the others are included in the SDKs (3.2+).Wildermuth
hi,@anthony iam getting nflag undeclared identifier error, what do i need to include for this to work.?Mizzen
just add in top of file static int nflag;Monaco
@anthony vage : Excuse me , I am new for Objective-C. I have add the above code in X-code project. How do you call the - (NSString*)ip2mac:(in_addr_t)addr after add to .m file ?Coronet
Will Apple approve this API?Shelah
Does apple approve such application..?Penman
K
8

I simplified the code and fixed a memory leak.

On my machine the header files where found in /usr/include/netinet and /usr/include/net.

- (NSString*)ip2mac:(in_addr_t)addr
{
    NSString *ret = nil;

    size_t needed;
    char *buf, *next;

    struct rt_msghdr *rtm;
    struct sockaddr_inarp *sin;
    struct sockaddr_dl *sdl;

    int mib[6];

    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, sizeof(mib) / sizeof(mib[0]), NULL, &needed, NULL, 0) < 0)
        err(1, "route-sysctl-estimate");

    if ((buf = (char*)malloc(needed)) == NULL)
        err(1, "malloc");

    if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &needed, NULL, 0) < 0)
        err(1, "retrieval of routing table");

    for (next = buf; next < buf + needed; 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 < 6)
            continue;

        u_char *cp = (u_char*)LLADDR(sdl);

        ret = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
               cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];

        break;
    }

    free(buf);

    return ret;
}
Kamenskuralski answered 17/5, 2011 at 17:40 Comment(1)
Can you please provide one simple demo code for it. Bcz i am not able to use it. ThanksNewport
M
2

I'm not sure the sysctl function is part of the public API. I've used the method obvious and my app was rejected for "non-public" API use. However I also don't believe there is any way to do this other than the methods shown above.

Moscow answered 15/6, 2011 at 16:14 Comment(3)
Hello new299, so if I am using this for a legal and public app it will get rejected from the app store?Democracy
It was for me, that was a while ago. Might be worth trying.Moscow
I know this is off topic, but might you have a direction to look for identifying devices on the network programmatically that will be accepted by the apple authorities? I've asked of SO hereDemocracy

© 2022 - 2024 — McMap. All rights reserved.