60 res->_tls_main =
false;
61 res->_session = _session;
62 res->_x509_cred = _x509_cred;
86 unsigned int size = 0,
read = 0;
90 throw NoConnection(
"No Socket",
HERE);
91 if (
_buffer.size() >= 2 && !psize)
93 size = (
unsigned char)
_buffer[0] * 256 + (
unsigned char)
_buffer[1];
101 if (psize &&
_buffer.size() >= psize)
103 str =
_buffer.substr(0, psize);
112 res = gnutls_record_recv(_session, chr, 2 -
_buffer.size());
115 res = recv(socket, chr, 2 -
_buffer.size(), 0);
117 throw ConnectionClosed(
"Connection Closed",
HERE);
119 size = (
unsigned char)
_buffer[0] * 256 + (
unsigned char)chr[0];
121 size = (
unsigned char)chr[0] * 256 + (
unsigned char)chr[1];
135 memset(chr, 0, MAXPKTSIZE);
138 res = gnutls_record_recv(_session, chr, size -
read);
141 res = recv(socket, chr, size -
read, 0);
143 throw ConnectionClosed(
"Connection Closed",
HERE);
144 str += std::string(chr, res).substr(0, res);
156 char chr[MAXPKTSIZE];
157 std::string str =
"";
159 struct sockaddr_in addr;
161 struct sockaddr_in6 addr6;
169 unsigned int pkg_size = 0,
read = 0;
177 size =
sizeof(addr6);
180 throw NoConnection(
"No Socket",
HERE);
181 if (
_buffer.size() >= 2 && !psize)
183 pkg_size = (
unsigned char)
_buffer[0] * 256 + (
unsigned char)
_buffer[1];
184 str =
_buffer.substr(2, pkg_size);
185 if (
_buffer.size() > pkg_size + 2)
191 if (psize &&
_buffer.size() >= psize)
193 str =
_buffer.substr(0, psize);
202 res = gnutls_record_recv(_session, chr, 2 -
_buffer.size());
205 res = recv(socket, chr, 2 -
_buffer.size(), 0);
207 throw ConnectionClosed(
"Connection Closed",
HERE);
209 pkg_size = (
unsigned char)
_buffer[0] * 256 + (
unsigned char)chr[0];
211 pkg_size = (
unsigned char)chr[0] * 256 + (
unsigned char)chr[1];
227 res = gnutls_record_recv(_session, chr, size -
read);
230 res = recv(socket, chr, size -
read, 0);
234 if (getpeername(socket, (
struct sockaddr *) &addr, &size) < 0)
235 throw GetpeernameError(
"getpeername error",
HERE);
238 if (getpeername(socket, (
struct sockaddr *) &addr6, &size) < 0)
239 throw GetpeernameError(
"getpeername error",
HERE);
242 throw ConnectionClosed(
"Connection Closed",
HERE);
243 str += std::string(chr, res).substr(0, res);
245 if (
read >= pkg_size)
252 host = std::string(inet_ntoa(addr.sin_addr));
253 port = ntohs(addr.sin_port);
258 char buf[INET6_ADDRSTRLEN];
259 if (inet_ntop(AF_INET6, &addr6.sin6_addr, buf, INET6_ADDRSTRLEN) == 0)
260 throw InetntopError(
"Not a valid address",
HERE);
261 host = std::string(buf);
262 port = ntohs(addr6.sin6_port);
TcpSocket * accept() const
accept a new client (For server only)
void _close(int socket) const
Close a connnection when close libc function return a negative value.
void enable_tls()
Enable TLS on socket.
std::string _read_line_bin(int socket, int &port, std::string &host, unsigned int psize)
Get a line from socket and store client hostname and port in port and host variable (when used with b...
Network namespace represent all networks connection.
void close()
Close the connection.
void connect(const std::string &hostname, int port)
Connect as an TCP client.
void _connect(int socket, int port, const std::string &host) const
Connect to a hostname when connect libc function return a negative value.
TcpSocket(SOCKET_VERSION version=V4)
void _listen(int socket) const
Listen on port when listen libc function return a negative value.
std::string read()
function used by >> operator (read a string on current socket)
std::string get_ip(TcpSocket *client) const
return ip of client (after an accept)
std::string _get_ip(int port, int socket) const
Get Client Ip.
int _accept(int port, int server_socket) const
Wait for a client when accept libc function return a negative value.
int _bind(int port, const std::string &host)
Bind a UDP server when socket libc function return a negative value if the selected protocole is inco...
This class represent a tcp connection (client and server)