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/sockagg.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 );
00098
00101 ~SIPEndPoint();
00103
00110 virtual void ShutDown();
00111
00115 virtual PString GetDefaultTransport() const;
00116
00121 virtual PBoolean NewIncomingConnection(
00122 OpalTransport * transport
00123 );
00124
00154 virtual PBoolean MakeConnection(
00155 OpalCall & call,
00156 const PString & party,
00157 void * userData,
00158 unsigned int options,
00159 OpalConnection::StringOptions * stringOptions
00160 );
00161
00180 virtual void OnReleased(
00181 OpalConnection & connection
00182 );
00183
00188 virtual PBoolean GarbageCollection();
00190
00196 virtual SIPConnection * CreateConnection(
00197 OpalCall & call,
00198 const PString & token,
00199 void * userData,
00200 const SIPURL & destination,
00201 OpalTransport * transport,
00202 SIP_PDU * invite,
00203 unsigned int options = 0,
00204 OpalConnection::StringOptions * stringOptions = NULL
00205
00206 );
00207
00210 virtual PBoolean SetupTransfer(
00211 const PString & token,
00212 const PString & callIdentity,
00213 const PString & remoteParty,
00214 void * userData = NULL
00215 );
00216
00220 virtual PBoolean ForwardConnection(
00221 SIPConnection & connection,
00222 const PString & forwardParty
00223 );
00224
00226
00229
00233 OpalTransport * CreateTransport(
00234 const SIPURL & remoteURL,
00235 const PString & localInterface = PString::Empty()
00236 );
00237
00238 virtual void HandlePDU(
00239 OpalTransport & transport
00240 );
00241
00244 virtual PBoolean OnReceivedPDU(
00245 OpalTransport & transport,
00246 SIP_PDU * pdu
00247 );
00248
00251 virtual bool OnReceivedConnectionlessPDU(
00252 OpalTransport & transport,
00253 SIP_PDU * pdu
00254 );
00255
00258 virtual void OnReceivedResponse(
00259 SIPTransaction & transaction,
00260 SIP_PDU & response
00261 );
00262
00265 virtual PBoolean OnReceivedINVITE(
00266 OpalTransport & transport,
00267 SIP_PDU * pdu
00268 );
00269
00272 virtual void OnReceivedIntervalTooBrief(
00273 SIPTransaction & transaction,
00274 SIP_PDU & response)
00275 ;
00276
00279 virtual void OnReceivedAuthenticationRequired(
00280 SIPTransaction & transaction,
00281 SIP_PDU & response
00282 );
00283
00287 virtual void OnReceivedOK(
00288 SIPTransaction & transaction,
00289 SIP_PDU & response
00290 );
00291
00294 virtual PBoolean OnReceivedNOTIFY(
00295 OpalTransport & transport,
00296 SIP_PDU & response
00297 );
00298
00301 virtual PBoolean OnReceivedREGISTER(
00302 OpalTransport & transport,
00303 SIP_PDU & pdu
00304 );
00305
00308 virtual PBoolean OnReceivedSUBSCRIBE(
00309 OpalTransport & transport,
00310 SIP_PDU & pdu
00311 );
00312
00315 virtual bool OnReceivedMESSAGE(
00316 OpalTransport & transport,
00317 SIP_PDU & response
00318 );
00319
00322 virtual bool OnReceivedOPTIONS(
00323 OpalTransport & transport,
00324 SIP_PDU & response
00325 );
00326
00329 virtual void OnTransactionFailed(
00330 SIPTransaction & transaction
00331 );
00332
00340 virtual void OnRTPStatistics(
00341 const SIPConnection & connection,
00342 const RTP_Session & session
00343 ) const;
00345
00346
00351 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00352 const PString & token,
00353 PSafetyMode mode = PSafeReadWrite
00354 ) { return PSafePtrCast<OpalConnection, SIPConnection>(GetConnectionWithLock(token, mode)); }
00355
00356 virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
00357
00358
00361 virtual void OnMessageReceived (const SIPURL & from, const PString & body);
00362 virtual void OnMessageReceived (const SIPURL & from, const SIP_PDU & pdu);
00363
00377 bool Register(
00378 const SIPRegister::Params & params,
00379 PString & aor
00380 );
00381
00383 bool Register(
00384 const PString & host,
00385 const PString & user = PString::Empty(),
00386 const PString & autName = PString::Empty(),
00387 const PString & password = PString::Empty(),
00388 const PString & authRealm = PString::Empty(),
00389 unsigned expire = 0,
00390 const PTimeInterval & minRetryTime = PMaxTimeInterval,
00391 const PTimeInterval & maxRetryTime = PMaxTimeInterval
00392 );
00393
00398 bool Unregister(const PString & aor);
00399
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 };
00433
00436 virtual void OnRegistrationStatus(
00437 const RegistrationStatus & status
00438 );
00439
00440
00441 virtual void OnRegistrationStatus(
00442 const PString & aor,
00443 PBoolean wasRegistering,
00444 PBoolean reRegistering,
00445 SIP_PDU::StatusCodes reason
00446 );
00447
00451 virtual void OnRegistrationFailed(
00452 const PString & aor,
00453 SIP_PDU::StatusCodes reason,
00454 PBoolean wasRegistering
00455 );
00456
00460 virtual void OnRegistered(
00461 const PString & aor,
00462 PBoolean wasRegistering
00463 );
00464
00465
00469 bool Subscribe(
00470 SIPSubscribe::PredefinedPackages eventPackage,
00471 unsigned expire,
00472 const PString & aor
00473 );
00474 bool Subscribe(
00475 const SIPSubscribe::Params & params,
00476 PString & aor
00477 );
00478
00479
00480 bool Unsubscribe(
00481 SIPSubscribe::PredefinedPackages eventPackage,
00482 const PString & aor
00483 );
00484 bool Unsubscribe(
00485 const PString & eventPackage,
00486 const PString & aor
00487 );
00488
00491 bool UnsubcribeAll(
00492 SIPSubscribe::PredefinedPackages eventPackage
00493 );
00494 bool UnsubcribeAll(
00495 const PString & eventPackage
00496 );
00497
00504 PBoolean IsSubscribed(
00505 const PString & eventPackage,
00506 const PString & aor,
00507 bool includeOffline = false
00508 );
00509
00512 unsigned GetSubscriptionCount(
00513 const SIPSubscribe::EventPackage & eventPackage
00514 ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
00515
00518 PStringList GetSubscriptions(
00519 const SIPSubscribe::EventPackage & eventPackage,
00520 bool includeOffline = false
00521 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
00522
00525 virtual void OnSubscriptionStatus(
00526 const PString & eventPackage,
00527 const SIPURL & uri,
00528 bool wasSubscribing,
00529 bool reSubscribing,
00530 SIP_PDU::StatusCodes reason
00531 );
00532
00535 virtual bool CanNotify(
00536 const PString & eventPackage
00537 );
00538
00541 bool Notify(
00542 const SIPURL & targetAddress,
00543 const PString & eventPackage,
00544 const PObject & body
00545 );
00546
00547
00550 virtual void OnDialogInfoReceived(
00551 const SIPDialogNotification & info
00552 );
00553
00554 void SendNotifyDialogInfo(
00555 const SIPDialogNotification & info
00556 );
00557
00558
00561 PBoolean Message(
00562 const PString & to,
00563 const PString & body
00564 );
00565 PBoolean Message(
00566 const PString & to,
00567 const PString & body,
00568 const PString & remoteContact,
00569 const PString & callID
00570 );
00571
00576 virtual void OnMessageFailed(
00577 const SIPURL & messageUrl,
00578 SIP_PDU::StatusCodes reason
00579 );
00580
00581
00585 bool Publish(
00586 const SIPSubscribe::Params & params,
00587 const PString & body,
00588 PString & aor
00589 );
00590 bool Publish(
00591 const PString & to,
00592 const PString & body,
00593 unsigned expire = 300
00594 );
00595
00598 PStringList GetPublications(
00599 const SIPSubscribe::EventPackage & eventPackage,
00600 bool includeOffline = false
00601 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
00602
00603
00607 bool PublishPresence(
00608 const SIPPresenceInfo & info,
00609 unsigned expire = 300
00610 );
00611
00614 virtual void OnPresenceInfoReceived (
00615 const SIPPresenceInfo & info
00616 );
00617 virtual void OnPresenceInfoReceived (
00618 const PString & identity,
00619 const PString & basic,
00620 const PString & note
00621 );
00622
00623
00626 PBoolean Ping(
00627 const PString & to
00628 );
00629
00630
00631 void SetMIMEForm(PBoolean v) { mimeForm = v; }
00632 PBoolean GetMIMEForm() const { return mimeForm; }
00633
00634 void SetMaxRetries(unsigned r) { maxRetries = r; }
00635 unsigned GetMaxRetries() const { return maxRetries; }
00636
00637 void SetRetryTimeouts(
00638 const PTimeInterval & t1,
00639 const PTimeInterval & t2
00640 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00641 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00642 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00643
00644 void SetNonInviteTimeout(
00645 const PTimeInterval & t
00646 ) { nonInviteTimeout = t; }
00647 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00648
00649 void SetPduCleanUpTimeout(
00650 const PTimeInterval & t
00651 ) { pduCleanUpTimeout = t; }
00652 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00653
00654 void SetInviteTimeout(
00655 const PTimeInterval & t
00656 ) { inviteTimeout = t; }
00657 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00658
00659 void SetAckTimeout(
00660 const PTimeInterval & t
00661 ) { ackTimeout = t; }
00662 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00663
00664 void SetRegistrarTimeToLive(
00665 const PTimeInterval & t
00666 ) { registrarTimeToLive = t; }
00667 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00668
00669 void SetNotifierTimeToLive(
00670 const PTimeInterval & t
00671 ) { notifierTimeToLive = t; }
00672 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00673
00674 void SetNATBindingTimeout(
00675 const PTimeInterval & t
00676 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00677 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00678
00679 void AddTransaction(
00680 SIPTransaction * transaction
00681 ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
00682
00683 PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
00684 { return transactions.FindWithLock(transactionID, mode); }
00685
00688 unsigned GetNextCSeq() { return ++lastSentCSeq; }
00689
00692 PBoolean GetAuthentication(const PString & authRealm, PString & realm, PString & user, PString & password);
00693
00699 virtual SIPURL GetRegisteredPartyName(const SIPURL & remoteURL, const OpalTransport & transport);
00700
00701
00704 virtual SIPURL GetDefaultRegisteredPartyName(const OpalTransport & transport);
00705
00706
00718 SIPURL GetContactURL(const OpalTransport &transport, const PString & userName, const PString & host);
00719
00720
00730 virtual SIPURL GetLocalURL(
00731 const OpalTransport & transport,
00732 const PString & userName = PString::Empty()
00733 );
00734
00735
00738 const SIPURL & GetProxy() const { return proxy; }
00739
00740
00743 void SetProxy(const SIPURL & url);
00744
00745
00748 void SetProxy(
00749 const PString & hostname,
00750 const PString & username,
00751 const PString & password
00752 );
00753
00754
00757 int GetDefaultAppearanceCode() const { return m_defaultAppearanceCode; }
00758
00761 void SetDefaultAppearanceCode(int code) { m_defaultAppearanceCode = code; }
00762
00769 virtual PString GetUserAgent() const;
00770
00773 void SetUserAgent(const PString & str) { userAgentString = str; }
00774
00775
00778 virtual unsigned GetAllowedMethods() const;
00779
00780
00783 enum NATBindingRefreshMethod{
00784 None,
00785 Options,
00786 EmptyRequest,
00787 NumMethods
00788 };
00789
00790
00793 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00794
00795 virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00796
00797 virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
00798
00799 #if OPAL_HAS_SIPIM
00800 virtual OpalSIPIMManager & GetSIPIMManager() { return m_sipIMManager; }
00801 #endif
00802
00803 protected:
00804 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00805 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00806
00807 SIPURL proxy;
00808 PString userAgentString;
00809
00810 bool mimeForm;
00811 unsigned maxRetries;
00812 PTimeInterval retryTimeoutMin;
00813 PTimeInterval retryTimeoutMax;
00814 PTimeInterval nonInviteTimeout;
00815 PTimeInterval pduCleanUpTimeout;
00816 PTimeInterval inviteTimeout;
00817 PTimeInterval ackTimeout;
00818 PTimeInterval registrarTimeToLive;
00819 PTimeInterval notifierTimeToLive;
00820 PTimeInterval natBindingTimeout;
00821
00822 bool m_shuttingDown;
00823 SIPHandlersList activeSIPHandlers;
00824 PStringToString m_receivedConnectionTokens;
00825
00826 PSafeDictionary<PString, SIPTransaction> transactions;
00827
00828 PTimer natBindingTimer;
00829 NATBindingRefreshMethod natMethod;
00830 PAtomicInteger lastSentCSeq;
00831 int m_defaultAppearanceCode;
00832
00833 struct SIP_PDU_Work
00834 {
00835 public:
00836 SIP_PDU_Work(SIPEndPoint & ep, const PString & token, SIP_PDU * pdu);
00837 ~SIP_PDU_Work();
00838
00839 void OnReceivedPDU();
00840
00841 private:
00842 SIPEndPoint & m_endpoint;
00843 PString m_token;
00844 SIP_PDU * m_pdu;
00845 };
00846
00847 typedef std::queue<SIP_PDU_Work *> SIP_PDUWorkQueue;
00848
00849 class SIP_PDU_Thread : public PThreadPoolWorkerBase
00850 {
00851 public:
00852 SIP_PDU_Thread(PThreadPoolBase & _pool);
00853 unsigned GetWorkSize() const;
00854 void OnAddWork(SIP_PDU_Work * work);
00855 void OnRemoveWork(SIP_PDU_Work *);
00856 void Shutdown();
00857 void Main();
00858
00859 protected:
00860 PMutex mutex;
00861 PSyncPoint sync;
00862 SIP_PDUWorkQueue pduQueue;
00863 };
00864
00865 typedef PThreadPool<SIP_PDU_Work, SIP_PDU_Thread> SIPMainThreadPool;
00866 SIPMainThreadPool threadPool;
00867
00868 enum {
00869 HighPriority = 80,
00870 LowPriority = 30,
00871 };
00872 class InterfaceMonitor : public PInterfaceMonitorClient
00873 {
00874 PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
00875 public:
00876 InterfaceMonitor(SIPEndPoint & manager, PINDEX priority);
00877
00878 virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
00879 virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
00880
00881 protected:
00882 SIPEndPoint & m_endpoint;
00883 };
00884 InterfaceMonitor m_highPriorityMonitor;
00885 InterfaceMonitor m_lowPriorityMonitor;
00886
00887 friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
00888 friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
00889
00890 #if OPAL_HAS_SIPIM
00891 OpalSIPIMManager m_sipIMManager;
00892 #endif
00893 };
00894
00895
00896 #endif // OPAL_SIP
00897
00898 #endif // OPAL_SIP_SIPEP_H
00899
00900
00901