handlers.h

Go to the documentation of this file.
00001 /*
00002  * handlers.h
00003  *
00004  * Session Initiation Protocol endpoint.
00005  *
00006  * Open Phone Abstraction Library (OPAL)
00007  *
00008  * Copyright (c) 2000 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 Phone Abstraction Library.
00021  *
00022  * The Initial Developer of the Original Code is Damien Sandras. 
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Log: handlers.h,v $
00027  * Revision 1.7  2007/09/21 01:34:09  rjongbloed
00028  * Rewrite of SIP transaction handling to:
00029  *   a) use PSafeObject and safe collections
00030  *   b) only one database of transactions, remove connection copy
00031  *   c) fix timers not always firing due to bogus deadlock avoidance
00032  *   d) cleaning up only occurs in the existing garbage collection thread
00033  *   e) use of read/write mutex on endpoint list to avoid possible deadlock
00034  *
00035  * Revision 1.6  2007/09/04 11:54:49  csoutheren
00036  * Fixed compilation on Linux
00037  *
00038  * Revision 1.5  2007/09/04 05:42:55  rjongbloed
00039  * Added OnRegistrationStatus() call back function so can distinguish
00040  *   between initial registration and refreshes.
00041  * Fixed handler states so Refreshing state is actually used!
00042  *
00043  * Revision 1.4  2007/06/30 16:43:19  dsandras
00044  * Make sure transactions are completed before allowing destruction using
00045  * WaitForTransactionCompletion. If we have a timeout while unsusbscribing,
00046  * then allow deleting the handler.
00047  *
00048  * Revision 1.3  2007/06/10 08:55:11  rjongbloed
00049  * Major rework of how SIP utilises sockets, using new "socket bundling" subsystem.
00050  *
00051  * Revision 1.2  2007/05/21 04:30:30  dereksmithies
00052  * put #ifndef _PTLIB_H protection around the include of ptlib.h
00053  *
00054  * Revision 1.1  2007/05/15 20:45:09  dsandras
00055  * Added various handlers to manage subscriptions for presence, message
00056  * waiting indications, registrations, state publishing,
00057  * message conversations, ...
00058  * Adds/fixes support for RFC3856, RFC3903, RFC3863, RFC3265, ...
00059  * Many improvements over the original SIPInfo code.
00060  * Code contributed by NOVACOM (http://www.novacom.be) thanks to
00061  * EuroWeb (http://www.euroweb.hu).
00062  *
00063  *
00064  */
00065 
00066 #ifndef __OPAL_HANDLERS_H
00067 #define __OPAL_HANDLERS_H
00068 
00069 #ifdef P_USE_PRAGMA
00070 #pragma interface
00071 #endif
00072 
00073 #ifndef _PTLIB_H
00074 #include <ptlib.h>
00075 #endif
00076 
00077 #include <ptlib/safecoll.h>
00078 
00079 #include <sip/sippdu.h>
00080 #include <sip/sipep.h>
00081 
00082 
00083 /* Class to handle SIP REGISTER, SUBSCRIBE, MESSAGE, and renew
00084  * the 'bindings' before they expire.
00085  */
00086 class SIPHandler : public PSafeObject 
00087 {
00088   PCLASSINFO(SIPHandler, PSafeObject);
00089 
00090 protected:
00091   SIPHandler(
00092     SIPEndPoint & ep, 
00093     const PString & to,
00094     const PTimeInterval & retryMin = PMaxTimeInterval,
00095     const PTimeInterval & retryMax = PMaxTimeInterval
00096   );
00097 
00098 public:
00099   ~SIPHandler();
00100 
00101   enum State {
00102 
00103     Subscribed,       // The registration is active
00104     Subscribing,      // The registration is in process
00105     Refreshing,       // The registration is being refreshed
00106     Unsubscribing,    // The unregistration is in process
00107     Unsubscribed      // The registrating is inactive
00108   };
00109 
00110   inline void SetState (SIPHandler::State s) 
00111     {
00112       state = s;
00113     }
00114 
00115   inline SIPHandler::State GetState () 
00116     {
00117       return state;
00118     }
00119 
00120   virtual OpalTransport &GetTransport()
00121     { return *transport; }
00122 
00123   virtual SIPAuthentication & GetAuthentication()
00124     { return authentication; }
00125 
00126   virtual const SIPURL & GetTargetAddress()
00127     { return targetAddress; }
00128 
00129   virtual const PString GetRemotePartyAddress();
00130 
00131   virtual BOOL OnReceivedNOTIFY(SIP_PDU & response);
00132 
00133   // An expire time of -1 corresponds to an invalid SIPHandler that 
00134   // should be deleted.
00135   virtual void SetExpire(int e);
00136 
00137   virtual int GetExpire()
00138     { return expire; }
00139 
00140   virtual PString GetCallID()
00141     { return callID; }
00142 
00143   virtual BOOL CanBeDeleted();
00144 
00145   virtual void SetAuthUser(const PString & u)
00146     { authUser = u;}
00147 
00148   virtual PString GetAuthUser() const
00149     { return authUser;}
00150 
00151   virtual void SetPassword(const PString & p)
00152     { password = p;}
00153 
00154   virtual void SetAuthRealm(const PString & r)
00155     { authRealm = r; authentication.SetAuthRealm(r);}
00156 
00157   virtual void SetBody(const PString & b)
00158     { body = b;}
00159 
00160   virtual SIPTransaction * CreateTransaction(OpalTransport & t) = 0;
00161 
00162   virtual SIP_PDU::Methods GetMethod() = 0;
00163   virtual SIPSubscribe::SubscribeType GetSubscribeType() 
00164     { return SIPSubscribe::Unknown; }
00165 
00166   virtual void OnReceivedOK(SIP_PDU & response);
00167   virtual void OnTransactionTimeout(SIPTransaction & transaction);
00168   virtual void OnFailed(SIP_PDU::StatusCodes);
00169 
00170   virtual BOOL SendRequest(SIPHandler::State s = Subscribing);
00171 
00172   int GetAuthenticationAttempts() { return authenticationAttempts; };
00173   void SetAuthenticationAttempts(unsigned attempts) { authenticationAttempts = attempts; };
00174   const PStringList & GetRouteSet() const { return routeSet; }
00175 
00176 protected:
00177   SIPEndPoint               & endpoint;
00178   SIPAuthentication           authentication;
00179   PSafePtr<SIPTransaction>    transaction;
00180   OpalTransport             * transport;
00181   SIPURL                      targetAddress;
00182   PString                     callID;
00183   int                         originalExpire;
00184   int                         expire;
00185   PString                     authRealm;
00186   PString                     authUser;
00187   PString                     password;
00188   PStringList                 routeSet;
00189   PString                     body;
00190   unsigned                    authenticationAttempts;
00191   State                       state;
00192   PTimer                      expireTimer; 
00193   PTimeInterval               retryTimeoutMin; 
00194   PTimeInterval               retryTimeoutMax; 
00195   PString remotePartyAddress;
00196 
00197 private:
00198   static BOOL WriteSIPHandler(
00199     OpalTransport & transport, 
00200     void * info
00201   );
00202 };
00203 
00204 
00205 class SIPRegisterHandler : public SIPHandler
00206 {
00207   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00208 
00209 public:
00210   SIPRegisterHandler(SIPEndPoint & ep, 
00211                      const PString & to,
00212                      const PString & authName, 
00213                      const PString & password, 
00214                      const PString & realm,
00215                      int expire,
00216                      const PTimeInterval & minRetryTime, 
00217                      const PTimeInterval & maxRetryTime);
00218 
00219   ~SIPRegisterHandler();
00220 
00221   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00222   virtual void OnReceivedOK(SIP_PDU & response);
00223   virtual void OnTransactionTimeout(SIPTransaction & transaction);
00224   virtual SIP_PDU::Methods GetMethod()
00225     { return SIP_PDU::Method_REGISTER; }
00226 
00227   virtual void OnFailed(SIP_PDU::StatusCodes r);
00228 private:
00229   void SendStatus(SIP_PDU::StatusCodes code);
00230   PDECLARE_NOTIFIER(PTimer, SIPRegisterHandler, OnExpireTimeout);
00231 };
00232 
00233 
00234 class SIPSubscribeHandler : public SIPHandler
00235 {
00236   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00237 public:
00238   SIPSubscribeHandler(SIPEndPoint & ep, 
00239                       SIPSubscribe::SubscribeType type,
00240                       const PString & to,
00241                       int expire);
00242   ~SIPSubscribeHandler();
00243 
00244   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00245   virtual void OnReceivedOK(SIP_PDU & response);
00246   virtual void OnTransactionTimeout(SIPTransaction & transaction);
00247   virtual BOOL OnReceivedNOTIFY(SIP_PDU & response);
00248   virtual SIP_PDU::Methods GetMethod ()
00249     { return SIP_PDU::Method_SUBSCRIBE; }
00250   virtual SIPSubscribe::SubscribeType GetSubscribeType() 
00251     { return type; }
00252 
00253   virtual void OnFailed (SIP_PDU::StatusCodes);
00254   unsigned GetNextCSeq() { return ++lastSentCSeq; }
00255 
00256 private:
00257   PDECLARE_NOTIFIER(PTimer, SIPSubscribeHandler, OnExpireTimeout);
00258 
00259   BOOL OnReceivedMWINOTIFY(SIP_PDU & response);
00260   BOOL OnReceivedPresenceNOTIFY(SIP_PDU & response);
00261 
00262   SIPSubscribe::SubscribeType type;
00263   BOOL dialogCreated;
00264   PString localPartyAddress;
00265   unsigned lastSentCSeq;
00266   unsigned lastReceivedCSeq;
00267 };
00268 
00269 class SIPPublishHandler : public SIPHandler
00270 {
00271   PCLASSINFO(SIPPublishHandler, SIPHandler);
00272 
00273 public:
00274   SIPPublishHandler(SIPEndPoint & ep, 
00275                     const PString & to,
00276                     const PString & body,
00277                     int expire);
00278   ~SIPPublishHandler();
00279 
00280   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00281   virtual void OnReceivedOK(SIP_PDU & response);
00282   virtual void OnTransactionTimeout(SIPTransaction & transaction);
00283   virtual SIP_PDU::Methods GetMethod()
00284     { return SIP_PDU::Method_PUBLISH; }
00285   virtual void OnFailed(SIP_PDU::StatusCodes r);
00286   virtual void SetBody(const PString & body);
00287   static PString BuildBody(const PString & to,
00288                            const PString & basic,
00289                            const PString & note);
00290 
00291 private:
00292   PDECLARE_NOTIFIER(PTimer, SIPPublishHandler, OnExpireTimeout);
00293   PDECLARE_NOTIFIER(PTimer, SIPPublishHandler, OnPublishTimeout);
00294   PTimer publishTimer;
00295   PString sipETag;
00296   BOOL stateChanged;
00297 };
00298 
00299 class SIPMessageHandler : public SIPHandler
00300 {
00301   PCLASSINFO(SIPMessageHandler, SIPHandler);
00302 public:
00303   SIPMessageHandler(SIPEndPoint & ep, 
00304                     const PString & to,
00305                     const PString & body);
00306   ~SIPMessageHandler();
00307 
00308   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00309   virtual void OnReceivedOK(SIP_PDU & response);
00310   virtual void OnTransactionTimeout(SIPTransaction & transaction);
00311   virtual SIP_PDU::Methods GetMethod ()
00312     { return SIP_PDU::Method_MESSAGE; }
00313   virtual void OnFailed (SIP_PDU::StatusCodes);
00314 
00315 private:
00316   PDECLARE_NOTIFIER(PTimer, SIPMessageHandler, OnExpireTimeout);
00317   unsigned timeoutRetry;
00318 };
00319 
00320 class SIPPingHandler : public SIPHandler
00321 {
00322   PCLASSINFO(SIPPingHandler, SIPHandler);
00323 public:
00324   SIPPingHandler(SIPEndPoint & ep, 
00325                  const PString & to);
00326   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00327   virtual void OnReceivedOK(SIP_PDU & response);
00328   virtual void OnTransactionTimeout(SIPTransaction & transaction);
00329   virtual SIP_PDU::Methods GetMethod ()
00330     { return SIP_PDU::Method_MESSAGE; }
00331   virtual void OnFailed (SIP_PDU::StatusCodes);
00332 
00333 private:
00334   PDECLARE_NOTIFIER(PTimer, SIPPingHandler, OnExpireTimeout);
00335 };
00336 
00337 
00342 class SIPHandlersList : public PSafeList<SIPHandler>
00343 {
00344 public:
00348     unsigned GetRegistrationsCount();
00349 
00353     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00354 
00358     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00359 
00367     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
00368     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, SIPSubscribe::SubscribeType type, PSafetyMode m);
00369 
00375     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00376 };
00377 
00378 
00379 #endif

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