2 * Copyright (c) 2015, 2016 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:(__unused NSData *)data
62 - (instancetype)initWithParameters:(NSDictionary *)parameters
66 NSString *intf = [parameters valueForKey:@kEntityName];
68 _internalRegistrationObject = nil;
69 _internalAssociatedEntity = [intf copy];
70 _internalAgentData = nil;
71 _internalShouldUpdateAgent = YES;
72 _internalAgentMapping = nil;
83 - (void)addAgentRegistrationObject:(NWNetworkAgentRegistration *)regObject
85 _internalRegistrationObject = regObject;
88 - (AgentType)getAgentType
90 return kAgentTypeUnknown;
93 - (NSUUID *)getAgentUUID
98 - (NSString *)getAgentName
100 return @kConfigAgentTypeGeneric;
103 - (AgentSubType)getAgentSubType
105 return kAgentSubTypeUnknown;
108 - (NWNetworkAgentRegistration *)getRegistrationObject
110 return _internalRegistrationObject;
113 - (NSString *)getAssociatedEntity
115 return _internalAssociatedEntity;
118 - (NSData *)getAgentData
120 return _internalAgentData;
123 - (NSData *)copyAgentData
125 return _internalAgentData;
128 - (void)setAgentMapping:(id)agent
130 _internalAgentMapping = agent;
133 - (id)getAgentMapping
135 return _internalAgentMapping;
138 - (void)setStartHandler:(void (^)(void))startHandler
140 if (startHandler != nil) {
141 self.internalStartHandler = startHandler;
145 - (BOOL)startAgentWithOptions:(NSDictionary *)options
150 ok = [self.internalRegistrationObject updateNetworkAgent:self];
156 - (void)updateAgentData:(NSData *)data
158 if ([data isEqual:_internalAgentData]) {
159 _internalShouldUpdateAgent = NO;
163 _internalAgentData = [data copy];
164 _internalShouldUpdateAgent = YES;
167 - (BOOL)shouldUpdateAgent
169 return _internalShouldUpdateAgent;
172 - (NSUUID *)createUUIDForName:(NSString *)agentName
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.
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.
181 - So for a name, we would always have the same UUID.
184 unsigned char hashValue[CC_MD5_DIGEST_LENGTH];
185 const char *strForHash = [agentName UTF8String];
186 CC_MD5(strForHash, (CC_LONG)strlen(strForHash), hashValue);
188 return [[NSUUID alloc] initWithUUIDBytes:hashValue];