Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/vos/remotesocketsite.hh

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001, 2002 Peter Amstutz
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020 
00021     Peter Amstutz <tetron@interreality.org>
00022 */
00023 #ifndef _REMOTESOCKETSITE_HH_
00024 #define _REMOTESOCKETSITE_HH_
00025 
00026 /** @file
00027     Defines RemoteSocketSite.
00028 */
00029 
00030 #include <vos/corelibs/vos/vosdefs.hh>
00031 #include <vos/corelibs/vos/remotesite.hh>
00032 #include <vos/corelibs/vos/localsocketsite.hh> // Needed for friend declaration
00033 
00034 namespace VOS
00035 {
00036     class RemoteSocketSite;
00037     class LocalSite;
00038 
00039 /**
00040      Connects to a remote site sort of asynchronously.  It starts the
00041      connection process in separate kernel thread so that a call to
00042      gethostbyname() or connect() does not hang the entire application
00043      process.  Meanwhile, the main thread repeatedly calls
00044      flushIncomingBuffers() while waiting for the worker thread to
00045      come back with the results of the connection.
00046 
00047      @internal
00048  */
00049 class AsyncConnect
00050 {
00051 public:
00052     bool* done;
00053     RemoteSocketSite** remotesite;
00054     LocalSite* localpeer;
00055     string remotehost;
00056     unsigned short int remoteport;
00057     string exstring;
00058     bool spooftest;
00059 
00060     AsyncConnect(bool* isdone, RemoteSocketSite** rs, LocalSite* localpeer,
00061                  const string& host, unsigned short int port, bool isspooftest);
00062     void operator()();
00063     static RemoteSocketSite* connect(const string& h, unsigned short int p, LocalSite* defaultPeer, bool isspooftest);
00064 };
00065 
00066 /** A message filter that denies (and drops) all outgoing messages except for
00067     anti-spoof checking and errors.
00068  */
00069 class OutgoingLock : public MessageFilter
00070 {
00071 public:
00072     virtual bool checkMessage(Message* m);
00073 };
00074 
00075 /** A message filter that enqueues all outgoing messages except for
00076     anti-spoof checking and errors.
00077  */
00078 class EnqueueOutgoing : public MessageFilter
00079 {
00080 public:
00081     queue< pair<Message*, MessageBlock*> > outgoingQueue;
00082     virtual bool checkMessage(Message* m);
00083     virtual bool checkMessage(MessageBlock* m);
00084 };
00085 
00086 /** A message filter that rewrites the To: field of messages.
00087  */
00088 class RewriteTo : public MessageFilter
00089 {
00090 public:
00091     string hostandport;
00092     RewriteTo(string hnp) : hostandport(hnp) { }
00093     virtual bool checkMessage(Message* m);
00094 };
00095 
00096 
00097 /** @class RemoteSocketSite remotesocketsite.hh vos/corelibs/vos/remotesocketsite.hh
00098  *
00099  * This class manages a connection to a remote site and the
00100     objects bound to that site.
00101  */
00102 class VOS_API RemoteSocketSite : public virtual RemoteSite
00103 {
00104 protected:
00105     unsigned short int remoteport;
00106     int readingFD;
00107     int writingFD;
00108     bool usingSOCKS;
00109     SSL* ssl;
00110     MessageBlock* partialMessage;
00111 
00112     void init(const string& hostname, unsigned short int port) throw (SiteConnectionError);
00113 
00114     RemoteSocketSite(const string& hostname, unsigned short int port) throw (SiteConnectionError);
00115     RemoteSocketSite(const string& hostname) throw (SiteConnectionError);
00116     RemoteSocketSite(int fd, struct sockaddr_in* peeraddr);
00117 
00118     virtual int readStream(char* data, unsigned int datasize);
00119     virtual int writeStream(const char* data, unsigned int datasize);
00120     //virtual void takeOver(RemoteSocketSite* rs);
00121 
00122     OutgoingLock outgoinglock;
00123     EnqueueOutgoing enqueueOutgoing;
00124 
00125     int sockSendBufSz;
00126     string outputBuffer;
00127 
00128 public:
00129     virtual ~RemoteSocketSite();
00130 
00131     /** @return the socket file descriptor over which we may read from this site */
00132     virtual int getReadingFD() const { return readingFD; }
00133 
00134     /** @return the socket file descriptor over which we may send to this site */
00135     virtual int getWritingFD() const { return writingFD; }
00136 
00137     virtual bool isConnected();
00138 
00139     /** Prevent messages from being sent along the underlying
00140         transport layer.
00141         @param enqueue true to save the messages in a queue, false to drop them
00142      */
00143     virtual void suppressOutgoing(bool enqueue);
00144 
00145     /** Reenable message sending after a call to suppressOutgoing()
00146         @param sendqueue true to send the queued messages, false to discard the queue
00147      */
00148     virtual void enableOutgoing(bool sendqueue, const string& rewriteTo = "?");
00149 
00150     virtual void sendMessage(Message* m);
00151     virtual void sendMessage(MessageBlock* m);
00152 
00153     /** Attempt to secure the connection using some transport-layer
00154         encryption.
00155         @param protocol the protocol to use.  Currently valid
00156         protocols are "SSLv3" and "TLSv1".  The latter protocol,
00157         "TLSv1" is a more recent standard based on SSL and should be
00158         preferred.
00159         @throw LocalSocketSite::SSLError on an error from OpenSSL
00160         @throw RemoteError if something went wrong or SSL support was not compiled in
00161     */
00162     void secureConnection(const string& protocol);
00163 
00164     /** Immediately try to switch to using a secure protocol.  Don't
00165         use this! (the other side needs to be told you want to switch)
00166         Use secureConnection() instead.
00167      */
00168     void switchProtocol(SSL_CTX* context);
00169 
00170     /** Get the remote site's X509 certificate.  You need to use the
00171         OpenSSL X509 API to access its fields.
00172      */
00173     X509* getCertificate();
00174     virtual void handleDisconnection();
00175     virtual void flushIncomingBuffers();
00176     virtual void flushOutgoingBuffers(const char* data = 0, unsigned int sz = 0);
00177 
00178     virtual void excise();
00179 
00180     //friend Site& Site::findSite(const string& s) throw (NoSuchSiteError);
00181     friend void LocalSocketSite::acceptConnectionRequests();
00182     friend class LocalSite;
00183     friend class AsyncConnect;
00184 };
00185 }
00186 
00187 #endif

Generated on Tue Aug 12 03:55:41 2003 for Interreality Project - VOS by doxygen 1.3.2