00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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 SIP_PDU::StatusCodes * reason = NULL
00397 );
00398
00399
00400 bool Register(
00401 const SIPRegister::Params & params,
00402 PString & aor,
00403 bool asynchronous
00404 );
00405
00407 bool P_DEPRECATED Register(
00408 const PString & host,
00409 const PString & user = PString::Empty(),
00410 const PString & autName = PString::Empty(),
00411 const PString & password = PString::Empty(),
00412 const PString & authRealm = PString::Empty(),
00413 unsigned expire = 0,
00414 const PTimeInterval & minRetryTime = PMaxTimeInterval,
00415 const PTimeInterval & maxRetryTime = PMaxTimeInterval
00416 );
00417
00432 PBoolean IsRegistered(
00433 const PString & aor,
00434 bool includeOffline = false
00435 );
00436
00446 bool Unregister(
00447 const PString & aor
00448 );
00449
00453 bool UnregisterAll();
00454
00457 unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
00458
00461 PStringList GetRegistrations(
00462 bool includeOffline = false
00463 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER); }
00464
00466 struct RegistrationStatus {
00467 SIPRegisterHandler * m_handler;
00468 PString m_addressofRecord;
00469 bool m_wasRegistering;
00470 bool m_reRegistering;
00471 SIP_PDU::StatusCodes m_reason;
00472 OpalProductInfo m_productInfo;
00473 void * m_userData;
00474 };
00475
00478 virtual void OnRegistrationStatus(
00479 const RegistrationStatus & status
00480 );
00481
00482
00483 virtual void OnRegistrationStatus(
00484 const PString & aor,
00485 PBoolean wasRegistering,
00486 PBoolean reRegistering,
00487 SIP_PDU::StatusCodes reason
00488 );
00489
00493 virtual void OnRegistrationFailed(
00494 const PString & aor,
00495 SIP_PDU::StatusCodes reason,
00496 PBoolean wasRegistering
00497 );
00498
00502 virtual void OnRegistered(
00503 const PString & aor,
00504 PBoolean wasRegistering
00505 );
00506
00507
00547 bool Subscribe(
00548 const SIPSubscribe::Params & params,
00549 PString & token,
00550 bool tokenIsAOR = true
00551 );
00552
00553
00554 bool Subscribe(
00555 SIPSubscribe::PredefinedPackages eventPackage,
00556 unsigned expire,
00557 const PString & aor
00558 );
00559
00566 bool IsSubscribed(
00567 const PString & aor,
00568 bool includeOffline = false
00569 );
00570 bool IsSubscribed(
00571 const PString & eventPackage,
00572 const PString & aor,
00573 bool includeOffline = false
00574 );
00575
00585 bool Unsubscribe(
00586 const PString & aor,
00587 bool invalidateNotifiers = false
00588 );
00589 bool Unsubscribe(
00590 SIPSubscribe::PredefinedPackages eventPackage,
00591 const PString & aor,
00592 bool invalidateNotifiers = false
00593 );
00594 bool Unsubscribe(
00595 const PString & eventPackage,
00596 const PString & aor,
00597 bool invalidateNotifiers = false
00598 );
00599
00603 bool UnsubcribeAll(
00604 SIPSubscribe::PredefinedPackages eventPackage
00605 );
00606 bool UnsubcribeAll(
00607 const PString & eventPackage
00608 );
00609
00612 unsigned GetSubscriptionCount(
00613 const SIPSubscribe::EventPackage & eventPackage
00614 ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
00615
00618 PStringList GetSubscriptions(
00619 const SIPSubscribe::EventPackage & eventPackage,
00620 bool includeOffline = false
00621 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
00622
00624 typedef SIPSubscribe::SubscriptionStatus SubscriptionStatus;
00625
00628 virtual void OnSubscriptionStatus(
00629 const SubscriptionStatus & status
00630 );
00631
00635 virtual void OnSubscriptionStatus(
00636 const PString & eventPackage,
00637 const SIPURL & uri,
00638 bool wasSubscribing,
00639 bool reSubscribing,
00640 SIP_PDU::StatusCodes reason
00641 );
00642
00643 virtual void OnSubscriptionStatus(
00644 SIPSubscribeHandler & handler,
00645 const SIPURL & uri,
00646 bool wasSubscribing,
00647 bool reSubscribing,
00648 SIP_PDU::StatusCodes reason
00649 );
00650
00653 virtual bool CanNotify(
00654 const PString & eventPackage
00655 );
00656
00659 bool Notify(
00660 const SIPURL & targetAddress,
00661 const PString & eventPackage,
00662 const PObject & body
00663 );
00664
00665
00668 virtual void OnDialogInfoReceived(
00669 const SIPDialogNotification & info
00670 );
00671
00672 void SendNotifyDialogInfo(
00673 const SIPDialogNotification & info
00674 );
00675
00676
00679 virtual PBoolean Message(
00680 OpalIM & message
00681 );
00682
00685 bool SendMESSAGE(
00686 SIPMessage::Params & params
00687 );
00688
00691 virtual void OnMESSAGECompleted(
00692 const SIPMessage::Params & params,
00693 SIP_PDU::StatusCodes reason
00694 );
00695
00696 struct ConnectionlessMessageInfo {
00697 ConnectionlessMessageInfo(OpalTransport & transport, SIP_PDU & pdu)
00698 : m_pdu(pdu), m_transport(transport), m_status(ResponseSent)
00699 { }
00700
00701 SIP_PDU & m_pdu;
00702 OpalTransport & m_transport;
00703 enum {
00704 NotHandled,
00705 SendOK,
00706 MethodNotAllowed,
00707 ResponseSent
00708 } m_status;
00709 };
00710
00711 typedef PNotifierTemplate<ConnectionlessMessageInfo &> ConnectionlessMessageNotifier;
00712 #define PDECLARE_ConnectionlessMessageNotifier(cls, fn) PDECLARE_NOTIFIER2(SIPEndPoint, cls, fn, SIPEndPoint::ConnectionlessMessageInfo &)
00713 #define PCREATE_ConnectionlessMessageNotifier(fn) PCREATE_NOTIFIER2(fn, SIPEndPoint::ConnectionlessMessageInfo &)
00714
00715 void SetConnectionlessMessageNotifier(
00716 const ConnectionlessMessageNotifier & notifier
00717 )
00718 { m_onConnectionlessMessage = notifier; }
00719
00720
00723 virtual bool SendOPTIONS(
00724 const SIPOptions::Params & params
00725 );
00726
00730 virtual void OnOptionsCompleted(
00731 const SIPOptions::Params & params,
00732 const SIP_PDU & response
00733 );
00734
00735
00739 bool Publish(
00740 const SIPSubscribe::Params & params,
00741 const PString & body,
00742 PString & aor
00743 );
00744 bool Publish(
00745 const PString & to,
00746 const PString & body,
00747 unsigned expire = 300
00748 );
00749
00752 PStringList GetPublications(
00753 const SIPSubscribe::EventPackage & eventPackage,
00754 bool includeOffline = false
00755 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
00756
00757
00761 bool PublishPresence(
00762 const SIPPresenceInfo & info,
00763 unsigned expire = 300
00764 );
00765
00768 virtual void OnPresenceInfoReceived (
00769 const SIPPresenceInfo & info
00770 );
00771 virtual void OnPresenceInfoReceived (
00772 const PString & identity,
00773 const PString & basic,
00774 const PString & note
00775 );
00776
00777
00780 PBoolean Ping(
00781 const PURL & to
00782 );
00783
00786 SIPConnection::PRACKMode GetDefaultPRACKMode() const { return m_defaultPrackMode; }
00787
00790 void SetDefaultPRACKMode(SIPConnection::PRACKMode mode) { m_defaultPrackMode = mode; }
00791
00792 void SetMIMEForm(PBoolean v) { mimeForm = v; }
00793 PBoolean GetMIMEForm() const { return mimeForm; }
00794
00795 void SetMaxRetries(unsigned r) { maxRetries = r; }
00796 unsigned GetMaxRetries() const { return maxRetries; }
00797
00798 void SetRetryTimeouts(
00799 const PTimeInterval & t1,
00800 const PTimeInterval & t2
00801 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00802 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00803 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00804
00805 void SetNonInviteTimeout(
00806 const PTimeInterval & t
00807 ) { nonInviteTimeout = t; }
00808 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00809
00810 void SetPduCleanUpTimeout(
00811 const PTimeInterval & t
00812 ) { pduCleanUpTimeout = t; }
00813 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00814
00815 void SetInviteTimeout(
00816 const PTimeInterval & t
00817 ) { inviteTimeout = t; }
00818 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00819
00820 void SetProgressTimeout(
00821 const PTimeInterval & t
00822 ) { m_progressTimeout = t; }
00823 const PTimeInterval & GetProgressTimeout() const { return m_progressTimeout; }
00824
00825 void SetAckTimeout(
00826 const PTimeInterval & t
00827 ) { ackTimeout = t; }
00828 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00829
00830 void SetRegistrarTimeToLive(
00831 const PTimeInterval & t
00832 ) { registrarTimeToLive = t; }
00833 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00834
00835 void SetNotifierTimeToLive(
00836 const PTimeInterval & t
00837 ) { notifierTimeToLive = t; }
00838 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00839
00840 void SetNATBindingTimeout(
00841 const PTimeInterval & t
00842 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00843 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00844
00845 void AddTransaction(
00846 SIPTransaction * transaction
00847 ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
00848
00849 PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
00850 { return transactions.FindWithLock(transactionID, mode); }
00851
00854 unsigned GetNextCSeq() { return ++lastSentCSeq; }
00855
00861 void SetRegisteredUserMode(bool v) { m_registeredUserMode = v; }
00862 bool GetRegisteredYserMode() const { return m_registeredUserMode; }
00863
00866 SIP_PDU::StatusCodes HandleAuthentication(
00867 SIPAuthentication * & authentication,
00868 unsigned & authenticateErrors,
00869 const SIP_PDU & response,
00870 const SIPURL & proxyOverride,
00871 const PString & username,
00872 const PString & password
00873 );
00874
00877 virtual SIPURL GetRegisteredProxy(const SIPURL & remoteURL);
00878
00884 virtual SIPURL GetRegisteredPartyName(const SIPURL & remoteURL, const OpalTransport & transport);
00885
00886
00889 virtual SIPURL GetDefaultRegisteredPartyName(const OpalTransport & transport);
00890
00891
00900 void AdjustToRegistration(
00901 SIP_PDU & pdu,
00902 const OpalTransport & transport,
00903 const SIPConnection * connection
00904 );
00905
00915 virtual SIPURL GetLocalURL(
00916 const OpalTransport & transport,
00917 const PString & userName = PString::Empty()
00918 );
00919
00920
00923 const SIPURL & GetProxy() const { return proxy; }
00924
00925
00928 void SetProxy(const SIPURL & url);
00929
00930
00933 void SetProxy(
00934 const PString & hostname,
00935 const PString & username,
00936 const PString & password
00937 );
00938
00939
00942 int GetDefaultAppearanceCode() const { return m_defaultAppearanceCode; }
00943
00946 void SetDefaultAppearanceCode(int code) { m_defaultAppearanceCode = code; }
00947
00954 virtual PString GetUserAgent() const;
00955
00958 void SetUserAgent(const PString & str) { userAgentString = str; }
00959
00960
00963 virtual unsigned GetAllowedMethods() const;
00964
00965
00968 enum NATBindingRefreshMethod{
00969 None,
00970 Options,
00971 EmptyRequest,
00972 NumMethods
00973 };
00974
00975
00978 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00979
00980 virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00981
00982 virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
00983
00984 void UpdateHandlerIndexes(SIPHandler * handler) { activeSIPHandlers.Update(handler); }
00985
00986 protected:
00987 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00988 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00989
00990 SIPURL proxy;
00991 PString userAgentString;
00992
00993 SIPConnection::PRACKMode m_defaultPrackMode;
00994
00995 bool mimeForm;
00996 unsigned maxRetries;
00997 PTimeInterval retryTimeoutMin;
00998 PTimeInterval retryTimeoutMax;
00999 PTimeInterval nonInviteTimeout;
01000 PTimeInterval pduCleanUpTimeout;
01001 PTimeInterval inviteTimeout;
01002 PTimeInterval m_progressTimeout;
01003 PTimeInterval ackTimeout;
01004 PTimeInterval registrarTimeToLive;
01005 PTimeInterval notifierTimeToLive;
01006 PTimeInterval natBindingTimeout;
01007 bool m_registeredUserMode;
01008
01009 bool m_shuttingDown;
01010 SIPHandlersList activeSIPHandlers;
01011 PStringToString m_receivedConnectionTokens;
01012
01013 PSafeDictionary<PString, SIPTransaction> transactions;
01014
01015 PTimer natBindingTimer;
01016 NATBindingRefreshMethod natMethod;
01017 PAtomicInteger lastSentCSeq;
01018 int m_defaultAppearanceCode;
01019
01020 struct RegistrationCompletion {
01021 PSyncPoint m_sync;
01022 SIP_PDU::StatusCodes m_reason;
01023 RegistrationCompletion() : m_reason(SIP_PDU::Information_Trying) { }
01024 };
01025 std::map<PString, RegistrationCompletion> m_registrationComplete;
01026
01027 ConnectionlessMessageNotifier m_onConnectionlessMessage;
01028
01029
01030 class SIP_Work
01031 {
01032 public:
01033 SIP_Work(SIPEndPoint & ep, SIP_PDU * pdu, const PString & token);
01034 virtual ~SIP_Work();
01035
01036 virtual void Work();
01037
01038 SIPEndPoint & m_endpoint;
01039 SIP_PDU * m_pdu;
01040 PString m_token;
01041 };
01042
01043 class WorkThreadPool : public PQueuedThreadPool<SIP_Work>
01044 {
01045 public:
01046 WorkThreadPool(unsigned maxWorkers)
01047 : PQueuedThreadPool<SIP_Work>(maxWorkers)
01048 { }
01049 virtual WorkerThreadBase * CreateWorkerThread();
01050 } m_connectionThreadPool, m_handlerThreadPool;
01051
01052
01053
01054 enum {
01055 HighPriority = 80,
01056 LowPriority = 30,
01057 };
01058 class InterfaceMonitor : public PInterfaceMonitorClient
01059 {
01060 PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
01061 public:
01062 InterfaceMonitor(SIPEndPoint & manager, PINDEX priority);
01063
01064 virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
01065 virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
01066
01067 protected:
01068 SIPEndPoint & m_endpoint;
01069 };
01070 InterfaceMonitor m_highPriorityMonitor;
01071 InterfaceMonitor m_lowPriorityMonitor;
01072
01073 friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
01074 friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
01075
01076 bool m_disableTrying;
01077
01078 P_REMOVE_VIRTUAL_VOID(OnReceivedIntervalTooBrief(SIPTransaction &, SIP_PDU &));
01079 P_REMOVE_VIRTUAL_VOID(OnReceivedAuthenticationRequired(SIPTransaction &, SIP_PDU &));
01080 P_REMOVE_VIRTUAL_VOID(OnReceivedOK(SIPTransaction &, SIP_PDU &));
01081 P_REMOVE_VIRTUAL_VOID(OnMessageFailed(const SIPURL &, SIP_PDU::StatusCodes));
01082 };
01083
01084
01085 #endif // OPAL_SIP
01086
01087 #endif // OPAL_SIP_SIPEP_H
01088
01089
01090