]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/DNSServiceReg.m
mDNSResponder-379.27.tar.gz
[apple/mdnsresponder.git] / Clients / DNSServiceReg.m
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18 #include "dns_sd.h"
19
20 @interface RegistrationController : NSObject
21 {
22 IBOutlet NSTableColumn *typeColumn;
23 IBOutlet NSTableColumn *nameColumn;
24 IBOutlet NSTableColumn *portColumn;
25 IBOutlet NSTableColumn *domainColumn;
26 IBOutlet NSTableColumn *textColumn;
27
28 IBOutlet NSTableView *serviceDisplayTable;
29
30 IBOutlet NSTextField *serviceTypeField;
31 IBOutlet NSTextField *serviceNameField;
32 IBOutlet NSTextField *servicePortField;
33 IBOutlet NSTextField *serviceDomainField;
34 IBOutlet NSTextField *serviceTextField;
35
36 NSMutableArray *srvtypeKeys;
37 NSMutableArray *srvnameKeys;
38 NSMutableArray *srvportKeys;
39 NSMutableArray *srvdomainKeys;
40 NSMutableArray *srvtextKeys;
41
42 NSMutableDictionary *registeredDict;
43 }
44
45 - (IBAction)registerService:(id)sender;
46 - (IBAction)unregisterService:(id)sender;
47
48 - (IBAction)addNewService:(id)sender;
49 - (IBAction)removeSelected:(id)sender;
50
51 @end
52
53 void reg_reply
54 (
55 DNSServiceRef sdRef,
56 DNSServiceFlags flags,
57 DNSServiceErrorType errorCode,
58 const char *name,
59 const char *regtype,
60 const char *domain,
61 void *context
62 )
63 {
64 // registration reply
65 printf("Got a reply from the server with error %d\n", errorCode);
66 return;
67 }
68
69 static void myCFSocketCallBack(CFSocketRef cfs, CFSocketCallBackType CallBackType, CFDataRef address, const void *data, void *context)
70 {
71 DNSServiceProcessResult((DNSServiceRef)context);
72 }
73
74 static void addDNSServiceRefToRunLoop(DNSServiceRef ref)
75 {
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);
81 CFRelease(rls);
82 }
83
84
85 @implementation RegistrationController
86
87 - (void)registerDefaults
88 {
89 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
90
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];
96
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"];
102
103 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
104 }
105
106 - (id)init
107 {
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];
113
114 registeredDict = [[NSMutableDictionary alloc] init];
115
116 [self registerDefaults];
117 return [super init];
118 }
119
120 - (void)awakeFromNib //BrowserController startup procedure
121 {
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"]];
127
128 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
129
130 }
131
132
133
134 - (IBAction)registerService:(id)sender
135 {
136 int selectedRow = [serviceDisplayTable selectedRow];
137 CFMachPortContext context;
138 DNSServiceRef dns_client;
139
140 if (selectedRow < 0) {
141 return;
142 }
143
144 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
145 if ([registeredDict objectForKey:key]) { printf("Already registered\n"); return; }
146
147 context.version = 1;
148 context.info = 0;
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);
155
156 DNSServiceErrorType err = DNSServiceRegister
157 (
158 &dns_client, 0, 0,
159 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
160 [key UTF8String],
161 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
162 NULL, htons([[srvportKeys objectAtIndex:selectedRow] intValue]),
163 txtbuffer[0]+1, txtbuffer,
164 reg_reply,
165 nil
166 );
167 if (err)
168 printf("DNSServiceRegister failed %d\n", err);
169 else
170 {
171 addDNSServiceRefToRunLoop(dns_client);
172 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:key];
173 }
174 }
175
176 - (IBAction)unregisterService:(id)sender
177 {
178 int selectedRow = [serviceDisplayTable selectedRow];
179 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
180
181 NSNumber *refPtr = [registeredDict objectForKey:key];
182 DNSServiceRef ref = (DNSServiceRef)[refPtr unsignedIntValue];
183
184 if (ref) {
185 DNSServiceRefDeallocate(ref);
186 [registeredDict removeObjectForKey:key];
187 }
188 }
189
190 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
191 {
192 if (row<0) return;
193 }
194
195 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
196 {
197 return [srvtypeKeys count];
198 }
199
200 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
201 {
202 if (theColumn == typeColumn) {
203 return [srvtypeKeys objectAtIndex:rowIndex];
204 }
205 if (theColumn == nameColumn) {
206 return [srvnameKeys objectAtIndex:rowIndex];
207 }
208 if (theColumn == portColumn) {
209 return [srvportKeys objectAtIndex:rowIndex];
210 }
211 if (theColumn == domainColumn) {
212 return [srvdomainKeys objectAtIndex:rowIndex];
213 }
214 if (theColumn == textColumn) {
215 return [srvtextKeys objectAtIndex:rowIndex];
216 }
217
218 return(0);
219 } //End of mandatory TableView methods
220
221 - (IBAction)removeSelected:(id)sender
222 {
223 // remove the selected row and force a refresh
224
225 int selectedRow = [serviceDisplayTable selectedRow];
226
227 if (selectedRow) {
228
229 [srvtypeKeys removeObjectAtIndex:selectedRow];
230 [srvnameKeys removeObjectAtIndex:selectedRow];
231 [srvportKeys removeObjectAtIndex:selectedRow];
232 [srvdomainKeys removeObjectAtIndex:selectedRow];
233 [srvtextKeys removeObjectAtIndex:selectedRow];
234
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"];
240
241 [serviceDisplayTable reloadData];
242 }
243 }
244
245 - (IBAction)addNewService:(id)sender
246 {
247 // add new entries from the edit fields to the arrays for the defaults
248
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]];
255
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"];
261
262 [serviceDisplayTable reloadData];
263 } else {
264 NSBeep();
265 }
266
267 }
268
269 @end
270
271 int main(int argc, const char *argv[])
272 {
273 return NSApplicationMain(argc, argv);
274 }