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 <rtp/rtpep.h>
00048 #include <sip/sipcon.h>
00049 #include <sip/handlers.h>
00050
00051
00053
00056 class SIPEndPoint : public OpalRTPEndPoint
00057 {
00058 PCLASSINFO(SIPEndPoint, OpalRTPEndPoint);
00059 public:
00064 SIPEndPoint(
00065 OpalManager & manager,
00066 unsigned maxThreads = 15
00067 );
00068
00071 ~SIPEndPoint();
00073
00080 virtual void ShutDown();
00081
00085 virtual PString GetDefaultTransport() const;
00086
00089 virtual WORD GetDefaultSignalPort() const;
00090
00095 virtual PStringList GetNetworkURIs(
00096 const PString & name
00097 ) const;
00098
00101 virtual void NewIncomingConnection(
00102 OpalListener & listener,
00103 const OpalTransportPtr & transport
00104 );
00105
00135 virtual PSafePtr<OpalConnection> MakeConnection(
00136 OpalCall & call,
00137 const PString & party,
00138 void * userData,
00139 unsigned int options,
00140 OpalConnection::StringOptions * stringOptions
00141 );
00142
00161 virtual void OnReleased(
00162 OpalConnection & connection
00163 );
00164
00172 virtual void OnConferenceStatusChanged(
00173 OpalEndPoint & endpoint,
00174 const PString & uri,
00175 OpalConferenceState::ChangeType change
00176 );
00177
00182 virtual PBoolean GarbageCollection();
00184
00190 virtual SIPConnection * CreateConnection(
00191 const SIPConnection::Init & init
00192
00193 );
00194
00197 virtual PBoolean SetupTransfer(
00198 const PString & token,
00199 const PString & callIdentity,
00200 const PString & remoteParty,
00201 void * userData = NULL
00202 );
00203
00207 virtual PBoolean ForwardConnection(
00208 SIPConnection & connection,
00209 const PString & forwardParty
00210 );
00211
00222 bool ClearDialogContext(
00223 const PString & descriptor
00224 );
00225 bool ClearDialogContext(
00226 SIPDialogContext & context
00227 );
00229
00232
00237 OpalTransportPtr GetTransport(
00238 const SIPTransactionOwner & transactor,
00239 SIP_PDU::StatusCodes & reason
00240 );
00241
00242 virtual void HandlePDU(
00243 const OpalTransportPtr & transport
00244 );
00245
00248 virtual bool OnReceivedPDU(
00249 SIP_PDU * pdu
00250 );
00251
00254 virtual bool OnReceivedConnectionlessPDU(
00255 SIP_PDU * pdu
00256 );
00257
00260 virtual void OnReceivedResponse(
00261 SIPTransaction & transaction,
00262 SIP_PDU & response
00263 );
00264
00267 virtual bool OnReceivedINVITE(
00268 SIP_PDU * pdu
00269 );
00270
00273 virtual bool OnReceivedNOTIFY(
00274 SIP_PDU & request
00275 );
00276
00279 virtual bool OnReceivedREGISTER(
00280 SIP_PDU & request
00281 );
00282
00285 virtual bool OnReceivedSUBSCRIBE(
00286 SIP_PDU & request,
00287 SIPDialogContext * dialog
00288 );
00289
00292 virtual bool OnReceivedMESSAGE(
00293 SIP_PDU & request
00294 );
00295
00298 virtual bool OnReceivedOPTIONS(
00299 SIP_PDU & request
00300 );
00301
00304 virtual void OnTransactionFailed(
00305 SIPTransaction & transaction
00306 );
00308
00309
00317 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00318 const PString & token,
00319 PSafetyMode mode = PSafeReadWrite,
00320 SIP_PDU::StatusCodes * errorCode = NULL
00321 );
00322
00323 virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
00324
00325
00360 bool Register(
00361 const SIPRegister::Params & params,
00362 PString & aor,
00363 SIP_PDU::StatusCodes * reason = NULL
00364 );
00365
00366
00367 bool Register(
00368 const SIPRegister::Params & params,
00369 PString & aor,
00370 bool asynchronous
00371 );
00372
00374 bool P_DEPRECATED Register(
00375 const PString & host,
00376 const PString & user = PString::Empty(),
00377 const PString & autName = PString::Empty(),
00378 const PString & password = PString::Empty(),
00379 const PString & authRealm = PString::Empty(),
00380 unsigned expire = 0,
00381 const PTimeInterval & minRetryTime = PMaxTimeInterval,
00382 const PTimeInterval & maxRetryTime = PMaxTimeInterval
00383 );
00384
00399 PBoolean IsRegistered(
00400 const PString & aor,
00401 bool includeOffline = false
00402 );
00403
00413 bool Unregister(
00414 const PString & aor
00415 );
00416
00420 bool UnregisterAll();
00421
00424 unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
00425
00428 PStringList GetRegistrations(
00429 bool includeOffline = false
00430 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER); }
00431
00433 struct RegistrationStatus {
00434 SIPRegisterHandler * m_handler;
00435 PString m_addressofRecord;
00436 bool m_wasRegistering;
00437 bool m_reRegistering;
00438 SIP_PDU::StatusCodes m_reason;
00439 OpalProductInfo m_productInfo;
00440 void * m_userData;
00441 };
00442
00446 bool GetRegistrationStatus(
00447 const PString & token,
00448 RegistrationStatus & status
00449 );
00450
00453 virtual void OnRegistrationStatus(
00454 const RegistrationStatus & status
00455 );
00456
00457
00458 virtual void OnRegistrationStatus(
00459 const PString & aor,
00460 PBoolean wasRegistering,
00461 PBoolean reRegistering,
00462 SIP_PDU::StatusCodes reason
00463 );
00464
00468 virtual void OnRegistrationFailed(
00469 const PString & aor,
00470 SIP_PDU::StatusCodes reason,
00471 PBoolean wasRegistering
00472 );
00473
00477 virtual void OnRegistered(
00478 const PString & aor,
00479 PBoolean wasRegistering
00480 );
00481
00482
00522 bool Subscribe(
00523 const SIPSubscribe::Params & params,
00524 PString & token,
00525 bool tokenIsAOR = true
00526 );
00527
00528
00529 bool Subscribe(
00530 SIPSubscribe::PredefinedPackages eventPackage,
00531 unsigned expire,
00532 const PString & aor
00533 );
00534
00541 bool IsSubscribed(
00542 const PString & aor,
00543 bool includeOffline = false
00544 );
00545 bool IsSubscribed(
00546 const PString & eventPackage,
00547 const PString & aor,
00548 bool includeOffline = false
00549 );
00550
00560 bool Unsubscribe(
00561 const PString & aor,
00562 bool invalidateNotifiers = false
00563 );
00564 bool Unsubscribe(
00565 SIPSubscribe::PredefinedPackages eventPackage,
00566 const PString & aor,
00567 bool invalidateNotifiers = false
00568 );
00569 bool Unsubscribe(
00570 const PString & eventPackage,
00571 const PString & aor,
00572 bool invalidateNotifiers = false
00573 );
00574
00578 bool UnsubcribeAll(
00579 SIPSubscribe::PredefinedPackages eventPackage
00580 );
00581 bool UnsubcribeAll(
00582 const PString & eventPackage
00583 );
00584
00587 unsigned GetSubscriptionCount(
00588 const SIPSubscribe::EventPackage & eventPackage
00589 ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
00590
00593 PStringList GetSubscriptions(
00594 const SIPSubscribe::EventPackage & eventPackage,
00595 bool includeOffline = false
00596 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
00597
00599 typedef SIPSubscribe::SubscriptionStatus SubscriptionStatus;
00600
00604 bool GetSubscriptionStatus(
00605 const PString & token,
00606 const PString & eventPackage,
00607 SubscriptionStatus & status
00608 );
00609
00612 virtual void OnSubscriptionStatus(
00613 const SubscriptionStatus & status
00614 );
00615
00619 virtual void OnSubscriptionStatus(
00620 const PString & eventPackage,
00621 const SIPURL & uri,
00622 bool wasSubscribing,
00623 bool reSubscribing,
00624 SIP_PDU::StatusCodes reason
00625 );
00626
00627 virtual void OnSubscriptionStatus(
00628 SIPSubscribeHandler & handler,
00629 const SIPURL & uri,
00630 bool wasSubscribing,
00631 bool reSubscribing,
00632 SIP_PDU::StatusCodes reason
00633 );
00634
00642 enum CanNotifyResult {
00643 CannotNotify,
00644 CanNotifyImmediate,
00645 CanNotifyDeferrred
00646 };
00647
00650 virtual CanNotifyResult CanNotify(
00651 const PString & eventPackage,
00652 const SIPURL & aor
00653 );
00654
00655
00656 virtual bool CanNotify(const PString & eventPackage);
00657
00660 bool Notify(
00661 const SIPURL & targetAddress,
00662 const PString & eventPackage,
00663 const PObject & body
00664 );
00665
00666
00669 virtual void OnDialogInfoReceived(
00670 const SIPDialogNotification & info
00671 );
00672
00673 void SendNotifyDialogInfo(
00674 const SIPDialogNotification & info
00675 );
00676
00677
00680 virtual void OnRegInfoReceived(
00681 const SIPRegNotification & info
00682 );
00683
00684
00687 bool SendMESSAGE(
00688 SIPMessage::Params & params
00689 );
00690
00693 virtual void OnMESSAGECompleted(
00694 const SIPMessage::Params & params,
00695 SIP_PDU::StatusCodes reason
00696 );
00697
00698 struct ConnectionlessMessageInfo {
00699 ConnectionlessMessageInfo(SIP_PDU & pdu)
00700 : m_pdu(pdu)
00701 , m_transport(*pdu.GetTransport())
00702 , m_status(ResponseSent)
00703 { }
00704
00705 SIP_PDU & m_pdu;
00706 OpalTransport & m_transport;
00707 enum {
00708 NotHandled,
00709 SendOK,
00710 MethodNotAllowed,
00711 ResponseSent
00712 } m_status;
00713 };
00714
00715 typedef PNotifierTemplate<ConnectionlessMessageInfo &> ConnectionlessMessageNotifier;
00716 #define PDECLARE_ConnectionlessMessageNotifier(cls, fn) PDECLARE_NOTIFIER2(SIPEndPoint, cls, fn, SIPEndPoint::ConnectionlessMessageInfo &)
00717 #define PCREATE_ConnectionlessMessageNotifier(fn) PCREATE_NOTIFIER2(fn, SIPEndPoint::ConnectionlessMessageInfo &)
00718
00719 void SetConnectionlessMessageNotifier(
00720 const ConnectionlessMessageNotifier & notifier
00721 )
00722 { m_onConnectionlessMessage = notifier; }
00723
00724
00727 virtual bool SendOPTIONS(
00728 const SIPOptions::Params & params
00729 );
00730
00734 virtual void OnOptionsCompleted(
00735 const SIPOptions::Params & params,
00736 const SIP_PDU & response
00737 );
00738
00739
00743 bool Publish(
00744 const SIPSubscribe::Params & params,
00745 const PString & body,
00746 PString & aor
00747 );
00748 bool Publish(
00749 const PString & to,
00750 const PString & body,
00751 unsigned expire = 300
00752 );
00753
00756 PStringList GetPublications(
00757 const SIPSubscribe::EventPackage & eventPackage,
00758 bool includeOffline = false
00759 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
00760
00761
00762 #if OPAL_SIP_PRESENCE
00763
00766 bool PublishPresence(
00767 const SIPPresenceInfo & info,
00768 unsigned expire = 300
00769 );
00770
00773 virtual void OnPresenceInfoReceived (
00774 const SIPPresenceInfo & info
00775 );
00776 virtual void OnPresenceInfoReceived (
00777 const PString & identity,
00778 const PString & basic,
00779 const PString & note
00780 );
00781 #endif // OPAL_SIP_PRESENCE
00782
00783
00786 PBoolean Ping(
00787 const PURL & to
00788 );
00789
00792 const PStringSet & GetAllowedEvents() const { return m_allowedEvents; }
00793
00796 SIPConnection::PRACKMode GetDefaultPRACKMode() const { return m_defaultPrackMode; }
00797
00800 void SetDefaultPRACKMode(SIPConnection::PRACKMode mode) { m_defaultPrackMode = mode; }
00801
00802 void SetMaxPacketSizeUDP(PINDEX sz) { m_maxPacketSizeUDP = sz; }
00803 PINDEX GetMaxPacketSizeUDP() const { return m_maxPacketSizeUDP; }
00804
00805 void SetMaxRetries(unsigned r) { maxRetries = r; }
00806 unsigned GetMaxRetries() const { return maxRetries; }
00807
00808 void SetRetryTimeouts(
00809 const PTimeInterval & t1,
00810 const PTimeInterval & t2
00811 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00812 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00813 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00814
00815 void SetNonInviteTimeout(
00816 const PTimeInterval & t
00817 ) { nonInviteTimeout = t; }
00818 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00819
00820 void SetPduCleanUpTimeout(
00821 const PTimeInterval & t
00822 ) { pduCleanUpTimeout = t; }
00823 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00824
00825 void SetInviteTimeout(
00826 const PTimeInterval & t
00827 ) { inviteTimeout = t; }
00828 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00829
00830 void SetProgressTimeout(
00831 const PTimeInterval & t
00832 ) { m_progressTimeout = t; }
00833 const PTimeInterval & GetProgressTimeout() const { return m_progressTimeout; }
00834
00835 void SetAckTimeout(
00836 const PTimeInterval & t
00837 ) { ackTimeout = t; }
00838 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00839
00840 void SetRegistrarTimeToLive(
00841 const PTimeInterval & t
00842 ) { registrarTimeToLive = t; }
00843 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00844
00845 void SetNotifierTimeToLive(
00846 const PTimeInterval & t
00847 ) { notifierTimeToLive = t; }
00848 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00849
00850 P_DEPRECATED void SetNATBindingTimeout(
00851 const PTimeInterval & t
00852 ) { m_keepAliveTimeout = t; }
00853 P_DEPRECATED const PTimeInterval & GetNATBindingTimeout() const { return m_keepAliveTimeout; }
00854
00857 enum KeepAliveType {
00858 NoKeepAlive,
00859 KeepAliveByCRLF,
00860 KeepAliveByOPTION,
00861 NumMethods
00862 };
00863 void GetKeepAlive(
00864 PTimeInterval & timeout,
00865 KeepAliveType & type
00866 ) { timeout = m_keepAliveTimeout; type = m_keepAliveType; }
00867 void SetKeepAlive(
00868 const PTimeInterval & timeout,
00869 KeepAliveType type
00870 ) { m_keepAliveTimeout = timeout; m_keepAliveType = type; }
00871
00872
00873 void AddTransaction(
00874 SIPTransaction * transaction
00875 ) { m_transactions.Append(transaction); }
00876
00877 PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
00878 { return PSafePtrCast<SIPTransactionBase, SIPTransaction>(m_transactions.FindWithLock(transactionID, mode)); }
00879
00882 unsigned GetNextCSeq() { return ++m_lastSentCSeq; }
00883
00889 void SetRegisteredUserMode(bool v) { m_registeredUserMode = v; }
00890 bool GetRegisteredUserMode() const { return m_registeredUserMode; }
00891
00894 bool GetAuthentication(
00895 const PString & realm,
00896 PString & authId,
00897 PString & password
00898 );
00899
00902 virtual SIPURL GetDefaultLocalURL(const OpalTransport & transport);
00903
00912 void AdjustToRegistration(
00913 SIP_PDU & pdu,
00914 SIPConnection * connection = NULL,
00915 const OpalTransport * transport = NULL
00916 );
00917
00918
00921 const SIPURL & GetProxy() const { return m_proxy; }
00922
00925 void SetProxy(const SIPURL & url);
00926
00929 void SetProxy(
00930 const PString & hostname,
00931 const PString & username,
00932 const PString & password
00933 );
00934
00935
00938 int GetDefaultAppearanceCode() const { return m_defaultAppearanceCode; }
00939
00942 void SetDefaultAppearanceCode(int code) { m_defaultAppearanceCode = code; }
00943
00950 virtual PString GetUserAgent() const;
00951
00954 void SetUserAgent(const PString & str) { m_userAgentString = str; }
00955
00956
00959 virtual unsigned GetAllowedMethods() const;
00960
00961
00962 typedef KeepAliveType NATBindingRefreshMethod;
00963 P_DEPRECATED void SetNATBindingRefreshMethod(const NATBindingRefreshMethod type) { SetKeepAlive(m_keepAliveTimeout, type); }
00964
00965 virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00966
00967 virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
00968
00969
00970 PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m)
00971 { return activeSIPHandlers.FindSIPHandlerByCallID(callID, m); }
00972
00973 void UpdateHandlerIndexes(SIPHandler * handler)
00974 { activeSIPHandlers.Update(handler); }
00975
00976
00977 SIPThreadPool & GetThreadPool() { return m_threadPool; }
00978
00979
00980 protected:
00981 void AddTransport(const OpalTransportPtr & transport);
00982 void TransportThreadMain(OpalTransportPtr transport);
00983
00984 SIPURL m_proxy;
00985 PString m_userAgentString;
00986 PStringSet m_allowedEvents;
00987
00988 SIPConnection::PRACKMode m_defaultPrackMode;
00989
00990 PINDEX m_maxPacketSizeUDP;
00991 unsigned maxRetries;
00992 PTimeInterval retryTimeoutMin;
00993 PTimeInterval retryTimeoutMax;
00994 PTimeInterval nonInviteTimeout;
00995 PTimeInterval pduCleanUpTimeout;
00996 PTimeInterval inviteTimeout;
00997 PTimeInterval m_progressTimeout;
00998 PTimeInterval ackTimeout;
00999 PTimeInterval registrarTimeToLive;
01000 PTimeInterval notifierTimeToLive;
01001 PTimeInterval natBindingTimeout;
01002 PTimeInterval m_keepAliveTimeout;
01003 KeepAliveType m_keepAliveType;
01004 bool m_registeredUserMode;
01005 bool m_shuttingDown;
01006
01007
01008 PSafeDictionary<OpalTransportAddress, OpalTransport> m_transportsTable;
01009
01010
01011 SIPHandlersList activeSIPHandlers;
01012 PSafePtr<SIPHandler> FindHandlerByPDU(const SIP_PDU & pdu, PSafetyMode mode);
01013
01014 PStringToString m_receivedConnectionTokens;
01015 PMutex m_receivedConnectionMutex;
01016
01017 PSafeSortedList<SIPTransactionBase> m_transactions;
01018
01019 PAtomicInteger m_lastSentCSeq;
01020 int m_defaultAppearanceCode;
01021
01022 struct RegistrationCompletion {
01023 PSyncPoint m_sync;
01024 SIP_PDU::StatusCodes m_reason;
01025 RegistrationCompletion() : m_reason(SIP_PDU::Information_Trying) { }
01026 };
01027 std::map<PString, RegistrationCompletion> m_registrationComplete;
01028
01029 ConnectionlessMessageNotifier m_onConnectionlessMessage;
01030 typedef std::multimap<PString, SIPURL> ConferenceMap;
01031 ConferenceMap m_conferenceAOR;
01032
01033
01034 SIPThreadPool m_threadPool;
01035
01036
01037 PDECLARE_InterfaceNotifier(SIPEndPoint, OnHighPriorityInterfaceChange);
01038 PDECLARE_InterfaceNotifier(SIPEndPoint, OnLowPriorityInterfaceChange);
01039
01040 bool m_disableTrying;
01041
01042 P_REMOVE_VIRTUAL_VOID(OnReceivedIntervalTooBrief(SIPTransaction &, SIP_PDU &));
01043 P_REMOVE_VIRTUAL_VOID(OnReceivedAuthenticationRequired(SIPTransaction &, SIP_PDU &));
01044 P_REMOVE_VIRTUAL_VOID(OnReceivedOK(SIPTransaction &, SIP_PDU &));
01045 P_REMOVE_VIRTUAL_VOID(OnMessageFailed(const SIPURL &, SIP_PDU::StatusCodes));
01046 P_REMOVE_VIRTUAL(SIPConnection *,CreateConnection(OpalCall &, const PString &, void *, const SIPURL &, OpalTransport *, SIP_PDU *, unsigned, OpalConnection::StringOptions *), NULL);
01047 P_REMOVE_VIRTUAL(PBoolean, OnReceivedPDU(OpalTransport &, SIP_PDU *), false);
01048 P_REMOVE_VIRTUAL(bool, OnReceivedConnectionlessPDU(OpalTransport &, SIP_PDU *), false);
01049 P_REMOVE_VIRTUAL(PBoolean, OnReceivedINVITE(OpalTransport &, SIP_PDU *), false);
01050 P_REMOVE_VIRTUAL(PBoolean, OnReceivedNOTIFY(OpalTransport &, SIP_PDU &), false);
01051 P_REMOVE_VIRTUAL(PBoolean, OnReceivedREGISTER(OpalTransport &, SIP_PDU &), false);
01052 P_REMOVE_VIRTUAL(PBoolean, OnReceivedSUBSCRIBE(OpalTransport &, SIP_PDU &, SIPDialogContext *), false);
01053 P_REMOVE_VIRTUAL(bool, OnReceivedMESSAGE(OpalTransport &, SIP_PDU &), false);
01054 P_REMOVE_VIRTUAL(bool, OnReceivedOPTIONS(OpalTransport &, SIP_PDU &), false);
01055 };
01056
01057
01058 #endif // OPAL_SIP
01059
01060 #endif // OPAL_SIP_SIPEP_H
01061
01062
01063