socket.hh
Go to the documentation of this file.
/*
** socket.hh
** Login : Julien Lemoine <speedblue@happycoders.org>
** Started on Sat Mar 1 23:20:24 2003 Julien Lemoine
** $Id: socket.hh,v 1.11 2004/11/14 19:37:46 speedblue Exp $
**
** Copyright (C) 2003,2004 Julien Lemoine
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef SOCKET_HH_
# define SOCKET_HH_
#include <iostream>
#include <list>
#include <string>
#include <cstring>
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
# define LIBSOCKET_WIN
#endif
#ifdef LIBSOCKET_WIN
# include <winsock.h>
#else
# include <sys/types.h> // for connect(), bind(), accept(),
# include <sys/time.h> // for struct timeval
#endif
#ifdef LIBSOCKET_WIN
# define SENDTO_FLAGS 0
#else
# if defined(__APPLE__) && defined(__MACH__)
# define SENDTO_FLAGS 0
# else
# define SENDTO_FLAGS MSG_NOSIGNAL
# endif
# include <sys/socket.h>// for connect(), listen(), bind() accept(),
# include <netinet/in.h>// for ntons(), htonl(), etc...
# include <arpa/inet.h> // for inet_addre()
# include <netdb.h> // for gethostbyname()
# include <unistd.h> // for read()
#endif
#ifdef TLS
# include <gnutls/gnutls.h>
#endif
#include "socketexception.hh"
namespace Network
{
typedef enum e_gnutls_kind
{
LIBSOCKET_TLS,
LIBSOCKET_SSL
} GnuTLSKind;
typedef enum e_pkind
{
text,
binary
} PROTO_KIND;
typedef enum e_kind
{
TCP,
UDP,
LOCAL
} SOCKET_KIND;
typedef enum e_version
{
V4,
V6
} SOCKET_VERSION;
// ip header 20 bytes
// udp header 8 bytes
static const int MAXPKTSIZE = 65507;
static const char DEFAULT_DELIM = '\0';
class Socket
{
public:
Socket(SOCKET_KIND kind, SOCKET_VERSION version = V4);
Socket(SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version = V4);
virtual ~Socket();
public:
void write(const std::string& str);
bool connected() const;
int get_socket();
void add_delim(const std::string& delim);
void del_delim(const std::string& delim);
void allow_empty_lines();
// Initialize GNUTLS support
void init_tls(GnuTLSKind kind,
unsigned size = 1024,
const std::string &certfile = "",
const std::string &keyfile = "",
const std::string &trustfile = "",
const std::string &crlfile = "");
void enable_tls();
virtual std::string read() = 0;
virtual std::string read(int timeout) = 0;
virtual std::string readn(unsigned int size) = 0;
virtual std::string readn(int timeout, unsigned int size) = 0;
protected:
void _close(int socket) const;
void _listen(int socket) const;
virtual std::string _read_line(int socket) = 0;
virtual std::string _read_line_bin(int socket, unsigned int size) = 0;
void _write_str(int socket, const std::string& str) const;
void _write_str_bin(int socket, const std::string& str) const;
void _set_timeout(bool enable, int socket, int timeout);
// @brief find the index and the size of the delimiter.
std::pair<int, int> _find_delim(const std::string& str, int start) const;
bool _update_buffer(std::pair<int, int> &delim, int &i, std::string &str);
bool _check_answer(int res, std::string &str);
protected:
SOCKET_KIND _kind;
SOCKET_VERSION _version;
unsigned _state_timeout;
int _socket;
int _recv_flags;
struct sockaddr_in _addr;
# ifdef IPV6_ENABLED
struct sockaddr_in6 _addr6;
# endif
PROTO_KIND _proto_kind;
std::list<std::string> _delim;
bool _empty_lines;
std::string _buffer;
bool _tls;
# ifdef TLS
gnutls_session _session;
gnutls_certificate_credentials _x509_cred;
unsigned _nbbits;
bool _tls_main;
# endif
};
Socket& operator<<(Socket& s, const std::string& str);
Socket& operator>>(Socket& s, std::string& str);
}
#endif /* !SOCKET_HH_ */
Network::LOCAL
@ LOCAL
Definition: socket.hh:82
Network::GnuTLSKind
enum Network::e_gnutls_kind GnuTLSKind
Network::e_gnutls_kind
e_gnutls_kind
Definition: socket.hh:66
Network::Socket::_listen
void _listen(int socket) const
Listen on port.
Definition: socket.cc:192
Network::Socket::_buffer
std::string _buffer
Definition: socket.hh:202
Network::e_version
e_version
Definition: socket.hh:85
Network::Socket::_proto_kind
PROTO_KIND _proto_kind
Definition: socket.hh:199
Network::Socket::_tls
bool _tls
Definition: socket.hh:203
Network::Socket::enable_tls
void enable_tls()
Enable TLS on socket.
Definition: socket.cc:69
Network::e_pkind
e_pkind
Definition: socket.hh:72
Network
Network namespace represent all networks connection.
Definition: localsocket.cc:32
Network::Socket::init_tls
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
Network::Socket::_empty_lines
bool _empty_lines
Definition: socket.hh:201
Network::Socket::_recv_flags
int _recv_flags
Definition: socket.hh:194
Network::Socket::_delim
std::list< std::string > _delim
Definition: socket.hh:200
Network::Socket::_addr
struct sockaddr_in _addr
Definition: socket.hh:195
Network::V6
@ V6
Definition: socket.hh:88
Network::Socket::_version
SOCKET_VERSION _version
Definition: socket.hh:191
Network::Socket::readn
virtual std::string readn(unsigned int size)=0
read a string from socket
Network::Socket::_set_timeout
void _set_timeout(bool enable, int socket, int timeout)
set a timeout on a socket
Definition: socket.cc:272
Network::Socket::_write_str_bin
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
Network::binary
@ binary
Definition: socket.hh:75
Network::Socket::_write_str
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
Network::Socket::read
virtual std::string read()=0
function used by >> operator (read a string on current socket)
Network::Socket::_kind
SOCKET_KIND _kind
Definition: socket.hh:190
Network::Socket::write
void write(const std::string &str)
function used by << operator (write a string on current socket)
Definition: socket.cc:295
Network::Socket::_read_line
virtual std::string _read_line(int socket)=0
Get a line from socket (when used with textual protocol)
Network::PROTO_KIND
enum Network::e_pkind PROTO_KIND
Network::Socket::allow_empty_lines
void allow_empty_lines()
, if set, empty lines will be returned in text procols (if not, they are skipped)
Definition: socket.cc:308
Network::e_kind
e_kind
Definition: socket.hh:78
Network::V4
@ V4
Definition: socket.hh:87
Network::Socket::_check_answer
bool _check_answer(int res, std::string &str)
return the content of the buffer is there is
Definition: socket.hxx:28
Network::Socket::_update_buffer
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
Network::Socket::connected
bool connected() const
return true when socket is connected
Definition: socket.cc:303
Network::Socket::_state_timeout
unsigned _state_timeout
Definition: socket.hh:192
Network::SOCKET_VERSION
enum Network::e_version SOCKET_VERSION
Network::Socket::_find_delim
std::pair< int, int > _find_delim(const std::string &str, int start) const
Definition: socket.cc:339
Network::text
@ text
Definition: socket.hh:74
Network::Socket::_socket
int _socket
Definition: socket.hh:193
Network::TCP
@ TCP
Definition: socket.hh:80
Network::Socket
This class represent an abstract socket connection (udp | tcp server | tcp client)
Definition: socket.hh:100
Network::LIBSOCKET_SSL
@ LIBSOCKET_SSL
Definition: socket.hh:69
Network::Socket::_read_line_bin
virtual std::string _read_line_bin(int socket, unsigned int size)=0
Get a line from socket (when used with binary protocol)
Network::Socket::get_socket
int get_socket()
get socket (fd) warning: be very carefull with this method
Definition: socket.cc:313
Network::Socket::add_delim
void add_delim(const std::string &delim)
set the delimitor for the text mode
Definition: socket.cc:318
Network::UDP
@ UDP
Definition: socket.hh:81
Network::LIBSOCKET_TLS
@ LIBSOCKET_TLS
Definition: socket.hh:68
Network::operator<<
Socket & operator<<(Socket &s, const std::string &str)
write a string on current socket
Definition: socket.cc:367
Network::SOCKET_KIND
enum Network::e_kind SOCKET_KIND
socketexception.hh
Network::operator>>
Socket & operator>>(Socket &s, std::string &str)
read a string on current socket
Definition: socket.cc:373
Network::Socket::_close
void _close(int socket) const
Close a connnection.
Definition: socket.cc:167
Network::Socket::~Socket
virtual ~Socket()
Definition: socket.cc:65
Network::Socket::Socket
Socket(SOCKET_KIND kind, SOCKET_VERSION version=V4)
Definition: socket.cc:31
Network::Socket::del_delim
void del_delim(const std::string &delim)
delete this delimitor for the socket
Definition: socket.cc:323