如何在iPhone上查询ARP表? [英] How do I query the ARP table on iPhone?

查看:255
本文介绍了如何在iPhone上查询ARP表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone开发的新手,我希望将wake-on-lan集成到我的应用程序中,而不会挤压我的用户在知道IP时输入计算机MAC地址。
我用谷歌搜索了几个小时,拿了一个ARP工具的源代码,但我不知道如何在iPhone上管理它。

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.

推荐答案

由于没有人回答我的问题......这是答案;)

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




    }


$ b来自苹果arp.c的$ b

在iphone上工作

from apples arp.c working on iphone

复制以下内容:

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

来自mac的

标题包含在项目文件夹中并添加到类中

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

这篇关于如何在iPhone上查询ARP表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆