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