socket api work
[lcr.git] / mISDN.cpp
index 63b2c2a..75cdadf 100644 (file)
--- a/mISDN.cpp
+++ b/mISDN.cpp
@@ -9,7 +9,6 @@
 **                                                                           **
 \*****************************************************************************/ 
 
-
 #include "main.h"
 #include <poll.h>
 #include <errno.h>
 #include <mlayer3.h>
 #else
 extern "C" {
-#include <net_l2.h>
+#include <mISDNuser/net_l2.h>
 }
 #endif
 
+#ifndef CMX_TXDATA_ON
+#define OLD_MISDN
+#endif
+#ifndef CMX_TXDATA_OFF
+#define OLD_MISDN
+#endif
+#ifndef CMX_DELAY
+#define OLD_MISDN
+#endif
+#ifndef PIPELINE_CFG
+#define OLD_MISDN
+#endif
+#ifndef CMX_TX_DATA
+#define OLD_MISDN
+#endif
+
+#ifdef OLD_MISDN
+#warning
+#warning *********************************************************
+#warning *
+#warning * It seems that you use an older version of mISDN.
+#warning * Features like voice recording or echo will not work.
+#warning * Also it causes problems with loading modules and may
+#warning * not work at all.
+#warning *
+#warning * Please upgrade to newer version. A working version can
+#warning * be found at www.linux-call-router.de.
+#warning *
+#warning * Do not use the mISDN_1_1 branch, it does not have all
+#warning * the features that are required. Use the master branch
+#warning * instead.
+#warning *
+#warning *********************************************************
+#warning
+#error
+#endif
+
 #ifndef ISDN_PID_L4_B_USER
 #define ISDN_PID_L4_B_USER 0x440000ff
 #endif
 
+// timeouts if activating/deactivating response from mISDN got lost
+#define B_TIMER_ACTIVATING 1
+#define B_TIMER_DEACTIVATING 1
+
 /* list of mISDN ports */
 struct mISDNport *mISDNport_first;
 
@@ -56,18 +96,19 @@ int mISDN_initialize(void)
                return(-1);
        }
 
-       /* initialize stuff of the NT lib */
+       /* open debug, if enabled and not only stack debugging */
+       if (options.deb && (options.deb != DEBUG_STACK))
+       {
+               SPRINT(debug_log, "%s/debug.log", INSTALL_DATA);
+               debug_fp = fopen(debug_log, "a");
+       }
+
        if (options.deb & DEBUG_STACK)
        {
-               global_debug = 0xffffffff & ~DBGM_MSG;
-//             global_debug = DBGM_L3DATA;
+               SPRINT(debug_stack, "%s/debug_mISDN.log", INSTALL_DATA);
+               mISDN_debug_init(DBGM_ALL, debug_stack, debug_stack, debug_stack);
        } else
-               global_debug = DBGM_MAN;
-       SPRINT(debug_log, "%s/debug.log", INSTALL_DATA);
-       if (options.deb & DEBUG_LOG)
-               mISDN_debug_init(global_debug, debug_log, debug_log, debug_log);
-       else
-               mISDN_debug_init(global_debug, NULL, NULL, NULL);
+               mISDN_debug_init(0, NULL, NULL, NULL);
 
        /* init mlayer3 */
        init_layer3(4); // buffer of 4
@@ -81,6 +122,10 @@ void mISDN_deinitialize(void)
 
        mISDN_debug_close();
 
+       if (debug_fp)
+               fclose(debug_fp);
+       debug_fp = NULL;
+
        if (mISDNsocket > -1)
                close(mISDNsocket);
 }
@@ -142,6 +187,7 @@ void mISDN_deinitialize(void)
        unsigned char buff[1025];
 
        debug_close();
+       global_debug = 0;
 
        if (mISDNdevice >= 0)
        {
@@ -168,7 +214,8 @@ PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_setti
        p_m_b_reserve = 0;
        p_m_delete = 0;
        p_m_hold = 0;
-       p_m_txvol = p_m_rxvol = 0;
+       p_m_tx_gain = mISDNport->ifport->interface->tx_gain;
+       p_m_rx_gain = mISDNport->ifport->interface->rx_gain;
        p_m_conf = 0;
        p_m_txdata = 0;
        p_m_delay = 0;
@@ -181,7 +228,8 @@ PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_setti
        p_m_timer = 0;
        p_m_remote_ref = 0; /* channel shall be exported to given remote */
        p_m_remote_id = 0; /* channel shall be exported to given remote */
-
+       SCPY(p_m_pipeline, mISDNport->ifport->interface->pipeline);
+       
        /* audio */
        p_m_load = 0;
        p_m_last_tv_sec = 0;
@@ -194,13 +242,18 @@ PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_setti
        p_m_crypt_msg_len = 0;
        p_m_crypt_msg[0] = '\0';
        p_m_crypt_msg_current = 0;
-       p_m_crypt_key[0] = '\0';
        p_m_crypt_key_len = 0;
        p_m_crypt_listen = 0;
        p_m_crypt_listen_state = 0;
        p_m_crypt_listen_len = 0;
        p_m_crypt_listen_msg[0] = '\0';
        p_m_crypt_listen_crc = 0;
+       if (mISDNport->ifport->interface->bf_len >= 4 && mISDNport->ifport->interface->bf_len <= 56)
+       {
+               memcpy(p_m_crypt_key, mISDNport->ifport->interface->bf_key, p_m_crypt_key_len);
+               p_m_crypt_key_len = mISDNport->ifport->interface->bf_len;
+               p_m_crypt = 1;
+       }
 
        /* if any channel requested by constructor */
        if (channel == CHANNEL_ANY)
@@ -460,7 +513,7 @@ static int _bchannel_create(struct mISDNport *mISDNport, int i)
 
        /* open socket */
        mISDNport->b_socket[i] = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
-       if (mISDNport->b_socket[i])
+       if (mISDNport->b_socket[i] < 0)
        {
                PERROR("Error: Failed to open bchannel-socket for index %d with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", i);
                return(0);
@@ -598,6 +651,8 @@ static void _bchannel_activate(struct mISDNport *mISDNport, int i, int activate)
        /* trace */
        chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
        add_trace("channel", NULL, "%d", i+1+(i>=15));
+       if (mISDNport->b_timer[i])
+               add_trace("event", NULL, "timeout recovery");
        end_trace();
 }
 
@@ -626,14 +681,20 @@ static void _bchannel_configure(struct mISDNport *mISDNport, int i)
        }
 
        /* set dsp features */
+#ifndef OLD_MISDN
        if (port->p_m_txdata)
                ph_control(mISDNport, port, handle, (port->p_m_txdata)?CMX_TXDATA_ON:CMX_TXDATA_OFF, 0, "DSP-TXDATA", port->p_m_txdata);
        if (port->p_m_delay)
                ph_control(mISDNport, port, handle, CMX_DELAY, port->p_m_delay, "DSP-DELAY", port->p_m_delay);
-       if (port->p_m_txvol)
-               ph_control(mISDNport, port, handle, VOL_CHANGE_TX, port->p_m_txvol, "DSP-TXVOL", port->p_m_txvol);
-       if (port->p_m_rxvol)
-               ph_control(mISDNport, port, handle, VOL_CHANGE_RX, port->p_m_rxvol, "DSP-RXVOL", port->p_m_rxvol);
+#endif
+       if (port->p_m_tx_gain)
+               ph_control(mISDNport, port, handle, VOL_CHANGE_TX, port->p_m_tx_gain, "DSP-TX_GAIN", port->p_m_tx_gain);
+       if (port->p_m_rx_gain)
+               ph_control(mISDNport, port, handle, VOL_CHANGE_RX, port->p_m_rx_gain, "DSP-RX_GAIN", port->p_m_rx_gain);
+#ifndef OLD_MISDN
+       if (port->p_m_pipeline[0])
+               ph_control_block(mISDNport, port, handle, PIPELINE_CFG, port->p_m_pipeline, strlen(port->p_m_pipeline)+1, "DSP-PIPELINE", 0);
+#endif
        if (port->p_m_conf)
                ph_control(mISDNport, port, handle, CMX_CONF_JOIN, port->p_m_conf, "DSP-CONF", port->p_m_conf);
        if (port->p_m_echo)
@@ -777,18 +838,31 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
 {
        class PmISDN *b_port = mISDNport->b_port[i];
        int state = mISDNport->b_state[i];
+       double timer = mISDNport->b_timer[i];
        unsigned long p_m_remote_ref = 0;
        unsigned long p_m_remote_id = 0;
+       int p_m_tx_gain = 0;
+       int p_m_rx_gain = 0;
+       char *p_m_pipeline = NULL;
+       unsigned char *p_m_crypt_key = NULL;
+       int p_m_crypt_key_len = 0;
+       int p_m_crypt_key_type = 0;
 #ifdef SOCKET_MISDN
-       unsigned long handle = mISDNport->b_addr[i];
+       unsigned long portid = (mISDNport->portnum<<8) + i+1+(i>=15);
 #else
-       int handle = mISDNport->b_addr[i];
+       unsigned long portid = mISDNport->b_stid[i];
 #endif
 
        if (b_port)
        {
                p_m_remote_id = b_port->p_m_remote_id;
                p_m_remote_ref = b_port->p_m_remote_ref;
+               p_m_tx_gain = b_port->p_m_tx_gain;
+               p_m_rx_gain = b_port->p_m_rx_gain;
+               p_m_pipeline = b_port->p_m_pipeline;
+               p_m_crypt_key = b_port->p_m_crypt_key;
+               p_m_crypt_key_len = b_port->p_m_crypt_key_len;
+               p_m_crypt_key_type = /*b_port->p_m_crypt_key_type*/1;
        }
 
        switch(event)
@@ -803,13 +877,13 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        if (p_m_remote_ref)
                        {
                                /* export bchannel */
-                               message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
+                               message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "assign");
 #ifdef SOCKET_MISDN
-                               add_trace("stack", "address", "%x", handle);
+                               add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
 #else
-                               add_trace("socket", "id", "%d", handle);
+                               add_trace("socket", "id", "%d", portid);
 #endif
                                end_trace();
                                state = B_STATE_EXPORTING;
@@ -822,6 +896,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                {
                                        _bchannel_activate(mISDNport, i, 1);
                                        state = B_STATE_ACTIVATING;
+                                       timer = now_d + B_TIMER_ACTIVATING;
                                }
                        }
                        break;
@@ -858,13 +933,13 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        /* in case, the bchannel is exported right after seize_bchannel */
                        /* export bchannel */
                        /* p_m_remote_id is set, when this event happens. */
-                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
+                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "assign");
 #ifdef SOCKET_MISDN
-                       add_trace("stack", "address", "%x", handle);
+                       add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
 #else
-                       add_trace("socket", "id", "%d", handle);
+                       add_trace("socket", "id", "%d", portid);
 #endif
                        end_trace();
                        state = B_STATE_EXPORTING;
@@ -886,6 +961,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        /* bchannel is active, so we deactivate */
                        _bchannel_activate(mISDNport, i, 0);
                        state = B_STATE_DEACTIVATING;
+                       timer = now_d + B_TIMER_DEACTIVATING;
                        break;
 
                        default:
@@ -898,6 +974,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                break;
 
                case B_EVENT_ACTIVATED:
+               timer = 0;
                switch(state)
                {
                        case B_STATE_ACTIVATING:
@@ -911,6 +988,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                /* bchannel is active, but exported OR not used anymore (or has wrong stack config), so we deactivate */
                                _bchannel_activate(mISDNport, i, 0);
                                state = B_STATE_DEACTIVATING;
+                               timer = now_d + B_TIMER_DEACTIVATING;
                        }
                        break;
 
@@ -933,13 +1011,13 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                 * OR bchannel is not used anymore
                                 * OR bchannel has been exported to an obsolete ref,
                                 * so reimport, to later export to new remote */
-                               message_bchannel_to_join(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, handle);
+                               message_bchannel_to_join(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0);
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "remove");
 #ifdef SOCKET_MISDN
-                               add_trace("stack", "address", "%x", handle);
+                               add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
 #else
-                               add_trace("socket", "id", "%d", handle);
+                               add_trace("socket", "id", "%d", portid);
 #endif
                                end_trace();
                                state = B_STATE_IMPORTING;
@@ -969,17 +1047,18 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        /* bchannel is active, so we deactivate */
                        _bchannel_activate(mISDNport, i, 0);
                        state = B_STATE_DEACTIVATING;
+                       timer = now_d + B_TIMER_DEACTIVATING;
                        break;
 
                        case B_STATE_REMOTE:
                        /* bchannel is exported, so we re-import */
-                       message_bchannel_to_join(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, handle);
+                       message_bchannel_to_join(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, portid, 0,0,0,0,0,0);
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "remove");
 #ifdef SOCKET_MISDN
-                       add_trace("stack", "address", "%x", handle);
+                       add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
 #else
-                       add_trace("socket", "id", "%d", handle);
+                       add_trace("socket", "id", "%d", portid);
 #endif
                        end_trace();
                        state = B_STATE_IMPORTING;
@@ -996,6 +1075,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                break;
 
                case B_EVENT_DEACTIVATED:
+               timer = 0;
                switch(state)
                {
                        case B_STATE_IDLE:
@@ -1010,13 +1090,13 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                /* bchannel is now deactivate, but is requied by Port class, so we reactivate / export */
                                if (p_m_remote_ref)
                                {
-                                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
+                                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
 #ifdef SOCKET_MISDN
-                                       add_trace("stack", "address", "%x", handle);
+                                       add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
 #else
-                                       add_trace("socket", "id", "%d", handle);
+                                       add_trace("socket", "id", "%d", portid);
 #endif
                                        end_trace();
                                        state = B_STATE_EXPORTING;
@@ -1028,6 +1108,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                        {
                                                _bchannel_activate(mISDNport, i, 1);
                                                state = B_STATE_ACTIVATING;
+                                               timer = now_d + B_TIMER_ACTIVATING;
                                        }
                                }
                        }
@@ -1050,13 +1131,13 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                /* bchannel is now imported, but is requied by Port class, so we reactivate / export */
                                if (p_m_remote_ref)
                                {
-                                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
+                                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, portid, p_m_tx_gain, p_m_rx_gain, p_m_pipeline, p_m_crypt_key, p_m_crypt_key_len, p_m_crypt_key_type);
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
 #ifdef SOCKET_MISDN
-                                       add_trace("stack", "address", "%x", handle);
+                                       add_trace("channel", NULL, "%d.%d", portid>>8, portid&0xff);
 #else
-                                       add_trace("socket", "id", "%d", handle);
+                                       add_trace("socket", "id", "%d", portid);
 #endif
                                        end_trace();
                                        state = B_STATE_EXPORTING;
@@ -1068,6 +1149,7 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                                        {
                                                _bchannel_activate(mISDNport, i, 1);
                                                state = B_STATE_ACTIVATING;
+                                               timer = now_d + B_TIMER_ACTIVATING;
                                        }
                                }
                        }
@@ -1079,11 +1161,35 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                }
                break;
 
+               case B_EVENT_TIMEOUT:
+               timer = 0;
+               switch(state)
+               {
+                       case B_STATE_IDLE:
+                       /* ignore due to deactivation confirm after unloading */
+                       break;
+
+                       case B_STATE_ACTIVATING:
+                       _bchannel_activate(mISDNport, i, 1);
+                       timer = now_d + B_TIMER_ACTIVATING;
+                       break;
+
+                       case B_STATE_DEACTIVATING:
+                       _bchannel_activate(mISDNport, i, 0);
+                       timer = now_d + B_TIMER_DEACTIVATING;
+                       break;
+
+                       default:
+                       PERROR("Illegal event %d at state %d, please correct.\n", event, state);
+               }
+               break;
+
                default:
                PERROR("Illegal event %d, please correct.\n", event);
        }
 
        mISDNport->b_state[i] = state;
+       mISDNport->b_timer[i] = timer;
 }
 
 
@@ -1173,15 +1279,14 @@ seize:
  */
 void PmISDN::drop_bchannel(void)
 {
-       if (p_m_b_index < 0)
-               return;
-
        /* unreserve channel */
        if (p_m_b_reserve)
                p_m_mISDNport->b_reserved--;
        p_m_b_reserve = 0;
 
        /* if not in use */
+       if (p_m_b_index < 0)
+               return;
        if (!p_m_b_channel)
                return;
 
@@ -1265,7 +1370,7 @@ void message_bchannel_from_join(class JoinRemote *joinremote, int type, unsigned
 #ifdef SOCKET_MISDN
                                if (mISDNport->b_socket[i] == handle)
 #else
-                               if (mISDNport->b_addr[i] == handle)
+                               if ((unsigned long)(mISDNport->portnum<<8)+i+1+(i>=15) == handle)
 #endif
                                        break;
                                i++;
@@ -1383,7 +1488,9 @@ int PmISDN::handler(void)
                        p_m_load = 0;
 
                /* to send data, tone must be active OR crypt messages must be on */
-               if ((p_tone_name[0] || p_m_crypt_msg_loops) && p_m_load < ISDN_LOAD)
+               if ((p_tone_name[0] || p_m_crypt_msg_loops)
+                && (p_m_load < ISDN_LOAD)
+                && (p_state==PORT_STATE_CONNECT || p_m_mISDNport->tones))
                {
                        int tosend = ISDN_LOAD - p_m_load, length; 
 #ifdef SOCKET_MISDN
@@ -1416,6 +1523,7 @@ int PmISDN::handler(void)
                                        /* next loop */
                                        p_m_crypt_msg_current = 0;
                                        p_m_crypt_msg_loops--;
+//                                     puts("eine loop weniger");
                                }
 
                                /* new length */
@@ -1532,6 +1640,22 @@ void PmISDN::bchannel_receive(iframe_t *frm)
                        message_put(message);
                        break;
 
+                       default:
+                       chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
+                       add_trace("unknown", NULL, "0x%x", cont);
+                       end_trace();
+               }
+               return;
+       }
+#ifdef SOCKET_MISDN
+       if (hh->prim == PH_SIGNAL_IND)
+#else
+       if (frm->prim == (PH_SIGNAL | INDICATION))
+#endif
+       {
+               switch(frm->dinfo)
+               {
+#ifndef OLD_MISDN
                        case CMX_TX_DATA:
                        if (!p_m_txdata)
                        {
@@ -1539,13 +1663,19 @@ void PmISDN::bchannel_receive(iframe_t *frm)
                                PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
                                return;
                        }
+                       /* see below (same condition) */
+                       if (p_state!=PORT_STATE_CONNECT
+                                && !p_m_mISDNport->tones)
+                               break;
+//                     printf(".");fflush(stdout);return;
                        if (p_record)
-                               record((unsigned char *)(cont+1), len - 4, 1); // from up
+                               record(data, len, 1); // from up
                        break;
+#endif
 
                        default:
-                       chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
-                       add_trace("unknown", NULL, "0x%x", cont);
+                       chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
+                       add_trace("unknown", NULL, "0x%x", frm->dinfo);
                        end_trace();
                }
                return;
@@ -1561,14 +1691,13 @@ void PmISDN::bchannel_receive(iframe_t *frm)
 #endif
                return;
        }
-
        /* calls will not process any audio data unless
         * the call is connected OR interface features audio during call setup.
         */
 //printf("%d -> %d prim=%x joindata=%d tones=%d\n", p_serial, ACTIVE_EPOINT(p_epointlist), frm->prim, p_m_joindata, p_m_mISDNport->earlyb);    
 #ifndef DEBUG_COREBRIDGE
        if (p_state!=PORT_STATE_CONNECT
-        && !p_m_mISDNport->earlyb)
+        && !p_m_mISDNport->tones)
                return;
 #endif
 
@@ -1765,32 +1894,32 @@ void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union
        switch(param->mISDNsignal.message)
        {
                case mISDNSIGNAL_VOLUME:
-               if (p_m_txvol != param->mISDNsignal.txvol)
+               if (p_m_tx_gain != param->mISDNsignal.tx_gain)
                {
-                       p_m_txvol = param->mISDNsignal.txvol;
-                       PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
+                       p_m_tx_gain = param->mISDNsignal.tx_gain;
+                       PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_tx_gain);
                        if (p_m_b_index >= 0)
                        if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
 #ifdef SOCKET_MISDN
-                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_socket[p_m_b_index], VOL_CHANGE_TX, p_m_txvol, "DSP-TXVOL", p_m_txvol);
+                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_socket[p_m_b_index], VOL_CHANGE_TX, p_m_tx_gain, "DSP-TX_GAIN", p_m_tx_gain);
 #else
-                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], VOL_CHANGE_TX, p_m_txvol, "DSP-TXVOL", p_m_txvol);
+                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], VOL_CHANGE_TX, p_m_tx_gain, "DSP-TX_GAIN", p_m_tx_gain);
 #endif
                } else
-                       PDEBUG(DEBUG_BCHANNEL, "we already have tx-volume shift=%d.\n", p_m_rxvol);
-               if (p_m_rxvol != param->mISDNsignal.rxvol)
+                       PDEBUG(DEBUG_BCHANNEL, "we already have tx-volume shift=%d.\n", p_m_rx_gain);
+               if (p_m_rx_gain != param->mISDNsignal.rx_gain)
                {
-                       p_m_rxvol = param->mISDNsignal.rxvol;
-                       PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
+                       p_m_rx_gain = param->mISDNsignal.rx_gain;
+                       PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rx_gain);
                        if (p_m_b_index >= 0)
                        if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
 #ifdef SOCKET_MISDN
-                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_socket[p_m_b_index], VOL_CHANGE_RX, p_m_rxvol, "DSP-RXVOL", p_m_rxvol);
+                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_socket[p_m_b_index], VOL_CHANGE_RX, p_m_rx_gain, "DSP-RX_GAIN", p_m_rx_gain);
 #else
-                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], VOL_CHANGE_RX, p_m_rxvol, "DSP-RXVOL", p_m_rxvol);
+                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], VOL_CHANGE_RX, p_m_rx_gain, "DSP-RX_GAIN", p_m_rx_gain);
 #endif
                } else
-                       PDEBUG(DEBUG_BCHANNEL, "we already have rx-volume shift=%d.\n", p_m_rxvol);
+                       PDEBUG(DEBUG_BCHANNEL, "we already have rx-volume shift=%d.\n", p_m_rx_gain);
                break;
 
                case mISDNSIGNAL_CONF:
@@ -1828,6 +1957,7 @@ void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union
                {
                        p_m_delay = param->mISDNsignal.delay;
                        PDEBUG(DEBUG_BCHANNEL, "we change delay mode to delay=%d.\n", p_m_delay);
+#ifndef OLD_MISDN
                        if (p_m_b_index >= 0)
                        if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
 #ifdef SOCKET_MISDN
@@ -1835,6 +1965,7 @@ void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union
 #else
                                ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], p_m_delay?CMX_DELAY:CMX_JITTER, p_m_delay, "DSP-DELAY", p_m_delay);
 #endif
+#endif
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have delay=%d.\n", p_m_delay);
                break;
@@ -1896,7 +2027,7 @@ void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parame
                        break;
                }
                p_m_crypt_msg_current = 0; /* reset */
-               p_m_crypt_msg_loops = 3; /* enable */
+               p_m_crypt_msg_loops = 6; /* enable */
 #if 0
                /* disable txmix, or we get corrupt data due to audio process */
                if (p_m_txmix && p_m_b_index>=0)
@@ -1947,19 +2078,183 @@ int PmISDN::message_epoint(unsigned long epoint_id, int message_id, union parame
 
 
 /*
- * main loop for processing messages from mISDN device
+ * main loop for processing messages from mISDN
  */
+#ifdef SOCKET_MISDN
+int mISDN_handler(void)
+{
+       int ret, work = 0;
+       struct mISDNport *mISDNport;
+       class PmISDN *isdnport;
+       int i;
+       char buffer[2048+MISDN_HEADER_LEN];
+       struct mISDNhead *hh = (struct mISDNhead *)buffer;
+
+       /* process all ports */
+       mISDNport = mISDNport_first;
+       while(mISDNport)
+       {
+               /* process all bchannels */
+               i = 0;
+               while(i < mISDNport->b_num)
+               {
+                       /* process timer events for bchannel handling */
+                       if (mISDNport->b_timer[i])
+                       {
+                               if (mISDNport->b_timer[i] <= now_d)
+                                       bchannel_event(mISDNport, i, B_EVENT_TIMEOUT);
+                       }
+                       /* handle port of bchannel */
+                       isdnport=mISDNport->b_port[i];
+                       if (isdnport)
+                       {
+                               /* call bridges in user space OR crypto OR recording */
+                               if (isdnport->p_m_joindata || isdnport->p_m_crypt_msg_loops || isdnport->p_m_crypt_listen || isdnport->p_record)
+                               {
+                                       /* rx IS required */
+                                       if (isdnport->p_m_rxoff)
+                                       {
+                                               /* turn on RX */
+                                               isdnport->p_m_rxoff = 0;
+                                               PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
+                                               if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+                                                       ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], CMX_RECEIVE_ON, 0, "DSP-RXOFF", 0);
+                                               return(1);
+                                       }
+                               } else
+                               {
+                                       /* rx NOT required */
+                                       if (!isdnport->p_m_rxoff)
+                                       {
+                                               /* turn off RX */
+                                               isdnport->p_m_rxoff = 1;
+                                               PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
+                                               if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+                                                       ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], CMX_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
+                                               return(1);
+                                       }
+                               }
+                               /* recording */
+                               if (isdnport->p_record)
+                               {
+                                       /* txdata IS required */
+                                       if (!isdnport->p_m_txdata)
+                                       {
+                                               /* turn on RX */
+                                               isdnport->p_m_txdata = 1;
+                                               PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is required, so we turn them on\n");
+                                               if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+                                                       ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], CMX_TXDATA_ON, 0, "DSP-TXDATA", 1);
+                                               return(1);
+                                       }
+                               } else
+                               {
+                                       /* txdata NOT required */
+                                       if (isdnport->p_m_txdata)
+                                       {
+                                               /* turn off RX */
+                                               isdnport->p_m_txdata = 0;
+                                               PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is not required, so we turn them off\n");
+                                               if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+                                                       ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], CMX_TXDATA_OFF, 0, "DSP-TXDATA", 0);
+                                               return(1);
+                                       }
+                               }
+                       }
+
+                       /* handle message from bchannel */
+                       if (mISDNport->b_stack[i] > -1)
+                       {
+                               ret = recv(mISDNport->b_stack[i], buffer, sizeof(buffer), 0);
+                               if (ret >= MISDN_HEADER_LEN)
+                               {
+                                       work = 1;
+                                       switch(hh->prim)
+                                       {
+                                               /* we don't care about confirms, we use rx data to sync tx */
+                                               case PH_DATA_CONF:
+                                               case DL_DATA_CONF:
+                                               break;
+
+                                               /* we receive audio data, we respond to it AND we send tones */
+                                               case PH_DATA_IND:
+                                               case DL_DATA_IND:
+                                               case PH_SIGNAL_IND:
+                                               case PH_CONTROL_IND:
+                                               if (mISDNport->b_port[i])
+                                                       mISDNport->b_port[i]->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
+                                               else
+                                                       PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (socket %d), ignoring.\n", mISDNport->b_stack[i]);
+                                               break;
+
+                                               case PH_ACTIVATE_IND:
+                                               case DL_ESTABLISH_IND:
+                                               case PH_ACTIVATE_CONF:
+                                               case DL_ESTABLISH_CONF:
+                                               PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", mISDNport->b_socket[i]);
+                                               bchannel_event(mISDNport, i, B_EVENT_ACTIVATED);
+                                               break;
+
+                                               case PH_DEACTIVATE_IND:
+                                               case DL_RELEASE_IND:
+                                               case PH_DEACTIVATE_CONF:
+                                               case DL_RELEASE_CONF:
+                                               PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", mISDNport->b_socket[i]);
+                                               bchannel_event(mISDNport, i, B_EVENT_DEACTIVATED);
+                                               break;
+
+                                               default:
+                                               PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, mISDNport->b_socket[i], msg->len);
+                                       }
+                               } else
+                               {
+                                       if (ret < 0 && errno != EWOULDBLOCK)
+                                               PERROR("Read from port %d, index %d failed with return code %d\n", mISDNport->portnum, i, ret);
+                               }
+                       }
+                       
+                       i++;
+               }
+#if 0
+               if (mISDNport->l1timeout && now>mISDNport->l1timeout)
+               { ---}
+                       PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
+                       mISDNport->l1timeout = 0;
+#endif
+
+               /* layer 2 establish timer */
+               if (mISDNport->l2establish)
+               {
+                       if (now-mISDNport->l2establish > 5)
+                       {
+
+                               PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link portnum=%d.\n", mISDNport->portnum);
+                               mISDNport->ml3->to_layer2(mISDNport->ml3, DL_ESTABLISH_REQ);
+                               l1l2l3_trace_header(mISDNport, NULL, DL_ESTABLISH_REQ, DIRECTION_OUT);
+                               end_trace();
+                               return(1);
+                       }
+               }
+
+
+               mISDNport = mISDNport->next;
+       }
+
+       /* if we received at least one b-frame, we will return 1 */
+       return(work);
+}
+#else
 int mISDN_handler(void)
 {
        int ret;
-       msg_t *msg;
-       iframe_t *frm;
        struct mISDNport *mISDNport;
        class PmISDN *isdnport;
-       net_stack_t *nst;
+       int i;
+       msg_t *msg;
+       iframe_t *frm;
        msg_t *dmsg;
        mISDNuser_head_t *hh;
-       int i;
+       net_stack_t *nst;
 
        /* the que avoids loopbacks when replying to stack after receiving
          * from stack. */
@@ -1970,6 +2265,12 @@ int mISDN_handler(void)
                i = 0;
                while(i < mISDNport->b_num)
                {
+                       /* process timer events for bchannel handling */
+                       if (mISDNport->b_timer[i])
+                       {
+                               if (mISDNport->b_timer[i] <= now_d)
+                                       bchannel_event(mISDNport, i, B_EVENT_TIMEOUT);
+                       }
                        isdnport=mISDNport->b_port[i];
                        if (isdnport)
                        {
@@ -2008,8 +2309,10 @@ int mISDN_handler(void)
                                                /* turn on RX */
                                                isdnport->p_m_txdata = 1;
                                                PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is required, so we turn them on\n");
+#ifndef OLD_MISDN
                                                if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
                                                        ph_control(mISDNport, isdnport, mISDNport->b_addr[isdnport->p_m_b_index], CMX_TXDATA_ON, 0, "DSP-TXDATA", 1);
+#endif
                                                return(1);
                                        }
                                } else
@@ -2020,8 +2323,10 @@ int mISDN_handler(void)
                                                /* turn off RX */
                                                isdnport->p_m_txdata = 0;
                                                PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is not required, so we turn them off\n");
+#ifndef OLD_MISDN
                                                if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
                                                        ph_control(mISDNport, isdnport, mISDNport->b_addr[isdnport->p_m_b_index], CMX_TXDATA_OFF, 0, "DSP-TXDATA", 0);
+#endif
                                                return(1);
                                        }
                                }
@@ -2030,7 +2335,7 @@ int mISDN_handler(void)
                }
 #if 0
                if (mISDNport->l1timeout && now>mISDNport->l1timeout)
-               {
+               { ---}
                        PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
                        mISDNport->l1timeout = 0;
 #endif
@@ -2048,10 +2353,11 @@ int mISDN_handler(void)
                                        if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
                                                free_msg(dmsg);
                                } else {
+                                       iframe_t act;
+
                                        PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link TE portnum=%d.\n", mISDNport->portnum);
                                        time(&mISDNport->l2establish);
                                        /* establish */
-                                       iframe_t act;
                                        act.prim = DL_ESTABLISH | REQUEST; 
                                        act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
                                        act.dinfo = 0;
@@ -2173,6 +2479,15 @@ int mISDN_handler(void)
        if (!mISDNport)
        {
                PERROR("message belongs to no mISDNport: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
+               // show a list of all mISDNports and their address
+#if 0
+               mISDNport = mISDNport_first;
+               while(mISDNport)
+               {
+                       PERROR(" port %s  %x -> %x\n", mISDNport->name, (frm->addr&MASTER_ID_MASK), (unsigned int)(mISDNport->upper_id&MASTER_ID_MASK));
+                       mISDNport = mISDNport->next;
+               } 
+#endif
                goto out;
        }
 
@@ -2305,6 +2620,7 @@ int mISDN_handler(void)
                        case PH_DATA | INDICATION:
                        case DL_DATA | INDICATION:
                        case PH_CONTROL | INDICATION:
+                       case PH_SIGNAL | INDICATION:
                        i = 0;
                        while(i < mISDNport->b_num)
                        {
@@ -2374,6 +2690,119 @@ int mISDN_handler(void)
        free_msg(msg);
        return(1);
 }
+#endif
+
+#ifdef SOCKET_MISDN
+int do_layer3(struct mlayer3 *ml3, unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
+{
+       struct mISDNport *mISDNport = ml3->private;
+
+       if (cmd == MT_ASSIGN)
+       {
+               ueberdenken!!!
+       }
+       
+       /* lock LCR */
+       achtung MT_ASSIGN kommt hier an
+       lock it baby
+
+       /* d-message */
+       switch(cmd)
+       {
+               case MGR_SHORTSTATUS_IND:
+               case MGR_SHORTSTATUS_CONF:
+               switch(frm->dinfo) {
+                       case SSTATUS_L1_ACTIVATED:
+                       l1l2l3_trace_header(mISDNport, NULL, PH_ACTIVATE_IND, DIRECTION_IN);
+                       end_trace();
+                       goto ss_act;
+                       case SSTATUS_L1_DEACTIVATED:
+                       l1l2l3_trace_header(mISDNport, NULL, PH_DEACTIVATE_IND, DIRECTION_IN);
+                       end_trace();
+                       goto ss_deact;
+                       case SSTATUS_L2_ESTABLISHED:
+                       l1l2l3_trace_header(mISDNport, NULL, DL_ESTABLISH_IND, DIRECTION_IN);
+                       end_trace();
+                       goto ss_estab;
+                       case SSTATUS_L2_RELEASED:
+                       l1l2l3_trace_header(mISDNport, NULL, DL_RELEASE_IND, DIRECTION_IN);
+                       end_trace();
+                       goto ss_rel;
+               }
+               break;
+
+               case PH_ACTIVATE_CONF:
+               case PH_ACTIVATE_IND:
+               l1l2l3_trace_header(mISDNport, NULL, cmd, DIRECTION_IN);
+               end_trace();
+               ss_act:
+               mISDNport->l1link = 1;
+#if 0
+               if (mISDNport->ntmode)
+                       setup_queue(mISDNport, 1);
+#endif
+               break;
+
+               case PH_DEACTIVATE | CONFIRM:
+               case PH_DEACTIVATE | INDICATION:
+               l1l2l3_trace_header(mISDNport, NULL, cmd, DIRECTION_IN);
+               end_trace();
+               ss_deact:
+               mISDNport->l1link = 0;
+raus mit der setup-queue, da dies im stack geschieht
+#if 0
+               if (mISDNport->ntmode)
+                       setup_queue(mISDNport, 0);
+#endif
+               break;
+
+               case PH_CONTROL_CONFIRM:
+               case PH_CONTROL_INDICATION:
+               PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
+               break;
+
+               case DL_ESTABLISH_IND:
+               case DL_ESTABLISH_CONF:
+               l1l2l3_trace_header(mISDNport, NULL, cmd, DIRECTION_IN);
+               end_trace();
+               if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
+               ss_estab:
+               if (mISDNport->l2establish)
+               {
+                       mISDNport->l2establish = 0;
+                       PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
+               }
+               mISDNport->l2link = 1;
+               break;
+
+               case DL_RELEASE_IND:
+               case DL_RELEASE_CONF:
+               l1l2l3_trace_header(mISDNport, NULL, cmd, DIRECTION_IN);
+               end_trace();
+               ss_rel:
+               mISDNport->l2link = 0;
+               if (mISDNport->ptp)
+               {
+                       time(&mISDNport->l2establish);
+                       PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
+               }
+               break;
+
+               default:
+               /* l3-data is sent to LCR */
+               message_from_mlayer3(mISDNport, cmd, pid, l3m);
+       }
+
+       /* unlock LCR */
+       unlock it baby
+
+       /* free message */
+       if (l3m)
+               free_l3_msg(l3m);
+       return(0);
+
+}
+#endif
 
 
 /*
@@ -3187,13 +3616,23 @@ done:
  */
 void PmISDN::txfromup(unsigned char *data, int length)
 {
+#ifdef SOCKET_MISDN
+       unsigned char buf[MISDN_HEADER_LEN+((length>ISDN_LOAD)?length:ISDN_LOAD)];
+       struct mISDNhead *frm = (struct mISDNhead *)buf;
+
+       if (p_m_b_index < 0)
+               return;
+       if (!p_m_mISDNport->b_socket[p_m_b_index])
+               return;
+#else
        unsigned char buf[mISDN_HEADER_LEN+((length>ISDN_LOAD)?length:ISDN_LOAD)];
        iframe_t *frm = (iframe_t *)buf;
 
-       /* configure frame */
-       frm->prim = DL_DATA | REQUEST; 
-       frm->addr = p_m_mISDNport->b_addr[p_m_b_index] | FLG_MSG_DOWN;
-       frm->dinfo = 0;
+       if (p_m_b_index < 0)
+               return;
+       if (!p_m_mISDNport->b_addr[p_m_b_index])
+               return;
+#endif
 
        /* check if high priority tones exist
         * ignore data in this case
@@ -3220,11 +3659,22 @@ void PmISDN::txfromup(unsigned char *data, int length)
        if (p_m_load+length > ISDN_MAXLOAD)
                return;
 
-       /* load data to buffer
-        */
-       memcpy(buf+mISDN_HEADER_LEN, data, length);
+       /* make and send frame */
+#ifdef SOCKET_MISDN
+       frm->prim = DL_DATA_REQ;
+       frm->id = 0;
+       memcpy(buf+MISDN_HEADER_LEN, data, length);
+       ret = sendto(p_m_mISDNport->b_socket[p_m_b_index], buf, MISDN_HEADER_LEN+length, 0, NULL, 0);
+       if (!ret)
+               PERROR("Failed to send to socket %d\n", handle);
+#else
+       frm->prim = DL_DATA | REQUEST; 
+       frm->addr = p_m_mISDNport->b_addr[p_m_b_index] | FLG_MSG_DOWN;
+       frm->dinfo = 0;
        frm->len = length;
+       memcpy(buf+mISDN_HEADER_LEN, data, length);
        mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
-       p_m_load += frm->len;
+#endif
+       p_m_load += length;
 }