Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

socket.hh

Go to the documentation of this file.
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.10 2004/06/06 20:45:57 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 
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> // for connect(), bind(), accept(),
00038 # include <sys/time.h>   // for struct timeval
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>// for connect(), listen(), bind() accept(),
00050 # include <netinet/in.h>// for ntons(), htonl(), etc...
00051 # include <arpa/inet.h> // for inet_addre()
00052 # include <netdb.h>     // for gethostbyname()
00053 # include <unistd.h>    // for read()
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   // ip header 20 bytes
00091   // udp header 8 bytes
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     // Initialize GNUTLS support
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     // @brief find the index and the size of the delimiter.
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      /* !SOCKET_HH_ */

Generated on Fri Apr 8 06:13:01 2005 for libsocket by  doxygen 1.4.0