os_abstraction.h

Go to the documentation of this file.
00001 /* $Id: os_abstraction.h 14177 2008-08-25 22:10:47Z smatz $ */
00002 
00009 #ifndef NETWORK_CORE_OS_ABSTRACTION_H
00010 #define NETWORK_CORE_OS_ABSTRACTION_H
00011 
00012 /* Include standard stuff per OS */
00013 
00014 #ifdef ENABLE_NETWORK
00015 
00016 /* Windows stuff */
00017 #if defined(WIN32) || defined(WIN64)
00018 #include <winsock2.h>
00019 #include <ws2tcpip.h>
00020 #include <windows.h>
00021 
00022 #if !(defined(__MINGW32__) || defined(__CYGWIN__))
00023   /* Windows has some different names for some types */
00024   typedef SSIZE_T ssize_t;
00025   typedef int socklen_t;
00026 #endif
00027 
00028 #define GET_LAST_ERROR() WSAGetLastError()
00029 #define EWOULDBLOCK WSAEWOULDBLOCK
00030 /* Windows has some different names for some types */
00031 typedef unsigned long in_addr_t;
00032 #endif /* WIN32 */
00033 
00034 /* UNIX stuff */
00035 #if defined(UNIX) && !defined(__OS2__)
00036 # define SOCKET int
00037 # define INVALID_SOCKET -1
00038 # if !defined(__MORPHOS__) && !defined(__AMIGA__)
00039 #   define ioctlsocket ioctl
00040 # if !defined(BEOS_NET_SERVER)
00041 #   define closesocket close
00042 # endif
00043 #   define GET_LAST_ERROR() (errno)
00044 # endif
00045 /* Need this for FIONREAD on solaris */
00046 # define BSD_COMP
00047 
00048 /* Includes needed for UNIX-like systems */
00049 # include <unistd.h>
00050 # include <sys/ioctl.h>
00051 # if defined(__BEOS__) && defined(BEOS_NET_SERVER)
00052 #   include <be/net/socket.h>
00053 #   include <be/kernel/OS.h> // snooze()
00054 #   include <be/net/netdb.h>
00055     typedef unsigned long in_addr_t;
00056 #   define INADDR_NONE INADDR_BROADCAST
00057 # else
00058 #   include <sys/socket.h>
00059 #   include <netinet/in.h>
00060 #   include <netinet/tcp.h>
00061 #   include <arpa/inet.h>
00062 #   include <net/if.h>
00063 /* According to glibc/NEWS, <ifaddrs.h> appeared in glibc-2.3. */
00064 #   if !defined(__sgi__) && !defined(SUNOS) && !defined(__MORPHOS__) && !defined(__BEOS__) && !defined(__INNOTEK_LIBC__) \
00065        && !(defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)) && !defined(__dietlibc__) && !defined(HPUX)
00066 /* If for any reason ifaddrs.h does not exist on your system, comment out
00067  *   the following two lines and an alternative way will be used to fetch
00068  *   the list of IPs from the system. */
00069 #     include <ifaddrs.h>
00070 #     define HAVE_GETIFADDRS
00071 #   endif
00072 #   if !defined(INADDR_NONE)
00073 #     define INADDR_NONE 0xffffffff
00074 #   endif
00075 #   if defined(__BEOS__) && !defined(BEOS_NET_SERVER)
00076       /* needed on Zeta */
00077 #     include <sys/sockio.h>
00078 #   endif
00079 # endif /* BEOS_NET_SERVER */
00080 
00081 # if !defined(__BEOS__) && defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 1)
00082     typedef uint32_t in_addr_t;
00083 # endif
00084 
00085 # include <errno.h>
00086 # include <sys/time.h>
00087 # include <netdb.h>
00088 #endif // UNIX
00089 
00090 #ifdef __BEOS__
00091   typedef int socklen_t;
00092 #endif
00093 
00094 #if defined(PSP)
00095 # include <sys/socket.h>
00096 # include <netinet/in.h>
00097 # include <arpa/inet.h>
00098 # include <pspnet.h>
00099 # include <pspnet_inet.h>
00100 # include <pspnet_apctl.h>
00101 # include <pspnet_resolver.h>
00102 # include <errno.h>
00103 # include <unistd.h>
00104 # include <sys/select.h>
00105 # include <sys/time.h>
00106 # include <sys/fd_set.h>
00107 
00108 # define TCP_NODELAY 1
00109 # define SO_NONBLOCK 0x1009
00110 # define SOCKET int
00111 # define INVALID_SOCKET -1
00112 # define INADDR_NONE 0xffffffff
00113 # define closesocket close
00114 # define GET_LAST_ERROR() sceNetInetGetErrno()
00115 #endif /* PSP */
00116 
00117 /* OS/2 stuff */
00118 #if defined(__OS2__)
00119 # define SOCKET int
00120 # define INVALID_SOCKET -1
00121 # define ioctlsocket ioctl
00122 # define closesocket close
00123 # define GET_LAST_ERROR() (sock_errno())
00124 
00125 /* Includes needed for OS/2 systems */
00126 # include <types.h>
00127 # include <unistd.h>
00128 # include <sys/ioctl.h>
00129 # include <sys/socket.h>
00130 # include <netinet/in.h>
00131 # include <netinet/tcp.h>
00132 # include <arpa/inet.h>
00133 # include <net/if.h>
00134 # include <errno.h>
00135 # include <sys/time.h>
00136 # include <netdb.h>
00137 # include <nerrno.h>
00138 # define INADDR_NONE 0xffffffff
00139 
00140 typedef int socklen_t;
00141 #if !defined(__INNOTEK_LIBC__)
00142 typedef unsigned long in_addr_t;
00143 #endif /* __INNOTEK_LIBC__ */
00144 #endif /* OS/2 */
00145 
00146 /* MorphOS and Amiga stuff */
00147 #if defined(__MORPHOS__) || defined(__AMIGA__)
00148 # include <exec/types.h>
00149 # include <proto/exec.h>   // required for Open/CloseLibrary()
00150   /* MorphOS defines his network functions with UBYTE arrays while we
00151    *  use char arrays. This gives tons of unneeded warnings */
00152 # define UBYTE char
00153 # if defined(__MORPHOS__)
00154 #   include <sys/filio.h>  // FIO* defines
00155 #   include <sys/sockio.h> // SIO* defines
00156 #   include <netinet/in.h>
00157 # else /* __AMIGA__ */
00158 #   include <proto/socket.h>
00159 # endif
00160 
00161 /* Make the names compatible */
00162 # define closesocket(s) CloseSocket(s)
00163 # define GET_LAST_ERROR() Errno()
00164 # define ioctlsocket(s, request, status) IoctlSocket((LONG)s, (ULONG)request, (char*)status)
00165 # define ioctl ioctlsocket
00166 
00167   typedef unsigned int in_addr_t;
00168   typedef long         socklen_t;
00169   extern struct Library *SocketBase;
00170 
00171 # ifdef __AMIGA__
00172   /* for usleep() implementation */
00173   extern struct Device      *TimerBase;
00174   extern struct MsgPort     *TimerPort;
00175   extern struct timerequest *TimerRequest;
00176 # endif
00177 #endif // __MORPHOS__ || __AMIGA__
00178 
00179 static inline bool SetNonBlocking(SOCKET d)
00180 {
00181 #ifdef WIN32
00182   u_long nonblocking = 1;
00183 #else
00184   int nonblocking = 1;
00185 #endif
00186 #if (defined(__BEOS__) && defined(BEOS_NET_SERVER)) || defined(PSP)
00187   return setsockopt(d, SOL_SOCKET, SO_NONBLOCK, &nonblocking, sizeof(nonblocking)) == 0;
00188 #else
00189   return ioctlsocket(d, FIONBIO, &nonblocking) == 0;
00190 #endif
00191 }
00192 
00193 static inline bool SetNoDelay(SOCKET d)
00194 {
00195   /* XXX should this be done at all? */
00196 #if !defined(BEOS_NET_SERVER) // not implemented on BeOS net_server
00197   int b = 1;
00198   /* The (const char*) cast is needed for windows */
00199   return setsockopt(d, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)) == 0;
00200 #else
00201   return true;
00202 #endif
00203 }
00204 
00205 #endif /* ENABLE_NETWORK */
00206 
00207 #endif /* NETWORK_CORE_OS_ABSTRACTION_H */

Generated on Mon Mar 9 23:33:48 2009 for openttd by  doxygen 1.5.6