# handlers.h

Go to the documentation of this file.

    00001 /*
    00002  * handlers.h
    00003  *
    00004  * Session Initiation Protocol non-connection protocol handlers.
    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  * $Revision: 29768 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2013-05-23 02:27:37 -0500 (Thu, 23 May 2013) $
    00029  */
    00030
    00031 #ifndef OPAL_SIP_HANDLERS_H
    00032 #define OPAL_SIP_HANDLERS_H
    00033
    00034 #ifdef P_USE_PRAGMA
    00035 #pragma interface
    00036 #endif
    00037
    00038 #ifndef _PTLIB_H
    00039 #include <ptlib.h>
    00040 #endif
    00041
    00042 #include <opal/buildopts.h>
    00043
    00044 #if OPAL_SIP
    00045
    00046 #include <opal/pres_ent.h>
    00047 #include <sip/sippdu.h>
    00048
    00049
    00051 class SIPHandlerBase : public PSafeObject
    00052 {
    00053     PCLASSINFO(SIPHandlerBase, PSafeObject);
    00054
    00055   protected:
    00056     SIPHandlerBase(const PString & callID) : m_callID(callID) { }
    00057
    00058   public:
    00059     const PString & GetCallID() const
    00060       { return m_callID; }
    00061
    00062   protected:
    00063     const PString m_callID;
    00064
    00065     // Keep a copy of the keys used for easy removal on destruction
    00066     typedef std::map<PString, PSafePtr<SIPHandler> > IndexMap;
    00067     std::pair<IndexMap::iterator, bool> m_byAorAndPackage;
    00068     std::pair<IndexMap::iterator, bool> m_byAuthIdAndRealm;
    00069     std::pair<IndexMap::iterator, bool> m_byAorUserAndRealm;
    00070
    00071   friend class SIPHandlersList;
    00072 };
    00073
    00074
    00075 /* Class to handle SIP REGISTER, SUBSCRIBE, MESSAGE, and renew
    00076  * the 'bindings' before they expire.
    00077  */
    00078 class SIPHandler : public SIPHandlerBase, public SIPTransactionOwner
    00079 {
    00080   PCLASSINFO(SIPHandler, SIPHandlerBase);
    00081
    00082 protected:
    00083   SIPHandler(
    00084     SIP_PDU::Methods method,
    00085     SIPEndPoint & ep,
    00086     const SIPParameters & params,
    00087     const PString & callID = SIPTransaction::GenerateCallID()
    00088   );
    00089
    00090 public:
    00091   ~SIPHandler();
    00092
    00093   virtual Comparison Compare(const PObject & other) const;
    00094
    00095   // Overrides from SIPTransactionOwner
    00096   virtual PString GetAuthID() const        { return m_username; }
    00097   virtual PString GetPassword() const      { return m_password; }
    00098
    00099   enum State {
    00100     Subscribed,       // The registration is active
    00101     Subscribing,      // The registration is in process
    00102     Unavailable,      // The registration is offline and still being attempted
    00103     Refreshing,       // The registration is being refreshed
    00104     Restoring,        // The registration is trying to be restored after being offline
    00105     Unsubscribing,    // The unregistration is in process
    00106     Unsubscribed,     // The registrating is inactive
    00107     NumStates
    00108   };
    00109
    00110   void SetState (SIPHandler::State s);
    00111
    00112   inline SIPHandler::State GetState() const
    00113   { return m_state; }
    00114
    00115   virtual const SIPURL & GetAddressOfRecord() const
    00116     { return m_addressOfRecord; }
    00117
    00118   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
    00119
    00120   virtual void SetExpire(int e);
    00121
    00122   virtual int GetExpire() const
    00123     { return m_currentExpireTime; }
    00124
    00125   virtual void SetBody(const PString & /*body*/) { }
    00126
    00127   virtual bool IsDuplicateCSeq(unsigned ) { return false; }
    00128
    00129   virtual SIPTransaction * CreateTransaction(OpalTransport &) { return NULL; }
    00130
    00131   SIP_PDU::Methods GetMethod() const { return m_method; }
    00132   virtual SIPSubscribe::EventPackage GetEventPackage() const { return SIPSubscribe::EventPackage(); }
    00133
    00134   virtual void OnReceivedResponse(SIPTransaction & transaction, SIP_PDU & response);
    00135   virtual void OnReceivedIntervalTooBrief(SIPTransaction & transaction, SIP_PDU & response);
    00136   virtual void OnReceivedTemporarilyUnavailable(SIPTransaction & transaction, SIP_PDU & response);
    00137   virtual void OnReceivedAuthenticationRequired(SIPTransaction & transaction, SIP_PDU & response);
    00138   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
    00139   virtual void OnTransactionFailed(SIPTransaction & transaction);
    00140
    00141 //  virtual void OnFailed(const SIP_PDU & response);
    00142   virtual void OnFailed(SIP_PDU::StatusCodes);
    00143   virtual void SendStatus(SIP_PDU::StatusCodes code, State state);
    00144
    00145   bool ActivateState(SIPHandler::State state);
    00146   virtual bool SendNotify(const PObject * /*body*/) { return false; }
    00147
    00148   SIP_PDU::StatusCodes GetLastResponseStatus() const { return m_lastResponseStatus; }
    00149
    00150   const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
    00151
    00152   const PString & GetRealm() const { return m_realm; }
    00153
    00154   virtual bool ShutDown();
    00155
    00156 protected:
    00157   virtual PBoolean SendRequest(SIPHandler::State state);
    00158   void RetryLater(unsigned after);
    00159   void OnExpireTimeout();
    00160   PDECLARE_WriteConnectCallback(SIPHandler, WriteTransaction);
    00161
    00162   PString                     m_username;
    00163   PString                     m_password;
    00164   PString                     m_realm;
    00165
    00166   const SIP_PDU::Methods      m_method;
    00167   const SIPURL                m_addressOfRecord;
    00168   SIPMIMEInfo                 m_mime;
    00169
    00170   unsigned                    m_lastCseq;
    00171   SIP_PDU::StatusCodes        m_lastResponseStatus;
    00172   int                         m_currentExpireTime;
    00173   int                         m_originalExpireTime;
    00174   int                         m_offlineExpireTime;
    00175   State                       m_state;
    00176   std::queue<State>           m_stateQueue;
    00177   bool                        m_receivedResponse;
    00178   SIPPoolTimer<SIPHandler>    m_expireTimer;
    00179   OpalProductInfo             m_productInfo;
    00180 };
    00181
    00182 #if PTRACING
    00183 ostream & operator<<(ostream & strm, SIPHandler::State state);
    00184 #endif
    00185
    00186
    00187 class SIPRegisterHandler : public SIPHandler
    00188 {
    00189   PCLASSINFO(SIPRegisterHandler, SIPHandler);
    00190
    00191 public:
    00192   SIPRegisterHandler(
    00193     SIPEndPoint & ep,
    00194     const SIPRegister::Params & params
    00195   );
    00196
    00197   virtual SIPTransaction * CreateTransaction(OpalTransport &);
    00198   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
    00199
    00200   void UpdateParameters(const SIPRegister::Params & params);
    00201
    00202   const SIPRegister::Params & GetParams() const { return m_parameters; }
    00203
    00204   const SIPURLList & GetContacts() const { return m_contactAddresses; }
    00205   const SIPURLList & GetServiceRoute() const { return m_serviceRoute; }
    00206
    00207 protected:
    00208   virtual PBoolean SendRequest(SIPHandler::State state);
    00209   virtual void SendStatus(SIP_PDU::StatusCodes code, State state);
    00210   PString CreateRegisterContact(const OpalTransportAddress & address, int q);
    00211
    00212   SIPRegister::Params  m_parameters;
    00213   unsigned             m_sequenceNumber;
    00214   SIPURLList           m_contactAddresses;
    00215   SIPURLList           m_serviceRoute;
    00216   OpalTransportAddress m_externalAddress;
    00217   PAtomicInteger::IntegerType m_rfc5626_reg_id;
    00218 };
    00219
    00220
    00221 class SIPSubscribeHandler : public SIPHandler
    00222 {
    00223   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
    00224 public:
    00225   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
    00226   ~SIPSubscribeHandler();
    00227
    00228   virtual SIPTransaction * CreateTransaction (OpalTransport &);
    00229   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
    00230   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
    00231   virtual void OnFailed(SIP_PDU::StatusCodes);
    00232   virtual SIPEventPackage GetEventPackage() const
    00233     { return m_parameters.m_eventPackage; }
    00234
    00235   void UpdateParameters(const SIPSubscribe::Params & params);
    00236
    00237   const SIPSubscribe::Params & GetParams() const { return m_parameters; }
    00238
    00239 protected:
    00240   virtual void WriteTransaction(OpalTransport & transport, bool & succeeded);
    00241   virtual void SendStatus(SIP_PDU::StatusCodes code, State state);
    00242   bool DispatchNOTIFY(SIP_PDU & request, SIP_PDU & response);
    00243
    00244   SIPSubscribe::Params     m_parameters;
    00245   bool                     m_unconfirmed;
    00246   SIPEventPackageHandler * m_packageHandler;
    00247 };
    00248
    00249
    00250 class SIPNotifyHandler : public SIPHandler
    00251 {
    00252   PCLASSINFO(SIPNotifyHandler, SIPHandler);
    00253 public:
    00254   SIPNotifyHandler(
    00255     SIPEndPoint & ep,
    00256     const SIPEventPackage & eventPackage,
    00257     const SIPDialogContext & dialog
    00258   );
    00259   ~SIPNotifyHandler();
    00260
    00261   virtual SIPTransaction * CreateTransaction(OpalTransport &);
    00262   virtual SIPEventPackage GetEventPackage() const
    00263     { return m_eventPackage; }
    00264
    00265   virtual void SetBody(const PString & body) { m_body = body; }
    00266
    00267   virtual bool SendNotify(const PObject * body);
    00268
    00269   enum Reasons {
    00270     Deactivated,
    00271     Probation,
    00272     Rejected,
    00273     Timeout,
    00274     GiveUp,
    00275     NoResource
    00276   };
    00277
    00278 protected:
    00279   virtual PBoolean SendRequest(SIPHandler::State state);
    00280   virtual void WriteTransaction(OpalTransport & transport, bool & succeeded);
    00281
    00282   SIPEventPackage          m_eventPackage;
    00283   Reasons                  m_reason;
    00284   SIPEventPackageHandler * m_packageHandler;
    00285   PString                  m_body;
    00286 };
    00287
    00288
    00289 class SIPPublishHandler : public SIPHandler
    00290 {
    00291   PCLASSINFO(SIPPublishHandler, SIPHandler);
    00292
    00293 public:
    00294   SIPPublishHandler(SIPEndPoint & ep,
    00295                     const SIPSubscribe::Params & params,
    00296                     const PString & body);
    00297
    00298   virtual void SetBody(const PString & body) { m_body = body; }
    00299
    00300   virtual SIPTransaction * CreateTransaction(OpalTransport &);
    00301   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
    00302   virtual SIPEventPackage GetEventPackage() const { return m_parameters.m_eventPackage; }
    00303
    00304 protected:
    00305   SIPSubscribe::Params m_parameters;
    00306   PString              m_body;
    00307   PString              m_sipETag;
    00308 };
    00309
    00310
    00311 class SIPMessageHandler : public SIPHandler
    00312 {
    00313   PCLASSINFO(SIPMessageHandler, SIPHandler);
    00314 public:
    00315   SIPMessageHandler(SIPEndPoint & ep, const SIPMessage::Params & params);
    00316
    00317   virtual SIPTransaction * CreateTransaction (OpalTransport &);
    00318   virtual void OnFailed(SIP_PDU::StatusCodes);
    00319   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
    00320
    00321   void UpdateParameters(const SIPMessage::Params & params);
    00322
    00323 protected:
    00324   SIPMessage::Params m_parameters;
    00325   bool               m_messageSent;
    00326 };
    00327
    00328
    00329 class SIPPingHandler : public SIPHandler
    00330 {
    00331   PCLASSINFO(SIPPingHandler, SIPHandler);
    00332 public:
    00333   SIPPingHandler(SIPEndPoint & ep, const PURL & to);
    00334   virtual SIPTransaction * CreateTransaction (OpalTransport &);
    00335 };
    00336
    00337
    00341 class SIPHandlersList
    00342 {
    00343   public:
    00346     void Append(SIPHandler * handler);
    00347
    00352     void Remove(SIPHandler * handler);
    00353
    00356     void Update(SIPHandler * handler);
    00357
    00360     bool DeleteObjectsToBeRemoved()
    00361       { return m_handlersList.DeleteObjectsToBeRemoved(); }
    00362
    00366     PSafePtr<SIPHandler> GetFirstHandler(PSafetyMode mode = PSafeReference) const
    00367       { return PSafePtr<SIPHandler>(m_handlersList, mode); }
    00368
    00372     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
    00373
    00377     PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
    00378
    00382     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
    00383
    00387     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, PSafetyMode m);
    00388
    00392     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
    00393
    00401     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
    00402     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
    00403
    00409     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
    00410
    00411   protected:
    00412     void RemoveIndexes(SIPHandler * handler);
    00413
    00414     PSafeSortedList<SIPHandlerBase> m_handlersList;
    00415
    00416     typedef SIPHandler::IndexMap IndexMap;
    00417     PSafePtr<SIPHandler> FindBy(IndexMap & by, const PString & key, PSafetyMode m);
    00418
    00419     IndexMap m_byAorAndPackage;
    00420     IndexMap m_byAuthIdAndRealm;
    00421     IndexMap m_byAorUserAndRealm;
    00422 };
    00423
    00424
    00425 #if OPAL_SIP_PRESENCE
    00426
    00429 class SIPPresenceInfo : public OpalPresenceInfo
    00430 {
    00431   PCLASSINFO_WITH_CLONE(SIPPresenceInfo, OpalPresenceInfo)
    00432
    00433 public:
    00434   // presence agent
    00435   PString m_presenceAgent;
    00436   PString m_personId;
    00437
    00438   PString AsXML() const;
    00439
    00440   static bool ParseNotify(
    00441     SIPSubscribe::NotifyCallbackInfo & notifyInfo,
    00442     list<SIPPresenceInfo> & info
    00443   );
    00444
    00445   void PrintOn(ostream & strm) const;
    00446   friend ostream & operator<<(ostream & strm, const SIPPresenceInfo & info) { info.PrintOn(strm); return strm; }
    00447
    00448   // Constructor
    00449   SIPPresenceInfo(
    00450     State state = Unchanged
    00451   ) : OpalPresenceInfo(state) { }
    00452   SIPPresenceInfo(
    00453     SIP_PDU::StatusCodes status,
    00454     bool subscribing
    00455   );
    00456   SIPPresenceInfo(
    00457     const OpalPresenceInfo & info
    00458   ) : OpalPresenceInfo(info) { }
    00459 };
    00460 #endif // OPAL_SIP_PRESENCE
    00461
    00462
    00465 struct SIPDialogNotification : public PObject
    00466 {
    00467   PCLASSINFO(SIPDialogNotification, PObject);
    00468
    00469   P_DECLARE_ENUM(States,
    00470     Terminated,
    00471     Trying,
    00472     Proceeding,
    00473     Early,
    00474     Confirmed
    00475   );
    00476   static PString GetStateName(States state);
    00477   PString GetStateName() const { return GetStateName(m_state); }
    00478
    00479   P_DECLARE_ENUM_EX(Events, NumEvents,
    00480     NoEvent, -1,
    00481     Cancelled,
    00482     Rejected,
    00483     Replaced,
    00484     LocalBye,
    00485     RemoteBye,
    00486     Error,
    00487     Timeout
    00488   );
    00489   static PString GetEventName(Events state);
    00490   PString GetEventName() const { return GetEventName(m_eventType); }
    00491
    00492   enum Rendering {
    00493     RenderingUnknown = -1,
    00494     NotRenderingMedia,
    00495     RenderingMedia
    00496   };
    00497
    00498   PString  m_entity;
    00499   PString  m_dialogId;
    00500   PString  m_callId;
    00501   bool     m_initiator;
    00502   States   m_state;
    00503   Events   m_eventType;
    00504   unsigned m_eventCode;
    00505   struct Participant {
    00506     Participant() : m_appearance(-1), m_byeless(false), m_rendering(RenderingUnknown) { }
    00507     PString   m_URI;
    00508     PString   m_dialogTag;
    00509     PString   m_identity;
    00510     PString   m_display;
    00511     int       m_appearance;
    00512     bool      m_byeless;
    00513     Rendering m_rendering;
    00514   } m_local, m_remote;
    00515
    00516   SIPDialogNotification(const PString & entity = PString::Empty());
    00517
    00518   void PrintOn(ostream & strm) const;
    00519 };
    00520
    00521
    00524 class SIPRegNotification : public PObject
    00525 {
    00526   PCLASSINFO(SIPRegNotification, PObject)
    00527 public:
    00528   P_DECLARE_ENUM(States,
    00529     Initial,
    00530     Active,
    00531     Terminated
    00532   );
    00533   static PString GetStateName(States state);
    00534   PString GetStateName() const { return GetStateName(m_state); }
    00535   static States GetStateFromName(const PCaselessString & str);
    00536
    00537   SIPRegNotification(
    00538     const SIPURL & aor = SIPURL(),
    00539     States state = Initial
    00540   );
    00541
    00542   SIPURL            m_aor;
    00543   States            m_state;
    00544   std::list<SIPURL> m_contacts;
    00545
    00546   void PrintOn(ostream & strm) const;
    00547 };
    00548
    00549
    00550 #endif // OPAL_SIP
    00551
    00552 #endif // OPAL_SIP_HANDLERS_H