aboutsummaryrefslogtreecommitdiffstats
path: root/channels/h323/compat_h323.cxx
blob: 609d18e8a587e28dea0ab39ee3f758e5f1042fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/*
 * ast_h323.cpp
 *
 * OpenH323 Channel Driver for ASTERISK PBX.
 *			By  Jeremy McNamara
 *			For The NuFone Network
 * 
 * chan_h323 has been derived from code created by
 *               Michael Manousos and Mark Spencer
 *
 * This file is part of the chan_h323 driver for Asterisk
 *
 * chan_h323 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 *
 * chan_h323 is distributed WITHOUT ANY WARRANTY; without even 
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 * PURPOSE. See the GNU General Public License for more details. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 * Version Info: $Id$
 */
#include <ptlib.h>
#include <h323.h>
#include <transports.h>

#include "ast_h323.h"
#include "compat_h323.h"

#if VERSION(OPENH323_MAJOR,OPENH323_MINOR,OPENH323_BUILD) < VERSION(1,17,3)
MyH323TransportTCP::MyH323TransportTCP(
				H323EndPoint & endpoint,
				PIPSocket::Address binding,
				PBoolean listen)
	: H323TransportTCP(endpoint, binding, listen)
{
}

PBoolean MyH323TransportTCP::Connect()
{
	if (IsListening())
		return TRUE;

	PTCPSocket * socket = new PTCPSocket(remotePort);
	Open(socket);

	channelPointerMutex.StartRead();

	socket->SetReadTimeout(10000/*endpoint.GetSignallingChannelConnectTimeout()*/);

	localPort = endpoint.GetNextTCPPort();
	WORD firstPort = localPort;
	for (;;) {
		PTRACE(4, "H323TCP\tConnecting to "
				<< remoteAddress << ':' << remotePort
				<< " (local port=" << localPort << ')');
		if (socket->Connect(localAddress, localPort, remoteAddress))
			break;

		int errnum = socket->GetErrorNumber();
		if (localPort == 0 || (errnum != EADDRINUSE && errnum != EADDRNOTAVAIL)) {
			PTRACE(1, "H323TCP\tCould not connect to "
					<< remoteAddress << ':' << remotePort
					<< " (local port=" << localPort << ") - "
					<< socket->GetErrorText() << '(' << errnum << ')');
			channelPointerMutex.EndRead();
			return SetErrorValues(socket->GetErrorCode(), errnum);
		}

		localPort = endpoint.GetNextTCPPort();
		if (localPort == firstPort) {
			PTRACE(1, "H323TCP\tCould not bind to any port in range " <<
					endpoint.GetTCPPortBase() << " to " << endpoint.GetTCPPortMax());
			channelPointerMutex.EndRead();
			return SetErrorValues(socket->GetErrorCode(), errnum);
		}
	}

	socket->SetReadTimeout(PMaxTimeInterval);

	channelPointerMutex.EndRead();

	return OnOpen();
}
#endif

PBoolean MyH323TransportUDP::DiscoverGatekeeper(H323Gatekeeper &gk, H323RasPDU &pdu, const H323TransportAddress &address)
{
	PThread *thd = PThread::Current();

	/* If we run in OpenH323's thread use it instead of creating new one */
	if (thd)
		return H323TransportUDP::DiscoverGatekeeper(gk, pdu, address);

	/* Make copy of arguments to pass them into thread */
	discoverGatekeeper = &gk;
	discoverPDU = &pdu;
	discoverAddress = &address;

	/* Assume discovery thread isn't finished */
	discoverReady = FALSE;

	/* Create discovery thread */
	thd = PThread::Create(PCREATE_NOTIFIER(DiscoverMain), 0,
							PThread::NoAutoDeleteThread,
							PThread::NormalPriority,
							"GkDiscovery:%x");

	/* Wait until discovery thread signal us its finished */ 
	for(;;) {
		discoverMutex.Wait();
		if (discoverReady)		/* Thread has been finished */
			break;
		discoverMutex.Signal();
	}
	discoverMutex.Signal();

	/* Cleanup/delete thread */
	thd->WaitForTermination();
	delete thd;

	return discoverResult;
}

void MyH323TransportUDP::DiscoverMain(PThread &thread, INT arg)
{
	PWaitAndSignal m(discoverMutex);

	discoverResult = H323TransportUDP::DiscoverGatekeeper(*discoverGatekeeper, *discoverPDU, *discoverAddress);
	discoverReady = TRUE;
}