[hipl-commit] [tiny] Rev 3717: Split-up of hip_handle_close into multiple functions.

  • From: Tim Just <tim.just@xxxxxxxxxxxxxx>
  • To: hipl-commit@xxxxxxxxxxxxx
  • Date: Thu, 25 Mar 2010 17:46:03 +0200

Committer: Tim Just <tim.just@xxxxxxxxxxxxxx>
Date: 25/03/2010 at 17:46:03
Revision: 3717
Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: tiny

Log:
  Split-up of hip_handle_close into multiple functions.
  
  The packet handling should be more flexible. Therfore the handling of
  incomint packets is split-up into multiple functions.

Modified:
  M  hipd/close.c
  M  hipd/close.h
  M  hipd/init.c

=== modified file 'hipd/close.c'
--- hipd/close.c        2010-03-19 15:00:16 +0000
+++ hipd/close.c        2010-03-25 15:44:43 +0000
@@ -192,20 +192,25 @@
 }
 
 /**
- * process a CLOSE message
- *
- * @param close the CLOSE message process
- * @param entry the corresponding host association
- * @return zero on success or negative on error
+ * hip_close_check_packet
+ *
+ * Check whether a received control packet is valid or not.
+ *
+ * @param packet_type The packet type of the control message (RFC 5201, 5.3.)
+ * @param ha_state The host association state (RFC 5201, 4.4.1.)
+ * @param *packet_ctx Pointer to the packet context, containing all
+ *                    information for the packet handling
+ *                    (received message, source and destination address, the
+ *                    ports and the corresponding entry from the host
+ *                    association database).
+ *
+ * @return zero on success, non-negative on error.
  */
-int hip_handle_close(const uint8_t packet_type,
-                     const uint32_t ha_state,
-                     struct hip_packet_context *ctx)
+int hip_close_check_packet(const uint8_t packet_type,
+                           const uint32_t ha_state,
+                           struct hip_packet_context *ctx)
 {
-    uint16_t mask = HIP_PACKET_CTRL_ANON;
-    int err = 0, echo_len;
-    struct hip_common *close_ack = NULL;
-    struct hip_echo_request *request;
+    int err = 0;
 #ifdef CONFIG_HIP_PERFORMANCE
     HIP_DEBUG("Start PERF_HANDLE_CLOSE\n");
     hip_perf_start_benchmark( perf_set, PERF_HANDLE_CLOSE );
@@ -235,35 +240,99 @@
     HIP_IFEL(ctx->hadb_entry->verify(ctx->hadb_entry->peer_pub_key, 
ctx->input_msg), -EINVAL,
              "Verification of close signature failed.\n");
 
-    HIP_IFE(!(close_ack = hip_msg_alloc()), -ENOMEM);
+out_err:
+    if (err) {
+        ctx->error = err;
+    }
+    return err;
+}
+
+/**
+ * hip_close_create_response
+ *
+ * Create an response (CLOSE_ACK) for a received CLOSE packet.
+ *
+ * @param packet_type The packet type of the control message (RFC 5201, 5.3.)
+ * @param ha_state The host association state (RFC 5201, 4.4.1.)
+ * @param *packet_ctx Pointer to the packet context, containing all
+ *                    information for the packet handling
+ *                    (received message, source and destination address, the
+ *                    ports and the corresponding entry from the host
+ *                    association database).
+ *
+ * @return zero on success, non-negative on error.
+ */
+int hip_close_create_response(const uint8_t packet_type,
+                              const uint32_t ha_state,
+                              struct hip_packet_context *ctx)
+{
+    int err = 0, echo_len;
+    uint16_t mask = HIP_PACKET_CTRL_ANON;
+    struct hip_echo_request *request;
+
+    HIP_IFE(!(ctx->output_msg = hip_msg_alloc()), -ENOMEM);
 
     HIP_IFEL(!(request =
-                   hip_get_param(ctx->input_msg, HIP_PARAM_ECHO_REQUEST_SIGN)),
-             -1, "No echo request under signature.\n");
+                 hip_get_param(ctx->input_msg, HIP_PARAM_ECHO_REQUEST_SIGN)),
+           -1, "No echo request under signature.\n");
     echo_len = hip_get_param_contents_len(request);
 
-    hip_build_network_hdr(close_ack,
-                          HIP_CLOSE_ACK,
-                          mask,
-                          &(ctx->hadb_entry)->hit_our,
-                          &(ctx->hadb_entry)->hit_peer);
+    hip_build_network_hdr(ctx->output_msg,
+                        HIP_CLOSE_ACK,
+                        mask,
+                        &(ctx->hadb_entry)->hit_our,
+                        &(ctx->hadb_entry)->hit_peer);
 
-    HIP_IFEL(hip_build_param_echo(close_ack, request + 1,
-                                  echo_len, 1, 0), -1,
-             "Failed to build echo param.\n");
+    HIP_IFEL(hip_build_param_echo(ctx->output_msg, request + 1,
+                                echo_len, 1, 0), -1,
+           "Failed to build echo param.\n");
 
     /************* HMAC ************/
-    HIP_IFEL(hip_build_param_hmac_contents(close_ack,
-                                           &(ctx->hadb_entry)->hip_hmac_out),
-             -1, "Building of HMAC failed.\n");
+    HIP_IFEL(hip_build_param_hmac_contents(ctx->output_msg,
+                                         &(ctx->hadb_entry)->hip_hmac_out),
+           -1, "Building of HMAC failed.\n");
 
     /********** Signature **********/
-    HIP_IFEL(ctx->hadb_entry->sign(ctx->hadb_entry->our_priv_key, close_ack), 
-EINVAL,
-             "Could not create signature.\n");
-
-    HIP_IFEL(hip_send_pkt(NULL, &(ctx->hadb_entry)->peer_addr, 
hip_get_local_nat_udp_port(),
+    HIP_IFEL(ctx->hadb_entry->sign(ctx->hadb_entry->our_priv_key,
+                                 ctx->output_msg),
+           -EINVAL,
+           "Could not create signature.\n");
+
+out_err:
+    if (err) {
+        ctx->error = err;
+    }
+    return err;
+}
+
+/**
+ * hip_close_create_response
+ *
+ * Send a before generated CLOSE_ACK packet.
+ *
+ * @param packet_type The packet type of the control message (RFC 5201, 5.3.)
+ * @param ha_state The host association state (RFC 5201, 4.4.1.)
+ * @param *packet_ctx Pointer to the packet context, containing all
+ *                    information for the packet handling
+ *                    (received message, source and destination address, the
+ *                    ports and the corresponding entry from the host
+ *                    association database).
+ *
+ * @return zero on success, non-negative on error.
+ */
+int hip_close_send_response(const uint8_t packet_type,
+                            const uint32_t ha_state,
+                            struct hip_packet_context *ctx)
+{
+    int err = 0;
+
+    HIP_IFEL(hip_send_pkt(NULL,
+                          &(ctx->hadb_entry)->peer_addr,
+                          hip_get_local_nat_udp_port(),
                           ctx->hadb_entry->peer_udp_port,
-                          close_ack, ctx->hadb_entry, 0),
+                          ctx->output_msg,
+                          ctx->hadb_entry,
+                          0),
              -ECOMM, "Sending CLOSE ACK message failed.\n");
 
     ctx->hadb_entry->state = HIP_STATE_CLOSED;
@@ -290,16 +359,15 @@
              -1,
              "Deleting peer info failed.\n");
 out_err:
-
-    if (close_ack) {
-        HIP_FREE(close_ack);
+    if (ctx->output_msg) {
+        HIP_FREE(ctx->output_msg);
     }
-
 #ifdef CONFIG_HIP_PERFORMANCE
     HIP_DEBUG("Stop and write PERF_HANDLE_CLOSE\n");
     hip_perf_stop_benchmark( perf_set, PERF_HANDLE_CLOSE );
     hip_perf_write_benchmark( perf_set, PERF_HANDLE_CLOSE );
 #endif
+
     return err;
 }
 

=== modified file 'hipd/close.h'
--- hipd/close.h        2010-03-19 14:29:54 +0000
+++ hipd/close.h        2010-03-25 15:44:43 +0000
@@ -19,9 +19,15 @@
 #include "lib/tool/pk.h"
 
 int hip_send_close(struct hip_common *msg, int delete_ha_info);
-int hip_handle_close(const uint8_t packet_type,
-                     const uint32_t ha_state,
-                     struct hip_packet_context *ctx);
+int hip_close_check_packet(const uint8_t packet_type,
+                           const uint32_t ha_state,
+                           struct hip_packet_context *ctx);
+int hip_close_create_response(const uint8_t packet_type,
+                              const uint32_t ha_state,
+                              struct hip_packet_context *ctx);
+int hip_close_send_response(const uint8_t packet_type,
+                            const uint32_t ha_state,
+                            struct hip_packet_context *ctx);
 int hip_handle_close_ack(const uint8_t packet_type,
                          const uint32_t ha_state,
                          struct hip_packet_context *ctx);

=== modified file 'hipd/init.c'
--- hipd/init.c 2010-03-25 10:47:44 +0000
+++ hipd/init.c 2010-03-25 15:44:43 +0000
@@ -699,8 +699,13 @@
     hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSING,     
&hip_handle_notify, 1000);
     hip_register_handle_function(HIP_NOTIFY, HIP_STATE_CLOSED,      
&hip_handle_notify, 1000);
 
-    hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED,  
&hip_handle_close, 1000);
-    hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING,      
&hip_handle_close, 1000);
+    hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED,  
&hip_close_check_packet,   20000);
+    hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED,  
&hip_close_create_response, 30000);
+    hip_register_handle_function(HIP_CLOSE, HIP_STATE_ESTABLISHED,  
&hip_close_send_response,   40000);
+
+    hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING,  
&hip_close_check_packet,   20000);
+    hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING,  
&hip_close_create_response, 30000);
+    hip_register_handle_function(HIP_CLOSE, HIP_STATE_CLOSING,  
&hip_close_send_response,   40000);
 
     hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSING, 
&hip_handle_close_ack, 1000);
     hip_register_handle_function(HIP_CLOSE_ACK, HIP_STATE_CLOSED,  
&hip_handle_close_ack, 1000);

Other related posts:

  • » [hipl-commit] [tiny] Rev 3717: Split-up of hip_handle_close into multiple functions. - Tim Just