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