backup
authorSuper User <root@isdn.jolly.ten>
Sun, 26 Aug 2007 13:23:58 +0000 (15:23 +0200)
committerSuper User <root@isdn.jolly.ten>
Sun, 26 Aug 2007 13:23:58 +0000 (15:23 +0200)
13 files changed:
README
admin_server.c
asterisk_client.c
default/interface.conf
interface.c
interface.h
joinremote.cpp
joinremote.h
mISDN.cpp
mISDN.h
main.c
main.h
message.h

diff --git a/README b/README
index 58e97e8..3a14001 100644 (file)
--- a/README
+++ b/README
@@ -366,4 +366,4 @@ Changes in Version 0.2
 - Stall warning
 - Audio recoriding still does not work.
 
 - Stall warning
 - Audio recoriding still does not work.
 
-
+- Added handle structure for all file descriptors
index 6b0647b..c42f25f 100644 (file)
@@ -648,7 +648,7 @@ int admin_message_to_join(struct admin_msg *msg, char *remote_name, int sock_id)
         || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK)
        {
                /* no ref, but address */
         || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK)
        {
                /* no ref, but address */
-               message_bchannel_from_join(NULL, msg->param.bchannel.type, msg->param.bchannel.addr);
+               message_bchannel_from_join(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
                return(0);
        }
        
                return(0);
        }
        
index 105bfb4..c430c30 100644 (file)
@@ -60,7 +60,7 @@ struct admin_list {
 struct chan_bchannel *bchannel_first;
 struct chan_call *call_first;
 
 struct chan_bchannel *bchannel_first;
 struct chan_call *call_first;
 
-struct chan_bchannel *find_bchannel_addr(unsigned long addr)
+struct chan_bchannel *find_bchannel_handle(unsigned long addr)
 {
        struct chan_bchannel *bchannel = bchannel_first;
 
 {
        struct chan_bchannel *bchannel = bchannel_first;
 
@@ -204,34 +204,34 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                switch(param->bchannel.type)
                {
                        case BCHANNEL_ASSIGN:
                switch(param->bchannel.type)
                {
                        case BCHANNEL_ASSIGN:
-                       if ((bchannel = find_bchannel_addr(param->bchannel.addr)))
+                       if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
                        {
                        {
-                               fprintf(stderr, "error: bchannel addr %x already assigned.\n", param->bchannel.addr);
+                               fprintf(stderr, "error: bchannel handle %x already assigned.\n", param->bchannel.handle);
                                return(-1);
                        }
                        /* create bchannel */
                        bchannel = alloc_bchannel();
                                return(-1);
                        }
                        /* create bchannel */
                        bchannel = alloc_bchannel();
-                       bchannel->addr = param->bchannel.addr;
+                       bchannel->addr = param->bchannel.handle;
                        /* in case, ref is not set, this bchannel instance must
                         * be created until it is removed again by LCR */
                        /* link to call */
                        if ((call = find_call_ref(ref)))
                        {
                                bchannel->ref = ref;
                        /* in case, ref is not set, this bchannel instance must
                         * be created until it is removed again by LCR */
                        /* link to call */
                        if ((call = find_call_ref(ref)))
                        {
                                bchannel->ref = ref;
-                               call->addr = param->bchannel.addr;
+                               call->addr = param->bchannel.handle;
                        }
 
 #warning open stack
                        /* acknowledge */
                        newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
                        }
 
 #warning open stack
                        /* acknowledge */
                        newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
-                       newparam.bchannel.addr = param->bchannel.addr;
+                       newparam.bchannel.handle = param->bchannel.handle;
                        send_message(MESSAGE_BCHANNEL, 0, &newparam);
                        break;
 
                        case BCHANNEL_REMOVE:
                        send_message(MESSAGE_BCHANNEL, 0, &newparam);
                        break;
 
                        case BCHANNEL_REMOVE:
-                       if (!(bchannel = find_bchannel_addr(param->bchannel.addr)))
+                       if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
                        {
                        {
-                               fprintf(stderr, "error: bchannel addr %x not assigned.\n", param->bchannel.addr);
+                               fprintf(stderr, "error: bchannel handle %x not assigned.\n", param->bchannel.handle);
                                return(-1);
                        }
                        /* unlink from call */
                                return(-1);
                        }
                        /* unlink from call */
@@ -244,7 +244,7 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
 #warning close stack
                        /* acknowledge */
                        newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
 #warning close stack
                        /* acknowledge */
                        newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
-                       newparam.bchannel.addr = param->bchannel.addr;
+                       newparam.bchannel.handle = param->bchannel.handle;
                        send_message(MESSAGE_BCHANNEL, 0, &newparam);
                        
                        break;
                        send_message(MESSAGE_BCHANNEL, 0, &newparam);
                        
                        break;
index 04017ae..be5b669 100644 (file)
 #port 3
 #port 4
 
 #port 3
 #port 4
 
-# Example of an ISDN interface on port 2, which accepts all extensions
+# Example of an internal ISDN interface on port 2, which accepts all extensions
 
 #[Int]
 
 #[Int]
+#nt
 #extension
 #port 2
 
 #extension
 #port 2
 
@@ -25,6 +26,7 @@
 # 201, 202 and 203
 
 #[Int]
 # 201, 202 and 203
 
 #[Int]
+#nt
 #extension
 #msn 201,202,203
 #port 2
 #extension
 #msn 201,202,203
 #port 2
 # "interface Int1,Int2,In3" in extension's settings file.
 
 #[Int1]
 # "interface Int1,Int2,In3" in extension's settings file.
 
 #[Int1]
+#nt
 #extension
 #msn 201,202,203
 #port 2
 
 #[Int2]
 #extension
 #msn 201,202,203
 #port 2
 
 #[Int2]
+#nt
 #extension
 #msn 201
 #port 3
 
 #[Int3]
 #extension
 #msn 201
 #port 3
 
 #[Int3]
+#nt
 #extension
 #msn 201
 #port 4
 #extension
 #msn 201
 #port 4
@@ -79,6 +84,7 @@
 # setup. Also we provide tones during setup also.
 # This is usefull to interconnect to another PBX.
 #[PBX]
 # setup. Also we provide tones during setup also.
 # This is usefull to interconnect to another PBX.
 #[PBX]
+#nt
 #port 5
 #earlyb yes
 #tones yes
 #port 5
 #earlyb yes
 #tones yes
@@ -93,6 +99,7 @@ port 1
 
 
 [Int]
 
 
 [Int]
+nt
 extension
 msn 200,201,202,203
 port 2
 extension
 msn 200,201,202,203
 port 2
index 9842e19..1c0956b 100644 (file)
@@ -186,6 +186,31 @@ static int inter_ptmp(struct interface *interface, char *filename, int line, cha
        ifport->ptmp = 1;
        return(0);
 }
        ifport->ptmp = 1;
        return(0);
 }
+static int inter_nt(struct interface *interface, char *filename, int line, char *parameter, char *value)
+{
+#ifdef SOCKET_MISDN
+       struct interface_port *ifport;
+
+       /* port in chain ? */
+       if (!interface->ifport)
+       {
+               SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
+               return(-1);
+       }
+       /* goto end of chain */
+       ifport = interface->ifport;
+       while(ifport->next)
+               ifport = ifport->next;
+       /* add value */
+       if (value[0])
+       {
+               SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
+               return(-1);
+       }
+       ifport->nt = 1;
+#endif
+       return(0);
+}
 static int inter_tones(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
        if (!strcasecmp(value, "yes"))
 static int inter_tones(struct interface *interface, char *filename, int line, char *parameter, char *value)
 {
        if (!strcasecmp(value, "yes"))
@@ -667,6 +692,14 @@ struct interface_param interface_param[] = {
        "The given port above is opened as point-to-multipoint.\n"
        "This is required on PRI NT-mode ports that are point-to-point by default.\n"
        "This parameter must follow a 'port' parameter."},
        "The given port above is opened as point-to-multipoint.\n"
        "This is required on PRI NT-mode ports that are point-to-point by default.\n"
        "This parameter must follow a 'port' parameter."},
+       {"nt", &inter_nt, "",
+       "The given port above is opened in NT-mode.\n"
+#ifdef SOCKET_MISDN
+       "This is required on interfaces that support both NT-mode and TE-mode.\n"
+#else
+       "This parameter is only required for socket based mISDN driver.\n"
+#endif
+       "This parameter must follow a 'port' parameter."},
 
        {"channel-out", &inter_channel_out, "[force,][<number>][,...][,free][,any][,no]",
        "Channel selection list for all outgoing calls to the interface.\n"
 
        {"channel-out", &inter_channel_out, "[force,][<number>][,...][,free][,any][,no]",
        "Channel selection list for all outgoing calls to the interface.\n"
@@ -1074,7 +1107,7 @@ void load_port(struct interface_port *ifport)
        struct mISDNport *mISDNport;
 
        /* open new port */
        struct mISDNport *mISDNport;
 
        /* open new port */
-       mISDNport = mISDNport_open(ifport->portnum, ifport->ptp, ifport->ptmp, ifport->interface);
+       mISDNport = mISDNport_open(ifport->portnum, ifport->ptp, ifport->ptmp, ifport->nt, ifport->interface);
        if (mISDNport)
        {
                /* link port */
        if (mISDNport)
        {
                /* link port */
index c7dac41..1022e38 100644 (file)
@@ -50,6 +50,7 @@ struct interface_port {
        int                     portnum; /* port number */
        int                     ptp; /* force load stack in PTP mode */
        int                     ptmp; /* force load stack in PTP mode */
        int                     portnum; /* port number */
        int                     ptp; /* force load stack in PTP mode */
        int                     ptmp; /* force load stack in PTP mode */
+       int                     nt; /* load stack in NT-mode */
        int                     channel_force; /* forces channel by protocol */
        int                     nodtmf; /* disables DTMF */
        struct select_channel   *out_channel; /* list of channels to select */
        int                     channel_force; /* forces channel by protocol */
        int                     nodtmf; /* disables DTMF */
        struct select_channel   *out_channel; /* list of channels to select */
index ba0612c..7fc14d1 100644 (file)
@@ -109,7 +109,7 @@ void JoinRemote::message_remote(int message_type, union parameter *param)
         * also ref is given, so we send message with ref */
        if (message_type == MESSAGE_BCHANNEL)
        {
         * also ref is given, so we send message with ref */
        if (message_type == MESSAGE_BCHANNEL)
        {
-               message_bchannel_from_join(this, param->bchannel.type, param->bchannel.addr);
+               message_bchannel_from_join(this, param->bchannel.type, param->bchannel.handle);
                return;
        }
        
                return;
        }
        
@@ -125,13 +125,13 @@ void JoinRemote::message_remote(int message_type, union parameter *param)
        }
 }
 
        }
 }
 
-void message_bchannel_to_join(unsigned long remote_id, unsigned long ref, int type, unsigned long addr)
+void message_bchannel_to_join(unsigned long remote_id, unsigned long ref, int type, unsigned long handle)
 {
        union parameter param;
 
        memset(&param, 0, sizeof(union parameter));
        param.bchannel.type = type;
 {
        union parameter param;
 
        memset(&param, 0, sizeof(union parameter));
        param.bchannel.type = type;
-       param.bchannel.addr = addr;
+       param.bchannel.handle = handle;
        if (admin_message_from_join(remote_id, ref, MESSAGE_BCHANNEL, &param)<0)
        {
                PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);
        if (admin_message_from_join(remote_id, ref, MESSAGE_BCHANNEL, &param)<0)
        {
                PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);
index b8d2cae..3959bd9 100644 (file)
@@ -24,4 +24,4 @@ class JoinRemote : public Join
 }; 
 
 
 }; 
 
 
-void message_bchannel_to_join(unsigned long remote_id, unsigned long ref, int type, unsigned long addr);
+void message_bchannel_to_join(unsigned long remote_id, unsigned long ref, int type, unsigned long handle);
index 9d3cfa0..63b2c2a 100644 (file)
--- a/mISDN.cpp
+++ b/mISDN.cpp
@@ -44,17 +44,32 @@ unsigned char mISDN_rand[256];
 int mISDN_rand_count = 0;
 
 #ifdef MISDN_SOCKET
 int mISDN_rand_count = 0;
 
 #ifdef MISDN_SOCKET
+int mISDNsocket = -1;
+
 int mISDN_initialize(void)
 {
        /* try to open raw socket to check kernel */
 int mISDN_initialize(void)
 {
        /* try to open raw socket to check kernel */
-       ret = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
-       if (ret < 0)
+       mISDNsocket = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
+       if (mISDNsocket < 0)
        {
                fprintf(stderr, "Cannot open mISDN due to %s. (Does your Kernel support socket based mISDN?)\n", strerror(errno));
                return(-1);
        }
        {
                fprintf(stderr, "Cannot open mISDN due to %s. (Does your Kernel support socket based mISDN?)\n", strerror(errno));
                return(-1);
        }
-       close(ret);
 
 
+       /* initialize stuff of the NT lib */
+       if (options.deb & DEBUG_STACK)
+       {
+               global_debug = 0xffffffff & ~DBGM_MSG;
+//             global_debug = DBGM_L3DATA;
+       } 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);
+
+       /* init mlayer3 */
        init_layer3(4); // buffer of 4
 
        return(0);
        init_layer3(4); // buffer of 4
 
        return(0);
@@ -63,6 +78,11 @@ int mISDN_initialize(void)
 void mISDN_deinitialize(void)
 {
        cleanup_layer3();
 void mISDN_deinitialize(void)
 {
        cleanup_layer3();
+
+       mISDN_debug_close();
+
+       if (mISDNsocket > -1)
+               close(mISDNsocket);
 }
 #else
 int entity = 0; /* used for udevice */
 }
 #else
 int entity = 0; /* used for udevice */
@@ -350,19 +370,34 @@ void l1l2l3_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsign
 /*
  * send control information to the channel (dsp-module)
  */
 /*
  * send control information to the channel (dsp-module)
  */
-void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, unsigned long b_addr, int c1, int c2, char *trace_name, int trace_value)
+void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
 {
 {
+#ifdef SOCKET_MISDN
+       unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
+       struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
+       unsigned long *d = buffer+MISDN_HEADER_LEN;
+       int ret;
+
+       ctrl->prim = PH_CONTROL_REQ;
+       ctrl->id = 0;
+       *d++ = c1;
+       *d++ = c2;
+       ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
+       if (!ret)
+               PERROR("Failed to send to socket %d\n", handle);
+#else
        unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
        iframe_t *ctrl = (iframe_t *)buffer; 
        unsigned long *d = (unsigned long *)&ctrl->data.p;
 
        ctrl->prim = PH_CONTROL | REQUEST;
        unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
        iframe_t *ctrl = (iframe_t *)buffer; 
        unsigned long *d = (unsigned long *)&ctrl->data.p;
 
        ctrl->prim = PH_CONTROL | REQUEST;
-       ctrl->addr = b_addr | FLG_MSG_DOWN;
+       ctrl->addr = handle | FLG_MSG_DOWN;
        ctrl->dinfo = 0;
        ctrl->len = sizeof(int)*2;
        *d++ = c1;
        *d++ = c2;
        mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
        ctrl->dinfo = 0;
        ctrl->len = sizeof(int)*2;
        *d++ = c1;
        *d++ = c2;
        mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
+#endif
        chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
        if (c1 == CMX_CONF_JOIN)
                add_trace(trace_name, NULL, "0x%08x", trace_value);
        chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
        if (c1 == CMX_CONF_JOIN)
                add_trace(trace_name, NULL, "0x%08x", trace_value);
@@ -371,19 +406,34 @@ void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, unsigned lo
        end_trace();
 }
 
        end_trace();
 }
 
-void ph_control_block(struct mISDNport *mISDNport, class PmISDN *isdnport, unsigned long b_addr, int c1, void *c2, int c2_len, char *trace_name, int trace_value)
+void ph_control_block(struct mISDNport *mISDNport, class PmISDN *isdnport, unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
 {
 {
+#ifdef SOCKET_MISDN
+       unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
+       struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
+       unsigned long *d = buffer+MISDN_HEADER_LEN;
+       int ret;
+
+       ctrl->prim = PH_CONTROL_REQ;
+       ctrl->id = 0;
+       *d++ = c1;
+       memcpy(d, c2, c2_len);
+       ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
+       if (!ret)
+               PERROR("Failed to send to socket %d\n", handle);
+#else
        unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
        iframe_t *ctrl = (iframe_t *)buffer;
        unsigned long *d = (unsigned long *)&ctrl->data.p;
 
        ctrl->prim = PH_CONTROL | REQUEST;
        unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
        iframe_t *ctrl = (iframe_t *)buffer;
        unsigned long *d = (unsigned long *)&ctrl->data.p;
 
        ctrl->prim = PH_CONTROL | REQUEST;
-       ctrl->addr = b_addr | FLG_MSG_DOWN;
+       ctrl->addr = handle | FLG_MSG_DOWN;
        ctrl->dinfo = 0;
        ctrl->len = sizeof(int)+c2_len;
        *d++ = c1;
        memcpy(d, c2, c2_len);
        mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
        ctrl->dinfo = 0;
        ctrl->len = sizeof(int)+c2_len;
        *d++ = c1;
        memcpy(d, c2, c2_len);
        mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
+#endif
        chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
        add_trace(trace_name, NULL, "%d", trace_value);
        end_trace();
        chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
        add_trace(trace_name, NULL, "%d", trace_value);
        end_trace();
@@ -397,9 +447,55 @@ void ph_control_block(struct mISDNport *mISDNport, class PmISDN *isdnport, unsig
 static int _bchannel_create(struct mISDNport *mISDNport, int i)
 {
        unsigned char buff[1024];
 static int _bchannel_create(struct mISDNport *mISDNport, int i)
 {
        unsigned char buff[1024];
+       int ret;
+#ifdef SOCKET_MISDN
+       unsigned long on = 1;
+       struct sockadd_mISDN addr;
+
+       if (mISDNport->b_socket[i])
+       {
+               PERROR("Error: Socket already created for index %d\n", i);
+               return(0);
+       }
+
+       /* open socket */
+       mISDNport->b_socket[i] = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
+       if (mISDNport->b_socket[i])
+       {
+               PERROR("Error: Failed to open bchannel-socket for index %d with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", i);
+               return(0);
+       }
+       
+       /* set nonblocking io */
+       ret = ioctl(mISDNport->b_socket[i], FIONBIO, &on);
+       if (ret < 0)
+       {
+               PERROR("Error: Failed to set bchannel-socket index %d into nonblocking IO\n", i);
+               close(mISDNport->b_socket[i]);
+               mISDNport->b_socket[i] = -1;
+               return(0);
+       }
+
+       /* bind socket to bchannel */
+       addr.family = AF_ISDN;
+       addr.dev = mISDNport->port-1;
+       addr.channel = i+1+(i>=15);
+       ret = bind(di->bchan, (struct sockaddr *)&addr, sizeof(addr));
+       if (ret < 0)
+       {
+               PERROR("Error: Failed to bind bchannel-socket for index %d with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", i);
+               close(mISDNport->b_socket[i]);
+               mISDNport->b_socket[i] = -1;
+               return(0);
+       }
+
+       chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
+       add_trace("channel", NULL, "%d", i+1+(i>=15));
+       add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
+       end_trace();
+#else
        layer_info_t li;
        mISDN_pid_t pid;
        layer_info_t li;
        mISDN_pid_t pid;
-       int ret;
 
        if (!mISDNport->b_stid[i])
        {
 
        if (!mISDNport->b_stid[i])
        {
@@ -463,6 +559,7 @@ static int _bchannel_create(struct mISDNport *mISDNport, int i)
        add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
        add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
        end_trace();
        add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
        add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
        end_trace();
+#endif
 
        return(1);
 
 
        return(1);
 
@@ -478,17 +575,30 @@ failed:
  */
 static void _bchannel_activate(struct mISDNport *mISDNport, int i, int activate)
 {
  */
 static void _bchannel_activate(struct mISDNport *mISDNport, int i, int activate)
 {
+#ifdef SOCKET_MISDN
+       struct mISDNhead act;
+       int ret;
+
+       act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ; 
+       act.id = 0;
+       ret = sendto(mISDNport->b_socket[i], &act, MISDN_HEADER_LEN, 0, NULL, 0);
+       if (!ret)
+               PERROR("Failed to send to socket %d\n", mISDNport->b_socket[i]);
+#else
        iframe_t act;
 
        /* activate bchannel */
        iframe_t act;
 
        /* activate bchannel */
-       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));
-       end_trace();
        act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST; 
        act.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
        act.dinfo = 0;
        act.len = 0;
        mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
        act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST; 
        act.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
        act.dinfo = 0;
        act.len = 0;
        mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+#endif
+
+       /* 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));
+       end_trace();
 }
 
 
 }
 
 
@@ -499,10 +609,16 @@ static void _bchannel_activate(struct mISDNport *mISDNport, int i, int activate)
 static void _bchannel_configure(struct mISDNport *mISDNport, int i)
 {
        struct PmISDN *port;
 static void _bchannel_configure(struct mISDNport *mISDNport, int i)
 {
        struct PmISDN *port;
-       int addr;
+#ifdef SOCKET_MISDN
+       int handle;
+
+       handle = mISDNport->b_socket[i];
+#else
+       unsigned long handle;
 
 
+       handle = mISDNport->b_addr[i];
+#endif
        port = mISDNport->b_port[i];
        port = mISDNport->b_port[i];
-       addr = mISDNport->b_addr[i];
        if (!port)
        {
                PERROR("bchannel index i=%d not associated with a port object\n", i);
        if (!port)
        {
                PERROR("bchannel index i=%d not associated with a port object\n", i);
@@ -511,27 +627,27 @@ static void _bchannel_configure(struct mISDNport *mISDNport, int i)
 
        /* set dsp features */
        if (port->p_m_txdata)
 
        /* set dsp features */
        if (port->p_m_txdata)
-               ph_control(mISDNport, port, addr, (port->p_m_txdata)?CMX_TXDATA_ON:CMX_TXDATA_OFF, 0, "DSP-TXDATA", 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)
        if (port->p_m_delay)
-               ph_control(mISDNport, port, addr, CMX_DELAY, port->p_m_delay, "DSP-DELAY", 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)
        if (port->p_m_txvol)
-               ph_control(mISDNport, port, addr, VOL_CHANGE_TX, port->p_m_txvol, "DSP-TXVOL", 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)
        if (port->p_m_rxvol)
-               ph_control(mISDNport, port, addr, VOL_CHANGE_RX, port->p_m_rxvol, "DSP-RXVOL", port->p_m_rxvol);
+               ph_control(mISDNport, port, handle, VOL_CHANGE_RX, port->p_m_rxvol, "DSP-RXVOL", port->p_m_rxvol);
        if (port->p_m_conf)
        if (port->p_m_conf)
-               ph_control(mISDNport, port, addr, CMX_CONF_JOIN, port->p_m_conf, "DSP-CONF", 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)
        if (port->p_m_echo)
-               ph_control(mISDNport, port, addr, CMX_ECHO_ON, 0, "DSP-ECHO", 1);
+               ph_control(mISDNport, port, handle, CMX_ECHO_ON, 0, "DSP-ECHO", 1);
        if (port->p_m_tone)
        if (port->p_m_tone)
-               ph_control(mISDNport, port, addr, TONE_PATT_ON, port->p_m_tone, "DSP-TONE", port->p_m_tone);
+               ph_control(mISDNport, port, handle, TONE_PATT_ON, port->p_m_tone, "DSP-TONE", port->p_m_tone);
        if (port->p_m_rxoff)
        if (port->p_m_rxoff)
-               ph_control(mISDNport, port, addr, CMX_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
+               ph_control(mISDNport, port, handle, CMX_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
 //     if (port->p_m_txmix)
 //     if (port->p_m_txmix)
-//             ph_control(mISDNport, port, addr, CMX_MIX_ON, 0, "DSP-MIX", 1);
+//             ph_control(mISDNport, port, handle, CMX_MIX_ON, 0, "DSP-MIX", 1);
        if (port->p_m_dtmf)
        if (port->p_m_dtmf)
-               ph_control(mISDNport, port, addr, DTMF_TONE_START, 0, "DSP-DTMF", 1);
+               ph_control(mISDNport, port, handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
        if (port->p_m_crypt)
        if (port->p_m_crypt)
-               ph_control_block(mISDNport, port, addr, BF_ENABLE_KEY, port->p_m_crypt_key, port->p_m_crypt_key_len, "DSP-CRYPT", port->p_m_crypt_key_len);
+               ph_control_block(mISDNport, port, handle, BF_ENABLE_KEY, port->p_m_crypt_key, port->p_m_crypt_key_len, "DSP-CRYPT", port->p_m_crypt_key_len);
 }
 
 /*
 }
 
 /*
@@ -540,6 +656,17 @@ static void _bchannel_configure(struct mISDNport *mISDNport, int i)
  */
 static void _bchannel_destroy(struct mISDNport *mISDNport, int i)
 {
  */
 static void _bchannel_destroy(struct mISDNport *mISDNport, int i)
 {
+#ifdef SOCKET_MISDN
+       chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
+       add_trace("channel", NULL, "%d", i+1+(i>=15));
+       add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
+       end_trace();
+       if (mISDNport->b_socket[i] > -1)
+       {
+               close(mISDNport->b_socket[i]);
+               mISDNport->b_socket[i] = -1;
+       }
+#else
        unsigned char buff[1024];
 
        chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
        unsigned char buff[1024];
 
        chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
@@ -555,6 +682,7 @@ static void _bchannel_destroy(struct mISDNport *mISDNport, int i)
                mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
                mISDNport->b_addr[i] = 0;
        }
                mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
                mISDNport->b_addr[i] = 0;
        }
+#endif
 }
 
 
 }
 
 
@@ -651,7 +779,11 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
        int state = mISDNport->b_state[i];
        unsigned long p_m_remote_ref = 0;
        unsigned long p_m_remote_id = 0;
        int state = mISDNport->b_state[i];
        unsigned long p_m_remote_ref = 0;
        unsigned long p_m_remote_id = 0;
-       unsigned long addr = mISDNport->b_addr[i];
+#ifdef SOCKET_MISDN
+       unsigned long handle = mISDNport->b_addr[i];
+#else
+       int handle = mISDNport->b_addr[i];
+#endif
 
        if (b_port)
        {
 
        if (b_port)
        {
@@ -671,10 +803,14 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
                        if (p_m_remote_ref)
                        {
                                /* export bchannel */
                        if (p_m_remote_ref)
                        {
                                /* export bchannel */
-                               message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, addr);
+                               message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "assign");
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "assign");
-                               add_trace("stack", "address", "%x", addr);
+#ifdef SOCKET_MISDN
+                               add_trace("stack", "address", "%x", handle);
+#else
+                               add_trace("socket", "id", "%d", handle);
+#endif
                                end_trace();
                                state = B_STATE_EXPORTING;
                                mISDNport->b_remote_id[i] = p_m_remote_id;
                                end_trace();
                                state = B_STATE_EXPORTING;
                                mISDNport->b_remote_id[i] = p_m_remote_id;
@@ -722,10 +858,14 @@ 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. */
                        /* 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, addr);
+                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "assign");
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "assign");
-                       add_trace("stack", "address", "%x", addr);
+#ifdef SOCKET_MISDN
+                       add_trace("stack", "address", "%x", handle);
+#else
+                       add_trace("socket", "id", "%d", handle);
+#endif
                        end_trace();
                        state = B_STATE_EXPORTING;
                        mISDNport->b_remote_id[i] = p_m_remote_id;
                        end_trace();
                        state = B_STATE_EXPORTING;
                        mISDNport->b_remote_id[i] = p_m_remote_id;
@@ -793,10 +933,14 @@ 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 */
                                 * 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, addr);
+                               message_bchannel_to_join(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, handle);
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "remove");
                                chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                add_trace("type", NULL, "remove");
-                               add_trace("stack", "address", "%x", addr);
+#ifdef SOCKET_MISDN
+                               add_trace("stack", "address", "%x", handle);
+#else
+                               add_trace("socket", "id", "%d", handle);
+#endif
                                end_trace();
                                state = B_STATE_IMPORTING;
                        }
                                end_trace();
                                state = B_STATE_IMPORTING;
                        }
@@ -829,10 +973,14 @@ void bchannel_event(struct mISDNport *mISDNport, int i, int event)
 
                        case B_STATE_REMOTE:
                        /* bchannel is exported, so we re-import */
 
                        case B_STATE_REMOTE:
                        /* bchannel is exported, so we re-import */
-                       message_bchannel_to_join(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, addr);
+                       message_bchannel_to_join(mISDNport->b_remote_id[i], 0, BCHANNEL_REMOVE, handle);
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "remove");
                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                        add_trace("type", NULL, "remove");
-                       add_trace("stack", "address", "%x", addr);
+#ifdef SOCKET_MISDN
+                       add_trace("stack", "address", "%x", handle);
+#else
+                       add_trace("socket", "id", "%d", handle);
+#endif
                        end_trace();
                        state = B_STATE_IMPORTING;
                        break;
                        end_trace();
                        state = B_STATE_IMPORTING;
                        break;
@@ -862,10 +1010,14 @@ 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)
                                {
                                /* 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, addr);
+                                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
-                                       add_trace("stack", "address", "%x", addr);
+#ifdef SOCKET_MISDN
+                                       add_trace("stack", "address", "%x", handle);
+#else
+                                       add_trace("socket", "id", "%d", handle);
+#endif
                                        end_trace();
                                        state = B_STATE_EXPORTING;
                                        mISDNport->b_remote_id[i] = p_m_remote_id;
                                        end_trace();
                                        state = B_STATE_EXPORTING;
                                        mISDNport->b_remote_id[i] = p_m_remote_id;
@@ -898,10 +1050,14 @@ 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)
                                {
                                /* 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, addr);
+                                       message_bchannel_to_join(p_m_remote_id, p_m_remote_ref, BCHANNEL_ASSIGN, handle);
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
                                        chan_trace_header(mISDNport, b_port, "MESSAGE_BCHANNEL (to remote application)", DIRECTION_NONE);
                                        add_trace("type", NULL, "assign");
-                                       add_trace("stack", "address", "%x", addr);
+#ifdef SOCKET_MISDN
+                                       add_trace("stack", "address", "%x", handle);
+#else
+                                       add_trace("socket", "id", "%d", handle);
+#endif
                                        end_trace();
                                        state = B_STATE_EXPORTING;
                                        mISDNport->b_remote_id[i] = p_m_remote_id;
                                        end_trace();
                                        state = B_STATE_EXPORTING;
                                        mISDNport->b_remote_id[i] = p_m_remote_id;
@@ -1040,7 +1196,7 @@ void PmISDN::drop_bchannel(void)
 }
 
 /* process bchannel export/import message from join */
 }
 
 /* process bchannel export/import message from join */
-void message_bchannel_from_join(class JoinRemote *joinremote, int type, unsigned long addr)
+void message_bchannel_from_join(class JoinRemote *joinremote, int type, unsigned long handle)
 {
        class Endpoint *epoint;
        class Port *port;
 {
        class Endpoint *epoint;
        class Port *port;
@@ -1106,7 +1262,11 @@ void message_bchannel_from_join(class JoinRemote *joinremote, int type, unsigned
                        ii = mISDNport->b_num;
                        while(i < ii)
                        {
                        ii = mISDNport->b_num;
                        while(i < ii)
                        {
-                               if (mISDNport->b_addr[i] == addr)
+#ifdef SOCKET_MISDN
+                               if (mISDNport->b_socket[i] == handle)
+#else
+                               if (mISDNport->b_addr[i] == handle)
+#endif
                                        break;
                                i++;
                        }
                                        break;
                                i++;
                        }
@@ -1116,7 +1276,7 @@ void message_bchannel_from_join(class JoinRemote *joinremote, int type, unsigned
                }
                if (!mISDNport)
                {
                }
                if (!mISDNport)
                {
-                       PERROR("received assign/remove ack for addr=%x, but address does not exist.\n", addr);
+                       PERROR("received assign/remove ack for handle=%x, but handle does not exist in any mISDNport structure.\n", handle);
                        break;
                }
                /* mISDNport may now be set or NULL */
                        break;
                }
                /* mISDNport may now be set or NULL */
@@ -1226,9 +1386,15 @@ int PmISDN::handler(void)
                if ((p_tone_name[0] || p_m_crypt_msg_loops) && p_m_load < ISDN_LOAD)
                {
                        int tosend = ISDN_LOAD - p_m_load, length; 
                if ((p_tone_name[0] || p_m_crypt_msg_loops) && p_m_load < ISDN_LOAD)
                {
                        int tosend = ISDN_LOAD - p_m_load, length; 
+#ifdef SOCKET_MISDN
+                       unsigned char buf[MISDN_HEADER_LEN+tosend];
+                       struct mISDNhead *frm = (struct mISDNhead *)buf;
+                       unsigned long *d = buf+MISDN_HEADER_LEN;
+#else
                        unsigned char buf[mISDN_HEADER_LEN+tosend];
                        iframe_t *frm = (iframe_t *)buf;
                        unsigned char *p = buf+mISDN_HEADER_LEN;
                        unsigned char buf[mISDN_HEADER_LEN+tosend];
                        iframe_t *frm = (iframe_t *)buf;
                        unsigned char *p = buf+mISDN_HEADER_LEN;
+#endif
 
                        /* copy crypto loops */
                        while (p_m_crypt_msg_loops && tosend)
 
                        /* copy crypto loops */
                        while (p_m_crypt_msg_loops && tosend)
@@ -1263,10 +1429,18 @@ int PmISDN::handler(void)
                        }
 
                        /* send data */
                        }
 
                        /* send data */
+#ifdef SOCKET_MISDN
+                       frm->prim = DL_DATA_REQ;
+                       frm->id = 0;
+                       ret = sendto(handle, buffer, MISDN_HEADER_LEN+ISDN_LOAD-p_m_load-tosend, 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 = ISDN_LOAD - p_m_load - tosend;
                        frm->prim = DL_DATA | REQUEST; 
                        frm->addr = p_m_mISDNport->b_addr[p_m_b_index] | FLG_MSG_DOWN;
                        frm->dinfo = 0;
                        frm->len = ISDN_LOAD - p_m_load - tosend;
+#endif
                        if (frm->len)
                                mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
                        p_m_load += frm->len;
                        if (frm->len)
                                mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
                        p_m_load += frm->len;
@@ -1297,23 +1471,34 @@ int PmISDN::handler(void)
 /*
  * whenever we get audio data from bchannel, we process it here
  */
 /*
  * whenever we get audio data from bchannel, we process it here
  */
+#ifdef SOCKET_MISDN
+void PmISDN::bchannel_receive(mISDNhead *hh, unsigned char *data, int len)
+{
+       unsigned long cont = *((unsigned long *)data);
+#else
 void PmISDN::bchannel_receive(iframe_t *frm)
 {
 void PmISDN::bchannel_receive(iframe_t *frm)
 {
+       int len = frm->len;
+       unsigned long cont = *((unsigned long *)&frm->data.p);
+       unsigned char *data =(unsigned char *)&frm->data.p;
+#endif
        unsigned char *data_temp;
        unsigned long length_temp;
        struct message *message;
        unsigned char *p;
        int l;
        unsigned char *data_temp;
        unsigned long length_temp;
        struct message *message;
        unsigned char *p;
        int l;
-       unsigned long cont;
 
 
+#ifdef SOCKET_MISDN
+       if (hh->prim == PH_CONTROL_IND)
+#else
        if (frm->prim == (PH_CONTROL | INDICATION))
        if (frm->prim == (PH_CONTROL | INDICATION))
+#endif
        {
        {
-               if (frm->len < 4)
+               if (len < 4)
                {
                        PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
                        return;
                }
                {
                        PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
                        return;
                }
-               cont = *((unsigned long *)&frm->data.p);
                if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
                {
                        chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
                if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
                {
                        chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
@@ -1355,7 +1540,7 @@ void PmISDN::bchannel_receive(iframe_t *frm)
                                return;
                        }
                        if (p_record)
                                return;
                        }
                        if (p_record)
-                               record((unsigned char *)(cont+1), frm->len - 4, 1); // from up
+                               record((unsigned char *)(cont+1), len - 4, 1); // from up
                        break;
 
                        default:
                        break;
 
                        default:
@@ -1365,9 +1550,15 @@ void PmISDN::bchannel_receive(iframe_t *frm)
                }
                return;
        }
                }
                return;
        }
+#ifdef SOCKET_MISDN
+       if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND)
+       {
+               PERROR("Bchannel received unknown primitve: 0x%x\n", hh->prim);
+#else
        if (frm->prim != (PH_DATA | INDICATION) && frm->prim != (DL_DATA | INDICATION))
        {
                PERROR("Bchannel received unknown primitve: 0x%x\n", frm->prim);
        if (frm->prim != (PH_DATA | INDICATION) && frm->prim != (DL_DATA | INDICATION))
        {
                PERROR("Bchannel received unknown primitve: 0x%x\n", frm->prim);
+#endif
                return;
        }
 
                return;
        }
 
@@ -1390,26 +1581,26 @@ void PmISDN::bchannel_receive(iframe_t *frm)
 
        /* record data */
        if (p_record)
 
        /* record data */
        if (p_record)
-               record((unsigned char *)&frm->data.p, frm->len, 0); // from down
+               record(data, len, 0); // from down
 
        /* randomize and listen to crypt message if enabled */
        if (p_m_crypt_listen)
        {
                /* the noisy randomizer */
 
        /* randomize and listen to crypt message if enabled */
        if (p_m_crypt_listen)
        {
                /* the noisy randomizer */
-               p = (unsigned char *)&frm->data.p;
-               l = frm->len;
+               p = data;
+               l = len;
                while(l--)
                        mISDN_rand[mISDN_rand_count & 0xff] += *p++;
 
                while(l--)
                        mISDN_rand[mISDN_rand_count & 0xff] += *p++;
 
-               cryptman_listen_bch((unsigned char *)&frm->data.p, frm->len);
+               cryptman_listen_bch(data, len);
        }
 
        }
 
-       p = (unsigned char *)&frm->data.p;
+       p = data;
 
        /* send data to epoint */
        if (p_m_joindata && ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
        {
 
        /* send data to epoint */
        if (p_m_joindata && ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
        {
-               length_temp = frm->len;
+               length_temp = len;
                data_temp = p;
                while(length_temp)
                {
                data_temp = p;
                while(length_temp)
                {
@@ -1437,7 +1628,11 @@ void PmISDN::set_echotest(int echo)
                PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
                if (p_m_b_channel)
                        if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
                PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
                if (p_m_b_channel)
                        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], p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0, "DSP-ECHO", p_m_echo);
+#else
                                ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0, "DSP-ECHO", p_m_echo);
                                ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0, "DSP-ECHO", p_m_echo);
+#endif
        }
 }
 
        }
 }
 
@@ -1462,10 +1657,15 @@ void PmISDN::set_tone(char *dir, char *tone)
        {
                nodsp:
                if (p_m_tone)
        {
                nodsp:
                if (p_m_tone)
+               if (p_m_b_index >= 0)
                if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
                {
                        PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
                if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
                {
                        PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
+#ifdef SOCKET_MISDN
+                       ph_control(p_m_mISDNport, this, p_m_mISDNport->b_socket[p_m_b_index], TONE_PATT_OFF, 0, "DSP-TONE", 0);
+#else
                        ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], TONE_PATT_OFF, 0, "DSP-TONE", 0);
                        ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], TONE_PATT_OFF, 0, "DSP-TONE", 0);
+#endif
                }
                p_m_tone = 0;
                Port::set_tone(dir, tone);
                }
                p_m_tone = 0;
                Port::set_tone(dir, tone);
@@ -1545,9 +1745,13 @@ void PmISDN::set_tone(char *dir, char *tone)
                /* set new tone */
                p_m_tone = id;
                PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
                /* set new tone */
                p_m_tone = id;
                PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
-               if (p_m_b_channel)
-                       if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
-                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone, "DSP-TONE", p_m_tone);
+               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], p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone, "DSP-TONE", p_m_tone);
+#else
+                       ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone, "DSP-TONE", p_m_tone);
+#endif
        }
        /* turn user-space tones off in cases of no tone OR dsp tone */
        Port::set_tone("",NULL);
        }
        /* turn user-space tones off in cases of no tone OR dsp tone */
        Port::set_tone("",NULL);
@@ -1565,18 +1769,26 @@ void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union
                {
                        p_m_txvol = param->mISDNsignal.txvol;
                        PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
                {
                        p_m_txvol = param->mISDNsignal.txvol;
                        PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
-                       if (p_m_b_channel)
-                               if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
-                                       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);
+                       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);
+#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);
+#endif
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have tx-volume shift=%d.\n", p_m_rxvol);
                if (p_m_rxvol != param->mISDNsignal.rxvol)
                {
                        p_m_rxvol = param->mISDNsignal.rxvol;
                        PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have tx-volume shift=%d.\n", p_m_rxvol);
                if (p_m_rxvol != param->mISDNsignal.rxvol)
                {
                        p_m_rxvol = param->mISDNsignal.rxvol;
                        PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
-                       if (p_m_b_channel)
-                               if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
-                                       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);
+                       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);
+#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);
+#endif
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have rx-volume shift=%d.\n", p_m_rxvol);
                break;
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have rx-volume shift=%d.\n", p_m_rxvol);
                break;
@@ -1588,9 +1800,13 @@ void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union
                {
                        p_m_conf = param->mISDNsignal.conf;
                        PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
                {
                        p_m_conf = param->mISDNsignal.conf;
                        PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
-                       if (p_m_b_channel)
-                               if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
-                                       ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf, "DSP-CONF", p_m_conf);
+                       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], (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf, "DSP-CONF", p_m_conf);
+#else
+                               ph_control(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf, "DSP-CONF", p_m_conf);
+#endif
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have conf=%d.\n", p_m_conf);
                /* we must set, even if currently tone forbids conf */
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have conf=%d.\n", p_m_conf);
                /* we must set, even if currently tone forbids conf */
@@ -1612,9 +1828,13 @@ 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);
                {
                        p_m_delay = param->mISDNsignal.delay;
                        PDEBUG(DEBUG_BCHANNEL, "we change delay mode to delay=%d.\n", p_m_delay);
-                       if (p_m_b_channel)
-                               if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
-                                       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);
+                       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], p_m_delay?CMX_DELAY:CMX_JITTER, p_m_delay, "DSP-DELAY", p_m_delay);
+#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
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have delay=%d.\n", p_m_delay);
                break;
                } else
                        PDEBUG(DEBUG_BCHANNEL, "we already have delay=%d.\n", p_m_delay);
                break;
@@ -1645,9 +1865,13 @@ void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parame
                memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
                crypt_off:
                PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
                memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
                crypt_off:
                PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
-               if (p_m_b_channel)
-                       if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
-                               ph_control_block(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len, "DSP-CRYPT", p_m_crypt_key_len);
+               if (p_m_b_index >= 0)
+               if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
+#ifdef SOCKET_MISDN
+                       ph_control_block(p_m_mISDNport, this, p_m_mISDNport->b_socket[p_m_b_index], p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len, "DSP-CRYPT", p_m_crypt_key_len);
+#else
+                       ph_control_block(p_m_mISDNport, this, p_m_mISDNport->b_addr[p_m_b_index], p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len, "DSP-CRYPT", p_m_crypt_key_len);
+#endif
                break;
 
                case CC_DACT_REQ:            /* deactivate session encryption */
                break;
 
                case CC_DACT_REQ:            /* deactivate session encryption */
@@ -1675,10 +1899,14 @@ void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parame
                p_m_crypt_msg_loops = 3; /* enable */
 #if 0
                /* disable txmix, or we get corrupt data due to audio process */
                p_m_crypt_msg_loops = 3; /* enable */
 #if 0
                /* disable txmix, or we get corrupt data due to audio process */
-               if (p_m_txmix)
+               if (p_m_txmix && p_m_b_index>=0)
                {
                        PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
                {
                        PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
+#ifdef SOCKET_MISDN
+                       ph_control(p_m_mISDNport, this, p_mISDNport->b_socket[p_m_b_index], CMX_MIX_OFF, 0, "DSP-TXMIX", 0);
+#else
                        ph_control(p_m_mISDNport, this, p_mISDNport->b_addr[p_m_b_index], CMX_MIX_OFF, 0, "DSP-TXMIX", 0);
                        ph_control(p_m_mISDNport, this, p_mISDNport->b_addr[p_m_b_index], CMX_MIX_OFF, 0, "DSP-TXMIX", 0);
+#endif
                }
 #endif
                break;
                }
 #endif
                break;
@@ -2151,27 +2379,36 @@ int mISDN_handler(void)
 /*
  * global function to add a new card (port)
  */
 /*
  * global function to add a new card (port)
  */
-struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *interface)
+struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, int force_nt, struct interface *interface)
 {
        int ret;
        unsigned char buff[1025];
        iframe_t *frm = (iframe_t *)buff;
        struct mISDNport *mISDNport, **mISDNportp;
        int i, cnt;
 {
        int ret;
        unsigned char buff[1025];
        iframe_t *frm = (iframe_t *)buff;
        struct mISDNport *mISDNport, **mISDNportp;
        int i, cnt;
-       int pri = 0;
-       int nt = 0;
+       int pri, bri, ports;
+       int nt, te;
 #ifdef SOCKET_MISDN
 #ifdef SOCKET_MISDN
-//     struct mlayer3 *layer3;
+       struct mlayer3 *ml3;
+       struct mISDN_devinfo devinfo;
+
+       ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
+       if (ret < 0)
+       {
+               fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
+               return(NULL);
+       }
 #else
 //     interface_info_t ii;
        net_stack_t *nst;
        manager_t *mgr;
        layer_info_t li;
        stack_info_t *stinf;
 #else
 //     interface_info_t ii;
        net_stack_t *nst;
        manager_t *mgr;
        layer_info_t li;
        stack_info_t *stinf;
-#endif
 
        /* query port's requirements */
        cnt = mISDN_get_stack_count(mISDNdevice);
 
        /* query port's requirements */
        cnt = mISDN_get_stack_count(mISDNdevice);
+#endif
+
        if (cnt <= 0)
        {
                PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
        if (cnt <= 0)
        {
                PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
@@ -2182,6 +2419,87 @@ struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *
                PERROR_RUNTIME("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
                return(NULL);
        }
                PERROR_RUNTIME("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
                return(NULL);
        }
+
+       pri = bri = ports = nt = te = 0;
+#ifdef SOCKET_MISDN
+       devinfo.id = port - 1;
+       ret = ioctl(socket, IMGETDEVINFO, &devinfo);
+       if (ret <= 0)
+       {
+               PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", i, ret);
+               break;
+       }
+       /* output the port info */
+       if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0))
+       {
+               bri = 1;
+               te = 1;
+       }
+       if (devinfo.Dprotocols & (1 << ISDN_P_NT_S0))
+       {
+               bri = 1;
+               nt = 1;
+       }
+#ifdef ISDN_P_TE_E1
+       if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1))
+       {
+               pri = 1;
+               te = 1;
+       }
+#endif
+#ifdef ISDN_P_NT_E1
+       if (devinfo.Dprotocols & (1 << ISDN_P_NT_E1))
+       {
+               pri = 1;
+               nt = 1;
+       }
+#endif
+#ifdef ISDN_P_FXS
+       if (devinfo.Dprotocols & (1 << ISDN_P_FXS))
+       {
+               pots = 1;
+               te = 1;
+       }
+#endif
+#ifdef ISDN_P_FXO
+       if (devinfo.Dprotocols & (1 << ISDN_P_FXO))
+       {
+               pots = 1;
+               nt = 1;
+       }
+#endif
+       if (force_nt && !nt)
+       {
+               PERROR_RUNTIME("Port %d does not support NT-mode.\n", port);
+               return(NULL);
+       }
+       if (bri && pri)
+       {
+               PERROR_RUNTIME("Port %d supports BRI and PRI?? What kind of controller is that?. (Can't use this!)\n", port);
+               return(NULL);
+       }
+       if (pots && !bri && !pri)
+       {
+               PERROR_RUNTIME("Port %d supports POTS, LCR does not!\n", port);
+               return(NULL);
+       }
+       if (!bri && !pri)
+       {
+               PERROR_RUNTIME("Port %d does not support BRI nor PRI!\n", port);
+               return(NULL);
+       }
+       if (!nt && !te)
+       {
+               PERROR_RUNTIME("Port %d does not support NT-mode nor TE-mode!\n", port);
+               return(NULL);
+       }
+       /* force nt, by turning off TE */
+       if (force_nt && nt)
+               te = 0;
+       /* if TE an NT is supported (and not forced to NT), turn off NT */
+       if (te && nt)
+               nt = 0;
+#else
        ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
        if (ret < 0)
        {
        ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
        if (ret < 0)
        {
@@ -2224,7 +2542,8 @@ struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *
                        PERROR_RUNTIME("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
                        return(NULL);
                }
                        PERROR_RUNTIME("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
                        return(NULL);
                }
-       } else
+       }
+       if (te)
        {
                /* TE */
                if (stinf->pid.protocol[1] == 0)
        {
                /* TE */
                if (stinf->pid.protocol[1] == 0)
@@ -2259,6 +2578,7 @@ struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *
                        return(NULL);
                }
        }
                        return(NULL);
                }
        }
+#endif
 
        /* add mISDNport structure */
        mISDNportp = &mISDNport_first;
 
        /* add mISDNport structure */
        mISDNportp = &mISDNport_first;
@@ -2275,16 +2595,16 @@ struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *
        prop = 0;
        if (ptp)
               prop |= FLG_PTP;
        prop = 0;
        if (ptp)
               prop |= FLG_PTP;
-       if (ptp)
+       if (ptmp && pri)
               prop |= FLG_FORCE_PTMP;
               prop |= FLG_FORCE_PTMP;
-       mISDNport->layer3 = open_layer3(port-1, protocol, prop , do_dchannel, mISDNport);
-       if (!mISDNport->layer3)
+       mISDNport->ml3 = open_layer3(port-1, protocol, prop , do_dchannel, mISDNport);
+       if (!mISDNport->ml3)
        {
                PERROR_RUNTIME("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
                return(NULL);
        }
 
        {
                PERROR_RUNTIME("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
                return(NULL);
        }
 
-#warning KKEIL: braucht man das noch?
+#if 0
        /* if ntmode, establish L1 to send the tei removal during start */
        if (mISDNport->ntmode)
        {
        /* if ntmode, establish L1 to send the tei removal during start */
        if (mISDNport->ntmode)
        {
@@ -2298,7 +2618,10 @@ struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *
                mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
                usleep(10000); /* to be sure, that l1 is up */
        }
                mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
                usleep(10000); /* to be sure, that l1 is up */
        }
+#endif
 
 
+       SCPY(mISDNport->name, devinfo.name);
+       mISDNport->b_num = devinfo.nrbchan;
 #else
        msg_queue_init(&mISDNport->downqueue);
        mISDNport->d_stid = stinf->id;
 #else
        msg_queue_init(&mISDNport->downqueue);
        mISDNport->d_stid = stinf->id;
@@ -2402,20 +2725,23 @@ struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *
                Isdnl3Init(nst);
        }
 
                Isdnl3Init(nst);
        }
 
+       mISDNport->b_num = stinf->childcnt;
 #endif
 #endif
-//     SCPY(mISDNport->name, "noname");
        mISDNport->portnum = port;
        mISDNport->ntmode = nt;
        mISDNport->pri = pri;
        mISDNport->ptp = ptp;
        mISDNport->portnum = port;
        mISDNport->ntmode = nt;
        mISDNport->pri = pri;
        mISDNport->ptp = ptp;
-       mISDNport->b_num = stinf->childcnt;
        PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
        i = 0;
        PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
        i = 0;
-       while(i < stinf->childcnt)
+       while(i < mISDNport->b_num)
        {
        {
-               mISDNport->b_stid[i] = stinf->child[i];
                mISDNport->b_state[i] = B_STATE_IDLE;
                mISDNport->b_state[i] = B_STATE_IDLE;
+#ifdef SOCKET_MISDN
+               mISDNport->b_socket = -1;
+#else
+               mISDNport->b_stid[i] = stinf->child[i];
                PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
                PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
+#endif
                i++;
        }
 
                i++;
        }
 
@@ -2534,6 +2860,9 @@ void mISDNport_close(struct mISDNport *mISDNport)
                i++;
        }
 
                i++;
        }
 
+#ifdef SOCKET_MISDN
+       close_layer3(mISDNport->ml3);
+#else
        /* free ressources of port */
        msg_queue_purge(&mISDNport->downqueue);
 
        /* free ressources of port */
        msg_queue_purge(&mISDNport->downqueue);
 
@@ -2560,6 +2889,7 @@ void mISDNport_close(struct mISDNport *mISDNport)
                if (mISDNport->upper_id)
                        mISDN_write_frame(mISDNdevice, buf, mISDNport->upper_id | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
        }
                if (mISDNport->upper_id)
                        mISDN_write_frame(mISDNdevice, buf, mISDNport->upper_id | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
        }
+#endif
 
        /* remove from list */
        mISDNportp = &mISDNport_first;
 
        /* remove from list */
        mISDNportp = &mISDNport_first;
@@ -2588,79 +2918,171 @@ void mISDNport_close(struct mISDNport *mISDNport)
  */
 void mISDN_port_info(void)
 {
  */
 void mISDN_port_info(void)
 {
-       int err;
+       int ret;
        int i, ii, p;
        int i, ii, p;
-       int useable, nt, pri;
+       int useable, nt, te, pri, bri, pots;
        unsigned char buff[1025];
        iframe_t *frm = (iframe_t *)buff;
        unsigned char buff[1025];
        iframe_t *frm = (iframe_t *)buff;
+#ifdef SOCKET_MISDN
+       struct mISDN_devinfo devinfo;
+       int socket;
+
+       /* open mISDN */
+       socket = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
+       if (socket < 0)
+       {
+               fprintf(stderr, "Cannot open mISDN due to %s. (Does your Kernel support socket based mISDN?)\n", strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       /* get number of stacks */
+       i = 1;
+       ret = ioctl(socket, IMGETCOUNT, &ii);
+       if (ret < 0)
+       {
+               fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
+               goto done;
+       }
+#else
        stack_info_t *stinf;
        int device;
 
        /* open mISDN */
        if ((device = mISDN_open()) < 0)
        {
        stack_info_t *stinf;
        int device;
 
        /* open mISDN */
        if ((device = mISDN_open()) < 0)
        {
-               fprintf(stderr, "cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", device, errno, strerror(errno));
+               fprintf(stderr, "Cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", device, errno, strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        /* get number of stacks */
        i = 1;
        ii = mISDN_get_stack_count(device);
                exit(EXIT_FAILURE);
        }
 
        /* get number of stacks */
        i = 1;
        ii = mISDN_get_stack_count(device);
+#endif
        printf("\n");
        if (ii <= 0)
        {
                printf("Found no card. Please be sure to load card drivers.\n");
        printf("\n");
        if (ii <= 0)
        {
                printf("Found no card. Please be sure to load card drivers.\n");
+               goto done;
        }
 
        /* loop the number of cards and get their info */
        while(i <= ii)
        {
        }
 
        /* loop the number of cards and get their info */
        while(i <= ii)
        {
-               err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
-               if (err <= 0)
+               nt = te = bri = pri = pots = 0;
+               useable = 0;
+
+#ifdef SOCKET_MISDN
+               devinfo.id = i - 1;
+               ret = ioctl(socket, IMGETDEVINFO, &devinfo);
+               if (ret <= 0)
                {
                {
-                       fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
+                       fprintf(stderr, "Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", i, ret);
                        break;
                }
                        break;
                }
-               stinf = (stack_info_t *)&frm->data.p;
 
 
-               nt = pri = 0;
-               useable = 1;
+               /* output the port info */
+               printf("Port %2d name='%s': ", i, devinfo.name);
+               if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0))
+               {
+                       bri = 1;
+                       te = 1;
+               }
+               if (devinfo.Dprotocols & (1 << ISDN_P_NT_S0))
+               {
+                       bri = 1;
+                       nt = 1;
+               }
+#ifdef ISDN_P_TE_E1
+               if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1))
+               {
+                       pri = 1;
+                       te = 1;
+               }
+#endif
+#ifdef ISDN_P_NT_E1
+               if (devinfo.Dprotocols & (1 << ISDN_P_NT_E1))
+               {
+                       pri = 1;
+                       nt = 1;
+               }
+#endif
+#ifdef ISDN_P_FXS
+               if (devinfo.Dprotocols & (1 << ISDN_P_FXS))
+               {
+                       pots = 1;
+                       te = 1;
+               }
+#endif
+#ifdef ISDN_P_FXO
+               if (devinfo.Dprotocols & (1 << ISDN_P_FXO))
+               {
+                       pots = 1;
+                       nt = 1;
+               }
+#endif
+               if ((te || nt) && (bri || pri || ports))
+                       usable = 1;
+
+               if (te && bri)
+                       printf("TE-mode BRI S/T interface line (for phone lines)");
+               if (nt && bri)
+                       printf("NT-mode BRI S/T interface port (for phones)");
+               if (te && pri)
+                       printf("TE-mode PRI E1  interface line (for phone lines)");
+               if (nt && pri)
+                       printf("NT-mode PRI E1  interface port (for E1 terminals)");
+               if (te && pots)
+                       printf("FXS     POTS    interface port (for analog lines)");
+               if (nt && pots)
+                       printf("FXO     POTS    interface port (for analog phones)");
+               if (ports)
+               {
+                       usable = 0;
+                       printf("\n -> Analog interfaces are not supported.");
+               } else
+               if (!usable)
+               {
+                       printf("unsupported interface protocol bits 0x%016x", devinfo.Dprotocols);
+               }
+               printf("\n");
+
+               printf("  - %d B-channels\n", devinfo.nfbchan);
+#else
+               ret = mISDN_get_stack_info(device, i, buff, sizeof(buff));
+               if (ret <= 0)
+               {
+                       fprintf(stderr, "mISDN_get_stack_info() failed: port=%d error=%d\n", i, ret);
+                       break;
+               }
+               stinf = (stack_info_t *)&frm->data.p;
 
                /* output the port info */
                printf("Port %2d: ", i);
                switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
                {
                        case ISDN_PID_L0_TE_S0:
 
                /* output the port info */
                printf("Port %2d: ", i);
                switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
                {
                        case ISDN_PID_L0_TE_S0:
+                       useable = 1;
+                       te = 1;
+                       bri = 1;
                        printf("TE-mode BRI S/T interface line (for phone lines)");
                        printf("TE-mode BRI S/T interface line (for phone lines)");
-#if 0
-                       if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
-                               printf(" HFC multiport card");
-#endif
                        break;
                        case ISDN_PID_L0_NT_S0:
                        break;
                        case ISDN_PID_L0_NT_S0:
+                       useable = 1;
                        nt = 1;
                        nt = 1;
+                       bri = 1;
                        printf("NT-mode BRI S/T interface port (for phones)");
                        printf("NT-mode BRI S/T interface port (for phones)");
-#if 0
-                       if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
-                               printf(" HFC multiport card");
-#endif
                        break;
                        case ISDN_PID_L0_TE_E1:
                        break;
                        case ISDN_PID_L0_TE_E1:
+                       useable = 1;
+                       te = 1;
                        pri = 1;
                        printf("TE-mode PRI E1  interface line (for phone lines)");
                        pri = 1;
                        printf("TE-mode PRI E1  interface line (for phone lines)");
-#if 0
-                       if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
-                               printf(" HFC-E1 card");
-#endif
                        break;
                        case ISDN_PID_L0_NT_E1:
                        break;
                        case ISDN_PID_L0_NT_E1:
+                       useable = 1;
                        nt = 1;
                        pri = 1;
                        nt = 1;
                        pri = 1;
-                       printf("NT-mode PRI E1  interface port (for phones)");
-#if 0
-                       if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
-                               printf(" HFC-E1 card");
-#endif
+                       printf("NT-mode PRI E1  interface port (for E1 terminals)");
                        break;
                        default:
                        useable = 0;
                        break;
                        default:
                        useable = 0;
@@ -2691,7 +3113,8 @@ void mISDN_port_info(void)
                                else
                                        printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
                        }
                                else
                                        printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
                        }
-               } else
+               }
+               if (te)
                {
                        if (stinf->pid.protocol[1] == 0)
                        {
                {
                        if (stinf->pid.protocol[1] == 0)
                        {
@@ -2737,6 +3160,7 @@ void mISDN_port_info(void)
                        }
                }
                printf("  - %d B-channels\n", stinf->childcnt);
                        }
                }
                printf("  - %d B-channels\n", stinf->childcnt);
+#endif
 
                if (!useable)
                        printf(" * Port NOT useable for LCR\n");
 
                if (!useable)
                        printf(" * Port NOT useable for LCR\n");
@@ -2747,9 +3171,14 @@ void mISDN_port_info(void)
        }
        printf("\n");
 
        }
        printf("\n");
 
+done:
+#ifdef SOCKET_MISDN
+       close(sd);
+#else
        /* close mISDN */
        /* close mISDN */
-       if ((err = mISDN_close(device)))
-               FATAL("mISDN_close() failed: err=%d '%s'\n", err, strerror(err));
+       if ((ret = mISDN_close(device)))
+               FATAL("mISDN_close() failed: err=%d '%s'\n", ret, strerror(ret));
+#endif
 }
 
 
 }
 
 
diff --git a/mISDN.h b/mISDN.h
index 32ed6d8..47626f1 100644 (file)
--- a/mISDN.h
+++ b/mISDN.h
@@ -17,13 +17,22 @@ extern int mISDNdevice;
 
 /* mISDN port structure list */
 struct mISDNport {
 
 /* mISDN port structure list */
 struct mISDNport {
+#ifdef SOCKET_MISDN
+       struct mlayer3 *ml3;
+#else
        net_stack_t nst; /* MUST be the first entry, so &nst equals &mISDNlist */
        manager_t mgr;
        net_stack_t nst; /* MUST be the first entry, so &nst equals &mISDNlist */
        manager_t mgr;
+       int upper_id; /* id to transfer data down */
+       int lower_id; /* id to transfer data up */
+       int d_stid;
+       msg_queue_t downqueue;          /* l4->l3 */
+#endif
        struct mISDNport *next;
        struct mISDNport *next;
+       char name[64]; /* name of port, if available */
        struct interface_port *ifport; /* link to interface_port */
 //     int iftype; /* IF_* */
 //     int multilink; /* if set, this port will not support callwaiting */
        struct interface_port *ifport; /* link to interface_port */
 //     int iftype; /* IF_* */
 //     int multilink; /* if set, this port will not support callwaiting */
-       int portnum; /* port number */
+       int portnum; /* port number 1..n */
        int ptp; /* if ptp is set, we keep track of l2link */
        int l1link; /* if l1 is available (only works with nt-mode) */
        int l2link; /* if l2 is available (at PTP we take this serious) */
        int ptp; /* if ptp is set, we keep track of l2link */
        int l1link; /* if l1 is available (only works with nt-mode) */
        int l2link; /* if l2 is available (at PTP we take this serious) */
@@ -34,20 +43,20 @@ struct mISDNport {
        int pri; /* is TRUE if port is a primary rate interface */
        int tones; /* TRUE if tones are sent outside connect state */
        int earlyb; /* TRUE if tones are received outside connect state */
        int pri; /* is TRUE if port is a primary rate interface */
        int tones; /* TRUE if tones are sent outside connect state */
        int earlyb; /* TRUE if tones are received outside connect state */
-       int upper_id; /* id to transfer data down */
-       int lower_id; /* id to transfer data up */
-       int d_stid;
        int b_num; /* number of bchannels */
        int b_reserved; /* number of bchannels reserved or in use */
        class PmISDN *b_port[128]; /* bchannel assigned to port object */
        int b_num; /* number of bchannels */
        int b_reserved; /* number of bchannels reserved or in use */
        class PmISDN *b_port[128]; /* bchannel assigned to port object */
+#ifdef SOCKET_MISDN
+       int b_socket[128];
+#else
        int b_stid[128];
        unsigned long b_addr[128];
        int b_stid[128];
        unsigned long b_addr[128];
+#endif
        int b_state[128]; /* statemachine, 0 = IDLE */
        unsigned long b_remote_id[128]; /* the socket currently exported */
        unsigned long b_remote_ref[128]; /* the ref currently exported */
        int procids[128]; /* keep track of free ids */
        int locally; /* local causes are sent as local causes not remote */
        int b_state[128]; /* statemachine, 0 = IDLE */
        unsigned long b_remote_id[128]; /* the socket currently exported */
        unsigned long b_remote_ref[128]; /* the ref currently exported */
        int procids[128]; /* keep track of free ids */
        int locally; /* local causes are sent as local causes not remote */
-       msg_queue_t downqueue;          /* l4->l3 */
 };
 extern mISDNport *mISDNport_first;
 
 };
 extern mISDNport *mISDNport_first;
 
@@ -70,14 +79,14 @@ calls with no bchannel (call waiting, call on hold).
 int mISDN_initialize(void);
 void mISDN_deinitialize(void);
 void mISDN_port_info(void);
 int mISDN_initialize(void);
 void mISDN_deinitialize(void);
 void mISDN_port_info(void);
-struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, struct interface *interface);
+struct mISDNport *mISDNport_open(int port, int ptp, int ptmp, int force_nt, struct interface *interface);
 void mISDNport_close_all(void);
 void mISDNport_close(struct mISDNport *mISDNport);
 void mISDN_port_reorder(void);
 int mISDN_handler(void);
 void enc_ie_cause_standalone(unsigned char **ntmode, msg_t *msg, int location, int cause);
 void mISDNport_close_all(void);
 void mISDNport_close(struct mISDNport *mISDNport);
 void mISDN_port_reorder(void);
 int mISDN_handler(void);
 void enc_ie_cause_standalone(unsigned char **ntmode, msg_t *msg, int location, int cause);
-void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, unsigned long b_addr, int c1, int c2, char *trace_name, int trace_value);
-void ph_control_block(struct mISDNport *mISDNport, unsigned long b_addr, int c1, void *c2, int c2_len, char *trace_name, int trace_value);
+void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value);
+void ph_control_block(struct mISDNport *mISDNport, unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value);
 msg_t *create_l2msg(int prim, int dinfo, int size);
 void setup_queue(struct mISDNport *mISDNport, int link);
 int stack2manager_nt(void *dat, void *arg);
 msg_t *create_l2msg(int prim, int dinfo, int size);
 void setup_queue(struct mISDNport *mISDNport, int link);
 int stack2manager_nt(void *dat, void *arg);
@@ -85,7 +94,7 @@ int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg);
 void chan_trace_header(struct mISDNport *mISDNport, class PmISDN *port, char *msgtext, int direction);
 void l1l2l3_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned long prim, int direction);
 void bchannel_event(struct mISDNport *mISDNport, int i, int event);
 void chan_trace_header(struct mISDNport *mISDNport, class PmISDN *port, char *msgtext, int direction);
 void l1l2l3_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned long prim, int direction);
 void bchannel_event(struct mISDNport *mISDNport, int i, int event);
-void message_bchannel_from_join(class JoinRemote *joinremote, int type, unsigned long addr);
+void message_bchannel_from_join(class JoinRemote *joinremote, int type, unsigned long handle);
 
 
 /* mISDN port classes */
 
 
 /* mISDN port classes */
diff --git a/main.c b/main.c
index 1561f9e..38c710a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -44,6 +44,10 @@ struct timezone now_tz;
 int global_debug = 0;
 int quit=0;
 
 int global_debug = 0;
 int quit=0;
 
+#if 0
+struct lcr_fdset lcr_fdset[FD_SETSIZE];
+#endif
+
 pthread_mutex_t mutexd; // debug output mutex
 //pthread_mutex_t mutext; // trace output mutex
 pthread_mutex_t mutexe; // error output mutex
 pthread_mutex_t mutexd; // debug output mutex
 //pthread_mutex_t mutext; // trace output mutex
 pthread_mutex_t mutexe; // error output mutex
@@ -200,6 +204,11 @@ int main(int argc, char *argv[])
        int                     idletime = 0, idlecheck = 0;
        char                    tracetext[256];
 
        int                     idletime = 0, idlecheck = 0;
        char                    tracetext[256];
 
+#if 0
+       /* init fdset */
+       memset(lcr_fdset, 0, sizeof(lcr_fdset));
+#endif
+
        /* current time */
        GET_NOW();
 
        /* current time */
        GET_NOW();
 
diff --git a/main.h b/main.h
index 4ea51b0..954c6cb 100644 (file)
--- a/main.h
+++ b/main.h
@@ -154,4 +154,10 @@ extern struct timezone now_tz;
 #define DIRECTION_OUT  1
 #define DIRECTION_IN   2
 
 #define DIRECTION_OUT  1
 #define DIRECTION_IN   2
 
+#if 0
+struct lcr_fdset {
+       struct mISDNport *mISDNport;
+       int b_index;
+};
+#endif
 
 
index 77e68bb..9482040 100644 (file)
--- a/message.h
+++ b/message.h
@@ -307,7 +307,7 @@ struct param_hello {
 
 struct param_bchannel {
        int type; /* BCHANNEL_* */
 
 struct param_bchannel {
        int type; /* BCHANNEL_* */
-       unsigned long addr; /* bchannel stack address */
+       unsigned long handle; /* bchannel stack/socket */
 };
 
 /* structure of message parameter */
 };
 
 /* structure of message parameter */