]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOS9/Searcher.c
mDNSResponder-107.1.tar.gz
[apple/mdnsresponder.git] / mDNSMacOS9 / Searcher.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: Searcher.c,v $
26 Revision 1.2 2004/05/27 06:30:21 cheshire
27 Add code to test DNSServiceQueryRecord()
28
29 Revision 1.1 2004/03/12 21:30:25 cheshire
30 Build a System-Context Shared Library from mDNSCore, for the benefit of developers
31 like Muse Research who want to be able to use mDNS/DNS-SD from GPL-licensed code.
32
33 */
34
35 #include <stdio.h> // For printf()
36 #include <string.h> // For strcpy()
37
38 #include <Events.h> // For WaitNextEvent()
39 #include <CodeFragments.h> // For SIOkUnresolvedCFragSymbolAddress
40
41 #include <SIOUX.h> // For SIOUXHandleOneEvent()
42
43 #include <OpenTransport.h>
44 #include <OpenTptInternet.h>
45
46 #include "dns_sd.h"
47
48 #define ns_c_in 1
49 #define ns_t_a 1
50
51 typedef union { UInt8 b[2]; UInt16 NotAnInteger; } mDNSOpaque16;
52 static UInt16 mDNSVal16(mDNSOpaque16 x) { return((UInt16)(x.b[0]<<8 | x.b[1])); }
53 static mDNSOpaque16 mDNSOpaque16fromIntVal(UInt16 v)
54 { mDNSOpaque16 x; x.b[0] = (UInt8)(v >> 8); x.b[1] = (UInt8)(v & 0xFF); return(x); }
55
56 typedef struct
57 {
58 OTLIFO serviceinfolist;
59 Boolean headerPrinted;
60 Boolean lostRecords;
61 } SearcherServices;
62
63 typedef struct
64 {
65 SearcherServices *services;
66 char name[kDNSServiceMaxDomainName];
67 char type[kDNSServiceMaxDomainName];
68 char domn[kDNSServiceMaxDomainName];
69 char host[kDNSServiceMaxDomainName];
70 char text[kDNSServiceMaxDomainName];
71 InetHost address;
72 mDNSOpaque16 notAnIntPort;
73 DNSServiceRef sdRef;
74 Boolean add;
75 Boolean dom;
76 OTLink link;
77 } linkedServiceInfo;
78
79 static SearcherServices services;
80
81 // PrintServiceInfo prints the service information to standard out
82 // A real application might want to do something else with the information
83 static void PrintServiceInfo(SearcherServices *services)
84 {
85 OTLink *link = OTReverseList(OTLIFOStealList(&services->serviceinfolist));
86
87 while (link)
88 {
89 linkedServiceInfo *s = OTGetLinkObject(link, linkedServiceInfo, link);
90
91 if (!services->headerPrinted)
92 {
93 printf("%-55s Type Domain Target Host IP Address Port Info\n", "Name");
94 services->headerPrinted = true;
95 }
96
97 if (s->dom)
98 {
99 if (s->add) printf("%-55s available for browsing\n", s->domn);
100 else printf("%-55s no longer available for browsing\n", s->domn);
101 }
102 else
103 {
104 char ip[16];
105 unsigned char *p = (unsigned char *)&s->address;
106 sprintf(ip, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
107 printf("%-55s %-16s %-14s ", s->name, s->type, s->domn);
108 if (s->add) printf("%-15s %-15s %5d %s\n", s->host, ip, mDNSVal16(s->notAnIntPort), s->text);
109 else printf("Removed\n");
110 }
111
112 link = link->fNext;
113 OTFreeMem(s);
114 }
115 }
116
117 static void FoundInstanceAddress(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
118 const char *fullname, uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void *rdata, uint32_t ttl, void *context)
119 {
120 linkedServiceInfo *info = (linkedServiceInfo *)context;
121 SearcherServices *services = info->services;
122 (void)sdRef; // Unused
123 (void)interfaceIndex; // Unused
124 (void)fullname; // Unused
125 (void)ttl; // Unused
126 if (errorCode == kDNSServiceErr_NoError)
127 if (flags & kDNSServiceFlagsAdd)
128 if (rrclass == ns_c_in && rrtype == ns_t_a && rdlen == sizeof(info->address))
129 {
130 memcpy(&info->address, rdata, sizeof(info->address));
131 DNSServiceRefDeallocate(info->sdRef);
132 OTLIFOEnqueue(&services->serviceinfolist, &info->link);
133 }
134 }
135
136 static void FoundInstanceInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
137 DNSServiceErrorType errorCode, const char *fullname, const char *hosttarget, uint16_t notAnIntPort,
138 uint16_t txtLen, const char *txtRecord, void *context)
139 {
140 linkedServiceInfo *info = (linkedServiceInfo *)context;
141 SearcherServices *services = info->services;
142 (void)sdRef; // Unused
143 (void)flags; // Unused
144 (void)interfaceIndex; // Unused
145 (void)errorCode; // Unused
146 (void)fullname; // Unused
147 strcpy(info->host, hosttarget);
148 if (txtLen == 0) info->text[0] = 0;
149 else
150 {
151 strncpy(info->text, txtRecord+1, txtRecord[0]);
152 info->text[txtRecord[0]] = 0;
153 }
154 info->notAnIntPort.NotAnInteger = notAnIntPort;
155 DNSServiceRefDeallocate(info->sdRef);
156 DNSServiceQueryRecord(&info->sdRef, 0, 0, info->host, ns_t_a, ns_c_in, FoundInstanceAddress, info);
157 }
158
159 // When a new named instance of a service is found, FoundInstance() is called.
160 // In this sample code we turn around and immediately to a DNSServiceResolve() to resolve that service name
161 // to find its target host, port, and txtinfo, but a normal browing application would just display the name.
162 // Resolving every single thing you find can be quite hard on the network, so you shouldn't do this
163 // in a real application. Defer resolving until the client has picked which instance from the
164 // long list of services is the one they want to use, and then resolve only that one.
165 static void FoundInstance(DNSServiceRef client, DNSServiceFlags flags, uint32_t interface, DNSServiceErrorType errorCode,
166 const char *replyName, const char *replyType, const char *replyDomain, void *context)
167 {
168 #pragma unused(client, interface, errorCode)
169 SearcherServices *services = (SearcherServices *)context;
170 linkedServiceInfo *info;
171
172 if (!services) { DebugStr("\pFoundInstance: services is NULL"); return; }
173
174 info = (linkedServiceInfo *)OTAllocMem(sizeof(linkedServiceInfo));
175 if (!info) { services->lostRecords = true; return; }
176
177 info->services = services;
178 strcpy(info->name, replyName);
179 strcpy(info->type, replyType);
180 strcpy(info->domn, replyDomain);
181 info->text[0] = 0;
182 info->add = (flags & kDNSServiceFlagsAdd) ? true : false;
183 info->dom = false;
184
185 if (!info->add) // If TTL == 0 we're deleting a service,
186 OTLIFOEnqueue(&services->serviceinfolist, &info->link);
187 else // else we're adding a new service
188 DNSServiceResolve(&info->sdRef, 0, 0, info->name, info->type, info->domn, FoundInstanceInfo, info);
189 }
190
191 // YieldSomeTime() just cooperatively yields some time to other processes running on classic Mac OS
192 static Boolean YieldSomeTime(UInt32 milliseconds)
193 {
194 extern Boolean SIOUXQuitting;
195 EventRecord e;
196 WaitNextEvent(everyEvent, &e, milliseconds / 17, NULL);
197 SIOUXHandleOneEvent(&e);
198 return(SIOUXQuitting);
199 }
200
201 int main()
202 {
203 OSStatus err;
204 void *tempmem;
205 DNSServiceRef sdRef;
206 DNSServiceErrorType dse;
207
208 SIOUXSettings.asktosaveonclose = false;
209 SIOUXSettings.userwindowtitle = "\pMulticast DNS Searcher";
210 SIOUXSettings.rows = 40;
211 SIOUXSettings.columns = 160;
212
213 printf("DNS-SD Search Client\n\n");
214 printf("This software reports errors using MacsBug breaks,\n");
215 printf("so if you don't have MacsBug installed your Mac may crash.\n\n");
216 printf("******************************************************************************\n\n");
217
218 if (DNSServiceBrowse == (void*)kUnresolvedCFragSymbolAddress)
219 {
220 printf("Before you can use mDNS/DNS-SD clients, you need to place the \n");
221 printf("\"Multicast DNS & DNS-SD\" Extension in the Extensions Folder and restart\n");
222 return(-1);
223 }
224
225 err = InitOpenTransport();
226 if (err) { printf("InitOpenTransport failed %d", err); return(err); }
227
228 // Make sure OT has a large enough memory pool for us to draw from at OTNotifier (interrupt) time
229 tempmem = OTAllocMem(0x10000);
230 if (tempmem) OTFreeMem(tempmem);
231 else printf("**** Warning: OTAllocMem couldn't pre-allocate 64K for us.\n");
232
233 services.serviceinfolist.fHead = NULL;
234 services.headerPrinted = false;
235 services.lostRecords = false;
236
237 printf("Sending mDNS service lookup queries and waiting for responses...\n\n");
238 dse = DNSServiceBrowse(&sdRef, 0, 0, "_http._tcp", "", FoundInstance, &services);
239 if (dse == kDNSServiceErr_NoError)
240 {
241 while (!YieldSomeTime(35))
242 {
243 if (services.serviceinfolist.fHead)
244 PrintServiceInfo(&services);
245
246 if (services.lostRecords)
247 {
248 services.lostRecords = false;
249 printf("**** Warning: Out of memory: Records have been missed.\n");
250 }
251 }
252 }
253
254 DNSServiceRefDeallocate(sdRef);
255 CloseOpenTransport();
256 return(0);
257 }