30 struct sockaddr_in
NetSocket::_get_addr(const std::string& host,
34 struct sockaddr_in addr;
36 memset(&addr, 0,
sizeof(
struct sockaddr_in));
37 he = gethostbyname(host.c_str());
39 throw HostnameError(
"Unknown Hostname",
HERE);
40 addr.sin_addr = *((
struct in_addr *)he->h_addr);
41 addr.sin_port = htons(port);
42 addr.sin_family = AF_INET;
47 struct sockaddr_in6 NetSocket::_get_addr6(const std::string& host,
50 struct sockaddr_in6 addr;
52 memset(&addr, 0,
sizeof(
struct sockaddr_in6));
53 if ( inet_pton(AF_INET6, host.c_str(), &addr.sin6_addr) == 0 )
54 throw InetptonError(
"Unknown Hostname",
HERE);
55 addr.sin6_port = htons(port);
56 addr.sin6_family = AF_INET6;
61 struct sockaddr_in NetSocket::_get_addr(int port) const
63 struct sockaddr_in addr;
65 memset(&addr, 0,
sizeof(
struct sockaddr_in));
66 addr.sin_addr.s_addr = htonl(INADDR_ANY);
67 addr.sin_port = htons(port);
68 addr.sin_family = AF_INET;
73 struct sockaddr_in6 NetSocket::_get_addr6(int port) const
75 struct sockaddr_in6 addr;
77 memset(&addr, 0,
sizeof(
struct sockaddr_in6));
78 if ( inet_pton(AF_INET6,
"0::0", &addr.sin6_addr) == 0 )
79 throw InetptonError(
"Not a valid address",
HERE);
80 addr.sin6_port = htons(port);
81 addr.sin6_family = AF_INET6;
95 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
98 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
101 else if (_kind ==
TCP)
106 s = socket(PF_INET, SOCK_STREAM, 0);
109 s = socket(PF_INET6, SOCK_STREAM, 0);
116 throw SocketError(
"Socket error",
HERE);
120 _addr = _get_addr(host, port);
123 _addr6 = _get_addr6(host, port);
137 s = socket(PF_INET, SOCK_STREAM, 0);
140 s = socket(PF_INET6, SOCK_STREAM, 0);
143 else if (_kind ==
UDP)
148 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
151 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
157 throw SocketError(
"Socket error",
HERE);
160 if (_kind ==
TCP && setsockopt(s, SOL_SOCKET,
161 SO_REUSEADDR, (
void *)&on,
163 throw SetsockoptError(
"setsockopt error",
HERE);
169 struct sockaddr_in addr;
170 addr = _get_addr(port);
171 if (bind(s,(
struct sockaddr*)&addr, (
int)
sizeof(addr)) == -1)
172 throw BindError(
"Bind error",
HERE);
177 struct sockaddr_in6 addr6;
178 addr6 = _get_addr6(port);
179 if (bind(s,(
struct sockaddr*)&addr6, (
int)
sizeof(addr6)) == -1)
180 throw BindError(
"Bind error",
HERE);
187 const std::string& host)
const 193 struct sockaddr_in addr;
194 addr = _get_addr(host, port);
195 if (connect(socket, (
struct sockaddr *)&addr,
197 throw ConnectError(
"Unable to connect",
HERE);
202 struct sockaddr_in6 addr6;
203 addr6 = _get_addr6(host, port);
204 if (connect(socket, (
struct sockaddr *)&addr6,
206 throw ConnectError(
"Unable to connect",
HERE);
219 struct sockaddr_in addr;
221 struct sockaddr_in6 addr6;
226 addr = _get_addr(port);
228 s = accept(socket, (
struct sockaddr*)&addr, &size);
233 addr6 = _get_addr6(port);
234 size =
sizeof(addr6);
235 s = accept(socket, (
struct sockaddr*)&addr6, &size);
239 throw AcceptError(
"Accept Error",
HERE);
245 struct sockaddr_in addr;
252 memset(&addr,
'\0',
sizeof(addr));
253 addr.sin_family = AF_INET;
254 addr.sin_addr.s_addr = htonl(INADDR_ANY);
255 addr.sin_port = htons(port);
257 getpeername(socket, (
struct sockaddr *)&addr, &size);
258 return(std::string(inet_ntoa(addr.sin_addr)));
263 char chr[MAXPKTSIZE];
264 std::string str =
"";
266 std::pair<int, int> delim;
270 throw NoConnection(
"No Socket",
HERE);
271 if (!_update_buffer(delim, i, str))
274 memset(chr, 0, MAXPKTSIZE);
276 _set_timeout(
true, _socket, _state_timeout);
279 res = recv(socket, chr, MAXPKTSIZE, 0);
281 res = recv(socket, chr, MAXPKTSIZE, MSG_TRUNC);
286 res = gnutls_record_recv(_session, chr, MAXPKTSIZE);
289 res = recv(socket, chr, MAXPKTSIZE, 0);
290 if (_check_answer(res, str))
292 _buffer += std::string(chr, res);
293 if (_update_buffer(delim, i, str))
303 char chr[MAXPKTSIZE];
304 std::string str =
"";
306 std::pair<int, int> delim;
307 struct sockaddr_in addr;
309 struct sockaddr_in6 addr6;
324 size =
sizeof(addr6);
327 throw NoConnection(
"No Socket",
HERE);
328 if (!_update_buffer(delim, i, str))
332 _set_timeout(
true, _socket, _state_timeout);
338 int flags = MSG_TRUNC;
344 res = recvfrom(socket, chr, MAXPKTSIZE, flags,
345 (
struct sockaddr *) &addr, &size);
348 res = recvfrom(socket, chr, MAXPKTSIZE, flags,
349 (
struct sockaddr *) &addr6, &size);
356 res = gnutls_record_recv(_session, chr, MAXPKTSIZE);
359 res = recvfrom(socket, chr, MAXPKTSIZE, 0, NULL, 0);
364 if (getpeername(socket, (
struct sockaddr *) &addr, &size) < 0)
365 throw GetpeernameError(
"getpeername error",
HERE);
369 if (getpeername(socket, (
struct sockaddr *) &addr6, &size) < 0)
370 throw GetpeernameError(
"getpeername error",
HERE);
373 if (_check_answer(res, str))
375 _buffer += std::string(chr, res);
376 if (_update_buffer(delim, i, str))
383 host = std::string(inet_ntoa(addr.sin_addr));
384 port = ntohs(addr.sin_port);
389 char buf[INET6_ADDRSTRLEN];
390 if (inet_ntop(AF_INET6, &addr6.sin6_addr, buf, INET6_ADDRSTRLEN) == 0)
391 throw InetntopError(
"Not a valid address",
HERE);
392 host = std::string(buf);
393 port = ntohs(addr6.sin6_port);
401 const std::string& host,
int port)
const 403 struct sockaddr_in addr;
405 struct sockaddr_in6 addr6;
408 const char *buf = str.c_str();
409 unsigned int count = 0;
414 addr = _get_addr(host, port);
417 addr6 = _get_addr6(host, port);
420 throw NoConnection(
"No Socket",
HERE);
421 while (res && count < str.size())
428 res = gnutls_record_send(_session, buf + count, str.size() - count);
431 res = sendto(socket, buf + count,
433 (
const struct sockaddr*)&addr,
sizeof(_addr));
436 res = sendto(socket, buf + count,
438 (
const struct sockaddr*)&addr6,
sizeof(_addr6));
441 throw ConnectionClosed(
"Connection Closed",
HERE);
447 const std::string& host,
int port)
const 449 struct sockaddr_in addr;
451 struct sockaddr_in6 addr6;
454 unsigned int count = 0;
456 char* buf =
new char[str.size() + 2];
458 char buf[str.size() + 2];
461 buf[0] = str.size() / 256;
462 buf[1] = str.size() % 256;
463 memcpy(buf + 2, str.c_str(), str.size());
467 addr = _get_addr(host, port);
470 addr6 = _get_addr6(host, port);
473 throw NoConnection(
"No Socket",
HERE);
474 while (res && count < str.size() + 2)
481 res = gnutls_record_send(_session, buf + count, str.size() + 2 - count);
484 res = sendto(socket, buf + count, str.size() + 2 - count,
486 (
const struct sockaddr*)&addr,
sizeof(_addr));
489 res = sendto(socket, buf + count, str.size() + 2 - count,
491 (
const struct sockaddr*)&addr6,
sizeof(_addr6));
494 throw ConnectionClosed(
"Connection Closed",
HERE);
503 const std::string& host,
int port)
505 if (_proto_kind ==
binary)
506 _write_str_bin(_socket, str, host, port);
508 _write_str(_socket, str, host, port);
513 if (_proto_kind ==
binary)
514 return _read_line_bin(_socket, port, host, 0);
516 return _read_line(_socket, port, host);
521 if (_proto_kind ==
binary)
523 _set_timeout(
true, _socket, timeout);
524 return _read_line_bin(_socket, port, host, 0);
528 _state_timeout = timeout;
529 return _read_line(_socket, port, host);
535 if (_proto_kind ==
binary)
536 return _read_line_bin(_socket, 0);
538 return _read_line(_socket);
543 if (_proto_kind ==
binary)
545 _set_timeout(
true, _socket, timeout);
546 return _read_line_bin(_socket, 0);
550 _state_timeout = timeout;
551 return _read_line(_socket);
561 return _read_line_bin(_socket, port, host, size);
567 if (!size || size > _buffer.size())
568 _set_timeout(
true, _socket, timeout);
572 return _read_line_bin(_socket, port, host, size);
580 return _read_line_bin(_socket, size);
585 if (!size || size > _buffer.size())
586 _set_timeout(
true, _socket, timeout);
590 return _read_line_bin(_socket, size);
void _write_str(int socket, const std::string &str, const std::string &host, int port) const
Write a string to a socket to a particular host (UDP) (when used with textual protocol) when there is...
Network namespace represent all networks connection.
void _connect(int socket, int port, const std::string &host) const
Connect to a hostname when connect libc function return a negative value.
virtual void writeto(const std::string &str, const std::string &host, int port)
function used to send a msg to a specific host (UDP)
void _write_str_bin(int socket, const std::string &str, const std::string &host, int port) const
Write a string to a socket to a particular host (UDP) (when used with binary protocol) when there is ...
std::string read()
function used by >> operator (read a string on current socket)
std::string _read_line(int socket)
Get a line from socket (when used with textual protocol) when there is no open socket when there is n...
This class is the top exception class used in libsocket.
This class represent an abstract socket connection (udp | tcp server | tcp client) ...
std::string readn(unsigned int size)
read a string from socket
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...