2 * Copyright (c) 2002-2003 Apple Computer, 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@
23 Change History (most recent first):
25 $Log: DNSServiceReg.m,v $
26 Revision 1.15 2004/06/05 02:01:08 cheshire
27 Move DNSServiceRegistration from mDNSMacOSX directory to Clients directory
29 Revision 1.14 2004/03/04 19:20:23 cheshire
30 Remove invalid UTF-8 character
32 Revision 1.13 2003/08/12 19:55:07 cheshire
39 @interface RegistrationController : NSObject
41 IBOutlet NSTableColumn *typeColumn;
42 IBOutlet NSTableColumn *nameColumn;
43 IBOutlet NSTableColumn *portColumn;
44 IBOutlet NSTableColumn *domainColumn;
45 IBOutlet NSTableColumn *textColumn;
47 IBOutlet NSTableView *serviceDisplayTable;
49 IBOutlet NSTextField *serviceTypeField;
50 IBOutlet NSTextField *serviceNameField;
51 IBOutlet NSTextField *servicePortField;
52 IBOutlet NSTextField *serviceDomainField;
53 IBOutlet NSTextField *serviceTextField;
55 NSMutableArray *srvtypeKeys;
56 NSMutableArray *srvnameKeys;
57 NSMutableArray *srvportKeys;
58 NSMutableArray *srvdomainKeys;
59 NSMutableArray *srvtextKeys;
61 NSMutableDictionary *registeredDict;
64 - (IBAction)registerService:(id)sender;
65 - (IBAction)unregisterService:(id)sender;
67 - (IBAction)addNewService:(id)sender;
68 - (IBAction)removeSelected:(id)sender;
75 DNSServiceFlags flags,
76 DNSServiceErrorType errorCode,
84 printf("Got a reply from the server with error %d\n", errorCode);
88 static void myCFSocketCallBack(CFSocketRef cfs, CFSocketCallBackType CallBackType, CFDataRef address, const void *data, void *context)
90 DNSServiceProcessResult((DNSServiceRef)context);
93 static void addDNSServiceRefToRunLoop(DNSServiceRef ref)
95 int s = DNSServiceRefSockFD(ref);
96 CFSocketContext myCFSocketContext = { 0, ref, NULL, NULL, NULL };
97 CFSocketRef c = CFSocketCreateWithNative(kCFAllocatorDefault, s, kCFSocketReadCallBack, myCFSocketCallBack, &myCFSocketContext);
98 CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, c, 0);
99 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
104 @implementation RegistrationController
106 - (void)registerDefaults
108 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
110 NSArray *typeArray = [NSArray arrayWithObjects:@"_ftp._tcp.", @"_ssh._tcp.", @"_tftp._tcp.", @"_http._tcp.", @"_printer._tcp.", @"_afpovertcp._tcp.", nil];
111 NSArray *nameArray = [NSArray arrayWithObjects:@"My ftp Server", @"My Computer", @"Testing Boot Image", @"A Web Server", @"Steve's Printer", @"Company AppleShare Server", nil];
112 NSArray *portArray = [NSArray arrayWithObjects:@"21", @"22", @"69", @"80", @"515", @"548", nil];
113 NSArray *domainArray = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", nil];
114 NSArray *textArray = [NSArray arrayWithObjects:@"", @"", @"image=mybootimage", @"path=/index.html", @"rn=lpt1", @"Vol=Public", nil];
116 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
117 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
118 [regDict setObject:portArray forKey:@"SrvPortKeys"];
119 [regDict setObject:domainArray forKey:@"SrvDomainKeys"];
120 [regDict setObject:textArray forKey:@"SrvTextKeys"];
122 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
127 srvtypeKeys = [[NSMutableArray array] retain]; //Define arrays for Type, Domain, and Name
128 srvnameKeys = [[NSMutableArray array] retain];
129 srvportKeys = [[NSMutableArray array] retain];
130 srvdomainKeys = [[NSMutableArray array] retain];
131 srvtextKeys = [[NSMutableArray array] retain];
133 registeredDict = [[NSMutableDictionary alloc] init];
135 [self registerDefaults];
139 - (void)awakeFromNib //BrowserController startup procedure
141 [srvtypeKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"]];
142 [srvnameKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"]];
143 [srvportKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvPortKeys"]];
144 [srvdomainKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvDomainKeys"]];
145 [srvtextKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTextKeys"]];
147 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
153 - (IBAction)registerService:(id)sender
155 int selectedRow = [serviceDisplayTable selectedRow];
156 CFMachPortContext context;
157 DNSServiceRef dns_client;
159 if (selectedRow < 0) {
163 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
164 if ([registeredDict objectForKey:key]) { printf("Already registered\n"); return; }
168 context.retain = NULL;
169 context.release = NULL;
170 context.copyDescription = NULL;
171 unsigned char txtbuffer[300];
172 strncpy(txtbuffer+1, [[srvtextKeys objectAtIndex:selectedRow] UTF8String], sizeof(txtbuffer)-1);
173 txtbuffer[0] = strlen(txtbuffer+1);
175 DNSServiceErrorType err = DNSServiceRegister
178 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
180 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
181 NULL, htons([[srvportKeys objectAtIndex:selectedRow] intValue]),
182 txtbuffer[0]+1, txtbuffer,
187 printf("DNSServiceRegister failed %d\n", err);
190 addDNSServiceRefToRunLoop(dns_client);
191 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:key];
195 - (IBAction)unregisterService:(id)sender
197 int selectedRow = [serviceDisplayTable selectedRow];
198 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
200 NSNumber *refPtr = [registeredDict objectForKey:key];
201 DNSServiceRef ref = (DNSServiceRef)[refPtr unsignedIntValue];
204 DNSServiceRefDeallocate(ref);
205 [registeredDict removeObjectForKey:key];
209 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
214 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
216 return [srvtypeKeys count];
219 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
221 if (theColumn == typeColumn) {
222 return [srvtypeKeys objectAtIndex:rowIndex];
224 if (theColumn == nameColumn) {
225 return [srvnameKeys objectAtIndex:rowIndex];
227 if (theColumn == portColumn) {
228 return [srvportKeys objectAtIndex:rowIndex];
230 if (theColumn == domainColumn) {
231 return [srvdomainKeys objectAtIndex:rowIndex];
233 if (theColumn == textColumn) {
234 return [srvtextKeys objectAtIndex:rowIndex];
238 } //End of mandatory TableView methods
240 - (IBAction)removeSelected:(id)sender
242 // remove the selected row and force a refresh
244 int selectedRow = [serviceDisplayTable selectedRow];
248 [srvtypeKeys removeObjectAtIndex:selectedRow];
249 [srvnameKeys removeObjectAtIndex:selectedRow];
250 [srvportKeys removeObjectAtIndex:selectedRow];
251 [srvdomainKeys removeObjectAtIndex:selectedRow];
252 [srvtextKeys removeObjectAtIndex:selectedRow];
254 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
255 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
256 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
257 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
258 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
260 [serviceDisplayTable reloadData];
264 - (IBAction)addNewService:(id)sender
266 // add new entries from the edit fields to the arrays for the defaults
268 if ([[serviceTypeField stringValue] length] && [[serviceNameField stringValue] length] && [[serviceDomainField stringValue] length]&& [[servicePortField stringValue] length]) {
269 [srvtypeKeys addObject:[serviceTypeField stringValue]];
270 [srvnameKeys addObject:[serviceNameField stringValue]];
271 [srvportKeys addObject:[servicePortField stringValue]];
272 [srvdomainKeys addObject:[serviceDomainField stringValue]];
273 [srvtextKeys addObject:[serviceTextField stringValue]];
275 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
276 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
277 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
278 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
279 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
281 [serviceDisplayTable reloadData];
290 int main(int argc, const char *argv[])
292 return NSApplicationMain(argc, argv);