166 lines
4.9 KiB
C++
166 lines
4.9 KiB
C++
//
|
|
// Socket
|
|
//
|
|
|
|
/* prevent multiple inclusions */
|
|
#ifndef __Socket__
|
|
#define __Socket__
|
|
|
|
/* includes *****************************************************************/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
#include <byteswap.h>
|
|
#include <netdb.h>
|
|
#include <net/if.h>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include "netlib.h"
|
|
|
|
/* defines ******************************************************************/
|
|
|
|
/* macros *******************************************************************/
|
|
|
|
#if defined(__BIG_ENDIAN)
|
|
# define ntohll(x) (x)
|
|
# define htonll(x) (x)
|
|
#elif defined(__LITTLE_ENDIAN)
|
|
# define ntohll(x) bswap_64(x)
|
|
# define htonll(x) bswap_64(x)
|
|
#endif
|
|
|
|
/* structs & typedefs *******************************************************/
|
|
|
|
/* c class definitions ******************************************************/
|
|
|
|
class Socket {
|
|
// public data
|
|
public:
|
|
|
|
// protected data
|
|
protected:
|
|
// socket
|
|
int m_family; // AF_UNIX, AF_INET or AF_INET6
|
|
int m_proto; // protocol
|
|
int m_sock; // socket
|
|
|
|
void LogSocket( const char *str );
|
|
|
|
// private data
|
|
private:
|
|
// reference
|
|
static char g_hostname[256];
|
|
char m_host[256];
|
|
char m_port[32];
|
|
|
|
// stats
|
|
unsigned long m_sentcnt;
|
|
unsigned long m_recvcnt;
|
|
|
|
// static data
|
|
|
|
// public methods
|
|
public:
|
|
// constructors
|
|
Socket();
|
|
|
|
// destructor
|
|
virtual ~Socket();
|
|
|
|
// copy constructor
|
|
Socket( const Socket& );
|
|
const Socket& operator=( const Socket& );
|
|
|
|
// public methods
|
|
int ClientUri( const char *uri );
|
|
int ServerUri( const char *uri );
|
|
|
|
// control
|
|
void SetFamily( int family ) {m_family = family;};
|
|
void SetProto( int proto ) {m_proto = proto;};
|
|
void SetHost(const char *host) {if(host)strncpy(m_host,host,sizeof(m_host)-1);};
|
|
void SetPort(const char *port) {if(port)strncpy(m_port,port,sizeof(m_port)-1);};
|
|
void SetHostPort(const char *host, const char *port)
|
|
{SetHost(host);SetPort(port);};
|
|
void SetSocket( int sock ) {m_sock=sock;};
|
|
|
|
// i/o
|
|
int Bind( const struct sockaddr_storage *sa, int len )
|
|
{return Bind(m_sock,sa,len);};
|
|
void Close( void );
|
|
|
|
int Recv( void *buf, int len );
|
|
int Send( const void *buf, int len );
|
|
int RecvFrom( void *buf, int max, struct sockaddr_storage *sa, socklen_t *len );
|
|
int SendTo( const void *buf, int max, const struct sockaddr_storage *sa, socklen_t len );
|
|
|
|
// status
|
|
unsigned long GetBytesSent(void) {return m_sentcnt;};
|
|
unsigned long GetBytesRecv(void) {return m_recvcnt;};
|
|
const char *GetHostName(void) {return g_hostname;};
|
|
const char *GetHost(void) {return m_host;};
|
|
const char *GetPort(void) {return m_port;};
|
|
|
|
int GetFamily(void) {return m_family;};
|
|
int GetProto(void) {return m_proto;};
|
|
int GetSock(void) {return m_sock;};
|
|
int GetSocket(void) {return m_sock;};
|
|
int SockAddr( struct sockaddr_storage *sa ) {return SockAddr(m_sock,sa);};
|
|
int SockName( char *buf, int max ) {return SockName(m_sock,buf,max); };
|
|
|
|
// virtual functions (must be overridden!)
|
|
virtual int Client( const char *host, const char *port ) {return -1;};
|
|
virtual int Server( const char *host, const char *port ) {return -1;};
|
|
virtual void Closed(void) {;};
|
|
|
|
// exceptions
|
|
class SocketException
|
|
{
|
|
char *toString(void) { return strerror(errno); };
|
|
};
|
|
|
|
// static methods
|
|
static int Bind( int sock, const struct sockaddr_storage *sa, socklen_t len );
|
|
static int Recv( int sock, void *buf, int max );
|
|
static int Send( int sock, const void *buf, int len );
|
|
static int RecvFrom( int sock, void *buf, int n,
|
|
struct sockaddr_storage *sa, socklen_t *len );
|
|
static int SendTo( int sock, const void *buf, int len,
|
|
const struct sockaddr_storage *sa, int salen );
|
|
|
|
static int Host2Addr(const char *host, const char *service,
|
|
struct sockaddr_storage *sa, socklen_t *len);
|
|
static const char *Host2Name( const struct sockaddr_storage *sa,
|
|
char *buf, int max );
|
|
static int Name2Host( const char *buf, struct sockaddr_storage *sa );
|
|
static int SockAddr( int sock, struct sockaddr_storage *sa );
|
|
static int SockName( int sock, char *buf, int max );
|
|
|
|
static int String2Mac( const char *str, unsigned char *mac );
|
|
static int Mac2String( const unsigned char *mac, char *str );
|
|
static int Interface2MacAddr( const char *name, unsigned char *mac );
|
|
static int Service2Port( const char *service, int port, const char *proto );
|
|
|
|
// protected methods
|
|
protected:
|
|
void Copy( const Socket &sock );
|
|
|
|
// private methods
|
|
private:
|
|
};
|
|
|
|
#endif
|