OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / libopenh323 / include / transports.h
1 /*
2  * transports.h
3  *
4  * H.323 protocol handler
5  *
6  * Open H323 Library
7  *
8  * Copyright (c) 1998-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 H323 Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions of this code were written with the assisance of funding from
25  * Vovida Networks, Inc. http://www.vovida.com.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Log: transports.h,v $
30  * Revision 1.49  2006/01/20 00:32:24  csoutheren
31  * First check-in of signalling aggregation code - incomplete and disabled by default
32  *
33  * Revision 1.48  2005/11/30 13:05:01  csoutheren
34  * Changed tags for Doxygen
35  *
36  * Revision 1.47  2005/11/21 21:07:41  shorne
37  * Added GnuGK Nat support
38  *
39  * Revision 1.46  2005/01/16 20:39:43  csoutheren
40  * Fixed problem with IPv6 INADDR_ANY
41  *
42  * Revision 1.45  2004/08/24 08:11:25  csoutheren
43  * Added initial support for receiving broadcasts on Linux
44  *
45  * Revision 1.44  2004/05/13 02:26:13  dereksmithies
46  * Fixes so make docs does not generate warning messages about brackets.
47  *
48  * Revision 1.43  2003/12/29 13:28:45  dominance
49  * fixed docbook syntax trying to generate LaTeX formula with ip$10.x.x.x.
50  *
51  * Revision 1.42  2003/04/10 09:44:55  robertj
52  * Added associated transport to new GetInterfaceAddresses() function so
53  *   interfaces can be ordered according to active transport links. Improves
54  *   interoperability.
55  * Replaced old listener GetTransportPDU() with GetInterfaceAddresses()
56  *   and H323SetTransportAddresses() functions.
57  *
58  * Revision 1.41  2003/04/10 01:03:25  craigs
59  * Added functions to access to lists of interfaces
60  *
61  * Revision 1.40  2003/03/21 05:24:02  robertj
62  * Added setting of remote port in UDP transport constructor.
63  *
64  * Revision 1.39  2003/02/06 04:29:23  robertj
65  * Added more support for adding things to H323TransportAddressArrays
66  *
67  * Revision 1.38  2002/11/21 06:39:56  robertj
68  * Changed promiscuous mode to be three way. Fixes race condition in gkserver
69  *   which can cause crashes or more PDUs to be sent to the wrong place.
70  *
71  * Revision 1.37  2002/11/10 08:10:43  robertj
72  * Moved constants for "well known" ports to better place (OPAL change).
73  *
74  * Revision 1.36  2002/09/16 01:14:15  robertj
75  * Added #define so can select if #pragma interface/implementation is used on
76  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
77  *
78  * Revision 1.35  2002/07/22 09:40:15  robertj
79  * Added ability to automatically convert string arrays, lists sorted lists
80  *   directly to H323TransportAddressArray.
81  *
82  * Revision 1.34  2002/07/02 10:02:29  robertj
83  * Added H323TransportAddress::GetIpAddress() so don't have to provide port
84  *   when you don't need it as in GetIpAndPort(),.
85  *
86  * Revision 1.33  2002/06/28 03:34:25  robertj
87  * Fixed issues with address translation on gatekeeper RAS channel.
88  *
89  * Revision 1.32  2002/06/12 03:51:59  robertj
90  * Added function to compare two transport addresses in a more intelligent
91  *   way that strict string comparison. Takes into account wildcarding.
92  *
93  * Revision 1.31  2002/02/11 04:20:25  robertj
94  * Fixed documentation errors, thanks Horacio J. Peña
95  *
96  * Revision 1.30  2002/02/05 23:29:23  robertj
97  * Changed default for H.323 listener to reuse addresses.
98  *
99  * Revision 1.29  2001/12/22 01:48:12  robertj
100  * Added ability to use local and remote port from transport channel as well
101  *   as explicit port in H.245 address PDU setting routine.
102  * Added PrintOn() to listener and transport for tracing purposes.
103  *
104  * Revision 1.28  2001/07/17 04:44:29  robertj
105  * Partial implementation of T.120 and T.38 logical channels.
106  *
107  * Revision 1.27  2001/06/25 02:30:46  robertj
108  * Allowed TCP listener socket to be opened in non-exclusive mode
109  *   (ie SO_REUSEADDR) to avoid daemon restart problems.
110  *
111  * Revision 1.26  2001/06/22 00:14:14  robertj
112  * Added ConnectTo() function to conencto specific address.
113  * Added promiscuous mode for UDP channel.
114  *
115  * Revision 1.25  2001/05/17 06:37:02  robertj
116  * Added multicast gatekeeper discovery support.
117  *
118  * Revision 1.24  2001/04/09 08:43:39  robertj
119  * Added ability to get transport address for a listener.
120  *
121  * Revision 1.23  2001/03/02 06:59:57  robertj
122  * Enhanced the globally unique identifier class.
123  *
124  * Revision 1.22  2001/02/09 05:16:24  robertj
125  * Added #pragma interface for GNU C++.
126  *
127  * Revision 1.21  2001/01/23 05:08:04  robertj
128  * Fixed bug when trying to clear call while waiting on H.245 channel connect, thanks Yura Aksyonov.
129  *
130  * Revision 1.20  2000/10/20 06:18:58  robertj
131  * Fixed very small race condition on creating new connectionon incoming call.
132  * Fixed memory/socket leak if do TCP connect and don't send valid setup PDU.
133  *
134  * Revision 1.19  2000/10/04 05:59:09  robertj
135  * Minor reorganisation of the H.245 secondary channel start up to make it simpler
136  *    to override its behaviour.
137  *
138  * Revision 1.18  2000/09/25 12:59:16  robertj
139  * Added StartListener() function that takes a H323TransportAddress to start
140  *     listeners bound to specific interfaces.
141  *
142  * Revision 1.17  2000/09/22 01:35:03  robertj
143  * Added support for handling LID's that only do symmetric codecs.
144  *
145  * Revision 1.16  2000/06/07 05:47:55  robertj
146  * Added call forwarding.
147  *
148  * Revision 1.15  2000/05/22 05:21:36  robertj
149  * Fixed race condition where controlChannel variable could be used before set.
150  *
151  * Revision 1.14  2000/05/18 11:53:34  robertj
152  * Changes to support doc++ documentation generation.
153  *
154  * Revision 1.13  2000/05/08 14:07:26  robertj
155  * Improved the provision and detection of calling and caller numbers, aliases and hostnames.
156  *
157  * Revision 1.12  2000/05/02 04:32:25  robertj
158  * Fixed copyright notice comment.
159  *
160  * Revision 1.11  1999/11/06 05:37:44  robertj
161  * Complete rewrite of termination of connection to avoid numerous race conditions.
162  *
163  * Revision 1.10  1999/10/16 03:47:57  robertj
164  * Fixed termination of gatekeeper RAS thread problem
165  *
166  * Revision 1.9  1999/10/10 08:59:47  robertj
167  * Fixed race condition in connection shutdown
168  *
169  * Revision 1.8  1999/09/14 06:52:54  robertj
170  * Added better support for multi-homed client hosts.
171  *
172  * Revision 1.7  1999/09/10 09:43:59  robertj
173  * Removed attempt at determining local interface for gatekeeper, so still has problem on multi-homed hosts.
174  *
175  * Revision 1.6  1999/08/31 12:34:18  robertj
176  * Added gatekeeper support.
177  *
178  * Revision 1.5  1999/08/25 05:14:21  robertj
179  * Fixed problem with calling modal dialog from a background thread.
180  *
181  * Revision 1.4  1999/07/14 06:06:14  robertj
182  * Fixed termination problems (race conditions) with deleting connection object.
183  *
184  * Revision 1.3  1999/07/09 06:09:49  robertj
185  * Major implementation. An ENORMOUS amount of stuff added everywhere.
186  *
187  * Revision 1.2  1999/06/13 12:41:14  robertj
188  * Implement logical channel transmitter.
189  * Fixed H245 connect on receiving call.
190  *
191  * Revision 1.1  1999/06/09 05:26:20  robertj
192  * Major restructuring of classes.
193  *
194  */
195
196 #ifndef __TRANSPORTS_H
197 #define __TRANSPORTS_H
198
199 #ifdef P_USE_PRAGMA
200 #pragma interface
201 #endif
202
203
204 #include <ptlib/sockets.h>
205
206
207 class H225_Setup_UUIE;
208 class H225_TransportAddress;
209 class H225_ArrayOf_TransportAddress;
210 class H225_TransportAddress_ipAddress;
211
212 class H245_TransportAddress;
213
214 class H323SignalPDU;
215 class H323RasPDU;
216 class H323EndPoint;
217 class H323Connection;
218 class H323Listener;
219 class H323Transport;
220 class H323Gatekeeper;
221
222
223
224 ///////////////////////////////////////////////////////////////////////////////
225
226 /**String representation of a transport address.
227  */
228
229 class H323TransportAddress : public PString
230 {
231   PCLASSINFO(H323TransportAddress, PString);
232   public:
233     H323TransportAddress() { }
234     H323TransportAddress(const char *);
235     H323TransportAddress(const PString &);
236     H323TransportAddress(const H225_TransportAddress &);
237     H323TransportAddress(const H245_TransportAddress &);
238     H323TransportAddress(const PIPSocket::Address &, WORD);
239
240     BOOL SetPDU(H225_TransportAddress & pdu) const;
241     BOOL SetPDU(H245_TransportAddress & pdu) const;
242
243     /**Determine if the two transport addresses are equivalent.
244       */
245     BOOL IsEquivalent(
246       const H323TransportAddress & address
247     );
248
249     /**Extract the ip address from transport address.
250        Returns FALSE, if the address is not an IP transport address.
251       */
252     BOOL GetIpAddress(
253       PIPSocket::Address & ip
254     ) const;
255
256     /**Extract the ip address and port number from transport address.
257        Returns FALSE, if the address is not an IP transport address.
258       */
259     BOOL GetIpAndPort(
260       PIPSocket::Address & ip,
261       WORD & port,
262       const char * proto = "tcp"
263     ) const;
264
265     /**Translate the transport address to a more human readable form.
266        Returns the hostname if using IP.
267       */
268     PString GetHostName() const;
269
270     /**Create a listener based on this transport address.
271
272        For example an address of "#ip$10.0.0.1:1720#" would create a TCP
273        listening socket that would be bound to the specific interface
274        10.0.0.1 and listens on port 1720. Note that the address
275        "#ip$*:1720#" can be used to bind to INADDR_ANY.
276
277        Also note that if the address has a trailing '+' character then the
278        socket will be bound using the REUSEADDR option.
279       */
280     H323Listener * CreateListener(
281       H323EndPoint & endpoint   ///<  Endpoint object for transport creation.
282     ) const;
283
284     /**Create a listener compatible for this address type.
285        This is similar to CreateListener() but does not use the TSAP specified
286        in the H323Transport. For example an address of "#ip$10.0.0.1:1720#"
287        would create a TCP listening socket that would be bound to the specific
288        interface 10.0.0.1 but listens on a random OS allocated port number.
289       */
290     H323Listener * CreateCompatibleListener(
291       H323EndPoint & endpoint   ///<  Endpoint object for transport creation.
292     ) const;
293
294     /**Create a transport suitable for this address type.
295       */
296     H323Transport * CreateTransport(
297       H323EndPoint & endpoint   ///<  Endpoint object for transport creation.
298     ) const;
299
300   protected:
301     void Validate();
302 };
303
304
305 PDECLARE_ARRAY(H323TransportAddressArray, H323TransportAddress)
306 #ifdef DOC_PLUS_PLUS
307 {
308 #endif
309   public:
310     H323TransportAddressArray(
311       const H323TransportAddress & address
312     ) { AppendAddress(address); }
313     H323TransportAddressArray(
314       const H225_ArrayOf_TransportAddress & addresses
315     );
316     H323TransportAddressArray(
317       const PStringArray & array
318     ) { AppendStringCollection(array); }
319     H323TransportAddressArray(
320       const PStringList & list
321     ) { AppendStringCollection(list); }
322     H323TransportAddressArray(
323       const PSortedStringList & list
324     ) { AppendStringCollection(list); }
325
326     void AppendString(
327       const char * address
328     );
329     void AppendString(
330       const PString & address
331     );
332     void AppendAddress(
333       const H323TransportAddress & address
334     );
335
336   protected:
337     void AppendStringCollection(
338       const PCollection & coll
339     );
340 };
341
342
343 /**This class describes a "listener" on a transport protocol.
344    A "listener" is an object that listens for incoming connections on the
345    particular transport. It is executed as a separate thread.
346
347    The Main() function is used to handle incoming H.323 connections and
348    dispatch them in new threads based on the actual H323Transport class. This
349    is defined in the descendent class that knows what the low level transport
350    is, eg H323ListenerIP for the TCP/IP protocol.
351
352    An application may create a descendent off this class and override
353    functions as required for operating the channel protocol.
354  */
355 class H323Listener : public PThread
356 {
357   PCLASSINFO(H323Listener, PThread);
358
359   public:
360   /**@name Construction */
361   //@{
362     /**Create a new listener.
363      */
364     H323Listener(
365       H323EndPoint & endpoint      ///<  Endpoint instance for channel
366     );
367   //@}
368
369   /**@name Overrides from PObject */
370   //@{
371     virtual void PrintOn(
372       ostream & strm
373     ) const;
374   //@}
375
376   /**@name Operations */
377   //@{
378     /** Open the listener.
379       */
380     virtual BOOL Open() = 0;
381
382     /**Stop the listener thread and no longer accept incoming connections.
383      */
384     virtual BOOL Close() = 0;
385
386     /**Accept a new incoming transport.
387       */
388     virtual H323Transport * Accept(
389       const PTimeInterval & timeout  ///<  Time to wait for incoming connection
390     ) = 0;
391
392     /**Get the local transport address on which this listener may be accessed.
393       */
394     virtual H323TransportAddress GetTransportAddress() const = 0;
395
396     /**Set up a transport address PDU for bidirectional logical channels.
397       */
398     virtual BOOL SetUpTransportPDU(
399       H245_TransportAddress & pdu,         ///<  Transport addresses listening on
400       const H323Transport & associatedTransport ///<  Associated transport for precendence and translation
401     ) = 0;
402   //@}
403
404   protected:
405     H323EndPoint & endpoint;  /// Endpoint that owns the listener.
406 };
407
408
409 PLIST(H323ListenerList, H323Listener);
410
411
412 /** Return a list of transport addresses corresponding to a listener list
413   */
414 H323TransportAddressArray H323GetInterfaceAddresses(
415   const H323ListenerList & listeners, ///<  List of listeners
416   BOOL excludeLocalHost = TRUE,       ///<  Flag to exclude 127.0.0.1
417   H323Transport * associatedTransport = NULL
418                           ///<  Associated transport for precedence and translation
419 );
420
421 H323TransportAddressArray H323GetInterfaceAddresses(
422   const H323TransportAddress & addr,  ///<  Possible INADDR_ANY address
423   BOOL excludeLocalHost = TRUE,       ///<  Flag to exclude 127.0.0.1
424   H323Transport * associatedTransport = NULL
425                           ///<  Associated transport for precedence and translation
426 );
427
428 /**Set the PDU field for the list of transport addresses
429   */
430 void H323SetTransportAddresses(
431   const H323Transport & associatedTransport,   ///<  Transport for NAT address translation
432   const H323TransportAddressArray & addresses, ///<  Addresses to set
433   H225_ArrayOf_TransportAddress & pdu          ///<  List of PDU transport addresses
434 );
435
436
437 /**This class describes a I/O transport protocol..
438    A "transport" is an object that listens for incoming connections on the
439    particular transport.
440  */
441 class H323Transport : public PIndirectChannel
442 {
443   PCLASSINFO(H323Transport, PIndirectChannel);
444
445   public:
446   /**@name Construction */
447   //@{
448     /**Create a new transport channel.
449      */
450     H323Transport(H323EndPoint & endpoint);
451     ~H323Transport();
452   //@}
453
454   /**@name Overrides from PObject */
455   //@{
456     virtual void PrintOn(
457       ostream & strm
458     ) const;
459   //@}
460
461   /**@name Operations */
462   //@{
463     /**Get the transport address of the local endpoint.
464       */
465     virtual H323TransportAddress GetLocalAddress() const = 0;
466
467     /**Get the transport address of the remote endpoint.
468       */
469     virtual H323TransportAddress GetRemoteAddress() const = 0;
470
471     /**Set remote address to connect to.
472        Note that this does not necessarily initiate a transport level
473        connection, but only indicates where to connect to. The actual
474        connection is made by the Connect() function.
475       */
476     virtual BOOL SetRemoteAddress(
477       const H323TransportAddress & address
478     ) = 0;
479
480     /**Connect to the remote address.
481       */
482     virtual BOOL Connect() = 0;
483
484     /**Connect to the specified address.
485       */
486     BOOL ConnectTo(
487       const H323TransportAddress & address
488     ) { return SetRemoteAddress(address) && Connect(); }
489
490     /**Close the channel.
491       */
492     virtual BOOL Close();
493
494     /**Check that the transport address PDU is compatible with transport.
495       */
496     virtual BOOL IsCompatibleTransport(
497       const H225_TransportAddress & pdu
498     ) const;
499
500     /**Set up a transport address PDU for RAS channel.
501       */
502     virtual void SetUpTransportPDU(
503       H225_TransportAddress & pdu,
504       BOOL localTsap
505     ) const;
506
507     enum {
508       UseLocalTSAP = 0x10001,
509       UseRemoteTSAP
510     };
511
512     /**Set up a transport address PDU for logical channel.
513        If tsap is UseLocalTSAP or UseRemoteTSAP then the local or remote port
514        of the transport is used, otherwise the explicit port number is used.
515       */
516     virtual void SetUpTransportPDU(
517       H245_TransportAddress & pdu,
518       unsigned tsap
519     ) const;
520
521     /// Promiscious modes for transport
522     enum PromisciousModes {
523       AcceptFromRemoteOnly,
524       AcceptFromAnyAutoSet,
525       AcceptFromAny,
526       NumPromisciousModes
527     };
528
529     /**Set read to promiscuous mode.
530        Normally only reads from the specifed remote address are accepted. This
531        flag allows packets to be accepted from any remote, provided the
532        underlying protocol can do so. For example TCP will do nothing.
533
534        The Read() call may optionally set the remote address automatically to
535        whatever the sender host of the last received message was.
536
537        Default behaviour does nothing.
538       */
539     virtual void SetPromiscuous(
540       PromisciousModes promiscuous
541     );
542
543     /**Get the transport address of the last received PDU.
544
545        Default behaviour returns GetRemoteAddress().
546       */
547     virtual H323TransportAddress GetLastReceivedAddress() const;
548
549     /**Read a protocol data unit from the transport.
550        This will read using the transports mechanism for PDU boundaries, for
551        example UDP is a single Read() call, while for TCP there is a TPKT
552        header that indicates the size of the PDU.
553       */
554     virtual BOOL ReadPDU(
555       PBYTEArray & pdu   ///<  PDU read from transport
556     ) = 0;
557
558     /**Extract a protocol data unit from the transport
559        This is used by the aggregator to deblock the incoming data stream
560        into valid PDUs.
561       */
562     virtual BOOL ExtractPDU(
563       const PBYTEArray & pdu, 
564       PINDEX & len
565     ) = 0;
566
567     /**Write a protocol data unit from the transport.
568        This will write using the transports mechanism for PDU boundaries, for
569        example UDP is a single Write() call, while for TCP there is a TPKT
570        header that indicates the size of the PDU.
571       */
572     virtual BOOL WritePDU(
573       const PBYTEArray & pdu  ///<  PDU to write
574     ) = 0;
575   //@}
576
577   /**@name Signalling Channel */
578   //@{
579     /**Wait for first PDU and find/create connection object.
580        If returns FALSE, then the transport is deleted by the calling thread.
581       */
582     BOOL HandleFirstSignallingChannelPDU();
583   //@}
584
585   /**@name Control Channel */
586   //@{
587     /**Begin the opening of a control channel.
588        This sets up the channel so that the remote endpoint can connect back
589        to this endpoint. This would be called on the signalling channel
590        instance of a H323Transport.
591       */
592     virtual H323Transport * CreateControlChannel(
593       H323Connection & connection
594     );
595
596     /**Finish the opening of a control channel.
597        This waits for the connect backfrom the remote endpoint, completing the
598        control channel open sequence.
599       */
600     virtual BOOL AcceptControlChannel(
601       H323Connection & connection
602     );
603
604     /**Connect the control channel.
605       */
606     virtual void StartControlChannel(
607       H323Connection & connection
608     );
609   //@}
610
611   /**@name RAS Channel */
612   //@{
613     /**Discover a Gatekeeper on the network.
614        This locates a gatekeeper on the network and associates this transport
615        object with packet exchange with that gatekeeper.
616       */
617     virtual BOOL DiscoverGatekeeper(
618       H323Gatekeeper & gk,                  ///<  Gatekeeper to set on discovery.
619       H323RasPDU & pdu,                     ///<  GatekeeperRequest PDU
620       const H323TransportAddress & address  ///<  Address of gatekeeper (if present)
621     );
622   //@}
623
624
625   /**@name Member variable access */
626   //@{
627     /**Get the associated endpoint to this transport.
628       */
629     H323EndPoint & GetEndPoint() const { return endpoint; }
630
631     /**Attach a thread to the transport.
632       */
633     void AttachThread(
634       PThread * thread
635     );
636
637     /**Wait for associated thread to terminate.
638       */
639     void CleanUpOnTermination();
640   //@}
641
642   protected:
643     H323EndPoint & endpoint;    /// Endpoint that owns the listener.
644     PThread      * thread;      /// Thread handling the transport
645     BOOL canGetInterface;
646 };
647
648
649
650 ///////////////////////////////////////////////////////////////////////////////
651 // Transport classes for IP
652
653 /**This class represents a particular H323 transport using IP.
654    It is used by the TCP and UDP transports.
655  */
656 class H323TransportIP : public H323Transport
657 {
658   PCLASSINFO(H323TransportIP, H323Transport);
659
660   public:
661     /**Create a new transport channel.
662      */
663     H323TransportIP(
664       H323EndPoint & endpoint,    ///<  H323 End Point object
665       PIPSocket::Address binding, ///<  Local interface to use
666       WORD remPort                ///<  Remote port to use
667     );
668
669     /**Get the transport dependent name of the local endpoint.
670       */
671     virtual H323TransportAddress GetLocalAddress() const;
672
673     /**Get the transport dependent name of the remote endpoint.
674       */
675     virtual H323TransportAddress GetRemoteAddress() const;
676
677     /**Check that the transport address PDU is compatible with transport.
678       */
679     virtual BOOL IsCompatibleTransport(
680       const H225_TransportAddress & pdu
681     ) const;
682
683     /**Set up a transport address PDU for RAS channel.
684       */
685     virtual void SetUpTransportPDU(
686       H225_TransportAddress & pdu,
687       BOOL localTsap
688     ) const;
689
690     /**Set up a transport address PDU for logical channel.
691       */
692     virtual void SetUpTransportPDU(
693       H245_TransportAddress & pdu,
694       unsigned tsap
695     ) const;
696
697
698   protected:
699     PIPSocket::Address localAddress;  // Address of the local interface
700     WORD               localPort;
701     PIPSocket::Address remoteAddress; // Address of the remote host
702     WORD               remotePort;
703 };
704
705
706 ///////////////////////////////////////////////////////////////////////////////
707 // Transport classes for TCP/IP
708
709 /**This class manages H323 connections using TCP/IP transport.
710  */
711 class H323ListenerTCP : public H323Listener
712 {
713   PCLASSINFO(H323ListenerTCP, H323Listener);
714
715   public:
716     /**Create a new listener for the TCP/IP protocol.
717      */
718     H323ListenerTCP(
719       H323EndPoint & endpoint,    ///<  Endpoint instance for channel
720       PIPSocket::Address binding, ///<  Local interface to listen on
721       WORD port,                  ///<  TCP port to listen for connections
722       BOOL exclusive = FALSE      ///<  Fail if listener port in use
723     );
724
725     /** Destroy the listener thread.
726       */
727     ~H323ListenerTCP();
728     
729   // Overrides from H323Listener
730     /** Open the listener.
731       */
732     virtual BOOL Open();
733
734     /**Stop the listener thread and no longer accept incoming connections.
735      */
736     virtual BOOL Close();
737
738     /**Accept a new incoming transport.
739       */
740     virtual H323Transport * Accept(
741       const PTimeInterval & timeout  ///<  Time to wait for incoming connection
742     );
743
744     /**Get the local transport address on which this listener may be accessed.
745       */
746     virtual H323TransportAddress GetTransportAddress() const;
747
748     /**Set up a transport address PDU for bidirectional logical channels.
749       */
750     virtual BOOL SetUpTransportPDU(
751       H245_TransportAddress & pdu,        ///<  Transport addresses listening on
752       const H323Transport & associatedTransport ///<  Associated transport for precendence and translation
753     );
754
755     WORD GetListenerPort() const { return listener.GetPort(); }
756
757
758   protected:
759     /**Handle incoming H.323 connections and dispatch them in new threads
760        based on the H323Transport class. This is defined in the descendent
761        class that knows what the low level transport is, eg H323ListenerIP
762        for the TCP/IP protocol.
763
764        Note this function does not return until the Close() function is called
765        or there is some other error.
766      */
767     virtual void Main();
768
769
770     PTCPSocket listener;
771     PIPSocket::Address localAddress;
772     BOOL exclusiveListener;
773 };
774
775
776 /**This class represents a particular H323 transport using TCP/IP.
777  */
778 class H323TransportTCP : public H323TransportIP
779 {
780   PCLASSINFO(H323TransportTCP, H323TransportIP);
781
782   public:
783     /**Create a new transport channel.
784      */
785     H323TransportTCP(
786       H323EndPoint & endpoint,    ///<  H323 End Point object
787       PIPSocket::Address binding = PIPSocket::GetDefaultIpAny(), ///<  Local interface to use
788       BOOL listen = FALSE         ///<  Flag for need to wait for remote to connect
789     );
790
791     /**Destroy transport channel.
792      */
793     ~H323TransportTCP();
794
795     /**Set default remote address to connect to.
796        Note that this does not necessarily initiate a transport level
797        connection, but only indicates where to connect to. The actual
798        connection is made by the Connect() function.
799       */
800     virtual BOOL SetRemoteAddress(
801       const H323TransportAddress & address
802     );
803
804     /**Connect to the remote party.
805       */
806     virtual BOOL Connect();
807
808     /**Close the channel.
809       */
810     virtual BOOL Close();
811
812     /**Read a protocol data unit from the transport.
813        This will read using the transports mechanism for PDU boundaries, for
814        example UDP is a single Read() call, while for TCP there is a TPKT
815        header that indicates the size of the PDU.
816       */
817     BOOL ReadPDU(
818       PBYTEArray & pdu   ///<  PDU read from transport
819     );
820
821     /**Extract a protocol data unit from the transport
822       */
823     BOOL ExtractPDU(
824       const PBYTEArray & pdu, 
825       PINDEX & len
826     );
827  
828     /**Write a protocol data unit from the transport.
829        This will write using the transports mechanism for PDU boundaries, for
830        example UDP is a single Write() call, while for TCP there is a TPKT
831        header that indicates the size of the PDU.
832       */
833     BOOL WritePDU(
834       const PBYTEArray & pdu  ///<  PDU to write
835     );
836
837     /**Begin the opening of a control channel.
838        This sets up the channel so that the remote endpoint can connect back
839        to this endpoint.
840       */
841     virtual H323Transport * CreateControlChannel(
842       H323Connection & connection
843     );
844
845     /**Finish the opening of a control channel.
846        This waits for the connect backfrom the remote endpoint, completing the
847        control channel open sequence.
848       */
849     virtual BOOL AcceptControlChannel(
850       H323Connection & connection
851     );
852
853     /**Indicate we are waiting from remote to connect back to us.
854       */
855     virtual BOOL IsListening() const;
856
857
858   protected:
859     /**This callback is executed when the Open() function is called with
860        open channels. It may be used by descendent channels to do any
861        handshaking required by the protocol that channel embodies.
862
863        The default behaviour is to simply return TRUE.
864
865        @return
866        Returns TRUE if the protocol handshaking is successful.
867      */
868     virtual BOOL OnOpen();
869
870
871     PTCPSocket * h245listener;
872 };
873
874
875 ///////////////////////////////////////////////////////////////////////////////
876 // Transport classes for UDP/IP
877
878 /**This class represents a particular H323 transport using UDP/IP.
879  */
880 class H323TransportUDP : public H323TransportIP
881 {
882   PCLASSINFO(H323TransportUDP, H323TransportIP);
883
884   public:
885     /**Create a new transport channel.
886      */
887     H323TransportUDP(
888       H323EndPoint & endpoint,                  ///<  H323 End Point object
889       PIPSocket::Address binding = PIPSocket::GetDefaultIpAny(),  ///<  Local interface to listen on
890       WORD localPort = 0,                       ///<  Local port to listen on
891       WORD remotePort = 0                       ///<  Remote port to connect on
892     );
893     ~H323TransportUDP();
894
895     /**Set default remote address to connect to.
896        Note that this does not necessarily initiate a transport level
897        connection, but only indicates where to connect to. The actual
898        connection is made by the Connect() function.
899       */
900     virtual BOOL SetRemoteAddress(
901       const H323TransportAddress & address
902     );
903
904     /**Connect to the remote party.
905       */
906     virtual BOOL Connect();
907
908     /**Set read to promiscuous mode.
909        Normally only reads from the specifed remote address are accepted. This
910        flag allows packets to be accepted from any remote, provided the
911        underlying protocol can do so.
912
913        The Read() call may optionally set the remote address automatically to
914        whatever the sender host of the last received message was.
915
916        Default behaviour sets the internal flag, so that Read() operates as
917        described.
918       */
919     virtual void SetPromiscuous(
920       PromisciousModes promiscuous
921     );
922
923     /**Get the transport address of the last received PDU.
924
925        Default behaviour returns the lastReceivedAddress member variable.
926       */
927     virtual H323TransportAddress GetLastReceivedAddress() const;
928
929     /**Read a protocol data unit from the transport.
930        This will read using the transports mechanism for PDU boundaries, for
931        example UDP is a single Read() call, while for TCP there is a TPKT
932        header that indicates the size of the PDU.
933       */
934     virtual BOOL ReadPDU(
935       PBYTEArray & pdu   ///<  PDU read from transport
936     );
937
938     /**Extract a protocol data unit from the transport
939       */
940     BOOL ExtractPDU(
941       const PBYTEArray & pdu, 
942       PINDEX & len
943     );
944  
945     /**Write a protocol data unit from the transport.
946        This will write using the transports mechanism for PDU boundaries, for
947        example UDP is a single Write() call, while for TCP there is a TPKT
948        header that indicates the size of the PDU.
949       */
950     virtual BOOL WritePDU(
951       const PBYTEArray & pdu  ///<  PDU to write
952     );
953
954     /**Discover a Gatekeeper on the local network.
955        This locates a gatekeeper on the network and associates this transport
956        object with packet exchange with that gatekeeper. This broadcasts a UDP
957        packet on the local network to find the gatekeeper's IP address.
958       */
959     virtual BOOL DiscoverGatekeeper(
960       H323Gatekeeper & gk,                  ///<  Gatekeeper to set on discovery.
961       H323RasPDU & pdu,                     ///<  GatekeeperRequest PDU
962       const H323TransportAddress & address  ///<  Address of gatekeeper (if present)
963     );
964
965     /**Get the transport address of the local endpoint.
966       */
967     virtual H323TransportAddress GetLocalAddress() const;
968
969   protected:
970     PromisciousModes     promiscuousReads;
971     H323TransportAddress lastReceivedAddress;
972     PIPSocket::Address   lastReceivedInterface;
973     WORD interfacePort;
974 };
975
976
977 #endif // __TRANSPORTS_H
978
979
980 /////////////////////////////////////////////////////////////////////////////