]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/Client.c
mDNSResponder-58.3.tar.gz
[apple/mdnsresponder.git] / mDNSPosix / Client.c
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: Client.c,v $
26 Revision 1.11 2003/11/17 20:14:32 cheshire
27 Typo: Wrote "domC" where it should have said "domainC"
28
29 Revision 1.10 2003/11/14 21:27:09 cheshire
30 <rdar://problem/3484766>: Security: Crashing bug in mDNSResponder
31 Fix code that should use buffer size MAX_ESCAPED_DOMAIN_NAME (1005) instead of 256-byte buffers.
32
33 Revision 1.9 2003/08/14 02:19:55 cheshire
34 <rdar://problem/3375491> Split generic ResourceRecord type into two separate types: AuthRecord and CacheRecord
35
36 Revision 1.8 2003/08/12 19:56:26 cheshire
37 Update to APSL 2.0
38
39 Revision 1.7 2003/07/02 21:19:58 cheshire
40 <rdar://problem/3313413> Update copyright notices, etc., in source code comments
41
42 Revision 1.6 2003/06/18 05:48:41 cheshire
43 Fix warnings
44
45 Revision 1.5 2003/05/06 00:00:50 cheshire
46 <rdar://problem/3248914> Rationalize naming of domainname manipulation functions
47
48 Revision 1.4 2002/12/23 22:13:31 jgraessl
49
50 Reviewed by: Stuart Cheshire
51 Initial IPv6 support for mDNSResponder.
52
53 Revision 1.3 2002/09/21 20:44:53 zarzycki
54 Added APSL info
55
56 Revision 1.2 2002/09/19 04:20:44 cheshire
57 Remove high-ascii characters that confuse some systems
58
59 Revision 1.1 2002/09/17 06:24:35 cheshire
60 First checkin
61
62 */
63
64 #include <assert.h>
65 #include <signal.h>
66 #include <stdio.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include <stdlib.h>
70
71 #include "mDNSClientAPI.h"// Defines the interface to the mDNS core code
72 #include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform
73 #include "ExampleClientApp.h"
74
75 // Globals
76 static mDNS mDNSStorage; // mDNS core uses this to store its globals
77 static mDNS_PlatformSupport PlatformStorage; // Stores this platform's globals
78 #define RR_CACHE_SIZE 500
79 static CacheRecord gRRCache[RR_CACHE_SIZE];
80
81 static const char *gProgramName = "mDNSResponderPosix";
82
83 static void BrowseCallback(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, mDNSBool AddRecord)
84 // A callback from the core mDNS code that indicates that we've received a
85 // response to our query. Note that this code runs on the main thread
86 // (in fact, there is only one thread!), so we can safely printf the results.
87 {
88 domainlabel name;
89 domainname type;
90 domainname domain;
91 char nameC [MAX_DOMAIN_LABEL+1]; // Unescaped name: up to 63 bytes plus C-string terminating NULL.
92 char typeC [MAX_ESCAPED_DOMAIN_NAME];
93 char domainC[MAX_ESCAPED_DOMAIN_NAME];
94 const char *state;
95
96 (void)m; // Unused
97 (void)question; // Unused
98
99 assert(answer->rrtype == kDNSType_PTR);
100
101 DeconstructServiceName(&answer->rdata->u.name, &name, &type, &domain);
102
103 ConvertDomainLabelToCString_unescaped(&name, nameC);
104 ConvertDomainNameToCString(&type, typeC);
105 ConvertDomainNameToCString(&domain, domainC);
106
107 // If the TTL has hit 0, the service is no longer available.
108 if (!AddRecord) {
109 state = "Lost ";
110 } else {
111 state = "Found";
112 }
113 fprintf(stderr, "*** %s name = '%s', type = '%s', domain = '%s'\n", state, nameC, typeC, domainC);
114 }
115
116 static mDNSBool CheckThatServiceTypeIsUsable(const char *serviceType, mDNSBool printExplanation)
117 // Checks that serviceType is a reasonable service type
118 // label and, if it isn't and printExplanation is true, prints
119 // an explanation of why not.
120 {
121 mDNSBool result;
122
123 result = mDNStrue;
124 if (result && strlen(serviceType) > 63) {
125 if (printExplanation) {
126 fprintf(stderr,
127 "%s: Service type specified by -t is too long (must be 63 characters or less)\n",
128 gProgramName);
129 }
130 result = mDNSfalse;
131 }
132 if (result && serviceType[0] == 0) {
133 if (printExplanation) {
134 fprintf(stderr,
135 "%s: Service type specified by -t can't be empty\n",
136 gProgramName);
137 }
138 result = mDNSfalse;
139 }
140 return result;
141 }
142
143 static const char kDefaultServiceType[] = "_afpovertcp._tcp.";
144
145 static void PrintUsage()
146 {
147 fprintf(stderr,
148 "Usage: %s [-v level] [-t type]\n",
149 gProgramName);
150 fprintf(stderr, " -v verbose mode, level is a number from 0 to 2\n");
151 fprintf(stderr, " 0 = no debugging info (default)\n");
152 fprintf(stderr, " 1 = standard debugging info\n");
153 fprintf(stderr, " 2 = intense debugging info\n");
154 fprintf(stderr, " -t uses 'type' as the service type (default is '%s')\n", kDefaultServiceType);
155 }
156
157 static const char *gServiceType = kDefaultServiceType;
158
159 static void ParseArguments(int argc, char **argv)
160 // Parses our command line arguments into the global variables
161 // listed above.
162 {
163 int ch;
164
165 // Set gProgramName to the last path component of argv[0]
166
167 gProgramName = strrchr(argv[0], '/');
168 if (gProgramName == NULL) {
169 gProgramName = argv[0];
170 } else {
171 gProgramName += 1;
172 }
173
174 // Parse command line options using getopt.
175
176 do {
177 ch = getopt(argc, argv, "v:t:");
178 if (ch != -1) {
179 switch (ch) {
180 case 'v':
181 gMDNSPlatformPosixVerboseLevel = atoi(optarg);
182 if (gMDNSPlatformPosixVerboseLevel < 0 || gMDNSPlatformPosixVerboseLevel > 2) {
183 fprintf(stderr,
184 "%s: Verbose mode must be in the range 0..2\n",
185 gProgramName);
186 exit(1);
187 }
188 break;
189 case 't':
190 gServiceType = optarg;
191 if ( ! CheckThatServiceTypeIsUsable(gServiceType, mDNStrue) ) {
192 exit(1);
193 }
194 break;
195 case '?':
196 default:
197 PrintUsage();
198 exit(1);
199 break;
200 }
201 }
202 } while (ch != -1);
203
204 // Check for any left over command line arguments.
205
206 if (optind != argc) {
207 fprintf(stderr, "%s: Unexpected argument '%s'\n", gProgramName, argv[optind]);
208 exit(1);
209 }
210 }
211
212 int main(int argc, char **argv)
213 // The program's main entry point. The program does a trivial
214 // mDNS query, looking for all AFP servers in the local domain.
215 {
216 int result;
217 mStatus status;
218 DNSQuestion question;
219 domainname type;
220 domainname domain;
221
222 // Parse our command line arguments. This won't come back if there's an error.
223 ParseArguments(argc, argv);
224
225 // Initialise the mDNS core.
226 status = mDNS_Init(&mDNSStorage, &PlatformStorage,
227 gRRCache, RR_CACHE_SIZE,
228 mDNS_Init_DontAdvertiseLocalAddresses,
229 mDNS_Init_NoInitCallback, mDNS_Init_NoInitCallbackContext);
230 if (status == mStatus_NoError) {
231
232 // Construct and start the query.
233
234 MakeDomainNameFromDNSNameString(&type, gServiceType);
235 MakeDomainNameFromDNSNameString(&domain, "local.");
236
237 status = mDNS_StartBrowse(&mDNSStorage, &question, &type, &domain, mDNSInterface_Any, BrowseCallback, NULL);
238
239 // Run the platform main event loop until the user types ^C.
240 // The BrowseCallback routine is responsible for printing
241 // any results that we find.
242
243 if (status == mStatus_NoError) {
244 fprintf(stderr, "Hit ^C when you're bored waiting for responses.\n");
245 ExampleClientEventLoop(&mDNSStorage);
246 mDNS_StopQuery(&mDNSStorage, &question);
247 mDNS_Close(&mDNSStorage);
248 }
249 }
250
251 if (status == mStatus_NoError) {
252 result = 0;
253 } else {
254 result = 2;
255 }
256 if ( (result != 0) || (gMDNSPlatformPosixVerboseLevel > 0) ) {
257 fprintf(stderr, "%s: Finished with status %ld, result %d\n", gProgramName, status, result);
258 }
259
260 return 0;
261 }