#ifndef _UDPTCPNETWORK_H_ #define _UDPTCPNETWORK_H_ #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) #define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) ver##0000 #define _NTDDI_VERSION_FROM_WIN32_WINNT(ver) _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) #ifndef _WIN32_WINNT # define _WIN32_WINNT 0x501 #endif #ifndef NTDDI_VERSION # define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT) #endif #include #include #include #include #ifndef bzero #define bzero(__z__, __x__) memset (__z__, 0x0, __x__) #endif #ifndef MSG_NOSIGNAL # define MSG_NOSIGNAL 0 #endif #else #include #include #include #include #include #include #endif #include #include #include using namespace std; #define NET_HOSTLEN 256 #define NET_PORTLEN 6 #define NET_BUFFERSIZE 1024 #define NET_MAX_RETRY 5 // retry to send data EINTR #define NET_MAX_TIMEOUT 30000 // timeout in ms /************************************************************************ * * global functions needed for networking * */ int dns_filladdr (string host, string port, int ai_family, struct sockaddr_storage *sAddr); char *itoa(char* buffer, int number, int size); /************************************************************************ * * udp related functions * */ class UDP { private: int sock; int localport; size_t readcnt; size_t writecnt; public: UDP(); ~UDP(); int Listen(int port); int SetBlocked(int bl); long int ReadTimeout(string *srcaddr, char *buffer, long int len, int timeout_ms); long int Read(string *srcaddr, char *buffer, long int len); long int Write(string destaddr, char *buffer, long int len); void Close(); int IsListen(); int IsData(int timeout_ms); // timeout in ms; int GetSocket() { return sock; }; }; /************************************************************************ * tcp related functions */ class TCP { private: int sock; string remote_host; string remote_port; int readcnt; int writecnt; int islisten; public: TCP(); TCP(int s); TCP(string h, string p); TCP(string hostport, int defaultport); ~TCP(); int Connect(); int Connect(string h, string p); int Connect(string hostport, int defaultport); long int ReadPop (char *buffer, long int pktlen, long int bufferlen); long int ReadTimeout(char *buffer, long int len, int timeout); long int Read(char *buffer, long int len); long int Write(char *buffer, long int len); void Close(); int IsConnected(); int IsData(int timeout); // timeout in ms; int IsListen() { return islisten; }; int Listen(int port); TCP* Accept(); int GetSocket() { return sock; }; void SetSocket(int s, struct sockaddr_storage *saddr, int saddrlen); const string GetRemoteAddr(); const string GetLocalAddr(); const string WebGetURLHost (string url); const string WebGetURLPort (string url); const string WebGetURLFile (string url); int WebGetFile (string url, char *buffer, int maxsize); }; /************************************************************************ * SSL functions */ class SSLSocket { private: int readcnt; int writecnt; string certfile; string keyfile; int timeout; SSL *ssl; SSL_CTX *ctx; struct timeval timeout_start; int NewServerCTX(); int NewClientCTX(); void TimeoutReset(); int TimeoutTime(); public: int sslerror; SSLSocket(); ~SSLSocket(); const string GetSSLErrorText(int err); int SetCertificat(string certf, string keyf); int Connect(int sockfd, int block_timeout); int Accept(int sockfd, int block_timeout); long int Read(char *buffer, long int len); long int Write(char *buffer, long int len); int Close(); // returns socket }; /************************************************************************ * unix socket related functions */ #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) #else class UNIX { private: int sock; string fname; int readcnt; int writecnt; public: UNIX(); UNIX(int fd, string filename); ~UNIX(); int Connect(string file); long int ReadTimeout(char *buffer, long int len, int timeout); long int Read(char *buffer, long int len); long int Write(char *buffer, long int len); void Close(); int IsConnected(); int IsData(int timeout); // timeout in ms; int Listen(string filename); UNIX* Accept(); int GetSocket() { return sock; }; }; #endif #endif