]>
Commit | Line | Data |
---|---|---|
f0cc3e7b | 1 | /* -*- Mode: C; tab-width: 4; c-file-style: "bsd"; c-basic-offset: 4; fill-column: 108; indent-tabs-mode: nil; -*- |
7f0064bd | 2 | * |
f0cc3e7b | 3 | * Copyright (c) 2002-2019 Apple Inc. All rights reserved. |
c9b9ae52 | 4 | * |
67c8f8a1 A |
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 | |
83fb1e36 | 8 | * |
67c8f8a1 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
83fb1e36 | 10 | * |
67c8f8a1 A |
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 | |
c9b9ae52 | 15 | * limitations under the License. |
c9b9ae52 | 16 | * |
263eeeab | 17 | */ |
c9b9ae52 | 18 | |
7f0064bd | 19 | #include "mDNSEmbeddedAPI.h" // Defines the interface provided to the client layer above |
67c8f8a1 | 20 | #include "DNSCommon.h" |
83fb1e36 | 21 | #include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform |
f0cc3e7b | 22 | #include "PlatformCommon.h" |
283ee3ff | 23 | #include "dns_sd.h" |
c9b9ae52 A |
24 | |
25 | #include <assert.h> | |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <errno.h> | |
29 | #include <string.h> | |
30 | #include <unistd.h> | |
8e92c31c A |
31 | #include <syslog.h> |
32 | #include <stdarg.h> | |
c9b9ae52 A |
33 | #include <fcntl.h> |
34 | #include <sys/types.h> | |
8e92c31c | 35 | #include <sys/time.h> |
c9b9ae52 A |
36 | #include <sys/socket.h> |
37 | #include <sys/uio.h> | |
8e92c31c | 38 | #include <sys/select.h> |
c9b9ae52 | 39 | #include <netinet/in.h> |
7cb34e5c | 40 | #include <arpa/inet.h> |
8e92c31c | 41 | #include <time.h> // platform support for UTC time |
f0cc3e7b | 42 | #include <ifaddrs.h> |
8e92c31c A |
43 | |
44 | #if USES_NETLINK | |
45 | #include <asm/types.h> | |
46 | #include <linux/netlink.h> | |
47 | #include <linux/rtnetlink.h> | |
48 | #else // USES_NETLINK | |
49 | #include <net/route.h> | |
50 | #include <net/if.h> | |
51 | #endif // USES_NETLINK | |
c9b9ae52 A |
52 | |
53 | #include "mDNSUNP.h" | |
8e92c31c | 54 | #include "GenLinkedList.h" |
12c5fa7a | 55 | #include "dnsproxy.h" |
c9b9ae52 A |
56 | |
57 | // *************************************************************************** | |
58 | // Structures | |
59 | ||
8e92c31c A |
60 | // Context record for interface change callback |
61 | struct IfChangeRec | |
83fb1e36 A |
62 | { |
63 | int NotifySD; | |
64 | mDNS *mDNS; | |
65 | }; | |
66 | typedef struct IfChangeRec IfChangeRec; | |
8e92c31c A |
67 | |
68 | // Note that static data is initialized to zero in (modern) C. | |
f0cc3e7b | 69 | static PosixEventSource *gEventSources; // linked list of PosixEventSource's |
83fb1e36 A |
70 | static sigset_t gEventSignalSet; // Signals which event loop listens for |
71 | static sigset_t gEventSignals; // Signals which were received while inside loop | |
c9b9ae52 | 72 | |
95d7a4a3 A |
73 | static PosixNetworkInterface *gRecentInterfaces; |
74 | ||
c9b9ae52 A |
75 | // *************************************************************************** |
76 | // Globals (for debugging) | |
77 | ||
78 | static int num_registered_interfaces = 0; | |
79 | static int num_pkts_accepted = 0; | |
80 | static int num_pkts_rejected = 0; | |
81 | ||
f0cc3e7b A |
82 | // *************************************************************************** |
83 | // Locals | |
84 | mDNSlocal void requestReadEvents(PosixEventSource *eventSource, | |
85 | const char *taskName, mDNSPosixEventCallback callback, void *context); | |
86 | mDNSlocal mStatus stopReadOrWriteEvents(int fd, mDNSBool freeSource, mDNSBool removeSource, int flags); | |
87 | mDNSlocal void requestWriteEvents(PosixEventSource *eventSource, | |
88 | const char *taskName, mDNSPosixEventCallback callback, void *context); | |
c9b9ae52 A |
89 | // *************************************************************************** |
90 | // Functions | |
91 | ||
f0cc3e7b A |
92 | #if MDNS_MALLOC_DEBUGGING |
93 | mDNSexport void mDNSPlatformValidateLists(void) | |
94 | { | |
95 | // This should validate gEventSources and any other Posix-specific stuff that gets allocated. | |
96 | } | |
97 | #endif | |
98 | ||
c9b9ae52 A |
99 | int gMDNSPlatformPosixVerboseLevel = 0; |
100 | ||
c9b9ae52 A |
101 | #define PosixErrorToStatus(errNum) ((errNum) == 0 ? mStatus_NoError : mStatus_UnknownErr) |
102 | ||
7f0064bd | 103 | mDNSlocal void SockAddrTomDNSAddr(const struct sockaddr *const sa, mDNSAddr *ipAddr, mDNSIPPort *ipPort) |
83fb1e36 A |
104 | { |
105 | switch (sa->sa_family) | |
106 | { | |
107 | case AF_INET: | |
108 | { | |
109 | struct sockaddr_in *sin = (struct sockaddr_in*)sa; | |
110 | ipAddr->type = mDNSAddrType_IPv4; | |
111 | ipAddr->ip.v4.NotAnInteger = sin->sin_addr.s_addr; | |
112 | if (ipPort) ipPort->NotAnInteger = sin->sin_port; | |
113 | break; | |
114 | } | |
c9b9ae52 | 115 | |
8e92c31c | 116 | #if HAVE_IPV6 |
83fb1e36 A |
117 | case AF_INET6: |
118 | { | |
119 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa; | |
7f0064bd | 120 | #ifndef NOT_HAVE_SA_LEN |
83fb1e36 | 121 | assert(sin6->sin6_len == sizeof(*sin6)); |
7f0064bd | 122 | #endif |
83fb1e36 A |
123 | ipAddr->type = mDNSAddrType_IPv6; |
124 | ipAddr->ip.v6 = *(mDNSv6Addr*)&sin6->sin6_addr; | |
125 | if (ipPort) ipPort->NotAnInteger = sin6->sin6_port; | |
126 | break; | |
127 | } | |
c9b9ae52 A |
128 | #endif |
129 | ||
83fb1e36 A |
130 | default: |
131 | verbosedebugf("SockAddrTomDNSAddr: Uknown address family %d\n", sa->sa_family); | |
132 | ipAddr->type = mDNSAddrType_None; | |
133 | if (ipPort) ipPort->NotAnInteger = 0; | |
134 | break; | |
135 | } | |
136 | } | |
c9b9ae52 A |
137 | |
138 | #if COMPILER_LIKES_PRAGMA_MARK | |
139 | #pragma mark ***** Send and Receive | |
140 | #endif | |
141 | ||
142 | // mDNS core calls this routine when it needs to send a packet. | |
7f0064bd | 143 | mDNSexport mStatus mDNSPlatformSendUDP(const mDNS *const m, const void *const msg, const mDNSu8 *const end, |
9f221bca | 144 | mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst, |
83fb1e36 A |
145 | mDNSIPPort dstPort, mDNSBool useBackgroundTrafficClass) |
146 | { | |
147 | int err = 0; | |
148 | struct sockaddr_storage to; | |
149 | PosixNetworkInterface * thisIntf = (PosixNetworkInterface *)(InterfaceID); | |
150 | int sendingsocket = -1; | |
151 | ||
152 | (void)src; // Will need to use this parameter once we implement mDNSPlatformUDPSocket/mDNSPlatformUDPClose | |
153 | (void) useBackgroundTrafficClass; | |
154 | ||
155 | assert(m != NULL); | |
156 | assert(msg != NULL); | |
157 | assert(end != NULL); | |
158 | assert((((char *) end) - ((char *) msg)) > 0); | |
159 | ||
160 | if (dstPort.NotAnInteger == 0) | |
161 | { | |
162 | LogMsg("mDNSPlatformSendUDP: Invalid argument -dstPort is set to 0"); | |
163 | return PosixErrorToStatus(EINVAL); | |
164 | } | |
165 | if (dst->type == mDNSAddrType_IPv4) | |
166 | { | |
167 | struct sockaddr_in *sin = (struct sockaddr_in*)&to; | |
c9b9ae52 | 168 | #ifndef NOT_HAVE_SA_LEN |
83fb1e36 | 169 | sin->sin_len = sizeof(*sin); |
c9b9ae52 | 170 | #endif |
83fb1e36 A |
171 | sin->sin_family = AF_INET; |
172 | sin->sin_port = dstPort.NotAnInteger; | |
173 | sin->sin_addr.s_addr = dst->ip.v4.NotAnInteger; | |
174 | sendingsocket = thisIntf ? thisIntf->multicastSocket4 : m->p->unicastSocket4; | |
175 | } | |
c9b9ae52 | 176 | |
8e92c31c | 177 | #if HAVE_IPV6 |
83fb1e36 A |
178 | else if (dst->type == mDNSAddrType_IPv6) |
179 | { | |
180 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&to; | |
181 | mDNSPlatformMemZero(sin6, sizeof(*sin6)); | |
7f0064bd | 182 | #ifndef NOT_HAVE_SA_LEN |
83fb1e36 | 183 | sin6->sin6_len = sizeof(*sin6); |
7f0064bd | 184 | #endif |
83fb1e36 A |
185 | sin6->sin6_family = AF_INET6; |
186 | sin6->sin6_port = dstPort.NotAnInteger; | |
187 | sin6->sin6_addr = *(struct in6_addr*)&dst->ip.v6; | |
188 | sendingsocket = thisIntf ? thisIntf->multicastSocket6 : m->p->unicastSocket6; | |
189 | } | |
c9b9ae52 A |
190 | #endif |
191 | ||
83fb1e36 A |
192 | if (sendingsocket >= 0) |
193 | err = sendto(sendingsocket, msg, (char*)end - (char*)msg, 0, (struct sockaddr *)&to, GET_SA_LEN(to)); | |
c9b9ae52 | 194 | |
83fb1e36 A |
195 | if (err > 0) err = 0; |
196 | else if (err < 0) | |
197 | { | |
198 | static int MessageCount = 0; | |
67c8f8a1 | 199 | // Don't report EHOSTDOWN (i.e. ARP failure), ENETDOWN, or no route to host for unicast destinations |
83fb1e36 A |
200 | if (!mDNSAddressIsAllDNSLinkGroup(dst)) |
201 | if (errno == EHOSTDOWN || errno == ENETDOWN || errno == EHOSTUNREACH || errno == ENETUNREACH) return(mStatus_TransientErr); | |
202 | ||
203 | if (MessageCount < 1000) | |
204 | { | |
205 | MessageCount++; | |
206 | if (thisIntf) | |
207 | LogMsg("mDNSPlatformSendUDP got error %d (%s) sending packet to %#a on interface %#a/%s/%d", | |
208 | errno, strerror(errno), dst, &thisIntf->coreIntf.ip, thisIntf->intfName, thisIntf->index); | |
209 | else | |
210 | LogMsg("mDNSPlatformSendUDP got error %d (%s) sending packet to %#a", errno, strerror(errno), dst); | |
211 | } | |
212 | } | |
213 | ||
214 | return PosixErrorToStatus(err); | |
215 | } | |
c9b9ae52 | 216 | |
f0cc3e7b A |
217 | mDNSlocal void TCPReadCallback(int fd, void *context) |
218 | { | |
219 | TCPSocket *sock = context; | |
220 | (void)fd; | |
221 | ||
222 | if (sock->flags & kTCPSocketFlags_UseTLS) | |
223 | { | |
224 | // implement | |
225 | } | |
226 | else | |
227 | { | |
228 | sock->callback(sock, sock->context, mDNSfalse, sock->err); | |
229 | } | |
230 | } | |
231 | ||
232 | mDNSlocal void tcpConnectCallback(int fd, void *context) | |
233 | { | |
234 | TCPSocket *sock = context; | |
235 | mDNSBool c = !sock->connected; | |
236 | int result; | |
237 | socklen_t len = sizeof result; | |
238 | ||
239 | sock->connected = mDNStrue; | |
240 | ||
241 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &result, &len) < 0) | |
242 | { | |
243 | LogInfo("ERROR: TCPConnectCallback - unable to get connect error: socket %d: Error %d (%s)", | |
244 | sock->events.fd, result, strerror(result)); | |
245 | sock->err = mStatus_ConnFailed; | |
246 | } | |
247 | else | |
248 | { | |
249 | if (result != 0) | |
250 | { | |
251 | sock->err = mStatus_ConnFailed; | |
252 | if (result == EHOSTUNREACH || result == EADDRNOTAVAIL || result == ENETDOWN) | |
253 | { | |
254 | LogInfo("ERROR: TCPConnectCallback - connect failed: socket %d: Error %d (%s)", | |
255 | sock->events.fd, result, strerror(result)); | |
256 | } | |
257 | else | |
258 | { | |
259 | LogMsg("ERROR: TCPConnectCallback - connect failed: socket %d: Error %d (%s)", | |
260 | sock->events.fd, result, strerror(result)); | |
261 | } | |
262 | } | |
263 | else | |
264 | { | |
265 | // The connection succeeded. | |
266 | sock->connected = mDNStrue; | |
267 | // Select for read events. | |
268 | sock->events.fd = fd; | |
269 | requestReadEvents(&sock->events, "mDNSPosix::tcpConnectCallback", TCPReadCallback, sock); | |
270 | } | |
271 | } | |
272 | ||
273 | if (sock->callback) | |
274 | { | |
275 | sock->callback(sock, sock->context, c, sock->err); | |
276 | // Here sock must be assumed to be invalid, in case the callback freed it. | |
277 | return; | |
278 | } | |
279 | } | |
280 | ||
c9b9ae52 | 281 | // This routine is called when the main loop detects that data is available on a socket. |
7f0064bd | 282 | mDNSlocal void SocketDataReady(mDNS *const m, PosixNetworkInterface *intf, int skt) |
83fb1e36 A |
283 | { |
284 | mDNSAddr senderAddr, destAddr; | |
285 | mDNSIPPort senderPort; | |
286 | ssize_t packetLen; | |
287 | DNSMessage packet; | |
288 | struct my_in_pktinfo packetInfo; | |
289 | struct sockaddr_storage from; | |
290 | socklen_t fromLen; | |
291 | int flags; | |
292 | mDNSu8 ttl; | |
293 | mDNSBool reject; | |
294 | const mDNSInterfaceID InterfaceID = intf ? intf->coreIntf.InterfaceID : NULL; | |
295 | ||
296 | assert(m != NULL); | |
297 | assert(skt >= 0); | |
298 | ||
299 | fromLen = sizeof(from); | |
300 | flags = 0; | |
301 | packetLen = recvfrom_flags(skt, &packet, sizeof(packet), &flags, (struct sockaddr *) &from, &fromLen, &packetInfo, &ttl); | |
302 | ||
303 | if (packetLen >= 0) | |
304 | { | |
305 | SockAddrTomDNSAddr((struct sockaddr*)&from, &senderAddr, &senderPort); | |
306 | SockAddrTomDNSAddr((struct sockaddr*)&packetInfo.ipi_addr, &destAddr, NULL); | |
307 | ||
308 | // If we have broken IP_RECVDSTADDR functionality (so far | |
309 | // I've only seen this on OpenBSD) then apply a hack to | |
310 | // convince mDNS Core that this isn't a spoof packet. | |
311 | // Basically what we do is check to see whether the | |
312 | // packet arrived as a multicast and, if so, set its | |
313 | // destAddr to the mDNS address. | |
314 | // | |
315 | // I must admit that I could just be doing something | |
316 | // wrong on OpenBSD and hence triggering this problem | |
317 | // but I'm at a loss as to how. | |
318 | // | |
319 | // If this platform doesn't have IP_PKTINFO or IP_RECVDSTADDR, then we have | |
320 | // no way to tell the destination address or interface this packet arrived on, | |
321 | // so all we can do is just assume it's a multicast | |
322 | ||
323 | #if HAVE_BROKEN_RECVDSTADDR || (!defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)) | |
324 | if ((destAddr.NotAnInteger == 0) && (flags & MSG_MCAST)) | |
325 | { | |
326 | destAddr.type = senderAddr.type; | |
327 | if (senderAddr.type == mDNSAddrType_IPv4) destAddr.ip.v4 = AllDNSLinkGroup_v4.ip.v4; | |
328 | else if (senderAddr.type == mDNSAddrType_IPv6) destAddr.ip.v6 = AllDNSLinkGroup_v6.ip.v6; | |
329 | } | |
330 | #endif | |
331 | ||
332 | // We only accept the packet if the interface on which it came | |
333 | // in matches the interface associated with this socket. | |
334 | // We do this match by name or by index, depending on which | |
335 | // information is available. recvfrom_flags sets the name | |
336 | // to "" if the name isn't available, or the index to -1 | |
337 | // if the index is available. This accomodates the various | |
338 | // different capabilities of our target platforms. | |
339 | ||
340 | reject = mDNSfalse; | |
341 | if (!intf) | |
342 | { | |
343 | // Ignore multicasts accidentally delivered to our unicast receiving socket | |
344 | if (mDNSAddrIsDNSMulticast(&destAddr)) packetLen = -1; | |
345 | } | |
346 | else | |
347 | { | |
348 | if (packetInfo.ipi_ifname[0] != 0) reject = (strcmp(packetInfo.ipi_ifname, intf->intfName) != 0); | |
349 | else if (packetInfo.ipi_ifindex != -1) reject = (packetInfo.ipi_ifindex != intf->index); | |
350 | ||
351 | if (reject) | |
352 | { | |
353 | verbosedebugf("SocketDataReady ignored a packet from %#a to %#a on interface %s/%d expecting %#a/%s/%d/%d", | |
354 | &senderAddr, &destAddr, packetInfo.ipi_ifname, packetInfo.ipi_ifindex, | |
355 | &intf->coreIntf.ip, intf->intfName, intf->index, skt); | |
356 | packetLen = -1; | |
357 | num_pkts_rejected++; | |
358 | if (num_pkts_rejected > (num_pkts_accepted + 1) * (num_registered_interfaces + 1) * 2) | |
359 | { | |
360 | fprintf(stderr, | |
361 | "*** WARNING: Received %d packets; Accepted %d packets; Rejected %d packets because of interface mismatch\n", | |
362 | num_pkts_accepted + num_pkts_rejected, num_pkts_accepted, num_pkts_rejected); | |
363 | num_pkts_accepted = 0; | |
364 | num_pkts_rejected = 0; | |
365 | } | |
366 | } | |
367 | else | |
368 | { | |
369 | verbosedebugf("SocketDataReady got a packet from %#a to %#a on interface %#a/%s/%d/%d", | |
370 | &senderAddr, &destAddr, &intf->coreIntf.ip, intf->intfName, intf->index, skt); | |
371 | num_pkts_accepted++; | |
372 | } | |
373 | } | |
374 | } | |
375 | ||
376 | if (packetLen >= 0) | |
377 | mDNSCoreReceive(m, &packet, (mDNSu8 *)&packet + packetLen, | |
378 | &senderAddr, senderPort, &destAddr, MulticastDNSPort, InterfaceID); | |
379 | } | |
380 | ||
f0cc3e7b A |
381 | mDNSexport TCPSocket *mDNSPlatformTCPSocket(TCPSocketFlags flags, mDNSAddr_Type addrType, mDNSIPPort * port, |
382 | domainname *hostname, mDNSBool useBackgroundTrafficClass) | |
83fb1e36 | 383 | { |
f0cc3e7b A |
384 | TCPSocket *sock; |
385 | int len = sizeof (TCPSocket); | |
386 | ||
387 | (void)useBackgroundTrafficClass; | |
388 | ||
389 | if (hostname) | |
390 | { | |
391 | len += sizeof (domainname); | |
392 | } | |
393 | sock = malloc(len); | |
394 | ||
395 | if (sock == NULL) | |
396 | { | |
397 | LogMsg("mDNSPlatformTCPSocket: no memory for socket"); | |
398 | return NULL; | |
399 | } | |
400 | memset(sock, 0, sizeof *sock); | |
401 | ||
402 | if (hostname) | |
403 | { | |
404 | sock->hostname = (domainname *)(sock + 1); | |
405 | LogMsg("mDNSPlatformTCPSocket: hostname %##s", hostname->c); | |
406 | AssignDomainName(sock->hostname, hostname); | |
407 | } | |
408 | ||
409 | sock->events.fd = -1; | |
410 | if (!mDNSPosixTCPSocketSetup(&sock->events.fd, addrType, port, &sock->port)) | |
411 | { | |
412 | if (sock->events.fd != -1) close(sock->events.fd); | |
413 | free(sock); | |
414 | return mDNSNULL; | |
415 | } | |
416 | ||
417 | // Set up the other fields in the structure. | |
418 | sock->flags = flags; | |
419 | sock->err = mStatus_NoError; | |
420 | sock->setup = mDNSfalse; | |
421 | sock->connected = mDNSfalse; | |
422 | return sock; | |
83fb1e36 | 423 | } |
67c8f8a1 | 424 | |
f0cc3e7b | 425 | mDNSexport mStatus mDNSPlatformTCPSocketSetCallback(TCPSocket *sock, TCPConnectionCallback callback, void *context) |
83fb1e36 | 426 | { |
f0cc3e7b A |
427 | sock->callback = callback; |
428 | sock->context = context; | |
429 | return mStatus_NoError; | |
430 | } | |
431 | ||
432 | mDNSexport TCPSocket *mDNSPlatformTCPAccept(TCPSocketFlags flags, int fd) | |
433 | { | |
434 | TCPSocket *sock; | |
435 | ||
436 | // XXX Add! | |
437 | if (flags & kTCPSocketFlags_UseTLS) | |
438 | { | |
439 | return mDNSNULL; // not supported yet. | |
440 | } | |
441 | ||
442 | sock = (TCPSocket *) mDNSPlatformMemAllocateClear(sizeof *sock); | |
443 | if (!sock) | |
444 | { | |
445 | return mDNSNULL; | |
446 | } | |
447 | ||
448 | sock->events.fd = fd; | |
449 | sock->flags = flags; | |
450 | sock->connected = mDNStrue; | |
451 | return sock; | |
452 | } | |
453 | ||
454 | ||
455 | mDNSlocal void tcpListenCallback(int fd, void *context) | |
456 | { | |
457 | TCPListener *listener = context; | |
458 | TCPSocket *sock; | |
459 | ||
460 | sock = mDNSPosixDoTCPListenCallback(fd, listener->addressType, listener->socketFlags, | |
461 | listener->callback, listener->context); | |
462 | if (sock != NULL) | |
463 | { | |
464 | requestReadEvents(&sock->events, "mDNSPosix::tcpListenCallback", TCPReadCallback, sock); | |
465 | } | |
466 | } | |
467 | ||
468 | mDNSexport TCPListener *mDNSPlatformTCPListen(mDNSAddr_Type addrType, mDNSIPPort *port, mDNSAddr *addr, | |
469 | TCPSocketFlags socketFlags, mDNSBool reuseAddr, int queueLength, | |
470 | TCPAcceptedCallback callback, void *context) | |
471 | { | |
472 | TCPListener *ret; | |
473 | int fd = -1; | |
474 | ||
475 | if (!mDNSPosixTCPListen(&fd, addrType, port, addr, reuseAddr, queueLength)) | |
476 | { | |
477 | if (fd != -1) | |
478 | { | |
479 | close(fd); | |
480 | } | |
481 | return mDNSNULL; | |
482 | } | |
483 | ||
484 | // Allocate a listener structure | |
485 | ret = (TCPListener *) mDNSPlatformMemAllocateClear(sizeof *ret); | |
486 | if (ret == NULL) | |
487 | { | |
488 | LogMsg("mDNSPlatformTCPListen: no memory for TCPListener struct."); | |
489 | close(fd); | |
490 | return mDNSNULL; | |
491 | } | |
492 | ret->events.fd = fd; | |
493 | ret->callback = callback; | |
494 | ret->context = context; | |
495 | ret->addressType = addrType; | |
496 | ret->socketFlags = socketFlags; | |
497 | ||
498 | // When we get a connection, mDNSPosixListenCallback will be called, and it will invoke the | |
499 | // callback we were passed. | |
500 | requestReadEvents(&ret->events, "tcpListenCallback", tcpListenCallback, ret); | |
501 | return ret; | |
83fb1e36 | 502 | } |
67c8f8a1 A |
503 | |
504 | mDNSexport int mDNSPlatformTCPGetFD(TCPSocket *sock) | |
83fb1e36 | 505 | { |
f0cc3e7b | 506 | return sock->events.fd; |
83fb1e36 | 507 | } |
67c8f8a1 | 508 | |
f0cc3e7b A |
509 | mDNSexport mStatus mDNSPlatformTCPConnect(TCPSocket *sock, const mDNSAddr *dst, mDNSOpaque16 dstport, |
510 | mDNSInterfaceID InterfaceID, TCPConnectionCallback callback, void *context) | |
83fb1e36 | 511 | { |
f0cc3e7b A |
512 | int result; |
513 | union { | |
514 | struct sockaddr sa; | |
515 | struct sockaddr_in sin; | |
516 | struct sockaddr_in6 sin6; | |
517 | } addr; | |
518 | socklen_t len; | |
519 | ||
520 | sock->callback = callback; | |
521 | sock->context = context; | |
522 | sock->setup = mDNSfalse; | |
523 | sock->connected = mDNSfalse; | |
524 | sock->err = mStatus_NoError; | |
525 | ||
526 | result = fcntl(sock->events.fd, F_GETFL, 0); | |
527 | if (result < 0) | |
528 | { | |
529 | LogMsg("mDNSPlatformTCPConnect: F_GETFL failed: %s", strerror(errno)); | |
530 | return mStatus_UnknownErr; | |
531 | } | |
532 | ||
533 | result = fcntl(sock->events.fd, F_SETFL, result | O_NONBLOCK); | |
534 | if (result < 0) | |
535 | { | |
536 | LogMsg("mDNSPlatformTCPConnect: F_SETFL failed: %s", strerror(errno)); | |
537 | return mStatus_UnknownErr; | |
538 | } | |
539 | ||
540 | // If we've been asked to bind to a single interface, do it. See comment in mDNSMacOSX.c for more info. | |
541 | if (InterfaceID) | |
542 | { | |
543 | PosixNetworkInterface *iface = (PosixNetworkInterface *)InterfaceID; | |
544 | #if defined(SO_BINDTODEVICE) | |
545 | result = setsockopt(sock->events.fd, | |
546 | SOL_SOCKET, SO_BINDTODEVICE, iface->intfName, strlen(iface->intfName)); | |
547 | if (result < 0) | |
548 | { | |
549 | LogMsg("mDNSPlatformTCPConnect: SO_BINDTODEVICE failed on %s: %s", iface->intfName, strerror(errno)); | |
550 | return mStatus_BadParamErr; | |
551 | } | |
552 | #else | |
553 | if (dst->type == mDNSAddrType_IPv4) | |
554 | { | |
555 | #if defined(IP_BOUND_IF) | |
556 | result = setsockopt(sock->events.fd, IPPROTO_IP, IP_BOUND_IF, &iface->index, sizeof iface->index); | |
557 | if (result < 0) | |
558 | { | |
559 | LogMsg("mDNSPlatformTCPConnect: IP_BOUND_IF failed on %s (%d): %s", | |
560 | iface->intfName, iface->index, strerror(errno)); | |
561 | return mStatus_BadParamErr; | |
562 | } | |
563 | #else | |
564 | (void)iface; | |
565 | #endif // IP_BOUND_IF | |
566 | } | |
567 | else | |
568 | { // IPv6 | |
569 | #if defined(IPV6_BOUND_IF) | |
570 | result = setsockopt(sock->events.fd, IPPROTO_IPV6, IPV6_BOUND_IF, &iface->index, sizeof iface->index); | |
571 | if (result < 0) | |
572 | { | |
573 | LogMsg("mDNSPlatformTCPConnect: IP_BOUND_IF failed on %s (%d): %s", | |
574 | iface->intfName, iface->index, strerror(errno)); | |
575 | return mStatus_BadParamErr; | |
576 | } | |
577 | #else | |
578 | (void)iface; | |
579 | #endif // IPV6_BOUND_IF | |
580 | } | |
581 | #endif // SO_BINDTODEVICE | |
582 | } | |
583 | ||
584 | memset(&addr, 0, sizeof addr); | |
585 | if (dst->type == mDNSAddrType_IPv4) | |
586 | { | |
587 | addr.sa.sa_family = AF_INET; | |
588 | addr.sin.sin_port = dstport.NotAnInteger; | |
589 | len = sizeof (struct sockaddr_in); | |
590 | addr.sin.sin_addr.s_addr = dst->ip.v4.NotAnInteger; | |
591 | } | |
592 | else | |
593 | { | |
594 | addr.sa.sa_family = AF_INET6; | |
595 | len = sizeof (struct sockaddr_in6); | |
596 | addr.sin6.sin6_port = dstport.NotAnInteger; | |
597 | memcpy(&addr.sin6.sin6_addr.s6_addr, &dst->ip.v6, sizeof addr.sin6.sin6_addr.s6_addr); | |
598 | } | |
599 | #ifndef NOT_HAVE_SA_LEN | |
600 | addr.sa.sa_len = len; | |
601 | #endif | |
602 | ||
603 | result = connect(sock->events.fd, (struct sockaddr *)&addr, len); | |
604 | if (result < 0) | |
605 | { | |
606 | if (errno == EINPROGRESS) | |
607 | { | |
608 | requestWriteEvents(&sock->events, "mDNSPlatformConnect", tcpConnectCallback, sock); | |
609 | return mStatus_ConnPending; | |
610 | } | |
611 | if (errno == EHOSTUNREACH || errno == EADDRNOTAVAIL || errno == ENETDOWN) | |
612 | { | |
613 | LogInfo("ERROR: mDNSPlatformTCPConnect - connect failed: socket %d: Error %d (%s)", | |
614 | sock->events.fd, errno, strerror(errno)); | |
615 | } | |
616 | else | |
617 | { | |
618 | LogMsg("ERROR: mDNSPlatformTCPConnect - connect failed: socket %d: Error %d (%s) length %d", | |
619 | sock->events.fd, errno, strerror(errno), len); | |
620 | } | |
621 | return mStatus_ConnFailed; | |
622 | } | |
623 | ||
624 | LogMsg("NOTE: mDNSPlatformTCPConnect completed synchronously"); | |
625 | return mStatus_NoError; | |
83fb1e36 | 626 | } |
8e92c31c | 627 | |
67c8f8a1 | 628 | mDNSexport void mDNSPlatformTCPCloseConnection(TCPSocket *sock) |
83fb1e36 | 629 | { |
f0cc3e7b A |
630 | if (sock) |
631 | { // can sock really be NULL when this is called? | |
632 | shutdown(sock->events.fd, SHUT_RDWR); | |
633 | stopReadOrWriteEvents(sock->events.fd, mDNSfalse, mDNStrue, | |
634 | PosixEventFlag_Read | PosixEventFlag_Write); | |
635 | close(sock->events.fd); | |
636 | free(sock); | |
637 | } | |
83fb1e36 | 638 | } |
8e92c31c | 639 | |
67c8f8a1 | 640 | mDNSexport long mDNSPlatformReadTCP(TCPSocket *sock, void *buf, unsigned long buflen, mDNSBool * closed) |
83fb1e36 | 641 | { |
f0cc3e7b A |
642 | ssize_t nread; |
643 | ||
644 | *closed = mDNSfalse; | |
645 | if (sock->flags & kTCPSocketFlags_UseTLS) | |
646 | { | |
647 | // Implement... | |
648 | nread = -1; | |
649 | *closed = mDNStrue; | |
650 | } else { | |
651 | nread = mDNSPosixReadTCP(sock->events.fd, buf, buflen, closed); | |
652 | } | |
653 | return nread; | |
654 | } | |
655 | ||
656 | mDNSexport mDNSBool mDNSPlatformTCPWritable(TCPSocket *sock) | |
657 | { | |
658 | fd_set w; | |
659 | int nfds = sock->events.fd + 1; | |
660 | int count; | |
661 | struct timeval tv; | |
662 | ||
663 | if (nfds > FD_SETSIZE) | |
664 | { | |
665 | LogMsg("ERROR: mDNSPlatformTCPWritable called on an fd that won't fit in an fd_set."); | |
666 | return mDNStrue; // hope for the best? | |
667 | } | |
668 | FD_SET(sock->events.fd, &w); | |
669 | tv.tv_sec = tv.tv_usec = 0; | |
670 | count = select(nfds, NULL, &w, NULL, &tv); | |
671 | if (count > 0) | |
672 | { | |
673 | return mDNStrue; | |
674 | } | |
675 | return mDNSfalse; | |
83fb1e36 | 676 | } |
8e92c31c | 677 | |
67c8f8a1 | 678 | mDNSexport long mDNSPlatformWriteTCP(TCPSocket *sock, const char *msg, unsigned long len) |
83fb1e36 | 679 | { |
f0cc3e7b A |
680 | if (sock->flags & kTCPSocketFlags_UseTLS) |
681 | { | |
682 | // implement | |
683 | return -1; | |
684 | } | |
685 | else | |
686 | { | |
687 | return mDNSPosixWriteTCP(sock->events.fd, msg, len); | |
688 | } | |
83fb1e36 | 689 | } |
67c8f8a1 | 690 | |
12c5fa7a | 691 | mDNSexport UDPSocket *mDNSPlatformUDPSocket(mDNSIPPort port) |
83fb1e36 | 692 | { |
83fb1e36 A |
693 | (void)port; // Unused |
694 | return NULL; | |
695 | } | |
67c8f8a1 A |
696 | |
697 | mDNSexport void mDNSPlatformUDPClose(UDPSocket *sock) | |
83fb1e36 A |
698 | { |
699 | (void)sock; // Unused | |
700 | } | |
701 | ||
12c5fa7a | 702 | mDNSexport void mDNSPlatformUpdateProxyList(const mDNSInterfaceID InterfaceID) |
83fb1e36 | 703 | { |
83fb1e36 A |
704 | (void)InterfaceID; // Unused |
705 | } | |
32bb7e43 A |
706 | |
707 | mDNSexport void mDNSPlatformSendRawPacket(const void *const msg, const mDNSu8 *const end, mDNSInterfaceID InterfaceID) | |
83fb1e36 A |
708 | { |
709 | (void)msg; // Unused | |
710 | (void)end; // Unused | |
711 | (void)InterfaceID; // Unused | |
712 | } | |
713 | ||
12c5fa7a | 714 | mDNSexport void mDNSPlatformSetLocalAddressCacheEntry(const mDNSAddr *const tpa, const mDNSEthAddr *const tha, mDNSInterfaceID InterfaceID) |
83fb1e36 | 715 | { |
83fb1e36 A |
716 | (void)tpa; // Unused |
717 | (void)tha; // Unused | |
718 | (void)InterfaceID; // Unused | |
719 | } | |
67c8f8a1 A |
720 | |
721 | mDNSexport mStatus mDNSPlatformTLSSetupCerts(void) | |
83fb1e36 A |
722 | { |
723 | return(mStatus_UnsupportedErr); | |
724 | } | |
725 | ||
67c8f8a1 | 726 | mDNSexport void mDNSPlatformTLSTearDownCerts(void) |
83fb1e36 A |
727 | { |
728 | } | |
c9b9ae52 | 729 | |
12c5fa7a | 730 | mDNSexport void mDNSPlatformSetAllowSleep(mDNSBool allowSleep, const char *reason) |
83fb1e36 | 731 | { |
83fb1e36 A |
732 | (void) allowSleep; |
733 | (void) reason; | |
734 | } | |
294beb6e A |
735 | |
736 | #if COMPILER_LIKES_PRAGMA_MARK | |
737 | #pragma mark - | |
738 | #pragma mark - /etc/hosts support | |
739 | #endif | |
740 | ||
741 | mDNSexport void FreeEtcHosts(mDNS *const m, AuthRecord *const rr, mStatus result) | |
83fb1e36 | 742 | { |
294beb6e | 743 | (void)m; // unused |
83fb1e36 A |
744 | (void)rr; |
745 | (void)result; | |
746 | } | |
294beb6e A |
747 | |
748 | ||
c9b9ae52 | 749 | #if COMPILER_LIKES_PRAGMA_MARK |
67c8f8a1 | 750 | #pragma mark ***** DDNS Config Platform Functions |
c9b9ae52 A |
751 | #endif |
752 | ||
12c5fa7a | 753 | mDNSexport mDNSBool mDNSPlatformSetDNSConfig(mDNSBool setservers, mDNSBool setsearch, domainname *const fqdn, DNameListElem **RegDomains, |
51601d48 | 754 | DNameListElem **BrowseDomains, mDNSBool ackConfig) |
83fb1e36 | 755 | { |
83fb1e36 | 756 | (void) setservers; |
83fb1e36 | 757 | (void) setsearch; |
51601d48 A |
758 | (void) ackConfig; |
759 | ||
f0cc3e7b A |
760 | if (fqdn ) fqdn->c[0] = 0; |
761 | if (RegDomains ) *RegDomains = NULL; | |
762 | if (BrowseDomains) *BrowseDomains = NULL; | |
763 | ||
51601d48 | 764 | return mDNStrue; |
83fb1e36 | 765 | } |
8e92c31c | 766 | |
12c5fa7a | 767 | mDNSexport mStatus mDNSPlatformGetPrimaryInterface(mDNSAddr * v4, mDNSAddr * v6, mDNSAddr * router) |
83fb1e36 | 768 | { |
83fb1e36 A |
769 | (void) v4; |
770 | (void) v6; | |
771 | (void) router; | |
67c8f8a1 | 772 | |
83fb1e36 A |
773 | return mStatus_UnsupportedErr; |
774 | } | |
c9b9ae52 | 775 | |
67c8f8a1 | 776 | mDNSexport void mDNSPlatformDynDNSHostNameStatusChanged(const domainname *const dname, const mStatus status) |
83fb1e36 A |
777 | { |
778 | (void) dname; | |
779 | (void) status; | |
780 | } | |
7f0064bd | 781 | |
8e92c31c A |
782 | #if COMPILER_LIKES_PRAGMA_MARK |
783 | #pragma mark ***** Init and Term | |
784 | #endif | |
785 | ||
c9b9ae52 A |
786 | // This gets the current hostname, truncating it at the first dot if necessary |
787 | mDNSlocal void GetUserSpecifiedRFC1034ComputerName(domainlabel *const namelabel) | |
83fb1e36 A |
788 | { |
789 | int len = 0; | |
790 | gethostname((char *)(&namelabel->c[1]), MAX_DOMAIN_LABEL); | |
791 | while (len < MAX_DOMAIN_LABEL && namelabel->c[len+1] && namelabel->c[len+1] != '.') len++; | |
792 | namelabel->c[0] = len; | |
793 | } | |
c9b9ae52 | 794 | |
8e92c31c A |
795 | // On OS X this gets the text of the field labelled "Computer Name" in the Sharing Prefs Control Panel |
796 | // Other platforms can either get the information from the appropriate place, | |
797 | // or they can alternatively just require all registering services to provide an explicit name | |
798 | mDNSlocal void GetUserSpecifiedFriendlyComputerName(domainlabel *const namelabel) | |
83fb1e36 A |
799 | { |
800 | // On Unix we have no better name than the host name, so we just use that. | |
801 | GetUserSpecifiedRFC1034ComputerName(namelabel); | |
802 | } | |
8e92c31c | 803 | |
7cb34e5c | 804 | mDNSexport int ParseDNSServers(mDNS *m, const char *filePath) |
83fb1e36 A |
805 | { |
806 | char line[256]; | |
807 | char nameserver[16]; | |
808 | char keyword[11]; | |
809 | int numOfServers = 0; | |
810 | FILE *fp = fopen(filePath, "r"); | |
811 | if (fp == NULL) return -1; | |
812 | while (fgets(line,sizeof(line),fp)) | |
813 | { | |
814 | struct in_addr ina; | |
815 | line[255]='\0'; // just to be safe | |
816 | if (sscanf(line,"%10s %15s", keyword, nameserver) != 2) continue; // it will skip whitespaces | |
817 | if (strncasecmp(keyword,"nameserver",10)) continue; | |
818 | if (inet_aton(nameserver, (struct in_addr *)&ina) != 0) | |
819 | { | |
820 | mDNSAddr DNSAddr; | |
821 | DNSAddr.type = mDNSAddrType_IPv4; | |
822 | DNSAddr.ip.v4.NotAnInteger = ina.s_addr; | |
f0cc3e7b | 823 | mDNS_AddDNSServer(m, NULL, mDNSInterface_Any, 0, &DNSAddr, UnicastDNSPort, kScopeNone, 0, mDNSfalse, mDNSfalse, mDNSfalse, mDNSfalse, 0, mDNStrue, mDNStrue, mDNSfalse); |
83fb1e36 A |
824 | numOfServers++; |
825 | } | |
826 | } | |
f0cc3e7b | 827 | fclose(fp); |
83fb1e36 A |
828 | return (numOfServers > 0) ? 0 : -1; |
829 | } | |
7cb34e5c | 830 | |
c9b9ae52 A |
831 | // Searches the interface list looking for the named interface. |
832 | // Returns a pointer to if it found, or NULL otherwise. | |
7f0064bd | 833 | mDNSlocal PosixNetworkInterface *SearchForInterfaceByName(mDNS *const m, const char *intfName) |
83fb1e36 A |
834 | { |
835 | PosixNetworkInterface *intf; | |
c9b9ae52 | 836 | |
83fb1e36 A |
837 | assert(m != NULL); |
838 | assert(intfName != NULL); | |
c9b9ae52 | 839 | |
83fb1e36 A |
840 | intf = (PosixNetworkInterface*)(m->HostInterfaces); |
841 | while ((intf != NULL) && (strcmp(intf->intfName, intfName) != 0)) | |
842 | intf = (PosixNetworkInterface *)(intf->coreIntf.next); | |
c9b9ae52 | 843 | |
83fb1e36 A |
844 | return intf; |
845 | } | |
c9b9ae52 | 846 | |
67c8f8a1 | 847 | mDNSexport mDNSInterfaceID mDNSPlatformInterfaceIDfromInterfaceIndex(mDNS *const m, mDNSu32 index) |
83fb1e36 A |
848 | { |
849 | PosixNetworkInterface *intf; | |
8e92c31c | 850 | |
83fb1e36 | 851 | assert(m != NULL); |
8e92c31c | 852 | |
83fb1e36 A |
853 | if (index == kDNSServiceInterfaceIndexLocalOnly) return(mDNSInterface_LocalOnly); |
854 | if (index == kDNSServiceInterfaceIndexP2P ) return(mDNSInterface_P2P); | |
855 | if (index == kDNSServiceInterfaceIndexAny ) return(mDNSInterface_Any); | |
8e92c31c | 856 | |
83fb1e36 A |
857 | intf = (PosixNetworkInterface*)(m->HostInterfaces); |
858 | while ((intf != NULL) && (mDNSu32) intf->index != index) | |
859 | intf = (PosixNetworkInterface *)(intf->coreIntf.next); | |
860 | ||
861 | return (mDNSInterfaceID) intf; | |
862 | } | |
8e92c31c | 863 | |
294beb6e | 864 | mDNSexport mDNSu32 mDNSPlatformInterfaceIndexfromInterfaceID(mDNS *const m, mDNSInterfaceID id, mDNSBool suppressNetworkChange) |
83fb1e36 A |
865 | { |
866 | PosixNetworkInterface *intf; | |
867 | (void) suppressNetworkChange; // Unused | |
8e92c31c | 868 | |
83fb1e36 | 869 | assert(m != NULL); |
8e92c31c | 870 | |
83fb1e36 A |
871 | if (id == mDNSInterface_LocalOnly) return(kDNSServiceInterfaceIndexLocalOnly); |
872 | if (id == mDNSInterface_P2P ) return(kDNSServiceInterfaceIndexP2P); | |
873 | if (id == mDNSInterface_Any ) return(kDNSServiceInterfaceIndexAny); | |
8e92c31c | 874 | |
83fb1e36 A |
875 | intf = (PosixNetworkInterface*)(m->HostInterfaces); |
876 | while ((intf != NULL) && (mDNSInterfaceID) intf != id) | |
877 | intf = (PosixNetworkInterface *)(intf->coreIntf.next); | |
8e92c31c | 878 | |
95d7a4a3 A |
879 | if (intf) return intf->index; |
880 | ||
881 | // If we didn't find the interface, check the RecentInterfaces list as well | |
882 | intf = gRecentInterfaces; | |
883 | while ((intf != NULL) && (mDNSInterfaceID) intf != id) | |
884 | intf = (PosixNetworkInterface *)(intf->coreIntf.next); | |
885 | ||
83fb1e36 A |
886 | return intf ? intf->index : 0; |
887 | } | |
8e92c31c | 888 | |
c9b9ae52 A |
889 | // Frees the specified PosixNetworkInterface structure. The underlying |
890 | // interface must have already been deregistered with the mDNS core. | |
7f0064bd | 891 | mDNSlocal void FreePosixNetworkInterface(PosixNetworkInterface *intf) |
83fb1e36 | 892 | { |
e0815622 | 893 | int rv; |
83fb1e36 A |
894 | assert(intf != NULL); |
895 | if (intf->intfName != NULL) free((void *)intf->intfName); | |
e0815622 A |
896 | if (intf->multicastSocket4 != -1) |
897 | { | |
898 | rv = close(intf->multicastSocket4); | |
899 | assert(rv == 0); | |
900 | } | |
8e92c31c | 901 | #if HAVE_IPV6 |
e0815622 A |
902 | if (intf->multicastSocket6 != -1) |
903 | { | |
904 | rv = close(intf->multicastSocket6); | |
905 | assert(rv == 0); | |
906 | } | |
8e92c31c | 907 | #endif |
95d7a4a3 A |
908 | |
909 | // Move interface to the RecentInterfaces list for a minute | |
910 | intf->LastSeen = mDNSPlatformUTC(); | |
911 | intf->coreIntf.next = &gRecentInterfaces->coreIntf; | |
912 | gRecentInterfaces = intf; | |
83fb1e36 | 913 | } |
c9b9ae52 A |
914 | |
915 | // Grab the first interface, deregister it, free it, and repeat until done. | |
7f0064bd | 916 | mDNSlocal void ClearInterfaceList(mDNS *const m) |
83fb1e36 A |
917 | { |
918 | assert(m != NULL); | |
919 | ||
920 | while (m->HostInterfaces) | |
921 | { | |
922 | PosixNetworkInterface *intf = (PosixNetworkInterface*)(m->HostInterfaces); | |
12c5fa7a | 923 | mDNS_DeregisterInterface(m, &intf->coreIntf, NormalActivation); |
83fb1e36 A |
924 | if (gMDNSPlatformPosixVerboseLevel > 0) fprintf(stderr, "Deregistered interface %s\n", intf->intfName); |
925 | FreePosixNetworkInterface(intf); | |
926 | } | |
927 | num_registered_interfaces = 0; | |
928 | num_pkts_accepted = 0; | |
929 | num_pkts_rejected = 0; | |
930 | } | |
c9b9ae52 | 931 | |
8e92c31c A |
932 | // Sets up a send/receive socket. |
933 | // If mDNSIPPort port is non-zero, then it's a multicast socket on the specified interface | |
934 | // If mDNSIPPort port is zero, then it's a randomly assigned port number, used for sending unicast queries | |
7f0064bd | 935 | mDNSlocal int SetupSocket(struct sockaddr *intfAddr, mDNSIPPort port, int interfaceIndex, int *sktPtr) |
83fb1e36 A |
936 | { |
937 | int err = 0; | |
938 | static const int kOn = 1; | |
939 | static const int kIntTwoFiveFive = 255; | |
940 | static const unsigned char kByteTwoFiveFive = 255; | |
941 | const mDNSBool JoinMulticastGroup = (port.NotAnInteger != 0); | |
942 | ||
943 | (void) interfaceIndex; // This parameter unused on plaforms that don't have IPv6 | |
944 | assert(intfAddr != NULL); | |
945 | assert(sktPtr != NULL); | |
946 | assert(*sktPtr == -1); | |
947 | ||
948 | // Open the socket... | |
949 | if (intfAddr->sa_family == AF_INET) *sktPtr = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
8e92c31c | 950 | #if HAVE_IPV6 |
83fb1e36 | 951 | else if (intfAddr->sa_family == AF_INET6) *sktPtr = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
c9b9ae52 | 952 | #endif |
83fb1e36 A |
953 | else return EINVAL; |
954 | ||
955 | if (*sktPtr < 0) { err = errno; perror((intfAddr->sa_family == AF_INET) ? "socket AF_INET" : "socket AF_INET6"); } | |
956 | ||
957 | // ... with a shared UDP port, if it's for multicast receiving | |
958 | if (err == 0 && port.NotAnInteger) | |
959 | { | |
f0cc3e7b | 960 | // <rdar://problem/20946253> Suggestions from Jonny Törnbom at Axis Communications |
9f221bca A |
961 | // We test for SO_REUSEADDR first, as suggested by Jonny Törnbom from Axis Communications |
962 | // Linux kernel versions 3.9 introduces support for socket option | |
963 | // SO_REUSEPORT, however this is not implemented the same as on *BSD | |
964 | // systems. Linux version implements a "port hijacking" prevention | |
965 | // mechanism, limiting processes wanting to bind to an already existing | |
966 | // addr:port to have the same effective UID as the first who bound it. What | |
967 | // this meant for us was that the daemon ran as one user and when for | |
968 | // instance mDNSClientPosix was executed by another user, it wasn't allowed | |
969 | // to bind to the socket. Our suggestion was to switch the order in which | |
970 | // SO_REUSEPORT and SO_REUSEADDR was tested so that SO_REUSEADDR stays on | |
971 | // top and SO_REUSEPORT to be used only if SO_REUSEADDR doesn't exist. | |
972 | #if defined(SO_REUSEADDR) && !defined(__MAC_OS_X_VERSION_MIN_REQUIRED) | |
83fb1e36 | 973 | err = setsockopt(*sktPtr, SOL_SOCKET, SO_REUSEADDR, &kOn, sizeof(kOn)); |
9f221bca A |
974 | #elif defined(SO_REUSEPORT) |
975 | err = setsockopt(*sktPtr, SOL_SOCKET, SO_REUSEPORT, &kOn, sizeof(kOn)); | |
83fb1e36 A |
976 | #else |
977 | #error This platform has no way to avoid address busy errors on multicast. | |
978 | #endif | |
979 | if (err < 0) { err = errno; perror("setsockopt - SO_REUSExxxx"); } | |
95d7a4a3 | 980 | |
f0cc3e7b | 981 | #if TARGET_OS_MAC |
95d7a4a3 A |
982 | // Enable inbound packets on IFEF_AWDL interface. |
983 | // Only done for multicast sockets, since we don't expect unicast socket operations | |
984 | // on the IFEF_AWDL interface. Operation is a no-op for other interface types. | |
985 | #ifndef SO_RECV_ANYIF | |
986 | #define SO_RECV_ANYIF 0x1104 /* unrestricted inbound processing */ | |
987 | #endif | |
988 | if (setsockopt(*sktPtr, SOL_SOCKET, SO_RECV_ANYIF, &kOn, sizeof(kOn)) < 0) perror("setsockopt - SO_RECV_ANYIF"); | |
f0cc3e7b | 989 | #endif |
83fb1e36 A |
990 | } |
991 | ||
992 | // We want to receive destination addresses and interface identifiers. | |
993 | if (intfAddr->sa_family == AF_INET) | |
994 | { | |
995 | struct ip_mreq imr; | |
996 | struct sockaddr_in bindAddr; | |
997 | if (err == 0) | |
998 | { | |
999 | #if defined(IP_PKTINFO) // Linux | |
1000 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_PKTINFO, &kOn, sizeof(kOn)); | |
1001 | if (err < 0) { err = errno; perror("setsockopt - IP_PKTINFO"); } | |
1002 | #elif defined(IP_RECVDSTADDR) || defined(IP_RECVIF) // BSD and Solaris | |
1003 | #if defined(IP_RECVDSTADDR) | |
1004 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_RECVDSTADDR, &kOn, sizeof(kOn)); | |
1005 | if (err < 0) { err = errno; perror("setsockopt - IP_RECVDSTADDR"); } | |
1006 | #endif | |
1007 | #if defined(IP_RECVIF) | |
1008 | if (err == 0) | |
1009 | { | |
1010 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_RECVIF, &kOn, sizeof(kOn)); | |
1011 | if (err < 0) { err = errno; perror("setsockopt - IP_RECVIF"); } | |
1012 | } | |
1013 | #endif | |
1014 | #else | |
1015 | #warning This platform has no way to get the destination interface information -- will only work for single-homed hosts | |
1016 | #endif | |
1017 | } | |
1018 | #if defined(IP_RECVTTL) // Linux | |
1019 | if (err == 0) | |
1020 | { | |
1021 | setsockopt(*sktPtr, IPPROTO_IP, IP_RECVTTL, &kOn, sizeof(kOn)); | |
1022 | // We no longer depend on being able to get the received TTL, so don't worry if the option fails | |
1023 | } | |
1024 | #endif | |
1025 | ||
1026 | // Add multicast group membership on this interface | |
1027 | if (err == 0 && JoinMulticastGroup) | |
1028 | { | |
1029 | imr.imr_multiaddr.s_addr = AllDNSLinkGroup_v4.ip.v4.NotAnInteger; | |
1030 | imr.imr_interface = ((struct sockaddr_in*)intfAddr)->sin_addr; | |
1031 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(imr)); | |
1032 | if (err < 0) { err = errno; perror("setsockopt - IP_ADD_MEMBERSHIP"); } | |
1033 | } | |
1034 | ||
1035 | // Specify outgoing interface too | |
1036 | if (err == 0 && JoinMulticastGroup) | |
1037 | { | |
1038 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_MULTICAST_IF, &((struct sockaddr_in*)intfAddr)->sin_addr, sizeof(struct in_addr)); | |
1039 | if (err < 0) { err = errno; perror("setsockopt - IP_MULTICAST_IF"); } | |
1040 | } | |
1041 | ||
1042 | // Per the mDNS spec, send unicast packets with TTL 255 | |
1043 | if (err == 0) | |
1044 | { | |
1045 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_TTL, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive)); | |
1046 | if (err < 0) { err = errno; perror("setsockopt - IP_TTL"); } | |
1047 | } | |
1048 | ||
1049 | // and multicast packets with TTL 255 too | |
1050 | // There's some debate as to whether IP_MULTICAST_TTL is an int or a byte so we just try both. | |
1051 | if (err == 0) | |
1052 | { | |
1053 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_MULTICAST_TTL, &kByteTwoFiveFive, sizeof(kByteTwoFiveFive)); | |
1054 | if (err < 0 && errno == EINVAL) | |
1055 | err = setsockopt(*sktPtr, IPPROTO_IP, IP_MULTICAST_TTL, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive)); | |
1056 | if (err < 0) { err = errno; perror("setsockopt - IP_MULTICAST_TTL"); } | |
1057 | } | |
1058 | ||
1059 | // And start listening for packets | |
1060 | if (err == 0) | |
1061 | { | |
1062 | bindAddr.sin_family = AF_INET; | |
1063 | bindAddr.sin_port = port.NotAnInteger; | |
1064 | bindAddr.sin_addr.s_addr = INADDR_ANY; // Want to receive multicasts AND unicasts on this socket | |
1065 | err = bind(*sktPtr, (struct sockaddr *) &bindAddr, sizeof(bindAddr)); | |
1066 | if (err < 0) { err = errno; perror("bind"); fflush(stderr); } | |
1067 | } | |
1068 | } // endif (intfAddr->sa_family == AF_INET) | |
c9b9ae52 | 1069 | |
8e92c31c | 1070 | #if HAVE_IPV6 |
83fb1e36 A |
1071 | else if (intfAddr->sa_family == AF_INET6) |
1072 | { | |
1073 | struct ipv6_mreq imr6; | |
1074 | struct sockaddr_in6 bindAddr6; | |
1075 | #if defined(IPV6_PKTINFO) | |
1076 | if (err == 0) | |
1077 | { | |
1078 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_2292_PKTINFO, &kOn, sizeof(kOn)); | |
1079 | if (err < 0) { err = errno; perror("setsockopt - IPV6_PKTINFO"); } | |
1080 | } | |
1081 | #else | |
1082 | #warning This platform has no way to get the destination interface information for IPv6 -- will only work for single-homed hosts | |
1083 | #endif | |
1084 | #if defined(IPV6_HOPLIMIT) | |
1085 | if (err == 0) | |
1086 | { | |
1087 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_2292_HOPLIMIT, &kOn, sizeof(kOn)); | |
1088 | if (err < 0) { err = errno; perror("setsockopt - IPV6_HOPLIMIT"); } | |
1089 | } | |
1090 | #endif | |
1091 | ||
1092 | // Add multicast group membership on this interface | |
1093 | if (err == 0 && JoinMulticastGroup) | |
1094 | { | |
1095 | imr6.ipv6mr_multiaddr = *(const struct in6_addr*)&AllDNSLinkGroup_v6.ip.v6; | |
1096 | imr6.ipv6mr_interface = interfaceIndex; | |
1097 | //LogMsg("Joining %.16a on %d", &imr6.ipv6mr_multiaddr, imr6.ipv6mr_interface); | |
1098 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_JOIN_GROUP, &imr6, sizeof(imr6)); | |
1099 | if (err < 0) | |
1100 | { | |
1101 | err = errno; | |
1102 | verbosedebugf("IPV6_JOIN_GROUP %.16a on %d failed.\n", &imr6.ipv6mr_multiaddr, imr6.ipv6mr_interface); | |
1103 | perror("setsockopt - IPV6_JOIN_GROUP"); | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | // Specify outgoing interface too | |
1108 | if (err == 0 && JoinMulticastGroup) | |
1109 | { | |
1110 | u_int multicast_if = interfaceIndex; | |
1111 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_MULTICAST_IF, &multicast_if, sizeof(multicast_if)); | |
1112 | if (err < 0) { err = errno; perror("setsockopt - IPV6_MULTICAST_IF"); } | |
1113 | } | |
1114 | ||
1115 | // We want to receive only IPv6 packets on this socket. | |
1116 | // Without this option, we may get IPv4 addresses as mapped addresses. | |
1117 | if (err == 0) | |
1118 | { | |
1119 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_V6ONLY, &kOn, sizeof(kOn)); | |
1120 | if (err < 0) { err = errno; perror("setsockopt - IPV6_V6ONLY"); } | |
1121 | } | |
1122 | ||
1123 | // Per the mDNS spec, send unicast packets with TTL 255 | |
1124 | if (err == 0) | |
1125 | { | |
1126 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive)); | |
1127 | if (err < 0) { err = errno; perror("setsockopt - IPV6_UNICAST_HOPS"); } | |
1128 | } | |
1129 | ||
1130 | // and multicast packets with TTL 255 too | |
1131 | // There's some debate as to whether IPV6_MULTICAST_HOPS is an int or a byte so we just try both. | |
1132 | if (err == 0) | |
1133 | { | |
1134 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &kByteTwoFiveFive, sizeof(kByteTwoFiveFive)); | |
1135 | if (err < 0 && errno == EINVAL) | |
1136 | err = setsockopt(*sktPtr, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &kIntTwoFiveFive, sizeof(kIntTwoFiveFive)); | |
1137 | if (err < 0) { err = errno; perror("setsockopt - IPV6_MULTICAST_HOPS"); } | |
1138 | } | |
1139 | ||
1140 | // And start listening for packets | |
1141 | if (err == 0) | |
1142 | { | |
1143 | mDNSPlatformMemZero(&bindAddr6, sizeof(bindAddr6)); | |
7f0064bd | 1144 | #ifndef NOT_HAVE_SA_LEN |
83fb1e36 | 1145 | bindAddr6.sin6_len = sizeof(bindAddr6); |
7f0064bd | 1146 | #endif |
83fb1e36 A |
1147 | bindAddr6.sin6_family = AF_INET6; |
1148 | bindAddr6.sin6_port = port.NotAnInteger; | |
1149 | bindAddr6.sin6_flowinfo = 0; | |
1150 | bindAddr6.sin6_addr = in6addr_any; // Want to receive multicasts AND unicasts on this socket | |
1151 | bindAddr6.sin6_scope_id = 0; | |
1152 | err = bind(*sktPtr, (struct sockaddr *) &bindAddr6, sizeof(bindAddr6)); | |
1153 | if (err < 0) { err = errno; perror("bind"); fflush(stderr); } | |
1154 | } | |
1155 | } // endif (intfAddr->sa_family == AF_INET6) | |
c9b9ae52 A |
1156 | #endif |
1157 | ||
83fb1e36 A |
1158 | // Set the socket to non-blocking. |
1159 | if (err == 0) | |
1160 | { | |
1161 | err = fcntl(*sktPtr, F_GETFL, 0); | |
1162 | if (err < 0) err = errno; | |
1163 | else | |
1164 | { | |
1165 | err = fcntl(*sktPtr, F_SETFL, err | O_NONBLOCK); | |
1166 | if (err < 0) err = errno; | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | // Clean up | |
e0815622 A |
1171 | if (err != 0 && *sktPtr != -1) |
1172 | { | |
1173 | int rv; | |
1174 | rv = close(*sktPtr); | |
1175 | assert(rv == 0); | |
1176 | *sktPtr = -1; | |
1177 | } | |
83fb1e36 A |
1178 | assert((err == 0) == (*sktPtr != -1)); |
1179 | return err; | |
1180 | } | |
c9b9ae52 A |
1181 | |
1182 | // Creates a PosixNetworkInterface for the interface whose IP address is | |
1183 | // intfAddr and whose name is intfName and registers it with mDNS core. | |
7f0064bd | 1184 | mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct sockaddr *intfMask, const char *intfName, int intfIndex) |
83fb1e36 A |
1185 | { |
1186 | int err = 0; | |
1187 | PosixNetworkInterface *intf; | |
1188 | PosixNetworkInterface *alias = NULL; | |
1189 | ||
1190 | assert(m != NULL); | |
1191 | assert(intfAddr != NULL); | |
1192 | assert(intfName != NULL); | |
1193 | assert(intfMask != NULL); | |
1194 | ||
1195 | // Allocate the interface structure itself. | |
9f221bca | 1196 | intf = (PosixNetworkInterface*)calloc(1, sizeof(*intf)); |
83fb1e36 A |
1197 | if (intf == NULL) { assert(0); err = ENOMEM; } |
1198 | ||
1199 | // And make a copy of the intfName. | |
1200 | if (err == 0) | |
1201 | { | |
f0cc3e7b A |
1202 | #ifdef LINUX |
1203 | char *s; | |
1204 | int len; | |
1205 | s = strchr(intfName, ':'); | |
1206 | if (s != NULL) | |
1207 | { | |
1208 | len = (s - intfName) + 1; | |
1209 | } | |
1210 | else | |
1211 | { | |
1212 | len = strlen(intfName) + 1; | |
1213 | } | |
1214 | intf->intfName = malloc(len); | |
1215 | if (intf->intfName == NULL) { assert(0); err = ENOMEM; } | |
1216 | memcpy(intf->intfName, intfName, len - 1); | |
1217 | intfName[len - 1] = 0; | |
1218 | #else | |
83fb1e36 A |
1219 | intf->intfName = strdup(intfName); |
1220 | if (intf->intfName == NULL) { assert(0); err = ENOMEM; } | |
f0cc3e7b | 1221 | #endif |
83fb1e36 A |
1222 | } |
1223 | ||
1224 | if (err == 0) | |
1225 | { | |
1226 | // Set up the fields required by the mDNS core. | |
1227 | SockAddrTomDNSAddr(intfAddr, &intf->coreIntf.ip, NULL); | |
1228 | SockAddrTomDNSAddr(intfMask, &intf->coreIntf.mask, NULL); | |
1229 | ||
1230 | //LogMsg("SetupOneInterface: %#a %#a", &intf->coreIntf.ip, &intf->coreIntf.mask); | |
1231 | strncpy(intf->coreIntf.ifname, intfName, sizeof(intf->coreIntf.ifname)); | |
1232 | intf->coreIntf.ifname[sizeof(intf->coreIntf.ifname)-1] = 0; | |
f0cc3e7b | 1233 | |
83fb1e36 A |
1234 | intf->coreIntf.Advertise = m->AdvertiseLocalAddresses; |
1235 | intf->coreIntf.McastTxRx = mDNStrue; | |
1236 | ||
1237 | // Set up the extra fields in PosixNetworkInterface. | |
1238 | assert(intf->intfName != NULL); // intf->intfName already set up above | |
1239 | intf->index = intfIndex; | |
1240 | intf->multicastSocket4 = -1; | |
8e92c31c | 1241 | #if HAVE_IPV6 |
83fb1e36 | 1242 | intf->multicastSocket6 = -1; |
8e92c31c | 1243 | #endif |
83fb1e36 A |
1244 | alias = SearchForInterfaceByName(m, intf->intfName); |
1245 | if (alias == NULL) alias = intf; | |
1246 | intf->coreIntf.InterfaceID = (mDNSInterfaceID)alias; | |
1247 | ||
1248 | if (alias != intf) | |
1249 | debugf("SetupOneInterface: %s %#a is an alias of %#a", intfName, &intf->coreIntf.ip, &alias->coreIntf.ip); | |
1250 | } | |
1251 | ||
1252 | // Set up the multicast socket | |
1253 | if (err == 0) | |
1254 | { | |
1255 | if (alias->multicastSocket4 == -1 && intfAddr->sa_family == AF_INET) | |
1256 | err = SetupSocket(intfAddr, MulticastDNSPort, intf->index, &alias->multicastSocket4); | |
8e92c31c | 1257 | #if HAVE_IPV6 |
83fb1e36 A |
1258 | else if (alias->multicastSocket6 == -1 && intfAddr->sa_family == AF_INET6) |
1259 | err = SetupSocket(intfAddr, MulticastDNSPort, intf->index, &alias->multicastSocket6); | |
c9b9ae52 | 1260 | #endif |
83fb1e36 A |
1261 | } |
1262 | ||
f9f18293 A |
1263 | // If interface is a direct link, address record will be marked as kDNSRecordTypeKnownUnique |
1264 | // and skip the probe phase of the probe/announce packet sequence. | |
1265 | intf->coreIntf.DirectLink = mDNSfalse; | |
b8d5688b | 1266 | #ifdef DIRECTLINK_INTERFACE_NAME |
9f221bca A |
1267 | if (strcmp(intfName, STRINGIFY(DIRECTLINK_INTERFACE_NAME)) == 0) |
1268 | intf->coreIntf.DirectLink = mDNStrue; | |
b8d5688b | 1269 | #endif |
9f221bca | 1270 | intf->coreIntf.SupportsUnicastMDNSResponse = mDNStrue; |
f9f18293 | 1271 | |
83fb1e36 A |
1272 | // The interface is all ready to go, let's register it with the mDNS core. |
1273 | if (err == 0) | |
12c5fa7a | 1274 | err = mDNS_RegisterInterface(m, &intf->coreIntf, NormalActivation); |
83fb1e36 A |
1275 | |
1276 | // Clean up. | |
1277 | if (err == 0) | |
1278 | { | |
1279 | num_registered_interfaces++; | |
1280 | debugf("SetupOneInterface: %s %#a Registered", intf->intfName, &intf->coreIntf.ip); | |
1281 | if (gMDNSPlatformPosixVerboseLevel > 0) | |
1282 | fprintf(stderr, "Registered interface %s\n", intf->intfName); | |
1283 | } | |
1284 | else | |
1285 | { | |
1286 | // Use intfName instead of intf->intfName in the next line to avoid dereferencing NULL. | |
1287 | debugf("SetupOneInterface: %s %#a failed to register %d", intfName, &intf->coreIntf.ip, err); | |
1288 | if (intf) { FreePosixNetworkInterface(intf); intf = NULL; } | |
1289 | } | |
1290 | ||
1291 | assert((err == 0) == (intf != NULL)); | |
1292 | ||
1293 | return err; | |
1294 | } | |
c9b9ae52 | 1295 | |
8e92c31c | 1296 | // Call get_ifi_info() to obtain a list of active interfaces and call SetupOneInterface() on each one. |
7f0064bd | 1297 | mDNSlocal int SetupInterfaceList(mDNS *const m) |
83fb1e36 A |
1298 | { |
1299 | mDNSBool foundav4 = mDNSfalse; | |
1300 | int err = 0; | |
f0cc3e7b A |
1301 | struct ifaddrs *intfList; |
1302 | struct ifaddrs *firstLoopback = NULL; | |
1303 | int firstLoopbackIndex = 0; | |
c9b9ae52 | 1304 | |
83fb1e36 A |
1305 | assert(m != NULL); |
1306 | debugf("SetupInterfaceList"); | |
c9b9ae52 | 1307 | |
f0cc3e7b | 1308 | if (getifaddrs(&intfList) < 0) |
83fb1e36 | 1309 | { |
f0cc3e7b | 1310 | err = errno; |
83fb1e36 | 1311 | } |
f0cc3e7b | 1312 | if (intfList == NULL) err = ENOENT; |
c9b9ae52 | 1313 | |
83fb1e36 A |
1314 | if (err == 0) |
1315 | { | |
f0cc3e7b | 1316 | struct ifaddrs *i = intfList; |
83fb1e36 A |
1317 | while (i) |
1318 | { | |
19fa75a9 A |
1319 | if ( i->ifa_addr != NULL && |
1320 | ((i->ifa_addr->sa_family == AF_INET) | |
8e92c31c | 1321 | #if HAVE_IPV6 |
f0cc3e7b | 1322 | || (i->ifa_addr->sa_family == AF_INET6) |
c9b9ae52 | 1323 | #endif |
f0cc3e7b | 1324 | ) && (i->ifa_flags & IFF_UP) && !(i->ifa_flags & IFF_POINTOPOINT)) |
83fb1e36 | 1325 | { |
f0cc3e7b A |
1326 | int ifIndex = if_nametoindex(i->ifa_name); |
1327 | if (ifIndex == 0) | |
1328 | { | |
1329 | continue; | |
1330 | } | |
1331 | if (i->ifa_flags & IFF_LOOPBACK) | |
83fb1e36 A |
1332 | { |
1333 | if (firstLoopback == NULL) | |
f0cc3e7b | 1334 | { |
83fb1e36 | 1335 | firstLoopback = i; |
f0cc3e7b A |
1336 | firstLoopbackIndex = ifIndex; |
1337 | } | |
83fb1e36 A |
1338 | } |
1339 | else | |
1340 | { | |
f0cc3e7b A |
1341 | if (SetupOneInterface(m, i->ifa_addr, i->ifa_netmask, i->ifa_name, ifIndex) == 0) |
1342 | { | |
1343 | if (i->ifa_addr->sa_family == AF_INET) | |
1344 | { | |
83fb1e36 | 1345 | foundav4 = mDNStrue; |
f0cc3e7b A |
1346 | } |
1347 | } | |
83fb1e36 A |
1348 | } |
1349 | } | |
f0cc3e7b | 1350 | i = i->ifa_next; |
83fb1e36 A |
1351 | } |
1352 | ||
1353 | // If we found no normal interfaces but we did find a loopback interface, register the | |
1354 | // loopback interface. This allows self-discovery if no interfaces are configured. | |
1355 | // Temporary workaround: Multicast loopback on IPv6 interfaces appears not to work. | |
1356 | // In the interim, we skip loopback interface only if we found at least one v4 interface to use | |
1357 | // if ((m->HostInterfaces == NULL) && (firstLoopback != NULL)) | |
1358 | if (!foundav4 && firstLoopback) | |
f0cc3e7b A |
1359 | { |
1360 | (void)SetupOneInterface(m, firstLoopback->ifa_addr, firstLoopback->ifa_netmask, firstLoopback->ifa_name, | |
1361 | firstLoopbackIndex); | |
1362 | } | |
83fb1e36 A |
1363 | } |
1364 | ||
1365 | // Clean up. | |
f0cc3e7b | 1366 | if (intfList != NULL) freeifaddrs(intfList); |
95d7a4a3 A |
1367 | |
1368 | // Clean up any interfaces that have been hanging around on the RecentInterfaces list for more than a minute | |
1369 | PosixNetworkInterface **ri = &gRecentInterfaces; | |
1370 | const mDNSs32 utc = mDNSPlatformUTC(); | |
1371 | while (*ri) | |
1372 | { | |
1373 | PosixNetworkInterface *pi = *ri; | |
1374 | if (utc - pi->LastSeen < 60) ri = (PosixNetworkInterface **)&pi->coreIntf.next; | |
1375 | else { *ri = (PosixNetworkInterface *)pi->coreIntf.next; free(pi); } | |
1376 | } | |
1377 | ||
83fb1e36 A |
1378 | return err; |
1379 | } | |
c9b9ae52 | 1380 | |
8e92c31c A |
1381 | #if USES_NETLINK |
1382 | ||
1383 | // See <http://www.faqs.org/rfcs/rfc3549.html> for a description of NetLink | |
1384 | ||
1385 | // Open a socket that will receive interface change notifications | |
67c8f8a1 | 1386 | mDNSlocal mStatus OpenIfNotifySocket(int *pFD) |
83fb1e36 A |
1387 | { |
1388 | mStatus err = mStatus_NoError; | |
1389 | struct sockaddr_nl snl; | |
1390 | int sock; | |
1391 | int ret; | |
1392 | ||
1393 | sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | |
1394 | if (sock < 0) | |
1395 | return errno; | |
1396 | ||
1397 | // Configure read to be non-blocking because inbound msg size is not known in advance | |
1398 | (void) fcntl(sock, F_SETFL, O_NONBLOCK); | |
1399 | ||
1400 | /* Subscribe the socket to Link & IP addr notifications. */ | |
1401 | mDNSPlatformMemZero(&snl, sizeof snl); | |
1402 | snl.nl_family = AF_NETLINK; | |
1403 | snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR; | |
1404 | ret = bind(sock, (struct sockaddr *) &snl, sizeof snl); | |
1405 | if (0 == ret) | |
1406 | *pFD = sock; | |
1407 | else | |
1408 | err = errno; | |
1409 | ||
1410 | return err; | |
1411 | } | |
8e92c31c A |
1412 | |
1413 | #if MDNS_DEBUGMSGS | |
83fb1e36 A |
1414 | mDNSlocal void PrintNetLinkMsg(const struct nlmsghdr *pNLMsg) |
1415 | { | |
1416 | const char *kNLMsgTypes[] = { "", "NLMSG_NOOP", "NLMSG_ERROR", "NLMSG_DONE", "NLMSG_OVERRUN" }; | |
1417 | const char *kNLRtMsgTypes[] = { "RTM_NEWLINK", "RTM_DELLINK", "RTM_GETLINK", "RTM_NEWADDR", "RTM_DELADDR", "RTM_GETADDR" }; | |
1418 | ||
1419 | printf("nlmsghdr len=%d, type=%s, flags=0x%x\n", pNLMsg->nlmsg_len, | |
1420 | pNLMsg->nlmsg_type < RTM_BASE ? kNLMsgTypes[pNLMsg->nlmsg_type] : kNLRtMsgTypes[pNLMsg->nlmsg_type - RTM_BASE], | |
1421 | pNLMsg->nlmsg_flags); | |
1422 | ||
1423 | if (RTM_NEWLINK <= pNLMsg->nlmsg_type && pNLMsg->nlmsg_type <= RTM_GETLINK) | |
1424 | { | |
1425 | struct ifinfomsg *pIfInfo = (struct ifinfomsg*) NLMSG_DATA(pNLMsg); | |
1426 | printf("ifinfomsg family=%d, type=%d, index=%d, flags=0x%x, change=0x%x\n", pIfInfo->ifi_family, | |
1427 | pIfInfo->ifi_type, pIfInfo->ifi_index, pIfInfo->ifi_flags, pIfInfo->ifi_change); | |
1428 | ||
1429 | } | |
1430 | else if (RTM_NEWADDR <= pNLMsg->nlmsg_type && pNLMsg->nlmsg_type <= RTM_GETADDR) | |
1431 | { | |
1432 | struct ifaddrmsg *pIfAddr = (struct ifaddrmsg*) NLMSG_DATA(pNLMsg); | |
1433 | printf("ifaddrmsg family=%d, index=%d, flags=0x%x\n", pIfAddr->ifa_family, | |
1434 | pIfAddr->ifa_index, pIfAddr->ifa_flags); | |
1435 | } | |
1436 | printf("\n"); | |
1437 | } | |
8e92c31c A |
1438 | #endif |
1439 | ||
83fb1e36 | 1440 | mDNSlocal mDNSu32 ProcessRoutingNotification(int sd) |
8e92c31c A |
1441 | // Read through the messages on sd and if any indicate that any interface records should |
1442 | // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0. | |
83fb1e36 A |
1443 | { |
1444 | ssize_t readCount; | |
1445 | char buff[4096]; | |
1446 | struct nlmsghdr *pNLMsg = (struct nlmsghdr*) buff; | |
1447 | mDNSu32 result = 0; | |
1448 | ||
1449 | // The structure here is more complex than it really ought to be because, | |
1450 | // unfortunately, there's no good way to size a buffer in advance large | |
1451 | // enough to hold all pending data and so avoid message fragmentation. | |
1452 | // (Note that FIONREAD is not supported on AF_NETLINK.) | |
1453 | ||
1454 | readCount = read(sd, buff, sizeof buff); | |
1455 | while (1) | |
1456 | { | |
1457 | // Make sure we've got an entire nlmsghdr in the buffer, and payload, too. | |
1458 | // If not, discard already-processed messages in buffer and read more data. | |
1459 | if (((char*) &pNLMsg[1] > (buff + readCount)) || // i.e. *pNLMsg extends off end of buffer | |
1460 | ((char*) pNLMsg + pNLMsg->nlmsg_len > (buff + readCount))) | |
1461 | { | |
1462 | if (buff < (char*) pNLMsg) // we have space to shuffle | |
1463 | { | |
1464 | // discard processed data | |
1465 | readCount -= ((char*) pNLMsg - buff); | |
1466 | memmove(buff, pNLMsg, readCount); | |
1467 | pNLMsg = (struct nlmsghdr*) buff; | |
1468 | ||
1469 | // read more data | |
1470 | readCount += read(sd, buff + readCount, sizeof buff - readCount); | |
1471 | continue; // spin around and revalidate with new readCount | |
1472 | } | |
1473 | else | |
1474 | break; // Otherwise message does not fit in buffer | |
1475 | } | |
8e92c31c A |
1476 | |
1477 | #if MDNS_DEBUGMSGS | |
83fb1e36 | 1478 | PrintNetLinkMsg(pNLMsg); |
8e92c31c A |
1479 | #endif |
1480 | ||
83fb1e36 A |
1481 | // Process the NetLink message |
1482 | if (pNLMsg->nlmsg_type == RTM_GETLINK || pNLMsg->nlmsg_type == RTM_NEWLINK) | |
1483 | result |= 1 << ((struct ifinfomsg*) NLMSG_DATA(pNLMsg))->ifi_index; | |
1484 | else if (pNLMsg->nlmsg_type == RTM_DELADDR || pNLMsg->nlmsg_type == RTM_NEWADDR) | |
1485 | result |= 1 << ((struct ifaddrmsg*) NLMSG_DATA(pNLMsg))->ifa_index; | |
1486 | ||
1487 | // Advance pNLMsg to the next message in the buffer | |
1488 | if ((pNLMsg->nlmsg_flags & NLM_F_MULTI) != 0 && pNLMsg->nlmsg_type != NLMSG_DONE) | |
1489 | { | |
1490 | ssize_t len = readCount - ((char*)pNLMsg - buff); | |
1491 | pNLMsg = NLMSG_NEXT(pNLMsg, len); | |
1492 | } | |
1493 | else | |
1494 | break; // all done! | |
1495 | } | |
1496 | ||
1497 | return result; | |
1498 | } | |
8e92c31c A |
1499 | |
1500 | #else // USES_NETLINK | |
1501 | ||
1502 | // Open a socket that will receive interface change notifications | |
67c8f8a1 | 1503 | mDNSlocal mStatus OpenIfNotifySocket(int *pFD) |
83fb1e36 A |
1504 | { |
1505 | *pFD = socket(AF_ROUTE, SOCK_RAW, 0); | |
8e92c31c | 1506 | |
83fb1e36 A |
1507 | if (*pFD < 0) |
1508 | return mStatus_UnknownErr; | |
8e92c31c | 1509 | |
83fb1e36 A |
1510 | // Configure read to be non-blocking because inbound msg size is not known in advance |
1511 | (void) fcntl(*pFD, F_SETFL, O_NONBLOCK); | |
8e92c31c | 1512 | |
83fb1e36 A |
1513 | return mStatus_NoError; |
1514 | } | |
8e92c31c A |
1515 | |
1516 | #if MDNS_DEBUGMSGS | |
83fb1e36 A |
1517 | mDNSlocal void PrintRoutingSocketMsg(const struct ifa_msghdr *pRSMsg) |
1518 | { | |
1519 | const char *kRSMsgTypes[] = { "", "RTM_ADD", "RTM_DELETE", "RTM_CHANGE", "RTM_GET", "RTM_LOSING", | |
1520 | "RTM_REDIRECT", "RTM_MISS", "RTM_LOCK", "RTM_OLDADD", "RTM_OLDDEL", "RTM_RESOLVE", | |
1521 | "RTM_NEWADDR", "RTM_DELADDR", "RTM_IFINFO", "RTM_NEWMADDR", "RTM_DELMADDR" }; | |
8e92c31c | 1522 | |
83fb1e36 | 1523 | int index = pRSMsg->ifam_type == RTM_IFINFO ? ((struct if_msghdr*) pRSMsg)->ifm_index : pRSMsg->ifam_index; |
8e92c31c | 1524 | |
83fb1e36 A |
1525 | printf("ifa_msghdr len=%d, type=%s, index=%d\n", pRSMsg->ifam_msglen, kRSMsgTypes[pRSMsg->ifam_type], index); |
1526 | } | |
8e92c31c A |
1527 | #endif |
1528 | ||
83fb1e36 | 1529 | mDNSlocal mDNSu32 ProcessRoutingNotification(int sd) |
8e92c31c A |
1530 | // Read through the messages on sd and if any indicate that any interface records should |
1531 | // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0. | |
83fb1e36 A |
1532 | { |
1533 | ssize_t readCount; | |
1534 | char buff[4096]; | |
1535 | struct ifa_msghdr *pRSMsg = (struct ifa_msghdr*) buff; | |
1536 | mDNSu32 result = 0; | |
8e92c31c | 1537 | |
83fb1e36 A |
1538 | readCount = read(sd, buff, sizeof buff); |
1539 | if (readCount < (ssize_t) sizeof(struct ifa_msghdr)) | |
1540 | return mStatus_UnsupportedErr; // cannot decipher message | |
8e92c31c A |
1541 | |
1542 | #if MDNS_DEBUGMSGS | |
83fb1e36 | 1543 | PrintRoutingSocketMsg(pRSMsg); |
8e92c31c A |
1544 | #endif |
1545 | ||
83fb1e36 A |
1546 | // Process the message |
1547 | if (pRSMsg->ifam_type == RTM_NEWADDR || pRSMsg->ifam_type == RTM_DELADDR || | |
1548 | pRSMsg->ifam_type == RTM_IFINFO) | |
1549 | { | |
1550 | if (pRSMsg->ifam_type == RTM_IFINFO) | |
1551 | result |= 1 << ((struct if_msghdr*) pRSMsg)->ifm_index; | |
1552 | else | |
1553 | result |= 1 << pRSMsg->ifam_index; | |
1554 | } | |
8e92c31c | 1555 | |
83fb1e36 A |
1556 | return result; |
1557 | } | |
8e92c31c A |
1558 | |
1559 | #endif // USES_NETLINK | |
1560 | ||
1561 | // Called when data appears on interface change notification socket | |
f0cc3e7b | 1562 | mDNSlocal void InterfaceChangeCallback(int fd, void *context) |
83fb1e36 A |
1563 | { |
1564 | IfChangeRec *pChgRec = (IfChangeRec*) context; | |
1565 | fd_set readFDs; | |
1566 | mDNSu32 changedInterfaces = 0; | |
1567 | struct timeval zeroTimeout = { 0, 0 }; | |
1568 | ||
1569 | (void)fd; // Unused | |
83fb1e36 A |
1570 | |
1571 | FD_ZERO(&readFDs); | |
1572 | FD_SET(pChgRec->NotifySD, &readFDs); | |
1573 | ||
1574 | do | |
1575 | { | |
1576 | changedInterfaces |= ProcessRoutingNotification(pChgRec->NotifySD); | |
1577 | } | |
1578 | while (0 < select(pChgRec->NotifySD + 1, &readFDs, (fd_set*) NULL, (fd_set*) NULL, &zeroTimeout)); | |
1579 | ||
1580 | // Currently we rebuild the entire interface list whenever any interface change is | |
1581 | // detected. If this ever proves to be a performance issue in a multi-homed | |
1582 | // configuration, more care should be paid to changedInterfaces. | |
1583 | if (changedInterfaces) | |
1584 | mDNSPlatformPosixRefreshInterfaceList(pChgRec->mDNS); | |
1585 | } | |
8e92c31c A |
1586 | |
1587 | // Register with either a Routing Socket or RtNetLink to listen for interface changes. | |
7f0064bd | 1588 | mDNSlocal mStatus WatchForInterfaceChange(mDNS *const m) |
83fb1e36 A |
1589 | { |
1590 | mStatus err; | |
1591 | IfChangeRec *pChgRec; | |
8e92c31c | 1592 | |
f0cc3e7b | 1593 | pChgRec = (IfChangeRec*) mDNSPlatformMemAllocateClear(sizeof *pChgRec); |
83fb1e36 A |
1594 | if (pChgRec == NULL) |
1595 | return mStatus_NoMemoryErr; | |
8e92c31c | 1596 | |
83fb1e36 A |
1597 | pChgRec->mDNS = m; |
1598 | err = OpenIfNotifySocket(&pChgRec->NotifySD); | |
1599 | if (err == 0) | |
1600 | err = mDNSPosixAddFDToEventLoop(pChgRec->NotifySD, InterfaceChangeCallback, pChgRec); | |
f0cc3e7b A |
1601 | if (err) |
1602 | mDNSPlatformMemFree(pChgRec); | |
8e92c31c | 1603 | |
83fb1e36 A |
1604 | return err; |
1605 | } | |
8e92c31c A |
1606 | |
1607 | // Test to see if we're the first client running on UDP port 5353, by trying to bind to 5353 without using SO_REUSEPORT. | |
1608 | // If we fail, someone else got here first. That's not a big problem; we can share the port for multicast responses -- | |
1609 | // we just need to be aware that we shouldn't expect to successfully receive unicast UDP responses. | |
7f0064bd | 1610 | mDNSlocal mDNSBool mDNSPlatformInit_CanReceiveUnicast(void) |
83fb1e36 A |
1611 | { |
1612 | int err; | |
1613 | int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
1614 | struct sockaddr_in s5353; | |
1615 | s5353.sin_family = AF_INET; | |
1616 | s5353.sin_port = MulticastDNSPort.NotAnInteger; | |
1617 | s5353.sin_addr.s_addr = 0; | |
1618 | err = bind(s, (struct sockaddr *)&s5353, sizeof(s5353)); | |
1619 | close(s); | |
1620 | if (err) debugf("No unicast UDP responses"); | |
1621 | else debugf("Unicast UDP responses okay"); | |
1622 | return(err == 0); | |
1623 | } | |
8e92c31c | 1624 | |
c9b9ae52 A |
1625 | // mDNS core calls this routine to initialise the platform-specific data. |
1626 | mDNSexport mStatus mDNSPlatformInit(mDNS *const m) | |
83fb1e36 A |
1627 | { |
1628 | int err = 0; | |
1629 | struct sockaddr sa; | |
1630 | assert(m != NULL); | |
c9b9ae52 | 1631 | |
83fb1e36 | 1632 | if (mDNSPlatformInit_CanReceiveUnicast()) m->CanReceiveUnicastOn5353 = mDNStrue; |
8e92c31c | 1633 | |
83fb1e36 | 1634 | // Tell mDNS core the names of this machine. |
c9b9ae52 | 1635 | |
83fb1e36 A |
1636 | // Set up the nice label |
1637 | m->nicelabel.c[0] = 0; | |
1638 | GetUserSpecifiedFriendlyComputerName(&m->nicelabel); | |
1639 | if (m->nicelabel.c[0] == 0) MakeDomainLabelFromLiteralString(&m->nicelabel, "Computer"); | |
c9b9ae52 | 1640 | |
83fb1e36 A |
1641 | // Set up the RFC 1034-compliant label |
1642 | m->hostlabel.c[0] = 0; | |
1643 | GetUserSpecifiedRFC1034ComputerName(&m->hostlabel); | |
1644 | if (m->hostlabel.c[0] == 0) MakeDomainLabelFromLiteralString(&m->hostlabel, "Computer"); | |
c9b9ae52 | 1645 | |
83fb1e36 | 1646 | mDNS_SetFQDN(m); |
c9b9ae52 | 1647 | |
83fb1e36 A |
1648 | sa.sa_family = AF_INET; |
1649 | m->p->unicastSocket4 = -1; | |
1650 | if (err == mStatus_NoError) err = SetupSocket(&sa, zeroIPPort, 0, &m->p->unicastSocket4); | |
8e92c31c | 1651 | #if HAVE_IPV6 |
83fb1e36 A |
1652 | sa.sa_family = AF_INET6; |
1653 | m->p->unicastSocket6 = -1; | |
1654 | if (err == mStatus_NoError) err = SetupSocket(&sa, zeroIPPort, 0, &m->p->unicastSocket6); | |
8e92c31c A |
1655 | #endif |
1656 | ||
83fb1e36 A |
1657 | // Tell mDNS core about the network interfaces on this machine. |
1658 | if (err == mStatus_NoError) err = SetupInterfaceList(m); | |
1659 | ||
1660 | // Tell mDNS core about DNS Servers | |
1661 | mDNS_Lock(m); | |
1662 | if (err == mStatus_NoError) ParseDNSServers(m, uDNS_SERVERS_FILE); | |
1663 | mDNS_Unlock(m); | |
1664 | ||
1665 | if (err == mStatus_NoError) | |
1666 | { | |
1667 | err = WatchForInterfaceChange(m); | |
1668 | // Failure to observe interface changes is non-fatal. | |
1669 | if (err != mStatus_NoError) | |
1670 | { | |
1671 | fprintf(stderr, "mDNS(%d) WARNING: Unable to detect interface changes (%d).\n", getpid(), err); | |
1672 | err = mStatus_NoError; | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | // We don't do asynchronous initialization on the Posix platform, so by the time | |
1677 | // we get here the setup will already have succeeded or failed. If it succeeded, | |
1678 | // we should just call mDNSCoreInitComplete() immediately. | |
1679 | if (err == mStatus_NoError) | |
1680 | mDNSCoreInitComplete(m, mStatus_NoError); | |
1681 | ||
1682 | return PosixErrorToStatus(err); | |
1683 | } | |
c9b9ae52 A |
1684 | |
1685 | // mDNS core calls this routine to clean up the platform-specific data. | |
1686 | // In our case all we need to do is to tear down every network interface. | |
1687 | mDNSexport void mDNSPlatformClose(mDNS *const m) | |
83fb1e36 | 1688 | { |
e0815622 | 1689 | int rv; |
83fb1e36 A |
1690 | assert(m != NULL); |
1691 | ClearInterfaceList(m); | |
e0815622 A |
1692 | if (m->p->unicastSocket4 != -1) |
1693 | { | |
1694 | rv = close(m->p->unicastSocket4); | |
1695 | assert(rv == 0); | |
1696 | } | |
8e92c31c | 1697 | #if HAVE_IPV6 |
e0815622 A |
1698 | if (m->p->unicastSocket6 != -1) |
1699 | { | |
1700 | rv = close(m->p->unicastSocket6); | |
1701 | assert(rv == 0); | |
1702 | } | |
8e92c31c | 1703 | #endif |
83fb1e36 | 1704 | } |
c9b9ae52 | 1705 | |
95d7a4a3 A |
1706 | // This is used internally by InterfaceChangeCallback. |
1707 | // It's also exported so that the Standalone Responder (mDNSResponderPosix) | |
1708 | // can call it in response to a SIGHUP (mainly for debugging purposes). | |
7cb34e5c | 1709 | mDNSexport mStatus mDNSPlatformPosixRefreshInterfaceList(mDNS *const m) |
83fb1e36 A |
1710 | { |
1711 | int err; | |
95d7a4a3 A |
1712 | // This is a pretty heavyweight way to process interface changes -- |
1713 | // destroying the entire interface list and then making fresh one from scratch. | |
1714 | // We should make it like the OS X version, which leaves unchanged interfaces alone. | |
83fb1e36 A |
1715 | ClearInterfaceList(m); |
1716 | err = SetupInterfaceList(m); | |
1717 | return PosixErrorToStatus(err); | |
1718 | } | |
c9b9ae52 A |
1719 | |
1720 | #if COMPILER_LIKES_PRAGMA_MARK | |
1721 | #pragma mark ***** Locking | |
1722 | #endif | |
1723 | ||
1724 | // On the Posix platform, locking is a no-op because we only ever enter | |
1725 | // mDNS core on the main thread. | |
1726 | ||
1727 | // mDNS core calls this routine when it wants to prevent | |
1728 | // the platform from reentering mDNS core code. | |
1729 | mDNSexport void mDNSPlatformLock (const mDNS *const m) | |
83fb1e36 A |
1730 | { |
1731 | (void) m; // Unused | |
1732 | } | |
c9b9ae52 A |
1733 | |
1734 | // mDNS core calls this routine when it release the lock taken by | |
1735 | // mDNSPlatformLock and allow the platform to reenter mDNS core code. | |
1736 | mDNSexport void mDNSPlatformUnlock (const mDNS *const m) | |
83fb1e36 A |
1737 | { |
1738 | (void) m; // Unused | |
1739 | } | |
c9b9ae52 A |
1740 | |
1741 | #if COMPILER_LIKES_PRAGMA_MARK | |
1742 | #pragma mark ***** Strings | |
1743 | #endif | |
1744 | ||
9f221bca A |
1745 | mDNSexport mDNSu32 mDNSPlatformStrLCopy(void *dst, const void *src, mDNSu32 len) |
1746 | { | |
1747 | #if HAVE_STRLCPY | |
1748 | return ((mDNSu32)strlcpy((char *)dst, (const char *)src, len)); | |
1749 | #else | |
1750 | size_t srcLen; | |
1751 | ||
1752 | srcLen = strlen((const char *)src); | |
1753 | if (srcLen < len) | |
1754 | { | |
1755 | memcpy(dst, src, srcLen + 1); | |
1756 | } | |
1757 | else if (len > 0) | |
1758 | { | |
1759 | memcpy(dst, src, len - 1); | |
1760 | ((char *)dst)[len - 1] = '\0'; | |
1761 | } | |
1762 | ||
1763 | return ((mDNSu32)srcLen); | |
1764 | #endif | |
83fb1e36 | 1765 | } |
c9b9ae52 A |
1766 | |
1767 | // mDNS core calls this routine to get the length of a C string. | |
1768 | // On the Posix platform this maps directly to the ANSI C strlen. | |
1769 | mDNSexport mDNSu32 mDNSPlatformStrLen (const void *src) | |
83fb1e36 | 1770 | { |
9f221bca | 1771 | return strlen((const char*)src); |
83fb1e36 | 1772 | } |
c9b9ae52 A |
1773 | |
1774 | // mDNS core calls this routine to copy memory. | |
1775 | // On the Posix platform this maps directly to the ANSI C memcpy. | |
67c8f8a1 | 1776 | mDNSexport void mDNSPlatformMemCopy(void *dst, const void *src, mDNSu32 len) |
83fb1e36 A |
1777 | { |
1778 | memcpy(dst, src, len); | |
1779 | } | |
c9b9ae52 A |
1780 | |
1781 | // mDNS core calls this routine to test whether blocks of memory are byte-for-byte | |
1782 | // identical. On the Posix platform this is a simple wrapper around ANSI C memcmp. | |
67c8f8a1 | 1783 | mDNSexport mDNSBool mDNSPlatformMemSame(const void *dst, const void *src, mDNSu32 len) |
83fb1e36 A |
1784 | { |
1785 | return memcmp(dst, src, len) == 0; | |
1786 | } | |
1787 | ||
1788 | // If the caller wants to know the exact return of memcmp, then use this instead | |
1789 | // of mDNSPlatformMemSame | |
1790 | mDNSexport int mDNSPlatformMemCmp(const void *dst, const void *src, mDNSu32 len) | |
1791 | { | |
1792 | return (memcmp(dst, src, len)); | |
1793 | } | |
1794 | ||
1795 | mDNSexport void mDNSPlatformQsort(void *base, int nel, int width, int (*compar)(const void *, const void *)) | |
1796 | { | |
1797 | return (qsort(base, nel, width, compar)); | |
1798 | } | |
1799 | ||
51601d48 A |
1800 | // Proxy stub functions |
1801 | mDNSexport mDNSu8 *DNSProxySetAttributes(DNSQuestion *q, DNSMessageHeader *h, DNSMessage *msg, mDNSu8 *ptr, mDNSu8 *limit) | |
1802 | { | |
1803 | (void) q; | |
1804 | (void) h; | |
1805 | (void) msg; | |
1806 | (void) ptr; | |
1807 | (void) limit; | |
1808 | ||
1809 | return ptr; | |
1810 | } | |
1811 | ||
12c5fa7a | 1812 | mDNSexport void DNSProxyInit(mDNSu32 IpIfArr[], mDNSu32 OpIf) |
51601d48 | 1813 | { |
51601d48 A |
1814 | (void) IpIfArr; |
1815 | (void) OpIf; | |
1816 | } | |
1817 | ||
12c5fa7a | 1818 | mDNSexport void DNSProxyTerminate(void) |
51601d48 | 1819 | { |
51601d48 A |
1820 | } |
1821 | ||
c9b9ae52 A |
1822 | // mDNS core calls this routine to clear blocks of memory. |
1823 | // On the Posix platform this is a simple wrapper around ANSI C memset. | |
f0cc3e7b | 1824 | mDNSexport void mDNSPlatformMemZero(void *dst, mDNSu32 len) |
83fb1e36 A |
1825 | { |
1826 | memset(dst, 0, len); | |
1827 | } | |
c9b9ae52 | 1828 | |
f0cc3e7b A |
1829 | #if !MDNS_MALLOC_DEBUGGING |
1830 | mDNSexport void *mDNSPlatformMemAllocate(mDNSu32 len) { return(mallocL("mDNSPlatformMemAllocate", len)); } | |
1831 | mDNSexport void *mDNSPlatformMemAllocateClear(mDNSu32 len) { return(callocL(name, len)); } | |
1832 | mDNSexport void mDNSPlatformMemFree (void *mem) { freeL("mDNSPlatformMemFree", mem); } | |
1833 | #endif | |
c9b9ae52 | 1834 | |
12c5fa7a A |
1835 | #if _PLATFORM_HAS_STRONG_PRNG_ |
1836 | mDNSexport mDNSu32 mDNSPlatformRandomNumber(void) | |
1837 | { | |
f0cc3e7b | 1838 | return(arc4random()); |
12c5fa7a A |
1839 | } |
1840 | #else | |
7f0064bd | 1841 | mDNSexport mDNSu32 mDNSPlatformRandomSeed(void) |
83fb1e36 A |
1842 | { |
1843 | struct timeval tv; | |
1844 | gettimeofday(&tv, NULL); | |
1845 | return(tv.tv_usec); | |
1846 | } | |
12c5fa7a | 1847 | #endif |
7f0064bd | 1848 | |
83fb1e36 | 1849 | mDNSexport mDNSs32 mDNSPlatformOneSecond = 1024; |
c9b9ae52 | 1850 | |
7f0064bd | 1851 | mDNSexport mStatus mDNSPlatformTimeInit(void) |
83fb1e36 A |
1852 | { |
1853 | // No special setup is required on Posix -- we just use gettimeofday(); | |
1854 | // This is not really safe, because gettimeofday can go backwards if the user manually changes the date or time | |
1855 | // We should find a better way to do this | |
1856 | return(mStatus_NoError); | |
1857 | } | |
c9b9ae52 | 1858 | |
7f0064bd | 1859 | mDNSexport mDNSs32 mDNSPlatformRawTime() |
83fb1e36 | 1860 | { |
f0cc3e7b A |
1861 | struct timespec tm; |
1862 | int ret = clock_gettime(CLOCK_MONOTONIC, &tm); | |
1863 | assert(ret == 0); // This call will only fail if the number of seconds does not fit in an object of type time_t. | |
1864 | ||
1865 | // tm.tv_sec is seconds since some unspecified starting point (it is usually the system start up time) | |
1866 | // tm.tv_nsec is nanoseconds since the start of this second (i.e. values 0 to 999999999) | |
1867 | // We use the lower 22 bits of tm.tv_sec for the top 22 bits of our result | |
1868 | // and we multiply tm.tv_nsec by 2 / 1953125 to get a value in the range 0-1023 to go in the bottom 10 bits. | |
83fb1e36 A |
1869 | // This gives us a proper modular (cyclic) counter that has a resolution of roughly 1ms (actually 1/1024 second) |
1870 | // and correctly cycles every 2^22 seconds (4194304 seconds = approx 48 days). | |
f0cc3e7b A |
1871 | |
1872 | return ((tm.tv_sec << 10) | (tm.tv_nsec * 2 / 1953125)); | |
83fb1e36 | 1873 | } |
c9b9ae52 | 1874 | |
8e92c31c | 1875 | mDNSexport mDNSs32 mDNSPlatformUTC(void) |
83fb1e36 A |
1876 | { |
1877 | return time(NULL); | |
1878 | } | |
8e92c31c | 1879 | |
12c5fa7a | 1880 | mDNSexport void mDNSPlatformSendWakeupPacket(mDNSInterfaceID InterfaceID, char *EthAddr, char *IPAddr, int iteration) |
83fb1e36 | 1881 | { |
83fb1e36 A |
1882 | (void) InterfaceID; |
1883 | (void) EthAddr; | |
1884 | (void) IPAddr; | |
1885 | (void) iteration; | |
1886 | } | |
1f519c61 | 1887 | |
9f221bca | 1888 | mDNSexport mDNSBool mDNSPlatformValidRecordForInterface(const AuthRecord *rr, mDNSInterfaceID InterfaceID) |
83fb1e36 A |
1889 | { |
1890 | (void) rr; | |
9f221bca | 1891 | (void) InterfaceID; |
83fb1e36 A |
1892 | |
1893 | return 1; | |
1894 | } | |
1895 | ||
1896 | mDNSexport mDNSBool mDNSPlatformValidQuestionForInterface(DNSQuestion *q, const NetworkInterfaceInfo *intf) | |
1897 | { | |
1898 | (void) q; | |
1899 | (void) intf; | |
1900 | ||
1901 | return 1; | |
1902 | } | |
1903 | ||
1904 | // Used for debugging purposes. For now, just set the buffer to zero | |
1905 | mDNSexport void mDNSPlatformFormatTime(unsigned long te, mDNSu8 *buf, int bufsize) | |
1906 | { | |
1907 | (void) te; | |
1908 | if (bufsize) buf[0] = 0; | |
1909 | } | |
1910 | ||
1911 | mDNSexport void mDNSPlatformSendKeepalive(mDNSAddr *sadd, mDNSAddr *dadd, mDNSIPPort *lport, mDNSIPPort *rport, mDNSu32 seq, mDNSu32 ack, mDNSu16 win) | |
1912 | { | |
1913 | (void) sadd; // Unused | |
1914 | (void) dadd; // Unused | |
1915 | (void) lport; // Unused | |
1916 | (void) rport; // Unused | |
1917 | (void) seq; // Unused | |
1918 | (void) ack; // Unused | |
1919 | (void) win; // Unused | |
1920 | } | |
1921 | ||
12c5fa7a | 1922 | mDNSexport mStatus mDNSPlatformRetrieveTCPInfo(mDNSAddr *laddr, mDNSIPPort *lport, mDNSAddr *raddr, mDNSIPPort *rport, mDNSTCPInfo *mti) |
83fb1e36 | 1923 | { |
83fb1e36 A |
1924 | (void) laddr; // Unused |
1925 | (void) raddr; // Unused | |
1926 | (void) lport; // Unused | |
1927 | (void) rport; // Unused | |
1928 | (void) mti; // Unused | |
1929 | ||
1930 | return mStatus_NoError; | |
1931 | } | |
294beb6e | 1932 | |
12c5fa7a | 1933 | mDNSexport mStatus mDNSPlatformGetRemoteMacAddr(mDNSAddr *raddr) |
51601d48 A |
1934 | { |
1935 | (void) raddr; // Unused | |
51601d48 A |
1936 | |
1937 | return mStatus_NoError; | |
1938 | } | |
1939 | ||
1940 | mDNSexport mStatus mDNSPlatformStoreSPSMACAddr(mDNSAddr *spsaddr, char *ifname) | |
1941 | { | |
1942 | (void) spsaddr; // Unused | |
1943 | (void) ifname; // Unused | |
1944 | ||
1945 | return mStatus_NoError; | |
1946 | } | |
1947 | ||
9f221bca | 1948 | mDNSexport mStatus mDNSPlatformClearSPSData(void) |
f9f18293 A |
1949 | { |
1950 | return mStatus_NoError; | |
1951 | } | |
1952 | ||
9f221bca A |
1953 | mDNSexport mStatus mDNSPlatformStoreOwnerOptRecord(char *ifname, DNSMessage *msg, int length) |
1954 | { | |
1955 | (void) ifname; // Unused | |
1956 | (void) msg; // Unused | |
1957 | (void) length; // Unused | |
1958 | return mStatus_UnsupportedErr; | |
1959 | } | |
1960 | ||
51601d48 A |
1961 | mDNSexport mDNSu16 mDNSPlatformGetUDPPort(UDPSocket *sock) |
1962 | { | |
1963 | (void) sock; // unused | |
9f221bca | 1964 | |
51601d48 A |
1965 | return (mDNSu16)-1; |
1966 | } | |
1967 | ||
1968 | mDNSexport mDNSBool mDNSPlatformInterfaceIsD2D(mDNSInterfaceID InterfaceID) | |
1969 | { | |
1970 | (void) InterfaceID; // unused | |
51601d48 | 1971 | |
9f221bca | 1972 | return mDNSfalse; |
51601d48 A |
1973 | } |
1974 | ||
e0815622 | 1975 | mDNSexport void mDNSPlatformSetSocktOpt(void *sock, mDNSTransport_Type transType, mDNSAddr_Type addrType, const DNSQuestion *q) |
51601d48 | 1976 | { |
9f221bca A |
1977 | (void) sock; |
1978 | (void) transType; | |
1979 | (void) addrType; | |
51601d48 A |
1980 | (void) q; |
1981 | } | |
1982 | ||
1983 | mDNSexport mDNSs32 mDNSPlatformGetPID() | |
1984 | { | |
1985 | return 0; | |
1986 | } | |
1987 | ||
8e92c31c | 1988 | mDNSlocal void mDNSPosixAddToFDSet(int *nfds, fd_set *readfds, int s) |
83fb1e36 A |
1989 | { |
1990 | if (*nfds < s + 1) *nfds = s + 1; | |
1991 | FD_SET(s, readfds); | |
1992 | } | |
8e92c31c | 1993 | |
f0cc3e7b | 1994 | mDNSexport void mDNSPosixGetFDSetForSelect(mDNS *m, int *nfds, fd_set *readfds, fd_set *writefds) |
83fb1e36 | 1995 | { |
f0cc3e7b A |
1996 | int numFDs = *nfds; |
1997 | PosixEventSource *iSource; | |
c9b9ae52 | 1998 | |
83fb1e36 A |
1999 | // 2. Build our list of active file descriptors |
2000 | PosixNetworkInterface *info = (PosixNetworkInterface *)(m->HostInterfaces); | |
f0cc3e7b | 2001 | if (m->p->unicastSocket4 != -1) mDNSPosixAddToFDSet(&numFDs, readfds, m->p->unicastSocket4); |
8e92c31c | 2002 | #if HAVE_IPV6 |
f0cc3e7b | 2003 | if (m->p->unicastSocket6 != -1) mDNSPosixAddToFDSet(&numFDs, readfds, m->p->unicastSocket6); |
8e92c31c | 2004 | #endif |
83fb1e36 A |
2005 | while (info) |
2006 | { | |
f0cc3e7b | 2007 | if (info->multicastSocket4 != -1) mDNSPosixAddToFDSet(&numFDs, readfds, info->multicastSocket4); |
8e92c31c | 2008 | #if HAVE_IPV6 |
f0cc3e7b | 2009 | if (info->multicastSocket6 != -1) mDNSPosixAddToFDSet(&numFDs, readfds, info->multicastSocket6); |
8e92c31c | 2010 | #endif |
83fb1e36 A |
2011 | info = (PosixNetworkInterface *)(info->coreIntf.next); |
2012 | } | |
c9b9ae52 | 2013 | |
f0cc3e7b A |
2014 | // Copy over the event fds. We have to do it this way because client-provided event loops expect |
2015 | // to initialize their FD sets first and then call mDNSPosixGetFDSet() | |
2016 | for (iSource = gEventSources; iSource; iSource = iSource->next) | |
2017 | { | |
2018 | if (iSource->readCallback != NULL) | |
2019 | FD_SET(iSource->fd, readfds); | |
2020 | if (iSource->writeCallback != NULL) | |
2021 | FD_SET(iSource->fd, writefds); | |
2022 | if (numFDs <= iSource->fd) | |
2023 | numFDs = iSource->fd + 1; | |
2024 | } | |
2025 | *nfds = numFDs; | |
2026 | } | |
2027 | ||
2028 | mDNSexport void mDNSPosixGetNextDNSEventTime(mDNS *m, struct timeval *timeout) | |
2029 | { | |
2030 | mDNSs32 ticks; | |
2031 | struct timeval interval; | |
2032 | ||
2033 | // 1. Call mDNS_Execute() to let mDNSCore do what it needs to do | |
2034 | mDNSs32 nextevent = mDNS_Execute(m); | |
2035 | ||
83fb1e36 A |
2036 | // 3. Calculate the time remaining to the next scheduled event (in struct timeval format) |
2037 | ticks = nextevent - mDNS_TimeNow(m); | |
2038 | if (ticks < 1) ticks = 1; | |
2039 | interval.tv_sec = ticks >> 10; // The high 22 bits are seconds | |
2040 | interval.tv_usec = ((ticks & 0x3FF) * 15625) / 16; // The low 10 bits are 1024ths | |
c9b9ae52 | 2041 | |
83fb1e36 A |
2042 | // 4. If client's proposed timeout is more than what we want, then reduce it |
2043 | if (timeout->tv_sec > interval.tv_sec || | |
2044 | (timeout->tv_sec == interval.tv_sec && timeout->tv_usec > interval.tv_usec)) | |
2045 | *timeout = interval; | |
2046 | } | |
c9b9ae52 | 2047 | |
f0cc3e7b A |
2048 | mDNSexport void mDNSPosixGetFDSet(mDNS *m, int *nfds, fd_set *readfds, fd_set *writefds, struct timeval *timeout) |
2049 | { | |
2050 | mDNSPosixGetNextDNSEventTime(m, timeout); | |
2051 | mDNSPosixGetFDSetForSelect(m, nfds, readfds, writefds); | |
2052 | } | |
2053 | ||
2054 | mDNSexport void mDNSPosixProcessFDSet(mDNS *const m, fd_set *readfds, fd_set *writefds) | |
83fb1e36 A |
2055 | { |
2056 | PosixNetworkInterface *info; | |
f0cc3e7b | 2057 | PosixEventSource *iSource; |
83fb1e36 A |
2058 | assert(m != NULL); |
2059 | assert(readfds != NULL); | |
2060 | info = (PosixNetworkInterface *)(m->HostInterfaces); | |
2061 | ||
2062 | if (m->p->unicastSocket4 != -1 && FD_ISSET(m->p->unicastSocket4, readfds)) | |
2063 | { | |
2064 | FD_CLR(m->p->unicastSocket4, readfds); | |
2065 | SocketDataReady(m, NULL, m->p->unicastSocket4); | |
2066 | } | |
8e92c31c | 2067 | #if HAVE_IPV6 |
83fb1e36 A |
2068 | if (m->p->unicastSocket6 != -1 && FD_ISSET(m->p->unicastSocket6, readfds)) |
2069 | { | |
2070 | FD_CLR(m->p->unicastSocket6, readfds); | |
2071 | SocketDataReady(m, NULL, m->p->unicastSocket6); | |
2072 | } | |
8e92c31c A |
2073 | #endif |
2074 | ||
83fb1e36 A |
2075 | while (info) |
2076 | { | |
2077 | if (info->multicastSocket4 != -1 && FD_ISSET(info->multicastSocket4, readfds)) | |
2078 | { | |
2079 | FD_CLR(info->multicastSocket4, readfds); | |
2080 | SocketDataReady(m, info, info->multicastSocket4); | |
2081 | } | |
8e92c31c | 2082 | #if HAVE_IPV6 |
83fb1e36 A |
2083 | if (info->multicastSocket6 != -1 && FD_ISSET(info->multicastSocket6, readfds)) |
2084 | { | |
2085 | FD_CLR(info->multicastSocket6, readfds); | |
2086 | SocketDataReady(m, info, info->multicastSocket6); | |
2087 | } | |
8e92c31c | 2088 | #endif |
83fb1e36 A |
2089 | info = (PosixNetworkInterface *)(info->coreIntf.next); |
2090 | } | |
83fb1e36 | 2091 | |
f0cc3e7b A |
2092 | // Now process routing socket events, discovery relay events and anything else of that ilk. |
2093 | for (iSource = gEventSources; iSource; iSource = iSource->next) | |
2094 | { | |
2095 | if (iSource->readCallback != NULL && FD_ISSET(iSource->fd, readfds)) | |
2096 | { | |
2097 | iSource->readCallback(iSource->fd, iSource->readContext); | |
2098 | break; // in case callback removed elements from gEventSources | |
2099 | } | |
2100 | else if (iSource->writeCallback != NULL && FD_ISSET(iSource->fd, writefds)) | |
2101 | { | |
2102 | mDNSPosixEventCallback writeCallback = iSource->writeCallback; | |
2103 | // Write events are one-shot: to get another event, the consumer has to put in a new request. | |
2104 | // We reset this before calling the callback just in case the callback requests another write | |
2105 | // callback, or deletes the event context from the list. | |
2106 | iSource->writeCallback = NULL; | |
2107 | writeCallback(iSource->fd, iSource->writeContext); | |
2108 | break; // in case callback removed elements from gEventSources | |
2109 | } | |
2110 | } | |
83fb1e36 | 2111 | } |
8e92c31c | 2112 | |
f0cc3e7b | 2113 | mDNSu32 mDNSPlatformEventContextSize = sizeof (PosixEventSource); |
8e92c31c | 2114 | |
f0cc3e7b A |
2115 | mDNSlocal void requestIOEvents(PosixEventSource *newSource, const char *taskName, |
2116 | mDNSPosixEventCallback callback, void *context, int flag) | |
2117 | { | |
2118 | PosixEventSource **epp = &gEventSources; | |
8e92c31c | 2119 | |
f0cc3e7b A |
2120 | if (newSource->fd >= (int) FD_SETSIZE || newSource->fd < 0) |
2121 | { | |
2122 | LogMsg("requestIOEvents called with fd %d > FD_SETSIZE %d.", newSource->fd, FD_SETSIZE); | |
2123 | assert(0); | |
2124 | } | |
83fb1e36 | 2125 | if (callback == NULL) |
f0cc3e7b A |
2126 | { |
2127 | LogMsg("requestIOEvents called no callback.", newSource->fd, FD_SETSIZE); | |
2128 | assert(0); | |
2129 | } | |
8e92c31c | 2130 | |
f0cc3e7b A |
2131 | // See if this event context is already on the list; if it is, no need to scan the list. |
2132 | if (!(newSource->flags & PosixEventFlag_OnList)) | |
2133 | { | |
2134 | while (*epp) | |
2135 | { | |
2136 | // This should never happen. | |
2137 | if (newSource == *epp) | |
2138 | { | |
2139 | LogMsg("Event context marked not on list but is on list."); | |
2140 | assert(0); | |
2141 | } | |
2142 | epp = &(*epp)->next; | |
2143 | } | |
2144 | if (*epp == NULL) | |
2145 | { | |
2146 | *epp = newSource; | |
2147 | newSource->next = NULL; | |
2148 | newSource->flags = PosixEventFlag_OnList; | |
2149 | } | |
2150 | } | |
8e92c31c | 2151 | |
f0cc3e7b A |
2152 | if (flag & PosixEventFlag_Read) |
2153 | { | |
2154 | newSource->readCallback = callback; | |
2155 | newSource->readContext = context; | |
2156 | newSource->flags |= PosixEventFlag_Read; | |
2157 | newSource->readTaskName = taskName; | |
2158 | } | |
2159 | if (flag & PosixEventFlag_Write) | |
2160 | { | |
2161 | newSource->writeCallback = callback; | |
2162 | newSource->writeContext = context; | |
2163 | newSource->flags |= PosixEventFlag_Write; | |
2164 | newSource->writeTaskName = taskName; | |
2165 | } | |
2166 | } | |
8e92c31c | 2167 | |
f0cc3e7b A |
2168 | mDNSlocal void requestReadEvents(PosixEventSource *eventSource, |
2169 | const char *taskName, mDNSPosixEventCallback callback, void *context) | |
2170 | { | |
2171 | requestIOEvents(eventSource, taskName, callback, context, PosixEventFlag_Read); | |
2172 | } | |
83fb1e36 | 2173 | |
f0cc3e7b A |
2174 | mDNSlocal void requestWriteEvents(PosixEventSource *eventSource, |
2175 | const char *taskName, mDNSPosixEventCallback callback, void *context) | |
2176 | { | |
2177 | requestIOEvents(eventSource, taskName, callback, context, PosixEventFlag_Write); | |
83fb1e36 | 2178 | } |
8e92c31c A |
2179 | |
2180 | // Remove a file descriptor from the set that mDNSPosixRunEventLoopOnce() listens to. | |
f0cc3e7b | 2181 | mDNSlocal mStatus stopReadOrWriteEvents(int fd, mDNSBool freeContext, mDNSBool removeContext, int flags) |
83fb1e36 | 2182 | { |
f0cc3e7b | 2183 | PosixEventSource *iSource, **epp = &gEventSources; |
83fb1e36 | 2184 | |
f0cc3e7b | 2185 | while (*epp) |
83fb1e36 | 2186 | { |
f0cc3e7b | 2187 | iSource = *epp; |
83fb1e36 A |
2188 | if (fd == iSource->fd) |
2189 | { | |
f0cc3e7b A |
2190 | if (flags & PosixEventFlag_Read) |
2191 | { | |
2192 | iSource->readCallback = NULL; | |
2193 | iSource->readContext = NULL; | |
2194 | } | |
2195 | if (flags & PosixEventFlag_Write) | |
2196 | { | |
2197 | iSource->writeCallback = NULL; | |
2198 | iSource->writeContext = NULL; | |
2199 | } | |
2200 | if (iSource->writeCallback == NULL && iSource->readCallback == NULL) | |
2201 | { | |
2202 | if (removeContext || freeContext) | |
2203 | *epp = iSource->next; | |
2204 | if (freeContext) | |
2205 | free(iSource); | |
2206 | } | |
83fb1e36 A |
2207 | return mStatus_NoError; |
2208 | } | |
f0cc3e7b | 2209 | epp = &(*epp)->next; |
83fb1e36 A |
2210 | } |
2211 | return mStatus_NoSuchNameErr; | |
2212 | } | |
8e92c31c | 2213 | |
f0cc3e7b A |
2214 | // Some of the mDNSPosix client code relies on being able to add FDs to the event loop without |
2215 | // providing storage for the event-related info. mDNSPosixAddFDToEventLoop and | |
2216 | // mDNSPosixRemoveFDFromEventLoop handle the event structure storage automatically. | |
2217 | mStatus mDNSPosixAddFDToEventLoop(int fd, mDNSPosixEventCallback callback, void *context) | |
2218 | { | |
2219 | PosixEventSource *newSource; | |
2220 | ||
2221 | newSource = (PosixEventSource*) malloc(sizeof *newSource); | |
2222 | if (NULL == newSource) | |
2223 | return mStatus_NoMemoryErr; | |
2224 | memset(newSource, 0, sizeof *newSource); | |
2225 | newSource->fd = fd; | |
2226 | ||
2227 | requestReadEvents(newSource, "mDNSPosixAddFDToEventLoop", callback, context); | |
2228 | return mStatus_NoError; | |
2229 | } | |
2230 | ||
2231 | mStatus mDNSPosixRemoveFDFromEventLoop(int fd) | |
2232 | { | |
2233 | return stopReadOrWriteEvents(fd, mDNStrue, mDNStrue, PosixEventFlag_Read | PosixEventFlag_Write); | |
2234 | } | |
2235 | ||
8e92c31c | 2236 | // Simply note the received signal in gEventSignals. |
83fb1e36 A |
2237 | mDNSlocal void NoteSignal(int signum) |
2238 | { | |
2239 | sigaddset(&gEventSignals, signum); | |
2240 | } | |
8e92c31c A |
2241 | |
2242 | // Tell the event package to listen for signal and report it in mDNSPosixRunEventLoopOnce(). | |
67c8f8a1 | 2243 | mStatus mDNSPosixListenForSignalInEventLoop(int signum) |
83fb1e36 A |
2244 | { |
2245 | struct sigaction action; | |
2246 | mStatus err; | |
2247 | ||
2248 | mDNSPlatformMemZero(&action, sizeof action); // more portable than member-wise assignment | |
2249 | action.sa_handler = NoteSignal; | |
2250 | err = sigaction(signum, &action, (struct sigaction*) NULL); | |
8e92c31c | 2251 | |
83fb1e36 | 2252 | sigaddset(&gEventSignalSet, signum); |
8e92c31c | 2253 | |
83fb1e36 A |
2254 | return err; |
2255 | } | |
8e92c31c A |
2256 | |
2257 | // Tell the event package to stop listening for signal in mDNSPosixRunEventLoopOnce(). | |
67c8f8a1 | 2258 | mStatus mDNSPosixIgnoreSignalInEventLoop(int signum) |
83fb1e36 A |
2259 | { |
2260 | struct sigaction action; | |
2261 | mStatus err; | |
8e92c31c | 2262 | |
83fb1e36 A |
2263 | mDNSPlatformMemZero(&action, sizeof action); // more portable than member-wise assignment |
2264 | action.sa_handler = SIG_DFL; | |
2265 | err = sigaction(signum, &action, (struct sigaction*) NULL); | |
8e92c31c | 2266 | |
83fb1e36 A |
2267 | sigdelset(&gEventSignalSet, signum); |
2268 | ||
2269 | return err; | |
2270 | } | |
8e92c31c A |
2271 | |
2272 | // Do a single pass through the attendent event sources and dispatch any found to their callbacks. | |
2273 | // Return as soon as internal timeout expires, or a signal we're listening for is received. | |
83fb1e36 A |
2274 | mStatus mDNSPosixRunEventLoopOnce(mDNS *m, const struct timeval *pTimeout, |
2275 | sigset_t *pSignalsReceived, mDNSBool *pDataDispatched) | |
2276 | { | |
f0cc3e7b A |
2277 | fd_set listenFDs; |
2278 | fd_set writeFDs; | |
2279 | int numFDs = 0, numReady; | |
83fb1e36 A |
2280 | struct timeval timeout = *pTimeout; |
2281 | ||
f0cc3e7b A |
2282 | // 1. Set up the fd_set as usual here. |
2283 | // This example client has no file descriptors of its own, | |
2284 | // but a real application would call FD_SET to add them to the set here | |
2285 | FD_ZERO(&listenFDs); | |
2286 | FD_ZERO(&writeFDs); | |
2287 | ||
2288 | // 2. Set up the timeout. | |
2289 | mDNSPosixGetNextDNSEventTime(m, &timeout); | |
83fb1e36 | 2290 | |
f0cc3e7b A |
2291 | // Include the sockets that are listening to the wire in our select() set |
2292 | mDNSPosixGetFDSetForSelect(m, &numFDs, &listenFDs, &writeFDs); | |
2293 | numReady = select(numFDs, &listenFDs, &writeFDs, (fd_set*) NULL, &timeout); | |
83fb1e36 | 2294 | |
83fb1e36 A |
2295 | if (numReady > 0) |
2296 | { | |
f0cc3e7b | 2297 | mDNSPosixProcessFDSet(m, &listenFDs, &writeFDs); |
83fb1e36 A |
2298 | *pDataDispatched = mDNStrue; |
2299 | } | |
f0cc3e7b A |
2300 | else if (numReady < 0) |
2301 | { | |
2302 | if (errno != EINTR) { | |
2303 | // This should never happen, represents a coding error, and is not recoverable, since | |
2304 | // we'll just sit here spinning and never receive another event. The usual reason for | |
2305 | // it to happen is that an FD was closed but not removed from the event list. | |
2306 | LogMsg("select failed: %s", strerror(errno)); | |
2307 | abort(); | |
2308 | } | |
2309 | } | |
83fb1e36 A |
2310 | else |
2311 | *pDataDispatched = mDNSfalse; | |
2312 | ||
2313 | (void) sigprocmask(SIG_BLOCK, &gEventSignalSet, (sigset_t*) NULL); | |
2314 | *pSignalsReceived = gEventSignals; | |
2315 | sigemptyset(&gEventSignals); | |
2316 | (void) sigprocmask(SIG_UNBLOCK, &gEventSignalSet, (sigset_t*) NULL); | |
2317 | ||
2318 | return mStatus_NoError; | |
2319 | } |