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);
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);
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);
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);
void init_tls(GnuTLSKind kind, unsigned size=1024, const std::string &certfile="", const std::string &keyfile="", const std::string &trustfile="", const std::string &crlfile="")