You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libUDPTCPNetwork/UDPTCPNetwork.h

220 lines
4.6 KiB

#ifndef _UDPTCPNETWORK_H_
#define _UDPTCPNETWORK_H_
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
#define WIN32_LEAN_AND_MEAN
#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 <winsock2.h>
#include <io.h>
#include <Ws2tcpip.h>
#include <string.h>
#ifndef bzero
#define bzero(__z__, __x__) memset (__z__, 0x0, __x__)
#endif
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/un.h>
#endif
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string>
using namespace std;
#define SOCKET int
#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);
void UDPTCPNetwork_Startup();
extern int UDPTCPNetwork_init;
#define UDPTCPNetwork() if(UDPTCPNetwork_init == 0) UDPTCPNetwork_Startup()
/************************************************************************
*
* udp related functions
*
*/
class UDP {
private:
SOCKET 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:
SOCKET sock;
string remote_host;
string remote_port;
int readcnt;
int writecnt;
int islisten;
public:
TCP();
TCP(SOCKET 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();
SOCKET GetSocket() { return sock; };
void SetSocket(SOCKET 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