4 * OpenH323 Channel Driver for ASTERISK PBX.
6 * For The NuFone Network
8 * chan_h323 has been derived from code created by
9 * Michael Manousos and Mark Spencer
11 * This file is part of the chan_h323 driver for Asterisk
13 * chan_h323 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * chan_h323 is distributed WITHOUT ANY WARRANTY; without even
19 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <arpa/inet.h>
43 #include <asterisk/logger.h>
48 #include "chan_h323.h"
51 /* PWlib Required Components */
52 #define MAJOR_VERSION 1
53 #define MINOR_VERSION 0
54 #define BUILD_TYPE ReleaseCode
55 #define BUILD_NUMBER 0
57 /** Counter for the number of connections */
61 int mode = H323_DTMF_RFC2833;
64 * We assume that only one endPoint should exist.
65 * The application cannot run the h323_end_point_create() more than once
66 * FIXME: Singleton this, for safety
68 MyH323EndPoint *endPoint = NULL;
70 /** PWLib entry point */
71 MyProcess *localProcess = NULL;
74 MyProcess::MyProcess(): PProcess("The NuFone Network's", "H.323 Channel Driver for Asterisk",
75 MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
80 void MyProcess::Main()
82 ast_verbose(" == Creating H.323 Endpoint\n");
83 endPoint = new MyH323EndPoint();
84 PTrace::Initialise(0, NULL, PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
87 #define H323_NAME OPAL_G7231_6k3"{sw}"
88 #define H323_G729 OPAL_G729 "{sw}"
89 #define H323_G729A OPAL_G729A"{sw}"
91 H323_REGISTER_CAPABILITY(H323_G7231Capability, H323_NAME);
92 H323_REGISTER_CAPABILITY(AST_G729Capability, H323_G729);
93 H323_REGISTER_CAPABILITY(AST_G729ACapability, H323_G729A);
95 H323_G7231Capability::H323_G7231Capability(BOOL annexA_)
96 : H323AudioCapability(7, 4)
101 PObject::Comparison H323_G7231Capability::Compare(const PObject & obj) const
103 Comparison result = H323AudioCapability::Compare(obj);
104 if (result != EqualTo)
107 PINDEX otherAnnexA = ((const H323_G7231Capability &)obj).annexA;
108 if (annexA < otherAnnexA)
110 if (annexA > otherAnnexA)
115 PObject * H323_G7231Capability::Clone() const
117 return new H323_G7231Capability(*this);
121 PString H323_G7231Capability::GetFormatName() const
127 unsigned H323_G7231Capability::GetSubType() const
129 return H245_AudioCapability::e_g7231;
133 BOOL H323_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
134 unsigned packetSize) const
136 cap.SetTag(H245_AudioCapability::e_g7231);
138 H245_AudioCapability_g7231 & g7231 = cap;
139 g7231.m_maxAl_sduAudioFrames = packetSize;
140 g7231.m_silenceSuppression = annexA;
146 BOOL H323_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
147 unsigned & packetSize)
149 if (cap.GetTag() != H245_AudioCapability::e_g7231)
152 const H245_AudioCapability_g7231 & g7231 = cap;
153 packetSize = g7231.m_maxAl_sduAudioFrames;
154 annexA = g7231.m_silenceSuppression;
160 H323Codec * H323_G7231Capability::CreateCodec(H323Codec::Direction direction) const
166 /////////////////////////////////////////////////////////////////////////////
168 AST_G729Capability::AST_G729Capability()
169 : H323AudioCapability(24, 6)
174 PObject * AST_G729Capability::Clone() const
176 return new AST_G729Capability(*this);
180 unsigned AST_G729Capability::GetSubType() const
182 return H245_AudioCapability::e_g729;
186 PString AST_G729Capability::GetFormatName() const
192 H323Codec * AST_G729Capability::CreateCodec(H323Codec::Direction direction) const
196 /////////////////////////////////////////////////////////////////////////////
198 AST_G729ACapability::AST_G729ACapability()
199 : H323AudioCapability(24, 6)
204 PObject * AST_G729ACapability::Clone() const
206 return new AST_G729ACapability(*this);
210 unsigned AST_G729ACapability::GetSubType() const
212 return H245_AudioCapability::e_g729AnnexA;
216 PString AST_G729ACapability::GetFormatName() const
222 H323Codec * AST_G729ACapability::CreateCodec(H323Codec::Direction direction) const
228 * The fullAddress parameter is used directly in the MakeCall method so
229 * the General form for the fullAddress argument is :
230 * [alias@][transport$]host[:port]
231 * default values: alias = the same value as host.
235 int MyH323EndPoint::MakeCall(const PString & dest, PString & token, unsigned int *callReference, unsigned int port, char *callerid, char *callername)
238 MyH323Connection * connection;
240 /* Determine whether we are using a gatekeeper or not. */
241 if (!GetGatekeeper()) {
244 cout << " -- Making call to " << fullAddress << " using gatekeeper." << endl;
249 cout << " -- Making call to " << fullAddress << "." << endl;
252 if (!(connection = (MyH323Connection *)H323EndPoint::MakeCallLocked(fullAddress, token))) {
254 cout << "Error making call to \"" << fullAddress << '"' << endl;
258 *callReference = connection->GetCallReference();
260 connection->SetLocalPartyName(PString(callerid));
263 localAliasNames.RemoveAll();
264 connection->SetLocalPartyName(PString(callername));
266 localAliasNames.AppendString(PString(callerid));
267 } else if (callerid) {
268 localAliasNames.RemoveAll();
269 connection->SetLocalPartyName(PString(callerid));
271 connection->AST_Outgoing = TRUE;
274 cout << " -- " << GetLocalUserName() << " is calling host " << fullAddress << endl;
275 cout << " -- " << "Call token is " << (const char *)token << endl;
276 cout << " -- Call reference is " << *callReference << endl;
278 connection->Unlock();
282 void MyH323EndPoint::SetEndpointTypeInfo( H225_EndpointType & info ) const
284 H323EndPoint::SetEndpointTypeInfo(info);
285 info.m_gateway.IncludeOptionalField(H225_GatewayInfo::e_protocol);
286 info.m_gateway.m_protocol.SetSize(1);
287 H225_SupportedProtocols &protocol=info.m_gateway.m_protocol[0];
288 protocol.SetTag(H225_SupportedProtocols::e_voice);
289 PINDEX as=SupportedPrefixes.GetSize();
290 ((H225_VoiceCaps &)protocol).m_supportedPrefixes.SetSize(as);
291 for (PINDEX p=0; p<as; p++) {
292 H323SetAliasAddress(SupportedPrefixes[p], ((H225_VoiceCaps &)protocol).m_supportedPrefixes[p].m_prefix);
296 void MyH323EndPoint::SetGateway(void)
298 terminalType = e_GatewayOnly;
301 H323Capabilities MyH323EndPoint::GetCapabilities(void)
306 BOOL MyH323EndPoint::ClearCall(const PString & token)
309 cout << " -- ClearCall: Request to clear call with token " << token << endl;
311 return H323EndPoint::ClearCall(token);
314 void MyH323EndPoint::SendUserTone(const PString &token, char tone)
316 H323Connection *connection = NULL;
318 connection = FindConnectionWithLock(token);
319 if (connection != NULL) {
320 connection->SendUserInputTone(tone, 500);
321 connection->Unlock();
325 void MyH323EndPoint::OnClosedLogicalChannel(H323Connection & connection, const H323Channel & channel)
329 cout << " channelsOpen = " << channelsOpen << endl;
330 H323EndPoint::OnClosedLogicalChannel(connection, channel);
333 BOOL MyH323EndPoint::OnConnectionForwarded(H323Connection & connection,
334 const PString & forwardParty,
335 const H323SignalPDU & pdu)
338 cout << " -- Call Forwarded to " << forwardParty << endl;
343 BOOL MyH323EndPoint::ForwardConnection(H323Connection & connection,
344 const PString & forwardParty,
345 const H323SignalPDU & pdu)
348 cout << " -- Forwarding call to " << forwardParty << endl;
350 return H323EndPoint::ForwardConnection(connection, forwardParty, pdu);
353 void MyH323EndPoint::OnConnectionEstablished(H323Connection & connection, const PString & estCallToken)
356 cout << "\t=-= In OnConnectionEstablished for call " << connection.GetCallReference() << endl;
357 cout << "\t\t-- Connection Established with \"" << connection.GetRemotePartyName() << "\"" << endl;
359 on_connection_established(connection.GetCallReference(), (const char *)connection.GetCallToken());
362 /** OnConnectionCleared callback function is called upon the dropping of an established
365 void MyH323EndPoint::OnConnectionCleared(H323Connection & connection, const PString & clearedCallToken)
369 PIPSocket::Address Ip;
372 remoteName = connection.GetRemotePartyName();
374 cd.call_reference = connection.GetCallReference();
375 cd.call_token = strdup((const char *)clearedCallToken);
376 cd.call_source_aliases = strdup((const char *)connection.GetRemotePartyName());
378 connection.GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
379 cd.sourceIp = strdup((const char *)Ip.AsString());
381 /* Convert complex strings */
383 if ((s = strchr(cd.call_source_aliases, ' ')) != NULL)
386 switch (connection.GetCallEndReason()) {
387 case H323Connection::EndedByCallForwarded :
389 cout << " -- " << remoteName << " has forwarded the call" << endl;
391 case H323Connection::EndedByRemoteUser :
393 cout << " -- " << remoteName << " has cleared the call" << endl;
395 case H323Connection::EndedByCallerAbort :
397 cout << " -- " << remoteName << " has stopped calling" << endl;
399 case H323Connection::EndedByRefusal :
401 cout << " -- " << remoteName << " did not accept your call" << endl;
403 case H323Connection::EndedByRemoteBusy :
405 cout << " -- " << remoteName << " was busy" << endl;
407 case H323Connection::EndedByRemoteCongestion :
409 cout << " -- Congested link to " << remoteName << endl;
411 case H323Connection::EndedByNoAnswer :
413 cout << " -- " << remoteName << " did not answer your call" << endl;
415 case H323Connection::EndedByTransportFail :
417 cout << " -- Call with " << remoteName << " ended abnormally" << endl;
419 case H323Connection::EndedByCapabilityExchange :
421 cout << " -- Could not find common codec with " << remoteName << endl;
423 case H323Connection::EndedByNoAccept :
425 cout << " -- Did not accept incoming call from " << remoteName << endl;
427 case H323Connection::EndedByAnswerDenied :
429 cout << " -- Refused incoming call from " << remoteName << endl;
431 case H323Connection::EndedByNoUser :
433 cout << " -- Remote endpoint could not find user: " << remoteName << endl;
435 case H323Connection::EndedByNoBandwidth :
437 cout << " -- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
439 case H323Connection::EndedByUnreachable :
441 cout << " -- " << remoteName << " could not be reached." << endl;
443 case H323Connection::EndedByHostOffline :
445 cout << " -- " << remoteName << " is not online." << endl;
447 case H323Connection::EndedByNoEndPoint :
449 cout << " -- No phone running for " << remoteName << endl;
451 case H323Connection::EndedByConnectFail :
453 cout << " -- Transport error calling " << remoteName << endl;
457 cout << " -- Call with " << remoteName << " completed (" << connection.GetCallEndReason() << ")" << endl;
461 if(connection.IsEstablished())
463 cout << " -- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
465 /* Invoke the PBX application registered callback */
466 on_connection_cleared(cd);
472 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *outbound)
474 unsigned options = 0;
477 options |= H323Connection::FastStartOptionDisable;
479 options |= H323Connection::FastStartOptionEnable;
482 options |= H323Connection::H245TunnelingOptionDisable;
484 options |= H323Connection::H245TunnelingOptionEnable;
486 return new MyH323Connection(*this, callReference, options);
489 /* MyH323Connection */
490 MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference,
492 : H323Connection(ep, callReference, options)
495 cout << " == New H.323 Connection created." << endl;
497 AST_RTP_Connected = FALSE;
498 AST_Outgoing = FALSE;
502 MyH323Connection::~MyH323Connection()
505 cout << " == H.323 Connection deleted." << endl;
510 H323Connection::AnswerCallResponse MyH323Connection::OnAnswerCall(const PString & caller,
511 const H323SignalPDU & /*setupPDU*/,
512 H323SignalPDU & /*connectPDU*/)
516 cout << "\t=-= In OnAnswerCall for call " << GetCallReference() << endl;
518 if (!on_answer_call(GetCallReference(), (const char *)GetCallToken()))
519 return H323Connection::AnswerCallDenied;
521 /* The call will be answered later with "AnsweringCall()" function.
523 return H323Connection::AnswerCallDeferred;
526 BOOL MyH323Connection::OnAlerting(const H323SignalPDU & /*alertingPDU*/, const PString & username)
528 PIPSocket::Address remoteIpAddress;
530 H323_ExternalRTPChannel * channel;
533 cout << "\t =-= In OnAlerting for call " << GetCallReference()
534 << ": sessionId=" << sessionId << endl;
536 /* Connect RTP if logical channel has already been opened */
538 if ( (channel = (H323_ExternalRTPChannel*) FindChannel(sessionId,TRUE)) ) {
539 channel->GetRemoteAddress(remoteIpAddress, remotePort);
541 cout << "\t\t--- found logical channel. Connecting RTP" << endl;
542 cout << "\t\tRTP channel id " << sessionId << " parameters:" << endl;
543 cout << "\t\t-- remoteIpAddress: " << remoteIpAddress << endl;
544 cout << "\t\t-- remotePort: " << remotePort << endl;
545 cout << "\t\t-- ExternalIpAddress: " << externalIpAddress << endl;
546 cout << "\t\t-- ExternalPort: " << externalPort << endl;
548 on_start_logical_channel(GetCallReference(),(const char *)remoteIpAddress.AsString(), remotePort,
549 (const char *)GetCallToken() );
550 AST_RTP_Connected=TRUE;
553 cout << "\t\t--- no logical channels" << endl;
556 cout << " -- Ringing phone for \"" << username << "\"" << endl;
559 on_chan_ringing(GetCallReference(), (const char *)GetCallToken() );
563 ast_log(LOG_ERROR,"chan_h323: OnAlerting: Could not obtain connection lock");
567 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
571 ast_verbose(" -- Received SETUP message\n");
578 PString sourceAliases;
580 PIPSocket::Address Ip;
584 sourceAliases = setupPDU.GetSourceAliases();
585 destAliases = setupPDU.GetDestinationAlias();
588 setupPDU.GetSourceE164(sourceE164);
590 sourceName=setupPDU.GetQ931().GetDisplayName();
592 setupPDU.GetDestinationE164(destE164);
594 /* Convert complex strings */
595 // FIXME: deal more than one source alias
596 if ((s = strchr(sourceAliases, ' ')) != NULL)
598 if ((s = strchr(sourceAliases, '\t')) != NULL)
600 if ((s1 = strchr(destAliases, ' ')) != NULL)
602 if ((s1 = strchr(destAliases, '\t')) != NULL)
606 cd.call_reference = GetCallReference();
608 cd.call_token = strdup((const char *)GetCallToken());
610 cd.call_source_aliases = strdup((const char *)sourceAliases);
611 cd.call_dest_alias = strdup((const char *)destAliases);
612 cd.call_source_e164 = strdup((const char *)sourceE164);
613 cd.call_dest_e164 = strdup((const char *)destE164);
614 cd.call_source_name = strdup((const char *)sourceName);
616 GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
617 cd.sourceIp = strdup((const char *)Ip.AsString());
619 /* Notify Asterisk of the request */
620 int res = on_incoming_call(cd);
624 cout << " -- Call Failed" << endl;
629 return H323Connection::OnReceivedSignalSetup(setupPDU);
632 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
638 cout << " -- Sending SETUP message" << endl;
640 sourceAliases = setupPDU.GetSourceAliases();
641 destAliases = setupPDU.GetDestinationAlias();
644 setupPDU.GetSourceE164(sourceE164);
646 setupPDU.GetDestinationE164(destE164);
648 /* Convert complex strings */
649 // FIXME: deal more than one source alias
651 if ((s = strchr(sourceAliases, ' ')) != NULL)
653 if ((s = strchr(sourceAliases, '\t')) != NULL)
655 if ((s1 = strchr(destAliases, ' ')) != NULL)
657 if ((s1 = strchr(destAliases, '\t')) != NULL)
660 cd.call_reference = GetCallReference();
662 cd.call_token = strdup((const char *)GetCallToken());
664 cd.call_source_aliases = strdup((const char *)sourceAliases);
665 cd.call_dest_alias = strdup((const char *)destAliases);
666 cd.call_source_e164 = strdup((const char *)sourceE164);
667 cd.call_dest_e164 = strdup((const char *)destE164);
669 int res = on_outgoing_call(cd);
673 cout << " -- Call Failed" << endl;
678 return H323Connection::OnSendSignalSetup(setupPDU);
681 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
684 cout << " -- Sending RELEASE COMPLETE" << endl;
686 return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
689 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
692 cout << " -- Received Facility message... " << endl;
694 return H323Connection::OnReceivedFacility(pdu);
697 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
700 cout << " -- Received RELEASE COMPLETE message..." << endl;
702 return H323Connection::OnReceivedReleaseComplete(pdu);
705 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
708 cout << " -- Closing logical channel..." << endl;
710 return H323Connection::OnClosingLogicalChannel(channel);
714 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
717 cout << " -- Sending user input tone (" << tone << ") to remote" << endl;
719 on_send_digit(GetCallReference(), tone);
720 H323Connection::SendUserInputTone(tone, duration);
723 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
725 if (mode == H323_DTMF_INBAND) {
727 cout << " -- Received user input tone (" << tone << ") from remote" << endl;
729 on_send_digit(GetCallReference(), tone);
731 H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
734 void MyH323Connection::OnUserInputString(const PString &value)
736 if (mode == H323_DTMF_RFC2833) {
738 cout << " -- Received user input string (" << value << ") from remote." << endl;
740 on_send_digit(GetCallReference(), value[0]);
744 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
745 H323Channel::Directions dir,
747 const H245_H2250LogicalChannelParameters * /*param*/)
749 struct rtp_info *info;
752 /* Determine the Local (A side) IP Address and port */
753 info = on_create_connection(GetCallReference(), (const char *)GetCallToken());
759 GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
760 externalPort = info->port;
761 sessionId = sessionID;
764 cout << " =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
765 cout << " -- externalIpAddress: " << externalIpAddress << endl;
766 cout << " -- externalPort: " << externalPort << endl;
767 cout << " -- SessionID: " << sessionID << endl;
768 cout << " -- Direction: " << dir << endl;
771 return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
774 /** This callback function is invoked once upon creation of each
775 * channel for an H323 session
777 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
779 PIPSocket::Address remoteIpAddress;
783 cout << " -- Started logical channel: ";
784 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));
785 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
788 /* adjust the count of channels we have open */
792 cout << " -- channelsOpen = " << channelsOpen << endl;
796 ast_log(LOG_ERROR,"chan_h323: OnStartLogicalChannel: Could not obtain connection lock");
799 /* Connect RTP for incoming calls */
801 H323_ExternalRTPChannel & external = (H323_ExternalRTPChannel &)channel;
802 external.GetRemoteAddress(remoteIpAddress, remotePort);
804 cout << "\t\tRTP channel id " << sessionId << " parameters:" << endl;
805 cout << "\t\t-- remoteIpAddress: " << remoteIpAddress << endl;
806 cout << "\t\t-- remotePort: " << remotePort << endl;
807 cout << "\t\t-- ExternalIpAddress: " << externalIpAddress << endl;
808 cout << "\t\t-- ExternalPort: " << externalPort << endl;
810 /* Notify Asterisk of remote RTP information */
811 on_start_logical_channel(GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort,
812 (const char *)GetCallToken());
813 AST_RTP_Connected = TRUE;
819 /* MyH323_ExternalRTPChannel */
820 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
821 const H323Capability & capability,
822 Directions direction,
824 const PIPSocket::Address & ip,
826 : H323_ExternalRTPChannel(connection, capability, direction, sessionID, ip, dataPort)
830 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
831 const H323Capability & capability,
832 Directions direction,
834 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id)
838 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
839 const H323Capability & capability,
840 Directions direction,
842 const H323TransportAddress & data,
843 const H323TransportAddress & control)
844 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id, data, control)
848 MyH323_ExternalRTPChannel::~MyH323_ExternalRTPChannel()
852 BOOL MyH323_ExternalRTPChannel::OnReceivedAckPDU(const H245_H2250LogicalChannelAckParameters & param)
854 PIPSocket::Address remoteIpAddress;
856 MyH323Connection* conn = (MyH323Connection*) &connection;
859 cout << "\t=-= In OnReceivedAckPDU for call " << connection.GetCallReference() << endl;
861 if (H323_ExternalRTPChannel::OnReceivedAckPDU(param)) {
862 if (!connection.Lock()) {
863 ast_log(LOG_ERROR,"chan_h323: OnReceivedAckPDU: Could not obtain connection lock");
866 /* if RTP hasn't been connected yet */
867 if (!conn->AST_RTP_Connected) {
868 H323_ExternalRTPChannel::GetRemoteAddress(remoteIpAddress, remotePort);
870 cout << "\t\tRTP channel id " << sessionID << " parameters:" << endl;
871 cout << "\t\t-- remoteIpAddress: " << remoteIpAddress << endl;
872 cout << "\t\t-- remotePort: " << remotePort << endl;
873 cout << "\t\t-- ExternalIpAddress: " << conn->externalIpAddress << endl;
874 cout << "\t\t-- ExternalPort: " << conn->externalPort << endl;
877 /* Notify Asterisk of remote RTP information */
878 on_start_logical_channel(connection.GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort,
879 (const char *)conn->GetCallToken());
880 conn->AST_RTP_Connected = TRUE;
887 /** IMPLEMENTATION OF C FUNCTIONS */
890 * The extern "C" directive takes care for
891 * the ANSI-C representation of linkable symbols
896 int h323_end_point_exist(void)
904 void h323_end_point_create(int no_fast_start, int no_h245_tunneling)
908 noFastStart = (BOOL)no_fast_start;
909 noH245Tunneling = (BOOL)no_h245_tunneling;
911 localProcess = new MyProcess();
912 localProcess->Main();
915 void h323_gk_urq(void)
917 if (!h323_end_point_exist()) {
918 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
921 endPoint->RemoveGatekeeper();
924 void h323_end_process(void)
926 endPoint->ClearAllCalls();
927 endPoint->RemoveListener(NULL);
932 void h323_debug(int flag, unsigned level)
935 PTrace:: SetLevel(level);
937 PTrace:: SetLevel(0);
941 /** Installs the callback functions on behalf of the PBX application */
942 void h323_callback_register(setup_incoming_cb ifunc,
943 setup_outbound_cb sfunc,
944 on_connection_cb confunc,
945 start_logchan_cb lfunc,
947 chan_ringing_cb rfunc,
948 con_established_cb efunc,
950 answer_call_cb acfunc)
952 on_incoming_call = ifunc;
953 on_outgoing_call = sfunc;
954 on_create_connection = confunc;
955 on_start_logical_channel = lfunc;
956 on_connection_cleared = clfunc;
957 on_chan_ringing = rfunc;
958 on_connection_established = efunc;
959 on_send_digit = dfunc;
960 on_answer_call = acfunc;
964 * Add capability to the capability table of the end point.
966 int h323_set_capability(int cap, int dtmfMode)
968 H323Capabilities oldcaps;
973 if (!h323_end_point_exist()) {
974 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
978 /* clean up old capabilities list before changing */
979 oldcaps = endPoint->GetCapabilities();
980 for (PINDEX i=0; i< oldcaps.GetSize(); i++) {
981 codecs.AppendString(oldcaps[i].GetFormatName());
983 endPoint->RemoveCapabilities(codecs);
986 if (dtmfMode == H323_DTMF_INBAND) {
987 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
989 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
992 if (cap & AST_FORMAT_SPEEX) {
993 /* Not real sure if Asterisk acutally supports all
994 of the various different bit rates so add them
995 all and figure it out later*/
997 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
998 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
999 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
1000 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
1001 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
1004 if (cap & AST_FORMAT_G729A) {
1005 AST_G729ACapability *g729aCap;
1006 AST_G729Capability *g729Cap;
1007 endPoint->SetCapability(0, 0, g729aCap = new AST_G729ACapability);
1008 endPoint->SetCapability(0, 0, g729Cap = new AST_G729Capability);
1011 if (cap & AST_FORMAT_G723_1) {
1012 H323_G7231Capability *g7231Cap;
1013 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
1016 if (cap & AST_FORMAT_GSM) {
1017 H323_GSM0610Capability *gsmCap;
1018 endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
1019 gsmCap->SetTxFramesInPacket(gsmFrames);
1022 if (cap & AST_FORMAT_ULAW) {
1023 H323_G711Capability *g711uCap;
1024 endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
1025 g711uCap->SetTxFramesInPacket(g711Frames);
1028 if (cap & AST_FORMAT_ALAW) {
1029 H323_G711Capability *g711aCap;
1030 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
1031 g711aCap->SetTxFramesInPacket(g711Frames);
1035 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1040 /** Start the H.323 listener */
1041 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
1044 if (!h323_end_point_exist()) {
1045 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
1049 PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
1055 /** H.323 listener */
1056 H323ListenerTCP *tcpListener;
1058 tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
1060 if (!endPoint->StartListener(tcpListener)) {
1061 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
1066 cout << " == H.323 listener started" << endl;
1072 int h323_set_alias(struct oh323_alias *alias)
1076 PString h323id(alias->name);
1077 PString e164(alias->e164);
1079 if (!h323_end_point_exist()) {
1080 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
1084 cout << " == Adding alias \"" << h323id << "\" to endpoint" << endl;
1085 endPoint->AddAliasName(h323id);
1086 endPoint->RemoveAliasName(localProcess->GetUserName());
1088 if (!e164.IsEmpty()) {
1089 cout << " == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
1090 endPoint->AddAliasName(e164);
1092 if (strlen(alias->prefix)) {
1094 num = strsep(&p, ",");
1096 cout << " == Adding Prefix \"" << num << "\" to endpoint" << endl;
1097 endPoint->SupportedPrefixes += PString(num);
1098 endPoint->SetGateway();
1099 num = strsep(&p, ",");
1107 void h323_set_id(char *id)
1112 cout << " == Using '" << h323id << "' as our H.323ID for this call" << endl;
1116 endPoint->SetLocalUserName(h323id);
1119 void h323_show_tokens(void)
1121 cout << "Current call tokens: " << setprecision(2) << endPoint->GetAllConnections() << endl;
1125 /** Establish Gatekeeper communiations, if so configured,
1126 * register aliases for the H.323 endpoint to respond to.
1128 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
1130 PString gkName = PString(gatekeeper);
1131 PString pass = PString(secret);
1132 H323TransportUDP *rasChannel;
1134 if (!h323_end_point_exist()) {
1135 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
1140 cout << "Error: Gatekeeper cannot be NULL" << endl;
1144 if (strlen(secret)) {
1145 endPoint->SetGatekeeperPassword(pass);
1148 if (gatekeeper_discover) {
1149 /* discover the gk using multicast */
1150 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
1151 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1153 cout << " *** Could not find a gatekeeper." << endl;
1157 rasChannel = new H323TransportUDP(*endPoint);
1160 cout << " *** No RAS Channel, this is bad" << endl;
1163 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1164 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1166 cout << " *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
1168 /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1176 /** Send a DTMF tone over the H323Connection with the
1179 void h323_send_tone(const char *call_token, char tone)
1181 if (!h323_end_point_exist()) {
1182 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1186 PString token = PString(call_token);
1187 endPoint->SendUserTone(token, tone);
1190 /** Make a call to the remote endpoint.
1192 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
1198 if (!h323_end_point_exist()) {
1201 res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid, call_options.callername);
1202 memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1206 int h323_clear_call(const char *call_token)
1208 if (!h323_end_point_exist()) {
1212 endPoint->ClearCall(PString(call_token));
1216 /* Send Alerting PDU to H.323 caller */
1217 int h323_send_alerting(const char *token)
1219 const PString currentToken(token);
1220 H323Connection * connection;
1222 connection = endPoint->FindConnectionWithLock(currentToken);
1225 ast_verbose("\tSending alerting\n");
1228 cout << "No connection found for " << token << endl;
1232 connection->AnsweringCall(H323Connection::AnswerCallPending);
1233 connection->Unlock();
1239 /* Send Progress PDU to H.323 caller */
1240 int h323_send_progress(const char *token)
1242 const PString currentToken(token);
1243 H323Connection * connection;
1245 connection = endPoint->FindConnectionWithLock(currentToken);
1248 cout << "No connection found for " << token << endl;
1252 connection->AnsweringCall(H323Connection::AnswerCallDeferredWithMedia);
1253 connection->Unlock();
1258 /** This function tells the h.323 stack to either
1259 answer or deny an incoming call */
1260 int h323_answering_call(const char *token, int busy)
1262 const PString currentToken(token);
1263 H323Connection * connection;
1265 connection = endPoint->FindConnectionWithLock(currentToken);
1267 if (connection == NULL) {
1268 cout << "No connection found for " << token << endl;
1274 ast_verbose("\tanswering call\n");
1275 connection->AnsweringCall(H323Connection::AnswerCallNow);
1278 ast_verbose("\tdenying call\n");
1279 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1281 connection->Unlock();
1286 int h323_show_codec(int fd, int argc, char *argv[])
1288 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1292 int h323_soft_hangup(const char *data)
1294 PString token(data);
1297 result = endPoint->ClearCall(token);
1301 /* alas, this doesn't work :( */
1302 void h323_native_bridge(const char *token, const char *them, char *capability)
1304 H323Channel *channel;
1305 MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1308 cout << "ERROR: No connection found, this is bad\n";
1312 cout << "Native Bridge: them [" << them << "]" << endl;
1314 channel = connection->FindChannel(connection->sessionId, TRUE);
1315 connection->bridging = TRUE;
1316 connection->CloseLogicalChannelNumber(channel->GetNumber());
1318 connection->Unlock();
1323 /* set defalt h323 options */
1324 void h323_set_options(int nofs, int noh245tun) {
1326 noH245Tunneling = noh245tun;