Send connman mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.01.org/mailman/listinfo/connman
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of connman digest..."


Today's Topics:

   1. [PATCH] PrefixLength handling for IPv4 and better error
      reporting (Benjamin Cama)
   2. [PATCH 1/2] ipconfig: Handle PrefixLength attribute for IPv4
      by D-Bus (Benjamin Cama)
   3. [PATCH 2/2] Handle IPv4 configuration errors from D-Bus or
      file (Benjamin Cama)


----------------------------------------------------------------------

Message: 1
Date: Tue, 10 Sep 2019 17:52:57 +0200
From: Benjamin Cama <[email protected]>
To: [email protected]
Subject: [PATCH] PrefixLength handling for IPv4 and better error
        reporting
Message-ID: <[email protected]>

Hi,

Here are two patches I am using for some time which allow specifying a
prefixlen for IPv4 by D-Bus, which I find very handy. The second one also help
catching errors.

They are a bit old so I do not remember the details, but I hope they help.

Regards,
Benjamin



------------------------------

Message: 2
Date: Tue, 10 Sep 2019 17:52:58 +0200
From: Benjamin Cama <[email protected]>
To: [email protected]
Subject: [PATCH 1/2] ipconfig: Handle PrefixLength attribute for IPv4
        by D-Bus
Message-ID: <[email protected]>

---
 src/ipaddress.c |  6 +++++-
 src/ipconfig.c  | 18 +++++++++++++-----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/ipaddress.c b/src/ipaddress.c
index d63d95c3..ebdabbb7 100644
--- a/src/ipaddress.c
+++ b/src/ipaddress.c
@@ -149,7 +149,11 @@ int connman_ipaddress_set_ipv4(struct connman_ipaddress 
*ipaddress,
 
        ipaddress->family = AF_INET;
 
-       ipaddress->prefixlen = connman_ipaddress_calc_netmask_len(netmask);
+       /* check if netmask or prefixlen */
+       if (g_strrstr(netmask, "."))
+               ipaddress->prefixlen = 
connman_ipaddress_calc_netmask_len(netmask);
+       else
+               ipaddress->prefixlen = g_ascii_strtoull(netmask, NULL, 10);
 
        g_free(ipaddress->local);
        ipaddress->local = g_strdup(address);
diff --git a/src/ipconfig.c b/src/ipconfig.c
index 25657733..b5ad1a36 100644
--- a/src/ipconfig.c
+++ b/src/ipconfig.c
@@ -1997,7 +1997,9 @@ int __connman_ipconfig_set_config(struct connman_ipconfig 
*ipconfig,
 
                        dbus_message_iter_get_basic(&value, &prefix_length);
 
-                       if (prefix_length < 0 || prefix_length > 128)
+                       if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 && 
(prefix_length < 0 || prefix_length > 128))
+                               return -EINVAL;
+                       else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4 
&& (prefix_length < 0 || prefix_length > 32))
                                return -EINVAL;
                } else if (g_str_equal(key, "Netmask")) {
                        if (type != DBUS_TYPE_STRING)
@@ -2071,10 +2073,16 @@ int __connman_ipconfig_set_config(struct 
connman_ipconfig *ipconfig,
 
                ipconfig->method = method;
 
-               if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
-                       connman_ipaddress_set_ipv4(ipconfig->address,
-                                               address, netmask, gateway);
-               else
+               if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
+                       if (prefix_length) {
+                               char prefix_length_str[3];
+                               sprintf(prefix_length_str, "%d", prefix_length);
+                               connman_ipaddress_set_ipv4(ipconfig->address,
+                                                       address, 
prefix_length_str, gateway);
+                       } else
+                               connman_ipaddress_set_ipv4(ipconfig->address,
+                                                       address, netmask, 
gateway);
+               } else
                        return connman_ipaddress_set_ipv6(
                                        ipconfig->address, address,
                                                prefix_length, gateway);
-- 
2.11.0



------------------------------

Message: 3
Date: Tue, 10 Sep 2019 17:52:59 +0200
From: Benjamin Cama <[email protected]>
To: [email protected]
Subject: [PATCH 2/2] Handle IPv4 configuration errors from D-Bus or
        file
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

It is useful to catch ?wrong? netmask (not CIDR), which were ignored
until then.
---
 include/ipaddress.h |  2 +-
 src/config.c        |  5 +++--
 src/dhcp.c          |  5 +++--
 src/ipaddress.c     | 12 ++++++++----
 src/ipconfig.c      | 10 ++++++----
 5 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/include/ipaddress.h b/include/ipaddress.h
index 3655ca86..a4c9871b 100644
--- a/include/ipaddress.h
+++ b/include/ipaddress.h
@@ -34,7 +34,7 @@ extern "C" {
 
 struct connman_ipaddress;
 
-unsigned char connman_ipaddress_calc_netmask_len(const char *netmask);
+signed char connman_ipaddress_calc_netmask_len(const char *netmask);
 struct connman_ipaddress *connman_ipaddress_alloc(int family);
 void connman_ipaddress_free(struct connman_ipaddress *ipaddress);
 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
diff --git a/src/config.c b/src/config.c
index 62023b10..ef42d847 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1367,9 +1367,10 @@ static int try_provision_service(struct 
connman_config_service *config,
                if (!address)
                        return -ENOENT;
 
-               connman_ipaddress_set_ipv4(address, config->ipv4_address,
+               if (connman_ipaddress_set_ipv4(address, config->ipv4_address,
                                        config->ipv4_netmask,
-                                       config->ipv4_gateway);
+                                       config->ipv4_gateway))
+                       return -EINVAL;
 
                connman_network_set_ipv4_method(network,
                                                CONNMAN_IPCONFIG_METHOD_FIXED);
diff --git a/src/dhcp.c b/src/dhcp.c
index 42e9f417..09e40ce2 100644
--- a/src/dhcp.c
+++ b/src/dhcp.c
@@ -439,7 +439,8 @@ static void lease_available_cb(GDHCPClient *dhcp_client, 
gpointer user_data)
        enum connman_ipconfig_method old_method;
        char *address, *netmask = NULL, *gateway = NULL;
        const char *c_address, *c_gateway;
-       unsigned char prefixlen, c_prefixlen;
+       signed char prefixlen;
+       unsigned char c_prefixlen;
        bool ip_change = false;
 
        DBG("Lease available");
@@ -467,7 +468,7 @@ static void lease_available_cb(GDHCPClient *dhcp_client, 
gpointer user_data)
                gateway = g_strdup(option->data);
 
        prefixlen = connman_ipaddress_calc_netmask_len(netmask);
-       if (prefixlen == 255)
+       if (prefixlen == -1)
                connman_warn("netmask: %s is invalid", netmask);
 
        DBG("c_address %s", c_address);
diff --git a/src/ipaddress.c b/src/ipaddress.c
index ebdabbb7..c36570dd 100644
--- a/src/ipaddress.c
+++ b/src/ipaddress.c
@@ -33,7 +33,7 @@
 
 #include "connman.h"
 
-unsigned char connman_ipaddress_calc_netmask_len(const char *netmask)
+signed char connman_ipaddress_calc_netmask_len(const char *netmask)
 {
        unsigned char bits;
        in_addr_t mask;
@@ -150,9 +150,13 @@ int connman_ipaddress_set_ipv4(struct connman_ipaddress 
*ipaddress,
        ipaddress->family = AF_INET;
 
        /* check if netmask or prefixlen */
-       if (g_strrstr(netmask, "."))
-               ipaddress->prefixlen = 
connman_ipaddress_calc_netmask_len(netmask);
-       else
+
+       if (g_strrstr(netmask, ".")) {
+               signed char prefixlen = 
connman_ipaddress_calc_netmask_len(netmask);
+               if (prefixlen == -1)
+                       return -EINVAL;
+               ipaddress->prefixlen = prefixlen;
+       } else
                ipaddress->prefixlen = g_ascii_strtoull(netmask, NULL, 10);
 
        g_free(ipaddress->local);
diff --git a/src/ipconfig.c b/src/ipconfig.c
index b5ad1a36..8f99e111 100644
--- a/src/ipconfig.c
+++ b/src/ipconfig.c
@@ -2077,11 +2077,13 @@ int __connman_ipconfig_set_config(struct 
connman_ipconfig *ipconfig,
                        if (prefix_length) {
                                char prefix_length_str[3];
                                sprintf(prefix_length_str, "%d", prefix_length);
-                               connman_ipaddress_set_ipv4(ipconfig->address,
-                                                       address, 
prefix_length_str, gateway);
+                               if 
(connman_ipaddress_set_ipv4(ipconfig->address,
+                                                       address, 
prefix_length_str, gateway))
+                                       return -EINVAL;
                        } else
-                               connman_ipaddress_set_ipv4(ipconfig->address,
-                                                       address, netmask, 
gateway);
+                               if 
(connman_ipaddress_set_ipv4(ipconfig->address,
+                                                       address, netmask, 
gateway))
+                                       return -EINVAL;
                } else
                        return connman_ipaddress_set_ipv6(
                                        ipconfig->address, address,
-- 
2.11.0



------------------------------

Subject: Digest Footer

_______________________________________________
connman mailing list
[email protected]
https://lists.01.org/mailman/listinfo/connman


------------------------------

End of connman Digest, Vol 47, Issue 6
**************************************

Reply via email to