371511751cf32b0bca8e88f740ce6bc51b5c89c5
[asterisk/asterisk.git] / channels / h323 / ast_h323.cpp
1 /*
2  * ast_h323.cpp
3  *
4  * OpenH323 Channel Driver for ASTERISK PBX.
5  *                      By  Jeremy McNamara
6  *                      For The NuFone Network
7  * 
8  * chan_h323 has been derived from code created by
9  *               Michael Manousos and Mark Spencer
10  *
11  * This file is part of the chan_h323 driver for Asterisk
12  *
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. 
17  *
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. 
21  *
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. 
25  *
26  * Version Info: $Id$
27  */
28 #include <arpa/inet.h>
29
30 #include <list>
31 #include <string>
32 #include <algorithm>
33
34 #include <ptlib.h>
35 #include <h323.h>
36 #include <h323pdu.h>
37 #include <mediafmt.h>
38 #include <lid.h>
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif   
43 #include "asterisk/logger.h"
44 #ifdef __cplusplus
45 }
46 #endif
47
48 #include "chan_h323.h"
49 #include "ast_h323.h"
50
51 /* PWlib Required Components  */
52 #define MAJOR_VERSION 1
53 #define MINOR_VERSION 0
54 #define BUILD_TYPE    ReleaseCode
55 #define BUILD_NUMBER  0
56
57 /** Counter for the number of connections */
58 int channelsOpen;
59
60 /* DTMF Mode */
61 int mode = H323_DTMF_RFC2833;
62
63 /**
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
67  */
68 MyH323EndPoint *endPoint = NULL;
69
70 /** PWLib entry point */
71 MyProcess *localProcess = NULL;
72
73 class PAsteriskLog : public PObject, public iostream {
74         PCLASSINFO(PAsteriskLog, PObject);
75
76         public:
77         PAsteriskLog() : iostream(cout.rdbuf()) { init(&buffer); }
78         ~PAsteriskLog() { flush(); }
79
80         private:
81         PAsteriskLog(const PAsteriskLog &) : iostream(cout.rdbuf()) { }
82         PAsteriskLog & operator=(const PAsteriskLog &) { return *this; }
83
84         class Buffer : public streambuf {
85                 public:
86                 virtual int overflow(int=EOF);
87                 virtual int underflow();
88                 virtual int sync();
89                 PString string;
90         } buffer;
91         friend class Buffer;
92 };
93
94 static PAsteriskLog *logstream = NULL;
95
96 int PAsteriskLog::Buffer::overflow(int c)
97 {
98         if (pptr() >= epptr()) {
99                 int ppos = pptr() - pbase();
100                 char *newptr = string.GetPointer(string.GetSize() + 2000);
101                 setp(newptr, newptr + string.GetSize() - 1);
102                 pbump(ppos);
103         }
104         if (c != EOF) {
105                 *pptr() = (char)c;
106                 pbump(1);
107         }
108         return 0;
109 }
110
111 int PAsteriskLog::Buffer::underflow()
112 {
113         return EOF;
114 }
115
116 int PAsteriskLog::Buffer::sync()
117 {
118         char *str = strdup(string);
119         char *s, *s1;
120         char c;
121
122         /* Pass each line with different ast_verbose() call */
123         for (s = str; s && *s; s = s1) {
124                 s1 = strchr(s, '\n');
125                 if (!s1)
126                         s1 = s + strlen(s);
127                 else
128                         s1++;
129                 c = *s1;
130                 *s1 = '\0';
131                 ast_verbose("%s", s);
132                 *s1 = c;
133         }
134         free(str);
135
136         string = PString();
137         char *base = string.GetPointer(2000);
138         setp(base, base + string.GetSize() - 1);
139         return 0;
140 }
141
142 #define cout (*logstream)
143
144 MyProcess::MyProcess(): PProcess("The NuFone Network's", "H.323 Channel Driver for Asterisk",
145              MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
146 {
147         /* Fix missed one in PWLib */
148         PX_firstTimeStart = FALSE;
149         Resume();
150 }
151
152 void MyProcess::Main()
153 {
154         cout << "  == Creating H.323 Endpoint" << endl;
155         endPoint = new MyH323EndPoint();
156         PTrace::Initialise(0, NULL, PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
157         PTrace::SetStream(logstream);
158 }
159
160 void PAssertFunc(const char *msg)
161 {
162         ast_log(LOG_ERROR, "%s\n", msg);
163         /* XXX: Probably we need to crash here */
164 }
165
166 H323_REGISTER_CAPABILITY(H323_G7231Capability, OPAL_G7231);
167 H323_REGISTER_CAPABILITY(AST_G729Capability,  OPAL_G729);
168 H323_REGISTER_CAPABILITY(AST_G729ACapability, OPAL_G729A);
169
170 H323_G7231Capability::H323_G7231Capability(BOOL annexA_)
171   : H323AudioCapability(7, 4)
172 {
173         annexA = annexA_;
174 }
175
176 PObject::Comparison H323_G7231Capability::Compare(const PObject & obj) const
177 {
178         Comparison result = H323AudioCapability::Compare(obj);
179         if (result != EqualTo) {
180                 return result;
181         }
182         PINDEX otherAnnexA = ((const H323_G7231Capability &)obj).annexA;
183         if (annexA < otherAnnexA) {
184                 return LessThan;
185         }
186         if (annexA > otherAnnexA) {
187                 return GreaterThan;
188         }
189         return EqualTo;
190 }
191
192 PObject * H323_G7231Capability::Clone() const
193 {
194          return new H323_G7231Capability(*this);
195 }
196
197 PString H323_G7231Capability::GetFormatName() const
198 {
199         return OPAL_G7231;
200 }
201
202 unsigned H323_G7231Capability::GetSubType() const
203 {
204         return H245_AudioCapability::e_g7231;
205 }
206
207 BOOL H323_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
208                                           unsigned packetSize) const
209 {
210         cap.SetTag(H245_AudioCapability::e_g7231);
211         H245_AudioCapability_g7231 & g7231 = cap;
212         g7231.m_maxAl_sduAudioFrames = packetSize;
213         g7231.m_silenceSuppression = annexA;
214         return TRUE;
215 }
216
217 BOOL H323_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
218                                            unsigned & packetSize)
219 {
220         if (cap.GetTag() != H245_AudioCapability::e_g7231) {
221                 return FALSE;
222         }
223         const H245_AudioCapability_g7231 & g7231 = cap;
224         packetSize = g7231.m_maxAl_sduAudioFrames;
225         annexA = g7231.m_silenceSuppression;
226         return TRUE;
227 }
228
229 H323Codec * H323_G7231Capability::CreateCodec(H323Codec::Direction direction) const
230 {
231         return NULL;
232 }
233
234 AST_G729Capability::AST_G729Capability()
235   : H323AudioCapability(24, 2)
236 {
237 }
238
239 PObject * AST_G729Capability::Clone() const
240 {
241         return new AST_G729Capability(*this);
242 }
243
244 unsigned AST_G729Capability::GetSubType() const
245 {
246         return H245_AudioCapability::e_g729;
247 }
248
249 PString AST_G729Capability::GetFormatName() const
250 {
251         return OPAL_G729;
252 }
253
254 H323Codec * AST_G729Capability::CreateCodec(H323Codec::Direction direction) const
255 {
256         return NULL;
257 }
258
259 AST_G729ACapability::AST_G729ACapability()
260   : H323AudioCapability(24, 6)
261 {
262 }
263
264 PObject * AST_G729ACapability::Clone() const
265 {
266         return new AST_G729ACapability(*this);
267 }
268
269 unsigned AST_G729ACapability::GetSubType() const
270 {
271         return H245_AudioCapability::e_g729AnnexA;
272 }
273
274 PString AST_G729ACapability::GetFormatName() const
275 {
276         return OPAL_G729A;
277 }
278
279 H323Codec * AST_G729ACapability::CreateCodec(H323Codec::Direction direction) const
280 {
281         return NULL;
282 }
283
284 /** MyH323EndPoint 
285   */
286 MyH323EndPoint::MyH323EndPoint()
287                 : H323EndPoint()
288 {
289         // Capabilities will be negotiated on per-connection basis
290         capabilities.RemoveAll();
291 }
292
293 /** The fullAddress parameter is used directly in the MakeCall method so
294   * the General form for the fullAddress argument is :
295   * [alias@][transport$]host[:port]
296   * default values:     alias = the same value as host.
297   *                                     transport = ip.
298   *                                     port = 1720.
299   */
300 int MyH323EndPoint::MakeCall(const PString & dest, PString & token, unsigned int *callReference, call_options_t *opts)
301 {
302         PString fullAddress;
303         MyH323Connection * connection;
304
305         /* Determine whether we are using a gatekeeper or not. */
306         if (GetGatekeeper()) {
307                 fullAddress = dest;
308                 if (h323debug) {
309                         cout << " -- Making call to " << fullAddress << " using gatekeeper." << endl;
310                 }
311         } else {
312                 fullAddress = dest; 
313                 if (h323debug) {
314                         cout << " -- Making call to " << fullAddress << " without gatekeeper." << endl;
315                 }
316         }
317         if (!(connection = (MyH323Connection *)H323EndPoint::MakeCallLocked(fullAddress, token, opts))) {
318                 if (h323debug) {
319                         cout << "Error making call to \"" << fullAddress << '"' << endl;
320                 }
321                 return 1;
322         }
323         *callReference = connection->GetCallReference();        
324
325         if (opts->cid_num) {
326                 connection->ast_cid_num = PString(opts->cid_num);
327         }
328         if (opts->cid_name) {
329                 connection->ast_cid_name = PString(opts->cid_name);
330                 connection->SetLocalPartyName(connection->ast_cid_name);
331         }
332
333         connection->dtmfCodec = (RTP_DataFrame::PayloadTypes)opts->dtmfcodec;
334
335         if (h323debug) {
336                 cout << "\t-- " << GetLocalUserName() << " is calling host " << fullAddress << endl;
337                 cout << "\t-- Call token is " << (const char *)token << endl;
338                 cout << "\t-- Call reference is " << *callReference << endl;
339                 cout << "\t-- DTMF Payload is " << connection->dtmfCodec << endl;
340         }
341         connection->Unlock();   
342         return 0;
343 }
344
345 void MyH323EndPoint::SetEndpointTypeInfo( H225_EndpointType & info ) const
346 {
347         H323EndPoint::SetEndpointTypeInfo(info);
348
349         if (terminalType == e_GatewayOnly){
350                 info.RemoveOptionalField(H225_EndpointType::e_terminal);
351                 info.IncludeOptionalField(H225_EndpointType::e_gateway);
352         }
353
354         info.m_gateway.IncludeOptionalField(H225_GatewayInfo::e_protocol);
355         info.m_gateway.m_protocol.SetSize(1);
356         H225_SupportedProtocols &protocol=info.m_gateway.m_protocol[0];
357         protocol.SetTag(H225_SupportedProtocols::e_voice);
358         PINDEX as=SupportedPrefixes.GetSize();
359         ((H225_VoiceCaps &)protocol).m_supportedPrefixes.SetSize(as);
360         for (PINDEX p=0; p<as; p++) {
361                 H323SetAliasAddress(SupportedPrefixes[p], ((H225_VoiceCaps &)protocol).m_supportedPrefixes[p].m_prefix);
362         }
363 }
364
365 void MyH323EndPoint::SetGateway(void)
366 {
367         terminalType = e_GatewayOnly;
368 }
369
370 BOOL MyH323EndPoint::ClearCall(const PString & token, H323Connection::CallEndReason reason)
371 {
372         if (h323debug) {
373                 cout << "\t-- ClearCall: Request to clear call with token " << token << ", cause " << reason << endl;
374         }
375         return H323EndPoint::ClearCall(token, reason);
376 }
377
378 BOOL MyH323EndPoint::ClearCall(const PString & token)
379 {
380         if (h323debug) {
381                 cout << "\t-- ClearCall: Request to clear call with token " << token << endl;
382         }
383         return H323EndPoint::ClearCall(token, H323Connection::EndedByLocalUser);
384 }
385
386 void MyH323EndPoint::SendUserTone(const PString &token, char tone)
387 {
388         H323Connection *connection = NULL;
389                 
390         connection = FindConnectionWithLock(token);
391         if (connection != NULL) {
392                 connection->SendUserInputTone(tone, 500);
393                 connection->Unlock();
394         }
395 }
396
397 void MyH323EndPoint::OnClosedLogicalChannel(H323Connection & connection, const H323Channel & channel)
398 {
399         channelsOpen--;
400         if (h323debug) {
401                 cout << "\t\tchannelsOpen = " << channelsOpen << endl;
402         }
403         H323EndPoint::OnClosedLogicalChannel(connection, channel);
404 }
405
406 BOOL MyH323EndPoint::OnConnectionForwarded(H323Connection & connection,
407                 const PString & forwardParty,
408                 const H323SignalPDU & pdu)
409  {
410         if (h323debug) {
411                 cout << "\t-- Call Forwarded to " << forwardParty << endl;
412         }
413         return FALSE;
414  }
415  
416 BOOL MyH323EndPoint::ForwardConnection(H323Connection & connection,
417                 const PString & forwardParty,
418                 const H323SignalPDU & pdu)
419 {
420         if (h323debug) {
421                 cout << "\t-- Forwarding call to " << forwardParty << endl;
422         }
423         return H323EndPoint::ForwardConnection(connection, forwardParty, pdu);
424 }
425
426 void MyH323EndPoint::OnConnectionEstablished(H323Connection & connection, const PString & estCallToken)
427 {
428         if (h323debug) {
429                 cout << "\t=-= In OnConnectionEstablished for call " << connection.GetCallReference() << endl;
430                 cout << "\t\t-- Connection Established with \"" << connection.GetRemotePartyName() << "\"" << endl;
431         }
432         on_connection_established(connection.GetCallReference(), (const char *)connection.GetCallToken());
433 }
434
435 /** OnConnectionCleared callback function is called upon the dropping of an established
436   * H323 connection. 
437   */
438 void MyH323EndPoint::OnConnectionCleared(H323Connection & connection, const PString & clearedCallToken)
439 {
440         PString remoteName = connection.GetRemotePartyName();
441
442         switch (connection.GetCallEndReason()) {
443                 case H323Connection::EndedByCallForwarded:
444                         if (h323debug) {
445                                 cout << "-- " << remoteName << " has forwarded the call" << endl;
446                         }
447                         break;
448                 case H323Connection::EndedByRemoteUser:
449                         if (h323debug) {
450                                 cout << "-- " << remoteName << " has cleared the call" << endl;
451                         }
452                         break;
453                 case H323Connection::EndedByCallerAbort:
454                         if (h323debug) {
455                                 cout << "-- " << remoteName << " has stopped calling" << endl;
456                         }
457                         break;
458                 case H323Connection::EndedByRefusal:
459                         if (h323debug) {
460                                 cout << "-- " << remoteName << " did not accept your call" << endl;
461                         }
462                         break;
463                 case H323Connection::EndedByRemoteBusy:
464                         if (h323debug) {
465                                 cout << "-- " << remoteName << " was busy" << endl;
466                         }
467                         break;
468                 case H323Connection::EndedByRemoteCongestion:
469                         if (h323debug) {
470                                 cout << "-- Congested link to " << remoteName << endl;
471                         }
472                         break;
473                 case H323Connection::EndedByNoAnswer:
474                         if (h323debug) {
475                                 cout << "-- " << remoteName << " did not answer your call" << endl;
476                         }
477                         break;
478                 case H323Connection::EndedByTransportFail:
479                         if (h323debug) {
480                                 cout << "-- Call with " << remoteName << " ended abnormally" << endl;
481                         }
482                         break;
483                 case H323Connection::EndedByCapabilityExchange:
484                         if (h323debug) {
485                                 cout << "-- Could not find common codec with " << remoteName << endl;
486                         }
487                         break;
488                 case H323Connection::EndedByNoAccept:
489                         if (h323debug) {
490                                 cout << "-- Did not accept incoming call from " << remoteName << endl;
491                         }
492                         break;
493                 case H323Connection::EndedByAnswerDenied:
494                         if (h323debug) {
495                                 cout << "-- Refused incoming call from " << remoteName << endl;
496                         }
497                         break;
498                 case H323Connection::EndedByNoUser:
499                         if (h323debug) {
500                                 cout << "-- Remote endpoint could not find user: " << remoteName << endl;
501                         }
502                         break;
503                 case H323Connection::EndedByNoBandwidth:
504                         if (h323debug) {
505                                 cout << "-- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
506                         }
507                         break;
508                 case H323Connection::EndedByUnreachable:
509                         if (h323debug) {
510                                 cout << "-- " << remoteName << " could not be reached." << endl;
511                         }
512                         break;
513                 case H323Connection::EndedByHostOffline:
514                         if (h323debug) {
515                                 cout << "-- " << remoteName << " is not online." << endl;
516                         }
517                         break;
518                 case H323Connection::EndedByNoEndPoint:
519                         if (h323debug) {
520                                 cout << "-- No phone running for " << remoteName << endl;
521                         }
522                         break;
523                 case H323Connection::EndedByConnectFail:
524                         if (h323debug) {
525                                 cout << "-- Transport error calling " << remoteName << endl;
526                         }
527                         break;
528                 default:
529                         if (h323debug)
530                                 cout << " -- Call with " << remoteName << " completed (" << connection.GetCallEndReason() << ")" << endl;
531
532         }
533
534         if (connection.IsEstablished()) {
535                 if (h323debug) {
536                         cout << "\t-- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
537                 }
538         }       
539         /* Invoke the PBX application registered callback */
540         on_connection_cleared(connection.GetCallReference(), clearedCallToken);
541         return;
542 }
543
544 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *o)
545 {
546         unsigned options = 0;
547         call_options_t *opts = (call_options_t *)o;
548
549         if (opts && opts->noFastStart) {
550                 options |= H323Connection::FastStartOptionDisable;
551         } else {
552                 options |= H323Connection::FastStartOptionEnable;
553         }
554         if (opts && opts->noH245Tunneling) {
555                 options |= H323Connection::H245TunnelingOptionDisable;
556         } else {
557                 options |= H323Connection::H245TunnelingOptionEnable;
558         }
559 /* Disable until I can figure out the proper way to deal with this */
560 #if 0
561         if (opts->noSilenceSuppression) {
562                 options |= H323Connection::SilenceSuppresionOptionDisable;
563         } else {
564                 options |= H323Connection::SilenceSUppressionOptionEnable;
565         }
566 #endif
567         return new MyH323Connection(*this, callReference, options);
568 }
569
570 /* MyH323Connection Implementation */    
571 MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference,
572                                                         unsigned options)
573         : H323Connection(ep, callReference, options)
574 {
575         cause = -1;
576         if (h323debug) {
577                 cout << "       == New H.323 Connection created." << endl;
578         }
579         return;
580 }
581
582 MyH323Connection::~MyH323Connection()
583 {
584         if (h323debug) {
585                 cout << "       == H.323 Connection deleted." << endl;
586         }
587         return;
588 }
589
590 BOOL MyH323Connection::OnReceivedProgress(const H323SignalPDU &pdu)
591 {
592         BOOL isInband;
593         unsigned pi;
594
595         if (!H323Connection::OnReceivedProgress(pdu)) {
596                 return FALSE;
597         }
598
599         if (!pdu.GetQ931().GetProgressIndicator(pi))
600                 pi = 0;
601         if (h323debug) {
602                 cout << "\t- Progress Indicator: " << pi << endl;
603         }
604         
605         switch(pi) {
606         case Q931::ProgressNotEndToEndISDN:
607         case Q931::ProgressInbandInformationAvailable:
608                 isInband = TRUE;
609                 break;
610         default:
611                 isInband = FALSE;
612         }
613         on_progress(GetCallReference(), (const char *)GetCallToken(), isInband);
614
615         return connectionState != ShuttingDownConnection;
616 }
617
618 H323Connection::AnswerCallResponse MyH323Connection::OnAnswerCall(const PString & caller,
619                                                                   const H323SignalPDU & setupPDU,
620                                                                   H323SignalPDU & /*connectPDU*/)
621 {
622         unsigned pi;
623
624         if (h323debug) {
625                cout << "\t=-= In OnAnswerCall for call " << GetCallReference() << endl;
626         }
627
628         if (connectionState == ShuttingDownConnection)
629                 return H323Connection::AnswerCallDenied;
630
631         if (!setupPDU.GetQ931().GetProgressIndicator(pi)) {
632                 pi = 0;
633         }
634         if (h323debug) {
635                 cout << "\t\t- Progress Indicator: " << pi << endl;
636         }
637         if (progressAlert) {
638                 pi = progressAlert;
639         } else if (pi == Q931::ProgressOriginNotISDN) {
640                 pi = Q931::ProgressInbandInformationAvailable;
641         }
642         if (pi) {
643                 alertingPDU->GetQ931().SetProgressIndicator(pi);
644         }
645         if (h323debug) {
646                 cout << "\t\t- Inserting PI of " << pi << " into ALERTING message" << endl;
647         }
648
649         if (!on_answer_call(GetCallReference(), (const char *)GetCallToken())) {
650                 return H323Connection::AnswerCallDenied;
651         }
652         /* The call will be answered later with "AnsweringCall()" function.
653          */ 
654         return H323Connection::AnswerCallDeferredWithMedia;
655 }
656
657 BOOL MyH323Connection::OnAlerting(const H323SignalPDU & alertingPDU, const PString & username)
658 {
659         if (h323debug) {
660                 cout << "\t=-= In OnAlerting for call " << GetCallReference()
661                       << ": sessionId=" << sessionId << endl;
662                  cout << "\t-- Ringing phone for \"" << username << "\"" << endl;
663         }
664
665         if (on_progress) {
666                 BOOL isInband;
667                 unsigned alertingPI;
668
669                 if (!alertingPDU.GetQ931().GetProgressIndicator(alertingPI)) {
670                         alertingPI = 0;
671                 }
672                 if (h323debug) {
673                         cout << "\t\t- Progress Indicator: " << alertingPI << endl;
674                 }
675                 
676                 switch(alertingPI) {
677                 case Q931::ProgressNotEndToEndISDN:
678                 case Q931::ProgressInbandInformationAvailable:
679                         isInband = TRUE;
680                         break;
681                 default:
682                         isInband = FALSE;
683                 }
684                 on_progress(GetCallReference(), (const char *)GetCallToken(), isInband);
685         }
686         on_chan_ringing(GetCallReference(), (const char *)GetCallToken() );
687         return connectionState != ShuttingDownConnection;
688 }
689
690 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
691 {
692         call_details_t cd;
693         PString sourceE164;
694         PString destE164;
695         PString sourceName;
696         PString sourceAliases;  
697         PString destAliases;
698         PIPSocket::Address Ip;
699         WORD sourcePort;
700         char *s, *s1; 
701
702         if (h323debug) {
703                 cout << ("\t--Received SETUP message\n");
704         }
705
706         if (connectionState == ShuttingDownConnection)
707                 return FALSE;
708
709         sourceAliases = setupPDU.GetSourceAliases();
710         destAliases = setupPDU.GetDestinationAlias();
711                         
712         sourceE164 = "";
713         setupPDU.GetSourceE164(sourceE164);
714         sourceName = "";
715         sourceName=setupPDU.GetQ931().GetDisplayName();
716         destE164 = "";
717         setupPDU.GetDestinationE164(destE164);
718
719         /* Convert complex strings */
720         //  FIXME: deal more than one source alias 
721         if ((s = strchr(sourceAliases, ' ')) != NULL) {
722                 *s = '\0';
723         }
724         if ((s = strchr(sourceAliases, '\t')) != NULL) {
725                 *s = '\0';
726         }
727         if ((s1 = strchr(destAliases, ' ')) != NULL) {
728                 *s1 = '\0';
729         }
730         if ((s1 = strchr(destAliases, '\t')) != NULL) {
731                 *s1 = '\0';
732         }
733
734         memset(&cd, 0, sizeof(cd));
735         cd.call_reference = GetCallReference();
736         cd.call_token = strdup((const char *)GetCallToken());
737         cd.call_source_aliases = strdup((const char *)sourceAliases);
738         cd.call_dest_alias = strdup((const char *)destAliases);
739         cd.call_source_e164 = strdup((const char *)sourceE164);
740         cd.call_dest_e164 = strdup((const char *)destE164);
741         cd.call_source_name = strdup((const char *)sourceName);
742
743         GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
744         cd.sourceIp = strdup((const char *)Ip.AsString());
745
746         /* Notify Asterisk of the request */
747         call_options_t *res = on_incoming_call(&cd);
748
749         if (!res) {
750                 if (h323debug) {
751                         cout << "       -- Call Failed" << endl;
752                 }
753                 return FALSE;
754         }
755
756         progressSetup = res->progress_setup;
757         progressAlert = res->progress_alert;
758         dtmfCodec = (RTP_DataFrame::PayloadTypes)res->dtmfcodec;
759
760
761         return H323Connection::OnReceivedSignalSetup(setupPDU);
762 }
763
764 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
765 {
766         call_details_t cd;
767         char *s, *s1;
768
769         if (h323debug) { 
770                 cout << "       -- Sending SETUP message" << endl;
771         }
772
773         if (connectionState == ShuttingDownConnection)
774                 return FALSE;
775
776         if (!ast_cid_num.IsEmpty()) {
777                 setupPDU.GetQ931().SetCallingPartyNumber(ast_cid_num);
778         }
779
780         if (!ast_cid_name.IsEmpty()) {
781                 setupPDU.GetQ931().SetDisplayName(ast_cid_name);
782         }
783
784         sourceAliases = setupPDU.GetSourceAliases();
785         destAliases = setupPDU.GetDestinationAlias();
786
787         sourceE164 = "";
788         setupPDU.GetSourceE164(sourceE164);
789         destE164 = "";
790         setupPDU.GetDestinationE164(destE164);
791
792         /* Convert complex strings */
793         //  FIXME: deal more than one source alias 
794         
795         if ((s = strchr(sourceAliases, ' ')) != NULL) {
796                 *s = '\0';
797         }
798         if ((s = strchr(sourceAliases, '\t')) != NULL) {
799                 *s = '\0';
800         }
801         if ((s1 = strchr(destAliases, ' ')) != NULL) {
802                  *s1 = '\0';
803         }
804         if ((s1 = strchr(destAliases, '\t')) != NULL) {
805                 *s1 = '\0';
806         }
807
808         memset(&cd, 0, sizeof(cd));
809         cd.call_reference = GetCallReference();
810         cd.call_token = strdup((const char *)GetCallToken());
811         cd.call_source_aliases = strdup((const char *)sourceAliases);
812         cd.call_dest_alias = strdup((const char *)destAliases);
813         cd.call_source_e164 = strdup((const char *)sourceE164);
814         cd.call_dest_e164 = strdup((const char *)destE164);
815
816         int res = on_outgoing_call(&cd);
817         if (!res) {
818                 if (h323debug) {
819                         cout << "\t-- Call Failed" << endl;
820                 }
821                 return FALSE;
822         }
823
824         if (progressSetup) {
825                 setupPDU.GetQ931().SetProgressIndicator(progressSetup);
826         }
827         return H323Connection::OnSendSignalSetup(setupPDU);
828 }
829
830 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
831 {
832         if (h323debug) {
833                 cout << "\t-- Sending RELEASE COMPLETE" << endl;
834         }
835         if (cause > 0)
836                 releaseCompletePDU.GetQ931().SetCause((Q931::CauseValues)cause);
837         return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
838 }
839
840 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
841 {
842         if (h323debug) {
843                 cout << "\t-- Received Facility message... " << endl;
844         }       
845         return H323Connection::OnReceivedFacility(pdu);
846 }
847
848 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
849 {
850         if (h323debug) {
851                 cout <<  "\t-- Received RELEASE COMPLETE message..." << endl;
852         }
853         if (on_hangup)
854                 on_hangup(GetCallReference(), (const char *)GetCallToken(), pdu.GetQ931().GetCause());
855         return H323Connection::OnReceivedReleaseComplete(pdu);
856 }
857
858 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
859 {
860         if (h323debug) {
861                 cout << "\t-- Closing logical channel..." << endl;
862         }
863         return H323Connection::OnClosingLogicalChannel(channel);
864 }
865
866 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
867 {
868         if (h323debug) {
869                 cout << "\t-- Sending user input tone (" << tone << ") to remote" << endl;
870         }
871         on_send_digit(GetCallReference(), tone, (const char *)GetCallToken());  
872         H323Connection::SendUserInputTone(tone, duration);
873 }
874
875 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
876 {
877         if (mode == H323_DTMF_INBAND) {
878                 if (h323debug) {
879                         cout << "\t-- Received user input tone (" << tone << ") from remote" << endl;
880                 }
881                 on_send_digit(GetCallReference(), tone, (const char *)GetCallToken());
882         }
883         H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
884 }
885
886 void MyH323Connection::OnUserInputString(const PString &value)
887 {
888         if (mode == H323_DTMF_RFC2833) {
889                 if (h323debug) {
890                         cout <<  "\t-- Received user input string (" << value << ") from remote." << endl;
891                 }
892                 on_send_digit(GetCallReference(), value[0], (const char *)GetCallToken());
893         }       
894 }
895
896 void MyH323Connection::OnSendCapabilitySet(H245_TerminalCapabilitySet & pdu)
897 {
898         PINDEX i;
899
900         H323Connection::OnSendCapabilitySet(pdu);
901
902         H245_ArrayOf_CapabilityTableEntry & tables = pdu.m_capabilityTable;
903         for(i = 0; i < tables.GetSize(); i++)
904         {
905                 H245_CapabilityTableEntry & entry = tables[i];
906                 if (entry.HasOptionalField(H245_CapabilityTableEntry::e_capability)) {
907                         H245_Capability & cap = entry.m_capability;
908                         if (cap.GetTag() == H245_Capability::e_receiveRTPAudioTelephonyEventCapability) {
909                                 H245_AudioTelephonyEventCapability & atec = cap;
910                                 atec.m_dynamicRTPPayloadType = dtmfCodec;
911                                 on_set_rfc2833_payload(GetCallReference(), (const char *)GetCallToken(), (int)dtmfCodec);
912                                 if (h323debug) {
913                                         cout << "\t-- Transmitting RFC2833 on payload " <<
914                                                 atec.m_dynamicRTPPayloadType << endl;
915                                 }
916                         }
917                 }
918         }
919 }
920
921 void MyH323Connection::OnSetLocalCapabilities()
922 {
923         if (on_setcapabilities)
924                 on_setcapabilities(GetCallReference(), (const char *)callToken);
925 }
926
927 BOOL MyH323Connection::OnReceivedCapabilitySet(const H323Capabilities & remoteCaps,
928                                                const H245_MultiplexCapability * muxCap,
929                                                H245_TerminalCapabilitySetReject & reject)
930 {
931         if (!H323Connection::OnReceivedCapabilitySet(remoteCaps, muxCap, reject)) {
932                 return FALSE;
933         }
934
935         const H323Capability * cap = remoteCaps.FindCapability(H323_UserInputCapability::SubTypeNames[H323_UserInputCapability::SignalToneRFC2833]);
936         if (cap != NULL) {
937                 RTP_DataFrame::PayloadTypes pt = ((H323_UserInputCapability*)cap)->GetPayloadType();
938                 on_set_rfc2833_payload(GetCallReference(), (const char *)GetCallToken(), (int)pt);
939                 if (h323debug) {
940                         cout << "\t-- Inbound RFC2833 on payload " << pt << endl;
941                 }
942         }
943         return TRUE;
944 }
945
946 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability, 
947                                                                    H323Channel::Directions dir,
948                                                                    unsigned sessionID,
949                                                                    const H245_H2250LogicalChannelParameters * /*param*/,
950                                                                    RTP_QOS * /*param*/ )
951 {
952         return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID);
953 }
954
955 /** This callback function is invoked once upon creation of each
956   * channel for an H323 session 
957   */
958 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
959 {    
960         /* Increase the count of channels we have open */
961         channelsOpen++;
962
963         if (h323debug) {
964                 cout << "\t-- Started logical channel: ";
965                 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));
966                 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
967                 cout <<  "\t\t-- channelsOpen = " << channelsOpen << endl;
968         }
969         return connectionState != ShuttingDownConnection;
970 }
971
972 void MyH323Connection::SetCapabilities(int cap, int dtmfMode)
973 {
974         int g711Frames = 20;
975 //      int gsmFrames  = 4;
976         PINDEX lastcap = -1; /* last common capability index */
977
978 #if 0
979         if (cap & AST_FORMAT_SPEEX) {
980                 /* Not real sure if Asterisk acutally supports all
981                    of the various different bit rates so add them 
982                    all and figure it out later*/
983
984                 localCapabilities.SetCapability(0, 0, new SpeexNarrow2AudioCapability());
985                 localCapabilities.SetCapability(0, 0, new SpeexNarrow3AudioCapability());
986                 localCapabilities.SetCapability(0, 0, new SpeexNarrow4AudioCapability());
987                 localCapabilities.SetCapability(0, 0, new SpeexNarrow5AudioCapability());
988                 localCapabilities.SetCapability(0, 0, new SpeexNarrow6AudioCapability());
989         }
990 #endif 
991         if (cap & AST_FORMAT_G729A) {
992                 AST_G729ACapability *g729aCap;
993                 AST_G729Capability *g729Cap;
994                 lastcap = localCapabilities.SetCapability(0, 0, g729aCap = new AST_G729ACapability);
995                 lastcap = localCapabilities.SetCapability(0, 0, g729Cap = new AST_G729Capability);
996         }
997         
998         if (cap & AST_FORMAT_G723_1) {
999                 H323_G7231Capability *g7231Cap;
1000                 lastcap = localCapabilities.SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
1001         } 
1002 #if 0
1003         if (cap & AST_FORMAT_GSM) {
1004                 H323_GSM0610Capability *gsmCap;
1005                 lastcap = localCapabilities.SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
1006                 gsmCap->SetTxFramesInPacket(gsmFrames);
1007         } 
1008 #endif
1009         if (cap & AST_FORMAT_ULAW) {
1010                 H323_G711Capability *g711uCap;
1011                 lastcap = localCapabilities.SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
1012                 g711uCap->SetTxFramesInPacket(g711Frames);
1013         } 
1014
1015         if (cap & AST_FORMAT_ALAW) {
1016                 H323_G711Capability *g711aCap;
1017                 lastcap = localCapabilities.SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
1018                 g711aCap->SetTxFramesInPacket(g711Frames);
1019         }
1020
1021         lastcap++;
1022         lastcap = localCapabilities.SetCapability(0, lastcap, new H323_UserInputCapability(H323_UserInputCapability::HookFlashH245));
1023
1024         lastcap++;
1025         mode = dtmfMode;
1026         if (dtmfMode == H323_DTMF_INBAND) {
1027                 localCapabilities.SetCapability(0, lastcap, new H323_UserInputCapability(H323_UserInputCapability::SignalToneH245));
1028                 sendUserInputMode = SendUserInputAsTone;
1029         } else {
1030                 localCapabilities.SetCapability(0, lastcap, new H323_UserInputCapability(H323_UserInputCapability::SignalToneRFC2833));
1031                 sendUserInputMode = SendUserInputAsInlineRFC2833;
1032         }
1033
1034         if (h323debug) {
1035                 cout <<  "Allowed Codecs:\n\t" << setprecision(2) << localCapabilities << endl;
1036         }
1037 }
1038
1039 /* MyH323_ExternalRTPChannel */
1040 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
1041                                                      const H323Capability & capability,
1042                                                      Directions direction,
1043                                                      unsigned id)
1044  : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id)
1045 {   
1046         struct rtp_info *info;
1047
1048         /* Determine the Local (A side) IP Address and port */
1049         info = on_external_rtp_create(connection.GetCallReference(), (const char *)connection.GetCallToken()); 
1050         if (!info) {
1051                 cout << "\tERROR: on_external_rtp_create failure" << endl;
1052                 return;
1053         } else {
1054                 localIpAddr = info->addr;
1055                 localPort = info->port;
1056                 /* tell the H.323 stack  */ 
1057                 SetExternalAddress(H323TransportAddress(localIpAddr, localPort), H323TransportAddress(localIpAddr, localPort + 1));
1058                 /* clean up allocated memory */
1059                 free(info);
1060         }
1061
1062         /* Get the payload code */
1063         OpalMediaFormat format(capability.GetFormatName(), FALSE);
1064         payloadCode = format.GetPayloadType();
1065
1066
1067 MyH323_ExternalRTPChannel::~MyH323_ExternalRTPChannel() 
1068 {
1069         if (h323debug) {
1070                 cout << "\tExternalRTPChannel Destroyed" << endl;
1071         }
1072 }
1073
1074 BOOL MyH323_ExternalRTPChannel::Start(void)
1075 {
1076         /* Call ancestor first */
1077         if (!H323_ExternalRTPChannel::Start()) {
1078                 return FALSE;
1079         }
1080
1081         if (h323debug) {
1082                 cout << "\t\tExternal RTP Session Starting" << endl;
1083                 cout << "\t\tRTP channel id " << sessionID << " parameters:" << endl;
1084         }
1085
1086         /* Update RTP parameters by outgoing voice path only */
1087         if (GetDirection() == IsTransmitter) {
1088                 /* Collect the remote information */
1089                 H323_ExternalRTPChannel::GetRemoteAddress(remoteIpAddr, remotePort);
1090
1091                 if (h323debug) {
1092                         cout << "\t\t-- remoteIpAddress: " << remoteIpAddr << endl;
1093                         cout << "\t\t-- remotePort: " << remotePort << endl;
1094                         cout << "\t\t-- ExternalIpAddress: " <<  localIpAddr << endl;
1095                         cout << "\t\t-- ExternalPort: " << localPort << endl;
1096                 }
1097
1098                 /* Notify Asterisk of remote RTP information */
1099                 on_start_rtp_channel(connection.GetCallReference(), (const char *)remoteIpAddr.AsString(), remotePort, 
1100                         (const char *)connection.GetCallToken(), (int)payloadCode);
1101         }
1102         return TRUE;
1103 }
1104
1105 BOOL MyH323_ExternalRTPChannel::OnReceivedAckPDU(const H245_H2250LogicalChannelAckParameters & param)
1106 {
1107         PIPSocket::Address remoteIpAddress;
1108         WORD remotePort;
1109
1110         if (h323debug) {
1111                 cout << "       MyH323_ExternalRTPChannel::OnReceivedAckPDU" << endl;
1112         }
1113
1114         if (H323_ExternalRTPChannel::OnReceivedAckPDU(param)) {
1115                 GetRemoteAddress(remoteIpAddress, remotePort);
1116                 if (h323debug) {
1117                         cout << "               -- remoteIpAddress: " << remoteIpAddress << endl;
1118                         cout << "               -- remotePort: " << remotePort << endl;
1119                 }
1120                 on_start_rtp_channel(connection.GetCallReference(), (const char *)remoteIpAddress.AsString(),
1121                                 remotePort, (const char *)connection.GetCallToken(), (int)payloadCode);
1122                 return TRUE;
1123         }
1124         return FALSE;
1125 }
1126
1127 /** IMPLEMENTATION OF C FUNCTIONS */
1128
1129 /**
1130  * The extern "C" directive takes care for 
1131  * the ANSI-C representation of linkable symbols 
1132  */
1133
1134 extern "C" {
1135
1136 int h323_end_point_exist(void)
1137 {
1138         if (!endPoint) {
1139                 return 0;
1140         }
1141         return 1;
1142 }
1143     
1144 void h323_end_point_create(void)
1145 {
1146         channelsOpen = 0;
1147         logstream = new PAsteriskLog();
1148         localProcess = new MyProcess(); 
1149         localProcess->Main();
1150 }
1151
1152 void h323_gk_urq(void)
1153 {
1154         if (!h323_end_point_exist()) {
1155                 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
1156                 return;
1157         }       
1158         endPoint->RemoveGatekeeper();
1159 }
1160
1161 void h323_debug(int flag, unsigned level)
1162 {
1163         if (flag) {
1164                 PTrace:: SetLevel(level); 
1165         } else { 
1166                 PTrace:: SetLevel(0); 
1167         }
1168 }
1169         
1170 /** Installs the callback functions on behalf of the PBX application  */
1171 void h323_callback_register(setup_incoming_cb   ifunc,
1172                             setup_outbound_cb   sfunc,
1173                             on_rtp_cb           rtpfunc,
1174                             start_rtp_cb        lfunc,
1175                             clear_con_cb        clfunc,
1176                             chan_ringing_cb     rfunc,
1177                             con_established_cb  efunc,
1178                             send_digit_cb       dfunc,
1179                             answer_call_cb      acfunc,
1180                             progress_cb         pgfunc,
1181                             rfc2833_cb          dtmffunc,
1182                             hangup_cb           hangupfunc,
1183                             setcapabilities_cb  capabilityfunc)
1184 {
1185         on_incoming_call = ifunc;
1186         on_outgoing_call = sfunc;
1187         on_external_rtp_create = rtpfunc;
1188         on_start_rtp_channel = lfunc;
1189         on_connection_cleared = clfunc;
1190         on_chan_ringing = rfunc;
1191         on_connection_established = efunc;
1192         on_send_digit = dfunc;
1193         on_answer_call = acfunc;
1194         on_progress = pgfunc;
1195         on_set_rfc2833_payload = dtmffunc;
1196         on_hangup = hangupfunc;
1197         on_setcapabilities = capabilityfunc;
1198 }
1199
1200 /**
1201  * Add capability to the capability table of the end point. 
1202  */
1203 int h323_set_capabilities(const char *token, int cap, int dtmfMode)
1204 {
1205         MyH323Connection *conn;
1206
1207         if (!h323_end_point_exist()) {
1208                 cout << " ERROR: [h323_set_capablities] No Endpoint, this is bad" << endl;
1209                 return 1;
1210         }
1211         if (!token || !*token) {
1212                 cout << " ERROR: [h323_set_capabilities] Invalid call token specified." << endl;
1213                 return 1;
1214         }
1215
1216         PString myToken(token);
1217         conn = (MyH323Connection *)endPoint->FindConnectionWithLock(myToken);
1218         if (!conn) {
1219                 cout << " ERROR: [h323_set_capabilities] Unable to find connection " << token << endl;
1220                 return 1;
1221         }
1222         conn->SetCapabilities(cap, dtmfMode);
1223         conn->Unlock();
1224
1225         return 0;
1226 }
1227
1228 /** Start the H.323 listener */
1229 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
1230 {
1231         
1232         if (!h323_end_point_exist()) {
1233                 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
1234                 return 1;
1235         }
1236         
1237         PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
1238         if (!listenPort) {
1239                 listenPort = 1720;
1240         }
1241         /** H.323 listener */  
1242         H323ListenerTCP *tcpListener;
1243         tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
1244         if (!endPoint->StartListener(tcpListener)) {
1245                 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
1246                 delete tcpListener;
1247                 return 1;
1248                 
1249         }
1250         cout << "  == H.323 listener started" << endl;
1251         return 0;
1252 };
1253  
1254 int h323_set_alias(struct oh323_alias *alias)
1255 {
1256         char *p;
1257         char *num;
1258         PString h323id(alias->name);
1259         PString e164(alias->e164);
1260         char *prefix;
1261         
1262         if (!h323_end_point_exist()) {
1263                 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
1264                 return 1;
1265         }
1266
1267         cout << "== Adding alias \"" << h323id << "\" to endpoint" << endl;
1268         endPoint->AddAliasName(h323id); 
1269         endPoint->RemoveAliasName(localProcess->GetUserName());
1270
1271         if (!e164.IsEmpty()) {
1272                 cout << "== Adding E.164 \"" << e164 << "\" to endpoint" << endl;
1273                 endPoint->AddAliasName(e164);
1274         }
1275         if (strlen(alias->prefix)) {
1276                 p = prefix = strdup(alias->prefix);
1277                 while((num = strsep(&p, ",")) != (char *)NULL) {
1278                         cout << "== Adding Prefix \"" << num << "\" to endpoint" << endl;
1279                         endPoint->SupportedPrefixes += PString(num);
1280                         endPoint->SetGateway();
1281                 }
1282                 if (prefix)
1283                         free(prefix);
1284         }
1285         return 0;
1286 }
1287
1288 void h323_set_id(char *id)
1289 {
1290         PString h323id(id);
1291
1292         if (h323debug) {
1293                 cout << "  == Using '" << h323id << "' as our H.323ID for this call" << endl;
1294         }
1295         /* EVIL HACK */
1296         endPoint->SetLocalUserName(h323id);
1297 }
1298
1299 void h323_show_tokens(void)
1300 {
1301         cout << "Current call tokens: " << setprecision(2) << endPoint->GetAllConnections() << endl;
1302 }
1303
1304 /** Establish Gatekeeper communiations, if so configured, 
1305   *     register aliases for the H.323 endpoint to respond to.
1306   */
1307 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
1308 {
1309         PString gkName = PString(gatekeeper);
1310         PString pass = PString(secret);
1311         H323TransportUDP *rasChannel;
1312
1313         if (!h323_end_point_exist()) {
1314                 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
1315                 return 1;
1316         }
1317
1318         if (!gatekeeper) {
1319                 cout << "Error: Gatekeeper cannot be NULL" << endl;
1320                 return 1;
1321         }
1322         if (strlen(secret)) {
1323                 endPoint->SetGatekeeperPassword(pass);
1324         }
1325         if (gatekeeper_discover) {
1326                 /* discover the gk using multicast */
1327                 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
1328                         cout << "== Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1329                 } else {
1330                         cout << "Warning: Could not find a gatekeeper." << endl;
1331                         return 1;
1332                 }       
1333         } else {
1334                 rasChannel = new H323TransportUDP(*endPoint);
1335
1336                 if (!rasChannel) {
1337                         cout << "Error: No RAS Channel, this is bad" << endl;
1338                         return 1;
1339                 }
1340                 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1341                         cout << "== Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1342                 } else {
1343                         cout << "Error registering with gatekeeper \"" << gkName << "\". " << endl;
1344                         /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1345                         return 1;
1346                 }
1347         }
1348         return 0;
1349 }
1350
1351 /** Send a DTMF tone over the H323Connection with the
1352   * specified token.
1353   */
1354 void h323_send_tone(const char *call_token, char tone)
1355 {
1356         if (!h323_end_point_exist()) {
1357                 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1358                 return;
1359         }
1360         PString token = PString(call_token);
1361         endPoint->SendUserTone(token, tone);
1362 }
1363
1364 /** Make a call to the remote endpoint.
1365   */
1366 int h323_make_call(char *dest, call_details_t *cd, call_options_t *call_options)
1367 {
1368         int res;
1369         PString token;
1370         PString host(dest);
1371
1372         if (!h323_end_point_exist()) {
1373                 return 1;
1374         }
1375
1376         res = endPoint->MakeCall(host, token, &cd->call_reference, call_options);
1377         memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1378         return res;
1379 };
1380
1381 int h323_clear_call(const char *call_token, int cause)
1382 {
1383         H225_ReleaseCompleteReason dummy;
1384         H323Connection::CallEndReason r = H323Connection::EndedByLocalUser;
1385         MyH323Connection *connection;
1386         const PString currentToken(call_token);
1387
1388         if (!h323_end_point_exist()) {
1389                 return 1;
1390         }
1391
1392         if (cause) {
1393                 r = H323TranslateToCallEndReason((Q931::CauseValues)(cause), dummy);
1394         }
1395
1396         connection = (MyH323Connection *)endPoint->FindConnectionWithLock(currentToken);
1397         if (connection) {
1398                 connection->SetCause(cause);
1399                 connection->SetCallEndReason(r);
1400                 connection->Unlock();
1401         }
1402         endPoint->ClearCall(currentToken, r);
1403         return 0;
1404 };
1405
1406 /* Send Alerting PDU to H.323 caller */
1407 int h323_send_alerting(const char *token)
1408 {
1409         const PString currentToken(token);
1410         H323Connection * connection;
1411
1412         if (h323debug) {
1413                 cout << "\tSending alerting\n" << endl;
1414         }
1415         connection = endPoint->FindConnectionWithLock(currentToken);
1416         if (!connection) {
1417                 cout << "No connection found for " << token << endl;
1418                 return -1;
1419         }
1420         connection->AnsweringCall(H323Connection::AnswerCallPending);
1421         connection->Unlock();
1422         return 0; 
1423
1424 }
1425
1426 /* Send Progress PDU to H.323 caller */
1427 int h323_send_progress(const char *token)
1428 {
1429         const PString currentToken(token);
1430         H323Connection * connection;
1431
1432         connection = endPoint->FindConnectionWithLock(currentToken);
1433         if (!connection) {
1434                 cout << "No connection found for " << token << endl;
1435                 return -1;
1436         }
1437         connection->AnsweringCall(H323Connection::AnswerCallDeferredWithMedia);
1438         connection->Unlock();
1439         return 0;  
1440 }
1441
1442 /** This function tells the h.323 stack to either 
1443     answer or deny an incoming call  */
1444 int h323_answering_call(const char *token, int busy) 
1445 {
1446         const PString currentToken(token);
1447         H323Connection * connection;
1448         
1449         connection = endPoint->FindConnectionWithLock(currentToken);
1450         
1451         if (!connection) {
1452                 cout << "No connection found for " << token << endl;
1453                 return -1;
1454         }
1455         if (!busy) {
1456                 if (h323debug) {
1457                         cout << "\tAnswering call " << token << endl;
1458                 }
1459                 connection->AnsweringCall(H323Connection::AnswerCallNow);
1460         } else {
1461                 if (h323debug) {
1462                         cout << "\tdenying call " << token << endl;
1463                 }
1464                 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1465         }
1466         connection->Unlock();
1467         return 0;
1468 }
1469
1470 int h323_show_codec(int fd, int argc, char *argv[])
1471 {
1472         cout <<  "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1473         return 0;
1474 }
1475
1476 int h323_soft_hangup(const char *data)
1477 {
1478         PString token(data);
1479         BOOL result;
1480         cout << "Soft hangup" << endl;  
1481         result = endPoint->ClearCall(token);    
1482         return result;
1483 }
1484
1485 /* alas, this doesn't work :(   */
1486 void h323_native_bridge(const char *token, const char *them, char *capability)
1487 {
1488         H323Channel *channel;
1489         MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1490         
1491         if (!connection) {
1492                 cout << "ERROR: No connection found, this is bad\n";
1493                 return;
1494         }
1495
1496         cout << "Native Bridge:  them [" << them << "]" << endl; 
1497
1498         channel = connection->FindChannel(connection->sessionId, TRUE);
1499         connection->bridging = TRUE;
1500         connection->CloseLogicalChannelNumber(channel->GetNumber());
1501         
1502         connection->Unlock();
1503         return;
1504
1505 }
1506
1507 #undef cout
1508 void h323_end_process(void)
1509 {
1510         if (endPoint) {
1511                 endPoint->ClearAllCalls();
1512                 endPoint->RemoveListener(NULL);
1513                 delete endPoint;
1514                 endPoint = NULL;
1515         }
1516         if (localProcess) {
1517                 delete localProcess;
1518                 localProcess = NULL;
1519         }
1520         PTrace::SetLevel(0);
1521         PTrace::SetStream(&cout);
1522         if (logstream) {
1523                 delete logstream;
1524                 logstream = NULL;
1525         }
1526 }
1527
1528 } /* extern "C" */
1529