OPAL Version 3.10.4

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

sipep.h
Go to the documentation of this file.
1 /*
2 * sipep.h
3 *
4 * Session Initiation Protocol endpoint.
5 *
6 * Open Phone Abstraction Library (OPAL)
7 * Formally known as the Open H323 project.
8 *
9 * Copyright (c) 2001 Equivalence Pty. Ltd.
10 *
11 * The contents of this file are subject to the Mozilla Public License
12 * Version 1.0 (the "License"); you may not use this file except in
13 * compliance with the License. You may obtain a copy of the License at
14 * http://www.mozilla.org/MPL/
15 *
16 * Software distributed under the License is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18 * the License for the specific language governing rights and limitations
19 * under the License.
20 *
21 * The Original Code is Open Phone Abstraction Library.
22 *
23 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 *
25 * Contributor(s): ______________________________________.
26 *
27 * $Revision: 26939 $
28 * $Author: rjongbloed $
29 * $Date: 2012-02-07 01:17:49 -0600 (Tue, 07 Feb 2012) $
30 */
31
32 #ifndef OPAL_SIP_SIPEP_H
33 #define OPAL_SIP_SIPEP_H
34
35 #ifdef P_USE_PRAGMA
36 #pragma interface
37 #endif
38
39 #ifndef _PTLIB_H
40 #include <ptlib.h>
41 #endif
42
43 #include <opal/buildopts.h>
44
45 #if OPAL_SIP
46
47 #include <ptclib/threadpool.h>
48 #include <opal/rtpep.h>
49 #include <sip/sipcon.h>
50 #include <sip/sippdu.h>
51 #include <sip/handlers.h>
52
53 #if OPAL_HAS_SIPIM
54 #include <im/sipim.h>
55 #endif
56
57 class SIPRegisterHandler;
58
59
60 //
61 // provide flag so applications know if presence is available
62 //
63 #define OPAL_HAS_SIP_PRESENCE 1
64
66
70 class SIPAuthInfo : public PObject
71 {
72 public:
73 SIPAuthInfo()
74 { }
75
76 SIPAuthInfo(const PString & u, const PString & p)
77 { username = u; password = p; }
78 PString username;
79 PString password;
80 };
81
83
86 class SIPEndPoint : public OpalRTPEndPoint
87 {
88 PCLASSINFO(SIPEndPoint, OpalRTPEndPoint);
89
90 public:
95 SIPEndPoint(
96 OpalManager & manager,
97 unsigned maxConnectionThreads = 10,
98 unsigned maxHandlerThreads = 5
99 );
100
103 ~SIPEndPoint();
105
112 virtual void ShutDown();
113
117 virtual PString GetDefaultTransport() const;
118
123 virtual PBoolean NewIncomingConnection(
124 OpalTransport * transport
125 );
126
156 virtual PSafePtr<OpalConnection> MakeConnection(
157 OpalCall & call,
158 const PString & party,
159 void * userData,
160 unsigned int options,
161 OpalConnection::StringOptions * stringOptions
162 );
163
182 virtual void OnReleased(
183 OpalConnection & connection
184 );
185
190 virtual PBoolean GarbageCollection();
192
198 virtual SIPConnection * CreateConnection(
199 OpalCall & call,
200 const PString & token,
201 void * userData,
202 const SIPURL & destination,
203 OpalTransport * transport,
204 SIP_PDU * invite,
205 unsigned int options = 0,
206 OpalConnection::StringOptions * stringOptions = NULL
207
208 );
209
212 virtual PBoolean SetupTransfer(
213 const PString & token,
214 const PString & callIdentity,
215 const PString & remoteParty,
216 void * userData = NULL
217 );
218
222 virtual PBoolean ForwardConnection(
223 SIPConnection & connection,
224 const PString & forwardParty
225 );
226
237 bool ClearDialogContext(
238 const PString & descriptor
239 );
240 bool ClearDialogContext(
241 SIPDialogContext & context
242 );
244
247
251 OpalTransport * CreateTransport(
252 const SIPURL & remoteURL,
253 const PString & localInterface = PString::Empty()
254 );
255
256 virtual void HandlePDU(
257 OpalTransport & transport
258 );
259
262 virtual PBoolean OnReceivedPDU(
263 OpalTransport & transport,
264 SIP_PDU * pdu
265 );
266
269 virtual bool OnReceivedConnectionlessPDU(
270 OpalTransport & transport,
271 SIP_PDU * pdu
272 );
273
276 virtual void OnReceivedResponse(
277 SIPTransaction & transaction,
278 SIP_PDU & response
279 );
280
283 virtual PBoolean OnReceivedINVITE(
284 OpalTransport & transport,
285 SIP_PDU * pdu
286 );
287
290 virtual PBoolean OnReceivedNOTIFY(
291 OpalTransport & transport,
292 SIP_PDU & response
293 );
294
297 virtual PBoolean OnReceivedREGISTER(
298 OpalTransport & transport,
299 SIP_PDU & pdu
300 );
301
304 virtual PBoolean OnReceivedSUBSCRIBE(
305 OpalTransport & transport,
306 SIP_PDU & pdu
307 );
308
311 virtual bool OnReceivedMESSAGE(
312 OpalTransport & transport,
313 SIP_PDU & response
314 );
315
318 virtual bool OnReceivedOPTIONS(
319 OpalTransport & transport,
320 SIP_PDU & response
321 );
322
325 virtual void OnTransactionFailed(
326 SIPTransaction & transaction
327 );
328
336 virtual void OnRTPStatistics(
337 const SIPConnection & connection,
338 const RTP_Session & session
339 ) const;
341
342
350 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
351 const PString & token,
352 PSafetyMode mode = PSafeReadWrite,
353 SIP_PDU::StatusCodes * errorCode = NULL
354 );
355
356 virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
357
358
393 bool Register(
394 const SIPRegister::Params & params,
395 PString & aor,
396 SIP_PDU::StatusCodes * reason = NULL
397 );
398
399 // For backward compatibility
400 bool Register(
401 const SIPRegister::Params & params,
402 PString & aor,
403 bool asynchronous
404 );
405
407 bool P_DEPRECATED Register(
408 const PString & host,
409 const PString & user = PString::Empty(),
410 const PString & autName = PString::Empty(),
411 const PString & password = PString::Empty(),
412 const PString & authRealm = PString::Empty(),
413 unsigned expire = 0,
414 const PTimeInterval & minRetryTime = PMaxTimeInterval,
415 const PTimeInterval & maxRetryTime = PMaxTimeInterval
416 );
417
432 PBoolean IsRegistered(
433 const PString & aor,
434 bool includeOffline = false
435 );
436
446 bool Unregister(
447 const PString & aor
448 );
449
453 bool UnregisterAll();
454
457 unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
458
461 PStringList GetRegistrations(
462 bool includeOffline = false
463 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER); }
464
466 struct RegistrationStatus {
467 SIPRegisterHandler * m_handler;
468 PString m_addressofRecord;
469 bool m_wasRegistering;
470 bool m_reRegistering;
471 SIP_PDU::StatusCodes m_reason;
472 OpalProductInfo m_productInfo;
473 void * m_userData;
474 };
475
478 virtual void OnRegistrationStatus(
479 const RegistrationStatus & status
480 );
481
482 // For backward compatibility
483 virtual void OnRegistrationStatus(
484 const PString & aor,
485 PBoolean wasRegistering,
486 PBoolean reRegistering,
487 SIP_PDU::StatusCodes reason
488 );
489
493 virtual void OnRegistrationFailed(
494 const PString & aor,
495 SIP_PDU::StatusCodes reason,
496 PBoolean wasRegistering
497 );
498
502 virtual void OnRegistered(
503 const PString & aor,
504 PBoolean wasRegistering
505 );
506
507
547 bool Subscribe(
548 const SIPSubscribe::Params & params,
549 PString & token,
550 bool tokenIsAOR = true
551 );
552
553 // For backward compatibility
554 bool Subscribe(
555 SIPSubscribe::PredefinedPackages eventPackage,
556 unsigned expire,
557 const PString & aor
558 );
559
566 bool IsSubscribed(
567 const PString & aor,
568 bool includeOffline = false
569 );
570 bool IsSubscribed(
571 const PString & eventPackage,
572 const PString & aor,
573 bool includeOffline = false
574 );
575
585 bool Unsubscribe(
586 const PString & aor,
587 bool invalidateNotifiers = false
588 );
589 bool Unsubscribe(
590 SIPSubscribe::PredefinedPackages eventPackage,
591 const PString & aor,
592 bool invalidateNotifiers = false
593 );
594 bool Unsubscribe(
595 const PString & eventPackage,
596 const PString & aor,
597 bool invalidateNotifiers = false
598 );
599
603 bool UnsubcribeAll(
604 SIPSubscribe::PredefinedPackages eventPackage
605 );
606 bool UnsubcribeAll(
607 const PString & eventPackage
608 );
609
612 unsigned GetSubscriptionCount(
613 const SIPSubscribe::EventPackage & eventPackage
614 ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
615
618 PStringList GetSubscriptions(
619 const SIPSubscribe::EventPackage & eventPackage,
620 bool includeOffline = false
621 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
622
624 typedef SIPSubscribe::SubscriptionStatus SubscriptionStatus;
625
628 virtual void OnSubscriptionStatus(
629 const SubscriptionStatus & status
630 );
631
635 virtual void OnSubscriptionStatus(
636 const PString & eventPackage,
637 const SIPURL & uri,
638 bool wasSubscribing,
639 bool reSubscribing,
640 SIP_PDU::StatusCodes reason
641 );
642
643 virtual void OnSubscriptionStatus(
644 SIPSubscribeHandler & handler,
645 const SIPURL & uri,
646 bool wasSubscribing,
647 bool reSubscribing,
648 SIP_PDU::StatusCodes reason
649 );
650
653 virtual bool CanNotify(
654 const PString & eventPackage
655 );
656
659 bool Notify(
660 const SIPURL & targetAddress,
661 const PString & eventPackage,
662 const PObject & body
663 );
664
665
668 virtual void OnDialogInfoReceived(
669 const SIPDialogNotification & info
670 );
671
672 void SendNotifyDialogInfo(
673 const SIPDialogNotification & info
674 );
675
676
679 virtual PBoolean Message(
680 OpalIM & message
681 );
682
685 bool SendMESSAGE(
686 SIPMessage::Params & params
687 );
688
691 virtual void OnMESSAGECompleted(
692 const SIPMessage::Params & params,
693 SIP_PDU::StatusCodes reason
694 );
695
696 struct ConnectionlessMessageInfo {
697 ConnectionlessMessageInfo(OpalTransport & transport, SIP_PDU & pdu)
698 : m_pdu(pdu), m_transport(transport), m_status(ResponseSent)
699 { }
700
701 SIP_PDU & m_pdu;
702 OpalTransport & m_transport;
703 enum {
704 NotHandled,
705 SendOK,
706 MethodNotAllowed,
707 ResponseSent
708 } m_status;
709 };
710
711 typedef PNotifierTemplate<ConnectionlessMessageInfo &> ConnectionlessMessageNotifier;
712 #define PDECLARE_ConnectionlessMessageNotifier(cls, fn) PDECLARE_NOTIFIER2(SIPEndPoint, cls, fn, SIPEndPoint::ConnectionlessMessageInfo &)
713 #define PCREATE_ConnectionlessMessageNotifier(fn) PCREATE_NOTIFIER2(fn, SIPEndPoint::ConnectionlessMessageInfo &)
714
715 void SetConnectionlessMessageNotifier(
716 const ConnectionlessMessageNotifier & notifier
717 )
718 { m_onConnectionlessMessage = notifier; }
719
720
723 virtual bool SendOPTIONS(
724 const SIPOptions::Params & params
725 );
726
730 virtual void OnOptionsCompleted(
731 const SIPOptions::Params & params,
732 const SIP_PDU & response
733 );
734
735
739 bool Publish(
740 const SIPSubscribe::Params & params,
741 const PString & body,
742 PString & aor
743 );
744 bool Publish(
745 const PString & to,
746 const PString & body,
747 unsigned expire = 300
748 );
749
752 PStringList GetPublications(
753 const SIPSubscribe::EventPackage & eventPackage,
754 bool includeOffline = false
755 ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
756
757
761 bool PublishPresence(
762 const SIPPresenceInfo & info,
763 unsigned expire = 300
764 );
765
768 virtual void OnPresenceInfoReceived (
769 const SIPPresenceInfo & info
770 );
771 virtual void OnPresenceInfoReceived (
772 const PString & identity,
773 const PString & basic,
774 const PString & note
775 );
776
777
780 PBoolean Ping(
781 const PURL & to
782 );
783
786 SIPConnection::PRACKMode GetDefaultPRACKMode() const { return m_defaultPrackMode; }
787
790 void SetDefaultPRACKMode(SIPConnection::PRACKMode mode) { m_defaultPrackMode = mode; }
791
792 void SetMIMEForm(PBoolean v) { mimeForm = v; }
793 PBoolean GetMIMEForm() const { return mimeForm; }
794
795 void SetMaxRetries(unsigned r) { maxRetries = r; }
796 unsigned GetMaxRetries() const { return maxRetries; }
797
798 void SetRetryTimeouts(
799 const PTimeInterval & t1,
800 const PTimeInterval & t2
801 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
802 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
803 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
804
805 void SetNonInviteTimeout(
806 const PTimeInterval & t
807 ) { nonInviteTimeout = t; }
808 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
809
810 void SetPduCleanUpTimeout(
811 const PTimeInterval & t
812 ) { pduCleanUpTimeout = t; }
813 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
814
815 void SetInviteTimeout(
816 const PTimeInterval & t
817 ) { inviteTimeout = t; }
818 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
819
820 void SetProgressTimeout(
821 const PTimeInterval & t
822 ) { m_progressTimeout = t; }
823 const PTimeInterval & GetProgressTimeout() const { return m_progressTimeout; }
824
825 void SetAckTimeout(
826 const PTimeInterval & t
827 ) { ackTimeout = t; }
828 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
829
830 void SetRegistrarTimeToLive(
831 const PTimeInterval & t
832 ) { registrarTimeToLive = t; }
833 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
834
835 void SetNotifierTimeToLive(
836 const PTimeInterval & t
837 ) { notifierTimeToLive = t; }
838 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
839
840 void SetNATBindingTimeout(
841 const PTimeInterval & t
842 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
843 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
844
845 void AddTransaction(
846 SIPTransaction * transaction
847 ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
848
849 PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
850 { return transactions.FindWithLock(transactionID, mode); }
851
854 unsigned GetNextCSeq() { return ++lastSentCSeq; }
855
861 void SetRegisteredUserMode(bool v) { m_registeredUserMode = v; }
862 bool GetRegisteredYserMode() const { return m_registeredUserMode; }
863
866 bool GetAuthentication(const PString & authRealm, PString & user, PString & password);
867
870 virtual SIPURL GetRegisteredProxy(const SIPURL & remoteURL);
871
877 virtual SIPURL GetRegisteredPartyName(const SIPURL & remoteURL, const OpalTransport & transport);
878
879
882 virtual SIPURL GetDefaultRegisteredPartyName(const OpalTransport & transport);
883
884
893 void AdjustToRegistration(
894 SIP_PDU & pdu,
895 const OpalTransport & transport,
896 const SIPConnection * connection
897 );
898
908 virtual SIPURL GetLocalURL(
909 const OpalTransport & transport,
910 const PString & userName = PString::Empty()
911 );
912
913
916 const SIPURL & GetProxy() const { return proxy; }
917
918
921 void SetProxy(const SIPURL & url);
922
923
926 void SetProxy(
927 const PString & hostname,
928 const PString & username,
929 const PString & password
930 );
931
932
935 int GetDefaultAppearanceCode() const { return m_defaultAppearanceCode; }
936
939 void SetDefaultAppearanceCode(int code) { m_defaultAppearanceCode = code; }
940
947 virtual PString GetUserAgent() const;
948
951 void SetUserAgent(const PString & str) { userAgentString = str; }
952
953
956 virtual unsigned GetAllowedMethods() const;
957
958
961 enum NATBindingRefreshMethod{
962 None,
963 Options,
964 EmptyRequest,
965 NumMethods
966 };
967
968
971 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
972
973 virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
974
975 virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
976
977 void UpdateHandlerIndexes(SIPHandler * handler) { activeSIPHandlers.Update(handler); }
978
979 protected:
980 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
981 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
982
983 SIPURL proxy;
984 PString userAgentString;
985
986 SIPConnection::PRACKMode m_defaultPrackMode;
987
988 bool mimeForm;
989 unsigned maxRetries;
990 PTimeInterval retryTimeoutMin; // T1
991 PTimeInterval retryTimeoutMax; // T2
992 PTimeInterval nonInviteTimeout; // T3
993 PTimeInterval pduCleanUpTimeout; // T4
994 PTimeInterval inviteTimeout;
995 PTimeInterval m_progressTimeout;
996 PTimeInterval ackTimeout;
997 PTimeInterval registrarTimeToLive;
998 PTimeInterval notifierTimeToLive;
999 PTimeInterval natBindingTimeout;
1000 bool m_registeredUserMode;
1001
1002 bool m_shuttingDown;
1003 SIPHandlersList activeSIPHandlers;
1004 PStringToString m_receivedConnectionTokens;
1005
1006 PSafeDictionary<PString, SIPTransaction> transactions;
1007
1008 PTimer natBindingTimer;
1009 NATBindingRefreshMethod natMethod;
1010 PAtomicInteger lastSentCSeq;
1011 int m_defaultAppearanceCode;
1012
1013 struct RegistrationCompletion {
1014 PSyncPoint m_sync;
1015 SIP_PDU::StatusCodes m_reason;
1016 RegistrationCompletion() : m_reason(SIP_PDU::Information_Trying) { }
1017 };
1018 std::map<PString, RegistrationCompletion> m_registrationComplete;
1019
1020 ConnectionlessMessageNotifier m_onConnectionlessMessage;
1021
1022 // Thread pooling
1023 class SIP_Work
1024 {
1025 public:
1026 SIP_Work(SIPEndPoint & ep, SIP_PDU * pdu, const PString & token);
1027 virtual ~SIP_Work();
1028
1029 virtual void Work();
1030
1031 SIPEndPoint & m_endpoint;
1032 SIP_PDU * m_pdu;
1033 PString m_token;
1034 };
1035
1036 class WorkThreadPool : public PQueuedThreadPool<SIP_Work>
1037 {
1038 public:
1039 WorkThreadPool(unsigned maxWorkers)
1040 : PQueuedThreadPool<SIP_Work>(maxWorkers)
1041 { }
1042 virtual WorkerThreadBase * CreateWorkerThread();
1043 } m_connectionThreadPool, m_handlerThreadPool;
1044
1045
1046 // Network interface checking
1047 enum {
1048 HighPriority = 80,
1049 LowPriority = 30,
1050 };
1051 class InterfaceMonitor : public PInterfaceMonitorClient
1052 {
1053 PCLASSINFO(InterfaceMonitor, PInterfaceMonitorClient);
1054 public:
1055 InterfaceMonitor(SIPEndPoint & manager, PINDEX priority);
1056
1057 virtual void OnAddInterface(const PIPSocket::InterfaceEntry & entry);
1058 virtual void OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
1059
1060 protected:
1061 SIPEndPoint & m_endpoint;
1062 };
1063 InterfaceMonitor m_highPriorityMonitor;
1064 InterfaceMonitor m_lowPriorityMonitor;
1065
1066 friend void InterfaceMonitor::OnAddInterface(const PIPSocket::InterfaceEntry & entry);
1067 friend void InterfaceMonitor::OnRemoveInterface(const PIPSocket::InterfaceEntry & entry);
1068
1069 bool m_disableTrying;
1070
1071 P_REMOVE_VIRTUAL_VOID(OnReceivedIntervalTooBrief(SIPTransaction &, SIP_PDU &));
1072 P_REMOVE_VIRTUAL_VOID(OnReceivedAuthenticationRequired(SIPTransaction &, SIP_PDU &));
1073 P_REMOVE_VIRTUAL_VOID(OnReceivedOK(SIPTransaction &, SIP_PDU &));
1074 P_REMOVE_VIRTUAL_VOID(OnMessageFailed(const SIPURL &, SIP_PDU::StatusCodes));
1075 };
1076
1077
1078 #endif // OPAL_SIP
1079
1080 #endif // OPAL_SIP_SIPEP_H
1081
1082
1083 // End of File ///////////////////////////////////////////////////////////////