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_ */