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