00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef SOCKET_HH_
00024 # define SOCKET_HH_
00025
00026 #include <iostream>
00027 #include <list>
00028 #include <string>
00029 #include <cstring>
00030
00031 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
00032 # define LIBSOCKET_WIN
00033 #endif
00034
00035 #ifdef LIBSOCKET_WIN
00036 # include <winsock.h>
00037 #else
00038 # include <sys/types.h>
00039 # include <sys/time.h>
00040 #endif
00041
00042 #ifdef LIBSOCKET_WIN
00043 # define SENDTO_FLAGS 0
00044 #else
00045 # if defined(__APPLE__) && defined(__MACH__)
00046 # define SENDTO_FLAGS 0
00047 # else
00048 # define SENDTO_FLAGS MSG_NOSIGNAL
00049 # endif
00050 # include <sys/socket.h>
00051 # include <netinet/in.h>
00052 # include <arpa/inet.h>
00053 # include <netdb.h>
00054 # include <unistd.h>
00055 #endif
00056
00057 #ifdef TLS
00058 # include <gnutls/gnutls.h>
00059 #endif
00060
00061 #include "socketexception.hh"
00062
00064 namespace Network
00065 {
00066 typedef enum e_gnutls_kind
00067 {
00068 LIBSOCKET_TLS,
00069 LIBSOCKET_SSL
00070 } GnuTLSKind;
00071
00072 typedef enum e_pkind
00073 {
00074 text,
00075 binary
00076 } PROTO_KIND;
00077
00078 typedef enum e_kind
00079 {
00080 TCP,
00081 UDP,
00082 LOCAL
00083 } SOCKET_KIND;
00084
00085 typedef enum e_version
00086 {
00087 V4,
00088 V6
00089 } SOCKET_VERSION;
00090
00091
00092
00093 static const int MAXPKTSIZE = 65507;
00094
00095 static const char DEFAULT_DELIM = '\0';
00096
00100 class Socket
00101 {
00102 public:
00103 Socket(SOCKET_KIND kind, SOCKET_VERSION version = V4);
00104 Socket(SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version = V4);
00105 virtual ~Socket();
00106
00107 public:
00110 void write(const std::string& str);
00111
00113 bool connected() const;
00114
00117 int get_socket();
00119 void add_delim(const std::string& delim);
00121 void del_delim(const std::string& delim);
00124 void allow_empty_lines();
00125
00126
00128 void init_tls(GnuTLSKind kind,
00129 unsigned size = 1024,
00130 const std::string &certfile = "",
00131 const std::string &keyfile = "",
00132 const std::string &trustfile = "",
00133 const std::string &crlfile = "");
00134
00136 void enable_tls();
00137
00140 virtual std::string read() = 0;
00142 virtual std::string read(int timeout) = 0;
00145 virtual std::string readn(unsigned int size) = 0;
00148 virtual std::string readn(int timeout, unsigned int size) = 0;
00149
00150 protected:
00154 void _close(int socket) const;
00158 void _listen(int socket) const;
00162 virtual std::string _read_line(int socket) = 0;
00166 virtual std::string _read_line_bin(int socket, unsigned int size) = 0;
00170 void _write_str(int socket, const std::string& str) const;
00174 void _write_str_bin(int socket, const std::string& str) const;
00180 void _set_timeout(bool enable, int socket, int timeout);
00181
00182 std::pair<int, int> _find_delim(const std::string& str, int start) const;
00185 bool _update_buffer(std::pair<int, int> &delim, int &i, std::string &str);
00187 bool _check_answer(int res, std::string &str);
00188
00189 protected:
00190 SOCKET_KIND _kind;
00191 SOCKET_VERSION _version;
00192 unsigned _state_timeout;
00193 int _socket;
00194 int _recv_flags;
00195 struct sockaddr_in _addr;
00196 # ifdef IPV6_ENABLED
00197 struct sockaddr_in6 _addr6;
00198 # endif
00199 PROTO_KIND _proto_kind;
00200 std::list<std::string> _delim;
00201 bool _empty_lines;
00202 std::string _buffer;
00203 bool _tls;
00204 # ifdef TLS
00205 gnutls_session _session;
00206 gnutls_certificate_credentials _x509_cred;
00207 unsigned _nbbits;
00208 bool _tls_main;
00209 # endif
00210 };
00211
00213 Socket& operator<<(Socket& s, const std::string& str);
00215 Socket& operator>>(Socket& s, std::string& str);
00216 }
00217
00218 #endif