1 /*
2  * h460_std17.h
3  *
4  * H460.17 NAT Traversal class.
5  *
6  * h323plus library
7  *
8  * Copyright (c) 2008-11 ISVO (Asia) Pte. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.1 (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  * Alternatively, the contents of this file may be used under the terms
16  * of the General Public License (the  "GNU License"), in which case the
17  * provisions of GNU License are applicable instead of those
18  * above. If you wish to allow use of your version of this file only
19  * under the terms of the GNU License and not to allow others to use
20  * your version of this file under the MPL, indicate your decision by
21  * deleting the provisions above and replace them with the notice and
22  * other provisions required by the GNU License. If you do not delete
23  * the provisions above, a recipient may use your version of this file
24  * under either the MPL or the GNU License."
25  *
26  * Software distributed under the License is distributed on an "AS IS"
27  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
28  * the License for the specific language governing rights and limitations
29  * under the License.
30  *
31  *
32  * The Initial Developer of the Original Code is ISVO (Asia) Pte. Ltd.
33  *
34  * Contributor(s): ______________________________________.
35  *
36  * $Id$
37  *
38  *
39  */
40 
41 #ifndef H_H460_FeatureStd17
42 #define H_H460_FeatureStd17
43 
44 #pragma once
45 
46 #include <ptlib/plugin.h>
47 #include <vector>
48 #include <queue>
49 
50 class H323EndPoint;
51 class H323Connection;
52 class H46017Handler;
53 class H460_FeatureStd17 : public H460_FeatureStd
54 {
55     PCLASSINFO(H460_FeatureStd17,H460_FeatureStd);
56 
57 public:
58 
59     H460_FeatureStd17();
60     virtual ~H460_FeatureStd17();
61 
62     // Universal Declarations Every H460 Feature should have the following
63     virtual void AttachEndPoint(H323EndPoint * _ep);
64     virtual void AttachConnection(H323Connection * _con);
65 
GetFeatureName()66     static PStringArray GetFeatureName() { return PStringArray("Std17"); };
GetFeatureFriendlyName()67     static PStringArray GetFeatureFriendlyName() { return PStringArray("NatTraversal-H.460.17"); };
68     static int GetPurpose();
GetFeaturePurpose()69     virtual int GetFeaturePurpose()  { return H460_FeatureStd17::GetPurpose(); }
GetIdentifier()70     static PStringArray GetIdentifier() { return PStringArray("17"); };
71 
CommonFeature()72     virtual PBoolean CommonFeature() { return isEnabled; }
73 
IsEnabled()74     static PBoolean IsEnabled()  { return isEnabled; }
75 
76     //////////////////////
77     // Public Function  call..
78     virtual PBoolean Initialise(const PString & remoteAddr = PString(), PBoolean srv = true);
79     virtual PBoolean Initialise(H323TransportSecurity * sec, const PString & remoteAddr = PString(), PBoolean srv = true);
80 
81     virtual void UnInitialise();
82 
GetHandler()83     H46017Handler * GetHandler() { return m_handler; }
84 
85 protected:
86     PBoolean InitialiseTunnel(const H323TransportAddress & remoteAddr, const H323TransportSecurity & sec);
87 
88 
89 private:
90     H323EndPoint * EP;
91     H323Connection * CON;
92 
93     H46017Handler * m_handler;
94     static PBoolean isEnabled;
95 
96 };
97 
98 // Need to declare for Factory Loader
99 #ifndef _WIN32_WCE
100     #if PTLIB_VER > 260
101        PPLUGIN_STATIC_LOAD(Std17, H460_Feature);
102     #else
103        PWLIB_STATIC_LOAD_PLUGIN(Std17, H460_Feature);
104     #endif
105 #endif
106 
107 ////////////////////////////////////////////////////////////////////////
108  #ifdef H323_H46026
109 class H46026Tunnel;
110 #endif
111 class H46017Handler;
112 class H46017Transport  : public H323TransportTCP
113 {
114   PCLASSINFO(H46017Transport, H323TransportTCP);
115 
116   public:
117 
118     enum PDUType {
119         e_raw
120     };
121 
122     /**Create a new transport channel.
123      */
124     H46017Transport(
125       H323EndPoint & endpoint,        /// H323 End Point object
126       PIPSocket::Address binding,     /// Bind Interface
127       H46017Handler * feat              /// Feature
128     );
129 
130     ~H46017Transport();
131 
132     /**Handle the H46017 Socket
133       */
134     PBoolean HandleH46017Socket();
135 
136 
137     /**Handle incoming H46017 PDU
138       */
139     PBoolean HandleH46017PDU(const Q931 & q931);
140     PBoolean HandleH46017PDU(H323SignalPDU & pdu);
141 
142     /**Write a protocol data unit from the transport.
143        This will write using the transports mechanism for PDU boundaries, for
144        example UDP is a single Write() call, while for TCP there is a TPKT
145        header that indicates the size of the PDU.
146       */
147     PBoolean WritePDU(
148       const PBYTEArray & pdu  ///<  PDU to write
149     );
150 
151     /**Write a protocol data unit from the transport.
152        This will write using the transports mechanism for PDU boundaries, for
153        example UDP is a single Write() call, while for TCP there is a TPKT
154        header that indicates the size of the PDU.
155       */
156     virtual PBoolean WriteSignalPDU(
157       const H323SignalPDU & pdu  /// PDU to write
158     );
159 
160     PBoolean WriteRasPDU(
161       const PBYTEArray & pdu
162     );
163 
164     /**Read a protocol data unit from the transport.
165        This will read using the transports mechanism for PDU boundaries, for
166        example UDP is a single Read() call, while for TCP there is a TPKT
167        header that indicates the size of the PDU.
168       */
169     virtual PBoolean ReadPDU(
170          PBYTEArray & pdu  /// PDU to Read
171     );
172 
173     void ConnectionLost(PBoolean established);
174 
175     PBoolean IsConnectionLost() const;
176 
177 #ifdef H323_H46026
178     void SetTunnel(H46026Tunnel * mgr);
179 #endif
180 
181 // Overrides
182     /**Connect to the remote party.
183       */
184     virtual PBoolean Connect();
185 
186     /**Close the channel.(Don't do anything)
187       */
188     virtual PBoolean Close();
189 
190     virtual PBoolean IsListening() const;
191 
192     virtual PBoolean IsOpen () const;
193 
CloseTransport()194     PBoolean CloseTransport() { return closeTransport; };
195 
196     virtual void CleanUpOnTermination();
197 
198   protected:
199 
200     /**Handle the H46017 SetupPDU
201       */
202     H323Connection * HandleH46017SetupPDU(H323SignalPDU & pdu);
203 
204     /**Handle the H46017 Tunnelled RAS
205       */
206     PBoolean HandleH46017SignalPDU(H323SignalPDU & pdu);
207 
208     /**Handle the H46017 Tunnelled RAS
209       */
210     PBoolean HandleH46017RAS(const H323SignalPDU & pdu);
211 
212     /**Handle the H46017 Signalling on Signal Process Thread.
213       */
214     PBoolean HandleH46017SignallingPDU(unsigned crv, H323SignalPDU & pdu);
215 
216     PBoolean WriteTunnel(H323SignalPDU & msg);
217 
218      PMutex connectionsMutex;
219      PMutex WriteMutex;
220      PMutex shutdownMutex;
221 
222      PTimeInterval ReadTimeOut;
223 
224      H46017Handler * Feature;
225 
226      PBoolean   remoteShutDown;
227      PBoolean    closeTransport;
228 
229      PSyncPoint  msgRecd;
230      PMutex signalMutex;
231      std::queue<H323SignalPDU>  recdpdu;
232      PThread * m_signalProcess;
233      PDECLARE_NOTIFIER(PThread, H46017Transport, SignalProcess);
234 
235  #ifdef H323_H46026
236      PBoolean   m_h46026tunnel;
237      H46026Tunnel * m_socketMgr;
238 
239      PThread * m_socketWrite;
240      PDECLARE_NOTIFIER(PThread, H46017Transport, SocketWrite);
241 #endif
242 
243 };
244 
245 //////////////////////////////////////////////////////////////////////
246 
247 class H46017RasTransport;
248 class H46017Handler : public PObject
249 {
250 
251     PCLASSINFO(H46017Handler, PObject);
252 
253 public:
254     H46017Handler(H323EndPoint & _ep,
255         const H323TransportAddress & _remoteAddress
256         );
257 
258     ~H46017Handler();
259 
260     H323EndPoint * GetEndPoint();
261 
262     PBoolean CreateNewTransport(const H323TransportSecurity & security);
263 
264     PBoolean ReRegister(const PString & newid);
265 
IsOpen()266     PBoolean IsOpen() { return openTransport; }
267 
IsConnectionLost()268     PBoolean IsConnectionLost() const { return connectionlost; }
SetConnectionLost(PBoolean newVal)269     void SetConnectionLost(PBoolean newVal) { connectionlost = newVal; }
270 
271     H323TransportAddress GetTunnelBindAddress() const;
272 
273     H46017Transport * GetTransport();
274     void AttachRasTransport(H46017RasTransport * _ras);
275     H46017RasTransport * GetRasTransport();
276 
277     void TransportClosed();
278 
279     PBoolean RegisterGatekeeper();
280 
281 #ifdef H323_H46026
282     /**Set Flag to say media tunneling
283       */
284     void SetH46026Tunnel(PBoolean tunnel);
285 
286     /**Is Media Tunneling
287       */
288     PBoolean IsH46026Tunnel();
289 #endif
290 
291 private:
292     H323EndPoint & ep;
293     H46017Transport * curtransport;
294     H46017RasTransport * ras;
295 
296     H323TransportAddress remoteAddress;
297     PIPSocket::Address localBindAddress;
298 
299     PBoolean connectionlost;
300     PBoolean openTransport;
301 
302 #ifdef H323_H46026
303     PBoolean   m_h46026tunnel;
304 #endif
305 
306 };
307 
308 //////////////////////////////////////////////////////////////////////
309 
310 class H46017RasTransport : public H323TransportUDP
311 {
312   PCLASSINFO(H46017RasTransport, H323TransportUDP);
313 
314   public:
315 
316     H46017RasTransport(
317       H323EndPoint & endpoint,
318       H46017Handler * handler
319     );
320     ~H46017RasTransport();
321 
322     virtual H323TransportAddress GetLocalAddress() const;
323 
324     virtual void SetUpTransportPDU(
325       H225_TransportAddress & pdu,
326       PBoolean localTsap,
327       H323Connection * connection = NULL
328     ) const;
329 
330     virtual PBoolean SetRemoteAddress(
331       const H323TransportAddress & address
332     );
333 
334     virtual H323TransportAddress GetRemoteAddress() const;
335 
336     virtual PBoolean Connect();
337 
338     /**Close the channel.
339       */
340     virtual PBoolean Close();
341 
342 
343     PBoolean ReceivedPDU(
344       const PBYTEArray & pdu  ///<  PDU read from Tunnel
345     );
346 
347     virtual PBoolean ReadPDU(
348       PBYTEArray & pdu   ///<  PDU read input
349     );
350 
351     virtual PBoolean WritePDU(
352       const PBYTEArray & pdu  ///<  PDU to write to tunnel
353     );
354 
355     virtual PBoolean DiscoverGatekeeper(
356       H323Gatekeeper & gk,                  ///<  Gatekeeper to set on discovery.
357       H323RasPDU & pdu,                     ///<  GatekeeperRequest PDU
358       const H323TransportAddress & address  ///<  Address of gatekeeper (if present)
359     );
360 
361     virtual PBoolean IsRASTunnelled();
362 
363     virtual PChannel::Errors GetErrorCode(ErrorGroup group = NumErrorGroups) const;
364 
365     virtual void CleanUpOnTermination();
366 
367   protected:
368     H46017Handler * m_handler;
369 
370   private:
371     PSyncPoint  msgRecd;
372     PBYTEArray  recdpdu;
373 
374     PBoolean    shutdown;
375 
376 };
377 
378 
379 #endif
380 
381 
382 
383