forked from pttlink/Asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan_h323.c
3279 lines (2964 loc) · 92.4 KB
/
chan_h323.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2005
*
* OpenH323 Channel Driver for ASTERISK PBX.
* By Jeremy McNamara
* For The NuFone Network
*
* chan_h323 has been derived from code created by
* Michael Manousos and Mark Spencer
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief This file is part of the chan_h323 driver for Asterisk
*
* \author Jeremy McNamara
*
* \par See also
* \arg Config_h323
*
* \ingroup channel_drivers
*/
/*** MODULEINFO
<depend>openh323</depend>
<defaultenabled>yes</defaultenabled>
***/
#ifdef __cplusplus
extern "C" {
#endif
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 147386 $")
#ifdef __cplusplus
}
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <sys/param.h>
#if defined(BSD) || defined(SOLARIS)
#ifndef IPTOS_MINCOST
#define IPTOS_MINCOST 0x02
#endif
#endif
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "asterisk/lock.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/musiconhold.h"
#include "asterisk/pbx.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/rtp.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/cli.h"
#include "asterisk/dsp.h"
#include "asterisk/causes.h"
#include "asterisk/stringfields.h"
#include "asterisk/abstract_jb.h"
#include "asterisk/astobj.h"
#ifdef __cplusplus
}
#endif
#include "h323/chan_h323.h"
receive_digit_cb on_receive_digit;
on_rtp_cb on_external_rtp_create;
start_rtp_cb on_start_rtp_channel;
setup_incoming_cb on_incoming_call;
setup_outbound_cb on_outgoing_call;
chan_ringing_cb on_chan_ringing;
con_established_cb on_connection_established;
clear_con_cb on_connection_cleared;
answer_call_cb on_answer_call;
progress_cb on_progress;
rfc2833_cb on_set_rfc2833_payload;
hangup_cb on_hangup;
setcapabilities_cb on_setcapabilities;
setpeercapabilities_cb on_setpeercapabilities;
/* global debug flag */
int h323debug;
/*! Global jitterbuffer configuration - by default, jb is disabled */
static struct ast_jb_conf default_jbconf =
{
.flags = 0,
.max_size = -1,
.resync_threshold = -1,
.impl = ""
};
static struct ast_jb_conf global_jbconf;
/** Variables required by Asterisk */
static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
static const char config[] = "h323.conf";
static char default_context[AST_MAX_CONTEXT] = "default";
static struct sockaddr_in bindaddr;
#define GLOBAL_CAPABILITY (AST_FORMAT_G723_1 | AST_FORMAT_GSM | AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_G729A | AST_FORMAT_H261)
/** H.323 configuration values */
static int h323_signalling_port = 1720;
static char gatekeeper[100];
static int gatekeeper_disable = 1;
static int gatekeeper_discover = 0;
static int gkroute = 0;
/* Find user by alias (h.323 id) is default, alternative is the incomming call's source IP address*/
static int userbyalias = 1;
static int acceptAnonymous = 1;
static int tos = 0;
static char secret[50];
static unsigned int unique = 0;
static call_options_t global_options;
/** Private structure of a OpenH323 channel */
struct oh323_pvt {
ast_mutex_t lock; /* Channel private lock */
call_options_t options; /* Options to be used during call setup */
int alreadygone; /* Whether or not we've already been destroyed by our peer */
int needdestroy; /* if we need to be destroyed */
call_details_t cd; /* Call details */
struct ast_channel *owner; /* Who owns us */
struct sockaddr_in sa; /* Our peer */
struct sockaddr_in redirip; /* Where our RTP should be going if not to us */
int nonCodecCapability; /* non-audio capability */
int outgoing; /* Outgoing or incoming call? */
char exten[AST_MAX_EXTENSION]; /* Requested extension */
char context[AST_MAX_CONTEXT]; /* Context where to start */
char accountcode[256]; /* Account code */
char rdnis[80]; /* Referring DNIS, if available */
int amaflags; /* AMA Flags */
struct ast_rtp *rtp; /* RTP Session */
struct ast_dsp *vad; /* Used for in-band DTMF detection */
int nativeformats; /* Codec formats supported by a channel */
int needhangup; /* Send hangup when Asterisk is ready */
int hangupcause; /* Hangup cause from OpenH323 layer */
int newstate; /* Pending state change */
int newcontrol; /* Pending control to send */
int newdigit; /* Pending DTMF digit to send */
int newduration; /* Pending DTMF digit duration to send */
int pref_codec; /* Preferred codec */
int peercapability; /* Capabilities learned from peer */
int jointcapability; /* Common capabilities for local and remote side */
struct ast_codec_pref peer_prefs; /* Preferenced list of codecs which remote side supports */
int dtmf_pt; /* Payload code used for RFC2833 messages */
int curDTMF; /* DTMF tone being generated to Asterisk side */
int DTMFsched; /* Scheduler descriptor for DTMF */
int update_rtp_info; /* Configuration of fd's array is pending */
int recvonly; /* Peer isn't wish to receive our voice stream */
int txDtmfDigit; /* DTMF digit being to send to H.323 side */
int noInbandDtmf; /* Inband DTMF processing by DSP isn't available */
int connection_established; /* Call got CONNECT message */
int got_progress; /* Call got PROGRESS message, pass inband audio */
struct oh323_pvt *next; /* Next channel in list */
} *iflist = NULL;
static struct ast_user_list {
ASTOBJ_CONTAINER_COMPONENTS(struct oh323_user);
} userl;
static struct ast_peer_list {
ASTOBJ_CONTAINER_COMPONENTS(struct oh323_peer);
} peerl;
static struct ast_alias_list {
ASTOBJ_CONTAINER_COMPONENTS(struct oh323_alias);
} aliasl;
/** Asterisk RTP stuff */
static struct sched_context *sched;
static struct io_context *io;
/** Protect the interface list (oh323_pvt) */
AST_MUTEX_DEFINE_STATIC(iflock);
/* Protect the monitoring thread, so only one process can kill or start it, and not
when it's doing something critical. */
AST_MUTEX_DEFINE_STATIC(monlock);
/* Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
AST_MUTEX_DEFINE_STATIC(caplock);
/* Protect the reload process */
AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
static int h323_reloading = 0;
/* This is the thread for the monitor which checks for input on the channels
which are not currently in use. */
static pthread_t monitor_thread = AST_PTHREADT_NULL;
static int restart_monitor(void);
static int h323_do_reload(void);
static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause);
static int oh323_digit_begin(struct ast_channel *c, char digit);
static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration);
static int oh323_call(struct ast_channel *c, char *dest, int timeout);
static int oh323_hangup(struct ast_channel *c);
static int oh323_answer(struct ast_channel *c);
static struct ast_frame *oh323_read(struct ast_channel *c);
static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static const struct ast_channel_tech oh323_tech = {
.type = "H323",
.description = tdesc,
.capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
.requester = oh323_request,
.send_digit_begin = oh323_digit_begin,
.send_digit_end = oh323_digit_end,
.call = oh323_call,
.hangup = oh323_hangup,
.answer = oh323_answer,
.read = oh323_read,
.write = oh323_write,
.indicate = oh323_indicate,
.fixup = oh323_fixup,
/* disable, for now */
#if 0
.bridge = ast_rtp_bridge,
#endif
};
static const char* redirectingreason2str(int redirectingreason)
{
switch (redirectingreason) {
case 0:
return "UNKNOWN";
case 1:
return "BUSY";
case 2:
return "NO_REPLY";
case 0xF:
return "UNCONDITIONAL";
default:
return "NOREDIRECT";
}
}
static void oh323_destroy_alias(struct oh323_alias *alias)
{
if (h323debug)
ast_log(LOG_DEBUG, "Destroying alias '%s'\n", alias->name);
free(alias);
}
static void oh323_destroy_user(struct oh323_user *user)
{
if (h323debug)
ast_log(LOG_DEBUG, "Destroying user '%s'\n", user->name);
ast_free_ha(user->ha);
free(user);
}
static void oh323_destroy_peer(struct oh323_peer *peer)
{
if (h323debug)
ast_log(LOG_DEBUG, "Destroying peer '%s'\n", peer->name);
ast_free_ha(peer->ha);
free(peer);
}
static int oh323_simulate_dtmf_end(const void *data)
{
struct oh323_pvt *pvt = (struct oh323_pvt *)data;
if (pvt) {
ast_mutex_lock(&pvt->lock);
/* Don't hold pvt lock while trying to lock the channel */
while(pvt->owner && ast_channel_trylock(pvt->owner)) {
ast_mutex_unlock(&pvt->lock);
usleep(1);
ast_mutex_lock(&pvt->lock);
}
if (pvt->owner) {
struct ast_frame f = {
.frametype = AST_FRAME_DTMF_END,
.subclass = pvt->curDTMF,
.samples = 0,
.src = "SIMULATE_DTMF_END",
};
ast_queue_frame(pvt->owner, &f);
ast_channel_unlock(pvt->owner);
}
pvt->DTMFsched = -1;
ast_mutex_unlock(&pvt->lock);
}
return 0;
}
/* Channel and private structures should be already locked */
static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
{
if (c->nativeformats != pvt->nativeformats) {
if (h323debug)
ast_log(LOG_DEBUG, "Preparing %s for new native format\n", c->name);
c->nativeformats = pvt->nativeformats;
ast_set_read_format(c, c->readformat);
ast_set_write_format(c, c->writeformat);
}
if (pvt->needhangup) {
if (h323debug)
ast_log(LOG_DEBUG, "Process pending hangup for %s\n", c->name);
c->_softhangup |= AST_SOFTHANGUP_DEV;
c->hangupcause = pvt->hangupcause;
ast_queue_hangup(c);
pvt->needhangup = 0;
pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->DTMFsched = -1;
}
if (pvt->newstate >= 0) {
ast_setstate(c, pvt->newstate);
pvt->newstate = -1;
}
if (pvt->newcontrol >= 0) {
ast_queue_control(c, pvt->newcontrol);
pvt->newcontrol = -1;
}
if (pvt->newdigit >= 0) {
struct ast_frame f = {
.frametype = AST_FRAME_DTMF_END,
.subclass = pvt->newdigit,
.samples = pvt->newduration * 8,
.len = pvt->newduration,
.src = "UPDATE_INFO",
};
if (pvt->newdigit == ' ') { /* signalUpdate message */
f.subclass = pvt->curDTMF;
if (pvt->DTMFsched >= 0) {
AST_SCHED_DEL(sched, pvt->DTMFsched);
}
} else { /* Regular input or signal message */
if (pvt->newduration) { /* This is a signal, signalUpdate follows */
f.frametype = AST_FRAME_DTMF_BEGIN;
AST_SCHED_DEL(sched, pvt->DTMFsched);
pvt->DTMFsched = ast_sched_add(sched, pvt->newduration, oh323_simulate_dtmf_end, pvt);
if (h323debug)
ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", pvt->newduration, pvt->DTMFsched);
}
pvt->curDTMF = pvt->newdigit;
}
ast_queue_frame(c, &f);
pvt->newdigit = -1;
}
if (pvt->update_rtp_info > 0) {
if (pvt->rtp) {
ast_jb_configure(c, &global_jbconf);
c->fds[0] = ast_rtp_fd(pvt->rtp);
c->fds[1] = ast_rtcp_fd(pvt->rtp);
ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
}
pvt->update_rtp_info = -1;
}
}
/* Only channel structure should be locked */
static void oh323_update_info(struct ast_channel *c)
{
struct oh323_pvt *pvt = c->tech_pvt;
if (pvt) {
ast_mutex_lock(&pvt->lock);
__oh323_update_info(c, pvt);
ast_mutex_unlock(&pvt->lock);
}
}
static void cleanup_call_details(call_details_t *cd)
{
if (cd->call_token) {
free(cd->call_token);
cd->call_token = NULL;
}
if (cd->call_source_aliases) {
free(cd->call_source_aliases);
cd->call_source_aliases = NULL;
}
if (cd->call_dest_alias) {
free(cd->call_dest_alias);
cd->call_dest_alias = NULL;
}
if (cd->call_source_name) {
free(cd->call_source_name);
cd->call_source_name = NULL;
}
if (cd->call_source_e164) {
free(cd->call_source_e164);
cd->call_source_e164 = NULL;
}
if (cd->call_dest_e164) {
free(cd->call_dest_e164);
cd->call_dest_e164 = NULL;
}
if (cd->sourceIp) {
free(cd->sourceIp);
cd->sourceIp = NULL;
}
if (cd->redirect_number) {
free(cd->redirect_number);
cd->redirect_number = NULL;
}
}
static void __oh323_destroy(struct oh323_pvt *pvt)
{
struct oh323_pvt *cur, *prev = NULL;
AST_SCHED_DEL(sched, pvt->DTMFsched);
if (pvt->rtp) {
ast_rtp_destroy(pvt->rtp);
}
/* Free dsp used for in-band DTMF detection */
if (pvt->vad) {
ast_dsp_free(pvt->vad);
}
cleanup_call_details(&pvt->cd);
/* Unlink us from the owner if we have one */
if (pvt->owner) {
ast_channel_lock(pvt->owner);
if (h323debug)
ast_log(LOG_DEBUG, "Detaching from %s\n", pvt->owner->name);
pvt->owner->tech_pvt = NULL;
ast_channel_unlock(pvt->owner);
}
cur = iflist;
while(cur) {
if (cur == pvt) {
if (prev)
prev->next = cur->next;
else
iflist = cur->next;
break;
}
prev = cur;
cur = cur->next;
}
if (!cur) {
ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
} else {
ast_mutex_unlock(&pvt->lock);
ast_mutex_destroy(&pvt->lock);
free(pvt);
}
}
static void oh323_destroy(struct oh323_pvt *pvt)
{
if (h323debug) {
ast_log(LOG_DEBUG, "Destroying channel %s\n", (pvt->owner ? pvt->owner->name : "<unknown>"));
}
ast_mutex_lock(&iflock);
ast_mutex_lock(&pvt->lock);
__oh323_destroy(pvt);
ast_mutex_unlock(&iflock);
}
static int oh323_digit_begin(struct ast_channel *c, char digit)
{
struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
char *token;
if (!pvt) {
ast_log(LOG_ERROR, "No private structure?! This is bad\n");
return -1;
}
ast_mutex_lock(&pvt->lock);
if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && (pvt->dtmf_pt > 0)) {
/* out-of-band DTMF */
if (h323debug) {
ast_log(LOG_DTMF, "Begin sending out-of-band digit %c on %s\n", digit, c->name);
}
ast_rtp_senddigit_begin(pvt->rtp, digit);
ast_mutex_unlock(&pvt->lock);
} else if (pvt->txDtmfDigit != digit) {
/* in-band DTMF */
if (h323debug) {
ast_log(LOG_DTMF, "Begin sending inband digit %c on %s\n", digit, c->name);
}
pvt->txDtmfDigit = digit;
token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
ast_mutex_unlock(&pvt->lock);
h323_send_tone(token, digit);
if (token) {
free(token);
}
} else
ast_mutex_unlock(&pvt->lock);
oh323_update_info(c);
return 0;
}
/**
* Send (play) the specified digit to the channel.
*
*/
static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration)
{
struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
char *token;
if (!pvt) {
ast_log(LOG_ERROR, "No private structure?! This is bad\n");
return -1;
}
ast_mutex_lock(&pvt->lock);
if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && (pvt->dtmf_pt > 0)) {
/* out-of-band DTMF */
if (h323debug) {
ast_log(LOG_DTMF, "End sending out-of-band digit %c on %s, duration %d\n", digit, c->name, duration);
}
ast_rtp_senddigit_end(pvt->rtp, digit);
ast_mutex_unlock(&pvt->lock);
} else {
/* in-band DTMF */
if (h323debug) {
ast_log(LOG_DTMF, "End sending inband digit %c on %s, duration %d\n", digit, c->name, duration);
}
pvt->txDtmfDigit = ' ';
token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
ast_mutex_unlock(&pvt->lock);
h323_send_tone(token, ' ');
if (token) {
free(token);
}
}
oh323_update_info(c);
return 0;
}
/**
* Make a call over the specified channel to the specified
* destination.
* Returns -1 on error, 0 on success.
*/
static int oh323_call(struct ast_channel *c, char *dest, int timeout)
{
int res = 0;
struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
const char *addr;
char called_addr[1024];
if (h323debug) {
ast_log(LOG_DEBUG, "Calling to %s on %s\n", dest, c->name);
}
if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
return -1;
}
ast_mutex_lock(&pvt->lock);
if (!gatekeeper_disable) {
if (ast_strlen_zero(pvt->exten)) {
ast_copy_string(called_addr, dest, sizeof(called_addr));
} else {
snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
}
} else {
res = htons(pvt->sa.sin_port);
addr = ast_inet_ntoa(pvt->sa.sin_addr);
if (ast_strlen_zero(pvt->exten)) {
snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
} else {
snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
}
}
/* make sure null terminated */
called_addr[sizeof(called_addr) - 1] = '\0';
if (c->cid.cid_num)
ast_copy_string(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
if (c->cid.cid_name)
ast_copy_string(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
if (c->cid.cid_rdnis) {
ast_copy_string(pvt->options.cid_rdnis, c->cid.cid_rdnis, sizeof(pvt->options.cid_rdnis));
}
pvt->options.presentation = c->cid.cid_pres;
pvt->options.type_of_number = c->cid.cid_ton;
if ((addr = pbx_builtin_getvar_helper(c, "PRIREDIRECTREASON"))) {
if (!strcasecmp(addr, "UNKNOWN"))
pvt->options.redirect_reason = 0;
else if (!strcasecmp(addr, "BUSY"))
pvt->options.redirect_reason = 1;
else if (!strcasecmp(addr, "NO_REPLY"))
pvt->options.redirect_reason = 2;
else if (!strcasecmp(addr, "UNCONDITIONAL"))
pvt->options.redirect_reason = 15;
else
pvt->options.redirect_reason = -1;
} else
pvt->options.redirect_reason = -1;
pvt->options.transfer_capability = c->transfercapability;
/* indicate that this is an outgoing call */
pvt->outgoing = 1;
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", c->transfercapability, ast_transfercapability2str(c->transfercapability));
if (h323debug)
ast_log(LOG_DEBUG, "Placing outgoing call to %s, %d\n", called_addr, pvt->options.dtmfcodec);
ast_mutex_unlock(&pvt->lock);
res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
if (res) {
ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
return -1;
}
oh323_update_info(c);
return 0;
}
static int oh323_answer(struct ast_channel *c)
{
int res;
struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
char *token;
if (h323debug)
ast_log(LOG_DEBUG, "Answering on %s\n", c->name);
ast_mutex_lock(&pvt->lock);
token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
ast_mutex_unlock(&pvt->lock);
res = h323_answering_call(token, 0);
if (token)
free(token);
oh323_update_info(c);
if (c->_state != AST_STATE_UP) {
ast_setstate(c, AST_STATE_UP);
}
return res;
}
static int oh323_hangup(struct ast_channel *c)
{
struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
int q931cause = AST_CAUSE_NORMAL_CLEARING;
char *call_token;
if (h323debug)
ast_log(LOG_DEBUG, "Hanging up and scheduling destroy of call %s\n", c->name);
if (!c->tech_pvt) {
ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
return 0;
}
ast_mutex_lock(&pvt->lock);
/* Determine how to disconnect */
if (pvt->owner != c) {
ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
ast_mutex_unlock(&pvt->lock);
return 0;
}
pvt->owner = NULL;
c->tech_pvt = NULL;
if (c->hangupcause) {
q931cause = c->hangupcause;
} else {
const char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
if (cause) {
if (!strcmp(cause, "CONGESTION")) {
q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
} else if (!strcmp(cause, "BUSY")) {
q931cause = AST_CAUSE_USER_BUSY;
} else if (!strcmp(cause, "CHANISUNVAIL")) {
q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
} else if (!strcmp(cause, "NOANSWER")) {
q931cause = AST_CAUSE_NO_ANSWER;
} else if (!strcmp(cause, "CANCEL")) {
q931cause = AST_CAUSE_CALL_REJECTED;
}
}
}
/* Start the process if it's not already started */
if (!pvt->alreadygone && !pvt->hangupcause) {
call_token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
if (call_token) {
/* Release lock to eliminate deadlock */
ast_mutex_unlock(&pvt->lock);
if (h323_clear_call(call_token, q931cause)) {
ast_log(LOG_WARNING, "ClearCall failed.\n");
}
free(call_token);
ast_mutex_lock(&pvt->lock);
}
}
pvt->needdestroy = 1;
ast_mutex_unlock(&pvt->lock);
/* Update usage counter */
ast_module_unref(ast_module_info->self);
return 0;
}
static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
{
/* Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
struct ast_frame *f;
/* Only apply it for the first packet, we just need the correct ip/port */
if (pvt->options.nat) {
ast_rtp_setnat(pvt->rtp, pvt->options.nat);
pvt->options.nat = 0;
}
f = ast_rtp_read(pvt->rtp);
/* Don't send RFC2833 if we're not supposed to */
if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
return &ast_null_frame;
}
if (pvt->owner) {
/* We already hold the channel lock */
if (f->frametype == AST_FRAME_VOICE) {
if (f->subclass != pvt->owner->nativeformats) {
/* Try to avoid deadlock */
if (ast_channel_trylock(pvt->owner)) {
ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
return &ast_null_frame;
}
if (h323debug)
ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
pvt->owner->nativeformats = f->subclass;
pvt->nativeformats = f->subclass;
ast_set_read_format(pvt->owner, pvt->owner->readformat);
ast_set_write_format(pvt->owner, pvt->owner->writeformat);
ast_channel_unlock(pvt->owner);
}
/* Do in-band DTMF detection */
if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
if ((pvt->nativeformats & (AST_FORMAT_SLINEAR | AST_FORMAT_ALAW | AST_FORMAT_ULAW))) {
if (!ast_channel_trylock(pvt->owner)) {
f = ast_dsp_process(pvt->owner, pvt->vad, f);
ast_channel_unlock(pvt->owner);
}
else
ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
} else if (pvt->nativeformats && !pvt->noInbandDtmf) {
ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(f->subclass));
pvt->noInbandDtmf = 1;
}
if (f &&(f->frametype == AST_FRAME_DTMF)) {
if (h323debug)
ast_log(LOG_DTMF, "Received in-band digit %c.\n", f->subclass);
}
}
}
}
return f;
}
static struct ast_frame *oh323_read(struct ast_channel *c)
{
struct ast_frame *fr;
struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
ast_mutex_lock(&pvt->lock);
__oh323_update_info(c, pvt);
switch(c->fdno) {
case 0:
fr = oh323_rtp_read(pvt);
break;
case 1:
if (pvt->rtp)
fr = ast_rtcp_read(pvt->rtp);
else
fr = &ast_null_frame;
break;
default:
ast_log(LOG_ERROR, "Unable to handle fd %d on channel %s\n", c->fdno, c->name);
fr = &ast_null_frame;
break;
}
ast_mutex_unlock(&pvt->lock);
return fr;
}
static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
{
struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
int res = 0;
if (frame->frametype != AST_FRAME_VOICE) {
if (frame->frametype == AST_FRAME_IMAGE) {
return 0;
} else {
ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
return 0;
}
} else {
if (!(frame->subclass & c->nativeformats)) {
ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
frame->subclass, c->nativeformats, c->readformat, c->writeformat);
return 0;
}
}
if (pvt) {
ast_mutex_lock(&pvt->lock);
if (pvt->rtp && !pvt->recvonly)
res = ast_rtp_write(pvt->rtp, frame);
__oh323_update_info(c, pvt);
ast_mutex_unlock(&pvt->lock);
}
return res;
}
static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen)
{
struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
char *token = (char *)NULL;
int res = -1;
int got_progress;
ast_mutex_lock(&pvt->lock);
token = (pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL);
got_progress = pvt->got_progress;
if (condition == AST_CONTROL_PROGRESS)
pvt->got_progress = 1;
else if ((condition == AST_CONTROL_BUSY) || (condition == AST_CONTROL_CONGESTION))
pvt->alreadygone = 1;
ast_mutex_unlock(&pvt->lock);
if (h323debug)
ast_log(LOG_DEBUG, "OH323: Indicating %d on %s\n", condition, token);
switch(condition) {
case AST_CONTROL_RINGING:
if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
h323_send_alerting(token);
res = (got_progress ? 0 : -1); /* Do not simulate any audio tones if we got PROGRESS message */
}
break;
case AST_CONTROL_PROGRESS:
if (c->_state != AST_STATE_UP) {
/* Do not send PROGRESS message more than once */
if (!got_progress)
h323_send_progress(token);
res = 0;
}
break;
case AST_CONTROL_BUSY:
if (c->_state != AST_STATE_UP) {
h323_answering_call(token, 1);
ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
res = 0;
}
break;
case AST_CONTROL_CONGESTION:
if (c->_state != AST_STATE_UP) {
h323_answering_call(token, 1);
ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
res = 0;
}
break;
case AST_CONTROL_HOLD:
ast_moh_start(c, data, NULL);
res = 0;
break;
case AST_CONTROL_UNHOLD:
ast_moh_stop(c);
res = 0;
break;
case AST_CONTROL_SRCUPDATE:
ast_rtp_new_source(pvt->rtp);
res = 0;
break;
case AST_CONTROL_PROCEEDING:
case -1:
break;
default:
ast_log(LOG_WARNING, "OH323: Don't know how to indicate condition %d on %s\n", condition, token);
break;
}
if (h323debug)
ast_log(LOG_DEBUG, "OH323: Indicated %d on %s, res=%d\n", condition, token, res);
if (token)
free(token);
oh323_update_info(c);
return res;
}
static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
{
struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
ast_mutex_lock(&pvt->lock);
if (pvt->owner != oldchan) {
ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
return -1;
}
pvt->owner = newchan;
ast_mutex_unlock(&pvt->lock);
return 0;
}
static int __oh323_rtp_create(struct oh323_pvt *pvt)
{
struct in_addr our_addr;
if (pvt->rtp)
return 0;
if (ast_find_ourip(&our_addr, bindaddr)) {
ast_mutex_unlock(&pvt->lock);
ast_log(LOG_ERROR, "Unable to locate local IP address for RTP stream\n");
return -1;
}
pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, our_addr);
if (!pvt->rtp) {
ast_mutex_unlock(&pvt->lock);
ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
return -1;
}
if (h323debug)
ast_log(LOG_DEBUG, "Created RTP channel\n");
ast_rtp_settos(pvt->rtp, tos);
if (h323debug)
ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
ast_rtp_setnat(pvt->rtp, pvt->options.nat);
if (pvt->dtmf_pt > 0)
ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt, "audio", "telephone-event", 0);
if (pvt->peercapability)
ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
ast_jb_configure(pvt->owner, &global_jbconf);
pvt->owner->fds[0] = ast_rtp_fd(pvt->rtp);
pvt->owner->fds[1] = ast_rtcp_fd(pvt->rtp);
ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
ast_channel_unlock(pvt->owner);
} else
pvt->update_rtp_info = 1;
return 0;
}
/* Private structure should be locked on a call */
static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
{