Since you know after June 1, app in iOS should support only-IPV6. I find a way that can change an ipv4 ip address to an ipv6 ip address. But, I cann't judge a network environment is only-IPV6. I already open a NAT64 WIFI by my MACBOOK, also use flight mode to make sure it's the only-IPV6. I just find there is an ipv6 ip address at first. Then work the code again, i will get both an ipv4 and an ipv6 address. I have no idea about it. Anyone have any idea?
There are my codes for fetch ip address:
- (NSMutableDictionary *)localIPAddressFetcher {
NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
struct ifaddrs *myaddrs, *temp_addr;
struct sockaddr_in *s4;
struct sockaddr_in6 *s6;
int status = 0;
char buf[64];
status = getifaddrs(&myaddrs);
if (status == 0) {
for (temp_addr = myaddrs; temp_addr != NULL; temp_addr = temp_addr->ifa_next) {
if (temp_addr->ifa_addr == NULL) continue;
if ((temp_addr->ifa_flags & IFF_UP) == 0) continue;
if (temp_addr->ifa_flags & IFF_LOOPBACK) continue;
if (temp_addr->ifa_addr->sa_family == AF_INET) {
s4 = (struct sockaddr_in *)(temp_addr->ifa_addr);
if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) != NULL) {
if (![[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"lo0"]) {
NSString *address = [NSString stringWithUTF8String:buf];
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"en0"]) {
[addressDic setObject:address forKey:#"ipv4_wifi"];
} else {
[addressDic setObject:address forKey:[NSString stringWithUTF8String:temp_addr->ifa_name]];
}
}
}
} else if (temp_addr->ifa_addr->sa_family == AF_INET6) {
s6 = (struct sockaddr_in6 *)(temp_addr->ifa_addr);
if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) != NULL) {
if (![[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"lo0"]) {
NSString *address = [NSString stringWithUTF8String:buf];
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"en0"]) {
[addressDic setObject:address forKey:#"ipv6_wifi"];
} else {
[addressDic setObject:address forKey:[NSString stringWithUTF8String:temp_addr->ifa_name]];
}
}
}
}
}
}
freeifaddrs(myaddrs);
return addressDic;}
Related
I want to get the mac Address,IP Address,Host Name, device Name And Brand Name of the devices which are connected to my wifi router.I will Get all these except the host name.
I have tried MMLAnscan,AFNetworking,LANScanMaster,etc it will provide me all the details except host name
+(NSString *)getHostFromIPAddress:(NSString*)ipAddress {
struct addrinfo *result = NULL;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
//"www.kame.net", "http"
int errorStatus = getaddrinfo([ipAddress cStringUsingEncoding:NSUTF8StringEncoding], NULL, &hints, &result);
//NSUTF8StringEncoding instead of NSASCIIStringEncoding
if (errorStatus != 0) {
return nil;
}
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) {
return nil;
}
freeaddrinfo(result);
CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
if (hostRef == nil) {
return nil;
}
CFRelease(addressRef);
BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
if (!succeeded) {
return nil;
}
NSMutableArray *hostnames = [NSMutableArray array];
CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; currentIndex++) {
[hostnames addObject:[(__bridge NSArray *)hostnamesRef objectAtIndex:currentIndex]];
NSLog(#"hostt:%#",hostnames);
}
NSLog(#"hostname:%#",hostnames[0]);
return hostnames[0];
}
I want hostnames of the devices which are connected to my wifi.
I use a third-party app to connect a VPN, and we can get the detail information in Settings->VPN->information. How can I get the Assigned IP programmatically in our app by Objective-C?
NSString *address = #"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
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) {
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"utun1"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
NSLog(#"ifaName: %#, Address: %#",[NSString stringWithUTF8String:temp_addr->ifa_name],address);
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
address will have the assigned IP Address
This is working on iOS 14:
- (NSString *)getVPNIPAddress
{
NSString *address = #"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
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)
{
NSLog(#"%#",[NSString stringWithUTF8String:temp_addr->ifa_name]);
// Check if interface is en0 which is the wifi connection on the iPhone
if(
[[NSString stringWithUTF8String:temp_addr->ifa_name] containsString:#"tun"] ||
[[NSString stringWithUTF8String:temp_addr->ifa_name] containsString:#"tap"] ||
[[NSString stringWithUTF8String:temp_addr->ifa_name] containsString:#"ipsec"] ||
[[NSString stringWithUTF8String:temp_addr->ifa_name] containsString:#"ppp"]){
// Get NSString from C String
struct sockaddr_in *in = (struct sockaddr_in*) temp_addr->ifa_addr;
address = [NSString stringWithUTF8String:inet_ntoa((in)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
I use the code below to get IP address of iPhone
#define IOS_CELLULAR #"pdp_ip0"
#define IOS_WIFI #"en0"
#define IOS_VPN #"utun0"
#define IP_ADDR_IPv4 #"ipv4"
#define IP_ADDR_IPv6 #"ipv6"
- (NSString *) getIPAddress1;
{
NSString *address = #"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
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)
{
// Check if interface is en0 which is the wifi connection on the iPhone
NSString *s= [NSString stringWithUTF8String:temp_addr->ifa_name];
NSLog(#"%# ---",s);
if([s isEqualToString:IOS_WIFI] || [s isEqualToString:IOS_CELLULAR] || [s isEqualToString:IOS_VPN])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
It worked before, but now it always returns #"error".
I even tried all method mention here
iPhone/iPad/OSX: How to get my IP address programmatically?
but none worked.
Your comments are welcome.
Try this
#import <ifaddrs.h>
#import <arpa/inet.h>
- (NSString *)getIPAddress {
NSString *address = #"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
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) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
I want to know is it possible in iOS to get the data usage by specific WIFI even when app is not running. Is there any API which can be used for this to get result?
To know the total WIFI I used following code:
- (NSArray *)getDataCounters
{
BOOL success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct if_data *networkStatisc;
int WiFiSent = 0;
int WiFiReceived = 0;
int WWANSent = 0;
int WWANReceived = 0;
NSString *name=[[NSString alloc]init];
success = getifaddrs(&addrs) == 0;
if (success)
{
cursor = addrs;
while (cursor != NULL)
{
name=[NSString stringWithFormat:#"%s",cursor->ifa_name];
//NSLog(#"ifa_name %s == %#\n", cursor->ifa_name,name);
// names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN
if (cursor->ifa_addr->sa_family == AF_LINK)
{
if ([name hasPrefix:#"en"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WiFiSent+=networkStatisc->ifi_obytes;
WiFiReceived+=networkStatisc->ifi_ibytes;
// NSLog(#"WiFiSent %d ==%d",WiFiSent,networkStatisc->ifi_obytes);
// NSLog(#"WiFiReceived %d ==%d",WiFiReceived,networkStatisc->ifi_ibytes);
}
if ([name hasPrefix:#"pdp_ip"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WWANSent+=networkStatisc->ifi_obytes;
WWANReceived+=networkStatisc->ifi_ibytes;
// NSLog(#"WWANSent %d ==%d",WWANSent,networkStatisc->ifi_obytes);
// NSLog(#"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes);
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
NSLog(#"%#",[NSArray arrayWithObjects:[NSNumber numberWithInt:(WiFiSent)/1000000], [NSNumber numberWithInt:(WiFiReceived)/1000000],[NSNumber numberWithInt:(WWANSent)/1000000],[NSNumber numberWithInt:(WWANReceived)/1000000], nil]);
return [NSArray arrayWithObjects:[NSNumber numberWithInt:(WiFiSent)/1000000], [NSNumber numberWithInt:(WiFiReceived)/1000000],[NSNumber numberWithInt:(WWANSent)/1000000],[NSNumber numberWithInt:(WWANReceived)/1000000], nil];
}
Please help me with this.
One task of my program is to scan local wi-fi network for any devices/computers in same network. I found solution to get all working devices IPs, but did not managed to get names of them. I did not find any clue to solve this problem.
Any suggestions?
In order to perform a reverse DNS lookup, you need to call the CFHostGetNames function, like this:
+ (NSArray *)hostnamesForIPv4Address:(NSString *)address
{
struct addrinfo *result = NULL;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
if (errorStatus != 0) {
return nil;
}
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) {
return nil;
}
freeaddrinfo(result);
CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
if (hostRef == nil) {
return nil;
}
CFRelease(addressRef);
BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
if (!succeeded) {
return nil;
}
NSMutableArray *hostnames = [NSMutableArray array];
CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; currentIndex++) {
[hostnames addObject:[(__bridge NSArray *)hostnamesRef objectAtIndex:currentIndex]];
}
return hostnames;
}
BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL); Now I encounter that always failed at this line, and I tried to use getnameinfo function, it is still can't get the hostname