h323trans.h

Go to the documentation of this file.
00001 /*
00002  * h323trans.h
00003  *
00004  * H.323 Transactor handler
00005  *
00006  * Open H323 Library
00007  *
00008  * Copyright (c) 2003 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Open H323 Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Log: h323trans.h,v $
00027  * Revision 2.2  2005/11/30 13:35:26  csoutheren
00028  * Changed tags for Doxygen
00029  *
00030  * Revision 2.1  2004/02/19 10:46:44  rjongbloed
00031  * Merged OpenH323 version 1.13.1 changes.
00032  *
00033  * Revision 1.15  2003/12/11 05:39:04  csoutheren
00034  * Added storage of H.225 version in endpoint structure
00035  * Disabled sending RIPs to endpoints that cannot handle them
00036  *
00037  * Revision 1.14  2003/04/30 07:50:58  robertj
00038  * Redesigned the alternate credentials in ARQ system as old implementation
00039  *   was fraught with concurrency issues, most importantly it can cause false
00040  *   detection of replay attacks taking out an endpoint completely.
00041  *
00042  * Revision 1.13  2003/04/10 09:40:05  robertj
00043  * Added associated transport to new GetInterfaceAddresses() function so
00044  *   interfaces can be ordered according to active transport links. Improves
00045  *   interoperability.
00046  *
00047  * Revision 1.12  2003/04/10 01:03:58  craigs
00048  * Added functions to access to lists of interfaces
00049  *
00050  * Revision 1.11  2003/04/09 03:08:06  robertj
00051  * Fixed race condition in shutting down transactor (pure virtual call)
00052  *
00053  * Revision 1.10  2003/04/01 05:59:30  robertj
00054  * Fixed H.501 transaction code setting members for m_common PDU part.
00055  *
00056  * Revision 1.9  2003/04/01 04:47:48  robertj
00057  * Abstracted H.225 RAS transaction processing (RIP and secondary thread) in
00058  *   server environment for use by H.501 peer elements.
00059  *
00060  * Revision 1.8  2003/03/26 00:46:25  robertj
00061  * Had another go at making H323Transactor being able to be created
00062  *   without having a listener running.
00063  *
00064  * Revision 1.7  2003/03/25 04:56:17  robertj
00065  * Fixed issues to do with multiple inheritence in transaction reply cache.
00066  *
00067  * Revision 1.6  2003/03/21 05:26:45  robertj
00068  * Added setting of remote port in UDP transport constructor.
00069  *
00070  * Revision 1.5  2003/03/20 01:51:07  robertj
00071  * More abstraction of H.225 RAS and H.501 protocols transaction handling.
00072  *
00073  * Revision 1.4  2003/03/01 00:23:42  craigs
00074  * New PeerElement implementation
00075  *
00076  * Revision 1.3  2003/02/25 06:48:15  robertj
00077  * More work on PDU transaction abstraction.
00078  *
00079  * Revision 1.2  2003/02/25 03:14:58  robertj
00080  * Added missing virtual destructor.
00081  *
00082  * Revision 1.1  2003/02/21 05:28:39  craigs
00083  * Factored out code for user with peer elements
00084  *
00085  */
00086 
00087 #ifndef __OPAL_H323TRANS_H
00088 #define __OPAL_H323TRANS_H
00089 
00090 #ifdef P_USE_PRAGMA
00091 #pragma interface
00092 #endif
00093 
00094 #include <h323/transaddr.h>
00095 #include <h323/h235auth.h>
00096 
00097 #include <ptclib/asner.h>
00098 
00099 
00100 class H323EndPoint;
00101 
00102 
00103 class H323TransactionPDU {
00104   public:
00105     H323TransactionPDU();
00106     H323TransactionPDU(const H235Authenticators & auth);
00107 
00108     virtual ~H323TransactionPDU() { }
00109 
00110     virtual BOOL Read(H323Transport & transport);
00111     virtual BOOL Write(H323Transport & transport);
00112 
00113     virtual PASN_Object & GetPDU() = 0;
00114     virtual PASN_Choice & GetChoice() = 0;
00115     virtual const PASN_Object & GetPDU() const = 0;
00116     virtual const PASN_Choice & GetChoice() const = 0;
00117     virtual unsigned GetSequenceNumber() const = 0;
00118     virtual unsigned GetRequestInProgressDelay() const = 0;
00119 #if PTRACING
00120     virtual const char * GetProtocolName() const = 0;
00121 #endif
00122     virtual H323TransactionPDU * ClonePDU() const = 0;
00123     virtual void DeletePDU() = 0;
00124 
00125     const H235Authenticators & GetAuthenticators() const { return authenticators; }
00126     void SetAuthenticators(
00127       const H235Authenticators & auth
00128     ) { authenticators = auth; }
00129 
00130     H235Authenticator::ValidationResult Validate(
00131       const PASN_Array & clearTokens,
00132       unsigned clearOptionalField,
00133       const PASN_Array & cryptoTokens,
00134       unsigned cryptoOptionalField
00135     ) const { return authenticators.ValidatePDU(*this, clearTokens, clearOptionalField, cryptoTokens, cryptoOptionalField, rawPDU); }
00136 
00137     void Prepare(
00138       PASN_Array & clearTokens,
00139       unsigned clearOptionalField,
00140       PASN_Array & cryptoTokens,
00141       unsigned cryptoOptionalField
00142     ) { authenticators.PreparePDU(*this, clearTokens, clearOptionalField, cryptoTokens, cryptoOptionalField); }
00143 
00144   protected:
00145     H235Authenticators authenticators;
00146     PPER_Stream        rawPDU;
00147 };
00148 
00149 
00151 
00152 class H323Transactor : public PObject
00153 {
00154   PCLASSINFO(H323Transactor, PObject);
00155   public:
00158 
00161     H323Transactor(
00162       H323EndPoint & endpoint,   
00163       H323Transport * transport, 
00164       WORD localPort,                     
00165       WORD remotePort                     
00166     );
00167     H323Transactor(
00168       H323EndPoint & endpoint,   
00169       const H323TransportAddress & iface, 
00170       WORD localPort,                     
00171       WORD remotePort                     
00172     );
00173 
00176     ~H323Transactor();
00178 
00183     void PrintOn(
00184       ostream & strm    
00185     ) const;
00187 
00192     BOOL SetTransport(
00193       const H323TransportAddress & iface 
00194     );
00195 
00198     H323TransportAddressArray GetInterfaceAddresses(
00199       BOOL excludeLocalHost = TRUE,       
00200       H323Transport * associatedTransport = NULL
00202     );
00203 
00206     virtual BOOL StartChannel();
00207 
00211     virtual void StopChannel();
00212 
00215     virtual H323TransactionPDU * CreateTransactionPDU() const = 0;
00216 
00219     virtual BOOL HandleTransaction(
00220       const PASN_Object & rawPDU
00221     ) = 0;
00222 
00225     virtual void OnSendingPDU(
00226       PASN_Object & rawPDU
00227     ) = 0;
00228 
00231     virtual BOOL WritePDU(
00232       H323TransactionPDU & pdu
00233     );
00234 
00237     virtual BOOL WriteTo(
00238       H323TransactionPDU & pdu,
00239       const H323TransportAddressArray & addresses,
00240       BOOL callback = TRUE
00241     );
00243 
00248     H323EndPoint & GetEndPoint() const { return endpoint; }
00249 
00252     H323Transport & GetTransport() const { return *transport; }
00253 
00256     void SetCheckResponseCryptoTokens(
00257       BOOL value    
00258     ) { checkResponseCryptoTokens = value; }
00259 
00262     BOOL GetCheckResponseCryptoTokens() { return checkResponseCryptoTokens; }
00264 
00265   protected:
00266     void Construct();
00267 
00268     unsigned GetNextSequenceNumber();
00269     BOOL SetUpCallSignalAddresses(
00270       H225_ArrayOf_TransportAddress & addresses
00271     );
00272 
00273     //Background thread handler.
00274     PDECLARE_NOTIFIER(PThread, H323Transactor, HandleTransactions);
00275         
00276     class Request : public PObject
00277     {
00278         PCLASSINFO(Request, PObject);
00279       public:
00280         Request(
00281           unsigned seqNum,
00282           H323TransactionPDU & pdu
00283         );
00284         Request(
00285           unsigned seqNum,
00286           H323TransactionPDU & pdu,
00287           const H323TransportAddressArray & addresses
00288         );
00289 
00290         BOOL Poll(H323Transactor &);
00291         void CheckResponse(unsigned, const PASN_Choice *);
00292         void OnReceiveRIP(unsigned milliseconds);
00293 
00294         // Inter-thread transfer variables
00295         unsigned rejectReason;
00296         void   * responseInfo;
00297 
00298         H323TransportAddressArray requestAddresses;
00299 
00300         unsigned             sequenceNumber;
00301         H323TransactionPDU & requestPDU;
00302         PTimeInterval        whenResponseExpected;
00303         PSyncPoint           responseHandled;
00304         PMutex               responseMutex;
00305 
00306         enum {
00307           AwaitingResponse,
00308           ConfirmReceived,
00309           RejectReceived,
00310           TryAlternate,
00311           BadCryptoTokens,
00312           RequestInProgress,
00313           NoResponseReceived
00314         } responseResult;
00315     };
00316 
00317     virtual BOOL MakeRequest(
00318       Request & request
00319     );
00320     BOOL CheckForResponse(
00321       unsigned,
00322       unsigned,
00323       const PASN_Choice * = NULL
00324     );
00325     BOOL HandleRequestInProgress(
00326       const H323TransactionPDU & pdu,
00327       unsigned delay
00328     );
00329     BOOL CheckCryptoTokens(
00330       const H323TransactionPDU & pdu,
00331       const PASN_Array & clearTokens,
00332       unsigned clearOptionalField,
00333       const PASN_Array & cryptoTokens,
00334       unsigned cryptoOptionalField
00335     );
00336 
00337     void AgeResponses();
00338     BOOL SendCachedResponse(
00339       const H323TransactionPDU & pdu
00340     );
00341 
00342     class Response : public PString
00343     {
00344         PCLASSINFO(Response, PString);
00345       public:
00346         Response(const H323TransportAddress & addr, unsigned seqNum);
00347         ~Response();
00348 
00349         void SetPDU(const H323TransactionPDU & pdu);
00350         BOOL SendCachedResponse(H323Transport & transport);
00351 
00352         PTime                lastUsedTime;
00353         PTimeInterval        retirementAge;
00354         H323TransactionPDU * replyPDU;
00355     };
00356 
00357     // Configuration variables
00358     H323EndPoint  & endpoint;
00359     WORD            defaultLocalPort;
00360     WORD            defaultRemotePort;
00361     H323Transport * transport;
00362     BOOL            checkResponseCryptoTokens;
00363 
00364     unsigned  nextSequenceNumber;
00365     PMutex    nextSequenceNumberMutex;
00366 
00367     PDictionary<POrdinalKey, Request> requests;
00368     PMutex                            requestsMutex;
00369     Request                         * lastRequest;
00370 
00371     PMutex                pduWriteMutex;
00372     PSortedList<Response> responses;
00373 };
00374 
00375 
00377 
00378 class H323Transaction : public PObject
00379 {
00380     PCLASSINFO(H323Transaction, PObject);
00381   public:
00386     H323Transaction(
00387       H323Transactor & transactor,
00388       const H323TransactionPDU & requestToCopy,
00389       H323TransactionPDU * confirm,
00390       H323TransactionPDU * reject
00391     );
00392     ~H323Transaction();
00394 
00395     enum Response {
00396       Ignore = -2,
00397       Reject = -1,
00398       Confirm = 0
00399     };
00400     inline static Response InProgress(unsigned time) { return (Response)(time&0xffff); }
00401 
00402     virtual H323TransactionPDU * CreateRIP(
00403       unsigned sequenceNumber,
00404       unsigned delay
00405     ) const = 0;
00406 
00407     BOOL HandlePDU();
00408 
00409     virtual BOOL WritePDU(
00410       H323TransactionPDU & pdu
00411     );
00412 
00413     BOOL CheckCryptoTokens(
00414       const H235Authenticators & authenticators
00415     );
00416 
00417 #if PTRACING
00418     virtual const char * GetName() const = 0;
00419 #endif
00420     virtual H235Authenticator::ValidationResult ValidatePDU() const = 0;
00421     virtual void SetRejectReason(
00422       unsigned reasonCode
00423     ) = 0;
00424 
00425     BOOL IsFastResponseRequired() const { return fastResponseRequired && canSendRIP; }
00426     BOOL CanSendRIP() const { return canSendRIP; }
00427     H323TransportAddress GetReplyAddress() const { return replyAddresses[0]; }
00428     const H323TransportAddressArray & GetReplyAddresses() const { return replyAddresses; }
00429     BOOL IsBehindNAT() const { return isBehindNAT; }
00430     H323Transactor & GetTransactor() const { return transactor; }
00431     H235Authenticator::ValidationResult GetAuthenticatorResult() const { return authenticatorResult; }
00432 
00433   protected:
00434     virtual Response OnHandlePDU() = 0;
00435     PDECLARE_NOTIFIER(PThread, H323Transaction, SlowHandler);
00436 
00437     H323Transactor         & transactor;
00438     unsigned                 requestSequenceNumber;
00439     H323TransportAddressArray replyAddresses;
00440     BOOL                     fastResponseRequired;
00441     H323TransactionPDU     * request;
00442     H323TransactionPDU     * confirm;
00443     H323TransactionPDU     * reject;
00444 
00445     H235Authenticators                  authenticators;
00446     H235Authenticator::ValidationResult authenticatorResult;
00447     BOOL                                isBehindNAT;
00448     BOOL                                canSendRIP;
00449 };
00450 
00451 
00453 
00454 class H323TransactionServer : public PObject
00455 {
00456   PCLASSINFO(H323TransactionServer, PObject);
00457   public:
00462     H323TransactionServer(
00463       H323EndPoint & endpoint
00464     );
00465 
00468     ~H323TransactionServer();
00470 
00471     virtual WORD GetDefaultUdpPort() = 0;
00472 
00477     H323EndPoint & GetOwnerEndPoint() const { return ownerEndPoint; }
00478 
00492     BOOL AddListeners(
00493       const H323TransportAddressArray & ifaces 
00494     );
00495 
00499     BOOL AddListener(
00500       const H323TransportAddress & interfaceName
00501     );
00502 
00509     BOOL AddListener(
00510       H323Transport * transport
00511     );
00512 
00519     BOOL AddListener(
00520       H323Transactor * listener
00521     );
00522 
00531     virtual H323Transactor * CreateListener(
00532       H323Transport * transport  
00533     ) = 0;
00534 
00538     BOOL RemoveListener(
00539       H323Transactor * listener
00540     );
00541 
00542     BOOL SetUpCallSignalAddresses(H225_ArrayOf_TransportAddress & addresses);
00544 
00545   protected:
00546     H323EndPoint & ownerEndPoint;
00547 
00548     PThread      * monitorThread;
00549     PSyncPoint     monitorExit;
00550 
00551     PMutex         mutex;
00552     PLIST(ListenerList, H323Transactor);
00553     ListenerList listeners;
00554 };
00555 
00556 
00557 #endif // __OPAL_H323TRANS_H
00558 
00559 

Generated on Fri Mar 7 06:33:31 2008 for OPAL by  doxygen 1.5.1