4 * OpenH323 Channel Driver for ASTERISK PBX.
6 * For The NuFone Network
8 * This code has been derived from code created by
9 * Michael Manousos and Mark Spencer
11 * This file is part of the chan_h323 driver for Asterisk
13 * chan_h323 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * chan_h323 is distributed WITHOUT ANY WARRANTY; without even
19 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <asterisk/lock.h>
33 #include <asterisk/logger.h>
34 #include <asterisk/channel.h>
35 #include <asterisk/channel_pvt.h>
36 #include <asterisk/config.h>
37 #include <asterisk/module.h>
38 #include <asterisk/pbx.h>
39 #include <asterisk/options.h>
40 #include <asterisk/lock.h>
41 #include <asterisk/sched.h>
42 #include <asterisk/io.h>
43 #include <asterisk/rtp.h>
44 #include <asterisk/acl.h>
45 #include <asterisk/callerid.h>
46 #include <asterisk/cli.h>
47 #include <asterisk/dsp.h>
48 #include <sys/socket.h>
55 #include <sys/signal.h>
56 #include <netinet/ip.h>
59 #include "chan_h323.h"
61 /** String variables required by ASTERISK */
62 static char *type = "H323";
63 static char *desc = "The NuFone Network's Open H.323 Channel Driver";
64 static char *tdesc = "The NuFone Network's Open H.323 Channel Driver";
65 static char *config = "h323.conf";
67 static char default_context[AST_MAX_EXTENSION];
69 /** H.323 configuration values */
70 static char gatekeeper[100];
71 static int gatekeeper_disable = 1;
72 static int gatekeeper_discover = 0;
73 static int manual = 0;
75 static int port = 1720;
77 static int gkroute = 0;
79 /* Just about everybody seems to support ulaw, so make it a nice default */
80 static int capability = AST_FORMAT_ULAW;
85 static int dtmfmode = H323_DTMF_RFC2833;
87 static char secret[50];
89 /** Private structure of a OpenH323 channel */
91 pthread_mutex_t lock; /* Channel private lock */
92 call_options_t call_opt; /* Options to be used during call setup */
93 int alreadygone; /* Whether or not we've already been destroyed by or peer */
94 int needdestroy; /* if we need to be destroyed */
95 call_details_t cd; /* Call details */
96 struct ast_channel *owner; /* Who owns us */
97 int capability; /* Special capability */
98 int nonCodecCapability;
99 int outgoing; /* Outgoing or incoming call? */
100 int nat; /* Are we talking to a NAT EP?*/
101 int bridge; /* Determine of we should native bridge or not*/
102 char exten[AST_MAX_EXTENSION]; /* Requested extension */
103 char context[AST_MAX_EXTENSION]; /* Context where to start */
104 char username[81]; /* H.323 alias using this channel */
105 char accountcode[256]; /* Account code */
106 int amaflags; /* AMA Flags */
107 char callerid[80]; /* Caller*ID if available */
108 struct ast_rtp *rtp; /* RTP Session */
110 struct ast_dsp *vad; /* Used for in-band DTMF detection */
111 struct oh323_pvt *next; /* Next channel in list */
114 static struct ast_user_list {
115 struct oh323_user *users;
116 pthread_mutex_t lock;
117 } userl = { NULL, AST_MUTEX_INITIALIZER };
119 static struct ast_peer_list {
120 struct oh323_peer *peers;
121 pthread_mutex_t lock;
122 } peerl = { NULL, AST_MUTEX_INITIALIZER };
124 static struct ast_alias_list {
125 struct oh323_alias *aliases;
126 pthread_mutex_t lock;
127 } aliasl = { NULL, AST_MUTEX_INITIALIZER };
129 /** Asterisk RTP stuff*/
130 static struct sched_context *sched;
131 static struct io_context *io;
133 /** Protect the interface list (of oh323_pvt's) */
134 static pthread_mutex_t iflock = AST_MUTEX_INITIALIZER;
136 /** Usage counter and associated lock */
137 static int usecnt =0;
138 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
140 /* Protect the monitoring thread, so only one process can kill or start it, and not
141 when it's doing something critical. */
142 static pthread_mutex_t monlock = AST_MUTEX_INITIALIZER;
144 /* This is the thread for the monitor which checks for input on the channels
145 which are not currently in use. */
146 static pthread_t monitor_thread = 0;
148 static int restart_monitor(void);
150 static void __oh323_destroy(struct oh323_pvt *p)
152 struct oh323_pvt *cur, *prev = NULL;
155 ast_rtp_destroy(p->rtp);
158 /* Unlink us from the owner if we have one */
160 ast_pthread_mutex_lock(&p->owner->lock);
161 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
162 p->owner->pvt->pvt = NULL;
163 ast_pthread_mutex_unlock(&p->owner->lock);
169 prev->next = cur->next;
178 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
183 static void oh323_destroy(struct oh323_pvt *p)
185 ast_pthread_mutex_lock(&iflock);
187 ast_pthread_mutex_unlock(&iflock);
190 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
192 struct oh323_alias *alias;
194 alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
197 memset(alias, 0, sizeof(struct oh323_alias));
198 strncpy(alias->name, name, sizeof(alias->name)-1);
201 if (!strcasecmp(v->name, "e164")) {
202 strncpy(alias->e164, v->value, sizeof(alias->e164)-1);
203 } else if (!strcasecmp(v->name, "prefix")) {
204 strncpy(alias->prefix, v->value, sizeof(alias->prefix)-1);
205 } else if (!strcasecmp(v->name, "context")) {
206 strncpy(alias->context, v->value, sizeof(alias->context)-1);
207 } else if (!strcasecmp(v->name, "secret")) {
208 strncpy(alias->prefix, v->value, sizeof(alias->secret)-1);
216 static struct oh323_user *build_user(char *name, struct ast_variable *v)
218 struct oh323_user *user;
221 user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
223 memset(user, 0, sizeof(struct oh323_user));
224 strncpy(user->name, name, sizeof(user->name)-1);
226 /* set the usage flag to a sane starting value*/
228 /* Assume we can native bridge */
232 if (!strcasecmp(v->name, "context")) {
233 strncpy(user->context, v->value, sizeof(user->context)-1);
234 } else if (!strcasecmp(v->name, "bridge")) {
235 user->bridge = ast_true(v->value);
236 } else if (!strcasecmp(v->name, "noFastStart")) {
237 user->noFastStart = ast_true(v->value);
238 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
239 user->noH245Tunneling = ast_true(v->value);
240 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
241 user->noSilenceSuppression = ast_true(v->value);
242 } else if (!strcasecmp(v->name, "secret")) {
243 strncpy(user->secret, v->value, sizeof(user->secret)-1);
244 } else if (!strcasecmp(v->name, "accountcode")) {
245 strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
246 } else if (!strcasecmp(v->name, "incominglimit")) {
247 user->incominglimit = atoi(v->value);
248 if (user->incominglimit < 0)
249 user->incominglimit = 0;
250 } else if (!strcasecmp(v->name, "host")) {
251 if (!strcasecmp(v->value, "dynamic")) {
252 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
255 } else if (ast_get_ip(&user->addr, v->value)) {
259 } else if (!strcasecmp(v->name, "amaflags")) {
260 format = ast_cdr_amaflags2int(v->value);
262 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
264 user->amaflags = format;
274 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
276 struct oh323_peer *peer;
277 struct oh323_peer *prev;
281 ast_pthread_mutex_lock(&peerl.lock);
285 if (!strcasecmp(peer->name, name)) {
294 /* Already in the list, remove it and it will be added back (or FREE'd) */
296 prev->next = peer->next;
298 peerl.peers = peer->next;
300 ast_pthread_mutex_unlock(&peerl.lock);
302 ast_pthread_mutex_unlock(&peerl.lock);
303 peer = malloc(sizeof(struct oh323_peer));
304 memset(peer, 0, sizeof(struct oh323_peer));
308 strncpy(peer->name, name, sizeof(peer->name)-1);
311 /* set the usage flag to a sane starting value*/
315 if (!strcasecmp(v->name, "context")) {
316 strncpy(peer->context, v->value, sizeof(peer->context)-1);
317 } else if (!strcasecmp(v->name, "bridge")) {
318 peer->bridge = ast_true(v->value);
319 } else if (!strcasecmp(v->name, "noFastStart")) {
320 peer->noFastStart = ast_true(v->value);
321 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
322 peer->noH245Tunneling = ast_true(v->value);
323 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
324 peer->noSilenceSuppression = ast_true(v->value);
325 } else if (!strcasecmp(v->name, "outgoinglimit")) {
326 peer->outgoinglimit = atoi(v->value);
327 if (peer->outgoinglimit > 0)
328 peer->outgoinglimit = 0;
329 } else if (!strcasecmp(v->name, "host")) {
330 if (!strcasecmp(v->value, "dynamic")) {
331 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
335 if (ast_get_ip(&peer->addr, v->value)) {
349 * Send (play) the specified digit to the channel.
352 static int oh323_digit(struct ast_channel *c, char digit)
354 struct oh323_pvt *p = c->pvt->pvt;
355 if (p && p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
356 ast_rtp_senddigit(p->rtp, digit);
358 /* If in-band DTMF is desired, send that */
359 if (p->dtmfmode & H323_DTMF_INBAND)
360 h323_send_tone(p->cd.call_token, digit);
366 * Make a call over the specified channel to the specified
367 * destination. This function will parse the destination string
368 * and determine the address-number to call.
369 * Return -1 on error, 0 on success.
371 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
374 struct oh323_pvt *p = c->pvt->pvt;
375 char called_addr[256];
378 ast_log(LOG_DEBUG, "dest=%s, timeout=%d.\n", dest, timeout);
380 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
381 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
385 /* set the connect port */
386 p->call_opt.port = port;
391 /* Clear the call token */
392 if ((p->cd).call_token == NULL)
393 (p->cd).call_token = (char *)malloc(128);
395 memset((char *)(p->cd).call_token, 0, 128);
397 if (p->cd.call_token == NULL) {
398 ast_log(LOG_ERROR, "Not enough memory.\n");
402 /* Build the address to call */
403 memset(called_addr, 0, sizeof(dest));
404 memcpy(called_addr, dest, sizeof(called_addr));
406 res = h323_make_call(called_addr, &(p->cd), p->call_opt);
409 printf("h323_make_call failed\n");
413 ast_setstate(c, AST_STATE_RINGING);
418 static int oh323_answer(struct ast_channel *c)
422 struct oh323_pvt *p = c->pvt->pvt;
424 res = h323_answering_call(p->cd.call_token, 0);
426 if (c->_state != AST_STATE_UP)
427 ast_setstate(c, AST_STATE_UP);
432 static int oh323_hangup(struct ast_channel *c)
434 struct oh323_pvt *p = c->pvt->pvt;
437 ast_log(LOG_DEBUG, "oh323_hangup(%s)\n", c->name);
439 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
442 ast_pthread_mutex_lock(&p->lock);
443 /* Determine how to disconnect */
445 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
446 ast_pthread_mutex_unlock(&p->lock);
449 if (!c || (c->_state != AST_STATE_UP))
454 /* Free dsp used for in-band DTMF detection */
456 ast_dsp_free(p->vad);
462 /* Start the process if it's not already started */
463 if (!p->alreadygone) {
464 if (h323_clear_call((p->cd).call_token))
465 ast_log(LOG_DEBUG, "ClearCall failed.\n");
470 ast_pthread_mutex_unlock(&p->lock);
474 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *p)
476 /* Retrieve audio/etc from channel. Assumes p->lock is already held. */
478 static struct ast_frame null_frame = { AST_FRAME_NULL, };
479 f = ast_rtp_read(p->rtp);
480 /* Don't send RFC2833 if we're not supposed to */
481 if (f && (f->frametype == AST_FRAME_DTMF) && !(p->dtmfmode & H323_DTMF_RFC2833))
484 /* We already hold the channel lock */
485 if (f->frametype == AST_FRAME_VOICE) {
486 if (f->subclass != p->owner->nativeformats) {
487 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
488 p->owner->nativeformats = f->subclass;
489 ast_set_read_format(p->owner, p->owner->readformat);
490 ast_set_write_format(p->owner, p->owner->writeformat);
493 /* Do in-band DTMF detection */
494 if (p->dtmfmode & H323_DTMF_INBAND) {
495 f = ast_dsp_process(p->owner,p->vad,f,0);
496 if (f->frametype == AST_FRAME_DTMF)
497 ast_log(LOG_DEBUG, "Got in-band digit %c.\n", f->subclass);
507 static struct ast_frame *oh323_read(struct ast_channel *c)
509 struct ast_frame *fr;
510 struct oh323_pvt *p = c->pvt->pvt;
511 ast_pthread_mutex_lock(&p->lock);
512 fr = oh323_rtp_read(p);
513 ast_pthread_mutex_unlock(&p->lock);
517 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
519 struct oh323_pvt *p = c->pvt->pvt;
521 if (frame->frametype != AST_FRAME_VOICE) {
522 if (frame->frametype == AST_FRAME_IMAGE)
525 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
529 if (!(frame->subclass & c->nativeformats)) {
530 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
531 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
536 ast_pthread_mutex_lock(&p->lock);
538 res = ast_rtp_write(p->rtp, frame);
540 ast_pthread_mutex_unlock(&p->lock);
545 /** FIXME: Can I acutally use this or does Open H.323 take care of everything? */
546 static int oh323_indicate(struct ast_channel *c, int condition)
549 struct oh323_pvt *p = c->pvt->pvt;
552 case AST_CONTROL_RINGING:
553 if (c->_state == AST_STATE_RING) {
554 // transmit_response(p, "180 Ringing", &p->initreq);
558 case AST_CONTROL_BUSY:
559 if (c->_state != AST_STATE_UP) {
560 // transmit_response(p, "600 Busy everywhere", &p->initreq);
562 ast_softhangup(c, AST_SOFTHANGUP_DEV);
566 case AST_CONTROL_CONGESTION:
567 if (c->_state != AST_STATE_UP) {
568 // transmit_response(p, "486 Busy here", &p->initreq);
570 ast_softhangup(c, AST_SOFTHANGUP_DEV);
577 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
583 // FIXME: WTF is this? Do I need this???
584 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
586 struct oh323_pvt *p = newchan->pvt->pvt;
588 ast_pthread_mutex_lock(&p->lock);
589 if (p->owner != oldchan) {
590 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
594 ast_pthread_mutex_unlock(&p->lock);
598 static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char *host)
600 struct ast_channel *tmp;
602 tmp = ast_channel_alloc(1);
606 snprintf(tmp->name, sizeof(tmp->name)-1, "H323/%s", host);
607 tmp->nativeformats = i->capability;
608 if (!tmp->nativeformats)
609 tmp->nativeformats = capability;
610 fmt = ast_best_codec(tmp->nativeformats);
612 tmp->fds[0] = ast_rtp_fd(i->rtp);
613 ast_setstate(tmp, state);
615 if (state == AST_STATE_RING)
618 tmp->writeformat = fmt;
619 tmp->pvt->rawwriteformat = fmt;
620 tmp->readformat = fmt;
621 tmp->pvt->rawreadformat = fmt;
623 /* Allocate dsp for in-band DTMF support */
624 if (i->dtmfmode & H323_DTMF_INBAND) {
625 i->vad = ast_dsp_new();
626 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
629 /* Register the OpenH323 channel's functions. */
631 tmp->pvt->send_digit = oh323_digit;
632 tmp->pvt->call = oh323_call;
633 tmp->pvt->hangup = oh323_hangup;
634 tmp->pvt->answer = oh323_answer;
635 tmp->pvt->read = oh323_read;
636 tmp->pvt->write = oh323_write;
637 tmp->pvt->indicate = oh323_indicate;
638 tmp->pvt->fixup = oh323_fixup;
639 // tmp->pvt->bridge = ast_rtp_bridge;
641 /* Set the owner of this channel */
644 ast_pthread_mutex_lock(&usecnt_lock);
646 ast_pthread_mutex_unlock(&usecnt_lock);
647 ast_update_use_count();
648 strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
649 strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
651 if (strlen(i->callerid))
652 tmp->callerid = strdup(i->callerid);
653 if (state != AST_STATE_DOWN) {
654 if (ast_pbx_start(tmp)) {
655 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
661 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
665 static struct oh323_pvt *oh323_alloc(int callid)
669 p = malloc(sizeof(struct oh323_pvt));
671 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
675 /* Keep track of stuff */
676 memset(p, 0, sizeof(struct oh323_pvt));
677 p->rtp = ast_rtp_new(NULL, NULL);
679 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
683 ast_rtp_settos(p->rtp, tos);
684 ast_pthread_mutex_init(&p->lock);
686 p->cd.call_reference = callid;
689 p->dtmfmode = dtmfmode;
690 if (p->dtmfmode & H323_DTMF_RFC2833)
691 p->nonCodecCapability |= AST_RTP_DTMF;
693 /* Add to interface list */
694 ast_pthread_mutex_lock(&iflock);
697 ast_pthread_mutex_unlock(&iflock);
701 static struct oh323_pvt *find_call(int call_reference)
705 ast_pthread_mutex_lock(&iflock);
709 if (p->cd.call_reference == call_reference) {
711 ast_pthread_mutex_unlock(&iflock);
716 ast_pthread_mutex_unlock(&iflock);
721 static struct ast_channel *oh323_request(char *type, int format, void *data)
726 struct ast_channel *tmpc = NULL;
731 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
734 format &= capability;
736 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
740 strncpy(tmp, dest, sizeof(tmp) - 1);
741 host = strchr(tmp, '@');
754 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
758 /* Assign a default capability */
759 p->capability = capability;
762 if (p->dtmfmode & H323_DTMF_RFC2833)
763 p->nonCodecCapability |= AST_RTP_DTMF;
765 p->nonCodecCapability &= ~AST_RTP_DTMF;
769 ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, p->username);
772 strncpy(p->username, ext, sizeof(p->username) - 1);
773 tmpc = oh323_new(p, AST_STATE_DOWN, host);
782 struct oh323_alias *find_alias(const char *source_aliases)
784 struct oh323_alias *a;
790 if (!strcasecmp(a->name, source_aliases)) {
798 struct oh323_user *find_user(const char *source_aliases)
800 struct oh323_user *u;
806 if (!strcasecmp(u->name, source_aliases)) {
815 struct oh323_peer *find_peer(char *dest_peer)
817 struct oh323_peer *p;
822 if (!strcasecmp(p->name, dest_peer)) {
832 * Callback for sending digits from H.323 up to asterisk
835 int send_digit(unsigned call_reference, char digit)
840 ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
841 p = find_call(call_reference);
844 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
847 memset(&f, 0, sizeof(f));
848 f.frametype = AST_FRAME_DTMF;
855 f.src = "SEND_DIGIT";
857 return ast_queue_frame(p->owner, &f, 1);
861 * Call-back function that gets called when any H.323 connection is made
863 * Returns the local RTP port
865 int create_connection(unsigned call_reference)
868 struct sockaddr_in us;
870 p = find_call(call_reference);
873 ast_log(LOG_ERROR, "Unable to allocate private structure, this is very bad.\n");
877 /* figure out our local RTP port and tell the H.323 stack about it*/
878 ast_rtp_get_us(p->rtp, &us);
879 return ntohs(us.sin_port);
883 * Call-back function for incoming calls
885 * Returns 1 on success
888 int setup_incoming_call(call_details_t cd)
891 struct oh323_pvt *p = NULL;
892 struct ast_channel *c = NULL;
893 struct oh323_user *user = NULL;
894 struct oh323_alias *alias = NULL;
896 /* allocate the call*/
897 p = oh323_alloc(cd.call_reference);
900 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
904 /* Populate the call details in the private structure */
905 p->cd.call_token = cd.call_token;
906 p->cd.call_source_aliases = cd.call_source_aliases;
907 p->cd.call_dest_alias = cd.call_dest_alias;
908 p->cd.call_source_e164 = cd.call_source_e164;
909 p->cd.call_dest_e164 = cd.call_dest_e164;
912 printf(" == Setting up Call\n");
913 printf(" -- Calling party name: %s\n", p->cd.call_source_aliases);
914 printf(" -- Calling party number: %s\n", p->cd.call_source_e164);
915 printf(" -- Called party name: %s\n", p->cd.call_dest_alias);
916 printf(" -- Called party number: %s\n", p->cd.call_dest_e164);
919 /* Decide if we are allowing Gatekeeper routed calls*/
920 if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
922 if (strlen(cd.call_dest_e164)) {
923 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
924 strncpy(p->context, default_context, sizeof(p->context)-1);
926 alias = find_alias(cd.call_dest_alias);
929 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
932 printf("Alias found: %s and %s\n", alias->name, alias->context);
934 strncpy(p->exten, alias->name, sizeof(p->exten)-1);
935 strncpy(p->context, alias->context, sizeof(p->context)-1);
938 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
941 /* Either this call is not from the Gatekeeper
942 or we are not allowing gk routed calls */
944 user = find_user(cd.call_source_aliases);
947 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
948 if (strlen(p->cd.call_dest_e164)) {
949 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
951 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
953 if (!strlen(default_context)) {
954 ast_log(LOG_ERROR, "Call from user '%s' rejected due to no default context\n", p->cd.call_source_aliases);
957 strncpy(p->context, default_context, sizeof(p->context)-1);
958 ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, p->context);
960 if (user->incominglimit > 0) {
961 if (user->inUse >= user->incominglimit) {
962 ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
966 strncpy(p->context, user->context, sizeof(p->context)-1);
967 p->bridge = user->bridge;
969 if (strlen(user->callerid) && strlen(p->callerid))
970 strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
972 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
974 if (strlen(p->cd.call_dest_e164)) {
975 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
977 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
980 if (strlen(user->accountcode)) {
981 strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
984 /* Increment the usage counter */
989 /* allocate a channel and tell asterisk about it */
990 c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
993 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1001 * Call-back function to establish an outgoing H.323 call
1003 * Returns 1 on success
1005 int setup_outgoing_call(call_details_t cd)
1011 if (p->inUse >= p->outgoinglimit) {
1012 ast_log(LOG_ERROR, "Call to %s rejected due to usage limit of %d outgoing channels\n", p->name, p->inUse);
1017 ast_log(LOG_ERROR, "Rejecting call: peer %s not found\n", dest_peer);
1023 * Call-back function that gets called for each rtp channel opened
1027 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort)
1029 struct oh323_pvt *p = NULL;
1030 struct sockaddr_in them;
1032 /* Find the call or allocate a private structure if call not found */
1033 p = find_call(call_reference);
1036 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1040 them.sin_family = AF_INET;
1041 them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
1042 them.sin_port = htons(remotePort);
1043 ast_rtp_set_peer(p->rtp, &them);
1049 * Call-back function to signal asterisk that the channel has been answered
1052 void connection_made(unsigned call_reference)
1054 struct ast_channel *c = NULL;
1055 struct oh323_pvt *p = NULL;
1057 p = find_call(call_reference);
1060 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1064 printf("Channel has no owner\n");
1069 ast_setstate(c, AST_STATE_UP);
1074 * Call-back function to cleanup communication
1077 void cleanup_connection(call_details_t cd)
1079 struct oh323_pvt *p = NULL;
1080 // struct oh323_peer *peer = NULL;
1081 struct oh323_user *user = NULL;
1082 struct ast_rtp *rtp = NULL;
1084 ast_log(LOG_DEBUG, "Cleaning up our mess\n");
1086 p = find_call(cd.call_reference);
1092 /* Decrement usage counter */
1094 user = find_user(cd.call_source_aliases);
1102 peer = find_peer(cd.call_dest_alias);
1105 user = find_user(cd.call_source_aliases);
1113 /* Immediately stop RTP */
1114 ast_rtp_destroy(rtp);
1121 ast_queue_hangup(p->owner, 1);
1127 static void *do_monitor(void *data)
1130 struct oh323_pvt *oh323 = NULL;
1133 /* Check for interfaces needing to be killed */
1134 ast_pthread_mutex_lock(&iflock);
1138 if (oh323->needdestroy) {
1139 __oh323_destroy(oh323);
1142 oh323 = oh323->next;
1144 ast_pthread_mutex_unlock(&iflock);
1146 pthread_testcancel();
1148 /* Wait for sched or io */
1149 res = ast_sched_wait(sched);
1150 if ((res < 0) || (res > 1000))
1152 res = ast_io_wait(io, res);
1153 ast_pthread_mutex_lock(&monlock);
1155 ast_sched_runq(sched);
1156 ast_pthread_mutex_unlock(&monlock);
1163 static int restart_monitor(void)
1165 /* If we're supposed to be stopped -- stay stopped */
1166 if (monitor_thread == -2)
1168 if (ast_pthread_mutex_lock(&monlock)) {
1169 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1172 if (monitor_thread == pthread_self()) {
1173 ast_pthread_mutex_unlock(&monlock);
1174 ast_log(LOG_WARNING, "Cannot kill myself\n");
1177 if (monitor_thread) {
1178 /* Wake up the thread */
1179 pthread_kill(monitor_thread, SIGURG);
1181 /* Start a new monitor */
1182 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1183 ast_pthread_mutex_unlock(&monlock);
1184 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1188 ast_pthread_mutex_unlock(&monlock);
1192 static int h323_do_trace(int fd, int argc, char *argv[])
1195 return RESULT_SHOWUSAGE;
1197 h323_debug(1, atoi(argv[2]));
1198 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1199 return RESULT_SUCCESS;
1202 static int h323_no_trace(int fd, int argc, char *argv[])
1205 return RESULT_SHOWUSAGE;
1208 ast_cli(fd, "H.323 trace disabled\n");
1209 return RESULT_SUCCESS;
1212 static int h323_do_debug(int fd, int argc, char *argv[])
1215 return RESULT_SHOWUSAGE;
1218 ast_cli(fd, "H323 debug enabled\n");
1219 return RESULT_SUCCESS;
1222 static int h323_no_debug(int fd, int argc, char *argv[])
1225 return RESULT_SHOWUSAGE;
1228 ast_cli(fd, "H323 Debug disabled\n");
1229 return RESULT_SUCCESS;
1232 static int h323_gk_cycle(int fd, int argc, char *argv[])
1235 return RESULT_SHOWUSAGE;
1239 /* Possibly register with a GK */
1240 if (!gatekeeper_disable) {
1241 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1242 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1247 return RESULT_SUCCESS;
1251 static char trace_usage[] =
1252 "Usage: h.323 trace <level num>\n"
1253 " Enables H.323 stack tracing for debugging purposes\n";
1255 static char no_trace_usage[] =
1256 "Usage: h.323 no trace\n"
1257 " Disables H.323 stack tracing for debugging purposes\n";
1259 static char debug_usage[] =
1260 "Usage: h.323 debug\n"
1261 " Enables chan_h323 debug output\n";
1263 static char no_debug_usage[] =
1264 "Usage: h.323 no debug\n"
1265 " Disables chan_h323 debug output\n";
1267 static char show_codec_usage[] =
1268 "Usage: h.323 show codec\n"
1269 " Shows all enabled codecs\n";
1271 static char show_cycle_usage[] =
1272 "Usage: h.323 gk cycle\n"
1273 " Manually re-register with the Gatekeper\n";
1276 static struct ast_cli_entry cli_trace =
1277 { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1278 static struct ast_cli_entry cli_no_trace =
1279 { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1280 static struct ast_cli_entry cli_debug =
1281 { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
1282 static struct ast_cli_entry cli_no_debug =
1283 { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
1284 static struct ast_cli_entry cli_show_codecs =
1285 { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1286 static struct ast_cli_entry cli_gk_cycle =
1287 { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1295 struct ast_config *cfg;
1296 struct ast_variable *v;
1297 struct oh323_peer *peer = NULL;
1298 struct oh323_user *user = NULL;
1299 struct oh323_alias *alias = NULL;
1304 cfg = ast_load(config);
1306 /* We *must* have a config file otherwise stop immediately */
1308 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
1313 dtmfmode = H323_DTMF_RFC2833;
1315 memset(&bindaddr, 0, sizeof(bindaddr));
1317 v = ast_variable_browse(cfg, "general");
1319 /* Create the interface list */
1320 if (!strcasecmp(v->name, "jitter")) {
1321 jitter = (int) strtol(v->value, NULL, 10);
1322 if (jitter < 20 || jitter > 10000) {
1323 ast_log(LOG_NOTICE, "Invalid jitter value! Valid range: 20ms to 10000ms. Recommended: 100ms");
1327 } else if (!strcasecmp(v->name, "port")) {
1328 port = (int)strtol(v->value, NULL, 10);
1329 } else if (!strcasecmp(v->name, "bindaddr")) {
1330 if (!(hp = gethostbyname(v->value))) {
1331 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
1333 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1335 } else if (!strcasecmp(v->name, "allow")) {
1336 format = ast_getformatbyname(v->value);
1338 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1340 capability |= format;
1341 } else if (!strcasecmp(v->name, "disallow")) {
1342 format = ast_getformatbyname(v->value);
1344 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1346 capability &= ~format;
1347 } else if (!strcasecmp(v->name, "tos")) {
1348 if (sscanf(v->value, "%i", &format) == 1)
1349 tos = format & 0xff;
1350 else if (!strcasecmp(v->value, "lowdelay"))
1351 tos = IPTOS_LOWDELAY;
1352 else if (!strcasecmp(v->value, "throughput"))
1353 tos = IPTOS_THROUGHPUT;
1354 else if (!strcasecmp(v->value, "reliability"))
1355 tos = IPTOS_RELIABILITY;
1356 else if (!strcasecmp(v->value, "mincost"))
1357 tos = IPTOS_MINCOST;
1358 else if (!strcasecmp(v->value, "none"))
1361 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
1362 } else if (!strcasecmp(v->name, "gatekeeper")) {
1363 if (!strcasecmp(v->value, "DISABLE")) {
1364 gatekeeper_disable = 1;
1366 } else if (!strcasecmp(v->value, "DISCOVER")) {
1367 gatekeeper_disable = 0;
1368 gatekeeper_discover = 1;
1371 gatekeeper_disable = 0;
1373 strncpy(gatekeeper, v->value, sizeof(gatekeeper)-1);
1375 } else if (!strcasecmp(v->name, "secret")) {
1376 strncpy(secret, v->value, sizeof(secret)-1);
1377 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
1378 gkroute = ast_true(v->value);
1379 } else if (!strcasecmp(v->name, "context")) {
1380 strncpy(default_context, v->value, sizeof(default_context)-1);
1381 printf(" == Setting default context to %s\n", default_context);
1382 } else if (!strcasecmp(v->name, "manual")) {
1383 manual = ast_true(v->value);
1384 } else if (!strcasecmp(v->name, "dtmfmode")) {
1385 if (!strcasecmp(v->value, "inband"))
1386 dtmfmode=H323_DTMF_INBAND;
1387 else if (!strcasecmp(v->value, "rfc2833"))
1388 dtmfmode = H323_DTMF_RFC2833;
1390 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1391 dtmfmode = H323_DTMF_RFC2833;
1397 cat = ast_category_browse(cfg, NULL);
1399 if (strcasecmp(cat, "general")) {
1400 utype = ast_variable_retrieve(cfg, cat, "type");
1402 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
1403 user = build_user(cat, ast_variable_browse(cfg, cat));
1405 ast_pthread_mutex_lock(&userl.lock);
1406 user->next = userl.users;
1408 ast_pthread_mutex_unlock(&userl.lock);
1410 } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
1411 peer = build_peer(cat, ast_variable_browse(cfg, cat));
1413 ast_pthread_mutex_lock(&peerl.lock);
1414 peer->next = peerl.peers;
1416 ast_pthread_mutex_unlock(&peerl.lock);
1418 } else if (!strcasecmp(utype, "h323")) {
1419 alias = build_alias(cat, ast_variable_browse(cfg, cat));
1421 ast_pthread_mutex_lock(&aliasl.lock);
1422 alias->next = aliasl.aliases;
1423 aliasl.aliases = alias;
1424 ast_pthread_mutex_unlock(&aliasl.lock);
1427 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
1430 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
1432 cat = ast_category_browse(cfg, cat);
1435 /* Register our H.323 aliases if any*/
1437 if (h323_set_alias(alias)) {
1438 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
1441 alias = alias->next;
1444 /* Add some capabilities */
1445 if(h323_set_capability(capability, dtmfmode)) {
1446 ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
1450 printf("CONTEXT RELOAD: [%s]\n", default_context);
1457 void delete_users(void)
1459 struct oh323_user *user, *userlast;
1460 struct oh323_peer *peer;
1462 /* Delete all users */
1463 ast_pthread_mutex_lock(&userl.lock);
1464 for (user=userl.users;user;) {
1470 ast_pthread_mutex_unlock(&userl.lock);
1471 ast_pthread_mutex_lock(&peerl.lock);
1472 for (peer=peerl.peers;peer;) {
1473 /* Assume all will be deleted, and we'll find out for sure later */
1477 ast_pthread_mutex_unlock(&peerl.lock);
1480 void delete_aliases(void)
1482 struct oh323_alias *alias, *aliaslast;
1484 /* Delete all users */
1485 ast_pthread_mutex_lock(&aliasl.lock);
1486 for (alias=aliasl.aliases;alias;) {
1491 aliasl.aliases=NULL;
1492 ast_pthread_mutex_unlock(&aliasl.lock);
1495 void prune_peers(void)
1497 /* Prune peers who still are supposed to be deleted */
1498 struct oh323_peer *peer, *peerlast, *peernext;
1499 ast_pthread_mutex_lock(&peerl.lock);
1501 for (peer=peerl.peers;peer;) {
1502 peernext = peer->next;
1506 peerlast->next = peernext;
1508 peerl.peers = peernext;
1513 ast_pthread_mutex_unlock(&peerl.lock);
1522 if (strlen(gatekeeper)) {
1528 /* Possibly register with a GK */
1529 if (!gatekeeper_disable) {
1530 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1531 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1541 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
1543 struct oh323_pvt *p;
1545 if (p && p->rtp && p->bridge)
1550 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp)
1552 struct oh323_pvt *p;
1553 struct sockaddr_in them;
1554 struct sockaddr_in us;
1561 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
1565 ast_rtp_get_peer(rtp, &them);
1566 printf("peer is now: %s\n", inet_ntoa(them.sin_addr));
1568 ast_rtp_set_peer(p->rtp, &them);
1569 ast_rtp_get_us(p->rtp, &us);
1571 h323_native_bridge(p->cd.call_token, inet_ntoa(them.sin_addr), inet_ntoa(us.sin_addr));
1577 static struct ast_rtp_protocol oh323_rtp = {
1578 get_rtp_info: oh323_get_rtp_peer,
1579 set_rtp_peer: oh323_set_rtp_peer,
1586 /* fire up the H.323 Endpoint */
1587 h323_end_point_create();
1589 res = reload_config();
1591 /* Make sure we can register our channel type */
1592 if (ast_channel_register(type, tdesc, capability, oh323_request)) {
1593 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1597 ast_cli_register(&cli_debug);
1598 ast_cli_register(&cli_no_debug);
1599 ast_cli_register(&cli_trace);
1600 ast_cli_register(&cli_no_trace);
1601 ast_cli_register(&cli_show_codecs);
1602 ast_cli_register(&cli_gk_cycle);
1604 oh323_rtp.type = type;
1605 ast_rtp_proto_register(&oh323_rtp);
1607 sched = sched_context_create();
1609 ast_log(LOG_WARNING, "Unable to create schedule context\n");
1611 io = io_context_create();
1613 ast_log(LOG_WARNING, "Unable to create I/O context\n");
1616 /* Register our callback functions */
1617 h323_callback_register(setup_incoming_call,
1618 setup_outgoing_call,
1620 setup_rtp_connection,
1622 connection_made, send_digit);
1624 /* start the h.323 listener */
1625 if (h323_start_listener(port, bindaddr, jitter)) {
1626 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
1631 /* Possibly register with a GK */
1632 if (!gatekeeper_disable) {
1633 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1634 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1639 /* And start the monitor for the first time */
1648 struct oh323_pvt *p, *pl;
1650 if (!ast_pthread_mutex_lock(&iflock)) {
1651 /* hangup all interfaces if they have an owner */
1655 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1659 ast_pthread_mutex_unlock(&iflock);
1661 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1665 if (!ast_pthread_mutex_lock(&iflock)) {
1666 /* destroy all the interfaces and free their memory */
1671 /* free associated memory */
1675 ast_pthread_mutex_unlock(&iflock);
1677 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1683 /* unregister channel type */
1684 ast_channel_unregister(type);
1693 ast_pthread_mutex_lock(&usecnt_lock);
1695 ast_pthread_mutex_unlock(&usecnt_lock);
1706 return ASTERISK_GPL_KEY;