• 获取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

    使用情况:

  • 相关阅读:
    MFC 时钟 计算器 日期天数计算
    test10
    test9
    iOS 防止按钮快速点击造成多次响应的避免方法
    NSBundle读取图片 plist文件和txt文件
    按指定格式的子字符串,删除和分割字符串
    python批处理入门知识点
    命令行ffmpeg批量旋转视频
    NSData转化成十六进制字符串
    xcode里面使用Memory Leaks和Instruments检测内存泄漏
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3812335.html
Copyright © 2020-2023  润新知