00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <sys/socket.h>
00035 #include <sys/signal.h>
00036 #include <sys/param.h>
00037 #if defined(BSD)
00038 #ifndef IPTOS_MINCOST
00039 #define IPTOS_MINCOST 0x02
00040 #endif
00041 #endif
00042 #include <arpa/inet.h>
00043 #include <net/if.h>
00044 #include <netinet/in.h>
00045 #include <netinet/in_systm.h>
00046 #include <netinet/ip.h>
00047 #include <unistd.h>
00048 #include <stdlib.h>
00049 #include <netdb.h>
00050 #include <stdio.h>
00051 #include <string.h>
00052 #include <errno.h>
00053 #include <fcntl.h>
00054 #ifdef __cplusplus
00055 extern "C" {
00056 #endif
00057
00058 #include "asterisk.h"
00059
00060 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00061
00062 #include "asterisk/lock.h"
00063 #include "asterisk/logger.h"
00064 #include "asterisk/channel.h"
00065 #include "asterisk/config.h"
00066 #include "asterisk/module.h"
00067 #include "asterisk/pbx.h"
00068 #include "asterisk/options.h"
00069 #include "asterisk/utils.h"
00070 #include "asterisk/lock.h"
00071 #include "asterisk/sched.h"
00072 #include "asterisk/io.h"
00073 #include "asterisk/rtp.h"
00074 #include "asterisk/acl.h"
00075 #include "asterisk/callerid.h"
00076 #include "asterisk/cli.h"
00077 #include "asterisk/dsp.h"
00078 #include "asterisk/causes.h"
00079 #ifdef __cplusplus
00080 }
00081 #endif
00082 #include "h323/chan_h323.h"
00083
00084 send_digit_cb on_send_digit;
00085 on_rtp_cb on_external_rtp_create;
00086 start_rtp_cb on_start_rtp_channel;
00087 setup_incoming_cb on_incoming_call;
00088 setup_outbound_cb on_outgoing_call;
00089 chan_ringing_cb on_chan_ringing;
00090 con_established_cb on_connection_established;
00091 clear_con_cb on_connection_cleared;
00092 answer_call_cb on_answer_call;
00093 progress_cb on_progress;
00094 rfc2833_cb on_set_rfc2833_payload;
00095 hangup_cb on_hangup;
00096 setcapabilities_cb on_setcapabilities;
00097
00098
00099 int h323debug;
00100
00101
00102 static const char type[] = "H323";
00103 static const char desc[] = "The NuFone Network's Open H.323 Channel Driver";
00104 static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
00105 static const char config[] = "h323.conf";
00106 static char default_context[AST_MAX_CONTEXT] = "default";
00107 static struct sockaddr_in bindaddr;
00108
00109
00110 static int h323_signalling_port = 1720;
00111 static char gatekeeper[100];
00112 static int gatekeeper_disable = 1;
00113 static int gatekeeper_discover = 0;
00114 static int usingGk = 0;
00115 static int gkroute = 0;
00116
00117 static int userbyalias = 1;
00118 static int tos = 0;
00119 static char secret[50];
00120 static unsigned int unique = 0;
00121
00122 static call_options_t global_options;
00123
00124
00125 struct oh323_pvt {
00126 ast_mutex_t lock;
00127 call_options_t options;
00128 int alreadygone;
00129 int needdestroy;
00130 call_details_t cd;
00131 struct ast_channel *owner;
00132 struct sockaddr_in sa;
00133 struct sockaddr_in redirip;
00134 int nonCodecCapability;
00135 int outgoing;
00136 char exten[AST_MAX_EXTENSION];
00137 char context[AST_MAX_CONTEXT];
00138 char accountcode[256];
00139 char cid_num[80];
00140 char cid_name[80];
00141 char rdnis[80];
00142 int amaflags;
00143 struct ast_rtp *rtp;
00144 struct ast_dsp *vad;
00145 int nativeformats;
00146 int needhangup;
00147 int hangupcause;
00148 int newstate;
00149 int newcontrol;
00150 struct oh323_pvt *next;
00151 } *iflist = NULL;
00152
00153 static struct ast_user_list {
00154 struct oh323_user *users;
00155 ast_mutex_t lock;
00156 } userl;
00157
00158 static struct ast_peer_list {
00159 struct oh323_peer *peers;
00160 ast_mutex_t lock;
00161 } peerl;
00162
00163 static struct ast_alias_list {
00164 struct oh323_alias *aliases;
00165 ast_mutex_t lock;
00166 } aliasl;
00167
00168
00169 static struct sched_context *sched;
00170 static struct io_context *io;
00171
00172
00173 AST_MUTEX_DEFINE_STATIC(iflock);
00174
00175
00176 static int usecnt = 0;
00177 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00178
00179
00180
00181 AST_MUTEX_DEFINE_STATIC(monlock);
00182
00183
00184 AST_MUTEX_DEFINE_STATIC(caplock);
00185
00186
00187 AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
00188 static int h323_reloading = 0;
00189
00190
00191
00192 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00193 static int restart_monitor(void);
00194 static int h323_do_reload(void);
00195
00196 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause);
00197 static int oh323_digit(struct ast_channel *c, char digit);
00198 static int oh323_call(struct ast_channel *c, char *dest, int timeout);
00199 static int oh323_hangup(struct ast_channel *c);
00200 static int oh323_answer(struct ast_channel *c);
00201 static struct ast_frame *oh323_read(struct ast_channel *c);
00202 static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
00203 static int oh323_indicate(struct ast_channel *c, int condition);
00204 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00205
00206 static const struct ast_channel_tech oh323_tech = {
00207 .type = type,
00208 .description = tdesc,
00209 .capabilities = AST_FORMAT_ULAW,
00210 .properties = AST_CHAN_TP_WANTSJITTER,
00211 .requester = oh323_request,
00212 .send_digit = oh323_digit,
00213 .call = oh323_call,
00214 .hangup = oh323_hangup,
00215 .answer = oh323_answer,
00216 .read = oh323_read,
00217 .write = oh323_write,
00218 .indicate = oh323_indicate,
00219 .fixup = oh323_fixup,
00220
00221 #if 0
00222 .bridge = ast_rtp_bridge,
00223 #endif
00224 };
00225
00226
00227 static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
00228 {
00229 if (c->nativeformats != pvt->nativeformats) {
00230 if (h323debug)
00231 ast_log(LOG_DEBUG, "Preparing %s for new native format\n", c->name);
00232 c->nativeformats = pvt->nativeformats;
00233 ast_set_read_format(c, c->readformat);
00234 ast_set_write_format(c, c->writeformat);
00235 }
00236 if (pvt->needhangup) {
00237 if (h323debug)
00238 ast_log(LOG_DEBUG, "Process pending hangup for %s\n", c->name);
00239 c->_softhangup |= AST_SOFTHANGUP_DEV;
00240 c->hangupcause = pvt->hangupcause;
00241 ast_queue_hangup(c);
00242 pvt->needhangup = 0;
00243 pvt->newstate = pvt->newcontrol = -1;
00244 }
00245 if (pvt->newstate >= 0) {
00246 ast_setstate(c, pvt->newstate);
00247 pvt->newstate = -1;
00248 }
00249 if (pvt->newcontrol >= 0) {
00250 ast_queue_control(c, pvt->newcontrol);
00251 pvt->newcontrol = -1;
00252 }
00253 }
00254
00255
00256 static void oh323_update_info(struct ast_channel *c)
00257 {
00258 struct oh323_pvt *pvt = c->tech_pvt;
00259
00260 if (pvt) {
00261 ast_mutex_lock(&pvt->lock);
00262 __oh323_update_info(c, pvt);
00263 ast_mutex_unlock(&pvt->lock);
00264 }
00265 }
00266
00267 static void cleanup_call_details(call_details_t *cd)
00268 {
00269 if (cd->call_token) {
00270 free(cd->call_token);
00271 cd->call_token = NULL;
00272 }
00273 if (cd->call_source_aliases) {
00274 free(cd->call_source_aliases);
00275 cd->call_source_aliases = NULL;
00276 }
00277 if (cd->call_dest_alias) {
00278 free(cd->call_dest_alias);
00279 cd->call_dest_alias = NULL;
00280 }
00281 if (cd->call_source_name) {
00282 free(cd->call_source_name);
00283 cd->call_source_name = NULL;
00284 }
00285 if (cd->call_source_e164) {
00286 free(cd->call_source_e164);
00287 cd->call_source_e164 = NULL;
00288 }
00289 if (cd->call_dest_e164) {
00290 free(cd->call_dest_e164);
00291 cd->call_dest_e164 = NULL;
00292 }
00293 if (cd->sourceIp) {
00294 free(cd->sourceIp);
00295 cd->sourceIp = NULL;
00296 }
00297 }
00298
00299 static void __oh323_destroy(struct oh323_pvt *pvt)
00300 {
00301 struct oh323_pvt *cur, *prev = NULL;
00302
00303 if (pvt->rtp) {
00304 ast_rtp_destroy(pvt->rtp);
00305 }
00306
00307
00308 if (pvt->vad) {
00309 ast_dsp_free(pvt->vad);
00310 }
00311 cleanup_call_details(&pvt->cd);
00312
00313
00314 if (pvt->owner) {
00315 ast_mutex_lock(&pvt->owner->lock);
00316 ast_log(LOG_DEBUG, "Detaching from %s\n", pvt->owner->name);
00317 pvt->owner->tech_pvt = NULL;
00318 ast_mutex_unlock(&pvt->owner->lock);
00319 }
00320 cur = iflist;
00321 while(cur) {
00322 if (cur == pvt) {
00323 if (prev)
00324 prev->next = cur->next;
00325 else
00326 iflist = cur->next;
00327 break;
00328 }
00329 prev = cur;
00330 cur = cur->next;
00331 }
00332 if (!cur) {
00333 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
00334 } else {
00335 ast_mutex_destroy(&pvt->lock);
00336 free(pvt);
00337 }
00338 }
00339
00340 static void oh323_destroy(struct oh323_pvt *pvt)
00341 {
00342 ast_mutex_lock(&iflock);
00343 __oh323_destroy(pvt);
00344 ast_mutex_unlock(&iflock);
00345 }
00346
00347
00348
00349
00350
00351 static int oh323_digit(struct ast_channel *c, char digit)
00352 {
00353 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
00354 char *token;
00355
00356 if (!pvt) {
00357 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
00358 return -1;
00359 }
00360 ast_mutex_lock(&pvt->lock);
00361 if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
00362
00363 if (h323debug) {
00364 ast_log(LOG_DEBUG, "Sending out-of-band digit %c on %s\n", digit, c->name);
00365 }
00366 ast_rtp_senddigit(pvt->rtp, digit);
00367 } else {
00368
00369 if (h323debug) {
00370 ast_log(LOG_DEBUG, "Sending inband digit %c on %s\n", digit, c->name);
00371 }
00372 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
00373 h323_send_tone(token, digit);
00374 if (token) {
00375 free(token);
00376 }
00377 }
00378 ast_mutex_unlock(&pvt->lock);
00379 oh323_update_info(c);
00380 return 0;
00381 }
00382
00383
00384
00385
00386
00387
00388 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
00389 {
00390 int res = 0;
00391 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
00392 char addr[INET_ADDRSTRLEN];
00393 char called_addr[1024];
00394
00395 if (h323debug) {
00396 ast_log(LOG_DEBUG, "Calling to %s on %s\n", dest, c->name);
00397 }
00398 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
00399 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
00400 return -1;
00401 }
00402 ast_mutex_lock(&pvt->lock);
00403 if (usingGk) {
00404 if (ast_strlen_zero(pvt->exten)) {
00405 strncpy(called_addr, dest, sizeof(called_addr));
00406 } else {
00407 snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
00408 }
00409 } else {
00410 ast_inet_ntoa(addr, sizeof(addr), pvt->sa.sin_addr);
00411 res = htons(pvt->sa.sin_port);
00412 if (ast_strlen_zero(pvt->exten)) {
00413 snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
00414 } else {
00415 snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
00416 }
00417 }
00418
00419 called_addr[sizeof(called_addr) - 1] = '\0';
00420
00421 if (c->cid.cid_num) {
00422 strncpy(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
00423 }
00424 if (c->cid.cid_name) {
00425 strncpy(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
00426 }
00427
00428
00429 pvt->outgoing = 1;
00430
00431 ast_log(LOG_DEBUG, "Placing outgoing call to %s, %d\n", called_addr, pvt->options.dtmfcodec);
00432 ast_mutex_unlock(&pvt->lock);
00433 res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
00434 if (res) {
00435 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
00436 return -1;
00437 }
00438 oh323_update_info(c);
00439 return 0;
00440 }
00441
00442 static int oh323_answer(struct ast_channel *c)
00443 {
00444 int res;
00445 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
00446 char *token;
00447
00448 if (h323debug)
00449 ast_log(LOG_DEBUG, "Answering on %s\n", c->name);
00450
00451 ast_mutex_lock(&pvt->lock);
00452 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
00453 ast_mutex_unlock(&pvt->lock);
00454 res = h323_answering_call(token, 0);
00455 if (token)
00456 free(token);
00457
00458 oh323_update_info(c);
00459 if (c->_state != AST_STATE_UP) {
00460 ast_setstate(c, AST_STATE_UP);
00461 }
00462 return res;
00463 }
00464
00465 static int oh323_hangup(struct ast_channel *c)
00466 {
00467 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
00468 int needcancel = 0;
00469 int q931cause = AST_CAUSE_NORMAL_CLEARING;
00470 char *call_token;
00471
00472
00473 if (h323debug)
00474 ast_log(LOG_DEBUG, "Hanging up call %s\n", c->name);
00475
00476 if (!c->tech_pvt) {
00477 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
00478 return 0;
00479 }
00480 ast_mutex_lock(&pvt->lock);
00481
00482 if (pvt->owner != c) {
00483 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
00484 ast_mutex_unlock(&pvt->lock);
00485 return 0;
00486 }
00487 if (!c || (c->_state != AST_STATE_UP)) {
00488 needcancel = 1;
00489 }
00490
00491 pvt->owner = NULL;
00492 c->tech_pvt = NULL;
00493
00494 if (c->hangupcause) {
00495 q931cause = c->hangupcause;
00496 } else {
00497 char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
00498 if (cause) {
00499 if (!strcmp(cause, "CONGESTION")) {
00500 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
00501 } else if (!strcmp(cause, "BUSY")) {
00502 q931cause = AST_CAUSE_USER_BUSY;
00503 } else if (!strcmp(cause, "CHANISUNVAIL")) {
00504 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
00505 } else if (!strcmp(cause, "NOANSWER")) {
00506 q931cause = AST_CAUSE_NO_ANSWER;
00507 } else if (!strcmp(cause, "CANCEL")) {
00508 q931cause = AST_CAUSE_CALL_REJECTED;
00509 }
00510 }
00511 }
00512
00513
00514 if (!pvt->alreadygone && !pvt->hangupcause) {
00515 call_token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
00516 if (call_token) {
00517
00518 ast_mutex_unlock(&pvt->lock);
00519 if (h323_clear_call(call_token, q931cause)) {
00520 ast_log(LOG_DEBUG, "ClearCall failed.\n");
00521 }
00522 free(call_token);
00523 ast_mutex_lock(&pvt->lock);
00524 }
00525 }
00526 pvt->needdestroy = 1;
00527
00528
00529 ast_mutex_lock(&usecnt_lock);
00530 usecnt--;
00531 if (usecnt < 0) {
00532 ast_log(LOG_WARNING, "Usecnt < 0\n");
00533 }
00534 ast_mutex_unlock(&usecnt_lock);
00535 ast_mutex_unlock(&pvt->lock);
00536 ast_update_use_count();
00537 return 0;
00538 }
00539
00540 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
00541 {
00542
00543 struct ast_frame *f;
00544 static struct ast_frame null_frame = { AST_FRAME_NULL, };
00545
00546
00547 if (pvt->options.nat) {
00548 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
00549 pvt->options.nat = 0;
00550 }
00551
00552 f = ast_rtp_read(pvt->rtp);
00553
00554 if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
00555 return &null_frame;
00556 }
00557 if (pvt->owner) {
00558
00559 if (f->frametype == AST_FRAME_VOICE) {
00560 if (f->subclass != pvt->owner->nativeformats) {
00561
00562 if (ast_mutex_trylock(&pvt->owner->lock)) {
00563 ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
00564 return &null_frame;
00565 }
00566 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
00567 pvt->owner->nativeformats = f->subclass;
00568 pvt->nativeformats = f->subclass;
00569 ast_set_read_format(pvt->owner, pvt->owner->readformat);
00570 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
00571 ast_mutex_unlock(&pvt->owner->lock);
00572 }
00573
00574 if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
00575 if (!ast_mutex_trylock(&pvt->owner->lock)) {
00576 f = ast_dsp_process(pvt->owner,pvt->vad,f);
00577 ast_mutex_unlock(&pvt->owner->lock);
00578 }
00579 else
00580 ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
00581 if (f &&(f->frametype == AST_FRAME_DTMF)) {
00582 ast_log(LOG_DEBUG, "Received in-band digit %c.\n", f->subclass);
00583 }
00584 }
00585 }
00586 }
00587 return f;
00588 }
00589
00590 static struct ast_frame *oh323_read(struct ast_channel *c)
00591 {
00592 struct ast_frame *fr;
00593 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
00594 ast_mutex_lock(&pvt->lock);
00595 __oh323_update_info(c, pvt);
00596 fr = oh323_rtp_read(pvt);
00597 ast_mutex_unlock(&pvt->lock);
00598 return fr;
00599 }
00600
00601 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
00602 {
00603 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
00604 int res = 0;
00605 if (frame->frametype != AST_FRAME_VOICE) {
00606 if (frame->frametype == AST_FRAME_IMAGE) {
00607 return 0;
00608 } else {
00609 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
00610 return 0;
00611 }
00612 } else {
00613 if (!(frame->subclass & c->nativeformats)) {
00614 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
00615 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
00616 return 0;
00617 }
00618 }
00619 if (pvt) {
00620 ast_mutex_lock(&pvt->lock);
00621 if (pvt->rtp) {
00622 res = ast_rtp_write(pvt->rtp, frame);
00623 }
00624 __oh323_update_info(c, pvt);
00625 ast_mutex_unlock(&pvt->lock);
00626 }
00627 return res;
00628 }
00629
00630 static int oh323_indicate(struct ast_channel *c, int condition)
00631 {
00632
00633 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
00634 char *token = (char *)NULL;
00635
00636 ast_mutex_lock(&pvt->lock);
00637 token = (pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL);
00638 ast_mutex_unlock(&pvt->lock);
00639
00640 if (h323debug)
00641 ast_log(LOG_DEBUG, "OH323: Indicating %d on %s\n", condition, token);
00642
00643 switch(condition) {
00644 case AST_CONTROL_RINGING:
00645 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
00646 h323_send_alerting(token);
00647 break;
00648 }
00649 if (token)
00650 free(token);
00651 return -1;
00652 case AST_CONTROL_PROGRESS:
00653 if (c->_state != AST_STATE_UP) {
00654 h323_send_progress(token);
00655 break;
00656 }
00657 if (token)
00658 free(token);
00659 return -1;
00660
00661 case AST_CONTROL_BUSY:
00662 if (c->_state != AST_STATE_UP) {
00663 h323_answering_call(token, 1);
00664 ast_mutex_lock(&pvt->lock);
00665 pvt->alreadygone = 1;
00666 ast_mutex_unlock(&pvt->lock);
00667 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
00668 break;
00669 }
00670 if (token)
00671 free(token);
00672 return -1;
00673 case AST_CONTROL_CONGESTION:
00674 if (c->_state != AST_STATE_UP) {
00675 h323_answering_call(token, 1);
00676 ast_mutex_lock(&pvt->lock);
00677 pvt->alreadygone = 1;
00678 ast_mutex_unlock(&pvt->lock);
00679 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
00680 break;
00681 }
00682 if (token)
00683 free(token);
00684 return -1;
00685 case AST_CONTROL_PROCEEDING:
00686 case -1:
00687 if (token)
00688 free(token);
00689 return -1;
00690 default:
00691 ast_log(LOG_WARNING, "Don't know how to indicate condition %d on %s\n", condition, token);
00692 if (token)
00693 free(token);
00694 return -1;
00695 }
00696
00697 if (h323debug)
00698 ast_log(LOG_DEBUG, "OH323: Indicated %d on %s\n", condition, token);
00699 if (token)
00700 free(token);
00701 oh323_update_info(c);
00702
00703 return -1;
00704 }
00705
00706 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00707 {
00708 struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
00709
00710 ast_mutex_lock(&pvt->lock);
00711 if (pvt->owner != oldchan) {
00712 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
00713 return -1;
00714 }
00715 pvt->owner = newchan;
00716 ast_mutex_unlock(&pvt->lock);
00717 return 0;
00718 }
00719
00720
00721 static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
00722 {
00723 struct ast_channel *ch;
00724 int fmt;
00725
00726
00727 ast_mutex_unlock(&pvt->lock);
00728 ch = ast_channel_alloc(1);
00729
00730 ast_mutex_lock(&usecnt_lock);
00731 usecnt++;
00732 ast_mutex_unlock(&usecnt_lock);
00733 ast_update_use_count();
00734 ast_mutex_lock(&pvt->lock);
00735 if (ch) {
00736 ch->tech = &oh323_tech;
00737 snprintf(ch->name, sizeof(ch->name), "H323/%s", host);
00738 ch->nativeformats = pvt->options.capability;
00739 if (!ch->nativeformats) {
00740 ch->nativeformats = global_options.capability;
00741 }
00742 pvt->nativeformats = ch->nativeformats;
00743 fmt = ast_best_codec(ch->nativeformats);
00744 ch->type = type;
00745 ch->fds[0] = ast_rtp_fd(pvt->rtp);
00746 if (state == AST_STATE_RING) {
00747 ch->rings = 1;
00748 }
00749 ch->writeformat = fmt;
00750 ch->rawwriteformat = fmt;
00751 ch->readformat = fmt;
00752 ch->rawreadformat = fmt;
00753
00754 if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
00755 pvt->vad = ast_dsp_new();
00756 ast_dsp_set_features(pvt->vad, DSP_FEATURE_DTMF_DETECT);
00757 }
00758
00759 ch->tech_pvt = pvt;
00760
00761 pvt->owner = ch;
00762
00763 strncpy(ch->context, pvt->context, sizeof(ch->context) - 1);
00764 strncpy(ch->exten, pvt->exten, sizeof(ch->exten) - 1);
00765 ch->priority = 1;
00766 if (!ast_strlen_zero(pvt->accountcode)) {
00767 strncpy(ch->accountcode, pvt->accountcode, sizeof(ch->accountcode) - 1);
00768 }
00769 if (pvt->amaflags) {
00770 ch->amaflags = pvt->amaflags;
00771 }
00772 if (!ast_strlen_zero(pvt->cid_num)) {
00773 ch->cid.cid_num = strdup(pvt->cid_num);
00774 } else if (!ast_strlen_zero(pvt->cd.call_source_e164)) {
00775 ch->cid.cid_num = strdup(pvt->cd.call_source_e164);
00776 }
00777 if (!ast_strlen_zero(pvt->cid_name)) {
00778 ch->cid.cid_name = strdup(pvt->cid_name);
00779 } else if (!ast_strlen_zero(pvt->cd.call_source_name)) {
00780 ch->cid.cid_name = strdup(pvt->cd.call_source_name);
00781 }
00782 if (!ast_strlen_zero(pvt->rdnis)) {
00783 ch->cid.cid_rdnis = strdup(pvt->rdnis);
00784 }
00785 if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
00786 ch->cid.cid_dnid = strdup(pvt->exten);
00787 }
00788 ast_setstate(ch, state);
00789 if (state != AST_STATE_DOWN) {
00790 if (ast_pbx_start(ch)) {
00791 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
00792 ast_hangup(ch);
00793 ch = NULL;
00794 }
00795 }
00796 } else {
00797 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
00798 }
00799 return ch;
00800 }
00801
00802 static struct oh323_pvt *oh323_alloc(int callid)
00803 {
00804 struct oh323_pvt *pvt;
00805
00806 pvt = (struct oh323_pvt *) malloc(sizeof(struct oh323_pvt));
00807 if (!pvt) {
00808 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
00809 return NULL;
00810 }
00811 memset(pvt, 0, sizeof(struct oh323_pvt));
00812 pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0,bindaddr.sin_addr);
00813 if (!pvt->rtp) {
00814 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
00815 free(pvt);
00816 return NULL;
00817 }
00818 ast_rtp_settos(pvt->rtp, tos);
00819 ast_mutex_init(&pvt->lock);
00820
00821 if ((pvt->cd).call_token == NULL) {
00822 (pvt->cd).call_token = (char *)malloc(128);
00823 }
00824 if (!pvt->cd.call_token) {
00825 ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
00826 return NULL;
00827 }
00828 memset((char *)(pvt->cd).call_token, 0, 128);
00829 pvt->cd.call_reference = callid;
00830 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
00831 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
00832 pvt->nonCodecCapability |= AST_RTP_DTMF;
00833 } else {
00834 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
00835 }
00836 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
00837 pvt->newstate = pvt->newcontrol = -1;
00838
00839 ast_mutex_lock(&iflock);
00840 pvt->next = iflist;
00841 iflist = pvt;
00842 ast_mutex_unlock(&iflock);
00843 return pvt;
00844 }
00845
00846 static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
00847 {
00848 struct oh323_pvt *pvt;
00849
00850 ast_mutex_lock(&iflock);
00851 pvt = iflist;
00852 while(pvt) {
00853 if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
00854
00855 if ((token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
00856 ast_mutex_lock(&pvt->lock);
00857 ast_mutex_unlock(&iflock);
00858 return pvt;
00859 } else if (token == NULL) {
00860 ast_log(LOG_WARNING, "Call Token is NULL\n");
00861 ast_mutex_lock(&pvt->lock);
00862 ast_mutex_unlock(&iflock);
00863 return pvt;
00864 }
00865 }
00866 pvt = pvt->next;
00867 }
00868 ast_mutex_unlock(&iflock);
00869 return NULL;
00870 }
00871
00872 static int update_state(struct oh323_pvt *pvt, int state, int signal)
00873 {
00874 if (!pvt)
00875 return 0;
00876 if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
00877 if (state >= 0)
00878 ast_setstate(pvt->owner, state);
00879 if (signal >= 0)
00880 ast_queue_control(pvt->owner, signal);
00881 return 1;
00882 }
00883 else {
00884 if (state >= 0)
00885 pvt->newstate = state;
00886 if (signal >= 0)
00887 pvt->newcontrol = signal;
00888 return 0;
00889 }
00890 }
00891
00892 struct oh323_user *find_user(const call_details_t *cd)
00893 {
00894 struct oh323_user *u;
00895 char iabuf[INET_ADDRSTRLEN];
00896 u = userl.users;
00897 if (userbyalias) {
00898 while(u) {
00899 if (!strcasecmp(u->name, cd->call_source_aliases)) {
00900 break;
00901 }
00902 u = u->next;
00903 }
00904 } else {
00905 while(u) {
00906 if (!strcasecmp(cd->sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), u->addr.sin_addr))) {
00907 break;
00908 }
00909 u = u->next;
00910 }
00911 }
00912 return u;
00913 }
00914
00915 struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin)
00916 {
00917 struct oh323_peer *p = NULL;
00918 static char iabuf[INET_ADDRSTRLEN];
00919
00920 p = peerl.peers;
00921 if (peer) {
00922 while(p) {
00923 if (!strcasecmp(p->name, peer)) {
00924 ast_log(LOG_DEBUG, "Found peer %s by name\n", peer);
00925 break;
00926 }
00927 p = p->next;
00928 }
00929 } else {
00930
00931 if (sin) {
00932 while (p) {
00933 if ((!inaddrcmp(&p->addr, sin)) ||
00934 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)) {
00935 ast_log(LOG_DEBUG, "Found peer %s/%s by addr\n", peer, ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr));
00936 break;
00937 }
00938 p = p->next;
00939 }
00940 }
00941 }
00942 if (!p) {
00943 ast_log(LOG_DEBUG, "Could not find peer %s by name or address\n", peer);
00944 }
00945 return p;
00946 }
00947
00948 static int create_addr(struct oh323_pvt *pvt, char *opeer)
00949 {
00950 struct hostent *hp;
00951 struct ast_hostent ahp;
00952 struct oh323_peer *p;
00953 int portno;
00954 int found = 0;
00955 char *port;
00956 char *hostn;
00957 char peer[256] = "";
00958
00959 strncpy(peer, opeer, sizeof(peer) - 1);
00960 port = strchr(peer, ':');
00961 if (port) {
00962 *port = '\0';
00963 port++;
00964 }
00965 pvt->sa.sin_family = AF_INET;
00966 ast_mutex_lock(&peerl.lock);
00967 p = find_peer(peer, NULL);
00968 if (p) {
00969 found++;
00970 memcpy(&pvt->options, &p->options, sizeof(pvt->options));
00971 if (pvt->rtp) {
00972 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
00973 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
00974 }
00975 if (pvt->options.dtmfmode) {
00976 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
00977 pvt->nonCodecCapability |= AST_RTP_DTMF;
00978 } else {
00979 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
00980 }
00981 }
00982 if (p->addr.sin_addr.s_addr) {
00983 pvt->sa.sin_addr = p->addr.sin_addr;
00984 pvt->sa.sin_port = p->addr.sin_port;
00985 }
00986 }
00987 ast_mutex_unlock(&peerl.lock);
00988 if (!p && !found) {
00989 hostn = peer;
00990 if (port) {
00991 portno = atoi(port);
00992 } else {
00993 portno = h323_signalling_port;
00994 }
00995 hp = ast_gethostbyname(hostn, &ahp);
00996 if (hp) {
00997 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
00998 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
00999 pvt->sa.sin_port = htons(portno);
01000 return 0;
01001 } else {
01002 ast_log(LOG_WARNING, "No such host: %s\n", peer);
01003 return -1;
01004 }
01005 } else if (!p) {
01006 return -1;
01007 } else {
01008 return 0;
01009 }
01010 }
01011 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause)
01012 {
01013 int oldformat;
01014 struct oh323_pvt *pvt;
01015 struct ast_channel *tmpc = NULL;
01016 char *dest = (char *)data;
01017 char *ext, *host;
01018 char *h323id = NULL;
01019 char tmp[256], tmp1[256];
01020
01021 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
01022 pvt = oh323_alloc(0);
01023 if (!pvt) {
01024 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
01025 return NULL;
01026 }
01027 oldformat = format;
01028 format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
01029 if (!format) {
01030 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
01031 return NULL;
01032 }
01033 strncpy(tmp, dest, sizeof(tmp) - 1);
01034 host = strchr(tmp, '@');
01035 if (host) {
01036 *host = '\0';
01037 host++;
01038 ext = tmp;
01039 } else {
01040 host = tmp;
01041 ext = NULL;
01042 }
01043 strtok_r(host, "/", &(h323id));
01044 if (!ast_strlen_zero(h323id)) {
01045 h323_set_id(h323id);
01046 }
01047 if (ext) {
01048 strncpy(pvt->exten, ext, sizeof(pvt->exten) - 1);
01049 }
01050 ast_log(LOG_DEBUG, "Extension: %s Host: %s\n", pvt->exten, host);
01051 if (!usingGk) {
01052 if (create_addr(pvt, host)) {
01053 oh323_destroy(pvt);
01054 return NULL;
01055 }
01056 }
01057 else {
01058 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
01059 if (pvt->rtp) {
01060 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
01061 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
01062 }
01063 if (pvt->options.dtmfmode) {
01064 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
01065 pvt->nonCodecCapability |= AST_RTP_DTMF;
01066 } else {
01067 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
01068 }
01069 }
01070 }
01071
01072 ast_mutex_lock(&caplock);
01073
01074 snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
01075 tmp1[sizeof(tmp1)-1] = '\0';
01076 ast_mutex_unlock(&caplock);
01077
01078 ast_mutex_lock(&pvt->lock);
01079 tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1);
01080 ast_mutex_unlock(&pvt->lock);
01081 if (!tmpc) {
01082 oh323_destroy(pvt);
01083 }
01084 ast_update_use_count();
01085 restart_monitor();
01086 return tmpc;
01087 }
01088
01089
01090 struct oh323_alias *find_alias(const char *source_aliases)
01091 {
01092 struct oh323_alias *a;
01093
01094 a = aliasl.aliases;
01095 while(a) {
01096 if (!strcasecmp(a->name, source_aliases)) {
01097 break;
01098 }
01099 a = a->next;
01100 }
01101 return a;
01102 }
01103
01104
01105
01106
01107
01108 int send_digit(unsigned call_reference, char digit, const char *token)
01109 {
01110 struct oh323_pvt *pvt;
01111 struct ast_frame f;
01112 int res;
01113
01114 ast_log(LOG_DEBUG, "Received Digit: %c\n", digit);
01115 pvt = find_call_locked(call_reference, token);
01116 if (!pvt) {
01117 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
01118 return -1;
01119 }
01120 memset(&f, 0, sizeof(f));
01121 f.frametype = AST_FRAME_DTMF;
01122 f.subclass = digit;
01123 f.datalen = 0;
01124 f.samples = 800;
01125 f.offset = 0;
01126 f.data = NULL;
01127 f.mallocd = 0;
01128 f.src = "SEND_DIGIT";
01129 res = ast_queue_frame(pvt->owner, &f);
01130 ast_mutex_unlock(&pvt->lock);
01131 return res;
01132 }
01133
01134
01135
01136
01137
01138
01139 struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
01140 {
01141 struct oh323_pvt *pvt;
01142 struct sockaddr_in us;
01143 struct rtp_info *info;
01144
01145 info = (struct rtp_info *)malloc(sizeof(struct rtp_info));
01146 if (!info) {
01147 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
01148 return NULL;
01149 }
01150 pvt = find_call_locked(call_reference, token);
01151 if (!pvt) {
01152 free(info);
01153 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
01154 return NULL;
01155 }
01156
01157 ast_rtp_get_us(pvt->rtp, &us);
01158 ast_mutex_unlock(&pvt->lock);
01159
01160 ast_inet_ntoa(info->addr, sizeof(info->addr), us.sin_addr);
01161 info->port = ntohs(us.sin_port);
01162 if (h323debug)
01163 ast_log(LOG_DEBUG, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
01164 return info;
01165 }
01166
01167
01168
01169
01170 struct rtpPayloadType {
01171 int isAstFormat;
01172 int code;
01173 };
01174
01175
01176
01177
01178
01179
01180 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
01181 {
01182 struct oh323_pvt *pvt;
01183 struct sockaddr_in them;
01184 struct rtpPayloadType rtptype;
01185
01186 if (h323debug)
01187 ast_log(LOG_DEBUG, "Setting up RTP connection for %s\n", token);
01188
01189
01190 pvt = find_call_locked(call_reference, token);
01191 if (!pvt) {
01192 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
01193 return;
01194 }
01195 if (pvt->alreadygone) {
01196 ast_mutex_unlock(&pvt->lock);
01197 return;
01198 }
01199 rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
01200 pvt->nativeformats = rtptype.code;
01201 if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
01202 pvt->owner->nativeformats = pvt->nativeformats;
01203 ast_set_read_format(pvt->owner, pvt->owner->readformat);
01204 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
01205 if (pvt->options.progress_audio)
01206 ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
01207 ast_mutex_unlock(&pvt->owner->lock);
01208 }
01209 else {
01210 if (pvt->options.progress_audio)
01211 pvt->newcontrol = AST_CONTROL_PROGRESS;
01212 if (h323debug)
01213 ast_log(LOG_DEBUG, "RTP connection preparation for %s is pending...\n", token);
01214 }
01215
01216 them.sin_family = AF_INET;
01217
01218 them.sin_addr.s_addr = inet_addr(remoteIp);
01219 them.sin_port = htons(remotePort);
01220 ast_rtp_set_peer(pvt->rtp, &them);
01221
01222 ast_mutex_unlock(&pvt->lock);
01223
01224 if (h323debug)
01225 ast_log(LOG_DEBUG, "RTP connection prepared for %s\n", token);
01226
01227 return;
01228 }
01229
01230
01231
01232
01233
01234 void connection_made(unsigned call_reference, const char *token)
01235 {
01236 struct ast_channel *c = NULL;
01237 struct oh323_pvt *pvt;
01238
01239 if (h323debug)
01240 ast_log(LOG_DEBUG, "Call %s answered\n", token);
01241
01242 pvt = find_call_locked(call_reference, token);
01243 if (!pvt) {
01244 ast_log(LOG_ERROR, "Something is wrong: connection\n");
01245 return;
01246 }
01247
01248
01249 if (!pvt->outgoing) {
01250 ast_mutex_unlock(&pvt->lock);
01251 return;
01252 }
01253 if (update_state(pvt, AST_STATE_UP, AST_CONTROL_ANSWER))
01254 ast_mutex_unlock(&pvt->owner->lock);
01255 ast_mutex_unlock(&pvt->lock);
01256 return;
01257 }
01258
01259 int progress(unsigned call_reference, const char *token, int inband)
01260 {
01261 struct oh323_pvt *pvt;
01262
01263 ast_log(LOG_DEBUG, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
01264
01265 pvt = find_call_locked(call_reference, token);
01266 if (!pvt) {
01267 ast_log(LOG_ERROR, "Private structure not found in progress.\n");
01268 return -1;
01269 }
01270 if (!pvt->owner) {
01271 ast_mutex_unlock(&pvt->lock);
01272 ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
01273 return -1;
01274 }
01275 if (update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING)))
01276 ast_mutex_unlock(&pvt->owner->lock);
01277 ast_mutex_unlock(&pvt->lock);
01278
01279 return 0;
01280 }
01281
01282
01283
01284
01285
01286
01287 call_options_t *setup_incoming_call(call_details_t *cd)
01288 {
01289 struct oh323_pvt *pvt;
01290 struct oh323_user *user = NULL;
01291 struct oh323_alias *alias = NULL;
01292 char iabuf[INET_ADDRSTRLEN];
01293
01294 if (h323debug)
01295 ast_log(LOG_DEBUG, "Setting up incoming call for %s\n", cd->call_token);
01296
01297
01298 pvt = oh323_alloc(cd->call_reference);
01299
01300 if (!pvt) {
01301 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
01302 return NULL;
01303 }
01304
01305
01306 memcpy(&pvt->cd, cd, sizeof(pvt->cd));
01307 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
01308
01309 if (h323debug) {
01310 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
01311 ast_verbose(VERBOSE_PREFIX_3 "\tCall token: [%s]\n", pvt->cd.call_token);
01312 ast_verbose(VERBOSE_PREFIX_3 "\tCalling party name: [%s]\n", pvt->cd.call_source_name);
01313 ast_verbose(VERBOSE_PREFIX_3 "\tCalling party number: [%s]\n", pvt->cd.call_source_e164);
01314 ast_verbose(VERBOSE_PREFIX_3 "\tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
01315 ast_verbose(VERBOSE_PREFIX_3 "\tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
01316 }
01317
01318
01319 if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk)) {
01320 if (!ast_strlen_zero(cd->call_dest_e164)) {
01321 strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
01322 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
01323 } else {
01324 alias = find_alias(cd->call_dest_alias);
01325 if (!alias) {
01326 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
01327 return NULL;
01328 }
01329 strncpy(pvt->exten, alias->name, sizeof(pvt->exten) - 1);
01330 strncpy(pvt->context, alias->context, sizeof(pvt->context) - 1);
01331 }
01332 } else {
01333
01334
01335 user = find_user(cd);
01336 if (!user) {
01337 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
01338 strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
01339 } else {
01340 strncpy(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten) - 1);
01341 }
01342 if (ast_strlen_zero(default_context)) {
01343 ast_log(LOG_ERROR, "Call from '%s' rejected due to no default context\n", pvt->cd.call_source_aliases);
01344 return NULL;
01345 }
01346 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
01347 ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd->call_source_aliases, pvt->context);
01348
01349 #if 0
01350 memset(&pvt->options, 0, sizeof(pvt->options));
01351 #endif
01352 } else {
01353 if (user->host) {
01354 if (strcasecmp(cd->sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), user->addr.sin_addr))) {
01355 if (ast_strlen_zero(user->context)) {
01356 if (ast_strlen_zero(default_context)) {
01357 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
01358 return NULL;
01359 }
01360 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
01361 } else {
01362 strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
01363 }
01364 pvt->exten[0] = 'i';
01365 pvt->exten[1] = '\0';
01366 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
01367 return NULL;
01368 }
01369 }
01370 strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
01371 memcpy(&pvt->options, &user->options, sizeof(pvt->options));
01372 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
01373 strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
01374 } else {
01375 strncpy(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten) - 1);
01376 }
01377 if (!ast_strlen_zero(user->accountcode)) {
01378 strncpy(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode) - 1);
01379 }
01380 if (user->amaflags) {
01381 pvt->amaflags = user->amaflags;
01382 }
01383 }
01384 }
01385 return &pvt->options;
01386 }
01387
01388
01389
01390
01391
01392
01393 static int answer_call(unsigned call_reference, const char *token)
01394 {
01395 struct oh323_pvt *pvt;
01396 struct ast_channel *c = NULL;
01397
01398 if (h323debug)
01399 ast_log(LOG_DEBUG, "Preparing Asterisk to answer for %s\n", token);
01400
01401
01402 pvt = find_call_locked(call_reference, token);
01403 if (!pvt) {
01404 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
01405 return 0;
01406 }
01407
01408 c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
01409
01410
01411 ast_mutex_unlock(&pvt->lock);
01412 if (!c) {
01413 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
01414 return 0;
01415 }
01416 return 1;
01417 }
01418
01419
01420
01421
01422
01423
01424 int setup_outgoing_call(call_details_t *cd)
01425 {
01426
01427 cleanup_call_details(cd);
01428
01429 return 1;
01430 }
01431
01432
01433
01434
01435
01436 void chan_ringing(unsigned call_reference, const char *token)
01437 {
01438 struct ast_channel *c = NULL;
01439 struct oh323_pvt *pvt;
01440
01441 if (h323debug)
01442 ast_log(LOG_DEBUG, "Ringing on %s\n", token);
01443
01444 pvt = find_call_locked(call_reference, token);
01445 if (!pvt) {
01446 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
01447 return;
01448 }
01449 if (!pvt->owner) {
01450 ast_mutex_unlock(&pvt->lock);
01451 ast_log(LOG_ERROR, "Channel has no owner\n");
01452 return;
01453 }
01454 if (update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING))
01455 ast_mutex_unlock(&pvt->owner->lock);
01456 ast_mutex_unlock(&pvt->lock);
01457 return;
01458 }
01459
01460
01461
01462
01463
01464 static void cleanup_connection(unsigned call_reference, const char *call_token)
01465 {
01466 struct oh323_pvt *pvt;
01467
01468 ast_log(LOG_DEBUG, "Cleaning connection to %s\n", call_token);
01469
01470 while (1) {
01471 pvt = find_call_locked(call_reference, call_token);
01472 if (!pvt) {
01473 if (h323debug)
01474 ast_log(LOG_DEBUG, "No connection for %s\n", call_token);
01475 return;
01476 }
01477 if (!pvt->owner || !ast_mutex_trylock(&pvt->owner->lock))
01478 break;
01479 #if 1
01480 #ifdef DEBUG_THREADS
01481 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s, locked at %ld/%d by %s (%s:%d)\n", call_token, pvt->owner->lock.thread, pvt->owner->lock.reentrancy, pvt->owner->lock.func, pvt->owner->lock.file, pvt->owner->lock.lineno);
01482 #else
01483 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
01484 #endif
01485 #endif
01486 ast_mutex_unlock(&pvt->lock);
01487 usleep(1);
01488 }
01489 if (pvt->rtp) {
01490
01491 ast_rtp_destroy(pvt->rtp);
01492 pvt->rtp = NULL;
01493 }
01494
01495 if (pvt->vad) {
01496 ast_dsp_free(pvt->vad);
01497 pvt->vad = NULL;
01498 }
01499 cleanup_call_details(&pvt->cd);
01500 pvt->alreadygone = 1;
01501
01502 if (pvt->owner) {
01503 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01504 ast_queue_hangup(pvt->owner);
01505 ast_mutex_unlock(&pvt->owner->lock);
01506 }
01507 ast_mutex_unlock(&pvt->lock);
01508 if (h323debug)
01509 ast_log(LOG_DEBUG, "Connection to %s cleaned\n", call_token);
01510 return;
01511 }
01512
01513 static void hangup_connection(unsigned int call_reference, const char *token, int cause)
01514 {
01515 struct oh323_pvt *pvt;
01516
01517 ast_log(LOG_DEBUG, "Hanging up connection to %s with cause %d\n", token, cause);
01518
01519 pvt = find_call_locked(call_reference, token);
01520 if (!pvt) {
01521 return;
01522 }
01523 if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
01524 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01525 pvt->owner->hangupcause = pvt->hangupcause = cause;
01526 ast_queue_hangup(pvt->owner);
01527 ast_mutex_unlock(&pvt->owner->lock);
01528 }
01529 else {
01530 pvt->needhangup = 1;
01531 pvt->hangupcause = cause;
01532 ast_log(LOG_DEBUG, "Hangup for %s is pending\n", token);
01533 }
01534 ast_mutex_unlock(&pvt->lock);
01535 }
01536
01537 void set_dtmf_payload(unsigned call_reference, const char *token, int payload)
01538 {
01539 struct oh323_pvt *pvt;
01540
01541 if (h323debug)
01542 ast_log(LOG_DEBUG, "Setting DTMF payload to %d on %s\n", payload, token);
01543
01544 pvt = find_call_locked(call_reference, token);
01545 if (!pvt) {
01546 return;
01547 }
01548 if (pvt->rtp) {
01549 ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", "telephone-event");
01550 }
01551 ast_mutex_unlock(&pvt->lock);
01552 if (h323debug)
01553 ast_log(LOG_DEBUG, "DTMF payload on %s set to %d\n", token, payload);
01554 }
01555
01556 static void set_local_capabilities(unsigned call_reference, const char *token)
01557 {
01558 struct oh323_pvt *pvt;
01559 int capability, dtmfmode;
01560
01561 if (h323debug)
01562 ast_log(LOG_DEBUG, "Setting capabilities for connection %s\n", token);
01563
01564 pvt = find_call_locked(call_reference, token);
01565 if (!pvt)
01566 return;
01567 capability = pvt->options.capability;
01568 dtmfmode = pvt->options.dtmfmode;
01569 ast_mutex_unlock(&pvt->lock);
01570 h323_set_capabilities(token, capability, dtmfmode);
01571
01572 if (h323debug)
01573 ast_log(LOG_DEBUG, "Capabilities for connection %s is set\n", token);
01574 }
01575
01576 static void *do_monitor(void *data)
01577 {
01578 int res;
01579 int reloading;
01580 struct oh323_pvt *oh323 = NULL;
01581
01582 for(;;) {
01583
01584 ast_mutex_lock(&h323_reload_lock);
01585 reloading = h323_reloading;
01586 h323_reloading = 0;
01587 ast_mutex_unlock(&h323_reload_lock);
01588 if (reloading) {
01589 if (option_verbose > 0) {
01590 ast_verbose(VERBOSE_PREFIX_1 "Reloading H.323\n");
01591 }
01592 h323_do_reload();
01593 }
01594
01595 ast_mutex_lock(&iflock);
01596 restartsearch:
01597 oh323 = iflist;
01598 while(oh323) {
01599 if (oh323->needdestroy) {
01600 __oh323_destroy(oh323);
01601 goto restartsearch;
01602 }
01603 oh323 = oh323->next;
01604 }
01605 ast_mutex_unlock(&iflock);
01606 pthread_testcancel();
01607
01608 res = ast_sched_wait(sched);
01609 if ((res < 0) || (res > 1000)) {
01610 res = 1000;
01611 }
01612 res = ast_io_wait(io, res);
01613 pthread_testcancel();
01614 ast_mutex_lock(&monlock);
01615 if (res >= 0) {
01616 ast_sched_runq(sched);
01617 }
01618 ast_mutex_unlock(&monlock);
01619 }
01620
01621 return NULL;
01622 }
01623
01624 static int restart_monitor(void)
01625 {
01626 pthread_attr_t attr;
01627
01628 if (monitor_thread == AST_PTHREADT_STOP) {
01629 return 0;
01630 }
01631 if (ast_mutex_lock(&monlock)) {
01632 ast_log(LOG_WARNING, "Unable to lock monitor\n");
01633 return -1;
01634 }
01635 if (monitor_thread == pthread_self()) {
01636 ast_mutex_unlock(&monlock);
01637 ast_log(LOG_WARNING, "Cannot kill myself\n");
01638 return -1;
01639 }
01640 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
01641
01642 pthread_kill(monitor_thread, SIGURG);
01643 } else {
01644 pthread_attr_init(&attr);
01645 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
01646
01647 if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
01648 ast_mutex_unlock(&monlock);
01649 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
01650 return -1;
01651 }
01652
01653 }
01654 ast_mutex_unlock(&monlock);
01655 return 0;
01656 }
01657
01658 static int h323_do_trace(int fd, int argc, char *argv[])
01659 {
01660 if (argc != 3) {
01661 return RESULT_SHOWUSAGE;
01662 }
01663 h323_debug(1, atoi(argv[2]));
01664 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
01665 return RESULT_SUCCESS;
01666 }
01667
01668 static int h323_no_trace(int fd, int argc, char *argv[])
01669 {
01670 if (argc != 3) {
01671 return RESULT_SHOWUSAGE;
01672 }
01673 h323_debug(0,0);
01674 ast_cli(fd, "H.323 trace disabled\n");
01675 return RESULT_SUCCESS;
01676 }
01677
01678 static int h323_do_debug(int fd, int argc, char *argv[])
01679 {
01680 if (argc != 2) {
01681 return RESULT_SHOWUSAGE;
01682 }
01683 h323debug = 1;
01684 ast_cli(fd, "H323 debug enabled\n");
01685 return RESULT_SUCCESS;
01686 }
01687
01688 static int h323_no_debug(int fd, int argc, char *argv[])
01689 {
01690 if (argc != 3) {
01691 return RESULT_SHOWUSAGE;
01692 }
01693 h323debug = 0;
01694 ast_cli(fd, "H323 Debug disabled\n");
01695 return RESULT_SUCCESS;
01696 }
01697
01698 static int h323_gk_cycle(int fd, int argc, char *argv[])
01699 {
01700 #if 0
01701 if (argc != 3) {
01702 return RESULT_SHOWUSAGE;
01703 }
01704 h323_gk_urq();
01705
01706
01707 if (!gatekeeper_disable) {
01708 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
01709 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
01710 }
01711 }
01712 #endif
01713 return RESULT_SUCCESS;
01714 }
01715
01716 static int h323_ep_hangup(int fd, int argc, char *argv[])
01717 {
01718 if (argc != 3) {
01719 return RESULT_SHOWUSAGE;
01720 }
01721 if (h323_soft_hangup(argv[2])) {
01722 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
01723 } else {
01724 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
01725 }
01726 return RESULT_SUCCESS;
01727 }
01728
01729 static int h323_tokens_show(int fd, int argc, char *argv[])
01730 {
01731 if (argc != 3) {
01732 return RESULT_SHOWUSAGE;
01733 }
01734 h323_show_tokens();
01735 return RESULT_SUCCESS;
01736 }
01737
01738 static char trace_usage[] =
01739 "Usage: h.323 trace <level num>\n"
01740 " Enables H.323 stack tracing for debugging purposes\n";
01741
01742 static char no_trace_usage[] =
01743 "Usage: h.323 no trace\n"
01744 " Disables H.323 stack tracing for debugging purposes\n";
01745
01746 static char debug_usage[] =
01747 "Usage: h.323 debug\n"
01748 " Enables H.323 debug output\n";
01749
01750 static char no_debug_usage[] =
01751 "Usage: h.323 no debug\n"
01752 " Disables H.323 debug output\n";
01753
01754 static char show_codec_usage[] =
01755 "Usage: h.323 show codec\n"
01756 " Shows all enabled codecs\n";
01757
01758 static char show_cycle_usage[] =
01759 "Usage: h.323 gk cycle\n"
01760 " Manually re-register with the Gatekeper (Currently Disabled)\n";
01761
01762 static char show_hangup_usage[] =
01763 "Usage: h.323 hangup <token>\n"
01764 " Manually try to hang up call identified by <token>\n";
01765
01766 static char show_tokens_usage[] =
01767 "Usage: h.323 show tokens\n"
01768 " Print out all active call tokens\n";
01769
01770 static char h323_reload_usage[] =
01771 "Usage: h323 reload\n"
01772 " Reloads H.323 configuration from sip.conf\n";
01773
01774 static struct ast_cli_entry cli_trace =
01775 { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
01776 static struct ast_cli_entry cli_no_trace =
01777 { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
01778 static struct ast_cli_entry cli_debug =
01779 { { "h.323", "debug", NULL }, h323_do_debug, "Enable H.323 debug", debug_usage };
01780 static struct ast_cli_entry cli_no_debug =
01781 { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable H.323 debug", no_debug_usage };
01782 static struct ast_cli_entry cli_show_codecs =
01783 { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
01784 static struct ast_cli_entry cli_gk_cycle =
01785 { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
01786 static struct ast_cli_entry cli_hangup_call =
01787 { { "h.323", "hangup", NULL }, h323_ep_hangup, "Manually try to hang up a call", show_hangup_usage };
01788 static struct ast_cli_entry cli_show_tokens =
01789 { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Show all active call tokens", show_tokens_usage };
01790
01791 static int update_common_options(struct ast_variable *v, struct call_options *options)
01792 {
01793 unsigned int format;
01794 int tmp;
01795
01796 if (!strcasecmp(v->name, "allow")) {
01797 format = ast_getformatbyname(v->value);
01798 if (format < 1)
01799 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
01800 else
01801 options->capability |= format;
01802 } else if (!strcasecmp(v->name, "disallow")) {
01803 format = ast_getformatbyname(v->value);
01804 if (format < 1)
01805 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
01806 else
01807 options->capability &= ~format;
01808 } else if (!strcasecmp(v->name, "dtmfmode")) {
01809 if (!strcasecmp(v->value, "inband")) {
01810 options->dtmfmode = H323_DTMF_INBAND;
01811 } else if (!strcasecmp(v->value, "rfc2833")) {
01812 options->dtmfmode = H323_DTMF_RFC2833;
01813 } else {
01814 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
01815 options->dtmfmode = H323_DTMF_RFC2833;
01816 }
01817 } else if (!strcasecmp(v->name, "dtmfcodec")) {
01818 tmp = atoi(v->value);
01819 if (tmp < 96)
01820 ast_log(LOG_WARNING, "Invalid global dtmfcodec value %s\n", v->value);
01821 else
01822 options->dtmfcodec = tmp;
01823 } else if (!strcasecmp(v->name, "bridge")) {
01824 options->bridge = ast_true(v->value);
01825 } else if (!strcasecmp(v->name, "nat")) {
01826 options->nat = ast_true(v->value);
01827 } else if (!strcasecmp(v->name, "noFastStart")) {
01828 options->noFastStart = ast_true(v->value);
01829 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
01830 options->noH245Tunneling = ast_true(v->value);
01831 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
01832 options->noSilenceSuppression = ast_true(v->value);
01833 } else if (!strcasecmp(v->name, "progress_setup")) {
01834 tmp = atoi(v->value);
01835 if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
01836 ast_log(LOG_WARNING, "Invalid value %d for progress_setup at line %d, assuming 0\n", tmp, v->lineno);
01837 tmp = 0;
01838 }
01839 options->progress_setup = tmp;
01840 } else if (!strcasecmp(v->name, "progress_alert")) {
01841 tmp = atoi(v->value);
01842 if ((tmp != 0) && (tmp != 8)) {
01843 ast_log(LOG_WARNING, "Invalud value %d for progress_alert at line %d, assuming 0\n", tmp, v->lineno);
01844 tmp = 0;
01845 }
01846 options->progress_alert = tmp;
01847 } else if (!strcasecmp(v->name, "progress_audio")) {
01848 options->progress_audio = ast_true(v->value);
01849 } else
01850 return 1;
01851
01852 return 0;
01853 }
01854
01855 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
01856 {
01857 struct oh323_alias *alias;
01858
01859 alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
01860 if (alias) {
01861 memset(alias, 0, sizeof(struct oh323_alias));
01862 strncpy(alias->name, name, sizeof(alias->name) - 1);
01863 while (v) {
01864 if (!strcasecmp(v->name, "e164")) {
01865 strncpy(alias->e164, v->value, sizeof(alias->e164) - 1);
01866 } else if (!strcasecmp(v->name, "prefix")) {
01867 strncpy(alias->prefix, v->value, sizeof(alias->prefix) - 1);
01868 } else if (!strcasecmp(v->name, "context")) {
01869 strncpy(alias->context, v->value, sizeof(alias->context) - 1);
01870 } else if (!strcasecmp(v->name, "secret")) {
01871 strncpy(alias->secret, v->value, sizeof(alias->secret) - 1);
01872 } else {
01873 if (strcasecmp(v->value, "h323")) {
01874 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
01875 }
01876 }
01877 v = v->next;
01878 }
01879 }
01880 return alias;
01881 }
01882
01883 static struct oh323_user *build_user(char *name, struct ast_variable *v)
01884 {
01885 struct oh323_user *user;
01886 int format;
01887
01888 user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
01889 if (user) {
01890 memset(user, 0, sizeof(struct oh323_user));
01891 strncpy(user->name, name, sizeof(user->name) - 1);
01892 memcpy(&user->options, &global_options, sizeof(user->options));
01893
01894 strncpy(user->context, default_context, sizeof(user->context) - 1);
01895 while(v) {
01896 if (!strcasecmp(v->name, "context")) {
01897 strncpy(user->context, v->value, sizeof(user->context) - 1);
01898 } else if (!update_common_options(v, &user->options)) {
01899
01900 } else if (!strcasecmp(v->name, "secret")) {
01901 strncpy(user->secret, v->value, sizeof(user->secret) - 1);
01902 } else if (!strcasecmp(v->name, "callerid")) {
01903 strncpy(user->callerid, v->value, sizeof(user->callerid) - 1);
01904 } else if (!strcasecmp(v->name, "accountcode")) {
01905 strncpy(user->accountcode, v->value, sizeof(user->accountcode) - 1);
01906 } else if (!strcasecmp(v->name, "host")) {
01907 if (!strcasecmp(v->value, "dynamic")) {
01908 ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
01909 free(user);
01910 return NULL;
01911 } else if (ast_get_ip(&user->addr, v->value)) {
01912 free(user);
01913 return NULL;
01914 }
01915
01916 user->host = 1;
01917 } else if (!strcasecmp(v->name, "amaflags")) {
01918 format = ast_cdr_amaflags2int(v->value);
01919 if (format < 0) {
01920 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
01921 } else {
01922 user->amaflags = format;
01923 }
01924 }
01925 v = v->next;
01926 }
01927 }
01928 return user;
01929 }
01930
01931 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
01932 {
01933 struct oh323_peer *peer;
01934 struct oh323_peer *prev;
01935 struct ast_ha *oldha = NULL;
01936 int found=0;
01937
01938 prev = NULL;
01939 ast_mutex_lock(&peerl.lock);
01940 peer = peerl.peers;
01941
01942 while(peer) {
01943 if (!strcasecmp(peer->name, name)) {
01944 break;
01945 }
01946 prev = peer;
01947 peer = peer->next;
01948 }
01949
01950 if (peer) {
01951 found++;
01952
01953 if (prev) {
01954 prev->next = peer->next;
01955 } else {
01956 peerl.peers = peer->next;
01957 }
01958 ast_mutex_unlock(&peerl.lock);
01959 } else {
01960 ast_mutex_unlock(&peerl.lock);
01961 peer = (struct oh323_peer*)malloc(sizeof(struct oh323_peer));
01962 if (peer)
01963 memset(peer, 0, sizeof(struct oh323_peer));
01964 }
01965 if (peer) {
01966 if (!found) {
01967 strncpy(peer->name, name, sizeof(peer->name) - 1);
01968 peer->addr.sin_port = htons(h323_signalling_port);
01969 peer->addr.sin_family = AF_INET;
01970 }
01971 oldha = peer->ha;
01972 peer->ha = NULL;
01973 peer->addr.sin_family = AF_INET;
01974 memcpy(&peer->options, &global_options, sizeof(peer->options));
01975
01976 while(v) {
01977 if (!update_common_options(v, &peer->options)) {
01978
01979 } else if (!strcasecmp(v->name, "host")) {
01980 if (!strcasecmp(v->value, "dynamic")) {
01981 ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
01982 free(peer);
01983 return NULL;
01984 }
01985 if (ast_get_ip(&peer->addr, v->value)) {
01986 ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
01987 free(peer);
01988 return NULL;
01989 }
01990 } else if (!strcasecmp(v->name, "port")) {
01991 peer->addr.sin_port = htons(atoi(v->value));
01992 }
01993 v=v->next;
01994 }
01995 }
01996 return peer;
01997 }
01998
01999 int reload_config(void)
02000 {
02001 int format;
02002 struct ast_config *cfg;
02003 struct ast_variable *v;
02004 struct oh323_peer *peer = NULL;
02005 struct oh323_user *user = NULL;
02006 struct oh323_alias *alias = NULL;
02007 struct ast_hostent ahp; struct hostent *hp;
02008 char *cat;
02009 char *utype;
02010
02011 cfg = ast_config_load(config);
02012
02013
02014 if (!cfg) {
02015 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
02016 return 1;
02017 }
02018
02019
02020 if (!h323_end_point_exist()) {
02021 h323_end_point_create();
02022 }
02023 h323debug = 0;
02024 memset(&bindaddr, 0, sizeof(bindaddr));
02025 memset(&global_options, 0, sizeof(global_options));
02026 global_options.dtmfcodec = 101;
02027 global_options.dtmfmode = H323_DTMF_RFC2833;
02028 global_options.capability = ~0;
02029 global_options.bridge = 1;
02030 v = ast_variable_browse(cfg, "general");
02031 while(v) {
02032
02033 if (!strcasecmp(v->name, "port")) {
02034 h323_signalling_port = (int)strtol(v->value, NULL, 10);
02035 } else if (!strcasecmp(v->name, "bindaddr")) {
02036 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
02037 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
02038 } else {
02039 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
02040 }
02041 } else if (!strcasecmp(v->name, "tos")) {
02042 if (sscanf(v->value, "%d", &format)) {
02043 tos = format & 0xff;
02044 } else if (!strcasecmp(v->value, "lowdelay")) {
02045 tos = IPTOS_LOWDELAY;
02046 } else if (!strcasecmp(v->value, "throughput")) {
02047 tos = IPTOS_THROUGHPUT;
02048 } else if (!strcasecmp(v->value, "reliability")) {
02049 tos = IPTOS_RELIABILITY;
02050 } else if (!strcasecmp(v->value, "mincost")) {
02051 tos = IPTOS_MINCOST;
02052 } else if (!strcasecmp(v->value, "none")) {
02053 tos = 0;
02054 } else {
02055 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
02056 }
02057 } else if (!strcasecmp(v->name, "gatekeeper")) {
02058 if (!strcasecmp(v->value, "DISABLE")) {
02059 gatekeeper_disable = 1;
02060 usingGk = 0;
02061 } else if (!strcasecmp(v->value, "DISCOVER")) {
02062 gatekeeper_disable = 0;
02063 gatekeeper_discover = 1;
02064 usingGk = 1;
02065 } else {
02066 gatekeeper_disable = 0;
02067 usingGk = 1;
02068 strncpy(gatekeeper, v->value, sizeof(gatekeeper) - 1);
02069 }
02070 } else if (!strcasecmp(v->name, "secret")) {
02071 strncpy(secret, v->value, sizeof(secret) - 1);
02072 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
02073 gkroute = ast_true(v->value);
02074 } else if (!strcasecmp(v->name, "context")) {
02075 strncpy(default_context, v->value, sizeof(default_context) - 1);
02076 ast_verbose(VERBOSE_PREFIX_2 "Setting default context to %s\n", default_context);
02077 } else if (!strcasecmp(v->name, "UserByAlias")) {
02078 userbyalias = ast_true(v->value);
02079 } else if (!update_common_options(v, &global_options)) {
02080
02081 }
02082 v = v->next;
02083 }
02084
02085 cat = ast_category_browse(cfg, NULL);
02086 while(cat) {
02087 if (strcasecmp(cat, "general")) {
02088 utype = ast_variable_retrieve(cfg, cat, "type");
02089 if (utype) {
02090 if (!strcasecmp(utype, "user")) {
02091 user = build_user(cat, ast_variable_browse(cfg, cat));
02092 if (user) {
02093 ast_mutex_lock(&userl.lock);
02094 user->next = userl.users;
02095 userl.users = user;
02096 ast_mutex_unlock(&userl.lock);
02097 }
02098 } else if (!strcasecmp(utype, "peer")) {
02099 peer = build_peer(cat, ast_variable_browse(cfg, cat));
02100 if (peer) {
02101 ast_mutex_lock(&peerl.lock);
02102 peer->next = peerl.peers;
02103 peerl.peers = peer;
02104 ast_mutex_unlock(&peerl.lock);
02105 }
02106 } else if (!strcasecmp(utype, "friend")) {
02107 user = build_user(cat, ast_variable_browse(cfg, cat));
02108 peer = build_peer(cat, ast_variable_browse(cfg, cat));
02109 if (user) {
02110 ast_mutex_lock(&userl.lock);
02111 user->next = userl.users;
02112 userl.users = user;
02113 ast_mutex_unlock(&userl.lock);
02114 }
02115 if (peer) {
02116 ast_mutex_lock(&peerl.lock);
02117 peer->next = peerl.peers;
02118 peerl.peers = peer;
02119 ast_mutex_unlock(&peerl.lock);
02120 }
02121 } else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias")) {
02122 alias = build_alias(cat, ast_variable_browse(cfg, cat));
02123 if (alias) {
02124 ast_mutex_lock(&aliasl.lock);
02125 alias->next = aliasl.aliases;
02126 aliasl.aliases = alias;
02127 ast_mutex_unlock(&aliasl.lock);
02128 }
02129 } else {
02130 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
02131 }
02132 } else {
02133 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
02134 }
02135 }
02136 cat = ast_category_browse(cfg, cat);
02137 }
02138 ast_config_destroy(cfg);
02139
02140
02141 while (alias) {
02142 if (h323_set_alias(alias)) {
02143 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
02144 return -1;
02145 }
02146 alias = alias->next;
02147 }
02148
02149 return 0;
02150 }
02151
02152 void delete_users(void)
02153 {
02154 struct oh323_user *user, *userlast;
02155 struct oh323_peer *peer;
02156
02157
02158 ast_mutex_lock(&userl.lock);
02159 for (user=userl.users;user;) {
02160 userlast = user;
02161 user=user->next;
02162 free(userlast);
02163 }
02164 userl.users=NULL;
02165 ast_mutex_unlock(&userl.lock);
02166 ast_mutex_lock(&peerl.lock);
02167 for (peer=peerl.peers;peer;) {
02168
02169 peer->delme = 1;
02170 peer = peer->next;
02171 }
02172 ast_mutex_unlock(&peerl.lock);
02173 }
02174
02175 void delete_aliases(void)
02176 {
02177 struct oh323_alias *alias, *aliaslast;
02178
02179
02180 ast_mutex_lock(&aliasl.lock);
02181 for (alias=aliasl.aliases;alias;) {
02182 aliaslast = alias;
02183 alias=alias->next;
02184 free(aliaslast);
02185 }
02186 aliasl.aliases=NULL;
02187 ast_mutex_unlock(&aliasl.lock);
02188 }
02189
02190 void prune_peers(void)
02191 {
02192
02193 struct oh323_peer *peer, *peerlast, *peernext;
02194 ast_mutex_lock(&peerl.lock);
02195 peerlast = NULL;
02196 for (peer=peerl.peers;peer;) {
02197 peernext = peer->next;
02198 if (peer->delme) {
02199 free(peer);
02200 if (peerlast) {
02201 peerlast->next = peernext;
02202 } else {
02203 peerl.peers = peernext;
02204 }
02205 } else {
02206 peerlast = peer;
02207 }
02208 peer = peernext;
02209 }
02210 ast_mutex_unlock(&peerl.lock);
02211 }
02212
02213 static int h323_reload(int fd, int argc, char *argv[])
02214 {
02215 ast_mutex_lock(&h323_reload_lock);
02216 if (h323_reloading) {
02217 ast_verbose("Previous H.323 reload not yet done\n");
02218 } else {
02219 h323_reloading = 1;
02220 }
02221 ast_mutex_unlock(&h323_reload_lock);
02222 restart_monitor();
02223 return 0;
02224 }
02225
02226 static int h323_do_reload(void)
02227 {
02228 delete_users();
02229 delete_aliases();
02230 prune_peers();
02231 reload_config();
02232 restart_monitor();
02233 return 0;
02234 }
02235
02236 int reload(void)
02237 {
02238 return h323_reload(0, 0, NULL);
02239 }
02240
02241 static struct ast_cli_entry cli_h323_reload =
02242 { { "h.323", "reload", NULL }, h323_reload, "Reload H.323 configuration", h323_reload_usage };
02243
02244 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
02245 {
02246 struct oh323_pvt *pvt;
02247 pvt = (struct oh323_pvt *) chan->tech_pvt;
02248 if (pvt && pvt->rtp && pvt->options.bridge) {
02249 return pvt->rtp;
02250 }
02251 return NULL;
02252 }
02253
02254 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
02255 {
02256 return NULL;
02257 }
02258
02259 static char *convertcap(int cap)
02260 {
02261 switch (cap) {
02262 case AST_FORMAT_G723_1:
02263 return "G.723";
02264 case AST_FORMAT_GSM:
02265 return "GSM";
02266 case AST_FORMAT_ULAW:
02267 return "ULAW";
02268 case AST_FORMAT_ALAW:
02269 return "ALAW";
02270 case AST_FORMAT_ADPCM:
02271 return "G.728";
02272 case AST_FORMAT_G729A:
02273 return "G.729";
02274 case AST_FORMAT_SPEEX:
02275 return "SPEEX";
02276 case AST_FORMAT_ILBC:
02277 return "ILBC";
02278 default:
02279 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
02280 return NULL;
02281 }
02282 }
02283
02284 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
02285 {
02286
02287 struct oh323_pvt *pvt;
02288 struct sockaddr_in them;
02289 struct sockaddr_in us;
02290 char *mode;
02291 char iabuf[INET_ADDRSTRLEN];
02292
02293 if (!rtp) {
02294 return 0;
02295 }
02296
02297 mode = convertcap(chan->writeformat);
02298 pvt = (struct oh323_pvt *) chan->tech_pvt;
02299 if (!pvt) {
02300 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
02301 return -1;
02302 }
02303 ast_rtp_get_peer(rtp, &them);
02304 ast_rtp_get_us(rtp, &us);
02305 h323_native_bridge(pvt->cd.call_token, ast_inet_ntoa(iabuf, sizeof(iabuf), them.sin_addr), mode);
02306 return 0;
02307 }
02308
02309 static struct ast_rtp_protocol oh323_rtp = {
02310 .type = type,
02311 .get_rtp_info = oh323_get_rtp_peer,
02312 .get_vrtp_info = oh323_get_vrtp_peer,
02313 .set_rtp_peer= oh323_set_rtp_peer,
02314 };
02315
02316 int load_module()
02317 {
02318 int res;
02319 ast_mutex_init(&userl.lock);
02320 ast_mutex_init(&peerl.lock);
02321 ast_mutex_init(&aliasl.lock);
02322 sched = sched_context_create();
02323 if (!sched) {
02324 ast_log(LOG_WARNING, "Unable to create schedule context\n");
02325 }
02326 io = io_context_create();
02327 if (!io) {
02328 ast_log(LOG_WARNING, "Unable to create I/O context\n");
02329 }
02330 res = reload_config();
02331 if (res) {
02332 return 0;
02333 } else {
02334
02335 if (ast_channel_register(&oh323_tech)) {
02336 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
02337 h323_end_process();
02338 return -1;
02339 }
02340 ast_cli_register(&cli_debug);
02341 ast_cli_register(&cli_no_debug);
02342 ast_cli_register(&cli_trace);
02343 ast_cli_register(&cli_no_trace);
02344 ast_cli_register(&cli_show_codecs);
02345 ast_cli_register(&cli_gk_cycle);
02346 ast_cli_register(&cli_hangup_call);
02347 ast_cli_register(&cli_show_tokens);
02348 ast_cli_register(&cli_h323_reload);
02349
02350 ast_rtp_proto_register(&oh323_rtp);
02351
02352
02353 h323_callback_register(setup_incoming_call,
02354 setup_outgoing_call,
02355 external_rtp_create,
02356 setup_rtp_connection,
02357 cleanup_connection,
02358 chan_ringing,
02359 connection_made,
02360 send_digit,
02361 answer_call,
02362 progress,
02363 set_dtmf_payload,
02364 hangup_connection,
02365 set_local_capabilities);
02366
02367 if (h323_start_listener(h323_signalling_port, bindaddr)) {
02368 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
02369 return -1;
02370 }
02371
02372 if (!gatekeeper_disable) {
02373 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
02374 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
02375 return 0;
02376 }
02377 }
02378
02379 restart_monitor();
02380 }
02381 return res;
02382 }
02383
02384 int unload_module()
02385 {
02386 struct oh323_pvt *p, *pl;
02387
02388
02389 ast_cli_unregister(&cli_debug);
02390 ast_cli_unregister(&cli_no_debug);
02391 ast_cli_unregister(&cli_trace);
02392 ast_cli_unregister(&cli_no_trace);
02393 ast_cli_unregister(&cli_show_codecs);
02394 ast_cli_unregister(&cli_gk_cycle);
02395 ast_cli_unregister(&cli_hangup_call);
02396 ast_cli_unregister(&cli_show_tokens);
02397 ast_cli_unregister(&cli_h323_reload);
02398 ast_rtp_proto_unregister(&oh323_rtp);
02399 ast_channel_unregister(&oh323_tech);
02400
02401 if (!ast_mutex_lock(&iflock)) {
02402
02403 p = iflist;
02404 while(p) {
02405 if (p->owner) {
02406 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
02407 }
02408 p = p->next;
02409 }
02410 iflist = NULL;
02411 ast_mutex_unlock(&iflock);
02412 } else {
02413 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
02414 return -1;
02415 }
02416 if (!ast_mutex_lock(&monlock)) {
02417 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
02418
02419 pthread_cancel(monitor_thread);
02420 pthread_kill(monitor_thread, SIGURG);
02421 pthread_join(monitor_thread, NULL);
02422 }
02423 monitor_thread = AST_PTHREADT_STOP;
02424 ast_mutex_unlock(&monlock);
02425 } else {
02426 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
02427 return -1;
02428 }
02429 if (!ast_mutex_lock(&iflock)) {
02430
02431 p = iflist;
02432 while(p) {
02433 pl = p;
02434 p = p->next;
02435
02436 ast_mutex_destroy(&pl->lock);
02437 free(pl);
02438 }
02439 iflist = NULL;
02440 ast_mutex_unlock(&iflock);
02441 } else {
02442 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
02443 return -1;
02444 }
02445 h323_gk_urq();
02446 h323_end_process();
02447 io_context_destroy(io);
02448 sched_context_destroy(sched);
02449 delete_users();
02450 delete_aliases();
02451 prune_peers();
02452 ast_mutex_destroy(&aliasl.lock);
02453 ast_mutex_destroy(&userl.lock);
02454 ast_mutex_destroy(&peerl.lock);
02455 return 0;
02456 }
02457
02458 int usecount()
02459 {
02460 return usecnt;
02461 }
02462
02463 char *description()
02464 {
02465 return (char *) desc;
02466 }
02467
02468 char *key()
02469 {
02470 return ASTERISK_GPL_KEY;
02471 }