4 * OpenH323 Channel Driver for ASTERISK PBX.
6 * For The NuFone Network
8 * This code 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;
63 /** Options for connections creation */
64 BOOL noFastStart = TRUE;
66 BOOL noSilenceSuppression;
69 * We assume that only one endPoint should exist.
70 * The application cannot run the h323_end_point_create() more than once
71 * FIXME: Singleton this, for safety
73 MyH323EndPoint *endPoint = NULL;
75 /** PWLib entry point */
76 MyProcess *localProcess = NULL;
79 MyProcess::MyProcess(): PProcess("The NuFone Network's", "H.323 Channel Driver for Asterisk",
80 MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
85 void MyProcess::Main()
87 ast_verbose(" == Creating H.323 Endpoint\n");
88 endPoint = new MyH323EndPoint();
89 PTrace::Initialise(0, NULL, PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
92 #define H323_NAME OPAL_G7231_6k3"{sw}"
93 #define H323_G729 OPAL_G729 "{sw}"
94 #define H323_G729A OPAL_G729A"{sw}"
96 H323_REGISTER_CAPABILITY(H323_G7231Capability, H323_NAME);
97 H323_REGISTER_CAPABILITY(AST_G729Capability, H323_G729);
98 H323_REGISTER_CAPABILITY(AST_G729ACapability, H323_G729A);
100 H323_G7231Capability::H323_G7231Capability(BOOL annexA_)
101 : H323AudioCapability(7, 4)
106 PObject::Comparison H323_G7231Capability::Compare(const PObject & obj) const
108 Comparison result = H323AudioCapability::Compare(obj);
109 if (result != EqualTo)
112 PINDEX otherAnnexA = ((const H323_G7231Capability &)obj).annexA;
113 if (annexA < otherAnnexA)
115 if (annexA > otherAnnexA)
120 PObject * H323_G7231Capability::Clone() const
122 return new H323_G7231Capability(*this);
126 PString H323_G7231Capability::GetFormatName() const
132 unsigned H323_G7231Capability::GetSubType() const
134 return H245_AudioCapability::e_g7231;
138 BOOL H323_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
139 unsigned packetSize) const
141 cap.SetTag(H245_AudioCapability::e_g7231);
143 H245_AudioCapability_g7231 & g7231 = cap;
144 g7231.m_maxAl_sduAudioFrames = packetSize;
145 g7231.m_silenceSuppression = annexA;
151 BOOL H323_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
152 unsigned & packetSize)
154 if (cap.GetTag() != H245_AudioCapability::e_g7231)
157 const H245_AudioCapability_g7231 & g7231 = cap;
158 packetSize = g7231.m_maxAl_sduAudioFrames;
159 annexA = g7231.m_silenceSuppression;
165 H323Codec * H323_G7231Capability::CreateCodec(H323Codec::Direction direction) const
171 /////////////////////////////////////////////////////////////////////////////
173 AST_G729Capability::AST_G729Capability()
174 : H323AudioCapability(24, 6)
179 PObject * AST_G729Capability::Clone() const
181 return new AST_G729Capability(*this);
185 unsigned AST_G729Capability::GetSubType() const
187 return H245_AudioCapability::e_g729;
191 PString AST_G729Capability::GetFormatName() const
197 H323Codec * AST_G729Capability::CreateCodec(H323Codec::Direction direction) const
201 /////////////////////////////////////////////////////////////////////////////
203 AST_G729ACapability::AST_G729ACapability()
204 : H323AudioCapability(24, 6)
209 PObject * AST_G729ACapability::Clone() const
211 return new AST_G729ACapability(*this);
215 unsigned AST_G729ACapability::GetSubType() const
217 return H245_AudioCapability::e_g729AnnexA;
221 PString AST_G729ACapability::GetFormatName() const
227 H323Codec * AST_G729ACapability::CreateCodec(H323Codec::Direction direction) const
233 * The fullAddress parameter is used directly in the MakeCall method so
234 * the General form for the fullAddress argument is :
235 * [alias@][transport$]host[:port]
236 * default values: alias = the same value as host.
240 int MyH323EndPoint::MakeCall(const PString & dest, PString & token, unsigned int *callReference, unsigned int port, char *callerid, char *callername)
243 MyH323Connection * connection;
245 /* Determine whether we are using a gatekeeper or not. */
246 if (GetGatekeeper() != NULL) {
249 cout << " -- Making call to " << fullAddress << " using gatekeeper." << endl;
251 fullAddress = dest; /* host */
253 cout << " -- Making call to " << fullAddress << "." << endl;
256 if (!(connection = (MyH323Connection *)H323EndPoint::MakeCallLocked(fullAddress, token))) {
258 cout << "Error making call to \"" << fullAddress << '"' << endl;
262 *callReference = connection->GetCallReference();
265 connection->SetLocalPartyName(PString(callerid));
267 localAliasNames.RemoveAll();
268 connection->SetLocalPartyName(PString(callername));
270 localAliasNames.AppendString(PString(callerid));
271 } else if (callerid) {
272 localAliasNames.RemoveAll();
273 connection->SetLocalPartyName(PString(callerid));
276 connection->AST_Outgoing = TRUE;
278 connection->Unlock();
281 cout << " -- " << GetLocalUserName() << " is calling host " << fullAddress << endl;
282 cout << " -- " << "Call token is " << (const char *)token << endl;
283 cout << " -- Call reference is " << *callReference << endl;
288 void MyH323EndPoint::SetEndpointTypeInfo( H225_EndpointType & info ) const
290 H323EndPoint::SetEndpointTypeInfo(info);
291 info.m_gateway.IncludeOptionalField(H225_GatewayInfo::e_protocol);
292 info.m_gateway.m_protocol.SetSize(1);
293 H225_SupportedProtocols &protocol=info.m_gateway.m_protocol[0];
294 protocol.SetTag(H225_SupportedProtocols::e_voice);
295 PINDEX as=SupportedPrefixes.GetSize();
296 ((H225_VoiceCaps &)protocol).m_supportedPrefixes.SetSize(as);
297 for (PINDEX p=0; p<as; p++) {
298 H323SetAliasAddress(SupportedPrefixes[p], ((H225_VoiceCaps &)protocol).m_supportedPrefixes[p].m_prefix);
302 void MyH323EndPoint::SetGateway(void)
304 terminalType = e_GatewayOnly;
307 H323Capabilities MyH323EndPoint::GetCapabilities(void)
312 BOOL MyH323EndPoint::ClearCall(const PString & token)
315 cout << " -- ClearCall: Request to clear call with token " << token << endl;
317 return H323EndPoint::ClearCall(token);
320 void MyH323EndPoint::SendUserTone(const PString &token, char tone)
322 H323Connection *connection = NULL;
324 connection = FindConnectionWithLock(token);
325 if (connection != NULL) {
326 connection->SendUserInputTone(tone, 500);
327 connection->Unlock();
331 void MyH323EndPoint::OnClosedLogicalChannel(H323Connection & connection, const H323Channel & channel)
335 cout << " channelsOpen = " << channelsOpen << endl;
336 H323EndPoint::OnClosedLogicalChannel(connection, channel);
339 BOOL MyH323EndPoint::OnConnectionForwarded(H323Connection & connection,
340 const PString & forwardParty,
341 const H323SignalPDU & pdu)
344 cout << " -- Call Forwarded to " << forwardParty << endl;
349 BOOL MyH323EndPoint::ForwardConnection(H323Connection & connection,
350 const PString & forwardParty,
351 const H323SignalPDU & pdu)
354 cout << " -- Forwarding call to " << forwardParty << endl;
356 return H323EndPoint::ForwardConnection(connection, forwardParty, pdu);
359 void MyH323EndPoint::OnConnectionEstablished(H323Connection & connection, const PString & estCallToken)
362 cout << "\t=-= In OnConnectionEstablished for call " << connection.GetCallReference() << endl;
363 cout << "\t\t-- Connection Established with \"" << connection.GetRemotePartyName() << "\"" << endl;
365 on_connection_established(connection.GetCallReference(), (const char *)connection.GetCallToken());
368 /** OnConnectionCleared callback function is called upon the dropping of an established
371 void MyH323EndPoint::OnConnectionCleared(H323Connection & connection, const PString & clearedCallToken)
375 PIPSocket::Address Ip;
378 remoteName = connection.GetRemotePartyName();
380 cd.call_reference = connection.GetCallReference();
381 cd.call_token = strdup((const char *)clearedCallToken);
382 cd.call_source_aliases = strdup((const char *)connection.GetRemotePartyName());
384 connection.GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
385 cd.sourceIp = strdup((const char *)Ip.AsString());
387 /* Convert complex strings */
389 if ((s = strchr(cd.call_source_aliases, ' ')) != NULL)
392 switch (connection.GetCallEndReason()) {
393 case H323Connection::EndedByCallForwarded :
395 cout << " -- " << remoteName << " has forwarded the call" << endl;
397 case H323Connection::EndedByRemoteUser :
399 cout << " -- " << remoteName << " has cleared the call" << endl;
401 case H323Connection::EndedByCallerAbort :
403 cout << " -- " << remoteName << " has stopped calling" << endl;
405 case H323Connection::EndedByRefusal :
407 cout << " -- " << remoteName << " did not accept your call" << endl;
409 case H323Connection::EndedByRemoteBusy :
411 cout << " -- " << remoteName << " was busy" << endl;
413 case H323Connection::EndedByRemoteCongestion :
415 cout << " -- Congested link to " << remoteName << endl;
417 case H323Connection::EndedByNoAnswer :
419 cout << " -- " << remoteName << " did not answer your call" << endl;
421 case H323Connection::EndedByTransportFail :
423 cout << " -- Call with " << remoteName << " ended abnormally" << endl;
425 case H323Connection::EndedByCapabilityExchange :
427 cout << " -- Could not find common codec with " << remoteName << endl;
429 case H323Connection::EndedByNoAccept :
431 cout << " -- Did not accept incoming call from " << remoteName << endl;
433 case H323Connection::EndedByAnswerDenied :
435 cout << " -- Refused incoming call from " << remoteName << endl;
437 case H323Connection::EndedByNoUser :
439 cout << " -- Remote endpoint could not find user: " << remoteName << endl;
441 case H323Connection::EndedByNoBandwidth :
443 cout << " -- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
445 case H323Connection::EndedByUnreachable :
447 cout << " -- " << remoteName << " could not be reached." << endl;
449 case H323Connection::EndedByHostOffline :
451 cout << " -- " << remoteName << " is not online." << endl;
453 case H323Connection::EndedByNoEndPoint :
455 cout << " -- No phone running for " << remoteName << endl;
457 case H323Connection::EndedByConnectFail :
459 cout << " -- Transport error calling " << remoteName << endl;
463 cout << " -- Call with " << remoteName << " completed (" << connection.GetCallEndReason() << ")" << endl;
467 if(connection.IsEstablished())
469 cout << " -- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
471 /* Invoke the PBX application registered callback */
472 on_connection_cleared(cd);
478 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *outbound)
480 unsigned options = 0;
483 options |= H323Connection::FastStartOptionDisable;
485 options |= H323Connection::FastStartOptionEnable;
488 options |= H323Connection::H245TunnelingOptionDisable;
490 options |= H323Connection::H245TunnelingOptionEnable;
492 return new MyH323Connection(*this, callReference, options);
495 /* MyH323Connection */
496 MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference,
498 : H323Connection(ep, callReference, options)
501 cout << " == New H.323 Connection created." << endl;
503 AST_RTP_Connected = FALSE;
504 AST_Outgoing = FALSE;
508 MyH323Connection::~MyH323Connection()
511 cout << " == H.323 Connection deleted." << endl;
516 H323Connection::AnswerCallResponse MyH323Connection::OnAnswerCall(const PString & caller,
517 const H323SignalPDU & /*setupPDU*/,
518 H323SignalPDU & /*connectPDU*/)
522 cout << "\t=-= In OnAnswerCall for call " << GetCallReference() << endl;
524 if (!on_answer_call(GetCallReference(), (const char *)GetCallToken()))
525 return H323Connection::AnswerCallDenied;
527 /* The call will be answered later with "AnsweringCall()" function.
529 return H323Connection::AnswerCallDeferred;
532 BOOL MyH323Connection::OnAlerting(const H323SignalPDU & /*alertingPDU*/, const PString & username)
534 PIPSocket::Address remoteIpAddress;
536 H323_ExternalRTPChannel * channel;
539 cout << "\t =-= In OnAlerting for call " << GetCallReference()
540 << ": sessionId=" << sessionId << endl;
542 /* Connect RTP if logical channel has already been opened */
544 if ( (channel = (H323_ExternalRTPChannel*) FindChannel(sessionId,TRUE)) ) {
545 channel->GetRemoteAddress(remoteIpAddress, remotePort);
547 cout << "\t\t--- found logical channel. Connecting RTP" << endl;
548 cout << "\t\tRTP channel id " << sessionId << " parameters:" << endl;
549 cout << "\t\t-- remoteIpAddress: " << remoteIpAddress << endl;
550 cout << "\t\t-- remotePort: " << remotePort << endl;
551 cout << "\t\t-- ExternalIpAddress: " << externalIpAddress << endl;
552 cout << "\t\t-- ExternalPort: " << externalPort << endl;
554 on_start_logical_channel(GetCallReference(),(const char *)remoteIpAddress.AsString(), remotePort,
555 (const char *)GetCallToken() );
556 AST_RTP_Connected=TRUE;
559 cout << "\t\t--- no logical channels" << endl;
562 cout << " -- Ringing phone for \"" << username << "\"" << endl;
565 on_chan_ringing(GetCallReference(), (const char *)GetCallToken() );
569 ast_log(LOG_ERROR,"chan_h323: OnAlerting: Could not obtain connection lock");
573 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
577 ast_verbose(" -- Received SETUP message\n");
584 PString sourceAliases;
586 PIPSocket::Address Ip;
590 sourceAliases = setupPDU.GetSourceAliases();
591 destAliases = setupPDU.GetDestinationAlias();
594 setupPDU.GetSourceE164(sourceE164);
596 sourceName=setupPDU.GetQ931().GetDisplayName();
598 setupPDU.GetDestinationE164(destE164);
600 /* Convert complex strings */
601 // FIXME: deal more than one source alias
602 if ((s = strchr(sourceAliases, ' ')) != NULL)
604 if ((s = strchr(sourceAliases, '\t')) != NULL)
606 if ((s1 = strchr(destAliases, ' ')) != NULL)
608 if ((s1 = strchr(destAliases, '\t')) != NULL)
612 cd.call_reference = GetCallReference();
614 cd.call_token = strdup((const char *)GetCallToken());
616 cd.call_source_aliases = strdup((const char *)sourceAliases);
617 cd.call_dest_alias = strdup((const char *)destAliases);
618 cd.call_source_e164 = strdup((const char *)sourceE164);
619 cd.call_dest_e164 = strdup((const char *)destE164);
620 cd.call_source_name = strdup((const char *)sourceName);
622 GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
623 cd.sourceIp = strdup((const char *)Ip.AsString());
625 /* Notify Asterisk of the request */
626 int res = on_incoming_call(cd);
630 cout << " -- Call Failed" << endl;
635 return H323Connection::OnReceivedSignalSetup(setupPDU);
638 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
644 cout << " -- Sending SETUP message" << endl;
646 sourceAliases = setupPDU.GetSourceAliases();
647 destAliases = setupPDU.GetDestinationAlias();
650 setupPDU.GetSourceE164(sourceE164);
652 setupPDU.GetDestinationE164(destE164);
654 /* Convert complex strings */
655 // FIXME: deal more than one source alias
657 if ((s = strchr(sourceAliases, ' ')) != NULL)
659 if ((s = strchr(sourceAliases, '\t')) != NULL)
661 if ((s1 = strchr(destAliases, ' ')) != NULL)
663 if ((s1 = strchr(destAliases, '\t')) != NULL)
666 cd.call_reference = GetCallReference();
668 cd.call_token = strdup((const char *)GetCallToken());
670 cd.call_source_aliases = strdup((const char *)sourceAliases);
671 cd.call_dest_alias = strdup((const char *)destAliases);
672 cd.call_source_e164 = strdup((const char *)sourceE164);
673 cd.call_dest_e164 = strdup((const char *)destE164);
675 int res = on_outgoing_call(cd);
679 cout << " -- Call Failed" << endl;
684 return H323Connection::OnSendSignalSetup(setupPDU);
687 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
690 cout << " -- Sending RELEASE COMPLETE" << endl;
692 return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
695 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
698 cout << " -- Received Facility message... " << endl;
700 return H323Connection::OnReceivedFacility(pdu);
703 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
706 cout << " -- Received RELEASE COMPLETE message..." << endl;
708 return H323Connection::OnReceivedReleaseComplete(pdu);
711 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
714 cout << " -- Closing logical channel..." << endl;
716 return H323Connection::OnClosingLogicalChannel(channel);
720 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
723 cout << " -- Sending user input tone (" << tone << ") to remote" << endl;
725 on_send_digit(GetCallReference(), tone);
726 H323Connection::SendUserInputTone(tone, duration);
729 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
731 if (mode == H323_DTMF_INBAND) {
733 cout << " -- Received user input tone (" << tone << ") from remote" << endl;
735 on_send_digit(GetCallReference(), tone);
737 H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
740 void MyH323Connection::OnUserInputString(const PString &value)
742 if (mode == H323_DTMF_RFC2833) {
744 cout << " -- Received user input string (" << value << ") from remote." << endl;
746 on_send_digit(GetCallReference(), value[0]);
750 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
751 H323Channel::Directions dir,
753 const H245_H2250LogicalChannelParameters * /*param*/)
755 struct rtp_info *info;
758 /* Determine the Local (A side) IP Address and port */
759 info = on_create_connection(GetCallReference(), (const char *)GetCallToken());
765 GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
766 externalPort = info->port;
767 sessionId = sessionID;
770 cout << " =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
771 cout << " -- externalIpAddress: " << externalIpAddress << endl;
772 cout << " -- externalPort: " << externalPort << endl;
773 cout << " -- SessionID: " << sessionID << endl;
774 cout << " -- Direction: " << dir << endl;
777 return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
780 /** This callback function is invoked once upon creation of each
781 * channel for an H323 session
783 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
785 PIPSocket::Address remoteIpAddress;
789 cout << " -- Started logical channel: ";
790 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));
791 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
794 /* adjust the count of channels we have open */
798 cout << " -- channelsOpen = " << channelsOpen << endl;
802 ast_log(LOG_ERROR,"chan_h323: OnStartLogicalChannel: Could not obtain connection lock");
805 /* Connect RTP for incoming calls */
807 H323_ExternalRTPChannel & external = (H323_ExternalRTPChannel &)channel;
808 external.GetRemoteAddress(remoteIpAddress, remotePort);
810 cout << "\t\tRTP channel id " << sessionId << " parameters:" << endl;
811 cout << "\t\t-- remoteIpAddress: " << remoteIpAddress << endl;
812 cout << "\t\t-- remotePort: " << remotePort << endl;
813 cout << "\t\t-- ExternalIpAddress: " << externalIpAddress << endl;
814 cout << "\t\t-- ExternalPort: " << externalPort << endl;
816 /* Notify Asterisk of remote RTP information */
817 on_start_logical_channel(GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort,
818 (const char *)GetCallToken());
819 AST_RTP_Connected = TRUE;
825 /* MyH323_ExternalRTPChannel */
826 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
827 const H323Capability & capability,
828 Directions direction,
830 const PIPSocket::Address & ip,
832 : H323_ExternalRTPChannel(connection, capability, direction, sessionID, ip, dataPort)
836 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
837 const H323Capability & capability,
838 Directions direction,
840 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id)
844 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
845 const H323Capability & capability,
846 Directions direction,
848 const H323TransportAddress & data,
849 const H323TransportAddress & control)
850 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id, data, control)
854 MyH323_ExternalRTPChannel::~MyH323_ExternalRTPChannel()
858 BOOL MyH323_ExternalRTPChannel::OnReceivedAckPDU(const H245_H2250LogicalChannelAckParameters & param)
860 PIPSocket::Address remoteIpAddress;
862 MyH323Connection* conn = (MyH323Connection*) &connection;
865 cout << "\t=-= In OnReceivedAckPDU for call " << connection.GetCallReference() << endl;
867 if (H323_ExternalRTPChannel::OnReceivedAckPDU(param)) {
868 if (!connection.Lock()) {
869 ast_log(LOG_ERROR,"chan_h323: OnReceivedAckPDU: Could not obtain connection lock");
872 /* if RTP hasn't been connected yet */
873 if (!conn->AST_RTP_Connected) {
874 H323_ExternalRTPChannel::GetRemoteAddress(remoteIpAddress, remotePort);
876 cout << "\t\tRTP channel id " << sessionID << " parameters:" << endl;
877 cout << "\t\t-- remoteIpAddress: " << remoteIpAddress << endl;
878 cout << "\t\t-- remotePort: " << remotePort << endl;
879 cout << "\t\t-- ExternalIpAddress: " << conn->externalIpAddress << endl;
880 cout << "\t\t-- ExternalPort: " << conn->externalPort << endl;
883 /* Notify Asterisk of remote RTP information */
884 on_start_logical_channel(connection.GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort,
885 (const char *)conn->GetCallToken());
886 conn->AST_RTP_Connected = TRUE;
893 /** IMPLEMENTATION OF C FUNCTIONS */
896 * The extern "C" directive takes care for
897 * the ANSI-C representation of linkable symbols
902 int h323_end_point_exist(void)
910 void h323_end_point_create(int no_fast_start, int no_h245_tunneling)
914 noFastStart = (BOOL)no_fast_start;
915 noH245Tunneling = (BOOL)no_h245_tunneling;
917 localProcess = new MyProcess();
918 localProcess->Main();
921 void h323_gk_urq(void)
923 if (!h323_end_point_exist()) {
924 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
927 endPoint->RemoveGatekeeper();
930 void h323_end_process(void)
932 endPoint->ClearAllCalls();
933 endPoint->RemoveListener(NULL);
938 void h323_debug(int flag, unsigned level)
941 PTrace:: SetLevel(level);
943 PTrace:: SetLevel(0);
947 /** Installs the callback functions on behalf of the PBX application */
948 void h323_callback_register(setup_incoming_cb ifunc,
949 setup_outbound_cb sfunc,
950 on_connection_cb confunc,
951 start_logchan_cb lfunc,
953 chan_ringing_cb rfunc,
954 con_established_cb efunc,
956 answer_call_cb acfunc)
958 on_incoming_call = ifunc;
959 on_outgoing_call = sfunc;
960 on_create_connection = confunc;
961 on_start_logical_channel = lfunc;
962 on_connection_cleared = clfunc;
963 on_chan_ringing = rfunc;
964 on_connection_established = efunc;
965 on_send_digit = dfunc;
966 on_answer_call = acfunc;
970 * Add capability to the capability table of the end point.
972 int h323_set_capability(int cap, int dtmfMode)
974 H323Capabilities oldcaps;
979 if (!h323_end_point_exist()) {
980 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
984 /* clean up old capabilities list before changing */
985 oldcaps = endPoint->GetCapabilities();
986 for (PINDEX i=0; i< oldcaps.GetSize(); i++) {
987 codecs.AppendString(oldcaps[i].GetFormatName());
989 endPoint->RemoveCapabilities(codecs);
992 if (dtmfMode == H323_DTMF_INBAND) {
993 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
995 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
997 if (cap & AST_FORMAT_SPEEX) {
998 /* Not real sure if Asterisk acutally supports all
999 of the various different bit rates so add them
1000 all and figure it out later*/
1002 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
1003 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
1004 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
1005 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
1006 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
1009 if (cap & AST_FORMAT_G729A) {
1010 AST_G729ACapability *g729aCap;
1011 AST_G729Capability *g729Cap;
1012 endPoint->SetCapability(0, 0, g729aCap = new AST_G729ACapability);
1013 endPoint->SetCapability(0, 0, g729Cap = new AST_G729Capability);
1016 if (cap & AST_FORMAT_G723_1) {
1017 H323_G7231Capability *g7231Cap;
1018 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
1021 if (cap & AST_FORMAT_GSM) {
1022 H323_GSM0610Capability *gsmCap;
1023 endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
1024 gsmCap->SetTxFramesInPacket(gsmFrames);
1027 if (cap & AST_FORMAT_ULAW) {
1028 H323_G711Capability *g711uCap;
1029 endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
1030 g711uCap->SetTxFramesInPacket(g711Frames);
1033 if (cap & AST_FORMAT_ALAW) {
1034 H323_G711Capability *g711aCap;
1035 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
1036 g711aCap->SetTxFramesInPacket(g711Frames);
1040 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1045 /** Start the H.323 listener */
1046 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
1049 if (!h323_end_point_exist()) {
1050 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
1054 PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
1060 /** H.323 listener */
1061 H323ListenerTCP *tcpListener;
1063 tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
1065 if (!endPoint->StartListener(tcpListener)) {
1066 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
1071 cout << " == H.323 listener started" << endl;
1077 int h323_set_alias(struct oh323_alias *alias)
1081 PString h323id(alias->name);
1082 PString e164(alias->e164);
1084 if (!h323_end_point_exist()) {
1085 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
1089 cout << " == Adding alias \"" << h323id << "\" to endpoint" << endl;
1090 endPoint->AddAliasName(h323id);
1091 endPoint->RemoveAliasName(localProcess->GetUserName());
1093 if (!e164.IsEmpty()) {
1094 cout << " == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
1095 endPoint->AddAliasName(e164);
1097 if (strlen(alias->prefix)) {
1099 num = strsep(&p, ",");
1101 cout << " == Adding Prefix \"" << num << "\" to endpoint" << endl;
1102 endPoint->SupportedPrefixes += PString(num);
1103 endPoint->SetGateway();
1104 num = strsep(&p, ",");
1112 void h323_set_id(char *id)
1117 cout << " == Using '" << h323id << "' as our H.323ID for this call" << endl;
1121 endPoint->SetLocalUserName(h323id);
1124 void h323_show_tokens(void)
1126 cout << "Current call tokens: " << setprecision(2) << endPoint->GetAllConnections() << endl;
1130 /** Establish Gatekeeper communiations, if so configured,
1131 * register aliases for the H.323 endpoint to respond to.
1133 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
1135 PString gkName = PString(gatekeeper);
1136 PString pass = PString(secret);
1137 H323TransportUDP *rasChannel;
1139 if (!h323_end_point_exist()) {
1140 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
1145 cout << "Error: Gatekeeper cannot be NULL" << endl;
1149 if (strlen(secret)) {
1150 endPoint->SetGatekeeperPassword(pass);
1153 if (gatekeeper_discover) {
1154 /* discover the gk using multicast */
1155 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
1156 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1158 cout << " *** Could not find a gatekeeper." << endl;
1162 rasChannel = new H323TransportUDP(*endPoint);
1165 cout << " *** No RAS Channel, this is bad" << endl;
1168 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1169 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1171 cout << " *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
1173 /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1181 /** Send a DTMF tone over the H323Connection with the
1184 void h323_send_tone(const char *call_token, char tone)
1186 if (!h323_end_point_exist()) {
1187 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1191 PString token = PString(call_token);
1192 endPoint->SendUserTone(token, tone);
1195 /** Make a call to the remote endpoint.
1197 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
1203 if (!h323_end_point_exist()) {
1207 noFastStart = call_options.noFastStart;
1208 noH245Tunneling = call_options.noH245Tunneling;
1210 res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid, call_options.callername);
1211 memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1216 int h323_clear_call(const char *call_token)
1218 if (!h323_end_point_exist()) {
1222 endPoint->ClearCall(PString(call_token));
1226 /* Send Alerting PDU to H.323 caller */
1227 int h323_send_alerting(const char *token)
1229 const PString currentToken(token);
1230 H323Connection * connection;
1232 connection = endPoint->FindConnectionWithLock(currentToken);
1235 ast_verbose("\tSending alerting\n");
1238 cout << "No connection found for " << token << endl;
1242 connection->AnsweringCall(H323Connection::AnswerCallPending);
1243 connection->Unlock();
1249 /* Send Progress PDU to H.323 caller */
1250 int h323_send_progress(const char *token)
1252 const PString currentToken(token);
1253 H323Connection * connection;
1255 connection = endPoint->FindConnectionWithLock(currentToken);
1258 cout << "No connection found for " << token << endl;
1262 connection->AnsweringCall(H323Connection::AnswerCallDeferredWithMedia);
1263 connection->Unlock();
1268 /** This function tells the h.323 stack to either
1269 answer or deny an incoming call */
1270 int h323_answering_call(const char *token, int busy)
1272 const PString currentToken(token);
1273 H323Connection * connection;
1275 connection = endPoint->FindConnectionWithLock(currentToken);
1277 if (connection == NULL) {
1278 cout << "No connection found for " << token << endl;
1284 ast_verbose("\tanswering call\n");
1285 connection->AnsweringCall(H323Connection::AnswerCallNow);
1288 ast_verbose("\tdenying call\n");
1289 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1291 connection->Unlock();
1296 int h323_show_codec(int fd, int argc, char *argv[])
1298 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1302 int h323_soft_hangup(const char *data)
1304 PString token(data);
1307 result = endPoint->ClearCall(token);
1311 /* alas, this doesn't work :( */
1312 void h323_native_bridge(const char *token, const char *them, char *capability)
1314 H323Channel *channel;
1315 MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1318 cout << "ERROR: No connection found, this is bad\n";
1322 cout << "Native Bridge: them [" << them << "]" << endl;
1324 channel = connection->FindChannel(connection->sessionId, TRUE);
1325 connection->bridging = TRUE;
1326 connection->CloseLogicalChannelNumber(channel->GetNumber());
1328 connection->Unlock();
1333 /* set defalt h323 options */
1334 void h323_set_options(int nofs, int noh245tun) {
1336 noH245Tunneling = noh245tun;