Extract sig_ss7_init_linkset() to sig_ss7.
[asterisk/asterisk.git] / channels / sig_ss7.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010 Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \brief SS7 signaling module.
22  *
23  * \author Matthew Fredrickson <creslin@digium.com>
24  * \author Richard Mudgett <rmudgett@digium.com>
25  *
26  * See Also:
27  * \arg \ref AstCREDITS
28  */
29
30
31 #include "asterisk.h"
32
33 #if defined(HAVE_SS7)
34
35 #include <signal.h>
36
37 #include "asterisk/pbx.h"
38 #include "asterisk/causes.h"
39 #include "asterisk/musiconhold.h"
40 #include "asterisk/transcap.h"
41
42 #include "sig_ss7.h"
43
44 /* ------------------------------------------------------------------- */
45
46 #define SIG_SS7_DEADLOCK_AVOIDANCE(p) \
47         do { \
48                 sig_ss7_unlock_private(p); \
49                 usleep(1); \
50                 sig_ss7_lock_private(p); \
51         } while (0)
52
53 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
54 {
55         if (p->calls->unlock_private) {
56                 p->calls->unlock_private(p->chan_pvt);
57         }
58 }
59
60 static void sig_ss7_lock_private(struct sig_ss7_chan *p)
61 {
62         if (p->calls->lock_private) {
63                 p->calls->lock_private(p->chan_pvt);
64         }
65 }
66
67 void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
68 {
69         p->inalarm = in_alarm;
70         if (p->calls->set_alarm) {
71                 p->calls->set_alarm(p->chan_pvt, in_alarm);
72         }
73 }
74
75 static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
76 {
77         if (p->calls->set_dialing) {
78                 p->calls->set_dialing(p->chan_pvt, is_dialing);
79         }
80 }
81
82 static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
83 {
84         if (p->calls->set_digital) {
85                 p->calls->set_digital(p->chan_pvt, is_digital);
86         }
87 }
88
89 static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
90 {
91         if (p->calls->set_inservice) {
92                 p->calls->set_inservice(p->chan_pvt, is_inservice);
93         }
94 }
95
96 static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
97 {
98         p->locallyblocked = is_blocked;
99         if (p->calls->set_locallyblocked) {
100                 p->calls->set_locallyblocked(p->chan_pvt, is_blocked);
101         }
102 }
103
104 static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
105 {
106         p->remotelyblocked = is_blocked;
107         if (p->calls->set_remotelyblocked) {
108                 p->calls->set_remotelyblocked(p->chan_pvt, is_blocked);
109         }
110 }
111
112 /*!
113  * \internal
114  * \brief Set the caller id information in the parent module.
115  * \since 1.8
116  *
117  * \param p sig_ss7 channel structure.
118  *
119  * \return Nothing
120  */
121 static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
122 {
123         struct ast_party_caller caller;
124
125         if (p->calls->set_callerid) {
126                 ast_party_caller_init(&caller);
127                 caller.id.number = p->cid_num;
128                 caller.id.name = p->cid_name;
129                 if (!ast_strlen_zero(p->cid_subaddr)) {
130                         caller.id.subaddress.valid = 1;
131                         //caller.id.subaddress.type = 0;/* nsap */
132                         //caller.id.subaddress.odd_even_indicator = 0;
133                         caller.id.subaddress.str = p->cid_subaddr;
134                 }
135                 caller.id.number_type = p->cid_ton;
136                 caller.id.number_presentation = p->callingpres;
137                 caller.ani = p->cid_ani;
138                 caller.ani2 = p->cid_ani2;
139                 p->calls->set_callerid(p->chan_pvt, &caller);
140         }
141 }
142
143 /*!
144  * \internal
145  * \brief Set the Dialed Number Identifier.
146  * \since 1.8
147  *
148  * \param p sig_ss7 channel structure.
149  * \param dnid Dialed Number Identifier string.
150  *
151  * \return Nothing
152  */
153 static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
154 {
155         if (p->calls->set_dnid) {
156                 p->calls->set_dnid(p->chan_pvt, dnid);
157         }
158 }
159
160 static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
161 {
162         int res;
163
164         if (p->calls->play_tone) {
165                 res = p->calls->play_tone(p->chan_pvt, tone);
166         } else {
167                 res = -1;
168         }
169         return res;
170 }
171
172 static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
173 {
174         if (p->calls->set_echocanceller) {
175                 return p->calls->set_echocanceller(p->chan_pvt, enable);
176         }
177         return -1;
178 }
179
180 static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
181 {
182         if (p->loopedback != enable) {
183                 p->loopedback = enable;
184                 if (p->calls->set_loopback) {
185                         p->calls->set_loopback(p->chan_pvt, enable);
186                 }
187         }
188 }
189
190 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)
191 {
192         struct ast_channel *ast;
193
194         if (p->calls->new_ast_channel) {
195                 ast = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
196         } else {
197                 return NULL;
198         }
199
200         if (!p->owner) {
201                 p->owner = ast;
202         }
203         p->alreadyhungup = 0;
204         ast->transfercapability = transfercapability;
205         pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
206                 ast_transfercapability2str(transfercapability));
207         if (transfercapability & AST_TRANS_CAP_DIGITAL) {
208                 sig_ss7_set_digital(p, 1);
209         }
210
211         return ast;
212 }
213
214 static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
215 {
216         if (linkset->calls->handle_link_exception) {
217                 linkset->calls->handle_link_exception(linkset, which);
218         }
219 }
220
221 /*!
222  * \internal
223  * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
224  * \since 1.8
225  *
226  * \param ss7 sig_ss7 SS7 control structure.
227  * \param chanpos Channel position in the span.
228  *
229  * \note Assumes the ss7->lock is already obtained.
230  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
231  *
232  * \return Nothing
233  */
234 static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
235 {
236         for (;;) {
237                 if (!ss7->pvts[chanpos]->owner) {
238                         /* There is no owner lock to get. */
239                         break;
240                 }
241                 if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
242                         /* We got the lock */
243                         break;
244                 }
245                 /* We must unlock the SS7 to avoid the possibility of a deadlock */
246                 ast_mutex_unlock(&ss7->lock);
247                 SIG_SS7_DEADLOCK_AVOIDANCE(ss7->pvts[chanpos]);
248                 ast_mutex_lock(&ss7->lock);
249         }
250 }
251
252 /*!
253  * \internal
254  * \brief Queue the given frame onto the owner channel.
255  * \since 1.8
256  *
257  * \param ss7 sig_ss7 SS7 control structure.
258  * \param chanpos Channel position in the span.
259  * \param frame Frame to queue onto the owner channel.
260  *
261  * \note Assumes the ss7->lock is already obtained.
262  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
263  *
264  * \return Nothing
265  */
266 static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
267 {
268         sig_ss7_lock_owner(ss7, chanpos);
269         if (ss7->pvts[chanpos]->owner) {
270                 ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
271                 ast_channel_unlock(ss7->pvts[chanpos]->owner);
272         }
273 }
274
275 /*!
276  * \internal
277  * \brief Queue a control frame of the specified subclass onto the owner channel.
278  * \since 1.8
279  *
280  * \param ss7 sig_ss7 SS7 control structure.
281  * \param chanpos Channel position in the span.
282  * \param subclass Control frame subclass to queue onto the owner channel.
283  *
284  * \note Assumes the ss7->lock is already obtained.
285  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
286  *
287  * \return Nothing
288  */
289 static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
290 {
291         struct ast_frame f = {AST_FRAME_CONTROL, };
292         struct sig_ss7_chan *p = ss7->pvts[chanpos];
293
294         if (p->calls->queue_control) {
295                 p->calls->queue_control(p->chan_pvt, subclass);
296         }
297
298         f.subclass.integer = subclass;
299         sig_ss7_queue_frame(ss7, chanpos, &f);
300 }
301
302 static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
303 {
304         int i;
305         int winner = -1;
306         for (i = 0; i < linkset->numchans; i++) {
307                 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
308                         winner = i;
309                         break;
310                 }
311         }
312         return winner;
313 }
314
315 static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
316 {
317         unsigned char status[32];
318         struct sig_ss7_chan *p = NULL;
319         int i, offset;
320
321         for (i = 0; i < linkset->numchans; i++) {
322                 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
323                         p = linkset->pvts[i];
324                         offset = p->cic - startcic;
325                         status[offset] = 0;
326                         if (p->locallyblocked)
327                                 status[offset] |= (1 << 0) | (1 << 4);
328                         if (p->remotelyblocked)
329                                 status[offset] |= (1 << 1) | (1 << 5);
330                         if (p->ss7call) {
331                                 if (p->outgoing)
332                                         status[offset] |= (1 << 3);
333                                 else
334                                         status[offset] |= (1 << 2);
335                         } else
336                                 status[offset] |= 0x3 << 2;
337                 }
338         }
339
340         if (p)
341                 isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
342         else
343                 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
344
345 }
346
347 static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
348 {
349         int i;
350
351         for (i = 0; i < linkset->numchans; i++) {
352                 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
353                         sig_ss7_lock_private(linkset->pvts[i]);
354                         if (linkset->pvts[i]->owner)
355                                 linkset->pvts[i]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
356                         sig_ss7_unlock_private(linkset->pvts[i]);
357                 }
358         }
359 }
360
361 static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
362 {
363         int i;
364
365         for (i = 0; i < linkset->numchans; i++) {
366                 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
367                         if (state) {
368                                 if (state[i])
369                                         sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
370                         } else
371                                 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
372                 }
373         }
374 }
375
376 static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
377 {
378         int i;
379
380         for (i = 0; i < linkset->numchans; i++) {
381                 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
382                         sig_ss7_set_inservice(linkset->pvts[i], 1);
383         }
384 }
385
386 static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
387 {
388         int i, startcic = -1, endcic, dpc;
389
390         if (linkset->numchans <= 0)
391                 return;
392
393         startcic = linkset->pvts[0]->cic;
394         /* DB: CIC's DPC fix */
395         dpc = linkset->pvts[0]->dpc;
396
397         for (i = 0; i < linkset->numchans; i++) {
398                 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)) {
399                         continue;
400                 } else {
401                         endcic = linkset->pvts[i]->cic;
402                         ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
403                         isup_grs(linkset->ss7, startcic, endcic, dpc);
404
405                         /* DB: CIC's DPC fix */
406                         if (linkset->pvts[i+1]) {
407                                 startcic = linkset->pvts[i+1]->cic;
408                                 dpc = linkset->pvts[i+1]->dpc;
409                         }
410                 }
411         }
412 }
413
414 /* This function is assumed to be called with the private channel lock and linkset lock held */
415 static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
416 {
417         struct ss7 *ss7 = linkset->ss7;
418         int law;
419         struct ast_channel *c;
420         char tmp[256];
421
422         if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
423                 p->proceeding = 1;
424                 isup_acm(ss7, p->ss7call);
425         }
426
427         if (linkset->type == SS7_ITU) {
428                 law = SIG_SS7_ALAW;
429         } else {
430                 law = SIG_SS7_ULAW;
431         }
432
433         /*
434          * Release the SS7 lock while we create the channel
435          * so other threads can send messages.
436          */
437         ast_mutex_unlock(&linkset->lock);
438         c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL);
439         if (!c) {
440                 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
441                 ast_mutex_lock(&linkset->lock);
442                 isup_rel(linkset->ss7, p->ss7call, -1);
443                 p->proceeding = 0;
444                 p->alreadyhungup = 1;
445                 return;
446         }
447
448         sig_ss7_set_echocanceller(p, 1);
449
450         /*
451          * It is reasonably safe to set the following
452          * channel variables while the channel private
453          * structure is locked.  The PBX has not been
454          * started yet and it is unlikely that any other task
455          * will do anything with the channel we have just
456          * created.
457          *
458          * We only reference these variables in the context of the ss7_linkset function
459          * when receiving either and IAM or a COT message.
460          */
461         if (!ast_strlen_zero(p->charge_number)) {
462                 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
463                 /* Clear this after we set it */
464                 p->charge_number[0] = 0;
465         }
466         if (!ast_strlen_zero(p->gen_add_number)) {
467                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
468                 /* Clear this after we set it */
469                 p->gen_add_number[0] = 0;
470         }
471         if (!ast_strlen_zero(p->jip_number)) {
472                 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
473                 /* Clear this after we set it */
474                 p->jip_number[0] = 0;
475         }
476         if (!ast_strlen_zero(p->gen_dig_number)) {
477                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
478                 /* Clear this after we set it */
479                 p->gen_dig_number[0] = 0;
480         }
481         if (!ast_strlen_zero(p->orig_called_num)) {
482                 pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
483                 /* Clear this after we set it */
484                 p->orig_called_num[0] = 0;
485         }
486
487         snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
488         pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
489         /* Clear this after we set it */
490         p->gen_dig_type = 0;
491
492         snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
493         pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
494         /* Clear this after we set it */
495         p->gen_dig_scheme = 0;
496
497         if (!ast_strlen_zero(p->lspi_ident)) {
498                 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
499                 /* Clear this after we set it */
500                 p->lspi_ident[0] = 0;
501         }
502
503         snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
504         pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
505         /* Clear this after we set it */
506         p->call_ref_ident = 0;
507
508         snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
509         pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
510         /* Clear this after we set it */
511         p->call_ref_pc = 0;
512
513         snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
514         pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
515         /* Clear this after we set it */
516         p->calling_party_cat = 0;
517
518         if (!ast_strlen_zero(p->redirecting_num)) {
519                 pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
520                 /* Clear this after we set it */
521                 p->redirecting_num[0] = 0;
522         }
523         if (!ast_strlen_zero(p->generic_name)) {
524                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
525                 /* Clear this after we set it */
526                 p->generic_name[0] = 0;
527         }
528
529         if (ast_pbx_start(c)) {
530                 ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", c->name, p->cic);
531                 ast_hangup(c);
532         } else {
533                 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
534         }
535         ast_mutex_lock(&linkset->lock);
536 }
537
538 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
539 {
540         if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
541                 if (size) {
542                         *buf = '\0';
543                 }
544                 return;
545         }
546         switch (nai) {
547         case SS7_NAI_INTERNATIONAL:
548                 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
549                 break;
550         case SS7_NAI_NATIONAL:
551                 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
552                 break;
553         case SS7_NAI_SUBSCRIBER:
554                 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
555                 break;
556         case SS7_NAI_UNKNOWN:
557                 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
558                 break;
559         default:
560                 snprintf(buf, size, "%s", number);
561                 break;
562         }
563 }
564
565 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
566 {
567         return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
568 }
569
570 /* This is a thread per linkset that handles all received events from libss7. */
571 void *ss7_linkset(void *data)
572 {
573         int res, i;
574         struct timeval *next = NULL, tv;
575         struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
576         struct ss7 *ss7 = linkset->ss7;
577         ss7_event *e = NULL;
578         struct sig_ss7_chan *p;
579         int chanpos;
580         struct pollfd pollers[SIG_SS7_NUM_DCHANS];
581         int cic;
582         unsigned int dpc;
583         int nextms = 0;
584
585         ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
586         ss7_start(ss7);
587
588         for (;;) {
589                 ast_mutex_lock(&linkset->lock);
590                 if ((next = ss7_schedule_next(ss7))) {
591                         tv = ast_tvnow();
592                         tv.tv_sec = next->tv_sec - tv.tv_sec;
593                         tv.tv_usec = next->tv_usec - tv.tv_usec;
594                         if (tv.tv_usec < 0) {
595                                 tv.tv_usec += 1000000;
596                                 tv.tv_sec -= 1;
597                         }
598                         if (tv.tv_sec < 0) {
599                                 tv.tv_sec = 0;
600                                 tv.tv_usec = 0;
601                         }
602                         nextms = tv.tv_sec * 1000;
603                         nextms += tv.tv_usec / 1000;
604                 }
605                 ast_mutex_unlock(&linkset->lock);
606
607                 for (i = 0; i < linkset->numsigchans; i++) {
608                         pollers[i].fd = linkset->fds[i];
609                         pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
610                         pollers[i].revents = 0;
611                 }
612
613                 res = poll(pollers, linkset->numsigchans, nextms);
614                 if ((res < 0) && (errno != EINTR)) {
615                         ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
616                 } else if (!res) {
617                         ast_mutex_lock(&linkset->lock);
618                         ss7_schedule_run(ss7);
619                         ast_mutex_unlock(&linkset->lock);
620                         continue;
621                 }
622
623                 ast_mutex_lock(&linkset->lock);
624                 for (i = 0; i < linkset->numsigchans; i++) {
625                         if (pollers[i].revents & POLLPRI) {
626                                 sig_ss7_handle_link_exception(linkset, i);
627                         }
628                         if (pollers[i].revents & POLLIN) {
629                                 res = ss7_read(ss7, pollers[i].fd);
630                         }
631                         if (pollers[i].revents & POLLOUT) {
632                                 res = ss7_write(ss7, pollers[i].fd);
633                                 if (res < 0) {
634                                         ast_debug(1, "Error in write %s\n", strerror(errno));
635                                 }
636                         }
637                 }
638
639                 while ((e = ss7_check_event(ss7))) {
640                         switch (e->e) {
641                         case SS7_EVENT_UP:
642                                 if (linkset->state != LINKSET_STATE_UP) {
643                                         ast_verbose("--- SS7 Up ---\n");
644                                         ss7_reset_linkset(linkset);
645                                 }
646                                 linkset->state = LINKSET_STATE_UP;
647                                 break;
648                         case SS7_EVENT_DOWN:
649                                 ast_verbose("--- SS7 Down ---\n");
650                                 linkset->state = LINKSET_STATE_DOWN;
651                                 for (i = 0; i < linkset->numchans; i++) {
652                                         p = linkset->pvts[i];
653                                         if (p) {
654                                                 sig_ss7_set_alarm(p, 1);
655                                         }
656                                 }
657                                 break;
658                         case MTP2_LINK_UP:
659                                 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
660                                 break;
661                         case MTP2_LINK_DOWN:
662                                 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
663                                 break;
664                         case ISUP_EVENT_CPG:
665                                 chanpos = ss7_find_cic(linkset, e->cpg.cic, e->cpg.opc);
666                                 if (chanpos < 0) {
667                                         ast_log(LOG_WARNING, "CPG on unconfigured CIC %d\n", e->cpg.cic);
668                                         break;
669                                 }
670                                 p = linkset->pvts[chanpos];
671                                 sig_ss7_lock_private(p);
672                                 switch (e->cpg.event) {
673                                 case CPG_EVENT_ALERTING:
674                                         p->alerting = 1;
675                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
676                                         break;
677                                 case CPG_EVENT_PROGRESS:
678                                 case CPG_EVENT_INBANDINFO:
679                                         {
680                                                 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
681                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
682                                                 p->progress = 1;
683                                                 sig_ss7_set_dialing(p, 0);
684 #if 0   /* This code no longer seems to be necessary so I did not convert it. */
685                                                 if (p->dsp && p->dsp_features) {
686                                                         ast_dsp_set_features(p->dsp, p->dsp_features);
687                                                         p->dsp_features = 0;
688                                                 }
689 #endif
690                                         }
691                                         break;
692                                 default:
693                                         ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
694                                         break;
695                                 }
696
697                                 sig_ss7_unlock_private(p);
698                                 break;
699                         case ISUP_EVENT_RSC:
700                                 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
701                                 chanpos = ss7_find_cic(linkset, e->rsc.cic, e->rsc.opc);
702                                 if (chanpos < 0) {
703                                         ast_log(LOG_WARNING, "RSC on unconfigured CIC %d\n", e->rsc.cic);
704                                         break;
705                                 }
706                                 p = linkset->pvts[chanpos];
707                                 sig_ss7_lock_private(p);
708                                 sig_ss7_set_inservice(p, 1);
709                                 sig_ss7_set_remotelyblocked(p, 0);
710                                 dpc = p->dpc;
711                                 isup_set_call_dpc(e->rsc.call, dpc);
712                                 p->ss7call = NULL;
713                                 if (p->owner)
714                                         p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
715                                 sig_ss7_unlock_private(p);
716                                 isup_rlc(ss7, e->rsc.call);
717                                 break;
718                         case ISUP_EVENT_GRS:
719                                 ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
720                                 chanpos = ss7_find_cic(linkset, e->grs.startcic, e->grs.opc);
721                                 if (chanpos < 0) {
722                                         ast_log(LOG_WARNING, "GRS on unconfigured CIC %d\n", e->grs.startcic);
723                                         break;
724                                 }
725                                 p = linkset->pvts[chanpos];
726                                 isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
727                                 ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
728                                 ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
729                                 break;
730                         case ISUP_EVENT_CQM:
731                                 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
732                                 ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
733                                 break;
734                         case ISUP_EVENT_GRA:
735                                 ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
736                                 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
737                                 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
738                                 break;
739                         case ISUP_EVENT_IAM:
740                                 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);
741                                 chanpos = ss7_find_cic(linkset, e->iam.cic, e->iam.opc);
742                                 if (chanpos < 0) {
743                                         ast_log(LOG_WARNING, "IAM on unconfigured CIC %d\n", e->iam.cic);
744                                         isup_rel(ss7, e->iam.call, -1);
745                                         break;
746                                 }
747                                 p = linkset->pvts[chanpos];
748                                 sig_ss7_lock_private(p);
749                                 if (p->owner) {
750                                         if (p->ss7call == e->iam.call) {
751                                                 sig_ss7_unlock_private(p);
752                                                 ast_log(LOG_WARNING, "Duplicate IAM requested on CIC %d\n", e->iam.cic);
753                                                 break;
754                                         } else {
755                                                 sig_ss7_unlock_private(p);
756                                                 ast_log(LOG_WARNING, "Ring requested on CIC %d already in use!\n", e->iam.cic);
757                                                 break;
758                                         }
759                                 }
760
761                                 dpc = p->dpc;
762                                 p->ss7call = e->iam.call;
763                                 isup_set_call_dpc(p->ss7call, dpc);
764
765                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
766                                         ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
767                                         p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
768                                 } else
769                                         p->cid_num[0] = 0;
770
771                                 /* Set DNID */
772                                 if (!ast_strlen_zero(e->iam.called_party_num)) {
773                                         ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
774                                                 e->iam.called_party_num, e->iam.called_nai);
775                                         sig_ss7_set_dnid(p, p->exten);
776                                 }
777
778                                 if (p->immediate) {
779                                         p->exten[0] = 's';
780                                         p->exten[1] = '\0';
781                                 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
782                                         char *st;
783                                         ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
784                                         st = strchr(p->exten, '#');
785                                         if (st) {
786                                                 *st = '\0';
787                                         }
788                                 } else {
789                                         p->exten[0] = '\0';
790                                 }
791
792                                 p->cid_ani[0] = '\0';
793                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
794                                         ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
795                                 else
796                                         p->cid_name[0] = '\0';
797
798                                 p->cid_ani2 = e->iam.oli_ani2;
799                                 p->cid_ton = 0;
800                                 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
801                                 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
802                                 p->gen_add_type = e->iam.gen_add_type;
803                                 p->gen_add_nai = e->iam.gen_add_nai;
804                                 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
805                                 p->gen_add_num_plan = e->iam.gen_add_num_plan;
806                                 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
807                                 p->gen_dig_type = e->iam.gen_dig_type;
808                                 p->gen_dig_scheme = e->iam.gen_dig_scheme;
809                                 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
810                                 ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
811                                 ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
812                                 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
813                                 p->calling_party_cat = e->iam.calling_party_cat;
814
815                                 sig_ss7_set_caller_id(p);
816
817                                 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
818                                         if (e->iam.cot_check_required) {
819                                                 sig_ss7_loopback(p, 1);
820                                         } else
821                                                 ss7_start_call(p, linkset);
822                                 } else {
823                                         ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
824                                         p->alreadyhungup = 1;
825                                         isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
826                                 }
827                                 sig_ss7_unlock_private(p);
828                                 break;
829                         case ISUP_EVENT_COT:
830                                 chanpos = ss7_find_cic(linkset, e->cot.cic, e->cot.opc);
831                                 if (chanpos < 0) {
832                                         ast_log(LOG_WARNING, "COT on unconfigured CIC %d\n", e->cot.cic);
833                                         isup_rel(ss7, e->cot.call, -1);
834                                         break;
835                                 }
836                                 p = linkset->pvts[chanpos];
837
838                                 sig_ss7_lock_private(p);
839                                 if (p->loopedback) {
840                                         sig_ss7_loopback(p, 0);
841                                         ss7_start_call(p, linkset);
842                                 }
843                                 sig_ss7_unlock_private(p);
844                                 break;
845                         case ISUP_EVENT_CCR:
846                                 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
847                                 chanpos = ss7_find_cic(linkset, e->ccr.cic, e->ccr.opc);
848                                 if (chanpos < 0) {
849                                         ast_log(LOG_WARNING, "CCR on unconfigured CIC %d\n", e->ccr.cic);
850                                         break;
851                                 }
852
853                                 p = linkset->pvts[chanpos];
854
855                                 sig_ss7_lock_private(p);
856                                 sig_ss7_loopback(p, 1);
857                                 sig_ss7_unlock_private(p);
858
859                                 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
860                                 break;
861                         case ISUP_EVENT_CVT:
862                                 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
863                                 chanpos = ss7_find_cic(linkset, e->cvt.cic, e->cvt.opc);
864                                 if (chanpos < 0) {
865                                         ast_log(LOG_WARNING, "CVT on unconfigured CIC %d\n", e->cvt.cic);
866                                         break;
867                                 }
868
869                                 p = linkset->pvts[chanpos];
870
871                                 sig_ss7_lock_private(p);
872                                 sig_ss7_loopback(p, 1);
873                                 sig_ss7_unlock_private(p);
874
875                                 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
876                                 break;
877                         case ISUP_EVENT_REL:
878                                 chanpos = ss7_find_cic(linkset, e->rel.cic, e->rel.opc);
879                                 if (chanpos < 0) {
880                                         ast_log(LOG_WARNING, "REL on unconfigured CIC %d\n", e->rel.cic);
881                                         break;
882                                 }
883                                 p = linkset->pvts[chanpos];
884                                 sig_ss7_lock_private(p);
885                                 if (p->owner) {
886                                         p->owner->hangupcause = e->rel.cause;
887                                         p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
888                                 } else {
889                                         ast_log(LOG_WARNING, "REL on channel (CIC %d) without owner!\n", p->cic);
890                                 }
891
892                                 /* End the loopback if we have one */
893                                 sig_ss7_loopback(p, 0);
894
895                                 isup_rlc(ss7, e->rel.call);
896                                 p->ss7call = NULL;
897
898                                 sig_ss7_unlock_private(p);
899                                 break;
900                         case ISUP_EVENT_ACM:
901                                 chanpos = ss7_find_cic(linkset, e->acm.cic, e->acm.opc);
902                                 if (chanpos < 0) {
903                                         ast_log(LOG_WARNING, "ACM on unconfigured CIC %d\n", e->acm.cic);
904                                         isup_rel(ss7, e->acm.call, -1);
905                                         break;
906                                 } else {
907                                         p = linkset->pvts[chanpos];
908
909                                         ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
910
911                                         if (e->acm.call_ref_ident > 0) {
912                                                 p->rlt = 1; /* Setting it but not using it here*/
913                                         }
914
915                                         sig_ss7_lock_private(p);
916                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
917                                         p->proceeding = 1;
918                                         sig_ss7_set_dialing(p, 0);
919                                         /* Send alerting if subscriber is free */
920                                         if (e->acm.called_party_status_ind == 1) {
921                                                 p->alerting = 1;
922                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
923                                         }
924                                         sig_ss7_unlock_private(p);
925                                 }
926                                 break;
927                         case ISUP_EVENT_CGB:
928                                 chanpos = ss7_find_cic(linkset, e->cgb.startcic, e->cgb.opc);
929                                 if (chanpos < 0) {
930                                         ast_log(LOG_WARNING, "CGB on unconfigured CIC %d\n", e->cgb.startcic);
931                                         break;
932                                 }
933                                 p = linkset->pvts[chanpos];
934                                 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
935                                 isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
936                                 break;
937                         case ISUP_EVENT_CGU:
938                                 chanpos = ss7_find_cic(linkset, e->cgu.startcic, e->cgu.opc);
939                                 if (chanpos < 0) {
940                                         ast_log(LOG_WARNING, "CGU on unconfigured CIC %d\n", e->cgu.startcic);
941                                         break;
942                                 }
943                                 p = linkset->pvts[chanpos];
944                                 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
945                                 isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
946                                 break;
947                         case ISUP_EVENT_UCIC:
948                                 chanpos = ss7_find_cic(linkset, e->ucic.cic, e->ucic.opc);
949                                 if (chanpos < 0) {
950                                         ast_log(LOG_WARNING, "UCIC on unconfigured CIC %d\n", e->ucic.cic);
951                                         break;
952                                 }
953                                 p = linkset->pvts[chanpos];
954                                 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
955                                 sig_ss7_lock_private(p);
956                                 sig_ss7_set_remotelyblocked(p, 1);
957                                 sig_ss7_set_inservice(p, 0);
958                                 sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
959                                 break;
960                         case ISUP_EVENT_BLO:
961                                 chanpos = ss7_find_cic(linkset, e->blo.cic, e->blo.opc);
962                                 if (chanpos < 0) {
963                                         ast_log(LOG_WARNING, "BLO on unconfigured CIC %d\n", e->blo.cic);
964                                         break;
965                                 }
966                                 p = linkset->pvts[chanpos];
967                                 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
968                                 sig_ss7_lock_private(p);
969                                 sig_ss7_set_remotelyblocked(p, 1);
970                                 sig_ss7_unlock_private(p);
971                                 isup_bla(linkset->ss7, e->blo.cic, p->dpc);
972                                 break;
973                         case ISUP_EVENT_BLA:
974                                 chanpos = ss7_find_cic(linkset, e->bla.cic, e->bla.opc);
975                                 if (chanpos < 0) {
976                                         ast_log(LOG_WARNING, "BLA on unconfigured CIC %d\n", e->bla.cic);
977                                         break;
978                                 }
979                                 ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
980                                 p = linkset->pvts[chanpos];
981                                 sig_ss7_lock_private(p);
982                                 sig_ss7_set_locallyblocked(p, 1);
983                                 sig_ss7_unlock_private(p);
984                                 break;
985                         case ISUP_EVENT_UBL:
986                                 chanpos = ss7_find_cic(linkset, e->ubl.cic, e->ubl.opc);
987                                 if (chanpos < 0) {
988                                         ast_log(LOG_WARNING, "UBL on unconfigured CIC %d\n", e->ubl.cic);
989                                         break;
990                                 }
991                                 p = linkset->pvts[chanpos];
992                                 ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
993                                 sig_ss7_lock_private(p);
994                                 sig_ss7_set_remotelyblocked(p, 0);
995                                 sig_ss7_unlock_private(p);
996                                 isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
997                                 break;
998                         case ISUP_EVENT_UBA:
999                                 chanpos = ss7_find_cic(linkset, e->uba.cic, e->uba.opc);
1000                                 if (chanpos < 0) {
1001                                         ast_log(LOG_WARNING, "UBA on unconfigured CIC %d\n", e->uba.cic);
1002                                         break;
1003                                 }
1004                                 p = linkset->pvts[chanpos];
1005                                 ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
1006                                 sig_ss7_lock_private(p);
1007                                 sig_ss7_set_locallyblocked(p, 0);
1008                                 sig_ss7_unlock_private(p);
1009                                 break;
1010                         case ISUP_EVENT_CON:
1011                         case ISUP_EVENT_ANM:
1012                                 if (e->e == ISUP_EVENT_CON)
1013                                         cic = e->con.cic;
1014                                 else
1015                                         cic = e->anm.cic;
1016
1017                                 chanpos = ss7_find_cic(linkset, cic, (e->e == ISUP_EVENT_ANM) ? e->anm.opc : e->con.opc);
1018                                 if (chanpos < 0) {
1019                                         ast_log(LOG_WARNING, "ANM/CON on unconfigured CIC %d\n", cic);
1020                                         isup_rel(ss7, (e->e == ISUP_EVENT_ANM) ? e->anm.call : e->con.call, -1);
1021                                         break;
1022                                 } else {
1023                                         p = linkset->pvts[chanpos];
1024                                         sig_ss7_lock_private(p);
1025                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
1026 #if 0   /* This code no longer seems to be necessary so I did not convert it. */
1027                                         if (p->dsp && p->dsp_features) {
1028                                                 ast_dsp_set_features(p->dsp, p->dsp_features);
1029                                                 p->dsp_features = 0;
1030                                         }
1031 #endif
1032                                         sig_ss7_set_echocanceller(p, 1);
1033                                         sig_ss7_unlock_private(p);
1034                                 }
1035                                 break;
1036                         case ISUP_EVENT_RLC:
1037                                 chanpos = ss7_find_cic(linkset, e->rlc.cic, e->rlc.opc);
1038                                 if (chanpos < 0) {
1039                                         ast_log(LOG_WARNING, "RLC on unconfigured CIC %d\n", e->rlc.cic);
1040                                         break;
1041                                 } else {
1042                                         p = linkset->pvts[chanpos];
1043                                         sig_ss7_lock_private(p);
1044                                         if (p->alreadyhungup)
1045                                                 p->ss7call = NULL;
1046                                         else
1047                                                 ast_log(LOG_NOTICE, "Received RLC out and we haven't sent REL.  Ignoring.\n");
1048                                         sig_ss7_unlock_private(p);
1049                                         }
1050                                         break;
1051                         case ISUP_EVENT_FAA:
1052                                 chanpos = ss7_find_cic(linkset, e->faa.cic, e->faa.opc);
1053                                 if (chanpos < 0) {
1054                                         ast_log(LOG_WARNING, "FAA on unconfigured CIC %d\n", e->faa.cic);
1055                                         break;
1056                                 } else {
1057                                         p = linkset->pvts[chanpos];
1058                                         ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
1059                                         sig_ss7_lock_private(p);
1060                                         if (p->alreadyhungup){
1061                                                 p->ss7call = NULL;
1062                                                 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR.  Ignoring.\n");
1063                                         }
1064                                         sig_ss7_unlock_private(p);
1065                                 }
1066                                 break;
1067                         default:
1068                                 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
1069                                 break;
1070                         }
1071                 }
1072                 ast_mutex_unlock(&linkset->lock);
1073         }
1074
1075         return 0;
1076 }
1077
1078 static inline void ss7_rel(struct sig_ss7_linkset *ss7)
1079 {
1080         ast_mutex_unlock(&ss7->lock);
1081 }
1082
1083 static inline int ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
1084 {
1085         int res;
1086         /* Grab the lock first */
1087         do {
1088                 res = ast_mutex_trylock(&ss7->lock);
1089                 if (res) {
1090                         SIG_SS7_DEADLOCK_AVOIDANCE(pvt);
1091                 }
1092         } while (res);
1093         /* Then break the poll */
1094         if (ss7->master != AST_PTHREADT_NULL)
1095                 pthread_kill(ss7->master, SIGURG);
1096         return 0;
1097 }
1098
1099 /*!
1100  * \brief Notify the SS7 layer that the link is in alarm.
1101  * \since 1.8
1102  *
1103  * \param linkset Controlling linkset for the channel.
1104  * \param which Link index of the signaling channel.
1105  *
1106  * \return Nothing
1107  */
1108 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
1109 {
1110         linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
1111         linkset->linkstate[which] &= ~LINKSTATE_UP;
1112         ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1113 }
1114
1115 /*!
1116  * \brief Notify the SS7 layer that the link is no longer in alarm.
1117  * \since 1.8
1118  *
1119  * \param linkset Controlling linkset for the channel.
1120  * \param which Link index of the signaling channel.
1121  *
1122  * \return Nothing
1123  */
1124 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
1125 {
1126         linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
1127         linkset->linkstate[which] |= LINKSTATE_STARTING;
1128         ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1129 }
1130
1131 /*!
1132  * \brief Setup and add a SS7 link channel.
1133  * \since 1.8
1134  *
1135  * \param linkset Controlling linkset for the channel.
1136  * \param which Link index of the signaling channel.
1137  * \param ss7type Switch type of the linkset
1138  * \param transport Signaling transport of channel.
1139  * \param inalarm Non-zero if the channel is in alarm.
1140  * \param networkindicator User configuration parameter.
1141  * \param pointcode User configuration parameter.
1142  * \param adjpointcode User configuration parameter.
1143  *
1144  * \retval 0 on success.
1145  * \retval -1 on error.
1146  */
1147 int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
1148 {
1149         if (!linkset->ss7) {
1150                 linkset->type = ss7type;
1151                 linkset->ss7 = ss7_new(ss7type);
1152                 if (!linkset->ss7) {
1153                         ast_log(LOG_ERROR, "Can't create new SS7!\n");
1154                         return -1;
1155                 }
1156         }
1157
1158         ss7_set_network_ind(linkset->ss7, networkindicator);
1159         ss7_set_pc(linkset->ss7, pointcode);
1160
1161         if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
1162                 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
1163         }
1164
1165         if (inalarm) {
1166                 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
1167                 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1168         } else {
1169                 linkset->linkstate[which] = LINKSTATE_DOWN;
1170                 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1171         }
1172
1173         ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
1174
1175         return 0;
1176 }
1177
1178 /*!
1179  * \brief Determine if the specified channel is available for an outgoing call.
1180  * \since 1.8
1181  *
1182  * \param p Signaling private structure pointer.
1183  *
1184  * \retval TRUE if the channel is available.
1185  */
1186 int sig_ss7_available(struct sig_ss7_chan *p)
1187 {
1188         if (!p->ss7) {
1189                 /* Something is wrong here.  A SS7 channel without the ss7 pointer? */
1190                 return 0;
1191         }
1192
1193         if (!p->inalarm && !p->owner && !p->ss7call
1194                 && !p->locallyblocked && !p->remotelyblocked) {
1195                 return 1;
1196         }
1197
1198         return 0;
1199 }
1200
1201 static unsigned char cid_pres2ss7pres(int cid_pres)
1202 {
1203          return (cid_pres >> 5) & 0x03;
1204 }
1205
1206 static unsigned char cid_pres2ss7screen(int cid_pres)
1207 {
1208         return cid_pres & 0x03;
1209 }
1210
1211 /*!
1212  * \brief Dial out using the specified SS7 channel.
1213  * \since 1.8
1214  *
1215  * \param p Signaling private structure pointer.
1216  * \param ast Asterisk channel structure pointer.
1217  * \param rdest Dialstring.
1218  *
1219  * \retval 0 on success.
1220  * \retval -1 on error.
1221  */
1222 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, char *rdest)
1223 {
1224         char ss7_called_nai;
1225         int called_nai_strip;
1226         char ss7_calling_nai;
1227         int calling_nai_strip;
1228         const char *charge_str = NULL;
1229         const char *gen_address = NULL;
1230         const char *gen_digits = NULL;
1231         const char *gen_dig_type = NULL;
1232         const char *gen_dig_scheme = NULL;
1233         const char *gen_name = NULL;
1234         const char *jip_digits = NULL;
1235         const char *lspi_ident = NULL;
1236         const char *rlt_flag = NULL;
1237         const char *call_ref_id = NULL;
1238         const char *call_ref_pc = NULL;
1239         const char *send_far = NULL;
1240         char *c;
1241         char *l;
1242         char dest[256];
1243
1244         ast_copy_string(dest, rdest, sizeof(dest));
1245
1246         c = strchr(dest, '/');
1247         if (c) {
1248                 c++;
1249         } else {
1250                 c = "";
1251         }
1252         if (strlen(c) < p->stripmsd) {
1253                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1254                 return -1;
1255         }
1256
1257         if (!p->hidecallerid) {
1258                 l = ast->connected.id.number;
1259         } else {
1260                 l = NULL;
1261         }
1262
1263         if (ss7_grab(p, p->ss7)) {
1264                 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
1265                 return -1;
1266         }
1267
1268         p->ss7call = isup_new_call(p->ss7->ss7);
1269         if (!p->ss7call) {
1270                 ss7_rel(p->ss7);
1271                 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
1272                 return -1;
1273         }
1274
1275         called_nai_strip = 0;
1276         ss7_called_nai = p->ss7->called_nai;
1277         if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
1278                 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1279                         called_nai_strip = strlen(p->ss7->internationalprefix);
1280                         ss7_called_nai = SS7_NAI_INTERNATIONAL;
1281                 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1282                         called_nai_strip = strlen(p->ss7->nationalprefix);
1283                         ss7_called_nai = SS7_NAI_NATIONAL;
1284                 } else {
1285                         ss7_called_nai = SS7_NAI_SUBSCRIBER;
1286                 }
1287         }
1288         isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
1289
1290         calling_nai_strip = 0;
1291         ss7_calling_nai = p->ss7->calling_nai;
1292         if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
1293                 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1294                         calling_nai_strip = strlen(p->ss7->internationalprefix);
1295                         ss7_calling_nai = SS7_NAI_INTERNATIONAL;
1296                 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1297                         calling_nai_strip = strlen(p->ss7->nationalprefix);
1298                         ss7_calling_nai = SS7_NAI_NATIONAL;
1299                 } else {
1300                         ss7_calling_nai = SS7_NAI_SUBSCRIBER;
1301                 }
1302         }
1303         isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
1304                 p->use_callingpres ? cid_pres2ss7pres(ast->connected.id.number_presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
1305                 p->use_callingpres ? cid_pres2ss7screen(ast->connected.id.number_presentation) : SS7_SCREENING_USER_PROVIDED );
1306
1307         isup_set_oli(p->ss7call, ast->connected.ani2);
1308         isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
1309
1310         /* Set the charge number if it is set */
1311         charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
1312         if (charge_str)
1313                 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
1314
1315         gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
1316         if (gen_address)
1317                 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); /* need to add some types here for NAI,PRES,TYPE */
1318
1319         gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
1320         gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
1321         gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
1322         if (gen_digits)
1323                 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
1324
1325         gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
1326         if (gen_name)
1327                 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
1328
1329         jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
1330         if (jip_digits)
1331                 isup_set_jip_digits(p->ss7call, jip_digits);
1332
1333         lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
1334         if (lspi_ident)
1335                 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
1336
1337         rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
1338         if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
1339                 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
1340         }
1341
1342         call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
1343         call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
1344         if (call_ref_id && call_ref_pc) {
1345                 isup_set_callref(p->ss7call, atoi(call_ref_id),
1346                                  call_ref_pc ? atoi(call_ref_pc) : 0);
1347         }
1348
1349         send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
1350         if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
1351                 (isup_far(p->ss7->ss7, p->ss7call));
1352
1353         isup_iam(p->ss7->ss7, p->ss7call);
1354         sig_ss7_set_dialing(p, 1);
1355         ast_setstate(ast, AST_STATE_DIALING);
1356         ss7_rel(p->ss7);
1357         return 0;
1358 }
1359
1360 /*!
1361  * \brief SS7 hangup channel.
1362  * \since 1.8
1363  *
1364  * \param p Signaling private structure pointer.
1365  * \param ast Asterisk channel structure pointer.
1366  *
1367  * \retval 0 on success.
1368  * \retval -1 on error.
1369  */
1370 int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
1371 {
1372         int res = 0;
1373
1374         if (!ast->tech_pvt) {
1375                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1376                 return 0;
1377         }
1378
1379         p->owner = NULL;
1380         sig_ss7_set_dialing(p, 0);
1381         p->outgoing = 0;
1382         p->proceeding = 0;
1383         p->progress = 0;
1384         p->alerting = 0;
1385         p->rlt = 0;
1386         p->exten[0] = '\0';
1387         /* Perform low level hangup if no owner left */
1388         if (p->ss7call) {
1389                 if (!ss7_grab(p, p->ss7)) {
1390                         if (!p->alreadyhungup) {
1391                                 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
1392                                 int icause = ast->hangupcause ? ast->hangupcause : -1;
1393
1394                                 if (cause) {
1395                                         if (atoi(cause))
1396                                                 icause = atoi(cause);
1397                                 }
1398                                 isup_rel(p->ss7->ss7, p->ss7call, icause);
1399                                 ss7_rel(p->ss7);
1400                                 p->alreadyhungup = 1;
1401                         } else
1402                                 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
1403                 } else {
1404                         ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
1405                         res = -1;
1406                 }
1407         }
1408
1409         return res;
1410 }
1411
1412 /*!
1413  * \brief SS7 answer channel.
1414  * \since 1.8
1415  *
1416  * \param p Signaling private structure pointer.
1417  * \param ast Asterisk channel structure pointer.
1418  *
1419  * \retval 0 on success.
1420  * \retval -1 on error.
1421  */
1422 int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
1423 {
1424         int res;
1425
1426         if (!ss7_grab(p, p->ss7)) {
1427                 p->proceeding = 1;
1428                 res = isup_anm(p->ss7->ss7, p->ss7call);
1429                 ss7_rel(p->ss7);
1430         } else {
1431                 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->ss7->span);
1432                 res = -1;
1433         }
1434         return res;
1435 }
1436
1437 /*!
1438  * \brief Fix up a channel:  If a channel is consumed, this is called.  Basically update any ->owner links.
1439  * \since 1.8
1440  *
1441  * \param oldchan Old channel pointer to replace.
1442  * \param newchan New channel pointer to set.
1443  * \param pchan Signaling private structure pointer.
1444  *
1445  * \return Nothing
1446  */
1447 void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
1448 {
1449         if (pchan->owner == oldchan) {
1450                 pchan->owner = newchan;
1451         }
1452 }
1453
1454 /*!
1455  * \brief SS7 answer channel.
1456  * \since 1.8
1457  *
1458  * \param p Signaling private structure pointer.
1459  * \param chan Asterisk channel structure pointer.
1460  * \param condition AST control frame subtype.
1461  * \param data AST control frame payload contents.
1462  * \param datalen Length of payload contents.
1463  *
1464  * \retval 0 on success.
1465  * \retval -1 on error or indication condition not handled.
1466  */
1467 int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
1468 {
1469         int res = -1;
1470
1471         switch (condition) {
1472         case AST_CONTROL_BUSY:
1473                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
1474                 break;
1475         case AST_CONTROL_RINGING:
1476                 if ((!p->alerting) && p->ss7 && !p->outgoing && (chan->_state != AST_STATE_UP)) {
1477                         if (p->ss7->ss7) {
1478                                 ss7_grab(p, p->ss7);
1479
1480                                 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
1481                                         p->rlt = 1;
1482                                 if (p->rlt != 1) /* No need to send CPG if call will be RELEASE */
1483                                         isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
1484                                 p->alerting = 1;
1485                                 ss7_rel(p->ss7);
1486                         }
1487                 }
1488
1489                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
1490
1491                 if (chan->_state != AST_STATE_UP && chan->_state != AST_STATE_RING) {
1492                         ast_setstate(chan, AST_STATE_RINGING);
1493                 }
1494                 break;
1495         case AST_CONTROL_PROCEEDING:
1496                 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
1497                 /* This IF sends the FAR for an answered ALEG call */
1498                 if (chan->_state == AST_STATE_UP && (p->rlt != 1)){
1499                         if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
1500                                 p->rlt = 1;
1501                 }
1502
1503                 if (!p->proceeding && p->ss7 && !p->outgoing) {
1504                         if (p->ss7->ss7) {
1505                                 ss7_grab(p, p->ss7);
1506                                 isup_acm(p->ss7->ss7, p->ss7call);
1507                                 p->proceeding = 1;
1508                                 ss7_rel(p->ss7);
1509                         }
1510                 }
1511                 /* don't continue in ast_indicate */
1512                 res = 0;
1513                 break;
1514         case AST_CONTROL_PROGRESS:
1515                 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
1516                 if (!p->progress && p->ss7 && !p->outgoing) {
1517                         if (p->ss7->ss7) {
1518                                 ss7_grab(p, p->ss7);
1519                                 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
1520                                 p->progress = 1;
1521                                 ss7_rel(p->ss7);
1522                                 /* enable echo canceler here on SS7 calls */
1523                                 sig_ss7_set_echocanceller(p, 1);
1524                         }
1525                 }
1526                 /* don't continue in ast_indicate */
1527                 res = 0;
1528                 break;
1529         case AST_CONTROL_CONGESTION:
1530                 chan->hangupcause = AST_CAUSE_CONGESTION;
1531                 break;
1532         case AST_CONTROL_HOLD:
1533                 ast_moh_start(chan, data, p->mohinterpret);
1534                 break;
1535         case AST_CONTROL_UNHOLD:
1536                 ast_moh_stop(chan);
1537                 break;
1538         case AST_CONTROL_SRCUPDATE:
1539                 res = 0;
1540                 break;
1541         case -1:
1542                 res = sig_ss7_play_tone(p, -1);
1543                 break;
1544         }
1545         return res;
1546 }
1547
1548 /*!
1549  * \brief SS7 channel request.
1550  * \since 1.8
1551  *
1552  * \param p Signaling private structure pointer.
1553  * \param law Companding law preferred
1554  * \param requestor Asterisk channel requesting a channel to dial (Can be NULL)
1555  * \param transfercapability
1556  *
1557  * \retval ast_channel on success.
1558  * \retval NULL on error.
1559  */
1560 struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
1561 {
1562         struct ast_channel *ast;
1563
1564         p->outgoing = 1;
1565         ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
1566         if (!ast) {
1567                 p->outgoing = 0;
1568         }
1569         return ast;
1570 }
1571
1572 /*!
1573  * \brief Delete the sig_ss7 private channel structure.
1574  * \since 1.8
1575  *
1576  * \param doomed sig_ss7 private channel structure to delete.
1577  *
1578  * \return Nothing
1579  */
1580 void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
1581 {
1582         ast_free(doomed);
1583 }
1584
1585 /*!
1586  * \brief Create a new sig_ss7 private channel structure.
1587  * \since 1.8
1588  *
1589  * \param pvt_data Upper layer private data structure.
1590  * \param callback Callbacks to the upper layer.
1591  * \param ss7 Controlling linkset for the channel.
1592  *
1593  * \retval sig_ss7_chan on success.
1594  * \retval NULL on error.
1595  */
1596 struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_callback *callback, struct sig_ss7_linkset *ss7)
1597 {
1598         struct sig_ss7_chan *pvt;
1599
1600         pvt = ast_calloc(1, sizeof(*pvt));
1601         if (!pvt) {
1602                 return pvt;
1603         }
1604
1605         pvt->calls = callback;
1606         pvt->chan_pvt = pvt_data;
1607         pvt->ss7 = ss7;
1608
1609         return pvt;
1610 }
1611
1612 /*!
1613  * \brief Initialize the SS7 linkset control.
1614  * \since 1.8
1615  *
1616  * \param ss7 sig_ss7 SS7 control structure.
1617  *
1618  * \return Nothing
1619  */
1620 void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
1621 {
1622         int idx;
1623
1624         memset(ss7, 0, sizeof(*ss7));
1625
1626         ast_mutex_init(&ss7->lock);
1627
1628         ss7->master = AST_PTHREADT_NULL;
1629         for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
1630                 ss7->fds[idx] = -1;
1631         }
1632 }
1633
1634 /* ------------------------------------------------------------------- */
1635
1636 #endif  /* defined(HAVE_SS7) */
1637 /* end sig_ss7.c */