OPAL  Version 3.14.3
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: 32514 $
28  * $Author: ededu $
29  * $Date: 2014-08-13 18:51:27 +1000 (Wed, 13 Aug 2014) $
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_config.h>
44 
45 #if OPAL_SIP
46 
47 #include <rtp/rtpep.h>
48 #include <sip/sipcon.h>
49 #include <sip/handlers.h>
50 
51 
53 
57 {
58  PCLASSINFO(SIPEndPoint, OpalRTPEndPoint);
59  public:
66  unsigned maxThreads = 15
67  );
68 
71  ~SIPEndPoint();
73 
80  virtual void ShutDown();
81 
85  virtual PString GetDefaultTransport() const;
86 
89  virtual WORD GetDefaultSignalPort() const;
90 
95  virtual PStringList GetNetworkURIs(
96  const PString & name
97  ) const;
98 
101  virtual void NewIncomingConnection(
102  OpalListener & listener,
103  const OpalTransportPtr & transport
104  );
105 
135  virtual PSafePtr<OpalConnection> MakeConnection(
136  OpalCall & call,
137  const PString & party,
138  void * userData,
139  unsigned int options,
140  OpalConnection::StringOptions * stringOptions
141  );
142 
161  virtual void OnReleased(
162  OpalConnection & connection
163  );
164 
172  virtual void OnConferenceStatusChanged(
173  OpalEndPoint & endpoint,
174  const PString & uri,
176  );
177 
182  virtual PBoolean GarbageCollection();
183 
186  virtual PStringList GetAvailableStringOptions() const;
188 
195  const SIPConnection::Init & init
196 
197  );
198 
202  virtual SDPSessionDescription * CreateSDP(
203  time_t sessionId,
204  unsigned version,
205  const OpalTransportAddress & address
206  );
207 
210  virtual PBoolean SetupTransfer(
211  const PString & token,
212  const PString & callIdentity,
213  const PString & remoteParty,
214  void * userData = NULL
215  );
216 
220  virtual PBoolean ForwardConnection(
221  SIPConnection & connection,
222  const PString & forwardParty
223  );
224 
235  bool ClearDialogContext(
236  const PString & descriptor
237  );
238  bool ClearDialogContext(
239  SIPDialogContext & context
240  );
242 
245 
251  const SIPTransactionOwner & transactor,
252  SIP_PDU::StatusCodes & reason
253  );
254 
255  virtual void HandlePDU(
256  const OpalTransportPtr & transport
257  );
258 
262  virtual bool OnReceivedPDU(
263  SIP_PDU * pdu
264  );
265 
268  virtual void OnReceivedResponse(
269  SIPTransaction & transaction,
270  SIP_PDU & response
271  );
272 
276  virtual bool OnReceivedINVITE(
277  SIP_PDU * pdu
278  );
279 
283  virtual bool OnReceivedNOTIFY(
284  SIP_PDU & request
285  );
286 
290  virtual bool OnReceivedREGISTER(
291  SIP_PDU & request
292  );
293 
297  virtual bool OnReceivedSUBSCRIBE(
298  SIP_PDU & request,
299  SIPDialogContext * dialog
300  );
301 
305  virtual bool OnReceivedMESSAGE(
306  SIP_PDU & request
307  );
308 
312  virtual bool OnReceivedOPTIONS(
313  SIP_PDU & request
314  );
315 
318  virtual void OnTransactionFailed(
319  SIPTransaction & transaction
320  );
322 
323 
331  PSafePtr<SIPConnection> GetSIPConnectionWithLock(
332  const PString & token,
333  PSafetyMode mode = PSafeReadWrite,
334  SIP_PDU::StatusCodes * errorCode = NULL
335  );
336 
337  virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
338 
339 
374  bool Register(
375  const SIPRegister::Params & params,
376  PString & aor,
377  SIP_PDU::StatusCodes * reason = NULL
378  );
379 
380  // For backward compatibility
381  bool Register(
382  const SIPRegister::Params & params,
383  PString & aor,
384  bool asynchronous
385  );
386 
388  bool P_DEPRECATED Register(
389  const PString & host,
390  const PString & user = PString::Empty(),
391  const PString & autName = PString::Empty(),
392  const PString & password = PString::Empty(),
393  const PString & authRealm = PString::Empty(),
394  unsigned expire = 0,
395  const PTimeInterval & minRetryTime = PMaxTimeInterval,
396  const PTimeInterval & maxRetryTime = PMaxTimeInterval
397  );
398 
413  PBoolean IsRegistered(
414  const PString & aor,
415  bool includeOffline = false
416  );
417 
427  bool Unregister(
428  const PString & aor
429  );
430 
434  bool UnregisterAll();
435 
439 
442  PStringList GetRegistrations(
443  bool includeOffline = false
444  ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER); }
445 
454  void * m_userData;
455  friend ostream & operator<<(ostream & strm, const RegistrationStatus & status);
456  };
457 
462  const PString & token,
463  RegistrationStatus & status
464  );
465 
468  virtual void OnRegistrationStatus(
469  const RegistrationStatus & status
470  );
471 
472  // For backward compatibility
473  virtual void OnRegistrationStatus(
474  const PString & aor,
475  PBoolean wasRegistering,
476  PBoolean reRegistering,
477  SIP_PDU::StatusCodes reason
478  );
479 
483  virtual void OnRegistrationFailed(
484  const PString & aor,
485  SIP_PDU::StatusCodes reason,
486  PBoolean wasRegistering
487  );
488 
492  virtual void OnRegistered(
493  const PString & aor,
494  PBoolean wasRegistering
495  );
496 
497 
537  bool Subscribe(
538  const SIPSubscribe::Params & params,
539  PString & token,
540  bool tokenIsAOR = true
541  );
542 
543  // For backward compatibility
544  bool Subscribe(
545  SIPSubscribe::PredefinedPackages eventPackage,
546  unsigned expire,
547  const PString & aor
548  );
549 
556  bool IsSubscribed(
557  const PString & aor,
558  bool includeOffline = false
559  );
560  bool IsSubscribed(
561  const PString & eventPackage,
562  const PString & aor,
563  bool includeOffline = false
564  );
565 
575  bool Unsubscribe(
576  const PString & aor,
577  bool invalidateNotifiers = false
578  );
579  bool Unsubscribe(
580  SIPSubscribe::PredefinedPackages eventPackage,
581  const PString & aor,
582  bool invalidateNotifiers = false
583  );
584  bool Unsubscribe(
585  const PString & eventPackage,
586  const PString & aor,
587  bool invalidateNotifiers = false
588  );
589 
593  bool UnsubcribeAll(
595  );
596  bool UnsubcribeAll(
597  const PString & eventPackage
598  );
599 
603  const SIPSubscribe::EventPackage & eventPackage
604  ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
605 
608  PStringList GetSubscriptions(
609  const SIPSubscribe::EventPackage & eventPackage,
610  bool includeOffline = false
611  ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
612 
615 
620  const PString & token,
621  const PString & eventPackage,
622  SubscriptionStatus & status
623  );
624 
627  virtual void OnSubscriptionStatus(
628  const SubscriptionStatus & status
629  );
630 
634  virtual void OnSubscriptionStatus(
635  const PString & eventPackage,
636  const SIPURL & uri,
637  bool wasSubscribing,
638  bool reSubscribing,
639  SIP_PDU::StatusCodes reason
640  );
641 
642  virtual void OnSubscriptionStatus(
643  SIPSubscribeHandler & handler,
644  const SIPURL & uri,
645  bool wasSubscribing,
646  bool reSubscribing,
647  SIP_PDU::StatusCodes reason
648  );
649 
661  };
662 
665  virtual CanNotifyResult CanNotify(
666  const PString & eventPackage,
667  const SIPURL & aor
668  );
669 
670  // For backward compatobility
671  virtual bool CanNotify(const PString & eventPackage);
672 
675  bool Notify(
676  const SIPURL & targetAddress,
677  const PString & eventPackage,
678  const PObject & body
679  );
680 
681 
684  virtual void OnDialogInfoReceived(
685  const SIPDialogNotification & info
686  );
687 
689  const SIPDialogNotification & info
690  );
691 
692 
695  virtual void OnRegInfoReceived(
696  const SIPRegNotification & info
697  );
698 
699 
702  bool SendMESSAGE(
703  SIPMessage::Params & params
704  );
705 
708  virtual void OnMESSAGECompleted(
709  const SIPMessage::Params & params,
710  SIP_PDU::StatusCodes reason
711  );
712 
715  : m_pdu(pdu)
716  , m_transport(*pdu.GetTransport())
718  { }
719 
722  enum {
727  } m_status;
728  };
729 
730  typedef PNotifierTemplate<ConnectionlessMessageInfo &> ConnectionlessMessageNotifier;
731  #define PDECLARE_ConnectionlessMessageNotifier(cls, fn) PDECLARE_NOTIFIER2(SIPEndPoint, cls, fn, SIPEndPoint::ConnectionlessMessageInfo &)
732  #define PCREATE_ConnectionlessMessageNotifier(fn) PCREATE_NOTIFIER2(fn, SIPEndPoint::ConnectionlessMessageInfo &)
733 
735  const ConnectionlessMessageNotifier & notifier
736  )
737  { m_onConnectionlessMessage = notifier; }
738 
739 
742  virtual bool SendOPTIONS(
743  const SIPOptions::Params & params
744  );
745 
749  virtual void OnOptionsCompleted(
750  const SIPOptions::Params & params,
751  const SIP_PDU & response
752  );
753 
754 
758  bool Publish(
759  const SIPSubscribe::Params & params,
760  const PString & body,
761  PString & aor
762  );
763  bool Publish(
764  const PString & to,
765  const PString & body,
766  unsigned expire = 300
767  );
768 
771  PStringList GetPublications(
772  const SIPSubscribe::EventPackage & eventPackage,
773  bool includeOffline = false
774  ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
775 
776 
777 #if OPAL_SIP_PRESENCE
778 
781  bool PublishPresence(
782  const SIPPresenceInfo & info,
783  unsigned expire = 300
784  );
785 
788  virtual void OnPresenceInfoReceived (
789  const SIPPresenceInfo & info
790  );
791  virtual void OnPresenceInfoReceived (
792  const PString & identity,
793  const PString & basic,
794  const PString & note
795  );
796 #endif // OPAL_SIP_PRESENCE
797 
798 
801  PBoolean Ping(
802  const PURL & to
803  );
804 
807  const PStringSet & GetAllowedEvents() const { return m_allowedEvents; }
808 
812 
816 
817  void SetMaxPacketSizeUDP(PINDEX sz) { m_maxPacketSizeUDP = sz; }
818  PINDEX GetMaxPacketSizeUDP() const { return m_maxPacketSizeUDP; }
819 
820  void SetMaxRetries(unsigned r) { maxRetries = r; }
821  unsigned GetMaxRetries() const { return maxRetries; }
822 
824  const PTimeInterval & t1,
825  const PTimeInterval & t2
826  ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
827  const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
828  const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
829 
831  const PTimeInterval & t
832  ) { nonInviteTimeout = t; }
833  const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
834 
836  const PTimeInterval & t
837  ) { pduCleanUpTimeout = t; }
838  const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
839 
841  const PTimeInterval & t
842  ) { inviteTimeout = t; }
843  const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
844 
846  const PTimeInterval & t
847  ) { m_progressTimeout = t; }
848  const PTimeInterval & GetProgressTimeout() const { return m_progressTimeout; }
849 
851  const PTimeInterval & t
852  ) { ackTimeout = t; }
853  const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
854 
856  const PTimeInterval & t
857  ) { registrarTimeToLive = t; }
858  const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
859 
861  const PTimeInterval & t
862  ) { notifierTimeToLive = t; }
863  const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
864 
865  P_DEPRECATED void SetNATBindingTimeout(
866  const PTimeInterval & t
867  ) { m_keepAliveTimeout = t; }
868  P_DEPRECATED const PTimeInterval & GetNATBindingTimeout() const { return m_keepAliveTimeout; }
869 
877  };
879  PTimeInterval & timeout,
880  KeepAliveType & type
881  ) { timeout = m_keepAliveTimeout; type = m_keepAliveType; }
883  const PTimeInterval & timeout,
884  KeepAliveType type
885  ) { m_keepAliveTimeout = timeout; m_keepAliveType = type; }
886 
887 
889  SIPTransaction * transaction
890  ) { m_transactions.Append(transaction); }
891 
892  PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
893  { return PSafePtrCast<SIPTransactionBase, SIPTransaction>(m_transactions.FindWithLock(transactionID, mode)); }
894 
897  unsigned GetNextCSeq() { return ++m_lastSentCSeq; }
898 
906 
909  bool GetAuthentication(
910  const PString & realm,
911  PString & authId,
912  PString & password
913  );
914 
917  virtual SIPURL GetDefaultLocalURL(const OpalTransport & transport);
918 
928  SIP_PDU & pdu,
929  SIPConnection * connection = NULL,
930  const OpalTransport * transport = NULL
931  );
932 
933 
936  const SIPURL & GetProxy() const { return m_proxy; }
937 
940  void SetProxy(const SIPURL & url);
941 
944  void SetProxy(
945  const PString & hostname,
946  const PString & username,
947  const PString & password
948  );
949 
950 
954 
958 
965  virtual PString GetUserAgent() const;
966 
969  void SetUserAgent(const PString & str) { m_userAgentString = str; }
970 
971 
974  virtual unsigned GetAllowedMethods() const;
975 
976 
979 
981 
982  virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
983 
984 
985  PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m)
986  { return activeSIPHandlers.FindSIPHandlerByCallID(callID, m); }
987 
989  { activeSIPHandlers.Update(handler); }
990 
991 
993 
994 
995  protected:
996  void AddTransport(const OpalTransportPtr & transport);
997  void TransportThreadMain(OpalTransportPtr transport);
998 
1001  PStringSet m_allowedEvents;
1002 
1004 
1006  unsigned maxRetries;
1007  PTimeInterval retryTimeoutMin; // T1
1008  PTimeInterval retryTimeoutMax; // T2
1009  PTimeInterval nonInviteTimeout; // T3
1010  PTimeInterval pduCleanUpTimeout; // T4
1011  PTimeInterval inviteTimeout;
1012  PTimeInterval m_progressTimeout;
1013  PTimeInterval ackTimeout;
1014  PTimeInterval registrarTimeToLive;
1015  PTimeInterval notifierTimeToLive;
1016  PTimeInterval m_keepAliveTimeout;
1020 
1021  // Transport management
1022  PSafeDictionary<OpalTransportAddress, OpalTransport> m_transportsTable;
1023 
1024  // Sub-protocol handlers
1026  PSafePtr<SIPHandler> FindHandlerByPDU(const SIP_PDU & pdu, PSafetyMode mode);
1027 
1030 
1031  PSafeSortedList<SIPTransactionBase> m_transactions;
1032 
1033  PAtomicInteger m_lastSentCSeq;
1035 
1037  PSyncPoint m_sync;
1039  RegistrationCompletion() : m_reason(SIP_PDU::Information_Trying) { }
1040  };
1041  std::map<PString, RegistrationCompletion> m_registrationComplete;
1042 
1044  typedef std::multimap<PString, SIPURL> ConferenceMap;
1046 
1047  // Thread pooling
1049 
1050  // Network interface checking
1051  PDECLARE_InterfaceNotifier(SIPEndPoint, OnHighPriorityInterfaceChange);
1052  PDECLARE_InterfaceNotifier(SIPEndPoint, OnLowPriorityInterfaceChange);
1053  PInterfaceMonitor::Notifier m_onHighPriorityInterfaceChange;
1054  PInterfaceMonitor::Notifier m_onLowPriorityInterfaceChange;
1055 
1057 
1058  P_REMOVE_VIRTUAL_VOID(OnReceivedIntervalTooBrief(SIPTransaction &, SIP_PDU &));
1059  P_REMOVE_VIRTUAL_VOID(OnReceivedAuthenticationRequired(SIPTransaction &, SIP_PDU &));
1060  P_REMOVE_VIRTUAL_VOID(OnReceivedOK(SIPTransaction &, SIP_PDU &));
1061  P_REMOVE_VIRTUAL_VOID(OnMessageFailed(const SIPURL &, SIP_PDU::StatusCodes));
1062  P_REMOVE_VIRTUAL(SIPConnection *,CreateConnection(OpalCall &, const PString &, void *, const SIPURL &, OpalTransport *, SIP_PDU *, unsigned, OpalConnection::StringOptions *), NULL);
1063  P_REMOVE_VIRTUAL(PBoolean, OnReceivedPDU(OpalTransport &, SIP_PDU *), false);
1064  P_REMOVE_VIRTUAL(bool, OnReceivedConnectionlessPDU(OpalTransport &, SIP_PDU *), false);
1065  P_REMOVE_VIRTUAL(bool, OnReceivedConnectionlessPDU(SIP_PDU *), false);
1066  P_REMOVE_VIRTUAL(PBoolean, OnReceivedINVITE(OpalTransport &, SIP_PDU *), false);
1067  P_REMOVE_VIRTUAL(PBoolean, OnReceivedNOTIFY(OpalTransport &, SIP_PDU &), false);
1068  P_REMOVE_VIRTUAL(PBoolean, OnReceivedREGISTER(OpalTransport &, SIP_PDU &), false);
1072 };
1073 
1074 
1075 #endif // OPAL_SIP
1076 
1077 #endif // OPAL_SIP_SIPEP_H
1078 
1079 
1080 // End of File ///////////////////////////////////////////////////////////////