1 /*
2 * libpri: An implementation of Primary Rate ISDN
3 *
4 * Written by Mark Spencer <markster@digium.com>
5 *
6 * Copyright (C) 2001-2005, Digium, Inc.
7 * All Rights Reserved.
8 */
9
10 /*
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
21 *
22 * In addition, when this program is distributed with Asterisk in
23 * any form that would qualify as a 'combined work' or as a
24 * 'derivative work' (but not mere aggregation), you can redistribute
25 * and/or modify the combination under the terms of the license
26 * provided with that copy of Asterisk, instead of the license
27 * terms granted here.
28 */
29
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <sys/select.h>
38 #include <stdarg.h>
39 #include "compat.h"
40 #include "libpri.h"
41 #include "pri_internal.h"
42 #include "pri_facility.h"
43
44 #define PRI_BIT(a_bit) (1UL << (a_bit))
45 #define PRI_ALL_SWITCHES 0xFFFFFFFF
46 #define PRI_ETSI_SWITCHES (PRI_BIT(PRI_SWITCH_EUROISDN_E1) | PRI_BIT(PRI_SWITCH_EUROISDN_T1))
47
48 struct pri_timer_table {
49 const char *name;
50 enum PRI_TIMERS_AND_COUNTERS number;
51 unsigned long used_by;
52 };
53
54 /*!
55 * \note Sort the timer table entries in the order of the timer name so
56 * pri_dump_info_str() can display them in a consistent order.
57 */
58 static const struct pri_timer_table pri_timer[] = {
59 /* *INDENT-OFF* */
60 /* timer name timer number used by switches */
61 { "N200", PRI_TIMER_N200, PRI_ALL_SWITCHES },
62 { "N201", PRI_TIMER_N201, PRI_ALL_SWITCHES },
63 { "N202", PRI_TIMER_N202, PRI_ALL_SWITCHES },
64 { "K", PRI_TIMER_K, PRI_ALL_SWITCHES },
65 { "T200", PRI_TIMER_T200, PRI_ALL_SWITCHES },
66 { "T201", PRI_TIMER_T201, PRI_ALL_SWITCHES },
67 { "T202", PRI_TIMER_T202, PRI_ALL_SWITCHES },
68 { "T203", PRI_TIMER_T203, PRI_ALL_SWITCHES },
69 { "T300", PRI_TIMER_T300, PRI_ALL_SWITCHES },
70 { "T301", PRI_TIMER_T301, PRI_ALL_SWITCHES },
71 { "T302", PRI_TIMER_T302, PRI_ALL_SWITCHES },
72 { "T303", PRI_TIMER_T303, PRI_ALL_SWITCHES },
73 { "T304", PRI_TIMER_T304, PRI_ALL_SWITCHES },
74 { "T305", PRI_TIMER_T305, PRI_ALL_SWITCHES },
75 { "T306", PRI_TIMER_T306, PRI_ALL_SWITCHES },
76 { "T307", PRI_TIMER_T307, PRI_ALL_SWITCHES },
77 { "T308", PRI_TIMER_T308, PRI_ALL_SWITCHES },
78 { "T309", PRI_TIMER_T309, PRI_ALL_SWITCHES },
79 { "T310", PRI_TIMER_T310, PRI_ALL_SWITCHES },
80 { "T312", PRI_TIMER_T312, PRI_ALL_SWITCHES },
81 { "T313", PRI_TIMER_T313, PRI_ALL_SWITCHES },
82 { "T314", PRI_TIMER_T314, PRI_ALL_SWITCHES },
83 { "T316", PRI_TIMER_T316, PRI_ALL_SWITCHES },
84 { "N316", PRI_TIMER_N316, PRI_ALL_SWITCHES },
85 { "T317", PRI_TIMER_T317, PRI_ALL_SWITCHES },
86 { "T318", PRI_TIMER_T318, PRI_ALL_SWITCHES },
87 { "T319", PRI_TIMER_T319, PRI_ALL_SWITCHES },
88 { "T320", PRI_TIMER_T320, PRI_ALL_SWITCHES },
89 { "T321", PRI_TIMER_T321, PRI_ALL_SWITCHES },
90 { "T322", PRI_TIMER_T322, PRI_ALL_SWITCHES },
91 { "T-HOLD", PRI_TIMER_T_HOLD, PRI_ALL_SWITCHES },
92 { "T-RETRIEVE", PRI_TIMER_T_RETRIEVE, PRI_ALL_SWITCHES },
93 { "T-RESPONSE", PRI_TIMER_T_RESPONSE, PRI_ALL_SWITCHES },
94 { "T-STATUS", PRI_TIMER_T_STATUS, PRI_ETSI_SWITCHES },
95 { "T-ACTIVATE", PRI_TIMER_T_ACTIVATE, PRI_ETSI_SWITCHES },
96 { "T-DEACTIVATE", PRI_TIMER_T_DEACTIVATE, PRI_ETSI_SWITCHES },
97 { "T-INTERROGATE", PRI_TIMER_T_INTERROGATE, PRI_ETSI_SWITCHES },
98 { "T-RETENTION", PRI_TIMER_T_RETENTION, PRI_ETSI_SWITCHES | PRI_BIT(PRI_SWITCH_QSIG) },
99 { "T-CCBS1", PRI_TIMER_T_CCBS1, PRI_ETSI_SWITCHES },
100 { "T-CCBS2", PRI_TIMER_T_CCBS2, PRI_ETSI_SWITCHES },
101 { "T-CCBS3", PRI_TIMER_T_CCBS3, PRI_ETSI_SWITCHES },
102 { "T-CCBS4", PRI_TIMER_T_CCBS4, PRI_ETSI_SWITCHES },
103 { "T-CCBS5", PRI_TIMER_T_CCBS5, PRI_ETSI_SWITCHES },
104 { "T-CCBS6", PRI_TIMER_T_CCBS6, PRI_ETSI_SWITCHES },
105 { "T-CCNR2", PRI_TIMER_T_CCNR2, PRI_ETSI_SWITCHES },
106 { "T-CCNR5", PRI_TIMER_T_CCNR5, PRI_ETSI_SWITCHES },
107 { "T-CCNR6", PRI_TIMER_T_CCNR6, PRI_ETSI_SWITCHES },
108 { "CC-T1", PRI_TIMER_QSIG_CC_T1, PRI_BIT(PRI_SWITCH_QSIG) },
109 { "CCBS-T2", PRI_TIMER_QSIG_CCBS_T2, PRI_BIT(PRI_SWITCH_QSIG) },
110 { "CCNR-T2", PRI_TIMER_QSIG_CCNR_T2, PRI_BIT(PRI_SWITCH_QSIG) },
111 { "CC-T3", PRI_TIMER_QSIG_CC_T3, PRI_BIT(PRI_SWITCH_QSIG) },
112 #if defined(QSIG_PATH_RESERVATION_SUPPORT)
113 { "CC-T4", PRI_TIMER_QSIG_CC_T4, PRI_BIT(PRI_SWITCH_QSIG) },
114 #endif /* defined(QSIG_PATH_RESERVATION_SUPPORT) */
115 /* *INDENT-ON* */
116 };
117
pri_node2str(int node)118 char *pri_node2str(int node)
119 {
120 switch(node) {
121 case PRI_UNKNOWN:
122 return "Unknown node type";
123 case PRI_NETWORK:
124 return "Network";
125 case PRI_CPE:
126 return "CPE";
127 default:
128 return "Invalid value";
129 }
130 }
131
pri_switch2str(int sw)132 char *pri_switch2str(int sw)
133 {
134 switch(sw) {
135 case PRI_SWITCH_NI2:
136 return "National ISDN";
137 case PRI_SWITCH_DMS100:
138 return "Nortel DMS100";
139 case PRI_SWITCH_LUCENT5E:
140 return "Lucent 5E";
141 case PRI_SWITCH_ATT4ESS:
142 return "AT&T 4ESS";
143 case PRI_SWITCH_NI1:
144 return "National ISDN 1";
145 case PRI_SWITCH_EUROISDN_E1:
146 return "EuroISDN";
147 case PRI_SWITCH_GR303_EOC:
148 return "GR303 EOC";
149 case PRI_SWITCH_GR303_TMC:
150 return "GR303 TMC";
151 case PRI_SWITCH_QSIG:
152 return "Q.SIG switch";
153 default:
154 return "Unknown switchtype";
155 }
156 }
157
pri_default_timers(struct pri * ctrl,int switchtype)158 static void pri_default_timers(struct pri *ctrl, int switchtype)
159 {
160 unsigned idx;
161
162 /* Initialize all timers/counters to unsupported/disabled. */
163 for (idx = 0; idx < PRI_MAX_TIMERS; ++idx) {
164 ctrl->timers[idx] = -1;
165 }
166
167 /* Set timer values to standard defaults. Time is in ms. */
168 ctrl->timers[PRI_TIMER_N200] = 3; /* Max numer of Q.921 retransmissions */
169 ctrl->timers[PRI_TIMER_N202] = 3; /* Max numer of transmissions of the TEI identity request message */
170
171 if (ctrl->bri) {
172 ctrl->timers[PRI_TIMER_K] = 1; /* Max number of outstanding I-frames */
173 } else {
174 ctrl->timers[PRI_TIMER_K] = 7; /* Max number of outstanding I-frames */
175 }
176
177 ctrl->timers[PRI_TIMER_T200] = 1000; /* Time between SABME's */
178 ctrl->timers[PRI_TIMER_T201] = ctrl->timers[PRI_TIMER_T200];/* Time between TEI Identity Checks (Default same as T200) */
179 ctrl->timers[PRI_TIMER_T202] = 2 * 1000; /* Min time between transmission of TEI Identity request messages */
180 ctrl->timers[PRI_TIMER_T203] = 10 * 1000; /* Max time without exchanging packets */
181
182 ctrl->timers[PRI_TIMER_T303] = 4 * 1000; /* Length between SETUP retransmissions and timeout */
183 ctrl->timers[PRI_TIMER_T305] = 30 * 1000; /* Wait for DISCONNECT acknowledge */
184 ctrl->timers[PRI_TIMER_T308] = 4 * 1000; /* Wait for RELEASE acknowledge */
185 ctrl->timers[PRI_TIMER_T309] = 6 * 1000; /* Time to wait before clearing calls in case of D-channel transient event. Q.931 specifies 6-90 seconds */
186 ctrl->timers[PRI_TIMER_T312] = (4 + 2) * 1000;/* Supervise broadcast SETUP message call reference retention. T303 + 2 seconds */
187 ctrl->timers[PRI_TIMER_T313] = 4 * 1000; /* Wait for CONNECT acknowledge, CPE side only */
188 #if 0 /* Default disable the T316 timer otherwise the user cannot disable it. */
189 ctrl->timers[PRI_TIMER_T316] = 2 * 60 * 1000; /* RESTART retransmit timer */
190 #endif
191 ctrl->timers[PRI_TIMER_N316] = 2; /* Send RESTART this many times before giving up. */
192
193 ctrl->timers[PRI_TIMER_TM20] = 2500; /* Max time awaiting XID response - Q.921 Appendix IV */
194 ctrl->timers[PRI_TIMER_NM20] = 3; /* Number of XID retransmits - Q.921 Appendix IV */
195
196 ctrl->timers[PRI_TIMER_T_HOLD] = 4 * 1000; /* Wait for HOLD request response. */
197 ctrl->timers[PRI_TIMER_T_RETRIEVE] = 4 * 1000;/* Wait for RETRIEVE request response. */
198
199 ctrl->timers[PRI_TIMER_T_RESPONSE] = 4 * 1000; /* Maximum time to wait for a typical APDU response. */
200
201 /* ETSI timers */
202 ctrl->timers[PRI_TIMER_T_STATUS] = 4 * 1000; /* Max time to wait for all replies to check for compatible terminals */
203 ctrl->timers[PRI_TIMER_T_ACTIVATE] = 10 * 1000; /* Request supervision timeout. */
204 ctrl->timers[PRI_TIMER_T_DEACTIVATE] = 4 * 1000;/* Deactivate supervision timeout. */
205 ctrl->timers[PRI_TIMER_T_INTERROGATE] = 4 * 1000;/* Interrogation supervision timeout. */
206
207 /* ETSI call-completion timers */
208 ctrl->timers[PRI_TIMER_T_RETENTION] = 30 * 1000;/* Max time to wait for user A to activate call-completion. */
209 ctrl->timers[PRI_TIMER_T_CCBS1] = 4 * 1000; /* T-STATUS timer equivalent for CC user A status. */
210 ctrl->timers[PRI_TIMER_T_CCBS2] = 45 * 60 * 1000;/* Max time the CCBS service will be active */
211 ctrl->timers[PRI_TIMER_T_CCBS3] = 20 * 1000; /* Max time to wait for user A to respond to user B availability. */
212 ctrl->timers[PRI_TIMER_T_CCBS4] = 5 * 1000; /* CC user B guard time before sending CC recall indication. */
213 ctrl->timers[PRI_TIMER_T_CCBS5] = 60 * 60 * 1000;/* Network B CCBS supervision timeout. */
214 ctrl->timers[PRI_TIMER_T_CCBS6] = 60 * 60 * 1000;/* Network A CCBS supervision timeout. */
215 ctrl->timers[PRI_TIMER_T_CCNR2] = 180 * 60 * 1000;/* Max time the CCNR service will be active */
216 ctrl->timers[PRI_TIMER_T_CCNR5] = 195 * 60 * 1000;/* Network B CCNR supervision timeout. */
217 ctrl->timers[PRI_TIMER_T_CCNR6] = 195 * 60 * 1000;/* Network A CCNR supervision timeout. */
218
219 /* Q.SIG call-completion timers */
220 ctrl->timers[PRI_TIMER_QSIG_CC_T1] = 30 * 1000;/* CC request supervision timeout. */
221 ctrl->timers[PRI_TIMER_QSIG_CCBS_T2] = 60 * 60 * 1000;/* CCBS supervision timeout. */
222 ctrl->timers[PRI_TIMER_QSIG_CCNR_T2] = 195 * 60 * 1000;/* CCNR supervision timeout. */
223 ctrl->timers[PRI_TIMER_QSIG_CC_T3] = 30 * 1000;/* Max time to wait for user A to respond to user B availability. */
224 #if defined(QSIG_PATH_RESERVATION_SUPPORT)
225 ctrl->timers[PRI_TIMER_QSIG_CC_T4] = 40 * 1000;/* Path reservation supervision timeout. */
226 #endif /* defined(QSIG_PATH_RESERVATION_SUPPORT) */
227
228 /* Set any switch specific override default values */
229 switch (switchtype) {
230 default:
231 break;
232 }
233 }
234
pri_set_timer(struct pri * ctrl,int timer,int value)235 int pri_set_timer(struct pri *ctrl, int timer, int value)
236 {
237 if (!ctrl || timer < 0 || PRI_MAX_TIMERS <= timer || value < 0) {
238 return -1;
239 }
240 ctrl->timers[timer] = value;
241 return 0;
242 }
243
pri_get_timer(struct pri * ctrl,int timer)244 int pri_get_timer(struct pri *ctrl, int timer)
245 {
246 if (!ctrl || timer < 0 || PRI_MAX_TIMERS <= timer) {
247 return -1;
248 }
249 return ctrl->timers[timer];
250 }
251
pri_set_service_message_support(struct pri * pri,int supportflag)252 int pri_set_service_message_support(struct pri *pri, int supportflag)
253 {
254 if (!pri) {
255 return -1;
256 }
257 pri->service_message_support = supportflag ? 1 : 0;
258 return 0;
259 }
260
pri_timer2idx(const char * timer_name)261 int pri_timer2idx(const char *timer_name)
262 {
263 unsigned idx;
264 enum PRI_TIMERS_AND_COUNTERS timer_number;
265
266 timer_number = -1;
267 for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
268 if (!strcasecmp(timer_name, pri_timer[idx].name)) {
269 timer_number = pri_timer[idx].number;
270 break;
271 }
272 }
273 return timer_number;
274 }
275
__pri_read(struct pri * pri,void * buf,int buflen)276 static int __pri_read(struct pri *pri, void *buf, int buflen)
277 {
278 int res = read(pri->fd, buf, buflen);
279 if (res < 0) {
280 if (errno != EAGAIN)
281 pri_error(pri, "Read on %d failed: %s\n", pri->fd, strerror(errno));
282 return 0;
283 }
284 return res;
285 }
286
__pri_write(struct pri * pri,void * buf,int buflen)287 static int __pri_write(struct pri *pri, void *buf, int buflen)
288 {
289 int res = write(pri->fd, buf, buflen);
290 if (res < 0) {
291 if (errno != EAGAIN)
292 pri_error(pri, "Write to %d failed: %s\n", pri->fd, strerror(errno));
293 return 0;
294 }
295 return res;
296 }
297
298 /*!
299 * \internal
300 * \brief Determine the default layer 2 persistence option.
301 *
302 * \param ctrl D channel controller.
303 *
304 * \return Default layer 2 persistence option. (legacy behaviour default)
305 */
pri_l2_persistence_option_default(struct pri * ctrl)306 static enum pri_layer2_persistence pri_l2_persistence_option_default(struct pri *ctrl)
307 {
308 enum pri_layer2_persistence persistence;
309
310 if (PTMP_MODE(ctrl)) {
311 persistence = PRI_L2_PERSISTENCE_LEAVE_DOWN;
312 } else {
313 persistence = PRI_L2_PERSISTENCE_KEEP_UP;
314 }
315
316 return persistence;
317 }
318
319 /*!
320 * \internal
321 * \brief Determine the default display text send options.
322 *
323 * \param ctrl D channel controller.
324 *
325 * \return Default display text send options. (legacy behaviour defaults)
326 */
pri_display_options_send_default(struct pri * ctrl)327 static unsigned long pri_display_options_send_default(struct pri *ctrl)
328 {
329 unsigned long flags;
330
331 switch (ctrl->switchtype) {
332 case PRI_SWITCH_QSIG:
333 flags = PRI_DISPLAY_OPTION_BLOCK;
334 break;
335 case PRI_SWITCH_EUROISDN_E1:
336 case PRI_SWITCH_EUROISDN_T1:
337 if (ctrl->localtype == PRI_CPE) {
338 flags = PRI_DISPLAY_OPTION_BLOCK;
339 break;
340 }
341 flags = PRI_DISPLAY_OPTION_NAME_INITIAL;
342 break;
343 default:
344 flags = PRI_DISPLAY_OPTION_NAME_INITIAL;
345 break;
346 }
347 return flags;
348 }
349
350 /*!
351 * \internal
352 * \brief Determine the default display text receive options.
353 *
354 * \param ctrl D channel controller.
355 *
356 * \return Default display text receive options. (legacy behaviour defaults)
357 */
pri_display_options_receive_default(struct pri * ctrl)358 static unsigned long pri_display_options_receive_default(struct pri *ctrl)
359 {
360 unsigned long flags;
361
362 switch (ctrl->switchtype) {
363 case PRI_SWITCH_QSIG:
364 flags = PRI_DISPLAY_OPTION_BLOCK;
365 break;
366 default:
367 flags = PRI_DISPLAY_OPTION_NAME_INITIAL;
368 break;
369 }
370 return flags;
371 }
372
373 /*!
374 * \internal
375 * \brief Determine the default date/time send option default.
376 *
377 * \param ctrl D channel controller.
378 *
379 * \return Default date/time send option.
380 */
pri_date_time_send_default(struct pri * ctrl)381 static int pri_date_time_send_default(struct pri *ctrl)
382 {
383 int date_time_send;
384
385 if (BRI_NT_PTMP(ctrl)) {
386 date_time_send = PRI_DATE_TIME_SEND_DATE_HHMM;
387 } else {
388 date_time_send = PRI_DATE_TIME_SEND_NO;
389 }
390
391 return date_time_send;
392 }
393
394 /*!
395 * \brief Destroy the given link.
396 *
397 * \param link Q.921 link to destroy.
398 *
399 * \return Nothing
400 */
pri_link_destroy(struct q921_link * link)401 void pri_link_destroy(struct q921_link *link)
402 {
403 if (link) {
404 struct q931_call *call;
405
406 call = link->dummy_call;
407 if (call) {
408 pri_schedule_del(call->pri, call->retranstimer);
409 call->retranstimer = 0;
410 pri_call_apdu_queue_cleanup(call);
411 }
412 free(link);
413 }
414 }
415
416 /*!
417 * \internal
418 * \brief Initialize the layer 2 link structure.
419 *
420 * \param ctrl D channel controller.
421 * \param link Q.921 link to initialize.
422 * \param sapi SAPI new link is to use.
423 * \param tei TEI new link is to use.
424 *
425 * \note It is assumed that the link has already been memset to zero.
426 *
427 * \return Nothing
428 */
pri_link_init(struct pri * ctrl,struct q921_link * link,int sapi,int tei)429 static void pri_link_init(struct pri *ctrl, struct q921_link *link, int sapi, int tei)
430 {
431 link->ctrl = ctrl;
432 link->sapi = sapi;
433 link->tei = tei;
434 }
435
436 /*!
437 * \brief Create a new layer 2 link.
438 *
439 * \param ctrl D channel controller.
440 * \param sapi SAPI new link is to use.
441 * \param tei TEI new link is to use.
442 *
443 * \retval link on success.
444 * \retval NULL on error.
445 */
pri_link_new(struct pri * ctrl,int sapi,int tei)446 struct q921_link *pri_link_new(struct pri *ctrl, int sapi, int tei)
447 {
448 struct link_dummy *dummy_link;
449 struct q921_link *link;
450
451 switch (ctrl->switchtype) {
452 case PRI_SWITCH_GR303_EOC:
453 case PRI_SWITCH_GR303_TMC:
454 link = calloc(1, sizeof(*link));
455 if (!link) {
456 return NULL;
457 }
458 dummy_link = NULL;
459 break;
460 default:
461 dummy_link = calloc(1, sizeof(*dummy_link));
462 if (!dummy_link) {
463 return NULL;
464 }
465 link = &dummy_link->link;
466 break;
467 }
468
469 pri_link_init(ctrl, link, sapi, tei);
470 if (dummy_link) {
471 /* Initialize the dummy call reference call record. */
472 link->dummy_call = &dummy_link->dummy_call;
473 q931_init_call_record(link, link->dummy_call, Q931_DUMMY_CALL_REFERENCE);
474 }
475
476 q921_start(link);
477
478 return link;
479 }
480
481 /*!
482 * \internal
483 * \brief Destroy the given D channel controller.
484 *
485 * \param ctrl D channel control to destroy.
486 *
487 * \return Nothing
488 */
pri_ctrl_destroy(struct pri * ctrl)489 static void pri_ctrl_destroy(struct pri *ctrl)
490 {
491 if (ctrl) {
492 struct q931_call *call;
493
494 if (ctrl->link.tei == Q921_TEI_GROUP
495 && ctrl->link.sapi == Q921_SAPI_LAYER2_MANAGEMENT
496 && ctrl->localtype == PRI_CPE) {
497 /* This dummy call was borrowed from the specific TEI link. */
498 call = NULL;
499 } else {
500 call = ctrl->link.dummy_call;
501 }
502 if (call) {
503 pri_schedule_del(call->pri, call->retranstimer);
504 call->retranstimer = 0;
505 pri_call_apdu_queue_cleanup(call);
506 }
507 free(ctrl->msg_line);
508 free(ctrl->sched.timer);
509 free(ctrl);
510 }
511 }
512
513 /*!
514 * \internal
515 * \brief Create a new D channel control structure.
516 *
517 * \param fd D channel file descriptor if no callback functions supplied.
518 * \param node Switch NET/CPE type
519 * \param switchtype ISDN switch type
520 * \param rd D channel read callback function
521 * \param wr D channel write callback function
522 * \param userdata Callback function parameter
523 * \param tei TEI new link is to use.
524 * \param bri TRUE if interface is BRI
525 *
526 * \retval ctrl on success.
527 * \retval NULL on error.
528 */
pri_ctrl_new(int fd,int node,int switchtype,pri_io_cb rd,pri_io_cb wr,void * userdata,int tei,int bri)529 static struct pri *pri_ctrl_new(int fd, int node, int switchtype, pri_io_cb rd, pri_io_cb wr, void *userdata, int tei, int bri)
530 {
531 int create_dummy_call;
532 struct d_ctrl_dummy *dummy_ctrl;
533 struct pri *ctrl;
534
535 switch (switchtype) {
536 case PRI_SWITCH_GR303_EOC:
537 case PRI_SWITCH_GR303_TMC:
538 create_dummy_call = 0;
539 break;
540 default:
541 if (bri && node == PRI_CPE && tei == Q921_TEI_GROUP) {
542 /*
543 * BRI TE PTMP will not use its own group dummy call record. It
544 * will use the specific TEI dummy call instead.
545 */
546 create_dummy_call = 0;
547 } else {
548 create_dummy_call = 1;
549 }
550 break;
551 }
552 if (create_dummy_call) {
553 dummy_ctrl = calloc(1, sizeof(*dummy_ctrl));
554 if (!dummy_ctrl) {
555 return NULL;
556 }
557 ctrl = &dummy_ctrl->ctrl;
558 } else {
559 ctrl = calloc(1, sizeof(*ctrl));
560 if (!ctrl) {
561 return NULL;
562 }
563 dummy_ctrl = NULL;
564 }
565 ctrl->msg_line = calloc(1, sizeof(*ctrl->msg_line));
566 if (!ctrl->msg_line) {
567 free(ctrl);
568 return NULL;
569 }
570
571 ctrl->bri = bri;
572 ctrl->fd = fd;
573 ctrl->read_func = rd;
574 ctrl->write_func = wr;
575 ctrl->userdata = userdata;
576 ctrl->localtype = node;
577 ctrl->switchtype = switchtype;
578 ctrl->cref = 1;
579 ctrl->nsf = PRI_NSF_NONE;
580 ctrl->callpool = &ctrl->localpool;
581 pri_default_timers(ctrl, switchtype);
582 ctrl->q921_rxcount = 0;
583 ctrl->q921_txcount = 0;
584 ctrl->q931_rxcount = 0;
585 ctrl->q931_txcount = 0;
586
587 ctrl->l2_persistence = pri_l2_persistence_option_default(ctrl);
588 ctrl->display_flags.send = pri_display_options_send_default(ctrl);
589 ctrl->display_flags.receive = pri_display_options_receive_default(ctrl);
590 switch (switchtype) {
591 case PRI_SWITCH_GR303_EOC:
592 ctrl->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
593 pri_link_init(ctrl, &ctrl->link, Q921_SAPI_GR303_EOC, Q921_TEI_GR303_EOC_OPS);
594 ctrl->link.next = pri_link_new(ctrl, Q921_SAPI_GR303_EOC, Q921_TEI_GR303_EOC_PATH);
595 if (!ctrl->link.next) {
596 pri_ctrl_destroy(ctrl);
597 return NULL;
598 }
599 break;
600 case PRI_SWITCH_GR303_TMC:
601 ctrl->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
602 pri_link_init(ctrl, &ctrl->link, Q921_SAPI_GR303_TMC_CALLPROC, Q921_TEI_GR303_TMC_CALLPROC);
603 ctrl->link.next = pri_link_new(ctrl, Q921_SAPI_GR303_TMC_SWITCHING, Q921_TEI_GR303_TMC_SWITCHING);
604 if (!ctrl->link.next) {
605 pri_ctrl_destroy(ctrl);
606 return NULL;
607 }
608 break;
609 default:
610 ctrl->protodisc = Q931_PROTOCOL_DISCRIMINATOR;
611 pri_link_init(ctrl, &ctrl->link,
612 (tei == Q921_TEI_GROUP) ? Q921_SAPI_LAYER2_MANAGEMENT : Q921_SAPI_CALL_CTRL,
613 tei);
614 break;
615 }
616 ctrl->date_time_send = pri_date_time_send_default(ctrl);
617 if (dummy_ctrl) {
618 /* Initialize the dummy call reference call record. */
619 ctrl->link.dummy_call = &dummy_ctrl->dummy_call;
620 q931_init_call_record(&ctrl->link, ctrl->link.dummy_call,
621 Q931_DUMMY_CALL_REFERENCE);
622 }
623
624 if (ctrl->link.tei == Q921_TEI_GROUP && ctrl->link.sapi == Q921_SAPI_LAYER2_MANAGEMENT
625 && ctrl->localtype == PRI_CPE) {
626 ctrl->link.next = pri_link_new(ctrl, Q921_SAPI_CALL_CTRL, Q921_TEI_PRI);
627 if (!ctrl->link.next) {
628 pri_ctrl_destroy(ctrl);
629 return NULL;
630 }
631 /*
632 * Make the group link use the just created specific TEI link
633 * dummy call instead. It makes no sense for TE PTMP interfaces
634 * to broadcast messages on the dummy call or to broadcast any
635 * messages for that matter.
636 */
637 ctrl->link.dummy_call = ctrl->link.next->dummy_call;
638 } else {
639 q921_start(&ctrl->link);
640 }
641
642 return ctrl;
643 }
644
pri_call_set_useruser(q931_call * c,const char * userchars)645 void pri_call_set_useruser(q931_call *c, const char *userchars)
646 {
647 /*
648 * There is a slight risk here if c is actually stale. However,
649 * if it is stale then it is better to catch it here than to
650 * write with it.
651 */
652 if (!userchars || !pri_is_call_valid(NULL, c)) {
653 return;
654 }
655 libpri_copy_string(c->useruserinfo, userchars, sizeof(c->useruserinfo));
656 }
657
pri_sr_set_useruser(struct pri_sr * sr,const char * userchars)658 void pri_sr_set_useruser(struct pri_sr *sr, const char *userchars)
659 {
660 sr->useruserinfo = userchars;
661 }
662
pri_restart(struct pri * pri)663 int pri_restart(struct pri *pri)
664 {
665 /* pri_restart() is no longer needed since the Q.921 rewrite. */
666 #if 0
667 /* Restart Q.921 layer */
668 if (pri) {
669 q921_reset(pri, 1);
670 q921_start(pri, pri->localtype == PRI_CPE);
671 }
672 #endif
673 return 0;
674 }
675
pri_new(int fd,int nodetype,int switchtype)676 struct pri *pri_new(int fd, int nodetype, int switchtype)
677 {
678 return pri_ctrl_new(fd, nodetype, switchtype, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 0);
679 }
680
pri_new_bri(int fd,int ptpmode,int nodetype,int switchtype)681 struct pri *pri_new_bri(int fd, int ptpmode, int nodetype, int switchtype)
682 {
683 if (ptpmode)
684 return pri_ctrl_new(fd, nodetype, switchtype, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 1);
685 else
686 return pri_ctrl_new(fd, nodetype, switchtype, __pri_read, __pri_write, NULL, Q921_TEI_GROUP, 1);
687 }
688
pri_new_cb(int fd,int nodetype,int switchtype,pri_io_cb io_read,pri_io_cb io_write,void * userdata)689 struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
690 {
691 if (!io_read)
692 io_read = __pri_read;
693 if (!io_write)
694 io_write = __pri_write;
695 return pri_ctrl_new(fd, nodetype, switchtype, io_read, io_write, userdata, Q921_TEI_PRI, 0);
696 }
697
pri_new_bri_cb(int fd,int ptpmode,int nodetype,int switchtype,pri_io_cb io_read,pri_io_cb io_write,void * userdata)698 struct pri *pri_new_bri_cb(int fd, int ptpmode, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
699 {
700 if (!io_read) {
701 io_read = __pri_read;
702 }
703 if (!io_write) {
704 io_write = __pri_write;
705 }
706 if (ptpmode) {
707 return pri_ctrl_new(fd, nodetype, switchtype, io_read, io_write, userdata, Q921_TEI_PRI, 1);
708 } else {
709 return pri_ctrl_new(fd, nodetype, switchtype, io_read, io_write, userdata, Q921_TEI_GROUP, 1);
710 }
711 }
712
pri_get_userdata(struct pri * pri)713 void *pri_get_userdata(struct pri *pri)
714 {
715 return pri ? pri->userdata : NULL;
716 }
717
pri_set_userdata(struct pri * pri,void * userdata)718 void pri_set_userdata(struct pri *pri, void *userdata)
719 {
720 if (pri)
721 pri->userdata = userdata;
722 }
723
pri_set_nsf(struct pri * pri,int nsf)724 void pri_set_nsf(struct pri *pri, int nsf)
725 {
726 if (pri)
727 pri->nsf = nsf;
728 }
729
pri_event2str(int id)730 char *pri_event2str(int id)
731 {
732 unsigned idx;
733 struct {
734 int id;
735 char *name;
736 } events[] = {
737 /* *INDENT-OFF* */
738 { PRI_EVENT_DCHAN_UP, "PRI_EVENT_DCHAN_UP" },
739 { PRI_EVENT_DCHAN_DOWN, "PRI_EVENT_DCHAN_DOWN" },
740 { PRI_EVENT_RESTART, "PRI_EVENT_RESTART" },
741 { PRI_EVENT_CONFIG_ERR, "PRI_EVENT_CONFIG_ERR" },
742 { PRI_EVENT_RING, "PRI_EVENT_RING" },
743 { PRI_EVENT_HANGUP, "PRI_EVENT_HANGUP" },
744 { PRI_EVENT_RINGING, "PRI_EVENT_RINGING" },
745 { PRI_EVENT_ANSWER, "PRI_EVENT_ANSWER" },
746 { PRI_EVENT_HANGUP_ACK, "PRI_EVENT_HANGUP_ACK" },
747 { PRI_EVENT_RESTART_ACK, "PRI_EVENT_RESTART_ACK" },
748 { PRI_EVENT_FACILITY, "PRI_EVENT_FACILITY" },
749 { PRI_EVENT_INFO_RECEIVED, "PRI_EVENT_INFO_RECEIVED" },
750 { PRI_EVENT_PROCEEDING, "PRI_EVENT_PROCEEDING" },
751 { PRI_EVENT_SETUP_ACK, "PRI_EVENT_SETUP_ACK" },
752 { PRI_EVENT_HANGUP_REQ, "PRI_EVENT_HANGUP_REQ" },
753 { PRI_EVENT_NOTIFY, "PRI_EVENT_NOTIFY" },
754 { PRI_EVENT_PROGRESS, "PRI_EVENT_PROGRESS" },
755 { PRI_EVENT_KEYPAD_DIGIT, "PRI_EVENT_KEYPAD_DIGIT" },
756 { PRI_EVENT_SERVICE, "PRI_EVENT_SERVICE" },
757 { PRI_EVENT_SERVICE_ACK, "PRI_EVENT_SERVICE_ACK" },
758 { PRI_EVENT_HOLD, "PRI_EVENT_HOLD" },
759 { PRI_EVENT_HOLD_ACK, "PRI_EVENT_HOLD_ACK" },
760 { PRI_EVENT_HOLD_REJ, "PRI_EVENT_HOLD_REJ" },
761 { PRI_EVENT_RETRIEVE, "PRI_EVENT_RETRIEVE" },
762 { PRI_EVENT_RETRIEVE_ACK, "PRI_EVENT_RETRIEVE_ACK" },
763 { PRI_EVENT_RETRIEVE_REJ, "PRI_EVENT_RETRIEVE_REJ" },
764 { PRI_EVENT_CONNECT_ACK, "PRI_EVENT_CONNECT_ACK" },
765 /* *INDENT-ON* */
766 };
767
768 for (idx = 0; idx < ARRAY_LEN(events); ++idx) {
769 if (events[idx].id == id) {
770 return events[idx].name;
771 }
772 }
773 return "Unknown Event";
774 }
775
pri_check_event(struct pri * pri)776 pri_event *pri_check_event(struct pri *pri)
777 {
778 char buf[1024];
779 int res;
780 pri_event *e;
781 res = pri->read_func ? pri->read_func(pri, buf, sizeof(buf)) : 0;
782 if (!res)
783 return NULL;
784 /* Receive the q921 packet */
785 e = q921_receive(pri, (q921_h *)buf, res);
786 return e;
787 }
788
wait_pri(struct pri * pri)789 static int wait_pri(struct pri *pri)
790 {
791 struct timeval *tv, real;
792 fd_set fds;
793 int res;
794 FD_ZERO(&fds);
795 FD_SET(pri->fd, &fds);
796 tv = pri_schedule_next(pri);
797 if (tv) {
798 gettimeofday(&real, NULL);
799 real.tv_sec = tv->tv_sec - real.tv_sec;
800 real.tv_usec = tv->tv_usec - real.tv_usec;
801 if (real.tv_usec < 0) {
802 real.tv_usec += 1000000;
803 real.tv_sec -= 1;
804 }
805 if (real.tv_sec < 0) {
806 real.tv_sec = 0;
807 real.tv_usec = 0;
808 }
809 }
810 res = select(pri->fd + 1, &fds, NULL, NULL, tv ? &real : tv);
811 if (res < 0)
812 return -1;
813 return res;
814 }
815
pri_mkerror(struct pri * pri,char * errstr)816 pri_event *pri_mkerror(struct pri *pri, char *errstr)
817 {
818 /* Return a configuration error */
819 pri->ev.err.e = PRI_EVENT_CONFIG_ERR;
820 libpri_copy_string(pri->ev.err.err, errstr, sizeof(pri->ev.err.err));
821 return &pri->ev;
822 }
823
824
pri_dchannel_run(struct pri * pri,int block)825 pri_event *pri_dchannel_run(struct pri *pri, int block)
826 {
827 pri_event *e;
828 int res;
829 if (!pri)
830 return NULL;
831 if (block) {
832 do {
833 e = NULL;
834 res = wait_pri(pri);
835 /* Check for error / interruption */
836 if (res < 0)
837 return NULL;
838 if (!res)
839 e = pri_schedule_run(pri);
840 else
841 e = pri_check_event(pri);
842 } while(!e);
843 } else {
844 e = pri_check_event(pri);
845 return e;
846 }
847 return e;
848 }
849
pri_set_debug(struct pri * pri,int debug)850 void pri_set_debug(struct pri *pri, int debug)
851 {
852 if (!pri)
853 return;
854 pri->debug = debug;
855 }
856
pri_get_debug(struct pri * pri)857 int pri_get_debug(struct pri *pri)
858 {
859 if (!pri)
860 return -1;
861 return pri->debug;
862 }
863
pri_facility_enable(struct pri * pri)864 void pri_facility_enable(struct pri *pri)
865 {
866 if (!pri)
867 return;
868 pri->sendfacility = 1;
869 }
870
pri_acknowledge(struct pri * pri,q931_call * call,int channel,int info)871 int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
872 {
873 if (!pri || !pri_is_call_valid(pri, call)) {
874 return -1;
875 }
876 return q931_alerting(pri, call, channel, info);
877 }
878
pri_proceeding(struct pri * pri,q931_call * call,int channel,int info)879 int pri_proceeding(struct pri *pri, q931_call *call, int channel, int info)
880 {
881 if (!pri || !pri_is_call_valid(pri, call)) {
882 return -1;
883 }
884 return q931_call_proceeding(pri, call, channel, info);
885 }
886
pri_progress_with_cause(struct pri * pri,q931_call * call,int channel,int info,int cause)887 int pri_progress_with_cause(struct pri *pri, q931_call *call, int channel, int info, int cause)
888 {
889 if (!pri || !pri_is_call_valid(pri, call)) {
890 return -1;
891 }
892
893 return q931_call_progress_with_cause(pri, call, channel, info, cause);
894 }
895
pri_progress(struct pri * pri,q931_call * call,int channel,int info)896 int pri_progress(struct pri *pri, q931_call *call, int channel, int info)
897 {
898 if (!pri || !pri_is_call_valid(pri, call)) {
899 return -1;
900 }
901
902 return q931_call_progress(pri, call, channel, info);
903 }
904
pri_information(struct pri * pri,q931_call * call,char digit)905 int pri_information(struct pri *pri, q931_call *call, char digit)
906 {
907 if (!pri || !pri_is_call_valid(pri, call)) {
908 return -1;
909 }
910 return q931_information(pri, call, digit);
911 }
912
pri_keypad_facility(struct pri * pri,q931_call * call,const char * digits)913 int pri_keypad_facility(struct pri *pri, q931_call *call, const char *digits)
914 {
915 if (!pri || !pri_is_call_valid(pri, call) || !digits || !digits[0]) {
916 return -1;
917 }
918
919 return q931_keypad_facility(pri, call, digits);
920 }
921
pri_notify(struct pri * pri,q931_call * call,int channel,int info)922 int pri_notify(struct pri *pri, q931_call *call, int channel, int info)
923 {
924 if (!pri || !pri_is_call_valid(pri, call)) {
925 return -1;
926 }
927 return q931_notify(pri, call, channel, info);
928 }
929
pri_destroycall(struct pri * pri,q931_call * call)930 void pri_destroycall(struct pri *pri, q931_call *call)
931 {
932 if (pri && pri_is_call_valid(pri, call)) {
933 q931_destroycall(pri, call);
934 }
935 }
936
pri_need_more_info(struct pri * pri,q931_call * call,int channel,int nonisdn)937 int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn)
938 {
939 if (!pri || !pri_is_call_valid(pri, call)) {
940 return -1;
941 }
942 return q931_setup_ack(pri, call, channel, nonisdn, 0);
943 }
944
pri_setup_ack(struct pri * ctrl,q931_call * call,int channel,int nonisdn,int inband)945 int pri_setup_ack(struct pri *ctrl, q931_call *call, int channel, int nonisdn, int inband)
946 {
947 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
948 return -1;
949 }
950 return q931_setup_ack(ctrl, call, channel, nonisdn, inband);
951 }
952
pri_answer(struct pri * pri,q931_call * call,int channel,int nonisdn)953 int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
954 {
955 if (!pri || !pri_is_call_valid(pri, call)) {
956 return -1;
957 }
958 return q931_connect(pri, call, channel, nonisdn);
959 }
960
pri_connect_ack(struct pri * ctrl,q931_call * call,int channel)961 int pri_connect_ack(struct pri *ctrl, q931_call *call, int channel)
962 {
963 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
964 return -1;
965 }
966 return q931_connect_acknowledge(ctrl, call, channel);
967 }
968
pri_connect_ack_enable(struct pri * ctrl,int enable)969 void pri_connect_ack_enable(struct pri *ctrl, int enable)
970 {
971 if (ctrl) {
972 ctrl->manual_connect_ack = enable ? 1 : 0;
973 }
974 }
975
976 /*!
977 * \brief Copy the PRI party name to the Q.931 party name structure.
978 *
979 * \param q931_name Q.931 party name structure
980 * \param pri_name PRI party name structure
981 *
982 * \return Nothing
983 */
pri_copy_party_name_to_q931(struct q931_party_name * q931_name,const struct pri_party_name * pri_name)984 void pri_copy_party_name_to_q931(struct q931_party_name *q931_name, const struct pri_party_name *pri_name)
985 {
986 q931_party_name_init(q931_name);
987 if (pri_name->valid) {
988 q931_name->valid = 1;
989 q931_name->presentation = pri_name->presentation & PRI_PRES_RESTRICTION;
990 q931_name->char_set = pri_name->char_set;
991 libpri_copy_string(q931_name->str, pri_name->str, sizeof(q931_name->str));
992 }
993 }
994
995 /*!
996 * \brief Copy the PRI party number to the Q.931 party number structure.
997 *
998 * \param q931_number Q.931 party number structure
999 * \param pri_number PRI party number structure
1000 *
1001 * \return Nothing
1002 */
pri_copy_party_number_to_q931(struct q931_party_number * q931_number,const struct pri_party_number * pri_number)1003 void pri_copy_party_number_to_q931(struct q931_party_number *q931_number, const struct pri_party_number *pri_number)
1004 {
1005 q931_party_number_init(q931_number);
1006 if (pri_number->valid) {
1007 q931_number->valid = 1;
1008 q931_number->presentation = pri_number->presentation
1009 & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1010 q931_number->plan = pri_number->plan;
1011 libpri_copy_string(q931_number->str, pri_number->str, sizeof(q931_number->str));
1012 }
1013 }
1014
1015 /*!
1016 * \brief Copy the PRI party subaddress to the Q.931 party subaddress structure.
1017 *
1018 * \param q931_subaddress Q.931 party subaddress structure
1019 * \param pri_subaddress PRI party subaddress structure
1020 *
1021 * \return Nothing
1022 */
pri_copy_party_subaddress_to_q931(struct q931_party_subaddress * q931_subaddress,const struct pri_party_subaddress * pri_subaddress)1023 void pri_copy_party_subaddress_to_q931(struct q931_party_subaddress *q931_subaddress, const struct pri_party_subaddress *pri_subaddress)
1024 {
1025 int length;
1026 int maxlen = sizeof(q931_subaddress->data) - 1;
1027
1028 q931_party_subaddress_init(q931_subaddress);
1029
1030 if (!pri_subaddress->valid) {
1031 return;
1032 }
1033
1034 q931_subaddress->valid = 1;
1035 q931_subaddress->type = pri_subaddress->type;
1036
1037 length = pri_subaddress->length;
1038 if (length > maxlen){
1039 length = maxlen;
1040 } else {
1041 q931_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
1042 }
1043 q931_subaddress->length = length;
1044 memcpy(q931_subaddress->data, pri_subaddress->data, length);
1045 q931_subaddress->data[length] = '\0';
1046 }
1047
1048 /*!
1049 * \brief Copy the PRI party id to the Q.931 party id structure.
1050 *
1051 * \param q931_id Q.931 party id structure
1052 * \param pri_id PRI party id structure
1053 *
1054 * \return Nothing
1055 */
pri_copy_party_id_to_q931(struct q931_party_id * q931_id,const struct pri_party_id * pri_id)1056 void pri_copy_party_id_to_q931(struct q931_party_id *q931_id, const struct pri_party_id *pri_id)
1057 {
1058 pri_copy_party_name_to_q931(&q931_id->name, &pri_id->name);
1059 pri_copy_party_number_to_q931(&q931_id->number, &pri_id->number);
1060 pri_copy_party_subaddress_to_q931(&q931_id->subaddress, &pri_id->subaddress);
1061 }
1062
pri_connected_line_update(struct pri * ctrl,q931_call * call,const struct pri_party_connected_line * connected)1063 int pri_connected_line_update(struct pri *ctrl, q931_call *call, const struct pri_party_connected_line *connected)
1064 {
1065 struct q931_party_id party_id;
1066 unsigned idx;
1067 unsigned new_name;
1068 unsigned new_number;
1069 unsigned new_subaddress;
1070 struct q931_call *subcall;
1071
1072 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
1073 return -1;
1074 }
1075
1076 pri_copy_party_id_to_q931(&party_id, &connected->id);
1077 q931_party_id_fixup(ctrl, &party_id);
1078
1079 new_name = q931_party_name_cmp(&party_id.name, &call->local_id.name);
1080 new_number = q931_party_number_cmp(&party_id.number, &call->local_id.number);
1081 new_subaddress = party_id.subaddress.valid
1082 && q931_party_subaddress_cmp(&party_id.subaddress, &call->local_id.subaddress);
1083
1084 /* Update the call and all subcalls with new local_id. */
1085 call->local_id = party_id;
1086 if (call->outboundbroadcast && call->master_call == call) {
1087 for (idx = 0; idx < ARRAY_LEN(call->subcalls); ++idx) {
1088 subcall = call->subcalls[idx];
1089 if (subcall) {
1090 subcall->local_id = party_id;
1091 }
1092 }
1093 }
1094
1095 switch (call->ourcallstate) {
1096 case Q931_CALL_STATE_CALL_INITIATED:
1097 case Q931_CALL_STATE_OVERLAP_SENDING:
1098 case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
1099 case Q931_CALL_STATE_CALL_DELIVERED:
1100 /*
1101 * The local party transferred to someone else before
1102 * the remote end answered.
1103 */
1104 switch (ctrl->switchtype) {
1105 case PRI_SWITCH_EUROISDN_E1:
1106 case PRI_SWITCH_EUROISDN_T1:
1107 if (BRI_NT_PTMP(ctrl)) {
1108 /*
1109 * NT PTMP mode
1110 *
1111 * We should not send these messages to the network if we are
1112 * the CPE side since phones do not transfer calls within
1113 * themselves. Well... If you consider handing the handset to
1114 * someone else a transfer then how is the network to know?
1115 */
1116 if (new_number) {
1117 q931_notify_redirection(ctrl, call, PRI_NOTIFY_TRANSFER_ACTIVE,
1118 &party_id.name, &party_id.number);
1119 }
1120 if (new_subaddress || (party_id.subaddress.valid && new_number)) {
1121 q931_subaddress_transfer(ctrl, call);
1122 }
1123 } else if (PTP_MODE(ctrl)) {
1124 /* PTP mode */
1125 if (new_number) {
1126 /* Immediately send EctInform APDU, callStatus=answered(0) */
1127 send_call_transfer_complete(ctrl, call, 0);
1128 }
1129 if (new_subaddress || (party_id.subaddress.valid && new_number)) {
1130 q931_subaddress_transfer(ctrl, call);
1131 }
1132 }
1133 break;
1134 case PRI_SWITCH_QSIG:
1135 if (new_name || new_number) {
1136 /* Immediately send CallTransferComplete APDU, callStatus=answered(0) */
1137 send_call_transfer_complete(ctrl, call, 0);
1138 }
1139 if (new_subaddress
1140 || (party_id.subaddress.valid && (new_name || new_number))) {
1141 q931_subaddress_transfer(ctrl, call);
1142 }
1143 break;
1144 default:
1145 break;
1146 }
1147 break;
1148 case Q931_CALL_STATE_ACTIVE:
1149 switch (ctrl->switchtype) {
1150 case PRI_SWITCH_EUROISDN_E1:
1151 case PRI_SWITCH_EUROISDN_T1:
1152 if (BRI_NT_PTMP(ctrl)) {
1153 /*
1154 * NT PTMP mode
1155 *
1156 * We should not send these messages to the network if we are
1157 * the CPE side since phones do not transfer calls within
1158 * themselves. Well... If you consider handing the handset to
1159 * someone else a transfer then how is the network to know?
1160 */
1161 if (new_number) {
1162 #if defined(USE_NOTIFY_FOR_ECT)
1163 /*
1164 * Some ISDN phones only handle the NOTIFY message that the
1165 * EN 300-369 spec says should be sent only if the call has not
1166 * connected yet.
1167 */
1168 q931_notify_redirection(ctrl, call, PRI_NOTIFY_TRANSFER_ACTIVE,
1169 &party_id.name, &party_id.number);
1170 #else
1171 q931_request_subaddress(ctrl, call, PRI_NOTIFY_TRANSFER_ACTIVE,
1172 &party_id.name, &party_id.number);
1173 #endif /* defined(USE_NOTIFY_FOR_ECT) */
1174 }
1175 if (new_subaddress || (party_id.subaddress.valid && new_number)) {
1176 q931_subaddress_transfer(ctrl, call);
1177 }
1178 } else if (PTP_MODE(ctrl)) {
1179 /* PTP mode */
1180 if (new_number) {
1181 /* Immediately send EctInform APDU, callStatus=answered(0) */
1182 send_call_transfer_complete(ctrl, call, 0);
1183 }
1184 if (new_subaddress || (party_id.subaddress.valid && new_number)) {
1185 q931_subaddress_transfer(ctrl, call);
1186 }
1187 }
1188 break;
1189 case PRI_SWITCH_QSIG:
1190 if (new_name || new_number) {
1191 /* Immediately send CallTransferComplete APDU, callStatus=answered(0) */
1192 send_call_transfer_complete(ctrl, call, 0);
1193 }
1194 if (new_subaddress
1195 || (party_id.subaddress.valid && (new_name || new_number))) {
1196 q931_subaddress_transfer(ctrl, call);
1197 }
1198 break;
1199 default:
1200 break;
1201 }
1202 break;
1203 default:
1204 /* Just save the data for further developments. */
1205 break;
1206 }
1207
1208 return 0;
1209 }
1210
pri_redirecting_update(struct pri * ctrl,q931_call * call,const struct pri_party_redirecting * redirecting)1211 int pri_redirecting_update(struct pri *ctrl, q931_call *call, const struct pri_party_redirecting *redirecting)
1212 {
1213 unsigned idx;
1214 struct q931_call *subcall;
1215
1216 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
1217 return -1;
1218 }
1219
1220 /* Save redirecting.to information and reason. */
1221 pri_copy_party_id_to_q931(&call->redirecting.to, &redirecting->to);
1222 q931_party_id_fixup(ctrl, &call->redirecting.to);
1223 call->redirecting.reason = redirecting->reason;
1224
1225 /*
1226 * Update all subcalls with new redirecting.to information and reason.
1227 * I do not think we will ever have any subcalls when this data is relevant,
1228 * but update it just in case.
1229 */
1230 if (call->outboundbroadcast && call->master_call == call) {
1231 for (idx = 0; idx < ARRAY_LEN(call->subcalls); ++idx) {
1232 subcall = call->subcalls[idx];
1233 if (subcall) {
1234 subcall->redirecting.to = call->redirecting.to;
1235 subcall->redirecting.reason = redirecting->reason;
1236 }
1237 }
1238 }
1239
1240 switch (call->ourcallstate) {
1241 case Q931_CALL_STATE_NULL:
1242 /* Save the remaining redirecting information before we place a call. */
1243 pri_copy_party_id_to_q931(&call->redirecting.from, &redirecting->from);
1244 q931_party_id_fixup(ctrl, &call->redirecting.from);
1245 pri_copy_party_id_to_q931(&call->redirecting.orig_called, &redirecting->orig_called);
1246 q931_party_id_fixup(ctrl, &call->redirecting.orig_called);
1247 call->redirecting.orig_reason = redirecting->orig_reason;
1248 if (redirecting->count <= 0) {
1249 if (call->redirecting.from.number.valid) {
1250 /*
1251 * We are redirecting with an unknown count
1252 * so assume the count is one.
1253 */
1254 call->redirecting.count = 1;
1255 } else {
1256 call->redirecting.count = 0;
1257 }
1258 } else if (redirecting->count < PRI_MAX_REDIRECTS) {
1259 call->redirecting.count = redirecting->count;
1260 } else {
1261 call->redirecting.count = PRI_MAX_REDIRECTS;
1262 }
1263 break;
1264 case Q931_CALL_STATE_OVERLAP_RECEIVING:
1265 case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
1266 case Q931_CALL_STATE_CALL_RECEIVED:
1267 /* This is an incoming call that has not connected yet. */
1268 if (!call->redirecting.to.number.valid) {
1269 /* Not being redirected toward valid number data. Ignore. */
1270 break;
1271 }
1272
1273 switch (ctrl->switchtype) {
1274 case PRI_SWITCH_EUROISDN_E1:
1275 case PRI_SWITCH_EUROISDN_T1:
1276 if (PTMP_MODE(ctrl)) {
1277 if (NT_MODE(ctrl)) {
1278 /*
1279 * NT PTMP mode
1280 *
1281 * We should not send these messages to the network if we are
1282 * the CPE side since phones do not redirect calls within
1283 * themselves. Well... If you consider someone else picking up
1284 * the handset a redirection then how is the network to know?
1285 */
1286 q931_notify_redirection(ctrl, call, PRI_NOTIFY_CALL_DIVERTING, NULL,
1287 &call->redirecting.to.number);
1288 }
1289 break;
1290 }
1291 /* PTP mode - same behaviour as Q.SIG */
1292 /* fall through */
1293 case PRI_SWITCH_QSIG:
1294 if (call->redirecting.state != Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3
1295 || strcmp(call->redirecting.to.number.str, call->called.number.str) != 0) {
1296 /* immediately send divertingLegInformation1 APDU */
1297 if (rose_diverting_leg_information1_encode(ctrl, call)
1298 || q931_facility(ctrl, call)) {
1299 pri_message(ctrl,
1300 "Could not schedule facility message for divertingLegInfo1\n");
1301 }
1302 }
1303 call->redirecting.state = Q931_REDIRECTING_STATE_IDLE;
1304
1305 /* immediately send divertingLegInformation3 APDU */
1306 if (rose_diverting_leg_information3_encode(ctrl, call, Q931_FACILITY)
1307 || q931_facility(ctrl, call)) {
1308 pri_message(ctrl,
1309 "Could not schedule facility message for divertingLegInfo3\n");
1310 }
1311 break;
1312 default:
1313 break;
1314 }
1315 break;
1316 default:
1317 pri_message(ctrl, "Ignored redirecting update because call in state %s(%d).\n",
1318 q931_call_state_str(call->ourcallstate), call->ourcallstate);
1319 break;
1320 }
1321
1322 return 0;
1323 }
1324
1325 #if defined(STATUS_REQUEST_PLACE_HOLDER)
1326 /*!
1327 * \brief Poll/ping for the status of any "called" party.
1328 *
1329 * \param ctrl D channel controller.
1330 * \param request_id The upper layer's ID number to match with the response in case
1331 * there are several requests at the same time.
1332 * \param req Setup request for "called" party to determine the status.
1333 *
1334 * \note
1335 * There could be one or more PRI_SUBCMD_STATUS_REQ_RSP to the status request
1336 * depending upon how many endpoints respond to the request.
1337 * (This includes the timeout termination response.)
1338 * \note
1339 * Could be used to poll for the status of call-completion party B.
1340 *
1341 * \retval 0 on success.
1342 * \retval -1 on error.
1343 */
pri_status_req(struct pri * ctrl,int request_id,const struct pri_sr * req)1344 int pri_status_req(struct pri *ctrl, int request_id, const struct pri_sr *req)
1345 {
1346 return -1;
1347 }
1348 #endif /* defined(STATUS_REQUEST_PLACE_HOLDER) */
1349
1350 #if defined(STATUS_REQUEST_PLACE_HOLDER)
1351 /*!
1352 * \brief Response to a poll/ping request for status of any "called" party by libpri.
1353 *
1354 * \param ctrl D channel controller.
1355 * \param invoke_id ID given by libpri when it requested the party status.
1356 * \param status free(0)/busy(1)/incompatible(2)
1357 *
1358 * \note
1359 * There could be zero, one, or more responses to the original
1360 * status request depending upon how many endpoints respond to the request.
1361 * \note
1362 * Could be used to poll for the status of call-completion party B.
1363 *
1364 * \return Nothing
1365 */
pri_status_req_rsp(struct pri * ctrl,int invoke_id,int status)1366 void pri_status_req_rsp(struct pri *ctrl, int invoke_id, int status)
1367 {
1368 }
1369 #endif /* defined(STATUS_REQUEST_PLACE_HOLDER) */
1370
1371 #if 0
1372 /* deprecated routines, use pri_hangup */
1373 int pri_release(struct pri *pri, q931_call *call, int cause)
1374 {
1375 if (!pri || !pri_is_call_valid(pri, call)) {
1376 return -1;
1377 }
1378 return q931_release(pri, call, cause);
1379 }
1380
1381 int pri_disconnect(struct pri *pri, q931_call *call, int cause)
1382 {
1383 if (!pri || !pri_is_call_valid(pri, call)) {
1384 return -1;
1385 }
1386 return q931_disconnect(pri, call, cause);
1387 }
1388 #endif
1389
pri_channel_bridge(q931_call * call1,q931_call * call2)1390 int pri_channel_bridge(q931_call *call1, q931_call *call2)
1391 {
1392 struct q931_call *winner;
1393
1394 /*
1395 * There is a slight risk here if call1 or call2 is actually
1396 * stale. However, if they are stale then it is better to catch
1397 * it here than to write with these pointers.
1398 */
1399 if (!pri_is_call_valid(NULL, call1) || !pri_is_call_valid(NULL, call2)) {
1400 return -1;
1401 }
1402
1403 winner = q931_find_winning_call(call1);
1404 if (!winner) {
1405 /* Cannot transfer: Call 1 does not have a winner yet. */
1406 return -1;
1407 }
1408 call1 = winner;
1409
1410 winner = q931_find_winning_call(call2);
1411 if (!winner) {
1412 /* Cannot transfer: Call 2 does not have a winner yet. */
1413 return -1;
1414 }
1415 call2 = winner;
1416
1417 /* Check to see if we're on the same PRI */
1418 if (call1->pri != call2->pri) {
1419 return -1;
1420 }
1421
1422 /* Check for bearer capability */
1423 if (call1->bc.transcapability != call2->bc.transcapability)
1424 return -1;
1425
1426 switch (call1->pri->switchtype) {
1427 case PRI_SWITCH_NI2:
1428 case PRI_SWITCH_LUCENT5E:
1429 case PRI_SWITCH_ATT4ESS:
1430 if (eect_initiate_transfer(call1->pri, call1, call2)) {
1431 return -1;
1432 }
1433 break;
1434 case PRI_SWITCH_DMS100:
1435 if (rlt_initiate_transfer(call1->pri, call1, call2)) {
1436 return -1;
1437 }
1438 break;
1439 case PRI_SWITCH_QSIG:
1440 call1->bridged_call = call2;
1441 call2->bridged_call = call1;
1442 if (anfpr_initiate_transfer(call1->pri, call1, call2)) {
1443 return -1;
1444 }
1445 break;
1446 case PRI_SWITCH_EUROISDN_E1:
1447 case PRI_SWITCH_EUROISDN_T1:
1448 if (etsi_initiate_transfer(call1->pri, call1, call2)) {
1449 return -1;
1450 }
1451 break;
1452 default:
1453 return -1;
1454 }
1455 return 0;
1456 }
1457
pri_hangup_fix_enable(struct pri * ctrl,int enable)1458 void pri_hangup_fix_enable(struct pri *ctrl, int enable)
1459 {
1460 if (ctrl) {
1461 ctrl->hangup_fix_enabled = enable ? 1 : 0;
1462 }
1463 }
1464
pri_hangup(struct pri * pri,q931_call * call,int cause)1465 int pri_hangup(struct pri *pri, q931_call *call, int cause)
1466 {
1467 if (!pri || !pri_is_call_valid(pri, call)) {
1468 return -1;
1469 }
1470 if (cause == -1)
1471 /* normal clear cause */
1472 cause = PRI_CAUSE_NORMAL_CLEARING;
1473 return q931_hangup(pri, call, cause);
1474 }
1475
pri_reset(struct pri * pri,int channel)1476 int pri_reset(struct pri *pri, int channel)
1477 {
1478 if (!pri)
1479 return -1;
1480 return q931_restart(pri, channel);
1481 }
1482
pri_maintenance_service(struct pri * pri,int span,int channel,int changestatus)1483 int pri_maintenance_service(struct pri *pri, int span, int channel, int changestatus)
1484 {
1485 if (!pri) {
1486 return -1;
1487 }
1488 return maintenance_service(pri, span, channel, changestatus);
1489 }
1490
pri_new_call(struct pri * pri)1491 q931_call *pri_new_call(struct pri *pri)
1492 {
1493 if (!pri)
1494 return NULL;
1495 return q931_new_call(pri);
1496 }
1497
pri_is_dummy_call(q931_call * call)1498 int pri_is_dummy_call(q931_call *call)
1499 {
1500 if (!call) {
1501 return 0;
1502 }
1503 return q931_is_dummy_call(call);
1504 }
1505
pri_dump_event(struct pri * pri,pri_event * e)1506 void pri_dump_event(struct pri *pri, pri_event *e)
1507 {
1508 if (!pri || !e)
1509 return;
1510 pri_message(pri, "Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
1511 }
1512
pri_sr_init(struct pri_sr * req)1513 void pri_sr_init(struct pri_sr *req)
1514 {
1515 memset(req, 0, sizeof(struct pri_sr));
1516 q931_party_redirecting_init(&req->redirecting);
1517 q931_party_id_init(&req->caller);
1518 q931_party_address_init(&req->called);
1519 req->reversecharge = PRI_REVERSECHARGE_NONE;
1520 }
1521
pri_sr_set_connection_call_independent(struct pri_sr * req)1522 int pri_sr_set_connection_call_independent(struct pri_sr *req)
1523 {
1524 if (!req)
1525 return -1;
1526
1527 req->cis_call = 1; /* have to set cis_call for all those pesky IEs we need to setup */
1528 req->cis_auto_disconnect = 1;
1529 return 0;
1530 }
1531
pri_sr_set_no_channel_call(struct pri_sr * req)1532 int pri_sr_set_no_channel_call(struct pri_sr *req)
1533 {
1534 if (!req) {
1535 return -1;
1536 }
1537
1538 req->cis_call = 1;
1539 return 0;
1540 }
1541
1542 /* Don't call any other pri functions on this */
pri_mwi_activate(struct pri * pri,q931_call * c,char * caller,int callerplan,char * callername,int callerpres,char * called,int calledplan)1543 int pri_mwi_activate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
1544 int calledplan)
1545 {
1546 struct pri_sr req;
1547
1548 if (!pri || !pri_is_call_valid(pri, c)) {
1549 return -1;
1550 }
1551
1552 pri_sr_init(&req);
1553 pri_sr_set_connection_call_independent(&req);
1554 pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1555 pri_sr_set_called(&req, called, calledplan, 0);
1556
1557 if (mwi_message_send(pri, c, &req, 1) < 0) {
1558 pri_message(pri, "Unable to send MWI activate message\n");
1559 return -1;
1560 }
1561 /* Do more stuff when we figure out that the CISC stuff works */
1562 return q931_setup(pri, c, &req);
1563 }
1564
pri_mwi_deactivate(struct pri * pri,q931_call * c,char * caller,int callerplan,char * callername,int callerpres,char * called,int calledplan)1565 int pri_mwi_deactivate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
1566 int calledplan)
1567 {
1568 struct pri_sr req;
1569
1570 if (!pri || !pri_is_call_valid(pri, c)) {
1571 return -1;
1572 }
1573
1574 pri_sr_init(&req);
1575 pri_sr_set_connection_call_independent(&req);
1576 pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1577 pri_sr_set_called(&req, called, calledplan, 0);
1578
1579 if(mwi_message_send(pri, c, &req, 0) < 0) {
1580 pri_message(pri, "Unable to send MWI deactivate message\n");
1581 return -1;
1582 }
1583
1584 return q931_setup(pri, c, &req);
1585 }
1586
pri_setup(struct pri * pri,q931_call * c,struct pri_sr * req)1587 int pri_setup(struct pri *pri, q931_call *c, struct pri_sr *req)
1588 {
1589 if (!pri || !pri_is_call_valid(pri, c)) {
1590 return -1;
1591 }
1592
1593 return q931_setup(pri, c, req);
1594 }
1595
pri_call(struct pri * pri,q931_call * c,int transmode,int channel,int exclusive,int nonisdn,char * caller,int callerplan,char * callername,int callerpres,char * called,int calledplan,int ulayer1)1596 int pri_call(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive,
1597 int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
1598 int calledplan, int ulayer1)
1599 {
1600 struct pri_sr req;
1601
1602 if (!pri || !pri_is_call_valid(pri, c)) {
1603 return -1;
1604 }
1605
1606 pri_sr_init(&req);
1607 pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
1608 pri_sr_set_called(&req, called, calledplan, 0);
1609 req.transmode = transmode;
1610 req.channel = channel;
1611 req.exclusive = exclusive;
1612 req.nonisdn = nonisdn;
1613 req.userl1 = ulayer1;
1614 return q931_setup(pri, c, &req);
1615 }
1616
1617 static void (*__pri_error)(struct pri *pri, char *stuff);
1618 static void (*__pri_message)(struct pri *pri, char *stuff);
1619
pri_set_message(void (* func)(struct pri * pri,char * stuff))1620 void pri_set_message(void (*func)(struct pri *pri, char *stuff))
1621 {
1622 __pri_message = func;
1623 }
1624
pri_set_error(void (* func)(struct pri * pri,char * stuff))1625 void pri_set_error(void (*func)(struct pri *pri, char *stuff))
1626 {
1627 __pri_error = func;
1628 }
1629
pri_old_message(struct pri * ctrl,const char * fmt,va_list * ap)1630 static void pri_old_message(struct pri *ctrl, const char *fmt, va_list *ap)
1631 {
1632 char tmp[1024];
1633
1634 vsnprintf(tmp, sizeof(tmp), fmt, *ap);
1635 if (__pri_message)
1636 __pri_message(ctrl, tmp);
1637 else
1638 fputs(tmp, stdout);
1639 }
1640
pri_message(struct pri * ctrl,const char * fmt,...)1641 void pri_message(struct pri *ctrl, const char *fmt, ...)
1642 {
1643 int added_length;
1644 va_list ap;
1645
1646 if (!ctrl || !ctrl->msg_line) {
1647 /* Just have to do it the old way. */
1648 va_start(ap, fmt);
1649 pri_old_message(ctrl, fmt, &ap);
1650 va_end(ap);
1651 return;
1652 }
1653
1654 va_start(ap, fmt);
1655 added_length = vsnprintf(ctrl->msg_line->str + ctrl->msg_line->length,
1656 sizeof(ctrl->msg_line->str) - ctrl->msg_line->length, fmt, ap);
1657 va_end(ap);
1658 if (added_length < 0
1659 || sizeof(ctrl->msg_line->str) <= ctrl->msg_line->length + added_length) {
1660 static char truncated_output[] =
1661 "v-- Error building output or output was truncated. (Next line) --v\n";
1662
1663 /*
1664 * This clause should never need to run because the
1665 * output line accumulation buffer is quite large.
1666 */
1667
1668 /* vsnprintf() error or output string was truncated. */
1669 if (__pri_message) {
1670 __pri_message(ctrl, truncated_output);
1671 } else {
1672 fputs(truncated_output, stdout);
1673 }
1674
1675 /* Add a terminating '\n' to force a flush of the line. */
1676 ctrl->msg_line->length = strlen(ctrl->msg_line->str);
1677 if (ctrl->msg_line->length) {
1678 ctrl->msg_line->str[ctrl->msg_line->length - 1] = '\n';
1679 } else {
1680 ctrl->msg_line->str[0] = '\n';
1681 ctrl->msg_line->str[1] = '\0';
1682 }
1683 } else {
1684 ctrl->msg_line->length += added_length;
1685 }
1686
1687 if (ctrl->msg_line->length
1688 && ctrl->msg_line->str[ctrl->msg_line->length - 1] == '\n') {
1689 /* The accumulated output line was terminated so send it out. */
1690 ctrl->msg_line->length = 0;
1691 if (__pri_message) {
1692 __pri_message(ctrl, ctrl->msg_line->str);
1693 } else {
1694 fputs(ctrl->msg_line->str, stdout);
1695 }
1696 }
1697 }
1698
pri_error(struct pri * pri,const char * fmt,...)1699 void pri_error(struct pri *pri, const char *fmt, ...)
1700 {
1701 char tmp[1024];
1702 va_list ap;
1703 va_start(ap, fmt);
1704 vsnprintf(tmp, sizeof(tmp), fmt, ap);
1705 va_end(ap);
1706 if (__pri_error)
1707 __pri_error(pri, tmp);
1708 else
1709 fputs(tmp, stderr);
1710 }
1711
1712 /* Set overlap mode */
pri_set_overlapdial(struct pri * pri,int state)1713 void pri_set_overlapdial(struct pri *pri,int state)
1714 {
1715 if (pri) {
1716 pri->overlapdial = state ? 1 : 0;
1717 }
1718 }
1719
pri_set_chan_mapping_logical(struct pri * pri,int state)1720 void pri_set_chan_mapping_logical(struct pri *pri, int state)
1721 {
1722 if (pri && pri->switchtype == PRI_SWITCH_QSIG) {
1723 pri->chan_mapping_logical = state ? 1 : 0;
1724 }
1725 }
1726
pri_set_inbanddisconnect(struct pri * pri,unsigned int enable)1727 void pri_set_inbanddisconnect(struct pri *pri, unsigned int enable)
1728 {
1729 if (pri) {
1730 pri->acceptinbanddisconnect = (enable != 0);
1731 }
1732 }
1733
pri_fd(struct pri * pri)1734 int pri_fd(struct pri *pri)
1735 {
1736 return pri->fd;
1737 }
1738
1739 /*!
1740 * \internal
1741 * \brief Append snprintf output to the given buffer.
1742 *
1743 * \param buf Buffer currently filling.
1744 * \param buf_used Offset into buffer where to put new stuff.
1745 * \param buf_size Actual buffer size of buf.
1746 * \param format printf format string.
1747 *
1748 * \return Total buffer space used.
1749 */
1750 static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...) __attribute__((format(printf, 4, 5)));
pri_snprintf(char * buf,size_t buf_used,size_t buf_size,const char * format,...)1751 static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...)
1752 {
1753 va_list args;
1754
1755 if (buf_used < buf_size) {
1756 va_start(args, format);
1757 buf_used += vsnprintf(buf + buf_used, buf_size - buf_used, format, args);
1758 va_end(args);
1759 }
1760 if (buf_size < buf_used) {
1761 buf_used = buf_size + 1;
1762 }
1763 return buf_used;
1764 }
1765
pri_dump_info_str(struct pri * ctrl)1766 char *pri_dump_info_str(struct pri *ctrl)
1767 {
1768 char *buf;
1769 size_t buf_size;
1770 size_t used;
1771 struct q921_frame *f;
1772 struct q921_link *link;
1773 struct pri_cc_record *cc_record;
1774 struct q931_call *call;
1775 unsigned num_calls;
1776 unsigned num_globals;
1777 unsigned q921outstanding;
1778 unsigned idx;
1779 unsigned long switch_bit;
1780
1781 if (!ctrl) {
1782 return NULL;
1783 }
1784
1785 buf_size = 4096; /* This should be bigger than we will ever need. */
1786 buf = malloc(buf_size);
1787 if (!buf) {
1788 return NULL;
1789 }
1790
1791 /* Might be nice to format these a little better */
1792 used = 0;
1793 used = pri_snprintf(buf, used, buf_size, "Switchtype: %s\n",
1794 pri_switch2str(ctrl->switchtype));
1795 used = pri_snprintf(buf, used, buf_size, "Type: %s%s%s\n",
1796 ctrl->bri ? "BRI " : "",
1797 pri_node2str(ctrl->localtype),
1798 PTMP_MODE(ctrl) ? " PTMP" : "");
1799 used = pri_snprintf(buf, used, buf_size, "Remote type: %s\n",
1800 pri_node2str(ctrl->remotetype));
1801 used = pri_snprintf(buf, used, buf_size, "Overlap Dial: %d\n", ctrl->overlapdial);
1802 used = pri_snprintf(buf, used, buf_size, "Logical Channel Mapping: %d\n",
1803 ctrl->chan_mapping_logical);
1804 used = pri_snprintf(buf, used, buf_size, "Timer and counter settings:\n");
1805 switch_bit = PRI_BIT(ctrl->switchtype);
1806 for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
1807 if (pri_timer[idx].used_by & switch_bit) {
1808 enum PRI_TIMERS_AND_COUNTERS tmr;
1809
1810 tmr = pri_timer[idx].number;
1811 if (0 <= ctrl->timers[tmr]
1812 || tmr == PRI_TIMER_T316) {
1813 used = pri_snprintf(buf, used, buf_size, " %s: %d\n",
1814 pri_timer[idx].name, ctrl->timers[tmr]);
1815 }
1816 }
1817 }
1818
1819 /* Remember that Q921 Counters include Q931 packets (and any retransmissions) */
1820 used = pri_snprintf(buf, used, buf_size, "Q931 RX: %d\n", ctrl->q931_rxcount);
1821 used = pri_snprintf(buf, used, buf_size, "Q931 TX: %d\n", ctrl->q931_txcount);
1822 used = pri_snprintf(buf, used, buf_size, "Q921 RX: %d\n", ctrl->q921_rxcount);
1823 used = pri_snprintf(buf, used, buf_size, "Q921 TX: %d\n", ctrl->q921_txcount);
1824 for (link = &ctrl->link; link; link = link->next) {
1825 q921outstanding = 0;
1826 for (f = link->tx_queue; f; f = f->next) {
1827 ++q921outstanding;
1828 }
1829 used = pri_snprintf(buf, used, buf_size, "Q921 Outstanding: %u (TEI=%d)\n",
1830 q921outstanding, link->tei);
1831 }
1832
1833 /* Count the call records in existance. Useful to check for unreleased calls. */
1834 num_calls = 0;
1835 num_globals = 0;
1836 for (call = *ctrl->callpool; call; call = call->next) {
1837 if (!(call->cr & ~Q931_CALL_REFERENCE_FLAG)) {
1838 ++num_globals;
1839 continue;
1840 }
1841 ++num_calls;
1842 if (call->outboundbroadcast) {
1843 used = pri_snprintf(buf, used, buf_size,
1844 "Master call subcall count: %d\n", q931_get_subcall_count(call));
1845 }
1846 }
1847 used = pri_snprintf(buf, used, buf_size, "Total active-calls:%u global:%u\n",
1848 num_calls, num_globals);
1849
1850 /*
1851 * List simplified call completion records.
1852 *
1853 * This should be last in the output because it could overflow
1854 * the buffer.
1855 */
1856 used = pri_snprintf(buf, used, buf_size, "CC records:\n");
1857 for (cc_record = ctrl->cc.pool; cc_record; cc_record = cc_record->next) {
1858 used = pri_snprintf(buf, used, buf_size,
1859 " %ld A:%s B:%s state:%s\n", cc_record->record_id,
1860 cc_record->party_a.number.valid ? cc_record->party_a.number.str : "",
1861 cc_record->party_b.number.valid ? cc_record->party_b.number.str : "",
1862 pri_cc_fsm_state_str(cc_record->state));
1863 }
1864
1865 if (buf_size < used) {
1866 pri_message(ctrl,
1867 "pri_dump_info_str(): Produced output exceeded buffer capacity. (Truncated)\n");
1868 }
1869 return buf;
1870 }
1871
pri_get_crv(struct pri * pri,q931_call * call,int * callmode)1872 int pri_get_crv(struct pri *pri, q931_call *call, int *callmode)
1873 {
1874 if (!pri || !pri_is_call_valid(pri, call)) {
1875 return -1;
1876 }
1877 return q931_call_getcrv(pri, call, callmode);
1878 }
1879
pri_set_crv(struct pri * pri,q931_call * call,int crv,int callmode)1880 int pri_set_crv(struct pri *pri, q931_call *call, int crv, int callmode)
1881 {
1882 if (!pri || !pri_is_call_valid(pri, call)) {
1883 return -1;
1884 }
1885 return q931_call_setcrv(pri, call, crv, callmode);
1886 }
1887
pri_enslave(struct pri * master,struct pri * slave)1888 void pri_enslave(struct pri *master, struct pri *slave)
1889 {
1890 if (!master || !slave) {
1891 return;
1892 }
1893
1894 if (slave->master) {
1895 struct pri *swp;
1896
1897 /* The slave already has a master */
1898 if (master->master || master->slave) {
1899 /* The new master has a master or it already has slaves. */
1900 return;
1901 }
1902
1903 /* Swap master and slave. */
1904 swp = master;
1905 master = slave;
1906 slave = swp;
1907 }
1908
1909 /*
1910 * To have some support for dynamic interfaces, the master NFAS
1911 * D channel control structure will always exist even if it is
1912 * abandoned/deleted by the upper layer. The master/slave
1913 * pointers ensure that the correct master will be used.
1914 */
1915
1916 master = PRI_NFAS_MASTER(master);
1917 master->nfas = 1;
1918 slave->nfas = 1;
1919 slave->callpool = &master->localpool;
1920
1921 /* Link the slave to the master on the end of the master's list. */
1922 slave->master = master;
1923 slave->slave = NULL;
1924 for (; master->slave; master = master->slave) {
1925 }
1926 master->slave = slave;
1927 }
1928
pri_sr_new(void)1929 struct pri_sr *pri_sr_new(void)
1930 {
1931 struct pri_sr *req;
1932 req = malloc(sizeof(*req));
1933 if (req)
1934 pri_sr_init(req);
1935 return req;
1936 }
1937
pri_sr_free(struct pri_sr * sr)1938 void pri_sr_free(struct pri_sr *sr)
1939 {
1940 free(sr);
1941 }
1942
pri_sr_set_channel(struct pri_sr * sr,int channel,int exclusive,int nonisdn)1943 int pri_sr_set_channel(struct pri_sr *sr, int channel, int exclusive, int nonisdn)
1944 {
1945 sr->channel = channel;
1946 sr->exclusive = exclusive;
1947 sr->nonisdn = nonisdn;
1948 return 0;
1949 }
1950
pri_sr_set_bearer(struct pri_sr * sr,int transmode,int userl1)1951 int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1)
1952 {
1953 sr->transmode = transmode;
1954 sr->userl1 = userl1;
1955 return 0;
1956 }
1957
pri_sr_set_called(struct pri_sr * sr,char * called,int calledplan,int numcomplete)1958 int pri_sr_set_called(struct pri_sr *sr, char *called, int calledplan, int numcomplete)
1959 {
1960 q931_party_address_init(&sr->called);
1961 if (called) {
1962 sr->called.number.valid = 1;
1963 sr->called.number.plan = calledplan;
1964 libpri_copy_string(sr->called.number.str, called, sizeof(sr->called.number.str));
1965 }
1966 sr->numcomplete = numcomplete;
1967 return 0;
1968 }
1969
pri_sr_set_called_subaddress(struct pri_sr * sr,const struct pri_party_subaddress * subaddress)1970 void pri_sr_set_called_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
1971 {
1972 pri_copy_party_subaddress_to_q931(&sr->called.subaddress, subaddress);
1973 }
1974
pri_sr_set_caller(struct pri_sr * sr,char * caller,char * callername,int callerplan,int callerpres)1975 int pri_sr_set_caller(struct pri_sr *sr, char *caller, char *callername, int callerplan, int callerpres)
1976 {
1977 q931_party_id_init(&sr->caller);
1978 if (caller) {
1979 sr->caller.number.valid = 1;
1980 sr->caller.number.presentation = callerpres
1981 & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1982 sr->caller.number.plan = callerplan;
1983 libpri_copy_string(sr->caller.number.str, caller, sizeof(sr->caller.number.str));
1984
1985 if (callername) {
1986 sr->caller.name.valid = 1;
1987 sr->caller.name.presentation = callerpres & PRI_PRES_RESTRICTION;
1988 sr->caller.name.char_set = PRI_CHAR_SET_ISO8859_1;
1989 libpri_copy_string(sr->caller.name.str, callername,
1990 sizeof(sr->caller.name.str));
1991 }
1992 }
1993 return 0;
1994 }
1995
pri_sr_set_caller_subaddress(struct pri_sr * sr,const struct pri_party_subaddress * subaddress)1996 void pri_sr_set_caller_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
1997 {
1998 pri_copy_party_subaddress_to_q931(&sr->caller.subaddress, subaddress);
1999 }
2000
pri_sr_set_caller_party(struct pri_sr * sr,const struct pri_party_id * caller)2001 void pri_sr_set_caller_party(struct pri_sr *sr, const struct pri_party_id *caller)
2002 {
2003 pri_copy_party_id_to_q931(&sr->caller, caller);
2004 }
2005
pri_sr_set_redirecting(struct pri_sr * sr,char * num,int plan,int pres,int reason)2006 int pri_sr_set_redirecting(struct pri_sr *sr, char *num, int plan, int pres, int reason)
2007 {
2008 q931_party_redirecting_init(&sr->redirecting);
2009 if (num && num[0]) {
2010 sr->redirecting.from.number.valid = 1;
2011 sr->redirecting.from.number.presentation = pres
2012 & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2013 sr->redirecting.from.number.plan = plan;
2014 libpri_copy_string(sr->redirecting.from.number.str, num,
2015 sizeof(sr->redirecting.from.number.str));
2016
2017 sr->redirecting.count = 1;
2018 sr->redirecting.reason = reason;
2019 }
2020 return 0;
2021 }
2022
pri_sr_set_redirecting_parties(struct pri_sr * sr,const struct pri_party_redirecting * redirecting)2023 void pri_sr_set_redirecting_parties(struct pri_sr *sr, const struct pri_party_redirecting *redirecting)
2024 {
2025 pri_copy_party_id_to_q931(&sr->redirecting.from, &redirecting->from);
2026 pri_copy_party_id_to_q931(&sr->redirecting.to, &redirecting->to);
2027 pri_copy_party_id_to_q931(&sr->redirecting.orig_called, &redirecting->orig_called);
2028 sr->redirecting.orig_reason = redirecting->orig_reason;
2029 sr->redirecting.reason = redirecting->reason;
2030 if (redirecting->count <= 0) {
2031 if (sr->redirecting.from.number.valid) {
2032 /*
2033 * We are redirecting with an unknown count
2034 * so assume the count is one.
2035 */
2036 sr->redirecting.count = 1;
2037 } else {
2038 sr->redirecting.count = 0;
2039 }
2040 } else if (redirecting->count < PRI_MAX_REDIRECTS) {
2041 sr->redirecting.count = redirecting->count;
2042 } else {
2043 sr->redirecting.count = PRI_MAX_REDIRECTS;
2044 }
2045 }
2046
pri_sr_set_reversecharge(struct pri_sr * sr,int requested)2047 void pri_sr_set_reversecharge(struct pri_sr *sr, int requested)
2048 {
2049 sr->reversecharge = requested;
2050 }
2051
pri_sr_set_keypad_digits(struct pri_sr * sr,const char * keypad_digits)2052 void pri_sr_set_keypad_digits(struct pri_sr *sr, const char *keypad_digits)
2053 {
2054 sr->keypad_digits = keypad_digits;
2055 }
2056
pri_transfer_enable(struct pri * ctrl,int enable)2057 void pri_transfer_enable(struct pri *ctrl, int enable)
2058 {
2059 if (ctrl) {
2060 ctrl->transfer_support = enable ? 1 : 0;
2061 }
2062 }
2063
pri_hold_enable(struct pri * ctrl,int enable)2064 void pri_hold_enable(struct pri *ctrl, int enable)
2065 {
2066 if (ctrl) {
2067 ctrl->hold_support = enable ? 1 : 0;
2068 }
2069 }
2070
pri_hold(struct pri * ctrl,q931_call * call)2071 int pri_hold(struct pri *ctrl, q931_call *call)
2072 {
2073 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
2074 return -1;
2075 }
2076 return q931_send_hold(ctrl, call);
2077 }
2078
pri_hold_ack(struct pri * ctrl,q931_call * call)2079 int pri_hold_ack(struct pri *ctrl, q931_call *call)
2080 {
2081 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
2082 return -1;
2083 }
2084 return q931_send_hold_ack(ctrl, call);
2085 }
2086
pri_hold_rej(struct pri * ctrl,q931_call * call,int cause)2087 int pri_hold_rej(struct pri *ctrl, q931_call *call, int cause)
2088 {
2089 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
2090 return -1;
2091 }
2092 return q931_send_hold_rej(ctrl, call, cause);
2093 }
2094
pri_retrieve(struct pri * ctrl,q931_call * call,int channel)2095 int pri_retrieve(struct pri *ctrl, q931_call *call, int channel)
2096 {
2097 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
2098 return -1;
2099 }
2100 return q931_send_retrieve(ctrl, call, channel);
2101 }
2102
pri_retrieve_ack(struct pri * ctrl,q931_call * call,int channel)2103 int pri_retrieve_ack(struct pri *ctrl, q931_call *call, int channel)
2104 {
2105 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
2106 return -1;
2107 }
2108 return q931_send_retrieve_ack(ctrl, call, channel);
2109 }
2110
pri_retrieve_rej(struct pri * ctrl,q931_call * call,int cause)2111 int pri_retrieve_rej(struct pri *ctrl, q931_call *call, int cause)
2112 {
2113 if (!ctrl || !pri_is_call_valid(ctrl, call)) {
2114 return -1;
2115 }
2116 return q931_send_retrieve_rej(ctrl, call, cause);
2117 }
2118
pri_callrerouting_facility(struct pri * pri,q931_call * call,const char * dest,const char * original,const char * reason)2119 int pri_callrerouting_facility(struct pri *pri, q931_call *call, const char *dest, const char* original, const char* reason)
2120 {
2121 if (!pri || !pri_is_call_valid(pri, call) || !dest) {
2122 return -1;
2123 }
2124
2125 return qsig_cf_callrerouting(pri, call, dest, original, reason);
2126 }
2127
pri_reroute_enable(struct pri * ctrl,int enable)2128 void pri_reroute_enable(struct pri *ctrl, int enable)
2129 {
2130 if (ctrl) {
2131 ctrl->deflection_support = enable ? 1 : 0;
2132 }
2133 }
2134
pri_reroute_call(struct pri * ctrl,q931_call * call,const struct pri_party_id * caller,const struct pri_party_redirecting * deflection,int subscription_option)2135 int pri_reroute_call(struct pri *ctrl, q931_call *call, const struct pri_party_id *caller, const struct pri_party_redirecting *deflection, int subscription_option)
2136 {
2137 const struct q931_party_id *caller_id;
2138 struct q931_party_id local_caller;
2139 struct q931_party_redirecting reroute;
2140
2141 if (!ctrl || !pri_is_call_valid(ctrl, call) || !deflection) {
2142 return -1;
2143 }
2144
2145 if (caller) {
2146 /* Convert the caller update information. */
2147 pri_copy_party_id_to_q931(&local_caller, caller);
2148 q931_party_id_fixup(ctrl, &local_caller);
2149 caller_id = &local_caller;
2150 } else {
2151 caller_id = NULL;
2152 }
2153
2154 /* Convert the deflection information. */
2155 q931_party_redirecting_init(&reroute);
2156 pri_copy_party_id_to_q931(&reroute.from, &deflection->from);
2157 q931_party_id_fixup(ctrl, &reroute.from);
2158 pri_copy_party_id_to_q931(&reroute.to, &deflection->to);
2159 q931_party_id_fixup(ctrl, &reroute.to);
2160 pri_copy_party_id_to_q931(&reroute.orig_called, &deflection->orig_called);
2161 q931_party_id_fixup(ctrl, &reroute.orig_called);
2162 reroute.reason = deflection->reason;
2163 reroute.orig_reason = deflection->orig_reason;
2164 if (deflection->count <= 0) {
2165 /*
2166 * We are deflecting with an unknown count
2167 * so assume the count is one.
2168 */
2169 reroute.count = 1;
2170 } else if (deflection->count < PRI_MAX_REDIRECTS) {
2171 reroute.count = deflection->count;
2172 } else {
2173 reroute.count = PRI_MAX_REDIRECTS;
2174 }
2175
2176 return send_reroute_request(ctrl, call, caller_id, &reroute, subscription_option);
2177 }
2178
pri_cc_enable(struct pri * ctrl,int enable)2179 void pri_cc_enable(struct pri *ctrl, int enable)
2180 {
2181 if (ctrl) {
2182 ctrl->cc_support = enable ? 1 : 0;
2183 }
2184 }
2185
pri_cc_recall_mode(struct pri * ctrl,int mode)2186 void pri_cc_recall_mode(struct pri *ctrl, int mode)
2187 {
2188 if (ctrl) {
2189 ctrl->cc.option.recall_mode = mode ? 1 : 0;
2190 }
2191 }
2192
pri_cc_retain_signaling_req(struct pri * ctrl,int signaling_retention)2193 void pri_cc_retain_signaling_req(struct pri *ctrl, int signaling_retention)
2194 {
2195 if (ctrl && 0 <= signaling_retention && signaling_retention < 3) {
2196 ctrl->cc.option.signaling_retention_req = signaling_retention;
2197 }
2198 }
2199
pri_cc_retain_signaling_rsp(struct pri * ctrl,int signaling_retention)2200 void pri_cc_retain_signaling_rsp(struct pri *ctrl, int signaling_retention)
2201 {
2202 if (ctrl) {
2203 ctrl->cc.option.signaling_retention_rsp = signaling_retention ? 1 : 0;
2204 }
2205 }
2206
pri_persistent_layer2_option(struct pri * ctrl,enum pri_layer2_persistence option)2207 void pri_persistent_layer2_option(struct pri *ctrl, enum pri_layer2_persistence option)
2208 {
2209 if (!ctrl) {
2210 return;
2211 }
2212 if (PTMP_MODE(ctrl)) {
2213 switch (option) {
2214 case PRI_L2_PERSISTENCE_DEFAULT:
2215 ctrl->l2_persistence = pri_l2_persistence_option_default(ctrl);
2216 break;
2217 case PRI_L2_PERSISTENCE_KEEP_UP:
2218 case PRI_L2_PERSISTENCE_LEAVE_DOWN:
2219 ctrl->l2_persistence = option;
2220 break;
2221 }
2222 if (ctrl->l2_persistence == PRI_L2_PERSISTENCE_KEEP_UP) {
2223 q921_bring_layer2_up(ctrl);
2224 }
2225 }
2226 }
2227
pri_display_options_send(struct pri * ctrl,unsigned long flags)2228 void pri_display_options_send(struct pri *ctrl, unsigned long flags)
2229 {
2230 if (!ctrl) {
2231 return;
2232 }
2233 if (!flags) {
2234 flags = pri_display_options_send_default(ctrl);
2235 }
2236 ctrl->display_flags.send = flags;
2237 }
2238
pri_display_options_receive(struct pri * ctrl,unsigned long flags)2239 void pri_display_options_receive(struct pri *ctrl, unsigned long flags)
2240 {
2241 if (!ctrl) {
2242 return;
2243 }
2244 if (!flags) {
2245 flags = pri_display_options_receive_default(ctrl);
2246 }
2247 ctrl->display_flags.receive = flags;
2248 }
2249
pri_display_text(struct pri * ctrl,q931_call * call,const struct pri_subcmd_display_txt * display)2250 int pri_display_text(struct pri *ctrl, q931_call *call, const struct pri_subcmd_display_txt *display)
2251 {
2252 if (!ctrl || !display || display->length <= 0
2253 || sizeof(display->text) < display->length || !pri_is_call_valid(ctrl, call)) {
2254 /* Parameter sanity checks failed. */
2255 return -1;
2256 }
2257 return q931_display_text(ctrl, call, display);
2258 }
2259
pri_date_time_send_option(struct pri * ctrl,int option)2260 void pri_date_time_send_option(struct pri *ctrl, int option)
2261 {
2262 if (!ctrl) {
2263 return;
2264 }
2265 switch (option) {
2266 case PRI_DATE_TIME_SEND_DEFAULT:
2267 ctrl->date_time_send = pri_date_time_send_default(ctrl);
2268 break;
2269 default:
2270 case PRI_DATE_TIME_SEND_NO:
2271 ctrl->date_time_send = PRI_DATE_TIME_SEND_NO;
2272 break;
2273 case PRI_DATE_TIME_SEND_DATE:
2274 case PRI_DATE_TIME_SEND_DATE_HH:
2275 case PRI_DATE_TIME_SEND_DATE_HHMM:
2276 case PRI_DATE_TIME_SEND_DATE_HHMMSS:
2277 if (NT_MODE(ctrl)) {
2278 /* Only networks may send date/time ie. */
2279 ctrl->date_time_send = option;
2280 } else {
2281 ctrl->date_time_send = PRI_DATE_TIME_SEND_NO;
2282 }
2283 break;
2284 }
2285 }
2286