1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
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
23 Revision 1.15 2004/06/05 02:01:08 cheshire
24 Move DNSServiceRegistration from mDNSMacOSX directory to Clients directory
26 Revision 1.14 2004/03/04 19:20:23 cheshire
27 Remove invalid UTF-8 character
29 Revision 1.13 2003/08/12 19:55:07 cheshire
36 @interface RegistrationController : NSObject
38 IBOutlet NSTableColumn *typeColumn;
39 IBOutlet NSTableColumn *nameColumn;
40 IBOutlet NSTableColumn *portColumn;
41 IBOutlet NSTableColumn *domainColumn;
42 IBOutlet NSTableColumn *textColumn;
44 IBOutlet NSTableView *serviceDisplayTable;
46 IBOutlet NSTextField *serviceTypeField;
47 IBOutlet NSTextField *serviceNameField;
48 IBOutlet NSTextField *servicePortField;
49 IBOutlet NSTextField *serviceDomainField;
50 IBOutlet NSTextField *serviceTextField;
52 NSMutableArray *srvtypeKeys;
53 NSMutableArray *srvnameKeys;
54 NSMutableArray *srvportKeys;
55 NSMutableArray *srvdomainKeys;
56 NSMutableArray *srvtextKeys;
58 NSMutableDictionary *registeredDict;
61 - (IBAction)registerService:(id)sender;
62 - (IBAction)unregisterService:(id)sender;
64 - (IBAction)addNewService:(id)sender;
65 - (IBAction)removeSelected:(id)sender;
72 DNSServiceFlags flags,
73 DNSServiceErrorType errorCode,
81 printf("Got a reply from the server with error %d\n", errorCode);
85 static void myCFSocketCallBack(CFSocketRef cfs, CFSocketCallBackType CallBackType, CFDataRef address, const void *data, void *context)
87 DNSServiceProcessResult((DNSServiceRef)context);
90 static void addDNSServiceRefToRunLoop(DNSServiceRef ref)
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);
101 @implementation RegistrationController
103 - (void)registerDefaults
105 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
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];
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"];
119 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
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];
130 registeredDict = [[NSMutableDictionary alloc] init];
132 [self registerDefaults];
136 - (void)awakeFromNib //BrowserController startup procedure
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"]];
144 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
150 - (IBAction)registerService:(id)sender
152 int selectedRow = [serviceDisplayTable selectedRow];
153 CFMachPortContext context;
154 DNSServiceRef dns_client;
156 if (selectedRow < 0) {
160 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
161 if ([registeredDict objectForKey:key]) { printf("Already registered\n"); return; }
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);
172 DNSServiceErrorType err = DNSServiceRegister
175 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
177 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
178 NULL, htons([[srvportKeys objectAtIndex:selectedRow] intValue]),
179 txtbuffer[0]+1, txtbuffer,
184 printf("DNSServiceRegister failed %d\n", err);
187 addDNSServiceRefToRunLoop(dns_client);
188 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:key];
192 - (IBAction)unregisterService:(id)sender
194 int selectedRow = [serviceDisplayTable selectedRow];
195 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
197 NSNumber *refPtr = [registeredDict objectForKey:key];
198 DNSServiceRef ref = (DNSServiceRef)[refPtr unsignedIntValue];
201 DNSServiceRefDeallocate(ref);
202 [registeredDict removeObjectForKey:key];
206 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
211 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
213 return [srvtypeKeys count];
216 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
218 if (theColumn == typeColumn) {
219 return [srvtypeKeys objectAtIndex:rowIndex];
221 if (theColumn == nameColumn) {
222 return [srvnameKeys objectAtIndex:rowIndex];
224 if (theColumn == portColumn) {
225 return [srvportKeys objectAtIndex:rowIndex];
227 if (theColumn == domainColumn) {
228 return [srvdomainKeys objectAtIndex:rowIndex];
230 if (theColumn == textColumn) {
231 return [srvtextKeys objectAtIndex:rowIndex];
235 } //End of mandatory TableView methods
237 - (IBAction)removeSelected:(id)sender
239 // remove the selected row and force a refresh
241 int selectedRow = [serviceDisplayTable selectedRow];
245 [srvtypeKeys removeObjectAtIndex:selectedRow];
246 [srvnameKeys removeObjectAtIndex:selectedRow];
247 [srvportKeys removeObjectAtIndex:selectedRow];
248 [srvdomainKeys removeObjectAtIndex:selectedRow];
249 [srvtextKeys removeObjectAtIndex:selectedRow];
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"];
257 [serviceDisplayTable reloadData];
261 - (IBAction)addNewService:(id)sender
263 // add new entries from the edit fields to the arrays for the defaults
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]];
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"];
278 [serviceDisplayTable reloadData];
287 int main(int argc, const char *argv[])
289 return NSApplicationMain(argc, argv);