-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanLAN.m
More file actions
201 lines (175 loc) · 6.52 KB
/
ScanLAN.m
File metadata and controls
201 lines (175 loc) · 6.52 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// ScanLAN.m
// LAN Scan
//
// Created by Mongi Zaidi on 24 February 2014.
// Copyright (c) 2014 Smart Touch. All rights reserved.
//
#import "ScanLAN.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
#include <netdb.h>
#import "SimplePingHelper.h"
@interface ScanLAN ()
@property NSString *localAddress;
@property NSString *baseAddress;
@property NSInteger currentHostAddress;
@property NSTimer *timer;
@property NSString *netMask;
@property NSInteger baseAddressEnd;
@property NSInteger timerIterationNumber;
@end
@implementation ScanLAN
- (id)initWithDelegate:(id<ScanLANDelegate>)delegate {
NSLog(@"init scanner");
self = [super init];
if(self)
{
self.delegate = delegate;
}
return self;
}
- (void)startScan {
NSLog(@"start scan");
self.localAddress = [self localIPAddress];
//This is used to test on the simulator
//self.localAddress = @"192.168.1.8";
//self.netMask = @"255.255.255.0";
NSArray *a = [self.localAddress componentsSeparatedByString:@"."];
NSArray *b = [self.netMask componentsSeparatedByString:@"."];
NSLog(@"A %@ %d", self.localAddress,[self isIpAddressValid:self.localAddress]);
if ([self isIpAddressValid:self.localAddress] && (a.count == 4) && (b.count == 4)) {
for (int i = 0; i<4; i++) {
long and = (long)[[a objectAtIndex:i] integerValue] & [[b objectAtIndex:i] integerValue];
if (!self.baseAddress.length) {
self.baseAddress = [NSString stringWithFormat:@"%ld", and];
}
else {
self.baseAddress = [NSString stringWithFormat:@"%@.%ld", self.baseAddress, and];
self.currentHostAddress = and;
self.baseAddressEnd = and;
}
}
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(pingAddress) userInfo:nil repeats:YES];
}
}
- (void)stopScan {
NSLog(@"stop scan");
[self.timer invalidate];
}
- (void)pingAddress{
self.currentHostAddress++;
NSString *address = [NSString stringWithFormat:@"%@%ld", self.baseAddress, (long)self.currentHostAddress];
[SimplePingHelper ping:address target:self sel:@selector(pingResult:)];
if (self.currentHostAddress>=254) {
[self.timer invalidate];
}
}
- (void)pingResult:(NSNumber*)success {
self.timerIterationNumber++;
if (success.boolValue) {
NSString *deviceIPAddress = [[[[NSString stringWithFormat:@"%@%ld", self.baseAddress, self.currentHostAddress] stringByReplacingOccurrencesOfString:@".0" withString:@"."] stringByReplacingOccurrencesOfString:@".00" withString:@"."] stringByReplacingOccurrencesOfString:@".." withString:@".0."];
NSString *deviceName = [self getHostFromIPAddress:[[NSString stringWithFormat:@"%@%ld", self.baseAddress, self.currentHostAddress] cStringUsingEncoding:NSASCIIStringEncoding]];
[self.delegate scanLANDidFindNewAdrress:deviceIPAddress havingHostName:deviceName];
}
else {
}
if (self.timerIterationNumber+self.baseAddressEnd>=254) {
[self.delegate scanLANDidFinishScanning];
}
}
- (NSString *)getHostFromIPAddress:(const char*)ipAddress {
NSString *hostName = nil;
int error;
struct addrinfo *results = NULL;
error = getaddrinfo(ipAddress, NULL, NULL, &results);
if (error != 0)
{
NSLog (@"Could not get any info for the address");
return nil; // or exit(1);
}
for (struct addrinfo *r = results; r; r = r->ai_next)
{
char hostname[NI_MAXHOST] = {0};
error = getnameinfo(r->ai_addr, r->ai_addrlen, hostname, sizeof hostname, NULL, 0 , 0);
if (error != 0)
{
continue; // try next one
}
else
{
hostName = [NSString stringWithFormat:@"%s", hostname];
break;
}
}
return hostName;
}
// Get IP Address
- (NSString *)getIPAddress
{
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
NSString *wifiAddress = nil;
NSString *cellAddress = nil;
// retrieve the current interfaces - returns 0 on success
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
sa_family_t sa_type = temp_addr->ifa_addr->sa_family;
if(sa_type == AF_INET || sa_type == AF_INET6) {
NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];
NSString *addr = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; // pdp_ip0
//NSLog(@"NAME: \"%@\" addr: %@", name, addr); // see for yourself
if([name isEqualToString:@"en0"]) {
// Interface is the wifi connection on the iPhone
wifiAddress = addr;
} else
if([name isEqualToString:@"pdp_ip0"]) {
// Interface is the cell connection on the iPhone
cellAddress = addr;
}
}
temp_addr = temp_addr->ifa_next;
}
// Free memory
freeifaddrs(interfaces);
}
NSString *addr = wifiAddress ? wifiAddress : cellAddress;
return addr ? addr : @"0.0.0.0";
}
- (NSString *) localIPAddress
{
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)
{
temp_addr = interfaces;
while(temp_addr != NULL)
{
// check if interface is en0 which is the wifi connection on the iPhone
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
self.netMask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return address;
}
- (BOOL) isIpAddressValid:(NSString *)ipAddress{
struct in_addr pin;
int success = inet_aton([ipAddress UTF8String],&pin);
if (success == 1) return TRUE;
return FALSE;
}
@end