2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
26 Revision 1.2 2004/05/20 18:38:31 cheshire
27 Fix build broken by removal of 'kDNSServiceFlagsAutoRename' from dns_sd.h
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.
35 #include <stdio.h> // For printf()
36 #include <string.h> // For strcpy()
38 #include <Events.h> // For WaitNextEvent()
40 #include <OpenTransport.h>
41 #include <OpenTptInternet.h>
43 #include <SIOUX.h> // For SIOUXHandleOneEvent()
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
); }
52 typedef struct RegisteredService_struct RegisteredService
;
53 struct RegisteredService_struct
55 RegisteredService
*next
;
58 DNSServiceErrorType errorCode
;
60 char typestr
[kDNSServiceMaxDomainName
];
61 char domstr
[kDNSServiceMaxDomainName
];
64 static RegisteredService p1
, p2
, afp
, http
, njp
;
65 static RegisteredService
*services
= NULL
, **nextservice
= &services
;
67 static void RegCallback(DNSServiceRef sdRef
, DNSServiceFlags flags
, DNSServiceErrorType errorCode
,
68 const char *name
, const char *regtype
, const char *domain
, void *context
)
70 RegisteredService
*rs
= (RegisteredService
*)context
;
71 (void)sdRef
; // Unused
72 (void)flags
; // Unused
74 rs
->errorCode
= errorCode
;
75 strcpy(rs
->namestr
, name
);
76 strcpy(rs
->typestr
, regtype
);
77 strcpy(rs
->domstr
, domain
);
80 static DNSServiceErrorType
RegisterService(RegisteredService
*rs
, mDNSOpaque16 OpaquePort
,
81 const char name
[], const char type
[], const char domain
[], const char txtinfo
[])
83 DNSServiceErrorType err
;
84 unsigned char txtbuffer
[257];
85 strncpy((char*)txtbuffer
+1, txtinfo
, 255);
87 txtbuffer
[0] = (unsigned char)strlen((char*)txtbuffer
);
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
);
93 printf("RegisterService(%s %s %s) failed %d\n", name
, type
, domain
, err
);
95 { *nextservice
= rs
; nextservice
= &rs
->next
; }
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
[])
104 static UInt16 NextPort
= 0xF000;
105 return RegisterService(rs
, mDNSOpaque16fromIntVal(NextPort
++), name
, type
, domain
, txtinfo
);
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
[])
115 mDNSOpaque16 OpaquePort
= mDNSOpaque16fromIntVal(PortAsNumber
);
119 TEndpointInfo endpointinfo
;
120 EndpointRef ep
= OTOpenEndpoint(OTCreateConfiguration(kTCPName
), 0, &endpointinfo
, &err
);
121 if (!ep
|| err
) { printf("OTOpenEndpoint (CreateProxyRegistrationForRealService) failed %d", err
); return(err
); }
123 ia
.fAddressType
= AF_INET
;
124 ia
.fPort
= OpaquePort
.NotAnInteger
;
126 bindReq
.addr
.maxlen
= sizeof(ia
);
127 bindReq
.addr
.len
= sizeof(ia
);
128 bindReq
.addr
.buf
= (UInt8
*)&ia
;
130 err
= OTBind(ep
, &bindReq
, NULL
);
132 if (err
== kOTBadAddressErr
)
133 err
= RegisterService(rs
, OpaquePort
, "", servicetype
, "local.", txtinfo
);
135 printf("OTBind failed %d", err
);
141 // YieldSomeTime() just cooperatively yields some time to other processes running on classic Mac OS
142 static Boolean
YieldSomeTime(UInt32 milliseconds
)
144 extern Boolean SIOUXQuitting
;
146 WaitNextEvent(everyEvent
, &e
, milliseconds
/ 17, NULL
);
147 SIOUXHandleOneEvent(&e
);
148 return(SIOUXQuitting
);
154 RegisteredService
*s
;
156 SIOUXSettings
.asktosaveonclose
= false;
157 SIOUXSettings
.userwindowtitle
= "\pMulticast DNS Responder";
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");
164 err
= InitOpenTransport();
165 if (err
) { printf("InitOpenTransport failed %d", err
); return(err
); }
167 printf("Advertising Services...\n");
171 RegisterFakeServiceForTesting(&p1
, "Web Server One", "_http._tcp.", "local.", "path=/index.html");
172 RegisterFakeServiceForTesting(&p2
, "Web Server Two", "_http._tcp.", "local.", "path=/path.html");
174 RegisterFakeServiceForTesting(&p1
, "Epson Stylus 900N", "_printer._tcp.", "local.", "rn=lpq1");
175 RegisterFakeServiceForTesting(&p2
, "HP LaserJet", "_printer._tcp.", "local.", "rn=lpq2");
177 RegisterFakeServiceForTesting(&p1
, "My Printer", "_printer._tcp.", "local.", "rn=lpq3");
178 RegisterFakeServiceForTesting(&p2
, "My Other Printer", "_printer._tcp.", "local.", "lrn=pq4");
181 // If AFP Server is running, register a record for it
182 CreateProxyRegistrationForRealService(&afp
, "_afpovertcp._tcp.", 548, "");
184 // If Web Server is running, register a record for it
185 CreateProxyRegistrationForRealService(&http
, "_http._tcp.", 80, "path=/index.html");
187 while (!YieldSomeTime(35))
188 for (s
= services
; s
; s
= s
->next
)
191 printf("%s %s %s registered\n", s
->namestr
, s
->typestr
, s
->domstr
);
192 s
->gotresult
= false;
195 for (s
= services
; s
; s
= s
->next
)
196 if (s
->sdRef
) DNSServiceRefDeallocate(s
->sdRef
);
198 CloseOpenTransport();