Cleanup: Make interface name be part of Port class
[lcr.git] / appbridge.cpp
index 50808ca..1202fc9 100644 (file)
@@ -78,55 +78,6 @@ void EndpointAppBridge::trace_header(const char *name, int direction)
                    msgtext);
 }
 
-/* hunts for the given interface
- * it does not need to have an mISDNport instance */
-struct interface *EndpointAppBridge::hunt_interface(char *ifname)
-{
-       struct interface *interface;
-       int there_is_an_external = 0;
-
-       interface = interface_first;
-
-       /* first find the given interface or, if not given, one with no extension */
-       checknext:
-       if (!interface) {
-               if (!there_is_an_external && !(ifname && ifname[0])) {
-                       trace_header("CHANNEL SELECTION (no external interface specified)", DIRECTION_NONE);
-                       add_trace("info", NULL, "Add 'extern' parameter to interface.conf.");
-                       end_trace();
-               }
-               return(NULL);
-       }
-
-       /* check for given interface */
-       if (ifname && ifname[0]) {
-               if (!strcasecmp(interface->name, ifname)) {
-                       /* found explicit interface */
-                       trace_header("CHANNEL SELECTION (found given interface)", DIRECTION_NONE);
-                       add_trace("interface", NULL, "%s", ifname);
-                       end_trace();
-                       goto foundif;
-               }
-
-       } else {
-               if (interface->external) {
-                       there_is_an_external = 1;
-                       /* found non extension */
-                       trace_header("CHANNEL SELECTION (found external interface)", DIRECTION_NONE);
-                       add_trace("interface", NULL, "%s", interface->name);
-                       end_trace();
-                       goto foundif;
-               }
-       }
-
-       interface = interface->next;
-       goto checknext;
-foundif:
-
-       return interface;
-}
-
-
 /* port MESSAGE_SETUP */
 void EndpointAppBridge::port_setup(struct port_list *portlist, int message_type, union parameter *param)
 {
@@ -138,6 +89,7 @@ void EndpointAppBridge::port_setup(struct port_list *portlist, int message_type,
        unsigned int bridge_id;
        unsigned int source_port_id = portlist->port_id;
        char portname[64];
+       int cause = 47;
 
        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);
 
@@ -156,12 +108,13 @@ void EndpointAppBridge::port_setup(struct port_list *portlist, int message_type,
                interface_in = interface_in->next;
        }
        if (!interface_in) {
-fail:
                PERROR("Cannot find source interface %s.\n", param->setup.callerinfo.interface);
+fail:
                message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_RELEASE);
-               message->param.disconnectinfo.cause = 47;
+               message->param.disconnectinfo.cause = cause;
                message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
                message_put(message);
+               ea_endpoint->free_portlist(portlist);
 
                /* destroy endpoint */
                ea_endpoint->ep_use = 0;
@@ -183,21 +136,73 @@ fail:
        /* create port for interface */
        SPRINT(portname, "%s-%d-out", interface_out->name, 0);
        memset(&port_settings, 0, sizeof(port_settings));
+#ifdef WITH_MISDN
+       if (interface_out->remote) {
+               struct admin_list       *admin;
+               admin = admin_first;
+               while(admin) {
+                       if (admin->remote_name[0] && !strcmp(admin->remote_name, interface_out->remote_app))
+                               break;
+                       admin = admin->next;
+               }
+               if (!admin) {
+                       trace_header("INTERFACE (remote not connected)", DIRECTION_NONE);
+                       add_trace("application", NULL, "%s", interface_out->remote_app);
+                       end_trace();
+                       goto fail;
+               }
+               port = new Premote(PORT_TYPE_REMOTE_OUT, portname, &port_settings, interface_out, admin->sock);
+       } else
+#endif
 #ifdef WITH_SIP
        if (interface_out->sip) {
                port = new Psip(PORT_TYPE_SIP_OUT, portname, &port_settings, interface_out);
-       }
+       } else
 #endif
 #ifdef WITH_GSM_BS
        if (interface_out->gsm_bs) {
                port = new Pgsm_bs(PORT_TYPE_GSM_BS_OUT, portname, &port_settings, interface_out);
-       }
+       } else
 #endif
 #ifdef WITH_GSM_MS
        if (interface_out->gsm_ms) {
-               port = new Pgsm_bs(PORT_TYPE_GSM_MS_OUT, portname, &port_settings, interface_out);
-       }
+               port = new Pgsm_ms(PORT_TYPE_GSM_MS_OUT, portname, &port_settings, interface_out);
+       } else
+#endif
+       {
+               char *ifname = interface_out->name;
+#ifdef WITH_MISDN
+               struct mISDNport *mISDNport;
+               int channel = 0;
+               int earlyb;
+               int mode = B_MODE_TRANSPARENT;
+
+               /* hunt for mISDNport and create Port */
+               mISDNport = hunt_port(ifname, &channel);
+               if (!mISDNport) {
+                       trace_header("INTERFACE (busy)", DIRECTION_NONE);
+                       add_trace("interface", NULL, "%s", ifname);
+                       end_trace();
+                       cause = 33;
+                       goto fail;
+               }
+
+               SPRINT(portname, "%s-%d-out", mISDNport->ifport->interface->name, mISDNport->portnum);
+#ifdef WITH_SS5
+               if (mISDNport->ss5)
+                       port = ss5_hunt_line(mISDNport);
+               else
 #endif
+               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);
+               earlyb = mISDNport->earlyb;
+#else
+               trace_header("INTERFACE (has no function)", DIRECTION_NONE);
+               add_trace("interface", NULL, "%s", ifname);
+               end_trace();
+               cause = 31;
+               goto fail;
+#endif
+       }
        if (!port)
                FATAL("Remote interface, but not supported???\n");
        portlist = ea_endpoint->portlist_new(port->p_serial, port->p_type, interface_out->is_earlyb == IS_YES);
@@ -220,18 +225,22 @@ fail:
 /* port MESSAGE_RELEASE */
 void EndpointAppBridge::port_release(struct port_list *portlist, int message_type, union parameter *param)
 {
-       unsigned int remote;
+       struct port_list *remote;
 
        PDEBUG(DEBUG_EPOINT, "EPOINT(%d) epoint received release from port\n");
 
        if (!ea_endpoint->ep_portlist || !ea_endpoint->ep_portlist->next)
                goto out;
        if (ea_endpoint->ep_portlist->port_id == portlist->port_id)
-               remote = ea_endpoint->ep_portlist->next->port_id;
+               remote = ea_endpoint->ep_portlist->next;
        else
-               remote = ea_endpoint->ep_portlist->port_id;
+               remote = ea_endpoint->ep_portlist;
        /* forward release */
-       message_forward(ea_endpoint->ep_serial, remote, EPOINT_TO_PORT, param);  
+       message_forward(ea_endpoint->ep_serial, remote->port_id, EPOINT_TO_PORT, param);  
+
+       /* remove relations to in and out port */
+       ea_endpoint->free_portlist(portlist);
+       ea_endpoint->free_portlist(remote);
 
 out:
        /* destroy endpoint */