Minor fix for GSM HR codec negotiation: Add missing 'break'.
[lcr.git] / appbridge.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** The EndpointAppBridge implements direct bridge between interfaces         **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12
13 #include "main.h"
14
15 class EndpointAppBridge *appbridge_first = NULL;
16
17 /*
18  * EndpointAppBridge constructor
19  */
20 EndpointAppBridge::EndpointAppBridge(class Endpoint *epoint, int origin) : EndpointApp(epoint, origin, EAPP_TYPE_BRIDGE)
21 {
22         class EndpointAppBridge **apppointer;
23
24         /* add application to chain */
25         next = NULL;
26         apppointer = &appbridge_first;
27         while(*apppointer)
28                 apppointer = &((*apppointer)->next);
29         *apppointer = this;
30
31         PDEBUG(DEBUG_EPOINT, "Bridge endpoint created\n");
32 }
33
34 /*
35  * EpointAppBridge destructor
36  */
37 EndpointAppBridge::~EndpointAppBridge(void)
38 {
39         class EndpointAppBridge *temp, **tempp;
40
41         /* detach */
42         temp =appbridge_first;
43         tempp = &appbridge_first;
44         while(temp) {
45                 if (temp == this)
46                         break;
47
48                 tempp = &temp->next;
49                 temp = temp->next;
50         }
51         if (temp == 0)
52                 FATAL("Endpoint not in endpoint's list.\n");
53         *tempp = next;
54
55         PDEBUG(DEBUG_EPOINT, "Bridge endpoint destroyed\n");
56 }
57
58
59 /*
60  * trace header for application
61  */
62 void EndpointAppBridge::trace_header(const char *name, int direction)
63 {
64         struct trace _trace;
65
66         char msgtext[sizeof(_trace.name)];
67
68         SCPY(msgtext, name);
69
70         /* init trace with given values */
71         start_trace(-1,
72                     NULL,
73                     "", //numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international),
74                     "", // e_dialinginfo.id,
75                     direction,
76                     CATEGORY_EP,
77                     ea_endpoint->ep_serial,
78                     msgtext);
79 }
80
81 /* port MESSAGE_SETUP */
82 void EndpointAppBridge::port_setup(struct port_list *portlist, int message_type, union parameter *param)
83 {
84         struct interface *interface_in = interface_first;
85         struct interface *interface_out = interface_first;
86         struct port_settings    port_settings;
87         class Port              *port = NULL;
88         struct lcr_msg *message;
89         unsigned int bridge_id;
90         unsigned int source_port_id = portlist->port_id;
91         char portname[64];
92         int cause = 47;
93
94         PDEBUG(DEBUG_EPOINT, "EPOINT(%d) epoint received setup from='%s' to='%s'\n", ea_endpoint->ep_serial, param->setup.callerinfo.id, param->setup.dialinginfo.id);
95
96         if (!ea_endpoint->ep_portlist) {
97                 PERROR("Endpoint has no port in portlist\n");
98                 return;
99         }
100         if (ea_endpoint->ep_portlist->next) {
101                 PDEBUG(DEBUG_EPOINT, "Endpoint already received setup, ignoring.\n");
102                 return;
103         }
104
105         while (interface_in) {
106                 if (!strcmp(interface_in->name, param->setup.callerinfo.interface))
107                         break;
108                 interface_in = interface_in->next;
109         }
110         if (!interface_in) {
111                 PERROR("Cannot find source interface %s.\n", param->setup.callerinfo.interface);
112 fail:
113                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_RELEASE);
114                 message->param.disconnectinfo.cause = cause;
115                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
116                 message_put(message);
117                 ea_endpoint->free_portlist(portlist);
118
119                 /* destroy endpoint */
120                 ea_endpoint->ep_use = 0;
121                 trigger_work(&ea_endpoint->ep_delete);
122                 return;
123         }
124
125         while (interface_out) {
126                 if (!strcmp(interface_out->name, interface_in->bridge_if))
127                         break;
128                 interface_out = interface_out->next;
129         }
130         if (!interface_out) {
131                 PERROR("Cannot find destination interface %s.\n", interface_in->bridge_if);
132                 goto fail;
133                 return;
134         }
135
136         /* create port for interface */
137         SPRINT(portname, "%s-%d-out", interface_out->name, 0);
138         memset(&port_settings, 0, sizeof(port_settings));
139         if (interface_out->remote) {
140                 struct admin_list       *admin;
141                 admin = admin_first;
142                 while(admin) {
143                         if (admin->remote_name[0] && !strcmp(admin->remote_name, interface_out->remote_app))
144                                 break;
145                         admin = admin->next;
146                 }
147                 if (!admin) {
148                         trace_header("INTERFACE (remote not connected)", DIRECTION_NONE);
149                         add_trace("application", NULL, "%s", interface_out->remote_app);
150                         end_trace();
151                         goto fail;
152                 }
153                 port = new Premote(PORT_TYPE_REMOTE_OUT, portname, &port_settings, interface_out, admin->sock);
154         } else
155 #ifdef WITH_SIP
156         if (interface_out->sip) {
157                 port = new Psip(PORT_TYPE_SIP_OUT, portname, &port_settings, interface_out);
158         } else
159 #endif
160 #ifdef WITH_GSM_BS
161         if (interface_out->gsm_bs) {
162                 port = new Pgsm_bs(PORT_TYPE_GSM_BS_OUT, portname, &port_settings, interface_out);
163         } else
164 #endif
165 #ifdef WITH_GSM_MS
166         if (interface_out->gsm_ms) {
167                 port = new Pgsm_ms(PORT_TYPE_GSM_MS_OUT, portname, &port_settings, interface_out);
168         } else
169 #endif
170         {
171                 char *ifname = interface_out->name;
172 #ifdef WITH_MISDN
173                 struct mISDNport *mISDNport;
174                 int channel = 0;
175                 int mode = B_MODE_TRANSPARENT;
176
177                 /* hunt for mISDNport and create Port */
178                 mISDNport = hunt_port(ifname, &channel);
179                 if (!mISDNport) {
180                         trace_header("INTERFACE (busy)", DIRECTION_NONE);
181                         add_trace("interface", NULL, "%s", ifname);
182                         end_trace();
183                         cause = 33;
184                         goto fail;
185                 }
186
187                 SPRINT(portname, "%s-%d-out", mISDNport->ifport->interface->name, mISDNport->portnum);
188 #ifdef WITH_SS5
189                 if (mISDNport->ss5)
190                         port = ss5_hunt_line(mISDNport);
191                 else
192 #endif
193 #ifdef ISDN_P_FXS_POTS
194                 if (mISDNport->pots)
195                         port = new Pfxs(PORT_TYPE_POTS_FXS_OUT, mISDNport, portname, &port_settings, mISDNport->ifport->interface, mode);
196                 else
197 #endif
198                         port = new Pdss1((mISDNport->ntmode)?PORT_TYPE_DSS1_NT_OUT:PORT_TYPE_DSS1_TE_OUT, mISDNport, portname, &port_settings, mISDNport->ifport->interface, channel, mISDNport->ifport->channel_force, mode);
199 #else
200                 trace_header("INTERFACE (has no function)", DIRECTION_NONE);
201                 add_trace("interface", NULL, "%s", ifname);
202                 end_trace();
203                 cause = 31;
204                 goto fail;
205 #endif
206         }
207         if (!port)
208                 FATAL("Remote interface, but not supported???\n");
209         portlist = ea_endpoint->portlist_new(port->p_serial, port->p_type, interface_out->is_earlyb == IS_YES);
210         if (!portlist)
211                 FATAL("EPOINT(%d) cannot allocate port_list relation\n", ea_endpoint->ep_serial);
212         /* forward setup */
213         message_forward(ea_endpoint->ep_serial, port->p_serial, EPOINT_TO_PORT, param);  
214
215         /* apply bridge to interfaces */
216         /* FIXME: use mISDN bridge for mISDN ports */
217         bridge_id = join_serial++;
218         message = message_create(ea_endpoint->ep_serial, source_port_id, EPOINT_TO_PORT, MESSAGE_BRIDGE);
219         message->param.bridge_id = bridge_id;
220         message_put(message);
221         message = message_create(ea_endpoint->ep_serial, port->p_serial, EPOINT_TO_PORT, MESSAGE_BRIDGE);
222         message->param.bridge_id = bridge_id;
223         message_put(message);
224 }
225
226 /* port MESSAGE_RELEASE */
227 void EndpointAppBridge::port_release(struct port_list *portlist, int message_type, union parameter *param)
228 {
229         struct port_list *remote;
230
231         PDEBUG(DEBUG_EPOINT, "EPOINT(%d) epoint received release from port\n");
232
233         if (!ea_endpoint->ep_portlist || !ea_endpoint->ep_portlist->next)
234                 goto out;
235         if (ea_endpoint->ep_portlist->port_id == portlist->port_id)
236                 remote = ea_endpoint->ep_portlist->next;
237         else
238                 remote = ea_endpoint->ep_portlist;
239         /* forward release */
240         message_forward(ea_endpoint->ep_serial, remote->port_id, EPOINT_TO_PORT, param);  
241
242         /* remove relations to in and out port */
243         ea_endpoint->free_portlist(portlist);
244         ea_endpoint->free_portlist(remote);
245
246 out:
247         /* destroy endpoint */
248         ea_endpoint->ep_use = 0;
249         trigger_work(&ea_endpoint->ep_delete);
250 }
251
252 /* port other messages */
253 void EndpointAppBridge::port_other(struct port_list *portlist, int message_type, union parameter *param)
254 {
255         unsigned int remote;
256
257         PDEBUG(DEBUG_EPOINT, "EPOINT(%d) epoint received message %d from port\n", message_type);
258
259         if (!ea_endpoint->ep_portlist || !ea_endpoint->ep_portlist->next)
260                 return;
261         if (ea_endpoint->ep_portlist->port_id == portlist->port_id)
262                 remote = ea_endpoint->ep_portlist->next->port_id;
263         else
264                 remote = ea_endpoint->ep_portlist->port_id;
265         /* forward release */
266         message_forward(ea_endpoint->ep_serial, remote, EPOINT_TO_PORT, param);  
267 }
268
269 /* port sends message to the endpoint
270  */
271 void EndpointAppBridge::ea_message_port(unsigned int port_id, int message_type, union parameter *param)
272 {
273         struct port_list *portlist;
274
275         portlist = ea_endpoint->ep_portlist;
276         while(portlist) {
277                 if (port_id == portlist->port_id)
278                         break;
279                 portlist = portlist->next;
280         }
281         if (!portlist) {
282                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d) warning: port is not related to this endpoint. This may happen, if port has been released after the message was created.\n", ea_endpoint->ep_serial);
283                 return;
284         }
285
286 //      PDEBUG(DEBUG_EPOINT, "received message %d (terminal %s, caller id %s)\n", message, e_ext.number, e_callerinfo.id);
287         switch(message_type) {
288                 /* PORT sends SETUP message */
289                 case MESSAGE_SETUP:
290                 port_setup(portlist, message_type, param);
291                 break;
292
293                 /* PORT sends RELEASE message */
294                 case MESSAGE_RELEASE:
295                 port_release(portlist, message_type, param);
296                 break;
297
298                 default:
299                 port_other(portlist, message_type, param);
300         }
301
302         /* Note: this endpoint may be destroyed, so we MUST return */
303 }
304