Change file naming to be conform OpenH323 rules
[asterisk/asterisk.git] / channels / h323 / compat_h323.cxx
1 #ifndef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #endif
4 /*
5  * ast_h323.cpp
6  *
7  * OpenH323 Channel Driver for ASTERISK PBX.
8  *                      By  Jeremy McNamara
9  *                      For The NuFone Network
10  * 
11  * chan_h323 has been derived from code created by
12  *               Michael Manousos and Mark Spencer
13  *
14  * This file is part of the chan_h323 driver for Asterisk
15  *
16  * chan_h323 is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version. 
20  *
21  * chan_h323 is distributed WITHOUT ANY WARRANTY; without even 
22  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
23  * PURPOSE. See the GNU General Public License for more details. 
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
28  *
29  * Version Info: $Id$
30  */
31 #include <ptlib.h>
32 #include <h323.h>
33 #include <transports.h>
34
35 #include "ast_h323.h"
36
37 #if VERSION(OPENH323_MAJOR,OPENH323_MINOR,OPENH323_BUILD) < VERSION(1,17,3)
38 MyH323TransportTCP::MyH323TransportTCP(
39                                 H323EndPoint & endpoint,
40                                 PIPSocket::Address binding,
41                                 BOOL listen)
42         : H323TransportTCP(endpoint, binding, listen)
43 {
44 }
45
46 BOOL MyH323TransportTCP::Connect()
47 {
48         if (IsListening())
49                 return TRUE;
50
51         PTCPSocket * socket = new PTCPSocket(remotePort);
52         Open(socket);
53
54         channelPointerMutex.StartRead();
55
56         socket->SetReadTimeout(10000/*endpoint.GetSignallingChannelConnectTimeout()*/);
57
58         localPort = endpoint.GetNextTCPPort();
59         WORD firstPort = localPort;
60         for (;;) {
61                 PTRACE(4, "H323TCP\tConnecting to "
62                                 << remoteAddress << ':' << remotePort
63                                 << " (local port=" << localPort << ')');
64                 if (socket->Connect(localAddress, localPort, remoteAddress))
65                         break;
66
67                 int errnum = socket->GetErrorNumber();
68                 if (localPort == 0 || (errnum != EADDRINUSE && errnum != EADDRNOTAVAIL)) {
69                         PTRACE(1, "H323TCP\tCould not connect to "
70                                         << remoteAddress << ':' << remotePort
71                                         << " (local port=" << localPort << ") - "
72                                         << socket->GetErrorText() << '(' << errnum << ')');
73                         channelPointerMutex.EndRead();
74                         return SetErrorValues(socket->GetErrorCode(), errnum);
75                 }
76
77                 localPort = endpoint.GetNextTCPPort();
78                 if (localPort == firstPort) {
79                         PTRACE(1, "H323TCP\tCould not bind to any port in range " <<
80                                         endpoint.GetTCPPortBase() << " to " << endpoint.GetTCPPortMax());
81                         channelPointerMutex.EndRead();
82                         return SetErrorValues(socket->GetErrorCode(), errnum);
83                 }
84         }
85
86         socket->SetReadTimeout(PMaxTimeInterval);
87
88         channelPointerMutex.EndRead();
89
90         return OnOpen();
91 }
92 #endif
93
94 BOOL MyH323TransportUDP::DiscoverGatekeeper(H323Gatekeeper &gk, H323RasPDU &pdu, const H323TransportAddress &address)
95 {
96         PThread *thd = PThread::Current();
97
98         /* If we run in OpenH323's thread use it instead of creating new one */
99         if (thd)
100                 return H323TransportUDP::DiscoverGatekeeper(gk, pdu, address);
101
102         /* Make copy of arguments to pass them into thread */
103         discoverGatekeeper = &gk;
104         discoverPDU = &pdu;
105         discoverAddress = &address;
106
107         /* Assume discovery thread isn't finished */
108         discoverReady = FALSE;
109
110         /* Create discovery thread */
111         thd = PThread::Create(PCREATE_NOTIFIER(DiscoverMain), 0,
112                                                         PThread::NoAutoDeleteThread,
113                                                         PThread::NormalPriority,
114                                                         "GkDiscovery:%x");
115
116         /* Wait until discovery thread signal us its finished */ 
117         for(;;) {
118                 discoverMutex.Wait();
119                 if (discoverReady)              /* Thread has been finished */
120                         break;
121                 discoverMutex.Signal();
122         }
123         discoverMutex.Signal();
124
125         /* Cleanup/delete thread */
126         thd->WaitForTermination();
127         delete thd;
128
129         return discoverResult;
130 }
131
132 void MyH323TransportUDP::DiscoverMain(PThread &thread, INT arg)
133 {
134         PWaitAndSignal m(discoverMutex);
135
136         discoverResult = H323TransportUDP::DiscoverGatekeeper(*discoverGatekeeper, *discoverPDU, *discoverAddress);
137         discoverReady = TRUE;
138 }