大佬教程收集整理的这篇文章主要介绍了在Objective-C中获取路由器mac(没有ARP系统调用),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对目标C完全不熟悉,我正在寻找一种更好的方法来做我已经在做的事情.
我正在调用“arp -a”并通过“Task”解析结果:
NSTask *task; task = [[NSTask alloc] init]; [task setLaunchPath: @"/usr/sbin/arp"]; NSArray *arguments; arguments = [NSArray arrayWithObjects: @"-a",nil]; [task setArguments: arguments]; NSPipe *pipe; pipe = [NSPipe pipe]; [task setStandardOutput: pipe]; NSFileHandle *file; file = [pipe fileHandleForReading]; [task launch]; NSData *data; data = [file readDataToEndOfFile];
我担心这不是很有效率.
有什么建议么?我每5秒运行一次此代码块.
您应该能够简单地复制粘贴大部分功能……而不是打印结果,只需存储原始地址.
编辑:这是源的精简版,只运行相当于arp -a.这应该编译没有任何特殊指令.
/* * Copyright (C) 1999 Apple Computer,Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software diStributed under the License are * diStributed on an 'AS IS' basis,WITHOUT WARRANTY OF ANY KIND,EITHER * EXPRESS OR IMPLIED,AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,* INCLUDING WITHOUT LIMITATION,ANY WARRANTIES OF MERCHANTABILITY,* FITnesS FOR A PARTICULAR PURPOSE,QUIET ENJOymENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #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 <net/if_types.h> #include <net/route.h> #include <neTinet/in.h> #include <neTinet/if_ether.h> #include <arpa/inet.h> #include <err.h> #include <errno.h> #include <netdb.h> #include <nlist.h> #include <paths.h> #include <stdio.h> #include <stdlib.h> #include <String.h> #include <unistd.h> static int nflag; void ether_print(cp) u_char *cp; { printf("%x:%x:%x:%x:%x:%x",cp[0],cp[1],cp[2],cp[3],cp[4],cp[5]); } /* * Dump the entire arp table */ int dump(addr) u_long addr; { 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; int found_entry = 0; 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,0) < 0) err(1,"route-sysctl-estimate"); if ((buf = malloc(needed)) == NULL) err(1,"malloc"); if (sysctl(mib,buf,"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; } printf("%s (%s) at ",host,inet_ntoa(sin->sin_addr)); if (sdl->sdl_alen) ether_print((u_char *)LLADDR(sdl)); else printf("(incompletE)"); if (rtm->rtm_rmx.rmx_expire == 0) printf(" peRMANent"); if (sin->sin_other & SIN_PROXY) printf(" published (proxy only)"); if (rtm->rtm_addrs & RTA_NETMASK) { sin = (struct sockaddr_inarp *) (sdl->sdl_len + (char *)sdl); if (sin->sin_addr.s_addr == 0xffffffff) printf(" published"); if (sin->sin_len != 8) printf("(weird)"); } printf("\n"); } return (found_entry); } int main (int argc,char const *argv[]) { dump(0); return 0; }
以上是大佬教程为你收集整理的在Objective-C中获取路由器mac(没有ARP系统调用)全部内容,希望文章能够帮你解决在Objective-C中获取路由器mac(没有ARP系统调用)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。