25 #include <sys/types.h>
32 _kind(kind), _version(version), _state_timeout(0),
33 _socket(0), _recv_flags(kind), _proto_kind(
text), _empty_lines(false),
34 _buffer(
""), _tls(false)
39 if (WSAStartup(MAKEWORD(1, 1), &wsadata) != 0)
40 throw WSAStartupError(
"WSAStartup failed",
HERE);
44 throw Ipv6SupportError(
"lib was not compiled with ipv6 support",
HERE);
49 _kind(kind), _version(version), _state_timeout(0),
50 _socket(0), _recv_flags(kind), _proto_kind(pkind), _empty_lines(false),
51 _buffer(
""), _tls(false)
56 if (WSAStartup(MAKEWORD(1, 1), &wsadata) != 0)
57 throw WSAStartupError(
"WSAStartup failed",
HERE);
61 throw Ipv6SupportError(
"lib was not compiled with ipv6 support",
HERE);
75 throw TLSError(
"You need to have a TCP connection",
HERE);
77 throw NoConnection(
"You need to have a connection",
HERE);
79 gnutls_transport_set_ptr(_session, (gnutls_transport_ptr)
_socket);
80 ret = gnutls_handshake(_session);
84 gnutls_deinit(_session);
85 throw TLSError(gnutls_strerror(ret),
HERE);
88 throw TLSSupportError(
"lib was not compiled with TLS support",
HERE);
93 unsigned size,
const std::string &certfile,
94 const std::string &keyfile,
95 const std::string &trustfile,
96 const std::string &crlfile)
99 static bool init =
false;
100 static gnutls_dh_params dh_params;
101 const int protocol_tls[] = { GNUTLS_TLS1, 0 };
102 const int protocol_ssl[] = { GNUTLS_SSL3, 0 };
103 const int cert_type_priority[] = { GNUTLS_CRT_X509,
104 GNUTLS_CRT_OPENPGP, 0 };
108 gnutls_global_init();
113 gnutls_certificate_allocate_credentials(&_x509_cred);
114 if (keyfile.size() > 0 && certfile.size() > 0)
116 std::ifstream key(keyfile.c_str()), cert(certfile.c_str());
117 if (!key.is_open() || !cert.is_open())
118 throw InvalidFile(
"key or cert invalid",
HERE);
123 if (trustfile.size() > 0)
124 gnutls_certificate_set_x509_trust_file(_x509_cred, trustfile.c_str(),
125 GNUTLS_X509_FMT_PEM);
126 if (crlfile.size() > 0)
127 gnutls_certificate_set_x509_crl_file(_x509_cred, crlfile.c_str(),
128 GNUTLS_X509_FMT_PEM);
129 gnutls_certificate_set_x509_key_file(_x509_cred, certfile.c_str(),
131 GNUTLS_X509_FMT_PEM);
132 gnutls_dh_params_init(&dh_params);
133 gnutls_dh_params_generate2(dh_params, _nbbits);
134 gnutls_certificate_set_dh_params(_x509_cred, dh_params);
136 if (gnutls_init(&_session, GNUTLS_SERVER))
137 throw TLSError(
"gnutls_init failed",
HERE);
141 if (gnutls_init(&_session, GNUTLS_CLIENT))
142 throw TLSError(
"gnutls_init failed",
HERE);
145 gnutls_set_default_priority(_session);
147 gnutls_protocol_set_priority(_session, protocol_tls);
149 gnutls_protocol_set_priority(_session, protocol_ssl);
151 if (keyfile.size() > 0 && certfile.size() > 0)
153 gnutls_credentials_set(_session, GNUTLS_CRD_CERTIFICATE, _x509_cred);
154 gnutls_certificate_server_set_request(_session, GNUTLS_CERT_REQUEST);
155 gnutls_dh_set_prime_bits(_session, _nbbits);
159 gnutls_certificate_type_set_priority(_session, cert_type_priority);
160 gnutls_credentials_set(_session, GNUTLS_CRD_CERTIFICATE, _x509_cred);
163 throw TLSSupportError(
"lib was not compiled with TLS support",
HERE);
169 #ifndef LIBSOCKET_WIN
170 if (socket < 0 || close(socket) < 0)
171 throw CloseError(
"Close Error",
HERE);
174 if (socket < 0 || closesocket(socket) < 0)
175 throw CloseError(
"Close Error",
HERE);
181 std::cout <<
"Deletion..." << std::endl;
182 gnutls_deinit(_session);
185 gnutls_certificate_free_credentials(_x509_cred);
186 gnutls_global_deinit();
194 if (socket < 0 || listen(socket, 5) < 0)
195 throw ListenError(
"Listen Error",
HERE);
201 unsigned int count = 0;
206 throw NoConnection(
"No Socket",
HERE);
207 while (res && count < str.size())
214 res = gnutls_record_send(_session, buf + count, str.size() - count);
217 res = sendto(socket, buf + count, str.size() - count,
SENDTO_FLAGS,
218 (
const struct sockaddr*)&
_addr,
sizeof(
_addr));
221 res = sendto(socket, buf + count, str.size() - count,
SENDTO_FLAGS,
222 (
const struct sockaddr*)&_addr6,
sizeof(_addr6));
225 throw ConnectionClosed(
"Connection Closed",
HERE);
233 unsigned int count = 0;
235 char* buf =
new char[str.size() + 2];
237 char buf[str.size() + 2];
239 buf[0] = str.size() / 256;
240 buf[1] = str.size() % 256;
241 memcpy(buf + 2, str.c_str(), str.size());
243 throw NoConnection(
"No Socket",
HERE);
244 while (res && count < str.size() + 2)
251 res = gnutls_record_send(_session, buf + count, str.size() + 2 - count);
254 res = sendto(socket, buf + count, str.size() + 2 - count,
256 (
const struct sockaddr*)&
_addr,
sizeof(
_addr));
259 res = sendto(socket, buf + count, str.size() + 2 - count,
261 (
const struct sockaddr*)&_addr6,
sizeof(_addr6));
264 throw ConnectionClosed(
"Connection Closed",
HERE);
275 struct timeval timetowait;
279 timetowait.tv_sec = timeout;
281 timetowait.tv_sec = 65535;
282 timetowait.tv_usec = 0;
284 FD_SET(socket, &fdset);
286 res = select(socket + 1, &fdset, NULL, NULL, &timetowait);
288 res = select(socket + 1, &fdset, NULL, NULL, NULL);
290 throw SelectError(
"Select error",
HERE);
292 throw Timeout(
"Timeout on socket",
HERE);
325 std::list<std::string>::iterator it, it2;
342 int pos = -1, size = 0;
343 std::list<std::string>::const_iterator it;
349 while (it !=
_delim.end())
352 i = str.find(
'\0', start);
354 i = str.find(*it, start);
355 if ((i >= 0) && ((
unsigned int)i < str.size()) &&
356 (pos < 0 || i < pos))
359 size = it->size() ? it->size() : 1;
364 return std::pair<int, int>(pos, size);
This class represent an abstract socket connection (udp | tcp server | tcp client) ...
void init_tls(GnuTLSKind kind, unsigned size=1024, const std::string &certfile="", const std::string &keyfile="", const std::string &trustfile="", const std::string &crlfile="")
when TLS is not enabled
void _write_str_bin(int socket, const std::string &str) const
Write a string to a socket (when used with binary protocol) when there is no open socket when there i...
int get_socket()
get socket (fd) warning: be very carefull with this method
Socket & operator<<(Socket &s, const std::string &str)
write a string on current socket
void _close(int socket) const
Close a connnection when close libc function return a negative value.
void enable_tls()
Enable TLS on socket.
Network namespace represent all networks connection.
enum Network::e_gnutls_kind GnuTLSKind
void _set_timeout(bool enable, int socket, int timeout)
set a timeout on a socket
Socket(SOCKET_KIND kind, SOCKET_VERSION version=V4)
void _listen(int socket) const
Listen on port when listen libc function return a negative value.
bool connected() const
return true when socket is connected
std::list< std::string > _delim
virtual std::string read()=0
function used by >> operator (read a string on current socket)
void write(const std::string &str)
function used by << operator (write a string on current socket)
void allow_empty_lines()
, if set, empty lines will be returned in text procols (if not, they are skipped) ...
enum Network::e_version SOCKET_VERSION
std::pair< int, int > _find_delim(const std::string &str, int start) const
enum Network::e_kind SOCKET_KIND
enum Network::e_pkind PROTO_KIND
Socket & operator>>(Socket &s, std::string &str)
read a string on current socket
void del_delim(const std::string &delim)
delete this delimitor for the socket
void _write_str(int socket, const std::string &str) const
Write a string to a socket (when used with textual protocol) when there is no open socket when there ...
void add_delim(const std::string &delim)
set the delimitor for the text mode