Review Board 1.7.16


Add ISDN Calling and Called Subaddress support functions to LIBPRI

Review Request #406 - Created Oct. 14, 2009 and submitted

Alec Davis
15604
Reviewers
asterisk-dev
mattf, rmudgett
LibPRI
Further Libpri support for subaddress, previously LIBPRI only supported receiving Calling Subaddress.

The first intentions of this are to add Transmit and Receive capabilites for Calling Subaddress and Called Subaddress.

Calling, Called, Connected and Redirecting Subaddress, now correctly supports User Specific type and the default NSAP.

Redirection and Redirecting Subaddress have been identified, but will come later.

 

Changes between revision 5 and 6

1 2 3 4 5 6 7
1 2 3 4 5 6 7

  1. /team/rmudgett/subaddr/q931.c: Loading...
/team/rmudgett/subaddr/q931.c
Diff Revision 5 Diff Revision 6
1
/*
1
/*
2
 * libpri: An implementation of Primary Rate ISDN
2
 * libpri: An implementation of Primary Rate ISDN
3
 *
3
 *
4
 * Written by Mark Spencer <markster@digium.com>
4
 * Written by Mark Spencer <markster@digium.com>
5
 *
5
 *
6
 * Copyright (C) 2001-2005, Digium, Inc.
6
 * Copyright (C) 2001-2005, Digium, Inc.
7
 * All Rights Reserved.
7
 * All Rights Reserved.
8
 */
8
 */
9

    
   
9

   
10
/*
10
/*
11
 * See http://www.asterisk.org for more information about
11
 * See http://www.asterisk.org for more information about
12
 * the Asterisk project. Please do not directly contact
12
 * the Asterisk project. Please do not directly contact
13
 * any of the maintainers of this project for assistance;
13
 * any of the maintainers of this project for assistance;
14
 * the project provides a web site, mailing lists and IRC
14
 * the project provides a web site, mailing lists and IRC
15
 * channels for your use.
15
 * channels for your use.
16
 *
16
 *
17
 * This program is free software, distributed under the terms of
17
 * This program is free software, distributed under the terms of
18
 * the GNU General Public License Version 2 as published by the
18
 * the GNU General Public License Version 2 as published by the
19
 * Free Software Foundation. See the LICENSE file included with
19
 * Free Software Foundation. See the LICENSE file included with
20
 * this program for more details.
20
 * this program for more details.
21
 *
21
 *
22
 * In addition, when this program is distributed with Asterisk in
22
 * In addition, when this program is distributed with Asterisk in
23
 * any form that would qualify as a 'combined work' or as a
23
 * any form that would qualify as a 'combined work' or as a
24
 * 'derivative work' (but not mere aggregation), you can redistribute
24
 * 'derivative work' (but not mere aggregation), you can redistribute
25
 * and/or modify the combination under the terms of the license
25
 * and/or modify the combination under the terms of the license
26
 * provided with that copy of Asterisk, instead of the license
26
 * provided with that copy of Asterisk, instead of the license
27
 * terms granted here.
27
 * terms granted here.
28
 */
28
 */
29

    
   
29

   
30
#include "compat.h"
30
#include "compat.h"
31
#include "libpri.h"
31
#include "libpri.h"
32
#include "pri_internal.h"
32
#include "pri_internal.h"
33
#include "pri_q921.h"
33
#include "pri_q921.h"
34
#include "pri_q931.h"
34
#include "pri_q931.h"
35
#include "pri_facility.h"
35
#include "pri_facility.h"
36
#include "rose.h"
36
#include "rose.h"
37

    
   
37

   
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <stdlib.h>
39
#include <stdlib.h>
40
#include <string.h>
40
#include <string.h>
41
#include <ctype.h>
41
#include <ctype.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <limits.h>
43
#include <limits.h>
44

    
   
44

   
45
#define MAX_MAND_IES 10
45
#define MAX_MAND_IES 10
46

    
   
46

   
47
struct msgtype {
47
struct msgtype {
48
	int msgnum;
48
	int msgnum;
49
	char *name;
49
	char *name;
50
	int mandies[MAX_MAND_IES];
50
	int mandies[MAX_MAND_IES];
51
};
51
};
52

    
   
52

   
53
static struct msgtype msgs[] = {
53
static struct msgtype msgs[] = {
54
	/* Call establishment messages */
54
	/* Call establishment messages */
55
	{ Q931_ALERTING, "ALERTING" },
55
	{ Q931_ALERTING, "ALERTING" },
56
	{ Q931_CALL_PROCEEDING, "CALL PROCEEDING" },
56
	{ Q931_CALL_PROCEEDING, "CALL PROCEEDING" },
57
	{ Q931_CONNECT, "CONNECT" },
57
	{ Q931_CONNECT, "CONNECT" },
58
	{ Q931_CONNECT_ACKNOWLEDGE, "CONNECT ACKNOWLEDGE" },
58
	{ Q931_CONNECT_ACKNOWLEDGE, "CONNECT ACKNOWLEDGE" },
59
	{ Q931_PROGRESS, "PROGRESS", { Q931_PROGRESS_INDICATOR } },
59
	{ Q931_PROGRESS, "PROGRESS", { Q931_PROGRESS_INDICATOR } },
60
	{ Q931_SETUP, "SETUP", { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT } },
60
	{ Q931_SETUP, "SETUP", { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT } },
61
	{ Q931_SETUP_ACKNOWLEDGE, "SETUP ACKNOWLEDGE" },
61
	{ Q931_SETUP_ACKNOWLEDGE, "SETUP ACKNOWLEDGE" },
62
	
62
	
63
	/* Call disestablishment messages */
63
	/* Call disestablishment messages */
64
	{ Q931_DISCONNECT, "DISCONNECT", { Q931_CAUSE } },
64
	{ Q931_DISCONNECT, "DISCONNECT", { Q931_CAUSE } },
65
	{ Q931_RELEASE, "RELEASE" },
65
	{ Q931_RELEASE, "RELEASE" },
66
	{ Q931_RELEASE_COMPLETE, "RELEASE COMPLETE" },
66
	{ Q931_RELEASE_COMPLETE, "RELEASE COMPLETE" },
67
	{ Q931_RESTART, "RESTART", { Q931_RESTART_INDICATOR } },
67
	{ Q931_RESTART, "RESTART", { Q931_RESTART_INDICATOR } },
68
	{ Q931_RESTART_ACKNOWLEDGE, "RESTART ACKNOWLEDGE", { Q931_RESTART_INDICATOR } },
68
	{ Q931_RESTART_ACKNOWLEDGE, "RESTART ACKNOWLEDGE", { Q931_RESTART_INDICATOR } },
69

    
   
69

   
70
	/* Miscellaneous */
70
	/* Miscellaneous */
71
	{ Q931_STATUS, "STATUS", { Q931_CAUSE, Q931_IE_CALL_STATE } },
71
	{ Q931_STATUS, "STATUS", { Q931_CAUSE, Q931_IE_CALL_STATE } },
72
	{ Q931_STATUS_ENQUIRY, "STATUS ENQUIRY" },
72
	{ Q931_STATUS_ENQUIRY, "STATUS ENQUIRY" },
73
	{ Q931_USER_INFORMATION, "USER_INFORMATION" },
73
	{ Q931_USER_INFORMATION, "USER_INFORMATION" },
74
	{ Q931_SEGMENT, "SEGMENT" },
74
	{ Q931_SEGMENT, "SEGMENT" },
75
	{ Q931_CONGESTION_CONTROL, "CONGESTION CONTROL" },
75
	{ Q931_CONGESTION_CONTROL, "CONGESTION CONTROL" },
76
	{ Q931_INFORMATION, "INFORMATION" },
76
	{ Q931_INFORMATION, "INFORMATION" },
77
	{ Q931_FACILITY, "FACILITY" },
77
	{ Q931_FACILITY, "FACILITY" },
78
	{ Q931_NOTIFY, "NOTIFY", { Q931_IE_NOTIFY_IND } },
78
	{ Q931_NOTIFY, "NOTIFY", { Q931_IE_NOTIFY_IND } },
79

    
   
79

   
80
	/* Call Management */
80
	/* Call Management */
81
	{ Q931_HOLD, "HOLD" },
81
	{ Q931_HOLD, "HOLD" },
82
	{ Q931_HOLD_ACKNOWLEDGE, "HOLD ACKNOWLEDGE" },
82
	{ Q931_HOLD_ACKNOWLEDGE, "HOLD ACKNOWLEDGE" },
83
	{ Q931_HOLD_REJECT, "HOLD REJECT" },
83
	{ Q931_HOLD_REJECT, "HOLD REJECT" },
84
	{ Q931_RETRIEVE, "RETRIEVE" },
84
	{ Q931_RETRIEVE, "RETRIEVE" },
85
	{ Q931_RETRIEVE_ACKNOWLEDGE, "RETRIEVE ACKNOWLEDGE" },
85
	{ Q931_RETRIEVE_ACKNOWLEDGE, "RETRIEVE ACKNOWLEDGE" },
86
	{ Q931_RETRIEVE_REJECT, "RETRIEVE REJECT" },
86
	{ Q931_RETRIEVE_REJECT, "RETRIEVE REJECT" },
87
	{ Q931_RESUME, "RESUME" },
87
	{ Q931_RESUME, "RESUME" },
88
	{ Q931_RESUME_ACKNOWLEDGE, "RESUME ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
88
	{ Q931_RESUME_ACKNOWLEDGE, "RESUME ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
89
	{ Q931_RESUME_REJECT, "RESUME REJECT", { Q931_CAUSE } },
89
	{ Q931_RESUME_REJECT, "RESUME REJECT", { Q931_CAUSE } },
90
	{ Q931_SUSPEND, "SUSPEND" },
90
	{ Q931_SUSPEND, "SUSPEND" },
91
	{ Q931_SUSPEND_ACKNOWLEDGE, "SUSPEND ACKNOWLEDGE" },
91
	{ Q931_SUSPEND_ACKNOWLEDGE, "SUSPEND ACKNOWLEDGE" },
92
	{ Q931_SUSPEND_REJECT, "SUSPEND REJECT" },
92
	{ Q931_SUSPEND_REJECT, "SUSPEND REJECT" },
93
};
93
};
94
static int post_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, int missingmand);
94
static int post_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, int missingmand);
95

    
   
95

   
96
struct msgtype att_maintenance_msgs[] = {
96
struct msgtype att_maintenance_msgs[] = {
97
	{ ATT_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
97
	{ ATT_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
98
	{ ATT_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
98
	{ ATT_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
99
};
99
};
100

    
   
100

   
101
struct msgtype national_maintenance_msgs[] = {
101
struct msgtype national_maintenance_msgs[] = {
102
	{ NATIONAL_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
102
	{ NATIONAL_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
103
	{ NATIONAL_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
103
	{ NATIONAL_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
104
};
104
};
105
static int post_handle_maintenance_message(struct pri *ctrl, int protodisc, struct q931_mh *mh, struct q931_call *c);
105
static int post_handle_maintenance_message(struct pri *ctrl, int protodisc, struct q931_mh *mh, struct q931_call *c);
106

    
   
106

   
107
static struct msgtype causes[] = {
107
static struct msgtype causes[] = {
108
	{ PRI_CAUSE_UNALLOCATED, "Unallocated (unassigned) number" },
108
	{ PRI_CAUSE_UNALLOCATED, "Unallocated (unassigned) number" },
109
	{ PRI_CAUSE_NO_ROUTE_TRANSIT_NET, "No route to specified transmit network" },
109
	{ PRI_CAUSE_NO_ROUTE_TRANSIT_NET, "No route to specified transmit network" },
110
	{ PRI_CAUSE_NO_ROUTE_DESTINATION, "No route to destination" },
110
	{ PRI_CAUSE_NO_ROUTE_DESTINATION, "No route to destination" },
111
	{ PRI_CAUSE_CHANNEL_UNACCEPTABLE, "Channel unacceptable" },
111
	{ PRI_CAUSE_CHANNEL_UNACCEPTABLE, "Channel unacceptable" },
112
	{ PRI_CAUSE_CALL_AWARDED_DELIVERED, "Call awarded and being delivered in an established channel" },
112
	{ PRI_CAUSE_CALL_AWARDED_DELIVERED, "Call awarded and being delivered in an established channel" },
113
	{ PRI_CAUSE_NORMAL_CLEARING, "Normal Clearing" },
113
	{ PRI_CAUSE_NORMAL_CLEARING, "Normal Clearing" },
114
	{ PRI_CAUSE_USER_BUSY, "User busy" },
114
	{ PRI_CAUSE_USER_BUSY, "User busy" },
115
	{ PRI_CAUSE_NO_USER_RESPONSE, "No user responding" },
115
	{ PRI_CAUSE_NO_USER_RESPONSE, "No user responding" },
116
	{ PRI_CAUSE_NO_ANSWER, "User alerting, no answer" },
116
	{ PRI_CAUSE_NO_ANSWER, "User alerting, no answer" },
117
	{ PRI_CAUSE_CALL_REJECTED, "Call Rejected" },
117
	{ PRI_CAUSE_CALL_REJECTED, "Call Rejected" },
118
	{ PRI_CAUSE_NUMBER_CHANGED, "Number changed" },
118
	{ PRI_CAUSE_NUMBER_CHANGED, "Number changed" },
119
	{ PRI_CAUSE_DESTINATION_OUT_OF_ORDER, "Destination out of order" },
119
	{ PRI_CAUSE_DESTINATION_OUT_OF_ORDER, "Destination out of order" },
120
	{ PRI_CAUSE_INVALID_NUMBER_FORMAT, "Invalid number format" },
120
	{ PRI_CAUSE_INVALID_NUMBER_FORMAT, "Invalid number format" },
121
	{ PRI_CAUSE_FACILITY_REJECTED, "Facility rejected" },
121
	{ PRI_CAUSE_FACILITY_REJECTED, "Facility rejected" },
122
	{ PRI_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "Response to STATus ENQuiry" },
122
	{ PRI_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "Response to STATus ENQuiry" },
123
	{ PRI_CAUSE_NORMAL_UNSPECIFIED, "Normal, unspecified" },
123
	{ PRI_CAUSE_NORMAL_UNSPECIFIED, "Normal, unspecified" },
124
	{ PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION, "Circuit/channel congestion" },
124
	{ PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION, "Circuit/channel congestion" },
125
	{ PRI_CAUSE_NETWORK_OUT_OF_ORDER, "Network out of order" },
125
	{ PRI_CAUSE_NETWORK_OUT_OF_ORDER, "Network out of order" },
126
	{ PRI_CAUSE_NORMAL_TEMPORARY_FAILURE, "Temporary failure" },
126
	{ PRI_CAUSE_NORMAL_TEMPORARY_FAILURE, "Temporary failure" },
127
	{ PRI_CAUSE_SWITCH_CONGESTION, "Switching equipment congestion" },
127
	{ PRI_CAUSE_SWITCH_CONGESTION, "Switching equipment congestion" },
128
	{ PRI_CAUSE_ACCESS_INFO_DISCARDED, "Access information discarded" },
128
	{ PRI_CAUSE_ACCESS_INFO_DISCARDED, "Access information discarded" },
129
	{ PRI_CAUSE_REQUESTED_CHAN_UNAVAIL, "Requested channel not available" },
129
	{ PRI_CAUSE_REQUESTED_CHAN_UNAVAIL, "Requested channel not available" },
130
	{ PRI_CAUSE_PRE_EMPTED, "Pre-empted" },
130
	{ PRI_CAUSE_PRE_EMPTED, "Pre-empted" },
131
	{ PRI_CAUSE_FACILITY_NOT_SUBSCRIBED, "Facility not subscribed" },
131
	{ PRI_CAUSE_FACILITY_NOT_SUBSCRIBED, "Facility not subscribed" },
132
	{ PRI_CAUSE_OUTGOING_CALL_BARRED, "Outgoing call barred" },
132
	{ PRI_CAUSE_OUTGOING_CALL_BARRED, "Outgoing call barred" },
133
	{ PRI_CAUSE_INCOMING_CALL_BARRED, "Incoming call barred" },
133
	{ PRI_CAUSE_INCOMING_CALL_BARRED, "Incoming call barred" },
134
	{ PRI_CAUSE_BEARERCAPABILITY_NOTAUTH, "Bearer capability not authorized" },
134
	{ PRI_CAUSE_BEARERCAPABILITY_NOTAUTH, "Bearer capability not authorized" },
135
	{ PRI_CAUSE_BEARERCAPABILITY_NOTAVAIL, "Bearer capability not available" },
135
	{ PRI_CAUSE_BEARERCAPABILITY_NOTAVAIL, "Bearer capability not available" },
136
	{ PRI_CAUSE_BEARERCAPABILITY_NOTIMPL, "Bearer capability not implemented" },
136
	{ PRI_CAUSE_BEARERCAPABILITY_NOTIMPL, "Bearer capability not implemented" },
137
	{ PRI_CAUSE_SERVICEOROPTION_NOTAVAIL, "Service or option not available, unspecified" },
137
	{ PRI_CAUSE_SERVICEOROPTION_NOTAVAIL, "Service or option not available, unspecified" },
138
	{ PRI_CAUSE_CHAN_NOT_IMPLEMENTED, "Channel not implemented" },
138
	{ PRI_CAUSE_CHAN_NOT_IMPLEMENTED, "Channel not implemented" },
139
	{ PRI_CAUSE_FACILITY_NOT_IMPLEMENTED, "Facility not implemented" },
139
	{ PRI_CAUSE_FACILITY_NOT_IMPLEMENTED, "Facility not implemented" },
140
	{ PRI_CAUSE_INVALID_CALL_REFERENCE, "Invalid call reference value" },
140
	{ PRI_CAUSE_INVALID_CALL_REFERENCE, "Invalid call reference value" },
141
	{ PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST, "Identified channel does not exist" },
141
	{ PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST, "Identified channel does not exist" },
142
	{ PRI_CAUSE_INCOMPATIBLE_DESTINATION, "Incompatible destination" },
142
	{ PRI_CAUSE_INCOMPATIBLE_DESTINATION, "Incompatible destination" },
143
	{ PRI_CAUSE_INVALID_MSG_UNSPECIFIED, "Invalid message unspecified" },
143
	{ PRI_CAUSE_INVALID_MSG_UNSPECIFIED, "Invalid message unspecified" },
144
	{ PRI_CAUSE_MANDATORY_IE_MISSING, "Mandatory information element is missing" },
144
	{ PRI_CAUSE_MANDATORY_IE_MISSING, "Mandatory information element is missing" },
145
	{ PRI_CAUSE_MESSAGE_TYPE_NONEXIST, "Message type nonexist." },
145
	{ PRI_CAUSE_MESSAGE_TYPE_NONEXIST, "Message type nonexist." },
146
	{ PRI_CAUSE_WRONG_MESSAGE, "Wrong message" },
146
	{ PRI_CAUSE_WRONG_MESSAGE, "Wrong message" },
147
	{ PRI_CAUSE_IE_NONEXIST, "Info. element nonexist or not implemented" },
147
	{ PRI_CAUSE_IE_NONEXIST, "Info. element nonexist or not implemented" },
148
	{ PRI_CAUSE_INVALID_IE_CONTENTS, "Invalid information element contents" },
148
	{ PRI_CAUSE_INVALID_IE_CONTENTS, "Invalid information element contents" },
149
	{ PRI_CAUSE_WRONG_CALL_STATE, "Message not compatible with call state" },
149
	{ PRI_CAUSE_WRONG_CALL_STATE, "Message not compatible with call state" },
150
	{ PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "Recover on timer expiry" },
150
	{ PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "Recover on timer expiry" },
151
	{ PRI_CAUSE_MANDATORY_IE_LENGTH_ERROR, "Mandatory IE length error" },
151
	{ PRI_CAUSE_MANDATORY_IE_LENGTH_ERROR, "Mandatory IE length error" },
152
	{ PRI_CAUSE_PROTOCOL_ERROR, "Protocol error, unspecified" },
152
	{ PRI_CAUSE_PROTOCOL_ERROR, "Protocol error, unspecified" },
153
	{ PRI_CAUSE_INTERWORKING, "Interworking, unspecified" },
153
	{ PRI_CAUSE_INTERWORKING, "Interworking, unspecified" },
154
};
154
};
155

    
   
155

   
156
static struct msgtype facilities[] = {
156
static struct msgtype facilities[] = {
157
       { PRI_NSF_SID_PREFERRED, "CPN (SID) preferred" },
157
       { PRI_NSF_SID_PREFERRED, "CPN (SID) preferred" },
158
       { PRI_NSF_ANI_PREFERRED, "BN (ANI) preferred" },
158
       { PRI_NSF_ANI_PREFERRED, "BN (ANI) preferred" },
159
       { PRI_NSF_SID_ONLY, "CPN (SID) only" },
159
       { PRI_NSF_SID_ONLY, "CPN (SID) only" },
160
       { PRI_NSF_ANI_ONLY, "BN (ANI) only" },
160
       { PRI_NSF_ANI_ONLY, "BN (ANI) only" },
161
       { PRI_NSF_CALL_ASSOC_TSC, "Call Associated TSC" },
161
       { PRI_NSF_CALL_ASSOC_TSC, "Call Associated TSC" },
162
       { PRI_NSF_NOTIF_CATSC_CLEARING, "Notification of CATSC Clearing or Resource Unavailable" },
162
       { PRI_NSF_NOTIF_CATSC_CLEARING, "Notification of CATSC Clearing or Resource Unavailable" },
163
       { PRI_NSF_OPERATOR, "Operator" },
163
       { PRI_NSF_OPERATOR, "Operator" },
164
       { PRI_NSF_PCCO, "Pre-subscribed Common Carrier Operator (PCCO)" },
164
       { PRI_NSF_PCCO, "Pre-subscribed Common Carrier Operator (PCCO)" },
165
       { PRI_NSF_SDN, "SDN (including GSDN)" },
165
       { PRI_NSF_SDN, "SDN (including GSDN)" },
166
       { PRI_NSF_TOLL_FREE_MEGACOM, "Toll Free MEGACOM" },
166
       { PRI_NSF_TOLL_FREE_MEGACOM, "Toll Free MEGACOM" },
167
       { PRI_NSF_MEGACOM, "MEGACOM" },
167
       { PRI_NSF_MEGACOM, "MEGACOM" },
168
       { PRI_NSF_ACCUNET, "ACCUNET Switched Digital Service" },
168
       { PRI_NSF_ACCUNET, "ACCUNET Switched Digital Service" },
169
       { PRI_NSF_LONG_DISTANCE_SERVICE, "Long Distance Service" },
169
       { PRI_NSF_LONG_DISTANCE_SERVICE, "Long Distance Service" },
170
       { PRI_NSF_INTERNATIONAL_TOLL_FREE, "International Toll Free Service" },
170
       { PRI_NSF_INTERNATIONAL_TOLL_FREE, "International Toll Free Service" },
171
       { PRI_NSF_ATT_MULTIQUEST, "AT&T MultiQuest" },
171
       { PRI_NSF_ATT_MULTIQUEST, "AT&T MultiQuest" },
172
       { PRI_NSF_CALL_REDIRECTION_SERVICE, "Call Redirection Service" }
172
       { PRI_NSF_CALL_REDIRECTION_SERVICE, "Call Redirection Service" }
173
};
173
};
174

    
   
174

   
175
#define FLAG_WHOLE_INTERFACE	0x01
175
#define FLAG_WHOLE_INTERFACE	0x01
176
#define FLAG_PREFERRED			0x02
176
#define FLAG_PREFERRED			0x02
177
#define FLAG_EXCLUSIVE			0x04
177
#define FLAG_EXCLUSIVE			0x04
178

    
   
178

   
179
#define RESET_INDICATOR_CHANNEL	0
179
#define RESET_INDICATOR_CHANNEL	0
180
#define RESET_INDICATOR_DS1		6
180
#define RESET_INDICATOR_DS1		6
181
#define RESET_INDICATOR_PRI		7
181
#define RESET_INDICATOR_PRI		7
182

    
   
182

   
183
#define TRANS_MODE_64_CIRCUIT	0x10
183
#define TRANS_MODE_64_CIRCUIT	0x10
184
#define TRANS_MODE_2x64_CIRCUIT	0x11
184
#define TRANS_MODE_2x64_CIRCUIT	0x11
185
#define TRANS_MODE_384_CIRCUIT	0x13
185
#define TRANS_MODE_384_CIRCUIT	0x13
186
#define TRANS_MODE_1536_CIRCUIT	0x15
186
#define TRANS_MODE_1536_CIRCUIT	0x15
187
#define TRANS_MODE_1920_CIRCUIT	0x17
187
#define TRANS_MODE_1920_CIRCUIT	0x17
188
#define TRANS_MODE_MULTIRATE	0x18
188
#define TRANS_MODE_MULTIRATE	0x18
189
#define TRANS_MODE_PACKET		0x40
189
#define TRANS_MODE_PACKET		0x40
190

    
   
190

   
191
#define RATE_ADAPT_56K			0x0f
191
#define RATE_ADAPT_56K			0x0f
192

    
   
192

   
193
#define LAYER_2_LAPB			0x46
193
#define LAYER_2_LAPB			0x46
194

    
   
194

   
195
#define LAYER_3_X25				0x66
195
#define LAYER_3_X25				0x66
196

    
   
196

   
197
/* The 4ESS uses a different audio field */
197
/* The 4ESS uses a different audio field */
198
#define PRI_TRANS_CAP_AUDIO_4ESS	0x08
198
#define PRI_TRANS_CAP_AUDIO_4ESS	0x08
199

    
   
199

   
200
/* Don't forget to update PRI_PROG_xxx at libpri.h */
200
/* Don't forget to update PRI_PROG_xxx at libpri.h */
201
#define Q931_PROG_CALL_NOT_E2E_ISDN						0x01
201
#define Q931_PROG_CALL_NOT_E2E_ISDN						0x01
202
#define Q931_PROG_CALLED_NOT_ISDN						0x02
202
#define Q931_PROG_CALLED_NOT_ISDN						0x02
203
#define Q931_PROG_CALLER_NOT_ISDN						0x03
203
#define Q931_PROG_CALLER_NOT_ISDN						0x03
204
#define Q931_PROG_CALLER_RETURNED_TO_ISDN					0x04
204
#define Q931_PROG_CALLER_RETURNED_TO_ISDN					0x04
205
#define Q931_PROG_INBAND_AVAILABLE						0x08
205
#define Q931_PROG_INBAND_AVAILABLE						0x08
206
#define Q931_PROG_DELAY_AT_INTERF						0x0a
206
#define Q931_PROG_DELAY_AT_INTERF						0x0a
207
#define Q931_PROG_INTERWORKING_WITH_PUBLIC				0x10
207
#define Q931_PROG_INTERWORKING_WITH_PUBLIC				0x10
208
#define Q931_PROG_INTERWORKING_NO_RELEASE				0x11
208
#define Q931_PROG_INTERWORKING_NO_RELEASE				0x11
209
#define Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER	0x12
209
#define Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER	0x12
210
#define Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER	0x13
210
#define Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER	0x13
211

    
   
211

   
212
#define CODE_CCITT					0x0
212
#define CODE_CCITT					0x0
213
#define CODE_INTERNATIONAL 			0x1
213
#define CODE_INTERNATIONAL 			0x1
214
#define CODE_NATIONAL 				0x2
214
#define CODE_NATIONAL 				0x2
215
#define CODE_NETWORK_SPECIFIC		0x3
215
#define CODE_NETWORK_SPECIFIC		0x3
216

    
   
216

   
217
#define LOC_USER					0x0
217
#define LOC_USER					0x0
218
#define LOC_PRIV_NET_LOCAL_USER		0x1
218
#define LOC_PRIV_NET_LOCAL_USER		0x1
219
#define LOC_PUB_NET_LOCAL_USER		0x2
219
#define LOC_PUB_NET_LOCAL_USER		0x2
220
#define LOC_TRANSIT_NET				0x3
220
#define LOC_TRANSIT_NET				0x3
221
#define LOC_PUB_NET_REMOTE_USER		0x4
221
#define LOC_PUB_NET_REMOTE_USER		0x4
222
#define LOC_PRIV_NET_REMOTE_USER	0x5
222
#define LOC_PRIV_NET_REMOTE_USER	0x5
223
#define LOC_INTERNATIONAL_NETWORK	0x7
223
#define LOC_INTERNATIONAL_NETWORK	0x7
224
#define LOC_NETWORK_BEYOND_INTERWORKING	0xa
224
#define LOC_NETWORK_BEYOND_INTERWORKING	0xa
225

    
   
225

   
226
static char *ie2str(int ie);
226
static char *ie2str(int ie);
227
static char *msg2str(int msg);
227
static char *msg2str(int msg);
228

    
   
228

   
229

    
   
229

   
230
#define FUNC_DUMP(name) void (name)(int full_ie, struct pri *pri, q931_ie *ie, int len, char prefix)
230
#define FUNC_DUMP(name) void (name)(int full_ie, struct pri *pri, q931_ie *ie, int len, char prefix)
231
#define FUNC_RECV(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len)
231
#define FUNC_RECV(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len)
232
#define FUNC_SEND(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
232
#define FUNC_SEND(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
233

    
   
233

   
234
#if 1
234
#if 1
235
/* Update call state with transition trace. */
235
/* Update call state with transition trace. */
236
#define UPDATE_OURCALLSTATE(ctrl,c,newstate) do {\
236
#define UPDATE_OURCALLSTATE(ctrl,c,newstate) do {\
237
	if (ctrl->debug & (PRI_DEBUG_Q931_STATE) && c->ourcallstate != newstate) \
237
	if (ctrl->debug & (PRI_DEBUG_Q931_STATE) && c->ourcallstate != newstate) \
238
		pri_message(ctrl, DBGHEAD "call %d on channel %d enters state %d (%s)\n", DBGINFO, \
238
		pri_message(ctrl, DBGHEAD "call %d on channel %d enters state %d (%s)\n", DBGINFO, \
239
			c->cr, c->channelno, newstate, q931_call_state_str(newstate)); \
239
			c->cr, c->channelno, newstate, q931_call_state_str(newstate)); \
240
		c->ourcallstate = newstate; \
240
		c->ourcallstate = newstate; \
241
	} while (0)
241
	} while (0)
242
#else
242
#else
243
/* Update call state with no trace. */
243
/* Update call state with no trace. */
244
#define UPDATE_OURCALLSTATE(ctrl,c,newstate) c->ourcallstate = newstate
244
#define UPDATE_OURCALLSTATE(ctrl,c,newstate) c->ourcallstate = newstate
245
#endif
245
#endif
246

    
   
246

   
247
struct ie {
247
struct ie {
248
	/* Maximal count of same IEs at the message (0 - any, 1..n - limited) */
248
	/* Maximal count of same IEs at the message (0 - any, 1..n - limited) */
249
	int max_count;
249
	int max_count;
250
	/* IE code */
250
	/* IE code */
251
	int ie;
251
	int ie;
252
	/* IE friendly name */
252
	/* IE friendly name */
253
	char *name;
253
	char *name;
254
	/* Dump an IE for debugging (preceed all lines by prefix) */
254
	/* Dump an IE for debugging (preceed all lines by prefix) */
255
	FUNC_DUMP(*dump);
255
	FUNC_DUMP(*dump);
256
	/* Handle IE  returns 0 on success, -1 on failure */
256
	/* Handle IE  returns 0 on success, -1 on failure */
257
	FUNC_RECV(*receive);
257
	FUNC_RECV(*receive);
258
	/* Add IE to a message, return the # of bytes added or -1 on failure */
258
	/* Add IE to a message, return the # of bytes added or -1 on failure */
259
	FUNC_SEND(*transmit);
259
	FUNC_SEND(*transmit);
260
};
260
};
261

    
   
261

   
262
/*!
262
/*!
263
 * \internal
263
 * \internal
264
 * \brief Encode the channel id information to pass to upper level.
264
 * \brief Encode the channel id information to pass to upper level.
265
 *
265
 *
266
 * \param call Q.931 call leg
266
 * \param call Q.931 call leg
267
 *
267
 *
268
 * \return Encoded channel value.
268
 * \return Encoded channel value.
269
 */
269
 */
270
static int q931_encode_channel(const q931_call *call)
270
static int q931_encode_channel(const q931_call *call)
271
{
271
{
272
	return call->channelno | (call->ds1no << 8) | (call->ds1explicit << 16)
272
	return call->channelno | (call->ds1no << 8) | (call->ds1explicit << 16)
273
		| (call->cis_call << 17);
273
		| (call->cis_call << 17);
274
}
274
}
275

    
   
275

   
276
/*!
276
/*!
277
 * \brief Determine if layer 2 is in PTMP mode.
277
 * \brief Determine if layer 2 is in PTMP mode.
278
 *
278
 *
279
 * \param ctrl D channel controller.
279
 * \param ctrl D channel controller.
280
 *
280
 *
281
 * \retval TRUE if in PTMP mode.
281
 * \retval TRUE if in PTMP mode.
282
 * \retval FALSE otherwise.
282
 * \retval FALSE otherwise.
283
 */
283
 */
284
int q931_is_ptmp(const struct pri *ctrl)
284
int q931_is_ptmp(const struct pri *ctrl)
285
{
285
{
286
	/* Check master control structure */
286
	/* Check master control structure */
287
	for (; ctrl->master; ctrl = ctrl->master) {
287
	for (; ctrl->master; ctrl = ctrl->master) {
288
	}
288
	}
289
	return ctrl->tei == Q921_TEI_GROUP;
289
	return ctrl->tei == Q921_TEI_GROUP;
290
}
290
}
291

    
   
291

   
292
/*!
292
/*!
293
 * \brief Initialize the given struct q931_party_name
293
 * \brief Initialize the given struct q931_party_name
294
 *
294
 *
295
 * \param name Structure to initialize
295
 * \param name Structure to initialize
296
 *
296
 *
297
 * \return Nothing
297
 * \return Nothing
298
 */
298
 */
299
void q931_party_name_init(struct q931_party_name *name)
299
void q931_party_name_init(struct q931_party_name *name)
300
{
300
{
301
	name->valid = 0;
301
	name->valid = 0;
302
	name->presentation = PRI_PRES_UNAVAILABLE;
302
	name->presentation = PRI_PRES_UNAVAILABLE;
303
	name->char_set = PRI_CHAR_SET_ISO8859_1;
303
	name->char_set = PRI_CHAR_SET_ISO8859_1;
304
	name->str[0] = '\0';
304
	name->str[0] = '\0';
305
}
305
}
306

    
   
306

   
307
/*!
307
/*!
308
 * \brief Initialize the given struct q931_party_number
308
 * \brief Initialize the given struct q931_party_number
309
 *
309
 *
310
 * \param number Structure to initialize
310
 * \param number Structure to initialize
311
 *
311
 *
312
 * \return Nothing
312
 * \return Nothing
313
 */
313
 */
314
void q931_party_number_init(struct q931_party_number *number)
314
void q931_party_number_init(struct q931_party_number *number)
315
{
315
{
316
	number->valid = 0;
316
	number->valid = 0;
317
	number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
317
	number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
318
	number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
318
	number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
319
	number->str[0] = '\0';
319
	number->str[0] = '\0';
320
}
320
}
321

    
   
321

   
322
/*!
322
/*!
323
 * \brief Initialize the given struct q931_party_subaddress
323
 * \brief Initialize the given struct q931_party_subaddress
324
 *
324
 *
325
 * \param subaddress Structure to initialize
325
 * \param subaddress Structure to initialize
326
 *
326
 *
327
 * \return Nothing
327
 * \return Nothing
328
 */
328
 */
329
void q931_party_subaddress_init(struct q931_party_subaddress *subaddress)
329
void q931_party_subaddress_init(struct q931_party_subaddress *subaddress)
330
{
330
{
331
	subaddress->valid = 0;
331
	subaddress->valid = 0;
332
	subaddress->type = 0;
332
	subaddress->type = 0;
333
	subaddress->odd_even_indicator = 0;
333
	subaddress->odd_even_indicator = 0;
334
	subaddress->length = 0;
334
	subaddress->length = 0;
335
	subaddress->data[0] = '\0';
335
	subaddress->data[0] = '\0';
336
}
336
}
337

    
   
337

   
338
/*!
338
/*!
339
 * \brief Initialize the given struct q931_party_address
339
 * \brief Initialize the given struct q931_party_address
340
 *
340
 *
341
 * \param address Structure to initialize
341
 * \param address Structure to initialize
342
 *
342
 *
343
 * \return Nothing
343
 * \return Nothing
344
 */
344
 */
345
void q931_party_address_init(struct q931_party_address *address)
345
void q931_party_address_init(struct q931_party_address *address)
346
{
346
{
347
	q931_party_number_init(&address->number);
347
	q931_party_number_init(&address->number);
348
	q931_party_subaddress_init(&address->subaddress);
348
	q931_party_subaddress_init(&address->subaddress);
349
}
349
}
350

    
   
350

   
351
/*!
351
/*!
352
 * \brief Initialize the given struct q931_party_id
352
 * \brief Initialize the given struct q931_party_id
353
 *
353
 *
354
 * \param id Structure to initialize
354
 * \param id Structure to initialize
355
 *
355
 *
356
 * \return Nothing
356
 * \return Nothing
357
 */
357
 */
358
void q931_party_id_init(struct q931_party_id *id)
358
void q931_party_id_init(struct q931_party_id *id)
359
{
359
{
360
	q931_party_name_init(&id->name);
360
	q931_party_name_init(&id->name);
361
	q931_party_number_init(&id->number);
361
	q931_party_number_init(&id->number);
362
	q931_party_subaddress_init(&id->subaddress);
362
	q931_party_subaddress_init(&id->subaddress);
363
}
363
}
364

    
   
364

   
365
/*!
365
/*!
366
 * \brief Initialize the given struct q931_party_redirecting
366
 * \brief Initialize the given struct q931_party_redirecting
367
 *
367
 *
368
 * \param redirecting Structure to initialize
368
 * \param redirecting Structure to initialize
369
 *
369
 *
370
 * \return Nothing
370
 * \return Nothing
371
 */
371
 */
372
void q931_party_redirecting_init(struct q931_party_redirecting *redirecting)
372
void q931_party_redirecting_init(struct q931_party_redirecting *redirecting)
373
{
373
{
374
	q931_party_id_init(&redirecting->from);
374
	q931_party_id_init(&redirecting->from);
375
	q931_party_id_init(&redirecting->to);
375
	q931_party_id_init(&redirecting->to);
376
	q931_party_id_init(&redirecting->orig_called);
376
	q931_party_id_init(&redirecting->orig_called);
377
	redirecting->state = Q931_REDIRECTING_STATE_IDLE;
377
	redirecting->state = Q931_REDIRECTING_STATE_IDLE;
378
	redirecting->count = 0;
378
	redirecting->count = 0;
379
	redirecting->orig_reason = PRI_REDIR_UNKNOWN;
379
	redirecting->orig_reason = PRI_REDIR_UNKNOWN;
380
	redirecting->reason = PRI_REDIR_UNKNOWN;
380
	redirecting->reason = PRI_REDIR_UNKNOWN;
381
}
381
}
382

    
   
382

   
383
/*!
383
/*!
384
 * \brief Compare the left and right party name.
384
 * \brief Compare the left and right party name.
385
 *
385
 *
386
 * \param left Left parameter party name.
386
 * \param left Left parameter party name.
387
 * \param right Right parameter party name.
387
 * \param right Right parameter party name.
388
 *
388
 *
389
 * \retval < 0 when left < right.
389
 * \retval < 0 when left < right.
390
 * \retval == 0 when left == right.
390
 * \retval == 0 when left == right.
391
 * \retval > 0 when left > right.
391
 * \retval > 0 when left > right.
392
 */
392
 */
393
int q931_party_name_cmp(const struct q931_party_name *left, const struct q931_party_name *right)
393
int q931_party_name_cmp(const struct q931_party_name *left, const struct q931_party_name *right)
394
{
394
{
395
	int cmp;
395
	int cmp;
396

    
   
396

   
397
	if (!left->valid) {
397
	if (!left->valid) {
398
		if (!right->valid) {
398
		if (!right->valid) {
399
			return 0;
399
			return 0;
400
		}
400
		}
401
		return -1;
401
		return -1;
402
	}
402
	}
403
	cmp = left->char_set - right->char_set;
403
	cmp = left->char_set - right->char_set;
404
	if (cmp) {
404
	if (cmp) {
405
		return cmp;
405
		return cmp;
406
	}
406
	}
407
	cmp = strcmp(left->str, right->str);
407
	cmp = strcmp(left->str, right->str);
408
	if (cmp) {
408
	if (cmp) {
409
		return cmp;
409
		return cmp;
410
	}
410
	}
411
	cmp = left->presentation - right->presentation;
411
	cmp = left->presentation - right->presentation;
412
	return cmp;
412
	return cmp;
413
}
413
}
414

    
   
414

   
415
/*!
415
/*!
416
 * \brief Compare the left and right party number.
416
 * \brief Compare the left and right party number.
417
 *
417
 *
418
 * \param left Left parameter party number.
418
 * \param left Left parameter party number.
419
 * \param right Right parameter party number.
419
 * \param right Right parameter party number.
420
 *
420
 *
421
 * \retval < 0 when left < right.
421
 * \retval < 0 when left < right.
422
 * \retval == 0 when left == right.
422
 * \retval == 0 when left == right.
423
 * \retval > 0 when left > right.
423
 * \retval > 0 when left > right.
424
 */
424
 */
425
int q931_party_number_cmp(const struct q931_party_number *left, const struct q931_party_number *right)
425
int q931_party_number_cmp(const struct q931_party_number *left, const struct q931_party_number *right)
426
{
426
{
427
	int cmp;
427
	int cmp;
428

    
   
428

   
429
	if (!left->valid) {
429
	if (!left->valid) {
430
		if (!right->valid) {
430
		if (!right->valid) {
431
			return 0;
431
			return 0;
432
		}
432
		}
433
		return -1;
433
		return -1;
434
	}
434
	}
435
	cmp = left->plan - right->plan;
435
	cmp = left->plan - right->plan;
436
	if (cmp) {
436
	if (cmp) {
437
		return cmp;
437
		return cmp;
438
	}
438
	}
439
	cmp = strcmp(left->str, right->str);
439
	cmp = strcmp(left->str, right->str);
440
	if (cmp) {
440
	if (cmp) {
441
		return cmp;
441
		return cmp;
442
	}
442
	}
443
	cmp = left->presentation - right->presentation;
443
	cmp = left->presentation - right->presentation;
444
	return cmp;
444
	return cmp;
445
}
445
}
446

    
   
446

   
447
/*!
447
/*!
448
 * \brief Compare the left and right party subaddress.
448
 * \brief Compare the left and right party subaddress.
449
 *
449
 *
450
 * \param left Left parameter party subaddress.
450
 * \param left Left parameter party subaddress.
451
 * \param right Right parameter party subaddress.
451
 * \param right Right parameter party subaddress.
452
 *
452
 *
453
 * \retval < 0 when left < right.
453
 * \retval < 0 when left < right.
454
 * \retval == 0 when left == right.
454
 * \retval == 0 when left == right.
455
 * \retval > 0 when left > right.
455
 * \retval > 0 when left > right.
456
 */
456
 */
457
int q931_party_subaddress_cmp(const struct q931_party_subaddress *left, const struct q931_party_subaddress *right)
457
int q931_party_subaddress_cmp(const struct q931_party_subaddress *left, const struct q931_party_subaddress *right)
458
{
458
{
459
	int cmp;
459
	int cmp;
460

    
   
460

   
461
	if (!left->valid) {
461
	if (!left->valid) {
462
		if (!right->valid) {
462
		if (!right->valid) {
463
			return 0;
463
			return 0;
464
		}
464
		}
465
		return -1;
465
		return -1;
466
	} else if (!right->valid) {
466
	} else if (!right->valid) {
467
		return 1;
467
		return 1;
468
	}
468
	}
469
	cmp = left->type - right->type;
469
	cmp = left->type - right->type;
470
	if (cmp) {
470
	if (cmp) {
471
		return cmp;
471
		return cmp;
472
	}
472
	}
473
	cmp = memcmp(left->data, right->data, (left->length < right->length) ? left->length : right->length);
473
	cmp = memcmp(left->data, right->data, (left->length < right->length) ? left->length : right->length);
474
	if (cmp) {
474
	if (cmp) {
475
		return cmp;
475
		return cmp;
476
	}
476
	}
477
	cmp = left->length - right->length;
477
	cmp = left->length - right->length;
478
	if (cmp) {
478
	if (cmp) {
479
		return cmp;
479
		return cmp;
480
	}
480
	}
481
	cmp = left->odd_even_indicator - right->odd_even_indicator;
481
	cmp = left->odd_even_indicator - right->odd_even_indicator;
482
	return cmp;
482
	return cmp;
483
}
483
}
484

    
   
484

   
485
/*!
485
/*!
486
 * \brief Compare the left and right party id.
486
 * \brief Compare the left and right party id.
487
 *
487
 *
488
 * \param left Left parameter party id.
488
 * \param left Left parameter party id.
489
 * \param right Right parameter party id.
489
 * \param right Right parameter party id.
490
 *
490
 *
491
 * \retval < 0 when left < right.
491
 * \retval < 0 when left < right.
492
 * \retval == 0 when left == right.
492
 * \retval == 0 when left == right.
493
 * \retval > 0 when left > right.
493
 * \retval > 0 when left > right.
494
 */
494
 */
495
int q931_party_id_cmp(const struct q931_party_id *left, const struct q931_party_id *right)
495
int q931_party_id_cmp(const struct q931_party_id *left, const struct q931_party_id *right)
496
{
496
{
497
	int cmp;
497
	int cmp;
498

    
   
498

   
499
	cmp = q931_party_number_cmp(&left->number, &right->number);
499
	cmp = q931_party_number_cmp(&left->number, &right->number);
500
	if (cmp) {
500
	if (cmp) {
501
		return cmp;
501
		return cmp;
502
	}
502
	}
503
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
503
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
504
	if (cmp) {
504
	if (cmp) {
505
		return cmp;
505
		return cmp;
506
	}
506
	}
507
	cmp = q931_party_name_cmp(&left->name, &right->name);
507
	cmp = q931_party_name_cmp(&left->name, &right->name);
508
	return cmp;
508
	return cmp;
509
}
509
}
510

    
   
510

   
511
/*!
511
/*!
512
 * \brief Copy the Q.931 party name to the PRI party name structure.
512
 * \brief Copy the Q.931 party name to the PRI party name structure.
513
 *
513
 *
514
 * \param pri_name PRI party name structure
514
 * \param pri_name PRI party name structure
515
 * \param q931_name Q.931 party name structure
515
 * \param q931_name Q.931 party name structure
516
 *
516
 *
517
 * \return Nothing
517
 * \return Nothing
518
 */
518
 */
519
void q931_party_name_copy_to_pri(struct pri_party_name *pri_name, const struct q931_party_name *q931_name)
519
void q931_party_name_copy_to_pri(struct pri_party_name *pri_name, const struct q931_party_name *q931_name)
520
{
520
{
521
	if (q931_name->valid) {
521
	if (q931_name->valid) {
522
		pri_name->valid = 1;
522
		pri_name->valid = 1;
523
		pri_name->presentation = q931_name->presentation;
523
		pri_name->presentation = q931_name->presentation;
524
		pri_name->char_set = q931_name->char_set;
524
		pri_name->char_set = q931_name->char_set;
525
		libpri_copy_string(pri_name->str, q931_name->str, sizeof(pri_name->str));
525
		libpri_copy_string(pri_name->str, q931_name->str, sizeof(pri_name->str));
526
	} else {
526
	} else {
527
		pri_name->valid = 0;
527
		pri_name->valid = 0;
528
		pri_name->presentation = PRI_PRES_UNAVAILABLE;
528
		pri_name->presentation = PRI_PRES_UNAVAILABLE;
529
		pri_name->char_set = PRI_CHAR_SET_ISO8859_1;
529
		pri_name->char_set = PRI_CHAR_SET_ISO8859_1;
530
		pri_name->str[0] = 0;
530
		pri_name->str[0] = 0;
531
	}
531
	}
532
}
532
}
533

    
   
533

   
534
/*!
534
/*!
535
 * \brief Copy the Q.931 party number to the PRI party number structure.
535
 * \brief Copy the Q.931 party number to the PRI party number structure.
536
 *
536
 *
537
 * \param pri_number PRI party number structure
537
 * \param pri_number PRI party number structure
538
 * \param q931_number Q.931 party number structure
538
 * \param q931_number Q.931 party number structure
539
 *
539
 *
540
 * \return Nothing
540
 * \return Nothing
541
 */
541
 */
542
void q931_party_number_copy_to_pri(struct pri_party_number *pri_number, const struct q931_party_number *q931_number)
542
void q931_party_number_copy_to_pri(struct pri_party_number *pri_number, const struct q931_party_number *q931_number)
543
{
543
{
544
	if (q931_number->valid) {
544
	if (q931_number->valid) {
545
		pri_number->valid = 1;
545
		pri_number->valid = 1;
546
		pri_number->presentation = q931_number->presentation;
546
		pri_number->presentation = q931_number->presentation;
547
		pri_number->plan = q931_number->plan;
547
		pri_number->plan = q931_number->plan;
548
		libpri_copy_string(pri_number->str, q931_number->str, sizeof(pri_number->str));
548
		libpri_copy_string(pri_number->str, q931_number->str, sizeof(pri_number->str));
549
	} else {
549
	} else {
550
		pri_number->valid = 0;
550
		pri_number->valid = 0;
551
		pri_number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
551
		pri_number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
552
		pri_number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
552
		pri_number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
553
		pri_number->str[0] = 0;
553
		pri_number->str[0] = 0;
554
	}
554
	}
555
}
555
}
556

    
   
556

   
557
/*!
557
/*!
558
 * \brief Copy the Q.931 party subaddress to the PRI party subaddress structure.
558
 * \brief Copy the Q.931 party subaddress to the PRI party subaddress structure.
559
 *
559
 *
560
 * \param pri_subaddress PRI party subaddress structure
560
 * \param pri_subaddress PRI party subaddress structure
561
 * \param q931_subaddress Q.931 party subaddress structure
561
 * \param q931_subaddress Q.931 party subaddress structure
562
 *
562
 *
563
 * \return Nothing
563
 * \return Nothing
564
 */
564
 */
565
void q931_party_subaddress_copy_to_pri(struct pri_party_subaddress *pri_subaddress, const struct q931_party_subaddress *q931_subaddress)
565
void q931_party_subaddress_copy_to_pri(struct pri_party_subaddress *pri_subaddress, const struct q931_party_subaddress *q931_subaddress)
566
{
566
{
567
	int length;
567
	int length;
568
	/* size of the pri_subaddress->data[] is not the same as the size of q931_subaddress->data[] */
568
	/* size of the pri_subaddress->data[] is not the same as the size of q931_subaddress->data[] */
569

    
   
569

   
570
	if (!q931_subaddress->valid) {
570
	if (!q931_subaddress->valid) {
571
		pri_subaddress->valid = 0;
571
		pri_subaddress->valid = 0;
572
		pri_subaddress->type = 0;
572
		pri_subaddress->type = 0;
573
		pri_subaddress->odd_even_indicator = 0;
573
		pri_subaddress->odd_even_indicator = 0;
574
		pri_subaddress->length = 0;
574
		pri_subaddress->length = 0;
575
		pri_subaddress->data[0] = '\0';
575
		pri_subaddress->data[0] = '\0';
576
		return;
576
		return;
577
	}
577
	}
578

    
   
578

   
579
	pri_subaddress->valid = 1;
579
	pri_subaddress->valid = 1;
580
	pri_subaddress->type = q931_subaddress->type;
580
	pri_subaddress->type = q931_subaddress->type;
581
	pri_subaddress->odd_even_indicator = q931_subaddress->odd_even_indicator;
581
	pri_subaddress->odd_even_indicator = q931_subaddress->odd_even_indicator;
582

    
   
582

   
583
	length = q931_subaddress->length;
583
	length = q931_subaddress->length;
584
	pri_subaddress->length = length;
584
	pri_subaddress->length = length;
585
	memcpy(pri_subaddress->data, q931_subaddress->data, length);
585
	memcpy(pri_subaddress->data, q931_subaddress->data, length);
586
	pri_subaddress->data[length] = '\0';
586
	pri_subaddress->data[length] = '\0';
587
}
587
}
588

    
   
588

   
589
/*!
589
/*!
590
 * \brief Copy the Q.931 party id to the PRI party id structure.
590
 * \brief Copy the Q.931 party id to the PRI party id structure.
591
 *
591
 *
592
 * \param pri_id PRI party id structure
592
 * \param pri_id PRI party id structure
593
 * \param q931_id Q.931 party id structure
593
 * \param q931_id Q.931 party id structure
594
 *
594
 *
595
 * \return Nothing
595
 * \return Nothing
596
 */
596
 */
597
void q931_party_id_copy_to_pri(struct pri_party_id *pri_id, const struct q931_party_id *q931_id)
597
void q931_party_id_copy_to_pri(struct pri_party_id *pri_id, const struct q931_party_id *q931_id)
598
{
598
{
599
	q931_party_name_copy_to_pri(&pri_id->name, &q931_id->name);
599
	q931_party_name_copy_to_pri(&pri_id->name, &q931_id->name);
600
	q931_party_number_copy_to_pri(&pri_id->number, &q931_id->number);
600
	q931_party_number_copy_to_pri(&pri_id->number, &q931_id->number);
601
	q931_party_subaddress_copy_to_pri(&pri_id->subaddress, &q931_id->subaddress);
601
	q931_party_subaddress_copy_to_pri(&pri_id->subaddress, &q931_id->subaddress);
602
}
602
}
603

    
   
603

   
604
/*!
604
/*!
605
 * \brief Copy the Q.931 redirecting data to the PRI redirecting structure.
605
 * \brief Copy the Q.931 redirecting data to the PRI redirecting structure.
606
 *
606
 *
607
 * \param pri_redirecting PRI redirecting structure
607
 * \param pri_redirecting PRI redirecting structure
608
 * \param q931_redirecting Q.931 redirecting structure
608
 * \param q931_redirecting Q.931 redirecting structure
609
 *
609
 *
610
 * \return Nothing
610
 * \return Nothing
611
 */
611
 */
612
void q931_party_redirecting_copy_to_pri(struct pri_party_redirecting *pri_redirecting, const struct q931_party_redirecting *q931_redirecting)
612
void q931_party_redirecting_copy_to_pri(struct pri_party_redirecting *pri_redirecting, const struct q931_party_redirecting *q931_redirecting)
613
{
613
{
614
	q931_party_id_copy_to_pri(&pri_redirecting->from, &q931_redirecting->from);
614
	q931_party_id_copy_to_pri(&pri_redirecting->from, &q931_redirecting->from);
615
	q931_party_id_copy_to_pri(&pri_redirecting->to, &q931_redirecting->to);
615
	q931_party_id_copy_to_pri(&pri_redirecting->to, &q931_redirecting->to);
616
	q931_party_id_copy_to_pri(&pri_redirecting->orig_called,
616
	q931_party_id_copy_to_pri(&pri_redirecting->orig_called,
617
		&q931_redirecting->orig_called);
617
		&q931_redirecting->orig_called);
618
	pri_redirecting->count = q931_redirecting->count;
618
	pri_redirecting->count = q931_redirecting->count;
619
	pri_redirecting->orig_reason = q931_redirecting->orig_reason;
619
	pri_redirecting->orig_reason = q931_redirecting->orig_reason;
620
	pri_redirecting->reason = q931_redirecting->reason;
620
	pri_redirecting->reason = q931_redirecting->reason;
621
}
621
}
622

    
   
622

   
623
/*!
623
/*!
624
 * \brief Fixup some values in the q931_party_id that may be objectionable by switches.
624
 * \brief Fixup some values in the q931_party_id that may be objectionable by switches.
625
 *
625
 *
626
 * \param ctrl D channel controller.
626
 * \param ctrl D channel controller.
627
 * \param id Party ID to tweak.
627
 * \param id Party ID to tweak.
628
 *
628
 *
629
 * \return Nothing
629
 * \return Nothing
630
 */
630
 */
631
void q931_party_id_fixup(const struct pri *ctrl, struct q931_party_id *id)
631
void q931_party_id_fixup(const struct pri *ctrl, struct q931_party_id *id)
632
{
632
{
633
	switch (ctrl->switchtype) {
633
	switch (ctrl->switchtype) {
634
	case PRI_SWITCH_DMS100:
634
	case PRI_SWITCH_DMS100:
635
	case PRI_SWITCH_ATT4ESS:
635
	case PRI_SWITCH_ATT4ESS:
636
		/* Doesn't like certain presentation types */
636
		/* Doesn't like certain presentation types */
637
		if (id->number.valid && !(id->number.presentation & 0x7c)) {
637
		if (id->number.valid && !(id->number.presentation & 0x7c)) {
638
			/* i.e., If presentation is allowed it must be a network number */
638
			/* i.e., If presentation is allowed it must be a network number */
639
			id->number.presentation = PRES_ALLOWED_NETWORK_NUMBER;
639
			id->number.presentation = PRES_ALLOWED_NETWORK_NUMBER;
640
		}
640
		}
641
		break;
641
		break;
642
	default:
642
	default:
643
		break;
643
		break;
644
	}
644
	}
645
}
645
}
646

    
   
646

   
647
/*!
647
/*!
648
 * \brief Determine the overall presentation value for the given party.
648
 * \brief Determine the overall presentation value for the given party.
649
 *
649
 *
650
 * \param id Party to determine the overall presentation value.
650
 * \param id Party to determine the overall presentation value.
651
 *
651
 *
652
 * \return Overall presentation value for the given party.
652
 * \return Overall presentation value for the given party.
653
 */
653
 */
654
int q931_party_id_presentation(const struct q931_party_id *id)
654
int q931_party_id_presentation(const struct q931_party_id *id)
655
{
655
{
656
	int number_priority;
656
	int number_priority;
657
	int number_value;
657
	int number_value;
658
	int number_screening;
658
	int number_screening;
659
	int name_priority;
659
	int name_priority;
660
	int name_value;
660
	int name_value;
661

    
   
661

   
662
	/* Determine name presentation priority. */
662
	/* Determine name presentation priority. */
663
	if (!id->name.valid) {
663
	if (!id->name.valid) {
664
		name_value = PRI_PRES_UNAVAILABLE;
664
		name_value = PRI_PRES_UNAVAILABLE;
665
		name_priority = 3;
665
		name_priority = 3;
666
	} else {
666
	} else {
667
		name_value = id->name.presentation & PRI_PRES_RESTRICTION;
667
		name_value = id->name.presentation & PRI_PRES_RESTRICTION;
668
		switch (name_value) {
668
		switch (name_value) {
669
		case PRI_PRES_RESTRICTED:
669
		case PRI_PRES_RESTRICTED:
670
			name_priority = 0;
670
			name_priority = 0;
671
			break;
671
			break;
672
		case PRI_PRES_ALLOWED:
672
		case PRI_PRES_ALLOWED:
673
			name_priority = 1;
673
			name_priority = 1;
674
			break;
674
			break;
675
		case PRI_PRES_UNAVAILABLE:
675
		case PRI_PRES_UNAVAILABLE:
676
			name_priority = 2;
676
			name_priority = 2;
677
			break;
677
			break;
678
		default:
678
		default:
679
			name_value = PRI_PRES_UNAVAILABLE;
679
			name_value = PRI_PRES_UNAVAILABLE;
680
			name_priority = 3;
680
			name_priority = 3;
681
			break;
681
			break;
682
		}
682
		}
683
	}
683
	}
684

    
   
684

   
685
	/* Determine number presentation priority. */
685
	/* Determine number presentation priority. */
686
	if (!id->number.valid) {
686
	if (!id->number.valid) {
687
		number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
687
		number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
688
		number_value = PRI_PRES_UNAVAILABLE;
688
		number_value = PRI_PRES_UNAVAILABLE;
689
		number_priority = 3;
689
		number_priority = 3;
690
	} else {
690
	} else {
691
		number_screening = id->number.presentation & PRI_PRES_NUMBER_TYPE;
691
		number_screening = id->number.presentation & PRI_PRES_NUMBER_TYPE;
692
		number_value = id->number.presentation & PRI_PRES_RESTRICTION;
692
		number_value = id->number.presentation & PRI_PRES_RESTRICTION;
693
		switch (number_value) {
693
		switch (number_value) {
694
		case PRI_PRES_RESTRICTED:
694
		case PRI_PRES_RESTRICTED:
695
			number_priority = 0;
695
			number_priority = 0;
696
			break;
696
			break;
697
		case PRI_PRES_ALLOWED:
697
		case PRI_PRES_ALLOWED:
698
			number_priority = 1;
698
			number_priority = 1;
699
			break;
699
			break;
700
		case PRI_PRES_UNAVAILABLE:
700
		case PRI_PRES_UNAVAILABLE:
701
			number_priority = 2;
701
			number_priority = 2;
702
			break;
702
			break;
703
		default:
703
		default:
704
			number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
704
			number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
705
			number_value = PRI_PRES_UNAVAILABLE;
705
			number_value = PRI_PRES_UNAVAILABLE;
706
			number_priority = 3;
706
			number_priority = 3;
707
			break;
707
			break;
708
		}
708
		}
709
	}
709
	}
710

    
   
710

   
711
	/* Select the wining presentation value. */
711
	/* Select the wining presentation value. */
712
	if (name_priority < number_priority) {
712
	if (name_priority < number_priority) {
713
		number_value = name_value;
713
		number_value = name_value;
714
	}
714
	}
715

    
   
715

   
716
	return number_value | number_screening;
716
	return number_value | number_screening;
717
}
717
}
718

    
   
718

   
719
static void q931_clr_subcommands(struct pri *ctrl)
719
static void q931_clr_subcommands(struct pri *ctrl)
720
{
720
{
721
	ctrl->subcmds.counter_subcmd = 0;
721
	ctrl->subcmds.counter_subcmd = 0;
722
}
722
}
723

    
   
723

   
724
struct pri_subcommand *q931_alloc_subcommand(struct pri *ctrl)
724
struct pri_subcommand *q931_alloc_subcommand(struct pri *ctrl)
725
{
725
{
726
	if (ctrl->subcmds.counter_subcmd < PRI_MAX_SUBCOMMANDS) {
726
	if (ctrl->subcmds.counter_subcmd < PRI_MAX_SUBCOMMANDS) {
727
		return &ctrl->subcmds.subcmd[ctrl->subcmds.counter_subcmd++];
727
		return &ctrl->subcmds.subcmd[ctrl->subcmds.counter_subcmd++];
728
	}
728
	}
729

    
   
729

   
730
	return NULL;
730
	return NULL;
731
}
731
}
732

    
   
732

   
733
static char *code2str(int code, struct msgtype *codes, int max)
733
static char *code2str(int code, struct msgtype *codes, int max)
734
{
734
{
735
	int x;
735
	int x;
736
	for (x=0;x<max; x++) 
736
	for (x=0;x<max; x++) 
737
		if (codes[x].msgnum == code)
737
		if (codes[x].msgnum == code)
738
			return codes[x].name;
738
			return codes[x].name;
739
	return "Unknown";
739
	return "Unknown";
740
}
740
}
741

    
   
741

   
742
static char *pritype(int type)
742
static char *pritype(int type)
743
{
743
{
744
	switch (type) {
744
	switch (type) {
745
	case PRI_CPE:
745
	case PRI_CPE:
746
		return "CPE";
746
		return "CPE";
747
		break;
747
		break;
748
	case PRI_NETWORK:
748
	case PRI_NETWORK:
749
		return "NET";
749
		return "NET";
750
		break;
750
		break;
751
	default:
751
	default:
752
		return "UNKNOWN";
752
		return "UNKNOWN";
753
	}
753
	}
754
}
754
}
755

    
   
755

   
756
static char *binary(int b, int len) {
756
static char *binary(int b, int len) {
757
	static char res[33];
757
	static char res[33];
758
	int x;
758
	int x;
759
	memset(res, 0, sizeof(res));
759
	memset(res, 0, sizeof(res));
760
	if (len > 32)
760
	if (len > 32)
761
		len = 32;
761
		len = 32;
762
	for (x=1;x<=len;x++)
762
	for (x=1;x<=len;x++)
763
		res[x-1] = b & (1 << (len - x)) ? '1' : '0';
763
		res[x-1] = b & (1 << (len - x)) ? '1' : '0';
764
	return res;
764
	return res;
765
}
765
}
766

    
   
766

   
767
static int receive_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
767
static int receive_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
768
{	
768
{	
769
	int x;
769
	int x;
770
	int pos = 0;
770
	int pos = 0;
771
	int need_extended_channel_octets;/*!< TRUE if octets 3.2 and 3.3 need to be present. */
771
	int need_extended_channel_octets;/*!< TRUE if octets 3.2 and 3.3 need to be present. */
772

    
   
772

   
773
	if (ie->data[0] & 0x08) {
773
	if (ie->data[0] & 0x08) {
774
		call->chanflags = FLAG_EXCLUSIVE;
774
		call->chanflags = FLAG_EXCLUSIVE;
775
	} else {
775
	} else {
776
		call->chanflags = FLAG_PREFERRED;
776
		call->chanflags = FLAG_PREFERRED;
777
	}
777
	}
778

    
   
778

   
779
	need_extended_channel_octets = 0;
779
	need_extended_channel_octets = 0;
780
	if (ie->data[0] & 0x20) {
780
	if (ie->data[0] & 0x20) {
781
		/* PRI encoded interface type */
781
		/* PRI encoded interface type */
782
		switch (ie->data[0] & 0x03) {
782
		switch (ie->data[0] & 0x03) {
783
		case 0x00:
783
		case 0x00:
784
			/* No channel */
784
			/* No channel */
785
			call->channelno = 0;
785
			call->channelno = 0;
786
			call->chanflags = FLAG_PREFERRED;
786
			call->chanflags = FLAG_PREFERRED;
787
			break;
787
			break;
788
		case 0x01:
788
		case 0x01:
789
			/* As indicated in following octets */
789
			/* As indicated in following octets */
790
			need_extended_channel_octets = 1;
790
			need_extended_channel_octets = 1;
791
			break;
791
			break;
792
		case 0x03:
792
		case 0x03:
793
			/* Any channel */
793
			/* Any channel */
794
			call->chanflags = FLAG_PREFERRED;
794
			call->chanflags = FLAG_PREFERRED;
795
			break;
795
			break;
796
		default:
796
		default:
797
			pri_error(ctrl, "!! Unexpected Channel selection %d\n", ie->data[0] & 0x03);
797
			pri_error(ctrl, "!! Unexpected Channel selection %d\n", ie->data[0] & 0x03);
798
			return -1;
798
			return -1;
799
		}
799
		}
800
	} else {
800
	} else {
801
		/* BRI encoded interface type */
801
		/* BRI encoded interface type */
802
		switch (ie->data[0] & 0x03) {
802
		switch (ie->data[0] & 0x03) {
803
		case 0x00:
803
		case 0x00:
804
			/* No channel */
804
			/* No channel */
805
			call->channelno = 0;
805
			call->channelno = 0;
806
			call->chanflags = FLAG_PREFERRED;
806
			call->chanflags = FLAG_PREFERRED;
807
			break;
807
			break;
808
		case 0x03:
808
		case 0x03:
809
			/* Any channel */
809
			/* Any channel */
810
			call->chanflags = FLAG_PREFERRED;
810
			call->chanflags = FLAG_PREFERRED;
811
			break;
811
			break;
812
		default:
812
		default:
813
			/* Specified B channel (B1 or B2) */
813
			/* Specified B channel (B1 or B2) */
814
			call->channelno = ie->data[0] & 0x03;
814
			call->channelno = ie->data[0] & 0x03;
815
			break;
815
			break;
816
		}
816
		}
817
	}
817
	}
818

    
   
818

   
819
	pos++;
819
	pos++;
820
	if (ie->data[0] & 0x40) {
820
	if (ie->data[0] & 0x40) {
821
		/* DS1 specified -- stop here */
821
		/* DS1 specified -- stop here */
822
		call->ds1no = ie->data[1] & 0x7f;
822
		call->ds1no = ie->data[1] & 0x7f;
823
		call->ds1explicit = 1;
823
		call->ds1explicit = 1;
824
		pos++;
824
		pos++;
825
	} else {
825
	} else {
826
		call->ds1explicit = 0;
826
		call->ds1explicit = 0;
827
	}
827
	}
828

    
   
828

   
829
	if (ie->data[0] & 0x04) {
829
	if (ie->data[0] & 0x04) {
830
		/* D channel call.  Signaling only. */
830
		/* D channel call.  Signaling only. */
831
		call->cis_call = 1;
831
		call->cis_call = 1;
832
		call->chanflags = FLAG_EXCLUSIVE;/* For safety mark this channel as exclusive. */
832
		call->chanflags = FLAG_EXCLUSIVE;/* For safety mark this channel as exclusive. */
833
		call->channelno = 0;
833
		call->channelno = 0;
834
		return 0;
834
		return 0;
835
	}
835
	}
836

    
   
836

   
837
	if (need_extended_channel_octets && pos + 2 < len) {
837
	if (need_extended_channel_octets && pos + 2 < len) {
838
		/* More coming */
838
		/* More coming */
839
		if ((ie->data[pos] & 0x0f) != 3) {
839
		if ((ie->data[pos] & 0x0f) != 3) {
840
			/* Channel type/mapping is not for B channel units. */
840
			/* Channel type/mapping is not for B channel units. */
841
			pri_error(ctrl, "!! Unexpected Channel Type %d\n", ie->data[1] & 0x0f);
841
			pri_error(ctrl, "!! Unexpected Channel Type %d\n", ie->data[1] & 0x0f);
842
			return -1;
842
			return -1;
843
		}
843
		}
844
		if ((ie->data[pos] & 0x60) != 0) {
844
		if ((ie->data[pos] & 0x60) != 0) {
845
			pri_error(ctrl, "!! Invalid CCITT coding %d\n", (ie->data[1] & 0x60) >> 5);
845
			pri_error(ctrl, "!! Invalid CCITT coding %d\n", (ie->data[1] & 0x60) >> 5);
846
			return -1;
846
			return -1;
847
		}
847
		}
848
		if (ie->data[pos] & 0x10) {
848
		if (ie->data[pos] & 0x10) {
849
			/* Expect Slot Map */
849
			/* Expect Slot Map */
850
			call->slotmap = 0;
850
			call->slotmap = 0;
851
			pos++;
851
			pos++;
852
			for (x=0;x<3;x++) {
852
			for (x=0;x<3;x++) {
853
				call->slotmap <<= 8;
853
				call->slotmap <<= 8;
854
				call->slotmap |= ie->data[x + pos];
854
				call->slotmap |= ie->data[x + pos];
855
			}
855
			}
856
		} else {
856
		} else {
857
			pos++;
857
			pos++;
858
			/* Only expect a particular channel */
858
			/* Only expect a particular channel */
859
			call->channelno = ie->data[pos] & 0x7f;
859
			call->channelno = ie->data[pos] & 0x7f;
860
			if (ctrl->chan_mapping_logical && call->channelno > 15)
860
			if (ctrl->chan_mapping_logical && call->channelno > 15)
861
				call->channelno++;
861
				call->channelno++;
862
		}
862
		}
863
	}
863
	}
864
	return 0;
864
	return 0;
865
}
865
}
866

    
   
866

   
867
static int transmit_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
867
static int transmit_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
868
{
868
{
869
	int pos = 0;
869
	int pos = 0;
870

    
   
870

   
871
	/* We are ready to transmit single IE only */
871
	/* We are ready to transmit single IE only */
872
	if (order > 1)
872
	if (order > 1)
873
		return 0;
873
		return 0;
874

    
   
874

   
875
	if (call->cis_call) {
875
	if (call->cis_call) {
876
		/*
876
		/*
877
		 * Read the standards docs to figure this out.
877
		 * Read the standards docs to figure this out.
878
		 * Q.SIG ECMA-165 section 7.3
878
		 * Q.SIG ECMA-165 section 7.3
879
		 * ITU Q.931 section 4.5.13
879
		 * ITU Q.931 section 4.5.13
880
		 */
880
		 */
881
		ie->data[pos++] = ctrl->bri ? 0x8c : 0xac;
881
		ie->data[pos++] = ctrl->bri ? 0x8c : 0xac;
882
		return pos + 2;
882
		return pos + 2;
883
	}
883
	}
884

    
   
884

   
885
	/* Start with standard stuff */
885
	/* Start with standard stuff */
886
	if (ctrl->switchtype == PRI_SWITCH_GR303_TMC)
886
	if (ctrl->switchtype == PRI_SWITCH_GR303_TMC)
887
		ie->data[pos] = 0x69;
887
		ie->data[pos] = 0x69;
888
	else if (ctrl->bri) {
888
	else if (ctrl->bri) {
889
		ie->data[pos] = 0x80;
889
		ie->data[pos] = 0x80;
890
		ie->data[pos] |= (call->channelno & 0x3);
890
		ie->data[pos] |= (call->channelno & 0x3);
891
	} else {
891
	} else {
892
		/* PRI */
892
		/* PRI */
893
		if (call->slotmap != -1 || (call->chanflags & FLAG_WHOLE_INTERFACE)) {
893
		if (call->slotmap != -1 || (call->chanflags & FLAG_WHOLE_INTERFACE)) {
894
			/* Specified channel */
894
			/* Specified channel */
895
			ie->data[pos] = 0xa1;
895
			ie->data[pos] = 0xa1;
896
		} else if (call->channelno < 0 || call->channelno == 0xff) {
896
		} else if (call->channelno < 0 || call->channelno == 0xff) {
897
			/* Any channel */
897
			/* Any channel */
898
			ie->data[pos] = 0xa3;
898
			ie->data[pos] = 0xa3;
899
		} else if (!call->channelno) {
899
		} else if (!call->channelno) {
900
			/* No channel */
900
			/* No channel */
901
			ie->data[pos] = 0xa0;
901
			ie->data[pos] = 0xa0;
902
		} else {
902
		} else {
903
			/* Specified channel */
903
			/* Specified channel */
904
			ie->data[pos] = 0xa1;
904
			ie->data[pos] = 0xa1;
905
		}
905
		}
906
	}
906
	}
907
	if (call->chanflags & FLAG_EXCLUSIVE) {
907
	if (call->chanflags & FLAG_EXCLUSIVE) {
908
		/* Channel is exclusive */
908
		/* Channel is exclusive */
909
		ie->data[pos] |= 0x08;
909
		ie->data[pos] |= 0x08;
910
	} else if (!call->chanflags) {
910
	} else if (!call->chanflags) {
911
		/* Don't need this IE */
911
		/* Don't need this IE */
912
		return 0;
912
		return 0;
913
	}
913
	}
914

    
   
914

   
915
	if (((ctrl->switchtype != PRI_SWITCH_QSIG) && (call->ds1no > 0)) || call->ds1explicit) {
915
	if (((ctrl->switchtype != PRI_SWITCH_QSIG) && (call->ds1no > 0)) || call->ds1explicit) {
916
		/* We are specifying the interface.  Octet 3.1 */
916
		/* We are specifying the interface.  Octet 3.1 */
917
		ie->data[pos++] |= 0x40;
917
		ie->data[pos++] |= 0x40;
918
		ie->data[pos++] = 0x80 | call->ds1no;
918
		ie->data[pos++] = 0x80 | call->ds1no;
919
	} else {
919
	} else {
920
		++pos;
920
		++pos;
921
	}
921
	}
922

    
   
922

   
923
	if (!ctrl->bri && (ie->data[0] & 0x03) == 0x01 /* Specified channel */
923
	if (!ctrl->bri && (ie->data[0] & 0x03) == 0x01 /* Specified channel */
924
		&& !(call->chanflags & FLAG_WHOLE_INTERFACE)) {
924
		&& !(call->chanflags & FLAG_WHOLE_INTERFACE)) {
925
		/* The 3.2 and 3.3 octets need to be present */
925
		/* The 3.2 and 3.3 octets need to be present */
926
		ie->data[pos] = 0x83;
926
		ie->data[pos] = 0x83;
927
		if (call->slotmap != -1) {
927
		if (call->slotmap != -1) {
928
			int octet;
928
			int octet;
929

    
   
929

   
930
			/* We have to send a channel map */
930
			/* We have to send a channel map */
931
			ie->data[pos++] |= 0x10;
931
			ie->data[pos++] |= 0x10;
932
			for (octet = 3; octet--;) {
932
			for (octet = 3; octet--;) {
933
				ie->data[pos++] = (call->slotmap >> (8 * octet)) & 0xff;
933
				ie->data[pos++] = (call->slotmap >> (8 * octet)) & 0xff;
934
			}
934
			}
935
		} else {
935
		} else {
936
			/* Channel number specified */
936
			/* Channel number specified */
937
			++pos;
937
			++pos;
938
			if (ctrl->chan_mapping_logical && call->channelno > 16) {
938
			if (ctrl->chan_mapping_logical && call->channelno > 16) {
939
				ie->data[pos++] = 0x80 | (call->channelno - 1);
939
				ie->data[pos++] = 0x80 | (call->channelno - 1);
940
			} else {
940
			} else {
941
				ie->data[pos++] = 0x80 | call->channelno;
941
				ie->data[pos++] = 0x80 | call->channelno;
942
			}
942
			}
943
		}
943
		}
944
	}
944
	}
945

    
   
945

   
946
	return pos + 2;
946
	return pos + 2;
947
}
947
}
948

    
   
948

   
949
static void dump_channel_id(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
949
static void dump_channel_id(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
950
{
950
{
951
	int pos;
951
	int pos;
952
	int x;
952
	int x;
953
	int res;
953
	int res;
954

    
   
954

   
955
	static const char *msg_chan_sel[] = {
955
	static const char *msg_chan_sel[] = {
956
		"No channel selected", "B1 channel", "B2 channel", "Any channel selected",
956
		"No channel selected", "B1 channel", "B2 channel", "Any channel selected",
957
		"No channel selected", "As indicated in following octets", "Reserved", "Any channel selected"
957
		"No channel selected", "As indicated in following octets", "Reserved", "Any channel selected"
958
	};
958
	};
959

    
   
959

   
960
	pri_message(ctrl,
960
	pri_message(ctrl,
961
		"%c Channel ID (len=%2d) [ Ext: %d  IntID: %s  %s  Spare: %d  %s  Dchan: %d\n",
961
		"%c Channel ID (len=%2d) [ Ext: %d  IntID: %s  %s  Spare: %d  %s  Dchan: %d\n",
962
		prefix, len,
962
		prefix, len,
963
		(ie->data[0] & 0x80) ? 1 : 0,
963
		(ie->data[0] & 0x80) ? 1 : 0,
964
		(ie->data[0] & 0x40) ? "Explicit" : "Implicit",
964
		(ie->data[0] & 0x40) ? "Explicit" : "Implicit",
965
		(ie->data[0] & 0x20) ? "Other(PRI)" : "BRI",
965
		(ie->data[0] & 0x20) ? "Other(PRI)" : "BRI",
966
		(ie->data[0] & 0x10) ? 1 : 0,
966
		(ie->data[0] & 0x10) ? 1 : 0,
967
		(ie->data[0] & 0x08) ? "Exclusive" : "Preferred",
967
		(ie->data[0] & 0x08) ? "Exclusive" : "Preferred",
968
		(ie->data[0] & 0x04) ? 1 : 0);
968
		(ie->data[0] & 0x04) ? 1 : 0);
969
	pri_message(ctrl, "%c                        ChanSel: %s\n",
969
	pri_message(ctrl, "%c                        ChanSel: %s\n",
970
		prefix, msg_chan_sel[(ie->data[0] & 0x03) | ((ie->data[0] >> 3) & 0x04)]);
970
		prefix, msg_chan_sel[(ie->data[0] & 0x03) | ((ie->data[0] >> 3) & 0x04)]);
971
	pos = 1;
971
	pos = 1;
972
	len -= 2;
972
	len -= 2;
973
	if (ie->data[0] & 0x40) {
973
	if (ie->data[0] & 0x40) {
974
		/* Explicitly defined DS1 */
974
		/* Explicitly defined DS1 */
975
		do {
975
		do {
976
			pri_message(ctrl, "%c                       Ext: %d  DS1 Identifier: %d  \n",
976
			pri_message(ctrl, "%c                       Ext: %d  DS1 Identifier: %d  \n",
977
				prefix, (ie->data[pos] & 0x80) >> 7, ie->data[pos] & 0x7f);
977
				prefix, (ie->data[pos] & 0x80) >> 7, ie->data[pos] & 0x7f);
978
			++pos;
978
			++pos;
979
		} while (!(ie->data[pos - 1] & 0x80) && pos < len);
979
		} while (!(ie->data[pos - 1] & 0x80) && pos < len);
980
	} else {
980
	} else {
981
		/* Implicitly defined DS1 */
981
		/* Implicitly defined DS1 */
982
	}
982
	}
983
	if (pos < len) {
983
	if (pos < len) {
984
		/* Still more information here */
984
		/* Still more information here */
985
		pri_message(ctrl,
985
		pri_message(ctrl,
986
			"%c                       Ext: %d  Coding: %d  %s Specified  Channel Type: %d\n",
986
			"%c                       Ext: %d  Coding: %d  %s Specified  Channel Type: %d\n",
987
			prefix, (ie->data[pos] & 0x80) >> 7, (ie->data[pos] & 60) >> 5,
987
			prefix, (ie->data[pos] & 0x80) >> 7, (ie->data[pos] & 60) >> 5,
988
			(ie->data[pos] & 0x10) ? "Slot Map" : "Number", ie->data[pos] & 0x0f);
988
			(ie->data[pos] & 0x10) ? "Slot Map" : "Number", ie->data[pos] & 0x0f);
989
		++pos;
989
		++pos;
990
	}
990
	}
991
	if (pos < len) {
991
	if (pos < len) {
992
		if (!(ie->data[pos - 1] & 0x10)) {
992
		if (!(ie->data[pos - 1] & 0x10)) {
993
			/* Number specified */
993
			/* Number specified */
994
			do {
994
			do {
995
				pri_message(ctrl,
995
				pri_message(ctrl,
996
					"%c                       Ext: %d  Channel: %d Type: %s%c\n",
996
					"%c                       Ext: %d  Channel: %d Type: %s%c\n",
997
					prefix, (ie->data[pos] & 0x80) >> 7,
997
					prefix, (ie->data[pos] & 0x80) >> 7,
998
					(ie->data[pos]) & 0x7f, pritype(ctrl->localtype),
998
					(ie->data[pos]) & 0x7f, pritype(ctrl->localtype),
999
					(pos + 1 < len) ? ' ' : ']');
999
					(pos + 1 < len) ? ' ' : ']');
1000
				++pos;
1000
				++pos;
1001
			} while (pos < len);
1001
			} while (pos < len);
1002
		} else {
1002
		} else {
1003
			/* Map specified */
1003
			/* Map specified */
1004
			res = 0;
1004
			res = 0;
1005
			x = 0;
1005
			x = 0;
1006
			do {
1006
			do {
1007
				res <<= 8;
1007
				res <<= 8;
1008
				res |= ie->data[pos++];
1008
				res |= ie->data[pos++];
1009
				++x;
1009
				++x;
1010
			} while (pos < len);
1010
			} while (pos < len);
1011
			pri_message(ctrl, "%c                       Map len: %d  Map: %s ]\n", prefix,
1011
			pri_message(ctrl, "%c                       Map len: %d  Map: %s ]\n", prefix,
1012
				x, binary(res, x << 3));
1012
				x, binary(res, x << 3));
1013
		}
1013
		}
1014
	} else {
1014
	} else {
1015
		pri_message(ctrl, "                         ]\n");
1015
		pri_message(ctrl, "                         ]\n");
1016
	}
1016
	}
1017
}
1017
}
1018

    
   
1018

   
1019
static char *ri2str(int ri)
1019
static char *ri2str(int ri)
1020
{
1020
{
1021
	static struct msgtype ris[] = {
1021
	static struct msgtype ris[] = {
1022
		{ 0, "Indicated Channel" },
1022
		{ 0, "Indicated Channel" },
1023
		{ 6, "Single DS1 Facility" },
1023
		{ 6, "Single DS1 Facility" },
1024
		{ 7, "All DS1 Facilities" },
1024
		{ 7, "All DS1 Facilities" },
1025
	};
1025
	};
1026
	return code2str(ri, ris, sizeof(ris) / sizeof(ris[0]));
1026
	return code2str(ri, ris, sizeof(ris) / sizeof(ris[0]));
1027
}
1027
}
1028

    
   
1028

   
1029
static void dump_restart_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1029
static void dump_restart_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1030
{
1030
{
1031
	pri_message(ctrl, "%c Restart Indentifier (len=%2d) [ Ext: %d  Spare: %d  Resetting %s (%d) ]\n", 
1031
	pri_message(ctrl, "%c Restart Indentifier (len=%2d) [ Ext: %d  Spare: %d  Resetting %s (%d) ]\n", 
1032
		prefix, len, (ie->data[0] & 0x80) >> 7, (ie->data[0] & 0x78) >> 3, ri2str(ie->data[0] & 0x7), ie->data[0] & 0x7);
1032
		prefix, len, (ie->data[0] & 0x80) >> 7, (ie->data[0] & 0x78) >> 3, ri2str(ie->data[0] & 0x7), ie->data[0] & 0x7);
1033
}
1033
}
1034

    
   
1034

   
1035
static int receive_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1035
static int receive_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1036
{
1036
{
1037
	/* Pretty simple */
1037
	/* Pretty simple */
1038
	call->ri = ie->data[0] & 0x7;
1038
	call->ri = ie->data[0] & 0x7;
1039
	return 0;
1039
	return 0;
1040
}
1040
}
1041

    
   
1041

   
1042
static int transmit_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1042
static int transmit_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1043
{
1043
{
1044
	/* Pretty simple */
1044
	/* Pretty simple */
1045
	switch(call->ri) {
1045
	switch(call->ri) {
1046
	case 0:
1046
	case 0:
1047
	case 6:
1047
	case 6:
1048
	case 7:
1048
	case 7:
1049
		ie->data[0] = 0x80 | (call->ri & 0x7);
1049
		ie->data[0] = 0x80 | (call->ri & 0x7);
1050
		break;
1050
		break;
1051
	case 5:
1051
	case 5:
1052
		/* Switch compatibility */
1052
		/* Switch compatibility */
1053
		ie->data[0] = 0xA0 | (call->ri & 0x7);
1053
		ie->data[0] = 0xA0 | (call->ri & 0x7);
1054
		break;
1054
		break;
1055
	default:
1055
	default:
1056
		pri_error(ctrl, "!! Invalid restart indicator value %d\n", call->ri);
1056
		pri_error(ctrl, "!! Invalid restart indicator value %d\n", call->ri);
1057
		return-1;
1057
		return-1;
1058
	}
1058
	}
1059
	return 3;
1059
	return 3;
1060
}
1060
}
1061

    
   
1061

   
1062
static char *redirection_reason2str(int mode)
1062
static char *redirection_reason2str(int mode)
1063
{
1063
{
1064
	static struct msgtype modes[] = {
1064
	static struct msgtype modes[] = {
1065
		{ PRI_REDIR_UNKNOWN, "Unknown" },
1065
		{ PRI_REDIR_UNKNOWN, "Unknown" },
1066
		{ PRI_REDIR_FORWARD_ON_BUSY, "Forwarded on busy" },
1066
		{ PRI_REDIR_FORWARD_ON_BUSY, "Forwarded on busy" },
1067
		{ PRI_REDIR_FORWARD_ON_NO_REPLY, "Forwarded on no reply" },
1067
		{ PRI_REDIR_FORWARD_ON_NO_REPLY, "Forwarded on no reply" },
1068
		{ PRI_REDIR_DEFLECTION, "Call deflected" },
1068
		{ PRI_REDIR_DEFLECTION, "Call deflected" },
1069
		{ PRI_REDIR_DTE_OUT_OF_ORDER, "Called DTE out of order" },
1069
		{ PRI_REDIR_DTE_OUT_OF_ORDER, "Called DTE out of order" },
1070
		{ PRI_REDIR_FORWARDED_BY_DTE, "Forwarded by called DTE" },
1070
		{ PRI_REDIR_FORWARDED_BY_DTE, "Forwarded by called DTE" },
1071
		{ PRI_REDIR_UNCONDITIONAL, "Forwarded unconditionally" },
1071
		{ PRI_REDIR_UNCONDITIONAL, "Forwarded unconditionally" },
1072
	};
1072
	};
1073
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1073
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1074
}
1074
}
1075

    
   
1075

   
1076
static char *cap2str(int mode)
1076
static char *cap2str(int mode)
1077
{
1077
{
1078
	static struct msgtype modes[] = {
1078
	static struct msgtype modes[] = {
1079
		{ PRI_TRANS_CAP_SPEECH, "Speech" },
1079
		{ PRI_TRANS_CAP_SPEECH, "Speech" },
1080
		{ PRI_TRANS_CAP_DIGITAL, "Unrestricted digital information" },
1080
		{ PRI_TRANS_CAP_DIGITAL, "Unrestricted digital information" },
1081
		{ PRI_TRANS_CAP_RESTRICTED_DIGITAL, "Restricted digital information" },
1081
		{ PRI_TRANS_CAP_RESTRICTED_DIGITAL, "Restricted digital information" },
1082
		{ PRI_TRANS_CAP_3_1K_AUDIO, "3.1kHz audio" },
1082
		{ PRI_TRANS_CAP_3_1K_AUDIO, "3.1kHz audio" },
1083
		{ PRI_TRANS_CAP_DIGITAL_W_TONES, "Unrestricted digital information with tones/announcements" },
1083
		{ PRI_TRANS_CAP_DIGITAL_W_TONES, "Unrestricted digital information with tones/announcements" },
1084
		{ PRI_TRANS_CAP_VIDEO, "Video" },
1084
		{ PRI_TRANS_CAP_VIDEO, "Video" },
1085
		{ PRI_TRANS_CAP_AUDIO_4ESS, "3.1khz audio (4ESS)" },
1085
		{ PRI_TRANS_CAP_AUDIO_4ESS, "3.1khz audio (4ESS)" },
1086
	};
1086
	};
1087
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1087
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1088
}
1088
}
1089

    
   
1089

   
1090
static char *mode2str(int mode)
1090
static char *mode2str(int mode)
1091
{
1091
{
1092
	static struct msgtype modes[] = {
1092
	static struct msgtype modes[] = {
1093
		{ TRANS_MODE_64_CIRCUIT, "64kbps, circuit-mode" },
1093
		{ TRANS_MODE_64_CIRCUIT, "64kbps, circuit-mode" },
1094
		{ TRANS_MODE_2x64_CIRCUIT, "2x64kbps, circuit-mode" },
1094
		{ TRANS_MODE_2x64_CIRCUIT, "2x64kbps, circuit-mode" },
1095
		{ TRANS_MODE_384_CIRCUIT, "384kbps, circuit-mode" },
1095
		{ TRANS_MODE_384_CIRCUIT, "384kbps, circuit-mode" },
1096
		{ TRANS_MODE_1536_CIRCUIT, "1536kbps, circuit-mode" },
1096
		{ TRANS_MODE_1536_CIRCUIT, "1536kbps, circuit-mode" },
1097
		{ TRANS_MODE_1920_CIRCUIT, "1920kbps, circuit-mode" },
1097
		{ TRANS_MODE_1920_CIRCUIT, "1920kbps, circuit-mode" },
1098
		{ TRANS_MODE_MULTIRATE, "Multirate (Nx64kbps)" },
1098
		{ TRANS_MODE_MULTIRATE, "Multirate (Nx64kbps)" },
1099
		{ TRANS_MODE_PACKET, "Packet Mode" },
1099
		{ TRANS_MODE_PACKET, "Packet Mode" },
1100
	};
1100
	};
1101
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1101
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1102
}
1102
}
1103

    
   
1103

   
1104
static char *l12str(int proto)
1104
static char *l12str(int proto)
1105
{
1105
{
1106
	static struct msgtype protos[] = {
1106
	static struct msgtype protos[] = {
1107
 		{ PRI_LAYER_1_ITU_RATE_ADAPT, "V.110 Rate Adaption" },
1107
 		{ PRI_LAYER_1_ITU_RATE_ADAPT, "V.110 Rate Adaption" },
1108
		{ PRI_LAYER_1_ULAW, "u-Law" },
1108
		{ PRI_LAYER_1_ULAW, "u-Law" },
1109
		{ PRI_LAYER_1_ALAW, "A-Law" },
1109
		{ PRI_LAYER_1_ALAW, "A-Law" },
1110
		{ PRI_LAYER_1_G721, "G.721 ADPCM" },
1110
		{ PRI_LAYER_1_G721, "G.721 ADPCM" },
1111
		{ PRI_LAYER_1_G722_G725, "G.722/G.725 7kHz Audio" },
1111
		{ PRI_LAYER_1_G722_G725, "G.722/G.725 7kHz Audio" },
1112
 		{ PRI_LAYER_1_H223_H245, "H.223/H.245 Multimedia" },
1112
 		{ PRI_LAYER_1_H223_H245, "H.223/H.245 Multimedia" },
1113
		{ PRI_LAYER_1_NON_ITU_ADAPT, "Non-ITU Rate Adaption" },
1113
		{ PRI_LAYER_1_NON_ITU_ADAPT, "Non-ITU Rate Adaption" },
1114
		{ PRI_LAYER_1_V120_RATE_ADAPT, "V.120 Rate Adaption" },
1114
		{ PRI_LAYER_1_V120_RATE_ADAPT, "V.120 Rate Adaption" },
1115
		{ PRI_LAYER_1_X31_RATE_ADAPT, "X.31 Rate Adaption" },
1115
		{ PRI_LAYER_1_X31_RATE_ADAPT, "X.31 Rate Adaption" },
1116
	};
1116
	};
1117
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1117
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1118
}
1118
}
1119

    
   
1119

   
1120
static char *ra2str(int proto)
1120
static char *ra2str(int proto)
1121
{
1121
{
1122
	static struct msgtype protos[] = {
1122
	static struct msgtype protos[] = {
1123
		{ PRI_RATE_ADAPT_9K6, "9.6 kbit/s" },
1123
		{ PRI_RATE_ADAPT_9K6, "9.6 kbit/s" },
1124
	};
1124
	};
1125
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1125
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1126
}
1126
}
1127

    
   
1127

   
1128
static char *l22str(int proto)
1128
static char *l22str(int proto)
1129
{
1129
{
1130
	static struct msgtype protos[] = {
1130
	static struct msgtype protos[] = {
1131
		{ LAYER_2_LAPB, "LAPB" },
1131
		{ LAYER_2_LAPB, "LAPB" },
1132
	};
1132
	};
1133
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1133
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1134
}
1134
}
1135

    
   
1135

   
1136
static char *l32str(int proto)
1136
static char *l32str(int proto)
1137
{
1137
{
1138
	static struct msgtype protos[] = {
1138
	static struct msgtype protos[] = {
1139
		{ LAYER_3_X25, "X.25" },
1139
		{ LAYER_3_X25, "X.25" },
1140
	};
1140
	};
1141
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1141
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1142
}
1142
}
1143

    
   
1143

   
1144
static char *int_rate2str(int proto)
1144
static char *int_rate2str(int proto)
1145
{
1145
{
1146
    static struct msgtype protos[] = {
1146
    static struct msgtype protos[] = {
1147
		{ PRI_INT_RATE_8K, "8 kbit/s" },
1147
		{ PRI_INT_RATE_8K, "8 kbit/s" },
1148
		{ PRI_INT_RATE_16K, "16 kbit/s" },
1148
		{ PRI_INT_RATE_16K, "16 kbit/s" },
1149
		{ PRI_INT_RATE_32K, "32 kbit/s" },
1149
		{ PRI_INT_RATE_32K, "32 kbit/s" },
1150
    };
1150
    };
1151
    return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1151
    return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1152
}
1152
}
1153

    
   
1153

   
1154
static void dump_bearer_capability(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1154
static void dump_bearer_capability(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1155
{
1155
{
1156
	int pos=2;
1156
	int pos=2;
1157
	pri_message(ctrl, "%c Bearer Capability (len=%2d) [ Ext: %d  Q.931 Std: %d  Info transfer capability: %s (%d)\n",
1157
	pri_message(ctrl, "%c Bearer Capability (len=%2d) [ Ext: %d  Q.931 Std: %d  Info transfer capability: %s (%d)\n",
1158
		prefix, len, (ie->data[0] & 0x80 ) >> 7, (ie->data[0] & 0x60) >> 5, cap2str(ie->data[0] & 0x1f), (ie->data[0] & 0x1f));
1158
		prefix, len, (ie->data[0] & 0x80 ) >> 7, (ie->data[0] & 0x60) >> 5, cap2str(ie->data[0] & 0x1f), (ie->data[0] & 0x1f));
1159
	pri_message(ctrl, "%c                              Ext: %d  Trans mode/rate: %s (%d)\n", prefix, (ie->data[1] & 0x80) >> 7, mode2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1159
	pri_message(ctrl, "%c                              Ext: %d  Trans mode/rate: %s (%d)\n", prefix, (ie->data[1] & 0x80) >> 7, mode2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1160

    
   
1160

   
1161
	/* octet 4.1 exists iff mode/rate is multirate */
1161
	/* octet 4.1 exists iff mode/rate is multirate */
1162
	if ((ie->data[1] & 0x7f) == 0x18) {
1162
	if ((ie->data[1] & 0x7f) == 0x18) {
1163
	    pri_message(ctrl, "%c                              Ext: %d  Transfer rate multiplier: %d x 64\n", prefix, (ie->data[2] & 0x80) >> 7, ie->data[2] & 0x7f);
1163
	    pri_message(ctrl, "%c                              Ext: %d  Transfer rate multiplier: %d x 64\n", prefix, (ie->data[2] & 0x80) >> 7, ie->data[2] & 0x7f);
1164
		pos++;
1164
		pos++;
1165
	}
1165
	}
1166

    
   
1166

   
1167
	/* don't count the IE num and length as part of the data */
1167
	/* don't count the IE num and length as part of the data */
1168
	len -= 2;
1168
	len -= 2;
1169
	
1169
	
1170
	/* Look for octet 5; this is identified by bits 5,6 == 01 */
1170
	/* Look for octet 5; this is identified by bits 5,6 == 01 */
1171
     	if (pos < len &&
1171
     	if (pos < len &&
1172
		(ie->data[pos] & 0x60) == 0x20) {
1172
		(ie->data[pos] & 0x60) == 0x20) {
1173

    
   
1173

   
1174
		/* although the layer1 is only the bottom 5 bits of the byte,
1174
		/* although the layer1 is only the bottom 5 bits of the byte,
1175
		   previous versions of this library passed bits 5&6 through
1175
		   previous versions of this library passed bits 5&6 through
1176
		   too, so we have to do the same for binary compatability */
1176
		   too, so we have to do the same for binary compatability */
1177
		u_int8_t layer1 = ie->data[pos] & 0x7f;
1177
		u_int8_t layer1 = ie->data[pos] & 0x7f;
1178

    
   
1178

   
1179
		pri_message(ctrl, "%c                                User information layer 1: %s (%d)\n",
1179
		pri_message(ctrl, "%c                                User information layer 1: %s (%d)\n",
1180
		            prefix, l12str(layer1), layer1);
1180
		            prefix, l12str(layer1), layer1);
1181
		pos++;
1181
		pos++;
1182
		
1182
		
1183
		/* octet 5a? */
1183
		/* octet 5a? */
1184
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1184
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1185
			int ra = ie->data[pos] & 0x7f;
1185
			int ra = ie->data[pos] & 0x7f;
1186

    
   
1186

   
1187
			pri_message(ctrl, "%c                                Async: %d, Negotiation: %d, "
1187
			pri_message(ctrl, "%c                                Async: %d, Negotiation: %d, "
1188
				"User rate: %s (%#x)\n", 
1188
				"User rate: %s (%#x)\n", 
1189
				prefix,
1189
				prefix,
1190
				ra & PRI_RATE_ADAPT_ASYNC ? 1 : 0,
1190
				ra & PRI_RATE_ADAPT_ASYNC ? 1 : 0,
1191
				ra & PRI_RATE_ADAPT_NEGOTIATION_POSS ? 1 : 0,
1191
				ra & PRI_RATE_ADAPT_NEGOTIATION_POSS ? 1 : 0,
1192
				ra2str(ra & PRI_RATE_USER_RATE_MASK),
1192
				ra2str(ra & PRI_RATE_USER_RATE_MASK),
1193
				ra & PRI_RATE_USER_RATE_MASK);
1193
				ra & PRI_RATE_USER_RATE_MASK);
1194
			pos++;
1194
			pos++;
1195
		}
1195
		}
1196
		
1196
		
1197
		/* octet 5b? */
1197
		/* octet 5b? */
1198
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1198
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1199
			u_int8_t data = ie->data[pos];
1199
			u_int8_t data = ie->data[pos];
1200
			if (layer1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1200
			if (layer1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1201
				pri_message(ctrl, "%c                                Intermediate rate: %s (%d), "
1201
				pri_message(ctrl, "%c                                Intermediate rate: %s (%d), "
1202
					"NIC on Tx: %d, NIC on Rx: %d, "
1202
					"NIC on Tx: %d, NIC on Rx: %d, "
1203
					"Flow control on Tx: %d, "
1203
					"Flow control on Tx: %d, "
1204
					"Flow control on Rx: %d\n",
1204
					"Flow control on Rx: %d\n",
1205
					prefix, int_rate2str((data & 0x60)>>5),
1205
					prefix, int_rate2str((data & 0x60)>>5),
1206
					(data & 0x60)>>5,
1206
					(data & 0x60)>>5,
1207
					(data & 0x10)?1:0,
1207
					(data & 0x10)?1:0,
1208
					(data & 0x08)?1:0,
1208
					(data & 0x08)?1:0,
1209
					(data & 0x04)?1:0,
1209
					(data & 0x04)?1:0,
1210
					(data & 0x02)?1:0);
1210
					(data & 0x02)?1:0);
1211
			} else if (layer1 == PRI_LAYER_1_V120_RATE_ADAPT) {
1211
			} else if (layer1 == PRI_LAYER_1_V120_RATE_ADAPT) {
1212
				pri_message(ctrl, "%c                                Hdr: %d, Multiframe: %d, Mode: %d, "
1212
				pri_message(ctrl, "%c                                Hdr: %d, Multiframe: %d, Mode: %d, "
1213
					"LLI negot: %d, Assignor: %d, "
1213
					"LLI negot: %d, Assignor: %d, "
1214
					"In-band neg: %d\n", prefix,
1214
					"In-band neg: %d\n", prefix,
1215
					(data & 0x40)?1:0,
1215
					(data & 0x40)?1:0,
1216
					(data & 0x20)?1:0,
1216
					(data & 0x20)?1:0,
1217
					(data & 0x10)?1:0,
1217
					(data & 0x10)?1:0,
1218
					(data & 0x08)?1:0,
1218
					(data & 0x08)?1:0,
1219
					(data & 0x04)?1:0,
1219
					(data & 0x04)?1:0,
1220
					(data & 0x02)?1:0);
1220
					(data & 0x02)?1:0);
1221
			} else {
1221
			} else {
1222
				pri_message(ctrl, "%c                                Unknown octet 5b: 0x%x\n",
1222
				pri_message(ctrl, "%c                                Unknown octet 5b: 0x%x\n",
1223
					prefix, data);
1223
					prefix, data);
1224
			}
1224
			}
1225
			pos++;
1225
			pos++;
1226
		}
1226
		}
1227

    
   
1227

   
1228
		/* octet 5c? */
1228
		/* octet 5c? */
1229
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1229
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1230
			u_int8_t data = ie->data[pos];
1230
			u_int8_t data = ie->data[pos];
1231
			const char *stop_bits[] = {"?","1","1.5","2"};
1231
			const char *stop_bits[] = {"?","1","1.5","2"};
1232
			const char *data_bits[] = {"?","5","7","8"};
1232
			const char *data_bits[] = {"?","5","7","8"};
1233
			const char *parity[] = {"Odd","?","Even","None",
1233
			const char *parity[] = {"Odd","?","Even","None",
1234
				       "zero","one","?","?"};
1234
				       "zero","one","?","?"};
1235
	
1235
	
1236
			pri_message(ctrl, "%c                                Stop bits: %s, data bits: %s, "
1236
			pri_message(ctrl, "%c                                Stop bits: %s, data bits: %s, "
1237
			    "parity: %s\n", prefix,
1237
			    "parity: %s\n", prefix,
1238
			    stop_bits[(data & 0x60) >> 5],
1238
			    stop_bits[(data & 0x60) >> 5],
1239
			    data_bits[(data & 0x18) >> 3],
1239
			    data_bits[(data & 0x18) >> 3],
1240
			    parity[(data & 0x7)]);
1240
			    parity[(data & 0x7)]);
1241
	
1241
	
1242
			pos++;
1242
			pos++;
1243
		}
1243
		}
1244
	
1244
	
1245
			/* octet 5d? */
1245
			/* octet 5d? */
1246
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1246
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1247
			u_int8_t data = ie->data[pos];
1247
			u_int8_t data = ie->data[pos];
1248
			pri_message(ctrl, "%c                                Duplex mode: %d, modem type: %d\n",
1248
			pri_message(ctrl, "%c                                Duplex mode: %d, modem type: %d\n",
1249
				prefix, (data & 0x40) ? 1 : 0,data & 0x3F);
1249
				prefix, (data & 0x40) ? 1 : 0,data & 0x3F);
1250
 			pos++;
1250
 			pos++;
1251
		}
1251
		}
1252
 	}
1252
 	}
1253

    
   
1253

   
1254

    
   
1254

   
1255
	/* Look for octet 6; this is identified by bits 5,6 == 10 */
1255
	/* Look for octet 6; this is identified by bits 5,6 == 10 */
1256
	if (pos < len && 
1256
	if (pos < len && 
1257
		(ie->data[pos] & 0x60) == 0x40) {
1257
		(ie->data[pos] & 0x60) == 0x40) {
1258
		pri_message(ctrl, "%c                                User information layer 2: %s (%d)\n",
1258
		pri_message(ctrl, "%c                                User information layer 2: %s (%d)\n",
1259
			prefix, l22str(ie->data[pos] & 0x1f),
1259
			prefix, l22str(ie->data[pos] & 0x1f),
1260
			ie->data[pos] & 0x1f);
1260
			ie->data[pos] & 0x1f);
1261
		pos++;
1261
		pos++;
1262
	}
1262
	}
1263

    
   
1263

   
1264
	/* Look for octet 7; this is identified by bits 5,6 == 11 */
1264
	/* Look for octet 7; this is identified by bits 5,6 == 11 */
1265
	if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
1265
	if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
1266
		pri_message(ctrl, "%c                                User information layer 3: %s (%d)\n",
1266
		pri_message(ctrl, "%c                                User information layer 3: %s (%d)\n",
1267
			prefix, l32str(ie->data[pos] & 0x1f),
1267
			prefix, l32str(ie->data[pos] & 0x1f),
1268
			ie->data[pos] & 0x1f);
1268
			ie->data[pos] & 0x1f);
1269
		pos++;
1269
		pos++;
1270

    
   
1270

   
1271
		/* octets 7a and 7b? */
1271
		/* octets 7a and 7b? */
1272
		if (pos + 1 < len && !(ie->data[pos-1] & 0x80) &&
1272
		if (pos + 1 < len && !(ie->data[pos-1] & 0x80) &&
1273
			!(ie->data[pos] & 0x80)) {
1273
			!(ie->data[pos] & 0x80)) {
1274
			unsigned int proto;
1274
			unsigned int proto;
1275
			proto = ((ie->data[pos] & 0xF) << 4 ) | 
1275
			proto = ((ie->data[pos] & 0xF) << 4 ) | 
1276
			         (ie->data[pos+1] & 0xF);
1276
			         (ie->data[pos+1] & 0xF);
1277

    
   
1277

   
1278
			pri_message(ctrl, "%c                                Network layer: 0x%x\n", prefix,
1278
			pri_message(ctrl, "%c                                Network layer: 0x%x\n", prefix,
1279
			            proto );
1279
			            proto );
1280
			pos += 2;
1280
			pos += 2;
1281
		}
1281
		}
1282
	}
1282
	}
1283
}
1283
}
1284

    
   
1284

   
1285
static int receive_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1285
static int receive_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1286
{
1286
{
1287
	int pos=2;
1287
	int pos=2;
1288
	if (ie->data[0] & 0x60) {
1288
	if (ie->data[0] & 0x60) {
1289
		pri_error(ctrl, "!! non-standard Q.931 standard field\n");
1289
		pri_error(ctrl, "!! non-standard Q.931 standard field\n");
1290
		return -1;
1290
		return -1;
1291
	}
1291
	}
1292
	call->transcapability = ie->data[0] & 0x1f;
1292
	call->transcapability = ie->data[0] & 0x1f;
1293
	call->transmoderate = ie->data[1] & 0x7f;
1293
	call->transmoderate = ie->data[1] & 0x7f;
1294
   
1294
   
1295
	/* octet 4.1 exists iff mode/rate is multirate */
1295
	/* octet 4.1 exists iff mode/rate is multirate */
1296
	if (call->transmoderate == TRANS_MODE_MULTIRATE) {
1296
	if (call->transmoderate == TRANS_MODE_MULTIRATE) {
1297
		call->transmultiple = ie->data[pos++] & 0x7f;
1297
		call->transmultiple = ie->data[pos++] & 0x7f;
1298
	}
1298
	}
1299

    
   
1299

   
1300
	/* Look for octet 5; this is identified by bits 5,6 == 01 */
1300
	/* Look for octet 5; this is identified by bits 5,6 == 01 */
1301
	if (pos < len && 
1301
	if (pos < len && 
1302
	     (ie->data[pos] & 0x60) == 0x20 ) {
1302
	     (ie->data[pos] & 0x60) == 0x20 ) {
1303
		/* although the layer1 is only the bottom 5 bits of the byte,
1303
		/* although the layer1 is only the bottom 5 bits of the byte,
1304
		   previous versions of this library passed bits 5&6 through
1304
		   previous versions of this library passed bits 5&6 through
1305
		   too, so we have to do the same for binary compatability */
1305
		   too, so we have to do the same for binary compatability */
1306
		call->userl1 = ie->data[pos] & 0x7f;
1306
		call->userl1 = ie->data[pos] & 0x7f;
1307
		pos++;
1307
		pos++;
1308
		
1308
		
1309
		/* octet 5a? */
1309
		/* octet 5a? */
1310
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1310
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1311
			call->rateadaption = ie->data[pos] & 0x7f;
1311
			call->rateadaption = ie->data[pos] & 0x7f;
1312
			pos++;
1312
			pos++;
1313
 		}
1313
 		}
1314
		
1314
		
1315
		/* octets 5b through 5d? */
1315
		/* octets 5b through 5d? */
1316
		while (pos < len && !(ie->data[pos-1] & 0x80)) {
1316
		while (pos < len && !(ie->data[pos-1] & 0x80)) {
1317
			pos++;
1317
			pos++;
1318
		}
1318
		}
1319
		
1319
		
1320
	}
1320
	}
1321

    
   
1321

   
1322
	/* Look for octet 6; this is identified by bits 5,6 == 10 */
1322
	/* Look for octet 6; this is identified by bits 5,6 == 10 */
1323
     	if (pos < len && 
1323
     	if (pos < len && 
1324
             (ie->data[pos] & 0x60) == 0x40) {
1324
             (ie->data[pos] & 0x60) == 0x40) {
1325
		call->userl2 = ie->data[pos++] & 0x1f;
1325
		call->userl2 = ie->data[pos++] & 0x1f;
1326
	}
1326
	}
1327

    
   
1327

   
1328
	/* Look for octet 7; this is identified by bits 5,6 == 11 */
1328
	/* Look for octet 7; this is identified by bits 5,6 == 11 */
1329
     	if (pos < len && 
1329
     	if (pos < len && 
1330
             (ie->data[pos] & 0x60) == 0x60) {
1330
             (ie->data[pos] & 0x60) == 0x60) {
1331
		call->userl3 = ie->data[pos++] & 0x1f;
1331
		call->userl3 = ie->data[pos++] & 0x1f;
1332
	}
1332
	}
1333
	return 0;
1333
	return 0;
1334
}
1334
}
1335

    
   
1335

   
1336
static int transmit_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1336
static int transmit_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1337
{
1337
{
1338
	int tc;
1338
	int tc;
1339
	int pos;
1339
	int pos;
1340

    
   
1340

   
1341
	/* We are ready to transmit single IE only */	
1341
	/* We are ready to transmit single IE only */	
1342
	if(order > 1)
1342
	if(order > 1)
1343
		return 0;
1343
		return 0;
1344

    
   
1344

   
1345
	if (ctrl->subchannel && !ctrl->bri) {
1345
	if (ctrl->subchannel && !ctrl->bri) {
1346
		/* Bearer capability is *hard coded* in GR-303 */
1346
		/* Bearer capability is *hard coded* in GR-303 */
1347
		ie->data[0] = 0x88;
1347
		ie->data[0] = 0x88;
1348
		ie->data[1] = 0x90;
1348
		ie->data[1] = 0x90;
1349
		return 4;
1349
		return 4;
1350
	}
1350
	}
1351

    
   
1351

   
1352
	if (call->cis_call) {
1352
	if (call->cis_call) {
1353
		ie->data[0] = 0xa8;
1353
		ie->data[0] = 0xa8;
1354
		ie->data[1] = 0x80;
1354
		ie->data[1] = 0x80;
1355
		return 4;
1355
		return 4;
1356
	}
1356
	}
1357

    
   
1357

   
1358
	tc = call->transcapability;
1358
	tc = call->transcapability;
1359
	ie->data[0] = 0x80 | tc;
1359
	ie->data[0] = 0x80 | tc;
1360
	ie->data[1] = call->transmoderate | 0x80;
1360
	ie->data[1] = call->transmoderate | 0x80;
1361

    
   
1361

   
1362
 	pos = 2;
1362
 	pos = 2;
1363
 	/* octet 4.1 exists iff mode/rate is multirate */
1363
 	/* octet 4.1 exists iff mode/rate is multirate */
1364
 	if (call->transmoderate == TRANS_MODE_MULTIRATE ) {
1364
 	if (call->transmoderate == TRANS_MODE_MULTIRATE ) {
1365
 		ie->data[pos++] = call->transmultiple | 0x80;
1365
 		ie->data[pos++] = call->transmultiple | 0x80;
1366
	}
1366
	}
1367

    
   
1367

   
1368
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) &&
1368
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) &&
1369
		(call->transmoderate == TRANS_MODE_PACKET)) {
1369
		(call->transmoderate == TRANS_MODE_PACKET)) {
1370
		/* Apparently EuroISDN switches don't seem to like user layer 2/3 */
1370
		/* Apparently EuroISDN switches don't seem to like user layer 2/3 */
1371
		return 4;
1371
		return 4;
1372
	}
1372
	}
1373

    
   
1373

   
1374
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (call->transmoderate == TRANS_MODE_64_CIRCUIT)) {
1374
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (call->transmoderate == TRANS_MODE_64_CIRCUIT)) {
1375
		/* Unrestricted digital 64k data calls don't use user layer 2/3 */
1375
		/* Unrestricted digital 64k data calls don't use user layer 2/3 */
1376
		return 4;
1376
		return 4;
1377
	}
1377
	}
1378

    
   
1378

   
1379
	if (call->transmoderate != TRANS_MODE_PACKET) {
1379
	if (call->transmoderate != TRANS_MODE_PACKET) {
1380
		/* If you have an AT&T 4ESS, you don't send any more info */
1380
		/* If you have an AT&T 4ESS, you don't send any more info */
1381
		if ((ctrl->switchtype != PRI_SWITCH_ATT4ESS) && (call->userl1 > -1)) {
1381
		if ((ctrl->switchtype != PRI_SWITCH_ATT4ESS) && (call->userl1 > -1)) {
1382
			ie->data[pos++] = call->userl1 | 0x80; /* XXX Ext bit? XXX */
1382
			ie->data[pos++] = call->userl1 | 0x80; /* XXX Ext bit? XXX */
1383
			if (call->userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1383
			if (call->userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1384
				ie->data[pos++] = call->rateadaption | 0x80;
1384
				ie->data[pos++] = call->rateadaption | 0x80;
1385
			}
1385
			}
1386
			return pos + 2;
1386
			return pos + 2;
1387
 		}
1387
 		}
1388
 
1388
 
1389
 		ie->data[pos++] = 0xa0 | (call->userl1 & 0x1f);
1389
 		ie->data[pos++] = 0xa0 | (call->userl1 & 0x1f);
1390
 
1390
 
1391
 		if (call->userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1391
 		if (call->userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1392
 		    ie->data[pos-1] &= ~0x80; /* clear EXT bit in octet 5 */
1392
 		    ie->data[pos-1] &= ~0x80; /* clear EXT bit in octet 5 */
1393
 		    ie->data[pos++] = call->rateadaption | 0x80;
1393
 		    ie->data[pos++] = call->rateadaption | 0x80;
1394
 		}
1394
 		}
1395
 	}
1395
 	}
1396
 	
1396
 	
1397
 	
1397
 	
1398
 	if (call->userl2 != -1)
1398
 	if (call->userl2 != -1)
1399
 		ie->data[pos++] = 0xc0 | (call->userl2 & 0x1f);
1399
 		ie->data[pos++] = 0xc0 | (call->userl2 & 0x1f);
1400
 
1400
 
1401
 	if (call->userl3 != -1)
1401
 	if (call->userl3 != -1)
1402
 		ie->data[pos++] = 0xe0 | (call->userl3 & 0x1f);
1402
 		ie->data[pos++] = 0xe0 | (call->userl3 & 0x1f);
1403
 
1403
 
1404
 	return pos + 2;
1404
 	return pos + 2;
1405
}
1405
}
1406

    
   
1406

   
1407
char *pri_plan2str(int plan)
1407
char *pri_plan2str(int plan)
1408
{
1408
{
1409
	static struct msgtype plans[] = {
1409
	static struct msgtype plans[] = {
1410
		{ PRI_INTERNATIONAL_ISDN, "International number in ISDN" },
1410
		{ PRI_INTERNATIONAL_ISDN, "International number in ISDN" },
1411
		{ PRI_NATIONAL_ISDN, "National number in ISDN" },
1411
		{ PRI_NATIONAL_ISDN, "National number in ISDN" },
1412
		{ PRI_LOCAL_ISDN, "Local number in ISDN" },
1412
		{ PRI_LOCAL_ISDN, "Local number in ISDN" },
1413
		{ PRI_PRIVATE, "Private numbering plan" },
1413
		{ PRI_PRIVATE, "Private numbering plan" },
1414
		{ PRI_UNKNOWN, "Unknown numbering plan" },
1414
		{ PRI_UNKNOWN, "Unknown numbering plan" },
1415
	};
1415
	};
1416
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1416
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1417
}
1417
}
1418

    
   
1418

   
1419
static char *npi2str(int plan)
1419
static char *npi2str(int plan)
1420
{
1420
{
1421
	static struct msgtype plans[] = {
1421
	static struct msgtype plans[] = {
1422
		{ PRI_NPI_UNKNOWN, "Unknown Number Plan" },
1422
		{ PRI_NPI_UNKNOWN, "Unknown Number Plan" },
1423
		{ PRI_NPI_E163_E164, "ISDN/Telephony Numbering Plan (E.164/E.163)" },
1423
		{ PRI_NPI_E163_E164, "ISDN/Telephony Numbering Plan (E.164/E.163)" },
1424
		{ PRI_NPI_X121, "Data Numbering Plan (X.121)" },
1424
		{ PRI_NPI_X121, "Data Numbering Plan (X.121)" },
1425
		{ PRI_NPI_F69, "Telex Numbering Plan (F.69)" },
1425
		{ PRI_NPI_F69, "Telex Numbering Plan (F.69)" },
1426
		{ PRI_NPI_NATIONAL, "National Standard Numbering Plan" },
1426
		{ PRI_NPI_NATIONAL, "National Standard Numbering Plan" },
1427
		{ PRI_NPI_PRIVATE, "Private Numbering Plan" },
1427
		{ PRI_NPI_PRIVATE, "Private Numbering Plan" },
1428
		{ PRI_NPI_RESERVED, "Reserved Number Plan" },
1428
		{ PRI_NPI_RESERVED, "Reserved Number Plan" },
1429
	};
1429
	};
1430
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1430
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1431
}
1431
}
1432

    
   
1432

   
1433
static char *ton2str(int plan)
1433
static char *ton2str(int plan)
1434
{
1434
{
1435
	static struct msgtype plans[] = {
1435
	static struct msgtype plans[] = {
1436
		{ PRI_TON_UNKNOWN, "Unknown Number Type" },
1436
		{ PRI_TON_UNKNOWN, "Unknown Number Type" },
1437
		{ PRI_TON_INTERNATIONAL, "International Number" },
1437
		{ PRI_TON_INTERNATIONAL, "International Number" },
1438
		{ PRI_TON_NATIONAL, "National Number" },
1438
		{ PRI_TON_NATIONAL, "National Number" },
1439
		{ PRI_TON_NET_SPECIFIC, "Network Specific Number" },
1439
		{ PRI_TON_NET_SPECIFIC, "Network Specific Number" },
1440
		{ PRI_TON_SUBSCRIBER, "Subscriber Number" },
1440
		{ PRI_TON_SUBSCRIBER, "Subscriber Number" },
1441
		{ PRI_TON_ABBREVIATED, "Abbreviated number" },
1441
		{ PRI_TON_ABBREVIATED, "Abbreviated number" },
1442
		{ PRI_TON_RESERVED, "Reserved Number" },
1442
		{ PRI_TON_RESERVED, "Reserved Number" },
1443
	};
1443
	};
1444
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1444
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1445
}
1445
}
1446

    
   
1446

   
1447
static char *subaddrtype2str(int plan)
1447
static char *subaddrtype2str(int plan)
1448
{
1448
{
1449
	static struct msgtype plans[] = {
1449
	static struct msgtype plans[] = {
1450
		{ 0, "NSAP (X.213/ISO 8348 AD2)" },
1450
		{ 0, "NSAP (X.213/ISO 8348 AD2)" },
1451
		{ 2, "User Specified" },
1451
		{ 2, "User Specified" },
1452
	};
1452
	};
1453
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1453
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1454
}
1454
}
1455

    
   
1455

   
1456
char *pri_pres2str(int pres)
1456
char *pri_pres2str(int pres)
1457
{
1457
{
1458
	static struct msgtype press[] = {
1458
	static struct msgtype press[] = {
1459
		{ PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "Presentation permitted, user number not screened" },
1459
		{ PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "Presentation permitted, user number not screened" },
1460
		{ PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "Presentation permitted, user number passed network screening" },
1460
		{ PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "Presentation permitted, user number passed network screening" },
1461
		{ PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "Presentation permitted, user number failed network screening" },
1461
		{ PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "Presentation permitted, user number failed network screening" },
1462
		{ PRES_ALLOWED_NETWORK_NUMBER, "Presentation allowed of network provided number" },
1462
		{ PRES_ALLOWED_NETWORK_NUMBER, "Presentation allowed of network provided number" },
1463
		{ PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "Presentation prohibited, user number not screened" },
1463
		{ PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "Presentation prohibited, user number not screened" },
1464
		{ PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "Presentation prohibited, user number passed network screening" },
1464
		{ PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "Presentation prohibited, user number passed network screening" },
1465
		{ PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "Presentation prohibited, user number failed network screening" },
1465
		{ PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "Presentation prohibited, user number failed network screening" },
1466
		{ PRES_PROHIB_NETWORK_NUMBER, "Presentation prohibited of network provided number" },
1466
		{ PRES_PROHIB_NETWORK_NUMBER, "Presentation prohibited of network provided number" },
1467
		{ PRES_NUMBER_NOT_AVAILABLE, "Number not available" },
1467
		{ PRES_NUMBER_NOT_AVAILABLE, "Number not available" },
1468
	};
1468
	};
1469
	return code2str(pres, press, sizeof(press) / sizeof(press[0]));
1469
	return code2str(pres, press, sizeof(press) / sizeof(press[0]));
1470
}
1470
}
1471

    
   
1471

   
1472
static void q931_get_number(unsigned char *num, int maxlen, unsigned char *src, int len)
1472
static void q931_get_number(unsigned char *num, int maxlen, unsigned char *src, int len)
1473
{
1473
{
1474
	if ((len < 0) || (len > maxlen - 1)) {
1474
	if ((len < 0) || (len > maxlen - 1)) {
1475
		num[0] = 0;
1475
		num[0] = 0;
1476
		return;
1476
		return;
1477
	}
1477
	}
1478
	memcpy(num, src, len);
1478
	memcpy(num, src, len);
1479
	num[len] = 0;
1479
	num[len] = 0;
1480
}
1480
}
1481

    
   
1481

   
1482
static void q931_get_subaddr_specific(unsigned char *num, int maxlen, unsigned char *src, int len, char oddflag)
1482
static void q931_get_subaddr_specific(unsigned char *num, int maxlen, unsigned char *src, int len, char oddflag)
1483
{
1483
{
1484
	/* User Specified */
1484
	/* User Specified */
1485
	int x;
1485
	int x;
1486
	char *ptr = (char *) num;
1486
	char *ptr = (char *) num;
1487

    
   
1487

   
1488
	if (len <= 0) {
1488
	if (len <= 0) {
1489
		num[0] = '\0';
1489
		num[0] = '\0';
1490
		return;
1490
		return;
1491
	}
1491
	}
1492

    
   
1492

   
1493
	if (((len * 2) + 1) > maxlen) {
1493
	if (((len * 2) + 1) > maxlen) {
1494
		len = (maxlen / 2) - 1;
1494
		len = (maxlen / 2) - 1;
1495
	}
1495
	}
1496

    
   
1496

   
1497
	for (x = 0; x < (len - 1); ++x)
1497
	for (x = 0; x < (len - 1); ++x)
1498
		ptr += sprintf(ptr, "%02x", src[x]);
1498
		ptr += sprintf(ptr, "%02x", src[x]);
1499

    
   
1499

   
1500
	if (oddflag) {
1500
	if (oddflag) {
1501
		/* ODD */
1501
		/* ODD */
1502
		sprintf(ptr, "%01x", (src[len - 1]) >> 4);
1502
		sprintf(ptr, "%01x", (src[len - 1]) >> 4);
1503
	} else {
1503
	} else {
1504
		/* EVEN */
1504
		/* EVEN */
1505
		sprintf(ptr, "%02x", src[len - 1]);
1505
		sprintf(ptr, "%02x", src[len - 1]);
1506
	}
1506
	}
1507
}
1507
}
1508

    
   
1508

   
1509
static int transmit_subaddr_helper(int full_ie, struct pri *ctrl, struct q931_party_subaddress *q931_subaddress, int msgtype, q931_ie *ie, int offset, int len, int order)
1509
static int transmit_subaddr_helper(int full_ie, struct pri *ctrl, struct q931_party_subaddress *q931_subaddress, int msgtype, q931_ie *ie, int offset, int len, int order)
1510
{
1510
{
1511
	size_t datalen;
1511
	size_t datalen;
1512

    
   
1512

   
1513
	if (!q931_subaddress->valid) {
1513
	if (!q931_subaddress->valid) {

    
   
1514
		pri_message(ctrl, "ALEC transmit_subaddr_helper valid = '0'!\n");
1514
		return 0;
1515
		return 0;
1515
	}
1516
	}
1516

    
   
1517

   

    
   
1518
	pri_message(ctrl, "ALEC transmit_subaddr_helper valid = '1'\n");
1517
	datalen = q931_subaddress->length;
1519
	datalen = q931_subaddress->length;
1518
	if (!q931_subaddress->type) {
1520
	if (!q931_subaddress->type) {
1519
		/* 0 = NSAP */
1521
		/* 0 = NSAP */
1520
		/* 0 = Odd/Even indicator */
1522
		/* 0 = Odd/Even indicator */
1521
		ie->data[0] = 0x80;
1523
		ie->data[0] = 0x80;
1522
	} else {
1524
	} else {
1523
		/* 2 = User Specified */
1525
		/* 2 = User Specified */
1524
		ie->data[0] = q931_subaddress->odd_even_indicator ? 0xA8 : 0xA0;
1526
		ie->data[0] = q931_subaddress->odd_even_indicator ? 0xA8 : 0xA0;
1525
	}
1527
	}
1526
	memcpy(ie->data + offset, q931_subaddress->data, datalen);
1528
	memcpy(ie->data + offset, q931_subaddress->data, datalen);
1527

    
   
1529

   
1528
	return datalen + (offset + 2);
1530
	return datalen + (offset + 2);
1529
}
1531
}
1530

    
   
1532

   
1531
static int receive_subaddr_helper(int full_ie, struct pri *ctrl, struct q931_party_subaddress *q931_subaddress, int msgtype, q931_ie *ie, int offset, int len)
1533
static int receive_subaddr_helper(int full_ie, struct pri *ctrl, struct q931_party_subaddress *q931_subaddress, int msgtype, q931_ie *ie, int offset, int len)
1532
{
1534
{
1533
	if (len <= 0) {
1535
	if (len <= 0) {
1534
		return -1;
1536
		return -1;
1535
	}
1537
	}
1536

    
   
1538

   
1537
	q931_subaddress->valid = 1;
1539
	q931_subaddress->valid = 1;
1538
	q931_subaddress->length = len;
1540
	q931_subaddress->length = len;
1539
	/* type: 0 = NSAP, 2 = User Specified */
1541
	/* type: 0 = NSAP, 2 = User Specified */
1540
	q931_subaddress->type = ((ie->data[0] & 0x70) >> 4);
1542
	q931_subaddress->type = ((ie->data[0] & 0x70) >> 4);
1541
	q931_subaddress->odd_even_indicator = (ie->data[0] & 0x08) ? 1 : 0;
1543
	q931_subaddress->odd_even_indicator = (ie->data[0] & 0x08) ? 1 : 0;
1542
	q931_get_number((unsigned char *) q931_subaddress->data, sizeof(q931_subaddress->data), ie->data + offset, len );
1544
	q931_get_number((unsigned char *) q931_subaddress->data, sizeof(q931_subaddress->data), ie->data + offset, len );
1543

    
   
1545

   
1544
	return 0;
1546
	return 0;
1545
}
1547
}
1546

    
   
1548

   
1547
static void dump_subaddr_helper(int full_ie, struct pri *ctrl, q931_ie *ie, int offset, int len, int datalen, char prefix, const char *named)
1549
static void dump_subaddr_helper(int full_ie, struct pri *ctrl, q931_ie *ie, int offset, int len, int datalen, char prefix, const char *named)
1548
{
1550
{
1549
	unsigned char cnum[256];
1551
	unsigned char cnum[256];
1550

    
   
1552

   
1551
	if (!(ie->data[0] & 0x70)) {
1553
	if (!(ie->data[0] & 0x70)) {
1552
		/* NSAP */
1554
		/* NSAP */
1553
		q931_get_number(cnum, sizeof(cnum), ie->data + offset, datalen);
1555
		q931_get_number(cnum, sizeof(cnum), ie->data + offset, datalen);
1554
	} else {
1556
	} else {
1555
		/* User Specified */
1557
		/* User Specified */
1556
		q931_get_subaddr_specific(cnum, sizeof(cnum), ie->data + offset, datalen, ie->data[0] & 0x08);
1558
		q931_get_subaddr_specific(cnum, sizeof(cnum), ie->data + offset, datalen, ie->data[0] & 0x08);
1557
	}
1559
	}
1558

    
   
1560

   
1559
	pri_message(ctrl, "%c %s Sub-Address (len=%2d) [ Ext: %d  Type: %s (%d)  O: %d  '%s' ]\n",
1561
	pri_message(ctrl, "%c %s Sub-Address (len=%2d) [ Ext: %d  Type: %s (%d)  O: %d  '%s' ]\n",
1560
		prefix, named, len, ie->data[0] >> 7,
1562
		prefix, named, len, ie->data[0] >> 7,
1561
		subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
1563
		subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
1562
		(ie->data[0] & 0x08) >> 3, cnum);
1564
		(ie->data[0] & 0x08) >> 3, cnum);
1563
}
1565
}
1564

    
   
1566

   
1565
static void dump_called_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1567
static void dump_called_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1566
{
1568
{
1567
	unsigned char cnum[256];
1569
	unsigned char cnum[256];
1568

    
   
1570

   
1569
	q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
1571
	q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
1570
	pri_message(ctrl, "%c Called Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)  '%s' ]\n",
1572
	pri_message(ctrl, "%c Called Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)  '%s' ]\n",
1571
		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);
1573
		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);
1572
}
1574
}
1573

    
   
1575

   
1574
static void dump_called_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1576
static void dump_called_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1575
{
1577
{
1576
	dump_subaddr_helper(full_ie, ctrl, ie, 1 , len, len - 3, prefix, "Called");
1578
	dump_subaddr_helper(full_ie, ctrl, ie, 1 , len, len - 3, prefix, "Called");
1577
}
1579
}
1578

    
   
1580

   
1579
static void dump_calling_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1581
static void dump_calling_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1580
{
1582
{
1581
	unsigned char cnum[256];
1583
	unsigned char cnum[256];
1582
	if (ie->data[0] & 0x80)
1584
	if (ie->data[0] & 0x80)
1583
		q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
1585
		q931_get_number(cnum, sizeof(cnum), ie->data + 1, len - 3);
1584
	else
1586
	else
1585
		q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
1587
		q931_get_number(cnum, sizeof(cnum), ie->data + 2, len - 4);
1586
	pri_message(ctrl, "%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);
1588
	pri_message(ctrl, "%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);
1587
	if (ie->data[0] & 0x80)
1589
	if (ie->data[0] & 0x80)
1588
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
1590
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
1589
	else
1591
	else
1590
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
1592
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
1591
}
1593
}
1592

    
   
1594

   
1593
static void dump_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1595
static void dump_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1594
{
1596
{
1595
	dump_subaddr_helper(full_ie, ctrl, ie, 1 , len, len - 3, prefix, "Calling");
1597
	dump_subaddr_helper(full_ie, ctrl, ie, 1 , len, len - 3, prefix, "Calling");
1596
}
1598
}
1597

    
   
1599

   
1598
static void dump_redirecting_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1600
static void dump_redirecting_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1599
{
1601
{
1600
	unsigned char cnum[256];
1602
	unsigned char cnum[256];
1601
	int i = 0;
1603
	int i = 0;
1602
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1604
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1603
	   walking through all bytes until one with ext bit (8) set to 1 */
1605
	   walking through all bytes until one with ext bit (8) set to 1 */
1604
	do {
1606
	do {
1605
		switch(i) {
1607
		switch(i) {
1606
		case 0:	/* Octet 3 */
1608
		case 0:	/* Octet 3 */
1607
			pri_message(ctrl, "%c Redirecting Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
1609
			pri_message(ctrl, "%c Redirecting Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
1608
				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);
1610
				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);
1609
			break;
1611
			break;
1610
		case 1: /* Octet 3a */
1612
		case 1: /* Octet 3a */
1611
			pri_message(ctrl, "\n%c                               Ext: %d  Presentation: %s (%d)",
1613
			pri_message(ctrl, "\n%c                               Ext: %d  Presentation: %s (%d)",
1612
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1614
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1613
			break;
1615
			break;
1614
		case 2: /* Octet 3b */
1616
		case 2: /* Octet 3b */
1615
			pri_message(ctrl, "\n%c                               Ext: %d  Reason: %s (%d)",
1617
			pri_message(ctrl, "\n%c                               Ext: %d  Reason: %s (%d)",
1616
				prefix, ie->data[2] >> 7, redirection_reason2str(ie->data[2] & 0x7f), ie->data[2] & 0x7f);
1618
				prefix, ie->data[2] >> 7, redirection_reason2str(ie->data[2] & 0x7f), ie->data[2] & 0x7f);
1617
			break;
1619
			break;
1618
		}
1620
		}
1619
	} while(!(ie->data[i++]& 0x80));
1621
	} while(!(ie->data[i++]& 0x80));
1620
	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
1622
	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
1621
	pri_message(ctrl, "  '%s' ]\n", cnum);
1623
	pri_message(ctrl, "  '%s' ]\n", cnum);
1622
}
1624
}
1623

    
   
1625

   
1624
static void dump_redirection_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1626
static void dump_redirection_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1625
{
1627
{
1626
	unsigned char cnum[256];
1628
	unsigned char cnum[256];
1627
	int i = 0;
1629
	int i = 0;
1628
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1630
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1629
	   walking through all bytes until one with ext bit (8) set to 1 */
1631
	   walking through all bytes until one with ext bit (8) set to 1 */
1630
	do {
1632
	do {
1631
		switch (i) {
1633
		switch (i) {
1632
		case 0:	/* Octet 3 */
1634
		case 0:	/* Octet 3 */
1633
			pri_message(ctrl,
1635
			pri_message(ctrl,
1634
				"%c Redirection Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
1636
				"%c Redirection Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
1635
				prefix, len, ie->data[0] >> 7,
1637
				prefix, len, ie->data[0] >> 7,
1636
				ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
1638
				ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
1637
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
1639
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
1638
			break;
1640
			break;
1639
		case 1: /* Octet 3a */
1641
		case 1: /* Octet 3a */
1640
			pri_message(ctrl, "\n%c                               Ext: %d  Presentation: %s (%d)",
1642
			pri_message(ctrl, "\n%c                               Ext: %d  Presentation: %s (%d)",
1641
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1643
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1642
			break;
1644
			break;
1643
		}
1645
		}
1644
	} while (!(ie->data[i++] & 0x80));
1646
	} while (!(ie->data[i++] & 0x80));
1645
	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
1647
	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
1646
	pri_message(ctrl, "  '%s' ]\n", cnum);
1648
	pri_message(ctrl, "  '%s' ]\n", cnum);
1647
}
1649
}
1648

    
   
1650

   
1649
static int receive_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1651
static int receive_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1650
{
1652
{
1651
	int i = 0;
1653
	int i = 0;
1652

    
   
1654

   
1653
	call->remote_id.number.valid = 1;
1655
	call->remote_id.number.valid = 1;
1654
	call->remote_id.number.presentation =
1656
	call->remote_id.number.presentation =
1655
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1657
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1656
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1658
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1657
	   walking through all bytes until one with ext bit (8) set to 1 */
1659
	   walking through all bytes until one with ext bit (8) set to 1 */
1658
	do {
1660
	do {
1659
		switch (i) {
1661
		switch (i) {
1660
		case 0:
1662
		case 0:
1661
			call->remote_id.number.plan = ie->data[i] & 0x7f;
1663
			call->remote_id.number.plan = ie->data[i] & 0x7f;
1662
			break;
1664
			break;
1663
		case 1:
1665
		case 1:
1664
			/* Keep only the presentation and screening fields */
1666
			/* Keep only the presentation and screening fields */
1665
			call->remote_id.number.presentation =
1667
			call->remote_id.number.presentation =
1666
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1668
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1667
			break;
1669
			break;
1668
		}
1670
		}
1669
	} while (!(ie->data[i++] & 0x80));
1671
	} while (!(ie->data[i++] & 0x80));
1670
	q931_get_number((unsigned char *) call->remote_id.number.str, sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);
1672
	q931_get_number((unsigned char *) call->remote_id.number.str, sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);

    
   
1673
	pri_message(ctrl, "ALEC transmit_connected_number str = '%s'\n", call->remote_id.number.str);
1671

    
   
1674

   
1672
	return 0;
1675
	return 0;
1673
}
1676
}
1674

    
   
1677

   
1675
static int transmit_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1678
static int transmit_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1676
{
1679
{
1677
	size_t datalen;
1680
	size_t datalen;
1678

    
   
1681

   
1679
	if (!call->local_id.number.valid) {
1682
	if (!call->local_id.number.valid) {

    
   
1683
		pri_message(ctrl, "ALEC transmit_connected_number valid = '0' !\n");
1680
		return 0;
1684
		return 0;
1681
	}
1685
	}
1682

    
   
1686

   
1683
	datalen = strlen(call->local_id.number.str);
1687
	datalen = strlen(call->local_id.number.str);
1684
	ie->data[0] = call->local_id.number.plan;
1688
	ie->data[0] = call->local_id.number.plan;
1685
	ie->data[1] = 0x80 | call->local_id.number.presentation;
1689
	ie->data[1] = 0x80 | call->local_id.number.presentation;
1686
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
1690
	memcpy(ie->data + 2, call->local_id.number.str, datalen);

    
   
1691
	pri_message(ctrl, "ALEC transmit_connected_number str= '%s'\n", call->local_id.number.str);
1687
	return datalen + (2 + 2);
1692
	return datalen + (2 + 2);
1688
}
1693
}
1689

    
   
1694

   
1690
static void dump_connected_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1695
static void dump_connected_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1691
{
1696
{
1692
	unsigned char cnum[256];
1697
	unsigned char cnum[256];
1693
	int i = 0;
1698
	int i = 0;
1694
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1699
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1695
	   walking through all bytes until one with ext bit (8) set to 1 */
1700
	   walking through all bytes until one with ext bit (8) set to 1 */
1696
	do {
1701
	do {
1697
		switch(i) {
1702
		switch(i) {
1698
		case 0:	/* Octet 3 */
1703
		case 0:	/* Octet 3 */
1699
			pri_message(ctrl, "%c Connected Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
1704
			pri_message(ctrl, "%c Connected Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
1700
				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);
1705
				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);
1701
			break;
1706
			break;
1702
		case 1: /* Octet 3a */
1707
		case 1: /* Octet 3a */
1703
			pri_message(ctrl, "\n%c                             Ext: %d  Presentation: %s (%d)",
1708
			pri_message(ctrl, "\n%c                             Ext: %d  Presentation: %s (%d)",
1704
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1709
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
1705
			break;
1710
			break;
1706
		}
1711
		}
1707
	} while(!(ie->data[i++]& 0x80));
1712
	} while(!(ie->data[i++]& 0x80));
1708
	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
1713
	q931_get_number(cnum, sizeof(cnum), ie->data + i, ie->len - i);
1709
	pri_message(ctrl, "  '%s' ]\n", cnum);
1714
	pri_message(ctrl, "  '%s' ]\n", cnum);
1710
}
1715
}
1711

    
   
1716

   
1712
static int receive_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1717
static int receive_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1713
{
1718
{
1714
	if (len < 3) {
1719
	if (len < 3) {
1715
		return -1;
1720
		return -1;
1716
	}
1721
	}
1717

    
   
1722

   

    
   
1723
	pri_message(ctrl, "ALEC receive_connected_subaddr start:\n");
1718
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie, 1, len - 3);
1724
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie, 1, len - 3);
1719
}
1725
}
1720

    
   
1726

   
1721
static int transmit_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1727
static int transmit_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1722
{
1728
{

    
   
1729
	pri_message(ctrl, "ALEC transmit_connected_subaddr start:\n");
1723
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie, 1, len, order);
1730
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie, 1, len, order);
1724
}
1731
}
1725

    
   
1732

   
1726
static void dump_connected_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1733
static void dump_connected_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1727
{
1734
{
1728
	dump_subaddr_helper(full_ie, ctrl, ie, 1 , len, len - 3, prefix, "Connected");
1735
	dump_subaddr_helper(full_ie, ctrl, ie, 1 , len, len - 3, prefix, "Connected");
1729
}
1736
}
1730

    
   
1737

   
1731
static int receive_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1738
static int receive_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1732
{
1739
{
1733
	int i = 0;
1740
	int i = 0;
1734

    
   
1741

   
1735
	call->redirecting.from.number.valid = 1;
1742
	call->redirecting.from.number.valid = 1;
1736
	call->redirecting.from.number.presentation =
1743
	call->redirecting.from.number.presentation =
1737
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1744
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1738
	call->redirecting.reason = PRI_REDIR_UNKNOWN;
1745
	call->redirecting.reason = PRI_REDIR_UNKNOWN;
1739
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1746
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1740
	   walking through all bytes until one with ext bit (8) set to 1 */
1747
	   walking through all bytes until one with ext bit (8) set to 1 */
1741
	do {
1748
	do {
1742
		switch (i) {
1749
		switch (i) {
1743
		case 0:
1750
		case 0:
1744
			call->redirecting.from.number.plan = ie->data[i] & 0x7f;
1751
			call->redirecting.from.number.plan = ie->data[i] & 0x7f;
1745
			break;
1752
			break;
1746
		case 1:
1753
		case 1:
1747
			/* Keep only the presentation and screening fields */
1754
			/* Keep only the presentation and screening fields */
1748
			call->redirecting.from.number.presentation =
1755
			call->redirecting.from.number.presentation =
1749
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1756
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1750
			break;
1757
			break;
1751
		case 2:
1758
		case 2:
1752
			call->redirecting.reason = ie->data[i] & 0x0f;
1759
			call->redirecting.reason = ie->data[i] & 0x0f;
1753
			break;
1760
			break;
1754
		}
1761
		}
1755
	} while (!(ie->data[i++] & 0x80));
1762
	} while (!(ie->data[i++] & 0x80));
1756
	q931_get_number((unsigned char *) call->redirecting.from.number.str, sizeof(call->redirecting.from.number.str), ie->data + i, ie->len - i);
1763
	q931_get_number((unsigned char *) call->redirecting.from.number.str, sizeof(call->redirecting.from.number.str), ie->data + i, ie->len - i);
1757
	return 0;
1764
	return 0;
1758
}
1765
}
1759

    
   
1766

   
1760
static int transmit_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1767
static int transmit_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1761
{
1768
{
1762
	size_t datalen;
1769
	size_t datalen;
1763

    
   
1770

   
1764
	if (order > 1)
1771
	if (order > 1)
1765
		return 0;
1772
		return 0;
1766
	if (!call->redirecting.from.number.valid) {
1773
	if (!call->redirecting.from.number.valid) {
1767
		return 0;
1774
		return 0;
1768
	}
1775
	}
1769

    
   
1776

   
1770
	datalen = strlen(call->redirecting.from.number.str);
1777
	datalen = strlen(call->redirecting.from.number.str);
1771
	ie->data[0] = call->redirecting.from.number.plan;
1778
	ie->data[0] = call->redirecting.from.number.plan;
1772
#if 1
1779
#if 1
1773
	/* ETSI and Q.952 do not define the screening field */
1780
	/* ETSI and Q.952 do not define the screening field */
1774
	ie->data[1] = call->redirecting.from.number.presentation & PRI_PRES_RESTRICTION;
1781
	ie->data[1] = call->redirecting.from.number.presentation & PRI_PRES_RESTRICTION;
1775
#else
1782
#else
1776
	/* Q.931 defines the screening field */
1783
	/* Q.931 defines the screening field */
1777
	ie->data[1] = call->redirecting.from.number.presentation;
1784
	ie->data[1] = call->redirecting.from.number.presentation;
1778
#endif
1785
#endif
1779
	ie->data[2] = (call->redirecting.reason & 0x0f) | 0x80;
1786
	ie->data[2] = (call->redirecting.reason & 0x0f) | 0x80;
1780
	memcpy(ie->data + 3, call->redirecting.from.number.str, datalen);
1787
	memcpy(ie->data + 3, call->redirecting.from.number.str, datalen);
1781
	return datalen + (3 + 2);
1788
	return datalen + (3 + 2);
1782
}
1789
}
1783

    
   
1790

   
1784
static void dump_redirecting_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1791
static void dump_redirecting_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1785
{
1792
{
1786
	dump_subaddr_helper(full_ie, ctrl, ie, 2, len, len - 4, prefix, "Redirecting");
1793
	dump_subaddr_helper(full_ie, ctrl, ie, 2, len, len - 4, prefix, "Redirecting");
1787
}
1794
}
1788

    
   
1795

   
1789
static int receive_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1796
static int receive_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1790
{
1797
{
1791
	int i = 0;
1798
	int i = 0;
1792

    
   
1799

   
1793
	call->redirection_number.valid = 1;
1800
	call->redirection_number.valid = 1;
1794
	call->redirection_number.presentation =
1801
	call->redirection_number.presentation =
1795
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1802
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1796
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1803
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1797
	   walking through all bytes until one with ext bit (8) set to 1 */
1804
	   walking through all bytes until one with ext bit (8) set to 1 */
1798
	do {
1805
	do {
1799
		switch (i) {
1806
		switch (i) {
1800
		case 0:
1807
		case 0:
1801
			call->redirection_number.plan = ie->data[i] & 0x7f;
1808
			call->redirection_number.plan = ie->data[i] & 0x7f;
1802
			break;
1809
			break;
1803
		case 1:
1810
		case 1:
1804
			/* Keep only the presentation and screening fields */
1811
			/* Keep only the presentation and screening fields */
1805
			call->redirection_number.presentation =
1812
			call->redirection_number.presentation =
1806
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1813
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1807
			break;
1814
			break;
1808
		}
1815
		}
1809
	} while (!(ie->data[i++] & 0x80));
1816
	} while (!(ie->data[i++] & 0x80));
1810
	q931_get_number((unsigned char *) call->redirection_number.str, sizeof(call->redirection_number.str), ie->data + i, ie->len - i);
1817
	q931_get_number((unsigned char *) call->redirection_number.str, sizeof(call->redirection_number.str), ie->data + i, ie->len - i);
1811
	return 0;
1818
	return 0;
1812
}
1819
}
1813

    
   
1820

   
1814
static int transmit_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1821
static int transmit_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1815
{
1822
{
1816
	size_t datalen;
1823
	size_t datalen;
1817

    
   
1824

   
1818
	if (order > 1) {
1825
	if (order > 1) {
1819
		return 0;
1826
		return 0;
1820
	}
1827
	}
1821
	if (!call->redirection_number.valid) {
1828
	if (!call->redirection_number.valid) {
1822
		return 0;
1829
		return 0;
1823
	}
1830
	}
1824

    
   
1831

   
1825
	datalen = strlen(call->redirection_number.str);
1832
	datalen = strlen(call->redirection_number.str);
1826
	ie->data[0] = call->redirection_number.plan;
1833
	ie->data[0] = call->redirection_number.plan;
1827
	ie->data[1] = (call->redirection_number.presentation & PRI_PRES_RESTRICTION) | 0x80;
1834
	ie->data[1] = (call->redirection_number.presentation & PRI_PRES_RESTRICTION) | 0x80;
1828
	memcpy(ie->data + 2, call->redirection_number.str, datalen);
1835
	memcpy(ie->data + 2, call->redirection_number.str, datalen);
1829
	return datalen + (2 + 2);
1836
	return datalen + (2 + 2);
1830
}
1837
}
1831

    
   
1838

   
1832
static int receive_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1839
static int receive_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1833
{
1840
{
1834
	if (len < 3) {
1841
	if (len < 3) {
1835
		return -1;
1842
		return -1;
1836
	}
1843
	}
1837

    
   
1844

   
1838
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie, 1, len - 3);
1845
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie, 1, len - 3);
1839
}
1846
}
1840

    
   
1847

   
1841
static int transmit_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1848
static int transmit_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1842
{
1849
{
1843
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie, 1, len, order);
1850
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie, 1, len, order);
1844
}
1851
}
1845

    
   
1852

   
1846
static int receive_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1853
static int receive_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1847
{
1854
{
1848
	if (len < 3) {
1855
	if (len < 3) {
1849
		return -1;
1856
		return -1;
1850
	}
1857
	}
1851
	return receive_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1, len - 3);
1858
	return receive_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1, len - 3);
1852
}
1859
}
1853

    
   
1860

   
1854
static int transmit_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1861
static int transmit_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1855
{
1862
{
1856
	return transmit_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1, len, order);
1863
	return transmit_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1, len, order);
1857
}
1864
}
1858

    
   
1865

   
1859
static int receive_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1866
static int receive_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1860
{
1867
{
1861
	size_t called_len;
1868
	size_t called_len;
1862
	size_t max_len;
1869
	size_t max_len;
1863
	char *called_end;
1870
	char *called_end;
1864

    
   
1871

   
1865
	if (len < 3) {
1872
	if (len < 3) {
1866
		return -1;
1873
		return -1;
1867
	}
1874
	}
1868

    
   
1875

   
1869
	call->called.number.valid = 1;
1876
	call->called.number.valid = 1;
1870
	call->called.number.plan = ie->data[0] & 0x7f;
1877
	call->called.number.plan = ie->data[0] & 0x7f;
1871
	if (msgtype == Q931_SETUP) {
1878
	if (msgtype == Q931_SETUP) {
1872
		q931_get_number((unsigned char *) call->called.number.str,
1879
		q931_get_number((unsigned char *) call->called.number.str,
1873
			sizeof(call->called.number.str), ie->data + 1, len - 3);
1880
			sizeof(call->called.number.str), ie->data + 1, len - 3);
1874
	} else if (call->ourcallstate == Q931_CALL_STATE_OVERLAP_RECEIVING) {
1881
	} else if (call->ourcallstate == Q931_CALL_STATE_OVERLAP_RECEIVING) {
1875
		/*
1882
		/*
1876
		 * Since we are receiving overlap digits now, we need to append
1883
		 * Since we are receiving overlap digits now, we need to append
1877
		 * them to any previously received digits in call->called.number.str.
1884
		 * them to any previously received digits in call->called.number.str.
1878
		 */
1885
		 */
1879
		called_len = strlen(call->called.number.str);
1886
		called_len = strlen(call->called.number.str);
1880
		called_end = call->called.number.str + called_len;
1887
		called_end = call->called.number.str + called_len;
1881
		max_len = (sizeof(call->called.number.str) - 1) - called_len;
1888
		max_len = (sizeof(call->called.number.str) - 1) - called_len;
1882
		if (max_len < len - 3) {
1889
		if (max_len < len - 3) {
1883
			called_len = max_len;
1890
			called_len = max_len;
1884
		} else {
1891
		} else {
1885
			called_len = len - 3;
1892
			called_len = len - 3;
1886
		}
1893
		}
1887
		strncat(called_end, (char *) ie->data + 1, called_len);
1894
		strncat(called_end, (char *) ie->data + 1, called_len);
1888
	}
1895
	}
1889

    
   
1896

   
1890
	q931_get_number((unsigned char *) call->overlap_digits, sizeof(call->overlap_digits),
1897
	q931_get_number((unsigned char *) call->overlap_digits, sizeof(call->overlap_digits),
1891
		ie->data + 1, len - 3);
1898
		ie->data + 1, len - 3);
1892
	return 0;
1899
	return 0;
1893
}
1900
}
1894

    
   
1901

   
1895
static int transmit_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1902
static int transmit_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1896
{
1903
{
1897
	size_t datalen;
1904
	size_t datalen;
1898

    
   
1905

   
1899
	if (!call->called.number.valid) {
1906
	if (!call->called.number.valid) {
1900
		return 0;
1907
		return 0;
1901
	}
1908
	}
1902

    
   
1909

   
1903
	datalen = strlen(call->overlap_digits);
1910
	datalen = strlen(call->overlap_digits);
1904
	ie->data[0] = 0x80 | call->called.number.plan;
1911
	ie->data[0] = 0x80 | call->called.number.plan;
1905
	memcpy(ie->data + 1, call->overlap_digits, datalen);
1912
	memcpy(ie->data + 1, call->overlap_digits, datalen);
1906
	return datalen + (1 + 2);
1913
	return datalen + (1 + 2);
1907
}
1914
}
1908

    
   
1915

   
1909
static int receive_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1916
static int receive_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1910
{
1917
{
1911
	int i = 0;
1918
	int i = 0;
1912

    
   
1919

   
1913
	call->remote_id.number.valid = 1;
1920
	call->remote_id.number.valid = 1;
1914
	call->remote_id.number.presentation =
1921
	call->remote_id.number.presentation =
1915
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1922
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
1916
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1923
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
1917
	   walking through all bytes until one with ext bit (8) set to 1 */
1924
	   walking through all bytes until one with ext bit (8) set to 1 */
1918
	do {
1925
	do {
1919
		switch (i) {
1926
		switch (i) {
1920
		case 0:
1927
		case 0:
1921
			call->remote_id.number.plan = ie->data[i] & 0x7f;
1928
			call->remote_id.number.plan = ie->data[i] & 0x7f;
1922
			break;
1929
			break;
1923
		case 1:
1930
		case 1:
1924
			/* Keep only the presentation and screening fields */
1931
			/* Keep only the presentation and screening fields */
1925
			call->remote_id.number.presentation =
1932
			call->remote_id.number.presentation =
1926
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1933
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
1927
			break;
1934
			break;
1928
		}
1935
		}
1929
	} while (!(ie->data[i++] & 0x80));
1936
	} while (!(ie->data[i++] & 0x80));
1930
	q931_get_number((unsigned char *) call->remote_id.number.str,
1937
	q931_get_number((unsigned char *) call->remote_id.number.str,
1931
		sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);
1938
		sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);
1932

    
   
1939

   
1933
	return 0;
1940
	return 0;
1934
}
1941
}
1935

    
   
1942

   
1936
static int transmit_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1943
static int transmit_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1937
{
1944
{
1938
	size_t datalen;
1945
	size_t datalen;
1939

    
   
1946

   
1940
	if (!call->local_id.number.valid) {
1947
	if (!call->local_id.number.valid) {
1941
		return 0;
1948
		return 0;
1942
	}
1949
	}
1943

    
   
1950

   
1944
	datalen = strlen(call->local_id.number.str);
1951
	datalen = strlen(call->local_id.number.str);
1945
	ie->data[0] = call->local_id.number.plan;
1952
	ie->data[0] = call->local_id.number.plan;
1946
	ie->data[1] = 0x80 | call->local_id.number.presentation;
1953
	ie->data[1] = 0x80 | call->local_id.number.presentation;
1947
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
1954
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
1948
	return datalen + (2 + 2);
1955
	return datalen + (2 + 2);
1949
}
1956
}
1950

    
   
1957

   
1951
static void dump_user_user(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1958
static void dump_user_user(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1952
{
1959
{
1953
	int x;
1960
	int x;
1954
	pri_message(ctrl, "%c User-User Information (len=%2d) [", prefix, len);
1961
	pri_message(ctrl, "%c User-User Information (len=%2d) [", prefix, len);
1955
	for (x=0;x<ie->len;x++)
1962
	for (x=0;x<ie->len;x++)
1956
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
1963
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
1957
	pri_message(ctrl, " ]\n");
1964
	pri_message(ctrl, " ]\n");
1958
}
1965
}
1959

    
   
1966

   
1960

    
   
1967

   
1961
static int receive_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1968
static int receive_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1962
{        
1969
{        
1963
        call->useruserprotocoldisc = ie->data[0] & 0xff;
1970
        call->useruserprotocoldisc = ie->data[0] & 0xff;
1964
        if (call->useruserprotocoldisc == 4) /* IA5 */
1971
        if (call->useruserprotocoldisc == 4) /* IA5 */
1965
          q931_get_number((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
1972
          q931_get_number((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
1966
	return 0;
1973
	return 0;
1967
}
1974
}
1968

    
   
1975

   
1969
static int transmit_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1976
static int transmit_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1970
{        
1977
{        
1971
	int datalen = strlen(call->useruserinfo);
1978
	int datalen = strlen(call->useruserinfo);
1972
	if (datalen > 0) {
1979
	if (datalen > 0) {
1973
		/* Restricted to 35 characters */
1980
		/* Restricted to 35 characters */
1974
		if (msgtype == Q931_USER_INFORMATION) {
1981
		if (msgtype == Q931_USER_INFORMATION) {
1975
			if (datalen > 260)
1982
			if (datalen > 260)
1976
				datalen = 260;
1983
				datalen = 260;
1977
		} else {
1984
		} else {
1978
			if (datalen > 35)
1985
			if (datalen > 35)
1979
				datalen = 35;
1986
				datalen = 35;
1980
		}
1987
		}
1981
		ie->data[0] = 4; /* IA5 characters */
1988
		ie->data[0] = 4; /* IA5 characters */
1982
		memcpy(&ie->data[1], call->useruserinfo, datalen);
1989
		memcpy(&ie->data[1], call->useruserinfo, datalen);
1983
		call->useruserinfo[0] = '\0';
1990
		call->useruserinfo[0] = '\0';
1984
		return datalen + 3;
1991
		return datalen + 3;
1985
	}
1992
	}
1986

    
   
1993

   
1987
	return 0;
1994
	return 0;
1988
}
1995
}
1989

    
   
1996

   
1990
static void dump_change_status(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1997
static void dump_change_status(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1991
{
1998
{
1992
	int x;
1999
	int x;
1993
	
2000
	
1994
	pri_message(ctrl, "%c Change Status Information (len=%2d) [", prefix, len);
2001
	pri_message(ctrl, "%c Change Status Information (len=%2d) [", prefix, len);
1995
	for (x=0; x<ie->len; x++) {
2002
	for (x=0; x<ie->len; x++) {
1996
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2003
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
1997
	}
2004
	}
1998
	pri_message(ctrl, " ]\n");
2005
	pri_message(ctrl, " ]\n");
1999
}
2006
}
2000

    
   
2007

   
2001
static int receive_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2008
static int receive_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2002
{
2009
{
2003
	call->changestatus = ie->data[0] & 0x0f;
2010
	call->changestatus = ie->data[0] & 0x0f;
2004
	return 0;
2011
	return 0;
2005
}
2012
}
2006

    
   
2013

   
2007
static int transmit_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2014
static int transmit_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2008
{
2015
{
2009
	ie->data[0] = 0xc0 | call->changestatus;
2016
	ie->data[0] = 0xc0 | call->changestatus;
2010
	return 3;
2017
	return 3;
2011
}
2018
}
2012

    
   
2019

   
2013
static char *prog2str(int prog)
2020
static char *prog2str(int prog)
2014
{
2021
{
2015
	static struct msgtype progs[] = {
2022
	static struct msgtype progs[] = {
2016
		{ Q931_PROG_CALL_NOT_E2E_ISDN, "Call is not end-to-end ISDN; further call progress information may be available inband." },
2023
		{ Q931_PROG_CALL_NOT_E2E_ISDN, "Call is not end-to-end ISDN; further call progress information may be available inband." },
2017
		{ <