| 1 |
// BaseSocket class - inherit & implement to make UNIX/INET domain sockets |
| 2 |
|
| 3 |
// For all support, instructions and copyright go to: |
| 4 |
// http://e2guardian.org/ |
| 5 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 6 |
|
| 7 |
#ifndef __HPP_BASESOCKET |
| 8 |
#define __HPP_BASESOCKET |
| 9 |
|
| 10 |
// INCLUDES |
| 11 |
|
| 12 |
#include <string> |
| 13 |
#include <sys/types.h> |
| 14 |
#include <sys/socket.h> |
| 15 |
#include <sys/un.h> |
| 16 |
#include <exception> |
| 17 |
#include <netinet/in.h> |
| 18 |
#include <arpa/inet.h> |
| 19 |
#include <poll.h> |
| 20 |
#include "openssl/ssl.h" |
| 21 |
|
| 22 |
#define SCK_READ_BUFF_SIZE 4096 |
| 23 |
|
| 24 |
class BaseSocket |
| 25 |
{ |
| 26 |
public: |
| 27 |
// create socket from FD - must be overridden to clear the relevant address structs in derived classes |
| 28 |
BaseSocket(int fd); |
| 29 |
|
| 30 |
// make a socket a listening server socket |
| 31 |
int listen(int queue); |
| 32 |
|
| 33 |
// grab socket's FD, |
| 34 |
// use sparingly, and DO NOT do manual data transfer with it |
| 35 |
int getFD(); |
| 36 |
|
| 37 |
// close socket |
| 38 |
void close(); |
| 39 |
|
| 40 |
// set socket-wide timeout |
| 41 |
void setTimeout(int t); |
| 42 |
int getTimeout(); |
| 43 |
int getErrno(); |
| 44 |
|
| 45 |
bool isClosing(); |
| 46 |
bool isHup(); |
| 47 |
bool sockError(); |
| 48 |
bool isTimedout(); |
| 49 |
bool isNoOpp(); |
| 50 |
bool isNoRead(); |
| 51 |
bool isNoWrite(); |
| 52 |
bool isOpen(); |
| 53 |
bool ishup; |
| 54 |
|
| 55 |
// close & reset the connection - these must clear address structures & call baseReset/baseAccept |
| 56 |
virtual void reset() = 0; |
| 57 |
virtual BaseSocket *accept() = 0; |
| 58 |
|
| 59 |
// non-blocking check for input data |
| 60 |
bool checkForInput(int timeout = 20); |
| 61 |
// non-blocking check for writable socket |
| 62 |
//bool readyForOutput(); |
| 63 |
// blocking check |
| 64 |
bool readyForOutput(int timeout); |
| 65 |
|
| 66 |
// get a line from the socket |
| 67 |
int getLine(char *buff, int size, int timeout, bool *chopped = NULL, bool *truncated = NULL); |
| 68 |
|
| 69 |
// write string to socket |
| 70 |
bool writeString(const char *line); |
| 71 |
|
| 72 |
// write buff to socket - blocking |
| 73 |
bool writeToSocket(const char *buff, int len, unsigned int flags, int timeout); |
| 74 |
|
| 75 |
// write buff to socket - returns number of bytes written or 0 if would block or -1 on error |
| 76 |
int writeToSocketNB(const char *buff, int len, unsigned int flags); |
| 77 |
|
| 78 |
// read from socket, returning number of bytes read |
| 79 |
int readFromSocket(char *buff, int len, unsigned int flags, int timeout, bool ret_part = false); |
| 80 |
short int get_wait_flag(bool write_flag); |
| 81 |
bool timedout = false; |
| 82 |
bool isNoBlock = false; |
| 83 |
bool doCheck = true; |
| 84 |
int buffstart = 0; |
| 85 |
int bufflen = 0; |
| 86 |
char buffer[SCK_READ_BUFF_SIZE]; |
| 87 |
|
| 88 |
protected: |
| 89 |
// socket-wide timeout |
| 90 |
int timeout = 5000; |
| 91 |
int s_errno = 0; |
| 92 |
// length of address of other end of socket (e.g. size of sockaddr_in or sockaddr_un) |
| 93 |
socklen_t peer_adr_length; |
| 94 |
// socket FD |
| 95 |
int sck = -1; |
| 96 |
bool isclosing = false; |
| 97 |
//bool ishup; |
| 98 |
bool sockerr; |
| 99 |
//bool timedout; |
| 100 |
// internal buffer |
| 101 |
struct pollfd infds[1]; |
| 102 |
struct pollfd outfds[1]; |
| 103 |
|
| 104 |
// constructor - sets default values. override this if you actually wish to create a default socket. |
| 105 |
BaseSocket(); |
| 106 |
// destructor - closes socket |
| 107 |
virtual ~BaseSocket(); |
| 108 |
|
| 109 |
// performs accept(). call from derived classes' accept method |
| 110 |
int baseAccept(struct sockaddr *acc_adr, socklen_t *acc_adr_length); |
| 111 |
// closes socket & resets timeout to default - call from derived classes' reset method |
| 112 |
void baseReset(); |
| 113 |
}; |
| 114 |
|
| 115 |
#endif |