compat_h323.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "ast_h323.h"
  35. #include "compat_h323.h"
  36. #if VERSION(OPENH323_MAJOR,OPENH323_MINOR,OPENH323_BUILD) < VERSION(1,17,3)
  37. MyH323TransportTCP::MyH323TransportTCP(
  38. H323EndPoint & endpoint,
  39. PIPSocket::Address binding,
  40. PBoolean listen)
  41. : H323TransportTCP(endpoint, binding, listen)
  42. {
  43. }
  44. PBoolean MyH323TransportTCP::Connect()
  45. {
  46. if (IsListening())
  47. return TRUE;
  48. PTCPSocket * socket = new PTCPSocket(remotePort);
  49. Open(socket);
  50. channelPointerMutex.StartRead();
  51. socket->SetReadTimeout(10000/*endpoint.GetSignallingChannelConnectTimeout()*/);
  52. localPort = endpoint.GetNextTCPPort();
  53. WORD firstPort = localPort;
  54. for (;;) {
  55. PTRACE(4, "H323TCP\tConnecting to "
  56. << remoteAddress << ':' << remotePort
  57. << " (local port=" << localPort << ')');
  58. if (socket->Connect(localAddress, localPort, remoteAddress))
  59. break;
  60. int errnum = socket->GetErrorNumber();
  61. if (localPort == 0 || (errnum != EADDRINUSE && errnum != EADDRNOTAVAIL)) {
  62. PTRACE(1, "H323TCP\tCould not connect to "
  63. << remoteAddress << ':' << remotePort
  64. << " (local port=" << localPort << ") - "
  65. << socket->GetErrorText() << '(' << errnum << ')');
  66. channelPointerMutex.EndRead();
  67. return SetErrorValues(socket->GetErrorCode(), errnum);
  68. }
  69. localPort = endpoint.GetNextTCPPort();
  70. if (localPort == firstPort) {
  71. PTRACE(1, "H323TCP\tCould not bind to any port in range " <<
  72. endpoint.GetTCPPortBase() << " to " << endpoint.GetTCPPortMax());
  73. channelPointerMutex.EndRead();
  74. return SetErrorValues(socket->GetErrorCode(), errnum);
  75. }
  76. }
  77. socket->SetReadTimeout(PMaxTimeInterval);
  78. channelPointerMutex.EndRead();
  79. return OnOpen();
  80. }
  81. #endif
  82. PBoolean MyH323TransportUDP::DiscoverGatekeeper(H323Gatekeeper &gk, H323RasPDU &pdu, const H323TransportAddress &address)
  83. {
  84. PThread *thd = PThread::Current();
  85. /* If we run in OpenH323's thread use it instead of creating new one */
  86. if (thd)
  87. return H323TransportUDP::DiscoverGatekeeper(gk, pdu, address);
  88. /* Make copy of arguments to pass them into thread */
  89. discoverGatekeeper = &gk;
  90. discoverPDU = &pdu;
  91. discoverAddress = &address;
  92. /* Assume discovery thread isn't finished */
  93. discoverReady = FALSE;
  94. /* Create discovery thread */
  95. thd = PThread::Create(PCREATE_NOTIFIER(DiscoverMain), 0,
  96. PThread::NoAutoDeleteThread,
  97. PThread::NormalPriority,
  98. "GkDiscovery:%x");
  99. /* Wait until discovery thread signal us its finished */
  100. for(;;) {
  101. discoverMutex.Wait();
  102. if (discoverReady) /* Thread has been finished */
  103. break;
  104. discoverMutex.Signal();
  105. }
  106. discoverMutex.Signal();
  107. /* Cleanup/delete thread */
  108. thd->WaitForTermination();
  109. delete thd;
  110. return discoverResult;
  111. }
  112. void MyH323TransportUDP::DiscoverMain(PThread &thread, INT arg)
  113. {
  114. PWaitAndSignal m(discoverMutex);
  115. discoverResult = H323TransportUDP::DiscoverGatekeeper(*discoverGatekeeper, *discoverPDU, *discoverAddress);
  116. discoverReady = TRUE;
  117. }