Allow to define MS side GSM interface again
[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 #ifdef WITH_SIP
140         if (interface_out->sip) {
141                 port = new Psip(PORT_TYPE_SIP_OUT, portname, &port_settings, interface_out);
142         } else
143 #endif
144 #ifdef WITH_GSM_BS
145         if (interface_out->gsm_bs) {
146                 port = new Pgsm_bs(PORT_TYPE_GSM_BS_OUT, portname, &port_settings, interface_out);
147         } else
148 #endif
149 #ifdef WITH_GSM_MS
150         if (interface_out->gsm_ms) {
151                 port = new Pgsm_bs(PORT_TYPE_GSM_MS_OUT, portname, &port_settings, interface_out);
152         } else
153 #endif
154         {
155                 char *ifname = interface_out->name;
156 #ifdef WITH_MISDN
157                 struct mISDNport *mISDNport;
158                 int channel = 0;
159                 struct admin_list *admin;
160                 int earlyb;
161                 int mode = B_MODE_TRANSPARENT;
162
163                 /* hunt for mISDNport and create Port */
164                 mISDNport = hunt_port(ifname, &channel);
165                 if (!mISDNport) {
166                         trace_header("INTERFACE (busy)", DIRECTION_NONE);
167                         add_trace("interface", NULL, "%s", ifname);
168                         end_trace();
169                         cause = 33;
170                         goto fail;
171                 }
172
173                 SPRINT(portname, "%s-%d-out", mISDNport->ifport->interface->name, mISDNport->portnum);
174 #ifdef WITH_SS5
175                 if (mISDNport->ss5)
176                         port = ss5_hunt_line(mISDNport);
177                 else
178 #endif
179                 if (mISDNport->ifport->remote) {
180                         admin = admin_first;
181                         while(admin) {
182                                 if (admin->remote_name[0] && !strcmp(admin->remote_name, mISDNport->ifport->remote_app))
183                                         break;
184                                 admin = admin->next;
185                         }
186                         if (!admin) {
187                                 trace_header("INTERFACE (remote not connected)", DIRECTION_NONE);
188                                 add_trace("application", NULL, "%s", mISDNport->ifport->remote_app);
189                                 end_trace();
190                                 cause = 27;
191                                 goto fail;
192                         }
193                         port = new Premote(PORT_TYPE_REMOTE_OUT, mISDNport, portname, &port_settings, channel, mISDNport->ifport->channel_force, mode, admin->sock);
194                 } else
195                         port = new Pdss1((mISDNport->ntmode)?PORT_TYPE_DSS1_NT_OUT:PORT_TYPE_DSS1_TE_OUT, mISDNport, portname, &port_settings, channel, mISDNport->ifport->channel_force, mode);
196                 earlyb = mISDNport->earlyb;
197 #else
198                 trace_header("INTERFACE (has no function)", DIRECTION_NONE);
199                 add_trace("interface", NULL, "%s", ifname);
200                 end_trace();
201                 cause = 31;
202                 goto fail;
203 #endif
204         }
205         if (!port)
206                 FATAL("Remote interface, but not supported???\n");
207         portlist = ea_endpoint->portlist_new(port->p_serial, port->p_type, interface_out->is_earlyb == IS_YES);
208         if (!portlist)
209                 FATAL("EPOINT(%d) cannot allocate port_list relation\n", ea_endpoint->ep_serial);
210         /* forward setup */
211         message_forward(ea_endpoint->ep_serial, port->p_serial, EPOINT_TO_PORT, param);  
212
213         /* apply bridge to interfaces */
214         /* FIXME: use mISDN bridge for mISDN ports */
215         bridge_id = join_serial++;
216         message = message_create(ea_endpoint->ep_serial, source_port_id, EPOINT_TO_PORT, MESSAGE_BRIDGE);
217         message->param.bridge_id = bridge_id;
218         message_put(message);
219         message = message_create(ea_endpoint->ep_serial, port->p_serial, EPOINT_TO_PORT, MESSAGE_BRIDGE);
220         message->param.bridge_id = bridge_id;
221         message_put(message);
222 }
223
224 /* port MESSAGE_RELEASE */
225 void EndpointAppBridge::port_release(struct port_list *portlist, int message_type, union parameter *param)
226 {
227         struct port_list *remote;
228
229         PDEBUG(DEBUG_EPOINT, "EPOINT(%d) epoint received release from port\n");
230
231         if (!ea_endpoint->ep_portlist || !ea_endpoint->ep_portlist->next)
232                 goto out;
233         if (ea_endpoint->ep_portlist->port_id == portlist->port_id)
234                 remote = ea_endpoint->ep_portlist->next;
235         else
236                 remote = ea_endpoint->ep_portlist;
237         /* forward release */
238         message_forward(ea_endpoint->ep_serial, remote->port_id, EPOINT_TO_PORT, param);  
239
240         /* remove relations to in and out port */
241         ea_endpoint->free_portlist(portlist);
242         ea_endpoint->free_portlist(remote);
243
244 out:
245         /* destroy endpoint */
246         ea_endpoint->ep_use = 0;
247         trigger_work(&ea_endpoint->ep_delete);
248 }
249
250 /* port other messages */
251 void EndpointAppBridge::port_other(struct port_list *portlist, int message_type, union parameter *param)
252 {
253         unsigned int remote;
254
255         PDEBUG(DEBUG_EPOINT, "EPOINT(%d) epoint received message %d from port\n", message_type);
256
257         if (!ea_endpoint->ep_portlist || !ea_endpoint->ep_portlist->next)
258                 return;
259         if (ea_endpoint->ep_portlist->port_id == portlist->port_id)
260                 remote = ea_endpoint->ep_portlist->next->port_id;
261         else
262                 remote = ea_endpoint->ep_portlist->port_id;
263         /* forward release */
264         message_forward(ea_endpoint->ep_serial, remote, EPOINT_TO_PORT, param);  
265 }
266
267 /* port sends message to the endpoint
268  */
269 void EndpointAppBridge::ea_message_port(unsigned int port_id, int message_type, union parameter *param)
270 {
271         struct port_list *portlist;
272
273         portlist = ea_endpoint->ep_portlist;
274         while(portlist) {
275                 if (port_id == portlist->port_id)
276                         break;
277                 portlist = portlist->next;
278         }
279         if (!portlist) {
280                 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);
281                 return;
282         }
283
284 //      PDEBUG(DEBUG_EPOINT, "received message %d (terminal %s, caller id %s)\n", message, e_ext.number, e_callerinfo.id);
285         switch(message_type) {
286                 /* PORT sends SETUP message */
287                 case MESSAGE_SETUP:
288                 port_setup(portlist, message_type, param);
289                 break;
290
291                 /* PORT sends RELEASE message */
292                 case MESSAGE_RELEASE:
293                 port_release(portlist, message_type, param);
294                 break;
295
296                 default:
297                 port_other(portlist, message_type, param);
298         }
299
300         /* Note: this endpoint may be destroyed, so we MUST return */
301 }
302