Zeroconf implementation conform to rfc 3927.
Ready for a review
---
 gdhcp/gzcip.c |  588 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdhcp/gzcip.h |   78 ++++++++
 2 files changed, 666 insertions(+), 0 deletions(-)
 create mode 100644 gdhcp/gzcip.c
 create mode 100644 gdhcp/gzcip.h

diff --git a/gdhcp/gzcip.c b/gdhcp/gzcip.c
new file mode 100644
index 0000000..551298c
--- /dev/null
+++ b/gdhcp/gzcip.c
@@ -0,0 +1,588 @@
+/*
+ *
+ *  Zeroconf client library with Glib integration
+ *
+ *  Copyright (C) 2010 Aldebaran Robotics. 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <arpa/inet.h>
+
+#include <netpacket/packet.h>
+#include <net/ethernet.h>
+#include <net/if_arp.h>
+#include <netinet/if_ether.h>
+
+#include <linux/if.h>
+
+#include <sys/time.h>
+#include <glib.h>
+
+#include "gzcip.h"
+#include "common.h"
+
+/* 169.254.0.0 */
+#define LINKLOCAL_ADDR 0xa9fe0000
+
+typedef enum _listen_mode {
+       L_NONE,
+       L_ARP,
+} ListenMode;
+
+typedef enum _zcip_client_state {
+       PROBE,
+       ANNOUNCE,
+       MONITOR,
+       DEFEND,
+} ClientState;
+
+struct _GZCIPClient {
+       GZCIPType type;
+       ClientState state;
+       int ifindex;
+       uint8_t mac_address[6];
+       struct in_addr requested_ip;
+       struct in_addr null_ip;
+       uint8_t null_addr[6];
+       uint8_t broadcast_addr[6];
+       char* assigned_ip;
+       int sockfd;
+       struct sockaddr_ll saddr;
+       uint8_t retry_times;
+       guint timeout;
+       guint conflicts;
+       ListenMode listen_mode;
+       int listener_sockfd;
+       GIOChannel *listener_channel;
+       guint listener_watch;
+       GZCIPClientEventFunc lease_available_cb;
+       GZCIPClientEventFunc lease_lost_cb;
+       GZCIPClientEventFunc address_conflict_cb;
+       gpointer lease_lost_data;
+       gpointer address_conflict_data;
+       gpointer lease_available_data;
+       GZCIPDebugFunc debug_func;
+       gpointer debug_data;
+};
+
+static gboolean probe_timeout(gpointer zcip_data);
+static gboolean announce_timeout(gpointer zcip_data);
+static gboolean defend_timeout(gpointer zcip_data);
+
+static void out(const char *str, gpointer data)
+{
+       printf("%s", str);
+}
+
+static inline void debug(GZCIPClient *zcip_client, const char *format, ...)
+{
+       char str[256];
+       va_list ap;
+
+       if (zcip_client->debug_func == NULL)
+               return;
+
+       va_start(ap, format);
+
+       if (vsnprintf(str, sizeof(str), format, ap) > 0)
+               zcip_client->debug_func(str, zcip_client->debug_data);
+
+       va_end(ap);
+}
+
+
+/**
+ * Pick a random link local IP address on 169.254/16, except that
+ * the first and last 256 addresses are reserved.
+ *
+ */
+static uint32_t pick(void)
+{
+       unsigned tmp;
+       struct timeval tv;
+       do {
+               gettimeofday(&tv, NULL);
+               srand(tv.tv_usec);
+               tmp = rand();
+               tmp = tmp & IN_CLASSB_HOST;
+       } while (tmp > (IN_CLASSB_HOST - 0x0200));
+       return ((LINKLOCAL_ADDR + 0x0100) + tmp);
+
+}
+ /* static uint32_t pick(void){ */
+ /*   uint8_t tmp[4]; */
+ /*   uint32_t addr; */
+ /*   tmp[3] = 192; */
+ /*   tmp[2] = 168; */
+ /*   tmp[1] = 1; */
+ /*   tmp[0] = 1; */
+
+ /*   memcpy(&addr, tmp, sizeof(addr)); */
+ /*   return addr; */
+ /* } */
+
+/*
+ * Return a random delay in range of secs
+ */
+static guint random_delay_ms(guint secs)
+{
+       struct timeval tv;
+       guint tmp;
+
+       gettimeofday(&tv, NULL);
+       srand(tv.tv_usec);
+       tmp = rand();
+       return tmp % (secs * 1000);
+}
+
+char *g_zcip_client_get_address(GZCIPClient *zcip_client)
+{
+       return g_strdup(zcip_client->assigned_ip);
+}
+
+char *g_zcip_client_get_netmask(GZCIPClient *zcip_client)
+{
+       return g_strdup("255.255.0.0");
+}
+
+static int zcip_socket(int ifindex)
+{
+       int fd;
+       struct sockaddr_ll sock;
+       fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP));
+       if (fd < 0)
+               return fd;
+
+       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) {
+               close(fd);
+               return -errno;
+       }
+
+       return fd;
+}
+
+static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
+                               gpointer zcip_data);
+
+static int switch_listening_mode(GZCIPClient *zcip_client,
+                               ListenMode listen_mode){
+
+       GIOChannel *listener_channel;
+       int listener_sockfd;
+
+       if (zcip_client->listen_mode == listen_mode)
+               return 0;
+
+       debug(zcip_client, "switching listen from %d to %d\n",
+               zcip_client->listen_mode, listen_mode);
+       if (zcip_client->listen_mode != L_NONE) {
+               g_source_remove(zcip_client->listener_watch);
+               zcip_client->listener_channel = NULL;
+               zcip_client->listen_mode = L_NONE;
+               zcip_client->listener_sockfd = -1;
+               zcip_client->listener_watch = 0;
+       }
+       if (listen_mode == L_NONE)
+               return 0;
+
+       if (listen_mode != L_ARP)
+               return -EIO;
+
+       listener_sockfd = zcip_socket(zcip_client->ifindex);
+
+       if (listener_sockfd < 0)
+               return -EIO;
+
+       listener_channel = g_io_channel_unix_new(listener_sockfd);
+       if (listener_channel == NULL) {
+               close(listener_sockfd);
+               return -EIO;
+       }
+       zcip_client->listen_mode = listen_mode;
+       zcip_client->listener_sockfd = listener_sockfd;
+       zcip_client->listener_channel = listener_channel;
+
+       g_io_channel_set_close_on_unref(listener_channel, TRUE);
+       zcip_client->listener_watch =
+               g_io_add_watch_full(listener_channel,
+                                       G_PRIORITY_HIGH, G_IO_IN,
+                                       listener_event, zcip_client,
+                                       NULL);
+       g_io_channel_unref(zcip_client->listener_channel);
+
+       return 0;
+}
+
+static void arp(GZCIPClient *zcip_client, struct in_addr source_ip,
+               uint8_t *target_eth, struct in_addr target_ip){
+
+       struct ether_arp p;
+       uint32_t ip_source;
+       uint32_t ip_target;
+       ip_source = htonl(source_ip.s_addr);
+       ip_target = htonl(target_ip.s_addr);
+       memset(&p, 0, sizeof(p));
+       // arp request
+       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, zcip_client->mac_address, ETH_ALEN);
+       memcpy(&p.arp_spa, &ip_source, sizeof(p.arp_spa));
+       memcpy(&p.arp_tha, target_eth, ETH_ALEN);
+       memcpy(&p.arp_tpa, &ip_target, sizeof(p.arp_tpa));
+
+       sendto((zcip_client->sockfd), &p, sizeof(p), 0,
+               (struct sockaddr*) &(zcip_client->saddr),
+               sizeof(zcip_client->saddr));
+}
+
+static gboolean send_probe_packet(gpointer zcip_data)
+{
+       GZCIPClient *zcip_client;
+       guint timeout;
+
+       zcip_client = zcip_data;
+       /*if requested_ip is not valid, pick a new address*/
+       if ( zcip_client->requested_ip.s_addr ==  0)
+               {
+                       debug(zcip_client, "pick a new address \n");
+                       zcip_client->requested_ip.s_addr = pick();
+               }
+
+       debug(zcip_client, "sending probe \n");
+
+       arp(zcip_client, zcip_client->null_ip, zcip_client->null_addr,
+           zcip_client->requested_ip);
+
+       if ( zcip_client->retry_times < 2 )
+               timeout =
+                       random_delay_ms(PROBE_MAX-PROBE_MIN) + (PROBE_MIN*1000);
+       else
+               timeout = (ANNOUNCE_WAIT * 1000);
+
+       zcip_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
+                                                 timeout,
+                                                 probe_timeout,
+                                                 zcip_client,
+                                                 NULL);
+       switch_listening_mode(zcip_client, L_ARP);
+       zcip_client->retry_times++;
+       return FALSE;
+}
+
+static gboolean send_announce_packet(gpointer zcip_data)
+{
+       GZCIPClient *zcip_client;
+       zcip_client = zcip_data;
+       debug(zcip_client, "sending announce \n");
+       arp(zcip_client, zcip_client->requested_ip, zcip_client->null_addr,
+                       zcip_client->requested_ip);
+       if (zcip_client->state == DEFEND){
+               zcip_client->timeout =
+                       g_timeout_add_seconds_full(G_PRIORITY_HIGH,
+                                               DEFEND_INTERVAL,
+                                               defend_timeout,
+                                               zcip_client,
+                                               NULL);
+               return TRUE;
+       }
+       else
+               zcip_client->timeout =
+                       g_timeout_add_seconds_full(G_PRIORITY_HIGH,
+                                               ANNOUNCE_INTERVAL,
+                                               announce_timeout,
+                                               zcip_client,
+                                               NULL);
+       zcip_client->retry_times++;
+       return TRUE;
+}
+
+static int zcip_recv_arp_packet( GZCIPClient *zcip_client ){
+       int bytes;
+       struct ether_arp arp;
+       uint32_t ip_requested;
+       int source_conflict;
+       int target_conflict;
+
+       memset(&arp, 0, sizeof(arp));
+       bytes = 0;
+       bytes = read(zcip_client->listener_sockfd, &arp, sizeof(arp));
+       if (bytes < 0)
+               return -1;
+
+       if ( arp.arp_op != htons(ARPOP_REPLY) &&
+            arp.arp_op != htons(ARPOP_REQUEST)){
+               return -1;
+       }
+
+       ip_requested = ntohl(zcip_client->requested_ip.s_addr);
+
+       source_conflict = memcmp(arp.arp_spa, &ip_requested,
+                               sizeof(ip_requested));
+       if (source_conflict != 0){
+               zcip_client->conflicts++;
+       }
+       target_conflict = memcmp(arp.arp_tpa, &ip_requested,
+                               sizeof(ip_requested));
+
+       if (source_conflict !=0 && target_conflict != 0)
+               return -1;
+
+       debug(zcip_client, "conflict detected\n");
+       if (zcip_client->state == MONITOR){
+               if (source_conflict != 0)
+                       return -1;
+               zcip_client->state = DEFEND;
+               debug(zcip_client, "DEFEND mode conflicts : %d\n",
+                      zcip_client->conflicts);
+               send_announce_packet(zcip_client);
+               return 0;
+       }
+       if (zcip_client->state == DEFEND){
+               if (source_conflict != 0)
+                       return -1;
+               else if (zcip_client->lease_lost_cb != NULL)
+                       zcip_client->lease_lost_cb(zcip_client,
+                                               zcip_client->lease_lost_data);
+       }
+       g_zcip_client_stop(zcip_client);
+
+       zcip_client->conflicts++;
+       if(zcip_client->conflicts < MAX_CONFLICTS)
+               g_zcip_client_start(zcip_client);
+       else
+               g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
+                                       g_zcip_client_start,
+                                       zcip_client);
+       return 0;
+}
+
+static gboolean defend_timeout(gpointer zcip_data)
+{
+       GZCIPClient *zcip_client = zcip_data;
+
+       zcip_client->conflicts = 0;
+       debug(zcip_client, "back to MONITOR mode\n");
+       zcip_client->state = MONITOR;
+       switch_listening_mode(zcip_client, L_ARP);
+
+       return FALSE;
+}
+
+static char *get_ip(uint32_t ip)
+{
+       struct in_addr addr;
+       addr.s_addr = ip;
+       return g_strdup(inet_ntoa(addr));
+}
+
+static gboolean announce_timeout(gpointer zcip_data)
+{
+       GZCIPClient *zcip_client = zcip_data;
+       uint32_t ip;
+
+       debug(zcip_client, "request timeout (retries %d) \n",
+              zcip_client->retry_times);
+
+       if( zcip_client->retry_times != ANNOUNCE_NUM ){
+               send_announce_packet(zcip_client);
+               return FALSE;
+       }
+
+       ip = htonl(zcip_client->requested_ip.s_addr);
+       debug(zcip_client, "switching to monitor mode\n");
+       zcip_client->state = MONITOR;
+       if(zcip_client->assigned_ip == NULL)
+               zcip_client->assigned_ip = get_ip(ip);
+       if (zcip_client->lease_available_cb != NULL)
+               zcip_client->lease_available_cb(zcip_client,
+                                       zcip_client->lease_available_data);
+       zcip_client->conflicts = 0;
+
+       return FALSE;
+}
+
+static gboolean probe_timeout(gpointer zcip_data)
+{
+
+       GZCIPClient *zcip_client = zcip_data;
+       debug(zcip_client, "request timeout (retries %d) \n",
+              zcip_client->retry_times);
+
+       if( zcip_client->retry_times == PROBE_NUM ) {
+               zcip_client->state = ANNOUNCE;
+               zcip_client->retry_times = 0;
+               send_announce_packet(zcip_client);
+               return FALSE;
+       }
+       send_probe_packet(zcip_client);
+       return FALSE;
+}
+
+static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
+                              gpointer zcip_data)
+
+{
+       GZCIPClient *zcip_client = zcip_data;
+       int re;
+
+       if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+               zcip_client->listener_watch = 0;
+               return TRUE;
+       }
+
+       if (zcip_client->listen_mode == L_NONE)
+               return FALSE;
+
+       if (zcip_client->listener_sockfd < 0){
+               debug(zcip_client, "socket close \n");
+               return -EIO;
+       }
+       if (zcip_client->listen_mode == L_ARP)
+               re = zcip_recv_arp_packet(zcip_client);
+       return TRUE;
+}
+
+gboolean g_zcip_client_start(gpointer zcip_data){
+
+       GZCIPClient *zcip_client = zcip_data;
+       guint timeout;
+
+       debug(zcip_client, "starting zeroconf\n");
+       timeout = random_delay_ms(PROBE_WAIT);
+       g_timeout_add_full(G_PRIORITY_HIGH,
+                          timeout,
+                          send_probe_packet,
+                          zcip_client,
+                          NULL);
+
+       return FALSE;
+}
+void g_zcip_client_stop(GZCIPClient *zcip_client){
+
+       debug(zcip_client, "stop zeroconf\n");
+       switch_listening_mode(zcip_client, L_NONE);
+
+       if (zcip_client->timeout > 0) {
+               debug(zcip_client, "removing timeout: %u\n",
+                     zcip_client->timeout);
+               g_source_remove(zcip_client->timeout);
+       }
+       zcip_client->timeout = 0;
+
+       if (zcip_client->listener_watch > 0) {
+               g_source_remove(zcip_client->listener_watch);
+               zcip_client->listener_watch = 0;
+       }
+
+       g_free(zcip_client->assigned_ip);
+       zcip_client->assigned_ip = NULL;
+
+       zcip_client->retry_times = 0;
+       zcip_client->requested_ip.s_addr = 0;
+       zcip_client->state = PROBE;
+
+}
+void g_zcip_client_register_event(GZCIPClient *zcip_client,
+                                 GZCIPClientEvent event,
+                                 GZCIPClientEventFunc func,
+                                 gpointer data)
+{
+       switch (event) {
+
+       case G_ZCIP_CLIENT_EVENT_LEASE_AVAILABLE:
+               zcip_client->lease_available_cb = func;
+               zcip_client->lease_available_data = data;
+               return;
+       case G_ZCIP_CLIENT_EVENT_LEASE_LOST:
+               zcip_client->lease_lost_cb = func;
+               zcip_client->lease_lost_data = data;
+               return;
+       }
+}
+
+
+
+GZCIPClient *
+g_zcip_client_new(GZCIPType type, int ifindex, GZCIPClientError *error)
+{
+       GZCIPClient *zcip_client;
+       struct sockaddr_ll saddr;
+
+       if (ifindex < 0) {
+               *error = G_ZCIP_CLIENT_ERROR_INVALID_INDEX;
+               return NULL;
+       }
+
+       zcip_client = g_try_new0(GZCIPClient, 1);
+
+       if (zcip_client == NULL) {
+               *error = G_ZCIP_CLIENT_ERROR_NOMEM;
+               return NULL;
+       }
+
+       if (interface_is_up(ifindex) == FALSE) {
+               *error = G_ZCIP_CLIENT_ERROR_INTERFACE_DOWN;
+               goto error;
+       }
+
+       get_interface_mac_address(ifindex, zcip_client->mac_address);
+       zcip_client->sockfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP));
+
+       if (zcip_client->sockfd < 0){
+               *error = G_ZCIP_CLIENT_ERROR_NOMEM;
+               goto error;
+       }
+
+       saddr.sll_family = AF_PACKET;
+       saddr.sll_protocol = htons(ETH_P_ARP);
+       saddr.sll_ifindex = ifindex;
+       saddr.sll_halen = ETH_ALEN;
+       memset(saddr.sll_addr, 0xFF, ETH_ALEN);
+
+
+       if (bind(zcip_client->sockfd,(struct sockaddr*) &saddr,
+                sizeof(saddr)) < 0){
+               goto error;
+       }
+
+       zcip_client->type        = type;
+       zcip_client->ifindex     = ifindex;
+       zcip_client->retry_times = 0;
+       zcip_client->state       = PROBE;
+       zcip_client->saddr       = saddr;
+       *error                   = G_ZCIP_CLIENT_ERROR_NONE;
+       memset(&(zcip_client->null_ip), 0, sizeof(zcip_client->null_ip));
+       memset(zcip_client->null_addr, 0, ETH_ALEN);
+       memset(zcip_client->broadcast_addr, 0xff, ETH_ALEN);
+       zcip_client->debug_func = &out;
+       return zcip_client;
+ error:
+       g_free(zcip_client);
+       return NULL;
+}
diff --git a/gdhcp/gzcip.h b/gdhcp/gzcip.h
new file mode 100644
index 0000000..c265cfd
--- /dev/null
+++ b/gdhcp/gzcip.h
@@ -0,0 +1,78 @@
+/*
+ *
+ *  Zeroconf client library with Glib integration
+ *
+ *  Copyright (C) 2010 Aldebaran Robotics. 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef        GZCIP_H_
+#define        GZCIP_H_
+#include <glib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* 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
+
+struct _GZCIPClient;
+
+typedef struct _GZCIPClient GZCIPClient;
+
+typedef enum {
+       G_ZCIP_CLIENT_ERROR_NONE,
+       G_ZCIP_CLIENT_ERROR_INTERFACE_UNAVAILABLE,
+       G_ZCIP_CLIENT_ERROR_INTERFACE_DOWN,
+       G_ZCIP_CLIENT_ERROR_NOMEM,
+       G_ZCIP_CLIENT_ERROR_INVALID_INDEX,
+} GZCIPClientError;
+
+typedef enum {
+       G_ZCIP_CLIENT_EVENT_LEASE_AVAILABLE,
+       G_ZCIP_CLIENT_EVENT_LEASE_LOST,
+} GZCIPClientEvent;
+
+  typedef enum {
+       G_ZCIP_IPV4,
+} GZCIPType;
+
+typedef void (*GZCIPClientEventFunc) (GZCIPClient *client, gpointer user_data);
+typedef void (*GZCIPDebugFunc)(const char *str, gpointer user_data);
+
+GZCIPClient *g_zcip_client_new(GZCIPType type, int ifindex, GZCIPClientError 
*error);
+char *g_zcip_client_get_address(GZCIPClient *zcip_client);
+char *g_zcip_client_get_netmask(GZCIPClient *zcip_client);
+gboolean g_zcip_client_start(gpointer zcip_data);
+void g_zcip_client_stop(GZCIPClient *zcip_client);
+void g_zcip_client_register_event(GZCIPClient *zcip_client,
+                               GZCIPClientEvent event,
+                               GZCIPClientEventFunc func,
+                               gpointer data);
+#ifdef __cplusplus
+}
+#endif
+#endif     /* !GZCIP_H_ */
--
1.7.1

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to