From: Christian Spielberger <christian.spielberger(a)gmail.com>
---
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