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