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 3 (Latest)

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 %s (len=%2d) [ Ext: %d  IntID: %s  %s  Spare: %d  %s  Dchan: %d\n",
1509
		prefix, len,
1509
		prefix, ie2str(full_ie), 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 if 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 if 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 if 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

   

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

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

    
   
2159

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

    
   
2164

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

    
   
2168

   
2160
	if (ie->data[0] & 0x80) {
2169
	if (ie->data[0] & 0x80) {
2161
		q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
2170
		q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
2162
	} else {
2171
	} else {
2163
		q931_strget(cnum, sizeof(cnum), ie->data + 2, len - 4);
2172
		q931_strget(cnum, sizeof(cnum), ie->data + 2, len - 4);
2164
	}
2173
	}
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);
2174
	pri_message(ctrl,
2166
	if (ie->data[0] & 0x80)
2175
		"%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)\n",
2167
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(0), 0, cnum);
2176
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,
2168
	else
2177
		ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
2169
		pri_message(ctrl, "%c                           Presentation: %s (%d)  '%s' ]\n", prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
2178
		npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);

    
   
2179
	if (ie->data[0] & 0x80) {

    
   
2180
		pri_message(ctrl, "%c                                 Presentation: %s (%d)  '%s' ]\n",

    
   
2181
			prefix, pri_pres2str(0), 0, cnum);

    
   
2182
	} else {

    
   
2183
		pri_message(ctrl, "%c                                 Presentation: %s (%d)  '%s' ]\n",

    
   
2184
			prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);

    
   
2185
	}
2170
}
2186
}
2171

    
   
2187

   
2172
static void dump_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2188
static void dump_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2173
{
2189
{
2174
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2190
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2175
}
2191
}
2176

    
   
2192

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

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

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

    
   
2200

   
2183
static void dump_redirecting_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2201
static void dump_redirecting_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2184
{
2202
{
2185
	unsigned char cnum[256];
2203
	unsigned char cnum[256];
2186
	int i = 0;
2204
	int i = 0;

    
   
2205

   

    
   
2206
	char *ie_str;

    
   
2207
	int indent;

    
   
2208

   

    
   
2209
	ie_str = ie2str(full_ie);

    
   
2210
	indent = strlen(ie_str) + 13; /* indentation including: prefix, ie.length etc: 13 = '> (len= 2) [ ') */

    
   
2211

   
2187
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2212
	/* 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 */
2213
	   walking through all bytes until one with ext bit (8) set to 1 */
2189
	do {
2214
	do {
2190
		switch(i) {
2215
		switch(i) {
2191
		case 0:	/* Octet 3 */
2216
		case 0:	/* Octet 3 */
2192
			pri_message(ctrl, "%c Redirecting Number (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",
2217
			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);
2218
				"%c %s (len=%2d) [ Ext: %d  TON: %s (%d)  NPI: %s (%d)",

    
   
2219
				prefix, ie_str, len, ie->data[0] >> 7,

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

    
   
2221
				npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
2194
			break;
2222
			break;
2195
		case 1: /* Octet 3a */
2223
		case 1: /* Octet 3a */
2196
			pri_message(ctrl, "\n");
2224
			pri_message(ctrl, "\n");
2197
			pri_message(ctrl, "%c                               Ext: %d  Presentation: %s (%d)",
2225
			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);
2226
				 indent, prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2199
			break;
2227
			break;
2200
		case 2: /* Octet 3b */
2228
		case 2: /* Octet 3b */
2201
			pri_message(ctrl, "\n");
2229
			pri_message(ctrl, "\n");
2202
			pri_message(ctrl, "%c                               Ext: %d  Reason: %s (%d)",
2230
			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);
2231
				 indent, prefix, ie->data[2] >> 7, redirection_reason2str(ie->data[2] & 0x7f), ie->data[2] & 0x7f);
2204
			break;
2232
			break;
2205
		}
2233
		}
2206
	} while(!(ie->data[i++]& 0x80));
2234
	} while (!(ie->data[i++] & 0x80));
2207
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2235
	q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
2208
	pri_message(ctrl, "  '%s' ]\n", cnum);
2236
	pri_message(ctrl, "  '%s' ]\n", cnum);
2209
}
2237
}
2210

    
   
2238

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

    
   
2264

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

    
   
2268

   
2241
	call->connected_number_in_message = 1;
2269
	call->connected_number_in_message = 1;
2242
	call->remote_id.number.valid = 1;
2270
	call->remote_id.number.valid = 1;
2243
	call->remote_id.number.presentation =
2271
	call->remote_id.number.presentation =
2244
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2272
		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
2273
	/* 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 */
2274
	   walking through all bytes until one with ext bit (8) set to 1 */
2247
	do {
2275
	do {
2248
		switch (i) {
2276
		switch (i) {
2249
		case 0:
2277
		case 0:
2250
			call->remote_id.number.plan = ie->data[i] & 0x7f;
2278
			call->remote_id.number.plan = ie->data[i] & 0x7f;
2251
			break;
2279
			break;
2252
		case 1:
2280
		case 1:
2253
			/* Keep only the presentation and screening fields */
2281
			/* Keep only the presentation and screening fields */
2254
			call->remote_id.number.presentation =
2282
			call->remote_id.number.presentation =
2255
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2283
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2256
			break;
2284
			break;
2257
		}
2285
		}
2258
	} while (!(ie->data[i++] & 0x80));
2286
	} while (!(ie->data[i++] & 0x80));
2259
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->remote_id.number.str,
2287
	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);
2288
		sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);
2261

    
   
2289

   
2262
	return 0;
2290
	return 0;
2263
}
2291
}
2264

    
   
2292

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

    
   
2296

   
2269
	if (!call->local_id.number.valid) {
2297
	if (!call->local_id.number.valid) {
2270
		return 0;
2298
		return 0;
2271
	}
2299
	}
2272

    
   
2300

   
2273
	datalen = strlen(call->local_id.number.str);
2301
	datalen = strlen(call->local_id.number.str);
2274
	ie->data[0] = call->local_id.number.plan;
2302
	ie->data[0] = call->local_id.number.plan;
2275
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2303
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2276
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2304
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2277
	return datalen + (2 + 2);
2305
	return datalen + (2 + 2);
2278
}
2306
}
2279

    
   
2307

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

    
   
2332

   
2305
static int receive_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2333
static int receive_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2306
{
2334
{
2307
	if (len < 3) {
2335
	if (len < 3) {
2308
		return -1;
2336
		return -1;
2309
	}
2337
	}
2310

    
   
2338

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

    
   
2342

   
2315
static int transmit_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2343
static int transmit_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2316
{
2344
{
2317
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2345
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2318
		1, len, order);
2346
		1, len, order);
2319
}
2347
}
2320

    
   
2348

   
2321
static void dump_connected_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2349
static void dump_connected_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2322
{
2350
{
2323
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2351
	dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
2324
}
2352
}
2325

    
   
2353

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

    
   
2357

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

    
   
2385

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

    
   
2389

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

    
   
2412

   
2385
	datalen = strlen(call->redirecting.from.number.str);
2413
	datalen = strlen(call->redirecting.from.number.str);
2386
	ie->data[0] = call->redirecting.from.number.plan;
2414
	ie->data[0] = call->redirecting.from.number.plan;
2387
#if 1
2415
#if 1
2388
	/* ETSI and Q.952 do not define the screening field */
2416
	/* ETSI and Q.952 do not define the screening field */
2389
	ie->data[1] = call->redirecting.from.number.presentation & PRI_PRES_RESTRICTION;
2417
	ie->data[1] = call->redirecting.from.number.presentation & PRI_PRES_RESTRICTION;
2390
#else
2418
#else
2391
	/* Q.931 defines the screening field */
2419
	/* Q.931 defines the screening field */
2392
	ie->data[1] = call->redirecting.from.number.presentation;
2420
	ie->data[1] = call->redirecting.from.number.presentation;
2393
#endif
2421
#endif
2394
	ie->data[2] = (call->redirecting.reason & 0x0f) | 0x80;
2422
	ie->data[2] = (call->redirecting.reason & 0x0f) | 0x80;
2395
	memcpy(ie->data + 3, call->redirecting.from.number.str, datalen);
2423
	memcpy(ie->data + 3, call->redirecting.from.number.str, datalen);
2396
	return datalen + (3 + 2);
2424
	return datalen + (3 + 2);
2397
}
2425
}
2398

    
   
2426

   
2399
static void dump_redirecting_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2427
static void dump_redirecting_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2400
{
2428
{
2401
	dump_subaddr_helper(full_ie, ctrl, ie, 2, len, len - 4, prefix);
2429
	dump_subaddr_helper(full_ie, ctrl, ie, 2, len, len - 4, prefix);
2402
}
2430
}
2403

    
   
2431

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

    
   
2435

   
2408
	call->redirection_number.valid = 1;
2436
	call->redirection_number.valid = 1;
2409
	call->redirection_number.presentation =
2437
	call->redirection_number.presentation =
2410
		PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2438
		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
2439
	/* 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 */
2440
	   walking through all bytes until one with ext bit (8) set to 1 */
2413
	do {
2441
	do {
2414
		switch (i) {
2442
		switch (i) {
2415
		case 0:
2443
		case 0:
2416
			call->redirection_number.plan = ie->data[i] & 0x7f;
2444
			call->redirection_number.plan = ie->data[i] & 0x7f;
2417
			break;
2445
			break;
2418
		case 1:
2446
		case 1:
2419
			/* Keep only the presentation and screening fields */
2447
			/* Keep only the presentation and screening fields */
2420
			call->redirection_number.presentation =
2448
			call->redirection_number.presentation =
2421
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2449
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2422
			break;
2450
			break;
2423
		}
2451
		}
2424
	} while (!(ie->data[i++] & 0x80));
2452
	} while (!(ie->data[i++] & 0x80));
2425
	q931_strget_gripe(ctrl, ie2str(full_ie),
2453
	q931_strget_gripe(ctrl, ie2str(full_ie),
2426
		(unsigned char *) call->redirection_number.str,
2454
		(unsigned char *) call->redirection_number.str,
2427
		sizeof(call->redirection_number.str), ie->data + i, ie->len - i);
2455
		sizeof(call->redirection_number.str), ie->data + i, ie->len - i);
2428
	return 0;
2456
	return 0;
2429
}
2457
}
2430

    
   
2458

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

    
   
2462

   
2435
	if (order > 1) {
2463
	if (order > 1) {
2436
		return 0;
2464
		return 0;
2437
	}
2465
	}
2438
	if (!call->redirection_number.valid) {
2466
	if (!call->redirection_number.valid) {
2439
		return 0;
2467
		return 0;
2440
	}
2468
	}
2441

    
   
2469

   
2442
	datalen = strlen(call->redirection_number.str);
2470
	datalen = strlen(call->redirection_number.str);
2443
	ie->data[0] = call->redirection_number.plan;
2471
	ie->data[0] = call->redirection_number.plan;
2444
	ie->data[1] = (call->redirection_number.presentation & PRI_PRES_RESTRICTION) | 0x80;
2472
	ie->data[1] = (call->redirection_number.presentation & PRI_PRES_RESTRICTION) | 0x80;
2445
	memcpy(ie->data + 2, call->redirection_number.str, datalen);
2473
	memcpy(ie->data + 2, call->redirection_number.str, datalen);
2446
	return datalen + (2 + 2);
2474
	return datalen + (2 + 2);
2447
}
2475
}
2448

    
   
2476

   
2449
static int receive_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2477
static int receive_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2450
{
2478
{
2451
	if (len < 3) {
2479
	if (len < 3) {
2452
		return -1;
2480
		return -1;
2453
	}
2481
	}
2454

    
   
2482

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

    
   
2486

   
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)
2487
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
{
2488
{
2461
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2489
	return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
2462
		1, len, order);
2490
		1, len, order);
2463
}
2491
}
2464

    
   
2492

   
2465
static int receive_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2493
static int receive_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2466
{
2494
{
2467
	if (len < 3) {
2495
	if (len < 3) {
2468
		return -1;
2496
		return -1;
2469
	}
2497
	}
2470
	return receive_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1,
2498
	return receive_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1,
2471
		len - 3);
2499
		len - 3);
2472
}
2500
}
2473

    
   
2501

   
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)
2502
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
{
2503
{
2476
	return transmit_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie,
2504
	return transmit_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie,
2477
		1, len, order);
2505
		1, len, order);
2478
}
2506
}
2479

    
   
2507

   
2480
static int receive_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2508
static int receive_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2481
{
2509
{
2482
	size_t called_len;
2510
	size_t called_len;
2483
	size_t max_len;
2511
	size_t max_len;
2484
	char *called_end;
2512
	char *called_end;
2485

    
   
2513

   
2486
	if (len < 3) {
2514
	if (len < 3) {
2487
		return -1;
2515
		return -1;
2488
	}
2516
	}
2489

    
   
2517

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

    
   
2554

   
2527
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->overlap_digits,
2555
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->overlap_digits,
2528
		sizeof(call->overlap_digits), ie->data + 1, len - 3);
2556
		sizeof(call->overlap_digits), ie->data + 1, len - 3);
2529
	return 0;
2557
	return 0;
2530
}
2558
}
2531

    
   
2559

   
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)
2560
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
{
2561
{
2534
	size_t datalen;
2562
	size_t datalen;
2535

    
   
2563

   
2536
	if (!call->called.number.valid) {
2564
	if (!call->called.number.valid) {
2537
		return 0;
2565
		return 0;
2538
	}
2566
	}
2539

    
   
2567

   
2540
	datalen = strlen(call->overlap_digits);
2568
	datalen = strlen(call->overlap_digits);
2541
	ie->data[0] = 0x80 | call->called.number.plan;
2569
	ie->data[0] = 0x80 | call->called.number.plan;
2542
	memcpy(ie->data + 1, call->overlap_digits, datalen);
2570
	memcpy(ie->data + 1, call->overlap_digits, datalen);
2543
	return datalen + (1 + 2);
2571
	return datalen + (1 + 2);
2544
}
2572
}
2545

    
   
2573

   
2546
static int receive_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2574
static int receive_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2547
{
2575
{
2548
	int i = 0;
2576
	int i = 0;
2549
	struct q931_party_number number;
2577
	struct q931_party_number number;
2550

    
   
2578

   
2551
	q931_party_number_init(&number);
2579
	q931_party_number_init(&number);
2552
	number.valid = 1;
2580
	number.valid = 1;
2553
	number.presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2581
	number.presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2554

    
   
2582

   
2555
	/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
2583
	/* 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 */
2584
	   walking through all bytes until one with ext bit (8) set to 1 */
2557
	do {
2585
	do {
2558
		switch (i) {
2586
		switch (i) {
2559
		case 0:
2587
		case 0:
2560
			number.plan = ie->data[i] & 0x7f;
2588
			number.plan = ie->data[i] & 0x7f;
2561
			break;
2589
			break;
2562
		case 1:
2590
		case 1:
2563
			/* Keep only the presentation and screening fields */
2591
			/* Keep only the presentation and screening fields */
2564
			number.presentation =
2592
			number.presentation =
2565
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2593
				ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
2566
			break;
2594
			break;
2567
		}
2595
		}
2568
	} while (!(ie->data[i++] & 0x80));
2596
	} while (!(ie->data[i++] & 0x80));
2569
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) number.str,
2597
	q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) number.str,
2570
		sizeof(number.str), ie->data + i, ie->len - i);
2598
		sizeof(number.str), ie->data + i, ie->len - i);
2571

    
   
2599

   
2572
	/* There can be more than one calling party number ie in the SETUP message. */
2600
	/* 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)
2601
	if (number.presentation == (PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER)
2574
		|| number.presentation == (PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER)) {
2602
		|| number.presentation == (PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER)) {
2575
		/* The number is network provided so it is an ANI number. */
2603
		/* The number is network provided so it is an ANI number. */
2576
		call->ani = number;
2604
		call->ani = number;
2577
		if (!call->remote_id.number.valid) {
2605
		if (!call->remote_id.number.valid) {
2578
			/* Copy ANI to CallerID if CallerID is not already set. */
2606
			/* Copy ANI to CallerID if CallerID is not already set. */
2579
			call->remote_id.number = number;
2607
			call->remote_id.number = number;
2580
		}
2608
		}
2581
	} else {
2609
	} else {
2582
		call->remote_id.number = number;
2610
		call->remote_id.number = number;
2583
	}
2611
	}
2584

    
   
2612

   
2585
	return 0;
2613
	return 0;
2586
}
2614
}
2587

    
   
2615

   
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)
2616
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
{
2617
{
2590
	size_t datalen;
2618
	size_t datalen;
2591

    
   
2619

   
2592
	if (!call->local_id.number.valid) {
2620
	if (!call->local_id.number.valid) {
2593
		return 0;
2621
		return 0;
2594
	}
2622
	}
2595

    
   
2623

   
2596
	datalen = strlen(call->local_id.number.str);
2624
	datalen = strlen(call->local_id.number.str);
2597
	ie->data[0] = call->local_id.number.plan;
2625
	ie->data[0] = call->local_id.number.plan;
2598
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2626
	ie->data[1] = 0x80 | call->local_id.number.presentation;
2599
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2627
	memcpy(ie->data + 2, call->local_id.number.str, datalen);
2600
	return datalen + (2 + 2);
2628
	return datalen + (2 + 2);
2601
}
2629
}
2602

    
   
2630

   
2603
static void dump_user_user(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2631
static void dump_user_user(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2604
{
2632
{
2605
	int x;
2633
	int x;
2606
	pri_message(ctrl, "%c User-User Information (len=%2d) [", prefix, len);
2634
	pri_message(ctrl, "%c %s (len=%2d) [", prefix, ie2str(full_ie), len);
2607
	for (x=0;x<ie->len;x++)
2635
	for (x = 0; x < ie->len; x++) {
2608
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2636
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);

    
   
2637
	}
2609
	pri_message(ctrl, " ]\n");
2638
	pri_message(ctrl, " ]\n");
2610
}
2639
}
2611

    
   
2640

   
2612

    
   
2641

   
2613
static int receive_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2642
static int receive_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2614
{
2643
{
2615
	call->useruserprotocoldisc = ie->data[0] & 0xff;
2644
	call->useruserprotocoldisc = ie->data[0] & 0xff;
2616
	if (call->useruserprotocoldisc == 4) { /* IA5 */
2645
	if (call->useruserprotocoldisc == 4) { /* IA5 */
2617
		q931_memget((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
2646
		q931_memget((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
2618
	}
2647
	}
2619
	return 0;
2648
	return 0;
2620
}
2649
}
2621

    
   
2650

   
2622
static int transmit_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2651
static int transmit_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2623
{        
2652
{        
2624
	int datalen = strlen(call->useruserinfo);
2653
	int datalen = strlen(call->useruserinfo);
2625
	if (datalen > 0) {
2654
	if (datalen > 0) {
2626
		/* Restricted to 35 characters */
2655
		/* Restricted to 35 characters */
2627
		if (msgtype == Q931_USER_INFORMATION) {
2656
		if (msgtype == Q931_USER_INFORMATION) {
2628
			if (datalen > 260)
2657
			if (datalen > 260)
2629
				datalen = 260;
2658
				datalen = 260;
2630
		} else {
2659
		} else {
2631
			if (datalen > 35)
2660
			if (datalen > 35)
2632
				datalen = 35;
2661
				datalen = 35;
2633
		}
2662
		}
2634
		ie->data[0] = 4; /* IA5 characters */
2663
		ie->data[0] = 4; /* IA5 characters */
2635
		memcpy(&ie->data[1], call->useruserinfo, datalen);
2664
		memcpy(&ie->data[1], call->useruserinfo, datalen);
2636
		call->useruserinfo[0] = '\0';
2665
		call->useruserinfo[0] = '\0';
2637
		return datalen + 3;
2666
		return datalen + 3;
2638
	}
2667
	}
2639

    
   
2668

   
2640
	return 0;
2669
	return 0;
2641
}
2670
}
2642

    
   
2671

   
2643
static void dump_change_status(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2672
static void dump_change_status(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2644
{
2673
{
2645
	int x;
2674
	int x;
2646
	
2675

   
2647
	pri_message(ctrl, "%c Change Status Information (len=%2d) [", prefix, len);
2676
	pri_message(ctrl, "%c %s (len=%2d) [", prefix, ie2str(full_ie), len);
2648
	for (x=0; x<ie->len; x++) {
2677
	for (x = 0; x < ie->len; x++) {
2649
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2678
		pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
2650
	}
2679
	}
2651
	pri_message(ctrl, " ]\n");
2680
	pri_message(ctrl, " ]\n");
2652
}
2681
}
2653

    
   
2682

   
2654
static int receive_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2683
static int receive_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2655
{
2684
{
2656
	call->changestatus = ie->data[0] & 0x0f;
2685
	call->changestatus = ie->data[0] & 0x0f;
2657
	return 0;
2686
	return 0;
2658
}
2687
}
2659

    
   
2688

   
2660
static int transmit_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2689
static int transmit_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2661
{
2690
{
2662
	ie->data[0] = 0xc0 | call->changestatus;
2691
	ie->data[0] = 0xc0 | call->changestatus;
2663
	return 3;
2692
	return 3;
2664
}
2693
}
2665

    
   
2694

   
2666
static char *prog2str(int prog)
2695
static char *prog2str(int prog)
2667
{
2696
{
2668
	static struct msgtype progs[] = {
2697
	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." },
2698
		{ 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." },
2699
		{ Q931_PROG_CALLED_NOT_ISDN, "Called equipment is non-ISDN." },
2671
		{ Q931_PROG_CALLER_NOT_ISDN, "Calling equipment is non-ISDN." },
2700
		{ Q931_PROG_CALLER_NOT_ISDN, "Calling equipment is non-ISDN." },
2672
		{ Q931_PROG_INBAND_AVAILABLE, "Inband information or appropriate pattern now available." },
2701
		{ Q931_PROG_INBAND_AVAILABLE, "Inband information or appropriate pattern now available." },
2673
		{ Q931_PROG_DELAY_AT_INTERF, "Delay in response at called Interface." },
2702
		{ Q931_PROG_DELAY_AT_INTERF, "Delay in response at called Interface." },
2674
		{ Q931_PROG_INTERWORKING_WITH_PUBLIC, "Interworking with a public network." },
2703
		{ 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." },
2704
		{ 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." },
2705
		{ 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." },
2706
		{ Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER, "Interworking with a network unable to supply a release signal after answer." },
2678
	};
2707
	};
2679
	return code2str(prog, progs, sizeof(progs) / sizeof(progs[0]));
2708
	return code2str(prog, progs, sizeof(progs) / sizeof(progs[0]));
2680
}
2709
}
2681

    
   
2710

   
2682
static char *coding2str(int cod)
2711
static char *coding2str(int cod)
2683
{
2712
{
2684
	static struct msgtype cods[] = {
2713
	static struct msgtype cods[] = {
2685
		{ CODE_CCITT, "CCITT (ITU) standard" },
2714
		{ CODE_CCITT, "CCITT (ITU) standard" },
2686
		{ CODE_INTERNATIONAL, "Non-ITU international standard" }, 
2715
		{ CODE_INTERNATIONAL, "Non-ITU international standard" }, 
2687
		{ CODE_NATIONAL, "National standard" }, 
2716
		{ CODE_NATIONAL, "National standard" }, 
2688
		{ CODE_NETWORK_SPECIFIC, "Network specific standard" },
2717
		{ CODE_NETWORK_SPECIFIC, "Network specific standard" },
2689
	};
2718
	};
2690
	return code2str(cod, cods, sizeof(cods) / sizeof(cods[0]));
2719
	return code2str(cod, cods, sizeof(cods) / sizeof(cods[0]));
2691
}
2720
}
2692

    
   
2721

   
2693
static char *loc2str(int loc)
2722
static char *loc2str(int loc)
2694
{
2723
{
2695
	static struct msgtype locs[] = {
2724
	static struct msgtype locs[] = {
2696
		{ LOC_USER, "User" },
2725
		{ LOC_USER, "User" },
2697
		{ LOC_PRIV_NET_LOCAL_USER, "Private network serving the local user" },
2726
		{ LOC_PRIV_NET_LOCAL_USER, "Private network serving the local user" },
2698
		{ LOC_PUB_NET_LOCAL_USER, "Public network serving the local user" },
2727
		{ LOC_PUB_NET_LOCAL_USER, "Public network serving the local user" },
2699
		{ LOC_TRANSIT_NET, "Transit network" },
2728
		{ LOC_TRANSIT_NET, "Transit network" },
2700
		{ LOC_PUB_NET_REMOTE_USER, "Public network serving the remote user" },
2729
		{ LOC_PUB_NET_REMOTE_USER, "Public network serving the remote user" },
2701
		{ LOC_PRIV_NET_REMOTE_USER, "Private network serving the remote user" },
2730
		{ LOC_PRIV_NET_REMOTE_USER, "Private network serving the remote user" },
2702
		{ LOC_INTERNATIONAL_NETWORK, "International network" },
2731
		{ LOC_INTERNATIONAL_NETWORK, "International network" },
2703
		{ LOC_NETWORK_BEYOND_INTERWORKING, "Network beyond the interworking point" },
2732
		{ LOC_NETWORK_BEYOND_INTERWORKING, "Network beyond the interworking point" },
2704
	};
2733
	};
2705
	return code2str(loc, locs, sizeof(locs) / sizeof(locs[0]));
2734
	return code2str(loc, locs, sizeof(locs) / sizeof(locs[0]));
2706
}
2735
}
2707

    
   
2736

   
2708
static void dump_progress_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2737
static void dump_progress_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
2709
{
2738
{
2710
	pri_message(ctrl, "%c Progress Indicator (len=%2d) [ Ext: %d  Coding: %s (%d)  0: %d  Location: %s (%d)\n",
2739
	pri_message(ctrl,
2711
		prefix, len, ie->data[0] >> 7, coding2str((ie->data[0] & 0x60) >> 5), (ie->data[0] & 0x60) >> 5,
2740
		"%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);
2741
		prefix, ie2str(full_ie), len, ie->data[0] >> 7, coding2str((ie->data[0] & 0x60) >> 5),

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

    
   
2743
		loc2str(ie->data[0] & 0xf), ie->data[0] & 0xf);
2713
	pri_message(ctrl, "%c                               Ext: %d  Progress Description: %s (%d) ]\n",
2744
	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);
2745
		prefix, ie->data[1] >> 7, prog2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
2715
}
2746
}
2716

    
   
2747

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

    
   
2751

   
2721
	if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_BLOCK) {
2752
	if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_BLOCK) {
2722
		return 0;
2753
		return 0;
2723
	}
2754
	}
2724

    
   
2755

   
2725
	data = ie->data;
2756
	data = ie->data;
2726
	if (data[0] & 0x80) {
2757
	if (data[0] & 0x80) {
2727
		/* Skip over character set */
2758
		/* Skip over character set */
2728
		data++;
2759
		data++;
2729
		len--;
2760
		len--;
2730
	}
2761
	}
2731

    
   
2762

   
2732
	call->display.text = data;
2763
	call->display.text = data;
2733
	call->display.full_ie = full_ie;
2764
	call->display.full_ie = full_ie;
2734
	call->display.length = len - 2;
2765
	call->display.length = len - 2;
2735
	call->display.char_set = PRI_CHAR_SET_ISO8859_1;
2766
	call->display.char_set = PRI_CHAR_SET_ISO8859_1;
2736
	return 0;
2767
	return 0;
2737
}
2768
}
2738

    
   
2769

   
2739
static int transmit_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2770
static int transmit_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2740
{
2771
{
2741
	size_t datalen;
2772
	size_t datalen;
2742
	int i;
2773
	int i;
2743

    
   
2774

   
2744
	if (!call->display.text || !call->display.length) {
2775
	if (!call->display.text || !call->display.length) {
2745
		return 0;
2776
		return 0;
2746
	}
2777
	}
2747
	if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_BLOCK) {
2778
	if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_BLOCK) {
2748
		return 0;
2779
		return 0;
2749
	}
2780
	}
2750

    
   
2781

   
2751
	i = 0;
2782
	i = 0;
2752
	switch (ctrl->switchtype) {
2783
	switch (ctrl->switchtype) {
2753
	case PRI_SWITCH_QSIG:
2784
	case PRI_SWITCH_QSIG:
2754
	case PRI_SWITCH_EUROISDN_E1:
2785
	case PRI_SWITCH_EUROISDN_E1:
2755
	case PRI_SWITCH_EUROISDN_T1:
2786
	case PRI_SWITCH_EUROISDN_T1:
2756
		break;
2787
		break;
2757
	default:
2788
	default:
2758
		/* Prefix text with character set indicator. */
2789
		/* Prefix text with character set indicator. */
2759
		ie->data[0] = 0xb1;
2790
		ie->data[0] = 0xb1;
2760
		++i;
2791
		++i;
2761
		break;
2792
		break;
2762
	}
2793
	}
2763

    
   
2794

   
2764
	datalen = call->display.length;
2795
	datalen = call->display.length;
2765
	if (MAX_DISPLAY_TEXT < datalen + i) {
2796
	if (MAX_DISPLAY_TEXT < datalen + i) {
2766
		datalen = MAX_DISPLAY_TEXT - i;
2797
		datalen = MAX_DISPLAY_TEXT - i;
2767
	}
2798
	}
2768
	memcpy(ie->data + i, call->display.text, datalen);
2799
	memcpy(ie->data + i, call->display.text, datalen);
2769
	return 2 + i + datalen;
2800
	return 2 + i + datalen;
2770
}
2801
}
2771

    
   
2802

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

    
   
2844

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

    
   
2846

   
2816
static int transmit_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2847
static int transmit_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
2817
{
2848
{
2818
	struct apdu_event **prev;
2849
	struct apdu_event **prev;
2819
	struct apdu_event *cur;
2850
	struct apdu_event *cur;
2820
	int apdu_len;
2851
	int apdu_len;
2821

    
   
2852

   
2822
	for (prev = &call->apdus, cur = call->apdus;
2853
	for (prev = &call->apdus, cur = call->apdus;
2823
		cur;
2854
		cur;
2824
		prev = &cur->next, cur = cur->next) {
2855
		prev = &cur->next, cur = cur->next) {
2825
		if (!cur->sent && (cur->message == msgtype || cur->message == Q931_ANY_MESSAGE)) {
2856
		if (!cur->sent && (cur->message == msgtype || cur->message == Q931_ANY_MESSAGE)) {
2826
			break;
2857
			break;
2827
		}
2858
		}
2828
	}
2859
	}
2829
	if (!cur) {
2860
	if (!cur) {
2830
		/* No APDU found */
2861
		/* No APDU found */
2831
		return 0;
2862
		return 0;
2832
	}
2863
	}
2833

    
   
2864

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

    
   
2870

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

    
   
2875

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

    
   
2878

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

    
   
2883

   
2853
		free(cur);
2884
		free(cur);
2854
		return 0;
2885
		return 0;
2855
	}
2886
	}
2856

    
   
2887

   
2857
	memcpy(ie->data, cur->apdu, cur->apdu_len);
2888
	memcpy(ie->data, cur->apdu, cur->apdu_len);
2858
	apdu_len = cur->apdu_len;
2889
	apdu_len = cur->apdu_len;
2859
	cur->sent = 1;
2890
	cur->sent = 1;
2860

    
   
2891

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

    
   
2894

   
2864
		if (0 < cur->response.timeout_time) {
2895
		if (0 < cur->response.timeout_time) {
2865
			/* Sender specified a timeout duration. */
2896
			/* Sender specified a timeout duration. */
2866
			cur->timer = pri_schedule_event(ctrl, cur->response.timeout_time,
2897
			cur->timer = pri_schedule_event(ctrl, cur->response.timeout_time,
2867
				q931_apdu_timeout, cur);
2898
				q931_apdu_timeout, cur);
2868
			failed = !cur->timer;
2899
			failed = !cur->timer;
2869
		} else {
2900
		} else {
2870
			/* Sender wants to "timeout" only when specified messages are received. */
2901
			/* Sender wants to "timeout" only when specified messages are received. */
2871
			failed = !cur->response.num_messages;
2902
			failed = !cur->response.num_messages;
2872
		}
2903
		}
2873
		if (failed) {
2904
		if (failed) {
2874
			/* Remove APDU from list. */
2905
			/* Remove APDU from list. */
2875
			*prev = cur->next;
2906
			*prev = cur->next;
2876

    
   
2907

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

    
   
2910

   
2880
			free(cur);
2911
			free(cur);
2881
		}
2912
		}
2882
	} else {
2913
	} else {
2883
		/* Remove APDU from list. */
2914
		/* Remove APDU from list. */
2884
		*prev = cur->next;
2915
		*prev = cur->next;
2885
		free(cur);
2916
		free(cur);
2886
	}
2917
	}
2887

    
   
2918

   
2888
	return apdu_len + 2;
2919
	return apdu_len + 2;
2889
}
2920
}
2890

    
   
2921

   
2891
static int receive_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2922
static int receive_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
2892
{
2923
{
2893
	/* Delay processing facility ie's till after all other ie's are processed. */
2924
	/* Delay processing facility ie's till after all other ie's are processed. */
2894
	if (MAX_FACILITY_IES <= ctrl->facility.count) {
2925
	if (MAX_FACILITY_IES <= ctrl->facility.count) {
2895
		pri_message(ctrl, "!! Too many facility ie's to delay.\n");
2926
		pri_message(ctrl, "!! Too many facility ie's to delay.\n");
2896
		return -1;
2927
		return -1;
2897
	}
2928
	}
2898
	/* Make sure we have enough room for the protocol profile ie octet(s) */
2929
	/* Make sure we have enough room for the protocol profile ie octet(s) */
2899
	if (ie->data + ie->len < ie->data + 2) {
2930
	if (ie->data + ie->len < ie->data + 2) {
2900
		return -1;
2931
		return -1;
2901
	}
2932
	}
2902

    
   
2933

   
2903
	/* Save the facility ie location for delayed decode. */
2934
	/* Save the facility ie location for delayed decode. */
2904
	ctrl->facility.ie[ctrl->facility.count] = ie;
2935
	ctrl->facility.ie[ctrl->facility.count] = ie;
2905
	ctrl->facility.codeset[ctrl->facility.count] = Q931_IE_CODESET((unsigned) full_ie);
2936
	ctrl->facility.codeset[ctrl->facility.count] = Q931_IE_CODESET((unsigned) full_ie);
2906
	++ctrl->facility.count;
2937
	++ctrl->facility.count;
2907
	return 0;
2938
	return 0;
2908
}
2939
}
2909

    
   
2940

   
2910
static int process_facility(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie)
2941
static int process_facility(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie)
2911
{
2942
{
2912
	struct fac_extension_header header;
2943
	struct fac_extension_header header;
2913
	struct rose_message rose;
2944
	struct rose_message rose;
2914
	const unsigned char *pos;
2945
	const unsigned char *pos;
2915
	const unsigned char *end;
2946
	const unsigned char *end;
2916

    
   
2947

   
2917
	pos = ie->data;
2948
	pos = ie->data;
2918
	end = ie->data + ie->len;
2949
	end = ie->data + ie->len;
2919

    
   
2950

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

    
   
2974

   
2944
	if (ctrl->debug & PRI_DEBUG_APDU) {
2975
	if (ctrl->debug & PRI_DEBUG_APDU) {
2945
		asn1_dump(ctrl, pos, end);
2976
		asn1_dump(ctrl, pos, end);
2946
	}
2977
	}
2947

    
   
2978

   
2948
	pos = fac_dec_extension_header(ctrl, pos, end, &header);
2979
	pos = fac_dec_extension_header(ctrl, pos, end, &header);
2949
	if (!pos) {
2980
	if (!pos) {
2950
		return -1;
2981
		return -1;
2951
	}
2982
	}
2952
	if (header.npp_present) {
2983
	if (header.npp_present) {
2953
		if (ctrl->debug & PRI_DEBUG_APDU) {
2984
		if (ctrl->debug & PRI_DEBUG_APDU) {
2954
			pri_message(ctrl,
2985
			pri_message(ctrl,
2955
				"!! Don't know how to handle Network Protocol Profile type 0x%X\n",
2986
				"!! Don't know how to handle Network Protocol Profile type 0x%X\n",
2956
				header.npp);
2987
				header.npp);
2957
		}
2988
		}
2958
		return -1;
2989
		return -1;
2959
	}
2990
	}
2960

    
   
2991

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

    
   
3017

   
2987
static void q931_handle_facilities(struct pri *ctrl, q931_call *call, int msgtype)
3018
static void q931_handle_facilities(struct pri *ctrl, q931_call *call, int msgtype)
2988
{
3019
{
2989
	unsigned idx;
3020
	unsigned idx;
2990
	unsigned codeset;
3021
	unsigned codeset;
2991
	unsigned full_ie;
3022
	unsigned full_ie;
2992
	q931_ie *ie;
3023
	q931_ie *ie;
2993

    
   
3024

   
2994
	for (idx = 0; idx < ctrl->facility.count; ++idx) {
3025
	for (idx = 0; idx < ctrl->facility.count; ++idx) {
2995
		ie = ctrl->facility.ie[idx];
3026
		ie = ctrl->facility.ie[idx];
2996
		if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
3027
		if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
2997
			codeset = ctrl->facility.codeset[idx];
3028
			codeset = ctrl->facility.codeset[idx];
2998
			full_ie = Q931_FULL_IE(codeset, ie->ie);
3029
			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));
3030
			pri_message(ctrl, "-- Delayed processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
3000
		}
3031
		}
3001
		process_facility(ctrl, call, msgtype, ie);
3032
		process_facility(ctrl, call, msgtype, ie);
3002
	}
3033
	}
3003
}
3034
}
3004

    
   
3035

   
3005
/*!
3036
/*!
3006
 * \internal
3037
 * \internal
3007
 * \brief Check if any APDU responses "timeout" with the current Q.931 message.
3038
 * \brief Check if any APDU responses "timeout" with the current Q.931 message.
3008
 *
3039
 *
3009
 * \param ctrl D channel controller.
3040
 * \param ctrl D channel controller.
3010
 * \param call Q.931 call leg.
3041
 * \param call Q.931 call leg.
3011
 * \param msgtype Q.931 message type received.
3042
 * \param msgtype Q.931 message type received.
3012
 *
3043
 *
3013
 * \return Nothing
3044
 * \return Nothing
3014
 */
3045
 */
3015
static void q931_apdu_msg_expire(struct pri *ctrl, struct q931_call *call, int msgtype)
3046
static void q931_apdu_msg_expire(struct pri *ctrl, struct q931_call *call, int msgtype)
3016
{
3047
{
3017
	struct apdu_event **prev;
3048
	struct apdu_event **prev;
3018
	struct apdu_event **prev_next;
3049
	struct apdu_event **prev_next;
3019
	struct apdu_event *cur;
3050
	struct apdu_event *cur;
3020
	unsigned idx;
3051
	unsigned idx;
3021

    
   
3052

   
3022
	for (prev = &call->apdus; *prev; prev = prev_next) {
3053
	for (prev = &call->apdus; *prev; prev = prev_next) {
3023
		cur = *prev;
3054
		cur = *prev;
3024
		prev_next = &cur->next;
3055
		prev_next = &cur->next;
3025
		if (cur->sent) {
3056
		if (cur->sent) {
3026
			for (idx = 0; idx < cur->response.num_messages; ++idx) {
3057
			for (idx = 0; idx < cur->response.num_messages; ++idx) {
3027
				if (cur->response.message_type[idx] == msgtype) {
3058
				if (cur->response.message_type[idx] == msgtype) {
3028
					/*
3059
					/*
3029
					 * APDU response message "timeout".
3060
					 * APDU response message "timeout".
3030
					 *
3061
					 *
3031
					 * Extract the APDU from the list so it cannot be
3062
					 * Extract the APDU from the list so it cannot be
3032
					 * deleted from under us by the callback.
3063
					 * deleted from under us by the callback.
3033
					 */
3064
					 */
3034
					prev_next = prev;
3065
					prev_next = prev;
3035
					*prev = cur->next;
3066
					*prev = cur->next;
3036

    
   
3067

   
3037
					/* Stop any response timeout. */
3068
					/* Stop any response timeout. */
3038
					pri_schedule_del(ctrl, cur->timer);
3069
					pri_schedule_del(ctrl, cur->timer);
3039
					cur->timer = 0;
3070
					cur->timer = 0;
3040

    
   
3071

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

    
   
3073

   
3043
					free(cur);
3074
					free(cur);
3044
					break;
3075
					break;
3045
				}
3076
				}
3046
			}
3077
			}
3047
		}
3078
		}
3048
	}
3079
	}
3049
}
3080
}
3050

    
   
3081

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

    
   
3152

   
3122
static int receive_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3153
static int receive_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3123
{
3154
{
3124
	call->sugcallstate = ie->data[0] & 0x3f;
3155
	call->sugcallstate = ie->data[0] & 0x3f;
3125
	return 0;
3156
	return 0;
3126
}
3157
}
3127

    
   
3158

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

    
   
3195

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

    
   
3218

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

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

    
   
3224
		coding2str((ie->data[0] & 0xC0) >> 6), (ie->data[0] & 0xC0) >> 6,
3192
		q931_call_state_str(ie->data[0] & 0x3f), ie->data[0] & 0x3f);
3225
		q931_call_state_str(ie->data[0] & 0x3f), ie->data[0] & 0x3f);
3193
}
3226
}
3194

    
   
3227

   
3195
static void dump_call_identity(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3228
static void dump_call_identity(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3196
{
3229
{
3197
	int x;
3230
	int x;
3198
	pri_message(ctrl, "%c Call Identity (len=%2d) [ ", prefix, len);
3231
	pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), len);
3199
	for (x=0;x<ie->len;x++) 
3232
	for (x = 0; x < ie->len; x++) {
3200
		pri_message(ctrl, "0x%02X ", ie->data[x]);
3233
		pri_message(ctrl, "0x%02X ", ie->data[x]);

    
   
3234
	}
3201
	pri_message(ctrl, " ]\n");
3235
	pri_message(ctrl, " ]\n");
3202
}
3236
}
3203

    
   
3237

   
3204
static void dump_time_date(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3238
static void dump_time_date(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3205
{
3239
{
3206
	pri_message(ctrl, "%c Time Date (len=%2d) [ ", prefix, len);
3240
	pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), len);
3207
	if (ie->len > 0)
3241
	if (ie->len > 0)
3208
		pri_message(ctrl, "%02d", ie->data[0]);
3242
		pri_message(ctrl, "%02d", ie->data[0]);
3209
	if (ie->len > 1)
3243
	if (ie->len > 1)
3210
		pri_message(ctrl, "-%02d", ie->data[1]);
3244
		pri_message(ctrl, "-%02d", ie->data[1]);
3211
	if (ie->len > 2)
3245
	if (ie->len > 2)
3212
		pri_message(ctrl, "-%02d", ie->data[2]);
3246
		pri_message(ctrl, "-%02d", ie->data[2]);
3213
	if (ie->len > 3)
3247
	if (ie->len > 3)
3214
		pri_message(ctrl, " %02d", ie->data[3]);
3248
		pri_message(ctrl, " %02d", ie->data[3]);
3215
	if (ie->len > 4)
3249
	if (ie->len > 4)
3216
		pri_message(ctrl, ":%02d", ie->data[4]);
3250
		pri_message(ctrl, ":%02d", ie->data[4]);
3217
	if (ie->len > 5)
3251
	if (ie->len > 5)
3218
		pri_message(ctrl, ":%02d", ie->data[5]);
3252
		pri_message(ctrl, ":%02d", ie->data[5]);
3219
	pri_message(ctrl, " ]\n");
3253
	pri_message(ctrl, " ]\n");
3220
}
3254
}
3221

    
   
3255

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

    
   
3261

   
3228
static int transmit_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3262
static int transmit_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3229
{
3263
{
3230
	time_t now;
3264
	time_t now;
3231
	struct tm timedate;
3265
	struct tm timedate;
3232
	int ie_len;
3266
	int ie_len;
3233

    
   
3267

   
3234
	do {
3268
	do {
3235
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE) {
3269
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE) {
3236
			ie_len = 0;
3270
			ie_len = 0;
3237
			break;
3271
			break;
3238
		}
3272
		}
3239

    
   
3273

   
3240
		/* Send the current date/time. */
3274
		/* Send the current date/time. */
3241
		time(&now);
3275
		time(&now);
3242
		localtime_r(&now, &timedate);
3276
		localtime_r(&now, &timedate);
3243
		ie->data[0] = timedate.tm_year - 100; /* 1900+ */
3277
		ie->data[0] = timedate.tm_year - 100; /* 1900+ */
3244
		ie->data[1] = timedate.tm_mon + 1;
3278
		ie->data[1] = timedate.tm_mon + 1;
3245
		ie->data[2] = timedate.tm_mday;
3279
		ie->data[2] = timedate.tm_mday;
3246
		ie_len = 2 + 3;
3280
		ie_len = 2 + 3;
3247
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HH) {
3281
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HH) {
3248
			break;
3282
			break;
3249
		}
3283
		}
3250

    
   
3284

   
3251
		/* Add optional hour. */
3285
		/* Add optional hour. */
3252
		ie->data[3] = timedate.tm_hour;
3286
		ie->data[3] = timedate.tm_hour;
3253
		++ie_len;
3287
		++ie_len;
3254
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMM) {
3288
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMM) {
3255
			break;
3289
			break;
3256
		}
3290
		}
3257

    
   
3291

   
3258
		/* Add optional minutes. */
3292
		/* Add optional minutes. */
3259
		ie->data[4] = timedate.tm_min;
3293
		ie->data[4] = timedate.tm_min;
3260
		++ie_len;
3294
		++ie_len;
3261
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMMSS) {
3295
		if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMMSS) {
3262
			break;
3296
			break;
3263
		}
3297
		}
3264

    
   
3298

   
3265
		/* Add optional seconds. */
3299
		/* Add optional seconds. */
3266
		ie->data[5] = timedate.tm_sec;
3300
		ie->data[5] = timedate.tm_sec;
3267
		++ie_len;
3301
		++ie_len;
3268
	} while (0);
3302
	} while (0);
3269
	return ie_len;
3303
	return ie_len;
3270
}
3304
}
3271

    
   
3305

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

    
   
3309

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

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

    
   
3314

   
3280
static int receive_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3315
static int receive_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3281
{
3316
{
3282
	if (ie->len == 0)
3317
	if (ie->len == 0)
3283
		return -1;
3318
		return -1;
3284

    
   
3319

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

    
   
3322

   
3288
	return 0;
3323
	return 0;
3289
}
3324
}
3290

    
   
3325

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

    
   
3329

   
3295
	sublen = strlen(call->keypad_digits);
3330
	sublen = strlen(call->keypad_digits);
3296
	if (sublen) {
3331
	if (sublen) {
3297
		libpri_copy_string((char *) ie->data, call->keypad_digits, sizeof(call->keypad_digits));
3332
		libpri_copy_string((char *) ie->data, call->keypad_digits, sizeof(call->keypad_digits));
3298
		return sublen + 2;
3333
		return sublen + 2;
3299
	} else
3334
	} else
3300
		return 0;
3335
		return 0;
3301
}
3336
}
3302

    
   
3337

   
3303
static void dump_display(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3338
static void dump_display(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3304
{
3339
{
3305
	int x;
3340
	int x;
3306
	unsigned char buf[2*80 + 1];
3341
	unsigned char buf[2*80 + 1];
3307
	char tmp[20 + 1];
3342
	char tmp[20 + 1];
3308

    
   
3343

   
3309
	x = 0;
3344
	x = 0;
3310
	if (ie->len && (ie->data[x] & 0x80)) {
3345
	if (ie->len && (ie->data[x] & 0x80)) {
3311
		snprintf(tmp, sizeof(tmp), "Charset: %02x ", ie->data[x] & 0x7f);
3346
		snprintf(tmp, sizeof(tmp), "Charset: %02x ", ie->data[x] & 0x7f);
3312
		++x;
3347
		++x;
3313
	} else {
3348
	} else {
3314
		tmp[0] = '\0';
3349
		tmp[0] = '\0';
3315
	}
3350
	}
3316
	q931_strget(buf, sizeof(buf), &ie->data[x], ie->len - x);
3351
	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);
3352
	pri_message(ctrl,

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

    
   
3355

   
3320
#define CHECK_OVERFLOW(limit) \
3356
#define CHECK_OVERFLOW(limit) \
3321
	if (tmpptr - tmp + limit >= sizeof(tmp)) { \
3357
	if (tmpptr - tmp + limit >= sizeof(tmp)) { \
3322
		*tmpptr = '\0'; \
3358
		*tmpptr = '\0'; \
3323
		pri_message(ctrl, "%s", tmpptr = tmp); \
3359
		pri_message(ctrl, "%s", tmpptr = tmp); \
3324
	}
3360
	}
3325

    
   
3361

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

    
   
3400

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

    
   
3404
		prefix, ie2str(full_ie), len, Q931_IE_CODESET(full_ie));
3368
	dump_ie_data(ctrl, ie->data, ie->len);
3405
	dump_ie_data(ctrl, ie->data, ie->len);
3369
	pri_message(ctrl, " ]\n");
3406
	pri_message(ctrl, " ]\n");
3370
#if 0	/* Lets not dump parse of facility contents here anymore. */
3407
#if 0	/* Lets not dump parse of facility contents here anymore. */
3371
	/*
3408
	/*
3372
	 * The ASN.1 decode dump has already been done when the facility ie was added to the outgoing
3409
	 * 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
3410
	 * 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.
3411
	 * messages.  This dump is redundant and very noisy.
3375
	 */
3412
	 */
3376
	if (ie->len > 1) {
3413
	if (ie->len > 1) {
3377
		int dataat = (ie->data[0] & 0x80) ? 1 : 2;
3414
		int dataat = (ie->data[0] & 0x80) ? 1 : 2;
3378

    
   
3415

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

    
   
3421

   
3385
static void dump_network_spec_fac(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3422
static void dump_network_spec_fac(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3386
{
3423
{
3387
	pri_message(ctrl, "%c Network-Specific Facilities (len=%2d) [ ", prefix, ie->len);
3424
	pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), ie->len);
3388
	if (ie->data[0] == 0x00) {
3425
	if (ie->data[0] == 0x00) {
3389
 		pri_message(ctrl, "%s", code2str(ie->data[1], facilities, ARRAY_LEN(facilities)));
3426
		pri_message(ctrl, "%s", code2str(ie->data[1], facilities, ARRAY_LEN(facilities)));
3390
	}
3427
	} else {
3391
	else

   
3392
 		dump_ie_data(ctrl, ie->data, ie->len);
3428
		dump_ie_data(ctrl, ie->data, ie->len);

    
   
3429
	}
3393
	pri_message(ctrl, " ]\n");
3430
	pri_message(ctrl, " ]\n");
3394
}
3431
}
3395

    
   
3432

   
3396
static int receive_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3433
static int receive_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3397
{
3434
{
3398
	return 0;
3435
	return 0;
3399
}
3436
}
3400

    
   
3437

   
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)
3438
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
{
3439
{
3403
	/* We are ready to transmit single IE only */
3440
	/* We are ready to transmit single IE only */
3404
	if (order > 1)
3441
	if (order > 1)
3405
		return 0;
3442
		return 0;
3406

    
   
3443

   
3407
	if (ctrl->nsf != PRI_NSF_NONE) {
3444
	if (ctrl->nsf != PRI_NSF_NONE) {
3408
		ie->data[0] = 0x00;
3445
		ie->data[0] = 0x00;
3409
		ie->data[1] = ctrl->nsf;
3446
		ie->data[1] = ctrl->nsf;
3410
		return 4;
3447
		return 4;
3411
	}
3448
	}
3412
	/* Leave off */
3449
	/* Leave off */
3413
	return 0;
3450
	return 0;
3414
}
3451
}
3415

    
   
3452

   
3416
char *pri_cause2str(int cause)
3453
char *pri_cause2str(int cause)
3417
{
3454
{
3418
	return code2str(cause, causes, sizeof(causes) / sizeof(causes[0]));
3455
	return code2str(cause, causes, sizeof(causes) / sizeof(causes[0]));
3419
}
3456
}
3420

    
   
3457

   
3421
static char *pri_causeclass2str(int cause)
3458
static char *pri_causeclass2str(int cause)
3422
{
3459
{
3423
	static struct msgtype causeclasses[] = {
3460
	static struct msgtype causeclasses[] = {
3424
		{ 0, "Normal Event" },
3461
		{ 0, "Normal Event" },
3425
		{ 1, "Normal Event" },
3462
		{ 1, "Normal Event" },
3426
		{ 2, "Network Congestion (resource unavailable)" },
3463
		{ 2, "Network Congestion (resource unavailable)" },
3427
		{ 3, "Service or Option not Available" },
3464
		{ 3, "Service or Option not Available" },
3428
		{ 4, "Service or Option not Implemented" },
3465
		{ 4, "Service or Option not Implemented" },
3429
		{ 5, "Invalid message (e.g. parameter out of range)" },
3466
		{ 5, "Invalid message (e.g. parameter out of range)" },
3430
		{ 6, "Protocol Error (e.g. unknown message)" },
3467
		{ 6, "Protocol Error (e.g. unknown message)" },
3431
		{ 7, "Interworking" },
3468
		{ 7, "Interworking" },
3432
	};
3469
	};
3433
	return code2str(cause, causeclasses, sizeof(causeclasses) / sizeof(causeclasses[0]));
3470
	return code2str(cause, causeclasses, sizeof(causeclasses) / sizeof(causeclasses[0]));
3434
}
3471
}
3435

    
   
3472

   
3436
static void dump_cause(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3473
static void dump_cause(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3437
{
3474
{
3438
	int x;
3475
	int x;
3439
	pri_message(ctrl, "%c Cause (len=%2d) [ Ext: %d  Coding: %s (%d)  Spare: %d  Location: %s (%d)\n",
3476
	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,
3477
		prefix, ie2str(full_ie), len, ie->data[0] >> 7,

    
   
3478
		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);
3479
		(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",
3480
	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, 
3481
		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);
3482
		pri_causeclass2str((ie->data[1] & 0x7f) >> 4), (ie->data[1] & 0x7f) >> 4);
3445
	if (ie->len < 3)
3483
	if (ie->len < 3)
3446
		return;
3484
		return;
3447
	/* Dump cause data in readable form */
3485
	/* Dump cause data in readable form */
3448
	switch(ie->data[1] & 0x7f) {
3486
	switch(ie->data[1] & 0x7f) {
3449
	case PRI_CAUSE_IE_NONEXIST:
3487
	case PRI_CAUSE_IE_NONEXIST:
3450
		for (x=2;x<ie->len;x++) 
3488
		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]));
3489
			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;
3490
		break;
3453
	case PRI_CAUSE_WRONG_CALL_STATE:
3491
	case PRI_CAUSE_WRONG_CALL_STATE:
3454
		for (x=2;x<ie->len;x++) 
3492
		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]));
3493
			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;
3494
		break;
3457
	case PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE:
3495
	case PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE:
3458
		pri_message(ctrl, "%c              Cause data:", prefix);
3496
		pri_message(ctrl, "%c              Cause data:", prefix);
3459
		for (x=2;x<ie->len;x++)
3497
		for (x=2;x<ie->len;x++)
3460
			pri_message(ctrl, " %02x", ie->data[x]);
3498
			pri_message(ctrl, " %02x", ie->data[x]);
3461
		pri_message(ctrl, " (Timer T");
3499
		pri_message(ctrl, " (Timer T");
3462
		for (x=2;x<ie->len;x++)
3500
		for (x=2;x<ie->len;x++)
3463
			pri_message(ctrl, "%c", ((ie->data[x] >= ' ') && (ie->data[x] < 0x7f)) ? ie->data[x] : '.');
3501
			pri_message(ctrl, "%c", ((ie->data[x] >= ' ') && (ie->data[x] < 0x7f)) ? ie->data[x] : '.');
3464
		pri_message(ctrl, ")\n");
3502
		pri_message(ctrl, ")\n");
3465
		break;
3503
		break;
3466
	default:
3504
	default:
3467
		for (x=2;x<ie->len;x++) 
3505
		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]);
3506
			pri_message(ctrl, "%c              Cause data %d: %02x (%d)\n", prefix, x-1, ie->data[x], ie->data[x]);
3469
		break;
3507
		break;
3470
	}
3508
	}
3471
}
3509
}
3472

    
   
3510

   
3473
static int receive_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3511
static int receive_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3474
{
3512
{
3475
	call->causeloc = ie->data[0] & 0xf;
3513
	call->causeloc = ie->data[0] & 0xf;
3476
	call->causecode = (ie->data[0] & 0x60) >> 5;
3514
	call->causecode = (ie->data[0] & 0x60) >> 5;
3477
	call->cause = (ie->data[1] & 0x7f);
3515
	call->cause = (ie->data[1] & 0x7f);
3478
	return 0;
3516
	return 0;
3479
}
3517
}
3480

    
   
3518

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

    
   
3524

   
3487
	if (call->cause > 0) {
3525
	if (call->cause > 0) {
3488
		ie->data[0] = 0x80 | (call->causecode << 5)  | (call->causeloc);
3526
		ie->data[0] = 0x80 | (call->causecode << 5)  | (call->causeloc);
3489
		ie->data[1] = 0x80 | (call->cause);
3527
		ie->data[1] = 0x80 | (call->cause);
3490
		return 4;
3528
		return 4;
3491
	} else {
3529
	} else {
3492
		/* Leave off */
3530
		/* Leave off */
3493
		return 0;
3531
		return 0;
3494
	}
3532
	}
3495
}
3533
}
3496

    
   
3534

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

    
   
3539

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

    
   
3546

   
3509
static int transmit_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3547
static int transmit_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3510
{
3548
{
3511
	if ((ctrl->overlapdial && call->complete) || /* Explicit */
3549
	if ((ctrl->overlapdial && call->complete) || /* Explicit */
3512
		(!ctrl->overlapdial && ((ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) || 
3550
		(!ctrl->overlapdial && ((ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) || 
3513
		/* Implicit */   	   (ctrl->switchtype == PRI_SWITCH_EUROISDN_T1)))) {
3551
		/* Implicit */   	   (ctrl->switchtype == PRI_SWITCH_EUROISDN_T1)))) {
3514
		/* Include this single-byte IE */
3552
		/* Include this single-byte IE */
3515
		return 1;
3553
		return 1;
3516
	}
3554
	}
3517
	return 0;
3555
	return 0;
3518
}
3556
}
3519

    
   
3557

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

    
   
3587

   
3550
static void dump_notify(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3588
static void dump_notify(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3551
{
3589
{
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);
3590
	pri_message(ctrl, "%c %s (len=%2d): Ext: %d  %s (%d)\n",

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

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

    
   
3594

   
3555
static int receive_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3595
static int receive_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3556
{
3596
{
3557
	call->notify = ie->data[0] & 0x7F;
3597
	call->notify = ie->data[0] & 0x7F;
3558
	return 0;
3598
	return 0;
3559
}
3599
}
3560

    
   
3600

   
3561
static int transmit_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3601
static int transmit_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3562
{
3602
{
3563
	if (call->notify >= 0) {
3603
	if (call->notify >= 0) {
3564
		ie->data[0] = 0x80 | call->notify;
3604
		ie->data[0] = 0x80 | call->notify;
3565
		return 3;
3605
		return 3;
3566
	}
3606
	}
3567
	return 0;
3607
	return 0;
3568
}
3608
}
3569

    
   
3609

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

    
   
3613
		prefix, (full_ie & 8) ? "Non-" : "", len, full_ie & 7);
3573
}
3614
}
3574

    
   
3615

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

    
   
3647

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

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

    
   
3653

   
3612
static int receive_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3654
static int receive_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3613
{
3655
{
3614
	call->ani2 = ie->data[0];
3656
	call->ani2 = ie->data[0];
3615
	return 0;
3657
	return 0;
3616
}
3658
}
3617

    
   
3659

   
3618
static int transmit_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3660
static int transmit_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
3619
{
3661
{
3620
#if 0	/* XXX Is this IE possible for 4ESS only? XXX */
3662
#if 0	/* XXX Is this IE possible for 4ESS only? XXX */
3621
	if(ctrl->switchtype == PRI_SWITCH_ATT4ESS) {
3663
	if(ctrl->switchtype == PRI_SWITCH_ATT4ESS) {
3622
		ie->data[0] = 0;
3664
		ie->data[0] = 0;
3623
		return 3;
3665
		return 3;
3624
	}
3666
	}
3625
#endif
3667
#endif
3626
	return 0;
3668
	return 0;
3627
}
3669
}
3628

    
   
3670

   
3629

    
   
3671

   
3630
static char *gdencoding2str(int encoding)
3672
static char *gdencoding2str(int encoding)
3631
{
3673
{
3632
	static struct msgtype gdencoding[] = {
3674
	static struct msgtype gdencoding[] = {
3633
		{ 0, "BCD even" },
3675
		{ 0, "BCD even" },
3634
		{ 1, "BCD odd" },
3676
		{ 1, "BCD odd" },
3635
		{ 2, "IA5" },
3677
		{ 2, "IA5" },
3636
		{ 3, "Binary" },
3678
		{ 3, "Binary" },
3637
	};
3679
	};
3638
	return code2str(encoding, gdencoding, sizeof(gdencoding) / sizeof(gdencoding[0]));
3680
	return code2str(encoding, gdencoding, sizeof(gdencoding) / sizeof(gdencoding[0]));
3639
}
3681
}
3640

    
   
3682

   
3641
static char *gdtype2str(int type)
3683
static char *gdtype2str(int type)
3642
{
3684
{
3643
	static struct msgtype gdtype[] = {
3685
	static struct msgtype gdtype[] = {
3644
		{  0, "Account Code" },
3686
		{  0, "Account Code" },
3645
		{  1, "Auth Code" },
3687
		{  1, "Auth Code" },
3646
		{  2, "Customer ID" },
3688
		{  2, "Customer ID" },
3647
		{  3, "Universal Access" },
3689
		{  3, "Universal Access" },
3648
		{  4, "Info Digits" },
3690
		{  4, "Info Digits" },
3649
		{  5, "Callid" },
3691
		{  5, "Callid" },
3650
		{  6, "Opart" },
3692
		{  6, "Opart" },
3651
		{  7, "TCN" },
3693
		{  7, "TCN" },
3652
		{  9, "Adin" },
3694
		{  9, "Adin" },
3653
	};
3695
	};
3654
	return code2str(type, gdtype, sizeof(gdtype) / sizeof(gdtype[0]));
3696
	return code2str(type, gdtype, sizeof(gdtype) / sizeof(gdtype[0]));
3655
}
3697
}
3656

    
   
3698

   
3657
static void dump_generic_digits(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3699
static void dump_generic_digits(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3658
{
3700
{
3659
	int encoding;
3701
	int encoding;
3660
	int type;
3702
	int type;
3661
	int idx;
3703
	int idx;
3662
	int value;
3704
	int value;
3663
	if (len < 3) {
3705
	if (len < 3) {
3664
		pri_message(ctrl, "%c Generic Digits (len=%02d): Invalid length\n", prefix, len);
3706
		pri_message(ctrl, "%c %s (len=%02d): Invalid length\n",

    
   
3707
			prefix, ie2str(full_ie), len);
3665
		return;
3708
		return;
3666
	}
3709
	}
3667
	encoding = (ie->data[0] >> 5) & 7;
3710
	encoding = (ie->data[0] >> 5) & 7;
3668
	type = ie->data[0] & 0x1F;
3711
	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));
3712
	pri_message(ctrl, "%c %s (len=%02d): Encoding %s  Type %s\n",

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

    
   
3747

   
3704
static int receive_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3748
static int receive_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3705
{
3749
{
3706
	int encoding;
3750
	int encoding;
3707
	int type;
3751
	int type;
3708
	int idx;
3752
	int idx;
3709
	int value;
3753
	int value;
3710
	int num_idx;
3754
	int num_idx;
3711
	char number[260];
3755
	char number[260];
3712

    
   
3756

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

    
   
3825

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

    
   
3831

   
3788
	if(ctrl->switchtype == PRI_SWITCH_NI1) {
3832
	if(ctrl->switchtype == PRI_SWITCH_NI1) {
3789
		ie->data[0] = 0x04;	/* BCD even, Info Digits */
3833
		ie->data[0] = 0x04;	/* BCD even, Info Digits */
3790
		ie->data[1] = 0x00;	/* POTS */
3834
		ie->data[1] = 0x00;	/* POTS */
3791
		return 4;
3835
		return 4;
3792
	}
3836
	}
3793
#endif
3837
#endif
3794
	return 0;
3838
	return 0;
3795
}
3839
}
3796

    
   
3840

   
3797

    
   
3841

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

    
   
3869

   
3826

    
   
3870

   
3827
static void dump_signal(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3871
static void dump_signal(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3828
{
3872
{
3829
	pri_message(ctrl, "%c Signal (len=%02d): ", prefix, len);
3873
	pri_message(ctrl, "%c %s (len=%02d): ", prefix, ie2str(full_ie), len);
3830
	if (len < 3) {
3874
	if (len < 3) {
3831
		pri_message(ctrl, "Invalid length\n");
3875
		pri_message(ctrl, "Invalid length\n");
3832
		return;
3876
		return;
3833
	}
3877
	}
3834
	pri_message(ctrl, "Signal %s (%d)\n", signal2str(ie->data[0]), ie->data[0]);
3878
	pri_message(ctrl, "Signal %s (%d)\n", signal2str(ie->data[0]), ie->data[0]);
3835
}
3879
}
3836

    
   
3880

   
3837
static void dump_transit_count(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3881
static void dump_transit_count(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
3838
{
3882
{
3839
	/* Defined in ECMA-225 */
3883
	/* Defined in ECMA-225 */
3840
	pri_message(ctrl, "%c Transit Count (len=%02d): ", prefix, len);
3884
	pri_message(ctrl, "%c %s (len=%02d): ", prefix, ie2str(full_ie), len);
3841
	if (len < 3) {
3885
	if (len < 3) {
3842
		pri_message(ctrl, "Invalid length\n");
3886
		pri_message(ctrl, "Invalid length\n");
3843
		return;
3887
		return;
3844
	}
3888
	}
3845
	pri_message(ctrl, "Count=%d (0x%02x)\n", ie->data[0] & 0x1f, ie->data[0] & 0x1f);
3889
	pri_message(ctrl, "Count=%d (0x%02x)\n", ie->data[0] & 0x1f, ie->data[0] & 0x1f);
3846
}
3890
}
3847

    
   
3891

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

    
   
3896

   
3853
static int receive_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3897
static int receive_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
3854
{
3898
{
3855
	call->reversecharge = ie->data[0] & 0x7;
3899
	call->reversecharge = ie->data[0] & 0x7;
3856
	return 0;
3900
	return 0;
3857
}
3901
}
3858

    
   
3902

   
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)
3903
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
{
3904
{
3861
	if (call->reversecharge != PRI_REVERSECHARGE_NONE) {
3905
	if (call->reversecharge != PRI_REVERSECHARGE_NONE) {
3862
		ie->data[0] = 0x80 | (call->reversecharge & 0x7);
3906