• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

network.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
00003 //   Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef __NETWORK_H__
00020 #define __NETWORK_H__
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "gnashconfig.h"
00024 #endif
00025 
00026 #if !defined(HAVE_WINSOCK_H) || defined(__OS2__)
00027 # include <sys/types.h>
00028 # include <netinet/in.h>
00029 # include <arpa/inet.h>
00030 # include <sys/select.h>
00031 #ifdef HAVE_POLL_H
00032 # include <poll.h>
00033 #else 
00034 # ifdef HAVE_EPOLL_H
00035 #  include <epoll.h>
00036 # endif
00037 #endif
00038 #else
00039 # include <winsock2.h>
00040 # include <windows.h>
00041 # include <fcntl.h>
00042 # include <sys/stat.h>
00043 # include <io.h>
00044 #endif
00045 
00046 #include <boost/shared_ptr.hpp>
00047 #include <boost/scoped_ptr.hpp>
00048 #include <boost/cstdint.hpp>
00049 #include <boost/thread/mutex.hpp>
00050 #include <vector>
00051 #include <cassert>
00052 #include <string>
00053 #include <map>
00054 
00055 #ifdef USE_SSH
00056 # include "sshclient.h"
00057 #endif
00058 
00059 #ifdef USE_SSL
00060 # include "sslclient.h"
00061 #endif
00062 
00063 #include "dsodefs.h" //For DSOEXPORT.
00064 
00065 namespace cygnal {
00066 class Buffer;
00067 }
00068 
00071 namespace gnash {
00072 
00073 // forward declare the encryption protocols
00074 class SSLClient;
00075 class SSHClient;
00076 
00077 // Define the default ports
00078 const short SSL_PORT    = 443;
00079 const short SSH_PORT    = 22;
00080 const short HTTP_PORT   = 80;
00081 
00082 // Delay Tolerant Networking Research Group, http://www.dtnrg.org
00083 const short DTN1_PORT   = 2445;
00084 const short DTN2_PORT   = 4556;
00085 
00086 // Define the ports for the RTMP protocols
00087 const short ADMIN_PORT  = 1111;
00088 const short RTMP_PORT   = 1935;
00089 const short RTMPE_PORT  = 1935;
00090 const short RTMPT_PORT  = HTTP_PORT;
00091 const short RTMPTE_PORT = HTTP_PORT;
00092 const short RTMPTS_PORT = SSL_PORT;
00093 const short CGIBIN_PORT = 1234;
00094 
00095 #ifdef __OS2__
00096  typedef int    socklen_t;
00097  #define SHUT_RDWR 0x2
00098 #endif
00099 
00100 #if defined(HAVE_WINSOCK_H) && !defined(__OS2__)
00101   typedef long   in_addr_t;
00102 #  define inet_lnaof(x) inet_addr(inet_ntoa(x))
00103   typedef int    socklen_t;
00104 #endif
00105 
00106 #if defined(HAVE_POLL_H) || defined(HAVE_PPOLL)
00107 #include <poll.h>
00108 #else
00109 struct pollfd {
00110     int   fd; 
00111     short events;
00112     short revents;
00113 };
00114 #endif
00115 
00120 class DSOEXPORT Network {
00121 public:
00123     typedef enum {
00124         NONE,
00125         HTTP,
00126         HTTPS,
00127         RTMP,
00128         RTMPT,
00129         RTMPTS,
00130         RTMPE,
00131         RTMPS,
00132         DTN
00133     } protocols_supported_e;
00134     // This is used to pass parameters to a thread using boost::bind
00135     typedef struct {
00136         int tid;
00137         int port;
00138         int netfd;
00139         void *entry;
00140         void *handler;
00141         cygnal::Buffer *buffer;
00142         std::string filespec;
00143         protocols_supported_e protocol;
00144     } thread_params_t;
00145     typedef boost::uint8_t byte_t;
00146     typedef bool entry_t (thread_params_t *);
00147 
00148     Network();
00149     ~Network();
00150     
00159     int createServer(void);
00160     int createServer(short port);
00161     
00170     int newConnection(void);
00171     int newConnection(int fd);
00172     int newConnection(bool block, int fd);
00173     int newConnection(bool block);
00174 
00181     bool connectSocket(const std::string &sock);
00182 
00191     bool createClient(void);
00192     bool createClient(short port);
00193     bool createClient(const std::string &hostname);
00194     bool createClient(const std::string &hostname, short port);
00195 
00207     boost::shared_ptr<cygnal::Buffer> readNet();
00208     int readNet(cygnal::Buffer &buffer);
00209     int readNet(int fd, cygnal::Buffer &buffer);
00210     int readNet(int fd, cygnal::Buffer *buffer);
00211     int readNet(cygnal::Buffer &buffer, int timeout);
00212     int readNet(int fd, cygnal::Buffer &buffer, int timeout);
00213     int readNet(byte_t *data, int nbytes);
00214     int readNet(byte_t *data, int nbytes, int timeout);
00215     int readNet(int fd, byte_t *data, int nbytes);
00216     int readNet(int fd, byte_t *data, int nbytes, int timeout);
00217     
00229     int writeNet(cygnal::Buffer *buffer);
00230     int writeNet(cygnal::Buffer &buffer);
00231     int writeNet(int fd, cygnal::Buffer *buffer);
00232     int writeNet(int fd, cygnal::Buffer &buffer);
00233     int writeNet(const std::string &data);
00234     int writeNet(const byte_t *data, int nbytes);
00235 //    int writeNet(int fd, const byte_t *buffer);
00236     int writeNet(int fd, const byte_t *buffer, int nbytes);
00237     int writeNet(int fd, const byte_t *buffer, int nbytes, int timeout);
00238     
00244     boost::shared_ptr<std::vector<struct pollfd> > waitForNetData(int limit, struct pollfd *fds);
00245     fd_set waitForNetData(int limit, fd_set data);
00246     fd_set waitForNetData(std::vector<int> &data);
00247         
00253     bool closeNet();
00254     bool closeNet(int fd);
00255     bool closeConnection();
00256     bool closeConnection(int fd);
00257 
00258     // Change the debug flag
00259     void toggleDebug(bool val);
00260     bool netDebug() { return _debug; };
00261     
00262     bool send(const char *str);
00263 
00264     // Accessors for testing
00265     bool connected() const
00266     {
00267         assert ( ( _connected && _sockfd > 0 ) || ( ! _connected && _sockfd <= 0 ) );
00268         return _connected;
00269     };
00270 
00271     void setPort(short x) { _port = x; };
00272     short getPort() const { return _port; };
00273     void setFileFd(int x) { _sockfd = x; };
00274     int getFileFd() const { return _sockfd; };
00275     int getListenFd() const { return _listenfd; };
00276     void setListenFd(int x) { _listenfd = x; };
00277     const std::string& getURL() const { return _url; }
00278     void setURL(const std::string& url) { _url = url; }
00279 
00280     const std::string& getProtocol() const  { return _protocol; }
00281     void setProtocol(const std::string& proto) { _protocol = proto; }
00282 
00283     const std::string& getHost() const { return _host; }
00284     void setHost(const std::string& host) { _host = host; }
00285 
00286     const std::string& getPortStr() const { return _portstr; }
00287     void setPortStr(const std::string& port) { _portstr = port; }
00288 
00289     const std::string& getPath() const { return _path; }
00290     void setPath(const std::string& path) { _path = path; }
00291 
00292     void setTimeout(int x) { _timeout = x; }
00293     int getTimeout() const { return _timeout; }
00294 
00295     Network &operator = (Network &net);
00296 
00297     // The pollfd are an array of data structures used by the poll()
00298     // system call. We have to keep track of these as network
00299     // connections get added and disconnected.
00300     void addPollFD(struct pollfd &fd, entry_t *ptr);
00301     void addPollFD(struct pollfd &fd);
00302     void erasePollFD(int fd);
00303     void erasePollFD(std::vector<struct pollfd>::iterator &itt);
00304     struct pollfd &getPollFD(int fd);
00305     struct pollfd *getPollFDPtr();
00306 #ifdef HAVE_POLL_H
00307     size_t getPollFDSize() { return _pollfds.size(); };
00308     void clearPollFD() { _pollfds.clear(); };
00309 #endif
00310 
00311     // The entry point is an function pointer, which is the event
00312     // handler when there is data on a file descriptor.
00313     void addEntry(int fd, entry_t *func);
00314     entry_t *getEntry(int fd);
00315     
00316 //    void executePollFD(int index) { _handler[index](); ];
00317 
00318 #ifdef USE_SSL
00319     bool initSSL(std::string &hostname);
00320     bool initSSL(std::string &hostname, std::string &password);
00321     bool initSSL(std::string &hostname, std::string &password, bool auth);
00322     bool initSSL(std::string &hostname, std::string &password, 
00323                  std::string &keyfile, std::string &calist,
00324                  std::string &rootpath, bool auth);
00325 #endif
00326 
00327     // Use an ioctl() to see how many bytes are in the network buffers.
00328     size_t sniffBytesReady(int fd);
00329     
00330  protected:
00331     in_addr_t   _ipaddr;
00332     int         _sockfd;        // the file descriptor used for reading and writing
00333     int         _listenfd;      // the file descriptor used to listen for new connections
00334     short       _port;
00335     std::string _portstr;
00336     std::string _url;
00337     std::string _protocol;
00338     std::string _host;
00339     std::string _path;
00340     bool        _connected;
00341     bool        _debug;
00342     int         _timeout;
00343     size_t      _bytes_loaded;
00346     std::map<int, entry_t *> _handlers;
00347     std::vector<struct pollfd> _pollfds;
00348     // This is the mutex that controls access to the que.
00349     boost::mutex        _poll_mutex;
00350     boost::mutex        _net_mutex;
00351 #ifdef USE_SSL
00352     boost::scoped_ptr<SSLClient> _ssl;
00353 #endif
00354 #ifdef USE_SSH
00355     boost::scoped_ptr<SSHClient> _ssh;
00356 #endif
00357 };
00358 
00359 } // end of gnash namespace
00360 
00361 // __NETWORK_H__
00362 #endif
00363 
00364 // Local Variables:
00365 // mode: C++
00366 // indent-tabs-mode: nil
00367 // End:

Generated on Fri Mar 16 2012 15:46:11 for Gnash by  doxygen 1.7.1