]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOS9/Responder.c
mDNSResponder-108.tar.gz
[apple/mdnsresponder.git] / mDNSMacOS9 / Responder.c
1 /*
2 * Copyright (c) 2004 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: Responder.c,v $
26 Revision 1.2 2004/05/20 18:38:31 cheshire
27 Fix build broken by removal of 'kDNSServiceFlagsAutoRename' from dns_sd.h
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
40 #include <OpenTransport.h>
41 #include <OpenTptInternet.h>
42
43 #include <SIOUX.h> // For SIOUXHandleOneEvent()
44
45 #include "dns_sd.h"
46
47 typedef union { UInt8 b[2]; UInt16 NotAnInteger; } mDNSOpaque16;
48 static UInt16 mDNSVal16(mDNSOpaque16 x) { return((UInt16)(x.b[0]<<8 | x.b[1])); }
49 static mDNSOpaque16 mDNSOpaque16fromIntVal(UInt16 v)
50 { mDNSOpaque16 x; x.b[0] = (UInt8)(v >> 8); x.b[1] = (UInt8)(v & 0xFF); return(x); }
51
52 typedef struct RegisteredService_struct RegisteredService;
53 struct RegisteredService_struct
54 {
55 RegisteredService *next;
56 DNSServiceRef sdRef;
57 Boolean gotresult;
58 DNSServiceErrorType errorCode;
59 char namestr[64];
60 char typestr[kDNSServiceMaxDomainName];
61 char domstr [kDNSServiceMaxDomainName];
62 };
63
64 static RegisteredService p1, p2, afp, http, njp;
65 static RegisteredService *services = NULL, **nextservice = &services;
66
67 static void RegCallback(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode,
68 const char *name, const char *regtype, const char *domain, void *context)
69 {
70 RegisteredService *rs = (RegisteredService *)context;
71 (void)sdRef; // Unused
72 (void)flags; // Unused
73 rs->gotresult = true;
74 rs->errorCode = errorCode;
75 strcpy(rs->namestr, name);
76 strcpy(rs->typestr, regtype);
77 strcpy(rs->domstr, domain);
78 }
79
80 static DNSServiceErrorType RegisterService(RegisteredService *rs, mDNSOpaque16 OpaquePort,
81 const char name[], const char type[], const char domain[], const char txtinfo[])
82 {
83 DNSServiceErrorType err;
84 unsigned char txtbuffer[257];
85 strncpy((char*)txtbuffer+1, txtinfo, 255);
86 txtbuffer[256] = 0;
87 txtbuffer[0] = (unsigned char)strlen((char*)txtbuffer);
88 rs->gotresult = 0;
89 rs->errorCode = kDNSServiceErr_NoError;
90 err = DNSServiceRegister(&rs->sdRef, /* kDNSServiceFlagsAutoRename*/ 0, 0,
91 name, type, domain, NULL, OpaquePort.NotAnInteger, (unsigned short)(1+txtbuffer[0]), txtbuffer, RegCallback, rs);
92 if (err)
93 printf("RegisterService(%s %s %s) failed %d\n", name, type, domain, err);
94 else
95 { *nextservice = rs; nextservice = &rs->next; }
96 return(err);
97 }
98
99 // RegisterFakeServiceForTesting() simulates the effect of services being registered on
100 // dynamically-allocated port numbers. No real service exists on that port -- this is just for testing.
101 static DNSServiceErrorType RegisterFakeServiceForTesting(RegisteredService *rs,
102 const char name[], const char type[], const char domain[], const char txtinfo[])
103 {
104 static UInt16 NextPort = 0xF000;
105 return RegisterService(rs, mDNSOpaque16fromIntVal(NextPort++), name, type, domain, txtinfo);
106 }
107
108 // CreateProxyRegistrationForRealService() checks to see if the given port is currently
109 // in use, and if so, advertises the specified service as present on that port.
110 // This is useful for advertising existing real services (Personal Web Sharing, Personal
111 // File Sharing, etc.) that currently don't register with mDNS Service Discovery themselves.
112 static DNSServiceErrorType CreateProxyRegistrationForRealService(RegisteredService *rs,
113 const char *servicetype, UInt16 PortAsNumber, const char txtinfo[])
114 {
115 mDNSOpaque16 OpaquePort = mDNSOpaque16fromIntVal(PortAsNumber);
116 InetAddress ia;
117 TBind bindReq;
118 OSStatus err;
119 TEndpointInfo endpointinfo;
120 EndpointRef ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &endpointinfo, &err);
121 if (!ep || err) { printf("OTOpenEndpoint (CreateProxyRegistrationForRealService) failed %d", err); return(err); }
122
123 ia.fAddressType = AF_INET;
124 ia.fPort = OpaquePort.NotAnInteger;
125 ia.fHost = 0;
126 bindReq.addr.maxlen = sizeof(ia);
127 bindReq.addr.len = sizeof(ia);
128 bindReq.addr.buf = (UInt8*)&ia;
129 bindReq.qlen = 0;
130 err = OTBind(ep, &bindReq, NULL);
131
132 if (err == kOTBadAddressErr)
133 err = RegisterService(rs, OpaquePort, "", servicetype, "local.", txtinfo);
134 else if (err)
135 printf("OTBind failed %d", err);
136
137 OTCloseProvider(ep);
138 return(err);
139 }
140
141 // YieldSomeTime() just cooperatively yields some time to other processes running on classic Mac OS
142 static Boolean YieldSomeTime(UInt32 milliseconds)
143 {
144 extern Boolean SIOUXQuitting;
145 EventRecord e;
146 WaitNextEvent(everyEvent, &e, milliseconds / 17, NULL);
147 SIOUXHandleOneEvent(&e);
148 return(SIOUXQuitting);
149 }
150
151 int main()
152 {
153 OSStatus err;
154 RegisteredService *s;
155
156 SIOUXSettings.asktosaveonclose = false;
157 SIOUXSettings.userwindowtitle = "\pMulticast DNS Responder";
158
159 printf("Multicast DNS Responder\n\n");
160 printf("This software reports errors using MacsBug breaks,\n");
161 printf("so if you don't have MacsBug installed your Mac may crash.\n\n");
162 printf("******************************************************************************\n\n");
163
164 err = InitOpenTransport();
165 if (err) { printf("InitOpenTransport failed %d", err); return(err); }
166
167 printf("Advertising Services...\n");
168
169 #define SRSET 0
170 #if SRSET==0
171 RegisterFakeServiceForTesting(&p1, "Web Server One", "_http._tcp.", "local.", "path=/index.html");
172 RegisterFakeServiceForTesting(&p2, "Web Server Two", "_http._tcp.", "local.", "path=/path.html");
173 #elif SRSET==1
174 RegisterFakeServiceForTesting(&p1, "Epson Stylus 900N", "_printer._tcp.", "local.", "rn=lpq1");
175 RegisterFakeServiceForTesting(&p2, "HP LaserJet", "_printer._tcp.", "local.", "rn=lpq2");
176 #else
177 RegisterFakeServiceForTesting(&p1, "My Printer", "_printer._tcp.", "local.", "rn=lpq3");
178 RegisterFakeServiceForTesting(&p2, "My Other Printer", "_printer._tcp.", "local.", "lrn=pq4");
179 #endif
180
181 // If AFP Server is running, register a record for it
182 CreateProxyRegistrationForRealService(&afp, "_afpovertcp._tcp.", 548, "");
183
184 // If Web Server is running, register a record for it
185 CreateProxyRegistrationForRealService(&http, "_http._tcp.", 80, "path=/index.html");
186
187 while (!YieldSomeTime(35))
188 for (s = services; s; s = s->next)
189 if (s->gotresult)
190 {
191 printf("%s %s %s registered\n", s->namestr, s->typestr, s->domstr);
192 s->gotresult = false;
193 }
194
195 for (s = services; s; s = s->next)
196 if (s->sdRef) DNSServiceRefDeallocate(s->sdRef);
197
198 CloseOpenTransport();
199 return(0);
200 }