ifa解析

+ (NSDictionary *)getIfaList {

NSMutableDictionary* ifaList = [[NSMutableDictionary alloc] init];

struct ifaddrs *interfaces = NULL;

struct ifaddrs *temp_addr = NULL;

@try {

// retrieve the current interfaces - returns 0 on success

NSInteger success = getifaddrs(&interfaces);

//NSLog(@"%@, success=%d", NSStringFromSelector(_cmd), success);

if (success == 0) {

// Loop through linked list of interfaces

temp_addr = interfaces;

while(temp_addr != NULL) {

if(temp_addr->ifa_addr->sa_family == AF_INET)

{

// Get NSString from C String

NSString *ifaName = [NSString stringWithUTF8String:temp_addr->ifa_name];

NSString *address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *) temp_addr->ifa_addr)->sin_addr)];

NSString *mask = @"";

if (temp_addr->ifa_netmask)

mask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *) temp_addr->ifa_netmask)->sin_addr)];

NSString *gateway = @"";

if (temp_addr->ifa_dstaddr)

gateway = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *) temp_addr->ifa_dstaddr)->sin_addr)];

LTNetIfa *ifa = [[LTNetIfa alloc] init];

ifa.name = ifaName;

ifa.address = address;

ifa.mask = mask;

ifa.gateway = gateway;

//NSLog(@"ifaName:%@, address:%@, mask:%@, gateway:%@\n", ifa.name, ifa.address, ifa.mask, ifa.gateway);

ifaList[ifaName] = ifa;

}

temp_addr = temp_addr->ifa_next;

}

}

} @catch (NSException *exception) {

NSLog(@"%@ Exception: %@", @"", exception);

} @finally {

// Free memory

freeifaddrs(interfaces);

}

return ifaList;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容