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