2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
#ifndef CNETWORK_H
|
|
|
|
#define CNETWORK_H
|
|
|
|
|
|
|
|
#include "BasicTypes.h"
|
|
|
|
|
2002-10-16 22:01:50 +00:00
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_POLL
|
|
|
|
# include <sys/poll.h>
|
|
|
|
#endif
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
|
|
|
// declare no functions in winsock2
|
|
|
|
# define INCL_WINSOCK_API_PROTOTYPES 0
|
|
|
|
# define INCL_WINSOCK_API_TYPEDEFS 0
|
|
|
|
# include <winsock2.h>
|
2001-11-19 00:33:36 +00:00
|
|
|
typedef int ssize_t;
|
|
|
|
#else
|
2002-06-19 11:23:49 +00:00
|
|
|
# undef FAR
|
|
|
|
# undef PASCAL
|
|
|
|
# define FAR
|
|
|
|
# define PASCAL
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if UNIX_LIKE
|
2002-10-16 22:01:50 +00:00
|
|
|
# include <netdb.h>
|
|
|
|
# include <errno.h>
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|
|
|
|
|
2002-07-28 19:06:52 +00:00
|
|
|
//! Networking functions
|
2001-11-19 00:33:36 +00:00
|
|
|
class CNetwork {
|
2002-04-29 14:40:01 +00:00
|
|
|
public:
|
2002-07-28 19:06:52 +00:00
|
|
|
// platform dependent types
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
typedef SOCKET Socket;
|
|
|
|
typedef struct sockaddr Address;
|
|
|
|
typedef int AddressLength;
|
2002-10-16 22:01:50 +00:00
|
|
|
#elif UNIX_LIKE
|
|
|
|
typedef int Socket;
|
|
|
|
typedef struct sockaddr Address;
|
|
|
|
typedef socklen_t AddressLength;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WINDOWS_LIKE || !HAVE_POLL
|
2002-05-23 14:04:35 +00:00
|
|
|
class PollEntry {
|
|
|
|
public:
|
2001-11-19 00:33:36 +00:00
|
|
|
Socket fd;
|
|
|
|
short events;
|
|
|
|
short revents;
|
|
|
|
};
|
|
|
|
enum {
|
2002-10-16 22:01:50 +00:00
|
|
|
kPOLLIN = 1,
|
|
|
|
kPOLLOUT = 2,
|
|
|
|
kPOLLERR = 4,
|
2001-11-19 00:33:36 +00:00
|
|
|
kPOLLNVAL = 8
|
|
|
|
};
|
2002-10-16 22:01:50 +00:00
|
|
|
#else
|
2001-11-19 00:33:36 +00:00
|
|
|
typedef struct pollfd PollEntry;
|
|
|
|
enum {
|
2002-10-16 22:01:50 +00:00
|
|
|
kPOLLIN = POLLIN,
|
|
|
|
kPOLLOUT = POLLOUT,
|
|
|
|
kPOLLERR = POLLERR,
|
2001-11-19 00:33:36 +00:00
|
|
|
kPOLLNVAL = POLLNVAL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2002-07-28 19:06:52 +00:00
|
|
|
//! @name manipulators
|
|
|
|
//@{
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-07-28 19:06:52 +00:00
|
|
|
//! Initialize network subsystem
|
|
|
|
/*!
|
|
|
|
This \b must be called before any other calls to the network subsystem.
|
|
|
|
*/
|
2001-11-19 00:33:36 +00:00
|
|
|
static void init();
|
2002-07-28 19:06:52 +00:00
|
|
|
|
|
|
|
//! Clean up network subsystem
|
|
|
|
/*!
|
|
|
|
This should be called when the network subsystem is no longer needed
|
|
|
|
and no longer in use.
|
|
|
|
*/
|
2001-11-19 00:33:36 +00:00
|
|
|
static void cleanup();
|
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
// byte swapping functions
|
2002-07-28 19:06:52 +00:00
|
|
|
//! Swap bytes to network order
|
2002-06-09 16:53:25 +00:00
|
|
|
static UInt32 swaphtonl(UInt32 hostlong);
|
2002-07-28 19:06:52 +00:00
|
|
|
//! Swap bytes to network order
|
2002-06-09 16:53:25 +00:00
|
|
|
static UInt16 swaphtons(UInt16 hostshort);
|
2002-07-28 19:06:52 +00:00
|
|
|
//! Swap bytes to host order
|
2002-06-09 16:53:25 +00:00
|
|
|
static UInt32 swapntohl(UInt32 netlong);
|
2002-07-28 19:06:52 +00:00
|
|
|
//! Swap bytes to host order
|
2002-06-09 16:53:25 +00:00
|
|
|
static UInt16 swapntohs(UInt16 netshort);
|
|
|
|
|
2002-07-28 19:06:52 +00:00
|
|
|
//@}
|
|
|
|
//! @name constants
|
|
|
|
//@{
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-07-28 19:06:52 +00:00
|
|
|
//! The error type
|
2001-11-19 00:33:36 +00:00
|
|
|
static const int Error;
|
2002-07-28 19:06:52 +00:00
|
|
|
//! The non-socket
|
2001-11-19 00:33:36 +00:00
|
|
|
static const Socket Null;
|
|
|
|
|
|
|
|
// getsockerror() constants
|
|
|
|
enum {
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
kEADDRINUSE = WSAEADDRINUSE,
|
2002-06-21 16:19:08 +00:00
|
|
|
kECONNECTING = WSAEWOULDBLOCK,
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
kEADDRINUSE = EADDRINUSE,
|
2002-06-21 16:19:08 +00:00
|
|
|
kECONNECTING = EINPROGRESS,
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|
|
|
|
kNone = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
// gethosterror() constants
|
|
|
|
enum {
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
kHOST_NOT_FOUND = WSAHOST_NOT_FOUND,
|
|
|
|
kNO_DATA = WSANO_DATA,
|
|
|
|
kNO_RECOVERY = WSANO_RECOVERY,
|
|
|
|
kTRY_AGAIN = WSATRY_AGAIN,
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
kHOST_NOT_FOUND = HOST_NOT_FOUND,
|
|
|
|
kNO_DATA = NO_DATA,
|
|
|
|
kNO_RECOVERY = NO_RECOVERY,
|
|
|
|
kTRY_AGAIN = TRY_AGAIN,
|
|
|
|
#endif
|
|
|
|
kHNone = 0
|
|
|
|
};
|
|
|
|
|
2002-07-28 19:06:52 +00:00
|
|
|
//@}
|
|
|
|
|
2002-06-09 16:53:25 +00:00
|
|
|
// socket interface (only available after init())
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
static Socket (PASCAL FAR *accept)(Socket s, Address FAR *addr, AddressLength FAR *addrlen);
|
|
|
|
static int (PASCAL FAR *bind)(Socket s, const Address FAR *addr, AddressLength namelen);
|
|
|
|
static int (PASCAL FAR *close)(Socket s);
|
|
|
|
static int (PASCAL FAR *connect)(Socket s, const Address FAR *name, AddressLength namelen);
|
2002-06-21 17:54:22 +00:00
|
|
|
static int (PASCAL FAR *ioctl)(Socket s, int cmd, void FAR *);
|
2001-11-19 00:33:36 +00:00
|
|
|
static int (PASCAL FAR *getpeername)(Socket s, Address FAR *name, AddressLength FAR * namelen);
|
|
|
|
static int (PASCAL FAR *getsockname)(Socket s, Address FAR *name, AddressLength FAR * namelen);
|
|
|
|
static int (PASCAL FAR *getsockopt)(Socket s, int level, int optname, void FAR * optval, AddressLength FAR *optlen);
|
|
|
|
static unsigned long (PASCAL FAR *inet_addr)(const char FAR * cp);
|
|
|
|
static char FAR * (PASCAL FAR *inet_ntoa)(struct in_addr in);
|
|
|
|
static int (PASCAL FAR *listen)(Socket s, int backlog);
|
|
|
|
static ssize_t (PASCAL FAR *read)(Socket s, void FAR * buf, size_t len);
|
|
|
|
static ssize_t (PASCAL FAR *recv)(Socket s, void FAR * buf, size_t len, int flags);
|
|
|
|
static ssize_t (PASCAL FAR *recvfrom)(Socket s, void FAR * buf, size_t len, int flags, Address FAR *from, AddressLength FAR * fromlen);
|
|
|
|
static int (PASCAL FAR *poll)(PollEntry[], int nfds, int timeout);
|
|
|
|
static ssize_t (PASCAL FAR *send)(Socket s, const void FAR * buf, size_t len, int flags);
|
|
|
|
static ssize_t (PASCAL FAR *sendto)(Socket s, const void FAR * buf, size_t len, int flags, const Address FAR *to, AddressLength tolen);
|
|
|
|
static int (PASCAL FAR *setsockopt)(Socket s, int level, int optname, const void FAR * optval, AddressLength optlen);
|
|
|
|
static int (PASCAL FAR *shutdown)(Socket s, int how);
|
|
|
|
static Socket (PASCAL FAR *socket)(int af, int type, int protocol);
|
|
|
|
static ssize_t (PASCAL FAR *write)(Socket s, const void FAR * buf, size_t len);
|
|
|
|
static struct hostent FAR * (PASCAL FAR *gethostbyaddr)(const char FAR * addr, int len, int type);
|
|
|
|
static struct hostent FAR * (PASCAL FAR *gethostbyname)(const char FAR * name);
|
|
|
|
static int (PASCAL FAR *gethostname)(char FAR * name, int namelen);
|
|
|
|
static struct servent FAR * (PASCAL FAR *getservbyport)(int port, const char FAR * proto);
|
|
|
|
static struct servent FAR * (PASCAL FAR *getservbyname)(const char FAR * name, const char FAR * proto);
|
|
|
|
static struct protoent FAR * (PASCAL FAR *getprotobynumber)(int proto);
|
|
|
|
static struct protoent FAR * (PASCAL FAR *getprotobyname)(const char FAR * name);
|
|
|
|
static int (PASCAL FAR *getsockerror)(void);
|
|
|
|
static int (PASCAL FAR *gethosterror)(void);
|
|
|
|
|
2002-06-21 16:19:08 +00:00
|
|
|
// convenience functions (only available after init())
|
|
|
|
|
2002-07-28 19:06:52 +00:00
|
|
|
//! Set socket to (non-)blocking operation
|
2002-06-21 16:19:08 +00:00
|
|
|
static int (PASCAL FAR *setblocking)(CNetwork::Socket s, bool blocking);
|
|
|
|
|
2002-10-16 22:01:50 +00:00
|
|
|
//! Turn Nagle algorithm on or off on socket
|
|
|
|
/*!
|
|
|
|
Set socket to send messages immediately (true) or to collect small
|
|
|
|
messages into one packet (false).
|
|
|
|
*/
|
|
|
|
static int (PASCAL FAR *setnodelay)(CNetwork::Socket s, bool nodelay);
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
private:
|
2002-10-16 22:01:50 +00:00
|
|
|
#if WINDOWS_LIKE
|
|
|
|
#define SELECT_TYPE_ARG1 int
|
|
|
|
#define SELECT_TYPE_ARG234 (fd_set *)
|
|
|
|
#define SELECT_TYPE_ARG5 (struct timeval *)
|
2001-11-19 00:33:36 +00:00
|
|
|
static void init2(HMODULE);
|
|
|
|
static ssize_t PASCAL FAR read2(Socket s, void FAR * buf, size_t len);
|
|
|
|
static ssize_t PASCAL FAR write2(Socket s, const void FAR * buf, size_t len);
|
2002-06-21 16:19:08 +00:00
|
|
|
static int PASCAL FAR setblocking2(CNetwork::Socket s, bool blocking);
|
2002-10-16 22:01:50 +00:00
|
|
|
static int PASCAL FAR setnodelay2(CNetwork::Socket s, bool nodelay);
|
2001-11-19 00:33:36 +00:00
|
|
|
static int (PASCAL FAR *WSACleanup)(void);
|
|
|
|
static int (PASCAL FAR *__WSAFDIsSet)(CNetwork::Socket, fd_set FAR *);
|
|
|
|
static int (PASCAL FAR *select)(int nfds, fd_set FAR *readfds, fd_set FAR *writefds, fd_set FAR *exceptfds, const struct timeval FAR *timeout);
|
|
|
|
#endif
|
2002-10-16 22:01:50 +00:00
|
|
|
|
|
|
|
#if UNIX_LIKE
|
|
|
|
static int PASCAL FAR gethostname2(char FAR * name, int namelen);
|
|
|
|
static int PASCAL FAR getsockerror2(void);
|
|
|
|
static int PASCAL FAR gethosterror2(void);
|
|
|
|
static int PASCAL FAR setblocking2(CNetwork::Socket s, bool blocking);
|
|
|
|
static int PASCAL FAR setnodelay2(CNetwork::Socket s, bool nodelay);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WINDOWS_LIKE || !HAVE_POLL
|
|
|
|
static int PASCAL FAR poll2(PollEntry[], int nfds, int timeout);
|
|
|
|
#endif
|
2001-11-19 00:33:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|