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.
20 @interface RegistrationController : NSObject
22 IBOutlet NSTableColumn *typeColumn;
23 IBOutlet NSTableColumn *nameColumn;
24 IBOutlet NSTableColumn *portColumn;
25 IBOutlet NSTableColumn *domainColumn;
26 IBOutlet NSTableColumn *textColumn;
28 IBOutlet NSTableView *serviceDisplayTable;
30 IBOutlet NSTextField *serviceTypeField;
31 IBOutlet NSTextField *serviceNameField;
32 IBOutlet NSTextField *servicePortField;
33 IBOutlet NSTextField *serviceDomainField;
34 IBOutlet NSTextField *serviceTextField;
36 NSMutableArray *srvtypeKeys;
37 NSMutableArray *srvnameKeys;
38 NSMutableArray *srvportKeys;
39 NSMutableArray *srvdomainKeys;
40 NSMutableArray *srvtextKeys;
42 NSMutableDictionary *registeredDict;
45 - (IBAction)registerService:(id)sender;
46 - (IBAction)unregisterService:(id)sender;
48 - (IBAction)addNewService:(id)sender;
49 - (IBAction)removeSelected:(id)sender;
56 DNSServiceFlags flags,
57 DNSServiceErrorType errorCode,
65 printf("Got a reply from the server with error %d\n", errorCode);
69 static void myCFSocketCallBack(CFSocketRef cfs, CFSocketCallBackType CallBackType, CFDataRef address, const void *data, void *context)
71 DNSServiceProcessResult((DNSServiceRef)context);
74 static void addDNSServiceRefToRunLoop(DNSServiceRef ref)
76 int s = DNSServiceRefSockFD(ref);
77 CFSocketContext myCFSocketContext = { 0, ref, NULL, NULL, NULL };
78 CFSocketRef c = CFSocketCreateWithNative(kCFAllocatorDefault, s, kCFSocketReadCallBack, myCFSocketCallBack, &myCFSocketContext);
79 CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, c, 0);
80 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
85 @implementation RegistrationController
87 - (void)registerDefaults
89 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
91 NSArray *typeArray = [NSArray arrayWithObjects:@"_ftp._tcp.", @"_ssh._tcp.", @"_tftp._tcp.", @"_http._tcp.", @"_printer._tcp.", @"_afpovertcp._tcp.", nil];
92 NSArray *nameArray = [NSArray arrayWithObjects:@"My ftp Server", @"My Computer", @"Testing Boot Image", @"A Web Server", @"Steve's Printer", @"Company AppleShare Server", nil];
93 NSArray *portArray = [NSArray arrayWithObjects:@"21", @"22", @"69", @"80", @"515", @"548", nil];
94 NSArray *domainArray = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", nil];
95 NSArray *textArray = [NSArray arrayWithObjects:@"", @"", @"image=mybootimage", @"path=/index.html", @"rn=lpt1", @"Vol=Public", nil];
97 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
98 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
99 [regDict setObject:portArray forKey:@"SrvPortKeys"];
100 [regDict setObject:domainArray forKey:@"SrvDomainKeys"];
101 [regDict setObject:textArray forKey:@"SrvTextKeys"];
103 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
108 srvtypeKeys = [[NSMutableArray array] retain]; //Define arrays for Type, Domain, and Name
109 srvnameKeys = [[NSMutableArray array] retain];
110 srvportKeys = [[NSMutableArray array] retain];
111 srvdomainKeys = [[NSMutableArray array] retain];
112 srvtextKeys = [[NSMutableArray array] retain];
114 registeredDict = [[NSMutableDictionary alloc] init];
116 [self registerDefaults];
120 - (void)awakeFromNib //BrowserController startup procedure
122 [srvtypeKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"]];
123 [srvnameKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"]];
124 [srvportKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvPortKeys"]];
125 [srvdomainKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvDomainKeys"]];
126 [srvtextKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTextKeys"]];
128 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
134 - (IBAction)registerService:(id)sender
136 int selectedRow = [serviceDisplayTable selectedRow];
137 CFMachPortContext context;
138 DNSServiceRef dns_client;
140 if (selectedRow < 0) {
144 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
145 if ([registeredDict objectForKey:key]) { printf("Already registered\n"); return; }
149 context.retain = NULL;
150 context.release = NULL;
151 context.copyDescription = NULL;
152 unsigned char txtbuffer[300];
153 strncpy(txtbuffer+1, [[srvtextKeys objectAtIndex:selectedRow] UTF8String], sizeof(txtbuffer)-1);
154 txtbuffer[0] = strlen(txtbuffer+1);
156 DNSServiceErrorType err = DNSServiceRegister
159 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
161 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
162 NULL, htons([[srvportKeys objectAtIndex:selectedRow] intValue]),
163 txtbuffer[0]+1, txtbuffer,
168 printf("DNSServiceRegister failed %d\n", err);
171 addDNSServiceRefToRunLoop(dns_client);
172 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:key];
176 - (IBAction)unregisterService:(id)sender
178 int selectedRow = [serviceDisplayTable selectedRow];
179 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
181 NSNumber *refPtr = [registeredDict objectForKey:key];
182 DNSServiceRef ref = (DNSServiceRef)[refPtr unsignedIntValue];
185 DNSServiceRefDeallocate(ref);
186 [registeredDict removeObjectForKey:key];
190 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
195 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
197 return [srvtypeKeys count];
200 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
202 if (theColumn == typeColumn) {
203 return [srvtypeKeys objectAtIndex:rowIndex];
205 if (theColumn == nameColumn) {
206 return [srvnameKeys objectAtIndex:rowIndex];
208 if (theColumn == portColumn) {
209 return [srvportKeys objectAtIndex:rowIndex];
211 if (theColumn == domainColumn) {
212 return [srvdomainKeys objectAtIndex:rowIndex];
214 if (theColumn == textColumn) {
215 return [srvtextKeys objectAtIndex:rowIndex];
219 } //End of mandatory TableView methods
221 - (IBAction)removeSelected:(id)sender
223 // remove the selected row and force a refresh
225 int selectedRow = [serviceDisplayTable selectedRow];
229 [srvtypeKeys removeObjectAtIndex:selectedRow];
230 [srvnameKeys removeObjectAtIndex:selectedRow];
231 [srvportKeys removeObjectAtIndex:selectedRow];
232 [srvdomainKeys removeObjectAtIndex:selectedRow];
233 [srvtextKeys removeObjectAtIndex:selectedRow];
235 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
236 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
237 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
238 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
239 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
241 [serviceDisplayTable reloadData];
245 - (IBAction)addNewService:(id)sender
247 // add new entries from the edit fields to the arrays for the defaults
249 if ([[serviceTypeField stringValue] length] && [[serviceNameField stringValue] length] && [[serviceDomainField stringValue] length]&& [[servicePortField stringValue] length]) {
250 [srvtypeKeys addObject:[serviceTypeField stringValue]];
251 [srvnameKeys addObject:[serviceNameField stringValue]];
252 [srvportKeys addObject:[servicePortField stringValue]];
253 [srvdomainKeys addObject:[serviceDomainField stringValue]];
254 [srvtextKeys addObject:[serviceTextField stringValue]];
256 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
257 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
258 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
259 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
260 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
262 [serviceDisplayTable reloadData];
271 int main(int argc, const char *argv[])
273 return NSApplicationMain(argc, argv);