OPAL  Version 3.14.3
h323trans.h
Go to the documentation of this file.
1 /*
2  * h323trans.h
3  *
4  * H.323 Transactor handler
5  *
6  * Open H323 Library
7  *
8  * Copyright (c) 2003 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 H323 Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 31891 $
27  * $Author: rjongbloed $
28  * $Date: 2014-05-14 11:49:23 +1000 (Wed, 14 May 2014) $
29  */
30 
31 #ifndef OPAL_H323_H323TRANS_H
32 #define OPAL_H323_H323TRANS_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <opal_config.h>
39 
40 #if OPAL_H323
41 
42 #include <h323/transaddr.h>
43 #include <h323/h235auth.h>
44 
45 #include <ptclib/asner.h>
46 
47 
48 class H323EndPoint;
49 
50 
52  public:
54  H323TransactionPDU(const H235Authenticators & auth);
55 
56  virtual ~H323TransactionPDU() { }
57 
58  virtual PBoolean Read(H323Transport & transport);
59  virtual PBoolean Write(H323Transport & transport);
60 
61  virtual PASN_Object & GetPDU() = 0;
62  virtual PASN_Choice & GetChoice() = 0;
63  virtual const PASN_Object & GetPDU() const = 0;
64  virtual const PASN_Choice & GetChoice() const = 0;
65  virtual unsigned GetSequenceNumber() const = 0;
66  virtual unsigned GetRequestInProgressDelay() const = 0;
67 #if PTRACING
68  virtual const char * GetProtocolName() const = 0;
69 #endif
70  virtual H323TransactionPDU * ClonePDU() const = 0;
71  virtual void DeletePDU() = 0;
72 
73  const H235Authenticators & GetAuthenticators() const { return authenticators; }
74  H235Authenticators & GetAuthenticators() { return authenticators; }
76  const H235Authenticators & auth
77  ) { authenticators = auth; }
78 
79  template <class RAS> H235Authenticator::ValidationResult Validate(const RAS & ras) const
80  {
81  return authenticators.ValidatePDU(*this, ras, rawPDU);
82  }
83 
84  template <class PDU> void Prepare(PDU & pdu)
85  {
86  authenticators.PreparePDU(*this, pdu);
87  }
88 
89  const PBYTEArray & GetRawPDU() const { return rawPDU; }
90 
91  protected:
92  mutable H235Authenticators authenticators;
93  PPER_Stream rawPDU;
94 };
95 
96 
98 
99 class H323Transactor : public PObject
100 {
101  PCLASSINFO(H323Transactor, PObject);
102  public:
105 
111  WORD localPort,
112  WORD remotePort
113  );
115  H323EndPoint & endpoint,
116  const H323TransportAddress & iface,
117  WORD localPort,
118  WORD remotePort
119  );
120 
123  ~H323Transactor();
125 
130  void PrintOn(
131  ostream & strm
132  ) const;
134 
139  PBoolean SetTransport(
140  const H323TransportAddress & iface
141  );
142 
145  virtual PBoolean StartChannel();
146 
150  virtual void StopChannel();
151 
154  virtual H323TransactionPDU * CreateTransactionPDU() const = 0;
155 
158  virtual PBoolean HandleTransaction(
159  const PASN_Object & rawPDU
160  ) = 0;
161 
164  virtual void OnSendingPDU(
165  PASN_Object & rawPDU
166  ) = 0;
167 
170  virtual PBoolean WritePDU(
171  H323TransactionPDU & pdu
172  );
173 
176  virtual PBoolean WriteTo(
177  H323TransactionPDU & pdu,
178  const H323TransportAddressArray & addresses,
179  PBoolean callback = true
180  );
182 
187  H323EndPoint & GetEndPoint() const { return endpoint; }
188 
191  H323Transport & GetTransport() const { return *transport; }
192 
196  PBoolean value
197  ) { checkResponseCryptoTokens = value; }
198 
203 
204  protected:
205  void Construct();
206 
207  unsigned GetNextSequenceNumber();
208  PBoolean SetUpCallSignalAddresses(
209  H225_ArrayOf_TransportAddress & addresses
210  );
211 
212  //Background thread handler.
213  PDECLARE_NOTIFIER(PThread, H323Transactor, HandleTransactions);
214 
215  class Request : public PObject
216  {
217  PCLASSINFO(Request, PObject);
218  public:
219  Request(
220  unsigned seqNum,
221  H323TransactionPDU & pdu,
223  );
224 
225  PBoolean Poll(H323Transactor &,
226  unsigned numRetries = 0,
227  PTimeInterval timeout = 0);
228  void CheckResponse(unsigned, const PASN_Choice *);
229  void OnReceiveRIP(unsigned milliseconds);
230 
231  unsigned sequenceNumber;
234 
235  // Inter-thread transfer variables
236  unsigned rejectReason;
237  void * responseInfo;
238 
239  enum {
247  } responseResult;
248 
249  PTimeInterval whenResponseExpected;
250  PSyncPoint responseHandled;
252  };
253 
254  virtual PBoolean MakeRequest(
255  Request & request
256  );
257  PBoolean CheckForResponse(
258  unsigned,
259  unsigned,
260  const PASN_Choice * = NULL
261  );
262  PBoolean HandleRequestInProgress(
263  const H323TransactionPDU & pdu,
264  unsigned delay
265  );
266  bool CheckCryptoTokens1(const H323TransactionPDU & pdu);
267  bool CheckCryptoTokens2();
268  template <class RAS> bool CheckCryptoTokens(
269  const H323TransactionPDU & pdu,
270  const RAS & ras
271  ) { return CheckCryptoTokens1(pdu) || (pdu.Validate(ras) == H235Authenticator::e_OK || CheckCryptoTokens2()); }
272 
273  void AgeResponses();
274  PBoolean SendCachedResponse(
275  const H323TransactionPDU & pdu
276  );
277 
278  class Response : public PString
279  {
280  PCLASSINFO(Response, PString);
281  public:
282  Response(const H323TransportAddress & addr, unsigned seqNum);
283  ~Response();
284 
285  void SetPDU(const H323TransactionPDU & pdu);
286  PBoolean SendCachedResponse(H323Transport & transport);
287 
289  PTimeInterval retirementAge;
291  };
292 
293  // Configuration variables
299 
302 
303  PDictionary<POrdinalKey, Request> requests;
306 
308  PSortedList<Response> responses;
309 };
310 
311 
313 
314 class H323Transaction : public PObject
315 {
316  PCLASSINFO(H323Transaction, PObject);
317  public:
324  const H323TransactionPDU & requestToCopy,
327  );
330 
331  enum Response {
332  Ignore = -2,
333  Reject = -1,
335  };
336  inline static Response InProgress(unsigned time) { return (Response)(time&0xffff); }
337 
338  virtual H323TransactionPDU * CreateRIP(
339  unsigned sequenceNumber,
340  unsigned delay
341  ) const = 0;
342 
343  PBoolean HandlePDU();
344 
345  virtual PBoolean WritePDU(
346  H323TransactionPDU & pdu
347  );
348 
349  virtual void PrepareConfirm() { }
350 
351  virtual bool CheckCryptoTokens();
352 
353 #if PTRACING
354  virtual const char * GetName() const = 0;
355 #endif
357  virtual unsigned GetSecurityRejectTag() const { return 0; }
358  virtual void SetRejectReason(
359  unsigned reasonCode
360  ) = 0;
361 
363  PBoolean CanSendRIP() const { return canSendRIP; }
366  PBoolean IsBehindNAT() const { return isBehindNAT; }
369  void SetAuthenticators(const H235Authenticators & auth) { authenticators = auth; }
370 
371  protected:
372  virtual Response OnHandlePDU() = 0;
373  PDECLARE_NOTIFIER(PThread, H323Transaction, SlowHandler);
374 
382 
383  H235Authenticators authenticators;
387 };
388 
389 
391 
392 class H323TransactionServer : public PObject
393 {
394  PCLASSINFO(H323TransactionServer, PObject);
395  public:
401  H323EndPoint & endpoint
402  );
403 
408 
409  virtual WORD GetDefaultUdpPort() = 0;
410 
416 
430  PBoolean AddListeners(
431  const H323TransportAddressArray & ifaces
432  );
433 
437  PBoolean AddListener(
438  const H323TransportAddress & interfaceName
439  );
440 
447  PBoolean AddListener(
448  H323Transport * transport
449  );
450 
457  PBoolean AddListener(
458  H323Transactor * listener
459  );
460 
469  virtual H323Transactor * CreateListener(
470  H323Transport * transport
471  ) = 0;
472 
476  PBoolean RemoveListener(
477  H323Transactor * listener
478  );
479 
480  PBoolean SetUpCallSignalAddresses(H225_ArrayOf_TransportAddress & addresses);
482 
483  protected:
485 
486  PThread * monitorThread;
487  PSyncPoint monitorExit;
488 
489  PMutex mutex;
490  PLIST(ListenerList, H323Transactor);
491  ListenerList listeners;
492 };
493 
494 
495 #endif // OPAL_H323
496 
497 #endif // OPAL_H323_H323TRANS_H
498 
499