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