| 1 |
// UdpSocket class - implements BaseSocket for 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_UDPSOCKET |
| 8 |
#define __HPP_UDPSOCKET |
| 9 |
|
| 10 |
// INCLUDES |
| 11 |
|
| 12 |
#include "BaseSocket.hpp" |
| 13 |
#include "String.hpp" |
| 14 |
#include <sstream> |
| 15 |
#include <iomanip> |
| 16 |
|
| 17 |
//#include "openssl/ssl.h" |
| 18 |
#include "String.hpp" |
| 19 |
|
| 20 |
// DECLARATIONS |
| 21 |
|
| 22 |
class UdpSocket : public BaseSocket |
| 23 |
{ |
| 24 |
//friend class FDTunnel; |
| 25 |
|
| 26 |
public: |
| 27 |
// create INET socket & clear address structs |
| 28 |
UdpSocket(); |
| 29 |
// create socket using pre-existing FD (address structs will be empty!) |
| 30 |
UdpSocket(int fd); |
| 31 |
// create socket from pre-existing FD, storing given local & remote IPs |
| 32 |
UdpSocket(int newfd, struct sockaddr_in myip, struct sockaddr_in peerip); |
| 33 |
~UdpSocket(); |
| 34 |
|
| 35 |
// connect to given IP & port (following default constructor) |
| 36 |
int connect(const std::string &ip, int port); |
| 37 |
|
| 38 |
// bind to given port |
| 39 |
int bind(int port); |
| 40 |
// bind to given IP & port, for machines with multiple NICs |
| 41 |
int bind(const std::string &ip, int port); |
| 42 |
|
| 43 |
// accept incoming connections & return new UdpSocket |
| 44 |
UdpSocket *accept(); |
| 45 |
|
| 46 |
// close socket & clear address structs |
| 47 |
void reset(); |
| 48 |
|
| 49 |
// get remote IP/port |
| 50 |
std::string getPeerIP(); |
| 51 |
int getPeerSourcePort(); |
| 52 |
int getPort(); |
| 53 |
void setPort(int port); |
| 54 |
void setClientAddr( std::string ip, int port); |
| 55 |
unsigned long int getPeerSourceAddr(); |
| 56 |
|
| 57 |
//std::string down_thread_id; |
| 58 |
|
| 59 |
// get local IP |
| 60 |
std::string getLocalIP(); |
| 61 |
int getLocalPort(); |
| 62 |
|
| 63 |
short int get_wait_flag(bool write_flag); |
| 64 |
|
| 65 |
void close(); |
| 66 |
|
| 67 |
|
| 68 |
// blocking check, can break on config reloads |
| 69 |
//void readyForOutput(int timeout, bool honour_reloadconfig = false); |
| 70 |
|
| 71 |
|
| 72 |
// write buffer to string |
| 73 |
bool writeString(const char *line); |
| 74 |
bool writeString(std::string line); |
| 75 |
|
| 76 |
// write buff to socket - blocking |
| 77 |
bool writeToSocket(const char *buff, int len, unsigned int flags, int timeout); |
| 78 |
|
| 79 |
// write buff to socket non-blocking - returns number of bytes written or 0 if would block or -1 on error |
| 80 |
int writeToSocketNB(const char *buff, int len, unsigned int flags); |
| 81 |
|
| 82 |
// read from socket, returning number of bytes read |
| 83 |
int readFromSocket(char *buff, int len, unsigned int flags, int timeout, bool ret_part = false); |
| 84 |
|
| 85 |
bool getIeof(); |
| 86 |
|
| 87 |
private: |
| 88 |
|
| 89 |
// local & remote addresses |
| 90 |
struct sockaddr_in my_adr; |
| 91 |
struct sockaddr_in peer_adr; |
| 92 |
int my_port = 0; |
| 93 |
std::string my_addr; |
| 94 |
int client_port = 0; |
| 95 |
std::string client_addr; |
| 96 |
bool ieof = false; |
| 97 |
}; |
| 98 |
|
| 99 |
#endif |