[hipl-commit] [tiny] Rev 3638: Modularization of socket handling in hipd.

  • From: Tim Just <tim.just@xxxxxxxxxxxxxx>
  • To: hipl-commit@xxxxxxxxxxxxx
  • Date: Tue, 9 Mar 2010 13:00:07 +0200

Committer: Tim Just <tim.just@xxxxxxxxxxxxxx>
Date: Tue Mar 09 11:57:49 2010 +0100
Revision: 3638
Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: tiny

Log:
  Modularization of socket handling in hipd.
  
  Added functionality to register sockets. Now modules can add own sockets,
  and their functionality will be called, if necessary.

Modified:
  M  hipd/hipd.c
  M  hipd/init.c
  M  hipd/modularization.c
  M  hipd/modularization.h
  M  modules/heartbeat/hipd/heartbeat.c

=== modified file 'hipd/hipd.c'
--- hipd/hipd.c 2010-03-08 15:47:13 +0000
+++ hipd/hipd.c 2010-03-09 10:57:49 +0000
@@ -15,6 +15,7 @@
 #endif /* HAVE_CONFIG_H */
 
 #include "hipd.h"
+#include "modularization.h"
 
 #ifdef CONFIG_HIP_PERFORMANCE
 #include "lib/performance/performance.h"
@@ -218,7 +219,7 @@
     int ch, killold = 0;
     // char buff[HIP_MAX_NETLINK_PACKET];
     fd_set read_fdset;
-    int foreground = 1, highest_descriptor = 0, err = 0, fix_alignment = 0;
+    int foreground = 1, err = 0, fix_alignment = 0;
     struct timeval timeout;
 
     /* The flushing is enabled by default. The reason for this is that
@@ -331,15 +332,6 @@
     HIP_IFEL(create_configs_and_exit, 0,
              "Configs created, exiting\n");
 
-    highest_descriptor = maxof(6,
-                               hip_nl_route.fd,
-                               hip_raw_sock_input_v6,
-                               hip_user_sock,
-                               hip_nl_ipsec.fd,
-                               hip_raw_sock_input_v4,
-                               hip_nat_sock_input_udp);
-//                               hip_icmp_sock);
-
     /* Allocate user message. */
     HIP_IFE(!(hipd_msg = hip_msg_alloc()), 1);
     HIP_IFE(!(hipd_msg_v4 = hip_msg_alloc()), 1);
@@ -352,24 +344,14 @@
                  "Starting select loop.\n");
     hipd_set_state(HIPD_STATE_EXEC);
     while (hipd_get_state() != HIPD_STATE_CLOSED) {
-        /* prepare file descriptor sets */
-        FD_ZERO(&read_fdset);
-        FD_SET(hip_nl_route.fd, &read_fdset);
-        FD_SET(hip_raw_sock_input_v6, &read_fdset);
-        FD_SET(hip_raw_sock_input_v4, &read_fdset);
-        FD_SET(hip_nat_sock_input_udp, &read_fdset);
-        FD_SET(hip_user_sock, &read_fdset);
-        FD_SET(hip_nl_ipsec.fd, &read_fdset);
-        //FD_SET(hip_icmp_sock, &read_fdset);
-        /* FD_SET(hip_firewall_sock, &read_fdset); */
+
+        hip_prepare_fd_set(&read_fdset);
+
         hip_firewall_sock = hip_user_sock;
 
         timeout.tv_sec  = HIP_SELECT_TIMEOUT;
         timeout.tv_usec = 0;
 
-        //HIP_DEBUG("select loop value hip_raw_socket_v4 = %d 
\n",hip_raw_sock_v4);
-        /* wait for socket activity */
-
 #ifdef CONFIG_HIP_FIREWALL
         if (hip_firewall_status < 0) {
             hip_msg_init(hipd_msg);
@@ -384,7 +366,7 @@
         }
 #endif
 
-        err = select((highest_descriptor + 1), &read_fdset, NULL, NULL, 
&timeout);
+        err = select((hip_get_highest_descriptor() + 1), &read_fdset, NULL, 
NULL, &timeout);
 
         if (err < 0) {
             HIP_ERROR("select() error: %s.\n", strerror(errno));

=== modified file 'hipd/init.c'
--- hipd/init.c 2010-03-08 15:47:13 +0000
+++ hipd/init.c 2010-03-09 10:57:49 +0000
@@ -357,6 +357,18 @@
 #endif /* ANDROID_CHANGES */
 #endif /* CONFIG_HIP_OPENWRT */
 
+static void hip_init_sockets(void)
+{
+    hip_register_socket(hip_nl_route.fd,        10000);
+    hip_register_socket(hip_raw_sock_input_v6,  10100);
+    hip_register_socket(hip_user_sock,          10200);
+    hip_register_socket(hip_nl_ipsec.fd,        10300);
+    hip_register_socket(hip_raw_sock_input_v4,  10400);
+    hip_register_socket(hip_nat_sock_input_udp, 10500);
+    //hip_register_socket(hip_opendht_sock_fqdn,  10600);
+    //hip_register_socket(hip_opendht_sock_hit,   10700);
+}
+
 static int hip_init_handle_functions(void)
 {
     int err = 0;
@@ -675,6 +687,8 @@
                      "Module initialization failed.\n");
     }
 
+    hip_init_sockets();
+
 out_err:
     return err;
 }

=== modified file 'hipd/modularization.c'
--- hipd/modularization.c       2010-03-08 09:44:30 +0000
+++ hipd/modularization.c       2010-03-09 10:57:49 +0000
@@ -13,15 +13,20 @@
 #include "lib/modularization/modularization.h"
 
 struct handle_function {
-    uint32_t            priority;
-    int               (*func_ptr)(const uint8_t packet_type,
-                                  const uint32_t ha_state,
-                                  struct hip_packet_context *ctx);
+    uint16_t priority;
+    int    (*func_ptr)(const uint8_t packet_type,
+                       const uint32_t ha_state,
+                       struct hip_packet_context *ctx);
 };
 
 struct maint_function {
-    uint32_t            priority;
-    int               (*func_ptr)(void);
+    uint16_t priority;
+    int    (*func_ptr)(void);
+};
+
+struct socketfd {
+    uint16_t priority;
+    int      fd;
 };
 
 /**
@@ -35,6 +40,11 @@
 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
@@ -199,7 +209,7 @@
 
     HIP_IFEL(!(new_entry = malloc(sizeof(struct maint_function))),
              -1,
-             "Error on allocating memory for a handle function entry.\n");
+             "Error on allocating memory for a maintenance function entry.\n");
 
     new_entry->priority    = priority;
     new_entry->func_ptr    = maint_function;
@@ -254,4 +264,87 @@
     }
 }
 
+/**
+ * hip_register_socket
+ *
+ */
+int hip_register_socket(int socketfd, 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;
+
+    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_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-08 09:44:30 +0000
+++ hipd/modularization.h       2010-03-09 10:57:49 +0000
@@ -34,4 +34,12 @@
 
 void hip_uninit_maint_functions(void);
 
+int hip_register_socket(int socketfd, const uint16_t priority);
+
+int hip_get_highest_descriptor(void);
+
+void hip_prepare_fd_set(fd_set *read_fdset);
+
+void hip_uninit_sockets(void);
+
 #endif /* HIP_HIPD_MODULARIZATION_H */

=== modified file 'modules/heartbeat/hipd/heartbeat.c'
--- modules/heartbeat/hipd/heartbeat.c  2010-03-08 15:47:13 +0000
+++ modules/heartbeat/hipd/heartbeat.c  2010-03-09 10:57:49 +0000
@@ -191,6 +191,7 @@
     int *icmpsockfd = &hip_icmp_sock;
 
     hip_register_maint_function(&hip_heartbeat_maintenance, 10000);
+    hip_register_socket(hip_icmp_sock, 30000);
 
     /* Make sure that hipd does not send icmpv6 immediately after base 
exchange */
     heartbeat_counter = HIP_HEARTBEAT_INTERVAL;

Other related posts:

  • » [hipl-commit] [tiny] Rev 3638: Modularization of socket handling in hipd. - Tim Just