lib-interface: Add parsing for interface 'options'
[samba.git] / source3 / lib / interface.c
index 31066b8b5cc48fd6d44007d8d80537babcd87cec..b8450316b6730db65ddf61bd6edcf09ed8181cbf 100644 (file)
@@ -21,6 +21,7 @@
 #include "includes.h"
 #include "lib/socket/interfaces.h"
 #include "librpc/gen_ndr/ioctl.h"
+#include "lib/util/smb_strtox.h"
 
 static struct iface_struct *probed_ifaces;
 static int total_probed;
@@ -337,6 +338,7 @@ static void add_interface(const struct iface_struct *ifs)
        iface->linkspeed = ifs->linkspeed;
        iface->capability = ifs->capability;
        iface->if_index = ifs->if_index;
+       iface->options = ifs->options;
 
        DLIST_ADD(local_interfaces, iface);
 
@@ -352,8 +354,11 @@ static void add_interface(const struct iface_struct *ifs)
 }
 
 
-static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap,
-                            uint32_t *if_index)
+static void parse_extra_info(char *key,
+                            uint64_t *speed,
+                            uint32_t *cap,
+                            uint32_t *if_index,
+                            uint32_t *options)
 {
        while (key != NULL && *key != '\0') {
                char *next_key;
@@ -370,7 +375,11 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap,
                        *val++ = 0;
 
                        if (strequal_m(key, "speed")) {
-                               *speed = (uint64_t)strtoull_err(val, NULL, 0, &error);
+                               *speed = (uint64_t)smb_strtoull(val,
+                                                               NULL,
+                                                               0,
+                                                               &error,
+                                                               SMB_STR_STANDARD);
                                if (error != 0) {
                                        DBG_DEBUG("Invalid speed value (%s)\n", val);
                                }
@@ -384,10 +393,23 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap,
                                                    "'%s'\n", val);
                                }
                        } else if (strequal_m(key, "if_index")) {
-                               *if_index = (uint32_t)strtoul_err(val, NULL, 0, &error);
+                               *if_index = (uint32_t)smb_strtoul(val,
+                                                                 NULL,
+                                                                 0,
+                                                                 &error,
+                                                                 SMB_STR_STANDARD);
                                if (error != 0) {
                                        DBG_DEBUG("Invalid key value (%s)\n", val);
                                }
+                       } else if (strequal_m(key, "options")) {
+                               if (strequal_m(val, "dynamic")) {
+                                       *options |= IFACE_DYNAMIC_OPTION;
+                               } else if (strequal_m(val, "nodynamic")) {
+                                       *options &= ~IFACE_DYNAMIC_OPTION;
+                               } else {
+                                       DBG_WARNING("Options unknown: "
+                                                   "'%s'\n", val);
+                               }
                        } else {
                                DBG_DEBUG("Key unknown: '%s'\n", key);
                        }
@@ -411,16 +433,22 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap,
  Additional information for an interface can be specified with
  this extended syntax:
 
-    interface[;key1=value1[,key2=value2[...]]]
+    "interface[;key1=value1[,key2=value2[...]]]"
+
+ Note: The double quoting is important for the
+       smb.conf parser! Otherwise the ';' and ',' separates
+       two interfaces.
 
  where
  - keys known: 'speed', 'capability', 'if_index'
  - speed is in bits per second
- - capabilites known: 'RSS', 'RDMA'
+ - capabilities known: 'RSS', 'RDMA'
  - if_index should be used with care, because
    these indexes should not conicide with indexes
    the kernel sets...
 
+ Note: The specified values overwrite the autodetected values!
+
 ****************************************************************************/
 
 static void interpret_interface(char *token)
@@ -440,17 +468,8 @@ static void interpret_interface(char *token)
        bool speed_set = false;
        bool cap_set = false;
        bool if_index_set = false;
-
-       /* first check if it is an interface name */
-       for (i=0;i<total_probed;i++) {
-               if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
-                       add_interface(&probed_ifaces[i]);
-                       added = true;
-               }
-       }
-       if (added) {
-               return;
-       }
+       uint32_t options = IFACE_NONE_OPTION;
+       bool options_set = false;
 
        /*
         * extract speed / capability information if present
@@ -458,7 +477,7 @@ static void interpret_interface(char *token)
        p = strchr_m(token, ';');
        if (p != NULL) {
                *p++ = 0;
-               parse_extra_info(p, &speed, &cap, &if_index);
+               parse_extra_info(p, &speed, &cap, &if_index, &options);
                if (speed != 0) {
                        speed_set = true;
                }
@@ -468,6 +487,32 @@ static void interpret_interface(char *token)
                if (if_index != 0) {
                        if_index_set = true;
                }
+               if (options != IFACE_NONE_OPTION) {
+                       options_set = true;
+               }
+       }
+
+       /* first check if it is an interface name */
+       for (i=0;i<total_probed;i++) {
+               if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
+                       if (speed_set) {
+                               probed_ifaces[i].linkspeed = speed;
+                       }
+                       if (cap_set) {
+                               probed_ifaces[i].capability = cap;
+                       }
+                       if (if_index_set) {
+                               probed_ifaces[i].if_index = if_index;
+                       }
+                       if (options_set) {
+                                probed_ifaces[i].options = options;
+                       }
+                       add_interface(&probed_ifaces[i]);
+                       added = true;
+               }
+       }
+       if (added) {
+               return;
        }
 
        p = strchr_m(token,'/');
@@ -491,6 +536,9 @@ static void interpret_interface(char *token)
                                if (if_index_set) {
                                        probed_ifaces[i].if_index = if_index;
                                }
+                               if (options_set) {
+                                       probed_ifaces[i].options = options;
+                               }
                                add_interface(&probed_ifaces[i]);
                                return;
                        }
@@ -523,11 +571,10 @@ static void interpret_interface(char *token)
                }
        } else {
                int error = 0;
-               char *endp = NULL;
                unsigned long val;
 
-               val = strtoul_err(p, &endp, 0, &error);
-               if (error != 0 || *endp != '\0') {
+               val = smb_strtoul(p, NULL, 0, &error, SMB_STR_FULL_STR_CONV);
+               if (error != 0) {
                        DEBUG(2,("interpret_interface: "
                                "can't determine netmask value from %s\n",
                                p));
@@ -572,6 +619,9 @@ static void interpret_interface(char *token)
                                if (if_index_set) {
                                        probed_ifaces[i].if_index = if_index;
                                }
+                               if (options_set) {
+                                       probed_ifaces[i].options = options;
+                               }
                                add_interface(&probed_ifaces[i]);
                                probed_ifaces[i].netmask = saved_mask;
                                return;
@@ -595,7 +645,7 @@ static void interpret_interface(char *token)
        ifs.netmask = ss_mask;
        ifs.bcast = ss_bcast;
        if (if_index_set) {
-               probed_ifaces[i].if_index = if_index;
+               ifs.if_index = if_index;
        }
        if (speed_set) {
                ifs.linkspeed = speed;
@@ -603,6 +653,7 @@ static void interpret_interface(char *token)
                ifs.linkspeed = 1000 * 1000 * 1000;
        }
        ifs.capability = cap;
+       ifs.options = options;
        add_interface(&ifs);
 }
 
@@ -696,3 +747,20 @@ bool interfaces_changed(void)
        TALLOC_FREE(ifaces);
        return ret;
 }
+
+/****************************************************************************
+ Return True if interface exists for given interface index
+**************************************************************************/
+
+bool interface_ifindex_exists(int if_index)
+{
+       struct interface *i;
+
+       for (i = local_interfaces; i != NULL; i = i->next) {
+               if (i->if_index == if_index) {
+                       return true;
+               }
+       }
+
+       return false;
+}