1 diff -urN libpri-1.2.2.orig/Makefile libpri-1.2.2/Makefile
2 --- libpri-1.2.2.orig/Makefile 2006-01-10 21:19:14.000000000 +0100
3 +++ libpri-1.2.2/Makefile 2006-01-18 12:28:07.000000000 +0100
5 DYNAMIC_LIBRARY=libpri.so.1.0
6 STATIC_OBJS=copy_string.o pri.o q921.o prisched.o q931.o pri_facility.o
7 DYNAMIC_OBJS=copy_string.lo pri.lo q921.lo prisched.lo q931.lo pri_facility.lo
8 -CFLAGS=-Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -g $(ALERTING) $(LIBPRI_COUNTERS)
9 +CFLAGS=-Wall -Wstrict-prototypes -Wmissing-prototypes -g $(ALERTING) $(LIBPRI_COUNTERS) -DRELAX_TRB
12 SOFLAGS = -Wl,-hlibpri.so.1.0
13 diff -urN libpri-1.2.2.orig/README libpri-1.2.2/README
14 --- libpri-1.2.2.orig/README 2005-11-29 19:39:18.000000000 +0100
15 +++ libpri-1.2.2/README 2006-01-18 12:28:07.000000000 +0100
17 -libpri: An implementation of Primate Rate ISDN
19 -Written by Mark Spencer <markster@digium.com>
20 +libpri: An implementation of Primate Rate ISDN (and BRI ISDN)
22 +Written by Mark Spencer <markster@linux-support.net>
23 +Modified for BRI support by Klaus-Peter Junghanns <kpj@junghanns.net>
28 based on the Bellcore specification SR-NWT-002343 for National ISDN. As of
29 May 12, 2001, it has been tested work with NI-2, Nortel DMS-100, and
30 Lucent 5E Custom protocols on switches from Nortel and Lucent.
31 +The BRI and euroISDN modifications are based on ETS 300 102-1.
33 What is the license for libpri?
34 ===============================
38 If you wish to use libpri in an application for which the GPL is not
39 -appropriate (e.g. a proprietary embedded system), licenses for libpri
40 -under more flexible terms can be readily obtained through Digium, Inc.
42 +appropriate (e.g. a proprietary embedded system), then you have to use
43 +a non-standard compliant version without BRI support.
46 How do I report bugs or contribute?
47 diff -urN libpri-1.2.2.orig/TODO libpri-1.2.2/TODO
48 --- libpri-1.2.2.orig/TODO 2005-11-29 19:39:18.000000000 +0100
49 +++ libpri-1.2.2/TODO 2006-01-18 12:28:07.000000000 +0100
55 --- Support unnumbered information frames
59 --- Implement the 11 missing Q.931 timers
60 +-- Locking Shift IE (you did that already, didnt you??)
61 +-- Implement the 10 missing Q.931 timers
63 diff -urN libpri-1.2.2.orig/libpri.h libpri-1.2.2/libpri.h
64 --- libpri-1.2.2.orig/libpri.h 2005-11-29 19:39:18.000000000 +0100
65 +++ libpri-1.2.2/libpri.h 2006-01-18 12:28:07.000000000 +0100
70 -#define PRI_NETWORK 1
71 +#define PRI_NETWORK 1 /* PTP modes, default for PRI */
73 +#define BRI_NETWORK_PTMP 3 /* PTMP modes, default for BRI */
74 +#define BRI_CPE_PTMP 4
75 +#define BRI_NETWORK 5 /* PTP modes */
79 #define PRI_DEBUG_Q921_RAW (1 << 0) /* Show raw HDLC frames */
81 #define PRI_EVENT_NOTIFY 16 /* Notification received */
82 #define PRI_EVENT_PROGRESS 17 /* When we get CALL_PROCEEDING or PROGRESS */
83 #define PRI_EVENT_KEYPAD_DIGIT 18 /* When we receive during ACTIVE state */
84 +#define PRI_EVENT_HOLD_REQ 19 /* R */
85 +#define PRI_EVENT_RETRIEVE_REQ 20
86 +#define PRI_EVENT_SUSPEND_REQ 21 /* park */
87 +#define PRI_EVENT_RESUME_REQ 22 /* unpark */
88 +#define PRI_EVENT_DISPLAY_RECEIVED 23
89 +#define PRI_EVENT_FACILITY 24 /* Facility */
92 #define PRI_STATE_DOWN 0
94 #define PRI_NSF_ATT_MULTIQUEST 0xF0
95 #define PRI_NSF_CALL_REDIRECTION_SERVICE 0xF7
97 +typedef struct q921_call q921_call;
98 typedef struct q931_call q931_call;
100 typedef struct pri_event_generic {
101 /* Events with no additional information fall in this category */
106 typedef struct pri_event_error {
107 @@ -273,18 +285,19 @@
112 char useruserinfo[260]; /* User->User info */
116 typedef struct pri_event_answer {
119 + int tei; /* belongs to this tei */
124 char useruserinfo[260]; /* User->User info */
128 typedef struct pri_event_facname {
129 @@ -302,26 +315,29 @@
131 int channel; /* Channel requested */
132 int callingpres; /* Presentation of Calling CallerID */
133 - int callingplanani; /* Dialing plan of Calling entity ANI */
134 + int callingpresuser; /* Presentation of Calling CallerID */
135 int callingplan; /* Dialing plan of Calling entity */
136 - char callingani[256]; /* Calling ANI */
137 - char callingnum[256]; /* Calling number */
138 + int callingplanuser; /* Dialing plan of Calling entity */
139 + int callingplanani; /* Dialing plan of Calling entity ANI */
140 + char callingnum[256]; /* Calling number, network provided */
141 + char callingani[256]; /* Calling number, user provided */
142 char callingname[256]; /* Calling name (if provided) */
143 int calledplan; /* Dialing plan of Called number */
144 int ani2; /* ANI II */
145 char callednum[256]; /* Called number */
146 - char redirectingnum[256]; /* Redirecting number */
147 - char redirectingname[256]; /* Redirecting name */
148 - int redirectingreason; /* Reason for redirect */
149 + char redirectingnum[256]; /* Redirecting number */
150 + char redirectingname[256]; /* Redirecting name */
151 + int redirectingreason; /* Reason for redirect */
152 int callingplanrdnis; /* Dialing plan of Redirecting Number */
153 - char useruserinfo[260]; /* User->User info */
154 + char useruserinfo[260]; /* User->User info */
155 int flexible; /* Are we flexible with our channel selection? */
156 int cref; /* Call Reference Number */
157 int ctype; /* Call type (see PRI_TRANS_CAP_* */
158 - int layer1; /* User layer 1 */
159 + int layer1; /* User layer 1 */
160 int complete; /* Have we seen "Complete" i.e. no more number? */
161 q931_call *call; /* Opaque call pointer */
162 - char callingsubaddr[256]; /* Calling parties subaddress */
163 + int tei; /* belongs to this tei */
164 + char callingsubaddr[256]; /* Calling parties subaddress */
167 char origcalledname[256];
169 int channel; /* Channel requested */
173 + int inband_progress;
174 q931_call *call; /* Opaque call pointer */
175 long aoc_units; /* Advise of Charge number of charged units */
176 char useruserinfo[260]; /* User->User info */
178 typedef struct pri_event_setup_ack {
181 + q931_call *call; /* Opaque call pointer */
182 } pri_event_setup_ack;
184 typedef struct pri_event_notify {
185 @@ -374,20 +393,80 @@
187 } pri_event_keypad_digit;
189 +typedef struct pri_event_hold_req {
195 +} pri_event_hold_req;
197 +/* euroisdn faciltiy fun */
198 +typedef struct pri_event_facility_req {
204 + char forwardnum[256]; /* Redirection destination */
206 +} pri_event_facility_req;
208 +typedef struct pri_event_retrieve_req {
214 +} pri_event_retrieve_req;
216 +typedef struct pri_event_suspend_req {
223 +} pri_event_suspend_req;
225 +typedef struct pri_event_resume_req {
232 +} pri_event_resume_req;
234 +typedef struct pri_event_display {
240 +} pri_event_display;
245 pri_event_generic gen; /* Generic view */
246 pri_event_restart restart; /* Restart view */
247 pri_event_error err; /* Error view */
248 pri_event_facname facname; /* Caller*ID Name on Facility */
249 + pri_event_facility_req facility; /* sservices */
250 pri_event_ring ring; /* Ring */
251 pri_event_hangup hangup; /* Hang up */
252 pri_event_ringing ringing; /* Ringing */
253 - pri_event_ringing answer; /* Answer */
254 + pri_event_answer answer; /* Answer */
255 pri_event_restart_ack restartack; /* Restart Acknowledge */
256 pri_event_proceeding proceeding; /* Call proceeding & Progress */
257 pri_event_setup_ack setup_ack; /* SETUP_ACKNOWLEDGE structure */
258 pri_event_notify notify; /* Notification */
259 + pri_event_hold_req hold_req;
260 + pri_event_retrieve_req retrieve_req;
261 + pri_event_suspend_req suspend_req;
262 + pri_event_resume_req resume_req;
263 + pri_event_display display;
264 pri_event_keypad_digit digit; /* Digits that come during a call */
268 channel operating in HDLC mode with FCS computed by the fd's driver. Also it
269 must be NON-BLOCKING! Frames received on the fd should include FCS. Nodetype
270 must be one of PRI_NETWORK or PRI_CPE. switchtype should be PRI_SWITCH_* */
271 -extern struct pri *pri_new(int fd, int nodetype, int switchtype);
272 +extern struct pri *pri_new(int fd, int nodetype, int switchtype, int span);
274 +extern void pri_shutdown(struct pri *pri);
276 /* Create D-channel just as above with user defined I/O callbacks and data */
277 extern struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata);
279 /* Enable transmission support of Facility IEs on the pri */
280 extern void pri_facility_enable(struct pri *pri);
282 +/* Set file descriptor for debugging to a file */
283 +extern void pri_set_debug_fd(struct pri *pri, int fd);
285 /* Run PRI on the given D-channel, taking care of any events that
286 need to be handled. If block is set, it will block until an event
287 occurs which needs to be handled */
289 /* Send a digit in overlap mode */
290 extern int pri_information(struct pri *pri, q931_call *call, char digit);
292 +/* Send a INFO msg with display ie */
293 +extern int pri_information_display(struct pri *pri, q931_call *call, char *display);
295 +/* add a display ie to a call, so it can be sent with the next message */
296 +extern int pri_add_display(struct pri *pri, q931_call *call, char *display);
298 /* Answer the incomplete(call without called number) call on the given channel.
299 Set non-isdn to non-zero if you are not connecting to ISDN equipment */
300 extern int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn);
302 Set non-isdn to non-zero if you are not connecting to ISDN equipment */
303 extern int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn);
305 +extern int pri_deflect(struct pri *pri, q931_call *call, char *destination);
307 +/* Ack a HOLD_REQ */
308 +extern int pri_hold_acknowledge(struct pri *pri, q931_call *call);
310 +/* Reject a HOLD_REQ */
311 +extern int pri_hold_reject(struct pri *pri, q931_call *call);
313 +/* Ack a RETRIEVE_REQ */
314 +extern int pri_retrieve_acknowledge(struct pri *pri, q931_call *call, int channel);
316 +/* Reject a RETRIEVE_REQ */
317 +extern int pri_retrieve_reject(struct pri *pri, q931_call *call);
319 +/* Ack a SUSPEND_REQ */
320 +extern int pri_suspend_acknowledge(struct pri *pri, q931_call *call, char *display);
322 +/* Reject a SUSPEND_REQ */
323 +extern int pri_suspend_reject(struct pri *pri, q931_call *call, char *display);
325 +/* Reject a RESUME_REQ */
326 +extern int pri_resume_reject(struct pri *pri, q931_call *call, char *display);
328 +/* Ack a RESUME_REQ */
329 +extern int pri_resume_acknowledge(struct pri *pri, q931_call *call, int channel, char *display);
331 +/* Send a Facility Message */
332 +extern int pri_facility(struct pri *pri, q931_call *call, int operation, char *arguments);
334 /* Set CRV reference for GR-303 calls */
338 #define PRI_USER_USER_TX
339 /* Set the user user field. Warning! don't send binary data accross this field */
340 extern void pri_sr_set_useruser(struct pri_sr *sr, char *userchars);
342 extern void pri_call_set_useruser(q931_call *sr, char *userchars);
344 extern int pri_setup(struct pri *pri, q931_call *call, struct pri_sr *req);
347 /* Override message and error stuff */
348 #define PRI_NEW_SET_API
349 -extern void pri_set_message(void (*__pri_error)(struct pri *pri, char *));
350 -extern void pri_set_error(void (*__pri_error)(struct pri *pri, char *));
351 +extern void pri_set_message(void (*__pri_error)(char *, int span));
352 +extern void pri_set_error(void (*__pri_error)(char *, int span));
354 /* Set overlap mode */
355 #define PRI_SET_OVERLAPDIAL
356 diff -urN libpri-1.2.2.orig/pri.c libpri-1.2.2/pri.c
357 --- libpri-1.2.2.orig/pri.c 2005-11-29 19:39:18.000000000 +0100
358 +++ libpri-1.2.2/pri.c 2006-01-18 12:28:07.000000000 +0100
361 * libpri: An implementation of Primary Rate ISDN
363 - * Written by Mark Spencer <markster@digium.com>
364 + * Written by Mark Spencer <markster@linux-suppot.net>
366 - * Copyright (C) 2001-2005, Digium
367 - * All Rights Reserved.
368 + * This program is confidential
370 - * This program is free software; you can redistribute it and/or modify
371 - * it under the terms of the GNU General Public License as published by
372 - * the Free Software Foundation; either version 2 of the License, or
373 - * (at your option) any later version.
375 - * This program is distributed in the hope that it will be useful,
376 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
377 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
378 - * GNU General Public License for more details.
380 - * You should have received a copy of the GNU General Public License
381 - * along with this program; if not, write to the Free Software
382 - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
383 + * Copyright (C) 2001, Linux Support Services, Inc.
384 + * All Rights Reserved.
396 + case BRI_NETWORK_PTMP:
397 + return "Network (PtMP)";
399 + return "CPE (PtMP)";
401 return "Invalid value";
407 -static struct pri *__pri_new(int fd, int node, int switchtype, struct pri *master, pri_io_cb rd, pri_io_cb wr, void *userdata)
408 +static struct pri *__pri_new(int fd, int node, int switchtype, struct pri *master, pri_io_cb rd, pri_io_cb wr, void *userdata, int span)
411 p = malloc(sizeof(struct pri));
414 p->callpool = &p->localpool;
415 pri_default_timers(p, switchtype);
418 #ifdef LIBPRI_COUNTERS
422 p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
423 p->sapi = Q921_SAPI_GR303_EOC;
424 p->tei = Q921_TEI_GR303_EOC_OPS;
425 - p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_EOC_PATH, p, NULL, NULL, NULL);
426 + p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_EOC_PATH, p, NULL, NULL, NULL, span);
427 if (!p->subchannel) {
431 p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
432 p->sapi = Q921_SAPI_GR303_TMC_CALLPROC;
433 p->tei = Q921_TEI_GR303_TMC_CALLPROC;
434 - p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_TMC_SWITCHING, p, NULL, NULL, NULL);
435 + p->subchannel = __pri_new(-1, node, PRI_SWITCH_GR303_TMC_SWITCHING, p, NULL, NULL, NULL, span);
436 if (!p->subchannel) {
441 /* Start Q.921 layer, Wait if we're the network */
443 - q921_start(p, p->localtype == PRI_CPE);
444 + q921_start(p, p->localtype == PRI_CPE, 0);
448 @@ -262,15 +260,16 @@
450 /* Restart Q.921 layer */
453 - q921_start(pri, pri->localtype == PRI_CPE);
454 +// XXX q921_reset(pri);
455 +// q921_start(pri, pri->localtype == PRI_CPE);
460 -struct pri *pri_new(int fd, int nodetype, int switchtype)
462 +struct pri *pri_new(int fd, int nodetype, int switchtype, int span)
464 - return __pri_new(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL);
465 + return __pri_new(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, span);
468 struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
470 io_read = __pri_read;
472 io_write = __pri_write;
473 - return __pri_new(fd, nodetype, switchtype, NULL, io_read, io_write, userdata);
474 + return __pri_new(fd, nodetype, switchtype, NULL, io_read, io_write, userdata, -1);
477 void *pri_get_userdata(struct pri *pri)
480 /* Return a configuration error */
481 pri->ev.err.e = PRI_EVENT_CONFIG_ERR;
482 - libpri_copy_string(pri->ev.err.err, errstr, sizeof(pri->ev.err.err));
483 + strncpy(pri->ev.err.err, errstr, sizeof(pri->ev.err.err) - 1);
491 +void pri_set_debug_fd(struct pri *pri, int fd)
496 + if (pri->subchannel)
497 + pri_set_debug_fd(pri->subchannel, fd);
500 int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
504 return q931_notify(pri, call, channel, info);
507 +int pri_information_display(struct pri *pri, q931_call *call, char *display)
511 + return q931_information_display(pri, call, display);
514 +int pri_add_display(struct pri *pri, q931_call *call, char *display)
518 + return q931_add_display(pri, call, display);
522 void pri_destroycall(struct pri *pri, q931_call *call)
526 return q931_connect(pri, call, channel, nonisdn);
529 +int pri_hold_acknowledge(struct pri *pri, q931_call *call)
533 + return q931_hold_acknowledge(pri, call);
536 +int pri_hold_reject(struct pri *pri, q931_call *call)
540 + return q931_hold_reject(pri, call);
543 +int pri_retrieve_acknowledge(struct pri *pri, q931_call *call, int channel)
547 + return q931_retrieve_acknowledge(pri, call, channel);
550 +int pri_retrieve_reject(struct pri *pri, q931_call *call)
554 + return q931_retrieve_reject(pri, call);
557 +int pri_suspend_acknowledge(struct pri *pri, q931_call *call, char *display)
561 + return q931_suspend_acknowledge(pri, call, display);
564 +int pri_suspend_reject(struct pri *pri, q931_call *call, char *display)
568 + return q931_suspend_reject(pri, call, display);
571 +int pri_resume_reject(struct pri *pri, q931_call *call, char *display)
575 + return q931_resume_reject(pri, call, display);
578 +int pri_resume_acknowledge(struct pri *pri, q931_call *call, int channel, char *display)
582 + return q931_resume_acknowledge(pri, call, channel, display);
585 +int pri_facility(struct pri *pri, q931_call *call, int operation, char *arguments)
589 +// return q931_facility(pri, call, operation, arguments);
590 + return q931_facility(pri, call);
593 +int pri_deflect(struct pri *pri, q931_call *call, char *destination)
595 + add_call_deflection_facility_ie(pri, call, destination);
596 + return q931_facility(pri, call);
600 /* deprecated routines, use pri_hangup */
601 int pri_release(struct pri *pri, q931_call *call, int cause)
602 @@ -543,12 +636,29 @@
604 int pri_hangup(struct pri *pri, q931_call *call, int cause)
610 /* normal clear cause */
612 - return q931_hangup(pri, call, cause);
613 + if ((cause == 34 || cause == 44 || cause == 82 || cause == 1 || cause == 81 || cause == 17) && (call->ourcallstate == Q931_CALL_STATE_ACTIVE)) {
614 + pri_error(pri, "Cause code %d not allowed when disconnecting an active call. Changing to cause 16.\n", cause);
618 + if (pri->localtype == BRI_NETWORK_PTMP) {
619 + res = q921_hangup(pri, call, 127);
621 + // q921_setup might give a HANGUP_ACK, if nobody got the call
622 + q931_hangup(pri, call, cause);
625 + return q931_hangup(pri, call, cause);
628 + return q931_hangup(pri, call, cause);
632 int pri_reset(struct pri *pri, int channel)
633 @@ -688,15 +798,15 @@
634 return q931_setup(pri, c, &req);
637 -static void (*__pri_error)(struct pri *pri, char *stuff);
638 -static void (*__pri_message)(struct pri *pri, char *stuff);
639 +static void (*__pri_error)(char *stuff,int span);
640 +static void (*__pri_message)(char *stuff,int span);
642 -void pri_set_message(void (*func)(struct pri *pri, char *stuff))
643 +void pri_set_message(void (*func)(char *stuff,int span))
645 __pri_message = func;
648 -void pri_set_error(void (*func)(struct pri *pri, char *stuff))
649 +void pri_set_error(void (*func)(char *stuff,int span))
653 @@ -708,10 +818,14 @@
655 vsnprintf(tmp, sizeof(tmp), fmt, ap);
658 - __pri_message(pri, tmp);
660 - fputs(tmp, stdout);
661 + if (__pri_message && pri) {
662 + if (pri->debugfd >= 0)
663 + write(pri->debugfd, tmp, strlen(tmp));
665 + __pri_message(tmp, pri->span);
667 + fputs(tmp, stdout);
671 void pri_error(struct pri *pri, char *fmt, ...)
672 @@ -721,10 +835,14 @@
674 vsnprintf(tmp, sizeof(tmp), fmt, ap);
677 - __pri_error(pri, tmp);
679 - fputs(tmp, stderr);
680 + if (__pri_error && pri) {
681 + if (pri->debugfd >= 0)
682 + write(pri->debugfd, tmp, strlen(tmp));
684 + __pri_error(tmp, pri->span);
686 + fputs(tmp, stderr);
690 /* Set overlap mode */
691 @@ -765,11 +883,13 @@
693 len += sprintf(buf + len, "Q921 Outstanding: %d\n", q921outstanding);
695 - len += sprintf(buf + len, "Window Length: %d/%d\n", pri->windowlen, pri->window);
696 - len += sprintf(buf + len, "Sentrej: %d\n", pri->sentrej);
697 - len += sprintf(buf + len, "SolicitFbit: %d\n", pri->solicitfbit);
698 - len += sprintf(buf + len, "Retrans: %d\n", pri->retrans);
699 - len += sprintf(buf + len, "Busy: %d\n", pri->busy);
700 + if (pri->localtype != BRI_NETWORK_PTMP) {
701 + len += sprintf(buf + len, "Window Length: %d/%d\n", pri->windowlen[0], pri->window[0]);
702 + len += sprintf(buf + len, "Sentrej: %d\n", pri->sentrej[0]);
703 + len += sprintf(buf + len, "SolicitFbit: %d\n", pri->solicitfbit[0]);
704 + len += sprintf(buf + len, "Retrans: %d\n", pri->retrans[0]);
705 + len += sprintf(buf + len, "Busy: %d\n", pri->busy[0]);
707 len += sprintf(buf + len, "Overlap Dial: %d\n", pri->overlapdial);
708 len += sprintf(buf + len, "T200 Timer: %d\n", pri->timers[PRI_TIMER_T200]);
709 len += sprintf(buf + len, "T203 Timer: %d\n", pri->timers[PRI_TIMER_T203]);
711 len += sprintf(buf + len, "T313 Timer: %d\n", pri->timers[PRI_TIMER_T313]);
712 len += sprintf(buf + len, "N200 Counter: %d\n", pri->timers[PRI_TIMER_N200]);
719 sr->redirectingreason = reason;
723 +void pri_shutdown(struct pri *pri)
725 + if ((pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_CPE_PTMP)) {
726 + q921_reset(pri, pri->tei, 1);
729 diff -urN libpri-1.2.2.orig/pri_facility.c libpri-1.2.2/pri_facility.c
730 --- libpri-1.2.2.orig/pri_facility.c 2005-11-29 19:39:18.000000000 +0100
731 +++ libpri-1.2.2/pri_facility.c 2006-01-19 11:55:21.000000000 +0100
734 - * libpri: An implementation of Primary Rate ISDN
736 - * Written by Matthew Fredrickson <creslin@digium.com>
738 - * Copyright (C) 2004-2005, Digium
739 - * All Rights Reserved.
741 - * This program is free software; you can redistribute it and/or modify
742 - * it under the terms of the GNU General Public License as published by
743 - * the Free Software Foundation; either version 2 of the License, or
744 - * (at your option) any later version.
746 - * This program is distributed in the hope that it will be useful,
747 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
748 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
749 - * GNU General Public License for more details.
751 - * You should have received a copy of the GNU General Public License
752 - * along with this program; if not, write to the Free Software
753 - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
757 + This file and it's contents are licensed under the terms and conditions
758 + of the GNU Public License. See http://www.gnu.org for details.
760 + Routines for dealing with facility messages and their respective
763 + by Matthew Fredrickson <creslin@digium.com>
764 + Copyright (C) 2004-2005 Digium, Inc
770 if (datalen > buflen) {
773 + memcpy(namebuf, comp->data, datalen);
775 - memcpy(namebuf, comp->data, datalen);
780 int asn1_string_encode(unsigned char asn1_type, void *data, int len, int max_len, void *src, int src_len)
790 @@ -375,11 +362,10 @@
791 pri_message(pri, "!! Unknown Party number component received 0x%X\n", comp->type);
794 - ASN1_FIXUP_LEN(comp, res);
795 NEXT_COMPONENT(comp, i);
797 pri_message(pri, "!! not all information is handled from Address component\n");
804 static int rose_presented_number_unscreened_decode(struct pri *pri, q931_call *call, unsigned char *data, int len, struct addressingdataelements_presentednumberunscreened *value)
808 struct rose_component *comp = NULL;
809 unsigned char *vdata = data;
813 case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_0): /* [0] presentationAllowedNumber */
814 value->pres = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
815 - size = rose_address_decode(pri, call, comp->data, comp->len, value);
816 - ASN1_FIXUP_LEN(comp, size);
818 + return rose_address_decode(pri, call, comp->data, comp->len, value) + 2;
819 case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_1): /* [1] IMPLICIT presentationRestricted */
820 if (comp->len != 0) { /* must be NULL */
821 pri_error(pri, "!! Invalid PresentationRestricted component received (len != 0)\n");
824 case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3): /* [3] presentationRestrictedNumber */
825 value->pres = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
826 - size = rose_address_decode(pri, call, comp->data, comp->len, value) + 2;
827 - ASN1_FIXUP_LEN(comp, size);
829 + return rose_address_decode(pri, call, comp->data, comp->len, value) + 2;
831 pri_message(pri, "Invalid PresentedNumberUnscreened component 0x%X\n", comp->type);
837 -static int rose_diverting_leg_information2_decode(struct pri *pri, q931_call *call, struct rose_component *sequence, int len)
838 +static int rose_diverting_leg_information2_decode(struct pri *pri, q931_call *call, unsigned char *data, int len)
841 int diversion_counter;
843 struct addressingdataelements_presentednumberunscreened divertingnr;
844 struct addressingdataelements_presentednumberunscreened originalcallednr;
845 struct rose_component *comp = NULL;
846 - unsigned char *vdata = sequence->data;
847 + unsigned char *vdata = data;
851 - if (sequence->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
852 - pri_message(pri, "Invalid DivertingLegInformation2Type argument\n");
856 - if (sequence->len == ASN1_LEN_INDEF) {
857 - len -= 4; /* For the 2 extra characters at the end
858 - * and two characters of header */
863 /* diversionCounter stuff */
864 GET_COMPONENT(comp, i, vdata, len);
865 @@ -477,20 +446,18 @@
867 if(pri->debug & PRI_DEBUG_APDU)
868 pri_message(pri, " Redirection reason: %d, total diversions: %d\n", diversion_reason, diversion_counter);
869 - pri_message(NULL, "Length of message is %d\n", len);
871 for(; i < len; NEXT_COMPONENT(comp, i)) {
872 GET_COMPONENT(comp, i, vdata, len);
873 - switch(comp->type) {
874 - case (ASN1_CONTEXT_SPECIFIC | ASN1_TAG_0):
875 + switch(comp->type & ASN1_TYPE_MASK) {
877 call->origredirectingreason = redirectingreason_for_q931(pri, comp->data[0]);
878 if (pri->debug & PRI_DEBUG_APDU)
879 pri_message(pri, " Received reason for original redirection %d\n", call->origredirectingreason);
881 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_1):
882 + case ASN1_TAG_1: /* divertingnr: presentednumberunscreened */
883 res = rose_presented_number_unscreened_decode(pri, call, comp->data, comp->len, &divertingnr);
884 /* TODO: Fix indefinite length form hacks */
885 - ASN1_FIXUP_LEN(comp, res);
889 @@ -499,64 +466,62 @@
890 pri_message(pri, " ton = %d, pres = %d, npi = %d\n", divertingnr.ton, divertingnr.pres, divertingnr.npi);
893 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_2):
894 + case ASN1_TAG_2: /* originalCalledNr: PresentedNumberUnscreened */
895 res = rose_presented_number_unscreened_decode(pri, call, comp->data, comp->len, &originalcallednr);
898 - ASN1_FIXUP_LEN(comp, res);
900 if (pri->debug & PRI_DEBUG_APDU) {
901 pri_message(pri, " Received originalcallednr '%s'\n", originalcallednr.partyaddress);
902 pri_message(pri, " ton = %d, pres = %d, npi = %d\n", originalcallednr.ton, originalcallednr.pres, originalcallednr.npi);
905 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_3):
906 - res = asn1_name_decode(comp->data, comp->len, redirectingname, sizeof(redirectingname));
909 - ASN1_FIXUP_LEN(comp, res);
912 + comp->len = asn1_name_decode(comp->data, comp->len, redirectingname, sizeof(redirectingname));
913 if (pri->debug & PRI_DEBUG_APDU)
914 pri_message(pri, " Received RedirectingName '%s'\n", redirectingname);
916 - case (ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTOR | ASN1_TAG_4):
917 - res = asn1_name_decode(comp->data, comp->len, origcalledname, sizeof(origcalledname));
920 - ASN1_FIXUP_LEN(comp, res);
923 + comp->len = asn1_name_decode(comp->data, comp->len, origcalledname, sizeof(origcalledname));
924 if (pri->debug & PRI_DEBUG_APDU)
925 pri_message(pri, " Received Originally Called Name '%s'\n", origcalledname);
928 - if (comp->type == 0 && comp->len == 0) {
929 - break; /* Found termination characters */
931 pri_message(pri, "!! Invalid DivertingLegInformation2 component received 0x%X\n", comp->type);
936 + return -1; /* Aborted before */
938 if (divertingnr.pres >= 0) {
939 call->redirectingplan = divertingnr.npi;
940 call->redirectingpres = divertingnr.pres;
941 call->redirectingreason = diversion_reason;
942 - libpri_copy_string(call->redirectingnum, divertingnr.partyaddress, sizeof(call->redirectingnum));
943 + strncpy(call->redirectingnum, divertingnr.partyaddress, sizeof(call->redirectingnum)-1);
944 + call->redirectingnum[sizeof(call->redirectingnum)-1] = '\0';
946 if (originalcallednr.pres >= 0) {
947 call->origcalledplan = originalcallednr.npi;
948 call->origcalledpres = originalcallednr.pres;
949 - libpri_copy_string(call->origcallednum, originalcallednr.partyaddress, sizeof(call->origcallednum));
950 + strncpy(call->origcallednum, originalcallednr.partyaddress, sizeof(call->origcallednum)-1);
951 + call->origcallednum[sizeof(call->origcallednum)-1] = '\0';
953 + if (strlen(redirectingname) > 0) {
954 + strncpy(call->redirectingname, redirectingname, sizeof(call->redirectingname));
955 + call->redirectingname[sizeof(call->redirectingname)-1] = '\0';
957 + if (strlen(origcalledname) > 0) {
958 + strncpy(call->origcalledname, origcalledname, sizeof(call->origcalledname));
959 + call->origcalledname[sizeof(call->origcalledname)-1] = '\0';
961 - libpri_copy_string(call->redirectingname, redirectingname, sizeof(call->redirectingname));
962 - libpri_copy_string(call->origcalledname, origcalledname, sizeof(call->origcalledname));
971 static int rose_diverting_leg_information2_encode(struct pri *pri, q931_call *call)
973 int i = 0, j, compsp = 0;
978 +/* Call deflection */
979 +int add_call_deflection_facility_ie(struct pri *pri, q931_call *c, char *destination) {
980 + int i = 0, j, compsp = 0;
981 + struct rose_component *comp, *compstk[10];
982 + unsigned char buffer[256];
984 + buffer[i++] = (ASN1_CONTEXT_SPECIFIC | Q932_PROTOCOL_ROSE);
986 + ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, buffer, i);
987 + ASN1_PUSH(compstk, compsp, comp);
989 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, get_invokeid(pri));
990 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, buffer, i, ROSE_CALLDEFLECTION);
992 + /* Argument sequence */
993 + ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
994 + ASN1_PUSH(compstk, compsp, comp);
997 + ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE), buffer, i);
998 + ASN1_PUSH(compstk, compsp, comp);
1000 +#ifndef CD_UNLIKE_IN_CAPI
1001 + /* arg.address.PartyNumber */
1004 + j = asn1_string_encode((ASN1_CONTEXT_SPECIFIC|ASN1_TAG_0), &buffer[i], sizeof(buffer)-i, 20, destination, strlen(destination));
1005 + if (j<0) return -1;
1008 + /* using PublicPartyNumber instead */
1009 + ASN1_ADD_SIMPLE(comp, (ASN1_CONSTRUCTOR | ASN1_SEQUENCE | ASN1_CONTEXT_SPECIFIC| ASN1_TAG_1), buffer, i);
1010 + ASN1_PUSH(compstk, compsp, comp);
1011 + /* ToN: unknown */
1012 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, buffer, i, 0);
1013 + j = asn1_string_encode(0x80, &buffer[i], sizeof(buffer)-i, 20, destination, strlen(destination));
1014 + if(j<0) return -1;
1016 + /* close PublicPartyNumber */
1017 + ASN1_FIXUP(compstk, compsp, buffer, i);
1020 + /* close Address */
1021 + ASN1_FIXUP(compstk, compsp, buffer, i);
1024 + ASN1_ADD_BYTECOMP(comp, ASN1_BOOLEAN, buffer, i, 0);
1026 + /* Fix length of stacked components */
1027 + while(compsp > 0) {
1028 + ASN1_FIXUP(compstk, compsp, buffer, i);
1030 + if (pri_call_apdu_queue(c, Q931_FACILITY, buffer, i, NULL, NULL))
1036 /* Sending callername information functions */
1037 static int add_callername_facility_ies(struct pri *pri, q931_call *c, int cpe)
1039 @@ -1132,6 +1155,7 @@
1041 unsigned char *vdata = data;
1042 struct rose_component *comp = NULL, *invokeid = NULL, *operationid = NULL;
1043 + struct addressingdataelements_presentednumberunscreened value;
1046 /* Invoke ID stuff */
1047 @@ -1148,13 +1172,15 @@
1048 NEXT_COMPONENT(comp, i);
1050 /* No argument - return with error */
1052 + if ((i >= len) && (operation_tag != ROSE_EXPLICIT_CALL_TRANSFER))
1055 - /* Arguement Tag */
1056 - GET_COMPONENT(comp, i, vdata, len);
1059 + if (operation_tag != ROSE_EXPLICIT_CALL_TRANSFER) {
1060 + /* Arguement Tag */
1061 + GET_COMPONENT(comp, i, vdata, len);
1066 if (pri->debug & PRI_DEBUG_APDU)
1067 pri_message(pri, " [ Handling operation %d ]\n", operation_tag);
1068 @@ -1178,7 +1204,11 @@
1069 case ROSE_DIVERTING_LEG_INFORMATION2:
1070 if (pri->debug & PRI_DEBUG_APDU)
1071 pri_message(pri, " Handle DivertingLegInformation2\n");
1072 - return rose_diverting_leg_information2_decode(pri, call, comp, len-i);
1073 + if (comp->type != (ASN1_CONSTRUCTOR | ASN1_SEQUENCE)) { /* Constructed Sequence */
1074 + pri_message(pri, "Invalid DivertingLegInformation2Type argument\n");
1077 + return rose_diverting_leg_information2_decode(pri, call, comp->data, comp->len);
1078 case ROSE_AOC_NO_CHARGING_INFO_AVAILABLE:
1079 if (pri->debug & PRI_DEBUG_APDU) {
1080 pri_message(pri, "ROSE %i: AOC No Charging Info Available - not handled!", operation_tag);
1081 @@ -1229,6 +1259,34 @@
1082 dump_apdu (pri, (u_int8_t *)comp, comp->len + 2);
1085 + case ROSE_CALLDEFLECTION:
1086 + // if (pri->debug & PRI_DEBUG_APDU) {
1087 + pri_message(pri, "ROSE %i: CD - not handled!", operation_tag);
1088 + dump_apdu (pri, comp->data, comp->len);
1089 + rose_public_party_number_decode(pri, call, comp->data + 2 , comp->len - 2, &value);
1090 + pri_message(pri, "party address %s\n", value.partyaddress);
1092 +/* GET_COMPONENT(comp, i, vdata, len);
1093 + if (comp->type == ASN1_SEQUENCE | ASN1_CONSTRUCTOR | ASN1_TAG_0) {
1095 + for (j=0;j<comp->len;j++) {
1096 + pri_error(pri, "comp->data %#x\n",comp->data[j]);
1099 + pri_error(pri, "c type %#x, c len %#x\n", comp->type, comp->len);
1100 + if (comp->type == ASN1_SEQUENCE | ASN1_CONSTRUCTOR) {
1101 + pri_error(pri, "c type %#x, c len %#x\n", comp->type, comp->len);
1107 + case ROSE_EXPLICIT_CALL_TRANSFER:
1108 + call->facility = operation_tag;
1109 + if (pri->debug & PRI_DEBUG_APDU) {
1110 + pri_message(pri, "ROSE %i: received ECT execute!", operation_tag);
1114 if (pri->debug & PRI_DEBUG_APDU) {
1115 pri_message(pri, "!! Unable to handle ROSE operation %d", operation_tag);
1116 diff -urN libpri-1.2.2.orig/pri_facility.h libpri-1.2.2/pri_facility.h
1117 --- libpri-1.2.2.orig/pri_facility.h 2005-11-29 19:39:18.000000000 +0100
1118 +++ libpri-1.2.2/pri_facility.h 2006-01-19 11:51:18.000000000 +0100
1120 /* Operation ID values */
1121 /* Q.952 ROSE operations (Diverting) */
1122 #define ROSE_DIVERTING_LEG_INFORMATION1 18
1123 -#define ROSE_DIVERTING_LEG_INFORMATION2 0x15
1124 +#define ROSE_DIVERTING_LEG_INFORMATION2 15
1125 #define ROSE_DIVERTING_LEG_INFORMATION3 19
1126 /* Q.956 ROSE operations (Advice Of Charge) */
1127 #define ROSE_AOC_NO_CHARGING_INFO_AVAILABLE 26
1129 #define ROSE_AOC_IDENTIFICATION_OF_CHARGE 37
1130 /* Q.SIG operations */
1131 #define SS_CNID_CALLINGNAME 0
1132 -#define SS_DIVERTING_LEG_INFORMATION2 21
1133 +#define SS_DIVERTING_LEG_INFORMATION2 22
1134 #define SS_MWI_ACTIVATE 80
1135 #define SS_MWI_DEACTIVATE 81
1136 #define SS_MWI_INTERROGATE 82
1138 +#define ROSE_CALLDEFLECTION 0x0D
1139 +#define ROSE_EXPLICIT_CALL_TRANSFER 0x06
1141 /* ROSE definitions and data structures */
1142 #define INVOKE_IDENTIFIER 0x02
1143 #define INVOKE_LINKED_IDENTIFIER 0x80
1144 @@ -180,12 +183,6 @@
1145 (variable) = ((variable) << 8) | (component)->data[comp_idx]; \
1148 -#define ASN1_FIXUP_LEN(component, size) \
1150 - if ((component)->len == ASN1_LEN_INDEF) \
1154 #define ASN1_ADD_SIMPLE(component, comptype, ptr, idx) \
1156 (component) = (struct rose_component *)&((ptr)[(idx)]); \
1158 /* Adds the "standard" ADPUs to a call */
1159 extern int pri_call_add_standard_apdus(struct pri *pri, q931_call *call);
1161 +extern int add_call_deflection_facility_ie(struct pri *pri, q931_call *c, char *destination);
1163 #endif /* _PRI_FACILITY_H */
1164 diff -urN libpri-1.2.2.orig/pri_internal.h libpri-1.2.2/pri_internal.h
1165 --- libpri-1.2.2.orig/pri_internal.h 2005-11-29 19:39:18.000000000 +0100
1166 +++ libpri-1.2.2/pri_internal.h 2006-01-18 12:28:07.000000000 +0100
1169 struct timeval when;
1170 void (*callback)(void *data);
1171 + void (*callback2)(void *data, int);
1181 /* No more than 128 scheduled events */
1182 +/* XXX is this sufficient for nfs ??? */
1183 #define MAX_SCHED 128
1185 +/* this can be freely configured to support more devices .... ok, 63 would be max! */
1186 +#define Q921_MAX_TEIS 16
1188 +/* dynamically allocated TEIs start here */
1189 +#define Q921_TEI_BASE 64
1191 #define MAX_TIMERS 32
1195 struct pri *master; /* Master channel if appropriate */
1196 struct pri_sched pri_sched[MAX_SCHED]; /* Scheduled events */
1197 int debug; /* Debug stuff */
1199 int state; /* State of D-channel */
1200 int switchtype; /* Switch type */
1201 int nsf; /* Network-Specific Facility (if any) */
1207 - int window; /* Max window size */
1208 - int windowlen; /* Fullness of window */
1209 - int v_s; /* Next N(S) for transmission */
1210 - int v_a; /* Last acknowledged frame */
1211 - int v_r; /* Next frame expected to be received */
1212 - int v_na; /* What we've told our peer we've acknowledged */
1213 - int solicitfbit; /* Have we sent an I or S frame with the F-bit set? */
1214 - int retrans; /* Retransmissions */
1215 - int sentrej; /* Are we in reject state */
1217 + int q921_state[Q921_MAX_TEIS];
1220 + /* TEI registry */
1221 + char q921_teis[Q921_MAX_TEIS];
1223 + char q921_tei_check[Q921_MAX_TEIS];
1224 + unsigned short q921_tei_check_ri[Q921_MAX_TEIS];
1228 + int busy[Q921_MAX_TEIS]; /* Peer is busy */
1230 + int window[Q921_MAX_TEIS]; /* Max window size */
1231 + int windowlen[Q921_MAX_TEIS]; /* Fullness of window */
1232 + int v_s[Q921_MAX_TEIS]; /* Next N(S) for transmission */
1233 + int v_a[Q921_MAX_TEIS]; /* Last acknowledged frame */
1234 + int v_r[Q921_MAX_TEIS]; /* Next frame expected to be received */
1235 + int v_na[Q921_MAX_TEIS]; /* What we've told our peer we've acknowledged */
1236 + int solicitfbit[Q921_MAX_TEIS]; /* Have we sent an I or S frame with the F-bit set? */
1237 + int retrans[Q921_MAX_TEIS]; /* Retransmissions */
1238 + int sabme_retrans[Q921_MAX_TEIS]; /* Retransmissions */
1240 + int sentrej[Q921_MAX_TEIS]; /* Are we in reject state */
1242 + /* Various timers */
1243 + int sabme_timer[Q921_MAX_TEIS];
1244 + int t203_timer[Q921_MAX_TEIS];
1245 + int t202_timer[Q921_MAX_TEIS];
1247 + int t201_timer[Q921_MAX_TEIS];
1248 + int t200_timer[Q921_MAX_TEIS];
1251 int cref; /* Next call reference value */
1253 - int busy; /* Peer is busy */
1255 - /* Various timers */
1256 - int sabme_timer; /* SABME retransmit */
1257 - int t203_timer; /* Max idle time */
1258 - int t200_timer; /* T-200 retransmission timer */
1259 /* All ISDN Timer values */
1260 int timers[MAX_TIMERS];
1264 pri_event ev; /* Static event thingy */
1266 - /* Q.921 Re-transmission queue */
1267 - struct q921_frame *txqueue;
1268 + /* Q.921 (Re)transmission queue */
1269 + struct q921_frame *txqueue[Q921_MAX_TEIS];
1272 q931_call **callpool;
1275 unsigned char last_invoke; /* Last ROSE invoke ID */
1276 unsigned char sendfacility;
1278 + int span; /* our fellow pri lives on this zaptel span */
1287 + int callerplanani;
1291 @@ -167,8 +199,13 @@
1293 int alive; /* Whether or not the call is alive */
1294 int acked; /* Whether setup has been acked or not */
1295 + int con_acked; /* Whether CONNECT has been CONNECT_ACKNOWLEDGEd or not */
1296 int sendhangupack; /* Whether or not to send a hangup ack */
1297 int proc; /* Whether we've sent a call proceeding / alerting */
1298 + int alert; /* Whether we've sent an alerting */
1301 + q921_call *phones;
1303 int ri; /* Restart Indicator (Restart Indicator IE) */
1305 @@ -202,15 +239,18 @@
1308 int callerpres; /* Caller presentation */
1309 - char callerani[256]; /* Caller */
1310 - char callernum[256];
1311 + int callerplanuser;
1312 + int callerpresuser; /* Caller presentation */
1313 + char callernum[256]; /* Calling Number (network provided) */
1314 + char callerani[256]; /* Calling Number, (user provided) */
1316 char callername[256];
1318 - char digitbuf[64]; /* Buffer for digits that come in KEYPAD_FACILITY */
1319 + char digitbuf[64]; /* Buffer for digits that come in KEYPAD_FACILITY */
1321 int ani2; /* ANI II */
1326 char callednum[256]; /* Called Number */
1327 int complete; /* no more digits coming */
1328 @@ -225,16 +265,27 @@
1329 char redirectingnum[256]; /* Number of redirecting party */
1330 char redirectingname[256]; /* Name of redirecting party */
1335 /* Filled in cases of multiple diversions */
1338 - int origredirectingreason; /* Original reason for redirect (in cases of multiple redirects) */
1339 + int origredirectingreason; /* Original reason for redirect (in cases of multiple redirects) */
1340 char origcalledname[256]; /* Original name of person being called */
1341 - char origcallednum[256]; /* Orignal number of person being called */
1342 + char origcallednum[256]; /* Orignal number of person being called */
1344 - int useruserprotocoldisc;
1345 + int useruserprotocoldisc;
1346 char useruserinfo[256];
1347 char callingsubaddr[256]; /* Calling parties sub address */
1349 + char callid[10]; /* call identity for SUSPEND/RESUME */
1350 + char digits[256]; /* additional digits received via info msgs (cpn or keypad) */
1351 + char display[256]; /* display ie received in info msgs or for sending */
1353 + /* euroisdn facility fun */
1354 + int facility; /* FACILTIY received */
1357 long aoc_units; /* Advice of Charge Units */
1362 extern int pri_schedule_event(struct pri *pri, int ms, void (*function)(void *data), void *data);
1363 +extern int pri_schedule_event2(struct pri *pri, int ms, void (*function)(void *data, int data2), void *data, int data2);
1365 extern pri_event *pri_schedule_run(struct pri *pri);
1368 extern pri_event *pri_mkerror(struct pri *pri, char *errstr);
1370 extern void pri_message(struct pri *pri, char *fmt, ...);
1373 extern void pri_error(struct pri *pri, char *fmt, ...);
1375 void libpri_copy_string(char *dst, const char *src, size_t size);
1376 diff -urN libpri-1.2.2.orig/pri_q921.h libpri-1.2.2/pri_q921.h
1377 --- libpri-1.2.2.orig/pri_q921.h 2005-11-29 19:39:18.000000000 +0100
1378 +++ libpri-1.2.2/pri_q921.h 2006-01-18 12:28:07.000000000 +0100
1380 #define Q921_FRAMETYPE_S 0x1
1382 #define Q921_TEI_GROUP 127
1383 +#define Q921_TEI_ID_REQUEST 0x1
1384 +#define Q921_TEI_ID_ASSIGNED 0x2
1385 +#define Q921_TEI_ID_DENIED 0x3
1386 +#define Q921_TEI_ID_CHK_REQ 0x4
1387 +#define Q921_TEI_ID_CHK_RES 0x5
1388 +#define Q921_TEI_ID_REMOVE 0x6
1389 +#define Q921_TEI_ID_VERIFY 0x7
1390 #define Q921_TEI_GR303_EOC_PATH 0
1391 #define Q921_TEI_GR303_EOC_OPS 4
1392 #define Q921_TEI_GR303_TMC_SWITCHING 0
1393 @@ -164,12 +171,14 @@
1394 extern void q921_dump(struct pri *pri, q921_h *h, int len, int showraw, int txrx);
1396 /* Bring up the D-channel */
1397 -extern void q921_start(struct pri *pri, int now);
1398 +extern void q921_start(struct pri *pri, int now, int tei);
1400 -extern void q921_reset(struct pri *pri);
1401 +extern void q921_reset(struct pri *pri, int tei, int discard);
1403 extern pri_event *q921_receive(struct pri *pri, q921_h *h, int len);
1405 -extern int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr);
1406 +extern int q921_transmit_uframe(struct pri *pri, void *buf, int len, int cr, int tei);
1408 +extern int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr, int tei);
1411 diff -urN libpri-1.2.2.orig/pri_q931.h libpri-1.2.2/pri_q931.h
1412 --- libpri-1.2.2.orig/pri_q931.h 2005-11-29 19:39:18.000000000 +0100
1413 +++ libpri-1.2.2/pri_q931.h 2006-01-18 12:28:07.000000000 +0100
1414 @@ -190,6 +190,10 @@
1415 #define Q931_IE_CODESET(x) ((x) >> 8)
1416 #define Q931_IE_IE(x) ((x) & 0xff)
1417 #define Q931_FULL_IE(codeset, ie) (((codeset) << 8) | ((ie) & 0xff))
1418 +#define Q931_IE_MAX_LEN 257
1421 +#define Q931_COLP 0x4c
1423 #define Q931_DISPLAY 0x28
1424 #define Q931_IE_SEGMENTED_MSG 0x00
1426 #define Q931_IE_USER_USER 0x7E
1427 #define Q931_IE_ESCAPE_FOR_EXT 0x7F
1429 +#define Q931_IE_SPECIAL 0x02
1432 /* Call state stuff */
1433 #define Q931_CALL_STATE_NULL 0
1436 #define Q931_SENDING_COMPLETE 0xa1
1438 -extern int q931_receive(struct pri *pri, q931_h *h, int len);
1439 +extern int q931_receive(struct pri *pri, q931_h *h, int len, int tei);
1441 extern int q931_alerting(struct pri *pri, q931_call *call, int channel, int info);
1443 @@ -257,6 +263,10 @@
1445 extern int q931_information(struct pri *pri, q931_call *call, char digit);
1447 +extern int q931_information_display(struct pri *pri, q931_call *call, char *display);
1449 +extern int q931_add_display(struct pri *pri, q931_call *call, char *display);
1451 extern int q931_connect(struct pri *pri, q931_call *call, int channel, int nonisdn);
1453 extern int q931_release(struct pri *pri, q931_call *call, int cause);
1454 @@ -265,6 +275,10 @@
1456 extern int q931_hangup(struct pri *pri, q931_call *call, int cause);
1458 +extern int q921_hangup(struct pri *pri, q931_call *c, int tei);
1460 +extern int q921_handle_hangup(struct pri *pri, q931_call *c, int tei);
1462 extern int q931_restart(struct pri *pri, int channel);
1464 extern int q931_facility(struct pri *pri, q931_call *call);
1465 @@ -279,5 +293,23 @@
1466 extern void q931_dump(struct pri *pri, q931_h *h, int len, int txrx);
1468 extern void __q931_destroycall(struct pri *pri, q931_call *c);
1470 +extern int q931_hold_acknowledge(struct pri *pri, q931_call *c);
1472 +extern int q931_hold_reject(struct pri *pri, q931_call *c);
1474 +extern int q931_retrieve_acknowledge(struct pri *pri, q931_call *c, int channel);
1476 +extern int q931_retrieve_reject(struct pri *pri, q931_call *c);
1478 +extern int q931_suspend_acknowledge(struct pri *pri, q931_call *c, char *display);
1480 +extern int q931_suspend_reject(struct pri *pri, q931_call *c, char *display);
1482 +extern int q931_resume_reject(struct pri *pri, q931_call *c, char *display);
1484 +extern int q931_resume_acknowledge(struct pri *pri, q931_call *c, int channel, char *display);
1486 +//extern int q931_facility(struct pri *pri, q931_call *c, int operation, char *arguments);
1489 diff -urN libpri-1.2.2.orig/pri_timers.h libpri-1.2.2/pri_timers.h
1490 --- libpri-1.2.2.orig/pri_timers.h 2005-11-29 19:39:18.000000000 +0100
1491 +++ libpri-1.2.2/pri_timers.h 2006-01-18 12:28:07.000000000 +0100
1494 /* -1 means we dont currently support the timer/counter */
1495 #define PRI_TIMERS_DEFAULT { 3, /* N200 */ \
1504 + 2000, /* T201 */ \
1505 + 5000, /* T202 */ \
1511 + 4000, /* T303 */ \
1515 diff -urN libpri-1.2.2.orig/pridump.c libpri-1.2.2/pridump.c
1516 --- libpri-1.2.2.orig/pridump.c 2005-11-29 19:39:18.000000000 +0100
1517 +++ libpri-1.2.2/pridump.c 2006-01-18 12:28:07.000000000 +0100
1520 * libpri: An implementation of Primary Rate ISDN
1522 - * Written by Mark Spencer <markster@digium.com>
1523 + * Written by Mark Spencer <markster@linux-support.net>
1525 - * Copyright (C) 2001-2005, Digium
1526 + * Copyright (C) 2001, Linux Support Services, Inc.
1527 * All Rights Reserved.
1529 * This program is free software; you can redistribute it and/or modify
1530 diff -urN libpri-1.2.2.orig/prisched.c libpri-1.2.2/prisched.c
1531 --- libpri-1.2.2.orig/prisched.c 2005-11-29 19:39:18.000000000 +0100
1532 +++ libpri-1.2.2/prisched.c 2006-01-18 12:28:07.000000000 +0100
1535 * libpri: An implementation of Primary Rate ISDN
1537 - * Written by Mark Spencer <markster@digium.com>
1538 + * Written by Mark Spencer <markster@linux-support.net>
1540 - * Copyright (C) 2001-2005, Digium
1541 + * Copyright (C) 2001, Linux Support Services, Inc.
1542 * All Rights Reserved.
1544 * This program is free software; you can redistribute it and/or modify
1552 #include "pri_internal.h"
1556 static int maxsched = 0;
1560 for (x=1;x<MAX_SCHED;x++)
1561 - if (!pri->pri_sched[x].callback)
1562 + if ((!pri->pri_sched[x].callback2) && (!pri->pri_sched[x].callback))
1564 if (x == MAX_SCHED) {
1565 pri_error(pri, "No more room in scheduler\n");
1568 pri->pri_sched[x].when = tv;
1569 pri->pri_sched[x].callback = function;
1570 + pri->pri_sched[x].callback2 = NULL;
1571 pri->pri_sched[x].data = data;
1572 + pri->pri_sched[x].hasdata2 = 0;
1573 + pri->pri_sched[x].data2 = 0;
1577 +int pri_schedule_event2(struct pri *pri, int ms, void (*function)(void *data, int data2), void *data, int data2)
1580 + struct timeval tv;
1581 + for (x=1;x<MAX_SCHED;x++)
1582 + if ((!pri->pri_sched[x].callback2) && (!pri->pri_sched[x].callback))
1584 + if (x == MAX_SCHED) {
1585 + pri_error(pri, "No more room in scheduler\n");
1590 + gettimeofday(&tv, NULL);
1591 + tv.tv_sec += ms / 1000;
1592 + tv.tv_usec += (ms % 1000) * 1000;
1593 + if (tv.tv_usec > 1000000) {
1594 + tv.tv_usec -= 1000000;
1597 + pri->pri_sched[x].when = tv;
1598 + pri->pri_sched[x].callback = NULL;
1599 + pri->pri_sched[x].callback2 = function;
1600 + pri->pri_sched[x].data = data;
1601 + pri->pri_sched[x].hasdata2 = 1;
1602 + pri->pri_sched[x].data2 = data2;
1607 if (pri->subchannel)
1608 closest = pri_schedule_next(pri->subchannel);
1609 for (x=1;x<MAX_SCHED;x++) {
1610 - if (pri->pri_sched[x].callback &&
1611 + if ((pri->pri_sched[x].callback || pri->pri_sched[x].callback2) &&
1612 (!closest || (closest->tv_sec > pri->pri_sched[x].when.tv_sec) ||
1613 ((closest->tv_sec == pri->pri_sched[x].when.tv_sec) &&
1614 (closest->tv_usec > pri->pri_sched[x].when.tv_usec))))
1615 @@ -76,26 +107,38 @@
1617 static pri_event *__pri_schedule_run(struct pri *pri, struct timeval *tv)
1620 - void (*callback)(void *);
1623 + void (*callback)(void *);
1624 + void (*callback2)(void *, int);
1629 if (pri->subchannel) {
1630 if ((e = __pri_schedule_run(pri->subchannel, tv))) {
1634 for (x=1;x<MAX_SCHED;x++) {
1635 - if (pri->pri_sched[x].callback &&
1636 + if ((pri->pri_sched[x].callback || pri->pri_sched[x].callback2) &&
1637 ((pri->pri_sched[x].when.tv_sec < tv->tv_sec) ||
1638 ((pri->pri_sched[x].when.tv_sec == tv->tv_sec) &&
1639 (pri->pri_sched[x].when.tv_usec <= tv->tv_usec)))) {
1641 callback = pri->pri_sched[x].callback;
1642 + callback2 = pri->pri_sched[x].callback2;
1643 data = pri->pri_sched[x].data;
1644 + data2 = pri->pri_sched[x].data2;
1645 pri->pri_sched[x].callback = NULL;
1646 + pri->pri_sched[x].callback2 = NULL;
1647 pri->pri_sched[x].data = NULL;
1649 + pri->pri_sched[x].data2 = 0;
1650 + if (pri->pri_sched[x].hasdata2 == 1) {
1651 + pri->pri_sched[x].hasdata2 = 0;
1652 + callback2(data, data2);
1660 if ((id >= MAX_SCHED) || (id < 0))
1661 pri_error(pri, "Asked to delete sched id %d???\n", id);
1662 pri->pri_sched[id].callback = NULL;
1663 + pri->pri_sched[id].callback2 = NULL;
1666 diff -urN libpri-1.2.2.orig/pritest.c libpri-1.2.2/pritest.c
1667 --- libpri-1.2.2.orig/pritest.c 2005-11-29 19:39:18.000000000 +0100
1668 +++ libpri-1.2.2/pritest.c 2006-01-18 12:28:07.000000000 +0100
1671 * libpri: An implementation of Primary Rate ISDN
1673 - * Written by Mark Spencer <markster@digium.com>
1674 + * Written by Mark Spencer <markster@linux-support.net>
1676 - * Copyright (C) 2001-2005, Digium
1677 + * Copyright (C) 2001, Linux Support Services, Inc.
1678 * All Rights Reserved.
1680 * This program is free software; you can redistribute it and/or modify
1682 #define PRI_DEF_NODETYPE PRI_CPE
1683 #define PRI_DEF_SWITCHTYPE PRI_SWITCH_NI2
1685 -#define MAX_CHAN 32
1686 -#define DCHANNEL_TIMESLOT 16
1688 +#define DCHANNEL_TIMESLOT 3
1691 static int offset = 0;
1693 static void do_channel(ZAP *z)
1695 /* This is the part that runs on a given channel */
1696 - zap_playf(z, "raw.ulaw", 0);
1697 + zap_playf(z, "raw.alaw", 0);
1703 if (!strcasecmp(node, "network"))
1705 + if (!strcasecmp(node, "bri_cpe_ptmp"))
1706 + return BRI_CPE_PTMP;
1707 + if (!strcasecmp(node, "bri_network_ptmp"))
1708 + return BRI_NETWORK_PTMP;
1709 + if (!strcasecmp(node, "bri_cpe"))
1711 + if (!strcasecmp(node, "bri_network"))
1712 + return BRI_NETWORK;
1716 @@ -285,6 +293,10 @@
1720 + case PRI_EVENT_HANGUP_REQ:
1721 + printf("-- Hanging up channel %d\n", e->hangup.channel);
1722 + hangup_channel(e->hangup.channel);
1725 fprintf(stderr, "--!! Unknown PRI event %d\n", e->e);
1727 diff -urN libpri-1.2.2.orig/q921.c libpri-1.2.2/q921.c
1728 --- libpri-1.2.2.orig/q921.c 2005-12-06 22:35:50.000000000 +0100
1729 +++ libpri-1.2.2/q921.c 2006-01-18 12:28:07.000000000 +0100
1732 * libpri: An implementation of Primary Rate ISDN
1734 - * Written by Mark Spencer <markster@digium.com>
1735 + * Written by Mark Spencer <markster@linux-support.net>
1737 - * Copyright (C) 2001-2005, Digium
1738 + * Copyright (C) 2001, Linux Support Services, Inc.
1739 * All Rights Reserved.
1740 + * Copyright (C) 2003,2004,2005 Junghanns.NET GmbH
1741 + * Klaus-Peter Junghanns <kpj@junghanns.net>
1743 * This program is free software; you can redistribute it and/or modify
1744 * it under the terms of the GNU General Public License as published by
1746 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1755 (hf).h.tei = (pri)->tei; \
1758 -static void reschedule_t203(struct pri *pri);
1759 +static void reschedule_t203(struct pri *pri, int tei);
1760 +static void q921_flush_txqueue(struct pri *pri, int tei, int devnull);
1762 -static void q921_discard_retransmissions(struct pri *pri)
1763 +static void q921_discard_retransmissions(struct pri *pri, int tei)
1765 struct q921_frame *f, *p;
1767 + int teio = tei - Q921_TEI_BASE;
1768 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
1770 + f = pri->txqueue[teio];
1781 - pri->txqueue = NULL;
1782 + pri->txqueue[teio] = NULL;
1785 static int q921_transmit(struct pri *pri, q921_h *h, int len)
1787 pri_error(pri, "Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
1790 - reschedule_t203(pri);
1791 + if (pri->localtype == BRI_CPE_PTMP) {
1792 + reschedule_t203(pri, pri->tei);
1793 + } else if (h->h.tei != Q921_TEI_GROUP) {
1794 + reschedule_t203(pri, h->h.tei);
1799 -static void q921_send_ua(struct pri *pri, int pfbit)
1800 +static void q921_send_ua(struct pri *pri, int pfbit, int tei)
1805 h.u.m2 = 0; /* M2 = 0 */
1806 h.u.p_f = pfbit; /* Final bit on */
1807 h.u.ft = Q921_FRAMETYPE_U;
1809 switch(pri->localtype) {
1812 @@ -107,6 +118,19 @@
1816 + case BRI_NETWORK_PTMP:
1819 + case BRI_CPE_PTMP:
1820 + h.h.tei = pri->tei;
1830 pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
1832 @@ -116,18 +140,359 @@
1833 q921_transmit(pri, &h, 3);
1836 -static void q921_send_sabme_now(void *vpri);
1838 +static void q921_send_disconnect(struct pri *pri, int pfbit, int tei) {
1840 + Q921_INIT(pri, h);
1844 + h.u.ft = Q921_FRAMETYPE_U;
1846 + switch(pri->localtype) {
1853 + case BRI_NETWORK_PTMP:
1856 + case BRI_CPE_PTMP:
1857 + h.h.tei = pri->tei;
1867 + pri_error(pri, "Don't know how to disconnect on a type %d node\n", pri->localtype);
1870 + if (pri->debug & PRI_DEBUG_Q921_STATE)
1871 + pri_message(pri, "Sending Disconnect\n");
1872 + q921_transmit(pri, &h, 3);
1876 -static void q921_send_sabme(void *vpri, int now)
1877 +static void q921_send_dm(struct pri *pri, int pfbit, int tei)
1880 + Q921_INIT(pri, h);
1881 + h.u.m3 = 0; /* M3 = 0 */
1882 + h.u.m2 = 3; /* M2 = 3 */
1883 + h.u.p_f = pfbit; /* Final bit on */
1884 + h.u.ft = Q921_FRAMETYPE_U;
1886 + switch(pri->localtype) {
1893 + case BRI_NETWORK_PTMP:
1896 + case BRI_CPE_PTMP:
1897 + h.h.tei = pri->tei;
1907 + pri_error(pri, "Don't know how to DM on a type %d node\n", pri->localtype);
1910 + if (pri->debug & PRI_DEBUG_Q921_STATE)
1911 + pri_message(pri, "Sending DM\n");
1912 + q921_transmit(pri, &h, 3);
1915 +static void q921_send_teireq(void *vpri) {
1916 + struct pri *pri = vpri;
1917 + unsigned short ri=0x6464;
1921 + if (pri->localtype != BRI_CPE_PTMP) {
1922 + pri_error(pri, "TEI req for non-ptmp???\n");
1925 + if (pri->t202_timer[0]) {
1926 + pri_schedule_del(pri, pri->t202_timer[0]);
1927 + pri->t202_timer[0] = 0;
1929 + if (pri->sabme_retrans[0]++ > (pri->timers[PRI_TIMER_N202])) {
1930 + /* delete txqueue */
1931 + q921_flush_txqueue(pri, 0, 1);
1932 + /* N202 retransmissions, activation of layer 2 failed, tell upper layer, start again */
1934 + pri->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
1935 + pri->ev.gen.tei = 0;
1936 + pri->sabme_retrans[0] = 0;
1937 + /* dont try again, they are gone */
1941 + pri->t202_timer[0] = pri_schedule_event(pri, pri->timers[PRI_TIMER_T202], q921_send_teireq, pri);
1944 + f = malloc(sizeof(q921_u) + 5 + 2);
1945 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
1947 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
1948 + f->h.tei = Q921_TEI_GROUP;
1954 + f->ft = Q921_FRAMETYPE_U;
1956 + f->data[1] = ri >> 8;
1957 + f->data[2] = ri & 0xff;
1958 + f->data[3] = Q921_TEI_ID_REQUEST;
1959 + f->data[4] = 0xff;
1960 + if (pri->debug & PRI_DEBUG_Q921_STATE)
1961 + pri_message(pri, "Sending TEI Request ri=%d\n",ri);
1962 + q921_transmit(pri,(q921_h *)&(f->h),8);
1966 +static void q921_send_teiassign(struct pri *pri,int ri,int tei) {
1969 + if (pri->localtype != BRI_NETWORK_PTMP) {
1970 + pri_error(pri, "TEI assign for non-ptmp???\n");
1974 + f = malloc(sizeof(q921_u) + 5 + 2);
1975 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
1977 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
1978 + f->h.tei = Q921_TEI_GROUP;
1984 + f->ft = Q921_FRAMETYPE_U;
1986 + f->data[1] = ri >> 8;
1987 + f->data[2] = ri & 0xff;
1988 + f->data[3] = Q921_TEI_ID_ASSIGNED;
1989 + f->data[4] = (tei << 1) | 0x1;
1990 + if (pri->debug & PRI_DEBUG_Q921_STATE)
1991 + pri_message(pri, "Sending TEI assign ri=%d tei=%d\n",ri,tei);
1992 + q921_transmit(pri,(q921_h *)&(f->h),8);
1995 + pri_error(pri, "q921_send_teiassign: failed to malloc f!\n");
1999 +static void q921_send_teichkresp(struct pri *pri,int tei) {
2001 + unsigned short ri=0x6464;
2004 + if (pri->localtype != BRI_CPE_PTMP) {
2005 + pri_error(pri, "TEI check response for non-ptmp???\n");
2009 + f = malloc(sizeof(q921_u) + 5 + 2);
2010 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2012 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2013 + f->h.tei = Q921_TEI_GROUP;
2014 + f->h.c_r = 0; // command u->n
2019 + f->ft = Q921_FRAMETYPE_U;
2021 + f->data[1] = ri >> 8;
2022 + f->data[2] = ri & 0xff;
2023 + f->data[3] = Q921_TEI_ID_CHK_RES;
2024 + f->data[4] = (tei << 1) | 0x1;
2025 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2026 + pri_message(pri, "Sending TEI check resp ri=%d tei=%d\n",ri,tei);
2027 + q921_transmit(pri,(q921_h *)&(f->h),8);
2032 +static void q921_send_teichkreq(struct pri *pri,int tei) {
2034 + unsigned short ri=0x6464;
2037 + if (pri->localtype != BRI_NETWORK_PTMP) {
2038 + pri_error(pri, "TEI check response for non-ptmp???\n");
2042 + f = malloc(sizeof(q921_u) + 5 + 2);
2043 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2045 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2046 + f->h.tei = Q921_TEI_GROUP;
2047 + f->h.c_r = 1; // command u->n
2052 + f->ft = Q921_FRAMETYPE_U;
2056 + f->data[3] = Q921_TEI_ID_CHK_REQ;
2057 + f->data[4] = (tei << 1) | 0x1;
2058 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2059 + pri_message(pri, "Sending TEI check request ri=%d tei=%d\n",ri,tei);
2060 + q921_transmit(pri,(q921_h *)&(f->h),8);
2065 +static void q921_send_teiverify(struct pri *pri,int tei) {
2068 + if (pri->localtype != BRI_CPE_PTMP) {
2069 + pri_error(pri, "TEI verify for non-ptmp???\n");
2073 + f = malloc(sizeof(q921_u) + 5 + 2);
2074 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2076 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2077 + f->h.tei = Q921_TEI_GROUP;
2078 + f->h.c_r = 0; // command u->n
2083 + f->ft = Q921_FRAMETYPE_U;
2087 + f->data[3] = Q921_TEI_ID_VERIFY;
2088 + f->data[4] = (tei << 1) | 0x1;
2089 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2090 + pri_message(pri, "Sending TEI verify tei=%d\n", tei);
2091 + q921_transmit(pri,(q921_h *)&(f->h),8);
2096 + static void q921_send_teiremove(struct pri *pri, int tei) {
2098 + unsigned short ri=0x6464;
2101 + if (pri->localtype != BRI_NETWORK_PTMP) {
2102 + pri_error(pri, "TEI remove for non-ptmp???\n");
2106 + f = malloc(sizeof(q921_u) + 5 + 2);
2107 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2109 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2110 + f->h.tei = Q921_TEI_GROUP;
2116 + f->ft = Q921_FRAMETYPE_U;
2118 + f->data[1] = ri >> 8;
2119 + f->data[2] = ri & 0xff;
2120 + f->data[3] = Q921_TEI_ID_REMOVE;
2121 + f->data[4] = (tei << 1) | 0x1;
2122 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2123 + pri_message(pri, "Sending TEI remove tei=%d\n",tei);
2124 + q921_transmit(pri,(q921_h *)&(f->h),8);
2129 + static void q921_send_teidenied(struct pri *pri, int ri, int tei) {
2132 + if (pri->localtype != BRI_NETWORK_PTMP) {
2133 + pri_error(pri, "TEI ID denied for non-ptmp???\n");
2137 + f = malloc(sizeof(q921_u) + 5 + 2);
2138 + memset(f,0x0,sizeof(q921_u) + 5 + 2);
2140 + f->h.sapi = Q921_SAPI_LAYER2_MANAGEMENT;
2141 + f->h.tei = Q921_TEI_GROUP;
2147 + f->ft = Q921_FRAMETYPE_U;
2149 + f->data[1] = ri >> 8;
2150 + f->data[2] = ri & 0xff;
2151 + f->data[3] = Q921_TEI_ID_DENIED;
2152 + f->data[4] = (tei << 1) | 0x1;
2153 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2154 + pri_message(pri, "Sending TEI ID denied tei=%d\n",tei);
2155 + q921_transmit(pri,(q921_h *)&(f->h),8);
2161 +static void q921_send_sabme_now(void *vpri, int tei);
2163 +static void q921_send_sabme(void *vpri, int now, int tei)
2165 struct pri *pri = vpri;
2167 - pri_schedule_del(pri, pri->sabme_timer);
2168 - pri->sabme_timer = 0;
2169 - pri->sabme_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], q921_send_sabme_now, pri);
2170 + int teio=tei - Q921_TEI_BASE;
2171 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2173 + if (pri->sabme_timer[teio]) {
2174 + pri_schedule_del(pri, pri->sabme_timer[teio]);
2175 + pri->sabme_timer[teio] = 0;
2177 + pri->sabme_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], q921_send_sabme_now, pri, tei);
2180 + if (pri->sabme_retrans[teio]++ > (pri->timers[PRI_TIMER_N202])) {
2181 + /* delete txqueue */
2182 + q921_flush_txqueue(pri, tei, 1);
2183 + /* N202 retransmissions, activation of layer 2 failed, tell upper layer, start again */
2185 + pri->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
2186 + pri->ev.gen.tei = tei;
2187 + pri->sabme_retrans[teio] = 0;
2188 + /* dont try again, they are gone */
2194 h.u.m3 = 3; /* M3 = 3 */
2195 h.u.m2 = 3; /* M2 = 3 */
2196 h.u.p_f = 1; /* Poll bit set */
2197 @@ -139,25 +504,42 @@
2201 + case BRI_NETWORK_PTMP:
2204 + case BRI_CPE_PTMP:
2206 + h.h.tei = pri->tei;
2215 - pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
2216 + pri_error(pri, "Don't know how to send SABME on a type %d node\n", pri->localtype);
2219 if (pri->debug & PRI_DEBUG_Q921_STATE)
2220 pri_message(pri, "Sending Set Asynchronous Balanced Mode Extended\n");
2221 q921_transmit(pri, &h, 3);
2222 - pri->q921_state = Q921_AWAITING_ESTABLISH;
2223 + pri->q921_state[teio] = Q921_AWAITING_ESTABLISH;
2226 -static void q921_send_sabme_now(void *vpri)
2227 +static void q921_send_sabme_now(void *vpri, int tei)
2229 - q921_send_sabme(vpri, 1);
2230 + q921_send_sabme(vpri, 1, tei);
2233 -static int q921_ack_packet(struct pri *pri, int num)
2236 +static int q921_ack_packet(struct pri *pri, int num, int tei)
2238 struct q921_frame *f, *prev = NULL;
2240 + int teio=tei - Q921_TEI_BASE;
2241 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2242 + f = pri->txqueue[teio];
2244 if (f->h.n_s == num) {
2245 /* Cancel each packet as necessary */
2246 @@ -165,26 +547,26 @@
2248 prev->next = f->next;
2250 - pri->txqueue = f->next;
2251 + pri->txqueue[teio] = f->next;
2252 if (pri->debug & PRI_DEBUG_Q921_STATE)
2253 - pri_message(pri, "-- ACKing packet %d, new txqueue is %d (-1 means empty)\n", f->h.n_s, pri->txqueue ? pri->txqueue->h.n_s : -1);
2254 + pri_message(pri, "-- ACKing packet %d, new txqueue is %d (-1 means empty)\n", f->h.n_s, pri->txqueue[teio] ? pri->txqueue[teio]->h.n_s : -1);
2257 + pri->v_a[teio] = num;
2259 /* Reset retransmission counter if we actually acked something */
2261 + pri->retrans[teio] = 0;
2262 /* Decrement window size */
2264 + pri->windowlen[teio]--;
2265 /* Search for something to send */
2267 + f = pri->txqueue[teio];
2269 if (!f->transmitted) {
2270 /* Send it now... */
2271 if (pri->debug & PRI_DEBUG_Q921_STATE)
2272 pri_message(pri, "-- Finally transmitting %d, since window opened up\n", f->h.n_s);
2275 - f->h.n_r = pri->v_r;
2276 + pri->windowlen[teio]++;
2277 + f->h.n_r = pri->v_r[teio];
2278 q921_transmit(pri, (q921_h *)(&f->h), f->len);
2281 @@ -198,77 +580,136 @@
2285 -static void t203_expire(void *);
2286 -static void t200_expire(void *);
2287 -static pri_event *q921_dchannel_down(struct pri *pri);
2288 +static void t203_expire(void *, int tei);
2289 +static void t200_expire(void *, int tei);
2290 +static pri_event *q921_dchannel_down(struct pri *pri, int tei);
2292 +static void reschedule_t203(struct pri *pri, int tei)
2294 + int teio=tei - Q921_TEI_BASE;
2295 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2296 + if (pri->t203_timer[teio]) {
2297 + pri_schedule_del(pri, pri->t203_timer[teio]);
2298 + pri->t203_timer[teio] = 0;
2299 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_RELEASED) {
2300 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2301 + pri_message(pri, "-- Restarting T203 counter\n");
2302 + /* Nothing to transmit, start the T203 counter instead */
2303 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
2308 -static void reschedule_t203(struct pri *pri)
2309 +static void q921_flush_txqueue(struct pri *pri, int tei, int devnull)
2311 - if (pri->t203_timer) {
2312 - pri_schedule_del(pri, pri->t203_timer);
2313 - if (pri->debug & PRI_DEBUG_Q921_STATE)
2314 - pri_message(pri, "-- Restarting T203 counter\n");
2315 - /* Nothing to transmit, start the T203 counter instead */
2316 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
2317 + struct q921_frame *f, *tmp = NULL;
2318 + int teio=tei - Q921_TEI_BASE;
2319 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2320 + f = pri->txqueue[teio];
2322 + /* nothing to send */
2325 + /* transmit all i-frames that were queued up while we were waiting for layer 2 to rise */
2333 + if (pri->localtype == BRI_CPE_PTMP) {
2334 + /* update TEI, it might have changed */
2335 + f->h.h.tei = pri->tei;
2337 + q921_transmit(pri, (q921_h *)&f->h, f->len);
2344 + pri->txqueue[teio] = NULL;
2345 + pri->v_s[teio] = 0;
2347 + if (pri->t200_timer[teio]) {
2348 + pri_schedule_del(pri, pri->t200_timer[teio]);
2349 + pri->t200_timer[teio] = 0;
2351 + if (pri->t203_timer[teio]) {
2352 + pri_schedule_del(pri, pri->t203_timer[teio]);
2353 + pri->t203_timer[teio] = 0;
2355 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2359 -static pri_event *q921_ack_rx(struct pri *pri, int ack)
2360 +static pri_event *q921_ack_rx(struct pri *pri, int ack, int tei)
2365 + int teio=tei - Q921_TEI_BASE;
2366 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2367 /* Make sure the ACK was within our window */
2368 - for (x=pri->v_a; (x != pri->v_s) && (x != ack); Q921_INC(x));
2369 + for (x=pri->v_a[teio]; (x != pri->v_s[teio]) && (x != ack); Q921_INC(x));
2371 /* ACK was outside of our window --- ignore */
2372 - pri_error(pri, "ACK received for '%d' outside of window of '%d' to '%d', restarting\n", ack, pri->v_a, pri->v_s);
2373 - ev = q921_dchannel_down(pri);
2374 - q921_start(pri, 1);
2375 + pri_error(pri, "ACK received for '%d' outside of window of '%d' to '%d', restarting\n", ack, pri->v_a[teio], pri->v_s[teio]);
2376 + ev = q921_dchannel_down(pri, tei);
2377 + q921_start(pri, 1, tei);
2381 /* Cancel each packet as necessary */
2382 if (pri->debug & PRI_DEBUG_Q921_STATE)
2383 - pri_message(pri, "-- ACKing all packets from %d to (but not including) %d\n", pri->v_a, ack);
2384 - for (x=pri->v_a; x != ack; Q921_INC(x))
2385 - cnt += q921_ack_packet(pri, x);
2386 - if (!pri->txqueue) {
2387 + pri_message(pri, "-- ACKing all packets from %d to (but not including) %d\n", pri->v_a[teio], ack);
2388 + for (x=pri->v_a[teio]; x != ack; Q921_INC(x))
2389 + cnt += q921_ack_packet(pri, x, tei);
2390 + if (!pri->txqueue[teio]) {
2391 + /* Something was ACK'd. Stop T200 counter */
2392 + if (pri->t200_timer[teio]) {
2393 + pri_schedule_del(pri, pri->t200_timer[teio]);
2394 + pri->t200_timer[teio] = 0;
2396 if (pri->debug & PRI_DEBUG_Q921_STATE)
2397 pri_message(pri, "-- Since there was nothing left, stopping T200 counter\n");
2398 - /* Something was ACK'd. Stop T200 counter */
2399 - pri_schedule_del(pri, pri->t200_timer);
2400 - pri->t200_timer = 0;
2402 - if (pri->t203_timer) {
2403 + if (pri->t203_timer[teio]) {
2404 + pri_schedule_del(pri, pri->t203_timer[teio]);
2405 + pri->t203_timer[teio] = 0;
2406 if (pri->debug & PRI_DEBUG_Q921_STATE)
2407 pri_message(pri, "-- Stopping T203 counter since we got an ACK\n");
2408 - pri_schedule_del(pri, pri->t203_timer);
2409 - pri->t203_timer = 0;
2411 - if (pri->txqueue) {
2412 + if (pri->txqueue[teio]) {
2413 /* Something left to transmit, Start the T200 counter again if we stopped it */
2414 + if (pri->t200_timer[teio]) {
2415 + pri_schedule_del(pri, pri->t200_timer[teio]);
2416 + pri->t200_timer[teio] = 0;
2418 if (pri->debug & PRI_DEBUG_Q921_STATE)
2419 - pri_message(pri, "-- Something left to transmit (%d), restarting T200 counter\n", pri->txqueue->h.n_s);
2420 - if (!pri->t200_timer)
2421 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2422 + pri_message(pri, "-- Something left to transmit (%d), restarting T200 counter\n", pri->txqueue[teio]->h.n_s);
2423 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2426 if (pri->debug & PRI_DEBUG_Q921_STATE)
2427 pri_message(pri, "-- Nothing left, starting T203 counter\n");
2428 /* Nothing to transmit, start the T203 counter instead */
2429 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
2430 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
2435 -static void q921_reject(struct pri *pri, int pf)
2436 +static void q921_reject(struct pri *pri, int pf, int tei)
2439 + int teio=tei - Q921_TEI_BASE;
2440 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2442 h.s.x0 = 0; /* Always 0 */
2443 h.s.ss = 2; /* Reject */
2444 h.s.ft = 1; /* Frametype (01) */
2445 - h.s.n_r = pri->v_r; /* Where to start retransmission */
2446 + h.s.n_r = pri->v_r[teio]; /* Where to start retransmission */
2448 switch(pri->localtype) {
2450 @@ -277,23 +718,38 @@
2454 + case BRI_NETWORK_PTMP:
2457 + case BRI_CPE_PTMP:
2468 - pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
2469 + pri_error(pri, "Don't know how to REJECT on a type %d node\n", pri->localtype);
2472 if (pri->debug & PRI_DEBUG_Q921_STATE)
2473 - pri_message(pri, "Sending Reject (%d)\n", pri->v_r);
2475 + pri_message(pri, "Sending Reject (%d)\n", pri->v_r[teio]);
2476 + pri->sentrej[teio] = 1;
2477 q921_transmit(pri, &h, 4);
2480 -static void q921_rr(struct pri *pri, int pbit, int cmd) {
2481 +static void q921_rr(struct pri *pri, int pbit, int cmd, int tei) {
2483 + int teio=tei - Q921_TEI_BASE;
2484 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2486 h.s.x0 = 0; /* Always 0 */
2487 h.s.ss = 0; /* Receive Ready */
2488 h.s.ft = 1; /* Frametype (01) */
2489 - h.s.n_r = pri->v_r; /* N/R */
2490 + h.s.n_r = pri->v_r[teio]; /* N/R */
2491 h.s.p_f = pbit; /* Poll/Final set appropriately */
2492 switch(pri->localtype) {
2494 @@ -308,81 +764,192 @@
2498 + case BRI_NETWORK_PTMP:
2505 + case BRI_CPE_PTMP:
2525 - pri_error(pri, "Don't know how to U/A on a type %d node\n", pri->localtype);
2526 + pri_error(pri, "Don't know how to RR on a type %d node\n", pri->localtype);
2529 - pri->v_na = pri->v_r; /* Make a note that we've already acked this */
2530 + pri->v_na[teio] = pri->v_r[teio]; /* Make a note that we've already acked this */
2531 if (pri->debug & PRI_DEBUG_Q921_STATE)
2532 - pri_message(pri, "Sending Receiver Ready (%d)\n", pri->v_r);
2533 + pri_message(pri, "Sending Receiver Ready (%d)\n", pri->v_r[teio]);
2534 q921_transmit(pri, &h, 4);
2537 -static void t200_expire(void *vpri)
2538 +static void t200_expire(void *vpri, int tei)
2540 struct pri *pri = vpri;
2541 - if (pri->txqueue) {
2542 + int teio=tei - Q921_TEI_BASE;
2543 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2544 + if (pri->txqueue[teio]) {
2545 /* Retransmit first packet in the queue, setting the poll bit */
2546 if (pri->debug & PRI_DEBUG_Q921_STATE)
2547 pri_message(pri, "-- T200 counter expired, What to do...\n");
2548 - /* Force Poll bit */
2549 - pri->txqueue->h.p_f = 1;
2551 - pri->txqueue->h.n_r = pri->v_r;
2552 - pri->v_na = pri->v_r;
2553 - pri->solicitfbit = 1;
2555 + if (pri->txqueue[teio]->transmitted) {
2556 + /* Force Poll bit, if this is a retransmission */
2557 + pri->txqueue[teio]->h.p_f = 1;
2558 + pri->solicitfbit[teio] = 1;
2560 + pri->txqueue[teio]->h.n_r = pri->v_r[teio];
2561 + pri->v_na[teio] = pri->v_r[teio];
2562 + pri->retrans[teio]++;
2564 /* Up to three retransmissions */
2565 - if (pri->retrans < pri->timers[PRI_TIMER_N200]) {
2566 + if ((pri->retrans[teio] + 1) <= pri->timers[PRI_TIMER_N200]) {
2567 /* Reschedule t200_timer */
2568 if (pri->debug & PRI_DEBUG_Q921_STATE)
2569 - pri_message(pri, "-- Retransmitting %d bytes\n", pri->txqueue->len);
2571 - q921_rr(pri, 1, 1);
2573 - if (!pri->txqueue->transmitted)
2574 + pri_message(pri, "-- Retransmitting %d bytes\n", pri->txqueue[teio]->len);
2575 + if (pri->busy[teio]) {
2576 + // pri_message(pri, "-- q921_rr(pri, 1, 1) \n", tei);
2577 + q921_rr(pri, 1, 1, tei);
2579 + if (!pri->txqueue[teio]->transmitted)
2580 pri_error(pri, "!! Not good - head of queue has not been transmitted yet\n");
2581 - q921_transmit(pri, (q921_h *)&pri->txqueue->h, pri->txqueue->len);
2582 + q921_transmit(pri, (q921_h *)&pri->txqueue[teio]->h, pri->txqueue[teio]->len);
2584 + pri->txqueue[teio]->transmitted++;
2586 if (pri->debug & PRI_DEBUG_Q921_STATE)
2587 - pri_message(pri, "-- Rescheduling retransmission (%d)\n", pri->retrans);
2588 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2589 + pri_message(pri, "-- Rescheduling retransmission (%d)\n", pri->retrans[teio]);
2590 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2592 if (pri->debug & PRI_DEBUG_Q921_STATE)
2593 pri_message(pri, "-- Timeout occured, restarting PRI\n");
2594 - pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
2595 - pri->t200_timer = 0;
2596 - q921_dchannel_down(pri);
2597 - q921_start(pri, 1);
2598 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
2599 + pri->t200_timer[teio] = 0;
2600 + q921_dchannel_down(pri, tei);
2601 + q921_start(pri, 1, tei);
2604 - } else if (pri->solicitfbit) {
2605 + } else if (pri->solicitfbit[teio]) {
2606 if (pri->debug & PRI_DEBUG_Q921_STATE)
2607 pri_message(pri, "-- Retrying poll with f-bit\n");
2609 - if (pri->retrans < pri->timers[PRI_TIMER_N200]) {
2610 - pri->solicitfbit = 1;
2611 - q921_rr(pri, 1, 1);
2612 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2613 + pri->retrans[teio]++;
2614 + if (pri->retrans[teio] < pri->timers[PRI_TIMER_N200]) {
2615 + pri->solicitfbit[teio] = 1;
2616 + q921_rr(pri, 1, 1, tei);
2617 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2619 if (pri->debug & PRI_DEBUG_Q921_STATE)
2620 pri_message(pri, "-- Timeout occured, restarting PRI\n");
2621 - pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
2622 - pri->t200_timer = 0;
2623 - q921_dchannel_down(pri);
2624 - q921_start(pri, 1);
2625 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
2626 + pri->t200_timer[teio] = 0;
2627 + q921_dchannel_down(pri, tei);
2628 + q921_start(pri, 1, tei);
2632 pri_error(pri, "T200 counter expired, nothing to send...\n");
2633 - pri->t200_timer = 0;
2634 + pri->t200_timer[teio] = 0;
2635 + if (pri->busy[teio]) {
2636 + if ((pri->retrans[teio] + 1) <= pri->timers[PRI_TIMER_N200]) {
2638 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2639 + pri->retrans[teio]++;
2640 + q921_rr(pri, 1, 1, tei);
2642 + q921_reset(pri, tei, 1);
2643 + q921_start(pri, 1, tei);
2649 -int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr)
2650 +int q921_transmit_uframe(struct pri *pri, void *buf, int len, int cr, int tei)
2653 + uf = malloc(sizeof(q921_u) + len + 2);
2654 + memset(uf,0,sizeof(q921_u) + len + 2);
2662 + uf->ft = Q921_FRAMETYPE_U;
2663 + switch(pri->localtype) {
2670 + case BRI_NETWORK_PTMP:
2673 + case BRI_CPE_PTMP:
2683 + pri_error(pri, "Don't know how to send U frames on a type %d node\n", pri->localtype);
2686 + memcpy(uf->data,buf,len);
2687 + q921_transmit(pri, (q921_h*)&(uf->h), 3+len);
2693 +int q921_transmit_iframe(struct pri *pri, void *buf, int len, int cr, int tei)
2695 q921_frame *f, *prev=NULL;
2696 - for (f=pri->txqueue; f; f = f->next) prev = f;
2697 + int teio=tei - Q921_TEI_BASE;
2698 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2699 + if ((pri->q921_state[teio] == Q921_LINK_CONNECTION_RELEASED) && (!pri->sabme_timer[teio])) {
2700 + if (pri->localtype == BRI_CPE_PTMP) {
2701 + if (pri->tei > 0) {
2702 + /* p2p datalink is down */
2703 + pri->sabme_retrans[teio] = 0;
2704 + q921_send_sabme_now(pri, pri->tei);
2705 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2706 + pri_message(pri, "Reactivating layer 2\n");
2709 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2710 + pri_message(pri, "reactivating layer 2, sending tei req\n");
2711 + q921_send_teireq(pri);
2713 + } else if ((pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) {
2714 + /* p2p datalink is down */
2715 + pri->sabme_retrans[teio] = 0;
2716 + q921_send_sabme_now(pri, pri->tei);
2717 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2718 + pri_message(pri, "Reactivating layer 2\n");
2721 + for (f=pri->txqueue[teio]; f; f = f->next) prev = f;
2722 f = malloc(sizeof(q921_frame) + len + 2);
2724 memset(f,0,sizeof(q921_frame) + len + 2);
2725 @@ -400,47 +967,80 @@
2729 + case BRI_NETWORK_PTMP:
2736 + case BRI_CPE_PTMP:
2737 + f->h.h.tei = pri->tei;
2759 memcpy(f->h.data, buf, len);
2760 - f->h.n_s = pri->v_s;
2761 - f->h.n_r = pri->v_r;
2763 - pri->v_na = pri->v_r;
2764 + f->h.n_s = pri->v_s[teio];
2765 + f->h.n_r = pri->v_r[teio];
2767 + pri->v_na[teio] = pri->v_r[teio];
2774 - /* Immediately transmit unless we're in a recovery state, or the window
2775 - size is too big */
2776 - if (!pri->retrans && !pri->busy) {
2777 - if (pri->windowlen < pri->window) {
2779 + pri->txqueue[teio] = f;
2781 + if ((pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) && ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK))){
2782 + /* no p2p datalink, yet. queue up the iframes... */
2783 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2784 + pri_message(pri, "Layer 3 transmit waiting for layer 2\n");
2786 + /* Immediately transmit unless we're in a recovery state, or the window
2787 + size is too big */
2788 + if (!pri->retrans[teio] && !pri->busy[teio]) {
2789 + if (pri->windowlen[teio] < pri->window[teio]) {
2790 + pri->windowlen[teio]++;
2791 q921_transmit(pri, (q921_h *)(&f->h), f->len);
2794 if (pri->debug & PRI_DEBUG_Q921_STATE)
2795 pri_message(pri, "Delaying transmission of %d, window is %d/%d long\n",
2796 - f->h.n_s, pri->windowlen, pri->window);
2797 + f->h.n_s, pri->windowlen[teio], pri->window[teio]);
2800 - if (pri->t203_timer) {
2802 + if (pri->t203_timer[teio]) {
2803 + pri_schedule_del(pri, pri->t203_timer[teio]);
2804 + pri->t203_timer[teio] = 0;
2805 if (pri->debug & PRI_DEBUG_Q921_STATE)
2806 pri_message(pri, "Stopping T_203 timer\n");
2807 - pri_schedule_del(pri, pri->t203_timer);
2808 - pri->t203_timer = 0;
2810 - if (!pri->t200_timer) {
2812 + if (!pri->t200_timer[teio]) {
2813 if (pri->debug & PRI_DEBUG_Q921_STATE)
2814 pri_message(pri, "Starting T_200 timer\n");
2815 - pri->t200_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2817 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2819 if (pri->debug & PRI_DEBUG_Q921_STATE)
2820 - pri_message(pri, "T_200 timer already going (%d)\n", pri->t200_timer);
2821 + pri_message(pri, "T_200 timer already going (%d)\n", pri->t200_timer[teio]);
2825 pri_error(pri, "!! Out of memory for Q.921 transmit\n");
2826 @@ -449,49 +1049,86 @@
2830 -static void t203_expire(void *vpri)
2831 +static void t203_expire(void *vpri, int tei)
2833 - struct pri *pri = vpri;
2834 - if (pri->q921_state == Q921_LINK_CONNECTION_ESTABLISHED) {
2835 - if (pri->debug & PRI_DEBUG_Q921_STATE)
2836 + struct pri *pri = vpri;
2837 + int teio=tei - Q921_TEI_BASE;
2838 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2840 + if (pri->q921_state[teio] == Q921_LINK_CONNECTION_ESTABLISHED) {
2841 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2842 pri_message(pri, "T203 counter expired, sending RR and scheduling T203 again\n");
2843 - /* Solicit an F-bit in the other's RR */
2844 - pri->solicitfbit = 1;
2846 - q921_rr(pri, 1, 1);
2847 - /* Start timer T200 to resend our RR if we don't get it */
2848 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri);
2849 + /* Solicit an F-bit in the other's RR */
2850 + pri->solicitfbit[teio] = 1;
2851 + pri->retrans[teio] = 0;
2852 + q921_rr(pri, 1, 1, tei);
2853 + /* Start timer T200 to resend our RR if we don't get it */
2854 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, tei);
2856 + if (pri->debug & PRI_DEBUG_Q921_STATE)
2857 + pri_message(pri, "T203 counter expired in weird state %d\n", pri->q921_state[teio]);
2858 + pri->t203_timer[teio] = 0;
2862 +static void q921_start_tei(struct pri *pri, int tei);
2865 +static void t201_expire(void *vpri, int tei)
2867 + struct pri *pri = vpri;
2868 + int teio=tei - Q921_TEI_BASE;
2870 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2871 + if (tei == Q921_TEI_GROUP) {
2872 + for (i=0;i<Q921_MAX_TEIS;i++) {
2873 + if (pri->q921_tei_check[i] == 1) {
2874 + pri->q921_tei_check[i] = 0;
2875 + q921_start_tei(pri, Q921_TEI_BASE + i);
2879 - if (pri->debug & PRI_DEBUG_Q921_STATE)
2880 - pri_message(pri, "T203 counter expired in weird state %d\n", pri->q921_state);
2881 - pri->t203_timer = 0;
2882 + if (pri->q921_tei_check[teio] == 1) {
2883 + pri->q921_tei_check[teio] = 0;
2884 + q921_start_tei(pri, tei);
2887 + pri->t201_timer[teio] = 0;
2890 static pri_event *q921_handle_iframe(struct pri *pri, q921_i *i, int len)
2894 + int teio= i->h.tei - Q921_TEI_BASE;
2895 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2896 +// pri_error(pri, "q921_handle_iframe: i->n_s %d pri->v_r[%d] %d \n", i->n_s, teio, pri->v_r[teio]);
2897 /* Make sure this is a valid packet */
2898 - if (i->n_s == pri->v_r) {
2899 + if (i->n_s == pri->v_r[teio]) {
2900 /* Increment next expected I-frame */
2901 - Q921_INC(pri->v_r);
2902 + Q921_INC(pri->v_r[teio]);
2903 /* Handle their ACK */
2905 - ev = q921_ack_rx(pri, i->n_r);
2906 + pri->sentrej[teio] = 0;
2907 + ev = q921_ack_rx(pri, i->n_r, i->h.tei);
2909 + pri_error(pri, "q921_handle_iframe: ev = %d \n", ev->e);
2914 /* If the Poll/Final bit is set, immediate send the RR */
2915 - q921_rr(pri, 1, 0);
2916 - } else if (pri->busy) {
2917 - q921_rr(pri, 0, 0);
2918 + q921_rr(pri, 1, 0, i->h.tei);
2919 + } else if (pri->busy[teio]) {
2920 + q921_rr(pri, 0, 0, i->h.tei);
2921 + } else if (pri->t200_timer[teio] && pri->retrans[teio]) {
2922 + pri_error(pri, "q921_handle_iframe: sending RR \n");
2923 + q921_rr(pri, 0, 0, i->h.tei);
2925 /* Receive Q.931 data */
2926 - res = q931_receive(pri, (q931_h *)i->data, len - 4);
2927 + res = q931_receive(pri, (q931_h *)i->data, len - 4, i->h.tei);
2928 /* Send an RR if one wasn't sent already */
2929 - if (pri->v_na != pri->v_r)
2930 - q921_rr(pri, 0, 0);
2931 + if (pri->v_na[teio] != pri->v_r[teio])
2932 + q921_rr(pri, 0, 0, i->h.tei);
2936 @@ -500,10 +1137,10 @@
2938 /* If we haven't already sent a reject, send it now, otherwise
2939 we are obliged to RR */
2940 - if (!pri->sentrej)
2941 - q921_reject(pri, i->p_f);
2942 + if (!pri->sentrej[teio])
2943 + q921_reject(pri, i->p_f, i->h.tei);
2945 - q921_rr(pri, 1, 0);
2946 + q921_rr(pri, 1, 0, i->h.tei);
2950 @@ -641,75 +1278,152 @@
2954 -static pri_event *q921_dchannel_up(struct pri *pri)
2955 +static void q921_tei_recovery(struct pri *pri, int tei) {
2957 + int teio=tei - Q921_TEI_BASE;
2958 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2959 + for (i=0;i<Q921_MAX_TEIS;i++) {
2960 + if ((i + Q921_TEI_BASE == tei) || (tei == Q921_TEI_GROUP)) {
2961 + pri->q921_tei_check[i] = 1;
2964 + q921_send_teichkreq(pri, tei);
2965 + if (!pri->t201_timer[teio]) {
2966 + pri->t201_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T201], t201_expire, pri, tei);
2970 +static void q921_invoke_tei_recovery(void *vpri, int tei) {
2971 + struct pri *pri = vpri;
2972 + q921_tei_recovery(pri, tei);
2976 +static pri_event *q921_dchannel_up(struct pri *pri, int tei)
2978 - /* Reset counters, etc */
2980 + // we treat this as MFE
2981 + int teio=tei - Q921_TEI_BASE;
2982 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
2984 + if (((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) && (pri->txqueue[teio])) {
2985 + /* Reset counters, etc */
2986 + q921_reset(pri, tei, 0);
2988 + /* Reset counters, discard frames, etc */
2989 + q921_reset(pri, tei, 1);
2992 /* Stop any SABME retransmissions */
2993 - pri_schedule_del(pri, pri->sabme_timer);
2994 - pri->sabme_timer = 0;
2995 + if (pri->sabme_timer[teio]) {
2996 + pri_schedule_del(pri, pri->sabme_timer[teio]);
2997 + pri->sabme_timer[teio] = 0;
3000 + if (pri->t202_timer[teio]) {
3001 + pri_schedule_del(pri, pri->t202_timer[teio]);
3002 + pri->t202_timer[teio] = 0;
3005 /* Reset any rejects */
3007 + pri->sentrej[teio] = 0;
3009 /* Go into connection established state */
3010 - pri->q921_state = Q921_LINK_CONNECTION_ESTABLISHED;
3012 - /* Start the T203 timer */
3013 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
3014 + pri->q921_state[teio] = Q921_LINK_CONNECTION_ESTABLISHED;
3016 - /* Report event that D-Channel is now up */
3017 - pri->ev.gen.e = PRI_EVENT_DCHAN_UP;
3019 + if (((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) && (pri->txqueue[teio])) {
3020 + /* transmit queued iframes ans start T200 (handled by flush_txqueue) */
3021 + q921_flush_txqueue(pri, tei, 0);
3022 + /* dont upset upper layers if we reactivate layer 2 */
3025 + /* Start the T203 timer */
3026 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
3028 + /* Report event that D-Channel is now up */
3029 + pri->ev.gen.e = PRI_EVENT_DCHAN_UP;
3030 + pri->ev.gen.tei = tei;
3035 -static pri_event *q921_dchannel_down(struct pri *pri)
3036 +static pri_event *q921_dchannel_down(struct pri *pri, int tei)
3038 + int teio=tei - Q921_TEI_BASE;
3039 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3041 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
3043 /* Reset counters, reset sabme timer etc */
3045 + if (pri->localtype == BRI_NETWORK_PTMP) {
3046 + if (pri->t203_timer[teio]) {
3047 + pri_schedule_del(pri, pri->t203_timer[teio]);
3048 + pri->t203_timer[teio] = 0;
3049 + if (pri->debug & PRI_DEBUG_Q921_STATE) {
3050 + pri_message(pri, "Stopping T_203 timer for TEI %d\n", tei);
3055 + q921_reset(pri, tei, 1);
3057 - /* Report event that D-Channel is now up */
3058 + /* Report event that D-Channel is now down */
3059 pri->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
3060 + pri->ev.gen.tei = tei;
3064 -void q921_reset(struct pri *pri)
3065 +void q921_reset(struct pri *pri, int tei, int discard)
3067 + int teio=tei - Q921_TEI_BASE;
3068 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3069 /* Having gotten a SABME we MUST reset our entire state */
3074 - pri->window = pri->timers[PRI_TIMER_K];
3075 - pri->windowlen = 0;
3076 - pri_schedule_del(pri, pri->sabme_timer);
3077 - pri_schedule_del(pri, pri->t203_timer);
3078 - pri_schedule_del(pri, pri->t200_timer);
3079 - pri->sabme_timer = 0;
3080 - pri->t203_timer = 0;
3081 - pri->t200_timer = 0;
3083 - pri->solicitfbit = 0;
3084 - pri->q921_state = Q921_LINK_CONNECTION_RELEASED;
3088 + pri->v_s[teio] = 0;
3090 + pri->v_a[teio] = 0;
3091 + pri->v_r[teio] = 0;
3092 + pri->v_na[teio] = 0;
3093 + pri->window[teio] = pri->timers[PRI_TIMER_K];
3094 + pri->windowlen[teio] = 0;
3095 + pri_schedule_del(pri, pri->sabme_timer[teio]);
3096 + pri_schedule_del(pri, pri->t203_timer[teio]);
3097 + pri_schedule_del(pri, pri->t200_timer[teio]);
3098 + pri->sabme_timer[teio] = 0;
3099 + pri->t203_timer[teio] = 0;
3100 + pri->t200_timer[teio] = 0;
3101 + pri_schedule_del(pri, pri->t202_timer[teio]);
3102 + pri->t202_timer[teio] = 0;
3103 + pri_schedule_del(pri, pri->t201_timer[teio]);
3104 + pri->t201_timer[teio] = 0;
3105 + pri->busy[teio] = 0;
3106 + pri->solicitfbit[teio] = 0;
3107 + pri->q921_state[teio] = Q921_LINK_CONNECTION_RELEASED;
3108 + pri->retrans[teio] = 0;
3109 + pri->sabme_retrans[teio] = 0;
3110 + pri->sentrej[teio] = 0;
3112 - /* Discard anything waiting to go out */
3113 - q921_discard_retransmissions(pri);
3115 + /* Discard anything waiting to go out */
3116 + q921_discard_retransmissions(pri, tei);
3121 static pri_event *__q921_receive_qualified(struct pri *pri, q921_h *h, int len)
3128 + int teio=h->h.tei - Q921_TEI_BASE;
3129 + if (((teio < 0) || (teio > Q921_MAX_TEIS)) || (pri->localtype != BRI_NETWORK_PTMP)) { teio=0; }
3131 switch(h->h.data[0] & Q921_FRAMETYPE_MASK) {
3134 - if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
3135 - pri_error(pri, "!! Got I-frame while link state %d\n", pri->q921_state);
3136 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) {
3137 + pri_error(pri, "!! Got I-frame while link state %d\n", pri->q921_state[teio]);
3140 /* Informational frame */
3141 @@ -720,8 +1434,10 @@
3142 return q921_handle_iframe(pri, &h->i, len);
3145 - if (pri->q921_state != Q921_LINK_CONNECTION_ESTABLISHED) {
3146 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) {
3147 pri_error(pri, "!! Got S-frame while link down\n");
3148 + q921_send_dm(pri, 1, h->h.tei);
3149 + q921_reset(pri, h->h.tei, 1);
3153 @@ -731,80 +1447,122 @@
3156 /* Receiver Ready */
3158 + pri->busy[teio] = 0;
3159 /* Acknowledge frames as necessary */
3160 - ev = q921_ack_rx(pri, h->s.n_r);
3161 + ev = q921_ack_rx(pri, h->s.n_r, h->h.tei);
3165 - /* If it's a p/f one then send back a RR in return with the p/f bit set */
3166 - if (pri->solicitfbit) {
3167 + /* If it's a p/f one then send back a RR in return with the p/f bit set */
3168 +// pri_error(pri, "-- h->s.pf %d pri->solicitfbit[teio] %d\n", h->s.p_f, pri->solicitfbit[teio]);
3169 + if (pri->solicitfbit[teio]) {
3170 if (pri->debug & PRI_DEBUG_Q921_STATE)
3171 pri_message(pri, "-- Got RR response to our frame\n");
3173 - if (pri->debug & PRI_DEBUG_Q921_STATE)
3174 + if (pri->txqueue[teio]) {
3175 + q921_flush_txqueue(pri, pri->tei, 0);
3177 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3178 pri_message(pri, "-- Unsolicited RR with P/F bit, responding\n");
3179 - q921_rr(pri, 1, 0);
3180 + q921_rr(pri, 1, 0, h->h.tei);
3183 - pri->solicitfbit = 0;
3184 + pri->solicitfbit[teio] = 0;
3188 /* Receiver not ready */
3189 if (pri->debug & PRI_DEBUG_Q921_STATE)
3190 pri_message(pri, "-- Got receiver not ready\n");
3192 - /* Send RR if poll bit set */
3193 - q921_rr(pri, h->s.p_f, 0);
3194 + if ((pri->localtype != PRI_NETWORK) && (pri->localtype != BRI_NETWORK) && (pri->localtype != BRI_NETWORK_PTMP)) {
3195 + if (h->s.p_f && h->s.h.c_r) {
3196 +// if (!pri->t200_timer[teio]) {
3197 + /* Send RR if poll bit set */
3198 + q921_rr(pri, h->s.p_f, 0, h->h.tei);
3202 + if (h->s.p_f && (!h->s.h.c_r)) {
3203 +// if (!pri->t200_timer[teio]) {
3204 + /* Send RR if poll bit set */
3205 + q921_rr(pri, h->s.p_f, 0, h->h.tei);
3211 + pri->busy[teio] = 1;
3212 + if (pri->t200_timer[teio]) {
3213 + pri_schedule_del(pri, pri->t200_timer[teio]);
3214 + pri->t200_timer[teio] = 0;
3216 + pri->t200_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T200], t200_expire, pri, h->h.tei);
3219 /* Just retransmit */
3220 if (pri->debug & PRI_DEBUG_Q921_STATE)
3221 pri_message(pri, "-- Got reject requesting packet %d... Retransmitting.\n", h->s.n_r);
3223 - /* If it has the poll bit set, send an appropriate supervisory response */
3224 - q921_rr(pri, 1, 0);
3228 + q921_ack_rx(pri, h->s.n_r, h->h.tei);
3229 /* Resend the proper I-frame */
3230 - for(f=pri->txqueue;f;f=f->next) {
3231 + for(f=pri->txqueue[teio];f;f=f->next) {
3232 +// pri_error(pri, "!! frame n_s %d transmitted %d (searching for %d)!\n", f->h.n_s, f->transmitted, h->s.n_r);
3233 if ((sendnow || (f->h.n_s == h->s.n_r)) && f->transmitted) {
3234 /* Matches the request, or follows in our window, and has
3235 already been transmitted. */
3237 - pri_error(pri, "!! Got reject for frame %d, retransmitting frame %d now, updating n_r!\n", h->s.n_r, f->h.n_s);
3238 - f->h.n_r = pri->v_r;
3240 + /* multiframe established */
3241 + f->transmitted = 0;
3242 + f->h.n_r = pri->v_r[teio];
3244 + if (pri->busy[teio]) {
3245 + pri->busy[teio] = 0;
3250 + pri_error(pri, "!! Got reject for frame %d, retransmitting frame %d now, updating n_r!\n", h->s.n_r, f->h.n_s);
3251 q921_transmit(pri, (q921_h *)(&f->h), f->len);
3256 - if (pri->txqueue) {
3257 - /* This should never happen */
3258 - if (!h->s.p_f || h->s.n_r) {
3259 + if (pri->txqueue[teio]) {
3261 + q921_rr(pri, 1, 0, h->h.tei);
3262 + } else if (!h->s.p_f || h->s.n_r) {
3263 pri_error(pri, "!! Got reject for frame %d, but we only have others!\n", h->s.n_r);
3266 /* Hrm, we have nothing to send, but have been REJ'd. Reset v_a, v_s, etc */
3267 - pri_error(pri, "!! Got reject for frame %d, but we have nothing -- resetting!\n", h->s.n_r);
3268 - pri->v_a = h->s.n_r;
3269 - pri->v_s = h->s.n_r;
3270 +/* pri_error(pri, "!! Got reject for frame %d, but we have nothing -- resetting!\n", h->s.n_r);
3271 + pri->v_a[teio] = h->s.n_r;
3272 + pri->v_s[teio] = h->s.n_r; */
3273 /* Reset t200 timer if it was somehow going */
3274 - if (pri->t200_timer) {
3275 - pri_schedule_del(pri, pri->t200_timer);
3276 - pri->t200_timer = 0;
3277 + if (pri->t200_timer[teio]) {
3278 + pri_schedule_del(pri, pri->t200_timer[teio]);
3279 + pri->t200_timer[teio] = 0;
3281 /* Reset and restart t203 timer */
3282 - if (pri->t203_timer)
3283 - pri_schedule_del(pri, pri->t203_timer);
3284 - pri->t203_timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri);
3285 + if (pri->t203_timer[teio])
3286 + pri_schedule_del(pri, pri->t203_timer[teio]);
3287 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, tei);
3288 + pri->solicitfbit[teio] = 0;
3293 + /* If it has the poll bit set (and an iframe was retransmitted), send an appropriate supervisory response */
3294 +// q921_rr(pri, 1, 0, h->h.tei);
3295 + /* Reset and restart t203 timer */
3296 + pri->solicitfbit[teio] = 0;
3297 + if (pri->t203_timer[teio])
3298 + pri_schedule_del(pri, pri->t203_timer[teio]);
3299 + pri->t203_timer[teio] = pri_schedule_event2(pri, pri->timers[PRI_TIMER_T203], t203_expire, pri, h->h.tei);
3304 pri_error(pri, "!! XXX Unknown Supervisory frame ss=0x%02x,pf=%02xnr=%02x vs=%02x, va=%02x XXX\n", h->s.ss, h->s.p_f, h->s.n_r,
3305 - pri->v_s, pri->v_a);
3306 + pri->v_s[teio], pri->v_a[teio]);
3310 @@ -821,8 +1579,16 @@
3311 if (pri->debug & PRI_DEBUG_Q921_STATE)
3312 pri_message(pri, "-- Got DM Mode from peer.\n");
3313 /* Disconnected mode, try again after T200 */
3314 - ev = q921_dchannel_down(pri);
3315 - q921_start(pri, 0);
3316 + ev = q921_dchannel_down(pri, h->h.tei);
3317 + if ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) {
3318 + /* release layer 2 */
3319 + // pri_error(pri, "got DM, restarting layer 2.\n");
3321 + q921_start(pri, 0, h->h.tei);
3323 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)){
3324 + q921_start(pri, 0, h->h.tei);
3329 @@ -830,21 +1596,144 @@
3330 pri_message(pri, "-- Ignoring unsolicited DM with p/f set to 0\n");
3332 /* Requesting that we start */
3333 - q921_start(pri, 0);
3334 + q921_start(pri, 0, h->h.tei);
3338 - } else if (!h->u.m2) {
3339 - pri_message(pri, "XXX Unnumbered Information not implemented XXX\n");
3340 + } else if (h->u.m2 == 0) {
3341 + if (h->u.ft == 3) {
3342 + switch (h->u.data[0]) { /* Management Entity Identifier */
3344 + /* TEI Procedure */
3345 + switch (h->u.data[3]) {
3346 + case Q921_TEI_ID_VERIFY:
3347 + if (pri->localtype != BRI_NETWORK_PTMP)
3349 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3350 + pri_message(pri, "got TEI verify for TEI = %d\n",h->u.data[4] >> 1);
3352 + case Q921_TEI_ID_ASSIGNED:
3353 + if (pri->localtype != BRI_CPE_PTMP)
3355 + if (pri->tei == (h->u.data[4] >> 1)) {
3356 + // TEI already assgined, CHECK_TEI or REMOVE_TEI
3357 + pri_error(pri, "Double assgined TEI!\n");
3359 + if (pri->ri == ((unsigned short) (h->u.data[1] << 8) + h->u.data[2])) {
3360 + if (pri->t202_timer[0]) {
3361 + pri_schedule_del(pri, pri->t202_timer[0]);
3362 + pri->t202_timer[0] = 0;
3364 + pri->tei = h->u.data[4] >> 1;
3366 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3367 + pri_message(pri, "received TEI assign TEI = %d ri=%d\n",pri->tei,(unsigned short) (h->u.data[1] << 8) + h->u.data[2]);
3368 + pri->sabme_retrans[teio] = 0;
3369 + q921_send_sabme_now(pri, pri->tei);
3372 + case Q921_TEI_ID_REMOVE:
3373 + pri_error(pri, "TEI remove TEI = %d\n",(h->u.data[4] >> 1));
3374 + if (pri->localtype != BRI_CPE_PTMP)
3376 + if (((h->u.data[4] >> 1) == Q921_TEI_GROUP) || (pri->tei == (h->u.data[4] >> 1))){
3377 + if (pri->debug & PRI_DEBUG_Q921_STATE)
3378 + pri_message(pri, "received TEI remove TEI = %d\n",pri->tei);
3382 + q921_reset(pri, 0, 1);
3383 + q921_send_teireq(pri);
3386 + case Q921_TEI_ID_REQUEST:
3387 + if (pri->localtype != BRI_NETWORK_PTMP)
3390 + tei = h->u.data[4] >> 1;
3391 + if (tei != Q921_TEI_GROUP) {
3392 + pri_message(pri, "got TEI request for unavailable TEI..\n");
3393 + q921_send_teidenied(pri,((unsigned int)(h->u.data[1] << 8) + h->u.data[2]),h->u.data[4] >> 1);
3397 + for (tei=0;tei<Q921_MAX_TEIS;tei++) {
3398 + if (pri->q921_teis[tei] == 0) {
3399 + pri->q921_teis[tei] = 1;
3403 + if (tei < Q921_MAX_TEIS) {
3404 + q921_send_teiassign(pri,((unsigned int)(h->u.data[1] << 8) + h->u.data[2]),tei + Q921_TEI_BASE);
3406 + pri_error(pri, "no TEI available. starting TEI recovery procedure. dont worry!\n");
3407 + q921_tei_recovery(pri, 127);
3411 + case Q921_TEI_ID_CHK_REQ:
3412 + if (pri->localtype != BRI_CPE_PTMP)
3414 + if ((((h->u.data[4] >> 1) == Q921_TEI_GROUP) || ((h->u.data[4] >> 1) == 0) || ((h->u.data[4] >> 1) == pri->tei)) && (pri->tei > 0)) {
3415 + pri_message(pri, "received TEI check request for TEI = %d\n",h->u.data[4] >> 1);
3416 + q921_send_teichkresp(pri, pri->tei);
3419 + case Q921_TEI_ID_CHK_RES:
3420 + if (pri->localtype != BRI_NETWORK_PTMP)
3422 + teio = (h->u.data[4] >> 1) - Q921_TEI_BASE;
3423 + if ((teio < 0) || (teio >= Q921_MAX_TEIS)) break;
3424 + if (pri->q921_tei_check[teio] == 1) {
3425 + pri->q921_tei_check_ri[teio] = (h->u.data[1] << 8) + h->u.data[2];
3426 + pri->q921_tei_check[teio] = 0;
3429 + pri_error(pri, "double assgined tei for tei %d teio %d\n", h->u.data[4] >> 1, teio);
3430 + pri->q921_tei_check[teio] = 0;
3431 + q921_start_tei(pri, h->u.data[4] >> 1);
3435 + pri_message(pri, "Ri = %d TEI msg = %x TEI = %x\n", (h->u.data[1] << 8) + h->u.data[2], h->u.data[3], h->u.data[4] >> 1);
3438 + case Q931_PROTOCOL_DISCRIMINATOR:
3439 + if (pri->localtype == BRI_CPE_PTMP) {
3440 + res = q931_receive(pri, (q931_h *)h->u.data, len-3, h->h.tei);
3441 + /* Send an RR if one wasn't sent already */
3442 + if (pri->v_na[teio] != pri->v_r[teio])
3443 + q921_rr(pri, 0, 0, pri->tei);
3447 + if (res & Q931_RES_HAVEEVENT)
3453 + pri_message(pri, "XXX Unnumbered Information not implemented XXX\n");
3458 if (pri->debug & PRI_DEBUG_Q921_STATE)
3459 pri_message(pri, "-- Got Disconnect from peer.\n");
3460 + if (pri->q921_state[teio] != Q921_LINK_CONNECTION_ESTABLISHED) {
3461 + q921_send_dm(pri, 1, h->h.tei);
3465 - q921_send_ua(pri, h->u.p_f);
3466 - ev = q921_dchannel_down(pri);
3467 - q921_start(pri, 0);
3468 + q921_send_ua(pri, h->u.p_f, h->h.tei);
3469 + ev = q921_dchannel_down(pri, h->h.tei);
3470 + if ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE) || (pri->localtype == BRI_NETWORK)) {
3471 + /* release layer 2 */
3474 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)){
3475 + q921_start(pri, 0, 0);
3480 @@ -866,17 +1755,26 @@
3483 /* Send Unnumbered Acknowledgement */
3484 - q921_send_ua(pri, h->u.p_f);
3485 - return q921_dchannel_up(pri);
3486 + q921_send_ua(pri, h->u.p_f, h->h.tei);
3487 + return q921_dchannel_up(pri, h->h.tei);
3488 } else if (h->u.m2 == 0) {
3490 - if (pri->q921_state == Q921_AWAITING_ESTABLISH) {
3491 + if (pri->q921_state[teio] == Q921_AWAITING_ESTABLISH) {
3492 if (pri->debug & PRI_DEBUG_Q921_STATE) {
3493 pri_message(pri, "-- Got UA from %s peer Link up.\n", h->h.c_r ? "cpe" : "network");
3495 - return q921_dchannel_up(pri);
3497 - pri_error(pri, "!! Got a UA, but i'm in state %d\n", pri->q921_state);
3498 + return q921_dchannel_up(pri, h->h.tei);
3500 + pri_error(pri, "!! Got a UA, but i'm in state %d\n", pri->q921_state[teio]);
3501 + if ((pri->localtype == BRI_CPE_PTMP) || (pri->localtype == BRI_CPE)) {
3502 + q921_reset(pri, h->h.tei, 1);
3503 + q921_send_teiverify(pri, h->h.tei);
3506 + // q921_send_dm(pri, 1, h->h.tei);
3507 + q921_reset(pri, h->h.tei, 1);
3511 pri_error(pri, "!! Weird frame received (m3=3, m2 = %d)\n", h->u.m2);
3513 @@ -901,19 +1799,42 @@
3517 - if (!pri->master && pri->debug & PRI_DEBUG_Q921_DUMP)
3518 - q921_dump(pri, h, len, pri->debug & PRI_DEBUG_Q921_RAW, 0);
3520 /* Check some reject conditions -- Start by rejecting improper ea's */
3521 if (h->h.ea1 || !(h->h.ea2))
3523 + if ((pri->localtype == PRI_CPE) || (pri->localtype == PRI_NETWORK)) {
3524 + /* Check for broadcasts - not yet handled (for PRI) */
3525 + if (h->h.tei == Q921_TEI_GROUP) {
3528 + } else if ((pri->localtype == BRI_CPE) || (pri->localtype == BRI_CPE_PTMP)) {
3529 + if ((h->h.tei != pri->tei) && (h->h.tei != Q921_TEI_GROUP)) {
3532 + } else if (pri->localtype == BRI_NETWORK_PTMP) {
3533 + /* discard anything from a strange TEI (strange == not assigned by us or the broadcast tei) */
3534 + if (((h->h.tei < Q921_TEI_BASE) || (h->h.tei > Q921_TEI_BASE + Q921_MAX_TEIS)) && (h->h.tei != Q921_TEI_GROUP)) {
3535 + if (pri->debug & PRI_DEBUG_Q921_DUMP)
3536 + pri_message(pri, "Received a Q.921 message from strange/unassigned TEI %d.\n");
3539 + if ((pri->q921_teis[h->h.tei - Q921_TEI_BASE] != 1) && (h->h.tei != Q921_TEI_GROUP)) {
3540 + if (pri->debug & PRI_DEBUG_Q921_DUMP)
3541 + pri_message(pri, "Received a Q.921 message from unassigned TEI %d.. Sending DM and assigning.\n", h->h.tei);
3543 + q921_send_dm(pri, 1, h->h.tei);
3544 + pri->q921_teis[h->h.tei - Q921_TEI_BASE] = 1;
3549 + if (!pri->master && pri->debug & PRI_DEBUG_Q921_DUMP)
3550 + q921_dump(pri, h, len, pri->debug & PRI_DEBUG_Q921_RAW, 0);
3552 - /* Check for broadcasts - not yet handled */
3553 - if (h->h.tei == Q921_TEI_GROUP)
3556 + if (pri->localtype != BRI_NETWORK_PTMP) {
3557 /* Check for SAPIs we don't yet handle */
3558 - if ((h->h.sapi != pri->sapi) || (h->h.tei != pri->tei)) {
3559 + if (((h->h.sapi != pri->sapi) && (h->h.sapi != Q921_SAPI_LAYER2_MANAGEMENT)) || ((h->h.tei != pri->tei) && (h->h.tei != Q921_TEI_GROUP) )) {
3560 #ifdef PROCESS_SUBCHANNELS
3561 /* If it's not us, try any subchannels we have */
3562 if (pri->subchannel)
3563 @@ -921,10 +1842,16 @@
3570 ev = __q921_receive_qualified(pri, h, len);
3571 - reschedule_t203(pri);
3574 + if (pri->localtype == BRI_CPE_PTMP) {
3575 + reschedule_t203(pri, pri->tei);
3577 + reschedule_t203(pri, h->h.tei);
3582 @@ -938,14 +1865,58 @@
3586 -void q921_start(struct pri *pri, int now)
3587 +static void q921_start_tei(struct pri *pri, int tei)
3589 - if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED) {
3590 - pri_error(pri, "!! q921_start: Not in 'Link Connection Released' state\n");
3592 + int teio=tei - Q921_TEI_BASE;
3593 + if (pri->localtype != BRI_NETWORK_PTMP) { return; }
3594 + if (((teio < 0) || (teio > Q921_MAX_TEIS))) { teio=0; }
3595 + pri->q921_teis[teio] = 0;
3596 + q921_send_teiremove(pri, tei);
3597 + q921_reset(pri,tei,1);
3600 +void q921_start(struct pri *pri, int now, int tei)
3603 +/* if (pri->q921_state != Q921_LINK_CONNECTION_RELEASED) {
3604 + pri_error(pri, "!! q921_start: Not in 'Link Connection Released' state\n");
3607 + /* Reset our interface */
3608 + if (pri->localtype != BRI_NETWORK_PTMP) {
3609 + q921_reset(pri,0,1);
3611 + /* Do the SABME XXX Maybe we should implement T_WAIT? XXX */
3612 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE) || (pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_CPE)) {
3613 + pri->sabme_retrans[0] = 0;
3614 + q921_send_sabme(pri, now, 0);
3617 + if (pri->localtype == BRI_NETWORK_PTMP) {
3619 + // initial start or complete restart
3620 + q921_send_teiremove(pri, 127);
3622 + for (i=0;i<Q921_MAX_TEIS;i++) {
3623 + q921_start_tei(pri,Q921_TEI_BASE+i);
3625 + /* clean up those TEIs */
3626 +// pri_schedule_event2(pri, 10000, q921_invoke_tei_recovery, pri, 127 );
3627 + } else if ((tei >= Q921_TEI_BASE) && (tei < Q921_TEI_BASE + Q921_MAX_TEIS)){
3628 + // restart of a single p2p datalink
3629 + q921_start_tei(pri,tei);
3632 + if (pri->localtype == BRI_CPE_PTMP){
3635 + /* let's get a TEI */
3636 + q921_send_teireq(pri);
3639 + /* save the planet by recycling */
3640 + pri->sabme_retrans[0] = 0;
3641 + q921_send_sabme(pri, now, tei);
3644 - /* Reset our interface */
3646 - /* Do the SABME XXX Maybe we should implement T_WAIT? XXX */
3647 - q921_send_sabme(pri, now);
3649 diff -urN libpri-1.2.2.orig/q931.c libpri-1.2.2/q931.c
3650 --- libpri-1.2.2.orig/q931.c 2006-01-17 14:43:18.000000000 +0100
3651 +++ libpri-1.2.2/q931.c 2006-01-27 05:41:13.000000000 +0100
3654 * libpri: An implementation of Primary Rate ISDN
3656 - * Written by Mark Spencer <markster@digium.com>
3657 + * Written by Mark Spencer <markster@linux-support.net>
3659 - * Copyright (C) 2001-2005, Digium
3660 + * Copyright (C) 2001, Linux Support Services, Inc.
3661 * All Rights Reserved.
3662 + * Copyright (C) 2003,2004,2005 Junghanns.NET GmbH
3663 + * Klaus-Peter Junghanns <kpj@junghanns.net>
3665 * This program is free software; you can redistribute it and/or modify
3666 * it under the terms of the GNU General Public License as published by
3668 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
3675 #include "pri_internal.h"
3684 @@ -205,6 +208,14 @@
3685 #define LOC_INTERNATIONAL_NETWORK 0x7
3686 #define LOC_NETWORK_BEYOND_INTERWORKING 0xa
3696 static char *ie2str(int ie);
3697 static char *msg2str(int msg);
3700 static void call_init(struct q931_call *c)
3702 memset(c, 0, sizeof(*c));
3705 c->sendhangupack = 0;
3706 c->forceinvert = -1;
3707 @@ -253,6 +265,10 @@
3712 + c->t303running = 0;
3715 c->ourcallstate = Q931_CALL_STATE_NULL;
3716 c->peercallstate = Q931_CALL_STATE_NULL;
3718 @@ -272,14 +288,17 @@
3722 -#ifdef NO_BRI_SUPPORT
3723 - if (!ie->data[0] & 0x20) {
3724 - pri_error(pri, "!! Not PRI type!?\n");
3726 + if ((pri->localtype != PRI_CPE) && (pri->localtype != PRI_NETWORK)) {
3727 + // pri_error(pri, "!! BRI type %d!?\n",ie->data[0] & 0x03);
3728 + call->channelno = ie->data[0] & 0x03;
3729 + if (call->channelno == 3) {
3730 + call->channelno = -1; // any channel
3735 #ifndef NOAUTO_CHANNEL_SELECTION_SUPPORT
3736 - switch (ie->data[0] & 3) {
3737 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
3738 + switch (ie->data[0] & 3) {
3740 call->justsignalling = 1;
3744 pri_error(pri, "!! Unexpected Channel selection %d\n", ie->data[0] & 3);
3749 if (ie->data[0] & 0x08)
3750 @@ -349,10 +369,16 @@
3753 /* Start with standard stuff */
3754 - if (pri->switchtype == PRI_SWITCH_GR303_TMC)
3755 + if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
3756 ie->data[pos] = 0x69;
3759 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
3760 ie->data[pos] = 0xa1;
3763 + ie->data[pos] = 0x80;
3766 /* Add exclusive flag if necessary */
3767 if (call->chanflags & FLAG_EXCLUSIVE)
3768 ie->data[pos] |= 0x08;
3772 if ((call->channelno > -1) || (call->slotmap != -1)) {
3773 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
3774 /* We'll have the octet 8.2 and 8.3's present */
3775 ie->data[pos++] = 0x83;
3776 if (call->channelno > -1) {
3777 @@ -384,11 +411,43 @@
3778 ie->data[pos++] = (call->slotmap & 0xff);
3784 + // pri_error(pri, "channelno %d, ds1no %d data %d\n",call->channelno,call->ds1no,ie->data[pos-1]);
3785 + if (pri->localtype == BRI_CPE_PTMP) {
3786 + if (msgtype == Q931_SETUP) {
3787 + // network, you decide!
3788 + if (call->channelno > -1) {
3789 + // ie->data[pos-1] = 0x83;
3790 + ie->data[pos-1] = 0x80 | call->channelno;
3793 + if (call->channelno > -1) {
3794 + ie->data[pos-1] |= call->channelno;
3798 + if (call->channelno > -1) {
3799 + ie->data[pos-1] |= call->channelno;
3805 + if (pri->localtype == BRI_CPE) {
3806 + ie->data[pos++] = 0x80 | 3;
3811 if (call->ds1no > 0) {
3815 + if (msgtype == Q931_RESTART_ACKNOWLEDGE) {
3816 + /* restart complete interface! */
3819 pri_error(pri, "!! No channel map, no channel, and no ds1? What am I supposed to identify?\n");
3822 @@ -734,8 +793,12 @@
3823 return code2str(pres, press, sizeof(press) / sizeof(press[0]));
3826 -static void q931_get_number(unsigned char *num, int maxlen, unsigned char *src, int len)
3827 +static void q931_get_number(char *num, int maxlen, unsigned char *src, int len)
3830 + pri_error(NULL, "q931_get_number received invalid len = %d\n", len);
3833 if (len > maxlen - 1) {
3836 @@ -746,50 +809,75 @@
3838 static FUNC_DUMP(dump_called_party_number)
3840 - unsigned char cnum[256];
3842 - q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
3843 - pri_message(pri, "%c Called Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d) '%s' ]\n",
3844 - prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f, cnum);
3847 + q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
3848 + pri_message(pri, "%c Called Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d) '%s' ]\n",
3849 + prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f, cnum);
3851 + pri_error(pri, "Called Number (len=%2d) too short.\n", len);
3855 static FUNC_DUMP(dump_called_party_subaddr)
3857 - unsigned char cnum[256];
3858 - q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
3859 - pri_message(pri, "%c Called Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
3860 - prefix, len, ie->data[0] >> 7,
3861 - subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
3862 - (ie->data[0] & 0x08) >> 3, cnum);
3865 + q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
3866 + pri_message(pri, "%c Called Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
3867 + prefix, len, ie->data[0] >> 7,
3868 + subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
3869 + (ie->data[0] & 0x08) >> 3, cnum);
3871 + pri_error(pri, "Called Party Subaddress (len=%2d) too short.\n", len);
3875 static FUNC_DUMP(dump_calling_party_number)
3877 - unsigned char cnum[256];
3878 - if (ie->data[0] & 0x80)
3879 - q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
3881 - q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
3882 - pri_message(pri, "%c Calling Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
3883 - if (ie->data[0] & 0x80)
3884 - pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
3886 - pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
3888 + if ((ie->data[0] & 0x80) && (len >= 3)) {
3889 + q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
3890 + pri_message(pri, "%c Calling Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
3891 + pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
3892 + } else if (len >= 4) {
3893 + q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
3894 + pri_message(pri, "%c Calling Number (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
3895 + pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
3897 + pri_error(pri, "Calling Party Number (len=%2d) too short.\n", len);
3901 static FUNC_DUMP(dump_calling_party_subaddr)
3903 - unsigned char cnum[256];
3904 - q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
3905 - pri_message(pri, "%c Calling Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
3906 - prefix, len, ie->data[0] >> 7,
3907 - subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
3908 - (ie->data[0] & 0x08) >> 3, cnum);
3911 + q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
3912 + pri_message(pri, "%c Calling Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
3913 + prefix, len, ie->data[0] >> 7,
3914 + subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
3915 + (ie->data[0] & 0x08) >> 3, cnum);
3917 + pri_error(pri, "Calling Party Subaddress (len=%2d) too short.\n", len);
3921 +static FUNC_DUMP(dump_colp)
3925 + q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
3926 + pri_message(pri, "%c COLP (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n", prefix, len, ie->data[0] >> 7, ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07, npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
3927 + pri_message(pri, "%c Presentation: %s (%d) '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
3929 + pri_error(pri, "COLP (len=%2d) too short.\n", len);
3933 static FUNC_DUMP(dump_redirecting_number)
3935 - unsigned char cnum[256];
3938 /* To follow Q.931 (4.5.1), we must search for start of octet 4 by
3939 walking through all bytes until one with ext bit (8) set to 1 */
3940 @@ -810,13 +898,17 @@
3943 while(!(ie->data[i++]& 0x80));
3944 - q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
3945 - pri_message(pri, " '%s' ]\n", cnum);
3946 + if ((ie->len - i) >= 0) {
3947 + q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
3948 + pri_message(pri, " '%s' ]\n", cnum);
3950 + pri_error(pri, "Redirecting Number (len=%2d) too short.\n", len);
3954 static FUNC_DUMP(dump_connected_number)
3956 - unsigned char cnum[256];
3959 /* To follow Q.931 (4.5.1), we must search for start of octet 4 by
3960 walking through all bytes until one with ext bit (8) set to 1 */
3961 @@ -833,8 +925,12 @@
3964 while(!(ie->data[i++]& 0x80));
3965 - q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
3966 - pri_message(pri, " '%s' ]\n", cnum);
3967 + if ((ie->len - i) >= 0) {
3968 + q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
3969 + pri_message(pri, " '%s' ]\n", cnum);
3971 + pri_error(pri, "Connected Number (len=%2d) too short.\n", len);
3979 while(!(ie->data[i++] & 0x80));
3980 - q931_get_number((unsigned char *) call->redirectingnum, sizeof(call->redirectingnum), ie->data + i, ie->len - i);
3981 + q931_get_number(call->redirectingnum, sizeof(call->redirectingnum), ie->data + i, ie->len - i);
3989 - if (call->redirectingnum && *call->redirectingnum) {
3990 + if (call->redirectingnum && strlen(call->redirectingnum)) {
3991 ie->data[0] = call->redirectingplan;
3992 ie->data[1] = call->redirectingpres;
3993 ie->data[2] = (call->redirectingreason & 0x0f) | 0x80;
3994 @@ -878,67 +974,90 @@
3996 static FUNC_DUMP(dump_redirecting_subaddr)
3998 - unsigned char cnum[256];
3999 - q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
4000 - pri_message(pri, "%c Redirecting Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4001 - prefix, len, ie->data[0] >> 7,
4002 - subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4003 - (ie->data[0] & 0x08) >> 3, cnum);
4006 + q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
4007 + pri_message(pri, "%c Redirecting Sub-Address (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
4008 + prefix, len, ie->data[0] >> 7,
4009 + subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
4010 + (ie->data[0] & 0x08) >> 3, cnum);
4012 + pri_error(pri, "Redirecting Subaddress (len=%2d) too short.\n", len);
4016 static FUNC_RECV(receive_calling_party_subaddr)
4018 /* copy digits to call->callingsubaddr */
4019 - q931_get_number((unsigned char *) call->callingsubaddr, sizeof(call->callingsubaddr), ie->data + 2, len - 4);
4021 + q931_get_number(call->callingsubaddr, sizeof(call->callingsubaddr), ie->data + 2, len - 4);
4023 + pri_error(call->pri, "Calling Party Subaddress (len=%2d) too short.\n", len);
4028 static FUNC_RECV(receive_called_party_number)
4030 - /* copy digits to call->callednum */
4031 - q931_get_number((unsigned char *) call->callednum, sizeof(call->callednum), ie->data + 1, len - 3);
4032 - call->calledplan = ie->data[0] & 0x7f;
4033 + /* copy digits to call->callednum or call->digits */
4035 + if (msgtype == Q931_INFORMATION) {
4036 + q931_get_number(call->digits, sizeof(call->digits), ie->data + 1, len - 3);
4038 + q931_get_number(call->callednum, sizeof(call->callednum), ie->data + 1, len - 3);
4040 + call->calledplan = ie->data[0] & 0x7f;
4042 + pri_error(call->pri, "Called Party Number (len=%2d) too short.\n", len);
4047 static FUNC_SEND(transmit_called_party_number)
4049 ie->data[0] = 0x80 | call->calledplan;
4050 - if (*call->callednum)
4051 + if (strlen(call->callednum))
4052 memcpy(ie->data + 1, call->callednum, strlen(call->callednum));
4053 return strlen(call->callednum) + 3;
4056 static FUNC_RECV(receive_calling_party_number)
4061 - if (ie->data[0] & 0x80) {
4062 - data = ie->data + 1;
4064 - call->callerpres = 0; /* PI presentation allowed SI user-provided, not screened */
4066 - data = ie->data + 2;
4068 - call->callerpres = ie->data[1] & 0x7f;
4071 - if (call->callerpres == PRES_ALLOWED_NETWORK_NUMBER ||
4072 - call->callerpres == PRES_PROHIB_NETWORK_NUMBER) {
4073 - q931_get_number((u_int8_t *)call->callerani, sizeof(call->callerani), data, length);
4074 - call->callerplanani = ie->data[0] & 0x7f;
4076 - if (!*call->callernum) { /*Copy ANI to CallerID if CallerID is not already set */
4077 - libpri_copy_string(call->callernum, call->callerani, sizeof(call->callernum));
4078 - call->callerplan = call->callerplanani;
4082 - q931_get_number((u_int8_t *)call->callernum, sizeof(call->callernum), data, length);
4083 - call->callerplan = ie->data[0] & 0x7f;
4086 + if (strlen(call->callernum)) {
4087 + call->callerplanuser = ie->data[0] & 0x7f;
4089 + call->callerplan = ie->data[0] & 0x7f;
4092 + if (ie->data[0] & 0x80) {
4094 + if (strlen(call->callernum)) {
4095 + // got A NUM already (this is not 100% correct, but the network should do it this way to protect bad implementations
4096 + q931_get_number(call->callerani, sizeof(call->callerani), ie->data + 1, len - 3);
4097 + call->callerpresuser = 0; /* PI presentation allowed
4098 + SI user-provided, not screened */
4100 + q931_get_number(call->callernum, sizeof(call->callernum), ie->data + 1, len - 3);
4101 + call->callerpres = 0; /* PI presentation allowed
4102 + SI user-provided, not screened */
4105 + pri_error(call->pri, "Calling Party Number (len=%2d) too short.\n", len);
4109 + if (strlen(call->callernum)) {
4110 + q931_get_number(call->callerani, sizeof(call->callerani), ie->data + 2, len - 4);
4111 + call->callerpresuser = ie->data[1] & 0x7f;
4113 + q931_get_number(call->callernum, sizeof(call->callernum), ie->data + 2, len - 4);
4114 + call->callerpres = ie->data[1] & 0x7f;
4117 + pri_error(call->pri, "Calling Party Number (len=%2d) too short.\n", len);
4123 @@ -946,7 +1065,7 @@
4125 ie->data[0] = call->callerplan;
4126 ie->data[1] = 0x80 | call->callerpres;
4127 - if (*call->callernum)
4128 + if (strlen(call->callernum))
4129 memcpy(ie->data + 2, call->callernum, strlen(call->callernum));
4130 return strlen(call->callernum) + 4;
4132 @@ -964,11 +1083,58 @@
4133 static FUNC_RECV(receive_user_user)
4135 call->useruserprotocoldisc = ie->data[0] & 0xff;
4136 - if (call->useruserprotocoldisc == 4) /* IA5 */
4137 - q931_get_number((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
4138 + if (call->useruserprotocoldisc == 4) { /* IA5 */
4140 + q931_get_number(call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
4142 + pri_error(call->pri, "User-User Information (len=%2d) too short.\n", len);
4148 +static FUNC_RECV(receive_call_identity)
4151 + q931_get_number(call->callid, sizeof(call->callid), ie->data, len - 2);
4153 + pri_error(call->pri, "Call Identity (len=%2d) too short.\n", len);
4158 +static FUNC_SEND(transmit_call_identity)
4160 + if (strlen(call->callid))
4161 + memcpy(ie->data , call->callid, strlen(call->callid));
4162 + return strlen(call->callednum) + 3;
4165 +static FUNC_RECV(receive_high_layer_compat)
4170 +static FUNC_SEND(transmit_high_layer_compat)
4172 + if (call->transcapability != PRI_TRANS_CAP_RESTRICTED_DIGITAL && call->transcapability != PRI_TRANS_CAP_DIGITAL && call->transcapability != PRI_TRANS_CAP_DIGITAL_W_TONES) {
4173 + ie->data[0] = 0x91;
4174 + ie->data[1] = 0x81;
4180 +static FUNC_DUMP(dump_high_layer_compat)
4183 + pri_message(pri, "%c High-layer compatibilty (len=%2d) [ ", prefix, len);
4184 + for (x=0;x<ie->len;x++)
4185 + pri_message(pri, "0x%02X ", ie->data[x]);
4186 + pri_message(pri, " ]\n");
4190 static FUNC_SEND(transmit_user_user)
4192 int datalen = strlen(call->useruserinfo);
4193 @@ -1050,22 +1216,41 @@
4197 - q931_get_number((unsigned char *) call->callername, sizeof(call->callername), data, len - 2);
4198 + if (msgtype == Q931_SETUP) {
4199 + /* we treat display IEs in the SETUP msg as callername */
4201 + q931_get_number(call->callername, sizeof(call->callername), data, len - 2);
4203 + pri_error(call->pri, "Display (len=%2d) too short.\n", len);
4206 + /* in other msgs we will pass it as text to chan_zap */
4208 + q931_get_number(call->display, sizeof(call->display), data, len - 2);
4210 + pri_error(call->pri, "Display (len=%2d) too short.\n", len);
4216 static FUNC_SEND(transmit_display)
4219 - if ((pri->switchtype != PRI_SWITCH_NI1) && (pri->switchtype != PRI_SWITCH_QSIG)
4220 - && *call->callername) {
4221 + int cpe = pri->localtype == BRI_CPE || pri->localtype == BRI_CPE_PTMP || pri->localtype == PRI_CPE;
4222 + if ((pri->switchtype != PRI_SWITCH_NI1) && (pri->switchtype != PRI_SWITCH_QSIG) && strlen(call->callername) && !cpe) {
4224 if(pri->switchtype != PRI_SWITCH_EUROISDN_E1) {
4228 - memcpy(ie->data + i, call->callername, strlen(call->callername));
4229 - return 2 + i + strlen(call->callername);
4230 + if (msgtype == Q931_SETUP_ACKNOWLEDGE) {
4231 + memcpy(ie->data + i, call->display, strlen(call->display));
4232 + return 2 + i + strlen(call->display);
4234 + memcpy(ie->data + i, call->callername, strlen(call->callername));
4235 + return 2 + i + strlen(call->callername);
4240 @@ -1112,6 +1297,111 @@
4245 + static FUNC_RECV(receive_facility_kpj)
4247 + unsigned char cpt_tag, cp_len, invoke_id_tag, invoke_id_len, operation_value_len, operation_value_tag;
4248 + unsigned char arg_len = 0;
4249 + unsigned char pos = 0;
4250 + short invoke_id = 0, operation_value = 0;
4251 + if ((ie->data[pos++] & 0x1F) == 0x11) {
4252 + /* service discriminator == supplementary services */
4253 + cpt_tag = ie->data[pos++] & 0x1F;
4254 + cp_len = ie->data[pos++];
4255 + switch (cpt_tag) {
4256 + case 1: /* invoke */
4257 + invoke_id_tag = ie->data[pos++];
4258 + if (invoke_id_tag != 0x02) {
4259 + // pri_error(call->pri, "invoke id tag != 0x02\n");
4262 + invoke_id_len = ie->data[pos++]; // 4
4263 + while (invoke_id_len > 0) {
4264 + invoke_id = (invoke_id << 8) | (ie->data[pos++] & 0xFF);
4267 + operation_value_tag = ie->data[pos++];
4268 + if (operation_value_tag != 0x02) {
4269 + // pri_error(call->pri, "operation value tag != 0x02\n");
4272 + operation_value_len = ie->data[pos++];
4273 + while (operation_value_len > 0) {
4274 + operation_value = (operation_value << 8) | (ie->data[pos++] & 0xFF);
4275 + operation_value_len--;
4277 + arg_len = ie->len - pos;
4278 + switch (operation_value) {
4279 + case 0x06: /* ECT execute */
4280 + call->facility = operation_value;
4282 + case 0x0D: /* call deflection */
4283 + call->facility = operation_value;
4286 + if (arg_len > 0) {
4288 + q931_get_number(call->redirectingnum, sizeof(call->redirectingnum), ie->data + pos, arg_len);
4290 + /* now retrieve the number */
4292 + case 0x22: /* AOC-D */
4294 + case 0x24: /* AOC-E */
4298 + case 2: /* return result */
4300 + case 3: /* return error */
4302 + case 4: /* reject */
4306 + /* OLD DIRTY ULAW HACK */
4307 + if (ie->len < 14) {
4308 + pri_error(call->pri, "!! Facility message shorter than 14 bytes\n");
4311 + if (ie->data[13] + 14 == ie->len) {
4312 + q931_get_number(call->callername, sizeof(call->callername) - 1, ie->data + 14, ie->len - 14);
4314 + call->facility = 0x0;
4319 + static FUNC_SEND(transmit_facility_kpj)
4323 + if (call->aoc && (pri->switchtype == PRI_SWITCH_EUROISDN_E1) && ((pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_NETWORK_PTMP) || (pri->localtype == PRI_NETWORK))) {
4324 + ie->data[0] = 0x90; /* PP remote operations */
4325 + ie->data[i++] = 0x00; /* component tag */
4326 + ie->data[i++] = 0x02; /* invoke id tag */
4327 + ie->data[i++] = 0x02; /* invoke id len */
4328 + ie->data[i++] = 0x34; /* invoke id */
4329 + ie->data[i++] = 0x56; /* invoke id */
4330 + ie->data[i++] = 0x02; /* operation value tag */
4331 + ie->data[i++] = 0x01; /* operation value len */
4332 + switch (msgtype) {
4334 + ie->data[i++] = 0x26; /* operation value AOC-S */
4336 + case Q931_DISCONNECT:
4337 + ie->data[i++] = 0x24; /* operation value AOC-E */
4340 + ie->data[i++] = 0x22; /* operation value AOC-D */
4349 static FUNC_SEND(transmit_facility)
4351 struct apdu_event *tmp;
4352 @@ -1138,6 +1428,182 @@
4357 +static FUNC_SEND(transmit_facility)
4359 + int i = 0, j, first_i, compsp = 0;
4360 + struct rose_component *comp, *compstk[10];
4361 + unsigned char namelen = strlen(call->callername);
4363 + if ((pri->switchtype == PRI_SWITCH_NI2) && (namelen > 15))
4364 + namelen = 15; /* According to GR-1367, for NI2 switches it can't be > 15 characters */
4365 + if ((namelen > 0) && ((pri->switchtype == PRI_SWITCH_QSIG) ||
4366 + ((pri->switchtype == PRI_SWITCH_NI2) && (pri->localtype == PRI_NETWORK)))) {
4369 + ie->data[i] = 0x80 | Q932_PROTOCOL_EXTENSIONS;
4371 + /* Interpretation component */
4372 + ASN1_ADD_BYTECOMP(comp, COMP_TYPE_INTERPRETATION, ie->data, i, 0x00 /* Discard unrecognized invokes */);
4375 + ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, ie->data, i);
4376 + ASN1_PUSH(compstk, compsp, comp);
4378 + /* Invoke component contents */
4380 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, ++pri->last_invoke);
4382 + /* Operation Tag */
4383 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, SS_CNID_CALLINGNAME);
4385 + /* Arugement Tag */
4386 + j = asn1_string_encode(ROSE_NAME_PRESENTATION_ALLOWED_SIMPLE, &ie->data[i], len - i, 15, call->callername, namelen);
4393 + /* Fix length of stacked components */
4394 + while(compsp > 0) {
4395 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4399 + if (/*(pri->switchtype == PRI_SWITCH_EUROISDN_E1) &&*/ call->redirectingnum && strlen(call->redirectingnum)) {
4400 + if (!(first_i = i)) {
4401 + /* Add protocol information header */
4402 + ie->data[i++] = 0x80 | Q932_PROTOCOL_ROSE;
4405 + /* ROSE invoke component */
4406 + ASN1_ADD_SIMPLE(comp, COMP_TYPE_INVOKE, ie->data, i);
4407 + ASN1_PUSH(compstk, compsp, comp);
4409 + /* ROSE invokeId component */
4410 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, ++pri->last_invoke);
4412 + /* ROSE operationId component */
4413 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, ROSE_DIVERTING_LEG_INFORMATION2);
4415 + /* ROSE ARGUMENT component */
4416 + ASN1_ADD_SIMPLE(comp, 0x30, ie->data, i);
4417 + ASN1_PUSH(compstk, compsp, comp);
4419 + /* ROSE DivertingLegInformation2.diversionCounter component */
4420 + /* Always is 1 because other isn't available in the current design */
4421 + ASN1_ADD_BYTECOMP(comp, ASN1_INTEGER, ie->data, i, 1);
4423 + /* ROSE DivertingLegInformation2.diversionReason component */
4424 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, ie->data, i, redirectingreason_from_q931(pri, call->redirectingreason));
4426 + /* ROSE DivertingLegInformation2.divertingNr component */
4427 + ASN1_ADD_SIMPLE(comp, 0xA1, ie->data, i);
4428 + ASN1_PUSH(compstk, compsp, comp);
4430 + /* Redirecting information always not screened */
4431 + switch(call->redirectingpres) {
4432 + case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
4433 + case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
4434 + if (call->redirectingnum && strlen(call->redirectingnum)) {
4435 + ASN1_ADD_SIMPLE(comp, 0xA0, ie->data, i);
4436 + ASN1_PUSH(compstk, compsp, comp);
4438 + /* NPI of redirected number is not supported in the current design */
4439 + ASN1_ADD_SIMPLE(comp, 0xA1, ie->data, i);
4440 + ASN1_PUSH(compstk, compsp, comp);
4442 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, ie->data, i, typeofnumber_from_q931(pri, call->redirectingplan >> 4));
4444 + j = asn1_string_encode(ASN1_NUMERICSTRING, &ie->data[i], len - i, 20, call->redirectingnum, strlen(call->redirectingnum));
4450 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4451 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4454 + /* fall through */
4455 + case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
4456 + case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
4457 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4459 + /* Don't know how to handle this */
4460 + case PRES_ALLOWED_NETWORK_NUMBER:
4461 + case PRES_PROHIB_NETWORK_NUMBER:
4462 + case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
4463 + case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
4464 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4467 + pri_message(pri, "!! Undefined presentation value for redirecting number: %d\n", call->redirectingpres);
4468 + case PRES_NUMBER_NOT_AVAILABLE:
4469 + ASN1_ADD_SIMPLE(comp, 0x82, ie->data, i);
4472 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4474 + /* ROSE DivertingLegInformation2.originalCalledNr component */
4475 + /* This information isn't supported by current design - duplicate divertingNr */
4476 + ASN1_ADD_SIMPLE(comp, 0xA2, ie->data, i);
4477 + ASN1_PUSH(compstk, compsp, comp);
4479 + /* Redirecting information always not screened */
4480 + switch(call->redirectingpres) {
4481 + case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
4482 + case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
4483 + if (call->redirectingnum && strlen(call->redirectingnum)) {
4484 + ASN1_ADD_SIMPLE(comp, 0xA0, ie->data, i);
4485 + ASN1_PUSH(compstk, compsp, comp);
4487 + ASN1_ADD_SIMPLE(comp, 0xA1, ie->data, i);
4488 + ASN1_PUSH(compstk, compsp, comp);
4490 + ASN1_ADD_BYTECOMP(comp, ASN1_ENUMERATED, ie->data, i, typeofnumber_from_q931(pri, call->redirectingplan >> 4));
4492 + j = asn1_string_encode(ASN1_NUMERICSTRING, &ie->data[i], len - i, 20, call->redirectingnum, strlen(call->redirectingnum));
4498 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4499 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4502 + /* fall through */
4503 + case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
4504 + case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
4505 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4507 + /* Don't know how to handle this */
4508 + case PRES_ALLOWED_NETWORK_NUMBER:
4509 + case PRES_PROHIB_NETWORK_NUMBER:
4510 + case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
4511 + case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
4512 + ASN1_ADD_SIMPLE(comp, 0x81, ie->data, i);
4515 + pri_message(pri, "!! Undefined presentation value for redirecting number: %d\n", call->redirectingpres);
4516 + case PRES_NUMBER_NOT_AVAILABLE:
4517 + ASN1_ADD_SIMPLE(comp, 0x82, ie->data, i);
4520 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4522 + /* Fix length of stacked components */
4523 + while(compsp > 0) {
4524 + ASN1_FIXUP(compstk, compsp, ie->data, i);
4528 + return (i ? i+2 : 0);
4532 static FUNC_RECV(receive_facility)
4535 @@ -1346,6 +1812,7 @@
4536 pri_message(pri, " ]\n");
4540 static FUNC_DUMP(dump_time_date)
4542 pri_message(pri, "%c Time Date (len=%2d) [ ", prefix, len);
4543 @@ -1366,39 +1833,60 @@
4545 static FUNC_DUMP(dump_keypad_facility)
4548 + char tmp[64] = "";
4550 if (ie->len == 0 || ie->len > sizeof(tmp))
4553 - libpri_copy_string(tmp, (char *) ie->data, sizeof(tmp));
4554 + strncpy(tmp, (char *) ie->data, sizeof(tmp));
4555 pri_message(pri, "%c Keypad Facility (len=%2d) [ %s ]\n", prefix, ie->len, tmp );
4558 static FUNC_RECV(receive_keypad_facility)
4566 if (ie->len > (sizeof(call->digitbuf) - 1))
4567 - mylen = (sizeof(call->digitbuf) - 1);
4568 + mylen = sizeof(call->digitbuf) - 1;
4572 - memcpy(call->digitbuf, ie->data, mylen);
4573 + strncpy(call->digitbuf, (char *) ie->data, mylen);
4575 - call->digitbuf[mylen] = 0;
4576 + /* I must be really neurotic */
4577 + call->digitbuf[sizeof(call->digitbuf)-1] = '\0';
4582 +static FUNC_RECV(receive_time_date)
4587 +static FUNC_SEND(transmit_time_date) {
4589 + struct tm *timedate;
4591 + timedate = localtime(&now);
4592 + ie->data[0] = timedate->tm_year - 100; // 1900+
4593 + ie->data[1] = timedate->tm_mon + 1;
4594 + ie->data[2] = timedate->tm_mday;
4595 + ie->data[3] = timedate->tm_hour;
4596 + ie->data[4] = timedate->tm_min;
4602 static FUNC_DUMP(dump_display)
4605 char *buf = malloc(len + 1);
4610 if ((x < ie->len) && (ie->data[x] & 0x80)) {
4611 @@ -1413,7 +1901,7 @@
4615 -static void dump_ie_data(unsigned char *c, int len)
4616 +static void dump_ie_data(struct pri *pri, unsigned char *c, int len)
4618 char tmp[1024] = "";
4620 @@ -1423,7 +1911,7 @@
4621 ((*c >= 'a') && (*c <= 'z')) ||
4622 ((*c >= '0') && (*c <= '9'))) {
4625 + if (strlen(tmp)) {
4629 @@ -1435,7 +1923,7 @@
4634 + if (strlen(tmp)) {
4638 @@ -1448,14 +1936,14 @@
4642 - pri_message(NULL, tmp);
4643 + pri_message(pri, tmp);
4646 static FUNC_DUMP(dump_facility)
4648 pri_message(pri, "%c Facility (len=%2d, codeset=%d) [ ", prefix, len, Q931_IE_CODESET(full_ie));
4649 - dump_ie_data(ie->data, ie->len);
4650 - pri_message(NULL, " ]\n");
4651 + dump_ie_data(pri, ie->data, ie->len);
4652 + pri_message(pri, " ]\n");
4655 static FUNC_DUMP(dump_network_spec_fac)
4656 @@ -1465,7 +1953,7 @@
4657 pri_message(pri, code2str(ie->data[1], facilities, sizeof(facilities) / sizeof(facilities[0])));
4660 - dump_ie_data(ie->data, ie->len);
4661 + dump_ie_data(pri, ie->data, ie->len);
4662 pri_message(pri, " ]\n");
4665 @@ -1933,7 +2421,7 @@
4666 { 0, Q931_TRANSIT_NET_SELECT, "Transit Network Selection" },
4667 { 1, Q931_RESTART_INDICATOR, "Restart Indicator", dump_restart_indicator, receive_restart_indicator, transmit_restart_indicator },
4668 { 0, Q931_LOW_LAYER_COMPAT, "Low-layer Compatibility" },
4669 - { 0, Q931_HIGH_LAYER_COMPAT, "High-layer Compatibility" },
4670 + { 1, Q931_HIGH_LAYER_COMPAT, "High-layer Compatibility" , dump_high_layer_compat, receive_high_layer_compat, transmit_high_layer_compat },
4671 { 1, Q931_PACKET_SIZE, "Packet Size" },
4672 { 1, Q931_IE_FACILITY, "Facility" , dump_facility, receive_facility, transmit_facility },
4673 { 1, Q931_IE_REDIRECTION_NUMBER, "Redirection Number" },
4674 @@ -1942,11 +2430,11 @@
4675 { 1, Q931_IE_INFO_REQUEST, "Feature Request" },
4676 { 1, Q931_IE_FEATURE_IND, "Feature Indication" },
4677 { 1, Q931_IE_SEGMENTED_MSG, "Segmented Message" },
4678 - { 1, Q931_IE_CALL_IDENTITY, "Call Identity", dump_call_identity },
4679 + { 1, Q931_IE_CALL_IDENTITY, "Call Identity", dump_call_identity, receive_call_identity, transmit_call_identity },
4680 { 1, Q931_IE_ENDPOINT_ID, "Endpoint Identification" },
4681 { 1, Q931_IE_NOTIFY_IND, "Notification Indicator", dump_notify, receive_notify, transmit_notify },
4682 { 1, Q931_DISPLAY, "Display", dump_display, receive_display, transmit_display },
4683 - { 1, Q931_IE_TIME_DATE, "Date/Time", dump_time_date },
4684 + { 1, Q931_IE_TIME_DATE, "Date/Time", dump_time_date, receive_time_date, transmit_time_date },
4685 { 1, Q931_IE_KEYPAD_FACILITY, "Keypad Facility", dump_keypad_facility, receive_keypad_facility },
4686 { 0, Q931_IE_SIGNAL, "Signal", dump_signal },
4687 { 1, Q931_IE_SWITCHHOOK, "Switch-hook" },
4688 @@ -1954,6 +2442,7 @@
4689 { 1, Q931_IE_ESCAPE_FOR_EXT, "Escape for Extension" },
4690 { 1, Q931_IE_CALL_STATUS, "Call Status" },
4691 { 1, Q931_IE_CHANGE_STATUS, "Change Status" },
4692 + { 1, Q931_COLP, "Connect Line ID Presentation", dump_colp},
4693 { 1, Q931_IE_CONNECTED_ADDR, "Connected Number", dump_connected_number },
4694 { 1, Q931_IE_CONNECTED_NUM, "Connected Number", dump_connected_number },
4695 { 1, Q931_IE_ORIGINAL_CALLED_NUMBER, "Original Called Number", dump_redirecting_number, receive_redirecting_number, transmit_redirecting_number },
4696 @@ -2024,7 +2513,7 @@
4698 if ((ie->ie & 0x80) != 0)
4705 @@ -2054,10 +2543,10 @@
4710 + /* if (cr & 0x80) {
4717 pri_error(NULL, "Call Reference Length not supported: %d\n", h->crlen);
4718 @@ -2071,14 +2560,14 @@
4719 int full_ie = Q931_FULL_IE(codeset, ie->ie);
4722 - pri_message(NULL, "%c [", prefix);
4723 - pri_message(NULL, "%02x", ie->ie);
4724 + pri_message(pri, "%c [", prefix);
4725 + pri_message(pri, "%02x", ie->ie);
4726 if (!(ie->ie & 0x80)) {
4727 - pri_message(NULL, " %02x", ielen(ie)-2);
4728 + pri_message(pri, " %02x", ielen(ie)-2);
4729 for (x = 0; x + 2 < ielen(ie); ++x)
4730 - pri_message(NULL, " %02x", ie->data[x]);
4731 + pri_message(pri, " %02x", ie->data[x]);
4733 - pri_message(NULL, "]\n");
4734 + pri_message(pri, "]\n");
4736 /* Special treatment for shifts */
4737 if((full_ie & 0xf0) == Q931_LOCKING_SHIFT)
4738 @@ -2098,14 +2587,46 @@
4739 pri_error(pri, "!! %c Unknown IE %d (len = %d)\n", prefix, base_ie, ielen(ie));
4742 -static q931_call *q931_getcall(struct pri *pri, int cr)
4743 +static q921_call *q921_getcall(struct pri *pri, struct q931_call *c, int tei)
4748 + if (cur->tei == tei) {
4753 + /* No call exists, make a new one */
4754 + if (pri->debug & PRI_DEBUG_Q921_STATE)
4755 + pri_message(pri, "-- Making new q921 call for cref %d tei %d\n", c->cr, tei);
4756 + cur = malloc(sizeof(struct q921_call));
4757 + memset(cur, 0, sizeof(cur));
4760 + cur->channel = -1;
4761 + cur->next = c->phones;
4766 +static q931_call *q931_getcall(struct pri *pri, int cr, int tei)
4768 q931_call *cur, *prev;
4769 cur = *pri->callpool;
4772 - if (cur->cr == cr)
4774 + if ((pri->localtype == BRI_NETWORK_PTMP) && (tei >= 0)) {
4775 + // hmm...ok, we might be the 1st responding to the setup
4776 + // or it is really our call
4777 + if ((cur->cr == cr) && ((cur->tei == tei) || (cur->tei == 127)))
4779 + // or we might not be the 1st responding, then we need to clone
4780 + // the call struct to hangup properly
4782 + if (cur->cr == cr)
4788 @@ -2118,6 +2639,7 @@
4789 /* Call reference */
4793 /* Append to end of list */
4796 @@ -2133,24 +2655,42 @@
4798 cur = *pri->callpool;
4800 - if (pri->cref > 32767)
4802 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
4803 + if (pri->cref > 32767)
4807 + if (pri->cref > 255)
4811 - if (cur->cr == (0x8000 | pri->cref))
4813 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
4814 + if (cur->cr == (0x8000 | pri->cref))
4817 + // BRIs have only 1 bye cref
4818 + if (cur->cr == (0x80 | pri->cref))
4824 - return q931_getcall(pri, pri->cref | 0x8000);
4825 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
4826 + return q931_getcall(pri, pri->cref | 0x8000, 0);
4829 + return q931_getcall(pri, pri->cref | 0x80, 0);
4833 -static void q931_destroy(struct pri *pri, int cr, q931_call *c)
4834 +static void q931_destroy(struct pri *pri, int cr, q931_call *c, int tei)
4836 q931_call *cur, *prev;
4838 cur = *pri->callpool;
4840 - if ((c && (cur == c)) || (!c && (cur->cr == cr))) {
4841 +// if ((c && (cur == c)) || (!c && (cur->cr == cr))) {
4842 + if ((c && (cur == c)) || (!c && ((cur->cr == cr) && ((pri->localtype != BRI_NETWORK_PTMP) || (cur->tei == tei))))) {
4844 prev->next = cur->next;
4846 @@ -2159,6 +2699,8 @@
4847 pri_message(pri, "NEW_HANGUP DEBUG: Destroying the call, ourstate %s, peerstate %s\n",callstate2str(cur->ourcallstate),callstate2str(cur->peercallstate));
4848 if (cur->retranstimer)
4849 pri_schedule_del(pri, cur->retranstimer);
4850 + if (cur->t303timer)
4851 + pri_schedule_del(pri, cur->t303timer);
4852 pri_call_apdu_queue_cleanup(cur);
4855 @@ -2169,16 +2711,16 @@
4856 pri_error(pri, "Can't destroy call %d!\n", cr);
4859 -static void q931_destroycall(struct pri *pri, int cr)
4860 +static void q931_destroycall(struct pri *pri, int cr, int tei)
4862 - return q931_destroy(pri, cr, NULL);
4863 + return q931_destroy(pri, cr, NULL, tei);
4867 void __q931_destroycall(struct pri *pri, q931_call *c)
4870 - q931_destroy(pri,0, c);
4871 + q931_destroy(pri,0, c, c->tei);
4875 @@ -2290,6 +2832,10 @@
4878 int full_ie = Q931_FULL_IE(codeset, ie->ie);
4879 + if (ielen(ie) > Q931_IE_MAX_LEN) {
4880 + pri_error(pri, "!! Invalid IE length %d (len = %d)\n", full_ie, ielen(ie));
4883 if (pri->debug & PRI_DEBUG_Q931_STATE)
4884 pri_message(pri, "-- Processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
4885 for (x=0;x<sizeof(ies) / sizeof(ies[0]);x++) {
4886 @@ -2307,21 +2853,36 @@
4890 -static void init_header(struct pri *pri, q931_call *call, unsigned char *buf, q931_h **hb, q931_mh **mhb, int *len)
4891 +static void init_header(struct pri *pri, q931_call *call, unsigned char *buf, q931_h **hb, q931_mh **mhb, int *len, int briflag)
4893 /* Returns header and message header and modifies length in place */
4894 q931_h *h = (q931_h *)buf;
4895 - q931_mh * mh = (q931_mh *)(h->contents + 2);
4897 h->pd = pri->protodisc;
4898 h->x0 = 0; /* Reserved 0 */
4899 - h->crlen = 2; /* Two bytes of Call Reference. Invert the top bit to make it from our sense */
4900 - if (call->cr || call->forceinvert) {
4902 + if (briflag == 1) {
4903 + mh = (q931_mh *)(h->contents + 1);
4904 + h->crlen = 1; /* One bytes of Call Reference. Invert the top bit to make it from our sense */
4905 + if (call->cr || call->forceinvert) {
4906 + h->crv[0] = (call->cr ^ 0x80);
4908 + /* Unless of course this has no call reference */
4913 + mh = (q931_mh *)(h->contents + 2);
4914 + h->crlen = 2; /* Two bytes of Call Reference. Invert the top bit to make it from our sense */
4915 + if (call->cr || call->forceinvert) {
4916 h->crv[0] = ((call->cr ^ 0x8000) & 0xff00) >> 8;
4917 h->crv[1] = (call->cr & 0xff);
4920 /* Unless of course this has no call reference */
4926 if (pri->subchannel) {
4927 /* On GR-303, top bit is always 0 */
4928 @@ -2330,13 +2891,23 @@
4936 -static int q931_xmit(struct pri *pri, q931_h *h, int len, int cr)
4937 +static int q931_xmit(struct pri *pri, q931_h *h, int len, int cr, int tei)
4939 - q921_transmit_iframe(pri, h, len, cr);
4941 + if (pri->localtype == BRI_NETWORK_PTMP) {
4942 + mh = (q931_mh *)(h->contents + 1);
4943 + if (mh->msg == Q931_SETUP) {
4944 + q921_transmit_uframe(pri, h, len, cr, tei);
4946 + q921_transmit_iframe(pri, h, len, cr, tei);
4948 + } else if (pri->localtype == BRI_CPE_PTMP) {
4949 + q921_transmit_iframe(pri, h, len, cr, pri->tei);
4951 + q921_transmit_iframe(pri, h, len, cr, tei);
4953 /* The transmit operation might dump the q921 header, so logging the q931
4954 message body after the transmit puts the sections of the message in the
4955 right order in the log */
4956 @@ -2362,7 +2933,11 @@
4958 memset(buf, 0, sizeof(buf));
4960 - init_header(pri, c, buf, &h, &mh, &len);
4961 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
4962 + init_header(pri, c, buf, &h, &mh, &len, 0);
4964 + init_header(pri, c, buf, &h, &mh, &len, 1);
4969 @@ -2397,11 +2972,34 @@
4971 /* Invert the logic */
4972 len = sizeof(buf) - len;
4973 - q931_xmit(pri, h, len, 1);
4974 + if (pri->localtype == BRI_CPE_PTMP) {
4975 + q931_xmit(pri, h, len, 1, pri->tei);
4977 + q931_xmit(pri, h, len, 1, c->tei);
4983 +static int facility_ies[] = { Q931_IE_FACILITY, -1 };
4986 +int q931_facility_kpj(struct pri *pri, q931_call *c, int operation, char *arguments)
4988 + switch (operation) {
4989 + case 0x26: c->aoc = 1;
4991 + case 0x24: c->aoc = 1;
4993 + case 0x22: c->aoc = 1;
4998 + return send_message(pri, c, Q931_FACILITY, facility_ies);
5002 static int status_ies[] = { Q931_CAUSE, Q931_CALL_STATE, -1 };
5004 static int q931_status(struct pri *pri, q931_call *c, int cause)
5005 @@ -2439,17 +3037,45 @@
5006 return send_message(pri, c, Q931_INFORMATION, information_ies);
5009 +static int information_display_ies[] = { Q931_DISPLAY, -1 };
5011 +int q931_information_display(struct pri *pri, q931_call *c, char *display)
5015 + if (!display) return -1;
5016 + strncpy(temp, c->callername, sizeof(temp));
5017 + strncpy(c->callername, display, sizeof(c->callername));
5018 + res = send_message(pri, c, Q931_INFORMATION, information_display_ies);
5019 + strncpy(c->callername, temp, sizeof(c->callername));
5023 +int q931_add_display(struct pri *pri, q931_call *c, char *display)
5025 + strncpy(c->display, display, sizeof(c->display));
5029 +/* static int information_special_ies[] = { Q931_IE_SPECIAL, -1 };
5030 +static int q931_information_special(struct pri *pri, q931_call *c)
5032 + return send_message(pri, c, Q931_FACILITY, information_special_ies);
5037 static int restart_ack_ies[] = { Q931_CHANNEL_IDENT, Q931_RESTART_INDICATOR, -1 };
5039 static int restart_ack(struct pri *pri, q931_call *c)
5041 c->ourcallstate = Q931_CALL_STATE_NULL;
5042 c->peercallstate = Q931_CALL_STATE_NULL;
5043 + c->chanflags &= ~FLAG_PREFERRED;
5044 + c->chanflags |= FLAG_EXCLUSIVE;
5045 return send_message(pri, c, Q931_RESTART_ACKNOWLEDGE, restart_ack_ies);
5048 -static int facility_ies[] = { Q931_IE_FACILITY, -1 };
5050 int q931_facility(struct pri*pri, q931_call *c)
5052 return send_message(pri, c, Q931_FACILITY, facility_ies);
5053 @@ -2463,7 +3089,6 @@
5054 if ((info > 0x2) || (info < 0x00))
5059 c->notify = info & 0x7F;
5061 @@ -2506,6 +3131,8 @@
5063 int q931_call_proceeding(struct pri *pri, q931_call *c, int channel, int info)
5065 + // never send two PROCEEDINGs!
5066 + if (c->proc > 0) return 0;
5068 c->ds1no = (channel & 0xff00) >> 8;
5069 c->ds1explicit = (channel & 0x10000) >> 16;
5070 @@ -2532,8 +3159,14 @@
5071 static int alerting_ies[] = { -1 };
5074 +// static int alerting_BRI_ies[] = { -1 };
5076 int q931_alerting(struct pri *pri, q931_call *c, int channel, int info)
5078 + // never send two ALERTINGs!
5079 + if (c->alert > 0) return 0;
5080 + // XXX novo vox hack
5081 +// if ((!c->proc) && (pri->localtype != BRI_CPE_PTMP))
5083 q931_call_proceeding(pri, c, channel, 0);
5085 @@ -2544,11 +3177,127 @@
5086 c->progressmask = 0;
5087 c->ourcallstate = Q931_CALL_STATE_CALL_RECEIVED;
5088 c->peercallstate = Q931_CALL_STATE_CALL_DELIVERED;
5091 return send_message(pri, c, Q931_ALERTING, alerting_ies);
5092 +/* if ((pri->localtype == PRI_NETWORK) || (pri->localtype == PRI_CPE)) {
5093 + return send_message(pri, c, Q931_ALERTING, alerting_ies);
5095 + if ((pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_NETWORK_PTMP)) {
5096 + return send_message(pri, c, Q931_ALERTING, alerting_ies);
5098 + // no PROGRESS_IND for BRI please
5099 + return send_message(pri, c, Q931_ALERTING, alerting_BRI_ies);
5104 +static int hold_acknowledge_ies[] = { -1 };
5106 +int q931_hold_acknowledge(struct pri *pri, q931_call *c)
5108 + return send_message(pri, c, Q931_HOLD_ACKNOWLEDGE, hold_acknowledge_ies);
5111 +static int hold_reject_ies[] = { Q931_CAUSE, -1 };
5113 +int q931_hold_reject(struct pri *pri, q931_call *c)
5116 + c->causecode = CODE_CCITT;
5117 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5118 + return send_message(pri, c, Q931_HOLD_REJECT, hold_reject_ies);
5121 +static int retrieve_acknowledge_ies[] = { Q931_CHANNEL_IDENT, -1 };
5123 +int q931_retrieve_acknowledge(struct pri *pri, q931_call *c, int channel)
5126 + c->channelno = channel;
5127 + c->chanflags &= ~FLAG_PREFERRED;
5128 + c->chanflags |= FLAG_EXCLUSIVE;
5129 + return send_message(pri, c, Q931_RETRIEVE_ACKNOWLEDGE, retrieve_acknowledge_ies);
5132 +static int retrieve_reject_ies[] = { -1 };
5134 +int q931_retrieve_reject(struct pri *pri, q931_call *c)
5136 + return send_message(pri, c, Q931_RETRIEVE_REJECT, retrieve_reject_ies);
5139 +static int suspend_acknowledge_ies[] = { Q931_DISPLAY, -1 };
5141 +int q931_suspend_acknowledge(struct pri *pri, q931_call *c, char *display)
5143 + char tempcallername[256];
5145 + c->ourcallstate = Q931_CALL_STATE_NULL;
5146 + c->peercallstate = Q931_CALL_STATE_NULL;
5147 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5148 + strncpy(c->callername,display,sizeof(c->callername));
5149 + res = send_message(pri, c, Q931_SUSPEND_ACKNOWLEDGE, suspend_acknowledge_ies);
5150 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5151 + __q931_destroycall(pri, c);
5155 +static int suspend_reject_ies[] = { Q931_DISPLAY, Q931_CAUSE, -1 };
5157 +int q931_suspend_reject(struct pri *pri, q931_call *c, char *display)
5159 + char tempcallername[256];
5161 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5162 + strncpy(c->callername,display,sizeof(c->callername));
5164 + c->causecode = CODE_CCITT;
5165 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5166 + res = send_message(pri, c, Q931_SUSPEND_REJECT, suspend_reject_ies);
5167 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5171 +static int resume_reject_ies[] = { Q931_CAUSE, Q931_DISPLAY, -1 };
5173 +int q931_resume_reject(struct pri *pri, q931_call *c, char *display)
5175 + char tempcallername[256];
5178 + c->causecode = CODE_CCITT;
5179 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5180 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5181 + strncpy(c->callername,display,sizeof(c->callername));
5182 + res = send_message(pri, c, Q931_RESUME_REJECT, resume_reject_ies);
5183 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5187 +static int resume_acknowledge_ies[] = { Q931_CHANNEL_IDENT, Q931_DISPLAY, -1 };
5189 +int q931_resume_acknowledge(struct pri *pri, q931_call *c, int channel, char *display)
5191 + char tempcallername[256];
5194 + c->channelno = channel;
5195 + c->chanflags &= ~FLAG_PREFERRED;
5196 + c->chanflags |= FLAG_EXCLUSIVE;
5198 + c->ourcallstate = Q931_CALL_STATE_ACTIVE;
5199 + c->peercallstate = Q931_CALL_STATE_ACTIVE;
5200 + strncpy(tempcallername,c->callername,sizeof(tempcallername));
5201 + strncpy(c->callername,display,sizeof(c->callername));
5202 + res = send_message(pri, c, Q931_RESUME_ACKNOWLEDGE, resume_acknowledge_ies);
5203 + strncpy(c->callername,tempcallername,sizeof(c->callername));
5207 -static int connect_ies[] = { Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, -1 };
5209 +static int connect_ies[] = { Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, Q931_DISPLAY, -1 };
5210 +static int connect_NET_ies[] = { Q931_CHANNEL_IDENT, Q931_PROGRESS_INDICATOR, Q931_IE_TIME_DATE, -1 };
5212 int q931_setup_ack(struct pri *pri, q931_call *c, int channel, int nonisdn)
5214 @@ -2569,9 +3318,36 @@
5215 c->ourcallstate = Q931_CALL_STATE_OVERLAP_RECEIVING;
5216 c->peercallstate = Q931_CALL_STATE_OVERLAP_SENDING;
5218 + if (pri->localtype == BRI_NETWORK_PTMP) {
5219 + c->progloc = LOC_PRIV_NET_LOCAL_USER;
5220 + c->progcode = CODE_CCITT;
5221 + c->progress = Q931_PROG_INBAND_AVAILABLE;
5223 return send_message(pri, c, Q931_SETUP_ACKNOWLEDGE, connect_ies);
5226 +static void pri_setup_response_timeout(void *data)
5228 + struct q931_call *c = data;
5229 + struct pri *pri = NULL;
5234 + c->cause = PRI_CAUSE_NO_USER_RESPONSE;
5235 + if (pri->debug & PRI_DEBUG_Q931_STATE)
5236 + pri_message(pri, "No response to SETUP message\n");
5238 + pri->ev.e = PRI_EVENT_HANGUP;
5239 + pri->ev.hangup.channel = c->channelno;
5240 + pri->ev.hangup.cref = c->cr;
5241 + pri->ev.hangup.aoc_units = -1;
5242 +// pri->ev.hangup.cause = c->cause;
5243 + pri->ev.hangup.cause = PRI_CAUSE_SWITCH_CONGESTION;
5244 + pri->ev.hangup.call = c;
5245 + q931_hangup(pri, c, c->cause);
5248 static void pri_connect_timeout(void *data)
5250 struct q931_call *c = data;
5251 @@ -2624,6 +3400,7 @@
5253 int q931_connect(struct pri *pri, q931_call *c, int channel, int nonisdn)
5255 + int network = pri->localtype == PRI_NETWORK || pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP;
5257 c->ds1no = (channel & 0xff00) >> 8;
5258 c->ds1explicit = (channel & 0x10000) >> 16;
5259 @@ -2638,16 +3415,29 @@
5260 c->progressmask = PRI_PROG_CALLED_NOT_ISDN;
5262 c->progressmask = 0;
5263 - c->ourcallstate = Q931_CALL_STATE_CONNECT_REQUEST;
5265 + /* WE decide when the call is up and active */
5266 + c->ourcallstate = Q931_CALL_STATE_ACTIVE;
5268 + c->ourcallstate = Q931_CALL_STATE_CONNECT_REQUEST;
5270 c->peercallstate = Q931_CALL_STATE_ACTIVE;
5274 if (c->retranstimer)
5275 pri_schedule_del(pri, c->retranstimer);
5276 c->retranstimer = 0;
5277 - if ((pri->localtype == PRI_CPE) && (!pri->subchannel))
5278 + if (!network && (!pri->subchannel)) {
5279 + /* make sure that CPEs get a CONNECT_ACKNOWLEDGE */
5280 c->retranstimer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T313], pri_connect_timeout, c);
5281 - return send_message(pri, c, Q931_CONNECT, connect_ies);
5283 + if ((pri->localtype != PRI_CPE) && (pri->localtype != BRI_CPE) && (pri->localtype != BRI_CPE_PTMP)) {
5284 + // networks may send datetime
5285 + return send_message(pri, c, Q931_CONNECT, connect_NET_ies);
5287 + return send_message(pri, c, Q931_CONNECT, connect_ies);
5291 static int release_ies[] = { Q931_CAUSE, Q931_IE_USER_USER, -1 };
5292 @@ -2681,7 +3471,7 @@
5293 int q931_restart(struct pri *pri, int channel)
5295 struct q931_call *c;
5296 - c = q931_getcall(pri, 0 | 0x8000);
5297 + c = q931_getcall(pri, 0 | 0x8000, 0);
5301 @@ -2718,9 +3508,15 @@
5305 +//static int setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_DISPLAY, Q931_PROGRESS_INDICATOR,
5306 +// Q931_IE_SIGNAL, Q931_CALLING_PARTY_NUMBER, Q931_CALLED_PARTY_NUMBER, Q931_SENDING_COMPLETE, -1 };
5308 static int setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_PROGRESS_INDICATOR, Q931_NETWORK_SPEC_FAC, Q931_DISPLAY, Q931_IE_USER_USER,
5309 Q931_CALLING_PARTY_NUMBER, Q931_CALLED_PARTY_NUMBER, Q931_REDIRECTING_NUMBER, Q931_SENDING_COMPLETE, Q931_IE_ORIGINATING_LINE_INFO, Q931_IE_GENERIC_DIGITS, -1 };
5311 +static int setup_bri_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_PROGRESS_INDICATOR, Q931_NETWORK_SPEC_FAC, Q931_DISPLAY, Q931_IE_USER_USER,
5312 + Q931_CALLING_PARTY_NUMBER, Q931_CALLED_PARTY_NUMBER, Q931_REDIRECTING_NUMBER, Q931_SENDING_COMPLETE, Q931_IE_ORIGINATING_LINE_INFO, Q931_IE_GENERIC_DIGITS, Q931_HIGH_LAYER_COMPAT, -1 };
5314 static int gr303_setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, -1 };
5316 static int cis_setup_ies[] = { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT, Q931_IE_FACILITY, Q931_CALLED_PARTY_NUMBER, -1 };
5317 @@ -2728,7 +3524,12 @@
5318 int q931_setup(struct pri *pri, q931_call *c, struct pri_sr *req)
5323 + if (pri->localtype == BRI_NETWORK_PTMP) {
5329 c->transcapability = req->transmode;
5330 c->transmoderate = TRANS_MODE_64_CIRCUIT;
5331 @@ -2754,12 +3555,12 @@
5332 else if (c->channelno)
5333 c->chanflags = FLAG_PREFERRED;
5335 - libpri_copy_string(c->callernum, req->caller, sizeof(c->callernum));
5336 + strncpy(c->callernum, req->caller, sizeof(c->callernum) - 1);
5337 c->callerplan = req->callerplan;
5338 if (req->callername)
5339 - libpri_copy_string(c->callername, req->callername, sizeof(c->callername));
5340 + strncpy(c->callername, req->callername, sizeof(c->callername) - 1);
5342 - c->callername[0] = '\0';
5343 + strcpy(c->callername, "");
5344 if ((pri->switchtype == PRI_SWITCH_DMS100) ||
5345 (pri->switchtype == PRI_SWITCH_ATT4ESS)) {
5346 /* Doesn't like certain presentation types */
5347 @@ -2768,13 +3569,13 @@
5349 c->callerpres = req->callerpres;
5351 - c->callernum[0] = '\0';
5352 - c->callername[0] = '\0';
5353 + strcpy(c->callernum, "");
5354 + strcpy(c->callername, "");
5355 c->callerplan = PRI_UNKNOWN;
5356 c->callerpres = PRES_NUMBER_NOT_AVAILABLE;
5358 if (req->redirectingnum) {
5359 - libpri_copy_string(c->redirectingnum, req->redirectingnum, sizeof(c->redirectingnum));
5360 + strncpy(c->redirectingnum, req->redirectingnum, sizeof(c->redirectingnum) - 1);
5361 c->redirectingplan = req->redirectingplan;
5362 if ((pri->switchtype == PRI_SWITCH_DMS100) ||
5363 (pri->switchtype == PRI_SWITCH_ATT4ESS)) {
5364 @@ -2785,13 +3586,13 @@
5365 c->redirectingpres = req->redirectingpres;
5366 c->redirectingreason = req->redirectingreason;
5368 - c->redirectingnum[0] = '\0';
5369 + strcpy(c->redirectingnum, "");
5370 c->redirectingplan = PRI_UNKNOWN;
5371 c->redirectingpres = PRES_NUMBER_NOT_AVAILABLE;
5372 c->redirectingreason = PRI_REDIR_UNKNOWN;
5375 - libpri_copy_string(c->callednum, req->called, sizeof(c->callednum));
5376 + strncpy(c->callednum, req->called, sizeof(c->callednum) - 1);
5377 c->calledplan = req->calledplan;
5380 @@ -2812,14 +3613,19 @@
5381 res = send_message(pri, c, Q931_SETUP, gr303_setup_ies);
5382 else if (c->justsignalling)
5383 res = send_message(pri, c, Q931_SETUP, cis_setup_ies);
5384 + else if (pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP)
5385 + res = send_message(pri, c, Q931_SETUP, setup_bri_ies);
5387 res = send_message(pri, c, Q931_SETUP, setup_ies);
5391 /* make sure we call PRI_EVENT_HANGUP_ACK once we send/receive RELEASE_COMPLETE */
5392 c->sendhangupack = 1;
5393 c->ourcallstate = Q931_CALL_STATE_CALL_INITIATED;
5394 c->peercallstate = Q931_CALL_STATE_OVERLAP_SENDING;
5395 + c->t303timer = pri_schedule_event(pri, pri->timers[PRI_TIMER_T303], pri_setup_response_timeout, c);
5396 + c->t303running = 1;
5400 @@ -2835,7 +3641,11 @@
5403 c->causecode = CODE_CCITT;
5404 - c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5405 + if ((pri->localtype == PRI_NETWORK) || (pri->localtype == BRI_NETWORK) || (pri->localtype == BRI_NETWORK_PTMP)) {
5406 + c->causeloc = LOC_PRIV_NET_LOCAL_USER;
5408 + c->causeloc = LOC_USER;
5410 /* release_ies has CAUSE in it */
5411 res = send_message(pri, c, Q931_RELEASE_COMPLETE, release_ies);
5413 @@ -2860,6 +3670,117 @@
5417 +/* here we cleanly hangup the phones that responded to our call but didnt get the call */
5418 +int q921_hangup(struct pri *pri, q931_call *c, int tei)
5420 + q921_call *cur,*prev;
5428 + if (pri->localtype != BRI_NETWORK_PTMP){
5431 +// pri_error(pri, "q921_hangup(%d, %d)\n", c->cr, tei);
5436 +// pri_error(pri, "tei %d\n", tei);
5443 + if (cur->tei != tei) {
5444 + c->cause = PRI_CAUSE_NORMAL_CLEARING;
5445 + c->tei = cur->tei;
5446 + if (pri->debug & PRI_DEBUG_Q921_STATE)
5447 + pri_message(pri, "sending RELEASE for TEI %d\n", cur->tei);
5448 + send_message(pri, c, Q931_RELEASE, release_ies);
5461 + if (c->tei == 127) {
5462 + q931_destroycall(pri, c->cr, c->tei);
5463 + // make sure * frees the channel
5464 +/* pri_error(pri, "returning PRI_EVENT_HANGUP_ACK\n");
5465 + res = Q931_RES_HAVEEVENT;
5466 + pri->ev.hangup.channel = c->channelno;
5467 + pri->ev.e = PRI_EVENT_HANGUP_ACK; */
5472 +/* here we handle release_completes from the phones
5473 + because some (elmeg) phones do not send a disconnect
5474 + message when the phone is busy */
5475 +int q921_handle_hangup(struct pri *pri, q931_call *c, int tei)
5477 + q921_call *cur,*match,*prev=NULL;
5484 + if (pri->localtype != BRI_NETWORK_PTMP){
5491 + if (cur->tei == tei) {
5494 + prev->next = cur->next;
5497 + c->phones = cur->next;
5502 + if (cur) cur = cur->next;
5512 + // if all phones have signalled busy AND the timer is not running anymore!
5513 + if ((left==0) && (c->cause == PRI_CAUSE_USER_BUSY) && (c->t303running == 0)) {
5514 + // pri_error(pri, "q921_handle_hangup(%d, %d, %d)\n", c->cr, tei, c->tei);
5515 + // make sure * frees the channel
5516 + res = Q931_RES_HAVEEVENT;
5517 + pri->ev.hangup.cause = PRI_CAUSE_USER_BUSY;
5518 + pri->ev.hangup.channel = c->channelno | (c->ds1no << 8);
5519 + pri->ev.hangup.cref = c->cr;
5520 + pri->ev.hangup.call = c;
5521 + pri->ev.e = PRI_EVENT_HANGUP;
5528 int q931_hangup(struct pri *pri, q931_call *c, int cause)
5531 @@ -2871,7 +3792,7 @@
5532 /* If mandatory IE was missing, insist upon that cause code */
5533 if (c->cause == PRI_CAUSE_MANDATORY_IE_MISSING)
5535 - if (cause == 34 || cause == 44 || cause == 82 || cause == 1 || cause == 81) {
5536 + if (cause == 34 || cause == 44 || cause == 82 || cause == 1 || cause == 81 || cause == 17) {
5537 /* We'll send RELEASE_COMPLETE with these causes */
5540 @@ -2885,7 +3806,7 @@
5541 case Q931_CALL_STATE_NULL:
5542 if (c->peercallstate == Q931_CALL_STATE_NULL)
5543 /* free the resources if we receive or send REL_COMPL */
5544 - q931_destroycall(pri, c->cr);
5545 + q931_destroycall(pri, c->cr, c->tei);
5546 else if (c->peercallstate == Q931_CALL_STATE_RELEASE_REQUEST)
5547 q931_release_complete(pri,c,cause);
5549 @@ -2911,6 +3832,11 @@
5550 /* received SETUP_ACKNOWLEDGE */
5551 /* send DISCONNECT in general */
5552 if (c->peercallstate != Q931_CALL_STATE_NULL && c->peercallstate != Q931_CALL_STATE_DISCONNECT_REQUEST && c->peercallstate != Q931_CALL_STATE_DISCONNECT_INDICATION && c->peercallstate != Q931_CALL_STATE_RELEASE_REQUEST && c->peercallstate != Q931_CALL_STATE_RESTART_REQUEST && c->peercallstate != Q931_CALL_STATE_RESTART) {
5553 + if (pri->localtype == BRI_NETWORK_PTMP) {
5554 + if (c->tei == 127) {
5559 q931_disconnect(pri,c,cause);
5560 else if (release_compl)
5561 @@ -2926,8 +3852,14 @@
5563 case Q931_CALL_STATE_DISCONNECT_INDICATION:
5564 /* received DISCONNECT */
5565 + if (pri->localtype == BRI_NETWORK_PTMP) {
5566 + if (c->tei == 127) {
5570 if (c->peercallstate == Q931_CALL_STATE_DISCONNECT_REQUEST) {
5572 +// pri_error(pri, "sending release to %d\n", c->tei);
5573 q931_release(pri,c,cause);
5576 @@ -2941,19 +3873,17 @@
5577 pri_error(pri, "q931_hangup shouldn't be called in this state, ourstate %s, peerstate %s\n",callstate2str(c->ourcallstate),callstate2str(c->peercallstate));
5580 - pri_error(pri, "We're not yet handling hanging up when our state is %d, contact support@digium.com, ourstate %s, peerstate %s\n",
5582 - callstate2str(c->ourcallstate),
5583 - callstate2str(c->peercallstate));
5584 + pri_error(pri, "We're not yet handling hanging up when our state is %d, contact support@digium.com, ourstate %s, peerstate %s\n",callstate2str(c->ourcallstate),callstate2str(c->peercallstate));
5587 /* we did handle hangup properly at this point */
5591 -int q931_receive(struct pri *pri, q931_h *h, int len)
5592 +int q931_receive(struct pri *pri, q931_h *h, int len, int tei)
5599 @@ -2965,6 +3895,7 @@
5600 int codeset, cur_codeset;
5602 struct apdu_event *cur = NULL;
5603 + int network = pri->localtype == PRI_NETWORK || pri->localtype == BRI_NETWORK || pri->localtype == BRI_NETWORK_PTMP;
5605 memset(last_ie, 0, sizeof(last_ie));
5606 if (pri->debug & PRI_DEBUG_Q931_DUMP)
5607 @@ -2978,13 +3909,13 @@
5608 KLUDGE this by changing byte 4 from a 0xf (SERVICE)
5609 to a 0x7 (SERVICE ACKNOWLEDGE) */
5610 h->raw[h->crlen + 2] -= 0x8;
5611 - q931_xmit(pri, h, len, 1);
5612 + q931_xmit(pri, h, len, 1, tei);
5614 } else if (h->pd != pri->protodisc) {
5615 pri_error(pri, "Warning: unknown/inappropriate protocol discriminator received (%02x/%d)\n", h->pd, h->pd);
5618 - c = q931_getcall(pri, q931_cr(h));
5619 + c = q931_getcall(pri, q931_cr(h), tei);
5621 pri_error(pri, "Unable to locate call %d\n", q931_cr(h));
5623 @@ -3002,11 +3933,12 @@
5627 - c->callername[0] = '\0';
5628 + strcpy(c->callername, "");
5631 if (pri->debug & PRI_DEBUG_Q931_STATE)
5632 pri_message(pri, "-- Processing Q.931 Call Setup\n");
5637 @@ -3022,33 +3954,49 @@
5641 - c->callernum[0] = '\0';
5642 - c->callednum[0] = '\0';
5643 - c->callername[0] = '\0';
5644 + strcpy(c->callernum, "");
5645 + strcpy(c->callednum, "");
5646 + strcpy(c->callername, "");
5647 c->callerani[0] = '\0';
5648 c->callerplanani = -1;
5649 - c->redirectingplan = -1;
5650 - c->redirectingpres = -1;
5651 - c->redirectingreason = -1;
5652 - c->origcalledplan = -1;
5653 - c->origcalledpres = -1;
5654 - c->origredirectingreason = -1;
5655 - c->redirectingnum[0] = '\0';
5656 - c->origcallednum[0] = '\0';
5657 - c->redirectingname[0] = '\0';
5658 - c->origcalledname[0] = '\0';
5659 - c->useruserprotocoldisc = -1;
5660 - c->useruserinfo[0] = '\0';
5661 + c->redirectingplan = -1;
5662 + c->redirectingpres = -1;
5663 + c->redirectingreason = -1;
5664 + c->origcalledplan = -1;
5665 + c->origcalledpres = -1;
5666 + c->origredirectingreason = -1;
5667 + strcpy(c->redirectingnum, "");
5668 + strcpy(c->origcallednum, "");
5669 + strcpy(c->redirectingname, "");
5670 + strcpy(c->origcalledname, "");
5671 + c->useruserprotocoldisc = -1;
5672 + strcpy(c->useruserinfo, "");
5676 - /* Fall through */
5677 + strcpy(c->digits, "");
5678 + strcpy(c->display, "");
5680 + c->progressmask = 0;
5683 + if (c->t303timer) {
5684 + c->t303running = 0;
5685 + pri_schedule_del(pri, c->t303timer);
5687 + c->useruserinfo[0] = '\0';
5693 - c->useruserinfo[0] = '\0';
5695 + c->useruserinfo[0] = '\0';
5696 case Q931_CALL_PROCEEDING:
5697 + if (c->t303timer) {
5698 + c->t303running = 0;
5699 + pri_schedule_del(pri, c->t303timer);
5703 c->progressmask = 0;
5705 @@ -3063,16 +4011,21 @@
5709 + c->useruserinfo[0] = '\0';
5710 if (c->retranstimer)
5711 pri_schedule_del(pri, c->retranstimer);
5712 c->retranstimer = 0;
5713 - c->useruserinfo[0] = '\0';
5714 + if (c->t303timer) {
5715 + c->t303running = 0;
5716 + pri_schedule_del(pri, c->t303timer);
5720 case Q931_RELEASE_COMPLETE:
5721 if (c->retranstimer)
5722 - pri_schedule_del(pri, c->retranstimer);
5723 + pri_schedule_del(pri, c->retranstimer);
5724 c->retranstimer = 0;
5725 - c->useruserinfo[0] = '\0';
5726 + c->useruserinfo[0] = '\0';
5730 @@ -3089,22 +4042,32 @@
5731 case Q931_STATUS_ENQUIRY:
5733 case Q931_SETUP_ACKNOWLEDGE:
5734 + if (c->t303timer) {
5735 + c->t303running = 0;
5736 + pri_schedule_del(pri, c->t303timer);
5744 + case Q931_RETRIEVE:
5749 + case Q931_SUSPEND:
5751 case Q931_USER_INFORMATION:
5753 case Q931_CONGESTION_CONTROL:
5755 case Q931_HOLD_ACKNOWLEDGE:
5756 case Q931_HOLD_REJECT:
5757 - case Q931_RETRIEVE:
5758 case Q931_RETRIEVE_ACKNOWLEDGE:
5759 case Q931_RETRIEVE_REJECT:
5761 case Q931_RESUME_ACKNOWLEDGE:
5762 case Q931_RESUME_REJECT:
5763 - case Q931_SUSPEND:
5764 case Q931_SUSPEND_ACKNOWLEDGE:
5765 case Q931_SUSPEND_REJECT:
5766 pri_error(pri, "!! Not yet handling pre-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
5767 @@ -3113,7 +4076,7 @@
5768 pri_error(pri, "!! Don't know how to post-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
5769 q931_status(pri,c, PRI_CAUSE_MESSAGE_TYPE_NONEXIST);
5771 - q931_destroycall(pri,c->cr);
5772 + q931_destroycall(pri,c->cr,c->tei);
5775 memset(mandies, 0, sizeof(mandies));
5776 @@ -3193,12 +4156,19 @@
5778 for (x=0;x<MAX_MAND_IES;x++) {
5780 - /* check if there is no channel identification when we're configured as network -> that's not an error */
5781 - if (((pri->localtype != PRI_NETWORK) || (mh->msg != Q931_SETUP) || (mandies[x] != Q931_CHANNEL_IDENT)) &&
5782 - ((mh->msg != Q931_PROGRESS) || (mandies[x] != Q931_PROGRESS_INDICATOR))) {
5783 - pri_error(pri, "XXX Missing handling for mandatory IE %d (cs%d, %s) XXX\n", Q931_IE_IE(mandies[x]), Q931_IE_CODESET(mandies[x]), ie2str(mandies[x]));
5785 + /* check if there is no channel identification when we're configured as network -> that's not an error */
5787 + if (((mh->msg == Q931_SETUP) && (mandies[x] == Q931_CHANNEL_IDENT)) ||
5788 + ((mh->msg == Q931_PROGRESS) && (mandies[x] == Q931_PROGRESS_INDICATOR))) {
5789 + /* according to ets 300 102-1 a progress indicator is mandatory, but so what? ;-) */
5791 + pri_error(pri, "XXX Missing handling for mandatory IE %d (cs%d, %s) XXX\n", Q931_IE_IE(mandies[x]), Q931_IE_CODESET(mandies[x]), ie2str(mandies[x]));
5795 + pri_error(pri, "XXX Missing handling for mandatory IE %d (cs%d, %s) XXX\n", Q931_IE_IE(mandies[x]), Q931_IE_CODESET(mandies[x]), ie2str(mandies[x]));
5801 @@ -3207,7 +4177,7 @@
5804 q931_status(pri, c, PRI_CAUSE_MANDATORY_IE_MISSING);
5805 - q931_destroycall(pri, c->cr);
5806 + q931_destroycall(pri, c->cr, c->tei);
5809 c->ourcallstate = Q931_CALL_STATE_RESTART;
5810 @@ -3225,6 +4195,7 @@
5812 /* Must be new call */
5814 + pri_error(pri, "received SETUP message for call that is not a new call (retransmission). \n");
5817 if (c->progressmask & PRI_PROG_CALLER_NOT_ISDN)
5818 @@ -3242,27 +4213,31 @@
5819 pri->ev.ring.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
5820 pri->ev.ring.callingpres = c->callerpres;
5821 pri->ev.ring.callingplan = c->callerplan;
5822 - pri->ev.ring.callingplanani = c->callerplanani;
5823 pri->ev.ring.callingplanrdnis = c->redirectingplan;
5824 pri->ev.ring.callingplanorigcalled = c->origcalledplan;
5825 pri->ev.ring.ani2 = c->ani2;
5826 - libpri_copy_string(pri->ev.ring.callingani, c->callerani, sizeof(pri->ev.ring.callingani));
5827 - libpri_copy_string(pri->ev.ring.callingnum, c->callernum, sizeof(pri->ev.ring.callingnum));
5828 - libpri_copy_string(pri->ev.ring.callingname, c->callername, sizeof(pri->ev.ring.callingname));
5829 + pri->ev.ring.callingplanani = c->callerplanani;
5830 + strncpy(pri->ev.ring.callingnum, c->callernum, sizeof(pri->ev.ring.callingnum) - 1);
5831 + strncpy(pri->ev.ring.callingname, c->callername, sizeof(pri->ev.ring.callingname) - 1);
5832 pri->ev.ring.calledplan = c->calledplan;
5833 - libpri_copy_string(pri->ev.ring.callingsubaddr, c->callingsubaddr, sizeof(pri->ev.ring.callingsubaddr));
5834 - libpri_copy_string(pri->ev.ring.callednum, c->callednum, sizeof(pri->ev.ring.callednum));
5835 - libpri_copy_string(pri->ev.ring.origcalledname, c->origcalledname, sizeof(pri->ev.ring.origcalledname));
5836 - libpri_copy_string(pri->ev.ring.origcallednum, c->origcallednum, sizeof(pri->ev.ring.origcallednum));
5837 - libpri_copy_string(pri->ev.ring.redirectingnum, c->redirectingnum, sizeof(pri->ev.ring.redirectingnum));
5838 - libpri_copy_string(pri->ev.ring.redirectingname, c->redirectingname, sizeof(pri->ev.ring.redirectingname));
5839 - libpri_copy_string(pri->ev.ring.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
5840 + strncpy(pri->ev.ring.callingsubaddr, c->callingsubaddr, sizeof(pri->ev.ring.callingsubaddr) - 1);
5841 + if (!strlen(c->callednum) && strlen(c->digitbuf)) {
5842 + strncpy(pri->ev.ring.callednum, c->digitbuf, sizeof(pri->ev.ring.callednum) - 1);
5844 + strncpy(pri->ev.ring.callednum, c->callednum, sizeof(pri->ev.ring.callednum) - 1);
5846 + strncpy(pri->ev.ring.origcalledname, c->origcalledname, sizeof(pri->ev.ring.origcalledname) - 1);
5847 + strncpy(pri->ev.ring.origcallednum, c->origcallednum, sizeof(pri->ev.ring.origcallednum) - 1);
5848 + strncpy(pri->ev.ring.redirectingnum, c->redirectingnum, sizeof(pri->ev.ring.redirectingnum) - 1);
5849 + strncpy(pri->ev.ring.redirectingname, c->redirectingname, sizeof(pri->ev.ring.redirectingname) - 1);
5850 + strncpy(pri->ev.ring.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo) - 1);
5851 c->useruserinfo[0] = '\0';
5852 pri->ev.ring.redirectingreason = c->redirectingreason;
5853 pri->ev.ring.origredirectingreason = c->origredirectingreason;
5854 pri->ev.ring.flexible = ! (c->chanflags & FLAG_EXCLUSIVE);
5855 - pri->ev.ring.cref = c->cr;
5856 + pri->ev.ring.tei = c->tei;
5857 pri->ev.ring.call = c;
5858 + pri->ev.ring.cref = c->cr;
5859 pri->ev.ring.layer1 = c->userl1;
5860 pri->ev.ring.complete = c->complete;
5861 pri->ev.ring.ctype = c->transcapability;
5862 @@ -3275,6 +4250,9 @@
5863 q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
5866 + if (pri->localtype == BRI_NETWORK_PTMP) {
5867 + l2c = q921_getcall(pri, c, tei);
5869 c->ourcallstate = Q931_CALL_STATE_CALL_DELIVERED;
5870 c->peercallstate = Q931_CALL_STATE_CALL_RECEIVED;
5871 pri->ev.e = PRI_EVENT_RINGING;
5872 @@ -3295,17 +4273,24 @@
5873 q931_status(pri, c, PRI_CAUSE_WRONG_MESSAGE);
5876 + /* TEI got the call */
5878 c->ourcallstate = Q931_CALL_STATE_ACTIVE;
5879 c->peercallstate = Q931_CALL_STATE_CONNECT_REQUEST;
5880 pri->ev.e = PRI_EVENT_ANSWER;
5881 pri->ev.answer.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
5882 pri->ev.answer.cref = c->cr;
5883 pri->ev.answer.call = c;
5884 + pri->ev.answer.tei = c->tei;
5885 pri->ev.answer.progress = c->progress;
5886 pri->ev.answer.progressmask = c->progressmask;
5887 libpri_copy_string(pri->ev.answer.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
5888 c->useruserinfo[0] = '\0';
5889 q931_connect_acknowledge(pri, c);
5890 + if (pri->localtype == BRI_NETWORK_PTMP) {
5891 + /* Release all other TEIs */
5892 + q921_hangup(pri, c, tei);
5894 if (c->justsignalling) { /* Make sure WE release when we initiatie a signalling only connection */
5895 q931_release(pri, c, PRI_CAUSE_NORMAL_CLEARING);
5897 @@ -3313,23 +4298,43 @@
5898 return Q931_RES_HAVEEVENT;
5901 - q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
5904 - pri->ev.e = PRI_EVENT_FACNAME;
5905 - libpri_copy_string(pri->ev.facname.callingname, c->callername, sizeof(pri->ev.facname.callingname));
5906 - libpri_copy_string(pri->ev.facname.callingnum, c->callernum, sizeof(pri->ev.facname.callingnum));
5907 - pri->ev.facname.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
5908 - pri->ev.facname.cref = c->cr;
5909 - pri->ev.facname.call = c;
5911 - pri_message(pri, "Sending facility event (%s/%s)\n", pri->ev.facname.callingname, pri->ev.facname.callingnum);
5913 + if ((pri->localtype == PRI_CPE) || (pri->localtype == PRI_NETWORK)) {
5914 + q931_release_complete(pri,c,PRI_CAUSE_INVALID_CALL_REFERENCE);
5916 + // BRI uses the dummy cref for sservices like ccnr
5920 + if (c->facility > 0) {
5921 + pri->ev.e = PRI_EVENT_FACILITY;
5922 + pri->ev.facility.channel = c->channelno | (c->ds1no << 8);
5923 + pri->ev.facility.cref = c->cr;
5924 + pri->ev.facility.tei = c->tei;
5925 + pri->ev.facility.call = c;
5926 + switch (c->facility) {
5927 + case 0x06: /* ECT execute */
5928 + pri->ev.facility.operation = 0x06;
5930 + case 0x0D: /* CD */
5931 + pri->ev.facility.operation = 0x0D;
5932 + strncpy(pri->ev.facility.forwardnum, c->redirectingnum, sizeof(pri->ev.facility.forwardnum) - 1);
5935 + pri->ev.facility.operation = c->facility;
5938 + pri->ev.e = PRI_EVENT_FACNAME;
5939 + strncpy(pri->ev.facname.callingname, c->callername, sizeof(pri->ev.facname.callingname) - 1);
5940 + strncpy(pri->ev.facname.callingnum, c->callernum, sizeof(pri->ev.facname.callingname) - 1);
5941 + pri->ev.facname.channel = c->channelno | (c->ds1no << 8);
5942 + pri->ev.facname.cref = c->cr;
5943 + pri->ev.facname.call = c;
5945 return Q931_RES_HAVEEVENT;
5948 q931_status(pri, c, PRI_CAUSE_MANDATORY_IE_MISSING);
5949 - q931_destroycall(pri, c->cr);
5950 + q931_destroycall(pri, c->cr, c->tei);
5953 pri->ev.e = PRI_EVENT_PROGRESS;
5954 @@ -3347,6 +4352,11 @@
5955 q931_status(pri,c,PRI_CAUSE_WRONG_MESSAGE);
5958 + if (pri->localtype == BRI_NETWORK_PTMP) {
5959 + l2c = q921_getcall(pri, c, tei);
5961 + l2c->channel = c->channelno;
5963 pri->ev.proceeding.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
5964 if (mh->msg == Q931_CALL_PROCEEDING) {
5965 pri->ev.e = PRI_EVENT_PROCEEDING;
5966 @@ -3364,16 +4374,21 @@
5969 if (c->ourcallstate != Q931_CALL_STATE_CONNECT_REQUEST) {
5970 + if ((c->ourcallstate == Q931_CALL_STATE_ACTIVE) && (c->con_acked == 0) && (network)) {
5971 + /* sending a CONNECT_ACKNOWLEDGE is optional for CPEs */
5973 q931_status(pri,c,PRI_CAUSE_WRONG_MESSAGE);
5978 c->ourcallstate = Q931_CALL_STATE_ACTIVE;
5979 c->peercallstate = Q931_CALL_STATE_ACTIVE;
5984 q931_status(pri, c, PRI_CAUSE_MANDATORY_IE_MISSING);
5985 - q931_destroycall(pri, c->cr);
5986 + q931_destroycall(pri, c->cr, c->tei);
5990 @@ -3410,31 +4425,69 @@
5995 + if (c->peercallstate != c->sugcallstate) {
5996 + pri_error(pri, "updating callstate, peercallstate %d to %d\n", c->peercallstate, c->sugcallstate);
5997 + c->peercallstate = c->sugcallstate;
5998 + if (c->sugcallstate != Q931_CALL_STATE_ACTIVE) {
5999 + /* pass hangup to upper layer! */
6001 + pri->ev.e = PRI_EVENT_HANGUP;
6002 + res = Q931_RES_HAVEEVENT;
6004 + } else if (c->sendhangupack) {
6005 + res = Q931_RES_HAVEEVENT;
6006 + pri->ev.e = PRI_EVENT_HANGUP_ACK;
6007 + q931_hangup(pri, c, c->cause);
6009 + q931_hangup(pri, c, c->cause);
6017 case Q931_RELEASE_COMPLETE:
6018 - c->ourcallstate = Q931_CALL_STATE_NULL;
6019 - c->peercallstate = Q931_CALL_STATE_NULL;
6020 - pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6021 - pri->ev.hangup.cref = c->cr;
6022 - pri->ev.hangup.cause = c->cause;
6023 - pri->ev.hangup.call = c;
6024 - libpri_copy_string(pri->ev.hangup.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
6025 - c->useruserinfo[0] = '\0';
6026 - /* Free resources */
6028 - pri->ev.e = PRI_EVENT_HANGUP;
6029 - res = Q931_RES_HAVEEVENT;
6031 - } else if (c->sendhangupack) {
6032 - res = Q931_RES_HAVEEVENT;
6033 - pri->ev.e = PRI_EVENT_HANGUP_ACK;
6034 - pri_hangup(pri, c, c->cause);
6040 - q931_hangup(pri,c,c->cause);
6041 + if (pri->localtype != BRI_NETWORK_PTMP) {
6042 + /* only stop the T303 timer if WE are not a BRI PTMP network */
6043 + if (c->t303timer) {
6044 + c->t303running = 0;
6045 + pri_schedule_del(pri, c->t303timer);
6050 + if ((pri->localtype != BRI_NETWORK_PTMP) || (c->tei == tei)) {
6051 + c->ourcallstate = Q931_CALL_STATE_NULL;
6052 + c->peercallstate = Q931_CALL_STATE_NULL;
6053 + pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6054 + pri->ev.hangup.cref = c->cr;
6055 + pri->ev.hangup.cause = c->cause;
6056 + pri->ev.hangup.call = c;
6057 + libpri_copy_string(pri->ev.hangup.useruserinfo, c->useruserinfo, sizeof(pri->ev.ring.useruserinfo));
6058 + c->useruserinfo[0] = '\0';
6059 + /* Free resources */
6061 + pri->ev.e = PRI_EVENT_HANGUP;
6062 + res = Q931_RES_HAVEEVENT;
6064 + } else if (c->sendhangupack) {
6065 + res = Q931_RES_HAVEEVENT;
6066 + pri->ev.e = PRI_EVENT_HANGUP_ACK;
6067 + pri_hangup(pri, c, c->cause);
6073 + q931_hangup(pri,c,c->cause);
6076 + // ignoring relase_complete
6077 + res = q921_handle_hangup(pri,c,tei);
6084 @@ -3450,6 +4503,7 @@
6085 pri->ev.e = PRI_EVENT_HANGUP;
6086 pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6087 pri->ev.hangup.cref = c->cr;
6088 + pri->ev.hangup.tei = c->tei;
6089 pri->ev.hangup.cause = c->cause;
6090 pri->ev.hangup.call = c;
6091 pri->ev.hangup.aoc_units = c->aoc_units;
6092 @@ -3478,8 +4532,14 @@
6093 pri->ev.e = PRI_EVENT_HANGUP_REQ;
6094 pri->ev.hangup.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6095 pri->ev.hangup.cref = c->cr;
6096 + pri->ev.hangup.tei = c->tei;
6097 pri->ev.hangup.cause = c->cause;
6098 pri->ev.hangup.call = c;
6099 + if (c->progressmask & PRI_PROG_INBAND_AVAILABLE) {
6100 + pri->ev.hangup.inband_progress = 1;
6102 + pri->ev.hangup.inband_progress = 0;
6104 pri->ev.hangup.aoc_units = c->aoc_units;
6106 return Q931_RES_HAVEEVENT;
6107 @@ -3505,14 +4565,14 @@
6108 pri->ev.e = PRI_EVENT_KEYPAD_DIGIT;
6109 pri->ev.digit.call = c;
6110 pri->ev.digit.channel = c->channelno | (c->ds1no << 8);
6111 - libpri_copy_string(pri->ev.digit.digits, c->digitbuf, sizeof(pri->ev.digit.digits));
6112 + strncpy(pri->ev.digit.digits, c->digitbuf, sizeof(pri->ev.digit.digits));
6113 return Q931_RES_HAVEEVENT;
6115 pri->ev.e = PRI_EVENT_INFO_RECEIVED;
6116 pri->ev.ring.call = c;
6117 pri->ev.ring.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6118 - libpri_copy_string(pri->ev.ring.callednum, c->callednum, sizeof(pri->ev.ring.callednum));
6119 - libpri_copy_string(pri->ev.ring.callingsubaddr, c->callingsubaddr, sizeof(pri->ev.ring.callingsubaddr));
6120 + strncpy(pri->ev.ring.callednum, c->digits, sizeof(pri->ev.ring.callednum) - 1);
6121 + strncpy(pri->ev.ring.callingsubaddr, c->callingsubaddr, sizeof(pri->ev.ring.callingsubaddr) - 1);
6122 pri->ev.ring.complete = c->complete; /* this covers IE 33 (Sending Complete) */
6123 return Q931_RES_HAVEEVENT;
6124 case Q931_STATUS_ENQUIRY:
6125 @@ -3530,7 +4590,7 @@
6126 c->peercallstate = Q931_CALL_STATE_OVERLAP_RECEIVING;
6127 pri->ev.e = PRI_EVENT_SETUP_ACK;
6128 pri->ev.setup_ack.channel = c->channelno | (c->ds1no << 8) | (c->ds1explicit << 16);
6130 + pri->ev.setup_ack.call = c;
6133 if (!cur->sent && cur->message == Q931_FACILITY) {
6134 @@ -3546,19 +4606,53 @@
6135 pri->ev.notify.channel = c->channelno;
6136 pri->ev.notify.info = c->notify;
6137 return Q931_RES_HAVEEVENT;
6139 + pri->ev.e = PRI_EVENT_HOLD_REQ;
6140 + pri->ev.hold_req.call = c;
6141 + pri->ev.hold_req.cref = c->cr;
6142 + pri->ev.hold_req.tei = c->tei;
6143 + pri->ev.hold_req.channel = c->channelno;
6144 + return Q931_RES_HAVEEVENT;
6146 + case Q931_RETRIEVE:
6147 + pri->ev.e = PRI_EVENT_RETRIEVE_REQ;
6148 + pri->ev.retrieve_req.call = c;
6149 + pri->ev.retrieve_req.cref = c->cr;
6150 + pri->ev.retrieve_req.tei = c->tei;
6151 + pri->ev.retrieve_req.channel = c->channelno;
6152 + return Q931_RES_HAVEEVENT;
6154 + case Q931_SUSPEND:
6155 + pri->ev.e = PRI_EVENT_SUSPEND_REQ;
6156 + pri->ev.suspend_req.call = c;
6157 + pri->ev.suspend_req.cref = c->cr;
6158 + pri->ev.suspend_req.tei = c->tei;
6159 + pri->ev.suspend_req.channel = c->channelno;
6160 + strncpy(pri->ev.suspend_req.callid, c->callid, sizeof(pri->ev.suspend_req.callid) - 1);
6161 + return Q931_RES_HAVEEVENT;
6164 + if (pri->localtype == BRI_NETWORK_PTMP) {
6165 + l2c = q921_getcall(pri, c, tei);
6168 + pri->ev.e = PRI_EVENT_RESUME_REQ;
6169 + pri->ev.resume_req.call = c;
6170 + pri->ev.resume_req.cref = c->cr;
6171 + pri->ev.resume_req.tei = c->tei;
6172 + pri->ev.resume_req.channel = c->channelno;
6173 + strncpy(pri->ev.resume_req.callid, c->callid, sizeof(pri->ev.resume_req.callid) - 1);
6174 + return Q931_RES_HAVEEVENT;
6176 case Q931_USER_INFORMATION:
6178 case Q931_CONGESTION_CONTROL:
6180 case Q931_HOLD_ACKNOWLEDGE:
6181 case Q931_HOLD_REJECT:
6182 - case Q931_RETRIEVE:
6183 case Q931_RETRIEVE_ACKNOWLEDGE:
6184 case Q931_RETRIEVE_REJECT:
6186 case Q931_RESUME_ACKNOWLEDGE:
6187 case Q931_RESUME_REJECT:
6188 - case Q931_SUSPEND:
6189 case Q931_SUSPEND_ACKNOWLEDGE:
6190 case Q931_SUSPEND_REJECT:
6191 pri_error(pri, "!! Not yet handling post-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
6192 @@ -3568,7 +4662,7 @@
6193 pri_error(pri, "!! Don't know how to post-handle message type %s (%d)\n", msg2str(mh->msg), mh->msg);
6194 q931_status(pri,c, PRI_CAUSE_MESSAGE_TYPE_NONEXIST);
6196 - q931_destroycall(pri,c->cr);
6197 + q931_destroycall(pri,c->cr,c->tei);