2 * Copyright (c) 2015-2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #import "configAgent.h"
26 @interface ConfigAgent()
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;
37 @implementation ConfigAgent
39 @synthesize agentUUID;
40 @synthesize agentDescription;
42 @synthesize kernelActivated;
43 @synthesize userActivated;
44 @synthesize voluntary;
45 @synthesize specificUseOnly;
47 + (NSString *)agentDomain
49 return @kConfigAgentDomain;
52 + (NSString *)agentType
54 return @kConfigAgentTypeGeneric;
57 + (instancetype)agentFromData:(NSData *)data
63 - (instancetype)initWithParameters:(NSDictionary *)parameters
67 NSString *intf = [parameters valueForKey:@kEntityName];
69 _internalRegistrationObject = nil;
70 _internalAssociatedEntity = [intf copy];
71 _internalAgentData = nil;
72 _internalShouldUpdateAgent = YES;
73 _internalAgentMapping = nil;
84 - (void)addAgentRegistrationObject:(NWNetworkAgentRegistration *)regObject
86 _internalRegistrationObject = regObject;
89 - (AgentType)getAgentType
91 return kAgentTypeUnknown;
94 - (NSUUID *)getAgentUUID
99 - (NSString *)getAgentName
101 return @kConfigAgentTypeGeneric;
104 - (AgentSubType)getAgentSubType
106 return kAgentSubTypeUnknown;
109 - (NWNetworkAgentRegistration *)getRegistrationObject
111 return _internalRegistrationObject;
114 - (NSString *)getAssociatedEntity
116 return _internalAssociatedEntity;
119 - (NSData *)getAgentData
121 return _internalAgentData;
124 - (NSData *)copyAgentData
126 return _internalAgentData;
129 - (void)setAgentMapping:(id)agent
131 _internalAgentMapping = agent;
134 - (id)getAgentMapping
136 return _internalAgentMapping;
139 - (void)setStartHandler:(void (^)(void))startHandler
141 if (startHandler != nil) {
142 self.internalStartHandler = startHandler;
146 - (BOOL)startAgentWithOptions:(NSDictionary *)options
148 #pragma unused(options)
152 ok = [self.internalRegistrationObject updateNetworkAgent:self];
158 - (void)updateAgentData:(NSData *)data
160 if ([data isEqual:_internalAgentData]) {
161 _internalShouldUpdateAgent = NO;
165 _internalAgentData = [data copy];
166 _internalShouldUpdateAgent = YES;
169 - (BOOL)shouldUpdateAgent
171 return _internalShouldUpdateAgent;
174 - (NSUUID *)createUUIDForName:(NSString *)agentName
176 /* We would like to have same UUIDs for an interface/domain. So here is a way to fix this,
177 without maintaining any state in configd.
179 - We know that every agent has a unique name.
180 - Use that name to calculate an MD5 hash. MD5 because the digest size is 16 bytes, and so it uuid_t.
181 - create a NSUUID from these raw bytes.
183 - So for a name, we would always have the same UUID.
186 unsigned char hashValue[CC_MD5_DIGEST_LENGTH];
187 const char *strForHash = [agentName UTF8String];
188 CC_MD5(strForHash, (CC_LONG)strlen(strForHash), hashValue);
190 return [[NSUUID alloc] initWithUUIDBytes:hashValue];