-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathremote.cpp
More file actions
269 lines (225 loc) · 8.22 KB
/
remote.cpp
File metadata and controls
269 lines (225 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*****************************************************************************\
** **
** LCR **
** **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg **
** **
** mISDN remote **
** **
\*****************************************************************************/
#include "main.h"
unsigned int new_remote = 1000;
/*
* constructor
*/
Premote::Premote(int type, char *portname, struct port_settings *settings, struct interface *interface, int remote_id) : Port(type, portname, settings, interface)
{
union parameter param;
p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
p_r_ref = new_remote++;
SCPY(p_r_remote_app, interface->remote_app);
p_r_tones = (interface->is_tones == IS_YES);
p_r_earlyb = (interface->is_earlyb == IS_YES);
/* send new ref to remote socket */
memset(¶m, 0, sizeof(union parameter));
if (type == PORT_TYPE_REMOTE_OUT)
param.newref.direction = 1; /* new ref from lcr */
p_r_remote_id = remote_id;
if (admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_NEWREF, ¶m) < 0)
FATAL("No socket with remote application '%s' found, this shall not happen. because we already created one.\n", p_r_remote_app);
PDEBUG(DEBUG_PORT, "Created new RemotePort(%s).\n", portname);
}
/*
* destructor
*/
Premote::~Premote()
{
PDEBUG(DEBUG_PORT, "Destroyed Remote process(%s).\n", p_name);
}
/*
* endpoint sends messages to the port
*/
int Premote::message_epoint(unsigned int epoint_id, int message_type, union parameter *param)
{
struct epoint_list *epointlist;
if (Port::message_epoint(epoint_id, message_type, param))
return 1;
switch (message_type) {
case MESSAGE_SETUP:
struct interface *interface;
interface = getinterfacebyname(p_interface_name);
if (!interface) {
PERROR("Cannot find interface %s.\n", p_interface_name);
return 0;
}
/* attach only if not already */
epointlist = p_epointlist;
while(epointlist) {
if (epointlist->epoint_id == epoint_id)
break;
epointlist = epointlist->next;
}
if (!epointlist)
epointlist_new(epoint_id);
/* set context to pbx */
if (!param->setup.dialinginfo.context[0]) {
if (interface->remote_context[0])
SCPY(param->setup.dialinginfo.context, interface->remote_context);
else
SCPY(param->setup.dialinginfo.context, "lcr");
}
memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
/* screen */
do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_interface_name);
do_screen(1, p_callerinfo.id2, sizeof(p_callerinfo.id2), &p_callerinfo.ntype2, &p_callerinfo.present2, p_interface_name);
do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, p_interface_name);
memcpy(¶m->setup.callerinfo, &p_callerinfo, sizeof(p_callerinfo));
memcpy(¶m->setup.redirinfo, &p_redirinfo, sizeof(p_redirinfo));
new_state(PORT_STATE_OUT_SETUP);
break;
case MESSAGE_PROCEEDING:
new_state(PORT_STATE_IN_PROCEEDING);
break;
case MESSAGE_ALERTING:
new_state(PORT_STATE_IN_ALERTING);
break;
case MESSAGE_CONNECT:
memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
new_state(PORT_STATE_CONNECT);
break;
case MESSAGE_DISCONNECT:
new_state(PORT_STATE_OUT_DISCONNECT);
break;
case MESSAGE_RELEASE:
new_state(PORT_STATE_RELEASE);
break;
}
/* look for Remote's interface */
if (admin_message_from_lcr(p_r_remote_id, p_r_ref, message_type, param)<0) {
PERROR("No socket with remote application '%s' found, this shall not happen. Closing socket shall cause release of all remote ports.\n", p_r_remote_app);
return 0;
}
if (message_type == MESSAGE_RELEASE) {
new_state(PORT_STATE_RELEASE);
delete this;
return 0;
}
return 0;
}
void Premote::message_remote(int message_type, union parameter *param)
{
class Endpoint *epoint;
struct lcr_msg *message;
struct interface *interface;
switch (message_type) {
case MESSAGE_TRAFFIC:
/* record audio */
if (p_record)
record(param->traffic.data, param->traffic.len, 0); // from down
if (p_tap)
tap(param->traffic.data, param->traffic.len, 0); // from down
bridge_tx(param->traffic.data, param->traffic.len);
if (p_tone_name[0]) {
read_audio(param->traffic.data, param->traffic.len);
/* record audio */
if (p_record)
record(param->traffic.data, param->traffic.len, 1); // from up
if (p_tap)
tap(param->traffic.data, param->traffic.len, 1); // from up
admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_TRAFFIC, param);
}
return;
case MESSAGE_SETUP:
interface = getinterfacebyname(p_interface_name);
if (!interface) {
PERROR("Cannot find interface %s.\n", p_interface_name);
return;
}
/* enable audio path */
if (interface->is_tones == IS_YES) {
union parameter newparam;
memset(&newparam, 0, sizeof(union parameter));
admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_PATTERN, &newparam);
newparam.audiopath = 1;
admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_AUDIOPATH, &newparam);
}
/* set source interface */
param->setup.callerinfo.itype = p_callerinfo.itype;
SCPY(param->setup.callerinfo.interface, interface->name);
/* create endpoint */
if (p_epointlist)
FATAL("Incoming call but already got an endpoint.\n");
if (!(epoint = new Endpoint(p_serial, 0)))
FATAL("No memory for Endpoint instance\n");
epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
epointlist_new(epoint->ep_serial);
memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
memcpy(&p_capainfo, ¶m->setup.capainfo, sizeof(p_capainfo));
memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
new_state(PORT_STATE_IN_SETUP);
break;
case MESSAGE_PROCEEDING:
new_state(PORT_STATE_OUT_PROCEEDING);
break;
case MESSAGE_ALERTING:
new_state(PORT_STATE_OUT_ALERTING);
break;
case MESSAGE_CONNECT:
memcpy(&p_connectinfo, ¶m->connectinfo, sizeof(p_connectinfo));
new_state(PORT_STATE_CONNECT);
break;
case MESSAGE_DISCONNECT:
new_state(PORT_STATE_IN_DISCONNECT);
break;
case MESSAGE_RELEASE:
new_state(PORT_STATE_RELEASE);
break;
}
/* cannot just forward, because param is not of container "struct lcr_msg" */
message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, message_type);
memcpy(&message->param, param, sizeof(message->param));
message_put(message);
if (message_type == MESSAGE_RELEASE) {
new_state(PORT_STATE_RELEASE);
delete this;
return;
}
}
/* receive from remote Port instance */
int Premote::bridge_rx(unsigned char *data, int len)
{
union parameter newparam;
int l;
/* send tones, if connected, or if early audio is enabled in proceeding/alerting state */
if (p_state != PORT_STATE_CONNECT
&& !(p_r_earlyb
&& (p_state == PORT_STATE_OUT_PROCEEDING
|| p_state == PORT_STATE_OUT_ALERTING))
&& !(p_r_tones
&& (p_state == PORT_STATE_IN_PROCEEDING
|| p_state == PORT_STATE_IN_ALERTING)))
return 0;
if (p_tone_name[0])
return 0;
memset(&newparam, 0, sizeof(union parameter));
/* split, if exeeds data size */
while(len) {
l = (len > (int)sizeof(newparam.traffic.data)) ? sizeof(newparam.traffic.data) : len;
newparam.traffic.len = l;
len -= l;
memcpy(newparam.traffic.data, data, l);
data += l;
/* record audio */
if (p_record)
record(data, len, 1); // from up
if (p_tap)
tap(data, len, 1); // from up
admin_message_from_lcr(p_r_remote_id, p_r_ref, MESSAGE_TRAFFIC, &newparam);
}
return 0;
}