1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
20 Revision 1.3 2006/08/14 23:24:29 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.2 2004/05/20 18:38:31 cheshire
24 Fix build broken by removal of 'kDNSServiceFlagsAutoRename' from dns_sd.h
26 Revision 1.1 2004/03/12 21:30:25 cheshire
27 Build a System-Context Shared Library from mDNSCore, for the benefit of developers
28 like Muse Research who want to be able to use mDNS/DNS-SD from GPL-licensed code.
32 #include <stdio.h> // For printf()
33 #include <string.h> // For strcpy()
35 #include <Events.h> // For WaitNextEvent()
37 #include <OpenTransport.h>
38 #include <OpenTptInternet.h>
40 #include <SIOUX.h> // For SIOUXHandleOneEvent()
44 typedef union { UInt8 b
[2]; UInt16 NotAnInteger
; } mDNSOpaque16
;
45 static UInt16
mDNSVal16(mDNSOpaque16 x
) { return((UInt16
)(x
.b
[0]<<8 | x
.b
[1])); }
46 static mDNSOpaque16
mDNSOpaque16fromIntVal(UInt16 v
)
47 { mDNSOpaque16 x
; x
.b
[0] = (UInt8
)(v
>> 8); x
.b
[1] = (UInt8
)(v
& 0xFF); return(x
); }
49 typedef struct RegisteredService_struct RegisteredService
;
50 struct RegisteredService_struct
52 RegisteredService
*next
;
55 DNSServiceErrorType errorCode
;
57 char typestr
[kDNSServiceMaxDomainName
];
58 char domstr
[kDNSServiceMaxDomainName
];
61 static RegisteredService p1
, p2
, afp
, http
, njp
;
62 static RegisteredService
*services
= NULL
, **nextservice
= &services
;
64 static void RegCallback(DNSServiceRef sdRef
, DNSServiceFlags flags
, DNSServiceErrorType errorCode
,
65 const char *name
, const char *regtype
, const char *domain
, void *context
)
67 RegisteredService
*rs
= (RegisteredService
*)context
;
68 (void)sdRef
; // Unused
69 (void)flags
; // Unused
71 rs
->errorCode
= errorCode
;
72 strcpy(rs
->namestr
, name
);
73 strcpy(rs
->typestr
, regtype
);
74 strcpy(rs
->domstr
, domain
);
77 static DNSServiceErrorType
RegisterService(RegisteredService
*rs
, mDNSOpaque16 OpaquePort
,
78 const char name
[], const char type
[], const char domain
[], const char txtinfo
[])
80 DNSServiceErrorType err
;
81 unsigned char txtbuffer
[257];
82 strncpy((char*)txtbuffer
+1, txtinfo
, 255);
84 txtbuffer
[0] = (unsigned char)strlen((char*)txtbuffer
);
86 rs
->errorCode
= kDNSServiceErr_NoError
;
87 err
= DNSServiceRegister(&rs
->sdRef
, /* kDNSServiceFlagsAutoRename*/ 0, 0,
88 name
, type
, domain
, NULL
, OpaquePort
.NotAnInteger
, (unsigned short)(1+txtbuffer
[0]), txtbuffer
, RegCallback
, rs
);
90 printf("RegisterService(%s %s %s) failed %d\n", name
, type
, domain
, err
);
92 { *nextservice
= rs
; nextservice
= &rs
->next
; }
96 // RegisterFakeServiceForTesting() simulates the effect of services being registered on
97 // dynamically-allocated port numbers. No real service exists on that port -- this is just for testing.
98 static DNSServiceErrorType
RegisterFakeServiceForTesting(RegisteredService
*rs
,
99 const char name
[], const char type
[], const char domain
[], const char txtinfo
[])
101 static UInt16 NextPort
= 0xF000;
102 return RegisterService(rs
, mDNSOpaque16fromIntVal(NextPort
++), name
, type
, domain
, txtinfo
);
105 // CreateProxyRegistrationForRealService() checks to see if the given port is currently
106 // in use, and if so, advertises the specified service as present on that port.
107 // This is useful for advertising existing real services (Personal Web Sharing, Personal
108 // File Sharing, etc.) that currently don't register with mDNS Service Discovery themselves.
109 static DNSServiceErrorType
CreateProxyRegistrationForRealService(RegisteredService
*rs
,
110 const char *servicetype
, UInt16 PortAsNumber
, const char txtinfo
[])
112 mDNSOpaque16 OpaquePort
= mDNSOpaque16fromIntVal(PortAsNumber
);
116 TEndpointInfo endpointinfo
;
117 EndpointRef ep
= OTOpenEndpoint(OTCreateConfiguration(kTCPName
), 0, &endpointinfo
, &err
);
118 if (!ep
|| err
) { printf("OTOpenEndpoint (CreateProxyRegistrationForRealService) failed %d", err
); return(err
); }
120 ia
.fAddressType
= AF_INET
;
121 ia
.fPort
= OpaquePort
.NotAnInteger
;
123 bindReq
.addr
.maxlen
= sizeof(ia
);
124 bindReq
.addr
.len
= sizeof(ia
);
125 bindReq
.addr
.buf
= (UInt8
*)&ia
;
127 err
= OTBind(ep
, &bindReq
, NULL
);
129 if (err
== kOTBadAddressErr
)
130 err
= RegisterService(rs
, OpaquePort
, "", servicetype
, "local.", txtinfo
);
132 printf("OTBind failed %d", err
);
138 // YieldSomeTime() just cooperatively yields some time to other processes running on classic Mac OS
139 static Boolean
YieldSomeTime(UInt32 milliseconds
)
141 extern Boolean SIOUXQuitting
;
143 WaitNextEvent(everyEvent
, &e
, milliseconds
/ 17, NULL
);
144 SIOUXHandleOneEvent(&e
);
145 return(SIOUXQuitting
);
151 RegisteredService
*s
;
153 SIOUXSettings
.asktosaveonclose
= false;
154 SIOUXSettings
.userwindowtitle
= "\pMulticast DNS Responder";
156 printf("Multicast DNS Responder\n\n");
157 printf("This software reports errors using MacsBug breaks,\n");
158 printf("so if you don't have MacsBug installed your Mac may crash.\n\n");
159 printf("******************************************************************************\n\n");
161 err
= InitOpenTransport();
162 if (err
) { printf("InitOpenTransport failed %d", err
); return(err
); }
164 printf("Advertising Services...\n");
168 RegisterFakeServiceForTesting(&p1
, "Web Server One", "_http._tcp.", "local.", "path=/index.html");
169 RegisterFakeServiceForTesting(&p2
, "Web Server Two", "_http._tcp.", "local.", "path=/path.html");
171 RegisterFakeServiceForTesting(&p1
, "Epson Stylus 900N", "_printer._tcp.", "local.", "rn=lpq1");
172 RegisterFakeServiceForTesting(&p2
, "HP LaserJet", "_printer._tcp.", "local.", "rn=lpq2");
174 RegisterFakeServiceForTesting(&p1
, "My Printer", "_printer._tcp.", "local.", "rn=lpq3");
175 RegisterFakeServiceForTesting(&p2
, "My Other Printer", "_printer._tcp.", "local.", "lrn=pq4");
178 // If AFP Server is running, register a record for it
179 CreateProxyRegistrationForRealService(&afp
, "_afpovertcp._tcp.", 548, "");
181 // If Web Server is running, register a record for it
182 CreateProxyRegistrationForRealService(&http
, "_http._tcp.", 80, "path=/index.html");
184 while (!YieldSomeTime(35))
185 for (s
= services
; s
; s
= s
->next
)
188 printf("%s %s %s registered\n", s
->namestr
, s
->typestr
, s
->domstr
);
189 s
->gotresult
= false;
192 for (s
= services
; s
; s
= s
->next
)
193 if (s
->sdRef
) DNSServiceRefDeallocate(s
->sdRef
);
195 CloseOpenTransport();