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