os_abstraction.h
Go to the documentation of this file.00001
00002
00009 #ifndef NETWORK_CORE_OS_ABSTRACTION_H
00010 #define NETWORK_CORE_OS_ABSTRACTION_H
00011
00012
00013
00014 #ifdef ENABLE_NETWORK
00015
00016
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
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
00031 typedef unsigned long in_addr_t;
00032 #endif
00033
00034
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
00046 # define BSD_COMP
00047
00048
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>
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
00064 # if !defined(__sgi__) && !defined(SUNOS) && !defined(__MORPHOS__) && !defined(__BEOS__) && !defined(__INNOTEK_LIBC__) \
00065 && !(defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)) && !defined(__dietlibc__)
00066
00067
00068
00069 # include <ifaddrs.h>
00070 # define HAVE_GETIFADDRS
00071 # endif
00072 # if defined(SUNOS) || defined(__MORPHOS__) || defined(__BEOS__)
00073 # define INADDR_NONE 0xffffffff
00074 # endif
00075 # if defined(__BEOS__) && !defined(BEOS_NET_SERVER)
00076
00077 # include <sys/sockio.h>
00078 # endif
00079 # endif
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
00116
00117
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
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
00144 #endif
00145
00146
00147 #if defined(__MORPHOS__) || defined(__AMIGA__)
00148 # include <exec/types.h>
00149 # include <proto/exec.h>
00150
00151
00152 # define UBYTE char
00153 # if defined(__MORPHOS__)
00154 # include <sys/filio.h>
00155 # include <sys/sockio.h>
00156 # include <netinet/in.h>
00157 # else
00158 # include <proto/socket.h>
00159 # endif
00160
00161
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
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
00196 #if !defined(BEOS_NET_SERVER) // not implemented on BeOS net_server
00197 int b = 1;
00198
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
00206
00207 #endif