OPAL  Version 3.12.9
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: 30861 $
28  * $Author: rjongbloed $
29  * $Date: 2013-11-07 13:08:39 +1100 (Thu, 07 Nov 2013) $
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 <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();
184 
191  const SIPConnection::Init & init
192 
193  );
194 
199  time_t sessionId,
200  unsigned version,
201  const OpalTransportAddress & address
202  );
203 
206  virtual PBoolean SetupTransfer(
207  const PString & token,
208  const PString & callIdentity,
209  const PString & remoteParty,
210  void * userData = NULL
211  );
212 
216  virtual PBoolean ForwardConnection(
217  SIPConnection & connection,
218  const PString & forwardParty
219  );
220 
231  bool ClearDialogContext(
232  const PString & descriptor
233  );
234  bool ClearDialogContext(
235  SIPDialogContext & context
236  );
238 
241 
247  const SIPTransactionOwner & transactor,
248  SIP_PDU::StatusCodes & reason
249  );
250 
251  virtual void HandlePDU(
252  const OpalTransportPtr & transport
253  );
254 
258  virtual bool OnReceivedPDU(
259  SIP_PDU * pdu
260  );
261 
264  virtual void OnReceivedResponse(
265  SIPTransaction & transaction,
266  SIP_PDU & response
267  );
268 
272  virtual bool OnReceivedINVITE(
273  SIP_PDU * pdu
274  );
275 
279  virtual bool OnReceivedNOTIFY(
280  SIP_PDU & request
281  );
282 
286  virtual bool OnReceivedREGISTER(
287  SIP_PDU & request
288  );
289 
293  virtual bool OnReceivedSUBSCRIBE(
294  SIP_PDU & request,
295  SIPDialogContext * dialog
296  );
297 
301  virtual bool OnReceivedMESSAGE(
302  SIP_PDU & request
303  );
304 
308  virtual bool OnReceivedOPTIONS(
309  SIP_PDU & request
310  );
311 
314  virtual void OnTransactionFailed(
315  SIPTransaction & transaction
316  );
318 
319 
327  PSafePtr<SIPConnection> GetSIPConnectionWithLock(
328  const PString & token,
329  PSafetyMode mode = PSafeReadWrite,
330  SIP_PDU::StatusCodes * errorCode = NULL
331  );
332 
333  virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
334 
335 
370  bool Register(
371  const SIPRegister::Params & params,
372  PString & aor,
373  SIP_PDU::StatusCodes * reason = NULL
374  );
375 
376  // For backward compatibility
377  bool Register(
378  const SIPRegister::Params & params,
379  PString & aor,
380  bool asynchronous
381  );
382 
384  bool P_DEPRECATED Register(
385  const PString & host,
386  const PString & user = PString::Empty(),
387  const PString & autName = PString::Empty(),
388  const PString & password = PString::Empty(),
389  const PString & authRealm = PString::Empty(),
390  unsigned expire = 0,
391  const PTimeInterval & minRetryTime = PMaxTimeInterval,
392  const PTimeInterval & maxRetryTime = PMaxTimeInterval
393  );
394 
409  PBoolean IsRegistered(
410  const PString & aor,
411  bool includeOffline = false
412  );
413 
423  bool Unregister(
424  const PString & aor
425  );
426 
430  bool UnregisterAll();
431 
435 
438  PStringList GetRegistrations(
439  bool includeOffline = false
440  ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER); }
441 
450  void * m_userData;
451  };
452 
457  const PString & token,
458  RegistrationStatus & status
459  );
460 
463  virtual void OnRegistrationStatus(
464  const RegistrationStatus & status
465  );
466 
467  // For backward compatibility
468  virtual void OnRegistrationStatus(
469  const PString & aor,
470  PBoolean wasRegistering,
471  PBoolean reRegistering,
472  SIP_PDU::StatusCodes reason
473  );
474 
478  virtual void OnRegistrationFailed(
479  const PString & aor,
480  SIP_PDU::StatusCodes reason,
481  PBoolean wasRegistering
482  );
483 
487  virtual void OnRegistered(
488  const PString & aor,
489  PBoolean wasRegistering
490  );
491 
492 
532  bool Subscribe(
533  const SIPSubscribe::Params & params,
534  PString & token,
535  bool tokenIsAOR = true
536  );
537 
538  // For backward compatibility
539  bool Subscribe(
540  SIPSubscribe::PredefinedPackages eventPackage,
541  unsigned expire,
542  const PString & aor
543  );
544 
551  bool IsSubscribed(
552  const PString & aor,
553  bool includeOffline = false
554  );
555  bool IsSubscribed(
556  const PString & eventPackage,
557  const PString & aor,
558  bool includeOffline = false
559  );
560 
570  bool Unsubscribe(
571  const PString & aor,
572  bool invalidateNotifiers = false
573  );
574  bool Unsubscribe(
575  SIPSubscribe::PredefinedPackages eventPackage,
576  const PString & aor,
577  bool invalidateNotifiers = false
578  );
579  bool Unsubscribe(
580  const PString & eventPackage,
581  const PString & aor,
582  bool invalidateNotifiers = false
583  );
584 
588  bool UnsubcribeAll(
590  );
591  bool UnsubcribeAll(
592  const PString & eventPackage
593  );
594 
598  const SIPSubscribe::EventPackage & eventPackage
599  ) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
600 
603  PStringList GetSubscriptions(
604  const SIPSubscribe::EventPackage & eventPackage,
605  bool includeOffline = false
606  ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_REGISTER, eventPackage); }
607 
610 
615  const PString & token,
616  const PString & eventPackage,
617  SubscriptionStatus & status
618  );
619 
622  virtual void OnSubscriptionStatus(
623  const SubscriptionStatus & status
624  );
625 
629  virtual void OnSubscriptionStatus(
630  const PString & eventPackage,
631  const SIPURL & uri,
632  bool wasSubscribing,
633  bool reSubscribing,
634  SIP_PDU::StatusCodes reason
635  );
636 
637  virtual void OnSubscriptionStatus(
638  SIPSubscribeHandler & handler,
639  const SIPURL & uri,
640  bool wasSubscribing,
641  bool reSubscribing,
642  SIP_PDU::StatusCodes reason
643  );
644 
656  };
657 
660  virtual CanNotifyResult CanNotify(
661  const PString & eventPackage,
662  const SIPURL & aor
663  );
664 
665  // For backward compatobility
666  virtual bool CanNotify(const PString & eventPackage);
667 
670  bool Notify(
671  const SIPURL & targetAddress,
672  const PString & eventPackage,
673  const PObject & body
674  );
675 
676 
679  virtual void OnDialogInfoReceived(
680  const SIPDialogNotification & info
681  );
682 
684  const SIPDialogNotification & info
685  );
686 
687 
690  virtual void OnRegInfoReceived(
691  const SIPRegNotification & info
692  );
693 
694 
697  bool SendMESSAGE(
698  SIPMessage::Params & params
699  );
700 
703  virtual void OnMESSAGECompleted(
704  const SIPMessage::Params & params,
705  SIP_PDU::StatusCodes reason
706  );
707 
710  : m_pdu(pdu)
711  , m_transport(*pdu.GetTransport())
713  { }
714 
717  enum {
722  } m_status;
723  };
724 
725  typedef PNotifierTemplate<ConnectionlessMessageInfo &> ConnectionlessMessageNotifier;
726  #define PDECLARE_ConnectionlessMessageNotifier(cls, fn) PDECLARE_NOTIFIER2(SIPEndPoint, cls, fn, SIPEndPoint::ConnectionlessMessageInfo &)
727  #define PCREATE_ConnectionlessMessageNotifier(fn) PCREATE_NOTIFIER2(fn, SIPEndPoint::ConnectionlessMessageInfo &)
728 
730  const ConnectionlessMessageNotifier & notifier
731  )
732  { m_onConnectionlessMessage = notifier; }
733 
734 
737  virtual bool SendOPTIONS(
738  const SIPOptions::Params & params
739  );
740 
744  virtual void OnOptionsCompleted(
745  const SIPOptions::Params & params,
746  const SIP_PDU & response
747  );
748 
749 
753  bool Publish(
754  const SIPSubscribe::Params & params,
755  const PString & body,
756  PString & aor
757  );
758  bool Publish(
759  const PString & to,
760  const PString & body,
761  unsigned expire = 300
762  );
763 
766  PStringList GetPublications(
767  const SIPSubscribe::EventPackage & eventPackage,
768  bool includeOffline = false
769  ) const { return activeSIPHandlers.GetAddresses(includeOffline, SIP_PDU::Method_PUBLISH, eventPackage); }
770 
771 
772 #if OPAL_SIP_PRESENCE
773 
776  bool PublishPresence(
777  const SIPPresenceInfo & info,
778  unsigned expire = 300
779  );
780 
783  virtual void OnPresenceInfoReceived (
784  const SIPPresenceInfo & info
785  );
786  virtual void OnPresenceInfoReceived (
787  const PString & identity,
788  const PString & basic,
789  const PString & note
790  );
791 #endif // OPAL_SIP_PRESENCE
792 
793 
796  PBoolean Ping(
797  const PURL & to
798  );
799 
802  const PStringSet & GetAllowedEvents() const { return m_allowedEvents; }
803 
807 
811 
812  void SetMaxPacketSizeUDP(PINDEX sz) { m_maxPacketSizeUDP = sz; }
813  PINDEX GetMaxPacketSizeUDP() const { return m_maxPacketSizeUDP; }
814 
815  void SetMaxRetries(unsigned r) { maxRetries = r; }
816  unsigned GetMaxRetries() const { return maxRetries; }
817 
819  const PTimeInterval & t1,
820  const PTimeInterval & t2
821  ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
822  const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
823  const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
824 
826  const PTimeInterval & t
827  ) { nonInviteTimeout = t; }
828  const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
829 
831  const PTimeInterval & t
832  ) { pduCleanUpTimeout = t; }
833  const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
834 
836  const PTimeInterval & t
837  ) { inviteTimeout = t; }
838  const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
839 
841  const PTimeInterval & t
842  ) { m_progressTimeout = t; }
843  const PTimeInterval & GetProgressTimeout() const { return m_progressTimeout; }
844 
846  const PTimeInterval & t
847  ) { ackTimeout = t; }
848  const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
849 
851  const PTimeInterval & t
852  ) { registrarTimeToLive = t; }
853  const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
854 
856  const PTimeInterval & t
857  ) { notifierTimeToLive = t; }
858  const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
859 
860  P_DEPRECATED void SetNATBindingTimeout(
861  const PTimeInterval & t
862  ) { m_keepAliveTimeout = t; }
863  P_DEPRECATED const PTimeInterval & GetNATBindingTimeout() const { return m_keepAliveTimeout; }
864 
872  };
874  PTimeInterval & timeout,
875  KeepAliveType & type
876  ) { timeout = m_keepAliveTimeout; type = m_keepAliveType; }
878  const PTimeInterval & timeout,
879  KeepAliveType type
880  ) { m_keepAliveTimeout = timeout; m_keepAliveType = type; }
881 
882 
884  SIPTransaction * transaction
885  ) { m_transactions.Append(transaction); }
886 
887  PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
888  { return PSafePtrCast<SIPTransactionBase, SIPTransaction>(m_transactions.FindWithLock(transactionID, mode)); }
889 
892  unsigned GetNextCSeq() { return ++m_lastSentCSeq; }
893 
901 
904  bool GetAuthentication(
905  const PString & realm,
906  PString & authId,
907  PString & password
908  );
909 
912  virtual SIPURL GetDefaultLocalURL(const OpalTransport & transport);
913 
923  SIP_PDU & pdu,
924  SIPConnection * connection = NULL,
925  const OpalTransport * transport = NULL
926  );
927 
928 
931  const SIPURL & GetProxy() const { return m_proxy; }
932 
935  void SetProxy(const SIPURL & url);
936 
939  void SetProxy(
940  const PString & hostname,
941  const PString & username,
942  const PString & password
943  );
944 
945 
949 
953 
960  virtual PString GetUserAgent() const;
961 
964  void SetUserAgent(const PString & str) { m_userAgentString = str; }
965 
966 
969  virtual unsigned GetAllowedMethods() const;
970 
971 
974 
976 
977  virtual void OnStartTransaction(SIPConnection & conn, SIPTransaction & transaction);
978 
979 
980  PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m)
981  { return activeSIPHandlers.FindSIPHandlerByCallID(callID, m); }
982 
984  { activeSIPHandlers.Update(handler); }
985 
986 
988 
989 
990  protected:
991  void AddTransport(const OpalTransportPtr & transport);
992  void TransportThreadMain(OpalTransportPtr transport);
993 
996  PStringSet m_allowedEvents;
997 
999 
1001  unsigned maxRetries;
1002  PTimeInterval retryTimeoutMin; // T1
1003  PTimeInterval retryTimeoutMax; // T2
1004  PTimeInterval nonInviteTimeout; // T3
1005  PTimeInterval pduCleanUpTimeout; // T4
1006  PTimeInterval inviteTimeout;
1007  PTimeInterval m_progressTimeout;
1008  PTimeInterval ackTimeout;
1009  PTimeInterval registrarTimeToLive;
1010  PTimeInterval notifierTimeToLive;
1011  PTimeInterval natBindingTimeout;
1012  PTimeInterval m_keepAliveTimeout;
1016 
1017  // Transport management
1018  PSafeDictionary<OpalTransportAddress, OpalTransport> m_transportsTable;
1019 
1020  // Sub-protocol handlers
1022  PSafePtr<SIPHandler> FindHandlerByPDU(const SIP_PDU & pdu, PSafetyMode mode);
1023 
1026 
1027  PSafeSortedList<SIPTransactionBase> m_transactions;
1028 
1029  PAtomicInteger m_lastSentCSeq;
1031 
1033  PSyncPoint m_sync;
1035  RegistrationCompletion() : m_reason(SIP_PDU::Information_Trying) { }
1036  };
1037  std::map<PString, RegistrationCompletion> m_registrationComplete;
1038 
1040  typedef std::multimap<PString, SIPURL> ConferenceMap;
1042 
1043  // Thread pooling
1045 
1046  // Network interface checking
1047  PDECLARE_InterfaceNotifier(SIPEndPoint, OnHighPriorityInterfaceChange);
1048  PDECLARE_InterfaceNotifier(SIPEndPoint, OnLowPriorityInterfaceChange);
1049  PInterfaceMonitor::Notifier m_onHighPriorityInterfaceChange;
1050  PInterfaceMonitor::Notifier m_onLowPriorityInterfaceChange;
1051 
1053 
1054  P_REMOVE_VIRTUAL_VOID(OnReceivedIntervalTooBrief(SIPTransaction &, SIP_PDU &));
1055  P_REMOVE_VIRTUAL_VOID(OnReceivedAuthenticationRequired(SIPTransaction &, SIP_PDU &));
1056  P_REMOVE_VIRTUAL_VOID(OnReceivedOK(SIPTransaction &, SIP_PDU &));
1057  P_REMOVE_VIRTUAL_VOID(OnMessageFailed(const SIPURL &, SIP_PDU::StatusCodes));
1058  P_REMOVE_VIRTUAL(SIPConnection *,CreateConnection(OpalCall &, const PString &, void *, const SIPURL &, OpalTransport *, SIP_PDU *, unsigned, OpalConnection::StringOptions *), NULL);
1059  P_REMOVE_VIRTUAL(PBoolean, OnReceivedPDU(OpalTransport &, SIP_PDU *), false);
1060  P_REMOVE_VIRTUAL(bool, OnReceivedConnectionlessPDU(OpalTransport &, SIP_PDU *), false);
1061  P_REMOVE_VIRTUAL(bool, OnReceivedConnectionlessPDU(SIP_PDU *), false);
1062  P_REMOVE_VIRTUAL(PBoolean, OnReceivedINVITE(OpalTransport &, SIP_PDU *), false);
1063  P_REMOVE_VIRTUAL(PBoolean, OnReceivedNOTIFY(OpalTransport &, SIP_PDU &), false);
1064  P_REMOVE_VIRTUAL(PBoolean, OnReceivedREGISTER(OpalTransport &, SIP_PDU &), false);
1068 };
1069 
1070 
1071 #endif // OPAL_SIP
1072 
1073 #endif // OPAL_SIP_SIPEP_H
1074 
1075 
1076 // End of File ///////////////////////////////////////////////////////////////