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