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  * $Revision: 24847 $
00027  * $Author: rjongbloed $
00028  * $Date: 2010-10-30 00:12:18 -0500 (Sat, 30 Oct 2010) $
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 <opal/connection.h>
00048 #include <sip/sippdu.h>
00049 
00050 
00051 /* Class to handle SIP REGISTER, SUBSCRIBE, MESSAGE, and renew
00052  * the 'bindings' before they expire.
00053  */
00054 class SIPHandler : public PSafeObject 
00055 {
00056   PCLASSINFO(SIPHandler, PSafeObject);
00057 
00058 protected:
00059   SIPHandler(
00060     SIP_PDU::Methods method,
00061     SIPEndPoint & ep,
00062     const SIPParameters & params
00063   );
00064 
00065 public:
00066   ~SIPHandler();
00067 
00068   virtual Comparison Compare(const PObject & other) const;
00069 
00070   virtual bool ShutDown();
00071 
00072   enum State {
00073     Subscribed,       // The registration is active
00074     Subscribing,      // The registration is in process
00075     Unavailable,      // The registration is offline and still being attempted
00076     Refreshing,       // The registration is being refreshed
00077     Restoring,        // The registration is trying to be restored after being offline
00078     Unsubscribing,    // The unregistration is in process
00079     Unsubscribed,     // The registrating is inactive
00080     NumStates
00081   };
00082 
00083   void SetState (SIPHandler::State s);
00084 
00085   inline SIPHandler::State GetState () 
00086   { return m_state; }
00087 
00088   virtual OpalTransport * GetTransport();
00089 
00090   virtual SIPAuthentication * GetAuthentication()
00091   { return authentication; }
00092 
00093   virtual const SIPURL & GetAddressOfRecord()
00094     { return m_addressOfRecord; }
00095 
00096   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00097 
00098   virtual void SetExpire(int e);
00099 
00100   virtual int GetExpire()
00101     { return expire; }
00102 
00103   virtual PString GetCallID()
00104     { return callID; }
00105 
00106   virtual void SetBody(const PString & /*body*/) { }
00107 
00108   virtual bool IsDuplicateCSeq(unsigned ) { return false; }
00109 
00110   virtual SIPTransaction * CreateTransaction(OpalTransport & t) = 0;
00111 
00112   SIP_PDU::Methods GetMethod() const { return m_method; }
00113   virtual SIPSubscribe::EventPackage GetEventPackage() const { return SIPEventPackage(); }
00114 
00115   virtual void OnReceivedResponse(SIPTransaction & transaction, SIP_PDU & response);
00116   virtual void OnReceivedIntervalTooBrief(SIPTransaction & transaction, SIP_PDU & response);
00117   virtual void OnReceivedTemporarilyUnavailable(SIPTransaction & transaction, SIP_PDU & response);
00118   virtual void OnReceivedAuthenticationRequired(SIPTransaction & transaction, SIP_PDU & response);
00119   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00120   virtual void OnTransactionFailed(SIPTransaction & transaction);
00121 
00122   virtual void OnFailed(const SIP_PDU & response);
00123   virtual void OnFailed(SIP_PDU::StatusCodes);
00124 
00125   bool ActivateState(SIPHandler::State state);
00126   virtual bool SendNotify(const PObject * /*body*/) { return false; }
00127 
00128   SIPEndPoint & GetEndPoint() const { return endpoint; }
00129 
00130   const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
00131 
00132   const PString & GetUsername() const     { return m_username; }
00133   const PString & GetPassword() const     { return m_password; }
00134   const PString & GetRealm() const        { return m_realm; }
00135   const SIPURL & GetRemoteAddress() const { return m_remoteAddress; }
00136   const SIPURL & GetProxy() const         { return m_proxy; }
00137 
00138   SIPMIMEInfo m_mime;
00139 
00140 protected:
00141   virtual PBoolean SendRequest(SIPHandler::State state);
00142   void RetryLater(unsigned after);
00143   PDECLARE_NOTIFIER(PTimer, SIPHandler, OnExpireTimeout);
00144   static PBoolean WriteSIPHandler(OpalTransport & transport, void * info);
00145   virtual bool WriteSIPHandler(OpalTransport & transport, bool forked);
00146 
00147   SIPEndPoint               & endpoint;
00148 
00149   SIPAuthentication         * authentication;
00150   PString                     m_username;
00151   PString                     m_password;
00152   PString                     m_realm;
00153 
00154   PSafeList<SIPTransaction>   m_transactions;
00155   OpalTransport             * m_transport;
00156 
00157   SIP_PDU::Methods            m_method;
00158   SIPURL                      m_addressOfRecord;
00159   SIPURL                      m_remoteAddress;
00160   PString                     callID;
00161   int                         expire;
00162   int                         originalExpire;
00163   int                         offlineExpire;
00164   unsigned                    authenticationAttempts;
00165   State                       m_state;
00166   queue<State>                m_stateQueue;
00167   bool                        m_receivedResponse;
00168   PTimer                      expireTimer; 
00169   PTimeInterval               retryTimeoutMin; 
00170   PTimeInterval               retryTimeoutMax; 
00171   SIPURL                      m_proxy;
00172   OpalProductInfo             m_productInfo;
00173 
00174   // Keep a copy of the keys used for easy removal on destruction
00175   typedef std::map<PString, PSafePtr<SIPHandler> > IndexMap;
00176   std::pair<IndexMap::iterator, bool> m_byCallID;
00177   std::pair<IndexMap::iterator, bool> m_byAorAndPackage;
00178   std::pair<IndexMap::iterator, bool> m_byAuthIdAndRealm;
00179   std::pair<IndexMap::iterator, bool> m_byAorUserAndRealm;
00180 
00181   friend class SIPHandlersList;
00182 };
00183 
00184 #if PTRACING
00185 ostream & operator<<(ostream & strm, SIPHandler::State state);
00186 #endif
00187 
00188 
00189 class SIPRegisterHandler : public SIPHandler
00190 {
00191   PCLASSINFO(SIPRegisterHandler, SIPHandler);
00192 
00193 public:
00194   SIPRegisterHandler(
00195     SIPEndPoint & ep,
00196     const SIPRegister::Params & params
00197   );
00198 
00199   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00200   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00201 
00202   virtual void OnFailed(SIP_PDU::StatusCodes r);
00203 
00204   void UpdateParameters(const SIPRegister::Params & params);
00205 
00206   const SIPRegister::Params & GetParams() const { return m_parameters; }
00207 
00208 protected:
00209   virtual PBoolean SendRequest(SIPHandler::State state);
00210   void SendStatus(SIP_PDU::StatusCodes code, State state);
00211 
00212   SIPRegister::Params m_parameters;
00213   unsigned            m_sequenceNumber;
00214 };
00215 
00216 
00217 class SIPSubscribeHandler : public SIPHandler
00218 {
00219   PCLASSINFO(SIPSubscribeHandler, SIPHandler);
00220 public:
00221   SIPSubscribeHandler(SIPEndPoint & ep, const SIPSubscribe::Params & params);
00222   ~SIPSubscribeHandler();
00223 
00224   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00225   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00226   virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
00227   virtual void OnFailed(const SIP_PDU & response);
00228   virtual SIPEventPackage GetEventPackage() const
00229     { return m_parameters.m_eventPackage; }
00230 
00231   void UpdateParameters(const SIPSubscribe::Params & params);
00232 
00233   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00234 
00235   const SIPSubscribe::Params & GetParams() const { return m_parameters; }
00236 
00237 protected:
00238   virtual PBoolean SendRequest(SIPHandler::State state);
00239   virtual bool WriteSIPHandler(OpalTransport & transport, bool forked);
00240   void SendStatus(SIP_PDU::StatusCodes code, State state);
00241   bool DispatchNOTIFY(SIP_PDU & request, SIP_PDU & response);
00242 
00243   SIPSubscribe::Params     m_parameters;
00244   SIPDialogContext         m_dialog;
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 PString & targetAddress,
00257     const SIPEventPackage & eventPackage,
00258     const SIPDialogContext & dialog
00259   );
00260   ~SIPNotifyHandler();
00261 
00262   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00263   virtual SIPEventPackage GetEventPackage() const
00264     { return m_eventPackage; }
00265 
00266   virtual void SetBody(const PString & body) { m_body = body; }
00267 
00268   virtual bool IsDuplicateCSeq(unsigned sequenceNumber) { return m_dialog.IsDuplicateCSeq(sequenceNumber); }
00269   virtual bool SendNotify(const PObject * body);
00270 
00271   enum Reasons {
00272     Deactivated,
00273     Probation,
00274     Rejected,
00275     Timeout,
00276     GiveUp,
00277     NoResource
00278   };
00279 
00280 protected:
00281   virtual PBoolean SendRequest(SIPHandler::State state);
00282   virtual bool WriteSIPHandler(OpalTransport & transport, bool forked);
00283 
00284   SIPEventPackage          m_eventPackage;
00285   SIPDialogContext         m_dialog;
00286   Reasons                  m_reason;
00287   SIPEventPackageHandler * m_packageHandler;
00288   PString                  m_body;
00289 };
00290 
00291 
00292 class SIPPublishHandler : public SIPHandler
00293 {
00294   PCLASSINFO(SIPPublishHandler, SIPHandler);
00295 
00296 public:
00297   SIPPublishHandler(SIPEndPoint & ep, 
00298                     const SIPSubscribe::Params & params,
00299                     const PString & body);
00300 
00301   virtual void SetBody(const PString & body) { m_body = body; }
00302 
00303   virtual SIPTransaction * CreateTransaction(OpalTransport &);
00304   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00305   virtual SIPEventPackage GetEventPackage() const { return m_parameters.m_eventPackage; }
00306 
00307 protected:
00308   SIPSubscribe::Params m_parameters;
00309   PString              m_body;
00310   PString              m_sipETag;
00311 };
00312 
00313 
00314 class SIPMessageHandler : public SIPHandler
00315 {
00316   PCLASSINFO(SIPMessageHandler, SIPHandler);
00317 public:
00318   SIPMessageHandler(SIPEndPoint & ep, const SIPMessage::Params & params);
00319 
00320   virtual void SetBody(const PString & body) { m_parameters.m_body = body; }
00321 
00322   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00323   virtual void OnFailed(SIP_PDU::StatusCodes);
00324   virtual void OnExpireTimeout(PTimer &, INT);
00325 
00326   const PString & GetLocalAddress() const { return m_parameters.m_localAddress; }
00327   const PString & GetIdentifier() const { return m_parameters.m_id; }
00328 
00329 protected:
00330   SIPMessage::Params m_parameters;
00331 };
00332 
00333 
00334 class SIPOptionsHandler : public SIPHandler
00335 {
00336   PCLASSINFO(SIPOptionsHandler, SIPHandler);
00337 public:
00338   SIPOptionsHandler(SIPEndPoint & ep, const SIPOptions::Params & params);
00339 
00340   virtual void SetBody(const PString & body) { m_parameters.m_body = body; }
00341 
00342   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00343   virtual void OnFailed(SIP_PDU::StatusCodes);
00344   virtual void OnFailed(const SIP_PDU & response);
00345   virtual void OnExpireTimeout(PTimer &, INT);
00346   virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
00347 
00348 protected:
00349   SIPOptions::Params m_parameters;
00350 };
00351 
00352 
00353 class SIPPingHandler : public SIPHandler
00354 {
00355   PCLASSINFO(SIPPingHandler, SIPHandler);
00356 public:
00357   SIPPingHandler(SIPEndPoint & ep, const PURL & to);
00358   virtual SIPTransaction * CreateTransaction (OpalTransport &);
00359 };
00360 
00361 
00365 class SIPHandlersList
00366 {
00367   public:
00370     void Append(SIPHandler * handler);
00371 
00376     void Remove(SIPHandler * handler);
00377 
00380     void Update(SIPHandler * handler);
00381 
00384     bool DeleteObjectsToBeRemoved()
00385       { return m_handlersList.DeleteObjectsToBeRemoved(); }
00386 
00390     PSafePtr<SIPHandler> GetFirstHandler(PSafetyMode mode = PSafeReference) const
00391       { return PSafePtr<SIPHandler>(m_handlersList, mode); }
00392 
00396     unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00397 
00401     PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
00402 
00406     PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
00407 
00411     PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
00412 
00420     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PURL & url, SIP_PDU::Methods meth, PSafetyMode m);
00421     PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PURL & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
00422 
00428     PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
00429 
00430   protected:
00431     void RemoveIndexes(SIPHandler * handler);
00432 
00433     PMutex m_extraMutex;
00434     PSafeList<SIPHandler> m_handlersList;
00435 
00436     typedef SIPHandler::IndexMap IndexMap;
00437     PSafePtr<SIPHandler> FindBy(IndexMap & by, const PString & key, PSafetyMode m);
00438 
00439     IndexMap m_byCallID;
00440     IndexMap m_byAorAndPackage;
00441     IndexMap m_byAuthIdAndRealm;
00442     IndexMap m_byAorUserAndRealm;
00443 };
00444 
00445 
00448 class SIPPresenceInfo : public OpalPresenceInfo
00449 {
00450 public:
00451   SIPPresenceInfo(
00452     const PString & personId = PString::Empty(),
00453     State state = Unchanged
00454   );
00455 
00456   // basic presence defined by RFC 3863
00457   PString m_tupleId;
00458   PString m_contact;
00459 
00460   // presence extensions defined by RFC 4480
00461   PStringArray m_activities;  // list of activities, seperated by newline
00462 
00463   // presence agent
00464   PString m_presenceAgent;
00465 
00466   PString AsXML() const;
00467 
00468   void PrintOn(ostream & strm) const;
00469   friend ostream & operator<<(ostream & strm, const SIPPresenceInfo & info) { info.PrintOn(strm); return strm; }
00470 
00471   static State FromSIPActivityString(const PString & str);
00472 
00473   static bool AsSIPActivityString(State state, PString & str);
00474   bool AsSIPActivityString(PString & str) const;
00475 
00476   PString m_personId;
00477 };
00478 
00479 
00482 struct SIPDialogNotification : public PObject
00483 {
00484   PCLASSINFO(SIPDialogNotification, PObject);
00485 
00486   enum States {
00487     Terminated,
00488     Trying,
00489     Proceeding,
00490     Early,
00491     Confirmed,
00492 
00493     FirstState = Terminated,
00494     LastState = Confirmed
00495   };
00496   friend States operator++(States & state) { return (state = (States)(state+1)); }
00497   friend States operator--(States & state) { return (state = (States)(state-1)); }
00498   static PString GetStateName(States state);
00499   PString GetStateName() const { return GetStateName(m_state); }
00500 
00501   enum Events {
00502     NoEvent = -1,
00503     Cancelled,
00504     Rejected,
00505     Replaced,
00506     LocalBye,
00507     RemoteBye,
00508     Error,
00509     Timeout,
00510 
00511     FirstEvent = Cancelled,
00512     LastEvent = Timeout
00513   };
00514   friend Events operator++(Events & evt) { return (evt = (Events)(evt+1)); }
00515   friend Events operator--(Events & evt) { return (evt = (Events)(evt-1)); }
00516   static PString GetEventName(Events state);
00517   PString GetEventName() const { return GetEventName(m_eventType); }
00518 
00519   enum Rendering {
00520     RenderingUnknown = -1,
00521     NotRenderingMedia,
00522     RenderingMedia
00523   };
00524 
00525   PString  m_entity;
00526   PString  m_dialogId;
00527   PString  m_callId;
00528   bool     m_initiator;
00529   States   m_state;
00530   Events   m_eventType;
00531   unsigned m_eventCode;
00532   struct Participant {
00533     Participant() : m_appearance(-1), m_byeless(false), m_rendering(RenderingUnknown) { }
00534     PString   m_URI;
00535     PString   m_dialogTag;
00536     PString   m_identity;
00537     PString   m_display;
00538     int       m_appearance;
00539     bool      m_byeless;
00540     Rendering m_rendering;
00541   } m_local, m_remote;
00542 
00543   SIPDialogNotification(const PString & entity = PString::Empty());
00544 
00545   void PrintOn(ostream & strm) const;
00546 };
00547 
00548 
00549 #endif // OPAL_SIP
00550 
00551 #endif // OPAL_SIP_HANDLERS_H

Generated on Sun Nov 21 20:20:50 2010 for OPAL by  doxygen 1.4.7