]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/mDNSPosix.c
mDNSResponder-164.tar.gz
[apple/mdnsresponder.git] / mDNSPosix / mDNSPosix.c
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 * Formatting notes:
18 * This code follows the "Whitesmiths style" C indentation rules. Plenty of discussion
19 * on C indentation can be found on the web, such as <http://www.kafejo.com/komp/1tbs.htm>,
20 * but for the sake of brevity here I will say just this: Curly braces are not syntactially
21 * part of an "if" statement; they are the beginning and ending markers of a compound statement;
22 * therefore common sense dictates that if they are part of a compound statement then they
23 * should be indented to the same level as everything else in that compound statement.
24 * Indenting curly braces at the same level as the "if" implies that curly braces are
25 * part of the "if", which is false. (This is as misleading as people who write "char* x,y;"
26 * thinking that variables x and y are both of type "char*" -- and anyone who doesn't
27 * understand why variable y is not of type "char*" just proves the point that poor code
28 * layout leads people to unfortunate misunderstandings about how the C language really works.)
29
30 Change History (most recent first):
31
32 $Log: mDNSPosix.c,v $
33 Revision 1.103 2007/10/02 19:31:17 cheshire
34 In ParseDNSServers, should use strncasecmp for case-insensitive compare
35
36 Revision 1.102 2007/09/12 19:23:17 cheshire
37 Get rid of unnecessary mDNSPlatformTCPIsConnected() routine
38
39 Revision 1.101 2007/07/20 00:54:23 cheshire
40 <rdar://problem/4641118> Need separate SCPreferences for per-user .Mac settings
41
42 Revision 1.100 2007/07/19 21:45:30 cheshire
43 Fixed code spacing
44
45 Revision 1.99 2007/07/11 02:56:51 cheshire
46 <rdar://problem/5303807> Register IPv6-only hostname and don't create port mappings for AutoTunnel services
47 Remove unused mDNSPlatformDefaultRegDomainChanged
48
49 Revision 1.98 2007/06/20 01:10:13 cheshire
50 <rdar://problem/5280520> Sync iPhone changes into main mDNSResponder code
51
52 Revision 1.97 2007/04/26 00:35:16 cheshire
53 <rdar://problem/5140339> uDNS: Domain discovery not working over VPN
54 Fixes to make sure results update correctly when connectivity changes (e.g. a DNS server
55 inside the firewall may give answers where a public one gives none, and vice versa.)
56
57 Revision 1.96 2007/04/22 20:29:59 cheshire
58 Fix locking error
59
60 Revision 1.95 2007/04/22 20:15:46 cheshire
61 Add missing parameters for mDNSPosixEventCallback
62
63 Revision 1.94 2007/04/17 19:21:29 cheshire
64 <rdar://problem/5140339> Domain discovery not working over VPN
65
66 Revision 1.93 2007/04/16 20:49:40 cheshire
67 Fix compile errors for mDNSPosix build
68
69 Revision 1.92 2007/04/05 20:40:37 cheshire
70 Remove unused mDNSPlatformTCPGetFlags()
71
72 Revision 1.91 2007/03/22 18:31:48 cheshire
73 Put dst parameter first in mDNSPlatformStrCopy/mDNSPlatformMemCopy, like conventional Posix strcpy/memcpy
74
75 Revision 1.90 2007/03/21 00:31:45 cheshire
76 Remove unnecessary (and unimplemented) platform functions
77
78 Revision 1.89 2007/03/20 17:07:15 cheshire
79 Rename "struct uDNS_TCPSocket_struct" to "TCPSocket", "struct uDNS_UDPSocket_struct" to "UDPSocket"
80
81 Revision 1.88 2007/03/07 00:30:18 mkrochma
82 <rdar://problem/5034370> POSIX: kDNSServiceInterfaceIndexAny not correctly handled
83 Thanks goes to Aidan Williams of Audinate who did a lot of work in diagnosing this
84
85 Revision 1.87 2007/02/08 21:12:28 cheshire
86 <rdar://problem/4386497> Stop reading /etc/mDNSResponder.conf on every sleep/wake
87
88 Revision 1.86 2007/01/05 08:30:52 cheshire
89 Trim excessive "$Log" checkin history from before 2006
90 (checkin history still available via "cvs log ..." of course)
91
92 Revision 1.85 2007/01/04 23:12:20 cheshire
93 Remove unused mDNSPlatformDefaultBrowseDomainChanged
94
95 Revision 1.84 2006/12/22 21:07:35 cheshire
96 <rdar://problem/4742742> Read *all* DNS keys from keychain,
97 not just key for the system-wide default registration domain
98
99 Revision 1.83 2006/12/21 00:09:46 cheshire
100 Use mDNSPlatformMemZero instead of bzero
101
102 Revision 1.82 2006/12/19 22:43:55 cheshire
103 Fix compiler warnings
104
105 Revision 1.81 2006/08/14 23:24:46 cheshire
106 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
107
108 Revision 1.80 2006/07/22 03:05:33 cheshire
109 Improve error reporting for socket creation failures
110
111 Revision 1.79 2006/07/06 00:02:16 cheshire
112 <rdar://problem/4472014> Add Private DNS client functionality to mDNSResponder
113
114 Revision 1.78 2006/06/28 09:12:22 cheshire
115 Added debugging message
116
117 Revision 1.77 2006/03/19 02:00:11 cheshire
118 <rdar://problem/4073825> Improve logic for delaying packets after repeated interface transitions
119
120 Revision 1.76 2006/01/09 19:29:16 cheshire
121 <rdar://problem/4403128> Cap number of "sendto failed" messages we allow mDNSResponder to log
122
123 Revision 1.75 2006/01/05 22:04:57 cheshire
124 <rdar://problem/4399479> Log error message when send fails with "operation not permitted"
125
126 Revision 1.74 2006/01/05 21:45:27 cheshire
127 <rdar://problem/4400118> Fix uninitialized structure member in IPv6 code
128
129 */
130
131 #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above
132 #include "DNSCommon.h"
133 #include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform
134 #include "dns_sd.h"
135
136 #include <assert.h>
137 #include <stdio.h>
138 #include <stdlib.h>
139 #include <errno.h>
140 #include <string.h>
141 #include <unistd.h>
142 #include <syslog.h>
143 #include <stdarg.h>
144 #include <fcntl.h>
145 #include <sys/types.h>
146 #include <sys/time.h>
147 #include <sys/socket.h>
148 #include <sys/uio.h>
149 #include <sys/select.h>
150 #include <netinet/in.h>
151 #include <arpa/inet.h>
152 #include <time.h> // platform support for UTC time
153
154 #if USES_NETLINK
155 #include <asm/types.h>
156 #include <linux/netlink.h>
157 #include <linux/rtnetlink.h>
158 #else // USES_NETLINK
159 #include <net/route.h>
160 #include <net/if.h>
161 #endif // USES_NETLINK
162
163 #include "mDNSUNP.h"
164 #include "GenLinkedList.h"
165
166 // ***************************************************************************
167 // Structures
168
169 // We keep a list of client-supplied event sources in PosixEventSource records
170 struct PosixEventSource
171 {
172 mDNSPosixEventCallback Callback;
173 void *Context;
174 int fd;
175 struct PosixEventSource *Next;
176 };
177 typedef struct PosixEventSource PosixEventSource;
178
179 // Context record for interface change callback
180 struct IfChangeRec
181 {
182 int NotifySD;
183 mDNS *mDNS;
184 };
185 typedef struct IfChangeRec IfChangeRec;
186
187 // Note that static data is initialized to zero in (modern) C.
188 static fd_set gEventFDs;
189 static int gMaxFD; // largest fd in gEventFDs
190 static GenLinkedList gEventSources; // linked list of PosixEventSource's
191 static sigset_t gEventSignalSet; // Signals which event loop listens for
192 static sigset_t gEventSignals; // Signals which were received while inside loop
193
194 // ***************************************************************************
195 // Globals (for debugging)
196
197 static int num_registered_interfaces = 0;
198 static int num_pkts_accepted = 0;
199 static int num_pkts_rejected = 0;
200
201 // ***************************************************************************
202 // Functions
203
204 int gMDNSPlatformPosixVerboseLevel = 0;
205
206 #define PosixErrorToStatus(errNum) ((errNum) == 0 ? mStatus_NoError : mStatus_UnknownErr)
207
208 mDNSlocal void SockAddrTomDNSAddr(const struct sockaddr *const sa, mDNSAddr *ipAddr, mDNSIPPort *ipPort)
209 {
210 switch (sa->sa_family)
211 {
212 case AF_INET:
213 {
214 struct sockaddr_in *sin = (struct sockaddr_in*)sa;
215 ipAddr->type = mDNSAddrType_IPv4;
216 ipAddr->ip.v4.NotAnInteger = sin->sin_addr.s_addr;
217 if (ipPort) ipPort->NotAnInteger = sin->sin_port;
218 break;
219 }
220
221 #if HAVE_IPV6
222 case AF_INET6:
223 {
224 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
225 #ifndef NOT_HAVE_SA_LEN
226 assert(sin6->sin6_len == sizeof(*sin6));
227 #endif
228 ipAddr->type = mDNSAddrType_IPv6;
229 ipAddr->ip.v6 = *(mDNSv6Addr*)&sin6->sin6_addr;
230 if (ipPort) ipPort->NotAnInteger = sin6->sin6_port;
231 break;
232 }
233 #endif
234
235 default:
236 verbosedebugf("SockAddrTomDNSAddr: Uknown address family %d\n", sa->sa_family);
237 ipAddr->type = mDNSAddrType_None;
238 if (ipPort) ipPort->NotAnInteger = 0;
239 break;
240 }
241 }
242
243 #if COMPILER_LIKES_PRAGMA_MARK
244 #pragma mark ***** Send and Receive
245 #endif
246
247 // mDNS core calls this routine when it needs to send a packet.
248 mDNSexport mStatus mDNSPlatformSendUDP(const mDNS *const m, const void *const msg, const mDNSu8 *const end,
249 mDNSInterfaceID InterfaceID, const mDNSAddr *dst, mDNSIPPort dstPort)
250 {
251 int err = 0;
252 struct sockaddr_storage to;
253 PosixNetworkInterface * thisIntf = (PosixNetworkInterface *)(InterfaceID);
254 int sendingsocket = -1;
255
256 assert(m != NULL);
257 assert(msg != NULL);
258 assert(end != NULL);
259 assert((((char *) end) - ((char *) msg)) > 0);
260 assert(dstPort.NotAnInteger != 0);
261
262 if (dst->type == mDNSAddrType_IPv4)
263 {
264 struct sockaddr_in *sin = (struct sockaddr_in*)&to;
265 #ifndef NOT_HAVE_SA_LEN
266 sin->sin_len = sizeof(*sin);
267 #endif
268 sin->sin_family = AF_INET;
269 sin->sin_port = dstPort.NotAnInteger;
270 sin->sin_addr.s_addr = dst->ip.v4.NotAnInteger;
271 sendingsocket = thisIntf ? thisIntf->multicastSocket4 : m->p->unicastSocket4;
272 }
273
274 #if HAVE_IPV6
275 else if (dst->type == mDNSAddrType_IPv6)
276 {
277 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&to;
278 mDNSPlatformMemZero(sin6, sizeof(*sin6));
279 #ifndef NOT_HAVE_SA_LEN
280 sin6->sin6_len = sizeof(*sin6);
281 #endif
282 sin6->sin6_family = AF_INET6;
283 sin6->sin6_port = dstPort.NotAnInteger;
284 sin6->sin6_addr = *(struct in6_addr*)&dst->ip.v6;
285 sendingsocket = thisIntf ? thisIntf->multicastSocket6 : m->p->unicastSocket6;
286 }
287 #endif
288
289 if (sendingsocket >= 0)
290 err = sendto(sendingsocket, msg, (char*)end - (char*)msg, 0, (struct sockaddr *)&to, GET_SA_LEN(to));
291
292 if (err > 0) err = 0;
293 else if (err < 0)
294 {
295 static int MessageCount = 0;
296 // Don't report EHOSTDOWN (i.e. ARP failure), ENETDOWN, or no route to host for unicast destinations
297 if (!mDNSAddressIsAllDNSLinkGroup(dst))
298 if (errno == EHOSTDOWN || errno == ENETDOWN || errno == EHOSTUNREACH || errno == ENETUNREACH) return(mStatus_TransientErr);
299
300 if (MessageCount < 1000)
301 {
302 MessageCount++;
303 if (thisIntf)
304 LogMsg("mDNSPlatformSendUDP got error %d (%s) sending packet to %#a on interface %#a/%s/%d",
305 errno, strerror(errno), dst, &thisIntf->coreIntf.ip, thisIntf->intfName, thisIntf->index);
306 else
307 LogMsg("mDNSPlatformSendUDP got error %d (%s) sending packet to %#a", errno, strerror(errno), dst);
308 }
309 }
310
311 return PosixErrorToStatus(err);
312 }
313
314 // This routine is called when the main loop detects that data is available on a socket.
315 mDNSlocal void SocketDataReady(mDNS *const m, PosixNetworkInterface *intf, int skt)
316 {
317 mDNSAddr senderAddr, destAddr;
318 mDNSIPPort senderPort;
319 ssize_t packetLen;
320 DNSMessage packet;
321 struct my_in_pktinfo packetInfo;
322 struct sockaddr_storage from;
323 socklen_t fromLen;
324 int flags;
325 mDNSu8 ttl;
326 mDNSBool reject;
327 const mDNSInterfaceID InterfaceID = intf ? intf->coreIntf.InterfaceID : NULL;
328
329 assert(m != NULL);
330 assert(skt >= 0);
331
332 fromLen = sizeof(from);
333 flags = 0;
334 packetLen = recvfrom_flags(skt, &packet, sizeof(packet), &flags, (struct sockaddr *) &from, &fromLen, &packetInfo, &ttl);
335
336 if (packetLen >= 0)
337 {
338 SockAddrTomDNSAddr((struct sockaddr*)&from, &senderAddr, &senderPort);
339 SockAddrTomDNSAddr((struct sockaddr*)&packetInfo.ipi_addr, &destAddr, NULL);
340
341 // If we have broken IP_RECVDSTADDR functionality (so far
342 // I've only seen this on OpenBSD) then apply a hack to
343 // convince mDNS Core that this isn't a spoof packet.
344 // Basically what we do is check to see whether the
345 // packet arrived as a multicast and, if so, set its
346 // destAddr to the mDNS address.
347 //
348 // I must admit that I could just be doing something
349 // wrong on OpenBSD and hence triggering this problem
350 // but I'm at a loss as to how.
351 //
352 // If this platform doesn't have IP_PKTINFO or IP_RECVDSTADDR, then we have
353 // no way to tell the destination address or interface this packet arrived on,
354 // so all we can do is just assume it's a multicast
355
356 #if HAVE_BROKEN_RECVDSTADDR || (!defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR))
357 if ((destAddr.NotAnInteger == 0) && (flags & MSG_MCAST))
358 {
359 destAddr.type = senderAddr.type;
360 if (senderAddr.type == mDNSAddrType_IPv4) destAddr.ip.v4 = AllDNSLinkGroup_v4.ip.v4;
361 else if (senderAddr.type == mDNSAddrType_IPv6) destAddr.ip.v6 = AllDNSLinkGroup_v6.ip.v6;
362 }
363 #endif
364
365 // We only accept the packet if the interface on which it came
366 // in matches the interface associated with this socket.
367 // We do this match by name or by index, depending on which
368 // information is available. recvfrom_flags sets the name
369 // to "" if the name isn't available, or the index to -1
370 // if the index is available. This accomodates the various
371 // different capabilities of our target platforms.
372
373 reject = mDNSfalse;
374 if (!intf)
375 {
376 // Ignore multicasts accidentally delivered to our unicast receiving socket
377 if (mDNSAddrIsDNSMulticast(&destAddr)) packetLen = -1;
378 }
379 else
380 {
381 if (packetInfo.ipi_ifname[0] != 0) reject = (strcmp(packetInfo.ipi_ifname, intf->intfName) != 0);
382 else if (packetInfo.ipi_ifindex != -1) reject = (packetInfo.ipi_ifindex != intf->index);
383
384 if (reject)
385 {
386 verbosedebugf("SocketDataReady ignored a packet from %#a to %#a on interface %s/%d expecting %#a/%s/%d/%d",
387 &senderAddr, &destAddr, packetInfo.ipi_ifname, packetInfo.ipi_ifindex,
388 &intf->coreIntf.ip, intf->intfName, intf->index, skt);
389 packetLen = -1;
390 num_pkts_rejected++;
391 if (num_pkts_rejected > (num_pkts_accepted + 1) * (num_registered_interfaces + 1) * 2)
392 {
393 fprintf(stderr,
394 "*** WARNING: Received %d packets; Accepted %d packets; Rejected %d packets because of interface mismatch\n",
395 num_pkts_accepted + num_pkts_rejected, num_pkts_accepted, num_pkts_rejected);
396 num_pkts_accepted = 0;
397 num_pkts_rejected = 0;
398 }
399 }
400 else
401 {
402 verbosedebugf("SocketDataReady got a packet from %#a to %#a on interface %#a/%s/%d/%d",
403 &senderAddr, &destAddr, &intf->coreIntf.ip, intf->intfName, intf->index, skt);
404 num_pkts_accepted++;
405 }
406 }
407 }
408
409 if (packetLen >= 0)
410 mDNSCoreReceive(m, &packet, (mDNSu8 *)&packet + packetLen,
411 &senderAddr, senderPort, &destAddr, MulticastDNSPort, InterfaceID);
412 }
413
414 mDNSexport TCPSocket *mDNSPlatformTCPSocket(mDNS * const m, TCPSocketFlags flags, mDNSIPPort * port)
415 {
416 (void)m; // Unused
417 (void)flags; // Unused
418 (void)port; // Unused
419 return NULL;
420 }
421
422 mDNSexport TCPSocket *mDNSPlatformTCPAccept(TCPSocketFlags flags, int sd)
423 {
424 (void)flags; // Unused
425 (void)sd; // Unused
426 return NULL;
427 }
428
429 mDNSexport int mDNSPlatformTCPGetFD(TCPSocket *sock)
430 {
431 (void)sock; // Unused
432 return -1;
433 }
434
435 mDNSexport mStatus mDNSPlatformTCPConnect(TCPSocket *sock, const mDNSAddr *dst, mDNSOpaque16 dstport, mDNSInterfaceID InterfaceID,
436 TCPConnectionCallback callback, void *context)
437 {
438 (void)sock; // Unused
439 (void)dst; // Unused
440 (void)dstport; // Unused
441 (void)InterfaceID; // Unused
442 (void)callback; // Unused
443 (void)context; // Unused
444 return(mStatus_UnsupportedErr);
445 }
446
447 mDNSexport void mDNSPlatformTCPCloseConnection(TCPSocket *sock)
448 {
449 (void)sock; // Unused
450 }
451
452 mDNSexport long mDNSPlatformReadTCP(TCPSocket *sock, void *buf, unsigned long buflen, mDNSBool * closed)
453 {
454 (void)sock; // Unused
455 (void)buf; // Unused
456 (void)buflen; // Unused
457 (void)closed; // Unused
458 return 0;
459 }
460
461 mDNSexport long mDNSPlatformWriteTCP(TCPSocket *sock, const char *msg, unsigned long len)
462 {
463 (void)sock; // Unused
464 (void)msg; // Unused
465 (void)len; // Unused
466 return 0;
467 }
468
469 mDNSexport UDPSocket *mDNSPlatformUDPSocket(mDNS * const m, mDNSIPPort port)
470 {
471 (void)m; // Unused
472 (void)port; // Unused
473 return NULL;
474 }
475
476 mDNSexport void mDNSPlatformUDPClose(UDPSocket *sock)
477 {
478 (void)sock; // Unused
479 }
480
481 mDNSexport mStatus mDNSPlatformTLSSetupCerts(void)
482 {
483 return(mStatus_UnsupportedErr);
484 }
485
486 mDNSexport void mDNSPlatformTLSTearDownCerts(void)
487 {
488 }
489
490 #if COMPILER_LIKES_PRAGMA_MARK
491 #pragma mark ***** DDNS Config Platform Functions
492 #endif
493
494 mDNSexport void mDNSPlatformSetDNSConfig(mDNS *const m, mDNSBool setservers, mDNSBool setsearch, domainname *const fqdn, DNameListElem **RegDomains, DNameListElem **BrowseDomains)
495 {
496 (void) m;
497 (void) setservers;
498 (void) fqdn;
499 (void) setsearch;
500 (void) RegDomains;
501 (void) BrowseDomains;
502 }
503
504 mDNSexport mStatus mDNSPlatformGetPrimaryInterface(mDNS * const m, mDNSAddr * v4, mDNSAddr * v6, mDNSAddr * router)
505 {
506 (void) m;
507 (void) v4;
508 (void) v6;
509 (void) router;
510
511 return mStatus_UnsupportedErr;
512 }
513
514 mDNSexport void mDNSPlatformDynDNSHostNameStatusChanged(const domainname *const dname, const mStatus status)
515 {
516 (void) dname;
517 (void) status;
518 }
519
520 #if COMPILER_LIKES_PRAGMA_MARK
521 #pragma mark ***** Init and Term
522 #endif
523
524 // This gets the current hostname, truncating it at the first dot if necessary
525 mDNSlocal void GetUserSpecifiedRFC1034ComputerName(domainlabel *const namelabel)
526 {
527 int len = 0;
528 gethostname((char *)(&namelabel->c[1]), MAX_DOMAIN_LABEL);
529 while (len < MAX_DOMAIN_LABEL && namelabel->c[len+1] && namelabel->c[len+1] != '.') len++;
530 namelabel->c[0] = len;
531 }
532
533 // On OS X this gets the text of the field labelled "Computer Name" in the Sharing Prefs Control Panel
534 // Other platforms can either get the information from the appropriate place,
535 // or they can alternatively just require all registering services to provide an explicit name
536 mDNSlocal void GetUserSpecifiedFriendlyComputerName(domainlabel *const namelabel)
537 {
538 // On Unix we have no better name than the host name, so we just use that.
539 GetUserSpecifiedRFC1034ComputerName(namelabel);
540 }
541
542 mDNSexport int ParseDNSServers(mDNS *m, const char *filePath)
543 {
544 char line[256];
545 char nameserver[16];
546 char keyword[10];
547 int numOfServers = 0;
548 FILE *fp = fopen(filePath, "r");
549 if (fp == NULL) return -1;
550 while (fgets(line,sizeof(line),fp))
551 {
552 struct in_addr ina;
553 line[255]='\0'; // just to be safe
554 if (sscanf(line,"%10s %15s", keyword, nameserver) != 2) continue; // it will skip whitespaces
555 if (strncasecmp(keyword,"nameserver",10)) continue;
556 if (inet_aton(nameserver, (struct in_addr *)&ina) != 0)
557 {
558 mDNSAddr DNSAddr;
559 DNSAddr.type = mDNSAddrType_IPv4;
560 DNSAddr.ip.v4.NotAnInteger = ina.s_addr;
561 mDNS_AddDNSServer(m, NULL, mDNSInterface_Any, &DNSAddr, UnicastDNSPort);
562 numOfServers++;
563 }
564 }
565 return (numOfServers > 0) ? 0 : -1;
566 }
567
568 // Searches the interface list looking for the named interface.
569 // Returns a pointer to if it found, or NULL otherwise.
570 mDNSlocal PosixNetworkInterface *SearchForInterfaceByName(mDNS *const m, const char *intfName)
571 {
572 PosixNetworkInterface *intf;
573
574 assert(m != NULL);
575 assert(intfName != NULL);
576
577 intf = (PosixNetworkInterface*)(m->HostInterfaces);
578 while ((intf != NULL) && (strcmp(intf->intfName, intfName) != 0))
579 intf = (PosixNetworkInterface *)(intf->coreIntf.next);
580
581 return intf;
582 }
583
584 mDNSexport mDNSInterfaceID mDNSPlatformInterfaceIDfromInterfaceIndex(mDNS *const m, mDNSu32 index)
585 {
586 PosixNetworkInterface *intf;
587
588 assert(m != NULL);
589
590 if (index == kDNSServiceInterfaceIndexLocalOnly) return(mDNSInterface_LocalOnly);
591 if (index == kDNSServiceInterfaceIndexAny ) return(mDNSInterface_Any);
592
593 intf = (PosixNetworkInterface*)(m->HostInterfaces);
594 while ((intf != NULL) && (mDNSu32) intf->index != index)
595 intf = (PosixNetworkInterface *)(intf->coreIntf.next);
596
597 return (mDNSInterfaceID) intf;
598 }
599
600 mDNSexport mDNSu32 mDNSPlatformInterfaceIndexfromInterfaceID(mDNS *const m, mDNSInterfaceID id)
601 {
602 PosixNetworkInterface *intf;
603
604 assert(m != NULL);
605
606 if (id == mDNSInterface_LocalOnly) return(kDNSServiceInterfaceIndexLocalOnly);
607 if (id == mDNSInterface_Any ) return(kDNSServiceInterfaceIndexAny);
608
609 intf = (PosixNetworkInterface*)(m->HostInterfaces);
610 while ((intf != NULL) && (mDNSInterfaceID) intf != id)
611 intf = (PosixNetworkInterface *)(intf->coreIntf.next);
612
613 return intf ? intf->index : 0;
614 }
615
616 // Frees the specified PosixNetworkInterface structure. The underlying
617 // interface must have already been deregistered with the mDNS core.
618 mDNSlocal void FreePosixNetworkInterface(PosixNetworkInterface *intf)
619 {
620 assert(intf != NULL);
621 if (intf->intfName != NULL) free((void *)intf->intfName);
622 if (intf->multicastSocket4 != -1) assert(close(intf->multicastSocket4) == 0);
623 #if HAVE_IPV6
624 if (intf->multicastSocket6 != -1) assert(close(intf->multicastSocket6) == 0);
625 #endif
626 free(intf);
627 }
628
629 // Grab the first interface, deregister it, free it, and repeat until done.
630 mDNSlocal void ClearInterfaceList(mDNS *const m)
631 {
632 assert(m != NULL);
633
634 while (m->HostInterfaces)
635 {
636 PosixNetworkInterface *intf = (PosixNetworkInterface*)(m->HostInterfaces);
637 mDNS_DeregisterInterface(m, &intf->coreIntf, mDNSfalse);
638 if (gMDNSPlatformPosixVerboseLevel > 0) fprintf(stderr, "Deregistered interface %s\n", intf->intfName);
639 FreePosixNetworkInterface(intf);
640 }
641 num_registered_interfaces = 0;
642 num_pkts_accepted = 0;
643 num_pkts_rejected = 0;
644 }
645
646 // Sets up a send/receive socket.
647 // If mDNSIPPort port is non-zero, then it's a multicast socket on the specified interface
648 // If mDNSIPPort port is zero, then it's a randomly assigned port number, used for sending unicast queries
649 mDNSlocal int SetupSocket(struct sockaddr *intfAddr, mDNSIPPort port, int interfaceIndex, int *sktPtr)
650 {
651 int err = 0;
652 static const int kOn = 1;
653 static const int kIntTwoFiveFive = 255;
654 static const unsigned char kByteTwoFiveFive = 255;
655 const mDNSBool JoinMulticastGroup = (port.NotAnInteger != 0);
656
657 (void) interfaceIndex; // This parameter unused on plaforms that don't have IPv6
658 assert(intfAddr != NULL);
659 assert(sktPtr != NULL);
660 assert(*sktPtr == -1);
661
662 // Open the socket...
663 if (intfAddr->sa_family == AF_INET) *sktPtr = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
664 #if HAVE_IPV6
665 else if (intfAddr->sa_family == AF_INET6) *sktPtr = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
666 #endif
667 else return EINVAL;
668
669 if (*sktPtr < 0) { err = errno; perror((intfAddr->sa_family == AF_INET) ? "socket AF_INET" : "socket AF_INET6"); }
670
671 // ... with a shared UDP port, if it's for multicast receiving
672 if (err == 0 && port.NotAnInteger)
673 {
674 #if defined(SO_REUSEPORT)
675 err = setsockopt(*sktPtr, SOL_SOCKET, SO_REUSEPORT, &kOn, sizeof(kOn));
676 #elif defined(SO_REUSEADDR)
677 err = setsockopt(*sktPtr, SOL_SOCKET, SO_REUSEADDR, &kOn, sizeof(kOn));
678 #else
679 #error This platform has no way to avoid address busy errors on multicast.
680 #endif
681 if (err < 0) { err = errno; perror("setsockopt - SO_REUSExxxx"); }
682 }
683
684 // We want to receive destination addresses and interface identifiers.
685 if (intfAddr->sa_family == AF_INET)
686 {
687 struct ip_mreq imr;
688 struct sockaddr_in bindAddr;
689 if (err == 0)
690 {
691 #if defined(IP_PKTINFO) // Linux
692 err = setsockopt(*sktPtr, IPPROTO_IP, IP_PKTINFO, &kOn, sizeof(kOn));
693 if (err < 0) { err = errno; perror("setsockopt - IP_PKTINFO"); }
694 #elif defined(IP_RECVDSTADDR) || defined(IP_RECVIF) // BSD and Solaris
695 #if defined(IP_RECVDSTADDR)
696 err = setsockopt(*sktPtr, IPPROTO_IP, IP_RECVDSTADDR, &kOn, sizeof(kOn));
697 if (err < 0) { err = errno; perror("setsockopt - IP_RECVDSTADDR"); }
698 #endif
699 #if defined(IP_RECVIF)
700 if (err == 0)
701 {
702 err = setsockopt(*sktPtr, IPPROTO_IP, IP_RECVIF, &kOn, sizeof(kOn));
703 if (err < 0) { err = errno; perror("setsockopt - IP_RECVIF"); }
704 }
705 #endif
706 #else
707 #warning This platform has no way to get the destination interface information -- will only work for single-homed hosts
708 #endif
709 }
710 #if defined(IP_RECVTTL) // Linux
711 if (err == 0)
712 {
713 setsockopt(*sktPtr, IPPROTO_IP, IP_RECVTTL, &kOn, sizeof(kOn));
714 // We no longer depend on being able to get the received TTL, so don't worry if the option fails
715 }
716 #endif
717
718 // Add multicast group membership on this interface
719 if (err == 0 && JoinMulticastGroup)
720 {
721 imr.imr_multiaddr.s_addr = AllDNSLinkGroup_v4.ip.v4.NotAnInteger;
722 imr.imr_interface = ((struct sockaddr_in*)intfAddr)->sin_addr;
723 err = setsockopt(*sktPtr, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(imr));
724 if (err < 0) { err = errno; perror("setsockopt - IP_ADD_MEMBERSHIP"); }
725 }
726
727 // Specify outgoing interface too
728 if (err == 0 && JoinMulticastGroup)
729 {
730 err = setsockopt(*sktPtr, IPPROTO_IP, IP_MULTICAST_IF, &((struct sockaddr_in*)intfAddr)->sin_addr, sizeof(struct in_addr));
731 if (err < 0) { err = errno; perror("setsockopt - IP_MULTICAST_IF"); }
732 }
733
734 // Per the mDNS spec, send unicast packets with TTL 255
735 if (err == 0)
736 {
737 err = setsockopt(*sktPtr, IPPROTO_IP, IP_TTL, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive));
738 if (err < 0) { err = errno; perror("setsockopt - IP_TTL"); }
739 }
740
741 // and multicast packets with TTL 255 too
742 // There's some debate as to whether IP_MULTICAST_TTL is an int or a byte so we just try both.
743 if (err == 0)
744 {
745 err = setsockopt(*sktPtr, IPPROTO_IP, IP_MULTICAST_TTL, &kByteTwoFiveFive, sizeof(kByteTwoFiveFive));
746 if (err < 0 && errno == EINVAL)
747 err = setsockopt(*sktPtr, IPPROTO_IP, IP_MULTICAST_TTL, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive));
748 if (err < 0) { err = errno; perror("setsockopt - IP_MULTICAST_TTL"); }
749 }
750
751 // And start listening for packets
752 if (err == 0)
753 {
754 bindAddr.sin_family = AF_INET;
755 bindAddr.sin_port = port.NotAnInteger;
756 bindAddr.sin_addr.s_addr = INADDR_ANY; // Want to receive multicasts AND unicasts on this socket
757 err = bind(*sktPtr, (struct sockaddr *) &bindAddr, sizeof(bindAddr));
758 if (err < 0) { err = errno; perror("bind"); fflush(stderr); }
759 }
760 } // endif (intfAddr->sa_family == AF_INET)
761
762 #if HAVE_IPV6
763 else if (intfAddr->sa_family == AF_INET6)
764 {
765 struct ipv6_mreq imr6;
766 struct sockaddr_in6 bindAddr6;
767 #if defined(IPV6_PKTINFO)
768 if (err == 0)
769 {
770 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_PKTINFO, &kOn, sizeof(kOn));
771 if (err < 0) { err = errno; perror("setsockopt - IPV6_PKTINFO"); }
772 }
773 #else
774 #warning This platform has no way to get the destination interface information for IPv6 -- will only work for single-homed hosts
775 #endif
776 #if defined(IPV6_HOPLIMIT)
777 if (err == 0)
778 {
779 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_HOPLIMIT, &kOn, sizeof(kOn));
780 if (err < 0) { err = errno; perror("setsockopt - IPV6_HOPLIMIT"); }
781 }
782 #endif
783
784 // Add multicast group membership on this interface
785 if (err == 0 && JoinMulticastGroup)
786 {
787 imr6.ipv6mr_multiaddr = *(const struct in6_addr*)&AllDNSLinkGroup_v6.ip.v6;
788 imr6.ipv6mr_interface = interfaceIndex;
789 //LogMsg("Joining %.16a on %d", &imr6.ipv6mr_multiaddr, imr6.ipv6mr_interface);
790 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_JOIN_GROUP, &imr6, sizeof(imr6));
791 if (err < 0)
792 {
793 err = errno;
794 verbosedebugf("IPV6_JOIN_GROUP %.16a on %d failed.\n", &imr6.ipv6mr_multiaddr, imr6.ipv6mr_interface);
795 perror("setsockopt - IPV6_JOIN_GROUP");
796 }
797 }
798
799 // Specify outgoing interface too
800 if (err == 0 && JoinMulticastGroup)
801 {
802 u_int multicast_if = interfaceIndex;
803 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_MULTICAST_IF, &multicast_if, sizeof(multicast_if));
804 if (err < 0) { err = errno; perror("setsockopt - IPV6_MULTICAST_IF"); }
805 }
806
807 // We want to receive only IPv6 packets on this socket.
808 // Without this option, we may get IPv4 addresses as mapped addresses.
809 if (err == 0)
810 {
811 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_V6ONLY, &kOn, sizeof(kOn));
812 if (err < 0) { err = errno; perror("setsockopt - IPV6_V6ONLY"); }
813 }
814
815 // Per the mDNS spec, send unicast packets with TTL 255
816 if (err == 0)
817 {
818 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive));
819 if (err < 0) { err = errno; perror("setsockopt - IPV6_UNICAST_HOPS"); }
820 }
821
822 // and multicast packets with TTL 255 too
823 // There's some debate as to whether IPV6_MULTICAST_HOPS is an int or a byte so we just try both.
824 if (err == 0)
825 {
826 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &kByteTwoFiveFive, sizeof(kByteTwoFiveFive));
827 if (err < 0 && errno == EINVAL)
828 err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive));
829 if (err < 0) { err = errno; perror("setsockopt - IPV6_MULTICAST_HOPS"); }
830 }
831
832 // And start listening for packets
833 if (err == 0)
834 {
835 mDNSPlatformMemZero(&bindAddr6, sizeof(bindAddr6));
836 #ifndef NOT_HAVE_SA_LEN
837 bindAddr6.sin6_len = sizeof(bindAddr6);
838 #endif
839 bindAddr6.sin6_family = AF_INET6;
840 bindAddr6.sin6_port = port.NotAnInteger;
841 bindAddr6.sin6_flowinfo = 0;
842 bindAddr6.sin6_addr = in6addr_any; // Want to receive multicasts AND unicasts on this socket
843 bindAddr6.sin6_scope_id = 0;
844 err = bind(*sktPtr, (struct sockaddr *) &bindAddr6, sizeof(bindAddr6));
845 if (err < 0) { err = errno; perror("bind"); fflush(stderr); }
846 }
847 } // endif (intfAddr->sa_family == AF_INET6)
848 #endif
849
850 // Set the socket to non-blocking.
851 if (err == 0)
852 {
853 err = fcntl(*sktPtr, F_GETFL, 0);
854 if (err < 0) err = errno;
855 else
856 {
857 err = fcntl(*sktPtr, F_SETFL, err | O_NONBLOCK);
858 if (err < 0) err = errno;
859 }
860 }
861
862 // Clean up
863 if (err != 0 && *sktPtr != -1) { assert(close(*sktPtr) == 0); *sktPtr = -1; }
864 assert((err == 0) == (*sktPtr != -1));
865 return err;
866 }
867
868 // Creates a PosixNetworkInterface for the interface whose IP address is
869 // intfAddr and whose name is intfName and registers it with mDNS core.
870 mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct sockaddr *intfMask, const char *intfName, int intfIndex)
871 {
872 int err = 0;
873 PosixNetworkInterface *intf;
874 PosixNetworkInterface *alias = NULL;
875
876 assert(m != NULL);
877 assert(intfAddr != NULL);
878 assert(intfName != NULL);
879 assert(intfMask != NULL);
880
881 // Allocate the interface structure itself.
882 intf = (PosixNetworkInterface*)malloc(sizeof(*intf));
883 if (intf == NULL) { assert(0); err = ENOMEM; }
884
885 // And make a copy of the intfName.
886 if (err == 0)
887 {
888 intf->intfName = strdup(intfName);
889 if (intf->intfName == NULL) { assert(0); err = ENOMEM; }
890 }
891
892 if (err == 0)
893 {
894 // Set up the fields required by the mDNS core.
895 SockAddrTomDNSAddr(intfAddr, &intf->coreIntf.ip, NULL);
896 SockAddrTomDNSAddr(intfMask, &intf->coreIntf.mask, NULL);
897 //LogMsg("SetupOneInterface: %#a %#a", &intf->coreIntf.ip, &intf->coreIntf.mask);
898 strncpy(intf->coreIntf.ifname, intfName, sizeof(intf->coreIntf.ifname));
899 intf->coreIntf.ifname[sizeof(intf->coreIntf.ifname)-1] = 0;
900 intf->coreIntf.Advertise = m->AdvertiseLocalAddresses;
901 intf->coreIntf.McastTxRx = mDNStrue;
902
903 // Set up the extra fields in PosixNetworkInterface.
904 assert(intf->intfName != NULL); // intf->intfName already set up above
905 intf->index = intfIndex;
906 intf->multicastSocket4 = -1;
907 #if HAVE_IPV6
908 intf->multicastSocket6 = -1;
909 #endif
910 alias = SearchForInterfaceByName(m, intf->intfName);
911 if (alias == NULL) alias = intf;
912 intf->coreIntf.InterfaceID = (mDNSInterfaceID)alias;
913
914 if (alias != intf)
915 debugf("SetupOneInterface: %s %#a is an alias of %#a", intfName, &intf->coreIntf.ip, &alias->coreIntf.ip);
916 }
917
918 // Set up the multicast socket
919 if (err == 0)
920 {
921 if (alias->multicastSocket4 == -1 && intfAddr->sa_family == AF_INET)
922 err = SetupSocket(intfAddr, MulticastDNSPort, intf->index, &alias->multicastSocket4);
923 #if HAVE_IPV6
924 else if (alias->multicastSocket6 == -1 && intfAddr->sa_family == AF_INET6)
925 err = SetupSocket(intfAddr, MulticastDNSPort, intf->index, &alias->multicastSocket6);
926 #endif
927 }
928
929 // The interface is all ready to go, let's register it with the mDNS core.
930 if (err == 0)
931 err = mDNS_RegisterInterface(m, &intf->coreIntf, mDNSfalse);
932
933 // Clean up.
934 if (err == 0)
935 {
936 num_registered_interfaces++;
937 debugf("SetupOneInterface: %s %#a Registered", intf->intfName, &intf->coreIntf.ip);
938 if (gMDNSPlatformPosixVerboseLevel > 0)
939 fprintf(stderr, "Registered interface %s\n", intf->intfName);
940 }
941 else
942 {
943 // Use intfName instead of intf->intfName in the next line to avoid dereferencing NULL.
944 debugf("SetupOneInterface: %s %#a failed to register %d", intfName, &intf->coreIntf.ip, err);
945 if (intf) { FreePosixNetworkInterface(intf); intf = NULL; }
946 }
947
948 assert((err == 0) == (intf != NULL));
949
950 return err;
951 }
952
953 // Call get_ifi_info() to obtain a list of active interfaces and call SetupOneInterface() on each one.
954 mDNSlocal int SetupInterfaceList(mDNS *const m)
955 {
956 mDNSBool foundav4 = mDNSfalse;
957 int err = 0;
958 struct ifi_info *intfList = get_ifi_info(AF_INET, mDNStrue);
959 struct ifi_info *firstLoopback = NULL;
960
961 assert(m != NULL);
962 debugf("SetupInterfaceList");
963
964 if (intfList == NULL) err = ENOENT;
965
966 #if HAVE_IPV6
967 if (err == 0) /* Link the IPv6 list to the end of the IPv4 list */
968 {
969 struct ifi_info **p = &intfList;
970 while (*p) p = &(*p)->ifi_next;
971 *p = get_ifi_info(AF_INET6, mDNStrue);
972 }
973 #endif
974
975 if (err == 0)
976 {
977 struct ifi_info *i = intfList;
978 while (i)
979 {
980 if ( ((i->ifi_addr->sa_family == AF_INET)
981 #if HAVE_IPV6
982 || (i->ifi_addr->sa_family == AF_INET6)
983 #endif
984 ) && (i->ifi_flags & IFF_UP) && !(i->ifi_flags & IFF_POINTOPOINT))
985 {
986 if (i->ifi_flags & IFF_LOOPBACK)
987 {
988 if (firstLoopback == NULL)
989 firstLoopback = i;
990 }
991 else
992 {
993 if (SetupOneInterface(m, i->ifi_addr, i->ifi_netmask, i->ifi_name, i->ifi_index) == 0)
994 if (i->ifi_addr->sa_family == AF_INET)
995 foundav4 = mDNStrue;
996 }
997 }
998 i = i->ifi_next;
999 }
1000
1001 // If we found no normal interfaces but we did find a loopback interface, register the
1002 // loopback interface. This allows self-discovery if no interfaces are configured.
1003 // Temporary workaround: Multicast loopback on IPv6 interfaces appears not to work.
1004 // In the interim, we skip loopback interface only if we found at least one v4 interface to use
1005 // if ((m->HostInterfaces == NULL) && (firstLoopback != NULL))
1006 if (!foundav4 && firstLoopback)
1007 (void) SetupOneInterface(m, firstLoopback->ifi_addr, firstLoopback->ifi_netmask, firstLoopback->ifi_name, firstLoopback->ifi_index);
1008 }
1009
1010 // Clean up.
1011 if (intfList != NULL) free_ifi_info(intfList);
1012 return err;
1013 }
1014
1015 #if USES_NETLINK
1016
1017 // See <http://www.faqs.org/rfcs/rfc3549.html> for a description of NetLink
1018
1019 // Open a socket that will receive interface change notifications
1020 mDNSlocal mStatus OpenIfNotifySocket(int *pFD)
1021 {
1022 mStatus err = mStatus_NoError;
1023 struct sockaddr_nl snl;
1024 int sock;
1025 int ret;
1026
1027 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1028 if (sock < 0)
1029 return errno;
1030
1031 // Configure read to be non-blocking because inbound msg size is not known in advance
1032 (void) fcntl(sock, F_SETFL, O_NONBLOCK);
1033
1034 /* Subscribe the socket to Link & IP addr notifications. */
1035 mDNSPlatformMemZero(&snl, sizeof snl);
1036 snl.nl_family = AF_NETLINK;
1037 snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
1038 ret = bind(sock, (struct sockaddr *) &snl, sizeof snl);
1039 if (0 == ret)
1040 *pFD = sock;
1041 else
1042 err = errno;
1043
1044 return err;
1045 }
1046
1047 #if MDNS_DEBUGMSGS
1048 mDNSlocal void PrintNetLinkMsg(const struct nlmsghdr *pNLMsg)
1049 {
1050 const char *kNLMsgTypes[] = { "", "NLMSG_NOOP", "NLMSG_ERROR", "NLMSG_DONE", "NLMSG_OVERRUN" };
1051 const char *kNLRtMsgTypes[] = { "RTM_NEWLINK", "RTM_DELLINK", "RTM_GETLINK", "RTM_NEWADDR", "RTM_DELADDR", "RTM_GETADDR" };
1052
1053 printf("nlmsghdr len=%d, type=%s, flags=0x%x\n", pNLMsg->nlmsg_len,
1054 pNLMsg->nlmsg_type < RTM_BASE ? kNLMsgTypes[pNLMsg->nlmsg_type] : kNLRtMsgTypes[pNLMsg->nlmsg_type - RTM_BASE],
1055 pNLMsg->nlmsg_flags);
1056
1057 if (RTM_NEWLINK <= pNLMsg->nlmsg_type && pNLMsg->nlmsg_type <= RTM_GETLINK)
1058 {
1059 struct ifinfomsg *pIfInfo = (struct ifinfomsg*) NLMSG_DATA(pNLMsg);
1060 printf("ifinfomsg family=%d, type=%d, index=%d, flags=0x%x, change=0x%x\n", pIfInfo->ifi_family,
1061 pIfInfo->ifi_type, pIfInfo->ifi_index, pIfInfo->ifi_flags, pIfInfo->ifi_change);
1062
1063 }
1064 else if (RTM_NEWADDR <= pNLMsg->nlmsg_type && pNLMsg->nlmsg_type <= RTM_GETADDR)
1065 {
1066 struct ifaddrmsg *pIfAddr = (struct ifaddrmsg*) NLMSG_DATA(pNLMsg);
1067 printf("ifaddrmsg family=%d, index=%d, flags=0x%x\n", pIfAddr->ifa_family,
1068 pIfAddr->ifa_index, pIfAddr->ifa_flags);
1069 }
1070 printf("\n");
1071 }
1072 #endif
1073
1074 mDNSlocal mDNSu32 ProcessRoutingNotification(int sd)
1075 // Read through the messages on sd and if any indicate that any interface records should
1076 // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0.
1077 {
1078 ssize_t readCount;
1079 char buff[4096];
1080 struct nlmsghdr *pNLMsg = (struct nlmsghdr*) buff;
1081 mDNSu32 result = 0;
1082
1083 // The structure here is more complex than it really ought to be because,
1084 // unfortunately, there's no good way to size a buffer in advance large
1085 // enough to hold all pending data and so avoid message fragmentation.
1086 // (Note that FIONREAD is not supported on AF_NETLINK.)
1087
1088 readCount = read(sd, buff, sizeof buff);
1089 while (1)
1090 {
1091 // Make sure we've got an entire nlmsghdr in the buffer, and payload, too.
1092 // If not, discard already-processed messages in buffer and read more data.
1093 if (((char*) &pNLMsg[1] > (buff + readCount)) || // i.e. *pNLMsg extends off end of buffer
1094 ((char*) pNLMsg + pNLMsg->nlmsg_len > (buff + readCount)))
1095 {
1096 if (buff < (char*) pNLMsg) // we have space to shuffle
1097 {
1098 // discard processed data
1099 readCount -= ((char*) pNLMsg - buff);
1100 memmove(buff, pNLMsg, readCount);
1101 pNLMsg = (struct nlmsghdr*) buff;
1102
1103 // read more data
1104 readCount += read(sd, buff + readCount, sizeof buff - readCount);
1105 continue; // spin around and revalidate with new readCount
1106 }
1107 else
1108 break; // Otherwise message does not fit in buffer
1109 }
1110
1111 #if MDNS_DEBUGMSGS
1112 PrintNetLinkMsg(pNLMsg);
1113 #endif
1114
1115 // Process the NetLink message
1116 if (pNLMsg->nlmsg_type == RTM_GETLINK || pNLMsg->nlmsg_type == RTM_NEWLINK)
1117 result |= 1 << ((struct ifinfomsg*) NLMSG_DATA(pNLMsg))->ifi_index;
1118 else if (pNLMsg->nlmsg_type == RTM_DELADDR || pNLMsg->nlmsg_type == RTM_NEWADDR)
1119 result |= 1 << ((struct ifaddrmsg*) NLMSG_DATA(pNLMsg))->ifa_index;
1120
1121 // Advance pNLMsg to the next message in the buffer
1122 if ((pNLMsg->nlmsg_flags & NLM_F_MULTI) != 0 && pNLMsg->nlmsg_type != NLMSG_DONE)
1123 {
1124 ssize_t len = readCount - ((char*)pNLMsg - buff);
1125 pNLMsg = NLMSG_NEXT(pNLMsg, len);
1126 }
1127 else
1128 break; // all done!
1129 }
1130
1131 return result;
1132 }
1133
1134 #else // USES_NETLINK
1135
1136 // Open a socket that will receive interface change notifications
1137 mDNSlocal mStatus OpenIfNotifySocket(int *pFD)
1138 {
1139 *pFD = socket(AF_ROUTE, SOCK_RAW, 0);
1140
1141 if (*pFD < 0)
1142 return mStatus_UnknownErr;
1143
1144 // Configure read to be non-blocking because inbound msg size is not known in advance
1145 (void) fcntl(*pFD, F_SETFL, O_NONBLOCK);
1146
1147 return mStatus_NoError;
1148 }
1149
1150 #if MDNS_DEBUGMSGS
1151 mDNSlocal void PrintRoutingSocketMsg(const struct ifa_msghdr *pRSMsg)
1152 {
1153 const char *kRSMsgTypes[] = { "", "RTM_ADD", "RTM_DELETE", "RTM_CHANGE", "RTM_GET", "RTM_LOSING",
1154 "RTM_REDIRECT", "RTM_MISS", "RTM_LOCK", "RTM_OLDADD", "RTM_OLDDEL", "RTM_RESOLVE",
1155 "RTM_NEWADDR", "RTM_DELADDR", "RTM_IFINFO", "RTM_NEWMADDR", "RTM_DELMADDR" };
1156
1157 int index = pRSMsg->ifam_type == RTM_IFINFO ? ((struct if_msghdr*) pRSMsg)->ifm_index : pRSMsg->ifam_index;
1158
1159 printf("ifa_msghdr len=%d, type=%s, index=%d\n", pRSMsg->ifam_msglen, kRSMsgTypes[pRSMsg->ifam_type], index);
1160 }
1161 #endif
1162
1163 mDNSlocal mDNSu32 ProcessRoutingNotification(int sd)
1164 // Read through the messages on sd and if any indicate that any interface records should
1165 // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0.
1166 {
1167 ssize_t readCount;
1168 char buff[4096];
1169 struct ifa_msghdr *pRSMsg = (struct ifa_msghdr*) buff;
1170 mDNSu32 result = 0;
1171
1172 readCount = read(sd, buff, sizeof buff);
1173 if (readCount < (ssize_t) sizeof(struct ifa_msghdr))
1174 return mStatus_UnsupportedErr; // cannot decipher message
1175
1176 #if MDNS_DEBUGMSGS
1177 PrintRoutingSocketMsg(pRSMsg);
1178 #endif
1179
1180 // Process the message
1181 if (pRSMsg->ifam_type == RTM_NEWADDR || pRSMsg->ifam_type == RTM_DELADDR ||
1182 pRSMsg->ifam_type == RTM_IFINFO)
1183 {
1184 if (pRSMsg->ifam_type == RTM_IFINFO)
1185 result |= 1 << ((struct if_msghdr*) pRSMsg)->ifm_index;
1186 else
1187 result |= 1 << pRSMsg->ifam_index;
1188 }
1189
1190 return result;
1191 }
1192
1193 #endif // USES_NETLINK
1194
1195 // Called when data appears on interface change notification socket
1196 mDNSlocal void InterfaceChangeCallback(int fd, short filter, void *context)
1197 {
1198 IfChangeRec *pChgRec = (IfChangeRec*) context;
1199 fd_set readFDs;
1200 mDNSu32 changedInterfaces = 0;
1201 struct timeval zeroTimeout = { 0, 0 };
1202
1203 (void)fd; // Unused
1204 (void)filter; // Unused
1205
1206 FD_ZERO(&readFDs);
1207 FD_SET(pChgRec->NotifySD, &readFDs);
1208
1209 do
1210 {
1211 changedInterfaces |= ProcessRoutingNotification(pChgRec->NotifySD);
1212 }
1213 while (0 < select(pChgRec->NotifySD + 1, &readFDs, (fd_set*) NULL, (fd_set*) NULL, &zeroTimeout));
1214
1215 // Currently we rebuild the entire interface list whenever any interface change is
1216 // detected. If this ever proves to be a performance issue in a multi-homed
1217 // configuration, more care should be paid to changedInterfaces.
1218 if (changedInterfaces)
1219 mDNSPlatformPosixRefreshInterfaceList(pChgRec->mDNS);
1220 }
1221
1222 // Register with either a Routing Socket or RtNetLink to listen for interface changes.
1223 mDNSlocal mStatus WatchForInterfaceChange(mDNS *const m)
1224 {
1225 mStatus err;
1226 IfChangeRec *pChgRec;
1227
1228 pChgRec = (IfChangeRec*) mDNSPlatformMemAllocate(sizeof *pChgRec);
1229 if (pChgRec == NULL)
1230 return mStatus_NoMemoryErr;
1231
1232 pChgRec->mDNS = m;
1233 err = OpenIfNotifySocket(&pChgRec->NotifySD);
1234 if (err == 0)
1235 err = mDNSPosixAddFDToEventLoop(pChgRec->NotifySD, InterfaceChangeCallback, pChgRec);
1236
1237 return err;
1238 }
1239
1240 // Test to see if we're the first client running on UDP port 5353, by trying to bind to 5353 without using SO_REUSEPORT.
1241 // If we fail, someone else got here first. That's not a big problem; we can share the port for multicast responses --
1242 // we just need to be aware that we shouldn't expect to successfully receive unicast UDP responses.
1243 mDNSlocal mDNSBool mDNSPlatformInit_CanReceiveUnicast(void)
1244 {
1245 int err;
1246 int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1247 struct sockaddr_in s5353;
1248 s5353.sin_family = AF_INET;
1249 s5353.sin_port = MulticastDNSPort.NotAnInteger;
1250 s5353.sin_addr.s_addr = 0;
1251 err = bind(s, (struct sockaddr *)&s5353, sizeof(s5353));
1252 close(s);
1253 if (err) debugf("No unicast UDP responses");
1254 else debugf("Unicast UDP responses okay");
1255 return(err == 0);
1256 }
1257
1258 // mDNS core calls this routine to initialise the platform-specific data.
1259 mDNSexport mStatus mDNSPlatformInit(mDNS *const m)
1260 {
1261 int err = 0;
1262 struct sockaddr sa;
1263 assert(m != NULL);
1264
1265 if (mDNSPlatformInit_CanReceiveUnicast()) m->CanReceiveUnicastOn5353 = mDNStrue;
1266
1267 // Tell mDNS core the names of this machine.
1268
1269 // Set up the nice label
1270 m->nicelabel.c[0] = 0;
1271 GetUserSpecifiedFriendlyComputerName(&m->nicelabel);
1272 if (m->nicelabel.c[0] == 0) MakeDomainLabelFromLiteralString(&m->nicelabel, "Computer");
1273
1274 // Set up the RFC 1034-compliant label
1275 m->hostlabel.c[0] = 0;
1276 GetUserSpecifiedRFC1034ComputerName(&m->hostlabel);
1277 if (m->hostlabel.c[0] == 0) MakeDomainLabelFromLiteralString(&m->hostlabel, "Computer");
1278
1279 mDNS_SetFQDN(m);
1280
1281 sa.sa_family = AF_INET;
1282 m->p->unicastSocket4 = -1;
1283 if (err == mStatus_NoError) err = SetupSocket(&sa, zeroIPPort, 0, &m->p->unicastSocket4);
1284 #if HAVE_IPV6
1285 sa.sa_family = AF_INET6;
1286 m->p->unicastSocket6 = -1;
1287 if (err == mStatus_NoError) err = SetupSocket(&sa, zeroIPPort, 0, &m->p->unicastSocket6);
1288 #endif
1289
1290 // Tell mDNS core about the network interfaces on this machine.
1291 if (err == mStatus_NoError) err = SetupInterfaceList(m);
1292
1293 // Tell mDNS core about DNS Servers
1294 mDNS_Lock(m);
1295 if (err == mStatus_NoError) ParseDNSServers(m, uDNS_SERVERS_FILE);
1296 mDNS_Unlock(m);
1297
1298 if (err == mStatus_NoError)
1299 {
1300 err = WatchForInterfaceChange(m);
1301 // Failure to observe interface changes is non-fatal.
1302 if (err != mStatus_NoError)
1303 {
1304 fprintf(stderr, "mDNS(%d) WARNING: Unable to detect interface changes (%d).\n", getpid(), err);
1305 err = mStatus_NoError;
1306 }
1307 }
1308
1309 // We don't do asynchronous initialization on the Posix platform, so by the time
1310 // we get here the setup will already have succeeded or failed. If it succeeded,
1311 // we should just call mDNSCoreInitComplete() immediately.
1312 if (err == mStatus_NoError)
1313 mDNSCoreInitComplete(m, mStatus_NoError);
1314
1315 return PosixErrorToStatus(err);
1316 }
1317
1318 // mDNS core calls this routine to clean up the platform-specific data.
1319 // In our case all we need to do is to tear down every network interface.
1320 mDNSexport void mDNSPlatformClose(mDNS *const m)
1321 {
1322 assert(m != NULL);
1323 ClearInterfaceList(m);
1324 if (m->p->unicastSocket4 != -1) assert(close(m->p->unicastSocket4) == 0);
1325 #if HAVE_IPV6
1326 if (m->p->unicastSocket6 != -1) assert(close(m->p->unicastSocket6) == 0);
1327 #endif
1328 }
1329
1330 mDNSexport mStatus mDNSPlatformPosixRefreshInterfaceList(mDNS *const m)
1331 {
1332 int err;
1333 ClearInterfaceList(m);
1334 err = SetupInterfaceList(m);
1335 return PosixErrorToStatus(err);
1336 }
1337
1338 #if COMPILER_LIKES_PRAGMA_MARK
1339 #pragma mark ***** Locking
1340 #endif
1341
1342 // On the Posix platform, locking is a no-op because we only ever enter
1343 // mDNS core on the main thread.
1344
1345 // mDNS core calls this routine when it wants to prevent
1346 // the platform from reentering mDNS core code.
1347 mDNSexport void mDNSPlatformLock (const mDNS *const m)
1348 {
1349 (void) m; // Unused
1350 }
1351
1352 // mDNS core calls this routine when it release the lock taken by
1353 // mDNSPlatformLock and allow the platform to reenter mDNS core code.
1354 mDNSexport void mDNSPlatformUnlock (const mDNS *const m)
1355 {
1356 (void) m; // Unused
1357 }
1358
1359 #if COMPILER_LIKES_PRAGMA_MARK
1360 #pragma mark ***** Strings
1361 #endif
1362
1363 // mDNS core calls this routine to copy C strings.
1364 // On the Posix platform this maps directly to the ANSI C strcpy.
1365 mDNSexport void mDNSPlatformStrCopy(void *dst, const void *src)
1366 {
1367 strcpy((char *)dst, (char *)src);
1368 }
1369
1370 // mDNS core calls this routine to get the length of a C string.
1371 // On the Posix platform this maps directly to the ANSI C strlen.
1372 mDNSexport mDNSu32 mDNSPlatformStrLen (const void *src)
1373 {
1374 return strlen((char*)src);
1375 }
1376
1377 // mDNS core calls this routine to copy memory.
1378 // On the Posix platform this maps directly to the ANSI C memcpy.
1379 mDNSexport void mDNSPlatformMemCopy(void *dst, const void *src, mDNSu32 len)
1380 {
1381 memcpy(dst, src, len);
1382 }
1383
1384 // mDNS core calls this routine to test whether blocks of memory are byte-for-byte
1385 // identical. On the Posix platform this is a simple wrapper around ANSI C memcmp.
1386 mDNSexport mDNSBool mDNSPlatformMemSame(const void *dst, const void *src, mDNSu32 len)
1387 {
1388 return memcmp(dst, src, len) == 0;
1389 }
1390
1391 // mDNS core calls this routine to clear blocks of memory.
1392 // On the Posix platform this is a simple wrapper around ANSI C memset.
1393 mDNSexport void mDNSPlatformMemZero(void *dst, mDNSu32 len)
1394 {
1395 memset(dst, 0, len);
1396 }
1397
1398 mDNSexport void * mDNSPlatformMemAllocate(mDNSu32 len) { return(malloc(len)); }
1399 mDNSexport void mDNSPlatformMemFree (void *mem) { free(mem); }
1400
1401 mDNSexport mDNSu32 mDNSPlatformRandomSeed(void)
1402 {
1403 struct timeval tv;
1404 gettimeofday(&tv, NULL);
1405 return(tv.tv_usec);
1406 }
1407
1408 mDNSexport mDNSs32 mDNSPlatformOneSecond = 1024;
1409
1410 mDNSexport mStatus mDNSPlatformTimeInit(void)
1411 {
1412 // No special setup is required on Posix -- we just use gettimeofday();
1413 // This is not really safe, because gettimeofday can go backwards if the user manually changes the date or time
1414 // We should find a better way to do this
1415 return(mStatus_NoError);
1416 }
1417
1418 mDNSexport mDNSs32 mDNSPlatformRawTime()
1419 {
1420 struct timeval tv;
1421 gettimeofday(&tv, NULL);
1422 // tv.tv_sec is seconds since 1st January 1970 (GMT, with no adjustment for daylight savings time)
1423 // tv.tv_usec is microseconds since the start of this second (i.e. values 0 to 999999)
1424 // We use the lower 22 bits of tv.tv_sec for the top 22 bits of our result
1425 // and we multiply tv.tv_usec by 16 / 15625 to get a value in the range 0-1023 to go in the bottom 10 bits.
1426 // This gives us a proper modular (cyclic) counter that has a resolution of roughly 1ms (actually 1/1024 second)
1427 // and correctly cycles every 2^22 seconds (4194304 seconds = approx 48 days).
1428 return((tv.tv_sec << 10) | (tv.tv_usec * 16 / 15625));
1429 }
1430
1431 mDNSexport mDNSs32 mDNSPlatformUTC(void)
1432 {
1433 return time(NULL);
1434 }
1435
1436 mDNSlocal void mDNSPosixAddToFDSet(int *nfds, fd_set *readfds, int s)
1437 {
1438 if (*nfds < s + 1) *nfds = s + 1;
1439 FD_SET(s, readfds);
1440 }
1441
1442 mDNSexport void mDNSPosixGetFDSet(mDNS *m, int *nfds, fd_set *readfds, struct timeval *timeout)
1443 {
1444 mDNSs32 ticks;
1445 struct timeval interval;
1446
1447 // 1. Call mDNS_Execute() to let mDNSCore do what it needs to do
1448 mDNSs32 nextevent = mDNS_Execute(m);
1449
1450 // 2. Build our list of active file descriptors
1451 PosixNetworkInterface *info = (PosixNetworkInterface *)(m->HostInterfaces);
1452 if (m->p->unicastSocket4 != -1) mDNSPosixAddToFDSet(nfds, readfds, m->p->unicastSocket4);
1453 #if HAVE_IPV6
1454 if (m->p->unicastSocket6 != -1) mDNSPosixAddToFDSet(nfds, readfds, m->p->unicastSocket6);
1455 #endif
1456 while (info)
1457 {
1458 if (info->multicastSocket4 != -1) mDNSPosixAddToFDSet(nfds, readfds, info->multicastSocket4);
1459 #if HAVE_IPV6
1460 if (info->multicastSocket6 != -1) mDNSPosixAddToFDSet(nfds, readfds, info->multicastSocket6);
1461 #endif
1462 info = (PosixNetworkInterface *)(info->coreIntf.next);
1463 }
1464
1465 // 3. Calculate the time remaining to the next scheduled event (in struct timeval format)
1466 ticks = nextevent - mDNS_TimeNow(m);
1467 if (ticks < 1) ticks = 1;
1468 interval.tv_sec = ticks >> 10; // The high 22 bits are seconds
1469 interval.tv_usec = ((ticks & 0x3FF) * 15625) / 16; // The low 10 bits are 1024ths
1470
1471 // 4. If client's proposed timeout is more than what we want, then reduce it
1472 if (timeout->tv_sec > interval.tv_sec ||
1473 (timeout->tv_sec == interval.tv_sec && timeout->tv_usec > interval.tv_usec))
1474 *timeout = interval;
1475 }
1476
1477 mDNSexport void mDNSPosixProcessFDSet(mDNS *const m, fd_set *readfds)
1478 {
1479 PosixNetworkInterface *info;
1480 assert(m != NULL);
1481 assert(readfds != NULL);
1482 info = (PosixNetworkInterface *)(m->HostInterfaces);
1483
1484 if (m->p->unicastSocket4 != -1 && FD_ISSET(m->p->unicastSocket4, readfds))
1485 {
1486 FD_CLR(m->p->unicastSocket4, readfds);
1487 SocketDataReady(m, NULL, m->p->unicastSocket4);
1488 }
1489 #if HAVE_IPV6
1490 if (m->p->unicastSocket6 != -1 && FD_ISSET(m->p->unicastSocket6, readfds))
1491 {
1492 FD_CLR(m->p->unicastSocket6, readfds);
1493 SocketDataReady(m, NULL, m->p->unicastSocket6);
1494 }
1495 #endif
1496
1497 while (info)
1498 {
1499 if (info->multicastSocket4 != -1 && FD_ISSET(info->multicastSocket4, readfds))
1500 {
1501 FD_CLR(info->multicastSocket4, readfds);
1502 SocketDataReady(m, info, info->multicastSocket4);
1503 }
1504 #if HAVE_IPV6
1505 if (info->multicastSocket6 != -1 && FD_ISSET(info->multicastSocket6, readfds))
1506 {
1507 FD_CLR(info->multicastSocket6, readfds);
1508 SocketDataReady(m, info, info->multicastSocket6);
1509 }
1510 #endif
1511 info = (PosixNetworkInterface *)(info->coreIntf.next);
1512 }
1513 }
1514
1515 // update gMaxFD
1516 mDNSlocal void DetermineMaxEventFD(void)
1517 {
1518 PosixEventSource *iSource;
1519
1520 gMaxFD = 0;
1521 for (iSource=(PosixEventSource*)gEventSources.Head; iSource; iSource = iSource->Next)
1522 if (gMaxFD < iSource->fd)
1523 gMaxFD = iSource->fd;
1524 }
1525
1526 // Add a file descriptor to the set that mDNSPosixRunEventLoopOnce() listens to.
1527 mStatus mDNSPosixAddFDToEventLoop(int fd, mDNSPosixEventCallback callback, void *context)
1528 {
1529 PosixEventSource *newSource;
1530
1531 if (gEventSources.LinkOffset == 0)
1532 InitLinkedList(&gEventSources, offsetof(PosixEventSource, Next));
1533
1534 if (fd >= (int) FD_SETSIZE || fd < 0)
1535 return mStatus_UnsupportedErr;
1536 if (callback == NULL)
1537 return mStatus_BadParamErr;
1538
1539 newSource = (PosixEventSource*) malloc(sizeof *newSource);
1540 if (NULL == newSource)
1541 return mStatus_NoMemoryErr;
1542
1543 newSource->Callback = callback;
1544 newSource->Context = context;
1545 newSource->fd = fd;
1546
1547 AddToTail(&gEventSources, newSource);
1548 FD_SET(fd, &gEventFDs);
1549
1550 DetermineMaxEventFD();
1551
1552 return mStatus_NoError;
1553 }
1554
1555 // Remove a file descriptor from the set that mDNSPosixRunEventLoopOnce() listens to.
1556 mStatus mDNSPosixRemoveFDFromEventLoop(int fd)
1557 {
1558 PosixEventSource *iSource;
1559
1560 for (iSource=(PosixEventSource*)gEventSources.Head; iSource; iSource = iSource->Next)
1561 {
1562 if (fd == iSource->fd)
1563 {
1564 FD_CLR(fd, &gEventFDs);
1565 RemoveFromList(&gEventSources, iSource);
1566 free(iSource);
1567 DetermineMaxEventFD();
1568 return mStatus_NoError;
1569 }
1570 }
1571 return mStatus_NoSuchNameErr;
1572 }
1573
1574 // Simply note the received signal in gEventSignals.
1575 mDNSlocal void NoteSignal(int signum)
1576 {
1577 sigaddset(&gEventSignals, signum);
1578 }
1579
1580 // Tell the event package to listen for signal and report it in mDNSPosixRunEventLoopOnce().
1581 mStatus mDNSPosixListenForSignalInEventLoop(int signum)
1582 {
1583 struct sigaction action;
1584 mStatus err;
1585
1586 mDNSPlatformMemZero(&action, sizeof action); // more portable than member-wise assignment
1587 action.sa_handler = NoteSignal;
1588 err = sigaction(signum, &action, (struct sigaction*) NULL);
1589
1590 sigaddset(&gEventSignalSet, signum);
1591
1592 return err;
1593 }
1594
1595 // Tell the event package to stop listening for signal in mDNSPosixRunEventLoopOnce().
1596 mStatus mDNSPosixIgnoreSignalInEventLoop(int signum)
1597 {
1598 struct sigaction action;
1599 mStatus err;
1600
1601 mDNSPlatformMemZero(&action, sizeof action); // more portable than member-wise assignment
1602 action.sa_handler = SIG_DFL;
1603 err = sigaction(signum, &action, (struct sigaction*) NULL);
1604
1605 sigdelset(&gEventSignalSet, signum);
1606
1607 return err;
1608 }
1609
1610 // Do a single pass through the attendent event sources and dispatch any found to their callbacks.
1611 // Return as soon as internal timeout expires, or a signal we're listening for is received.
1612 mStatus mDNSPosixRunEventLoopOnce(mDNS *m, const struct timeval *pTimeout,
1613 sigset_t *pSignalsReceived, mDNSBool *pDataDispatched)
1614 {
1615 fd_set listenFDs = gEventFDs;
1616 int fdMax = 0, numReady;
1617 struct timeval timeout = *pTimeout;
1618
1619 // Include the sockets that are listening to the wire in our select() set
1620 mDNSPosixGetFDSet(m, &fdMax, &listenFDs, &timeout); // timeout may get modified
1621 if (fdMax < gMaxFD)
1622 fdMax = gMaxFD;
1623
1624 numReady = select(fdMax + 1, &listenFDs, (fd_set*) NULL, (fd_set*) NULL, &timeout);
1625
1626 // If any data appeared, invoke its callback
1627 if (numReady > 0)
1628 {
1629 PosixEventSource *iSource;
1630
1631 (void) mDNSPosixProcessFDSet(m, &listenFDs); // call this first to process wire data for clients
1632
1633 for (iSource=(PosixEventSource*)gEventSources.Head; iSource; iSource = iSource->Next)
1634 {
1635 if (FD_ISSET(iSource->fd, &listenFDs))
1636 {
1637 iSource->Callback(iSource->fd, 0, iSource->Context);
1638 break; // in case callback removed elements from gEventSources
1639 }
1640 }
1641 *pDataDispatched = mDNStrue;
1642 }
1643 else
1644 *pDataDispatched = mDNSfalse;
1645
1646 (void) sigprocmask(SIG_BLOCK, &gEventSignalSet, (sigset_t*) NULL);
1647 *pSignalsReceived = gEventSignals;
1648 sigemptyset(&gEventSignals);
1649 (void) sigprocmask(SIG_UNBLOCK, &gEventSignalSet, (sigset_t*) NULL);
1650
1651 return mStatus_NoError;
1652 }