2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
#ifndef NETWORK_CORE_OS_ABSTRACTION_H
|
|
|
|
#define NETWORK_CORE_OS_ABSTRACTION_H
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/**
|
|
|
|
* @file os_abstraction.h Network stuff has many things that needs to be
|
|
|
|
* included and/or implemented by default.
|
|
|
|
* All those things are in this file.
|
|
|
|
*/
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Include standard stuff per OS */
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2005-05-15 01:48:39 +02:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Windows stuff */
|
2005-10-03 00:39:56 +02:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
2004-12-23 23:31:46 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2005-10-03 00:39:56 +02:00
|
|
|
#include <windows.h>
|
2004-12-23 21:23:05 +01:00
|
|
|
|
2005-03-14 20:27:19 +01:00
|
|
|
#if !(defined(__MINGW32__) || defined(__CYGWIN__))
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Windows has some different names for some types */
|
2004-12-23 23:31:46 +01:00
|
|
|
typedef SSIZE_T ssize_t;
|
2005-02-07 21:36:41 +01:00
|
|
|
typedef int socklen_t;
|
2004-12-23 23:31:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GET_LAST_ERROR() WSAGetLastError()
|
|
|
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Windows has some different names for some types */
|
2004-12-04 18:54:56 +01:00
|
|
|
typedef unsigned long in_addr_t;
|
2007-01-02 18:34:03 +01:00
|
|
|
#endif /* WIN32 */
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* UNIX stuff */
|
2007-01-05 19:03:30 +01:00
|
|
|
#if defined(UNIX) && !defined(__OS2__)
|
2004-12-04 18:54:56 +01:00
|
|
|
# define SOCKET int
|
|
|
|
# define INVALID_SOCKET -1
|
|
|
|
# if !defined(__MORPHOS__) && !defined(__AMIGA__)
|
|
|
|
# define ioctlsocket ioctl
|
|
|
|
# if !defined(BEOS_NET_SERVER)
|
|
|
|
# define closesocket close
|
|
|
|
# endif
|
|
|
|
# define GET_LAST_ERROR() (errno)
|
|
|
|
# endif
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Need this for FIONREAD on solaris */
|
2004-12-04 18:54:56 +01:00
|
|
|
# define BSD_COMP
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Includes needed for UNIX-like systems */
|
2004-12-04 18:54:56 +01:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# if defined(__BEOS__) && defined(BEOS_NET_SERVER)
|
|
|
|
# include <be/net/socket.h>
|
|
|
|
# include <be/kernel/OS.h> // snooze()
|
|
|
|
# include <be/net/netdb.h>
|
|
|
|
typedef unsigned long in_addr_t;
|
|
|
|
# define INADDR_NONE INADDR_BROADCAST
|
|
|
|
# else
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <netinet/tcp.h>
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
# include <net/if.h>
|
2007-01-02 18:34:03 +01:00
|
|
|
/* According to glibc/NEWS, <ifaddrs.h> appeared in glibc-2.3. */
|
2006-12-10 01:20:26 +01:00
|
|
|
# if !defined(__sgi__) && !defined(SUNOS) && !defined(__MORPHOS__) && !defined(__BEOS__) && !defined(__INNOTEK_LIBC__) \
|
2005-04-13 20:36:29 +02:00
|
|
|
&& !(defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)) && !defined(__dietlibc__)
|
2007-01-02 18:34:03 +01:00
|
|
|
/* If for any reason ifaddrs.h does not exist on your system, comment out
|
|
|
|
* the following two lines and an alternative way will be used to fetch
|
|
|
|
* the list of IPs from the system. */
|
2004-12-18 18:31:02 +01:00
|
|
|
# include <ifaddrs.h>
|
|
|
|
# define HAVE_GETIFADDRS
|
2005-02-28 23:33:31 +01:00
|
|
|
# endif
|
|
|
|
# if defined(SUNOS) || defined(__MORPHOS__) || defined(__BEOS__)
|
2004-12-18 18:31:02 +01:00
|
|
|
# define INADDR_NONE 0xffffffff
|
2005-02-28 23:33:31 +01:00
|
|
|
# endif
|
2005-01-17 14:13:11 +01:00
|
|
|
# if defined(__BEOS__) && !defined(BEOS_NET_SERVER)
|
2007-01-02 18:34:03 +01:00
|
|
|
/* needed on Zeta */
|
2005-01-17 14:13:11 +01:00
|
|
|
# include <sys/sockio.h>
|
|
|
|
# endif
|
2007-01-02 18:34:03 +01:00
|
|
|
# endif /* BEOS_NET_SERVER */
|
2004-12-23 20:23:56 +01:00
|
|
|
|
2005-05-15 01:48:39 +02:00
|
|
|
# if !defined(__BEOS__) && defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 1)
|
2004-12-23 20:23:56 +01:00
|
|
|
typedef uint32_t in_addr_t;
|
2005-02-28 23:33:31 +01:00
|
|
|
# endif
|
2004-12-23 20:23:56 +01:00
|
|
|
|
2004-12-04 18:54:56 +01:00
|
|
|
# include <errno.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <netdb.h>
|
|
|
|
#endif // UNIX
|
|
|
|
|
2005-03-06 13:26:38 +01:00
|
|
|
#ifdef __BEOS__
|
2005-03-06 17:23:32 +01:00
|
|
|
typedef int socklen_t;
|
2005-03-06 13:26:38 +01:00
|
|
|
#endif
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* OS/2 stuff */
|
2004-12-23 15:46:16 +01:00
|
|
|
#if defined(__OS2__)
|
|
|
|
# define SOCKET int
|
|
|
|
# define INVALID_SOCKET -1
|
|
|
|
# define ioctlsocket ioctl
|
|
|
|
# define closesocket close
|
2004-12-27 11:40:04 +01:00
|
|
|
# define GET_LAST_ERROR() (sock_errno())
|
2004-12-23 15:46:16 +01:00
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Includes needed for OS/2 systems */
|
2004-12-23 15:46:16 +01:00
|
|
|
# include <types.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <netinet/tcp.h>
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
# include <net/if.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <netdb.h>
|
|
|
|
# include <nerrno.h>
|
2004-12-27 11:40:04 +01:00
|
|
|
# define INADDR_NONE 0xffffffff
|
2004-12-23 15:46:16 +01:00
|
|
|
|
2005-03-15 13:21:59 +01:00
|
|
|
typedef int socklen_t;
|
2006-12-10 01:20:26 +01:00
|
|
|
#if !defined(__INNOTEK_LIBC__)
|
2004-12-23 15:46:16 +01:00
|
|
|
typedef unsigned long in_addr_t;
|
2006-12-10 01:20:26 +01:00
|
|
|
#endif /* __INNOTEK_LIBC__ */
|
2007-01-02 18:34:03 +01:00
|
|
|
#endif /* OS/2 */
|
2004-12-23 15:46:16 +01:00
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* MorphOS and Amiga stuff */
|
2004-12-04 18:54:56 +01:00
|
|
|
#if defined(__MORPHOS__) || defined(__AMIGA__)
|
|
|
|
# include <exec/types.h>
|
2007-01-02 18:34:03 +01:00
|
|
|
# include <proto/exec.h> // required for Open/CloseLibrary()
|
2007-01-26 09:27:59 +01:00
|
|
|
/* MorphOS defines his network functions with UBYTE arrays while we
|
|
|
|
* use char arrays. This gives tons of unneeded warnings */
|
|
|
|
# define UBYTE char
|
2004-12-04 18:54:56 +01:00
|
|
|
# if defined(__MORPHOS__)
|
2007-01-02 18:34:03 +01:00
|
|
|
# include <sys/filio.h> // FIO* defines
|
|
|
|
# include <sys/sockio.h> // SIO* defines
|
2005-02-08 16:42:28 +01:00
|
|
|
# include <netinet/in.h>
|
2007-01-02 18:34:03 +01:00
|
|
|
# else /* __AMIGA__ */
|
2004-12-04 18:54:56 +01:00
|
|
|
# include <proto/socket.h>
|
|
|
|
# endif
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
/* Make the names compatible */
|
2004-12-04 18:54:56 +01:00
|
|
|
# define closesocket(s) CloseSocket(s)
|
|
|
|
# define GET_LAST_ERROR() Errno()
|
|
|
|
# define ioctlsocket(s,request,status) IoctlSocket((LONG)s,(ULONG)request,(char*)status)
|
2004-12-22 22:12:36 +01:00
|
|
|
# define ioctl ioctlsocket
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2004-12-22 22:12:36 +01:00
|
|
|
typedef unsigned int in_addr_t;
|
2005-02-08 16:42:28 +01:00
|
|
|
typedef long socklen_t;
|
2004-12-22 22:12:36 +01:00
|
|
|
extern struct Library *SocketBase;
|
|
|
|
|
|
|
|
# ifdef __AMIGA__
|
2007-01-02 18:34:03 +01:00
|
|
|
/* for usleep() implementation */
|
2004-12-22 22:12:36 +01:00
|
|
|
extern struct Device *TimerBase;
|
|
|
|
extern struct MsgPort *TimerPort;
|
|
|
|
extern struct timerequest *TimerRequest;
|
|
|
|
# endif
|
2004-12-04 18:54:56 +01:00
|
|
|
#endif // __MORPHOS__ || __AMIGA__
|
|
|
|
|
2007-02-11 14:39:23 +01:00
|
|
|
static inline bool SetNonBlocking(SOCKET d)
|
2005-02-07 10:56:16 +01:00
|
|
|
{
|
2007-01-02 18:34:03 +01:00
|
|
|
#ifdef WIN32
|
2005-03-09 12:49:34 +01:00
|
|
|
u_long nonblocking = 1;
|
2007-01-02 18:34:03 +01:00
|
|
|
#else
|
2005-02-07 10:56:16 +01:00
|
|
|
int nonblocking = 1;
|
2007-01-02 18:34:03 +01:00
|
|
|
#endif
|
|
|
|
#if defined(__BEOS__) && defined(BEOS_NET_SERVER)
|
2005-02-07 10:56:16 +01:00
|
|
|
return setsockopt(d, SOL_SOCKET, SO_NONBLOCK, &nonblocking, sizeof(nonblocking)) == 0;
|
2007-01-02 18:34:03 +01:00
|
|
|
#else
|
2005-02-07 10:56:16 +01:00
|
|
|
return ioctlsocket(d, FIONBIO, &nonblocking) == 0;
|
2007-01-02 18:34:03 +01:00
|
|
|
#endif
|
2005-02-07 10:56:16 +01:00
|
|
|
}
|
|
|
|
|
2007-02-11 14:39:23 +01:00
|
|
|
static inline bool SetNoDelay(SOCKET d)
|
2005-02-07 10:56:16 +01:00
|
|
|
{
|
2007-01-02 18:34:03 +01:00
|
|
|
/* XXX should this be done at all? */
|
|
|
|
#if !defined(BEOS_NET_SERVER) // not implemented on BeOS net_server
|
2005-02-07 10:56:16 +01:00
|
|
|
int b = 1;
|
2007-01-02 18:34:03 +01:00
|
|
|
/* The (const char*) cast is needed for windows */
|
2005-02-07 10:56:16 +01:00
|
|
|
return setsockopt(d, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)) == 0;
|
2007-01-02 18:34:03 +01:00
|
|
|
#else
|
2005-02-07 10:56:16 +01:00
|
|
|
return true;
|
2007-01-02 18:34:03 +01:00
|
|
|
#endif
|
2005-02-07 10:56:16 +01:00
|
|
|
}
|
|
|
|
|
2007-01-02 22:27:28 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
/* Looks like sys/socket.h uses a name we got in macros.h */
|
|
|
|
#undef ALIGN
|
|
|
|
#endif
|
|
|
|
|
2005-05-15 01:48:39 +02:00
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2007-01-02 18:34:03 +01:00
|
|
|
#endif /* NETWORK_CORE_OS_ABSTRACTION_H */
|