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
00030 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__)
00031 # define LIBSOCKET_WIN
00032 #endif
00033
00034 #ifdef LIBSOCKET_WIN
00035 # include <winsock.h>
00036 #else
00037 # include <sys/types.h>
00038 # include <sys/time.h>
00039 #endif
00040
00041 #ifdef LIBSOCKET_WIN
00042 # define SENDTO_FLAGS 0
00043 #else
00044 # if defined(__APPLE__) && defined(__MACH__)
00045 # define SENDTO_FLAGS 0
00046 # else
00047 # define SENDTO_FLAGS MSG_NOSIGNAL
00048 # endif
00049 # include <sys/socket.h>
00050 # include <netinet/in.h>
00051 # include <arpa/inet.h>
00052 # include <netdb.h>
00053 # include <unistd.h>
00054 #endif
00055
00056 #ifdef TLS
00057 # include <gnutls/gnutls.h>
00058 #endif
00059
00060 #include "socketexception.hh"
00061
00063 namespace Network
00064 {
00065 typedef enum e_gnutls_kind
00066 {
00067 LIBSOCKET_TLS,
00068 LIBSOCKET_SSL
00069 } GnuTLSKind;
00070
00071 typedef enum e_pkind
00072 {
00073 text,
00074 binary
00075 } PROTO_KIND;
00076
00077 typedef enum e_kind
00078 {
00079 TCP,
00080 UDP,
00081 LOCAL
00082 } SOCKET_KIND;
00083
00084 typedef enum e_version
00085 {
00086 V4,
00087 V6
00088 } SOCKET_VERSION;
00089
00090
00091
00092 static const int MAXPKTSIZE = 65507;
00093
00094 static const char DEFAULT_DELIM = '\0';
00095
00099 class Socket
00100 {
00101 public:
00102 Socket(SOCKET_KIND kind, SOCKET_VERSION version = V4);
00103 Socket(SOCKET_KIND kind, PROTO_KIND pkind, SOCKET_VERSION version = V4);
00104 virtual ~Socket();
00105
00106 public:
00109 void write(const std::string& str);
00110
00112 bool connected() const;
00113
00116 int get_socket();
00118 void add_delim(const std::string& delim);
00120 void del_delim(const std::string& delim);
00123 void allow_empty_lines();
00124
00125
00127 void init_tls(GnuTLSKind kind,
00128 unsigned size = 1024,
00129 const std::string &certfile = "",
00130 const std::string &keyfile = "",
00131 const std::string &trustfile = "",
00132 const std::string &crlfile = "");
00133
00135 void enable_tls();
00136
00139 virtual std::string read() = 0;
00141 virtual std::string read(int timeout) = 0;
00144 virtual std::string readn(unsigned int size) = 0;
00147 virtual std::string readn(int timeout, unsigned int size) = 0;
00148
00149 protected:
00153 void _close(int socket) const;
00157 void _listen(int socket) const;
00161 virtual std::string _read_line(int socket) = 0;
00165 virtual std::string _read_line_bin(int socket, unsigned int size) = 0;
00169 void _write_str(int socket, const std::string& str) const;
00173 void _write_str_bin(int socket, const std::string& str) const;
00179 void _set_timeout(bool enable, int socket, int timeout);
00180
00181 std::pair<int, int> _find_delim(const std::string& str, int start) const;
00184 bool _update_buffer(std::pair<int, int> &delim, int &i, std::string &str);
00186 bool Socket::_check_answer(int res, std::string &str);
00187
00188 protected:
00189 SOCKET_KIND _kind;
00190 SOCKET_VERSION _version;
00191 unsigned _state_timeout;
00192 int _socket;
00193 int _recv_flags;
00194 struct sockaddr_in _addr;
00195 # ifdef IPV6_ENABLED
00196 struct sockaddr_in6 _addr6;
00197 # endif
00198 PROTO_KIND _proto_kind;
00199 std::list<std::string> _delim;
00200 bool _empty_lines;
00201 std::string _buffer;
00202 bool _tls;
00203 # ifdef TLS
00204 gnutls_session _session;
00205 gnutls_certificate_credentials _x509_cred;
00206 unsigned _nbbits;
00207 bool _tls_main;
00208 # endif
00209 };
00210
00212 Socket& operator<<(Socket& s, const std::string& str);
00214 Socket& operator>>(Socket& s, std::string& str);
00215 }
00216
00217 #endif