OPAL Version 3.10.2
sipep.h
Go to the documentation of this file.
00001 /*
00002  * sipep.h
00003  *
00004  * Session Initiation Protocol endpoint.
00005  *
00006  * Open Phone Abstraction Library (OPAL)
00007  * Formally known as the Open H323 project.
00008  *
00009  * Copyright (c) 2001 Equivalence Pty. Ltd.
00010  *
00011  * The contents of this file are subject to the Mozilla Public License
00012  * Version 1.0 (the "License"); you may not use this file except in
00013  * compliance with the License. You may obtain a copy of the License at
00014  * http://www.mozilla.org/MPL/
00015  *
00016  * Software distributed under the License is distributed on an "AS IS"
00017  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00018  * the License for the specific language governing rights and limitations
00019  * under the License.
00020  *
00021  * The Original Code is Open Phone Abstraction Library.
00022  *
00023  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00024  *
00025  * Contributor(s): ______________________________________.
00026  *
00027  * $Revision: 26270 $
00028  * $Author: rjongbloed $
00029  * $Date: 2011-08-03 03:29:01 -0500 (Wed, 03 Aug 2011) $
00030  */
00031 
00032 #ifndef OPAL_SIP_SIPEP_H
00033 #define OPAL_SIP_SIPEP_H
00034 
00035 #ifdef P_USE_PRAGMA
00036 #pragma interface
00037 #endif
00038 
00039 #ifndef _PTLIB_H
00040 #include <ptlib.h>
00041 #endif
00042 
00043 #include <opal/buildopts.h>
00044 
00045 #if OPAL_SIP
00046 
00047 #include <ptclib/threadpool.h>
00048 #include <opal/rtpep.h>
00049 #include <sip/sipcon.h>
00050 #include <sip/sippdu.h>
00051 #include <sip/handlers.h> 
00052 
00053 #if OPAL_HAS_SIPIM
00054 #include <im/sipim.h>
00055 #endif
00056 
00057 class SIPRegisterHandler;
00058 
00059 
00060 //
00061 //  provide flag so applications know if presence is available
00062 //
00063 #define OPAL_HAS_SIP_PRESENCE   1
00064 
00066 
00070 class SIPAuthInfo : public PObject
00071 {
00072   public:
00073     SIPAuthInfo()
00074     { }
00075 
00076     SIPAuthInfo(const PString & u, const PString & p)
00077     { username = u; password = p; }
00078     PString username;
00079     PString password;
00080 };
00081 
00083 
00086 class SIPEndPoint : public OpalRTPEndPoint
00087 {
00088   PCLASSINFO(SIPEndPoint, OpalRTPEndPoint);
00089 
00090   public:
00095     SIPEndPoint(
00096       OpalManager & manager,
00097       unsigned maxConnectionThreads = 10,
00098       unsigned maxHandlerThreads = 5
00099     );
00100 
00103     ~SIPEndPoint();
00105 
00112     virtual void ShutDown();
00113 
00117     virtual PString GetDefaultTransport() const;
00118 
00123     virtual PBoolean NewIncomingConnection(
00124       OpalTransport * transport  
00125     );
00126 
00156     virtual PSafePtr<OpalConnection> MakeConnection(
00157       OpalCall & call,                         
00158       const PString & party,                   
00159       void * userData,                         
00160       unsigned int options,                    
00161       OpalConnection::StringOptions * stringOptions  
00162     );
00163 
00182     virtual void OnReleased(
00183       OpalConnection & connection   
00184     );
00185 
00190     virtual PBoolean GarbageCollection();
00192 
00198     virtual SIPConnection * CreateConnection(
00199       OpalCall & call,                         
00200       const PString & token,                   
00201       void * userData,                         
00202       const SIPURL & destination,              
00203       OpalTransport * transport,               
00204       SIP_PDU * invite,                        
00205       unsigned int options = 0,                
00206       OpalConnection::StringOptions * stringOptions = NULL 
00207 
00208     );
00209     
00212     virtual PBoolean SetupTransfer(
00213       const PString & token,        
00214       const PString & callIdentity, 
00215       const PString & remoteParty,  
00216       void * userData = NULL        
00217     );
00218     
00222     virtual PBoolean ForwardConnection(
00223       SIPConnection & connection,     
00224       const PString & forwardParty    
00225     );
00226 
00237     bool ClearDialogContext(
00238       const PString & descriptor  
00239     );
00240     bool ClearDialogContext(
00241       SIPDialogContext & context  
00242     );
00244   
00247 
00251     OpalTransport * CreateTransport(
00252       const SIPURL & remoteURL,
00253       const PString & localInterface = PString::Empty()
00254     );
00255 
00256     virtual void HandlePDU(
00257       OpalTransport & transport
00258     );
00259 
00262     virtual PBoolean OnReceivedPDU(
00263       OpalTransport & transport,
00264       SIP_PDU * pdu
00265     );
00266 
00269     virtual bool OnReceivedConnectionlessPDU(
00270       OpalTransport & transport, 
00271       SIP_PDU * pdu
00272     );
00273 
00276     virtual void OnReceivedResponse(
00277       SIPTransaction & transaction,
00278       SIP_PDU & response
00279     );
00280 
00283     virtual PBoolean OnReceivedINVITE(
00284       OpalTransport & transport,
00285       SIP_PDU * pdu
00286     );
00287 
00290     virtual PBoolean OnReceivedNOTIFY(
00291       OpalTransport & transport,
00292       SIP_PDU & response
00293     );
00294 
00297     virtual PBoolean OnReceivedREGISTER(
00298       OpalTransport & transport, 
00299       SIP_PDU & pdu
00300     );
00301 
00304     virtual PBoolean OnReceivedSUBSCRIBE(
00305       OpalTransport & transport, 
00306       SIP_PDU & pdu
00307     );
00308 
00311     virtual bool OnReceivedMESSAGE(
00312       OpalTransport & transport,
00313       SIP_PDU & response
00314     );
00315 
00318     virtual bool OnReceivedOPTIONS(
00319       OpalTransport & transport,
00320       SIP_PDU & response
00321     );
00322     
00325     virtual void OnTransactionFailed(
00326       SIPTransaction & transaction
00327     );
00328     
00336     virtual void OnRTPStatistics(
00337       const SIPConnection & connection,  
00338       const RTP_Session & session         
00339     ) const;
00341  
00342 
00350     PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00351       const PString & token,     
00352       PSafetyMode mode = PSafeReadWrite,  
00353       SIP_PDU::StatusCodes * errorCode = NULL 
00354     );
00355 
00356     virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
00357 
00358 
00393     bool Register(
00394       const SIPRegister::Params & params, 
00395       PString & aor,                      
00396       bool asynchronous = true            
00397     );
00398 
00400     bool P_DEPRECATED Register(
00401       const PString & host,
00402       const PString & user = PString::Empty(),
00403       const PString & autName = PString::Empty(),
00404       const PString & password = PString::Empty(),
00405       const PString & authRealm = PString::Empty(),
00406       unsigned expire = 0,
00407       const PTimeInterval & minRetryTime = PMaxTimeInterval, 
00408       const PTimeInterval & maxRetryTime = PMaxTimeInterval
00409     );
00410 
00425     PBoolean IsRegistered(
00426       const PString & aor,          
00427       bool includeOffline = false   
00428     );
00429 
00439     bool Unregister(
00440       const PString & aor    
00441     );
00442 
00446     bool UnregisterAll();
00447 
00450     unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
00451 
00454     PStringList GetRegistrations(
00455       bool includeOffline = false   
00456     ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER); }
00457 
00459     struct RegistrationStatus {
00460       SIPRegisterHandler * m_handler;           
00461       PString              m_addressofRecord;   
00462       bool                 m_wasRegistering;    
00463       bool                 m_reRegistering;     
00464       SIP_PDU::StatusCodes m_reason;            
00465       OpalProductInfo      m_productInfo;       
00466       void               * m_userData;          
00467     };
00468 
00471     virtual void OnRegistrationStatus(
00472       const RegistrationStatus & status   
00473     );
00474 
00475     // For backward compatibility
00476     virtual void OnRegistrationStatus(
00477       const PString & aor,
00478       PBoolean wasRegistering,
00479       PBoolean reRegistering,
00480       SIP_PDU::StatusCodes reason
00481     );
00482 
00486     virtual void OnRegistrationFailed(
00487       const PString & aor,
00488       SIP_PDU::StatusCodes reason,
00489       PBoolean wasRegistering
00490     );
00491 
00495     virtual void OnRegistered(
00496       const PString & aor,
00497       PBoolean wasRegistering
00498     );
00499 
00500 
00540     bool Subscribe(
00541       const SIPSubscribe::Params & params, 
00542       PString & token,                     
00543       bool tokenIsAOR = true               
00544     );
00545 
00546     // For backward compatibility
00547     bool Subscribe(
00548       SIPSubscribe::PredefinedPackages eventPackage, 
00549       unsigned expire,                               
00550       const PString & aor                            
00551     );
00552 
00559     bool IsSubscribed(
00560       const PString & aor,           
00561       bool includeOffline = false    
00562     );
00563     bool IsSubscribed(
00564       const PString & eventPackage,  
00565       const PString & aor,           
00566       bool includeOffline = false    
00567     );
00568 
00578     bool Unsubscribe(
00579       const PString & aor,             
00580       bool invalidateNotifiers = false 
00581     );
00582     bool Unsubscribe(
00583       SIPSubscribe::PredefinedPackages eventPackage,  
00584       const PString & aor,                            
00585       bool invalidateNotifiers = false                
00586     );
00587     bool Unsubscribe(
00588       const PString & eventPackage,  
00589       const PString & aor,             
00590       bool invalidateNotifiers = false 
00591     );
00592 
00596     bool UnsubcribeAll(
00597       SIPSubscribe::PredefinedPackages eventPackage  
00598     );
00599     bool UnsubcribeAll(
00600       const PString & eventPackage  
00601     );
00602 
00605     unsigned GetSubscriptionCount(
00606       const SIPSubscribe::EventPackage & eventPackage  
00607     ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
00608 
00611     PStringList GetSubscriptions(
00612       const SIPSubscribe::EventPackage & eventPackage, 
00613       bool includeOffline = false   
00614     ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
00615 
00617     typedef SIPSubscribe::SubscriptionStatus SubscriptionStatus;
00618 
00621     virtual void OnSubscriptionStatus(
00622       const SubscriptionStatus & status   
00623     );
00624 
00628     virtual void OnSubscriptionStatus(
00629       const PString & eventPackage, 
00630       const SIPURL & uri,           
00631       bool wasSubscribing,          
00632       bool reSubscribing,           
00633       SIP_PDU::StatusCodes reason   
00634     );
00635 
00636     virtual void OnSubscriptionStatus(
00637       SIPSubscribeHandler & handler, 
00638       const SIPURL & uri,            
00639       bool wasSubscribing,           
00640       bool reSubscribing,            
00641       SIP_PDU::StatusCodes reason    
00642     );
00643 
00646     virtual bool CanNotify(
00647       const PString & eventPackage 
00648     );
00649 
00652     bool Notify(
00653       const SIPURL & targetAddress, 
00654       const PString & eventPackage, 
00655       const PObject & body          
00656     );
00657 
00658 
00661     virtual void OnDialogInfoReceived(
00662       const SIPDialogNotification & info  
00663     );
00664 
00665     void SendNotifyDialogInfo(
00666       const SIPDialogNotification & info  
00667     );
00668 
00669 
00672     virtual PBoolean Message(
00673       OpalIM & message
00674     );
00675 
00678     bool SendMESSAGE(
00679       SIPMessage::Params & params
00680     );
00681 
00684     virtual void OnMESSAGECompleted(
00685       const SIPMessage::Params & params,
00686       SIP_PDU::StatusCodes reason
00687     );
00688 
00689 
00692     virtual bool SendOPTIONS(
00693       const SIPOptions::Params & params
00694     );
00695 
00699     virtual void OnOptionsCompleted(
00700       const SIPOptions::Params & params,    
00701       const SIP_PDU & response              
00702     );
00703 
00704 
00708     bool Publish(
00709       const SIPSubscribe::Params & params,
00710       const PString & body,
00711       PString & aor
00712     );
00713     bool Publish(
00714       const PString & to,   
00715       const PString & body, 
00716       unsigned expire = 300 
00717     );
00718 
00721     PStringList GetPublications(
00722       const SIPSubscribe::EventPackage & eventPackage, 
00723       bool includeOffline = false   
00724     ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
00725 
00726 
00730     bool PublishPresence(
00731       const SIPPresenceInfo & info,  
00732       unsigned expire = 300          
00733     );
00734     
00737     virtual void OnPresenceInfoReceived (
00738       const SIPPresenceInfo & info  
00739     );
00740     virtual void OnPresenceInfoReceived (
00741       const PString & identity,
00742       const PString & basic,
00743       const PString & note
00744     );
00745 
00746 
00749     PBoolean Ping(
00750       const PURL & to
00751     );
00752 
00755     SIPConnection::PRACKMode GetDefaultPRACKMode() const { return m_defaultPrackMode; }
00756 
00759     void SetDefaultPRACKMode(SIPConnection::PRACKMode mode) { m_defaultPrackMode = mode; }
00760 
00761     void SetMIMEForm(PBoolean v) { mimeForm = v; }
00762     PBoolean GetMIMEForm() const { return mimeForm; }
00763 
00764     void SetMaxRetries(unsigned r) { maxRetries = r; }
00765     unsigned GetMaxRetries() const { return maxRetries; }
00766 
00767     void SetRetryTimeouts(
00768       const PTimeInterval & t1,
00769       const PTimeInterval & t2
00770     ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00771     const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00772     const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00773 
00774     void SetNonInviteTimeout(
00775       const PTimeInterval & t
00776     ) { nonInviteTimeout = t; }
00777     const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00778 
00779     void SetPduCleanUpTimeout(
00780       const PTimeInterval & t
00781     ) { pduCleanUpTimeout = t; }
00782     const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00783 
00784     void SetInviteTimeout(
00785       const PTimeInterval & t
00786     ) { inviteTimeout = t; }
00787     const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00788 
00789     void SetProgressTimeout(
00790       const PTimeInterval & t
00791     ) { m_progressTimeout = t; }
00792     const PTimeInterval & GetProgressTimeout() const { return m_progressTimeout; }
00793 
00794     void SetAckTimeout(
00795       const PTimeInterval & t
00796     ) { ackTimeout = t; }
00797     const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00798 
00799     void SetRegistrarTimeToLive(
00800       const PTimeInterval & t
00801     ) { registrarTimeToLive = t; }
00802     const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00803     
00804     void SetNotifierTimeToLive(
00805       const PTimeInterval & t
00806     ) { notifierTimeToLive = t; }
00807     const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00808     
00809     void SetNATBindingTimeout(
00810       const PTimeInterval & t
00811     ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00812     const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00813 
00814     void AddTransaction(
00815       SIPTransaction * transaction
00816     ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
00817 
00818     PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
00819     { return transactions.FindWithLock(transactionID, mode); }
00820     
00823     unsigned GetNextCSeq() { return ++lastSentCSeq; }
00824 
00827     bool GetAuthentication(const PString & authRealm, PString & user, PString & password); 
00828     
00831     virtual SIPURL GetRegisteredProxy(const SIPURL & remoteURL);
00832 
00838     virtual SIPURL GetRegisteredPartyName(const SIPURL & remoteURL, const OpalTransport & transport);
00839 
00840 
00843     virtual SIPURL GetDefaultRegisteredPartyName(const OpalTransport & transport);
00844     
00845 
00854     void AdjustToRegistration(
00855       const OpalTransport & transport,
00856       SIP_PDU & pdu
00857     );
00858 
00868     virtual SIPURL GetLocalURL(
00869        const OpalTransport & transport,             
00870        const PString & userName = PString::Empty()  
00871     );
00872     
00873     
00876     const SIPURL & GetProxy() const { return proxy; }
00877 
00878     
00881     void SetProxy(const SIPURL & url);
00882     
00883     
00886     void SetProxy(
00887       const PString & hostname,
00888       const PString & username,
00889       const PString & password
00890     );
00891 
00892     
00895     int GetDefaultAppearanceCode() const { return m_defaultAppearanceCode; }
00896 
00899     void SetDefaultAppearanceCode(int code) { m_defaultAppearanceCode = code; }
00900 
00907     virtual PString GetUserAgent() const;
00908         
00911     void SetUserAgent(const PString & str) { userAgentString = str; }
00912 
00913 
00916     virtual unsigned GetAllowedMethods() const;
00917 
00918 
00921     enum NATBindingRefreshMethod{
00922       None,
00923       Options,
00924       EmptyRequest,
00925       NumMethods
00926     };
00927 
00928 
00931     void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00932 
00933     virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00934 
00935     virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
00936 
00937     void UpdateHandlerIndexes(SIPHandler * handler) { activeSIPHandlers.Update(handler); }
00938 
00939   protected:
00940     PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00941     PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00942 
00943     SIPURL        proxy;
00944     PString       userAgentString;
00945 
00946     SIPConnection::PRACKMode m_defaultPrackMode;
00947 
00948     bool          mimeForm;
00949     unsigned      maxRetries;
00950     PTimeInterval retryTimeoutMin;   // T1
00951     PTimeInterval retryTimeoutMax;   // T2
00952     PTimeInterval nonInviteTimeout;  // T3
00953     PTimeInterval pduCleanUpTimeout; // T4
00954     PTimeInterval inviteTimeout;
00955     PTimeInterval m_progressTimeout;
00956     PTimeInterval ackTimeout;
00957     PTimeInterval registrarTimeToLive;
00958     PTimeInterval notifierTimeToLive;
00959     PTimeInterval natBindingTimeout;
00960 
00961     bool              m_shuttingDown;
00962     SIPHandlersList   activeSIPHandlers;
00963     PStringToString   m_receivedConnectionTokens;
00964 
00965     PSafeDictionary<PString, SIPTransaction> transactions;
00966 
00967     PTimer                  natBindingTimer;
00968     NATBindingRefreshMethod natMethod;
00969     PAtomicInteger          lastSentCSeq;
00970     int                     m_defaultAppearanceCode;
00971 
00972     std::map<PString, PSyncPoint> m_registrationComplete;
00973 
00974 
00975     // Thread pooling
00976     class SIP_Work
00977     {
00978       public:
00979         SIP_Work(SIPEndPoint & ep, SIP_PDU * pdu, const PString & token);
00980         virtual ~SIP_Work();
00981 
00982         virtual void Work();
00983 
00984         SIPEndPoint & m_endpoint;
00985         SIP_PDU     * m_pdu;
00986         PString       m_token;
00987     };
00988 
00989     class WorkThreadPool : public PQueuedThreadPool<SIP_Work>
00990     {
00991       public:
00992         WorkThreadPool(unsigned maxWorkers)
00993           : PQueuedThreadPool<SIP_Work>(maxWorkers)
00994         { }
00995         virtual WorkerThreadBase * CreateWorkerThread();
00996     } m_connectionThreadPool, m_handlerThreadPool;
00997 
00998 
00999     // Network interface checking
01000     enum {
01001       HighPriority = 80,
01002       LowPriority  = 30,
01003     };
01004     class InterfaceMonitor : public PInterfaceMonitorClient
01005     {
01006         PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
01007       public:
01008         InterfaceMonitor(SIPEndPoint & manager, PINDEX priority);
01009         
01010         virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
01011         virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
01012 
01013       protected:
01014         SIPEndPoint & m_endpoint;
01015     };
01016     InterfaceMonitor m_highPriorityMonitor;
01017     InterfaceMonitor m_lowPriorityMonitor;
01018 
01019     friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
01020     friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
01021 
01022     bool m_disableTrying;
01023 
01024     P_REMOVE_VIRTUAL_VOID(OnReceivedIntervalTooBrief(SIPTransaction &, SIP_PDU &));
01025     P_REMOVE_VIRTUAL_VOID(OnReceivedAuthenticationRequired(SIPTransaction &, SIP_PDU &));
01026     P_REMOVE_VIRTUAL_VOID(OnReceivedOK(SIPTransaction &, SIP_PDU &));
01027     P_REMOVE_VIRTUAL_VOID(OnMessageFailed(const SIPURL &, SIP_PDU::StatusCodes));
01028 
01029   public:
01030     struct ConnectionlessMessageInfo {
01031       ConnectionlessMessageInfo(OpalTransport & transport, SIP_PDU & pdu)
01032         : m_pdu(pdu), m_transport(transport), m_status(true)
01033       { }
01034 
01035       SIP_PDU & m_pdu;
01036       OpalTransport & m_transport;
01037       bool m_status;
01038     };
01039 
01040     typedef PNotifierTemplate<ConnectionlessMessageInfo &> ConnectionlessMessageNotifier;
01041     #define PDECLARE_ConnectionlessMessageNotifier(cls, fn) PDECLARE_NOTIFIER2(SIPEndPoint, cls, fn, SIPEndPoint::ConnectionlessMessageInfo &)
01042     #define PCREATE_ConnectionlessMessageNotifier(fn) PCREATE_NOTIFIER2(fn, SIPEndPoint::ConnectionlessMessageInfo &)
01043 
01044     void SetConnectionlessMessageNotifier(
01045       const ConnectionlessMessageNotifier & notifier
01046     )
01047     { m_onConnectionlessMessage = notifier; }
01048 
01049   protected:
01050     ConnectionlessMessageNotifier m_onConnectionlessMessage;
01051 };
01052 
01053 
01054 #endif // OPAL_SIP
01055 
01056 #endif // OPAL_SIP_SIPEP_H
01057 
01058 
01059 // End of File ///////////////////////////////////////////////////////////////