1 /* -*- Mode: C; tab-width: 4; c-file-style: "bsd"; c-basic-offset: 4; fill-column: 108; indent-tabs-mode: nil; -*- 2 * 3 * Copyright (c) 2002-2021 Apple Computer, Inc. All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * https://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 #ifndef __mDNSPlatformPosix_h 19 #define __mDNSPlatformPosix_h 20 21 #include <signal.h> 22 #include <sys/time.h> 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 // PosixNetworkInterface is a record extension of the core NetworkInterfaceInfo 29 // type that supports extra fields needed by the Posix platform. 30 // 31 // IMPORTANT: coreIntf must be the first field in the structure because 32 // we cast between pointers to the two different types regularly. 33 34 typedef struct PosixNetworkInterface PosixNetworkInterface; 35 36 struct PosixNetworkInterface 37 { 38 NetworkInterfaceInfo coreIntf; // MUST be the first element in this structure 39 mDNSs32 LastSeen; 40 char * intfName; 41 PosixNetworkInterface * aliasIntf; 42 int index; 43 int multicastSocket4; 44 #if HAVE_IPV6 45 int multicastSocket6; 46 #endif 47 }; 48 49 // This is a global because debugf_() needs to be able to check its value 50 extern int gMDNSPlatformPosixVerboseLevel; 51 52 struct mDNS_PlatformSupport_struct 53 { 54 int unicastSocket4; 55 #if HAVE_IPV6 56 int unicastSocket6; 57 #endif 58 }; 59 60 // We keep a list of client-supplied event sources in PosixEventSource records 61 // Add a file descriptor to the set that mDNSPosixRunEventLoopOnce() listens to. 62 #define PosixEventFlag_OnList 1 63 #define PosixEventFlag_Read 2 64 #define PosixEventFlag_Write 4 65 66 typedef void (*mDNSPosixEventCallback)(int fd, void *context); 67 struct PosixEventSource 68 { 69 struct PosixEventSource *next; 70 mDNSPosixEventCallback readCallback; 71 mDNSPosixEventCallback writeCallback; 72 const char *readTaskName; 73 const char *writeTaskName; 74 void *readContext; 75 void *writeContext; 76 int fd; 77 unsigned flags; 78 }; 79 typedef struct PosixEventSource PosixEventSource; 80 81 struct UDPSocket_struct 82 { 83 mDNSIPPort port; 84 PosixEventSource events; 85 mDNSAddr remoteAddress; 86 mDNSIPPort remotePort; 87 mDNSBool setup; 88 mDNSBool randomizePort; 89 }; 90 91 struct TCPSocket_struct 92 { 93 mDNSIPPort port; // MUST BE FIRST FIELD -- mDNSCore expects every TCPSocket_struct to begin with mDNSIPPort 94 TCPSocketFlags flags; // MUST BE SECOND FIELD -- mDNSCore expects every TCPSocket_struct have TCPSocketFlags flags after mDNSIPPort 95 TCPConnectionCallback callback; 96 PosixEventSource events; 97 TLSContext *tls; 98 domainname *hostname; 99 mDNSAddr remoteAddress; 100 mDNSIPPort remotePort; 101 void *context; 102 mDNSBool setup; 103 mDNSBool connected; 104 mStatus err; 105 }; 106 107 struct TCPListener_struct 108 { 109 TCPAcceptedCallback callback; 110 PosixEventSource events; 111 TLSServerContext *tls; 112 void *context; 113 mDNSAddr_Type addressType; 114 TCPSocketFlags socketFlags; 115 }; 116 117 #define uDNS_SERVERS_FILE "/etc/resolv.conf" 118 extern int ParseDNSServers(mDNS *m, const char *filePath); 119 extern mStatus mDNSPlatformPosixRefreshInterfaceList(mDNS *const m); 120 // See comment in implementation. 121 122 // Call mDNSPosixGetFDSet before calling select(), to update the parameters 123 // as may be necessary to meet the needs of the mDNSCore code. 124 // The timeout pointer MUST NOT be NULL. 125 // Set timeout->tv_sec to FutureTime if you want to have effectively no timeout 126 // After calling mDNSPosixGetFDSet(), call select(nfds, &readfds, NULL, NULL, &timeout); as usual 127 // After select() returns, call mDNSPosixProcessFDSet() to let mDNSCore do its work 128 extern void mDNSPosixGetFDSet(mDNS *m, int *nfds, fd_set *readfds, fd_set *writefds, struct timeval *timeout); 129 extern void mDNSPosixProcessFDSet(mDNS *const m, fd_set *readfds, fd_set *writefds); 130 131 extern mStatus mDNSPosixAddFDToEventLoop( int fd, mDNSPosixEventCallback callback, void *context); 132 extern mStatus mDNSPosixRemoveFDFromEventLoop( int fd); 133 extern mStatus mDNSPosixListenForSignalInEventLoop( int signum); 134 extern mStatus mDNSPosixIgnoreSignalInEventLoop( int signum); 135 extern mStatus mDNSPosixRunEventLoopOnce( mDNS *m, const struct timeval *pTimeout, sigset_t *pSignalsReceived, mDNSBool *pDataDispatched); 136 137 extern mStatus mDNSPosixListenForSignalInEventLoop( int signum); 138 extern mStatus mDNSPosixIgnoreSignalInEventLoop( int signum); 139 extern mStatus mDNSPosixRunEventLoopOnce( mDNS *m, const struct timeval *pTimeout, sigset_t *pSignalsReceived, mDNSBool *pDataDispatched); 140 141 // From the TLS shim: 142 extern mDNSBool mDNSPosixTLSInit(void); 143 extern void mDNSPosixTLSContextFree(TLSContext *tls); 144 extern TLSContext *mDNSPosixTLSClientStateCreate(TCPSocket *sock); 145 extern mDNSBool mDNSPosixTLSStart(TCPSocket *sock); 146 extern TLSContext *PosixTLSAccept(TCPListener *listenContext); 147 extern int mDNSPosixTLSRead(TCPSocket *sock, void *buf, unsigned long buflen, mDNSBool *closed); 148 extern int mDNSPosixTLSWrite(TCPSocket *sock, const void *buf, unsigned long buflen); 149 150 #ifdef __cplusplus 151 } 152 #endif 153 154 #endif 155