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
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
00175 virtual void OnReleased(
00176 OpalConnection & connection
00177 );
00178
00183 virtual PBoolean GarbageCollection();
00185
00191 virtual SIPConnection * CreateConnection(
00192 OpalCall & call,
00193 const PString & token,
00194 void * userData,
00195 const SIPURL & destination,
00196 OpalTransport * transport,
00197 SIP_PDU * invite,
00198 unsigned int options = 0,
00199 OpalConnection::StringOptions * stringOptions = NULL
00200
00201 );
00202
00205 virtual PBoolean SetupTransfer(
00206 const PString & token,
00207 const PString & callIdentity,
00208 const PString & remoteParty,
00209 void * userData = NULL
00210 );
00211
00215 virtual PBoolean ForwardConnection(
00216 SIPConnection & connection,
00217 const PString & forwardParty
00218 );
00219
00221
00224
00228 OpalTransport * CreateTransport(
00229 const SIPURL & remoteURL,
00230 const PString & localInterface = PString::Empty()
00231 );
00232
00233 virtual void HandlePDU(
00234 OpalTransport & transport
00235 );
00236
00239 virtual PBoolean OnReceivedPDU(
00240 OpalTransport & transport,
00241 SIP_PDU * pdu
00242 );
00243
00246 virtual bool OnReceivedConnectionlessPDU(
00247 OpalTransport & transport,
00248 SIP_PDU * pdu
00249 );
00250
00253 virtual void OnReceivedResponse(
00254 SIPTransaction & transaction,
00255 SIP_PDU & response
00256 );
00257
00260 virtual PBoolean OnReceivedINVITE(
00261 OpalTransport & transport,
00262 SIP_PDU * pdu
00263 );
00264
00267 virtual void OnReceivedIntervalTooBrief(
00268 SIPTransaction & transaction,
00269 SIP_PDU & response)
00270 ;
00271
00274 virtual void OnReceivedAuthenticationRequired(
00275 SIPTransaction & transaction,
00276 SIP_PDU & response
00277 );
00278
00282 virtual void OnReceivedOK(
00283 SIPTransaction & transaction,
00284 SIP_PDU & response
00285 );
00286
00289 virtual PBoolean OnReceivedNOTIFY(
00290 OpalTransport & transport,
00291 SIP_PDU & response
00292 );
00293
00296 virtual PBoolean OnReceivedREGISTER(
00297 OpalTransport & transport,
00298 SIP_PDU & pdu
00299 );
00300
00303 virtual PBoolean OnReceivedSUBSCRIBE(
00304 OpalTransport & transport,
00305 SIP_PDU & pdu
00306 );
00307
00310 virtual bool OnReceivedMESSAGE(
00311 OpalTransport & transport,
00312 SIP_PDU & response
00313 );
00314
00317 virtual bool OnReceivedOPTIONS(
00318 OpalTransport & transport,
00319 SIP_PDU & response
00320 );
00321
00324 virtual void OnTransactionFailed(
00325 SIPTransaction & transaction
00326 );
00327
00335 virtual void OnRTPStatistics(
00336 const SIPConnection & connection,
00337 const RTP_Session & session
00338 ) const;
00340
00341
00349 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00350 const PString & token,
00351 PSafetyMode mode = PSafeReadWrite,
00352 SIP_PDU::StatusCodes * errorCode = NULL
00353 );
00354
00355 virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
00356
00357
00360 virtual void OnMessageReceived (const SIPURL & from, const PString & body);
00361 virtual void OnMessageReceived (const SIPURL & from, const SIP_PDU & pdu);
00362
00376 bool Register(
00377 const SIPRegister::Params & params,
00378 PString & aor
00379 );
00380
00382 bool Register(
00383 const PString & host,
00384 const PString & user = PString::Empty(),
00385 const PString & autName = PString::Empty(),
00386 const PString & password = PString::Empty(),
00387 const PString & authRealm = PString::Empty(),
00388 unsigned expire = 0,
00389 const PTimeInterval & minRetryTime = PMaxTimeInterval,
00390 const PTimeInterval & maxRetryTime = PMaxTimeInterval
00391 );
00392
00397 bool Unregister(const PString & aor);
00398
00402 bool UnregisterAll();
00403
00410 PBoolean IsRegistered(
00411 const PString & aor,
00412 bool includeOffline = false
00413 );
00414
00417 unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
00418
00421 PStringList GetRegistrations(
00422 bool includeOffline = false
00423 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER); }
00424
00426 struct RegistrationStatus {
00427 PString m_addressofRecord;
00428 bool m_wasRegistering;
00429 bool m_reRegistering;
00430 SIP_PDU::StatusCodes m_reason;
00431 OpalProductInfo m_productInfo;
00432 void * m_userData;
00433 };
00434
00437 virtual void OnRegistrationStatus(
00438 const RegistrationStatus & status
00439 );
00440
00441
00442 virtual void OnRegistrationStatus(
00443 const PString & aor,
00444 PBoolean wasRegistering,
00445 PBoolean reRegistering,
00446 SIP_PDU::StatusCodes reason
00447 );
00448
00452 virtual void OnRegistrationFailed(
00453 const PString & aor,
00454 SIP_PDU::StatusCodes reason,
00455 PBoolean wasRegistering
00456 );
00457
00461 virtual void OnRegistered(
00462 const PString & aor,
00463 PBoolean wasRegistering
00464 );
00465
00466
00470 bool Subscribe(
00471 SIPSubscribe::PredefinedPackages eventPackage,
00472 unsigned expire,
00473 const PString & aor
00474 );
00475 bool Subscribe(
00476 const SIPSubscribe::Params & params,
00477 PString & aor
00478 );
00479
00482 bool Unsubscribe(
00483 SIPSubscribe::PredefinedPackages eventPackage,
00484 const PString & aor
00485 );
00486 bool Unsubscribe(
00487 const PString & eventPackage,
00488 const PString & aor
00489 );
00490
00494 bool UnsubcribeAll(
00495 SIPSubscribe::PredefinedPackages eventPackage
00496 );
00497 bool UnsubcribeAll(
00498 const PString & eventPackage
00499 );
00500
00507 PBoolean IsSubscribed(
00508 const PString & eventPackage,
00509 const PString & aor,
00510 bool includeOffline = false
00511 );
00512
00515 unsigned GetSubscriptionCount(
00516 const SIPSubscribe::EventPackage & eventPackage
00517 ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
00518
00521 PStringList GetSubscriptions(
00522 const SIPSubscribe::EventPackage & eventPackage,
00523 bool includeOffline = false
00524 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
00525
00528 virtual void OnSubscriptionStatus(
00529 const PString & eventPackage,
00530 const SIPURL & uri,
00531 bool wasSubscribing,
00532 bool reSubscribing,
00533 SIP_PDU::StatusCodes reason
00534 );
00535
00538 virtual bool CanNotify(
00539 const PString & eventPackage
00540 );
00541
00544 bool Notify(
00545 const SIPURL & targetAddress,
00546 const PString & eventPackage,
00547 const PObject & body
00548 );
00549
00550
00553 virtual void OnDialogInfoReceived(
00554 const SIPDialogNotification & info
00555 );
00556
00557 void SendNotifyDialogInfo(
00558 const SIPDialogNotification & info
00559 );
00560
00561
00564 PBoolean Message(
00565 const PString & to,
00566 const PString & body
00567 );
00568 PBoolean Message(
00569 const PString & to,
00570 const PString & body,
00571 const PString & remoteContact,
00572 const PString & callID
00573 );
00574
00579 virtual void OnMessageFailed(
00580 const SIPURL & messageUrl,
00581 SIP_PDU::StatusCodes reason
00582 );
00583
00584
00588 bool Publish(
00589 const SIPSubscribe::Params & params,
00590 const PString & body,
00591 PString & aor
00592 );
00593 bool Publish(
00594 const PString & to,
00595 const PString & body,
00596 unsigned expire = 300
00597 );
00598
00601 PStringList GetPublications(
00602 const SIPSubscribe::EventPackage & eventPackage,
00603 bool includeOffline = false
00604 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
00605
00606
00610 bool PublishPresence(
00611 const SIPPresenceInfo & info,
00612 unsigned expire = 300
00613 );
00614
00617 virtual void OnPresenceInfoReceived (
00618 const SIPPresenceInfo & info
00619 );
00620 virtual void OnPresenceInfoReceived (
00621 const PString & identity,
00622 const PString & basic,
00623 const PString & note
00624 );
00625
00626
00629 PBoolean Ping(
00630 const PString & to
00631 );
00632
00633
00634 void SetMIMEForm(PBoolean v) { mimeForm = v; }
00635 PBoolean GetMIMEForm() const { return mimeForm; }
00636
00637 void SetMaxRetries(unsigned r) { maxRetries = r; }
00638 unsigned GetMaxRetries() const { return maxRetries; }
00639
00640 void SetRetryTimeouts(
00641 const PTimeInterval & t1,
00642 const PTimeInterval & t2
00643 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00644 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00645 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00646
00647 void SetNonInviteTimeout(
00648 const PTimeInterval & t
00649 ) { nonInviteTimeout = t; }
00650 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00651
00652 void SetPduCleanUpTimeout(
00653 const PTimeInterval & t
00654 ) { pduCleanUpTimeout = t; }
00655 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00656
00657 void SetInviteTimeout(
00658 const PTimeInterval & t
00659 ) { inviteTimeout = t; }
00660 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00661
00662 void SetAckTimeout(
00663 const PTimeInterval & t
00664 ) { ackTimeout = t; }
00665 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00666
00667 void SetRegistrarTimeToLive(
00668 const PTimeInterval & t
00669 ) { registrarTimeToLive = t; }
00670 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00671
00672 void SetNotifierTimeToLive(
00673 const PTimeInterval & t
00674 ) { notifierTimeToLive = t; }
00675 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00676
00677 void SetNATBindingTimeout(
00678 const PTimeInterval & t
00679 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00680 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00681
00682 void AddTransaction(
00683 SIPTransaction * transaction
00684 ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
00685
00686 PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
00687 { return transactions.FindWithLock(transactionID, mode); }
00688
00691 unsigned GetNextCSeq() { return ++lastSentCSeq; }
00692
00695 PBoolean GetAuthentication(const PString & authRealm, PString & realm, PString & user, PString & password);
00696
00702 virtual SIPURL GetRegisteredPartyName(const SIPURL & remoteURL, const OpalTransport & transport);
00703
00704
00707 virtual SIPURL GetDefaultRegisteredPartyName(const OpalTransport & transport);
00708
00709
00721 SIPURL GetContactURL(const OpalTransport &transport, const SIPURL & localURI);
00722
00723
00733 virtual SIPURL GetLocalURL(
00734 const OpalTransport & transport,
00735 const PString & userName = PString::Empty()
00736 );
00737
00738
00741 const SIPURL & GetProxy() const { return proxy; }
00742
00743
00746 void SetProxy(const SIPURL & url);
00747
00748
00751 void SetProxy(
00752 const PString & hostname,
00753 const PString & username,
00754 const PString & password
00755 );
00756
00757
00760 int GetDefaultAppearanceCode() const { return m_defaultAppearanceCode; }
00761
00764 void SetDefaultAppearanceCode(int code) { m_defaultAppearanceCode = code; }
00765
00772 virtual PString GetUserAgent() const;
00773
00776 void SetUserAgent(const PString & str) { userAgentString = str; }
00777
00778
00781 virtual unsigned GetAllowedMethods() const;
00782
00783
00786 enum NATBindingRefreshMethod{
00787 None,
00788 Options,
00789 EmptyRequest,
00790 NumMethods
00791 };
00792
00793
00796 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00797
00798 virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00799
00800 virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
00801
00802 #if OPAL_HAS_SIPIM
00803 virtual OpalSIPIMManager & GetSIPIMManager() { return m_sipIMManager; }
00804 #endif
00805
00806 protected:
00807 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00808 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00809
00810 SIPURL proxy;
00811 PString userAgentString;
00812
00813 bool mimeForm;
00814 unsigned maxRetries;
00815 PTimeInterval retryTimeoutMin;
00816 PTimeInterval retryTimeoutMax;
00817 PTimeInterval nonInviteTimeout;
00818 PTimeInterval pduCleanUpTimeout;
00819 PTimeInterval inviteTimeout;
00820 PTimeInterval ackTimeout;
00821 PTimeInterval registrarTimeToLive;
00822 PTimeInterval notifierTimeToLive;
00823 PTimeInterval natBindingTimeout;
00824
00825 bool m_shuttingDown;
00826 SIPHandlersList activeSIPHandlers;
00827 PStringToString m_receivedConnectionTokens;
00828
00829 PSafeDictionary<PString, SIPTransaction> transactions;
00830
00831 PTimer natBindingTimer;
00832 NATBindingRefreshMethod natMethod;
00833 PAtomicInteger lastSentCSeq;
00834 int m_defaultAppearanceCode;
00835
00836 struct SIP_PDU_Work
00837 {
00838 public:
00839 SIP_PDU_Work(SIPEndPoint & ep, const PString & token, SIP_PDU * pdu);
00840 ~SIP_PDU_Work();
00841
00842 void OnReceivedPDU();
00843
00844 SIPEndPoint & m_endpoint;
00845 PString m_token;
00846 SIP_PDU * m_pdu;
00847 };
00848
00849 class PDUThreadPool : public PThreadPool<SIP_PDU_Work>
00850 {
00851 public:
00852 virtual WorkerThreadBase * CreateWorkerThread();
00853 } m_connectionThreadPool, m_handlerThreadPool;
00854
00855 typedef std::queue<SIP_PDU_Work *> SIP_PDUWorkQueue;
00856
00857 class SIP_PDU_Thread : public PDUThreadPool::WorkerThread
00858 {
00859 public:
00860 SIP_PDU_Thread(PDUThreadPool & pool_);
00861
00862 void AddWork(SIP_PDU_Work * work);
00863 void RemoveWork(SIP_PDU_Work * work);
00864 unsigned GetWorkSize() const;
00865
00866 void Main();
00867 void Shutdown();
00868
00869 protected:
00870 PSyncPoint m_sync;
00871 SIP_PDUWorkQueue m_pduQueue;
00872 };
00873
00874 enum {
00875 HighPriority = 80,
00876 LowPriority = 30,
00877 };
00878 class InterfaceMonitor : public PInterfaceMonitorClient
00879 {
00880 PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
00881 public:
00882 InterfaceMonitor(SIPEndPoint & manager, PINDEX priority);
00883
00884 virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
00885 virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
00886
00887 protected:
00888 SIPEndPoint & m_endpoint;
00889 };
00890 InterfaceMonitor m_highPriorityMonitor;
00891 InterfaceMonitor m_lowPriorityMonitor;
00892
00893 friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
00894 friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
00895
00896 #if OPAL_HAS_SIPIM
00897 OpalSIPIMManager m_sipIMManager;
00898 #endif
00899 };
00900
00901
00902 #endif // OPAL_SIP
00903
00904 #endif // OPAL_SIP_SIPEP_H
00905
00906
00907