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