首页 > 代码库 > 获取INET4与INET6的信息
获取INET4与INET6的信息
获取INET4与INET6的信息
参考书籍:
本人封装的源码:
//// IPAddressInfo.h// YXNETWORK//// http://www.cnblogs.com/YouXianMing///// Copyright (c) 2014年 Y.X. All rights reserved.//#import <Foundation/Foundation.h>@interface IPAddressInfo : NSObject+ (NSDictionary *)net4Info;@end
//// IPAddressInfo.m// YXNETWORK//// http://www.cnblogs.com/YouXianMing///// Copyright (c) 2014年 Y.X. All rights reserved.//#import "IPAddressInfo.h"#import <sys/types.h>#import <sys/socket.h>#import <netdb.h>#import <arpa/inet.h>#include <ifaddrs.h>#include <arpa/inet.h>static NSMutableDictionary *net4 = nil;static NSMutableDictionary *net6 = nil;@implementation IPAddressInfo+ (void)initialize{ if (self == [IPAddressInfo class]) { net4 = [NSMutableDictionary new]; net6 = [NSMutableDictionary new]; }}+ (NSDictionary *)net4Info{ [self updataInfo]; return net4;}+ (void)updataInfo{ struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // 从当前接口中获取信息 success = getifaddrs(&interfaces); if (success == 0) { // 遍历接口 temp_addr = interfaces; for (temp_addr = interfaces; temp_addr != NULL; temp_addr = temp_addr->ifa_next) { if(temp_addr->ifa_addr->sa_family == AF_INET) { NSMutableDictionary *tmp = [NSMutableDictionary new]; // INET4 char naddr[INET_ADDRSTRLEN]; char nmask[INET_ADDRSTRLEN]; char ngate[INET_ADDRSTRLEN]; inet_ntop(AF_INET,&((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr,naddr,INET_ADDRSTRLEN); inet_ntop(AF_INET,&((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr,nmask,INET_ADDRSTRLEN); inet_ntop(AF_INET,&((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr,ngate,INET_ADDRSTRLEN); NSString *interfaceName = [NSString stringWithUTF8String:temp_addr->ifa_name]; NSString *address = [NSString stringWithUTF8String:naddr]; NSString *netmask = [NSString stringWithUTF8String:nmask]; NSString *gateway = [NSString stringWithUTF8String:ngate]; [tmp setObject:address forKey:@"address"]; [tmp setObject:netmask forKey:@"netmask"]; [tmp setObject:gateway forKey:@"gateway"]; [net4 setObject:tmp forKey:interfaceName]; } else if(temp_addr->ifa_addr->sa_family == AF_INET6) { // INET6 char naddr[INET6_ADDRSTRLEN]; char ngate[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6,&((struct sockaddr_in6 *)temp_addr->ifa_addr)->sin6_addr,naddr,INET6_ADDRSTRLEN); NSString *tmpGateway = nil; if ((struct sockaddr_in6 *)temp_addr->ifa_dstaddr != NULL) { inet_ntop(AF_INET6,&((struct sockaddr_in6 *)temp_addr->ifa_dstaddr)->sin6_addr,ngate,INET6_ADDRSTRLEN); tmpGateway = [NSString stringWithUTF8String:ngate]; } else { tmpGateway = @" "; } // NSString *interfaceName = [NSString stringWithUTF8String:temp_addr->ifa_name];// NSString *address = [NSString stringWithUTF8String:naddr];// NSString *netmask = @" ";// NSString *gateway = tmpGateway;// // NSLog(@"%@", interfaceName);// NSLog(@"%@", address);// NSLog(@"%@", netmask);// NSLog(@"%@", gateway); } else { continue; } } } // 释放资源 freeifaddrs(interfaces);}@end
使用情况:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。