Review Board 1.7.16


use ie2str(full_ie) where possible in q931.c

Review Request #1633 - Created Dec. 18, 2011 and submitted

Alec Davis
1.4
Reviewers
asterisk-dev
rmudgett
LibPRI
compiles

Only 'dump_channel_id' left is.
basic calls.
adjust some output formatting as suggested.

Diff revision 1

This is not the most recent revision of the diff. The latest diff is revision 3. See what's changed.

1 2 3
1 2 3

  1. branches/1.4/q931.c: Loading...
branches/1.4/q931.c
Revision 2279 New Change
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_facility.h"
33
#include "pri_facility.h"
34

    
   
34

   
35
#include <unistd.h>
35
#include <unistd.h>
36
#include <stdlib.h>
36
#include <stdlib.h>
37
#include <time.h>
37
#include <time.h>
38
#include <string.h>
38
#include <string.h>
39
#include <ctype.h>
39
#include <ctype.h>
40
#include <stdio.h>
40
#include <stdio.h>
41
#include <limits.h>
41
#include <limits.h>
42

    
   
42

   
43
#define MAX_MAND_IES 10
43
#define MAX_MAND_IES 10
44

    
   
44

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

    
   
50

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

    
   
67

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

    
   
78

   
79
	/* Call Management */
79
	/* Call Management */
80
	{ Q931_HOLD, "HOLD" },
80
	{ Q931_HOLD, "HOLD" },
81
	{ Q931_HOLD_ACKNOWLEDGE, "HOLD ACKNOWLEDGE" },
81
	{ Q931_HOLD_ACKNOWLEDGE, "HOLD ACKNOWLEDGE" },
82
	{ Q931_HOLD_REJECT, "HOLD REJECT", { Q931_CAUSE } },
82
	{ Q931_HOLD_REJECT, "HOLD REJECT", { Q931_CAUSE } },
83
	{ Q931_RETRIEVE, "RETRIEVE" },
83
	{ Q931_RETRIEVE, "RETRIEVE" },
84
	{ Q931_RETRIEVE_ACKNOWLEDGE, "RETRIEVE ACKNOWLEDGE" },
84
	{ Q931_RETRIEVE_ACKNOWLEDGE, "RETRIEVE ACKNOWLEDGE" },
85
	{ Q931_RETRIEVE_REJECT, "RETRIEVE REJECT", { Q931_CAUSE } },
85
	{ Q931_RETRIEVE_REJECT, "RETRIEVE REJECT", { Q931_CAUSE } },
86
	{ Q931_RESUME, "RESUME" },
86
	{ Q931_RESUME, "RESUME" },
87
	{ Q931_RESUME_ACKNOWLEDGE, "RESUME ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
87
	{ Q931_RESUME_ACKNOWLEDGE, "RESUME ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
88
	{ Q931_RESUME_REJECT, "RESUME REJECT", { Q931_CAUSE } },
88
	{ Q931_RESUME_REJECT, "RESUME REJECT", { Q931_CAUSE } },
89
	{ Q931_SUSPEND, "SUSPEND" },
89
	{ Q931_SUSPEND, "SUSPEND" },
90
	{ Q931_SUSPEND_ACKNOWLEDGE, "SUSPEND ACKNOWLEDGE" },
90
	{ Q931_SUSPEND_ACKNOWLEDGE, "SUSPEND ACKNOWLEDGE" },
91
	{ Q931_SUSPEND_REJECT, "SUSPEND REJECT" },
91
	{ Q931_SUSPEND_REJECT, "SUSPEND REJECT" },
92

    
   
92

   
93
	{ Q931_ANY_MESSAGE, "ANY MESSAGE" },
93
	{ Q931_ANY_MESSAGE, "ANY MESSAGE" },
94
};
94
};
95

    
   
95

   
96
static int post_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, int missingmand);
96
static int post_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, int missingmand);
97
static void nt_ptmp_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, int *allow_event, int *allow_posthandle);
97
static void nt_ptmp_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, int *allow_event, int *allow_posthandle);
98

    
   
98

   
99
struct msgtype att_maintenance_msgs[] = {
99
struct msgtype att_maintenance_msgs[] = {
100
	{ ATT_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
100
	{ ATT_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
101
	{ ATT_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
101
	{ ATT_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
102
};
102
};
103

    
   
103

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

    
   
109

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

    
   
160

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

    
   
179

   
180
#define FLAG_WHOLE_INTERFACE	0x01
180
#define FLAG_WHOLE_INTERFACE	0x01
181
#define FLAG_PREFERRED			0x02
181
#define FLAG_PREFERRED			0x02
182
#define FLAG_EXCLUSIVE			0x04
182
#define FLAG_EXCLUSIVE			0x04
183

    
   
183

   
184
#define RESET_INDICATOR_CHANNEL	0
184
#define RESET_INDICATOR_CHANNEL	0
185
#define RESET_INDICATOR_DS1		6
185
#define RESET_INDICATOR_DS1		6
186
#define RESET_INDICATOR_PRI		7
186
#define RESET_INDICATOR_PRI		7
187

    
   
187

   
188
#define TRANS_MODE_64_CIRCUIT	0x10
188
#define TRANS_MODE_64_CIRCUIT	0x10
189
#define TRANS_MODE_2x64_CIRCUIT	0x11
189
#define TRANS_MODE_2x64_CIRCUIT	0x11
190
#define TRANS_MODE_384_CIRCUIT	0x13
190
#define TRANS_MODE_384_CIRCUIT	0x13
191
#define TRANS_MODE_1536_CIRCUIT	0x15
191
#define TRANS_MODE_1536_CIRCUIT	0x15
192
#define TRANS_MODE_1920_CIRCUIT	0x17
192
#define TRANS_MODE_1920_CIRCUIT	0x17
193
#define TRANS_MODE_MULTIRATE	0x18
193
#define TRANS_MODE_MULTIRATE	0x18
194
#define TRANS_MODE_PACKET		0x40
194
#define TRANS_MODE_PACKET		0x40
195

    
   
195

   
196
#define RATE_ADAPT_56K			0x0f
196
#define RATE_ADAPT_56K			0x0f
197

    
   
197

   
198
#define LAYER_2_LAPB			0x46
198
#define LAYER_2_LAPB			0x46
199

    
   
199

   
200
#define LAYER_3_X25				0x66
200
#define LAYER_3_X25				0x66
201

    
   
201

   
202
/* The 4ESS uses a different audio field */
202
/* The 4ESS uses a different audio field */
203
#define PRI_TRANS_CAP_AUDIO_4ESS	0x08
203
#define PRI_TRANS_CAP_AUDIO_4ESS	0x08
204

    
   
204

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

    
   
216

   
217
#define CODE_CCITT					0x0
217
#define CODE_CCITT					0x0
218
#define CODE_INTERNATIONAL 			0x1
218
#define CODE_INTERNATIONAL 			0x1
219
#define CODE_NATIONAL 				0x2
219
#define CODE_NATIONAL 				0x2
220
#define CODE_NETWORK_SPECIFIC		0x3
220
#define CODE_NETWORK_SPECIFIC		0x3
221

    
   
221

   
222
#define LOC_USER					0x0
222
#define LOC_USER					0x0
223
#define LOC_PRIV_NET_LOCAL_USER		0x1
223
#define LOC_PRIV_NET_LOCAL_USER		0x1
224
#define LOC_PUB_NET_LOCAL_USER		0x2
224
#define LOC_PUB_NET_LOCAL_USER		0x2
225
#define LOC_TRANSIT_NET				0x3
225
#define LOC_TRANSIT_NET				0x3
226
#define LOC_PUB_NET_REMOTE_USER		0x4
226
#define LOC_PUB_NET_REMOTE_USER		0x4
227
#define LOC_PRIV_NET_REMOTE_USER	0x5
227
#define LOC_PRIV_NET_REMOTE_USER	0x5
228
#define LOC_INTERNATIONAL_NETWORK	0x7
228
#define LOC_INTERNATIONAL_NETWORK	0x7
229
#define LOC_NETWORK_BEYOND_INTERWORKING	0xa
229
#define LOC_NETWORK_BEYOND_INTERWORKING	0xa
230

    
   
230

   
231
static char *ie2str(int ie);
231
static char *ie2str(int ie);
232

    
   
232

   
233

    
   
233

   
234
#define FUNC_DUMP(name) void (name)(int full_ie, struct pri *pri, q931_ie *ie, int len, char prefix)
234
#define FUNC_DUMP(name) void (name)(int full_ie, struct pri *pri, q931_ie *ie, int len, char prefix)
235
#define FUNC_RECV(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len)
235
#define FUNC_RECV(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len)
236
#define FUNC_SEND(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
236
#define FUNC_SEND(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
237

    
   
237

   
238
#if 1
238
#if 1
239
/* Update call state with transition trace. */
239
/* Update call state with transition trace. */
240
#define UPDATE_OURCALLSTATE(ctrl, call, newstate) \
240
#define UPDATE_OURCALLSTATE(ctrl, call, newstate) \
241
	do { \
241
	do { \
242
		if (((ctrl)->debug & PRI_DEBUG_Q931_STATE) && (call)->ourcallstate != (newstate)) { \
242
		if (((ctrl)->debug & PRI_DEBUG_Q931_STATE) && (call)->ourcallstate != (newstate)) { \
243
			pri_message((ctrl), \
243
			pri_message((ctrl), \
244
				DBGHEAD "%s %d enters state %d (%s).  Hold state: %s\n", \
244
				DBGHEAD "%s %d enters state %d (%s).  Hold state: %s\n", \
245
				DBGINFO, ((call) == (call)->master_call) ? "Call" : "Subcall", \
245
				DBGINFO, ((call) == (call)->master_call) ? "Call" : "Subcall", \
246
				(call)->cr, (newstate), q931_call_state_str(newstate), \
246
				(call)->cr, (newstate), q931_call_state_str(newstate), \
247
				q931_hold_state_str((call)->master_call->hold_state)); \
247
				q931_hold_state_str((call)->master_call->hold_state)); \
248
		} \
248
		} \
249
		(call)->ourcallstate = (newstate); \
249
		(call)->ourcallstate = (newstate); \
250
	} while (0)
250
	} while (0)
251
#else
251
#else
252
/* Update call state with no trace. */
252
/* Update call state with no trace. */
253
#define UPDATE_OURCALLSTATE(ctrl, call, newstate) (call)->ourcallstate = (newstate)
253
#define UPDATE_OURCALLSTATE(ctrl, call, newstate) (call)->ourcallstate = (newstate)
254
#endif
254
#endif
255

    
   
255

   
256
#if 1
256
#if 1
257
/* Update hold state with transition trace. */
257
/* Update hold state with transition trace. */
258
#define UPDATE_HOLD_STATE(ctrl, master_call, newstate) \
258
#define UPDATE_HOLD_STATE(ctrl, master_call, newstate) \
259
	do { \
259
	do { \
260
		if (((ctrl)->debug & PRI_DEBUG_Q931_STATE) \
260
		if (((ctrl)->debug & PRI_DEBUG_Q931_STATE) \
261
			&& (master_call)->hold_state != (newstate)) { \
261
			&& (master_call)->hold_state != (newstate)) { \
262
			pri_message((ctrl), \
262
			pri_message((ctrl), \
263
				DBGHEAD "Call %d in state %d (%s) enters Hold state: %s\n", \
263
				DBGHEAD "Call %d in state %d (%s) enters Hold state: %s\n", \
264
				DBGINFO, (master_call)->cr, (master_call)->ourcallstate, \
264
				DBGINFO, (master_call)->cr, (master_call)->ourcallstate, \
265
				q931_call_state_str((master_call)->ourcallstate), \
265
				q931_call_state_str((master_call)->ourcallstate), \
266
				q931_hold_state_str(newstate)); \
266
				q931_hold_state_str(newstate)); \
267
		} \
267
		} \
268
		(master_call)->hold_state = (newstate); \
268
		(master_call)->hold_state = (newstate); \
269
	} while (0)
269
	} while (0)
270
#else
270
#else
271
/* Update hold state with no trace. */
271
/* Update hold state with no trace. */
272
#define UPDATE_HOLD_STATE(ctrl, master_call, newstate) (master_call)->hold_state = (newstate)
272
#define UPDATE_HOLD_STATE(ctrl, master_call, newstate) (master_call)->hold_state = (newstate)
273
#endif
273
#endif
274

    
   
274

   
275
struct ie {
275
struct ie {
276
	/* Maximal count of same IEs at the message (0 - any, 1..n - limited) */
276
	/* Maximal count of same IEs at the message (0 - any, 1..n - limited) */
277
	int max_count;
277
	int max_count;
278
	/* IE code */
278
	/* IE code */
279
	int ie;
279
	int ie;
280
	/* IE friendly name */
280
	/* IE friendly name */
281
	char *name;
281
	char *name;
282
	/* Dump an IE for debugging (preceed all lines by prefix) */
282
	/* Dump an IE for debugging (preceed all lines by prefix) */
283
	FUNC_DUMP(*dump);
283
	FUNC_DUMP(*dump);
284
	/* Handle IE  returns 0 on success, -1 on failure */
284
	/* Handle IE  returns 0 on success, -1 on failure */
285
	FUNC_RECV(*receive);
285
	FUNC_RECV(*receive);
286
	/* Add IE to a message, return the # of bytes added or -1 on failure */
286
	/* Add IE to a message, return the # of bytes added or -1 on failure */
287
	FUNC_SEND(*transmit);
287
	FUNC_SEND(*transmit);
288
};
288
};
289

    
   
289

   
290
/*!
290
/*!
291
 * \internal
291
 * \internal
292
 * \brief Encode the channel id information to pass to upper level.
292
 * \brief Encode the channel id information to pass to upper level.
293
 *
293
 *
294
 * \param call Q.931 call leg
294
 * \param call Q.931 call leg
295
 *
295
 *
296
 * \return Encoded channel value.
296
 * \return Encoded channel value.
297
 */
297
 */
298
static int q931_encode_channel(const q931_call *call)
298
static int q931_encode_channel(const q931_call *call)
299
{
299
{
300
	int held_call;
300
	int held_call;
301
	int channelno;
301
	int channelno;
302
	int ds1no;
302
	int ds1no;
303

    
   
303

   
304
	switch (call->master_call->hold_state) {
304
	switch (call->master_call->hold_state) {
305
	case Q931_HOLD_STATE_CALL_HELD:
305
	case Q931_HOLD_STATE_CALL_HELD:
306
	case Q931_HOLD_STATE_RETRIEVE_REQ:
306
	case Q931_HOLD_STATE_RETRIEVE_REQ:
307
	case Q931_HOLD_STATE_RETRIEVE_IND:
307
	case Q931_HOLD_STATE_RETRIEVE_IND:
308
		held_call = 1 << 18;
308
		held_call = 1 << 18;
309
		break;
309
		break;
310
	default:
310
	default:
311
		held_call = 0;
311
		held_call = 0;
312
		break;
312
		break;
313
	}
313
	}
314
	if (held_call || call->cis_call) {
314
	if (held_call || call->cis_call) {
315
		/* So a -1 does not wipe out the held_call or cis_call flags. */
315
		/* So a -1 does not wipe out the held_call or cis_call flags. */
316
		channelno = call->channelno & 0xFF;
316
		channelno = call->channelno & 0xFF;
317
		ds1no = call->ds1no & 0xFF;
317
		ds1no = call->ds1no & 0xFF;
318
	} else {
318
	} else {
319
		channelno = call->channelno;
319
		channelno = call->channelno;
320
		ds1no = call->ds1no;
320
		ds1no = call->ds1no;
321
	}
321
	}
322
	return channelno | (ds1no << 8) | (call->ds1explicit << 16) | (call->cis_call << 17)
322
	return channelno | (ds1no << 8) | (call->ds1explicit << 16) | (call->cis_call << 17)
323
		| held_call;
323
		| held_call;
324
}
324
}
325

    
   
325

   
326
/*!
326
/*!
327
 * \brief Check if the given call ptr is valid.
327
 * \brief Check if the given call ptr is valid.
328
 *
328
 *
329
 * \param ctrl D channel controller.
329
 * \param ctrl D channel controller.
330
 * \param call Q.931 call leg.
330
 * \param call Q.931 call leg.
331
 *
331
 *
332
 * \retval TRUE if call ptr is valid.
332
 * \retval TRUE if call ptr is valid.
333
 * \retval FALSE if call ptr is invalid.
333
 * \retval FALSE if call ptr is invalid.
334
 */
334
 */
335
int q931_is_call_valid(struct pri *ctrl, struct q931_call *call)
335
int q931_is_call_valid(struct pri *ctrl, struct q931_call *call)
336
{
336
{
337
	struct q931_call *cur;
337
	struct q931_call *cur;
338
	struct q921_link *link;
338
	struct q921_link *link;
339
	int idx;
339
	int idx;
340

    
   
340

   
341
	if (!call) {
341
	if (!call) {
342
		return 0;
342
		return 0;
343
	}
343
	}
344

    
   
344

   
345
	if (!ctrl) {
345
	if (!ctrl) {
346
		/* Must use suspect ctrl from call ptr. */
346
		/* Must use suspect ctrl from call ptr. */
347
		if (!call->pri) {
347
		if (!call->pri) {
348
			/* Definitely a bad call pointer. */
348
			/* Definitely a bad call pointer. */
349
			return 0;
349
			return 0;
350
		}
350
		}
351
		ctrl = call->pri;
351
		ctrl = call->pri;
352
	}
352
	}
353

    
   
353

   
354
	/* Check real call records. */
354
	/* Check real call records. */
355
	for (cur = *ctrl->callpool; cur; cur = cur->next) {
355
	for (cur = *ctrl->callpool; cur; cur = cur->next) {
356
		if (call == cur) {
356
		if (call == cur) {
357
			/* Found it. */
357
			/* Found it. */
358
			return 1;
358
			return 1;
359
		}
359
		}
360
		if (cur->outboundbroadcast) {
360
		if (cur->outboundbroadcast) {
361
			/* Check subcalls for call ptr. */
361
			/* Check subcalls for call ptr. */
362
			for (idx = 0; idx < ARRAY_LEN(cur->subcalls); ++idx) {
362
			for (idx = 0; idx < ARRAY_LEN(cur->subcalls); ++idx) {
363
				if (call == cur->subcalls[idx]) {
363
				if (call == cur->subcalls[idx]) {
364
					/* Found it. */
364
					/* Found it. */
365
					return 1;
365
					return 1;
366
				}
366
				}
367
			}
367
			}
368
		}
368
		}
369
	}
369
	}
370

    
   
370

   
371
	/* Check dummy call records. */
371
	/* Check dummy call records. */
372
	for (link = &ctrl->link; link; link = link->next) {
372
	for (link = &ctrl->link; link; link = link->next) {
373
		if (link->dummy_call == call) {
373
		if (link->dummy_call == call) {
374
			/* Found it. */
374
			/* Found it. */
375
			return 1;
375
			return 1;
376
		}
376
		}
377
	}
377
	}
378

    
   
378

   
379
	/* Well it looks like this is a stale call ptr. */
379
	/* Well it looks like this is a stale call ptr. */
380
	return 0;
380
	return 0;
381
}
381
}
382

    
   
382

   
383
/*!
383
/*!
384
 * \brief Check if the given call ptr is valid and gripe if not.
384
 * \brief Check if the given call ptr is valid and gripe if not.
385
 *
385
 *
386
 * \param ctrl D channel controller.
386
 * \param ctrl D channel controller.
387
 * \param call Q.931 call leg.
387
 * \param call Q.931 call leg.
388
 * \param func_name Calling function name for debug tracing. (__PRETTY_FUNCTION__)
388
 * \param func_name Calling function name for debug tracing. (__PRETTY_FUNCTION__)
389
 * \param func_line Calling function line number for debug tracing. (__LINE__)
389
 * \param func_line Calling function line number for debug tracing. (__LINE__)
390
 *
390
 *
391
 * \retval TRUE if call ptr is valid.
391
 * \retval TRUE if call ptr is valid.
392
 * \retval FALSE if call ptr is invalid.
392
 * \retval FALSE if call ptr is invalid.
393
 */
393
 */
394
int q931_is_call_valid_gripe(struct pri *ctrl, struct q931_call *call, const char *func_name, unsigned long func_line)
394
int q931_is_call_valid_gripe(struct pri *ctrl, struct q931_call *call, const char *func_name, unsigned long func_line)
395
{
395
{
396
	int res;
396
	int res;
397

    
   
397

   
398
	if (!call) {
398
	if (!call) {
399
		/* Let's not gripe about this invalid call pointer. */
399
		/* Let's not gripe about this invalid call pointer. */
400
		return 0;
400
		return 0;
401
	}
401
	}
402
	res = q931_is_call_valid(ctrl, call);
402
	res = q931_is_call_valid(ctrl, call);
403
	if (!res) {
403
	if (!res) {
404
		pri_message(ctrl, "!! %s() line:%lu Called with invalid call ptr (%p)\n",
404
		pri_message(ctrl, "!! %s() line:%lu Called with invalid call ptr (%p)\n",
405
			func_name, func_line, call);
405
			func_name, func_line, call);
406
	}
406
	}
407
	return res;
407
	return res;
408
}
408
}
409

    
   
409

   
410
/*!
410
/*!
411
 * \brief Initialize the given struct q931_party_name
411
 * \brief Initialize the given struct q931_party_name
412
 *
412
 *
413
 * \param name Structure to initialize
413
 * \param name Structure to initialize
414
 *
414
 *
415
 * \return Nothing
415
 * \return Nothing
416
 */
416
 */
417
void q931_party_name_init(struct q931_party_name *name)
417
void q931_party_name_init(struct q931_party_name *name)
418
{
418
{
419
	name->valid = 0;
419
	name->valid = 0;
420
	name->presentation = PRI_PRES_UNAVAILABLE;
420
	name->presentation = PRI_PRES_UNAVAILABLE;
421
	name->char_set = PRI_CHAR_SET_ISO8859_1;
421
	name->char_set = PRI_CHAR_SET_ISO8859_1;
422
	name->str[0] = '\0';
422
	name->str[0] = '\0';
423
}
423
}
424

    
   
424

   
425
/*!
425
/*!
426
 * \brief Initialize the given struct q931_party_number
426
 * \brief Initialize the given struct q931_party_number
427
 *
427
 *
428
 * \param number Structure to initialize
428
 * \param number Structure to initialize
429
 *
429
 *
430
 * \return Nothing
430
 * \return Nothing
431
 */
431
 */
432
void q931_party_number_init(struct q931_party_number *number)
432
void q931_party_number_init(struct q931_party_number *number)
433
{
433
{
434
	number->valid = 0;
434
	number->valid = 0;
435
	number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
435
	number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
436
	number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
436
	number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
437
	number->str[0] = '\0';
437
	number->str[0] = '\0';
438
}
438
}
439

    
   
439

   
440
/*!
440
/*!
441
 * \brief Initialize the given struct q931_party_subaddress
441
 * \brief Initialize the given struct q931_party_subaddress
442
 *
442
 *
443
 * \param subaddress Structure to initialize
443
 * \param subaddress Structure to initialize
444
 *
444
 *
445
 * \return Nothing
445
 * \return Nothing
446
 */
446
 */
447
void q931_party_subaddress_init(struct q931_party_subaddress *subaddress)
447
void q931_party_subaddress_init(struct q931_party_subaddress *subaddress)
448
{
448
{
449
	subaddress->valid = 0;
449
	subaddress->valid = 0;
450
	subaddress->type = 0;
450
	subaddress->type = 0;
451
	subaddress->odd_even_indicator = 0;
451
	subaddress->odd_even_indicator = 0;
452
	subaddress->length = 0;
452
	subaddress->length = 0;
453
	subaddress->data[0] = '\0';
453
	subaddress->data[0] = '\0';
454
}
454
}
455

    
   
455

   
456
/*!
456
/*!
457
 * \brief Initialize the given struct q931_party_address
457
 * \brief Initialize the given struct q931_party_address
458
 *
458
 *
459
 * \param address Structure to initialize
459
 * \param address Structure to initialize
460
 *
460
 *
461
 * \return Nothing
461
 * \return Nothing
462
 */
462
 */
463
void q931_party_address_init(struct q931_party_address *address)
463
void q931_party_address_init(struct q931_party_address *address)
464
{
464
{
465
	q931_party_number_init(&address->number);
465
	q931_party_number_init(&address->number);
466
	q931_party_subaddress_init(&address->subaddress);
466
	q931_party_subaddress_init(&address->subaddress);
467
}
467
}
468

    
   
468

   
469
/*!
469
/*!
470
 * \brief Initialize the given struct q931_party_id
470
 * \brief Initialize the given struct q931_party_id
471
 *
471
 *
472
 * \param id Structure to initialize
472
 * \param id Structure to initialize
473
 *
473
 *
474
 * \return Nothing
474
 * \return Nothing
475
 */
475
 */
476
void q931_party_id_init(struct q931_party_id *id)
476
void q931_party_id_init(struct q931_party_id *id)
477
{
477
{
478
	q931_party_name_init(&id->name);
478
	q931_party_name_init(&id->name);
479
	q931_party_number_init(&id->number);
479
	q931_party_number_init(&id->number);
480
	q931_party_subaddress_init(&id->subaddress);
480
	q931_party_subaddress_init(&id->subaddress);
481
}
481
}
482

    
   
482

   
483
/*!
483
/*!
484
 * \brief Initialize the given struct q931_party_redirecting
484
 * \brief Initialize the given struct q931_party_redirecting
485
 *
485
 *
486
 * \param redirecting Structure to initialize
486
 * \param redirecting Structure to initialize
487
 *
487
 *
488
 * \return Nothing
488
 * \return Nothing
489
 */
489
 */
490
void q931_party_redirecting_init(struct q931_party_redirecting *redirecting)
490
void q931_party_redirecting_init(struct q931_party_redirecting *redirecting)
491
{
491
{
492
	q931_party_id_init(&redirecting->from);
492
	q931_party_id_init(&redirecting->from);
493
	q931_party_id_init(&redirecting->to);
493
	q931_party_id_init(&redirecting->to);
494
	q931_party_id_init(&redirecting->orig_called);
494
	q931_party_id_init(&redirecting->orig_called);
495
	redirecting->state = Q931_REDIRECTING_STATE_IDLE;
495
	redirecting->state = Q931_REDIRECTING_STATE_IDLE;
496
	redirecting->count = 0;
496
	redirecting->count = 0;
497
	redirecting->orig_reason = PRI_REDIR_UNKNOWN;
497
	redirecting->orig_reason = PRI_REDIR_UNKNOWN;
498
	redirecting->reason = PRI_REDIR_UNKNOWN;
498
	redirecting->reason = PRI_REDIR_UNKNOWN;
499
}
499
}
500

    
   
500

   
501
/*!
501
/*!
502
 * \brief Compare the left and right party name.
502
 * \brief Compare the left and right party name.
503
 *
503
 *
504
 * \param left Left parameter party name.
504
 * \param left Left parameter party name.
505
 * \param right Right parameter party name.
505
 * \param right Right parameter party name.
506
 *
506
 *
507
 * \retval < 0 when left < right.
507
 * \retval < 0 when left < right.
508
 * \retval == 0 when left == right.
508
 * \retval == 0 when left == right.
509
 * \retval > 0 when left > right.
509
 * \retval > 0 when left > right.
510
 */
510
 */
511
int q931_party_name_cmp(const struct q931_party_name *left, const struct q931_party_name *right)
511
int q931_party_name_cmp(const struct q931_party_name *left, const struct q931_party_name *right)
512
{
512
{
513
	int cmp;
513
	int cmp;
514

    
   
514

   
515
	if (!left->valid) {
515
	if (!left->valid) {
516
		if (!right->valid) {
516
		if (!right->valid) {
517
			return 0;
517
			return 0;
518
		}
518
		}
519
		return -1;
519
		return -1;
520
	} else if (!right->valid) {
520
	} else if (!right->valid) {
521
		return 1;
521
		return 1;
522
	}
522
	}
523
	cmp = left->char_set - right->char_set;
523
	cmp = left->char_set - right->char_set;
524
	if (cmp) {
524
	if (cmp) {
525
		return cmp;
525
		return cmp;
526
	}
526
	}
527
	cmp = strcmp(left->str, right->str);
527
	cmp = strcmp(left->str, right->str);
528
	if (cmp) {
528
	if (cmp) {
529
		return cmp;
529
		return cmp;
530
	}
530
	}
531
	cmp = left->presentation - right->presentation;
531
	cmp = left->presentation - right->presentation;
532
	return cmp;
532
	return cmp;
533
}
533
}
534

    
   
534

   
535
/*!
535
/*!
536
 * \brief Compare the left and right party number.
536
 * \brief Compare the left and right party number.
537
 *
537
 *
538
 * \param left Left parameter party number.
538
 * \param left Left parameter party number.
539
 * \param right Right parameter party number.
539
 * \param right Right parameter party number.
540
 *
540
 *
541
 * \retval < 0 when left < right.
541
 * \retval < 0 when left < right.
542
 * \retval == 0 when left == right.
542
 * \retval == 0 when left == right.
543
 * \retval > 0 when left > right.
543
 * \retval > 0 when left > right.
544
 */
544
 */
545
int q931_party_number_cmp(const struct q931_party_number *left, const struct q931_party_number *right)
545
int q931_party_number_cmp(const struct q931_party_number *left, const struct q931_party_number *right)
546
{
546
{
547
	int cmp;
547
	int cmp;
548

    
   
548

   
549
	if (!left->valid) {
549
	if (!left->valid) {
550
		if (!right->valid) {
550
		if (!right->valid) {
551
			return 0;
551
			return 0;
552
		}
552
		}
553
		return -1;
553
		return -1;
554
	} else if (!right->valid) {
554
	} else if (!right->valid) {
555
		return 1;
555
		return 1;
556
	}
556
	}
557
	cmp = left->plan - right->plan;
557
	cmp = left->plan - right->plan;
558
	if (cmp) {
558
	if (cmp) {
559
		return cmp;
559
		return cmp;
560
	}
560
	}
561
	cmp = strcmp(left->str, right->str);
561
	cmp = strcmp(left->str, right->str);
562
	if (cmp) {
562
	if (cmp) {
563
		return cmp;
563
		return cmp;
564
	}
564
	}
565
	cmp = left->presentation - right->presentation;
565
	cmp = left->presentation - right->presentation;
566
	return cmp;
566
	return cmp;
567
}
567
}
568

    
   
568

   
569
/*!
569
/*!
570
 * \brief Compare the left and right party subaddress.
570
 * \brief Compare the left and right party subaddress.
571
 *
571
 *
572
 * \param left Left parameter party subaddress.
572
 * \param left Left parameter party subaddress.
573
 * \param right Right parameter party subaddress.
573
 * \param right Right parameter party subaddress.
574
 *
574
 *
575
 * \retval < 0 when left < right.
575
 * \retval < 0 when left < right.
576
 * \retval == 0 when left == right.
576
 * \retval == 0 when left == right.
577
 * \retval > 0 when left > right.
577
 * \retval > 0 when left > right.
578
 */
578
 */
579
int q931_party_subaddress_cmp(const struct q931_party_subaddress *left, const struct q931_party_subaddress *right)
579
int q931_party_subaddress_cmp(const struct q931_party_subaddress *left, const struct q931_party_subaddress *right)
580
{
580
{
581
	int cmp;
581
	int cmp;
582

    
   
582

   
583
	if (!left->valid) {
583
	if (!left->valid) {
584
		if (!right->valid) {
584
		if (!right->valid) {
585
			return 0;
585
			return 0;
586
		}
586
		}
587
		return -1;
587
		return -1;
588
	} else if (!right->valid) {
588
	} else if (!right->valid) {
589
		return 1;
589
		return 1;
590
	}
590
	}
591
	cmp = left->type - right->type;
591
	cmp = left->type - right->type;
592
	if (cmp) {
592
	if (cmp) {
593
		return cmp;
593
		return cmp;
594
	}
594
	}
595
	cmp = memcmp(left->data, right->data,
595
	cmp = memcmp(left->data, right->data,
596
		(left->length < right->length) ? left->length : right->length);
596
		(left->length < right->length) ? left->length : right->length);
597
	if (cmp) {
597
	if (cmp) {
598
		return cmp;
598
		return cmp;
599
	}
599
	}
600
	cmp = left->length - right->length;
600
	cmp = left->length - right->length;
601
	if (cmp) {
601
	if (cmp) {
602
		return cmp;
602
		return cmp;
603
	}
603
	}
604
	cmp = left->odd_even_indicator - right->odd_even_indicator;
604
	cmp = left->odd_even_indicator - right->odd_even_indicator;
605
	return cmp;
605
	return cmp;
606
}
606
}
607

    
   
607

   
608
/*!
608
/*!
609
 * \brief Compare the left and right party address.
609
 * \brief Compare the left and right party address.
610
 *
610
 *
611
 * \param left Left parameter party address.
611
 * \param left Left parameter party address.
612
 * \param right Right parameter party address.
612
 * \param right Right parameter party address.
613
 *
613
 *
614
 * \retval < 0 when left < right.
614
 * \retval < 0 when left < right.
615
 * \retval == 0 when left == right.
615
 * \retval == 0 when left == right.
616
 * \retval > 0 when left > right.
616
 * \retval > 0 when left > right.
617
 */
617
 */
618
int q931_party_address_cmp(const struct q931_party_address *left, const struct q931_party_address *right)
618
int q931_party_address_cmp(const struct q931_party_address *left, const struct q931_party_address *right)
619
{
619
{
620
	int cmp;
620
	int cmp;
621

    
   
621

   
622
	cmp = q931_party_number_cmp(&left->number, &right->number);
622
	cmp = q931_party_number_cmp(&left->number, &right->number);
623
	if (cmp) {
623
	if (cmp) {
624
		return cmp;
624
		return cmp;
625
	}
625
	}
626
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
626
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
627
	return cmp;
627
	return cmp;
628
}
628
}
629

    
   
629

   
630
/*!
630
/*!
631
 * \brief Compare the left and right party id.
631
 * \brief Compare the left and right party id.
632
 *
632
 *
633
 * \param left Left parameter party id.
633
 * \param left Left parameter party id.
634
 * \param right Right parameter party id.
634
 * \param right Right parameter party id.
635
 *
635
 *
636
 * \retval < 0 when left < right.
636
 * \retval < 0 when left < right.
637
 * \retval == 0 when left == right.
637
 * \retval == 0 when left == right.
638
 * \retval > 0 when left > right.
638
 * \retval > 0 when left > right.
639
 */
639
 */
640
int q931_party_id_cmp(const struct q931_party_id *left, const struct q931_party_id *right)
640
int q931_party_id_cmp(const struct q931_party_id *left, const struct q931_party_id *right)
641
{
641
{
642
	int cmp;
642
	int cmp;
643

    
   
643

   
644
	cmp = q931_party_number_cmp(&left->number, &right->number);
644
	cmp = q931_party_number_cmp(&left->number, &right->number);
645
	if (cmp) {
645
	if (cmp) {
646
		return cmp;
646
		return cmp;
647
	}
647
	}
648
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
648
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
649
	if (cmp) {
649
	if (cmp) {
650
		return cmp;
650
		return cmp;
651
	}
651
	}
652
	cmp = q931_party_name_cmp(&left->name, &right->name);
652
	cmp = q931_party_name_cmp(&left->name, &right->name);
653
	return cmp;
653
	return cmp;
654
}
654
}
655

    
   
655

   
656
/*!
656
/*!
657
 * \brief Compare the left and right party id addresses.
657
 * \brief Compare the left and right party id addresses.
658
 *
658
 *
659
 * \param left Left parameter party id.
659
 * \param left Left parameter party id.
660
 * \param right Right parameter party id.
660
 * \param right Right parameter party id.
661
 *
661
 *
662
 * \retval < 0 when left < right.
662
 * \retval < 0 when left < right.
663
 * \retval == 0 when left == right.
663
 * \retval == 0 when left == right.
664
 * \retval > 0 when left > right.
664
 * \retval > 0 when left > right.
665
 */
665
 */
666
int q931_party_id_cmp_address(const struct q931_party_id *left, const struct q931_party_id *right)
666
int q931_party_id_cmp_address(const struct q931_party_id *left, const struct q931_party_id *right)
667
{
667
{
668
	int cmp;
668
	int cmp;
669

    
   
669

   
670
	cmp = q931_party_number_cmp(&left->number, &right->number);
670
	cmp = q931_party_number_cmp(&left->number, &right->number);
671
	if (cmp) {
671
	if (cmp) {
672
		return cmp;
672
		return cmp;
673
	}
673
	}
674
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
674
	cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
675
	return cmp;
675
	return cmp;
676
}
676
}
677

    
   
677

   
678
/*!
678
/*!
679
 * \brief Compare the party id to the party address.
679
 * \brief Compare the party id to the party address.
680
 *
680
 *
681
 * \param id Party id.
681
 * \param id Party id.
682
 * \param address Party address.
682
 * \param address Party address.
683
 *
683
 *
684
 * \retval < 0 when id < address.
684
 * \retval < 0 when id < address.
685
 * \retval == 0 when id == address.
685
 * \retval == 0 when id == address.
686
 * \retval > 0 when id > address.
686
 * \retval > 0 when id > address.
687
 */
687
 */
688
int q931_cmp_party_id_to_address(const struct q931_party_id *id, const struct q931_party_address *address)
688
int q931_cmp_party_id_to_address(const struct q931_party_id *id, const struct q931_party_address *address)
689
{
689
{
690
	int cmp;
690
	int cmp;
691

    
   
691

   
692
	cmp = q931_party_number_cmp(&id->number, &address->number);
692
	cmp = q931_party_number_cmp(&id->number, &address->number);
693
	if (cmp) {
693
	if (cmp) {
694
		return cmp;
694
		return cmp;
695
	}
695
	}
696
	cmp = q931_party_subaddress_cmp(&id->subaddress, &address->subaddress);
696
	cmp = q931_party_subaddress_cmp(&id->subaddress, &address->subaddress);
697
	return cmp;
697
	return cmp;
698
}
698
}
699

    
   
699

   
700
/*!
700
/*!
701
 * \brief Copy a party id into a party address.
701
 * \brief Copy a party id into a party address.
702
 *
702
 *
703
 * \param address Party address.
703
 * \param address Party address.
704
 * \param id Party id.
704
 * \param id Party id.
705
 *
705
 *
706
 * \return Nothing
706
 * \return Nothing
707
 */
707
 */
708
void q931_party_id_copy_to_address(struct q931_party_address *address, const struct q931_party_id *id)
708
void q931_party_id_copy_to_address(struct q931_party_address *address, const struct q931_party_id *id)
709
{
709
{
710
	address->number = id->number;
710
	address->number = id->number;
711
	address->subaddress = id->subaddress;
711
	address->subaddress = id->subaddress;
712
}
712
}
713

    
   
713

   
714
/*!
714
/*!
715
 * \brief Copy the Q.931 party name to the PRI party name structure.
715
 * \brief Copy the Q.931 party name to the PRI party name structure.
716
 *
716
 *
717
 * \param pri_name PRI party name structure
717
 * \param pri_name PRI party name structure
718
 * \param q931_name Q.931 party name structure
718
 * \param q931_name Q.931 party name structure
719
 *
719
 *
720
 * \return Nothing
720
 * \return Nothing
721
 */
721
 */
722
void q931_party_name_copy_to_pri(struct pri_party_name *pri_name, const struct q931_party_name *q931_name)
722
void q931_party_name_copy_to_pri(struct pri_party_name *pri_name, const struct q931_party_name *q931_name)
723
{
723
{
724
	if (q931_name->valid) {
724
	if (q931_name->valid) {
725
		pri_name->valid = 1;
725
		pri_name->valid = 1;
726
		pri_name->presentation = q931_name->presentation;
726
		pri_name->presentation = q931_name->presentation;
727
		pri_name->char_set = q931_name->char_set;
727
		pri_name->char_set = q931_name->char_set;
728
		libpri_copy_string(pri_name->str, q931_name->str, sizeof(pri_name->str));
728
		libpri_copy_string(pri_name->str, q931_name->str, sizeof(pri_name->str));
729
	} else {
729
	} else {
730
		pri_name->valid = 0;
730
		pri_name->valid = 0;
731
		pri_name->presentation = PRI_PRES_UNAVAILABLE;
731
		pri_name->presentation = PRI_PRES_UNAVAILABLE;
732
		pri_name->char_set = PRI_CHAR_SET_ISO8859_1;
732
		pri_name->char_set = PRI_CHAR_SET_ISO8859_1;
733
		pri_name->str[0] = 0;
733
		pri_name->str[0] = 0;
734
	}
734
	}
735
}
735
}
736

    
   
736

   
737
/*!
737
/*!
738
 * \brief Copy the Q.931 party number to the PRI party number structure.
738
 * \brief Copy the Q.931 party number to the PRI party number structure.
739
 *
739
 *
740
 * \param pri_number PRI party number structure
740
 * \param pri_number PRI party number structure
741
 * \param q931_number Q.931 party number structure
741
 * \param q931_number Q.931 party number structure
742
 *
742
 *
743
 * \return Nothing
743
 * \return Nothing
744
 */
744
 */
745
void q931_party_number_copy_to_pri(struct pri_party_number *pri_number, const struct q931_party_number *q931_number)
745
void q931_party_number_copy_to_pri(struct pri_party_number *pri_number, const struct q931_party_number *q931_number)
746
{
746
{
747
	if (q931_number->valid) {
747
	if (q931_number->valid) {
748
		pri_number->valid = 1;
748
		pri_number->valid = 1;
749
		pri_number->presentation = q931_number->presentation;
749
		pri_number->presentation = q931_number->presentation;
750
		pri_number->plan = q931_number->plan;
750
		pri_number->plan = q931_number->plan;
751
		libpri_copy_string(pri_number->str, q931_number->str, sizeof(pri_number->str));
751
		libpri_copy_string(pri_number->str, q931_number->str, sizeof(pri_number->str));
752
	} else {
752
	} else {
753
		pri_number->valid = 0;
753
		pri_number->valid = 0;
754
		pri_number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
754
		pri_number->presentation = PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED;
755
		pri_number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
755
		pri_number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
756
		pri_number->str[0] = 0;
756
		pri_number->str[0] = 0;
757
	}
757
	}
758
}
758
}
759

    
   
759

   
760
/*!
760
/*!
761
 * \brief Copy the Q.931 party subaddress to the PRI party subaddress structure.
761
 * \brief Copy the Q.931 party subaddress to the PRI party subaddress structure.
762
 *
762
 *
763
 * \param pri_subaddress PRI party subaddress structure
763
 * \param pri_subaddress PRI party subaddress structure
764
 * \param q931_subaddress Q.931 party subaddress structure
764
 * \param q931_subaddress Q.931 party subaddress structure
765
 *
765
 *
766
 * \return Nothing
766
 * \return Nothing
767
 */
767
 */
768
void q931_party_subaddress_copy_to_pri(struct pri_party_subaddress *pri_subaddress, const struct q931_party_subaddress *q931_subaddress)
768
void q931_party_subaddress_copy_to_pri(struct pri_party_subaddress *pri_subaddress, const struct q931_party_subaddress *q931_subaddress)
769
{
769
{
770
	int length;
770
	int length;
771

    
   
771

   
772
	if (!q931_subaddress->valid) {
772
	if (!q931_subaddress->valid) {
773
		pri_subaddress->valid = 0;
773
		pri_subaddress->valid = 0;
774
		pri_subaddress->type = 0;
774
		pri_subaddress->type = 0;
775
		pri_subaddress->odd_even_indicator = 0;
775
		pri_subaddress->odd_even_indicator = 0;
776
		pri_subaddress->length = 0;
776
		pri_subaddress->length = 0;
777
		pri_subaddress->data[0] = '\0';
777
		pri_subaddress->data[0] = '\0';
778
		return;
778
		return;
779
	}
779
	}
780

    
   
780

   
781
	pri_subaddress->valid = 1;
781
	pri_subaddress->valid = 1;
782
	pri_subaddress->type = q931_subaddress->type;
782
	pri_subaddress->type = q931_subaddress->type;
783
	pri_subaddress->odd_even_indicator = q931_subaddress->odd_even_indicator;
783
	pri_subaddress->odd_even_indicator = q931_subaddress->odd_even_indicator;
784

    
   
784

   
785
	/*
785
	/*
786
	 * The size of pri_subaddress->data[] is not the same as the size of
786
	 * The size of pri_subaddress->data[] is not the same as the size of
787
	 * q931_subaddress->data[].
787
	 * q931_subaddress->data[].
788
	 */
788
	 */
789
	length = q931_subaddress->length;
789
	length = q931_subaddress->length;
790
	pri_subaddress->length = length;
790
	pri_subaddress->length = length;
791
	memcpy(pri_subaddress->data, q931_subaddress->data, length);
791
	memcpy(pri_subaddress->data, q931_subaddress->data, length);
792
	pri_subaddress->data[length] = '\0';
792
	pri_subaddress->data[length] = '\0';
793
}
793
}
794

    
   
794

   
795
/*!
795
/*!
796
 * \brief Copy the Q.931 party address to the PRI party address structure.
796
 * \brief Copy the Q.931 party address to the PRI party address structure.
797
 *
797
 *
798
 * \param pri_address PRI party address structure
798
 * \param pri_address PRI party address structure
799
 * \param q931_address Q.931 party address structure
799
 * \param q931_address Q.931 party address structure
800
 *
800
 *
801
 * \return Nothing
801
 * \return Nothing
802
 */
802
 */
803
void q931_party_address_copy_to_pri(struct pri_party_address *pri_address, const struct q931_party_address *q931_address)
803
void q931_party_address_copy_to_pri(struct pri_party_address *pri_address, const struct q931_party_address *q931_address)
804
{
804
{
805
	q931_party_number_copy_to_pri(&pri_address->number, &q931_address->number);
805
	q931_party_number_copy_to_pri(&pri_address->number, &q931_address->number);
806
	q931_party_subaddress_copy_to_pri(&pri_address->subaddress, &q931_address->subaddress);
806
	q931_party_subaddress_copy_to_pri(&pri_address->subaddress, &q931_address->subaddress);
807
}
807
}
808

    
   
808

   
809
/*!
809
/*!
810
 * \brief Copy the Q.931 party id to the PRI party id structure.
810
 * \brief Copy the Q.931 party id to the PRI party id structure.
811
 *
811
 *
812
 * \param pri_id PRI party id structure
812
 * \param pri_id PRI party id structure
813
 * \param q931_id Q.931 party id structure
813
 * \param q931_id Q.931 party id structure
814
 *
814
 *
815
 * \return Nothing
815
 * \return Nothing
816
 */
816
 */
817
void q931_party_id_copy_to_pri(struct pri_party_id *pri_id, const struct q931_party_id *q931_id)
817
void q931_party_id_copy_to_pri(struct pri_party_id *pri_id, const struct q931_party_id *q931_id)
818
{
818
{
819
	q931_party_name_copy_to_pri(&pri_id->name, &q931_id->name);
819
	q931_party_name_copy_to_pri(&pri_id->name, &q931_id->name);
820
	q931_party_number_copy_to_pri(&pri_id->number, &q931_id->number);
820
	q931_party_number_copy_to_pri(&pri_id->number, &q931_id->number);
821
	q931_party_subaddress_copy_to_pri(&pri_id->subaddress, &q931_id->subaddress);
821
	q931_party_subaddress_copy_to_pri(&pri_id->subaddress, &q931_id->subaddress);
822
}
822
}
823

    
   
823

   
824
/*!
824
/*!
825
 * \brief Copy the Q.931 redirecting data to the PRI redirecting structure.
825
 * \brief Copy the Q.931 redirecting data to the PRI redirecting structure.
826
 *
826
 *
827
 * \param pri_redirecting PRI redirecting structure
827
 * \param pri_redirecting PRI redirecting structure
828
 * \param q931_redirecting Q.931 redirecting structure
828
 * \param q931_redirecting Q.931 redirecting structure
829
 *
829
 *
830
 * \return Nothing
830
 * \return Nothing
831
 */
831
 */
832
void q931_party_redirecting_copy_to_pri(struct pri_party_redirecting *pri_redirecting, const struct q931_party_redirecting *q931_redirecting)
832
void q931_party_redirecting_copy_to_pri(struct pri_party_redirecting *pri_redirecting, const struct q931_party_redirecting *q931_redirecting)
833
{
833
{
834
	q931_party_id_copy_to_pri(&pri_redirecting->from, &q931_redirecting->from);
834
	q931_party_id_copy_to_pri(&pri_redirecting->from, &q931_redirecting->from);
835
	q931_party_id_copy_to_pri(&pri_redirecting->to, &q931_redirecting->to);
835
	q931_party_id_copy_to_pri(&pri_redirecting->to, &q931_redirecting->to);
836
	q931_party_id_copy_to_pri(&pri_redirecting->orig_called,
836
	q931_party_id_copy_to_pri(&pri_redirecting->orig_called,
837
		&q931_redirecting->orig_called);
837
		&q931_redirecting->orig_called);
838
	pri_redirecting->count = q931_redirecting->count;
838
	pri_redirecting->count = q931_redirecting->count;
839
	pri_redirecting->orig_reason = q931_redirecting->orig_reason;
839
	pri_redirecting->orig_reason = q931_redirecting->orig_reason;
840
	pri_redirecting->reason = q931_redirecting->reason;
840
	pri_redirecting->reason = q931_redirecting->reason;
841
}
841
}
842

    
   
842

   
843
/*!
843
/*!
844
 * \brief Fixup some values in the q931_party_id that may be objectionable by switches.
844
 * \brief Fixup some values in the q931_party_id that may be objectionable by switches.
845
 *
845
 *
846
 * \param ctrl D channel controller.
846
 * \param ctrl D channel controller.
847
 * \param id Party ID to tweak.
847
 * \param id Party ID to tweak.
848
 *
848
 *
849
 * \return Nothing
849
 * \return Nothing
850
 */
850
 */
851
void q931_party_id_fixup(const struct pri *ctrl, struct q931_party_id *id)
851
void q931_party_id_fixup(const struct pri *ctrl, struct q931_party_id *id)
852
{
852
{
853
	switch (ctrl->switchtype) {
853
	switch (ctrl->switchtype) {
854
	case PRI_SWITCH_DMS100:
854
	case PRI_SWITCH_DMS100:
855
	case PRI_SWITCH_ATT4ESS:
855
	case PRI_SWITCH_ATT4ESS:
856
		/* Doesn't like certain presentation types */
856
		/* Doesn't like certain presentation types */
857
		if (id->number.valid && !(id->number.presentation & 0x7c)) {
857
		if (id->number.valid && !(id->number.presentation & 0x7c)) {
858
			/* i.e., If presentation is allowed it must be a network number */
858
			/* i.e., If presentation is allowed it must be a network number */
859
			id->number.presentation = PRES_ALLOWED_NETWORK_NUMBER;
859
			id->number.presentation = PRES_ALLOWED_NETWORK_NUMBER;
860
		}
860
		}
861
		break;
861
		break;
862
	default:
862
	default:
863
		break;
863
		break;
864
	}
864
	}
865
}
865
}
866

    
   
866

   
867
/*!
867
/*!
868
 * \brief Determine the overall presentation value for the given party.
868
 * \brief Determine the overall presentation value for the given party.
869
 *
869
 *
870
 * \param id Party to determine the overall presentation value.
870
 * \param id Party to determine the overall presentation value.
871
 *
871
 *
872
 * \return Overall presentation value for the given party.
872
 * \return Overall presentation value for the given party.
873
 */
873
 */
874
int q931_party_id_presentation(const struct q931_party_id *id)
874
int q931_party_id_presentation(const struct q931_party_id *id)
875
{
875
{
876
	int number_priority;
876
	int number_priority;
877
	int number_value;
877
	int number_value;
878
	int number_screening;
878
	int number_screening;
879
	int name_priority;
879
	int name_priority;
880
	int name_value;
880
	int name_value;
881

    
   
881

   
882
	/* Determine name presentation priority. */
882
	/* Determine name presentation priority. */
883
	if (!id->name.valid) {
883
	if (!id->name.valid) {
884
		name_value = PRI_PRES_UNAVAILABLE;
884
		name_value = PRI_PRES_UNAVAILABLE;
885
		name_priority = 3;
885
		name_priority = 3;
886
	} else {
886
	} else {
887
		name_value = id->name.presentation & PRI_PRES_RESTRICTION;
887
		name_value = id->name.presentation & PRI_PRES_RESTRICTION;
888
		switch (name_value) {
888
		switch (name_value) {
889
		case PRI_PRES_RESTRICTED:
889
		case PRI_PRES_RESTRICTED:
890
			name_priority = 0;
890
			name_priority = 0;
891
			break;
891
			break;
892
		case PRI_PRES_ALLOWED:
892
		case PRI_PRES_ALLOWED:
893
			name_priority = 1;
893
			name_priority = 1;
894
			break;
894
			break;
895
		case PRI_PRES_UNAVAILABLE:
895
		case PRI_PRES_UNAVAILABLE:
896
			name_priority = 2;
896
			name_priority = 2;
897
			break;
897
			break;
898
		default:
898
		default:
899
			name_value = PRI_PRES_UNAVAILABLE;
899
			name_value = PRI_PRES_UNAVAILABLE;
900
			name_priority = 3;
900
			name_priority = 3;
901
			break;
901
			break;
902
		}
902
		}
903
	}
903
	}
904

    
   
904

   
905
	/* Determine number presentation priority. */
905
	/* Determine number presentation priority. */
906
	if (!id->number.valid) {
906
	if (!id->number.valid) {
907
		number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
907
		number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
908
		number_value = PRI_PRES_UNAVAILABLE;
908
		number_value = PRI_PRES_UNAVAILABLE;
909
		number_priority = 3;
909
		number_priority = 3;
910
	} else {
910
	} else {
911
		number_screening = id->number.presentation & PRI_PRES_NUMBER_TYPE;
911
		number_screening = id->number.presentation & PRI_PRES_NUMBER_TYPE;
912
		number_value = id->number.presentation & PRI_PRES_RESTRICTION;
912
		number_value = id->number.presentation & PRI_PRES_RESTRICTION;
913
		switch (number_value) {
913
		switch (number_value) {
914
		case PRI_PRES_RESTRICTED:
914
		case PRI_PRES_RESTRICTED:
915
			number_priority = 0;
915
			number_priority = 0;
916
			break;
916
			break;
917
		case PRI_PRES_ALLOWED:
917
		case PRI_PRES_ALLOWED:
918
			number_priority = 1;
918
			number_priority = 1;
919
			break;
919
			break;
920
		case PRI_PRES_UNAVAILABLE:
920
		case PRI_PRES_UNAVAILABLE:
921
			number_priority = 2;
921
			number_priority = 2;
922
			break;
922
			break;
923
		default:
923
		default:
924
			number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
924
			number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
925
			number_value = PRI_PRES_UNAVAILABLE;
925
			number_value = PRI_PRES_UNAVAILABLE;
926
			number_priority = 3;
926
			number_priority = 3;
927
			break;
927
			break;
928
		}
928
		}
929
	}
929
	}
930

    
   
930

   
931
	/* Select the wining presentation value. */
931
	/* Select the wining presentation value. */
932
	if (name_priority < number_priority) {
932
	if (name_priority < number_priority) {
933
		number_value = name_value;
933
		number_value = name_value;
934
	}
934
	}
935

    
   
935

   
936
	return number_value | number_screening;
936
	return number_value | number_screening;
937
}
937
}
938

    
   
938

   
939
/*!
939
/*!
940
 * \internal
940
 * \internal
941
 * \brief Get binary buffer contents into the destination buffer.
941
 * \brief Get binary buffer contents into the destination buffer.
942
 *
942
 *
943
 * \param dst Destination buffer.
943
 * \param dst Destination buffer.
944
 * \param dst_size Destination buffer sizeof()
944
 * \param dst_size Destination buffer sizeof()
945
 * \param src Source buffer.
945
 * \param src Source buffer.
946
 * \param src_len Source buffer length to copy.
946
 * \param src_len Source buffer length to copy.
947
 *
947
 *
948
 * \note The destination buffer is nul terminated just in case
948
 * \note The destination buffer is nul terminated just in case
949
 * the contents are used as a string anyway.
949
 * the contents are used as a string anyway.
950
 *
950
 *
951
 * \retval 0 on success.
951
 * \retval 0 on success.
952
 * \retval -1 on error.  The copy did not happen.
952
 * \retval -1 on error.  The copy did not happen.
953
 */
953
 */
954
static int q931_memget(unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
954
static int q931_memget(unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
955
{
955
{
956
	if (src_len < 0 || src_len > dst_size - 1) {
956
	if (src_len < 0 || src_len > dst_size - 1) {
957
		dst[0] = 0;
957
		dst[0] = 0;
958
		return -1;
958
		return -1;
959
	}
959
	}
960
	memcpy(dst, src, src_len);
960
	memcpy(dst, src, src_len);
961
	dst[src_len] = 0;
961
	dst[src_len] = 0;
962
	return 0;
962
	return 0;
963
}
963
}
964

    
   
964

   
965
/*!
965
/*!
966
 * \internal
966
 * \internal
967
 * \brief Get source buffer contents into the destination buffer for a string.
967
 * \brief Get source buffer contents into the destination buffer for a string.
968
 *
968
 *
969
 * \param dst Destination buffer.
969
 * \param dst Destination buffer.
970
 * \param dst_size Destination buffer sizeof()
970
 * \param dst_size Destination buffer sizeof()
971
 * \param src Source buffer.
971
 * \param src Source buffer.
972
 * \param src_len Source buffer length to copy.
972
 * \param src_len Source buffer length to copy.
973
 *
973
 *
974
 * \note The destination buffer is nul terminated.
974
 * \note The destination buffer is nul terminated.
975
 * \note Nul bytes from the source buffer are not copied.
975
 * \note Nul bytes from the source buffer are not copied.
976
 *
976
 *
977
 * \retval 0 on success.
977
 * \retval 0 on success.
978
 * \retval -1 if nul bytes were found in the source data.
978
 * \retval -1 if nul bytes were found in the source data.
979
 */
979
 */
980
static int q931_strget(unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
980
static int q931_strget(unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
981
{
981
{
982
	int saw_nul;
982
	int saw_nul;
983

    
   
983

   
984
	if (src_len < 1) {
984
	if (src_len < 1) {
985
		dst[0] = '\0';
985
		dst[0] = '\0';
986
		return 0;
986
		return 0;
987
	}
987
	}
988

    
   
988

   
989
	saw_nul = 0;
989
	saw_nul = 0;
990
	--dst_size;
990
	--dst_size;
991
	while (dst_size && src_len) {
991
	while (dst_size && src_len) {
992
		if (*src) {
992
		if (*src) {
993
			*dst++ = *src;
993
			*dst++ = *src;
994
			--dst_size;
994
			--dst_size;
995
		} else {
995
		} else {
996
			/* Skip nul bytes in the source buffer. */
996
			/* Skip nul bytes in the source buffer. */
997
			saw_nul = -1;
997
			saw_nul = -1;
998
		}
998
		}
999
		++src;
999
		++src;
1000
		--src_len;
1000
		--src_len;
1001
	}
1001
	}
1002
	*dst = '\0';
1002
	*dst = '\0';
1003

    
   
1003

   
1004
	return saw_nul;
1004
	return saw_nul;
1005
}
1005
}
1006

    
   
1006

   
1007

    
   
1007

   
1008
/*!
1008
/*!
1009
 * \internal
1009
 * \internal
1010
 * \brief Get source buffer contents into the destination buffer for a string.
1010
 * \brief Get source buffer contents into the destination buffer for a string.
1011
 *
1011
 *
1012
 * \param ctrl D channel controller.
1012
 * \param ctrl D channel controller.
1013
 * \param ie_name IE name to report nul bytes found in.
1013
 * \param ie_name IE name to report nul bytes found in.
1014
 * \param dst Destination buffer.
1014
 * \param dst Destination buffer.
1015
 * \param dst_size Destination buffer sizeof()
1015
 * \param dst_size Destination buffer sizeof()
1016
 * \param src Source buffer.
1016
 * \param src Source buffer.
1017
 * \param src_len Source buffer length to copy.
1017
 * \param src_len Source buffer length to copy.
1018
 *
1018
 *
1019
 * \note The destination buffer is nul terminated.
1019
 * \note The destination buffer is nul terminated.
1020
 * \note Nul bytes from the source buffer are not copied.
1020
 * \note Nul bytes from the source buffer are not copied.
1021
 *
1021
 *
1022
 * \retval 0 on success.
1022
 * \retval 0 on success.
1023
 * \retval -1 if nul bytes were found in the source data.
1023
 * \retval -1 if nul bytes were found in the source data.
1024
 */
1024
 */
1025
static int q931_strget_gripe(struct pri *ctrl, const char *ie_name, unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
1025
static int q931_strget_gripe(struct pri *ctrl, const char *ie_name, unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
1026
{
1026
{
1027
	int saw_nul;
1027
	int saw_nul;
1028

    
   
1028

   
1029
/* To quietly remove nul octets just comment out the following line. */
1029
/* To quietly remove nul octets just comment out the following line. */
1030
#define UNCONDITIONALLY_REPORT_REMOVED_NUL_OCTETS	1
1030
#define UNCONDITIONALLY_REPORT_REMOVED_NUL_OCTETS	1
1031

    
   
1031

   
1032
	saw_nul = q931_strget(dst, dst_size, src, src_len);
1032
	saw_nul = q931_strget(dst, dst_size, src, src_len);
1033
	if (saw_nul
1033
	if (saw_nul
1034
#if !defined(UNCONDITIONALLY_REPORT_REMOVED_NUL_OCTETS)
1034
#if !defined(UNCONDITIONALLY_REPORT_REMOVED_NUL_OCTETS)
1035
		&& (ctrl->debug & PRI_DEBUG_Q931_STATE)
1035
		&& (ctrl->debug & PRI_DEBUG_Q931_STATE)
1036
#endif
1036
#endif
1037
		) {
1037
		) {
1038
		pri_message(ctrl, "!! Removed nul octets from IE '%s' and returning '%s'.\n",
1038
		pri_message(ctrl, "!! Removed nul octets from IE '%s' and returning '%s'.\n",
1039
			ie_name, dst);
1039
			ie_name, dst);
1040
	}
1040
	}
1041

    
   
1041

   
1042
	return saw_nul;
1042
	return saw_nul;
1043
}
1043
}
1044

    
   
1044

   
1045
/*!
1045
/*!
1046
 * \internal
1046
 * \internal
1047
 * \brief Clear the display text.
1047
 * \brief Clear the display text.
1048
 *
1048
 *
1049
 * \param call Q.931 call to clear display text.
1049
 * \param call Q.931 call to clear display text.
1050
 *
1050
 *
1051
 * \return Nothing
1051
 * \return Nothing
1052
 */
1052
 */
1053
static void q931_display_clear(struct q931_call *call)
1053
static void q931_display_clear(struct q931_call *call)
1054
{
1054
{
1055
	call->display.text = NULL;
1055
	call->display.text = NULL;
1056
}
1056
}
1057

    
   
1057

   
1058
/*!
1058
/*!
1059
 * \internal
1059
 * \internal
1060
 * \brief Set the display text for the party name.
1060
 * \brief Set the display text for the party name.
1061
 *
1061
 *
1062
 * \param call Q.931 call to set display text to the party name.
1062
 * \param call Q.931 call to set display text to the party name.
1063
 *
1063
 *
1064
 * \return Nothing
1064
 * \return Nothing
1065
 */
1065
 */
1066
static void q931_display_name_send(struct q931_call *call, const struct q931_party_name *name)
1066
static void q931_display_name_send(struct q931_call *call, const struct q931_party_name *name)
1067
{
1067
{
1068
	if (name->valid) {
1068
	if (name->valid) {
1069
		switch (name->presentation & PRI_PRES_RESTRICTION) {
1069
		switch (name->presentation & PRI_PRES_RESTRICTION) {
1070
		case PRI_PRES_ALLOWED:
1070
		case PRI_PRES_ALLOWED:
1071
			call->display.text = (unsigned char *) name->str;
1071
			call->display.text = (unsigned char *) name->str;
1072
			call->display.full_ie = 0;
1072
			call->display.full_ie = 0;
1073
			call->display.length = strlen(name->str);
1073
			call->display.length = strlen(name->str);
1074
			call->display.char_set = name->char_set;
1074
			call->display.char_set = name->char_set;
1075
			break;
1075
			break;
1076
		default:
1076
		default:
1077
			call->display.text = NULL;
1077
			call->display.text = NULL;
1078
			break;
1078
			break;
1079
		}
1079
		}
1080
	} else {
1080
	} else {
1081
		call->display.text = NULL;
1081
		call->display.text = NULL;
1082
	}
1082
	}
1083
}
1083
}
1084

    
   
1084

   
1085
/*!
1085
/*!
1086
 * \brief Get the display text into the party name.
1086
 * \brief Get the display text into the party name.
1087
 *
1087
 *
1088
 * \param call Q.931 call to get the display text into the party name.
1088
 * \param call Q.931 call to get the display text into the party name.
1089
 * \param name Party name to fill if there is display text.
1089
 * \param name Party name to fill if there is display text.
1090
 *
1090
 *
1091
 * \note
1091
 * \note
1092
 * The party name is not touched if there is no display text.
1092
 * The party name is not touched if there is no display text.
1093
 *
1093
 *
1094
 * \note
1094
 * \note
1095
 * The display text is consumed.
1095
 * The display text is consumed.
1096
 *
1096
 *
1097
 * \return TRUE if party name filled.
1097
 * \return TRUE if party name filled.
1098
 */
1098
 */
1099
int q931_display_name_get(struct q931_call *call, struct q931_party_name *name)
1099
int q931_display_name_get(struct q931_call *call, struct q931_party_name *name)
1100
{
1100
{
1101
	if (!call->display.text) {
1101
	if (!call->display.text) {
1102
		return 0;
1102
		return 0;
1103
	}
1103
	}
1104

    
   
1104

   
1105
	name->valid = 1;
1105
	name->valid = 1;
1106
	name->char_set = call->display.char_set;
1106
	name->char_set = call->display.char_set;
1107
	q931_strget_gripe(call->pri, ie2str(call->display.full_ie),
1107
	q931_strget_gripe(call->pri, ie2str(call->display.full_ie),
1108
		(unsigned char *) name->str, sizeof(name->str), call->display.text,
1108
		(unsigned char *) name->str, sizeof(name->str), call->display.text,
1109
		call->display.length);
1109
		call->display.length);
1110
	if (name->str[0]) {
1110
	if (name->str[0]) {
1111
		name->presentation = PRI_PRES_ALLOWED;
1111
		name->presentation = PRI_PRES_ALLOWED;
1112
	} else {
1112
	} else {
1113
		name->presentation = PRI_PRES_RESTRICTED;
1113
		name->presentation = PRI_PRES_RESTRICTED;
1114
	}
1114
	}
1115

    
   
1115

   
1116
	/* Mark the display text as consumed. */
1116
	/* Mark the display text as consumed. */
1117
	call->display.text = NULL;
1117
	call->display.text = NULL;
1118

    
   
1118

   
1119
	return 1;
1119
	return 1;
1120
}
1120
}
1121

    
   
1121

   
1122
/*!
1122
/*!
1123
 * \internal
1123
 * \internal
1124
 * \brief Fill a subcmd with any display text.
1124
 * \brief Fill a subcmd with any display text.
1125
 *
1125
 *
1126
 * \param ctrl D channel controller.
1126
 * \param ctrl D channel controller.
1127
 * \param call Q.931 call leg.
1127
 * \param call Q.931 call leg.
1128
 *
1128
 *
1129
 * \note
1129
 * \note
1130
 * The display text is consumed.
1130
 * The display text is consumed.
1131
 *
1131
 *
1132
 * \return Nothing
1132
 * \return Nothing
1133
 */
1133
 */
1134
static void q931_display_subcmd(struct pri *ctrl, struct q931_call *call)
1134
static void q931_display_subcmd(struct pri *ctrl, struct q931_call *call)
1135
{
1135
{
1136
	struct pri_subcommand *subcmd;
1136
	struct pri_subcommand *subcmd;
1137

    
   
1137

   
1138
	if (call->display.text && call->display.length
1138
	if (call->display.text && call->display.length
1139
		&& (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_TEXT)) {
1139
		&& (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_TEXT)) {
1140
		subcmd = q931_alloc_subcommand(ctrl);
1140
		subcmd = q931_alloc_subcommand(ctrl);
1141
		if (subcmd) {
1141
		if (subcmd) {
1142
			/* Setup display text subcommand */
1142
			/* Setup display text subcommand */
1143
			subcmd->cmd = PRI_SUBCMD_DISPLAY_TEXT;
1143
			subcmd->cmd = PRI_SUBCMD_DISPLAY_TEXT;
1144
			subcmd->u.display.char_set = call->display.char_set;
1144
			subcmd->u.display.char_set = call->display.char_set;
1145
			q931_strget_gripe(ctrl, ie2str(call->display.full_ie),
1145
			q931_strget_gripe(ctrl, ie2str(call->display.full_ie),
1146
				(unsigned char *) subcmd->u.display.text, sizeof(subcmd->u.display.text),
1146
				(unsigned char *) subcmd->u.display.text, sizeof(subcmd->u.display.text),
1147
				call->display.text, call->display.length);
1147
				call->display.text, call->display.length);
1148
			subcmd->u.display.length = strlen(subcmd->u.display.text);
1148
			subcmd->u.display.length = strlen(subcmd->u.display.text);
1149
		}
1149
		}
1150
	}
1150
	}
1151

    
   
1151

   
1152
	/* Mark the display text as consumed. */
1152
	/* Mark the display text as consumed. */
1153
	call->display.text = NULL;
1153
	call->display.text = NULL;
1154
}
1154
}
1155

    
   
1155

   
1156
/*!
1156
/*!
1157
 * \brief Find the winning subcall if it exists or current call if not outboundbroadcast.
1157
 * \brief Find the winning subcall if it exists or current call if not outboundbroadcast.
1158
 *
1158
 *
1159
 * \param call Starting Q.931 call record of search.
1159
 * \param call Starting Q.931 call record of search.
1160
 *
1160
 *
1161
 * \retval winning-call or given call if not outboundbroadcast.
1161
 * \retval winning-call or given call if not outboundbroadcast.
1162
 * \retval NULL if no winning call yet.
1162
 * \retval NULL if no winning call yet.
1163
 */
1163
 */
1164
struct q931_call *q931_find_winning_call(struct q931_call *call)
1164
struct q931_call *q931_find_winning_call(struct q931_call *call)
1165
{
1165
{
1166
	struct q931_call *master;
1166
	struct q931_call *master;
1167

    
   
1167

   
1168
	master = call->master_call;
1168
	master = call->master_call;
1169
	if (master->outboundbroadcast) {
1169
	if (master->outboundbroadcast) {
1170
		/* We have potential subcalls.  Now get the winning call if declared yet. */
1170
		/* We have potential subcalls.  Now get the winning call if declared yet. */
1171
		if (master->pri_winner < 0) {
1171
		if (master->pri_winner < 0) {
1172
			/* Winner not declared yet.*/
1172
			/* Winner not declared yet.*/
1173
			call = NULL;
1173
			call = NULL;
1174
		} else {
1174
		} else {
1175
			call = master->subcalls[master->pri_winner];
1175
			call = master->subcalls[master->pri_winner];
1176
		}
1176
		}
1177
	}
1177
	}
1178
	return call;
1178
	return call;
1179
}
1179
}
1180

    
   
1180

   
1181
/*!
1181
/*!
1182
 * \internal
1182
 * \internal
1183
 * \brief Append the given ie contents to the save ie location.
1183
 * \brief Append the given ie contents to the save ie location.
1184
 *
1184
 *
1185
 * \param save_ie Saved ie contents to append new ie.
1185
 * \param save_ie Saved ie contents to append new ie.
1186
 * \param ie Contents to append.
1186
 * \param ie Contents to append.
1187
 *
1187
 *
1188
 * \return Nothing
1188
 * \return Nothing
1189
 */
1189
 */
1190
static void q931_append_ie_contents(struct q931_saved_ie_contents *save_ie, struct q931_ie *ie)
1190
static void q931_append_ie_contents(struct q931_saved_ie_contents *save_ie, struct q931_ie *ie)
1191
{
1191
{
1192
	int size;
1192
	int size;
1193

    
   
1193

   
1194
	size = ie->len + 2;
1194
	size = ie->len + 2;
1195
	if (size < sizeof(save_ie->data) - save_ie->length) {
1195
	if (size < sizeof(save_ie->data) - save_ie->length) {
1196
		/* Contents will fit so append it. */
1196
		/* Contents will fit so append it. */
1197
		memcpy(&save_ie->data[save_ie->length], ie, size);
1197
		memcpy(&save_ie->data[save_ie->length], ie, size);
1198
		save_ie->length += size;
1198
		save_ie->length += size;
1199
	}
1199
	}
1200
}
1200
}
1201

    
   
1201

   
1202
static void q931_clr_subcommands(struct pri *ctrl)
1202
static void q931_clr_subcommands(struct pri *ctrl)
1203
{
1203
{
1204
	ctrl->subcmds.counter_subcmd = 0;
1204
	ctrl->subcmds.counter_subcmd = 0;
1205
}
1205
}
1206

    
   
1206

   
1207
struct pri_subcommand *q931_alloc_subcommand(struct pri *ctrl)
1207
struct pri_subcommand *q931_alloc_subcommand(struct pri *ctrl)
1208
{
1208
{
1209
	if (ctrl->subcmds.counter_subcmd < PRI_MAX_SUBCOMMANDS) {
1209
	if (ctrl->subcmds.counter_subcmd < PRI_MAX_SUBCOMMANDS) {
1210
		return &ctrl->subcmds.subcmd[ctrl->subcmds.counter_subcmd++];
1210
		return &ctrl->subcmds.subcmd[ctrl->subcmds.counter_subcmd++];
1211
	}
1211
	}
1212

    
   
1212

   
1213
	pri_error(ctrl, "ERROR: Too many facility subcommands\n");
1213
	pri_error(ctrl, "ERROR: Too many facility subcommands\n");
1214
	return NULL;
1214
	return NULL;
1215
}
1215
}
1216

    
   
1216

   
1217
static char *code2str(int code, struct msgtype *codes, int max)
1217
static char *code2str(int code, struct msgtype *codes, int max)
1218
{
1218
{
1219
	int x;
1219
	int x;
1220
	for (x=0;x<max; x++) 
1220
	for (x=0;x<max; x++) 
1221
		if (codes[x].msgnum == code)
1221
		if (codes[x].msgnum == code)
1222
			return codes[x].name;
1222
			return codes[x].name;
1223
	return "Unknown";
1223
	return "Unknown";
1224
}
1224
}
1225

    
   
1225

   
1226
static char *pritype(int type)
1226
static char *pritype(int type)
1227
{
1227
{
1228
	switch (type) {
1228
	switch (type) {
1229
	case PRI_CPE:
1229
	case PRI_CPE:
1230
		return "CPE";
1230
		return "CPE";
1231
		break;
1231
		break;
1232
	case PRI_NETWORK:
1232
	case PRI_NETWORK:
1233
		return "NET";
1233
		return "NET";
1234
		break;
1234
		break;
1235
	default:
1235
	default:
1236
		return "UNKNOWN";
1236
		return "UNKNOWN";
1237
	}
1237
	}
1238
}
1238
}
1239

    
   
1239

   
1240
static char *binary(int b, int len) {
1240
static char *binary(int b, int len) {
1241
	static char res[33];
1241
	static char res[33];
1242
	int x;
1242
	int x;
1243
	memset(res, 0, sizeof(res));
1243
	memset(res, 0, sizeof(res));
1244
	if (len > 32)
1244
	if (len > 32)
1245
		len = 32;
1245
		len = 32;
1246
	for (x=1;x<=len;x++)
1246
	for (x=1;x<=len;x++)
1247
		res[x-1] = b & (1 << (len - x)) ? '1' : '0';
1247
		res[x-1] = b & (1 << (len - x)) ? '1' : '0';
1248
	return res;
1248
	return res;
1249
}
1249
}
1250

    
   
1250

   
1251
static int receive_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1251
static int receive_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1252
{	
1252
{	
1253
	int x;
1253
	int x;
1254
	int pos = 0;
1254
	int pos = 0;
1255
	int need_extended_channel_octets;/*!< TRUE if octets 3.2 and 3.3 need to be present. */
1255
	int need_extended_channel_octets;/*!< TRUE if octets 3.2 and 3.3 need to be present. */
1256

    
   
1256

   
1257
	call->restart.count = 0;
1257
	call->restart.count = 0;
1258

    
   
1258

   
1259
	if (ie->data[0] & 0x08) {
1259
	if (ie->data[0] & 0x08) {
1260
		call->chanflags = FLAG_EXCLUSIVE;
1260
		call->chanflags = FLAG_EXCLUSIVE;
1261
	} else {
1261
	} else {
1262
		call->chanflags = FLAG_PREFERRED;
1262
		call->chanflags = FLAG_PREFERRED;
1263
	}
1263
	}
1264

    
   
1264

   
1265
	need_extended_channel_octets = 0;
1265
	need_extended_channel_octets = 0;
1266
	if (ie->data[0] & 0x20) {
1266
	if (ie->data[0] & 0x20) {
1267
		/* PRI encoded interface type */
1267
		/* PRI encoded interface type */
1268
		switch (ie->data[0] & 0x03) {
1268
		switch (ie->data[0] & 0x03) {
1269
		case 0x00:
1269
		case 0x00:
1270
			/* No channel */
1270
			/* No channel */
1271
			call->channelno = 0;
1271
			call->channelno = 0;
1272
			call->chanflags = FLAG_PREFERRED;
1272
			call->chanflags = FLAG_PREFERRED;
1273
			break;
1273
			break;
1274
		case 0x01:
1274
		case 0x01:
1275
			/* As indicated in following octets */
1275
			/* As indicated in following octets */
1276
			need_extended_channel_octets = 1;
1276
			need_extended_channel_octets = 1;
1277
			break;
1277
			break;
1278
		case 0x03:
1278
		case 0x03:
1279
			/* Any channel */
1279
			/* Any channel */
1280
			call->chanflags = FLAG_PREFERRED;
1280
			call->chanflags = FLAG_PREFERRED;
1281
			break;
1281
			break;
1282
		default:
1282
		default:
1283
			pri_error(ctrl, "!! Unexpected Channel selection %d\n", ie->data[0] & 0x03);
1283
			pri_error(ctrl, "!! Unexpected Channel selection %d\n", ie->data[0] & 0x03);
1284
			return -1;
1284
			return -1;
1285
		}
1285
		}
1286
	} else {
1286
	} else {
1287
		/* BRI encoded interface type */
1287
		/* BRI encoded interface type */
1288
		switch (ie->data[0] & 0x03) {
1288
		switch (ie->data[0] & 0x03) {
1289
		case 0x00:
1289
		case 0x00:
1290
			/* No channel */
1290
			/* No channel */
1291
			call->channelno = 0;
1291
			call->channelno = 0;
1292
			call->chanflags = FLAG_PREFERRED;
1292
			call->chanflags = FLAG_PREFERRED;
1293
			break;
1293
			break;
1294
		case 0x03:
1294
		case 0x03:
1295
			/* Any channel */
1295
			/* Any channel */
1296
			call->chanflags = FLAG_PREFERRED;
1296
			call->chanflags = FLAG_PREFERRED;
1297
			break;
1297
			break;
1298
		default:
1298
		default:
1299
			/* Specified B channel (B1 or B2) */
1299
			/* Specified B channel (B1 or B2) */
1300
			call->channelno = ie->data[0] & 0x03;
1300
			call->channelno = ie->data[0] & 0x03;
1301
			break;
1301
			break;
1302
		}
1302
		}
1303
	}
1303
	}
1304

    
   
1304

   
1305
	pos++;
1305
	pos++;
1306
	if (ie->data[0] & 0x40) {
1306
	if (ie->data[0] & 0x40) {
1307
		/* DS1 specified -- stop here */
1307
		/* DS1 specified -- stop here */
1308
		call->ds1no = ie->data[1] & 0x7f;
1308
		call->ds1no = ie->data[1] & 0x7f;
1309
		call->ds1explicit = 1;
1309
		call->ds1explicit = 1;
1310
		pos++;
1310
		pos++;
1311
	} else {
1311
	} else {
1312
		call->ds1explicit = 0;
1312
		call->ds1explicit = 0;
1313
	}
1313
	}
1314

    
   
1314

   
1315
	if (ie->data[0] & 0x04) {
1315
	if (ie->data[0] & 0x04) {
1316
		/* D channel call.  Signaling only. */
1316
		/* D channel call.  Signaling only. */
1317
		call->cis_call = 1;
1317
		call->cis_call = 1;
1318
		call->chanflags = FLAG_EXCLUSIVE;/* For safety mark this channel as exclusive. */
1318
		call->chanflags = FLAG_EXCLUSIVE;/* For safety mark this channel as exclusive. */
1319
		call->channelno = 0;
1319
		call->channelno = 0;
1320
		return 0;
1320
		return 0;
1321
	}
1321
	}
1322

    
   
1322

   
1323
	if (need_extended_channel_octets && pos + 2 < len) {
1323
	if (need_extended_channel_octets && pos + 2 < len) {
1324
		/* More coming */
1324
		/* More coming */
1325
		if ((ie->data[pos] & 0x0f) != 3) {
1325
		if ((ie->data[pos] & 0x0f) != 3) {
1326
			/* Channel type/mapping is not for B channel units. */
1326
			/* Channel type/mapping is not for B channel units. */
1327
			pri_error(ctrl, "!! Unexpected Channel Type %d\n", ie->data[pos] & 0x0f);
1327
			pri_error(ctrl, "!! Unexpected Channel Type %d\n", ie->data[pos] & 0x0f);
1328
			return -1;
1328
			return -1;
1329
		}
1329
		}
1330
		if ((ie->data[pos] & 0x60) != 0) {
1330
		if ((ie->data[pos] & 0x60) != 0) {
1331
			pri_error(ctrl, "!! Invalid CCITT coding %d\n", (ie->data[pos] & 0x60) >> 5);
1331
			pri_error(ctrl, "!! Invalid CCITT coding %d\n", (ie->data[pos] & 0x60) >> 5);
1332
			return -1;
1332
			return -1;
1333
		}
1333
		}
1334
		if (ie->data[pos] & 0x10) {
1334
		if (ie->data[pos] & 0x10) {
1335
			/* Expect Slot Map */
1335
			/* Expect Slot Map */
1336
			call->slotmap = 0;
1336
			call->slotmap = 0;
1337
			pos++;
1337
			pos++;
1338
			call->slotmap_size = (ie->len - pos > 3) ? 1 : 0;
1338
			call->slotmap_size = (ie->len - pos > 3) ? 1 : 0;
1339
			for (x = 0; x < (call->slotmap_size ? 4 : 3); ++x) {
1339
			for (x = 0; x < (call->slotmap_size ? 4 : 3); ++x) {
1340
				call->slotmap <<= 8;
1340
				call->slotmap <<= 8;
1341
				call->slotmap |= ie->data[x + pos];
1341
				call->slotmap |= ie->data[x + pos];
1342
			}
1342
			}
1343

    
   
1343

   
1344
			if (msgtype == Q931_RESTART) {
1344
			if (msgtype == Q931_RESTART) {
1345
				int bit;
1345
				int bit;
1346

    
   
1346

   
1347
				/* Convert the slotmap to a channel list for RESTART support. */
1347
				/* Convert the slotmap to a channel list for RESTART support. */
1348
				for (bit = 0; bit < ARRAY_LEN(call->restart.chan_no); ++bit) {
1348
				for (bit = 0; bit < ARRAY_LEN(call->restart.chan_no); ++bit) {
1349
					if (call->slotmap & (1UL << bit)) {
1349
					if (call->slotmap & (1UL << bit)) {
1350
						call->restart.chan_no[call->restart.count++] = bit
1350
						call->restart.chan_no[call->restart.count++] = bit
1351
							+ (call->slotmap_size ? 0 : 1);
1351
							+ (call->slotmap_size ? 0 : 1);
1352
					}
1352
					}
1353
				}
1353
				}
1354
			}
1354
			}
1355
		} else {
1355
		} else {
1356
			pos++;
1356
			pos++;
1357
			/* Only expect a particular channel */
1357
			/* Only expect a particular channel */
1358
			call->channelno = ie->data[pos] & 0x7f;
1358
			call->channelno = ie->data[pos] & 0x7f;
1359
			if (ctrl->chan_mapping_logical && call->channelno > 15) {
1359
			if (ctrl->chan_mapping_logical && call->channelno > 15) {
1360
				call->channelno++;
1360
				call->channelno++;
1361
			}
1361
			}
1362

    
   
1362

   
1363
			if (msgtype == Q931_RESTART) {
1363
			if (msgtype == Q931_RESTART) {
1364
				/* Read in channel list for RESTART support. */
1364
				/* Read in channel list for RESTART support. */
1365
				while (call->restart.count < ARRAY_LEN(call->restart.chan_no)) {
1365
				while (call->restart.count < ARRAY_LEN(call->restart.chan_no)) {
1366
					int chan_no;
1366
					int chan_no;
1367
	
1367
	
1368
					chan_no = ie->data[pos] & 0x7f;
1368
					chan_no = ie->data[pos] & 0x7f;
1369
					if (ctrl->chan_mapping_logical && chan_no > 15) {
1369
					if (ctrl->chan_mapping_logical && chan_no > 15) {
1370
						++chan_no;
1370
						++chan_no;
1371
					}
1371
					}
1372
					call->restart.chan_no[call->restart.count++] = chan_no;
1372
					call->restart.chan_no[call->restart.count++] = chan_no;
1373
					if (ie->data[pos++] & 0x80) {
1373
					if (ie->data[pos++] & 0x80) {
1374
						/* Channel list finished. */
1374
						/* Channel list finished. */
1375
						break;
1375
						break;
1376
					}
1376
					}
1377
					if (ie->len <= pos) {
1377
					if (ie->len <= pos) {
1378
						/* No more ie contents. */
1378
						/* No more ie contents. */
1379
						break;
1379
						break;
1380
					}
1380
					}
1381
				}
1381
				}
1382
			}
1382
			}
1383
		}
1383
		}
1384
	}
1384
	}
1385
	return 0;
1385
	return 0;
1386
}
1386
}
1387

    
   
1387

   
1388
static int transmit_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1388
static int transmit_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1389
{
1389
{
1390
	int pos = 0;
1390
	int pos = 0;
1391

    
   
1391

   
1392
	/* We are ready to transmit single IE only */
1392
	/* We are ready to transmit single IE only */
1393
	if (order > 1)
1393
	if (order > 1)
1394
		return 0;
1394
		return 0;
1395

    
   
1395

   
1396
	if (call->cis_call) {
1396
	if (call->cis_call) {
1397
		/*
1397
		/*
1398
		 * Read the standards docs to figure this out.
1398
		 * Read the standards docs to figure this out.
1399
		 * Q.SIG ECMA-165 section 7.3
1399
		 * Q.SIG ECMA-165 section 7.3
1400
		 * ITU Q.931 section 4.5.13
1400
		 * ITU Q.931 section 4.5.13
1401
		 */
1401
		 */
1402
		ie->data[pos++] = ctrl->bri ? 0x8c : 0xac;
1402
		ie->data[pos++] = ctrl->bri ? 0x8c : 0xac;
1403
		return pos + 2;
1403
		return pos + 2;
1404
	}
1404
	}
1405

    
   
1405

   
1406
	/* Start with standard stuff */
1406
	/* Start with standard stuff */
1407
	if (ctrl->switchtype == PRI_SWITCH_GR303_TMC)
1407
	if (ctrl->switchtype == PRI_SWITCH_GR303_TMC)
1408
		ie->data[pos] = 0x69;
1408
		ie->data[pos] = 0x69;
1409
	else if (ctrl->bri) {
1409
	else if (ctrl->bri) {
1410
		ie->data[pos] = 0x80;
1410
		ie->data[pos] = 0x80;
1411
		ie->data[pos] |= (call->channelno & 0x3);
1411
		ie->data[pos] |= (call->channelno & 0x3);
1412
	} else {
1412
	} else {
1413
		/* PRI */
1413
		/* PRI */
1414
		if (call->slotmap != -1 || (call->chanflags & FLAG_WHOLE_INTERFACE)) {
1414
		if (call->slotmap != -1 || (call->chanflags & FLAG_WHOLE_INTERFACE)) {
1415
			/* Specified channel */
1415
			/* Specified channel */
1416
			ie->data[pos] = 0xa1;
1416
			ie->data[pos] = 0xa1;
1417
		} else if (call->channelno < 0 || call->channelno == 0xff) {
1417
		} else if (call->channelno < 0 || call->channelno == 0xff) {
1418
			/* Any channel */
1418
			/* Any channel */
1419
			ie->data[pos] = 0xa3;
1419
			ie->data[pos] = 0xa3;
1420
		} else if (!call->channelno) {
1420
		} else if (!call->channelno) {
1421
			/* No channel */
1421
			/* No channel */
1422
			ie->data[pos] = 0xa0;
1422
			ie->data[pos] = 0xa0;
1423
		} else {
1423
		} else {
1424
			/* Specified channel */
1424
			/* Specified channel */
1425
			ie->data[pos] = 0xa1;
1425
			ie->data[pos] = 0xa1;
1426
		}
1426
		}
1427
	}
1427
	}
1428
	if (call->chanflags & FLAG_EXCLUSIVE) {
1428
	if (call->chanflags & FLAG_EXCLUSIVE) {
1429
		/* Channel is exclusive */
1429
		/* Channel is exclusive */
1430
		if (!(ie->data[pos] & 0x03)) {
1430
		if (!(ie->data[pos] & 0x03)) {
1431
			/* An exclusive no channel id ie is to be discarded. */
1431
			/* An exclusive no channel id ie is to be discarded. */
1432
			return 0;
1432
			return 0;
1433
		}
1433
		}
1434
		ie->data[pos] |= 0x08;
1434
		ie->data[pos] |= 0x08;
1435
	} else if (!call->chanflags) {
1435
	} else if (!call->chanflags) {
1436
		/* Don't need this IE */
1436
		/* Don't need this IE */
1437
		return 0;
1437
		return 0;
1438
	}
1438
	}
1439

    
   
1439

   
1440
	if (!ctrl->bri && (((ctrl->switchtype != PRI_SWITCH_QSIG) && (call->ds1no > 0)) || call->ds1explicit)) {
1440
	if (!ctrl->bri && (((ctrl->switchtype != PRI_SWITCH_QSIG) && (call->ds1no > 0)) || call->ds1explicit)) {
1441
		/* We are specifying the interface.  Octet 3.1 */
1441
		/* We are specifying the interface.  Octet 3.1 */
1442
		ie->data[pos++] |= 0x40;
1442
		ie->data[pos++] |= 0x40;
1443
		ie->data[pos++] = 0x80 | call->ds1no;
1443
		ie->data[pos++] = 0x80 | call->ds1no;
1444
	} else {
1444
	} else {
1445
		++pos;
1445
		++pos;
1446
	}
1446
	}
1447

    
   
1447

   
1448
	if (!ctrl->bri && (ie->data[0] & 0x03) == 0x01 /* Specified channel */
1448
	if (!ctrl->bri && (ie->data[0] & 0x03) == 0x01 /* Specified channel */
1449
		&& !(call->chanflags & FLAG_WHOLE_INTERFACE)) {
1449
		&& !(call->chanflags & FLAG_WHOLE_INTERFACE)) {
1450
		/* The 3.2 and 3.3 octets need to be present */
1450
		/* The 3.2 and 3.3 octets need to be present */
1451
		ie->data[pos] = 0x83;
1451
		ie->data[pos] = 0x83;
1452
		if (0 < call->channelno && call->channelno != 0xff) {
1452
		if (0 < call->channelno && call->channelno != 0xff) {
1453
			/* Channel number specified and preferred over slot map if we have one. */
1453
			/* Channel number specified and preferred over slot map if we have one. */
1454
			++pos;
1454
			++pos;
1455
			if (msgtype == Q931_RESTART_ACKNOWLEDGE && call->restart.count) {
1455
			if (msgtype == Q931_RESTART_ACKNOWLEDGE && call->restart.count) {
1456
				int chan_no;
1456
				int chan_no;
1457
				int idx;
1457
				int idx;
1458

    
   
1458

   
1459
				/* Build RESTART_ACKNOWLEDGE channel list */
1459
				/* Build RESTART_ACKNOWLEDGE channel list */
1460
				for (idx = 0; idx < call->restart.count; ++idx) {
1460
				for (idx = 0; idx < call->restart.count; ++idx) {
1461
					chan_no = call->restart.chan_no[idx];
1461
					chan_no = call->restart.chan_no[idx];
1462
					if (ctrl->chan_mapping_logical && chan_no > 16) {
1462
					if (ctrl->chan_mapping_logical && chan_no > 16) {
1463
						--chan_no;
1463
						--chan_no;
1464
					}
1464
					}
1465
					if (call->restart.count <= idx + 1) {
1465
					if (call->restart.count <= idx + 1) {
1466
						/* Last channel list channel. */
1466
						/* Last channel list channel. */
1467
						chan_no |= 0x80;
1467
						chan_no |= 0x80;
1468
					}
1468
					}
1469
					ie->data[pos++] = chan_no;
1469
					ie->data[pos++] = chan_no;
1470
				}
1470
				}
1471
			} else {
1471
			} else {
1472
				if (ctrl->chan_mapping_logical && call->channelno > 16) {
1472
				if (ctrl->chan_mapping_logical && call->channelno > 16) {
1473
					ie->data[pos++] = 0x80 | (call->channelno - 1);
1473
					ie->data[pos++] = 0x80 | (call->channelno - 1);
1474
				} else {
1474
				} else {
1475
					ie->data[pos++] = 0x80 | call->channelno;
1475
					ie->data[pos++] = 0x80 | call->channelno;
1476
				}
1476
				}
1477
			}
1477
			}
1478
		} else if (call->slotmap != -1) {
1478
		} else if (call->slotmap != -1) {
1479
			int octet;
1479
			int octet;
1480

    
   
1480

   
1481
			/* We have to send a slot map */
1481
			/* We have to send a slot map */
1482
			ie->data[pos++] |= 0x10;
1482
			ie->data[pos++] |= 0x10;
1483
			for (octet = call->slotmap_size ? 4 : 3; octet--;) {
1483
			for (octet = call->slotmap_size ? 4 : 3; octet--;) {
1484
				ie->data[pos++] = (call->slotmap >> (8 * octet)) & 0xff;
1484
				ie->data[pos++] = (call->slotmap >> (8 * octet)) & 0xff;
1485
			}
1485
			}
1486
		} else {
1486
		} else {
1487
			pri_error(ctrl, "XXX We need either a channelno or slotmap but have neither!\n");
1487
			pri_error(ctrl, "XXX We need either a channelno or slotmap but have neither!\n");
1488
			/* Discard this malformed ie. */
1488
			/* Discard this malformed ie. */
1489
			return 0;
1489
			return 0;
1490
		}
1490
		}
1491
	}
1491
	}
1492

    
   
1492

   
1493
	return pos + 2;
1493
	return pos + 2;
1494
}
1494
}
1495

    
   
1495

   
1496
static void dump_channel_id(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1496
static void dump_channel_id(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1497
{
1497
{
1498
	int pos;
1498
	int pos;
1499
	int x;
1499
	int x;
1500
	int res;
1500
	int res;
1501

    
   
1501

   
1502
	static const char *msg_chan_sel[] = {
1502
	static const char *msg_chan_sel[] = {
1503
		"No channel selected", "B1 channel", "B2 channel", "Any channel selected",
1503
		"No channel selected", "B1 channel", "B2 channel", "Any channel selected",
1504
		"No channel selected", "As indicated in following octets", "Reserved", "Any channel selected"
1504
		"No channel selected", "As indicated in following octets", "Reserved", "Any channel selected"
1505
	};
1505
	};
1506

    
   
1506

   
1507
	pri_message(ctrl,
1507
	pri_message(ctrl,
1508
		"%c Channel ID (len=%2d) [ Ext: %d  IntID: %s  %s  Spare: %d  %s  Dchan: %d\n",
1508
		"%c Channel ID (len=%2d) [ Ext: %d  IntID: %s  %s  Spare: %d  %s  Dchan: %d\n",
1509
		prefix, len,
1509
		prefix, len,
1510
		(ie->data[0] & 0x80) ? 1 : 0,
1510
		(ie->data[0] & 0x80) ? 1 : 0,
1511
		(ie->data[0] & 0x40) ? "Explicit" : "Implicit",
1511
		(ie->data[0] & 0x40) ? "Explicit" : "Implicit",
1512
		(ie->data[0] & 0x20) ? "Other(PRI)" : "BRI",
1512
		(ie->data[0] & 0x20) ? "Other(PRI)" : "BRI",
1513
		(ie->data[0] & 0x10) ? 1 : 0,
1513
		(ie->data[0] & 0x10) ? 1 : 0,
1514
		(ie->data[0] & 0x08) ? "Exclusive" : "Preferred",
1514
		(ie->data[0] & 0x08) ? "Exclusive" : "Preferred",
1515
		(ie->data[0] & 0x04) ? 1 : 0);
1515
		(ie->data[0] & 0x04) ? 1 : 0);
1516
	pri_message(ctrl, "%c                       ChanSel: %s\n",
1516
	pri_message(ctrl, "%c                       ChanSel: %s\n",
1517
		prefix, msg_chan_sel[(ie->data[0] & 0x03) | ((ie->data[0] >> 3) & 0x04)]);
1517
		prefix, msg_chan_sel[(ie->data[0] & 0x03) | ((ie->data[0] >> 3) & 0x04)]);
1518
	pos = 1;
1518
	pos = 1;
1519
	len -= 2;
1519
	len -= 2;
1520
	if (ie->data[0] & 0x40) {
1520
	if (ie->data[0] & 0x40) {
1521
		/* Explicitly defined DS1 */
1521
		/* Explicitly defined DS1 */
1522
		do {
1522
		do {
1523
			pri_message(ctrl, "%c                       Ext: %d  DS1 Identifier: %d  \n",
1523
			pri_message(ctrl, "%c                       Ext: %d  DS1 Identifier: %d  \n",
1524
				prefix, (ie->data[pos] & 0x80) >> 7, ie->data[pos] & 0x7f);
1524
				prefix, (ie->data[pos] & 0x80) >> 7, ie->data[pos] & 0x7f);
1525
			++pos;
1525
			++pos;
1526
		} while (!(ie->data[pos - 1] & 0x80) && pos < len);
1526
		} while (!(ie->data[pos - 1] & 0x80) && pos < len);
1527
	} else {
1527
	} else {
1528
		/* Implicitly defined DS1 */
1528
		/* Implicitly defined DS1 */
1529
	}
1529
	}
1530
	if (pos < len) {
1530
	if (pos < len) {
1531
		/* Still more information here */
1531
		/* Still more information here */
1532
		pri_message(ctrl,
1532
		pri_message(ctrl,
1533
			"%c                       Ext: %d  Coding: %d  %s Specified  Channel Type: %d\n",
1533
			"%c                       Ext: %d  Coding: %d  %s Specified  Channel Type: %d\n",
1534
			prefix, (ie->data[pos] & 0x80) >> 7, (ie->data[pos] & 60) >> 5,
1534
			prefix, (ie->data[pos] & 0x80) >> 7, (ie->data[pos] & 60) >> 5,
1535
			(ie->data[pos] & 0x10) ? "Slot Map" : "Number", ie->data[pos] & 0x0f);
1535
			(ie->data[pos] & 0x10) ? "Slot Map" : "Number", ie->data[pos] & 0x0f);
1536
		++pos;
1536
		++pos;
1537
	}
1537
	}
1538
	if (pos < len) {
1538
	if (pos < len) {
1539
		if (!(ie->data[pos - 1] & 0x10)) {
1539
		if (!(ie->data[pos - 1] & 0x10)) {
1540
			/* Number specified */
1540
			/* Number specified */
1541
			do {
1541
			do {
1542
				pri_message(ctrl,
1542
				pri_message(ctrl,
1543
					"%c                       Ext: %d  Channel: %d Type: %s%c\n",
1543
					"%c                       Ext: %d  Channel: %d Type: %s%c\n",
1544
					prefix, (ie->data[pos] & 0x80) >> 7,
1544
					prefix, (ie->data[pos] & 0x80) >> 7,
1545
					(ie->data[pos]) & 0x7f, pritype(ctrl->localtype),
1545
					(ie->data[pos]) & 0x7f, pritype(ctrl->localtype),
1546
					(pos + 1 < len) ? ' ' : ']');
1546
					(pos + 1 < len) ? ' ' : ']');
1547
				++pos;
1547
				++pos;
1548
			} while (pos < len);
1548
			} while (pos < len);
1549
		} else {
1549
		} else {
1550
			/* Map specified */
1550
			/* Map specified */
1551
			res = 0;
1551
			res = 0;
1552
			x = 0;
1552
			x = 0;
1553
			do {
1553
			do {
1554
				res <<= 8;
1554
				res <<= 8;
1555
				res |= ie->data[pos++];
1555
				res |= ie->data[pos++];
1556
				++x;
1556
				++x;
1557
			} while (pos < len);
1557
			} while (pos < len);
1558
			pri_message(ctrl, "%c                       Map len: %d  Map: %s ]\n", prefix,
1558
			pri_message(ctrl, "%c                       Map len: %d  Map: %s ]\n", prefix,
1559
				x, binary(res, x << 3));
1559
				x, binary(res, x << 3));
1560
		}
1560
		}
1561
	} else {
1561
	} else {
1562
		pri_message(ctrl, "%c                     ]\n", prefix);
1562
		pri_message(ctrl, "%c                     ]\n", prefix);
1563
	}
1563
	}
1564
}
1564
}
1565

    
   
1565

   
1566
static char *ri2str(int ri)
1566
static char *ri2str(int ri)
1567
{
1567
{
1568
	static struct msgtype ris[] = {
1568
	static struct msgtype ris[] = {
1569
		{ 0, "Indicated Channel" },
1569
		{ 0, "Indicated Channel" },
1570
		{ 6, "Single DS1 Facility" },
1570
		{ 6, "Single DS1 Facility" },
1571
		{ 7, "All DS1 Facilities" },
1571
		{ 7, "All DS1 Facilities" },
1572
	};
1572
	};
1573
	return code2str(ri, ris, sizeof(ris) / sizeof(ris[0]));
1573
	return code2str(ri, ris, sizeof(ris) / sizeof(ris[0]));
1574
}
1574
}
1575

    
   
1575

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

    
   
1580
		prefix, ie2str(full_ie), len, (ie->data[0] & 0x80) >> 7,

    
   
1581
		(ie->data[0] & 0x78) >> 3, ri2str(ie->data[0] & 0x7), ie->data[0] & 0x7);
1580
}
1582
}
1581

    
   
1583

   
1582
static int receive_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1584
static int receive_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1583
{
1585
{
1584
	/* Pretty simple */
1586
	/* Pretty simple */
1585
	call->ri = ie->data[0] & 0x7;
1587
	call->ri = ie->data[0] & 0x7;
1586
	return 0;
1588
	return 0;
1587
}
1589
}
1588

    
   
1590

   
1589
static int transmit_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1591
static int transmit_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1590
{
1592
{
1591
	/* Pretty simple */
1593
	/* Pretty simple */
1592
	switch(call->ri) {
1594
	switch(call->ri) {
1593
	case 0:
1595
	case 0:
1594
	case 6:
1596
	case 6:
1595
	case 7:
1597
	case 7:
1596
		ie->data[0] = 0x80 | (call->ri & 0x7);
1598
		ie->data[0] = 0x80 | (call->ri & 0x7);
1597
		break;
1599
		break;
1598
	case 5:
1600
	case 5:
1599
		/* Switch compatibility */
1601
		/* Switch compatibility */
1600
		ie->data[0] = 0xA0 | (call->ri & 0x7);
1602
		ie->data[0] = 0xA0 | (call->ri & 0x7);
1601
		break;
1603
		break;
1602
	default:
1604
	default:
1603
		pri_error(ctrl, "!! Invalid restart indicator value %d\n", call->ri);
1605
		pri_error(ctrl, "!! Invalid restart indicator value %d\n", call->ri);
1604
		return-1;
1606
		return-1;
1605
	}
1607
	}
1606
	return 3;
1608
	return 3;
1607
}
1609
}
1608

    
   
1610

   
1609
static char *redirection_reason2str(int mode)
1611
static char *redirection_reason2str(int mode)
1610
{
1612
{
1611
	static struct msgtype modes[] = {
1613
	static struct msgtype modes[] = {
1612
		{ PRI_REDIR_UNKNOWN, "Unknown" },
1614
		{ PRI_REDIR_UNKNOWN, "Unknown" },
1613
		{ PRI_REDIR_FORWARD_ON_BUSY, "Forwarded on busy" },
1615
		{ PRI_REDIR_FORWARD_ON_BUSY, "Forwarded on busy" },
1614
		{ PRI_REDIR_FORWARD_ON_NO_REPLY, "Forwarded on no reply" },
1616
		{ PRI_REDIR_FORWARD_ON_NO_REPLY, "Forwarded on no reply" },
1615
		{ PRI_REDIR_DEFLECTION, "Call deflected" },
1617
		{ PRI_REDIR_DEFLECTION, "Call deflected" },
1616
		{ PRI_REDIR_DTE_OUT_OF_ORDER, "Called DTE out of order" },
1618
		{ PRI_REDIR_DTE_OUT_OF_ORDER, "Called DTE out of order" },
1617
		{ PRI_REDIR_FORWARDED_BY_DTE, "Forwarded by called DTE" },
1619
		{ PRI_REDIR_FORWARDED_BY_DTE, "Forwarded by called DTE" },
1618
		{ PRI_REDIR_UNCONDITIONAL, "Forwarded unconditionally" },
1620
		{ PRI_REDIR_UNCONDITIONAL, "Forwarded unconditionally" },
1619
	};
1621
	};
1620
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1622
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1621
}
1623
}
1622

    
   
1624

   
1623
static char *cap2str(int mode)
1625
static char *cap2str(int mode)
1624
{
1626
{
1625
	static struct msgtype modes[] = {
1627
	static struct msgtype modes[] = {
1626
		{ PRI_TRANS_CAP_SPEECH, "Speech" },
1628
		{ PRI_TRANS_CAP_SPEECH, "Speech" },
1627
		{ PRI_TRANS_CAP_DIGITAL, "Unrestricted digital information" },
1629
		{ PRI_TRANS_CAP_DIGITAL, "Unrestricted digital information" },
1628
		{ PRI_TRANS_CAP_RESTRICTED_DIGITAL, "Restricted digital information" },
1630
		{ PRI_TRANS_CAP_RESTRICTED_DIGITAL, "Restricted digital information" },
1629
		{ PRI_TRANS_CAP_3_1K_AUDIO, "3.1kHz audio" },
1631
		{ PRI_TRANS_CAP_3_1K_AUDIO, "3.1kHz audio" },
1630
		{ PRI_TRANS_CAP_DIGITAL_W_TONES, "Unrestricted digital information with tones/announcements" },
1632
		{ PRI_TRANS_CAP_DIGITAL_W_TONES, "Unrestricted digital information with tones/announcements" },
1631
		{ PRI_TRANS_CAP_VIDEO, "Video" },
1633
		{ PRI_TRANS_CAP_VIDEO, "Video" },
1632
		{ PRI_TRANS_CAP_AUDIO_4ESS, "3.1khz audio (4ESS)" },
1634
		{ PRI_TRANS_CAP_AUDIO_4ESS, "3.1khz audio (4ESS)" },
1633
	};
1635
	};
1634
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1636
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1635
}
1637
}
1636

    
   
1638

   
1637
static char *mode2str(int mode)
1639
static char *mode2str(int mode)
1638
{
1640
{
1639
	static struct msgtype modes[] = {
1641
	static struct msgtype modes[] = {
1640
		{ TRANS_MODE_64_CIRCUIT, "64kbps, circuit-mode" },
1642
		{ TRANS_MODE_64_CIRCUIT, "64kbps, circuit-mode" },
1641
		{ TRANS_MODE_2x64_CIRCUIT, "2x64kbps, circuit-mode" },
1643
		{ TRANS_MODE_2x64_CIRCUIT, "2x64kbps, circuit-mode" },
1642
		{ TRANS_MODE_384_CIRCUIT, "384kbps, circuit-mode" },
1644
		{ TRANS_MODE_384_CIRCUIT, "384kbps, circuit-mode" },
1643
		{ TRANS_MODE_1536_CIRCUIT, "1536kbps, circuit-mode" },
1645
		{ TRANS_MODE_1536_CIRCUIT, "1536kbps, circuit-mode" },
1644
		{ TRANS_MODE_1920_CIRCUIT, "1920kbps, circuit-mode" },
1646
		{ TRANS_MODE_1920_CIRCUIT, "1920kbps, circuit-mode" },
1645
		{ TRANS_MODE_MULTIRATE, "Multirate (Nx64kbps)" },
1647
		{ TRANS_MODE_MULTIRATE, "Multirate (Nx64kbps)" },
1646
		{ TRANS_MODE_PACKET, "Packet Mode" },
1648
		{ TRANS_MODE_PACKET, "Packet Mode" },
1647
	};
1649
	};
1648
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1650
	return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
1649
}
1651
}
1650

    
   
1652

   
1651
static char *l12str(int proto)
1653
static char *l12str(int proto)
1652
{
1654
{
1653
	static struct msgtype protos[] = {
1655
	static struct msgtype protos[] = {
1654
 		{ PRI_LAYER_1_ITU_RATE_ADAPT, "V.110 Rate Adaption" },
1656
 		{ PRI_LAYER_1_ITU_RATE_ADAPT, "V.110 Rate Adaption" },
1655
		{ PRI_LAYER_1_ULAW, "u-Law" },
1657
		{ PRI_LAYER_1_ULAW, "u-Law" },
1656
		{ PRI_LAYER_1_ALAW, "A-Law" },
1658
		{ PRI_LAYER_1_ALAW, "A-Law" },
1657
		{ PRI_LAYER_1_G721, "G.721 ADPCM" },
1659
		{ PRI_LAYER_1_G721, "G.721 ADPCM" },
1658
		{ PRI_LAYER_1_G722_G725, "G.722/G.725 7kHz Audio" },
1660
		{ PRI_LAYER_1_G722_G725, "G.722/G.725 7kHz Audio" },
1659
 		{ PRI_LAYER_1_H223_H245, "H.223/H.245 Multimedia" },
1661
 		{ PRI_LAYER_1_H223_H245, "H.223/H.245 Multimedia" },
1660
		{ PRI_LAYER_1_NON_ITU_ADAPT, "Non-ITU Rate Adaption" },
1662
		{ PRI_LAYER_1_NON_ITU_ADAPT, "Non-ITU Rate Adaption" },
1661
		{ PRI_LAYER_1_V120_RATE_ADAPT, "V.120 Rate Adaption" },
1663
		{ PRI_LAYER_1_V120_RATE_ADAPT, "V.120 Rate Adaption" },
1662
		{ PRI_LAYER_1_X31_RATE_ADAPT, "X.31 Rate Adaption" },
1664
		{ PRI_LAYER_1_X31_RATE_ADAPT, "X.31 Rate Adaption" },
1663
	};
1665
	};
1664
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1666
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1665
}
1667
}
1666

    
   
1668

   
1667
static char *ra2str(int proto)
1669
static char *ra2str(int proto)
1668
{
1670
{
1669
	static struct msgtype protos[] = {
1671
	static struct msgtype protos[] = {
1670
		{ PRI_RATE_ADAPT_9K6, "9.6 kbit/s" },
1672
		{ PRI_RATE_ADAPT_9K6, "9.6 kbit/s" },
1671
	};
1673
	};
1672
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1674
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1673
}
1675
}
1674

    
   
1676

   
1675
static char *l22str(int proto)
1677
static char *l22str(int proto)
1676
{
1678
{
1677
	static struct msgtype protos[] = {
1679
	static struct msgtype protos[] = {
1678
		{ LAYER_2_LAPB, "LAPB" },
1680
		{ LAYER_2_LAPB, "LAPB" },
1679
	};
1681
	};
1680
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1682
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1681
}
1683
}
1682

    
   
1684

   
1683
static char *l32str(int proto)
1685
static char *l32str(int proto)
1684
{
1686
{
1685
	static struct msgtype protos[] = {
1687
	static struct msgtype protos[] = {
1686
		{ LAYER_3_X25, "X.25" },
1688
		{ LAYER_3_X25, "X.25" },
1687
	};
1689
	};
1688
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1690
	return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1689
}
1691
}
1690

    
   
1692

   
1691
static char *int_rate2str(int proto)
1693
static char *int_rate2str(int proto)
1692
{
1694
{
1693
    static struct msgtype protos[] = {
1695
    static struct msgtype protos[] = {
1694
		{ PRI_INT_RATE_8K, "8 kbit/s" },
1696
		{ PRI_INT_RATE_8K, "8 kbit/s" },
1695
		{ PRI_INT_RATE_16K, "16 kbit/s" },
1697
		{ PRI_INT_RATE_16K, "16 kbit/s" },
1696
		{ PRI_INT_RATE_32K, "32 kbit/s" },
1698
		{ PRI_INT_RATE_32K, "32 kbit/s" },
1697
    };
1699
    };
1698
    return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1700
    return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
1699
}
1701
}
1700

    
   
1702

   
1701
static void dump_bearer_capability(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1703
static void dump_bearer_capability(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
1702
{
1704
{
1703
	int pos=2;
1705
	int pos=2;
1704
	pri_message(ctrl, "%c Bearer Capability (len=%2d) [ Ext: %d  Coding-Std: %d  Info transfer capability: %s (%d)\n",
1706
	pri_message(ctrl,
1705
		prefix, len, (ie->data[0] & 0x80 ) >> 7, (ie->data[0] & 0x60) >> 5, cap2str(ie->data[0] & 0x1f), (ie->data[0] & 0x1f));
1707
		"%c %s (len=%2d) [ Ext: %d  Coding-Std: %d  Info transfer capability: %s (%d)\n",

    
   
1708
		prefix, ie2str(full_ie), len, (ie->data[0] & 0x80 ) >> 7,

    
   
1709
		(ie->data[0] & 0x60) >> 5, cap2str(ie->data[0] & 0x1f),

    
   
1710
		(ie->data[0] & 0x1f));
1706
	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);
1711
	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);
1707

    
   
1712

   
1708
	/* octet 4.1 exists iff mode/rate is multirate */
1713
	/* octet 4.1 exists iff mode/rate is multirate */
1709
	if ((ie->data[1] & 0x7f) == 0x18) {
1714
	if ((ie->data[1] & 0x7f) == 0x18) {
1710
	    pri_message(ctrl, "%c                              Ext: %d  Transfer rate multiplier: %d x 64\n", prefix, (ie->data[2] & 0x80) >> 7, ie->data[2] & 0x7f);
1715
	    pri_message(ctrl, "%c                              Ext: %d  Transfer rate multiplier: %d x 64\n", prefix, (ie->data[2] & 0x80) >> 7, ie->data[2] & 0x7f);
1711
		pos++;
1716
		pos++;
1712
	}
1717
	}
1713

    
   
1718

   
1714
	/* don't count the IE num and length as part of the data */
1719
	/* don't count the IE num and length as part of the data */
1715
	len -= 2;
1720
	len -= 2;
1716
	
1721
	
1717
	/* Look for octet 5; this is identified by bits 5,6 == 01 */
1722
	/* Look for octet 5; this is identified by bits 5,6 == 01 */
1718
     	if (pos < len &&
1723
     	if (pos < len &&
1719
		(ie->data[pos] & 0x60) == 0x20) {
1724
		(ie->data[pos] & 0x60) == 0x20) {
1720

    
   
1725

   
1721
		/* although the layer1 is only the bottom 5 bits of the byte,
1726
		/* although the layer1 is only the bottom 5 bits of the byte,
1722
		   previous versions of this library passed bits 5&6 through
1727
		   previous versions of this library passed bits 5&6 through
1723
		   too, so we have to do the same for binary compatability */
1728
		   too, so we have to do the same for binary compatability */
1724
		u_int8_t layer1 = ie->data[pos] & 0x7f;
1729
		u_int8_t layer1 = ie->data[pos] & 0x7f;
1725

    
   
1730

   
1726
		pri_message(ctrl, "%c                                User information layer 1: %s (%d)\n",
1731
		pri_message(ctrl, "%c                                User information layer 1: %s (%d)\n",
1727
		            prefix, l12str(layer1), layer1);
1732
		            prefix, l12str(layer1), layer1);
1728
		pos++;
1733
		pos++;
1729
		
1734
		
1730
		/* octet 5a? */
1735
		/* octet 5a? */
1731
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1736
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1732
			int ra = ie->data[pos] & 0x7f;
1737
			int ra = ie->data[pos] & 0x7f;
1733

    
   
1738

   
1734
			pri_message(ctrl, "%c                                Async: %d, Negotiation: %d, "
1739
			pri_message(ctrl, "%c                                Async: %d, Negotiation: %d, "
1735
				"User rate: %s (%#x)\n", 
1740
				"User rate: %s (%#x)\n", 
1736
				prefix,
1741
				prefix,
1737
				ra & PRI_RATE_ADAPT_ASYNC ? 1 : 0,
1742
				ra & PRI_RATE_ADAPT_ASYNC ? 1 : 0,
1738
				ra & PRI_RATE_ADAPT_NEGOTIATION_POSS ? 1 : 0,
1743
				ra & PRI_RATE_ADAPT_NEGOTIATION_POSS ? 1 : 0,
1739
				ra2str(ra & PRI_RATE_USER_RATE_MASK),
1744
				ra2str(ra & PRI_RATE_USER_RATE_MASK),
1740
				ra & PRI_RATE_USER_RATE_MASK);
1745
				ra & PRI_RATE_USER_RATE_MASK);
1741
			pos++;
1746
			pos++;
1742
		}
1747
		}
1743
		
1748
		
1744
		/* octet 5b? */
1749
		/* octet 5b? */
1745
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1750
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1746
			u_int8_t data = ie->data[pos];
1751
			u_int8_t data = ie->data[pos];
1747
			if (layer1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1752
			if (layer1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1748
				pri_message(ctrl, "%c                                Intermediate rate: %s (%d), "
1753
				pri_message(ctrl, "%c                                Intermediate rate: %s (%d), "
1749
					"NIC on Tx: %d, NIC on Rx: %d, "
1754
					"NIC on Tx: %d, NIC on Rx: %d, "
1750
					"Flow control on Tx: %d, "
1755
					"Flow control on Tx: %d, "
1751
					"Flow control on Rx: %d\n",
1756
					"Flow control on Rx: %d\n",
1752
					prefix, int_rate2str((data & 0x60)>>5),
1757
					prefix, int_rate2str((data & 0x60)>>5),
1753
					(data & 0x60)>>5,
1758
					(data & 0x60)>>5,
1754
					(data & 0x10)?1:0,
1759
					(data & 0x10)?1:0,
1755
					(data & 0x08)?1:0,
1760
					(data & 0x08)?1:0,
1756
					(data & 0x04)?1:0,
1761
					(data & 0x04)?1:0,
1757
					(data & 0x02)?1:0);
1762
					(data & 0x02)?1:0);
1758
			} else if (layer1 == PRI_LAYER_1_V120_RATE_ADAPT) {
1763
			} else if (layer1 == PRI_LAYER_1_V120_RATE_ADAPT) {
1759
				pri_message(ctrl, "%c                                Hdr: %d, Multiframe: %d, Mode: %d, "
1764
				pri_message(ctrl, "%c                                Hdr: %d, Multiframe: %d, Mode: %d, "
1760
					"LLI negot: %d, Assignor: %d, "
1765
					"LLI negot: %d, Assignor: %d, "
1761
					"In-band neg: %d\n", prefix,
1766
					"In-band neg: %d\n", prefix,
1762
					(data & 0x40)?1:0,
1767
					(data & 0x40)?1:0,
1763
					(data & 0x20)?1:0,
1768
					(data & 0x20)?1:0,
1764
					(data & 0x10)?1:0,
1769
					(data & 0x10)?1:0,
1765
					(data & 0x08)?1:0,
1770
					(data & 0x08)?1:0,
1766
					(data & 0x04)?1:0,
1771
					(data & 0x04)?1:0,
1767
					(data & 0x02)?1:0);
1772
					(data & 0x02)?1:0);
1768
			} else {
1773
			} else {
1769
				pri_message(ctrl, "%c                                Unknown octet 5b: 0x%x\n",
1774
				pri_message(ctrl, "%c                                Unknown octet 5b: 0x%x\n",
1770
					prefix, data);
1775
					prefix, data);
1771
			}
1776
			}
1772
			pos++;
1777
			pos++;
1773
		}
1778
		}
1774

    
   
1779

   
1775
		/* octet 5c? */
1780
		/* octet 5c? */
1776
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1781
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1777
			u_int8_t data = ie->data[pos];
1782
			u_int8_t data = ie->data[pos];
1778
			const char *stop_bits[] = {"?","1","1.5","2"};
1783
			const char *stop_bits[] = {"?","1","1.5","2"};
1779
			const char *data_bits[] = {"?","5","7","8"};
1784
			const char *data_bits[] = {"?","5","7","8"};
1780
			const char *parity[] = {"Odd","?","Even","None",
1785
			const char *parity[] = {"Odd","?","Even","None",
1781
				       "zero","one","?","?"};
1786
				       "zero","one","?","?"};
1782
	
1787
	
1783
			pri_message(ctrl, "%c                                Stop bits: %s, data bits: %s, "
1788
			pri_message(ctrl, "%c                                Stop bits: %s, data bits: %s, "
1784
			    "parity: %s\n", prefix,
1789
			    "parity: %s\n", prefix,
1785
			    stop_bits[(data & 0x60) >> 5],
1790
			    stop_bits[(data & 0x60) >> 5],
1786
			    data_bits[(data & 0x18) >> 3],
1791
			    data_bits[(data & 0x18) >> 3],
1787
			    parity[(data & 0x7)]);
1792
			    parity[(data & 0x7)]);
1788
	
1793
	
1789
			pos++;
1794
			pos++;
1790
		}
1795
		}
1791
	
1796
	
1792
			/* octet 5d? */
1797
			/* octet 5d? */
1793
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1798
		if (pos < len && !(ie->data[pos-1] & 0x80)) {
1794
			u_int8_t data = ie->data[pos];
1799
			u_int8_t data = ie->data[pos];
1795
			pri_message(ctrl, "%c                                Duplex mode: %d, modem type: %d\n",
1800
			pri_message(ctrl, "%c                                Duplex mode: %d, modem type: %d\n",
1796
				prefix, (data & 0x40) ? 1 : 0,data & 0x3F);
1801
				prefix, (data & 0x40) ? 1 : 0,data & 0x3F);
1797
 			pos++;
1802
 			pos++;
1798
		}
1803
		}
1799
 	}
1804
 	}
1800

    
   
1805

   
1801

    
   
1806

   
1802
	/* Look for octet 6; this is identified by bits 5,6 == 10 */
1807
	/* Look for octet 6; this is identified by bits 5,6 == 10 */
1803
	if (pos < len && 
1808
	if (pos < len && 
1804
		(ie->data[pos] & 0x60) == 0x40) {
1809
		(ie->data[pos] & 0x60) == 0x40) {
1805
		pri_message(ctrl, "%c                                User information layer 2: %s (%d)\n",
1810
		pri_message(ctrl, "%c                                User information layer 2: %s (%d)\n",
1806
			prefix, l22str(ie->data[pos] & 0x1f),
1811
			prefix, l22str(ie->data[pos] & 0x1f),
1807
			ie->data[pos] & 0x1f);
1812
			ie->data[pos] & 0x1f);
1808
		pos++;
1813
		pos++;
1809
	}
1814
	}
1810

    
   
1815

   
1811
	/* Look for octet 7; this is identified by bits 5,6 == 11 */
1816
	/* Look for octet 7; this is identified by bits 5,6 == 11 */
1812
	if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
1817
	if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
1813
		pri_message(ctrl, "%c                                User information layer 3: %s (%d)\n",
1818
		pri_message(ctrl, "%c                                User information layer 3: %s (%d)\n",
1814
			prefix, l32str(ie->data[pos] & 0x1f),
1819
			prefix, l32str(ie->data[pos] & 0x1f),
1815
			ie->data[pos] & 0x1f);
1820
			ie->data[pos] & 0x1f);
1816
		pos++;
1821
		pos++;
1817

    
   
1822

   
1818
		/* octets 7a and 7b? */
1823
		/* octets 7a and 7b? */
1819
		if (pos + 1 < len && !(ie->data[pos-1] & 0x80) &&
1824
		if (pos + 1 < len && !(ie->data[pos-1] & 0x80) &&
1820
			!(ie->data[pos] & 0x80)) {
1825
			!(ie->data[pos] & 0x80)) {
1821
			unsigned int proto;
1826
			unsigned int proto;
1822
			proto = ((ie->data[pos] & 0xF) << 4 ) | 
1827
			proto = ((ie->data[pos] & 0xF) << 4 ) | 
1823
			         (ie->data[pos+1] & 0xF);
1828
			         (ie->data[pos+1] & 0xF);
1824

    
   
1829

   
1825
			pri_message(ctrl, "%c                                Network layer: 0x%x\n", prefix,
1830
			pri_message(ctrl, "%c                                Network layer: 0x%x\n", prefix,
1826
			            proto );
1831
			            proto );
1827
			pos += 2;
1832
			pos += 2;
1828
		}
1833
		}
1829
	}
1834
	}
1830
}
1835
}
1831

    
   
1836

   
1832
static int receive_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1837
static int receive_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
1833
{
1838
{
1834
	int pos = 2;
1839
	int pos = 2;
1835

    
   
1840

   
1836
	switch (ie->data[0] & 0x60) {
1841
	switch (ie->data[0] & 0x60) {
1837
	case 0x00:/* ITU-T standardized coding */
1842
	case 0x00:/* ITU-T standardized coding */
1838
		call->bc.transcapability = ie->data[0] & 0x1f;
1843
		call->bc.transcapability = ie->data[0] & 0x1f;
1839
		call->bc.transmoderate = ie->data[1] & 0x7f;
1844
		call->bc.transmoderate = ie->data[1] & 0x7f;
1840

    
   
1845

   
1841
		/* octet 4.1 exists iff mode/rate is multirate */
1846
		/* octet 4.1 exists iff mode/rate is multirate */
1842
		if (call->bc.transmoderate == TRANS_MODE_MULTIRATE) {
1847
		if (call->bc.transmoderate == TRANS_MODE_MULTIRATE) {
1843
			call->bc.transmultiple = ie->data[pos++] & 0x7f;
1848
			call->bc.transmultiple = ie->data[pos++] & 0x7f;
1844
		}
1849
		}
1845

    
   
1850

   
1846
		/* Look for octet 5; this is identified by bits 5,6 == 01 */
1851
		/* Look for octet 5; this is identified by bits 5,6 == 01 */
1847
		if (pos < len && (ie->data[pos] & 0x60) == 0x20) {
1852
		if (pos < len && (ie->data[pos] & 0x60) == 0x20) {
1848
			/* although the layer1 is only the bottom 5 bits of the byte,
1853
			/* although the layer1 is only the bottom 5 bits of the byte,
1849
			   previous versions of this library passed bits 5&6 through
1854
			   previous versions of this library passed bits 5&6 through
1850
			   too, so we have to do the same for binary compatability */
1855
			   too, so we have to do the same for binary compatability */
1851
			call->bc.userl1 = ie->data[pos] & 0x7f;
1856
			call->bc.userl1 = ie->data[pos] & 0x7f;
1852
			pos++;
1857
			pos++;
1853

    
   
1858

   
1854
			/* octet 5a? */
1859
			/* octet 5a? */
1855
			if (pos < len && !(ie->data[pos-1] & 0x80)) {
1860
			if (pos < len && !(ie->data[pos-1] & 0x80)) {
1856
				call->bc.rateadaption = ie->data[pos] & 0x7f;
1861
				call->bc.rateadaption = ie->data[pos] & 0x7f;
1857
				pos++;
1862
				pos++;
1858
			}
1863
			}
1859

    
   
1864

   
1860
			/* octets 5b through 5d? */
1865
			/* octets 5b through 5d? */
1861
			while (pos < len && !(ie->data[pos-1] & 0x80)) {
1866
			while (pos < len && !(ie->data[pos-1] & 0x80)) {
1862
				pos++;
1867
				pos++;
1863
			}
1868
			}
1864

    
   
1869

   
1865
		}
1870
		}
1866

    
   
1871

   
1867
		/* Look for octet 6; this is identified by bits 5,6 == 10 */
1872
		/* Look for octet 6; this is identified by bits 5,6 == 10 */
1868
		if (pos < len && (ie->data[pos] & 0x60) == 0x40) {
1873
		if (pos < len && (ie->data[pos] & 0x60) == 0x40) {
1869
			call->bc.userl2 = ie->data[pos++] & 0x1f;
1874
			call->bc.userl2 = ie->data[pos++] & 0x1f;
1870
		}
1875
		}
1871

    
   
1876

   
1872
		/* Look for octet 7; this is identified by bits 5,6 == 11 */
1877
		/* Look for octet 7; this is identified by bits 5,6 == 11 */
1873
		if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
1878
		if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
1874
			call->bc.userl3 = ie->data[pos++] & 0x1f;
1879
			call->bc.userl3 = ie->data[pos++] & 0x1f;
1875
		}
1880
		}
1876
		break;
1881
		break;
1877
	case 0x20:/* ISO/IEC standard */
1882
	case 0x20:/* ISO/IEC standard */
1878
		if (ie->data[0] == 0xa8 && ie->data[1] == 0x80) {
1883
		if (ie->data[0] == 0xa8 && ie->data[1] == 0x80) {
1879
			/*
1884
			/*
1880
			 * Q.SIG uses for CIS calls. ECMA-165 Section 11.3.1
1885
			 * Q.SIG uses for CIS calls. ECMA-165 Section 11.3.1
1881
			 * This mandatory ie is more or less a place holder in this case.
1886
			 * This mandatory ie is more or less a place holder in this case.
1882
			 */
1887
			 */
1883
			call->bc.transcapability = PRI_TRANS_CAP_DIGITAL;
1888
			call->bc.transcapability = PRI_TRANS_CAP_DIGITAL;
1884
			call->bc.transmoderate = TRANS_MODE_64_CIRCUIT;
1889
			call->bc.transmoderate = TRANS_MODE_64_CIRCUIT;
1885
			break;
1890
			break;
1886
		}
1891
		}
1887
		/* Fall through */
1892
		/* Fall through */
1888
	default:
1893
	default:
1889
		pri_error(ctrl, "!! Coding-standard field is not Q.931.\n");
1894
		pri_error(ctrl, "!! Coding-standard field is not Q.931.\n");
1890
		return -1;
1895
		return -1;
1891
	}
1896
	}
1892
	return 0;
1897
	return 0;
1893
}
1898
}
1894

    
   
1899

   
1895
static int transmit_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1900
static int transmit_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
1896
{
1901
{
1897
	int tc;
1902
	int tc;
1898
	int pos;
1903
	int pos;
1899

    
   
1904

   
1900
	/* We are ready to transmit single IE only */	
1905
	/* We are ready to transmit single IE only */	
1901
	if(order > 1)
1906
	if(order > 1)
1902
		return 0;
1907
		return 0;
1903

    
   
1908

   
1904
	if (ctrl->link.next && !ctrl->bri) {
1909
	if (ctrl->link.next && !ctrl->bri) {
1905
		/* Bearer capability is *hard coded* in GR-303 */
1910
		/* Bearer capability is *hard coded* in GR-303 */
1906
		ie->data[0] = 0x88;
1911
		ie->data[0] = 0x88;
1907
		ie->data[1] = 0x90;
1912
		ie->data[1] = 0x90;
1908
		return 4;
1913
		return 4;
1909
	}
1914
	}
1910

    
   
1915

   
1911
	if (call->cis_call) {
1916
	if (call->cis_call) {
1912
		ie->data[0] = 0xa8;
1917
		ie->data[0] = 0xa8;
1913
		ie->data[1] = 0x80;
1918
		ie->data[1] = 0x80;
1914
		return 4;
1919
		return 4;
1915
	}
1920
	}
1916

    
   
1921

   
1917
	tc = call->bc.transcapability;
1922
	tc = call->bc.transcapability;
1918
	ie->data[0] = 0x80 | tc;
1923
	ie->data[0] = 0x80 | tc;
1919
	ie->data[1] = call->bc.transmoderate | 0x80;
1924
	ie->data[1] = call->bc.transmoderate | 0x80;
1920

    
   
1925

   
1921
 	pos = 2;
1926
 	pos = 2;
1922
 	/* octet 4.1 exists iff mode/rate is multirate */
1927
 	/* octet 4.1 exists iff mode/rate is multirate */
1923
 	if (call->bc.transmoderate == TRANS_MODE_MULTIRATE ) {
1928
 	if (call->bc.transmoderate == TRANS_MODE_MULTIRATE ) {
1924
 		ie->data[pos++] = call->bc.transmultiple | 0x80;
1929
 		ie->data[pos++] = call->bc.transmultiple | 0x80;
1925
	}
1930
	}
1926

    
   
1931

   
1927
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) &&
1932
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) &&
1928
		(call->bc.transmoderate == TRANS_MODE_PACKET)) {
1933
		(call->bc.transmoderate == TRANS_MODE_PACKET)) {
1929
		/* Apparently EuroISDN switches don't seem to like user layer 2/3 */
1934
		/* Apparently EuroISDN switches don't seem to like user layer 2/3 */
1930
		return 4;
1935
		return 4;
1931
	}
1936
	}
1932

    
   
1937

   
1933
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (call->bc.transmoderate == TRANS_MODE_64_CIRCUIT)) {
1938
	if ((tc & PRI_TRANS_CAP_DIGITAL) && (call->bc.transmoderate == TRANS_MODE_64_CIRCUIT)) {
1934
		/* Unrestricted digital 64k data calls don't use user layer 2/3 */
1939
		/* Unrestricted digital 64k data calls don't use user layer 2/3 */
1935
		return 4;
1940
		return 4;
1936
	}
1941
	}
1937

    
   
1942

   
1938
	if (call->bc.transmoderate != TRANS_MODE_PACKET) {
1943
	if (call->bc.transmoderate != TRANS_MODE_PACKET) {
1939
		/* If you have an AT&T 4ESS, you don't send any more info */
1944
		/* If you have an AT&T 4ESS, you don't send any more info */
1940
		if ((ctrl->switchtype != PRI_SWITCH_ATT4ESS) && (call->bc.userl1 > -1)) {
1945
		if ((ctrl->switchtype != PRI_SWITCH_ATT4ESS) && (call->bc.userl1 > -1)) {
1941
			ie->data[pos++] = call->bc.userl1 | 0x80; /* XXX Ext bit? XXX */
1946
			ie->data[pos++] = call->bc.userl1 | 0x80; /* XXX Ext bit? XXX */
1942
			if (call->bc.userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1947
			if (call->bc.userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1943
				ie->data[pos++] = call->bc.rateadaption | 0x80;
1948
				ie->data[pos++] = call->bc.rateadaption | 0x80;
1944
			}
1949
			}
1945
			return pos + 2;
1950
			return pos + 2;
1946
 		}
1951
 		}
1947
 
1952
 
1948
 		ie->data[pos++] = 0xa0 | (call->bc.userl1 & 0x1f);
1953
 		ie->data[pos++] = 0xa0 | (call->bc.userl1 & 0x1f);
1949
 
1954
 
1950
 		if (call->bc.userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1955
 		if (call->bc.userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
1951
 		    ie->data[pos-1] &= ~0x80; /* clear EXT bit in octet 5 */
1956
 		    ie->data[pos-1] &= ~0x80; /* clear EXT bit in octet 5 */
1952
 		    ie->data[pos++] = call->bc.rateadaption | 0x80;
1957
 		    ie->data[pos++] = call->bc.rateadaption | 0x80;
1953
 		}
1958
 		}
1954
 	}
1959
 	}
1955
 	
1960
 	
1956
 	
1961
 	
1957
 	if (call->bc.userl2 != -1)
1962
 	if (call->bc.userl2 != -1)
1958
 		ie->data[pos++] = 0xc0 | (call->bc.userl2 & 0x1f);
1963
 		ie->data[pos++] = 0xc0 | (call->bc.userl2 & 0x1f);
1959
 
1964
 
1960
 	if (call->bc.userl3 != -1)
1965
 	if (call->bc.userl3 != -1)
1961
 		ie->data[pos++] = 0xe0 | (call->bc.userl3 & 0x1f);
1966
 		ie->data[pos++] = 0xe0 | (call->bc.userl3 & 0x1f);
1962
 
1967
 
1963
 	return pos + 2;
1968
 	return pos + 2;
1964
}
1969
}
1965

    
   
1970

   
1966
char *pri_plan2str(int plan)
1971
char *pri_plan2str(int plan)
1967
{
1972
{
1968
	static struct msgtype plans[] = {
1973
	static struct msgtype plans[] = {
1969
		{ PRI_INTERNATIONAL_ISDN, "International number in ISDN" },
1974
		{ PRI_INTERNATIONAL_ISDN, "International number in ISDN" },
1970
		{ PRI_NATIONAL_ISDN, "National number in ISDN" },
1975
		{ PRI_NATIONAL_ISDN, "National number in ISDN" },
1971
		{ PRI_LOCAL_ISDN, "Local number in ISDN" },
1976
		{ PRI_LOCAL_ISDN, "Local number in ISDN" },
1972
		{ PRI_PRIVATE, "Private numbering plan" },
1977
		{ PRI_PRIVATE, "Private numbering plan" },
1973
		{ PRI_UNKNOWN, "Unknown numbering plan" },
1978
		{ PRI_UNKNOWN, "Unknown numbering plan" },
1974
	};
1979
	};
1975
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1980
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1976
}
1981
}
1977

    
   
1982

   
1978
static char *npi2str(int plan)
1983
static char *npi2str(int plan)
1979
{
1984
{
1980
	static struct msgtype plans[] = {
1985
	static struct msgtype plans[] = {
1981
		{ PRI_NPI_UNKNOWN, "Unknown Number Plan" },
1986
		{ PRI_NPI_UNKNOWN, "Unknown Number Plan" },
1982
		{ PRI_NPI_E163_E164, "ISDN/Telephony Numbering Plan (E.164/E.163)" },
1987
		{ PRI_NPI_E163_E164, "ISDN/Telephony Numbering Plan (E.164/E.163)" },
1983
		{ PRI_NPI_X121, "Data Numbering Plan (X.121)" },
1988
		{ PRI_NPI_X121, "Data Numbering Plan (X.121)" },
1984
		{ PRI_NPI_F69, "Telex Numbering Plan (F.69)" },
1989
		{ PRI_NPI_F69, "Telex Numbering Plan (F.69)" },
1985
		{ PRI_NPI_NATIONAL, "National Standard Numbering Plan" },
1990
		{ PRI_NPI_NATIONAL, "National Standard Numbering Plan" },
1986
		{ PRI_NPI_PRIVATE, "Private Numbering Plan" },
1991
		{ PRI_NPI_PRIVATE, "Private Numbering Plan" },
1987
		{ PRI_NPI_RESERVED, "Reserved Number Plan" },
1992
		{ PRI_NPI_RESERVED, "Reserved Number Plan" },
1988
	};
1993
	};
1989
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1994
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
1990
}
1995
}
1991

    
   
1996

   
1992
static char *ton2str(int plan)
1997
static char *ton2str(int plan)
1993
{
1998
{
1994
	static struct msgtype plans[] = {
1999
	static struct msgtype plans[] = {
1995
		{ PRI_TON_UNKNOWN, "Unknown Number Type" },
2000
		{ PRI_TON_UNKNOWN, "Unknown Number Type" },
1996
		{ PRI_TON_INTERNATIONAL, "International Number" },
2001
		{ PRI_TON_INTERNATIONAL, "International Number" },
1997
		{ PRI_TON_NATIONAL, "National Number" },
2002
		{ PRI_TON_NATIONAL, "National Number" },
1998
		{ PRI_TON_NET_SPECIFIC, "Network Specific Number" },
2003
		{ PRI_TON_NET_SPECIFIC, "Network Specific Number" },
1999
		{ PRI_TON_SUBSCRIBER, "Subscriber Number" },
2004
		{ PRI_TON_SUBSCRIBER, "Subscriber Number" },
2000
		{ PRI_TON_ABBREVIATED, "Abbreviated number" },
2005
		{ PRI_TON_ABBREVIATED, "Abbreviated number" },
2001
		{ PRI_TON_RESERVED, "Reserved Number" },
2006
		{ PRI_TON_RESERVED, "Reserved Number" },
2002
	};
2007
	};
2003
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
2008
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
2004
}
2009
}
2005

    
   
2010

   
2006
static char *subaddrtype2str(int plan)
2011
static char *subaddrtype2str(int plan)
2007
{
2012
{
2008
	static struct msgtype plans[] = {
2013
	static struct msgtype plans[] = {
2009
		{ 0, "NSAP (X.213/ISO 8348 AD2)" },
2014
		{ 0, "NSAP (X.213/ISO 8348 AD2)" },
2010
		{ 2, "User Specified" },
2015
		{ 2, "User Specified" },
2011
	};
2016
	};
2012
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
2017
	return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
2013
}
2018
}
2014

    
   
2019

   
2015
/* Calling Party Category (Definitions from Q.763) */
2020
/* Calling Party Category (Definitions from Q.763) */
2016
static char *cpc2str(int plan)
2021
static char *cpc2str(int plan)
2017
{
2022
{
2018
	static struct msgtype plans[] = {
2023
	static struct msgtype plans[] = {
2019
		{ 0, "Unknown Source" },
2024
		{ 0, "Unknown Source" },
2020
		{ 1, "Operator French" },
2025
		{ 1, "Operator French" },
2021
		{ 2, "Operator English" },
2026
		{ 2, "Operator English" },
2022
		{ 3, "Operator German" },
2027
		{ 3, "Operator German" },
2023
		{ 4, "Operator Russian" },
2028
		{ 4, "Operator Russian" },
2024
		{ 5, "Operator Spanish" },
2029
		{ 5, "Operator Spanish" },
2025
		{ 6, "Mut Agree Chinese" },
2030
		{ 6, "Mut Agree Chinese" },
2026
		{ 7, "Mut Agreement" },
2031
		{ 7, "Mut Agreement" },
2027
		{ 8, "Mut Agree Japanese" },
2032
		{ 8, "Mut Agree Japanese" },
2028
		{ 9, "National Operator" },
2033
		{ 9, "National Operator" },
2029
		{ 10, "Ordinary Toll Caller" },
2034
		{ 10, "Ordinary Toll Caller" },
2030
		{ 11, "Priority Toll Caller" },
2035
		{ 11, "Priority Toll Caller" },
2031
		{ 12, "Data Call" },
2036
		{ 12, "Data Call" },
2032
		{ 13, "Test Call" },
2037
		{ 13, "Test Call" },
2033
		{ 14, "Spare" },
2038
		{ 14, "Spare" },
2034
		{ 15, "Pay Phone" },
2039
		{ 15, "Pay Phone" },
2035
	};
2040
	};
2036
	return code2str(plan, plans, ARRAY_LEN(plans));
2041
	return code2str(plan, plans, ARRAY_LEN(plans));
2037
}
2042
}
2038

    
   
2043

   
2039
char *pri_pres2str(int pres)
2044
char *pri_pres2str(int pres)
2040
{
2045
{
2041
	static struct msgtype press[] = {
2046
	static struct msgtype press[] = {
2042
		{ PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "Presentation permitted, user number not screened" },
2047
		{ PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "Presentation permitted, user number not screened" },
2043
		{ PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "Presentation permitted, user number passed network screening" },
2048
		{ PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "Presentation permitted, user number passed network screening" },
2044
		{ PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "Presentation permitted, user number failed network screening" },
2049
		{ PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "Presentation permitted, user number failed network screening" },
2045
		{ PRES_ALLOWED_NETWORK_NUMBER, "Presentation allowed of network provided number" },
2050
		{ PRES_ALLOWED_NETWORK_NUMBER, "Presentation allowed of network provided number" },
2046
		{ PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "Presentation prohibited, user number not screened" },
2051
		{ PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "Presentation prohibited, user number not screened" },
2047
		{ PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "Presentation prohibited, user number passed network screening" },
2052
		{ PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "Presentation prohibited, user number passed network screening" },
2048
		{ PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "Presentation prohibited, user number failed network screening" },
2053
		{ PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "Presentation prohibited, user number failed network screening" },
2049
		{ PRES_PROHIB_NETWORK_NUMBER, "Presentation prohibited of network provided number" },
2054
		{ PRES_PROHIB_NETWORK_NUMBER, "Presentation prohibited of network provided number" },
2050
		{ PRES_NUMBER_NOT_AVAILABLE, "Number not available" },
2055
		{ PRES_NUMBER_NOT_AVAILABLE, "Number not available" },
2051
	};
2056
	};
2052
	return code2str(pres, press, sizeof(press) / sizeof(press[0]));
2057
	return code2str(pres, press, sizeof(press) / sizeof(press[0]));
2053
}
2058
}
2054

    
   
2059

   
2055
static void q931_get_subaddr_specific(unsigned char *num, int maxlen, unsigned char *src, int len, char oddflag)
2060
static void q931_get_subaddr_specific(unsigned char *num, int maxlen, unsigned char *src, int len, char oddflag)
2056
{
2061
{
2057
	/* User Specified */
2062
	/* User Specified */
2058
	int x;
2063
	int x;
2059
	char *ptr = (char *) num;
2064
	char *ptr = (char *) num;
2060

    
   
2065

   
2061
	if (len <= 0) {
2066
	if (len <= 0) {
2062
		num[0] = '\0';
2067
		num[0] = '\0';
2063
		return;
2068
		return;
2064
	}
2069
	}
2065

    
   
2070

   
2066
	if (((len * 2) + 1) > maxlen) {
2071
	if (((len * 2) + 1) > maxlen) {
2067
		len = (maxlen / 2) - 1;
2072
		len = (maxlen / 2) - 1;
2068
	}
2073
	}
2069

    
   
2074

   
2070
	for (x = 0; x < (len - 1); ++x) {
2075
	for (x = 0; x < (len - 1); ++x) {
2071
		ptr += sprintf(ptr, "%02x", src[x]);
2076
		ptr += sprintf(ptr, "%02x", src[x]);
2072
	}
2077
	}
2073

    
   
2078

   
2074
	if (oddflag) {
2079
	if (oddflag) {
2075
		/* ODD */
2080
		/* ODD */
2076
		sprintf(ptr, "%01x", (src[len - 1]) >> 4);
2081
		sprintf(ptr, "%01x", (src[len - 1]) >> 4);
2077
	} else {
2082
	} else {
2078
		/* EVEN */
2083
		/* EVEN */
2079
		sprintf(ptr, "%02x", src[len - 1]);
2084
		sprintf(ptr, "%02x", src[len - 1]);
2080
	}
2085
	}
2081
}
2086
}
2082

    
   
2087

   
2083
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)
2088
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)
2084
{
2089
{
2085
	size_t datalen;
2090
	size_t datalen;
2086

    
   
2091

   
2087
	if (!q931_subaddress->valid) {
2092
	if (!q931_subaddress->valid) {
2088
		return 0;
2093
		return 0;
2089
	}
2094
	}
2090

    
   
2095

   
2091
	datalen = q931_subaddress->length;
2096
	datalen = q931_subaddress->length;
2092
	if (!q931_subaddress->type) {
2097
	if (!q931_subaddress->type) {
2093
		/* 0 = NSAP */
2098
		/* 0 = NSAP */
2094
		/* 0 = Odd/Even indicator */
2099
		/* 0 = Odd/Even indicator */
2095
		ie->data[0] = 0x80;
2100
		ie->data[0] = 0x80;
2096
	} else {
2101
	} else {
2097
		/* 2 = User Specified */
2102
		/* 2 = User Specified */
2098
		ie->data[0] = q931_subaddress->odd_even_indicator ? 0xA8 : 0xA0;
2103
		ie->data[0] = q931_subaddress->odd_even_indicator ? 0xA8 : 0xA0;
2099
	}
2104
	}
2100
	memcpy(ie->data + offset, q931_subaddress->data, datalen);
2105
	memcpy(ie->data + offset, q931_subaddress->data, datalen);
2101

    
   
2106

   
2102
	return datalen + (offset + 2);
2107
	return datalen + (offset + 2);
2103
}
2108
}
2104

    
   
2109

   
2105
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)
2110
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)
2106
{
2111
{
2107
	if (len <= 0) {
2112
	if (len <= 0) {
2108
		return -1;
2113
		return -1;
2109
	}
2114
	}
2110

    
   
2115

   
2111
	q931_subaddress->valid = 1;
2116
	q931_subaddress->valid = 1;
2112
	q931_subaddress->length = len;
2117
	q931_subaddress->length = len;
2113
	/* type: 0 = NSAP, 2 = User Specified */
2118
	/* type: 0 = NSAP, 2 = User Specified */
2114
	q931_subaddress->type = ((ie->data[0] & 0x70) >> 4);
2119
	q931_subaddress->type = ((ie->data[0] & 0x70) >> 4);
2115
	q931_subaddress->odd_even_indicator = (ie->data[0] & 0x08) ? 1 : 0;
2120
	q931_subaddress->odd_even_indicator = (ie->data[0] & 0x08) ? 1 : 0;
2116
	q931_memget(q931_subaddress->data, sizeof(q931_subaddress->data),
2121
	q931_memget(q931_subaddress->data, sizeof(q931_subaddress->data),
2117
		ie->data + offset, len);
2122
		ie->data + offset, len);
2118

    
   
2123

   
2119
	return 0;
2124
	return 0;
2120
}
2125
}
2121

    
   
2126

   
2122
static void dump_subaddr_helper(int full_ie, struct pri *ctrl, q931_ie *ie, int offset, int len, int datalen, char prefix)
2127
static void dump_subaddr_helper(int full_ie, struct pri *ctrl, q931_ie *ie, int offset, int len, int datalen, char prefix)
2123
{
2128
{
2124
	unsigned char cnum[256];
2129
	unsigned char cnum[256];
2125

    
   
2130

   
2126
	if (!(ie->data[0] & 0x70)) {
2131
	if (!(ie->data[0] & 0x70)) {
2127
		/* NSAP  Get it as a string for dump display purposes only. */
2132
		/* NSAP  Get it as a string for dump display purposes only. */
2128
		q931_strget(cnum, sizeof(cnum), ie->data + offset, datalen);
2133
		q931_strget(cnum, sizeof(cnum), ie->data + offset, datalen);
2129
	} else {
2134
	} else {
2130
		/* User Specified */
2135
		/* User Specified */
2131
		q931_get_subaddr_specific(cnum, sizeof(cnum), ie->data + offset, datalen,
2136
		q931_get_subaddr_specific(cnum, sizeof(cnum), ie->data + offset, datalen,
2132
			ie->data[0] & 0x08);
2137
			ie->data[0] & 0x08);
2133
	}
2138
	}
2134

    
   
2139

   
2135
	pri_message(ctrl,
2140
	pri_message(ctrl,
2136
		"%c %s (len=%2d) [ Ext: %d  Type: %s (%d)  O: %d  '%s' ]\n",
2141
		"%c %s (len=%2d) [ Ext: %d  Type: %s (%d)  O: %d  '%s' ]\n",
2137
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,
2142
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,
2138
		subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
2143
		subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
2139
		(ie->data[0] & 0x08) >> 3, cnum);
2144
		(ie->data[0] & 0x08) >> 3, cnum);
2140
}
2145
}
2141

    
   
2146

   
2142
static void dump_called_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2147
static void dump_called_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2143
{
2148
{
2144
	unsigned char cnum[256];
2149
	unsigned char cnum[256];
2145

    
   
2150

   
2146
	q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
2151
	q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
2147
	pri_message(ctrl, "%c Called Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)  '%s' ]\n",
2152
	pri_message(ctrl,
2148
		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);
2153
		"%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)  '%s' ]\n",

    
   
2154
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
2155
		ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,

    
   
2156
		npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f, cnum);
2149
}
2157
}
2150

    
   
2158

   
2151
static void dump_called_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2159
static void dump_called_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2152
{
2160
{
2153
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2161
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2154
}
2162
}
2155

    
   
2163

   
2156
static void dump_calling_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2164
static void dump_calling_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2157
{
2165
{
2158
	unsigned char cnum[256];
2166
	unsigned char cnum[256];
2159

    
   
2167

   
2160
	if (ie->data[0] & 0x80) {
2168
	if (ie->data[0] & 0x80) {
2161
		q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
2169
		q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
2162
	} else {
2170
	} else {
2163
		q931_strget(cnum, sizeof(cnum), ie->data + 2, len - 4);
2171
		q931_strget(cnum, sizeof(cnum), ie->data + 2, len - 4);
2164
	}
2172
	}
2165
	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);
2173
	pri_message(ctrl,

    
   
2174
		"%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)\n",

    
   
2175
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
2176
		ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,

    
   
2177
		npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
2166
	if (ie->data[0] & 0x80)
2178
	if (ie->data[0] & 0x80)
2167
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
2179
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
2168
	else
2180
	else
2169
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
2181
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
2170
}
2182
}
2171

    
   
2183

   
2172
static void dump_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2184
static void dump_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2173
{
2185
{
2174
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2186
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2175
}
2187
}
2176

    
   
2188

   
2177
static void dump_calling_party_category(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2189
static void dump_calling_party_category(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2178
{
2190
{
2179
	pri_message(ctrl, "%c Calling Party Category (len=%2d) [ Ext: %d  Cat: %s (%d) ]\n",
2191
	pri_message(ctrl,
2180
		prefix, len, ie->data[0] >> 7, cpc2str(ie->data[0] & 0x0F), ie->data[0] & 0x0F);
2192
		"%c %s (len=%2d) [ Ext: %d  Cat: %s (%d) ]\n",

    
   
2193
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
2194
		cpc2str(ie->data[0] & 0x0F), ie->data[0] & 0x0F);
2181
}
2195
}
2182

    
   
2196

   
2183
static void dump_redirecting_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2197
static void dump_redirecting_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2184
{
2198
{
2185
	unsigned char cnum[256];
2199
	unsigned char cnum[256];
2186
	int i = 0;
2200
	int i = 0;
2187
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2201
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2188
	   walking through all bytes until one with ext bit (8) set to 1 */
2202
	   walking through all bytes until one with ext bit (8) set to 1 */
2189
	do {
2203
	do {
2190
		switch(i) {
2204
		switch(i) {
2191
		case 0:	/* Octet 3 */
2205
		case 0:	/* Octet 3 */
2192
			pri_message(ctrl, "%c Redirecting Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
2206
			pri_message(ctrl,
2193
				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);
2207
				"%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",

    
   
2208
				prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
2209
				ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,

    
   
2210
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
2194
			break;
2211
			break;
2195
		case 1: /* Octet 3a */
2212
		case 1: /* Octet 3a */
2196
			pri_message(ctrl, "\n");
2213
			pri_message(ctrl, "\n");
2197
			pri_message(ctrl, "%c                               Ext: %d  Presentation: %s (%d)",
2214
			pri_message(ctrl, "%c                               Ext: %d  Presentation: %s (%d)",
2198
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2215
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2199
			break;
2216
			break;
2200
		case 2: /* Octet 3b */
2217
		case 2: /* Octet 3b */
2201
			pri_message(ctrl, "\n");
2218
			pri_message(ctrl, "\n");
2202
			pri_message(ctrl, "%c                               Ext: %d  Reason: %s (%d)",
2219
			pri_message(ctrl, "%c                               Ext: %d  Reason: %s (%d)",
2203
				prefix, ie->data[2] >> 7, redirection_reason2str(ie->data[2] & 0x7f), ie->data[2] & 0x7f);
2220
				prefix, ie->data[2] >> 7, redirection_reason2str(ie->data[2] & 0x7f), ie->data[2] & 0x7f);
2204
			break;
2221
			break;
2205
		}
2222
		}
2206
	} while(!(ie->data[i++]& 0x80));
2223
	} while(!(ie->data[i++]& 0x80));
2207
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2224
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2208
	pri_message(ctrl, "  '%s' ]\n", cnum);
2225
	pri_message(ctrl, "  '%s' ]\n", cnum);
2209
}
2226
}
2210

    
   
2227

   
2211
static void dump_redirection_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2228
static void dump_redirection_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2212
{
2229
{
2213
	unsigned char cnum[256];
2230
	unsigned char cnum[256];
2214
	int i = 0;
2231
	int i = 0;
2215
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2232
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2216
	   walking through all bytes until one with ext bit (8) set to 1 */
2233
	   walking through all bytes until one with ext bit (8) set to 1 */
2217
	do {
2234
	do {
2218
		switch (i) {
2235
		switch (i) {
2219
		case 0:	/* Octet 3 */
2236
		case 0:	/* Octet 3 */
2220
			pri_message(ctrl,
2237
			pri_message(ctrl,
2221
				"%c Redirection Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
2238
				"%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
2222
				prefix, len, ie->data[0] >> 7,
2239
				prefix, ie2str(full_ie), len, ie->data[0] >> 7,
2223
				ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
2240
				ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
2224
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
2241
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
2225
			break;
2242
			break;
2226
		case 1: /* Octet 3a */
2243
		case 1: /* Octet 3a */
2227
			pri_message(ctrl, "\n");
2244
			pri_message(ctrl, "\n");
2228
			pri_message(ctrl, "%c                               Ext: %d  Presentation: %s (%d)",
2245
			pri_message(ctrl, "%c                               Ext: %d  Presentation: %s (%d)",
2229
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2246
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2230
			break;
2247
			break;
2231
		}
2248
		}
2232
	} while (!(ie->data[i++] & 0x80));
2249
	} while (!(ie->data[i++] & 0x80));
2233
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2250
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2234
	pri_message(ctrl, "  '%s' ]\n", cnum);
2251
	pri_message(ctrl, "  '%s' ]\n", cnum);
2235
}
2252
}
2236

    
   
2253

   
2237
static int receive_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2254
static int receive_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2238
{
2255
{
2239
	int i = 0;
2256
	int i = 0;
2240

    
   
2257

   
2241
	call->connected_number_in_message = 1;
2258
	call->connected_number_in_message = 1;
2242
	call->remote_id.number.valid = 1;
2259
	call->remote_id.number.valid = 1;
2243
	call->remote_id.number.presentation =
2260
	call->remote_id.number.presentation =
2244
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2261
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2245
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2262
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2246
	   walking through all bytes until one with ext bit (8) set to 1 */
2263
	   walking through all bytes until one with ext bit (8) set to 1 */
2247
	do {
2264
	do {
2248
		switch (i) {
2265
		switch (i) {
2249
		case 0:
2266
		case 0:
2250
			call->remote_id.number.plan = ie->data[i] & 0x7f;
2267
			call->remote_id.number.plan = ie->data[i] & 0x7f;
2251
			break;
2268
			break;
2252
		case 1:
2269
		case 1:
2253
			/* Keep only the presentation and screening fields */
2270
			/* Keep only the presentation and screening fields */
2254
			call->remote_id.number.presentation =
2271
			call->remote_id.number.presentation =
2255
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2272
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2256
			break;
2273
			break;
2257
		}
2274
		}
2258
	} while (!(ie->data[i++] & 0x80));
2275
	} while (!(ie->data[i++] & 0x80));
2259
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->remote_id.number.str,
2276
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->remote_id.number.str,
2260
		sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);
2277
		sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);
2261

    
   
2278

   
2262
	return 0;
2279
	return 0;
2263
}
2280
}
2264

    
   
2281

   
2265
static int transmit_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2282
static int transmit_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2266
{
2283
{
2267
	size_t datalen;
2284
	size_t datalen;
2268

    
   
2285

   
2269
	if (!call->local_id.number.valid) {
2286
	if (!call->local_id.number.valid) {
2270
		return 0;
2287
		return 0;
2271
	}
2288
	}
2272

    
   
2289

   
2273
	datalen = strlen(call->local_id.number.str);
2290
	datalen = strlen(call->local_id.number.str);
2274
	ie->data[0] = call->local_id.number.plan;
2291
	ie->data[0] = call->local_id.number.plan;
2275
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2292
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2276
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2293
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2277
	return datalen + (2 + 2);
2294
	return datalen + (2 + 2);
2278
}
2295
}
2279

    
   
2296

   
2280
static void dump_connected_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2297
static void dump_connected_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2281
{
2298
{
2282
	unsigned char cnum[256];
2299
	unsigned char cnum[256];
2283
	int i = 0;
2300
	int i = 0;
2284
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2301
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2285
	   walking through all bytes until one with ext bit (8) set to 1 */
2302
	   walking through all bytes until one with ext bit (8) set to 1 */
2286
	do {
2303
	do {
2287
		switch(i) {
2304
		switch(i) {
2288
		case 0:	/* Octet 3 */
2305
		case 0:	/* Octet 3 */
2289
			pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
2306
			pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
2290
				prefix, ie2str(full_ie), len, ie->data[0] >> 7,
2307
				prefix, ie2str(full_ie), len, ie->data[0] >> 7,
2291
				ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
2308
				ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
2292
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
2309
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
2293
			break;
2310
			break;
2294
		case 1: /* Octet 3a */
2311
		case 1: /* Octet 3a */
2295
			pri_message(ctrl, "\n");
2312
			pri_message(ctrl, "\n");
2296
			pri_message(ctrl, "%c                             Ext: %d  Presentation: %s (%d)",
2313
			pri_message(ctrl, "%c                             Ext: %d  Presentation: %s (%d)",
2297
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2314
				prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2298
			break;
2315
			break;
2299
		}
2316
		}
2300
	} while(!(ie->data[i++]& 0x80));
2317
	} while(!(ie->data[i++]& 0x80));
2301
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2318
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2302
	pri_message(ctrl, "  '%s' ]\n", cnum);
2319
	pri_message(ctrl, "  '%s' ]\n", cnum);
2303
}
2320
}
2304

    
   
2321

   
2305
static int receive_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2322
static int receive_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2306
{
2323
{
2307
	if (len < 3) {
2324
	if (len < 3) {
2308
		return -1;
2325
		return -1;
2309
	}
2326
	}
2310

    
   
2327

   
2311
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie,
2328
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie,
2312
		1, len - 3);
2329
		1, len - 3);
2313
}
2330
}
2314

    
   
2331

   
2315
static int transmit_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2332
static int transmit_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2316
{
2333
{
2317
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2334
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2318
		1, len, order);
2335
		1, len, order);
2319
}
2336
}
2320

    
   
2337

   
2321
static void dump_connected_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2338
static void dump_connected_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2322
{
2339
{
2323
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2340
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2324
}
2341
}
2325

    
   
2342

   
2326
static int receive_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2343
static int receive_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2327
{
2344
{
2328
	int i = 0;
2345
	int i = 0;
2329

    
   
2346

   
2330
	call->redirecting_number_in_message = 1;
2347
	call->redirecting_number_in_message = 1;
2331
	call->redirecting.from.number.valid = 1;
2348
	call->redirecting.from.number.valid = 1;
2332
	call->redirecting.from.number.presentation =
2349
	call->redirecting.from.number.presentation =
2333
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2350
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2334
	call->redirecting.reason = PRI_REDIR_UNKNOWN;
2351
	call->redirecting.reason = PRI_REDIR_UNKNOWN;
2335
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2352
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2336
	   walking through all bytes until one with ext bit (8) set to 1 */
2353
	   walking through all bytes until one with ext bit (8) set to 1 */
2337
	do {
2354
	do {
2338
		switch (i) {
2355
		switch (i) {
2339
		case 0:
2356
		case 0:
2340
			call->redirecting.from.number.plan = ie->data[i] & 0x7f;
2357
			call->redirecting.from.number.plan = ie->data[i] & 0x7f;
2341
			break;
2358
			break;
2342
		case 1:
2359
		case 1:
2343
			/* Keep only the presentation and screening fields */
2360
			/* Keep only the presentation and screening fields */
2344
			call->redirecting.from.number.presentation =
2361
			call->redirecting.from.number.presentation =
2345
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2362
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2346
			break;
2363
			break;
2347
		case 2:
2364
		case 2:
2348
			call->redirecting.reason = ie->data[i] & 0x0f;
2365
			call->redirecting.reason = ie->data[i] & 0x0f;
2349
			break;
2366
			break;
2350
		}
2367
		}
2351
	} while (!(ie->data[i++] & 0x80));
2368
	} while (!(ie->data[i++] & 0x80));
2352
	q931_strget_gripe(ctrl, ie2str(full_ie),
2369
	q931_strget_gripe(ctrl, ie2str(full_ie),
2353
		(unsigned char *) call->redirecting.from.number.str,
2370
		(unsigned char *) call->redirecting.from.number.str,
2354
		sizeof(call->redirecting.from.number.str), ie->data + i, ie->len - i);
2371
		sizeof(call->redirecting.from.number.str), ie->data + i, ie->len - i);
2355
	return 0;
2372
	return 0;
2356
}
2373
}
2357

    
   
2374

   
2358
static int transmit_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2375
static int transmit_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2359
{
2376
{
2360
	size_t datalen;
2377
	size_t datalen;
2361

    
   
2378

   
2362
	if (order > 1)
2379
	if (order > 1)
2363
		return 0;
2380
		return 0;
2364
	if (!call->redirecting.from.number.valid) {
2381
	if (!call->redirecting.from.number.valid) {
2365
		return 0;
2382
		return 0;
2366
	}
2383
	}
2367
	if (BRI_TE_PTMP(ctrl)) {
2384
	if (BRI_TE_PTMP(ctrl)) {
2368
		/*
2385
		/*
2369
		 * We should not send this ie to the network if we are the TE
2386
		 * We should not send this ie to the network if we are the TE
2370
		 * PTMP side since phones do not redirect calls within
2387
		 * PTMP side since phones do not redirect calls within
2371
		 * themselves.  Well... If you consider someone else dialing the
2388
		 * themselves.  Well... If you consider someone else dialing the
2372
		 * handset a redirection then how is the network to know?
2389
		 * handset a redirection then how is the network to know?
2373
		 */
2390
		 */
2374
		return 0;
2391
		return 0;
2375
	}
2392
	}
2376
	if (call->redirecting.state != Q931_REDIRECTING_STATE_IDLE) {
2393
	if (call->redirecting.state != Q931_REDIRECTING_STATE_IDLE) {
2377
		/*
2394
		/*
2378
		 * There was a DivertingLegInformation2 in the message so the
2395
		 * There was a DivertingLegInformation2 in the message so the
2379
		 * Q931_REDIRECTING_NUMBER ie is redundant.  Some networks
2396
		 * Q931_REDIRECTING_NUMBER ie is redundant.  Some networks
2380
		 * (Deutsche Telekom) complain about it.
2397
		 * (Deutsche Telekom) complain about it.
2381
		 */
2398
		 */
2382
		return 0;
2399
		return 0;
2383
	}
2400
	}
2384

    
   
2401

   
2385
	datalen = strlen(call->redirecting.from.number.str);
2402
	datalen = strlen(call->redirecting.from.number.str);
2386
	ie->data[0] = call->redirecting.from.number.plan;
2403
	ie->data[0] = call->redirecting.from.number.plan;
2387
#if 1
2404
#if 1
2388
	/* ETSI and Q.952 do not define the screening field */
2405
	/* ETSI and Q.952 do not define the screening field */
2389
	ie->data[1] = call->redirecting.from.number.presentation & PRI_PRES_RESTRICTION;
2406
	ie->data[1] = call->redirecting.from.number.presentation & PRI_PRES_RESTRICTION;
2390
#else
2407
#else
2391
	/* Q.931 defines the screening field */
2408
	/* Q.931 defines the screening field */
2392
	ie->data[1] = call->redirecting.from.number.presentation;
2409
	ie->data[1] = call->redirecting.from.number.presentation;
2393
#endif
2410
#endif
2394
	ie->data[2] = (call->redirecting.reason & 0x0f) | 0x80;
2411
	ie->data[2] = (call->redirecting.reason & 0x0f) | 0x80;
2395
	memcpy(ie->data + 3, call->redirecting.from.number.str, datalen);
2412
	memcpy(ie->data + 3, call->redirecting.from.number.str, datalen);
2396
	return datalen + (3 + 2);
2413
	return datalen + (3 + 2);
2397
}
2414
}
2398

    
   
2415

   
2399
static void dump_redirecting_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2416
static void dump_redirecting_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2400
{
2417
{
2401
	dump_subaddr_helper(full_ie, ctrl, ie, 2, len, len - 4, prefix);
2418
	dump_subaddr_helper(full_ie, ctrl, ie, 2, len, len - 4, prefix);
2402
}
2419
}
2403

    
   
2420

   
2404
static int receive_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2421
static int receive_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2405
{
2422
{
2406
	int i = 0;
2423
	int i = 0;
2407

    
   
2424

   
2408
	call->redirection_number.valid = 1;
2425
	call->redirection_number.valid = 1;
2409
	call->redirection_number.presentation =
2426
	call->redirection_number.presentation =
2410
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2427
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2411
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2428
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2412
	   walking through all bytes until one with ext bit (8) set to 1 */
2429
	   walking through all bytes until one with ext bit (8) set to 1 */
2413
	do {
2430
	do {
2414
		switch (i) {
2431
		switch (i) {
2415
		case 0:
2432
		case 0:
2416
			call->redirection_number.plan = ie->data[i] & 0x7f;
2433
			call->redirection_number.plan = ie->data[i] & 0x7f;
2417
			break;
2434
			break;
2418
		case 1:
2435
		case 1:
2419
			/* Keep only the presentation and screening fields */
2436
			/* Keep only the presentation and screening fields */
2420
			call->redirection_number.presentation =
2437
			call->redirection_number.presentation =
2421
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2438
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2422
			break;
2439
			break;
2423
		}
2440
		}
2424
	} while (!(ie->data[i++] & 0x80));
2441
	} while (!(ie->data[i++] & 0x80));
2425
	q931_strget_gripe(ctrl, ie2str(full_ie),
2442
	q931_strget_gripe(ctrl, ie2str(full_ie),
2426
		(unsigned char *) call->redirection_number.str,
2443
		(unsigned char *) call->redirection_number.str,
2427
		sizeof(call->redirection_number.str), ie->data + i, ie->len - i);
2444
		sizeof(call->redirection_number.str), ie->data + i, ie->len - i);
2428
	return 0;
2445
	return 0;
2429
}
2446
}
2430

    
   
2447

   
2431
static int transmit_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2448
static int transmit_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2432
{
2449
{
2433
	size_t datalen;
2450
	size_t datalen;
2434

    
   
2451

   
2435
	if (order > 1) {
2452
	if (order > 1) {
2436
		return 0;
2453
		return 0;
2437
	}
2454
	}
2438
	if (!call->redirection_number.valid) {
2455
	if (!call->redirection_number.valid) {
2439
		return 0;
2456
		return 0;
2440
	}
2457
	}
2441

    
   
2458

   
2442
	datalen = strlen(call->redirection_number.str);
2459
	datalen = strlen(call->redirection_number.str);
2443
	ie->data[0] = call->redirection_number.plan;
2460
	ie->data[0] = call->redirection_number.plan;
2444
	ie->data[1] = (call->redirection_number.presentation & PRI_PRES_RESTRICTION) | 0x80;
2461
	ie->data[1] = (call->redirection_number.presentation & PRI_PRES_RESTRICTION) | 0x80;
2445
	memcpy(ie->data + 2, call->redirection_number.str, datalen);
2462
	memcpy(ie->data + 2, call->redirection_number.str, datalen);
2446
	return datalen + (2 + 2);
2463
	return datalen + (2 + 2);
2447
}
2464
}
2448

    
   
2465

   
2449
static int receive_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2466
static int receive_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2450
{
2467
{
2451
	if (len < 3) {
2468
	if (len < 3) {
2452
		return -1;
2469
		return -1;
2453
	}
2470
	}
2454

    
   
2471

   
2455
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie,
2472
	return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie,
2456
		1, len - 3);
2473
		1, len - 3);
2457
}
2474
}
2458

    
   
2475

   
2459
static int transmit_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2476
static int transmit_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2460
{
2477
{
2461
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2478
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2462
		1, len, order);
2479
		1, len, order);
2463
}
2480
}
2464

    
   
2481

   
2465
static int receive_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2482
static int receive_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2466
{
2483
{
2467
	if (len < 3) {
2484
	if (len < 3) {
2468
		return -1;
2485
		return -1;
2469
	}
2486
	}
2470
	return receive_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1,
2487
	return receive_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1,
2471
		len - 3);
2488
		len - 3);
2472
}
2489
}
2473

    
   
2490

   
2474
static int transmit_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2491
static int transmit_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2475
{
2492
{
2476
	return transmit_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie,
2493
	return transmit_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie,
2477
		1, len, order);
2494
		1, len, order);
2478
}
2495
}
2479

    
   
2496

   
2480
static int receive_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2497
static int receive_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2481
{
2498
{
2482
	size_t called_len;
2499
	size_t called_len;
2483
	size_t max_len;
2500
	size_t max_len;
2484
	char *called_end;
2501
	char *called_end;
2485

    
   
2502

   
2486
	if (len < 3) {
2503
	if (len < 3) {
2487
		return -1;
2504
		return -1;
2488
	}
2505
	}
2489

    
   
2506

   
2490
	switch (msgtype) {
2507
	switch (msgtype) {
2491
	case Q931_FACILITY:
2508
	case Q931_FACILITY:
2492
		if (!q931_is_dummy_call(call)) {
2509
		if (!q931_is_dummy_call(call)) {
2493
			/* Discard the number. */
2510
			/* Discard the number. */
2494
			return 0;
2511
			return 0;
2495
		}
2512
		}
2496
		/* Fall through */
2513
		/* Fall through */
2497
	case Q931_REGISTER:
2514
	case Q931_REGISTER:
2498
		/* Accept the number for REGISTER only because it is so similar to SETUP. */
2515
		/* Accept the number for REGISTER only because it is so similar to SETUP. */
2499
	case Q931_SETUP:
2516
	case Q931_SETUP:
2500
		q931_strget((unsigned char *) call->called.number.str,
2517
		q931_strget((unsigned char *) call->called.number.str,
2501
			sizeof(call->called.number.str), ie->data + 1, len - 3);
2518
			sizeof(call->called.number.str), ie->data + 1, len - 3);
2502
		break;
2519
		break;
2503
	case Q931_INFORMATION:
2520
	case Q931_INFORMATION:
2504
		if (call->ourcallstate == Q931_CALL_STATE_OVERLAP_RECEIVING) {
2521
		if (call->ourcallstate == Q931_CALL_STATE_OVERLAP_RECEIVING) {
2505
			/*
2522
			/*
2506
			 * Since we are receiving overlap digits now, we need to append
2523
			 * Since we are receiving overlap digits now, we need to append
2507
			 * them to any previously received digits in call->called.number.str.
2524
			 * them to any previously received digits in call->called.number.str.
2508
			 */
2525
			 */
2509
			called_len = strlen(call->called.number.str);
2526
			called_len = strlen(call->called.number.str);
2510
			called_end = call->called.number.str + called_len;
2527
			called_end = call->called.number.str + called_len;
2511
			max_len = (sizeof(call->called.number.str) - 1) - called_len;
2528
			max_len = (sizeof(call->called.number.str) - 1) - called_len;
2512
			if (max_len < len - 3) {
2529
			if (max_len < len - 3) {
2513
				called_len = max_len;
2530
				called_len = max_len;
2514
			} else {
2531
			} else {
2515
				called_len = len - 3;
2532
				called_len = len - 3;
2516
			}
2533
			}
2517
			strncat(called_end, (char *) ie->data + 1, called_len);
2534
			strncat(called_end, (char *) ie->data + 1, called_len);
2518
		}
2535
		}
2519
		break;
2536
		break;
2520
	default:
2537
	default:
2521
		/* Discard the number. */
2538
		/* Discard the number. */
2522
		return 0;
2539
		return 0;
2523
	}
2540
	}
2524
	call->called.number.valid = 1;
2541
	call->called.number.valid = 1;
2525
	call->called.number.plan = ie->data[0] & 0x7f;
2542
	call->called.number.plan = ie->data[0] & 0x7f;
2526

    
   
2543

   
2527
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->overlap_digits,
2544
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->overlap_digits,
2528
		sizeof(call->overlap_digits), ie->data + 1, len - 3);
2545
		sizeof(call->overlap_digits), ie->data + 1, len - 3);
2529
	return 0;
2546
	return 0;
2530
}
2547
}
2531

    
   
2548

   
2532
static int transmit_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2549
static int transmit_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2533
{
2550
{
2534
	size_t datalen;
2551
	size_t datalen;
2535

    
   
2552

   
2536
	if (!call->called.number.valid) {
2553
	if (!call->called.number.valid) {
2537
		return 0;
2554
		return 0;
2538
	}
2555
	}
2539

    
   
2556

   
2540
	datalen = strlen(call->overlap_digits);
2557
	datalen = strlen(call->overlap_digits);
2541
	ie->data[0] = 0x80 | call->called.number.plan;
2558
	ie->data[0] = 0x80 | call->called.number.plan;
2542
	memcpy(ie->data + 1, call->overlap_digits, datalen);
2559
	memcpy(ie->data + 1, call->overlap_digits, datalen);
2543
	return datalen + (1 + 2);
2560
	return datalen + (1 + 2);
2544
}
2561
}
2545

    
   
2562

   
2546
static int receive_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2563
static int receive_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2547
{
2564
{
2548
	int i = 0;
2565
	int i = 0;
2549
	struct q931_party_number number;
2566
	struct q931_party_number number;
2550

    
   
2567

   
2551
	q931_party_number_init(&number);
2568
	q931_party_number_init(&number);
2552
	number.valid = 1;
2569
	number.valid = 1;
2553
	number.presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2570
	number.presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2554

    
   
2571

   
2555
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2572
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2556
	   walking through all bytes until one with ext bit (8) set to 1 */
2573
	   walking through all bytes until one with ext bit (8) set to 1 */
2557
	do {
2574
	do {
2558
		switch (i) {
2575
		switch (i) {
2559
		case 0:
2576
		case 0:
2560
			number.plan = ie->data[i] & 0x7f;
2577
			number.plan = ie->data[i] & 0x7f;
2561
			break;
2578
			break;
2562
		case 1:
2579
		case 1:
2563
			/* Keep only the presentation and screening fields */
2580
			/* Keep only the presentation and screening fields */
2564
			number.presentation =
2581
			number.presentation =
2565
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2582
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2566
			break;
2583
			break;
2567
		}
2584
		}
2568
	} while (!(ie->data[i++] & 0x80));
2585
	} while (!(ie->data[i++] & 0x80));
2569
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) number.str,
2586
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) number.str,
2570
		sizeof(number.str), ie->data + i, ie->len - i);
2587
		sizeof(number.str), ie->data + i, ie->len - i);
2571

    
   
2588

   
2572
	/* There can be more than one calling party number ie in the SETUP message. */
2589
	/* There can be more than one calling party number ie in the SETUP message. */
2573
	if (number.presentation == (PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER)
2590
	if (number.presentation == (PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER)
2574
		|| number.presentation == (PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER)) {
2591
		|| number.presentation == (PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER)) {
2575
		/* The number is network provided so it is an ANI number. */
2592
		/* The number is network provided so it is an ANI number. */
2576
		call->ani = number;
2593
		call->ani = number;
2577
		if (!call->remote_id.number.valid) {
2594
		if (!call->remote_id.number.valid) {
2578
			/* Copy ANI to CallerID if CallerID is not already set. */
2595
			/* Copy ANI to CallerID if CallerID is not already set. */
2579
			call->remote_id.number = number;
2596
			call->remote_id.number = number;
2580
		}
2597
		}
2581
	} else {
2598
	} else {
2582
		call->remote_id.number = number;
2599
		call->remote_id.number = number;
2583
	}
2600
	}
2584

    
   
2601

   
2585
	return 0;
2602
	return 0;
2586
}
2603
}
2587

    
   
2604

   
2588
static int transmit_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2605
static int transmit_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2589
{
2606
{
2590
	size_t datalen;
2607
	size_t datalen;
2591

    
   
2608

   
2592
	if (!call->local_id.number.valid) {
2609
	if (!call->local_id.number.valid) {
2593
		return 0;
2610
		return 0;
2594
	}
2611
	}
2595

    
   
2612

   
2596
	datalen = strlen(call->local_id.number.str);
2613
	datalen = strlen(call->local_id.number.str);
2597
	ie->data[0] = call->local_id.number.plan;
2614
	ie->data[0] = call->local_id.number.plan;
2598
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2615
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2599
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2616
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2600
	return datalen + (2 + 2);
2617
	return datalen + (2 + 2);
2601
}
2618
}
2602

    
   
2619

   
2603
static void dump_user_user(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2620
static void dump_user_user(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2604
{
2621
{
2605
	int x;
2622
	int x;
2606
	pri_message(ctrl, "%c User-User Information (len=%2d) [", prefix, len);
2623
	pri_message(ctrl, "%c %s (len=%2d) [", prefix, ie2str(full_ie), len);
2607
	for (x=0;x<ie->len;x++)
2624
	for (x = 0; x < ie->len; x++)
2608
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2625
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2609
	pri_message(ctrl, " ]\n");
2626
	pri_message(ctrl, " ]\n");
2610
}
2627
}
2611

    
   
2628

   
2612

    
   
2629

   
2613
static int receive_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2630
static int receive_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2614
{
2631
{
2615
	call->useruserprotocoldisc = ie->data[0] & 0xff;
2632
	call->useruserprotocoldisc = ie->data[0] & 0xff;
2616
	if (call->useruserprotocoldisc == 4) { /* IA5 */
2633
	if (call->useruserprotocoldisc == 4) { /* IA5 */
2617
		q931_memget((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
2634
		q931_memget((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
2618
	}
2635
	}
2619
	return 0;
2636
	return 0;
2620
}
2637
}
2621

    
   
2638

   
2622
static int transmit_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2639
static int transmit_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2623
{        
2640
{        
2624
	int datalen = strlen(call->useruserinfo);
2641
	int datalen = strlen(call->useruserinfo);
2625
	if (datalen > 0) {
2642
	if (datalen > 0) {
2626
		/* Restricted to 35 characters */
2643
		/* Restricted to 35 characters */
2627
		if (msgtype == Q931_USER_INFORMATION) {
2644
		if (msgtype == Q931_USER_INFORMATION) {
2628
			if (datalen > 260)
2645
			if (datalen > 260)
2629
				datalen = 260;
2646
				datalen = 260;
2630
		} else {
2647
		} else {
2631
			if (datalen > 35)
2648
			if (datalen > 35)
2632
				datalen = 35;
2649
				datalen = 35;
2633
		}
2650
		}
2634
		ie->data[0] = 4; /* IA5 characters */
2651
		ie->data[0] = 4; /* IA5 characters */
2635
		memcpy(&ie->data[1], call->useruserinfo, datalen);
2652
		memcpy(&ie->data[1], call->useruserinfo, datalen);
2636
		call->useruserinfo[0] = '\0';
2653
		call->useruserinfo[0] = '\0';
2637
		return datalen + 3;
2654
		return datalen + 3;
2638
	}
2655
	}
2639

    
   
2656

   
2640
	return 0;
2657
	return 0;
2641
}
2658
}
2642

    
   
2659

   
2643
static void dump_change_status(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2660
static void dump_change_status(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2644
{
2661
{
2645
	int x;
2662
	int x;
2646
	
2663
	
2647
	pri_message(ctrl, "%c Change Status Information (len=%2d) [", prefix, len);
2664
	pri_message(ctrl, "%c %s (len=%2d) [", prefix, ie2str(full_ie), len);
2648
	for (x=0; x<ie->len; x++) {
2665
	for (x = 0; x < ie->len; x++) {
2649
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2666
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2650
	}
2667
	}
2651
	pri_message(ctrl, " ]\n");
2668
	pri_message(ctrl, " ]\n");
2652
}
2669
}
2653

    
   
2670

   
2654
static int receive_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2671
static int receive_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2655
{
2672
{
2656
	call->changestatus = ie->data[0] & 0x0f;
2673
	call->changestatus = ie->data[0] & 0x0f;
2657
	return 0;
2674
	return 0;
2658
}
2675
}
2659

    
   
2676

   
2660
static int transmit_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2677
static int transmit_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2661
{
2678
{
2662
	ie->data[0] = 0xc0 | call->changestatus;
2679
	ie->data[0] = 0xc0 | call->changestatus;
2663
	return 3;
2680
	return 3;
2664
}
2681
}
2665

    
   
2682

   
2666
static char *prog2str(int prog)
2683
static char *prog2str(int prog)
2667
{
2684
{
2668
	static struct msgtype progs[] = {
2685
	static struct msgtype progs[] = {
2669
		{ Q931_PROG_CALL_NOT_E2E_ISDN, "Call is not end-to-end ISDN; further call progress information may be available inband." },
2686
		{ Q931_PROG_CALL_NOT_E2E_ISDN, "Call is not end-to-end ISDN; further call progress information may be available inband." },
2670
		{ Q931_PROG_CALLED_NOT_ISDN, "Called equipment is non-ISDN." },
2687
		{ Q931_PROG_CALLED_NOT_ISDN, "Called equipment is non-ISDN." },
2671
		{ Q931_PROG_CALLER_NOT_ISDN, "Calling equipment is non-ISDN." },
2688
		{ Q931_PROG_CALLER_NOT_ISDN, "Calling equipment is non-ISDN." },
2672
		{ Q931_PROG_INBAND_AVAILABLE, "Inband information or appropriate pattern now available." },
2689
		{ Q931_PROG_INBAND_AVAILABLE, "Inband information or appropriate pattern now available." },
2673
		{ Q931_PROG_DELAY_AT_INTERF, "Delay in response at called Interface." },
2690
		{ Q931_PROG_DELAY_AT_INTERF, "Delay in response at called Interface." },
2674
		{ Q931_PROG_INTERWORKING_WITH_PUBLIC, "Interworking with a public network." },
2691
		{ Q931_PROG_INTERWORKING_WITH_PUBLIC, "Interworking with a public network." },
2675
		{ Q931_PROG_INTERWORKING_NO_RELEASE, "Interworking with a network unable to supply a release signal." },
2692
		{ Q931_PROG_INTERWORKING_NO_RELEASE, "Interworking with a network unable to supply a release signal." },
2676
		{ Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER, "Interworking with a network unable to supply a release signal before answer." },
2693
		{ Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER, "Interworking with a network unable to supply a release signal before answer." },
2677
		{ Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER, "Interworking with a network unable to supply a release signal after answer." },
2694
		{ Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER, "Interworking with a network unable to supply a release signal after answer." },
2678
	};
2695
	};
2679
	return code2str(prog, progs, sizeof(progs) / sizeof(progs[0]));
2696
	return code2str(prog, progs, sizeof(progs) / sizeof(progs[0]));
2680
}
2697
}
2681

    
   
2698

   
2682
static char *coding2str(int cod)
2699
static char *coding2str(int cod)
2683
{
2700
{
2684
	static struct msgtype cods[] = {
2701
	static struct msgtype cods[] = {
2685
		{ CODE_CCITT, "CCITT (ITU) standard" },
2702
		{ CODE_CCITT, "CCITT (ITU) standard" },
2686
		{ CODE_INTERNATIONAL, "Non-ITU international standard" }, 
2703
		{ CODE_INTERNATIONAL, "Non-ITU international standard" }, 
2687
		{ CODE_NATIONAL, "National standard" }, 
2704
		{ CODE_NATIONAL, "National standard" }, 
2688
		{ CODE_NETWORK_SPECIFIC, "Network specific standard" },
2705
		{ CODE_NETWORK_SPECIFIC, "Network specific standard" },
2689
	};
2706
	};
2690
	return code2str(cod, cods, sizeof(cods) / sizeof(cods[0]));
2707
	return code2str(cod, cods, sizeof(cods) / sizeof(cods[0]));
2691
}
2708
}
2692

    
   
2709

   
2693
static char *loc2str(int loc)
2710
static char *loc2str(int loc)
2694
{
2711
{
2695
	static struct msgtype locs[] = {
2712
	static struct msgtype locs[] = {
2696
		{ LOC_USER, "User" },
2713
		{ LOC_USER, "User" },
2697
		{ LOC_PRIV_NET_LOCAL_USER, "Private network serving the local user" },
2714
		{ LOC_PRIV_NET_LOCAL_USER, "Private network serving the local user" },
2698
		{ LOC_PUB_NET_LOCAL_USER, "Public network serving the local user" },
2715
		{ LOC_PUB_NET_LOCAL_USER, "Public network serving the local user" },
2699
		{ LOC_TRANSIT_NET, "Transit network" },
2716
		{ LOC_TRANSIT_NET, "Transit network" },
2700
		{ LOC_PUB_NET_REMOTE_USER, "Public network serving the remote user" },
2717
		{ LOC_PUB_NET_REMOTE_USER, "Public network serving the remote user" },
2701
		{ LOC_PRIV_NET_REMOTE_USER, "Private network serving the remote user" },
2718
		{ LOC_PRIV_NET_REMOTE_USER, "Private network serving the remote user" },
2702
		{ LOC_INTERNATIONAL_NETWORK, "International network" },
2719
		{ LOC_INTERNATIONAL_NETWORK, "International network" },
2703
		{ LOC_NETWORK_BEYOND_INTERWORKING, "Network beyond the interworking point" },
2720
		{ LOC_NETWORK_BEYOND_INTERWORKING, "Network beyond the interworking point" },
2704
	};
2721
	};
2705
	return code2str(loc, locs, sizeof(locs) / sizeof(locs[0]));
2722
	return code2str(loc, locs, sizeof(locs) / sizeof(locs[0]));
2706
}
2723
}
2707

    
   
2724

   
2708
static void dump_progress_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2725
static void dump_progress_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2709
{
2726
{
2710
	pri_message(ctrl, "%c Progress Indicator (len=%2d) [ Ext: %d  Coding: %s (%d)  0: %d  Location: %s (%d)\n",
2727
	pri_message(ctrl,
2711
		prefix, len, ie->data[0] >> 7, coding2str((ie->data[0] & 0x60) >> 5), (ie->data[0] & 0x60) >> 5,
2728
		"%c %s (len=%2d) [ Ext: %d  Coding: %s (%d)  0: %d  Location: %s (%d)\n",
2712
		(ie->data[0] & 0x10) >> 4, loc2str(ie->data[0] & 0xf), ie->data[0] & 0xf);
2729
		prefix, ie2str(full_ie), len, ie->data[0] >> 7, coding2str((ie->data[0] & 0x60) >> 5),

    
   
2730
		(ie->data[0] & 0x60) >> 5, (ie->data[0] & 0x10) >> 4,

    
   
2731
		loc2str(ie->data[0] & 0xf), ie->data[0] & 0xf);
2713
	pri_message(ctrl, "%c                               Ext: %d  Progress Description: %s (%d) ]\n",
2732
	pri_message(ctrl, "%c                               Ext: %d  Progress Description: %s (%d) ]\n",
2714
		prefix, ie->data[1] >> 7, prog2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2733
		prefix, ie->data[1] >> 7, prog2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2715
}
2734
}
2716

    
   
2735

   
2717
static int receive_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2736
static int receive_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2718
{
2737
{
2719
	u_int8_t *data;
2738
	u_int8_t *data;
2720

    
   
2739

   
2721
	if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_BLOCK) {
2740
	if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_BLOCK) {
2722
		return 0;
2741
		return 0;
2723
	}
2742
	}
2724

    
   
2743

   
2725
	data = ie->data;
2744
	data = ie->data;
2726
	if (data[0] & 0x80) {
2745
	if (data[0] & 0x80) {
2727
		/* Skip over character set */
2746
		/* Skip over character set */
2728
		data++;
2747
		data++;
2729
		len--;
2748
		len--;
2730
	}
2749
	}
2731

    
   
2750

   
2732
	call->display.text = data;
2751
	call->display.text = data;
2733
	call->display.full_ie = full_ie;
2752
	call->display.full_ie = full_ie;
2734
	call->display.length = len - 2;
2753
	call->display.length = len - 2;
2735
	call->display.char_set = PRI_CHAR_SET_ISO8859_1;
2754
	call->display.char_set = PRI_CHAR_SET_ISO8859_1;
2736
	return 0;
2755
	return 0;
2737
}
2756
}
2738

    
   
2757

   
2739
static int transmit_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2758
static int transmit_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2740
{
2759
{
2741
	size_t datalen;
2760
	size_t datalen;
2742
	int i;
2761
	int i;
2743

    
   
2762

   
2744
	if (!call->display.text || !call->display.length) {
2763
	if (!call->display.text || !call->display.length) {
2745
		return 0;
2764
		return 0;
2746
	}
2765
	}
2747
	if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_BLOCK) {
2766
	if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_BLOCK) {
2748
		return 0;
2767
		return 0;
2749
	}
2768
	}
2750

    
   
2769

   
2751
	i = 0;
2770
	i = 0;
2752
	switch (ctrl->switchtype) {
2771
	switch (ctrl->switchtype) {
2753
	case PRI_SWITCH_QSIG:
2772
	case PRI_SWITCH_QSIG:
2754
	case PRI_SWITCH_EUROISDN_E1:
2773
	case PRI_SWITCH_EUROISDN_E1:
2755
	case PRI_SWITCH_EUROISDN_T1:
2774
	case PRI_SWITCH_EUROISDN_T1:
2756
		break;
2775
		break;
2757
	default:
2776
	default:
2758
		/* Prefix text with character set indicator. */
2777
		/* Prefix text with character set indicator. */
2759
		ie->data[0] = 0xb1;
2778
		ie->data[0] = 0xb1;
2760
		++i;
2779
		++i;
2761
		break;
2780
		break;
2762
	}
2781
	}
2763

    
   
2782

   
2764
	datalen = call->display.length;
2783
	datalen = call->display.length;
2765
	if (MAX_DISPLAY_TEXT < datalen + i) {
2784
	if (MAX_DISPLAY_TEXT < datalen + i) {
2766
		datalen = MAX_DISPLAY_TEXT - i;
2785
		datalen = MAX_DISPLAY_TEXT - i;
2767
	}
2786
	}
2768
	memcpy(ie->data + i, call->display.text, datalen);
2787
	memcpy(ie->data + i, call->display.text, datalen);
2769
	return 2 + i + datalen;
2788
	return 2 + i + datalen;
2770
}
2789
}
2771

    
   
2790

   
2772
static int receive_progress_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2791
static int receive_progress_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2773
{
2792
{
2774
	call->progloc = ie->data[0] & 0xf;
2793
	call->progloc = ie->data[0] & 0xf;
2775
	call->progcode = (ie->data[0] & 0x60) >> 5;
2794
	call->progcode = (ie->data[0] & 0x60) >> 5;
2776
	switch (call->progress = (ie->data[1] & 0x7f)) {
2795
	switch (call->progress = (ie->data[1] & 0x7f)) {
2777
	case Q931_PROG_CALL_NOT_E2E_ISDN:
2796
	case Q931_PROG_CALL_NOT_E2E_ISDN:
2778
		call->progressmask |= PRI_PROG_CALL_NOT_E2E_ISDN;
2797
		call->progressmask |= PRI_PROG_CALL_NOT_E2E_ISDN;
2779
		break;
2798
		break;
2780
	case Q931_PROG_CALLED_NOT_ISDN:
2799
	case Q931_PROG_CALLED_NOT_ISDN:
2781
		call->progressmask |= PRI_PROG_CALLED_NOT_ISDN;
2800
		call->progressmask |= PRI_PROG_CALLED_NOT_ISDN;
2782
		break;
2801
		break;
2783
	case Q931_PROG_CALLER_NOT_ISDN:
2802
	case Q931_PROG_CALLER_NOT_ISDN:
2784
		call->progressmask |= PRI_PROG_CALLER_NOT_ISDN;
2803
		call->progressmask |= PRI_PROG_CALLER_NOT_ISDN;
2785
		break;
2804
		break;
2786
	case Q931_PROG_CALLER_RETURNED_TO_ISDN:
2805
	case Q931_PROG_CALLER_RETURNED_TO_ISDN:
2787
		call->progressmask |= PRI_PROG_CALLER_RETURNED_TO_ISDN;
2806
		call->progressmask |= PRI_PROG_CALLER_RETURNED_TO_ISDN;
2788
		break;
2807
		break;
2789
	case Q931_PROG_INBAND_AVAILABLE:
2808
	case Q931_PROG_INBAND_AVAILABLE:
2790
		call->progressmask |= PRI_PROG_INBAND_AVAILABLE;
2809
		call->progressmask |= PRI_PROG_INBAND_AVAILABLE;
2791
		break;
2810
		break;
2792
	case Q931_PROG_DELAY_AT_INTERF:
2811
	case Q931_PROG_DELAY_AT_INTERF:
2793
		call->progressmask |= PRI_PROG_DELAY_AT_INTERF;
2812
		call->progressmask |= PRI_PROG_DELAY_AT_INTERF;
2794
		break;
2813
		break;
2795
	case Q931_PROG_INTERWORKING_WITH_PUBLIC:
2814
	case Q931_PROG_INTERWORKING_WITH_PUBLIC:
2796
		call->progressmask |= PRI_PROG_INTERWORKING_WITH_PUBLIC;
2815
		call->progressmask |= PRI_PROG_INTERWORKING_WITH_PUBLIC;
2797
		break;
2816
		break;
2798
	case Q931_PROG_INTERWORKING_NO_RELEASE:
2817
	case Q931_PROG_INTERWORKING_NO_RELEASE:
2799
		call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE;
2818
		call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE;
2800
		break;
2819
		break;
2801
	case Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER:
2820
	case Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER:
2802
		call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER;
2821
		call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER;
2803
		break;
2822
		break;
2804
	case Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER:
2823
	case Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER:
2805
		call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER;
2824
		call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER;
2806
		break;
2825
		break;
2807
	default:
2826
	default:
2808
		pri_error(ctrl, "XXX Invalid Progress indicator value received: %02x\n",(ie->data[1] & 0x7f));
2827
		pri_error(ctrl, "XXX Invalid Progress indicator value received: %02x\n",(ie->data[1] & 0x7f));
2809
		break;
2828
		break;
2810
	}
2829
	}
2811
	return 0;
2830
	return 0;
2812
}
2831
}
2813

    
   
2832

   
2814
static void q931_apdu_timeout(void *data);
2833
static void q931_apdu_timeout(void *data);
2815

    
   
2834

   
2816
static int transmit_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2835
static int transmit_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2817
{
2836
{
2818
	struct apdu_event **prev;
2837
	struct apdu_event **prev;
2819
	struct apdu_event *cur;
2838
	struct apdu_event *cur;
2820
	int apdu_len;
2839
	int apdu_len;
2821

    
   
2840

   
2822
	for (prev = &call->apdus, cur = call->apdus;
2841
	for (prev = &call->apdus, cur = call->apdus;
2823
		cur;
2842
		cur;
2824
		prev = &cur->next, cur = cur->next) {
2843
		prev = &cur->next, cur = cur->next) {
2825
		if (!cur->sent && (cur->message == msgtype || cur->message == Q931_ANY_MESSAGE)) {
2844
		if (!cur->sent && (cur->message == msgtype || cur->message == Q931_ANY_MESSAGE)) {
2826
			break;
2845
			break;
2827
		}
2846
		}
2828
	}
2847
	}
2829
	if (!cur) {
2848
	if (!cur) {
2830
		/* No APDU found */
2849
		/* No APDU found */
2831
		return 0;
2850
		return 0;
2832
	}
2851
	}
2833

    
   
2852

   
2834
	if (ctrl->debug & PRI_DEBUG_APDU) {
2853
	if (ctrl->debug & PRI_DEBUG_APDU) {
2835
		pri_message(ctrl, "Adding facility ie contents to send in %s message:\n",
2854
		pri_message(ctrl, "Adding facility ie contents to send in %s message:\n",
2836
			msg2str(msgtype));
2855
			msg2str(msgtype));
2837
		facility_decode_dump(ctrl, cur->apdu, cur->apdu_len);
2856
		facility_decode_dump(ctrl, cur->apdu, cur->apdu_len);
2838
	}
2857
	}
2839

    
   
2858

   
2840
	if (len < cur->apdu_len) { 
2859
	if (len < cur->apdu_len) { 
2841
		pri_error(ctrl,
2860
		pri_error(ctrl,
2842
			"Could not fit facility ie in message.  Size needed:%d  Available space:%d\n",
2861
			"Could not fit facility ie in message.  Size needed:%d  Available space:%d\n",
2843
			cur->apdu_len + 2, len);
2862
			cur->apdu_len + 2, len);
2844

    
   
2863

   
2845
		/* Remove APDU from list. */
2864
		/* Remove APDU from list. */
2846
		*prev = cur->next;
2865
		*prev = cur->next;
2847

    
   
2866

   
2848
		if (cur->response.callback) {
2867
		if (cur->response.callback) {
2849
			/* Indicate to callback that the APDU had a problem getting sent. */
2868
			/* Indicate to callback that the APDU had a problem getting sent. */
2850
			cur->response.callback(APDU_CALLBACK_REASON_ERROR, ctrl, call, cur, NULL);
2869
			cur->response.callback(APDU_CALLBACK_REASON_ERROR, ctrl, call, cur, NULL);
2851
		}
2870
		}
2852

    
   
2871

   
2853
		free(cur);
2872
		free(cur);
2854
		return 0;
2873
		return 0;
2855
	}
2874
	}
2856

    
   
2875

   
2857
	memcpy(ie->data, cur->apdu, cur->apdu_len);
2876
	memcpy(ie->data, cur->apdu, cur->apdu_len);
2858
	apdu_len = cur->apdu_len;
2877
	apdu_len = cur->apdu_len;
2859
	cur->sent = 1;
2878
	cur->sent = 1;
2860

    
   
2879

   
2861
	if (cur->response.callback && cur->response.timeout_time) {
2880
	if (cur->response.callback && cur->response.timeout_time) {
2862
		int failed;
2881
		int failed;
2863

    
   
2882

   
2864
		if (0 < cur->response.timeout_time) {
2883
		if (0 < cur->response.timeout_time) {
2865
			/* Sender specified a timeout duration. */
2884
			/* Sender specified a timeout duration. */
2866
			cur->timer = pri_schedule_event(ctrl, cur->response.timeout_time,
2885
			cur->timer = pri_schedule_event(ctrl, cur->response.timeout_time,
2867
				q931_apdu_timeout, cur);
2886
				q931_apdu_timeout, cur);
2868
			failed = !cur->timer;
2887
			failed = !cur->timer;
2869
		} else {
2888
		} else {
2870
			/* Sender wants to "timeout" only when specified messages are received. */
2889
			/* Sender wants to "timeout" only when specified messages are received. */
2871
			failed = !cur->response.num_messages;
2890
			failed = !cur->response.num_messages;
2872
		}
2891
		}
2873
		if (failed) {
2892
		if (failed) {
2874
			/* Remove APDU from list. */
2893
			/* Remove APDU from list. */
2875
			*prev = cur->next;
2894
			*prev = cur->next;
2876

    
   
2895

   
2877
			/* Indicate to callback that the APDU had a problem getting sent. */
2896
			/* Indicate to callback that the APDU had a problem getting sent. */
2878
			cur->response.callback(APDU_CALLBACK_REASON_ERROR, ctrl, call, cur, NULL);
2897
			cur->response.callback(APDU_CALLBACK_REASON_ERROR, ctrl, call, cur, NULL);
2879

    
   
2898

   
2880
			free(cur);
2899
			free(cur);
2881
		}
2900
		}
2882
	} else {
2901
	} else {
2883
		/* Remove APDU from list. */
2902
		/* Remove APDU from list. */
2884
		*prev = cur->next;
2903
		*prev = cur->next;
2885
		free(cur);
2904
		free(cur);
2886
	}
2905
	}
2887

    
   
2906

   
2888
	return apdu_len + 2;
2907
	return apdu_len + 2;
2889
}
2908
}
2890

    
   
2909

   
2891
static int receive_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2910
static int receive_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2892
{
2911
{
2893
	/* Delay processing facility ie's till after all other ie's are processed. */
2912
	/* Delay processing facility ie's till after all other ie's are processed. */
2894
	if (MAX_FACILITY_IES <= ctrl->facility.count) {
2913
	if (MAX_FACILITY_IES <= ctrl->facility.count) {
2895
		pri_message(ctrl, "!! Too many facility ie's to delay.\n");
2914
		pri_message(ctrl, "!! Too many facility ie's to delay.\n");
2896
		return -1;
2915
		return -1;
2897
	}
2916
	}
2898
	/* Make sure we have enough room for the protocol profile ie octet(s) */
2917
	/* Make sure we have enough room for the protocol profile ie octet(s) */
2899
	if (ie->data + ie->len < ie->data + 2) {
2918
	if (ie->data + ie->len < ie->data + 2) {
2900
		return -1;
2919
		return -1;
2901
	}
2920
	}
2902

    
   
2921

   
2903
	/* Save the facility ie location for delayed decode. */
2922
	/* Save the facility ie location for delayed decode. */
2904
	ctrl->facility.ie[ctrl->facility.count] = ie;
2923
	ctrl->facility.ie[ctrl->facility.count] = ie;
2905
	ctrl->facility.codeset[ctrl->facility.count] = Q931_IE_CODESET((unsigned) full_ie);
2924
	ctrl->facility.codeset[ctrl->facility.count] = Q931_IE_CODESET((unsigned) full_ie);
2906
	++ctrl->facility.count;
2925
	++ctrl->facility.count;
2907
	return 0;
2926
	return 0;
2908
}
2927
}
2909

    
   
2928

   
2910
static int process_facility(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie)
2929
static int process_facility(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie)
2911
{
2930
{
2912
	struct fac_extension_header header;
2931
	struct fac_extension_header header;
2913
	struct rose_message rose;
2932
	struct rose_message rose;
2914
	const unsigned char *pos;
2933
	const unsigned char *pos;
2915
	const unsigned char *end;
2934
	const unsigned char *end;
2916

    
   
2935

   
2917
	pos = ie->data;
2936
	pos = ie->data;
2918
	end = ie->data + ie->len;
2937
	end = ie->data + ie->len;
2919

    
   
2938

   
2920
	/* Make sure we have enough room for the protocol profile ie octet(s) */
2939
	/* Make sure we have enough room for the protocol profile ie octet(s) */
2921
	if (end < pos + 2) {
2940
	if (end < pos + 2) {
2922
		return -1;
2941
		return -1;
2923
	}
2942
	}
2924
	switch (*pos & Q932_PROTOCOL_MASK) {
2943
	switch (*pos & Q932_PROTOCOL_MASK) {
2925
	case Q932_PROTOCOL_ROSE:
2944
	case Q932_PROTOCOL_ROSE:
2926
	case Q932_PROTOCOL_EXTENSIONS:
2945
	case Q932_PROTOCOL_EXTENSIONS:
2927
		break;
2946
		break;
2928
	default:
2947
	default:
2929
	case Q932_PROTOCOL_CMIP:
2948
	case Q932_PROTOCOL_CMIP:
2930
	case Q932_PROTOCOL_ACSE:
2949
	case Q932_PROTOCOL_ACSE:
2931
		if (ctrl->debug & PRI_DEBUG_APDU) {
2950
		if (ctrl->debug & PRI_DEBUG_APDU) {
2932
			pri_message(ctrl,
2951
			pri_message(ctrl,
2933
				"!! Don't know how to handle Q.932 Protocol Profile type 0x%X\n",
2952
				"!! Don't know how to handle Q.932 Protocol Profile type 0x%X\n",
2934
				*pos & Q932_PROTOCOL_MASK);
2953
				*pos & Q932_PROTOCOL_MASK);
2935
		}
2954
		}
2936
		return -1;
2955
		return -1;
2937
	}
2956
	}
2938
	if (!(*pos & 0x80)) {
2957
	if (!(*pos & 0x80)) {
2939
		/* DMS-100 Service indicator octet - Just ignore for now */
2958
		/* DMS-100 Service indicator octet - Just ignore for now */
2940
		++pos;
2959
		++pos;
2941
	}
2960
	}
2942
	++pos;
2961
	++pos;
2943

    
   
2962

   
2944
	if (ctrl->debug & PRI_DEBUG_APDU) {
2963
	if (ctrl->debug & PRI_DEBUG_APDU) {
2945
		asn1_dump(ctrl, pos, end);
2964
		asn1_dump(ctrl, pos, end);
2946
	}
2965
	}
2947

    
   
2966

   
2948
	pos = fac_dec_extension_header(ctrl, pos, end, &header);
2967
	pos = fac_dec_extension_header(ctrl, pos, end, &header);
2949
	if (!pos) {
2968
	if (!pos) {
2950
		return -1;
2969
		return -1;
2951
	}
2970
	}
2952
	if (header.npp_present) {
2971
	if (header.npp_present) {
2953
		if (ctrl->debug & PRI_DEBUG_APDU) {
2972
		if (ctrl->debug & PRI_DEBUG_APDU) {
2954
			pri_message(ctrl,
2973
			pri_message(ctrl,
2955
				"!! Don't know how to handle Network Protocol Profile type 0x%X\n",
2974
				"!! Don't know how to handle Network Protocol Profile type 0x%X\n",
2956
				header.npp);
2975
				header.npp);
2957
		}
2976
		}
2958
		return -1;
2977
		return -1;
2959
	}
2978
	}
2960

    
   
2979

   
2961
	/* Process all components in the facility. */
2980
	/* Process all components in the facility. */
2962
	while (pos < end) {
2981
	while (pos < end) {
2963
		pos = rose_decode(ctrl, pos, end, &rose);
2982
		pos = rose_decode(ctrl, pos, end, &rose);
2964
		if (!pos) {
2983
		if (!pos) {
2965
			return -1;
2984
			return -1;
2966
		}
2985
		}
2967
		switch (rose.type) {
2986
		switch (rose.type) {
2968
		case ROSE_COMP_TYPE_INVOKE:
2987
		case ROSE_COMP_TYPE_INVOKE:
2969
			rose_handle_invoke(ctrl, call, msgtype, ie, &header, &rose.component.invoke);
2988
			rose_handle_invoke(ctrl, call, msgtype, ie, &header, &rose.component.invoke);
2970
			break;
2989
			break;
2971
		case ROSE_COMP_TYPE_RESULT:
2990
		case ROSE_COMP_TYPE_RESULT:
2972
			rose_handle_result(ctrl, call, msgtype, ie, &header, &rose.component.result);
2991
			rose_handle_result(ctrl, call, msgtype, ie, &header, &rose.component.result);
2973
			break;
2992
			break;
2974
		case ROSE_COMP_TYPE_ERROR:
2993
		case ROSE_COMP_TYPE_ERROR:
2975
			rose_handle_error(ctrl, call, msgtype, ie, &header, &rose.component.error);
2994
			rose_handle_error(ctrl, call, msgtype, ie, &header, &rose.component.error);
2976
			break;
2995
			break;
2977
		case ROSE_COMP_TYPE_REJECT:
2996
		case ROSE_COMP_TYPE_REJECT:
2978
			rose_handle_reject(ctrl, call, msgtype, ie, &header, &rose.component.reject);
2997
			rose_handle_reject(ctrl, call, msgtype, ie, &header, &rose.component.reject);
2979
			break;
2998
			break;
2980
		default:
2999
		default:
2981
			return -1;
3000
			return -1;
2982
		}
3001
		}
2983
	}
3002
	}
2984
	return 0;
3003
	return 0;
2985
}
3004
}
2986

    
   
3005

   
2987
static void q931_handle_facilities(struct pri *ctrl, q931_call *call, int msgtype)
3006
static void q931_handle_facilities(struct pri *ctrl, q931_call *call, int msgtype)
2988
{
3007
{
2989
	unsigned idx;
3008
	unsigned idx;
2990
	unsigned codeset;
3009
	unsigned codeset;
2991
	unsigned full_ie;
3010
	unsigned full_ie;
2992
	q931_ie *ie;
3011
	q931_ie *ie;
2993

    
   
3012

   
2994
	for (idx = 0; idx < ctrl->facility.count; ++idx) {
3013
	for (idx = 0; idx < ctrl->facility.count; ++idx) {
2995
		ie = ctrl->facility.ie[idx];
3014
		ie = ctrl->facility.ie[idx];
2996
		if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
3015
		if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
2997
			codeset = ctrl->facility.codeset[idx];
3016
			codeset = ctrl->facility.codeset[idx];
2998
			full_ie = Q931_FULL_IE(codeset, ie->ie);
3017
			full_ie = Q931_FULL_IE(codeset, ie->ie);
2999
			pri_message(ctrl, "-- Delayed processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
3018
			pri_message(ctrl, "-- Delayed processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
3000
		}
3019
		}
3001
		process_facility(ctrl, call, msgtype, ie);
3020
		process_facility(ctrl, call, msgtype, ie);
3002
	}
3021
	}
3003
}
3022
}
3004

    
   
3023

   
3005
/*!
3024
/*!
3006
 * \internal
3025
 * \internal
3007
 * \brief Check if any APDU responses "timeout" with the current Q.931 message.
3026
 * \brief Check if any APDU responses "timeout" with the current Q.931 message.
3008
 *
3027
 *
3009
 * \param ctrl D channel controller.
3028
 * \param ctrl D channel controller.
3010
 * \param call Q.931 call leg.
3029
 * \param call Q.931 call leg.
3011
 * \param msgtype Q.931 message type received.
3030
 * \param msgtype Q.931 message type received.
3012
 *
3031
 *
3013
 * \return Nothing
3032
 * \return Nothing
3014
 */
3033
 */
3015
static void q931_apdu_msg_expire(struct pri *ctrl, struct q931_call *call, int msgtype)
3034
static void q931_apdu_msg_expire(struct pri *ctrl, struct q931_call *call, int msgtype)
3016
{
3035
{
3017
	struct apdu_event **prev;
3036
	struct apdu_event **prev;
3018
	struct apdu_event **prev_next;
3037
	struct apdu_event **prev_next;
3019
	struct apdu_event *cur;
3038
	struct apdu_event *cur;
3020
	unsigned idx;
3039
	unsigned idx;
3021

    
   
3040

   
3022
	for (prev = &call->apdus; *prev; prev = prev_next) {
3041
	for (prev = &call->apdus; *prev; prev = prev_next) {
3023
		cur = *prev;
3042
		cur = *prev;
3024
		prev_next = &cur->next;
3043
		prev_next = &cur->next;
3025
		if (cur->sent) {
3044
		if (cur->sent) {
3026
			for (idx = 0; idx < cur->response.num_messages; ++idx) {
3045
			for (idx = 0; idx < cur->response.num_messages; ++idx) {
3027
				if (cur->response.message_type[idx] == msgtype) {
3046
				if (cur->response.message_type[idx] == msgtype) {
3028
					/*
3047
					/*
3029
					 * APDU response message "timeout".
3048
					 * APDU response message "timeout".
3030
					 *
3049
					 *
3031
					 * Extract the APDU from the list so it cannot be
3050
					 * Extract the APDU from the list so it cannot be
3032
					 * deleted from under us by the callback.
3051
					 * deleted from under us by the callback.
3033
					 */
3052
					 */
3034
					prev_next = prev;
3053
					prev_next = prev;
3035
					*prev = cur->next;
3054
					*prev = cur->next;
3036

    
   
3055

   
3037
					/* Stop any response timeout. */
3056
					/* Stop any response timeout. */
3038
					pri_schedule_del(ctrl, cur->timer);
3057
					pri_schedule_del(ctrl, cur->timer);
3039
					cur->timer = 0;
3058
					cur->timer = 0;
3040

    
   
3059

   
3041
					cur->response.callback(APDU_CALLBACK_REASON_TIMEOUT, ctrl, call, cur, NULL);
3060
					cur->response.callback(APDU_CALLBACK_REASON_TIMEOUT, ctrl, call, cur, NULL);
3042

    
   
3061

   
3043
					free(cur);
3062
					free(cur);
3044
					break;
3063
					break;
3045
				}
3064
				}
3046
			}
3065
			}
3047
		}
3066
		}
3048
	}
3067
	}
3049
}
3068
}
3050

    
   
3069

   
3051
static int transmit_progress_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3070
static int transmit_progress_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3052
{
3071
{
3053
	int code, mask;
3072
	int code, mask;
3054
	/* Can't send progress indicator on GR-303 -- EVER! */
3073
	/* Can't send progress indicator on GR-303 -- EVER! */
3055
	if (ctrl->link.next && !ctrl->bri)
3074
	if (ctrl->link.next && !ctrl->bri)
3056
		return 0;
3075
		return 0;
3057
	if (call->progressmask > 0) {
3076
	if (call->progressmask > 0) {
3058
		if (call->progressmask & (mask = PRI_PROG_CALL_NOT_E2E_ISDN))
3077
		if (call->progressmask & (mask = PRI_PROG_CALL_NOT_E2E_ISDN))
3059
			code = Q931_PROG_CALL_NOT_E2E_ISDN;
3078
			code = Q931_PROG_CALL_NOT_E2E_ISDN;
3060
		else if (call->progressmask & (mask = PRI_PROG_CALLED_NOT_ISDN))
3079
		else if (call->progressmask & (mask = PRI_PROG_CALLED_NOT_ISDN))
3061
			code = Q931_PROG_CALLED_NOT_ISDN;
3080
			code = Q931_PROG_CALLED_NOT_ISDN;
3062
		else if (call->progressmask & (mask = PRI_PROG_CALLER_NOT_ISDN))
3081
		else if (call->progressmask & (mask = PRI_PROG_CALLER_NOT_ISDN))
3063
			code = Q931_PROG_CALLER_NOT_ISDN;
3082
			code = Q931_PROG_CALLER_NOT_ISDN;
3064
		else if (call->progressmask & (mask = PRI_PROG_INBAND_AVAILABLE))
3083
		else if (call->progressmask & (mask = PRI_PROG_INBAND_AVAILABLE))
3065
			code = Q931_PROG_INBAND_AVAILABLE;
3084
			code = Q931_PROG_INBAND_AVAILABLE;
3066
		else if (call->progressmask & (mask = PRI_PROG_DELAY_AT_INTERF))
3085
		else if (call->progressmask & (mask = PRI_PROG_DELAY_AT_INTERF))
3067
			code = Q931_PROG_DELAY_AT_INTERF;
3086
			code = Q931_PROG_DELAY_AT_INTERF;
3068
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_WITH_PUBLIC))
3087
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_WITH_PUBLIC))
3069
			code = Q931_PROG_INTERWORKING_WITH_PUBLIC;
3088
			code = Q931_PROG_INTERWORKING_WITH_PUBLIC;
3070
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE))
3089
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE))
3071
			code = Q931_PROG_INTERWORKING_NO_RELEASE;
3090
			code = Q931_PROG_INTERWORKING_NO_RELEASE;
3072
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER))
3091
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER))
3073
			code = Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER;
3092
			code = Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER;
3074
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER))
3093
		else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER))
3075
			code = Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER;
3094
			code = Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER;
3076
		else {
3095
		else {
3077
			code = 0;
3096
			code = 0;
3078
			pri_error(ctrl, "XXX Undefined progress bit: %x\n", call->progressmask);
3097
			pri_error(ctrl, "XXX Undefined progress bit: %x\n", call->progressmask);
3079
		}
3098
		}
3080
		if (code) {
3099
		if (code) {
3081
			ie->data[0] = 0x80 | (call->progcode << 5)  | (call->progloc);
3100
			ie->data[0] = 0x80 | (call->progcode << 5)  | (call->progloc);
3082
			ie->data[1] = 0x80 | code;
3101
			ie->data[1] = 0x80 | code;
3083
			call->progressmask &= ~mask;
3102
			call->progressmask &= ~mask;
3084
			return 4;
3103
			return 4;
3085
		}
3104
		}
3086
	}
3105
	}
3087
	/* Leave off */
3106
	/* Leave off */
3088
	return 0;
3107
	return 0;
3089
}
3108
}
3090
static int transmit_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3109
static int transmit_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3091
{
3110
{
3092
	ie->data[0] = Q931_CALL_STATE_NULL;
3111
	ie->data[0] = Q931_CALL_STATE_NULL;
3093
	switch (call->ourcallstate) {
3112
	switch (call->ourcallstate) {
3094
	case Q931_CALL_STATE_NULL:
3113
	case Q931_CALL_STATE_NULL:
3095
	case Q931_CALL_STATE_CALL_INITIATED:
3114
	case Q931_CALL_STATE_CALL_INITIATED:
3096
	case Q931_CALL_STATE_OVERLAP_SENDING:
3115
	case Q931_CALL_STATE_OVERLAP_SENDING:
3097
	case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
3116
	case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
3098
	case Q931_CALL_STATE_CALL_DELIVERED:
3117
	case Q931_CALL_STATE_CALL_DELIVERED:
3099
	case Q931_CALL_STATE_CALL_PRESENT:
3118
	case Q931_CALL_STATE_CALL_PRESENT:
3100
	case Q931_CALL_STATE_CALL_RECEIVED:
3119
	case Q931_CALL_STATE_CALL_RECEIVED:
3101
	case Q931_CALL_STATE_CONNECT_REQUEST:
3120
	case Q931_CALL_STATE_CONNECT_REQUEST:
3102
	case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
3121
	case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
3103
	case Q931_CALL_STATE_ACTIVE:
3122
	case Q931_CALL_STATE_ACTIVE:
3104
	case Q931_CALL_STATE_DISCONNECT_REQUEST:
3123
	case Q931_CALL_STATE_DISCONNECT_REQUEST:
3105
	case Q931_CALL_STATE_DISCONNECT_INDICATION:
3124
	case Q931_CALL_STATE_DISCONNECT_INDICATION:
3106
	case Q931_CALL_STATE_SUSPEND_REQUEST:
3125
	case Q931_CALL_STATE_SUSPEND_REQUEST:
3107
	case Q931_CALL_STATE_RESUME_REQUEST:
3126
	case Q931_CALL_STATE_RESUME_REQUEST:
3108
	case Q931_CALL_STATE_RELEASE_REQUEST:
3127
	case Q931_CALL_STATE_RELEASE_REQUEST:
3109
	case Q931_CALL_STATE_CALL_ABORT:
3128
	case Q931_CALL_STATE_CALL_ABORT:
3110
	case Q931_CALL_STATE_OVERLAP_RECEIVING:
3129
	case Q931_CALL_STATE_OVERLAP_RECEIVING:
3111
	case Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE:
3130
	case Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE:
3112
	case Q931_CALL_STATE_RESTART_REQUEST:
3131
	case Q931_CALL_STATE_RESTART_REQUEST:
3113
	case Q931_CALL_STATE_RESTART:
3132
	case Q931_CALL_STATE_RESTART:
3114
		ie->data[0] = call->ourcallstate;
3133
		ie->data[0] = call->ourcallstate;
3115
		break;
3134
		break;
3116
	case Q931_CALL_STATE_NOT_SET:
3135
	case Q931_CALL_STATE_NOT_SET:
3117
		break;
3136
		break;
3118
	}
3137
	}
3119
	return 3;
3138
	return 3;
3120
}
3139
}
3121

    
   
3140

   
3122
static int receive_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3141
static int receive_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3123
{
3142
{
3124
	call->sugcallstate = ie->data[0] & 0x3f;
3143
	call->sugcallstate = ie->data[0] & 0x3f;
3125
	return 0;
3144
	return 0;
3126
}
3145
}
3127

    
   
3146

   
3128
/*!
3147
/*!
3129
 * \brief Convert the internal Q.931 call state to a string.
3148
 * \brief Convert the internal Q.931 call state to a string.
3130
 *
3149
 *
3131
 * \param callstate Internal Q.931 call state.
3150
 * \param callstate Internal Q.931 call state.
3132
 *
3151
 *
3133
 * \return String equivalent of the given Q.931 call state.
3152
 * \return String equivalent of the given Q.931 call state.
3134
 */
3153
 */
3135
const char *q931_call_state_str(enum Q931_CALL_STATE callstate)
3154
const char *q931_call_state_str(enum Q931_CALL_STATE callstate)
3136
{
3155
{
3137
	static struct msgtype callstates[] = {
3156
	static struct msgtype callstates[] = {
3138
/* *INDENT-OFF* */
3157
/* *INDENT-OFF* */
3139
		{ Q931_CALL_STATE_NULL,                     "Null" },
3158
		{ Q931_CALL_STATE_NULL,                     "Null" },
3140
		{ Q931_CALL_STATE_CALL_INITIATED,           "Call Initiated" },
3159
		{ Q931_CALL_STATE_CALL_INITIATED,           "Call Initiated" },
3141
		{ Q931_CALL_STATE_OVERLAP_SENDING,          "Overlap Sending" },
3160
		{ Q931_CALL_STATE_OVERLAP_SENDING,          "Overlap Sending" },
3142
		{ Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING, "Outgoing Call Proceeding" },
3161
		{ Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING, "Outgoing Call Proceeding" },
3143
		{ Q931_CALL_STATE_CALL_DELIVERED,           "Call Delivered" },
3162
		{ Q931_CALL_STATE_CALL_DELIVERED,           "Call Delivered" },
3144
		{ Q931_CALL_STATE_CALL_PRESENT,             "Call Present" },
3163
		{ Q931_CALL_STATE_CALL_PRESENT,             "Call Present" },
3145
		{ Q931_CALL_STATE_CALL_RECEIVED,            "Call Received" },
3164
		{ Q931_CALL_STATE_CALL_RECEIVED,            "Call Received" },
3146
		{ Q931_CALL_STATE_CONNECT_REQUEST,          "Connect Request" },
3165
		{ Q931_CALL_STATE_CONNECT_REQUEST,          "Connect Request" },
3147
		{ Q931_CALL_STATE_INCOMING_CALL_PROCEEDING, "Incoming Call Proceeding" },
3166
		{ Q931_CALL_STATE_INCOMING_CALL_PROCEEDING, "Incoming Call Proceeding" },
3148
		{ Q931_CALL_STATE_ACTIVE,                   "Active" },
3167
		{ Q931_CALL_STATE_ACTIVE,                   "Active" },
3149
		{ Q931_CALL_STATE_DISCONNECT_REQUEST,       "Disconnect Request" },
3168
		{ Q931_CALL_STATE_DISCONNECT_REQUEST,       "Disconnect Request" },
3150
		{ Q931_CALL_STATE_DISCONNECT_INDICATION,    "Disconnect Indication" },
3169
		{ Q931_CALL_STATE_DISCONNECT_INDICATION,    "Disconnect Indication" },
3151
		{ Q931_CALL_STATE_SUSPEND_REQUEST,          "Suspend Request" },
3170
		{ Q931_CALL_STATE_SUSPEND_REQUEST,          "Suspend Request" },
3152
		{ Q931_CALL_STATE_RESUME_REQUEST,           "Resume Request" },
3171
		{ Q931_CALL_STATE_RESUME_REQUEST,           "Resume Request" },
3153
		{ Q931_CALL_STATE_RELEASE_REQUEST,          "Release Request" },
3172
		{ Q931_CALL_STATE_RELEASE_REQUEST,          "Release Request" },
3154
		{ Q931_CALL_STATE_CALL_ABORT,               "Call Abort" },
3173
		{ Q931_CALL_STATE_CALL_ABORT,               "Call Abort" },
3155
		{ Q931_CALL_STATE_OVERLAP_RECEIVING,        "Overlap Receiving" },
3174
		{ Q931_CALL_STATE_OVERLAP_RECEIVING,        "Overlap Receiving" },
3156
		{ Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE, "Call Independent Service" },
3175
		{ Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE, "Call Independent Service" },
3157
		{ Q931_CALL_STATE_RESTART_REQUEST,          "Restart Request" },
3176
		{ Q931_CALL_STATE_RESTART_REQUEST,          "Restart Request" },
3158
		{ Q931_CALL_STATE_RESTART,                  "Restart" },
3177
		{ Q931_CALL_STATE_RESTART,                  "Restart" },
3159
		{ Q931_CALL_STATE_NOT_SET,                  "Not set. Internal use only." },
3178
		{ Q931_CALL_STATE_NOT_SET,                  "Not set. Internal use only." },
3160
/* *INDENT-ON* */
3179
/* *INDENT-ON* */
3161
	};
3180
	};
3162
	return code2str(callstate, callstates, ARRAY_LEN(callstates));
3181
	return code2str(callstate, callstates, ARRAY_LEN(callstates));
3163
}
3182
}
3164

    
   
3183

   
3165
/*!
3184
/*!
3166
 * \internal
3185
 * \internal
3167
 * \brief Convert the Q.932 supplementary hold state to a string.
3186
 * \brief Convert the Q.932 supplementary hold state to a string.
3168
 *
3187
 *
3169
 * \param state Q.932 supplementary hold state.
3188
 * \param state Q.932 supplementary hold state.
3170
 *
3189
 *
3171
 * \return String equivalent of the given hold state.
3190
 * \return String equivalent of the given hold state.
3172
 */
3191
 */
3173
static const char *q931_hold_state_str(enum Q931_HOLD_STATE state)
3192
static const char *q931_hold_state_str(enum Q931_HOLD_STATE state)
3174
{
3193
{
3175
	static struct msgtype hold_states[] = {
3194
	static struct msgtype hold_states[] = {
3176
/* *INDENT-OFF* */
3195
/* *INDENT-OFF* */
3177
		{ Q931_HOLD_STATE_IDLE,         "Idle" },
3196
		{ Q931_HOLD_STATE_IDLE,         "Idle" },
3178
		{ Q931_HOLD_STATE_HOLD_REQ,     "Hold Request" },
3197
		{ Q931_HOLD_STATE_HOLD_REQ,     "Hold Request" },
3179
		{ Q931_HOLD_STATE_HOLD_IND,     "Hold Indication" },
3198
		{ Q931_HOLD_STATE_HOLD_IND,     "Hold Indication" },
3180
		{ Q931_HOLD_STATE_CALL_HELD,    "Call Held" },
3199
		{ Q931_HOLD_STATE_CALL_HELD,    "Call Held" },
3181
		{ Q931_HOLD_STATE_RETRIEVE_REQ, "Retrieve Request" },
3200
		{ Q931_HOLD_STATE_RETRIEVE_REQ, "Retrieve Request" },
3182
		{ Q931_HOLD_STATE_RETRIEVE_IND, "Retrieve Indication" },
3201
		{ Q931_HOLD_STATE_RETRIEVE_IND, "Retrieve Indication" },
3183
/* *INDENT-ON* */
3202
/* *INDENT-ON* */
3184
	};
3203
	};
3185
	return code2str(state, hold_states, ARRAY_LEN(hold_states));
3204
	return code2str(state, hold_states, ARRAY_LEN(hold_states));
3186
}
3205
}
3187

    
   
3206

   
3188
static void dump_call_state(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3207
static void dump_call_state(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3189
{
3208
{
3190
	pri_message(ctrl, "%c Call State (len=%2d) [ Ext: %d  Coding: %s (%d)  Call state: %s (%d)\n",
3209
	pri_message(ctrl,
3191
		prefix, len, ie->data[0] >> 7, coding2str((ie->data[0] & 0xC0) >> 6), (ie->data[0] & 0xC0) >> 6,
3210
		"%c %s (len=%2d) [ Ext: %d  Coding: %s (%d)  Call state: %s (%d)\n",

    
   
3211
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
3212
		coding2str((ie->data[0] & 0xC0) >> 6), (ie->data[0] & 0xC0) >> 6,
3192
		q931_call_state_str(ie->data[0] & 0x3f), ie->data[0] & 0x3f);
3213
		q931_call_state_str(ie->data[0] & 0x3f), ie->data[0] & 0x3f);
3193
}
3214
}
3194

    
   
3215

   
3195
static void dump_call_identity(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3216
static void dump_call_identity(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3196
{
3217
{
3197
	int x;
3218
	int x;
3198
	pri_message(ctrl, "%c Call Identity (len=%2d) [ ", prefix, len);
3219
	pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), len);
3199
	for (x=0;x<ie->len;x++) 
3220
	for (x = 0; x < ie->len; x++) 
3200
		pri_message(ctrl, "0x%02X ", ie->data[x]);
3221
		pri_message(ctrl, "0x%02X ", ie->data[x]);
3201
	pri_message(ctrl, " ]\n");
3222
	pri_message(ctrl, " ]\n");
3202
}
3223
}
3203

    
   
3224

   
3204
static void dump_time_date(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3225
static void dump_time_date(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3205
{
3226
{
3206
	pri_message(ctrl, "%c Time Date (len=%2d) [ ", prefix, len);
3227
	pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), len);
3207
	if (ie->len > 0)
3228
	if (ie->len > 0)
3208
		pri_message(ctrl, "%02d", ie->data[0]);
3229
		pri_message(ctrl, "%02d", ie->data[0]);
3209
	if (ie->len > 1)
3230
	if (ie->len > 1)
3210
		pri_message(ctrl, "-%02d", ie->data[1]);
3231
		pri_message(ctrl, "-%02d", ie->data[1]);
3211
	if (ie->len > 2)
3232
	if (ie->len > 2)
3212
		pri_message(ctrl, "-%02d", ie->data[2]);
3233
		pri_message(ctrl, "-%02d", ie->data[2]);
3213
	if (ie->len > 3)
3234
	if (ie->len > 3)
3214
		pri_message(ctrl, " %02d", ie->data[3]);
3235
		pri_message(ctrl, " %02d", ie->data[3]);
3215
	if (ie->len > 4)
3236
	if (ie->len > 4)
3216
		pri_message(ctrl, ":%02d", ie->data[4]);
3237
		pri_message(ctrl, ":%02d", ie->data[4]);
3217
	if (ie->len > 5)
3238
	if (ie->len > 5)
3218
		pri_message(ctrl, ":%02d", ie->data[5]);
3239
		pri_message(ctrl, ":%02d", ie->data[5]);
3219
	pri_message(ctrl, " ]\n");
3240
	pri_message(ctrl, " ]\n");
3220
}
3241
}
3221

    
   
3242

   
3222
static int receive_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3243
static int receive_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3223
{
3244
{
3224
	/* Ignore incoming Date/Time since we have no use for it at this time. */
3245
	/* Ignore incoming Date/Time since we have no use for it at this time. */
3225
	return 0;
3246
	return 0;
3226
}
3247
}
3227

    
   
3248

   
3228
static int transmit_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3249
static int transmit_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3229
{
3250
{
3230
	time_t now;
3251
	time_t now;
3231
	struct tm timedate;
3252
	struct tm timedate;
3232
	int ie_len;
3253
	int ie_len;
3233

    
   
3254

   
3234
	do {
3255
	do {
3235
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE) {
3256
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE) {
3236
			ie_len = 0;
3257
			ie_len = 0;
3237
			break;
3258
			break;
3238
		}
3259
		}
3239

    
   
3260

   
3240
		/* Send the current date/time. */
3261
		/* Send the current date/time. */
3241
		time(&now);
3262
		time(&now);
3242
		localtime_r(&now, &timedate);
3263
		localtime_r(&now, &timedate);
3243
		ie->data[0] = timedate.tm_year - 100; /* 1900+ */
3264
		ie->data[0] = timedate.tm_year - 100; /* 1900+ */
3244
		ie->data[1] = timedate.tm_mon + 1;
3265
		ie->data[1] = timedate.tm_mon + 1;
3245
		ie->data[2] = timedate.tm_mday;
3266
		ie->data[2] = timedate.tm_mday;
3246
		ie_len = 2 + 3;
3267
		ie_len = 2 + 3;
3247
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HH) {
3268
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HH) {
3248
			break;
3269
			break;
3249
		}
3270
		}
3250

    
   
3271

   
3251
		/* Add optional hour. */
3272
		/* Add optional hour. */
3252
		ie->data[3] = timedate.tm_hour;
3273
		ie->data[3] = timedate.tm_hour;
3253
		++ie_len;
3274
		++ie_len;
3254
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMM) {
3275
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMM) {
3255
			break;
3276
			break;
3256
		}
3277
		}
3257

    
   
3278

   
3258
		/* Add optional minutes. */
3279
		/* Add optional minutes. */
3259
		ie->data[4] = timedate.tm_min;
3280
		ie->data[4] = timedate.tm_min;
3260
		++ie_len;
3281
		++ie_len;
3261
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMMSS) {
3282
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMMSS) {
3262
			break;
3283
			break;
3263
		}
3284
		}
3264

    
   
3285

   
3265
		/* Add optional seconds. */
3286
		/* Add optional seconds. */
3266
		ie->data[5] = timedate.tm_sec;
3287
		ie->data[5] = timedate.tm_sec;
3267
		++ie_len;
3288
		++ie_len;
3268
	} while (0);
3289
	} while (0);
3269
	return ie_len;
3290
	return ie_len;
3270
}
3291
}
3271

    
   
3292

   
3272
static void dump_keypad_facility(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3293
static void dump_keypad_facility(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3273
{
3294
{
3274
	unsigned char tmp[64];
3295
	unsigned char tmp[64];
3275

    
   
3296

   
3276
	q931_strget(tmp, sizeof(tmp), ie->data, ie->len);
3297
	q931_strget(tmp, sizeof(tmp), ie->data, ie->len);
3277
	pri_message(ctrl, "%c Keypad Facility (len=%2d) [ %s ]\n", prefix, ie->len, tmp);
3298
	pri_message(ctrl,

    
   
3299
		"%c %s (len=%2d) [ %s ]\n", prefix, ie2str(full_ie), ie->len, tmp);
3278
}
3300
}
3279

    
   
3301

   
3280
static int receive_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3302
static int receive_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3281
{
3303
{
3282
	if (ie->len == 0)
3304
	if (ie->len == 0)
3283
		return -1;
3305
		return -1;
3284

    
   
3306

   
3285
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->keypad_digits,
3307
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->keypad_digits,
3286
		sizeof(call->keypad_digits), ie->data, ie->len);
3308
		sizeof(call->keypad_digits), ie->data, ie->len);
3287

    
   
3309

   
3288
	return 0;
3310
	return 0;
3289
}
3311
}
3290

    
   
3312

   
3291
static int transmit_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3313
static int transmit_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3292
{
3314
{
3293
	int sublen;
3315
	int sublen;
3294

    
   
3316

   
3295
	sublen = strlen(call->keypad_digits);
3317
	sublen = strlen(call->keypad_digits);
3296
	if (sublen) {
3318
	if (sublen) {
3297
		libpri_copy_string((char *) ie->data, call->keypad_digits, sizeof(call->keypad_digits));
3319
		libpri_copy_string((char *) ie->data, call->keypad_digits, sizeof(call->keypad_digits));
3298
		return sublen + 2;
3320
		return sublen + 2;
3299
	} else
3321
	} else
3300
		return 0;
3322
		return 0;
3301
}
3323
}
3302

    
   
3324

   
3303
static void dump_display(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3325
static void dump_display(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3304
{
3326
{
3305
	int x;
3327
	int x;
3306
	unsigned char buf[2*80 + 1];
3328
	unsigned char buf[2*80 + 1];
3307
	char tmp[20 + 1];
3329
	char tmp[20 + 1];
3308

    
   
3330

   
3309
	x = 0;
3331
	x = 0;
3310
	if (ie->len && (ie->data[x] & 0x80)) {
3332
	if (ie->len && (ie->data[x] & 0x80)) {
3311
		snprintf(tmp, sizeof(tmp), "Charset: %02x ", ie->data[x] & 0x7f);
3333
		snprintf(tmp, sizeof(tmp), "Charset: %02x ", ie->data[x] & 0x7f);
3312
		++x;
3334
		++x;
3313
	} else {
3335
	} else {
3314
		tmp[0] = '\0';
3336
		tmp[0] = '\0';
3315
	}
3337
	}
3316
	q931_strget(buf, sizeof(buf), &ie->data[x], ie->len - x);
3338
	q931_strget(buf, sizeof(buf), &ie->data[x], ie->len - x);
3317
	pri_message(ctrl, "%c Display (len=%2d) %s[ %s ]\n", prefix, ie->len, tmp, buf);
3339
	pri_message(ctrl,

    
   
3340
		"%c %s (len=%2d) %s[ %s ]\n", prefix, ie2str(full_ie), ie->len, tmp, buf);
3318
}
3341
}
3319

    
   
3342

   
3320
#define CHECK_OVERFLOW(limit) \
3343
#define CHECK_OVERFLOW(limit) \
3321
	if (tmpptr - tmp + limit >= sizeof(tmp)) { \
3344
	if (tmpptr - tmp + limit >= sizeof(tmp)) { \
3322
		*tmpptr = '\0'; \
3345
		*tmpptr = '\0'; \
3323
		pri_message(ctrl, "%s", tmpptr = tmp); \
3346
		pri_message(ctrl, "%s", tmpptr = tmp); \
3324
	}
3347
	}
3325

    
   
3348

   
3326
static void dump_ie_data(struct pri *ctrl, unsigned char *c, int len)
3349
static void dump_ie_data(struct pri *ctrl, unsigned char *c, int len)
3327
{
3350
{
3328
	static char hexs[16] = "0123456789ABCDEF";
3351
	static char hexs[16] = "0123456789ABCDEF";
3329
	char tmp[1024], *tmpptr;
3352
	char tmp[1024], *tmpptr;
3330
	int lastascii = 0;
3353
	int lastascii = 0;
3331
	tmpptr = tmp;
3354
	tmpptr = tmp;
3332
	for (; len; --len, ++c) {
3355
	for (; len; --len, ++c) {
3333
		CHECK_OVERFLOW(7);
3356
		CHECK_OVERFLOW(7);
3334
		if (isprint(*c)) {
3357
		if (isprint(*c)) {
3335
			if (!lastascii) {
3358
			if (!lastascii) {
3336
				if (tmpptr != tmp) { 
3359
				if (tmpptr != tmp) { 
3337
					*tmpptr++ = ',';
3360
					*tmpptr++ = ',';
3338
					*tmpptr++ = ' ';
3361
					*tmpptr++ = ' ';
3339
				}
3362
				}
3340
				*tmpptr++ = '\'';
3363
				*tmpptr++ = '\'';
3341
				lastascii = 1;
3364
				lastascii = 1;
3342
			}
3365
			}
3343
			*tmpptr++ = *c;
3366
			*tmpptr++ = *c;
3344
		} else {
3367
		} else {
3345
			if (lastascii) {
3368
			if (lastascii) {
3346
				*tmpptr++ = '\'';
3369
				*tmpptr++ = '\'';
3347
				lastascii = 0;
3370
				lastascii = 0;
3348
			}
3371
			}
3349
			if (tmpptr != tmp) { 
3372
			if (tmpptr != tmp) { 
3350
				*tmpptr++ = ',';
3373
				*tmpptr++ = ',';
3351
				*tmpptr++ = ' ';
3374
				*tmpptr++ = ' ';
3352
			}
3375
			}
3353
			*tmpptr++ = '0';
3376
			*tmpptr++ = '0';
3354
			*tmpptr++ = 'x';
3377
			*tmpptr++ = 'x';
3355
			*tmpptr++ = hexs[(*c >> 4) & 0x0f];
3378
			*tmpptr++ = hexs[(*c >> 4) & 0x0f];
3356
			*tmpptr++ = hexs[(*c) & 0x0f];
3379
			*tmpptr++ = hexs[(*c) & 0x0f];
3357
		}
3380
		}
3358
	}
3381
	}
3359
	if (lastascii)
3382
	if (lastascii)
3360
		*tmpptr++ = '\'';
3383
		*tmpptr++ = '\'';
3361
	*tmpptr = '\0';
3384
	*tmpptr = '\0';
3362
	pri_message(ctrl, "%s", tmp);
3385
	pri_message(ctrl, "%s", tmp);
3363
}
3386
}
3364

    
   
3387

   
3365
static void dump_facility(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3388
static void dump_facility(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3366
{
3389
{
3367
	pri_message(ctrl, "%c Facility (len=%2d, codeset=%d) [ ", prefix, len, Q931_IE_CODESET(full_ie));
3390
	pri_message(ctrl, "%c %s (len=%2d, codeset=%d) [ ",

    
   
3391
		prefix, ie2str(full_ie), len, Q931_IE_CODESET(full_ie));
3368
	dump_ie_data(ctrl, ie->data, ie->len);
3392
	dump_ie_data(ctrl, ie->data, ie->len);
3369
	pri_message(ctrl, " ]\n");
3393
	pri_message(ctrl, " ]\n");
3370
#if 0	/* Lets not dump parse of facility contents here anymore. */
3394
#if 0	/* Lets not dump parse of facility contents here anymore. */
3371
	/*
3395
	/*
3372
	 * The ASN.1 decode dump has already been done when the facility ie was added to the outgoing
3396
	 * The ASN.1 decode dump has already been done when the facility ie was added to the outgoing
3373
	 * message or the ASN.1 decode dump will be done when the facility ie is processed on incoming
3397
	 * message or the ASN.1 decode dump will be done when the facility ie is processed on incoming
3374
	 * messages.  This dump is redundant and very noisy.
3398
	 * messages.  This dump is redundant and very noisy.
3375
	 */
3399
	 */
3376
	if (ie->len > 1) {
3400
	if (ie->len > 1) {
3377
		int dataat = (ie->data[0] & 0x80) ? 1 : 2;
3401
		int dataat = (ie->data[0] & 0x80) ? 1 : 2;
3378

    
   
3402

   
3379
		pri_message(ctrl, "PROTOCOL %02X\n", ie->data[0] & Q932_PROTOCOL_MASK);
3403
		pri_message(ctrl, "PROTOCOL %02X\n", ie->data[0] & Q932_PROTOCOL_MASK);
3380
		asn1_dump(ctrl, ie->data + dataat, ie->data + ie->len);
3404
		asn1_dump(ctrl, ie->data + dataat, ie->data + ie->len);
3381
	}
3405
	}
3382
#endif	/* Lets not dump parse of facility contents here anymore. */
3406
#endif	/* Lets not dump parse of facility contents here anymore. */
3383
}
3407
}
3384

    
   
3408

   
3385
static void dump_network_spec_fac(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3409
static void dump_network_spec_fac(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3386
{
3410
{
3387
	pri_message(ctrl, "%c Network-Specific Facilities (len=%2d) [ ", prefix, ie->len);
3411
	pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), ie->len);
3388
	if (ie->data[0] == 0x00) {
3412
	if (ie->data[0] == 0x00) {
3389
 		pri_message(ctrl, "%s", code2str(ie->data[1], facilities, ARRAY_LEN(facilities)));
3413
 		pri_message(ctrl, "%s", code2str(ie->data[1], facilities, ARRAY_LEN(facilities)));
3390
	}
3414
	}
3391
	else
3415
	else
3392
 		dump_ie_data(ctrl, ie->data, ie->len);
3416
 		dump_ie_data(ctrl, ie->data, ie->len);
3393
	pri_message(ctrl, " ]\n");
3417
	pri_message(ctrl, " ]\n");
3394
}
3418
}
3395

    
   
3419

   
3396
static int receive_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3420
static int receive_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3397
{
3421
{
3398
	return 0;
3422
	return 0;
3399
}
3423
}
3400

    
   
3424

   
3401
static int transmit_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3425
static int transmit_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3402
{
3426
{
3403
	/* We are ready to transmit single IE only */
3427
	/* We are ready to transmit single IE only */
3404
	if (order > 1)
3428
	if (order > 1)
3405
		return 0;
3429
		return 0;
3406

    
   
3430

   
3407
	if (ctrl->nsf != PRI_NSF_NONE) {
3431
	if (ctrl->nsf != PRI_NSF_NONE) {
3408
		ie->data[0] = 0x00;
3432
		ie->data[0] = 0x00;
3409
		ie->data[1] = ctrl->nsf;
3433
		ie->data[1] = ctrl->nsf;
3410
		return 4;
3434
		return 4;
3411
	}
3435
	}
3412
	/* Leave off */
3436
	/* Leave off */
3413
	return 0;
3437
	return 0;
3414
}
3438
}
3415

    
   
3439

   
3416
char *pri_cause2str(int cause)
3440
char *pri_cause2str(int cause)
3417
{
3441
{
3418
	return code2str(cause, causes, sizeof(causes) / sizeof(causes[0]));
3442
	return code2str(cause, causes, sizeof(causes) / sizeof(causes[0]));
3419
}
3443
}
3420

    
   
3444

   
3421
static char *pri_causeclass2str(int cause)
3445
static char *pri_causeclass2str(int cause)
3422
{
3446
{
3423
	static struct msgtype causeclasses[] = {
3447
	static struct msgtype causeclasses[] = {
3424
		{ 0, "Normal Event" },
3448
		{ 0, "Normal Event" },
3425
		{ 1, "Normal Event" },
3449
		{ 1, "Normal Event" },
3426
		{ 2, "Network Congestion (resource unavailable)" },
3450
		{ 2, "Network Congestion (resource unavailable)" },
3427
		{ 3, "Service or Option not Available" },
3451
		{ 3, "Service or Option not Available" },
3428
		{ 4, "Service or Option not Implemented" },
3452
		{ 4, "Service or Option not Implemented" },
3429
		{ 5, "Invalid message (e.g. parameter out of range)" },
3453
		{ 5, "Invalid message (e.g. parameter out of range)" },
3430
		{ 6, "Protocol Error (e.g. unknown message)" },
3454
		{ 6, "Protocol Error (e.g. unknown message)" },
3431
		{ 7, "Interworking" },
3455
		{ 7, "Interworking" },
3432
	};
3456
	};
3433
	return code2str(cause, causeclasses, sizeof(causeclasses) / sizeof(causeclasses[0]));
3457
	return code2str(cause, causeclasses, sizeof(causeclasses) / sizeof(causeclasses[0]));
3434
}
3458
}
3435

    
   
3459

   
3436
static void dump_cause(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3460
static void dump_cause(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3437
{
3461
{
3438
	int x;
3462
	int x;
3439
	pri_message(ctrl, "%c Cause (len=%2d) [ Ext: %d  Coding: %s (%d)  Spare: %d  Location: %s (%d)\n",
3463
	pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d  Coding: %s (%d)  Spare: %d  Location: %s (%d)\n",
3440
		prefix, len, ie->data[0] >> 7, coding2str((ie->data[0] & 0x60) >> 5), (ie->data[0] & 0x60) >> 5,
3464
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
3465
		coding2str((ie->data[0] & 0x60) >> 5), (ie->data[0] & 0x60) >> 5,
3441
		(ie->data[0] & 0x10) >> 4, loc2str(ie->data[0] & 0xf), ie->data[0] & 0xf);
3466
		(ie->data[0] & 0x10) >> 4, loc2str(ie->data[0] & 0xf), ie->data[0] & 0xf);
3442
	pri_message(ctrl, "%c                  Ext: %d  Cause: %s (%d), class = %s (%d) ]\n",
3467
	pri_message(ctrl, "%c                  Ext: %d  Cause: %s (%d), class = %s (%d) ]\n",
3443
		prefix, (ie->data[1] >> 7), pri_cause2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, 
3468
		prefix, (ie->data[1] >> 7), pri_cause2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, 
3444
			pri_causeclass2str((ie->data[1] & 0x7f) >> 4), (ie->data[1] & 0x7f) >> 4);
3469
			pri_causeclass2str((ie->data[1] & 0x7f) >> 4), (ie->data[1] & 0x7f) >> 4);
3445
	if (ie->len < 3)
3470
	if (ie->len < 3)
3446
		return;
3471
		return;
3447
	/* Dump cause data in readable form */
3472
	/* Dump cause data in readable form */
3448
	switch(ie->data[1] & 0x7f) {
3473
	switch(ie->data[1] & 0x7f) {
3449
	case PRI_CAUSE_IE_NONEXIST:
3474
	case PRI_CAUSE_IE_NONEXIST:
3450
		for (x=2;x<ie->len;x++) 
3475
		for (x=2;x<ie->len;x++) 
3451
			pri_message(ctrl, "%c              Cause data %d: %02x (%d, %s IE)\n", prefix, x-1, ie->data[x], ie->data[x], ie2str(ie->data[x]));
3476
			pri_message(ctrl, "%c              Cause data %d: %02x (%d, %s IE)\n", prefix, x-1, ie->data[x], ie->data[x], ie2str(ie->data[x]));
3452
		break;
3477
		break;
3453
	case PRI_CAUSE_WRONG_CALL_STATE:
3478
	case PRI_CAUSE_WRONG_CALL_STATE:
3454
		for (x=2;x<ie->len;x++) 
3479
		for (x=2;x<ie->len;x++) 
3455
			pri_message(ctrl, "%c              Cause data %d: %02x (%d, %s message)\n", prefix, x-1, ie->data[x], ie->data[x], msg2str(ie->data[x]));
3480
			pri_message(ctrl, "%c              Cause data %d: %02x (%d, %s message)\n", prefix, x-1, ie->data[x], ie->data[x], msg2str(ie->data[x]));
3456
		break;
3481
		break;
3457
	case PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE:
3482
	case PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE:
3458
		pri_message(ctrl, "%c              Cause data:", prefix);
3483
		pri_message(ctrl, "%c              Cause data:", prefix);
3459
		for (x=2;x<ie->len;x++)
3484
		for (x=2;x<ie->len;x++)
3460
			pri_message(ctrl, " %02x", ie->data[x]);
3485
			pri_message(ctrl, " %02x", ie->data[x]);
3461
		pri_message(ctrl, " (Timer T");
3486
		pri_message(ctrl, " (Timer T");
3462
		for (x=2;x<ie->len;x++)
3487
		for (x=2;x<ie->len;x++)
3463
			pri_message(ctrl, "%c", ((ie->data[x] >= ' ') && (ie->data[x] < 0x7f)) ? ie->data[x] : '.');
3488
			pri_message(ctrl, "%c", ((ie->data[x] >= ' ') && (ie->data[x] < 0x7f)) ? ie->data[x] : '.');
3464
		pri_message(ctrl, ")\n");
3489
		pri_message(ctrl, ")\n");
3465
		break;
3490
		break;
3466
	default:
3491
	default:
3467
		for (x=2;x<ie->len;x++) 
3492
		for (x=2;x<ie->len;x++) 
3468
			pri_message(ctrl, "%c              Cause data %d: %02x (%d)\n", prefix, x-1, ie->data[x], ie->data[x]);
3493
			pri_message(ctrl, "%c              Cause data %d: %02x (%d)\n", prefix, x-1, ie->data[x], ie->data[x]);
3469
		break;
3494
		break;
3470
	}
3495
	}
3471
}
3496
}
3472

    
   
3497

   
3473
static int receive_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3498
static int receive_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3474
{
3499
{
3475
	call->causeloc = ie->data[0] & 0xf;
3500
	call->causeloc = ie->data[0] & 0xf;
3476
	call->causecode = (ie->data[0] & 0x60) >> 5;
3501
	call->causecode = (ie->data[0] & 0x60) >> 5;
3477
	call->cause = (ie->data[1] & 0x7f);
3502
	call->cause = (ie->data[1] & 0x7f);
3478
	return 0;
3503
	return 0;
3479
}
3504
}
3480

    
   
3505

   
3481
static int transmit_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3506
static int transmit_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3482
{
3507
{
3483
	/* We are ready to transmit single IE only */
3508
	/* We are ready to transmit single IE only */
3484
	if (order > 1)
3509
	if (order > 1)
3485
		return 0;
3510
		return 0;
3486

    
   
3511

   
3487
	if (call->cause > 0) {
3512
	if (call->cause > 0) {
3488
		ie->data[0] = 0x80 | (call->causecode << 5)  | (call->causeloc);
3513
		ie->data[0] = 0x80 | (call->causecode << 5)  | (call->causeloc);
3489
		ie->data[1] = 0x80 | (call->cause);
3514
		ie->data[1] = 0x80 | (call->cause);
3490
		return 4;
3515
		return 4;
3491
	} else {
3516
	} else {
3492
		/* Leave off */
3517
		/* Leave off */
3493
		return 0;
3518
		return 0;
3494
	}
3519
	}
3495
}
3520
}
3496

    
   
3521

   
3497
static void dump_sending_complete(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3522
static void dump_sending_complete(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3498
{
3523
{
3499
	pri_message(ctrl, "%c Sending Complete (len=%2d)\n", prefix, len);
3524
	pri_message(ctrl, "%c %s (len=%2d)\n", prefix, ie2str(full_ie), len);
3500
}
3525
}
3501

    
   
3526

   
3502
static int receive_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3527
static int receive_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3503
{
3528
{
3504
	/* We've got a "Complete" message: Exect no further digits. */
3529
	/* We've got a "Complete" message: Exect no further digits. */
3505
	call->complete = 1; 
3530
	call->complete = 1; 
3506
	return 0;
3531
	return 0;
3507
}
3532
}
3508

    
   
3533

   
3509
static int transmit_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3534
static int transmit_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3510
{
3535
{
3511
	if ((ctrl->overlapdial && call->complete) || /* Explicit */
3536
	if ((ctrl->overlapdial && call->complete) || /* Explicit */
3512
		(!ctrl->overlapdial && ((ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) || 
3537
		(!ctrl->overlapdial && ((ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) || 
3513
		/* Implicit */   	   (ctrl->switchtype == PRI_SWITCH_EUROISDN_T1)))) {
3538
		/* Implicit */   	   (ctrl->switchtype == PRI_SWITCH_EUROISDN_T1)))) {
3514
		/* Include this single-byte IE */
3539
		/* Include this single-byte IE */
3515
		return 1;
3540
		return 1;
3516
	}
3541
	}
3517
	return 0;
3542
	return 0;
3518
}
3543
}
3519

    
   
3544

   
3520
static char *notify2str(int info)
3545
static char *notify2str(int info)
3521
{
3546
{
3522
	/* ITU-T Q.763 */
3547
	/* ITU-T Q.763 */
3523
	static struct msgtype notifies[] = {
3548
	static struct msgtype notifies[] = {
3524
		{ PRI_NOTIFY_USER_SUSPENDED, "User suspended" },
3549
		{ PRI_NOTIFY_USER_SUSPENDED, "User suspended" },
3525
		{ PRI_NOTIFY_USER_RESUMED, "User resumed" },
3550
		{ PRI_NOTIFY_USER_RESUMED, "User resumed" },
3526
		{ PRI_NOTIFY_BEARER_CHANGE, "Bearer service change (DSS1)" },
3551
		{ PRI_NOTIFY_BEARER_CHANGE, "Bearer service change (DSS1)" },
3527
		{ PRI_NOTIFY_ASN1_COMPONENT, "ASN.1 encoded component (DSS1)" },
3552
		{ PRI_NOTIFY_ASN1_COMPONENT, "ASN.1 encoded component (DSS1)" },
3528
		{ PRI_NOTIFY_COMPLETION_DELAY, "Call completion delay" },
3553
		{ PRI_NOTIFY_COMPLETION_DELAY, "Call completion delay" },
3529
		{ PRI_NOTIFY_CONF_ESTABLISHED, "Conference established" },
3554
		{ PRI_NOTIFY_CONF_ESTABLISHED, "Conference established" },
3530
		{ PRI_NOTIFY_CONF_DISCONNECTED, "Conference disconnected" },
3555
		{ PRI_NOTIFY_CONF_DISCONNECTED, "Conference disconnected" },
3531
		{ PRI_NOTIFY_CONF_PARTY_ADDED, "Other party added" },
3556
		{ PRI_NOTIFY_CONF_PARTY_ADDED, "Other party added" },
3532
		{ PRI_NOTIFY_CONF_ISOLATED, "Isolated" },
3557
		{ PRI_NOTIFY_CONF_ISOLATED, "Isolated" },
3533
		{ PRI_NOTIFY_CONF_REATTACHED, "Reattached" },
3558
		{ PRI_NOTIFY_CONF_REATTACHED, "Reattached" },
3534
		{ PRI_NOTIFY_CONF_OTHER_ISOLATED, "Other party isolated" },
3559
		{ PRI_NOTIFY_CONF_OTHER_ISOLATED, "Other party isolated" },
3535
		{ PRI_NOTIFY_CONF_OTHER_REATTACHED, "Other party reattached" },
3560
		{ PRI_NOTIFY_CONF_OTHER_REATTACHED, "Other party reattached" },
3536
		{ PRI_NOTIFY_CONF_OTHER_SPLIT, "Other party split" },
3561
		{ PRI_NOTIFY_CONF_OTHER_SPLIT, "Other party split" },
3537
		{ PRI_NOTIFY_CONF_OTHER_DISCONNECTED, "Other party disconnected" },
3562
		{ PRI_NOTIFY_CONF_OTHER_DISCONNECTED, "Other party disconnected" },
3538
		{ PRI_NOTIFY_CONF_FLOATING, "Conference floating" },
3563
		{ PRI_NOTIFY_CONF_FLOATING, "Conference floating" },
3539
		{ PRI_NOTIFY_WAITING_CALL, "Call is waiting call" },
3564
		{ PRI_NOTIFY_WAITING_CALL, "Call is waiting call" },
3540
		{ PRI_NOTIFY_DIVERSION_ACTIVATED, "Diversion activated (DSS1)" },
3565
		{ PRI_NOTIFY_DIVERSION_ACTIVATED, "Diversion activated (DSS1)" },
3541
		{ PRI_NOTIFY_TRANSFER_ALERTING, "Call transfer, alerting" },
3566
		{ PRI_NOTIFY_TRANSFER_ALERTING, "Call transfer, alerting" },
3542
		{ PRI_NOTIFY_TRANSFER_ACTIVE, "Call transfer, active" },
3567
		{ PRI_NOTIFY_TRANSFER_ACTIVE, "Call transfer, active" },
3543
		{ PRI_NOTIFY_REMOTE_HOLD, "Remote hold" },
3568
		{ PRI_NOTIFY_REMOTE_HOLD, "Remote hold" },
3544
		{ PRI_NOTIFY_REMOTE_RETRIEVAL, "Remote retrieval" },
3569
		{ PRI_NOTIFY_REMOTE_RETRIEVAL, "Remote retrieval" },
3545
		{ PRI_NOTIFY_CALL_DIVERTING, "Call is diverting" },
3570
		{ PRI_NOTIFY_CALL_DIVERTING, "Call is diverting" },
3546
	};
3571
	};
3547
	return code2str(info, notifies, sizeof(notifies) / sizeof(notifies[0]));
3572
	return code2str(info, notifies, sizeof(notifies) / sizeof(notifies[0]));
3548
}
3573
}
3549

    
   
3574

   
3550
static void dump_notify(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3575
static void dump_notify(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3551
{
3576
{
3552
	pri_message(ctrl, "%c Notification indicator (len=%2d): Ext: %d  %s (%d)\n", prefix, len, ie->data[0] >> 7, notify2str(ie->data[0] & 0x7f), ie->data[0] & 0x7f);
3577
	pri_message(ctrl, "%c %s (len=%2d): Ext: %d  %s (%d)\n",

    
   
3578
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
3579
		notify2str(ie->data[0] & 0x7f), ie->data[0] & 0x7f);
3553
}
3580
}
3554

    
   
3581

   
3555
static int receive_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3582
static int receive_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3556
{
3583
{
3557
	call->notify = ie->data[0] & 0x7F;
3584
	call->notify = ie->data[0] & 0x7F;
3558
	return 0;
3585
	return 0;
3559
}
3586
}
3560

    
   
3587

   
3561
static int transmit_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3588
static int transmit_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3562
{
3589
{
3563
	if (call->notify >= 0) {
3590
	if (call->notify >= 0) {
3564
		ie->data[0] = 0x80 | call->notify;
3591
		ie->data[0] = 0x80 | call->notify;
3565
		return 3;
3592
		return 3;
3566
	}
3593
	}
3567
	return 0;
3594
	return 0;
3568
}
3595
}
3569

    
   
3596

   
3570
static void dump_shift(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3597
static void dump_shift(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3571
{
3598
{
3572
	pri_message(ctrl, "%c %sLocking Shift (len=%02d): Requested codeset %d\n", prefix, (full_ie & 8) ? "Non-" : "", len, full_ie & 7);
3599
	pri_message(ctrl, "%c %s%s (len=%02d): Requested codeset %d\n",

    
   
3600
		prefix, (full_ie & 8) ? "Non-" : "", ie2str(full_ie), len, full_ie & 7);
3573
}
3601
}
3574

    
   
3602

   
3575
static char *lineinfo2str(int info)
3603
static char *lineinfo2str(int info)
3576
{
3604
{
3577
	/* NAPNA ANI II digits */
3605
	/* NAPNA ANI II digits */
3578
	static struct msgtype lineinfo[] = {
3606
	static struct msgtype lineinfo[] = {
3579
		{  0, "Plain Old Telephone Service (POTS)" },
3607
		{  0, "Plain Old Telephone Service (POTS)" },
3580
		{  1, "Multiparty line (more than 2)" },
3608
		{  1, "Multiparty line (more than 2)" },
3581
		{  2, "ANI failure" },
3609
		{  2, "ANI failure" },
3582
		{  6, "Station Level Rating" },
3610
		{  6, "Station Level Rating" },
3583
		{  7, "Special Operator Handling Required" },
3611
		{  7, "Special Operator Handling Required" },
3584
		{ 20, "Automatic Identified Outward Dialing (AIOD)" },
3612
		{ 20, "Automatic Identified Outward Dialing (AIOD)" },
3585
		{ 23, "Coing or Non-Coin" },
3613
		{ 23, "Coing or Non-Coin" },
3586
		{ 24, "Toll free translated to POTS originated for non-pay station" },
3614
		{ 24, "Toll free translated to POTS originated for non-pay station" },
3587
		{ 25, "Toll free translated to POTS originated from pay station" },
3615
		{ 25, "Toll free translated to POTS originated from pay station" },
3588
		{ 27, "Pay station with coin control signalling" },
3616
		{ 27, "Pay station with coin control signalling" },
3589
		{ 29, "Prison/Inmate Service" },
3617
		{ 29, "Prison/Inmate Service" },
3590
		{ 30, "Intercept (blank)" },
3618
		{ 30, "Intercept (blank)" },
3591
		{ 31, "Intercept (trouble)" },
3619
		{ 31, "Intercept (trouble)" },
3592
		{ 32, "Intercept (regular)" },
3620
		{ 32, "Intercept (regular)" },
3593
		{ 34, "Telco Operator Handled Call" },
3621
		{ 34, "Telco Operator Handled Call" },
3594
		{ 52, "Outward Wide Area Telecommunications Service (OUTWATS)" },
3622
		{ 52, "Outward Wide Area Telecommunications Service (OUTWATS)" },
3595
		{ 60, "TRS call from unrestricted line" },
3623
		{ 60, "TRS call from unrestricted line" },
3596
		{ 61, "Cellular/Wireless PCS (Type 1)" },
3624
		{ 61, "Cellular/Wireless PCS (Type 1)" },
3597
		{ 62, "Cellular/Wireless PCS (Type 2)" },
3625
		{ 62, "Cellular/Wireless PCS (Type 2)" },
3598
		{ 63, "Cellular/Wireless PCS (Roaming)" },
3626
		{ 63, "Cellular/Wireless PCS (Roaming)" },
3599
		{ 66, "TRS call from hotel/motel" },
3627
		{ 66, "TRS call from hotel/motel" },
3600
		{ 67, "TRS call from restricted line" },
3628
		{ 67, "TRS call from restricted line" },
3601
		{ 70, "Line connected to pay station" },
3629
		{ 70, "Line connected to pay station" },
3602
		{ 93, "Private virtual network call" },
3630
		{ 93, "Private virtual network call" },
3603
	};
3631
	};
3604
	return code2str(info, lineinfo, sizeof(lineinfo) / sizeof(lineinfo[0]));
3632
	return code2str(info, lineinfo, sizeof(lineinfo) / sizeof(lineinfo[0]));
3605
}
3633
}
3606

    
   
3634

   
3607
static void dump_line_information(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3635
static void dump_line_information(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3608
{
3636
{
3609
	pri_message(ctrl, "%c Originating Line Information (len=%02d): %s (%d)\n", prefix, len, lineinfo2str(ie->data[0]), ie->data[0]);
3637
	pri_message(ctrl, "%c %s (len=%02d): %s (%d)\n",

    
   
3638
		prefix, ie2str(full_ie), len, lineinfo2str(ie->data[0]), ie->data[0]);
3610
}
3639
}
3611

    
   
3640

   
3612
static int receive_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3641
static int receive_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3613
{
3642
{
3614
	call->ani2 = ie->data[0];
3643
	call->ani2 = ie->data[0];
3615
	return 0;
3644
	return 0;
3616
}
3645
}
3617

    
   
3646

   
3618
static int transmit_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3647
static int transmit_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3619
{
3648
{
3620
#if 0	/* XXX Is this IE possible for 4ESS only? XXX */
3649
#if 0	/* XXX Is this IE possible for 4ESS only? XXX */
3621
	if(ctrl->switchtype == PRI_SWITCH_ATT4ESS) {
3650
	if(ctrl->switchtype == PRI_SWITCH_ATT4ESS) {
3622
		ie->data[0] = 0;
3651
		ie->data[0] = 0;
3623
		return 3;
3652
		return 3;
3624
	}
3653
	}
3625
#endif
3654
#endif
3626
	return 0;
3655
	return 0;
3627
}
3656
}
3628

    
   
3657

   
3629

    
   
3658

   
3630
static char *gdencoding2str(int encoding)
3659
static char *gdencoding2str(int encoding)
3631
{
3660
{
3632
	static struct msgtype gdencoding[] = {
3661
	static struct msgtype gdencoding[] = {
3633
		{ 0, "BCD even" },
3662
		{ 0, "BCD even" },
3634
		{ 1, "BCD odd" },
3663
		{ 1, "BCD odd" },
3635
		{ 2, "IA5" },
3664
		{ 2, "IA5" },
3636
		{ 3, "Binary" },
3665
		{ 3, "Binary" },
3637
	};
3666
	};
3638
	return code2str(encoding, gdencoding, sizeof(gdencoding) / sizeof(gdencoding[0]));
3667
	return code2str(encoding, gdencoding, sizeof(gdencoding) / sizeof(gdencoding[0]));
3639
}
3668
}
3640

    
   
3669

   
3641
static char *gdtype2str(int type)
3670
static char *gdtype2str(int type)
3642
{
3671
{
3643
	static struct msgtype gdtype[] = {
3672
	static struct msgtype gdtype[] = {
3644
		{  0, "Account Code" },
3673
		{  0, "Account Code" },
3645
		{  1, "Auth Code" },
3674
		{  1, "Auth Code" },
3646
		{  2, "Customer ID" },
3675
		{  2, "Customer ID" },
3647
		{  3, "Universal Access" },
3676
		{  3, "Universal Access" },
3648
		{  4, "Info Digits" },
3677
		{  4, "Info Digits" },
3649
		{  5, "Callid" },
3678
		{  5, "Callid" },
3650
		{  6, "Opart" },
3679
		{  6, "Opart" },
3651
		{  7, "TCN" },
3680
		{  7, "TCN" },
3652
		{  9, "Adin" },
3681
		{  9, "Adin" },
3653
	};
3682
	};
3654
	return code2str(type, gdtype, sizeof(gdtype) / sizeof(gdtype[0]));
3683
	return code2str(type, gdtype, sizeof(gdtype) / sizeof(gdtype[0]));
3655
}
3684
}
3656

    
   
3685

   
3657
static void dump_generic_digits(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3686
static void dump_generic_digits(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3658
{
3687
{
3659
	int encoding;
3688
	int encoding;
3660
	int type;
3689
	int type;
3661
	int idx;
3690
	int idx;
3662
	int value;
3691
	int value;
3663
	if (len < 3) {
3692
	if (len < 3) {
3664
		pri_message(ctrl, "%c Generic Digits (len=%02d): Invalid length\n", prefix, len);
3693
		pri_message(ctrl, "%c %s (len=%02d): Invalid length\n",

    
   
3694
			prefix, ie2str(full_ie), len);
3665
		return;
3695
		return;
3666
	}
3696
	}
3667
	encoding = (ie->data[0] >> 5) & 7;
3697
	encoding = (ie->data[0] >> 5) & 7;
3668
	type = ie->data[0] & 0x1F;
3698
	type = ie->data[0] & 0x1F;
3669
	pri_message(ctrl, "%c Generic Digits (len=%02d): Encoding %s  Type %s\n", prefix, len, gdencoding2str(encoding), gdtype2str(type));
3699
	pri_message(ctrl, "%c %s (len=%02d): Encoding %s  Type %s\n",

    
   
3700
		prefix, ie2str(full_ie), len, gdencoding2str(encoding), gdtype2str(type));
3670
	if (encoding == 3) {	/* Binary */
3701
	if (encoding == 3) {	/* Binary */
3671
		pri_message(ctrl, "%c                            Don't know how to handle binary encoding\n",
3702
		pri_message(ctrl, "%c                            Don't know how to handle binary encoding\n",
3672
			prefix);
3703
			prefix);
3673
		return;
3704
		return;
3674
	}
3705
	}
3675
	if (len == 3)	/* No number information */
3706
	if (len == 3)	/* No number information */
3676
		return;
3707
		return;
3677
	pri_message(ctrl, "%c                            Digits: ", prefix);
3708
	pri_message(ctrl, "%c                            Digits: ", prefix);
3678
	value = 0;
3709
	value = 0;
3679
	for(idx = 3; idx < len; ++idx) {
3710
	for(idx = 3; idx < len; ++idx) {
3680
		switch(encoding) {
3711
		switch(encoding) {
3681
		case 0:		/* BCD even */
3712
		case 0:		/* BCD even */
3682
		case 1:		/* BCD odd */
3713
		case 1:		/* BCD odd */
3683
			pri_message(ctrl, "%d", ie->data[idx-2] & 0x0f);
3714
			pri_message(ctrl, "%d", ie->data[idx-2] & 0x0f);
3684
			value = value * 10 + (ie->data[idx-2] & 0x0f);
3715
			value = value * 10 + (ie->data[idx-2] & 0x0f);
3685
			if(!encoding || (idx+1 < len)) {	/* Special handling for BCD odd */
3716
			if(!encoding || (idx+1 < len)) {	/* Special handling for BCD odd */
3686
				pri_message(ctrl, "%d", (ie->data[idx-2] >> 4) & 0x0f);
3717
				pri_message(ctrl, "%d", (ie->data[idx-2] >> 4) & 0x0f);
3687
				value = value * 10 + ((ie->data[idx-2] >> 4) & 0x0f);
3718
				value = value * 10 + ((ie->data[idx-2] >> 4) & 0x0f);
3688
			}
3719
			}
3689
			break;
3720
			break;
3690
		case 2:		/* IA5 */
3721
		case 2:		/* IA5 */
3691
			pri_message(ctrl, "%c", ie->data[idx-2]);
3722
			pri_message(ctrl, "%c", ie->data[idx-2]);
3692
			value = value * 10 + ie->data[idx-2] - '0';
3723
			value = value * 10 + ie->data[idx-2] - '0';
3693
			break;
3724
			break;
3694
		}
3725
		}
3695
	}
3726
	}
3696
	switch(type) {
3727
	switch(type) {
3697
		case 4:		/* Info Digits */
3728
		case 4:		/* Info Digits */
3698
			pri_message(ctrl, " - %s", lineinfo2str(value));
3729
			pri_message(ctrl, " - %s", lineinfo2str(value));
3699
			break;
3730
			break;
3700
	}
3731
	}
3701
	pri_message(ctrl, "\n");
3732
	pri_message(ctrl, "\n");
3702
}
3733
}
3703

    
   
3734

   
3704
static int receive_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3735
static int receive_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3705
{
3736
{
3706
	int encoding;
3737
	int encoding;
3707
	int type;
3738
	int type;
3708
	int idx;
3739
	int idx;
3709
	int value;
3740
	int value;
3710
	int num_idx;
3741
	int num_idx;
3711
	char number[260];
3742
	char number[260];
3712

    
   
3743

   
3713
	if (len < 3) {
3744
	if (len < 3) {
3714
		pri_error(ctrl, "Invalid length of Generic Digits IE\n");
3745
		pri_error(ctrl, "Invalid length of Generic Digits IE\n");
3715
		return -1;
3746
		return -1;
3716
	}
3747
	}
3717
	encoding = (ie->data[0] >> 5) & 7;
3748
	encoding = (ie->data[0] >> 5) & 7;
3718
	type = ie->data[0] & 0x1F;
3749
	type = ie->data[0] & 0x1F;
3719
	if (encoding == 3) {	/* Binary */
3750
	if (encoding == 3) {	/* Binary */
3720
		pri_message(ctrl, "!! Unable to handle binary encoded Generic Digits IE\n");
3751
		pri_message(ctrl, "!! Unable to handle binary encoded Generic Digits IE\n");
3721
		return 0;
3752
		return 0;
3722
	}
3753
	}
3723
	if (len == 3)	/* No number information */
3754
	if (len == 3)	/* No number information */
3724
		return 0;
3755
		return 0;
3725
	value = 0;
3756
	value = 0;
3726
	switch(type) {
3757
	switch(type) {
3727
	/* Integer value handling */
3758
	/* Integer value handling */
3728
	case 4:		/* Info Digits */
3759
	case 4:		/* Info Digits */
3729
		for(idx = 3; idx < len; ++idx) {
3760
		for(idx = 3; idx < len; ++idx) {
3730
			switch(encoding) {
3761
			switch(encoding) {
3731
			case 0:		/* BCD even */
3762
			case 0:		/* BCD even */
3732
			case 1:		/* BCD odd */
3763
			case 1:		/* BCD odd */
3733
				value = value * 10 + (ie->data[idx-2] & 0x0f);
3764
				value = value * 10 + (ie->data[idx-2] & 0x0f);
3734
				if(!encoding || (idx+1 < len))	/* Special handling for BCD odd */
3765
				if(!encoding || (idx+1 < len))	/* Special handling for BCD odd */
3735
					value = value * 10 + ((ie->data[idx-2] >> 4) & 0x0f);
3766
					value = value * 10 + ((ie->data[idx-2] >> 4) & 0x0f);
3736
				break;
3767
				break;
3737
			case 2:		/* IA5 */
3768
			case 2:		/* IA5 */
3738
				value = value * 10 + (ie->data[idx-2] - '0');
3769
				value = value * 10 + (ie->data[idx-2] - '0');
3739
				break;
3770
				break;
3740
			}
3771
			}
3741
		}
3772
		}
3742
		break;
3773
		break;
3743
	/* String value handling */
3774
	/* String value handling */
3744
	case 5:		/* Callid */
3775
	case 5:		/* Callid */
3745
		num_idx = 0;
3776
		num_idx = 0;
3746
		for(idx = 3; (idx < len) && (num_idx < sizeof(number) - 4); ++idx) {
3777
		for(idx = 3; (idx < len) && (num_idx < sizeof(number) - 4); ++idx) {
3747
			switch(encoding) {
3778
			switch(encoding) {
3748
			case 0:		/* BCD even */
3779
			case 0:		/* BCD even */
3749
			case 1:		/* BCD odd */
3780
			case 1:		/* BCD odd */
3750
				number[num_idx++] = '0' + (ie->data[idx-2] & 0x0f);
3781
				number[num_idx++] = '0' + (ie->data[idx-2] & 0x0f);
3751
				if(!encoding || (idx+1 < len))	/* Special handling for BCD odd */
3782
				if(!encoding || (idx+1 < len))	/* Special handling for BCD odd */
3752
					number[num_idx++] = '0' + ((ie->data[idx-2] >> 4) & 0x0f);
3783
					number[num_idx++] = '0' + ((ie->data[idx-2] >> 4) & 0x0f);
3753
				break;
3784
				break;
3754
			case 2:
3785
			case 2:
3755
				number[num_idx++] = ie->data[idx-2];
3786
				number[num_idx++] = ie->data[idx-2];
3756
				break;
3787
				break;
3757
			}
3788
			}
3758
		}
3789
		}
3759
		number[num_idx] = '\0';
3790
		number[num_idx] = '\0';
3760
		break;
3791
		break;
3761
	}
3792
	}
3762
	switch(type) {
3793
	switch(type) {
3763
	case 4:		/* Info Digits */
3794
	case 4:		/* Info Digits */
3764
		call->ani2 = value;
3795
		call->ani2 = value;
3765
		break;
3796
		break;
3766
#if 0
3797
#if 0
3767
	case 5:		/* Callid */
3798
	case 5:		/* Callid */
3768
		if (!call->remote_id.number.valid) {
3799
		if (!call->remote_id.number.valid) {
3769
			call->remote_id.number.valid = 1;
3800
			call->remote_id.number.valid = 1;
3770
			call->remote_id.number.presentation =
3801
			call->remote_id.number.presentation =
3771
				PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
3802
				PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
3772
			call->remote_id.number.plan = PRI_UNKNOWN;
3803
			call->remote_id.number.plan = PRI_UNKNOWN;
3773
			libpri_copy_string(call->remote_id.number.str, number,
3804
			libpri_copy_string(call->remote_id.number.str, number,
3774
				sizeof(call->remote_id.number.str));
3805
				sizeof(call->remote_id.number.str));
3775
		}
3806
		}
3776
		break;
3807
		break;
3777
#endif
3808
#endif
3778
	}
3809
	}
3779
	return 0;
3810
	return 0;
3780
}
3811
}
3781

    
   
3812

   
3782
static int transmit_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3813
static int transmit_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3783
{
3814
{
3784
#if 0	/* XXX Is this IE possible for other switches? XXX */
3815
#if 0	/* XXX Is this IE possible for other switches? XXX */
3785
	if (order > 1)
3816
	if (order > 1)
3786
		return 0;
3817
		return 0;
3787

    
   
3818

   
3788
	if(ctrl->switchtype == PRI_SWITCH_NI1) {
3819
	if(ctrl->switchtype == PRI_SWITCH_NI1) {
3789
		ie->data[0] = 0x04;	/* BCD even, Info Digits */
3820
		ie->data[0] = 0x04;	/* BCD even, Info Digits */
3790
		ie->data[1] = 0x00;	/* POTS */
3821
		ie->data[1] = 0x00;	/* POTS */
3791
		return 4;
3822
		return 4;
3792
	}
3823
	}
3793
#endif
3824
#endif
3794
	return 0;
3825
	return 0;
3795
}
3826
}
3796

    
   
3827

   
3797

    
   
3828

   
3798
static char *signal2str(int signal)
3829
static char *signal2str(int signal)
3799
{
3830
{
3800
	/* From Q.931 4.5.8 Table 4-24 */
3831
	/* From Q.931 4.5.8 Table 4-24 */
3801
	static struct msgtype mtsignal[] = {
3832
	static struct msgtype mtsignal[] = {
3802
		{  0, "Dial tone" },
3833
		{  0, "Dial tone" },
3803
		{  1, "Ring back tone" },
3834
		{  1, "Ring back tone" },
3804
		{  2, "Intercept tone" },
3835
		{  2, "Intercept tone" },
3805
		{  3, "Network congestion tone" },
3836
		{  3, "Network congestion tone" },
3806
		{  4, "Busy tone" },
3837
		{  4, "Busy tone" },
3807
		{  5, "Confirm tone" },
3838
		{  5, "Confirm tone" },
3808
		{  6, "Answer tone" },
3839
		{  6, "Answer tone" },
3809
		{  7, "Call waiting tone" },
3840
		{  7, "Call waiting tone" },
3810
		{  8, "Off-hook warning tone" },
3841
		{  8, "Off-hook warning tone" },
3811
		{  9, "Pre-emption tone" },
3842
		{  9, "Pre-emption tone" },
3812
		{ 63, "Tones off" },
3843
		{ 63, "Tones off" },
3813
		{ 64, "Alerting on - pattern 0" },
3844
		{ 64, "Alerting on - pattern 0" },
3814
		{ 65, "Alerting on - pattern 1" },
3845
		{ 65, "Alerting on - pattern 1" },
3815
		{ 66, "Alerting on - pattern 2" },
3846
		{ 66, "Alerting on - pattern 2" },
3816
		{ 67, "Alerting on - pattern 3" },
3847
		{ 67, "Alerting on - pattern 3" },
3817
		{ 68, "Alerting on - pattern 4" },
3848
		{ 68, "Alerting on - pattern 4" },
3818
		{ 69, "Alerting on - pattern 5" },
3849
		{ 69, "Alerting on - pattern 5" },
3819
		{ 70, "Alerting on - pattern 6" },
3850
		{ 70, "Alerting on - pattern 6" },
3820
		{ 71, "Alerting on - pattern 7" },
3851
		{ 71, "Alerting on - pattern 7" },
3821
		{ 79, "Alerting off" },
3852
		{ 79, "Alerting off" },
3822
	};
3853
	};
3823
	return code2str(signal, mtsignal, sizeof(mtsignal) / sizeof(mtsignal[0]));
3854
	return code2str(signal, mtsignal, sizeof(mtsignal) / sizeof(mtsignal[0]));
3824
}
3855
}
3825

    
   
3856

   
3826

    
   
3857

   
3827
static void dump_signal(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3858
static void dump_signal(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3828
{
3859
{
3829
	pri_message(ctrl, "%c Signal (len=%02d): ", prefix, len);
3860
	pri_message(ctrl, "%c %s (len=%02d): ", prefix, ie2str(full_ie), len);
3830
	if (len < 3) {
3861
	if (len < 3) {
3831
		pri_message(ctrl, "Invalid length\n");
3862
		pri_message(ctrl, "Invalid length\n");
3832
		return;
3863
		return;
3833
	}
3864
	}
3834
	pri_message(ctrl, "Signal %s (%d)\n", signal2str(ie->data[0]), ie->data[0]);
3865
	pri_message(ctrl, "Signal %s (%d)\n", signal2str(ie->data[0]), ie->data[0]);
3835
}
3866
}
3836

    
   
3867

   
3837
static void dump_transit_count(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3868
static void dump_transit_count(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3838
{
3869
{
3839
	/* Defined in ECMA-225 */
3870
	/* Defined in ECMA-225 */
3840
	pri_message(ctrl, "%c Transit Count (len=%02d): ", prefix, len);
3871
	pri_message(ctrl, "%c %s (len=%02d): ", prefix, ie2str(full_ie), len);
3841
	if (len < 3) {
3872
	if (len < 3) {
3842
		pri_message(ctrl, "Invalid length\n");
3873
		pri_message(ctrl, "Invalid length\n");
3843
		return;
3874
		return;
3844
	}
3875
	}
3845
	pri_message(ctrl, "Count=%d (0x%02x)\n", ie->data[0] & 0x1f, ie->data[0] & 0x1f);
3876
	pri_message(ctrl, "Count=%d (0x%02x)\n", ie->data[0] & 0x1f, ie->data[0] & 0x1f);
3846
}
3877
}
3847

    
   
3878

   
3848
static void dump_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3879
static void dump_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3849
{
3880
{
3850
	pri_message(ctrl, "%c Reverse Charging Indication (len=%02d): %d\n", prefix, len, ie->data[0] & 0x7);
3881
	pri_message(ctrl, "%c %s (len=%02d): %d\n", prefix, ie2str(full_ie), len, ie->data[0] & 0x7);
3851
}
3882
}
3852

    
   
3883

   
3853
static int receive_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3884
static int receive_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3854
{
3885
{
3855
	call->reversecharge = ie->data[0] & 0x7;
3886
	call->reversecharge = ie->data[0] & 0x7;
3856
	return 0;
3887
	return 0;
3857
}
3888
}
3858

    
   
3889

   
3859
static int transmit_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3890
static int transmit_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3860
{
3891
{
3861
	if (call->reversecharge != PRI_REVERSECHARGE_NONE) {
3892
	if (call->reversecharge != PRI_REVERSECHARGE_NONE) {
3862
		ie->data[0] = 0x80 | (call->reversecharge & 0x7);
3893
		ie->data[0] = 0x80 | (call->reversecharge & 0x7);
3863
		return 3;
3894
		return 3;
3864
	}
3895
	}
3865
	return 0;
3896
	return 0;
3866
}
3897
}
3867

    
   
3898

   
3868
static struct ie ies[] = {
3899
static struct ie ies[] = {
3869
	/* Codeset 0 - Common */
3900
	/* Codeset 0 - Common */
3870
	{ 1, NATIONAL_CHANGE_STATUS, "Change Status", dump_change_status, receive_change_status, transmit_change_status },
3901
	{ 1, NATIONAL_CHANGE_STATUS, "Change Status Information", dump_change_status, receive_change_status, transmit_change_status },
3871
	{ 0, Q931_LOCKING_SHIFT, "Locking Shift", dump_shift },
3902
	{ 0, Q931_LOCKING_SHIFT, "Locking Shift", dump_shift },
3872
	{ 0, Q931_BEARER_CAPABILITY, "Bearer Capability", dump_bearer_capability, receive_bearer_capability, transmit_bearer_capability },
3903
	{ 0, Q931_BEARER_CAPABILITY, "Bearer Capability", dump_bearer_capability, receive_bearer_capability, transmit_bearer_capability },
3873
	{ 0, Q931_CAUSE, "Cause", dump_cause, receive_cause, transmit_cause },
3904
	{ 0, Q931_CAUSE, "Cause", dump_cause, receive_cause, transmit_cause },
3874
	{ 1, Q931_IE_CALL_STATE, "Call State", dump_call_state, receive_call_state, transmit_call_state },
3905
	{ 1, Q931_IE_CALL_STATE, "Call State", dump_call_state, receive_call_state, transmit_call_state },
3875
	{ 0, Q931_CHANNEL_IDENT, "Channel Identification", dump_channel_id, receive_channel_id, transmit_channel_id },
3906
	{ 0, Q931_CHANNEL_IDENT, "Channel Identification", dump_channel_id, receive_channel_id, transmit_channel_id },
3876
	{ 0, Q931_PROGRESS_INDICATOR, "Progress Indicator", dump_progress_indicator, receive_progress_indicator, transmit_progress_indicator },
3907
	{ 0, Q931_PROGRESS_INDICATOR, "Progress Indicator", dump_progress_indicator, receive_progress_indicator, transmit_progress_indicator },
3877