libsocket 1.5
|
00001 /* 00002 ** socket.hh 00003 ** Login : Julien Lemoine <speedblue@happycoders.org> 00004 ** Started on Sat Mar 1 23:20:24 2003 Julien Lemoine 00005 ** $Id: socket.hh,v 1.11 2004/11/14 19:37:46 speedblue Exp $ 00006 ** 00007 ** Copyright (C) 2003,2004 Julien Lemoine 00008 ** This program is free software; you can redistribute it and/or modify 00009 ** it under the terms of the GNU Lesser General Public License as published by 00010 ** the Free Software Foundation; either version 2 of the License, or 00011 ** (at your option) any later version. 00012 ** 00013 ** This program is distributed in the hope that it will be useful, 00014 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 ** GNU Lesser General Public License for more details. 00017 ** 00018 ** You should have received a copy of the GNU Lesser General Public License 00019 ** along with this program; if not, write to the Free Software 00020 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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> // for connect(), bind(), accept(), 00039 # include <sys/time.h> // for struct timeval 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>// for connect(), listen(), bind() accept(), 00051 # include <netinet/in.h>// for ntons(), htonl(), etc... 00052 # include <arpa/inet.h> // for inet_addre() 00053 # include <netdb.h> // for gethostbyname() 00054 # include <unistd.h> // for read() 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 // ip header 20 bytes 00092 // udp header 8 bytes 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 // Initialize GNUTLS support 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 // @brief find the index and the size of the delimiter. 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 /* !SOCKET_HH_ */