]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/DNSServiceReg.m
mDNSResponder-107.5.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.15 2004/06/05 02:01:08 cheshire
27 Move DNSServiceRegistration from mDNSMacOSX directory to Clients directory
28
29 Revision 1.14 2004/03/04 19:20:23 cheshire
30 Remove invalid UTF-8 character
31
32 Revision 1.13 2003/08/12 19:55:07 cheshire
33 Update to APSL 2.0
34
35 */
36
37 #include "dns_sd.h"
38
39 @interface RegistrationController : NSObject
40 {
41 IBOutlet NSTableColumn *typeColumn;
42 IBOutlet NSTableColumn *nameColumn;
43 IBOutlet NSTableColumn *portColumn;
44 IBOutlet NSTableColumn *domainColumn;
45 IBOutlet NSTableColumn *textColumn;
46
47 IBOutlet NSTableView *serviceDisplayTable;
48
49 IBOutlet NSTextField *serviceTypeField;
50 IBOutlet NSTextField *serviceNameField;
51 IBOutlet NSTextField *servicePortField;
52 IBOutlet NSTextField *serviceDomainField;
53 IBOutlet NSTextField *serviceTextField;
54
55 NSMutableArray *srvtypeKeys;
56 NSMutableArray *srvnameKeys;
57 NSMutableArray *srvportKeys;
58 NSMutableArray *srvdomainKeys;
59 NSMutableArray *srvtextKeys;
60
61 NSMutableDictionary *registeredDict;
62 }
63
64 - (IBAction)registerService:(id)sender;
65 - (IBAction)unregisterService:(id)sender;
66
67 - (IBAction)addNewService:(id)sender;
68 - (IBAction)removeSelected:(id)sender;
69
70 @end
71
72 void reg_reply
73 (
74 DNSServiceRef sdRef,
75 DNSServiceFlags flags,
76 DNSServiceErrorType errorCode,
77 const char *name,
78 const char *regtype,
79 const char *domain,
80 void *context
81 )
82 {
83 // registration reply
84 printf("Got a reply from the server with error %d\n", errorCode);
85 return;
86 }
87
88 static void myCFSocketCallBack(CFSocketRef cfs, CFSocketCallBackType CallBackType, CFDataRef address, const void *data, void *context)
89 {
90 DNSServiceProcessResult((DNSServiceRef)context);
91 }
92
93 static void addDNSServiceRefToRunLoop(DNSServiceRef ref)
94 {
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);
100 CFRelease(rls);
101 }
102
103
104 @implementation RegistrationController
105
106 - (void)registerDefaults
107 {
108 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
109
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];
115
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"];
121
122 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
123 }
124
125 - (id)init
126 {
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];
132
133 registeredDict = [[NSMutableDictionary alloc] init];
134
135 [self registerDefaults];
136 return [super init];
137 }
138
139 - (void)awakeFromNib //BrowserController startup procedure
140 {
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"]];
146
147 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
148
149 }
150
151
152
153 - (IBAction)registerService:(id)sender
154 {
155 int selectedRow = [serviceDisplayTable selectedRow];
156 CFMachPortContext context;
157 DNSServiceRef dns_client;
158
159 if (selectedRow < 0) {
160 return;
161 }
162
163 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
164 if ([registeredDict objectForKey:key]) { printf("Already registered\n"); return; }
165
166 context.version = 1;
167 context.info = 0;
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);
174
175 DNSServiceErrorType err = DNSServiceRegister
176 (
177 &dns_client, 0, 0,
178 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
179 [key UTF8String],
180 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
181 NULL, htons([[srvportKeys objectAtIndex:selectedRow] intValue]),
182 txtbuffer[0]+1, txtbuffer,
183 reg_reply,
184 nil
185 );
186 if (err)
187 printf("DNSServiceRegister failed %d\n", err);
188 else
189 {
190 addDNSServiceRefToRunLoop(dns_client);
191 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:key];
192 }
193 }
194
195 - (IBAction)unregisterService:(id)sender
196 {
197 int selectedRow = [serviceDisplayTable selectedRow];
198 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
199
200 NSNumber *refPtr = [registeredDict objectForKey:key];
201 DNSServiceRef ref = (DNSServiceRef)[refPtr unsignedIntValue];
202
203 if (ref) {
204 DNSServiceRefDeallocate(ref);
205 [registeredDict removeObjectForKey:key];
206 }
207 }
208
209 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
210 {
211 if (row<0) return;
212 }
213
214 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
215 {
216 return [srvtypeKeys count];
217 }
218
219 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
220 {
221 if (theColumn == typeColumn) {
222 return [srvtypeKeys objectAtIndex:rowIndex];
223 }
224 if (theColumn == nameColumn) {
225 return [srvnameKeys objectAtIndex:rowIndex];
226 }
227 if (theColumn == portColumn) {
228 return [srvportKeys objectAtIndex:rowIndex];
229 }
230 if (theColumn == domainColumn) {
231 return [srvdomainKeys objectAtIndex:rowIndex];
232 }
233 if (theColumn == textColumn) {
234 return [srvtextKeys objectAtIndex:rowIndex];
235 }
236
237 return(0);
238 } //End of mandatory TableView methods
239
240 - (IBAction)removeSelected:(id)sender
241 {
242 // remove the selected row and force a refresh
243
244 int selectedRow = [serviceDisplayTable selectedRow];
245
246 if (selectedRow) {
247
248 [srvtypeKeys removeObjectAtIndex:selectedRow];
249 [srvnameKeys removeObjectAtIndex:selectedRow];
250 [srvportKeys removeObjectAtIndex:selectedRow];
251 [srvdomainKeys removeObjectAtIndex:selectedRow];
252 [srvtextKeys removeObjectAtIndex:selectedRow];
253
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"];
259
260 [serviceDisplayTable reloadData];
261 }
262 }
263
264 - (IBAction)addNewService:(id)sender
265 {
266 // add new entries from the edit fields to the arrays for the defaults
267
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]];
274
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"];
280
281 [serviceDisplayTable reloadData];
282 } else {
283 NSBeep();
284 }
285
286 }
287
288 @end
289
290 int main(int argc, const char *argv[])
291 {
292 return NSApplicationMain(argc, argv);
293 }