]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/DNSServiceReg.m
mDNSResponder-161.1.tar.gz
[apple/mdnsresponder.git] / Clients / DNSServiceReg.m
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16
17 Change History (most recent first):
18
19 $Log: DNSServiceReg.m,v $
20 Revision 1.16 2006/08/14 23:23:55 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
22
23 Revision 1.15 2004/06/05 02:01:08 cheshire
24 Move DNSServiceRegistration from mDNSMacOSX directory to Clients directory
25
26 Revision 1.14 2004/03/04 19:20:23 cheshire
27 Remove invalid UTF-8 character
28
29 Revision 1.13 2003/08/12 19:55:07 cheshire
30 Update to APSL 2.0
31
32 */
33
34 #include "dns_sd.h"
35
36 @interface RegistrationController : NSObject
37 {
38 IBOutlet NSTableColumn *typeColumn;
39 IBOutlet NSTableColumn *nameColumn;
40 IBOutlet NSTableColumn *portColumn;
41 IBOutlet NSTableColumn *domainColumn;
42 IBOutlet NSTableColumn *textColumn;
43
44 IBOutlet NSTableView *serviceDisplayTable;
45
46 IBOutlet NSTextField *serviceTypeField;
47 IBOutlet NSTextField *serviceNameField;
48 IBOutlet NSTextField *servicePortField;
49 IBOutlet NSTextField *serviceDomainField;
50 IBOutlet NSTextField *serviceTextField;
51
52 NSMutableArray *srvtypeKeys;
53 NSMutableArray *srvnameKeys;
54 NSMutableArray *srvportKeys;
55 NSMutableArray *srvdomainKeys;
56 NSMutableArray *srvtextKeys;
57
58 NSMutableDictionary *registeredDict;
59 }
60
61 - (IBAction)registerService:(id)sender;
62 - (IBAction)unregisterService:(id)sender;
63
64 - (IBAction)addNewService:(id)sender;
65 - (IBAction)removeSelected:(id)sender;
66
67 @end
68
69 void reg_reply
70 (
71 DNSServiceRef sdRef,
72 DNSServiceFlags flags,
73 DNSServiceErrorType errorCode,
74 const char *name,
75 const char *regtype,
76 const char *domain,
77 void *context
78 )
79 {
80 // registration reply
81 printf("Got a reply from the server with error %d\n", errorCode);
82 return;
83 }
84
85 static void myCFSocketCallBack(CFSocketRef cfs, CFSocketCallBackType CallBackType, CFDataRef address, const void *data, void *context)
86 {
87 DNSServiceProcessResult((DNSServiceRef)context);
88 }
89
90 static void addDNSServiceRefToRunLoop(DNSServiceRef ref)
91 {
92 int s = DNSServiceRefSockFD(ref);
93 CFSocketContext myCFSocketContext = { 0, ref, NULL, NULL, NULL };
94 CFSocketRef c = CFSocketCreateWithNative(kCFAllocatorDefault, s, kCFSocketReadCallBack, myCFSocketCallBack, &myCFSocketContext);
95 CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, c, 0);
96 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
97 CFRelease(rls);
98 }
99
100
101 @implementation RegistrationController
102
103 - (void)registerDefaults
104 {
105 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
106
107 NSArray *typeArray = [NSArray arrayWithObjects:@"_ftp._tcp.", @"_ssh._tcp.", @"_tftp._tcp.", @"_http._tcp.", @"_printer._tcp.", @"_afpovertcp._tcp.", nil];
108 NSArray *nameArray = [NSArray arrayWithObjects:@"My ftp Server", @"My Computer", @"Testing Boot Image", @"A Web Server", @"Steve's Printer", @"Company AppleShare Server", nil];
109 NSArray *portArray = [NSArray arrayWithObjects:@"21", @"22", @"69", @"80", @"515", @"548", nil];
110 NSArray *domainArray = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", nil];
111 NSArray *textArray = [NSArray arrayWithObjects:@"", @"", @"image=mybootimage", @"path=/index.html", @"rn=lpt1", @"Vol=Public", nil];
112
113 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
114 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
115 [regDict setObject:portArray forKey:@"SrvPortKeys"];
116 [regDict setObject:domainArray forKey:@"SrvDomainKeys"];
117 [regDict setObject:textArray forKey:@"SrvTextKeys"];
118
119 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
120 }
121
122 - (id)init
123 {
124 srvtypeKeys = [[NSMutableArray array] retain]; //Define arrays for Type, Domain, and Name
125 srvnameKeys = [[NSMutableArray array] retain];
126 srvportKeys = [[NSMutableArray array] retain];
127 srvdomainKeys = [[NSMutableArray array] retain];
128 srvtextKeys = [[NSMutableArray array] retain];
129
130 registeredDict = [[NSMutableDictionary alloc] init];
131
132 [self registerDefaults];
133 return [super init];
134 }
135
136 - (void)awakeFromNib //BrowserController startup procedure
137 {
138 [srvtypeKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"]];
139 [srvnameKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"]];
140 [srvportKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvPortKeys"]];
141 [srvdomainKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvDomainKeys"]];
142 [srvtextKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTextKeys"]];
143
144 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
145
146 }
147
148
149
150 - (IBAction)registerService:(id)sender
151 {
152 int selectedRow = [serviceDisplayTable selectedRow];
153 CFMachPortContext context;
154 DNSServiceRef dns_client;
155
156 if (selectedRow < 0) {
157 return;
158 }
159
160 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
161 if ([registeredDict objectForKey:key]) { printf("Already registered\n"); return; }
162
163 context.version = 1;
164 context.info = 0;
165 context.retain = NULL;
166 context.release = NULL;
167 context.copyDescription = NULL;
168 unsigned char txtbuffer[300];
169 strncpy(txtbuffer+1, [[srvtextKeys objectAtIndex:selectedRow] UTF8String], sizeof(txtbuffer)-1);
170 txtbuffer[0] = strlen(txtbuffer+1);
171
172 DNSServiceErrorType err = DNSServiceRegister
173 (
174 &dns_client, 0, 0,
175 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
176 [key UTF8String],
177 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
178 NULL, htons([[srvportKeys objectAtIndex:selectedRow] intValue]),
179 txtbuffer[0]+1, txtbuffer,
180 reg_reply,
181 nil
182 );
183 if (err)
184 printf("DNSServiceRegister failed %d\n", err);
185 else
186 {
187 addDNSServiceRefToRunLoop(dns_client);
188 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:key];
189 }
190 }
191
192 - (IBAction)unregisterService:(id)sender
193 {
194 int selectedRow = [serviceDisplayTable selectedRow];
195 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
196
197 NSNumber *refPtr = [registeredDict objectForKey:key];
198 DNSServiceRef ref = (DNSServiceRef)[refPtr unsignedIntValue];
199
200 if (ref) {
201 DNSServiceRefDeallocate(ref);
202 [registeredDict removeObjectForKey:key];
203 }
204 }
205
206 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
207 {
208 if (row<0) return;
209 }
210
211 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
212 {
213 return [srvtypeKeys count];
214 }
215
216 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
217 {
218 if (theColumn == typeColumn) {
219 return [srvtypeKeys objectAtIndex:rowIndex];
220 }
221 if (theColumn == nameColumn) {
222 return [srvnameKeys objectAtIndex:rowIndex];
223 }
224 if (theColumn == portColumn) {
225 return [srvportKeys objectAtIndex:rowIndex];
226 }
227 if (theColumn == domainColumn) {
228 return [srvdomainKeys objectAtIndex:rowIndex];
229 }
230 if (theColumn == textColumn) {
231 return [srvtextKeys objectAtIndex:rowIndex];
232 }
233
234 return(0);
235 } //End of mandatory TableView methods
236
237 - (IBAction)removeSelected:(id)sender
238 {
239 // remove the selected row and force a refresh
240
241 int selectedRow = [serviceDisplayTable selectedRow];
242
243 if (selectedRow) {
244
245 [srvtypeKeys removeObjectAtIndex:selectedRow];
246 [srvnameKeys removeObjectAtIndex:selectedRow];
247 [srvportKeys removeObjectAtIndex:selectedRow];
248 [srvdomainKeys removeObjectAtIndex:selectedRow];
249 [srvtextKeys removeObjectAtIndex:selectedRow];
250
251 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
252 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
253 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
254 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
255 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
256
257 [serviceDisplayTable reloadData];
258 }
259 }
260
261 - (IBAction)addNewService:(id)sender
262 {
263 // add new entries from the edit fields to the arrays for the defaults
264
265 if ([[serviceTypeField stringValue] length] && [[serviceNameField stringValue] length] && [[serviceDomainField stringValue] length]&& [[servicePortField stringValue] length]) {
266 [srvtypeKeys addObject:[serviceTypeField stringValue]];
267 [srvnameKeys addObject:[serviceNameField stringValue]];
268 [srvportKeys addObject:[servicePortField stringValue]];
269 [srvdomainKeys addObject:[serviceDomainField stringValue]];
270 [srvtextKeys addObject:[serviceTextField stringValue]];
271
272 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
273 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
274 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
275 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
276 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
277
278 [serviceDisplayTable reloadData];
279 } else {
280 NSBeep();
281 }
282
283 }
284
285 @end
286
287 int main(int argc, const char *argv[])
288 {
289 return NSApplicationMain(argc, argv);
290 }