views:

835

answers:

3

Hi,

i am new on iPhone development. I want to integrate wake on lan into my app without squeezing my users to enter the computers mac adress when the ip is already known. I googled for about some hours, take the source code of apples arp tool but i don't know howto manage this on iPhone. I am grateful for any help.

Thanks! anthonyvage

+4  A: 

Since nobody ha 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:

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

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

anthony vage
Thanks for the tip. I only copied the "route.h" header, since the others are included in the SDKs (3.2+).
Laurent Etiemble
A: 

Do you happen to have a simple example app of this perhaps dumping to NSLog? I've been trying to figure to figure this out with my novice skills without much luck. Thanks in advance.

Steve Geller
A: 

i get the following errors

/if_ether.h:71:24: error: net/if_arp.h: No such file or directory

/if_ether.h:115: error: field 'ea_hdr' has incomplete type

i included the three header files from this path

developer/SDks/MACOS10.6.sdk/system/library/frameworks/kernel.framework/headers/net/

anyone???