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. [RFC PATCH 03/27] acd: Add random_delay_ms() function from
      gdhcp (Peter Meerwald-Stadler)
   2. [RFC PATCH 06/27] shared/arp: Add function
      get_interface_mac_address() (Peter Meerwald-Stadler)
   3. [RFC PATCH 04/27] dhcp: Use shared get_random() function
      (Peter Meerwald-Stadler)
   4. [RFC PATCH 08/27] inet: Add function connman_inet_is_ifup()
      (Peter Meerwald-Stadler)
   5. [RFC PATCH 05/27] shared: Add low-level ARP functions
      (Peter Meerwald-Stadler)


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

Message: 1
Date: Wed, 21 Mar 2018 14:42:13 +0100
From: Peter Meerwald-Stadler <[email protected]>
To: [email protected]
Cc: [email protected],     [email protected]
Subject: [RFC PATCH 03/27] acd: Add random_delay_ms() function from
        gdhcp
Message-ID: <[email protected]>

From: Christian Spielberger <[email protected]>

---
 Makefile.am         |  8 +++---
 gdhcp/client.c      |  7 ++---
 gdhcp/ipv4ll.c      | 13 ++-------
 gdhcp/ipv4ll.h      |  1 -
 src/shared/random.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/random.h | 30 +++++++++++++++++++++
 6 files changed, 119 insertions(+), 18 deletions(-)
 create mode 100644 src/shared/random.c
 create mode 100644 src/shared/random.h

diff --git a/Makefile.am b/Makefile.am
index 97f1c87d..21ef474a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -48,7 +48,8 @@ backtrace_sources = src/backtrace.c
 endif
 
 shared_sources = src/shared/util.h src/shared/util.c \
-               src/shared/netlink.h src/shared/netlink.c
+               src/shared/netlink.h src/shared/netlink.c \
+               src/shared/random.h src/shared/random.c
 
 if DATAFILES
 
@@ -350,10 +351,11 @@ tools_wpad_test_LDADD = @GLIB_LIBS@ -lresolv
 
 tools_stats_tool_LDADD = @GLIB_LIBS@
 
-tools_dhcp_test_SOURCES = $(gdhcp_sources) tools/dhcp-test.c
+tools_dhcp_test_SOURCES = $(gdhcp_sources) $(shared_sources) tools/dhcp-test.c
 tools_dhcp_test_LDADD = @GLIB_LIBS@
 
-tools_dhcp_server_test_SOURCES = $(gdhcp_sources) tools/dhcp-server-test.c
+tools_dhcp_server_test_SOURCES = $(gdhcp_sources) $(shared_sources) \
+               tools/dhcp-server-test.c
 tools_dhcp_server_test_LDADD = @GLIB_LIBS@
 
 tools_dbus_test_SOURCES = tools/dbus-test.c
diff --git a/gdhcp/client.c b/gdhcp/client.c
index 67357782..495e9721 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -43,6 +43,7 @@
 
 #include <glib.h>
 
+#include "shared/random.h"
 #include "gdhcp.h"
 #include "common.h"
 #include "ipv4ll.h"
@@ -556,7 +557,7 @@ static gboolean send_probe_packet(gpointer dhcp_data)
 
        if (dhcp_client->retry_times < PROBE_NUM) {
                /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
-               timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
+               timeout = random_delay_ms(PROBE_MAX-PROBE_MIN);
                timeout += PROBE_MIN*1000;
        } else
                timeout = (ANNOUNCE_WAIT * 1000);
@@ -1376,7 +1377,7 @@ static void ipv4ll_start(GDHCPClient *dhcp_client)
        dhcp_client->requested_ip = ipv4ll_random_ip();
 
        /*first wait a random delay to avoid storm of arp request on boot*/
-       timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
+       timeout = random_delay_ms(PROBE_WAIT);
 
        dhcp_client->retry_times++;
        dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
@@ -1466,7 +1467,7 @@ static int ipv4ll_recv_arp_packet(GDHCPClient 
*dhcp_client)
                dhcp_client->retry_times++;
                dhcp_client->timeout =
                        g_timeout_add_full(G_PRIORITY_HIGH,
-                                       ipv4ll_random_delay_ms(PROBE_WAIT),
+                                       random_delay_ms(PROBE_WAIT),
                                        send_probe_packet,
                                        dhcp_client,
                                        NULL);
diff --git a/gdhcp/ipv4ll.c b/gdhcp/ipv4ll.c
index d9001987..4433337c 100644
--- a/gdhcp/ipv4ll.c
+++ b/gdhcp/ipv4ll.c
@@ -33,6 +33,8 @@
 #include <arpa/inet.h>
 
 #include <glib.h>
+
+#include "shared/random.h"
 #include "ipv4ll.h"
 #include "common.h"
 
@@ -52,17 +54,6 @@ uint32_t ipv4ll_random_ip(void)
        return ((LINKLOCAL_ADDR + 0x0100) + tmp);
 }
 
-/**
- * Return a random delay in range of zero to secs*1000
- */
-guint ipv4ll_random_delay_ms(guint secs)
-{
-       uint64_t rand;
-
-       dhcp_get_random(&rand);
-       return rand % (secs * 1000);
-}
-
 int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
                    uint32_t target_ip, int ifindex)
 {
diff --git a/gdhcp/ipv4ll.h b/gdhcp/ipv4ll.h
index bee8138a..bf8c3636 100644
--- a/gdhcp/ipv4ll.h
+++ b/gdhcp/ipv4ll.h
@@ -44,7 +44,6 @@ extern "C" {
 #define DEFEND_INTERVAL            10
 
 uint32_t ipv4ll_random_ip(void);
-guint ipv4ll_random_delay_ms(guint secs);
 int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
                    uint32_t target_ip, int ifindex);
 int ipv4ll_arp_socket(int ifindex);
diff --git a/src/shared/random.c b/src/shared/random.c
new file mode 100644
index 00000000..85248c6c
--- /dev/null
+++ b/src/shared/random.c
@@ -0,0 +1,78 @@
+/*
+ *
+ *  Random number generation
+ *
+ *  based on IPv4 Local Link library with GLib integration,
+ *         Copyright (C) 2009-2010  Aldebaran Robotics. All rights reserved.
+ *
+ *  Copyright (C) 2018  Commend International. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "shared/random.h"
+
+#define URANDOM "/dev/urandom"
+
+static int random_fd = -1;
+
+int get_random(uint64_t *val)
+{
+       int r;
+
+       if (random_fd < 0) {
+               random_fd = open(URANDOM, O_RDONLY);
+               if (random_fd < 0) {
+                       r = -errno;
+                       *val = random();
+
+                       return r;
+               }
+       }
+
+       if (read(random_fd, val, sizeof(uint64_t)) < 0) {
+               r = -errno;
+               *val = random();
+
+               return r;
+       }
+
+       return 0;
+}
+
+void cleanup_random(void)
+{
+       if (random_fd < 0)
+               return;
+
+       close(random_fd);
+       random_fd = -1;
+}
+
+/**
+ * Return a random delay in range of zero to secs*1000
+ */
+unsigned int random_delay_ms(unsigned int secs)
+{
+       uint64_t rand;
+
+       get_random(&rand);
+       return rand % (secs * 1000);
+}
+
diff --git a/src/shared/random.h b/src/shared/random.h
new file mode 100644
index 00000000..10614b14
--- /dev/null
+++ b/src/shared/random.h
@@ -0,0 +1,30 @@
+/*
+ *
+ *  Random number generation library
+ *
+ *  based on IPv4 Local Link library with GLib integration,
+ *         Copyright (C) 2009-2010  Aldebaran Robotics. All rights reserved.
+ *
+ *  Copyright (C) 2018  Commend International. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ */
+
+#ifndef SHARED_RANDOM_H
+#define SHARED_RANDOM_H
+
+#include <stdint.h>
+
+int get_random(uint64_t *val);
+void cleanup_random(void);
+unsigned int random_delay_ms(unsigned int secs);
+
+#endif
-- 
2.16.2



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

Message: 2
Date: Wed, 21 Mar 2018 14:42:16 +0100
From: Peter Meerwald-Stadler <[email protected]>
To: [email protected]
Cc: [email protected],     [email protected]
Subject: [RFC PATCH 06/27] shared/arp: Add function
        get_interface_mac_address()
Message-ID: <[email protected]>

From: Christian Spielberger <[email protected]>

---
 gdhcp/client.c   | 32 --------------------------------
 src/shared/arp.c | 38 ++++++++++++++++++++++++++++++++++++++
 src/shared/arp.h |  2 ++
 3 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/gdhcp/client.c b/gdhcp/client.c
index a52e8197..05c322cc 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -607,38 +607,6 @@ static gboolean send_announce_packet(gpointer dhcp_data)
        return TRUE;
 }
 
-static void get_interface_mac_address(int index, uint8_t *mac_address)
-{
-       struct ifreq ifr;
-       int sk, err;
-
-       sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
-       if (sk < 0) {
-               perror("Open socket error");
-               return;
-       }
-
-       memset(&ifr, 0, sizeof(ifr));
-       ifr.ifr_ifindex = index;
-
-       err = ioctl(sk, SIOCGIFNAME, &ifr);
-       if (err < 0) {
-               perror("Get interface name error");
-               goto done;
-       }
-
-       err = ioctl(sk, SIOCGIFHWADDR, &ifr);
-       if (err < 0) {
-               perror("Get mac address error");
-               goto done;
-       }
-
-       memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
-
-done:
-       close(sk);
-}
-
 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
 {
        if (!dhcp_client)
diff --git a/src/shared/arp.c b/src/shared/arp.c
index d436eaa9..985dd20d 100644
--- a/src/shared/arp.c
+++ b/src/shared/arp.c
@@ -19,17 +19,22 @@
  */
 #include <string.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <errno.h>
 #include <unistd.h>
 
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <sys/types.h>
+#include <sys/ioctl.h>
+
 #include <netpacket/packet.h>
 #include <net/ethernet.h>
 #include <netinet/if_ether.h>
 #include <net/if_arp.h>
 
+#include <linux/if.h>
+
 #include <arpa/inet.h>
 
 #include "shared/arp.h"
@@ -106,3 +111,36 @@ int arp_socket(int ifindex)
 
        return fd;
 }
+
+void get_interface_mac_address(int index, uint8_t *mac_address)
+{
+       struct ifreq ifr;
+       int sk, err;
+
+       sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (sk < 0) {
+               perror("Open socket error");
+               return;
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       ifr.ifr_ifindex = index;
+
+       err = ioctl(sk, SIOCGIFNAME, &ifr);
+       if (err < 0) {
+               perror("Get interface name error");
+               goto done;
+       }
+
+       err = ioctl(sk, SIOCGIFHWADDR, &ifr);
+       if (err < 0) {
+               perror("Get MAC address error");
+               goto done;
+       }
+
+       memcpy(mac_address, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+
+done:
+       close(sk);
+}
+
diff --git a/src/shared/arp.h b/src/shared/arp.h
index 8917f3fd..6df9ba0a 100644
--- a/src/shared/arp.h
+++ b/src/shared/arp.h
@@ -38,4 +38,6 @@ int send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
                    uint32_t target_ip, int ifindex);
 int arp_socket(int ifindex);
 
+void get_interface_mac_address(int index, uint8_t *mac_address);
+
 #endif
-- 
2.16.2



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

Message: 3
Date: Wed, 21 Mar 2018 14:42:14 +0100
From: Peter Meerwald-Stadler <[email protected]>
To: [email protected]
Cc: [email protected],     [email protected]
Subject: [RFC PATCH 04/27] dhcp: Use shared get_random() function
Message-ID: <[email protected]>

From: Christian Spielberger <[email protected]>

---
 gdhcp/client.c |  8 ++++----
 gdhcp/common.c | 39 ++-------------------------------------
 gdhcp/gdhcp.h  |  3 ---
 gdhcp/ipv4ll.c |  2 +-
 gdhcp/ipv4ll.h |  2 +-
 src/dhcp.c     |  2 --
 src/main.c     |  3 +++
 7 files changed, 11 insertions(+), 48 deletions(-)

diff --git a/gdhcp/client.c b/gdhcp/client.c
index 495e9721..1c988c0c 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -520,7 +520,7 @@ static int send_release(GDHCPClient *dhcp_client,
        debug(dhcp_client, "sending DHCP release request");
 
        init_packet(dhcp_client, &packet, DHCPRELEASE);
-       dhcp_get_random(&rand);
+       get_random(&rand);
        packet.xid = rand;
        packet.ciaddr = htonl(ciaddr);
 
@@ -1688,7 +1688,7 @@ static gboolean continue_rebound(gpointer user_data)
        /*recalculate remaining rebind time*/
        dhcp_client->T2 >>= 1;
        if (dhcp_client->T2 > 60) {
-               dhcp_get_random(&rand);
+               get_random(&rand);
                dhcp_client->t2_timeout =
                        g_timeout_add_full(G_PRIORITY_HIGH,
                                        dhcp_client->T2 * 1000 + (rand % 2000) 
- 1000,
@@ -1736,7 +1736,7 @@ static gboolean continue_renew (gpointer user_data)
        dhcp_client->T1 >>= 1;
 
        if (dhcp_client->T1 > 60) {
-               dhcp_get_random(&rand);
+               get_random(&rand);
                dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
                                dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
                                continue_renew,
@@ -2830,7 +2830,7 @@ int g_dhcp_client_start(GDHCPClient *dhcp_client, const 
char *last_address)
                if (re != 0)
                        return re;
 
-               dhcp_get_random(&rand);
+               get_random(&rand);
                dhcp_client->xid = rand;
                dhcp_client->start = time(NULL);
        }
diff --git a/gdhcp/common.c b/gdhcp/common.c
index 6f816718..40445904 100644
--- a/gdhcp/common.c
+++ b/gdhcp/common.c
@@ -37,6 +37,7 @@
 #include <arpa/inet.h>
 #include <fcntl.h>
 
+#include "shared/random.h"
 #include "gdhcp.h"
 #include "common.h"
 
@@ -60,42 +61,6 @@ static const DHCPOption client_options[] = {
        { OPTION_UNKNOWN,               0x00 },
 };
 
-#define URANDOM "/dev/urandom"
-static int random_fd = -1;
-
-int dhcp_get_random(uint64_t *val)
-{
-       int r;
-
-       if (random_fd < 0) {
-               random_fd = open(URANDOM, O_RDONLY);
-               if (random_fd < 0) {
-                       r = -errno;
-                       *val = random();
-
-                       return r;
-               }
-       }
-
-       if (read(random_fd, val, sizeof(uint64_t)) < 0) {
-               r = -errno;
-               *val = random();
-
-               return r;
-       }
-
-       return 0;
-}
-
-void dhcp_cleanup_random(void)
-{
-       if (random_fd < 0)
-               return;
-
-       close(random_fd);
-       random_fd = -1;
-}
-
 GDHCPOptionType dhcp_get_code_type(uint8_t code)
 {
        int i;
@@ -400,7 +365,7 @@ void dhcpv6_init_header(struct dhcpv6_packet *packet, 
uint8_t type)
 
        packet->message = type;
 
-       dhcp_get_random(&rand);
+       get_random(&rand);
        id = rand;
 
        packet->transaction_id[0] = (id >> 16) & 0xff;
diff --git a/gdhcp/gdhcp.h b/gdhcp/gdhcp.h
index eaf6a748..1285431e 100644
--- a/gdhcp/gdhcp.h
+++ b/gdhcp/gdhcp.h
@@ -232,9 +232,6 @@ void g_dhcp_server_set_save_lease(GDHCPServer *dhcp_server,
 void g_dhcp_server_set_lease_added_cb(GDHCPServer *dhcp_server,
                                                        GDHCPLeaseAddedCb cb);
 
-int dhcp_get_random(uint64_t *val);
-void dhcp_cleanup_random(void);
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/gdhcp/ipv4ll.c b/gdhcp/ipv4ll.c
index 4433337c..c78a7011 100644
--- a/gdhcp/ipv4ll.c
+++ b/gdhcp/ipv4ll.c
@@ -47,7 +47,7 @@ uint32_t ipv4ll_random_ip(void)
        uint64_t rand;
 
        do {
-               dhcp_get_random(&rand);
+               get_random(&rand);
                tmp = rand;
                tmp = tmp & IN_CLASSB_HOST;
        } while (tmp > (IN_CLASSB_HOST - 0x0200));
diff --git a/gdhcp/ipv4ll.h b/gdhcp/ipv4ll.h
index bf8c3636..3f5edfa8 100644
--- a/gdhcp/ipv4ll.h
+++ b/gdhcp/ipv4ll.h
@@ -22,7 +22,7 @@
 #ifndef __G_IPV4LL_H
 #define __G_IPV4LL_H
 
-#include <glib.h>
+#include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/src/dhcp.c b/src/dhcp.c
index 1af1eb52..b57a7949 100644
--- a/src/dhcp.c
+++ b/src/dhcp.c
@@ -761,6 +761,4 @@ void __connman_dhcp_cleanup(void)
 
        g_hash_table_destroy(ipconfig_table);
        ipconfig_table = NULL;
-
-       dhcp_cleanup_random();
 }
diff --git a/src/main.c b/src/main.c
index 318bf02b..dda54bb8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -37,6 +37,7 @@
 
 #include <gdbus.h>
 
+#include "shared/random.h"
 #include "connman.h"
 
 #define DEFAULT_INPUT_REQUEST_TIMEOUT (120 * 1000)
@@ -868,5 +869,7 @@ int main(int argc, char *argv[])
        g_free(option_debug);
        g_free(option_wifi);
 
+       cleanup_random();
+
        return 0;
 }
-- 
2.16.2



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

Message: 4
Date: Wed, 21 Mar 2018 14:42:18 +0100
From: Peter Meerwald-Stadler <[email protected]>
To: [email protected]
Cc: [email protected],     [email protected]
Subject: [RFC PATCH 08/27] inet: Add function connman_inet_is_ifup()
Message-ID: <[email protected]>

From: Christian Spielberger <[email protected]>

---
 include/inet.h |  1 +
 src/inet.c     | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/include/inet.h b/include/inet.h
index 6482934a..9c1918f3 100644
--- a/include/inet.h
+++ b/include/inet.h
@@ -38,6 +38,7 @@ char *connman_inet_ifname(int index);
 
 int connman_inet_ifup(int index);
 int connman_inet_ifdown(int index);
+bool connman_inet_is_ifup(int index);
 
 int connman_inet_set_address(int index, struct connman_ipaddress *ipaddress);
 int connman_inet_clear_address(int index, struct connman_ipaddress *ipaddress);
diff --git a/src/inet.c b/src/inet.c
index dcd1ab24..0ef979b3 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -330,6 +330,40 @@ done:
        return err;
 }
 
+bool connman_inet_is_ifup(int index)
+{
+       int sk;
+       struct ifreq ifr;
+       bool ret = false;
+
+       sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (sk < 0) {
+               connman_warn("Failed to open socket.");
+               return false;
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       ifr.ifr_ifindex = index;
+
+       if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
+               connman_warn("Failed to get interface name for interface %d", 
index);
+               goto done;
+       }
+
+       if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
+               connman_warn("Failed to get interface flags index %d", index);
+               goto done;
+       }
+
+       if (ifr.ifr_flags & IFF_UP)
+               ret = true;
+
+done:
+       close(sk);
+
+       return ret;
+}
+
 struct in6_ifreq {
        struct in6_addr ifr6_addr;
        __u32 ifr6_prefixlen;
-- 
2.16.2



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

Message: 5
Date: Wed, 21 Mar 2018 14:42:15 +0100
From: Peter Meerwald-Stadler <[email protected]>
To: [email protected]
Cc: [email protected],     [email protected]
Subject: [RFC PATCH 05/27] shared: Add low-level ARP functions
Message-ID: <[email protected]>

From: Christian Spielberger <[email protected]>

---
 Makefile.am      |   3 +-
 gdhcp/client.c   |   7 ++--
 gdhcp/ipv4ll.c   |  73 +------------------------------------
 gdhcp/ipv4ll.h   |  15 --------
 src/shared/arp.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/arp.h |  41 +++++++++++++++++++++
 6 files changed, 156 insertions(+), 91 deletions(-)
 create mode 100644 src/shared/arp.c
 create mode 100644 src/shared/arp.h

diff --git a/Makefile.am b/Makefile.am
index 21ef474a..23c5171c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -49,7 +49,8 @@ endif
 
 shared_sources = src/shared/util.h src/shared/util.c \
                src/shared/netlink.h src/shared/netlink.c \
-               src/shared/random.h src/shared/random.c
+               src/shared/random.h src/shared/random.c \
+               src/shared/arp.h src/shared/arp.c
 
 if DATAFILES
 
diff --git a/gdhcp/client.c b/gdhcp/client.c
index 1c988c0c..a52e8197 100644
--- a/gdhcp/client.c
+++ b/gdhcp/client.c
@@ -44,6 +44,7 @@
 #include <glib.h>
 
 #include "shared/random.h"
+#include "shared/arp.h"
 #include "gdhcp.h"
 #include "common.h"
 #include "ipv4ll.h"
@@ -552,7 +553,7 @@ static gboolean send_probe_packet(gpointer dhcp_data)
                dhcp_client->state = IPV4LL_PROBE;
                switch_listening_mode(dhcp_client, L_ARP);
        }
-       ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
+       send_arp_packet(dhcp_client->mac_address, 0,
                        dhcp_client->requested_ip, dhcp_client->ifindex);
 
        if (dhcp_client->retry_times < PROBE_NUM) {
@@ -581,7 +582,7 @@ static gboolean send_announce_packet(gpointer dhcp_data)
 
        debug(dhcp_client, "sending IPV4LL announce request");
 
-       ipv4ll_send_arp_packet(dhcp_client->mac_address,
+       send_arp_packet(dhcp_client->mac_address,
                                dhcp_client->requested_ip,
                                dhcp_client->requested_ip,
                                dhcp_client->ifindex);
@@ -1568,7 +1569,7 @@ static int switch_listening_mode(GDHCPClient *dhcp_client,
                                                        dhcp_client->interface,
                                                        AF_INET);
        } else if (listen_mode == L_ARP)
-               listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
+               listener_sockfd = arp_socket(dhcp_client->ifindex);
        else
                return -EIO;
 
diff --git a/gdhcp/ipv4ll.c b/gdhcp/ipv4ll.c
index c78a7011..eebf8938 100644
--- a/gdhcp/ipv4ll.c
+++ b/gdhcp/ipv4ll.c
@@ -35,6 +35,7 @@
 #include <glib.h>
 
 #include "shared/random.h"
+#include "shared/arp.h"
 #include "ipv4ll.h"
 #include "common.h"
 
@@ -54,75 +55,3 @@ uint32_t ipv4ll_random_ip(void)
        return ((LINKLOCAL_ADDR + 0x0100) + tmp);
 }
 
-int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
-                   uint32_t target_ip, int ifindex)
-{
-       struct sockaddr_ll dest;
-       struct ether_arp p;
-       uint32_t ip_source;
-       uint32_t ip_target;
-       int fd, n;
-
-       fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
-       if (fd < 0)
-               return -errno;
-
-       memset(&dest, 0, sizeof(dest));
-       memset(&p, 0, sizeof(p));
-
-       dest.sll_family = AF_PACKET;
-       dest.sll_protocol = htons(ETH_P_ARP);
-       dest.sll_ifindex = ifindex;
-       dest.sll_halen = ETH_ALEN;
-       memset(dest.sll_addr, 0xFF, ETH_ALEN);
-       if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
-               int err = errno;
-               close(fd);
-               return -err;
-       }
-
-       ip_source = htonl(source_ip);
-       ip_target = htonl(target_ip);
-       p.arp_hrd = htons(ARPHRD_ETHER);
-       p.arp_pro = htons(ETHERTYPE_IP);
-       p.arp_hln = ETH_ALEN;
-       p.arp_pln = 4;
-       p.arp_op = htons(ARPOP_REQUEST);
-
-       memcpy(&p.arp_sha, source_eth, ETH_ALEN);
-       memcpy(&p.arp_spa, &ip_source, sizeof(p.arp_spa));
-       memcpy(&p.arp_tpa, &ip_target, sizeof(p.arp_tpa));
-
-       n = sendto(fd, &p, sizeof(p), 0,
-              (struct sockaddr*) &dest, sizeof(dest));
-       if (n < 0)
-               n = -errno;
-
-       close(fd);
-
-       return n;
-}
-
-int ipv4ll_arp_socket(int ifindex)
-{
-       int fd;
-       struct sockaddr_ll sock;
-
-       fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
-       if (fd < 0)
-               return fd;
-
-       memset(&sock, 0, sizeof(sock));
-
-       sock.sll_family = AF_PACKET;
-       sock.sll_protocol = htons(ETH_P_ARP);
-       sock.sll_ifindex = ifindex;
-
-       if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
-               int err = errno;
-               close(fd);
-               return -err;
-       }
-
-       return fd;
-}
diff --git a/gdhcp/ipv4ll.h b/gdhcp/ipv4ll.h
index 3f5edfa8..315ca3db 100644
--- a/gdhcp/ipv4ll.h
+++ b/gdhcp/ipv4ll.h
@@ -31,22 +31,7 @@ extern "C" {
 /* 169.254.0.0 */
 #define LINKLOCAL_ADDR 0xa9fe0000
 
-/* See RFC 3927 */
-#define PROBE_WAIT          1
-#define PROBE_NUM           3
-#define PROBE_MIN           1
-#define PROBE_MAX           2
-#define ANNOUNCE_WAIT       2
-#define ANNOUNCE_NUM        2
-#define ANNOUNCE_INTERVAL    2
-#define MAX_CONFLICTS      10
-#define RATE_LIMIT_INTERVAL 60
-#define DEFEND_INTERVAL            10
-
 uint32_t ipv4ll_random_ip(void);
-int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
-                   uint32_t target_ip, int ifindex);
-int ipv4ll_arp_socket(int ifindex);
 
 #ifdef __cplusplus
 }
diff --git a/src/shared/arp.c b/src/shared/arp.c
new file mode 100644
index 00000000..d436eaa9
--- /dev/null
+++ b/src/shared/arp.c
@@ -0,0 +1,108 @@
+/*
+ *
+ *  ARP library
+ *
+ *  based on IPv4 Local Link library with GLib integration,
+ *         Copyright (C) 2009-2010  Aldebaran Robotics. All rights reserved.
+ *
+ *  Copyright (C) 2018  Commend International. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ */
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netpacket/packet.h>
+#include <net/ethernet.h>
+#include <netinet/if_ether.h>
+#include <net/if_arp.h>
+
+#include <arpa/inet.h>
+
+#include "shared/arp.h"
+
+int send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
+                   uint32_t target_ip, int ifindex)
+{
+       struct sockaddr_ll dest;
+       struct ether_arp p;
+       uint32_t ip_source;
+       uint32_t ip_target;
+       int fd, n;
+
+       fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (fd < 0)
+               return -errno;
+
+       memset(&dest, 0, sizeof(dest));
+       memset(&p, 0, sizeof(p));
+
+       dest.sll_family = AF_PACKET;
+       dest.sll_protocol = htons(ETH_P_ARP);
+       dest.sll_ifindex = ifindex;
+       dest.sll_halen = ETH_ALEN;
+       memset(dest.sll_addr, 0xFF, ETH_ALEN);
+       if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
+               int err = errno;
+               close(fd);
+               return -err;
+       }
+
+       ip_source = htonl(source_ip);
+       ip_target = htonl(target_ip);
+       p.arp_hrd = htons(ARPHRD_ETHER);
+       p.arp_pro = htons(ETHERTYPE_IP);
+       p.arp_hln = ETH_ALEN;
+       p.arp_pln = 4;
+       p.arp_op = htons(ARPOP_REQUEST);
+
+       memcpy(&p.arp_sha, source_eth, ETH_ALEN);
+       memcpy(&p.arp_spa, &ip_source, sizeof(p.arp_spa));
+       memcpy(&p.arp_tpa, &ip_target, sizeof(p.arp_tpa));
+
+       n = sendto(fd, &p, sizeof(p), 0,
+              (struct sockaddr*) &dest, sizeof(dest));
+       if (n < 0)
+               n = -errno;
+
+       close(fd);
+
+       return n;
+}
+
+int arp_socket(int ifindex)
+{
+       int fd;
+       struct sockaddr_ll sock;
+
+       fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (fd < 0)
+               return fd;
+
+       memset(&sock, 0, sizeof(sock));
+
+       sock.sll_family = AF_PACKET;
+       sock.sll_protocol = htons(ETH_P_ARP);
+       sock.sll_ifindex = ifindex;
+
+       if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
+               int err = errno;
+               close(fd);
+               return -err;
+       }
+
+       return fd;
+}
diff --git a/src/shared/arp.h b/src/shared/arp.h
new file mode 100644
index 00000000..8917f3fd
--- /dev/null
+++ b/src/shared/arp.h
@@ -0,0 +1,41 @@
+/*
+ *  ARP library
+ *
+ *  based on IPv4 Local Link library with GLib integration,
+ *         Copyright (C) 2009-2010  Aldebaran Robotics. All rights reserved.
+ *
+ *  Copyright (C) 2018  Commend International. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ */
+
+#ifndef SHARED_ARP_H
+#define SHARED_ARP_H
+
+#include <stdint.h>
+
+/* IPv4 Link-Local (RFC 3927), IPv4 Address Conflict Detection (RFC 5227) */
+#define PROBE_WAIT          1
+#define PROBE_NUM           3
+#define PROBE_MIN           1
+#define PROBE_MAX           2
+#define ANNOUNCE_WAIT       2
+#define ANNOUNCE_NUM        2
+#define ANNOUNCE_INTERVAL    2
+#define MAX_CONFLICTS      10
+#define RATE_LIMIT_INTERVAL 60
+#define DEFEND_INTERVAL            10
+
+int send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
+                   uint32_t target_ip, int ifindex);
+int arp_socket(int ifindex);
+
+#endif
-- 
2.16.2



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

Subject: Digest Footer

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


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

End of connman Digest, Vol 29, Issue 20
***************************************

Reply via email to