[hipl-commit] [tiny] Rev 3659: Finished heartbeat modularization and encapsulated socket related functions.

  • From: Tim Just <tim.just@xxxxxxxxxxxxxx>
  • To: hipl-commit@xxxxxxxxxxxxx
  • Date: Thu, 11 Mar 2010 16:11:05 +0200

Committer: Tim Just <tim.just@xxxxxxxxxxxxxx>
Date: Thu Mar 11 15:08:33 2010 +0100
Revision: 3659
Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: tiny

Log:
  Finished heartbeat modularization and encapsulated socket related functions.
  
  The modularization of heartbeat related code is finished. Although the
  heartbeat functionality is still not working.
  
  The socket related functionality is now located in hipd/hip_socket.{h,c}

Modified:
  A  hipd/hip_socket.c
  A  hipd/hip_socket.h
  M  Makefile.am
  M  hipd/hipd.c
  M  hipd/init.c
  M  hipd/modularization.c
  M  hipd/modularization.h
  M  hipd/output.c
  M  hipd/output.h
  M  lib/core/message.c
  M  modules/heartbeat/hipd/heartbeat.c
  M  modules/heartbeat/hipd/heartbeat.h

=== modified file 'Makefile.am'
--- Makefile.am 2010-03-11 11:21:47 +0000
+++ Makefile.am 2010-03-11 14:08:33 +0000
@@ -98,7 +98,8 @@
                     hipd/esp_prot_light_update.c \
                     hipd/nsupdate.c \
                     hipd/hit_to_ip.c \
-                    hipd/hadb_legacy.c
+                    hipd/hadb_legacy.c \
+                    hipd/hip_socket.c
 
 if HIP_PFKEY
 hipd_hipd_SOURCES += hipd/pfkeyapi.c

=== added file 'hipd/hip_socket.c'
--- hipd/hip_socket.c   1970-01-01 00:00:00 +0000
+++ hipd/hip_socket.c   2010-03-11 14:08:33 +0000
@@ -0,0 +1,237 @@
+/** @file
+ * This file defines handling functions for network sockets for the Host
+ * Identity Protocol (HIP).
+ *
+ * Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
+ *
+ * @author  Tim Just
+ */
+
+#include <malloc.h>
+
+#include "hip_socket.h"
+#include "hipd.h"
+#include "input.h"
+#include "modularization.h"
+
+struct socketfd {
+    uint16_t priority;
+    int      fd;
+    int    (*func_ptr)(struct hip_packet_context *ctx);
+};
+
+/**
+ * List for storage of used sockets
+ */
+static hip_ll_t *hip_sockets;
+
+static int hip_handle_raw_input_v6(struct hip_packet_context *packet_ctx)
+{
+    int err = 0;
+
+    if (hip_read_control_msg_v6(hip_raw_sock_input_v6,
+                                packet_ctx,
+                                0)) {
+        HIP_ERROR("Reading network msg failed\n");
+        err = hip_receive_control_packet(packet_ctx);
+        if (err) {
+            HIP_ERROR("hip_receive_control_packet()!\n");
+        }
+    }
+
+    return err;
+}
+
+static int hip_handle_raw_input_v4(struct hip_packet_context *packet_ctx)
+{
+    int err = 0;
+
+    if (hip_read_control_msg_v4(hip_raw_sock_input_v4,
+                                packet_ctx,
+                                IPV4_HDR_SIZE)) {
+        HIP_ERROR("Reading network msg failed\n");
+    } else {
+        err = hip_receive_control_packet(packet_ctx);
+        if (err) {
+            HIP_ERROR("hip_receive_control_packet()!\n");
+        }
+    }
+
+    return err;
+}
+
+static int hip_handle_nat_input(struct hip_packet_context *packet_ctx)
+{
+    int err = 0;
+
+    HIP_DEBUG("Receiving a message on UDP from NAT " \
+              "socket (file descriptor: %d).\n",
+              hip_nat_sock_input_udp);
+
+    err = hip_read_control_msg_v4(hip_nat_sock_input_udp,
+                                  packet_ctx,
+                                  HIP_UDP_ZERO_BYTES_LEN);
+    if (err) {
+        HIP_ERROR("Reading network msg failed\n");
+    } else {
+        err = hip_receive_udp_control_packet(packet_ctx);
+    }
+
+    return err;
+}
+
+static int hip_handle_user_sock(struct hip_packet_context *packet_ctx)
+{
+    int err = 0;
+    struct sockaddr_in6 app_src;
+
+    if (hip_read_user_control_msg(hip_user_sock,
+                                  packet_ctx->input_msg,
+                                  &app_src)) {
+        HIP_ERROR("Reading user msg failed\n");
+    } else {
+        err = hip_handle_user_msg(packet_ctx->input_msg, &app_src);
+    }
+
+    return err;
+}
+
+static int hip_handle_nl_ipsec_sock(struct hip_packet_context *packet_ctx)
+{
+    HIP_DEBUG("netlink receive\n");
+    if (hip_netlink_receive(&hip_nl_ipsec)) {
+        HIP_ERROR("Netlink receiving failed\n");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int hip_handle_nl_route_sock(struct hip_packet_context *packet_ctx)
+{
+    HIP_DEBUG("netlink route receive\n");
+    if (hip_netlink_receive(&hip_nl_route)) {
+        HIP_ERROR("Netlink receiving failed\n");
+        return -1;
+    }
+
+    return 0;
+}
+
+void hip_init_sockets(void)
+{
+    hip_register_socket(hip_raw_sock_input_v6,  &hip_handle_raw_input_v6,  
10000);
+    hip_register_socket(hip_raw_sock_input_v4,  &hip_handle_raw_input_v4,  
10100);
+    hip_register_socket(hip_nat_sock_input_udp, &hip_handle_nat_input,     
10200);
+    hip_register_socket(hip_nl_ipsec.fd,        &hip_handle_nl_ipsec_sock, 
10300);
+    hip_register_socket(hip_user_sock,          &hip_handle_user_sock,     
10400);
+    hip_register_socket(hip_nl_route.fd,        &hip_handle_nl_route_sock, 
10500);
+}
+
+/**
+ * hip_register_socket
+ *
+ */
+int hip_register_socket(int socketfd,
+                        int (*func_ptr)(struct hip_packet_context *ctx),
+                        const uint16_t priority)
+{
+    int err = 0;
+    struct socketfd *new_socket = NULL;
+
+    HIP_IFEL(!(new_socket = malloc(sizeof(struct socketfd))),
+             -1,
+             "Error on allocating memory for a socket entry.\n");
+
+    new_socket->priority = priority;
+    new_socket->fd       = socketfd;
+    new_socket->func_ptr = func_ptr;
+
+    hip_sockets = lmod_register_function(hip_sockets,
+                                         new_socket,
+                                         priority);
+    if (!hip_sockets) {
+        HIP_ERROR("Error on registering a maintenance function.\n");
+        err = -1;
+    }
+
+out_err:
+    return err;
+}
+
+/**
+ * hip_get_highest_descriptor
+ *
+ */
+int hip_get_highest_descriptor(void)
+{
+    int highest_descriptor = 0;
+    hip_ll_node_t *iter    = NULL;
+
+    if (hip_sockets) {
+        while ((iter = hip_ll_iterate(hip_sockets, iter))) {
+            if (((struct socketfd*) iter->ptr)->fd >= highest_descriptor) {
+                highest_descriptor = ((struct socketfd*) iter->ptr)->fd;
+            }
+        }
+    } else {
+        HIP_DEBUG("No sockets registered.\n");
+    }
+
+    return highest_descriptor;
+}
+
+/**
+ * hip_prepare_fd_set
+ *
+ */
+void hip_prepare_fd_set(fd_set *read_fdset)
+{
+    hip_ll_node_t *iter = NULL;
+
+    FD_ZERO(read_fdset);
+
+    if (hip_sockets) {
+        while ((iter = hip_ll_iterate(hip_sockets, iter))) {
+            FD_SET(((struct socketfd*) iter->ptr)->fd, read_fdset);
+        }
+    } else {
+        HIP_DEBUG("No sockets registered.\n");
+    }
+}
+
+/**
+ * hip_run_socket_handles
+ *
+ */
+void hip_run_socket_handles(fd_set *read_fdset, struct hip_packet_context *ctx)
+{
+    hip_ll_node_t *iter = NULL;
+    int socketfd;
+
+    if (hip_sockets) {
+        while ((iter = hip_ll_iterate(hip_sockets, iter))) {
+            socketfd = ((struct socketfd*) iter->ptr)->fd;
+
+            if (FD_ISSET(socketfd, read_fdset)) {
+                ((struct socketfd*) iter->ptr)->func_ptr(ctx);
+            }
+        }
+    } else {
+        HIP_DEBUG("No sockets registered.\n");
+    }
+}
+
+/**
+ * hip_uninit_sockets
+ *
+ * Free the memory used for storage of socket fd's.
+ *
+ */
+void hip_uninit_sockets(void)
+{
+    if (hip_sockets) {
+        hip_ll_uninit(hip_sockets, free);
+        free(hip_sockets);
+    }
+}

=== added file 'hipd/hip_socket.h'
--- hipd/hip_socket.h   1970-01-01 00:00:00 +0000
+++ hipd/hip_socket.h   2010-03-11 14:08:33 +0000
@@ -0,0 +1,31 @@
+/**
+ * @file
+ * The header file for hipd/hip_socket.c
+ *
+ * Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
+ *
+ * @author Tim Just
+ *
+ */
+#ifndef HIP_HIPD_SOCKET_H
+#define HIP_HIPD_SOCKET_H
+
+#include <stdint.h>
+#include <sys/select.h>
+#include "lib/core/protodefs.h"
+
+void hip_init_sockets(void);
+
+int hip_register_socket(int socketfd,
+                        int (*func_ptr)(struct hip_packet_context *ctx),
+                        const uint16_t priority);
+
+int hip_get_highest_descriptor(void);
+
+void hip_prepare_fd_set(fd_set *read_fdset);
+
+void hip_run_socket_handles(fd_set *read_fdset, struct hip_packet_context 
*ctx);
+
+void hip_uninit_sockets(void);
+
+#endif /* HIP_HIPD_SOCKET_H */

=== modified file 'hipd/hipd.c'
--- hipd/hipd.c 2010-03-11 08:07:12 +0000
+++ hipd/hipd.c 2010-03-11 14:08:33 +0000
@@ -12,6 +12,7 @@
 
 #include "config.h"
 #include "hipd.h"
+#include "hip_socket.h"
 #include "modularization.h"
 #include "lib/core/filemanip.h"
 #include "lib/core/straddr.h"
@@ -391,87 +392,6 @@
 
         hip_run_socket_handles(&read_fdset, &packet_ctx);
 
-        if (FD_ISSET(hip_raw_sock_input_v6, &read_fdset)) {
-
-            hip_msg_init(packet_ctx.input_msg);
-
-            if (hip_read_control_msg_v6(hip_raw_sock_input_v6,
-                                        &packet_ctx,
-                                        0)) {
-                HIP_ERROR("Reading network msg failed\n");
-            } else {
-                err = hip_receive_control_packet(&packet_ctx);
-                if (err) {
-                    HIP_ERROR("hip_receive_control_packet()!\n");
-                }
-            }
-        }
-
-        if (FD_ISSET(hip_raw_sock_input_v4, &read_fdset)) {
-
-            hip_msg_init(packet_ctx.input_msg);
-
-            if (hip_read_control_msg_v4(hip_raw_sock_input_v4,
-                                        &packet_ctx,
-                                        IPV4_HDR_SIZE)) {
-                HIP_ERROR("Reading network msg failed\n");
-            } else {
-                err = hip_receive_control_packet(&packet_ctx);
-                if (err) {
-                    HIP_ERROR("hip_receive_control_packet()!\n");
-                }
-            }
-        }
-
-        if (FD_ISSET(hip_nat_sock_input_udp, &read_fdset)) {
-            HIP_DEBUG("Receiving a message on UDP from NAT " \
-                      "socket (file descriptor: %d).\n",
-                      hip_nat_sock_input_udp);
-
-            hip_msg_init(packet_ctx.input_msg);
-
-            err = hip_read_control_msg_v4(hip_nat_sock_input_udp,
-                                          &packet_ctx,
-                                          HIP_UDP_ZERO_BYTES_LEN);
-            if (err) {
-                HIP_ERROR("Reading network msg failed\n");
-            } else {
-                err =  hip_receive_udp_control_packet(&packet_ctx);
-            }
-        }
-
-        if (FD_ISSET(hip_user_sock, &read_fdset)) {
-            struct sockaddr_in6 app_src;
-
-            hip_msg_init(packet_ctx.input_msg);
-
-            if (hip_read_user_control_msg(hip_user_sock,
-                                          packet_ctx.input_msg,
-                                          &app_src)) {
-                HIP_ERROR("Reading user msg failed\n");
-            } else {
-                err = hip_handle_user_msg(packet_ctx.input_msg, &app_src);
-            }
-        }
-
-        if (FD_ISSET(hip_nl_ipsec.fd, &read_fdset)) {
-            /* Something on IF and address event netlink socket,
-             * fetch it. */
-            HIP_DEBUG("netlink receive\n");
-            if (hip_netlink_receive(&hip_nl_ipsec)) {
-                HIP_ERROR("Netlink receiving failed\n");
-            }
-        }
-
-        if (FD_ISSET(hip_nl_route.fd, &read_fdset)) {
-            /* Something on IF and address event netlink socket,
-             * fetch it. */
-            HIP_DEBUG("netlink route receive\n");
-            if (hip_netlink_receive(&hip_nl_route)) {
-                HIP_ERROR("Netlink receiving failed\n");
-            }
-        }
-
 to_maintenance:
         err = hip_periodic_maintenance();
         if (err) {

=== modified file 'hipd/init.c'
--- hipd/init.c 2010-03-11 08:07:12 +0000
+++ hipd/init.c 2010-03-11 14:08:33 +0000
@@ -14,6 +14,7 @@
 
 #include "config.h"
 #include "esp_prot_light_update.h"
+#include "hip_socket.h"
 #include "init.h"
 #include "oppdb.h"
 #include "lib/core/common_defines.h"
@@ -353,16 +354,6 @@
 #endif /* ANDROID_CHANGES */
 #endif /* CONFIG_HIP_OPENWRT */
 
-static void hip_init_sockets(void)
-{
-    hip_register_socket(hip_nl_route.fd,        NULL, 10000);
-    hip_register_socket(hip_raw_sock_input_v6,  NULL, 10100);
-    hip_register_socket(hip_user_sock,          NULL, 10200);
-    hip_register_socket(hip_nl_ipsec.fd,        NULL, 10300);
-    hip_register_socket(hip_raw_sock_input_v4,  NULL, 10400);
-    hip_register_socket(hip_nat_sock_input_udp, NULL, 10500);
-}
-
 static int hip_init_handle_functions(void)
 {
     int err = 0;

=== modified file 'hipd/modularization.c'
--- hipd/modularization.c       2010-03-11 11:21:47 +0000
+++ hipd/modularization.c       2010-03-11 14:08:33 +0000
@@ -24,13 +24,6 @@
     int    (*func_ptr)(void);
 };
 
-struct socketfd {
-    uint16_t priority;
-    int      fd;
-    int    (*func_ptr)(int socketfd,
-                       struct hip_packet_context *ctx);
-};
-
 /**
  * @todo add description
  */
@@ -42,11 +35,6 @@
 static hip_ll_t *hip_maintenance_functions;
 
 /**
- * @todo add description
- */
-static hip_ll_t *hip_sockets;
-
-/**
  * hip_register_handle_function
  *
  * Register a function for handling of the specified combination from packet
@@ -265,113 +253,3 @@
         free(hip_maintenance_functions);
     }
 }
-
-/**
- * hip_register_socket
- *
- */
-int hip_register_socket(int socketfd,
-                        int (*func_ptr)(int socketfd,
-                                        struct hip_packet_context *ctx),
-                        const uint16_t priority)
-{
-    int err = 0;
-    struct socketfd *new_socket = NULL;
-
-    HIP_IFEL(!(new_socket = malloc(sizeof(struct socketfd))),
-             -1,
-             "Error on allocating memory for a socket entry.\n");
-
-    new_socket->priority = priority;
-    new_socket->fd       = socketfd;
-    new_socket->func_ptr = func_ptr;
-
-    hip_sockets = lmod_register_function(hip_sockets,
-                                         new_socket,
-                                         priority);
-    if (!hip_sockets) {
-        HIP_ERROR("Error on registering a maintenance function.\n");
-        err = -1;
-    }
-
-out_err:
-    return err;
-}
-
-/**
- * hip_get_highest_descriptor
- *
- */
-int hip_get_highest_descriptor(void)
-{
-    int highest_descriptor = 0;
-    hip_ll_node_t *iter    = NULL;
-
-    if (hip_sockets) {
-        while ((iter = hip_ll_iterate(hip_sockets, iter))) {
-            if (((struct socketfd*) iter->ptr)->fd >= highest_descriptor) {
-                highest_descriptor = ((struct socketfd*) iter->ptr)->fd;
-            }
-        }
-    } else {
-        HIP_DEBUG("No sockets registered.\n");
-    }
-
-    return highest_descriptor;
-}
-
-/**
- * hip_prepare_fd_set
- *
- */
-void hip_prepare_fd_set(fd_set *read_fdset)
-{
-    hip_ll_node_t *iter = NULL;
-
-    FD_ZERO(read_fdset);
-
-    if (hip_sockets) {
-        while ((iter = hip_ll_iterate(hip_sockets, iter))) {
-            FD_SET(((struct socketfd*) iter->ptr)->fd, read_fdset);
-        }
-    } else {
-        HIP_DEBUG("No sockets registered.\n");
-    }
-}
-
-/**
- * hip_run_socket_handles
- *
- */
-void hip_run_socket_handles(fd_set *read_fdset, struct hip_packet_context *ctx)
-{
-    hip_ll_node_t *iter = NULL;
-    int socketfd;
-
-    if (hip_sockets) {
-        while ((iter = hip_ll_iterate(hip_sockets, iter))) {
-            socketfd = ((struct socketfd*) iter->ptr)->fd;
-
-            if (FD_ISSET(socketfd, read_fdset)) {
-//                ((struct socketfd*) iter->ptr)->func_ptr(socketfd, ctx);
-            }
-        }
-    } else {
-        HIP_DEBUG("No sockets registered.\n");
-    }
-}
-
-/**
- * hip_uninit_sockets
- *
- * Free the memory used for storage of socket fd's.
- *
- */
-void hip_uninit_sockets(void)
-{
-    if (hip_sockets) {
-        hip_ll_uninit(hip_sockets, free);
-        free(hip_sockets);
-    }
-}
-

=== modified file 'hipd/modularization.h'
--- hipd/modularization.h       2010-03-09 16:53:56 +0000
+++ hipd/modularization.h       2010-03-11 14:08:33 +0000
@@ -1,6 +1,6 @@
 /**
  * @file
- * The header file for hipd/modularization.h
+ * The header file for hipd/modularization.c
  *
  * Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
  *
@@ -34,17 +34,4 @@
 
 void hip_uninit_maint_functions(void);
 
-int hip_register_socket(int socketfd,
-                        int (*func_ptr)(int socketfd,
-                                        struct hip_packet_context *ctx),
-                        const uint16_t priority);
-
-int hip_get_highest_descriptor(void);
-
-void hip_prepare_fd_set(fd_set *read_fdset);
-
-void hip_run_socket_handles(fd_set *read_fdset, struct hip_packet_context 
*ctx);
-
-void hip_uninit_sockets(void);
-
 #endif /* HIP_HIPD_MODULARIZATION_H */

=== modified file 'hipd/output.c'
--- hipd/output.c       2010-03-10 17:48:17 +0000
+++ hipd/output.c       2010-03-11 14:08:33 +0000
@@ -1809,109 +1809,3 @@
 
     return err;
 };
-
-/**
- * This function sends ICMPv6 echo with timestamp to dsthit
- *
- * @param socket to send with
- * @param srchit HIT to send from
- * @param dsthit HIT to send to
- *
- * @return 0 on success negative on error
- */
-int hip_send_icmp(int sockfd, hip_ha_t *entry)
-{
-    int err                = 0, i = 0, identifier = 0;
-    struct icmp6_hdr *icmph = NULL;
-    struct sockaddr_in6 dst6;
-    u_char cmsgbuf[CMSG_SPACE(sizeof(struct inet6_pktinfo))];
-    u_char *icmp_pkt       = NULL;
-    struct msghdr mhdr;
-    struct iovec iov[1];
-    struct cmsghdr *chdr;
-    struct inet6_pktinfo *pkti;
-    struct timeval tval;
-
-    HIP_IFEL(!entry, 0, "No entry\n");
-
-    HIP_IFEL((entry->outbound_sa_count == 0), 0,
-             "No outbound sa, ignoring keepalive\n")
-
-    _HIP_DEBUG("Starting to send ICMPv6 heartbeat\n");
-
-    /* memset and malloc everything you need */
-    memset(&mhdr, 0, sizeof(struct msghdr));
-    memset(&tval, 0, sizeof(struct timeval));
-    memset(cmsgbuf, 0, sizeof(cmsgbuf));
-    memset(iov, 0, sizeof(struct iovec));
-    memset(&dst6, 0, sizeof(dst6));
-
-    icmp_pkt         = malloc(HIP_MAX_ICMP_PACKET);
-    HIP_IFEL((!icmp_pkt), -1, "Malloc for icmp_pkt failed\n");
-    memset(icmp_pkt, 0, sizeof(HIP_MAX_ICMP_PACKET));
-
-    chdr             = (struct cmsghdr *) (void *) cmsgbuf;
-    pkti             = (struct inet6_pktinfo *) (void *) (CMSG_DATA(chdr));
-
-    identifier       = getpid() & 0xFFFF;
-
-    /* Build ancillary data */
-    chdr->cmsg_len   = CMSG_LEN(sizeof(struct inet6_pktinfo));
-    chdr->cmsg_level = IPPROTO_IPV6;
-    chdr->cmsg_type  = IPV6_PKTINFO;
-    memcpy(&pkti->ipi6_addr, &entry->hit_our, sizeof(struct in6_addr));
-
-    /* get the destination */
-    memcpy(&dst6.sin6_addr, &entry->hit_peer, sizeof(struct in6_addr));
-    dst6.sin6_family        = AF_INET6;
-    dst6.sin6_flowinfo      = 0;
-
-    /* build icmp header */
-    icmph                   = (struct icmp6_hdr *) (void *) icmp_pkt;
-    icmph->icmp6_type       = ICMP6_ECHO_REQUEST;
-    icmph->icmp6_code       = 0;
-    entry->heartbeats_sent++;
-
-    icmph->icmp6_seq        = htons(entry->heartbeats_sent);
-    icmph->icmp6_id         = identifier;
-
-    gettimeofday(&tval, NULL);
-
-    memset(&icmp_pkt[8], 0xa5, HIP_MAX_ICMP_PACKET - 8);
-    /* put timeval into the packet */
-    memcpy(&icmp_pkt[8], &tval, sizeof(struct timeval));
-
-    /* put the icmp packet to the io vector struct for the msghdr */
-    iov[0].iov_base     = icmp_pkt;
-    iov[0].iov_len      = sizeof(struct icmp6_hdr) + sizeof(struct timeval);
-
-    /* build the msghdr for the sendmsg, put ancillary data also*/
-    mhdr.msg_name       = &dst6;
-    mhdr.msg_namelen    = sizeof(struct sockaddr_in6);
-    mhdr.msg_iov        = iov;
-    mhdr.msg_iovlen     = 1;
-    mhdr.msg_control    = &cmsgbuf;
-    mhdr.msg_controllen = sizeof(cmsgbuf);
-
-    i                   = sendmsg(sockfd, &mhdr, 0);
-    if (i <= 0) {
-        HIP_PERROR("SENDMSG ");
-        /* Set return error, even if 0 bytes sent. */
-        err = (0 > i) ? i : -1;
-    }
-
-    /* Debug information*/
-    _HIP_DEBUG_HIT("src hit", &entry->hit_our);
-    _HIP_DEBUG_HIT("dst hit", &entry->hit_peer);
-    _HIP_DEBUG("i == %d socket = %d\n", i, sockfd);
-    HIP_PERROR("SENDMSG ");
-
-    HIP_IFEL((i < 0), -1, "Failed to send ICMP into ESP tunnel\n");
-    HIP_DEBUG_HIT("Succesfully sent heartbeat to", &entry->hit_peer);
-
-out_err:
-    if (icmp_pkt) {
-        free(icmp_pkt);
-    }
-    return err;
-}

=== modified file 'hipd/output.h'
--- hipd/output.h       2010-03-11 08:07:12 +0000
+++ hipd/output.h       2010-03-11 14:08:33 +0000
@@ -73,7 +73,6 @@
 int hip_send_pkt(const struct in6_addr *local_addr, const struct in6_addr 
*peer_addr,
                  const in_port_t src_port, const in_port_t dst_port,
                  struct hip_common *msg, hip_ha_t *entry, const int 
retransmit);
-int hip_send_icmp(int sockfd, hip_ha_t *entry);
 int hip_send_udp_stun(struct in6_addr *local_addr, struct in6_addr *peer_addr,
                       in_port_t src_port, in_port_t dst_port,
                       const void *msg, int length);

=== modified file 'lib/core/message.c'
--- lib/core/message.c  2010-03-11 08:07:12 +0000
+++ lib/core/message.c  2010-03-11 14:08:33 +0000
@@ -495,6 +495,7 @@
 
     HIP_DEBUG("Receiving user message.\n");
 
+    hip_msg_init(hip_msg);
     memset(saddr, 0, sizeof(*saddr));
 
     len = sizeof(*saddr);
@@ -571,6 +572,8 @@
     int err = 0, len;
     int cmsg_level, cmsg_type;
 
+    hip_msg_init(packet_ctx->input_msg);
+
     HIP_ASSERT(packet_ctx->src_addr);
     HIP_ASSERT(packet_ctx->dst_addr);
 

=== modified file 'modules/heartbeat/hipd/heartbeat.c'
--- modules/heartbeat/hipd/heartbeat.c  2010-03-10 15:24:59 +0000
+++ modules/heartbeat/hipd/heartbeat.c  2010-03-11 14:08:33 +0000
@@ -6,6 +6,7 @@
 #include "heartbeat.h"
 #include "hipd/hadb.h"
 #include "hipd/init.h"
+#include "hipd/hip_socket.h"
 #include "hipd/modularization.h"
 
 #define HIP_HEARTBEAT_INTERVAL 20
@@ -14,6 +15,138 @@
 int heartbeat_counter = 0;
 
 /**
+ * This function sends ICMPv6 echo with timestamp to dsthit
+ *
+ * @param socket to send with
+ * @param srchit HIT to send from
+ * @param dsthit HIT to send to
+ *
+ * @return 0 on success negative on error
+ */
+static int hip_send_icmp(int sockfd, hip_ha_t *entry)
+{
+    int err                = 0, i = 0, identifier = 0;
+    struct icmp6_hdr *icmph = NULL;
+    struct sockaddr_in6 dst6;
+    u_char cmsgbuf[CMSG_SPACE(sizeof(struct inet6_pktinfo))];
+    u_char *icmp_pkt       = NULL;
+    struct msghdr mhdr;
+    struct iovec iov[1];
+    struct cmsghdr *chdr;
+    struct inet6_pktinfo *pkti;
+    struct timeval tval;
+
+    HIP_IFEL(!entry, 0, "No entry\n");
+
+    HIP_IFEL((entry->outbound_sa_count == 0), 0,
+             "No outbound sa, ignoring keepalive\n")
+
+    _HIP_DEBUG("Starting to send ICMPv6 heartbeat\n");
+
+    /* memset and malloc everything you need */
+    memset(&mhdr, 0, sizeof(struct msghdr));
+    memset(&tval, 0, sizeof(struct timeval));
+    memset(cmsgbuf, 0, sizeof(cmsgbuf));
+    memset(iov, 0, sizeof(struct iovec));
+    memset(&dst6, 0, sizeof(dst6));
+
+    icmp_pkt         = malloc(HIP_MAX_ICMP_PACKET);
+    HIP_IFEL((!icmp_pkt), -1, "Malloc for icmp_pkt failed\n");
+    memset(icmp_pkt, 0, sizeof(HIP_MAX_ICMP_PACKET));
+
+    chdr             = (struct cmsghdr *) (void *) cmsgbuf;
+    pkti             = (struct inet6_pktinfo *) (void *) (CMSG_DATA(chdr));
+
+    identifier       = getpid() & 0xFFFF;
+
+    /* Build ancillary data */
+    chdr->cmsg_len   = CMSG_LEN(sizeof(struct inet6_pktinfo));
+    chdr->cmsg_level = IPPROTO_IPV6;
+    chdr->cmsg_type  = IPV6_PKTINFO;
+    memcpy(&pkti->ipi6_addr, &entry->hit_our, sizeof(struct in6_addr));
+
+    /* get the destination */
+    memcpy(&dst6.sin6_addr, &entry->hit_peer, sizeof(struct in6_addr));
+    dst6.sin6_family        = AF_INET6;
+    dst6.sin6_flowinfo      = 0;
+
+    /* build icmp header */
+    icmph                   = (struct icmp6_hdr *) (void *) icmp_pkt;
+    icmph->icmp6_type       = ICMP6_ECHO_REQUEST;
+    icmph->icmp6_code       = 0;
+    entry->heartbeats_sent++;
+
+    icmph->icmp6_seq        = htons(entry->heartbeats_sent);
+    icmph->icmp6_id         = identifier;
+
+    gettimeofday(&tval, NULL);
+
+    memset(&icmp_pkt[8], 0xa5, HIP_MAX_ICMP_PACKET - 8);
+    /* put timeval into the packet */
+    memcpy(&icmp_pkt[8], &tval, sizeof(struct timeval));
+
+    /* put the icmp packet to the io vector struct for the msghdr */
+    iov[0].iov_base     = icmp_pkt;
+    iov[0].iov_len      = sizeof(struct icmp6_hdr) + sizeof(struct timeval);
+
+    /* build the msghdr for the sendmsg, put ancillary data also*/
+    mhdr.msg_name       = &dst6;
+    mhdr.msg_namelen    = sizeof(struct sockaddr_in6);
+    mhdr.msg_iov        = iov;
+    mhdr.msg_iovlen     = 1;
+    mhdr.msg_control    = &cmsgbuf;
+    mhdr.msg_controllen = sizeof(cmsgbuf);
+
+    i                   = sendmsg(sockfd, &mhdr, 0);
+    if (i <= 0) {
+        HIP_PERROR("SENDMSG ");
+        /* Set return error, even if 0 bytes sent. */
+        err = (0 > i) ? i : -1;
+    }
+
+    /* Debug information*/
+    _HIP_DEBUG_HIT("src hit", &entry->hit_our);
+    _HIP_DEBUG_HIT("dst hit", &entry->hit_peer);
+    _HIP_DEBUG("i == %d socket = %d\n", i, sockfd);
+    _HIP_PERROR("SENDMSG ");
+
+    HIP_IFEL((i < 0), -1, "Failed to send ICMP into ESP tunnel\n");
+    HIP_DEBUG_HIT("Sent heartbeat to", &entry->hit_peer);
+
+out_err:
+    if (icmp_pkt) {
+        free(icmp_pkt);
+    }
+    return err;
+}
+
+static int hip_heartbeat_handle_icmp_sock(struct hip_packet_context *ctx)
+{
+    int err = 0;
+
+    HIP_DEBUG("\n\nhandle icmp sock\n\n");
+
+    HIP_IFEL(hip_icmp_recvmsg(hip_icmp_sock), -1,
+             "Failed to recvmsg from ICMPv6\n");
+
+out_err:
+    return err;
+}
+
+static int hip_heartbeat_maintenance(void)
+{
+    /* Check if the heartbeats should be sent */
+    if (heartbeat_counter < 1) {
+        hip_for_each_ha(hip_send_heartbeat, &hip_icmp_sock);
+        heartbeat_counter = HIP_HEARTBEAT_INTERVAL;
+    } else {
+        heartbeat_counter--;
+    }
+
+    return 0;
+}
+
+/**
  * This function goes through the HA database and sends an icmp echo to all of 
them
  *
  * @param socket to send with
@@ -168,24 +301,6 @@
     return err;
 }
 
-int hip_heartbeat_maintenance(void)
-{
-    /* Check if the heartbeats should be sent */
-    if (heartbeat_counter < 1) {
-        hip_for_each_ha(hip_send_heartbeat, &hip_icmp_sock);
-        heartbeat_counter = HIP_HEARTBEAT_INTERVAL;
-    } else {
-        heartbeat_counter--;
-    }
-
-    return 0;
-}
-
-//        if (FD_ISSET(hip_icmp_sock, &read_fdset)) {
-//            HIP_IFEL(hip_icmp_recvmsg(hip_icmp_sock), -1,
-//                     "Failed to recvmsg from ICMPv6\n");
-//        }
-
 /**
  * Initialize icmpv6 socket.
  */
@@ -200,7 +315,7 @@
              "Error on registering HEATBEAT module.\n");
 
     hip_register_maint_function(&hip_heartbeat_maintenance, 10000);
-    hip_register_socket(hip_icmp_sock, NULL, 30000);
+    hip_register_socket(hip_icmp_sock, &hip_heartbeat_handle_icmp_sock, 30000);
 
     /* Make sure that hipd does not send icmpv6 immediately after base 
exchange */
     heartbeat_counter = HIP_HEARTBEAT_INTERVAL;

=== modified file 'modules/heartbeat/hipd/heartbeat.h'
--- modules/heartbeat/hipd/heartbeat.h  2010-03-08 15:47:13 +0000
+++ modules/heartbeat/hipd/heartbeat.h  2010-03-11 14:08:33 +0000
@@ -4,7 +4,6 @@
 #include "lib/core/state.h"
 
 int hip_heartbeat_init(void);
-int hip_heartbeat_maintenance(void);
 int hip_send_heartbeat(hip_ha_t *entry, void *opaq);
 int hip_icmp_recvmsg(int sockfd);

Other related posts:

  • » [hipl-commit] [tiny] Rev 3659: Finished heartbeat modularization and encapsulated socket related functions. - Tim Just