OPAL  Version 3.14.3
handlers.h
Go to the documentation of this file.
1 /*
2  * handlers.h
3  *
4  * Session Initiation Protocol non-connection protocol handlers.
5  *
6  * Open Phone Abstraction Library (OPAL)
7  *
8  * Copyright (c) 2000 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Open Phone Abstraction Library.
21  *
22  * The Initial Developer of the Original Code is Damien Sandras.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 30068 $
27  * $Author: rjongbloed $
28  * $Date: 2013-06-28 09:41:24 +1000 (Fri, 28 Jun 2013) $
29  */
30 
31 #ifndef OPAL_SIP_HANDLERS_H
32 #define OPAL_SIP_HANDLERS_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #ifndef _PTLIB_H
39 #include <ptlib.h>
40 #endif
41 
42 #include <opal_config.h>
43 
44 #if OPAL_SIP
45 
46 #include <opal/pres_ent.h>
47 #include <sip/sippdu.h>
48 
49 
51 class SIPHandlerBase : public PSafeObject
52 {
53  PCLASSINFO(SIPHandlerBase, PSafeObject);
54 
55  protected:
56  SIPHandlerBase(const PString & callID) : m_callID(callID) { }
57 
58  public:
59  const PString & GetCallID() const
60  { return m_callID; }
61 
62  protected:
63  const PString m_callID;
64 
65  // Keep a copy of the keys used for easy removal on destruction
66  typedef std::map<PString, PSafePtr<SIPHandler> > IndexMap;
67  std::pair<IndexMap::iterator, bool> m_byAorAndPackage;
68  std::pair<IndexMap::iterator, bool> m_byAuthIdAndRealm;
69  std::pair<IndexMap::iterator, bool> m_byAorUserAndRealm;
70 
71  friend class SIPHandlersList;
72 };
73 
74 
75 /* Class to handle SIP REGISTER, SUBSCRIBE, MESSAGE, and renew
76  * the 'bindings' before they expire.
77  */
79 {
80  PCLASSINFO(SIPHandler, SIPHandlerBase);
81 
82 protected:
83  SIPHandler(
84  SIP_PDU::Methods method,
85  SIPEndPoint & ep,
86  const SIPParameters & params,
87  const PString & callID = SIPTransaction::GenerateCallID()
88  );
89 
90 public:
91  ~SIPHandler();
92 
93  virtual Comparison Compare(const PObject & other) const;
94 
95  // Overrides from SIPTransactionOwner
96  virtual PString GetAuthID() const { return m_username; }
97  virtual PString GetPassword() const { return m_password; }
98 
99  enum State {
100  Subscribed, // The registration is active
101  Subscribing, // The registration is in process
102  Unavailable, // The registration is offline and still being attempted
103  Refreshing, // The registration is being refreshed
104  Restoring, // The registration is trying to be restored after being offline
105  Unsubscribing, // The unregistration is in process
106  Unsubscribed, // The registrating is inactive
108  };
109 
110  void SetState (SIPHandler::State s);
111 
113  { return m_state; }
114 
115  virtual const SIPURL & GetAddressOfRecord() const
116  { return m_addressOfRecord; }
117 
118  virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
119 
120  virtual void SetExpire(int e);
121 
122  virtual int GetExpire() const
123  { return m_currentExpireTime; }
124 
125  virtual void SetBody(const PString & /*body*/) { }
126 
127  virtual bool IsDuplicateCSeq(unsigned ) { return false; }
128 
129  virtual SIPTransaction * CreateTransaction(OpalTransport &) { return NULL; }
130 
131  SIP_PDU::Methods GetMethod() const { return m_method; }
133 
134  virtual void OnReceivedResponse(SIPTransaction & transaction, SIP_PDU & response);
135  virtual void OnReceivedIntervalTooBrief(SIPTransaction & transaction, SIP_PDU & response);
136  virtual void OnReceivedTemporarilyUnavailable(SIPTransaction & transaction, SIP_PDU & response);
137  virtual void OnReceivedAuthenticationRequired(SIPTransaction & transaction, SIP_PDU & response);
138  virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
139  virtual void OnTransactionFailed(SIPTransaction & transaction);
140 
141 // virtual void OnFailed(const SIP_PDU & response);
142  virtual void OnFailed(SIP_PDU::StatusCodes);
143  virtual void SendStatus(SIP_PDU::StatusCodes code, State state);
144 
145  bool ActivateState(SIPHandler::State state, bool resetInterface = false);
146  virtual bool SendNotify(const PObject * /*body*/) { return false; }
147 
149 
150  const OpalProductInfo & GetProductInfo() const { return m_productInfo; }
151 
152  const PString & GetRealm() const { return m_realm; }
153 
154  virtual bool ShutDown();
155 
156 protected:
157  virtual PBoolean SendRequest(SIPHandler::State state);
158  void RetryLater(unsigned after);
159  void OnExpireTimeout();
160  PDECLARE_WriteConnectCallback(SIPHandler, WriteTransaction);
161 
162  PString m_username;
163  PString m_password;
164  PString m_realm;
165 
169 
170  unsigned m_lastCseq;
176  std::queue<State> m_stateQueue;
180 };
181 
182 #if PTRACING
183 ostream & operator<<(ostream & strm, SIPHandler::State state);
184 #endif
185 
186 
188 {
189  PCLASSINFO(SIPRegisterHandler, SIPHandler);
190 
191 public:
193  SIPEndPoint & ep,
194  const SIPRegister::Params & params
195  );
196 
198  virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
199 
200  void UpdateParameters(const SIPRegister::Params & params);
201 
202  const SIPRegister::Params & GetParams() const { return m_parameters; }
203 
204  const SIPURLList & GetContacts() const { return m_contactAddresses; }
205  const SIPURLList & GetServiceRoute() const { return m_serviceRoute; }
206 
207 protected:
208  virtual PBoolean SendRequest(SIPHandler::State state);
209  virtual void SendStatus(SIP_PDU::StatusCodes code, State state);
210  PString CreateRegisterContact(const OpalTransportAddress & address, int q);
211 
217  PAtomicInteger::IntegerType m_rfc5626_reg_id;
218 };
219 
220 
222 {
223  PCLASSINFO(SIPSubscribeHandler, SIPHandler);
224 public:
227 
229  virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
230  virtual PBoolean OnReceivedNOTIFY(SIP_PDU & response);
231  virtual void OnFailed(SIP_PDU::StatusCodes);
233  { return m_parameters.m_eventPackage; }
234 
235  void UpdateParameters(const SIPSubscribe::Params & params);
236 
237  const SIPSubscribe::Params & GetParams() const { return m_parameters; }
238 
239 protected:
240  virtual void WriteTransaction(OpalTransport & transport, bool & succeeded);
241  virtual void SendStatus(SIP_PDU::StatusCodes code, State state);
242  bool DispatchNOTIFY(SIP_PDU & request, SIP_PDU & response);
243 
247 };
248 
249 
251 {
252  PCLASSINFO(SIPNotifyHandler, SIPHandler);
253 public:
255  SIPEndPoint & ep,
256  const SIPEventPackage & eventPackage,
257  const SIPDialogContext & dialog
258  );
260 
263  { return m_eventPackage; }
264 
265  virtual void SetBody(const PString & body) { m_body = body; }
266 
267  virtual bool SendNotify(const PObject * body);
268 
269  enum Reasons {
276  };
277 
278 protected:
279  virtual PBoolean SendRequest(SIPHandler::State state);
280  virtual void WriteTransaction(OpalTransport & transport, bool & succeeded);
281 
285  PString m_body;
286 };
287 
288 
290 {
291  PCLASSINFO(SIPPublishHandler, SIPHandler);
292 
293 public:
295  const SIPSubscribe::Params & params,
296  const PString & body);
297 
298  virtual void SetBody(const PString & body) { m_body = body; }
299 
301  virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
303 
304 protected:
306  PString m_body;
307  PString m_sipETag;
308 };
309 
310 
312 {
313  PCLASSINFO(SIPMessageHandler, SIPHandler);
314 public:
315  SIPMessageHandler(SIPEndPoint & ep, const SIPMessage::Params & params);
316 
318  virtual void OnFailed(SIP_PDU::StatusCodes);
319  virtual void OnReceivedOK(SIPTransaction & transaction, SIP_PDU & response);
320 
321  void UpdateParameters(const SIPMessage::Params & params);
322 
323 protected:
326 };
327 
328 
330 {
331  PCLASSINFO(SIPPingHandler, SIPHandler);
332 public:
333  SIPPingHandler(SIPEndPoint & ep, const PURL & to);
335 };
336 
337 
342 {
343  public:
346  void Append(SIPHandler * handler);
347 
352  void Remove(SIPHandler * handler);
353 
356  void Update(SIPHandler * handler);
357 
361  { return m_handlersList.DeleteObjectsToBeRemoved(); }
362 
366  PSafePtr<SIPHandler> GetFirstHandler(PSafetyMode mode = PSafeReference) const
367  { return PSafePtr<SIPHandler>(m_handlersList, mode); }
368 
372  unsigned GetCount(SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
373 
377  PStringList GetAddresses(bool includeOffline, SIP_PDU::Methods meth, const PString & eventPackage = PString::Empty()) const;
378 
382  PSafePtr<SIPHandler> FindSIPHandlerByCallID(const PString & callID, PSafetyMode m);
383 
387  PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, PSafetyMode m);
388 
392  PSafePtr<SIPHandler> FindSIPHandlerByAuthRealm(const PString & authRealm, const PString & userName, PSafetyMode m);
393 
401  PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, PSafetyMode m);
402  PSafePtr<SIPHandler> FindSIPHandlerByUrl(const PString & url, SIP_PDU::Methods meth, const PString & eventPackage, PSafetyMode m);
403 
409  PSafePtr <SIPHandler> FindSIPHandlerByDomain(const PString & name, SIP_PDU::Methods meth, PSafetyMode m);
410 
411  protected:
412  void RemoveIndexes(SIPHandler * handler);
413 
414  PSafeSortedList<SIPHandlerBase> m_handlersList;
415 
417  PSafePtr<SIPHandler> FindBy(IndexMap & by, const PString & key, PSafetyMode m);
418 
422 };
423 
424 
425 #if OPAL_SIP_PRESENCE
426 
429 class SIPPresenceInfo : public OpalPresenceInfo
430 {
431  PCLASSINFO_WITH_CLONE(SIPPresenceInfo, OpalPresenceInfo)
432 
433 public:
434  // presence agent
435  PString m_presenceAgent;
436  PString m_personId;
437 
438  PString AsXML() const;
439 
440  static bool ParseNotify(
442  list<SIPPresenceInfo> & info
443  );
444  static bool ParseXML(
445  const PXML & xml,
446  list<SIPPresenceInfo> & info
447  );
448 
449  void PrintOn(ostream & strm) const;
450  friend ostream & operator<<(ostream & strm, const SIPPresenceInfo & info) { info.PrintOn(strm); return strm; }
451 
452  // Constructor
453  SIPPresenceInfo(
454  State state = Unchanged
455  ) : OpalPresenceInfo(state) { }
456  SIPPresenceInfo(
457  SIP_PDU::StatusCodes status,
458  bool subscribing
459  );
460  SIPPresenceInfo(
461  const OpalPresenceInfo & info
462  ) : OpalPresenceInfo(info) { }
463 };
464 #endif // OPAL_SIP_PRESENCE
465 
466 
469 struct SIPDialogNotification : public PObject
470 {
472 
473  P_DECLARE_ENUM(States,
474  Terminated,
475  Trying,
476  Proceeding,
477  Early,
478  Confirmed
479  );
480  static PString GetStateName(States state);
481  PString GetStateName() const { return GetStateName(m_state); }
482 
483  P_DECLARE_ENUM_EX(Events, NumEvents,
484  NoEvent, -1,
485  Cancelled,
486  Rejected,
487  Replaced,
488  LocalBye,
489  RemoteBye,
490  Error,
491  Timeout
492  );
493  static PString GetEventName(Events state);
494  PString GetEventName() const { return GetEventName(m_eventType); }
495 
496  enum Rendering {
500  };
501 
502  PString m_entity;
503  PString m_dialogId;
504  PString m_callId;
506  States m_state;
507  Events m_eventType;
508  unsigned m_eventCode;
509  struct Participant {
511  PString m_URI;
512  PString m_dialogTag;
513  PString m_identity;
514  PString m_display;
516  bool m_byeless;
518  } m_local, m_remote;
519 
520  SIPDialogNotification(const PString & entity = PString::Empty());
521 
522  void PrintOn(ostream & strm) const;
523 };
524 
525 
528 class SIPRegNotification : public PObject
529 {
530  PCLASSINFO(SIPRegNotification, PObject)
531 public:
532  P_DECLARE_ENUM(States,
533  Initial,
534  Active,
535  Terminated
536  );
537  static PString GetStateName(States state);
538  PString GetStateName() const { return GetStateName(m_state); }
539  static States GetStateFromName(const PCaselessString & str);
540 
542  const SIPURL & aor = SIPURL(),
543  States state = Initial
544  );
545 
547  States m_state;
548  std::list<SIPURL> m_contacts;
549 
550  void PrintOn(ostream & strm) const;
551 };
552 
553 
554 #endif // OPAL_SIP
555 
556 #endif // OPAL_SIP_HANDLERS_H