blob: 16efc6e3261c149e5a0aa427a8cbb32c8f56f5f5 (
plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import "Network.h"
NSString *NetworkAddressesKey = @"addresses";
NSString *NetworkBridgeKey = @"bridge";
NSString *NetworkBroadcastKey = @"broadcast";
NSString *NetworkDhcpKey = @"dhcp";
NSString *NetworkMacKey = @"mac";
NSString *NetworkMtuKey = @"mtu";
NSString *NetworkMulticastKey = @"multicast";
NSString *NetworkNameKey = @"name";
NSString *NetworkNetconfKey = @"netconf";
NSString *NetworkNwidKey = @"nwid";
NSString *NetworkPortNameKey = @"port";
NSString *NetworkPortErrorKey = @"portError";
NSString *NetworkStatusKey = @"status";
NSString *NetworkTypeKey = @"type";
NSString *NetworkAllowManagedKey = @"allowManaged";
NSString *NetworkAllowGlobalKey = @"allowGlobal";
NSString *NetworkAllowDefaultKey = @"allowDefault";
@implementation Network
- (id)initWithJsonData:(NSDictionary*)jsonData
{
self = [super init];
if(self) {
if([jsonData objectForKey:@"assignedAddresses"]) {
_assignedAddresses = (NSArray<NSString*>*)[jsonData objectForKey:@"assignedAddresses"];
}
if([jsonData objectForKey:@"bridge"]) {
_bridge = [(NSNumber*)[jsonData objectForKey:@"bridge"] boolValue];
}
if([jsonData objectForKey:@"broadcastEnabled"]) {
_broadcastEnabled = [(NSNumber*)[jsonData objectForKey:@"broadcastEnabled"] boolValue];
}
if([jsonData objectForKey:@"dhcp"]) {
_dhcp = [(NSNumber*)[jsonData objectForKey:@"dhcp"] boolValue];
}
if([jsonData objectForKey:@"mac"]) {
_mac = (NSString*)[jsonData objectForKey:@"mac"];
}
if([jsonData objectForKey:@"mtu"]) {
_mtu = [(NSNumber*)[jsonData objectForKey:@"mtu"] intValue];
}
if([jsonData objectForKey:@"name"]) {
_name = (NSString*)[jsonData objectForKey:@"name"];
}
if([jsonData objectForKey:@"netconfRevision"]) {
_netconfRevision = [(NSNumber*)[jsonData objectForKey:@"netconfRevision"] intValue];
}
if([jsonData objectForKey:@"nwid"]) {
NSString *networkid = (NSString*)[jsonData objectForKey:@"nwid"];
NSScanner *scanner = [NSScanner scannerWithString:networkid];
[scanner scanHexLongLong:&_nwid];
}
if([jsonData objectForKey:@"portDeviceName"]) {
_portDeviceName = (NSString*)[jsonData objectForKey:@"portDeviceName"];
}
if([jsonData objectForKey:@"portError"]) {
_portError = [(NSNumber*)[jsonData objectForKey:@"portError"] intValue];
}
if([jsonData objectForKey:@"allowManaged"]) {
_allowManaged = [(NSNumber*)[jsonData objectForKey:@"allowManaged"] boolValue];
}
if([jsonData objectForKey:@"allowGlobal"]) {
_allowGlobal = [(NSNumber*)[jsonData objectForKey:@"allowGlobal"] boolValue];
}
if([jsonData objectForKey:@"allowDefault"]) {
_allowDefault = [(NSNumber*)[jsonData objectForKey:@"allowDefault"] boolValue];
}
if([jsonData objectForKey:@"status"]) {
NSString *statusStr = (NSString*)[jsonData objectForKey:@"status"];
if([statusStr isEqualToString:@"REQUESTING_CONFIGURATION"]) {
_status = REQUESTING_CONFIGURATION;
}
else if([statusStr isEqualToString:@"OK"]) {
_status = OK;
}
else if([statusStr isEqualToString:@"ACCESS_DENIED"]) {
_status = ACCESS_DENIED;
}
else if([statusStr isEqualToString:@"NOT_FOUND"]) {
_status = NOT_FOUND;
}
else if([statusStr isEqualToString:@"PORT_ERROR"]) {
_status = PORT_ERROR;
}
else if([statusStr isEqualToString:@"CLIENT_TOO_OLD"]) {
_status = CLIENT_TOO_OLD;
}
}
if([jsonData objectForKey:@"type"]) {
NSString *typeStr = (NSString*)[jsonData objectForKey:@"type"];
if([typeStr isEqualToString:@"PRIVATE"]) {
_type = PRIVATE;
}
else if([typeStr isEqualToString:@"PUBLIC"]) {
_type = PUBLIC;
}
}
_connected = YES;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if(self) {
if([aDecoder containsValueForKey:NetworkAddressesKey]) {
_assignedAddresses = (NSArray<NSString*>*)[aDecoder decodeObjectForKey:NetworkAddressesKey];
}
if([aDecoder containsValueForKey:NetworkBridgeKey]) {
_bridge = [aDecoder decodeBoolForKey:NetworkBridgeKey];
}
if([aDecoder containsValueForKey:NetworkBroadcastKey]) {
_broadcastEnabled = [aDecoder decodeBoolForKey:NetworkBroadcastKey];
}
if([aDecoder containsValueForKey:NetworkDhcpKey]) {
_dhcp = [aDecoder decodeBoolForKey:NetworkDhcpKey];
}
if([aDecoder containsValueForKey:NetworkMacKey]) {
_mac = (NSString*)[aDecoder decodeObjectForKey:NetworkMacKey];
}
if([aDecoder containsValueForKey:NetworkMtuKey]) {
_mtu = (int)[aDecoder decodeIntegerForKey:NetworkMtuKey];
}
if([aDecoder containsValueForKey:NetworkNameKey]) {
_name = (NSString*)[aDecoder decodeObjectForKey:NetworkNameKey];
}
if([aDecoder containsValueForKey:NetworkNetconfKey]) {
_netconfRevision = (int)[aDecoder decodeIntegerForKey:NetworkNetconfKey];
}
if([aDecoder containsValueForKey:NetworkNwidKey]) {
_nwid = [(NSNumber*)[aDecoder decodeObjectForKey:NetworkNwidKey] unsignedLongLongValue];
}
if([aDecoder containsValueForKey:NetworkPortNameKey]) {
_portDeviceName = (NSString*)[aDecoder decodeObjectForKey:NetworkPortNameKey];
}
if([aDecoder containsValueForKey:NetworkPortErrorKey]) {
_portError = (int)[aDecoder decodeIntegerForKey:NetworkPortErrorKey];
}
if([aDecoder containsValueForKey:NetworkStatusKey]) {
_status = (enum NetworkStatus)[aDecoder decodeIntegerForKey:NetworkStatusKey];
}
if([aDecoder containsValueForKey:NetworkTypeKey]) {
_type = (enum NetworkType)[aDecoder decodeIntegerForKey:NetworkTypeKey];
}
if([aDecoder containsValueForKey:NetworkAllowManagedKey]) {
_allowManaged = [aDecoder decodeBoolForKey:NetworkAllowManagedKey];
}
if([aDecoder containsValueForKey:NetworkAllowGlobalKey]) {
_allowGlobal = [aDecoder decodeBoolForKey:NetworkAllowGlobalKey];
}
if([aDecoder containsValueForKey:NetworkAllowDefaultKey]) {
_allowDefault = [aDecoder decodeBoolForKey:NetworkAllowDefaultKey];
}
_connected = NO;
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_assignedAddresses forKey:NetworkAddressesKey];
[aCoder encodeBool:_bridge forKey:NetworkBridgeKey];
[aCoder encodeBool:_broadcastEnabled forKey:NetworkBroadcastKey];
[aCoder encodeBool:_dhcp forKey:NetworkDhcpKey];
[aCoder encodeObject:_mac forKey:NetworkMacKey];
[aCoder encodeInteger:_mtu forKey:NetworkMtuKey];
[aCoder encodeObject:_name forKey:NetworkNameKey];
[aCoder encodeInteger:_netconfRevision forKey:NetworkNetconfKey];
[aCoder encodeObject:[NSNumber numberWithUnsignedLongLong:_nwid]
forKey:NetworkNwidKey];
[aCoder encodeObject:_portDeviceName forKey:NetworkPortNameKey];
[aCoder encodeInteger:_portError forKey:NetworkPortErrorKey];
[aCoder encodeInteger:_status forKey:NetworkStatusKey];
[aCoder encodeInteger:_type forKey:NetworkTypeKey];
[aCoder encodeBool:_allowManaged forKey:NetworkAllowManagedKey];
[aCoder encodeBool:_allowGlobal forKey:NetworkAllowGlobalKey];
[aCoder encodeBool:_allowDefault forKey:NetworkAllowDefaultKey];
}
+ (BOOL)defaultRouteExists:(NSArray<Network *>*)netList
{
for(Network *net in netList) {
if (net.allowDefault && net.connected) {
return YES;
}
}
return NO;
}
- (NSString*)statusString {
switch(_status) {
case REQUESTING_CONFIGURATION:
return @"REQUESTING_CONFIGURATION";
case OK:
return @"OK";
case ACCESS_DENIED:
return @"ACCESS_DENIED";
case NOT_FOUND:
return @"NOT_FOUND";
case PORT_ERROR:
return @"PORT_ERROR";
case CLIENT_TOO_OLD:
return @"CLIENT_TOO_OLD";
default:
return @"";
}
}
- (NSString*)typeString {
switch(_type) {
case PUBLIC:
return @"PUBLIC";
case PRIVATE:
return @"PRIVATE";
default:
return @"";
}
}
@end
|