7 * OpenH323 Channel Driver for ASTERISK PBX.
9 * For The NuFone Network
11 * chan_h323 has been derived from code created by
12 * Michael Manousos and Mark Spencer
14 * This file is part of the chan_h323 driver for Asterisk
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.
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.
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.
33 #include <transports.h>
37 #if VERSION(OPENH323_MAJOR,OPENH323_MINOR,OPENH323_BUILD) < VERSION(1,17,3)
38 MyH323TransportTCP::MyH323TransportTCP(
39 H323EndPoint
& endpoint
,
40 PIPSocket::Address binding
,
42 : H323TransportTCP(endpoint
, binding
, listen
)
46 BOOL
MyH323TransportTCP::Connect()
51 PTCPSocket
* socket
= new PTCPSocket(remotePort
);
54 channelPointerMutex
.StartRead();
56 socket
->SetReadTimeout(10000/*endpoint.GetSignallingChannelConnectTimeout()*/);
58 localPort
= endpoint
.GetNextTCPPort();
59 WORD firstPort
= localPort
;
61 PTRACE(4, "H323TCP\tConnecting to "
62 << remoteAddress
<< ':' << remotePort
63 << " (local port=" << localPort
<< ')');
64 if (socket
->Connect(localAddress
, localPort
, remoteAddress
))
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
);
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
);
86 socket
->SetReadTimeout(PMaxTimeInterval
);
88 channelPointerMutex
.EndRead();
94 BOOL
MyH323TransportUDP::DiscoverGatekeeper(H323Gatekeeper
&gk
, H323RasPDU
&pdu
, const H323TransportAddress
&address
)
96 PThread
*thd
= PThread::Current();
98 /* If we run in OpenH323's thread use it instead of creating new one */
100 return H323TransportUDP::DiscoverGatekeeper(gk
, pdu
, address
);
102 /* Make copy of arguments to pass them into thread */
103 discoverGatekeeper
= &gk
;
105 discoverAddress
= &address
;
107 /* Assume discovery thread isn't finished */
108 discoverReady
= FALSE
;
110 /* Create discovery thread */
111 thd
= PThread::Create(PCREATE_NOTIFIER(DiscoverMain
), 0,
112 PThread::NoAutoDeleteThread
,
113 PThread::NormalPriority
,
116 /* Wait until discovery thread signal us its finished */
118 discoverMutex
.Wait();
119 if (discoverReady
) /* Thread has been finished */
121 discoverMutex
.Signal();
123 discoverMutex
.Signal();
125 /* Cleanup/delete thread */
126 thd
->WaitForTermination();
129 return discoverResult
;
132 void MyH323TransportUDP::DiscoverMain(PThread
&thread
, INT arg
)
134 PWaitAndSignal
m(discoverMutex
);
136 discoverResult
= H323TransportUDP::DiscoverGatekeeper(*discoverGatekeeper
, *discoverPDU
, *discoverAddress
);
137 discoverReady
= TRUE
;