1*472cd20dSToomas Soome /* -*- Mode: C; tab-width: 4; c-file-style: "bsd"; c-basic-offset: 4; fill-column: 108; indent-tabs-mode: nil; -*- 2c65ebfc7SToomas Soome * 3c65ebfc7SToomas Soome * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved. 4c65ebfc7SToomas Soome * 5c65ebfc7SToomas Soome * Licensed under the Apache License, Version 2.0 (the "License"); 6c65ebfc7SToomas Soome * you may not use this file except in compliance with the License. 7c65ebfc7SToomas Soome * You may obtain a copy of the License at 8c65ebfc7SToomas Soome * 9c65ebfc7SToomas Soome * http://www.apache.org/licenses/LICENSE-2.0 10c65ebfc7SToomas Soome * 11c65ebfc7SToomas Soome * Unless required by applicable law or agreed to in writing, software 12c65ebfc7SToomas Soome * distributed under the License is distributed on an "AS IS" BASIS, 13c65ebfc7SToomas Soome * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14c65ebfc7SToomas Soome * See the License for the specific language governing permissions and 15c65ebfc7SToomas Soome * limitations under the License. 16c65ebfc7SToomas Soome */ 17c65ebfc7SToomas Soome 18c65ebfc7SToomas Soome #ifndef __mDNSPlatformPosix_h 19c65ebfc7SToomas Soome #define __mDNSPlatformPosix_h 20c65ebfc7SToomas Soome 21c65ebfc7SToomas Soome #include <signal.h> 22c65ebfc7SToomas Soome #include <sys/time.h> 23c65ebfc7SToomas Soome 24c65ebfc7SToomas Soome #ifdef __cplusplus 25c65ebfc7SToomas Soome extern "C" { 26c65ebfc7SToomas Soome #endif 27c65ebfc7SToomas Soome 28c65ebfc7SToomas Soome // PosixNetworkInterface is a record extension of the core NetworkInterfaceInfo 29c65ebfc7SToomas Soome // type that supports extra fields needed by the Posix platform. 30c65ebfc7SToomas Soome // 31c65ebfc7SToomas Soome // IMPORTANT: coreIntf must be the first field in the structure because 32c65ebfc7SToomas Soome // we cast between pointers to the two different types regularly. 33c65ebfc7SToomas Soome 34c65ebfc7SToomas Soome typedef struct PosixNetworkInterface PosixNetworkInterface; 35c65ebfc7SToomas Soome 36c65ebfc7SToomas Soome struct PosixNetworkInterface 37c65ebfc7SToomas Soome { 38c65ebfc7SToomas Soome NetworkInterfaceInfo coreIntf; // MUST be the first element in this structure 39c65ebfc7SToomas Soome mDNSs32 LastSeen; 40c65ebfc7SToomas Soome const char * intfName; 41c65ebfc7SToomas Soome PosixNetworkInterface * aliasIntf; 42c65ebfc7SToomas Soome int index; 43c65ebfc7SToomas Soome int multicastSocket4; 44c65ebfc7SToomas Soome #if HAVE_IPV6 45c65ebfc7SToomas Soome int multicastSocket6; 46c65ebfc7SToomas Soome #endif 47c65ebfc7SToomas Soome }; 48c65ebfc7SToomas Soome 49c65ebfc7SToomas Soome // This is a global because debugf_() needs to be able to check its value 50c65ebfc7SToomas Soome extern int gMDNSPlatformPosixVerboseLevel; 51c65ebfc7SToomas Soome 52c65ebfc7SToomas Soome struct mDNS_PlatformSupport_struct 53c65ebfc7SToomas Soome { 54c65ebfc7SToomas Soome int unicastSocket4; 55c65ebfc7SToomas Soome #if HAVE_IPV6 56c65ebfc7SToomas Soome int unicastSocket6; 57c65ebfc7SToomas Soome #endif 58c65ebfc7SToomas Soome }; 59c65ebfc7SToomas Soome 60*472cd20dSToomas Soome // We keep a list of client-supplied event sources in PosixEventSource records 61*472cd20dSToomas Soome // Add a file descriptor to the set that mDNSPosixRunEventLoopOnce() listens to. 62*472cd20dSToomas Soome #define PosixEventFlag_OnList 1 63*472cd20dSToomas Soome #define PosixEventFlag_Read 2 64*472cd20dSToomas Soome #define PosixEventFlag_Write 4 65*472cd20dSToomas Soome 66*472cd20dSToomas Soome typedef void (*mDNSPosixEventCallback)(int fd, void *context); 67*472cd20dSToomas Soome struct PosixEventSource 68*472cd20dSToomas Soome { 69*472cd20dSToomas Soome struct PosixEventSource *next; 70*472cd20dSToomas Soome mDNSPosixEventCallback readCallback; 71*472cd20dSToomas Soome mDNSPosixEventCallback writeCallback; 72*472cd20dSToomas Soome const char *readTaskName; 73*472cd20dSToomas Soome const char *writeTaskName; 74*472cd20dSToomas Soome void *readContext; 75*472cd20dSToomas Soome void *writeContext; 76*472cd20dSToomas Soome int fd; 77*472cd20dSToomas Soome unsigned flags; 78*472cd20dSToomas Soome }; 79*472cd20dSToomas Soome typedef struct PosixEventSource PosixEventSource; 80*472cd20dSToomas Soome 81*472cd20dSToomas Soome struct TCPSocket_struct 82*472cd20dSToomas Soome { 83*472cd20dSToomas Soome mDNSIPPort port; // MUST BE FIRST FIELD -- mDNSCore expects every TCPSocket_struct to begin with mDNSIPPort 84*472cd20dSToomas Soome TCPSocketFlags flags; // MUST BE SECOND FIELD -- mDNSCore expects every TCPSocket_struct have TCPSocketFlags flags after mDNSIPPort 85*472cd20dSToomas Soome TCPConnectionCallback callback; 86*472cd20dSToomas Soome PosixEventSource events; 87*472cd20dSToomas Soome // SSL context goes here. 88*472cd20dSToomas Soome domainname *hostname; 89*472cd20dSToomas Soome mDNSAddr remoteAddress; 90*472cd20dSToomas Soome mDNSIPPort remotePort; 91*472cd20dSToomas Soome void *context; 92*472cd20dSToomas Soome mDNSBool setup; 93*472cd20dSToomas Soome mDNSBool connected; 94*472cd20dSToomas Soome mStatus err; 95*472cd20dSToomas Soome }; 96*472cd20dSToomas Soome 97*472cd20dSToomas Soome struct TCPListener_struct 98*472cd20dSToomas Soome { 99*472cd20dSToomas Soome TCPAcceptedCallback callback; 100*472cd20dSToomas Soome PosixEventSource events; 101*472cd20dSToomas Soome void *context; 102*472cd20dSToomas Soome mDNSAddr_Type addressType; 103*472cd20dSToomas Soome TCPSocketFlags socketFlags; 104*472cd20dSToomas Soome }; 105*472cd20dSToomas Soome 106c65ebfc7SToomas Soome #define uDNS_SERVERS_FILE "/etc/resolv.conf" 107c65ebfc7SToomas Soome extern int ParseDNSServers(mDNS *m, const char *filePath); 108c65ebfc7SToomas Soome extern mStatus mDNSPlatformPosixRefreshInterfaceList(mDNS *const m); 109c65ebfc7SToomas Soome // See comment in implementation. 110c65ebfc7SToomas Soome 111*472cd20dSToomas Soome // Get the next upcoming mDNS (or DNS) event time as a posix timeval that can be passed to select. 112*472cd20dSToomas Soome // This will only update timeout if the next mDNS event is sooner than the value that was passed. 113*472cd20dSToomas Soome // Therefore, use { FutureTime, 0 } as an initializer if no other timer events are being managed. 114*472cd20dSToomas Soome extern void mDNSPosixGetNextDNSEventTime(mDNS *m, struct timeval *timeout); 115*472cd20dSToomas Soome 116*472cd20dSToomas Soome // Returns all the FDs that the posix I/O event system expects to be passed to select. 117*472cd20dSToomas Soome extern void mDNSPosixGetFDSetForSelect(mDNS *m, int *nfds, fd_set *readfds, fd_set *writefds); 118*472cd20dSToomas Soome 119c65ebfc7SToomas Soome // Call mDNSPosixGetFDSet before calling select(), to update the parameters 120c65ebfc7SToomas Soome // as may be necessary to meet the needs of the mDNSCore code. 121c65ebfc7SToomas Soome // The timeout pointer MUST NOT be NULL. 122c65ebfc7SToomas Soome // Set timeout->tv_sec to FutureTime if you want to have effectively no timeout 123c65ebfc7SToomas Soome // After calling mDNSPosixGetFDSet(), call select(nfds, &readfds, NULL, NULL, &timeout); as usual 124c65ebfc7SToomas Soome // After select() returns, call mDNSPosixProcessFDSet() to let mDNSCore do its work 125*472cd20dSToomas Soome // mDNSPosixGetFDSet simply calls mDNSPosixGetNextDNSEventTime and then mDNSPosixGetFDSetForSelect. 126*472cd20dSToomas Soome extern void mDNSPosixGetFDSet(mDNS *m, int *nfds, fd_set *readfds, fd_set *writefds, struct timeval *timeout); 127c65ebfc7SToomas Soome 128*472cd20dSToomas Soome 129*472cd20dSToomas Soome extern void mDNSPosixProcessFDSet(mDNS *const m, fd_set *readfds, fd_set *writefds); 130c65ebfc7SToomas Soome 131c65ebfc7SToomas Soome extern mStatus mDNSPosixAddFDToEventLoop( int fd, mDNSPosixEventCallback callback, void *context); 132c65ebfc7SToomas Soome extern mStatus mDNSPosixRemoveFDFromEventLoop( int fd); 133c65ebfc7SToomas Soome extern mStatus mDNSPosixListenForSignalInEventLoop( int signum); 134c65ebfc7SToomas Soome extern mStatus mDNSPosixIgnoreSignalInEventLoop( int signum); 135c65ebfc7SToomas Soome extern mStatus mDNSPosixRunEventLoopOnce( mDNS *m, const struct timeval *pTimeout, sigset_t *pSignalsReceived, mDNSBool *pDataDispatched); 136c65ebfc7SToomas Soome 137*472cd20dSToomas Soome extern mStatus mDNSPosixListenForSignalInEventLoop( int signum); 138*472cd20dSToomas Soome extern mStatus mDNSPosixIgnoreSignalInEventLoop( int signum); 139*472cd20dSToomas Soome extern mStatus mDNSPosixRunEventLoopOnce( mDNS *m, const struct timeval *pTimeout, sigset_t *pSignalsReceived, mDNSBool *pDataDispatched); 140*472cd20dSToomas Soome 141c65ebfc7SToomas Soome #ifdef __cplusplus 142c65ebfc7SToomas Soome } 143c65ebfc7SToomas Soome #endif 144c65ebfc7SToomas Soome 145c65ebfc7SToomas Soome #endif 146