unified socket application interface (for asterisk and maybe other apps)
[lcr.git] / interface.c
index 2fe7b2c..fa7270d 100644 (file)
@@ -23,14 +23,8 @@ void default_out_channel(struct interface_port *ifport)
 {
        struct select_channel *selchannel, **selchannelp;
 
 {
        struct select_channel *selchannel, **selchannelp;
 
-       selchannel = (struct select_channel *)malloc(sizeof(struct select_channel));
-       if (!selchannel)
-       {
-               PERROR("No memory!");
-               return;
-       }
+       selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
        memuse++;
        memuse++;
-       memset(selchannel, 0, sizeof(struct select_channel));
        
        if (ifport->mISDNport->ntmode)
                selchannel->channel = CHANNEL_FREE;
        
        if (ifport->mISDNport->ntmode)
                selchannel->channel = CHANNEL_FREE;
@@ -43,14 +37,8 @@ void default_out_channel(struct interface_port *ifport)
        if (!ifport->mISDNport->ptp && ifport->mISDNport->ntmode)
        {
                selchannelp = &(selchannel->next);
        if (!ifport->mISDNport->ptp && ifport->mISDNport->ntmode)
        {
                selchannelp = &(selchannel->next);
-               selchannel = (struct select_channel *)malloc(sizeof(struct select_channel));
-               if (!selchannel)
-               {
-                       PERROR("No memory!");
-                       return;
-               }
+               selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
                memuse++;
                memuse++;
-               memset(selchannel, 0, sizeof(struct select_channel));
                selchannel->channel = CHANNEL_NO; // call waiting
                *selchannelp = selchannel;
        }
                selchannel->channel = CHANNEL_NO; // call waiting
                *selchannelp = selchannel;
        }
@@ -62,14 +50,8 @@ void default_in_channel(struct interface_port *ifport)
 {
        struct select_channel *selchannel;
 
 {
        struct select_channel *selchannel;
 
-       selchannel = (struct select_channel *)malloc(sizeof(struct select_channel));
-       if (!selchannel)
-       {
-               PERROR("No memory!");
-               return;
-       }
+       selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
        memuse++;
        memuse++;
-       memset(selchannel, 0, sizeof(struct select_channel));
        
        selchannel->channel = CHANNEL_FREE;
        
        
        selchannel->channel = CHANNEL_FREE;
        
@@ -256,7 +238,6 @@ static int inter_port(struct interface *interface, char *filename, int line, cha
 {
        struct interface_port *ifport, **ifportp;
        struct interface *searchif;
 {
        struct interface_port *ifport, **ifportp;
        struct interface *searchif;
-       struct interface_port *searchport;
        int val;
 
        val = get_number(value);
        int val;
 
        val = get_number(value);
@@ -269,7 +250,7 @@ static int inter_port(struct interface *interface, char *filename, int line, cha
        searchif = interface_newlist;
        while(searchif)
        {
        searchif = interface_newlist;
        while(searchif)
        {
-               searchport = searchif->ifport;
+               ifport = searchif->ifport;
                while(ifport)
                {
                        if (ifport->portnum == val)
                while(ifport)
                {
                        if (ifport->portnum == val)
@@ -282,14 +263,8 @@ static int inter_port(struct interface *interface, char *filename, int line, cha
                searchif = searchif->next;
        }
        /* alloc port substructure */
                searchif = searchif->next;
        }
        /* alloc port substructure */
-       ifport = (struct interface_port *)malloc(sizeof(struct interface_port));
-       if (!ifport)
-       {
-               SPRINT(interface_error, "No memory!");
-               return(-1);
-       }
+       ifport = (struct interface_port *)MALLOC(sizeof(struct interface_port));
        memuse++;
        memuse++;
-       memset(ifport, 0, sizeof(struct interface_port));
        ifport->interface = interface;
        /* set value */
        ifport->portnum = val;
        ifport->interface = interface;
        /* set value */
        ifport->portnum = val;
@@ -361,14 +336,8 @@ static int inter_channel_out(struct interface *interface, char *filename, int li
                        }
                        selchannel:
                        /* add to select-channel list */
                        }
                        selchannel:
                        /* add to select-channel list */
-                       selchannel = (struct select_channel *)malloc(sizeof(struct select_channel));
-                       if (!selchannel)
-                       {
-                               SPRINT(interface_error, "No memory!");
-                               return(-1);
-                       }
+                       selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
                        memuse++;
                        memuse++;
-                       memset(selchannel, 0, sizeof(struct select_channel));
                        /* set value */
                        selchannel->channel = val;
                        /* tail port */
                        /* set value */
                        selchannel->channel = val;
                        /* tail port */
@@ -427,14 +396,8 @@ static int inter_channel_in(struct interface *interface, char *filename, int lin
                        }
                        selchannel:
                        /* add to select-channel list */
                        }
                        selchannel:
                        /* add to select-channel list */
-                       selchannel = (struct select_channel *)malloc(sizeof(struct select_channel));
-                       if (!selchannel)
-                       {
-                               SPRINT(interface_error, "No memory!");
-                               return(-1);
-                       }
+                       selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
                        memuse++;
                        memuse++;
-                       memset(selchannel, 0, sizeof(struct select_channel));
                        /* set value */
                        selchannel->channel = val;
                        /* tail port */
                        /* set value */
                        selchannel->channel = val;
                        /* tail port */
@@ -469,14 +432,8 @@ static int inter_msn(struct interface *interface, char *filename, int line, char
                el = p;
                p = get_seperated(p);
                /* add MSN to list */
                el = p;
                p = get_seperated(p);
                /* add MSN to list */
-               ifmsn = (struct interface_msn *)malloc(sizeof(struct interface_msn));
-               if (!ifmsn)
-               {
-                       SPRINT(interface_error, "No memory!");
-                       return(-1);
-               }
+               ifmsn = (struct interface_msn *)MALLOC(sizeof(struct interface_msn));
                memuse++;
                memuse++;
-               memset(ifmsn, 0, sizeof(struct interface_msn));
                /* set value */
                SCPY(ifmsn->msn, el);
                /* tail port */
                /* set value */
                SCPY(ifmsn->msn, el);
                /* tail port */
@@ -501,14 +458,8 @@ static int inter_screen(struct interface_screen **ifscreenp, struct interface *i
        el = p;
        p = get_seperated(p);
        /* add screen entry to list*/
        el = p;
        p = get_seperated(p);
        /* add screen entry to list*/
-       ifscreen = (struct interface_screen *)malloc(sizeof(struct interface_screen));
-       if (!ifscreen)
-       {
-               SPRINT(interface_error, "No memory!");
-               return(-1);
-       }
+       ifscreen = (struct interface_screen *)MALLOC(sizeof(struct interface_screen));
        memuse++;
        memuse++;
-       memset(ifscreen, 0, sizeof(struct interface_screen));
        ifscreen->match_type = -1; /* unchecked */
        ifscreen->match_present = -1; /* unchecked */
        ifscreen->result_type = -1; /* unchanged */
        ifscreen->match_type = -1; /* unchecked */
        ifscreen->match_present = -1; /* unchecked */
        ifscreen->result_type = -1; /* unchanged */
@@ -717,7 +668,7 @@ struct interface_param interface_param[] = {
        "This is required on PRI NT-mode ports that are point-to-point by default.\n"
        "This parameter must follow a 'port' parameter."},
 
        "This is required on PRI NT-mode ports that are point-to-point by default.\n"
        "This parameter must follow a 'port' parameter."},
 
-       {"channel_out", &inter_channel_out, "[force,][<number>][,...][,free][,any][,no]",
+       {"channel-out", &inter_channel_out, "[force,][<number>][,...][,free][,any][,no]",
        "Channel selection list for all outgoing calls to the interface.\n"
        "A free channels is searched in order of appearance.\n"
        "This parameter must follow a 'port' parameter.\n"
        "Channel selection list for all outgoing calls to the interface.\n"
        "A free channels is searched in order of appearance.\n"
        "This parameter must follow a 'port' parameter.\n"
@@ -727,10 +678,10 @@ struct interface_param interface_param[] = {
        " any - On outgoing calls, signal 'any channel acceptable'. (see DSS1)\n"
        " no - Signal 'no channel available' aka 'call waiting'. (see DSS1)"},
 
        " any - On outgoing calls, signal 'any channel acceptable'. (see DSS1)\n"
        " no - Signal 'no channel available' aka 'call waiting'. (see DSS1)"},
 
-       {"channel_in", &inter_channel_in, "[force,][<number>][,...][,free][,any][,no]",
+       {"channel-in", &inter_channel_in, "[force,][<number>][,...][,free][,any][,no]",
        "Channel selection list for all incomming calls from the interface.\n"
        "A free channels is accepted if in the list.\n"
        "Channel selection list for all incomming calls from the interface.\n"
        "A free channels is accepted if in the list.\n"
-       "If no channel was requested, the first free channel found is selected.\n"
+       "If any channel was requested, the first free channel found is selected.\n"
        "This parameter must follow a 'port' parameter.\n"
        " <number>[,...] - List of channels to accept.\n"
        " free - Accept any free channel"},
        "This parameter must follow a 'port' parameter.\n"
        " <number>[,...] - List of channels to accept.\n"
        " free - Accept any free channel"},
@@ -789,10 +740,7 @@ struct interface *read_interfaces(void)
        struct interface_param  *ifparam;
 
        if (interface_newlist != NULL)
        struct interface_param  *ifparam;
 
        if (interface_newlist != NULL)
-       {
-               PERROR("software error, list is not empty.\n");
-               exit(-1);
-       }
+               FATAL("list is not empty.\n");
        interface_error[0] = '\0';
        SPRINT(filename, "%s/interface.conf", INSTALL_DATA);
 
        interface_error[0] = '\0';
        SPRINT(filename, "%s/interface.conf", INSTALL_DATA);
 
@@ -883,7 +831,7 @@ struct interface *read_interfaces(void)
                        parameter[strlen(parameter)-1] = '\0';
 
                        /* check if interface name already exists */
                        parameter[strlen(parameter)-1] = '\0';
 
                        /* check if interface name already exists */
-                       interface = interface_first;
+                       interface = interface_newlist;
                        while(interface)
                        {
                                if (!strcasecmp(interface->name, parameter+1))
                        while(interface)
                        {
                                if (!strcasecmp(interface->name, parameter+1))
@@ -895,14 +843,8 @@ struct interface *read_interfaces(void)
                        }
 
                        /* append interface to new list */
                        }
 
                        /* append interface to new list */
-                       interface = (struct interface *)malloc(sizeof(struct interface));
-                       if (!interface)
-                       {
-                               SPRINT(interface_error, "No memory!");
-                               goto error;
-                       }
+                       interface = (struct interface *)MALLOC(sizeof(struct interface));
                        memuse++;
                        memuse++;
-                       memset(interface, 0, sizeof(struct interface));
 
                        /* name interface */
                        SCPY(interface->name, parameter+1);
 
                        /* name interface */
                        SCPY(interface->name, parameter+1);
@@ -921,10 +863,12 @@ struct interface *read_interfaces(void)
                        {
                                if (ifparam->func(interface, filename, line, parameter, value))
                                        goto error;
                        {
                                if (ifparam->func(interface, filename, line, parameter, value))
                                        goto error;
-                               continue;
+                               break;
                        }
                        ifparam++;
                }
                        }
                        ifparam++;
                }
+               if (ifparam->name)
+                       continue;
 
                SPRINT(interface_error, "Error in %s (line %d): unknown parameter: '%s'.\n", filename, line, parameter);
                goto error;
 
                SPRINT(interface_error, "Error in %s (line %d): unknown parameter: '%s'.\n", filename, line, parameter);
                goto error;
@@ -963,8 +907,7 @@ void free_interfaces(struct interface *interface)
                        {
                                temp = selchannel;
                                selchannel = selchannel->next;
                        {
                                temp = selchannel;
                                selchannel = selchannel->next;
-                               memset(temp, 0, sizeof(struct select_channel));
-                               free(temp);
+                               FREE(temp, sizeof(struct select_channel));
                                memuse--;
                        }
                        selchannel = ifport->out_channel;
                                memuse--;
                        }
                        selchannel = ifport->out_channel;
@@ -972,14 +915,12 @@ void free_interfaces(struct interface *interface)
                        {
                                temp = selchannel;
                                selchannel = selchannel->next;
                        {
                                temp = selchannel;
                                selchannel = selchannel->next;
-                               memset(temp, 0, sizeof(struct select_channel));
-                               free(temp);
+                               FREE(temp, sizeof(struct select_channel));
                                memuse--;
                        }
                        temp = ifport;
                        ifport = ifport->next;
                                memuse--;
                        }
                        temp = ifport;
                        ifport = ifport->next;
-                       memset(temp, 0, sizeof(struct interface_port));
-                       free(temp);
+                       FREE(temp, sizeof(struct interface_port));
                        memuse--;
                }
                ifmsn = interface->ifmsn;
                        memuse--;
                }
                ifmsn = interface->ifmsn;
@@ -987,8 +928,7 @@ void free_interfaces(struct interface *interface)
                {
                        temp = ifmsn;
                        ifmsn = ifmsn->next;
                {
                        temp = ifmsn;
                        ifmsn = ifmsn->next;
-                       memset(temp, 0, sizeof(struct interface_msn));
-                       free(temp);
+                       FREE(temp, sizeof(struct interface_msn));
                        memuse--;
                }
                ifscreen = interface->ifscreen_in;
                        memuse--;
                }
                ifscreen = interface->ifscreen_in;
@@ -996,8 +936,7 @@ void free_interfaces(struct interface *interface)
                {
                        temp = ifscreen;
                        ifscreen = ifscreen->next;
                {
                        temp = ifscreen;
                        ifscreen = ifscreen->next;
-                       memset(temp, 0, sizeof(struct interface_screen));
-                       free(temp);
+                       FREE(temp, sizeof(struct interface_screen));
                        memuse--;
                }
                ifscreen = interface->ifscreen_out;
                        memuse--;
                }
                ifscreen = interface->ifscreen_out;
@@ -1005,8 +944,7 @@ void free_interfaces(struct interface *interface)
                {
                        temp = ifscreen;
                        ifscreen = ifscreen->next;
                {
                        temp = ifscreen;
                        ifscreen = ifscreen->next;
-                       memset(temp, 0, sizeof(struct interface_screen));
-                       free(temp);
+                       FREE(temp, sizeof(struct interface_screen));
                        memuse--;
                }
                iffilter = interface->iffilter;
                        memuse--;
                }
                iffilter = interface->iffilter;
@@ -1014,18 +952,43 @@ void free_interfaces(struct interface *interface)
                {
                        temp = iffilter;
                        iffilter = iffilter->next;
                {
                        temp = iffilter;
                        iffilter = iffilter->next;
-                       memset(temp, 0, sizeof(struct interface_filter));
-                       free(temp);
+                       FREE(temp, sizeof(struct interface_filter));
                        memuse--;
                }
                temp = interface;
                interface = interface->next;
                        memuse--;
                }
                temp = interface;
                interface = interface->next;
-               memset(temp, 0, sizeof(struct interface));
-               free(temp);
+               FREE(temp, sizeof(struct interface));
                memuse--;
        }
 }
 
                memuse--;
        }
 }
 
+/*
+ * defaults of ports if not specified by config
+ */
+static void set_defaults(struct interface_port *ifport)
+{
+       /* default channel selection list */
+       if (!ifport->out_channel)
+               default_out_channel(ifport);
+       if (!ifport->in_channel)
+               default_in_channel(ifport);
+       /* default is_tones */
+       if (ifport->interface->is_tones)
+               ifport->mISDNport->tones = (ifport->interface->is_tones==IS_YES);
+       else
+               ifport->mISDNport->tones = (ifport->mISDNport->ntmode)?1:0;
+       /* default is_earlyb */
+       if (ifport->interface->is_earlyb)
+               ifport->mISDNport->earlyb = (ifport->interface->is_earlyb==IS_YES);
+       else
+               ifport->mISDNport->earlyb = (ifport->mISDNport->ntmode)?0:1;
+       /* set locally flag */
+       if (ifport->interface->extension)
+               ifport->mISDNport->locally = 1;
+       else
+               ifport->mISDNport->locally = 0;
+}
+
 
 /*
  * all links between mISDNport and interface are made
 
 /*
  * all links between mISDNport and interface are made
@@ -1060,6 +1023,7 @@ void relink_interfaces(void)
                                {
                                        ifport->mISDNport = mISDNport;
                                        mISDNport->ifport = ifport;
                                {
                                        ifport->mISDNport = mISDNport;
                                        mISDNport->ifport = ifport;
+                                       set_defaults(ifport);
                                }
                                mISDNport = mISDNport->next;
                        }
                                }
                                mISDNport = mISDNport->next;
                        }
@@ -1092,38 +1056,37 @@ void relink_interfaces(void)
                {
                        if (!ifport->mISDNport)
                        {
                {
                        if (!ifport->mISDNport)
                        {
-                               /* open new port */
-                               mISDNport = mISDNport_open(ifport->portnum, ifport->ptp, ifport->ptmp);
-                               if (mISDNport)
-                               {
-                                       ifport->mISDNport = mISDNport;
-                                       mISDNport->ifport = ifport;
-                               }
-                       }
-                       if (ifport->mISDNport)
-                       {
-                               /* default channel selection list */
-                               if (!ifport->out_channel)
-                                       default_out_channel(ifport);
-                               if (!ifport->in_channel)
-                                       default_in_channel(ifport);
-                               /* default is_tones */
-                               if (ifport->interface->is_tones)
-                                       ifport->mISDNport->is_tones = (ifport->interface->is_tones==IS_YES);
-                               else
-                                       ifport->mISDNport->is_tones = (ifport->mISDNport->ntmode)?1:0;
-                               /* default is_earlyb */
-                               if (ifport->interface->is_earlyb)
-                                       ifport->mISDNport->is_earlyb = (ifport->interface->is_earlyb==IS_YES);
-                               else
-                                       ifport->mISDNport->is_earlyb = (ifport->mISDNport->ntmode)?0:1;
+                               load_port(ifport);
                        }
                        }
+                       ifport = ifport->next;
                }
                interface = interface->next;
        }
 
 }
 
                }
                interface = interface->next;
        }
 
 }
 
+
+/*
+ * load port
+ */
+void load_port(struct interface_port *ifport)
+{
+       struct mISDNport *mISDNport;
+
+       /* open new port */
+       mISDNport = mISDNport_open(ifport->portnum, ifport->ptp, ifport->ptmp, ifport->interface);
+       if (mISDNport)
+       {
+               /* link port */
+               ifport->mISDNport = mISDNport;
+               mISDNport->ifport = ifport;
+               set_defaults(ifport);
+       } else
+       {
+               ifport->block = 2; /* not available */
+       }
+}
+
 /*
  * give summary of interface syntax
  */
 /*
  * give summary of interface syntax
  */
@@ -1151,4 +1114,3 @@ void doc_interface(void)
        }
 }
 
        }
 }
 
-