libsocket 1.5
socket.hh
Go to the documentation of this file.
1/*
2** socket.hh
3** Login : Julien Lemoine <speedblue@happycoders.org>
4** Started on Sat Mar 1 23:20:24 2003 Julien Lemoine
5** $Id: socket.hh,v 1.11 2004/11/14 19:37:46 speedblue Exp $
6**
7** Copyright (C) 2003,2004 Julien Lemoine
8** This program is free software; you can redistribute it and/or modify
9** it under the terms of the GNU Lesser General Public License as published by
10** the Free Software Foundation; either version 2 of the License, or
11** (at your option) any later version.
12**
13** This program is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** GNU Lesser General Public License for more details.
17**
18** You should have received a copy of the GNU Lesser General Public License
19** along with this program; if not, write to the Free Software
20** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#ifndef SOCKET_HH_
24# define SOCKET_HH_
25
26#include <iostream>
27#include <list>
28#include <string>
29#include <cstring>
30
31#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
32# define LIBSOCKET_WIN
33#endif
34
35#ifdef LIBSOCKET_WIN
36# include <winsock.h>
37#else
38# include <sys/types.h> // for connect(), bind(), accept(),
39# include <sys/time.h> // for struct timeval
40#endif
41
42#ifdef LIBSOCKET_WIN
43# define SENDTO_FLAGS 0
44#else
45# if defined(__APPLE__) && defined(__MACH__)
46# define SENDTO_FLAGS 0
47# else
48# define SENDTO_FLAGS MSG_NOSIGNAL
49# endif
50# include <sys/socket.h>// for connect(), listen(), bind() accept(),
51# include <netinet/in.h>// for ntons(), htonl(), etc...
52# include <arpa/inet.h> // for inet_addre()
53# include <netdb.h> // for gethostbyname()
54# include <unistd.h> // for read()
55#endif
56
57#ifdef TLS
58# include <gnutls/gnutls.h>
59#endif
60
61#include "socketexception.hh"
62
64namespace Network
65{
66 typedef enum e_gnutls_kind
67 {
71
72 typedef enum e_pkind
73 {
75 binary
77
78 typedef enum e_kind
79 {
82 LOCAL
84
85 typedef enum e_version
86 {
88 V6
90
91 // ip header 20 bytes
92 // udp header 8 bytes
93 static const int MAXPKTSIZE = 65507;
94
95 static const char DEFAULT_DELIM = '\0';
96
100 class Socket
101 {
102 public:
103 Socket(SOCKET_KIND kind, SOCKET_VERSION version = V4);
104 Socket(SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version = V4);
105 virtual ~Socket();
106
107 public:
110 void write(const std::string& str);
111
113 bool connected() const;
114
117 int get_socket();
119 void add_delim(const std::string& delim);
121 void del_delim(const std::string& delim);
124 void allow_empty_lines();
125
126 // Initialize GNUTLS support
128 void init_tls(GnuTLSKind kind,
129 unsigned size = 1024,
130 const std::string &certfile = "",
131 const std::string &keyfile = "",
132 const std::string &trustfile = "",
133 const std::string &crlfile = "");
134
136 void enable_tls();
137
140 virtual std::string read() = 0;
142 virtual std::string read(int timeout) = 0;
145 virtual std::string readn(unsigned int size) = 0;
148 virtual std::string readn(int timeout, unsigned int size) = 0;
149
150 protected:
154 void _close(int socket) const;
158 void _listen(int socket) const;
162 virtual std::string _read_line(int socket) = 0;
166 virtual std::string _read_line_bin(int socket, unsigned int size) = 0;
170 void _write_str(int socket, const std::string& str) const;
174 void _write_str_bin(int socket, const std::string& str) const;
180 void _set_timeout(bool enable, int socket, int timeout);
181 // @brief find the index and the size of the delimiter.
182 std::pair<int, int> _find_delim(const std::string& str, int start) const;
185 bool _update_buffer(std::pair<int, int> &delim, int &i, std::string &str);
187 bool _check_answer(int res, std::string &str);
188
189 protected:
195 struct sockaddr_in _addr;
196# ifdef IPV6_ENABLED
197 struct sockaddr_in6 _addr6;
198# endif
200 std::list<std::string> _delim;
202 std::string _buffer;
203 bool _tls;
204# ifdef TLS
205 gnutls_session _session;
206 gnutls_certificate_credentials _x509_cred;
207 unsigned _nbbits;
208 bool _tls_main;
209# endif
210 };
211
213 Socket& operator<<(Socket& s, const std::string& str);
215 Socket& operator>>(Socket& s, std::string& str);
216}
217
218#endif /* !SOCKET_HH_ */
This class represent an abstract socket connection (udp | tcp server | tcp client)
Definition: socket.hh:101
std::pair< int, int > _find_delim(const std::string &str, int start) const
Definition: socket.cc:339
int get_socket()
get socket (fd) warning: be very carefull with this method
Definition: socket.cc:313
Socket(SOCKET_KIND kind, SOCKET_VERSION version=V4)
Definition: socket.cc:31
bool _empty_lines
Definition: socket.hh:201
virtual std::string _read_line_bin(int socket, unsigned int size)=0
Get a line from socket (when used with binary protocol)
void write(const std::string &str)
function used by << operator (write a string on current socket)
Definition: socket.cc:295
void _write_str_bin(int socket, const std::string &str) const
Write a string to a socket (when used with binary protocol)
Definition: socket.cc:230
virtual std::string readn(unsigned int size)=0
read a string from socket
void _set_timeout(bool enable, int socket, int timeout)
set a timeout on a socket
Definition: socket.cc:272
std::string _buffer
Definition: socket.hh:202
void add_delim(const std::string &delim)
set the delimitor for the text mode
Definition: socket.cc:318
virtual std::string _read_line(int socket)=0
Get a line from socket (when used with textual protocol)
virtual std::string readn(int timeout, unsigned int size)=0
read a string with a timeout
struct sockaddr_in _addr
Definition: socket.hh:195
void del_delim(const std::string &delim)
delete this delimitor for the socket
Definition: socket.cc:323
bool _check_answer(int res, std::string &str)
return the content of the buffer is there is
Definition: socket.hxx:28
unsigned _state_timeout
Definition: socket.hh:192
SOCKET_KIND _kind
Definition: socket.hh:190
void enable_tls()
Enable TLS on socket.
Definition: socket.cc:69
SOCKET_VERSION _version
Definition: socket.hh:191
bool _update_buffer(std::pair< int, int > &delim, int &i, std::string &str)
look delimiter and remove delimiter at begining of buffer if needed
Definition: socket.hxx:45
void _listen(int socket) const
Listen on port.
Definition: socket.cc:192
bool connected() const
return true when socket is connected
Definition: socket.cc:303
virtual std::string read(int timeout)=0
read a string with a timeout
void _close(int socket) const
Close a connnection.
Definition: socket.cc:167
void allow_empty_lines()
, if set, empty lines will be returned in text procols (if not, they are skipped)
Definition: socket.cc:308
std::list< std::string > _delim
Definition: socket.hh:200
void init_tls(GnuTLSKind kind, unsigned size=1024, const std::string &certfile="", const std::string &keyfile="", const std::string &trustfile="", const std::string &crlfile="")
Definition: socket.cc:92
void _write_str(int socket, const std::string &str) const
Write a string to a socket (when used with textual protocol)
Definition: socket.cc:198
virtual std::string read()=0
function used by >> operator (read a string on current socket)
virtual ~Socket()
Definition: socket.cc:65
PROTO_KIND _proto_kind
Definition: socket.hh:199
Network namespace represent all networks connection.
Definition: localsocket.cc:33
Socket & operator>>(Socket &s, std::string &str)
read a string on current socket
Definition: socket.cc:373
e_version
Definition: socket.hh:86
@ V4
Definition: socket.hh:87
@ V6
Definition: socket.hh:88
e_kind
Definition: socket.hh:79
@ TCP
Definition: socket.hh:80
@ UDP
Definition: socket.hh:81
@ LOCAL
Definition: socket.hh:82
Socket & operator<<(Socket &s, const std::string &str)
write a string on current socket
Definition: socket.cc:367
e_pkind
Definition: socket.hh:73
@ text
Definition: socket.hh:74
@ binary
Definition: socket.hh:75
enum Network::e_version SOCKET_VERSION
enum Network::e_kind SOCKET_KIND
enum Network::e_gnutls_kind GnuTLSKind
e_gnutls_kind
Definition: socket.hh:67
@ LIBSOCKET_TLS
Definition: socket.hh:68
@ LIBSOCKET_SSL
Definition: socket.hh:69
enum Network::e_pkind PROTO_KIND