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