0c9b8f51e7e49a136633df8cf1032b9ef4a54a5b
[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         if (linkset->type == SS7_ITU) {
574                 law = SIG_SS7_ALAW;
575         } else {
576                 law = SIG_SS7_ULAW;
577         }
578
579         /*
580          * Release the SS7 lock while we create the channel so other
581          * threads can send messages.  We must also release the private
582          * lock to prevent deadlock while creating the channel.
583          */
584         ast_mutex_unlock(&linkset->lock);
585         sig_ss7_unlock_private(p);
586         c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL);
587         if (!c) {
588                 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
589                 ast_mutex_lock(&linkset->lock);
590                 sig_ss7_lock_private(p);
591                 isup_rel(linkset->ss7, p->ss7call, -1);
592                 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
593                 p->alreadyhungup = 1;
594                 ast_callid_threadstorage_auto_clean(callid, callid_created);
595                 return;
596         }
597
598         /* Hold the channel and private lock while we setup the channel. */
599         ast_channel_lock(c);
600         sig_ss7_lock_private(p);
601
602         sig_ss7_set_echocanceller(p, 1);
603
604         /*
605          * It is reasonably safe to set the following
606          * channel variables while the channel private
607          * structure is locked.  The PBX has not been
608          * started yet and it is unlikely that any other task
609          * will do anything with the channel we have just
610          * created.
611          *
612          * We only reference these variables in the context of the ss7_linkset function
613          * when receiving either and IAM or a COT message.
614          */
615         if (!ast_strlen_zero(p->charge_number)) {
616                 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
617                 /* Clear this after we set it */
618                 p->charge_number[0] = 0;
619         }
620         if (!ast_strlen_zero(p->gen_add_number)) {
621                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
622                 /* Clear this after we set it */
623                 p->gen_add_number[0] = 0;
624         }
625         if (!ast_strlen_zero(p->jip_number)) {
626                 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
627                 /* Clear this after we set it */
628                 p->jip_number[0] = 0;
629         }
630         if (!ast_strlen_zero(p->gen_dig_number)) {
631                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
632                 /* Clear this after we set it */
633                 p->gen_dig_number[0] = 0;
634         }
635         if (!ast_strlen_zero(p->orig_called_num)) {
636                 pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
637                 /* Clear this after we set it */
638                 p->orig_called_num[0] = 0;
639         }
640
641         snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
642         pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
643         /* Clear this after we set it */
644         p->gen_dig_type = 0;
645
646         snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
647         pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
648         /* Clear this after we set it */
649         p->gen_dig_scheme = 0;
650
651         if (!ast_strlen_zero(p->lspi_ident)) {
652                 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
653                 /* Clear this after we set it */
654                 p->lspi_ident[0] = 0;
655         }
656
657         snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
658         pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
659         /* Clear this after we set it */
660         p->call_ref_ident = 0;
661
662         snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
663         pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
664         /* Clear this after we set it */
665         p->call_ref_pc = 0;
666
667         snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
668         pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
669         /* Clear this after we set it */
670         p->calling_party_cat = 0;
671
672         if (!ast_strlen_zero(p->redirecting_num)) {
673                 pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
674                 /* Clear this after we set it */
675                 p->redirecting_num[0] = 0;
676         }
677         if (!ast_strlen_zero(p->generic_name)) {
678                 pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
679                 /* Clear this after we set it */
680                 p->generic_name[0] = 0;
681         }
682
683         sig_ss7_unlock_private(p);
684         ast_channel_unlock(c);
685
686         if (ast_pbx_start(c)) {
687                 ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", ast_channel_name(c), p->cic);
688                 ast_hangup(c);
689         } else {
690                 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
691         }
692
693         /* Must return with linkset and private lock. */
694         ast_mutex_lock(&linkset->lock);
695         sig_ss7_lock_private(p);
696         ast_callid_threadstorage_auto_clean(callid, callid_created);
697 }
698
699 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
700 {
701         if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
702                 if (size) {
703                         *buf = '\0';
704                 }
705                 return;
706         }
707         switch (nai) {
708         case SS7_NAI_INTERNATIONAL:
709                 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
710                 break;
711         case SS7_NAI_NATIONAL:
712                 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
713                 break;
714         case SS7_NAI_SUBSCRIBER:
715                 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
716                 break;
717         case SS7_NAI_UNKNOWN:
718                 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
719                 break;
720         default:
721                 snprintf(buf, size, "%s", number);
722                 break;
723         }
724 }
725
726 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
727 {
728         return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
729 }
730
731 /*!
732  * \internal
733  * \brief Set callid threadstorage for the ss7_linkset thread to that of an existing channel
734  *
735  * \param linkset ss7 span control structure.
736  * \param chanpos channel position in the span
737  *
738  * \note Assumes the ss7->lock is already obtained.
739  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
740  *
741  * \return a reference to the callid bound to the channel which has also
742  *         been bound to threadstorage if it exists. If this returns non-NULL,
743  *         the callid must be unreffed and the threadstorage should be unbound
744  *         before the while loop wraps in ss7_linkset.
745  */
746 static struct ast_callid *func_ss7_linkset_callid(struct sig_ss7_linkset *linkset, int chanpos)
747 {
748         struct ast_callid *callid = NULL;
749         sig_ss7_lock_owner(linkset, chanpos);
750         if (linkset->pvts[chanpos]->owner) {
751                 callid = ast_channel_callid(linkset->pvts[chanpos]->owner);
752                 ast_channel_unlock(linkset->pvts[chanpos]->owner);
753                 if (callid) {
754                         ast_callid_threadassoc_add(callid);
755                 }
756         }
757
758         return callid;
759 }
760
761 /* This is a thread per linkset that handles all received events from libss7. */
762 void *ss7_linkset(void *data)
763 {
764         int res, i;
765         struct timeval *next = NULL, tv;
766         struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
767         struct ss7 *ss7 = linkset->ss7;
768         ss7_event *e = NULL;
769         struct sig_ss7_chan *p;
770         struct pollfd pollers[SIG_SS7_NUM_DCHANS];
771         int nextms = 0;
772
773         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
774
775         ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
776         ast_mutex_lock(&linkset->lock);
777         ss7_start(ss7);
778         ast_mutex_unlock(&linkset->lock);
779
780         for (;;) {
781                 ast_mutex_lock(&linkset->lock);
782                 if ((next = ss7_schedule_next(ss7))) {
783                         tv = ast_tvnow();
784                         tv.tv_sec = next->tv_sec - tv.tv_sec;
785                         tv.tv_usec = next->tv_usec - tv.tv_usec;
786                         if (tv.tv_usec < 0) {
787                                 tv.tv_usec += 1000000;
788                                 tv.tv_sec -= 1;
789                         }
790                         if (tv.tv_sec < 0) {
791                                 tv.tv_sec = 0;
792                                 tv.tv_usec = 0;
793                         }
794                         nextms = tv.tv_sec * 1000;
795                         nextms += tv.tv_usec / 1000;
796                 }
797
798                 for (i = 0; i < linkset->numsigchans; i++) {
799                         pollers[i].fd = linkset->fds[i];
800                         pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
801                         pollers[i].revents = 0;
802                 }
803                 ast_mutex_unlock(&linkset->lock);
804
805                 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
806                 pthread_testcancel();
807                 res = poll(pollers, linkset->numsigchans, nextms);
808                 pthread_testcancel();
809                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
810
811                 if ((res < 0) && (errno != EINTR)) {
812                         ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
813                 } else if (!res) {
814                         ast_mutex_lock(&linkset->lock);
815                         ss7_schedule_run(ss7);
816                         ast_mutex_unlock(&linkset->lock);
817                         continue;
818                 }
819
820                 ast_mutex_lock(&linkset->lock);
821                 for (i = 0; i < linkset->numsigchans; i++) {
822                         if (pollers[i].revents & POLLPRI) {
823                                 sig_ss7_handle_link_exception(linkset, i);
824                         }
825                         if (pollers[i].revents & POLLIN) {
826                                 res = ss7_read(ss7, pollers[i].fd);
827                         }
828                         if (pollers[i].revents & POLLOUT) {
829                                 res = ss7_write(ss7, pollers[i].fd);
830                                 if (res < 0) {
831                                         ast_debug(1, "Error in write %s\n", strerror(errno));
832                                 }
833                         }
834                 }
835
836                 while ((e = ss7_check_event(ss7))) {
837                         struct ast_callid *callid = NULL;
838                         int chanpos = -1;
839                         char cause_str[30];
840
841                         if (linkset->debug) {
842                                 ast_verbose("Linkset %d: Processing event: %s\n",
843                                         linkset->span, ss7_event2str(e->e));
844                         }
845
846                         switch (e->e) {
847                         case SS7_EVENT_UP:
848                                 if (linkset->state != LINKSET_STATE_UP) {
849                                         ast_verbose("--- SS7 Up ---\n");
850                                         ss7_reset_linkset(linkset);
851                                 }
852                                 linkset->state = LINKSET_STATE_UP;
853                                 break;
854                         case SS7_EVENT_DOWN:
855                                 ast_verbose("--- SS7 Down ---\n");
856                                 linkset->state = LINKSET_STATE_DOWN;
857                                 for (i = 0; i < linkset->numchans; i++) {
858                                         p = linkset->pvts[i];
859                                         if (p) {
860                                                 sig_ss7_set_alarm(p, 1);
861                                         }
862                                 }
863                                 break;
864                         case MTP2_LINK_UP:
865                                 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
866                                 break;
867                         case MTP2_LINK_DOWN:
868                                 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
869                                 break;
870                         case ISUP_EVENT_CPG:
871                                 chanpos = ss7_find_cic_gripe(linkset, e->cpg.cic, e->cpg.opc, "CPG");
872                                 if (chanpos < 0) {
873                                         break;
874                                 }
875                                 p = linkset->pvts[chanpos];
876                                 sig_ss7_lock_private(p);
877                                 callid = func_ss7_linkset_callid(linkset, chanpos);
878
879                                 switch (e->cpg.event) {
880                                 case CPG_EVENT_ALERTING:
881                                         if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
882                                                 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
883                                         }
884                                         sig_ss7_lock_owner(linkset, chanpos);
885                                         if (p->owner) {
886                                                 ast_setstate(p->owner, AST_STATE_RINGING);
887                                                 ast_channel_unlock(p->owner);
888                                         }
889                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
890                                         break;
891                                 case CPG_EVENT_PROGRESS:
892                                 case CPG_EVENT_INBANDINFO:
893                                         {
894                                                 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
895                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
896                                                 p->progress = 1;
897                                                 sig_ss7_set_dialing(p, 0);
898                                                 sig_ss7_open_media(p);
899                                         }
900                                         break;
901                                 default:
902                                         ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
903                                         break;
904                                 }
905
906                                 sig_ss7_unlock_private(p);
907                                 break;
908                         case ISUP_EVENT_RSC:
909                                 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
910                                 chanpos = ss7_find_cic_gripe(linkset, e->rsc.cic, e->rsc.opc, "RSC");
911                                 if (chanpos < 0) {
912                                         break;
913                                 }
914                                 p = linkset->pvts[chanpos];
915                                 sig_ss7_lock_private(p);
916                                 callid = func_ss7_linkset_callid(linkset, chanpos);
917                                 sig_ss7_set_inservice(p, 1);
918                                 sig_ss7_set_remotelyblocked(p, 0);
919                                 isup_set_call_dpc(e->rsc.call, p->dpc);
920                                 sig_ss7_lock_owner(linkset, chanpos);
921                                 p->ss7call = NULL;
922                                 if (p->owner) {
923                                         ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_RSC", AST_CAUSE_INTERWORKING);
924                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
925                                         ast_channel_unlock(p->owner);
926                                 }
927                                 sig_ss7_unlock_private(p);
928                                 isup_rlc(ss7, e->rsc.call);
929                                 break;
930                         case ISUP_EVENT_GRS:
931                                 ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
932                                 chanpos = ss7_find_cic_gripe(linkset, e->grs.startcic, e->grs.opc, "GRS");
933                                 if (chanpos < 0) {
934                                         break;
935                                 }
936                                 p = linkset->pvts[chanpos];
937                                 isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
938                                 ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
939                                 ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
940                                 break;
941                         case ISUP_EVENT_CQM:
942                                 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
943                                 ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
944                                 break;
945                         case ISUP_EVENT_GRA:
946                                 ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
947                                 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
948                                 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
949                                 break;
950                         case ISUP_EVENT_IAM:
951                                 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);
952                                 chanpos = ss7_find_cic_gripe(linkset, e->iam.cic, e->iam.opc, "IAM");
953                                 if (chanpos < 0) {
954                                         isup_rel(ss7, e->iam.call, -1);
955                                         break;
956                                 }
957                                 p = linkset->pvts[chanpos];
958                                 sig_ss7_lock_private(p);
959                                 sig_ss7_lock_owner(linkset, chanpos);
960                                 if (p->call_level != SIG_SS7_CALL_LEVEL_IDLE) {
961                                         /*
962                                          * Detected glare/dual-seizure
963                                          *
964                                          * Always abort both calls since we can't implement the dual
965                                          * seizure procedures due to our channel assignment architecture
966                                          * and the fact that we cannot tell libss7 to discard its call
967                                          * structure to ignore the incoming IAM.
968                                          */
969                                         ast_debug(1,
970                                                 "Linkset %d: SS7 IAM glare on CIC/DPC %d/%d.  Dropping both calls.\n",
971                                                 linkset->span, e->iam.cic, e->iam.opc);
972                                         if (p->call_level == SIG_SS7_CALL_LEVEL_ALLOCATED) {
973                                                 /*
974                                                  * We have not sent our IAM yet and we never will at this point.
975                                                  */
976                                                 p->alreadyhungup = 1;
977                                                 isup_rel(ss7, e->iam.call, -1);
978                                         }
979                                         p->call_level = SIG_SS7_CALL_LEVEL_GLARE;
980                                         if (p->owner) {
981                                                 ss7_queue_pvt_cause_data(p->owner, "SS7 ISUP_EVENT_IAM (glare)", AST_CAUSE_NORMAL_CLEARING);
982                                                 ast_channel_hangupcause_set(p->owner, AST_CAUSE_NORMAL_CLEARING);
983                                                 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
984                                                 ast_channel_unlock(p->owner);
985                                         }
986                                         sig_ss7_unlock_private(p);
987                                         break;
988                                 }
989                                 /*
990                                  * The channel should not have an owner at this point since we
991                                  * are in the process of creating an owner for it.
992                                  */
993                                 ast_assert(!p->owner);
994
995                                 /* Mark channel as in use so no outgoing call will steal it. */
996                                 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
997                                 p->ss7call = e->iam.call;
998
999                                 isup_set_call_dpc(p->ss7call, p->dpc);
1000
1001                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
1002                                         ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
1003                                         p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
1004                                 } else
1005                                         p->cid_num[0] = 0;
1006
1007                                 /* Set DNID */
1008                                 if (!ast_strlen_zero(e->iam.called_party_num)) {
1009                                         ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
1010                                                 e->iam.called_party_num, e->iam.called_nai);
1011                                 } else {
1012                                         p->exten[0] = '\0';
1013                                 }
1014                                 sig_ss7_set_dnid(p, p->exten);
1015
1016                                 if (p->immediate) {
1017                                         p->exten[0] = 's';
1018                                         p->exten[1] = '\0';
1019                                 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
1020                                         char *st;
1021                                         ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
1022                                         st = strchr(p->exten, '#');
1023                                         if (st) {
1024                                                 *st = '\0';
1025                                         }
1026                                 } else {
1027                                         p->exten[0] = '\0';
1028                                 }
1029
1030                                 p->cid_ani[0] = '\0';
1031                                 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
1032                                         ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
1033                                 else
1034                                         p->cid_name[0] = '\0';
1035
1036                                 p->cid_ani2 = e->iam.oli_ani2;
1037                                 p->cid_ton = 0;
1038                                 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
1039                                 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
1040                                 p->gen_add_type = e->iam.gen_add_type;
1041                                 p->gen_add_nai = e->iam.gen_add_nai;
1042                                 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
1043                                 p->gen_add_num_plan = e->iam.gen_add_num_plan;
1044                                 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
1045                                 p->gen_dig_type = e->iam.gen_dig_type;
1046                                 p->gen_dig_scheme = e->iam.gen_dig_scheme;
1047                                 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
1048                                 ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
1049                                 ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
1050                                 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
1051                                 p->calling_party_cat = e->iam.calling_party_cat;
1052
1053                                 sig_ss7_set_caller_id(p);
1054
1055                                 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
1056                                         if (e->iam.cot_check_required) {
1057                                                 p->call_level = SIG_SS7_CALL_LEVEL_CONTINUITY;
1058                                                 sig_ss7_loopback(p, 1);
1059                                         } else {
1060                                                 ss7_start_call(p, linkset);
1061                                         }
1062                                 } else {
1063                                         ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
1064                                         p->alreadyhungup = 1;
1065                                         isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
1066                                 }
1067                                 sig_ss7_unlock_private(p);
1068                                 break;
1069                         case ISUP_EVENT_COT:
1070                                 chanpos = ss7_find_cic_gripe(linkset, e->cot.cic, e->cot.opc, "COT");
1071                                 if (chanpos < 0) {
1072                                         isup_rel(ss7, e->cot.call, -1);
1073                                         break;
1074                                 }
1075                                 p = linkset->pvts[chanpos];
1076
1077                                 sig_ss7_lock_private(p);
1078                                 if (p->loopedback) {
1079                                         sig_ss7_loopback(p, 0);
1080                                         ss7_start_call(p, linkset);
1081                                 }
1082                                 sig_ss7_unlock_private(p);
1083                                 break;
1084                         case ISUP_EVENT_CCR:
1085                                 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
1086                                 chanpos = ss7_find_cic_gripe(linkset, e->ccr.cic, e->ccr.opc, "CCR");
1087                                 if (chanpos < 0) {
1088                                         break;
1089                                 }
1090
1091                                 p = linkset->pvts[chanpos];
1092
1093                                 sig_ss7_lock_private(p);
1094                                 sig_ss7_loopback(p, 1);
1095                                 sig_ss7_unlock_private(p);
1096
1097                                 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
1098                                 break;
1099                         case ISUP_EVENT_CVT:
1100                                 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
1101                                 chanpos = ss7_find_cic_gripe(linkset, e->cvt.cic, e->cvt.opc, "CVT");
1102                                 if (chanpos < 0) {
1103                                         break;
1104                                 }
1105
1106                                 p = linkset->pvts[chanpos];
1107
1108                                 sig_ss7_lock_private(p);
1109                                 sig_ss7_loopback(p, 1);
1110                                 sig_ss7_unlock_private(p);
1111
1112                                 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
1113                                 break;
1114                         case ISUP_EVENT_REL:
1115                                 chanpos = ss7_find_cic_gripe(linkset, e->rel.cic, e->rel.opc, "REL");
1116                                 if (chanpos < 0) {
1117                                         /* Continue hanging up the call anyway. */
1118                                         isup_rlc(ss7, e->rel.call);
1119                                         break;
1120                                 }
1121                                 p = linkset->pvts[chanpos];
1122                                 sig_ss7_lock_private(p);
1123                                 callid = func_ss7_linkset_callid(linkset, chanpos);
1124                                 sig_ss7_lock_owner(linkset, chanpos);
1125                                 if (p->owner) {
1126                                         snprintf(cause_str, sizeof(cause_str), "SS7 ISUP_EVENT_REL (%d)", e->rel.cause);
1127                                         ss7_queue_pvt_cause_data(p->owner, cause_str, e->rel.cause);
1128
1129                                         ast_channel_hangupcause_set(p->owner, e->rel.cause);
1130                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
1131                                         ast_channel_unlock(p->owner);
1132                                 }
1133
1134                                 /* End the loopback if we have one */
1135                                 sig_ss7_loopback(p, 0);
1136
1137                                 isup_rlc(ss7, e->rel.call);
1138                                 p->ss7call = NULL;
1139
1140                                 sig_ss7_unlock_private(p);
1141                                 break;
1142                         case ISUP_EVENT_ACM:
1143                                 chanpos = ss7_find_cic_gripe(linkset, e->acm.cic, e->acm.opc, "ACM");
1144                                 if (chanpos < 0) {
1145                                         isup_rel(ss7, e->acm.call, -1);
1146                                         break;
1147                                 }
1148                                 {
1149                                         p = linkset->pvts[chanpos];
1150
1151                                         ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
1152
1153                                         if (e->acm.call_ref_ident > 0) {
1154                                                 p->rlt = 1; /* Setting it but not using it here*/
1155                                         }
1156
1157                                         sig_ss7_lock_private(p);
1158                                         callid = func_ss7_linkset_callid(linkset, chanpos);
1159                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
1160                                         if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
1161                                                 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
1162                                         }
1163                                         sig_ss7_set_dialing(p, 0);
1164                                         /* Send alerting if subscriber is free */
1165                                         if (e->acm.called_party_status_ind == 1) {
1166                                                 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
1167                                                         p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
1168                                                 }
1169                                                 sig_ss7_lock_owner(linkset, chanpos);
1170                                                 if (p->owner) {
1171                                                         ast_setstate(p->owner, AST_STATE_RINGING);
1172                                                         ast_channel_unlock(p->owner);
1173                                                 }
1174                                                 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
1175                                         }
1176                                         sig_ss7_unlock_private(p);
1177                                 }
1178                                 break;
1179                         case ISUP_EVENT_CGB:
1180                                 chanpos = ss7_find_cic_gripe(linkset, e->cgb.startcic, e->cgb.opc, "CGB");
1181                                 if (chanpos < 0) {
1182                                         break;
1183                                 }
1184                                 p = linkset->pvts[chanpos];
1185                                 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
1186                                 isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
1187                                 break;
1188                         case ISUP_EVENT_CGU:
1189                                 chanpos = ss7_find_cic_gripe(linkset, e->cgu.startcic, e->cgu.opc, "CGU");
1190                                 if (chanpos < 0) {
1191                                         break;
1192                                 }
1193                                 p = linkset->pvts[chanpos];
1194                                 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
1195                                 isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
1196                                 break;
1197                         case ISUP_EVENT_UCIC:
1198                                 chanpos = ss7_find_cic_gripe(linkset, e->ucic.cic, e->ucic.opc, "UCIC");
1199                                 if (chanpos < 0) {
1200                                         break;
1201                                 }
1202                                 p = linkset->pvts[chanpos];
1203                                 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
1204                                 sig_ss7_lock_private(p);
1205                                 sig_ss7_set_remotelyblocked(p, 1);
1206                                 sig_ss7_set_inservice(p, 0);
1207                                 sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
1208                                 break;
1209                         case ISUP_EVENT_BLO:
1210                                 chanpos = ss7_find_cic_gripe(linkset, e->blo.cic, e->blo.opc, "BLO");
1211                                 if (chanpos < 0) {
1212                                         break;
1213                                 }
1214                                 p = linkset->pvts[chanpos];
1215                                 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
1216                                 sig_ss7_lock_private(p);
1217                                 sig_ss7_set_remotelyblocked(p, 1);
1218                                 sig_ss7_unlock_private(p);
1219                                 isup_bla(linkset->ss7, e->blo.cic, p->dpc);
1220                                 break;
1221                         case ISUP_EVENT_BLA:
1222                                 chanpos = ss7_find_cic_gripe(linkset, e->bla.cic, e->bla.opc, "BLA");
1223                                 if (chanpos < 0) {
1224                                         break;
1225                                 }
1226                                 ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
1227                                 p = linkset->pvts[chanpos];
1228                                 sig_ss7_lock_private(p);
1229                                 sig_ss7_set_locallyblocked(p, 1);
1230                                 sig_ss7_unlock_private(p);
1231                                 break;
1232                         case ISUP_EVENT_UBL:
1233                                 chanpos = ss7_find_cic_gripe(linkset, e->ubl.cic, e->ubl.opc, "UBL");
1234                                 if (chanpos < 0) {
1235                                         break;
1236                                 }
1237                                 p = linkset->pvts[chanpos];
1238                                 ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
1239                                 sig_ss7_lock_private(p);
1240                                 sig_ss7_set_remotelyblocked(p, 0);
1241                                 sig_ss7_unlock_private(p);
1242                                 isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
1243                                 break;
1244                         case ISUP_EVENT_UBA:
1245                                 chanpos = ss7_find_cic_gripe(linkset, e->uba.cic, e->uba.opc, "UBA");
1246                                 if (chanpos < 0) {
1247                                         break;
1248                                 }
1249                                 p = linkset->pvts[chanpos];
1250                                 ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
1251                                 sig_ss7_lock_private(p);
1252                                 sig_ss7_set_locallyblocked(p, 0);
1253                                 sig_ss7_unlock_private(p);
1254                                 break;
1255                         case ISUP_EVENT_CON:
1256                         case ISUP_EVENT_ANM:
1257                                 if (e->e == ISUP_EVENT_CON) {
1258                                         chanpos = ss7_find_cic_gripe(linkset, e->con.cic, e->con.opc, "CON");
1259                                         if (chanpos < 0) {
1260                                                 isup_rel(ss7, e->con.call, -1);
1261                                                 break;
1262                                         }
1263                                 } else {
1264                                         chanpos = ss7_find_cic_gripe(linkset, e->anm.cic, e->anm.opc, "ANM");
1265                                         if (chanpos < 0) {
1266                                                 isup_rel(ss7, e->anm.call, -1);
1267                                                 break;
1268                                         }
1269                                 }
1270
1271                                 {
1272                                         p = linkset->pvts[chanpos];
1273                                         sig_ss7_lock_private(p);
1274                                         callid = func_ss7_linkset_callid(linkset, chanpos);
1275                                         if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1276                                                 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
1277                                         }
1278                                         sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
1279                                         sig_ss7_set_dialing(p, 0);
1280                                         sig_ss7_open_media(p);
1281                                         sig_ss7_set_echocanceller(p, 1);
1282                                         sig_ss7_unlock_private(p);
1283                                 }
1284                                 break;
1285                         case ISUP_EVENT_RLC:
1286                                 /* XXX Call ptr should be passed up from libss7! */
1287                                 chanpos = ss7_find_cic_gripe(linkset, e->rlc.cic, e->rlc.opc, "RLC");
1288                                 if (chanpos < 0) {
1289                                         break;
1290                                 }
1291                                 {
1292                                         p = linkset->pvts[chanpos];
1293                                         sig_ss7_lock_private(p);
1294                                         callid = func_ss7_linkset_callid(linkset, chanpos);
1295                                         if (p->alreadyhungup) {
1296                                                 if (!p->owner) {
1297                                                         p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1298                                                 }
1299                                                 p->ss7call = NULL;
1300                                         }
1301                                         sig_ss7_unlock_private(p);
1302                                 }
1303                                 break;
1304                         case ISUP_EVENT_FAA:
1305                                 /*!
1306                                  * \todo The handling of the SS7 FAA message is not good and I
1307                                  * don't know enough to handle it correctly.
1308                                  */
1309                                 chanpos = ss7_find_cic_gripe(linkset, e->faa.cic, e->faa.opc, "FAA");
1310                                 if (chanpos < 0) {
1311                                         isup_rel(linkset->ss7, e->faa.call, -1);
1312                                         break;
1313                                 }
1314                                 {
1315                                         /* XXX FAR and FAA used for something dealing with transfers? */
1316                                         p = linkset->pvts[chanpos];
1317                                         ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
1318                                         sig_ss7_lock_private(p);
1319                                         callid = func_ss7_linkset_callid(linkset, chanpos);
1320                                         if (p->alreadyhungup){
1321                                                 if (!p->owner) {
1322                                                         p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1323                                                 }
1324                                                 /* XXX We seem to be leaking the isup call structure here. */
1325                                                 p->ss7call = NULL;
1326                                                 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR.  Ignoring.\n");
1327                                         }
1328                                         sig_ss7_unlock_private(p);
1329                                 }
1330                                 break;
1331                         default:
1332                                 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
1333                                 break;
1334                         }
1335
1336                         /* Call ID stuff needs to be cleaned up here */
1337                         if (callid) {
1338                                 callid = ast_callid_unref(callid);
1339                                 ast_callid_threadassoc_remove();
1340                         }
1341                 }
1342                 ast_mutex_unlock(&linkset->lock);
1343         }
1344
1345         return 0;
1346 }
1347
1348 static inline void ss7_rel(struct sig_ss7_linkset *ss7)
1349 {
1350         ast_mutex_unlock(&ss7->lock);
1351 }
1352
1353 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
1354 {
1355         /* Grab the lock first */
1356         while (ast_mutex_trylock(&ss7->lock)) {
1357                 /* Avoid deadlock */
1358                 sig_ss7_deadlock_avoidance_private(pvt);
1359         }
1360         /* Then break the poll */
1361         if (ss7->master != AST_PTHREADT_NULL) {
1362                 pthread_kill(ss7->master, SIGURG);
1363         }
1364 }
1365
1366 /*!
1367  * \brief Notify the SS7 layer that the link is in alarm.
1368  * \since 1.8
1369  *
1370  * \param linkset Controlling linkset for the channel.
1371  * \param which Link index of the signaling channel.
1372  *
1373  * \return Nothing
1374  */
1375 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
1376 {
1377         linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
1378         linkset->linkstate[which] &= ~LINKSTATE_UP;
1379         ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1380 }
1381
1382 /*!
1383  * \brief Notify the SS7 layer that the link is no longer in alarm.
1384  * \since 1.8
1385  *
1386  * \param linkset Controlling linkset for the channel.
1387  * \param which Link index of the signaling channel.
1388  *
1389  * \return Nothing
1390  */
1391 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
1392 {
1393         linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
1394         linkset->linkstate[which] |= LINKSTATE_STARTING;
1395         ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1396 }
1397
1398 /*!
1399  * \brief Setup and add a SS7 link channel.
1400  * \since 1.8
1401  *
1402  * \param linkset Controlling linkset for the channel.
1403  * \param which Link index of the signaling channel.
1404  * \param ss7type Switch type of the linkset
1405  * \param transport Signaling transport of channel.
1406  * \param inalarm Non-zero if the channel is in alarm.
1407  * \param networkindicator User configuration parameter.
1408  * \param pointcode User configuration parameter.
1409  * \param adjpointcode User configuration parameter.
1410  *
1411  * \retval 0 on success.
1412  * \retval -1 on error.
1413  */
1414 int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
1415 {
1416         if (!linkset->ss7) {
1417                 linkset->type = ss7type;
1418                 linkset->ss7 = ss7_new(ss7type);
1419                 if (!linkset->ss7) {
1420                         ast_log(LOG_ERROR, "Can't create new SS7!\n");
1421                         return -1;
1422                 }
1423         }
1424
1425         ss7_set_network_ind(linkset->ss7, networkindicator);
1426         ss7_set_pc(linkset->ss7, pointcode);
1427
1428         if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
1429                 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
1430         }
1431
1432         if (inalarm) {
1433                 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
1434                 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1435         } else {
1436                 linkset->linkstate[which] = LINKSTATE_DOWN;
1437                 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1438         }
1439
1440         ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
1441
1442         return 0;
1443 }
1444
1445 /*!
1446  * \internal
1447  * \brief Determine if a private channel structure is available.
1448  *
1449  * \param pvt Channel to determine if available.
1450  *
1451  * \return TRUE if the channel is available.
1452  */
1453 static int sig_ss7_is_chan_available(struct sig_ss7_chan *pvt)
1454 {
1455         if (!pvt->inalarm && !pvt->owner && !pvt->ss7call
1456                 && pvt->call_level == SIG_SS7_CALL_LEVEL_IDLE
1457                 && !pvt->locallyblocked && !pvt->remotelyblocked) {
1458                 return 1;
1459         }
1460         return 0;
1461 }
1462
1463 /*!
1464  * \brief Determine if the specified channel is available for an outgoing call.
1465  * \since 1.8
1466  *
1467  * \param p Signaling private structure pointer.
1468  *
1469  * \retval TRUE if the channel is available.
1470  */
1471 int sig_ss7_available(struct sig_ss7_chan *p)
1472 {
1473         int available;
1474
1475         if (!p->ss7) {
1476                 /* Something is wrong here.  A SS7 channel without the ss7 pointer? */
1477                 return 0;
1478         }
1479
1480         /* Only have to deal with the linkset lock. */
1481         ast_mutex_lock(&p->ss7->lock);
1482         available = sig_ss7_is_chan_available(p);
1483         if (available) {
1484                 p->call_level = SIG_SS7_CALL_LEVEL_ALLOCATED;
1485         }
1486         ast_mutex_unlock(&p->ss7->lock);
1487
1488         return available;
1489 }
1490
1491 static unsigned char cid_pres2ss7pres(int cid_pres)
1492 {
1493          return (cid_pres >> 5) & 0x03;
1494 }
1495
1496 static unsigned char cid_pres2ss7screen(int cid_pres)
1497 {
1498         return cid_pres & 0x03;
1499 }
1500
1501 /*!
1502  * \brief Dial out using the specified SS7 channel.
1503  * \since 1.8
1504  *
1505  * \param p Signaling private structure pointer.
1506  * \param ast Asterisk channel structure pointer.
1507  * \param rdest Dialstring.
1508  *
1509  * \retval 0 on success.
1510  * \retval -1 on error.
1511  */
1512 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, const char *rdest)
1513 {
1514         char ss7_called_nai;
1515         int called_nai_strip;
1516         char ss7_calling_nai;
1517         int calling_nai_strip;
1518         const char *charge_str = NULL;
1519         const char *gen_address = NULL;
1520         const char *gen_digits = NULL;
1521         const char *gen_dig_type = NULL;
1522         const char *gen_dig_scheme = NULL;
1523         const char *gen_name = NULL;
1524         const char *jip_digits = NULL;
1525         const char *lspi_ident = NULL;
1526         const char *rlt_flag = NULL;
1527         const char *call_ref_id = NULL;
1528         const char *call_ref_pc = NULL;
1529         const char *send_far = NULL;
1530         char *c;
1531         char *l;
1532         char dest[256];
1533
1534         ast_copy_string(dest, rdest, sizeof(dest));
1535
1536         c = strchr(dest, '/');
1537         if (c) {
1538                 c++;
1539         } else {
1540                 c = "";
1541         }
1542         if (strlen(c) < p->stripmsd) {
1543                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1544                 return -1;
1545         }
1546
1547         if (!p->hidecallerid) {
1548                 l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
1549         } else {
1550                 l = NULL;
1551         }
1552
1553         ss7_grab(p, p->ss7);
1554
1555         if (p->call_level != SIG_SS7_CALL_LEVEL_ALLOCATED) {
1556                 /* Call collision before sending IAM.  Abort call. */
1557                 ss7_rel(p->ss7);
1558                 return -1;
1559         }
1560
1561         p->ss7call = isup_new_call(p->ss7->ss7);
1562         if (!p->ss7call) {
1563                 ss7_rel(p->ss7);
1564                 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
1565                 return -1;
1566         }
1567
1568         called_nai_strip = 0;
1569         ss7_called_nai = p->ss7->called_nai;
1570         if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
1571                 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1572                         called_nai_strip = strlen(p->ss7->internationalprefix);
1573                         ss7_called_nai = SS7_NAI_INTERNATIONAL;
1574                 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1575                         called_nai_strip = strlen(p->ss7->nationalprefix);
1576                         ss7_called_nai = SS7_NAI_NATIONAL;
1577                 } else {
1578                         ss7_called_nai = SS7_NAI_SUBSCRIBER;
1579                 }
1580         }
1581         isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
1582
1583         calling_nai_strip = 0;
1584         ss7_calling_nai = p->ss7->calling_nai;
1585         if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
1586                 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1587                         calling_nai_strip = strlen(p->ss7->internationalprefix);
1588                         ss7_calling_nai = SS7_NAI_INTERNATIONAL;
1589                 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1590                         calling_nai_strip = strlen(p->ss7->nationalprefix);
1591                         ss7_calling_nai = SS7_NAI_NATIONAL;
1592                 } else {
1593                         ss7_calling_nai = SS7_NAI_SUBSCRIBER;
1594                 }
1595         }
1596         isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
1597                 p->use_callingpres ? cid_pres2ss7pres(ast_channel_connected(ast)->id.number.presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
1598                 p->use_callingpres ? cid_pres2ss7screen(ast_channel_connected(ast)->id.number.presentation) : SS7_SCREENING_USER_PROVIDED);
1599
1600         isup_set_oli(p->ss7call, ast_channel_connected(ast)->ani2);
1601         isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
1602
1603         /* Set the charge number if it is set */
1604         charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
1605         if (charge_str)
1606                 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
1607
1608         gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
1609         if (gen_address)
1610                 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 */
1611
1612         gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
1613         gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
1614         gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
1615         if (gen_digits)
1616                 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
1617
1618         gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
1619         if (gen_name)
1620                 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
1621
1622         jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
1623         if (jip_digits)
1624                 isup_set_jip_digits(p->ss7call, jip_digits);
1625
1626         lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
1627         if (lspi_ident)
1628                 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
1629
1630         rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
1631         if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
1632                 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
1633         }
1634
1635         call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
1636         call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
1637         if (call_ref_id && call_ref_pc) {
1638                 isup_set_callref(p->ss7call, atoi(call_ref_id),
1639                                  call_ref_pc ? atoi(call_ref_pc) : 0);
1640         }
1641
1642         send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
1643         if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
1644                 (isup_far(p->ss7->ss7, p->ss7call));
1645
1646         p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
1647         isup_iam(p->ss7->ss7, p->ss7call);
1648         sig_ss7_set_dialing(p, 1);
1649         ast_setstate(ast, AST_STATE_DIALING);
1650         ss7_rel(p->ss7);
1651         return 0;
1652 }
1653
1654 /*!
1655  * \brief SS7 hangup channel.
1656  * \since 1.8
1657  *
1658  * \param p Signaling private structure pointer.
1659  * \param ast Asterisk channel structure pointer.
1660  *
1661  * \retval 0 on success.
1662  * \retval -1 on error.
1663  */
1664 int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
1665 {
1666         int res = 0;
1667
1668         if (!ast_channel_tech_pvt(ast)) {
1669                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1670                 return 0;
1671         }
1672
1673         p->owner = NULL;
1674         sig_ss7_set_dialing(p, 0);
1675         sig_ss7_set_outgoing(p, 0);
1676         p->progress = 0;
1677         p->rlt = 0;
1678         p->exten[0] = '\0';
1679         /* Perform low level hangup if no owner left */
1680         ss7_grab(p, p->ss7);
1681         p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1682         if (p->ss7call) {
1683                 if (!p->alreadyhungup) {
1684                         const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
1685                         int icause = ast_channel_hangupcause(ast) ? ast_channel_hangupcause(ast) : -1;
1686
1687                         if (cause) {
1688                                 if (atoi(cause)) {
1689                                         icause = atoi(cause);
1690                                 }
1691                         }
1692                         isup_rel(p->ss7->ss7, p->ss7call, icause);
1693                         p->alreadyhungup = 1;
1694                 }
1695         }
1696         ss7_rel(p->ss7);
1697
1698         return res;
1699 }
1700
1701 /*!
1702  * \brief SS7 answer channel.
1703  * \since 1.8
1704  *
1705  * \param p Signaling private structure pointer.
1706  * \param ast Asterisk channel structure pointer.
1707  *
1708  * \retval 0 on success.
1709  * \retval -1 on error.
1710  */
1711 int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
1712 {
1713         int res;
1714
1715         ss7_grab(p, p->ss7);
1716         if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1717                 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
1718         }
1719         sig_ss7_open_media(p);
1720         res = isup_anm(p->ss7->ss7, p->ss7call);
1721         ss7_rel(p->ss7);
1722         return res;
1723 }
1724
1725 /*!
1726  * \brief Fix up a channel:  If a channel is consumed, this is called.  Basically update any ->owner links.
1727  * \since 1.8
1728  *
1729  * \param oldchan Old channel pointer to replace.
1730  * \param newchan New channel pointer to set.
1731  * \param pchan Signaling private structure pointer.
1732  *
1733  * \return Nothing
1734  */
1735 void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
1736 {
1737         if (pchan->owner == oldchan) {
1738                 pchan->owner = newchan;
1739         }
1740 }
1741
1742 /*!
1743  * \brief SS7 answer channel.
1744  * \since 1.8
1745  *
1746  * \param p Signaling private structure pointer.
1747  * \param chan Asterisk channel structure pointer.
1748  * \param condition AST control frame subtype.
1749  * \param data AST control frame payload contents.
1750  * \param datalen Length of payload contents.
1751  *
1752  * \retval 0 on success.
1753  * \retval -1 on error or indication condition not handled.
1754  */
1755 int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
1756 {
1757         int res = -1;
1758
1759         switch (condition) {
1760         case AST_CONTROL_BUSY:
1761                 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1762                         ast_channel_hangupcause_set(chan, AST_CAUSE_USER_BUSY);
1763                         ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
1764                         res = 0;
1765                         break;
1766                 }
1767                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
1768                 break;
1769         case AST_CONTROL_RINGING:
1770                 ss7_grab(p, p->ss7);
1771                 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
1772                         p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
1773                         if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
1774                                 p->rlt = 1;
1775                         }
1776
1777                         /* No need to send CPG if call will be RELEASE */
1778                         if (p->rlt != 1) {
1779                                 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
1780                         }
1781                 }
1782                 ss7_rel(p->ss7);
1783
1784                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
1785
1786                 if (ast_channel_state(chan) != AST_STATE_UP && ast_channel_state(chan) != AST_STATE_RING) {
1787                         ast_setstate(chan, AST_STATE_RINGING);
1788                 }
1789                 break;
1790         case AST_CONTROL_PROCEEDING:
1791                 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",ast_channel_name(chan));
1792                 ss7_grab(p, p->ss7);
1793                 /* This IF sends the FAR for an answered ALEG call */
1794                 if (ast_channel_state(chan) == AST_STATE_UP && (p->rlt != 1)){
1795                         if ((isup_far(p->ss7->ss7, p->ss7call)) != -1) {
1796                                 p->rlt = 1;
1797                         }
1798                 }
1799
1800                 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING && !p->outgoing) {
1801                         p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
1802                         isup_acm(p->ss7->ss7, p->ss7call);
1803                 }
1804                 ss7_rel(p->ss7);
1805                 /* don't continue in ast_indicate */
1806                 res = 0;
1807                 break;
1808         case AST_CONTROL_PROGRESS:
1809                 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",ast_channel_name(chan));
1810                 ss7_grab(p, p->ss7);
1811                 if (!p->progress && p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
1812                         p->progress = 1;/* No need to send inband-information progress again. */
1813                         isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
1814                         ss7_rel(p->ss7);
1815
1816                         /* enable echo canceler here on SS7 calls */
1817                         sig_ss7_set_echocanceller(p, 1);
1818                 } else {
1819                         ss7_rel(p->ss7);
1820                 }
1821                 /* don't continue in ast_indicate */
1822                 res = 0;
1823                 break;
1824         case AST_CONTROL_INCOMPLETE:
1825                 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1826                         ast_channel_hangupcause_set(chan, AST_CAUSE_INVALID_NUMBER_FORMAT);
1827                         ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
1828                         res = 0;
1829                         break;
1830                 }
1831                 /* Wait for DTMF digits to complete the dialed number. */
1832                 res = 0;
1833                 break;
1834         case AST_CONTROL_CONGESTION:
1835                 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1836                         ast_channel_hangupcause_set(chan, AST_CAUSE_CONGESTION);
1837                         ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
1838                         res = 0;
1839                         break;
1840                 }
1841                 res = sig_ss7_play_tone(p, SIG_SS7_TONE_CONGESTION);
1842                 break;
1843         case AST_CONTROL_HOLD:
1844                 ast_moh_start(chan, data, p->mohinterpret);
1845                 break;
1846         case AST_CONTROL_UNHOLD:
1847                 ast_moh_stop(chan);
1848                 break;
1849         case AST_CONTROL_SRCUPDATE:
1850                 res = 0;
1851                 break;
1852         case -1:
1853                 res = sig_ss7_play_tone(p, -1);
1854                 break;
1855         }
1856         return res;
1857 }
1858
1859 /*!
1860  * \brief SS7 channel request.
1861  * \since 1.8
1862  *
1863  * \param p Signaling private structure pointer.
1864  * \param law Companding law preferred
1865  * \param requestor Asterisk channel requesting a channel to dial (Can be NULL)
1866  * \param transfercapability
1867  *
1868  * \retval ast_channel on success.
1869  * \retval NULL on error.
1870  */
1871 struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
1872 {
1873         struct ast_channel *ast;
1874
1875         sig_ss7_set_outgoing(p, 1);
1876         ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
1877         if (!ast) {
1878                 sig_ss7_set_outgoing(p, 0);
1879
1880                 /* Release the allocated channel.  Only have to deal with the linkset lock. */
1881                 ast_mutex_lock(&p->ss7->lock);
1882                 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1883                 ast_mutex_unlock(&p->ss7->lock);
1884         }
1885         return ast;
1886 }
1887
1888 /*!
1889  * \brief Delete the sig_ss7 private channel structure.
1890  * \since 1.8
1891  *
1892  * \param doomed sig_ss7 private channel structure to delete.
1893  *
1894  * \return Nothing
1895  */
1896 void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
1897 {
1898         ast_free(doomed);
1899 }
1900
1901 #define SIG_SS7_SC_HEADER       "%-4s %4s %-4s %-3s %-3s %-10s %-4s %s\n"
1902 #define SIG_SS7_SC_LINE          "%4d %4d %-4s %-3s %-3s %-10s %-4s %s"
1903 void sig_ss7_cli_show_channels_header(int fd)
1904 {
1905         ast_cli(fd, SIG_SS7_SC_HEADER, "link", "",     "Chan", "Lcl", "Rem", "Call",  "SS7",  "Channel");
1906         ast_cli(fd, SIG_SS7_SC_HEADER, "set",  "Chan", "Idle", "Blk", "Blk", "Level", "Call", "Name");
1907 }
1908
1909 void sig_ss7_cli_show_channels(int fd, struct sig_ss7_linkset *linkset)
1910 {
1911         char line[256];
1912         int idx;
1913         struct sig_ss7_chan *pvt;
1914
1915         ast_mutex_lock(&linkset->lock);
1916         for (idx = 0; idx < linkset->numchans; ++idx) {
1917                 if (!linkset->pvts[idx]) {
1918                         continue;
1919                 }
1920                 pvt = linkset->pvts[idx];
1921                 sig_ss7_lock_private(pvt);
1922                 sig_ss7_lock_owner(linkset, idx);
1923
1924                 snprintf(line, sizeof(line), SIG_SS7_SC_LINE,
1925                         linkset->span,
1926                         pvt->channel,
1927                         sig_ss7_is_chan_available(pvt) ? "Yes" : "No",
1928                         pvt->locallyblocked ? "Yes" : "No",
1929                         pvt->remotelyblocked ? "Yes" : "No",
1930                         sig_ss7_call_level2str(pvt->call_level),
1931                         pvt->ss7call ? "Yes" : "No",
1932                         pvt->owner ? ast_channel_name(pvt->owner) : "");
1933
1934                 if (pvt->owner) {
1935                         ast_channel_unlock(pvt->owner);
1936                 }
1937                 sig_ss7_unlock_private(pvt);
1938
1939                 ast_mutex_unlock(&linkset->lock);
1940                 ast_cli(fd, "%s\n", line);
1941                 ast_mutex_lock(&linkset->lock);
1942         }
1943         ast_mutex_unlock(&linkset->lock);
1944 }
1945
1946 /*!
1947  * \brief Create a new sig_ss7 private channel structure.
1948  * \since 1.8
1949  *
1950  * \param pvt_data Upper layer private data structure.
1951  * \param ss7 Controlling linkset for the channel.
1952  *
1953  * \retval sig_ss7_chan on success.
1954  * \retval NULL on error.
1955  */
1956 struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_linkset *ss7)
1957 {
1958         struct sig_ss7_chan *pvt;
1959
1960         pvt = ast_calloc(1, sizeof(*pvt));
1961         if (!pvt) {
1962                 return pvt;
1963         }
1964
1965         pvt->chan_pvt = pvt_data;
1966         pvt->ss7 = ss7;
1967
1968         return pvt;
1969 }
1970
1971 /*!
1972  * \brief Initialize the SS7 linkset control.
1973  * \since 1.8
1974  *
1975  * \param ss7 SS7 linkset control structure.
1976  *
1977  * \return Nothing
1978  */
1979 void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
1980 {
1981         int idx;
1982
1983         memset(ss7, 0, sizeof(*ss7));
1984
1985         ast_mutex_init(&ss7->lock);
1986
1987         ss7->master = AST_PTHREADT_NULL;
1988         for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
1989                 ss7->fds[idx] = -1;
1990         }
1991 }
1992
1993 /* ------------------------------------------------------------------- */
1994
1995 #endif  /* defined(HAVE_SS7) */
1996 /* end sig_ss7.c */