]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/DNSServiceReg.m
mDNSResponder-58.8.1.tar.gz
[apple/mdnsresponder.git] / Clients / DNSServiceReg.m
1 /*
2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
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 Change History (most recent first):
24
25 $Log: DNSServiceReg.m,v $
26 Revision 1.13 2003/08/12 19:55:07 cheshire
27 Update to APSL 2.0
28
29 */
30
31 #import "RegistrationController.h"
32
33 #include <DNSServiceDiscovery/DNSServiceDiscovery.h>
34
35 void reg_reply (
36 int errorCode,
37 void *context
38 )
39 {
40 // registration reply
41 printf("Got a reply from the server with error %d\n", errorCode);
42 return;
43 }
44
45 void
46 MyHandleMachMessage ( CFMachPortRef port, void * msg, CFIndex size, void * info )
47 {
48 DNSServiceDiscovery_handleReply(msg);
49 }
50
51 @implementation RegistrationController
52
53 - (void)registerDefaults
54 {
55 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
56
57 NSArray *typeArray = [NSArray arrayWithObjects:@"_ftp._tcp.", @"_ssh._tcp.", @"_tftp._tcp.", @"_http._tcp.", @"_printer._tcp.", @"_afpovertcp._tcp.", nil];
58 NSArray *nameArray = [NSArray arrayWithObjects:@"My ftp Server", @"My Computer", @"Testing Boot Image", @"A Web Server", @"Steveƕs Printer", @"Company AppleShare Server", nil];
59 NSArray *portArray = [NSArray arrayWithObjects:@"21", @"22", @"69", @"80", @"515", @"548", nil];
60 NSArray *domainArray = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", nil];
61 NSArray *textArray = [NSArray arrayWithObjects:@"", @"", @"image=mybootimage", @"path=/index.html", @"rn=lpt1", @"Vol=Public", nil];
62
63 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
64 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
65 [regDict setObject:portArray forKey:@"SrvPortKeys"];
66 [regDict setObject:domainArray forKey:@"SrvDomainKeys"];
67 [regDict setObject:textArray forKey:@"SrvTextKeys"];
68
69 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
70 }
71
72 - (id)init
73 {
74 srvtypeKeys = [[NSMutableArray array] retain]; //Define arrays for Type, Domain, and Name
75 srvnameKeys = [[NSMutableArray array] retain];
76 srvportKeys = [[NSMutableArray array] retain];
77 srvdomainKeys = [[NSMutableArray array] retain];
78 srvtextKeys = [[NSMutableArray array] retain];
79
80 registeredDict = [[NSMutableDictionary alloc] init];
81
82 [self registerDefaults];
83 return [super init];
84 }
85
86 - (void)awakeFromNib //BrowserController startup procedure
87 {
88 [srvtypeKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"]];
89 [srvnameKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"]];
90 [srvportKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvPortKeys"]];
91 [srvdomainKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvDomainKeys"]];
92 [srvtextKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTextKeys"]];
93
94 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
95
96 }
97
98
99
100 - (IBAction)registerService:(id)sender
101 {
102 int selectedRow = [serviceDisplayTable selectedRow];
103 CFRunLoopSourceRef rls;
104 uint16_t registerPort;
105 CFMachPortRef cfMachPort;
106 CFMachPortContext context;
107 Boolean shouldFreeInfo;
108 dns_service_discovery_ref dns_client;
109 mach_port_t port;
110
111 if (selectedRow < 0) {
112 return;
113 }
114
115 context.version = 1;
116 context.info = 0;
117 context.retain = NULL;
118 context.release = NULL;
119 context.copyDescription = NULL;
120
121 registerPort = [[srvportKeys objectAtIndex:selectedRow] intValue];
122
123 dns_client = DNSServiceRegistrationCreate
124 (
125 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
126 [[srvtypeKeys objectAtIndex:selectedRow] UTF8String],
127 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
128 registerPort,
129 [[srvtextKeys objectAtIndex:selectedRow] UTF8String],
130 reg_reply,
131 nil
132 );
133
134 port = DNSServiceDiscoveryMachPort(dns_client);
135
136 if (port) {
137
138 //printf("port is %d\n", port);
139
140 cfMachPort = CFMachPortCreateWithPort ( kCFAllocatorDefault, port, ( CFMachPortCallBack ) MyHandleMachMessage,&context,&shouldFreeInfo );
141
142 rls = CFMachPortCreateRunLoopSource(NULL, cfMachPort, 0);
143 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
144 CFRelease(rls);
145 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:[srvtypeKeys objectAtIndex:selectedRow]];
146 } else {
147 printf("Could not obtain client port\n");
148 }
149
150 }
151
152 - (IBAction)unregisterService:(id)sender
153 {
154 int selectedRow = [serviceDisplayTable selectedRow];
155 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
156
157 NSNumber *refPtr = [registeredDict objectForKey:key];
158 dns_service_discovery_ref ref = (dns_service_discovery_ref)[refPtr unsignedIntValue];
159
160 if (ref) {
161 DNSServiceDiscoveryDeallocate(ref);
162 [registeredDict removeObjectForKey:key];
163 }
164 }
165
166 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
167 {
168 if (row<0) return;
169 }
170
171 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
172 {
173 return [srvtypeKeys count];
174 }
175
176 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
177 {
178 if (theColumn == typeColumn) {
179 return [srvtypeKeys objectAtIndex:rowIndex];
180 }
181 if (theColumn == nameColumn) {
182 return [srvnameKeys objectAtIndex:rowIndex];
183 }
184 if (theColumn == portColumn) {
185 return [srvportKeys objectAtIndex:rowIndex];
186 }
187 if (theColumn == domainColumn) {
188 return [srvdomainKeys objectAtIndex:rowIndex];
189 }
190 if (theColumn == textColumn) {
191 return [srvtextKeys objectAtIndex:rowIndex];
192 }
193
194 return(0);
195 } //End of mandatory TableView methods
196
197 - (IBAction)removeSelected:(id)sender
198 {
199 // remove the selected row and force a refresh
200
201 int selectedRow = [serviceDisplayTable selectedRow];
202
203 if (selectedRow) {
204
205 [srvtypeKeys removeObjectAtIndex:selectedRow];
206 [srvnameKeys removeObjectAtIndex:selectedRow];
207 [srvportKeys removeObjectAtIndex:selectedRow];
208 [srvdomainKeys removeObjectAtIndex:selectedRow];
209 [srvtextKeys removeObjectAtIndex:selectedRow];
210
211 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
212 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
213 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
214 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
215 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
216
217 [serviceDisplayTable reloadData];
218 }
219 }
220
221 - (IBAction)addNewService:(id)sender
222 {
223 // add new entries from the edit fields to the arrays for the defaults
224
225 if ([[serviceTypeField stringValue] length] && [[serviceNameField stringValue] length] && [[serviceDomainField stringValue] length]&& [[servicePortField stringValue] length]) {
226 [srvtypeKeys addObject:[serviceTypeField stringValue]];
227 [srvnameKeys addObject:[serviceNameField stringValue]];
228 [srvportKeys addObject:[servicePortField stringValue]];
229 [srvdomainKeys addObject:[serviceDomainField stringValue]];
230 [srvtextKeys addObject:[serviceTextField stringValue]];
231
232 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
233 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
234 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
235 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
236 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
237
238 [serviceDisplayTable reloadData];
239 } else {
240 NSBeep();
241 }
242
243 }
244
245
246
247 @end