]> git.saurik.com Git - apple/configd.git/blob - Plugins/IPMonitor/configAgent.m
configd-888.30.2.tar.gz
[apple/configd.git] / Plugins / IPMonitor / configAgent.m
1 /*
2 * Copyright (c) 2015, 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import "configAgent.h"
25
26 @interface ConfigAgent()
27
28 @property (nonatomic) NWNetworkAgentRegistration * internalRegistrationObject;
29 @property (nonatomic) NSString *internalAssociatedEntity;
30 @property (nonatomic, copy) NSData *internalAgentData;
31 @property (nonatomic) BOOL internalShouldUpdateAgent;
32 @property (strong) void (^internalStartHandler)(void);
33 @property (nonatomic) id internalAgentMapping;
34
35 @end
36
37 @implementation ConfigAgent
38
39 @synthesize agentUUID;
40 @synthesize agentDescription;
41 @synthesize active;
42 @synthesize kernelActivated;
43 @synthesize userActivated;
44 @synthesize voluntary;
45 @synthesize specificUseOnly;
46
47 + (NSString *)agentDomain
48 {
49 return @kConfigAgentDomain;
50 }
51
52 + (NSString *)agentType
53 {
54 return @kConfigAgentTypeGeneric;
55 }
56
57 + (instancetype)agentFromData:(__unused NSData *)data
58 {
59 return nil;
60 }
61
62 - (instancetype)initWithParameters:(NSDictionary *)parameters
63 {
64 self = [super init];
65 if (self) {
66 NSString *intf = [parameters valueForKey:@kEntityName];
67
68 _internalRegistrationObject = nil;
69 _internalAssociatedEntity = [intf copy];
70 _internalAgentData = nil;
71 _internalShouldUpdateAgent = YES;
72 _internalAgentMapping = nil;
73
74 active = YES;
75 kernelActivated = NO;
76 userActivated = YES;
77 voluntary = NO;
78 }
79
80 return self;
81 }
82
83 - (void)addAgentRegistrationObject:(NWNetworkAgentRegistration *)regObject
84 {
85 _internalRegistrationObject = regObject;
86 }
87
88 - (AgentType)getAgentType
89 {
90 return kAgentTypeUnknown;
91 }
92
93 - (NSUUID *)getAgentUUID
94 {
95 return nil;
96 }
97
98 - (NSString *)getAgentName
99 {
100 return @kConfigAgentTypeGeneric;
101 }
102
103 - (AgentSubType)getAgentSubType
104 {
105 return kAgentSubTypeUnknown;
106 }
107
108 - (NWNetworkAgentRegistration *)getRegistrationObject
109 {
110 return _internalRegistrationObject;
111 }
112
113 - (NSString *)getAssociatedEntity
114 {
115 return _internalAssociatedEntity;
116 }
117
118 - (NSData *)getAgentData
119 {
120 return _internalAgentData;
121 }
122
123 - (NSData *)copyAgentData
124 {
125 return _internalAgentData;
126 }
127
128 - (void)setAgentMapping:(id)agent
129 {
130 _internalAgentMapping = agent;
131 }
132
133 - (id)getAgentMapping
134 {
135 return _internalAgentMapping;
136 }
137
138 - (void)setStartHandler:(void (^)(void))startHandler
139 {
140 if (startHandler != nil) {
141 self.internalStartHandler = startHandler;
142 }
143 }
144
145 - (BOOL)startAgentWithOptions:(NSDictionary *)options
146 {
147 BOOL ok = NO;
148 if (!self.active) {
149 self.active = YES;
150 ok = [self.internalRegistrationObject updateNetworkAgent:self];
151 }
152
153 return ok;
154 }
155
156 - (void)updateAgentData:(NSData *)data
157 {
158 if ([data isEqual:_internalAgentData]) {
159 _internalShouldUpdateAgent = NO;
160 return;
161 }
162
163 _internalAgentData = [data copy];
164 _internalShouldUpdateAgent = YES;
165 }
166
167 - (BOOL)shouldUpdateAgent
168 {
169 return _internalShouldUpdateAgent;
170 }
171
172 - (NSUUID *)createUUIDForName:(NSString *)agentName
173 {
174 /* We would like to have same UUIDs for an interface/domain. So here is a way to fix this,
175 without maintaining any state in configd.
176
177 - We know that every agent has a unique name.
178 - Use that name to calculate an MD5 hash. MD5 because the digest size is 16 bytes, and so it uuid_t.
179 - create a NSUUID from these raw bytes.
180
181 - So for a name, we would always have the same UUID.
182
183 */
184 unsigned char hashValue[CC_MD5_DIGEST_LENGTH];
185 const char *strForHash = [agentName UTF8String];
186 CC_MD5(strForHash, (CC_LONG)strlen(strForHash), hashValue);
187
188 return [[NSUUID alloc] initWithUUIDBytes:hashValue];
189 }
190
191 @end