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 #include "asterisk.h"
00032
00033 #if defined(HAVE_SS7)
00034
00035 #include <signal.h>
00036
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/causes.h"
00039 #include "asterisk/musiconhold.h"
00040 #include "asterisk/cli.h"
00041 #include "asterisk/transcap.h"
00042
00043 #include "sig_ss7.h"
00044
00045
00046
00047 static const char *sig_ss7_call_level2str(enum sig_ss7_call_level level)
00048 {
00049 switch (level) {
00050 case SIG_SS7_CALL_LEVEL_IDLE:
00051 return "Idle";
00052 case SIG_SS7_CALL_LEVEL_ALLOCATED:
00053 return "Allocated";
00054 case SIG_SS7_CALL_LEVEL_CONTINUITY:
00055 return "Continuity";
00056 case SIG_SS7_CALL_LEVEL_SETUP:
00057 return "Setup";
00058 case SIG_SS7_CALL_LEVEL_PROCEEDING:
00059 return "Proceeding";
00060 case SIG_SS7_CALL_LEVEL_ALERTING:
00061 return "Alerting";
00062 case SIG_SS7_CALL_LEVEL_CONNECT:
00063 return "Connect";
00064 case SIG_SS7_CALL_LEVEL_GLARE:
00065 return "Glare";
00066 }
00067 return "Unknown";
00068 }
00069
00070 #define SIG_SS7_DEADLOCK_AVOIDANCE(p) \
00071 do { \
00072 sig_ss7_unlock_private(p); \
00073 usleep(1); \
00074 sig_ss7_lock_private(p); \
00075 } while (0)
00076
00077 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
00078 {
00079 if (p->calls->unlock_private) {
00080 p->calls->unlock_private(p->chan_pvt);
00081 }
00082 }
00083
00084 static void sig_ss7_lock_private(struct sig_ss7_chan *p)
00085 {
00086 if (p->calls->lock_private) {
00087 p->calls->lock_private(p->chan_pvt);
00088 }
00089 }
00090
00091 void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
00092 {
00093 p->inalarm = in_alarm;
00094 if (p->calls->set_alarm) {
00095 p->calls->set_alarm(p->chan_pvt, in_alarm);
00096 }
00097 }
00098
00099 static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
00100 {
00101 if (p->calls->set_dialing) {
00102 p->calls->set_dialing(p->chan_pvt, is_dialing);
00103 }
00104 }
00105
00106 static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
00107 {
00108 if (p->calls->set_digital) {
00109 p->calls->set_digital(p->chan_pvt, is_digital);
00110 }
00111 }
00112
00113 static void sig_ss7_set_outgoing(struct sig_ss7_chan *p, int is_outgoing)
00114 {
00115 p->outgoing = is_outgoing;
00116 if (p->calls->set_outgoing) {
00117 p->calls->set_outgoing(p->chan_pvt, is_outgoing);
00118 }
00119 }
00120
00121 static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
00122 {
00123 if (p->calls->set_inservice) {
00124 p->calls->set_inservice(p->chan_pvt, is_inservice);
00125 }
00126 }
00127
00128 static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
00129 {
00130 p->locallyblocked = is_blocked;
00131 if (p->calls->set_locallyblocked) {
00132 p->calls->set_locallyblocked(p->chan_pvt, is_blocked);
00133 }
00134 }
00135
00136 static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
00137 {
00138 p->remotelyblocked = is_blocked;
00139 if (p->calls->set_remotelyblocked) {
00140 p->calls->set_remotelyblocked(p->chan_pvt, is_blocked);
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
00154 {
00155 struct ast_party_caller caller;
00156
00157 if (p->calls->set_callerid) {
00158 ast_party_caller_init(&caller);
00159
00160 caller.id.name.str = p->cid_name;
00161 caller.id.name.presentation = p->callingpres;
00162 caller.id.name.valid = 1;
00163
00164 caller.id.number.str = p->cid_num;
00165 caller.id.number.plan = p->cid_ton;
00166 caller.id.number.presentation = p->callingpres;
00167 caller.id.number.valid = 1;
00168
00169 if (!ast_strlen_zero(p->cid_subaddr)) {
00170 caller.id.subaddress.valid = 1;
00171
00172
00173 caller.id.subaddress.str = p->cid_subaddr;
00174 }
00175
00176 caller.ani.number.str = p->cid_ani;
00177
00178
00179 caller.ani.number.valid = 1;
00180
00181 caller.ani2 = p->cid_ani2;
00182 p->calls->set_callerid(p->chan_pvt, &caller);
00183 }
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
00197 {
00198 if (p->calls->set_dnid) {
00199 p->calls->set_dnid(p->chan_pvt, dnid);
00200 }
00201 }
00202
00203 static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
00204 {
00205 int res;
00206
00207 if (p->calls->play_tone) {
00208 res = p->calls->play_tone(p->chan_pvt, tone);
00209 } else {
00210 res = -1;
00211 }
00212 return res;
00213 }
00214
00215 static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
00216 {
00217 if (p->calls->set_echocanceller) {
00218 return p->calls->set_echocanceller(p->chan_pvt, enable);
00219 }
00220 return -1;
00221 }
00222
00223 static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
00224 {
00225 if (p->loopedback != enable) {
00226 p->loopedback = enable;
00227 if (p->calls->set_loopback) {
00228 p->calls->set_loopback(p->chan_pvt, enable);
00229 }
00230 }
00231 }
00232
00233 static struct ast_channel *sig_ss7_new_ast_channel(struct sig_ss7_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
00234 {
00235 struct ast_channel *ast;
00236
00237 if (p->calls->new_ast_channel) {
00238 ast = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
00239 } else {
00240 return NULL;
00241 }
00242 if (!ast) {
00243 return NULL;
00244 }
00245
00246 if (!p->owner) {
00247 p->owner = ast;
00248 }
00249 p->alreadyhungup = 0;
00250 ast->transfercapability = transfercapability;
00251 pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
00252 ast_transfercapability2str(transfercapability));
00253 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
00254 sig_ss7_set_digital(p, 1);
00255 }
00256
00257 return ast;
00258 }
00259
00260 static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
00261 {
00262 if (linkset->calls->handle_link_exception) {
00263 linkset->calls->handle_link_exception(linkset, which);
00264 }
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
00281 {
00282 for (;;) {
00283 if (!ss7->pvts[chanpos]->owner) {
00284
00285 break;
00286 }
00287 if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
00288
00289 break;
00290 }
00291
00292 ast_mutex_unlock(&ss7->lock);
00293 SIG_SS7_DEADLOCK_AVOIDANCE(ss7->pvts[chanpos]);
00294 ast_mutex_lock(&ss7->lock);
00295 }
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
00313 {
00314 sig_ss7_lock_owner(ss7, chanpos);
00315 if (ss7->pvts[chanpos]->owner) {
00316 ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
00317 ast_channel_unlock(ss7->pvts[chanpos]->owner);
00318 }
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
00336 {
00337 struct ast_frame f = {AST_FRAME_CONTROL, };
00338 struct sig_ss7_chan *p = ss7->pvts[chanpos];
00339
00340 if (p->calls->queue_control) {
00341 p->calls->queue_control(p->chan_pvt, subclass);
00342 }
00343
00344 f.subclass.integer = subclass;
00345 sig_ss7_queue_frame(ss7, chanpos, &f);
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
00360 {
00361 int i;
00362 int winner = -1;
00363 for (i = 0; i < linkset->numchans; i++) {
00364 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
00365 winner = i;
00366 break;
00367 }
00368 }
00369 return winner;
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 static int ss7_find_cic_gripe(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc, const char *msg_name)
00385 {
00386 int chanpos;
00387
00388 chanpos = ss7_find_cic(linkset, cic, dpc);
00389 if (chanpos < 0) {
00390 ast_log(LOG_WARNING, "Linkset %d: SS7 %s requested unconfigured CIC/DPC %d/%d.\n",
00391 linkset->span, msg_name, cic, dpc);
00392 return -1;
00393 }
00394 return chanpos;
00395 }
00396
00397 static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
00398 {
00399 unsigned char status[32];
00400 struct sig_ss7_chan *p = NULL;
00401 int i, offset;
00402
00403 for (i = 0; i < linkset->numchans; i++) {
00404 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
00405 p = linkset->pvts[i];
00406 offset = p->cic - startcic;
00407 status[offset] = 0;
00408 if (p->locallyblocked)
00409 status[offset] |= (1 << 0) | (1 << 4);
00410 if (p->remotelyblocked)
00411 status[offset] |= (1 << 1) | (1 << 5);
00412 if (p->ss7call) {
00413 if (p->outgoing)
00414 status[offset] |= (1 << 3);
00415 else
00416 status[offset] |= (1 << 2);
00417 } else
00418 status[offset] |= 0x3 << 2;
00419 }
00420 }
00421
00422 if (p)
00423 isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
00424 else
00425 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
00426
00427 }
00428
00429 static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
00430 {
00431 int i;
00432
00433 for (i = 0; i < linkset->numchans; i++) {
00434 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
00435 sig_ss7_lock_private(linkset->pvts[i]);
00436 if (linkset->pvts[i]->owner)
00437 linkset->pvts[i]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
00438 sig_ss7_unlock_private(linkset->pvts[i]);
00439 }
00440 }
00441 }
00442
00443 static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
00444 {
00445 int i;
00446
00447
00448 for (i = 0; i < linkset->numchans; i++) {
00449 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
00450 if (state) {
00451 if (state[i])
00452 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
00453 } else
00454 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
00455 }
00456 }
00457 }
00458
00459 static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
00460 {
00461 int i;
00462
00463 for (i = 0; i < linkset->numchans; i++) {
00464 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
00465 sig_ss7_set_inservice(linkset->pvts[i], 1);
00466 }
00467 }
00468
00469 static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
00470 {
00471 int i, startcic = -1, endcic, dpc;
00472
00473 if (linkset->numchans <= 0)
00474 return;
00475
00476 startcic = linkset->pvts[0]->cic;
00477
00478 dpc = linkset->pvts[0]->dpc;
00479
00480 for (i = 0; i < linkset->numchans; i++) {
00481 if (linkset->pvts[i+1] && linkset->pvts[i+1]->dpc == dpc && ((linkset->pvts[i+1]->cic - linkset->pvts[i]->cic) == 1) && (linkset->pvts[i]->cic - startcic < 31)) {
00482 continue;
00483 } else {
00484 endcic = linkset->pvts[i]->cic;
00485 ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
00486 isup_grs(linkset->ss7, startcic, endcic, dpc);
00487
00488
00489 if (linkset->pvts[i+1]) {
00490 startcic = linkset->pvts[i+1]->cic;
00491 dpc = linkset->pvts[i+1]->dpc;
00492 }
00493 }
00494 }
00495 }
00496
00497
00498 static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
00499 {
00500 struct ss7 *ss7 = linkset->ss7;
00501 int law;
00502 struct ast_channel *c;
00503 char tmp[256];
00504
00505 if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
00506 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
00507 isup_acm(ss7, p->ss7call);
00508 } else {
00509 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
00510 }
00511
00512 if (linkset->type == SS7_ITU) {
00513 law = SIG_SS7_ALAW;
00514 } else {
00515 law = SIG_SS7_ULAW;
00516 }
00517
00518
00519
00520
00521
00522
00523 ast_mutex_unlock(&linkset->lock);
00524 sig_ss7_unlock_private(p);
00525 c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL);
00526 if (!c) {
00527 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
00528 ast_mutex_lock(&linkset->lock);
00529 sig_ss7_lock_private(p);
00530 isup_rel(linkset->ss7, p->ss7call, -1);
00531 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
00532 p->alreadyhungup = 1;
00533 return;
00534 }
00535
00536
00537 ast_channel_lock(c);
00538 sig_ss7_lock_private(p);
00539
00540 sig_ss7_set_echocanceller(p, 1);
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 if (!ast_strlen_zero(p->charge_number)) {
00554 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
00555
00556 p->charge_number[0] = 0;
00557 }
00558 if (!ast_strlen_zero(p->gen_add_number)) {
00559 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
00560
00561 p->gen_add_number[0] = 0;
00562 }
00563 if (!ast_strlen_zero(p->jip_number)) {
00564 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
00565
00566 p->jip_number[0] = 0;
00567 }
00568 if (!ast_strlen_zero(p->gen_dig_number)) {
00569 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
00570
00571 p->gen_dig_number[0] = 0;
00572 }
00573 if (!ast_strlen_zero(p->orig_called_num)) {
00574 pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
00575
00576 p->orig_called_num[0] = 0;
00577 }
00578
00579 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
00580 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
00581
00582 p->gen_dig_type = 0;
00583
00584 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
00585 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
00586
00587 p->gen_dig_scheme = 0;
00588
00589 if (!ast_strlen_zero(p->lspi_ident)) {
00590 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
00591
00592 p->lspi_ident[0] = 0;
00593 }
00594
00595 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
00596 pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
00597
00598 p->call_ref_ident = 0;
00599
00600 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
00601 pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
00602
00603 p->call_ref_pc = 0;
00604
00605 snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
00606 pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
00607
00608 p->calling_party_cat = 0;
00609
00610 if (!ast_strlen_zero(p->redirecting_num)) {
00611 pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
00612
00613 p->redirecting_num[0] = 0;
00614 }
00615 if (!ast_strlen_zero(p->generic_name)) {
00616 pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
00617
00618 p->generic_name[0] = 0;
00619 }
00620
00621 sig_ss7_unlock_private(p);
00622 ast_channel_unlock(c);
00623
00624 if (ast_pbx_start(c)) {
00625 ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", c->name, p->cic);
00626 ast_hangup(c);
00627 } else {
00628 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
00629 }
00630
00631
00632 ast_mutex_lock(&linkset->lock);
00633 sig_ss7_lock_private(p);
00634 }
00635
00636 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
00637 {
00638 if (ast_strlen_zero(number)) {
00639 if (size) {
00640 *buf = '\0';
00641 }
00642 return;
00643 }
00644 switch (nai) {
00645 case SS7_NAI_INTERNATIONAL:
00646 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
00647 break;
00648 case SS7_NAI_NATIONAL:
00649 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
00650 break;
00651 case SS7_NAI_SUBSCRIBER:
00652 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
00653 break;
00654 case SS7_NAI_UNKNOWN:
00655 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
00656 break;
00657 default:
00658 snprintf(buf, size, "%s", number);
00659 break;
00660 }
00661 }
00662
00663 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
00664 {
00665 return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
00666 }
00667
00668
00669 void *ss7_linkset(void *data)
00670 {
00671 int res, i;
00672 struct timeval *next = NULL, tv;
00673 struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
00674 struct ss7 *ss7 = linkset->ss7;
00675 ss7_event *e = NULL;
00676 struct sig_ss7_chan *p;
00677 int chanpos;
00678 struct pollfd pollers[SIG_SS7_NUM_DCHANS];
00679 int nextms = 0;
00680
00681 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
00682
00683 ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
00684 ast_mutex_lock(&linkset->lock);
00685 ss7_start(ss7);
00686 ast_mutex_unlock(&linkset->lock);
00687
00688 for (;;) {
00689 ast_mutex_lock(&linkset->lock);
00690 if ((next = ss7_schedule_next(ss7))) {
00691 tv = ast_tvnow();
00692 tv.tv_sec = next->tv_sec - tv.tv_sec;
00693 tv.tv_usec = next->tv_usec - tv.tv_usec;
00694 if (tv.tv_usec < 0) {
00695 tv.tv_usec += 1000000;
00696 tv.tv_sec -= 1;
00697 }
00698 if (tv.tv_sec < 0) {
00699 tv.tv_sec = 0;
00700 tv.tv_usec = 0;
00701 }
00702 nextms = tv.tv_sec * 1000;
00703 nextms += tv.tv_usec / 1000;
00704 }
00705
00706 for (i = 0; i < linkset->numsigchans; i++) {
00707 pollers[i].fd = linkset->fds[i];
00708 pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
00709 pollers[i].revents = 0;
00710 }
00711 ast_mutex_unlock(&linkset->lock);
00712
00713 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
00714 pthread_testcancel();
00715 res = poll(pollers, linkset->numsigchans, nextms);
00716 pthread_testcancel();
00717 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
00718
00719 if ((res < 0) && (errno != EINTR)) {
00720 ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
00721 } else if (!res) {
00722 ast_mutex_lock(&linkset->lock);
00723 ss7_schedule_run(ss7);
00724 ast_mutex_unlock(&linkset->lock);
00725 continue;
00726 }
00727
00728 ast_mutex_lock(&linkset->lock);
00729 for (i = 0; i < linkset->numsigchans; i++) {
00730 if (pollers[i].revents & POLLPRI) {
00731 sig_ss7_handle_link_exception(linkset, i);
00732 }
00733 if (pollers[i].revents & POLLIN) {
00734 res = ss7_read(ss7, pollers[i].fd);
00735 }
00736 if (pollers[i].revents & POLLOUT) {
00737 res = ss7_write(ss7, pollers[i].fd);
00738 if (res < 0) {
00739 ast_debug(1, "Error in write %s\n", strerror(errno));
00740 }
00741 }
00742 }
00743
00744 while ((e = ss7_check_event(ss7))) {
00745 if (linkset->debug) {
00746 ast_verbose("Linkset %d: Processing event: %s\n",
00747 linkset->span, ss7_event2str(e->e));
00748 }
00749
00750 switch (e->e) {
00751 case SS7_EVENT_UP:
00752 if (linkset->state != LINKSET_STATE_UP) {
00753 ast_verbose("--- SS7 Up ---\n");
00754 ss7_reset_linkset(linkset);
00755 }
00756 linkset->state = LINKSET_STATE_UP;
00757 break;
00758 case SS7_EVENT_DOWN:
00759 ast_verbose("--- SS7 Down ---\n");
00760 linkset->state = LINKSET_STATE_DOWN;
00761 for (i = 0; i < linkset->numchans; i++) {
00762 p = linkset->pvts[i];
00763 if (p) {
00764 sig_ss7_set_alarm(p, 1);
00765 }
00766 }
00767 break;
00768 case MTP2_LINK_UP:
00769 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
00770 break;
00771 case MTP2_LINK_DOWN:
00772 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
00773 break;
00774 case ISUP_EVENT_CPG:
00775 chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
00776 if (chanpos < 0) {
00777 break;
00778 }
00779 p = linkset->pvts[chanpos];
00780 sig_ss7_lock_private(p);
00781 switch (e->cpg.event) {
00782 case CPG_EVENT_ALERTING:
00783 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
00784 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
00785 }
00786 sig_ss7_lock_owner(linkset, chanpos);
00787 if (p->owner) {
00788 ast_setstate(p->owner, AST_STATE_RINGING);
00789 ast_channel_unlock(p->owner);
00790 }
00791 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
00792 break;
00793 case CPG_EVENT_PROGRESS:
00794 case CPG_EVENT_INBANDINFO:
00795 {
00796 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
00797 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
00798 p->progress = 1;
00799 sig_ss7_set_dialing(p, 0);
00800 #if 0
00801 if (p->dsp && p->dsp_features) {
00802 ast_dsp_set_features(p->dsp, p->dsp_features);
00803 p->dsp_features = 0;
00804 }
00805 #endif
00806 }
00807 break;
00808 default:
00809 ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
00810 break;
00811 }
00812
00813 sig_ss7_unlock_private(p);
00814 break;
00815 case ISUP_EVENT_RSC:
00816 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
00817 chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
00818 if (chanpos < 0) {
00819 break;
00820 }
00821 p = linkset->pvts[chanpos];
00822 sig_ss7_lock_private(p);
00823 sig_ss7_set_inservice(p, 1);
00824 sig_ss7_set_remotelyblocked(p, 0);
00825 isup_set_call_dpc(e->rsc.call, p->dpc);
00826 sig_ss7_lock_owner(linkset, chanpos);
00827 p->ss7call = NULL;
00828 if (p->owner) {
00829 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
00830 ast_channel_unlock(p->owner);
00831 }
00832 sig_ss7_unlock_private(p);
00833 isup_rlc(ss7, e->rsc.call);
00834 break;
00835 case ISUP_EVENT_GRS:
00836 ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
00837 chanpos = ss7_find_cic_gripe(linkset, e->grs.startcic, e->grs.opc, "GRS");
00838 if (chanpos < 0) {
00839 break;
00840 }
00841 p = linkset->pvts[chanpos];
00842 isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
00843 ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
00844 ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
00845 break;
00846 case ISUP_EVENT_CQM:
00847 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
00848 ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
00849 break;
00850 case ISUP_EVENT_GRA:
00851 ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
00852 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
00853 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
00854 break;
00855 case ISUP_EVENT_IAM:
00856 ast_debug(1, "Got IAM for CIC %d and called number %s, calling number %s\n", e->iam.cic, e->iam.called_party_num, e->iam.calling_party_num);
00857 chanpos = ss7_find_cic_gripe(linkset, e->iam.cic, e->iam.opc, "IAM");
00858 if (chanpos < 0) {
00859 isup_rel(ss7, e->iam.call, -1);
00860 break;
00861 }
00862 p = linkset->pvts[chanpos];
00863 sig_ss7_lock_private(p);
00864 sig_ss7_lock_owner(linkset, chanpos);
00865 if (p->call_level != SIG_SS7_CALL_LEVEL_IDLE) {
00866
00867
00868
00869
00870
00871
00872
00873
00874 ast_debug(1,
00875 "Linkset %d: SS7 IAM glare on CIC/DPC %d/%d. Dropping both calls.\n",
00876 linkset->span, e->iam.cic, e->iam.opc);
00877 if (p->call_level == SIG_SS7_CALL_LEVEL_ALLOCATED) {
00878
00879
00880
00881 p->alreadyhungup = 1;
00882 isup_rel(ss7, e->iam.call, -1);
00883 }
00884 p->call_level = SIG_SS7_CALL_LEVEL_GLARE;
00885 if (p->owner) {
00886 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
00887 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
00888 ast_channel_unlock(p->owner);
00889 }
00890 sig_ss7_unlock_private(p);
00891 break;
00892 }
00893
00894
00895
00896
00897 ast_assert(!p->owner);
00898
00899
00900 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
00901 p->ss7call = e->iam.call;
00902
00903 isup_set_call_dpc(p->ss7call, p->dpc);
00904
00905 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
00906 ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
00907 p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
00908 } else
00909 p->cid_num[0] = 0;
00910
00911
00912 if (!ast_strlen_zero(e->iam.called_party_num)) {
00913 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
00914 e->iam.called_party_num, e->iam.called_nai);
00915 } else {
00916 p->exten[0] = '\0';
00917 }
00918 sig_ss7_set_dnid(p, p->exten);
00919
00920 if (p->immediate) {
00921 p->exten[0] = 's';
00922 p->exten[1] = '\0';
00923 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
00924 char *st;
00925 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
00926 st = strchr(p->exten, '#');
00927 if (st) {
00928 *st = '\0';
00929 }
00930 } else {
00931 p->exten[0] = '\0';
00932 }
00933
00934 p->cid_ani[0] = '\0';
00935 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
00936 ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
00937 else
00938 p->cid_name[0] = '\0';
00939
00940 p->cid_ani2 = e->iam.oli_ani2;
00941 p->cid_ton = 0;
00942 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
00943 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
00944 p->gen_add_type = e->iam.gen_add_type;
00945 p->gen_add_nai = e->iam.gen_add_nai;
00946 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
00947 p->gen_add_num_plan = e->iam.gen_add_num_plan;
00948 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
00949 p->gen_dig_type = e->iam.gen_dig_type;
00950 p->gen_dig_scheme = e->iam.gen_dig_scheme;
00951 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
00952 ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
00953 ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
00954 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
00955 p->calling_party_cat = e->iam.calling_party_cat;
00956
00957 sig_ss7_set_caller_id(p);
00958
00959 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
00960 if (e->iam.cot_check_required) {
00961 p->call_level = SIG_SS7_CALL_LEVEL_CONTINUITY;
00962 sig_ss7_loopback(p, 1);
00963 } else {
00964 ss7_start_call(p, linkset);
00965 }
00966 } else {
00967 ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
00968 p->alreadyhungup = 1;
00969 isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
00970 }
00971 sig_ss7_unlock_private(p);
00972 break;
00973 case ISUP_EVENT_COT:
00974 chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
00975 if (chanpos < 0) {
00976 isup_rel(ss7, e->cot.call, -1);
00977 break;
00978 }
00979 p = linkset->pvts[chanpos];
00980
00981 sig_ss7_lock_private(p);
00982 if (p->loopedback) {
00983 sig_ss7_loopback(p, 0);
00984 ss7_start_call(p, linkset);
00985 }
00986 sig_ss7_unlock_private(p);
00987 break;
00988 case ISUP_EVENT_CCR:
00989 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
00990 chanpos = ss7_find_cic_gripe(linkset, e->ccr.cic, e->ccr.opc, "CCR");
00991 if (chanpos < 0) {
00992 break;
00993 }
00994
00995 p = linkset->pvts[chanpos];
00996
00997 sig_ss7_lock_private(p);
00998 sig_ss7_loopback(p, 1);
00999 sig_ss7_unlock_private(p);
01000
01001 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
01002 break;
01003 case ISUP_EVENT_CVT:
01004 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
01005 chanpos = ss7_find_cic_gripe(linkset, e->cvt.cic, e->cvt.opc, "CVT");
01006 if (chanpos < 0) {
01007 break;
01008 }
01009
01010 p = linkset->pvts[chanpos];
01011
01012 sig_ss7_lock_private(p);
01013 sig_ss7_loopback(p, 1);
01014 sig_ss7_unlock_private(p);
01015
01016 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
01017 break;
01018 case ISUP_EVENT_REL:
01019 chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
01020 if (chanpos < 0) {
01021
01022 isup_rlc(ss7, e->rel.call);
01023 break;
01024 }
01025 p = linkset->pvts[chanpos];
01026 sig_ss7_lock_private(p);
01027 sig_ss7_lock_owner(linkset, chanpos);
01028 if (p->owner) {
01029 p->owner->hangupcause = e->rel.cause;
01030 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
01031 ast_channel_unlock(p->owner);
01032 }
01033
01034
01035 sig_ss7_loopback(p, 0);
01036
01037 isup_rlc(ss7, e->rel.call);
01038 p->ss7call = NULL;
01039
01040 sig_ss7_unlock_private(p);
01041 break;
01042 case ISUP_EVENT_ACM:
01043 chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
01044 if (chanpos < 0) {
01045 isup_rel(ss7, e->acm.call, -1);
01046 break;
01047 }
01048 {
01049 p = linkset->pvts[chanpos];
01050
01051 ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
01052
01053 if (e->acm.call_ref_ident > 0) {
01054 p->rlt = 1;
01055 }
01056
01057 sig_ss7_lock_private(p);
01058 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
01059 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
01060 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
01061 }
01062 sig_ss7_set_dialing(p, 0);
01063
01064 if (e->acm.called_party_status_ind == 1) {
01065 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
01066 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
01067 }
01068 sig_ss7_lock_owner(linkset, chanpos);
01069 if (p->owner) {
01070 ast_setstate(p->owner, AST_STATE_RINGING);
01071 ast_channel_unlock(p->owner);
01072 }
01073 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
01074 }
01075 sig_ss7_unlock_private(p);
01076 }
01077 break;
01078 case ISUP_EVENT_CGB:
01079 chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
01080 if (chanpos < 0) {
01081 break;
01082 }
01083 p = linkset->pvts[chanpos];
01084 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
01085 isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
01086 break;
01087 case ISUP_EVENT_CGU:
01088 chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
01089 if (chanpos < 0) {
01090 break;
01091 }
01092 p = linkset->pvts[chanpos];
01093 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
01094 isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
01095 break;
01096 case ISUP_EVENT_UCIC:
01097 chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
01098 if (chanpos < 0) {
01099 break;
01100 }
01101 p = linkset->pvts[chanpos];
01102 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
01103 sig_ss7_lock_private(p);
01104 sig_ss7_set_remotelyblocked(p, 1);
01105 sig_ss7_set_inservice(p, 0);
01106 sig_ss7_unlock_private(p);
01107 break;
01108 case ISUP_EVENT_BLO:
01109 chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
01110 if (chanpos < 0) {
01111 break;
01112 }
01113 p = linkset->pvts[chanpos];
01114 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
01115 sig_ss7_lock_private(p);
01116 sig_ss7_set_remotelyblocked(p, 1);
01117 sig_ss7_unlock_private(p);
01118 isup_bla(linkset->ss7, e->blo.cic, p->dpc);
01119 break;
01120 case ISUP_EVENT_BLA:
01121 chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
01122 if (chanpos < 0) {
01123 break;
01124 }
01125 ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
01126 p = linkset->pvts[chanpos];
01127 sig_ss7_lock_private(p);
01128 sig_ss7_set_locallyblocked(p, 1);
01129 sig_ss7_unlock_private(p);
01130 break;
01131 case ISUP_EVENT_UBL:
01132 chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
01133 if (chanpos < 0) {
01134 break;
01135 }
01136 p = linkset->pvts[chanpos];
01137 ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
01138 sig_ss7_lock_private(p);
01139 sig_ss7_set_remotelyblocked(p, 0);
01140 sig_ss7_unlock_private(p);
01141 isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
01142 break;
01143 case ISUP_EVENT_UBA:
01144 chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
01145 if (chanpos < 0) {
01146 break;
01147 }
01148 p = linkset->pvts[chanpos];
01149 ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
01150 sig_ss7_lock_private(p);
01151 sig_ss7_set_locallyblocked(p, 0);
01152 sig_ss7_unlock_private(p);
01153 break;
01154 case ISUP_EVENT_CON:
01155 case ISUP_EVENT_ANM:
01156 if (e->e == ISUP_EVENT_CON) {
01157 chanpos = ss7_find_cic_gripe(linkset, e->con.cic, e->con.opc, "CON");
01158 if (chanpos < 0) {
01159 isup_rel(ss7, e->con.call, -1);
01160 break;
01161 }
01162 } else {
01163 chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
01164 if (chanpos < 0) {
01165 isup_rel(ss7, e->anm.call, -1);
01166 break;
01167 }
01168 }
01169
01170 {
01171 p = linkset->pvts[chanpos];
01172 sig_ss7_lock_private(p);
01173 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
01174 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
01175 }
01176 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
01177 #if 0
01178 if (p->dsp && p->dsp_features) {
01179 ast_dsp_set_features(p->dsp, p->dsp_features);
01180 p->dsp_features = 0;
01181 }
01182 #endif
01183 sig_ss7_set_echocanceller(p, 1);
01184 sig_ss7_unlock_private(p);
01185 }
01186 break;
01187 case ISUP_EVENT_RLC:
01188
01189 chanpos = ss7_find_cic_gripe(linkset, e->rlc.cic, e->rlc.opc, "RLC");
01190 if (chanpos < 0) {
01191 break;
01192 }
01193 {
01194 p = linkset->pvts[chanpos];
01195 sig_ss7_lock_private(p);
01196 if (p->alreadyhungup) {
01197 if (!p->owner) {
01198 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
01199 }
01200 p->ss7call = NULL;
01201 }
01202 sig_ss7_unlock_private(p);
01203 }
01204 break;
01205 case ISUP_EVENT_FAA:
01206
01207
01208
01209
01210 chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
01211 if (chanpos < 0) {
01212 isup_rel(linkset->ss7, e->faa.call, -1);
01213 break;
01214 }
01215 {
01216
01217 p = linkset->pvts[chanpos];
01218 ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
01219 sig_ss7_lock_private(p);
01220 if (p->alreadyhungup){
01221 if (!p->owner) {
01222 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
01223 }
01224
01225 p->ss7call = NULL;
01226 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR. Ignoring.\n");
01227 }
01228 sig_ss7_unlock_private(p);
01229 }
01230 break;
01231 default:
01232 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
01233 break;
01234 }
01235 }
01236 ast_mutex_unlock(&linkset->lock);
01237 }
01238
01239 return 0;
01240 }
01241
01242 static inline void ss7_rel(struct sig_ss7_linkset *ss7)
01243 {
01244 ast_mutex_unlock(&ss7->lock);
01245 }
01246
01247 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
01248 {
01249 int res;
01250
01251 do {
01252 res = ast_mutex_trylock(&ss7->lock);
01253 if (res) {
01254 SIG_SS7_DEADLOCK_AVOIDANCE(pvt);
01255 }
01256 } while (res);
01257
01258 if (ss7->master != AST_PTHREADT_NULL)
01259 pthread_kill(ss7->master, SIGURG);
01260 }
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
01272 {
01273 linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
01274 linkset->linkstate[which] &= ~LINKSTATE_UP;
01275 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
01276 }
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
01288 {
01289 linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
01290 linkset->linkstate[which] |= LINKSTATE_STARTING;
01291 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
01292 }
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310 int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
01311 {
01312 if (!linkset->ss7) {
01313 linkset->type = ss7type;
01314 linkset->ss7 = ss7_new(ss7type);
01315 if (!linkset->ss7) {
01316 ast_log(LOG_ERROR, "Can't create new SS7!\n");
01317 return -1;
01318 }
01319 }
01320
01321 ss7_set_network_ind(linkset->ss7, networkindicator);
01322 ss7_set_pc(linkset->ss7, pointcode);
01323
01324 if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
01325 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
01326 }
01327
01328 if (inalarm) {
01329 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
01330 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
01331 } else {
01332 linkset->linkstate[which] = LINKSTATE_DOWN;
01333 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
01334 }
01335
01336 ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
01337
01338 return 0;
01339 }
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349 static int sig_ss7_is_chan_available(struct sig_ss7_chan *pvt)
01350 {
01351 if (!pvt->inalarm && !pvt->owner && !pvt->ss7call
01352 && pvt->call_level == SIG_SS7_CALL_LEVEL_IDLE
01353 && !pvt->locallyblocked && !pvt->remotelyblocked) {
01354 return 1;
01355 }
01356 return 0;
01357 }
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367 int sig_ss7_available(struct sig_ss7_chan *p)
01368 {
01369 int available;
01370
01371 if (!p->ss7) {
01372
01373 return 0;
01374 }
01375
01376
01377 ast_mutex_lock(&p->ss7->lock);
01378 available = sig_ss7_is_chan_available(p);
01379 if (available) {
01380 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
01381 }
01382 ast_mutex_unlock(&p->ss7->lock);
01383
01384 return available;
01385 }
01386
01387 static unsigned char cid_pres2ss7pres(int cid_pres)
01388 {
01389 return (cid_pres >> 5) & 0x03;
01390 }
01391
01392 static unsigned char cid_pres2ss7screen(int cid_pres)
01393 {
01394 return cid_pres & 0x03;
01395 }
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, char *rdest)
01409 {
01410 char ss7_called_nai;
01411 int called_nai_strip;
01412 char ss7_calling_nai;
01413 int calling_nai_strip;
01414 const char *charge_str = NULL;
01415 const char *gen_address = NULL;
01416 const char *gen_digits = NULL;
01417 const char *gen_dig_type = NULL;
01418 const char *gen_dig_scheme = NULL;
01419 const char *gen_name = NULL;
01420 const char *jip_digits = NULL;
01421 const char *lspi_ident = NULL;
01422 const char *rlt_flag = NULL;
01423 const char *call_ref_id = NULL;
01424 const char *call_ref_pc = NULL;
01425 const char *send_far = NULL;
01426 char *c;
01427 char *l;
01428 char dest[256];
01429
01430 ast_copy_string(dest, rdest, sizeof(dest));
01431
01432 c = strchr(dest, '/');
01433 if (c) {
01434 c++;
01435 } else {
01436 c = "";
01437 }
01438 if (strlen(c) < p->stripmsd) {
01439 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01440 return -1;
01441 }
01442
01443 if (!p->hidecallerid) {
01444 l = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
01445 } else {
01446 l = NULL;
01447 }
01448
01449 ss7_grab(p, p->ss7);
01450
01451 if (p->call_level != SIG_SS7_CALL_LEVEL_ALLOCATED) {
01452
01453 ss7_rel(p->ss7);
01454 return -1;
01455 }
01456
01457 p->ss7call = isup_new_call(p->ss7->ss7);
01458 if (!p->ss7call) {
01459 ss7_rel(p->ss7);
01460 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
01461 return -1;
01462 }
01463
01464 called_nai_strip = 0;
01465 ss7_called_nai = p->ss7->called_nai;
01466 if (ss7_called_nai == SS7_NAI_DYNAMIC) {
01467 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
01468 called_nai_strip = strlen(p->ss7->internationalprefix);
01469 ss7_called_nai = SS7_NAI_INTERNATIONAL;
01470 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
01471 called_nai_strip = strlen(p->ss7->nationalprefix);
01472 ss7_called_nai = SS7_NAI_NATIONAL;
01473 } else {
01474 ss7_called_nai = SS7_NAI_SUBSCRIBER;
01475 }
01476 }
01477 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
01478
01479 calling_nai_strip = 0;
01480 ss7_calling_nai = p->ss7->calling_nai;
01481 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) {
01482 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
01483 calling_nai_strip = strlen(p->ss7->internationalprefix);
01484 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
01485 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
01486 calling_nai_strip = strlen(p->ss7->nationalprefix);
01487 ss7_calling_nai = SS7_NAI_NATIONAL;
01488 } else {
01489 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
01490 }
01491 }
01492 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
01493 p->use_callingpres ? cid_pres2ss7pres(ast->connected.id.number.presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
01494 p->use_callingpres ? cid_pres2ss7screen(ast->connected.id.number.presentation) : SS7_SCREENING_USER_PROVIDED);
01495
01496 isup_set_oli(p->ss7call, ast->connected.ani2);
01497 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
01498
01499
01500 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
01501 if (charge_str)
01502 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
01503
01504 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
01505 if (gen_address)
01506 isup_set_gen_address(p->ss7call, gen_address, p->gen_add_nai,p->gen_add_pres_ind, p->gen_add_num_plan,p->gen_add_type);
01507
01508 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
01509 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
01510 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
01511 if (gen_digits)
01512 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
01513
01514 gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
01515 if (gen_name)
01516 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
01517
01518 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
01519 if (jip_digits)
01520 isup_set_jip_digits(p->ss7call, jip_digits);
01521
01522 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
01523 if (lspi_ident)
01524 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
01525
01526 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
01527 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
01528 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00);
01529 }
01530
01531 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
01532 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
01533 if (call_ref_id && call_ref_pc) {
01534 isup_set_callref(p->ss7call, atoi(call_ref_id),
01535 call_ref_pc ? atoi(call_ref_pc) : 0);
01536 }
01537
01538 send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
01539 if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
01540 (isup_far(p->ss7->ss7, p->ss7call));
01541
01542 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
01543 isup_iam(p->ss7->ss7, p->ss7call);
01544 sig_ss7_set_dialing(p, 1);
01545 ast_setstate(ast, AST_STATE_DIALING);
01546 ss7_rel(p->ss7);
01547 return 0;
01548 }
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560 int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
01561 {
01562 int res = 0;
01563
01564 if (!ast->tech_pvt) {
01565 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
01566 return 0;
01567 }
01568
01569 p->owner = NULL;
01570 sig_ss7_set_dialing(p, 0);
01571 sig_ss7_set_outgoing(p, 0);
01572 p->progress = 0;
01573 p->rlt = 0;
01574 p->exten[0] = '\0';
01575
01576 ss7_grab(p, p->ss7);
01577 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
01578 if (p->ss7call) {
01579 if (!p->alreadyhungup) {
01580 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
01581 int icause = ast->hangupcause ? ast->hangupcause : -1;
01582
01583 if (cause) {
01584 if (atoi(cause)) {
01585 icause = atoi(cause);
01586 }
01587 }
01588 isup_rel(p->ss7->ss7, p->ss7call, icause);
01589 p->alreadyhungup = 1;
01590 }
01591 }
01592 ss7_rel(p->ss7);
01593
01594 return res;
01595 }
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607 int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
01608 {
01609 int res;
01610
01611 ss7_grab(p, p->ss7);
01612 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
01613 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
01614 }
01615 res = isup_anm(p->ss7->ss7, p->ss7call);
01616 ss7_rel(p->ss7);
01617 return res;
01618 }
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630 void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
01631 {
01632 if (pchan->owner == oldchan) {
01633 pchan->owner = newchan;
01634 }
01635 }
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650 int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
01651 {
01652 int res = -1;
01653
01654 switch (condition) {
01655 case AST_CONTROL_BUSY:
01656 res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
01657 break;
01658 case AST_CONTROL_RINGING:
01659 ss7_grab(p, p->ss7);
01660 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
01661 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
01662 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
01663 p->rlt = 1;
01664 }
01665
01666
01667 if (p->rlt != 1) {
01668 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
01669 }
01670 }
01671 ss7_rel(p->ss7);
01672
01673 res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
01674
01675 if (chan->_state != AST_STATE_UP && chan->_state != AST_STATE_RING) {
01676 ast_setstate(chan, AST_STATE_RINGING);
01677 }
01678 break;
01679 case AST_CONTROL_PROCEEDING:
01680 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
01681 ss7_grab(p, p->ss7);
01682
01683 if (chan->_state == AST_STATE_UP && (p->rlt != 1)){
01684 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
01685 p->rlt = 1;
01686 }
01687 }
01688
01689 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING && !p->outgoing) {
01690 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
01691 isup_acm(p->ss7->ss7, p->ss7call);
01692 }
01693 ss7_rel(p->ss7);
01694
01695 res = 0;
01696 break;
01697 case AST_CONTROL_PROGRESS:
01698 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
01699 ss7_grab(p, p->ss7);
01700 if (!p->progress && p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
01701 p->progress = 1;
01702 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
01703 ss7_rel(p->ss7);
01704
01705
01706 sig_ss7_set_echocanceller(p, 1);
01707 } else {
01708 ss7_rel(p->ss7);
01709 }
01710
01711 res = 0;
01712 break;
01713 case AST_CONTROL_INCOMPLETE:
01714
01715 if (p->call_level == SIG_SS7_CALL_LEVEL_CONNECT) {
01716 res = 0;
01717 break;
01718 }
01719 chan->hangupcause = AST_CAUSE_INVALID_NUMBER_FORMAT;
01720 break;
01721 case AST_CONTROL_CONGESTION:
01722 chan->hangupcause = AST_CAUSE_CONGESTION;
01723 break;
01724 case AST_CONTROL_HOLD:
01725 ast_moh_start(chan, data, p->mohinterpret);
01726 break;
01727 case AST_CONTROL_UNHOLD:
01728 ast_moh_stop(chan);
01729 break;
01730 case AST_CONTROL_SRCUPDATE:
01731 res = 0;
01732 break;
01733 case -1:
01734 res = sig_ss7_play_tone(p, -1);
01735 break;
01736 }
01737 return res;
01738 }
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752 struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
01753 {
01754 struct ast_channel *ast;
01755
01756 sig_ss7_set_outgoing(p, 1);
01757 ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
01758 if (!ast) {
01759 sig_ss7_set_outgoing(p, 0);
01760
01761
01762 ast_mutex_lock(&p->ss7->lock);
01763 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
01764 ast_mutex_unlock(&p->ss7->lock);
01765 }
01766 return ast;
01767 }
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777 void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
01778 {
01779 ast_free(doomed);
01780 }
01781
01782 #define SIG_SS7_SC_HEADER "%-4s %4s %-4s %-3s %-3s %-10s %-4s %s\n"
01783 #define SIG_SS7_SC_LINE "%4d %4d %-4s %-3s %-3s %-10s %-4s %s"
01784 void sig_ss7_cli_show_channels_header(int fd)
01785 {
01786 ast_cli(fd, SIG_SS7_SC_HEADER, "link", "", "Chan", "Lcl", "Rem", "Call", "SS7", "Channel");
01787 ast_cli(fd, SIG_SS7_SC_HEADER, "set", "Chan", "Idle", "Blk", "Blk", "Level", "Call", "Name");
01788 }
01789
01790 void sig_ss7_cli_show_channels(int fd, struct sig_ss7_linkset *linkset)
01791 {
01792 char line[256];
01793 int idx;
01794 struct sig_ss7_chan *pvt;
01795
01796 ast_mutex_lock(&linkset->lock);
01797 for (idx = 0; idx < linkset->numchans; ++idx) {
01798 if (!linkset->pvts[idx]) {
01799 continue;
01800 }
01801 pvt = linkset->pvts[idx];
01802 sig_ss7_lock_private(pvt);
01803 sig_ss7_lock_owner(linkset, idx);
01804
01805 snprintf(line, sizeof(line), SIG_SS7_SC_LINE,
01806 linkset->span,
01807 pvt->channel,
01808 sig_ss7_is_chan_available(pvt) ? "Yes" : "No",
01809 pvt->locallyblocked ? "Yes" : "No",
01810 pvt->remotelyblocked ? "Yes" : "No",
01811 sig_ss7_call_level2str(pvt->call_level),
01812 pvt->ss7call ? "Yes" : "No",
01813 pvt->owner ? pvt->owner->name : "");
01814
01815 if (pvt->owner) {
01816 ast_channel_unlock(pvt->owner);
01817 }
01818 sig_ss7_unlock_private(pvt);
01819
01820 ast_mutex_unlock(&linkset->lock);
01821 ast_cli(fd, "%s\n", line);
01822 ast_mutex_lock(&linkset->lock);
01823 }
01824 ast_mutex_unlock(&linkset->lock);
01825 }
01826
01827
01828
01829
01830
01831
01832
01833
01834
01835
01836
01837
01838 struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_callback *callback, struct sig_ss7_linkset *ss7)
01839 {
01840 struct sig_ss7_chan *pvt;
01841
01842 pvt = ast_calloc(1, sizeof(*pvt));
01843 if (!pvt) {
01844 return pvt;
01845 }
01846
01847 pvt->calls = callback;
01848 pvt->chan_pvt = pvt_data;
01849 pvt->ss7 = ss7;
01850
01851 return pvt;
01852 }
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862 void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
01863 {
01864 int idx;
01865
01866 memset(ss7, 0, sizeof(*ss7));
01867
01868 ast_mutex_init(&ss7->lock);
01869
01870 ss7->master = AST_PTHREADT_NULL;
01871 for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
01872 ss7->fds[idx] = -1;
01873 }
01874 }
01875
01876
01877
01878 #endif
01879