]> git.saurik.com Git - apple/configd.git/blame - Plugins/IPMonitor/configAgent.m
configd-1109.101.1.tar.gz
[apple/configd.git] / Plugins / IPMonitor / configAgent.m
CommitLineData
942cecd7 1/*
afb19109 2 * Copyright (c) 2015-2018 Apple Inc. All rights reserved.
942cecd7
A
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
1ef45fa4 57+ (instancetype)agentFromData:(NSData *)data
942cecd7 58{
1ef45fa4 59#pragma unused(data)
942cecd7
A
60 return nil;
61}
62
63- (instancetype)initWithParameters:(NSDictionary *)parameters
64{
65 self = [super init];
66 if (self) {
67 NSString *intf = [parameters valueForKey:@kEntityName];
68
69 _internalRegistrationObject = nil;
70 _internalAssociatedEntity = [intf copy];
71 _internalAgentData = nil;
72 _internalShouldUpdateAgent = YES;
73 _internalAgentMapping = nil;
74
75 active = YES;
76 kernelActivated = NO;
77 userActivated = YES;
78 voluntary = NO;
79 }
80
81 return self;
82}
83
84- (void)addAgentRegistrationObject:(NWNetworkAgentRegistration *)regObject
85{
86 _internalRegistrationObject = regObject;
87}
88
89- (AgentType)getAgentType
90{
91 return kAgentTypeUnknown;
92}
93
94- (NSUUID *)getAgentUUID
95{
96 return nil;
97}
98
99- (NSString *)getAgentName
100{
101 return @kConfigAgentTypeGeneric;
102}
103
104- (AgentSubType)getAgentSubType
105{
106 return kAgentSubTypeUnknown;
107}
108
109- (NWNetworkAgentRegistration *)getRegistrationObject
110{
111 return _internalRegistrationObject;
112}
113
114- (NSString *)getAssociatedEntity
115{
116 return _internalAssociatedEntity;
117}
118
119- (NSData *)getAgentData
120{
121 return _internalAgentData;
122}
123
124- (NSData *)copyAgentData
125{
126 return _internalAgentData;
127}
128
129- (void)setAgentMapping:(id)agent
130{
131 _internalAgentMapping = agent;
132}
133
134- (id)getAgentMapping
135{
136 return _internalAgentMapping;
137}
138
139- (void)setStartHandler:(void (^)(void))startHandler
140{
141 if (startHandler != nil) {
142 self.internalStartHandler = startHandler;
143 }
144}
145
146- (BOOL)startAgentWithOptions:(NSDictionary *)options
147{
1ef45fa4 148#pragma unused(options)
942cecd7
A
149 BOOL ok = NO;
150 if (!self.active) {
151 self.active = YES;
152 ok = [self.internalRegistrationObject updateNetworkAgent:self];
153 }
154
155 return ok;
156}
157
158- (void)updateAgentData:(NSData *)data
159{
160 if ([data isEqual:_internalAgentData]) {
161 _internalShouldUpdateAgent = NO;
162 return;
163 }
164
165 _internalAgentData = [data copy];
166 _internalShouldUpdateAgent = YES;
167}
168
169- (BOOL)shouldUpdateAgent
170{
171 return _internalShouldUpdateAgent;
172}
173
174- (NSUUID *)createUUIDForName:(NSString *)agentName
175{
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.
178
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.
182
183 - So for a name, we would always have the same UUID.
184
185 */
afb19109 186 unsigned char hashValue[CC_SHA256_DIGEST_LENGTH];
942cecd7 187 const char *strForHash = [agentName UTF8String];
afb19109
A
188 CC_SHA256(strForHash, (CC_LONG)strlen(strForHash), hashValue);
189
942cecd7
A
190 return [[NSUUID alloc] initWithUUIDBytes:hashValue];
191}
192
1ef45fa4 193@end