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 bool GetAuthentication(const PString & authRealm, PString & user, PString & password);
00867
00870 virtual SIPURL GetRegisteredProxy(const SIPURL & remoteURL);
00871
00877 virtual SIPURL GetRegisteredPartyName(const SIPURL & remoteURL, const OpalTransport & transport);
00878
00879
00882 virtual SIPURL GetDefaultRegisteredPartyName(const OpalTransport & transport);
00883
00884
00893 void AdjustToRegistration(
00894 SIP_PDU & pdu,
00895 const OpalTransport & transport,
00896 const SIPConnection * connection
00897 );
00898
00908 virtual SIPURL GetLocalURL(
00909 const OpalTransport & transport,
00910 const PString & userName = PString::Empty()
00911 );
00912
00913
00916 const SIPURL & GetProxy() const { return proxy; }
00917
00918
00921 void SetProxy(const SIPURL & url);
00922
00923
00926 void SetProxy(
00927 const PString & hostname,
00928 const PString & username,
00929 const PString & password
00930 );
00931
00932
00935 int GetDefaultAppearanceCode() const { return m_defaultAppearanceCode; }
00936
00939 void SetDefaultAppearanceCode(int code) { m_defaultAppearanceCode = code; }
00940
00947 virtual PString GetUserAgent() const;
00948
00951 void SetUserAgent(const PString & str) { userAgentString = str; }
00952
00953
00956 virtual unsigned GetAllowedMethods() const;
00957
00958
00961 enum NATBindingRefreshMethod{
00962 None,
00963 Options,
00964 EmptyRequest,
00965 NumMethods
00966 };
00967
00968
00971 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00972
00973 virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00974
00975 virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
00976
00977 void UpdateHandlerIndexes(SIPHandler * handler) { activeSIPHandlers.Update(handler); }
00978
00979 protected:
00980 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00981 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00982
00983 SIPURL proxy;
00984 PString userAgentString;
00985
00986 SIPConnection::PRACKMode m_defaultPrackMode;
00987
00988 bool mimeForm;
00989 unsigned maxRetries;
00990 PTimeInterval retryTimeoutMin;
00991 PTimeInterval retryTimeoutMax;
00992 PTimeInterval nonInviteTimeout;
00993 PTimeInterval pduCleanUpTimeout;
00994 PTimeInterval inviteTimeout;
00995 PTimeInterval m_progressTimeout;
00996 PTimeInterval ackTimeout;
00997 PTimeInterval registrarTimeToLive;
00998 PTimeInterval notifierTimeToLive;
00999 PTimeInterval natBindingTimeout;
01000 bool m_registeredUserMode;
01001
01002 bool m_shuttingDown;
01003 SIPHandlersList activeSIPHandlers;
01004 PStringToString m_receivedConnectionTokens;
01005
01006 PSafeDictionary<PString, SIPTransaction> transactions;
01007
01008 PTimer natBindingTimer;
01009 NATBindingRefreshMethod natMethod;
01010 PAtomicInteger lastSentCSeq;
01011 int m_defaultAppearanceCode;
01012
01013 struct RegistrationCompletion {
01014 PSyncPoint m_sync;
01015 SIP_PDU::StatusCodes m_reason;
01016 RegistrationCompletion() : m_reason(SIP_PDU::Information_Trying) { }
01017 };
01018 std::map<PString, RegistrationCompletion> m_registrationComplete;
01019
01020 ConnectionlessMessageNotifier m_onConnectionlessMessage;
01021
01022
01023 class SIP_Work
01024 {
01025 public:
01026 SIP_Work(SIPEndPoint & ep, SIP_PDU * pdu, const PString & token);
01027 virtual ~SIP_Work();
01028
01029 virtual void Work();
01030
01031 SIPEndPoint & m_endpoint;
01032 SIP_PDU * m_pdu;
01033 PString m_token;
01034 };
01035
01036 class WorkThreadPool : public PQueuedThreadPool<SIP_Work>
01037 {
01038 public:
01039 WorkThreadPool(unsigned maxWorkers)
01040 : PQueuedThreadPool<SIP_Work>(maxWorkers)
01041 { }
01042 virtual WorkerThreadBase * CreateWorkerThread();
01043 } m_connectionThreadPool, m_handlerThreadPool;
01044
01045
01046
01047 enum {
01048 HighPriority = 80,
01049 LowPriority = 30,
01050 };
01051 class InterfaceMonitor : public PInterfaceMonitorClient
01052 {
01053 PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
01054 public:
01055 InterfaceMonitor(SIPEndPoint & manager, PINDEX priority);
01056
01057 virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
01058 virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
01059
01060 protected:
01061 SIPEndPoint & m_endpoint;
01062 };
01063 InterfaceMonitor m_highPriorityMonitor;
01064 InterfaceMonitor m_lowPriorityMonitor;
01065
01066 friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
01067 friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
01068
01069 bool m_disableTrying;
01070
01071 P_REMOVE_VIRTUAL_VOID(OnReceivedIntervalTooBrief(SIPTransaction &, SIP_PDU &));
01072 P_REMOVE_VIRTUAL_VOID(OnReceivedAuthenticationRequired(SIPTransaction &, SIP_PDU &));
01073 P_REMOVE_VIRTUAL_VOID(OnReceivedOK(SIPTransaction &, SIP_PDU &));
01074 P_REMOVE_VIRTUAL_VOID(OnMessageFailed(const SIPURL &, SIP_PDU::StatusCodes));
01075 };
01076
01077
01078 #endif // OPAL_SIP
01079
01080 #endif // OPAL_SIP_SIPEP_H
01081
01082
01083