# 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: 21062 $
    00028  * $Author: rjongbloed $
    00029  * $Date: 2008-09-18 11:41:40 +0000 (Thu, 18 Sep 2008) $
    00030  */
    00031
    00032 #ifndef __OPAL_SIPEP_H
    00033 #define __OPAL_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 #include <ptclib/sockagg.h>
    00046 #include <opal/rtpep.h>
    00047 #include <sip/sipcon.h>
    00048 #include <sip/sippdu.h>
    00049 #include <sip/handlers.h>
    00050
    00051
    00052 class SIPRegisterHandler;
    00053
    00054
    00055 //
    00056 //  provide flag so applications know if presence is available
    00057 //
    00058 #define OPAL_HAS_SIP_PRESENCE   1
    00059
    00061
    00065 class SIPAuthInfo : public PObject
    00066 {
    00067   public:
    00068     SIPAuthInfo()
    00069     { }
    00070
    00071     SIPAuthInfo(const PString & u, const PString & p)
    00072     { username = u; password = p; }
    00073     PString username;
    00074     PString password;
    00075 };
    00076
    00078
    00081 class SIPEndPoint : public OpalRTPEndPoint
    00082 {
    00083   PCLASSINFO(SIPEndPoint, OpalRTPEndPoint);
    00084
    00085   public:
    00090     SIPEndPoint(
    00091       OpalManager & manager
    00092     );
    00093
    00096     ~SIPEndPoint();
    00098
    00105     virtual void ShutDown();
    00106
    00110     virtual PString GetDefaultTransport() const;
    00111
    00116     virtual PBoolean NewIncomingConnection(
    00117       OpalTransport * transport
    00118     );
    00119
    00149     virtual PBoolean MakeConnection(
    00150       OpalCall & call,
    00151       const PString & party,
    00152       void * userData,
    00153       unsigned int options,
    00154       OpalConnection::StringOptions * stringOptions
    00155     );
    00156
    00161     virtual PBoolean GarbageCollection();
    00163
    00169     virtual SIPConnection * CreateConnection(
    00170       OpalCall & call,
    00171       const PString & token,
    00172       void * userData,
    00173       const SIPURL & destination,
    00174       OpalTransport * transport,
    00175       SIP_PDU * invite,
    00176       unsigned int options = 0,
    00177       OpalConnection::StringOptions * stringOptions = NULL
    00178
    00179     );
    00180
    00183     virtual PBoolean SetupTransfer(
    00184       const PString & token,
    00185       const PString & callIdentity,
    00186       const PString & remoteParty,
    00187       void * userData = NULL
    00188     );
    00189
    00193     virtual PBoolean ForwardConnection(
    00194       SIPConnection & connection,
    00195       const PString & forwardParty
    00196     );
    00197
    00199
    00202
    00206     OpalTransport * CreateTransport(
    00207       const OpalTransportAddress & remoteAddress,
    00208       const OpalTransportAddress & localAddress = OpalTransportAddress()
    00209     );
    00210
    00211     virtual void HandlePDU(
    00212       OpalTransport & transport
    00213     );
    00214
    00217     virtual PBoolean OnReceivedPDU(
    00218       OpalTransport & transport,
    00219       SIP_PDU * pdu
    00220     );
    00221
    00224     virtual bool OnReceivedConnectionlessPDU(
    00225       OpalTransport & transport,
    00226       SIP_PDU * pdu
    00227     );
    00228
    00231     virtual void OnReceivedResponse(
    00232       SIPTransaction & transaction,
    00233       SIP_PDU & response
    00234     );
    00235
    00238     virtual PBoolean OnReceivedINVITE(
    00239       OpalTransport & transport,
    00240       SIP_PDU * pdu
    00241     );
    00242
    00245     virtual void OnReceivedIntervalTooBrief(
    00246       SIPTransaction & transaction,
    00247       SIP_PDU & response)
    00248     ;
    00249
    00252     virtual void OnReceivedAuthenticationRequired(
    00253       SIPTransaction & transaction,
    00254       SIP_PDU & response
    00255     );
    00256
    00260     virtual void OnReceivedOK(
    00261       SIPTransaction & transaction,
    00262       SIP_PDU & response
    00263     );
    00264
    00267     virtual PBoolean OnReceivedNOTIFY(
    00268       OpalTransport & transport,
    00269       SIP_PDU & response
    00270     );
    00271
    00274     virtual PBoolean OnReceivedREGISTER(
    00275       OpalTransport & transport,
    00276       SIP_PDU & pdu
    00277     );
    00278
    00281     virtual PBoolean OnReceivedSUBSCRIBE(
    00282       OpalTransport & transport,
    00283       SIP_PDU & pdu
    00284     );
    00285
    00288     virtual bool OnReceivedMESSAGE(
    00289       OpalTransport & transport,
    00290       SIP_PDU & response
    00291     );
    00292
    00295     virtual bool OnReceivedOPTIONS(
    00296       OpalTransport & transport,
    00297       SIP_PDU & response
    00298     );
    00299
    00302     virtual void OnTransactionFailed(
    00303       SIPTransaction & transaction
    00304     );
    00305
    00313     virtual void OnRTPStatistics(
    00314       const SIPConnection & connection,
    00315       const RTP_Session & session
    00316     ) const;
    00318
    00319
    00324     PSafePtr<SIPConnection> GetSIPConnectionWithLock(
    00325       const PString & token,
    00326       PSafetyMode mode = PSafeReadWrite
    00327     ) { return PSafePtrCast<OpalConnection, SIPConnection>(GetConnectionWithLock(token, mode)); }
    00328
    00329     virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
    00330
    00331
    00334     virtual void OnMessageReceived (const SIPURL & from,
    00335                     const PString & body);
    00336
    00337
    00351     bool Register(
    00352       const SIPRegister::Params & params
    00353     );
    00354
    00356     bool Register(
    00357       const PString & host,
    00358       const PString & user = PString::Empty(),
    00359       const PString & autName = PString::Empty(),
    00360       const PString & password = PString::Empty(),
    00361       const PString & authRealm = PString::Empty(),
    00362       unsigned expire = 0,
    00363       const PTimeInterval & minRetryTime = PMaxTimeInterval,
    00364       const PTimeInterval & maxRetryTime = PMaxTimeInterval
    00365     );
    00366
    00371     bool Unregister(const PString & aor);
    00372
    00375     bool UnregisterAll();
    00376
    00380     PBoolean IsRegistered(const PString & aor);
    00381
    00384     unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
    00385
    00387     struct RegistrationStatus {
    00388       PString              m_addressofRecord;
    00389       bool                 m_wasRegistering;
    00390       bool                 m_reRegistering;
    00391       SIP_PDU::StatusCodes m_reason;
    00392       OpalProductInfo      m_productInfo;
    00393     };
    00394
    00397     virtual void OnRegistrationStatus(
    00398       const RegistrationStatus & status
    00399     );
    00400
    00401     // For backward compatibility
    00402     virtual void OnRegistrationStatus(
    00403       const PString & aor,
    00404       PBoolean wasRegistering,
    00405       PBoolean reRegistering,
    00406       SIP_PDU::StatusCodes reason
    00407     );
    00408
    00412     virtual void OnRegistrationFailed(
    00413       const PString & aor,
    00414       SIP_PDU::StatusCodes reason,
    00415       PBoolean wasRegistering
    00416     );
    00417
    00421     virtual void OnRegistered(
    00422       const PString & aor,
    00423       PBoolean wasRegistering
    00424     );
    00425
    00426
    00430     bool Subscribe(
    00431       SIPSubscribe::PredefinedPackages eventPackage,
    00432       unsigned expire,
    00433       const PString & to
    00434     );
    00435     bool Subscribe(
    00436       const SIPSubscribe::Params & params
    00437     );
    00438
    00439
    00440     bool Unsubscribe(
    00441       SIPSubscribe::PredefinedPackages eventPackage,
    00442       const PString & to
    00443     );
    00444     bool Unsubscribe(
    00445       const PString & eventPackage,
    00446       const PString & to
    00447     );
    00448
    00451     bool UnsubcribeAll(
    00452       SIPSubscribe::PredefinedPackages eventPackage
    00453     );
    00454     bool UnsubcribeAll(
    00455       const PString & eventPackage
    00456     );
    00457
    00461     PBoolean IsSubscribed(
    00462       const PString & eventPackage,
    00463       const PString & to
    00464     );
    00465
    00468     unsigned GetSubscriptionCount(const PString & eventPackage) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
    00469
    00472     virtual void OnSubscriptionStatus(
    00473       const PString & eventPackage,
    00474       const SIPURL & uri,
    00475       bool wasSubscribing,
    00476       bool reSubscribing,
    00477       SIP_PDU::StatusCodes reason
    00478     );
    00479
    00480
    00483     PBoolean Message (
    00484       const PString & to,
    00485       const PString & body
    00486     );
    00487
    00488
    00492     PBoolean Publish(
    00493       const PString & to,
    00494       const PString & body,
    00495       unsigned expire = 0
    00496     );
    00497
    00498
    00501     PBoolean Ping(
    00502       const PString & to
    00503     );
    00504
    00507     virtual void OnPresenceInfoReceived (
    00508       const PString & user,
    00509       const PString & basic,
    00510       const PString & note
    00511     );
    00512
    00517     virtual void OnMessageFailed(
    00518       const SIPURL & messageUrl,
    00519       SIP_PDU::StatusCodes reason);
    00520
    00521
    00522     void SetMIMEForm(PBoolean v) { mimeForm = v; }
    00523     PBoolean GetMIMEForm() const { return mimeForm; }
    00524
    00525     void SetMaxRetries(unsigned r) { maxRetries = r; }
    00526     unsigned GetMaxRetries() const { return maxRetries; }
    00527
    00528     void SetRetryTimeouts(
    00529       const PTimeInterval & t1,
    00530       const PTimeInterval & t2
    00531     ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
    00532     const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
    00533     const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
    00534
    00535     void SetNonInviteTimeout(
    00536       const PTimeInterval & t
    00537     ) { nonInviteTimeout = t; }
    00538     const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
    00539
    00540     void SetPduCleanUpTimeout(
    00541       const PTimeInterval & t
    00542     ) { pduCleanUpTimeout = t; }
    00543     const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
    00544
    00545     void SetInviteTimeout(
    00546       const PTimeInterval & t
    00547     ) { inviteTimeout = t; }
    00548     const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
    00549
    00550     void SetAckTimeout(
    00551       const PTimeInterval & t
    00552     ) { ackTimeout = t; }
    00553     const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
    00554
    00555     void SetRegistrarTimeToLive(
    00556       const PTimeInterval & t
    00557     ) { registrarTimeToLive = t; }
    00558     const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
    00559
    00560     void SetNotifierTimeToLive(
    00561       const PTimeInterval & t
    00562     ) { notifierTimeToLive = t; }
    00563     const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
    00564
    00565     void SetNATBindingTimeout(
    00566       const PTimeInterval & t
    00567     ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
    00568     const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
    00569
    00570     void AddTransaction(
    00571       SIPTransaction * transaction
    00572     ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
    00573
    00574     PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
    00575     { return transactions.FindWithLock(transactionID, mode); }
    00576
    00579     unsigned GetNextCSeq() { return ++lastSentCSeq; }
    00580
    00581
    00584     PBoolean GetAuthentication(const PString & authRealm, PString & realm, PString & user, PString & password);
    00585
    00591     virtual SIPURL GetRegisteredPartyName(const SIPURL &);
    00592
    00593
    00596     virtual SIPURL GetDefaultRegisteredPartyName();
    00597
    00598
    00610     SIPURL GetContactURL(const OpalTransport &transport, const PString & userName, const PString & host);
    00611
    00612
    00622     virtual SIPURL GetLocalURL(
    00623        const OpalTransport & transport,
    00624        const PString & userName = PString::Empty()
    00625     );
    00626
    00627
    00630     const SIPURL & GetProxy() const { return proxy; }
    00631
    00632
    00635     void SetProxy(const SIPURL & url);
    00636
    00637
    00640     void SetProxy(
    00641       const PString & hostname,
    00642       const PString & username,
    00643       const PString & password
    00644     );
    00645
    00646
    00653     virtual PString GetUserAgent() const;
    00654
    00657     void SetUserAgent(const PString & str) { userAgentString = str; }
    00658
    00659
    00662     virtual unsigned GetAllowedMethods() const;
    00663
    00664     PBoolean SendResponse(
    00665       SIP_PDU::StatusCodes code,
    00666       OpalTransport & transport,
    00667       SIP_PDU & pdu
    00668     );
    00669
    00672     enum NATBindingRefreshMethod{
    00673       None,
    00674       Options,
    00675       EmptyRequest,
    00676       NumMethods
    00677     };
    00678
    00679
    00682     void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
    00683
    00684     virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
    00685
    00686   protected:
    00687     PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
    00688     PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
    00689
    00690     SIPURL        proxy;
    00691     PString       userAgentString;
    00692
    00693     bool          mimeForm;
    00694     unsigned      maxRetries;
    00695     PTimeInterval retryTimeoutMin;   // T1
    00696     PTimeInterval retryTimeoutMax;   // T2
    00697     PTimeInterval nonInviteTimeout;  // T3
    00698     PTimeInterval pduCleanUpTimeout; // T4
    00699     PTimeInterval inviteTimeout;
    00700     PTimeInterval ackTimeout;
    00701     PTimeInterval registrarTimeToLive;
    00702     PTimeInterval notifierTimeToLive;
    00703     PTimeInterval natBindingTimeout;
    00704
    00705     SIPHandlersList   activeSIPHandlers;
    00706
    00707     PSafeDictionary<PString, SIPTransaction> transactions;
    00708
    00709     PTimer                  natBindingTimer;
    00710     NATBindingRefreshMethod natMethod;
    00711
    00712     PAtomicInteger          lastSentCSeq;
    00713
    00714     struct SIP_PDU_Work
    00715     {
    00716       SIP_PDU_Work()
    00717       { ep = NULL; pdu = NULL; }
    00718
    00719       SIPEndPoint * ep;
    00720       SIP_PDU * pdu;
    00721       PString callID;
    00722     };
    00723
    00724     typedef std::queue<SIP_PDU_Work *> SIP_PDUWorkQueue;
    00725
    00726     class SIP_PDU_Thread : public PThreadPoolWorkerBase
    00727     {
    00728       public:
    00729         SIP_PDU_Thread(PThreadPoolBase & _pool);
    00730         unsigned GetWorkSize() const;
    00731         void OnAddWork(SIP_PDU_Work * work);
    00732         void OnRemoveWork(SIP_PDU_Work *);
    00733         void Shutdown();
    00734         void Main();
    00735
    00736       protected:
    00737         PMutex mutex;
    00738         PSyncPoint sync;
    00739         SIP_PDUWorkQueue pduQueue;
    00740     };
    00741
    00742     typedef PThreadPool<SIP_PDU_Work, SIP_PDU_Thread> SIPMainThreadPool;
    00743     SIPMainThreadPool threadPool;
    00744
    00745     class InterfaceMonitor : public PInterfaceMonitorClient
    00746     {
    00747         PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
    00748       public:
    00749         InterfaceMonitor(SIPEndPoint & manager);
    00750
    00751         virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
    00752         virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
    00753
    00754     protected:
    00755         SIPEndPoint & m_endpoint;
    00756     } m_interfaceMonitor;
    00757
    00758     friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
    00759     friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
    00760 };
    00761
    00762 #endif // __OPAL_SIPEP_H
    00763
    00764
    00765 // End of File ///////////////////////////////////////////////////////////////