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);
106 s = socket(PF_INET, SOCK_STREAM, 0);
109 s = socket(PF_INET6, SOCK_STREAM, 0);
116 throw SocketError(
"Socket error",
HERE);
123 _addr6 = _get_addr6(host, port);
137 s = socket(PF_INET, SOCK_STREAM, 0);
140 s = socket(PF_INET6, SOCK_STREAM, 0);
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;
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;
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;
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);
274 memset(chr, 0, MAXPKTSIZE);
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);
292 _buffer += std::string(chr, res);
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);
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);
375 _buffer += std::string(chr, res);
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;
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());
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)
567 if (!size || size >
_buffer.size())
585 if (!size || size >
_buffer.size())
This class is the top exception class used in libsocket.
This class represent an abstract socket connection (udp | tcp server | tcp client)
int _bind(int port, const std::string &host)
Bind a UDP server.
virtual void writeto(const std::string &str, const std::string &host, int port)
function used to send a msg to a specific host (UDP)
std::string _read_line(int socket)
Get a line from socket (when used with textual protocol)
std::string _get_ip(int port, int socket) const
Get Client Ip.
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)
std::string readn(unsigned int size)
read a string from socket
void _connect(int socket, int port, const std::string &host) const
Connect to a hostname.
struct sockaddr_in _get_addr(int port) const
internal function (construct a sockaddr)
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)
int _accept(int port, int server_socket) const
Wait for a client.
virtual std::string _read_line_bin(int socket, int &port, std::string &host, unsigned int pkg_size)=0
Get a line from socket and store client hostname and port in port and host variable (when used with b...
std::string read()
function used by >> operator (read a string on current socket)
void _set_timeout(bool enable, int socket, int timeout)
set a timeout on a socket
bool _check_answer(int res, std::string &str)
return the content of the buffer is there is
bool _update_buffer(std::pair< int, int > &delim, int &i, std::string &str)
look delimiter and remove delimiter at begining of buffer if needed
Network namespace represent all networks connection.