-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMyIP.m
51 lines (40 loc) · 1.04 KB
/
MyIP.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// MyIP.m
// ip
//
// Created by Nicolas Seriot on 12.11.08.
// Copyright 2008 Sen:te. All rights reserved.
//
#import "MyIP.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
static MyIP *sharedInstance = nil;
@implementation MyIP
+ (MyIP *)sharedInstance {
if (sharedInstance == nil) {
sharedInstance = [[MyIP alloc] init];
}
return sharedInstance;
}
- (NSDictionary *)ipsForInterfaces {
struct ifaddrs *list;
if(getifaddrs(&list) < 0) {
perror("getifaddrs");
return nil;
}
NSMutableDictionary *d = [NSMutableDictionary dictionary];
struct ifaddrs *cur;
for(cur = list; cur != NULL; cur = cur->ifa_next) {
if(cur->ifa_addr->sa_family != AF_INET)
continue;
struct sockaddr_in *addrStruct = (struct sockaddr_in *)cur->ifa_addr;
NSString *name = [NSString stringWithUTF8String:cur->ifa_name];
NSString *addr = [NSString stringWithUTF8String:inet_ntoa(addrStruct->sin_addr)];
[d setValue:addr forKey:name];
}
freeifaddrs(list);
return d;
}
@end