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