Don't do reinvite if both parties talk diffrent codecs
[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  * This code 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 "ast_h323.h"
29 #include "h323t38.h"
30
31
32 /* PWlib Required Components  */
33 #define MAJOR_VERSION 0
34 #define MINOR_VERSION 1
35 #define BUILD_TYPE    ReleaseCode
36 #define BUILD_NUMBER  0
37
38 /** Counter for the number of connections */
39 int channelsOpen;
40
41 /* DTMF Mode */
42 int mode = H323_DTMF_RFC2833;
43
44 /** Options for connections creation */
45 BOOL    noFastStart;
46 BOOL    noH245Tunnelling;
47 BOOL    noSilenceSuppression;
48
49 /**
50  * We assume that only one endPoint should exist.
51  * The application cannot run the h323_end_point_create() more than once
52  * FIXME: Singleton this, for safety
53  */
54 MyH323EndPoint *endPoint = NULL;
55
56 /** PWLib entry point */
57 MyProcess *localProcess = NULL;
58
59 /** H.323 listener */
60 H323ListenerTCP *tcpListener;
61
62 MyProcess::MyProcess(): PProcess("The NuFone Network's", "H.323 Channel Driver for Asterisk",
63              MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
64 {
65         Resume();
66 }
67
68 MyProcess::~MyProcess()
69 {
70         cout << " == PWLib proces going down." << endl;
71         delete endPoint;
72         endPoint = NULL;
73 }
74
75 void MyProcess::Main()
76 {
77         cout << "  == Creating H.323 Endpoint" << endl;
78         endPoint = new MyH323EndPoint();
79         PTrace::Initialise(0, NULL, PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
80 }
81
82 ClearCallThread::ClearCallThread(const char *tc) : PThread(10000, PThread::NoAutoDeleteThread)
83
84         token = tc;
85         Resume(); 
86 }
87
88 ClearCallThread::~ClearCallThread()
89 {
90         if (h323debug)
91                 cout <<  " == ClearCall thread going down." << endl;
92         return;
93 }
94     
95 void ClearCallThread::Main()
96 {
97         endPoint->ClearCall(token);
98         return;
99 }
100
101
102 #define H323_NAME OPAL_G7231_6k3"{sw}"
103
104 H323_REGISTER_CAPABILITY(H323_G7231Capability, H323_NAME);
105
106 H323_G7231Capability::H323_G7231Capability(BOOL annexA_)
107   : H323AudioCapability(7, 4)
108 {
109   annexA = annexA_;
110 }
111
112 PObject::Comparison H323_G7231Capability::Compare(const PObject & obj) const
113 {
114   Comparison result = H323AudioCapability::Compare(obj);
115   if (result != EqualTo)
116     return result;
117
118   PINDEX otherAnnexA = ((const H323_G7231Capability &)obj).annexA;
119   if (annexA < otherAnnexA)
120     return LessThan;
121   if (annexA > otherAnnexA)
122     return GreaterThan;
123   return EqualTo;
124 }
125
126 PObject * H323_G7231Capability::Clone() const
127 {
128   return new H323_G7231Capability(*this);
129 }
130
131
132 PString H323_G7231Capability::GetFormatName() const
133 {
134   return H323_NAME;
135 }
136
137
138 unsigned H323_G7231Capability::GetSubType() const
139 {
140   return H245_AudioCapability::e_g7231;
141 }
142
143
144 BOOL H323_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
145                                           unsigned packetSize) const
146 {
147   cap.SetTag(H245_AudioCapability::e_g7231);
148
149   H245_AudioCapability_g7231 & g7231 = cap;
150   g7231.m_maxAl_sduAudioFrames = packetSize;
151   g7231.m_silenceSuppression = annexA;
152
153   return TRUE;
154 }
155
156
157 BOOL H323_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
158                                            unsigned & packetSize)
159 {
160   if (cap.GetTag() != H245_AudioCapability::e_g7231)
161     return FALSE;
162
163   const H245_AudioCapability_g7231 & g7231 = cap;
164   packetSize = g7231.m_maxAl_sduAudioFrames;
165   annexA = g7231.m_silenceSuppression;
166
167   return TRUE;
168 }
169
170
171 H323Codec * H323_G7231Capability::CreateCodec(H323Codec::Direction direction) const
172 {
173   return NULL;
174 }
175
176 /** MyH323EndPoint 
177   * The fullAddress parameter is used directly in the MakeCall method so
178   * the General form for the fullAddress argument is :
179   * [alias@][transport$]host[:port]
180   * default values:     alias = the same value as host.
181   *                                     transport = ip.
182   *                                     port = 1720.
183   */
184 int MyH323EndPoint::MakeCall(const PString & dest, PString & token, 
185                                                                                 unsigned int *callReference, unsigned int port, char *callerid)
186 {
187         PString fullAddress;
188         MyH323Connection * connection;
189
190         /* Determine whether we are using a gatekeeper or not. */
191         if (GetGatekeeper() != NULL) {
192                 fullAddress = dest;
193                 if (h323debug)
194                         cout << " -- Making call to " << fullAddress << " using gatekeeper." << endl;
195         } else {
196                         fullAddress = dest; /* host */
197                         if (h323debug)
198                                 cout << " -- Making call to " << fullAddress << "." << endl;
199         }
200
201         if (!(connection = (MyH323Connection *)H323EndPoint::MakeCallLocked(fullAddress, token))) {
202                 if (h323debug)
203                         cout << "Error making call to \"" << fullAddress << '"' << endl;
204                 return 1;
205         }
206         
207         *callReference = connection->GetCallReference();
208         
209         if (callerid)
210                 connection->SetLocalPartyName(PString(callerid));
211
212         connection->Unlock();   
213
214         if (h323debug) {
215                 cout << "       -- " << GetLocalUserName() << " is calling host " << fullAddress << endl;
216                 cout << "       -- " << "Call token is " << (const char *)token << endl;
217                 cout << "       -- Call reference is " << *callReference << endl;
218         }
219         return 0;
220 }
221
222 void MyH323EndPoint::SetEndpointTypeInfo( H225_EndpointType & info ) const
223 {
224   H323EndPoint::SetEndpointTypeInfo(info);
225   info.m_gateway.IncludeOptionalField(H225_GatewayInfo::e_protocol);
226   info.m_gateway.m_protocol.SetSize(1);
227   H225_SupportedProtocols &protocol=info.m_gateway.m_protocol[0];
228   protocol.SetTag(H225_SupportedProtocols::e_voice);
229   PINDEX as=SupportedPrefixes.GetSize();
230   ((H225_VoiceCaps &)protocol).m_supportedPrefixes.SetSize(as);
231   for (PINDEX p=0; p<as; p++) {
232     H323SetAliasAddress(SupportedPrefixes[p], ((H225_VoiceCaps &)protocol).m_supportedPrefixes[p].m_prefix);
233   }
234 }
235
236 void MyH323EndPoint::SetGateway(void)
237 {
238         terminalType = e_GatewayOnly;
239 }
240
241 H323Capabilities MyH323EndPoint::GetCapabilities(void)
242 {
243         return capabilities;
244 }
245
246 BOOL MyH323EndPoint::ClearCall(const PString & token)
247 {
248         if (h323debug)
249                 cout << "       -- ClearCall: Request to clear call with token " << token << endl;
250         return H323EndPoint::ClearCall(token);
251 }
252
253 void MyH323EndPoint::SendUserTone(const PString &token, char tone)
254 {
255         H323Connection *connection = NULL;
256                 
257         connection = FindConnectionWithLock(token);
258         if (connection != NULL) {
259                 connection->SendUserInputTone(tone, 500);
260                 connection->Unlock();
261         }
262 }
263
264 void MyH323EndPoint::OnClosedLogicalChannel(H323Connection & connection, const H323Channel & channel)
265 {
266         channelsOpen--;
267         if (h323debug)
268                 cout << "               channelsOpen = " << channelsOpen << endl;
269         H323EndPoint::OnClosedLogicalChannel(connection, channel);
270 }
271
272 BOOL MyH323EndPoint::OnConnectionForwarded(H323Connection & connection,
273                 const PString & forwardParty,
274                 const H323SignalPDU & pdu)
275  {
276         if (h323debug)
277         cout << "       -- Call Forwarded to " << forwardParty << endl;
278         return FALSE;
279  }
280  
281 BOOL MyH323EndPoint::ForwardConnection(H323Connection & connection,
282                 const PString & forwardParty,
283                 const H323SignalPDU & pdu)
284 {
285         if (h323debug)
286                 cout << "       -- Forwarding call to " << forwardParty << endl;
287         return H323EndPoint::ForwardConnection(connection, forwardParty, pdu);
288 }
289
290 void MyH323EndPoint::OnConnectionEstablished(H323Connection & connection, const PString & estCallToken)
291 {
292         if (h323debug)
293                 cout << "       -- Connection Established with \"" << connection.GetRemotePartyName() << "\"" << endl;
294         on_connection_established(connection.GetCallReference());
295 }
296
297 /** OnConnectionCleared callback function is called upon the dropping of an established
298   * H323 connection. 
299   */
300 void MyH323EndPoint::OnConnectionCleared(H323Connection & connection, const PString & clearedCallToken)
301 {
302         PString remoteName = connection.GetRemotePartyName();
303         
304         call_details_t cd;
305
306         cd.call_reference = connection.GetCallReference();
307         cd.call_token = (const char *)connection.GetCallToken();
308         cd.call_source_aliases = (const char *)connection.GetRemotePartyName();
309         
310         /* Convert complex strings */
311         char *s;
312     if ((s = strchr(cd.call_source_aliases, ' ')) != NULL)
313                 *s = '\0';
314
315         /* Invoke the PBX application registered callback */
316         on_connection_cleared(cd);
317
318         switch (connection.GetCallEndReason()) {
319                 case H323Connection::EndedByCallForwarded :
320                         if (h323debug)
321                                 cout << " -- " << remoteName << " has forwarded the call" << endl;
322                         break;
323                 case H323Connection::EndedByRemoteUser :
324                         if (h323debug)
325                                 cout << " -- " << remoteName << " has cleared the call" << endl;
326                         break;
327                 case H323Connection::EndedByCallerAbort :
328                         if (h323debug)
329                                 cout << " -- " << remoteName << " has stopped calling" << endl;
330                         break;
331                 case H323Connection::EndedByRefusal :
332                         if (h323debug)
333                                 cout << " -- " << remoteName << " did not accept your call" << endl;
334                         break;
335                 case H323Connection::EndedByRemoteBusy :
336                         if (h323debug)
337                         cout << " -- " << remoteName << " was busy" << endl;
338                         break;
339                 case H323Connection::EndedByRemoteCongestion :
340                         if (h323debug)
341                                 cout << " -- Congested link to " << remoteName << endl;
342                         break;
343                 case H323Connection::EndedByNoAnswer :
344                         if (h323debug)
345                                 cout << " -- " << remoteName << " did not answer your call" << endl;
346                         break;
347                 case H323Connection::EndedByTransportFail :
348                         if (h323debug)
349                                 cout << " -- Call with " << remoteName << " ended abnormally" << endl;
350                         break;
351                 case H323Connection::EndedByCapabilityExchange :
352                         if (h323debug)
353                                 cout << " -- Could not find common codec with " << remoteName << endl;
354                         break;
355                 case H323Connection::EndedByNoAccept :
356                         if (h323debug)
357                                 cout << " -- Did not accept incoming call from " << remoteName << endl;
358                         break;
359                 case H323Connection::EndedByAnswerDenied :
360                         if (h323debug)
361                                 cout << " -- Refused incoming call from " << remoteName << endl;
362                         break;
363                 case H323Connection::EndedByNoUser :
364                         if (h323debug)
365                                 cout << " -- Remote endpoint could not find user: " << remoteName << endl;
366                         break;
367                 case H323Connection::EndedByNoBandwidth :
368                         if (h323debug)
369                                 cout << " -- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
370                         break;
371                 case H323Connection::EndedByUnreachable :
372                         if (h323debug)
373                                 cout << " -- " << remoteName << " could not be reached." << endl;
374                         break;
375                 case H323Connection::EndedByHostOffline :
376                         if (h323debug)
377                                 cout << " -- " << remoteName << " is not online." << endl;
378                         break;
379                 case H323Connection::EndedByNoEndPoint :
380                         if (h323debug)
381                                 cout << " -- No phone running for " << remoteName << endl;
382                         break;
383                 case H323Connection::EndedByConnectFail :
384                         if (h323debug)
385                                 cout << " -- Transport error calling " << remoteName << endl;
386                         break;
387                 default :
388                         if (h323debug)
389                                 cout << " -- Call with " << remoteName << " completed" << endl;
390         }
391         if(connection.IsEstablished()) 
392                 if (h323debug)
393                         cout << "        -- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
394 }
395
396
397 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *outbound)
398 {
399         unsigned options = 0;
400
401         if (noFastStart)
402                 options |= H323Connection::FastStartOptionDisable;
403
404         if (noH245Tunnelling)
405                 options |= H323Connection::H245TunnelingOptionDisable;
406
407         return new MyH323Connection(*this, callReference, options);
408 }
409
410 /* MyH323Connection */    
411 MyH323Connection::MyH323Connection(MyH323EndPoint & ep,
412                                                                                    unsigned callReference,
413                                                                                unsigned options)
414                                                                                    : H323Connection(ep, 
415                                                                                                                         callReference, 
416                                                                                                                         options)
417 {
418     remoteIpAddress = 0;        // IP Address of remote endpoint
419         remotePort              = 0;    // remote endpoint Data port (control is dataPort+1)
420
421         if (h323debug)
422                 cout << "       == New H.323 Connection created." << endl;
423         return;
424 }
425
426 MyH323Connection::~MyH323Connection()
427 {
428         if (h323debug)
429                 cout << "       == H.323 Connection deleted." << endl;
430         return;
431 }
432
433 H323Connection::AnswerCallResponse      MyH323Connection::OnAnswerCall(const PString & caller,
434                                                                                                                                    const H323SignalPDU & /*setupPDU*/,
435                                                                                                                                    H323SignalPDU & /*connectPDU*/)
436 {
437         /* The call will be answered later with "AnsweringCall()" function.
438          */ 
439         return H323Connection::AnswerCallAlertWithMedia;
440 }
441
442 BOOL  MyH323Connection::OnAlerting(const H323SignalPDU & /*alertingPDU*/, const PString & username)
443 {
444         
445         if (h323debug)
446                 cout << "       -- Ringing phone for \"" << username << "\"" << endl;
447         return TRUE;
448 }
449
450 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
451 {
452         
453         if (h323debug)
454                 cout << "       -- Received SETUP message..." << endl;
455
456         call_details_t cd;
457         
458         PString sourceE164;
459         PString destE164;
460         PString sourceAliases;  
461         PString destAliases;
462         PString sourceIp;
463                 
464         PIPSocket::Address Ip;
465         WORD                       sourcePort;
466
467         sourceAliases = setupPDU.GetSourceAliases();
468         destAliases = setupPDU.GetDestinationAlias();
469                         
470         sourceE164 = "";
471         setupPDU.GetSourceE164(sourceE164);
472         destE164 = "";
473         setupPDU.GetDestinationE164(destE164);
474
475         /* Convert complex strings */
476         //  FIXME: deal more than one source alias 
477         char *s; 
478     if ((s = strchr(sourceAliases, ' ')) != NULL)
479                 *s = '\0';
480     if ((s = strchr(sourceAliases, '\t')) != NULL)
481                 *s = '\0';
482         char *s1;
483     if ((s1 = strchr(destAliases, ' ')) != NULL)
484          *s1 = '\0';
485         if ((s1 = strchr(destAliases, '\t')) != NULL)
486          *s1 = '\0';
487
488         GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
489
490         sourceIp = Ip.AsString();
491
492         cd.call_reference               = GetCallReference();
493         cd.call_token                   = (const char *)GetCallToken();
494         cd.call_source_aliases  = (const char *)sourceAliases;
495         cd.call_dest_alias              = (const char *)destAliases;
496         cd.call_source_e164             = (const char *)sourceE164;
497         cd.call_dest_e164               = (const char *)destE164;
498         cd.sourceIp                             = (const char *)sourceIp;
499         
500         /* Notify Asterisk of the request */
501         int res = on_incoming_call(cd); 
502
503         if (!res) {
504                 if (h323debug)
505                         cout << "       -- Call Failed" << endl;
506                 return FALSE;
507         }
508         
509         return H323Connection::OnReceivedSignalSetup(setupPDU);
510 }
511
512 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
513 {
514         call_details_t cd;
515         
516         if (h323debug) 
517                 cout << "       -- Sending SETUP message" << endl;
518         
519         sourceAliases = setupPDU.GetSourceAliases();
520         destAliases = setupPDU.GetDestinationAlias();
521
522         sourceE164 = "";
523         setupPDU.GetSourceE164(sourceE164);
524         destE164 = "";
525         setupPDU.GetDestinationE164(destE164);
526
527         /* Convert complex strings */
528         //  FIXME: deal more than one source alias 
529         char *s; 
530     if ((s = strchr(sourceAliases, ' ')) != NULL)
531                 *s = '\0';
532     if ((s = strchr(sourceAliases, '\t')) != NULL)
533                 *s = '\0';
534         char *s1;
535     if ((s1 = strchr(destAliases, ' ')) != NULL)
536          *s1 = '\0';
537         if ((s1 = strchr(destAliases, '\t')) != NULL)
538          *s1 = '\0';
539
540         cd.call_reference               = GetCallReference();
541         cd.call_token                   = (const char *)GetCallToken();
542         cd.call_source_aliases  = (const char *)sourceAliases;
543         cd.call_dest_alias              = (const char *)destAliases;
544         cd.call_source_e164             = (const char *)sourceE164;
545         cd.call_dest_e164               = (const char *)destE164;
546
547         int res = on_outgoing_call(cd); 
548                 
549         if (!res) {
550                 if (h323debug)
551                         cout << "       -- Call Failed" << endl;
552                 return FALSE;
553         }
554
555         return H323Connection::OnSendSignalSetup(setupPDU);
556 }
557
558 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
559 {
560         if (h323debug)
561                 cout << "       -- Sending RELEASE COMPLETE" << endl;
562         return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
563 }
564
565 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
566 {
567         if (h323debug)
568                 cout << "       -- Received Facility message... " << endl;
569         return H323Connection::OnReceivedFacility(pdu);
570 }
571
572 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
573 {
574         if (h323debug)
575                 cout <<  "      -- Received RELEASE COMPLETE message..." << endl;
576         return H323Connection::OnReceivedReleaseComplete(pdu);
577
578 }
579
580 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
581 {
582         if (h323debug)
583                 cout << "       -- Closing logical channel..." << endl;
584         return H323Connection::OnClosingLogicalChannel(channel);
585 }
586
587
588 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
589 {
590         if (h323debug)
591                 cout << "       -- Sending user input tone (" << tone << ") to remote" << endl;
592
593         on_send_digit(GetCallReference(), tone);
594                 
595         H323Connection::SendUserInputTone(tone, duration);
596 }
597
598 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
599 {
600         if (mode == H323_DTMF_INBAND) {
601                 if (h323debug)
602                         cout << "       -- Received user input tone (" << tone << ") from remote" << endl;
603                 on_send_digit(GetCallReference(), tone);
604         }
605         H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
606 }
607
608 void MyH323Connection::OnUserInputString(const PString &value)
609 {
610         if (mode == H323_DTMF_RFC2833) {
611                 if (h323debug)
612                         cout <<  "      -- Received user input string (" << value << ") from remote." << endl;
613                 on_send_digit(GetCallReference(), value[0]);
614         }       
615 }
616
617 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
618                                                                                                                      H323Channel::Directions dir,
619                                                                                                                      unsigned sessionID,
620                                                                                                                      const H245_H2250LogicalChannelParameters * /*param*/)
621 {
622         struct rtp_info *info;
623         WORD port;
624
625         /* Determine the Local (A side) IP Address and port */
626         info = on_create_connection(GetCallReference()); 
627         
628 //      if (bridging) {
629 //              externalIpAddress = PIPSocket::Address(info->addr);
630 //      } else {
631         GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
632 //      }
633
634 //      externalIpAddress = PIPSocket::Address("192.168.1.50");
635
636         externalPort = info->port;
637         
638         if (h323debug) {
639                 cout << "       =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
640                 cout << "               -- externalIpAddress: " << externalIpAddress << endl;
641                 cout << "               -- externalPort: " << externalPort << endl;
642                 cout << "               -- SessionID: " << sessionID << endl;
643                 cout << "               -- Direction: " << dir << endl;
644         }
645         return new H323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
646 }
647
648 /** This callback function is invoked once upon creation of each
649   * channel for an H323 session 
650   */
651 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
652 {    
653         if (h323debug) {
654                 cout << "        -- Started logical channel: "; 
655                 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" ")); 
656                 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
657         }
658         // adjust the count of channels we have open
659         channelsOpen++;
660         if (h323debug)
661                 cout <<  "              -- channelsOpen = " << channelsOpen << endl;
662         
663         H323_ExternalRTPChannel & external = (H323_ExternalRTPChannel &)channel;
664     external.GetRemoteAddress(remoteIpAddress, remotePort); 
665
666         if (h323debug) {
667                 cout << "               -- remoteIpAddress: " << remoteIpAddress << endl;
668                 cout << "               -- remotePort: " << remotePort << endl;
669                 cout << "               -- ExternalIpAddress: " << externalIpAddress << endl;
670                 cout << "               -- ExternalPort: " << externalPort << endl;
671         }
672         /* Notify Asterisk of remote RTP information */
673         on_start_logical_channel(GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
674
675         return TRUE;    
676 }
677
678 #if 0
679 MyGatekeeperServer::MyGatekeeperServer(MyH323EndPoint & ep)
680   : H323GatekeeperServer(ep),
681     endpoint(ep)
682 {
683 }
684
685
686 BOOL MyGatekeeperServer::Initialise()
687 {
688   PINDEX i;
689
690   PWaitAndSignal mutex(reconfigurationMutex);
691   
692   SetGatekeeperIdentifier("TESTIES");
693
694     // Interfaces to listen on
695   H323TransportAddressArray interfaces;
696   interfaces.Append(new H323TransportAddress(0.0.0.0);
697   AddListeners(interfaces);
698
699   // lots more to come
700   
701   return TRUE;
702
703 }
704
705 #endif
706
707 /** IMPLEMENTATION OF C FUNCTIONS */
708
709 /**
710  * The extern "C" directive takes care for 
711  * the ANSI-C representation of linkable symbols 
712  */
713 extern "C" {
714
715 int end_point_exist(void)
716 {
717         if (!endPoint) {
718                 return 0;
719         }
720         return 1;
721 }
722     
723 void h323_end_point_create(void)
724 {
725         channelsOpen = 0;
726         localProcess = new MyProcess(); 
727         localProcess->Main();
728 }
729
730 void h323_gk_urq(void)
731 {
732         if (!end_point_exist()) {
733                 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
734                 return;
735         }
736         
737         endPoint->RemoveGatekeeper();
738 }
739
740 void h323_end_process(void)
741 {       
742         delete localProcess;
743 }
744
745 void h323_debug(int flag, unsigned level)
746 {
747         if (flag) 
748                 PTrace:: SetLevel(level); 
749         else 
750                 PTrace:: SetLevel(0); 
751 }
752         
753 /** Installs the callback functions on behalf of the PBX application  */
754 void h323_callback_register(setup_incoming_cb  ifunc,
755                                                         setup_outbound_cb  sfunc,
756                                                         on_connection_cb   confunc,
757                                                         start_logchan_cb   lfunc,
758                                                         clear_con_cb       clfunc,
759                                                         con_established_cb efunc,
760                                                         send_digit_cb      dfunc)
761 {
762         on_incoming_call = ifunc;
763         on_outgoing_call = sfunc;
764         on_create_connection = confunc;
765         on_start_logical_channel = lfunc;
766         on_connection_cleared = clfunc;
767         on_connection_established = efunc;
768         on_send_digit = dfunc;
769 }
770
771 /**
772  * Add capability to the capability table of the end point. 
773  */
774 int h323_set_capability(int cap, int dtmfMode)
775 {
776         int g711Frames = 30;
777     int gsmFrames  = 4;
778
779         
780         if (!end_point_exist()) {
781                 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
782                 return 1;
783         }
784
785         mode = dtmfMode;
786         if (dtmfMode == H323_DTMF_INBAND)
787             endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
788         else
789                 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
790          
791
792         /* Hardcode this for now (Someone tell me if T.38 works now 
793            or provide me with some debug so we can make this work */
794
795 //      endPoint->SetCapability(0, 0, new H323_T38Capability(H323_T38Capability::e_UDP));
796         
797         if (cap & AST_FORMAT_SPEEX) {
798                 /* Not real sure if Asterisk acutally supports all
799                    of the various different bit rates so add them 
800                    all and figure it out later*/
801
802                 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
803                 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
804                 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
805                 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
806                 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
807         }
808
809 #if WANT_G729
810         if (cap & AST_FORMAT_G729A) {
811                 H323_G729ACapability *g729aCap;
812                 endPoint->SetCapability(0, 0, g729aCap = new H323_G729ACapability);
813         }
814 #endif
815         
816         if (cap & AST_FORMAT_G723_1) {
817                 H323_G7231Capability *g7231Cap;
818                 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
819         } 
820
821         if (cap & AST_FORMAT_GSM) {
822                 H323_GSM0610Capability *gsmCap;
823             endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
824             gsmCap->SetTxFramesInPacket(gsmFrames);
825         } 
826
827         if (cap & AST_FORMAT_ULAW) {
828
829                 H323_G711Capability *g711uCap;
830             endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
831                 g711uCap->SetTxFramesInPacket(g711Frames);
832         } 
833
834         if (cap & AST_FORMAT_ALAW) {
835                 H323_G711Capability *g711aCap;
836                 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
837                 g711aCap->SetTxFramesInPacket(g711Frames);
838         } 
839
840         return 0;
841 }
842
843 /** Start the H.323 listener */
844 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
845 {
846         
847         if (!end_point_exist()) {
848                 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
849                 return 1;
850         }
851         
852         PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
853
854         if (!listenPort)
855                 listenPort = 1720;
856         
857         tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
858
859         if (!endPoint->StartListener(tcpListener)) {
860                 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
861                 delete tcpListener;
862                 return 1;
863                 
864         }
865                 
866 //      cout << "  == H.323 listener started on " << ((H323ListenerTCP *) tcpListener)->GetTransportAddress() << endl;
867         cout << "  == H.323 listener started" << endl;
868
869         return 0;
870 };
871  
872    
873 int h323_set_alias(struct oh323_alias *alias)
874 {
875         char *p;
876         char *num;
877         PString h323id(alias->name);
878         PString e164(alias->e164);
879         
880         if (!end_point_exist()) {
881                 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
882                 return 1;
883         }
884
885         cout << "  == Adding alias \"" << h323id << "\" to endpoint" << endl;
886         endPoint->AddAliasName(h323id); 
887
888         endPoint->RemoveAliasName(localProcess->GetUserName());
889
890         if (!e164.IsEmpty()) {
891                 cout << "  == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
892                 endPoint->AddAliasName(e164);
893         }
894         if (strlen(alias->prefix)) {
895                 p = alias->prefix;
896                 num = strsep(&p, ",");
897                 while(num) {
898                 cout << "  == Adding Prefix \"" << num << "\" to endpoint" << endl;
899                         endPoint->SupportedPrefixes += PString(num);
900                         endPoint->SetGateway();
901                 num = strsep(&p, ",");          
902                 }
903         }
904
905         return 0;
906 }
907
908
909 void h323_set_id(char *id)
910 {
911         PString h323id(id);
912
913         if (h323debug) {
914                 cout << "  == Using '" << h323id << "' as our H.323ID for this call" << endl;
915         }
916
917         /* EVIL HACK */
918         endPoint->SetLocalUserName(h323id);
919 }
920
921 /** Establish Gatekeeper communiations, if so configured, 
922   *     register aliases for the H.323 endpoint to respond to.
923   */
924 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
925 {
926         PString gkName = PString(gatekeeper);
927         PString pass   = PString(secret);
928
929         if (!end_point_exist()) {
930                 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
931                 return 1;
932         }
933
934         if (!gatekeeper) {
935                 cout << "Error: Gatekeeper cannot be NULL" << endl;
936                 return 1;
937         }
938
939         if (strlen(secret)) {
940                 endPoint->SetGatekeeperPassword(pass);
941         }
942                 
943
944         if (gatekeeper_discover) {
945                 /* discover the gk using multicast */
946                 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
947                         cout << "  == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
948                 } else {
949                         cout << "  *** Could not find a gatekeeper." << endl;
950                         return 1;
951                 }       
952         } else {
953                 /* Gatekeeper operations */
954                 H323TransportUDP *rasChannel = new H323TransportUDP(*endPoint);
955         
956                 if (!rasChannel) {
957                         cout << "       *** No RAS Channel, this is bad" << endl;
958                         return 1;
959                 }
960                 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
961                         cout << "  == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
962                 } else {
963                         cout << "  *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
964                         
965                         /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
966
967                 //      delete rasChannel;
968                         return 1;
969                 }
970         }
971         
972         return 0;
973 }
974
975 /** Send a DTMF tone over the H323Connection with the
976   * specified token.
977   */
978 void h323_send_tone(const char *call_token, char tone)
979 {
980         if (!end_point_exist()) {
981                 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
982                 return;
983         }
984
985         PString token = PString(call_token);
986         endPoint->SendUserTone(token, tone);
987 }
988
989 /** Make a call to the remote endpoint.
990   */
991 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
992 {
993         int res;
994         PString token;
995
996         if (!end_point_exist()) {
997                 return 1;
998         }
999         
1000         PString dest(host);
1001
1002         res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid);
1003         memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1004         
1005         return res;
1006 };
1007
1008 int h323_clear_call(const char *call_token)
1009 {
1010         if (!end_point_exist()) {
1011                 return 1;
1012         }
1013
1014         ClearCallThread *clearCallThread = new ClearCallThread(call_token);
1015         clearCallThread->WaitForTermination();
1016         
1017         return 0;
1018 };
1019
1020 /** This function tells the h.323 stack to either 
1021     answer or deny an incoming call  */
1022 int h323_answering_call(const char *token, int busy) 
1023 {
1024         const PString currentToken(token);
1025
1026         H323Connection * connection;
1027         
1028         connection = endPoint->FindConnectionWithLock(currentToken);
1029         
1030         if (connection == NULL) {
1031                 cout << "No connection found for " << token << endl;
1032                 return -1;
1033         }
1034
1035         if (!busy){
1036                 connection->AnsweringCall(H323Connection::AnswerCallNow);
1037                 connection->Unlock();
1038
1039         } else {
1040                 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1041                 connection->Unlock();
1042         };
1043
1044         return 0;
1045 }
1046
1047
1048 int h323_show_codec(int fd, int argc, char *argv[])
1049 {
1050         cout <<  "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1051
1052         return 0;
1053 }
1054
1055
1056 /* alas, this doesn't work :(   */
1057 void h323_native_bridge(const char *token, char *them, char *capability)
1058 {
1059         H323Channel *channel;
1060         MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1061         PString mode(capability);
1062         
1063         if (!mode) {
1064                 return;
1065         }
1066
1067         if (!connection){
1068                 cout << "ERROR: No connection found, this is bad\n";
1069                 return;
1070         }
1071
1072         cout << "Native Bridge:  them [" << them << "]" << endl; 
1073
1074         channel = connection->FindChannel(connection->sessionId, TRUE);
1075         connection->bridging = TRUE;
1076         connection->CloseLogicalChannelNumber(channel->GetNumber());
1077         connection->RequestModeChange(mode);    
1078         connection->Unlock();
1079         return;
1080
1081 }
1082
1083 } /* extern "C" */
1084
1085
1086