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-17 20:56:28 +00:00
|
|
|
#include "CString.h"
|
|
|
|
#include "stdvector.h"
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-12-25 10:35:59 +00:00
|
|
|
#if !defined(HAVE_SOCKLEN_T)
|
2002-11-03 18:09:28 +00:00
|
|
|
// Darwin is so unsure what to use for socklen_t it makes us choose
|
2002-12-25 10:35:59 +00:00
|
|
|
# if defined(__APPLE__)
|
|
|
|
# if !defined(_BSD_SOCKLEN_T_)
|
|
|
|
# define _BSD_SOCKLEN_T_ int
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
typedef int socklen_t;
|
2002-11-03 18:09:28 +00:00
|
|
|
# endif
|
|
|
|
#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;
|
2002-12-25 10:35:59 +00:00
|
|
|
# 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
|
|
|
#endif
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if UNIX_LIKE
|
2002-12-25 10:35:59 +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-10-30 22:16:30 +00:00
|
|
|
# include <netinet/in.h>
|
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-29 22:07:55 +00:00
|
|
|
typedef short AddressType;
|
2002-10-20 22:36:24 +00:00
|
|
|
typedef struct in_addr InternetAddress;
|
2002-10-16 22:01:50 +00:00
|
|
|
#elif UNIX_LIKE
|
|
|
|
typedef int Socket;
|
|
|
|
typedef struct sockaddr Address;
|
|
|
|
typedef socklen_t AddressLength;
|
2002-10-29 22:07:55 +00:00
|
|
|
typedef int AddressType;
|
2002-10-20 22:36:24 +00:00
|
|
|
typedef struct in_addr InternetAddress;
|
2002-10-16 22:01:50 +00:00
|
|
|
#endif
|
|
|
|
|
2002-12-25 10:35:59 +00:00
|
|
|
#if !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-10-17 20:56:28 +00:00
|
|
|
//! Host name information
|
|
|
|
class CHostInfo {
|
|
|
|
public:
|
|
|
|
CHostInfo() { }
|
|
|
|
CHostInfo(const struct hostent*);
|
|
|
|
|
|
|
|
void swap(CHostInfo&);
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::vector<CString> AliasList;
|
|
|
|
typedef std::vector<const char*> AddressList;
|
|
|
|
|
|
|
|
CString m_name;
|
|
|
|
AliasList m_aliases;
|
2002-10-29 22:07:55 +00:00
|
|
|
AddressType m_addressType;
|
2002-10-17 20:56:28 +00:00
|
|
|
int m_addressLength;
|
|
|
|
AddressList m_addresses;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_addressData;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Network service information
|
|
|
|
class CServiceInfo {
|
|
|
|
public:
|
|
|
|
CServiceInfo() { }
|
|
|
|
CServiceInfo(const struct servent*);
|
|
|
|
|
|
|
|
void swap(CServiceInfo&);
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::vector<CString> AliasList;
|
|
|
|
|
|
|
|
CString m_name;
|
|
|
|
AliasList m_aliases;
|
|
|
|
int m_port;
|
|
|
|
CString m_protocol;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Network protocol information
|
|
|
|
class CProtocolInfo {
|
|
|
|
public:
|
|
|
|
CProtocolInfo() { }
|
|
|
|
CProtocolInfo(const struct protoent*);
|
|
|
|
|
|
|
|
void swap(CProtocolInfo&);
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::vector<CString> AliasList;
|
|
|
|
|
|
|
|
CString m_name;
|
|
|
|
AliasList m_aliases;
|
|
|
|
int m_protocol;
|
|
|
|
};
|
|
|
|
|
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-10-30 22:16:30 +00:00
|
|
|
kEINTR = WSAEINTR,
|
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,
|
2002-10-30 22:16:30 +00:00
|
|
|
kEINTR = EINTR,
|
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
|
2002-10-17 20:56:28 +00:00
|
|
|
kHOST_OK = 0
|
2001-11-19 00:33:36 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2002-12-25 10:35:59 +00:00
|
|
|
static Socket accept(Socket s, Address* addr, AddressLength* addrlen);
|
|
|
|
static int bind(Socket s, const Address* addr, AddressLength namelen);
|
|
|
|
static int close(Socket s);
|
|
|
|
static int connect(Socket s, const Address* name, AddressLength namelen);
|
|
|
|
static int gethostname(char* name, int namelen);
|
|
|
|
static int getpeername(Socket s, Address* name, AddressLength* namelen);
|
|
|
|
static int getsockerror(void);
|
|
|
|
static int getsockname(Socket s, Address* name, AddressLength* namelen);
|
|
|
|
static int getsockopt(Socket s, int level, int optname, void* optval, AddressLength* optlen);
|
|
|
|
static int inet_aton(const char* cp, InternetAddress* addr);
|
|
|
|
static CString inet_ntoa(struct in_addr in);
|
|
|
|
static int ioctl(Socket s, int cmd, void* );
|
|
|
|
static int listen(Socket s, int backlog);
|
|
|
|
static int poll(PollEntry[], int nfds, int timeout);
|
|
|
|
static ssize_t read(Socket s, void* buf, size_t len);
|
|
|
|
static ssize_t recv(Socket s, void* buf, size_t len, int flags);
|
|
|
|
static ssize_t recvfrom(Socket s, void* buf, size_t len, int flags, Address* from, AddressLength* fromlen);
|
|
|
|
static ssize_t send(Socket s, const void* buf, size_t len, int flags);
|
|
|
|
static ssize_t sendto(Socket s, const void* buf, size_t len, int flags, const Address* to, AddressLength tolen);
|
|
|
|
static int setsockopt(Socket s, int level, int optname, const void* optval, AddressLength optlen);
|
|
|
|
static int shutdown(Socket s, int how);
|
|
|
|
static Socket socket(int af, int type, int protocol);
|
|
|
|
static ssize_t write(Socket s, const void* buf, size_t len);
|
|
|
|
static int gethostbyaddr(CHostInfo* hostinfo, const char* addr, int len, int type);
|
|
|
|
static int gethostbyname(CHostInfo* hostinfo, const char* name);
|
|
|
|
static int getservbyport(CServiceInfo* servinfo, int port, const char* proto);
|
|
|
|
static int getservbyname(CServiceInfo* servinfo, const char* name, const char* proto);
|
|
|
|
static int getprotobynumber(CProtocolInfo* protoinfo, int proto);
|
|
|
|
static int getprotobyname(CProtocolInfo* protoinfo, const char* name);
|
2001-11-19 00:33:36 +00:00
|
|
|
|
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-12-25 10:35:59 +00:00
|
|
|
static int setblocking(Socket s, bool blocking);
|
2002-06-21 16:19:08 +00:00
|
|
|
|
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).
|
|
|
|
*/
|
2002-12-25 10:35:59 +00:00
|
|
|
static int setnodelay(Socket s, bool nodelay);
|
2002-10-16 22:01:50 +00:00
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
private:
|
2002-10-16 22:01:50 +00:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
static void init2(HMODULE);
|
2002-10-16 22:01:50 +00:00
|
|
|
#endif
|
2001-11-19 00:33:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|