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