Objective c <;CFData 0x100516c60[0x7fff71adaea0]>;{length=6,capacity=6,bytes=0x0022412b03ad}
每次开始构建Xcode项目时,我都会在控制台中看到:Objective c <;CFData 0x100516c60[0x7fff71adaea0]>;{length=6,capacity=6,bytes=0x0022412b03ad},objective-c,xcode,cocoa,cfdata,Objective C,Xcode,Cocoa,Cfdata,每次开始构建Xcode项目时,我都会在控制台中看到: <CFData 0x100516c60 [0x7fff71adaea0]>{length = 6, capacity = 6, bytes = 0x0022412b03ad} {length=6,capacity=6,bytes=0x0022412b03ad} 我不知道这是什么意思,也不知道是什么原因造成的 有人能帮我吗 谢谢 编辑: 我认为这种方法创造了它: // // PrimaryMac.m // Network R
<CFData 0x100516c60 [0x7fff71adaea0]>{length = 6, capacity = 6, bytes = 0x0022412b03ad}
{length=6,capacity=6,bytes=0x0022412b03ad}
我不知道这是什么意思,也不知道是什么原因造成的
有人能帮我吗
谢谢
编辑:
我认为这种方法创造了它:
//
// PrimaryMac.m
// Network Radar
//
// Created by Daniel Diener on 06.07.11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "PrimaryMac.h"
#include <stdio.h>
#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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/network/IOEthernetInterface.h>
#include <IOKit/network/IONetworkInterface.h>
#include <IOKit/network/IOEthernetController.h>
@implementation PrimaryMac
@synthesize localMac, remoteMac;
static kern_return_t FindEthernetInterfaces(io_iterator_t *matchingServices);
static kern_return_t GetMACAddress(io_iterator_t intfIterator, UInt8 *MACAddress, UInt8 bufferSize);
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
//LOCAL MAC:::::::::::::::::::
// Returns an iterator containing the primary (built-in) Ethernet interface. The caller is responsible for
// releasing the iterator after the caller is done with it.
static kern_return_t FindEthernetInterfaces(io_iterator_t *matchingServices)
{
kern_return_t kernResult;
CFMutableDictionaryRef matchingDict;
CFMutableDictionaryRef propertyMatchDict;
// Ethernet interfaces are instances of class kIOEthernetInterfaceClass.
// IOServiceMatching is a convenience function to create a dictionary with the key kIOProviderClassKey and
// the specified value.
matchingDict = IOServiceMatching(kIOEthernetInterfaceClass);
// Note that another option here would be:
// matchingDict = IOBSDMatching("en0");
// but en0: isn't necessarily the primary interface, especially on systems with multiple Ethernet ports.
if (NULL == matchingDict) {
printf("IOServiceMatching returned a NULL dictionary.\n");
}
else {
// Each IONetworkInterface object has a Boolean property with the key kIOPrimaryInterface. Only the
// primary (built-in) interface has this property set to TRUE.
// IOServiceGetMatchingServices uses the default matching criteria defined by IOService. This considers
// only the following properties plus any family-specific matching in this order of precedence
// (see IOService::passiveMatch):
//
// kIOProviderClassKey (IOServiceMatching)
// kIONameMatchKey (IOServiceNameMatching)
// kIOPropertyMatchKey
// kIOPathMatchKey
// kIOMatchedServiceCountKey
// family-specific matching
// kIOBSDNameKey (IOBSDNameMatching)
// kIOLocationMatchKey
// The IONetworkingFamily does not define any family-specific matching. This means that in
// order to have IOServiceGetMatchingServices consider the kIOPrimaryInterface property, we must
// add that property to a separate dictionary and then add that to our matching dictionary
// specifying kIOPropertyMatchKey.
propertyMatchDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (NULL == propertyMatchDict) {
printf("CFDictionaryCreateMutable returned a NULL dictionary.\n");
}
else {
// Set the value in the dictionary of the property with the given key, or add the key
// to the dictionary if it doesn't exist. This call retains the value object passed in.
CFDictionarySetValue(propertyMatchDict, CFSTR(kIOPrimaryInterface), kCFBooleanTrue);
// Now add the dictionary containing the matching value for kIOPrimaryInterface to our main
// matching dictionary. This call will retain propertyMatchDict, so we can release our reference
// on propertyMatchDict after adding it to matchingDict.
CFDictionarySetValue(matchingDict, CFSTR(kIOPropertyMatchKey), propertyMatchDict);
CFRelease(propertyMatchDict);
}
}
// IOServiceGetMatchingServices retains the returned iterator, so release the iterator when we're done with it.
// IOServiceGetMatchingServices also consumes a reference on the matching dictionary so we don't need to release
// the dictionary explicitly.
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, matchingServices);
if (KERN_SUCCESS != kernResult) {
printf("IOServiceGetMatchingServices returned 0x%08x\n", kernResult);
}
return kernResult;
}
// Given an iterator across a set of Ethernet interfaces, return the MAC address of the last one.
// If no interfaces are found the MAC address is set to an empty string.
// In this sample the iterator should contain just the primary interface.
static kern_return_t GetMACAddress(io_iterator_t intfIterator, UInt8 *MACAddress, UInt8 bufferSize)
{
io_object_t intfService;
io_object_t controllerService;
kern_return_t kernResult = KERN_FAILURE;
// Make sure the caller provided enough buffer space. Protect against buffer overflow problems.
if (bufferSize < kIOEthernetAddressSize) {
return kernResult;
}
// Initialize the returned address
bzero(MACAddress, bufferSize);
// IOIteratorNext retains the returned object, so release it when we're done with it.
while ((intfService = IOIteratorNext(intfIterator)))
{
CFTypeRef MACAddressAsCFData;
// IONetworkControllers can't be found directly by the IOServiceGetMatchingServices call,
// since they are hardware nubs and do not participate in driver matching. In other words,
// registerService() is never called on them. So we've found the IONetworkInterface and will
// get its parent controller by asking for it specifically.
// IORegistryEntryGetParentEntry retains the returned object, so release it when we're done with it.
kernResult = IORegistryEntryGetParentEntry(intfService,
kIOServicePlane,
&controllerService);
if (KERN_SUCCESS != kernResult) {
printf("IORegistryEntryGetParentEntry returned 0x%08x\n", kernResult);
}
else {
// Retrieve the MAC address property from the I/O Registry in the form of a CFData
MACAddressAsCFData = IORegistryEntryCreateCFProperty(controllerService,
CFSTR(kIOMACAddress),
kCFAllocatorDefault,
0);
if (MACAddressAsCFData) {
CFShow(MACAddressAsCFData); // for display purposes only; output goes to stderr
// Get the raw bytes of the MAC address from the CFData
CFDataGetBytes(MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
CFRelease(MACAddressAsCFData);
}
// Done with the parent Ethernet controller object so we release it.
(void) IOObjectRelease(controllerService);
}
// Done with the Ethernet interface object so we release it.
(void) IOObjectRelease(intfService);
}
return kernResult;
}
- (void)createLocalMac{
kern_return_t kernResult = KERN_SUCCESS;
io_iterator_t intfIterator;
UInt8 MACAddress[kIOEthernetAddressSize];
kernResult = FindEthernetInterfaces(&intfIterator);
if (KERN_SUCCESS != kernResult) {
printf("FindEthernetInterfaces returned 0x%08x\n", kernResult);
}
else {
kernResult = GetMACAddress(intfIterator, MACAddress, sizeof(MACAddress));
if (KERN_SUCCESS != kernResult) {
printf("GetMACAddress returned 0x%08x\n", kernResult);
}
else {
localMac = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",
MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3], MACAddress[4], MACAddress[5]];
}
}
(void) IOObjectRelease(intfIterator); // Release the iterator.
}
//REMOTE MAC:::::::::::::::::::
- (void)createRemoteMac:(NSString *)ipAddr{
NSString *ret = nil;
in_addr_t addr = inet_addr([ipAddr UTF8String]);
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);
remoteMac = ret;
}
- (void)dealloc {
[remoteMac release];
[localMac release];
[super dealloc];
}
@end
//
//PrimaryMac.m
//网络雷达
//
//丹尼尔·迪纳于2011年7月6日创作。
//版权所有2011年uu MyCompanyName uuu。版权所有。
//
#导入“PrimaryMac.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“if_types.h”
#包括“路线h”
#包括“如果使用乙醚,则为h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
@实现PrimaryMac
@综合localMac、remoteMac;
静态内核返回查找网络接口(io迭代器匹配服务);
静态内核返回GetMACAddress(io迭代器intfIterator,UInt8*MACAddress,UInt8 bufferSize);
-(id)init
{
self=[super init];
如果(自我){
}
回归自我;
}
//本地MAC:
//返回包含主(内置)以太网接口的迭代器。打电话的人负责
//调用方使用迭代器后释放迭代器。
静态内核返回查找其他网络接口(io迭代器匹配服务)
{
kern_返回kern_t结果;
CFMutableDictionaryRef匹配dict;
CFMutableDictionaryRef propertyMatchDict;
//以太网接口是KioEthernetInterface类的实例。
//iOServiceMatch是一个方便的函数,可以使用kIOProviderClassKey键和
//指定的值。
matchingDict=IOServiceMatching(kIOEthernetInterfaceClass);
//请注意,这里的另一个选项是:
//matchingDict=IOBSDMatching(“en0”);
//但en0:不一定是主要接口,尤其是在具有多个以太网端口的系统上。
if(NULL==matchingDict){
printf(“IOServiceMatch返回一个空字典。\n”);
}
否则{
//每个IONetworkInterface对象都有一个键为kIOPrimaryInterface的布尔属性
//主(内置)接口的此属性设置为TRUE。
//IOServiceGetMatchingServices使用IOService定义的默认匹配条件
//仅以下特性加上此优先顺序的任何特定于族的匹配
//(请参阅IOService::被动匹配):
//
//kIOProviderClassKey(iServiceMatching)
//kIONameMatchKey(IOServiceNameMatching)
//kIOPropertyMatchKey
//基夫拉奇
//kIOMatchedServiceCountKey
//家庭特定匹配
//kIOBSDNameKey(IOBSDNameMatching)
//kIOLocationMatchKey
//IONetworkingFamily没有定义任何特定于族的匹配
/为了使IOSReVeGeMattServices服务考虑KioprimialIdulink属性,我们必须
//将该属性添加到单独的字典中,然后将其添加到匹配的字典中
//指定kIOPropertyMatchKey。
propertyMatchDict=CFDictionaryCreateMutable(kCFAllocatorDefault,0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if(NULL==propertyMatchDict){
printf(“CFDictionaryCreateMutable返回了一个空字典。\n”);
}
否则{
//在具有给定键的属性的字典中设置值,或添加键
//如果字典不存在,则调用。此调用保留传入的值对象。
CFDictionarySetValue(propertyMatchDict、CFSTR(kIOPrimaryInterface)、kCFBooleanTrue);
//现在将包含kIOPrimaryInterface匹配值的字典添加到我们的主
//匹配字典。此调用将保留propertyMatchDict,因此我们可以释放引用
//将其添加到matchingDict后,在propertyMatchDict上。
CFDictionarySetValue(matchingDict、CFSTR(kIOPropertyMatchKey)、propertyMatchDict);
CFRelease(propertyMatchDict);
}
}
//IOServiceGetMatchingServices保留返回的迭代器,因此在完成迭代器后释放它。
//IOServiceGetMatchingServices还使用匹配字典上的引用,因此我们不需要发布
//这本词典很明确。
kernResult=IOServiceGetMatchingServices(kIOMasterPortDefault、matchingDict、matchingServices);
if(KERN_SUCCESS!=kernsult){
printf(“IOServiceGetMatchingServices返回0x%08x\n”,结果);
}
返回结果;
}
//给定一组以太网接口的迭代器,返回最后一个接口的MAC地址。
//如果没有找到接口,MAC地址将设置为空字符串。
//在这个示例中,迭代器应该只包含主接口。
静态内核返回GetMACAddress(io迭代器intfIterator,UInt8*MACAddress,UInt8 bufferSize)
{
io_对象_t intfService;
io对象控制器服务;
kern_return_t kernResult=kern_失败;
//确保调用者提供足够的缓冲区空间。防止缓冲区溢出问题。
if(bufferSizeif (MACAddressAsCFData) {
//Comment out the CFShow here
CFShow(MACAddressAsCFData); // for display purposes only; output goes to stderr