network/core/os_abstraction.h
branchcustombridgeheads
changeset 5642 bfa6074e2833
equal deleted inserted replaced
5641:d4d00a16ef26 5642:bfa6074e2833
       
     1 /* $Id$ */
       
     2 
       
     3 #ifndef NETWORK_CORE_OS_ABSTRACTION_H
       
     4 #define NETWORK_CORE_OS_ABSTRACTION_H
       
     5 
       
     6 /**
       
     7  * @file os_abstraction.h Network stuff has many things that needs to be
       
     8  *                        included and/or implemented by default.
       
     9  *                        All those things are in this file.
       
    10  */
       
    11 
       
    12 /* Include standard stuff per OS */
       
    13 
       
    14 #ifdef ENABLE_NETWORK
       
    15 
       
    16 /* Windows stuff */
       
    17 #if defined(WIN32) || defined(WIN64)
       
    18 #include <winsock2.h>
       
    19 #include <ws2tcpip.h>
       
    20 #include <windows.h>
       
    21 
       
    22 #if !(defined(__MINGW32__) || defined(__CYGWIN__))
       
    23 	/* Windows has some different names for some types */
       
    24 	typedef SSIZE_T ssize_t;
       
    25 	typedef int socklen_t;
       
    26 #endif
       
    27 
       
    28 #define GET_LAST_ERROR() WSAGetLastError()
       
    29 #define EWOULDBLOCK WSAEWOULDBLOCK
       
    30 /* Windows has some different names for some types */
       
    31 typedef unsigned long in_addr_t;
       
    32 #endif /* WIN32 */
       
    33 
       
    34 /* UNIX stuff */
       
    35 #if defined(UNIX)
       
    36 #	define SOCKET int
       
    37 #	define INVALID_SOCKET -1
       
    38 #	if !defined(__MORPHOS__) && !defined(__AMIGA__)
       
    39 #		define ioctlsocket ioctl
       
    40 #	if !defined(BEOS_NET_SERVER)
       
    41 #		define closesocket close
       
    42 #	endif
       
    43 #		define GET_LAST_ERROR() (errno)
       
    44 #	endif
       
    45 /* Need this for FIONREAD on solaris */
       
    46 #	define BSD_COMP
       
    47 
       
    48 /* Includes needed for UNIX-like systems */
       
    49 #	include <unistd.h>
       
    50 #	include <sys/ioctl.h>
       
    51 #	if defined(__BEOS__) && defined(BEOS_NET_SERVER)
       
    52 #		include <be/net/socket.h>
       
    53 #		include <be/kernel/OS.h> // snooze()
       
    54 #		include <be/net/netdb.h>
       
    55 		typedef unsigned long in_addr_t;
       
    56 #		define INADDR_NONE INADDR_BROADCAST
       
    57 #	else
       
    58 #		include <sys/socket.h>
       
    59 #		include <netinet/in.h>
       
    60 #		include <netinet/tcp.h>
       
    61 #		include <arpa/inet.h>
       
    62 #		include <net/if.h>
       
    63 /* According to glibc/NEWS, <ifaddrs.h> appeared in glibc-2.3. */
       
    64 #		if !defined(__sgi__) && !defined(SUNOS) && !defined(__MORPHOS__) && !defined(__BEOS__) && !defined(__INNOTEK_LIBC__) \
       
    65 		   && !(defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)) && !defined(__dietlibc__)
       
    66 /* If for any reason ifaddrs.h does not exist on your system, comment out
       
    67  *   the following two lines and an alternative way will be used to fetch
       
    68  *   the list of IPs from the system. */
       
    69 #			include <ifaddrs.h>
       
    70 #			define HAVE_GETIFADDRS
       
    71 #		endif
       
    72 #		if defined(SUNOS) || defined(__MORPHOS__) || defined(__BEOS__)
       
    73 #			define INADDR_NONE 0xffffffff
       
    74 #		endif
       
    75 #		if defined(__BEOS__) && !defined(BEOS_NET_SERVER)
       
    76 			/* needed on Zeta */
       
    77 #			include <sys/sockio.h>
       
    78 #		endif
       
    79 #	endif /* BEOS_NET_SERVER */
       
    80 
       
    81 #	if !defined(__BEOS__) && defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 1)
       
    82 		typedef uint32_t in_addr_t;
       
    83 #	endif
       
    84 
       
    85 #	include <errno.h>
       
    86 #	include <sys/time.h>
       
    87 #	include <netdb.h>
       
    88 #endif // UNIX
       
    89 
       
    90 #ifdef __BEOS__
       
    91 	typedef int socklen_t;
       
    92 #endif
       
    93 
       
    94 /* OS/2 stuff */
       
    95 #if defined(__OS2__)
       
    96 #	define SOCKET int
       
    97 #	define INVALID_SOCKET -1
       
    98 #	define ioctlsocket ioctl
       
    99 #	define closesocket close
       
   100 #	define GET_LAST_ERROR() (sock_errno())
       
   101 
       
   102 /* Includes needed for OS/2 systems */
       
   103 #	include <types.h>
       
   104 #	include <unistd.h>
       
   105 #	include <sys/ioctl.h>
       
   106 #	include <sys/socket.h>
       
   107 #	include <netinet/in.h>
       
   108 #	include <netinet/tcp.h>
       
   109 #	include <arpa/inet.h>
       
   110 #	include <net/if.h>
       
   111 #	include <errno.h>
       
   112 #	include <sys/time.h>
       
   113 #	include <netdb.h>
       
   114 #	include <nerrno.h>
       
   115 #	define INADDR_NONE 0xffffffff
       
   116 
       
   117 typedef int socklen_t;
       
   118 #if !defined(__INNOTEK_LIBC__)
       
   119 typedef unsigned long in_addr_t;
       
   120 #endif /* __INNOTEK_LIBC__ */
       
   121 #endif /* OS/2 */
       
   122 
       
   123 /* MorphOS and Amiga stuff */
       
   124 #if defined(__MORPHOS__) || defined(__AMIGA__)
       
   125 #	include <exec/types.h>
       
   126 #	include <proto/exec.h>   // required for Open/CloseLibrary()
       
   127 #	if defined(__MORPHOS__)
       
   128 #		include <sys/filio.h>  // FIO* defines
       
   129 #		include <sys/sockio.h> // SIO* defines
       
   130 #		include <netinet/in.h>
       
   131 #	else /* __AMIGA__ */
       
   132 #		include	<proto/socket.h>
       
   133 #	endif
       
   134 
       
   135 /* Make the names compatible */
       
   136 #	define closesocket(s) CloseSocket(s)
       
   137 #	define GET_LAST_ERROR() Errno()
       
   138 #	define ioctlsocket(s,request,status) IoctlSocket((LONG)s,(ULONG)request,(char*)status)
       
   139 #	define ioctl ioctlsocket
       
   140 
       
   141 	typedef unsigned int in_addr_t;
       
   142 	typedef long         socklen_t;
       
   143 	extern struct Library *SocketBase;
       
   144 
       
   145 #	ifdef __AMIGA__
       
   146 	/* for usleep() implementation */
       
   147 	extern struct Device      *TimerBase;
       
   148 	extern struct MsgPort     *TimerPort;
       
   149 	extern struct timerequest *TimerRequest;
       
   150 #	endif
       
   151 #endif // __MORPHOS__ || __AMIGA__
       
   152 
       
   153 static inline bool SetNonBlocking(int d)
       
   154 {
       
   155 #ifdef WIN32
       
   156 	u_long nonblocking = 1;
       
   157 #else
       
   158 	int nonblocking = 1;
       
   159 #endif
       
   160 #if defined(__BEOS__) && defined(BEOS_NET_SERVER)
       
   161 	return setsockopt(d, SOL_SOCKET, SO_NONBLOCK, &nonblocking, sizeof(nonblocking)) == 0;
       
   162 #else
       
   163 	return ioctlsocket(d, FIONBIO, &nonblocking) == 0;
       
   164 #endif
       
   165 }
       
   166 
       
   167 static inline bool SetNoDelay(int d)
       
   168 {
       
   169 	/* XXX should this be done at all? */
       
   170 #if !defined(BEOS_NET_SERVER) // not implemented on BeOS net_server
       
   171 	int b = 1;
       
   172 	/* The (const char*) cast is needed for windows */
       
   173 	return setsockopt(d, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)) == 0;
       
   174 #else
       
   175 	return true;
       
   176 #endif
       
   177 }
       
   178 
       
   179 #endif /* ENABLE_NETWORK */
       
   180 
       
   181 #endif /* NETWORK_CORE_OS_ABSTRACTION_H */