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
00054 class SIPRegisterHandler;
00055
00056
00057
00058
00059
00060 #define OPAL_HAS_SIP_PRESENCE 1
00061
00063
00067 class SIPAuthInfo : public PObject
00068 {
00069 public:
00070 SIPAuthInfo()
00071 { }
00072
00073 SIPAuthInfo(const PString & u, const PString & p)
00074 { username = u; password = p; }
00075 PString username;
00076 PString password;
00077 };
00078
00080
00083 class SIPEndPoint : public OpalRTPEndPoint
00084 {
00085 PCLASSINFO(SIPEndPoint, OpalRTPEndPoint);
00086
00087 public:
00092 SIPEndPoint(
00093 OpalManager & manager
00094 );
00095
00098 ~SIPEndPoint();
00100
00107 virtual void ShutDown();
00108
00112 virtual PString GetDefaultTransport() const;
00113
00118 virtual PBoolean NewIncomingConnection(
00119 OpalTransport * transport
00120 );
00121
00151 virtual PBoolean MakeConnection(
00152 OpalCall & call,
00153 const PString & party,
00154 void * userData,
00155 unsigned int options,
00156 OpalConnection::StringOptions * stringOptions
00157 );
00158
00163 virtual PBoolean GarbageCollection();
00165
00171 virtual SIPConnection * CreateConnection(
00172 OpalCall & call,
00173 const PString & token,
00174 void * userData,
00175 const SIPURL & destination,
00176 OpalTransport * transport,
00177 SIP_PDU * invite,
00178 unsigned int options = 0,
00179 OpalConnection::StringOptions * stringOptions = NULL
00180
00181 );
00182
00185 virtual PBoolean SetupTransfer(
00186 const PString & token,
00187 const PString & callIdentity,
00188 const PString & remoteParty,
00189 void * userData = NULL
00190 );
00191
00195 virtual PBoolean ForwardConnection(
00196 SIPConnection & connection,
00197 const PString & forwardParty
00198 );
00199
00201
00204
00208 OpalTransport * CreateTransport(
00209 const OpalTransportAddress & remoteAddress,
00210 const OpalTransportAddress & localAddress = OpalTransportAddress()
00211 );
00212
00213 virtual void HandlePDU(
00214 OpalTransport & transport
00215 );
00216
00219 virtual PBoolean OnReceivedPDU(
00220 OpalTransport & transport,
00221 SIP_PDU * pdu
00222 );
00223
00226 virtual bool OnReceivedConnectionlessPDU(
00227 OpalTransport & transport,
00228 SIP_PDU * pdu
00229 );
00230
00233 virtual void OnReceivedResponse(
00234 SIPTransaction & transaction,
00235 SIP_PDU & response
00236 );
00237
00240 virtual PBoolean OnReceivedINVITE(
00241 OpalTransport & transport,
00242 SIP_PDU * pdu
00243 );
00244
00247 virtual void OnReceivedIntervalTooBrief(
00248 SIPTransaction & transaction,
00249 SIP_PDU & response)
00250 ;
00251
00254 virtual void OnReceivedAuthenticationRequired(
00255 SIPTransaction & transaction,
00256 SIP_PDU & response
00257 );
00258
00262 virtual void OnReceivedOK(
00263 SIPTransaction & transaction,
00264 SIP_PDU & response
00265 );
00266
00269 virtual PBoolean OnReceivedNOTIFY(
00270 OpalTransport & transport,
00271 SIP_PDU & response
00272 );
00273
00276 virtual PBoolean OnReceivedREGISTER(
00277 OpalTransport & transport,
00278 SIP_PDU & pdu
00279 );
00280
00283 virtual PBoolean OnReceivedSUBSCRIBE(
00284 OpalTransport & transport,
00285 SIP_PDU & pdu
00286 );
00287
00290 virtual bool OnReceivedMESSAGE(
00291 OpalTransport & transport,
00292 SIP_PDU & response
00293 );
00294
00297 virtual bool OnReceivedOPTIONS(
00298 OpalTransport & transport,
00299 SIP_PDU & response
00300 );
00301
00304 virtual void OnTransactionFailed(
00305 SIPTransaction & transaction
00306 );
00307
00315 virtual void OnRTPStatistics(
00316 const SIPConnection & connection,
00317 const RTP_Session & session
00318 ) const;
00320
00321
00326 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00327 const PString & token,
00328 PSafetyMode mode = PSafeReadWrite
00329 ) { return PSafePtrCast<OpalConnection, SIPConnection>(GetConnectionWithLock(token, mode)); }
00330
00331 virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
00332
00333
00336 virtual void OnMessageReceived (const SIPURL & from,
00337 const PString & body);
00338
00339
00353 bool Register(
00354 const SIPRegister::Params & params
00355 );
00356
00358 bool Register(
00359 const PString & host,
00360 const PString & user = PString::Empty(),
00361 const PString & autName = PString::Empty(),
00362 const PString & password = PString::Empty(),
00363 const PString & authRealm = PString::Empty(),
00364 unsigned expire = 0,
00365 const PTimeInterval & minRetryTime = PMaxTimeInterval,
00366 const PTimeInterval & maxRetryTime = PMaxTimeInterval
00367 );
00368
00373 bool Unregister(const PString & aor);
00374
00377 bool UnregisterAll();
00378
00382 PBoolean IsRegistered(const PString & aor);
00383
00386 unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
00387
00389 struct RegistrationStatus {
00390 PString m_addressofRecord;
00391 bool m_wasRegistering;
00392 bool m_reRegistering;
00393 SIP_PDU::StatusCodes m_reason;
00394 OpalProductInfo m_productInfo;
00395 };
00396
00399 virtual void OnRegistrationStatus(
00400 const RegistrationStatus & status
00401 );
00402
00403
00404 virtual void OnRegistrationStatus(
00405 const PString & aor,
00406 PBoolean wasRegistering,
00407 PBoolean reRegistering,
00408 SIP_PDU::StatusCodes reason
00409 );
00410
00414 virtual void OnRegistrationFailed(
00415 const PString & aor,
00416 SIP_PDU::StatusCodes reason,
00417 PBoolean wasRegistering
00418 );
00419
00423 virtual void OnRegistered(
00424 const PString & aor,
00425 PBoolean wasRegistering
00426 );
00427
00428
00432 bool Subscribe(
00433 SIPSubscribe::PredefinedPackages eventPackage,
00434 unsigned expire,
00435 const PString & to
00436 );
00437 bool Subscribe(
00438 const SIPSubscribe::Params & params
00439 );
00440
00441
00442 bool Unsubscribe(
00443 SIPSubscribe::PredefinedPackages eventPackage,
00444 const PString & to
00445 );
00446 bool Unsubscribe(
00447 const PString & eventPackage,
00448 const PString & to
00449 );
00450
00453 bool UnsubcribeAll(
00454 SIPSubscribe::PredefinedPackages eventPackage
00455 );
00456 bool UnsubcribeAll(
00457 const PString & eventPackage
00458 );
00459
00463 PBoolean IsSubscribed(
00464 const PString & eventPackage,
00465 const PString & to
00466 );
00467
00470 unsigned GetSubscriptionCount(const PString & eventPackage) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
00471
00474 virtual void OnSubscriptionStatus(
00475 const PString & eventPackage,
00476 const SIPURL & uri,
00477 bool wasSubscribing,
00478 bool reSubscribing,
00479 SIP_PDU::StatusCodes reason
00480 );
00481
00482
00485 PBoolean Message (
00486 const PString & to,
00487 const PString & body
00488 );
00489
00490
00494 PBoolean Publish(
00495 const PString & to,
00496 const PString & body,
00497 unsigned expire = 0
00498 );
00499
00500
00503 PBoolean Ping(
00504 const PString & to
00505 );
00506
00509 virtual void OnPresenceInfoReceived (
00510 const PString & user,
00511 const PString & basic,
00512 const PString & note
00513 );
00514
00519 virtual void OnMessageFailed(
00520 const SIPURL & messageUrl,
00521 SIP_PDU::StatusCodes reason);
00522
00523
00524 void SetMIMEForm(PBoolean v) { mimeForm = v; }
00525 PBoolean GetMIMEForm() const { return mimeForm; }
00526
00527 void SetMaxRetries(unsigned r) { maxRetries = r; }
00528 unsigned GetMaxRetries() const { return maxRetries; }
00529
00530 void SetRetryTimeouts(
00531 const PTimeInterval & t1,
00532 const PTimeInterval & t2
00533 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00534 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00535 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00536
00537 void SetNonInviteTimeout(
00538 const PTimeInterval & t
00539 ) { nonInviteTimeout = t; }
00540 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00541
00542 void SetPduCleanUpTimeout(
00543 const PTimeInterval & t
00544 ) { pduCleanUpTimeout = t; }
00545 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00546
00547 void SetInviteTimeout(
00548 const PTimeInterval & t
00549 ) { inviteTimeout = t; }
00550 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00551
00552 void SetAckTimeout(
00553 const PTimeInterval & t
00554 ) { ackTimeout = t; }
00555 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00556
00557 void SetRegistrarTimeToLive(
00558 const PTimeInterval & t
00559 ) { registrarTimeToLive = t; }
00560 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00561
00562 void SetNotifierTimeToLive(
00563 const PTimeInterval & t
00564 ) { notifierTimeToLive = t; }
00565 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00566
00567 void SetNATBindingTimeout(
00568 const PTimeInterval & t
00569 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00570 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00571
00572 void AddTransaction(
00573 SIPTransaction * transaction
00574 ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
00575
00576 PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
00577 { return transactions.FindWithLock(transactionID, mode); }
00578
00581 unsigned GetNextCSeq() { return ++lastSentCSeq; }
00582
00583
00586 PBoolean GetAuthentication(const PString & authRealm, PString & realm, PString & user, PString & password);
00587
00593 virtual SIPURL GetRegisteredPartyName(const SIPURL &);
00594
00595
00598 virtual SIPURL GetDefaultRegisteredPartyName();
00599
00600
00612 SIPURL GetContactURL(const OpalTransport &transport, const PString & userName, const PString & host);
00613
00614
00624 virtual SIPURL GetLocalURL(
00625 const OpalTransport & transport,
00626 const PString & userName = PString::Empty()
00627 );
00628
00629
00632 const SIPURL & GetProxy() const { return proxy; }
00633
00634
00637 void SetProxy(const SIPURL & url);
00638
00639
00642 void SetProxy(
00643 const PString & hostname,
00644 const PString & username,
00645 const PString & password
00646 );
00647
00648
00655 virtual PString GetUserAgent() const;
00656
00659 void SetUserAgent(const PString & str) { userAgentString = str; }
00660
00661
00664 virtual unsigned GetAllowedMethods() const;
00665
00666 PBoolean SendResponse(
00667 SIP_PDU::StatusCodes code,
00668 OpalTransport & transport,
00669 SIP_PDU & pdu
00670 );
00671
00674 enum NATBindingRefreshMethod{
00675 None,
00676 Options,
00677 EmptyRequest,
00678 NumMethods
00679 };
00680
00681
00684 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00685
00686 virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00687
00688 protected:
00689 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00690 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00691
00692 SIPURL proxy;
00693 PString userAgentString;
00694
00695 bool mimeForm;
00696 unsigned maxRetries;
00697 PTimeInterval retryTimeoutMin;
00698 PTimeInterval retryTimeoutMax;
00699 PTimeInterval nonInviteTimeout;
00700 PTimeInterval pduCleanUpTimeout;
00701 PTimeInterval inviteTimeout;
00702 PTimeInterval ackTimeout;
00703 PTimeInterval registrarTimeToLive;
00704 PTimeInterval notifierTimeToLive;
00705 PTimeInterval natBindingTimeout;
00706
00707 SIPHandlersList activeSIPHandlers;
00708
00709 PSafeDictionary<PString, SIPTransaction> transactions;
00710
00711 PTimer natBindingTimer;
00712 NATBindingRefreshMethod natMethod;
00713
00714 PAtomicInteger lastSentCSeq;
00715
00716 struct SIP_PDU_Work
00717 {
00718 SIP_PDU_Work()
00719 { ep = NULL; pdu = NULL; }
00720
00721 SIPEndPoint * ep;
00722 SIP_PDU * pdu;
00723 PString callID;
00724 };
00725
00726 typedef std::queue<SIP_PDU_Work *> SIP_PDUWorkQueue;
00727
00728 class SIP_PDU_Thread : public PThreadPoolWorkerBase
00729 {
00730 public:
00731 SIP_PDU_Thread(PThreadPoolBase & _pool);
00732 unsigned GetWorkSize() const;
00733 void OnAddWork(SIP_PDU_Work * work);
00734 void OnRemoveWork(SIP_PDU_Work *);
00735 void Shutdown();
00736 void Main();
00737
00738 protected:
00739 PMutex mutex;
00740 PSyncPoint sync;
00741 SIP_PDUWorkQueue pduQueue;
00742 };
00743
00744 typedef PThreadPool<SIP_PDU_Work, SIP_PDU_Thread> SIPMainThreadPool;
00745 SIPMainThreadPool threadPool;
00746
00747 enum {
00748 HighPriority = 80,
00749 LowPriority = 30,
00750 };
00751 class InterfaceMonitor : public PInterfaceMonitorClient
00752 {
00753 PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
00754 public:
00755 InterfaceMonitor(SIPEndPoint & manager, PINDEX priority);
00756
00757 virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
00758 virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
00759
00760 protected:
00761 SIPEndPoint & m_endpoint;
00762 };
00763 InterfaceMonitor m_highPriorityMonitor;
00764 InterfaceMonitor m_lowPriorityMonitor;
00765
00766 friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
00767 friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
00768 };
00769
00770
00771 #endif // OPAL_SIP
00772
00773 #endif // OPAL_SIP_SIPEP_H
00774
00775
00776