[hipl-commit] [trunk] Rev 3585: Reformatted hipd directory.

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

Committer: Tim Just <tim.just@xxxxxxxxxxxxxx>
Date: Wed Feb 10 23:32:46 2010 +0100
Revision: 3585
Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: trunk

Log:
  Reformatted hipd directory.

Modified:
  M  hipd/accessor.c
  M  hipd/accessor.c.doxyme
  M  hipd/accessor.h
  M  hipd/accessor.h.doxyme
  M  hipd/blind.c
  M  hipd/blind.c.doxyme
  M  hipd/blind.h
  M  hipd/blind.h.doxyme
  M  hipd/bos.c
  M  hipd/bos.c.doxyme
  M  hipd/bos.h
  M  hipd/bos.h.doxyme
  M  hipd/cert.c
  M  hipd/cert.c.doxyme
  M  hipd/cert.h
  M  hipd/cert.h.doxyme
  M  hipd/close.c
  M  hipd/close.c.doxyme
  M  hipd/close.h.doxyme
  M  hipd/configfilereader.c
  M  hipd/configfilereader.c.doxyme
  M  hipd/configfilereader.h
  M  hipd/configfilereader.h.doxyme
  M  hipd/cookie.c
  M  hipd/cookie.c.doxyme
  M  hipd/cookie.h
  M  hipd/cookie.h.doxyme
  M  hipd/dh.c
  M  hipd/dh.c.doxyme
  M  hipd/dh.h
  M  hipd/dh.h.doxyme
  M  hipd/dht.c
  M  hipd/dhtqueue.c
  M  hipd/dhtqueue.h
  M  hipd/esp_prot_anchordb.c
  M  hipd/esp_prot_anchordb.h
  M  hipd/esp_prot_hipd_msg.c
  M  hipd/esp_prot_hipd_msg.h
  M  hipd/esp_prot_light_update.c
  M  hipd/esp_prot_light_update.h
  M  hipd/hadb.c
  M  hipd/hadb.c.doxyme
  M  hipd/hadb.h
  M  hipd/hadb.h.doxyme
  M  hipd/hadb_legacy.c
  M  hipd/hadb_legacy.c.doxyme
  M  hipd/hadb_legacy.h
  M  hipd/hadb_legacy.h.doxyme
  M  hipd/heartbeat.c
  M  hipd/hi3.c
  M  hipd/hi3.c.doxyme
  M  hipd/hi3.h
  M  hipd/hi3.h.doxyme
  M  hipd/hidb.c
  M  hipd/hidb.c.doxyme
  M  hipd/hidb.h
  M  hipd/hidb.h.doxyme
  M  hipd/hipd.c
  M  hipd/hipd.c.doxyme
  M  hipd/hipd.h
  M  hipd/hipd.h.doxyme
  M  hipd/hipqueue.c.doxyme
  M  hipd/hipqueue.h.doxyme
  M  hipd/hiprelay.c
  M  hipd/hiprelay.c.doxyme
  M  hipd/hiprelay.h
  M  hipd/hiprelay.h.doxyme
  M  hipd/hit_to_ip.c
  M  hipd/hit_to_ip.h
  M  hipd/icookie.c
  M  hipd/icookie.c.doxyme
  M  hipd/icookie.h.doxyme
  M  hipd/init.c
  M  hipd/init.c.doxyme
  M  hipd/init.h
  M  hipd/init.h.doxyme
  M  hipd/input.c
  M  hipd/input.c.doxyme
  M  hipd/input.h
  M  hipd/input.h.doxyme
  M  hipd/keymat.c
  M  hipd/keymat.c.doxyme
  M  hipd/keymat.h
  M  hipd/keymat.h.doxyme
  M  hipd/maintenance.c
  M  hipd/maintenance.c.doxyme
  M  hipd/maintenance.h
  M  hipd/maintenance.h.doxyme
  M  hipd/nat.c
  M  hipd/nat.c.doxyme
  M  hipd/nat.h
  M  hipd/nat.h.doxyme
  M  hipd/netdev.c
  M  hipd/netdev.c.doxyme
  M  hipd/netdev.h
  M  hipd/netdev.h.doxyme
  M  hipd/nsupdate.c
  M  hipd/nsupdate.h
  M  hipd/oppdb.c
  M  hipd/oppdb.c.doxyme
  M  hipd/oppdb.h
  M  hipd/oppdb.h.doxyme
  M  hipd/oppipdb.c
  M  hipd/oppipdb.c.doxyme
  M  hipd/oppipdb.h
  M  hipd/oppipdb.h.doxyme
  M  hipd/output.c
  M  hipd/output.c.doxyme
  M  hipd/output.h
  M  hipd/output.h.doxyme
  M  hipd/pfkeyapi.c
  M  hipd/pfkeyapi.c.doxyme
  M  hipd/pisa.c
  M  hipd/pisa.c.doxyme
  M  hipd/pisa.h.doxyme
  M  hipd/registration.c
  M  hipd/registration.c.doxyme
  M  hipd/registration.h
  M  hipd/registration.h.doxyme
  M  hipd/tcptimeout.c
  M  hipd/tcptimeout.c.doxyme
  M  hipd/tcptimeout.h
  M  hipd/tcptimeout.h.doxyme
  M  hipd/update.c
  M  hipd/update.c.doxyme
  M  hipd/update.h
  M  hipd/update.h.doxyme
  M  hipd/update_legacy.c
  M  hipd/update_legacy.c.doxyme
  M  hipd/update_legacy.h.doxyme
  M  hipd/user.c
  M  hipd/user.c.doxyme
  M  hipd/user.h
  M  hipd/user.h.doxyme
  M  hipd/user_ipsec_hipd_msg.c
  M  hipd/user_ipsec_hipd_msg.h
  M  hipd/user_ipsec_sadb_api.c
  M  hipd/user_ipsec_sadb_api.h

=== modified file 'hipd/accessor.c'
--- hipd/accessor.c     2009-12-30 23:36:06 +0000
+++ hipd/accessor.c     2010-02-10 22:32:46 +0000
@@ -1,4 +1,3 @@
-
 /*
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,7 +17,7 @@
 #include "accessor.h"
 
 
-unsigned int hipd_state = HIPD_STATE_CLOSED;
+unsigned int hipd_state         = HIPD_STATE_CLOSED;
 #ifdef CONFIG_HIP_OPPORTUNISTIC
 unsigned int opportunistic_mode = 1;
 #endif // CONFIG_HIP_OPPORTUNISTIC
@@ -28,182 +27,199 @@
  * Set global daemon state.
  * @param state @see daemon_states
  */
-void hipd_set_state(unsigned int state){
-       hipd_state = (state & HIPD_STATE_MASK) | (hipd_state & 
~HIPD_STATE_MASK);
+void hipd_set_state(unsigned int state)
+{
+    hipd_state = (state & HIPD_STATE_MASK) | (hipd_state & ~HIPD_STATE_MASK);
 }
 
-
 /**
  * Get global daemon flag status.
  * @param state @see daemon_states
  * @return 1 if flag is on, 0 if not.
  */
-int hipd_get_flag(unsigned int flag){
-       return (hipd_state & flag) ? 1 : 0;
+int hipd_get_flag(unsigned int flag)
+{
+    return (hipd_state & flag) ? 1 : 0;
 }
 
-
 /**
  * Set global daemon flag.
  * @param state @see daemon_states
  */
-void hipd_set_flag(unsigned int flag){
-       hipd_state = hipd_state | flag;
+void hipd_set_flag(unsigned int flag)
+{
+    hipd_state = hipd_state | flag;
 }
 
-
 /**
  * Get global daemon state.
  * @return @see daemon_states
  */
-unsigned int hipd_get_state(void){
-       return (hipd_state & HIPD_STATE_MASK);
+unsigned int hipd_get_state(void)
+{
+    return hipd_state & HIPD_STATE_MASK;
 }
 
-
 /**
  * Determines whether agent is alive, or not.
  *
  * @return non-zero, if agent is alive.
  */
-int hip_agent_is_alive(){
+int hip_agent_is_alive()
+{
 #ifdef CONFIG_HIP_AGENT
-       return hip_agent_status;
+    return hip_agent_status;
 #else
-       return 0;
+    return 0;
 #endif /* CONFIG_HIP_AGENT */
 }
 
-
 #ifdef CONFIG_HIP_OPPORTUNISTIC
 /**
  * No description.
  */
-int hip_set_opportunistic_mode(struct hip_common *msg){
-       int err =  0;
-       unsigned int *mode = NULL;
-       
-       mode = hip_get_param_contents(msg, HIP_PARAM_UINT);
-       if (!mode) {
-               err = -EINVAL;
-               goto out_err;
-       }
-  
-       HIP_DEBUG("mode=%d\n", *mode);
-
-       if(*mode == 0 || *mode == 1 || *mode == 2){
-               opportunistic_mode = *mode;
-       } else {
-               HIP_ERROR("Invalid value for opportunistic mode\n");
-               err = -EINVAL;
-               goto out_err;
-       }
-
-       hip_msg_init(msg);
-       HIP_IFE(hip_build_user_hdr(msg, 
-                                  (opportunistic_mode == 2 ? 
-                                   SO_HIP_SET_OPPTCP_ON : 
-                                   SO_HIP_SET_OPPTCP_OFF),
-                                  0), -1);
-       hip_set_opportunistic_tcp_status(msg);
-       
- out_err:
-       return err;
-}
-
-
-/**
- * No description.
- */
-int hip_query_opportunistic_mode(struct hip_common *msg){
-       int err = 0;
-       unsigned int opp_mode = opportunistic_mode;
-       
-       hip_msg_init(msg);
-       
-       HIP_IFEL(hip_build_user_hdr(msg,
-                                   SO_HIP_ANSWER_OPPORTUNISTIC_MODE_QUERY, 0),
-                -1, "build user header failed\n");
-       
-       HIP_IFEL(hip_build_param_contents(msg, (void *) &opp_mode,
-                                         HIP_PARAM_UINT,
-                                         sizeof(unsigned int)), -1,
-                "build param opp_mode failed\n");
-       
- out_err:
-  return err;
-}
-
-/**
-* No description.
- */
-int hip_query_ip_hit_mapping(struct hip_common *msg){
-       int err = 0;
-       unsigned int mapping = 0;
-       struct in6_addr *hit = NULL;
-       hip_ha_t *entry = NULL;
-       
-       
-       hit = (struct in6_addr *) hip_get_param_contents(msg, 
HIP_PARAM_PSEUDO_HIT);
-       HIP_ASSERT(hit_is_opportunistic_hashed_hit(hit));
-       
-       entry = hip_hadb_try_to_find_by_peer_hit(hit);
-       if(entry)
-               mapping = 1;
-       else 
-               mapping = 0;
-       
-       hip_msg_init(msg);
-       
-       HIP_IFEL(hip_build_user_hdr(msg,
-                                   SO_HIP_ANSWER_IP_HIT_MAPPING_QUERY, 0),
-                -1, "build user header failed\n");
-       HIP_IFEL(hip_build_param_contents(msg, (void *) &mapping,
-                                         HIP_PARAM_UINT,
-                                         sizeof(unsigned int)), -1,
-                "build param mapping failed\n");
-
- out_err:
-       return err;
-}
+int hip_set_opportunistic_mode(struct hip_common *msg)
+{
+    int err            =  0;
+    unsigned int *mode = NULL;
+
+    mode = hip_get_param_contents(msg, HIP_PARAM_UINT);
+    if (!mode) {
+        err = -EINVAL;
+        goto out_err;
+    }
+
+    HIP_DEBUG("mode=%d\n", *mode);
+
+    if (*mode == 0 || *mode == 1 || *mode == 2) {
+        opportunistic_mode = *mode;
+    } else {
+        HIP_ERROR("Invalid value for opportunistic mode\n");
+        err = -EINVAL;
+        goto out_err;
+    }
+
+    hip_msg_init(msg);
+    HIP_IFE(hip_build_user_hdr(msg,
+                               (opportunistic_mode == 2 ?
+                                SO_HIP_SET_OPPTCP_ON :
+                                SO_HIP_SET_OPPTCP_OFF),
+                               0), -1);
+    hip_set_opportunistic_tcp_status(msg);
+
+out_err:
+    return err;
+}
+
+/**
+ * No description.
+ */
+int hip_query_opportunistic_mode(struct hip_common *msg)
+{
+    int err               = 0;
+    unsigned int opp_mode = opportunistic_mode;
+
+    hip_msg_init(msg);
+
+    HIP_IFEL(hip_build_user_hdr(msg,
+                                SO_HIP_ANSWER_OPPORTUNISTIC_MODE_QUERY, 0),
+             -1, "build user header failed\n");
+
+    HIP_IFEL(hip_build_param_contents(msg, (void *) &opp_mode,
+                                      HIP_PARAM_UINT,
+                                      sizeof(unsigned int)), -1,
+             "build param opp_mode failed\n");
+
+out_err:
+    return err;
+}
+
+/**
+ * No description.
+ */
+int hip_query_ip_hit_mapping(struct hip_common *msg)
+{
+    int err              = 0;
+    unsigned int mapping = 0;
+    struct in6_addr *hit = NULL;
+    hip_ha_t *entry      = NULL;
+
+
+    hit = (struct in6_addr *) hip_get_param_contents(msg, 
HIP_PARAM_PSEUDO_HIT);
+    HIP_ASSERT(hit_is_opportunistic_hashed_hit(hit));
+
+    entry = hip_hadb_try_to_find_by_peer_hit(hit);
+    if (entry) {
+        mapping = 1;
+    } else {
+        mapping = 0;
+    }
+
+    hip_msg_init(msg);
+
+    HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_ANSWER_IP_HIT_MAPPING_QUERY, 0),
+             -1,
+             "build user header failed\n");
+    HIP_IFEL(hip_build_param_contents(msg,
+                                      (void *) &mapping,
+                                      HIP_PARAM_UINT,
+                                      sizeof(unsigned int)),
+              -1,
+              "build param mapping failed\n");
+
+out_err:
+    return err;
+}
+
 #endif // CONFIG_HIP_OPPORTUNISTIC
 
-int hip_get_hip_proxy_status(void){
-       return hipproxy;
-}
-
-int hip_set_hip_proxy_on(void){
-       int err = 0;
-       hipproxy = 1;
-       HIP_DEBUG("hip_set_hip_proxy_on() invoked.\n");
-       return err;
-}
-
-int hip_set_hip_proxy_off(void){
-       int err = 0;
-       hipproxy = 0;
-       HIP_DEBUG("hip_set_hip_proxy_off() invoked.\n");
-       return err;
-}
-
-int hip_get_sava_client_status(void) {
-  return hipsava_client;
-}
-int hip_get_sava_server_status(void) {
-  return hipsava_server;
-}
-void hip_set_sava_client_on(void) {
-  hipsava_client = 1;
-}
-
-void hip_set_sava_server_on(void) {
-  hipsava_server = 1;
-}
-
-void hip_set_sava_client_off(void) {
-  hipsava_client = 0;
-}
-
-void hip_set_sava_server_off(void) {
-  hipsava_server = 0;
+int hip_get_hip_proxy_status(void)
+{
+    return hipproxy;
+}
+
+int hip_set_hip_proxy_on(void)
+{
+    int err = 0;
+    hipproxy = 1;
+    HIP_DEBUG("hip_set_hip_proxy_on() invoked.\n");
+    return err;
+}
+
+int hip_set_hip_proxy_off(void)
+{
+    int err = 0;
+    hipproxy = 0;
+    HIP_DEBUG("hip_set_hip_proxy_off() invoked.\n");
+    return err;
+}
+
+int hip_get_sava_client_status(void)
+{
+    return hipsava_client;
+}
+
+int hip_get_sava_server_status(void)
+{
+    return hipsava_server;
+}
+
+void hip_set_sava_client_on(void)
+{
+    hipsava_client = 1;
+}
+
+void hip_set_sava_server_on(void)
+{
+    hipsava_server = 1;
+}
+
+void hip_set_sava_client_off(void)
+{
+    hipsava_client = 0;
+}
+
+void hip_set_sava_server_off(void)
+{
+    hipsava_server = 0;
 }

=== modified file 'hipd/accessor.c.doxyme'
--- hipd/accessor.c.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/accessor.c.doxyme      2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,145 +45,144 @@
 
 
 /**
- * hip_get_hip_proxy_status 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_get_sava_client_status 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_get_sava_server_status 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_query_ip_hit_mapping 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * hip_query_opportunistic_mode 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * hip_set_hip_proxy_off 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_set_hip_proxy_on 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_set_opportunistic_mode 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * hip_set_sava_client_off 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_set_sava_client_on 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_set_sava_server_off 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_set_sava_server_on 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hipd_get_flag 
- *
- *
- * @param flag
- * @return 
- **/
-
-
-/**
- * hipd_get_state 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hipd_set_flag 
- *
- *
- * @param flag
- * @return 
- **/
-
-
-/**
- * hipd_set_state 
+ * hip_get_hip_proxy_status
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_get_sava_client_status
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_get_sava_server_status
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_query_ip_hit_mapping
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * hip_query_opportunistic_mode
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * hip_set_hip_proxy_off
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_set_hip_proxy_on
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_set_opportunistic_mode
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * hip_set_sava_client_off
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_set_sava_client_on
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_set_sava_server_off
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_set_sava_server_on
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hipd_get_flag
+ *
+ *
+ * @param flag
+ * @return
+ **/
+
+
+/**
+ * hipd_get_state
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hipd_set_flag
+ *
+ *
+ * @param flag
+ * @return
+ **/
+
+
+/**
+ * hipd_set_state
  *
  *
  * @param state
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/accessor.h'
--- hipd/accessor.h     2010-01-19 09:28:42 +0000
+++ hipd/accessor.h     2010-02-10 22:32:46 +0000
@@ -17,23 +17,23 @@
  */
 
 /** Low mask for daemon states. */
-#define HIPD_STATE_MASK                0xff
+#define HIPD_STATE_MASK         0xff
 /** Daemon is ok and executing. */
-#define HIPD_STATE_EXEC                0x00
+#define HIPD_STATE_EXEC         0x00
 /** Daemon is closing. */
-#define HIPD_STATE_CLOSING     0x01
+#define HIPD_STATE_CLOSING      0x01
 /** Daemon is closed, exiting main(). */
-#define HIPD_STATE_CLOSED      0x02
+#define HIPD_STATE_CLOSED       0x02
 
 /** Daemon is restarting. */
-#define HIPD_FLAG_RESTART      0x00000100
+#define HIPD_FLAG_RESTART       0x00000100
 
 
 /* @}  */
 
-#define INDEX_HASH_LENGTH      SHA_DIGEST_LENGTH
+#define INDEX_HASH_LENGTH       SHA_DIGEST_LENGTH
 
-#define INDEX_HASH_FN          HIP_DIGEST_SHA1
+#define INDEX_HASH_FN           HIP_DIGEST_SHA1
 
 unsigned int hipd_get_state(void);
 void hipd_set_state(unsigned int);
@@ -54,7 +54,7 @@
 void hip_set_sava_server_off(void);
 
 /** Specifies the NAT status of the daemon. This value indicates if the current
-    machine is behind a NAT. Defined in hipd.c */
+ *  machine is behind a NAT. Defined in hipd.c */
 extern int hipproxy;
 
 /*SAVAH modes*/
@@ -64,4 +64,3 @@
 extern unsigned int opportunistic_mode;
 
 #endif /* HIPD_ACCESSOR_H */
-

=== modified file 'hipd/accessor.h.doxyme'
--- hipd/accessor.h.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/accessor.h.doxyme      2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/accessor.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/blind.c'
--- hipd/blind.c        2010-02-08 15:18:06 +0000
+++ hipd/blind.c        2010-02-10 22:32:46 +0000
@@ -1,6 +1,6 @@
 /** @file
  * This file defines tools of  blind extension for the Host Identity Protocol
- * 
+ *
  * @author  Laura Takkinen
  * @note    Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
  */
@@ -11,142 +11,142 @@
 
 int hip_check_whether_to_use_blind(hip_common_t *msg, hip_ha_t *entry,  int 
*use_blind)
 {
-       /* Check for error conditions. */
-       if(msg == NULL || use_blind == 0) {
-               *use_blind = 0;
-               return -1;
-       }
-       /* If packet controls define that BLIND should not be used, it does not
-          matter whether our host wishes to use BLIND or not.*/
-       if (((ntohs(msg->control) & HIP_PACKET_CTRL_BLIND)) == 0) {
-               *use_blind = 0;
-               return 0;
-       }
-       /* Packet controls define that we should use BLIND. Let's check if we
-          have BLIND activated. */
-       
-       /* If the host association state defines that BLIND should be used, we
-          don't have to take into account the global BLIND status. The global
-          BLIND bit is set using hipconf, but we may have already created some
-          HIP associations having BLIND bit on. */
-       if(entry != NULL && entry->blind != 0) {
-               *use_blind = 1;
-               return 0;
-       } else if (entry == NULL) {
-               *use_blind = 0;
-               return 0;
-       } else if(entry->blind == 0) {
-               *use_blind = 0;
-               return 0;
-       } else {
-               *use_blind = 0;
-               return -1;
-       }
+    /* Check for error conditions. */
+    if (msg == NULL || use_blind == 0) {
+        *use_blind = 0;
+        return -1;
+    }
+    /* If packet controls define that BLIND should not be used, it does not
+     * matter whether our host wishes to use BLIND or not.*/
+    if (((ntohs(msg->control) & HIP_PACKET_CTRL_BLIND)) == 0) {
+        *use_blind = 0;
+        return 0;
+    }
+    /* Packet controls define that we should use BLIND. Let's check if we
+     * have BLIND activated. */
+
+    /* If the host association state defines that BLIND should be used, we
+     * don't have to take into account the global BLIND status. The global
+     * BLIND bit is set using hipconf, but we may have already created some
+     * HIP associations having BLIND bit on. */
+    if (entry != NULL && entry->blind != 0) {
+        *use_blind = 1;
+        return 0;
+    } else if (entry == NULL) {
+        *use_blind = 0;
+        return 0;
+    } else if (entry->blind == 0) {
+        *use_blind = 0;
+        return 0;
+    } else {
+        *use_blind = 0;
+        return -1;
+    }
 }
 
-
 /* For internal use only */
 static int hip_set_blind_on_sa(hip_ha_t *entry, void *not_used)
 {
-  int err = 0;
-  
-  if(entry) {
-    entry->blind = 1;
-  }
-
-  return err;
+    int err = 0;
+
+    if (entry) {
+        entry->blind = 1;
+    }
+
+    return err;
 }
+
 /* For internal use only */
 static int hip_set_blind_off_sa(hip_ha_t *entry, void *not_used)
 {
-  int err = 0;
-  
-  if(entry) {
-    entry->blind = 0;
-  }
-
-  return err;
+    int err = 0;
+
+    if (entry) {
+        entry->blind = 0;
+    }
+
+    return err;
 }
 
 /* This functions is used to set the blind on.
  * "blind" entry of all existing associations is set to 1
  * and hip_blind_status is set to 1.
-*/
+ */
 int hip_set_blind_on(void)
 {
-  int err = 0;
-  
-  hip_blind_status = 1;
-  HIP_IFEL(hip_for_each_ha(hip_set_blind_on_sa, NULL), 0,
-          "for_each_ha err.\n");
-  
- out_err:
-  return err;
+    int err = 0;
+
+    hip_blind_status = 1;
+    HIP_IFEL(hip_for_each_ha(hip_set_blind_on_sa, NULL), 0,
+             "for_each_ha err.\n");
+
+out_err:
+    return err;
 }
 
 /* This functions is used to set the blind off.
  * "blind" entry of all existing associations is set to 0
  * and hip_blind_status is set to 0.
-*/
+ */
 int hip_set_blind_off(void)
 {
-  int err = 0;
-  
-  hip_blind_status = 0;
-  HIP_IFEL(hip_for_each_ha(hip_set_blind_off_sa, NULL), 0,
-          "for_each_ha err.\n");
-  
- out_err:
-  return err;
+    int err = 0;
+
+    hip_blind_status = 0;
+    HIP_IFEL(hip_for_each_ha(hip_set_blind_off_sa, NULL), 0,
+             "for_each_ha err.\n");
+
+out_err:
+    return err;
 }
 
 /**
  * @brief This functions is used to query if the blind is in use.
- * 
+ *
  * @return Zero if blind is off and 1 if blind is on.
  */
 int hip_blind_get_status(void)
 {
-  return hip_blind_status;
+    return hip_blind_status;
 }
 
 /* Extracts the uint16_t type nonce from the message.
  * @msg Message from where the nonce is extracted
  * @msg_nonce The nonce that the message included
  * Returns 0 in success, otherwise returns -1.
-*/
+ */
 int hip_blind_get_nonce(struct hip_common *msg, uint16_t *msg_nonce)
 {
-  int err = 0;
-  struct hip_blind_nonce *nonce = NULL;
+    int err                       = 0;
+    struct hip_blind_nonce *nonce = NULL;
 
-  // get value of the nonce from the i1 message
-  HIP_IFEL((nonce = hip_get_param(msg, HIP_PARAM_BLIND_NONCE)) == NULL, 
-          -1, "hip_get_param_nonce failed\n");
-  *msg_nonce = ntohs(nonce->nonce);
- out_err:
-  return err;
+    // get value of the nonce from the i1 message
+    HIP_IFEL((nonce = hip_get_param(msg, HIP_PARAM_BLIND_NONCE)) == NULL,
+             -1, "hip_get_param_nonce failed\n");
+    *msg_nonce = ntohs(nonce->nonce);
+out_err:
+    return err;
 }
 
 /**
  * @brief Forms a plain HIT from a blinded one.
- * 
+ *
  * @param  nonce     Nonce that is used to calculate the plain HIT.
  * @param  blind_hit Blinded HIT from where the plain HIT is formed.
  * @param  plain_hit Calculated plain HIT
  * @return           Zero in success, otherwise -1.
-*/
+ */
 int hip_plain_fingerprint(uint16_t *nonce,  struct in6_addr *blind_hit,
-                         struct in6_addr *plain_hit)
+                          struct in6_addr *plain_hit)
 {
-       int err = 0;
-    
-       HIP_IFEL(hip_blind_find_local_hi(nonce, blind_hit, plain_hit), 
-                -1, "hip_blind_find_local_hit failed\n");
-       _HIP_DEBUG_HIT("local hit_found", plain_hit);
-
- out_err:
-       return err;
+    int err = 0;
+
+    HIP_IFEL(hip_blind_find_local_hi(nonce, blind_hit, plain_hit),
+             -1, "hip_blind_find_local_hit failed\n");
+    _HIP_DEBUG_HIT("local hit_found", plain_hit);
+
+out_err:
+    return err;
 }
 
 /* Forms the blinded HIT from the key (SHA1 hash from the key).
@@ -155,322 +155,325 @@
  * @key_len Length of the key
  * @blind_hit Calculated blind HIT (hash)
  * Returns 0 in success, otherwise returns -1.
-*/
-int hip_do_blind(char *key, unsigned int key_len, struct in6_addr *blind_hit) 
+ */
+int hip_do_blind(char *key, unsigned int key_len, struct in6_addr *blind_hit)
 {
-  int err = 0;
-  u8 digest[HIP_AH_SHA_LEN];
-
-  HIP_DEBUG("\n");
-  
-  HIP_IFEL((err = hip_build_digest(HIP_DIGEST_SHA1, key, key_len, digest)), 
-          err, "Building of digest failed\n");
-
-  bzero(blind_hit, sizeof(hip_hit_t));
-  HIP_IFEL(khi_encode(digest, sizeof(digest) * 8,
-                     ((u8 *) blind_hit) + 3,
-                     sizeof(hip_hit_t) * 8 - HIP_HIT_PREFIX_LEN),
-          -1, "encoding failed\n");
-
-  HIP_DEBUG_HIT("HIT before prefix: ", blind_hit);
-  set_hit_prefix(blind_hit);
-  HIP_DEBUG_HIT("HIT after prefix: ", blind_hit);
-
-  /*memcpy(blind_hit, digest, sizeof(struct in6_addr));*/  
-
- out_err:
-  return err;
+    int err = 0;
+    u8 digest[HIP_AH_SHA_LEN];
+
+    HIP_DEBUG("\n");
+
+    HIP_IFEL((err = hip_build_digest(HIP_DIGEST_SHA1, key, key_len, digest)),
+             err, "Building of digest failed\n");
+
+    bzero(blind_hit, sizeof(hip_hit_t));
+    HIP_IFEL(khi_encode(digest, sizeof(digest) * 8,
+                        ((u8 *) blind_hit) + 3,
+                        sizeof(hip_hit_t) * 8 - HIP_HIT_PREFIX_LEN),
+             -1, "encoding failed\n");
+
+    HIP_DEBUG_HIT("HIT before prefix: ", blind_hit);
+    set_hit_prefix(blind_hit);
+    HIP_DEBUG_HIT("HIT after prefix: ", blind_hit);
+
+    /*memcpy(blind_hit, digest, sizeof(struct in6_addr));*/
+
+out_err:
+    return err;
 }
 
-
-/* Sets nonce and calculates the blinded 
+/* Sets nonce and calculates the blinded
  * fingerprints for the hip_ha_t entry.
  * @entry hip_ha_t entry which blind fields are adjusted
  * Returns 0 in success, otherwise returns -1.
-*/
+ */
 static int hip_blind_fingerprints(hip_ha_t *entry)
 {
-  int err = 0;
-  char *key_our = NULL, *key_peer = NULL;
-  unsigned int key_len = sizeof(struct in6_addr);
-
-  HIP_DEBUG("\n");
-  
-  // get nonce
-  get_random_bytes(&entry->blind_nonce_i, sizeof(uint16_t));
-
-  // generate key = nonce|hit_our
-  HIP_IFEL((key_our = HIP_MALLOC(sizeof(uint16_t)+ sizeof(struct in6_addr), 
0)) == NULL, 
-          -1, "Couldn't allocate memory\n");
-  memcpy(key_our, &entry->hit_our, sizeof(struct in6_addr));
-  memcpy(key_our + sizeof(struct in6_addr), &entry->blind_nonce_i, 
sizeof(uint16_t));
-  
-  // generate key = nonce|hit_peer
-  HIP_IFEL((key_peer = HIP_MALLOC(sizeof(uint16_t)+ sizeof(struct in6_addr), 
0)) == NULL, 
-          -1, "Couldn't allocate memory\n");
-  memcpy(key_peer, &entry->hit_peer, sizeof(struct in6_addr));
-  memcpy(key_peer + sizeof(struct in6_addr), &entry->blind_nonce_i, 
sizeof(uint16_t));
-  
-  // build digests
-  HIP_IFEL((err = hip_do_blind(key_our, key_len, &entry->hit_our_blind)), 
-          err, "Building of digest failed\n");
-  HIP_IFEL((err = hip_do_blind(key_peer, key_len, &entry->hit_peer_blind)), 
-          err, "Building of digest failed\n");
-
- out_err:
-  return err;
+    int err              = 0;
+    char *key_our        = NULL, *key_peer = NULL;
+    unsigned int key_len = sizeof(struct in6_addr);
+
+    HIP_DEBUG("\n");
+
+    // get nonce
+    get_random_bytes(&entry->blind_nonce_i, sizeof(uint16_t));
+
+    // generate key = nonce|hit_our
+    HIP_IFEL((key_our = HIP_MALLOC(sizeof(uint16_t) + sizeof(struct in6_addr), 
0)) == NULL,
+             -1, "Couldn't allocate memory\n");
+    memcpy(key_our, &entry->hit_our, sizeof(struct in6_addr));
+    memcpy(key_our + sizeof(struct in6_addr), &entry->blind_nonce_i, 
sizeof(uint16_t));
+
+    // generate key = nonce|hit_peer
+    HIP_IFEL((key_peer = HIP_MALLOC(sizeof(uint16_t) + sizeof(struct 
in6_addr), 0)) == NULL,
+             -1, "Couldn't allocate memory\n");
+    memcpy(key_peer, &entry->hit_peer, sizeof(struct in6_addr));
+    memcpy(key_peer + sizeof(struct in6_addr), &entry->blind_nonce_i, 
sizeof(uint16_t));
+
+    // build digests
+    HIP_IFEL((err = hip_do_blind(key_our, key_len, &entry->hit_our_blind)),
+             err, "Building of digest failed\n");
+    HIP_IFEL((err = hip_do_blind(key_peer, key_len, &entry->hit_peer_blind)),
+             err, "Building of digest failed\n");
+
+out_err:
+    return err;
 }
+
 /* Tests if @plain_hit blinded with nonce is same as @blind_hit*/
 int hip_blind_verify(uint16_t *nonce, struct in6_addr *plain_hit,
-                    struct in6_addr *blind_hit)
+                     struct in6_addr *blind_hit)
 {
-       int ret = 0;
-       char *key = NULL;
-       unsigned int key_len = sizeof(struct in6_addr);
-       struct in6_addr *test_hit = NULL;
-
-       HIP_DEBUG("\n");
-  
-       test_hit = HIP_MALLOC(sizeof(struct in6_addr), 0);
-       if (test_hit == NULL) {
-               HIP_ERROR("Couldn't allocate memory\n");
-               ret = -1;
-               goto out_err;
-       }
-
-       // generate key = nonce|hit_our
-       key = HIP_MALLOC(sizeof(uint16_t)+ sizeof(struct in6_addr), 0); 
-       if (key == NULL) { 
-               HIP_ERROR("Couldn't allocate memory\n");
-               ret = -1;
-               goto out_err;
-       }
-  
-       memcpy(key, plain_hit, sizeof(struct in6_addr));
-       memcpy(key + sizeof(struct in6_addr), nonce, sizeof(uint16_t));
-  
-       // build digests
-       ret = hip_do_blind(key, key_len, test_hit);
-       if (ret == -1) {
-               HIP_ERROR("Building of digest failed\n");
-               goto out_err;
-       } 
-       HIP_DEBUG_HIT("test_hit", test_hit);
-       HIP_DEBUG_HIT("blind_hit", blind_hit);
-       ret = ipv6_addr_cmp(test_hit, blind_hit);//memcmp return 0 if equal
- 
- out_err: 
-       if (test_hit)
-               HIP_FREE(test_hit);
-       return (ret == 0) ;
+    int ret                   = 0;
+    char *key                 = NULL;
+    unsigned int key_len      = sizeof(struct in6_addr);
+    struct in6_addr *test_hit = NULL;
+
+    HIP_DEBUG("\n");
+
+    test_hit = HIP_MALLOC(sizeof(struct in6_addr), 0);
+    if (test_hit == NULL) {
+        HIP_ERROR("Couldn't allocate memory\n");
+        ret = -1;
+        goto out_err;
+    }
+
+    // generate key = nonce|hit_our
+    key = HIP_MALLOC(sizeof(uint16_t) + sizeof(struct in6_addr), 0);
+    if (key == NULL) {
+        HIP_ERROR("Couldn't allocate memory\n");
+        ret = -1;
+        goto out_err;
+    }
+
+    memcpy(key, plain_hit, sizeof(struct in6_addr));
+    memcpy(key + sizeof(struct in6_addr), nonce, sizeof(uint16_t));
+
+    // build digests
+    ret = hip_do_blind(key, key_len, test_hit);
+    if (ret == -1) {
+        HIP_ERROR("Building of digest failed\n");
+        goto out_err;
+    }
+    HIP_DEBUG_HIT("test_hit", test_hit);
+    HIP_DEBUG_HIT("blind_hit", blind_hit);
+    ret = ipv6_addr_cmp(test_hit, blind_hit);    //memcmp return 0 if equal
+
+out_err:
+    if (test_hit) {
+        HIP_FREE(test_hit);
+    }
+    return ret == 0;
 }
 
-
 struct hip_common *hip_blind_build_i1(hip_ha_t *entry, uint16_t *mask)
 {
-  struct hip_common *i1;
-  int err = 0;
-
-  i1 = hip_msg_alloc();
-  if(i1 == NULL) {
-    HIP_ERROR("Out of memory\n");
-    return NULL;
-  }
-  *mask |= HIP_PACKET_CTRL_BLIND;
-  
-  if(entry->blind)
-    HIP_DEBUG("Blind flag is on\n");
-  else
-    HIP_DEBUG("Blind flag is off\n");
-
-  // Set blinded fingerprints
-  err = hip_blind_fingerprints(entry);
-  if(err) {
-    HIP_ERROR("hip_blind_fingerprints failed\n");
-    return NULL;
-  }
-  // Build network header by using blinded HITs
-  entry->hadb_misc_func->hip_build_network_hdr(i1, HIP_I1, *mask,
-                                              &entry->hit_our_blind,
-                                              &entry->hit_peer_blind);
-  HIP_DEBUG("add nonce to the message\n");
-  err = hip_build_param_blind_nonce(i1, entry->blind_nonce_i);
-  if(err) {
-    HIP_ERROR("Unable to attach nonce to the message.\n");
-    return NULL;
-  }
-  return i1;
+    struct hip_common *i1;
+    int err = 0;
+
+    i1 = hip_msg_alloc();
+    if (i1 == NULL) {
+        HIP_ERROR("Out of memory\n");
+        return NULL;
+    }
+    *mask |= HIP_PACKET_CTRL_BLIND;
+
+    if (entry->blind) {
+        HIP_DEBUG("Blind flag is on\n");
+    } else {
+        HIP_DEBUG("Blind flag is off\n");
+    }
+
+    // Set blinded fingerprints
+    err = hip_blind_fingerprints(entry);
+    if (err) {
+        HIP_ERROR("hip_blind_fingerprints failed\n");
+        return NULL;
+    }
+    // Build network header by using blinded HITs
+    entry->hadb_misc_func->hip_build_network_hdr(i1, HIP_I1, *mask,
+                                                 &entry->hit_our_blind,
+                                                 &entry->hit_peer_blind);
+    HIP_DEBUG("add nonce to the message\n");
+    err = hip_build_param_blind_nonce(i1, entry->blind_nonce_i);
+    if (err) {
+        HIP_ERROR("Unable to attach nonce to the message.\n");
+        return NULL;
+    }
+    return i1;
 }
 
 /* Fills the r2 message with required blind parameters*/
 int hip_blind_build_r2(struct hip_common *i2, struct hip_common *r2, hip_ha_t 
*entry, uint16_t *mask)
 {
-  int err = 0, host_id_in_enc_len = 0;
-  char *enc_in_msg = NULL, *host_id_in_enc = NULL;
-  unsigned char *iv = NULL;
-  
-  HIP_DEBUG("/n");
-
-  /*
-  *mask |= HIP_PACKET_CTRL_BLIND;
-    
-  // Build network header by using blinded HITs
-  entry->hadb_misc_func->
-    hip_build_network_hdr(r2, HIP_R2, *mask, &entry->hit_our_blind,
-    &entry->hit_peer_blind);*/
-  
-  /************ Encrypted ***********/
-  switch (entry->hip_transform) {
-  case HIP_HIP_AES_SHA1:
-    HIP_IFEL(hip_build_param_encrypted_aes_sha1(r2, (struct hip_tlv_common 
*)entry->our_pub), 
-            -1, "Building of param encrypted failed.\n");
-    enc_in_msg = hip_get_param(r2, HIP_PARAM_ENCRYPTED);
-    HIP_ASSERT(enc_in_msg); /* Builder internal error. */
-    iv = ((struct hip_encrypted_aes_sha1 *) enc_in_msg)->iv;
-    get_random_bytes(iv, 16);
-    host_id_in_enc = enc_in_msg +
-      sizeof(struct hip_encrypted_aes_sha1);
-    break;
-  case HIP_HIP_3DES_SHA1:
-    HIP_IFEL(hip_build_param_encrypted_3des_sha1(r2, (struct hip_tlv_common 
*)entry->our_pub), 
-            -1, "Building of param encrypted failed.\n");
-    enc_in_msg = hip_get_param(r2, HIP_PARAM_ENCRYPTED);
-    HIP_ASSERT(enc_in_msg); /* Builder internal error. */
-    iv = ((struct hip_encrypted_3des_sha1 *) enc_in_msg)->iv;
-    get_random_bytes(iv, 8);
-    host_id_in_enc = enc_in_msg +
-      sizeof(struct hip_encrypted_3des_sha1);
-    break;
-  case HIP_HIP_NULL_SHA1:
-    HIP_IFEL(hip_build_param_encrypted_null_sha1(r2, (struct hip_tlv_common 
*)entry->our_pub), 
-            -1, "Building of param encrypted failed.\n");
-    enc_in_msg = hip_get_param(r2, HIP_PARAM_ENCRYPTED);
-    HIP_ASSERT(enc_in_msg); /* Builder internal error. */
-    iv = NULL;
-    host_id_in_enc = enc_in_msg +
-      sizeof(struct hip_encrypted_null_sha1);
-    break;
-  default:
-    HIP_IFEL(1, -ENOSYS, "HIP transform not supported (%d)\n",
-            &entry->hip_transform);
-  }
-  
-  
-  /* Calculate the length of the host id inside the encrypted param */
-  host_id_in_enc_len = hip_get_param_total_len(host_id_in_enc);
-  
-  /* Adjust the host id length for AES (block size 16).
-     build_param_encrypted_aes has already taken care that there is
-     enough padding */
-  if (entry->hip_transform == HIP_HIP_AES_SHA1) {
-    int remainder = host_id_in_enc_len % 16;
-    if (remainder) {
-      HIP_DEBUG("Remainder %d (for AES)\n", remainder);
-      host_id_in_enc_len += remainder;
-    }
-  }
-  
-  HIP_IFEL(hip_crypto_encrypted(host_id_in_enc, iv,
-                               entry->hip_transform,
-                               host_id_in_enc_len,
-                               &entry->hip_enc_out.key,
-                               HIP_DIRECTION_ENCRYPT), -1, 
-          "Building of param encrypted failed\n");
-  
- out_err:
-  return err;
+    int err           = 0, host_id_in_enc_len = 0;
+    char *enc_in_msg  = NULL, *host_id_in_enc = NULL;
+    unsigned char *iv = NULL;
+
+    HIP_DEBUG("/n");
+
+    /*
+     *mask |= HIP_PACKET_CTRL_BLIND;
+     *
+     * // Build network header by using blinded HITs
+     * entry->hadb_misc_func->
+     * hip_build_network_hdr(r2, HIP_R2, *mask, &entry->hit_our_blind,
+     * &entry->hit_peer_blind);*/
+
+    /************ Encrypted ***********/
+    switch (entry->hip_transform) {
+    case HIP_HIP_AES_SHA1:
+        HIP_IFEL(hip_build_param_encrypted_aes_sha1(r2, (struct hip_tlv_common 
*) entry->our_pub),
+                 -1, "Building of param encrypted failed.\n");
+        enc_in_msg     = hip_get_param(r2, HIP_PARAM_ENCRYPTED);
+        HIP_ASSERT(enc_in_msg); /* Builder internal error. */
+        iv             = ((struct hip_encrypted_aes_sha1 *) enc_in_msg)->iv;
+        get_random_bytes(iv, 16);
+        host_id_in_enc = enc_in_msg +
+                         sizeof(struct hip_encrypted_aes_sha1);
+        break;
+    case HIP_HIP_3DES_SHA1:
+        HIP_IFEL(hip_build_param_encrypted_3des_sha1(r2, (struct 
hip_tlv_common *) entry->our_pub),
+                 -1, "Building of param encrypted failed.\n");
+        enc_in_msg     = hip_get_param(r2, HIP_PARAM_ENCRYPTED);
+        HIP_ASSERT(enc_in_msg); /* Builder internal error. */
+        iv             = ((struct hip_encrypted_3des_sha1 *) enc_in_msg)->iv;
+        get_random_bytes(iv, 8);
+        host_id_in_enc = enc_in_msg +
+                         sizeof(struct hip_encrypted_3des_sha1);
+        break;
+    case HIP_HIP_NULL_SHA1:
+        HIP_IFEL(hip_build_param_encrypted_null_sha1(r2, (struct 
hip_tlv_common *) entry->our_pub),
+                 -1, "Building of param encrypted failed.\n");
+        enc_in_msg     = hip_get_param(r2, HIP_PARAM_ENCRYPTED);
+        HIP_ASSERT(enc_in_msg); /* Builder internal error. */
+        iv             = NULL;
+        host_id_in_enc = enc_in_msg +
+                         sizeof(struct hip_encrypted_null_sha1);
+        break;
+    default:
+        HIP_IFEL(1, -ENOSYS, "HIP transform not supported (%d)\n",
+                 &entry->hip_transform);
+    }
+
+
+    /* Calculate the length of the host id inside the encrypted param */
+    host_id_in_enc_len = hip_get_param_total_len(host_id_in_enc);
+
+    /* Adjust the host id length for AES (block size 16).
+     * build_param_encrypted_aes has already taken care that there is
+     * enough padding */
+    if (entry->hip_transform == HIP_HIP_AES_SHA1) {
+        int remainder = host_id_in_enc_len % 16;
+        if (remainder) {
+            HIP_DEBUG("Remainder %d (for AES)\n", remainder);
+            host_id_in_enc_len += remainder;
+        }
+    }
+
+    HIP_IFEL(hip_crypto_encrypted(host_id_in_enc, iv,
+                                  entry->hip_transform,
+                                  host_id_in_enc_len,
+                                  &entry->hip_enc_out.key,
+                                  HIP_DIRECTION_ENCRYPT), -1,
+             "Building of param encrypted failed\n");
+
+out_err:
+    return err;
 }
 
-int hip_blind_verify_r2(struct hip_common *r2, hip_ha_t *entry) 
+int hip_blind_verify_r2(struct hip_common *r2, hip_ha_t *entry)
 {
-  int err = 0;
-  char *tmp_enc = NULL, *enc = NULL;
-  struct hip_host_id *host_id_in_enc = NULL;
-  unsigned char *iv = NULL;
-  uint16_t crypto_len;
-  struct in6_addr *plain_peer_hit = NULL;
-
-  //initiator has a state related to blinded hits -> finding plain hits and 
key material easily
-  // get encrypted parameter and decrypt it -> should contain responder public 
key
-  // verify that the plain fingerprint corresponds decrypted 
-  
-  /* Extract encrypted parameter and decrypt it. There should be public host id
-   * of responder.
-   */
-  /* Decrypt the HOST_ID and verify it against the sender HIT. */
-  HIP_IFEL(!(enc = hip_get_param(r2, HIP_PARAM_ENCRYPTED)),
-          -ENOENT, "Could not find enc parameter\n");
-  
-  HIP_IFEL(!(tmp_enc = HIP_MALLOC(hip_get_param_total_len(enc),
-                                 GFP_KERNEL)), -ENOMEM,
-          "No memory for temporary host_id\n");
-  memcpy(tmp_enc, enc, hip_get_param_total_len(enc));
-
-  /* Decrypt ENCRYPTED field. */
-  
-  switch (entry->hip_transform) {
-  case HIP_HIP_AES_SHA1:
-    host_id_in_enc = (struct hip_host_id *)
-      (tmp_enc + sizeof(struct hip_encrypted_aes_sha1));
-    iv = ((struct hip_encrypted_aes_sha1 *) tmp_enc)->iv;
-    /* 4 = reserved, 16 = iv */
-    crypto_len = hip_get_param_contents_len(enc) - 4 - 16;
-    HIP_DEBUG("aes crypto len: %d\n", crypto_len);
-    break;
-  case HIP_HIP_3DES_SHA1:
-    host_id_in_enc = (struct hip_host_id *)
-      (tmp_enc + sizeof(struct hip_encrypted_3des_sha1));
-    iv = ((struct hip_encrypted_3des_sha1 *) tmp_enc)->iv;
-    /* 4 = reserved, 8 = iv */
-    crypto_len = hip_get_param_contents_len(enc) - 4 - 8;
-    break;
-  case HIP_HIP_NULL_SHA1:
-    host_id_in_enc = (struct hip_host_id *)
-      (tmp_enc + sizeof(struct hip_encrypted_null_sha1));
-    iv = NULL;
-    /* 4 = reserved */
-    crypto_len = hip_get_param_contents_len(enc) - 4;
-    break;
-  default:
-    HIP_IFEL(1, -EINVAL, "Unknown HIP transform: %d\n", &entry->hip_transform);
-  }
-  
-  HIP_IFEL(hip_crypto_encrypted(host_id_in_enc, iv, entry->hip_transform,
-                               crypto_len, &entry->hip_enc_in.key,
-                               HIP_DIRECTION_DECRYPT), -EINVAL,
-          "Decryption of Host ID failed\n");
-  
-  HIP_HEXDUMP("Decrypted HOST_ID", host_id_in_enc,
-             hip_get_param_total_len(host_id_in_enc));
-  
-  HIP_IFEL(hip_get_param_type(host_id_in_enc) != HIP_PARAM_HOST_ID, -EINVAL,
-          "The decrypted parameter is not a host id\n");
-  
-  HIP_IFEL((plain_peer_hit = HIP_MALLOC(sizeof(struct in6_addr), 0)) == NULL,
-          -1, "Couldn't allocate memory\n");
-  HIP_IFEL(hip_host_id_to_hit(host_id_in_enc, plain_peer_hit, 
HIP_HIT_TYPE_HASH100),
-          -1, "hip_host_id_to_hit faile\n");
-  HIP_IFEL(hip_blind_verify(&entry->blind_nonce_i, 
-                           plain_peer_hit, 
-                           &r2->hits) != 1, -1, "hip_blind_verity failed\n");
-          
-  /* Store the peer's public key to HA and validate it */
-  /** @todo Do not store the key if the verification fails. */
-  HIP_IFE(hip_init_peer(entry, r2, host_id_in_enc), -EINVAL); 
-  HIP_IFEL(entry->verify(entry->peer_pub_key, r2), -EINVAL,
-          "Verification of R1 signature failed\n");
-  
- out_err:
-  if(tmp_enc)
-    HIP_FREE(tmp_enc);
-  if(plain_peer_hit)
-    HIP_FREE(plain_peer_hit);
-  return err;
+    int err                            = 0;
+    char *tmp_enc                      = NULL, *enc = NULL;
+    struct hip_host_id *host_id_in_enc = NULL;
+    unsigned char *iv                  = NULL;
+    uint16_t crypto_len;
+    struct in6_addr *plain_peer_hit    = NULL;
+
+    //initiator has a state related to blinded hits -> finding plain hits and 
key material easily
+    // get encrypted parameter and decrypt it -> should contain responder 
public key
+    // verify that the plain fingerprint corresponds decrypted
+
+    /* Extract encrypted parameter and decrypt it. There should be public host 
id
+     * of responder.
+     */
+    /* Decrypt the HOST_ID and verify it against the sender HIT. */
+    HIP_IFEL(!(enc = hip_get_param(r2, HIP_PARAM_ENCRYPTED)),
+             -ENOENT, "Could not find enc parameter\n");
+
+    HIP_IFEL(!(tmp_enc = HIP_MALLOC(hip_get_param_total_len(enc),
+                                    GFP_KERNEL)), -ENOMEM,
+             "No memory for temporary host_id\n");
+    memcpy(tmp_enc, enc, hip_get_param_total_len(enc));
+
+    /* Decrypt ENCRYPTED field. */
+
+    switch (entry->hip_transform) {
+    case HIP_HIP_AES_SHA1:
+        host_id_in_enc = (struct hip_host_id *)
+                         (tmp_enc + sizeof(struct hip_encrypted_aes_sha1));
+        iv             = ((struct hip_encrypted_aes_sha1 *) tmp_enc)->iv;
+        /* 4 = reserved, 16 = iv */
+        crypto_len     = hip_get_param_contents_len(enc) - 4 - 16;
+        HIP_DEBUG("aes crypto len: %d\n", crypto_len);
+        break;
+    case HIP_HIP_3DES_SHA1:
+        host_id_in_enc = (struct hip_host_id *)
+                         (tmp_enc + sizeof(struct hip_encrypted_3des_sha1));
+        iv             = ((struct hip_encrypted_3des_sha1 *) tmp_enc)->iv;
+        /* 4 = reserved, 8 = iv */
+        crypto_len     = hip_get_param_contents_len(enc) - 4 - 8;
+        break;
+    case HIP_HIP_NULL_SHA1:
+        host_id_in_enc = (struct hip_host_id *)
+                         (tmp_enc + sizeof(struct hip_encrypted_null_sha1));
+        iv             = NULL;
+        /* 4 = reserved */
+        crypto_len     = hip_get_param_contents_len(enc) - 4;
+        break;
+    default:
+        HIP_IFEL(1, -EINVAL, "Unknown HIP transform: %d\n", 
&entry->hip_transform);
+    }
+
+    HIP_IFEL(hip_crypto_encrypted(host_id_in_enc, iv, entry->hip_transform,
+                                  crypto_len, &entry->hip_enc_in.key,
+                                  HIP_DIRECTION_DECRYPT), -EINVAL,
+             "Decryption of Host ID failed\n");
+
+    HIP_HEXDUMP("Decrypted HOST_ID", host_id_in_enc,
+                hip_get_param_total_len(host_id_in_enc));
+
+    HIP_IFEL(hip_get_param_type(host_id_in_enc) != HIP_PARAM_HOST_ID, -EINVAL,
+             "The decrypted parameter is not a host id\n");
+
+    HIP_IFEL((plain_peer_hit = HIP_MALLOC(sizeof(struct in6_addr), 0)) == NULL,
+             -1, "Couldn't allocate memory\n");
+    HIP_IFEL(hip_host_id_to_hit(host_id_in_enc, plain_peer_hit, 
HIP_HIT_TYPE_HASH100),
+             -1, "hip_host_id_to_hit faile\n");
+    HIP_IFEL(hip_blind_verify(&entry->blind_nonce_i,
+                              plain_peer_hit,
+                              &r2->hits) != 1, -1, "hip_blind_verity 
failed\n");
+
+    /* Store the peer's public key to HA and validate it */
+    /** @todo Do not store the key if the verification fails. */
+    HIP_IFE(hip_init_peer(entry, r2, host_id_in_enc), -EINVAL);
+    HIP_IFEL(entry->verify(entry->peer_pub_key, r2), -EINVAL,
+             "Verification of R1 signature failed\n");
+
+out_err:
+    if (tmp_enc) {
+        HIP_FREE(tmp_enc);
+    }
+    if (plain_peer_hit) {
+        HIP_FREE(plain_peer_hit);
+    }
+    return err;
 }
 
 /**
  * Constructs a new R1 packet payload.
- * 
+ *
  * @param src_hit      a pointer to the source host identity tag used in the
  *                     packet.
  * @param sign         a funtion pointer to a signature funtion.
@@ -480,196 +483,201 @@
  * @return             zero on success, or negative error value on error.
  */
 struct hip_common *hip_blind_create_r1(const struct in6_addr *src_hit,
-                                      int (*sign)(void *key, struct hip_common 
*m),
-                                      void *private_key,
-                                      const struct hip_host_id *host_id_pub,
-                                      int cookie_k)
+                                       int (*sign)(void *key, struct 
hip_common *m),
+                                       void *private_key,
+                                       const struct hip_host_id *host_id_pub,
+                                       int cookie_k)
 {
-       hip_common_t *msg = NULL;
-       int err = 0, dh_size1 = 0, dh_size2 = 0, written1 = 0, written2 = 0;
-       int mask = 0;
-       u8 *dh_data1 = NULL, *dh_data2 = NULL;
-       hip_srv_t service_list[HIP_TOTAL_EXISTING_SERVICES];
-       unsigned int service_count = 0;
-       
-       /* Supported HIP and ESP transforms. */
-       hip_transform_suite_t transform_hip_suite[] = {
-               HIP_HIP_AES_SHA1,
-               HIP_HIP_3DES_SHA1,
-               HIP_HIP_NULL_SHA1
-       };
-       hip_transform_suite_t transform_esp_suite[] = {
-               HIP_ESP_3DES_SHA1,
-               HIP_ESP_AES_SHA1,
-               HIP_ESP_NULL_SHA1
-       };
-       
-       HIP_DEBUG("hip_blind_create_r1() invoked.\n");
-
-       HIP_IFEL(!(msg = hip_msg_alloc()), -ENOMEM, "Out of memory\n");
-
-       /* Allocate memory for writing the first Diffie-Hellman shared secret */
-       HIP_IFEL((dh_size1 = hip_get_dh_size(HIP_FIRST_DH_GROUP_ID)) == 0, 
-                -1, "Could not get dh_size1\n");
-       HIP_IFEL(!(dh_data1 = HIP_MALLOC(dh_size1, GFP_ATOMIC)), 
-                -1, "Failed to alloc memory for dh_data1\n");
-       memset(dh_data1, 0, dh_size1);
-
-       _HIP_DEBUG("dh_size=%d\n", dh_size1);
-
-       /* Allocate memory for writing the second Diffie-Hellman shared secret 
*/
-       HIP_IFEL((dh_size2 = hip_get_dh_size(HIP_SECOND_DH_GROUP_ID)) == 0, 
-                -1, "Could not get dh_size2\n");
-       HIP_IFEL(!(dh_data2 = HIP_MALLOC(dh_size2, GFP_ATOMIC)), 
-                -1, "Failed to alloc memory for dh_data2\n");
-       memset(dh_data2, 0, dh_size2);
-
-       _HIP_DEBUG("dh_size=%d\n", dh_size2);
-       
-       /* Ready to begin building of the R1 packet */
-       mask |= HIP_PACKET_CTRL_BLIND;
-
-       HIP_DEBUG("mask=0x%x\n", mask);
-       /*! \todo TH: hip_build_network_hdr has to be replaced with an 
apprporiate function pointer */
-       HIP_DEBUG_HIT("src_hit used to build r1 network header", src_hit);
-       hip_build_network_hdr(msg, HIP_R1, mask, src_hit, NULL);
-
-       /********** R1_COUNTER (OPTIONAL) *********/
-
-       /********** PUZZLE ************/
-       HIP_IFEL(hip_build_param_puzzle(msg, cookie_k,
-               42 /* 2^(42-32) sec lifetime */, 0, 0),
-                 -1, "Cookies were burned. Bummer!\n");
-
-       /********** Diffie-Hellman **********/
-       HIP_IFEL((written1 = hip_insert_dh(dh_data1, dh_size1,
-                                         HIP_FIRST_DH_GROUP_ID)) < 0,
-                -1, "Could not extract the first DH public key\n");
-       HIP_IFEL((written2 = hip_insert_dh(dh_data2, dh_size2,
-                                         HIP_SECOND_DH_GROUP_ID)) < 0,
-                -1, "Could not extract the second DH public key\n");
-
-       HIP_IFEL(hip_build_param_diffie_hellman_contents(msg,
-                HIP_FIRST_DH_GROUP_ID, dh_data1, written1, 
-                HIP_SECOND_DH_GROUP_ID, dh_data2, written2), -1,
-                "Building of DH failed.\n");
-
-
-       /********** HIP transform. **********/
-       HIP_IFEL(hip_build_param_hip_transform(msg,
-                                          transform_hip_suite,
-                                          sizeof(transform_hip_suite) /
-                                          sizeof(hip_transform_suite_t)), -1, 
-                "Building of HIP transform failed\n");
-
-       /********** ESP-ENC transform. **********/
-       HIP_IFEL(hip_build_param_esp_transform(msg,
-                                          transform_esp_suite,
-                                          sizeof(transform_esp_suite) /
-                                          sizeof(hip_transform_suite_t)), -1, 
-                "Building of ESP transform failed\n");
-
-       
-       /********** Host_id  is not included in the the blinded R1 **********/
-
-
-       /********** REG_INFO *********/
-       hip_get_active_services(service_list, &service_count);
-       hip_build_param_reg_info(msg, service_list, service_count);
-       
-       /*
-         int *list;
-         int count = 0;
-               
-         count = hip_get_services_list(&list);
-       
-         HIP_DEBUG("Amount of services is %d.\n", count);
-       
-         int i;
-         for (i = 0; i < count; i++) {
-         HIP_DEBUG("Service is %d.\n", list[i]);
-         }
-       
-         if (count > 0) {
-         HIP_DEBUG("Adding REG_INFO parameter.\n");
-         HIP_IFEL(hip_build_param_reg_info(msg,  0, 0, list, count), -1, 
-         "Building of reg_info failed\n");     
-         }*/
-
-       /********** ECHO_REQUEST_SIGN (OPTIONAL) *********/
-
-       //HIP_HEXDUMP("Pubkey:", host_id_pub, 
hip_get_param_total_len(host_id_pub));
-
-       /********** Signature 2 **********/     
-       HIP_IFEL(sign(private_key, msg), -1, "Signing of R1 failed.\n");
-       _HIP_HEXDUMP("R1", msg, hip_get_msg_total_len(msg));
-
-       /********** ECHO_REQUEST (OPTIONAL) *********/
-
-       /* Fill puzzle parameters */
-       {
-               struct hip_puzzle *pz;
-               uint64_t random_i;
-
-               HIP_IFEL(!(pz = hip_get_param(msg, HIP_PARAM_PUZZLE)), -1, 
-                        "Internal error\n");
-
-               // FIX ME: this does not always work:
-               //get_random_bytes(pz->opaque, HIP_PUZZLE_OPAQUE_LEN);
-
-               /* hardcode kludge */
-               pz->opaque[0] = 'H';
-               pz->opaque[1] = 'I';
-               //pz->opaque[2] = 'P';
-               /* todo: remove random_i variable */
-               get_random_bytes(&random_i,sizeof(random_i));
-               pz->I = random_i;
-       }
-
-       /************** Packet ready ***************/
-
-       if (dh_data1)
-               HIP_FREE(dh_data1);
-       if (dh_data2)
-               HIP_FREE(dh_data2);
-
-       return msg;
-
-  out_err:
-       //      if (host_id_pub)
-       //      HIP_FREE(host_id_pub);
-       if (msg)
-               HIP_FREE(msg);
-       if (dh_data1)
-               HIP_FREE(dh_data1);
-       if (dh_data2)
-               HIP_FREE(dh_data2);
-
-       return NULL;
+    hip_common_t *msg = NULL;
+    int err                                     = 0, dh_size1 = 0, dh_size2 = 
0, written1 = 0, written2 = 0;
+    int mask                                    = 0;
+    u8 *dh_data1                                = NULL, *dh_data2 = NULL;
+    hip_srv_t service_list[HIP_TOTAL_EXISTING_SERVICES];
+    unsigned int service_count                  = 0;
+
+    /* Supported HIP and ESP transforms. */
+    hip_transform_suite_t transform_hip_suite[] = {
+        HIP_HIP_AES_SHA1,
+        HIP_HIP_3DES_SHA1,
+        HIP_HIP_NULL_SHA1
+    };
+    hip_transform_suite_t transform_esp_suite[] = {
+        HIP_ESP_3DES_SHA1,
+        HIP_ESP_AES_SHA1,
+        HIP_ESP_NULL_SHA1
+    };
+
+    HIP_DEBUG("hip_blind_create_r1() invoked.\n");
+
+    HIP_IFEL(!(msg = hip_msg_alloc()), -ENOMEM, "Out of memory\n");
+
+    /* Allocate memory for writing the first Diffie-Hellman shared secret */
+    HIP_IFEL((dh_size1 = hip_get_dh_size(HIP_FIRST_DH_GROUP_ID)) == 0,
+             -1, "Could not get dh_size1\n");
+    HIP_IFEL(!(dh_data1 = HIP_MALLOC(dh_size1, GFP_ATOMIC)),
+             -1, "Failed to alloc memory for dh_data1\n");
+    memset(dh_data1, 0, dh_size1);
+
+    _HIP_DEBUG("dh_size=%d\n", dh_size1);
+
+    /* Allocate memory for writing the second Diffie-Hellman shared secret */
+    HIP_IFEL((dh_size2 = hip_get_dh_size(HIP_SECOND_DH_GROUP_ID)) == 0,
+             -1, "Could not get dh_size2\n");
+    HIP_IFEL(!(dh_data2 = HIP_MALLOC(dh_size2, GFP_ATOMIC)),
+             -1, "Failed to alloc memory for dh_data2\n");
+    memset(dh_data2, 0, dh_size2);
+
+    _HIP_DEBUG("dh_size=%d\n", dh_size2);
+
+    /* Ready to begin building of the R1 packet */
+    mask |= HIP_PACKET_CTRL_BLIND;
+
+    HIP_DEBUG("mask=0x%x\n", mask);
+    /*! \todo TH: hip_build_network_hdr has to be replaced with an apprporiate 
function pointer */
+    HIP_DEBUG_HIT("src_hit used to build r1 network header", src_hit);
+    hip_build_network_hdr(msg, HIP_R1, mask, src_hit, NULL);
+
+    /********** R1_COUNTER (OPTIONAL) *********/
+
+    /********** PUZZLE ************/
+    HIP_IFEL(hip_build_param_puzzle(msg, cookie_k,
+                                    42 /* 2^(42-32) sec lifetime */, 0, 0),
+             -1, "Cookies were burned. Bummer!\n");
+
+    /********** Diffie-Hellman **********/
+    HIP_IFEL((written1 = hip_insert_dh(dh_data1, dh_size1,
+                                       HIP_FIRST_DH_GROUP_ID)) < 0,
+             -1, "Could not extract the first DH public key\n");
+    HIP_IFEL((written2 = hip_insert_dh(dh_data2, dh_size2,
+                                       HIP_SECOND_DH_GROUP_ID)) < 0,
+             -1, "Could not extract the second DH public key\n");
+
+    HIP_IFEL(hip_build_param_diffie_hellman_contents(msg,
+                                                     HIP_FIRST_DH_GROUP_ID, 
dh_data1, written1,
+                                                     HIP_SECOND_DH_GROUP_ID, 
dh_data2, written2), -1,
+             "Building of DH failed.\n");
+
+
+    /********** HIP transform. **********/
+    HIP_IFEL(hip_build_param_hip_transform(msg,
+                                           transform_hip_suite,
+                                           sizeof(transform_hip_suite) /
+                                           sizeof(hip_transform_suite_t)), -1,
+             "Building of HIP transform failed\n");
+
+    /********** ESP-ENC transform. **********/
+    HIP_IFEL(hip_build_param_esp_transform(msg,
+                                           transform_esp_suite,
+                                           sizeof(transform_esp_suite) /
+                                           sizeof(hip_transform_suite_t)), -1,
+             "Building of ESP transform failed\n");
+
+
+    /********** Host_id  is not included in the the blinded R1 **********/
+
+
+    /********** REG_INFO *********/
+    hip_get_active_services(service_list, &service_count);
+    hip_build_param_reg_info(msg, service_list, service_count);
+
+    /*
+     * int *list;
+     * int count = 0;
+     *
+     * count = hip_get_services_list(&list);
+     *
+     * HIP_DEBUG("Amount of services is %d.\n", count);
+     *
+     * int i;
+     * for (i = 0; i < count; i++) {
+     * HIP_DEBUG("Service is %d.\n", list[i]);
+     * }
+     *
+     * if (count > 0) {
+     * HIP_DEBUG("Adding REG_INFO parameter.\n");
+     * HIP_IFEL(hip_build_param_reg_info(msg,  0, 0, list, count), -1,
+     * "Building of reg_info failed\n");
+     * }*/
+
+    /********** ECHO_REQUEST_SIGN (OPTIONAL) *********/
+
+    //HIP_HEXDUMP("Pubkey:", host_id_pub, 
hip_get_param_total_len(host_id_pub));
+
+    /********** Signature 2 **********/
+    HIP_IFEL(sign(private_key, msg), -1, "Signing of R1 failed.\n");
+    _HIP_HEXDUMP("R1", msg, hip_get_msg_total_len(msg));
+
+    /********** ECHO_REQUEST (OPTIONAL) *********/
+
+    /* Fill puzzle parameters */
+    {
+        struct hip_puzzle *pz;
+        uint64_t random_i;
+
+        HIP_IFEL(!(pz = hip_get_param(msg, HIP_PARAM_PUZZLE)), -1,
+                 "Internal error\n");
+
+        // FIX ME: this does not always work:
+        //get_random_bytes(pz->opaque, HIP_PUZZLE_OPAQUE_LEN);
+
+        /* hardcode kludge */
+        pz->opaque[0] = 'H';
+        pz->opaque[1] = 'I';
+        //pz->opaque[2] = 'P';
+        /* todo: remove random_i variable */
+        get_random_bytes(&random_i, sizeof(random_i));
+        pz->I         = random_i;
+    }
+
+    /************** Packet ready ***************/
+
+    if (dh_data1) {
+        HIP_FREE(dh_data1);
+    }
+    if (dh_data2) {
+        HIP_FREE(dh_data2);
+    }
+
+    return msg;
+
+out_err:
+    // if (host_id_pub)
+    // HIP_FREE(host_id_pub);
+    if (msg) {
+        HIP_FREE(msg);
+    }
+    if (dh_data1) {
+        HIP_FREE(dh_data1);
+    }
+    if (dh_data2) {
+        HIP_FREE(dh_data2);
+    }
+
+    return NULL;
 }
 
-int hip_blind_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit, 
-                    int (*sign)(void *key, struct hip_common *m),
-                    void *privkey, struct hip_host_id *pubkey)
+int hip_blind_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit,
+                           int (*sign)(void *key, struct hip_common *m),
+                           void *privkey, struct hip_host_id *pubkey)
 {
-       int i=0;
-       for(i = 0; i < HIP_R1TABLESIZE; i++) {
-               int cookie_k;
-
-               cookie_k = hip_get_cookie_difficulty(NULL);
-
-               r1table[i].r1 = hip_blind_create_r1(hit, sign, privkey, pubkey,
-                                             cookie_k);
-               if (!r1table[i].r1) {
-                       HIP_ERROR("Unable to precreate R1s\n");
-                       goto err_out;
-               }
-
-               HIP_DEBUG("Packet %d created\n", i);
-       }
-
-       return 1;
-
- err_out:
-       return 0;
+    int i = 0;
+    for (i = 0; i < HIP_R1TABLESIZE; i++) {
+        int cookie_k;
+
+        cookie_k      = hip_get_cookie_difficulty(NULL);
+
+        r1table[i].r1 = hip_blind_create_r1(hit, sign, privkey, pubkey,
+                                            cookie_k);
+        if (!r1table[i].r1) {
+            HIP_ERROR("Unable to precreate R1s\n");
+            goto err_out;
+        }
+
+        HIP_DEBUG("Packet %d created\n", i);
+    }
+
+    return 1;
+
+err_out:
+    return 0;
 }

=== modified file 'hipd/blind.c.doxyme'
--- hipd/blind.c.doxyme 2009-12-12 10:44:54 +0000
+++ hipd/blind.c.doxyme 2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,29 +45,29 @@
 
 
 /**
- * hip_blind_build_i1 
+ * hip_blind_build_i1
  *
  *
  * @param entry
  * @param mask
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_build_r2 
+ * hip_blind_build_r2
  *
  *
  * @param i2
  * @param r2
  * @param entry
  * @param mask
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_create_r1 
+ * hip_blind_create_r1
  *
  *
  * @param src_hit
@@ -75,40 +76,40 @@
  * @param host_id_priv
  * @param host_id_pub
  * @param cookie_k)
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_fingerprints 
+ * hip_blind_fingerprints
  *
  *
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_get_nonce 
+ * hip_blind_get_nonce
  *
  *
  * @param msg
  * @param msg_nonce
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_get_status 
+ * hip_blind_get_status
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_precreate_r1 
+ * hip_blind_precreate_r1
  *
  *
  * @param r1table
@@ -117,98 +118,97 @@
  * @param m)
  * @param privkey
  * @param pubkey)
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_verify 
+ * hip_blind_verify
  *
  *
  * @param nonce
  * @param plain_hit
  * @param blind_hit
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_verify_r2 
+ * hip_blind_verify_r2
  *
  *
  * @param r2
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_check_whether_to_use_blind 
+ * hip_check_whether_to_use_blind
  *
  *
  * @param msg
  * @param entry
  * @param use_blind
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_do_blind 
+ * hip_do_blind
  *
  *
  * @param key
  * @param key_len
  * @param blind_hit
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_plain_fingerprint 
+ * hip_plain_fingerprint
  *
  *
  * @param nonce
  * @param blind_hit
  * @param plain_hit
- * @return 
- **/
-
-
-/**
- * hip_set_blind_off 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_set_blind_off_sa 
- *
- *
- * @param entry
- * @param not_used
- * @return 
- **/
-
-
-/**
- * hip_set_blind_on 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_set_blind_on_sa 
- *
- *
- * @param entry
- * @param not_used
- * @return 
- **/
-
+ * @return
+ **/
+
+
+/**
+ * hip_set_blind_off
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_set_blind_off_sa
+ *
+ *
+ * @param entry
+ * @param not_used
+ * @return
+ **/
+
+
+/**
+ * hip_set_blind_on
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_set_blind_on_sa
+ *
+ *
+ * @param entry
+ * @param not_used
+ * @return
+ **/

=== modified file 'hipd/blind.h'
--- hipd/blind.h        2010-01-19 11:26:32 +0000
+++ hipd/blind.h        2010-02-10 22:32:46 +0000
@@ -1,5 +1,5 @@
 #ifndef BLIND_H
-#define BLIND_H 
+#define BLIND_H
 
 #include "lib/core/debug.h"
 #include "lib/tool/crypto.h"
@@ -10,36 +10,36 @@
 extern int hip_blind_status; //blind on/off flag
 
 int hip_check_whether_to_use_blind(hip_common_t *msg, hip_ha_t *entry,
-                                  int *use_blind);
+                                   int *use_blind);
 int hip_set_blind_on(void);
 int hip_set_blind_off(void);
 int hip_blind_get_status(void);
-int hip_blind_get_nonce(struct hip_common *msg, 
-                       uint16_t *msg_nonce);
-int hip_plain_fingerprint(uint16_t *nonce, 
-                         struct in6_addr *blind_hit, 
-                         struct in6_addr *plain_hit);
+int hip_blind_get_nonce(struct hip_common *msg,
+                        uint16_t *msg_nonce);
+int hip_plain_fingerprint(uint16_t *nonce,
+                          struct in6_addr *blind_hit,
+                          struct in6_addr *plain_hit);
 int hip_do_blind(char *key, unsigned int key_len, struct in6_addr *blind_hit);
-int hip_blind_verify(uint16_t *nonce, 
-                    struct in6_addr *plain_hit, 
-                    struct in6_addr *blind_hit);
-int hip_blind_verify_r2(struct hip_common *r2, 
-                            hip_ha_t *entry);
- 
+int hip_blind_verify(uint16_t *nonce,
+                     struct in6_addr *plain_hit,
+                     struct in6_addr *blind_hit);
+int hip_blind_verify_r2(struct hip_common *r2,
+                        hip_ha_t *entry);
+
 struct hip_common *hip_blind_build_i1(hip_ha_t *entry, uint16_t *mask);
-int hip_blind_build_r2(struct hip_common *i2, 
-                        struct hip_common *r2,
-                        hip_ha_t *entry, 
-                        uint16_t *mask);
+int hip_blind_build_r2(struct hip_common *i2,
+                       struct hip_common *r2,
+                       hip_ha_t *entry,
+                       uint16_t *mask);
 
 struct hip_common *hip_blind_create_r1(const struct in6_addr *src_hit,
-                                      int (*sign)(void *key, struct hip_common 
*m),
-                                      void *private_key,
-                                      const struct hip_host_id *host_id_pub,
-                                      int cookie_k);
+                                       int (*sign)(void *key, struct 
hip_common *m),
+                                       void *private_key,
+                                       const struct hip_host_id *host_id_pub,
+                                       int cookie_k);
 
-int hip_blind_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit, 
-                          int (*sign)(void *key, struct hip_common *m),
-                          void *privkey, struct hip_host_id *pubkey);
+int hip_blind_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit,
+                           int (*sign)(void *key, struct hip_common *m),
+                           void *privkey, struct hip_host_id *pubkey);
 
 #endif

=== modified file 'hipd/blind.h.doxyme'
--- hipd/blind.h.doxyme 2009-12-12 10:44:54 +0000
+++ hipd/blind.h.doxyme 2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/blind.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/bos.c'
--- hipd/bos.c  2010-02-08 15:18:06 +0000
+++ hipd/bos.c  2010-02-10 22:32:46 +0000
@@ -14,23 +14,22 @@
  */
 static int hip_create_bos_signature(void *priv, int algo, struct hip_common 
*bos)
 {
-       int err = 0;
-       
-       if (algo == HIP_HI_DSA) {
-               HIP_DEBUG("Creating DSA signature\n");
-               err = hip_dsa_sign(priv, bos);
-       } else if (algo == HIP_HI_RSA) {
-               HIP_DEBUG("Creating RSA signature\n");
-               err = hip_rsa_sign(priv, bos);
-       } else {
-               HIP_ERROR("Unsupported algorithm:%d\n", algo);
-               err = -1;
-       }
-
-       return err;
+    int err = 0;
+
+    if (algo == HIP_HI_DSA) {
+        HIP_DEBUG("Creating DSA signature\n");
+        err = hip_dsa_sign(priv, bos);
+    } else if (algo == HIP_HI_RSA) {
+        HIP_DEBUG("Creating RSA signature\n");
+        err = hip_rsa_sign(priv, bos);
+    } else {
+        HIP_ERROR("Unsupported algorithm:%d\n", algo);
+        err = -1;
+    }
+
+    return err;
 }
 
-
 /** hip_socket_send_bos - send a BOS packet
  * @param msg input message (should be empty)
  *
@@ -41,142 +40,151 @@
  */
 int hip_send_bos(const struct hip_common *msg)
 {
-       struct hip_common *bos = NULL;
-       struct in6_addr hit_our;
-       struct in6_addr daddr;
-       struct hip_host_id  *host_id_pub = NULL;
-       struct netdev_address *n;
-       hip_list_t *item, *tmp;
-       void *private_key;
-       int err = 0, i;
-       
-       HIP_DEBUG("\n");
-       
-       /* Extra consistency test */
-       if (hip_get_msg_type(msg) != SO_HIP_BOS)
-       {
-               err = -EINVAL;
-               HIP_ERROR("Bad message type\n");
-               goto out_err;
-       }
-       
-       /* allocate space for new BOS */
-       bos = hip_msg_alloc();
-       if (!bos)
-       {
-               HIP_ERROR("Allocation of BOS failed\n");
-               err = -ENOMEM;
-               goto out_err;
-       }
-
-       /* Determine our HIT */
-       if (hip_get_any_localhost_hit(&hit_our, HIP_HI_DEFAULT_ALGO, 0))
-       {
-               HIP_ERROR("Our HIT not found\n");
-               err = -EINVAL;
-               goto out_err;
-       }
-       HIP_DEBUG_IN6ADDR("hit_our = ", &hit_our);
-
-       /* Get our public host ID and private key */
-       err = hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID, &hit_our,
-                               HIP_HI_DEFAULT_ALGO, &host_id_pub, 
&private_key);
-       if (err) {
-               HIP_ERROR("No local host ID found\n");
-               goto out_err;
-       }
-
-       /* Ready to begin building the BOS packet */
-       /*! \todo TH: hip_build_network_hdr has to be replaced with an 
appropriate function pointer */
-       hip_build_network_hdr(bos, HIP_BOS, HIP_HA_CTRL_NONE, &hit_our, NULL);
-
-       /********** HOST_ID *********/
-       _HIP_DEBUG("This HOST ID belongs to: %s\n",
-                  hip_get_param_host_id_hostname(host_id_pub));
-       err = hip_build_param(bos, host_id_pub);
-       if (err)
-       {
-               HIP_ERROR("Building of host id failed\n");
-               goto out_err;
-       }
-
-       /********** SIGNATURE **********/
-       HIP_ASSERT(private_key);
-       /* HIP_HI_DEFAULT_ALGO corresponds to HIP_HI_DSA therefore the
-          signature will be dsa */
-       /* Build a digest of the packet built so far. Signature will
-          be calculated over the digest. */
-
-       if (hip_create_bos_signature(private_key, HIP_HI_DEFAULT_ALGO, bos))
-       {
-               HIP_ERROR("Could not create signature\n");
-               err = -EINVAL;
-               goto out_err;
-       }
-
-       /************** BOS packet ready ***************/
-
-       /**************SENDING ON IPv6*****************/
-       /* Use All Nodes Addresses (link-local) from RFC2373 */
-       daddr.s6_addr32[0] = htonl(0xFF020000);
-       daddr.s6_addr32[1] = 0;
-       daddr.s6_addr32[2] = 0;
-       daddr.s6_addr32[3] = htonl(0x1);
-       HIP_HEXDUMP("dst addr:", &daddr, 16);
-
-       list_for_each_safe(item, tmp, addresses, i)
-       {
-               n = (struct netdev_address *)list_entry(item);
-               HIP_HEXDUMP("BOS src address:", hip_cast_sa_addr((struct 
sockaddr *)&n->addr), hip_sa_addr_len(&n->addr));
-               /* Packet is send on raw HIP no matter what is the global NAT
-                  status, because NAT travelsal is not supported for IPv6. */
-               err = hip_send_pkt(hip_cast_sa_addr((struct sockaddr 
*)&n->addr),
-                                                       &daddr, 0 ,0, bos, 
NULL, 0);
-               if (err)
-                       HIP_ERROR("sending of BOS failed, err=%d\n", err);
-       }
-       err = 0;
-
-       /** @todo: Miika, please test this. I doubt there are some extra packets
-           sent. --Abi */
-
-       /**************SENDING ON IPv4*****************/
-       /* Use All Nodes Addresses (link-local) from RFC2373 */
-       daddr.s6_addr32[0] = 0;
-       daddr.s6_addr32[1] = 0;
-       daddr.s6_addr32[2] = htonl(0xffff);
-       daddr.s6_addr32[3] = htonl(0xffffffff);
-       HIP_HEXDUMP("dst addr:", &daddr, 16);
-
-       list_for_each_safe(item, tmp, addresses, i)
-       {
-               n = (struct netdev_address *)list_entry(item);
-               HIP_HEXDUMP("BOS src address:", hip_cast_sa_addr((struct 
sockaddr *)&n->addr),
-                                                                       
hip_sa_addr_len(&n->addr));
-               /* If global NAT status is "on", the packet is send on UDP. */
-               if(hip_nat_status) {
-                       err = hip_send_pkt(hip_cast_sa_addr((struct sockaddr 
*)&n->addr), &daddr,
-                                          hip_get_local_nat_udp_port(), 
hip_get_peer_nat_udp_port(),
-                                          bos, NULL, 0);
-               }
-               else err = hip_send_pkt(hip_cast_sa_addr((struct sockaddr 
*)&n->addr),
-                                                       &daddr,0,0, bos, NULL, 
0);
-               if (err) HIP_ERROR("sending of BOS failed, err=%d\n", err);
-       }
-       err = 0;
-
-
+    struct hip_common *bos          = NULL;
+    struct in6_addr hit_our;
+    struct in6_addr daddr;
+    struct hip_host_id *host_id_pub = NULL;
+    struct netdev_address *n;
+    hip_list_t *item, *tmp;
+    void *private_key;
+    int err                         = 0, i;
+
+    HIP_DEBUG("\n");
+
+    /* Extra consistency test */
+    if (hip_get_msg_type(msg) != SO_HIP_BOS) {
+        err = -EINVAL;
+        HIP_ERROR("Bad message type\n");
+        goto out_err;
+    }
+
+    /* allocate space for new BOS */
+    bos = hip_msg_alloc();
+    if (!bos) {
+        HIP_ERROR("Allocation of BOS failed\n");
+        err = -ENOMEM;
+        goto out_err;
+    }
+
+    /* Determine our HIT */
+    if (hip_get_any_localhost_hit(&hit_our, HIP_HI_DEFAULT_ALGO, 0)) {
+        HIP_ERROR("Our HIT not found\n");
+        err = -EINVAL;
+        goto out_err;
+    }
+    HIP_DEBUG_IN6ADDR("hit_our = ", &hit_our);
+
+    /* Get our public host ID and private key */
+    err = hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID, &hit_our,
+                                       HIP_HI_DEFAULT_ALGO, &host_id_pub, 
&private_key);
+    if (err) {
+        HIP_ERROR("No local host ID found\n");
+        goto out_err;
+    }
+
+    /* Ready to begin building the BOS packet */
+    /*! \todo TH: hip_build_network_hdr has to be replaced with an appropriate 
function pointer */
+    hip_build_network_hdr(bos, HIP_BOS, HIP_HA_CTRL_NONE, &hit_our, NULL);
+
+    /********** HOST_ID *********/
+    _HIP_DEBUG("This HOST ID belongs to: %s\n",
+               hip_get_param_host_id_hostname(host_id_pub));
+    err = hip_build_param(bos, host_id_pub);
+    if (err) {
+        HIP_ERROR("Building of host id failed\n");
+        goto out_err;
+    }
+
+    /********** SIGNATURE **********/
+    HIP_ASSERT(private_key);
+    /* HIP_HI_DEFAULT_ALGO corresponds to HIP_HI_DSA therefore the
+     * signature will be dsa */
+    /* Build a digest of the packet built so far. Signature will
+     * be calculated over the digest. */
+
+    if (hip_create_bos_signature(private_key, HIP_HI_DEFAULT_ALGO, bos)) {
+        HIP_ERROR("Could not create signature\n");
+        err = -EINVAL;
+        goto out_err;
+    }
+
+    /************** BOS packet ready ***************/
+
+    /**************SENDING ON IPv6*****************/
+    /* Use All Nodes Addresses (link-local) from RFC2373 */
+    daddr.s6_addr32[0] = htonl(0xFF020000);
+    daddr.s6_addr32[1] = 0;
+    daddr.s6_addr32[2] = 0;
+    daddr.s6_addr32[3] = htonl(0x1);
+    HIP_HEXDUMP("dst addr:", &daddr, 16);
+
+    list_for_each_safe(item, tmp, addresses, i)
+    {
+        n   = (struct netdev_address *) list_entry(item);
+        HIP_HEXDUMP("BOS src address:",
+                    hip_cast_sa_addr((struct sockaddr *) &n->addr),
+                    hip_sa_addr_len(&n->addr));
+        /* Packet is send on raw HIP no matter what is the global NAT
+         * status, because NAT travelsal is not supported for IPv6. */
+        err = hip_send_pkt(hip_cast_sa_addr((struct sockaddr *) &n->addr),
+                           &daddr, 0, 0, bos, NULL, 0);
+        if (err) {
+            HIP_ERROR("sending of BOS failed, err=%d\n", err);
+        }
+    }
+    err = 0;
+
+    /** @todo: Miika, please test this. I doubt there are some extra packets
+     *  sent. --Abi */
+
+    /**************SENDING ON IPv4*****************/
+    /* Use All Nodes Addresses (link-local) from RFC2373 */
+    daddr.s6_addr32[0] = 0;
+    daddr.s6_addr32[1] = 0;
+    daddr.s6_addr32[2] = htonl(0xffff);
+    daddr.s6_addr32[3] = htonl(0xffffffff);
+    HIP_HEXDUMP("dst addr:", &daddr, 16);
+
+    list_for_each_safe(item, tmp, addresses, i)
+    {
+        n = (struct netdev_address *) list_entry(item);
+        HIP_HEXDUMP("BOS src address:",
+                    hip_cast_sa_addr((struct sockaddr *) &n->addr),
+                    hip_sa_addr_len(&n->addr));
+        /* If global NAT status is "on", the packet is send on UDP. */
+        if (hip_nat_status) {
+            err = hip_send_pkt(hip_cast_sa_addr((struct sockaddr *) &n->addr),
+                               &daddr,
+                               hip_get_local_nat_udp_port(),
+                               hip_get_peer_nat_udp_port(),
+                               bos,
+                               NULL,
+                               0);
+        } else {
+            err = hip_send_pkt(hip_cast_sa_addr((struct sockaddr *) &n->addr),
+                               &daddr,
+                               0,
+                               0,
+                               bos,
+                               NULL,
+                               0);
+        }
+        if (err) {
+            HIP_ERROR("sending of BOS failed, err=%d\n", err);
+        }
+    }
+    err = 0;
 
 out_err:
-       if (host_id_pub)
-               HIP_FREE(host_id_pub);
-       if (bos)
-               HIP_FREE(bos);
-       return err;
+    if (host_id_pub) {
+        HIP_FREE(host_id_pub);
+    }
+    if (bos) {
+        HIP_FREE(bos);
+    }
+    return err;
 }
 
-
-
 /**
  * hip_handle_bos - handle incoming BOS packet
  * @param skb sk_buff where the HIP packet is in
@@ -189,78 +197,77 @@
  */
 
 int hip_handle_bos(struct hip_common *bos,
-                  struct in6_addr *bos_saddr,
-                  struct in6_addr *bos_daddr,
-                  hip_ha_t *entry,
-                  hip_portpair_t *stateless_info)
+                   struct in6_addr *bos_saddr,
+                   struct in6_addr *bos_daddr,
+                   hip_ha_t *entry,
+                   hip_portpair_t *stateless_info)
 {
-       int err = 0, len;
-       struct hip_host_id *peer_host_id;
-       hip_lsi_t lsi;
-       //struct hip_lhi peer_lhi;
-       struct in6_addr peer_hit;
-       char *str;
-       struct in6_addr *dstip;
-       char src[INET6_ADDRSTRLEN];
-
-       /* according to the section 8.6 of the base draft,
-        * we must first check signature
-        */
-       HIP_IFEL(!(peer_host_id = hip_get_param(bos, HIP_PARAM_HOST_ID)), 
-ENOENT,
-                "No HOST_ID found in BOS\n");
-
-       HIP_IFEL(hip_verify_packet_signature(bos, peer_host_id), -EINVAL,
-                "Verification of BOS signature failed\n");
-
-
-       /* Validate HIT against received host id */     
-       hip_host_id_to_hit(peer_host_id, &peer_hit, HIP_HIT_TYPE_HASH100);
-       HIP_IFEL(ipv6_addr_cmp(&peer_hit, &bos->hits) != 0, -EINVAL,
-                "Sender HIT does not match the advertised host_id\n");
-       
-       HIP_HEXDUMP("Advertised HIT:", &bos->hits, 16);
-       
-       /* Everything ok, first save host id to db */
-       HIP_IFE(hip_get_param_host_id_di_type_len(peer_host_id, &str, &len) < 
0, -1);
-       HIP_DEBUG("Identity type: %s, Length: %d, Name: %s\n",
-                 str, len, hip_get_param_host_id_hostname(peer_host_id));
-
-       /* Now save the peer IP address */
-       dstip = bos_saddr;
-       hip_in6_ntop(dstip, src);
-       HIP_DEBUG("BOS sender IP: saddr %s\n", src);
-
-       if (entry) {
-               struct in6_addr daddr;
-
-               HIP_DEBUG("I guess we should not even get here ...\n");
-               HIP_DEBUG("I think so!\n");
-
-               /* The entry may contain the wrong address mapping... */
-               HIP_DEBUG("Updating existing entry\n");
-               hip_hadb_get_peer_addr(entry, &daddr);
-               if (ipv6_addr_cmp(&daddr, dstip) != 0) {
-                       HIP_DEBUG("Mapped address doesn't match received 
address\n");
-                       HIP_DEBUG("Assuming that the mapped address was 
actually RVS's.\n");
-                       HIP_HEXDUMP("Mapping", &daddr, 16);
-                       HIP_HEXDUMP("Received", dstip, 16);
-                       hip_hadb_delete_peer_addrlist_one_old(entry, &daddr);
-                       HIP_ERROR("assuming we are doing base exchange\n");
-                       hip_hadb_add_peer_addr(entry, dstip, 0, 0, 0,
-                                              hip_get_peer_nat_udp_port());
-               }
-       } else {
-               // FIXME: just add it here and not via workorder.
-
-               /* we have no previous infomation on the peer, create
-                * a new HIP HA */
-               HIP_IFEL((hip_hadb_add_peer_info(&bos->hits, dstip, &lsi, 
NULL)<0), -1,
-                        "Failed to insert new peer info");
-               HIP_DEBUG("HA entry created.\n");
-
-       }
-
- out_err:
-       return err;
+    int err = 0, len;
+    struct hip_host_id *peer_host_id;
+    hip_lsi_t lsi;
+    //struct hip_lhi peer_lhi;
+    struct in6_addr peer_hit;
+    char *str;
+    struct in6_addr *dstip;
+    char src[INET6_ADDRSTRLEN];
+
+    /* according to the section 8.6 of the base draft,
+     * we must first check signature
+     */
+    HIP_IFEL(!(peer_host_id = hip_get_param(bos, HIP_PARAM_HOST_ID)), -ENOENT,
+             "No HOST_ID found in BOS\n");
+
+    HIP_IFEL(hip_verify_packet_signature(bos, peer_host_id), -EINVAL,
+             "Verification of BOS signature failed\n");
+
+
+    /* Validate HIT against received host id */
+    hip_host_id_to_hit(peer_host_id, &peer_hit, HIP_HIT_TYPE_HASH100);
+    HIP_IFEL(ipv6_addr_cmp(&peer_hit, &bos->hits) != 0, -EINVAL,
+             "Sender HIT does not match the advertised host_id\n");
+
+    HIP_HEXDUMP("Advertised HIT:", &bos->hits, 16);
+
+    /* Everything ok, first save host id to db */
+    HIP_IFE(hip_get_param_host_id_di_type_len(peer_host_id, &str, &len) < 0, 
-1);
+    HIP_DEBUG("Identity type: %s, Length: %d, Name: %s\n",
+              str, len, hip_get_param_host_id_hostname(peer_host_id));
+
+    /* Now save the peer IP address */
+    dstip = bos_saddr;
+    hip_in6_ntop(dstip, src);
+    HIP_DEBUG("BOS sender IP: saddr %s\n", src);
+
+    if (entry) {
+        struct in6_addr daddr;
+
+        HIP_DEBUG("I guess we should not even get here ...\n");
+        HIP_DEBUG("I think so!\n");
+
+        /* The entry may contain the wrong address mapping... */
+        HIP_DEBUG("Updating existing entry\n");
+        hip_hadb_get_peer_addr(entry, &daddr);
+        if (ipv6_addr_cmp(&daddr, dstip) != 0) {
+            HIP_DEBUG("Mapped address doesn't match received address\n");
+            HIP_DEBUG("Assuming that the mapped address was actually 
RVS's.\n");
+            HIP_HEXDUMP("Mapping", &daddr, 16);
+            HIP_HEXDUMP("Received", dstip, 16);
+            hip_hadb_delete_peer_addrlist_one_old(entry, &daddr);
+            HIP_ERROR("assuming we are doing base exchange\n");
+            hip_hadb_add_peer_addr(entry, dstip, 0, 0, 0,
+                                   hip_get_peer_nat_udp_port());
+        }
+    } else {
+        // FIXME: just add it here and not via workorder.
+
+        /* we have no previous infomation on the peer, create
+         * a new HIP HA */
+        HIP_IFEL((hip_hadb_add_peer_info(&bos->hits, dstip, &lsi, NULL) < 0),
+                 -1,
+                 "Failed to insert new peer info");
+        HIP_DEBUG("HA entry created.\n");
+    }
+
+out_err:
+    return err;
 }
-

=== modified file 'hipd/bos.c.doxyme'
--- hipd/bos.c.doxyme   2009-12-12 10:44:54 +0000
+++ hipd/bos.c.doxyme   2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,18 +45,18 @@
 
 
 /**
- * hip_create_bos_signature 
+ * hip_create_bos_signature
  *
  *
  * @param priv
  * @param algo
  * @param bos
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_handle_bos 
+ * hip_handle_bos
  *
  *
  * @param bos
@@ -63,15 +64,14 @@
  * @param bos_daddr
  * @param entry
  * @param stateless_info
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_send_bos 
+ * hip_send_bos
  *
  *
  * @param msg
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/bos.h'
--- hipd/bos.h  2010-01-19 11:26:32 +0000
+++ hipd/bos.h  2010-02-10 22:32:46 +0000
@@ -13,12 +13,13 @@
 #include "lib/core/state.h"
 
 int hip_send_bos(const struct hip_common *msg);
-int hip_verify_packet_signature(struct hip_common *bos, struct hip_host_id 
*peer_host_id);
+int hip_verify_packet_signature(struct hip_common *bos,
+                                struct hip_host_id *peer_host_id);
 
 int hip_handle_bos(struct hip_common *bos,
-                  struct in6_addr *bos_saddr,
-                  struct in6_addr *bos_daddr,
-                  hip_ha_t *entry, hip_portpair_t *);
+                   struct in6_addr *bos_saddr,
+                   struct in6_addr *bos_daddr,
+                   hip_ha_t *entry, hip_portpair_t *);
 
 
 #endif /* HIP_BOS_NEW_H */

=== modified file 'hipd/bos.h.doxyme'
--- hipd/bos.h.doxyme   2009-12-12 10:44:54 +0000
+++ hipd/bos.h.doxyme   2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/bos.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/cert.c'
--- hipd/cert.c 2010-02-08 15:18:06 +0000
+++ hipd/cert.c 2010-02-10 22:32:46 +0000
@@ -16,279 +16,313 @@
  * creates the public key sequence and the signature sequence
  *
  * @param msg points to the msg gotten from "client" that should
- *            contain HIP_PARAM_CERT_SPKI_INFO 
+ *            contain HIP_PARAM_CERT_SPKI_INFO
  * @param db is the HIP_HASHTABLE to
  *           query for the hostid entry
  *
  * @return 0 if signature was created without errors, negative value
  *         is returned on errors
  */
-int hip_cert_spki_sign(struct hip_common * msg, HIP_HASHTABLE * db) {
-        int err = 0, sig_len = 0, algo = 0, t = 0;
-        struct hip_cert_spki_info * p_cert;
-        struct hip_cert_spki_info * cert;
-       struct hip_host_id * host_id = NULL;
-        unsigned char sha_digest[21];
-        unsigned char * signature_b64 = NULL;
-        unsigned char * digest_b64 = NULL;
-        unsigned char *sha_retval;
-        u8 * signature = NULL;
-        DSA_SIG * dsa_sig = NULL;
-        
-        /* RSA needed variables */
-        RSA *rsa = NULL;
-        unsigned char * e_bin = NULL, * n_bin = NULL;
-        unsigned char * e_hex = NULL, * n_b64 = NULL;
-        /* DSA needed variables */
-        DSA * dsa = NULL;
-        unsigned char * p_bin = NULL, * q_bin = NULL, * g_bin = NULL, * y_bin 
= NULL;
-        unsigned char * p_b64 = NULL, * q_b64 = NULL, * g_b64 = NULL, * y_b64 
= NULL;
-                           
-        cert = malloc(sizeof(struct hip_cert_spki_info));
-        HIP_IFEL((!cert), -1, "Malloc for cert failed\n");
-        memset(cert, 0, sizeof(struct hip_cert_spki_info));
-
-        HIP_IFEL(!(p_cert = hip_get_param(msg,HIP_PARAM_CERT_SPKI_INFO)), 
-                 -1, "No cert_info struct found\n");
-        memcpy(cert, p_cert, sizeof(struct hip_cert_spki_info));
-
-       _HIP_DEBUG("\n\n** CONTENTS of public key sequence **\n"
-                   "%s\n\n",cert->public_key);
-        _HIP_DEBUG("\n\n** CONTENTS of cert sequence to be signed **\n"
-                   "%s\n\n", cert->cert);
-       _HIP_DEBUG("\n\n** CONTENTS of public key sequence **\n"
-                   "%s\n\n",cert->signature);
-        HIP_DEBUG_HIT("Getting keys for HIT",&cert->issuer_hit);
-
-       HIP_IFEL(hip_get_host_id_and_priv_key(hip_local_hostid_db, 
&cert->issuer_hit,
-                                             HIP_ANY_ALGO, &host_id, (void 
**)&rsa), 
-                -1, "Private key not found\n");
-       algo = host_id->rdata.algorithm;
-       if (algo == HIP_HI_DSA)
-               dsa = (DSA *)rsa;
-
-        memset(sha_digest, '\0', sizeof(sha_digest));
-
-        digest_b64 = malloc(30); 
-        HIP_IFEL((!digest_b64), -1, "Malloc for digest_b64 failed\n");
-        memset(digest_b64, 0, 30);
-
-        /* build sha1 digest that will be signed */
-        HIP_IFEL(!(sha_retval = SHA1((unsigned char *)cert->cert, strlen((char 
*)cert->cert), sha_digest)),
-                 -1, "SHA1 error when creating digest.\n");        
-        _HIP_HEXDUMP("SHA1 digest of cert sequence ", sha_digest, 
sizeof(sha_digest));
-
-        if (algo == HIP_HI_RSA) {
-
-                signature_b64 = malloc(RSA_size(rsa)); 
-                HIP_IFEL((!signature_b64), -1, "Malloc for signature_b64 
failed\n");
-                memset(signature_b64, 0, RSA_size(rsa));
-                
-                n_bin = malloc(RSA_size(rsa) + 1);
-                HIP_IFEL((!n_bin), -1, "Malloc for n_bin failed\n");
-
-                n_b64 = malloc(RSA_size(rsa) + 20);
-                HIP_IFEL((!n_b64), -1, "Malloc for n_b64 failed\n");
-                memset(n_b64, 0, (RSA_size(rsa) + 20));
-                 
-                e_bin = malloc(BN_num_bytes(rsa->e) + 1);
-                HIP_IFEL((!e_bin), -1, "Malloc for e_bin failed\n");
-                memset(e_bin, 0, (BN_num_bytes(rsa->e) + 1));
-                                
-                /* RSA sign the digest */ 
-                sig_len = RSA_size(rsa);
-                signature = malloc(sig_len);
-                HIP_IFEL((!signature), -1, "Malloc for signature failed\n");
-                memset(signature, 0, sig_len);
-
-                err = RSA_sign(NID_sha1, sha_digest, SHA_DIGEST_LENGTH, 
signature,
-                               (unsigned int *)&sig_len, rsa);
-                HIP_IFEL((err = err == 0 ? -1 : 0), -1, "RSA_sign error\n");
-
-                _HIP_HEXDUMP("Signature created for the certificate ", 
signature, sig_len);
-                _HIP_DEBUG("Siglen %d, err :%d\n", sig_len, err);
-
-        } else if (algo == HIP_HI_DSA) {
-
-                p_bin = malloc(BN_num_bytes(dsa->p) + 1);
-                HIP_IFEL((!p_bin), -1, "Malloc for p_bin failed\n");
-
-                q_bin = malloc(BN_num_bytes(dsa->q) + 1);
-                HIP_IFEL((!q_bin), -1, "Malloc for q_bin failed\n");
-                
-                g_bin = malloc(BN_num_bytes(dsa->g) + 1);
-                HIP_IFEL((!g_bin), -1, "Malloc for g_bin failed\n");
-                
-                y_bin = malloc(BN_num_bytes(dsa->pub_key) + 1);
-                HIP_IFEL((!y_bin), -1, "Malloc for y_bin failed\n");
-                
-                p_b64 = malloc(BN_num_bytes(dsa->p) + 20);
-                HIP_IFEL((!p_b64), -1, "Malloc for p_b64 failed\n");
-
-                q_b64 = malloc(BN_num_bytes(dsa->q) + 20);
-                HIP_IFEL((!q_b64), -1, "Malloc for q_b64 failed\n");
-
-                g_b64 = malloc(BN_num_bytes(dsa->g) + 20);
-                HIP_IFEL((!g_b64), -1, "Malloc for g_b64 failed\n");
-
-                y_b64 = malloc(BN_num_bytes(dsa->pub_key) + 20);
-                HIP_IFEL((!y_b64), -1, "Malloc for y_b64 failed\n");
-
-                signature = malloc(HIP_DSA_SIG_SIZE);
-                memset(signature, 0, HIP_DSA_SIG_SIZE);
-
-                t = BN_num_bytes(dsa->p);
-                t = (t - 64) / 8;
-                HIP_IFEL(t > 8, 1, "Illegal DSA key\n");
-
-                signature[0] = t;
-                dsa_sig = DSA_do_sign(sha_digest, SHA_DIGEST_LENGTH, dsa);
-                bn2bin_safe(dsa_sig->r, &signature[1], DSA_PRIV);
-                bn2bin_safe(dsa_sig->s, &signature[1 + DSA_PRIV], DSA_PRIV);
-                sig_len = SHA_DIGEST_LENGTH + DSA_PRIV * 2;
-
-        } else HIP_IFEL(1 == 0, -1, "Unknown algorithm for signing\n");
-
-        /* clearing signature field just to be sure */
-        memset(cert->signature, '\0', sizeof(cert->signature));
-
-        HIP_IFEL(!(digest_b64 = (unsigned char *)base64_encode((unsigned char 
*)sha_digest, 
-                                                              (unsigned 
int)sizeof(sha_digest))), 
-                -1, "Failed to encode digest_b64\n");
-        HIP_IFEL(!(signature_b64 = (unsigned char *)base64_encode((unsigned 
char *)signature, 
-                                                                 (unsigned 
int)sig_len)),
-                -1, "Failed to encode signature_b64\n");
-        /* create (signature (hash sha1 |digest|)|signature|) */
-        sprintf(cert->signature, "(signature (hash sha1 |%s|)|%s|)", 
-                (char *)digest_b64, (char *)signature_b64);
-
-        _HIP_DEBUG("Sig sequence \n%s\n",cert->signature);
-
-        /* Create the public key sequence */
-        if (algo == HIP_HI_RSA) {
-
-                /* 
-                   RSA public-key
-                   draft-paajarvi-xml-spki-cert-00 section 3.1.1 
-
-                   <!ELEMENT rsa-pubkey (rsa-e,rsa-n)>
-                   <!ELEMENT rsa-e (#PCDATA)>
-                   <!ELEMENT rsa-n (#PCDATA)>
-                */
-                HIP_IFEL(!(BN_bn2bin(rsa->n, n_bin)), -1,
-                         "Error in converting public exponent from BN to 
bin\n");
-                
-                HIP_IFEL(!(n_b64 = (unsigned char *)base64_encode((unsigned 
char *)n_bin, 
-                                                                 
RSA_size(rsa))), -1,
-                        "Failed to encode n_b64\n");
-                 
-                HIP_IFEL(!(BN_bn2bin(rsa->e, e_bin)), -1,
-                         "Error in converting public exponent from BN to 
bin\n");
-                e_hex = (unsigned char *)BN_bn2hex(rsa->e);
-                
-                sprintf(cert->public_key, "(public_key (rsa-pkcs1-sha1 (e 
#%s#)(n |%s|)))", 
-                        e_hex, n_b64);
-
-        } else if (algo == HIP_HI_DSA) {
-
-                /* 
-                   DSA public-key
-                   draft-paajarvi-xml-spki-cert-00 section 3.1.2 
-
-                   <!ELEMENT dsa-pubkey (dsa-p,dsa-q,dsa-g,dsa-y)>
-                   <!ELEMENT dsa-p (#PCDATA)>
-                   <!ELEMENT dsa-q (#PCDATA)>
-                   <!ELEMENT dsa-g (#PCDATA)>
-                   <!ELEMENT dsa-y (#PCDATA)>
-                */
-                HIP_IFEL(!(BN_bn2bin(dsa->p, p_bin)), -1,
-                         "Error in converting public exponent from BN to 
bin\n");
-                HIP_IFEL(!(p_b64 = (unsigned char *)base64_encode((unsigned 
char *)p_bin, 
-                                                                 
BN_num_bytes(dsa->p))),
-                        -1, "Failed to encode p_b64\n"); 
-                
-                HIP_IFEL(!(BN_bn2bin(dsa->q, q_bin)), -1,
-                         "Error in converting public exponent from BN to 
bin\n");
-                HIP_IFEL(!(q_b64 = (unsigned char *)base64_encode((unsigned 
char *)q_bin, 
-                                                                 
BN_num_bytes(dsa->q))),
-                        -1, "Failed to encode q_64");
-
-                HIP_IFEL(!(BN_bn2bin(dsa->g, g_bin)), -1,
-                         "Error in converting public exponent from BN to 
bin\n");
-                HIP_IFEL(!(g_b64 = (unsigned char *)base64_encode((unsigned 
char *)g_bin, 
-                                                                 
BN_num_bytes(dsa->g))), 
-                        -1, "Failed to encode g_b64\n");
-                
-                HIP_IFEL(!(BN_bn2bin(dsa->pub_key, y_bin)), -1,
-                         "Error in converting public exponent from BN to 
bin\n");
-                HIP_IFEL(!(y_b64 = (unsigned char *)base64_encode((unsigned 
char *)y_bin, 
-                                                                 
BN_num_bytes(dsa->pub_key))),
-                        -1, "Failed to encode y_b64\n");
-                
-                sprintf(cert->public_key, "(public_key (dsa-pkcs1-sha1 (p 
|%s|)(q |%s|)"
-                                                                      "(g 
|%s|)(y |%s|)))", 
-                        p_b64, q_b64, g_b64, y_b64);
-
-        } else HIP_IFEL(1 == 0, -1, "Unknown algorithm for public-key 
element\n");
-
-        _HIP_DEBUG("\n\nPublic-key sequence:\n%s\n\n",cert->public_key);
-        _HIP_DEBUG("\n\nCert sequence:\n%s\n\n",cert->cert);
-        _HIP_DEBUG("\n\nSignature sequence:\n%s\n\n",cert->signature);
-
-        /* Put the results into the msg back */
-
-       _HIP_DEBUG("Len public-key (%d) + cert (%d) + signature (%d) = %d\n"
-                  "Sizeof hip_cert_spki_info %d\n",
-                 strlen(cert->public_key), strlen((char *)cert->cert), 
strlen((char *)cert->signature),
-                  (strlen(cert->public_key)+strlen((char 
*)cert->cert)+strlen((char *)cert->signature)),
-                  sizeof(struct hip_cert_spki_info));
-
-        hip_msg_init(msg);
-
-        HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_SPKI_SIGN, 0), -1, 
-                 "Failed to build user header\n");
-        HIP_IFEL(hip_build_param_cert_spki_info(msg, cert), -1,
-                 "Failed to build cert_info\n");                 
-
-        _HIP_DUMP_MSG(msg);
-
- out_err:
-
-       /* free malloced memory */
-       if (digest_b64) free(digest_b64);
-       if (signature_b64) free(signature_b64);
-       if (signature) free(signature);
-       if (host_id) free(host_id);
-
-        /* openssl structs */
+int hip_cert_spki_sign(struct hip_common *msg, HIP_HASHTABLE *db)
+{
+    int err                      = 0, sig_len = 0, algo = 0, t = 0;
+    struct hip_cert_spki_info *p_cert;
+    struct hip_cert_spki_info *cert;
+    struct hip_host_id *host_id  = NULL;
+    unsigned char sha_digest[21];
+    unsigned char *signature_b64 = NULL;
+    unsigned char *digest_b64    = NULL;
+    unsigned char *sha_retval;
+    u8 *signature                = NULL;
+    DSA_SIG *dsa_sig             = NULL;
+
+    /* RSA needed variables */
+    RSA *rsa                     = NULL;
+    unsigned char *e_bin         = NULL, *n_bin = NULL;
+    unsigned char *e_hex         = NULL, *n_b64 = NULL;
+    /* DSA needed variables */
+    DSA *dsa                     = NULL;
+    unsigned char *p_bin         = NULL, *q_bin = NULL, *g_bin = NULL, *y_bin 
= NULL;
+    unsigned char *p_b64         = NULL, *q_b64 = NULL, *g_b64 = NULL, *y_b64 
= NULL;
+
+    cert = malloc(sizeof(struct hip_cert_spki_info));
+    HIP_IFEL((!cert), -1, "Malloc for cert failed\n");
+    memset(cert, 0, sizeof(struct hip_cert_spki_info));
+
+    HIP_IFEL(!(p_cert = hip_get_param(msg, HIP_PARAM_CERT_SPKI_INFO)),
+             -1, "No cert_info struct found\n");
+    memcpy(cert, p_cert, sizeof(struct hip_cert_spki_info));
+
+    _HIP_DEBUG("\n\n** CONTENTS of public key sequence **\n"
+               "%s\n\n", cert->public_key);
+    _HIP_DEBUG("\n\n** CONTENTS of cert sequence to be signed **\n"
+               "%s\n\n", cert->cert);
+    _HIP_DEBUG("\n\n** CONTENTS of public key sequence **\n"
+               "%s\n\n", cert->signature);
+    HIP_DEBUG_HIT("Getting keys for HIT", &cert->issuer_hit);
+
+    HIP_IFEL(hip_get_host_id_and_priv_key(hip_local_hostid_db, 
&cert->issuer_hit,
+                                          HIP_ANY_ALGO, &host_id, (void **) 
&rsa),
+             -1, "Private key not found\n");
+    algo = host_id->rdata.algorithm;
+    if (algo == HIP_HI_DSA) {
+        dsa = (DSA *) rsa;
+    }
+
+    memset(sha_digest, '\0', sizeof(sha_digest));
+
+    digest_b64 = malloc(30);
+    HIP_IFEL((!digest_b64), -1, "Malloc for digest_b64 failed\n");
+    memset(digest_b64, 0, 30);
+
+    /* build sha1 digest that will be signed */
+    HIP_IFEL(!(sha_retval = SHA1((unsigned char *) cert->cert, strlen((char *) 
cert->cert), sha_digest)),
+             -1, "SHA1 error when creating digest.\n");
+    _HIP_HEXDUMP("SHA1 digest of cert sequence ", sha_digest, 
sizeof(sha_digest));
+
+    if (algo == HIP_HI_RSA) {
+        signature_b64 = malloc(RSA_size(rsa));
+        HIP_IFEL((!signature_b64), -1, "Malloc for signature_b64 failed\n");
+        memset(signature_b64, 0, RSA_size(rsa));
+
+        n_bin         = malloc(RSA_size(rsa) + 1);
+        HIP_IFEL((!n_bin), -1, "Malloc for n_bin failed\n");
+
+        n_b64         = malloc(RSA_size(rsa) + 20);
+        HIP_IFEL((!n_b64), -1, "Malloc for n_b64 failed\n");
+        memset(n_b64, 0, (RSA_size(rsa) + 20));
+
+        e_bin         = malloc(BN_num_bytes(rsa->e) + 1);
+        HIP_IFEL((!e_bin), -1, "Malloc for e_bin failed\n");
+        memset(e_bin, 0, (BN_num_bytes(rsa->e) + 1));
+
+        /* RSA sign the digest */
+        sig_len   = RSA_size(rsa);
+        signature = malloc(sig_len);
+        HIP_IFEL((!signature), -1, "Malloc for signature failed\n");
+        memset(signature, 0, sig_len);
+
+        err       = RSA_sign(NID_sha1, sha_digest, SHA_DIGEST_LENGTH, 
signature,
+                             (unsigned int *) &sig_len, rsa);
+        HIP_IFEL((err = err == 0 ? -1 : 0), -1, "RSA_sign error\n");
+
+        _HIP_HEXDUMP("Signature created for the certificate ", signature, 
sig_len);
+        _HIP_DEBUG("Siglen %d, err :%d\n", sig_len, err);
+    } else if (algo == HIP_HI_DSA) {
+        p_bin        = malloc(BN_num_bytes(dsa->p) + 1);
+        HIP_IFEL((!p_bin), -1, "Malloc for p_bin failed\n");
+
+        q_bin        = malloc(BN_num_bytes(dsa->q) + 1);
+        HIP_IFEL((!q_bin), -1, "Malloc for q_bin failed\n");
+
+        g_bin        = malloc(BN_num_bytes(dsa->g) + 1);
+        HIP_IFEL((!g_bin), -1, "Malloc for g_bin failed\n");
+
+        y_bin        = malloc(BN_num_bytes(dsa->pub_key) + 1);
+        HIP_IFEL((!y_bin), -1, "Malloc for y_bin failed\n");
+
+        p_b64        = malloc(BN_num_bytes(dsa->p) + 20);
+        HIP_IFEL((!p_b64), -1, "Malloc for p_b64 failed\n");
+
+        q_b64        = malloc(BN_num_bytes(dsa->q) + 20);
+        HIP_IFEL((!q_b64), -1, "Malloc for q_b64 failed\n");
+
+        g_b64        = malloc(BN_num_bytes(dsa->g) + 20);
+        HIP_IFEL((!g_b64), -1, "Malloc for g_b64 failed\n");
+
+        y_b64        = malloc(BN_num_bytes(dsa->pub_key) + 20);
+        HIP_IFEL((!y_b64), -1, "Malloc for y_b64 failed\n");
+
+        signature    = malloc(HIP_DSA_SIG_SIZE);
+        memset(signature, 0, HIP_DSA_SIG_SIZE);
+
+        t            = BN_num_bytes(dsa->p);
+        t            = (t - 64) / 8;
+        HIP_IFEL(t > 8, 1, "Illegal DSA key\n");
+
+        signature[0] = t;
+        dsa_sig      = DSA_do_sign(sha_digest, SHA_DIGEST_LENGTH, dsa);
+        bn2bin_safe(dsa_sig->r, &signature[1], DSA_PRIV);
+        bn2bin_safe(dsa_sig->s, &signature[1 + DSA_PRIV], DSA_PRIV);
+        sig_len      = SHA_DIGEST_LENGTH + DSA_PRIV * 2;
+    } else {
+        HIP_IFEL(1 == 0, -1, "Unknown algorithm for signing\n");
+    }
+
+    /* clearing signature field just to be sure */
+    memset(cert->signature, '\0', sizeof(cert->signature));
+
+    HIP_IFEL(!(digest_b64 = (unsigned char *) base64_encode((unsigned char *) 
sha_digest,
+                                                            (unsigned int) 
sizeof(sha_digest))),
+             -1, "Failed to encode digest_b64\n");
+    HIP_IFEL(!(signature_b64 = (unsigned char *) base64_encode((unsigned char 
*) signature,
+                                                               (unsigned int) 
sig_len)),
+             -1, "Failed to encode signature_b64\n");
+    /* create (signature (hash sha1 |digest|)|signature|) */
+    sprintf(cert->signature, "(signature (hash sha1 |%s|)|%s|)",
+            (char *) digest_b64, (char *) signature_b64);
+
+    _HIP_DEBUG("Sig sequence \n%s\n", cert->signature);
+
+    /* Create the public key sequence */
+    if (algo == HIP_HI_RSA) {
+        /*
+         * RSA public-key
+         * draft-paajarvi-xml-spki-cert-00 section 3.1.1
+         *
+         * <!ELEMENT rsa-pubkey (rsa-e,rsa-n)>
+         * <!ELEMENT rsa-e (#PCDATA)>
+         * <!ELEMENT rsa-n (#PCDATA)>
+         */
+        HIP_IFEL(!(BN_bn2bin(rsa->n, n_bin)),
+                 -1,
+                 "Error in converting public exponent from BN to bin\n");
+
+        HIP_IFEL(!(n_b64 = (unsigned char *) base64_encode((unsigned char *) 
n_bin, RSA_size(rsa))),
+                 -1,
+                 "Failed to encode n_b64\n");
+
+        HIP_IFEL(!(BN_bn2bin(rsa->e, e_bin)),
+                 -1,
+                 "Error in converting public exponent from BN to bin\n");
+
+        e_hex = (unsigned char *) BN_bn2hex(rsa->e);
+
+        sprintf(cert->public_key, "(public_key (rsa-pkcs1-sha1 (e #%s#)(n 
|%s|)))",
+                e_hex,
+                n_b64);
+    } else if (algo == HIP_HI_DSA) {
+        /*
+         * DSA public-key
+         * draft-paajarvi-xml-spki-cert-00 section 3.1.2
+         *
+         * <!ELEMENT dsa-pubkey (dsa-p,dsa-q,dsa-g,dsa-y)>
+         * <!ELEMENT dsa-p (#PCDATA)>
+         * <!ELEMENT dsa-q (#PCDATA)>
+         * <!ELEMENT dsa-g (#PCDATA)>
+         * <!ELEMENT dsa-y (#PCDATA)>
+         */
+        HIP_IFEL(!(BN_bn2bin(dsa->p, p_bin)), -1,
+                 "Error in converting public exponent from BN to bin\n");
+        HIP_IFEL(!(p_b64 = (unsigned char *) base64_encode((unsigned char *) 
p_bin,
+                                                           
BN_num_bytes(dsa->p))),
+                 -1, "Failed to encode p_b64\n");
+
+        HIP_IFEL(!(BN_bn2bin(dsa->q, q_bin)), -1,
+                 "Error in converting public exponent from BN to bin\n");
+        HIP_IFEL(!(q_b64 = (unsigned char *) base64_encode((unsigned char *) 
q_bin,
+                                                           
BN_num_bytes(dsa->q))),
+                 -1, "Failed to encode q_64");
+
+        HIP_IFEL(!(BN_bn2bin(dsa->g, g_bin)), -1,
+                 "Error in converting public exponent from BN to bin\n");
+        HIP_IFEL(!(g_b64 = (unsigned char *) base64_encode((unsigned char *) 
g_bin,
+                                                           
BN_num_bytes(dsa->g))),
+                 -1, "Failed to encode g_b64\n");
+
+        HIP_IFEL(!(BN_bn2bin(dsa->pub_key, y_bin)), -1,
+                 "Error in converting public exponent from BN to bin\n");
+        HIP_IFEL(!(y_b64 = (unsigned char *) base64_encode((unsigned char *) 
y_bin,
+                                                           
BN_num_bytes(dsa->pub_key))),
+                 -1, "Failed to encode y_b64\n");
+
+        sprintf(cert->public_key, "(public_key (dsa-pkcs1-sha1 (p |%s|)(q 
|%s|)"
+                                  "(g |%s|)(y |%s|)))",
+                p_b64, q_b64, g_b64, y_b64);
+    } else {HIP_IFEL(1 == 0, -1, "Unknown algorithm for public-key element\n");
+    }
+
+    _HIP_DEBUG("\n\nPublic-key sequence:\n%s\n\n", cert->public_key);
+    _HIP_DEBUG("\n\nCert sequence:\n%s\n\n", cert->cert);
+    _HIP_DEBUG("\n\nSignature sequence:\n%s\n\n", cert->signature);
+
+    /* Put the results into the msg back */
+
+    _HIP_DEBUG("Len public-key (%d) + cert (%d) + signature (%d) = %d\n"
+               "Sizeof hip_cert_spki_info %d\n",
+               strlen(cert->public_key), strlen((char *) cert->cert), 
strlen((char *) cert->signature),
+               (strlen(cert->public_key) + strlen((char *) cert->cert) + 
strlen((char *) cert->signature)),
+               sizeof(struct hip_cert_spki_info));
+
+    hip_msg_init(msg);
+
+    HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_SPKI_SIGN, 0), -1,
+             "Failed to build user header\n");
+    HIP_IFEL(hip_build_param_cert_spki_info(msg, cert), -1,
+             "Failed to build cert_info\n");
+
+    _HIP_DUMP_MSG(msg);
+
+out_err:
+
+    /* free malloced memory */
+    if (digest_b64) {
+        free(digest_b64);
+    }
+    if (signature_b64) {
+        free(signature_b64);
+    }
+    if (signature) {
+        free(signature);
+    }
+    if (host_id) {
+        free(host_id);
+    }
+
+    /* openssl structs */
 /*        if (algo == HIP_HI_RSA) {
-                if (rsa) RSA_free(rsa);
-        } else if(algo == HIP_HI_DSA) {
-                if (dsa) DSA_free(dsa);
-        }*/
-
-        /* RSA pubkey */
-       if (e_bin) free(e_bin);
-       if (n_bin) free(n_bin);
-        /* encoded */
-       if (e_hex) OPENSSL_free(e_hex);
-       if (n_b64) free(n_b64);
-
-        /* DSA pubkey */ 
-       if (p_bin) free(p_bin);
-        if (q_bin) free(q_bin);
-        if (g_bin) free(g_bin);
-        if (y_bin) free(y_bin);
-        /* encoded */
-       if (p_b64) free(p_b64);
-        if (q_b64) free(q_b64);
-        if (g_b64) free(g_b64);
-        if (y_b64) free(y_b64);
-
-       if (dsa_sig)
-               DSA_SIG_free(dsa_sig);
-
-        return err;
+ *              if (rsa) RSA_free(rsa);
+ *      } else if(algo == HIP_HI_DSA) {
+ *              if (dsa) DSA_free(dsa);
+ *      }*/
+
+    /* RSA pubkey */
+    if (e_bin) {
+        free(e_bin);
+    }
+    if (n_bin) {
+        free(n_bin);
+    }
+    /* encoded */
+    if (e_hex) {
+        OPENSSL_free(e_hex);
+    }
+    if (n_b64) {
+        free(n_b64);
+    }
+
+    /* DSA pubkey */
+    if (p_bin) {
+        free(p_bin);
+    }
+    if (q_bin) {
+        free(q_bin);
+    }
+    if (g_bin) {
+        free(g_bin);
+    }
+    if (y_bin) {
+        free(y_bin);
+    }
+    /* encoded */
+    if (p_b64) {
+        free(p_b64);
+    }
+    if (q_b64) {
+        free(q_b64);
+    }
+    if (g_b64) {
+        free(g_b64);
+    }
+    if (y_b64) {
+        free(y_b64);
+    }
+
+    if (dsa_sig) {
+        DSA_SIG_free(dsa_sig);
+    }
+
+    return err;
 }
 
 /**
@@ -300,354 +334,372 @@
  *
  * @return 0 if signature matches, -1 if error or signature did NOT match
  */
-int hip_cert_spki_verify(struct hip_common * msg) {
-       int err = 0, start = 0, stop = 0, evpret = 0, keylen = 0, algo = 0;
-        char buf[200];
-
-        unsigned char sha_digest[21];
-        unsigned char *sha_retval;
-        unsigned char * signature_hash = NULL;
-        unsigned char * signature_hash_b64 = NULL;
-        unsigned char * signature_b64 = NULL;
-
-        struct hip_cert_spki_info * p_cert;
-        struct hip_cert_spki_info * cert = NULL;
-        unsigned char * signature = NULL;
-
-        /** RSA **/
-        RSA *rsa = NULL;
-        unsigned long e_code;
-        char * e_hex = NULL;
-        unsigned char * modulus_b64 = NULL;
-        unsigned char * modulus = NULL;
-
-        /** DSA **/
-        DSA *dsa = NULL;
-        unsigned char * p_bin = NULL, * q_bin = NULL, * g_bin = NULL, * y_bin 
= NULL;
-        unsigned char * p_b64 = NULL, * q_b64 = NULL, * g_b64 = NULL, * y_b64 
= NULL;
-        DSA_SIG *dsa_sig = NULL;
-
-        /* rules for regular expressions */
-
-        /* 
-           Rule to get the info if we are using DSA
-        */
-        char dsa_rule[] = "[d][s][a][-][p][k][c][s][1][-][s][h][a][1]";
-
-        /* 
-           Rule to get the info if we are using RSA
-        */
-        char rsa_rule[] = "[r][s][a][-][p][k][c][s][1][-][s][h][a][1]";
-
-        /* 
-           Rule to get DSA p
-           Look for pattern "(p |" and stop when first "|" 
-           anything in base 64 is accepted inbetween
-        */
-        char p_rule[] = "[(][p][ ][|][[A-Za-z0-9+/()#=-]*[|]";
-
-        /*
-          Rule to get DSA q
-           Look for pattern "(q |" and stop when first "|" 
-           anything in base 64 is accepted inbetween
-        */
-        char q_rule[] = "[(][q][ ][|][[A-Za-z0-9+/()#=-]*[|]";
-
-        /*
-          Rule to get DSA g
-           Look for pattern "(g |" and stop when first "|" 
-           anything in base 64 is accepted inbetween
-        */
-        char g_rule[] = "[(][g][ ][|][[A-Za-z0-9+/()#=-]*[|]";
-
-        /*
-          Rule to get DSA y / pub_key
-          Look for pattern "(y |" and stop when first "|" 
-           anything in base 64 is accepted inbetween
-        */
-        char y_rule[] = "[(][y][ ][|][[A-Za-z0-9+/()#=-]*[|]";
-
-        /* 
-           rule to get the public exponent RSA 
-           Look for the part that says # and after that some hex blob and #
-        */
-        char e_rule[] = "[#][0-9A-Fa-f]*[#]";
-
-        /* 
-           rule to get the public modulus RSA
-           Look for the part that starts with '|' and after that anything
-           that is in base 64 char set and then '|' again
-        */
-        char n_rule[] = "[|][A-Za-z0-9+/()#=-]*[|]";
-
-        /* 
-           rule to get the signature hash 
-           Look for the similar than the n_rule
-        */
-        char h_rule[] = "[|][A-Za-z0-9+/()#=-]*[|]";
-
-        /* 
-           rule to get the signature 
-           Look for part that starts ")|" and base 64 blob after it
-           and stops to '|' char remember to add and subtract 2 from 
-           the indexes below
-        */
-        char s_rule[] = "[)][|][A-Za-z0-9+/()#=-]*[|]";
-
-        cert = malloc(sizeof(struct hip_cert_spki_info));
-        HIP_IFEL((!cert), -1, "Malloc for cert failed\n");
-        memset(cert, 0, sizeof(struct hip_cert_spki_info));
-
-        HIP_IFEL(!(p_cert = hip_get_param(msg,HIP_PARAM_CERT_SPKI_INFO)), 
-                 -1, "No cert_info struct found\n");
-        memcpy(cert, p_cert, sizeof(struct hip_cert_spki_info));
-        _HIP_DEBUG("\n\n** CONTENTS of public key sequence **\n"
-                   "%s\n\n",cert->public_key); 
-
-        /* check the algo DSA or RSA  */
-        HIP_DEBUG("Verifying\nRunning regexps to identify algo\n");
-        start = stop = 0;
-        algo = hip_cert_regex(dsa_rule, cert->public_key, &start, &stop);
-        if (algo != -1) {
-                HIP_DEBUG("Public-key is DSA\n");
-                algo = HIP_HI_DSA;
-                goto algo_check_done;
-        }
-        start = stop = 0;
-        algo = hip_cert_regex(rsa_rule, cert->public_key, &start, &stop);
-        if (algo != -1) { 
-                HIP_DEBUG("Public-key is RSA\n");
-                algo = HIP_HI_RSA;
-                goto algo_check_done;
-        }
-        HIP_DEBUG((1!=1), -1,"Unknown algorithm\n");
-               
- algo_check_done:               
-        if (algo == HIP_HI_RSA) {
-
-                /* malloc space for new rsa */
-                rsa = RSA_new();
-                HIP_IFEL(!rsa, -1, "Failed to malloc RSA\n");
-
-                /* extract the public-key from cert to rsa */
-
-                /* public exponent first */
-                start = stop = 0;
-                HIP_IFEL(hip_cert_regex(e_rule, cert->public_key, &start, 
&stop), -1,
-                         "Failed to run hip_cert_regex (exponent)\n");
-                _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-                e_hex = malloc(stop-start);
-                HIP_IFEL((!e_hex), -1, "Malloc for e_hex failed\n");
-                snprintf(e_hex, (stop-start-1), "%s", (char 
*)&cert->public_key[start + 1]);
-                _HIP_DEBUG("E_HEX %s\n",e_hex);
-                
-                /* public modulus */
-                start = stop = 0;
-                HIP_IFEL(hip_cert_regex(n_rule, cert->public_key, &start, 
&stop), -1,
-                         "Failed to run hip_cert_regex (modulus)\n");
-                _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-                modulus_b64 = malloc(stop-start+1);
-                HIP_IFEL((!modulus_b64), -1, "Malloc for modulus_b64 
failed\n");
-                memset(modulus_b64, 0, (stop-start+1));
-                modulus = malloc(stop-start+1);
-                HIP_IFEL((!modulus), -1, "Malloc for modulus failed\n");
-                memset(modulus, 0, (stop-start+1));
-                snprintf((char *)modulus_b64, (stop-start-1), "%s", (char 
*)&cert->public_key[start + 1]);
-                _HIP_DEBUG("modulus_b64 %s\n",modulus_b64);
-                
-                /* put the stuff into the RSA struct */
-                BN_hex2bn(&rsa->e, e_hex);
-                evpret = EVP_DecodeBlock(modulus, modulus_b64, 
-                                         strlen((char *)modulus_b64));
-                
-                /* EVP returns a multiple of 3 octets, subtract any extra */
-                keylen = evpret;
-                if (keylen % 4 != 0) {
-                       --keylen;
-                        keylen = keylen - keylen % 2;                  
-               }
-                _HIP_DEBUG("keylen = %d (%d bits)\n", keylen, keylen * 8);
-                signature = malloc(keylen);
-                HIP_IFEL((!signature), -1, "Malloc for signature failed.\n");
-                rsa->n = BN_bin2bn(modulus, keylen, 0);
-                
-                _HIP_DEBUG("In verification RSA e=%s\n", BN_bn2hex(rsa->e));
-                _HIP_DEBUG("In verification RSA n=%s\n", BN_bn2hex(rsa->n));
-
-        } else if (algo == HIP_HI_DSA) {
-                
-                /* malloc space for new dsa */
-                dsa = DSA_new();
-                HIP_IFEL(!dsa, -1, "Failed to malloc DSA\n");
-                
-                /* Extract public key from the cert */
-                
-                /* dsa->p */
-                start = stop = 0;
-                HIP_IFEL(hip_cert_regex(p_rule, cert->public_key, &start, 
&stop), -1,
-                         "Failed to run hip_cert_regex dsa->p\n");
-                _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-                p_b64 = malloc(stop-start+1);
-                HIP_IFEL((!p_b64), -1, "Malloc for p_b64 failed\n");
-                memset(p_b64, 0, (stop-start+1));
-                p_bin = malloc(stop-start+1);
-                HIP_IFEL((!p_bin), -1, "Malloc for p_bin failed\n");
-                memset(p_bin, 0, (stop-start+1));
-                snprintf((char *)p_b64, (stop-start-1), "%s", (char 
*)&cert->public_key[start + 1]);
-                _HIP_DEBUG("p_b64 %s\n",p_b64);
-                evpret = EVP_DecodeBlock(p_bin, p_b64, strlen((char *)p_b64));
-
-                /* dsa->q */
-                start = stop = 0;
-                HIP_IFEL(hip_cert_regex(q_rule, cert->public_key, &start, 
&stop), -1,
-                         "Failed to run hip_cert_regex dsa->q\n");
-                _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-                q_b64 = malloc(stop-start+1);
-                HIP_IFEL((!q_b64), -1, "Malloc for q_b64 failed\n");
-                memset(q_b64, 0, (stop-start+1));
-                q_bin = malloc(stop-start+1);
-                HIP_IFEL((!q_bin), -1, "Malloc for q_bin failed\n");
-                memset(q_bin, 0, (stop-start+1));
-                snprintf((char *)q_b64, (stop-start-1), "%s", (char 
*)&cert->public_key[start + 1]);
-                _HIP_DEBUG("q_b64 %s\n",q_b64);
-                evpret = EVP_DecodeBlock(q_bin, q_b64, strlen((char *)q_b64));
-
-                /* dsa->g */
-                start = stop = 0;
-                HIP_IFEL(hip_cert_regex(g_rule, cert->public_key, &start, 
&stop), -1,
-                         "Failed to run hip_cert_regex dsa->g\n");
-                _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-                g_b64 = malloc(stop-start+1);
-                HIP_IFEL((!g_b64), -1, "Malloc for g_b64 failed\n");
-                memset(g_b64, 0, (stop-start+1));
-                g_bin = malloc(stop-start+1);
-                HIP_IFEL((!g_bin), -1, "Malloc for g_bin failed\n");
-                memset(g_bin, 0, (stop-start+1));
-                snprintf((char *)g_b64, (stop-start-1), "%s", (char 
*)&cert->public_key[start + 1]);
-                _HIP_DEBUG("g_b64 %s\n",g_b64);
-                evpret = EVP_DecodeBlock(g_bin, g_b64, strlen((char *)g_b64));
-
-                /* dsa->y */
-                start = stop = 0;
-                HIP_IFEL(hip_cert_regex(y_rule, cert->public_key, &start, 
&stop), -1,
-                         "Failed to run hip_cert_regex dsa->y\n");
-                _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-                y_b64 = malloc(stop-start+1);
-                HIP_IFEL((!y_b64), -1, "Malloc for y_b64 failed\n");
-                memset(y_b64, 0, (stop-start+1));
-                y_bin = malloc(stop-start+1);
-                HIP_IFEL((!y_bin), -1, "Malloc for y_bin failed\n");
-                memset(y_bin, 0, (stop-start+1));
-                snprintf((char *)y_b64, (stop-start-1), "%s", (char 
*)&cert->public_key[start + 1]);
-                _HIP_DEBUG("y_b64 %s\n",y_b64);
-                evpret = EVP_DecodeBlock(y_bin, y_b64, strlen((char *)y_b64));
-                
-        } else HIP_IFEL((1==0), -1, "Unknown algorithm\n");        
-
-        memset(sha_digest, '\0', sizeof(sha_digest));        
-        /* build sha1 digest that will be signed */
-        HIP_IFEL(!(sha_retval = SHA1((unsigned char *)cert->cert, 
-                                     strlen((char *)cert->cert), sha_digest)),
-                 -1, "SHA1 error when creating digest.\n");        
-        _HIP_HEXDUMP("SHA1 digest of cert sequence ", sha_digest, 20);         
 
-   
-        /* Get the signature hash and compare it to the sha_digest we just 
made */
-        start = stop = 0;
-        HIP_IFEL(hip_cert_regex(h_rule, cert->signature, &start, &stop), -1,
-                 "Failed to run hip_cert_regex (signature hash)\n");
-        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-        signature_hash_b64 = malloc(stop-start+1);
-        HIP_IFEL((!signature_hash_b64), -1, "Failed to malloc 
signature_hash_b64\n");
-        memset(signature_hash_b64, '\0', (stop-start+1));        
-        signature_hash = malloc(stop-start+1);
-        HIP_IFEL((!signature_hash), -1, "Failed to malloc signature_hash\n");
-        snprintf((char *)signature_hash_b64, (stop-start-1), "%s", 
-                 (char *)&cert->signature[start + 1]);       
-        _HIP_DEBUG("SIG HASH B64 %s\n", signature_hash_b64);
-        evpret = EVP_DecodeBlock(signature_hash, signature_hash_b64, 
-                                 strlen((char *)signature_hash_b64));
-        HIP_IFEL(memcmp(sha_digest, signature_hash, 20), -1,
-                 "Signature hash did not match of the one made from the"
-                 "cert sequence in the certificate\n");
-
-        /* memset signature and put it into its place */
-        start = stop = 0;
-        HIP_IFEL(hip_cert_regex(s_rule, cert->signature, &start, &stop), -1,
-                 "Failed to run hip_cert_regex (signature)\n");
-        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
-        signature_b64 = malloc(stop-start+1);
-        HIP_IFEL((!signature_b64), -1, "Failed to malloc signature_b64\n");
-        memset(signature_b64, '\0', keylen);
-        snprintf((char *)signature_b64, (stop-start-2),"%s", (char 
*)&cert->signature[start + 2]);       
-        _HIP_DEBUG("SIG_B64 %s\n", signature_b64);
-        if (algo == HIP_HI_DSA) {
-                signature = malloc(stop-start+1);
-                HIP_IFEL(!signature, -1, "Failed to malloc signature (dsa)\n");
-        }
-        evpret = EVP_DecodeBlock(signature, signature_b64, 
-                                 strlen((char *)signature_b64));
-        _HIP_HEXDUMP("SIG\n", signature, keylen);
-
-        if (algo == HIP_HI_RSA) {
-                /* do the verification */
-                err = RSA_verify(NID_sha1, sha_digest, SHA_DIGEST_LENGTH,
-                                 signature, RSA_size(rsa), rsa);
-                e_code = ERR_get_error();
-                ERR_load_crypto_strings();
-                ERR_error_string(e_code ,buf);
-                
-                _HIP_DEBUG("***********RSA ERROR*************\n");
-                _HIP_DEBUG("RSA_size(rsa) = %d\n",RSA_size(rsa));
-                _HIP_DEBUG("Signature length :%d\n",strlen((char *)signature));
-                _HIP_DEBUG("Error string :%s\n",buf);
-                _HIP_DEBUG("LIB error :%s\n",ERR_lib_error_string(e_code));
-                _HIP_DEBUG("func error :%s\n",ERR_func_error_string(e_code));
-                _HIP_DEBUG("Reason error 
:%s\n",ERR_reason_error_string(e_code));
-                _HIP_DEBUG("***********RSA ERROR*************\n");
-
-                /* RSA_verify returns 1 if success. */
-                cert->success = err == 1 ? 0 : -1;
-                HIP_IFEL((err = err == 1 ? 0 : -1), -1, "RSA_verify error\n");
-
-        } else if (algo == HIP_HI_DSA) {
-
-                /* build the signature structure */
-                dsa_sig = DSA_SIG_new();
-                HIP_IFEL(!dsa_sig, 1, "Failed to allocate DSA_SIG\n");
-                dsa_sig->r = BN_bin2bn(&signature[1], DSA_PRIV, NULL);
-                dsa_sig->s = BN_bin2bn(&signature[1 + DSA_PRIV], DSA_PRIV, 
NULL);
-
-                /* verify the DSA signature */
-                err = DSA_do_verify(sha_digest, SHA_DIGEST_LENGTH, 
-                                    dsa_sig, dsa) == 0 ? 1 : 0;
-
-                /* DSA_do_verify returns 1 if success. */
-                cert->success = err == 1 ? 0 : -1;
-                HIP_IFEL((err = err == 1 ? 0 : -1), -1, "DSA_do_verify 
error\n");
-
-        } else HIP_IFEL((1==0), -1, "Unknown algorithm\n");
-
-        hip_msg_init(msg);
-
-        HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_SPKI_SIGN, 0), -1, 
-                 "Failed to build user header\n");
-        HIP_IFEL(hip_build_param_cert_spki_info(msg, cert), -1,
-                 "Failed to build cert_info\n");                 
-
-        _HIP_DUMP_MSG(msg);
-         
+int hip_cert_spki_verify(struct hip_common *msg)
+{
+    int err = 0, start = 0, stop = 0, evpret = 0, keylen = 0, algo = 0;
+    char buf[200];
+
+    unsigned char sha_digest[21];
+    unsigned char *sha_retval;
+    unsigned char *signature_hash     = NULL;
+    unsigned char *signature_hash_b64 = NULL;
+    unsigned char *signature_b64      = NULL;
+
+    struct hip_cert_spki_info *p_cert;
+    struct hip_cert_spki_info *cert   = NULL;
+    unsigned char *signature          = NULL;
+
+    /** RSA **/
+    RSA *rsa                          = NULL;
+    unsigned long e_code;
+    char *e_hex                       = NULL;
+    unsigned char *modulus_b64        = NULL;
+    unsigned char *modulus            = NULL;
+
+    /** DSA **/
+    DSA *dsa                          = NULL;
+    unsigned char *p_bin              = NULL, *q_bin = NULL, *g_bin = NULL, 
*y_bin = NULL;
+    unsigned char *p_b64              = NULL, *q_b64 = NULL, *g_b64 = NULL, 
*y_b64 = NULL;
+    DSA_SIG *dsa_sig                  = NULL;
+
+    /* rules for regular expressions */
+
+    /*
+     * Rule to get the info if we are using DSA
+     */
+    char dsa_rule[] = "[d][s][a][-][p][k][c][s][1][-][s][h][a][1]";
+
+    /*
+     * Rule to get the info if we are using RSA
+     */
+    char rsa_rule[] = "[r][s][a][-][p][k][c][s][1][-][s][h][a][1]";
+
+    /*
+     * Rule to get DSA p
+     * Look for pattern "(p |" and stop when first "|"
+     * anything in base 64 is accepted inbetween
+     */
+    char p_rule[]   = "[(][p][ ][|][[A-Za-z0-9+/()#=-]*[|]";
+
+    /*
+     * Rule to get DSA q
+     * Look for pattern "(q |" and stop when first "|"
+     * anything in base 64 is accepted inbetween
+     */
+    char q_rule[]   = "[(][q][ ][|][[A-Za-z0-9+/()#=-]*[|]";
+
+    /*
+     * Rule to get DSA g
+     * Look for pattern "(g |" and stop when first "|"
+     * anything in base 64 is accepted inbetween
+     */
+    char g_rule[]   = "[(][g][ ][|][[A-Za-z0-9+/()#=-]*[|]";
+
+    /*
+     * Rule to get DSA y / pub_key
+     * Look for pattern "(y |" and stop when first "|"
+     * anything in base 64 is accepted inbetween
+     */
+    char y_rule[]   = "[(][y][ ][|][[A-Za-z0-9+/()#=-]*[|]";
+
+    /*
+     * rule to get the public exponent RSA
+     * Look for the part that says # and after that some hex blob and #
+     */
+    char e_rule[]   = "[#][0-9A-Fa-f]*[#]";
+
+    /*
+     * rule to get the public modulus RSA
+     * Look for the part that starts with '|' and after that anything
+     * that is in base 64 char set and then '|' again
+     */
+    char n_rule[]   = "[|][A-Za-z0-9+/()#=-]*[|]";
+
+    /*
+     * rule to get the signature hash
+     * Look for the similar than the n_rule
+     */
+    char h_rule[]   = "[|][A-Za-z0-9+/()#=-]*[|]";
+
+    /*
+     * rule to get the signature
+     * Look for part that starts ")|" and base 64 blob after it
+     * and stops to '|' char remember to add and subtract 2 from
+     * the indexes below
+     */
+    char s_rule[]   = "[)][|][A-Za-z0-9+/()#=-]*[|]";
+
+    cert = malloc(sizeof(struct hip_cert_spki_info));
+    HIP_IFEL((!cert), -1, "Malloc for cert failed\n");
+    memset(cert, 0, sizeof(struct hip_cert_spki_info));
+
+    HIP_IFEL(!(p_cert = hip_get_param(msg, HIP_PARAM_CERT_SPKI_INFO)),
+             -1, "No cert_info struct found\n");
+    memcpy(cert, p_cert, sizeof(struct hip_cert_spki_info));
+    _HIP_DEBUG("\n\n** CONTENTS of public key sequence **\n"
+               "%s\n\n", cert->public_key);
+
+    /* check the algo DSA or RSA  */
+    HIP_DEBUG("Verifying\nRunning regexps to identify algo\n");
+    start = stop = 0;
+    algo  = hip_cert_regex(dsa_rule, cert->public_key, &start, &stop);
+    if (algo != -1) {
+        HIP_DEBUG("Public-key is DSA\n");
+        algo = HIP_HI_DSA;
+        goto algo_check_done;
+    }
+    start = stop = 0;
+    algo  = hip_cert_regex(rsa_rule, cert->public_key, &start, &stop);
+    if (algo != -1) {
+        HIP_DEBUG("Public-key is RSA\n");
+        algo = HIP_HI_RSA;
+        goto algo_check_done;
+    }
+    HIP_DEBUG((1 != 1), -1, "Unknown algorithm\n");
+
+algo_check_done:
+    if (algo == HIP_HI_RSA) {
+        /* malloc space for new rsa */
+        rsa = RSA_new();
+        HIP_IFEL(!rsa, -1, "Failed to malloc RSA\n");
+
+        /* extract the public-key from cert to rsa */
+
+        /* public exponent first */
+        start = stop = 0;
+        HIP_IFEL(hip_cert_regex(e_rule, cert->public_key, &start, &stop), -1,
+                 "Failed to run hip_cert_regex (exponent)\n");
+        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+        e_hex = malloc(stop - start);
+        HIP_IFEL((!e_hex), -1, "Malloc for e_hex failed\n");
+        snprintf(e_hex, (stop - start - 1), "%s", (char *) 
&cert->public_key[start + 1]);
+        _HIP_DEBUG("E_HEX %s\n", e_hex);
+
+        /* public modulus */
+        start       = stop = 0;
+        HIP_IFEL(hip_cert_regex(n_rule, cert->public_key, &start, &stop), -1,
+                 "Failed to run hip_cert_regex (modulus)\n");
+        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+        modulus_b64 = malloc(stop - start + 1);
+        HIP_IFEL((!modulus_b64), -1, "Malloc for modulus_b64 failed\n");
+        memset(modulus_b64, 0, (stop - start + 1));
+        modulus     = malloc(stop - start + 1);
+        HIP_IFEL((!modulus), -1, "Malloc for modulus failed\n");
+        memset(modulus, 0, (stop - start + 1));
+        snprintf((char *) modulus_b64, (stop - start - 1), "%s", (char *) 
&cert->public_key[start + 1]);
+        _HIP_DEBUG("modulus_b64 %s\n", modulus_b64);
+
+        /* put the stuff into the RSA struct */
+        BN_hex2bn(&rsa->e, e_hex);
+        evpret = EVP_DecodeBlock(modulus, modulus_b64,
+                                 strlen((char *) modulus_b64));
+
+        /* EVP returns a multiple of 3 octets, subtract any extra */
+        keylen = evpret;
+        if (keylen % 4 != 0) {
+            --keylen;
+            keylen = keylen - keylen % 2;
+        }
+        _HIP_DEBUG("keylen = %d (%d bits)\n", keylen, keylen * 8);
+        signature = malloc(keylen);
+        HIP_IFEL((!signature), -1, "Malloc for signature failed.\n");
+        rsa->n    = BN_bin2bn(modulus, keylen, 0);
+
+        _HIP_DEBUG("In verification RSA e=%s\n", BN_bn2hex(rsa->e));
+        _HIP_DEBUG("In verification RSA n=%s\n", BN_bn2hex(rsa->n));
+    } else if (algo == HIP_HI_DSA) {
+        /* malloc space for new dsa */
+        dsa = DSA_new();
+        HIP_IFEL(!dsa, -1, "Failed to malloc DSA\n");
+
+        /* Extract public key from the cert */
+
+        /* dsa->p */
+        start = stop = 0;
+        HIP_IFEL(hip_cert_regex(p_rule, cert->public_key, &start, &stop), -1,
+                 "Failed to run hip_cert_regex dsa->p\n");
+        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+        p_b64 = malloc(stop - start + 1);
+        HIP_IFEL((!p_b64), -1, "Malloc for p_b64 failed\n");
+        memset(p_b64, 0, (stop - start + 1));
+        p_bin = malloc(stop - start + 1);
+        HIP_IFEL((!p_bin), -1, "Malloc for p_bin failed\n");
+        memset(p_bin, 0, (stop - start + 1));
+        snprintf((char *) p_b64, (stop - start - 1), "%s", (char *) 
&cert->public_key[start + 1]);
+        _HIP_DEBUG("p_b64 %s\n", p_b64);
+        evpret = EVP_DecodeBlock(p_bin, p_b64, strlen((char *) p_b64));
+
+        /* dsa->q */
+        start  = stop = 0;
+        HIP_IFEL(hip_cert_regex(q_rule, cert->public_key, &start, &stop), -1,
+                 "Failed to run hip_cert_regex dsa->q\n");
+        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+        q_b64  = malloc(stop - start + 1);
+        HIP_IFEL((!q_b64), -1, "Malloc for q_b64 failed\n");
+        memset(q_b64, 0, (stop - start + 1));
+        q_bin  = malloc(stop - start + 1);
+        HIP_IFEL((!q_bin), -1, "Malloc for q_bin failed\n");
+        memset(q_bin, 0, (stop - start + 1));
+        snprintf((char *) q_b64, (stop - start - 1), "%s", (char *) 
&cert->public_key[start + 1]);
+        _HIP_DEBUG("q_b64 %s\n", q_b64);
+        evpret = EVP_DecodeBlock(q_bin, q_b64, strlen((char *) q_b64));
+
+        /* dsa->g */
+        start  = stop = 0;
+        HIP_IFEL(hip_cert_regex(g_rule, cert->public_key, &start, &stop), -1,
+                 "Failed to run hip_cert_regex dsa->g\n");
+        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+        g_b64  = malloc(stop - start + 1);
+        HIP_IFEL((!g_b64), -1, "Malloc for g_b64 failed\n");
+        memset(g_b64, 0, (stop - start + 1));
+        g_bin  = malloc(stop - start + 1);
+        HIP_IFEL((!g_bin), -1, "Malloc for g_bin failed\n");
+        memset(g_bin, 0, (stop - start + 1));
+        snprintf((char *) g_b64, (stop - start - 1), "%s", (char *) 
&cert->public_key[start + 1]);
+        _HIP_DEBUG("g_b64 %s\n", g_b64);
+        evpret = EVP_DecodeBlock(g_bin, g_b64, strlen((char *) g_b64));
+
+        /* dsa->y */
+        start  = stop = 0;
+        HIP_IFEL(hip_cert_regex(y_rule, cert->public_key, &start, &stop), -1,
+                 "Failed to run hip_cert_regex dsa->y\n");
+        _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+        y_b64  = malloc(stop - start + 1);
+        HIP_IFEL((!y_b64), -1, "Malloc for y_b64 failed\n");
+        memset(y_b64, 0, (stop - start + 1));
+        y_bin  = malloc(stop - start + 1);
+        HIP_IFEL((!y_bin), -1, "Malloc for y_bin failed\n");
+        memset(y_bin, 0, (stop - start + 1));
+        snprintf((char *) y_b64, (stop - start - 1), "%s", (char *) 
&cert->public_key[start + 1]);
+        _HIP_DEBUG("y_b64 %s\n", y_b64);
+        evpret = EVP_DecodeBlock(y_bin, y_b64, strlen((char *) y_b64));
+    } else {
+        HIP_IFEL((1 == 0), -1, "Unknown algorithm\n");
+    }
+
+    memset(sha_digest, '\0', sizeof(sha_digest));
+    /* build sha1 digest that will be signed */
+    HIP_IFEL(!(sha_retval = SHA1((unsigned char *) cert->cert,
+                                 strlen((char *) cert->cert), sha_digest)),
+             -1, "SHA1 error when creating digest.\n");
+    _HIP_HEXDUMP("SHA1 digest of cert sequence ", sha_digest, 20);
+
+    /* Get the signature hash and compare it to the sha_digest we just made */
+    start              = stop = 0;
+    HIP_IFEL(hip_cert_regex(h_rule, cert->signature, &start, &stop), -1,
+             "Failed to run hip_cert_regex (signature hash)\n");
+    _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+    signature_hash_b64 = malloc(stop - start + 1);
+    HIP_IFEL((!signature_hash_b64), -1, "Failed to malloc 
signature_hash_b64\n");
+    memset(signature_hash_b64, '\0', (stop - start + 1));
+    signature_hash     = malloc(stop - start + 1);
+    HIP_IFEL((!signature_hash), -1, "Failed to malloc signature_hash\n");
+    snprintf((char *) signature_hash_b64, (stop - start - 1), "%s",
+             (char *) &cert->signature[start + 1]);
+    _HIP_DEBUG("SIG HASH B64 %s\n", signature_hash_b64);
+    evpret = EVP_DecodeBlock(signature_hash, signature_hash_b64,
+                             strlen((char *) signature_hash_b64));
+    HIP_IFEL(memcmp(sha_digest, signature_hash, 20), -1,
+             "Signature hash did not match of the one made from the"
+             "cert sequence in the certificate\n");
+
+    /* memset signature and put it into its place */
+    start         = stop = 0;
+    HIP_IFEL(hip_cert_regex(s_rule, cert->signature, &start, &stop), -1,
+             "Failed to run hip_cert_regex (signature)\n");
+    _HIP_DEBUG("REGEX results from %d to %d\n", start, stop);
+    signature_b64 = malloc(stop - start + 1);
+    HIP_IFEL((!signature_b64), -1, "Failed to malloc signature_b64\n");
+    memset(signature_b64, '\0', keylen);
+    snprintf((char *) signature_b64, (stop - start - 2), "%s", (char *) 
&cert->signature[start + 2]);
+    _HIP_DEBUG("SIG_B64 %s\n", signature_b64);
+    if (algo == HIP_HI_DSA) {
+        signature = malloc(stop - start + 1);
+        HIP_IFEL(!signature, -1, "Failed to malloc signature (dsa)\n");
+    }
+    evpret = EVP_DecodeBlock(signature, signature_b64,
+                             strlen((char *) signature_b64));
+    _HIP_HEXDUMP("SIG\n", signature, keylen);
+
+    if (algo == HIP_HI_RSA) {
+        /* do the verification */
+        err    = RSA_verify(NID_sha1, sha_digest, SHA_DIGEST_LENGTH,
+                            signature, RSA_size(rsa), rsa);
+        e_code = ERR_get_error();
+        ERR_load_crypto_strings();
+        ERR_error_string(e_code, buf);
+
+        _HIP_DEBUG("***********RSA ERROR*************\n");
+        _HIP_DEBUG("RSA_size(rsa) = %d\n", RSA_size(rsa));
+        _HIP_DEBUG("Signature length :%d\n", strlen((char *) signature));
+        _HIP_DEBUG("Error string :%s\n", buf);
+        _HIP_DEBUG("LIB error :%s\n", ERR_lib_error_string(e_code));
+        _HIP_DEBUG("func error :%s\n", ERR_func_error_string(e_code));
+        _HIP_DEBUG("Reason error :%s\n", ERR_reason_error_string(e_code));
+        _HIP_DEBUG("***********RSA ERROR*************\n");
+
+        /* RSA_verify returns 1 if success. */
+        cert->success = err == 1 ? 0 : -1;
+        HIP_IFEL((err = err == 1 ? 0 : -1), -1, "RSA_verify error\n");
+    } else if (algo == HIP_HI_DSA) {
+        /* build the signature structure */
+        dsa_sig       = DSA_SIG_new();
+        HIP_IFEL(!dsa_sig, 1, "Failed to allocate DSA_SIG\n");
+        dsa_sig->r    = BN_bin2bn(&signature[1], DSA_PRIV, NULL);
+        dsa_sig->s    = BN_bin2bn(&signature[1 + DSA_PRIV], DSA_PRIV, NULL);
+
+        /* verify the DSA signature */
+        err           = DSA_do_verify(sha_digest, SHA_DIGEST_LENGTH,
+                                      dsa_sig, dsa) == 0 ? 1 : 0;
+
+        /* DSA_do_verify returns 1 if success. */
+        cert->success = err == 1 ? 0 : -1;
+        HIP_IFEL((err = err == 1 ? 0 : -1), -1, "DSA_do_verify error\n");
+    } else {
+        HIP_IFEL((1 == 0), -1, "Unknown algorithm\n");
+    }
+
+    hip_msg_init(msg);
+
+    HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_SPKI_SIGN, 0), -1,
+             "Failed to build user header\n");
+    HIP_IFEL(hip_build_param_cert_spki_info(msg, cert), -1,
+             "Failed to build cert_info\n");
+
+    _HIP_DUMP_MSG(msg);
+
 out_err:
-        if (signature_hash_b64) free(signature_hash_b64);
-        if (signature_hash) free(signature_hash);
-        if (modulus_b64) free(modulus_b64);
-        if (modulus) free(modulus);
-        if (cert) free(cert);
-        if (rsa) RSA_free(rsa);
-       if (signature) free(signature);
-       if (e_hex) free(e_hex);
-        if (dsa) DSA_free(dsa);
-       if (dsa_sig) DSA_SIG_free(dsa_sig);
-       return (err);
+    if (signature_hash_b64) {
+        free(signature_hash_b64);
+    }
+    if (signature_hash) {
+        free(signature_hash);
+    }
+    if (modulus_b64) {
+        free(modulus_b64);
+    }
+    if (modulus) {
+        free(modulus);
+    }
+    if (cert) {
+        free(cert);
+    }
+    if (rsa) {
+        RSA_free(rsa);
+    }
+    if (signature) {
+        free(signature);
+    }
+    if (e_hex) {
+        free(e_hex);
+    }
+    if (dsa) {
+        DSA_free(dsa);
+    }
+    if (dsa_sig) {
+        DSA_SIG_free(dsa_sig);
+    }
+    return err;
 }
 
 /****************************************************************************
@@ -662,325 +714,343 @@
  * @param msg is a pointer to the msg containing a x509v3 cert in cert 
parameter
  * @param db is the HIP_HASHTABLE to query for the hostid entry
  *
- * @return 0 on success negative otherwise. 
- */ 
-int hip_cert_x509v3_handle_request_to_sign(struct hip_common * msg,  
HIP_HASHTABLE * db) {
-       int err = 0, i = 0, nid = 0, ret = 0, secs = 0, algo = 0;
-       CONF * conf;
-       CONF_VALUE * item;
-       STACK_OF(CONF_VALUE) * sec_general = NULL;
-       STACK_OF(CONF_VALUE) * sec_name = NULL;
-       STACK_OF(CONF_VALUE) * sec_ext = NULL;
-
-        X509_REQ * req = NULL;
-        X509_NAME * issuer = NULL;
-        X509_NAME * subj = NULL;
-        X509_EXTENSION * ext = NULL;
-        STACK_OF(X509_EXTENSION) * extlist = NULL;
-        X509_NAME_ENTRY *ent;
-        EVP_PKEY *pkey; 
-        /** XX TODO THIS should come from a configuration file 
-            monotonically increasing counter **/
-        long serial = 0; 
-        const EVP_MD * digest = NULL;
-        X509 *cert;
-        X509V3_CTX ctx;
-        struct hip_cert_x509_req * subject;
-        char subject_hit[41];
-        char issuer_hit[41];
-        char ialtname[45];
-        char saltname[45];
-        struct in6_addr * issuer_hit_n;
-        struct hip_host_id * host_id;
-        RSA * rsa = NULL;
-        DSA * dsa = NULL;
-        char cert_str_pem[1024];
-        unsigned char * der_cert = NULL;
-        int der_cert_len = 0;
-
-        HIP_IFEL(!(subject = malloc(sizeof(struct in6_addr))), -1, 
-                 "Malloc for subject failed\n");   
-        HIP_IFEL(!(issuer_hit_n = malloc(sizeof(struct in6_addr))), -1, 
-                 "Malloc for subject failed\n"); 
-        HIP_IFEL(!(pkey = malloc(sizeof(EVP_PKEY))), -1, 
-                 "Malloc for pkey failed\n");  
-        HIP_IFEL(!memset(subject, 0, sizeof(subject)), -1,
-                 "Failed to memset memory for subject\n");
-        HIP_IFEL(!memset(issuer_hit_n, 0, sizeof(issuer_hit_n)), -1,
-                 "Failed to memset memory for issuer\n");               
-        HIP_IFEL(!memset(subject_hit, '\0', sizeof(subject_hit)), -1,
-                 "Failed to memset memory for subject\n");                
-        HIP_IFEL(!memset(issuer_hit_n, 0, sizeof(struct in6_addr)), -1,
-                 "Failed to memset memory for issuer HIT\n");
-        HIP_IFEL(!memset(cert_str_pem, 0, sizeof(cert_str_pem)), -1,
-                 "Failed to memset memory for cert_str\n");
-        HIP_IFEL(!memset(ialtname, 0, sizeof(ialtname)), -1,
-                 "Failed to memset memory for ialtname\n");
-        HIP_IFEL(!memset(saltname, 0, sizeof(saltname)), -1,
-                 "Failed to memset memory for saltname\n");
-       
-        OpenSSL_add_all_algorithms();
-        ERR_load_crypto_strings();
-        
-       HIP_DEBUG("Reading configuration file (%s)\n", HIP_CERT_CONF_PATH);
-       conf = hip_cert_open_conf();
-       sec_general = hip_cert_read_conf_section("hip_x509v3", conf);
-        sec_name = hip_cert_read_conf_section("hip_x509v3_name", conf);
-        sec_ext = hip_cert_read_conf_section("hip_x509v3_extensions", conf);
-       hip_cert_free_conf(conf);
-
-        /* Get the general information */
-        HIP_IFEL((sec_general == NULL), -1, 
-                 "Failed to load general certificate information\n");
-        HIP_IFEL(!(req = X509_REQ_new()), -1, "Failed to create X509_REQ 
object");
-
-        HIP_IFEL((sec_name = NULL), -1,
-                 "Failed to load issuer naming information for the 
certificate\n");
- 
-        /* Issuer naming */
-        if (sec_general != NULL) {
-                /* Loop through the conf stack for general information */
-                extlist = sk_X509_EXTENSION_new_null();
-                for (i = 0; i < sk_CONF_VALUE_num(sec_general); i++) {
-                        item = (void*)sk_CONF_VALUE_value(sec_general, i);
-                        _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n", 
-                                   item->section, item->name, item->value);
-                        if(!strcmp(item->name, "issuerhit")) {
-                                strcpy(issuer_hit, item->value);
-                                ret = inet_pton(AF_INET6, item->value, 
issuer_hit_n);
-                                HIP_IFEL((ret < 0 && errno == EAFNOSUPPORT), 
-1, 
-                                         "Failed to convert issuer HIT to 
hip_hit_t\n");
-                                HIP_DEBUG_HIT("Issuer HIT", issuer_hit_n);
-                                /* on conversion more to get rid of padding 
0s*/
-                                memset(issuer_hit, 0, sizeof(issuer_hit));
-                                HIP_IFEL((!inet_ntop(AF_INET6, issuer_hit_n, 
-                                                     issuer_hit, 
sizeof(issuer_hit))),
-                                         -1, "Failed to convert subject hit to 
"
-                                         "presentation format\n");
-                        }
-                        if(!strcmp(item->name, "days")) 
-                           secs = HIP_CERT_DAY * atoi(item->value);
-                }
-        } 
-        HIP_IFEL(!(issuer = X509_NAME_new()), -1, "Failed to set create issuer 
name");
-        nid = OBJ_txt2nid("commonName");
-        HIP_IFEL((nid == NID_undef), -1, "NID text not defined\n");
-        HIP_IFEL(!(ent = X509_NAME_ENTRY_create_by_NID (NULL, nid, 
MBSTRING_ASC,
-                                                        (unsigned char 
*)issuer_hit, -1)), -1,
-                 "Failed to create name entry for issuer\n");
-        HIP_IFEL((X509_NAME_add_entry(issuer, ent, -1, 0) != 1), -1,
-                 "Failed to add entry to issuer name\n");
-        
-        /* Subject naming */
-        /* Get the subject hit from msg */
-        HIP_IFEL(!(subject = hip_get_param(msg, HIP_PARAM_CERT_X509_REQ)), 
-                 -1, "No cert_info struct found\n");
-        _HIP_DEBUG_HIT("Subject", &subject->addr);
-        HIP_IFEL((!inet_ntop(AF_INET6, &subject->addr, subject_hit, 
sizeof(subject_hit))),
-                 -1, "Failed to convert subject hit to presentation format\n");
-        _HIP_DEBUG("Subject HIT is %s (id for commonName = %d)\n", 
subject_hit, nid);
-        HIP_IFEL(!(subj = X509_NAME_new()), -1, "Failed to set create subject 
name");
-        nid = OBJ_txt2nid("commonName");
-        HIP_IFEL((nid == NID_undef), -1, "NID text not defined\n");
-        HIP_IFEL(!(ent = X509_NAME_ENTRY_create_by_NID (NULL, nid, 
MBSTRING_ASC,
-                                                        (unsigned char 
*)subject_hit, -1)), -1,
-                 "Failed to create name entry for subject\n");
-        HIP_IFEL((X509_NAME_add_entry(subj, ent, -1, 0) != 1), -1,
-                 "Failed to add entry to subject name\n");
-        HIP_IFEL((X509_REQ_set_subject_name (req, subj) != 1), -1,
-                 "Failed to add subject name to certificate request\n");
-          
-       /* XX TODO add a check to skip subjectAltName and issuerAltName because 
they are 
-          already in use by with IP:<hit> stuff */
-        if (sec_ext != NULL) {
-                /* Loop through the conf stack and add extensions to ext stack 
*/
-                extlist = sk_X509_EXTENSION_new_null();
-                for (i = 0; i < sk_CONF_VALUE_num(sec_ext); i++) {
-                        item = (void*)sk_CONF_VALUE_value(sec_ext, i);
-                        _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n", 
-                                   item->section, item->name, item->value);
-                        HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx, 
-                                                       item->name, item->value 
)), -1, 
-                                 "Failed to create extension\n");
-                        sk_X509_EXTENSION_push(extlist, ext);
-                }
-                HIP_IFEL((!X509_REQ_add_extensions(req, extlist)), -1,
-                          "Failed to add extensions to the request\n");
-        }
-#if 0
-        /* DEBUG PART START for the certificate request */
-        HIP_DEBUG("x.509v3 certificate request in readable format\n\n");
-        HIP_IFEL(!X509_REQ_print_fp(stdout, req), -1,
-                 "Failed to print x.509v3 request in human readable format\n");
-        HIP_DEBUG("x.509v3 certificate request in PEM format\n\n");
-        HIP_IFEL((PEM_write_X509_REQ(stdout, req) != 1), -1 ,
-                 "Failed to write the x509 request in PEM to stdout\n");
-        /* DEBUG PART END for the certificate request*/
-#endif     
-   
-        /** NOW WE ARE READY TO CREATE A CERTIFICATE FROM THE REQUEST **/      
  
-        HIP_DEBUG("Starting the certificate creation\n");
-
-        HIP_IFEL(!(cert = X509_new ()), -1,
-                 "Failed to create X509 object\n");        
-
-        HIP_IFEL((X509_set_version (cert, 2L) != 1), -1,
-                  "Failed to set certificate version\n");
-        /** XX TODO serial should be stored after increasing it **/
-        ASN1_INTEGER_set (X509_get_serialNumber(cert), serial++);
-        
-        HIP_IFEL((X509_set_subject_name (cert, subj) != 1), -1,
-                "Failed to set subject name of certificate\n");
-        HIP_IFEL((X509_set_issuer_name (cert, issuer) != 1), -1,
-                 "Failed to set issuer name of certificate\n");
-        HIP_IFEL(!(X509_gmtime_adj (X509_get_notBefore (cert), 0)), -1,
-                 "Error setting beginning time of the certificate");
-        HIP_IFEL(!(X509_gmtime_adj (X509_get_notAfter (cert), secs)), -1, 
-                 "Error setting ending time of the certificate");
-
-        HIP_DEBUG("Getting the key\n");
-
-       HIP_IFEL(hip_get_host_id_and_priv_key(hip_local_hostid_db, issuer_hit_n,
-                                             HIP_ANY_ALGO, &host_id, (void 
*)&rsa), 
-                -1, "Private key not found\n");
-
-       algo = host_id->rdata.algorithm;
-       if (algo == HIP_HI_DSA)
-               dsa = (DSA *)rsa;
-        
-        if (algo == HIP_HI_RSA) {
-                
-                HIP_IFEL(!EVP_PKEY_assign_RSA(pkey, rsa), -1, 
-                         "Failed to convert RSA to EVP_PKEY\n");
-                HIP_IFEL((X509_set_pubkey (cert, pkey) != 1), -1, 
-                         "Failed to set public key of the certificate\n");
-        } else if (algo == HIP_HI_DSA) {
-
-                HIP_IFEL(!EVP_PKEY_assign_DSA(pkey, dsa), -1, 
-                         "Failed to convert DSA to EVP_PKEY\n");
-                HIP_IFEL((X509_set_pubkey (cert, pkey) != 1), -1, 
-                         "Failed to set public key of the certificate\n");
-                
-        } else HIP_IFEL (1==0, -1, "Unknown algorithm\n");
-
-       X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
-
-        if (sec_ext != NULL) {
-                for (i = 0; i < sk_CONF_VALUE_num(sec_ext); i++) {
-                        item = (void*)sk_CONF_VALUE_value(sec_ext, i);
-                        _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n", 
-                                   item->section, item->name, item->value);
-                        /* 
-                           Skip issuerAltName and subjectAltName because 
-                           HITs use them already. Skip also basicConstraint =
-                           CA:true and subjectKeyIdentifier because they are
-                           added automatically in the code below 
-                        */
-                        if (!strcmp(item->name, "issuerAltname")) continue;
-                        if (!strcmp(item->name, "subjectAltname")) continue;
-                        if (0 == memcmp(subject_hit, issuer_hit, 
sizeof(issuer_hit))) {
-                                if (!strcmp(item->name, "basicConstraints") &&
-                                    !strcmp(item->value, "CA:true"))
-                                        continue;
-                                if (!strcmp(item->name, 
"subjectKeyIdentifier"))
-                                        continue;
-                        }
-                        HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx, 
-                                                         item->name, 
item->value )), -1, 
-                                 "Failed to create extension\n");
-                        HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
-                                 "Failed to add extensions to the cert\n");    
     
-                }
-        }
-       
-        if (0 == memcmp(subject_hit, issuer_hit, sizeof(issuer_hit))) {
-                /* Subjects and issuers hit match so 
-                  we are writing a CA cert and in CA self-signed
-                   certificate you have to have subject key identifier
-                   present, when adding subjectKeyIdentifier give string
-                   hash to the X509_EXT_conf it knows what to do with it */
-
-                HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx, 
-                                                 "basicConstraints", 
-                                                 "CA:true")), -1, 
-                         "Failed to create extension\n");
-                HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
-                         "Failed to add extensions to the cert\n");
-
-                HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx, 
-                                                 "subjectKeyIdentifier", 
-                                                 "hash")), -1, 
-                         "Failed to create extension\n");
-                HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
-                         "Failed to add extensions to the cert\n");
-        }
-
-       /* add subjectAltName = IP:<HIT> */
-       sprintf(ialtname, "IP:%s",issuer_hit);
-       HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx, 
-                                        "issuerAltName", 
-                                        ialtname)), -1, 
-                "Failed to create extension\n");
-       HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
-                "Failed to add extensions to the cert\n");
-       /* add subjectAltName = IP:<HIT> */
-       sprintf(saltname, "IP:%s",subject_hit);
-       HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx, 
-                                        "subjectAltName", 
-                                        saltname)), -1, 
-                "Failed to create extension\n");
-       HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
-                "Failed to add extensions to the cert\n");
-
-        if (algo == HIP_HI_RSA)
-                digest = EVP_sha1();
-        else if (algo == HIP_HI_DSA)
-                digest = EVP_dss1();
-        else
-                HIP_IFEL((1==0), -1, "Unknown algorithm\n");
-
-        HIP_IFEL(!(X509_sign (cert, pkey, digest)), -1,
-                 "Failed to sign x509v3 certificate\n"); 
-#if 0
-        /* DEBUG PART START for the certificate */
-        HIP_DEBUG("x.509v3 certificate in readable format\n\n");
-        HIP_IFEL(!X509_print_fp(stdout, cert), -1,
-                 "Failed to print x.509v3 in human readable format\n");
-        HIP_DEBUG("x.509v3 certificate in PEM format\n\n");
-        HIP_IFEL((PEM_write_X509(stdout, cert) != 1), -1, 
-                 "Failed to write the x509 in PEM to stdout\n");
-        /* DEBUG PART END for the certificate */
-#endif
-
-        /** DER **/
-        HIP_IFEL(((der_cert_len = i2d_X509(cert, &der_cert)) < 0), -1, 
-                 "Failed to convert cert to DER\n");
-        _HIP_HEXDUMP("DER:\n", der_cert, der_cert_len);
-        _HIP_DEBUG("DER length %d\n", der_cert_len);
-        /** end DER **/
-
-        hip_msg_init(msg);
-
-        HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_X509V3_SIGN, 0), -1, 
-                 "Failed to build user header\n");
-        HIP_IFEL(hip_build_param_cert_x509_resp(msg, (char *)der_cert, 
der_cert_len), -1, 
-                 "Failed to create x509 response parameter\n");        
-        _HIP_DUMP_MSG(msg);
+ * @return 0 on success negative otherwise.
+ */
+int hip_cert_x509v3_handle_request_to_sign(struct hip_common *msg,  
HIP_HASHTABLE *db)
+{
+    int err = 0, i = 0, nid = 0, ret = 0, secs = 0, algo = 0;
+    CONF *conf;
+    CONF_VALUE *item;
+    STACK_OF(CONF_VALUE) * sec_general = NULL;
+    STACK_OF(CONF_VALUE) * sec_name    = NULL;
+    STACK_OF(CONF_VALUE) * sec_ext     = NULL;
+
+    X509_REQ *req       = NULL;
+    X509_NAME *issuer   = NULL;
+    X509_NAME *subj     = NULL;
+    X509_EXTENSION *ext = NULL;
+    STACK_OF(X509_EXTENSION) * extlist = NULL;
+    X509_NAME_ENTRY *ent;
+    EVP_PKEY *pkey;
+    /** XX TODO THIS should come from a configuration file
+     *  monotonically increasing counter **/
+    long serial          = 0;
+    const EVP_MD *digest = NULL;
+    X509 *cert;
+    X509V3_CTX ctx;
+    struct hip_cert_x509_req *subject;
+    char subject_hit[41];
+    char issuer_hit[41];
+    char ialtname[45];
+    char saltname[45];
+    struct in6_addr *issuer_hit_n;
+    struct hip_host_id *host_id;
+    RSA *rsa                = NULL;
+    DSA *dsa                = NULL;
+    char cert_str_pem[1024];
+    unsigned char *der_cert = NULL;
+    int der_cert_len        = 0;
+
+    HIP_IFEL(!(subject = malloc(sizeof(struct in6_addr))), -1,
+             "Malloc for subject failed\n");
+    HIP_IFEL(!(issuer_hit_n = malloc(sizeof(struct in6_addr))), -1,
+             "Malloc for subject failed\n");
+    HIP_IFEL(!(pkey = malloc(sizeof(EVP_PKEY))), -1,
+             "Malloc for pkey failed\n");
+    HIP_IFEL(!memset(subject, 0, sizeof(subject)), -1,
+             "Failed to memset memory for subject\n");
+    HIP_IFEL(!memset(issuer_hit_n, 0, sizeof(issuer_hit_n)), -1,
+             "Failed to memset memory for issuer\n");
+    HIP_IFEL(!memset(subject_hit, '\0', sizeof(subject_hit)), -1,
+             "Failed to memset memory for subject\n");
+    HIP_IFEL(!memset(issuer_hit_n, 0, sizeof(struct in6_addr)), -1,
+             "Failed to memset memory for issuer HIT\n");
+    HIP_IFEL(!memset(cert_str_pem, 0, sizeof(cert_str_pem)), -1,
+             "Failed to memset memory for cert_str\n");
+    HIP_IFEL(!memset(ialtname, 0, sizeof(ialtname)), -1,
+             "Failed to memset memory for ialtname\n");
+    HIP_IFEL(!memset(saltname, 0, sizeof(saltname)), -1,
+             "Failed to memset memory for saltname\n");
+
+    OpenSSL_add_all_algorithms();
+    ERR_load_crypto_strings();
+
+    HIP_DEBUG("Reading configuration file (%s)\n", HIP_CERT_CONF_PATH);
+    conf        = hip_cert_open_conf();
+    sec_general = hip_cert_read_conf_section("hip_x509v3", conf);
+    sec_name    = hip_cert_read_conf_section("hip_x509v3_name", conf);
+    sec_ext     = hip_cert_read_conf_section("hip_x509v3_extensions", conf);
+    hip_cert_free_conf(conf);
+
+    /* Get the general information */
+    HIP_IFEL((sec_general == NULL), -1,
+             "Failed to load general certificate information\n");
+    HIP_IFEL(!(req = X509_REQ_new()), -1, "Failed to create X509_REQ object");
+
+    HIP_IFEL((sec_name = NULL), -1,
+             "Failed to load issuer naming information for the certificate\n");
+
+    /* Issuer naming */
+    if (sec_general != NULL) {
+        /* Loop through the conf stack for general information */
+        extlist = sk_X509_EXTENSION_new_null();
+        for (i = 0; i < sk_CONF_VALUE_num(sec_general); i++) {
+            item = (void *) sk_CONF_VALUE_value(sec_general, i);
+            _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n",
+                       item->section, item->name, item->value);
+            if (!strcmp(item->name, "issuerhit")) {
+                strcpy(issuer_hit, item->value);
+                ret = inet_pton(AF_INET6, item->value, issuer_hit_n);
+                HIP_IFEL((ret < 0 && errno == EAFNOSUPPORT), -1,
+                         "Failed to convert issuer HIT to hip_hit_t\n");
+                HIP_DEBUG_HIT("Issuer HIT", issuer_hit_n);
+                /* on conversion more to get rid of padding 0s*/
+                memset(issuer_hit, 0, sizeof(issuer_hit));
+                HIP_IFEL((!inet_ntop(AF_INET6, issuer_hit_n,
+                                     issuer_hit, sizeof(issuer_hit))),
+                         -1, "Failed to convert subject hit to "
+                             "presentation format\n");
+            }
+            if (!strcmp(item->name, "days")) {
+                secs = HIP_CERT_DAY * atoi(item->value);
+            }
+        }
+    }
+    HIP_IFEL(!(issuer = X509_NAME_new()), -1, "Failed to set create issuer 
name");
+    nid = OBJ_txt2nid("commonName");
+    HIP_IFEL((nid == NID_undef), -1, "NID text not defined\n");
+    HIP_IFEL(!(ent = X509_NAME_ENTRY_create_by_NID(NULL, nid, MBSTRING_ASC,
+                                                   (unsigned char *) 
issuer_hit, -1)), -1,
+             "Failed to create name entry for issuer\n");
+    HIP_IFEL((X509_NAME_add_entry(issuer, ent, -1, 0) != 1), -1,
+             "Failed to add entry to issuer name\n");
+
+    /* Subject naming */
+    /* Get the subject hit from msg */
+    HIP_IFEL(!(subject = hip_get_param(msg, HIP_PARAM_CERT_X509_REQ)),
+             -1, "No cert_info struct found\n");
+    _HIP_DEBUG_HIT("Subject", &subject->addr);
+    HIP_IFEL((!inet_ntop(AF_INET6, &subject->addr, subject_hit, 
sizeof(subject_hit))),
+             -1, "Failed to convert subject hit to presentation format\n");
+    _HIP_DEBUG("Subject HIT is %s (id for commonName = %d)\n", subject_hit, 
nid);
+    HIP_IFEL(!(subj = X509_NAME_new()), -1, "Failed to set create subject 
name");
+    nid = OBJ_txt2nid("commonName");
+    HIP_IFEL((nid == NID_undef), -1, "NID text not defined\n");
+    HIP_IFEL(!(ent = X509_NAME_ENTRY_create_by_NID(NULL, nid, MBSTRING_ASC,
+                                                   (unsigned char *) 
subject_hit, -1)), -1,
+             "Failed to create name entry for subject\n");
+    HIP_IFEL((X509_NAME_add_entry(subj, ent, -1, 0) != 1), -1,
+             "Failed to add entry to subject name\n");
+    HIP_IFEL((X509_REQ_set_subject_name(req, subj) != 1), -1,
+             "Failed to add subject name to certificate request\n");
+
+    /* XX TODO add a check to skip subjectAltName and issuerAltName because 
they are
+     * already in use by with IP:<hit> stuff */
+    if (sec_ext != NULL) {
+        /* Loop through the conf stack and add extensions to ext stack */
+        extlist = sk_X509_EXTENSION_new_null();
+        for (i = 0; i < sk_CONF_VALUE_num(sec_ext); i++) {
+            item = (void *) sk_CONF_VALUE_value(sec_ext, i);
+            _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n",
+                       item->section, item->name, item->value);
+            HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx,
+                                             item->name, item->value )), -1,
+                     "Failed to create extension\n");
+            sk_X509_EXTENSION_push(extlist, ext);
+        }
+        HIP_IFEL((!X509_REQ_add_extensions(req, extlist)), -1,
+                 "Failed to add extensions to the request\n");
+    }
+#if 0
+    /* DEBUG PART START for the certificate request */
+    HIP_DEBUG("x.509v3 certificate request in readable format\n\n");
+    HIP_IFEL(!X509_REQ_print_fp(stdout, req), -1,
+             "Failed to print x.509v3 request in human readable format\n");
+    HIP_DEBUG("x.509v3 certificate request in PEM format\n\n");
+    HIP_IFEL((PEM_write_X509_REQ(stdout, req) != 1), -1,
+             "Failed to write the x509 request in PEM to stdout\n");
+    /* DEBUG PART END for the certificate request*/
+#endif
+
+    /** NOW WE ARE READY TO CREATE A CERTIFICATE FROM THE REQUEST **/
+    HIP_DEBUG("Starting the certificate creation\n");
+
+    HIP_IFEL(!(cert = X509_new()), -1,
+             "Failed to create X509 object\n");
+
+    HIP_IFEL((X509_set_version(cert, 2L) != 1), -1,
+             "Failed to set certificate version\n");
+    /** XX TODO serial should be stored after increasing it **/
+    ASN1_INTEGER_set(X509_get_serialNumber(cert), serial++);
+
+    HIP_IFEL((X509_set_subject_name(cert, subj) != 1), -1,
+             "Failed to set subject name of certificate\n");
+    HIP_IFEL((X509_set_issuer_name(cert, issuer) != 1), -1,
+             "Failed to set issuer name of certificate\n");
+    HIP_IFEL(!(X509_gmtime_adj(X509_get_notBefore(cert), 0)), -1,
+             "Error setting beginning time of the certificate");
+    HIP_IFEL(!(X509_gmtime_adj(X509_get_notAfter(cert), secs)), -1,
+             "Error setting ending time of the certificate");
+
+    HIP_DEBUG("Getting the key\n");
+
+    HIP_IFEL(hip_get_host_id_and_priv_key(hip_local_hostid_db, issuer_hit_n,
+                                          HIP_ANY_ALGO, &host_id, (void *) 
&rsa),
+             -1, "Private key not found\n");
+
+    algo = host_id->rdata.algorithm;
+    if (algo == HIP_HI_DSA) {
+        dsa = (DSA *) rsa;
+    }
+
+    if (algo == HIP_HI_RSA) {
+        HIP_IFEL(!EVP_PKEY_assign_RSA(pkey, rsa), -1,
+                 "Failed to convert RSA to EVP_PKEY\n");
+        HIP_IFEL((X509_set_pubkey(cert, pkey) != 1), -1,
+                 "Failed to set public key of the certificate\n");
+    } else if (algo == HIP_HI_DSA) {
+        HIP_IFEL(!EVP_PKEY_assign_DSA(pkey, dsa), -1,
+                 "Failed to convert DSA to EVP_PKEY\n");
+        HIP_IFEL((X509_set_pubkey(cert, pkey) != 1), -1,
+                 "Failed to set public key of the certificate\n");
+    } else {
+        HIP_IFEL(1 == 0, -1, "Unknown algorithm\n");
+    }
+
+    X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
+
+    if (sec_ext != NULL) {
+        for (i = 0; i < sk_CONF_VALUE_num(sec_ext); i++) {
+            item = (void *) sk_CONF_VALUE_value(sec_ext, i);
+            _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n",
+                       item->section, item->name, item->value);
+            /*
+             * Skip issuerAltName and subjectAltName because
+             * HITs use them already. Skip also basicConstraint =
+             * CA:true and subjectKeyIdentifier because they are
+             * added automatically in the code below
+             */
+            if (!strcmp(item->name, "issuerAltname")) {
+                continue;
+            }
+            if (!strcmp(item->name, "subjectAltname")) {
+                continue;
+            }
+            if (0 == memcmp(subject_hit, issuer_hit, sizeof(issuer_hit))) {
+                if (!strcmp(item->name, "basicConstraints") &&
+                    !strcmp(item->value, "CA:true")) {
+                    continue;
+                }
+                if (!strcmp(item->name, "subjectKeyIdentifier")) {
+                    continue;
+                }
+            }
+            HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx,
+                                             item->name, item->value )), -1,
+                     "Failed to create extension\n");
+            HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
+                     "Failed to add extensions to the cert\n");
+        }
+    }
+
+    if (0 == memcmp(subject_hit, issuer_hit, sizeof(issuer_hit))) {
+        /* Subjects and issuers hit match so
+         * we are writing a CA cert and in CA self-signed
+         * certificate you have to have subject key identifier
+         * present, when adding subjectKeyIdentifier give string
+         * hash to the X509_EXT_conf it knows what to do with it */
+
+        HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx,
+                                         "basicConstraints",
+                                         "CA:true")), -1,
+                 "Failed to create extension\n");
+        HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
+                 "Failed to add extensions to the cert\n");
+
+        HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx,
+                                         "subjectKeyIdentifier",
+                                         "hash")), -1,
+                 "Failed to create extension\n");
+        HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
+                 "Failed to add extensions to the cert\n");
+    }
+
+    /* add subjectAltName = IP:<HIT> */
+    sprintf(ialtname, "IP:%s", issuer_hit);
+    HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx,
+                                     "issuerAltName",
+                                     ialtname)), -1,
+             "Failed to create extension\n");
+    HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
+             "Failed to add extensions to the cert\n");
+    /* add subjectAltName = IP:<HIT> */
+    sprintf(saltname, "IP:%s", subject_hit);
+    HIP_IFEL(!(ext = X509V3_EXT_conf(NULL, &ctx,
+                                     "subjectAltName",
+                                     saltname)), -1,
+             "Failed to create extension\n");
+    HIP_IFEL((!X509_add_ext(cert, ext, -1)), -1,
+             "Failed to add extensions to the cert\n");
+
+    if (algo == HIP_HI_RSA) {
+        digest = EVP_sha1();
+    } else if (algo == HIP_HI_DSA) {
+        digest = EVP_dss1();
+    } else {
+        HIP_IFEL((1 == 0), -1, "Unknown algorithm\n");
+    }
+
+    HIP_IFEL(!(X509_sign(cert, pkey, digest)), -1,
+             "Failed to sign x509v3 certificate\n");
+#if 0
+    /* DEBUG PART START for the certificate */
+    HIP_DEBUG("x.509v3 certificate in readable format\n\n");
+    HIP_IFEL(!X509_print_fp(stdout, cert), -1,
+             "Failed to print x.509v3 in human readable format\n");
+    HIP_DEBUG("x.509v3 certificate in PEM format\n\n");
+    HIP_IFEL((PEM_write_X509(stdout, cert) != 1), -1,
+             "Failed to write the x509 in PEM to stdout\n");
+    /* DEBUG PART END for the certificate */
+#endif
+
+    /** DER **/
+    HIP_IFEL(((der_cert_len = i2d_X509(cert, &der_cert)) < 0), -1,
+             "Failed to convert cert to DER\n");
+    _HIP_HEXDUMP("DER:\n", der_cert, der_cert_len);
+    _HIP_DEBUG("DER length %d\n", der_cert_len);
+    /** end DER **/
+
+    hip_msg_init(msg);
+
+    HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_X509V3_SIGN, 0), -1,
+             "Failed to build user header\n");
+    HIP_IFEL(hip_build_param_cert_x509_resp(msg, (char *) der_cert, 
der_cert_len), -1,
+             "Failed to create x509 response parameter\n");
+    _HIP_DUMP_MSG(msg);
 
 out_err:
-       if(host_id) free(host_id);
-        if(req != NULL) X509_REQ_free(req);
-        if(extlist != NULL) sk_X509_EXTENSION_pop_free (extlist, 
X509_EXTENSION_free);
-        //BIO_flush(out);
-        //BIO_free_all(out);
-
-       return err;
-} 
-
-int verify_callback (int ok, X509_STORE_CTX * stor) {
-       /* This is not called from anywhere else than this file */
-  if (!ok) HIP_DEBUG("Error: %s\n", X509_verify_cert_error_string 
(stor->error));
-  return ok;
+    if (host_id) {
+        free(host_id);
+    }
+    if (req != NULL) {
+        X509_REQ_free(req);
+    }
+    if (extlist != NULL) {
+        sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
+    }
+    //BIO_flush(out);
+    //BIO_free_all(out);
+
+    return err;
+}
+
+int verify_callback(int ok, X509_STORE_CTX *stor)
+{
+    /* This is not called from anywhere else than this file */
+    if (!ok) {
+        HIP_DEBUG("Error: %s\n", X509_verify_cert_error_string(stor->error));
+    }
+    return ok;
 }
 
 /**
@@ -988,74 +1058,78 @@
  *
  * @param msg is a pointer to the requesting msg that contains a cert 
parameter with x509v3 cert
  *
- * @return 0 on success negative otherwise. 
- */ 
-int hip_cert_x509v3_handle_request_to_verify(struct hip_common * msg) {
-        int err = 0;
-        struct hip_cert_x509_resp verify;
-        struct hip_cert_x509_resp * p;
-       X509 *cert = NULL;
-       X509_STORE *store = NULL;
-       X509_STORE_CTX *verify_ctx = NULL;
-        unsigned char *der_cert = NULL;
-       unsigned char **vessel = NULL;
-
-       OpenSSL_add_all_algorithms ();
-       ERR_load_crypto_strings ();
-
-        _HIP_DUMP_MSG(msg);
-        memset(&verify, 0, sizeof(struct hip_cert_x509_resp));
-        HIP_IFEL(!(p = hip_get_param(msg, HIP_PARAM_CERT_X509_REQ)), -1,
-                   "Failed to get cert info from the msg\n");
-        memcpy(&verify, p, sizeof(struct hip_cert_x509_resp));
-      
-        der_cert = (unsigned char *)&p->der;
-
-        _HIP_HEXDUMP("DER:\n", verify.der, verify.der_len);
-        _HIP_DEBUG("DER length %d\n", verify.der_len);
-        
-       vessel = &der_cert;
-        HIP_IFEL(((cert = d2i_X509(NULL, (BROKEN_SSL_CONST unsigned char 
**)vessel ,verify.der_len)) == NULL), -1,
-                 "Failed to convert cert from DER to internal format\n");
-        /*
-       HIP_IFEL(!X509_print_fp(stdout, cert), -1,
-                 "Failed to print x.509v3 in human readable format\n"); 
-        */
-
-       HIP_IFEL(!(store = X509_STORE_new ()), -1,
-               "Failed to create X509_STORE_CTX object\n");
-       X509_STORE_set_verify_cb_func(store, verify_callback);
-
-       /* self signed so te cert itself should verify itself */
-
-       HIP_IFEL(!X509_STORE_add_cert(store, cert), -1,
-                "Failed to add cert to ctx\n");
-       
-       HIP_IFEL((!(verify_ctx = X509_STORE_CTX_new ())), -1,
-               "Failed to create X509_STORE_CTX object\n");
-
-       HIP_IFEL((X509_STORE_CTX_init(verify_ctx, store, cert, NULL) != 1), -1,
-                "Failed to initialize verification context\n");
-
-       if (X509_verify_cert(verify_ctx) != 1) {
-               HIP_DEBUG("Error verifying the certificate\n");
-               err = -1; 
-       } else {
-               HIP_DEBUG("Certificate verified correctly!\n");
-               err = 0;
-       }
-       
-       hip_msg_init(msg);
-       HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_X509V3_VERIFY, err), -1, 
-                 "Failed to build user header\n");
-        HIP_IFEL(hip_build_param_cert_x509_resp(msg, (char *)&der_cert, 
p->der_len), -1, 
-                 "Failed to create x509 response parameter\n");        
-
-        _HIP_DUMP_MSG(msg);    
- out_err:
-       X509_STORE_CTX_cleanup(verify_ctx);
-       if (store) X509_STORE_free(store);
-       if (cert) X509_free(cert);
-        return err;
+ * @return 0 on success negative otherwise.
+ */
+int hip_cert_x509v3_handle_request_to_verify(struct hip_common *msg)
+{
+    int err                    = 0;
+    struct hip_cert_x509_resp verify;
+    struct hip_cert_x509_resp *p;
+    X509 *cert                 = NULL;
+    X509_STORE *store          = NULL;
+    X509_STORE_CTX *verify_ctx = NULL;
+    unsigned char *der_cert    = NULL;
+    unsigned char **vessel     = NULL;
+
+    OpenSSL_add_all_algorithms();
+    ERR_load_crypto_strings();
+
+    _HIP_DUMP_MSG(msg);
+    memset(&verify, 0, sizeof(struct hip_cert_x509_resp));
+    HIP_IFEL(!(p = hip_get_param(msg, HIP_PARAM_CERT_X509_REQ)), -1,
+             "Failed to get cert info from the msg\n");
+    memcpy(&verify, p, sizeof(struct hip_cert_x509_resp));
+
+    der_cert = (unsigned char *) &p->der;
+
+    _HIP_HEXDUMP("DER:\n", verify.der, verify.der_len);
+    _HIP_DEBUG("DER length %d\n", verify.der_len);
+
+    vessel = &der_cert;
+    HIP_IFEL(((cert = d2i_X509(NULL, (BROKEN_SSL_CONST unsigned char **) 
vessel, verify.der_len)) == NULL), -1,
+             "Failed to convert cert from DER to internal format\n");
+    /*
+     * HIP_IFEL(!X509_print_fp(stdout, cert), -1,
+     *       "Failed to print x.509v3 in human readable format\n");
+     */
+
+    HIP_IFEL(!(store = X509_STORE_new()), -1,
+             "Failed to create X509_STORE_CTX object\n");
+    X509_STORE_set_verify_cb_func(store, verify_callback);
+
+    /* self signed so te cert itself should verify itself */
+
+    HIP_IFEL(!X509_STORE_add_cert(store, cert), -1,
+             "Failed to add cert to ctx\n");
+
+    HIP_IFEL((!(verify_ctx = X509_STORE_CTX_new())), -1,
+             "Failed to create X509_STORE_CTX object\n");
+
+    HIP_IFEL((X509_STORE_CTX_init(verify_ctx, store, cert, NULL) != 1), -1,
+             "Failed to initialize verification context\n");
+
+    if (X509_verify_cert(verify_ctx) != 1) {
+        HIP_DEBUG("Error verifying the certificate\n");
+        err = -1;
+    } else {
+        HIP_DEBUG("Certificate verified correctly!\n");
+        err = 0;
+    }
+
+    hip_msg_init(msg);
+    HIP_IFEL(hip_build_user_hdr(msg, SO_HIP_CERT_X509V3_VERIFY, err), -1,
+             "Failed to build user header\n");
+    HIP_IFEL(hip_build_param_cert_x509_resp(msg, (char *) &der_cert, 
p->der_len), -1,
+             "Failed to create x509 response parameter\n");
+
+    _HIP_DUMP_MSG(msg);
+out_err:
+    X509_STORE_CTX_cleanup(verify_ctx);
+    if (store) {
+        X509_STORE_free(store);
+    }
+    if (cert) {
+        X509_free(cert);
+    }
+    return err;
 }
-

=== modified file 'hipd/cert.c.doxyme'
--- hipd/cert.c.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/cert.c.doxyme  2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,74 +45,73 @@
 
 
 /**
- * hip_cert_hostid2dsa 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_cert_hostid2key 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_cert_hostid2rsa 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_cert_spki_sign 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_cert_spki_verify 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_cert_x509v3_handle_request_to_sign 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_cert_x509v3_handle_request_to_verify 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * verify_callback 
+ * hip_cert_hostid2dsa
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_cert_hostid2key
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_cert_hostid2rsa
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_cert_spki_sign
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_cert_spki_verify
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_cert_x509v3_handle_request_to_sign
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_cert_x509v3_handle_request_to_verify
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * verify_callback
  *
  *
  * @param ok
  * @param autogen.sh
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/cert.h'
--- hipd/cert.h 2010-02-09 14:49:27 +0000
+++ hipd/cert.h 2010-02-10 22:32:46 +0000
@@ -56,8 +56,8 @@
 //#define BROKEN_SSL_CONST
 
 #undef SKM_sk_value
-#define SKM_sk_value(type, st,i) \
-        ((type *)(void*)sk_value(st, i))
+#define SKM_sk_value(type, st, i) \
+    ((type *) (void *) sk_value(st, i))
 
 #undef sk_CONF_VALUE_value
 #define sk_CONF_VALUE_value(st, i) SKM_sk_value(CONF_VALUE, (st), (i))

=== modified file 'hipd/cert.h.doxyme'
--- hipd/cert.h.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/cert.h.doxyme  2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/cert.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/close.c'
--- hipd/close.c        2010-02-08 15:18:06 +0000
+++ hipd/close.c        2010-02-10 22:32:46 +0000
@@ -1,5 +1,5 @@
 /* TODO: Doxygen documentation incomplete. Please fix
- **/
+**/
 
 #ifdef HAVE_CONFIG_H
   #include "config.h"
@@ -14,412 +14,421 @@
 static int hip_xmit_close(hip_ha_t *entry, void *opaque)
 {
 #ifdef CONFIG_HIP_PERFORMANCE
-       HIP_DEBUG("Start PERF_CLOSE_SEND, PERF_CLOSE_COMPLETE\n");
-       hip_perf_start_benchmark( perf_set, PERF_CLOSE_SEND );
-       hip_perf_start_benchmark( perf_set, PERF_CLOSE_COMPLETE );
+    HIP_DEBUG("Start PERF_CLOSE_SEND, PERF_CLOSE_COMPLETE\n");
+    hip_perf_start_benchmark( perf_set, PERF_CLOSE_SEND );
+    hip_perf_start_benchmark( perf_set, PERF_CLOSE_COMPLETE );
 #endif
-       int err = 0, mask = 0;
-       hip_hit_t *peer = (hip_hit_t *) opaque;
-       int delete_ha_info = *(int *)(opaque + sizeof(hip_hit_t));
-
-       struct hip_common *close = NULL;
-
-       if (peer)
-               HIP_DEBUG_HIT("Peer HIT to be closed", peer);
-
-       if (peer && !ipv6_addr_any(peer) &&
-           memcmp(&entry->hit_peer, peer, sizeof(hip_hit_t))) {
-               HIP_DEBUG("Peer HIT did not match, ignoring.\n");
-               goto out_err;
-       }
+    int err                  = 0, mask = 0;
+    hip_hit_t *peer          = (hip_hit_t *) opaque;
+    int delete_ha_info       = *(int *) (opaque + sizeof(hip_hit_t));
+
+    struct hip_common *close = NULL;
+
+    if (peer) {
+        HIP_DEBUG_HIT("Peer HIT to be closed", peer);
+    }
+
+    if (peer && !ipv6_addr_any(peer) &&
+        memcmp(&entry->hit_peer, peer, sizeof(hip_hit_t))) {
+        HIP_DEBUG("Peer HIT did not match, ignoring.\n");
+        goto out_err;
+    }
 
 #ifdef CONFIG_HIP_OPPORTUNISTIC
-       /* Check and remove the IP of the peer from the opp non-HIP database */
-       hip_oppipdb_delentry(&(entry->peer_addr));
+    /* Check and remove the IP of the peer from the opp non-HIP database */
+    hip_oppipdb_delentry(&(entry->peer_addr));
 #endif
 
-        if (!(entry->state == HIP_STATE_ESTABLISHED) && delete_ha_info) {
-               HIP_DEBUG("Not sending CLOSE message, invalid hip state "\
-                         "in current host association. State is %s.\n", 
-                         hip_state_str(entry->state));
-               err = hip_del_peer_info_entry(entry);
-               goto out_err;
-       } else if (!(entry->state == HIP_STATE_ESTABLISHED) && !delete_ha_info) 
{
-         HIP_DEBUG("Not sending CLOSE message, invalid hip state "     \
-                   "in current host association. And NOT deleting the mapping. 
State is %s.\n", 
-                   hip_state_str(entry->state));
-         goto out_err;
-       }
-
-       HIP_DEBUG("State is ESTABLISHED in current host association, sending "\
-                 "CLOSE message to peer.\n");
-
-       hip_firewall_set_bex_data(SO_HIP_FW_UPDATE_DB, entry, &entry->hit_our, 
&entry->hit_peer);
-       
-       HIP_IFE(!(close = hip_msg_alloc()), -ENOMEM);
-
-       entry->hadb_misc_func->
-               hip_build_network_hdr(close, HIP_CLOSE, mask, &entry->hit_our,
-                                     &entry->hit_peer);
-
-       /********ECHO (SIGNED) **********/
-
-       get_random_bytes(entry->echo_data, sizeof(entry->echo_data));
-
-       HIP_IFEL(hip_build_param_echo(close, entry->echo_data,
-                                     sizeof(entry->echo_data), 1, 1), -1,
-                "Failed to build echo param.\n");
-
-       /************* HMAC ************/
-       HIP_IFEL(hip_build_param_hmac_contents(close,
-                                              &entry->hip_hmac_out),
-                -1, "Building of HMAC failed.\n");
-       /********** Signature **********/
-       HIP_IFEL(entry->sign(entry->our_priv_key, close), -EINVAL,
-                "Could not create signature.\n");
-
-       HIP_IFEL(entry->hadb_xmit_func->
-                hip_send_pkt(NULL, &entry->peer_addr,
-                             (entry->nat_mode ? hip_get_local_nat_udp_port() : 
0),
-                             entry->peer_udp_port, close, entry, 0),
-                -ECOMM, "Sending CLOSE message failed.\n");
-       
-       entry->state = HIP_STATE_CLOSING;
+    if (!(entry->state == HIP_STATE_ESTABLISHED) && delete_ha_info) {
+        HIP_DEBUG("Not sending CLOSE message, invalid hip state " \
+                  "in current host association. State is %s.\n",
+                  hip_state_str(entry->state));
+        err = hip_del_peer_info_entry(entry);
+        goto out_err;
+    } else if (!(entry->state == HIP_STATE_ESTABLISHED) && !delete_ha_info) {
+        HIP_DEBUG("Not sending CLOSE message, invalid hip state "     \
+                  "in current host association. And NOT deleting the mapping. 
State is %s.\n",
+                  hip_state_str(entry->state));
+        goto out_err;
+    }
+
+    HIP_DEBUG("State is ESTABLISHED in current host association, sending " \
+              "CLOSE message to peer.\n");
+
+    hip_firewall_set_bex_data(SO_HIP_FW_UPDATE_DB,
+                              entry,
+                              &entry->hit_our,
+                              &entry->hit_peer);
+
+    HIP_IFE(!(close = hip_msg_alloc()), -ENOMEM);
+
+    entry->hadb_misc_func->hip_build_network_hdr(close,
+                                                 HIP_CLOSE,
+                                                 mask,
+                                                 &entry->hit_our,
+                                                 &entry->hit_peer);
+
+    /********ECHO (SIGNED) **********/
+
+    get_random_bytes(entry->echo_data, sizeof(entry->echo_data));
+
+    HIP_IFEL(hip_build_param_echo(close, entry->echo_data, 
sizeof(entry->echo_data), 1, 1),
+             -1,
+             "Failed to build echo param.\n");
+
+    /************* HMAC ************/
+    HIP_IFEL(hip_build_param_hmac_contents(close, &entry->hip_hmac_out),
+             -1,
+             "Building of HMAC failed.\n");
+    /********** Signature **********/
+    HIP_IFEL(entry->sign(entry->our_priv_key, close),
+             -EINVAL,
+             "Could not create signature.\n");
+
+    HIP_IFEL(entry->hadb_xmit_func->
+             hip_send_pkt(NULL, &entry->peer_addr,
+                          (entry->nat_mode ? hip_get_local_nat_udp_port() : 0),
+                          entry->peer_udp_port, close, entry, 0),
+             -ECOMM, "Sending CLOSE message failed.\n");
+
+    entry->state = HIP_STATE_CLOSING;
 #ifdef CONFIG_HIP_PERFORMANCE
-       HIP_DEBUG("Stop and write PERF_CLOSE_SEND\n");
-       hip_perf_stop_benchmark( perf_set, PERF_CLOSE_SEND );
-       hip_perf_write_benchmark( perf_set, PERF_CLOSE_SEND );
+    HIP_DEBUG("Stop and write PERF_CLOSE_SEND\n");
+    hip_perf_stop_benchmark( perf_set, PERF_CLOSE_SEND );
+    hip_perf_write_benchmark( perf_set, PERF_CLOSE_SEND );
 #endif
-       
- out_err:
-       if (close)
-               HIP_FREE(close);
-
-       return err;
+
+out_err:
+    if (close) {
+        HIP_FREE(close);
+    }
+
+    return err;
 }
 
-
-int hip_send_close(struct hip_common *msg, 
-                  int delete_ha_info)
+int hip_send_close(struct hip_common *msg,
+                   int delete_ha_info)
 {
-       int err = 0, retry, n;
-       char  * opaque = NULL;
-       hip_hit_t *hit = NULL;
-       struct sockaddr_in6 sock_addr;
-       struct hip_common *msg_to_firewall = NULL;
-
-       HIP_DEBUG("msg=%p\n", msg);
-       
-       HIP_IFEL(!(opaque = (char *)malloc(sizeof(hip_hit_t) + sizeof(int))), 
-                -1, "failed to allocate memory");
-       
-       if(msg)
-               hit = hip_get_param_contents(msg, HIP_PARAM_HIT);
-
-       memset(opaque, 0, sizeof(hip_hit_t) + sizeof(int));
-
-       if(hit)
-               memcpy(opaque, hit, sizeof(hip_hit_t));
-
-       memcpy(opaque + sizeof(hip_hit_t), &delete_ha_info, sizeof(int));
-       
-
-       HIP_IFEL(hip_for_each_ha(&hip_xmit_close, (void *) opaque),
-               -1, "Failed to reset all HAs\n");
-
-       /* send msg to firewall to reset
-        * the db entries there too */
-       msg_to_firewall = hip_msg_alloc();
-       hip_msg_init(msg_to_firewall);
-       HIP_IFE(hip_build_user_hdr(msg_to_firewall,
-                                  SO_HIP_RESET_FIREWALL_DB, 0), -1);
-       bzero(&sock_addr, sizeof(sock_addr));
-       sock_addr.sin6_family = AF_INET6;
-       sock_addr.sin6_port = htons(HIP_FIREWALL_PORT);
-       sock_addr.sin6_addr = in6addr_loopback;
-
-       for(retry = 0; retry < 3; retry++){
-               n = hip_sendto_user(msg_to_firewall, (struct sockaddr 
*)&sock_addr);
-               if(n <= 0){
-                       HIP_ERROR("resetting firewall db failed (round %d)\n",
-                                 retry);
-                       HIP_DEBUG("Sleeping few seconds to wait for fw\n");
-                       sleep(2);
-               }else{
-                       HIP_DEBUG("resetof  firewall db ok (sent %d bytes)\n",
-                                 n);
-                       break;
-               }
-       }
+    int err                            = 0, retry, n;
+    char *opaque                       = NULL;
+    hip_hit_t *hit                     = NULL;
+    struct sockaddr_in6 sock_addr;
+    struct hip_common *msg_to_firewall = NULL;
+
+    HIP_DEBUG("msg=%p\n", msg);
+
+    HIP_IFEL(!(opaque = (char *) malloc(sizeof(hip_hit_t) + sizeof(int))),
+             -1, "failed to allocate memory");
+
+    if (msg) {
+        hit = hip_get_param_contents(msg, HIP_PARAM_HIT);
+    }
+
+    memset(opaque, 0, sizeof(hip_hit_t) + sizeof(int));
+
+    if (hit) {
+        memcpy(opaque, hit, sizeof(hip_hit_t));
+    }
+
+    memcpy(opaque + sizeof(hip_hit_t), &delete_ha_info, sizeof(int));
+
+
+    HIP_IFEL(hip_for_each_ha(&hip_xmit_close, (void *) opaque),
+             -1, "Failed to reset all HAs\n");
+
+    /* send msg to firewall to reset
+     * the db entries there too */
+    msg_to_firewall = hip_msg_alloc();
+    hip_msg_init(msg_to_firewall);
+    HIP_IFE(hip_build_user_hdr(msg_to_firewall,
+                               SO_HIP_RESET_FIREWALL_DB, 0), -1);
+    bzero(&sock_addr, sizeof(sock_addr));
+    sock_addr.sin6_family = AF_INET6;
+    sock_addr.sin6_port   = htons(HIP_FIREWALL_PORT);
+    sock_addr.sin6_addr   = in6addr_loopback;
+
+    for (retry = 0; retry < 3; retry++) {
+        n = hip_sendto_user(msg_to_firewall, (struct sockaddr *) &sock_addr);
+        if (n <= 0) {
+            HIP_ERROR("resetting firewall db failed (round %d)\n",
+                      retry);
+            HIP_DEBUG("Sleeping few seconds to wait for fw\n");
+            sleep(2);
+        } else {
+            HIP_DEBUG("resetof  firewall db ok (sent %d bytes)\n",
+                      n);
+            break;
+        }
+    }
 
 out_err:
-       if (msg_to_firewall)
-               HIP_FREE(msg_to_firewall);
-       if (opaque)
-               HIP_FREE(opaque);
-       return err;
+    if (msg_to_firewall) {
+        HIP_FREE(msg_to_firewall);
+    }
+    if (opaque) {
+        HIP_FREE(opaque);
+    }
+    return err;
 }
 
 int hip_handle_close(struct hip_common *close, hip_ha_t *entry)
 {
 #ifdef CONFIG_HIP_PERFORMANCE
-       HIP_DEBUG("Start PERF_HANDLE_CLOSE\n");
-       hip_perf_start_benchmark( perf_set, PERF_HANDLE_CLOSE );
+    HIP_DEBUG("Start PERF_HANDLE_CLOSE\n");
+    hip_perf_start_benchmark( perf_set, PERF_HANDLE_CLOSE );
 #endif
-       int err = 0, mask = 0;
-       struct hip_common *close_ack = NULL;
-       struct hip_echo_request *request;
-       int echo_len;
-
-       /* verify HMAC */
-        if (entry->is_loopback) {
-               HIP_IFEL(hip_verify_packet_hmac(close, &entry->hip_hmac_out),
-                        -ENOENT, "HMAC validation on close failed.\n");
-        } else {
-               HIP_IFEL(hip_verify_packet_hmac(close, &entry->hip_hmac_in),
-                        -ENOENT, "HMAC validation on close failed.\n");
-       }
-
-       /* verify signature */
-       HIP_IFEL(entry->verify(entry->peer_pub_key, close), -EINVAL,
-                "Verification of close signature failed.\n");
-
-       HIP_IFE(!(close_ack = hip_msg_alloc()), -ENOMEM);
-
-       HIP_IFEL(!(request =
-                  hip_get_param(close, HIP_PARAM_ECHO_REQUEST_SIGN)),
-                -1, "No echo request under signature.\n");
-       echo_len = hip_get_param_contents_len(request);
-
-       entry->hadb_misc_func->hip_build_network_hdr(close_ack, HIP_CLOSE_ACK,
-                             mask, &entry->hit_our,
-                             &entry->hit_peer);
-
-       HIP_IFEL(hip_build_param_echo(close_ack, request + 1,
-                                     echo_len, 1, 0), -1,
-                "Failed to build echo param.\n");
-
-       /************* HMAC ************/
-       HIP_IFEL(hip_build_param_hmac_contents(close_ack,
-                                              &entry->hip_hmac_out),
-                -1, "Building of HMAC failed.\n");
-
-       /********** Signature **********/
-       HIP_IFEL(entry->sign(entry->our_priv_key, close_ack), -EINVAL,
-                "Could not create signature.\n");
-       
-       HIP_IFEL(entry->hadb_xmit_func->
-                hip_send_pkt(NULL, &entry->peer_addr, 
hip_get_local_nat_udp_port(),
-                             entry->peer_udp_port,
-                             close_ack, entry, 0),
-                -ECOMM, "Sending CLOSE ACK message failed.\n");
-       
-       entry->state = HIP_STATE_CLOSED;
-
-       HIP_DEBUG("CLOSED.\n");
+    int err                      = 0, mask = 0;
+    struct hip_common *close_ack = NULL;
+    struct hip_echo_request *request;
+    int echo_len;
+
+    /* verify HMAC */
+    if (entry->is_loopback) {
+        HIP_IFEL(hip_verify_packet_hmac(close, &entry->hip_hmac_out),
+                 -ENOENT, "HMAC validation on close failed.\n");
+    } else {
+        HIP_IFEL(hip_verify_packet_hmac(close, &entry->hip_hmac_in),
+                 -ENOENT, "HMAC validation on close failed.\n");
+    }
+
+    /* verify signature */
+    HIP_IFEL(entry->verify(entry->peer_pub_key, close), -EINVAL,
+             "Verification of close signature failed.\n");
+
+    HIP_IFE(!(close_ack = hip_msg_alloc()), -ENOMEM);
+
+    HIP_IFEL(!(request =
+                   hip_get_param(close, HIP_PARAM_ECHO_REQUEST_SIGN)),
+             -1, "No echo request under signature.\n");
+    echo_len = hip_get_param_contents_len(request);
+
+    entry->hadb_misc_func->hip_build_network_hdr(close_ack, HIP_CLOSE_ACK,
+                                                 mask, &entry->hit_our,
+                                                 &entry->hit_peer);
+
+    HIP_IFEL(hip_build_param_echo(close_ack, request + 1,
+                                  echo_len, 1, 0), -1,
+             "Failed to build echo param.\n");
+
+    /************* HMAC ************/
+    HIP_IFEL(hip_build_param_hmac_contents(close_ack,
+                                           &entry->hip_hmac_out),
+             -1, "Building of HMAC failed.\n");
+
+    /********** Signature **********/
+    HIP_IFEL(entry->sign(entry->our_priv_key, close_ack), -EINVAL,
+             "Could not create signature.\n");
+
+    HIP_IFEL(entry->hadb_xmit_func->
+             hip_send_pkt(NULL, &entry->peer_addr, 
hip_get_local_nat_udp_port(),
+                          entry->peer_udp_port,
+                          close_ack, entry, 0),
+             -ECOMM, "Sending CLOSE ACK message failed.\n");
+
+    entry->state = HIP_STATE_CLOSED;
+
+    HIP_DEBUG("CLOSED.\n");
 
 /* If this host has a relay hashtable, i.e. the host is a HIP UDP relay or RVS,
-   then we need to delete the relay record matching the sender's HIT. */
+ * then we need to delete the relay record matching the sender's HIT. */
 #ifdef CONFIG_HIP_RVS
-       if(hip_relay_get_status())
-       {
-            hip_relrec_t dummy;
-            memcpy(&(dummy.hit_r), &(close->hits),
-                   sizeof(close->hits));
-            hip_relht_rec_free_doall(&dummy);
-            /* Check that the element really got deleted. */
-            if(hip_relht_get(&dummy) == NULL)
-            {
-                 HIP_DEBUG_HIT("Deleted relay record for HIT",
-                               &(close->hits));
-            }
-       }
+    if (hip_relay_get_status()) {
+        hip_relrec_t dummy;
+        memcpy(&(dummy.hit_r), &(close->hits),
+               sizeof(close->hits));
+        hip_relht_rec_free_doall(&dummy);
+        /* Check that the element really got deleted. */
+        if (hip_relht_get(&dummy) == NULL) {
+            HIP_DEBUG_HIT("Deleted relay record for HIT",
+                          &(close->hits));
+        }
+    }
 #endif
-       
-       HIP_IFEL(hip_del_peer_info(&entry->hit_our, &entry->hit_peer), -1,
-                                  "Deleting peer info failed.\n");
+
+    HIP_IFEL(hip_del_peer_info(&entry->hit_our, &entry->hit_peer), -1,
+             "Deleting peer info failed.\n");
 #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 );
+    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
- out_err:
-
-       if (close_ack)
-               HIP_FREE(close_ack);
-
-       return err;
+out_err:
+
+    if (close_ack) {
+        HIP_FREE(close_ack);
+    }
+
+    return err;
 }
 
 int hip_receive_close(struct hip_common *close,
-                     hip_ha_t          *entry) 
+                      hip_ha_t          *entry)
 {
-       int state = 0;
-       int err = 0;
-       uint16_t mask = HIP_PACKET_CTRL_ANON;
-
-       /* XX FIX: CHECK THE SIGNATURE */
-
-       HIP_DEBUG("\n");
-       HIP_IFEL(ipv6_addr_any(&close->hitr), -1,
-                "Received NULL receiver HIT in CLOSE. Dropping\n");
-
-       if (!hip_controls_sane(ntohs(close->control), mask)) {
-               HIP_ERROR("Received illegal controls in CLOSE: 0x%x. 
Dropping\n",
-                         ntohs(close->control));
-               goto out_err;
-       }
-
-       if (!entry) {
-               HIP_DEBUG("No HA for the received close\n");
-               goto out_err;
-       } else {
-               HIP_LOCK_HA(entry);
-               state = entry->state;
-       }
-
-       switch(state) {
-       case HIP_STATE_ESTABLISHED:
-       case HIP_STATE_CLOSING:
-               err = entry->hadb_handle_func->hip_handle_close(close, entry);
-               break;
-       default:
-               HIP_ERROR("Internal state (%d) is incorrect\n", state);
-               break;
-       }
-
-       if (entry) {
-               /* XX CHECK: is the put done twice? once already in handle? */
-               HIP_UNLOCK_HA(entry);
-               /* hip_put_ha(entry); */
-       }
- out_err:
-       return err;
+    int state     = 0;
+    int err       = 0;
+    uint16_t mask = HIP_PACKET_CTRL_ANON;
+
+    /* XX FIX: CHECK THE SIGNATURE */
+
+    HIP_DEBUG("\n");
+    HIP_IFEL(ipv6_addr_any(&close->hitr), -1,
+             "Received NULL receiver HIT in CLOSE. Dropping\n");
+
+    if (!hip_controls_sane(ntohs(close->control), mask)) {
+        HIP_ERROR("Received illegal controls in CLOSE: 0x%x. Dropping\n",
+                  ntohs(close->control));
+        goto out_err;
+    }
+
+    if (!entry) {
+        HIP_DEBUG("No HA for the received close\n");
+        goto out_err;
+    } else {
+        HIP_LOCK_HA(entry);
+        state = entry->state;
+    }
+
+    switch (state) {
+    case HIP_STATE_ESTABLISHED:
+    case HIP_STATE_CLOSING:
+        err = entry->hadb_handle_func->hip_handle_close(close, entry);
+        break;
+    default:
+        HIP_ERROR("Internal state (%d) is incorrect\n", state);
+        break;
+    }
+
+    if (entry) {
+        /* XX CHECK: is the put done twice? once already in handle? */
+        HIP_UNLOCK_HA(entry);
+        /* hip_put_ha(entry); */
+    }
+out_err:
+    return err;
 }
 
 int hip_handle_close_ack(struct hip_common *close_ack, hip_ha_t *entry)
 {
 #ifdef CONFIG_HIP_PERFORMANCE
-       HIP_DEBUG("Start PERF_HANDLE_CLOSE_ACK\n");
-       hip_perf_start_benchmark( perf_set, PERF_HANDLE_CLOSE_ACK );
+    HIP_DEBUG("Start PERF_HANDLE_CLOSE_ACK\n");
+    hip_perf_start_benchmark( perf_set, PERF_HANDLE_CLOSE_ACK );
 #endif
-       int err = 0;
-       struct hip_echo_request *echo_resp;
-
-       /* verify ECHO */
-       HIP_IFEL(!(echo_resp =
-                  hip_get_param(close_ack, HIP_PARAM_ECHO_RESPONSE_SIGN)),
-                -1, "Echo response not found\n");
-       HIP_IFEL(memcmp(echo_resp + 1, entry->echo_data,
-                       sizeof(entry->echo_data)), -1,
-                "Echo response did not match request\n");
-
-       /* verify HMAC */
-        if (entry->is_loopback) {
-               HIP_IFEL(hip_verify_packet_hmac(close_ack,
-                                               &entry->hip_hmac_out),
-                        -ENOENT, "HMAC validation on close ack failed\n");
-       } else {
-               HIP_IFEL(hip_verify_packet_hmac(close_ack,
-                                               &entry->hip_hmac_in),
-                        -ENOENT, "HMAC validation on close ack failed\n");
-       }
-       /* verify signature */
-       HIP_IFEL(entry->verify(entry->peer_pub_key, close_ack), -EINVAL,
-                "Verification of close ack signature failed\n");
-
-       entry->state = HIP_STATE_CLOSED;
-
-       HIP_DEBUG("CLOSED\n");
+    int err = 0;
+    struct hip_echo_request *echo_resp;
+
+    /* verify ECHO */
+    HIP_IFEL(!(echo_resp =
+                   hip_get_param(close_ack, HIP_PARAM_ECHO_RESPONSE_SIGN)),
+             -1, "Echo response not found\n");
+    HIP_IFEL(memcmp(echo_resp + 1, entry->echo_data,
+                    sizeof(entry->echo_data)), -1,
+             "Echo response did not match request\n");
+
+    /* verify HMAC */
+    if (entry->is_loopback) {
+        HIP_IFEL(hip_verify_packet_hmac(close_ack,
+                                        &entry->hip_hmac_out),
+                 -ENOENT, "HMAC validation on close ack failed\n");
+    } else {
+        HIP_IFEL(hip_verify_packet_hmac(close_ack,
+                                        &entry->hip_hmac_in),
+                 -ENOENT, "HMAC validation on close ack failed\n");
+    }
+    /* verify signature */
+    HIP_IFEL(entry->verify(entry->peer_pub_key, close_ack), -EINVAL,
+             "Verification of close ack signature failed\n");
+
+    entry->state = HIP_STATE_CLOSED;
+
+    HIP_DEBUG("CLOSED\n");
 
 #ifdef CONFIG_HIP_OPPORTUNISTIC
-       /* Check and remove the IP of the peer from the opp non-HIP database */
-       hip_oppipdb_delentry(&(entry->peer_addr));
+    /* Check and remove the IP of the peer from the opp non-HIP database */
+    hip_oppipdb_delentry(&(entry->peer_addr));
 #endif
 
-       HIP_IFEL(hip_del_peer_info(&entry->hit_our, &entry->hit_peer), -1,
-                "Deleting peer info failed\n");
-       
-       //hip_hadb_remove_state(entry);
-       //hip_delete_esp(entry);
-
-       /* by now, if everything is according to plans, the refcnt should
-          be 1 */
-       /* hip_put_ha(entry); */
+    HIP_IFEL(hip_del_peer_info(&entry->hit_our, &entry->hit_peer), -1,
+             "Deleting peer info failed\n");
+
+    //hip_hadb_remove_state(entry);
+    //hip_delete_esp(entry);
+
+    /* by now, if everything is according to plans, the refcnt should
+     * be 1 */
+    /* hip_put_ha(entry); */
 
 #ifdef CONFIG_HIP_PERFORMANCE
-       HIP_DEBUG("Stop and write PERF_HANDLE_CLOSE_ACK, 
PERF_CLOSE_COMPLETE\n");
-       hip_perf_stop_benchmark( perf_set, PERF_HANDLE_CLOSE_ACK );
-       hip_perf_write_benchmark( perf_set, PERF_HANDLE_CLOSE_ACK );
-       hip_perf_stop_benchmark( perf_set, PERF_CLOSE_COMPLETE );
-       hip_perf_write_benchmark( perf_set, PERF_CLOSE_COMPLETE );
+    HIP_DEBUG("Stop and write PERF_HANDLE_CLOSE_ACK, PERF_CLOSE_COMPLETE\n");
+    hip_perf_stop_benchmark( perf_set, PERF_HANDLE_CLOSE_ACK );
+    hip_perf_write_benchmark( perf_set, PERF_HANDLE_CLOSE_ACK );
+    hip_perf_stop_benchmark( perf_set, PERF_CLOSE_COMPLETE );
+    hip_perf_write_benchmark( perf_set, PERF_CLOSE_COMPLETE );
 #endif
 
- out_err:
+out_err:
 
-       return err;
+    return err;
 }
 
-
 int hip_receive_close_ack(struct hip_common *close_ack,
-                         hip_ha_t *entry) 
+                          hip_ha_t *entry)
 {
-       int state = 0;
-       int err = 0;
-       uint16_t mask = HIP_PACKET_CTRL_ANON;
-
-       /* XX FIX:  */
-
-       HIP_DEBUG("\n");
-
-       HIP_IFEL(ipv6_addr_any(&close_ack->hitr), -1,
-                "Received NULL receiver HIT in CLOSE ACK. Dropping\n");
-
-       if (!hip_controls_sane(ntohs(close_ack->control), mask
-                      //HIP_CONTROL_CERTIFICATES | HIP_PACKET_CTRL_ANON |
-                      // | HIP_CONTROL_SHT_MASK | HIP_CONTROL_DHT_MASK)) {
-                              )) {
-               HIP_ERROR("Received illegal controls in CLOSE ACK: 0x%x. 
Dropping\n",
-                         ntohs(close_ack->control));
-               goto out_err;
-       }
-       
-       if (!entry) {
-               HIP_DEBUG("No HA for the received close ack\n");
-               goto out_err;
-       } else {
-               HIP_LOCK_HA(entry);
-               state = entry->state;
-       }
-
-       switch(state) {
-       case HIP_STATE_CLOSING:
-       case HIP_STATE_CLOSED:
-               err = entry->hadb_handle_func->hip_handle_close_ack(close_ack, 
entry);
-               break;
-       default:
-               HIP_ERROR("Internal state (%d) is incorrect\n", state);
-               break;
-       }
-
- out_err:
-       return err;
+    int state     = 0;
+    int err       = 0;
+    uint16_t mask = HIP_PACKET_CTRL_ANON;
+
+    /* XX FIX:  */
+
+    HIP_DEBUG("\n");
+
+    HIP_IFEL(ipv6_addr_any(&close_ack->hitr), -1,
+             "Received NULL receiver HIT in CLOSE ACK. Dropping\n");
+
+    if (!hip_controls_sane(ntohs(close_ack->control), mask
+                           //HIP_CONTROL_CERTIFICATES | HIP_PACKET_CTRL_ANON |
+                           // | HIP_CONTROL_SHT_MASK | HIP_CONTROL_DHT_MASK)) {
+                           )) {
+        HIP_ERROR("Received illegal controls in CLOSE ACK: 0x%x. Dropping\n",
+                  ntohs(close_ack->control));
+        goto out_err;
+    }
+
+    if (!entry) {
+        HIP_DEBUG("No HA for the received close ack\n");
+        goto out_err;
+    } else {
+        HIP_LOCK_HA(entry);
+        state = entry->state;
+    }
+
+    switch (state) {
+    case HIP_STATE_CLOSING:
+    case HIP_STATE_CLOSED:
+        err = entry->hadb_handle_func->hip_handle_close_ack(close_ack, entry);
+        break;
+    default:
+        HIP_ERROR("Internal state (%d) is incorrect\n", state);
+        break;
+    }
+
+out_err:
+    return err;
 }
 
 int hip_purge_closing_ha(hip_ha_t *ha, void *notused)
 {
-       int err = 0;
-
-       if ((ha->state == HIP_STATE_CLOSING || ha->state == HIP_STATE_CLOSED)) {
-               if (ha->purge_timeout <= 0) {
-                       HIP_DEBUG("Purging HA (state=%d)\n", ha->state);
-                       HIP_IFEL(hip_del_peer_info(&ha->hit_our, 
&ha->hit_peer), -1,
-                                "Deleting peer info failed.\n");
-               } else {
-                       ha->purge_timeout--;
-               }
-       }
-
- out_err:
-       return err;
+    int err = 0;
+
+    if ((ha->state == HIP_STATE_CLOSING || ha->state == HIP_STATE_CLOSED)) {
+        if (ha->purge_timeout <= 0) {
+            HIP_DEBUG("Purging HA (state=%d)\n", ha->state);
+            HIP_IFEL(hip_del_peer_info(&ha->hit_our, &ha->hit_peer), -1,
+                     "Deleting peer info failed.\n");
+        } else {
+            ha->purge_timeout--;
+        }
+    }
+
+out_err:
+    return err;
 }

=== modified file 'hipd/close.c.doxyme'
--- hipd/close.c.doxyme 2009-12-12 10:44:54 +0000
+++ hipd/close.c.doxyme 2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,61 +45,60 @@
 
 
 /**
- * hip_handle_close 
+ * hip_handle_close
  *
  *
  * @param close
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_handle_close_ack 
+ * hip_handle_close_ack
  *
  *
  * @param close_ack
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_purge_closing_ha 
+ * hip_purge_closing_ha
  *
  *
  * @param ha
  * @param notused
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_receive_close 
+ * hip_receive_close
  *
  *
  * @param close
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_receive_close_ack 
+ * hip_receive_close_ack
  *
  *
  * @param close_ack
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_send_close 
+ * hip_send_close
  *
  *
  * @param msg
  * @param delete_ha_info
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/close.h.doxyme'
--- hipd/close.h.doxyme 2009-12-12 10:44:54 +0000
+++ hipd/close.h.doxyme 2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/close.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/configfilereader.c'
--- hipd/configfilereader.c     2009-12-29 15:00:23 +0000
+++ hipd/configfilereader.c     2010-02-10 22:32:46 +0000
@@ -1,6 +1,6 @@
 /** @file
  * This file defines an all-round configurationfilereader.
- * 
+ *
  * @author  Lauri Silvennoinen
  * @note    Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
  * @see     configfilereader.h for usage instructions.
@@ -8,13 +8,13 @@
 #include "configfilereader.h"
 
 /** Error value for generic config file error. (Everything but -EIO and EOF are
-    acceptable here.) */
+ *  acceptable here.) */
 #define HIP_EINPUT             -EIO
 /** Error value for generic config file error. (Everything but -EIO and EOF are
-    acceptable here.) */
+ *  acceptable here.) */
 #define HIP_EVAL               -10
 /** Error value for too long config file line. (Everything but -EIO and EOF are
-    acceptable here.) */
+ *  acceptable here.) */
 #define HIP_ELONG              -11
 
 /** HIP relay config file parameter separator as a char. */
@@ -26,21 +26,24 @@
  * Checks whether the parameter line is commented. A commented line has
  * @c HIP_RELAY_COMMENT character as the first non-blank character on the line.
  *
- * @param  line the line to check. 
+ * @param  line the line to check.
  * @return 1 if the line is a comment, zero otherwise.
- */ 
+ */
 static int hip_cf_is_comment(char *line)
 {
-       int i = 0;
-
-       /* Clean leading white space. */
-       while(isspace(line[i]) && i <  HIP_RELAY_MAX_LINE_LEN) { i++; }
-
-       /* The firts non-blank character is a comment mark. */
-       if(line[i] == HIP_RELAY_COMMENT)
-               return 1;
-
-       return 0;
+    int i = 0;
+
+    /* Clean leading white space. */
+    while (isspace(line[i]) && i <  HIP_RELAY_MAX_LINE_LEN) {
+        i++;
+    }
+
+    /* The firts non-blank character is a comment mark. */
+    if (line[i] == HIP_RELAY_COMMENT) {
+        return 1;
+    }
+
+    return 0;
 }
 
 /**
@@ -48,52 +51,52 @@
  * parameter @c linkedlist.
  *
  * @param  linkedlist the list where to add the new element.
- * @param  data       the data that is stored to the new element. 
+ * @param  data       the data that is stored to the new element.
  * @return            zero on success, HIP_EVAL if @c linkedlist or @c data is
  *                    NULL or if there was an error when allocating memory to
  *                    the new element.
- */ 
+ */
 static int hip_cvl_add(hip_configvaluelist_t *linkedlist, const void *data)
 {
-       if (linkedlist == NULL || data == NULL)
-               return HIP_EVAL;
-
-       hip_configfilevalue_t *newnode =
-               (hip_configfilevalue_t*) malloc(sizeof(hip_configfilevalue_t));
-       
-       if(newnode == NULL) {
-               HIP_ERROR("Error on allocating memory for a linked list 
node.\n");
-               return HIP_EVAL;
-       }
-       
-       memcpy(newnode->data, data, sizeof(newnode->data));
-       newnode->next = NULL;
-
-       /* Item to add is the first item of the list. */
-       if(linkedlist->head == NULL) {
-               linkedlist->head = newnode;
-               return 0;
-       } else {
-               hip_configfilevalue_t *pointer = linkedlist->head;
-               
-               while(pointer->next != NULL) {
-                       pointer = pointer->next;
-               }
-
-               pointer->next = newnode;
-               
-               return 0;
-       }
-
-       return 0;
+    if (linkedlist == NULL || data == NULL) {
+        return HIP_EVAL;
+    }
+
+    hip_configfilevalue_t *newnode =
+        (hip_configfilevalue_t *) malloc(sizeof(hip_configfilevalue_t));
+
+    if (newnode == NULL) {
+        HIP_ERROR("Error on allocating memory for a linked list node.\n");
+        return HIP_EVAL;
+    }
+
+    memcpy(newnode->data, data, sizeof(newnode->data));
+    newnode->next = NULL;
+
+    /* Item to add is the first item of the list. */
+    if (linkedlist->head == NULL) {
+        linkedlist->head = newnode;
+        return 0;
+    } else {
+        hip_configfilevalue_t *pointer = linkedlist->head;
+
+        while (pointer->next != NULL) {
+            pointer = pointer->next;
+        }
+
+        pointer->next = newnode;
+
+        return 0;
+    }
+
+    return 0;
 }
 
-
 /**
  * Parses values from a line. Parses values from the parameter @c line and
  * stores the values into the linked list of @c values. The target buffer @c
  * values must be initialized with hip_cvl_init() before calling this function.
- * 
+ *
  * Values are in the string on the righthandside of @c HIP_RELAY_PAR_SEP.
  * Values may contain spaces and they must be at most @c HIP_RELAY_MAX_VAL_LEN
  * characters long each. The parameters must be separated with
@@ -109,124 +112,134 @@
  */
 static int hip_cf_parse_val(char *line, hip_configvaluelist_t *values)
 {
-       int i = 0, j = 0, k = 0, l = 0, end = 0;
-       char value[HIP_RELAY_MAX_VAL_LEN + 1];
-
-       /* Search for the line end. */
-       while(line[end] != '\0' && end < HIP_RELAY_MAX_LINE_LEN) { end++; }
-       
-       /* Check that the line ends with HIP_RELAY_VAL_CON. */
-       i = end -1;
-       if(i < 0) { i = 0; }
-       while(isspace(line[i]) && i >= 0) { i--; }
-       
-       if(line[i] != HIP_RELAY_VAL_CON) { return HIP_EVAL; }
-
-       i = 0;
-
-       /* Search for the parameter separator. */
-       while(line[i] != HIP_RELAY_PAR_SEP && i < end) { i++; }
-       
-       /* If found, we can start parsing the values. */
-       if(line[i] == HIP_RELAY_PAR_SEP) {
-               /* Move to the next character after parameter separator. */
-               i++;
-               j = i;
-
-               /* Read the input until the end is reached. */
-               do{
-                       /* Search for the value separator or the end of line */
-                       while(line[j] != HIP_RELAY_VAL_SEP && j < end) { j++; }
-
-                       /* Search for the leading value container mark. */
-                       while(line[i] != HIP_RELAY_VAL_CON && i < j) {
-                               /* Check for trash before the leading container
-                                  mark or if there is no more room for the
-                                  actual value. */
-                               if(!isspace(line[i]) || (j - i) < 2){
-                                       return HIP_EVAL;
-                               }
-                               i++;
-                       }
-                       
-                       /* A special case of illegal input where there are
-                          multiple HIP_RELAY_VAL_SEP characters between the
-                          values and no space between these HIP_RELAY_VAL_SEP
-                          characters. */
-                       if(line[i] == HIP_RELAY_VAL_SEP &&
-                          line[j] == HIP_RELAY_VAL_SEP) {
-                               return HIP_EVAL;
-                       }
-                       
-                       /* If found, we now have the value including the value
-                          container marks between i (inclusive) and j
-                          (exclusive). I.e. we have:
-                          "value"   , 
-                          ^         ^
-                          i         j (or alternatively j can be at "end"). */
-
-                       if(line[i] == HIP_RELAY_VAL_CON &&
-                          (line[j] == HIP_RELAY_VAL_SEP || j == end)) {
-                               
-                               i++;
-                               memset(value, '\0', sizeof(value));
-                               k = i;
-                               l = 0;
-                       
-                               /* Search for the trailing value container. */
-                               while(line[k] != HIP_RELAY_VAL_CON && k < j) {
-                                       value[l] = line[k];
-                                       k++; 
-                                       l++;
-                                       if(l >= HIP_RELAY_MAX_VAL_LEN){
-                                               /* Too long value. */
-                                               return HIP_EVAL;
-                                       }
-                               }
-                               
-                               /* Check for trash after the trailing container
-                                  mark. */
-                               l = k;
-                               l++;
-                               while(l < j) {
-                                       if(!isspace(line[l])) {
-                                               return HIP_EVAL;
-                                       }
-                                       l++; 
-                               }
-                               
-                               /* If no trash is found and the trailing
-                                  container mark is found, we have succesfully
-                                  read one value. */
-                               if(line[k] == HIP_RELAY_VAL_CON) {
-                                       hip_cvl_add(values, value);
-                               } else {
-                                       /* Closing value container was not
-                                          found. */
-                                       return HIP_EVAL;
-                               }
-                       }
-                       /* We have now read one value between the value
-                          separators (or parameter and value separator), let's
-                          move to next character. */
-                       j++;
-                       i = j;
-               } while(j < end);
-               /* All values were read succesfully. */
-               return 0;
-       }
-       
-       /* We didn't find the parameter separator at all. */
-       return HIP_EVAL;
+    int i = 0, j = 0, k = 0, l = 0, end = 0;
+    char value[HIP_RELAY_MAX_VAL_LEN + 1];
+
+    /* Search for the line end. */
+    while (line[end] != '\0' && end < HIP_RELAY_MAX_LINE_LEN) {
+        end++;
+    }
+
+    /* Check that the line ends with HIP_RELAY_VAL_CON. */
+    i = end - 1;
+    if (i < 0) {
+        i = 0;
+    }
+    while (isspace(line[i]) && i >= 0) {
+        i--;
+    }
+
+    if (line[i] != HIP_RELAY_VAL_CON) {
+        return HIP_EVAL;
+    }
+
+    i = 0;
+
+    /* Search for the parameter separator. */
+    while (line[i] != HIP_RELAY_PAR_SEP && i < end) {
+        i++;
+    }
+
+    /* If found, we can start parsing the values. */
+    if (line[i] == HIP_RELAY_PAR_SEP) {
+        /* Move to the next character after parameter separator. */
+        i++;
+        j = i;
+
+        /* Read the input until the end is reached. */
+        do {
+            /* Search for the value separator or the end of line */
+            while (line[j] != HIP_RELAY_VAL_SEP && j < end) {
+                j++;
+            }
+
+            /* Search for the leading value container mark. */
+            while (line[i] != HIP_RELAY_VAL_CON && i < j) {
+                /* Check for trash before the leading container
+                 * mark or if there is no more room for the
+                 * actual value. */
+                if (!isspace(line[i]) || (j - i) < 2) {
+                    return HIP_EVAL;
+                }
+                i++;
+            }
+
+            /* A special case of illegal input where there are
+             * multiple HIP_RELAY_VAL_SEP characters between the
+             * values and no space between these HIP_RELAY_VAL_SEP
+             * characters. */
+            if (line[i] == HIP_RELAY_VAL_SEP &&
+                line[j] == HIP_RELAY_VAL_SEP) {
+                return HIP_EVAL;
+            }
+
+            /* If found, we now have the value including the value
+             * container marks between i (inclusive) and j
+             * (exclusive). I.e. we have:
+             * "value"   ,
+             * ^         ^
+             * i         j (or alternatively j can be at "end"). */
+
+            if (line[i] == HIP_RELAY_VAL_CON &&
+                (line[j] == HIP_RELAY_VAL_SEP || j == end)) {
+                i++;
+                memset(value, '\0', sizeof(value));
+                k = i;
+                l = 0;
+
+                /* Search for the trailing value container. */
+                while (line[k] != HIP_RELAY_VAL_CON && k < j) {
+                    value[l] = line[k];
+                    k++;
+                    l++;
+                    if (l >= HIP_RELAY_MAX_VAL_LEN) {
+                        /* Too long value. */
+                        return HIP_EVAL;
+                    }
+                }
+
+                /* Check for trash after the trailing container
+                 * mark. */
+                l = k;
+                l++;
+                while (l < j) {
+                    if (!isspace(line[l])) {
+                        return HIP_EVAL;
+                    }
+                    l++;
+                }
+
+                /* If no trash is found and the trailing
+                 * container mark is found, we have succesfully
+                 * read one value. */
+                if (line[k] == HIP_RELAY_VAL_CON) {
+                    hip_cvl_add(values, value);
+                } else {
+                    /* Closing value container was not
+                     * found. */
+                    return HIP_EVAL;
+                }
+            }
+            /* We have now read one value between the value
+             * separators (or parameter and value separator), let's
+             * move to next character. */
+            j++;
+            i = j;
+        } while (j < end);
+        /* All values were read succesfully. */
+        return 0;
+    }
+
+    /* We didn't find the parameter separator at all. */
+    return HIP_EVAL;
 }
 
-
 /**
  * Parses parameter from a line. Parses parameter from the parameter @c line 
and
  * stores the value into @c parameter. The target buffer @c parameter is not
  * terminated with '\0' thus it should be initialized with '\0' before calling
  * this function. The source buffer must terminate to '\0'.
- * 
+ *
  * Parameter is the string on the lefthandside of @c HIP_RELAY_PAR_SEP. The
  * parameter may <b>NOT</b> contain spaces and it can be at most
  * @c HIP_RELAY_MAX_PAR_LEN characters long.
@@ -236,51 +249,56 @@
  * @return           zero on success, HIP_EVAL otherwise.
  * @note             This function is not meant to be called outside this file.
  *                   Use hip_cf_get_line_data() to get data from lines.
- */ 
+ */
 static int hip_cf_parse_par(char *line, char *parameter)
-{      
-       int i = 0, j = 0, k = 0, l = 0;
-       
-       /* Search for the parameter separator */
-       while(line[i] != HIP_RELAY_PAR_SEP && line[i] != '\0' &&
-             i < HIP_RELAY_MAX_LINE_LEN) { i++; }
-
-       /* If found, parse parameter. */
-       if(line[i] == HIP_RELAY_PAR_SEP) {
-               
-               /* Clean leading white space. */
-               while(isspace(line[j]) && j < i) { j++; }
-               
-               l = j;
-
-               /* Copy characters until space is encountered. */
-               while(!isspace(line[j]) && j < i &&
-                     (j - l) <= (HIP_RELAY_MAX_PAR_LEN + 1)) {
-                       parameter[k] = line[j];
-                       j++;
-                       k++;
-               }
-
-               if(j - l > HIP_RELAY_MAX_PAR_LEN) {
-                       /* Parameter maximum length exceeded. */
-                       return HIP_EVAL;
-               }
-               
-               /* Clean the trailing white space. */
-               while(isspace(line[j]) && j <= i) { j++; }
-               
-               /* Check if there was trash between the parameter and the
-                  parameter separator. */
-               if(j != i) {
-                       return HIP_EVAL;
-               }
-               
-               /* Parameter read succesfully. */
-               return 0;
-       }
-
-       /* No parameter separator found. */
-       return HIP_EVAL;
+{
+    int i = 0, j = 0, k = 0, l = 0;
+
+    /* Search for the parameter separator */
+    while (line[i] != HIP_RELAY_PAR_SEP && line[i] != '\0' &&
+           i < HIP_RELAY_MAX_LINE_LEN) {
+        i++;
+    }
+
+    /* If found, parse parameter. */
+    if (line[i] == HIP_RELAY_PAR_SEP) {
+        /* Clean leading white space. */
+        while (isspace(line[j]) && j < i) {
+            j++;
+        }
+
+        l = j;
+
+        /* Copy characters until space is encountered. */
+        while (!isspace(line[j]) && j < i &&
+               (j - l) <= (HIP_RELAY_MAX_PAR_LEN + 1)) {
+            parameter[k] = line[j];
+            j++;
+            k++;
+        }
+
+        if (j - l > HIP_RELAY_MAX_PAR_LEN) {
+            /* Parameter maximum length exceeded. */
+            return HIP_EVAL;
+        }
+
+        /* Clean the trailing white space. */
+        while (isspace(line[j]) && j <= i) {
+            j++;
+        }
+
+        /* Check if there was trash between the parameter and the
+         * parameter separator. */
+        if (j != i) {
+            return HIP_EVAL;
+        }
+
+        /* Parameter read succesfully. */
+        return 0;
+    }
+
+    /* No parameter separator found. */
+    return HIP_EVAL;
 }
 
 /**
@@ -294,52 +312,56 @@
  * positive. The return values does <b>not</b> indicate whether there was an
  * error or not. One should use the value of @c parseerr to check if the line 
was
  * read succesfully.
- * 
+ *
  * @param  fp       a pointer to the source file.
  * @param  buf      a target buffer where to put the line.
  * @param  parseerr zero on success, HIP_EINPUT if an illegal control character
  *                  was encountered, HIP_ELONG if maximum number of characters
- *                  per line (@c HIP_RELAY_MAX_LINE_LEN) was exceed.           
   
+ *                  per line (@c HIP_RELAY_MAX_LINE_LEN) was exceed.
  * @return          EOF if the end of file was reached or if @c fp or @c buf is
  *                  NULL, otherwise the number of succesfully read characters.
  * @note            This function is not meant to be called outside this file.
  *                  Use hip_cf_get_line_data() to get data from lines.
- */ 
+ */
 static int hip_cf_readline(FILE *fp, char *buf, int *parseerr)
 {
-       int ch = 0, i = 0;
-       *parseerr = 0;
-
-       /* Read characters from the file until EOF or a control character is
-          confronted, or a maximum line length is exceed. */
-       while((ch = fgetc(fp)) != EOF && iscntrl(ch) == 0 &&
-             i < HIP_RELAY_MAX_LINE_LEN) {
-               buf[i] = ch;
-               i++;
-       }
-       
-       if(ch == EOF) {
-               return EOF;
-       } else if(iscntrl(ch) != 0 && ch != '\n') {
-               /*printf("Error on reading input. Control character "\
-                 "encountered.\n");*/
-               *parseerr = HIP_EINPUT; 
-               /* If an illegal control character is confronted, we omit the
-                  rest of the line. */
-               while((ch = fgetc(fp)) != EOF && ch != '\n') {}
-               if(ch == EOF)
-                       return EOF;
-       } else if (i >= HIP_RELAY_MAX_LINE_LEN) {
-               /*printf("Error on reading input. Maximum line length (%d "\
-                 "characters) exceeded.\n", HIP_RELAY_MAX_LINE_LEN);*/
-               *parseerr = HIP_ELONG;
-               /* If maximum line length is exceeded, we omit the rest of the
-                  line. */
-               while((ch = fgetc(fp)) != EOF && ch != '\n') {}
-               if(ch == EOF)
-                       return EOF;
-       } 
-       return i;
+    int ch = 0, i = 0;
+    *parseerr = 0;
+
+    /* Read characters from the file until EOF or a control character is
+     * confronted, or a maximum line length is exceed. */
+    while ((ch = fgetc(fp)) != EOF && iscntrl(ch) == 0 &&
+           i < HIP_RELAY_MAX_LINE_LEN) {
+        buf[i] = ch;
+        i++;
+    }
+
+    if (ch == EOF) {
+        return EOF;
+    } else if (iscntrl(ch) != 0 && ch != '\n') {
+        /*printf("Error on reading input. Control character "\
+         * "encountered.\n");*/
+        *parseerr = HIP_EINPUT;
+        /* If an illegal control character is confronted, we omit the
+         * rest of the line. */
+        while ((ch = fgetc(fp)) != EOF && ch != '\n') {
+        }
+        if (ch == EOF) {
+            return EOF;
+        }
+    } else if (i >= HIP_RELAY_MAX_LINE_LEN) {
+        /*printf("Error on reading input. Maximum line length (%d "\
+         * "characters) exceeded.\n", HIP_RELAY_MAX_LINE_LEN);*/
+        *parseerr = HIP_ELONG;
+        /* If maximum line length is exceeded, we omit the rest of the
+         * line. */
+        while ((ch = fgetc(fp)) != EOF && ch != '\n') {
+        }
+        if (ch == EOF) {
+            return EOF;
+        }
+    }
+    return i;
 }
 
 /**
@@ -356,51 +378,51 @@
  * @param  values    a target linked list where the values are stored.
  * @param  parseerr  zero on success, HIP_EINPUT if an illegal control 
character
  *                   was encountered, HIP_ELONG if maximum number of characters
- *                   per line (@c HIP_RELAY_MAX_LINE_LEN) was exceed.          
    
+ *                   per line (@c HIP_RELAY_MAX_LINE_LEN) was exceed.
  * @return           EOF if the end of file was reached or if @c fp,
  *                   @c parameter, @c values or @c parseerr is NULL, otherwise
  *                   the number of succesfully read characters.
- */ 
+ */
 int hip_cf_get_line_data(FILE *fp, char *parameter, hip_configvaluelist_t 
*values,
-                        int *parseerr)
+                         int *parseerr)
 {
-       
-       if(fp == NULL || parameter == NULL || values == NULL || parseerr == 
NULL)
-               return EOF;
-       
-       int lineerr = 0;
-       char line[HIP_RELAY_MAX_LINE_LEN + 1];
-               
-       memset(line, '\0', sizeof(line));
-       
-       lineerr = hip_cf_readline(fp, line, parseerr);
-       
-       /* If there was no error on the line, let's try to parse the data. */
-       if(*parseerr == 0) {
-               if(hip_cf_is_comment(line)) {
-                       *parseerr = HIP_EVAL;
-               } else {
-                       *parseerr = hip_cf_parse_par(line, parameter);
-                       if(*parseerr == 0) {
-                               *parseerr = hip_cf_parse_val(line, values);
-                       }
-               }
-       }
-       
-       return lineerr;
+    if (fp == NULL || parameter == NULL || values == NULL || parseerr == NULL) 
{
+        return EOF;
+    }
+
+    int lineerr = 0;
+    char line[HIP_RELAY_MAX_LINE_LEN + 1];
+
+    memset(line, '\0', sizeof(line));
+
+    lineerr = hip_cf_readline(fp, line, parseerr);
+
+    /* If there was no error on the line, let's try to parse the data. */
+    if (*parseerr == 0) {
+        if (hip_cf_is_comment(line)) {
+            *parseerr = HIP_EVAL;
+        } else {
+            *parseerr = hip_cf_parse_par(line, parameter);
+            if (*parseerr == 0) {
+                *parseerr = hip_cf_parse_val(line, values);
+            }
+        }
+    }
+
+    return lineerr;
 }
 
-
 /**
  * Initializes a linked list. Sets the parameter @c linkedlist head to NULL if
  * the list itself is not NULL.
  *
  * @param linkedlist the list to init.
- */ 
+ */
 void hip_cvl_init(hip_configvaluelist_t *linkedlist)
 {
-       if(linkedlist != NULL)
-               linkedlist->head = NULL;
+    if (linkedlist != NULL) {
+        linkedlist->head = NULL;
+    }
 }
 
 /**
@@ -408,58 +430,61 @@
  * @c linkedlist and frees the memory allocated to the elements. The parameter
  * @c linkedlist is not itself freed.
  *
- * @param linkedlist the list to uninitialize.  
- */ 
+ * @param linkedlist the list to uninitialize.
+ */
 void hip_cvl_uninit(hip_configvaluelist_t *linkedlist)
 {
-       if(linkedlist == NULL || linkedlist->head == NULL)
-               return;
-
-       hip_configfilevalue_t *pointer = NULL;
-       pointer = linkedlist->head;
-
-       /* Free the item currently at list head and move the next item to list
-          head. Continue this until the item at list head is NULL. */
-       while(linkedlist->head != NULL) {
-               pointer = linkedlist->head->next;
-               free(linkedlist->head);
-               linkedlist->head = pointer;
-       }
+    if (linkedlist == NULL || linkedlist->head == NULL) {
+        return;
+    }
+
+    hip_configfilevalue_t *pointer = NULL;
+    pointer = linkedlist->head;
+
+    /* Free the item currently at list head and move the next item to list
+     * head. Continue this until the item at list head is NULL. */
+    while (linkedlist->head != NULL) {
+        pointer          = linkedlist->head->next;
+        free(linkedlist->head);
+        linkedlist->head = pointer;
+    }
 }
 
 /**
- * Gets the next element from a linked list. Gets the next element from 
+ * Gets the next element from a linked list. Gets the next element from
  * parameter @c linkedlist or NULL if the list end has been reached or if
  * @c linkedlist itself is NULL. If you need to get the first element of the
  * list, call this function with @c current set as NULL.
  *
- * @param  linkedlist the linked list from where to retrieve the element.  
+ * @param  linkedlist the linked list from where to retrieve the element.
  * @param  current    the current element or NULL if the first item from the
  *                    list is to be retrieved.
  * @return            the next element or NULL if the list end has been reached
  *                    or @c linkedlist is NULL.
- */ 
+ */
 hip_configfilevalue_t *hip_cvl_get_next(hip_configvaluelist_t *linkedlist,
-                                       hip_configfilevalue_t *current)
+                                        hip_configfilevalue_t *current)
 {
-       if (linkedlist == NULL)
-               return NULL;
-       if(current == NULL)
-               return linkedlist->head;
-       
-       return current->next;
+    if (linkedlist == NULL) {
+        return NULL;
+    }
+    if (current == NULL) {
+        return linkedlist->head;
+    }
+
+    return current->next;
 }
 
 /**
  * Prints node data to stdout. This function is intended for debug use.
  *
  * @param the node whose contents are to be printed.
- */ 
+ */
 void print_node(hip_configfilevalue_t *node)
 {
-       if(node == NULL){
-               HIP_INFO("Node NULL.\n");
-               return;
-       }
-       HIP_INFO(" '%s'\n", (node->data == NULL) ? "NULL" : node->data);
+    if (node == NULL) {
+        HIP_INFO("Node NULL.\n");
+        return;
+    }
+    HIP_INFO(" '%s'\n", (node->data == NULL) ? "NULL" : node->data);
 }

=== modified file 'hipd/configfilereader.c.doxyme'
--- hipd/configfilereader.c.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/configfilereader.c.doxyme      2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,100 +45,99 @@
 
 
 /**
- * hip_cf_get_line_data 
+ * hip_cf_get_line_data
  *
  *
  * @param fp
  * @param parameter
  * @param values
  * @param parseerr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_cf_is_comment 
+ * hip_cf_is_comment
  *
  *
  * @param line
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_cf_parse_par 
+ * hip_cf_parse_par
  *
  *
  * @param line
  * @param parameter
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_cf_parse_val 
+ * hip_cf_parse_val
  *
  *
  * @param line
  * @param values
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_cf_readline 
+ * hip_cf_readline
  *
  *
  * @param fp
  * @param buf
  * @param parseerr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_cvl_add 
+ * hip_cvl_add
  *
  *
  * @param linkedlist
  * @param data
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_cvl_get_next 
+ * hip_cvl_get_next
  *
  *
  * @param linkedlist
  * @param current
- * @return 
- **/
-
-
-/**
- * hip_cvl_init 
- *
- *
- * @param linkedlist
- * @return 
- **/
-
-
-/**
- * hip_cvl_uninit 
- *
- *
- * @param linkedlist
- * @return 
- **/
-
-
-/**
- * print_node 
+ * @return
+ **/
+
+
+/**
+ * hip_cvl_init
+ *
+ *
+ * @param linkedlist
+ * @return
+ **/
+
+
+/**
+ * hip_cvl_uninit
+ *
+ *
+ * @param linkedlist
+ * @return
+ **/
+
+
+/**
+ * print_node
  *
  *
  * @param node
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/configfilereader.h'
--- hipd/configfilereader.h     2010-01-19 09:28:42 +0000
+++ hipd/configfilereader.h     2010-02-10 22:32:46 +0000
@@ -2,7 +2,7 @@
  * A header file for configfilereader.c
  *
  * This is a general purpose configurationfilereader. The configurationfile
- * consists of stanzas of the following form: 
+ * consists of stanzas of the following form:
  * <pre>
  * parametername = "value1", "value2", "value3", ..., "valueN"
  * </pre>
@@ -14,7 +14,7 @@
  *
  * There is no need to use any other function from this file than
  * hip_cf_get_line_data().
- * 
+ *
  * Usage:
  * <ol>
  * <li>Declare integers <code>lineerr</code> and <code>parseerr</code> and set
@@ -31,16 +31,16 @@
  *     memset(parameter, '\0', sizeof(parameter));
  *     hip_cvl_init(&values);
  *     lineerr = hip_cf_get_line_data(fp, parameter, &values, &parseerr);
- *                             
+ *
  *     if(parseerr == 0){
- *        
+ *
  *       ... parameter has now the parameter name ...
- * 
+ *
  *        hip_configfilevalue_t *current = NULL;
- *       while((current = hip_cvl_get_next(&values, current)) != NULL) {
- * 
+ *    while((current = hip_cvl_get_next(&values, current)) != NULL) {
+ *
  *           ... do stuff with the current value ...
- *       
+ *
  *        }
  *    }
  *    hip_cvl_uninit(&values);
@@ -49,7 +49,7 @@
  * </li>
  * <li>Close the configfile using <code>close()</code></li>
  * </ol>
- * 
+ *
  * @author  Lauri Silvennoinen
  * @version 1.0
  * @date    14.02.2008
@@ -77,22 +77,22 @@
 #define HIP_RELAY_VAL_SEP      ','
 
 /** Linked list node. */
-typedef struct hip_cvl_node{
-       char data[HIP_RELAY_MAX_VAL_LEN + 1]; /**< Node data. */
-       struct hip_cvl_node *next; /**< A pointer to next item. */ 
-}hip_configfilevalue_t;
+typedef struct hip_cvl_node {
+    char                 data[HIP_RELAY_MAX_VAL_LEN + 1]; /**< Node data. */
+    struct hip_cvl_node *next;     /**< A pointer to next item. */
+} hip_configfilevalue_t;
 
 /** Linked list. */
-typedef struct{
-       hip_configfilevalue_t *head; /**< A pointer to the first item of the 
list. */
-}hip_configvaluelist_t;
+typedef struct {
+    hip_configfilevalue_t *head;     /**< A pointer to the first item of the 
list. */
+} hip_configvaluelist_t;
 
 int hip_cf_get_line_data(FILE *fp, char *parameter, hip_configvaluelist_t 
*values,
-                        int *parseerr);
+                         int *parseerr);
 void hip_cvl_init(hip_configvaluelist_t *linkedlist);
 void hip_cvl_uninit(hip_configvaluelist_t *linkedlist);
 hip_configfilevalue_t *hip_cvl_get_next(hip_configvaluelist_t *linkedlist,
-                                      hip_configfilevalue_t *current);
+                                        hip_configfilevalue_t *current);
 void print_node(hip_configfilevalue_t *node);
 
 #endif /* CONFIGFILEREADER_H */

=== modified file 'hipd/configfilereader.h.doxyme'
--- hipd/configfilereader.h.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/configfilereader.h.doxyme      2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/configfilereader.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/cookie.c'
--- hipd/cookie.c       2010-01-09 14:18:07 +0000
+++ hipd/cookie.c       2010-02-10 22:32:46 +0000
@@ -1,7 +1,7 @@
 /**
  * @file
  * HIP cookie handling. Licence: GNU/GPL
- * 
+ *
  * @author Kristian Slavov <ksl#iki.fi>
  * @author Miika Komu <miika#iki.fi>
  *
@@ -23,39 +23,41 @@
 #ifndef CONFIG_HIP_ICOOKIE /* see also spam.c for overriding functions */
 
 #if 0
-void hip_init_puzzle_defaults() {
-       return;
+void hip_init_puzzle_defaults()
+{
+    return;
 }
+
 #endif
 
 int hip_get_cookie_difficulty(hip_hit_t *not_used)
 {
-       /* Note: we could return a higher value if we detect DoS */
-       return hip_cookie_difficulty;
+    /* Note: we could return a higher value if we detect DoS */
+    return hip_cookie_difficulty;
 }
 
 int hip_set_cookie_difficulty(hip_hit_t *not_used, int k)
 {
-       if (k > HIP_PUZZLE_MAX_K || k < 1) {
-               HIP_ERROR("Bad cookie value (%d), min=%d, max=%d\n",
-                         k, 1, HIP_PUZZLE_MAX_K);
-               return -1;
-       }
-       hip_cookie_difficulty = k;
-       HIP_DEBUG("HIP cookie value set to %d\n", k);
-       return k;
+    if (k > HIP_PUZZLE_MAX_K || k < 1) {
+        HIP_ERROR("Bad cookie value (%d), min=%d, max=%d\n",
+                  k, 1, HIP_PUZZLE_MAX_K);
+        return -1;
+    }
+    hip_cookie_difficulty = k;
+    HIP_DEBUG("HIP cookie value set to %d\n", k);
+    return k;
 }
 
 int hip_inc_cookie_difficulty(hip_hit_t *not_used)
 {
-       int k = hip_get_cookie_difficulty(NULL) + 1;
-       return hip_set_cookie_difficulty(NULL, k);
+    int k = hip_get_cookie_difficulty(NULL) + 1;
+    return hip_set_cookie_difficulty(NULL, k);
 }
 
 int hip_dec_cookie_difficulty(hip_hit_t *not_used)
 {
-       int k = hip_get_cookie_difficulty(NULL) - 1;
-       return hip_set_cookie_difficulty(NULL, k);
+    int k = hip_get_cookie_difficulty(NULL) - 1;
+    return hip_set_cookie_difficulty(NULL, k);
 }
 
 /**
@@ -67,24 +69,25 @@
  * @return 0 <= x < HIP_R1TABLESIZE
  */
 int hip_calc_cookie_idx(struct in6_addr *ip_i, struct in6_addr *ip_r,
-                              struct in6_addr *hit_i)
+                        struct in6_addr *hit_i)
 {
-       register u32 base=0;
-       int i;
-
-       for(i = 0; i < 4; i++) {
-               base ^= ip_i->s6_addr32[i];
-               base ^= ip_r->s6_addr32[i];
-       }
-
-       for(i = 0; i < 3; i++) {
-               base ^= ((base >> (24 - i * 8)) & 0xFF);
-       }
-
-       /* base ready */
-
-       return (base) % HIP_R1TABLESIZE;
+    register u32 base = 0;
+    int i;
+
+    for (i = 0; i < 4; i++) {
+        base ^= ip_i->s6_addr32[i];
+        base ^= ip_r->s6_addr32[i];
+    }
+
+    for (i = 0; i < 3; i++) {
+        base ^= ((base >> (24 - i * 8)) & 0xFF);
+    }
+
+    /* base ready */
+
+    return (base) % HIP_R1TABLESIZE;
 }
+
 #endif /* !CONFIG_HIP_ICOOKIE */
 
 /**
@@ -92,300 +95,303 @@
  * @param ip_i Initiator's IPv6
  * @param ip_r Responder's IPv6
  *
- * Comments for the if 0 code are inlined below. 
- * 
+ * Comments for the if 0 code are inlined below.
+ *
  * Returns NULL if error.
  */
 struct hip_common *hip_get_r1(struct in6_addr *ip_i, struct in6_addr *ip_r,
-                             struct in6_addr *our_hit,
-                             struct in6_addr *peer_hit)
+                              struct in6_addr *our_hit,
+                              struct in6_addr *peer_hit)
 {
-       struct hip_common *err = NULL, *r1 = NULL;
-       struct hip_r1entry * hip_r1table = NULL;
-       struct hip_host_id_entry *hid = NULL;
-       int idx, len;
+    struct hip_common *err          = NULL, *r1 = NULL;
+    struct hip_r1entry *hip_r1table = NULL;
+    struct hip_host_id_entry *hid   = NULL;
+    int idx, len;
 
-       /* Find the proper R1 table and copy the R1 message from the table */
-       HIP_READ_LOCK_DB(HIP_DB_LOCAL_HID);     
-       HIP_IFEL(!(hid = hip_get_hostid_entry_by_lhi_and_algo(HIP_DB_LOCAL_HID, 
our_hit, HIP_ANY_ALGO, -1)), 
-                NULL, "Unknown HIT\n");
+    /* Find the proper R1 table and copy the R1 message from the table */
+    HIP_READ_LOCK_DB(HIP_DB_LOCAL_HID);
+    HIP_IFEL(!(hid = hip_get_hostid_entry_by_lhi_and_algo(HIP_DB_LOCAL_HID, 
our_hit, HIP_ANY_ALGO, -1)),
+             NULL, "Unknown HIT\n");
 
 #ifdef CONFIG_HIP_BLIND
-       if (hip_blind_get_status()) {
-          hip_r1table = hid->blindr1;
-        }
+    if (hip_blind_get_status()) {
+        hip_r1table = hid->blindr1;
+    }
 #endif
-       if (!hip_blind_get_status()) {
-          hip_r1table = hid->r1;
-        }
-       
-       // BLIND TODO: indexing?
-       idx = hip_calc_cookie_idx(ip_i, ip_r, peer_hit);
-       HIP_DEBUG("Calculated index: %d\n", idx);
-
-       /* the code under if 0 periodically changes the puzzle. It is not 
included
-          in compilation as there is currently no easy way of signing the R1 
packet
-          after having changed its puzzle.
-       */
+    if (!hip_blind_get_status()) {
+        hip_r1table = hid->r1;
+    }
+
+    // BLIND TODO: indexing?
+    idx = hip_calc_cookie_idx(ip_i, ip_r, peer_hit);
+    HIP_DEBUG("Calculated index: %d\n", idx);
+
+    /* the code under if 0 periodically changes the puzzle. It is not included
+     * in compilation as there is currently no easy way of signing the R1 
packet
+     * after having changed its puzzle.
+     */
 #if 0
-       /* generating opaque data */
-       do_gettimeofday(&tv);
-
-       /* extract the puzzle */
-       if (!(pz = hip_get_param(err->r1, HIP_PARAM_PUZZLE)), NULL, 
-           "Internal error: Could not find PUZZLE parameter in precreated R1 
packet\n");
-
-       ts = pz->opaque[0];
-       ts |= ((int)pz->opaque[1] << 8);
-
-       if (ts != 0) {
-               /* check if the cookie is too old */
-               diff = (tv.tv_sec & 0xFFFFFF) - ts;
-               if (diff < 0)
-                       diff += 0x1000000;
-
-               HIP_DEBUG("Old puzzle still valid\n");
-               if (diff <= HIP_PUZZLE_MAX_LIFETIME)
-                       return err;
-       }
-
-       /* either ts == 0 or diff > HIP_PUZZLE_MAX_LIFETIME */
-       _HIP_DEBUG("Creating new puzzle\n");
-       hip_create_new_puzzle(pz, r1, &tv);
-
-       /* XXX: sign the R1 */
+    /* generating opaque data */
+    do_gettimeofday(&tv);
+
+    /* extract the puzzle */
+    if (!(pz = hip_get_param(err->r1, HIP_PARAM_PUZZLE)), NULL,
+        "Internal error: Could not find PUZZLE parameter in precreated R1 
packet\n") {
+        ;
+    }
+
+    ts  = pz->opaque[0];
+    ts |= ((int) pz->opaque[1] << 8);
+
+    if (ts != 0) {
+        /* check if the cookie is too old */
+        diff = (tv.tv_sec & 0xFFFFFF) - ts;
+        if (diff < 0) {
+            diff += 0x1000000;
+        }
+
+        HIP_DEBUG("Old puzzle still valid\n");
+        if (diff <= HIP_PUZZLE_MAX_LIFETIME) {
+            return err;
+        }
+    }
+
+    /* either ts == 0 or diff > HIP_PUZZLE_MAX_LIFETIME */
+    _HIP_DEBUG("Creating new puzzle\n");
+    hip_create_new_puzzle(pz, r1, &tv);
+
+    /* XXX: sign the R1 */
 #endif
-       /* Create a copy of the found entry */
-       len = hip_get_msg_total_len(hip_r1table[idx].r1);
-       r1 = hip_msg_alloc();
-       memcpy(r1, hip_r1table[idx].r1, len);
-       err = r1;
-
- out_err:      
-       if (!err && r1)
-               HIP_FREE(r1);
-
-       HIP_READ_UNLOCK_DB(HIP_DB_LOCAL_HID);
-       return err;
+    /* Create a copy of the found entry */
+    len = hip_get_msg_total_len(hip_r1table[idx].r1);
+    r1  = hip_msg_alloc();
+    memcpy(r1, hip_r1table[idx].r1, len);
+    err = r1;
+
+out_err:
+    if (!err && r1) {
+        HIP_FREE(r1);
+    }
+
+    HIP_READ_UNLOCK_DB(HIP_DB_LOCAL_HID);
+    return err;
 }
 
-
-struct hip_r1entry * hip_init_r1(void)
+struct hip_r1entry *hip_init_r1(void)
 {
-       struct hip_r1entry *err;
-
-       HIP_IFE(!(err = (struct hip_r1entry *)HIP_MALLOC(sizeof(struct 
hip_r1entry) * HIP_R1TABLESIZE,
-                                                        GFP_KERNEL)), NULL); 
-       memset(err, 0, sizeof(struct hip_r1entry) * HIP_R1TABLESIZE);
-
- out_err:
-       return err;
+    struct hip_r1entry *err;
+
+    HIP_IFE(!(err = (struct hip_r1entry *) HIP_MALLOC(sizeof(struct 
hip_r1entry) * HIP_R1TABLESIZE,
+                                                      GFP_KERNEL)), NULL);
+    memset(err, 0, sizeof(struct hip_r1entry) * HIP_R1TABLESIZE);
+
+out_err:
+    return err;
 }
 
 #ifndef CONFIG_HIP_ICOOKIE
 /*
  * @sign the signing function to use
  */
-int hip_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit, 
-                    int (*sign)(void *key, struct hip_common *m),
-                    void *privkey, struct hip_host_id *pubkey)
+int hip_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit,
+                     int (*sign)(void *key, struct hip_common *m),
+                     void *privkey, struct hip_host_id *pubkey)
 {
-       int i=0;
-       for(i = 0; i < HIP_R1TABLESIZE; i++) {
-               int cookie_k;
-
-               cookie_k = hip_get_cookie_difficulty(NULL);
-
-               r1table[i].r1 = hip_create_r1(hit, sign, privkey, pubkey,
-                                             cookie_k);
-               if (!r1table[i].r1) {
-                       HIP_ERROR("Unable to precreate R1s\n");
-                       goto err_out;
-               }
-
-               HIP_DEBUG("Packet %d created\n", i);
-       }
-
-       return 1;
-
- err_out:
-       return 0;
+    int i = 0;
+    for (i = 0; i < HIP_R1TABLESIZE; i++) {
+        int cookie_k;
+
+        cookie_k      = hip_get_cookie_difficulty(NULL);
+
+        r1table[i].r1 = hip_create_r1(hit, sign, privkey, pubkey,
+                                      cookie_k);
+        if (!r1table[i].r1) {
+            HIP_ERROR("Unable to precreate R1s\n");
+            goto err_out;
+        }
+
+        HIP_DEBUG("Packet %d created\n", i);
+    }
+
+    return 1;
+
+err_out:
+    return 0;
 }
+
 #endif /* !CONFIG_HIP_ICOOKIE */
 
 void hip_uninit_r1(struct hip_r1entry *hip_r1table)
 {
-       int i;
+    int i;
 
-       /* The R1 packet consist of 2 memory blocks. One contains the actual
-        * buffer where the packet is formed, while the other contains
-        * pointers to different TLVs to speed up parsing etc.
-        * The r1->common is the actual buffer, and r1 is the structure
-        * holding only pointers to the TLVs.
-        */
-       if (hip_r1table) {
-               for(i=0; i < HIP_R1TABLESIZE; i++) {
-                       if (hip_r1table[i].r1) {
-                               HIP_FREE(hip_r1table[i].r1);
-                       }
-               }
-               HIP_FREE(hip_r1table);
-               hip_r1table = NULL;
-       }
+    /* The R1 packet consist of 2 memory blocks. One contains the actual
+     * buffer where the packet is formed, while the other contains
+     * pointers to different TLVs to speed up parsing etc.
+     * The r1->common is the actual buffer, and r1 is the structure
+     * holding only pointers to the TLVs.
+     */
+    if (hip_r1table) {
+        for (i = 0; i < HIP_R1TABLESIZE; i++) {
+            if (hip_r1table[i].r1) {
+                HIP_FREE(hip_r1table[i].r1);
+            }
+        }
+        HIP_FREE(hip_r1table);
+        hip_r1table = NULL;
+    }
 }
 
 /**
  * Verifies the solution of a puzzle. First we check that K and I are the same
  * as in the puzzle we sent. If not, then we check the previous ones (since the
- * puzzle might just have been expired). 
- * 
+ * puzzle might just have been expired).
+ *
  * @param ip_i     a pointer to Initiator's IP address.
  * @param ip_r     a pointer to Responder's IP address.
  * @param hdr      a pointer to HIP packet common header
  * @param solution a pointer to a solution structure
  * @return         Zero if the cookie was verified succesfully, negative
  *                 otherwise.
- */ 
-int hip_verify_cookie(in6_addr_t *ip_i, in6_addr_t *ip_r, 
-                     hip_common_t *hdr, struct hip_solution *solution)
-{
-       /* In a effort to conform the HIPL coding convention, the return value
-          of this function was inverted. I.e. This function now returns
-          negative for error conditions, zero otherwise. It used to be the
-          other way around. -Lauri 23.07.2008. */
-       struct hip_puzzle *puzzle = NULL;
-       struct hip_r1entry *result = NULL;
-       struct hip_host_id_entry *hid = NULL;
-       int err = 0;
-       
-#ifdef CONFIG_HIP_BLIND
-       if (hip_blind_get_status()) {
-               struct in6_addr plain_local_hit;
-               uint16_t nonce;
-               HIP_IFEL(hip_blind_get_nonce(hdr, &nonce), -1,
-                        "hip_blind_get_nonce failed\n");
-               HIP_IFEL(hip_plain_fingerprint(&nonce,
-                                              &hdr->hitr, &plain_local_hit), 
-                        -1, "hip_plain_fingerprint failed\n");
-               
-               /* Find the proper R1 table, use plain hit */
-               HIP_IFEL(!(hid = hip_get_hostid_entry_by_lhi_and_algo(
-                                  HIP_DB_LOCAL_HID, &plain_local_hit,
-                                  HIP_ANY_ALGO, -1)), 
-                        -1, "Requested source HIT not (any more) 
available.\n");
-               
-               result = &hid->blindr1[hip_calc_cookie_idx(ip_i, ip_r, 
&hdr->hits)];
-       }
-#endif
-       
-       /* Find the proper R1 table, no blind used */
-       if (!hip_blind_get_status()) {
-               
-               HIP_IFEL(!(hid = hip_get_hostid_entry_by_lhi_and_algo(
-                                  HIP_DB_LOCAL_HID, &hdr->hitr, HIP_ANY_ALGO,
-                                  -1)), 
-                        -1, "Requested source HIT not (any more) 
available.\n");
-               result = &hid->r1[hip_calc_cookie_idx(ip_i, ip_r, &hdr->hits)];
-       }
-
-       puzzle = hip_get_param(result->r1, HIP_PARAM_PUZZLE);
-       HIP_IFEL(!puzzle, -1, "Internal error: could not find the cookie\n");
-
-       _HIP_HEXDUMP("opaque in solution", solution->opaque,
-                    HIP_PUZZLE_OPAQUE_LEN);
-       _HIP_HEXDUMP("Copaque in result", result->Copaque,
-                    HIP_PUZZLE_OPAQUE_LEN);
-       _HIP_HEXDUMP("opaque in puzzle", puzzle->opaque,
-                    HIP_PUZZLE_OPAQUE_LEN);
-
-       HIP_IFEL(memcmp(solution->opaque, puzzle->opaque,
-                       HIP_PUZZLE_OPAQUE_LEN), -1, 
-                "Received cookie opaque does not match the sent opaque\n");
-       
-       HIP_DEBUG("Solution's I (0x%llx), sent I (0x%llx)\n",
-                 solution->I, puzzle->I);
-
-       _HIP_HEXDUMP("opaque in solution", solution->opaque, 
-                    HIP_PUZZLE_OPAQUE_LEN);
-       _HIP_HEXDUMP("opaque in result", result->Copaque, 
-                    HIP_PUZZLE_OPAQUE_LEN);
-       _HIP_HEXDUMP("opaque in puzzle", puzzle->opaque, 
-                    HIP_PUZZLE_OPAQUE_LEN);
-
-       if (solution->K != puzzle->K) {
-               HIP_INFO("Solution's K (%d) does not match sent K (%d)\n",
-                        solution->K, puzzle->K);
-
-               HIP_IFEL(solution->K != result->Ck, -1,
-                        "Solution's K did not match any sent Ks.\n");
-               HIP_IFEL(solution->I != result->Ci, -1, 
-                        "Solution's I did not match the sent I\n");
-               HIP_IFEL(memcmp(solution->opaque, result->Copaque,
-                               HIP_PUZZLE_OPAQUE_LEN), -1,
-                        "Solution's opaque data does not match sent opaque "\
-                        "data.\n");
-               HIP_DEBUG("Received solution to an old puzzle\n");
-
-       } else {
-               HIP_HEXDUMP("solution", solution, sizeof(*solution));
-               HIP_HEXDUMP("puzzle", puzzle, sizeof(*puzzle));
-               HIP_IFEL(solution->I != puzzle->I, -1,
-                        "Solution's I did not match the sent I\n");
-               HIP_IFEL(memcmp(solution->opaque, puzzle->opaque,
-                               HIP_PUZZLE_OPAQUE_LEN), -1, 
-                        "Solution's opaque data does not match the opaque "\
-                        "data sent\n");
-       }
-       
-       HIP_IFEL(!hip_solve_puzzle(solution, hdr, HIP_VERIFY_PUZZLE), -1, 
-                "Puzzle incorrectly solved.\n");
-       
- out_err:
-       
-       return err;
-}
-
-static int hip_recreate_r1s_for_entry_move(struct hip_host_id_entry *entry, 
void *new_hash)
-{
-       int err = 0;
-
-       hip_uninit_r1(entry->r1);
-       HIP_IFE(!(entry->r1 = hip_init_r1()), -ENOMEM);
-       HIP_IFE(!hip_precreate_r1(entry->r1, &entry->lhi.hit,
-                       (hip_get_host_id_algo(entry->host_id) ==
-                       HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign),
-                       entry->private_key, entry->host_id), -1);
-
-#ifdef CONFIG_HIP_BLIND
-       hip_uninit_r1(entry->blindr1);
-       HIP_IFE(!(entry->r1 = hip_init_r1()), -ENOMEM);
-       HIP_IFE(!hip_precreate_r1(entry->blindr1, &entry->lhi.hit,
-                       (hip_get_host_id_algo(entry->host_id) ==
-                       HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign),
-                       entry->private_key, entry->host_id), -1);
-#endif
-
-out_err:
-       return err;
-}
-
+ */
+int hip_verify_cookie(in6_addr_t *ip_i, in6_addr_t *ip_r,
+                      hip_common_t *hdr, struct hip_solution *solution)
+{
+    /* In a effort to conform the HIPL coding convention, the return value
+     * of this function was inverted. I.e. This function now returns
+     * negative for error conditions, zero otherwise. It used to be the
+     * other way around. -Lauri 23.07.2008. */
+    struct hip_puzzle *puzzle     = NULL;
+    struct hip_r1entry *result    = NULL;
+    struct hip_host_id_entry *hid = NULL;
+    int err                       = 0;
+
+#ifdef CONFIG_HIP_BLIND
+    if (hip_blind_get_status()) {
+        struct in6_addr plain_local_hit;
+        uint16_t nonce;
+        HIP_IFEL(hip_blind_get_nonce(hdr, &nonce), -1,
+                 "hip_blind_get_nonce failed\n");
+        HIP_IFEL(hip_plain_fingerprint(&nonce,
+                                       &hdr->hitr, &plain_local_hit),
+                 -1, "hip_plain_fingerprint failed\n");
+
+        /* Find the proper R1 table, use plain hit */
+        HIP_IFEL(!(hid = hip_get_hostid_entry_by_lhi_and_algo(
+                       HIP_DB_LOCAL_HID, &plain_local_hit,
+                       HIP_ANY_ALGO, -1)),
+                 -1, "Requested source HIT not (any more) available.\n");
+
+        result = &hid->blindr1[hip_calc_cookie_idx(ip_i, ip_r, &hdr->hits)];
+    }
+#endif
+
+    /* Find the proper R1 table, no blind used */
+    if (!hip_blind_get_status()) {
+        HIP_IFEL(!(hid = hip_get_hostid_entry_by_lhi_and_algo(
+                       HIP_DB_LOCAL_HID, &hdr->hitr, HIP_ANY_ALGO,
+                       -1)),
+                 -1, "Requested source HIT not (any more) available.\n");
+        result = &hid->r1[hip_calc_cookie_idx(ip_i, ip_r, &hdr->hits)];
+    }
+
+    puzzle = hip_get_param(result->r1, HIP_PARAM_PUZZLE);
+    HIP_IFEL(!puzzle, -1, "Internal error: could not find the cookie\n");
+
+    _HIP_HEXDUMP("opaque in solution", solution->opaque,
+                 HIP_PUZZLE_OPAQUE_LEN);
+    _HIP_HEXDUMP("Copaque in result", result->Copaque,
+                 HIP_PUZZLE_OPAQUE_LEN);
+    _HIP_HEXDUMP("opaque in puzzle", puzzle->opaque,
+                 HIP_PUZZLE_OPAQUE_LEN);
+
+    HIP_IFEL(memcmp(solution->opaque, puzzle->opaque,
+                    HIP_PUZZLE_OPAQUE_LEN), -1,
+             "Received cookie opaque does not match the sent opaque\n");
+
+    HIP_DEBUG("Solution's I (0x%llx), sent I (0x%llx)\n",
+              solution->I, puzzle->I);
+
+    _HIP_HEXDUMP("opaque in solution", solution->opaque,
+                 HIP_PUZZLE_OPAQUE_LEN);
+    _HIP_HEXDUMP("opaque in result", result->Copaque,
+                 HIP_PUZZLE_OPAQUE_LEN);
+    _HIP_HEXDUMP("opaque in puzzle", puzzle->opaque,
+                 HIP_PUZZLE_OPAQUE_LEN);
+
+    if (solution->K != puzzle->K) {
+        HIP_INFO("Solution's K (%d) does not match sent K (%d)\n",
+                 solution->K, puzzle->K);
+
+        HIP_IFEL(solution->K != result->Ck, -1,
+                 "Solution's K did not match any sent Ks.\n");
+        HIP_IFEL(solution->I != result->Ci, -1,
+                 "Solution's I did not match the sent I\n");
+        HIP_IFEL(memcmp(solution->opaque, result->Copaque,
+                        HIP_PUZZLE_OPAQUE_LEN), -1,
+                 "Solution's opaque data does not match sent opaque " \
+                 "data.\n");
+        HIP_DEBUG("Received solution to an old puzzle\n");
+    } else {
+        HIP_HEXDUMP("solution", solution, sizeof(*solution));
+        HIP_HEXDUMP("puzzle", puzzle, sizeof(*puzzle));
+        HIP_IFEL(solution->I != puzzle->I, -1,
+                 "Solution's I did not match the sent I\n");
+        HIP_IFEL(memcmp(solution->opaque, puzzle->opaque,
+                        HIP_PUZZLE_OPAQUE_LEN), -1,
+                 "Solution's opaque data does not match the opaque " \
+                 "data sent\n");
+    }
+
+    HIP_IFEL(!hip_solve_puzzle(solution, hdr, HIP_VERIFY_PUZZLE), -1,
+             "Puzzle incorrectly solved.\n");
+
+out_err:
+
+    return err;
+}
+
+static int hip_recreate_r1s_for_entry_move(struct hip_host_id_entry *entry,
+                                           void *new_hash)
+{
+    int err = 0;
+
+    hip_uninit_r1(entry->r1);
+    HIP_IFE(!(entry->r1 = hip_init_r1()), -ENOMEM);
+    HIP_IFE(!hip_precreate_r1(entry->r1, &entry->lhi.hit,
+                              (hip_get_host_id_algo(entry->host_id) ==
+                               HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign),
+                              entry->private_key, entry->host_id), -1);
+
+#ifdef CONFIG_HIP_BLIND
+    hip_uninit_r1(entry->blindr1);
+    HIP_IFE(!(entry->r1 = hip_init_r1()), -ENOMEM);
+    HIP_IFE(!hip_precreate_r1(entry->blindr1, &entry->lhi.hit,
+                              (hip_get_host_id_algo(entry->host_id) ==
+                               HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign),
+                              entry->private_key, entry->host_id), -1);
+#endif
+
+out_err:
+    return err;
+}
 
 int hip_recreate_all_precreated_r1_packets()
 {
-       HIP_HASHTABLE *ht = hip_ht_init(hip_hidb_hash, hip_hidb_match);
-       hip_list_t *curr, *iter;
-       struct hip_host_id *tmp;
-       int c;
-
-       hip_for_each_hi(hip_recreate_r1s_for_entry_move, ht);
-
-       list_for_each_safe(curr, iter, ht, c)
-       {
-               tmp = (struct hip_host_id *)list_entry(curr);
-               hip_ht_add(HIP_DB_LOCAL_HID, tmp);
-               list_del(tmp, ht);
-       }
-
-       hip_ht_uninit(ht);
-       return 0;
+    HIP_HASHTABLE *ht = hip_ht_init(hip_hidb_hash, hip_hidb_match);
+    hip_list_t *curr, *iter;
+    struct hip_host_id *tmp;
+    int c;
+
+    hip_for_each_hi(hip_recreate_r1s_for_entry_move, ht);
+
+    list_for_each_safe(curr, iter, ht, c)
+    {
+        tmp = (struct hip_host_id *) list_entry(curr);
+        hip_ht_add(HIP_DB_LOCAL_HID, tmp);
+        list_del(tmp, ht);
+    }
+
+    hip_ht_uninit(ht);
+    return 0;
 }

=== modified file 'hipd/cookie.c.doxyme'
--- hipd/cookie.c.doxyme        2009-12-12 10:44:54 +0000
+++ hipd/cookie.c.doxyme        2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,66 +45,66 @@
 
 
 /**
- * hip_calc_cookie_idx 
+ * hip_calc_cookie_idx
  *
  *
  * @param ip_i
  * @param ip_r
  * @param hit_i
- * @return 
- **/
-
-
-/**
- * hip_dec_cookie_difficulty 
- *
- *
- * @param not_used
- * @return 
- **/
-
-
-/**
- * hip_get_cookie_difficulty 
- *
- *
- * @param not_used
- * @return 
- **/
-
-
-/**
- * hip_get_r1 
+ * @return
+ **/
+
+
+/**
+ * hip_dec_cookie_difficulty
+ *
+ *
+ * @param not_used
+ * @return
+ **/
+
+
+/**
+ * hip_get_cookie_difficulty
+ *
+ *
+ * @param not_used
+ * @return
+ **/
+
+
+/**
+ * hip_get_r1
  *
  *
  * @param ip_i
  * @param ip_r
  * @param our_hit
  * @param peer_hit
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_inc_cookie_difficulty 
+ * hip_inc_cookie_difficulty
  *
  *
  * @param not_used
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_r1 
+ * hip_init_r1
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_precreate_r1 
+ * hip_precreate_r1
  *
  *
  * @param r1table
@@ -112,47 +113,46 @@
  * @param m)
  * @param privkey
  * @param pubkey)
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_recreate_r1s_for_entry_move 
+ * hip_recreate_r1s_for_entry_move
  *
  *
  * @param entry
  * @param new_hash
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_set_cookie_difficulty 
+ * hip_set_cookie_difficulty
  *
  *
  * @param not_used
  * @param k
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_uninit_r1 
+ * hip_uninit_r1
  *
  *
  * @param hip_r1table
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_verify_cookie 
+ * hip_verify_cookie
  *
  *
  * @param ip_i
  * @param ip_r
  * @param hdr
  * @param solution
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/cookie.h'
--- hipd/cookie.h       2010-01-19 09:28:42 +0000
+++ hipd/cookie.h       2010-02-10 22:32:46 +0000
@@ -8,26 +8,29 @@
 #include "hipd.h"
 
 struct hip_r1entry {
-       struct hip_common *r1;
-       uint32_t generation;
-       uint64_t Ci;
-       uint8_t Ck;
-       uint8_t Copaque[3];
+    struct hip_common *r1;
+    uint32_t           generation;
+    uint64_t           Ci;
+    uint8_t            Ck;
+    uint8_t            Copaque[3];
 };
 
 #define HIP_R1TABLESIZE         3 /* precreate only this many R1s */
 #define HIP_PUZZLE_MAX_K        28
-struct hip_common * hip_get_r1(struct in6_addr *ip_i, struct in6_addr *ip_r, 
struct in6_addr *src_hit, struct in6_addr *peer_hit);
-struct hip_r1entry * hip_init_r1(void);
+struct hip_common *hip_get_r1(struct in6_addr *ip_i,
+                              struct in6_addr *ip_r,
+                              struct in6_addr *src_hit,
+                              struct in6_addr *peer_hit);
+struct hip_r1entry *hip_init_r1(void);
 void hip_uninit_r1(struct hip_r1entry *);
 int hip_recreate_all_precreated_r1_packets(void);
-int hip_precreate_r1(struct hip_r1entry *r1table, 
-                    struct in6_addr *hit, 
-                    int (*sign)(void *key, struct hip_common *m),
-                    void *privkey,                  
-                    struct hip_host_id *pubkey);
+int hip_precreate_r1(struct hip_r1entry *r1table,
+                     struct in6_addr *hit,
+                     int (*sign)(void *key, struct hip_common *m),
+                     void *privkey,
+                     struct hip_host_id *pubkey);
 int hip_verify_cookie(in6_addr_t *ip_i, in6_addr_t *ip_r,  hip_common_t *hdr,
-                     struct hip_solution *cookie);
+                      struct hip_solution *cookie);
 int hip_set_cookie_difficulty(hip_hit_t *not_used, int k);
 int hip_get_cookie_difficulty(hip_hit_t *not_used);
 int hip_inc_cookie_difficulty(hip_hit_t *not_used);

=== modified file 'hipd/cookie.h.doxyme'
--- hipd/cookie.h.doxyme        2009-12-12 10:44:54 +0000
+++ hipd/cookie.h.doxyme        2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/cookie.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/dh.c'
--- hipd/dh.c   2009-12-29 15:00:23 +0000
+++ hipd/dh.c   2010-02-10 22:32:46 +0000
@@ -7,7 +7,7 @@
  * - Mika Kousa <mkousa@xxxxxx>
  * - Kristian Slavov <ksl@xxxxxx>
  * - Tobias Heer <heer@xxxxxxxxxx>
- */ 
+ */
 
 #include "dh.h"
 
@@ -23,39 +23,39 @@
  */
 int hip_insert_dh(u8 *buffer, int bufsize, int group_id)
 {
-       size_t res;
-       DH *tmp;
-
-        /*
-        * First check that we have the key available.
-        * Then encode it into the buffer
-        */
-
-       if (dh_table[group_id] == NULL) {
-               tmp = hip_generate_dh_key(group_id);
-
-               dh_table[group_id] = tmp;
-
-               if (dh_table[group_id] == NULL) {
-                       HIP_ERROR("DH key %d not found and could not create 
it\n",
-                                 group_id);
-                       return -1;
-               }
-       }
-
-       tmp = dh_table[group_id];
-
-       res = hip_encode_dh_publickey(tmp, buffer, bufsize);
-       if (res < 0) {
-               HIP_ERROR("Encoding error\n");
-               res = -3;
-               goto err_free;
-       }
-
-       _HIP_HEXDUMP("DH public key: ", buffer, res);
-
- err_free:
-       return res;
+    size_t res;
+    DH *tmp;
+
+    /*
+     * First check that we have the key available.
+     * Then encode it into the buffer
+     */
+
+    if (dh_table[group_id] == NULL) {
+        tmp                = hip_generate_dh_key(group_id);
+
+        dh_table[group_id] = tmp;
+
+        if (dh_table[group_id] == NULL) {
+            HIP_ERROR("DH key %d not found and could not create it\n",
+                      group_id);
+            return -1;
+        }
+    }
+
+    tmp = dh_table[group_id];
+
+    res = hip_encode_dh_publickey(tmp, buffer, bufsize);
+    if (res < 0) {
+        HIP_ERROR("Encoding error\n");
+        res = -3;
+        goto err_free;
+    }
+
+    _HIP_HEXDUMP("DH public key: ", buffer, res);
+
+err_free:
+    return res;
 }
 
 /**
@@ -71,42 +71,44 @@
  * @return the length of the shared secret in octets if successful,
  * or -1 if an error occured.
  */
-int hip_calculate_shared_secret(uint8_t *public_value, uint8_t group_id,
-                                signed int len, unsigned char *buffer, int 
bufsize)
+int hip_calculate_shared_secret(uint8_t *public_value,
+                                uint8_t group_id,
+                                signed int len,
+                                unsigned char *buffer,
+                                int bufsize)
 {
-       int err = 0;
-       DH *tmp;
-
-        /*
-        * First check that we have the key available.
-        * Then encode it into the buffer
-        */
-
-       if (dh_table[group_id] == NULL) {
-               tmp = hip_generate_dh_key(group_id);
-               _HIP_DEBUG("Generating key\n");
-               dh_table[group_id] = tmp;
-
-               if (dh_table[group_id] == NULL) {
-                       HIP_ERROR("Unsupported DH group: %d\n", group_id);
-                       return -1;
-               }
-       }
-
-       err = hip_gen_dh_shared_key(dh_table[group_id], public_value,
-                                   len, buffer, bufsize);
-       if (err < 0) {
-                HIP_ERROR("Could not create shared secret\n");
-               return -1;
+    int err = 0;
+    DH *tmp;
+
+    /*
+     * First check that we have the key available.
+     * Then encode it into the buffer
+     */
+
+    if (dh_table[group_id] == NULL) {
+        tmp                = hip_generate_dh_key(group_id);
+        _HIP_DEBUG("Generating key\n");
+        dh_table[group_id] = tmp;
+
+        if (dh_table[group_id] == NULL) {
+            HIP_ERROR("Unsupported DH group: %d\n", group_id);
+            return -1;
         }
-
-       _HIP_HEXDUMP("Peer DH pubkey", public_value, len);
-       _HIP_HEXDUMP("Shared key", buffer, bufsize);
-
-       return err;
+    }
+
+    err = hip_gen_dh_shared_key(dh_table[group_id], public_value,
+                                len, buffer, bufsize);
+    if (err < 0) {
+        HIP_ERROR("Could not create shared secret\n");
+        return -1;
+    }
+
+    _HIP_HEXDUMP("Peer DH pubkey", public_value, len);
+    _HIP_HEXDUMP("Shared key", buffer, bufsize);
+
+    return err;
 }
 
-
 /**
  * hip_regen_dh_keys - Regenerate Diffie-Hellman keys for HIP
  * @param bitmask Mask of groups to generate.
@@ -115,56 +117,56 @@
  */
 static void hip_regen_dh_keys(u32 bitmask)
 {
-       DH *tmp,*okey;
-       int maxmask,i;
-       int cnt = 0;
-
-       /* if MAX_DH_GROUP_ID = 4 --> maxmask = 0...01111 */
-       maxmask = (1 << (HIP_MAX_DH_GROUP_ID+1)) - 1;
-       bitmask &= maxmask;
-
-       for(i=1; i<=HIP_MAX_DH_GROUP_ID; i++) {
-               if (bitmask & (1 << i)) {
-                       tmp = hip_generate_dh_key(i);
-                       if (!tmp) {
-                               HIP_INFO("Error while generating group: 
%d\n",i);
-                               continue;
-                       }
-
-                       okey = dh_table[i];
-                       dh_table[i] = tmp;
-
-                       hip_free_dh(okey);
-
-                       cnt++;
-
-                       HIP_DEBUG("DH key for group %d generated\n",i);
-               } 
-       }
-       HIP_DEBUG("%d keys generated\n",cnt);
+    DH *tmp, *okey;
+    int maxmask, i;
+    int cnt = 0;
+
+    /* if MAX_DH_GROUP_ID = 4 --> maxmask = 0...01111 */
+    maxmask  = (1 << (HIP_MAX_DH_GROUP_ID + 1)) - 1;
+    bitmask &= maxmask;
+
+    for (i = 1; i <= HIP_MAX_DH_GROUP_ID; i++) {
+        if (bitmask & (1 << i)) {
+            tmp = hip_generate_dh_key(i);
+            if (!tmp) {
+                HIP_INFO("Error while generating group: %d\n", i);
+                continue;
+            }
+
+            okey        = dh_table[i];
+            dh_table[i] = tmp;
+
+            hip_free_dh(okey);
+
+            cnt++;
+
+            HIP_DEBUG("DH key for group %d generated\n", i);
+        }
+    }
+    HIP_DEBUG("%d keys generated\n", cnt);
 }
 
-void hip_dh_uninit(void) {
-       int i;
-       for(i=1;i<HIP_MAX_DH_GROUP_ID;i++) {
-               if (dh_table[i] != NULL) {
-                       hip_free_dh(dh_table[i]);
-                       dh_table[i] = NULL;
-               }
-       }       
+void hip_dh_uninit(void)
+{
+    int i;
+    for (i = 1; i < HIP_MAX_DH_GROUP_ID; i++) {
+        if (dh_table[i] != NULL) {
+            hip_free_dh(dh_table[i]);
+            dh_table[i] = NULL;
+        }
+    }
 }
 
 int hip_init_cipher(void)
 {
-       u32 supported_groups;
-
-       supported_groups = (1 << HIP_DH_OAKLEY_1 |
-                            1 << HIP_DH_OAKLEY_5 |
-                           1 << HIP_DH_384);
-
-       HIP_DEBUG("Generating DH keys\n");
-       hip_regen_dh_keys(supported_groups);
-
-       return 1;
+    u32 supported_groups;
+
+    supported_groups = (1 << HIP_DH_OAKLEY_1 |
+                        1 << HIP_DH_OAKLEY_5 |
+                        1 << HIP_DH_384);
+
+    HIP_DEBUG("Generating DH keys\n");
+    hip_regen_dh_keys(supported_groups);
+
+    return 1;
 }
-

=== modified file 'hipd/dh.c.doxyme'
--- hipd/dh.c.doxyme    2009-12-12 10:44:54 +0000
+++ hipd/dh.c.doxyme    2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,7 +45,7 @@
 
 
 /**
- * hip_calculate_shared_secret 
+ * hip_calculate_shared_secret
  *
  *
  * @param public_value
@@ -52,44 +53,43 @@
  * @param len
  * @param buffer
  * @param bufsize
- * @return 
- **/
-
-
-/**
- * hip_dh_uninit 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_init_cipher 
- *
- *
- * @param void
- * @return 
- **/
-
-
-/**
- * hip_insert_dh 
+ * @return
+ **/
+
+
+/**
+ * hip_dh_uninit
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_init_cipher
+ *
+ *
+ * @param void
+ * @return
+ **/
+
+
+/**
+ * hip_insert_dh
  *
  *
  * @param buffer
  * @param bufsize
  * @param group_id
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_regen_dh_keys 
+ * hip_regen_dh_keys
  *
  *
  * @param bitmask
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/dh.h'
--- hipd/dh.h   2010-01-19 11:26:32 +0000
+++ hipd/dh.h   2010-02-10 22:32:46 +0000
@@ -7,8 +7,8 @@
 int hip_insert_dh(u8 *buffer, int bufsize, int group_id);
 void hip_dh_uninit(void);
 int hip_calculate_shared_secret(uint8_t *public_value,
-                               uint8_t group_id,
-                               signed int len,
-                               unsigned char *buffer, 
-                               int bufsize);
+                                uint8_t group_id,
+                                signed int len,
+                                unsigned char *buffer,
+                                int bufsize);
 #endif /* HIP_SECURITY_H */

=== modified file 'hipd/dh.h.doxyme'
--- hipd/dh.h.doxyme    2009-12-12 10:44:54 +0000
+++ hipd/dh.h.doxyme    2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/dh.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/dht.c'
--- hipd/dht.c  2010-01-29 20:54:15 +0000
+++ hipd/dht.c  2010-02-10 22:32:46 +0000
@@ -2,10 +2,10 @@
  * @file hipd/dht.c
  *
  * <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Summary on the usage
  *
- * @brief All the necessary functionality for DHT (OpenDHT/OpenLookup) usage. 
+ * @brief All the necessary functionality for DHT (OpenDHT/OpenLookup) usage.
  *
  * @author: Samu Varjonen <samu.varjonen@xxxxxxx>
  **/
@@ -16,9 +16,13 @@
 static void hip_publish_hit(char *, char *);
 static int hip_publish_addr(char *);
 static int hip_dht_put_hdrr(unsigned char *, unsigned char *, int, int, void 
*);
+
 #ifdef CONFIG_HIP_AGENT
 static int hip_prepare_send_cert_put(unsigned char *, unsigned char *, int, 
int);
-static int hip_sqlite_callback(void *NotUsed, int argc, char **argv, char 
**azColName);
+static int hip_sqlite_callback(void *NotUsed,
+                               int argc,
+                               char **argv,
+                               char **azColName);
 #endif /* CONFIG_HIP_AGENT */
 
 /**
@@ -30,59 +34,54 @@
  *
  * @return void
  **/
-void 
-hip_init_dht_sockets(int *socket, int *socket_status)
+void hip_init_dht_sockets(int *socket, int *socket_status)
 {
-       if (hip_opendht_inuse == SO_HIP_DHT_ON) 
-       {
-               if (*socket_status == STATE_OPENDHT_IDLE) 
-               {
-                       HIP_DEBUG("Connecting to the DHT with socket no: %d 
\n", *socket);
-                       if (*socket < 1)
-                               *socket = init_dht_gateway_socket_gw(*socket, 
-                                                                    
opendht_serving_gateway);
-                       opendht_error = 0;
-                       opendht_error = connect_dht_gateway(*socket, 
-                                                           
opendht_serving_gateway, 0); 
-               }
-               if (opendht_error == EINPROGRESS) 
-               {
-                       *socket_status = STATE_OPENDHT_WAITING_CONNECT; 
-                       /* connect not ready */
-                       HIP_DEBUG("OpenDHT connect unfinished. Socket No: %d 
\n",*socket);
-               }
-               else if (opendht_error > -1 && opendht_error != EINPROGRESS)
-               {
-                       *socket_status = STATE_OPENDHT_START_SEND ;
-               }               
-       }
+    if (hip_opendht_inuse == SO_HIP_DHT_ON) {
+        if (*socket_status == STATE_OPENDHT_IDLE) {
+            HIP_DEBUG("Connecting to the DHT with socket no: %d \n", *socket);
+            if (*socket < 1) {
+                *socket = init_dht_gateway_socket_gw(*socket,
+                                                     opendht_serving_gateway);
+            }
+            opendht_error = 0;
+            opendht_error = connect_dht_gateway(*socket,
+                                                opendht_serving_gateway, 0);
+        }
+        if (opendht_error == EINPROGRESS) {
+            *socket_status = STATE_OPENDHT_WAITING_CONNECT;
+            /* connect not ready */
+            HIP_DEBUG("OpenDHT connect unfinished. Socket No: %d \n", *socket);
+        } else if (opendht_error > -1 && opendht_error != EINPROGRESS)   {
+            *socket_status = STATE_OPENDHT_START_SEND;
+        }
+    }
 }
 
 /**
- * hip_register_to_dht - Insert mapping for local host IP addresses to HITs to 
the queue.
+ * hip_register_to_dht - Insert mapping for local host IP addresses to HITs to
+ *                       the queue.
  *
  * @return void
  **/
-void 
-hip_register_to_dht(void)
-{  
-       int pub_addr_ret = 0, err = 0;
-       char tmp_hit_str[INET6_ADDRSTRLEN + 2];
-       struct in6_addr tmp_hit;
-       
-       /* Check if OpenDHT is off then out_err*/
-       HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
-       
-       HIP_IFEL(hip_get_default_hit(&tmp_hit), -1, "No HIT found\n");
-
-       hip_convert_hit_to_str(&tmp_hit, NULL, opendht_current_key);
-       hip_convert_hit_to_str(&tmp_hit, NULL, tmp_hit_str);
-
-       hip_publish_hit(opendht_name_mapping, tmp_hit_str);
-       pub_addr_ret = hip_publish_addr(tmp_hit_str);
-
- out_err:
-       return;
+void hip_register_to_dht(void)
+{
+    int pub_addr_ret = 0, err = 0;
+    char tmp_hit_str[INET6_ADDRSTRLEN + 2];
+    struct in6_addr tmp_hit;
+
+    /* Check if OpenDHT is off then out_err*/
+    HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
+
+    HIP_IFEL(hip_get_default_hit(&tmp_hit), -1, "No HIT found\n");
+
+    hip_convert_hit_to_str(&tmp_hit, NULL, opendht_current_key);
+    hip_convert_hit_to_str(&tmp_hit, NULL, tmp_hit_str);
+
+    hip_publish_hit(opendht_name_mapping, tmp_hit_str);
+    pub_addr_ret = hip_publish_addr(tmp_hit_str);
+
+out_err:
+    return;
 }
 
 /**
@@ -94,78 +93,79 @@
  *
  * @return void
  **/
-static void 
-hip_publish_hit(char *hostname, char *tmp_hit_str)
+static void hip_publish_hit(char *hostname, char *tmp_hit_str)
 {
-       int err = 0;
-       /* Assuming HIP Max packet length, max for DHT put 
-          while it may be too long for OpenDHT */
-       char out_packet[HIP_MAX_PACKET]; 
-       
-       HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
-       
-       memset(out_packet, '\0', HIP_MAX_PACKET);
-       opendht_error = opendht_put((unsigned char *)hostname,
-                                   (unsigned char *)tmp_hit_str, 
-                                   (unsigned char *)opendht_host_name,
-                                   opendht_serving_gateway_port,
-                                   opendht_serving_gateway_ttl,out_packet);
-       
+    int err = 0;
+    /* Assuming HIP Max packet length, max for DHT put
+     * while it may be too long for OpenDHT */
+    char out_packet[HIP_MAX_PACKET];
+
+    HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
+
+    memset(out_packet, '\0', HIP_MAX_PACKET);
+    opendht_error = opendht_put((unsigned char *) hostname,
+                                (unsigned char *) tmp_hit_str,
+                                (unsigned char *) opendht_host_name,
+                                opendht_serving_gateway_port,
+                                opendht_serving_gateway_ttl, out_packet);
+
+    if (opendht_error < 0) {
+        HIP_DEBUG("HTTP packet creation for FDQN->HIT PUT failed.\n");
+    } else {
+        HIP_DEBUG("Sending FDQN->HIT PUT packet to queue. Packet Length: %d\n",
+                  strlen(out_packet) + 1);
+        opendht_error = hip_write_to_dht_queue(out_packet,
+                                               strlen(out_packet) + 1);
         if (opendht_error < 0) {
-               HIP_DEBUG("HTTP packet creation for FDQN->HIT PUT failed.\n");
-       } else {
-               HIP_DEBUG("Sending FDQN->HIT PUT packet to queue. Packet 
Length: %d\n",strlen(out_packet)+1);
-               opendht_error = 
hip_write_to_dht_queue(out_packet,strlen(out_packet)+1);
-               if (opendht_error < 0) {
-                       HIP_DEBUG ("Failed to insert FDQN->HIT PUT data in 
queue \n");
-               }
-       }
+            HIP_DEBUG("Failed to insert FDQN->HIT PUT data in queue \n");
+        }
+    }
 out_err:
-        return;
+    return;
 }
 
 /**
  * hip_publish address - This function creates HTTP packet for publish address
  *                       and writes it in the queue for sending
  *
- * @param *tmp_hit_str 
+ * @param *tmp_hit_str
  *
  * @return 0 on success and -1 on errors
  *
  * @note Keep in mind that id opendht is not enabled this function returns zero
  **/
-static int 
-hip_publish_addr(char * tmp_hit_str)
+static int hip_publish_addr(char *tmp_hit_str)
 {
-        char out_packet[HIP_MAX_PACKET];
-        /* Assuming HIP Max packet length, max for DHT put
-           while OpenDHT max size may be lower */
-        int err = 0;
-       
-        HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
-       
-        memset(out_packet, '\0', HIP_MAX_PACKET);
-        opendht_error = hip_dht_put_hdrr((unsigned char *)tmp_hit_str, 
-                                        (unsigned char *)opendht_host_name,
-                                        opendht_serving_gateway_port,
-                                        
opendht_serving_gateway_ttl,out_packet);
+    char out_packet[HIP_MAX_PACKET];
+    /* Assuming HIP Max packet length, max for DHT put
+     * while OpenDHT max size may be lower */
+    int err = 0;
+
+    HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
+
+    memset(out_packet, '\0', HIP_MAX_PACKET);
+    opendht_error = hip_dht_put_hdrr((unsigned char *) tmp_hit_str,
+                                     (unsigned char *) opendht_host_name,
+                                     opendht_serving_gateway_port,
+                                     opendht_serving_gateway_ttl, out_packet);
+    if (opendht_error < 0) {
+        HIP_DEBUG("HTTP packet creation for HIT->IP PUT failed.\n");
+        return -1;
+    } else {
+        HIP_DEBUG("Sending HTTP HIT->IP PUT packet to queue.\n");
+        opendht_error = hip_write_to_dht_queue(out_packet, strlen(out_packet) 
+ 1);
         if (opendht_error < 0) {
-                HIP_DEBUG("HTTP packet creation for HIT->IP PUT failed.\n");
-                return -1;
-        } else {
-                HIP_DEBUG("Sending HTTP HIT->IP PUT packet to queue.\n");
-                opendht_error = 
hip_write_to_dht_queue(out_packet,strlen(out_packet)+1);
-                if (opendht_error < 0) {
-                        HIP_DEBUG ("Failed to insert HIT->IP PUT data in queue 
\n");
-                        return -1;
-                }
+            HIP_DEBUG("Failed to insert HIT->IP PUT data in queue \n");
+            return -1;
         }
+    }
 out_err:
-        return 0;
+    return 0;
 }
 
 /**
- * hip_dht_put_hdrr - Function that will create the hdrr packet for the put 
operation used with the DHT
+ * hip_dht_put_hdrr - Function that will create the hdrr packet for the put
+ *                    operation used with the DHT
  *
  * @param *key Key that is used for the put
  * @param *host Host that will be written to the HTTP header HOST option
@@ -175,150 +175,153 @@
  *
  * @return int 0 on succes, -1 on error
  **/
-static int 
-hip_dht_put_hdrr(unsigned char * key,
-                unsigned char * host,
-                int opendht_port,
-                int opendht_ttl,void *put_packet)
-{ 
-       int err = 0;
-       int key_len = 0, value_len = 0;
-       struct hip_common *hdrr_msg = NULL;
-       char tmp_key[21];
-       struct in6_addr addrkey;
-
-       memset(&addrkey, 0, sizeof(&addrkey));
-       
-       hdrr_msg = hip_msg_alloc();
-       hip_build_network_hdr(hdrr_msg, HIP_HDRR, 0, &addrkey, &addrkey);
-       value_len = hip_build_locators_old(hdrr_msg, 0);
-       
-       HIP_IFEL((inet_pton(AF_INET6, (char *)key, &addrkey.s6_addr) == 0), -1,
-                "Lookup for HOST ID structure from HI DB failed as key 
provided is not a HIT\n");
-       
-       /* The function below builds and appends Host Id
-        * and signature to the msg */
-       /*
-        * Setting two message parameters as stated in RFC for HDRR
-        * First one is sender's HIT
-        * Second one is message type, which is draft is assumed to be 20 but 
it is already used so using 22
-        * XXTODO check the msg type -Samu
-        */
-       ipv6_addr_copy(&hdrr_msg->hits, &addrkey);
-       
-       err = hip_build_host_id_and_signature(hdrr_msg, &addrkey);
-       if( err != 0) {
-               HIP_DEBUG("Appending Host ID and Signature to HDRR failed.\n");
-               goto out_err;
-       }
-       
-       _HIP_DUMP_MSG(hdrr_msg);
-       key_len = opendht_handle_key(key, tmp_key);
-       value_len = hip_get_msg_total_len(hdrr_msg);
-       _HIP_DEBUG("Value len %d\n",value_len);
-       
-       /* Debug info can be later removed from cluttering the logs */
-       hip_print_locator_addresses(hdrr_msg);
-       
-       /* store for removals*/
-       if (opendht_current_hdrr) {
-               free(opendht_current_hdrr);
-               opendht_current_hdrr = NULL;
-       }
-       opendht_current_hdrr = hip_msg_alloc();
-       memcpy(opendht_current_hdrr, hdrr_msg, sizeof(hip_common_t));
-       
-       /* Put operation HIT->IP */
-       if (build_packet_put_rm((unsigned char *)tmp_key,
-                               key_len,
-                               (unsigned char *)hdrr_msg,
-                               value_len,
-                               opendht_hdrr_secret,
-                               40,
-                               opendht_port,
-                               (unsigned char *)host,
-                               put_packet, opendht_ttl) != 0) {
-               HIP_DEBUG("Put packet creation failed.\n");
-               err = -1;
-       }
-       HIP_DEBUG("Host address in OpenDHT put locator : %s\n", host);
-       HIP_DEBUG("Actual OpenDHT send starts here\n");
-       err = 0;
+static int hip_dht_put_hdrr(unsigned char *key,
+                            unsigned char *host,
+                            int opendht_port,
+                            int opendht_ttl, void *put_packet)
+{
+    int err                     = 0;
+    int key_len                 = 0, value_len = 0;
+    struct hip_common *hdrr_msg = NULL;
+    char tmp_key[21];
+    struct in6_addr addrkey;
+
+    memset(&addrkey, 0, sizeof(&addrkey));
+
+    hdrr_msg  = hip_msg_alloc();
+    hip_build_network_hdr(hdrr_msg, HIP_HDRR, 0, &addrkey, &addrkey);
+    value_len = hip_build_locators_old(hdrr_msg, 0);
+
+    HIP_IFEL((inet_pton(AF_INET6, (char *) key, &addrkey.s6_addr) == 0), -1,
+             "Lookup for HOST ID structure from HI DB failed as key provided 
is not a HIT\n");
+
+    /* The function below builds and appends Host Id
+     * and signature to the msg */
+    /*
+     * Setting two message parameters as stated in RFC for HDRR
+     * First one is sender's HIT
+     * Second one is message type, which is draft is assumed to be 20 but it
+     * is already used so using 22
+     * XX TODO check the msg type -Samu
+     */
+    ipv6_addr_copy(&hdrr_msg->hits, &addrkey);
+
+    err = hip_build_host_id_and_signature(hdrr_msg, &addrkey);
+    if (err != 0) {
+        HIP_DEBUG("Appending Host ID and Signature to HDRR failed.\n");
+        goto out_err;
+    }
+
+    _HIP_DUMP_MSG(hdrr_msg);
+    key_len   = opendht_handle_key(key, tmp_key);
+    value_len = hip_get_msg_total_len(hdrr_msg);
+    _HIP_DEBUG("Value len %d\n", value_len);
+
+    /* Debug info can be later removed from cluttering the logs */
+    hip_print_locator_addresses(hdrr_msg);
+
+    /* store for removals*/
+    if (opendht_current_hdrr) {
+        free(opendht_current_hdrr);
+        opendht_current_hdrr = NULL;
+    }
+    opendht_current_hdrr = hip_msg_alloc();
+    memcpy(opendht_current_hdrr, hdrr_msg, sizeof(hip_common_t));
+
+    /* Put operation HIT->IP */
+    if (build_packet_put_rm((unsigned char *) tmp_key,
+                            key_len,
+                            (unsigned char *) hdrr_msg,
+                            value_len,
+                            opendht_hdrr_secret,
+                            40,
+                            opendht_port,
+                            (unsigned char *) host,
+                            put_packet, opendht_ttl) != 0) {
+        HIP_DEBUG("Put packet creation failed.\n");
+        err = -1;
+    }
+    HIP_DEBUG("Host address in OpenDHT put locator : %s\n", host);
+    HIP_DEBUG("Actual OpenDHT send starts here\n");
+    err = 0;
 out_err:
-       if (hdrr_msg)
-               HIP_FREE(hdrr_msg);
-       return(err);
+    if (hdrr_msg) {
+        HIP_FREE(hdrr_msg);
+    }
+    return err;
 }
 
 /**
  * hip_send_queue_data - This function reads the data from hip_queue
  *                   and sends it to the lookup service for publishing
- * 
+ *
  * @param *socket Socket to be initialized
  * @param *socket_status Updates the status of the socket after every socket 
oepration
  *
  * @return int 0 on success, -1 on errors
  **/
-static int 
-hip_send_queue_data(int *socket, int *socket_status)
+static int hip_send_queue_data(int *socket, int *socket_status)
 {
-       int err = 0;
-       
-       char packet[2048];
-       HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
-       
-       if (*socket_status == STATE_OPENDHT_IDLE) {
-               HIP_DEBUG("Connecting to the DHT with socket no: %d \n", 
*socket);
-               if (*socket < 1)
-                       *socket = init_dht_gateway_socket_gw(*socket, 
opendht_serving_gateway);
-               opendht_error = 0;
-               opendht_error = connect_dht_gateway(*socket, 
-                                                   opendht_serving_gateway, 
0); 
-               if (opendht_error == -1) {
-                       HIP_DEBUG("Error connecting to the DHT. Socket No: 
%d\n", *socket);
-                       hip_opendht_error_count++;
-               } else if (opendht_error > -1 && opendht_error != EINPROGRESS) {
-                       /*Get packet from queue, if there then proceed*/
-                       memset(packet, '\0', sizeof(packet));
-                       opendht_error = hip_read_from_dht_queue(packet);
-                       _HIP_DEBUG("Packet: %s\n",packet);
-                       if (opendht_error < 0 && strlen(packet)>0) {
-                               HIP_DEBUG("Packet reading from queue 
failed.\n");
-                       } else {
-                               opendht_error = opendht_send(*socket,packet);
-                               if (opendht_error < 0) {
-                                       HIP_DEBUG("Error sending data to the 
DHT. Socket No: %d\n",
-                                                 *socket);
-                                       hip_opendht_error_count++;
-                               } else
-                                       *socket_status = 
STATE_OPENDHT_WAITING_ANSWER;
-                       }
-               } 
-               if (opendht_error == EINPROGRESS) {
-                       *socket_status = STATE_OPENDHT_WAITING_CONNECT; 
-                       /* connect not ready */
-                       HIP_DEBUG("OpenDHT connect unfinished. Socket No: %d 
\n",*socket);
-               }
-       } else if (*socket_status == STATE_OPENDHT_START_SEND) {
-               /* connect finished send the data */
-               /*Get packet from queue, if there then proceed*/
-               memset(packet, '\0', sizeof(packet));
-               opendht_error = hip_read_from_dht_queue(packet);
-               _HIP_DEBUG("Packet: %s\n",packet);
-               if (opendht_error < 0  && strlen (packet)>0) {
-                       HIP_DEBUG("Packet reading from queue failed.\n");
-               } else {
-                       opendht_error = opendht_send(*socket,packet);
-                       if (opendht_error < 0) {
-                               HIP_DEBUG("Error sending data to the DHT. 
Socket No: %d\n", 
-                                         *socket);
-                               hip_opendht_error_count++;
-                       } else
-                               *socket_status = STATE_OPENDHT_WAITING_ANSWER;
-               }
-       }
- out_err:
-       return err;
+    int err = 0;
+
+    char packet[2048];
+    HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
+
+    if (*socket_status == STATE_OPENDHT_IDLE) {
+        HIP_DEBUG("Connecting to the DHT with socket no: %d \n", *socket);
+        if (*socket < 1) {
+            *socket = init_dht_gateway_socket_gw(*socket, 
opendht_serving_gateway);
+        }
+        opendht_error = 0;
+        opendht_error = connect_dht_gateway(*socket,
+                                            opendht_serving_gateway, 0);
+        if (opendht_error == -1) {
+            HIP_DEBUG("Error connecting to the DHT. Socket No: %d\n", *socket);
+            hip_opendht_error_count++;
+        } else if (opendht_error > -1 && opendht_error != EINPROGRESS) {
+            /*Get packet from queue, if there then proceed*/
+            memset(packet, '\0', sizeof(packet));
+            opendht_error = hip_read_from_dht_queue(packet);
+            _HIP_DEBUG("Packet: %s\n", packet);
+            if (opendht_error < 0 && strlen(packet) > 0) {
+                HIP_DEBUG("Packet reading from queue failed.\n");
+            } else {
+                opendht_error = opendht_send(*socket, packet);
+                if (opendht_error < 0) {
+                    HIP_DEBUG("Error sending data to the DHT. Socket No: %d\n",
+                              *socket);
+                    hip_opendht_error_count++;
+                } else {
+                    *socket_status = STATE_OPENDHT_WAITING_ANSWER;
+                }
+            }
+        }
+        if (opendht_error == EINPROGRESS) {
+            *socket_status = STATE_OPENDHT_WAITING_CONNECT;
+            /* connect not ready */
+            HIP_DEBUG("OpenDHT connect unfinished. Socket No: %d \n", *socket);
+        }
+    } else if (*socket_status == STATE_OPENDHT_START_SEND) {
+        /* connect finished send the data */
+        /*Get packet from queue, if there then proceed*/
+        memset(packet, '\0', sizeof(packet));
+        opendht_error = hip_read_from_dht_queue(packet);
+        _HIP_DEBUG("Packet: %s\n", packet);
+        if (opendht_error < 0  && strlen(packet) > 0) {
+            HIP_DEBUG("Packet reading from queue failed.\n");
+        } else {
+            opendht_error = opendht_send(*socket, packet);
+            if (opendht_error < 0) {
+                HIP_DEBUG("Error sending data to the DHT. Socket No: %d\n",
+                          *socket);
+                hip_opendht_error_count++;
+            } else {
+                *socket_status = STATE_OPENDHT_WAITING_ANSWER;
+            }
+        }
+    }
+out_err:
+    return err;
 }
 
 #ifdef CONFIG_HIP_AGENT
@@ -333,29 +336,31 @@
  *
  * @return 0 on success, negative value on error
  **/
-static int 
-hip_prepare_send_cert_put(unsigned char * key, unsigned char * value, int 
key_len, int valuelen)
+static int hip_prepare_send_cert_put(unsigned char *key,
+                                     unsigned char *value,
+                                     int key_len,
+                                     int valuelen)
 {
-
-       int value_len = valuelen;/*length of certificate*/
-       char put_packet[2048];
-       if (build_packet_put((unsigned char *)key,
-                            key_len,
-                            (unsigned char *)value,
-                            value_len,
-                            opendht_serving_gateway_port,
-                            (unsigned char *)opendht_host_name,
-                            (char*)put_packet, opendht_serving_gateway_ttl)
-           != 0)
-       {
-               HIP_DEBUG("Put packet creation failed.\n");
-               return(-1);
-       }
-       opendht_error = hip_write_to_dht_queue(put_packet,strlen(put_packet)+1);
-       if (opendht_error < 0)
-               HIP_DEBUG("Failed to insert CERT PUT data in queue \n");
-       return 0;
+    int value_len = valuelen;    /*length of certificate*/
+    char put_packet[2048];
+    if (build_packet_put((unsigned char *) key,
+                         key_len,
+                         (unsigned char *) value,
+                         value_len,
+                         opendht_serving_gateway_port,
+                         (unsigned char *) opendht_host_name,
+                         (char *) put_packet, opendht_serving_gateway_ttl)
+        != 0) {
+        HIP_DEBUG("Put packet creation failed.\n");
+        return -1;
+    }
+    opendht_error = hip_write_to_dht_queue(put_packet, strlen(put_packet) + 1);
+    if (opendht_error < 0) {
+        HIP_DEBUG("Failed to insert CERT PUT data in queue \n");
+    }
+    return 0;
 }
+
 #endif /* CONFIG_HIP_AGENT */
 
 #ifdef CONFIG_HIP_AGENT
@@ -371,210 +376,223 @@
  *
  * @return 0 on success, -1 on errors
  **/
-static int 
-hip_sqlite_callback(void *NotUsed, int argc, char **argv, char **azColName) {
-       int i;
-       struct in6_addr lhit, rhit;
-       unsigned char conc_hits_key[21] ;
-       int err = 0 ;
-       char cert[512]; /*Should be size of certificate*/
+static int hip_sqlite_callback(void *NotUsed,
+                               int argc,
+                               char **argv,
+                               char **azColName)
+{
+    int i;
+    struct in6_addr lhit, rhit;
+    unsigned char conc_hits_key[21];
+    int err = 0;
+    char cert[512];     /*Should be size of certificate*/
 
-       memset(conc_hits_key, '\0', 21);
-       for(i=0; i<argc; i++){
-               _HIP_DEBUG("%s = %s\n", azColName[i], argv[i] ? argv[i] : 
"NULL");
-               if (!strcmp(azColName[i],"lhit"))
-               {
-               /*convret hit to inet6_addr*/
-               err = inet_pton(AF_INET6, (char *)argv[i], &lhit.s6_addr);
-               }
-               else if (!strcmp(azColName[i],"rhit"))
-               {
-               err = inet_pton(AF_INET6, (char *)argv[i], &rhit.s6_addr);
-               /*convret hit to inet6_addr*/
-               }
-               else if (!strcmp(azColName[i],"cert"))
-               {
-                       if(!(char *)argv)
-                               err = -1 ;
-                       else
-                       memcpy(cert, (char *)argv[i], 512/*should be size of 
certificate*/);
-               }
-       }
-       if(err)
-       {
-               int keylen = 0 ;
-               keylen = handle_cert_key(&lhit, &rhit, conc_hits_key);
-               /*send key-value pair to dht*/
-               if (keylen)
-               {
-                 err = hip_prepare_send_cert_put(conc_hits_key, (unsigned char 
*) cert, keylen, sizeof(cert) );
-               }
-               else
-               {
-                       HIP_DEBUG ("Unable to handle publish cert key\n");
-                       err = -1 ;
-               }
-       }
-       return err;
+    memset(conc_hits_key, '\0', 21);
+    for (i = 0; i < argc; i++) {
+        _HIP_DEBUG("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+        if (!strcmp(azColName[i], "lhit")) {
+            /*convret hit to inet6_addr*/
+            err = inet_pton(AF_INET6, (char *) argv[i], &lhit.s6_addr);
+        } else if (!strcmp(azColName[i], "rhit"))    {
+            err = inet_pton(AF_INET6, (char *) argv[i], &rhit.s6_addr);
+            /*convret hit to inet6_addr*/
+        } else if (!strcmp(azColName[i], "cert"))    {
+            if (!(char *) argv) {
+                err = -1;
+            } else {
+                memcpy(cert, (char *) argv[i], 512 /*should be size of 
certificate*/);
+            }
+        }
+    }
+    if (err) {
+        int keylen = 0;
+        keylen = handle_cert_key(&lhit, &rhit, conc_hits_key);
+        /*send key-value pair to dht*/
+        if (keylen) {
+            err = hip_prepare_send_cert_put(conc_hits_key,
+                                            (unsigned char *) cert,
+                                            keylen,
+                                            sizeof(cert));
+        } else {
+            HIP_DEBUG("Unable to handle publish cert key\n");
+            err = -1;
+        }
+    }
+    return err;
 }
+
 #endif /* CONFIG_HIP_AGENT */
 
 /**
- * hip_publish_certificates - Reads the daemon database and then publishes 
certificate 
+ * hip_publish_certificates - Reads the daemon database and then publishes 
certificate
  *                        after regular interval defined in hipd.h
  *
  * @return error value 0 on success and negative on error
  **/
-int 
-hip_publish_certificates(void)
+int hip_publish_certificates(void)
 {
 #ifdef CONFIG_HIP_AGENT
-        return hip_sqlite_select(daemon_db, 
HIP_CERT_DB_SELECT_HITS,hip_sqlite_callback);
+    return hip_sqlite_select(daemon_db,
+                             HIP_CERT_DB_SELECT_HITS,
+                             hip_sqlite_callback);
 #endif
-     return 0;
+    return 0;
 }
 
 /**
- * hip_dht_remove_current_hdrr - Reads the daemon database and then publishes 
certificate 
+ * hip_dht_remove_current_hdrr - Reads the daemon database and then publishes 
certificate
  *                               after regular interval defined in hipd.h
  *
  * @return void
  **/
-void 
-hip_dht_remove_current_hdrr(void) 
+void hip_dht_remove_current_hdrr(void)
 {
-       int err = 0;
-       int value_len = 0;
-       char remove_packet[2048];
-       HIP_DEBUG("Building a remove packet for the current HDRR and queuing 
it\n");
-                           
-       value_len = hip_get_msg_total_len(opendht_current_hdrr);
-       err = build_packet_rm((unsigned char *)opendht_current_key,
-                             strlen(opendht_current_key),
-                             (unsigned char *)opendht_current_hdrr,
-                             value_len, 
-                             opendht_hdrr_secret,
-                             40,
-                             opendht_serving_gateway_port,
-                             (unsigned char *) opendht_host_name,
-                             (char *) &remove_packet,
-                             opendht_serving_gateway_ttl);
-       if (err < 0) {
-               HIP_DEBUG("Error creating the remove current HDRR packet\n");
-               goto out_err;
-       }
-
-        err = hip_write_to_dht_queue(remove_packet, strlen(remove_packet) + 1);
-       if (err < 0) 
-               HIP_DEBUG ("Failed to insert HDRR remove data in queue \n");
-out_err:       
-       return;
+    int err       = 0;
+    int value_len = 0;
+    char remove_packet[2048];
+    HIP_DEBUG("Building a remove packet for the current HDRR and queuing 
it\n");
+
+    value_len = hip_get_msg_total_len(opendht_current_hdrr);
+    err       = build_packet_rm((unsigned char *) opendht_current_key,
+                                strlen(opendht_current_key),
+                                (unsigned char *) opendht_current_hdrr,
+                                value_len,
+                                opendht_hdrr_secret,
+                                40,
+                                opendht_serving_gateway_port,
+                                (unsigned char *) opendht_host_name,
+                                (char *) &remove_packet,
+                                opendht_serving_gateway_ttl);
+    if (err < 0) {
+        HIP_DEBUG("Error creating the remove current HDRR packet\n");
+        goto out_err;
+    }
+
+    err = hip_write_to_dht_queue(remove_packet, strlen(remove_packet) + 1);
+    if (err < 0) {
+        HIP_DEBUG("Failed to insert HDRR remove data in queue \n");
+    }
+out_err:
+    return;
 }
 
 /**
- * hip_verify_hdrr - This function verifies host id in the value (HDRR) 
against HIT used as a 
- *                   key for DHT and it also verifies the signature in HDRR 
This works on the 
- *                   hip_common message sent to the daemon modifies the 
message and sets the 
- *                   required flag if (or not) verified
- * 
+ * hip_verify_hdrr
+ *
+ * This function verifies host id in the value (HDRR) against HIT used as a
+ * key for DHT and it also verifies the signature in HDRR This works on the
+ * hip_common message sent to the daemon modifies the message and sets the
+ * required flag if (or not) verified
+ *
  * @param msg HDRR to be verified
  * @param addrkey HIT key used for lookup
  *
- * @return 0 on successful verification (OR of signature and host_id 
verification)
+ * @return 0 on successful verification
+ *          (OR of signature and host_id verification)
  **/
-int 
-hip_verify_hdrr(struct hip_common * msg, struct in6_addr * addrkey)
+int hip_verify_hdrr(struct hip_common *msg, struct in6_addr *addrkey)
 {
-       struct hip_host_id *hostid ; 
-       struct in6_addr *hit_from_hostid ;
-       struct in6_addr *hit_used_as_key ;
-       struct hip_hdrr_info *hdrr_info = NULL;
-       int alg = -1;
-       int is_hit_verified  = -1;
-       int is_sig_verified  = -1;
-       int err = 0 ;
-       void *key;
-               
-       hostid = hip_get_param (msg, HIP_PARAM_HOST_ID);
-       if ( addrkey == NULL)
-       {
-               hdrr_info = hip_get_param (msg, HIP_PARAM_HDRR_INFO);
-               hit_used_as_key = &hdrr_info->dht_key ; 
-       } else {
-               hit_used_as_key = addrkey;
-       }
-       
-       //Check for algo and call verify signature from pk.c
-       alg = hip_get_host_id_algo(hostid);
-        
-       /* Type of the hip msg in header has been modified to 
-        * user message type SO_HIP_VERIFY_DHT_HDRR_RESP , to
-        * get it here. Revert it back to HDRR to give it
-        * original shape as returned by the DHT and
-        *  then verify signature
-        */
-
-       hip_set_msg_type(msg,HIP_HDRR);
-       _HIP_DUMP_MSG (msg);
-       HIP_IFEL(!(hit_from_hostid = malloc(sizeof(struct in6_addr))), -1, 
"Malloc for HIT failed\n");
-       switch (alg) {
-       case HIP_HI_RSA:
-               key = hip_key_rr_to_rsa((struct hip_host_id_priv *)hostid, 0);
-               is_sig_verified = hip_rsa_verify(key, msg);
-               err = hip_rsa_host_id_to_hit (hostid, hit_from_hostid, 
HIP_HIT_TYPE_HASH100);
-               is_hit_verified = memcmp(hit_from_hostid, hit_used_as_key, 
sizeof(struct in6_addr)) ;
-               if (key)
-                       RSA_free(key);
-               break;
-       case HIP_HI_DSA:
-               key = hip_key_rr_to_dsa((struct hip_host_id_priv *)hostid, 0);
-               is_sig_verified = hip_dsa_verify(key, msg);
-               err = hip_dsa_host_id_to_hit (hostid, hit_from_hostid, 
HIP_HIT_TYPE_HASH100);
-               is_hit_verified = memcmp(hit_from_hostid, hit_used_as_key, 
sizeof(struct in6_addr)) ; 
-               if (key)
-                       DSA_free(key);
-               break;
-       default:
-               HIP_ERROR("Unsupported HI algorithm used cannot verify 
signature (%d)\n", alg);
-               break;
-       }
-       _HIP_DUMP_MSG (msg);
-       if (err != 0) {
-               HIP_DEBUG("Unable to convert host id to hit for host id 
verification \n");
-       }
-       if(hdrr_info) {
-               hdrr_info->hit_verified = is_hit_verified ;
-               hdrr_info->sig_verified = is_sig_verified ;
-       }
-       HIP_DEBUG ("Sig verified (0=true): %d\nHit Verified (0=true): %d \n"
-               ,is_sig_verified, is_hit_verified);
-       return (is_sig_verified | is_hit_verified) ;
+    struct hip_host_id *hostid;
+    struct in6_addr *hit_from_hostid;
+    struct in6_addr *hit_used_as_key;
+    struct hip_hdrr_info *hdrr_info = NULL;
+    int alg                         = -1;
+    int is_hit_verified             = -1;
+    int is_sig_verified             = -1;
+    int err                         = 0;
+    void *key;
+
+    hostid = hip_get_param(msg, HIP_PARAM_HOST_ID);
+    if (addrkey == NULL) {
+        hdrr_info       = hip_get_param(msg, HIP_PARAM_HDRR_INFO);
+        hit_used_as_key = &hdrr_info->dht_key;
+    } else {
+        hit_used_as_key = addrkey;
+    }
+
+    //Check for algo and call verify signature from pk.c
+    alg = hip_get_host_id_algo(hostid);
+
+    /* Type of the hip msg in header has been modified to
+     * user message type SO_HIP_VERIFY_DHT_HDRR_RESP , to
+     * get it here. Revert it back to HDRR to give it
+     * original shape as returned by the DHT and
+     *  then verify signature
+     */
+
+    hip_set_msg_type(msg, HIP_HDRR);
+    _HIP_DUMP_MSG(msg);
+    HIP_IFEL(!(hit_from_hostid = malloc(sizeof(struct in6_addr))),
+             -1,
+             "Malloc for HIT failed\n");
+    switch (alg) {
+    case HIP_HI_RSA:
+        key             = hip_key_rr_to_rsa((struct hip_host_id_priv *) 
hostid, 0);
+        is_sig_verified = hip_rsa_verify(key, msg);
+        err             = hip_rsa_host_id_to_hit(hostid,
+                                                 hit_from_hostid,
+                                                 HIP_HIT_TYPE_HASH100);
+        is_hit_verified = memcmp(hit_from_hostid,
+                                 hit_used_as_key,
+                                 sizeof(struct in6_addr));
+        if (key) {
+            RSA_free(key);
+        }
+        break;
+    case HIP_HI_DSA:
+        key             = hip_key_rr_to_dsa((struct hip_host_id_priv *) 
hostid, 0);
+        is_sig_verified = hip_dsa_verify(key, msg);
+        err             = hip_dsa_host_id_to_hit(hostid,
+                                                 hit_from_hostid,
+                                                 HIP_HIT_TYPE_HASH100);
+        is_hit_verified = memcmp(hit_from_hostid,
+                                 hit_used_as_key,
+                                 sizeof(struct in6_addr));
+        if (key) {
+            DSA_free(key);
+        }
+        break;
+    default:
+        HIP_ERROR("Unsupported HI algorithm used cannot verify signature 
(%d)\n",
+                  alg);
+        break;
+    }
+    _HIP_DUMP_MSG(msg);
+    if (err != 0) {
+        HIP_DEBUG("Unable to convert host id to hit for host id verification 
\n");
+    }
+    if (hdrr_info) {
+        hdrr_info->hit_verified = is_hit_verified;
+        hdrr_info->sig_verified = is_sig_verified;
+    }
+    HIP_DEBUG("Sig verified (0=true): %d\nHit Verified (0=true): %d \n"
+              , is_sig_verified, is_hit_verified);
+    return is_sig_verified | is_hit_verified;
 out_err:
 
-       return err;
+    return err;
 }
 
-/** 
- * hip_send_packet_to_lookup_from_queue - Calls to a function which sends 
+/**
+ * hip_send_packet_to_lookup_from_queue - Calls to a function which sends
  *                                    data from the queue to the dht
  *
  * @return void
  **/
-void 
-hip_send_packet_to_lookup_from_queue(void)
+void hip_send_packet_to_lookup_from_queue(void)
 {
-       int err = 0;
-
-       HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
-
-       HIP_DEBUG("DHT error count now %d/%d.\n", 
-                       hip_opendht_error_count, OPENDHT_ERROR_COUNT_MAX);
-       if (hip_opendht_error_count > OPENDHT_ERROR_COUNT_MAX) {
-               HIP_DEBUG("DHT error count reached resolving trying to change 
gateway\n");
-               hip_init_dht();
-       }
-       hip_send_queue_data (&hip_opendht_sock_fqdn, &hip_opendht_fqdn_sent);
-       hip_send_queue_data (&hip_opendht_sock_hit, &hip_opendht_hit_sent);
+    int err = 0;
+
+    HIP_IFE((hip_opendht_inuse != SO_HIP_DHT_ON), 0);
+
+    HIP_DEBUG("DHT error count now %d/%d.\n",
+              hip_opendht_error_count, OPENDHT_ERROR_COUNT_MAX);
+    if (hip_opendht_error_count > OPENDHT_ERROR_COUNT_MAX) {
+        HIP_DEBUG("DHT error count reached resolving trying to change 
gateway\n");
+        hip_init_dht();
+    }
+    hip_send_queue_data(&hip_opendht_sock_fqdn, &hip_opendht_fqdn_sent);
+    hip_send_queue_data(&hip_opendht_sock_hit, &hip_opendht_hit_sent);
 out_err:
-       return;
+    return;
 }

=== modified file 'hipd/dhtqueue.c'
--- hipd/dhtqueue.c     2010-01-31 21:29:06 +0000
+++ hipd/dhtqueue.c     2010-02-10 22:32:46 +0000
@@ -1,6 +1,6 @@
 /** @file
  *  HIP Queue
- *  
+ *
  * @author: Samu Varjonen <samu.varjonen@xxxxxxx>
  * @note:   Distributed under <a 
href="http://www.gnu.org/licenses/gpl.txt";>GNU/GPL</a>. This is actually a 
singly linked list. -samu
  */
@@ -12,27 +12,32 @@
 #include "dhtqueue.h"
 #include "lib/core/misc.h"
 
-struct hip_queue
-{
-       void * data;
-       int data_len;
+struct hip_queue {
+    void *data;
+    int   data_len;
 };
 
 HIP_HASHTABLE *hip_dht_queue = NULL;
 
-/** 
- * hip_dht_queue_hash - Hash callback for LHASH used to store the item into 
the hashtable
+/**
+ * hip_dht_queue_hash
+ *
+ * Hash callback for LHASH used to store the item into the hashtable
  *
  * @param item hip_queue structure to be hashed
- * 
+ *
  * @return the hash as unsigned long
  *
  * @note only for internal use in this file
  */
-unsigned long hip_dht_queue_hash(const struct hip_queue *item) {
-       uint8_t hash[HIP_AH_SHA_LEN];
-       hip_build_digest(HIP_DIGEST_SHA1, (void *)item, sizeof(struct 
hip_queue), hash);
-       return *((unsigned long *)(void*)hash); 
+unsigned long hip_dht_queue_hash(const struct hip_queue *item)
+{
+    uint8_t hash[HIP_AH_SHA_LEN];
+    hip_build_digest(HIP_DIGEST_SHA1,
+                     (void *) item,
+                     sizeof(struct hip_queue),
+                     hash);
+    return *((unsigned long *) (void *) hash);
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
@@ -46,157 +51,179 @@
  *
  * @return 0 on equal otherwise non-zero
  */
-static int hip_dht_queue_cmp(const struct hip_queue *item1, 
-                            const struct hip_queue *item2) {
-       return (strcmp((char *)item1, (char *)item2));
+static int hip_dht_queue_cmp(const struct hip_queue *item1,
+                             const struct hip_queue *item2)
+{
+    return strcmp((char *) item1, (char *) item2);
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
 static IMPLEMENT_LHASH_COMP_FN(hip_dht_queue, const struct hip_queue)
 
 /**
-* hip_init_dht_queue - This function initializes the opedht_queue
-* 
-* @return status of the operation 0 on success, -1 on failure
-*/
-int hip_init_dht_queue() {
-
-       if (hip_dht_queue == NULL)
-               hip_dht_queue = hip_ht_init(LHASH_HASH_FN(hip_dht_queue),
-                                           LHASH_COMP_FN(hip_dht_queue));
-       if (hip_dht_queue == NULL) 
-               return(-1);
-       return(0);
+ * hip_init_dht_queue - This function initializes the opedht_queue
+ *
+ * @return status of the operation 0 on success, -1 on failure
+ */
+int hip_init_dht_queue()
+{
+    if (hip_dht_queue == NULL) {
+        hip_dht_queue = hip_ht_init(LHASH_HASH_FN(hip_dht_queue),
+                                    LHASH_COMP_FN(hip_dht_queue));
+    }
+    if (hip_dht_queue == NULL) {
+        return -1;
+    }
+    return 0;
 }
 
 void hip_dht_queue_rec_free_doall(struct hip_queue *rec)
 {
-       struct hip_queue *deleted_rec;
-       
-       if(hip_dht_queue == NULL || rec == NULL)
-               return;
-       
-       /* Check if such element exist, and delete the pointer from
-          the hashtable. */
-       deleted_rec = list_del(rec, hip_dht_queue);
-
-       /* Free the memory allocated for the element. */
-       if (deleted_rec != NULL) {
-               /* We set the memory to '\0' because the user may still have a
-                  reference to the memory region that is freed here. */
-               memset(deleted_rec, '\0', sizeof(*deleted_rec));
-               free(deleted_rec);
-               HIP_DEBUG("Queue record deleted.\n");
-       }
+    struct hip_queue *deleted_rec;
+
+    if (hip_dht_queue == NULL || rec == NULL) {
+        return;
+    }
+
+    /* Check if such element exist, and delete the pointer from
+     * the hashtable. */
+    deleted_rec = list_del(rec, hip_dht_queue);
+
+    /* Free the memory allocated for the element. */
+    if (deleted_rec != NULL) {
+        /* We set the memory to '\0' because the user may still have a
+         * reference to the memory region that is freed here. */
+        memset(deleted_rec, '\0', sizeof(*deleted_rec));
+        free(deleted_rec);
+        HIP_DEBUG("Queue record deleted.\n");
+    }
 }
 
 /** A callback wrapper of the prototype required by @c lh_doall_arg(). */
 static IMPLEMENT_LHASH_DOALL_FN(hip_dht_queue_rec_free, struct hip_queue)
 
-void hip_dht_queue_uninit() {
+void hip_dht_queue_uninit()
+{
 #ifdef CONFIG_HIP_DHT
-       return;
+    return;
 #endif
 
-       if(hip_dht_queue == NULL)
-               return;
-       
-       hip_ht_doall(hip_dht_queue, 
(LHASH_DOALL_FN_TYPE)LHASH_DOALL_FN(hip_dht_queue_rec_free));
-       hip_ht_uninit(hip_dht_queue);
-       hip_dht_queue = NULL;
+    if (hip_dht_queue == NULL) {
+        return;
+    }
+
+    hip_ht_doall(hip_dht_queue,
+                 (LHASH_DOALL_FN_TYPE) LHASH_DOALL_FN(hip_dht_queue_rec_free));
+    hip_ht_uninit(hip_dht_queue);
+    hip_dht_queue = NULL;
 }
 
 /**
-* write_fifo_queue - This function writes data to the hip_queue structure
-*
-* @param write_data data to be written on the queue node
-* @param data_size_in_bytes size of the data sent
-*
-* @return status of the operation 0 on success, -1 on failure
-*/
-int hip_write_to_dht_queue (void *write_data, int data_size_in_bytes) {
-       void *temp_data = NULL;
-       struct hip_queue *new_item = NULL;
-       int err = -1;
-       
-       _HIP_DEBUG("Write, Items in dht_queue %d on enter\n", dht_queue_count);
-       temp_data = malloc(data_size_in_bytes);
-       HIP_IFEL((!temp_data), -1, "Failed to malloc memory for data\n");
-       memset(temp_data, 0, sizeof(data_size_in_bytes));
-       memcpy (temp_data,write_data, data_size_in_bytes);
-
-       new_item = (struct hip_queue *)malloc(sizeof(struct hip_queue));
-       HIP_IFEL((!new_item), -1, "Failed to malloc memory for queue new 
item\n");
-       memset(new_item, 0, sizeof(struct hip_queue));
-       new_item->data_len = data_size_in_bytes;
-       new_item->data = temp_data;                    
-       err = hip_ht_add(hip_dht_queue, new_item);
-       dht_queue_count = dht_queue_count + 1;
-
-       /* Debug line do not leave uncommented */
-       //hip_debug_print_dht_queue();
-       _HIP_DEBUG("Write, Items in dht_queue %d on exit\n", dht_queue_count);
-       
+ * write_fifo_queue - This function writes data to the hip_queue structure
+ *
+ * @param write_data data to be written on the queue node
+ * @param data_size_in_bytes size of the data sent
+ *
+ * @return status of the operation 0 on success, -1 on failure
+ */
+int hip_write_to_dht_queue(void *write_data, int data_size_in_bytes)
+{
+    void *temp_data            = NULL;
+    struct hip_queue *new_item = NULL;
+    int err                    = -1;
+
+    _HIP_DEBUG("Write, Items in dht_queue %d on enter\n", dht_queue_count);
+    temp_data = malloc(data_size_in_bytes);
+    HIP_IFEL((!temp_data), -1, "Failed to malloc memory for data\n");
+    memset(temp_data, 0, sizeof(data_size_in_bytes));
+    memcpy(temp_data, write_data, data_size_in_bytes);
+
+    new_item           = (struct hip_queue *) malloc(sizeof(struct hip_queue));
+    HIP_IFEL((!new_item), -1, "Failed to malloc memory for queue new item\n");
+    memset(new_item, 0, sizeof(struct hip_queue));
+    new_item->data_len = data_size_in_bytes;
+    new_item->data     = temp_data;
+    err                = hip_ht_add(hip_dht_queue, new_item);
+    dht_queue_count    = dht_queue_count + 1;
+
+    /* Debug line do not leave uncommented */
+    //hip_debug_print_dht_queue();
+    _HIP_DEBUG("Write, Items in dht_queue %d on exit\n", dht_queue_count);
+
 out_err:
-       return err ;  
+    return err;
 }
 
 /**
-* hip_read_from_dht_queue - This function writes data to the hip_queue 
structure
-*
-* @param read_data stores the data read from queue node
-*
-* @return status of the operation 0 on success, -1 on failure
-*/
-int hip_read_from_dht_queue (void *read_data)
+ * hip_read_from_dht_queue
+ *
+ * This function writes data to the hip_queue structure
+ *
+ * @param read_data stores the data read from queue node
+ *
+ * @return status of the operation 0 on success, -1 on failure
+ */
+int hip_read_from_dht_queue(void *read_data)
 {
-       int i = 0;
-       hip_list_t *item, *tmp;
-       struct hip_queue *this = NULL;
-
-       _HIP_DEBUG("Read, Items in dht_queue %d on enter\n", dht_queue_count);
-       
-       list_for_each_safe(item, tmp, hip_dht_queue, i) {
-               this = (struct hip_queue *)list_entry(item);
-               if (this == NULL) return(-1);
-               memcpy (read_data, this->data, this->data_len);
-               _HIP_DEBUG ("Node data read: %s \n", (char*)read_data);
-
-               hip_ht_delete(hip_dht_queue, this);
-               if (this->data)
-                       free(this->data);
-               if (this)
-                       free(this);
-
-               _HIP_DEBUG("Read, Items in dht_queue %d on exit\n", 
dht_queue_count);   
-               dht_queue_count = dht_queue_count -1;
-               // ugly way but I need only one item at a time and this was fast
-               return(0); 
-       }
-       /* Debug line do not leave uncommented */
-       //hip_debug_print_dht_queue();
-       if (this && this->data) free(this->data);
-       if (this) free(this);
-       return(0);
+    int i                  = 0;
+    hip_list_t *item, *tmp;
+    struct hip_queue *this = NULL;
+
+    _HIP_DEBUG("Read, Items in dht_queue %d on enter\n", dht_queue_count);
+
+    list_for_each_safe(item, tmp, hip_dht_queue, i) {
+        this = (struct hip_queue *) list_entry(item);
+        if (this == NULL) {
+            return -1;
+        }
+        memcpy(read_data, this->data, this->data_len);
+        _HIP_DEBUG("Node data read: %s \n", (char *) read_data);
+
+        hip_ht_delete(hip_dht_queue, this);
+        if (this->data) {
+            free(this->data);
+        }
+        if (this) {
+            free(this);
+        }
+
+        _HIP_DEBUG("Read, Items in dht_queue %d on exit\n", dht_queue_count);
+        dht_queue_count = dht_queue_count - 1;
+        // ugly way but I need only one item at a time and this was fast
+        return 0;
+    }
+    /* Debug line do not leave uncommented */
+    //hip_debug_print_dht_queue();
+    if (this && this->data) {
+        free(this->data);
+    }
+    if (this) {
+        free(this);
+    }
+    return 0;
 }
 
 #if 0
-/** 
- * hip_debug_print_queue - This function prints all the dht queue members
- *
- @ return void
-*/
-static void hip_debug_print_dht_queue() {
-       int i = 0;
-       hip_list_t *item, *tmp;
-       struct hip_queue *entry;
+/**
+ * hip_debug_print_queue
+ *
+ * This function prints all the dht queue members
+ *
+ * @ return void
+ */
+static void hip_debug_print_dht_queue()
+{
+    int i = 0;
+    hip_list_t *item, *tmp;
+    struct hip_queue *entry;
 
-       HIP_DEBUG("DEBUGGING QUEUE comment out if left uncommented\n");
-       HIP_DEBUG("Head count %d\n", dht_queue_count);
-       list_for_each_safe(item, tmp, hip_dht_queue, i) {
-               entry = list_entry(item);
-               HIP_DEBUG("Node data_len = %d\n", entry->data_len);
-               HIP_DEBUG("Node data= %s\n", entry->data);
-       }  
+    HIP_DEBUG("DEBUGGING QUEUE comment out if left uncommented\n");
+    HIP_DEBUG("Head count %d\n", dht_queue_count);
+    list_for_each_safe(item, tmp, hip_dht_queue, i) {
+        entry = list_entry(item);
+        HIP_DEBUG("Node data_len = %d\n", entry->data_len);
+        HIP_DEBUG("Node data= %s\n", entry->data);
+    }
 }
+
 #endif

=== modified file 'hipd/dhtqueue.h'
--- hipd/dhtqueue.h     2010-01-31 21:29:06 +0000
+++ hipd/dhtqueue.h     2010-02-10 22:32:46 +0000
@@ -1,11 +1,11 @@
 /** @file
  * A header file for hipqueue.c
- * 
+ *
  * @author  Pardeep Maheshwaree <pmaheshw@xxxxxxxxx>
  * @author  Samu Varjonen <samu.varjonen@xxxxxxx>
  * @note    Distributed under <a 
href="http://www.gnu.org/licenses/gpl.txt";>GNU/GPL</a>
  */
- 
+
 #ifndef _HIP_HIPQUEUE
 #define _HIP_HIPQUEUE
 

=== modified file 'hipd/esp_prot_anchordb.c'
--- hipd/esp_prot_anchordb.c    2010-01-19 09:28:42 +0000
+++ hipd/esp_prot_anchordb.c    2010-02-10 22:32:46 +0000
@@ -20,16 +20,15 @@
 
 
 /* defines the structure storing the anchors */
-typedef struct anchor_db
-{
-       /* amount of anchors for each transform */
-       int num_anchors[MAX_NUM_TRANSFORMS];
-       /* length of the anchors for each transform */
-       int anchor_lengths[MAX_NUM_TRANSFORMS];
-       /* length of the corresponding hchain/htree */
-       int hash_item_length[MAX_NUM_TRANSFORMS];
-       /* set to support max amount of anchors possible */
-       unsigned char 
*anchors[MAX_NUM_TRANSFORMS][HCSTORE_MAX_HCHAINS_PER_ITEM];
+typedef struct anchor_db {
+    /* amount of anchors for each transform */
+    int            num_anchors[MAX_NUM_TRANSFORMS];
+    /* length of the anchors for each transform */
+    int            anchor_lengths[MAX_NUM_TRANSFORMS];
+    /* length of the corresponding hchain/htree */
+    int            hash_item_length[MAX_NUM_TRANSFORMS];
+    /* set to support max amount of anchors possible */
+    unsigned char *anchors[MAX_NUM_TRANSFORMS][HCSTORE_MAX_HCHAINS_PER_ITEM];
 } anchor_db_t;
 
 /* stores all anchors sent by the firewall */
@@ -38,37 +37,36 @@
 /** inits the anchorDB */
 void anchor_db_init(void)
 {
-       // set to 0 / NULL
-       memset(anchor_db.num_anchors, 0, MAX_NUM_TRANSFORMS * sizeof(int));
-       memset(anchor_db.anchor_lengths, 0, MAX_NUM_TRANSFORMS * sizeof(int));
-       memset(anchor_db.anchor_lengths, 0, MAX_NUM_TRANSFORMS * sizeof(int));
-       memset(anchor_db.anchors, 0, MAX_NUM_TRANSFORMS * 
HCSTORE_MAX_HCHAINS_PER_ITEM);
+    // set to 0 / NULL
+    memset(anchor_db.num_anchors, 0, MAX_NUM_TRANSFORMS * sizeof(int));
+    memset(anchor_db.anchor_lengths, 0, MAX_NUM_TRANSFORMS * sizeof(int));
+    memset(anchor_db.anchor_lengths, 0, MAX_NUM_TRANSFORMS * sizeof(int));
+    memset(anchor_db.anchors, 0, MAX_NUM_TRANSFORMS * 
HCSTORE_MAX_HCHAINS_PER_ITEM);
 
-       HIP_DEBUG("inited hchain anchorDB\n");
+    HIP_DEBUG("inited hchain anchorDB\n");
 }
 
 /** uninits the anchorDB */
 void anchor_db_uninit(void)
 {
-       int i, j;
-
-       // free all hashes
-       for (i = 0; i < MAX_NUM_TRANSFORMS; i++)
-       {
-               anchor_db.num_anchors[i] = 0;
-               anchor_db.anchor_lengths[i] = 0;
-               anchor_db.hash_item_length[i] = 0;
-
-               for (j = 0; j < HCSTORE_MAX_HCHAINS_PER_ITEM; j++)
-               {
-                       if (anchor_db.anchors[i][j])
-                               free(anchor_db.anchors[i][j]);
-
-                       anchor_db.anchors[i][j] = NULL;
-               }
-       }
-
-       HIP_DEBUG("uninited hchain anchorDB\n");
+    int i, j;
+
+    // free all hashes
+    for (i = 0; i < MAX_NUM_TRANSFORMS; i++) {
+        anchor_db.num_anchors[i]      = 0;
+        anchor_db.anchor_lengths[i]   = 0;
+        anchor_db.hash_item_length[i] = 0;
+
+        for (j = 0; j < HCSTORE_MAX_HCHAINS_PER_ITEM; j++) {
+            if (anchor_db.anchors[i][j]) {
+                free(anchor_db.anchors[i][j]);
+            }
+
+            anchor_db.anchors[i][j] = NULL;
+        }
+    }
+
+    HIP_DEBUG("uninited hchain anchorDB\n");
 }
 
 /** handles a user-message sent by the firewall when the bex-store is updated
@@ -78,93 +76,86 @@
  */
 int anchor_db_update(const struct hip_common *msg)
 {
-       struct hip_tlv_common *param = NULL;
-       unsigned char *anchor = NULL;
-       int err = 0, i, j;
-       uint8_t esp_transforms[MAX_NUM_TRANSFORMS];
-
-       HIP_ASSERT(msg != NULL);
-
-       // if this function is called, the extension should be active
-       if (esp_prot_active)
-       {
-               memset(esp_transforms, 0, MAX_NUM_TRANSFORMS * sizeof(uint8_t));
-
-               HIP_DEBUG("updating hchain anchorDB...\n");
-
-               /* XX TODO ineffcient -> only add non-existing elements instead 
of
-                *         uniniting and adding all elements again */
-               anchor_db_uninit();
-
-               /*** set up anchor_db.num_anchors and anchor_db.anchor_lengths 
***/
-               // get first int value
-               HIP_IFEL(!(param = (struct hip_tlv_common *) hip_get_param(msg, 
HIP_PARAM_UINT)),
-                               -1, "parameter missing in user-message from 
fw\n");
-
-               // don't set up anything for UNUSED transform
-               for (i = 0; i < esp_prot_num_transforms - 1; i++)
-               {
-                       // needed for redirection to correct slot in anchor_db
-                       esp_transforms[i] = *(uint8_t *) 
hip_get_param_contents_direct(param);
-                       HIP_DEBUG("esp_transform is %u\n", esp_transforms[i]);
-
-                       HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(msg, param)),
-                                       -1, "parameter missing in user-message 
from fw\n");
-                       anchor_db.num_anchors[esp_transforms[i]] = *(int *) 
hip_get_param_contents_direct(param);
-                       HIP_DEBUG("num_anchors is %i\n", 
anchor_db.num_anchors[esp_transforms[i]]);
-
-                       HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(msg, param)),
-                                       -1, "parameter missing in user-message 
from fw\n");
-                       anchor_db.anchor_lengths[esp_transforms[i]] = *(int *) 
hip_get_param_contents_direct(param);
-                       HIP_DEBUG("anchor_length is %i\n", 
anchor_db.anchor_lengths[esp_transforms[i]]);
-
-                       HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(msg, param)),
-                                       -1, "parameter missing in user-message 
from fw\n");
-               }
-
-               for (i = 0; i < esp_prot_num_transforms - 1; i++)
-               {
-                       HIP_DEBUG("transform %u:\n", esp_transforms[i]);
-
-                       for (j = 0; j < 
anchor_db.num_anchors[esp_transforms[i]]; j++)
-                       {
-                               
HIP_IFEL(!(anchor_db.anchors[esp_transforms[i]][j] = (unsigned char 
*)malloc(anchor_db.
-                                               
anchor_lengths[esp_transforms[i]])), -1, "failed to allocate memory\n");
-
-                               anchor = (unsigned char *) 
hip_get_param_contents_direct(param);
-                               memcpy(anchor_db.anchors[esp_transforms[i]][j], 
anchor,
-                                               
anchor_db.anchor_lengths[esp_transforms[i]]);
-                               HIP_HEXDUMP("adding anchor: ", 
anchor_db.anchors[esp_transforms[i]][j],
-                                               
anchor_db.anchor_lengths[esp_transforms[i]]);
-
-                               HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(
-                                               msg, param)), -1, "parameter 
missing in user-message from fw\n");
-                               anchor_db.hash_item_length[esp_transforms[i]] = 
*(int *)
-                                               
hip_get_param_contents_direct(param);
-                               HIP_DEBUG("adding hash_item_length: %i\n",
-                                               
anchor_db.hash_item_length[esp_transforms[i]]);
-
-                               // exclude getting the next param for the very 
last loop
-                               if (!(i == esp_prot_num_transforms - 2 && j == 
anchor_db.num_anchors[esp_transforms[i]] - 1))
-                               {
-                                       HIP_IFEL(!(param = (struct 
hip_tlv_common *) hip_get_next_param(
-                                                       msg, param)), -1, 
"parameter missing in user-message from fw\n");
-                               }
-                       }
-               }
-
-               HIP_DEBUG("anchor_db successfully updated\n");
-
-       } else
-       {
-               HIP_ERROR("received anchor_db update, but esp protection 
extension disabled\n");
-
-               err = -1;
-               goto out_err;
-       }
-
-  out_err:
-       return err;
+    struct hip_tlv_common *param = NULL;
+    unsigned char *anchor        = NULL;
+    int err                      = 0, i, j;
+    uint8_t esp_transforms[MAX_NUM_TRANSFORMS];
+
+    HIP_ASSERT(msg != NULL);
+
+    // if this function is called, the extension should be active
+    if (esp_prot_active) {
+        memset(esp_transforms, 0, MAX_NUM_TRANSFORMS * sizeof(uint8_t));
+
+        HIP_DEBUG("updating hchain anchorDB...\n");
+
+        /* XX TODO ineffcient -> only add non-existing elements instead of
+         *         uniniting and adding all elements again */
+        anchor_db_uninit();
+
+        /*** set up anchor_db.num_anchors and anchor_db.anchor_lengths ***/
+        // get first int value
+        HIP_IFEL(!(param = (struct hip_tlv_common *) hip_get_param(msg, 
HIP_PARAM_UINT)),
+                 -1, "parameter missing in user-message from fw\n");
+
+        // don't set up anything for UNUSED transform
+        for (i = 0; i < esp_prot_num_transforms - 1; i++) {
+            // needed for redirection to correct slot in anchor_db
+            esp_transforms[i] = *(uint8_t *) 
hip_get_param_contents_direct(param);
+            HIP_DEBUG("esp_transform is %u\n", esp_transforms[i]);
+
+            HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(msg, param)),
+                     -1, "parameter missing in user-message from fw\n");
+            anchor_db.num_anchors[esp_transforms[i]] = *(int *) 
hip_get_param_contents_direct(param);
+            HIP_DEBUG("num_anchors is %i\n", 
anchor_db.num_anchors[esp_transforms[i]]);
+
+            HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(msg, param)),
+                     -1, "parameter missing in user-message from fw\n");
+            anchor_db.anchor_lengths[esp_transforms[i]] = *(int *) 
hip_get_param_contents_direct(param);
+            HIP_DEBUG("anchor_length is %i\n", 
anchor_db.anchor_lengths[esp_transforms[i]]);
+
+            HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(msg, param)),
+                     -1, "parameter missing in user-message from fw\n");
+        }
+
+        for (i = 0; i < esp_prot_num_transforms - 1; i++) {
+            HIP_DEBUG("transform %u:\n", esp_transforms[i]);
+
+            for (j = 0; j < anchor_db.num_anchors[esp_transforms[i]]; j++) {
+                HIP_IFEL(!(anchor_db.anchors[esp_transforms[i]][j] = (unsigned 
char *) malloc(anchor_db.
+                                                                               
               anchor_lengths[esp_transforms[i]])), -1, "failed to allocate 
memory\n");
+
+                anchor = (unsigned char *) 
hip_get_param_contents_direct(param);
+                memcpy(anchor_db.anchors[esp_transforms[i]][j], anchor,
+                       anchor_db.anchor_lengths[esp_transforms[i]]);
+                HIP_HEXDUMP("adding anchor: ", 
anchor_db.anchors[esp_transforms[i]][j],
+                            anchor_db.anchor_lengths[esp_transforms[i]]);
+
+                HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(
+                               msg, param)), -1, "parameter missing in 
user-message from fw\n");
+                anchor_db.hash_item_length[esp_transforms[i]] = *(int *)
+                                                                
hip_get_param_contents_direct(param);
+                HIP_DEBUG("adding hash_item_length: %i\n",
+                          anchor_db.hash_item_length[esp_transforms[i]]);
+
+                // exclude getting the next param for the very last loop
+                if (!(i == esp_prot_num_transforms - 2 && j == 
anchor_db.num_anchors[esp_transforms[i]] - 1)) {
+                    HIP_IFEL(!(param = (struct hip_tlv_common *) 
hip_get_next_param(
+                                   msg, param)), -1, "parameter missing in 
user-message from fw\n");
+                }
+            }
+        }
+
+        HIP_DEBUG("anchor_db successfully updated\n");
+    } else {
+        HIP_ERROR("received anchor_db update, but esp protection extension 
disabled\n");
+
+        err = -1;
+        goto out_err;
+    }
+
+out_err:
+    return err;
 }
 
 /** returns number of elements for the given transform
@@ -174,11 +165,13 @@
  */
 int anchor_db_get_num_anchors(const uint8_t transform)
 {
-       HIP_ASSERT(transform > 0);
-
-       HIP_DEBUG("anchor_db.num_anchors[%u]: %i\n", transform, 
anchor_db.num_anchors[transform]);
-
-       return anchor_db.num_anchors[transform];
+    HIP_ASSERT(transform > 0);
+
+    HIP_DEBUG("anchor_db.num_anchors[%u]: %i\n",
+              transform,
+              anchor_db.num_anchors[transform]);
+
+    return anchor_db.num_anchors[transform];
 }
 
 /* returns an unused anchor element for the given transform
@@ -186,38 +179,38 @@
  * @param      transform the ESP protection extension transform
  * @return     anchor, NULL if empty
  */
-unsigned char * anchor_db_get_anchor(const uint8_t transform)
+unsigned char *anchor_db_get_anchor(const uint8_t transform)
 {
-       unsigned char *stored_anchor = NULL;
-       int anchor_offset = 0;
-       int err = 0;
-
-       // ensure correct boundaries
-       HIP_ASSERT(transform > 0);
-
-       // get index of last unused anchor for this transform
-       HIP_IFEL((anchor_offset = anchor_db.num_anchors[transform] - 1) < 0, -1,
-                       "anchor_db is empty for this transform\n");
-
-       // ensure correct boundaries
-       HIP_ASSERT(anchor_offset >= 0 && anchor_offset < 
HCSTORE_MAX_HCHAINS_PER_ITEM);
-       HIP_IFEL(!(stored_anchor = 
anchor_db.anchors[transform][anchor_offset]), -1,
-                       "anchor_offset points to empty slot\n");
-
-       // remove anchor from db
-       anchor_db.anchors[transform][anchor_offset] = NULL;
-       anchor_offset = anchor_db.num_anchors[transform]--;
-
-  out_err:
-       if (err)
-       {
-               if (stored_anchor)
-                       free(stored_anchor);
-
-               stored_anchor = NULL;
-       }
-
-       return stored_anchor;
+    unsigned char *stored_anchor = NULL;
+    int anchor_offset            = 0;
+    int err                      = 0;
+
+    // ensure correct boundaries
+    HIP_ASSERT(transform > 0);
+
+    // get index of last unused anchor for this transform
+    HIP_IFEL((anchor_offset = anchor_db.num_anchors[transform] - 1) < 0, -1,
+             "anchor_db is empty for this transform\n");
+
+    // ensure correct boundaries
+    HIP_ASSERT(anchor_offset >= 0 && anchor_offset < 
HCSTORE_MAX_HCHAINS_PER_ITEM);
+    HIP_IFEL(!(stored_anchor = anchor_db.anchors[transform][anchor_offset]), 
-1,
+             "anchor_offset points to empty slot\n");
+
+    // remove anchor from db
+    anchor_db.anchors[transform][anchor_offset] = NULL;
+    anchor_offset = anchor_db.num_anchors[transform]--;
+
+out_err:
+    if (err) {
+        if (stored_anchor) {
+            free(stored_anchor);
+        }
+
+        stored_anchor = NULL;
+    }
+
+    return stored_anchor;
 }
 
 /** returns the anchor-length for a given transform
@@ -227,9 +220,9 @@
  */
 int anchor_db_get_anchor_length(const uint8_t transform)
 {
-       HIP_ASSERT(transform > 0);
+    HIP_ASSERT(transform > 0);
 
-       return anchor_db.anchor_lengths[transform];
+    return anchor_db.anchor_lengths[transform];
 }
 
 /** returns the hash-item-length for a given transform
@@ -239,7 +232,7 @@
  */
 int anchor_db_get_hash_item_length(const uint8_t transform)
 {
-       HIP_ASSERT(transform > 0);
+    HIP_ASSERT(transform > 0);
 
-       return anchor_db.hash_item_length[transform];
+    return anchor_db.hash_item_length[transform];
 }

=== modified file 'hipd/esp_prot_anchordb.h'
--- hipd/esp_prot_anchordb.h    2010-01-19 09:28:42 +0000
+++ hipd/esp_prot_anchordb.h    2010-02-10 22:32:46 +0000
@@ -23,7 +23,7 @@
 void anchor_db_uninit(void);
 int anchor_db_update(const struct hip_common *msg);
 int anchor_db_get_num_anchors(const uint8_t transform);
-unsigned char * anchor_db_get_anchor(const uint8_t transform);
+unsigned char *anchor_db_get_anchor(const uint8_t transform);
 int anchor_db_get_anchor_length(const uint8_t transform);
 int anchor_db_get_hash_item_length(const uint8_t transform);
 

=== modified file 'hipd/esp_prot_hipd_msg.c'
--- hipd/esp_prot_hipd_msg.c    2010-02-08 15:18:06 +0000
+++ hipd/esp_prot_hipd_msg.c    2010-02-10 22:32:46 +0000
@@ -29,44 +29,61 @@
  * @param spi                  spi of IPsec association
  * @return 0 on success, < 0 in case of an error
  **/
-static int esp_prot_send_update_response(const hip_common_t *recv_update, 
hip_ha_t *entry,
-               const in6_addr_t *src_ip, const in6_addr_t *dst_ip, const 
uint32_t spi)
+static int esp_prot_send_update_response(const hip_common_t *recv_update,
+                                         hip_ha_t *entry,
+                                         const in6_addr_t *src_ip,
+                                         const in6_addr_t *dst_ip,
+                                         const uint32_t spi)
 {
-       hip_common_t *resp_update = NULL;
-       struct hip_seq *seq = NULL;
-       uint16_t mask = 0;
-       int err = 0;
-
-       HIP_IFEL(!(seq = (struct hip_seq *) hip_get_param(recv_update, 
HIP_PARAM_SEQ)), -1,
-                       "SEQ not found\n");
-
-       HIP_IFEL(!(resp_update = hip_msg_alloc()), -ENOMEM, "out of memory\n");
-
-       entry->hadb_misc_func->hip_build_network_hdr(resp_update, HIP_UPDATE, 
mask,
-                       &recv_update->hitr, &recv_update->hits);
-
-       /* Add ESP_INFO */
-       HIP_IFEL(hip_build_param_esp_info(resp_update, 
entry->current_keymat_index,
-                       spi, spi), -1, "Building of ESP_INFO param failed\n");
-
-       /* Add ACK */
-       HIP_IFEL(hip_build_param_ack(resp_update, ntohl(seq->update_id)), -1,
-                       "Building of ACK failed\n");
-
-       /* Add HMAC */
-       HIP_IFEL(hip_build_param_hmac_contents(resp_update, 
&entry->hip_hmac_out), -1,
-                       "Building of HMAC failed\n");
-
-       /* Add SIGNATURE */
-       HIP_IFEL(entry->sign(entry->our_priv_key, resp_update), -EINVAL,
-                       "Could not sign UPDATE. Failing\n");
-
-       HIP_IFEL(entry->hadb_xmit_func->hip_send_pkt(src_ip, dst_ip,
-                       (entry->nat_mode ? hip_get_local_nat_udp_port() : 0), 
entry->peer_udp_port,
-                       resp_update, entry, 0), -1, "failed to send 
ANCHOR-UPDATE\n");
-
-  out_err:
-       return err;
+    hip_common_t *resp_update = NULL;
+    struct hip_seq *seq       = NULL;
+    uint16_t mask             = 0;
+    int err                   = 0;
+
+    HIP_IFEL(!(seq = (struct hip_seq *) hip_get_param(recv_update, 
HIP_PARAM_SEQ)),
+             -1,
+             "SEQ not found\n");
+
+    HIP_IFEL(!(resp_update = hip_msg_alloc()), -ENOMEM, "out of memory\n");
+
+    entry->hadb_misc_func->hip_build_network_hdr(resp_update,
+                                                 HIP_UPDATE,
+                                                 mask,
+                                                 &recv_update->hitr,
+                                                 &recv_update->hits);
+
+    /* Add ESP_INFO */
+    HIP_IFEL(hip_build_param_esp_info(resp_update,
+                                      entry->current_keymat_index,
+                                      spi,
+                                      spi),
+             -1,
+             "Building of ESP_INFO param failed\n");
+
+    /* Add ACK */
+    HIP_IFEL(hip_build_param_ack(resp_update, ntohl(seq->update_id)),
+             -1,
+             "Building of ACK failed\n");
+
+    /* Add HMAC */
+    HIP_IFEL(hip_build_param_hmac_contents(resp_update, &entry->hip_hmac_out),
+             -1,
+             "Building of HMAC failed\n");
+
+    /* Add SIGNATURE */
+    HIP_IFEL(entry->sign(entry->our_priv_key, resp_update), -EINVAL,
+             "Could not sign UPDATE. Failing\n");
+
+    HIP_IFEL(entry->hadb_xmit_func->hip_send_pkt(src_ip,
+                                                 dst_ip,
+                                                 (entry->nat_mode ? 
hip_get_local_nat_udp_port() : 0),
+                                                 entry->peer_udp_port,
+                                                 resp_update, entry, 0),
+             -1,
+             "failed to send ANCHOR-UPDATE\n");
+
+out_err:
+    return err;
 }
 
 /**
@@ -77,35 +94,33 @@
  * @param      transforms the transforms array
  * @return     the overall preferred transform
  */
-static uint8_t esp_prot_select_transform(const int num_transforms, const 
uint8_t transforms[])
+static uint8_t esp_prot_select_transform(const int num_transforms,
+                                         const uint8_t transforms[])
 {
-       uint8_t transform = ESP_PROT_TFM_UNUSED;
-       int err = 0, i, j;
-
-       for (i = 0; i < esp_prot_num_transforms; i++)
-       {
-               for (j = 0; j < num_transforms; j++)
-               {
-                       if (esp_prot_transforms[i] == transforms[j])
-                       {
-                               HIP_DEBUG("found matching transform: %u\n", 
esp_prot_transforms[i]);
-
-                               transform = esp_prot_transforms[i];
-                               goto out_err;
-                       }
-               }
-       }
-
-       HIP_ERROR("NO matching transform found\n");
-       transform = ESP_PROT_TFM_UNUSED;
-
-  out_err:
-       if (err)
-       {
-               transform = ESP_PROT_TFM_UNUSED;
-       }
-
-       return transform;
+    uint8_t transform = ESP_PROT_TFM_UNUSED;
+    int err           = 0, i, j;
+
+    for (i = 0; i < esp_prot_num_transforms; i++) {
+        for (j = 0; j < num_transforms; j++) {
+            if (esp_prot_transforms[i] == transforms[j]) {
+                HIP_DEBUG("found matching transform: %u\n",
+                          esp_prot_transforms[i]);
+
+                transform = esp_prot_transforms[i];
+                goto out_err;
+            }
+        }
+    }
+
+    HIP_ERROR("NO matching transform found\n");
+    transform = ESP_PROT_TFM_UNUSED;
+
+out_err:
+    if (err) {
+        transform = ESP_PROT_TFM_UNUSED;
+    }
+
+    return transform;
 }
 
 /********************** user-messages *********************/
@@ -118,53 +133,48 @@
  */
 int esp_prot_set_preferred_transforms(const struct hip_common *msg)
 {
-       struct hip_tlv_common *param = NULL;
-       int err = 0, i;
-
-       param = (struct hip_tlv_common *)hip_get_param(msg, HIP_PARAM_INT);
-       esp_prot_active = *((int *)hip_get_param_contents_direct(param));
-       HIP_DEBUG("esp_prot_active: %i\n", esp_prot_active);
-
-       // process message and store the preferred transforms
-       param = (struct hip_tlv_common *)hip_get_next_param(msg, param);
-       esp_prot_num_transforms = *((int 
*)hip_get_param_contents_direct(param));
-       HIP_DEBUG("esp protection num_transforms: %i\n", 
esp_prot_num_transforms);
-
-       param = (struct hip_tlv_common *)hip_get_next_param(msg, param);
-       esp_prot_num_parallel_hchains = *((long 
*)hip_get_param_contents_direct(param));
-       HIP_DEBUG("esp_prot_num_parallel_hchains: %i\n", 
esp_prot_num_parallel_hchains);
-
-       for (i = 0; i < MAX_NUM_TRANSFORMS; i++)
-       {
-               if (i < esp_prot_num_transforms)
-               {
-                       param = (struct hip_tlv_common 
*)hip_get_next_param(msg, param);
-                       esp_prot_transforms[i] = *((uint8_t 
*)hip_get_param_contents_direct(param));
-                       HIP_DEBUG("esp protection transform %i: %u\n", i + 1, 
esp_prot_transforms[i]);
-
-               } else
-               {
-                       esp_prot_transforms[i] = 0;
-               }
-       }
-
-       // this works as we always have to send at least ESP_PROT_TFM_UNUSED
-       if (esp_prot_active) {
-               anchor_db_init();
-               HIP_DEBUG("switched to esp protection extension\n");
-       }
-       else {
-               anchor_db_uninit();
-               HIP_DEBUG("switched to normal esp mode\n");
-       }
-
-       /* we have to make sure that the precalculated R1s include the esp
-        * protection extension transform */
-       HIP_DEBUG("recreate all R1s\n");
-       HIP_IFEL(hip_recreate_all_precreated_r1_packets(), -1, "failed to 
recreate all R1s\n");
-
-  out_err:
-       return err;
+    struct hip_tlv_common *param = NULL;
+    int err                      = 0, i;
+
+    param = (struct hip_tlv_common *) hip_get_param(msg, HIP_PARAM_INT);
+    esp_prot_active = *((int *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("esp_prot_active: %i\n", esp_prot_active);
+
+    // process message and store the preferred transforms
+    param = (struct hip_tlv_common *) hip_get_next_param(msg, param);
+    esp_prot_num_transforms = *((int *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("esp protection num_transforms: %i\n", esp_prot_num_transforms);
+
+    param = (struct hip_tlv_common *) hip_get_next_param(msg, param);
+    esp_prot_num_parallel_hchains = *((long *) 
hip_get_param_contents_direct(param));
+    HIP_DEBUG("esp_prot_num_parallel_hchains: %i\n", 
esp_prot_num_parallel_hchains);
+
+    for (i = 0; i < MAX_NUM_TRANSFORMS; i++) {
+        if (i < esp_prot_num_transforms) {
+            param = (struct hip_tlv_common *) hip_get_next_param(msg, param);
+            esp_prot_transforms[i] = *((uint8_t *) 
hip_get_param_contents_direct(param));
+            HIP_DEBUG("esp protection transform %i: %u\n", i + 1, 
esp_prot_transforms[i]);
+        } else {
+            esp_prot_transforms[i] = 0;
+        }
+    }
+
+    // this works as we always have to send at least ESP_PROT_TFM_UNUSED
+    if (esp_prot_active) {
+        anchor_db_init();
+        HIP_DEBUG("switched to esp protection extension\n");
+    } else {
+        anchor_db_uninit();
+        HIP_DEBUG("switched to normal esp mode\n");
+    }
+
+    /* we have to make sure that the precalculated R1s include the esp
+     * protection extension transform */
+    HIP_DEBUG("recreate all R1s\n");
+    HIP_IFEL(hip_recreate_all_precreated_r1_packets(), -1, "failed to recreate 
all R1s\n");
+
+out_err:
+    return err;
 }
 
 /** handles the user-message sent by fw when a new anchor has to be set
@@ -175,156 +185,150 @@
  */
 int esp_prot_handle_trigger_update_msg(const struct hip_common *msg)
 {
-       struct hip_tlv_common *param = NULL;
-       hip_hit_t *local_hit = NULL, *peer_hit = NULL;
-       uint8_t esp_prot_tfm = 0;
-       int hash_length = 0;
-       unsigned char *esp_prot_anchor = NULL;
-       int soft_update = 0;
-       int anchor_offset[MAX_NUM_PARALLEL_HCHAINS];
-       int secret_length[MAX_NUM_PARALLEL_HCHAINS];
-       int branch_length[MAX_NUM_PARALLEL_HCHAINS];
-       int root_length = 0;
-       unsigned char *secret[MAX_NUM_PARALLEL_HCHAINS];
-       unsigned char *branch_nodes[MAX_NUM_PARALLEL_HCHAINS];
-       unsigned char *root[MAX_NUM_PARALLEL_HCHAINS];
-       hip_ha_t *entry = NULL;
-       int hash_item_length = 0;
-       unsigned char cmp_val[MAX_HASH_LENGTH];
-       int err = 0;
-       long num_parallel_hchains = 0, i;
-
-       memset(cmp_val, 0, MAX_HASH_LENGTH);
-
-       param = hip_get_param(msg, HIP_PARAM_HIT);
-       local_hit = (hip_hit_t *) hip_get_param_contents_direct(param);
-       HIP_DEBUG_HIT("src_hit", local_hit);
-
-       param = hip_get_next_param(msg, param);
-       peer_hit = (hip_hit_t *) hip_get_param_contents_direct(param);
-       HIP_DEBUG_HIT("dst_hit", peer_hit);
-
-       // get matching entry from hadb for HITs provided above
-       HIP_IFEL(!(entry = hip_hadb_find_byhits(local_hit, peer_hit)), -1,
-                       "failed to retrieve requested HA entry\n");
-
-       param = hip_get_param(msg, HIP_PARAM_ESP_PROT_TFM);
-       esp_prot_tfm = *((uint8_t *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("esp_prot_transform: %u\n", esp_prot_tfm);
-
-       // check if transforms are matching and add anchor as new local_anchor
-       HIP_IFEL(entry->esp_prot_transform != esp_prot_tfm, -1,
-                       "esp prot transform changed without new BEX\n");
-       HIP_DEBUG("esp prot transforms match\n");
-
-       param = hip_get_param(msg, HIP_PARAM_INT);
-       hash_item_length  = *((int *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("hash_item_length: %i\n", hash_item_length);
-
-       // set the hash_item_length of the item used for this update
-       entry->hash_item_length = hash_item_length;
-
-       // we need to know the hash_length for this transform
-       hash_length = anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-       param = hip_get_next_param(msg, param);
-       num_parallel_hchains = *((long *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("num_parallel_hchains: %i\n", num_parallel_hchains);
-
-       // process all update anchors now
-       param = hip_get_param(msg, HIP_PARAM_HCHAIN_ANCHOR);
-       for (i = 0; i < num_parallel_hchains; i++)
-       {
-               esp_prot_anchor = (unsigned char *) 
hip_get_param_contents_direct(param);
-               HIP_HEXDUMP("anchor: ", esp_prot_anchor, hash_length);
-
-               // make sure that the update-anchor is not set yet
-               HIP_IFEL(memcmp(&entry->esp_local_update_anchors[i][0], 
&cmp_val[0], MAX_HASH_LENGTH), -1,
-                               "next hchain changed in fw, but we still have 
the last update-anchor set!");
-
-               // set the update anchor
-               memcpy(&entry->esp_local_update_anchors[i][0], esp_prot_anchor, 
hash_length);
-
-               param = hip_get_next_param(msg, param);
-       }
-
-       //param = hip_get_next_param(msg, param);
-       root_length  = *((int *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("root_length: %i\n", root_length);
-       entry->esp_root_length = root_length;
-
-       // process all update roots now
-       if (root_length > 0)
-       {
-               param = hip_get_param(msg, HIP_PARAM_ROOT);
-               for (i = 0; i < num_parallel_hchains; i++)
-               {
-                       root[i] = (unsigned char *) 
hip_get_param_contents_direct(param);
-                       memcpy(&entry->esp_root[i][0], root[i], root_length);
-
-                       HIP_HEXDUMP("root: ", &entry->esp_root[i][0], 
root_length);
-
-                       param = hip_get_next_param(msg, param);
-               }
-       }
-
-       //param = hip_get_next_param(msg, param);
-       soft_update  = *((int *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("soft_update: %i\n", soft_update);
-
-       if (soft_update)
-       {
-               for (i = 0; i < num_parallel_hchains; i++)
-               {
-                       param = hip_get_next_param(msg, param);
-                       anchor_offset[i]  = *((int *) 
hip_get_param_contents_direct(param));
-                       HIP_DEBUG("anchor_offset: %i\n", anchor_offset[i]);
-
-                       param = hip_get_next_param(msg, param);
-                       secret_length[i]  = *((int *) 
hip_get_param_contents_direct(param));
-                       HIP_DEBUG("secret_length: %i\n", secret_length[i]);
-
-                       param = hip_get_next_param(msg, param);
-                       branch_length[i] = *((int *) 
hip_get_param_contents_direct(param));
-                       HIP_DEBUG("branch_length: %i\n", branch_length[i]);
-
-                       param = hip_get_next_param(msg, param);
-                       secret[i] = (unsigned char *) 
hip_get_param_contents_direct(param);
-                       HIP_HEXDUMP("secret: ", secret[i], secret_length[i]);
-
-                       param = hip_get_next_param(msg, param);
-                       branch_nodes[i] = (unsigned char *) 
hip_get_param_contents_direct(param);
-                       HIP_HEXDUMP("branch_nodes: ", branch_nodes[i], 
branch_length[i]);
-               }
-       }
-
-       if (soft_update)
-       {
-               HIP_IFEL(esp_prot_send_light_update(entry, anchor_offset, 
secret, secret_length,
-                               branch_nodes, branch_length), -1,
-                               "failed to send anchor update\n");
-
-       } else
-       {
-               /* this should send an update only containing the mandatory 
params
-                * HMAC and HIP_SIGNATURE as well as the ESP_PROT_ANCHOR and the
-                * SEQ param (to garanty freshness of the ANCHOR) in the signed 
part
-                * of the message
-                *
-                * params used for this call:
-                * - hadb entry matching the HITs passed in the trigger msg
-                * - not sending locators -> list = NULL and count = 0
-                * - no interface triggers this event -> -1
-                * - bitwise telling about which params to add to UPDATE -> set 
3rd bit to 1
-                * - UPDATE not due to adding of a new addresses
-                * - not setting any address, as none is updated */
-               // TODO 10.11.2009: This send_update call should be modified
-                /*HIP_IFEL(hip_send_update_old(entry, NULL, 0, -1, 
SEND_UPDATE_ESP_ANCHOR, 0, NULL),
-                               -1, "failed to send anchor update\n");*/
-       }
-
-  out_err:
-       return err;
+    struct hip_tlv_common *param   = NULL;
+    hip_hit_t *local_hit           = NULL, *peer_hit = NULL;
+    uint8_t esp_prot_tfm           = 0;
+    int hash_length                = 0;
+    unsigned char *esp_prot_anchor = NULL;
+    int soft_update                = 0;
+    int anchor_offset[MAX_NUM_PARALLEL_HCHAINS];
+    int secret_length[MAX_NUM_PARALLEL_HCHAINS];
+    int branch_length[MAX_NUM_PARALLEL_HCHAINS];
+    int root_length                = 0;
+    unsigned char *secret[MAX_NUM_PARALLEL_HCHAINS];
+    unsigned char *branch_nodes[MAX_NUM_PARALLEL_HCHAINS];
+    unsigned char *root[MAX_NUM_PARALLEL_HCHAINS];
+    hip_ha_t *entry                = NULL;
+    int hash_item_length           = 0;
+    unsigned char cmp_val[MAX_HASH_LENGTH];
+    int err                        = 0;
+    long num_parallel_hchains      = 0, i;
+
+    memset(cmp_val, 0, MAX_HASH_LENGTH);
+
+    param     = hip_get_param(msg, HIP_PARAM_HIT);
+    local_hit = (hip_hit_t *) hip_get_param_contents_direct(param);
+    HIP_DEBUG_HIT("src_hit", local_hit);
+
+    param     = hip_get_next_param(msg, param);
+    peer_hit  = (hip_hit_t *) hip_get_param_contents_direct(param);
+    HIP_DEBUG_HIT("dst_hit", peer_hit);
+
+    // get matching entry from hadb for HITs provided above
+    HIP_IFEL(!(entry = hip_hadb_find_byhits(local_hit, peer_hit)), -1,
+             "failed to retrieve requested HA entry\n");
+
+    param        = hip_get_param(msg, HIP_PARAM_ESP_PROT_TFM);
+    esp_prot_tfm = *((uint8_t *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("esp_prot_transform: %u\n", esp_prot_tfm);
+
+    // check if transforms are matching and add anchor as new local_anchor
+    HIP_IFEL(entry->esp_prot_transform != esp_prot_tfm, -1,
+             "esp prot transform changed without new BEX\n");
+    HIP_DEBUG("esp prot transforms match\n");
+
+    param                   = hip_get_param(msg, HIP_PARAM_INT);
+    hash_item_length        = *((int *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("hash_item_length: %i\n", hash_item_length);
+
+    // set the hash_item_length of the item used for this update
+    entry->hash_item_length = hash_item_length;
+
+    // we need to know the hash_length for this transform
+    hash_length             = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+    param                   = hip_get_next_param(msg, param);
+    num_parallel_hchains    = *((long *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("num_parallel_hchains: %i\n", num_parallel_hchains);
+
+    // process all update anchors now
+    param                   = hip_get_param(msg, HIP_PARAM_HCHAIN_ANCHOR);
+    for (i = 0; i < num_parallel_hchains; i++) {
+        esp_prot_anchor = (unsigned char *) 
hip_get_param_contents_direct(param);
+        HIP_HEXDUMP("anchor: ", esp_prot_anchor, hash_length);
+
+        // make sure that the update-anchor is not set yet
+        HIP_IFEL(memcmp(&entry->esp_local_update_anchors[i][0],
+                        &cmp_val[0], MAX_HASH_LENGTH),
+                 -1,
+                 "next hchain changed in fw, but we still have the last 
update-anchor set!");
+
+        // set the update anchor
+        memcpy(&entry->esp_local_update_anchors[i][0], esp_prot_anchor, 
hash_length);
+
+        param = hip_get_next_param(msg, param);
+    }
+
+    //param = hip_get_next_param(msg, param);
+    root_length            = *((int *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("root_length: %i\n", root_length);
+    entry->esp_root_length = root_length;
+
+    // process all update roots now
+    if (root_length > 0) {
+        param = hip_get_param(msg, HIP_PARAM_ROOT);
+        for (i = 0; i < num_parallel_hchains; i++) {
+            root[i] = (unsigned char *) hip_get_param_contents_direct(param);
+            memcpy(&entry->esp_root[i][0], root[i], root_length);
+
+            HIP_HEXDUMP("root: ", &entry->esp_root[i][0], root_length);
+
+            param = hip_get_next_param(msg, param);
+        }
+    }
+
+    //param = hip_get_next_param(msg, param);
+    soft_update = *((int *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("soft_update: %i\n", soft_update);
+
+    if (soft_update) {
+        for (i = 0; i < num_parallel_hchains; i++) {
+            param            = hip_get_next_param(msg, param);
+            anchor_offset[i] = *((int *) hip_get_param_contents_direct(param));
+            HIP_DEBUG("anchor_offset: %i\n", anchor_offset[i]);
+
+            param            = hip_get_next_param(msg, param);
+            secret_length[i] = *((int *) hip_get_param_contents_direct(param));
+            HIP_DEBUG("secret_length: %i\n", secret_length[i]);
+
+            param            = hip_get_next_param(msg, param);
+            branch_length[i] = *((int *) hip_get_param_contents_direct(param));
+            HIP_DEBUG("branch_length: %i\n", branch_length[i]);
+
+            param            = hip_get_next_param(msg, param);
+            secret[i]        = (unsigned char *) 
hip_get_param_contents_direct(param);
+            HIP_HEXDUMP("secret: ", secret[i], secret_length[i]);
+
+            param            = hip_get_next_param(msg, param);
+            branch_nodes[i]  = (unsigned char *) 
hip_get_param_contents_direct(param);
+            HIP_HEXDUMP("branch_nodes: ", branch_nodes[i], branch_length[i]);
+        }
+    }
+
+    if (soft_update) {
+        HIP_IFEL(esp_prot_send_light_update(entry, anchor_offset, secret, 
secret_length,
+                                            branch_nodes, branch_length), -1,
+                 "failed to send anchor update\n");
+    } else {
+        /* this should send an update only containing the mandatory params
+         * HMAC and HIP_SIGNATURE as well as the ESP_PROT_ANCHOR and the
+         * SEQ param (to garanty freshness of the ANCHOR) in the signed part
+         * of the message
+         *
+         * params used for this call:
+         * - hadb entry matching the HITs passed in the trigger msg
+         * - not sending locators -> list = NULL and count = 0
+         * - no interface triggers this event -> -1
+         * - bitwise telling about which params to add to UPDATE -> set 3rd 
bit to 1
+         * - UPDATE not due to adding of a new addresses
+         * - not setting any address, as none is updated */
+        // TODO 10.11.2009: This send_update call should be modified
+        /*HIP_IFEL(hip_send_update_old(entry, NULL, 0, -1, 
SEND_UPDATE_ESP_ANCHOR, 0, NULL),
+         *              -1, "failed to send anchor update\n");*/
+    }
+
+out_err:
+    return err;
 }
 
 /** handles the user-message sent by fw when the anchors have changed in
@@ -335,80 +339,78 @@
  */
 int esp_prot_handle_anchor_change_msg(const struct hip_common *msg)
 {
-       struct hip_tlv_common *param = NULL;
-       hip_hit_t *local_hit = NULL, *peer_hit = NULL;
-       uint8_t esp_prot_tfm = 0;
-       int hash_length = 0;
-       unsigned char *esp_prot_anchor = NULL;
-       hip_ha_t *entry = NULL;
-       int direction = 0;
-       long num_parallel_hchains = 0, i;
-       int err = 0;
-
-       param = hip_get_param(msg, HIP_PARAM_HIT);
-       local_hit = (hip_hit_t *) hip_get_param_contents_direct(param);
-       HIP_DEBUG_HIT("src_hit", local_hit);
-
-       param = hip_get_next_param(msg, param);
-       peer_hit = (hip_hit_t *) hip_get_param_contents_direct(param);
-       HIP_DEBUG_HIT("dst_hit", peer_hit);
-
-       param = hip_get_param(msg, HIP_PARAM_INT);
-       direction = *((int *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("direction: %i\n", direction);
-
-       param = hip_get_param(msg, HIP_PARAM_ESP_PROT_TFM);
-       esp_prot_tfm = *((uint8_t *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("esp_prot_transform: %u\n", esp_prot_tfm);
-
-       param = hip_get_param(msg, HIP_PARAM_INT);
-       num_parallel_hchains = *((long *) hip_get_param_contents_direct(param));
-       HIP_DEBUG("num_parallel_hchains: %u\n", num_parallel_hchains);
-
-       param = hip_get_param(msg, HIP_PARAM_HCHAIN_ANCHOR);
-
-
-       // get matching entry from hadb for HITs provided above
-       HIP_IFEL(!(entry = hip_hadb_find_byhits(local_hit, peer_hit)), -1,
-                       "failed to retrieve requested HA entry\n");
-
-       // check if transforms are matching and add anchor as new local_anchor
-       HIP_IFEL(entry->esp_prot_transform != esp_prot_tfm, -1,
-                       "esp prot transform changed without new BEX\n");
-       HIP_DEBUG("esp prot transforms match\n");
-
-       // we need to know the hash_length for this transform
-       hash_length = anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-       // only handle outbound direction here
-       if (direction == HIP_SPI_DIRECTION_OUT)
-       {
-               for (i = 0; i < num_parallel_hchains; i++)
-               {
-                       esp_prot_anchor = (unsigned char *) 
hip_get_param_contents_direct(param);
-                       HIP_HEXDUMP("anchor: ", esp_prot_anchor, hash_length);
-
-                       // make sure that the update-anchor is set
-                       HIP_IFEL(memcmp(&entry->esp_local_update_anchors[i][0], 
esp_prot_anchor, hash_length),
-                                       -1, "hchain-anchors used for outbound 
connections NOT in sync\n");
-
-                       // set update anchor as new active local anchor
-                       memcpy(&entry->esp_local_anchors[i][0], 
&entry->esp_local_update_anchors[i][0], hash_length);
-                       memset(&entry->esp_local_update_anchors[i][0], 0, 
MAX_HASH_LENGTH);
-
-                       HIP_DEBUG("changed update_anchor to local_anchor\n");
-
-                       param = hip_get_next_param(msg, param);
-               }
-
-               goto out_err;
-       }
-
-       HIP_ERROR("failure when changing update_anchor to local_anchor\n");
-       err = -1;
-
-  out_err:
-       return err;
+    struct hip_tlv_common *param   = NULL;
+    hip_hit_t *local_hit           = NULL, *peer_hit = NULL;
+    uint8_t esp_prot_tfm           = 0;
+    int hash_length                = 0;
+    unsigned char *esp_prot_anchor = NULL;
+    hip_ha_t *entry                = NULL;
+    int direction                  = 0;
+    long num_parallel_hchains      = 0, i;
+    int err                        = 0;
+
+    param                = hip_get_param(msg, HIP_PARAM_HIT);
+    local_hit            = (hip_hit_t *) hip_get_param_contents_direct(param);
+    HIP_DEBUG_HIT("src_hit", local_hit);
+
+    param                = hip_get_next_param(msg, param);
+    peer_hit             = (hip_hit_t *) hip_get_param_contents_direct(param);
+    HIP_DEBUG_HIT("dst_hit", peer_hit);
+
+    param                = hip_get_param(msg, HIP_PARAM_INT);
+    direction            = *((int *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("direction: %i\n", direction);
+
+    param                = hip_get_param(msg, HIP_PARAM_ESP_PROT_TFM);
+    esp_prot_tfm         = *((uint8_t *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("esp_prot_transform: %u\n", esp_prot_tfm);
+
+    param                = hip_get_param(msg, HIP_PARAM_INT);
+    num_parallel_hchains = *((long *) hip_get_param_contents_direct(param));
+    HIP_DEBUG("num_parallel_hchains: %u\n", num_parallel_hchains);
+
+    param                = hip_get_param(msg, HIP_PARAM_HCHAIN_ANCHOR);
+
+
+    // get matching entry from hadb for HITs provided above
+    HIP_IFEL(!(entry = hip_hadb_find_byhits(local_hit, peer_hit)), -1,
+             "failed to retrieve requested HA entry\n");
+
+    // check if transforms are matching and add anchor as new local_anchor
+    HIP_IFEL(entry->esp_prot_transform != esp_prot_tfm, -1,
+             "esp prot transform changed without new BEX\n");
+    HIP_DEBUG("esp prot transforms match\n");
+
+    // we need to know the hash_length for this transform
+    hash_length = anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+    // only handle outbound direction here
+    if (direction == HIP_SPI_DIRECTION_OUT) {
+        for (i = 0; i < num_parallel_hchains; i++) {
+            esp_prot_anchor = (unsigned char *) 
hip_get_param_contents_direct(param);
+            HIP_HEXDUMP("anchor: ", esp_prot_anchor, hash_length);
+
+            // make sure that the update-anchor is set
+            HIP_IFEL(memcmp(&entry->esp_local_update_anchors[i][0], 
esp_prot_anchor, hash_length),
+                     -1, "hchain-anchors used for outbound connections NOT in 
sync\n");
+
+            // set update anchor as new active local anchor
+            memcpy(&entry->esp_local_anchors[i][0], 
&entry->esp_local_update_anchors[i][0], hash_length);
+            memset(&entry->esp_local_update_anchors[i][0], 0, MAX_HASH_LENGTH);
+
+            HIP_DEBUG("changed update_anchor to local_anchor\n");
+
+            param = hip_get_next_param(msg, param);
+        }
+
+        goto out_err;
+    }
+
+    HIP_ERROR("failure when changing update_anchor to local_anchor\n");
+    err = -1;
+
+out_err:
+    return err;
 }
 
 /** sets the ESP protection extension transform and anchor in user-messages
@@ -421,90 +423,77 @@
  * @return     0 if ok, != 0 else
  */
 int esp_prot_sa_add(hip_ha_t *entry, struct hip_common *msg, const int 
direction,
-               const int update)
+                    const int update)
 {
-       unsigned char (* hchain_anchors)[MAX_HASH_LENGTH] = NULL;
-       int hash_length = 0;
-       uint32_t hash_item_length = 0;
-       int err = 0, i;
-
-       HIP_DEBUG("direction: %i\n", direction);
-
-       // we always tell the negotiated transform to the firewall
-       HIP_DEBUG("esp protection transform is %u \n", 
entry->esp_prot_transform);
-       HIP_IFEL(hip_build_param_contents(msg, (void 
*)&entry->esp_prot_transform,
-                       HIP_PARAM_ESP_PROT_TFM, sizeof(uint8_t)), -1,
-                       "build param contents failed\n");
-
-       // but we only transmit the anchor to the firewall, if the esp 
extension is used
-       if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED)
-       {
-               hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-               // choose the anchor depending on the direction and update or 
add
-               if (update)
-               {
-                       if (direction == HIP_SPI_DIRECTION_OUT)
-                       {
-                               HIP_IFEL(!(hchain_anchors = 
entry->esp_local_update_anchors), -1,
-                                               "hchain anchor expected, but 
not present\n");
-
-                               hash_item_length = 
entry->esp_local_update_length;
-
-                       } else
-                       {
-                               HIP_IFEL(!(hchain_anchors = 
entry->esp_peer_update_anchors), -1,
-                                               "hchain anchor expected, but 
not present\n");
-
-                               hash_item_length = 
entry->esp_peer_update_length;
-                       }
-               } else
-               {
-                       if (direction == HIP_SPI_DIRECTION_OUT)
-                       {
-                               HIP_IFEL(!(hchain_anchors = 
entry->esp_local_anchors), -1,
-                                               "hchain anchor expected, but 
not present\n");
-
-                               hash_item_length = 
entry->esp_local_active_length;
-
-                       } else
-                       {
-                               HIP_IFEL(!(hchain_anchors = 
entry->esp_peer_anchors), -1,
-                                               "hchain anchor expected, but 
not present\n");
-
-                               hash_item_length = 
entry->esp_peer_active_length;
-                       }
-               }
-
-               // add parameters to hipfw message
-               HIP_IFEL(hip_build_param_contents(msg, (void 
*)&hash_item_length,
-                               HIP_PARAM_ITEM_LENGTH, sizeof(uint32_t)), -1,
-                               "build param contents failed\n");
-
-               // add parameters to hipfw message
-               HIP_IFEL(hip_build_param_contents(msg, (void 
*)&esp_prot_num_parallel_hchains,
-                               HIP_PARAM_UINT, sizeof(uint16_t)), -1,
-                               "build param contents failed\n");
-
-               for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-               {
-                       HIP_HEXDUMP("esp protection anchor is ", 
&hchain_anchors[i][0], hash_length);
-
-                       HIP_IFEL(hip_build_param_contents(msg, (void 
*)&hchain_anchors[i][0],
-                                       HIP_PARAM_HCHAIN_ANCHOR, hash_length), 
-1,
-                                       "build param contents failed\n");
-               }
-
-       } else
-       {
-               HIP_DEBUG("no anchor added, transform UNUSED\n");
-       }
-
-  out_err:
-       return err;
+    unsigned char (*hchain_anchors)[MAX_HASH_LENGTH] = NULL;
+    int hash_length           = 0;
+    uint32_t hash_item_length = 0;
+    int err                   = 0, i;
+
+    HIP_DEBUG("direction: %i\n", direction);
+
+    // we always tell the negotiated transform to the firewall
+    HIP_DEBUG("esp protection transform is %u \n", entry->esp_prot_transform);
+    HIP_IFEL(hip_build_param_contents(msg, (void *) &entry->esp_prot_transform,
+                                      HIP_PARAM_ESP_PROT_TFM, 
sizeof(uint8_t)), -1,
+             "build param contents failed\n");
+
+    // but we only transmit the anchor to the firewall, if the esp extension 
is used
+    if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED) {
+        hash_length = anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+        // choose the anchor depending on the direction and update or add
+        if (update) {
+            if (direction == HIP_SPI_DIRECTION_OUT) {
+                HIP_IFEL(!(hchain_anchors = entry->esp_local_update_anchors), 
-1,
+                         "hchain anchor expected, but not present\n");
+
+                hash_item_length = entry->esp_local_update_length;
+            } else {
+                HIP_IFEL(!(hchain_anchors = entry->esp_peer_update_anchors), 
-1,
+                         "hchain anchor expected, but not present\n");
+
+                hash_item_length = entry->esp_peer_update_length;
+            }
+        } else {
+            if (direction == HIP_SPI_DIRECTION_OUT) {
+                HIP_IFEL(!(hchain_anchors = entry->esp_local_anchors), -1,
+                         "hchain anchor expected, but not present\n");
+
+                hash_item_length = entry->esp_local_active_length;
+            } else {
+                HIP_IFEL(!(hchain_anchors = entry->esp_peer_anchors), -1,
+                         "hchain anchor expected, but not present\n");
+
+                hash_item_length = entry->esp_peer_active_length;
+            }
+        }
+
+        // add parameters to hipfw message
+        HIP_IFEL(hip_build_param_contents(msg, (void *) &hash_item_length,
+                                          HIP_PARAM_ITEM_LENGTH, 
sizeof(uint32_t)), -1,
+                 "build param contents failed\n");
+
+        // add parameters to hipfw message
+        HIP_IFEL(hip_build_param_contents(msg, (void *) 
&esp_prot_num_parallel_hchains,
+                                          HIP_PARAM_UINT, sizeof(uint16_t)), 
-1,
+                 "build param contents failed\n");
+
+        for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+            HIP_HEXDUMP("esp protection anchor is ", &hchain_anchors[i][0], 
hash_length);
+
+            HIP_IFEL(hip_build_param_contents(msg, (void *) 
&hchain_anchors[i][0],
+                                              HIP_PARAM_HCHAIN_ANCHOR, 
hash_length), -1,
+                     "build param contents failed\n");
+        }
+    } else {
+        HIP_DEBUG("no anchor added, transform UNUSED\n");
+    }
+
+out_err:
+    return err;
 }
 
-
 /********************* BEX parameters *********************/
 
 /**
@@ -515,31 +504,28 @@
  **/
 int esp_prot_r1_add_transforms(hip_common_t *msg)
 {
-       int err = 0;
-
-       /* only supported in usermode and optional there
-        *
-        * add the transform only when usermode is active */
-       if (hip_use_userspace_ipsec)
-       {
-               HIP_DEBUG("userspace IPsec hint: esp protection extension might 
be in use\n");
-
-               /* send the stored transforms */
-               HIP_IFEL(hip_build_param_esp_prot_transform(msg, 
esp_prot_num_transforms,
-                               esp_prot_transforms), -1,
-                               "Building of ESP protection mode failed\n");
-
-               HIP_DEBUG("ESP prot transforms param built\n");
-
-       } else
-       {
-               HIP_DEBUG("userspace IPsec hint: esp protection extension 
UNUSED, skip\n");
-       }
-
-       _HIP_DUMP_MSG(msg);
-
-  out_err:
-       return err;
+    int err = 0;
+
+    /* only supported in usermode and optional there
+     *
+     * add the transform only when usermode is active */
+    if (hip_use_userspace_ipsec) {
+        HIP_DEBUG("userspace IPsec hint: esp protection extension might be in 
use\n");
+
+        /* send the stored transforms */
+        HIP_IFEL(hip_build_param_esp_prot_transform(msg, 
esp_prot_num_transforms,
+                                                    esp_prot_transforms), -1,
+                 "Building of ESP protection mode failed\n");
+
+        HIP_DEBUG("ESP prot transforms param built\n");
+    } else {
+        HIP_DEBUG("userspace IPsec hint: esp protection extension UNUSED, 
skip\n");
+    }
+
+    _HIP_DUMP_MSG(msg);
+
+out_err:
+    return err;
 }
 
 /**
@@ -551,52 +537,47 @@
  **/
 int esp_prot_r1_handle_transforms(hip_ha_t *entry, struct hip_context *ctx)
 {
-       struct hip_param *param = NULL;
-       struct esp_prot_preferred_tfms *prot_transforms = NULL;
-       int err = 0;
-
-       /* this is only handled if we are using userspace ipsec,
-        * otherwise we just ignore it */
-       if (hip_use_userspace_ipsec)
-       {
-               HIP_DEBUG("userspace IPsec hint: ESP extension might be in 
use\n");
-
-               param = hip_get_param(ctx->input, 
HIP_PARAM_ESP_PROT_TRANSFORMS);
-
-               // check if the transform parameter was sent
-               if (param)
-               {
-                       HIP_DEBUG("received preferred transforms from peer\n");
-
-                       // store that we received the param for further 
processing
-                       ctx->esp_prot_param = 1;
-
-                       prot_transforms = (struct esp_prot_preferred_tfms *) 
param;
-
-                       // select transform and store it for this connection
-                       entry->esp_prot_transform = 
esp_prot_select_transform(prot_transforms->num_transforms,
-                                       prot_transforms->transforms);
-
-               } else
-               {
-                       HIP_DEBUG("R1 does not contain preferred ESP protection 
transforms, " \
-                                       "locally setting UNUSED\n");
-
-                       // store that we didn't received the param
-                       ctx->esp_prot_param = 0;
-
-                       // if the other end-host does not want to use the 
extension, we don't either
-                       entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-               }
-       } else
-       {
-               HIP_DEBUG("no userspace IPsec hint for ESP extension, locally 
setting UNUSED\n");
-
-               // make sure we don't add the anchor now and don't add any 
transform or anchor
-               entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-       }
-
-       return err;
+    struct hip_param *param                         = NULL;
+    struct esp_prot_preferred_tfms *prot_transforms = NULL;
+    int err                                         = 0;
+
+    /* this is only handled if we are using userspace ipsec,
+     * otherwise we just ignore it */
+    if (hip_use_userspace_ipsec) {
+        HIP_DEBUG("userspace IPsec hint: ESP extension might be in use\n");
+
+        param = hip_get_param(ctx->input, HIP_PARAM_ESP_PROT_TRANSFORMS);
+
+        // check if the transform parameter was sent
+        if (param) {
+            HIP_DEBUG("received preferred transforms from peer\n");
+
+            // store that we received the param for further processing
+            ctx->esp_prot_param       = 1;
+
+            prot_transforms           = (struct esp_prot_preferred_tfms *) 
param;
+
+            // select transform and store it for this connection
+            entry->esp_prot_transform = 
esp_prot_select_transform(prot_transforms->num_transforms,
+                                                                  
prot_transforms->transforms);
+        } else {
+            HIP_DEBUG("R1 does not contain preferred ESP protection " \
+                      "transforms, locally setting UNUSED\n");
+
+            // store that we didn't received the param
+            ctx->esp_prot_param       = 0;
+
+            // if the other end-host does not want to use the extension, we 
don't either
+            entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+        }
+    } else {
+        HIP_DEBUG("no userspace IPsec hint for ESP extension, locally setting 
UNUSED\n");
+
+        // make sure we don't add the anchor now and don't add any transform 
or anchor
+        entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+    }
+
+    return err;
 }
 
 /**
@@ -607,79 +588,74 @@
  * @param ctx          packet context for the I2 message
  * @return 0 on success, -1 in case of an error
  **/
-int esp_prot_i2_add_anchor(hip_common_t *i2, hip_ha_t *entry, const struct 
hip_context *ctx)
+int esp_prot_i2_add_anchor(hip_common_t *i2, hip_ha_t *entry,
+                           const struct hip_context *ctx)
 {
-       unsigned char *anchor = NULL;
-       int hash_length = 0;
-       int hash_item_length = 0;
-       int err = 0, i;
-
-       /* only add, if extension in use and we agreed on a transform
-        *
-        * @note the transform was selected in handle R1 */
-       if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED)
-       {
-               // check for sufficient elements
-               if (anchor_db_get_num_anchors(entry->esp_prot_transform) >= 
esp_prot_num_parallel_hchains)
-               {
-                       hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
-                       HIP_DEBUG("hash_length: %i\n", hash_length);
-                       hash_item_length = 
anchor_db_get_hash_item_length(entry->esp_prot_transform);
-
-                       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-                       {
-                               // add all anchors now
-                               HIP_IFEL(!(anchor = 
anchor_db_get_anchor(entry->esp_prot_transform)), -1,
-                                               "no anchor elements available, 
threading?\n");
-                               HIP_IFEL(hip_build_param_esp_prot_anchor(i2, 
entry->esp_prot_transform,
-                                               anchor, NULL, hash_length, 
hash_item_length), -1,
-                                               "Building of ESP protection 
anchor failed\n");
-
-                               // store local_anchor
-                               memcpy(&entry->esp_local_anchors[i][0], anchor, 
hash_length);
-                               HIP_HEXDUMP("stored local anchor: ", 
&entry->esp_local_anchors[i][0], hash_length);
-
-                               entry->esp_local_active_length = 
hash_item_length;
-                               HIP_DEBUG("entry->esp_local_active_length: 
%u\n",
-                                               entry->esp_local_active_length);
-                       }
-               } else
-               {
-                       // fall back
-                       HIP_ERROR("agreed on using esp hchain protection, but 
not sufficient elements");
-
-                       entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-
-                       // inform our peer
-                       HIP_IFEL(hip_build_param_esp_prot_anchor(i2, 
entry->esp_prot_transform,
-                                       NULL, NULL, 0, 0), -1,
-                                       "Building of ESP protection anchor 
failed\n");
-               }
-       } else
-       {
-               // only reply, if transforms param in R1; send UNUSED param
-               if (ctx->esp_prot_param)
-               {
-                       HIP_DEBUG("R1 contained transforms, but agreed not to 
use the extension\n");
-
-                       entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-
-                       HIP_IFEL(hip_build_param_esp_prot_anchor(i2, 
entry->esp_prot_transform,
-                                       NULL, NULL, 0, 0), -1,
-                                       "Building of ESP protection anchor 
failed\n");
-               } else
-               {
-                       HIP_DEBUG("peer didn't send transforms in R1, locally 
setting UNUSED\n");
-
-                       entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-               }
-       }
-
-  out_err:
-       if (anchor)
-               free(anchor);
-
-       return err;
+    unsigned char *anchor = NULL;
+    int hash_length       = 0;
+    int hash_item_length  = 0;
+    int err               = 0, i;
+
+    /* only add, if extension in use and we agreed on a transform
+     *
+     * @note the transform was selected in handle R1 */
+    if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED) {
+        // check for sufficient elements
+        if (anchor_db_get_num_anchors(entry->esp_prot_transform) >= 
esp_prot_num_parallel_hchains) {
+            hash_length      = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
+            HIP_DEBUG("hash_length: %i\n", hash_length);
+            hash_item_length = 
anchor_db_get_hash_item_length(entry->esp_prot_transform);
+
+            for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                // add all anchors now
+                HIP_IFEL(!(anchor = 
anchor_db_get_anchor(entry->esp_prot_transform)), -1,
+                         "no anchor elements available, threading?\n");
+                HIP_IFEL(hip_build_param_esp_prot_anchor(i2, 
entry->esp_prot_transform,
+                                                         anchor, NULL, 
hash_length, hash_item_length), -1,
+                         "Building of ESP protection anchor failed\n");
+
+                // store local_anchor
+                memcpy(&entry->esp_local_anchors[i][0], anchor, hash_length);
+                HIP_HEXDUMP("stored local anchor: ", 
&entry->esp_local_anchors[i][0], hash_length);
+
+                entry->esp_local_active_length = hash_item_length;
+                HIP_DEBUG("entry->esp_local_active_length: %u\n",
+                          entry->esp_local_active_length);
+            }
+        } else {
+            // fall back
+            HIP_ERROR("agreed on using esp hchain protection, but not 
sufficient elements");
+
+            entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+
+            // inform our peer
+            HIP_IFEL(hip_build_param_esp_prot_anchor(i2, 
entry->esp_prot_transform,
+                                                     NULL, NULL, 0, 0), -1,
+                     "Building of ESP protection anchor failed\n");
+        }
+    } else {
+        // only reply, if transforms param in R1; send UNUSED param
+        if (ctx->esp_prot_param) {
+            HIP_DEBUG("R1 contained transforms, but agreed not to use the 
extension\n");
+
+            entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+
+            HIP_IFEL(hip_build_param_esp_prot_anchor(i2, 
entry->esp_prot_transform,
+                                                     NULL, NULL, 0, 0), -1,
+                     "Building of ESP protection anchor failed\n");
+        } else {
+            HIP_DEBUG("peer didn't send transforms in R1, locally setting 
UNUSED\n");
+
+            entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+        }
+    }
+
+out_err:
+    if (anchor) {
+        free(anchor);
+    }
+
+    return err;
 }
 
 /**
@@ -691,85 +667,73 @@
  **/
 int esp_prot_i2_handle_anchor(hip_ha_t *entry, const struct hip_context *ctx)
 {
-       struct hip_tlv_common *param = NULL;
-       struct esp_prot_anchor *prot_anchor = NULL;
-       int hash_length = 0;
-       int err = 0, i;
-
-       /* only supported in user-mode ipsec and optional there */
-       if (hip_use_userspace_ipsec && esp_prot_num_transforms > 1)
-       {
-               HIP_DEBUG("userspace IPsec hint: esp protection extension might 
be in use\n");
-
-               if ( (param = hip_get_param(ctx->input, 
HIP_PARAM_ESP_PROT_ANCHOR)) )
-               {
-                       prot_anchor = (struct esp_prot_anchor *) param;
-
-                       // check if the anchor has a supported transform
-                       if (esp_prot_check_transform(esp_prot_num_transforms, 
esp_prot_transforms,
-                                       prot_anchor->transform) >= 0)
-                       {
-                               // we know this transform
-                               entry->esp_prot_transform = 
prot_anchor->transform;
-                               hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-                               if (entry->esp_prot_transform == 
ESP_PROT_TFM_UNUSED)
-                               {
-                                       HIP_DEBUG("agreed NOT to use esp 
protection extension\n");
-
-                                       // there should be no other anchors in 
this case
-                                       goto out_err;
-                               }
-
-                               // store number of elements per hash structure
-                               entry->esp_peer_active_length = 
ntohl(prot_anchor->hash_item_length);
-                               HIP_DEBUG("entry->esp_peer_active_length: %u\n",
-                                               entry->esp_peer_active_length);
-
-                               // store all contained anchors
-                               for (i = 0; i < esp_prot_num_parallel_hchains; 
i++)
-                               {
-                                       if (!prot_anchor || 
prot_anchor->transform != entry->esp_prot_transform)
-                                       {
-                                               // we expect an anchor and all 
anchors should have the same transform
-                                               err = -1;
-                                               goto out_err;
-
-                                       } else
-                                       {
-                                               // store peer_anchor
-                                               
memcpy(&entry->esp_peer_anchors[i][0], &prot_anchor->anchors[0],
-                                                               hash_length);
-                                               HIP_HEXDUMP("received anchor: 
", &entry->esp_peer_anchors[i][0],
-                                                                               
                        hash_length);
-                                       }
-
-                                       // get next anchor
-                                       param = hip_get_next_param(ctx->input, 
param);
-                                       prot_anchor = (struct esp_prot_anchor 
*) param;
-                               }
-                       } else
-                       {
-                               HIP_ERROR("received anchor with unknown 
transform, falling back\n");
-
-                               entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-                       }
-
-               } else
-               {
-                       HIP_DEBUG("NO esp anchor sent, locally setting 
UNUSED\n");
-
-                       entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-               }
-       } else
-       {
-               HIP_DEBUG("userspace IPsec hint: esp protection extension NOT 
in use\n");
-
-               entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-       }
-
-  out_err:
-       return err;
+    struct hip_tlv_common *param        = NULL;
+    struct esp_prot_anchor *prot_anchor = NULL;
+    int hash_length                     = 0;
+    int err                             = 0, i;
+
+    /* only supported in user-mode ipsec and optional there */
+    if (hip_use_userspace_ipsec && esp_prot_num_transforms > 1) {
+        HIP_DEBUG("userspace IPsec hint: esp protection extension might be in 
use\n");
+
+        if ((param = hip_get_param(ctx->input, HIP_PARAM_ESP_PROT_ANCHOR))) {
+            prot_anchor = (struct esp_prot_anchor *) param;
+
+            // check if the anchor has a supported transform
+            if (esp_prot_check_transform(esp_prot_num_transforms, 
esp_prot_transforms,
+                                         prot_anchor->transform) >= 0) {
+                // we know this transform
+                entry->esp_prot_transform = prot_anchor->transform;
+                hash_length               = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+                if (entry->esp_prot_transform == ESP_PROT_TFM_UNUSED) {
+                    HIP_DEBUG("agreed NOT to use esp protection extension\n");
+
+                    // there should be no other anchors in this case
+                    goto out_err;
+                }
+
+                // store number of elements per hash structure
+                entry->esp_peer_active_length = 
ntohl(prot_anchor->hash_item_length);
+                HIP_DEBUG("entry->esp_peer_active_length: %u\n",
+                          entry->esp_peer_active_length);
+
+                // store all contained anchors
+                for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                    if (!prot_anchor || prot_anchor->transform != 
entry->esp_prot_transform) {
+                        // we expect an anchor and all anchors should have the 
same transform
+                        err = -1;
+                        goto out_err;
+                    } else {
+                        // store peer_anchor
+                        memcpy(&entry->esp_peer_anchors[i][0], 
&prot_anchor->anchors[0],
+                               hash_length);
+                        HIP_HEXDUMP("received anchor: ", 
&entry->esp_peer_anchors[i][0],
+                                    hash_length);
+                    }
+
+                    // get next anchor
+                    param       = hip_get_next_param(ctx->input, param);
+                    prot_anchor = (struct esp_prot_anchor *) param;
+                }
+            } else {
+                HIP_ERROR("received anchor with unknown transform, falling 
back\n");
+
+                entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+            }
+        } else {
+            HIP_DEBUG("NO esp anchor sent, locally setting UNUSED\n");
+
+            entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+        }
+    } else {
+        HIP_DEBUG("userspace IPsec hint: esp protection extension NOT in 
use\n");
+
+        entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+    }
+
+out_err:
+    return err;
 }
 
 /**
@@ -781,64 +745,59 @@
  **/
 int esp_prot_r2_add_anchor(hip_common_t *r2, hip_ha_t *entry)
 {
-       unsigned char *anchor = NULL;
-       int hash_length = 0;
-       int hash_item_length = 0;
-       int err = 0, i;
-
-       // only add, if extension in use, we agreed on a transform and no error 
until now
-       if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED)
-       {
-               // check for sufficient elements
-               if (anchor_db_get_num_anchors(entry->esp_prot_transform) >= 
esp_prot_num_parallel_hchains)
-               {
-                       hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
-                       HIP_DEBUG("hash_length: %i\n", hash_length);
-
-                       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-                       {
-                               // add all anchors now
-                               HIP_IFEL(!(anchor = 
anchor_db_get_anchor(entry->esp_prot_transform)), -1,
-                                               "no anchor elements available, 
threading?\n");
-                               hash_item_length = 
anchor_db_get_hash_item_length(entry->esp_prot_transform);
-                               HIP_IFEL(hip_build_param_esp_prot_anchor(r2, 
entry->esp_prot_transform,
-                                               anchor, NULL, hash_length, 
hash_item_length), -1,
-                                               "Building of ESP protection 
anchor failed\n");
-
-                               // store local_anchor
-                               memcpy(&entry->esp_local_anchors[i][0], anchor, 
hash_length);
-                               HIP_HEXDUMP("stored local anchor: ", 
&entry->esp_local_anchors[i][0], hash_length);
-
-                               entry->esp_local_active_length = 
anchor_db_get_hash_item_length(
-                                               entry->esp_prot_transform);
-                               HIP_DEBUG("entry->esp_local_active_length: 
%u\n",
-                                               entry->esp_local_active_length);
-                       }
-
-               } else
-               {
-                       // fall back
-                       HIP_ERROR("agreed on using esp hchain protection, but 
no elements");
-
-                       entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-
-                       // inform our peer about fallback
-                       HIP_IFEL(hip_build_param_esp_prot_anchor(r2, 
entry->esp_prot_transform,
-                                       NULL, NULL, 0, 0), -1,
-                                       "Building of ESP protection anchor 
failed\n");
-               }
-       } else
-       {
-               HIP_DEBUG("esp protection extension NOT in use for this 
connection\n");
-
-               entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-       }
-
-  out_err:
-       if (anchor)
-               free(anchor);
-
-       return err;
+    unsigned char *anchor = NULL;
+    int hash_length       = 0;
+    int hash_item_length  = 0;
+    int err               = 0, i;
+
+    // only add, if extension in use, we agreed on a transform and no error 
until now
+    if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED) {
+        // check for sufficient elements
+        if (anchor_db_get_num_anchors(entry->esp_prot_transform) >= 
esp_prot_num_parallel_hchains) {
+            hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
+            HIP_DEBUG("hash_length: %i\n", hash_length);
+
+            for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                // add all anchors now
+                HIP_IFEL(!(anchor = 
anchor_db_get_anchor(entry->esp_prot_transform)), -1,
+                         "no anchor elements available, threading?\n");
+                hash_item_length = 
anchor_db_get_hash_item_length(entry->esp_prot_transform);
+                HIP_IFEL(hip_build_param_esp_prot_anchor(r2, 
entry->esp_prot_transform,
+                                                         anchor, NULL, 
hash_length, hash_item_length), -1,
+                         "Building of ESP protection anchor failed\n");
+
+                // store local_anchor
+                memcpy(&entry->esp_local_anchors[i][0], anchor, hash_length);
+                HIP_HEXDUMP("stored local anchor: ", 
&entry->esp_local_anchors[i][0], hash_length);
+
+                entry->esp_local_active_length = 
anchor_db_get_hash_item_length(
+                    entry->esp_prot_transform);
+                HIP_DEBUG("entry->esp_local_active_length: %u\n",
+                          entry->esp_local_active_length);
+            }
+        } else {
+            // fall back
+            HIP_ERROR("agreed on using esp hchain protection, but no 
elements");
+
+            entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+
+            // inform our peer about fallback
+            HIP_IFEL(hip_build_param_esp_prot_anchor(r2, 
entry->esp_prot_transform,
+                                                     NULL, NULL, 0, 0), -1,
+                     "Building of ESP protection anchor failed\n");
+        }
+    } else {
+        HIP_DEBUG("esp protection extension NOT in use for this connection\n");
+
+        entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+    }
+
+out_err:
+    if (anchor) {
+        free(anchor);
+    }
+
+    return err;
 }
 
 /**
@@ -850,83 +809,70 @@
  **/
 int esp_prot_r2_handle_anchor(hip_ha_t *entry, const struct hip_context *ctx)
 {
-       struct hip_tlv_common *param = NULL;
-       struct esp_prot_anchor *prot_anchor = NULL;
-       int hash_length = 0;
-       int err = 0, i;
-
-       // only process anchor, if we agreed on using it before
-       if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED)
-       {
-               if ( (param = hip_get_param(ctx->input, 
HIP_PARAM_ESP_PROT_ANCHOR)) )
-               {
-                       prot_anchor = (struct esp_prot_anchor *) param;
-
-                       // check if the anchor has got the negotiated transform
-                       if (prot_anchor->transform == entry->esp_prot_transform)
-                       {
-                               hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-                               // store number of elements per hash structure
-                               entry->esp_peer_active_length = 
ntohl(prot_anchor->hash_item_length);
-                               HIP_DEBUG("entry->esp_peer_active_length: %u\n",
-                                               entry->esp_peer_active_length);
-
-                               // store all contained anchors
-                               for (i = 0; i < esp_prot_num_parallel_hchains; 
i++)
-                               {
-                                       if (prot_anchor->transform != 
entry->esp_prot_transform || !prot_anchor)
-                                       {
-                                               // we expect an anchor and all 
anchors should have the same transform
-                                               err = -1;
-                                               goto out_err;
-
-                                       } else
-                                       {
-                                               // store peer_anchor
-                                               
memcpy(&entry->esp_peer_anchors[i][0], &prot_anchor->anchors[0],
-                                                               hash_length);
-                                               HIP_HEXDUMP("received anchor: 
", &entry->esp_peer_anchors[i][0],
-                                                                               
                        hash_length);
-                                       }
-
-                                       // get next anchor
-                                       param = hip_get_next_param(ctx->input, 
param);
-                                       prot_anchor = (struct esp_prot_anchor 
*) param;
-                               }
-                       } else if (prot_anchor->transform == 
ESP_PROT_TFM_UNUSED)
-                       {
-                               HIP_DEBUG("peer encountered problems and did 
fallback\n");
-
-                               // also fallback
-                               entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-
-                       } else
-                       {
-                               HIP_ERROR("received anchor does NOT use 
negotiated transform, falling back\n");
-
-                               // fallback
-                               entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-                       }
-               } else
-               {
-                       HIP_DEBUG("agreed on using esp hchain extension, but no 
anchor sent or error\n");
-
-                       // fall back option
-                       entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-               }
-       } else
-       {
-               HIP_DEBUG("NOT using esp protection extension\n");
-
-               entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
-       }
-
-  out_err:
-       return err;
+    struct hip_tlv_common *param        = NULL;
+    struct esp_prot_anchor *prot_anchor = NULL;
+    int hash_length                     = 0;
+    int err                             = 0, i;
+
+    // only process anchor, if we agreed on using it before
+    if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED) {
+        if ((param = hip_get_param(ctx->input, HIP_PARAM_ESP_PROT_ANCHOR))) {
+            prot_anchor = (struct esp_prot_anchor *) param;
+
+            // check if the anchor has got the negotiated transform
+            if (prot_anchor->transform == entry->esp_prot_transform) {
+                hash_length                   = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+                // store number of elements per hash structure
+                entry->esp_peer_active_length = 
ntohl(prot_anchor->hash_item_length);
+                HIP_DEBUG("entry->esp_peer_active_length: %u\n",
+                          entry->esp_peer_active_length);
+
+                // store all contained anchors
+                for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                    if (prot_anchor->transform != entry->esp_prot_transform || 
!prot_anchor) {
+                        // we expect an anchor and all anchors should have the 
same transform
+                        err = -1;
+                        goto out_err;
+                    } else {
+                        // store peer_anchor
+                        memcpy(&entry->esp_peer_anchors[i][0], 
&prot_anchor->anchors[0],
+                               hash_length);
+                        HIP_HEXDUMP("received anchor: ", 
&entry->esp_peer_anchors[i][0],
+                                    hash_length);
+                    }
+
+                    // get next anchor
+                    param       = hip_get_next_param(ctx->input, param);
+                    prot_anchor = (struct esp_prot_anchor *) param;
+                }
+            } else if (prot_anchor->transform == ESP_PROT_TFM_UNUSED) {
+                HIP_DEBUG("peer encountered problems and did fallback\n");
+
+                // also fallback
+                entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+            } else {
+                HIP_ERROR("received anchor does NOT use negotiated transform, 
falling back\n");
+
+                // fallback
+                entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+            }
+        } else {
+            HIP_DEBUG("agreed on using esp hchain extension, but no anchor 
sent or error\n");
+
+            // fall back option
+            entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+        }
+    } else {
+        HIP_DEBUG("NOT using esp protection extension\n");
+
+        entry->esp_prot_transform = ESP_PROT_TFM_UNUSED;
+    }
+
+out_err:
+    return err;
 }
 
-
 /******************** UPDATE parameters *******************/
 
 /**
@@ -939,59 +885,63 @@
  * @return 0 on success, -1 in case of an error
  **/
 int esp_prot_handle_update(const hip_common_t *recv_update, hip_ha_t *entry,
-                          const in6_addr_t *src_ip, const in6_addr_t *dst_ip)
+                           const in6_addr_t *src_ip, const in6_addr_t *dst_ip)
 {
-       struct hip_seq * seq = NULL;
-       struct hip_ack * ack = NULL;
-       struct hip_esp_info * esp_info = NULL;
-       uint32_t spi = 0;
-       int err = 0;
-
-       HIP_ASSERT(entry != NULL);
-
-       seq = (struct hip_seq *) hip_get_param(recv_update, HIP_PARAM_SEQ);
-       ack = (struct hip_ack *) hip_get_param(recv_update, HIP_PARAM_ACK);
-       esp_info = (struct hip_esp_info *) hip_get_param(recv_update, 
HIP_PARAM_ESP_INFO);
-
-       if (seq && !ack && !esp_info)
-       {
-               /* this is the first ANCHOR-UPDATE msg
-                *
-                * @note contains anchors -> update inbound SA
-                * @note response has to contain corresponding ACK and ESP_INFO 
*/
-               HIP_IFEL(esp_prot_update_handle_anchor(recv_update, entry,
-                               src_ip, dst_ip, &spi), -1,
-                               "failed to handle anchor in UPDATE msg\n");
-               HIP_DEBUG("successfully processed anchors in ANCHOR-UPDATE\n");
-
-               // send ANCHOR_UPDATE response, when the anchor was verified 
above
-               HIP_IFEL(esp_prot_send_update_response(recv_update, entry, 
dst_ip,
-                               src_ip, spi), -1, "failed to send UPDATE 
replay");
-
-       } else if (!seq && ack && esp_info)
-       {
-               /* this is the second ANCHOR-UPDATE msg
-                *
-                * @note contains ACK for previously sent anchors -> update 
outbound SA */
-               HIP_DEBUG("received ACK for previously sent ANCHOR-UPDATE\n");
-
-               // the update was successful, stop retransmission
-               entry->update_state = 0;
-
-               // notify sadb about next anchor
-               HIP_IFEL(entry->hadb_ipsec_func->hip_add_sa(dst_ip, src_ip,
-                               &entry->hit_our, &entry->hit_peer, 
entry->spi_outbound_new,
-                               entry->esp_transform, &entry->esp_out, 
&entry->auth_out, 0,
-                               HIP_SPI_DIRECTION_OUT, 1, entry), -1,
-                               "failed to notify sadb about next anchor\n");
-
-       } else
-       {
-               HIP_DEBUG("NOT a pure ANCHOR-UPDATE, unhandled\n");
-       }
-
-  out_err:
-       return err;
+    struct hip_seq *seq           = NULL;
+    struct hip_ack *ack           = NULL;
+    struct hip_esp_info *esp_info = NULL;
+    uint32_t spi                  = 0;
+    int err                       = 0;
+
+    HIP_ASSERT(entry != NULL);
+
+    seq      = (struct hip_seq *) hip_get_param(recv_update, HIP_PARAM_SEQ);
+    ack      = (struct hip_ack *) hip_get_param(recv_update, HIP_PARAM_ACK);
+    esp_info = (struct hip_esp_info *) hip_get_param(recv_update, 
HIP_PARAM_ESP_INFO);
+
+    if (seq && !ack && !esp_info) {
+        /* this is the first ANCHOR-UPDATE msg
+         *
+         * @note contains anchors -> update inbound SA
+         * @note response has to contain corresponding ACK and ESP_INFO */
+        HIP_IFEL(esp_prot_update_handle_anchor(recv_update, entry,
+                                               src_ip, dst_ip, &spi), -1,
+                 "failed to handle anchor in UPDATE msg\n");
+        HIP_DEBUG("successfully processed anchors in ANCHOR-UPDATE\n");
+
+        // send ANCHOR_UPDATE response, when the anchor was verified above
+        HIP_IFEL(esp_prot_send_update_response(recv_update, entry, dst_ip,
+                                               src_ip, spi), -1, "failed to 
send UPDATE replay");
+    } else if (!seq && ack && esp_info) {
+        /* this is the second ANCHOR-UPDATE msg
+         *
+         * @note contains ACK for previously sent anchors -> update outbound 
SA */
+        HIP_DEBUG("received ACK for previously sent ANCHOR-UPDATE\n");
+
+        // the update was successful, stop retransmission
+        entry->update_state = 0;
+
+        // notify sadb about next anchor
+        HIP_IFEL(entry->hadb_ipsec_func->hip_add_sa(dst_ip,
+                                                    src_ip,
+                                                    &entry->hit_our,
+                                                    &entry->hit_peer,
+                                                    entry->spi_outbound_new,
+                                                    entry->esp_transform,
+                                                    &entry->esp_out,
+                                                    &entry->auth_out,
+                                                    0,
+                                                    HIP_SPI_DIRECTION_OUT,
+                                                    1,
+                                                    entry),
+                 -1,
+                 "failed to notify sadb about next anchor\n");
+    } else {
+        HIP_DEBUG("NOT a pure ANCHOR-UPDATE, unhandled\n");
+    }
+
+out_err:
+    return err;
 }
 
 /**
@@ -1003,60 +953,60 @@
  **/
 int esp_prot_update_add_anchor(hip_common_t *update, hip_ha_t *entry)
 {
-       struct hip_seq * seq = NULL;
-       int hash_length = 0;
-       int err = 0, i;
-
-       // only do further processing when extension is in use
-       if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED)
-       {
-               /* we only want to send anchors in 1. and 2. UPDATE message
-                *
-                * @note we can distinguish the 1. and 2. UPDATE message by
-                *               looking at the presence of SEQ param in the 
packet
-                *               to be sent */
-               seq = (struct hip_seq *) hip_get_param(update, HIP_PARAM_SEQ);
-
-               if (seq)
-               {
-                       // we need to know the hash_length for this transform
-                       hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-                       /* @note update-anchor will be set, if there was a 
anchor UPDATE before
-                        *       or if this is an anchor UPDATE; otherwise 
update-anchor will
-                        *       be NULL
-                        *
-                        * XX TODO we need to choose the correct SA with the 
anchor we want to
-                        *         update, when supporting multihoming and when 
this is a
-                        *         pure anchor-update */
-                       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-                       {
-                               HIP_IFEL(hip_build_param_esp_prot_anchor(update,
-                                               entry->esp_prot_transform, 
&entry->esp_local_anchors[i][0],
-                                               
&entry->esp_local_update_anchors[i][0], hash_length, entry->hash_item_length),
-                                               -1, "building of ESP protection 
ANCHOR failed\n");
-                       }
-
-                       // only add the root if it is specified
-                       if (entry->esp_root_length > 0)
-                       {
-                               for (i = 0; i < esp_prot_num_parallel_hchains; 
i++)
-                               {
-                                       
HIP_IFEL(hip_build_param_esp_prot_root(update,
-                                                       entry->esp_root_length, 
&entry->esp_root[i][0]), -1,
-                                                       "building of ESP ROOT 
failed\n");
-                               }
-                       }
-
-                       entry->esp_local_update_length = 
anchor_db_get_hash_item_length(
-                                       entry->esp_prot_transform);
-                       HIP_DEBUG("entry->esp_local_update_length: %u\n",
-                                       entry->esp_local_update_length);
-               }
-       }
-
-  out_err:
-       return err;
+    struct hip_seq *seq = NULL;
+    int hash_length     = 0;
+    int err             = 0, i;
+
+    // only do further processing when extension is in use
+    if (entry->esp_prot_transform > ESP_PROT_TFM_UNUSED) {
+        /* we only want to send anchors in 1. and 2. UPDATE message
+         *
+         * @note we can distinguish the 1. and 2. UPDATE message by
+         *       looking at the presence of SEQ param in the packet
+         *       to be sent */
+        seq = (struct hip_seq *) hip_get_param(update, HIP_PARAM_SEQ);
+
+        if (seq) {
+            // we need to know the hash_length for this transform
+            hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+            /* @note update-anchor will be set, if there was a anchor UPDATE 
before
+             *       or if this is an anchor UPDATE; otherwise update-anchor 
will
+             *       be NULL
+             *
+             * XX TODO we need to choose the correct SA with the anchor we 
want to
+             *         update, when supporting multihoming and when this is a
+             *         pure anchor-update */
+            for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                HIP_IFEL(hip_build_param_esp_prot_anchor(update,
+                                                         
entry->esp_prot_transform,
+                                                         
&entry->esp_local_anchors[i][0],
+                                                         
&entry->esp_local_update_anchors[i][0],
+                                                         hash_length, 
entry->hash_item_length),
+                         -1,
+                         "building of ESP protection ANCHOR failed\n");
+            }
+
+            // only add the root if it is specified
+            if (entry->esp_root_length > 0) {
+                for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                    HIP_IFEL(hip_build_param_esp_prot_root(update,
+                                                           
entry->esp_root_length,
+                                                           
&entry->esp_root[i][0]),
+                             -1,
+                             "building of ESP ROOT failed\n");
+                }
+            }
+
+            entry->esp_local_update_length = anchor_db_get_hash_item_length(
+                entry->esp_prot_transform);
+            HIP_DEBUG("entry->esp_local_update_length: %u\n",
+                      entry->esp_local_update_length);
+        }
+    }
+
+out_err:
+    return err;
 }
 
 /**
@@ -1069,121 +1019,123 @@
  * @param spi                  the ipsec spi number
  * @return 0 on success, -1 in case of an error
  **/
-int esp_prot_update_handle_anchor(const hip_common_t *recv_update, hip_ha_t 
*entry,
-               const in6_addr_t *src_ip, const in6_addr_t *dst_ip, uint32_t 
*spi)
+int esp_prot_update_handle_anchor(const hip_common_t *recv_update,
+                                  hip_ha_t *entry,
+                                  const in6_addr_t *src_ip,
+                                  const in6_addr_t *dst_ip,
+                                  uint32_t *spi)
 {
-       struct esp_prot_anchor *prot_anchor = NULL;
-       struct hip_tlv_common *param = NULL;
-       int hash_length = 0;
-       unsigned char cmp_value[MAX_HASH_LENGTH];
-       int err = 0, i;
-
-       HIP_ASSERT(spi != NULL);
-
-       *spi = 0;
-
-       param = hip_get_param(recv_update, HIP_PARAM_ESP_PROT_ANCHOR);
-       prot_anchor = (struct esp_prot_anchor *) param;
-
-       if (prot_anchor)
-       {
-               /* XX TODO find matching SA entry in host association for 
active_anchor
-                *         and _inbound_ direction */
-
-               // we need to know the hash_length for this transform
-               hash_length = 
anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-               // compare peer_update_anchor to 0
-               memset(cmp_value, 0, MAX_HASH_LENGTH);
-
-               /* treat the very first hchain update after the BEX differently
-                * -> assume properties of first parallal chain same as for 
others */
-               if (!memcmp(&entry->esp_peer_update_anchors[0][0], 
&cmp_value[0], MAX_HASH_LENGTH))
-               {
-                       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-                       {
-                               // check that we are receiving an anchor 
matching the negotiated transform
-                               HIP_IFEL(entry->esp_prot_transform != 
prot_anchor->transform, -1,
-                                               "esp prot transform changed 
without new BEX\n");
-                               HIP_DEBUG("esp prot transforms match\n");
-
-                               // check that we are receiving an anchor 
matching the active one
-                               HIP_IFEL(memcmp(&prot_anchor->anchors[0], 
&entry->esp_peer_anchors[i][0],
-                                               hash_length), -1, "esp prot 
active peer anchors do NOT match\n");
-                               HIP_DEBUG("esp prot active peer anchors 
match\n");
-
-                               // set the update anchor as the peer's update 
anchor
-                               //memset(entry->esp_peer_update_anchor, 0, 
MAX_HASH_LENGTH);
-                               memcpy(&entry->esp_peer_update_anchors[i][0], 
&prot_anchor->anchors[hash_length],
-                                               hash_length);
-                               HIP_DEBUG("peer_update_anchor set\n");
-
-                               entry->esp_peer_update_length = 
ntohl(prot_anchor->hash_item_length);
-                               HIP_DEBUG("entry->esp_peer_update_length: %u\n",
-                                               entry->esp_peer_update_length);
-
-                               param = hip_get_next_param(recv_update, param);
-                               prot_anchor = (struct esp_prot_anchor *) param;
-                       }
-               } else if (!memcmp(&entry->esp_peer_update_anchors[0][0], 
&prot_anchor->anchors[0],
-                                       hash_length))
-               {
-                       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-                       {
-                               // check that we are receiving an anchor 
matching the active one
-                               HIP_IFEL(memcmp(&prot_anchor->anchors[0], 
&entry->esp_peer_update_anchors[i][0],
-                                               hash_length), -1, "esp prot 
active peer anchors do NOT match\n");
-                               HIP_DEBUG("last received esp prot update peer 
anchor and sent one match\n");
-
-                               // track the anchor updates by moving one 
anchor forward
-                               memcpy(&entry->esp_peer_anchors[i][0], 
&entry->esp_peer_update_anchors[i][0], hash_length);
-
-                               // set the update anchor as the peer's update 
anchor
-                               //memset(entry->esp_peer_update_anchor, 0, 
MAX_HASH_LENGTH);
-                               memcpy(&entry->esp_peer_update_anchors[i][0], 
&prot_anchor->anchors[hash_length],
-                                               hash_length);
-                               HIP_DEBUG("peer_update_anchor set\n");
-
-                               entry->esp_peer_update_length = 
ntohl(prot_anchor->hash_item_length);
-                               HIP_DEBUG("entry->esp_peer_update_length: %u\n",
-                                               entry->esp_peer_update_length);
-
-                               param = hip_get_next_param(recv_update, param);
-                               prot_anchor = (struct esp_prot_anchor *) param;
-                       }
-               } else
-               {
-                       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-                       {
-                               prot_anchor = (struct esp_prot_anchor *) param;
-
-                               HIP_IFEL(memcmp(&prot_anchor->anchors[0], 
&entry->esp_peer_anchors[i][0],
-                                               hash_length), -1, "received 
unverifiable anchor\n");
-
-                               /**** received newer update for active anchor 
****/
-
-                               // set the update anchor as the peer's update 
anchor
-                               //memset(entry->esp_peer_update_anchor, 0, 
MAX_HASH_LENGTH);
-                               memcpy(&entry->esp_peer_update_anchors[i][0], 
&prot_anchor->anchors[hash_length],
-                                               hash_length);
-                               HIP_DEBUG("peer_update_anchor set\n");
-
-                               entry->esp_peer_update_length = 
ntohl(prot_anchor->hash_item_length);
-                               HIP_DEBUG("entry->esp_peer_update_length: %u\n",
-                                               entry->esp_peer_update_length);
-                       }
-               }
-
-               /* @note spi is also needed in ACK packet
-                * @note like this we do NOT support multihoming
-                *
-                * XX TODO instead use the SA of the SPI looked up in TODO above
-                * when merging with UPDATE re-implementation */
-               *spi = entry->spi_inbound_current;
-
-               // as we don't verify the hashes in the end-host, we don't have 
to update the outbound SA now
-       }
-
-  out_err:
-       return err;
+    struct esp_prot_anchor *prot_anchor = NULL;
+    struct hip_tlv_common *param        = NULL;
+    int hash_length                     = 0;
+    unsigned char cmp_value[MAX_HASH_LENGTH];
+    int err                             = 0, i;
+
+    HIP_ASSERT(spi != NULL);
+
+    *spi        = 0;
+
+    param       = hip_get_param(recv_update, HIP_PARAM_ESP_PROT_ANCHOR);
+    prot_anchor = (struct esp_prot_anchor *) param;
+
+    if (prot_anchor) {
+        /* XX TODO find matching SA entry in host association for active_anchor
+         *         and _inbound_ direction */
+
+        // we need to know the hash_length for this transform
+        hash_length = anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+        // compare peer_update_anchor to 0
+        memset(cmp_value, 0, MAX_HASH_LENGTH);
+
+        /* treat the very first hchain update after the BEX differently
+         * -> assume properties of first parallal chain same as for others */
+        if (!memcmp(&entry->esp_peer_update_anchors[0][0], &cmp_value[0], 
MAX_HASH_LENGTH)) {
+            for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                // check that we are receiving an anchor matching the 
negotiated transform
+                HIP_IFEL(entry->esp_prot_transform != prot_anchor->transform, 
-1,
+                         "esp prot transform changed without new BEX\n");
+                HIP_DEBUG("esp prot transforms match\n");
+
+                // check that we are receiving an anchor matching the active 
one
+                HIP_IFEL(memcmp(&prot_anchor->anchors[0], 
&entry->esp_peer_anchors[i][0],
+                                hash_length), -1, "esp prot active peer 
anchors do NOT match\n");
+                HIP_DEBUG("esp prot active peer anchors match\n");
+
+                // set the update anchor as the peer's update anchor
+                //memset(entry->esp_peer_update_anchor, 0, MAX_HASH_LENGTH);
+                memcpy(&entry->esp_peer_update_anchors[i][0], 
&prot_anchor->anchors[hash_length],
+                       hash_length);
+                HIP_DEBUG("peer_update_anchor set\n");
+
+                entry->esp_peer_update_length = 
ntohl(prot_anchor->hash_item_length);
+                HIP_DEBUG("entry->esp_peer_update_length: %u\n",
+                          entry->esp_peer_update_length);
+
+                param                         = 
hip_get_next_param(recv_update, param);
+                prot_anchor                   = (struct esp_prot_anchor *) 
param;
+            }
+        } else if (!memcmp(&entry->esp_peer_update_anchors[0][0], 
&prot_anchor->anchors[0],
+                           hash_length)) {
+            for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                // check that we are receiving an anchor matching the active 
one
+                HIP_IFEL(memcmp(&prot_anchor->anchors[0], 
&entry->esp_peer_update_anchors[i][0],
+                                hash_length), -1, "esp prot active peer 
anchors do NOT match\n");
+                HIP_DEBUG("last received esp prot update peer anchor and sent 
one match\n");
+
+                // track the anchor updates by moving one anchor forward
+                memcpy(&entry->esp_peer_anchors[i][0],
+                       &entry->esp_peer_update_anchors[i][0],
+                       hash_length);
+
+                // set the update anchor as the peer's update anchor
+                //memset(entry->esp_peer_update_anchor, 0, MAX_HASH_LENGTH);
+                memcpy(&entry->esp_peer_update_anchors[i][0], 
&prot_anchor->anchors[hash_length],
+                       hash_length);
+                HIP_DEBUG("peer_update_anchor set\n");
+
+                entry->esp_peer_update_length = 
ntohl(prot_anchor->hash_item_length);
+                HIP_DEBUG("entry->esp_peer_update_length: %u\n",
+                          entry->esp_peer_update_length);
+
+                param                         = 
hip_get_next_param(recv_update, param);
+                prot_anchor                   = (struct esp_prot_anchor *) 
param;
+            }
+        } else {
+            for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+                prot_anchor = (struct esp_prot_anchor *) param;
+
+                HIP_IFEL(memcmp(&prot_anchor->anchors[0], 
&entry->esp_peer_anchors[i][0],
+                                hash_length), -1, "received unverifiable 
anchor\n");
+
+                /**** received newer update for active anchor ****/
+
+                // set the update anchor as the peer's update anchor
+                //memset(entry->esp_peer_update_anchor, 0, MAX_HASH_LENGTH);
+                memcpy(&entry->esp_peer_update_anchors[i][0],
+                       &prot_anchor->anchors[hash_length],
+                       hash_length);
+
+                HIP_DEBUG("peer_update_anchor set\n");
+
+                entry->esp_peer_update_length = 
ntohl(prot_anchor->hash_item_length);
+                HIP_DEBUG("entry->esp_peer_update_length: %u\n",
+                          entry->esp_peer_update_length);
+            }
+        }
+
+        /* @note spi is also needed in ACK packet
+         * @note like this we do NOT support multihoming
+         *
+         * XX TODO instead use the SA of the SPI looked up in TODO above
+         * when merging with UPDATE re-implementation */
+        *spi = entry->spi_inbound_current;
+
+        /* as we don't verify the hashes in the end-host, we don't have to
+         * update the outbound SA now
+         */
+    }
+
+out_err:
+    return err;
 }

=== modified file 'hipd/esp_prot_hipd_msg.h'
--- hipd/esp_prot_hipd_msg.h    2010-01-19 09:28:42 +0000
+++ hipd/esp_prot_hipd_msg.h    2010-02-10 22:32:46 +0000
@@ -22,17 +22,22 @@
 int esp_prot_handle_trigger_update_msg(const struct hip_common *msg);
 int esp_prot_handle_anchor_change_msg(const struct hip_common *msg);
 int esp_prot_sa_add(hip_ha_t *entry, struct hip_common *msg, const int 
direction,
-               const int update);
+                    const int update);
 int esp_prot_r1_add_transforms(hip_common_t *msg);
 int esp_prot_r1_handle_transforms(hip_ha_t *entry, struct hip_context *ctx);
-int esp_prot_i2_add_anchor(hip_common_t *i2, hip_ha_t *entry, const struct 
hip_context *ctx);
+int esp_prot_i2_add_anchor(hip_common_t *i2,
+                           hip_ha_t *entry,
+                           const struct hip_context *ctx);
 int esp_prot_i2_handle_anchor(hip_ha_t *entry, const struct hip_context *ctx);
 int esp_prot_r2_add_anchor(hip_common_t *r2, hip_ha_t *entry);
 int esp_prot_r2_handle_anchor(hip_ha_t *entry, const struct hip_context *ctx);
 int esp_prot_handle_update(const hip_common_t *recv_update, hip_ha_t *entry,
-                          const in6_addr_t *src_ip, const in6_addr_t *dst_ip);
+                           const in6_addr_t *src_ip, const in6_addr_t *dst_ip);
 int esp_prot_update_add_anchor(hip_common_t *update, hip_ha_t *entry);
-int esp_prot_update_handle_anchor(const hip_common_t *recv_update, hip_ha_t 
*entry,
-               const in6_addr_t *src_ip, const in6_addr_t *dst_ip, uint32_t 
*spi);
+int esp_prot_update_handle_anchor(const hip_common_t *recv_update,
+                                  hip_ha_t *entry,
+                                  const in6_addr_t *src_ip,
+                                  const in6_addr_t *dst_ip,
+                                  uint32_t *spi);
 
 #endif /*ESP_PROT_HIPD_MSG_H_*/

=== modified file 'hipd/esp_prot_light_update.c'
--- hipd/esp_prot_light_update.c        2010-01-19 09:28:42 +0000
+++ hipd/esp_prot_light_update.c        2010-02-10 22:32:46 +0000
@@ -26,38 +26,40 @@
  * @return 0 in case of succcess, -1 otherwise
  **/
 static int esp_prot_send_light_ack(hip_ha_t *entry, const in6_addr_t *src_addr,
-               const in6_addr_t *dst_addr, const uint32_t spi)
+                                   const in6_addr_t *dst_addr, const uint32_t 
spi)
 {
-       hip_common_t *light_ack = NULL;
-       uint16_t mask = 0;
-       int err = 0;
-
-       HIP_IFEL(!(light_ack = hip_msg_alloc()), -ENOMEM,
-                "failed to allocate memory\n");
-
-       entry->hadb_misc_func->hip_build_network_hdr(light_ack, HIP_LUPDATE,
-                                                        mask, &entry->hit_our,
-                                                        &entry->hit_peer);
-
-       /* Add ESP_INFO */
-       HIP_IFEL(hip_build_param_esp_info(light_ack, 
entry->current_keymat_index,
-                       spi, spi), -1, "Building of ESP_INFO failed\n");
-
-       /* Add ACK */
-       HIP_IFEL(hip_build_param_ack(light_ack, entry->light_update_id_in), -1,
-                       "Building of ACK failed\n");
-
-       /* Add HMAC */
-       HIP_IFEL(hip_build_param_hmac_contents(light_ack, 
&entry->hip_hmac_out), -1,
-                       "Building of HMAC failed\n");
-
-       HIP_IFEL(entry->hadb_xmit_func->hip_send_pkt(src_addr, dst_addr,
-                               (entry->nat_mode ? hip_get_local_nat_udp_port() 
: 0), entry->peer_udp_port,
-                               light_ack, entry, 0), -1, "failed to send 
ANCHOR-UPDATE\n");
-
-  out_err:
-       return err;
-
+    hip_common_t *light_ack = NULL;
+    uint16_t mask           = 0;
+    int err                 = 0;
+
+    HIP_IFEL(!(light_ack = hip_msg_alloc()), -ENOMEM,
+             "failed to allocate memory\n");
+
+    entry->hadb_misc_func->hip_build_network_hdr(light_ack, HIP_LUPDATE,
+                                                 mask, &entry->hit_our,
+                                                 &entry->hit_peer);
+
+    /* Add ESP_INFO */
+    HIP_IFEL(hip_build_param_esp_info(light_ack, entry->current_keymat_index,
+                                      spi, spi), -1, "Building of ESP_INFO 
failed\n");
+
+    /* Add ACK */
+    HIP_IFEL(hip_build_param_ack(light_ack, entry->light_update_id_in), -1,
+             "Building of ACK failed\n");
+
+    /* Add HMAC */
+    HIP_IFEL(hip_build_param_hmac_contents(light_ack, &entry->hip_hmac_out), 
-1,
+             "Building of HMAC failed\n");
+
+    HIP_IFEL(entry->hadb_xmit_func->hip_send_pkt(src_addr, dst_addr,
+                                                 (entry->nat_mode ? 
hip_get_local_nat_udp_port() : 0),
+                                                 entry->peer_udp_port,
+                                                 light_ack, entry, 0),
+             -1,
+             "failed to send ANCHOR-UPDATE\n");
+
+out_err:
+    return err;
 }
 
 /**
@@ -71,85 +73,92 @@
  * @param branch_length                length of each branch
  * @return 0 in case of succcess, -1 otherwise
  **/
-int esp_prot_send_light_update(hip_ha_t *entry, const int anchor_offset[],
-               unsigned char *secret[MAX_NUM_PARALLEL_HCHAINS], const int 
secret_length[],
-               unsigned char *branch_nodes[MAX_NUM_PARALLEL_HCHAINS], const 
int branch_length[])
+int esp_prot_send_light_update(hip_ha_t *entry,
+                               const int anchor_offset[],
+                               unsigned char *secret[MAX_NUM_PARALLEL_HCHAINS],
+                               const int secret_length[],
+                               unsigned char 
*branch_nodes[MAX_NUM_PARALLEL_HCHAINS],
+                               const int branch_length[])
 {
-       hip_common_t *light_update = NULL;
-       int hash_length = 0;
-       uint16_t mask = 0;
-       int err = 0, i;
-
-       HIP_IFEL(!(light_update = hip_msg_alloc()), -ENOMEM,
-                "failed to allocate memory\n");
-
-       entry->hadb_misc_func->hip_build_network_hdr(light_update, HIP_LUPDATE,
-                                                    mask, &entry->hit_our, 
&entry->hit_peer);
-
-       /********************* add SEQ *********************/
-
-       entry->light_update_id_out++;
-       HIP_DEBUG("outgoing light UPDATE ID=%u\n", entry->light_update_id_out);
-
-       HIP_IFEL(hip_build_param_seq(light_update, entry->light_update_id_out), 
-1,
-                       "building of SEQ param failed\n");
-
-       /********** add ESP-PROT anchor, branch, secret, root **********/
-
-       hash_length = anchor_db_get_anchor_length(entry->esp_prot_transform);
-
-       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-       {
-               HIP_IFEL(hip_build_param_esp_prot_anchor(light_update,
-                               entry->esp_prot_transform, 
&entry->esp_local_anchors[i][0],
-                               &entry->esp_local_update_anchors[i][0], 
hash_length, entry->hash_item_length),
-                               -1, "building of ESP protection ANCHOR 
failed\n");
-       }
-
-       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-       {
-               HIP_IFEL(hip_build_param_esp_prot_branch(light_update,
-                               anchor_offset[i], branch_length[i], 
branch_nodes[i]), -1,
-                               "building of ESP BRANCH failed\n");
-       }
-
-       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-       {
-                HIP_IFEL(hip_build_param_esp_prot_secret(light_update, 
secret_length[i], secret[i]),
-                                -1, "building of ESP SECRET failed\n");
-       }
-
-       for (i = 0; i < esp_prot_num_parallel_hchains; i++)
-       {
-               // only send root if the update hchain has got a link_tree
-               if (entry->esp_root_length > 0)
-               {
-                       HIP_IFEL(hip_build_param_esp_prot_root(light_update, 
entry->esp_root_length,
-                                       entry->esp_root[i]), -1, "building of 
ESP ROOT failed\n");
-               }
-       }
-
-       /******************** add HMAC **********************/
-       HIP_IFEL(hip_build_param_hmac_contents(light_update, 
&entry->hip_hmac_out), -1,
-                       "building of HMAC failed\n");
-
-       /* send the packet with retransmission enabled */
-       entry->light_update_retrans = 1;
-
-       HIP_IFEL(entry->hadb_xmit_func->
-                       hip_send_pkt(&entry->our_addr, &entry->peer_addr,
-                       (entry->nat_mode ? hip_get_local_nat_udp_port() : 0),
-                       entry->peer_udp_port, light_update, entry, 
entry->light_update_retrans),
-                       -1, "failed to send light anchor update\n");
-
-  out_err:
-       if (err)
-               entry->light_update_retrans = 1;
-
-       if (light_update)
-               free(light_update);
-
-       return err;
+    hip_common_t *light_update = NULL;
+    int hash_length            = 0;
+    uint16_t mask              = 0;
+    int err                    = 0, i;
+
+    HIP_IFEL(!(light_update = hip_msg_alloc()), -ENOMEM,
+             "failed to allocate memory\n");
+
+    entry->hadb_misc_func->hip_build_network_hdr(light_update, HIP_LUPDATE,
+                                                 mask, &entry->hit_our, 
&entry->hit_peer);
+
+    /********************* add SEQ *********************/
+
+    entry->light_update_id_out++;
+    HIP_DEBUG("outgoing light UPDATE ID=%u\n", entry->light_update_id_out);
+
+    HIP_IFEL(hip_build_param_seq(light_update, entry->light_update_id_out), -1,
+             "building of SEQ param failed\n");
+
+    /********** add ESP-PROT anchor, branch, secret, root **********/
+
+    hash_length = anchor_db_get_anchor_length(entry->esp_prot_transform);
+
+    for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+        HIP_IFEL(hip_build_param_esp_prot_anchor(light_update,
+                                                 entry->esp_prot_transform, 
&entry->esp_local_anchors[i][0],
+                                                 
&entry->esp_local_update_anchors[i][0], hash_length, entry->hash_item_length),
+                 -1, "building of ESP protection ANCHOR failed\n");
+    }
+
+    for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+        HIP_IFEL(hip_build_param_esp_prot_branch(light_update,
+                                                 anchor_offset[i], 
branch_length[i], branch_nodes[i]), -1,
+                 "building of ESP BRANCH failed\n");
+    }
+
+    for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+        HIP_IFEL(hip_build_param_esp_prot_secret(light_update, 
secret_length[i], secret[i]),
+                 -1, "building of ESP SECRET failed\n");
+    }
+
+    for (i = 0; i < esp_prot_num_parallel_hchains; i++) {
+        // only send root if the update hchain has got a link_tree
+        if (entry->esp_root_length > 0) {
+            HIP_IFEL(hip_build_param_esp_prot_root(light_update,
+                                                   entry->esp_root_length,
+                                                   entry->esp_root[i]),
+                     -1,
+                     "building of ESP ROOT failed\n");
+        }
+    }
+
+    /******************** add HMAC **********************/
+    HIP_IFEL(hip_build_param_hmac_contents(light_update, 
&entry->hip_hmac_out), -1,
+             "building of HMAC failed\n");
+
+    /* send the packet with retransmission enabled */
+    entry->light_update_retrans = 1;
+
+    HIP_IFEL(entry->hadb_xmit_func->hip_send_pkt(&entry->our_addr,
+                                                 &entry->peer_addr,
+                                                 (entry->nat_mode ? 
hip_get_local_nat_udp_port() : 0),
+                                                 entry->peer_udp_port,
+                                                 light_update,
+                                                 entry,
+                                                 entry->light_update_retrans),
+             -1,
+             "failed to send light anchor update\n");
+
+out_err:
+    if (err) {
+        entry->light_update_retrans = 1;
+    }
+
+    if (light_update) {
+        free(light_update);
+    }
+
+    return err;
 }
 
 /**
@@ -161,80 +170,82 @@
  * @param entry                host association for this connection
  * @return 0 in case of succcess, -1 otherwise
  **/
-int esp_prot_receive_light_update(hip_common_t *msg, const in6_addr_t 
*src_addr,
-              const in6_addr_t *dst_addr, hip_ha_t *entry)
+int esp_prot_receive_light_update(hip_common_t *msg,
+                                  const in6_addr_t *src_addr,
+                                  const in6_addr_t *dst_addr,
+                                  hip_ha_t *entry)
 {
-       struct hip_seq *seq = NULL;
-       struct hip_ack *ack = NULL;
-       uint32_t seq_no = 0;
-       uint32_t ack_no = 0;
-       uint32_t spi = 0;
-       int err = 0;
-
-       HIP_IFEL(hip_verify_packet_hmac(msg, &entry->hip_hmac_in), -1,
-                       "HMAC validation on UPDATE failed.\n");
-
-       ack = hip_get_param(msg, HIP_PARAM_ACK);
-       seq = hip_get_param(msg, HIP_PARAM_SEQ);
-
-       if (seq != NULL)
-       {
-               /********** SEQ ***********/
-               seq_no = ntohl(seq->update_id);
-
-               HIP_DEBUG("SEQ parameter found with update ID: %u\n", seq_no);
-               HIP_DEBUG("previous incoming update id=%u\n", 
entry->light_update_id_in);
-
-               if (seq_no < entry->light_update_id_in) {
-                       HIP_DEBUG("old SEQ, dropping...\n");
-
-                       err = -EINVAL;
-                       goto out_err;
-
-               } else if (seq_no == entry->light_update_id_in) {
-
-                       HIP_DEBUG("retransmitted UPDATE packet (?), 
continuing\n");
-
-               } else
-               {
-                       HIP_DEBUG("new SEQ, storing...\n");
-                       entry->light_update_id_in = seq_no;
-               }
-
-               /********** ANCHOR ***********/
-               HIP_IFEL(esp_prot_update_handle_anchor(msg, entry, src_addr, 
dst_addr, &spi),
-                               -1, "failed to handle anchors\n");
-
-               // send ACK
-               esp_prot_send_light_ack(entry, dst_addr, src_addr, spi);
-
-       } else if (ack != NULL)
-       {
-               /********** ACK ***********/
-               ack_no = ntohl(ack->peer_update_id);
-
-               HIP_DEBUG("ACK found with peer update ID: %u\n", ack_no);
-
-               HIP_IFEL(ack_no != entry->light_update_id_out, -1,
-                               "received non-matching ACK\n");
-
-               // stop retransmission
-               entry->light_update_retrans = 0;
-
-               // notify sadb about next anchor
-               HIP_IFEL(entry->hadb_ipsec_func->hip_add_sa(dst_addr, src_addr,
-                               &entry->hit_our, &entry->hit_peer, 
entry->spi_outbound_new,
-                               entry->esp_transform, &entry->esp_out, 
&entry->auth_out, 0,
-                               HIP_SPI_DIRECTION_OUT, 1, entry), -1,
-                               "failed to notify sadb about next anchor\n");
-
-       } else
-       {
-               HIP_ERROR("light update message received, but no SEQ or ACK 
found\n");
-
-               err = -1;
-       }
-
-  out_err:
-       return err;
+    struct hip_seq *seq = NULL;
+    struct hip_ack *ack = NULL;
+    uint32_t seq_no     = 0;
+    uint32_t ack_no     = 0;
+    uint32_t spi        = 0;
+    int err             = 0;
+
+    HIP_IFEL(hip_verify_packet_hmac(msg, &entry->hip_hmac_in), -1,
+             "HMAC validation on UPDATE failed.\n");
+
+    ack = hip_get_param(msg, HIP_PARAM_ACK);
+    seq = hip_get_param(msg, HIP_PARAM_SEQ);
+
+    if (seq != NULL) {
+        /********** SEQ ***********/
+        seq_no = ntohl(seq->update_id);
+
+        HIP_DEBUG("SEQ parameter found with update ID: %u\n", seq_no);
+        HIP_DEBUG("previous incoming update id=%u\n", 
entry->light_update_id_in);
+
+        if (seq_no < entry->light_update_id_in) {
+            HIP_DEBUG("old SEQ, dropping...\n");
+
+            err = -EINVAL;
+            goto out_err;
+        } else if (seq_no == entry->light_update_id_in) {
+            HIP_DEBUG("retransmitted UPDATE packet (?), continuing\n");
+        } else {
+            HIP_DEBUG("new SEQ, storing...\n");
+            entry->light_update_id_in = seq_no;
+        }
+
+        /********** ANCHOR ***********/
+        HIP_IFEL(esp_prot_update_handle_anchor(msg, entry, src_addr, dst_addr, 
&spi),
+                 -1, "failed to handle anchors\n");
+
+        // send ACK
+        esp_prot_send_light_ack(entry, dst_addr, src_addr, spi);
+    } else if (ack != NULL) {
+        /********** ACK ***********/
+        ack_no = ntohl(ack->peer_update_id);
+
+        HIP_DEBUG("ACK found with peer update ID: %u\n", ack_no);
+
+        HIP_IFEL(ack_no != entry->light_update_id_out, -1,
+                 "received non-matching ACK\n");
+
+        // stop retransmission
+        entry->light_update_retrans = 0;
+
+        // notify sadb about next anchor
+        HIP_IFEL(entry->hadb_ipsec_func->hip_add_sa(dst_addr,
+                                                    src_addr,
+                                                    &entry->hit_our,
+                                                    &entry->hit_peer,
+                                                    entry->spi_outbound_new,
+                                                    entry->esp_transform,
+                                                    &entry->esp_out,
+                                                    &entry->auth_out,
+                                                    0,
+                                                    HIP_SPI_DIRECTION_OUT,
+                                                    1,
+                                                    entry),
+                 -1,
+                 "failed to notify sadb about next anchor\n");
+    } else {
+        HIP_ERROR("light update message received, but no SEQ or ACK found\n");
+
+        err = -1;
+    }
+
+out_err:
+    return err;
 }

=== modified file 'hipd/esp_prot_light_update.h'
--- hipd/esp_prot_light_update.h        2010-01-19 09:28:42 +0000
+++ hipd/esp_prot_light_update.h        2010-02-10 22:32:46 +0000
@@ -17,10 +17,15 @@
 
 #include "lib/core/protodefs.h"
 
-int esp_prot_send_light_update(hip_ha_t *entry, const int anchor_offset[],
-               unsigned char *secret[MAX_NUM_PARALLEL_HCHAINS], const int 
secret_length[],
-               unsigned char *branch_nodes[MAX_NUM_PARALLEL_HCHAINS], const 
int branch_length[]);
-int esp_prot_receive_light_update(hip_common_t *msg, const in6_addr_t 
*src_addr,
-              const in6_addr_t *dst_addr, hip_ha_t *entry);
+int esp_prot_send_light_update(hip_ha_t *entry,
+                               const int anchor_offset[],
+                               unsigned char *secret[MAX_NUM_PARALLEL_HCHAINS],
+                               const int secret_length[],
+                               unsigned char 
*branch_nodes[MAX_NUM_PARALLEL_HCHAINS],
+                               const int branch_length[]);
+int esp_prot_receive_light_update(hip_common_t *msg,
+                                  const in6_addr_t *src_addr,
+                                  const in6_addr_t *dst_addr,
+                                  hip_ha_t *entry);
 
 #endif /* ESP_PROT_LIGHT_UPDATE_H_ */

=== modified file 'hipd/hadb.c'
--- hipd/hadb.c 2010-02-08 15:18:06 +0000
+++ hipd/hadb.c 2010-02-10 22:32:46 +0000
@@ -15,15 +15,15 @@
 struct in_addr peer_lsi_index;
 
 struct hip_peer_map_info {
-       hip_hit_t peer_hit;
-        struct in6_addr peer_addr;
-       hip_lsi_t peer_lsi;
-       struct in6_addr our_addr;
-       uint8_t peer_hostname[HIP_HOST_ID_HOSTNAME_LEN_MAX];
+    hip_hit_t       peer_hit;
+    struct in6_addr peer_addr;
+    hip_lsi_t       peer_lsi;
+    struct in6_addr our_addr;
+    uint8_t         peer_hostname[HIP_HOST_ID_HOSTNAME_LEN_MAX];
 };
 
 /* default set of miscellaneous function pointers. This has to be in the global
-   scope. */
+ * scope. */
 
 /** A transmission function set for sending raw HIP packets. */
 hip_xmit_func_set_t default_xmit_func_set;
@@ -45,27 +45,26 @@
  *
  * @param rec a pointer to a host assosiation.
  * @return    the calculated hash or zero if ha, hit_our or hit_peer is NULL.
-*/
+ */
 static unsigned long hip_ha_hash(const hip_ha_t *ha)
 {
-       hip_hit_t hitpair[2];
-       uint8_t hash[HIP_AH_SHA_LEN];
-
-       if(ha == NULL || &(ha->hit_our) == NULL || &(ha->hit_peer) == NULL)
-       {
-               return 0;
-       }
-
-       /* The HIT fields of an host association struct cannot be assumed to be
-          alligned consecutively. Therefore, we must copy them to a temporary
-          array. */
-       memcpy(&hitpair[0], &(ha->hit_our), sizeof(ha->hit_our));
-       memcpy(&hitpair[1], &(ha->hit_peer), sizeof(ha->hit_peer));
-
-       hip_build_digest(HIP_DIGEST_SHA1, (void *)hitpair, sizeof(hitpair),
-                        hash);
-
-       return *((unsigned long *)(void*)hash);
+    hip_hit_t hitpair[2];
+    uint8_t hash[HIP_AH_SHA_LEN];
+
+    if (ha == NULL || &(ha->hit_our) == NULL || &(ha->hit_peer) == NULL) {
+        return 0;
+    }
+
+    /* The HIT fields of an host association struct cannot be assumed to be
+     * alligned consecutively. Therefore, we must copy them to a temporary
+     * array. */
+    memcpy(&hitpair[0], &(ha->hit_our), sizeof(ha->hit_our));
+    memcpy(&hitpair[1], &(ha->hit_peer), sizeof(ha->hit_peer));
+
+    hip_build_digest(HIP_DIGEST_SHA1, (void *) hitpair, sizeof(hitpair),
+                     hash);
+
+    return *((unsigned long *) (void *) hash);
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
@@ -73,13 +72,12 @@
 
 static int hip_ha_cmp(const hip_ha_t *ha1, const hip_ha_t *ha2)
 {
-     if(ha1 == NULL || &(ha1->hit_our) == NULL || &(ha1->hit_peer) == NULL ||
-        ha2 == NULL || &(ha2->hit_our) == NULL || &(ha2->hit_peer) == NULL)
-     {
-          return 1;
-     }
+    if (ha1 == NULL || &(ha1->hit_our) == NULL || &(ha1->hit_peer) == NULL ||
+        ha2 == NULL || &(ha2->hit_our) == NULL || &(ha2->hit_peer) == NULL) {
+        return 1;
+    }
 
-     return (hip_ha_LHASH_HASH(ha1) != hip_ha_LHASH_HASH(ha2));
+    return hip_ha_LHASH_HASH(ha1) != hip_ha_LHASH_HASH(ha2);
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
@@ -87,33 +85,33 @@
 
 static unsigned long hip_hash_peer_addr(const void *ptr)
 {
-       struct in6_addr *addr = &((struct hip_peer_addr_list_item 
*)ptr)->address;
-        uint8_t hash[HIP_AH_SHA_LEN];
-
-       hip_build_digest(HIP_DIGEST_SHA1, addr, sizeof(*addr), hash);
-
-       return *((unsigned long *)(void*) hash);
+    struct in6_addr *addr = &((struct hip_peer_addr_list_item *) ptr)->address;
+    uint8_t hash[HIP_AH_SHA_LEN];
+
+    hip_build_digest(HIP_DIGEST_SHA1, addr, sizeof(*addr), hash);
+
+    return *((unsigned long *) (void *) hash);
 }
 
 static int hip_match_peer_addr(const void *ptr1, const void *ptr2)
 {
-       return (hip_hash_peer_addr(ptr1) != hip_hash_peer_addr(ptr2));
+    return hip_hash_peer_addr(ptr1) != hip_hash_peer_addr(ptr2);
 }
 
 /*
-  Support for multiple inbound IPsec SAs:
-
-  We need a separate hashtable containing elements HIT and SPI, which
-  tells which HIT has the inbound SPI. When an ESP packet is received,
-  we first get the SPI from it and perform a lookup on the HIT-SPI
-  hashtable to get the mapping. Then we perform another lookup from
-  the HIT hashtable using the HIT we got from the previous
-  lookup. This way we get the HA belonging to the connection.
-
-  hs = HIT-SPI (struct hip_hit_spi)
-
-  (functions hip_ .. _hs)
-*/
+ * Support for multiple inbound IPsec SAs:
+ *
+ * We need a separate hashtable containing elements HIT and SPI, which
+ * tells which HIT has the inbound SPI. When an ESP packet is received,
+ * we first get the SPI from it and perform a lookup on the HIT-SPI
+ * hashtable to get the mapping. Then we perform another lookup from
+ * the HIT hashtable using the HIT we got from the previous
+ * lookup. This way we get the HA belonging to the connection.
+ *
+ * hs = HIT-SPI (struct hip_hit_spi)
+ *
+ * (functions hip_ .. _hs)
+ */
 
 
 /*
@@ -130,17 +128,18 @@
 
 void hip_hadb_set_lsi_pair(hip_ha_t *entry)
 {
-        hip_lsi_t aux;
-       //Assign value to lsi_our searching in hidb by the correspondent hit
-       _HIP_DEBUG("hip_hadb_set_lsi_pair\n");
-       if (entry){
-               hip_hidb_get_lsi_by_hit(&entry->hit_our, &entry->lsi_our);
-               //Assign lsi_peer
-               if (hip_map_hit_to_lsi_from_hosts_files(&entry->hit_peer,&aux))
-                       hip_generate_peer_lsi(&aux);
-               memcpy(&entry->lsi_peer, &aux, sizeof(hip_lsi_t));
-               _HIP_DEBUG_LSI("entry->lsi_peer is ", &entry->lsi_peer);
-       }
+    hip_lsi_t aux;
+    //Assign value to lsi_our searching in hidb by the correspondent hit
+    _HIP_DEBUG("hip_hadb_set_lsi_pair\n");
+    if (entry) {
+        hip_hidb_get_lsi_by_hit(&entry->hit_our, &entry->lsi_our);
+        //Assign lsi_peer
+        if (hip_map_hit_to_lsi_from_hosts_files(&entry->hit_peer, &aux)) {
+            hip_generate_peer_lsi(&aux);
+        }
+        memcpy(&entry->lsi_peer, &aux, sizeof(hip_lsi_t));
+        _HIP_DEBUG_LSI("entry->lsi_peer is ", &entry->lsi_peer);
+    }
 }
 
 /**
@@ -149,24 +148,24 @@
  */
 hip_ha_t *hip_hadb_find_byhits(const hip_hit_t *hit, const hip_hit_t *hit2)
 {
-  //int n = 0;
-       hip_ha_t ha, *ret;
-       memcpy(&ha.hit_our, hit, sizeof(hip_hit_t));
-       memcpy(&ha.hit_peer, hit2, sizeof(hip_hit_t));
-       HIP_DEBUG_HIT("HIT1", hit);
-       HIP_DEBUG_HIT("HIT2", hit2);
-
-       //HIP_DEBUG("----------Checking database-----------------\n");
-       //hip_for_each_ha(hip_print_info_hadb, &n);
-       //HIP_DEBUG("----------End Checking database-----------------\n");
-       ret = hip_ht_find(hadb_hit, &ha);
-       if (!ret) {
-               memcpy(&ha.hit_peer, hit, sizeof(hip_hit_t));
-               memcpy(&ha.hit_our, hit2, sizeof(hip_hit_t));
-               ret = hip_ht_find(hadb_hit, &ha);
-       }
-
-       return ret;
+    //int n = 0;
+    hip_ha_t ha, *ret;
+    memcpy(&ha.hit_our, hit, sizeof(hip_hit_t));
+    memcpy(&ha.hit_peer, hit2, sizeof(hip_hit_t));
+    HIP_DEBUG_HIT("HIT1", hit);
+    HIP_DEBUG_HIT("HIT2", hit2);
+
+    //HIP_DEBUG("----------Checking database-----------------\n");
+    //hip_for_each_ha(hip_print_info_hadb, &n);
+    //HIP_DEBUG("----------End Checking database-----------------\n");
+    ret = hip_ht_find(hadb_hit, &ha);
+    if (!ret) {
+        memcpy(&ha.hit_peer, hit, sizeof(hip_hit_t));
+        memcpy(&ha.hit_our, hit2, sizeof(hip_hit_t));
+        ret = hip_ht_find(hadb_hit, &ha);
+    }
+
+    return ret;
 }
 
 /**
@@ -188,36 +187,37 @@
  */
 hip_ha_t *hip_hadb_try_to_find_by_peer_hit(const hip_hit_t *hit)
 {
-       hip_list_t *item, *tmp;
-       struct hip_host_id_entry *e;
-       hip_ha_t *entry = NULL;
-       hip_hit_t our_hit;
-       int i;
-
-       memset(&our_hit, 0, sizeof(our_hit));
-
-       /* Let's try with the default HIT first */
-       hip_get_default_hit(&our_hit);
-
-       if ( (entry = hip_hadb_find_byhits(hit, &our_hit)) ) {
-               _HIP_DEBUG_HIT("Returning default HIT", our_hit);
-               return entry;
-       }
-
-       /* and then with rest (actually default HIT is here redundantly) */
-       list_for_each_safe(item, tmp, hip_local_hostid_db, i)
-       {
-               e = (struct hip_host_id_entry *)list_entry(item);
-               ipv6_addr_copy(&our_hit, &e->lhi.hit);
-               _HIP_DEBUG_HIT("try_to_find_by_peer_hit:", &our_hit);
-               _HIP_DEBUG_HIT("hit:", hit);
-               entry = hip_hadb_find_byhits(hit, &our_hit);
-               if (!entry)
-                       continue;
-               else
-                       return entry;
-       }
-       return NULL;
+    hip_list_t *item, *tmp;
+    struct hip_host_id_entry *e;
+    hip_ha_t *entry = NULL;
+    hip_hit_t our_hit;
+    int i;
+
+    memset(&our_hit, 0, sizeof(our_hit));
+
+    /* Let's try with the default HIT first */
+    hip_get_default_hit(&our_hit);
+
+    if ((entry = hip_hadb_find_byhits(hit, &our_hit))) {
+        _HIP_DEBUG_HIT("Returning default HIT", our_hit);
+        return entry;
+    }
+
+    /* and then with rest (actually default HIT is here redundantly) */
+    list_for_each_safe(item, tmp, hip_local_hostid_db, i)
+    {
+        e     = (struct hip_host_id_entry *) list_entry(item);
+        ipv6_addr_copy(&our_hit, &e->lhi.hit);
+        _HIP_DEBUG_HIT("try_to_find_by_peer_hit:", &our_hit);
+        _HIP_DEBUG_HIT("hit:", hit);
+        entry = hip_hadb_find_byhits(hit, &our_hit);
+        if (!entry) {
+            continue;
+        } else {
+            return entry;
+        }
+    }
+    return NULL;
 }
 
 /**
@@ -235,99 +235,106 @@
  */
 int hip_hadb_insert_state(hip_ha_t *ha)
 {
-       hip_hastate_t st = 0;
-       hip_ha_t *tmp = NULL;
-
-       HIP_DEBUG("hip_hadb_insert_state() invoked.\n");
-
-       /* assume already locked ha */
-
-       HIP_ASSERT(!(ipv6_addr_any(&ha->hit_peer)));
-
-       HIP_DEBUG("hip_hadb_insert_state() invoked. Inserting a new state to "\
-                 "the HIP association hash table.\n");
-
-       if(ha == NULL) {
-               HIP_DIE("Trying to insert a NULL HIP association to the HIP "\
-                       "association hash table.\n");
-       } else if (ipv6_addr_any(&ha->hit_peer)) {
-               HIP_DIE("Trying to insert a HIP association with zero "\
-                       "(ipv6_addr_any) peer HIT to the HIP association hash "\
-                       "table.\n");
-       }
-
-       st = ha->hastate;
+    hip_hastate_t st = 0;
+    hip_ha_t *tmp    = NULL;
+
+    HIP_DEBUG("hip_hadb_insert_state() invoked.\n");
+
+    /* assume already locked ha */
+
+    HIP_ASSERT(!(ipv6_addr_any(&ha->hit_peer)));
+
+    HIP_DEBUG("hip_hadb_insert_state() invoked. Inserting a new state to " \
+              "the HIP association hash table.\n");
+
+    if (ha == NULL) {
+        HIP_DIE("Trying to insert a NULL HIP association to the HIP " \
+                "association hash table.\n");
+    } else if (ipv6_addr_any(&ha->hit_peer)) {
+        HIP_DIE("Trying to insert a HIP association with zero " \
+                "(ipv6_addr_any) peer HIT to the HIP association hash " \
+                "table.\n");
+    }
+
+    st = ha->hastate;
 
 #ifdef CONFIG_HIP_DEBUG /* Debug block. */
-       {
-               char hito[INET6_ADDRSTRLEN], hitp[INET6_ADDRSTRLEN];
-               inet_ntop(AF_INET6, &ha->hit_our, hito, INET6_ADDRSTRLEN);
-               inet_ntop(AF_INET6, &ha->hit_peer, hitp, INET6_ADDRSTRLEN);
-               HIP_DEBUG("Trying to insert a new state to the HIP "\
-                         "association database. Our HIT: %s,"\
-                         "Peer HIT: %s, HIP association state: %d\n",
-                         hito, hitp, ha->hastate);
-       }
+    {
+        char hito[INET6_ADDRSTRLEN], hitp[INET6_ADDRSTRLEN];
+        inet_ntop(AF_INET6, &ha->hit_our, hito, INET6_ADDRSTRLEN);
+        inet_ntop(AF_INET6, &ha->hit_peer, hitp, INET6_ADDRSTRLEN);
+        HIP_DEBUG("Trying to insert a new state to the HIP " \
+                  "association database. Our HIT: %s," \
+                  "Peer HIT: %s, HIP association state: %d\n",
+                  hito, hitp, ha->hastate);
+    }
 #endif
 
-       /* We're using hastate here as if it was a binary mask. hastate,
-          however, is of signed type and all of the predefined values are not
-          in the power of two. -Lauri 07.08.2008 */
-       if (!(st & HIP_HASTATE_HITOK)) {
-               tmp = hip_ht_find(hadb_hit, ha);
-
-               if (tmp == NULL) {
-                       if ((ha->lsi_peer).s_addr == 0) {
-                               hip_hadb_set_lsi_pair(ha);
-                       }
-                       hip_ht_add(hadb_hit, ha);
-                       st |= HIP_HASTATE_HITOK;
-                       HIP_DEBUG("HIP association was inserted "\
-                                 "successfully.\n");
-               } else {
-                       /* hip_db_put_ha(tmp, hip_hadb_delete_state); */
-                       HIP_DEBUG("HIP association was NOT inserted because "\
-                                 "a HIP association with matching HITs was "\
-                                 "already present in the database.\n");
-               }
-       } else {
-               HIP_DEBUG("HIP association was NOT inserted because the "\
-                         "HIP association state is not OK.\n");
-       }
-
-
-       ha->hastate = st;
-       return st;
+    /* We're using hastate here as if it was a binary mask. hastate,
+     * however, is of signed type and all of the predefined values are not
+     * in the power of two. -Lauri 07.08.2008 */
+    if (!(st & HIP_HASTATE_HITOK)) {
+        tmp = hip_ht_find(hadb_hit, ha);
+
+        if (tmp == NULL) {
+            if ((ha->lsi_peer).s_addr == 0) {
+                hip_hadb_set_lsi_pair(ha);
+            }
+            hip_ht_add(hadb_hit, ha);
+            st |= HIP_HASTATE_HITOK;
+            HIP_DEBUG("HIP association was inserted " \
+                      "successfully.\n");
+        } else {
+            /* hip_db_put_ha(tmp, hip_hadb_delete_state); */
+            HIP_DEBUG("HIP association was NOT inserted because " \
+                      "a HIP association with matching HITs was " \
+                      "already present in the database.\n");
+        }
+    } else {
+        HIP_DEBUG("HIP association was NOT inserted because the " \
+                  "HIP association state is not OK.\n");
+    }
+
+
+    ha->hastate = st;
+    return st;
 }
 
 static void hip_print_debug_info(const struct in6_addr *local_addr,
-                         const struct in6_addr *peer_addr,
-                         const hip_hit_t  *local_hit,
-                         const hip_hit_t  *peer_hit,
-                         const hip_lsi_t  *peer_lsi,
-                         const char *peer_hostname,
-                         const in_port_t *local_nat_udp_port,
-                         const in_port_t *peer_nat_udp_port)
-{                  
-
-       if(local_addr)
-               HIP_DEBUG_IN6ADDR("Our addr", local_addr);
-       if(peer_addr)
-               HIP_DEBUG_IN6ADDR("Peer addr", peer_addr);
-       if(local_hit)
-               HIP_DEBUG_HIT("Our HIT", local_hit);
-       if(peer_hit)
-               HIP_DEBUG_HIT("Peer HIT", peer_hit);
-       if(peer_lsi)
-               HIP_DEBUG_LSI("Peer LSI", peer_lsi);
-       if(peer_hostname)
-               HIP_DEBUG("Peer hostname: %s\n", peer_hostname);
-       
-       if (local_nat_udp_port)
-               HIP_DEBUG("Local NAT traversal UDP port: %d\n", 
*local_nat_udp_port);
-       
-       if (peer_nat_udp_port)
-               HIP_DEBUG("Peer NAT traversal UDP port: %d\n", 
*peer_nat_udp_port);
+                                 const struct in6_addr *peer_addr,
+                                 const hip_hit_t  *local_hit,
+                                 const hip_hit_t  *peer_hit,
+                                 const hip_lsi_t  *peer_lsi,
+                                 const char *peer_hostname,
+                                 const in_port_t *local_nat_udp_port,
+                                 const in_port_t *peer_nat_udp_port)
+{
+    if (local_addr) {
+        HIP_DEBUG_IN6ADDR("Our addr", local_addr);
+    }
+    if (peer_addr) {
+        HIP_DEBUG_IN6ADDR("Peer addr", peer_addr);
+    }
+    if (local_hit) {
+        HIP_DEBUG_HIT("Our HIT", local_hit);
+    }
+    if (peer_hit) {
+        HIP_DEBUG_HIT("Peer HIT", peer_hit);
+    }
+    if (peer_lsi) {
+        HIP_DEBUG_LSI("Peer LSI", peer_lsi);
+    }
+    if (peer_hostname) {
+        HIP_DEBUG("Peer hostname: %s\n", peer_hostname);
+    }
+
+    if (local_nat_udp_port) {
+        HIP_DEBUG("Local NAT traversal UDP port: %d\n", *local_nat_udp_port);
+    }
+
+    if (peer_nat_udp_port) {
+        HIP_DEBUG("Peer NAT traversal UDP port: %d\n", *peer_nat_udp_port);
+    }
 }
 
 /**
@@ -346,137 +353,136 @@
  *         based policy to work alternative b) add SP pair for all local HITs.
  */
 int hip_hadb_add_peer_info_complete(const hip_hit_t *local_hit,
-                                   const hip_hit_t *peer_hit,
-                                   const hip_lsi_t *peer_lsi,
-                                   const struct in6_addr *local_addr,
-                                   const struct in6_addr *peer_addr,
-                                   const char *peer_hostname)
+                                    const hip_hit_t *peer_hit,
+                                    const hip_lsi_t *peer_lsi,
+                                    const struct in6_addr *local_addr,
+                                    const struct in6_addr *peer_addr,
+                                    const char *peer_hostname)
 {
-       int err = 0;
-       hip_ha_t *entry = NULL, *aux = NULL;
-       hip_lsi_t lsi_aux;
-       in_port_t nat_udp_port_local = hip_get_local_nat_udp_port();
-       in_port_t nat_udp_port_peer = hip_get_peer_nat_udp_port();
-
-       HIP_DEBUG_IN6ADDR("Local IP address ", local_addr);
-
-       hip_print_debug_info(local_addr, peer_addr,
-                            local_hit,  peer_hit,
-                            peer_lsi,   peer_hostname,
-                            &nat_udp_port_local,
-                            &nat_udp_port_peer);
-
-       entry = hip_hadb_find_byhits(local_hit, peer_hit);
-
-       if (entry) {
-               // hip_hadb_dump_spis_out_old(entry);
-               HIP_DEBUG_LSI("    Peer lsi   ",&entry->lsi_peer);
+    int err                      = 0;
+    hip_ha_t *entry              = NULL, *aux = NULL;
+    hip_lsi_t lsi_aux;
+    in_port_t nat_udp_port_local = hip_get_local_nat_udp_port();
+    in_port_t nat_udp_port_peer  = hip_get_peer_nat_udp_port();
+
+    HIP_DEBUG_IN6ADDR("Local IP address ", local_addr);
+
+    hip_print_debug_info(local_addr, peer_addr,
+                         local_hit,  peer_hit,
+                         peer_lsi,   peer_hostname,
+                         &nat_udp_port_local,
+                         &nat_udp_port_peer);
+
+    entry = hip_hadb_find_byhits(local_hit, peer_hit);
+
+    if (entry) {
+        // hip_hadb_dump_spis_out_old(entry);
+        HIP_DEBUG_LSI("    Peer lsi   ", &entry->lsi_peer);
 
 #if 0 /* Required for OpenDHT code of Pardeep?  */
-               /* Check if LSIs are different */
-               if (peer_lsi) {
-                       HIP_IFEL(hip_lsi_are_equal(&entry->lsi_peer, peer_lsi) 
||
-                                peer_lsi->s_addr == 0 , 0,
-                                "Ignoring new mapping, old one exists\n");
-               }
+        /* Check if LSIs are different */
+        if (peer_lsi) {
+            HIP_IFEL(hip_lsi_are_equal(&entry->lsi_peer, peer_lsi) ||
+                     peer_lsi->s_addr == 0, 0,
+                     "Ignoring new mapping, old one exists\n");
+        }
 #endif
-       }
-
-       if (!entry){
-               HIP_DEBUG("hip_hadb_create_state\n");
-               entry = hip_hadb_create_state(0);
-               HIP_IFEL(!entry, -1, "Unable to create a new entry");
-               _HIP_DEBUG("created a new sdb entry\n");
-
-               entry->peer_addr_list_to_be_added =
-                       hip_ht_init(hip_hash_peer_addr, hip_match_peer_addr);
-       }
-
-       ipv6_addr_copy(&entry->hit_peer, peer_hit);
-       ipv6_addr_copy(&entry->hit_our, local_hit);
-       ipv6_addr_copy(&entry->our_addr, local_addr);
-       HIP_IFEL(hip_hidb_get_lsi_by_hit(local_hit, &entry->lsi_our), -1,
-                "Unable to find local hit");
-
-       /* Copying peer_lsi */
-       if (peer_lsi != NULL && peer_lsi->s_addr != 0) {
-               ipv4_addr_copy(&entry->lsi_peer, peer_lsi);
-       } else {
-               /* Check if exists an entry in the hadb with the
-                  peer_hit given */
-               aux = hip_hadb_try_to_find_by_peer_hit(peer_hit);
-               if (aux && &(aux->lsi_peer).s_addr != 0){
-                       /* Exists: Assign its lsi to the new entry created */
-                       ipv4_addr_copy(&entry->lsi_peer, &aux->lsi_peer);
-               } else if (!hip_map_hit_to_lsi_from_hosts_files(peer_hit, 
&lsi_aux)) {
-                       ipv4_addr_copy(&entry->lsi_peer, &lsi_aux);
-               } else if (hip_hidb_hit_is_our(peer_hit)) {
-                       /* Loopback (see bug id 893) */
-                       entry->lsi_peer = entry->lsi_our;
-               } else {
-                       /* Not exists: Call to the automatic generation */
-                       hip_generate_peer_lsi(&lsi_aux);
-                       ipv4_addr_copy(&entry->lsi_peer, &lsi_aux);
-               }
-       }
-
-       /* If global NAT status is on, that is if the current host is behind
-          NAT, the NAT status of the host association is set on and the send
-          function set is set to "nat_xmit_func_set". */
-       if(hip_nat_status && IN6_IS_ADDR_V4MAPPED(peer_addr) &&
-          !ipv6_addr_is_teredo(peer_addr)) {
-               entry->nat_mode = hip_nat_status;
-               entry->peer_udp_port = hip_get_peer_nat_udp_port();
-               entry->local_udp_port = hip_get_local_nat_udp_port();
-               entry->hadb_xmit_func = &nat_xmit_func_set;
-       }
-       else {
-               /* NAT mode is not reset here due to "shotgun" support.
-                  Hipd may get multiple locator mappings of which some can be
-                  IPv4 and others IPv6. If NAT mode is on and the last
-                  added address is IPv6, we don't want to reset NAT mode.
-                  Note that send_udp() function can shortcut to send_raw()
-                  when it gets an IPv6 address. */
-               entry->hadb_xmit_func = &default_xmit_func_set;
-       }
+    }
+
+    if (!entry) {
+        HIP_DEBUG("hip_hadb_create_state\n");
+        entry                             = hip_hadb_create_state(0);
+        HIP_IFEL(!entry, -1, "Unable to create a new entry");
+        _HIP_DEBUG("created a new sdb entry\n");
+
+        entry->peer_addr_list_to_be_added =
+            hip_ht_init(hip_hash_peer_addr, hip_match_peer_addr);
+    }
+
+    ipv6_addr_copy(&entry->hit_peer, peer_hit);
+    ipv6_addr_copy(&entry->hit_our, local_hit);
+    ipv6_addr_copy(&entry->our_addr, local_addr);
+    HIP_IFEL(hip_hidb_get_lsi_by_hit(local_hit, &entry->lsi_our), -1,
+             "Unable to find local hit");
+
+    /* Copying peer_lsi */
+    if (peer_lsi != NULL && peer_lsi->s_addr != 0) {
+        ipv4_addr_copy(&entry->lsi_peer, peer_lsi);
+    } else {
+        /* Check if exists an entry in the hadb with the
+         * peer_hit given */
+        aux = hip_hadb_try_to_find_by_peer_hit(peer_hit);
+        if (aux && &(aux->lsi_peer).s_addr != 0) {
+            /* Exists: Assign its lsi to the new entry created */
+            ipv4_addr_copy(&entry->lsi_peer, &aux->lsi_peer);
+        } else if (!hip_map_hit_to_lsi_from_hosts_files(peer_hit, &lsi_aux)) {
+            ipv4_addr_copy(&entry->lsi_peer, &lsi_aux);
+        } else if (hip_hidb_hit_is_our(peer_hit)) {
+            /* Loopback (see bug id 893) */
+            entry->lsi_peer = entry->lsi_our;
+        } else {
+            /* Not exists: Call to the automatic generation */
+            hip_generate_peer_lsi(&lsi_aux);
+            ipv4_addr_copy(&entry->lsi_peer, &lsi_aux);
+        }
+    }
+
+    /* If global NAT status is on, that is if the current host is behind
+     * NAT, the NAT status of the host association is set on and the send
+     * function set is set to "nat_xmit_func_set". */
+    if (hip_nat_status && IN6_IS_ADDR_V4MAPPED(peer_addr) &&
+        !ipv6_addr_is_teredo(peer_addr)) {
+        entry->nat_mode       = hip_nat_status;
+        entry->peer_udp_port  = hip_get_peer_nat_udp_port();
+        entry->local_udp_port = hip_get_local_nat_udp_port();
+        entry->hadb_xmit_func = &nat_xmit_func_set;
+    } else {
+        /* NAT mode is not reset here due to "shotgun" support.
+         * Hipd may get multiple locator mappings of which some can be
+         * IPv4 and others IPv6. If NAT mode is on and the last
+         * added address is IPv6, we don't want to reset NAT mode.
+         * Note that send_udp() function can shortcut to send_raw()
+         * when it gets an IPv6 address. */
+        entry->hadb_xmit_func = &default_xmit_func_set;
+    }
 
 #ifdef CONFIG_HIP_BLIND
-       if(hip_blind_status)
-               entry->blind = 1;
+    if (hip_blind_status) {
+        entry->blind = 1;
+    }
 #endif
-       if (hip_hidb_hit_is_our(peer_hit)) {
-               HIP_DEBUG("Peer HIT is ours (loopback)\n");
-               entry->is_loopback = 1;
-       }
+    if (hip_hidb_hit_is_our(peer_hit)) {
+        HIP_DEBUG("Peer HIT is ours (loopback)\n");
+        entry->is_loopback = 1;
+    }
 
-       entry->hip_is_opptcp_on = hip_get_opportunistic_tcp_status();
+    entry->hip_is_opptcp_on = hip_get_opportunistic_tcp_status();
 #ifdef CONFIG_HIP_I3
-       entry->hip_is_hi3_on =    hip_get_hi3_status();
+    entry->hip_is_hi3_on    =    hip_get_hi3_status();
 #endif
 #ifdef CONFIG_HIP_HIPPROXY
-       entry->hipproxy = hip_get_hip_proxy_status();
+    entry->hipproxy         = hip_get_hip_proxy_status();
 #endif
 
-       /* @todo: lock ha when we have threads */
-
-       HIP_DEBUG_LSI("entry->lsi_peer \n", &entry->lsi_peer);
-       hip_hadb_insert_state(entry);
-
-       /* Add initial HIT-IP mapping. */
-       HIP_IFEL(hip_hadb_add_peer_addr(entry, peer_addr, 0, 0, 
PEER_ADDR_STATE_ACTIVE, hip_get_peer_nat_udp_port()),
-                -2, "error while adding a new peer address\n");
-
-       /* @todo: unlock ha when we have threads */
-
-       HIP_IFEL(default_ipsec_func_set.hip_setup_hit_sp_pair(peer_hit, 
local_hit,
-                                                              local_addr, 
peer_addr, 0, 1, 0),
-                -1, "Error in setting the SPs\n");
+    /* @todo: lock ha when we have threads */
+
+    HIP_DEBUG_LSI("entry->lsi_peer \n", &entry->lsi_peer);
+    hip_hadb_insert_state(entry);
+
+    /* Add initial HIT-IP mapping. */
+    HIP_IFEL(hip_hadb_add_peer_addr(entry, peer_addr, 0, 0, 
PEER_ADDR_STATE_ACTIVE, hip_get_peer_nat_udp_port()),
+             -2, "error while adding a new peer address\n");
+
+    /* @todo: unlock ha when we have threads */
+
+    HIP_IFEL(default_ipsec_func_set.hip_setup_hit_sp_pair(peer_hit, local_hit,
+                                                          local_addr, 
peer_addr, 0, 1, 0),
+             -1, "Error in setting the SPs\n");
 
 out_err:
-       return err;
+    return err;
 }
 
-
 /**
  * .
  *
@@ -485,110 +491,121 @@
  * @return               ...
  */
 static int hip_hadb_add_peer_info_wrapper(struct hip_host_id_entry *entry,
-                                         void *peer_map_void)
+                                          void *peer_map_void)
 {
-       struct hip_peer_map_info *peer_map = peer_map_void;
-       int err = 0;
-
-       HIP_DEBUG("hip_hadb_add_peer_info_wrapper() invoked.\n");
-       HIP_IFEL(hip_hadb_add_peer_info_complete(&entry->lhi.hit,
-                                                &peer_map->peer_hit,
-                                                &peer_map->peer_lsi,
-                                                &peer_map->our_addr,
-                                                &peer_map->peer_addr,
-                                                (char*) 
&peer_map->peer_hostname), -1,
-                "Failed to add peer info\n");
-
- out_err:
-       return err;
+    struct hip_peer_map_info *peer_map = peer_map_void;
+    int err                            = 0;
+
+    HIP_DEBUG("hip_hadb_add_peer_info_wrapper() invoked.\n");
+    HIP_IFEL(hip_hadb_add_peer_info_complete(&entry->lhi.hit,
+                                             &peer_map->peer_hit,
+                                             &peer_map->peer_lsi,
+                                             &peer_map->our_addr,
+                                             &peer_map->peer_addr,
+                                             (char *) 
&peer_map->peer_hostname),
+             -1,
+             "Failed to add peer info\n");
+
+out_err:
+    return err;
 }
 
 int hip_hadb_add_peer_info(hip_hit_t *peer_hit, struct in6_addr *peer_addr,
-                          hip_lsi_t *peer_lsi, const char *peer_hostname)
+                           hip_lsi_t *peer_lsi, const char *peer_hostname)
 {
-       int err = 0;
-       struct hip_peer_map_info peer_map;
-
-       HIP_DEBUG("hip_hadb_add_peer_info() invoked.\n");
-
-       in_port_t nat_local_udp_port = hip_get_local_nat_udp_port();
-       in_port_t nat_peer_udp_port = hip_get_peer_nat_udp_port();
-       hip_print_debug_info(NULL, peer_addr, NULL, peer_hit, peer_lsi, 
peer_hostname,
-                       &nat_local_udp_port, &nat_peer_udp_port);
-
-       HIP_IFEL(!ipv6_addr_is_hit(peer_hit), -1, "Not a HIT\n");
-
-       memset(&peer_map, 0, sizeof(peer_map));
-
-       memcpy(&peer_map.peer_hit, peer_hit, sizeof(hip_hit_t));
-       if (peer_addr)
-               memcpy(&peer_map.peer_addr, peer_addr, sizeof(struct in6_addr));
-       memset(peer_map.peer_hostname, '\0', HIP_HOST_ID_HOSTNAME_LEN_MAX);
-
-       if(peer_lsi)
-               memcpy(&peer_map.peer_lsi, peer_lsi, sizeof(struct in6_addr));
-
-       if(peer_hostname)
-               memcpy(peer_map.peer_hostname, peer_hostname,
-                      HIP_HOST_ID_HOSTNAME_LEN_MAX - 1);
-
-       HIP_IFEL(hip_select_source_address(
-                        &peer_map.our_addr, &peer_map.peer_addr),
-                -1, "Cannot find source address\n");
-       
-       HIP_IFEL(hip_for_each_hi(hip_hadb_add_peer_info_wrapper, &peer_map), 0,
-                "for_each_hi err.\n");
-
- out_err:
-       return err;
+    int err = 0;
+    struct hip_peer_map_info peer_map;
+
+    HIP_DEBUG("hip_hadb_add_peer_info() invoked.\n");
+
+    in_port_t nat_local_udp_port = hip_get_local_nat_udp_port();
+    in_port_t nat_peer_udp_port  = hip_get_peer_nat_udp_port();
+    hip_print_debug_info(NULL,
+                         peer_addr,
+                         NULL,
+                         peer_hit,
+                         peer_lsi,
+                         peer_hostname,
+                         &nat_local_udp_port,
+                         &nat_peer_udp_port);
+
+    HIP_IFEL(!ipv6_addr_is_hit(peer_hit), -1, "Not a HIT\n");
+
+    memset(&peer_map, 0, sizeof(peer_map));
+
+    memcpy(&peer_map.peer_hit, peer_hit, sizeof(hip_hit_t));
+    if (peer_addr) {
+        memcpy(&peer_map.peer_addr, peer_addr, sizeof(struct in6_addr));
+    }
+    memset(peer_map.peer_hostname, '\0', HIP_HOST_ID_HOSTNAME_LEN_MAX);
+
+    if (peer_lsi) {
+        memcpy(&peer_map.peer_lsi, peer_lsi, sizeof(struct in6_addr));
+    }
+
+    if (peer_hostname) {
+        memcpy(peer_map.peer_hostname, peer_hostname,
+               HIP_HOST_ID_HOSTNAME_LEN_MAX - 1);
+    }
+
+    HIP_IFEL(hip_select_source_address(
+                 &peer_map.our_addr, &peer_map.peer_addr),
+             -1, "Cannot find source address\n");
+
+    HIP_IFEL(hip_for_each_hi(hip_hadb_add_peer_info_wrapper, &peer_map), 0,
+             "for_each_hi err.\n");
+
+out_err:
+    return err;
 }
 
 int hip_add_peer_map(const struct hip_common *input)
 {
-       struct in6_addr *hit = NULL , *ip = NULL;
-       hip_lsi_t *lsi = NULL;
-       char *peer_hostname = NULL;
-       int err = 0;
-       _HIP_HEXDUMP("packet", input,  hip_get_msg_total_len(input));
-
-       hit = (struct in6_addr *)
-               hip_get_param_contents(input, HIP_PARAM_HIT);
-
-       lsi = (hip_lsi_t *)
-               hip_get_param_contents(input, HIP_PARAM_LSI);
-
-       ip = (struct in6_addr *)
-               hip_get_param_contents(input, HIP_PARAM_IPV6_ADDR);
-
-       peer_hostname = (char *)
-               hip_get_param_contents(input, HIP_PARAM_HOSTNAME);
-
-       if (!ip && (!lsi || !hit)){
-               HIP_ERROR("handle async map: no ip and maybe no lsi or hit\n");
-               err = -ENODATA;
-               goto out_err;
-       }
-
-       if (lsi)
-               HIP_DEBUG_LSI("lsi value is\n",lsi);
-
-       if (peer_hostname)
-               HIP_DEBUG("Peer hostname value is %s\n", peer_hostname);
-
-       err = hip_hadb_add_peer_info(hit, ip, lsi, peer_hostname);
-
-       _HIP_DEBUG_HIT("hip_add_map_info peer's real hit=", hit);
-       _HIP_ASSERT(hit_is_opportunistic_hashed_hit(hit));
-
-       if (err) {
-               HIP_ERROR("Failed to insert peer map (%d)\n", err);
-               goto out_err;
-       }
-
- out_err:
-
-       return err;
-
+    struct in6_addr *hit = NULL, *ip = NULL;
+    hip_lsi_t *lsi       = NULL;
+    char *peer_hostname  = NULL;
+    int err              = 0;
+    _HIP_HEXDUMP("packet", input,  hip_get_msg_total_len(input));
+
+    hit           = (struct in6_addr *)
+                    hip_get_param_contents(input, HIP_PARAM_HIT);
+
+    lsi           = (hip_lsi_t *)
+                    hip_get_param_contents(input, HIP_PARAM_LSI);
+
+    ip            = (struct in6_addr *)
+                    hip_get_param_contents(input, HIP_PARAM_IPV6_ADDR);
+
+    peer_hostname = (char *)
+                    hip_get_param_contents(input, HIP_PARAM_HOSTNAME);
+
+    if (!ip && (!lsi || !hit)) {
+        HIP_ERROR("handle async map: no ip and maybe no lsi or hit\n");
+        err = -ENODATA;
+        goto out_err;
+    }
+
+    if (lsi) {
+        HIP_DEBUG_LSI("lsi value is\n", lsi);
+    }
+
+    if (peer_hostname) {
+        HIP_DEBUG("Peer hostname value is %s\n", peer_hostname);
+    }
+
+    err = hip_hadb_add_peer_info(hit, ip, lsi, peer_hostname);
+
+    _HIP_DEBUG_HIT("hip_add_map_info peer's real hit=", hit);
+    _HIP_ASSERT(hit_is_opportunistic_hashed_hit(hit));
+
+    if (err) {
+        HIP_ERROR("Failed to insert peer map (%d)\n", err);
+        goto out_err;
+    }
+
+out_err:
+
+    return err;
 }
 
 /**
@@ -599,43 +616,45 @@
  * @param new_func_set pointer to the new function set.
  * @return             0 if everything was stored successfully, otherwise < 0.
  */
-static int hip_hadb_set_misc_function_set(hip_ha_t * entry,
-                                         hip_misc_func_set_t * new_func_set){
-        /** @todo add check whether all function pointers are set. */
-        if( entry ){
-                entry->hadb_misc_func = new_func_set;
-                return 0;
-        }
-        return -1;
-}
-
-int hip_hadb_set_xmit_function_set(hip_ha_t * entry,
-                                   hip_xmit_func_set_t * new_func_set){
-        if( entry ){
-                entry->hadb_xmit_func = new_func_set;
-                return 0;
-        }
-        return -1;
-}
-
-static int hip_hadb_set_input_filter_function_set(hip_ha_t * entry,
-                                                 hip_input_filter_func_set_t * 
new_func_set)
-{
-        if( entry ){
-                entry->hadb_input_filter_func = new_func_set;
-                return 0;
-        }
-        return -1;
-}
-
-static int hip_hadb_set_output_filter_function_set(hip_ha_t * entry,
-                                                  hip_output_filter_func_set_t 
* new_func_set)
-{
-        if( entry ){
-                entry->hadb_output_filter_func = new_func_set;
-                return 0;
-        }
-        return -1;
+static int hip_hadb_set_misc_function_set(hip_ha_t *entry,
+                                          hip_misc_func_set_t *new_func_set)
+{
+    /** @todo add check whether all function pointers are set. */
+    if (entry) {
+        entry->hadb_misc_func = new_func_set;
+        return 0;
+    }
+    return -1;
+}
+
+int hip_hadb_set_xmit_function_set(hip_ha_t *entry,
+                                   hip_xmit_func_set_t *new_func_set)
+{
+    if (entry) {
+        entry->hadb_xmit_func = new_func_set;
+        return 0;
+    }
+    return -1;
+}
+
+static int hip_hadb_set_input_filter_function_set(hip_ha_t *entry,
+                                                  hip_input_filter_func_set_t 
*new_func_set)
+{
+    if (entry) {
+        entry->hadb_input_filter_func = new_func_set;
+        return 0;
+    }
+    return -1;
+}
+
+static int hip_hadb_set_output_filter_function_set(hip_ha_t *entry,
+                                                   
hip_output_filter_func_set_t *new_func_set)
+{
+    if (entry) {
+        entry->hadb_output_filter_func = new_func_set;
+        return 0;
+    }
+    return -1;
 }
 
 /**
@@ -645,73 +664,73 @@
  */
 static int hip_hadb_init_entry(hip_ha_t *entry)
 {
-        int err = 0;
-        HIP_IFEL(!entry, -1, "HA is NULL\n");
+    int err = 0;
+    HIP_IFEL(!entry, -1, "HA is NULL\n");
 
 #if 0
-       INIT_LIST_HEAD(&entry->next_hit);
-       INIT_LIST_HEAD(&entry->spis_in_old);
-       INIT_LIST_HEAD(&entry->spis_out_old);
+    INIT_LIST_HEAD(&entry->next_hit);
+    INIT_LIST_HEAD(&entry->spis_in_old);
+    INIT_LIST_HEAD(&entry->spis_out_old);
 #endif
 
 #ifdef CONFIG_HIP_HIPPROXY
-       entry->hipproxy = 0;
+    entry->hipproxy = 0;
 #endif
-       //HIP_LOCK_INIT(entry);
-       //atomic_set(&entry->refcnt,0);
-
-       entry->state = HIP_STATE_UNASSOCIATED;
-       entry->hastate = HIP_HASTATE_INVALID;
-       entry->purge_timeout = HIP_HA_PURGE_TIMEOUT;
-
-       /* Function pointer sets which define HIP behavior in respect to the
-          hadb_entry. */
-       HIP_IFEL(hip_hadb_set_rcv_function_set(entry, &default_rcv_func_set),
-                -1, "Can't set new function pointer set.\n");
-       HIP_IFEL(hip_hadb_set_handle_function_set(entry,
-                                                 &default_handle_func_set),
-                -1, "Can't set new function pointer set.\n");
-       /*HIP_IFEL(hip_hadb_set_update_function_set(entry,
-                                                 &default_update_func_set),
-                -1, "Can't set new function pointer set\n");*/
-
-       HIP_IFEL(hip_hadb_set_misc_function_set(entry, &default_misc_func_set),
-                -1, "Can't set new function pointer set.\n");
-
-       /* Set the xmit function set as function set for sending raw HIP. */
-       HIP_IFEL(hip_hadb_set_xmit_function_set(entry, &default_xmit_func_set),
-                -1, "Can't set new function pointer set.\n");
-
-       HIP_IFEL(hip_hadb_set_input_filter_function_set(
-                        entry, &default_input_filter_func_set), -1,
-                "Can't set new input filter function pointer set.\n");
-       HIP_IFEL(hip_hadb_set_output_filter_function_set(
-                        entry, &default_output_filter_func_set), -1,
-                "Can't set new output filter function pointer set.\n");
-
-       /* added by Tao Wan, on 24, Jan, 2008 */
-       entry->hadb_ipsec_func = &default_ipsec_func_set;
-
-       //initialize the peer hostname
-       memset(entry->peer_hostname, '\0', HIP_HOST_ID_HOSTNAME_LEN_MAX);
-
-        entry->shotgun_status = hip_shotgun_status;
-
-        entry->addresses_to_send_echo_request = hip_linked_list_init();
-
-        entry->peer_addresses_old = hip_linked_list_init();
-
-        // Randomize inbound SPI
-        get_random_bytes(&entry->spi_inbound_current,
-                sizeof(entry->spi_inbound_current));
-
-       HIP_IFE(!(entry->hip_msg_retrans.buf =
-                 malloc(HIP_MAX_NETWORK_PACKET)), -ENOMEM);
-       entry->hip_msg_retrans.count = 0;
-       memset(entry->hip_msg_retrans.buf, 0, HIP_MAX_NETWORK_PACKET);
+    //HIP_LOCK_INIT(entry);
+    //atomic_set(&entry->refcnt,0);
+
+    entry->state         = HIP_STATE_UNASSOCIATED;
+    entry->hastate       = HIP_HASTATE_INVALID;
+    entry->purge_timeout = HIP_HA_PURGE_TIMEOUT;
+
+    /* Function pointer sets which define HIP behavior in respect to the
+     * hadb_entry. */
+    HIP_IFEL(hip_hadb_set_rcv_function_set(entry, &default_rcv_func_set),
+             -1, "Can't set new function pointer set.\n");
+    HIP_IFEL(hip_hadb_set_handle_function_set(entry,
+                                              &default_handle_func_set),
+             -1, "Can't set new function pointer set.\n");
+    /*HIP_IFEL(hip_hadb_set_update_function_set(entry,
+     *                                        &default_update_func_set),
+     *       -1, "Can't set new function pointer set\n");*/
+
+    HIP_IFEL(hip_hadb_set_misc_function_set(entry, &default_misc_func_set),
+             -1, "Can't set new function pointer set.\n");
+
+    /* Set the xmit function set as function set for sending raw HIP. */
+    HIP_IFEL(hip_hadb_set_xmit_function_set(entry, &default_xmit_func_set),
+             -1, "Can't set new function pointer set.\n");
+
+    HIP_IFEL(hip_hadb_set_input_filter_function_set(
+                 entry, &default_input_filter_func_set), -1,
+             "Can't set new input filter function pointer set.\n");
+    HIP_IFEL(hip_hadb_set_output_filter_function_set(
+                 entry, &default_output_filter_func_set), -1,
+             "Can't set new output filter function pointer set.\n");
+
+    /* added by Tao Wan, on 24, Jan, 2008 */
+    entry->hadb_ipsec_func = &default_ipsec_func_set;
+
+    //initialize the peer hostname
+    memset(entry->peer_hostname, '\0', HIP_HOST_ID_HOSTNAME_LEN_MAX);
+
+    entry->shotgun_status                 = hip_shotgun_status;
+
+    entry->addresses_to_send_echo_request = hip_linked_list_init();
+
+    entry->peer_addresses_old             = hip_linked_list_init();
+
+    // Randomize inbound SPI
+    get_random_bytes(&entry->spi_inbound_current,
+                     sizeof(entry->spi_inbound_current));
+
+    HIP_IFE(!(entry->hip_msg_retrans.buf =
+                  malloc(HIP_MAX_NETWORK_PACKET)), -ENOMEM);
+    entry->hip_msg_retrans.count = 0;
+    memset(entry->hip_msg_retrans.buf, 0, HIP_MAX_NETWORK_PACKET);
 
 out_err:
-        return err;
+    return err;
 }
 
 /**
@@ -722,18 +741,18 @@
  */
 hip_ha_t *hip_hadb_create_state(int gfpmask)
 {
-       hip_ha_t *entry = NULL;
-
-       entry = (hip_ha_t *) malloc(sizeof(struct hip_hadb_state));
-       if (entry == NULL) {
-               return NULL;
-       }
-
-       memset(entry, 0, sizeof(struct hip_hadb_state));
-
-        hip_hadb_init_entry(entry);
-
-       return entry;
+    hip_ha_t *entry = NULL;
+
+    entry = (hip_ha_t *) malloc(sizeof(struct hip_hadb_state));
+    if (entry == NULL) {
+        return NULL;
+    }
+
+    memset(entry, 0, sizeof(struct hip_hadb_state));
+
+    hip_hadb_init_entry(entry);
+
+    return entry;
 }
 
 /* END OF PRIMITIVE FUNCTIONS */
@@ -744,50 +763,44 @@
  */
 int hip_hadb_select_spi_addr(hip_ha_t *entry, struct hip_spi_out_item 
*spi_out, struct in6_addr *addr)
 {
-       int err = 0, i;
-       struct hip_peer_addr_list_item *s, *candidate = NULL;
-       struct timeval latest, dt;
-       hip_list_t *item, *tmp;
-
-       list_for_each_safe(item, tmp, spi_out->peer_addr_list, i)
-       {
-               s = (struct hip_peer_addr_list_item *)list_entry(item);
-               if (s->address_state != PEER_ADDR_STATE_ACTIVE)
-               {
-                       _HIP_DEBUG("skipping non-active address %s\n",addrstr);
-                       continue;
-               }
-
-               if (candidate)
-               {
-                       int this_is_later;
-                       this_is_later = hip_timeval_diff(&s->modified_time, 
&latest, &dt);
-                       _HIP_DEBUG("latest=%ld.%06ld\n", latest.tv_sec, 
latest.tv_usec);
-                       _HIP_DEBUG("dt=%ld.%06ld\n", dt.tv_sec, dt.tv_usec);
-                       if (this_is_later)
-                       {
-                               _HIP_DEBUG("is later, change\n");
-                               memcpy(&latest, &s->modified_time, 
sizeof(struct timeval));
-                               candidate = s;
-                       }
-               }
-               else
-               {
-                       candidate = s;
-                       memcpy(&latest, &s->modified_time, sizeof(struct 
timeval));
-               }
-       }
-
-       if (!candidate)
-       {
-               HIP_ERROR("did not find usable peer address\n");
-               HIP_DEBUG("todo: select from other SPIs ?\n");
-               /* todo: select other SPI as the default SPI out */
-               err = -ENOMSG;
-       }
-       else ipv6_addr_copy(addr, &candidate->address);
-
-       return err;
+    int err = 0, i;
+    struct hip_peer_addr_list_item *s, *candidate = NULL;
+    struct timeval latest, dt;
+    hip_list_t *item, *tmp;
+
+    list_for_each_safe(item, tmp, spi_out->peer_addr_list, i)
+    {
+        s = (struct hip_peer_addr_list_item *) list_entry(item);
+        if (s->address_state != PEER_ADDR_STATE_ACTIVE) {
+            _HIP_DEBUG("skipping non-active address %s\n", addrstr);
+            continue;
+        }
+
+        if (candidate) {
+            int this_is_later;
+            this_is_later = hip_timeval_diff(&s->modified_time, &latest, &dt);
+            _HIP_DEBUG("latest=%ld.%06ld\n", latest.tv_sec, latest.tv_usec);
+            _HIP_DEBUG("dt=%ld.%06ld\n", dt.tv_sec, dt.tv_usec);
+            if (this_is_later) {
+                _HIP_DEBUG("is later, change\n");
+                memcpy(&latest, &s->modified_time, sizeof(struct timeval));
+                candidate = s;
+            }
+        } else {
+            candidate = s;
+            memcpy(&latest, &s->modified_time, sizeof(struct timeval));
+        }
+    }
+
+    if (!candidate) {
+        HIP_ERROR("did not find usable peer address\n");
+        HIP_DEBUG("todo: select from other SPIs ?\n");
+        /* todo: select other SPI as the default SPI out */
+        err = -ENOMSG;
+    } else {ipv6_addr_copy(addr, &candidate->address);
+    }
+
+    return err;
 }
 
 /**
@@ -809,14 +822,14 @@
  */
 int hip_hadb_get_peer_addr(hip_ha_t *entry, struct in6_addr *addr)
 {
-       int err = 0;
-       //      struct hip_spi_out_item *spi_out;
-
-       /* assume already locked entry */
-
-       HIP_DEBUG_HIT("entry def addr", &entry->peer_addr);
-       ipv6_addr_copy(addr, &entry->peer_addr);
-        return err;
+    int err = 0;
+    // struct hip_spi_out_item *spi_out;
+
+    /* assume already locked entry */
+
+    HIP_DEBUG_HIT("entry def addr", &entry->peer_addr);
+    ipv6_addr_copy(addr, &entry->peer_addr);
+    return err;
 }
 
 /**
@@ -831,97 +844,97 @@
  * added successfully 0 is returned, else < 0.
  */
 int hip_hadb_add_peer_addr(hip_ha_t *entry, const struct in6_addr *new_addr,
-                          uint32_t spi, uint32_t lifetime, int state,
-                          in_port_t port)
+                           uint32_t spi, uint32_t lifetime, int state,
+                           in_port_t port)
 {
-        int err = 0;
-        struct hip_peer_addr_list_item *a_item;
-        char addrstr[INET6_ADDRSTRLEN];
-
-        /* assumes already locked entry */
-
-        /* check if we are adding the peer's address during the base
- *          * exchange */
-        if (spi == 0) {
-                HIP_DEBUG("SPI is 0, set address as the bex address\n");
-                if (!ipv6_addr_any(&entry->peer_addr)) {
-                        hip_in6_ntop(&entry->peer_addr, addrstr);
-                        HIP_DEBUG("warning, overwriting existing preferred 
address %s\n",
-                                  addrstr);
-                }
-                ipv6_addr_copy(&entry->peer_addr, new_addr);
-                HIP_DEBUG_IN6ADDR("entry->peer_address \n", &entry->peer_addr);
-
-                if (entry->peer_addr_list_to_be_added) {
-                        /*Adding the peer address to the 
entry->peer_addr_list_to_be_added
- *                          * So that later aftre base exchange it can be 
transfered to 
- *                                                   * SPI OUT's peer address 
list*/
-                        a_item = (struct hip_peer_addr_list_item 
*)HIP_MALLOC(sizeof(struct hip_peer_addr_list_item), GFP_KERNEL);
-                        if (!a_item)
-                        {
-                                HIP_ERROR("item HIP_MALLOC failed\n");
-                                err = -ENOMEM;
-                                goto out_err;
-                        }
-                        a_item->lifetime = lifetime;
-                        ipv6_addr_copy(&a_item->address, new_addr);
-                        a_item->address_state = state;
-                        do_gettimeofday(&a_item->modified_time);
-
-                        list_add(a_item, entry->peer_addr_list_to_be_added);
-                }
-                goto out_err;
+    int err = 0;
+    struct hip_peer_addr_list_item *a_item;
+    char addrstr[INET6_ADDRSTRLEN];
+
+    /* assumes already locked entry */
+
+    /* check if we are adding the peer's address during the base
+     *          * exchange */
+    if (spi == 0) {
+        HIP_DEBUG("SPI is 0, set address as the bex address\n");
+        if (!ipv6_addr_any(&entry->peer_addr)) {
+            hip_in6_ntop(&entry->peer_addr, addrstr);
+            HIP_DEBUG("warning, overwriting existing preferred address %s\n",
+                      addrstr);
         }
-
-        err = hip_hadb_get_peer_addr_info_old(entry, new_addr, NULL, NULL);
-        if (err)
-                goto out_err;
-
-        a_item = (struct hip_peer_addr_list_item *)HIP_MALLOC(sizeof(struct 
hip_peer_addr_list_item), GFP_KERNEL);
-        if (!a_item)
-        {
+        ipv6_addr_copy(&entry->peer_addr, new_addr);
+        HIP_DEBUG_IN6ADDR("entry->peer_address \n", &entry->peer_addr);
+
+        if (entry->peer_addr_list_to_be_added) {
+            /*Adding the peer address to the entry->peer_addr_list_to_be_added
+             *                          * So that later aftre base exchange it 
can be transfered to
+             *                                                   * SPI OUT's 
peer address list*/
+            a_item = (struct hip_peer_addr_list_item *) 
HIP_MALLOC(sizeof(struct hip_peer_addr_list_item), GFP_KERNEL);
+            if (!a_item) {
                 HIP_ERROR("item HIP_MALLOC failed\n");
                 err = -ENOMEM;
                 goto out_err;
+            }
+            a_item->lifetime      = lifetime;
+            ipv6_addr_copy(&a_item->address, new_addr);
+            a_item->address_state = state;
+            do_gettimeofday(&a_item->modified_time);
+
+            list_add(a_item, entry->peer_addr_list_to_be_added);
         }
-
-        a_item->lifetime = lifetime;
-        a_item->port = port;
-        ipv6_addr_copy(&a_item->address, new_addr);
-        a_item->address_state = state;
-        do_gettimeofday(&a_item->modified_time);
-
-        list_add(a_item, entry->peer_addresses_old);
+        goto out_err;
+    }
+
+    err    = hip_hadb_get_peer_addr_info_old(entry, new_addr, NULL, NULL);
+    if (err) {
+        goto out_err;
+    }
+
+    a_item = (struct hip_peer_addr_list_item *) HIP_MALLOC(sizeof(struct 
hip_peer_addr_list_item), GFP_KERNEL);
+    if (!a_item) {
+        HIP_ERROR("item HIP_MALLOC failed\n");
+        err = -ENOMEM;
+        goto out_err;
+    }
+
+    a_item->lifetime      = lifetime;
+    a_item->port          = port;
+    ipv6_addr_copy(&a_item->address, new_addr);
+    a_item->address_state = state;
+    do_gettimeofday(&a_item->modified_time);
+
+    list_add(a_item, entry->peer_addresses_old);
 
 out_err:
-        return err;
+    return err;
 }
 
 int hip_del_peer_info_entry(hip_ha_t *ha)
 {
 #ifdef CONFIG_HIP_OPPORTUNISTIC
-       hip_opp_block_t *opp_entry   = NULL;
+    hip_opp_block_t *opp_entry = NULL;
 #endif
 
-       HIP_LOCK_HA(ha);
+    HIP_LOCK_HA(ha);
 
-       /* by now, if everything is according to plans, the refcnt
-          should be 1 */
-       HIP_DEBUG_HIT("our HIT", &ha->hit_our);
-       HIP_DEBUG_HIT("peer HIT", &ha->hit_peer);
-       hip_delete_hit_sp_pair(&ha->hit_peer, &ha->hit_our, IPPROTO_ESP, 1);
+    /* by now, if everything is according to plans, the refcnt
+     * should be 1 */
+    HIP_DEBUG_HIT("our HIT", &ha->hit_our);
+    HIP_DEBUG_HIT("peer HIT", &ha->hit_peer);
+    hip_delete_hit_sp_pair(&ha->hit_peer, &ha->hit_our, IPPROTO_ESP, 1);
 
 #ifdef CONFIG_HIP_OPPORTUNISTIC
-       opp_entry = hip_oppdb_find_by_ip(&ha->peer_addr);
-       if(opp_entry)
-               hip_oppdb_entry_clean_up(opp_entry);
+    opp_entry = hip_oppdb_find_by_ip(&ha->peer_addr);
+    if (opp_entry) {
+        hip_oppdb_entry_clean_up(opp_entry);
+    }
 #endif
 
-       hip_hadb_delete_state(ha);
-
-       HIP_UNLOCK_HA(ha);
-
-       return 0;
+    hip_hadb_delete_state(ha);
+
+    HIP_UNLOCK_HA(ha);
+
+    return 0;
 }
 
 /**
@@ -929,14 +942,14 @@
  */
 int hip_del_peer_info(hip_hit_t *our_hit, hip_hit_t *peer_hit)
 {
-       hip_ha_t *ha;
-
-       ha = hip_hadb_find_byhits(our_hit, peer_hit);
-       if (!ha) {
-               return -ENOENT;
-       }
-
-       return hip_del_peer_info_entry(ha);
+    hip_ha_t *ha;
+
+    ha = hip_hadb_find_byhits(our_hit, peer_hit);
+    if (!ha) {
+        return -ENOENT;
+    }
+
+    return hip_del_peer_info_entry(ha);
 }
 
 /**
@@ -948,62 +961,60 @@
  * @return             0 if everything was stored successfully, otherwise < 0.
  */
 int hip_store_base_exchange_keys(struct hip_hadb_state *entry,
-                                 struct hip_context *ctx, int is_initiator)
+                                 struct hip_context *ctx, int is_initiator)
 {
-       int err = 0;
-       int hmac_key_len, enc_key_len, auth_key_len, hip_enc_key_len;
-
-       hmac_key_len = hip_hmac_key_length(entry->esp_transform);
-       enc_key_len = hip_enc_key_length(entry->esp_transform);
-       auth_key_len = hip_auth_key_length_esp(entry->esp_transform);
-       hip_enc_key_len = hip_transform_key_length(entry->hip_transform);
-
-       memcpy(&entry->hip_hmac_out, &ctx->hip_hmac_out, hmac_key_len);
-       memcpy(&entry->hip_hmac_in, &ctx->hip_hmac_in, hmac_key_len);
-
-       memcpy(&entry->esp_in.key, &ctx->esp_in.key, enc_key_len);
-       memcpy(&entry->auth_in.key, &ctx->auth_in.key, auth_key_len);
-
-       memcpy(&entry->esp_out.key, &ctx->esp_out.key, enc_key_len);
-       memcpy(&entry->auth_out.key, &ctx->auth_out.key, auth_key_len);
-
-       memcpy(&entry->hip_enc_out.key, &ctx->hip_enc_out.key, hip_enc_key_len);
-       memcpy(&entry->hip_enc_in.key, &ctx->hip_enc_in.key, hip_enc_key_len);
-
-       hip_update_entry_keymat(entry, ctx->current_keymat_index,
-                               ctx->keymat_calc_index, ctx->esp_keymat_index,
-                               ctx->current_keymat_K);
-
-       if (entry->dh_shared_key)
-       {
-               HIP_DEBUG("HIP_FREEing old dh_shared_key\n");
-               HIP_FREE(entry->dh_shared_key);
-               entry->dh_shared_key = NULL;
-       }
-
-       entry->dh_shared_key_len = 0;
-       /** @todo reuse pointer, no HIP_MALLOC */
-       entry->dh_shared_key = (char *)HIP_MALLOC(ctx->dh_shared_key_len, 
GFP_ATOMIC);
-       if (!entry->dh_shared_key)
-       {
-               HIP_ERROR("entry dh_shared HIP_MALLOC failed\n");
-               err = -ENOMEM;
-               goto out_err;
-       }
-
-       entry->dh_shared_key_len = ctx->dh_shared_key_len;
-       memcpy(entry->dh_shared_key, ctx->dh_shared_key, 
entry->dh_shared_key_len);
-       _HIP_HEXDUMP("Entry DH SHARED", entry->dh_shared_key, 
entry->dh_shared_key_len);
-       _HIP_HEXDUMP("Entry Kn", entry->current_keymat_K, HIP_AH_SHA_LEN);
-       return err;
+    int err = 0;
+    int hmac_key_len, enc_key_len, auth_key_len, hip_enc_key_len;
+
+    hmac_key_len    = hip_hmac_key_length(entry->esp_transform);
+    enc_key_len     = hip_enc_key_length(entry->esp_transform);
+    auth_key_len    = hip_auth_key_length_esp(entry->esp_transform);
+    hip_enc_key_len = hip_transform_key_length(entry->hip_transform);
+
+    memcpy(&entry->hip_hmac_out, &ctx->hip_hmac_out, hmac_key_len);
+    memcpy(&entry->hip_hmac_in, &ctx->hip_hmac_in, hmac_key_len);
+
+    memcpy(&entry->esp_in.key, &ctx->esp_in.key, enc_key_len);
+    memcpy(&entry->auth_in.key, &ctx->auth_in.key, auth_key_len);
+
+    memcpy(&entry->esp_out.key, &ctx->esp_out.key, enc_key_len);
+    memcpy(&entry->auth_out.key, &ctx->auth_out.key, auth_key_len);
+
+    memcpy(&entry->hip_enc_out.key, &ctx->hip_enc_out.key, hip_enc_key_len);
+    memcpy(&entry->hip_enc_in.key, &ctx->hip_enc_in.key, hip_enc_key_len);
+
+    hip_update_entry_keymat(entry, ctx->current_keymat_index,
+                            ctx->keymat_calc_index, ctx->esp_keymat_index,
+                            ctx->current_keymat_K);
+
+    if (entry->dh_shared_key) {
+        HIP_DEBUG("HIP_FREEing old dh_shared_key\n");
+        HIP_FREE(entry->dh_shared_key);
+        entry->dh_shared_key = NULL;
+    }
+
+    entry->dh_shared_key_len = 0;
+    /** @todo reuse pointer, no HIP_MALLOC */
+    entry->dh_shared_key     = (char *) HIP_MALLOC(ctx->dh_shared_key_len, 
GFP_ATOMIC);
+    if (!entry->dh_shared_key) {
+        HIP_ERROR("entry dh_shared HIP_MALLOC failed\n");
+        err = -ENOMEM;
+        goto out_err;
+    }
+
+    entry->dh_shared_key_len = ctx->dh_shared_key_len;
+    memcpy(entry->dh_shared_key, ctx->dh_shared_key, entry->dh_shared_key_len);
+    _HIP_HEXDUMP("Entry DH SHARED", entry->dh_shared_key, 
entry->dh_shared_key_len);
+    _HIP_HEXDUMP("Entry Kn", entry->current_keymat_K, HIP_AH_SHA_LEN);
+    return err;
 
 out_err:
-       if (entry->dh_shared_key) {
-               HIP_FREE(entry->dh_shared_key);
-               entry->dh_shared_key = NULL;
-       }
+    if (entry->dh_shared_key) {
+        HIP_FREE(entry->dh_shared_key);
+        entry->dh_shared_key = NULL;
+    }
 
-       return err;
+    return err;
 }
 
 /**
@@ -1015,47 +1026,46 @@
  * @return     ...
  */
 int hip_init_peer(hip_ha_t *entry, struct hip_common *msg,
-                 struct hip_host_id *peer)
+                  struct hip_host_id *peer)
 {
-       int err = 0;
-       int len = hip_get_param_total_len(peer);
-       struct in6_addr hit;
-
-       /* public key and verify function might be initialized already in the
-          case of loopback */
-
-       if (entry->peer_pub)
-       {
-               HIP_DEBUG("Not initializing peer host id, old exists\n");
-               goto out_err;
-       }
-
-       HIP_IFEL(hip_host_id_to_hit(peer, &hit, HIP_HIT_TYPE_HASH100) ||
-                ipv6_addr_cmp(&hit, &entry->hit_peer),
-                -1, "Unable to verify sender's HOST_ID\n");
-
-       HIP_IFEL(!(entry->peer_pub = HIP_MALLOC(len, GFP_KERNEL)),
-                -ENOMEM, "Out of memory\n");
-
-       memcpy(entry->peer_pub, peer, len);
-       entry->verify =
-               hip_get_host_id_algo(entry->peer_pub) == HIP_HI_RSA ?
-               hip_rsa_verify : hip_dsa_verify;
-
-       if (hip_get_host_id_algo(entry->peer_pub) == HIP_HI_RSA)
-               entry->peer_pub_key = hip_key_rr_to_rsa(
-                               (struct hip_host_id_priv *)entry->peer_pub, 0);
-       else
-               entry->peer_pub_key = hip_key_rr_to_dsa(
-                               (struct hip_host_id_priv *)entry->peer_pub, 0);
-
- out_err:
-       HIP_DEBUG_HIT("peer's hit", &hit);
-       HIP_DEBUG_HIT("entry's hit", &entry->hit_peer);
-       return err;
+    int err = 0;
+    int len = hip_get_param_total_len(peer);
+    struct in6_addr hit;
+
+    /* public key and verify function might be initialized already in the
+     * case of loopback */
+
+    if (entry->peer_pub) {
+        HIP_DEBUG("Not initializing peer host id, old exists\n");
+        goto out_err;
+    }
+
+    HIP_IFEL(hip_host_id_to_hit(peer, &hit, HIP_HIT_TYPE_HASH100) ||
+             ipv6_addr_cmp(&hit, &entry->hit_peer),
+             -1, "Unable to verify sender's HOST_ID\n");
+
+    HIP_IFEL(!(entry->peer_pub = HIP_MALLOC(len, GFP_KERNEL)),
+             -ENOMEM, "Out of memory\n");
+
+    memcpy(entry->peer_pub, peer, len);
+    entry->verify =
+        hip_get_host_id_algo(entry->peer_pub) == HIP_HI_RSA ?
+        hip_rsa_verify : hip_dsa_verify;
+
+    if (hip_get_host_id_algo(entry->peer_pub) == HIP_HI_RSA) {
+        entry->peer_pub_key = hip_key_rr_to_rsa(
+            (struct hip_host_id_priv *) entry->peer_pub, 0);
+    } else {
+        entry->peer_pub_key = hip_key_rr_to_dsa(
+            (struct hip_host_id_priv *) entry->peer_pub, 0);
+    }
+
+out_err:
+    HIP_DEBUG_HIT("peer's hit", &hit);
+    HIP_DEBUG_HIT("entry's hit", &entry->hit_peer);
+    return err;
 }
 
-
 /**
  * Initializes a HIP association.
  *
@@ -1068,190 +1078,195 @@
  */
 int hip_init_us(hip_ha_t *entry, hip_hit_t *hit_our)
 {
-        int err = 0, alg = 0;
-
-       if (entry->our_pub != NULL) {
-               free(entry->our_pub);
-               entry->our_pub = NULL;
-       }
-
-       /* Try to fetch our private host identity first using RSA then using 
DSA.
-          Note, that hip_get_host_id() allocates a new buffer and this buffer
-          must be freed in out_err if an error occurs. */
-
-       if (hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID, hit_our, HIP_HI_RSA,
-                               &entry->our_pub, &entry->our_priv_key)) {
-                HIP_IFEL(hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID, 
hit_our,
-                               HIP_HI_DSA, &entry->our_pub, 
&entry->our_priv_key),
-                               -1, "Local host identity not found\n");
-       }
-
-       /* RFC 4034 obsoletes RFC 2535 and flags field differ */
-       /* Get RFC2535 3.1 KEY RDATA format algorithm (Integer value). */
-       alg = hip_get_host_id_algo(entry->our_pub);
-       /* Using this integer we get a function pointer to a function that
-          signs our host identity. */
-       entry->sign = (alg == HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign);
-
-       /* Calculate our HIT from our public Host Identifier (HI).
-          Note, that currently (06.08.2008) both of these functions use DSA */
-       err = ((alg == HIP_HI_DSA) ?
-               hip_dsa_host_id_to_hit(entry->our_pub, &entry->hit_our,
-                                      HIP_HIT_TYPE_HASH100) :
-               hip_rsa_host_id_to_hit(entry->our_pub, &entry->hit_our,
-                                      HIP_HIT_TYPE_HASH100));
-       HIP_IFEL(err, err, "Unable to digest the HIT out of public key.");
-       if(err != 0) {
-               HIP_ERROR("Unable to digest the HIT out of public key.");
-               goto out_err;
-       }
-
- out_err:
-
-       if (err && entry->our_pub) {
-               HIP_FREE(entry->our_pub);
-               entry->our_pub = NULL;
-       }
-
-       return err;
+    int err = 0, alg = 0;
+
+    if (entry->our_pub != NULL) {
+        free(entry->our_pub);
+        entry->our_pub = NULL;
+    }
+
+    /* Try to fetch our private host identity first using RSA then using DSA.
+     * Note, that hip_get_host_id() allocates a new buffer and this buffer
+     * must be freed in out_err if an error occurs. */
+
+    if (hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID, hit_our, HIP_HI_RSA,
+                                     &entry->our_pub, &entry->our_priv_key)) {
+        HIP_IFEL(hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID, hit_our,
+                                              HIP_HI_DSA, &entry->our_pub, 
&entry->our_priv_key),
+                 -1, "Local host identity not found\n");
+    }
+
+    /* RFC 4034 obsoletes RFC 2535 and flags field differ */
+    /* Get RFC2535 3.1 KEY RDATA format algorithm (Integer value). */
+    alg         = hip_get_host_id_algo(entry->our_pub);
+    /* Using this integer we get a function pointer to a function that
+     * signs our host identity. */
+    entry->sign = (alg == HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign);
+
+    /* Calculate our HIT from our public Host Identifier (HI).
+     * Note, that currently (06.08.2008) both of these functions use DSA */
+    err         = ((alg == HIP_HI_DSA) ?
+                   hip_dsa_host_id_to_hit(entry->our_pub, &entry->hit_our,
+                                          HIP_HIT_TYPE_HASH100) :
+                   hip_rsa_host_id_to_hit(entry->our_pub, &entry->hit_our,
+                                          HIP_HIT_TYPE_HASH100));
+    HIP_IFEL(err, err, "Unable to digest the HIT out of public key.");
+    if (err != 0) {
+        HIP_ERROR("Unable to digest the HIT out of public key.");
+        goto out_err;
+    }
+
+out_err:
+
+    if (err && entry->our_pub) {
+        HIP_FREE(entry->our_pub);
+        entry->our_pub = NULL;
+    }
+
+    return err;
 }
 
 /* ----------------- */
 
 void hip_init_hadb(void)
 {
-     /** @todo Check for errors. */
-
-     /* The next line initializes the hash table for host associations. Note
-       that we are using callback wrappers IMPLEMENT_LHASH_HASH_FN and
-       IMPLEMENT_LHASH_COMP_FN defined in the beginning of this file. These
-       provide automagic variable casts, so that all elements stored in the
-       hash table are cast to hip_ha_t. Lauri 09.10.2007 16:58. */
-
-     hadb_hit = hip_ht_init(LHASH_HASH_FN(hip_ha),
-                           LHASH_COMP_FN(hip_ha));
-
-     /* initialize default function pointer sets for receiving messages*/
-     default_rcv_func_set.hip_receive_i1        = hip_receive_i1;
-     default_rcv_func_set.hip_receive_r1        = hip_receive_r1;
-     default_rcv_func_set.hip_receive_i2        = hip_receive_i2;
-     default_rcv_func_set.hip_receive_r2        = hip_receive_r2;
-     default_rcv_func_set.hip_receive_update    = hip_receive_update;
-     default_rcv_func_set.hip_receive_notify    = hip_receive_notify;
-     default_rcv_func_set.hip_receive_bos       = hip_receive_bos;
-     default_rcv_func_set.hip_receive_close     = hip_receive_close;
-     default_rcv_func_set.hip_receive_close_ack = hip_receive_close_ack;
-
-     /* initialize alternative function pointer sets for receiving messages*/
-     /* insert your alternative function sets here!*/
-
-     /* initialize default function pointer sets for handling messages*/
-     default_handle_func_set.hip_handle_i1        = hip_handle_i1;
-     default_handle_func_set.hip_handle_r1        = hip_handle_r1;
-     default_handle_func_set.hip_handle_i2        = hip_handle_i2;
-     default_handle_func_set.hip_handle_r2        = hip_handle_r2;
-     default_handle_func_set.hip_handle_bos       = hip_handle_bos;
-     default_handle_func_set.hip_handle_close     = hip_handle_close;
-     default_handle_func_set.hip_handle_close_ack = hip_handle_close_ack;
-
-     /* initialize alternative function pointer sets for handling messages*/
-     /* insert your alternative function sets here!*/
-
-     /* initialize default function pointer sets for misc functions*/
-     default_misc_func_set.hip_solve_puzzle           = hip_solve_puzzle;
-     default_misc_func_set.hip_produce_keying_material = 
hip_produce_keying_material;
-     default_misc_func_set.hip_create_i2              = hip_create_i2;
-     default_misc_func_set.hip_create_r2              = hip_create_r2;
-     default_misc_func_set.hip_build_network_hdr       = hip_build_network_hdr;
-
-     /* initialize alternative function pointer sets for misc functions*/
-     /* insert your alternative function sets here!*/
-
-     /* initialize default function pointer sets for update functions*/
-     /*default_update_func_set.hip_handle_update_plain_locator = 
hip_handle_update_plain_locator_old;
-     default_update_func_set.hip_handle_update_addr_verify   = 
hip_handle_update_addr_verify_old;
-     default_update_func_set.hip_update_handle_ack          = 
hip_update_handle_ack_old;
-     default_update_func_set.hip_handle_update_established   = 
hip_handle_update_established_old;
-     default_update_func_set.hip_handle_update_rekeying      = 
hip_handle_update_rekeying_old;
-     default_update_func_set.hip_update_send_addr_verify     = 
hip_update_send_addr_verify_deprecated;
-     default_update_func_set.hip_update_send_echo           = 
hip_update_send_echo_old;*/
-
-     /* xmit function set */
+    /** @todo Check for errors. */
+
+    /* The next line initializes the hash table for host associations. Note
+     * that we are using callback wrappers IMPLEMENT_LHASH_HASH_FN and
+     * IMPLEMENT_LHASH_COMP_FN defined in the beginning of this file. These
+     * provide automagic variable casts, so that all elements stored in the
+     * hash table are cast to hip_ha_t. Lauri 09.10.2007 16:58. */
+
+    hadb_hit                                   = 
hip_ht_init(LHASH_HASH_FN(hip_ha),
+                                                             
LHASH_COMP_FN(hip_ha));
+
+    /* initialize default function pointer sets for receiving messages*/
+    default_rcv_func_set.hip_receive_i1        = hip_receive_i1;
+    default_rcv_func_set.hip_receive_r1        = hip_receive_r1;
+    default_rcv_func_set.hip_receive_i2        = hip_receive_i2;
+    default_rcv_func_set.hip_receive_r2        = hip_receive_r2;
+    default_rcv_func_set.hip_receive_update    = hip_receive_update;
+    default_rcv_func_set.hip_receive_notify    = hip_receive_notify;
+    default_rcv_func_set.hip_receive_bos       = hip_receive_bos;
+    default_rcv_func_set.hip_receive_close     = hip_receive_close;
+    default_rcv_func_set.hip_receive_close_ack = hip_receive_close_ack;
+
+    /* initialize alternative function pointer sets for receiving messages*/
+    /* insert your alternative function sets here!*/
+
+    /* initialize default function pointer sets for handling messages*/
+    default_handle_func_set.hip_handle_i1        = hip_handle_i1;
+    default_handle_func_set.hip_handle_r1        = hip_handle_r1;
+    default_handle_func_set.hip_handle_i2        = hip_handle_i2;
+    default_handle_func_set.hip_handle_r2        = hip_handle_r2;
+    default_handle_func_set.hip_handle_bos       = hip_handle_bos;
+    default_handle_func_set.hip_handle_close     = hip_handle_close;
+    default_handle_func_set.hip_handle_close_ack = hip_handle_close_ack;
+
+    /* initialize alternative function pointer sets for handling messages*/
+    /* insert your alternative function sets here!*/
+
+    /* initialize default function pointer sets for misc functions*/
+    default_misc_func_set.hip_solve_puzzle            = hip_solve_puzzle;
+    default_misc_func_set.hip_produce_keying_material = 
hip_produce_keying_material;
+    default_misc_func_set.hip_create_i2               = hip_create_i2;
+    default_misc_func_set.hip_create_r2               = hip_create_r2;
+    default_misc_func_set.hip_build_network_hdr       = hip_build_network_hdr;
+
+    /* initialize alternative function pointer sets for misc functions*/
+    /* insert your alternative function sets here!*/
+
+    /* initialize default function pointer sets for update functions*/
+    /*default_update_func_set.hip_handle_update_plain_locator = 
hip_handle_update_plain_locator_old;
+     * default_update_func_set.hip_handle_update_addr_verify   = 
hip_handle_update_addr_verify_old;
+     * default_update_func_set.hip_update_handle_ack        = 
hip_update_handle_ack_old;
+     * default_update_func_set.hip_handle_update_established   = 
hip_handle_update_established_old;
+     * default_update_func_set.hip_handle_update_rekeying      = 
hip_handle_update_rekeying_old;
+     * default_update_func_set.hip_update_send_addr_verify     = 
hip_update_send_addr_verify_deprecated;
+     * default_update_func_set.hip_update_send_echo         = 
hip_update_send_echo_old;*/
+
+    /* xmit function set */
 #ifdef CONFIG_HIP_I3
-     if(hip_get_hi3_status()){
-            default_xmit_func_set.hip_send_pkt = hip_send_i3;
-     }
-     else
+    if (hip_get_hi3_status()) {
+        default_xmit_func_set.hip_send_pkt = hip_send_i3;
+    } else
 #endif
-     {
-            default_xmit_func_set.hip_send_pkt = hip_send_pkt;
-     }
-     
-
-     nat_xmit_func_set.hip_send_pkt = hip_send_pkt;
-
-     /* filter function sets */
-     /* Compiler warning: assignment from incompatible pointer type.
-       Please fix this, if you know what is the correct value.
-       -Lauri 25.09.2007 15:11. */
+    {
+        default_xmit_func_set.hip_send_pkt = hip_send_pkt;
+    }
+
+
+    nat_xmit_func_set.hip_send_pkt = hip_send_pkt;
+
+    /* filter function sets */
+    /* Compiler warning: assignment from incompatible pointer type.
+     * Please fix this, if you know what is the correct value.
+     * -Lauri 25.09.2007 15:11. */
     /* Wirtz 27/11/09 pointers are completely incomp. ( 1param to 4 params )
-    *  uncommented, please fix or remove completely */    
+     *  uncommented, please fix or remove completely */
     // default_input_filter_func_set.hip_input_filter  = hip_agent_filter;
     // default_output_filter_func_set.hip_output_filter = hip_agent_filter;
 
-     /* Tao Wan and Miika komu added, 24 Jan, 2008 for IPsec (userspace / 
kernel part)
-      *
-      * copy in user_ipsec_hipd_msg.c */
-     if (hip_use_userspace_ipsec) {
-            default_ipsec_func_set.hip_add_sa = hip_userspace_ipsec_add_sa;
-            default_ipsec_func_set.hip_delete_sa = 
hip_userspace_ipsec_delete_sa;
-            default_ipsec_func_set.hip_setup_hit_sp_pair = 
hip_userspace_ipsec_setup_hit_sp_pair;
-            default_ipsec_func_set.hip_delete_hit_sp_pair = 
hip_userspace_ipsec_delete_hit_sp_pair;
-            default_ipsec_func_set.hip_flush_all_policy = 
hip_userspace_ipsec_flush_all_policy;
-            default_ipsec_func_set.hip_flush_all_sa = 
hip_userspace_ipsec_flush_all_sa;
-            default_ipsec_func_set.hip_acquire_spi = hip_acquire_spi;
-            default_ipsec_func_set.hip_delete_default_prefix_sp_pair = 
hip_userspace_ipsec_delete_default_prefix_sp_pair;
-            default_ipsec_func_set.hip_setup_default_sp_prefix_pair = 
hip_userspace_ipsec_setup_default_sp_prefix_pair;
-     } else {
-            default_ipsec_func_set.hip_add_sa = hip_add_sa;
-            default_ipsec_func_set.hip_delete_sa = hip_delete_sa;
-            default_ipsec_func_set.hip_setup_hit_sp_pair = 
hip_setup_hit_sp_pair;
-            default_ipsec_func_set.hip_delete_hit_sp_pair = 
hip_delete_hit_sp_pair;
-            default_ipsec_func_set.hip_flush_all_policy = hip_flush_all_policy;
-            default_ipsec_func_set.hip_flush_all_sa = hip_flush_all_sa;
-            default_ipsec_func_set.hip_acquire_spi = hip_acquire_spi;
-            default_ipsec_func_set.hip_delete_default_prefix_sp_pair = 
hip_delete_default_prefix_sp_pair;
-            default_ipsec_func_set.hip_setup_default_sp_prefix_pair = 
hip_setup_default_sp_prefix_pair;
-     }
-}
-
-hip_xmit_func_set_t *hip_get_xmit_default_func_set(void) {
-       return &default_xmit_func_set;
-}
-
-hip_misc_func_set_t *hip_get_misc_default_func_set(void) {
-       return &default_misc_func_set;
-}
-
-hip_input_filter_func_set_t *hip_get_input_filter_default_func_set(void) {
-       return &default_input_filter_func_set;
-}
-
-hip_output_filter_func_set_t *hip_get_output_filter_default_func_set(void) {
-       return &default_output_filter_func_set;
-}
-
-hip_rcv_func_set_t *hip_get_rcv_default_func_set(void) {
-       return &default_rcv_func_set;
-}
-
-hip_handle_func_set_t *hip_get_handle_default_func_set(void) {
-       return &default_handle_func_set;
+    /* Tao Wan and Miika komu added, 24 Jan, 2008 for IPsec (userspace / 
kernel part)
+     *
+     * copy in user_ipsec_hipd_msg.c */
+    if (hip_use_userspace_ipsec) {
+        default_ipsec_func_set.hip_add_sa                        = 
hip_userspace_ipsec_add_sa;
+        default_ipsec_func_set.hip_delete_sa                     = 
hip_userspace_ipsec_delete_sa;
+        default_ipsec_func_set.hip_setup_hit_sp_pair             = 
hip_userspace_ipsec_setup_hit_sp_pair;
+        default_ipsec_func_set.hip_delete_hit_sp_pair            = 
hip_userspace_ipsec_delete_hit_sp_pair;
+        default_ipsec_func_set.hip_flush_all_policy              = 
hip_userspace_ipsec_flush_all_policy;
+        default_ipsec_func_set.hip_flush_all_sa                  = 
hip_userspace_ipsec_flush_all_sa;
+        default_ipsec_func_set.hip_acquire_spi                   = 
hip_acquire_spi;
+        default_ipsec_func_set.hip_delete_default_prefix_sp_pair = 
hip_userspace_ipsec_delete_default_prefix_sp_pair;
+        default_ipsec_func_set.hip_setup_default_sp_prefix_pair  = 
hip_userspace_ipsec_setup_default_sp_prefix_pair;
+    } else {
+        default_ipsec_func_set.hip_add_sa                        = hip_add_sa;
+        default_ipsec_func_set.hip_delete_sa                     = 
hip_delete_sa;
+        default_ipsec_func_set.hip_setup_hit_sp_pair             = 
hip_setup_hit_sp_pair;
+        default_ipsec_func_set.hip_delete_hit_sp_pair            = 
hip_delete_hit_sp_pair;
+        default_ipsec_func_set.hip_flush_all_policy              = 
hip_flush_all_policy;
+        default_ipsec_func_set.hip_flush_all_sa                  = 
hip_flush_all_sa;
+        default_ipsec_func_set.hip_acquire_spi                   = 
hip_acquire_spi;
+        default_ipsec_func_set.hip_delete_default_prefix_sp_pair = 
hip_delete_default_prefix_sp_pair;
+        default_ipsec_func_set.hip_setup_default_sp_prefix_pair  = 
hip_setup_default_sp_prefix_pair;
+    }
+}
+
+hip_xmit_func_set_t *hip_get_xmit_default_func_set(void)
+{
+    return &default_xmit_func_set;
+}
+
+hip_misc_func_set_t *hip_get_misc_default_func_set(void)
+{
+    return &default_misc_func_set;
+}
+
+hip_input_filter_func_set_t *hip_get_input_filter_default_func_set(void)
+{
+    return &default_input_filter_func_set;
+}
+
+hip_output_filter_func_set_t *hip_get_output_filter_default_func_set(void)
+{
+    return &default_output_filter_func_set;
+}
+
+hip_rcv_func_set_t *hip_get_rcv_default_func_set(void)
+{
+    return &default_rcv_func_set;
+}
+
+hip_handle_func_set_t *hip_get_handle_default_func_set(void)
+{
+    return &default_handle_func_set;
 }
 
 /*hip_update_func_set_t *hip_get_update_default_func_set() {
-       return &default_update_func_set;
-}*/
+ *      return &default_update_func_set;
+ * }*/
 
 /**
  * Sets function pointer set for an hadb record. Pointer values will not be
@@ -1261,151 +1276,151 @@
  * @param new_func_set  a pointer to the new function set
  * @return              0 if everything was stored successfully, otherwise < 0.
  */
-int hip_hadb_set_rcv_function_set(hip_ha_t * entry,
-                                        hip_rcv_func_set_t * new_func_set){
-     /** @todo add check whether all function pointers are set */
-     if( entry ){
-         entry->hadb_rcv_func = new_func_set;
-         return 0;
-     }
-      return -1;
-}
-
-/**
- * Sets function pointer set for an hadb record. Pointer values will not be
- * copied!
- *
- * @param entry        a pointer to the hadb record.
- * @param new_func_set a pointer to the new function set.
- * @return             0 if everything was stored successfully, otherwise < 0.
- */
-int hip_hadb_set_handle_function_set(hip_ha_t * entry,
-                                           hip_handle_func_set_t * 
new_func_set){
-       /** @todo add check whether all function pointers are set. */
-       if( entry ){
-               entry->hadb_handle_func = new_func_set;
-               return 0;
-       }
-       return -1;
-}
-
-/**
- * Sets function pointer set for an hadb record. Pointer values will not be
- * copied!
- *
- * @param entry        a pointer to the hadb record.
- * @param new_func_set a pointer to the new function set.
- * @return             0 if everything was stored successfully, otherwise < 0.
- */
-int hip_hadb_set_update_function_set(hip_ha_t * entry,
-                                           hip_update_func_set_t * 
new_func_set){
-     /** @todo add check whether all function pointers are set */
-       if( entry ){
-               entry->hadb_update_func = new_func_set;
-               return 0;
-       }
-       //HIP_ERROR("Func pointer set malformed. Func pointer set NOT appied.");
-       return -1;
+int hip_hadb_set_rcv_function_set(hip_ha_t *entry,
+                                  hip_rcv_func_set_t *new_func_set)
+{
+    /** @todo add check whether all function pointers are set */
+    if (entry) {
+        entry->hadb_rcv_func = new_func_set;
+        return 0;
+    }
+    return -1;
+}
+
+/**
+ * Sets function pointer set for an hadb record. Pointer values will not be
+ * copied!
+ *
+ * @param entry        a pointer to the hadb record.
+ * @param new_func_set a pointer to the new function set.
+ * @return             0 if everything was stored successfully, otherwise < 0.
+ */
+int hip_hadb_set_handle_function_set(hip_ha_t *entry,
+                                     hip_handle_func_set_t *new_func_set)
+{
+    /** @todo add check whether all function pointers are set. */
+    if (entry) {
+        entry->hadb_handle_func = new_func_set;
+        return 0;
+    }
+    return -1;
+}
+
+/**
+ * Sets function pointer set for an hadb record. Pointer values will not be
+ * copied!
+ *
+ * @param entry        a pointer to the hadb record.
+ * @param new_func_set a pointer to the new function set.
+ * @return             0 if everything was stored successfully, otherwise < 0.
+ */
+int hip_hadb_set_update_function_set(hip_ha_t *entry,
+                                     hip_update_func_set_t *new_func_set)
+{
+    /** @todo add check whether all function pointers are set */
+    if (entry) {
+        entry->hadb_update_func = new_func_set;
+        return 0;
+    }
+    //HIP_ERROR("Func pointer set malformed. Func pointer set NOT appied.");
+    return -1;
 }
 
 /* NOTE! When modifying this function, remember that some control values may
-   not be allowed to co-exist. Therefore the logical OR might not be enough
-   for all controls. */
+ * not be allowed to co-exist. Therefore the logical OR might not be enough
+ * for all controls. */
 /**
  * Switches on a local control bit for a host assosiation entry.
- * 
+ *
  * @param entry a pointer to a host assosiation.
  * @param mask  a bit mask representing the control value.
  * @note  mask is a single mask, not a logical AND or OR mask.
-*/
+ */
 void hip_hadb_set_local_controls(hip_ha_t *entry, hip_controls_t mask)
 {
-       if(entry != NULL) {
-               switch(mask) {
-
-               case HIP_HA_CTRL_NONE:
-                       entry->local_controls &= mask;
-               case HIP_HA_CTRL_LOCAL_REQ_UNSUP:
-               case HIP_HA_CTRL_LOCAL_REQ_RELAY:
-               case HIP_HA_CTRL_LOCAL_REQ_FULLRELAY:
-               case HIP_HA_CTRL_LOCAL_REQ_RVS:
-               case HIP_HA_CTRL_LOCAL_REQ_SAVAH:
-               case HIP_HA_CTRL_LOCAL_GRANTED_FULLRELAY:
+    if (entry != NULL) {
+        switch (mask) {
+        case HIP_HA_CTRL_NONE:
+            entry->local_controls &= mask;
+        case HIP_HA_CTRL_LOCAL_REQ_UNSUP:
+        case HIP_HA_CTRL_LOCAL_REQ_RELAY:
+        case HIP_HA_CTRL_LOCAL_REQ_FULLRELAY:
+        case HIP_HA_CTRL_LOCAL_REQ_RVS:
+        case HIP_HA_CTRL_LOCAL_REQ_SAVAH:
+        case HIP_HA_CTRL_LOCAL_GRANTED_FULLRELAY:
 #if 0
-                       if(mask == HIP_HA_CTRL_LOCAL_REQ_RELAY)
-                       {
-                               hip_nat_set_control(entry, 1);
-                               HIP_DEBUG("nat control has been reset to 1\n");
-                       }
-#endif                 
-                       entry->local_controls |= mask;
-                       break;
-               default:
-                       HIP_ERROR("Unknown local controls given.\n");
-               }
-       }
+            if (mask == HIP_HA_CTRL_LOCAL_REQ_RELAY) {
+                hip_nat_set_control(entry, 1);
+                HIP_DEBUG("nat control has been reset to 1\n");
+            }
+#endif
+            entry->local_controls |= mask;
+            break;
+        default:
+            HIP_ERROR("Unknown local controls given.\n");
+        }
+    }
 }
 
 /* NOTE! When modifying this function, remember that some control values may
-   not be allowed to co-exist. Therefore the logical OR might not be enough
-   for all controls. */
+ * not be allowed to co-exist. Therefore the logical OR might not be enough
+ * for all controls. */
 /**
  * Switches on a peer control bit for a host assosiation entry.
- * 
+ *
  * @param entry a pointer to a host assosiation.
  * @param mask  a bit mask representing the control value.
  * @note  mask is a single mask, not a logical AND or OR mask
-*/
+ */
 void hip_hadb_set_peer_controls(hip_ha_t *entry, hip_controls_t mask)
 {
-       if(entry != NULL) {
-               switch(mask) {
-
-               case HIP_HA_CTRL_NONE:
-                       entry->peer_controls &= mask;
-               case HIP_HA_CTRL_PEER_UNSUP_CAPABLE:
-               case HIP_HA_CTRL_PEER_RVS_CAPABLE:
-               case HIP_HA_CTRL_PEER_RELAY_CAPABLE:
-               case HIP_HA_CTRL_PEER_SAVAH_CAPABLE:
-               case HIP_HA_CTRL_PEER_FULLRELAY_CAPABLE:
-               case HIP_HA_CTRL_PEER_GRANTED_SAVAH:
-               case HIP_HA_CTRL_PEER_GRANTED_UNSUP:
-               case HIP_HA_CTRL_PEER_GRANTED_RVS:                      
-               case HIP_HA_CTRL_PEER_GRANTED_RELAY:
-               case HIP_HA_CTRL_PEER_GRANTED_FULLRELAY:
-               case HIP_HA_CTRL_PEER_REFUSED_UNSUP:
-               case HIP_HA_CTRL_PEER_REFUSED_RELAY:
-               case HIP_HA_CTRL_PEER_REFUSED_RVS:
-               case HIP_HA_CTRL_PEER_REFUSED_SAVAH:
-               case HIP_HA_CTRL_PEER_REFUSED_FULLRELAY:
+    if (entry != NULL) {
+        switch (mask) {
+        case HIP_HA_CTRL_NONE:
+            entry->peer_controls &= mask;
+        case HIP_HA_CTRL_PEER_UNSUP_CAPABLE:
+        case HIP_HA_CTRL_PEER_RVS_CAPABLE:
+        case HIP_HA_CTRL_PEER_RELAY_CAPABLE:
+        case HIP_HA_CTRL_PEER_SAVAH_CAPABLE:
+        case HIP_HA_CTRL_PEER_FULLRELAY_CAPABLE:
+        case HIP_HA_CTRL_PEER_GRANTED_SAVAH:
+        case HIP_HA_CTRL_PEER_GRANTED_UNSUP:
+        case HIP_HA_CTRL_PEER_GRANTED_RVS:
+        case HIP_HA_CTRL_PEER_GRANTED_RELAY:
+        case HIP_HA_CTRL_PEER_GRANTED_FULLRELAY:
+        case HIP_HA_CTRL_PEER_REFUSED_UNSUP:
+        case HIP_HA_CTRL_PEER_REFUSED_RELAY:
+        case HIP_HA_CTRL_PEER_REFUSED_RVS:
+        case HIP_HA_CTRL_PEER_REFUSED_SAVAH:
+        case HIP_HA_CTRL_PEER_REFUSED_FULLRELAY:
 #if 0
-                       if(mask == HIP_HA_CTRL_PEER_GRANTED_RELAY)
-                       {
-                               hip_nat_set_control(entry, 1);
-                               HIP_DEBUG("nat control has been reset to 1\n");
-                       }
+            if (mask == HIP_HA_CTRL_PEER_GRANTED_RELAY) {
+                hip_nat_set_control(entry, 1);
+                HIP_DEBUG("nat control has been reset to 1\n");
+            }
 #endif
-                       entry->peer_controls |= mask;
-                       break;
-               default:
-                       HIP_ERROR("Unknown peer controls given.\n");
-               }
-       }
+            entry->peer_controls |= mask;
+            break;
+        default:
+            HIP_ERROR("Unknown peer controls given.\n");
+        }
+    }
 }
 
 void hip_hadb_cancel_local_controls(hip_ha_t *entry, hip_controls_t mask)
 {
-       if(entry != NULL) {
-               entry->local_controls &= (~mask);
-       }
+    if (entry != NULL) {
+        entry->local_controls &= (~mask);
+    }
 }
 
 void hip_hadb_rec_free_doall(hip_ha_t *rec)
 {
-       if(hadb_hit == NULL || rec == NULL)
-               return;
+    if (hadb_hit == NULL || rec == NULL) {
+        return;
+    }
 
-       hip_del_peer_info_entry(rec);   
+    hip_del_peer_info_entry(rec);
 }
 
 /** A callback wrapper of the prototype required by @c lh_doall_arg(). */
@@ -1416,47 +1431,48 @@
  */
 void hip_uninit_hadb()
 {
-       if(hadb_hit == NULL)
-               return;
-       
-       hip_ht_doall(hadb_hit, 
(LHASH_DOALL_FN_TYPE)LHASH_DOALL_FN(hip_hadb_rec_free));
-       hip_ht_uninit(hadb_hit);
-       hadb_hit = NULL;
+    if (hadb_hit == NULL) {
+        return;
+    }
+
+    hip_ht_doall(hadb_hit, (LHASH_DOALL_FN_TYPE) 
LHASH_DOALL_FN(hip_hadb_rec_free));
+    hip_ht_uninit(hadb_hit);
+    hadb_hit = NULL;
 }
 
 void hip_delete_all_sp()
 {
-       //struct hip_hit_spi *hs, *tmp_hs;
-       HIP_DEBUG("\n");
-
-       HIP_DEBUG("DEBUG: DUMP SPI LISTS\n");
-
-       /* I think this is not very safe deallocation.
-        * Locking the hadb_spi and hadb_hit could be one option, but I'm not
-        * very sure that it will work, as they are locked later in
-        * hip_hadb_remove_state() for a while.
-        *
-        * The list traversing is not safe in smp way :(
-        */
-       HIP_DEBUG("DELETING HA HT\n");
-
+    //struct hip_hit_spi *hs, *tmp_hs;
+    HIP_DEBUG("\n");
+
+    HIP_DEBUG("DEBUG: DUMP SPI LISTS\n");
+
+    /* I think this is not very safe deallocation.
+     * Locking the hadb_spi and hadb_hit could be one option, but I'm not
+     * very sure that it will work, as they are locked later in
+     * hip_hadb_remove_state() for a while.
+     *
+     * The list traversing is not safe in smp way :(
+     */
+    HIP_DEBUG("DELETING HA HT\n");
 }
+
 /**
  * Removes all the addresses from the addresses_to_send_echo_request list
  * and deallocates them.
  * @param ha pointer to a host association
-*/
+ */
 void hip_remove_addresses_to_send_echo_request(hip_ha_t *ha)
 {
-       int i = 0;
-        struct in6_addr *address;
-       hip_list_t *item, *tmp;
+    int i = 0;
+    struct in6_addr *address;
+    hip_list_t *item, *tmp;
 
-       list_for_each_safe(item, tmp, ha->addresses_to_send_echo_request, i) {
-               address = (struct in6_addr *)list_entry(item);
-               list_del(address, ha->addresses_to_send_echo_request);
-               HIP_FREE(address);
-        }
+    list_for_each_safe(item, tmp, ha->addresses_to_send_echo_request, i) {
+        address = (struct in6_addr *) list_entry(item);
+        list_del(address, ha->addresses_to_send_echo_request);
+        HIP_FREE(address);
+    }
 }
 
 /**
@@ -1470,67 +1486,72 @@
  */
 void hip_hadb_delete_state(hip_ha_t *ha)
 {
-       hip_list_t *item = NULL, *tmp = NULL; 
-       struct hip_peer_addr_list_item *addr_li;
-       int i;
-
-       HIP_DEBUG("ha=0x%p\n", ha);
-
-       /* Delete SAs */
-
-       if (ha->dh_shared_key)
-               HIP_FREE(ha->dh_shared_key);
-       if (ha->hip_msg_retrans.buf) {
-               HIP_FREE(ha->hip_msg_retrans.buf);
-       }
-       if (ha->peer_pub) {
-               if (hip_get_host_id_algo(ha->peer_pub) == HIP_HI_RSA &&
-                                                       ha->peer_pub_key)
-                       RSA_free(ha->peer_pub_key);
-               else if (ha->peer_pub_key)
-                       DSA_free(ha->peer_pub_key);
-               HIP_FREE(ha->peer_pub);
-       }
-       if (ha->our_priv)
-               HIP_FREE(ha->our_priv);
-       if (ha->our_pub)
-               HIP_FREE(ha->our_pub);
-       if (ha->rendezvous_addr)
-               HIP_FREE(ha->rendezvous_addr);
-
-        if (ha->addresses_to_send_echo_request) {
-                hip_remove_addresses_to_send_echo_request(ha);
-               hip_ht_uninit(ha->addresses_to_send_echo_request);
-       }
-
-       if (ha->locator)
-               free(ha->locator);
-
-       if (ha->peer_addr_list_to_be_added) {
-               list_for_each_safe(item, tmp, ha->peer_addr_list_to_be_added, 
i) {
-                       addr_li = (struct hip_peer_addr_list_item 
*)list_entry(item);
-                       list_del(addr_li, ha->peer_addr_list_to_be_added);
-                       free(addr_li);
-                       HIP_DEBUG_HIT("SPI out address", &addr_li->address);
-               }
-               hip_ht_uninit(ha->peer_addr_list_to_be_added);
-       }
-
-       if (ha->peer_addresses_old) {
-               list_for_each_safe(item, tmp, ha->peer_addresses_old, i) {
-                       addr_li = (struct hip_peer_addr_list_item 
*)list_entry(item);
-                       list_del(addr_li, ha->peer_addresses_old);
-                       free(addr_li);
-                       HIP_DEBUG_HIT("SPI out address", &addr_li->address);
-               }
-               hip_ht_uninit(ha->peer_addresses_old);
-       }
-
-       list_del(ha, hadb_hit);
-       HIP_FREE(ha);
+    hip_list_t *item = NULL, *tmp = NULL;
+    struct hip_peer_addr_list_item *addr_li;
+    int i;
+
+    HIP_DEBUG("ha=0x%p\n", ha);
+
+    /* Delete SAs */
+
+    if (ha->dh_shared_key) {
+        HIP_FREE(ha->dh_shared_key);
+    }
+    if (ha->hip_msg_retrans.buf) {
+        HIP_FREE(ha->hip_msg_retrans.buf);
+    }
+    if (ha->peer_pub) {
+        if (hip_get_host_id_algo(ha->peer_pub) == HIP_HI_RSA &&
+            ha->peer_pub_key) {
+            RSA_free(ha->peer_pub_key);
+        } else if (ha->peer_pub_key) {
+            DSA_free(ha->peer_pub_key);
+        }
+        HIP_FREE(ha->peer_pub);
+    }
+    if (ha->our_priv) {
+        HIP_FREE(ha->our_priv);
+    }
+    if (ha->our_pub) {
+        HIP_FREE(ha->our_pub);
+    }
+    if (ha->rendezvous_addr) {
+        HIP_FREE(ha->rendezvous_addr);
+    }
+
+    if (ha->addresses_to_send_echo_request) {
+        hip_remove_addresses_to_send_echo_request(ha);
+        hip_ht_uninit(ha->addresses_to_send_echo_request);
+    }
+
+    if (ha->locator) {
+        free(ha->locator);
+    }
+
+    if (ha->peer_addr_list_to_be_added) {
+        list_for_each_safe(item, tmp, ha->peer_addr_list_to_be_added, i) {
+            addr_li = (struct hip_peer_addr_list_item *) list_entry(item);
+            list_del(addr_li, ha->peer_addr_list_to_be_added);
+            free(addr_li);
+            HIP_DEBUG_HIT("SPI out address", &addr_li->address);
+        }
+        hip_ht_uninit(ha->peer_addr_list_to_be_added);
+    }
+
+    if (ha->peer_addresses_old) {
+        list_for_each_safe(item, tmp, ha->peer_addresses_old, i) {
+            addr_li = (struct hip_peer_addr_list_item *) list_entry(item);
+            list_del(addr_li, ha->peer_addresses_old);
+            free(addr_li);
+            HIP_DEBUG_HIT("SPI out address", &addr_li->address);
+        }
+        hip_ht_uninit(ha->peer_addresses_old);
+    }
+
+    list_del(ha, hadb_hit);
+    HIP_FREE(ha);
 }
 
-
 /**
  * Maps function @c func to every HA in HIT hash table. The hash table is
  * LOCKED while we process all the entries. This means that the mapper function
@@ -1546,40 +1567,41 @@
  */
 int hip_for_each_ha(int (*func)(hip_ha_t *entry, void *opaq), void *opaque)
 {
-       int i = 0, fail = 0;
-       hip_ha_t *this;
-       hip_list_t *item, *tmp;
-
-       if (!func)
-               return -EINVAL;
-
-       HIP_LOCK_HT(&hadb_hit);
-       list_for_each_safe(item, tmp, hadb_hit, i)
-       {
-               this = (hip_ha_t *)list_entry(item);
-               _HIP_DEBUG("list_for_each_safe\n");
-               /* @todo: lock ha when we have threads */
-               fail = func(this, opaque);
-               /* @todo: unlock ha when we have threads */
-               if (fail)
-                       goto out_err;
-       }
-
- out_err:
-       HIP_UNLOCK_HT(&hadb_hit);
-       return fail;
+    int i = 0, fail = 0;
+    hip_ha_t *this;
+    hip_list_t *item, *tmp;
+
+    if (!func) {
+        return -EINVAL;
+    }
+
+    HIP_LOCK_HT(&hadb_hit);
+    list_for_each_safe(item, tmp, hadb_hit, i)
+    {
+        this = (hip_ha_t *) list_entry(item);
+        _HIP_DEBUG("list_for_each_safe\n");
+        /* @todo: lock ha when we have threads */
+        fail = func(this, opaque);
+        /* @todo: unlock ha when we have threads */
+        if (fail) {
+            goto out_err;
+        }
+    }
+
+out_err:
+    HIP_UNLOCK_HT(&hadb_hit);
+    return fail;
 }
 
 /** Enumeration for hip_count_open_connections */
 static int hip_count_one_entry(hip_ha_t *entry, void *cntr)
 {
-       int *counter = cntr;
-       if (entry->state == HIP_STATE_CLOSING ||
-           entry->state == HIP_STATE_ESTABLISHED)
-       {
-               (*counter)++;
-       }
-       return 0;
+    int *counter = cntr;
+    if (entry->state == HIP_STATE_CLOSING ||
+        entry->state == HIP_STATE_ESTABLISHED) {
+        (*counter)++;
+    }
+    return 0;
 }
 
 /**
@@ -1587,75 +1609,75 @@
  */
 int hip_count_open_connections(void)
 {
-       int n = 0;
-
-       hip_for_each_ha(hip_count_one_entry, &n);
-
-       return n;
+    int n = 0;
+
+    hip_for_each_ha(hip_count_one_entry, &n);
+
+    return n;
 }
 
 int hip_handle_get_ha_info(hip_ha_t *entry, void *opaq)
 {
-       int err = 0;
-       struct hip_hadb_user_info_state hid;
-       struct hip_common *msg = (struct hip_common *) opaq;
-       
-       memset(&hid, 0, sizeof(hid));
-       hid.state = entry->state;
-       ipv6_addr_copy(&hid.hit_our, &entry->hit_our);
-       ipv6_addr_copy(&hid.hit_peer, &entry->hit_peer);
-       ipv6_addr_copy(&hid.ip_our, &entry->our_addr);
-       ipv6_addr_copy(&hid.ip_peer, &entry->peer_addr);
-       ipv4_addr_copy(&hid.lsi_our, &entry->lsi_our);
-       ipv4_addr_copy(&hid.lsi_peer, &entry->lsi_peer);
-       memcpy(&hid.peer_hostname, &entry->peer_hostname, 
HIP_HOST_ID_HOSTNAME_LEN_MAX);
-
-       hid.heartbeats_on = hip_icmp_interval;
-       calc_statistics(&entry->heartbeats_statistics, (uint32_t 
*)&hid.heartbeats_received, NULL, NULL,
-                       &hid.heartbeats_mean, &hid.heartbeats_variance, 
STATS_IN_MSECS);
+    int err                = 0;
+    struct hip_hadb_user_info_state hid;
+    struct hip_common *msg = (struct hip_common *) opaq;
+
+    memset(&hid, 0, sizeof(hid));
+    hid.state = entry->state;
+    ipv6_addr_copy(&hid.hit_our, &entry->hit_our);
+    ipv6_addr_copy(&hid.hit_peer, &entry->hit_peer);
+    ipv6_addr_copy(&hid.ip_our, &entry->our_addr);
+    ipv6_addr_copy(&hid.ip_peer, &entry->peer_addr);
+    ipv4_addr_copy(&hid.lsi_our, &entry->lsi_our);
+    ipv4_addr_copy(&hid.lsi_peer, &entry->lsi_peer);
+    memcpy(&hid.peer_hostname, &entry->peer_hostname, 
HIP_HOST_ID_HOSTNAME_LEN_MAX);
+
+    hid.heartbeats_on       = hip_icmp_interval;
+    calc_statistics(&entry->heartbeats_statistics, (uint32_t *) 
&hid.heartbeats_received, NULL, NULL,
+                    &hid.heartbeats_mean, &hid.heartbeats_variance, 
STATS_IN_MSECS);
 #if 0
-       hid.heartbeats_mean = entry->heartbeats_mean;
-       hid.heartbeats_variance = entry->heartbeats_variance;
-       hid.heartbeats_received = entry->heartbeats_statistics.num_items;
+    hid.heartbeats_mean     = entry->heartbeats_mean;
+    hid.heartbeats_variance = entry->heartbeats_variance;
+    hid.heartbeats_received = entry->heartbeats_statistics.num_items;
 #endif
-       hid.heartbeats_sent = entry->heartbeats_sent;
-
-       /*For some reason this gives negative result*/
-       /*hip_timeval_diff(&entry->bex_start, 
-                        &entry->bex_end,
-                        &hid.bex_duration);*/
-
-
-       //      struct timeval * duration = hip_get_duration(entry->bex_start, 
entry->bex_end);
-       //      HIP_ASSERT(duration != NULL);
-       //      memcpy(&hid.bex_duration,  duration, sizeof(struct timeval));
-
-
-       
-       _HIP_HEXDUMP("HEXHID ", &hid, sizeof(struct hip_hadb_user_info_state));
-       
-       hid.nat_udp_port_peer = entry->peer_udp_port;
-       hid.nat_udp_port_local = entry->local_udp_port;
-
-       hid.peer_controls = entry->peer_controls;
-
-       /* does not print heartbeat info, but I do not think it even should 
-Samu*/
-       hip_print_debug_info(&hid.ip_our,   &hid.ip_peer,
-                            &hid.hit_our,  &hid.hit_peer,
-                            &hid.lsi_peer, (char*)&hid.peer_hostname,
-                            &hid.nat_udp_port_local, &hid.nat_udp_port_peer);
-
-        hid.shotgun_status = entry->shotgun_status;
-
-       err = hip_build_param_contents(msg, &hid, HIP_PARAM_HA_INFO,
-                                      sizeof(hid));
-       if (err)
-               HIP_ERROR("Building ha info failed\n");
-
-       _HIP_HEXDUMP("HEXHID ", &hid, sizeof(struct hip_hadb_user_info_state));
-
-       return err;
-
+    hid.heartbeats_sent     = entry->heartbeats_sent;
+
+    /*For some reason this gives negative result*/
+    /*hip_timeval_diff(&entry->bex_start,
+     *               &entry->bex_end,
+     *               &hid.bex_duration);*/
+
+
+    // struct timeval * duration = hip_get_duration(entry->bex_start, 
entry->bex_end);
+    // HIP_ASSERT(duration != NULL);
+    // memcpy(&hid.bex_duration,  duration, sizeof(struct timeval));
+
+
+
+    _HIP_HEXDUMP("HEXHID ", &hid, sizeof(struct hip_hadb_user_info_state));
+
+    hid.nat_udp_port_peer  = entry->peer_udp_port;
+    hid.nat_udp_port_local = entry->local_udp_port;
+
+    hid.peer_controls      = entry->peer_controls;
+
+    /* does not print heartbeat info, but I do not think it even should -Samu*/
+    hip_print_debug_info(&hid.ip_our,   &hid.ip_peer,
+                         &hid.hit_our,  &hid.hit_peer,
+                         &hid.lsi_peer, (char *) &hid.peer_hostname,
+                         &hid.nat_udp_port_local, &hid.nat_udp_port_peer);
+
+    hid.shotgun_status = entry->shotgun_status;
+
+    err                = hip_build_param_contents(msg, &hid, HIP_PARAM_HA_INFO,
+                                                  sizeof(hid));
+    if (err) {
+        HIP_ERROR("Building ha info failed\n");
+    }
+
+    _HIP_HEXDUMP("HEXHID ", &hid, sizeof(struct hip_hadb_user_info_state));
+
+    return err;
 }
 
 /**
@@ -1663,20 +1685,20 @@
  */
 int hip_hadb_map_ip_to_hit(hip_ha_t *entry, void *id2)
 {
-       struct in6_addr *id = id2;
-    int err = 0;
-       
+    struct in6_addr *id = id2;
+    int err             = 0;
+
     if (ipv6_addr_cmp(&entry->peer_addr, id) == 0 &&
-               !ipv6_addr_any(&entry->hit_peer) &&
-               !hit_is_opportunistic_hashed_hit(&entry->hit_peer)) {
-               ipv6_addr_copy(id, &entry->hit_peer);
-               HIP_DEBUG_HIT("hit", &entry->hit_peer);
-               HIP_DEBUG_HIT("pref", &entry->peer_addr);
-               HIP_DEBUG_HIT("id", id);
-               err = -1; /* break iteration */
-       }
+        !ipv6_addr_any(&entry->hit_peer) &&
+        !hit_is_opportunistic_hashed_hit(&entry->hit_peer)) {
+        ipv6_addr_copy(id, &entry->hit_peer);
+        HIP_DEBUG_HIT("hit", &entry->hit_peer);
+        HIP_DEBUG_HIT("pref", &entry->peer_addr);
+        HIP_DEBUG_HIT("id", id);
+        err = -1;         /* break iteration */
+    }
 
-       return err;
+    return err;
 }
 
 #ifdef CONFIG_HIP_RVS
@@ -1697,31 +1719,32 @@
  *                   a matching host association was not found.
  * @author           Miika Komu
  * @date             31.08.2006
-*/ 
+ */
 hip_ha_t *hip_hadb_find_rvs_candidate_entry(hip_hit_t *local_hit,
-                                           hip_hit_t *rvs_ip)
+                                            hip_hit_t *rvs_ip)
 {
-       int i = 0;
-       hip_ha_t *this = NULL, *result = NULL;
-       hip_list_t *item = NULL, *tmp = NULL; //
-
-       HIP_LOCK_HT(&hadb_hit);
-       list_for_each_safe(item, tmp, hadb_hit, i)
-       {
-               this = (hip_ha_t *)list_entry(item);
-               _HIP_DEBUG("List_for_each_entry_safe\n");
-               /* @todo: lock ha when we have threads */
-               if ((ipv6_addr_cmp(local_hit, &this->hit_our) == 0) &&
-                   (ipv6_addr_cmp(rvs_ip, &this->peer_addr) == 0)) {
-                       result = this;
-                       break;
-               }
-               /* @todo: unlock ha when we have threads */
-       }
-       HIP_UNLOCK_HT(&hadb_hit);
-
-       return result;
+    int i            = 0;
+    hip_ha_t *this   = NULL, *result = NULL;
+    hip_list_t *item = NULL, *tmp = NULL;     //
+
+    HIP_LOCK_HT(&hadb_hit);
+    list_for_each_safe(item, tmp, hadb_hit, i)
+    {
+        this = (hip_ha_t *) list_entry(item);
+        _HIP_DEBUG("List_for_each_entry_safe\n");
+        /* @todo: lock ha when we have threads */
+        if ((ipv6_addr_cmp(local_hit, &this->hit_our) == 0) &&
+            (ipv6_addr_cmp(rvs_ip, &this->peer_addr) == 0)) {
+            result = this;
+            break;
+        }
+        /* @todo: unlock ha when we have threads */
+    }
+    HIP_UNLOCK_HT(&hadb_hit);
+
+    return result;
 }
+
 #endif
 
 
@@ -1731,64 +1754,67 @@
  * @date 22.07.2008
  */
 hip_ha_t *hip_hadb_find_by_blind_hits(hip_hit_t *local_blind_hit,
-                                     hip_hit_t *peer_blind_hit)
+                                      hip_hit_t *peer_blind_hit)
 {
-       int err = 0;
-       /* int i = 0; */
-       /* hip_ha_t *this = NULL, *tmp = NULL; */
-       hip_ha_t *result = NULL;
-
-       /*
-         This loop is disabled since &hadb_byhit[i] does not exist anymore and
-         the code won't compile with CONFIG_HIP_BLIND flag set.
-         -Lauri 22.07.2008
-         for(i = 0; i < HIP_HADB_SIZE; i++) {
-
-         list_for_each_entry_safe(this, tmp, &hadb_byhit[i], next_hit)
-         {
-         _HIP_DEBUG("List_for_each_entry_safe\n");
-         hip_hold_ha(this);
-         if ((ipv6_addr_cmp(local_blind_hit, &this->hit_our_blind) == 0) &&
-         (ipv6_addr_cmp(peer_blind_hit, &this->hit_peer_blind) == 0)) {
-         result = this;
-         break;
-         }
-         hip_db_put_ha(this, hip_hadb_delete_state);
-         if (err)
-         break;
-         }
-         if (err)
-         break;
-         }
-       */
-
-       if (err)
-               result = NULL;
-
-       return result;
+    int err          = 0;
+    /* int i = 0; */
+    /* hip_ha_t *this = NULL, *tmp = NULL; */
+    hip_ha_t *result = NULL;
+
+    /*
+     * This loop is disabled since &hadb_byhit[i] does not exist anymore and
+     * the code won't compile with CONFIG_HIP_BLIND flag set.
+     * -Lauri 22.07.2008
+     * for(i = 0; i < HIP_HADB_SIZE; i++) {
+     *
+     * list_for_each_entry_safe(this, tmp, &hadb_byhit[i], next_hit)
+     * {
+     * _HIP_DEBUG("List_for_each_entry_safe\n");
+     * hip_hold_ha(this);
+     * if ((ipv6_addr_cmp(local_blind_hit, &this->hit_our_blind) == 0) &&
+     * (ipv6_addr_cmp(peer_blind_hit, &this->hit_peer_blind) == 0)) {
+     * result = this;
+     * break;
+     * }
+     * hip_db_put_ha(this, hip_hadb_delete_state);
+     * if (err)
+     * break;
+     * }
+     * if (err)
+     * break;
+     * }
+     */
+
+    if (err) {
+        result = NULL;
+    }
+
+    return result;
 }
+
 #endif
 
-static int hip_host_file_info_exists_lsi(hip_lsi_t *lsi){
-  uint8_t hostname[HOST_NAME_MAX];
-  struct in6_addr mapped_lsi;
-  
-  memset(hostname, 0, sizeof(hostname));
-
-  IPV4_TO_IPV6_MAP(lsi, &mapped_lsi);
-
-  return !(hip_for_each_hosts_file_line(HIPL_HOSTS_FILE,
-                                      hip_map_first_id_to_hostname_from_hosts,
-                                      &mapped_lsi, hostname) &&
-               hip_for_each_hosts_file_line(HOSTS_FILE,
-                                      hip_map_first_id_to_hostname_from_hosts,
-                                      &mapped_lsi, hostname));
+static int hip_host_file_info_exists_lsi(hip_lsi_t *lsi)
+{
+    uint8_t hostname[HOST_NAME_MAX];
+    struct in6_addr mapped_lsi;
+
+    memset(hostname, 0, sizeof(hostname));
+
+    IPV4_TO_IPV6_MAP(lsi, &mapped_lsi);
+
+    return !(hip_for_each_hosts_file_line(HIPL_HOSTS_FILE,
+                                          
hip_map_first_id_to_hostname_from_hosts,
+                                          &mapped_lsi, hostname) &&
+             hip_for_each_hosts_file_line(HOSTS_FILE,
+                                          
hip_map_first_id_to_hostname_from_hosts,
+                                          &mapped_lsi, hostname));
 }
 
 static int hip_hadb_find_lsi(hip_ha_t *entry, void *lsi)
 {
     int exist_lsi;
-    exist_lsi = hip_lsi_are_equal(&entry->lsi_peer,(hip_lsi_t *)lsi);
+    exist_lsi = hip_lsi_are_equal(&entry->lsi_peer, (hip_lsi_t *) lsi);
     if (exist_lsi) {
         memset(lsi, 0, sizeof(lsi));
     }
@@ -1797,45 +1823,47 @@
 
 static int hip_hadb_exists_lsi(hip_lsi_t *lsi)
 {
-       int res = 0;
-       hip_lsi_t lsi_aux;
-
-       memcpy(&lsi_aux, lsi, sizeof(hip_lsi_t));
-       hip_for_each_ha(hip_hadb_find_lsi, &lsi_aux);
-
-       if (ipv4_addr_cmp(&lsi_aux, lsi) != 0){
-               res = 1;
-               HIP_DEBUG("lsi exists\n");
-       }
-       return res;
+    int res = 0;
+    hip_lsi_t lsi_aux;
+
+    memcpy(&lsi_aux, lsi, sizeof(hip_lsi_t));
+    hip_for_each_ha(hip_hadb_find_lsi, &lsi_aux);
+
+    if (ipv4_addr_cmp(&lsi_aux, lsi) != 0) {
+        res = 1;
+        HIP_DEBUG("lsi exists\n");
+    }
+    return res;
 }
 
 static int lsi_assigned(struct in_addr add)
 {
-        int exist = 0;
-        exist = hip_hidb_exists_lsi(&add);
-        if (!exist)
-                exist = hip_hadb_exists_lsi(&add);
-        if (!exist)
-                exist = hip_host_file_info_exists_lsi(&add);
-        return exist;
+    int exist = 0;
+    exist = hip_hidb_exists_lsi(&add);
+    if (!exist) {
+        exist = hip_hadb_exists_lsi(&add);
+    }
+    if (!exist) {
+        exist = hip_host_file_info_exists_lsi(&add);
+    }
+    return exist;
 }
 
 int hip_generate_peer_lsi(hip_lsi_t *lsi)
 {
-        struct in_addr lsi_prefix;
-        uint8_t hostname[HOST_NAME_MAX];
-        int index = 1;
-
-        do {
-                lsi_prefix.s_addr = htonl(HIP_LSI_PREFIX|index++);
-        } while (lsi_assigned(lsi_prefix) ||
-                 !hip_map_lsi_to_hostname_from_hosts(lsi, (char*)hostname));
-
-        _HIP_DEBUG_LSI("lsi free final value is ", &lsi_prefix);
-
-        *lsi = lsi_prefix;
-        return 0;
+    struct in_addr lsi_prefix;
+    uint8_t hostname[HOST_NAME_MAX];
+    int index = 1;
+
+    do {
+        lsi_prefix.s_addr = htonl(HIP_LSI_PREFIX | index++);
+    } while (lsi_assigned(lsi_prefix) ||
+             !hip_map_lsi_to_hostname_from_hosts(lsi, (char *) hostname));
+
+    _HIP_DEBUG_LSI("lsi free final value is ", &lsi_prefix);
+
+    *lsi = lsi_prefix;
+    return 0;
 }
 
 /**
@@ -1847,144 +1875,150 @@
  * Our implementation doesn't allow repeated lsi tuples.
  */
 
-hip_ha_t *hip_hadb_try_to_find_by_pair_lsi(hip_lsi_t *lsi_src, hip_lsi_t 
*lsi_dst){
-        hip_list_t *item, *aux;
-       hip_ha_t *tmp;
-       int i;
+hip_ha_t *hip_hadb_try_to_find_by_pair_lsi(hip_lsi_t *lsi_src, hip_lsi_t 
*lsi_dst)
+{
+    hip_list_t *item, *aux;
+    hip_ha_t *tmp;
+    int i;
 
-       list_for_each_safe(item, aux, hadb_hit, i)
-       {
-               tmp = (hip_ha_t *)list_entry(item);
-               if(!hip_lsi_are_equal(&tmp->lsi_peer, lsi_dst))
-                       continue;
-               else if (hip_lsi_are_equal(&tmp->lsi_our, lsi_src))
-                       return tmp;
-               else
-                       continue;
-       }
-       return NULL;
+    list_for_each_safe(item, aux, hadb_hit, i)
+    {
+        tmp = (hip_ha_t *) list_entry(item);
+        if (!hip_lsi_are_equal(&tmp->lsi_peer, lsi_dst)) {
+            continue;
+        } else if (hip_lsi_are_equal(&tmp->lsi_our, lsi_src)) {
+            return tmp;
+        } else {
+            continue;
+        }
+    }
+    return NULL;
 }
 
-hip_ha_t *hip_hadb_try_to_find_by_peer_lsi(hip_lsi_t *lsi_dst) {
-        hip_list_t *item, *aux;
-       hip_ha_t *tmp;
-       int i;
+hip_ha_t *hip_hadb_try_to_find_by_peer_lsi(hip_lsi_t *lsi_dst)
+{
+    hip_list_t *item, *aux;
+    hip_ha_t *tmp;
+    int i;
 
-       list_for_each_safe(item, aux, hadb_hit, i)
-       {
-               tmp = (hip_ha_t *)list_entry(item);
-               if(hip_lsi_are_equal(&tmp->lsi_peer, lsi_dst))
-                       return tmp;
-       }
-       return NULL;
+    list_for_each_safe(item, aux, hadb_hit, i)
+    {
+        tmp = (hip_ha_t *) list_entry(item);
+        if (hip_lsi_are_equal(&tmp->lsi_peer, lsi_dst)) {
+            return tmp;
+        }
+    }
+    return NULL;
 }
 
 int hip_get_local_addr(struct hip_common *msg)
 {
-       hip_ha_t* entry;
-       int err;
-       struct in6_addr local_address;
-       hip_hit_t* src_hit;
-       hip_hit_t* dst_hit;
+    hip_ha_t *entry;
+    int err;
+    struct in6_addr local_address;
+    hip_hit_t *src_hit;
+    hip_hit_t *dst_hit;
 
-       src_hit = (hip_hit_t *) hip_get_param_contents(msg, HIP_PARAM_HIT);
-       dst_hit = (hip_hit_t *) hip_get_param_contents(msg, 
HIP_PARAM_IPV6_ADDR);
-       HIP_DEBUG_HIT("src_hit from local address request: ", src_hit);
-       HIP_DEBUG_HIT("dst_hit from local address request: ", dst_hit);
+    src_hit = (hip_hit_t *) hip_get_param_contents(msg, HIP_PARAM_HIT);
+    dst_hit = (hip_hit_t *) hip_get_param_contents(msg, HIP_PARAM_IPV6_ADDR);
+    HIP_DEBUG_HIT("src_hit from local address request: ", src_hit);
+    HIP_DEBUG_HIT("dst_hit from local address request: ", dst_hit);
 /*     if (ptr) {
-               memcpy(peer_hit, ptr, sizeof(hip_hit_t));
-               HIP_DEBUG_HIT("peer_hit", peer_hit);
-               *fallback = 0;
-       }
-*/
-       memset(&local_address, 0, sizeof(struct in6_addr));
-       entry = hip_hadb_find_byhits(src_hit, dst_hit);
-
-       hip_msg_init(msg);
-       //HIP_DEBUG_IN6ADDR(" local address: ", &entry->our_addr);
-
-       if(!entry)
-               HIP_DEBUG("Can't find local address because of no entry in 
hadb!\n");
-
-       ipv6_addr_copy(&local_address, &entry->our_addr);
-
-       //hip_build_user_hdr(msg, HIP_HIPPROXY_LOCAL_ADDRESS, 0);
-       err = hip_build_param_contents(msg, &local_address, HIP_PARAM_IPV6_ADDR,
-                                      sizeof(struct in6_addr));
-       if (err)
-               HIP_ERROR("Building local address info failed\n");
-
-       return 0;
+ *              memcpy(peer_hit, ptr, sizeof(hip_hit_t));
+ *              HIP_DEBUG_HIT("peer_hit", peer_hit);
+ *fallback = 0;
+ *      }
+ */
+    memset(&local_address, 0, sizeof(struct in6_addr));
+    entry = hip_hadb_find_byhits(src_hit, dst_hit);
+
+    hip_msg_init(msg);
+    //HIP_DEBUG_IN6ADDR(" local address: ", &entry->our_addr);
+
+    if (!entry) {
+        HIP_DEBUG("Can't find local address because of no entry in hadb!\n");
+    }
+
+    ipv6_addr_copy(&local_address, &entry->our_addr);
+
+    //hip_build_user_hdr(msg, HIP_HIPPROXY_LOCAL_ADDRESS, 0);
+    err = hip_build_param_contents(msg, &local_address, HIP_PARAM_IPV6_ADDR,
+                                   sizeof(struct in6_addr));
+    if (err) {
+        HIP_ERROR("Building local address info failed\n");
+    }
+
+    return 0;
 }
 
 void hip_delete_security_associations_and_sp(struct hip_hadb_state *ha)
 {
-        int prev_spi_out = ha->spi_outbound_current;
-        int prev_spi_in = ha->spi_inbound_current;
-
-        // Delete previous security policies
-        ha->hadb_ipsec_func->hip_delete_hit_sp_pair(&ha->hit_our, 
&ha->hit_peer,
-                IPPROTO_ESP, 1);
-        ha->hadb_ipsec_func->hip_delete_hit_sp_pair(&ha->hit_peer, 
&ha->hit_our,
-                IPPROTO_ESP, 1);
-
-        // Delete the previous SAs
-        HIP_DEBUG("Previous SPI out =0x%x\n", prev_spi_out);
-        HIP_DEBUG("Previous SPI in =0x%x\n", prev_spi_in);
-
-        HIP_DEBUG_IN6ADDR("Our current active addr", &ha->our_addr);
-        HIP_DEBUG_IN6ADDR("Peer's current active addr", &ha->peer_addr);
-
-        default_ipsec_func_set.hip_delete_sa(prev_spi_out, &ha->peer_addr,
-                                            &ha->our_addr, 
HIP_SPI_DIRECTION_OUT, ha);
-           default_ipsec_func_set.hip_delete_sa(prev_spi_in, &ha->our_addr,
-                                            &ha->peer_addr, 
HIP_SPI_DIRECTION_IN, ha);
-
-        return;
+    int prev_spi_out = ha->spi_outbound_current;
+    int prev_spi_in  = ha->spi_inbound_current;
+
+    // Delete previous security policies
+    ha->hadb_ipsec_func->hip_delete_hit_sp_pair(&ha->hit_our, &ha->hit_peer,
+                                                IPPROTO_ESP, 1);
+    ha->hadb_ipsec_func->hip_delete_hit_sp_pair(&ha->hit_peer, &ha->hit_our,
+                                                IPPROTO_ESP, 1);
+
+    // Delete the previous SAs
+    HIP_DEBUG("Previous SPI out =0x%x\n", prev_spi_out);
+    HIP_DEBUG("Previous SPI in =0x%x\n", prev_spi_in);
+
+    HIP_DEBUG_IN6ADDR("Our current active addr", &ha->our_addr);
+    HIP_DEBUG_IN6ADDR("Peer's current active addr", &ha->peer_addr);
+
+    default_ipsec_func_set.hip_delete_sa(prev_spi_out, &ha->peer_addr,
+                                         &ha->our_addr, HIP_SPI_DIRECTION_OUT, 
ha);
+    default_ipsec_func_set.hip_delete_sa(prev_spi_in, &ha->our_addr,
+                                         &ha->peer_addr, HIP_SPI_DIRECTION_IN, 
ha);
+
+    return;
 };
 
 int hip_recreate_security_associations_and_sp(struct hip_hadb_state *ha, 
in6_addr_t *src_addr,
-        in6_addr_t *dst_addr)
+                                              in6_addr_t *dst_addr)
 {
-        int err = 0;
-
-        int new_spi_out = ha->spi_outbound_new;
-        int new_spi_in = ha->spi_inbound_current;
-
-        hip_delete_security_associations_and_sp(ha);
-
-        // Create a new security policy
-        HIP_IFEL(ha->hadb_ipsec_func->hip_setup_hit_sp_pair(&ha->hit_peer,
-                &ha->hit_our, dst_addr, src_addr, IPPROTO_ESP, 1, 0),
-             -1, "Setting up SP pair failed\n");
-
-        // Create a new inbound SA
-        HIP_DEBUG("Creating a new inbound SA, SPI=0x%x\n", new_spi_in);
-
-        HIP_IFEL(ha->hadb_ipsec_func->hip_add_sa(dst_addr, src_addr,
-                &ha->hit_peer, &ha->hit_our, new_spi_in, ha->esp_transform,
-                &ha->esp_in, &ha->auth_in, 1, HIP_SPI_DIRECTION_IN, 0,
-                ha), -1,
-             "Error while changing inbound security association\n");
-
-       HIP_DEBUG("New inbound SA created with SPI=0x%x\n", new_spi_in);
-
-        /*HIP_IFEL(ha->hadb_ipsec_func->hip_setup_hit_sp_pair(&ha->hit_our,
-                &ha->hit_peer, src_addr, dst_addr, IPPROTO_ESP, 1, 0), -1,
-                "Setting up SP pair failed\n");      */
-
-        // Create a new outbound SA
-        HIP_DEBUG("Creating a new outbound SA, SPI=0x%x\n", new_spi_out);
-       ha->local_udp_port = ha->nat_mode ? hip_get_local_nat_udp_port() : 0;
-
-       HIP_IFEL(ha->hadb_ipsec_func->hip_add_sa(src_addr, dst_addr,
-                &ha->hit_our, &ha->hit_peer, new_spi_out, ha->esp_transform,
-                &ha->esp_out, &ha->auth_out, 1, HIP_SPI_DIRECTION_OUT, 0,
-                ha), -1,
-             "Error while changing outbound security association\n");
-
-       HIP_DEBUG("New outbound SA created with SPI=0x%x\n", new_spi_out);
+    int err         = 0;
+
+    int new_spi_out = ha->spi_outbound_new;
+    int new_spi_in  = ha->spi_inbound_current;
+
+    hip_delete_security_associations_and_sp(ha);
+
+    // Create a new security policy
+    HIP_IFEL(ha->hadb_ipsec_func->hip_setup_hit_sp_pair(&ha->hit_peer,
+                                                        &ha->hit_our, 
dst_addr, src_addr, IPPROTO_ESP, 1, 0),
+             -1, "Setting up SP pair failed\n");
+
+    // Create a new inbound SA
+    HIP_DEBUG("Creating a new inbound SA, SPI=0x%x\n", new_spi_in);
+
+    HIP_IFEL(ha->hadb_ipsec_func->hip_add_sa(dst_addr, src_addr,
+                                             &ha->hit_peer, &ha->hit_our, 
new_spi_in, ha->esp_transform,
+                                             &ha->esp_in, &ha->auth_in, 1, 
HIP_SPI_DIRECTION_IN, 0,
+                                             ha), -1,
+             "Error while changing inbound security association\n");
+
+    HIP_DEBUG("New inbound SA created with SPI=0x%x\n", new_spi_in);
+
+    /*HIP_IFEL(ha->hadb_ipsec_func->hip_setup_hit_sp_pair(&ha->hit_our,
+     *      &ha->hit_peer, src_addr, dst_addr, IPPROTO_ESP, 1, 0), -1,
+     *       "Setting up SP pair failed\n");      */
+
+    // Create a new outbound SA
+    HIP_DEBUG("Creating a new outbound SA, SPI=0x%x\n", new_spi_out);
+    ha->local_udp_port = ha->nat_mode ? hip_get_local_nat_udp_port() : 0;
+
+    HIP_IFEL(ha->hadb_ipsec_func->hip_add_sa(src_addr, dst_addr,
+                                             &ha->hit_our, &ha->hit_peer, 
new_spi_out, ha->esp_transform,
+                                             &ha->esp_out, &ha->auth_out, 1, 
HIP_SPI_DIRECTION_OUT, 0,
+                                             ha), -1,
+             "Error while changing outbound security association\n");
+
+    HIP_DEBUG("New outbound SA created with SPI=0x%x\n", new_spi_out);
 
 out_err:
-        return err;
+    return err;
 };

=== modified file 'hipd/hadb.c.doxyme'
--- hipd/hadb.c.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/hadb.c.doxyme  2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,82 +45,82 @@
 
 
 /**
- * hip_add_peer_map 
+ * hip_add_peer_map
  *
  *
  * @param input
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_count_open_connections 
+ * hip_count_open_connections
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_del_peer_info 
+ * hip_del_peer_info
  *
  *
  * @param our_hit
  * @param peer_hit
- * @return 
- **/
-
-
-/**
- * hip_del_peer_info_entry 
- *
- *
- * @param ha
- * @return 
- **/
-
-
-/**
- * hip_delete_security_associations_and_sp 
- *
- *
- * @param ha
- * @return 
- **/
-
-
-/**
- * hip_for_each_ha 
+ * @return
+ **/
+
+
+/**
+ * hip_del_peer_info_entry
+ *
+ *
+ * @param ha
+ * @return
+ **/
+
+
+/**
+ * hip_delete_security_associations_and_sp
+ *
+ *
+ * @param ha
+ * @return
+ **/
+
+
+/**
+ * hip_for_each_ha
  *
  *
  * @param entry
  * @param opaq)
  * @param opaque)
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_generate_peer_lsi 
+ * hip_generate_peer_lsi
  *
  *
  * @param lsi
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_local_addr 
+ * hip_get_local_addr
  *
  *
  * @param msg
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_add_peer_addr 
+ * hip_hadb_add_peer_addr
  *
  *
  * @param entry
@@ -128,24 +129,24 @@
  * @param lifetime
  * @param state
  * @param port
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_add_peer_info 
+ * hip_hadb_add_peer_info
  *
  *
  * @param peer_hit
  * @param peer_addr
  * @param peer_lsi
  * @param peer_hostname
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_add_peer_info_complete 
+ * hip_hadb_add_peer_info_complete
  *
  *
  * @param local_hit
@@ -154,307 +155,307 @@
  * @param local_addr
  * @param peer_addr
  * @param peer_hostname
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_add_peer_info_wrapper 
+ * hip_hadb_add_peer_info_wrapper
  *
  *
  * @param entry
  * @param peer_map_void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_cancel_local_controls 
+ * hip_hadb_cancel_local_controls
  *
  *
  * @param entry
  * @param mask
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_create_state 
+ * hip_hadb_create_state
  *
  *
  * @param gfpmask
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_delete_state 
+ * hip_hadb_delete_state
  *
  *
  * @param ha
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_find_by_blind_hits 
+ * hip_hadb_find_by_blind_hits
  *
  *
  * @param local_blind_hit
  * @param peer_blind_hit
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_find_byhits 
+ * hip_hadb_find_byhits
  *
  *
  * @param hit
  * @param hit2
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_find_rvs_candidate_entry 
+ * hip_hadb_find_rvs_candidate_entry
  *
  *
  * @param local_hit
  * @param rvs_ip
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_get_peer_addr 
+ * hip_hadb_get_peer_addr
  *
  *
  * @param entry
  * @param addr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_hash_file_hits 
+ * hip_hadb_hash_file_hits
  *
  *
  * @param ptr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_init_entry 
+ * hip_hadb_init_entry
  *
  *
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_insert_state 
+ * hip_hadb_insert_state
  *
  *
  * @param ha
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_map_ip_to_hit 
+ * hip_hadb_map_ip_to_hit
  *
  *
  * @param entry
  * @param id2
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_put_entry 
+ * hip_hadb_put_entry
  *
  *
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_select_spi_addr 
+ * hip_hadb_select_spi_addr
  *
  *
  * @param entry
  * @param spi_out
  * @param addr
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_handle_function_set 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_input_filter_function_set 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_local_controls 
- *
- *
- * @param entry
- * @param mask
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_misc_function_set 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_output_filter_function_set 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_peer_controls 
- *
- *
- * @param entry
- * @param mask
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_rcv_function_set 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_update_function_set 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_hadb_set_xmit_function_set 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_hadb_try_to_find_by_pair_lsi 
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_handle_function_set
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_input_filter_function_set
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_local_controls
+ *
+ *
+ * @param entry
+ * @param mask
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_misc_function_set
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_output_filter_function_set
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_peer_controls
+ *
+ *
+ * @param entry
+ * @param mask
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_rcv_function_set
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_update_function_set
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_set_xmit_function_set
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_hadb_try_to_find_by_pair_lsi
  *
  *
  * @param lsi_src
  * @param lsi_dst
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_try_to_find_by_peer_hit 
+ * hip_hadb_try_to_find_by_peer_hit
  *
  *
  * @param hit
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_try_to_find_by_peer_lsi 
+ * hip_hadb_try_to_find_by_peer_lsi
  *
  *
  * @param lsi_dst
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_handle_get_ha_info 
+ * hip_handle_get_ha_info
  *
  *
  * @param entry
  * @param opaq
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_host_file_info_exists_lsi 
+ * hip_host_file_info_exists_lsi
  *
  *
  * @param lsi
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_hadb 
+ * hip_init_hadb
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_peer 
+ * hip_init_peer
  *
  *
  * @param entry
  * @param msg
  * @param peer
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_us 
+ * hip_init_us
  *
  *
  * @param entry
  * @param hit_our
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_print_debug_info 
+ * hip_print_debug_info
  *
  *
  * @param local_addr
@@ -465,46 +466,45 @@
  * @param peer_hostname
  * @param local_nat_udp_port
  * @param peer_nat_udp_port
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_recreate_security_associations_and_sp 
+ * hip_recreate_security_associations_and_sp
  *
  *
  * @param ha
  * @param src_addr
  * @param dst_addr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_remove_addresses_to_send_echo_request 
+ * hip_remove_addresses_to_send_echo_request
  *
  *
  * @param ha
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_store_base_exchange_keys 
+ * hip_store_base_exchange_keys
  *
  *
  * @param entry
  * @param ctx
  * @param is_initiator
- * @return 
+ * @return
  **/
 
 
 /**
- * lsi_assigned 
+ * lsi_assigned
  *
  *
  * @param add
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/hadb.h'
--- hipd/hadb.h 2010-02-08 15:18:06 +0000
+++ hipd/hadb.h 2010-02-10 22:32:46 +0000
@@ -13,8 +13,8 @@
 #include "lib/core/hashtable.h"
 #include "lib/core/state.h"
 #include "lib/core/builder.h"
-#include "input.h"     // required for declaration of receive functions
-#include "update.h"    // required for declaration of update function
+#include "input.h"      // required for declaration of receive functions
+#include "update.h"     // required for declaration of update function
 #include "user_ipsec_sadb_api.h"
 #include "lib/tool/xfrmapi.h"
 #include "nat.h"
@@ -25,7 +25,7 @@
 #endif
 
 #define HIP_LOCK_INIT(ha)
-#define HIP_LOCK_HA(ha) 
+#define HIP_LOCK_HA(ha)
 #define HIP_UNLOCK_HA(ha)
 
 #define do_gettimeofday(x) gettimeofday(x, NULL)
@@ -42,7 +42,7 @@
 /* Matching */
 static inline int hip_hadb_match_spi(const void *key_1, const void *key_2)
 {
-       return (* (const u32 *) key_1 == * (const u32 *) key_2);
+    return *(const u32 *) key_1 == *(const u32 *) key_2;
 }
 
 int hip_ha_compare(const hip_ha_t *ha1, const hip_ha_t *ha2);
@@ -62,8 +62,8 @@
 void hip_hadb_delete_state(hip_ha_t *ha);
 int hip_hadb_insert_state(hip_ha_t *ha);
 void hip_delete_security_associations_and_sp(struct hip_hadb_state *ha);
-int hip_init_peer(hip_ha_t *entry, struct hip_common *msg, 
-                    struct hip_host_id *peer);
+int hip_init_peer(hip_ha_t *entry, struct hip_common *msg,
+                  struct hip_host_id *peer);
 int hip_init_us(hip_ha_t *entry, hip_hit_t *hit_our);
 
 
@@ -71,62 +71,67 @@
 int hip_hadb_get_peer_addr(hip_ha_t *entry, struct in6_addr *addr);
 
 int hip_hadb_add_peer_addr(hip_ha_t *entry, const struct in6_addr *new_addr,
-                          uint32_t interface_id, uint32_t lifetime,
-                          int state, in_port_t port);
+                           uint32_t interface_id, uint32_t lifetime,
+                           int state, in_port_t port);
 
 int hip_add_peer_map(const struct hip_common *input);
 
-int hip_hadb_add_peer_info(hip_hit_t *hit, struct in6_addr *addr, hip_lsi_t 
*peer_lsi,
-                          const char *peer_hostname);
+int hip_hadb_add_peer_info(hip_hit_t *hit,
+                           struct in6_addr *addr,
+                           hip_lsi_t *peer_lsi,
+                           const char *peer_hostname);
 
 int hip_hadb_add_peer_info_complete(const hip_hit_t *local_hit,
-                                   const hip_hit_t *peer_hit,
-                                   const hip_lsi_t *peer_lsi,
-                                   const struct in6_addr *local_addr,
-                                   const struct in6_addr *peer_addr,
-                                   const char *peer_hostname);
+                                    const hip_hit_t *peer_hit,
+                                    const hip_lsi_t *peer_lsi,
+                                    const struct in6_addr *local_addr,
+                                    const struct in6_addr *peer_addr,
+                                    const char *peer_hostname);
 
 int hip_del_peer_info_entry(hip_ha_t *ha);
 int hip_del_peer_info(hip_hit_t *, hip_hit_t *);
 
 void hip_hadb_set_spi_ifindex(hip_ha_t *entry, uint32_t spi, int ifindex);
-int hip_store_base_exchange_keys(struct hip_hadb_state *entry, 
-                                struct hip_context *ctx, int is_initiator);
+int hip_store_base_exchange_keys(struct hip_hadb_state *entry,
+                                 struct hip_context *ctx, int is_initiator);
 /* Utilities */
 
 hip_ha_t *hip_hadb_create_state(int gfpmask);
 
 #if 0
 typedef struct hip_peer_addr_opaque {
-        struct in6_addr addr;
-        struct hip_peer_addr_opaque *next;
+    struct in6_addr              addr;
+    struct hip_peer_addr_opaque *next;
 } hip_peer_addr_opaque_t;         /* Structure to record peer addresses */
 
 typedef struct hip_peer_entry_opaque {
-       unsigned int count;
-        struct hip_host_id *host_id;
-       hip_hit_t hit;
-        hip_peer_addr_opaque_t *addr_list;
-        struct hip_peer_entry_opaque *next;
+    unsigned int                  count;
+    struct hip_host_id *          host_id;
+    hip_hit_t                     hit;
+    hip_peer_addr_opaque_t *      addr_list;
+    struct hip_peer_entry_opaque *next;
 } hip_peer_entry_opaque_t;         /* Structure to record kernel peer entry */
 
 typedef struct hip_peer_opaque {
-       unsigned int count;
-        struct hip_peer_entry_opaque *head;
-        struct hip_peer_entry_opaque *end;
+    unsigned int                  count;
+    struct hip_peer_entry_opaque *head;
+    struct hip_peer_entry_opaque *end;
 } hip_peer_opaque_t;         /* Structure to record kernel peer list */
 #endif
 
-int hip_for_each_ha(int (func)(hip_ha_t *entry, void *opaq), void *opaque);
-
-// next 2 functions are not called from outside but make sense and are 
'proposed' in libhipcore/state.h
+int hip_for_each_ha(int(func) (hip_ha_t * entry, void *opaq), void *opaque);
+
+/* next 2 functions are not called from outside but make sense and are
+ * 'proposed' in libhipcore/state.h
+ */
+
 int hip_hadb_set_rcv_function_set(hip_ha_t *entry,
-                                  hip_rcv_func_set_t *new_func_set);
+                                  hip_rcv_func_set_t *new_func_set);
 int hip_hadb_set_handle_function_set(hip_ha_t *entry,
-                                  hip_handle_func_set_t *new_func_set);
+                                     hip_handle_func_set_t *new_func_set);
 
-int hip_hadb_set_xmit_function_set(hip_ha_t * entry,
-                                  hip_xmit_func_set_t * new_func_set);
+int hip_hadb_set_xmit_function_set(hip_ha_t *entry,
+                                   hip_xmit_func_set_t *new_func_set);
 
 void hip_hadb_set_local_controls(hip_ha_t *entry, hip_controls_t mask);
 void hip_hadb_set_peer_controls(hip_ha_t *entry, hip_controls_t mask);
@@ -138,7 +143,7 @@
 
 hip_ha_t *hip_hadb_find_rvs_candidate_entry(hip_hit_t *, hip_hit_t *);
 hip_ha_t *hip_hadb_find_by_blind_hits(hip_hit_t *local_blind_hit,
-                                     hip_hit_t *peer_blind_hit);
+                                      hip_hit_t *peer_blind_hit);
 
 int hip_handle_get_ha_info(hip_ha_t *entry, void *);
 int hip_hadb_map_ip_to_hit(hip_ha_t *entry, void *id2);
@@ -149,8 +154,9 @@
 hip_ha_t *hip_hadb_try_to_find_by_pair_lsi(hip_lsi_t *lsi_src, hip_lsi_t 
*lsi_dst);
 int hip_get_local_addr(struct hip_common *msg);
 
-int hip_recreate_security_associations_and_sp(struct hip_hadb_state *ha, 
in6_addr_t *src_addr,
-        in6_addr_t *dst_addr);
+int hip_recreate_security_associations_and_sp(struct hip_hadb_state *ha,
+                                              in6_addr_t *src_addr,
+                                              in6_addr_t *dst_addr);
 
 hip_rcv_func_set_t *hip_get_rcv_default_func_set(void);
 hip_handle_func_set_t *hip_get_handle_default_func_set(void);

=== modified file 'hipd/hadb.h.doxyme'
--- hipd/hadb.h.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/hadb.h.doxyme  2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hadb.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/hadb_legacy.c'
--- hipd/hadb_legacy.c  2010-02-08 15:18:06 +0000
+++ hipd/hadb_legacy.c  2010-02-10 22:32:46 +0000
@@ -21,68 +21,70 @@
  *                      is returned, else @c interface_id and @c lifetime are
  *                      not assigned a value and 0 is returned.
  */
-int hip_hadb_get_peer_addr_info_old(hip_ha_t *entry, const struct in6_addr 
*addr,
-                               uint32_t *lifetime, struct timeval 
*modified_time)
+int hip_hadb_get_peer_addr_info_old(hip_ha_t *entry,
+                                    const struct in6_addr *addr,
+                                    uint32_t *lifetime,
+                                    struct timeval *modified_time)
 {
-       // 99999: REMOVE
-        /*struct hip_peer_addr_list_item *peer_addr_list_item;
-       int i = 1, ii;
-       struct hip_spi_out_item *spi_out;
-       hip_list_t *item, *tmp, *a_item, *a_tmp;*/
-
-        struct hip_peer_addr_list_item *peer_addr_list_item;
-       int i = 1, ii;
-       hip_list_t *item, *tmp;
-
-        list_for_each_safe(item, tmp, entry->peer_addresses_old, ii)
-        {
-                peer_addr_list_item = (struct hip_peer_addr_list_item 
*)list_entry(item);
-               if (!ipv6_addr_cmp(&peer_addr_list_item->address, addr))
-                {
-                        _HIP_DEBUG("found\n");
-                        if (lifetime)
-                                *lifetime = peer_addr_list_item->lifetime;
-
-                        if (modified_time)
-                        {
-                                modified_time->tv_sec = 
peer_addr_list_item->modified_time.tv_sec;
-                                modified_time->tv_usec = 
peer_addr_list_item->modified_time.tv_usec;
-                        }
-
-                        return 1;
-                }
-
-                i++;
+    // 99999: REMOVE
+    /*struct hip_peer_addr_list_item *peer_addr_list_item;
+     * int i = 1, ii;
+     * struct hip_spi_out_item *spi_out;
+     * hip_list_t *item, *tmp, *a_item, *a_tmp;*/
+
+    struct hip_peer_addr_list_item *peer_addr_list_item;
+    int i = 1, ii;
+    hip_list_t *item, *tmp;
+
+    list_for_each_safe(item, tmp, entry->peer_addresses_old, ii)
+    {
+        peer_addr_list_item = (struct hip_peer_addr_list_item *) 
list_entry(item);
+
+        if (!ipv6_addr_cmp(&peer_addr_list_item->address, addr)) {
+            _HIP_DEBUG("found\n");
+            if (lifetime) {
+                *lifetime = peer_addr_list_item->lifetime;
+            }
+
+            if (modified_time) {
+                modified_time->tv_sec  = 
peer_addr_list_item->modified_time.tv_sec;
+                modified_time->tv_usec = 
peer_addr_list_item->modified_time.tv_usec;
+            }
+
+            return 1;
         }
 
-       // 99999: REMOVE
-        /* assumes already locked entry */
+        i++;
+    }
+
+    // 99999: REMOVE
+    /* assumes already locked entry */
 /*     list_for_each_safe(item, tmp, entry->spis_out_old, ii)
-       {
-               spi_out = list_entry(item);
-               list_for_each_safe(a_item, a_tmp, entry->peer_addresses_old, 
iii)
-               {
-                       s = list_entry(a_item);
-                       if (!ipv6_addr_cmp(&s->address, addr))
-                       {
-                               _HIP_DEBUG("found\n");
-                               if (lifetime)
-                                       *lifetime = s->lifetime;
-                               if (modified_time)
-                               {
-                                       modified_time->tv_sec = 
s->modified_time.tv_sec;
-                                       modified_time->tv_usec = 
s->modified_time.tv_usec;
-                               }
-                               if (spi)
-                                       *spi = spi_out->spi;
-                               return 1;
-                       }
-                       i++;
-               }
-       }*/
+ *      {
+ *              spi_out = list_entry(item);
+ *              list_for_each_safe(a_item, a_tmp, entry->peer_addresses_old, 
iii)
+ *              {
+ *                      s = list_entry(a_item);
+ *                      if (!ipv6_addr_cmp(&s->address, addr))
+ *                      {
+ *                              _HIP_DEBUG("found\n");
+ *                              if (lifetime)
+ *lifetime = s->lifetime;
+ *                              if (modified_time)
+ *                              {
+ *                                      modified_time->tv_sec = 
s->modified_time.tv_sec;
+ *                                      modified_time->tv_usec = 
s->modified_time.tv_usec;
+ *                              }
+ *                              if (spi)
+ *spi = spi_out->spi;
+ *                              return 1;
+ *                      }
+ *                      i++;
+ *              }
+ *      }*/
 
-       _HIP_DEBUG("not found\n");
-       return 0;
+    _HIP_DEBUG("not found\n");
+    return 0;
 }
 
 /**
@@ -93,26 +95,24 @@
  */
 void hip_hadb_delete_peer_addrlist_one_old(hip_ha_t *ha, struct in6_addr *addr)
 {
-       struct hip_peer_addr_list_item *peer_addr_list_item;
-       int i;
-       hip_list_t *item, *tmp;
-
-       /* possibly deprecated function .. */
-
-        list_for_each_safe(item, tmp, ha->peer_addresses_old, i)
-        {
-                peer_addr_list_item = (struct hip_peer_addr_list_item 
*)list_entry(item);
-                if (!ipv6_addr_cmp(&peer_addr_list_item->address, addr))
-                {
-                        _HIP_DEBUG("deleting address\n");
-                        list_del(item, ha->peer_addresses_old);
-                        HIP_FREE(item);
-                        /* if address is on more than one spi list then do not 
goto out */
-                        goto out;
-                }
-            }
-
- out:
-       return;
+    struct hip_peer_addr_list_item *peer_addr_list_item;
+    int i;
+    hip_list_t *item, *tmp;
+
+    /* possibly deprecated function .. */
+
+    list_for_each_safe(item, tmp, ha->peer_addresses_old, i)
+    {
+        peer_addr_list_item = (struct hip_peer_addr_list_item *) 
list_entry(item);
+        if (!ipv6_addr_cmp(&peer_addr_list_item->address, addr)) {
+            _HIP_DEBUG("deleting address\n");
+            list_del(item, ha->peer_addresses_old);
+            HIP_FREE(item);
+            /* if address is on more than one spi list then do not goto out */
+            goto out;
+        }
+    }
+
+out:
+    return;
 }
-

=== modified file 'hipd/hadb_legacy.c.doxyme'
--- hipd/hadb_legacy.c.doxyme   2009-12-12 10:44:54 +0000
+++ hipd/hadb_legacy.c.doxyme   2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,23 +45,22 @@
 
 
 /**
- * hip_hadb_delete_peer_addrlist_one_old 
+ * hip_hadb_delete_peer_addrlist_one_old
  *
  *
  * @param ha
  * @param addr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hadb_get_peer_addr_info_old 
+ * hip_hadb_get_peer_addr_info_old
  *
  *
  * @param entry
  * @param addr
  * @param lifetime
  * @param modified_time
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/hadb_legacy.h'
--- hipd/hadb_legacy.h  2010-02-08 15:18:06 +0000
+++ hipd/hadb_legacy.h  2010-02-10 22:32:46 +0000
@@ -3,19 +3,23 @@
 
 #include "hadb.h"
 
-int hip_hadb_get_peer_addr_info_old(hip_ha_t *entry, const struct in6_addr 
*addr,
-                               uint32_t *lifetime, struct timeval 
*modified_time);
-void hip_update_handle_ack_old(hip_ha_t *entry, struct hip_ack *ack, int 
have_esp_info);
+int hip_hadb_get_peer_addr_info_old(hip_ha_t *entry,
+                                    const struct in6_addr *addr,
+                                    uint32_t *lifetime,
+                                    struct timeval *modified_time);
+void hip_update_handle_ack_old(hip_ha_t *entry,
+                               struct hip_ack *ack,
+                               int have_esp_info);
 //add by santtu
-int hip_hadb_add_udp_addr_old(hip_ha_t *entry, struct in6_addr *addr,
-                            int is_bex_address, uint32_t lifetime,
-                            int is_preferred_addr,
-                            uint16_t port,
-                            uint32_t priority,
-                            uint8_t kind);
+int hip_hadb_add_udp_addr_old(hip_ha_t *entry,
+                              struct in6_addr *addr,
+                              int is_bex_address,
+                              uint32_t lifetime,
+                              int is_preferred_addr,
+                              uint16_t port,
+                              uint32_t priority,
+                              uint8_t kind);
 
 void hip_hadb_delete_peer_addrlist_one_old(hip_ha_t *ha, struct in6_addr 
*addr);
 
-
-
 #endif /* HADB_LEGACY_H */

=== modified file 'hipd/hadb_legacy.h.doxyme'
--- hipd/hadb_legacy.h.doxyme   2009-12-12 10:44:54 +0000
+++ hipd/hadb_legacy.h.doxyme   2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hadb_legacy.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/heartbeat.c'
--- hipd/heartbeat.c    2010-01-31 19:44:07 +0000
+++ hipd/heartbeat.c    2010-02-10 22:32:46 +0000
@@ -3,73 +3,78 @@
 
 int hip_handle_update_heartbeat_trigger(hip_ha_t *ha, void *unused)
 {
-        struct hip_locator_info_addr_item *locators;
-        hip_common_t *locator_msg = NULL;
-       int err = 0;
-
-        if (!(ha->hastate == HIP_HASTATE_HITOK &&
-             ha->state == HIP_STATE_ESTABLISHED &&
-             ha->disable_sas == 0))
-               goto out_err;
-
-       ha->update_trigger_on_heartbeat_counter++;
-       _HIP_DEBUG("Trigger count %d/%d\n", 
ha->update_trigger_on_heartbeat_counter,
-                 HIP_ADDRESS_CHANGE_HB_COUNT_TRIGGER * hip_icmp_interval);
-
-       if (ha->update_trigger_on_heartbeat_counter <
-           HIP_ADDRESS_CHANGE_HB_COUNT_TRIGGER * hip_icmp_interval)
-               goto out_err;
-
-       /* Time to try a handover because heart beats have been failing.
-          Let's also reset to counter to avoid UPDATE looping in case e.g.
-          there is just no connectivity at all. */
-       ha->update_trigger_on_heartbeat_counter = 0;
-
-       HIP_DEBUG("Hearbeat counter with ha expired, trigger UPDATE\n");
-
-        HIP_IFEL(!(locator_msg = hip_msg_alloc()), -ENOMEM,
-            "Out of memory while allocation memory for the packet\n");
-        HIP_IFE(hip_create_locators(locator_msg, &locators), -1);
-
-       HIP_IFEL(hip_send_locators_to_one_peer(NULL, ha, &ha->our_addr,
-                                            &ha->peer_addr, locators, 
HIP_UPDATE_LOCATOR),
-                -1, "Failed to trigger update\n");
-                
-       ha->update_trigger_on_heartbeat_counter = 0;
+    struct hip_locator_info_addr_item *locators;
+    hip_common_t *locator_msg = NULL;
+    int err                   = 0;
+
+    if (!(ha->hastate == HIP_HASTATE_HITOK &&
+          ha->state == HIP_STATE_ESTABLISHED &&
+          ha->disable_sas == 0)) {
+        goto out_err;
+    }
+
+    ha->update_trigger_on_heartbeat_counter++;
+    _HIP_DEBUG("Trigger count %d/%d\n", 
ha->update_trigger_on_heartbeat_counter,
+               HIP_ADDRESS_CHANGE_HB_COUNT_TRIGGER * hip_icmp_interval);
+
+    if (ha->update_trigger_on_heartbeat_counter <
+        HIP_ADDRESS_CHANGE_HB_COUNT_TRIGGER * hip_icmp_interval) {
+        goto out_err;
+    }
+
+    /* Time to try a handover because heart beats have been failing.
+     * Let's also reset to counter to avoid UPDATE looping in case e.g.
+     * there is just no connectivity at all. */
+    ha->update_trigger_on_heartbeat_counter = 0;
+
+    HIP_DEBUG("Hearbeat counter with ha expired, trigger UPDATE\n");
+
+    HIP_IFEL(!(locator_msg = hip_msg_alloc()), -ENOMEM,
+             "Out of memory while allocation memory for the packet\n");
+    HIP_IFE(hip_create_locators(locator_msg, &locators), -1);
+
+    HIP_IFEL(hip_send_locators_to_one_peer(NULL, ha, &ha->our_addr,
+                                           &ha->peer_addr, locators,
+                                           HIP_UPDATE_LOCATOR),
+             -1, "Failed to trigger update\n");
+
+    ha->update_trigger_on_heartbeat_counter = 0;
 
 out_err:
-       if (locator_msg)
-               free(locator_msg);
-         
-       return err;
+    if (locator_msg) {
+        free(locator_msg);
+    }
+
+    return err;
 }
 
-/** 
+/**
  * This function goes through the HA database and sends an icmp echo to all of 
them
  *
  * @param socket to send with
  *
  * @return 0 on success negative on error
  */
-int hip_send_heartbeat(hip_ha_t *entry, void *opaq) {
-       int err = 0;
-       int *sockfd = (int *) opaq;
+int hip_send_heartbeat(hip_ha_t *entry, void *opaq)
+{
+    int err     = 0;
+    int *sockfd = (int *) opaq;
 
-       if (entry->state == HIP_STATE_ESTABLISHED) {
-           if (entry->outbound_sa_count > 0) {
-                   _HIP_DEBUG("list_for_each_safe\n");
-                   HIP_IFEL(hip_send_icmp(*sockfd, entry), 0,
-                            "Error sending heartbeat, ignore\n");
-           } else {
-                   /* This can occur when ESP transform is not negotiated
-                      with e.g. a HIP Relay or Rendezvous server */
-                   HIP_DEBUG("No SA, sending NOTIFY instead of ICMPv6\n");
-                   err = hip_nat_send_keep_alive(entry, NULL);
-           }
+    if (entry->state == HIP_STATE_ESTABLISHED) {
+        if (entry->outbound_sa_count > 0) {
+            _HIP_DEBUG("list_for_each_safe\n");
+            HIP_IFEL(hip_send_icmp(*sockfd, entry), 0,
+                     "Error sending heartbeat, ignore\n");
+        } else {
+            /* This can occur when ESP transform is not negotiated
+             * with e.g. a HIP Relay or Rendezvous server */
+            HIP_DEBUG("No SA, sending NOTIFY instead of ICMPv6\n");
+            err = hip_nat_send_keep_alive(entry, NULL);
         }
+    }
 
 out_err:
-       return err;
+    return err;
 }
 
 /**
@@ -81,110 +86,119 @@
  *
  * @note see RFC2292
  */
-int hip_icmp_recvmsg(int sockfd) {
-       int err = 0, ret = 0, identifier = 0;
-       struct msghdr mhdr;
-       struct cmsghdr * chdr;
-       struct iovec iov[1];
-       u_char cmsgbuf[CMSG_SPACE(sizeof(struct inet6_pktinfo))];
-       u_char iovbuf[HIP_MAX_ICMP_PACKET];
-       struct icmp6hdr * icmph = NULL;
-       struct inet6_pktinfo * pktinfo;
-       struct sockaddr_in6 src_sin6;
-       struct in6_addr * src = NULL, * dst = NULL;
-       struct timeval * stval = NULL, * rtval = NULL, * ptr = NULL;
-
-       /* malloc what you need */
-       stval = malloc(sizeof(struct timeval));
-       HIP_IFEL((!stval), -1, "Malloc for stval failed\n");
-       rtval = malloc(sizeof(struct timeval));
-       HIP_IFEL((!rtval), -1, "Malloc for rtval failed\n");
-       src = malloc(sizeof(struct in6_addr));
-       HIP_IFEL((!src), -1, "Malloc for dst6 failed\n");
-       dst = malloc(sizeof(struct in6_addr));
-       HIP_IFEL((!dst), -1, "Malloc for dst failed\n");
-
-       /* cast */
-       chdr = (struct cmsghdr *)(void*)cmsgbuf;
-       pktinfo = (struct inet6_pktinfo *)(void*)(CMSG_DATA(chdr));
-
-       /* clear memory */
-       memset(stval, 0, sizeof(struct timeval));
-       memset(rtval, 0, sizeof(struct timeval));
-       memset(src, 0, sizeof(struct in6_addr));
-       memset(dst, 0, sizeof(struct in6_addr));
-       memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
-       memset(&iov, 0, sizeof(&iov));
-       memset(&iovbuf, 0, sizeof(iovbuf));
-       memset(&mhdr, 0, sizeof(mhdr));
-
-       /* receive control msg */
-        chdr->cmsg_level = IPPROTO_IPV6;
-       chdr->cmsg_type = IPV6_2292PKTINFO;
-       chdr->cmsg_len = CMSG_LEN (sizeof (struct inet6_pktinfo));
-
-       /* Input output buffer */
-       iov[0].iov_base = &iovbuf;
-       iov[0].iov_len = sizeof(iovbuf);
-
-       /* receive msg hdr */
-       mhdr.msg_iov = &(iov[0]);
-       mhdr.msg_iovlen = 1;
-       mhdr.msg_name = (caddr_t) &src_sin6;
-       mhdr.msg_namelen = sizeof (struct sockaddr_in6);
-       mhdr.msg_control = (caddr_t) cmsgbuf;
-       mhdr.msg_controllen = sizeof (cmsgbuf);
-
-       ret = recvmsg (sockfd, &mhdr, MSG_DONTWAIT);
-       _HIP_PERROR("RECVMSG ");
-       if (errno == EAGAIN) {
-               err = 0;
-               _HIP_DEBUG("Asynchronous, maybe next time\n");
-               goto out_err;
-       }
-       if (ret < 0) {
-               HIP_DEBUG("Recvmsg on ICMPv6 failed\n");
-               err = -1;
-               goto out_err;
-       }
-
-       /* Get the current time as the return time */
-       gettimeofday(rtval, (struct timezone *)NULL);
-
-       /* Check if the process identifier is ours and that this really is echo 
response */
-       icmph = (struct icmp6hdr *)(void*) iovbuf;
-       if (icmph->icmp6_type != ICMPV6_ECHO_REPLY) {
-               err = 0;
-               goto out_err;
-       }
-       identifier = getpid() & 0xFFFF;
-       if (identifier != icmph->icmp6_identifier) {
-               err = 0;
-               goto out_err;
-       }
-
-       /* Get the timestamp as the sent time*/
-       ptr = (struct timeval *)(icmph + 1);
-       memcpy(stval, ptr, sizeof(struct timeval));
-
-       /* gather addresses */
-       memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
-       memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
-
-       if (!ipv6_addr_is_hit(src) && !ipv6_addr_is_hit(dst)) {
-           HIP_DEBUG("Addresses are NOT HITs, this msg is not for us\n");
-       }
-
-       /* Calculate and store everything into the correct entry */
-       HIP_IFEL(hip_icmp_statistics(src, dst, stval, rtval), -1,
-                "Failed to calculate the statistics and store the values\n");
+int hip_icmp_recvmsg(int sockfd)
+{
+    int err = 0, ret = 0, identifier = 0;
+    struct msghdr mhdr;
+    struct cmsghdr *chdr;
+    struct iovec iov[1];
+    u_char cmsgbuf[CMSG_SPACE(sizeof(struct inet6_pktinfo))];
+    u_char iovbuf[HIP_MAX_ICMP_PACKET];
+    struct icmp6hdr *icmph = NULL;
+    struct inet6_pktinfo *pktinfo;
+    struct sockaddr_in6 src_sin6;
+    struct in6_addr *src   = NULL, *dst = NULL;
+    struct timeval *stval  = NULL, *rtval = NULL, *ptr = NULL;
+
+    /* malloc what you need */
+    stval   = malloc(sizeof(struct timeval));
+    HIP_IFEL((!stval), -1, "Malloc for stval failed\n");
+    rtval   = malloc(sizeof(struct timeval));
+    HIP_IFEL((!rtval), -1, "Malloc for rtval failed\n");
+    src     = malloc(sizeof(struct in6_addr));
+    HIP_IFEL((!src), -1, "Malloc for dst6 failed\n");
+    dst     = malloc(sizeof(struct in6_addr));
+    HIP_IFEL((!dst), -1, "Malloc for dst failed\n");
+
+    /* cast */
+    chdr    = (struct cmsghdr *) (void *) cmsgbuf;
+    pktinfo = (struct inet6_pktinfo *) (void *) (CMSG_DATA(chdr));
+
+    /* clear memory */
+    memset(stval, 0, sizeof(struct timeval));
+    memset(rtval, 0, sizeof(struct timeval));
+    memset(src, 0, sizeof(struct in6_addr));
+    memset(dst, 0, sizeof(struct in6_addr));
+    memset(&src_sin6, 0, sizeof(struct sockaddr_in6));
+    memset(&iov, 0, sizeof(&iov));
+    memset(&iovbuf, 0, sizeof(iovbuf));
+    memset(&mhdr, 0, sizeof(mhdr));
+
+    /* receive control msg */
+    chdr->cmsg_level    = IPPROTO_IPV6;
+    chdr->cmsg_type     = IPV6_2292PKTINFO;
+    chdr->cmsg_len      = CMSG_LEN(sizeof(struct inet6_pktinfo));
+
+    /* Input output buffer */
+    iov[0].iov_base     = &iovbuf;
+    iov[0].iov_len      = sizeof(iovbuf);
+
+    /* receive msg hdr */
+    mhdr.msg_iov        = &(iov[0]);
+    mhdr.msg_iovlen     = 1;
+    mhdr.msg_name       = (caddr_t) &src_sin6;
+    mhdr.msg_namelen    = sizeof(struct sockaddr_in6);
+    mhdr.msg_control    = (caddr_t) cmsgbuf;
+    mhdr.msg_controllen = sizeof(cmsgbuf);
+
+    ret                 = recvmsg(sockfd, &mhdr, MSG_DONTWAIT);
+    _HIP_PERROR("RECVMSG ");
+    if (errno == EAGAIN) {
+        err = 0;
+        _HIP_DEBUG("Asynchronous, maybe next time\n");
+        goto out_err;
+    }
+    if (ret < 0) {
+        HIP_DEBUG("Recvmsg on ICMPv6 failed\n");
+        err = -1;
+        goto out_err;
+    }
+
+    /* Get the current time as the return time */
+    gettimeofday(rtval, (struct timezone *) NULL);
+
+    /* Check if the process identifier is ours and that this really is echo 
response */
+    icmph = (struct icmp6hdr *) (void *) iovbuf;
+    if (icmph->icmp6_type != ICMPV6_ECHO_REPLY) {
+        err = 0;
+        goto out_err;
+    }
+    identifier = getpid() & 0xFFFF;
+    if (identifier != icmph->icmp6_identifier) {
+        err = 0;
+        goto out_err;
+    }
+
+    /* Get the timestamp as the sent time*/
+    ptr = (struct timeval *) (icmph + 1);
+    memcpy(stval, ptr, sizeof(struct timeval));
+
+    /* gather addresses */
+    memcpy(src, &src_sin6.sin6_addr, sizeof(struct in6_addr));
+    memcpy(dst, &pktinfo->ipi6_addr, sizeof(struct in6_addr));
+
+    if (!ipv6_addr_is_hit(src) && !ipv6_addr_is_hit(dst)) {
+        HIP_DEBUG("Addresses are NOT HITs, this msg is not for us\n");
+    }
+
+    /* Calculate and store everything into the correct entry */
+    HIP_IFEL(hip_icmp_statistics(src, dst, stval, rtval), -1,
+             "Failed to calculate the statistics and store the values\n");
 
 out_err:
-       
-       if (stval) free(stval);
-       if (rtval) free(rtval);
-       if (src) free(src);
-       if (dst) free(dst);
-       
-       return err;
+
+    if (stval) {
+        free(stval);
+    }
+    if (rtval) {
+        free(rtval);
+    }
+    if (src) {
+        free(src);
+    }
+    if (dst) {
+        free(dst);
+    }
+
+    return err;
 }

=== modified file 'hipd/hi3.c'
--- hipd/hi3.c  2010-01-06 22:18:26 +0000
+++ hipd/hi3.c  2010-02-10 22:32:46 +0000
@@ -3,273 +3,269 @@
 
 #define HI3_TRIGGER_MAX 10
 
-cl_trigger* hi3_pri_tr[HI3_TRIGGER_MAX];
-cl_trigger* hi3_pub_tr[HI3_TRIGGER_MAX];
+cl_trigger *hi3_pri_tr[HI3_TRIGGER_MAX];
+cl_trigger *hi3_pub_tr[HI3_TRIGGER_MAX];
 
 ID hi3_pri_id[HI3_TRIGGER_MAX];
 ID hi3_pub_id[HI3_TRIGGER_MAX];
-int hi3_pub_tr_count = 0;
-cl_trigger* cl_pub_tr_set = NULL;
+int hi3_pub_tr_count      = 0;
+cl_trigger *cl_pub_tr_set = NULL;
 
-static int hip_hi3_add_pub_trigger_id(struct hip_host_id_entry *entry, void* 
count);
+static int hip_hi3_add_pub_trigger_id(struct hip_host_id_entry *entry, void 
*count);
 static int hip_hi3_insert_trigger(void);
 
 /**
  * The callback for i3 "no matching id" callback.
- * 
+ *
  * @param ctx_data a pointer to...
  * @param data     a pointer to...
  * @param fun_ctx  a pointer to...
  * @todo           tkoponen: should this somehow trigger the timeout for 
waiting
  *                 outbound traffic (state machine)?
  */
-static void no_matching_trigger(void *ctx_data, void *data, void *fun_ctx){
-       char id[100];
-       sprintf_i3_id(id, (ID *)ctx_data);
-       
-       HIP_ERROR("Following ID not found: %s\n", id);
-}
-
-
-int hip_i3_init(){
-       if( cl_init(HIPL_HI3_FILE)!= CL_RET_OK){
-               HIP_ERROR("hi3: error creating context!\n");
-               exit(-1);
-       }
-       
-       cl_register_callback(CL_CBK_TRIGGER_NOT_FOUND, no_matching_trigger, 
NULL);
-
-       hip_hi3_insert_trigger();
-
-
-       return 0;
-}
-
-
-static int hip_hi3_add_pub_trigger_id(struct hip_host_id_entry *entry, void* 
count){
-       int i = *(int*)count;
-       if( i > HI3_TRIGGER_MAX ){
-               HIP_ERROR("Trigger number exceeded");
-               return 0;
-       }
-
-       bzero(&hi3_pub_id[i], ID_LEN);
-       memcpy(&hi3_pub_id[i], &entry->lhi.hit, sizeof(hip_hit_t));
-       (*((int*)count))++;
-
-       return 0;
-}
-
+static void no_matching_trigger(void *ctx_data, void *data, void *fun_ctx)
+{
+    char id[100];
+    sprintf_i3_id(id, (ID *) ctx_data);
+
+    HIP_ERROR("Following ID not found: %s\n", id);
+}
+
+int hip_i3_init()
+{
+    if (cl_init(HIPL_HI3_FILE) != CL_RET_OK) {
+        HIP_ERROR("hi3: error creating context!\n");
+        exit(-1);
+    }
+
+    cl_register_callback(CL_CBK_TRIGGER_NOT_FOUND, no_matching_trigger, NULL);
+
+    hip_hi3_insert_trigger();
+
+
+    return 0;
+}
+
+static int hip_hi3_add_pub_trigger_id(struct hip_host_id_entry *entry, void 
*count)
+{
+    int i = *(int *) count;
+    if (i > HI3_TRIGGER_MAX) {
+        HIP_ERROR("Trigger number exceeded");
+        return 0;
+    }
+
+    bzero(&hi3_pub_id[i], ID_LEN);
+    memcpy(&hi3_pub_id[i], &entry->lhi.hit, sizeof(hip_hit_t));
+    (*((int *) count))++;
+
+    return 0;
+}
 
 /**
  * This is the i3 callback to process received data.
  */
-static void hip_hi3_receive_payload(void *t, void* data, void *fun_ctx)
-{ 
-       struct hip_common *hip_common;
-       //      struct hip_work_order *hwo;
-       //      struct sockaddr_in6 src, dst;
-       //      struct hi3_ipv4_addr *h4;
-       //      struct hi3_ipv6_addr *h6;
-       //      int family, l, type;
-       cl_buf* clb = (cl_buf *)data;
-       char *buf = clb->data;
-       int len = clb->data_len;
-       hip_portpair_t msg_info;
-
-       /* See if there is at least the HIP header in the packet */
-        if(len < sizeof(struct hip_common)){
-               HIP_ERROR("Received packet too small. Dropping\n");
-               goto out_err;
-       }
-       
-       hip_common = (struct hip_common*)buf;
-       HIP_DEBUG("Received HIP packet type %d\n", hip_common->type_hdr);
-       _HIP_HEXDUMP("HIP PACKET", hip_common,
-                    hip_get_msg_total_len(hip_common));
-
-       /*        if (hip_verify_network_header(hip_common, 
-                                     (struct sockaddr *)&src, 
-                                     (struct sockaddr *)&dst,
-                                     len)) {
-               HIP_ERROR("Verifying of the network header failed\n");
-               goto out_err;
-               }*/
-
-       if(hip_check_network_msg(hip_common)){
-               HIP_ERROR("HIP packet is invalid\n");
-               goto out_err;
-       }
-       
-       memset(&msg_info, 0, sizeof(msg_info));
-       msg_info.hi3_in_use = 1;
-
-       struct in6_addr lpback1 = IN6ADDR_LOOPBACK_INIT;
-       struct in6_addr lpback2 = IN6ADDR_LOOPBACK_INIT;
-
-       if(hip_receive_control_packet(hip_common, &lpback1 , &lpback2, 
//hip_cast_sa_addr(&src), hip_cast_sa_addr(&dst),
-                                      &msg_info, 0)){
-               HIP_ERROR("HIP packet processsing failed\n");
-               goto out_err;
-       }
-
- out_err:
-       //cl_free_buf(clb);
-       ;
+static void hip_hi3_receive_payload(void *t, void *data, void *fun_ctx)
+{
+    struct hip_common *hip_common;
+    // struct hip_work_order *hwo;
+    // struct sockaddr_in6 src, dst;
+    // struct hi3_ipv4_addr *h4;
+    // struct hi3_ipv6_addr *h6;
+    // int family, l, type;
+    cl_buf *clb = (cl_buf *) data;
+    char *buf   = clb->data;
+    int len     = clb->data_len;
+    hip_portpair_t msg_info;
+
+    /* See if there is at least the HIP header in the packet */
+    if (len < sizeof(struct hip_common)) {
+        HIP_ERROR("Received packet too small. Dropping\n");
+        goto out_err;
+    }
+
+    hip_common = (struct hip_common *) buf;
+    HIP_DEBUG("Received HIP packet type %d\n", hip_common->type_hdr);
+    _HIP_HEXDUMP("HIP PACKET", hip_common,
+                 hip_get_msg_total_len(hip_common));
+
+    /*        if (hip_verify_network_header(hip_common,
+     *                            (struct sockaddr *)&src,
+     *                            (struct sockaddr *)&dst,
+     *                            len)) {
+     *      HIP_ERROR("Verifying of the network header failed\n");
+     *      goto out_err;
+     *      }*/
+
+    if (hip_check_network_msg(hip_common)) {
+        HIP_ERROR("HIP packet is invalid\n");
+        goto out_err;
+    }
+
+    memset(&msg_info, 0, sizeof(msg_info));
+    msg_info.hi3_in_use = 1;
+
+    struct in6_addr lpback1 = IN6ADDR_LOOPBACK_INIT;
+    struct in6_addr lpback2 = IN6ADDR_LOOPBACK_INIT;
+
+    if (hip_receive_control_packet(hip_common, &lpback1, &lpback2, &msg_info, 
0)) {
+        HIP_ERROR("HIP packet processsing failed\n");
+        goto out_err;
+    }
+
+out_err:
+    //cl_free_buf(clb);
+    ;
 }
 
-
-/* 
+/*
  * i3 callbacks for trigger management
  */
-static void hip_hi3_constraint_failed(void *t, void *data, void *fun_ctx){
-       /* This should never occur if the infrastructure works */
-       HIP_ERROR("Trigger constraint failed\n");
-}
-
-
-static void hip_hi3_trigger_inserted(void *t, void *data, void *fun_ctx){
-       char id[100];
-       cl_trigger *type = (cl_trigger *)t;
-
-       sprintf_i3_id(id, &type->t->id);
-       
-       HIP_ERROR("Trigger inserted: %s\n", id);
-}
-
-
-static void hip_hi3_trigger_failure(void *t, void *data, void *fun_ctx) {
-       cl_trigger *type = (cl_trigger *)t;
-
-       /* FIXME: A small delay before trying again? */
-       HIP_ERROR("Trigger failed, reinserting...\n");
-       
-       /* Reinsert trigger */
-       cl_insert_trigger(type, 0);
-}
-
-
-static int hip_hi3_insert_trigger(void){
-       Key key[HI3_TRIGGER_MAX];
-       int i;
+static void hip_hi3_constraint_failed(void *t, void *data, void *fun_ctx)
+{
+    /* This should never occur if the infrastructure works */
+    HIP_ERROR("Trigger constraint failed\n");
+}
+
+static void hip_hi3_trigger_inserted(void *t, void *data, void *fun_ctx)
+{
+    char id[100];
+    cl_trigger *type = (cl_trigger *) t;
+
+    sprintf_i3_id(id, &type->t->id);
+
+    HIP_ERROR("Trigger inserted: %s\n", id);
+}
+
+static void hip_hi3_trigger_failure(void *t, void *data, void *fun_ctx)
+{
+    cl_trigger *type = (cl_trigger *) t;
+
+    /* FIXME: A small delay before trying again? */
+    HIP_ERROR("Trigger failed, reinserting...\n");
+
+    /* Reinsert trigger */
+    cl_insert_trigger(type, 0);
+}
+
+static int hip_hi3_insert_trigger(void)
+{
+    Key key[HI3_TRIGGER_MAX];
+    int i;
 //     hip_hit_t peer_hit;
 
-       //      hip_get_default_hit(&peer_hit);
-       //      hip_i3_init(/*&peer_hit*/);
-       //      hi3_pub_tr_count = 1;
-       //      memcpy(&hi3_pub_id[0], &peer_hit, sizeof(hip_hit_t));
-       hip_for_each_hi(hip_hi3_add_pub_trigger_id, &hi3_pub_tr_count );
+    // hip_get_default_hit(&peer_hit);
+    // hip_i3_init(/*&peer_hit*/);
+    // hi3_pub_tr_count = 1;
+    // memcpy(&hi3_pub_id[0], &peer_hit, sizeof(hip_hit_t));
+    hip_for_each_hi(hip_hi3_add_pub_trigger_id, &hi3_pub_tr_count );
 
-       for( i=0; i<hi3_pub_tr_count; i++ ){
-               get_random_bytes(hi3_pri_id[i].x, ID_LEN);      
+    for (i = 0; i < hi3_pub_tr_count; i++) {
+        get_random_bytes(hi3_pri_id[i].x, ID_LEN);
 //             get_random_bytes(key.x, KEY_LEN);
 
-               hi3_pub_tr[i] = cl_create_trigger_id(&hi3_pub_id[i], 
ID_LEN_BITS, &hi3_pri_id[i],
-                                                 
CL_TRIGGER_CFLAG_R_CONSTRAINT);
+        hi3_pub_tr[i] = cl_create_trigger_id(&hi3_pub_id[i], ID_LEN_BITS, 
&hi3_pri_id[i],
+                                             CL_TRIGGER_CFLAG_R_CONSTRAINT);
 //                             CL_TRIGGER_CFLAG_L_CONSTRAINT |
 //                             CL_TRIGGER_CFLAG_PUBLIC);
 
-               cl_register_trigger_callback(hi3_pub_tr[i], 
CL_CBK_TRIGGER_CONSTRAINT_FAILED,
-                                            hip_hi3_constraint_failed, NULL);
-               cl_register_trigger_callback(hi3_pub_tr[i], 
CL_CBK_RECEIVE_PAYLOAD,
-                                            hip_hi3_receive_payload, NULL);
-               cl_register_trigger_callback(hi3_pub_tr[i], 
CL_CBK_TRIGGER_INSERTED,
-                                            hip_hi3_trigger_inserted, NULL);
-               cl_register_trigger_callback(hi3_pub_tr[i], 
CL_CBK_TRIGGER_REFRESH_FAILED,
-                                            hip_hi3_trigger_failure, NULL);
-
-
-               hi3_pri_tr[i]  = cl_create_trigger(&hi3_pri_id[i], ID_LEN_BITS, 
&key[i],
-                                               CL_TRIGGER_CFLAG_R_CONSTRAINT);
-
-
-               /* associate callbacks with the inserted trigger */
-               cl_register_trigger_callback(hi3_pri_tr[i], 
CL_CBK_TRIGGER_CONSTRAINT_FAILED,
-                                            hip_hi3_constraint_failed, NULL);
-               cl_register_trigger_callback(hi3_pri_tr[i], 
CL_CBK_RECEIVE_PAYLOAD,
-                                            hip_hi3_receive_payload, NULL);
-               cl_register_trigger_callback(hi3_pri_tr[i], 
CL_CBK_TRIGGER_INSERTED,
-                                            hip_hi3_trigger_inserted, NULL);
-               cl_register_trigger_callback(hi3_pri_tr[i], 
CL_CBK_TRIGGER_REFRESH_FAILED,
-                                            hip_hi3_trigger_failure, NULL);
-       }
-       /* Insert triggers */
-       for(i=0; i<hi3_pub_tr_count; i++){
-               cl_insert_trigger(hi3_pri_tr[i], 0);
-               cl_insert_trigger(hi3_pub_tr[i], 0);
-       }
-
-       return 0;
-
-}
-
-
-int hip_hi3_clean(){
-       int i=0;
-       for(i=0; i<hi3_pub_tr_count; i++){
-               cl_destroy_trigger(hi3_pub_tr[i]);
-               cl_destroy_trigger(hi3_pri_tr[i]);
-       }
-       hi3_pub_tr_count = 0;
-
-       cl_exit();
-
-       return 0;
-}
-
+        cl_register_trigger_callback(hi3_pub_tr[i], 
CL_CBK_TRIGGER_CONSTRAINT_FAILED,
+                                     hip_hi3_constraint_failed, NULL);
+        cl_register_trigger_callback(hi3_pub_tr[i], CL_CBK_RECEIVE_PAYLOAD,
+                                     hip_hi3_receive_payload, NULL);
+        cl_register_trigger_callback(hi3_pub_tr[i], CL_CBK_TRIGGER_INSERTED,
+                                     hip_hi3_trigger_inserted, NULL);
+        cl_register_trigger_callback(hi3_pub_tr[i], 
CL_CBK_TRIGGER_REFRESH_FAILED,
+                                     hip_hi3_trigger_failure, NULL);
+
+
+        hi3_pri_tr[i] = cl_create_trigger(&hi3_pri_id[i], ID_LEN_BITS, &key[i],
+                                          CL_TRIGGER_CFLAG_R_CONSTRAINT);
+
+
+        /* associate callbacks with the inserted trigger */
+        cl_register_trigger_callback(hi3_pri_tr[i], 
CL_CBK_TRIGGER_CONSTRAINT_FAILED,
+                                     hip_hi3_constraint_failed, NULL);
+        cl_register_trigger_callback(hi3_pri_tr[i], CL_CBK_RECEIVE_PAYLOAD,
+                                     hip_hi3_receive_payload, NULL);
+        cl_register_trigger_callback(hi3_pri_tr[i], CL_CBK_TRIGGER_INSERTED,
+                                     hip_hi3_trigger_inserted, NULL);
+        cl_register_trigger_callback(hi3_pri_tr[i], 
CL_CBK_TRIGGER_REFRESH_FAILED,
+                                     hip_hi3_trigger_failure, NULL);
+    }
+    /* Insert triggers */
+    for (i = 0; i < hi3_pub_tr_count; i++) {
+        cl_insert_trigger(hi3_pri_tr[i], 0);
+        cl_insert_trigger(hi3_pub_tr[i], 0);
+    }
+
+    return 0;
+}
+
+int hip_hi3_clean()
+{
+    int i = 0;
+    for (i = 0; i < hi3_pub_tr_count; i++) {
+        cl_destroy_trigger(hi3_pub_tr[i]);
+        cl_destroy_trigger(hi3_pri_tr[i]);
+    }
+    hi3_pub_tr_count = 0;
+
+    cl_exit();
+
+    return 0;
+}
 
 int hip_do_i3_stuff_for_i2(struct hip_locator *locator, hip_portpair_t 
*i2_info,
-                          in6_addr_t *i2_saddr, in6_addr_t *i2_daddr){
-       int n_addrs = 0, ii = 0, use_ip4 = 1;
-       struct hip_locator_info_addr_item *first = NULL;
-       struct netdev_address *n = NULL;
-       hip_list_t *item = NULL, *tmp = NULL;
-
-       if(locator == NULL){
-               return 0;
-       }
-       
-        if(locator){
-               n_addrs = hip_get_locator_addr_item_count(locator);
-               
-               if(i2_info->hi3_in_use && n_addrs > 0){
-                       
-                        first = (struct hip_locator_info_addr_item *)locator + 
sizeof(struct hip_locator);
-                        memcpy(i2_saddr, &first->address,
-                              sizeof(struct in6_addr));
-                       
-                        list_for_each_safe(item, tmp, addresses, ii){
-                               n = list_entry(item);
-                               
-                               if(ipv6_addr_is_hit(hip_cast_sa_addr((struct 
sockaddr *)&n->addr))){
-                                       continue;
-                               }
-                               if(!hip_sockaddr_is_v6_mapped((struct sockaddr 
*)&n->addr)){
-                                       memcpy(i2_daddr, 
hip_cast_sa_addr((struct sockaddr *)&n->addr),
-                                              hip_sa_addr_len(&n->addr));
-                                       ii = -1;
-                                       use_ip4 = 0;
-                                       break;
-                               }
-                       }
-                        if(use_ip4){
-                                list_for_each_safe(item, tmp, addresses, ii){
-                                       n = list_entry(item);
-                                       
-                                       
if(ipv6_addr_is_hit(hip_cast_sa_addr((struct sockaddr *)&n->addr))){
-                                               continue;
-                                       }
-                                       if(hip_sockaddr_is_v6_mapped((struct 
sockaddr *)&n->addr)){
-                                               memcpy(i2_daddr, 
hip_cast_sa_addr((struct sockaddr *)&n->addr),
-                                                      
hip_sa_addr_len(&n->addr));
-                                               ii = -1;
-                                               break;
-                                       }
-                               }
-                        }
-               }
-       }
-
-       return 0;
+                           in6_addr_t *i2_saddr, in6_addr_t *i2_daddr)
+{
+    int n_addrs                              = 0, ii = 0, use_ip4 = 1;
+    struct hip_locator_info_addr_item *first = NULL;
+    struct netdev_address *n                 = NULL;
+    hip_list_t *item                         = NULL, *tmp = NULL;
+
+    if (locator == NULL) {
+        return 0;
+    }
+
+    if (locator) {
+        n_addrs = hip_get_locator_addr_item_count(locator);
+
+        if (i2_info->hi3_in_use && n_addrs > 0) {
+            first = (struct hip_locator_info_addr_item *) locator + 
sizeof(struct hip_locator);
+            memcpy(i2_saddr, &first->address,
+                   sizeof(struct in6_addr));
+
+            list_for_each_safe(item, tmp, addresses, ii) {
+                n = list_entry(item);
+
+                if (ipv6_addr_is_hit(hip_cast_sa_addr((struct sockaddr *) 
&n->addr))) {
+                    continue;
+                }
+                if (!hip_sockaddr_is_v6_mapped((struct sockaddr *) &n->addr)) {
+                    memcpy(i2_daddr, hip_cast_sa_addr((struct sockaddr *) 
&n->addr),
+                           hip_sa_addr_len(&n->addr));
+                    ii      = -1;
+                    use_ip4 = 0;
+                    break;
+                }
+            }
+            if (use_ip4) {
+                list_for_each_safe(item, tmp, addresses, ii) {
+                    n = list_entry(item);
+
+                    if (ipv6_addr_is_hit(hip_cast_sa_addr((struct sockaddr *) 
&n->addr))) {
+                        continue;
+                    }
+                    if (hip_sockaddr_is_v6_mapped((struct sockaddr *) 
&n->addr)) {
+                        memcpy(i2_daddr, hip_cast_sa_addr((struct sockaddr *) 
&n->addr),
+                               hip_sa_addr_len(&n->addr));
+                        ii = -1;
+                        break;
+                    }
+                }
+            }
+        }
+    }
+
+    return 0;
 }
-

=== modified file 'hipd/hi3.c.doxyme'
--- hipd/hi3.c.doxyme   2009-12-12 10:44:54 +0000
+++ hipd/hi3.c.doxyme   2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,56 +45,55 @@
 
 
 /**
- * hip_do_i3_stuff_for_i2 
+ * hip_do_i3_stuff_for_i2
  *
  *
  * @param locator
  * @param i2_info
  * @param i2_saddr
  * @param i2_daddr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hi3_add_pub_trigger_id 
+ * hip_hi3_add_pub_trigger_id
  *
  *
  * @param entry
  * @param count
- * @return 
- **/
-
-
-/**
- * hip_hi3_constraint_failed 
- *
- *
- * @param test
- * @param data
- * @param fun_ctx
- * @return 
- **/
-
-
-/**
- * hip_hi3_receive_payload 
- *
- *
- * @param test
- * @param data
- * @param fun_ctx
- * @return 
- **/
-
-
-/**
- * hip_hi3_trigger_failure 
- *
- *
- * @param test
- * @param data
- * @param fun_ctx
- * @return 
- **/
-
+ * @return
+ **/
+
+
+/**
+ * hip_hi3_constraint_failed
+ *
+ *
+ * @param test
+ * @param data
+ * @param fun_ctx
+ * @return
+ **/
+
+
+/**
+ * hip_hi3_receive_payload
+ *
+ *
+ * @param test
+ * @param data
+ * @param fun_ctx
+ * @return
+ **/
+
+
+/**
+ * hip_hi3_trigger_failure
+ *
+ *
+ * @param test
+ * @param data
+ * @param fun_ctx
+ * @return
+ **/

=== modified file 'hipd/hi3.h'
--- hipd/hi3.h  2010-01-19 09:28:42 +0000
+++ hipd/hi3.h  2010-02-10 22:32:46 +0000
@@ -11,10 +11,10 @@
 #include "i3/i3_client/i3_client_api.h"
 #include "i3/i3/i3_id.h"
 
-#define HIPL_HI3_FILE     HIPL_SYSCONFDIR"/hi3_conf"
+#define HIPL_HI3_FILE     HIPL_SYSCONFDIR "/hi3_conf"
 
 #define HIPL_HI3_FILE_EX \
-"<?xml version=\"1.0\"?>\n\
+    "<?xml version=\"1.0\"?>\n\
 <I3ConfigFile version=\"0.1\">\n\
        <I3ServerDetails\n\
                UsePing=\"yes\"\n\
@@ -35,11 +35,11 @@
 
 /**
  * Does some i3 related stuff to I2 packet.
- * 
+ *
  * In an attempt to clean up the input.c code, this functionality was moved to
  * a function from hip_handle_i2(). What the code does, remains a mystery.
  * -Lauri 24.07.2008
- * 
+ *
  * @param locator  a pointer to a locator parameter. This is both a source and 
a
  *                 destination buffer.
  * @param i2_info  a pointer to a data structure that has information if i3 is
@@ -49,7 +49,6 @@
  * @param i2_daddr a pointer to I2 packet destination IP address.
  */
 int hip_do_i3_stuff_for_i2(struct hip_locator *locator, hip_portpair_t 
*i2_info,
-                          in6_addr_t *i2_saddr, in6_addr_t *i2_daddr);
+                           in6_addr_t *i2_saddr, in6_addr_t *i2_daddr);
 
 #endif /* HIP_HI3_H */
-

=== modified file 'hipd/hi3.h.doxyme'
--- hipd/hi3.h.doxyme   2009-12-12 10:44:54 +0000
+++ hipd/hi3.h.doxyme   2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hi3.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/hidb.c'
--- hipd/hidb.c 2010-02-08 15:18:06 +0000
+++ hipd/hidb.c 2010-02-10 22:32:46 +0000
@@ -1,4 +1,3 @@
-
 /**
  * @file
  * HIP host id database and accessors.
@@ -7,7 +6,7 @@
  * @author Miika Komu <miika#iki.fi>
  * @author Mika Kousa <mkousa#iki.fi>
  * @author Kristian Slavov <kslavov#hiit.fi>
- * @author Teresa Finez <tfinezmo#hut.tkk.fi> 
+ * @author Teresa Finez <tfinezmo#hut.tkk.fi>
  */
 #ifdef HAVE_CONFIG_H
   #include "config.h"
@@ -18,18 +17,19 @@
 HIP_HASHTABLE *hip_local_hostid_db = NULL;
 #define HIP_MAX_HOST_ID_LEN 1600
 
-static char *lsi_addresses[] = {"1.0.0.1","1.0.0.2","1.0.0.3","1.0.0.4"};
+static char *lsi_addresses[] = {"1.0.0.1", "1.0.0.2", "1.0.0.3", "1.0.0.4"};
 
 static int hip_add_host_id(hip_db_struct_t *db,
-                          const struct hip_lhi *lhi,
-                          hip_lsi_t *lsi,
-                          const struct hip_host_id_priv *host_id,
-                          int (*insert)(struct hip_host_id_entry *, void 
**arg), 
-                          int (*remove)(struct hip_host_id_entry *, void 
**arg),
-                          void *arg);
+                           const struct hip_lhi *lhi,
+                           hip_lsi_t *lsi,
+                           const struct hip_host_id_priv *host_id,
+                           int (*insert)(struct hip_host_id_entry *, void 
**arg),
+                           int (*remove)(struct hip_host_id_entry *, void 
**arg),
+                           void *arg);
 static struct hip_host_id *hip_get_public_key(const struct hip_host_id_priv 
*hid);
 static int hip_hidb_add_lsi(hip_db_struct_t *db, struct hip_host_id_entry 
*id_entry);
-static struct hip_host_id_entry *hip_hidb_get_entry_by_lsi(hip_db_struct_t 
*db, const struct in_addr *lsi);
+static struct hip_host_id_entry *hip_hidb_get_entry_by_lsi(hip_db_struct_t *db,
+                                                           const struct 
in_addr *lsi);
 
 
 /** @todo All get_any's should be removed (tkoponen). */
@@ -43,167 +43,172 @@
  */
 
 /* Do not access these databases directly: use the accessors in this file. */
-unsigned long hip_hidb_hash(const void *ptr) {
-       hip_hit_t *hit = &(((struct hip_host_id_entry *) ptr)->lhi.hit);
-        uint8_t hash[HIP_AH_SHA_LEN];
-
-       hip_build_digest(HIP_DIGEST_SHA1, hit, sizeof(hip_hit_t), hash);
-
-       return *((unsigned long *)(void*) hash);
-}
-
-int hip_hidb_match(const void *ptr1, const void *ptr2) {
-       return (hip_hidb_hash(ptr1) != hip_hidb_hash(ptr2));
-}
-
-void hip_init_hostid_db(hip_db_struct_t **db) {
-       hip_local_hostid_db = hip_ht_init(hip_hidb_hash, hip_hidb_match);
+unsigned long hip_hidb_hash(const void *ptr)
+{
+    hip_hit_t *hit = &(((struct hip_host_id_entry *) ptr)->lhi.hit);
+    uint8_t hash[HIP_AH_SHA_LEN];
+
+    hip_build_digest(HIP_DIGEST_SHA1, hit, sizeof(hip_hit_t), hash);
+
+    return *((unsigned long *) (void *) hash);
+}
+
+int hip_hidb_match(const void *ptr1, const void *ptr2)
+{
+    return hip_hidb_hash(ptr1) != hip_hidb_hash(ptr2);
+}
+
+void hip_init_hostid_db(hip_db_struct_t **db)
+{
+    hip_local_hostid_db = hip_ht_init(hip_hidb_hash, hip_hidb_match);
 }
 
 /**
  * Deletes the given HI (network byte order) from the database. Matches HIs
  * based on the HIT.
- * 
+ *
  * @param db  database from which to delete.
  * @param lhi the HIT to be deleted from the database.
  * @return    zero on success, otherwise negative.
  */
 static int hip_del_host_id(hip_db_struct_t *db, struct hip_lhi *lhi)
 {
-       int err = -ENOENT;
-       struct hip_host_id_entry *id = NULL;
-
-       HIP_ASSERT(lhi != NULL);
-
-       id = hip_get_hostid_entry_by_lhi_and_algo(db, &lhi->hit, HIP_ANY_ALGO, 
-1);
-       if (id == NULL) {
-               HIP_WRITE_UNLOCK_DB(db);
-               HIP_ERROR("lhi not found\n");
-               err = -ENOENT;
-               return err;
-       }
-
-       /* Call the handler to execute whatever required after the
-           host id is no more in the database */
-       if (id->remove) 
-               id->remove(id, &id->arg);
-
-       /* free the dynamically reserved memory and
-          set host_id to null to signal that it is free */
-       if (id->r1)
-               hip_uninit_r1(id->r1);
+    int err                      = -ENOENT;
+    struct hip_host_id_entry *id = NULL;
+
+    HIP_ASSERT(lhi != NULL);
+
+    id = hip_get_hostid_entry_by_lhi_and_algo(db, &lhi->hit, HIP_ANY_ALGO, -1);
+    if (id == NULL) {
+        HIP_WRITE_UNLOCK_DB(db);
+        HIP_ERROR("lhi not found\n");
+        err = -ENOENT;
+        return err;
+    }
+
+    /* Call the handler to execute whatever required after the
+     * host id is no more in the database */
+    if (id->remove) {
+        id->remove(id, &id->arg);
+    }
+
+    /* free the dynamically reserved memory and
+     * set host_id to null to signal that it is free */
+    if (id->r1) {
+        hip_uninit_r1(id->r1);
+    }
 #ifdef CONFIG_HIP_BLIND
-       if (id->blindr1)
-         hip_uninit_r1(id->blindr1);
+    if (id->blindr1) {
+        hip_uninit_r1(id->blindr1);
+    }
 #endif
 
-       if (hip_get_host_id_algo(id->host_id) == HIP_HI_RSA && id->private_key)
-               RSA_free(id->private_key);
-       else if (id->private_key)
-               DSA_free(id->private_key);
-
-       HIP_FREE(id->host_id);
-       list_del(id, db);
-       HIP_FREE(id);
-       id = NULL;
-
-       err = 0;
-       return err;
+    if (hip_get_host_id_algo(id->host_id) == HIP_HI_RSA && id->private_key) {
+        RSA_free(id->private_key);
+    } else if (id->private_key) {
+        DSA_free(id->private_key);
+    }
+
+    HIP_FREE(id->host_id);
+    list_del(id, db);
+    HIP_FREE(id);
+    id  = NULL;
+
+    err = 0;
+    return err;
 }
 
 /**
  * Uninitializes local/peer Host Id table. All elements of the @c db are
  * deleted. Since local and peer host id databases include dynamically 
allocated
  * host_id element, it is also freed.
- * 
- * @param db database structure to delete. 
+ *
+ * @param db database structure to delete.
  */
 static void hip_uninit_hostid_db(hip_db_struct_t *db)
 {
-       hip_list_t *curr, *iter;
-       struct hip_host_id_entry *tmp;
-       int count, err;
-
-       HIP_WRITE_LOCK_DB(db);
-
-       list_for_each_safe(curr, iter, db, count) {
-               struct hip_lhi lhi;
-
-               tmp = (struct hip_host_id_entry *)list_entry(curr);
-
-               memcpy(&lhi, &tmp->lhi, sizeof(lhi));
-               err = hip_del_host_id(db, &lhi);
-       }
-
-       hip_ht_uninit(db);
-
-       HIP_WRITE_UNLOCK_DB(db);
+    hip_list_t *curr, *iter;
+    struct hip_host_id_entry *tmp;
+    int count, err;
+
+    HIP_WRITE_LOCK_DB(db);
+
+    list_for_each_safe(curr, iter, db, count) {
+        struct hip_lhi lhi;
+
+        tmp = (struct hip_host_id_entry *) list_entry(curr);
+
+        memcpy(&lhi, &tmp->lhi, sizeof(lhi));
+        err = hip_del_host_id(db, &lhi);
+    }
+
+    hip_ht_uninit(db);
+
+    HIP_WRITE_UNLOCK_DB(db);
 }
 
 /**
  * Finds the host id corresponding to the given @c hit.
  *
- * If @c hit is null, finds the first used host id. 
+ * If @c hit is null, finds the first used host id.
  * If algo is HIP_ANY_ALGO, ignore algore comparison.
  *
  * @param db   database to be searched. Usually either HIP_DB_PEER_HID or
  *             HIP_DB_LOCAL_HID
- * @param hit  the local host id to be searched 
+ * @param hit  the local host id to be searched
  * @param anon -1 if you don't care, 1 if anon, 0 if public
  * @return     NULL, if failed or non-NULL if succeeded.
  */
-struct hip_host_id_entry *hip_get_hostid_entry_by_lhi_and_algo(
-     hip_db_struct_t *db, const struct in6_addr *hit, int algo, int anon)
-{
-       struct hip_host_id_entry *id_entry;
-       hip_list_t *item;
-       int c;
-       list_for_each(item, db, c) {
-               id_entry = (struct hip_host_id_entry *)list_entry(item);        
        
-               _HIP_DEBUG("ALGO VALUE :%d, algo value of id entry :%d\n",
-                          algo, hip_get_host_id_algo(id_entry->host_id));
-                _HIP_DEBUG_HIT("Comparing HIT", &id_entry->lhi.hit);
-                
-               if ((hit == NULL || !ipv6_addr_cmp(&id_entry->lhi.hit, hit)) &&
-                   (algo == HIP_ANY_ALGO ||
-                    (hip_get_host_id_algo(id_entry->host_id) == algo)) &&
-                   (anon == -1 || id_entry->lhi.anonymous == anon))
-                       return id_entry;
-       }
-       HIP_DEBUG("Failed to find a host ID entry, Returning NULL.\n");
-       return NULL;
-
-}
-
-int hip_hidb_hit_is_our(const hip_hit_t *our) {
-       return (hip_get_hostid_entry_by_lhi_and_algo(hip_local_hostid_db, our,
-                                                    HIP_ANY_ALGO, -1) != NULL);
-}
-
-
-int hip_hidb_get_lsi_by_hit(const hip_hit_t *our, hip_lsi_t *our_lsi){
-       struct hip_host_id_entry *id_entry;
-       hip_list_t *item;
-       int c, err = 1;
-
-       list_for_each(item, hip_local_hostid_db, c) {
-               id_entry = (struct hip_host_id_entry *)list_entry(item);
-               if (hip_hit_are_equal(&id_entry->lhi.hit, our)){
-                       memcpy(our_lsi, &id_entry->lsi, sizeof(hip_lsi_t));
-                       return 0;
-               }               
-       }
-       return err;
-}
-
+struct hip_host_id_entry *hip_get_hostid_entry_by_lhi_and_algo(hip_db_struct_t 
*db,
+                                                               const struct 
in6_addr *hit,
+                                                               int algo,
+                                                               int anon)
+{
+    struct hip_host_id_entry *id_entry;
+    hip_list_t *item;
+    int c;
+    list_for_each(item, db, c) {
+        id_entry = (struct hip_host_id_entry *) list_entry(item);
+        _HIP_DEBUG("ALGO VALUE :%d, algo value of id entry :%d\n",
+                   algo, hip_get_host_id_algo(id_entry->host_id));
+        _HIP_DEBUG_HIT("Comparing HIT", &id_entry->lhi.hit);
+
+        if ((hit == NULL || !ipv6_addr_cmp(&id_entry->lhi.hit, hit)) &&
+            (algo == HIP_ANY_ALGO ||
+             (hip_get_host_id_algo(id_entry->host_id) == algo)) &&
+            (anon == -1 || id_entry->lhi.anonymous == anon)) {
+            return id_entry;
+        }
+    }
+    HIP_DEBUG("Failed to find a host ID entry, Returning NULL.\n");
+    return NULL;
+}
+
+int hip_hidb_hit_is_our(const hip_hit_t *our)
+{
+    return hip_get_hostid_entry_by_lhi_and_algo(hip_local_hostid_db, our,
+                                                HIP_ANY_ALGO, -1) != NULL;
+}
+
+int hip_hidb_get_lsi_by_hit(const hip_hit_t *our, hip_lsi_t *our_lsi)
+{
+    struct hip_host_id_entry *id_entry;
+    hip_list_t *item;
+    int c, err = 1;
+
+    list_for_each(item, hip_local_hostid_db, c) {
+        id_entry = (struct hip_host_id_entry *) list_entry(item);
+        if (hip_hit_are_equal(&id_entry->lhi.hit, our)) {
+            memcpy(our_lsi, &id_entry->lsi, sizeof(hip_lsi_t));
+            return 0;
+        }
+    }
+    return err;
+}
 
 /*
- *
- *
  * Interface functions to access databases.
  *
- *
- *
  */
 
 /***
@@ -223,14 +228,13 @@
  */
 void hip_uninit_host_id_dbs(void)
 {
-       hip_uninit_hostid_db(hip_local_hostid_db);
+    hip_uninit_hostid_db(hip_local_hostid_db);
 }
 
-
 /**
  * Adds the given HI into the database. Checks for duplicates. If one is found,
  * the current HI is @b NOT stored.
- * 
+ *
  * @param db      database structure.
  * @param lhi     HIT
  * @param host_id HI
@@ -240,90 +244,93 @@
  * @return        0 on success, otherwise an negative error value is returned.
  */
 static int hip_add_host_id(hip_db_struct_t *db,
-                          const struct hip_lhi *lhi,
-                          hip_lsi_t *lsi,
-                          const struct hip_host_id_priv *host_id,
-                          int (*insert)(struct hip_host_id_entry *, void 
**arg), 
-                          int (*remove)(struct hip_host_id_entry *, void 
**arg),
-                          void *arg)
+                           const struct hip_lhi *lhi,
+                           hip_lsi_t *lsi,
+                           const struct hip_host_id_priv *host_id,
+                           int (*insert)(struct hip_host_id_entry *, void 
**arg),
+                           int (*remove)(struct hip_host_id_entry *, void 
**arg),
+                           void *arg)
 {
-       int err = 0;
-       struct hip_host_id_entry *id_entry = NULL;
-       struct hip_host_id_entry *old_entry;
-
-       HIP_WRITE_LOCK_DB(db);
-
-       _HIP_HEXDUMP("adding host id", &lhi->hit, sizeof(struct in6_addr));
-
-       HIP_ASSERT(&lhi->hit != NULL);
-       _HIP_DEBUG("host id algo:%d \n", hip_get_host_id_algo(host_id));
-       HIP_IFEL(!(id_entry = (struct hip_host_id_entry *) 
HIP_MALLOC(sizeof(struct hip_host_id_entry),
-                                                                     0)), 
-ENOMEM,
-                "No memory available for host id\n");
-       memset(id_entry, 0, sizeof(struct hip_host_id_entry));
-
-       ipv6_addr_copy(&id_entry->lhi.hit, &lhi->hit);
-       id_entry->lhi.anonymous = lhi->anonymous;
-
-       /* check for duplicates */
-       old_entry = hip_get_hostid_entry_by_lhi_and_algo(db, &lhi->hit, 
-                                                        HIP_ANY_ALGO, -1);
-       if (old_entry != NULL) {
-               HIP_WRITE_UNLOCK_DB(db);
-               HIP_ERROR("Trying to add duplicate lhi\n");
-               err = -EEXIST;
-               goto out_err;
-       }
-
-       /* assign a free lsi address */
-       HIP_IFEL((hip_hidb_add_lsi(db, id_entry))<0, -EEXIST, "No LSI free\n");
-       
-       memcpy(lsi, &id_entry->lsi, sizeof(hip_lsi_t));
-       id_entry->insert = insert;
-       id_entry->remove = remove;
-       id_entry->arg = arg;
-
-       list_add(id_entry, db);
-
-       if (hip_get_host_id_algo((struct hip_host_id *)host_id) == HIP_HI_RSA)
-               id_entry->private_key = hip_key_rr_to_rsa(host_id, 1);
-       else
-               id_entry->private_key = hip_key_rr_to_dsa(host_id, 1);
-               
-       HIP_DEBUG("Generating a new R1 set.\n");
-       HIP_IFEL(!(id_entry->r1 = hip_init_r1()), -ENOMEM, "Unable to allocate 
R1s.\n");        
-       id_entry->host_id = hip_get_public_key(host_id);
-               HIP_IFEL(!hip_precreate_r1(id_entry->r1, (struct in6_addr 
*)&lhi->hit,
-               (hip_get_host_id_algo(id_entry->host_id) == HIP_HI_RSA ? 
hip_rsa_sign : hip_dsa_sign),
-               id_entry->private_key, id_entry->host_id), -ENOENT, "Unable to 
precreate R1s.\n");
+    int err                            = 0;
+    struct hip_host_id_entry *id_entry = NULL;
+    struct hip_host_id_entry *old_entry;
+
+    HIP_WRITE_LOCK_DB(db);
+
+    _HIP_HEXDUMP("adding host id", &lhi->hit, sizeof(struct in6_addr));
+
+    HIP_ASSERT(&lhi->hit != NULL);
+    _HIP_DEBUG("host id algo:%d \n", hip_get_host_id_algo(host_id));
+    HIP_IFEL(!(id_entry = (struct hip_host_id_entry *) 
HIP_MALLOC(sizeof(struct hip_host_id_entry),
+                                                                  0)), -ENOMEM,
+             "No memory available for host id\n");
+    memset(id_entry, 0, sizeof(struct hip_host_id_entry));
+
+    ipv6_addr_copy(&id_entry->lhi.hit, &lhi->hit);
+    id_entry->lhi.anonymous = lhi->anonymous;
+
+    /* check for duplicates */
+    old_entry               = hip_get_hostid_entry_by_lhi_and_algo(db, 
&lhi->hit,
+                                                                   
HIP_ANY_ALGO, -1);
+    if (old_entry != NULL) {
+        HIP_WRITE_UNLOCK_DB(db);
+        HIP_ERROR("Trying to add duplicate lhi\n");
+        err = -EEXIST;
+        goto out_err;
+    }
+
+    /* assign a free lsi address */
+    HIP_IFEL((hip_hidb_add_lsi(db, id_entry)) < 0, -EEXIST, "No LSI free\n");
+
+    memcpy(lsi, &id_entry->lsi, sizeof(hip_lsi_t));
+    id_entry->insert = insert;
+    id_entry->remove = remove;
+    id_entry->arg    = arg;
+
+    list_add(id_entry, db);
+
+    if (hip_get_host_id_algo((struct hip_host_id *) host_id) == HIP_HI_RSA) {
+        id_entry->private_key = hip_key_rr_to_rsa(host_id, 1);
+    } else {
+        id_entry->private_key = hip_key_rr_to_dsa(host_id, 1);
+    }
+
+    HIP_DEBUG("Generating a new R1 set.\n");
+    HIP_IFEL(!(id_entry->r1 = hip_init_r1()), -ENOMEM, "Unable to allocate 
R1s.\n");
+    id_entry->host_id = hip_get_public_key(host_id);
+    HIP_IFEL(!hip_precreate_r1(id_entry->r1, (struct in6_addr *) &lhi->hit,
+                               (hip_get_host_id_algo(id_entry->host_id) == 
HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign),
+                               id_entry->private_key, id_entry->host_id), 
-ENOENT, "Unable to precreate R1s.\n");
 #ifdef CONFIG_HIP_BLIND
-       HIP_IFEL(!(id_entry->blindr1 = hip_init_r1()), -ENOMEM, "Unable to 
allocate blind R1s.\n");
-               HIP_IFEL(!hip_blind_precreate_r1(id_entry->blindr1, (struct 
in6_addr *)&lhi->hit,
-               (hip_get_host_id_algo(id_entry->host_id) == HIP_HI_RSA ? 
hip_rsa_sign : hip_dsa_sign),
-               id_entry->private_key, id_entry->host_id), -ENOENT, "Unable to 
precreate R1s.\n");
+    HIP_IFEL(!(id_entry->blindr1 = hip_init_r1()), -ENOMEM, "Unable to 
allocate blind R1s.\n");
+    HIP_IFEL(!hip_blind_precreate_r1(id_entry->blindr1, (struct in6_addr *) 
&lhi->hit,
+                                     (hip_get_host_id_algo(id_entry->host_id) 
== HIP_HI_RSA ? hip_rsa_sign : hip_dsa_sign),
+                                     id_entry->private_key, 
id_entry->host_id), -ENOENT, "Unable to precreate R1s.\n");
 #endif
 
-       /* Called while the database is locked, perhaps not the best
-           option but HIs are not added often */
-       if (insert) 
-               insert(id_entry, &arg);
-
- out_err:
-       if (err && id_entry) {
-           if (id_entry->host_id) {
-               if (id_entry->private_key) {
-                   if (hip_get_host_id_algo(id_entry->host_id) == HIP_HI_RSA)
-                       RSA_free(id_entry->private_key);
-                   else
-                       DSA_free(id_entry->private_key);
-               }
-               HIP_FREE(id_entry->host_id);
-           }
-           HIP_FREE(id_entry);
-       }
-
-       HIP_WRITE_UNLOCK_DB(db);
-       return err;
+    /* Called while the database is locked, perhaps not the best
+     * option but HIs are not added often */
+    if (insert) {
+        insert(id_entry, &arg);
+    }
+
+out_err:
+    if (err && id_entry) {
+        if (id_entry->host_id) {
+            if (id_entry->private_key) {
+                if (hip_get_host_id_algo(id_entry->host_id) == HIP_HI_RSA) {
+                    RSA_free(id_entry->private_key);
+                } else {
+                    DSA_free(id_entry->private_key);
+                }
+            }
+            HIP_FREE(id_entry->host_id);
+        }
+        HIP_FREE(id_entry);
+    }
+
+    HIP_WRITE_UNLOCK_DB(db);
+    return err;
 }
 
 /**
@@ -334,193 +341,194 @@
  */
 int hip_handle_add_local_hi(const struct hip_common *input)
 {
-       int err = 0;
-       struct hip_host_id_priv *host_identity = NULL;
-       struct hip_lhi lhi;
-       struct hip_tlv_common *param = NULL;
-       struct hip_eid_endpoint *eid_endpoint = NULL;
-       struct in6_addr in6_lsi;
-       hip_lsi_t lsi;
-       
-       HIP_DEBUG("/* --------- */ \n");
-       HIP_DEBUG_IN6ADDR("input->hits = ", &input->hits);
-       HIP_DEBUG_IN6ADDR("input->hitr = ", &input->hitr);
-       if ((err = hip_get_msg_err(input)) != 0) {
-               HIP_ERROR("daemon failed (%d)\n", err);
-               goto out_err;
-       }
-
-       /* Iterate through all host identities in the input */
-       while((param = hip_get_next_param(input, param)) != NULL) {
-         
-         /* NOTE: changed to use hip_eid_endpoint structs instead of 
-            hip_host_id:s when passing IDs from user space to kernel */
-               if  (hip_get_param_type(param) != HIP_PARAM_EID_ENDPOINT)
-                       continue;
-               HIP_DEBUG("host id found in the msg\n");
-
-               eid_endpoint = (struct hip_eid_endpoint *)param;
-
-               HIP_IFEL(!eid_endpoint,-ENOENT,"No host endpoint in input\n");
-
-               host_identity = &eid_endpoint->endpoint.id.host_id;
-         
-               _HIP_HEXDUMP("host id\n", host_identity,
-                      hip_get_param_total_len(host_identity));
-         
-               HIP_IFEL(hip_private_host_id_to_hit(host_identity, &lhi.hit,
-                                             HIP_HIT_TYPE_HASH100),
-                       -EFAULT, "Host id to hit conversion failed\n");
-         
-               lhi.anonymous =
-                       (eid_endpoint->endpoint.flags & HIP_ENDPOINT_FLAG_ANON)
-                       ?
-                       1 : 0;
-
-               /*  lhi.algo = eid_endpoint.algo;*/
-
-               err = hip_add_host_id(HIP_DB_LOCAL_HID, &lhi,
-                                     &lsi, host_identity,
-                                     NULL, NULL, NULL);
-
-               /* Currently only RSA pub is added by default (bug id 522).
-                  Ignore redundant adding in case user wants to enable
-                  multiple HITs. */
-               HIP_IFEL((err == -EEXIST), 0,
-                        "Ignoring redundant HI\n");
-
-               /* Adding the pair <HI,LSI> */
-               HIP_IFEL(err,
-                       -EFAULT, "adding of local host identity failed\n");
-
-               
-               IPV4_TO_IPV6_MAP(&lsi, &in6_lsi);
-               /* Adding routes just in case they don't exist */
-               hip_add_iface_local_route(&lhi.hit);
-               hip_add_iface_local_route(&in6_lsi);
-               
-               /* Adding HITs and LSIs to the interface */
-               HIP_IFEL(hip_add_iface_local_hit(&lhi.hit), -1,
-                       "Failed to add HIT to the device\n");
-               HIP_IFEL(hip_add_iface_local_hit(&in6_lsi), -1, 
-                       "Failed to add LSI to the device\n");
-       }
-
-       HIP_DEBUG("Adding of HIP localhost identities was successful\n");
-
- out_err:      
-       return err;
+    int err                                = 0;
+    struct hip_host_id_priv *host_identity = NULL;
+    struct hip_lhi lhi;
+    struct hip_tlv_common *param           = NULL;
+    struct hip_eid_endpoint *eid_endpoint  = NULL;
+    struct in6_addr in6_lsi;
+    hip_lsi_t lsi;
+
+    HIP_DEBUG("/* --------- */ \n");
+    HIP_DEBUG_IN6ADDR("input->hits = ", &input->hits);
+    HIP_DEBUG_IN6ADDR("input->hitr = ", &input->hitr);
+    if ((err = hip_get_msg_err(input)) != 0) {
+        HIP_ERROR("daemon failed (%d)\n", err);
+        goto out_err;
+    }
+
+    /* Iterate through all host identities in the input */
+    while ((param = hip_get_next_param(input, param)) != NULL) {
+        /* NOTE: changed to use hip_eid_endpoint structs instead of
+        *  hip_host_id:s when passing IDs from user space to kernel */
+        if  (hip_get_param_type(param) != HIP_PARAM_EID_ENDPOINT) {
+            continue;
+        }
+        HIP_DEBUG("host id found in the msg\n");
+
+        eid_endpoint  = (struct hip_eid_endpoint *) param;
+
+        HIP_IFEL(!eid_endpoint, -ENOENT, "No host endpoint in input\n");
+
+        host_identity = &eid_endpoint->endpoint.id.host_id;
+
+        _HIP_HEXDUMP("host id\n", host_identity,
+                     hip_get_param_total_len(host_identity));
+
+        HIP_IFEL(hip_private_host_id_to_hit(host_identity, &lhi.hit,
+                                            HIP_HIT_TYPE_HASH100),
+                 -EFAULT, "Host id to hit conversion failed\n");
+
+        lhi.anonymous =
+            (eid_endpoint->endpoint.flags & HIP_ENDPOINT_FLAG_ANON)
+            ?
+            1 : 0;
+
+        /*  lhi.algo = eid_endpoint.algo;*/
+
+        err = hip_add_host_id(HIP_DB_LOCAL_HID, &lhi,
+                              &lsi, host_identity,
+                              NULL, NULL, NULL);
+
+        /* Currently only RSA pub is added by default (bug id 522).
+         * Ignore redundant adding in case user wants to enable
+         * multiple HITs. */
+        HIP_IFEL((err == -EEXIST), 0,
+                 "Ignoring redundant HI\n");
+
+        /* Adding the pair <HI,LSI> */
+        HIP_IFEL(err,
+                 -EFAULT, "adding of local host identity failed\n");
+
+
+        IPV4_TO_IPV6_MAP(&lsi, &in6_lsi);
+        /* Adding routes just in case they don't exist */
+        hip_add_iface_local_route(&lhi.hit);
+        hip_add_iface_local_route(&in6_lsi);
+
+        /* Adding HITs and LSIs to the interface */
+        HIP_IFEL(hip_add_iface_local_hit(&lhi.hit), -1,
+                 "Failed to add HIT to the device\n");
+        HIP_IFEL(hip_add_iface_local_hit(&in6_lsi), -1,
+                 "Failed to add LSI to the device\n");
+    }
+
+    HIP_DEBUG("Adding of HIP localhost identities was successful\n");
+
+out_err:
+    return err;
 }
 
 /**
  * Handles the deletion of a localhost host identity.
- * 
+ *
  * @param input the message containing the hit to be deleted.
  * @return    zero on success, or negative error value on failure.
  */
 int hip_handle_del_local_hi(const struct hip_common *input)
 {
-       struct in6_addr *hit;
-       struct hip_lhi lhi;
-       char buf[46];
-       int err = 0;
-
-       hit = (struct in6_addr *)
-               hip_get_param_contents(input, HIP_PARAM_HIT);
-       HIP_IFEL(!hit, -ENODATA, "no hit\n");
-
-       hip_in6_ntop(hit, buf);
-       HIP_INFO("del HIT: %s\n", buf);
-
-       ipv6_addr_copy(&lhi.hit, hit);
-
-        if ( (err = hip_del_host_id(HIP_DB_LOCAL_HID, &lhi)) ) {
-               HIP_ERROR("deleting of local host identity failed\n");
-               goto out_err;
-        }
-
-       /** @todo remove associations from hadb & beetdb by the deleted HI. */
-       HIP_DEBUG("Removal of HIP localhost identity was successful\n");
- out_err:
-       return err;
+    struct in6_addr *hit;
+    struct hip_lhi lhi;
+    char buf[46];
+    int err = 0;
+
+    hit = (struct in6_addr *)
+          hip_get_param_contents(input, HIP_PARAM_HIT);
+    HIP_IFEL(!hit, -ENODATA, "no hit\n");
+
+    hip_in6_ntop(hit, buf);
+    HIP_INFO("del HIT: %s\n", buf);
+
+    ipv6_addr_copy(&lhi.hit, hit);
+
+    if ((err = hip_del_host_id(HIP_DB_LOCAL_HID, &lhi))) {
+        HIP_ERROR("deleting of local host identity failed\n");
+        goto out_err;
+    }
+
+    /** @todo remove associations from hadb & beetdb by the deleted HI. */
+    HIP_DEBUG("Removal of HIP localhost identity was successful\n");
+out_err:
+    return err;
 }
 
 /**
  * Copies to the @c target the first local HIT that is found.
- * 
+ *
  * @param target placeholder for the target
  * @param algo   the algoritm to match, but if HIP_ANY_ALGO comparison is
- '               ignored.
+ * '               ignored.
  * @param anon   -1 if you don't care, 1 if anon, 0 if public
  * @return       0 if ok, and negative if failed.
  */
 int hip_get_any_localhost_hit(struct in6_addr *target, int algo, int anon)
 {
-       struct hip_host_id_entry *entry;
-       int err = 0;
-
-       HIP_READ_LOCK_DB(hip_local_hostid_db);
-       
-       entry = hip_get_hostid_entry_by_lhi_and_algo(hip_local_hostid_db,
-                                                    NULL, algo, anon);
-       if (!entry) {
-               err=-ENOENT;
-               goto out;
-       }
-       
-       ipv6_addr_copy(target,&entry->lhi.hit);
-       err = 0;
-       
- out:
-       HIP_READ_UNLOCK_DB(hip_local_hostid_db);
-       return err;
+    struct hip_host_id_entry *entry;
+    int err = 0;
+
+    HIP_READ_LOCK_DB(hip_local_hostid_db);
+
+    entry = hip_get_hostid_entry_by_lhi_and_algo(hip_local_hostid_db,
+                                                 NULL, algo, anon);
+    if (!entry) {
+        err = -ENOENT;
+        goto out;
+    }
+
+    ipv6_addr_copy(target, &entry->lhi.hit);
+    err = 0;
+
+out:
+    HIP_READ_UNLOCK_DB(hip_local_hostid_db);
+    return err;
 }
 
 #if 0
 /**
  * Returns pointer to newly allocated area that contains a localhost HI. NULL
- * is returned if problems are encountered. 
+ * is returned if problems are encountered.
  *
  * @param db   a pointer to a database.
  * @param lhi  HIT to match, if null, any.
  * @param algo algorithm to match, if HIP_ANY_ALGO, any.
  * @note       Remember to free the host id structure after use.
  */
-static struct hip_host_id *hip_get_host_id(hip_db_struct_t *db, 
-                                          struct in6_addr *hit, int algo)
+static struct hip_host_id *hip_get_host_id(hip_db_struct_t *db,
+                                           struct in6_addr *hit, int algo)
 {
-       struct hip_host_id_entry *tmp = NULL;
-       struct hip_host_id *result = NULL;
-       int t = 0;
-
-       HIP_READ_LOCK_DB(db);
-
-       tmp = hip_get_hostid_entry_by_lhi_and_algo(db, hit, algo, -1);
-       if (!tmp) {
-               HIP_READ_UNLOCK_DB(db);
-               HIP_ERROR("No host ID found.\n");
-               return NULL;
-       }
-
-       t = hip_get_param_total_len(tmp->host_id);
-       _HIP_DEBUG("Host ID length is %d bytes.\n", t);
-       if (t > HIP_MAX_HOST_ID_LEN) {
-               HIP_READ_UNLOCK_DB(db);
-               return NULL;
-       }
-
-       result = HIP_MALLOC(t, GFP_ATOMIC);
-       if (!result) {
-               HIP_READ_UNLOCK_DB(db);
-               HIP_ERROR("Out of memory.\n");
-               return NULL;
-       }
-
-       memcpy(result, tmp->host_id, t);
-       HIP_READ_UNLOCK_DB(db);
-
-       return result;
+    struct hip_host_id_entry *tmp = NULL;
+    struct hip_host_id *result    = NULL;
+    int t                         = 0;
+
+    HIP_READ_LOCK_DB(db);
+
+    tmp = hip_get_hostid_entry_by_lhi_and_algo(db, hit, algo, -1);
+    if (!tmp) {
+        HIP_READ_UNLOCK_DB(db);
+        HIP_ERROR("No host ID found.\n");
+        return NULL;
+    }
+
+    t = hip_get_param_total_len(tmp->host_id);
+    _HIP_DEBUG("Host ID length is %d bytes.\n", t);
+    if (t > HIP_MAX_HOST_ID_LEN) {
+        HIP_READ_UNLOCK_DB(db);
+        return NULL;
+    }
+
+    result = HIP_MALLOC(t, GFP_ATOMIC);
+    if (!result) {
+        HIP_READ_UNLOCK_DB(db);
+        HIP_ERROR("Out of memory.\n");
+        return NULL;
+    }
+
+    memcpy(result, tmp->host_id, t);
+    HIP_READ_UNLOCK_DB(db);
+
+    return result;
 }
+
 #endif
 
 /**
@@ -528,38 +536,38 @@
  */
 static struct hip_host_id *hip_get_dsa_public_key(const struct 
hip_host_id_priv *hi)
 {
-       int key_len;
-       /* T could easily have been an int, since the compiler will
-          probably add 3 alignment bytes here anyway. */
-       u8 T;
-       struct hip_host_id *ret;
-
-       /* check T, Miika won't like this */
-       T = *((u8 *)(hi->key));
-       if (T > 8) {
-               HIP_ERROR("Invalid T-value in DSA key (0x%x)\n",T);
-               return NULL;
-       }
-       if (T != 8) {
-               HIP_DEBUG("T-value in DSA-key not 8 (0x%x)!\n",T);
-       }
-       key_len = 64 + (T * 8);
-
-       ret = malloc(sizeof(struct hip_host_id));
-       memcpy(ret, hi, sizeof(struct hip_host_id));
-
-       /* the secret component of the DSA key is always 20 bytes */
-       ret->hi_length = htons(ntohs(hi->hi_length) - DSA_PRIV);
-       memset((char *)(&ret->key) + ntohs(ret->hi_length) - sizeof(hi->rdata),
-                                       0, sizeof(ret->key) - 
ntohs(ret->hi_length));
-       ret->length = htons(sizeof(struct hip_host_id));
-
-       return ret;
+    int key_len;
+    /* T could easily have been an int, since the compiler will
+     * probably add 3 alignment bytes here anyway. */
+    u8 T;
+    struct hip_host_id *ret;
+
+    /* check T, Miika won't like this */
+    T = *((u8 *) (hi->key));
+    if (T > 8) {
+        HIP_ERROR("Invalid T-value in DSA key (0x%x)\n", T);
+        return NULL;
+    }
+    if (T != 8) {
+        HIP_DEBUG("T-value in DSA-key not 8 (0x%x)!\n", T);
+    }
+    key_len        = 64 + (T * 8);
+
+    ret            = malloc(sizeof(struct hip_host_id));
+    memcpy(ret, hi, sizeof(struct hip_host_id));
+
+    /* the secret component of the DSA key is always 20 bytes */
+    ret->hi_length = htons(ntohs(hi->hi_length) - DSA_PRIV);
+    memset((char *) (&ret->key) + ntohs(ret->hi_length) - sizeof(hi->rdata),
+           0, sizeof(ret->key) - ntohs(ret->hi_length));
+    ret->length    = htons(sizeof(struct hip_host_id));
+
+    return ret;
 }
 
 #if 0
 /**
- * . 
+ * .
  *
  * @return a newly allocated area that contains the public key part of the
  * localhost host identity. NULL is returned if errors detected.
@@ -567,17 +575,19 @@
  */
 static struct hip_host_id *hip_get_any_localhost_dsa_public_key(void)
 {
-       struct hip_host_id *res;
-       
-       res = hip_get_host_id(hip_local_hostid_db, NULL, HIP_HI_DSA);
-       if (!res)
-               HIP_ERROR("No host id for localhost\n");
-
-       return res;
+    struct hip_host_id *res;
+
+    res = hip_get_host_id(hip_local_hostid_db, NULL, HIP_HI_DSA);
+    if (!res) {
+        HIP_ERROR("No host id for localhost\n");
+    }
+
+    return res;
 }
+
 #endif
 
-/** 
+/**
  * Gets the RSA public key from a Host Identity
  *
  * @param tmp a pointer to a Host Identity.
@@ -585,67 +595,69 @@
  */
 static struct hip_host_id *hip_get_rsa_public_key(const struct 
hip_host_id_priv *tmp)
 {
-       int rsa_pub_len;
-       struct hip_rsa_keylen keylen;
-       struct hip_host_id *ret;
-
-       /** @todo check some value in the RSA key? */
-      
-       _HIP_HEXDUMP("HOSTID...",tmp, hip_get_param_total_len(tmp));
-
-       hip_get_rsa_keylen(tmp, &keylen, 1);
-       rsa_pub_len = keylen.e_len + keylen.e + keylen.n;
-
-       ret = malloc(sizeof(struct hip_host_id));
-       memcpy(ret, tmp, sizeof(struct hip_host_id) -
-                               sizeof(ret->key) - sizeof(ret->hostname));
-       ret->hi_length = htons(rsa_pub_len + sizeof(struct 
hip_host_id_key_rdata));
-       memcpy(ret->key, tmp->key, rsa_pub_len);
-       memcpy(ret->hostname, tmp->hostname, sizeof(ret->hostname));
-       ret->length = htons(sizeof(struct hip_host_id));
-
-       return ret;
+    int rsa_pub_len;
+    struct hip_rsa_keylen keylen;
+    struct hip_host_id *ret;
+
+    /** @todo check some value in the RSA key? */
+
+    _HIP_HEXDUMP("HOSTID...", tmp, hip_get_param_total_len(tmp));
+
+    hip_get_rsa_keylen(tmp, &keylen, 1);
+    rsa_pub_len    = keylen.e_len + keylen.e + keylen.n;
+
+    ret            = malloc(sizeof(struct hip_host_id));
+    memcpy(ret, tmp, sizeof(struct hip_host_id) -
+           sizeof(ret->key) - sizeof(ret->hostname));
+    ret->hi_length = htons(rsa_pub_len + sizeof(struct hip_host_id_key_rdata));
+    memcpy(ret->key, tmp->key, rsa_pub_len);
+    memcpy(ret->hostname, tmp->hostname, sizeof(ret->hostname));
+    ret->length    = htons(sizeof(struct hip_host_id));
+
+    return ret;
 }
 
 #if 0
 /**
  * .
- * 
+ *
  * @return a newly allocated area that contains the public key part of the
  *         localhost host identity. %NULL is returned if errors detected.
  * @note   Remember to free the return value.
  */
 static struct hip_host_id *hip_get_any_localhost_rsa_public_key(void)
 {
-       struct hip_host_id *res;
-
-       res = hip_get_host_id(hip_local_hostid_db, NULL, HIP_HI_RSA);
-       if (!res)
-               HIP_ERROR("No host id for localhost\n");
-         
-       return res;     
+    struct hip_host_id *res;
+
+    res = hip_get_host_id(hip_local_hostid_db, NULL, HIP_HI_RSA);
+    if (!res) {
+        HIP_ERROR("No host id for localhost\n");
+    }
+
+    return res;
 }
+
 #endif
 
-/** 
+/**
  * Transforms a private/public key pair to a public key, private key is 
deleted.
  *
  * @param hid a pointer to a host identity.
  * @return    a pointer to a host identity if the transformation was
  *            successful, NULL otherwise.
  */
-static struct hip_host_id *hip_get_public_key(const struct hip_host_id_priv 
*hid) 
+static struct hip_host_id *hip_get_public_key(const struct hip_host_id_priv 
*hid)
 {
-       int alg = hip_get_host_id_algo((struct hip_host_id *)hid);
-       switch (alg) {
-       case HIP_HI_RSA:
-               return hip_get_rsa_public_key(hid);
-       case HIP_HI_DSA:
-               return hip_get_dsa_public_key(hid);
-       default:
-               HIP_ERROR("Unsupported HI algorithm (%d)\n", alg);
-               return NULL;
-       }
+    int alg = hip_get_host_id_algo((struct hip_host_id *) hid);
+    switch (alg) {
+    case HIP_HI_RSA:
+        return hip_get_rsa_public_key(hid);
+    case HIP_HI_DSA:
+        return hip_get_dsa_public_key(hid);
+    default:
+        HIP_ERROR("Unsupported HI algorithm (%d)\n", alg);
+        return NULL;
+    }
 }
 
 /**
@@ -658,247 +670,265 @@
 
 static int hip_hidb_add_lsi(hip_db_struct_t *db, struct hip_host_id_entry 
*id_entry)
 {
-       struct hip_host_id_entry *id_entry_aux;
-       hip_list_t *item;
-       hip_lsi_t lsi_aux;
-       int err = 0, used_lsi, c, i;
-       int len = sizeof(lsi_addresses)/sizeof(*lsi_addresses);
-
-       for(i=0; i < len; i++) {        
-               inet_aton(lsi_addresses[i],&lsi_aux);
-               used_lsi = 0;
-
-               list_for_each(item, db, c) {
-                       id_entry_aux = (struct hip_host_id_entry 
*)list_entry(item);
-                       if (hip_lsi_are_equal(&lsi_aux,&id_entry_aux->lsi)) {
-                               used_lsi = 1;
-                               c = -1;                         
-                       }
-               }
-
-               if (!used_lsi){
-                       memcpy(&id_entry->lsi, &lsi_aux, sizeof(hip_lsi_t));
-                       _HIP_DEBUG("LSI 
assigned:%s\n",inet_ntoa(id_entry->lsi));
-                       break;
-               }
-       }
-       return err;     
-}
-
-/**
-* Search if the lsi exists already in the hidb
-*
-* @param lsi lsi we are searching
-* @return 0 if it's not in the hidb, 1 if it is
-*/
-int hip_hidb_exists_lsi(hip_lsi_t *lsi){
-       struct hip_host_id_entry *id_entry;
-       hip_list_t *item;
-       int c, res = 0;
-
-       list_for_each(item, hip_local_hostid_db, c) {
-               id_entry = (struct hip_host_id_entry *)list_entry(item);
-               if (hip_lsi_are_equal(&id_entry->lsi, lsi))
-                       return 1;               
-       }
-       return res;
-}
-
-
-/**
- * Lists every hit in the database. 
- * 
+    struct hip_host_id_entry *id_entry_aux;
+    hip_list_t *item;
+    hip_lsi_t lsi_aux;
+    int err = 0, used_lsi, c, i;
+    int len = sizeof(lsi_addresses) / sizeof(*lsi_addresses);
+
+    for (i = 0; i < len; i++) {
+        inet_aton(lsi_addresses[i], &lsi_aux);
+        used_lsi = 0;
+
+        list_for_each(item, db, c) {
+            id_entry_aux = (struct hip_host_id_entry *) list_entry(item);
+            if (hip_lsi_are_equal(&lsi_aux, &id_entry_aux->lsi)) {
+                used_lsi = 1;
+                c        = -1;
+            }
+        }
+
+        if (!used_lsi) {
+            memcpy(&id_entry->lsi, &lsi_aux, sizeof(hip_lsi_t));
+            _HIP_DEBUG("LSI assigned:%s\n", inet_ntoa(id_entry->lsi));
+            break;
+        }
+    }
+    return err;
+}
+
+/**
+ * Search if the lsi exists already in the hidb
+ *
+ * @param lsi lsi we are searching
+ * @return 0 if it's not in the hidb, 1 if it is
+ */
+int hip_hidb_exists_lsi(hip_lsi_t *lsi)
+{
+    struct hip_host_id_entry *id_entry;
+    hip_list_t *item;
+    int c, res = 0;
+
+    list_for_each(item, hip_local_hostid_db, c) {
+        id_entry = (struct hip_host_id_entry *) list_entry(item);
+        if (hip_lsi_are_equal(&id_entry->lsi, lsi)) {
+            return 1;
+        }
+    }
+    return res;
+}
+
+/**
+ * Lists every hit in the database.
+ *
  * @param func   a mapper function.
  * @param opaque opaque data for the mapper function.
  * @return       ...
- * 
+ *
  * @note Works like hip_for_each_ha().
  */
 int hip_for_each_hi(int (*func)(struct hip_host_id_entry *entry, void *opaq), 
void *opaque)
 {
-       hip_list_t *curr, *iter;
-       struct hip_host_id_entry *tmp;
-       int err = 0, c;
-
-       HIP_READ_LOCK_DB(hip_local_hostid_db);
-
-       list_for_each_safe(curr, iter, hip_local_hostid_db, c)
-       {
-               tmp = (struct hip_host_id_entry *)list_entry(curr);
-               HIP_DEBUG_HIT("Found HIT", &tmp->lhi.hit);
-               HIP_DEBUG_LSI("Found LSI", &tmp->lsi);
-               err = func(tmp, opaque);
-               if (err)
-                 goto out_err;
-       }
-
-out_err:
-       HIP_READ_UNLOCK_DB(hip_local_hostid_db);
-
-       return (err);
-}
-
-static struct hip_host_id_entry *hip_hidb_get_entry_by_lsi(
-     hip_db_struct_t *db, const struct in_addr *lsi)
-{
-       struct hip_host_id_entry *id_entry;
-       hip_list_t *item;
-       int c;
-
-       list_for_each(item, db, c) {
-               id_entry = (struct hip_host_id_entry *)list_entry(item);
-               if (!ipv4_addr_cmp(&id_entry->lsi, lsi))
-                       return id_entry;
-       }
-       return NULL;
-}
-
-int hip_hidb_associate_default_hit_lsi(hip_hit_t *default_hit, hip_lsi_t 
*default_lsi){
-       int err = 0;
-       hip_lsi_t aux_lsi; 
-       struct hip_host_id_entry *tmp1;
-       struct hip_host_id_entry *tmp2;
-
-       //1. Check if default_hit already associated with default_lsi
-       HIP_IFEL((err = hip_hidb_get_lsi_by_hit(default_hit, &aux_lsi)), -1, 
"Error no lsi associated to hit\n");
-
-       if(ipv4_addr_cmp(&aux_lsi, default_lsi)){
-               HIP_IFEL(!(tmp1 = 
hip_get_hostid_entry_by_lhi_and_algo(HIP_DB_LOCAL_HID, default_hit, 
HIP_ANY_ALGO, -1)),
-                        -1,"Default hit not found in hidb\n");
-               HIP_IFEL(!(tmp2 = hip_hidb_get_entry_by_lsi(HIP_DB_LOCAL_HID, 
default_lsi)), -1,
-                        "Default lsi not found in hidb\n");
-
-               memcpy(&tmp2->lsi, &tmp1->lsi, sizeof(tmp1->lsi));
-               memcpy(&tmp1->lsi, default_lsi, sizeof(tmp2->lsi));
-       }
-
- out_err:
-       return err;
+    hip_list_t *curr, *iter;
+    struct hip_host_id_entry *tmp;
+    int err = 0, c;
+
+    HIP_READ_LOCK_DB(hip_local_hostid_db);
+
+    list_for_each_safe(curr, iter, hip_local_hostid_db, c)
+    {
+        tmp = (struct hip_host_id_entry *) list_entry(curr);
+        HIP_DEBUG_HIT("Found HIT", &tmp->lhi.hit);
+        HIP_DEBUG_LSI("Found LSI", &tmp->lsi);
+        err = func(tmp, opaque);
+        if (err) {
+            goto out_err;
+        }
+    }
+
+out_err:
+    HIP_READ_UNLOCK_DB(hip_local_hostid_db);
+
+    return err;
+}
+
+static struct hip_host_id_entry *hip_hidb_get_entry_by_lsi(hip_db_struct_t *db,
+                                                           const struct 
in_addr *lsi)
+{
+    struct hip_host_id_entry *id_entry;
+    hip_list_t *item;
+    int c;
+
+    list_for_each(item, db, c) {
+        id_entry = (struct hip_host_id_entry *) list_entry(item);
+        if (!ipv4_addr_cmp(&id_entry->lsi, lsi)) {
+            return id_entry;
+        }
+    }
+    return NULL;
+}
+
+int hip_hidb_associate_default_hit_lsi(hip_hit_t *default_hit, hip_lsi_t 
*default_lsi)
+{
+    int err = 0;
+    hip_lsi_t aux_lsi;
+    struct hip_host_id_entry *tmp1;
+    struct hip_host_id_entry *tmp2;
+
+    //1. Check if default_hit already associated with default_lsi
+    HIP_IFEL((err = hip_hidb_get_lsi_by_hit(default_hit, &aux_lsi)),
+             -1,
+             "Error no lsi associated to hit\n");
+
+    if (ipv4_addr_cmp(&aux_lsi, default_lsi)) {
+        HIP_IFEL(!(tmp1 = 
hip_get_hostid_entry_by_lhi_and_algo(HIP_DB_LOCAL_HID,
+                                                               default_hit,
+                                                               HIP_ANY_ALGO,
+                                                               -1)),
+                 -1, "Default hit not found in hidb\n");
+        HIP_IFEL(!(tmp2 = hip_hidb_get_entry_by_lsi(HIP_DB_LOCAL_HID, 
default_lsi)), -1,
+                 "Default lsi not found in hidb\n");
+
+        memcpy(&tmp2->lsi, &tmp1->lsi, sizeof(tmp1->lsi));
+        memcpy(&tmp1->lsi, default_lsi, sizeof(tmp2->lsi));
+    }
+
+out_err:
+    return err;
 }
 
 //#ifdef CONFIG_HIP_BLIND
 int hip_blind_find_local_hi(uint16_t *nonce,  struct in6_addr *test_hit,
-                           struct in6_addr *local_hit)
+                            struct in6_addr *local_hit)
 {
-  hip_list_t *curr, *iter;
-  struct hip_host_id_entry *tmp;
-  int err = 0, c;
-  char *key = NULL;
-  unsigned int key_len = sizeof(struct in6_addr);
-  struct in6_addr *blind_hit = NULL;
-
-  // generate key = nonce|hit_our
-  HIP_IFEL((key = HIP_MALLOC(sizeof(uint16_t)+ sizeof(struct in6_addr), 0)) == 
NULL, 
-          -1, "Couldn't allocate memory\n");
-
-  HIP_IFEL((blind_hit = HIP_MALLOC(sizeof(struct in6_addr), 0)) == NULL, 
-          -1, "Couldn't allocate memory\n");
-   
-  HIP_READ_LOCK_DB(hip_local_hostid_db);
-  
-  list_for_each_safe(curr, iter, hip_local_hostid_db, c)
+    hip_list_t *curr, *iter;
+    struct hip_host_id_entry *tmp;
+    int err                    = 0, c;
+    char *key                  = NULL;
+    unsigned int key_len       = sizeof(struct in6_addr);
+    struct in6_addr *blind_hit = NULL;
+
+    // generate key = nonce|hit_our
+    HIP_IFEL((key = HIP_MALLOC(sizeof(uint16_t) + sizeof(struct in6_addr), 0)) 
== NULL,
+             -1, "Couldn't allocate memory\n");
+
+    HIP_IFEL((blind_hit = HIP_MALLOC(sizeof(struct in6_addr), 0)) == NULL,
+             -1, "Couldn't allocate memory\n");
+
+    HIP_READ_LOCK_DB(hip_local_hostid_db);
+
+    list_for_each_safe(curr, iter, hip_local_hostid_db, c)
     {
-      tmp = (struct hip_host_id_entry *)list_entry(curr);
-      HIP_HEXDUMP("Found HIT", &tmp->lhi.hit, 16);
-      
-      // let's test the hit
-      memcpy(key, &tmp->lhi.hit, sizeof(struct in6_addr));
-      memcpy(key + sizeof(struct in6_addr), &nonce, sizeof(uint16_t));
-      HIP_IFEL(hip_do_blind(key, key_len, blind_hit), -1, "hip_do_blind failed 
\n");
-      if (blind_hit == NULL) {
-       err = -1;
-       goto out_err;
-      }
-      HIP_HEXDUMP("test HIT:", test_hit, 16);
-      if (hip_match_hit(test_hit, blind_hit)) {
-       HIP_HEXDUMP("Plain HIT found:", &tmp->lhi.hit, 16);
-       memcpy(local_hit, &tmp->lhi.hit, sizeof(struct in6_addr));
-       goto out_err;
-      }
-    }
-  
-  HIP_READ_UNLOCK_DB(hip_local_hostid_db);
-  
- out_err:
-  if(key)
-    HIP_FREE(key);
-  if(blind_hit)
-    HIP_FREE(blind_hit);
-  return err;  
+        tmp = (struct hip_host_id_entry *) list_entry(curr);
+        HIP_HEXDUMP("Found HIT", &tmp->lhi.hit, 16);
+
+        // let's test the hit
+        memcpy(key, &tmp->lhi.hit, sizeof(struct in6_addr));
+        memcpy(key + sizeof(struct in6_addr), &nonce, sizeof(uint16_t));
+        HIP_IFEL(hip_do_blind(key, key_len, blind_hit), -1, "hip_do_blind 
failed \n");
+        if (blind_hit == NULL) {
+            err = -1;
+            goto out_err;
+        }
+        HIP_HEXDUMP("test HIT:", test_hit, 16);
+        if (hip_match_hit(test_hit, blind_hit)) {
+            HIP_HEXDUMP("Plain HIT found:", &tmp->lhi.hit, 16);
+            memcpy(local_hit, &tmp->lhi.hit, sizeof(struct in6_addr));
+            goto out_err;
+        }
+    }
+
+    HIP_READ_UNLOCK_DB(hip_local_hostid_db);
+
+out_err:
+    if (key) {
+        HIP_FREE(key);
+    }
+    if (blind_hit) {
+        HIP_FREE(blind_hit);
+    }
+    return err;
 }
+
 //#endif
 
 int hip_get_host_id_and_priv_key(hip_db_struct_t *db, struct in6_addr *hit,
-                       int algo, struct hip_host_id **host_id, void **key) {
-       int err = 0, host_id_len;
-       struct hip_host_id_entry *entry = NULL;
-
-       HIP_READ_LOCK_DB(db);
-
-       entry = hip_get_hostid_entry_by_lhi_and_algo(db, hit, algo, -1);
-       //HIP_IFEL(!entry, "Host ID not found\n", -1);
-       HIP_IFE(!entry, -1);
-
-       host_id_len = hip_get_param_total_len(entry->host_id);
-       HIP_IFE(host_id_len > HIP_MAX_HOST_ID_LEN, -1);
-
-       *host_id = HIP_MALLOC(host_id_len, GFP_ATOMIC);
-       HIP_IFE(!*host_id, -ENOMEM);
-       memcpy (*host_id, entry->host_id, host_id_len);
-
-       *key = entry->private_key;
-       HIP_IFE(!*key, -1);
-       
-  out_err:
-       HIP_READ_UNLOCK_DB(db);
-       return err;
+                                 int algo, struct hip_host_id **host_id, void 
**key)
+{
+    int err                         = 0, host_id_len;
+    struct hip_host_id_entry *entry = NULL;
+
+    HIP_READ_LOCK_DB(db);
+
+    entry       = hip_get_hostid_entry_by_lhi_and_algo(db, hit, algo, -1);
+    //HIP_IFEL(!entry, "Host ID not found\n", -1);
+    HIP_IFE(!entry, -1);
+
+    host_id_len = hip_get_param_total_len(entry->host_id);
+    HIP_IFE(host_id_len > HIP_MAX_HOST_ID_LEN, -1);
+
+    *host_id    = HIP_MALLOC(host_id_len, GFP_ATOMIC);
+    HIP_IFE(!*host_id, -ENOMEM);
+    memcpy(*host_id, entry->host_id, host_id_len);
+
+    *key        = entry->private_key;
+    HIP_IFE(!*key, -1);
+
+out_err:
+    HIP_READ_UNLOCK_DB(db);
+    return err;
 }
 
 int hip_build_host_id_and_signature(struct hip_common *msg,  hip_hit_t *hit)
 {
-       struct hip_host_id *hi_public = NULL;
-       int err = 0;
-       int alg = -1;
-       void *private_key;
-
-       HIP_IFEL((hit == NULL), -1, "Null HIT\n");
-
-       /*
-        * Below is the code for getting host id and appending it to the 
message (after removing private
-        * key from it hi_public
-        * Where as hi_private is used to create signature on message
-        * Both of these are appended to the message sequally
-        */
-
-       if ( (err = hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID, hit,
-                                              HIP_ANY_ALGO, &hi_public, 
&private_key)) ) {
-               HIP_ERROR("Unable to locate HI from HID with HIT as key");
-               goto out_err;
-       }
-
-       HIP_IFE(hip_build_param(msg, hi_public), -1);
-       _HIP_DUMP_MSG(msg);
-
-       alg = hip_get_host_id_algo(hi_public);
-       switch (alg) {
-       case HIP_HI_RSA:
-               err = hip_rsa_sign(private_key, msg);
-               break;
-       case HIP_HI_DSA:
-               err = hip_dsa_sign(private_key, msg);
-               break;
-       default:
-               HIP_ERROR("Unsupported HI algorithm (%d)\n", alg);
-               break;
-       }
-
-       _HIP_DUMP_MSG(msg);
+    struct hip_host_id *hi_public = NULL;
+    int err                       = 0;
+    int alg                       = -1;
+    void *private_key;
+
+    HIP_IFEL((hit == NULL), -1, "Null HIT\n");
+
+    /*
+     * Below is the code for getting host id and appending it to the message
+     * (after removing private key from it hi_public)
+     * Where as hi_private is used to create signature on message
+     * Both of these are appended to the message sequally
+     */
+
+    if ((err = hip_get_host_id_and_priv_key(HIP_DB_LOCAL_HID,
+                                            hit,
+                                            HIP_ANY_ALGO,
+                                            &hi_public,
+                                            &private_key)))
+    {
+        HIP_ERROR("Unable to locate HI from HID with HIT as key");
+        goto out_err;
+    }
+
+    HIP_IFE(hip_build_param(msg, hi_public), -1);
+    _HIP_DUMP_MSG(msg);
+
+    alg = hip_get_host_id_algo(hi_public);
+    switch (alg) {
+    case HIP_HI_RSA:
+        err = hip_rsa_sign(private_key, msg);
+        break;
+    case HIP_HI_DSA:
+        err = hip_dsa_sign(private_key, msg);
+        break;
+    default:
+        HIP_ERROR("Unsupported HI algorithm (%d)\n", alg);
+        break;
+    }
+
+    _HIP_DUMP_MSG(msg);
 
 out_err:
-       if (hi_public)
-               free (hi_public);
+    if (hi_public) {
+        free(hi_public);
+    }
 
-       return err;
+    return err;
 }
 
 #undef HIP_READ_LOCK_DB

=== modified file 'hipd/hidb.c.doxyme'
--- hipd/hidb.c.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/hidb.c.doxyme  2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,7 +45,7 @@
 
 
 /**
- * hip_add_host_id 
+ * hip_add_host_id
  *
  *
  * @param db
@@ -56,94 +57,94 @@
  * @param tools
  * @param arg)
  * @param arg)
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_blind_find_local_hi 
+ * hip_blind_find_local_hi
  *
  *
  * @param nonce
  * @param test_hit
  * @param local_hit
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_build_host_id_and_signature 
+ * hip_build_host_id_and_signature
  *
  *
  * @param msg
  * @param hit
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_del_host_id 
+ * hip_del_host_id
  *
  *
  * @param db
  * @param lhi
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_for_each_hi 
+ * hip_for_each_hi
  *
  *
  * @param entry
  * @param opaq)
  * @param opaque)
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_any_localhost_dsa_public_key 
+ * hip_get_any_localhost_dsa_public_key
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_any_localhost_hit 
+ * hip_get_any_localhost_hit
  *
  *
  * @param target
  * @param algo
  * @param anon
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_any_localhost_rsa_public_key 
+ * hip_get_any_localhost_rsa_public_key
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_host_id 
+ * hip_get_host_id
  *
  *
  * @param db
  * @param hit
  * @param algo
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_host_id_and_priv_key 
+ * hip_get_host_id_and_priv_key
  *
  *
  * @param db
@@ -151,149 +152,148 @@
  * @param algo
  * @param host_id
  * @param key
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_hostid_entry_by_lhi_and_algo 
+ * hip_get_hostid_entry_by_lhi_and_algo
  *
  *
  * @param db
  * @param hit
  * @param algo
  * @param anon
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_get_public_key 
+ * hip_get_public_key
  *
  *
  * @param hid
- * @return 
- **/
-
-
-/**
- * hip_handle_add_local_hi 
- *
- *
- * @param input
- * @return 
- **/
-
-
-/**
- * hip_handle_del_local_hi 
- *
- *
- * @param input
- * @return 
- **/
-
-
-/**
- * hip_hidb_add_lsi 
+ * @return
+ **/
+
+
+/**
+ * hip_handle_add_local_hi
+ *
+ *
+ * @param input
+ * @return
+ **/
+
+
+/**
+ * hip_handle_del_local_hi
+ *
+ *
+ * @param input
+ * @return
+ **/
+
+
+/**
+ * hip_hidb_add_lsi
  *
  *
  * @param db
  * @param id_entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hidb_associate_default_hit_lsi 
+ * hip_hidb_associate_default_hit_lsi
  *
  *
  * @param default_hit
  * @param default_lsi
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hidb_exists_lsi 
+ * hip_hidb_exists_lsi
  *
  *
  * @param lsi
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hidb_get_entry_by_lsi 
+ * hip_hidb_get_entry_by_lsi
  *
  *
  * @param db
  * @param lsi
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hidb_get_lsi_by_hit 
+ * hip_hidb_get_lsi_by_hit
  *
  *
  * @param our
  * @param our_lsi
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hidb_hash 
+ * hip_hidb_hash
  *
  *
  * @param ptr
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hidb_hit_is_our 
+ * hip_hidb_hit_is_our
  *
  *
  * @param our
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_hidb_match 
+ * hip_hidb_match
  *
  *
  * @param ptr1
  * @param ptr2
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_hostid_db 
+ * hip_init_hostid_db
  *
  *
  * @param db
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_uninit_host_id_dbs 
+ * hip_uninit_host_id_dbs
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_uninit_hostid_db 
+ * hip_uninit_hostid_db
  *
  *
  * @param db
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/hidb.h'
--- hipd/hidb.h 2010-02-08 15:18:06 +0000
+++ hipd/hidb.h 2010-02-10 22:32:46 +0000
@@ -12,20 +12,20 @@
 
 #if 0
 #define HIP_READ_LOCK_DB(db) do { \
-        read_lock_irqsave(&(db)->db_lock,lf); \
-       } while(0)
+        read_lock_irqsave(&(db)->db_lock, lf); \
+} while (0)
 
 #define HIP_WRITE_LOCK_DB(db) do { \
-       write_lock_irqsave(&(db)->db_lock,lf); \
-       } while(0)
+        write_lock_irqsave(&(db)->db_lock, lf); \
+} while (0)
 
 #define HIP_READ_UNLOCK_DB(db) do { \
-       read_unlock_irqrestore(&(db)->db_lock,lf); \
-        } while(0)
+        read_unlock_irqrestore(&(db)->db_lock, lf); \
+} while (0)
 
 #define HIP_WRITE_UNLOCK_DB(db) do { \
-       write_unlock_irqrestore(&(db)->db_lock,lf); \
-        } while(0)
+        write_unlock_irqrestore(&(db)->db_lock, lf); \
+} while (0)
 #else
 #define HIP_READ_LOCK_DB(db)
 #define HIP_WRITE_LOCK_DB(db)
@@ -40,10 +40,10 @@
 #define INET6_ADDRSTRLEN 46
 
 struct hip_entry_list {
-       hip_list_t list;
-       struct in6_addr peer_hit;
-       /* These two _MUST_ be left untouched. Feel free to add more
-        * to the end */
+    hip_list_t      list;
+    struct in6_addr peer_hit;
+    /* These two _MUST_ be left untouched. Feel free to add more
+     * to the end */
 };
 
 /* Use this to point your target while accessing a database */
@@ -53,11 +53,11 @@
 extern hip_db_struct_t *hip_local_hostid_db;
 
 struct hip_host_id_entry *hip_get_hostid_entry_by_lhi_and_algo(hip_db_struct_t 
*db,
-                                                              const struct 
in6_addr *hit,
-                                                              int algo, int 
anon);
+                                                               const struct 
in6_addr *hit,
+                                                               int algo, int 
anon);
 int hip_get_any_localhost_hit(struct in6_addr *target, int algo, int anon);
 int hip_get_host_id_and_priv_key(hip_db_struct_t *db, struct in6_addr *hit,
-                        int algo, struct hip_host_id **host_id, void **key);
+                                 int algo, struct hip_host_id **host_id, void 
**key);
 int hip_hit_is_our(struct in6_addr *hit);
 
 void hip_uninit_host_id_dbs(void);
@@ -68,7 +68,7 @@
 int hip_for_each_hi(int (*func)(struct hip_host_id_entry *entry, void *opaq), 
void *opaque);
 
 int hip_blind_find_local_hi(uint16_t *nonce, struct in6_addr *test_hit,
-                           struct in6_addr *local_hit);
+                            struct in6_addr *local_hit);
 int hip_build_host_id_and_signature(struct hip_common *msg,  hip_hit_t *hit);
 /*lsi support*/
 int hip_hidb_exists_lsi(hip_lsi_t *lsi);

=== modified file 'hipd/hidb.h.doxyme'
--- hipd/hidb.h.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/hidb.h.doxyme  2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hidb.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/hipd.c'
--- hipd/hipd.c 2010-02-08 21:28:25 +0000
+++ hipd/hipd.c 2010-02-10 22:32:46 +0000
@@ -20,121 +20,122 @@
 
 
 /* Defined as a global just to allow freeing in exit(). Do not use outside
-   of this file! */
-struct hip_common *hipd_msg = NULL;
-struct hip_common *hipd_msg_v4 = NULL;
+ * of this file! */
+struct hip_common *hipd_msg          = NULL;
+struct hip_common *hipd_msg_v4       = NULL;
 
-int is_active_mhaddr = 1;  /**< Which mhaddr to use active or lazy? (default: 
active) */
-int is_hard_handover = 0;  /**< if hard handover is forced to be used 
(default: no) */
-int hip_blind_status = 0; /**< Blind status */
+int is_active_mhaddr                 = 1; /**< Which mhaddr to use active or 
lazy? (default: active) */
+int is_hard_handover                 = 0; /**< if hard handover is forced to 
be used (default: no) */
+int hip_blind_status                 = 0; /**< Blind status */
 
 /** Suppress advertising of none, AF_INET or AF_INET6 address in UPDATEs.
-    0 = none = default, AF_INET, AF_INET6 */
-int suppress_af_family = 0;
+ *  0 = none = default, AF_INET, AF_INET6 */
+int suppress_af_family               = 0;
 
 /* For sending HIP control messages */
-int hip_raw_sock_output_v6 = 0;
-int hip_raw_sock_output_v4 = 0;
+int hip_raw_sock_output_v6           = 0;
+int hip_raw_sock_output_v4           = 0;
 
 /* For receiving HIP control messages */
-int hip_raw_sock_input_v6 = 0;
-int hip_raw_sock_input_v4 = 0;
-
-/** File descriptor of the socket used for sending HIP control packet 
- *  NAT traversal on UDP/IPv4 
- */
-int hip_nat_sock_output_udp = 0;
-
-/** File descriptor of the socket used for receiving HIP control packet 
- *  NAT traversal on UDP/IPv4 
- */
-int hip_nat_sock_input_udp = 0;
-
-int hip_nat_sock_output_udp_v6 =0;
-int hip_nat_sock_input_udp_v6 = 0;
+int hip_raw_sock_input_v6            = 0;
+int hip_raw_sock_input_v4            = 0;
+
+/** File descriptor of the socket used for sending HIP control packet
+ *  NAT traversal on UDP/IPv4
+ */
+int hip_nat_sock_output_udp          = 0;
+
+/** File descriptor of the socket used for receiving HIP control packet
+ *  NAT traversal on UDP/IPv4
+ */
+int hip_nat_sock_input_udp           = 0;
+
+int hip_nat_sock_output_udp_v6       = 0;
+int hip_nat_sock_input_udp_v6        = 0;
 
 /** Specifies the NAT status of the daemon. This value indicates if the current
-    machine is behind a NAT. */
+ *  machine is behind a NAT. */
 hip_transform_suite_t hip_nat_status = 0;
 
 /** ICMPv6 socket and the interval 0 for interval means off **/
-int hip_icmp_sock = 0;
-int hip_icmp_interval = HIP_NAT_KEEP_ALIVE_INTERVAL;
+int hip_icmp_sock                    = 0;
+int hip_icmp_interval                = HIP_NAT_KEEP_ALIVE_INTERVAL;
 
-/** Specifies the HIP PROXY status of the daemon. This value indicates if the 
HIP PROXY is running. */
-int hipproxy = 0;
+/** Specifies the HIP PROXY status of the daemon.
+ *  This value indicates if the HIP PROXY is running. */
+int hipproxy                         = 0;
 
 /*SAVAH modes*/
-int hipsava_client = 0;
-int hipsava_server = 0;
+int hipsava_client                   = 0;
+int hipsava_server                   = 0;
 
 /* Encrypt host id in I2 */
-int hip_encrypt_i2_hi = 0;
+int hip_encrypt_i2_hi                = 0;
 
 /* Communication interface to userspace apps (hipconf etc) */
-int hip_user_sock = 0;
+int hip_user_sock                    = 0;
 struct sockaddr_un hip_user_addr;
 
 /** For receiving netlink IPsec events (acquire, expire, etc) */
-struct rtnl_handle hip_nl_ipsec  = {0};
+struct rtnl_handle hip_nl_ipsec      = {0};
 
 /** For getting/setting routes and adding HITs (it was not possible to use
-    nf_ipsec for this purpose). */
+ *  nf_ipsec for this purpose). */
 struct rtnl_handle hip_nl_route = { 0 };
 
-int hip_agent_status = 0;
+int hip_agent_status                     = 0;
 
 struct sockaddr_in6 hip_firewall_addr;
-int hip_firewall_sock = 0;
+int hip_firewall_sock                    = 0;
 
 /* used to change the transform order see hipconf usage to see the usage
-   This is set to AES, 3DES, NULL by default see hipconf trasform order for
-   more information.
-*/
-int hip_transform_order = 123;
+ * This is set to AES, 3DES, NULL by default see hipconf trasform order for
+ * more information.
+ */
+int hip_transform_order                  = 123;
 
 /* OpenDHT related variables */
-int hip_opendht_sock_fqdn = -1; /* FQDN->HIT mapping */
-int hip_opendht_sock_hit = -1; /* HIT->IP mapping */
-int hip_opendht_fqdn_sent = STATE_OPENDHT_IDLE;
-int hip_opendht_hit_sent = STATE_OPENDHT_IDLE;
+int hip_opendht_sock_fqdn                = -1; /* FQDN->HIT mapping */
+int hip_opendht_sock_hit                 = -1; /* HIT->IP mapping */
+int hip_opendht_fqdn_sent                = STATE_OPENDHT_IDLE;
+int hip_opendht_hit_sent                 = STATE_OPENDHT_IDLE;
 
-int dht_queue_count = 0;
-int opendht_error = 0;
+int dht_queue_count                      = 0;
+int opendht_error                        = 0;
 unsigned char opendht_response[HIP_MAX_PACKET];
-struct addrinfo * opendht_serving_gateway = NULL;
-int opendht_serving_gateway_port = OPENDHT_PORT;
-int opendht_serving_gateway_ttl = OPENDHT_TTL;
+struct addrinfo *opendht_serving_gateway = NULL;
+int opendht_serving_gateway_port         = OPENDHT_PORT;
+int opendht_serving_gateway_ttl          = OPENDHT_TTL;
 
-struct in6_addr * sava_serving_gateway = NULL;
+struct in6_addr *sava_serving_gateway    = NULL;
 
 char opendht_name_mapping[HIP_HOST_ID_HOSTNAME_LEN_MAX]; /* what name should 
be used as key */
 char opendht_host_name[256];
 unsigned char opendht_hdrr_secret[40];
-hip_common_t * opendht_current_hdrr = NULL;
+hip_common_t *opendht_current_hdrr = NULL;
 char opendht_current_key[INET6_ADDRSTRLEN + 2];
 
 /* now DHT is always off, so you have to set it on if you want to use it */
-int hip_opendht_inuse = SO_HIP_DHT_OFF;
-int hip_opendht_error_count = 0; /* Error count, counting errors from 
libhipopendht */
+int hip_opendht_inuse              = SO_HIP_DHT_OFF;
+int hip_opendht_error_count        = 0; /* Error count, counting errors from 
libhipopendht */
 
-int hip_buddies_inuse = SO_HIP_BUDDIES_OFF;
+int hip_buddies_inuse              = SO_HIP_BUDDIES_OFF;
 
 /* Tells to the daemon should it build LOCATOR parameters to R1 and I2 */
-int hip_locator_status = SO_HIP_SET_LOCATOR_OFF;
+int hip_locator_status             = SO_HIP_SET_LOCATOR_OFF;
 
 /* It tells the daemon to set tcp timeout parameters. Added By Tao Wan, on 
09.Jan.2008 */
-int hip_tcptimeout_status = SO_HIP_SET_TCPTIMEOUT_ON;
+int hip_tcptimeout_status          = SO_HIP_SET_TCPTIMEOUT_ON;
 
 /* Create /etc/hip stuff and exit (used for binary hipfw packaging) */
-int create_configs_and_exit = 0;
+int create_configs_and_exit        = 0;
 
 /* We are caching the IP addresses of the host here. The reason is that during
-   in hip_handle_acquire it is not possible to call getifaddrs (it creates
-   a new netlink socket and seems like only one can be open per process).
-   Feel free to experiment by porting the required functionality from
-   iproute2/ip/ipaddrs.c:ipaddr_list_or_flush(). It would make these global
-   variable and most of the functions referencing them unnecessary -miika */
+ * in hip_handle_acquire it is not possible to call getifaddrs (it creates
+ * a new netlink socket and seems like only one can be open per process).
+ * Feel free to experiment by porting the required functionality from
+ * iproute2/ip/ipaddrs.c:ipaddr_list_or_flush(). It would make these global
+ * variable and most of the functions referencing them unnecessary -miika */
 
 int address_count;
 HIP_HASHTABLE *addresses;
@@ -149,22 +150,22 @@
  * userspace ipsec or not. If it is 1, hip uses the user space ipsec.
  * It will not use if hip_use_userspace_ipsec = 0. Added By Tao Wan
  */
-int hip_use_userspace_ipsec = 0;
-
-int hip_use_userspace_data_packet_mode = 0 ;
-
-int esp_prot_active = 0;
-int esp_prot_num_transforms = 0;
+int hip_use_userspace_ipsec                  = 0;
+
+int hip_use_userspace_data_packet_mode       = 0;
+
+int esp_prot_active                          = 0;
+int esp_prot_num_transforms                  = 0;
 uint8_t esp_prot_transforms[MAX_NUM_TRANSFORMS];
-long esp_prot_num_parallel_hchains = 0;
+long esp_prot_num_parallel_hchains           = 0;
 
-int hip_shotgun_status = SO_HIP_SHOTGUN_OFF;
+int hip_shotgun_status                       = SO_HIP_SHOTGUN_OFF;
 
 int hip_trigger_update_on_heart_beat_failure = 1;
-int hip_wait_addr_changes_to_stabilize = 1;
+int hip_wait_addr_changes_to_stabilize       = 1;
 
-int hip_use_opptcp = 0; // false
-int hip_use_hi3    = 0; // false
+int hip_use_opptcp                           = 0; // false
+int hip_use_hi3                              = 0; // false
 #ifdef CONFIG_HIP_AGENT
 sqlite3 *daemon_db;
 #endif
@@ -175,125 +176,129 @@
 
 void hip_set_opportunistic_tcp_status(struct hip_common *msg)
 {
-       struct sockaddr_in6 sock_addr;
-       int retry, type, n;
-
-       type = hip_get_msg_type(msg);
-
-       _HIP_DEBUG("type=%d\n", type);
-
-       memset(&sock_addr, 0, sizeof(sock_addr));
-       sock_addr.sin6_family = AF_INET6;
-       sock_addr.sin6_port = htons(HIP_FIREWALL_PORT);
-       sock_addr.sin6_addr = in6addr_loopback;
-
-       for (retry = 0; retry < 3; retry++) {
-               /* Switched from hip_sendto() to hip_sendto_user() due to
-                  namespace collision. Both message.h and user.c had functions
-                  hip_sendto(). Introducing a prototype hip_sendto() to user.h
-                  led to compiler errors --> user.c hip_sendto() renamed to
-                  hip_sendto_user().
-
-                  Lesson learned: use function prototypes unless functions are
-                  ment only for local (inside the same file where defined) use.
-                  -Lauri 11.07.2008 */
-               n = hip_sendto_user(msg, (struct sockaddr *)&sock_addr);
-               if (n <= 0) {
-                       HIP_ERROR("hipconf opptcp failed (round %d)\n", retry);
-                       HIP_DEBUG("Sleeping few seconds to wait for fw\n");
-                       sleep(2);
-               } else {
-                       HIP_DEBUG("hipconf opptcp ok (sent %d bytes)\n", n);
-                       break;
-               }
-       }
-
-       if (type == SO_HIP_SET_OPPTCP_ON)
-               hip_use_opptcp = 1;
-       else
-               hip_use_opptcp = 0;
-
-       HIP_DEBUG("Opportunistic tcp set %s\n",
-                 (hip_use_opptcp ? "on" : "off"));
-}
-
-int hip_get_opportunistic_tcp_status(){
-        return hip_use_opptcp;
-}
-
+    struct sockaddr_in6 sock_addr;
+    int retry, type, n;
+
+    type = hip_get_msg_type(msg);
+
+    _HIP_DEBUG("type=%d\n", type);
+
+    memset(&sock_addr, 0, sizeof(sock_addr));
+    sock_addr.sin6_family = AF_INET6;
+    sock_addr.sin6_port   = htons(HIP_FIREWALL_PORT);
+    sock_addr.sin6_addr   = in6addr_loopback;
+
+    for (retry = 0; retry < 3; retry++) {
+        /* Switched from hip_sendto() to hip_sendto_user() due to
+         * namespace collision. Both message.h and user.c had functions
+         * hip_sendto(). Introducing a prototype hip_sendto() to user.h
+         * led to compiler errors --> user.c hip_sendto() renamed to
+         * hip_sendto_user().
+         *
+         * Lesson learned: use function prototypes unless functions are
+         * ment only for local (inside the same file where defined) use.
+         * -Lauri 11.07.2008 */
+        n = hip_sendto_user(msg, (struct sockaddr *) &sock_addr);
+        if (n <= 0) {
+            HIP_ERROR("hipconf opptcp failed (round %d)\n", retry);
+            HIP_DEBUG("Sleeping few seconds to wait for fw\n");
+            sleep(2);
+        } else {
+            HIP_DEBUG("hipconf opptcp ok (sent %d bytes)\n", n);
+            break;
+        }
+    }
+
+    if (type == SO_HIP_SET_OPPTCP_ON) {
+        hip_use_opptcp = 1;
+    } else {
+        hip_use_opptcp = 0;
+    }
+
+    HIP_DEBUG("Opportunistic tcp set %s\n",
+              (hip_use_opptcp ? "on" : "off"));
+}
+
+int hip_get_opportunistic_tcp_status()
+{
+    return hip_use_opptcp;
+}
 
 /* hi3 */
 #ifdef CONFIG_HIP_I3
-void hip_set_hi3_status(struct hip_common *msg){
-       struct sockaddr_in6 sock_addr;
-       int retry, type, n;
-
-       type = hip_get_msg_type(msg);
-
-       _HIP_DEBUG("type=%d\n", type);
-
-       bzero(&sock_addr, sizeof(sock_addr));
-       sock_addr.sin6_family = AF_INET6;
-       sock_addr.sin6_port = htons(HIP_FIREWALL_PORT);
-       sock_addr.sin6_addr = in6addr_loopback;
-
-       for (retry = 0; retry < 3; retry++) {
-               n = hip_sendto_user(msg, (struct sockaddr *)&sock_addr);
-               if (n <= 0) {
-                       HIP_ERROR("hipconf hi3 failed (round %d)\n", retry);
-                       HIP_DEBUG("Sleeping few seconds to wait for fw\n");
-                       sleep(2);
-               } else {
-                       HIP_DEBUG("hipconf hi3 ok (sent %d bytes)\n", n);
-                       break;
-               }
-       }
-
-       if(type == SO_HIP_SET_HI3_ON){
-               hip_i3_init();
-               hip_use_hi3 = 1;
-               hip_locator_status = SO_HIP_SET_LOCATOR_ON;
-       }
-       else{
-               hip_locator_status = SO_HIP_SET_LOCATOR_OFF;
-               hip_hi3_clean();
-               hip_use_hi3 = 0;
-       }
-
-       HIP_DEBUG("hi3 set %s\n",
-                 (hip_use_hi3 ? "on" : "off"));
-}
-
-int hip_get_hi3_status(){
-        return hip_use_hi3;
-}
+void hip_set_hi3_status(struct hip_common *msg)
+{
+    struct sockaddr_in6 sock_addr;
+    int retry, type, n;
+
+    type = hip_get_msg_type(msg);
+
+    _HIP_DEBUG("type=%d\n", type);
+
+    bzero(&sock_addr, sizeof(sock_addr));
+    sock_addr.sin6_family = AF_INET6;
+    sock_addr.sin6_port   = htons(HIP_FIREWALL_PORT);
+    sock_addr.sin6_addr   = in6addr_loopback;
+
+    for (retry = 0; retry < 3; retry++) {
+        n = hip_sendto_user(msg, (struct sockaddr *) &sock_addr);
+        if (n <= 0) {
+            HIP_ERROR("hipconf hi3 failed (round %d)\n", retry);
+            HIP_DEBUG("Sleeping few seconds to wait for fw\n");
+            sleep(2);
+        } else {
+            HIP_DEBUG("hipconf hi3 ok (sent %d bytes)\n", n);
+            break;
+        }
+    }
+
+    if (type == SO_HIP_SET_HI3_ON) {
+        hip_i3_init();
+        hip_use_hi3        = 1;
+        hip_locator_status = SO_HIP_SET_LOCATOR_ON;
+    } else {
+        hip_locator_status = SO_HIP_SET_LOCATOR_OFF;
+        hip_hi3_clean();
+        hip_use_hi3        = 0;
+    }
+
+    HIP_DEBUG("hi3 set %s\n",
+              (hip_use_hi3 ? "on" : "off"));
+}
+
+int hip_get_hi3_status()
+{
+    return hip_use_hi3;
+}
+
 #endif
 
-static void usage(void) {
-       fprintf(stderr, "Usage: hipd [options]\n\n");
-       fprintf(stderr, "  -b run in background\n");
-       fprintf(stderr, "  -i <device name> add interface to the white list. 
Use additional -i for additional devices.\n");
-       fprintf(stderr, "  -k kill existing hipd\n");
-       fprintf(stderr, "  -N do not flush ipsec rules on exit\n");
-       fprintf(stderr, "  -a fix alignment issues automatically(ARM)\n");
-       fprintf(stderr, "  -f set debug type format to short\n");
-       fprintf(stderr, "\n");
+static void usage(void)
+{
+    fprintf(stderr, "Usage: hipd [options]\n\n");
+    fprintf(stderr, "  -b run in background\n");
+    fprintf(stderr, "  -i <device name> add interface to the white list. Use 
additional -i for additional devices.\n");
+    fprintf(stderr, "  -k kill existing hipd\n");
+    fprintf(stderr, "  -N do not flush ipsec rules on exit\n");
+    fprintf(stderr, "  -a fix alignment issues automatically(ARM)\n");
+    fprintf(stderr, "  -f set debug type format to short\n");
+    fprintf(stderr, "\n");
 }
 
 int hip_send_agent(struct hip_common *msg)
 {
-        struct sockaddr_in6 hip_agent_addr;
-        int alen;
-
-        memset(&hip_agent_addr, 0, sizeof(hip_agent_addr));
-        hip_agent_addr.sin6_family = AF_INET6;
-        hip_agent_addr.sin6_addr = in6addr_loopback;
-        hip_agent_addr.sin6_port = htons(HIP_AGENT_PORT);
-
-        alen = sizeof(hip_agent_addr);
-
-        return sendto(hip_user_sock, msg, hip_get_msg_total_len(msg), 0,
-                       (struct sockaddr *)&hip_agent_addr, alen);
+    struct sockaddr_in6 hip_agent_addr;
+    int alen;
+
+    memset(&hip_agent_addr, 0, sizeof(hip_agent_addr));
+    hip_agent_addr.sin6_family = AF_INET6;
+    hip_agent_addr.sin6_addr   = in6addr_loopback;
+    hip_agent_addr.sin6_port   = htons(HIP_AGENT_PORT);
+
+    alen                       = sizeof(hip_agent_addr);
+
+    return sendto(hip_user_sock, msg, hip_get_msg_total_len(msg), 0,
+                  (struct sockaddr *) &hip_agent_addr, alen);
 }
 
 #ifdef CONFIG_HIP_AGENT
@@ -303,616 +308,613 @@
  * @param *uadb_info structure containing data sent by the agent
  * @return 0 on success, -1 on failure
  */
-int add_cert_and_hits_to_db (struct hip_uadb_info *uadb_info)
+int add_cert_and_hits_to_db(struct hip_uadb_info *uadb_info)
 {
-       int err = 0 ;
-       char insert_into[512];
-       char hit[40];
-       char hit2[40];
+    int err = 0;
+    char insert_into[512];
+    char hit[40];
+    char hit2[40];
 
-       HIP_IFE(!daemon_db, -1);
-       hip_in6_ntop(&uadb_info->hitr, hit);
-       hip_in6_ntop(&uadb_info->hitl, hit2);
-       _HIP_DEBUG("Value: %s\n", hit);
-       sprintf(insert_into, "INSERT INTO hits VALUES("
-                        "'%s', '%s', '%s');",
-                        hit2, hit, uadb_info->cert);
+    HIP_IFE(!daemon_db, -1);
+    hip_in6_ntop(&uadb_info->hitr, hit);
+    hip_in6_ntop(&uadb_info->hitl, hit2);
+    _HIP_DEBUG("Value: %s\n", hit);
+    sprintf(insert_into, "INSERT INTO hits VALUES("
+                         "'%s', '%s', '%s');",
+            hit2, hit, uadb_info->cert);
     err = hip_sqlite_insert_into_table(daemon_db, insert_into);
 
 out_err:
-       return (err) ;
+    return err;
 }
-#endif /* CONFIG_HIP_AGENT */
+
+#endif  /* CONFIG_HIP_AGENT */
 
 /**
  * Receive message from agent socket.
  */
 int hip_recv_agent(struct hip_common *msg)
 {
-       int n, err = 0;
-       hip_hdr_type_t msg_type;
+    int n, err = 0;
+    hip_hdr_type_t msg_type;
 #ifdef CONFIG_HIP_AGENT
-       char hit[40];
-       struct hip_uadb_info *uadb_info ;
-#endif /* CONFIG_HIP_AGENT */
-
-       HIP_DEBUG("Received a message from agent\n");
-
-       msg_type = hip_get_msg_type(msg);
-
-       if (msg_type == SO_HIP_AGENT_PING)
-       {
-               hip_msg_init(msg);
-               hip_build_user_hdr(msg, SO_HIP_AGENT_PING_REPLY, 0);
-               n = hip_send_agent(msg);
-               HIP_IFEL(n < 0, 0, "sendto() failed on agent socket\n");
-
-               if (err == 0)
-               {
-                       HIP_DEBUG("HIP agent ok.\n");
-                       if (hip_agent_status == 0)
-                       {
-                               hip_agent_status = 1;
-                               hip_agent_update();
-                       }
-                       hip_agent_status = 1;
-               }
-       }
-       else if (msg_type == SO_HIP_AGENT_QUIT)
-       {
-               HIP_DEBUG("Agent quit.\n");
-               hip_agent_status = 0;
-       }
-       else if (msg_type == HIP_R1 || msg_type == HIP_I1)
-       {
-               struct hip_common *emsg;
-               struct in6_addr *src_addr, *dst_addr;
-               hip_portpair_t *msg_info;
-               void *reject;
-
-               emsg = hip_get_param_contents(msg, HIP_PARAM_ENCAPS_MSG);
-               src_addr = hip_get_param_contents(msg, HIP_PARAM_SRC_ADDR);
-               dst_addr = hip_get_param_contents(msg, HIP_PARAM_DST_ADDR);
-               msg_info = hip_get_param_contents(msg, HIP_PARAM_PORTPAIR);
-               reject = hip_get_param(msg, HIP_PARAM_AGENT_REJECT);
-
-               if (emsg && src_addr && dst_addr && msg_info && !reject)
-               {
-                       HIP_DEBUG("Received accepted I1/R1 packet from 
agent.\n");
-                       hip_receive_control_packet(emsg, src_addr, dst_addr, 
msg_info, 0);
-               }
-               else if (emsg && src_addr && dst_addr && msg_info)
-               {
+    char hit[40];
+    struct hip_uadb_info *uadb_info;
+#endif  /* CONFIG_HIP_AGENT */
+
+    HIP_DEBUG("Received a message from agent\n");
+
+    msg_type = hip_get_msg_type(msg);
+
+    if (msg_type == SO_HIP_AGENT_PING) {
+        hip_msg_init(msg);
+        hip_build_user_hdr(msg, SO_HIP_AGENT_PING_REPLY, 0);
+        n = hip_send_agent(msg);
+        HIP_IFEL(n < 0, 0, "sendto() failed on agent socket\n");
+
+        if (err == 0) {
+            HIP_DEBUG("HIP agent ok.\n");
+            if (hip_agent_status == 0) {
+                hip_agent_status = 1;
+                hip_agent_update();
+            }
+            hip_agent_status = 1;
+        }
+    } else if (msg_type == SO_HIP_AGENT_QUIT)   {
+        HIP_DEBUG("Agent quit.\n");
+        hip_agent_status = 0;
+    } else if (msg_type == HIP_R1 || msg_type == HIP_I1)   {
+        struct hip_common *emsg;
+        struct in6_addr *src_addr, *dst_addr;
+        hip_portpair_t *msg_info;
+        void *reject;
+
+        emsg     = hip_get_param_contents(msg, HIP_PARAM_ENCAPS_MSG);
+        src_addr = hip_get_param_contents(msg, HIP_PARAM_SRC_ADDR);
+        dst_addr = hip_get_param_contents(msg, HIP_PARAM_DST_ADDR);
+        msg_info = hip_get_param_contents(msg, HIP_PARAM_PORTPAIR);
+        reject   = hip_get_param(msg, HIP_PARAM_AGENT_REJECT);
+
+        if (emsg && src_addr && dst_addr && msg_info && !reject) {
+            HIP_DEBUG("Received accepted I1/R1 packet from agent.\n");
+            hip_receive_control_packet(emsg, src_addr, dst_addr, msg_info, 0);
+        } else if (emsg && src_addr && dst_addr && msg_info)   {
 #ifdef CONFIG_HIP_OPPORTUNISTIC
 
-                       HIP_DEBUG("Received rejected R1 packet from agent.\n");
-                       err = hip_for_each_opp(hip_handle_opp_reject, src_addr);
-                       HIP_IFEL(err, 0, "for_each_ha err.\n");
+            HIP_DEBUG("Received rejected R1 packet from agent.\n");
+            err = hip_for_each_opp(hip_handle_opp_reject, src_addr);
+            HIP_IFEL(err, 0, "for_each_ha err.\n");
 #endif
-               }
+        }
 #ifdef CONFIG_HIP_AGENT
-               /*Store the accepted HIT info from agent*/
-               uadb_info = hip_get_param(msg, HIP_PARAM_UADB_INFO);
-               if (uadb_info)
-               {
-                       HIP_DEBUG("Received User Agent accepted HIT info from 
agent.\n");
-                       hip_in6_ntop(&uadb_info->hitl, hit);
-               _HIP_DEBUG("Value: %s\n", hit);
-               add_cert_and_hits_to_db(uadb_info);
-               }
-#endif /* CONFIG_HIP_AGENT */
-       }
+        /*Store the accepted HIT info from agent*/
+        uadb_info = hip_get_param(msg, HIP_PARAM_UADB_INFO);
+        if (uadb_info) {
+            HIP_DEBUG("Received User Agent accepted HIT info from agent.\n");
+            hip_in6_ntop(&uadb_info->hitl, hit);
+            _HIP_DEBUG("Value: %s\n", hit);
+            add_cert_and_hits_to_db(uadb_info);
+        }
+#endif  /* CONFIG_HIP_AGENT */
+    }
 
 out_err:
-       return err;
+    return err;
 }
 
-int hip_sendto_firewall(const struct hip_common *msg){
+int hip_sendto_firewall(const struct hip_common *msg)
+{
 #ifdef CONFIG_HIP_FIREWALL
-        int n = 0;
-       HIP_DEBUG("CONFIG_HIP_FIREWALL DEFINED AND STATUS IS %d\n", 
hip_get_firewall_status());
-       struct sockaddr_in6 hip_firewall_addr;
-       socklen_t alen = sizeof(hip_firewall_addr);
-
-       bzero(&hip_firewall_addr, alen);
-       hip_firewall_addr.sin6_family = AF_INET6;
-       hip_firewall_addr.sin6_port = htons(HIP_FIREWALL_PORT);
-       hip_firewall_addr.sin6_addr = in6addr_loopback;
-
-       n = sendto(hip_firewall_sock, msg, hip_get_msg_total_len(msg),
-                          0, (struct sockaddr *)&hip_firewall_addr, alen);
-       return n;
+    int n          = 0;
+    HIP_DEBUG("CONFIG_HIP_FIREWALL DEFINED AND STATUS IS %d\n", 
hip_get_firewall_status());
+    struct sockaddr_in6 hip_firewall_addr;
+    socklen_t alen = sizeof(hip_firewall_addr);
+
+    bzero(&hip_firewall_addr, alen);
+    hip_firewall_addr.sin6_family = AF_INET6;
+    hip_firewall_addr.sin6_port   = htons(HIP_FIREWALL_PORT);
+    hip_firewall_addr.sin6_addr   = in6addr_loopback;
+
+    n = sendto(hip_firewall_sock,
+               msg,
+               hip_get_msg_total_len(msg),
+               0,
+               (struct sockaddr *) &hip_firewall_addr,
+               alen);
+    return n;
 #else
-       HIP_DEBUG("Firewall is disabled.\n");
-       return 0;
+    HIP_DEBUG("Firewall is disabled.\n");
+    return 0;
 #endif // CONFIG_HIP_FIREWALL
 }
 
-
 /**
  * Daemon main function.
  */
 static int hipd_main(int argc, char *argv[])
 {
-       int ch, killold = 0;
-       //      char buff[HIP_MAX_NETLINK_PACKET];
-       fd_set read_fdset;
-       fd_set write_fdset;
-       int foreground = 1, highest_descriptor = 0, err = 0, fix_alignment = 0;
-       struct timeval timeout;
-
-
-        /* The flushing is enabled by default. The reason for this is that
-          people are doing some very experimental features on some branches
-          that may crash the daemon and leave the SAs floating around to
-          disturb further base exchanges. Use -N flag to disable this. */
-       int flush_ipsec = 1;
+    int ch, killold = 0;
+    // char buff[HIP_MAX_NETLINK_PACKET];
+    fd_set read_fdset;
+    fd_set write_fdset;
+    int foreground = 1, highest_descriptor = 0, err = 0, fix_alignment = 0;
+    struct timeval timeout;
+
+
+    /* The flushing is enabled by default. The reason for this is that
+     * people are doing some very experimental features on some branches
+     * that may crash the daemon and leave the SAs floating around to
+     * disturb further base exchanges. Use -N flag to disable this. */
+    int flush_ipsec = 1;
 
 
 #ifdef CONFIG_HIP_PERFORMANCE
-       int bench_set = 0;
-       HIP_DEBUG("Creating perf set\n");
-       perf_set = hip_perf_create(PERF_MAX);
-
-       check_and_create_dir("results", DEFAULT_CONFIG_DIR_MODE);
-       
-       hip_perf_set_name(perf_set, PERF_I1_SEND, "results/PERF_I1_SEND.csv");
-       hip_perf_set_name(perf_set, PERF_I1,"results/PERF_I1.csv");
-       hip_perf_set_name(perf_set, PERF_R1,"results/PERF_R1.csv");
-       hip_perf_set_name(perf_set, PERF_I2,"results/PERF_I2.csv");
-       hip_perf_set_name(perf_set, PERF_R2,"results/PERF_R2.csv");
-       hip_perf_set_name(perf_set, 
PERF_DH_CREATE,"results/PERF_DH_CREATE.csv");
-       hip_perf_set_name(perf_set, PERF_SIGN,"results/PERF_SIGN.csv");
-       hip_perf_set_name(perf_set, 
PERF_DSA_SIGN_IMPL,"results/PERF_DSA_SIGN_IMPL.csv");
-       hip_perf_set_name(perf_set, PERF_VERIFY,"results/PERF_VERIFY.csv");
-       hip_perf_set_name(perf_set, PERF_BASE,"results/PERF_BASE.csv");
-       hip_perf_set_name(perf_set, PERF_ALL,"results/PERF_ALL.csv");
-       hip_perf_set_name(perf_set, 
PERF_UPDATE_SEND,"results/PERF_UPDATE_SEND.csv");
-       hip_perf_set_name(perf_set, 
PERF_VERIFY_UPDATE,"results/PERF_VERIFY_UPDATE.csv");
-       hip_perf_set_name(perf_set, 
PERF_UPDATE_COMPLETE,"results/PERF_UPDATE_COMPLETE.csv");
-       hip_perf_set_name(perf_set, 
PERF_HANDLE_UPDATE_ESTABLISHED,"results/PERF_HANDLE_UPDATE_ESTABLISHED.csv");
-       hip_perf_set_name(perf_set, 
PERF_HANDLE_UPDATE_REKEYING,"results/PERF_HANDLE_UPDATE_REKEYING.csv");
-       hip_perf_set_name(perf_set, 
PERF_UPDATE_FINISH_REKEYING,"results/PERF_UPDATE_FINISH_REKEYING.csv");
-       hip_perf_set_name(perf_set, 
PERF_CLOSE_SEND,"results/PERF_CLOSE_SEND.csv");
-       hip_perf_set_name(perf_set, 
PERF_HANDLE_CLOSE,"results/PERF_HANDLE_CLOSE.csv");
-       hip_perf_set_name(perf_set, 
PERF_HANDLE_CLOSE_ACK,"results/PERF_HANDLE_CLOSE_ACK.csv");
-       hip_perf_set_name(perf_set, 
PERF_HANDLE_UPDATE_1,"results/PERF_HANDLE_UPDATE_1.csv");
-       hip_perf_set_name(perf_set, 
PERF_HANDLE_UPDATE_2,"results/PERF_HANDLE_UPDATE_2.csv");
-       hip_perf_set_name(perf_set, 
PERF_CLOSE_COMPLETE,"results/PERF_CLOSE_COMPLETE.csv");
-       hip_perf_set_name(perf_set, 
PERF_DSA_VERIFY_IMPL,"results/PERF_DSA_VERIFY_IMPL.csv");
-       hip_perf_set_name(perf_set, 
PERF_RSA_VERIFY_IMPL,"results/PERF_RSA_VERIFY_IMPL.csv");
-       hip_perf_set_name(perf_set, 
PERF_RSA_SIGN_IMPL,"results/PERF_RSA_SIGN_IMPL.csv");
-       hip_perf_open(perf_set);
+    int bench_set   = 0;
+    HIP_DEBUG("Creating perf set\n");
+    perf_set = hip_perf_create(PERF_MAX);
+
+    check_and_create_dir("results", DEFAULT_CONFIG_DIR_MODE);
+
+    hip_perf_set_name(perf_set, PERF_I1_SEND, "results/PERF_I1_SEND.csv");
+    hip_perf_set_name(perf_set, PERF_I1, "results/PERF_I1.csv");
+    hip_perf_set_name(perf_set, PERF_R1, "results/PERF_R1.csv");
+    hip_perf_set_name(perf_set, PERF_I2, "results/PERF_I2.csv");
+    hip_perf_set_name(perf_set, PERF_R2, "results/PERF_R2.csv");
+    hip_perf_set_name(perf_set, PERF_DH_CREATE, "results/PERF_DH_CREATE.csv");
+    hip_perf_set_name(perf_set, PERF_SIGN, "results/PERF_SIGN.csv");
+    hip_perf_set_name(perf_set, PERF_DSA_SIGN_IMPL, 
"results/PERF_DSA_SIGN_IMPL.csv");
+    hip_perf_set_name(perf_set, PERF_VERIFY, "results/PERF_VERIFY.csv");
+    hip_perf_set_name(perf_set, PERF_BASE, "results/PERF_BASE.csv");
+    hip_perf_set_name(perf_set, PERF_ALL, "results/PERF_ALL.csv");
+    hip_perf_set_name(perf_set, PERF_UPDATE_SEND, 
"results/PERF_UPDATE_SEND.csv");
+    hip_perf_set_name(perf_set, PERF_VERIFY_UPDATE, 
"results/PERF_VERIFY_UPDATE.csv");
+    hip_perf_set_name(perf_set, PERF_UPDATE_COMPLETE, 
"results/PERF_UPDATE_COMPLETE.csv");
+    hip_perf_set_name(perf_set, PERF_HANDLE_UPDATE_ESTABLISHED, 
"results/PERF_HANDLE_UPDATE_ESTABLISHED.csv");
+    hip_perf_set_name(perf_set, PERF_HANDLE_UPDATE_REKEYING, 
"results/PERF_HANDLE_UPDATE_REKEYING.csv");
+    hip_perf_set_name(perf_set, PERF_UPDATE_FINISH_REKEYING, 
"results/PERF_UPDATE_FINISH_REKEYING.csv");
+    hip_perf_set_name(perf_set, PERF_CLOSE_SEND, 
"results/PERF_CLOSE_SEND.csv");
+    hip_perf_set_name(perf_set, PERF_HANDLE_CLOSE, 
"results/PERF_HANDLE_CLOSE.csv");
+    hip_perf_set_name(perf_set, PERF_HANDLE_CLOSE_ACK, 
"results/PERF_HANDLE_CLOSE_ACK.csv");
+    hip_perf_set_name(perf_set, PERF_HANDLE_UPDATE_1, 
"results/PERF_HANDLE_UPDATE_1.csv");
+    hip_perf_set_name(perf_set, PERF_HANDLE_UPDATE_2, 
"results/PERF_HANDLE_UPDATE_2.csv");
+    hip_perf_set_name(perf_set, PERF_CLOSE_COMPLETE, 
"results/PERF_CLOSE_COMPLETE.csv");
+    hip_perf_set_name(perf_set, PERF_DSA_VERIFY_IMPL, 
"results/PERF_DSA_VERIFY_IMPL.csv");
+    hip_perf_set_name(perf_set, PERF_RSA_VERIFY_IMPL, 
"results/PERF_RSA_VERIFY_IMPL.csv");
+    hip_perf_set_name(perf_set, PERF_RSA_SIGN_IMPL, 
"results/PERF_RSA_SIGN_IMPL.csv");
+    hip_perf_open(perf_set);
 #endif
 
-       /* default is long format */
-       hip_set_logfmt(LOGFMT_LONG);
-
-       /* Parse command-line options */
-       while ((ch = getopt(argc, argv, ":bi:kNchaf")) != -1)
-       {
-               switch (ch)
-               {
-               case 'b':
-                       foreground = 0;
-                       break;
-               case 'i':
-                       if(hip_netdev_white_list_add(optarg))
-                               HIP_INFO("Successfully added device <%s> to 
white list.\n",optarg);
-                       else
-                               HIP_DIE("Error adding device <%s> to white 
list. Dying...\n",optarg);   
-               
-                       break;
-               case 'k':
-                       killold = 1;
-                       break;
-               case 'N':
-                       flush_ipsec = 0;
-                       break;
-               case 'c':
-                       create_configs_and_exit = 1;
-                       break;
-               case 'a':
-                       fix_alignment = 1;
-                       break;
-               case 'f':
-                       HIP_INFO("Setting output format to short\n");
-                       hip_set_logfmt(LOGFMT_SHORT);
-                       break;
-               case '?':
-               case 'h':
-               default:
-                       usage();
-                       return err;
-               }
-       }
-
-       if(fix_alignment)
-       {
-               HIP_DEBUG("Setting alignment traps to 3(fix+ warn)\n");
-               if ( system("echo 3 > /proc/cpu/alignment == -1") ) {
-                       HIP_ERROR("Setting alignment traps failed.");
-               }
-       }
-
-       /* Configuration is valid! Fork a daemon, if so configured */
-       if (foreground)
-       {
-               hip_set_logtype(LOGTYPE_STDERR);
-               HIP_DEBUG("foreground\n");
-       }
-       else
-       {
-               hip_set_logtype(LOGTYPE_SYSLOG);
-               if (fork() > 0)
-                       return(0);
-       }
-
-       HIP_INFO("hipd pid=%d starting\n", getpid());
-       time(&load_time);
-       
-       /* Default initialization function. */
-       HIP_IFEL(hipd_init(flush_ipsec, killold), 1, "hipd_init() failed!\n");
-
-       HIP_IFEL(create_configs_and_exit, 0,
-                "Configs created, exiting\n");
-
-       highest_descriptor = maxof(9, 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_opendht_sock_fqdn, hip_opendht_sock_hit,
-                                  hip_icmp_sock);
-
-       /* Allocate user message. */
-       HIP_IFE(!(hipd_msg = hip_msg_alloc()), 1);
-        HIP_IFE(!(hipd_msg_v4 = hip_msg_alloc()), 1);
-       HIP_DEBUG("Daemon running. Entering select loop.\n");
-
-       /* Enter to the select-loop */
-       HIP_DEBUG_GL(HIP_DEBUG_GROUP_INIT,
-                    HIP_DEBUG_LEVEL_INFORMATIVE,
-                    "Hipd daemon running.\n"
-                    "Starting select loop.\n");
-       hipd_set_state(HIPD_STATE_EXEC);
-       while (hipd_get_state() != HIPD_STATE_CLOSED)
-       {
-               /* prepare file descriptor sets */
-               if (hip_opendht_inuse == SO_HIP_DHT_ON) {
-                        FD_ZERO(&write_fdset);
-                        if (hip_opendht_fqdn_sent == 
STATE_OPENDHT_WAITING_CONNECT)
-                                FD_SET(hip_opendht_sock_fqdn, &write_fdset);
-                        if (hip_opendht_hit_sent == 
STATE_OPENDHT_WAITING_CONNECT)
-                                FD_SET(hip_opendht_sock_hit, &write_fdset);
-               }
-               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_firewall_sock = hip_user_sock;
-
-               if (hip_opendht_fqdn_sent == STATE_OPENDHT_WAITING_ANSWER)
-                       FD_SET(hip_opendht_sock_fqdn, &read_fdset);
-               if (hip_opendht_hit_sent == STATE_OPENDHT_WAITING_ANSWER)
-                       FD_SET(hip_opendht_sock_hit, &read_fdset);
-
-               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 */
+    /* default is long format */
+    hip_set_logfmt(LOGFMT_LONG);
+
+    /* Parse command-line options */
+    while ((ch = getopt(argc, argv, ":bi:kNchaf")) != -1) {
+        switch (ch) {
+        case 'b':
+            foreground = 0;
+            break;
+        case 'i':
+            if (hip_netdev_white_list_add(optarg)) {
+                HIP_INFO("Successfully added device <%s> to white list.\n", 
optarg);
+            } else {
+                HIP_DIE("Error adding device <%s> to white list. Dying...\n", 
optarg);
+            }
+
+            break;
+        case 'k':
+            killold                 = 1;
+            break;
+        case 'N':
+            flush_ipsec             = 0;
+            break;
+        case 'c':
+            create_configs_and_exit = 1;
+            break;
+        case 'a':
+            fix_alignment           = 1;
+            break;
+        case 'f':
+            HIP_INFO("Setting output format to short\n");
+            hip_set_logfmt(LOGFMT_SHORT);
+            break;
+        case '?':
+        case 'h':
+        default:
+            usage();
+            return err;
+        }
+    }
+
+    if (fix_alignment) {
+        HIP_DEBUG("Setting alignment traps to 3(fix+ warn)\n");
+        if (system("echo 3 > /proc/cpu/alignment == -1")) {
+            HIP_ERROR("Setting alignment traps failed.");
+        }
+    }
+
+    /* Configuration is valid! Fork a daemon, if so configured */
+    if (foreground) {
+        hip_set_logtype(LOGTYPE_STDERR);
+        HIP_DEBUG("foreground\n");
+    } else {
+        hip_set_logtype(LOGTYPE_SYSLOG);
+        if (fork() > 0) {
+            return 0;
+        }
+    }
+
+    HIP_INFO("hipd pid=%d starting\n", getpid());
+    time(&load_time);
+
+    /* Default initialization function. */
+    HIP_IFEL(hipd_init(flush_ipsec, killold), 1, "hipd_init() failed!\n");
+
+    HIP_IFEL(create_configs_and_exit, 0,
+             "Configs created, exiting\n");
+
+    highest_descriptor = maxof(9, 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_opendht_sock_fqdn, hip_opendht_sock_hit,
+                               hip_icmp_sock);
+
+    /* Allocate user message. */
+    HIP_IFE(!(hipd_msg = hip_msg_alloc()), 1);
+    HIP_IFE(!(hipd_msg_v4 = hip_msg_alloc()), 1);
+    HIP_DEBUG("Daemon running. Entering select loop.\n");
+
+    /* Enter to the select-loop */
+    HIP_DEBUG_GL(HIP_DEBUG_GROUP_INIT,
+                 HIP_DEBUG_LEVEL_INFORMATIVE,
+                 "Hipd daemon running.\n"
+                 "Starting select loop.\n");
+    hipd_set_state(HIPD_STATE_EXEC);
+    while (hipd_get_state() != HIPD_STATE_CLOSED) {
+        /* prepare file descriptor sets */
+        if (hip_opendht_inuse == SO_HIP_DHT_ON) {
+            FD_ZERO(&write_fdset);
+            if (hip_opendht_fqdn_sent == STATE_OPENDHT_WAITING_CONNECT) {
+                FD_SET(hip_opendht_sock_fqdn, &write_fdset);
+            }
+            if (hip_opendht_hit_sent == STATE_OPENDHT_WAITING_CONNECT) {
+                FD_SET(hip_opendht_sock_hit, &write_fdset);
+            }
+        }
+        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_firewall_sock = hip_user_sock;
+
+        if (hip_opendht_fqdn_sent == STATE_OPENDHT_WAITING_ANSWER) {
+            FD_SET(hip_opendht_sock_fqdn, &read_fdset);
+        }
+        if (hip_opendht_hit_sent == STATE_OPENDHT_WAITING_ANSWER) {
+            FD_SET(hip_opendht_sock_hit, &read_fdset);
+        }
+
+        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);
-                       err = hip_build_user_hdr(hipd_msg, 
SO_HIP_FIREWALL_STATUS, 0);
-                       if (err) {
-                               HIP_ERROR("hip_build_user_hdr\n");
-                       } else {
-                               hip_firewall_status = 0;
-                               HIP_DEBUG("sent %d bytes to firewall\n",
-                                               hip_sendto_firewall(hipd_msg));
-                       }
-               }
-#endif
-
-               /* If DHT is on have to use write sets for asynchronic 
communication */
-               if (hip_opendht_inuse == SO_HIP_DHT_ON) 
-               {
-                       err = select((highest_descriptor + 1), &read_fdset,
-                                               &write_fdset, NULL, &timeout);
-               }
-               else
-               {
-                       err = select((highest_descriptor + 1), &read_fdset,
-                                               NULL, NULL, &timeout);
-               }
-
-               if (err < 0) 
-               {
-                       HIP_ERROR("select() error: %s.\n", strerror(errno));
-                       goto to_maintenance;
-               } else if (err == 0) 
-               {
-                       /* idle cycle - select() timeout */
-                       _HIP_DEBUG("Idle.\n");
-                       goto to_maintenance;
-               }
-#ifdef CONFIG_HIP_PERFORMANCE
-               if(bench_set){ //1 = true; 0 = false
-                       HIP_DEBUG("Stop and write PERF_ALL\n");
-                       hip_perf_stop_benchmark(perf_set, PERF_ALL);
-                       hip_perf_write_benchmark(perf_set, PERF_ALL);
-                       bench_set  = 0;
-               }
-#endif
-
-                 /* see bugzilla bug id 392 to see why */
-                if (FD_ISSET(hip_raw_sock_input_v6, &read_fdset) &&
-                    FD_ISSET(hip_raw_sock_input_v4, &read_fdset)) {
-                    int type, err_v6 = 0, err_v4 = 0;
-                    struct in6_addr saddr, daddr;
-                    struct in6_addr saddr_v4, daddr_v4;
-                    hip_portpair_t pkt_info;
-                    HIP_DEBUG("Receiving messages on raw HIP from IPv6/HIP and 
IPv4/HIP\n");
-                    hip_msg_init(hipd_msg);
-                    hip_msg_init(hipd_msg_v4);
-                    err_v4 = hip_read_control_msg_v4(hip_raw_sock_input_v4, 
hipd_msg_v4,
-                                                     &saddr_v4, &daddr_v4,
-                                                     &pkt_info, IPV4_HDR_SIZE);
-                    err_v6 = hip_read_control_msg_v6(hip_raw_sock_input_v6, 
hipd_msg,
-                                                     &saddr, &daddr, 
&pkt_info, 0);
-                    if (err_v4 > -1) {
-                        type = hip_get_msg_type(hipd_msg_v4);
-                        if (type == HIP_R2) {
-                               err = hip_receive_control_packet(hipd_msg_v4, 
&saddr_v4,
-                                                             &daddr_v4, 
&pkt_info, 1);
-                            if (err) 
HIP_ERROR("hip_receive_control_packet()!\n");
-                            err = hip_receive_control_packet(hipd_msg, &saddr, 
&daddr,
-                                                             &pkt_info, 1);
-                            if (err) 
HIP_ERROR("hip_receive_control_packet()!\n");
-                        } else {
-                            err = hip_receive_control_packet(hipd_msg, &saddr, 
&daddr,
-                                                             &pkt_info, 1);
-                            if (err) 
HIP_ERROR("hip_receive_control_packet()!\n");
-                            err = hip_receive_control_packet(hipd_msg_v4, 
&saddr_v4,
-                                                             &daddr_v4, 
&pkt_info, 1);
-                            if (err) 
HIP_ERROR("hip_receive_control_packet()!\n");
-                        }
-                    }
-                } else {
-                    if (FD_ISSET(hip_raw_sock_input_v6, &read_fdset)) {
-#ifdef CONFIG_HIP_PERFORMANCE
-                       HIP_DEBUG("Start PERF_ALL\n");
-                       bench_set = 1; //1 = true; 0 = false
-                       hip_perf_start_benchmark(perf_set, PERF_ALL);
-#endif
-                       
-                        /* Receiving of a raw HIP message from IPv6 socket. */
-                       struct in6_addr saddr, daddr;
-                       hip_portpair_t pkt_info;
-                       HIP_DEBUG("Receiving a message on raw HIP from "\
-                                 "IPv6/HIP socket (file descriptor: %d).\n",
-                                 hip_raw_sock_input_v6);
-                       hip_msg_init(hipd_msg);
-                       if (hip_read_control_msg_v6(hip_raw_sock_input_v6, 
hipd_msg,
-                                                   &saddr, &daddr, &pkt_info, 
0)) {
-                            HIP_ERROR("Reading network msg failed\n");
-                       } else {
-                            err = hip_receive_control_packet(hipd_msg, &saddr, 
&daddr, &pkt_info, 1);
-                            if (err) 
HIP_ERROR("hip_receive_control_packet()!\n");
-                       }
-                    }
-
-                    if (FD_ISSET(hip_raw_sock_input_v4, &read_fdset)){
-                       HIP_DEBUG("HIP RAW SOCKET\n");
-                       /* Receiving of a raw HIP message from IPv4 socket. */
-                       struct in6_addr saddr, daddr;
-                       hip_portpair_t pkt_info;
-                       HIP_DEBUG("Receiving a message on raw HIP from "\
-                                 "IPv4/HIP socket (file descriptor: %d).\n",
-                                 hip_raw_sock_input_v4);
-                       hip_msg_init(hipd_msg);
-                       HIP_DEBUG("Getting a msg on v4\n");
-                       /* Assuming that IPv4 header does not include any
-                          options */
-                       if (hip_read_control_msg_v4(hip_raw_sock_input_v4, 
hipd_msg,
-                                                   &saddr, &daddr, &pkt_info, 
IPV4_HDR_SIZE)) {
-                            HIP_ERROR("Reading network msg failed\n");
-                       } else {
-                            err = hip_receive_control_packet(hipd_msg, &saddr, 
&daddr, &pkt_info, 1);
-                            if (err) 
HIP_ERROR("hip_receive_control_packet()!\n");
-                       }
-
-                    }
-                }
-
-
-               if (FD_ISSET(hip_icmp_sock, &read_fdset))
-               {
-                       HIP_IFEL(hip_icmp_recvmsg(hip_icmp_sock), -1,
-                                "Failed to recvmsg from ICMPv6\n");
-               }
-
-               if (FD_ISSET(hip_nat_sock_input_udp, &read_fdset))
-               {
-                       /* Data structures for storing the source and
-                          destination addresses and ports of the incoming
-                          packet. */
-                       struct in6_addr saddr, daddr;
-                       hip_portpair_t pkt_info;
-
-                       /* Receiving of a UDP message from NAT socket. */
-                       HIP_DEBUG("Receiving a message on UDP from NAT "\
-                                 "socket (file descriptor: %d).\n",
-                                 hip_nat_sock_input_udp);
-
-                       /* Initialization of the hip_common header struct. We'll
-                          store the HIP header data here. */
-                       hip_msg_init(hipd_msg);
-
-                       /* Read in the values to hip_msg, saddr, daddr and
-                          pkt_info. */
-                       /* if ( hip_read_control_msg_v4(hip_nat_sock_udp, 
hipd_msg,&saddr, &daddr,&pkt_info, 0) ) */
-                       err = hip_read_control_msg_v4(hip_nat_sock_input_udp, 
hipd_msg,&saddr, &daddr,&pkt_info, HIP_UDP_ZERO_BYTES_LEN);
-                       if (err)
-                       {
-                                HIP_ERROR("Reading network msg failed\n");
-
-
-                               /* If the values were read in succesfully, we
-                                  do the UDP specific stuff next. */
-                        }
-                       else
-                       {
-                          err =  hip_receive_udp_control_packet(hipd_msg, 
&saddr, &daddr, &pkt_info);
-                        }
-
-               }
-       
-               if (FD_ISSET(hip_user_sock, &read_fdset))
-               {
-                       /* Receiving of a message from user socket. */
-                       struct sockaddr_in6 app_src;
-
-                       HIP_DEBUG("Receiving user message.\n");
-
-                       hip_msg_init(hipd_msg);
-                       
-                       if (hip_read_user_control_msg(hip_user_sock, hipd_msg, 
&app_src)) {
-                               HIP_ERROR("Reading user msg failed\n");
-                       }
-                       else {
-                               err = hip_handle_user_msg(hipd_msg, &app_src);
-                       }
-               }
+        if (hip_firewall_status < 0) {
+            hip_msg_init(hipd_msg);
+            err = hip_build_user_hdr(hipd_msg, SO_HIP_FIREWALL_STATUS, 0);
+            if (err) {
+                HIP_ERROR("hip_build_user_hdr\n");
+            } else {
+                hip_firewall_status = 0;
+                HIP_DEBUG("sent %d bytes to firewall\n",
+                          hip_sendto_firewall(hipd_msg));
+            }
+        }
+#endif
+
+        /* If DHT is on have to use write sets for asynchronic communication */
+        if (hip_opendht_inuse == SO_HIP_DHT_ON) {
+            err = select((highest_descriptor + 1), &read_fdset,
+                         &write_fdset, NULL, &timeout);
+        } else {
+            err = select((highest_descriptor + 1), &read_fdset,
+                         NULL, NULL, &timeout);
+        }
+
+        if (err < 0) {
+            HIP_ERROR("select() error: %s.\n", strerror(errno));
+            goto to_maintenance;
+        } else if (err == 0) {
+            /* idle cycle - select() timeout */
+            _HIP_DEBUG("Idle.\n");
+            goto to_maintenance;
+        }
+#ifdef CONFIG_HIP_PERFORMANCE
+        if (bench_set) {       //1 = true; 0 = false
+            HIP_DEBUG("Stop and write PERF_ALL\n");
+            hip_perf_stop_benchmark(perf_set, PERF_ALL);
+            hip_perf_write_benchmark(perf_set, PERF_ALL);
+            bench_set = 0;
+        }
+#endif
+
+        /* see bugzilla bug id 392 to see why */
+        if (FD_ISSET(hip_raw_sock_input_v6, &read_fdset) &&
+            FD_ISSET(hip_raw_sock_input_v4, &read_fdset)) {
+            int type, err_v6 = 0, err_v4 = 0;
+            struct in6_addr saddr, daddr;
+            struct in6_addr saddr_v4, daddr_v4;
+            hip_portpair_t pkt_info;
+            HIP_DEBUG("Receiving messages on raw HIP from IPv6/HIP and 
IPv4/HIP\n");
+            hip_msg_init(hipd_msg);
+            hip_msg_init(hipd_msg_v4);
+            err_v4 = hip_read_control_msg_v4(hip_raw_sock_input_v4, 
hipd_msg_v4,
+                                             &saddr_v4, &daddr_v4,
+                                             &pkt_info, IPV4_HDR_SIZE);
+            err_v6 = hip_read_control_msg_v6(hip_raw_sock_input_v6, hipd_msg,
+                                             &saddr, &daddr, &pkt_info, 0);
+            if (err_v4 > -1) {
+                type = hip_get_msg_type(hipd_msg_v4);
+                if (type == HIP_R2) {
+                    err = hip_receive_control_packet(hipd_msg_v4, &saddr_v4,
+                                                     &daddr_v4, &pkt_info, 1);
+                    if (err) {
+                        HIP_ERROR("hip_receive_control_packet()!\n");
+                    }
+                    err = hip_receive_control_packet(hipd_msg, &saddr, &daddr,
+                                                     &pkt_info, 1);
+                    if (err) {
+                        HIP_ERROR("hip_receive_control_packet()!\n");
+                    }
+                } else {
+                    err = hip_receive_control_packet(hipd_msg, &saddr, &daddr,
+                                                     &pkt_info, 1);
+                    if (err) {
+                        HIP_ERROR("hip_receive_control_packet()!\n");
+                    }
+                    err = hip_receive_control_packet(hipd_msg_v4, &saddr_v4,
+                                                     &daddr_v4, &pkt_info, 1);
+                    if (err) {
+                        HIP_ERROR("hip_receive_control_packet()!\n");
+                    }
+                }
+            }
+        } else {
+            if (FD_ISSET(hip_raw_sock_input_v6, &read_fdset)) {
+#ifdef CONFIG_HIP_PERFORMANCE
+                HIP_DEBUG("Start PERF_ALL\n");
+                bench_set = 1;         //1 = true; 0 = false
+                hip_perf_start_benchmark(perf_set, PERF_ALL);
+#endif
+
+                /* Receiving of a raw HIP message from IPv6 socket. */
+                struct in6_addr saddr, daddr;
+                hip_portpair_t pkt_info;
+                HIP_DEBUG("Receiving a message on raw HIP from " \
+                          "IPv6/HIP socket (file descriptor: %d).\n",
+                          hip_raw_sock_input_v6);
+                hip_msg_init(hipd_msg);
+                if (hip_read_control_msg_v6(hip_raw_sock_input_v6, hipd_msg,
+                                            &saddr, &daddr, &pkt_info, 0)) {
+                    HIP_ERROR("Reading network msg failed\n");
+                } else {
+                    err = hip_receive_control_packet(hipd_msg, &saddr, &daddr, 
&pkt_info, 1);
+                    if (err) {
+                        HIP_ERROR("hip_receive_control_packet()!\n");
+                    }
+                }
+            }
+
+            if (FD_ISSET(hip_raw_sock_input_v4, &read_fdset)) {
+                HIP_DEBUG("HIP RAW SOCKET\n");
+                /* Receiving of a raw HIP message from IPv4 socket. */
+                struct in6_addr saddr, daddr;
+                hip_portpair_t pkt_info;
+                HIP_DEBUG("Receiving a message on raw HIP from " \
+                          "IPv4/HIP socket (file descriptor: %d).\n",
+                          hip_raw_sock_input_v4);
+                hip_msg_init(hipd_msg);
+                HIP_DEBUG("Getting a msg on v4\n");
+                /* Assuming that IPv4 header does not include any
+                 * options */
+                if (hip_read_control_msg_v4(hip_raw_sock_input_v4, hipd_msg,
+                                            &saddr, &daddr, &pkt_info, 
IPV4_HDR_SIZE)) {
+                    HIP_ERROR("Reading network msg failed\n");
+                } else {
+                    err = hip_receive_control_packet(hipd_msg, &saddr, &daddr, 
&pkt_info, 1);
+                    if (err) {
+                        HIP_ERROR("hip_receive_control_packet()!\n");
+                    }
+                }
+            }
+        }
+
+
+        if (FD_ISSET(hip_icmp_sock, &read_fdset)) {
+            HIP_IFEL(hip_icmp_recvmsg(hip_icmp_sock), -1,
+                     "Failed to recvmsg from ICMPv6\n");
+        }
+
+        if (FD_ISSET(hip_nat_sock_input_udp, &read_fdset)) {
+            /* Data structures for storing the source and
+             * destination addresses and ports of the incoming
+             * packet. */
+            struct in6_addr saddr, daddr;
+            hip_portpair_t pkt_info;
+
+            /* Receiving of a UDP message from NAT socket. */
+            HIP_DEBUG("Receiving a message on UDP from NAT " \
+                      "socket (file descriptor: %d).\n",
+                      hip_nat_sock_input_udp);
+
+            /* Initialization of the hip_common header struct. We'll
+             * store the HIP header data here. */
+            hip_msg_init(hipd_msg);
+
+            /* Read in the values to hip_msg, saddr, daddr and
+             * pkt_info. */
+            /* if ( hip_read_control_msg_v4(hip_nat_sock_udp, hipd_msg,&saddr, 
&daddr,&pkt_info, 0) ) */
+            err = hip_read_control_msg_v4(hip_nat_sock_input_udp,
+                                          hipd_msg,
+                                          &saddr,
+                                          &daddr,
+                                          &pkt_info,
+                                          HIP_UDP_ZERO_BYTES_LEN);
+            if (err) {
+                HIP_ERROR("Reading network msg failed\n");
+                /* If the values were read in successfully, we
+                 * do the UDP specific stuff next. */
+            } else {
+                err =  hip_receive_udp_control_packet(hipd_msg,
+                                                      &saddr,
+                                                      &daddr,
+                                                      &pkt_info);
+            }
+        }
+
+        if (FD_ISSET(hip_user_sock, &read_fdset)) {
+            /* Receiving of a message from user socket. */
+            struct sockaddr_in6 app_src;
+
+            HIP_DEBUG("Receiving user message.\n");
+
+            hip_msg_init(hipd_msg);
+
+            if (hip_read_user_control_msg(hip_user_sock, hipd_msg, &app_src)) {
+                HIP_ERROR("Reading user msg failed\n");
+            } else {
+                err = hip_handle_user_msg(hipd_msg, &app_src);
+            }
+        }
 #ifdef CONFIG_HIP_DHT
-                /* DHT SOCKETS HANDLING */
-                if (hip_opendht_inuse == SO_HIP_DHT_ON && 
hip_opendht_sock_fqdn != -1) {
-                        if (FD_ISSET(hip_opendht_sock_fqdn, &read_fdset) &&
-                            FD_ISSET(hip_opendht_sock_fqdn, &write_fdset) &&
-                            (hip_opendht_inuse == SO_HIP_DHT_ON)) {
-                                /* Error with the connect */
-                                HIP_ERROR("Error OpenDHT socket is readable 
and writable\n");
-                        } else if (FD_ISSET(hip_opendht_sock_fqdn, 
&write_fdset)) {
-                                hip_opendht_fqdn_sent = 
STATE_OPENDHT_START_SEND;
-                        }
-                        if (FD_ISSET(hip_opendht_sock_fqdn, &read_fdset) &&
-                            (hip_opendht_inuse == SO_HIP_DHT_ON)) {
-                                /* Receive answer from openDHT FQDN->HIT 
mapping */
-                                if (hip_opendht_fqdn_sent == 
STATE_OPENDHT_WAITING_ANSWER) {
-                                        memset(opendht_response, '\0', 
sizeof(opendht_response));
-                                        opendht_error = 
opendht_read_response(hip_opendht_sock_fqdn,
-                                                                              
opendht_response);
-                                        if (opendht_error == -1) {
-                                                HIP_DEBUG("Put was 
unsuccesfull \n");
-                                                hip_opendht_error_count++;
-                                                HIP_DEBUG("DHT error count now 
%d/%d.\n",
-                                                          
hip_opendht_error_count, OPENDHT_ERROR_COUNT_MAX);
-                                        }
-                                        else
-                                                HIP_DEBUG("Put was success 
(FQDN->HIT)\n");
-
-                                        close(hip_opendht_sock_fqdn);
-                                        hip_opendht_sock_fqdn = 0;
-                                        hip_opendht_sock_fqdn = 
init_dht_gateway_socket_gw(hip_opendht_sock_fqdn, opendht_serving_gateway);
-                                        hip_opendht_fqdn_sent = 
STATE_OPENDHT_IDLE;
-                                        opendht_error = 0;
-                                }
-                        }
-                        if (FD_ISSET(hip_opendht_sock_hit, &read_fdset) &&
-                            FD_ISSET(hip_opendht_sock_hit, &write_fdset) &&
-                            (hip_opendht_inuse == SO_HIP_DHT_ON)) {
-                                /* Error with the connect */
-                                HIP_ERROR("Error OpenDHT socket is readable 
and writable\n");
-                        } else if (FD_ISSET(hip_opendht_sock_hit, 
&write_fdset)) {
-                                hip_opendht_hit_sent = 
STATE_OPENDHT_START_SEND;
-                        }
-                        if ((FD_ISSET(hip_opendht_sock_hit, &read_fdset)) &&
-                            (hip_opendht_inuse == SO_HIP_DHT_ON)) {
-                                /* Receive answer from openDHT HIT->IP mapping 
*/
-                                if (hip_opendht_hit_sent == 
STATE_OPENDHT_WAITING_ANSWER) {
-                                        memset(opendht_response, '\0', 
sizeof(opendht_response));
-                                        opendht_error = 
opendht_read_response(hip_opendht_sock_hit,
-                                                                              
opendht_response);
-                                        if (opendht_error == -1) {
-                                                HIP_DEBUG("Put was 
unsuccesfull \n");
-                                                hip_opendht_error_count++;
-                                                HIP_DEBUG("DHT error count now 
%d/%d.\n",
-                                                          
hip_opendht_error_count, OPENDHT_ERROR_COUNT_MAX);
-                                        }
-                                        else
-                                                HIP_DEBUG("Put was success 
(HIT->IP)\n");
-                                        close(hip_opendht_sock_hit);
-                                        hip_opendht_sock_hit = 0;
-                                        hip_opendht_sock_hit = 
init_dht_gateway_socket_gw(hip_opendht_sock_hit, opendht_serving_gateway);
-                                        hip_opendht_hit_sent = 
STATE_OPENDHT_IDLE;
-                                        opendht_error= 0;
-                                }
-                        }
-                }
-#endif /* CONFIG_HIP_DHT */
-                /* END DHT SOCKETS HANDLING */
-
-               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_netdev_event, NULL))
-                               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_netdev_event, NULL))
-                               HIP_ERROR("Netlink receiving failed\n");
-               }
+        /* DHT SOCKETS HANDLING */
+        if (hip_opendht_inuse == SO_HIP_DHT_ON && hip_opendht_sock_fqdn != -1) 
{
+            if (FD_ISSET(hip_opendht_sock_fqdn, &read_fdset) &&
+                FD_ISSET(hip_opendht_sock_fqdn, &write_fdset) &&
+                (hip_opendht_inuse == SO_HIP_DHT_ON)) {
+                /* Error with the connect */
+                HIP_ERROR("Error OpenDHT socket is readable and writable\n");
+            } else if (FD_ISSET(hip_opendht_sock_fqdn, &write_fdset)) {
+                hip_opendht_fqdn_sent = STATE_OPENDHT_START_SEND;
+            }
+            if (FD_ISSET(hip_opendht_sock_fqdn, &read_fdset) &&
+                (hip_opendht_inuse == SO_HIP_DHT_ON)) {
+                /* Receive answer from openDHT FQDN->HIT mapping */
+                if (hip_opendht_fqdn_sent == STATE_OPENDHT_WAITING_ANSWER) {
+                    memset(opendht_response, '\0', sizeof(opendht_response));
+                    opendht_error = 
opendht_read_response(hip_opendht_sock_fqdn,
+                                                          opendht_response);
+                    if (opendht_error == -1) {
+                        HIP_DEBUG("Put was unsuccesfull \n");
+                        hip_opendht_error_count++;
+                        HIP_DEBUG("DHT error count now %d/%d.\n",
+                                  hip_opendht_error_count, 
OPENDHT_ERROR_COUNT_MAX);
+                    } else {
+                        HIP_DEBUG("Put was success (FQDN->HIT)\n");
+                    }
+
+                    close(hip_opendht_sock_fqdn);
+                    hip_opendht_sock_fqdn = 0;
+                    hip_opendht_sock_fqdn = 
init_dht_gateway_socket_gw(hip_opendht_sock_fqdn,
+                                                                       
opendht_serving_gateway);
+                    hip_opendht_fqdn_sent = STATE_OPENDHT_IDLE;
+                    opendht_error         = 0;
+                }
+            }
+            if (FD_ISSET(hip_opendht_sock_hit, &read_fdset) &&
+                FD_ISSET(hip_opendht_sock_hit, &write_fdset) &&
+                (hip_opendht_inuse == SO_HIP_DHT_ON)) {
+                /* Error with the connect */
+                HIP_ERROR("Error OpenDHT socket is readable and writable\n");
+            } else if (FD_ISSET(hip_opendht_sock_hit, &write_fdset)) {
+                hip_opendht_hit_sent = STATE_OPENDHT_START_SEND;
+            }
+            if ((FD_ISSET(hip_opendht_sock_hit, &read_fdset)) &&
+                (hip_opendht_inuse == SO_HIP_DHT_ON)) {
+                /* Receive answer from openDHT HIT->IP mapping */
+                if (hip_opendht_hit_sent == STATE_OPENDHT_WAITING_ANSWER) {
+                    memset(opendht_response, '\0', sizeof(opendht_response));
+                    opendht_error = opendht_read_response(hip_opendht_sock_hit,
+                                                          opendht_response);
+                    if (opendht_error == -1) {
+                        HIP_DEBUG("Put was unsuccesfull \n");
+                        hip_opendht_error_count++;
+                        HIP_DEBUG("DHT error count now %d/%d.\n",
+                                  hip_opendht_error_count, 
OPENDHT_ERROR_COUNT_MAX);
+                    } else {
+                        HIP_DEBUG("Put was success (HIT->IP)\n");
+                    }
+                    close(hip_opendht_sock_hit);
+                    hip_opendht_sock_hit = 0;
+                    hip_opendht_sock_hit = 
init_dht_gateway_socket_gw(hip_opendht_sock_hit,
+                                                                      
opendht_serving_gateway);
+                    hip_opendht_hit_sent = STATE_OPENDHT_IDLE;
+                    opendht_error        = 0;
+                }
+            }
+        }
+#endif  /* CONFIG_HIP_DHT */
+        /* END DHT SOCKETS HANDLING */
+
+        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_netdev_event, NULL)) {
+                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_netdev_event, NULL)) {
+                HIP_ERROR("Netlink receiving failed\n");
+            }
+        }
 
 to_maintenance:
-               err = periodic_maintenance();
-               if (err)
-               {
-                       HIP_ERROR("Error (%d) ignoring. %s\n", err,
-                                 ((errno) ? strerror(errno) : ""));
-                       err = 0;
-               }
-       }
+        err = periodic_maintenance();
+        if (err) {
+            HIP_ERROR("Error (%d) ignoring. %s\n", err,
+                      ((errno) ? strerror(errno) : ""));
+            err = 0;
+        }
+    }
 
 out_err:
-       /* free allocated resources */
-       hip_exit(err);
-
-       HIP_INFO("hipd pid=%d exiting, retval=%d\n", getpid(), err);
-
-       return err;
+    /* free allocated resources */
+    hip_exit(err);
+
+    HIP_INFO("hipd pid=%d exiting, retval=%d\n", getpid(), err);
+
+    return err;
 }
 
-
 int main(int argc, char *argv[])
 {
-       int err = 0;
-       uid_t euid;
-
-       euid = geteuid();
-       HIP_IFEL((euid != 0), -1, "hipd must be started as root\n");            
        // We need to recreate the NAT UDP sockets to bind to the new port.
-
-       HIP_IFE(hipd_main(argc, argv), -1);
-       if (hipd_get_flag(HIPD_FLAG_RESTART))
-       {
-               HIP_INFO(" !!!!! HIP DAEMON RESTARTING !!!!! \n");
-               hip_handle_exec_application(0, EXEC_LOADLIB_NONE, argc, argv);
-       }
+    int err = 0;
+    uid_t euid;
+
+    euid = geteuid();
+    /* We need to recreate the NAT UDP sockets to bind to the new port. */
+    HIP_IFEL((euid != 0), -1, "hipd must be started as root\n");
+
+    HIP_IFE(hipd_main(argc, argv), -1);
+    if (hipd_get_flag(HIPD_FLAG_RESTART)) {
+        HIP_INFO(" !!!!! HIP DAEMON RESTARTING !!!!! \n");
+        hip_handle_exec_application(0, EXEC_LOADLIB_NONE, argc, argv);
+    }
 
 out_err:
-       return err;
+    return err;
 }

=== modified file 'hipd/hipd.c.doxyme'
--- hipd/hipd.c.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/hipd.c.doxyme  2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,65 +45,64 @@
 
 
 /**
- * add_cert_and_hits_to_db 
+ * add_cert_and_hits_to_db
  *
  *
  * @param uadb_info
- * @return 
- **/
-
-
-/**
- * hip_recv_agent 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * hip_send_agent 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * hip_sendto_firewall 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * hip_set_hi3_status 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * hip_set_opportunistic_tcp_status 
- *
- *
- * @param msg
- * @return 
- **/
-
-
-/**
- * main 
+ * @return
+ **/
+
+
+/**
+ * hip_recv_agent
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * hip_send_agent
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * hip_sendto_firewall
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * hip_set_hi3_status
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * hip_set_opportunistic_tcp_status
+ *
+ *
+ * @param msg
+ * @return
+ **/
+
+
+/**
+ * main
  *
  *
  * @param argc
  * @param argv[]
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/hipd.h'
--- hipd/hipd.h 2010-02-08 15:18:06 +0000
+++ hipd/hipd.h 2010-02-10 22:32:46 +0000
@@ -31,7 +31,7 @@
 #include "lib/core/message.h"
 #include "lib/core/esp_prot_common.h"
 #ifdef CONFIG_HIP_AGENT
-       #include "lib/core/sqlitedbapi.h"
+        #include "lib/core/sqlitedbapi.h"
 #endif
 #include "dhtqueue.h"
 
@@ -49,24 +49,21 @@
 #define HIP_OPP_WAIT              5 /* seconds */
 #define HIP_OPP_FALLBACK_INTERVAL 1 /* seconds */
 #define HIP_OPP_FALLBACK_INIT \
-           (HIP_OPP_FALLBACK_INTERVAL / HIP_SELECT_TIMEOUT)
+    (HIP_OPP_FALLBACK_INTERVAL / HIP_SELECT_TIMEOUT)
 /* the interval with which the hadb entries are checked for retransmissions */
 #define HIP_RETRANSMIT_INIT \
-           (HIP_RETRANSMIT_INTERVAL / HIP_SELECT_TIMEOUT)
+    (HIP_RETRANSMIT_INTERVAL / HIP_SELECT_TIMEOUT)
 /* wait about n seconds before retransmitting.
-   the actual time is between n and n + RETRANSMIT_INIT seconds */
+ * the actual time is between n and n + RETRANSMIT_INIT seconds */
 #define HIP_RETRANSMIT_WAIT 10
- 
-#define HIP_R1_PRECREATE_INTERVAL 60*60 /* seconds */
-#define HIP_R1_PRECREATE_INIT \
-           (HIP_R1_PRECREATE_INTERVAL / HIP_SELECT_TIMEOUT)
+
+#define HIP_R1_PRECREATE_INTERVAL 60 * 60 /* seconds */
+#define HIP_R1_PRECREATE_INIT (HIP_R1_PRECREATE_INTERVAL / HIP_SELECT_TIMEOUT)
 #define OPENDHT_REFRESH_INTERVAL 30 /* seconds Original 60 using 1 with 
sockaddrs */
-#define OPENDHT_REFRESH_INIT \
-           (OPENDHT_REFRESH_INTERVAL / HIP_SELECT_TIMEOUT)
+#define OPENDHT_REFRESH_INIT (OPENDHT_REFRESH_INTERVAL / HIP_SELECT_TIMEOUT)
 
 #define QUEUE_CHECK_INTERVAL 15 /* seconds */
-#define QUEUE_CHECK_INIT \
-           (QUEUE_CHECK_INTERVAL / HIP_SELECT_TIMEOUT)
+#define QUEUE_CHECK_INIT (QUEUE_CHECK_INTERVAL / HIP_SELECT_TIMEOUT)
 
 #define CERTIFICATE_PUBLISH_INTERVAL 120 /* seconds */
 #define HIP_HA_PURGE_TIMEOUT 5
@@ -75,7 +72,7 @@
 #define HIP_PACKET_DUPLICATES                1
 /* Set to 1 if you want to simulate lost output packet */
 #define HIP_SIMULATE_PACKET_LOSS             1
- /* Packet loss probability in percents */
+/* Packet loss probability in percents */
 #define HIP_SIMULATE_PACKET_LOSS_PROBABILITY 0
 #define HIP_SIMULATE_PACKET_IS_LOST() (random() < ((uint64_t) 
HIP_SIMULATE_PACKET_LOSS_PROBABILITY * RAND_MAX) / 100)
 
@@ -87,7 +84,7 @@
 #define HIPD_NL_GROUP 32
 
 #ifdef CONFIG_HIP_AGENT
-       extern sqlite3 *daemon_db;
+extern sqlite3 *daemon_db;
 #endif
 
 extern struct rtnl_handle hip_nl_route;
@@ -117,7 +114,7 @@
 extern int hip_firewall_sock, hip_firewall_status;
 extern struct sockaddr_in6 hip_firewall_addr;
 
-extern int hit_db_lock ;
+extern int hit_db_lock;
 extern int is_active_mhaddr;
 extern int is_hard_handover;
 
@@ -130,7 +127,7 @@
 
 extern int hip_tcptimeout_status;
 
-extern struct addrinfo * opendht_serving_gateway;
+extern struct addrinfo *opendht_serving_gateway;
 extern int opendht_serving_gateway_ttl;
 extern int opendht_serving_gateway_port;
 
@@ -139,7 +136,7 @@
 extern int opendht_error;
 extern char opendht_current_key[INET6_ADDRSTRLEN + 2];
 extern char opendht_name_mapping[HIP_HOST_ID_HOSTNAME_LEN_MAX];
-extern hip_common_t * opendht_current_hdrr;
+extern hip_common_t *opendht_current_hdrr;
 extern unsigned char opendht_hdrr_secret[40];
 extern char opendht_host_name[256];
 
@@ -152,7 +149,7 @@
 
 extern hip_transform_suite_t hip_nat_status;
 
-extern struct in6_addr * sava_serving_gateway;
+extern struct in6_addr *sava_serving_gateway;
 
 extern int hip_use_userspace_data_packet_mode;
 
@@ -185,7 +182,7 @@
 
 //int hip_sendto(const struct hip_common *msg, const struct sockaddr_in6 *dst);
 
-#ifdef CONFIG_HIP_I3 
+#ifdef CONFIG_HIP_I3
 int hip_get_hi3_status( void );
 void hip_set_hi3_status(struct hip_common *msg);
 #endif /* CONFIG_HIP_I3 */

=== modified file 'hipd/hipd.h.doxyme'
--- hipd/hipd.h.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/hipd.h.doxyme  2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hipd.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/hipqueue.c.doxyme'
--- hipd/hipqueue.c.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/hipqueue.c.doxyme      2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,39 +45,38 @@
 
 
 /**
- * hip_dht_queue_cmp 
+ * hip_dht_queue_cmp
  *
  *
  * @param item1
  * @param item2
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_dht_queue_hash 
+ * hip_dht_queue_hash
  *
  *
  * @param item
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_read_from_dht_queue 
+ * hip_read_from_dht_queue
  *
  *
  * @param read_data
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_write_to_dht_queue 
+ * hip_write_to_dht_queue
  *
  *
  * @param write_data
  * @param data_size_in_bytes
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/hipqueue.h.doxyme'
--- hipd/hipqueue.h.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/hipqueue.h.doxyme      2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hipqueue.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/hiprelay.c'
--- hipd/hiprelay.c     2010-01-18 20:14:50 +0000
+++ hipd/hiprelay.c     2010-02-10 22:32:46 +0000
@@ -3,7 +3,7 @@
  * for the Host Identity Protocol (HIP). See header file for usage
  * instructions. Version 1.1 added support for white list and configuration
  * file.
- * 
+ *
  * @author  Lauri Silvennoinen
  * @note    Related RFC: <a href="http://www.rfc-editor.org/rfc/rfc5204.txt";>
  *          Host Identity Protocol (HIP) Rendezvous Extension</a>
@@ -12,7 +12,7 @@
  *          draft-ietf-hip-nat-traversal-03</a>
  * @note    Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
  * @see     hiprelay.h
- */ 
+ */
 #ifdef HAVE_CONFIG_H
   #include "config.h"
 #endif /* HAVE_CONFIG_H */
@@ -26,60 +26,59 @@
  *        string.
  */
 #define HIP_RC_FILE_CONTENT \
-"# HIP relay / RVS configuration file.\n"\
-"#\n"\
-"# This file consists of stanzas of the following form:\n"\
-"# \n"\
-"# parametername = \"value1\", \"value2\", ... \"valueN\"\n"\
-"#\n"\
-"# where there can be as many values as needed per line with the limitation 
of\n"\
-"# total line length of ",HIP_RELAY_MAX_LINE_LEN," characters. The 
'parametername' is at most ",HIP_RELAY_MAX_PAR_LEN,"\n"\
-"# characters long and 'values' are at most ",HIP_RELAY_MAX_VAL_LEN," 
characters long. A value itself\n"\
-"# may not contain a '",HIP_RELAY_VAL_SEP,"' character.\n"\
-"#\n"\
-"# The '",HIP_RELAY_COMMENT,"' character is used for comments. End of line 
comments are not allowed.\n"\
-"\n"\
-"# Relay whitelist status. When this is set to 'yes', only clients whose HIT 
is\n"\
-"# listed on the whitelist are allowed to register to the relay / RVS 
service.\n"\
-"# When this is set to 'no', any client is allowed to register. This defaults 
as\n"\
-"# 'yes' when no value is given.\n"\
-"whitelist_enabled = \"no\"\n"\
-"\n"\
-"# Relay whitelist. The HITs of the clients that are allowed to register to\n"\
-"# the relay / RVS service. You may use multiple stanzas of the same name.\n"\
-"whitelist = \"\"\n"\
-"\n"\
-"# The minimum number of seconds the relay / RVS client is granted the 
service.\n"\
-"# If the service request defines a value smaller than this value, this value 
is\n"\
-"# used.\n"\
-"minimum_lifetime = \"60\"\n"\
-"\n"\
-"# The maximum number of seconds the relay / RVS client is granted the 
service.\n"\
-"# If the service request defines a value bigger than this value, this value 
is\n"\
-"# used.\n"\
-"maximum_lifetime = \"3600\"\n"
+"# HIP relay / RVS configuration file.\n" \
+"#\n" \
+"# This file consists of stanzas of the following form:\n" \
+"# \n" \
+"# parametername = \"value1\", \"value2\", ... \"valueN\"\n" \
+"#\n" \
+"# where there can be as many values as needed per line with the limitation 
of\n" \
+"# total line length of ", HIP_RELAY_MAX_LINE_LEN, " characters. The 
'parametername' is at most ", HIP_RELAY_MAX_PAR_LEN, "\n" \
+"# characters long and 'values' are at most ", HIP_RELAY_MAX_VAL_LEN, " 
characters long. A value itself\n" \
+"# may not contain a '", HIP_RELAY_VAL_SEP, "' character.\n" \
+"#\n" \
+"# The '", HIP_RELAY_COMMENT, "' character is used for comments. End of line 
comments are not allowed.\n" \
+"\n" \
+"# Relay whitelist status. When this is set to 'yes', only clients whose HIT 
is\n" \
+"# listed on the whitelist are allowed to register to the relay / RVS 
service.\n" \
+"# When this is set to 'no', any client is allowed to register. This defaults 
as\n" \
+"# 'yes' when no value is given.\n" \
+"whitelist_enabled = \"no\"\n" \
+"\n" \
+"# Relay whitelist. The HITs of the clients that are allowed to register to\n" 
\
+"# the relay / RVS service. You may use multiple stanzas of the same name.\n" \
+"whitelist = \"\"\n" \
+"\n" \
+"# The minimum number of seconds the relay / RVS client is granted the 
service.\n" \
+"# If the service request defines a value smaller than this value, this value 
is\n" \
+"# used.\n" \
+"minimum_lifetime = \"60\"\n" \
+"\n" \
+"# The maximum number of seconds the relay / RVS client is granted the 
service.\n" \
+"# If the service request defines a value bigger than this value, this value 
is\n" \
+"# used.\n" "maximum_lifetime = \"3600\"\n"
 /** The printf format string of @c HIP_RC_FILE_CONTENT. */
 #define HIP_RC_FILE_FORMAT_STRING "%s%d%s%d%s%d%s%c%s%c%s"
 
 /** HIP relay config file name and path. */
-#define HIP_RELAY_CONFIG_FILE  HIPL_SYSCONFDIR"/relay_config"
+#define HIP_RELAY_CONFIG_FILE  HIPL_SYSCONFDIR "/relay_config"
 
 /** A hashtable for storing the relay records. */
-static HIP_HASHTABLE *hiprelay_ht = NULL;
+static HIP_HASHTABLE *hiprelay_ht       = NULL;
 /** A hashtable for storing the the HITs of the clients that are allowed to use
  *  the relay / RVS service. */
-static HIP_HASHTABLE *hiprelay_wl = NULL;
+static HIP_HASHTABLE *hiprelay_wl       = NULL;
 
 /** Minimum relay record life time as a 8-bit integer. */
-uint8_t hiprelay_min_lifetime = HIP_RELREC_MIN_LIFETIME;
+uint8_t hiprelay_min_lifetime           = HIP_RELREC_MIN_LIFETIME;
 /** Maximum relay record life time as a 8-bit integer. */
-uint8_t hiprelay_max_lifetime = HIP_RELREC_MAX_LIFETIME;
-/** 
+uint8_t hiprelay_max_lifetime           = HIP_RELREC_MAX_LIFETIME;
+/**
  * A boolean to indicating if the RVS / relay is enabled. User sets this value
  * using the hipconf tool.
  */
-hip_relay_status_t relay_enabled = HIP_RELAY_OFF;
-/** 
+hip_relay_status_t relay_enabled        = HIP_RELAY_OFF;
+/**
  * A boolean to indicating if the RVS / relay whitelist is enabled. User sets
  * this value from the relay configuration file.
  */
@@ -91,10 +90,10 @@
  *
  * @return zero on success, -1 otherwise.
  * @note   do not call this function directly, instead call hip_relay_init().
- */ 
+ */
 static int hip_relht_init(void);
 
-/** 
+/**
  * Uninitializes the HIP relay record hashtable @c hiprelay_ht. Frees the 
memory
  * allocated for the hashtable and for the relay records. Thus, after calling
  * this function, all memory allocated from the heap related to the relay 
record
@@ -123,10 +122,10 @@
  *
  * @return zero on success, -1 otherwise.
  * @note   do not call this function directly, instead call hip_relay_init().
- */ 
+ */
 static int hip_relwl_init(void);
 
-/** 
+/**
  * Uninitializes the HIP relay whitelist hashtable @c hiprelay_wl. Frees the
  * memory allocated for the hashtable and for the HITs. Thus, after calling
  * this function, all memory allocated from the heap related to the whitelist
@@ -161,18 +160,18 @@
  * allocated for the element. The parameter HIT is itself left untouched, it is
  * only used as an search key.
  *
- * @param hit a pointer to a HIT. 
+ * @param hit a pointer to a HIT.
  */
 static void hip_relwl_hit_free_doall(hip_hit_t *hit);
 
 /**
  * Reads RVS / HIP Relay configuration from a file. Reads configuration
- * information from @c HIP_RELAY_CONFIG_FILE. 
+ * information from @c HIP_RELAY_CONFIG_FILE.
  *
  * @return zero on success, -ENOENT if the file could not be opened for 
reading.
  * @note   The white list @c hiprelay_wl must be initialized before this
  *         function is called.
- */ 
+ */
 static int hip_relay_read_config(void);
 
 /**
@@ -183,13 +182,13 @@
  *
  * @return zero on success, -ENOENT if the file could not be opened for 
writing.
  * @note   Truncates existing file to zero length.
- */ 
+ */
 static int hip_relay_write_config(void);
 
 /**
  * The hash function of the @c hiprelay_ht hashtable. Calculates a hash from
  * parameter relay record HIT.
- * 
+ *
  * @param rec a pointer to a relay record.
  * @return    the calculated hash or zero if @c rec or hit_r is NULL.
  */
@@ -198,28 +197,28 @@
 /**
  * The hash function of the @c hiprelay_wl hashtable. Calculates a hash from
  * parameter HIT.
- * 
+ *
  * @param hit a pointer to a HIT.
  * @return    the calculated hash or zero if @c hit is NULL.
  */
 static unsigned long hip_relwl_hash(const hip_hit_t *hit);
 
 static int hip_relay_forward_response(const hip_common_t *r,
-                              const uint8_t type_hdr, 
-                              const in6_addr_t *r_saddr,
-                              const in6_addr_t *r_daddr , 
-                              const hip_portpair_t *r_info , 
-                              const in6_addr_t *relay_to_addr,
-                              const in_port_t relay_to_port);
+                                      const uint8_t type_hdr,
+                                      const in6_addr_t *r_saddr,
+                                      const in6_addr_t *r_daddr,
+                                      const hip_portpair_t *r_info,
+                                      const in6_addr_t *relay_to_addr,
+                                      const in_port_t relay_to_port);
 
 
 static void hip_relht_rec_free_type_doall_arg(hip_relrec_t *rec, const 
hip_relrec_type_t *type)
 {
-       hip_relrec_t *fetch_record = hip_relht_get(rec);
+    hip_relrec_t *fetch_record = hip_relht_get(rec);
 
-       if(fetch_record != NULL && fetch_record->type == *type) {
-               hip_relht_rec_free_doall(rec);
-       }
+    if (fetch_record != NULL && fetch_record->type == *type) {
+        hip_relht_rec_free_doall(rec);
+    }
 }
 
 /** A callback wrapper of the prototype required by @c lh_doall_arg(). */
@@ -227,20 +226,21 @@
 
 hip_relay_status_t hip_relay_get_status()
 {
-       return relay_enabled;
+    return relay_enabled;
 }
 
 void hip_relay_set_status(hip_relay_status_t status)
 {
-       relay_enabled = status;
+    relay_enabled = status;
 }
 
 static unsigned long hip_relht_hash(const hip_relrec_t *rec)
 {
-       if(rec == NULL || &(rec->hit_r) == NULL)
-               return 0;
-       
-       return hip_hash_func(&(rec->hit_r));
+    if (rec == NULL || &(rec->hit_r) == NULL) {
+        return 0;
+    }
+
+    return hip_hash_func(&(rec->hit_r));
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
@@ -248,11 +248,12 @@
 
 static int hip_relht_cmp(const hip_relrec_t *rec1, const hip_relrec_t *rec2)
 {
-       if(rec1 == NULL || &(rec1->hit_r) == NULL ||
-          rec2 == NULL || &(rec2->hit_r) == NULL)
-               return 1;
+    if (rec1 == NULL || &(rec1->hit_r) == NULL ||
+        rec2 == NULL || &(rec2->hit_r) == NULL) {
+        return 1;
+    }
 
-       return (hip_relht_hash(rec1) != hip_relht_hash(rec2));
+    return hip_relht_hash(rec1) != hip_relht_hash(rec2);
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
@@ -260,52 +261,55 @@
 
 int hip_relht_put(hip_relrec_t *rec)
 {
-       hip_relrec_t key, *match;
-
-       if(hiprelay_ht == NULL || rec == NULL)
-               return -1;
-     
-       /* If we are trying to insert a duplicate element (same HIT), we have to
-          delete the previous entry. If we do not do so, only the pointer in
-          the hashtable is replaced and the reference to the previous element
-          is lost resulting in a memory leak. */
-       memcpy(&(key.hit_r), &(rec->hit_r), sizeof(rec->hit_r));
-       match = hip_relht_get(rec);
-
-       if(match != NULL) {
-               hip_relht_rec_free_doall(&key);
-               list_add(rec, hiprelay_ht);
-               return -1;
-       } else {
-               list_add(rec, hiprelay_ht);
-               return 0;
-       }
+    hip_relrec_t key, *match;
+
+    if (hiprelay_ht == NULL || rec == NULL) {
+        return -1;
+    }
+
+    /* If we are trying to insert a duplicate element (same HIT), we have to
+     * delete the previous entry. If we do not do so, only the pointer in
+     * the hashtable is replaced and the reference to the previous element
+     * is lost resulting in a memory leak. */
+    memcpy(&(key.hit_r), &(rec->hit_r), sizeof(rec->hit_r));
+    match = hip_relht_get(rec);
+
+    if (match != NULL) {
+        hip_relht_rec_free_doall(&key);
+        list_add(rec, hiprelay_ht);
+        return -1;
+    } else {
+        list_add(rec, hiprelay_ht);
+        return 0;
+    }
 }
 
 hip_relrec_t *hip_relht_get(const hip_relrec_t *rec)
 {
-       if(hiprelay_ht == NULL || rec == NULL)
-               return NULL;
+    if (hiprelay_ht == NULL || rec == NULL) {
+        return NULL;
+    }
 
-       return (hip_relrec_t *)list_find(rec, hiprelay_ht);
+    return (hip_relrec_t *) list_find(rec, hiprelay_ht);
 }
 
 void hip_relht_rec_free_doall(hip_relrec_t *rec)
 {
-       if(hiprelay_ht == NULL || rec == NULL)
-               return;
-
-       /* Check if such element exist, and delete the pointer from the 
hashtable. */
-       hip_relrec_t *deleted_rec = list_del(rec, hiprelay_ht);
-
-       /* Free the memory allocated for the element. */
-       if(deleted_rec != NULL) {
-               /* We set the memory to '\0' because the user may still have a
-                  reference to the memory region that is freed here. */
-               memset(deleted_rec, '\0', sizeof(*deleted_rec));
-               free(deleted_rec);
-               HIP_DEBUG("Relay record deleted.\n");
-       }
+    if (hiprelay_ht == NULL || rec == NULL) {
+        return;
+    }
+
+    /* Check if such element exist, and delete the pointer from the hashtable. 
*/
+    hip_relrec_t *deleted_rec = list_del(rec, hiprelay_ht);
+
+    /* Free the memory allocated for the element. */
+    if (deleted_rec != NULL) {
+        /* We set the memory to '\0' because the user may still have a
+         * reference to the memory region that is freed here. */
+        memset(deleted_rec, '\0', sizeof(*deleted_rec));
+        free(deleted_rec);
+        HIP_DEBUG("Relay record deleted.\n");
+    }
 }
 
 /** A callback wrapper of the prototype required by @c lh_doall(). */
@@ -313,13 +317,14 @@
 
 static void hip_relht_rec_free_expired_doall(hip_relrec_t *rec)
 {
-       if(rec == NULL) // No need to check hiprelay_ht
-               return;
+    if (rec == NULL) {  // No need to check hiprelay_ht
+        return;
+    }
 
-       if(time(NULL) - rec->created > rec->lifetime) {
-               HIP_DEBUG("Relay record expired, deleting.\n");
-               hip_relht_rec_free_doall(rec);
-       }
+    if (time(NULL) - rec->created > rec->lifetime) {
+        HIP_DEBUG("Relay record expired, deleting.\n");
+        hip_relht_rec_free_doall(rec);
+    }
 }
 
 /** A callback wrapper of the prototype required by @c lh_doall(). */
@@ -327,132 +332,140 @@
 
 unsigned long hip_relht_size()
 {
-       if(hiprelay_ht == NULL)
-               return 0;
+    if (hiprelay_ht == NULL) {
+        return 0;
+    }
 
-       return ((struct lhash_st *) hiprelay_ht)->num_items;
+    return ((struct lhash_st *) hiprelay_ht)->num_items;
 }
 
 void hip_relht_maintenance()
 {
-       if(hiprelay_ht == NULL)
-               return;
-     
-       unsigned int tmp = ((struct lhash_st *) hiprelay_ht)->down_load;
-       ((struct lhash_st *)hiprelay_ht)->down_load = 0;
-       hip_ht_doall(hiprelay_ht, 
(LHASH_DOALL_FN_TYPE)LHASH_DOALL_FN(hip_relht_rec_free_expired));
-       ((struct lhash_st *) hiprelay_ht)->down_load = tmp;
+    if (hiprelay_ht == NULL) {
+        return;
+    }
+
+    unsigned int tmp = ((struct lhash_st *) hiprelay_ht)->down_load;
+    ((struct lhash_st *) hiprelay_ht)->down_load = 0;
+    hip_ht_doall(hiprelay_ht, (LHASH_DOALL_FN_TYPE) 
LHASH_DOALL_FN(hip_relht_rec_free_expired));
+    ((struct lhash_st *) hiprelay_ht)->down_load = tmp;
 }
 
 void hip_relht_free_all_of_type(const hip_relrec_type_t type)
 {
-       if(hiprelay_ht == NULL)
-               return;
-       
-       unsigned int tmp = ((struct lhash_st *) hiprelay_ht)->down_load;
-       ((struct lhash_st *) hiprelay_ht)->down_load = 0;
-       hip_ht_doall_arg(hiprelay_ht, 
(LHASH_DOALL_ARG_FN_TYPE)LHASH_DOALL_ARG_FN(hip_relht_rec_free_type),
-                    (void *)&type);
-       ((struct lhash_st *) hiprelay_ht)->down_load = tmp;
+    if (hiprelay_ht == NULL) {
+        return;
+    }
+
+    unsigned int tmp = ((struct lhash_st *) hiprelay_ht)->down_load;
+    ((struct lhash_st *) hiprelay_ht)->down_load = 0;
+    hip_ht_doall_arg(hiprelay_ht, (LHASH_DOALL_ARG_FN_TYPE) 
LHASH_DOALL_ARG_FN(hip_relht_rec_free_type),
+                     (void *) &type);
+    ((struct lhash_st *) hiprelay_ht)->down_load = tmp;
 }
 
 hip_relrec_t *hip_relrec_alloc(const hip_relrec_type_t type,
-                              const uint8_t lifetime,
-                              const in6_addr_t *hit_r, const hip_hit_t *ip_r,
-                              const in_port_t port,
-                              const hip_crypto_key_t *hmac,
-                              const hip_xmit_func_t func)
+                               const uint8_t lifetime,
+                               const in6_addr_t *hit_r, const hip_hit_t *ip_r,
+                               const in_port_t port,
+                               const hip_crypto_key_t *hmac,
+                               const hip_xmit_func_t func)
 {
-       if(hit_r == NULL || ip_r == NULL || hmac == NULL || func == NULL)
-               return NULL;
-
-       hip_relrec_t *rec = (hip_relrec_t*) malloc(sizeof(hip_relrec_t));
-     
-       if(rec == NULL) {
-               HIP_ERROR("Error allocating memory for HIP relay record.\n");
-               return NULL;
-       }
-       rec->type = type;
-       memcpy(&(rec->hit_r), hit_r, sizeof(*hit_r));
-       memcpy(&(rec->ip_r), ip_r, sizeof(*ip_r));
-       rec->udp_port_r = port;
-       memcpy(&(rec->hmac_relay), hmac, sizeof(*hmac));
-       rec->send_fn = func;
-       hip_relrec_set_lifetime(rec, lifetime);
-       rec->created = time(NULL);
-     
-       return rec;
+    if (hit_r == NULL || ip_r == NULL || hmac == NULL || func == NULL) {
+        return NULL;
+    }
+
+    hip_relrec_t *rec = (hip_relrec_t *) malloc(sizeof(hip_relrec_t));
+
+    if (rec == NULL) {
+        HIP_ERROR("Error allocating memory for HIP relay record.\n");
+        return NULL;
+    }
+    rec->type       = type;
+    memcpy(&(rec->hit_r), hit_r, sizeof(*hit_r));
+    memcpy(&(rec->ip_r), ip_r, sizeof(*ip_r));
+    rec->udp_port_r = port;
+    memcpy(&(rec->hmac_relay), hmac, sizeof(*hmac));
+    rec->send_fn    = func;
+    hip_relrec_set_lifetime(rec, lifetime);
+    rec->created    = time(NULL);
+
+    return rec;
 }
 
 void hip_relrec_set_mode(hip_relrec_t *rec, const hip_relrec_type_t type)
 {
-       if(rec != NULL)
-               rec->type = type;
+    if (rec != NULL) {
+        rec->type = type;
+    }
 }
 
 void hip_relrec_set_lifetime(hip_relrec_t *rec, const uint8_t lifetime)
 {
-       if(rec != NULL) {
-               rec->lifetime = pow(2, ((double)(lifetime-64)/8));
-       }
+    if (rec != NULL) {
+        rec->lifetime = pow(2, ((double) (lifetime - 64) / 8));
+    }
 }
 
 void hip_relrec_set_udpport(hip_relrec_t *rec, const in_port_t port)
 {
-       if(rec != NULL)
-               rec->udp_port_r = port;
+    if (rec != NULL) {
+        rec->udp_port_r = port;
+    }
 }
 
 void hip_relrec_info(const hip_relrec_t *rec)
 {
-       if(rec == NULL)
-               return;
-     
-       char status[1024];
-       char *cursor = status;
-       cursor += sprintf(cursor, "Relay record info:\n");
-       cursor += sprintf(cursor, " Record type: ");
-       cursor += sprintf(cursor, (rec->type == HIP_RELAY) ?
-                         "Full relay of HIP packets\n" :
-                         (rec->type == HIP_RVSRELAY) ?
-                         "RVS relay of I1 packet\n" : 
-                         rec->type == HIP_FULLRELAY ?
-                         "Full relay of HIP and ESP packets" :
-                         "undefined\n");
-       cursor += sprintf(cursor, " Record lifetime: %lu seconds\n",
-                         rec->lifetime);
-       cursor += sprintf(cursor, " Record created: %lu seconds ago\n",
-                         time(NULL) - rec->created);
-       cursor += sprintf(cursor, " HIT of R: %04x:%04x:%04x:%04x:"\
-                         "%04x:%04x:%04x:%04x\n",
-                         ntohs(rec->hit_r.s6_addr16[0]),
-                         ntohs(rec->hit_r.s6_addr16[1]),
-                         ntohs(rec->hit_r.s6_addr16[2]),
-                         ntohs(rec->hit_r.s6_addr16[3]),
-                         ntohs(rec->hit_r.s6_addr16[4]),
-                         ntohs(rec->hit_r.s6_addr16[5]),
-                         ntohs(rec->hit_r.s6_addr16[6]),
-                         ntohs(rec->hit_r.s6_addr16[7]));
-       cursor += sprintf(cursor, " IP of R:  %04x:%04x:%04x:%04x:"\
-                         "%04x:%04x:%04x:%04x\n",
-                         ntohs(rec->ip_r.s6_addr16[0]),
-                         ntohs(rec->ip_r.s6_addr16[1]),
-                         ntohs(rec->ip_r.s6_addr16[2]),
-                         ntohs(rec->ip_r.s6_addr16[3]),
-                         ntohs(rec->ip_r.s6_addr16[4]),
-                         ntohs(rec->ip_r.s6_addr16[5]),
-                         ntohs(rec->ip_r.s6_addr16[6]),
-                         ntohs(rec->ip_r.s6_addr16[7]));
-
-       HIP_DEBUG("\n%s", status);
+    if (rec == NULL) {
+        return;
+    }
+
+    char status[1024];
+    char *cursor = status;
+    cursor += sprintf(cursor, "Relay record info:\n");
+    cursor += sprintf(cursor, " Record type: ");
+    cursor += sprintf(cursor, (rec->type == HIP_RELAY) ?
+                      "Full relay of HIP packets\n" :
+                      (rec->type == HIP_RVSRELAY) ?
+                      "RVS relay of I1 packet\n" :
+                      rec->type == HIP_FULLRELAY ?
+                      "Full relay of HIP and ESP packets" :
+                      "undefined\n");
+    cursor += sprintf(cursor, " Record lifetime: %lu seconds\n",
+                      rec->lifetime);
+    cursor += sprintf(cursor, " Record created: %lu seconds ago\n",
+                      time(NULL) - rec->created);
+    cursor += sprintf(cursor, " HIT of R: %04x:%04x:%04x:%04x:" \
+                              "%04x:%04x:%04x:%04x\n",
+                      ntohs(rec->hit_r.s6_addr16[0]),
+                      ntohs(rec->hit_r.s6_addr16[1]),
+                      ntohs(rec->hit_r.s6_addr16[2]),
+                      ntohs(rec->hit_r.s6_addr16[3]),
+                      ntohs(rec->hit_r.s6_addr16[4]),
+                      ntohs(rec->hit_r.s6_addr16[5]),
+                      ntohs(rec->hit_r.s6_addr16[6]),
+                      ntohs(rec->hit_r.s6_addr16[7]));
+    cursor += sprintf(cursor, " IP of R:  %04x:%04x:%04x:%04x:" \
+                              "%04x:%04x:%04x:%04x\n",
+                      ntohs(rec->ip_r.s6_addr16[0]),
+                      ntohs(rec->ip_r.s6_addr16[1]),
+                      ntohs(rec->ip_r.s6_addr16[2]),
+                      ntohs(rec->ip_r.s6_addr16[3]),
+                      ntohs(rec->ip_r.s6_addr16[4]),
+                      ntohs(rec->ip_r.s6_addr16[5]),
+                      ntohs(rec->ip_r.s6_addr16[6]),
+                      ntohs(rec->ip_r.s6_addr16[7]));
+
+    HIP_DEBUG("\n%s", status);
 }
 
 static unsigned long hip_relwl_hash(const hip_hit_t *hit)
 {
-       if(hit == NULL)
-               return 0;
-       
-       return hip_hash_func(hit);
+    if (hit == NULL) {
+        return 0;
+    }
+
+    return hip_hash_func(hit);
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
@@ -460,10 +473,11 @@
 
 static int hip_relwl_cmp(const hip_hit_t *hit1, const hip_hit_t *hit2)
 {
-       if(hit1 == NULL || hit2 == NULL)
-               return 1;
+    if (hit1 == NULL || hit2 == NULL) {
+        return 1;
+    }
 
-       return (hip_relwl_hash(hit1) != hip_relwl_hash(hit2));
+    return hip_relwl_hash(hit1) != hip_relwl_hash(hit2);
 }
 
 /** A callback wrapper of the prototype required by @c lh_new(). */
@@ -471,63 +485,68 @@
 
 static int hip_relwl_put(hip_hit_t *hit)
 {
-       if(hiprelay_wl == NULL || hit == NULL)
-               return -1;
-     
-       /* If we are trying to insert a duplicate element (same HIT), we have to
-          delete the previous entry. If we do not do so, only the pointer in
-          the hashtable is replaced and the reference to the previous element
-          is lost resulting in a memory leak. */
-       hip_hit_t *dummy = hip_relwl_get(hit);
-       if(dummy != NULL) {
-               hip_relwl_hit_free_doall(dummy);
-               list_add(hit, hiprelay_wl);
-               return -1;
-       } else {
-               list_add(hit, hiprelay_wl);
-               return 0;
-       }
+    if (hiprelay_wl == NULL || hit == NULL) {
+        return -1;
+    }
+
+    /* If we are trying to insert a duplicate element (same HIT), we have to
+     * delete the previous entry. If we do not do so, only the pointer in
+     * the hashtable is replaced and the reference to the previous element
+     * is lost resulting in a memory leak. */
+    hip_hit_t *dummy = hip_relwl_get(hit);
+    if (dummy != NULL) {
+        hip_relwl_hit_free_doall(dummy);
+        list_add(hit, hiprelay_wl);
+        return -1;
+    } else {
+        list_add(hit, hiprelay_wl);
+        return 0;
+    }
 }
 
 hip_hit_t *hip_relwl_get(const hip_hit_t *hit)
 {
-       if(hiprelay_wl == NULL || hit == NULL)
-               return NULL;
+    if (hiprelay_wl == NULL || hit == NULL) {
+        return NULL;
+    }
 
-       return (hip_hit_t *)list_find(hit, hiprelay_wl);
+    return (hip_hit_t *) list_find(hit, hiprelay_wl);
 }
 
 #ifdef CONFIG_HIP_DEBUG
 /**
  * Returns the number of HITs in the hashtable @c hiprelay_wl.
- * 
+ *
  * @return  number of HITs in the hashtable.
  */
 static unsigned long hip_relwl_size(void)
 {
-       if(hiprelay_wl == NULL)
-               return 0;
+    if (hiprelay_wl == NULL) {
+        return 0;
+    }
 
-       return ((struct lhash_st *) hiprelay_wl)->num_items;
+    return ((struct lhash_st *) hiprelay_wl)->num_items;
 }
+
 #endif /* CONFIG_HIP_DEBUG */
 
 static void hip_relwl_hit_free_doall(hip_hit_t *hit)
 {
-       if(hiprelay_wl == NULL || hit == NULL)
-               return;
-       
-       /* Check if such element exist, and delete the pointer from the 
hashtable. */
-       hip_hit_t *deleted_hit = list_del(hit, hiprelay_wl);
-
-       /* Free the memory allocated for the element. */
-       if(deleted_hit != NULL) {
-               /* We set the memory to '\0' because the user may still have a
-                  reference to the memory region that is freed here. */
-               memset(deleted_hit, '\0', sizeof(*deleted_hit));
-               free(deleted_hit);
-               HIP_DEBUG("HIT deleted from the relay whitelist.\n");
-       }
+    if (hiprelay_wl == NULL || hit == NULL) {
+        return;
+    }
+
+    /* Check if such element exist, and delete the pointer from the hashtable. 
*/
+    hip_hit_t *deleted_hit = list_del(hit, hiprelay_wl);
+
+    /* Free the memory allocated for the element. */
+    if (deleted_hit != NULL) {
+        /* We set the memory to '\0' because the user may still have a
+         * reference to the memory region that is freed here. */
+        memset(deleted_hit, '\0', sizeof(*deleted_hit));
+        free(deleted_hit);
+        HIP_DEBUG("HIT deleted from the relay whitelist.\n");
+    }
 }
 
 /** A callback wrapper of the prototype required by @c lh_doall(). */
@@ -535,281 +554,277 @@
 
 hip_relay_wl_status_t hip_relwl_get_status()
 {
-       return whitelist_enabled;
+    return whitelist_enabled;
 }
 
 int hip_rvs_validate_lifetime(uint8_t requested_lifetime,
-                             uint8_t *granted_lifetime)
+                              uint8_t *granted_lifetime)
 {
-       if(requested_lifetime < hiprelay_min_lifetime){
-               *granted_lifetime = hiprelay_min_lifetime;
-               return -1;
-       }else if(requested_lifetime > hiprelay_max_lifetime){
-               *granted_lifetime = hiprelay_max_lifetime;
-               return -1;
-       }else{
-               *granted_lifetime = requested_lifetime;
-               return 0;
-       }
+    if (requested_lifetime < hiprelay_min_lifetime) {
+        *granted_lifetime = hiprelay_min_lifetime;
+        return -1;
+    } else if (requested_lifetime > hiprelay_max_lifetime)   {
+        *granted_lifetime = hiprelay_max_lifetime;
+        return -1;
+    } else {
+        *granted_lifetime = requested_lifetime;
+        return 0;
+    }
 }
 
 int hip_relay_forward(const hip_common_t *msg, const in6_addr_t *saddr,
-                 const in6_addr_t *daddr, hip_relrec_t *rec,
-                 const hip_portpair_t *info, const uint8_t type_hdr,
-                  const hip_relrec_type_t relay_type)
+                      const in6_addr_t *daddr, hip_relrec_t *rec,
+                      const hip_portpair_t *info, const uint8_t type_hdr,
+                      const hip_relrec_type_t relay_type)
 {
-       hip_common_t *msg_to_be_relayed = NULL;
-       hip_tlv_common_t *current_param = NULL;
-       int err = 0, from_added = 0;
-        hip_tlv_type_t param_type = 0;
-        hip_tlv_type_t hmac_param_type = 0;
-
-       /* A function pointer to either hip_build_param_from() or
-          hip_build_param_relay_from(). */
-       int (*builder_function) (struct hip_common *msg,
-                                const struct in6_addr *addr,
-                                const in_port_t port);
-
-       HIP_DEBUG("hip_relay_rvs() invoked.\n");
-        HIP_DEBUG("Msg type :      %s (%d)\n",
-              hip_message_type_name(hip_get_msg_type(msg)),
-              hip_get_msg_type(msg));
-        HIP_DEBUG_IN6ADDR("hip_relay_rvs():  source address", saddr);
-       HIP_DEBUG_IN6ADDR("hip_relay_rvs():  destination address", daddr);
-       HIP_DEBUG_HIT("hip_relay_rvs(): Relay record hit", &rec->hit_r);
-       HIP_DEBUG("Relay record port: %d.\n", rec->udp_port_r);
-       HIP_DEBUG("source port: %u, destination port: %u\n",
-                 info->src_port, info->dst_port);
-               
-        if (relay_type == HIP_RVSRELAY)
-        {
-            param_type = HIP_PARAM_FROM;
-            builder_function = hip_build_param_from;
-        } else
-       {
-            param_type = HIP_PARAM_RELAY_FROM;
-            builder_function = hip_build_param_relay_from;
-        }
-
-       HIP_IFEL(!(msg_to_be_relayed = hip_msg_alloc()), -ENOMEM,
-                "No memory to copy original I1\n");    
-
-       /* The packet forwarding is achieved by rewriting the source and
-          destination IP addresses. */
-       hip_build_network_hdr(msg_to_be_relayed, type_hdr, 0,
-                             &(msg->hits), &(msg->hitr));
-
-       /* Adding FROM (RELAY_FROM) parameter. Loop through all the parameters 
in
-          the received packet, and insert a new FROM (RELAY_FROM) parameter
-          after the last found FROM (RELAY_FROM) parameter. Notice that in most
-          cases the incoming I1 has no paramaters at all, and this "while" loop
-          is skipped. Multiple rvses en route to responder is one (and only?)
-          case when the incoming I1 packet has parameters. */
-       while ((current_param = hip_get_next_param(msg, current_param)) != 
NULL) {
-               
-               HIP_DEBUG("Found parameter in the packet.\n");
-               /* Copy while type is smaller than or equal to FROM (RELAY_FROM)
-                  or a new FROM (RELAY_FROM) has already been added. */
-               if (from_added || hip_get_param_type(current_param) <= 
param_type) {
-                       HIP_DEBUG("Copying existing parameter to the packet "\
-                                 "to be relayed.\n");
-                       hip_build_param(msg_to_be_relayed,current_param);
-                       continue;
-               }
-               /* Parameter under inspection has greater type than FROM
-                  (RELAY_FROM) parameter: insert a new FROM (RELAY_FROM) 
parameter
-                  between the last found FROM (RELAY_FROM) parameter and
-                  "current_param". */
-               else {
-                       HIP_DEBUG("Created new %s and copied "\
-                                 "current parameter to relayed packet.\n",
-                                 hip_param_type_name(param_type));
-                       builder_function(msg_to_be_relayed, saddr,
-                                        info->src_port);
-                       hip_build_param(msg_to_be_relayed, current_param);
-                       from_added = 1;
-               }
-       }
-
-       /* If the incoming packet had no parameters after the existing FROM 
(RELAY_FROM)
-          parameters, new FROM (RELAY_FROM) parameter is not added until here. 
*/
-       if (!from_added) {
-               HIP_DEBUG("No parameters found, adding a new %s.\n",
-                         hip_param_type_name(param_type));
-               builder_function(msg_to_be_relayed, saddr, info->src_port);
-       }
-
-       /* Zero message HIP checksum. */
-       hip_zero_msg_checksum(msg_to_be_relayed);
-
-       if (relay_type == HIP_RVSRELAY)
-               hmac_param_type = HIP_PARAM_RVS_HMAC;
-       else
-               hmac_param_type = HIP_PARAM_RELAY_HMAC;
-
-        /* Adding RVS_HMAC or RELAY_HMAC parameter as the last parameter of 
the relayed
-          packet. Notice, that this presumes that there are no parameters
-          whose type value is greater than RVS_HMAC or RELAY_HMAC in the 
incoming I1
-          packet. */
-       HIP_DEBUG("Adding a new RVS_HMAC or RELAY_HMAC parameter as the last 
parameter.\n");
-       HIP_IFEL(hip_build_param_hmac(msg_to_be_relayed,
-                                     &(rec->hmac_relay),
-                                     hmac_param_type), -1,
-                "Building of RVS_HMAC or RELAY_HMAC failed.\n");
-       
-       /* If the client is behind NAT the packet is relayed on UDP. If
-          there is no NAT the packet is relayed on raw HIP. We don't have to
-          take care of which send-function to use, as the rec->send_fn was
-          initiated with correct value when the relay relay was created. Note
-          that we use NULL as source IP address instead of
-          i1_daddr. A source address is selected in the corresponding
-          send-function. */
-
-       if (info->src_port)
-        {
-               // if the incoming message is via UDP, the RVS relay must use 
UDP also.
-               HIP_IFEL(hip_send_pkt(NULL, &(rec->ip_r), 
hip_get_local_nat_udp_port(),
-                                     rec->udp_port_r, msg_to_be_relayed, NULL, 
0),
-                        -ECOMM, "Relaying the packet failed.\n");
-       } else
-        {
-               HIP_IFEL(rec->send_fn(NULL, &(rec->ip_r), 
hip_get_local_nat_udp_port(),
-                                     rec->udp_port_r, msg_to_be_relayed, NULL, 
0),
-                        -ECOMM, "Relaying the packet failed.\n");
-       }
-       
-       /* Once we have relayed the I1 packet successfully, we update the time 
of
-          last contact. */
-       rec->last_contact = time(NULL);
-
-       HIP_DEBUG_HIT("hip_relay_forward(): Relayed the packet to", 
&(rec->ip_r));
-
- out_err:
-       if (msg_to_be_relayed != NULL)
-            free(msg_to_be_relayed);
-
-       return err;
+    hip_common_t *msg_to_be_relayed = NULL;
+    hip_tlv_common_t *current_param = NULL;
+    int err                         = 0, from_added = 0;
+    hip_tlv_type_t param_type       = 0;
+    hip_tlv_type_t hmac_param_type  = 0;
+
+    /* A function pointer to either hip_build_param_from() or
+     * hip_build_param_relay_from(). */
+    int (*builder_function)(struct hip_common *msg,
+                            const struct in6_addr *addr,
+                            const in_port_t port);
+
+    HIP_DEBUG("hip_relay_rvs() invoked.\n");
+    HIP_DEBUG("Msg type :      %s (%d)\n",
+              hip_message_type_name(hip_get_msg_type(msg)),
+              hip_get_msg_type(msg));
+    HIP_DEBUG_IN6ADDR("hip_relay_rvs():  source address", saddr);
+    HIP_DEBUG_IN6ADDR("hip_relay_rvs():  destination address", daddr);
+    HIP_DEBUG_HIT("hip_relay_rvs(): Relay record hit", &rec->hit_r);
+    HIP_DEBUG("Relay record port: %d.\n", rec->udp_port_r);
+    HIP_DEBUG("source port: %u, destination port: %u\n",
+              info->src_port, info->dst_port);
+
+    if (relay_type == HIP_RVSRELAY) {
+        param_type       = HIP_PARAM_FROM;
+        builder_function = hip_build_param_from;
+    } else {
+        param_type       = HIP_PARAM_RELAY_FROM;
+        builder_function = hip_build_param_relay_from;
+    }
+
+    HIP_IFEL(!(msg_to_be_relayed = hip_msg_alloc()), -ENOMEM,
+             "No memory to copy original I1\n");
+
+    /* The packet forwarding is achieved by rewriting the source and
+     * destination IP addresses. */
+    hip_build_network_hdr(msg_to_be_relayed, type_hdr, 0,
+                          &(msg->hits), &(msg->hitr));
+
+    /* Adding FROM (RELAY_FROM) parameter. Loop through all the parameters in
+     * the received packet, and insert a new FROM (RELAY_FROM) parameter
+     * after the last found FROM (RELAY_FROM) parameter. Notice that in most
+     * cases the incoming I1 has no paramaters at all, and this "while" loop
+     * is skipped. Multiple rvses en route to responder is one (and only?)
+     * case when the incoming I1 packet has parameters. */
+    while ((current_param = hip_get_next_param(msg, current_param)) != NULL) {
+        HIP_DEBUG("Found parameter in the packet.\n");
+        /* Copy while type is smaller than or equal to FROM (RELAY_FROM)
+         * or a new FROM (RELAY_FROM) has already been added. */
+        if (from_added || hip_get_param_type(current_param) <= param_type) {
+            HIP_DEBUG("Copying existing parameter to the packet " \
+                      "to be relayed.\n");
+            hip_build_param(msg_to_be_relayed, current_param);
+            continue;
+        }
+        /* Parameter under inspection has greater type than FROM
+         * (RELAY_FROM) parameter: insert a new FROM (RELAY_FROM) parameter
+         * between the last found FROM (RELAY_FROM) parameter and
+         * "current_param". */
+        else {
+            HIP_DEBUG("Created new %s and copied " \
+                      "current parameter to relayed packet.\n",
+                      hip_param_type_name(param_type));
+            builder_function(msg_to_be_relayed, saddr,
+                             info->src_port);
+            hip_build_param(msg_to_be_relayed, current_param);
+            from_added = 1;
+        }
+    }
+
+    /* If the incoming packet had no parameters after the existing FROM 
(RELAY_FROM)
+     * parameters, new FROM (RELAY_FROM) parameter is not added until here. */
+    if (!from_added) {
+        HIP_DEBUG("No parameters found, adding a new %s.\n",
+                  hip_param_type_name(param_type));
+        builder_function(msg_to_be_relayed, saddr, info->src_port);
+    }
+
+    /* Zero message HIP checksum. */
+    hip_zero_msg_checksum(msg_to_be_relayed);
+
+    if (relay_type == HIP_RVSRELAY) {
+        hmac_param_type = HIP_PARAM_RVS_HMAC;
+    } else {
+        hmac_param_type = HIP_PARAM_RELAY_HMAC;
+    }
+
+    /* Adding RVS_HMAC or RELAY_HMAC parameter as the last parameter of the 
relayed
+     * packet. Notice, that this presumes that there are no parameters
+     * whose type value is greater than RVS_HMAC or RELAY_HMAC in the incoming 
I1
+     * packet. */
+    HIP_DEBUG("Adding a new RVS_HMAC or RELAY_HMAC parameter as the last 
parameter.\n");
+    HIP_IFEL(hip_build_param_hmac(msg_to_be_relayed,
+                                  &(rec->hmac_relay),
+                                  hmac_param_type), -1,
+             "Building of RVS_HMAC or RELAY_HMAC failed.\n");
+
+    /* If the client is behind NAT the packet is relayed on UDP. If
+     * there is no NAT the packet is relayed on raw HIP. We don't have to
+     * take care of which send-function to use, as the rec->send_fn was
+     * initiated with correct value when the relay relay was created. Note
+     * that we use NULL as source IP address instead of
+     * i1_daddr. A source address is selected in the corresponding
+     * send-function. */
+
+    if (info->src_port) {
+        // if the incoming message is via UDP, the RVS relay must use UDP also.
+        HIP_IFEL(hip_send_pkt(NULL, &(rec->ip_r), hip_get_local_nat_udp_port(),
+                              rec->udp_port_r, msg_to_be_relayed, NULL, 0),
+                 -ECOMM, "Relaying the packet failed.\n");
+    } else {
+        HIP_IFEL(rec->send_fn(NULL, &(rec->ip_r), hip_get_local_nat_udp_port(),
+                              rec->udp_port_r, msg_to_be_relayed, NULL, 0),
+                 -ECOMM, "Relaying the packet failed.\n");
+    }
+
+    /* Once we have relayed the I1 packet successfully, we update the time of
+     * last contact. */
+    rec->last_contact = time(NULL);
+
+    HIP_DEBUG_HIT("hip_relay_forward(): Relayed the packet to", &(rec->ip_r));
+
+out_err:
+    if (msg_to_be_relayed != NULL) {
+        free(msg_to_be_relayed);
+    }
+
+    return err;
 }
 
-static int hip_relay_read_config(){
-       FILE *fp = NULL;
-       int lineerr = 0, parseerr = 0, err = 0;
-       char parameter[HIP_RELAY_MAX_PAR_LEN + 1];
-       hip_configvaluelist_t values;
-       hip_hit_t hit, *wl_hit = NULL;
-       uint8_t max = 255; /* Theoretical maximum lifetime value. */
-
-       HIP_IFEL(((fp = fopen(HIP_RELAY_CONFIG_FILE, "r")) == NULL), -ENOENT,
-                "Cannot open file %s for reading.\n", HIP_RELAY_CONFIG_FILE);
-       
-       do {
-               parseerr = 0;
-               memset(parameter, '\0', sizeof(parameter));
-               hip_cvl_init(&values);
-               lineerr = hip_cf_get_line_data(fp, parameter, &values, 
&parseerr);
-                               
-               if(parseerr == 0){
-                       _HIP_DEBUG("param: '%s'\n", parameter);
-                       hip_configfilevalue_t *current = NULL;
-                       if(strcmp(parameter, "whitelist_enabled") == 0) {
-                               current = hip_cvl_get_next(&values, current);
-                               if(strcmp(current->data, "no") == 0) {
-                                       whitelist_enabled = HIP_RELAY_WL_OFF;
-                               }
-                       } else if(strcmp(parameter, "whitelist") == 0) {
-                               while((current = 
-                                      hip_cvl_get_next(&values, current))
-                                     != NULL) {
-                                       /* Try to convert the characters to an
-                                          IPv6 address. */
-                                       if(inet_pton(AF_INET6, current->data,
-                                                    &hit) > 0)
-                                       {
-                                               /* store the HIT to the 
whitelist. */
-                                               wl_hit = (hip_hit_t*)
-                                                       
malloc(sizeof(hip_hit_t));
-                                               if(wl_hit == NULL) {
-                                                       HIP_ERROR("Error "\
-                                                                 "allocating "\
-                                                                 "memory for "\
-                                                                 "whitelist "\
-                                                                 "HIT.\n");
-                                                       break;
-                                               }
-                                               memcpy(wl_hit, &hit, 
sizeof(hit));
-                                               hip_relwl_put(wl_hit);
-                                               print_node(current);
-                                       }
-                               }
-                       } else if(strcmp(parameter, "minimum_lifetime") == 0) {
-                               time_t tmp = 0;
-                               uint8_t val = 0;
-                               current = hip_cvl_get_next(&values, current);
-                               tmp = atol(current->data);
-                               
-                               if(hip_get_lifetime_value(tmp, &val) == 0) {
-                                       /* hip_get_lifetime_value() truncates 
the
-                                          value. We want the minimum to be at
-                                          least the value specified. */
-                                       if(val < max) {
-                                               val++;
-                                       }
-                                       hiprelay_min_lifetime = val;
-                               }
-                       } else if(strcmp(parameter, "maximum_lifetime") == 0) {
-                               time_t tmp = 0;
-                               uint8_t val = 0;
-                               current = hip_cvl_get_next(&values, current);
-                               tmp = atol(current->data);
-                               
-                               if(hip_get_lifetime_value(tmp, &val) == 0) {
-                                       hiprelay_max_lifetime = val;
-                               }
-                       }
-               }
-
-               hip_cvl_uninit(&values);
-               
-       } while(lineerr != EOF);
-       
-       if(fclose(fp) != 0) {
-               HIP_ERROR("Cannot close file %s.\n", HIP_RELAY_CONFIG_FILE);
-       }
-       
-       /* Check that the read values are sane. If not, rollback to defaults. */
-       if(hiprelay_min_lifetime > hiprelay_max_lifetime) {
-               hiprelay_min_lifetime = HIP_RELREC_MIN_LIFETIME;
-               hiprelay_max_lifetime = HIP_RELREC_MAX_LIFETIME;
-       }
-
-       HIP_DEBUG("\nRead relay configuration file with following values:\n"\
-                 "Whitelist enabled: %s\nNumber of HITs in the whitelist: "\
-                 "%lu\nMinimum lifetime: %ld\nMaximum lifetime: %ld\n",
-                 (whitelist_enabled) ? "YES" : "NO", hip_relwl_size(),
-                 hiprelay_min_lifetime, hiprelay_max_lifetime);
-       
- out_err:
-       
-       return err;
+static int hip_relay_read_config()
+{
+    FILE *fp    = NULL;
+    int lineerr = 0, parseerr = 0, err = 0;
+    char parameter[HIP_RELAY_MAX_PAR_LEN + 1];
+    hip_configvaluelist_t values;
+    hip_hit_t hit, *wl_hit = NULL;
+    uint8_t max = 255;     /* Theoretical maximum lifetime value. */
+
+    HIP_IFEL(((fp = fopen(HIP_RELAY_CONFIG_FILE, "r")) == NULL), -ENOENT,
+             "Cannot open file %s for reading.\n", HIP_RELAY_CONFIG_FILE);
+
+    do {
+        parseerr = 0;
+        memset(parameter, '\0', sizeof(parameter));
+        hip_cvl_init(&values);
+        lineerr  = hip_cf_get_line_data(fp, parameter, &values, &parseerr);
+
+        if (parseerr == 0) {
+            _HIP_DEBUG("param: '%s'\n", parameter);
+            hip_configfilevalue_t *current = NULL;
+            if (strcmp(parameter, "whitelist_enabled") == 0) {
+                current = hip_cvl_get_next(&values, current);
+                if (strcmp(current->data, "no") == 0) {
+                    whitelist_enabled = HIP_RELAY_WL_OFF;
+                }
+            } else if (strcmp(parameter, "whitelist") == 0) {
+                while ((current =
+                            hip_cvl_get_next(&values, current))
+                       != NULL) {
+                    /* Try to convert the characters to an
+                     * IPv6 address. */
+                    if (inet_pton(AF_INET6, current->data,
+                                  &hit) > 0) {
+                        /* store the HIT to the whitelist. */
+                        wl_hit = (hip_hit_t *)
+                                 malloc(sizeof(hip_hit_t));
+                        if (wl_hit == NULL) {
+                            HIP_ERROR("Error " \
+                                      "allocating " \
+                                      "memory for " \
+                                      "whitelist " \
+                                      "HIT.\n");
+                            break;
+                        }
+                        memcpy(wl_hit, &hit, sizeof(hit));
+                        hip_relwl_put(wl_hit);
+                        print_node(current);
+                    }
+                }
+            } else if (strcmp(parameter, "minimum_lifetime") == 0) {
+                time_t tmp  = 0;
+                uint8_t val = 0;
+                current = hip_cvl_get_next(&values, current);
+                tmp     = atol(current->data);
+
+                if (hip_get_lifetime_value(tmp, &val) == 0) {
+                    /* hip_get_lifetime_value() truncates the
+                     * value. We want the minimum to be at
+                     * least the value specified. */
+                    if (val < max) {
+                        val++;
+                    }
+                    hiprelay_min_lifetime = val;
+                }
+            } else if (strcmp(parameter, "maximum_lifetime") == 0) {
+                time_t tmp  = 0;
+                uint8_t val = 0;
+                current = hip_cvl_get_next(&values, current);
+                tmp     = atol(current->data);
+
+                if (hip_get_lifetime_value(tmp, &val) == 0) {
+                    hiprelay_max_lifetime = val;
+                }
+            }
+        }
+
+        hip_cvl_uninit(&values);
+    } while (lineerr != EOF);
+
+    if (fclose(fp) != 0) {
+        HIP_ERROR("Cannot close file %s.\n", HIP_RELAY_CONFIG_FILE);
+    }
+
+    /* Check that the read values are sane. If not, rollback to defaults. */
+    if (hiprelay_min_lifetime > hiprelay_max_lifetime) {
+        hiprelay_min_lifetime = HIP_RELREC_MIN_LIFETIME;
+        hiprelay_max_lifetime = HIP_RELREC_MAX_LIFETIME;
+    }
+
+    HIP_DEBUG("\nRead relay configuration file with following values:\n" \
+              "Whitelist enabled: %s\nNumber of HITs in the whitelist: " \
+              "%lu\nMinimum lifetime: %ld\nMaximum lifetime: %ld\n",
+              (whitelist_enabled) ? "YES" : "NO", hip_relwl_size(),
+              hiprelay_min_lifetime, hiprelay_max_lifetime);
+
+out_err:
+
+    return err;
 }
 
 static int hip_relay_write_config()
 {
-       int err = 0;
-       FILE *fp = NULL;
-
-       HIP_IFEL(((fp = fopen(HIP_RELAY_CONFIG_FILE, "w")) == NULL), -ENOENT,
-                "Cannot open file %s for writing.\n", HIP_RELAY_CONFIG_FILE);
-
-       fprintf(fp, HIP_RC_FILE_FORMAT_STRING, HIP_RC_FILE_CONTENT);
-
-       if(fclose(fp) != 0) {
-               HIP_ERROR("Cannot close file %s.\n", HIP_RELAY_CONFIG_FILE);
-       }
-
- out_err:
-
-       return err;
+    int err  = 0;
+    FILE *fp = NULL;
+
+    HIP_IFEL(((fp = fopen(HIP_RELAY_CONFIG_FILE, "w")) == NULL), -ENOENT,
+             "Cannot open file %s for writing.\n", HIP_RELAY_CONFIG_FILE);
+
+    fprintf(fp, HIP_RC_FILE_FORMAT_STRING, HIP_RC_FILE_CONTENT);
+
+    if (fclose(fp) != 0) {
+        HIP_ERROR("Cannot close file %s.\n", HIP_RELAY_CONFIG_FILE);
+    }
+
+out_err:
+
+    return err;
 }
 
 /**
@@ -818,65 +833,65 @@
  *           No lines over 80 characters. No capital letters in funcion names
  *           (C-style). Function commmends should be in header file.
  */
-int hip_relay_handle_relay_to(struct hip_common * msg,
-                             int msg_type,                           
-                             struct in6_addr *src_addr,
-                             struct in6_addr *dst_addr,
-                             hip_portpair_t *msg_info)
+int hip_relay_handle_relay_to(struct hip_common *msg,
+                              int msg_type,
+                              struct in6_addr *src_addr,
+                              struct in6_addr *dst_addr,
+                              hip_portpair_t *msg_info)
 {
-       int err = 0;
-       hip_relrec_t *rec = NULL, dummy;
-       struct hip_relay_to *relay_to;
-       //check if full relay service is active
-       
-       if(hip_relay_get_status() == HIP_RELAY_OFF) {
-               /* Should we set err to -1? */
-               goto out_err;
-       }
-       
-       HIP_DEBUG("handle_relay_to: full relay is on\n");
-       // check if the relay has been registered
-       
-       /* Check if we have a relay record in our database matching the
-          I's HIT. We should find one, if the I is
-          registered to relay.*/
-       HIP_DEBUG_HIT("Searching relay record on HIT:", &msg->hits);
-       memcpy(&(dummy.hit_r), &msg->hits, sizeof(msg->hits));
-       rec = hip_relht_get(&dummy);
-       
-       if(rec == NULL) {
-               HIP_DEBUG("handle_relay_to: No matching relay record found.\n");
-               goto out_err;
-       } else if(rec->type == HIP_RVSRELAY) {
-               goto out_err;
-       }
-  
-       HIP_DEBUG("handle_relay_to: Matching relay record found:Full-Relay.\n");
-       
-       //check if there is a relay_to parameter            
-       relay_to = (struct hip_relay_to *) hip_get_param(msg, 
HIP_PARAM_RELAY_TO);
-       HIP_IFEL(!relay_to, 0, "No relay_to  found\n");
-       
-       // check msg type       
-       switch(msg_type) {
-       case HIP_R1:
-       case HIP_R2:
-       case HIP_UPDATE:
-       case HIP_NOTIFY:
-               HIP_DEBUG_IN6ADDR("the relay to address: ",
-                                 (struct in6_addr *)&relay_to->address);
-               HIP_DEBUG("the relay to ntohs(port): %d",
-                         ntohs(relay_to->port));
-               hip_relay_forward_response(
-                       msg, msg_type, src_addr, dst_addr, msg_info,
-                       (in6_addr_t *)&relay_to->address, 
ntohs(relay_to->port));
-               //  state = HIP_STATE_NONE;
-               err = 1;
-               goto out_err;
-       }
-       
- out_err:
-       return err;    
+    int err           = 0;
+    hip_relrec_t *rec = NULL, dummy;
+    struct hip_relay_to *relay_to;
+    //check if full relay service is active
+
+    if (hip_relay_get_status() == HIP_RELAY_OFF) {
+        /* Should we set err to -1? */
+        goto out_err;
+    }
+
+    HIP_DEBUG("handle_relay_to: full relay is on\n");
+    // check if the relay has been registered
+
+    /* Check if we have a relay record in our database matching the
+     * I's HIT. We should find one, if the I is
+     * registered to relay.*/
+    HIP_DEBUG_HIT("Searching relay record on HIT:", &msg->hits);
+    memcpy(&(dummy.hit_r), &msg->hits, sizeof(msg->hits));
+    rec = hip_relht_get(&dummy);
+
+    if (rec == NULL) {
+        HIP_DEBUG("handle_relay_to: No matching relay record found.\n");
+        goto out_err;
+    } else if (rec->type == HIP_RVSRELAY) {
+        goto out_err;
+    }
+
+    HIP_DEBUG("handle_relay_to: Matching relay record found:Full-Relay.\n");
+
+    //check if there is a relay_to parameter
+    relay_to = (struct hip_relay_to *) hip_get_param(msg, HIP_PARAM_RELAY_TO);
+    HIP_IFEL(!relay_to, 0, "No relay_to  found\n");
+
+    // check msg type
+    switch (msg_type) {
+    case HIP_R1:
+    case HIP_R2:
+    case HIP_UPDATE:
+    case HIP_NOTIFY:
+        HIP_DEBUG_IN6ADDR("the relay to address: ",
+                          (struct in6_addr *) &relay_to->address);
+        HIP_DEBUG("the relay to ntohs(port): %d",
+                  ntohs(relay_to->port));
+        hip_relay_forward_response(
+            msg, msg_type, src_addr, dst_addr, msg_info,
+            (in6_addr_t *) &relay_to->address, ntohs(relay_to->port));
+        //  state = HIP_STATE_NONE;
+        err = 1;
+        goto out_err;
+    }
+
+out_err:
+    return err;
 }
 
 /**
@@ -886,89 +901,85 @@
  *           (C-style). Function commmends should be in header file.
  */
 static int hip_relay_forward_response(const hip_common_t *r,
-                                     const uint8_t type_hdr, 
-                                     const in6_addr_t *r_saddr,
-                                     const in6_addr_t *r_daddr , 
-                                     const hip_portpair_t *r_info , 
-                                     const in6_addr_t *relay_to_addr,
-                                     const in_port_t relay_to_port)
+                                      const uint8_t type_hdr,
+                                      const in6_addr_t *r_saddr,
+                                      const in6_addr_t *r_daddr,
+                                      const hip_portpair_t *r_info,
+                                      const in6_addr_t *relay_to_addr,
+                                      const in_port_t relay_to_port)
 {
-       struct hip_common *r_to_be_relayed = NULL;
-       struct hip_tlv_common *current_param = NULL;
-       int err = 0;
-
-       HIP_DEBUG_IN6ADDR("hip_relay_forward_response:  source address", 
r_saddr);
-       HIP_DEBUG_IN6ADDR("hip_relay_forward_response:  destination address", 
r_daddr);
-       HIP_DEBUG_IN6ADDR("hip_relay_forward_response:  relay to address", 
relay_to_addr);
-       HIP_DEBUG("Relay_to port: %d.\n", relay_to_port);
-
-       HIP_IFEL(!(r_to_be_relayed = hip_msg_alloc()), -ENOMEM,
-                "No memory to copy original I1\n");    
-
-       hip_build_network_hdr(r_to_be_relayed, type_hdr, 0,
-                             &(r->hits), &(r->hitr));
-
-       while ((current_param = hip_get_next_param(r, current_param)) != NULL){
-               
-               HIP_DEBUG("Found parameter in R.\n");
-               HIP_DEBUG("Copying existing parameter to R packet "\
-                         "to be relayed.\n");
-               hip_build_param(r_to_be_relayed,current_param);
-       }
-
-       hip_zero_msg_checksum(r_to_be_relayed);
-
-       if(relay_to_port == 0) {
-               HIP_IFEL(hip_send_pkt(NULL, (struct in6_addr *) relay_to_addr, 
hip_get_local_nat_udp_port(),
-                                     relay_to_port, r_to_be_relayed, NULL, 0),
-                        -ECOMM, "forwarding response failed in raw\n");
-       } else {
-               HIP_IFEL(hip_send_pkt(NULL, (struct in6_addr *) relay_to_addr, 
hip_get_local_nat_udp_port(),
-                                     relay_to_port, r_to_be_relayed, NULL, 0),
-                        -ECOMM, "forwarding response failed in UDP\n");
-       }
-       
-       HIP_DEBUG_HIT("hip_relay_forward_response: Relayed  to", relay_to_addr);
-       
- out_err:
-       if(r_to_be_relayed != NULL) {
-               HIP_FREE(r_to_be_relayed);
-       }
-       return err;
+    struct hip_common *r_to_be_relayed   = NULL;
+    struct hip_tlv_common *current_param = NULL;
+    int err                              = 0;
+
+    HIP_DEBUG_IN6ADDR("hip_relay_forward_response:  source address", r_saddr);
+    HIP_DEBUG_IN6ADDR("hip_relay_forward_response:  destination address", 
r_daddr);
+    HIP_DEBUG_IN6ADDR("hip_relay_forward_response:  relay to address", 
relay_to_addr);
+    HIP_DEBUG("Relay_to port: %d.\n", relay_to_port);
+
+    HIP_IFEL(!(r_to_be_relayed = hip_msg_alloc()), -ENOMEM,
+             "No memory to copy original I1\n");
+
+    hip_build_network_hdr(r_to_be_relayed, type_hdr, 0,
+                          &(r->hits), &(r->hitr));
+
+    while ((current_param = hip_get_next_param(r, current_param)) != NULL) {
+        HIP_DEBUG("Found parameter in R.\n");
+        HIP_DEBUG("Copying existing parameter to R packet " \
+                  "to be relayed.\n");
+        hip_build_param(r_to_be_relayed, current_param);
+    }
+
+    hip_zero_msg_checksum(r_to_be_relayed);
+
+    if (relay_to_port == 0) {
+        HIP_IFEL(hip_send_pkt(NULL, (struct in6_addr *) relay_to_addr, 
hip_get_local_nat_udp_port(),
+                              relay_to_port, r_to_be_relayed, NULL, 0),
+                 -ECOMM, "forwarding response failed in raw\n");
+    } else {
+        HIP_IFEL(hip_send_pkt(NULL, (struct in6_addr *) relay_to_addr, 
hip_get_local_nat_udp_port(),
+                              relay_to_port, r_to_be_relayed, NULL, 0),
+                 -ECOMM, "forwarding response failed in UDP\n");
+    }
+
+    HIP_DEBUG_HIT("hip_relay_forward_response: Relayed  to", relay_to_addr);
+
+out_err:
+    if (r_to_be_relayed != NULL) {
+        HIP_FREE(r_to_be_relayed);
+    }
+    return err;
 }
 
 int hip_relay_add_rvs_to_ha(hip_common_t *source_msg, hip_ha_t *entry)
 {
-       struct hip_via_rvs *via_rvs = NULL; 
-       int err = 0;
- 
-       // Get rendezvous server's IP addresses
-       via_rvs = (struct hip_via_rvs *)
-               hip_get_param(source_msg, HIP_PARAM_VIA_RVS);
-       
-       if (!via_rvs)
-       {
-               _HIP_DEBUG("No VIA_RVS parameter.");
-               return -1;
-       }
-
-        if (!entry->rendezvous_addr)
-        {
-            HIP_IFEL(!(entry->rendezvous_addr = malloc(sizeof(struct 
in6_addr))),
-                               -1, "Malloc failed for in6_addr\n");
-        }
-
-       memcpy(entry->rendezvous_addr, &via_rvs->address, sizeof(struct 
in6_addr));
-       if (!entry->rendezvous_addr)
-       {
-               HIP_DEBUG("Couldn't get rendezvous IP address.");
-               return -1;
-       }
-       
-       HIP_DEBUG_IN6ADDR("The rvs address: ", entry->rendezvous_addr);
-       
+    struct hip_via_rvs *via_rvs = NULL;
+    int err                     = 0;
+
+    // Get rendezvous server's IP addresses
+    via_rvs = (struct hip_via_rvs *)
+              hip_get_param(source_msg, HIP_PARAM_VIA_RVS);
+
+    if (!via_rvs) {
+        _HIP_DEBUG("No VIA_RVS parameter.");
+        return -1;
+    }
+
+    if (!entry->rendezvous_addr) {
+        HIP_IFEL(!(entry->rendezvous_addr = malloc(sizeof(struct in6_addr))),
+                 -1, "Malloc failed for in6_addr\n");
+    }
+
+    memcpy(entry->rendezvous_addr, &via_rvs->address, sizeof(struct in6_addr));
+    if (!entry->rendezvous_addr) {
+        HIP_DEBUG("Couldn't get rendezvous IP address.");
+        return -1;
+    }
+
+    HIP_DEBUG_IN6ADDR("The rvs address: ", entry->rendezvous_addr);
+
 out_err:
-       return err;
+    return err;
 }
 
 /**
@@ -979,73 +990,73 @@
  *           from doc/HACKING. Stick to C-coding style using LINUX indendation.
  *           No lines over 80 characters. No capital letters in funcion names
  *           (C-style). Function commmends should be in header file.
- * 
+ *
  */
 int hip_relay_handle_from(hip_common_t *source_msg,
-                         in6_addr_t *rvs_ip,
-                         in6_addr_t *dest_ip, in_port_t *dest_port)
+                          in6_addr_t *rvs_ip,
+                          in6_addr_t *dest_ip, in_port_t *dest_port)
 {
-       hip_tlv_type_t param_type;
-       //   struct hip_relay_from *relay_from = NULL;
-       struct hip_from *from = NULL;
-       hip_ha_t *rvs_ha_entry = NULL;
-       
-       /* Check if the incoming I1 packet has a FROM parameters. */
-   
-       from = (struct hip_from *)
-               hip_get_param(source_msg, HIP_PARAM_FROM);
-       
-       /* Copy parameter data to target buffers. */
-       if(from == NULL) {
-               HIP_DEBUG("No FROM or RELAY_FROM parameters found in I1.\n");
-               return 0;
-       } else {
-               param_type = HIP_PARAM_FROM;
-               memcpy(dest_ip, &from->address, sizeof(from->address));
-       } 
-       
-
-       /* The relayed I1 packet has the initiator's HIT as source HIT, and the
-          responder HIT as destination HIT. We would like to verify the HMAC
-          against the host association that was created when the responder
-          registered to the rvs. That particular host association has the
-          responder's HIT as source HIT and the rvs' HIT as destination HIT.
-          Because we do not have the HIT of RVS in the incoming I1 message, we
-          have to get the host association using the responder's HIT and the IP
-          address of the RVS as search keys. */
+    hip_tlv_type_t param_type;
+    //   struct hip_relay_from *relay_from = NULL;
+    struct hip_from *from  = NULL;
+    hip_ha_t *rvs_ha_entry = NULL;
+
+    /* Check if the incoming I1 packet has a FROM parameters. */
+
+    from = (struct hip_from *)
+           hip_get_param(source_msg, HIP_PARAM_FROM);
+
+    /* Copy parameter data to target buffers. */
+    if (from == NULL) {
+        HIP_DEBUG("No FROM or RELAY_FROM parameters found in I1.\n");
+        return 0;
+    } else {
+        param_type = HIP_PARAM_FROM;
+        memcpy(dest_ip, &from->address, sizeof(from->address));
+    }
+
+
+    /* The relayed I1 packet has the initiator's HIT as source HIT, and the
+     * responder HIT as destination HIT. We would like to verify the HMAC
+     * against the host association that was created when the responder
+     * registered to the rvs. That particular host association has the
+     * responder's HIT as source HIT and the rvs' HIT as destination HIT.
+     * Because we do not have the HIT of RVS in the incoming I1 message, we
+     * have to get the host association using the responder's HIT and the IP
+     * address of the RVS as search keys. */
 #ifdef CONFIG_HIP_RVS
-       rvs_ha_entry =
-               hip_hadb_find_rvs_candidate_entry(&source_msg->hitr, rvs_ip);
-     
-       if (rvs_ha_entry == NULL) {
-               HIP_DEBUG_HIT("rvs hit not found in the entry table rvs_ip:",
-                             rvs_ip);
-               HIP_DEBUG_HIT("rvs hit not found in the entry table "\
-                             "&source_msg->hitr:", &source_msg->hitr);
-               HIP_DEBUG("The I1 packet was received from RVS, but the host "\
-                         "association created during registration is not 
found. "
-                         "RVS_HMAC cannot be verified.\n");
-               return -1;
-       }
-       
-       HIP_DEBUG("RVS host or relay host association found.\n");
-
-       /* Verify the RVS hmac. */
-       if(from != NULL && hip_verify_packet_rvs_hmac(
-                  source_msg, &rvs_ha_entry->hip_hmac_out) != 0) {
-               HIP_DEBUG("RVS_HMAC verification failed.\n");
-               HIP_DEBUG("Ignoring HMAC verification\n");
-               /* Notice that the HMAC is currently ignored to allow rvs/relay 
e.g.
-                  in the following use case: I <----IPv4 ----> RVS 
<----IPv6---> R 
-                  Otherwise we have to loop through all host associations and 
try
-                  all HMAC keys. See bug id 753 */
-               //return -1;
-       }
-     
-       HIP_DEBUG("RVS_HMAC verified.\n");
-#endif /* CONFIG_HIP_RVS */
-
-       return 1;
+    rvs_ha_entry =
+        hip_hadb_find_rvs_candidate_entry(&source_msg->hitr, rvs_ip);
+
+    if (rvs_ha_entry == NULL) {
+        HIP_DEBUG_HIT("rvs hit not found in the entry table rvs_ip:",
+                      rvs_ip);
+        HIP_DEBUG_HIT("rvs hit not found in the entry table " \
+                      "&source_msg->hitr:", &source_msg->hitr);
+        HIP_DEBUG("The I1 packet was received from RVS, but the host " \
+                  "association created during registration is not found. "
+                  "RVS_HMAC cannot be verified.\n");
+        return -1;
+    }
+
+    HIP_DEBUG("RVS host or relay host association found.\n");
+
+    /* Verify the RVS hmac. */
+    if (from != NULL && hip_verify_packet_rvs_hmac(
+            source_msg, &rvs_ha_entry->hip_hmac_out) != 0) {
+        HIP_DEBUG("RVS_HMAC verification failed.\n");
+        HIP_DEBUG("Ignoring HMAC verification\n");
+        /* Notice that the HMAC is currently ignored to allow rvs/relay e.g.
+         * in the following use case: I <----IPv4 ----> RVS <----IPv6---> R
+         * Otherwise we have to loop through all host associations and try
+         * all HMAC keys. See bug id 753 */
+        //return -1;
+    }
+
+    HIP_DEBUG("RVS_HMAC verified.\n");
+#endif  /* CONFIG_HIP_RVS */
+
+    return 1;
 }
 
 /**
@@ -1058,234 +1069,236 @@
  *           (C-style). Function commmends should be in header file.
  */
 int hip_relay_handle_relay_from(hip_common_t *source_msg,
-                               in6_addr_t *relay_ip,
-                               in6_addr_t *dest_ip, in_port_t *dest_port)
+                                in6_addr_t *relay_ip,
+                                in6_addr_t *dest_ip, in_port_t *dest_port)
 {
-       hip_tlv_type_t param_type;
-       struct hip_relay_from *relay_from = NULL;
-       hip_ha_t *relay_ha_entry = NULL;
-
-       /* Check if the incoming I1 packet has  RELAY_FROM parameters. */
-       relay_from = (struct hip_relay_from *)
-               hip_get_param(source_msg, HIP_PARAM_RELAY_FROM);
-               
-       /* Copy parameter data to target buffers. */
-       if(relay_from == NULL) {
-               HIP_DEBUG("No RELAY_FROM parameters found in I1.\n");
-               return 0;
-       } else  {
-               HIP_DEBUG("Found RELAY_FROM parameter in I.\n");
-               // set the relay ip and port to the destination address and 
port.
-               param_type = HIP_PARAM_RELAY_FROM;
-                 
-               memcpy(dest_ip, &relay_from->address, 
sizeof(relay_from->address));
-               *dest_port = ntohs(relay_from->port);
-               //      *dest_port = relay_from->port;
-               HIP_DEBUG("RELAY_FROM port in I. %d \n", *dest_port);
-       }
-     
-       /* The relayed I1 packet has the initiator's HIT as source HIT, and the
-          responder HIT as destination HIT. We would like to verify the HMAC
-          against the host association that was created when the responder
-          registered to the rvs. That particular host association has the
-          responder's HIT as source HIT and the rvs' HIT as destination HIT.
-          Because we do not have the HIT of Relay in the incoming I1 message, 
we
-          have to get the host association using the responder's HIT and the IP
-          address of the Relay as search keys.
-       
-          the fucntion hip_hadb_find_rvs_candidate_entry is designed for RVS 
case, but 
-          we reuse it in Relay also.
-       */
+    hip_tlv_type_t param_type;
+    struct hip_relay_from *relay_from = NULL;
+    hip_ha_t *relay_ha_entry          = NULL;
+
+    /* Check if the incoming I1 packet has  RELAY_FROM parameters. */
+    relay_from = (struct hip_relay_from *)
+                 hip_get_param(source_msg, HIP_PARAM_RELAY_FROM);
+
+    /* Copy parameter data to target buffers. */
+    if (relay_from == NULL) {
+        HIP_DEBUG("No RELAY_FROM parameters found in I1.\n");
+        return 0;
+    } else {
+        HIP_DEBUG("Found RELAY_FROM parameter in I.\n");
+        // set the relay ip and port to the destination address and port.
+        param_type = HIP_PARAM_RELAY_FROM;
+
+        memcpy(dest_ip, &relay_from->address, sizeof(relay_from->address));
+        *dest_port = ntohs(relay_from->port);
+        //     *dest_port = relay_from->port;
+        HIP_DEBUG("RELAY_FROM port in I. %d \n", *dest_port);
+    }
+
+    /* The relayed I1 packet has the initiator's HIT as source HIT, and the
+     * responder HIT as destination HIT. We would like to verify the HMAC
+     * against the host association that was created when the responder
+     * registered to the rvs. That particular host association has the
+     * responder's HIT as source HIT and the rvs' HIT as destination HIT.
+     * Because we do not have the HIT of Relay in the incoming I1 message, we
+     * have to get the host association using the responder's HIT and the IP
+     * address of the Relay as search keys.
+     *
+     * the fucntion hip_hadb_find_rvs_candidate_entry is designed for RVS 
case, but
+     * we reuse it in Relay also.
+     */
 #ifdef CONFIG_HIP_RVS
-       relay_ha_entry =
-               hip_hadb_find_rvs_candidate_entry(&source_msg->hitr, relay_ip);
-     
-       if (relay_ha_entry == NULL) {
-               HIP_DEBUG_HIT("relay hit not found in the entry table rvs_ip:",
-                             relay_ip);
-               HIP_DEBUG_HIT("relay hit not found in the entry table "\
-                             "&source_msg->hitr:", &source_msg->hitr);
-               HIP_DEBUG("The I1 packet was received from Relay, but the host 
"\
-                         "association created during registration is not 
found. "
-                         "RVS_HMAC cannot be verified.\n");
-               return -1;
-       }
-       
-       HIP_DEBUG("RVS host or relay host association found.\n");
-          
-       if(relay_from != NULL &&
-          hip_verify_packet_hmac_general(source_msg,
-                                         &relay_ha_entry->hip_hmac_out,
-                                         HIP_PARAM_RELAY_HMAC ) != 0) {
-               /* Notice that the HMAC is currently ignored to allow rvs/relay 
e.g.
-                  in the following use case: I <----IPv4 ----> RVS 
<----IPv6---> R 
-                  Otherwise we have to loop through all host associations and 
try
-                  all HMAC keys. See bug id 753 */
-               HIP_DEBUG("Full_Relay_HMAC verification failed.\n");
-               HIP_DEBUG("Ignoring HMAC verification\n");
-               //return -1;
-       }
-       
-       HIP_DEBUG("RVS_HMAC or Full_Relay verified.\n");
-#endif /* CONFIG_HIP_RVS */
-
-       return 1;
+    relay_ha_entry =
+        hip_hadb_find_rvs_candidate_entry(&source_msg->hitr, relay_ip);
+
+    if (relay_ha_entry == NULL) {
+        HIP_DEBUG_HIT("relay hit not found in the entry table rvs_ip:",
+                      relay_ip);
+        HIP_DEBUG_HIT("relay hit not found in the entry table " \
+                      "&source_msg->hitr:", &source_msg->hitr);
+        HIP_DEBUG("The I1 packet was received from Relay, but the host " \
+                  "association created during registration is not found. "
+                  "RVS_HMAC cannot be verified.\n");
+        return -1;
+    }
+
+    HIP_DEBUG("RVS host or relay host association found.\n");
+
+    if (relay_from != NULL &&
+        hip_verify_packet_hmac_general(source_msg,
+                                       &relay_ha_entry->hip_hmac_out,
+                                       HIP_PARAM_RELAY_HMAC ) != 0) {
+        /* Notice that the HMAC is currently ignored to allow rvs/relay e.g.
+         * in the following use case: I <----IPv4 ----> RVS <----IPv6---> R
+         * Otherwise we have to loop through all host associations and try
+         * all HMAC keys. See bug id 753 */
+        HIP_DEBUG("Full_Relay_HMAC verification failed.\n");
+        HIP_DEBUG("Ignoring HMAC verification\n");
+        //return -1;
+    }
+
+    HIP_DEBUG("RVS_HMAC or Full_Relay verified.\n");
+#endif  /* CONFIG_HIP_RVS */
+
+    return 1;
 }
 
-int hip_relay_handle_relay_to_in_client(struct hip_common * msg,
-                             int msg_type,                           
-                             struct in6_addr *src_addr,
-                             struct in6_addr *dst_addr,
-                             hip_portpair_t *msg_info,
-                             hip_ha_t *entry)
+int hip_relay_handle_relay_to_in_client(struct hip_common *msg,
+                                        int msg_type,
+                                        struct in6_addr *src_addr,
+                                        struct in6_addr *dst_addr,
+                                        hip_portpair_t *msg_info,
+                                        hip_ha_t *entry)
 {
-       int err = 0;
-       struct hip_relay_to *relay_to;
-       //check if full relay service is active
-       
-       if(!entry){
-               HIP_DEBUG("handle relay_to in client is failed\n");
-               goto out_err;
-       }
-       
-       
-       HIP_DEBUG("handle relay_to in client is on\n");
-       // check if the relay has been registered
-       
-       //check if there is a relay_to parameter            
-       relay_to = (struct hip_relay_to *) hip_get_param(msg, 
HIP_PARAM_RELAY_TO);
-       HIP_IFEL(!relay_to, 0, "No relay_to  found\n");
-       
-       // check msg type       
-       switch(msg_type) {
-       case HIP_R1:
-       case HIP_R2:
-               //disable the update and notify message. we need to think about 
them later
-  //   case HIP_UPDATE:
-  //   case HIP_NOTIFY:
-               HIP_DEBUG_IN6ADDR("the relay to address: ",
-                                 (struct in6_addr *) &relay_to->address);
-               HIP_DEBUG("the relay to ntohs(port): %d, local udp port %d\n",
-                         ntohs(relay_to->port),entry->local_udp_port);
-               
-               if(ipv6_addr_cmp((struct in6_addr *) &relay_to->address, 
&entry->our_addr)){
-                       HIP_DEBUG("relay_to address is saved as reflexive addr. 
\n");
-                       entry->local_reflexive_udp_port = ntohs(relay_to->port);
-                       memcpy(&entry->local_reflexive_address,
-                              &relay_to->address, sizeof(in6_addr_t));
-               }
-               //  state = HIP_STATE_NONE;
-               err = 1;
-               goto out_err;
-       }
-       
- out_err:
-       return err;    
+    int err = 0;
+    struct hip_relay_to *relay_to;
+    //check if full relay service is active
+
+    if (!entry) {
+        HIP_DEBUG("handle relay_to in client is failed\n");
+        goto out_err;
+    }
+
+
+    HIP_DEBUG("handle relay_to in client is on\n");
+    // check if the relay has been registered
+
+    //check if there is a relay_to parameter
+    relay_to = (struct hip_relay_to *) hip_get_param(msg, HIP_PARAM_RELAY_TO);
+    HIP_IFEL(!relay_to, 0, "No relay_to  found\n");
+
+    // check msg type
+    switch (msg_type) {
+    case HIP_R1:
+    case HIP_R2:
+        //disable the update and notify message. we need to think about them 
later
+        //     case HIP_UPDATE:
+        //     case HIP_NOTIFY:
+        HIP_DEBUG_IN6ADDR("the relay to address: ",
+                          (struct in6_addr *) &relay_to->address);
+        HIP_DEBUG("the relay to ntohs(port): %d, local udp port %d\n",
+                  ntohs(relay_to->port), entry->local_udp_port);
+
+        if (ipv6_addr_cmp((struct in6_addr *) &relay_to->address, 
&entry->our_addr)) {
+            HIP_DEBUG("relay_to address is saved as reflexive addr. \n");
+            entry->local_reflexive_udp_port = ntohs(relay_to->port);
+            memcpy(&entry->local_reflexive_address,
+                   &relay_to->address, sizeof(in6_addr_t));
+        }
+        //  state = HIP_STATE_NONE;
+        err = 1;
+        goto out_err;
+    }
+
+out_err:
+    return err;
 }
 
 static int hip_relht_init()
 {
-       /* Check that the relay hashtable is not already initialized. */
-       if(hiprelay_ht != NULL) {
-               return -1;
-       }
-       
-       hiprelay_ht = hip_ht_init(LHASH_HASH_FN(hip_relht),
-                                 LHASH_COMP_FN(hip_relht));
-       
-       if(hiprelay_ht == NULL) {
-               return -1;
-       }
-       
-       return 0;
+    /* Check that the relay hashtable is not already initialized. */
+    if (hiprelay_ht != NULL) {
+        return -1;
+    }
+
+    hiprelay_ht = hip_ht_init(LHASH_HASH_FN(hip_relht),
+                              LHASH_COMP_FN(hip_relht));
+
+    if (hiprelay_ht == NULL) {
+        return -1;
+    }
+
+    return 0;
 }
 
 static void hip_relht_uninit()
 {
-       if(hiprelay_ht == NULL)
-               return;
+    if (hiprelay_ht == NULL) {
+        return;
+    }
 
-       hip_ht_doall(hiprelay_ht, 
(LHASH_DOALL_FN_TYPE)LHASH_DOALL_FN(hip_relht_rec_free));
-       hip_ht_uninit(hiprelay_ht);
-       hiprelay_ht = NULL;
+    hip_ht_doall(hiprelay_ht, (LHASH_DOALL_FN_TYPE) 
LHASH_DOALL_FN(hip_relht_rec_free));
+    hip_ht_uninit(hiprelay_ht);
+    hiprelay_ht = NULL;
 }
 
 int hip_relay_init()
 {
-       int err = 0;
-
-       HIP_IFEL(hip_relht_init(), -1,
-                "Unable to initialize HIP relay / RVS database.\n");
-       HIP_IFEL(hip_relwl_init(), -1,
-                "Unable to initialize HIP relay / RVS whitelist.\n");
-       
-       if(hip_relay_read_config() == -ENOENT) {
-               HIP_ERROR("The configuration file \"%s\" could not be read.\n"\
-                         "Trying to write a new configuration file from "\
-                         "scratch.\n", HIP_RELAY_CONFIG_FILE);
-               if(hip_relay_write_config() == -ENOENT) {
-                       HIP_ERROR("Could not create a configuration file "\
-                                 "\"%s\".\n", HIP_RELAY_CONFIG_FILE);
-               } else {
-                       HIP_DEBUG("Created a new configuration file \"%s\".\n",
-                                HIP_RELAY_CONFIG_FILE);
-               }
-       } else {
-               HIP_DEBUG("Read configuration file \"%s\" successfully.\n",
-                        HIP_RELAY_CONFIG_FILE);
-       }
-       
- out_err:
-       if(hiprelay_wl == NULL) {
-               hip_relht_uninit();
-       }
-       
-       return err;
+    int err = 0;
+
+    HIP_IFEL(hip_relht_init(), -1,
+             "Unable to initialize HIP relay / RVS database.\n");
+    HIP_IFEL(hip_relwl_init(), -1,
+             "Unable to initialize HIP relay / RVS whitelist.\n");
+
+    if (hip_relay_read_config() == -ENOENT) {
+        HIP_ERROR("The configuration file \"%s\" could not be read.\n" \
+                  "Trying to write a new configuration file from " \
+                  "scratch.\n", HIP_RELAY_CONFIG_FILE);
+        if (hip_relay_write_config() == -ENOENT) {
+            HIP_ERROR("Could not create a configuration file " \
+                      "\"%s\".\n", HIP_RELAY_CONFIG_FILE);
+        } else {
+            HIP_DEBUG("Created a new configuration file \"%s\".\n",
+                      HIP_RELAY_CONFIG_FILE);
+        }
+    } else {
+        HIP_DEBUG("Read configuration file \"%s\" successfully.\n",
+                  HIP_RELAY_CONFIG_FILE);
+    }
+
+out_err:
+    if (hiprelay_wl == NULL) {
+        hip_relht_uninit();
+    }
+
+    return err;
 }
 
 void hip_relay_uninit()
 {
-       hip_relht_uninit();
-       hip_relwl_uninit();
+    hip_relht_uninit();
+    hip_relwl_uninit();
 }
 
 int hip_relay_reinit()
 {
-       int err = 0;
-
-       hip_relwl_uninit();
-       HIP_IFEL(hip_relwl_init(), -1, "Could not initialize the HIP relay / ",
-                "RVS whitelist.\n");
-       HIP_IFEL(hip_relay_read_config(), -1, "Could not read the ",
-                "configuration file \"%s\"\n", HIP_RELAY_CONFIG_FILE); 
-       
- out_err:       
-       return err;
+    int err = 0;
+
+    hip_relwl_uninit();
+    HIP_IFEL(hip_relwl_init(), -1, "Could not initialize the HIP relay / ",
+             "RVS whitelist.\n");
+    HIP_IFEL(hip_relay_read_config(), -1, "Could not read the ",
+             "configuration file \"%s\"\n", HIP_RELAY_CONFIG_FILE);
+
+out_err:
+    return err;
 }
 
 static int hip_relwl_init()
 {
-       /* Check that the relay whitelist is not already initialized. */
-       if(hiprelay_wl != NULL) {
-               return -1;
-       }
-
-       hiprelay_wl = hip_ht_init(LHASH_HASH_FN(hip_relwl),
-                                 LHASH_COMP_FN(hip_relwl)); 
-       
-       if(hiprelay_wl == NULL) {
-               return -1;
-       }
-       
-       return 0;
+    /* Check that the relay whitelist is not already initialized. */
+    if (hiprelay_wl != NULL) {
+        return -1;
+    }
+
+    hiprelay_wl = hip_ht_init(LHASH_HASH_FN(hip_relwl),
+                              LHASH_COMP_FN(hip_relwl));
+
+    if (hiprelay_wl == NULL) {
+        return -1;
+    }
+
+    return 0;
 }
 
 static void hip_relwl_uninit()
 {
-       if(hiprelay_wl == NULL)
-               return;
+    if (hiprelay_wl == NULL) {
+        return;
+    }
 
-       hip_ht_doall(hiprelay_wl, 
(LHASH_DOALL_FN_TYPE)LHASH_DOALL_FN(hip_relwl_hit_free));
-       hip_ht_uninit(hiprelay_wl);
-       hiprelay_wl = NULL;
+    hip_ht_doall(hiprelay_wl, (LHASH_DOALL_FN_TYPE) 
LHASH_DOALL_FN(hip_relwl_hit_free));
+    hip_ht_uninit(hiprelay_wl);
+    hiprelay_wl = NULL;
 }

=== modified file 'hipd/hiprelay.c.doxyme'
--- hipd/hiprelay.c.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/hiprelay.c.doxyme      2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,17 +45,17 @@
 
 
 /**
- * hip_relay_add_rvs_to_ha 
+ * hip_relay_add_rvs_to_ha
  *
  *
  * @param source_msg
  * @param entry
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relay_forward 
+ * hip_relay_forward
  *
  *
  * @param msg
@@ -64,12 +65,12 @@
  * @param info
  * @param type_hdr
  * @param relay_type
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relay_forward_response 
+ * hip_relay_forward_response
  *
  *
  * @param r
@@ -79,118 +80,118 @@
  * @param r_info
  * @param relay_to_addr
  * @param relay_to_port
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relay_handle_from 
+ * hip_relay_handle_from
  *
  *
  * @param source_msg
  * @param rvs_ip
  * @param dest_ip
  * @param dest_port
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relay_handle_relay_from 
+ * hip_relay_handle_relay_from
  *
  *
  * @param source_msg
  * @param relay_ip
  * @param dest_ip
  * @param dest_port
- * @return 
- **/
-
-
-/**
- * hip_relay_handle_relay_to 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_relay_handle_relay_to_in_client 
- *
- *
- * @param autogen.sh
- * @return 
- **/
-
-
-/**
- * hip_relay_set_status 
+ * @return
+ **/
+
+
+/**
+ * hip_relay_handle_relay_to
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_relay_handle_relay_to_in_client
+ *
+ *
+ * @param autogen.sh
+ * @return
+ **/
+
+
+/**
+ * hip_relay_set_status
  *
  *
  * @param status
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relht_cmp 
+ * hip_relht_cmp
  *
  *
  * @param rec1
  * @param rec2
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relht_free_all_of_type 
+ * hip_relht_free_all_of_type
  *
  *
  * @param type
- * @return 
- **/
-
-
-/**
- * hip_relht_get 
- *
- *
- * @param rec
- * @return 
- **/
-
-
-/**
- * hip_relht_hash 
- *
- *
- * @param rec
- * @return 
- **/
-
-
-/**
- * hip_relht_put 
- *
- *
- * @param rec
- * @return 
- **/
-
-
-/**
- * hip_relht_rec_free_doall 
- *
- *
- * @param rec
- * @return 
- **/
-
-
-/**
- * hip_relrec_alloc 
+ * @return
+ **/
+
+
+/**
+ * hip_relht_get
+ *
+ *
+ * @param rec
+ * @return
+ **/
+
+
+/**
+ * hip_relht_hash
+ *
+ *
+ * @param rec
+ * @return
+ **/
+
+
+/**
+ * hip_relht_put
+ *
+ *
+ * @param rec
+ * @return
+ **/
+
+
+/**
+ * hip_relht_rec_free_doall
+ *
+ *
+ * @param rec
+ * @return
+ **/
+
+
+/**
+ * hip_relrec_alloc
  *
  *
  * @param type
@@ -200,101 +201,100 @@
  * @param port
  * @param hmac
  * @param func
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relrec_info 
+ * hip_relrec_info
  *
  *
  * @param rec
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relrec_set_lifetime 
+ * hip_relrec_set_lifetime
  *
  *
  * @param rec
  * @param lifetime
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relrec_set_mode 
+ * hip_relrec_set_mode
  *
  *
  * @param rec
  * @param type
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relrec_set_udpport 
+ * hip_relrec_set_udpport
  *
  *
  * @param rec
  * @param port
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_relwl_cmp 
+ * hip_relwl_cmp
  *
  *
  * @param hit1
  * @param hit2
- * @return 
- **/
-
-
-/**
- * hip_relwl_get 
- *
- *
- * @param hit
- * @return 
- **/
-
-
-/**
- * hip_relwl_hash 
- *
- *
- * @param hit
- * @return 
- **/
-
-
-/**
- * hip_relwl_hit_free_doall 
- *
- *
- * @param hit
- * @return 
- **/
-
-
-/**
- * hip_relwl_put 
- *
- *
- * @param hit
- * @return 
- **/
-
-
-/**
- * hip_rvs_validate_lifetime 
+ * @return
+ **/
+
+
+/**
+ * hip_relwl_get
+ *
+ *
+ * @param hit
+ * @return
+ **/
+
+
+/**
+ * hip_relwl_hash
+ *
+ *
+ * @param hit
+ * @return
+ **/
+
+
+/**
+ * hip_relwl_hit_free_doall
+ *
+ *
+ * @param hit
+ * @return
+ **/
+
+
+/**
+ * hip_relwl_put
+ *
+ *
+ * @param hit
+ * @return
+ **/
+
+
+/**
+ * hip_rvs_validate_lifetime
  *
  *
  * @param requested_lifetime
  * @param granted_lifetime
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/hiprelay.h'
--- hipd/hiprelay.h     2010-01-19 09:28:42 +0000
+++ hipd/hiprelay.h     2010-02-10 22:32:46 +0000
@@ -56,7 +56,7 @@
  * </pre>
  * </li>
  * </ul>
- * 
+ *
  * @author  Lauri Silvennoinen
  * @version 1.1
  * @date    31.03.2008
@@ -103,42 +103,42 @@
 #define HIP_RELREC_MAX_LIFETIME 159 // Equals 3756 seconds.
 
 /** HIP Relay record. These records are stored in the HIP Relay hashtable. */
-typedef struct{
-       /** The type of this relay record (full relay or rvs) */
-       uint8_t type;
-       /** The lifetime of this record, seconds. */
-        time_t lifetime;
-       /** Time when this record was created, seconds since epoch. */
-       time_t created;
-       /** Time when this record was last used, seconds since epoch. */
-       time_t last_contact;
-       /** HIT of Responder (Relay Client) */
-       hip_hit_t hit_r;
-       /** IP address of Responder (Relay Client) */
-       in6_addr_t ip_r;
-       /** Client UDP port received in I2 packet of registration. */
-       in_port_t udp_port_r;
-       /** Integrity key established while registration occurred. */
-       hip_crypto_key_t hmac_relay;
-       /** Function pointer to send function (raw or udp). */
-       hip_xmit_func_t send_fn;
-}hip_relrec_t;
+typedef struct {
+    /** The type of this relay record (full relay or rvs) */
+    uint8_t          type;
+    /** The lifetime of this record, seconds. */
+    time_t           lifetime;
+    /** Time when this record was created, seconds since epoch. */
+    time_t           created;
+    /** Time when this record was last used, seconds since epoch. */
+    time_t           last_contact;
+    /** HIT of Responder (Relay Client) */
+    hip_hit_t        hit_r;
+    /** IP address of Responder (Relay Client) */
+    in6_addr_t       ip_r;
+    /** Client UDP port received in I2 packet of registration. */
+    in_port_t        udp_port_r;
+    /** Integrity key established while registration occurred. */
+    hip_crypto_key_t hmac_relay;
+    /** Function pointer to send function (raw or udp). */
+    hip_xmit_func_t  send_fn;
+} hip_relrec_t;
 
-/** 
+/**
  * Relay record encapsulation modes used in a relay record. This mode is 
between
  * the Relay and the Responder.
  */
-typedef enum{HIP_RELAY = HIP_SERVICE_RELAY,
-                    HIP_FULLRELAY = HIP_SERVICE_FULLRELAY,
-                    HIP_RVSRELAY = HIP_SERVICE_RENDEZVOUS}hip_relrec_type_t;
+typedef enum { HIP_RELAY     = HIP_SERVICE_RELAY,
+               HIP_FULLRELAY = HIP_SERVICE_FULLRELAY,
+               HIP_RVSRELAY  = HIP_SERVICE_RENDEZVOUS } hip_relrec_type_t;
 /** Possible states of the RVS / relay. */
-typedef enum{HIP_RELAY_OFF = 0, HIP_RELAY_ON = 1, HIP_RELAY_FULL = 
2}hip_relay_status_t;
+typedef enum { HIP_RELAY_OFF = 0, HIP_RELAY_ON = 1, HIP_RELAY_FULL = 2 } 
hip_relay_status_t;
 /** Possible states of the whitelist. */
-typedef enum{HIP_RELAY_WL_OFF = 0, HIP_RELAY_WL_ON = 1}hip_relay_wl_status_t;
+typedef enum { HIP_RELAY_WL_OFF = 0, HIP_RELAY_WL_ON = 1 } 
hip_relay_wl_status_t;
 
-/** 
+/**
  * Returns relay status.
- * 
+ *
  * @return HIP_RELAY_ON if the RVS / relay is "on", HIP_RELAY_OFF otherwise.
  */
 hip_relay_status_t hip_relay_get_status(void);
@@ -148,7 +148,7 @@
  *
  * @param status zero if the relay is to be disabled, anything else to enable
  *               the relay.
- */ 
+ */
 void hip_relay_set_status(hip_relay_status_t status);
 
 /**
@@ -159,45 +159,45 @@
  */
 static inline unsigned long hip_hash_func(const hip_hit_t *hit)
 {
-       uint32_t bits_1st = 0;
-       unsigned long hash = 0;
-
-       /* HITs are of the form: 2001:001x:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
-          We have four groups of 32 bit sequences here, but the first 28 bits
-          are constant and have no hash value. Therefore, we create a new
-          replacement sequence for first 32 bit sequence. */
-          
-       bits_1st = (~hit->s6_addr[3]) << 28;
-       bits_1st |= hit->s6_addr[3] << 24;
-       bits_1st |= hit->s6_addr[7] << 16;
-       bits_1st |= hit->s6_addr[11] << 8;
-       bits_1st |= hit->s6_addr[15];
-               
-       /* We calculate the hash by avalanching the bits. The avalanching
-          ensures that we make use of all bits when dealing with 64 bits
-          architectures. */
-       hash =  (bits_1st ^ hit->s6_addr32[1]);
-       hash ^= hash << 3;
-       hash ^= (hit->s6_addr32[2] ^ hit->s6_addr32[3]);
-       hash += hash >> 5;
-       hash ^= hash << 4;
-       hash += hash >> 17;
-       hash ^= hash << 25;
-       hash += hash >> 6;
-       
-       return hash;
+    uint32_t bits_1st  = 0;
+    unsigned long hash = 0;
+
+    /* HITs are of the form: 2001:001x:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
+     * We have four groups of 32 bit sequences here, but the first 28 bits
+     * are constant and have no hash value. Therefore, we create a new
+     * replacement sequence for first 32 bit sequence. */
+
+    bits_1st  = (~hit->s6_addr[3]) << 28;
+    bits_1st |= hit->s6_addr[3] << 24;
+    bits_1st |= hit->s6_addr[7] << 16;
+    bits_1st |= hit->s6_addr[11] << 8;
+    bits_1st |= hit->s6_addr[15];
+
+    /* We calculate the hash by avalanching the bits. The avalanching
+     * ensures that we make use of all bits when dealing with 64 bits
+     * architectures. */
+    hash      =  (bits_1st ^ hit->s6_addr32[1]);
+    hash     ^= hash << 3;
+    hash     ^= (hit->s6_addr32[2] ^ hit->s6_addr32[3]);
+    hash     += hash >> 5;
+    hash     ^= hash << 4;
+    hash     += hash >> 17;
+    hash     ^= hash << 25;
+    hash     += hash >> 6;
+
+    return hash;
 }
 
 /**
  * Initializes the HIP relay / RVS. Initializes the HIP relay hashtable and
  * whitelist.
- */ 
+ */
 int hip_relay_init(void);
 
 /**
  * Uninitializes the HIP relay / RVS. Uninitializes the HIP relay hashtable and
  * whitelist.
- */ 
+ */
 void hip_relay_uninit(void);
 
 /**
@@ -217,8 +217,8 @@
 /**
  * Sets the mode of a relay record. This function sets the @c flags field of a
  * relay record.
- * 
- * @param rec  a pointer to a relay record. 
+ *
+ * @param rec  a pointer to a relay record.
  * @param mode the mode to be set for the parameter record. One of the 
following:
  *             <ul>
  *             <li>HIP_REL_NONE</li>
@@ -232,23 +232,23 @@
 /**
  * Sets the lifetime of a relay record.
  * The service lifetime is set to 2^((lifetime - 64)/8) seconds.
- * 
- * @param rec      a pointer to a relay record. 
- * @param lifetime the lifetime of the above formula. 
+ *
+ * @param rec      a pointer to a relay record.
+ * @param lifetime the lifetime of the above formula.
  */
 void hip_relrec_set_lifetime(hip_relrec_t *rec, const uint8_t lifetime);
 
 /**
- * Sets the UDP port number of a relay record. 
- * 
- * @param rec  a pointer to a relay record. 
- * @param port UDP port number. 
+ * Sets the UDP port number of a relay record.
+ *
+ * @param rec  a pointer to a relay record.
+ * @param port UDP port number.
  */
 void hip_relrec_set_udpport(hip_relrec_t *rec, const in_port_t port);
 
 /**
  * Prints info of the parameter relay record using @c HIP_INFO() macro.
- * 
+ *
  * @param rec a pointer to a relay record.
  */
 void hip_relrec_info(const hip_relrec_t *rec);
@@ -256,7 +256,7 @@
 /**
  * The compare function of the @c hiprelay_ht hashtable. Compares the hash
  * values calculated from parameters @c rec1 and @c rec2.
- * 
+ *
  * @param rec1 a pointer to a HIT.
  * @param rec2 a pointer to a HIT.
  * @return     0 if keys are equal and neither is NULL, non-zero otherwise.
@@ -305,7 +305,7 @@
  * located in a different memory location thatn the hashtable entry), then
  * @c rec is left untouched.
  *
- * @param rec a pointer to a relay record. 
+ * @param rec a pointer to a relay record.
  */
 void hip_relht_rec_free_doall(hip_relrec_t *rec);
 
@@ -320,13 +320,13 @@
  * different memory location thatn the hashtable entry), then @c rec is left
  * untouched.
  *
- * @param rec a pointer to a relay record. 
- */ 
+ * @param rec a pointer to a relay record.
+ */
 void hip_relht_rec_free_type_doall(hip_relrec_t *rec, const hip_relrec_type_t 
*type);
 
 /**
  * Returns the number of relay records in the hashtable @c hiprelay_ht.
- * 
+ *
  * @return  number of relay records in the hashtable.
  */
 unsigned long hip_relht_size(void);
@@ -343,7 +343,7 @@
 
 /**
  * Allocates a new relay record.
- * 
+ *
  * @param type     the type of this relay record (HIP_FULLRELAY or
  *                 HIP_RVSRELAY).
  * @param lifetime the lifetime of this relayrecord as defined in registration
@@ -357,11 +357,11 @@
  *                 this function.
  */
 hip_relrec_t *hip_relrec_alloc(const hip_relrec_type_t type,
-                              const uint8_t lifetime,
-                              const in6_addr_t *hit_r, const hip_hit_t *ip_r,
-                              const in_port_t port,
-                              const hip_crypto_key_t *hmac,
-                              const hip_xmit_func_t func);
+                               const uint8_t lifetime,
+                               const in6_addr_t *hit_r, const hip_hit_t *ip_r,
+                               const in_port_t port,
+                               const hip_crypto_key_t *hmac,
+                               const hip_xmit_func_t func);
 
 /**
  * Deletes all entries of @c type from the relay record hashtable and frees the
@@ -374,7 +374,7 @@
 /**
  * The compare function of the @c hiprelay_wl hashtable. Compares the hash
  * values calculated from parameter @c hit1 and @c hit2.
- * 
+ *
  * @param hit1 a pointer to a HIT.
  * @param hit2 a pointer to a HIT.
  * @return     0 if keys are equal and neither is NULL, non-zero otherwise.
@@ -394,7 +394,7 @@
  *
  * @return HIP_RELAY_ON if the RVS / relay whitelist is "on", HIP_RELAY_OFF
  *         otherwise.
- */ 
+ */
 hip_relay_wl_status_t hip_relwl_get_status(void);
 
 /**
@@ -411,14 +411,14 @@
  *                            i.e. is smaller than @c hiprelay_min_lifetime or
  *                            is greater than @c hiprelay_max_lifetime. Zero
  *                            otherwise.
- */ 
+ */
 int hip_rvs_validate_lifetime(uint8_t requested_lifetime,
-                             uint8_t *granted_lifetime);
+                              uint8_t *granted_lifetime);
 
 int hip_relay_forward(const hip_common_t *msg, const in6_addr_t *saddr,
-                                         const in6_addr_t *daddr, hip_relrec_t 
*rec,
-                                         const hip_portpair_t *info, const 
uint8_t type_hdr,
-                                         const hip_relrec_type_t relay_type);
+                      const in6_addr_t *daddr, hip_relrec_t *rec,
+                      const hip_portpair_t *info, const uint8_t type_hdr,
+                      const hip_relrec_type_t relay_type);
 
 /**
  * Validates a requested HIP relay service lifetime. If
@@ -436,13 +436,13 @@
  *                            otherwise.
  * @note                      Currently this is just a call back wrapper for
  *                            hip_rvs_validate_lifetime() because RVS and relay
- *                            services share the same lifetimes. 
+ *                            services share the same lifetimes.
  */
 static inline int hip_relay_validate_lifetime(uint8_t requested_lifetime,
-                                             uint8_t *granted_lifetime)
+                                              uint8_t *granted_lifetime)
 {
-       return hip_rvs_validate_lifetime(requested_lifetime,
-                                        granted_lifetime);
+    return hip_rvs_validate_lifetime(requested_lifetime,
+                                     granted_lifetime);
 }
 
 /**
@@ -460,7 +460,7 @@
  * sends @c FROM:I, @c FROM:RVS1, ... , <code>FROM:RVS(n-1)</code>. If 
initiator
  * is located behind a NAT, the first @c FROM parameter is replaced with a
  * @c RELAY_FROM parameter.
- * 
+ *
  * @param i1       a pointer to the I1 HIP packet common header with source and
  *                 destination HITs.
  * @param i1_saddr a pointer to the source address from where the I1 packet was
@@ -476,9 +476,9 @@
  *                 implementation
  */
 int hip_relay_rvs(const hip_common_t *i1,
-                 const in6_addr_t *i1_saddr,
-                 const in6_addr_t *i1_daddr, hip_relrec_t *rec,
-                 const hip_portpair_t *i1_info);
+                  const in6_addr_t *i1_saddr,
+                  const in6_addr_t *i1_daddr, hip_relrec_t *rec,
+                  const hip_portpair_t *i1_info);
 
 int hip_relay_add_rvs_to_ha(hip_common_t *source_msg, hip_ha_t *entry);
 
@@ -490,7 +490,7 @@
  * parameter. If a parameter is found, the values are copied to target buffers
  * @c dest_ip and @c dest_port. Next the hmac in RVS_HMAC is verified using
  * the host association created during registration. This host association
- * is searched using hitr from @c source_msg and @c rvs_ip as search keys. 
+ * is searched using hitr from @c source_msg and @c rvs_ip as search keys.
  *
  * @param  source_msg a pointer to the I1 HIP packet common header with source
  *                    and destination HITs.
@@ -500,26 +500,26 @@
  *                    parameter.
  * @param dest_port   a target buffer for the port number in RELAY_FROM
  *                    parameter.
- * @return            zero 
- */ 
+ * @return            zero
+ */
 
 int hip_relay_handle_from(hip_common_t *source_msg,
-                         in6_addr_t *rvs_ip,
-                         in6_addr_t *dest_ip, in_port_t *dest_port);
+                          in6_addr_t *rvs_ip,
+                          in6_addr_t *dest_ip, in_port_t *dest_port);
 
 int hip_relay_handle_relay_from(hip_common_t *source_msg,
                                 in6_addr_t *relay_ip,
                                 in6_addr_t *dest_ip, in_port_t *dest_port);
 
-int hip_relay_handle_relay_to_in_client(struct hip_common * msg,
-                                                                               
int msg_type,                           
-                                                                               
struct in6_addr *src_addr,
-                                                                               
struct in6_addr *dst_addr,
-                                                                               
hip_portpair_t *msg_info,
-                                                                               
hip_ha_t *entry);
-
-
-int hip_relay_handle_relay_to(struct hip_common * msg,
+int hip_relay_handle_relay_to_in_client(struct hip_common *msg,
+                                        int msg_type,
+                                        struct in6_addr *src_addr,
+                                        struct in6_addr *dst_addr,
+                                        hip_portpair_t *msg_info,
+                                        hip_ha_t *entry);
+
+
+int hip_relay_handle_relay_to(struct hip_common *msg,
                               int msg_type,
                               struct in6_addr *src_addr,
                               struct in6_addr *dst_addr,
@@ -530,9 +530,9 @@
  *
  */
 int hip_relay_forward_I(const hip_common_t *i1,
-                       const in6_addr_t *i1_saddr,
-                       const in6_addr_t *i1_daddr, hip_relrec_t *rec,
-                       const hip_portpair_t *i1_info,
-                       const uint8_t);
+                        const in6_addr_t *i1_saddr,
+                        const in6_addr_t *i1_daddr, hip_relrec_t *rec,
+                        const hip_portpair_t *i1_info,
+                        const uint8_t);
 
 #endif /* HIP_HIPRELAY_H */

=== modified file 'hipd/hiprelay.h.doxyme'
--- hipd/hiprelay.h.doxyme      2009-12-12 10:44:54 +0000
+++ hipd/hiprelay.h.doxyme      2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hiprelay.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/hit_to_ip.c'
--- hipd/hit_to_ip.c    2010-01-19 11:02:34 +0000
+++ hipd/hit_to_ip.c    2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hit_to_ip.c
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * @brief look for locators in hit-to-ip domain
  * @brief usually invoked by hip_map_id_to_addr
  *
@@ -10,7 +10,7 @@
  *
  * @author Oleg Ponomarev <oleg.ponomarev@xxxxxxx>
  **/
- 
+
 #include "maintenance.h"
 #include "lib/conf/hipconf.h"
 #include <netinet/in.h>
@@ -35,26 +35,28 @@
 int hip_hit_to_ip_status = 0;
 
 /**
- * hip_set_hit_to_ip_status 
+ * hip_set_hit_to_ip_status
  *
  * This function is an interface to turn on/off locators lookup in hit-to-ip 
domain
  *
  * @param status 0 unless locator lookups in hit-to-ip domain wanted, 1 
otherwise
  **/
-void hip_set_hit_to_ip_status(const int status) {
-  hip_hit_to_ip_status = status;
+void hip_set_hit_to_ip_status(const int status)
+{
+    hip_hit_to_ip_status = status;
 }
 
 /**
- * hip_get_hit_to_ip_status 
+ * hip_get_hit_to_ip_status
  *
  * This function is an interface to check if locators lookup in hit-to-ip 
domain if wanted
  *
  * @return 0 unless locator lookups in hit-to-ip domain wanted, 1 otherwise
  **/
 
-int hip_get_hit_to_ip_status(void) {
-  return hip_hit_to_ip_status;
+int hip_get_hit_to_ip_status(void)
+{
+    return hip_hit_to_ip_status;
 }
 
 // append unless set in configuration
@@ -63,121 +65,131 @@
 char *hip_hit_to_ip_zone = NULL;
 
 /**
- * hip_hit_to_ip_set 
+ * hip_hit_to_ip_set
  *
  * Set the zone for hit-to-ip domain lookups
  *
  * @param zone domain as a string, e.g. "hit-to-ip.infrahip.net"
  **/
-void hip_hit_to_ip_set(const char *zone) {
-  char *tmp = hip_hit_to_ip_zone;
-  
-  hip_hit_to_ip_zone = strdup(zone);
-
-  if (tmp!=NULL)
-       free(tmp);
+void hip_hit_to_ip_set(const char *zone)
+{
+    char *tmp = hip_hit_to_ip_zone;
+
+    hip_hit_to_ip_zone = strdup(zone);
+
+    if (tmp != NULL) {
+        free(tmp);
+    }
 }
 
 static const char hex_digits[] = {
-        '0', '1', '2', '3', '4', '5', '6', '7',
-        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+    '0', '1', '2', '3', '4', '5', '6', '7',
+    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
 };
 
 /**
- * hip_get_hit_to_ip_hostname 
+ * hip_get_hit_to_ip_hostname
  *
  * returns 
"5.7.d.1.c.c.8.d.0.6.3.b.a.4.6.2.5.0.5.2.e.4.7.5.e.1.0.0.1.0.0.2.hit-to-ip.infrahip.net"
 for 2001:1e:574e:2505:264a:b360:d8cc:1d75
  *
  * @param hit          HIT as a string
  * @param hostname[out]        buffer for the result
  * @param hostname_len length of the buffer
- * @return             0
+ * @return      0
  **/
-static int hip_get_hit_to_ip_hostname(const hip_hit_t *hit, const char 
*hostname, const int hostname_len) {
-       if ((hit == NULL)||(hostname == NULL))
-               return ERR;
-
-        uint8_t *bytes = (uint8_t *) hit->s6_addr;
-        char *cp = (char *) hostname;
-       int i; // no C99 :(
-        for (i = 15; i >= 0; i--) {
-               *cp++ = hex_digits[bytes[i] & 0x0f];
-                *cp++ = '.';
-                *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
-                *cp++ = '.';
-        }
-
-       if (hip_hit_to_ip_zone==NULL)
-         strncpy(cp, HIT_TO_IP_ZONE_DEFAULT, hostname_len-64);
-       else
-         strncpy(cp, hip_hit_to_ip_zone , hostname_len-64);
-
-       return OK;
+static int hip_get_hit_to_ip_hostname(const hip_hit_t *hit, const char 
*hostname, const int hostname_len)
+{
+    if ((hit == NULL) || (hostname == NULL)) {
+        return ERR;
+    }
+
+    uint8_t *bytes = (uint8_t *) hit->s6_addr;
+    char *cp       = (char *) hostname;
+    int i;     // no C99 :(
+    for (i = 15; i >= 0; i--) {
+        *cp++ = hex_digits[bytes[i] & 0x0f];
+        *cp++ = '.';
+        *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
+        *cp++ = '.';
+    }
+
+    if (hip_hit_to_ip_zone == NULL) {
+        strncpy(cp, HIT_TO_IP_ZONE_DEFAULT, hostname_len - 64);
+    } else {
+        strncpy(cp, hip_hit_to_ip_zone, hostname_len - 64);
+    }
+
+    return OK;
 }
 
 /**
- * hip_hit_to_ip 
+ * hip_hit_to_ip
  *
  * checks for ip address for hit preferring IPv4 one
  *
  * @param hit          HIT to look locators for
  * @param retval[out]  buffer for the result
- * @return             0 on success, -1 otherwise
+ * @return      0 on success, -1 otherwise
  **/
 
-int hip_hit_to_ip(hip_hit_t *hit, struct in6_addr *retval) {
-       struct addrinfo *rp = NULL; // no C99 :(
-       char hit_to_ip_hostname[64+HIT_TO_IP_ZONE_MAX_LEN+1];
-       int found_addr = 0;
-       struct addrinfo hints;
-       struct addrinfo *result = NULL;
-       int res;
-
-       if ((hit == NULL)||(retval == NULL))
-               return ERR;
-
-       if (hip_get_hit_to_ip_hostname(hit, hit_to_ip_hostname, 
sizeof(hit_to_ip_hostname))!=OK)
-               return ERR;
-
-       memset(&hints, 0, sizeof(hints));
-       hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
-       hints.ai_socktype = SOCK_DGRAM; /* Datagram socket. Right? */
-       hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
-       hints.ai_protocol = 0;          /* Any protocol */
-       hints.ai_canonname = NULL;
-       hints.ai_addr = NULL;
-       hints.ai_next = NULL;
-
-       /* getaddrinfo is too complex for DNS lookup, but let us use it now */
-       res = getaddrinfo( hit_to_ip_hostname, NULL, &hints, &result );
-       HIP_DEBUG("getaddrinfo(%s) returned %d\n", hit_to_ip_hostname, res);
-
-       if (res!=0) {
-               HIP_DEBUG("getaddrinfo error %s\n", gai_strerror(res));
-               return ERR;
-       }
-
-       /* Look at the list and return only one address, let us prefer AF_INET 
*/
-       for (rp = result; rp != NULL; rp = rp->ai_next) {
-               HIP_DEBUG_SOCKADDR("getaddrinfo result", rp->ai_addr);
-
-               if (rp->ai_family == AF_INET) {
-                       struct sockaddr_in *tmp_sockaddr_in_ptr = (struct 
sockaddr_in *)(void*) (rp->ai_addr);
-                       IPV4_TO_IPV6_MAP(&(tmp_sockaddr_in_ptr->sin_addr), 
retval)
-                       found_addr = 1;
-                       break;
-               } else if (rp->ai_family == AF_INET6) {
-                       struct sockaddr_in6 *tmp_sockaddr_in6_ptr = (struct 
sockaddr_in6 *)(void*) (rp->ai_addr);
-                       ipv6_addr_copy(retval, 
&(tmp_sockaddr_in6_ptr->sin6_addr));
-                       found_addr = 1;
-               }
-       }
-
-       if (result)
-               freeaddrinfo(result);
-
-       if (found_addr)
-               return OK;
-       else
-               return ERR;
+int hip_hit_to_ip(hip_hit_t *hit, struct in6_addr *retval)
+{
+    struct addrinfo *rp     = NULL; // no C99 :(
+    char hit_to_ip_hostname[64 + HIT_TO_IP_ZONE_MAX_LEN + 1];
+    int found_addr          = 0;
+    struct addrinfo hints;
+    struct addrinfo *result = NULL;
+    int res;
+
+    if ((hit == NULL) || (retval == NULL)) {
+        return ERR;
+    }
+
+    if (hip_get_hit_to_ip_hostname(hit, hit_to_ip_hostname, 
sizeof(hit_to_ip_hostname)) != OK) {
+        return ERR;
+    }
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family    = AF_UNSPEC;     /* Allow IPv4 or IPv6 */
+    hints.ai_socktype  = SOCK_DGRAM;    /* Datagram socket. Right? */
+    hints.ai_flags     = AI_PASSIVE;    /* For wildcard IP address */
+    hints.ai_protocol  = 0;             /* Any protocol */
+    hints.ai_canonname = NULL;
+    hints.ai_addr      = NULL;
+    hints.ai_next      = NULL;
+
+    /* getaddrinfo is too complex for DNS lookup, but let us use it now */
+    res                = getaddrinfo( hit_to_ip_hostname, NULL, &hints, 
&result );
+    HIP_DEBUG("getaddrinfo(%s) returned %d\n", hit_to_ip_hostname, res);
+
+    if (res != 0) {
+        HIP_DEBUG("getaddrinfo error %s\n", gai_strerror(res));
+        return ERR;
+    }
+
+    /* Look at the list and return only one address, let us prefer AF_INET */
+    for (rp = result; rp != NULL; rp = rp->ai_next) {
+        HIP_DEBUG_SOCKADDR("getaddrinfo result", rp->ai_addr);
+
+        if (rp->ai_family == AF_INET) {
+            struct sockaddr_in *tmp_sockaddr_in_ptr = (struct sockaddr_in *) 
(void *) (rp->ai_addr);
+            IPV4_TO_IPV6_MAP(&(tmp_sockaddr_in_ptr->sin_addr), retval)
+            found_addr = 1;
+            break;
+        } else if (rp->ai_family == AF_INET6) {
+            struct sockaddr_in6 *tmp_sockaddr_in6_ptr = (struct sockaddr_in6 
*) (void *) (rp->ai_addr);
+            ipv6_addr_copy(retval, &(tmp_sockaddr_in6_ptr->sin6_addr));
+            found_addr = 1;
+        }
+    }
+
+    if (result) {
+        freeaddrinfo(result);
+    }
+
+    if (found_addr) {
+        return OK;
+    } else {
+        return ERR;
+    }
 }

=== modified file 'hipd/hit_to_ip.h'
--- hipd/hit_to_ip.h    2009-12-12 11:33:34 +0000
+++ hipd/hit_to_ip.h    2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/hit_to_ip.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * @brief look for locators in hit-to-ip domain
  * @brief usually invoked by hip_map_id_to_addr
  *

=== modified file 'hipd/icookie.c'
--- hipd/icookie.c      2009-12-29 15:21:29 +0000
+++ hipd/icookie.c      2010-02-10 22:32:46 +0000
@@ -1,12 +1,12 @@
- /*
-  * HIPL extensions for indexing cookies with just peer HITs. Currently
-  * used only for spam related experimentation.
-  *
-  * See also cookie.c for overriding functions.
-  *
-  * Author: Miika Komu <miika@xxxxxx>
-  *
-  */
+/*
+ * HIPL extensions for indexing cookies with just peer HITs. Currently
+ * used only for spam related experimentation.
+ *
+ * See also cookie.c for overriding functions.
+ *
+ * Author: Miika Komu <miika@xxxxxx>
+ *
+ */
 
 #ifdef HAVE_CONFIG_H
   #include "config.h"
@@ -16,94 +16,101 @@
 #include "icookie.h"
 
 /* We need maintain a separate table for the K values of cookies because
-   otherwise they are just overwritten when R1s are recreated periodically. */
+ * otherwise they are just overwritten when R1s are recreated periodically. */
 int hip_puzzle_k[HIP_R1TABLESIZE];
 
-void hip_init_puzzle_defaults() {
-       int i;
-       for (i = 0; i < HIP_R1TABLESIZE; i++)
-               hip_puzzle_k[i] = HIP_DEFAULT_COOKIE_K;
+void hip_init_puzzle_defaults()
+{
+    int i;
+    for (i = 0; i < HIP_R1TABLESIZE; i++) {
+        hip_puzzle_k[i] = HIP_DEFAULT_COOKIE_K;
+    }
 }
 
 int hip_calc_cookie_idx(struct in6_addr *ip_i, struct in6_addr *ip_r,
-                              struct in6_addr *hit_i)
-{
-       register u32 base=0;
-       int i;
-
-       /* Indexing based on the initiator HIT only: however, this may happen
-          on the expense of DoS protection against zombies. */
-       for(i = 0; i < 4; i++) {
-               base ^= hit_i->s6_addr32[i];
-       }
-
-       for(i = 0; i < 3; i++) {
-               base ^= ((base >> (24 - i * 8)) & 0xFF);
-       }
-
-       /* base ready */
-
-       HIP_DEBUG("idx=%d\n", ((base) % HIP_R1TABLESIZE));
-
-       return (base) % HIP_R1TABLESIZE;
-}
-
-int hip_get_cookie_difficulty(hip_hit_t *hit_i) {
-       return hip_puzzle_k[hip_calc_cookie_idx(NULL, NULL, hit_i)];
-}
-
-static int hip_get_cookie_difficulty_by_index(int r1_index) {
-       return hip_puzzle_k[r1_index];
-}
-
-int hip_set_cookie_difficulty(hip_hit_t *hit_i, int k) {
-       if (k > HIP_PUZZLE_MAX_K || k < 1) {
-               HIP_ERROR("Bad cookie value (%d), min=%d, max=%d\n",
-                         k, 1, HIP_PUZZLE_MAX_K);
-               return -1;
-       }
-
-       hip_puzzle_k[hip_calc_cookie_idx(NULL, NULL, hit_i)] = k;
-
-       HIP_DEBUG("HIP cookie value set to %d\n", k);
-       return k;
-}
-
-int hip_inc_cookie_difficulty(hip_hit_t *hit_i) {
-       int k = hip_get_cookie_difficulty(hit_i) + 1;
-       return hip_set_cookie_difficulty(hit_i, k);
-}
-
-int hip_dec_cookie_difficulty(hip_hit_t *hit_i) {
-       int k = hip_get_cookie_difficulty(hit_i) - 1;
-       return hip_set_cookie_difficulty(hit_i, k);
-}
-
-int hip_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit, 
-                    int (*sign)(struct hip_host_id *p, struct hip_common *m),
-                    void *privkey, struct hip_host_id *pubkey)
-{
-       int i;
-
-       for(i = 0; i < HIP_R1TABLESIZE; i++) {
-               int cookie_k;
-
-               cookie_k = hip_get_cookie_difficulty_by_index(i);
-
-               r1table[i].r1 = hip_create_r1(hit, sign, privkey, pubkey,
-                                             cookie_km);
-               if (!r1table[i].r1) {
-                       HIP_ERROR("Unable to precreate R1s\n");
-                       goto err_out;
-               }
-
-               HIP_DEBUG("Packet %d created\n", i);
-       }
-
-       return 1;
-
- err_out:
-       return 0;
+                        struct in6_addr *hit_i)
+{
+    register u32 base = 0;
+    int i;
+
+    /* Indexing based on the initiator HIT only: however, this may happen
+     * on the expense of DoS protection against zombies. */
+    for (i = 0; i < 4; i++) {
+        base ^= hit_i->s6_addr32[i];
+    }
+
+    for (i = 0; i < 3; i++) {
+        base ^= ((base >> (24 - i * 8)) & 0xFF);
+    }
+
+    /* base ready */
+
+    HIP_DEBUG("idx=%d\n", ((base) % HIP_R1TABLESIZE));
+
+    return (base) % HIP_R1TABLESIZE;
+}
+
+int hip_get_cookie_difficulty(hip_hit_t *hit_i)
+{
+    return hip_puzzle_k[hip_calc_cookie_idx(NULL, NULL, hit_i)];
+}
+
+static int hip_get_cookie_difficulty_by_index(int r1_index)
+{
+    return hip_puzzle_k[r1_index];
+}
+
+int hip_set_cookie_difficulty(hip_hit_t *hit_i, int k)
+{
+    if (k > HIP_PUZZLE_MAX_K || k < 1) {
+        HIP_ERROR("Bad cookie value (%d), min=%d, max=%d\n",
+                  k, 1, HIP_PUZZLE_MAX_K);
+        return -1;
+    }
+
+    hip_puzzle_k[hip_calc_cookie_idx(NULL, NULL, hit_i)] = k;
+
+    HIP_DEBUG("HIP cookie value set to %d\n", k);
+    return k;
+}
+
+int hip_inc_cookie_difficulty(hip_hit_t *hit_i)
+{
+    int k = hip_get_cookie_difficulty(hit_i) + 1;
+    return hip_set_cookie_difficulty(hit_i, k);
+}
+
+int hip_dec_cookie_difficulty(hip_hit_t *hit_i)
+{
+    int k = hip_get_cookie_difficulty(hit_i) - 1;
+    return hip_set_cookie_difficulty(hit_i, k);
+}
+
+int hip_precreate_r1(struct hip_r1entry *r1table, struct in6_addr *hit,
+                     int (*sign)(struct hip_host_id *p, struct hip_common *m),
+                     void *privkey, struct hip_host_id *pubkey)
+{
+    int i;
+
+    for (i = 0; i < HIP_R1TABLESIZE; i++) {
+        int cookie_k;
+
+        cookie_k      = hip_get_cookie_difficulty_by_index(i);
+
+        r1table[i].r1 = hip_create_r1(hit, sign, privkey, pubkey,
+                                      cookie_km);
+        if (!r1table[i].r1) {
+            HIP_ERROR("Unable to precreate R1s\n");
+            goto err_out;
+        }
+
+        HIP_DEBUG("Packet %d created\n", i);
+    }
+
+    return 1;
+
+err_out:
+    return 0;
 }
 
 #endif /* CONFIG_HIP_ICOOKIE */

=== modified file 'hipd/icookie.c.doxyme'
--- hipd/icookie.c.doxyme       2009-12-12 10:44:54 +0000
+++ hipd/icookie.c.doxyme       2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,54 +45,54 @@
 
 
 /**
- * hip_calc_cookie_idx 
+ * hip_calc_cookie_idx
  *
  *
  * @param ip_i
  * @param ip_r
  * @param hit_i
- * @return 
- **/
-
-
-/**
- * hip_dec_cookie_difficulty 
- *
- *
- * @param hit_i
- * @return 
- **/
-
-
-/**
- * hip_get_cookie_difficulty 
- *
- *
- * @param hit_i
- * @return 
- **/
-
-
-/**
- * hip_get_cookie_difficulty_by_index 
+ * @return
+ **/
+
+
+/**
+ * hip_dec_cookie_difficulty
+ *
+ *
+ * @param hit_i
+ * @return
+ **/
+
+
+/**
+ * hip_get_cookie_difficulty
+ *
+ *
+ * @param hit_i
+ * @return
+ **/
+
+
+/**
+ * hip_get_cookie_difficulty_by_index
  *
  *
  * @param r1_index
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_inc_cookie_difficulty 
+ * hip_inc_cookie_difficulty
  *
  *
  * @param hit_i
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_precreate_r1 
+ * hip_precreate_r1
  *
  *
  * @param r1table
@@ -100,16 +101,15 @@
  * @param m)
  * @param privkey
  * @param pubkey)
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_set_cookie_difficulty 
+ * hip_set_cookie_difficulty
  *
  *
  * @param hit_i
  * @param k
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/icookie.h.doxyme'
--- hipd/icookie.h.doxyme       2009-12-12 10:44:54 +0000
+++ hipd/icookie.h.doxyme       2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/icookie.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/init.c'
--- hipd/init.c 2010-02-08 15:18:06 +0000
+++ hipd/init.c 2010-02-10 22:32:46 +0000
@@ -31,41 +31,41 @@
 /**
  * HIP daemon lock file is used to prevent multiple instances
  * of the daemon to start and to record current daemon pid.
- */ 
-#define HIP_DAEMON_LOCK_FILE   HIPL_LOCKDIR"/hipd.lock"
+ */
+#define HIP_DAEMON_LOCK_FILE    HIPL_LOCKDIR "/hipd.lock"
 
 /* the /etc/hip/dhtservers file*/
-#define HIPL_DHTSERVERS_FILE     HIPL_SYSCONFDIR"/dhtservers"
+#define HIPL_DHTSERVERS_FILE     HIPL_SYSCONFDIR "/dhtservers"
 
 #define HIPL_DHTSERVERS_FILE_EX \
-"193.167.187.134 hipdht2.infrahip.net\n"
+    "193.167.187.134 hipdht2.infrahip.net\n"
 
 
 #ifndef ANDROID_CHANGES
 
 /** ICMPV6_FILTER related stuff **/
-#define BIT_CLEAR(nr, addr) do { ((__u32 *)(addr))[(nr) >> 5] &= ~(1U << ((nr) 
& 31)); } while(0)
-#define BIT_SET(nr, addr) do { ((__u32 *)(addr))[(nr) >> 5] |= (1U << ((nr) & 
31)); } while(0)
-#define BIT_TEST(nr, addr) do { (__u32 *)(addr))[(nr) >> 5] & (1U << ((nr) & 
31)); } while(0)
+#define BIT_CLEAR(nr, addr) do { ((__u32 *) (addr))[(nr) >> 5] &= ~(1U << 
((nr) & 31)); } while (0)
+#define BIT_SET(nr, addr) do { ((__u32 *) (addr))[(nr) >> 5] |= (1U << ((nr) & 
31)); } while (0)
+#define BIT_TEST(nr, addr) do { (__u32 *) (addr))[(nr) >> 5] & (1U << ((nr) & 
31)); } while (0)
 
 #ifndef ICMP6_FILTER_WILLPASS
 #define ICMP6_FILTER_WILLPASS(type, filterp) \
-        (BIT_TEST((type), filterp) == 0)
+    (BIT_TEST((type), filterp) == 0)
 
 #define ICMP6_FILTER_WILLBLOCK(type, filterp) \
-        BIT_TEST((type), filterp)
+    BIT_TEST((type), filterp)
 
 #define ICMP6_FILTER_SETPASS(type, filterp) \
-        BIT_CLEAR((type), filterp)
+    BIT_CLEAR((type), filterp)
 
 #define ICMP6_FILTER_SETBLOCK(type, filterp) \
-        BIT_SET((type), filterp)
+    BIT_SET((type), filterp)
 
 #define ICMP6_FILTER_SETPASSALL(filterp) \
-        memset(filterp, 0, sizeof(struct icmp6_filter));
+    memset(filterp, 0, sizeof(struct icmp6_filter));
 
 #define ICMP6_FILTER_SETBLOCKALL(filterp) \
-        memset(filterp, 0xFF, sizeof(struct icmp6_filter));
+    memset(filterp, 0xFF, sizeof(struct icmp6_filter));
 #endif
 /** end ICMPV6_FILTER related stuff **/
 
@@ -75,145 +75,153 @@
 static int init_random_seed(void);
 static int hip_init_certs(void);
 static int hip_init_raw_sock_v6(int *hip_raw_sock_v6, int proto);
-static struct hip_host_id_entry * hip_return_first_rsa(void);
+static struct hip_host_id_entry *hip_return_first_rsa(void);
 
 
/******************************************************************************/
 /** Catch SIGCHLD. */
 static void hip_sig_chld(int signum)
 {
 #ifdef ANDROID_CHANGES
-       int status;
+    int status;
 #else
-       union wait status;
+    union wait status;
 #endif
 
-       int pid;
-
-       signal(signum, hip_sig_chld);
-
-       /* Get child process status, so it wont be left as zombie for long 
time. */
-       while ((pid = wait3(&status, WNOHANG, 0)) > 0)
-       {
-               /* Maybe do something.. */
-               _HIP_DEBUG("Child quit with pid %d\n", pid);
-       }
+    int pid;
+
+    signal(signum, hip_sig_chld);
+
+    /* Get child process status, so it wont be left as zombie for long time. */
+    while ((pid = wait3(&status, WNOHANG, 0)) > 0) {
+        /* Maybe do something.. */
+        _HIP_DEBUG("Child quit with pid %d\n", pid);
+    }
 }
 
 static int set_cloexec_flag(int desc, int value)
 {
-       int oldflags = fcntl (desc, F_GETFD, 0);
-       /* If reading the flags failed, return error indication now.*/
-          if (oldflags < 0)
-                  return oldflags;
-          /* Set just the flag we want to set. */
+    int oldflags = fcntl(desc, F_GETFD, 0);
+    /* If reading the flags failed, return error indication now.*/
+    if (oldflags < 0) {
+        return oldflags;
+    }
+    /* Set just the flag we want to set. */
 
-       if (value != 0)
-               oldflags |= FD_CLOEXEC;
-       else
-               oldflags &= ~FD_CLOEXEC;
-       /* Store modified flag word in the descriptor. */
-       return fcntl (desc, F_SETFD, oldflags);
+    if (value != 0) {
+        oldflags |= FD_CLOEXEC;
+    } else {
+        oldflags &= ~FD_CLOEXEC;
+    }
+    /* Store modified flag word in the descriptor. */
+    return fcntl(desc, F_SETFD, oldflags);
 }
 
 #ifndef CONFIG_HIP_OPENWRT
 #ifdef CONFIG_HIP_DEBUG
 static void hip_print_sysinfo(void)
 {
-       FILE *fp = NULL;
-       char str[256];
-       int current = 0;
-       int pipefd[2];
-       int stdout_fd;
-       int ch;
-
-       fp = fopen("/etc/debian_version", "r");
-       if(!fp)
-               fp = fopen("/etc/redhat-release", "r");
-
-       if(fp) {
-
-               while(fgets(str, sizeof(str), fp)) {
-                       HIP_DEBUG("version=%s", str);
-               }
-               if (fclose(fp))
-                       HIP_ERROR("Error closing version file\n");
-               fp = NULL;
-
-       }
-
-       fp = fopen("/proc/cpuinfo", "r");
-       if(fp) {
-
-               HIP_DEBUG("Printing /proc/cpuinfo\n");
-
-               /* jk: char != int !!! */
-               while ((ch = fgetc(fp)) != EOF) {
-                       str[current] = ch;
-                       /* Tabs end up broken in syslog: remove */
-                       if (str[current] == '\t')
-                               continue;
-                       if(str[current++] == '\n' || current == sizeof(str)-1){
-                               str[current] = '\0';
-                               HIP_DEBUG(str);
-                               current = 0;
-                       }
-               }
-
-               if (fclose(fp))
-                       HIP_ERROR("Error closing /proc/cpuinfo\n");
-               fp = NULL;
-
-       } else {
-               HIP_ERROR("Failed to open file /proc/cpuinfo\n");
-       }
-
-       /* Route stdout into a pipe to capture lsmod output */
-
-       stdout_fd = dup(1);
-       if (stdout_fd < 0) {
-               HIP_ERROR("Stdout backup failed\n");
-               return;
-       }
-       if (pipe(pipefd)) {
-               HIP_ERROR("Pipe creation failed\n");
-               return;
-       }
-       if (dup2(pipefd[1], 1) < 0) {
-               HIP_ERROR("Stdout capture failed\n");
-               if (close(pipefd[1]))
-                       HIP_ERROR("Error closing write end of pipe\n");
-               if (close(pipefd[0]))
-                       HIP_ERROR("Error closing read end of pipe\n");
-               return;
-       }
-
-       if ( system("lsmod") == -1 ) {
-               HIP_ERROR("lsmod failed");
-       };
-
-       if (dup2(stdout_fd, 1) < 0)
-               HIP_ERROR("Stdout restore failed\n");
-       if (close(stdout_fd))
-               HIP_ERROR("Error closing stdout backup\n");
-       if (close(pipefd[1]))
-               HIP_ERROR("Error closing write end of pipe\n");
-
-       fp = fdopen(pipefd[0], "r");
-       if(fp) {
-
-               HIP_DEBUG("Printing lsmod output\n");
-               while(fgets(str, sizeof(str), fp)) {
-                       HIP_DEBUG(str);
-               }
-               if (fclose(fp))
-                       HIP_ERROR("Error closing read end of pipe\n");
-
-       } else {
-               HIP_ERROR("Error opening pipe for reading\n");
-               if (close(pipefd[0]))
-                       HIP_ERROR("Error closing read end of pipe\n");
-       }
+    FILE *fp    = NULL;
+    char str[256];
+    int current = 0;
+    int pipefd[2];
+    int stdout_fd;
+    int ch;
+
+    fp = fopen("/etc/debian_version", "r");
+    if (!fp) {
+        fp = fopen("/etc/redhat-release", "r");
+    }
+
+    if (fp) {
+        while (fgets(str, sizeof(str), fp)) {
+            HIP_DEBUG("version=%s", str);
+        }
+        if (fclose(fp)) {
+            HIP_ERROR("Error closing version file\n");
+        }
+        fp = NULL;
+    }
+
+    fp = fopen("/proc/cpuinfo", "r");
+    if (fp) {
+        HIP_DEBUG("Printing /proc/cpuinfo\n");
+
+        /* jk: char != int !!! */
+        while ((ch = fgetc(fp)) != EOF) {
+            str[current] = ch;
+            /* Tabs end up broken in syslog: remove */
+            if (str[current] == '\t') {
+                continue;
+            }
+            if (str[current++] == '\n' || current == sizeof(str) - 1) {
+                str[current] = '\0';
+                HIP_DEBUG(str);
+                current      = 0;
+            }
+        }
+
+        if (fclose(fp)) {
+            HIP_ERROR("Error closing /proc/cpuinfo\n");
+        }
+        fp = NULL;
+    } else {
+        HIP_ERROR("Failed to open file /proc/cpuinfo\n");
+    }
+
+    /* Route stdout into a pipe to capture lsmod output */
+
+    stdout_fd = dup(1);
+    if (stdout_fd < 0) {
+        HIP_ERROR("Stdout backup failed\n");
+        return;
+    }
+    if (pipe(pipefd)) {
+        HIP_ERROR("Pipe creation failed\n");
+        return;
+    }
+    if (dup2(pipefd[1], 1) < 0) {
+        HIP_ERROR("Stdout capture failed\n");
+        if (close(pipefd[1])) {
+            HIP_ERROR("Error closing write end of pipe\n");
+        }
+        if (close(pipefd[0])) {
+            HIP_ERROR("Error closing read end of pipe\n");
+        }
+        return;
+    }
+
+    if (system("lsmod") == -1) {
+        HIP_ERROR("lsmod failed");
+    }
+    ;
+
+    if (dup2(stdout_fd, 1) < 0) {
+        HIP_ERROR("Stdout restore failed\n");
+    }
+    if (close(stdout_fd)) {
+        HIP_ERROR("Error closing stdout backup\n");
+    }
+    if (close(pipefd[1])) {
+        HIP_ERROR("Error closing write end of pipe\n");
+    }
+
+    fp = fdopen(pipefd[0], "r");
+    if (fp) {
+        HIP_DEBUG("Printing lsmod output\n");
+        while (fgets(str, sizeof(str), fp)) {
+            HIP_DEBUG(str);
+        }
+        if (fclose(fp)) {
+            HIP_ERROR("Error closing read end of pipe\n");
+        }
+    } else {
+        HIP_ERROR("Error opening pipe for reading\n");
+        if (close(pipefd[0])) {
+            HIP_ERROR("Error closing read end of pipe\n");
+        }
+    }
 }
+
 #endif
 #endif
 
@@ -222,156 +230,159 @@
  */
 static void hip_create_file_unless_exists(const char *path, const char 
*contents)
 {
-        struct stat status;
-        if (stat(path, &status)  == 0)
-                return;
+    struct stat status;
+    if (stat(path, &status)  == 0) {
+        return;
+    }
 
-        FILE *fp = fopen(path, "w");
-        HIP_ASSERT(fp);
-        size_t items = fwrite(contents, strlen(contents), 1, fp);
-        HIP_ASSERT(items > 0);
-        fclose(fp);
+    FILE *fp     = fopen(path, "w");
+    HIP_ASSERT(fp);
+    size_t items = fwrite(contents, strlen(contents), 1, fp);
+    HIP_ASSERT(items > 0);
+    fclose(fp);
 }
 
-
 static void hip_load_configuration(void)
 {
-       const char *cfile = "default";
-
-        /* HIPL_CONFIG_FILE, HIPL_CONFIG_FILE_EX and so on are defined in in 
the autogenerated config.h */
-
-        hip_create_file_unless_exists(HIPL_CONFIG_FILE, HIPL_CONFIG_FILE_EX);
-
-       hip_create_file_unless_exists(HIPL_HOSTS_FILE, HIPL_HOSTS_FILE_EX);
+    const char *cfile = "default";
+
+    /* HIPL_CONFIG_FILE, HIPL_CONFIG_FILE_EX and so on are defined in
+     * the auto-generated config.h */
+
+    hip_create_file_unless_exists(HIPL_CONFIG_FILE, HIPL_CONFIG_FILE_EX);
+
+    hip_create_file_unless_exists(HIPL_HOSTS_FILE, HIPL_HOSTS_FILE_EX);
 
 #ifdef CONFIG_HIP_I3
-       hip_create_file_unless_exists(HIPL_HI3_FILE, HIPL_HI3_FILE_EX);
+    hip_create_file_unless_exists(HIPL_HI3_FILE, HIPL_HI3_FILE_EX);
 #endif
-       hip_create_file_unless_exists(HIPL_DHTSERVERS_FILE, 
HIPL_DHTSERVERS_FILE_EX);
-
-       hip_create_file_unless_exists(HIPL_NSUPDATE_CONF_FILE, 
HIPL_NSUPDATE_CONF_FILE_EX);
-       
-       /* Load the configuration. The configuration is loaded as a sequence
-          of hipd system calls. Assumably the user socket buffer is large
-          enough to buffer all of the hipconf commands.. */
-
-       hip_conf_handle_load(NULL, ACTION_LOAD, &cfile, 1, 1);
+    hip_create_file_unless_exists(HIPL_DHTSERVERS_FILE, 
HIPL_DHTSERVERS_FILE_EX);
+
+    hip_create_file_unless_exists(HIPL_NSUPDATE_CONF_FILE, 
HIPL_NSUPDATE_CONF_FILE_EX);
+
+    /* Load the configuration. The configuration is loaded as a sequence
+     * of hipd system calls. Assumably the user socket buffer is large
+     * enough to buffer all of the hipconf commands.. */
+
+    hip_conf_handle_load(NULL, ACTION_LOAD, &cfile, 1, 1);
 }
 
 static void hip_set_os_dep_variables(void)
 {
-       struct utsname un;
-       int rel[4] = {0};
-
-       uname(&un);
-
-       HIP_DEBUG("sysname=%s nodename=%s release=%s version=%s machine=%s\n",
-                 un.sysname, un.nodename, un.release, un.version, un.machine);
-
-       sscanf(un.release, "%d.%d.%d.%d", &rel[0], &rel[1], &rel[2], &rel[3]);
-
-       /*
-         2.6.19 and above introduced some changes to kernel API names:
-         - XFRM_BEET changed from 2 to 4
-         - crypto algo names changed
-       */
+    struct utsname un;
+    int rel[4] = {0};
+
+    uname(&un);
+
+    HIP_DEBUG("sysname=%s nodename=%s release=%s version=%s machine=%s\n",
+              un.sysname, un.nodename, un.release, un.version, un.machine);
+
+    sscanf(un.release, "%d.%d.%d.%d", &rel[0], &rel[1], &rel[2], &rel[3]);
+
+    /*
+     * 2.6.19 and above introduced some changes to kernel API names:
+     * - XFRM_BEET changed from 2 to 4
+     * - crypto algo names changed
+     */
 
 #ifndef CONFIG_HIP_PFKEY
-       if (rel[0] <= 2 && rel[1] <= 6 && rel[2] < 19) {
-               hip_xfrm_set_beet(2);
-               hip_xfrm_set_algo_names(0);
-       } else {
-               //hip_xfrm_set_beet(1); /* TUNNEL mode */
-               hip_xfrm_set_beet(4); /* BEET mode */
-               hip_xfrm_set_algo_names(1);
-       }
+    if (rel[0] <= 2 && rel[1] <= 6 && rel[2] < 19) {
+        hip_xfrm_set_beet(2);
+        hip_xfrm_set_algo_names(0);
+    } else {
+        //hip_xfrm_set_beet(1); /* TUNNEL mode */
+        hip_xfrm_set_beet(4);         /* BEET mode */
+        hip_xfrm_set_algo_names(1);
+    }
 #endif
 
 #ifndef CONFIG_HIP_PFKEY
-       /* This requires new kernel versions (the 2.6.18 patch) - jk */
-        hip_xfrm_set_default_sa_prefix_len(128);
+    /* This requires new kernel versions (the 2.6.18 patch) - jk */
+    hip_xfrm_set_default_sa_prefix_len(128);
 #endif
 }
 
 #ifdef CONFIG_HIP_AGENT
 /**
- * hip_init_daemon_hitdb - The function initialzies the database at daemon
- * which recives the information from agent to be stored
+ * hip_init_daemon_hitdb - The function initializes the database at daemon
+ * which receives the information from agent to be stored
  */
 static int hip_init_daemon_hitdb(void)
 {
-       char *file = HIP_CERT_DB_PATH_AND_NAME;
-       int err = 0 ;
-       
-       _HIP_DEBUG("Loading HIT database from %s.\n", file);
-       daemon_db = hip_sqlite_open_db(file, HIP_CERT_DB_CREATE_TBLS);
-       HIP_IFE(!daemon_db, -1);
+    char *file = HIP_CERT_DB_PATH_AND_NAME;
+    int err    = 0;
+
+    _HIP_DEBUG("Loading HIT database from %s.\n", file);
+    daemon_db = hip_sqlite_open_db(file, HIP_CERT_DB_CREATE_TBLS);
+    HIP_IFE(!daemon_db, -1);
 
 out_err:
-       return (err);
+    return err;
 }
-#endif /* CONFIG_HIP_AGENT */
+
+#endif  /* CONFIG_HIP_AGENT */
 
 /**
- * Init raw ipv4 socket.
+ * Initialize raw ipv4 socket.
  */
 static int hip_init_raw_sock_v4(int *hip_raw_sock_v4, int proto)
 {
-       int on = 1, err = 0;
-       int off = 0;
-
-       *hip_raw_sock_v4 = socket(AF_INET, SOCK_RAW, proto);
-       set_cloexec_flag(*hip_raw_sock_v4, 1);
-       HIP_IFEL(*hip_raw_sock_v4 <= 0, 1, "Raw socket v4 creation failed. Not 
root?\n");
-
-       /* see bug id 212 why RECV_ERR is off */
-       err = setsockopt(*hip_raw_sock_v4, IPPROTO_IP, IP_RECVERR, &off, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt v4 recverr failed\n");
-       err = setsockopt(*hip_raw_sock_v4, SOL_SOCKET, SO_BROADCAST, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt v4 failed to set broadcast \n");
-       err = setsockopt(*hip_raw_sock_v4, IPPROTO_IP, IP_PKTINFO, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt v4 pktinfo failed\n");
-       err = setsockopt(*hip_raw_sock_v4, SOL_SOCKET, SO_REUSEADDR, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt v4 reuseaddr failed\n");
-
- out_err:
-       return err;
+    int on  = 1, err = 0;
+    int off = 0;
+
+    *hip_raw_sock_v4 = socket(AF_INET, SOCK_RAW, proto);
+    set_cloexec_flag(*hip_raw_sock_v4, 1);
+    HIP_IFEL(*hip_raw_sock_v4 <= 0, 1, "Raw socket v4 creation failed. Not 
root?\n");
+
+    /* see bug id 212 why RECV_ERR is off */
+    err = setsockopt(*hip_raw_sock_v4, IPPROTO_IP, IP_RECVERR, &off, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt v4 recverr failed\n");
+    err = setsockopt(*hip_raw_sock_v4, SOL_SOCKET, SO_BROADCAST, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt v4 failed to set broadcast \n");
+    err = setsockopt(*hip_raw_sock_v4, IPPROTO_IP, IP_PKTINFO, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt v4 pktinfo failed\n");
+    err = setsockopt(*hip_raw_sock_v4, SOL_SOCKET, SO_REUSEADDR, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt v4 reuseaddr failed\n");
+
+out_err:
+    return err;
 }
 
 /**
- * Init icmpv6 socket.
+ * Initialize icmpv6 socket.
  */
 static int hip_init_icmp_v6(int *icmpsockfd)
 {
-       int err = 0, on = 1;
-       struct icmp6_filter filter;
-
-       /* Make sure that hipd does not send icmpv6 immediately after base 
exchange */
-       heartbeat_counter = hip_icmp_interval;
-
-       *icmpsockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
-       set_cloexec_flag(*icmpsockfd, 1);
-       HIP_IFEL(*icmpsockfd <= 0, 1, "ICMPv6 socket creation failed\n");
-
-       ICMP6_FILTER_SETBLOCKALL(&filter);
+    int err = 0, on = 1;
+    struct icmp6_filter filter;
+
+    /* Make sure that hipd does not send icmpv6 immediately after base 
exchange */
+    heartbeat_counter = hip_icmp_interval;
+
+    *icmpsockfd       = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+    set_cloexec_flag(*icmpsockfd, 1);
+    HIP_IFEL(*icmpsockfd <= 0, 1, "ICMPv6 socket creation failed\n");
+
+    ICMP6_FILTER_SETBLOCKALL(&filter);
 #ifdef ANDROID_CHANGES
-       ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
-       err = setsockopt(*icmpsockfd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
-                        sizeof(struct icmp6_filter));
+    ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
+    err = setsockopt(*icmpsockfd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
+                     sizeof(struct icmp6_filter));
 #else
-       ICMP6_FILTER_SETPASS(ICMPV6_ECHO_REPLY, &filter);
-       err = setsockopt(*icmpsockfd, IPPROTO_ICMPV6, ICMPV6_FILTER, &filter,
-                        sizeof(struct icmp6_filter));
+    ICMP6_FILTER_SETPASS(ICMPV6_ECHO_REPLY, &filter);
+    err = setsockopt(*icmpsockfd, IPPROTO_ICMPV6, ICMPV6_FILTER, &filter,
+                     sizeof(struct icmp6_filter));
 #endif
-       HIP_IFEL(err, -1, "setsockopt icmp ICMP6_FILTER failed\n");
-
-
-       err = setsockopt(*icmpsockfd, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt icmp IPV6_RECVPKTINFO failed\n");
-
- out_err:
-       return err;
+    HIP_IFEL(err, -1, "setsockopt icmp ICMP6_FILTER failed\n");
+
+
+    err = setsockopt(*icmpsockfd, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt icmp IPV6_RECVPKTINFO failed\n");
+
+out_err:
+    return err;
 }
+
 /**
  * Probe kernel modules.
  */
@@ -380,43 +391,44 @@
 #ifndef ANDROID_CHANGES
 static void hip_probe_kernel_modules(void)
 {
-       int count, err, status;
-       char cmd[40];
-       int mod_total;
-       char *mod_name[] =
-       {
-               "xfrm6_tunnel", "xfrm4_tunnel",
-               "ip6_tunnel", "ipip", "ip4_tunnel",
-               "xfrm_user", "dummy", "esp6", "esp4",
-               "ipv6", "crypto_null", "cbc",
-               "blkcipher", "des", "aes",
-               "xfrm4_mode_beet", "xfrm6_mode_beet", "sha1",
-               "capability"
-       };
-
-       mod_total = sizeof(mod_name) / sizeof(char *);
-
-       HIP_DEBUG("Probing for %d modules. When the modules are built-in, the 
errors can be ignored\n", mod_total);
-
-       for (count = 0; count < mod_total; count++)
-       {
-               snprintf(cmd, sizeof(cmd), "%s %s", "/sbin/modprobe", 
mod_name[count]);
-               HIP_DEBUG("%s\n", cmd);
-               err = fork();
-               if (err < 0) HIP_ERROR("Failed to fork() for modprobe!\n");
-               else if (err == 0)
-               {
-                       /* Redirect stderr, so few non fatal errors wont show 
up. */
-                       if ( freopen("/dev/null", "w", stderr) == NULL ) {
-                               HIP_ERROR("freopen if /dev/null failed.");
-                       };
-                       execlp("/sbin/modprobe", "/sbin/modprobe", 
mod_name[count], (char *)NULL);
-               }
-               else waitpid(err, &status, 0);
-       }
-
-       HIP_DEBUG("Probing completed\n");
+    int count, err, status;
+    char cmd[40];
+    int mod_total;
+    char *mod_name[] =
+    {
+        "xfrm6_tunnel",    "xfrm4_tunnel",
+        "ip6_tunnel",      "ipip",           "ip4_tunnel",
+        "xfrm_user",       "dummy",          "esp6", "esp4",
+        "ipv6",            "crypto_null",    "cbc",
+        "blkcipher",       "des",            "aes",
+        "xfrm4_mode_beet", "xfrm6_mode_beet","sha1",
+        "capability"
+    };
+
+    mod_total = sizeof(mod_name) / sizeof(char *);
+
+    HIP_DEBUG("Probing for %d modules. When the modules are built-in, the 
errors can be ignored\n", mod_total);
+
+    for (count = 0; count < mod_total; count++) {
+        snprintf(cmd, sizeof(cmd), "%s %s", "/sbin/modprobe", mod_name[count]);
+        HIP_DEBUG("%s\n", cmd);
+        err = fork();
+        if (err < 0) {
+            HIP_ERROR("Failed to fork() for modprobe!\n");
+        } else if (err == 0) {
+            /* Redirect stderr, so few non fatal errors wont show up. */
+            if (freopen("/dev/null", "w", stderr) == NULL) {
+                HIP_ERROR("freopen if /dev/null failed.");
+            }
+            ;
+            execlp("/sbin/modprobe", "/sbin/modprobe", mod_name[count], (char 
*) NULL);
+        } else {waitpid(err, &status, 0);
+        }
+    }
+
+    HIP_DEBUG("Probing completed\n");
 }
+
 #endif /* ANDROID_CHANGES */
 #endif /* CONFIG_HIP_OPENWRT */
 
@@ -425,11 +437,11 @@
  */
 int hipd_init(int flush_ipsec, int killold)
 {
-       int err = 0, certerr = 0, hitdberr = 0;
-       unsigned int mtu_val = HIP_HIT_DEV_MTU;
-       char str[64];
-       char mtu[16];
-       struct sockaddr_in6 daemon_addr;
+    int err              = 0, certerr = 0, hitdberr = 0;
+    unsigned int mtu_val = HIP_HIT_DEV_MTU;
+    char str[64];
+    char mtu[16];
+    struct sockaddr_in6 daemon_addr;
 
 #ifndef ANDROID_CHANGES
 #ifdef HIP_LIBINET6
@@ -438,240 +450,245 @@
 #endif /* HIP_LIBINET6 */
 #endif /* ANDROID_CHANGES */
 
-       memset(str, 0, 64);
-       memset(mtu, 0, 16);
-
-       /* Make sure that root path is set up correcly (e.g. on Fedora 9).
-          Otherwise may get warnings from system() commands.
-          @todo: should append, not overwrite  */
-       setenv("PATH", HIP_DEFAULT_EXEC_PATH, 1);
-
-       /* Open daemon lock file and read pid from it. */
-       HIP_IFEL(hip_create_lock_file(HIP_DAEMON_LOCK_FILE, killold), -1,
-                "locking failed\n");
-
-       hip_init_hostid_db(NULL);
-
-       hip_set_os_dep_variables();
-
-       #if 0
-       hip_bex_timestamp_db_init();
-       #endif
+    memset(str, 0, 64);
+    memset(mtu, 0, 16);
+
+    /* Make sure that root path is set up correcly (e.g. on Fedora 9).
+     * Otherwise may get warnings from system() commands.
+     * @todo: should append, not overwrite  */
+    setenv("PATH", HIP_DEFAULT_EXEC_PATH, 1);
+
+    /* Open daemon lock file and read pid from it. */
+    HIP_IFEL(hip_create_lock_file(HIP_DAEMON_LOCK_FILE, killold), -1,
+             "locking failed\n");
+
+    hip_init_hostid_db(NULL);
+
+    hip_set_os_dep_variables();
+
+        #if 0
+    hip_bex_timestamp_db_init();
+        #endif
 
 #ifndef CONFIG_HIP_OPENWRT
 #ifdef CONFIG_HIP_DEBUG
-       hip_print_sysinfo();
+    hip_print_sysinfo();
 #endif
 #ifndef ANDROID_CHANGES
-       hip_probe_kernel_modules();
+    hip_probe_kernel_modules();
 #endif
 #endif
 
-       /* Register signal handlers */
-       signal(SIGINT, hip_close);
-       signal(SIGTERM, hip_close);
-       signal(SIGCHLD, hip_sig_chld);
+    /* Register signal handlers */
+    signal(SIGINT, hip_close);
+    signal(SIGTERM, hip_close);
+    signal(SIGCHLD, hip_sig_chld);
 
 #ifdef CONFIG_HIP_OPPORTUNISTIC
-       HIP_IFEL(hip_init_oppip_db(), -1,
-                "Cannot initialize opportunistic mode IP database for "\
-                 "non HIP capable hosts!\n");
+    HIP_IFEL(hip_init_oppip_db(), -1,
+             "Cannot initialize opportunistic mode IP database for " \
+             "non HIP capable hosts!\n");
 #endif
-       HIP_IFEL((hip_init_cipher() < 0), 1, "Unable to init ciphers.\n");
-
-       HIP_IFE(init_random_seed(), -1);
-
-       hip_init_hadb();
-        /* hip_init_puzzle_defaults just returns, removed -samu  */
+    HIP_IFEL((hip_init_cipher() < 0), 1, "Unable to init ciphers.\n");
+
+    HIP_IFE(init_random_seed(), -1);
+
+    hip_init_hadb();
+    /* hip_init_puzzle_defaults just returns, removed -samu  */
 #if 0
-       hip_init_puzzle_defaults();
+    hip_init_puzzle_defaults();
 #endif
 
 #ifdef CONFIG_HIP_OPPORTUNISTIC
-       hip_init_opp_db();
+    hip_init_opp_db();
 #endif
 
 
-       /* Resolve our current addresses, afterwards the events from kernel
-          will maintain the list This needs to be done before opening
-          NETLINK_ROUTE! See the comment about address_count global var. */
-       HIP_DEBUG("Initializing the netdev_init_addresses\n");
-
-       hip_netdev_init_addresses(&hip_nl_ipsec);
-
-       if (rtnl_open_byproto(&hip_nl_route,
-                             RTMGRP_LINK | RTMGRP_IPV6_IFADDR | IPPROTO_IPV6
-                             | RTMGRP_IPV4_IFADDR | IPPROTO_IP,
-                             NETLINK_ROUTE) < 0)
-       {
-               err = 1;
-               HIP_ERROR("Routing socket error: %s\n", strerror(errno));
-               goto out_err;
-       }
-
-       /* Open the netlink socket for address and IF events */
-       if (rtnl_open_byproto(&hip_nl_ipsec, XFRMGRP_ACQUIRE, NETLINK_XFRM) < 0)
-       {
-               HIP_ERROR("Netlink address and IF events socket error: %s\n", 
strerror(errno));
-               err = 1;
-               goto out_err;
-       }
+    /* Resolve our current addresses, afterwards the events from kernel
+     * will maintain the list This needs to be done before opening
+     * NETLINK_ROUTE! See the comment about address_count global var. */
+    HIP_DEBUG("Initializing the netdev_init_addresses\n");
+
+    hip_netdev_init_addresses(&hip_nl_ipsec);
+
+    if (rtnl_open_byproto(&hip_nl_route,
+                          RTMGRP_LINK | RTMGRP_IPV6_IFADDR | IPPROTO_IPV6
+                          | RTMGRP_IPV4_IFADDR | IPPROTO_IP,
+                          NETLINK_ROUTE) < 0) {
+        err = 1;
+        HIP_ERROR("Routing socket error: %s\n", strerror(errno));
+        goto out_err;
+    }
+
+    /* Open the netlink socket for address and IF events */
+    if (rtnl_open_byproto(&hip_nl_ipsec, XFRMGRP_ACQUIRE, NETLINK_XFRM) < 0) {
+        HIP_ERROR("Netlink address and IF events socket error: %s\n",
+                  strerror(errno));
+        err = 1;
+        goto out_err;
+    }
 
 #ifndef CONFIG_HIP_PFKEY
-       hip_xfrm_set_nl_ipsec(&hip_nl_ipsec);
-#endif
-
-#if 0
-       {
-                int ret_sockopt = 0, value = 0;
-                socklen_t value_len = sizeof(value);
-               int ipsec_buf_size = 200000;
-               socklen_t ipsec_buf_sizeof = sizeof(ipsec_buf_size);
-                ret_sockopt = getsockopt(hip_nl_ipsec.fd, SOL_SOCKET, 
SO_RCVBUF,
-                                         &value, &value_len);
-                if (ret_sockopt != 0)
-                    HIP_DEBUG("Getting receive buffer size of hip_nl_ipsec.fd 
failed\n");
-                ipsec_buf_size = value * 2;
-                HIP_DEBUG("Default setting of receive buffer size for 
hip_nl_ipsec was %d.\n"
-                          "Setting it to %d.\n", value, ipsec_buf_size);
-               ret_sockopt = setsockopt(hip_nl_ipsec.fd, SOL_SOCKET, SO_RCVBUF,
-                          &ipsec_buf_size, ipsec_buf_sizeof);
-                if (ret_sockopt !=0 )
-                    HIP_DEBUG("Setting receive buffer size of hip_nl_ipsec.fd 
failed\n");
-                ret_sockopt = 0;
-               ret_sockopt = setsockopt(hip_nl_ipsec.fd, SOL_SOCKET, SO_SNDBUF,
-                          &ipsec_buf_size, ipsec_buf_sizeof);
-                if (ret_sockopt !=0 )
-                    HIP_DEBUG("Setting send buffer size of hip_nl_ipsec.fd 
failed\n");
-       }
-#endif
-
-       HIP_IFEL(hip_init_raw_sock_v6(&hip_raw_sock_output_v6, IPPROTO_HIP), 
-1, "raw sock output v6\n");
-       HIP_IFEL(hip_init_raw_sock_v4(&hip_raw_sock_output_v4, IPPROTO_HIP), 
-1, "raw sock output v4\n");
-       // Notice that hip_nat_sock_input should be initialized after 
hip_nat_sock_output
-       // because for the sockets bound to the same address/port, only the 
last socket seems
-       // to receive the packets. 
-#if 0
-       HIP_IFEL(hip_create_nat_sock_udp(&hip_nat_sock_output_udp, 0), -1, "raw 
sock output udp\n");
+    hip_xfrm_set_nl_ipsec(&hip_nl_ipsec);
+#endif
+
+#if 0
+    {
+        int ret_sockopt            = 0, value = 0;
+        socklen_t value_len        = sizeof(value);
+        int ipsec_buf_size         = 200000;
+        socklen_t ipsec_buf_sizeof = sizeof(ipsec_buf_size);
+        ret_sockopt    = getsockopt(hip_nl_ipsec.fd, SOL_SOCKET, SO_RCVBUF,
+                                    &value, &value_len);
+        if (ret_sockopt != 0) {
+            HIP_DEBUG("Getting receive buffer size of hip_nl_ipsec.fd 
failed\n");
+        }
+        ipsec_buf_size = value * 2;
+        HIP_DEBUG("Default setting of receive buffer size for hip_nl_ipsec was 
%d.\n"
+                  "Setting it to %d.\n", value, ipsec_buf_size);
+        ret_sockopt    = setsockopt(hip_nl_ipsec.fd, SOL_SOCKET, SO_RCVBUF,
+                                    &ipsec_buf_size, ipsec_buf_sizeof);
+        if (ret_sockopt != 0) {
+            HIP_DEBUG("Setting receive buffer size of hip_nl_ipsec.fd 
failed\n");
+        }
+        ret_sockopt    = 0;
+        ret_sockopt    = setsockopt(hip_nl_ipsec.fd, SOL_SOCKET, SO_SNDBUF,
+                                    &ipsec_buf_size, ipsec_buf_sizeof);
+        if (ret_sockopt != 0) {
+            HIP_DEBUG("Setting send buffer size of hip_nl_ipsec.fd failed\n");
+        }
+    }
+#endif
+
+    HIP_IFEL(hip_init_raw_sock_v6(&hip_raw_sock_output_v6, IPPROTO_HIP), -1, 
"raw sock output v6\n");
+    HIP_IFEL(hip_init_raw_sock_v4(&hip_raw_sock_output_v4, IPPROTO_HIP), -1, 
"raw sock output v4\n");
+    // Notice that hip_nat_sock_input should be initialized after 
hip_nat_sock_output
+    // because for the sockets bound to the same address/port, only the last 
socket seems
+    // to receive the packets.
+#if 0
+    HIP_IFEL(hip_create_nat_sock_udp(&hip_nat_sock_output_udp, 0), -1, "raw 
sock output udp\n");
 #else
-       HIP_IFEL(hip_init_raw_sock_v4(&hip_nat_sock_output_udp, IPPROTO_UDP), 
-1, "raw sock output udp\n");
+    HIP_IFEL(hip_init_raw_sock_v4(&hip_nat_sock_output_udp, IPPROTO_UDP), -1, 
"raw sock output udp\n");
 #endif
-       HIP_IFEL(hip_init_raw_sock_v6(&hip_raw_sock_input_v6, IPPROTO_HIP), -1, 
"raw sock input v6\n");
-       HIP_IFEL(hip_init_raw_sock_v4(&hip_raw_sock_input_v4, IPPROTO_HIP), -1, 
"raw sock input v4\n");
-       HIP_IFEL(hip_create_nat_sock_udp(&hip_nat_sock_input_udp, 0, 0), -1, 
"raw sock input udp\n");
-       HIP_IFEL(hip_init_icmp_v6(&hip_icmp_sock), -1, "icmpv6 sock\n");
-
-       HIP_DEBUG("hip_raw_sock_v6 input = %d\n", hip_raw_sock_input_v6);
-       HIP_DEBUG("hip_raw_sock_v6 output = %d\n", hip_raw_sock_output_v6);
-       HIP_DEBUG("hip_raw_sock_v4 input = %d\n", hip_raw_sock_input_v4);
-       HIP_DEBUG("hip_raw_sock_v4 output = %d\n", hip_raw_sock_output_v4);
-       HIP_DEBUG("hip_nat_sock_udp input = %d\n", hip_nat_sock_input_udp);
-       HIP_DEBUG("hip_nat_sock_udp output = %d\n", hip_nat_sock_output_udp);
-       HIP_DEBUG("hip_icmp_sock = %d\n", hip_icmp_sock);
-
-       if (flush_ipsec)
-       {
-               default_ipsec_func_set.hip_flush_all_sa();
-               default_ipsec_func_set.hip_flush_all_policy();
-       }
-
-       HIP_DEBUG("Setting SP\n");
-       default_ipsec_func_set.hip_delete_default_prefix_sp_pair();
-       HIP_IFE(default_ipsec_func_set.hip_setup_default_sp_prefix_pair(), -1);
-
-       HIP_DEBUG("Setting iface %s\n", HIP_HIT_DEV);
-       set_up_device(HIP_HIT_DEV, 0);
-       HIP_IFE(set_up_device(HIP_HIT_DEV, 1), 1);
-       HIP_DEBUG("Lowering MTU of dev " HIP_HIT_DEV " to %u\n", mtu_val);
-       sprintf(mtu, "%u", mtu_val);
-       strcpy(str, "ifconfig dummy0 mtu ");
-       strcat(str, mtu);
-       /* MTU is set using system call rather than in do_chflags to avoid
-        * chicken and egg problems in hipd start up. */
-       if ( system(str) == -1 ) {
-               HIP_ERROR("Exec %s failed", str);
-       }
-
-
-       HIP_IFE(hip_init_host_ids(), 1);
-
-       hip_user_sock = socket(AF_INET6, SOCK_DGRAM, 0);
-       HIP_IFEL((hip_user_sock < 0), 1,
-                "Could not create socket for user communication.\n");
-       bzero(&daemon_addr, sizeof(daemon_addr));
-       daemon_addr.sin6_family = AF_INET6;
-       daemon_addr.sin6_port = htons(HIP_DAEMON_LOCAL_PORT);
-       daemon_addr.sin6_addr = in6addr_loopback;
-       set_cloexec_flag(hip_user_sock, 1);
-
-       HIP_IFEL(bind(hip_user_sock, (struct sockaddr *)& daemon_addr,
-                     sizeof(daemon_addr)), -1,
-                "Bind on daemon addr failed\n");
-
-       hip_load_configuration();
+    HIP_IFEL(hip_init_raw_sock_v6(&hip_raw_sock_input_v6, IPPROTO_HIP), -1, 
"raw sock input v6\n");
+    HIP_IFEL(hip_init_raw_sock_v4(&hip_raw_sock_input_v4, IPPROTO_HIP), -1, 
"raw sock input v4\n");
+    HIP_IFEL(hip_create_nat_sock_udp(&hip_nat_sock_input_udp, 0, 0), -1, "raw 
sock input udp\n");
+    HIP_IFEL(hip_init_icmp_v6(&hip_icmp_sock), -1, "icmpv6 sock\n");
+
+    HIP_DEBUG("hip_raw_sock_v6 input = %d\n", hip_raw_sock_input_v6);
+    HIP_DEBUG("hip_raw_sock_v6 output = %d\n", hip_raw_sock_output_v6);
+    HIP_DEBUG("hip_raw_sock_v4 input = %d\n", hip_raw_sock_input_v4);
+    HIP_DEBUG("hip_raw_sock_v4 output = %d\n", hip_raw_sock_output_v4);
+    HIP_DEBUG("hip_nat_sock_udp input = %d\n", hip_nat_sock_input_udp);
+    HIP_DEBUG("hip_nat_sock_udp output = %d\n", hip_nat_sock_output_udp);
+    HIP_DEBUG("hip_icmp_sock = %d\n", hip_icmp_sock);
+
+    if (flush_ipsec) {
+        default_ipsec_func_set.hip_flush_all_sa();
+        default_ipsec_func_set.hip_flush_all_policy();
+    }
+
+    HIP_DEBUG("Setting SP\n");
+    default_ipsec_func_set.hip_delete_default_prefix_sp_pair();
+    HIP_IFE(default_ipsec_func_set.hip_setup_default_sp_prefix_pair(), -1);
+
+    HIP_DEBUG("Setting iface %s\n", HIP_HIT_DEV);
+    set_up_device(HIP_HIT_DEV, 0);
+    HIP_IFE(set_up_device(HIP_HIT_DEV, 1), 1);
+    HIP_DEBUG("Lowering MTU of dev " HIP_HIT_DEV " to %u\n", mtu_val);
+    sprintf(mtu, "%u", mtu_val);
+    strcpy(str, "ifconfig dummy0 mtu ");
+    strcat(str, mtu);
+    /* MTU is set using system call rather than in do_chflags to avoid
+     * chicken and egg problems in hipd start up. */
+    if (system(str) == -1) {
+        HIP_ERROR("Exec %s failed", str);
+    }
+
+
+    HIP_IFE(hip_init_host_ids(), 1);
+
+    hip_user_sock           = socket(AF_INET6, SOCK_DGRAM, 0);
+    HIP_IFEL((hip_user_sock < 0), 1,
+             "Could not create socket for user communication.\n");
+    bzero(&daemon_addr, sizeof(daemon_addr));
+    daemon_addr.sin6_family = AF_INET6;
+    daemon_addr.sin6_port   = htons(HIP_DAEMON_LOCAL_PORT);
+    daemon_addr.sin6_addr   = in6addr_loopback;
+    set_cloexec_flag(hip_user_sock, 1);
+
+    HIP_IFEL(bind(hip_user_sock, (struct sockaddr *) &daemon_addr,
+                  sizeof(daemon_addr)), -1,
+             "Bind on daemon addr failed\n");
+
+    hip_load_configuration();
 
 #ifdef CONFIG_HIP_HI3
-       if( hip_use_i3 ) {
-               hip_locator_status = SO_HIP_SET_LOCATOR_ON;
-       }
+    if (hip_use_i3) {
+        hip_locator_status = SO_HIP_SET_LOCATOR_ON;
+    }
 #endif
 
 #ifdef CONFIG_HIP_DHT
-       {
-               memset(opendht_host_name, 0, sizeof(opendht_host_name));
-
-               hip_opendht_sock_fqdn = 
init_dht_gateway_socket_gw(hip_opendht_sock_fqdn, opendht_serving_gateway);
-               set_cloexec_flag(hip_opendht_sock_fqdn, 1);
-               hip_opendht_sock_hit = 
init_dht_gateway_socket_gw(hip_opendht_sock_hit, opendht_serving_gateway);
-               set_cloexec_flag(hip_opendht_sock_hit, 1);
-       }
-#endif /* CONFIG_HIP_DHT */
-
-       certerr = 0;
-       certerr = hip_init_certs();
-       if (certerr < 0) HIP_DEBUG("Initializing cert configuration file 
returned error\n");
+    {
+        memset(opendht_host_name, 0, sizeof(opendht_host_name));
+
+        hip_opendht_sock_fqdn = 
init_dht_gateway_socket_gw(hip_opendht_sock_fqdn, opendht_serving_gateway);
+        set_cloexec_flag(hip_opendht_sock_fqdn, 1);
+        hip_opendht_sock_hit  = 
init_dht_gateway_socket_gw(hip_opendht_sock_hit, opendht_serving_gateway);
+        set_cloexec_flag(hip_opendht_sock_hit, 1);
+    }
+#endif  /* CONFIG_HIP_DHT */
+
+    certerr = 0;
+    certerr = hip_init_certs();
+    if (certerr < 0) {
+        HIP_DEBUG("Initializing cert configuration file returned error\n");
+    }
 
 #if 0
-       /* init new tcptimeout parameters, added by Tao Wan on 14.Jan.2008*/
+    /* init new tcptimeout parameters, added by Tao Wan on 14.Jan.2008*/
 
-       HIP_IFEL(set_new_tcptimeout_parameters_value(), -1,
-                       "set new tcptimeout parameters error\n");
+    HIP_IFEL(set_new_tcptimeout_parameters_value(), -1,
+             "set new tcptimeout parameters error\n");
 #endif
-       
-       hitdberr = 0;
+
+    hitdberr = 0;
 #ifdef CONFIG_HIP_AGENT
-       hitdberr = hip_init_daemon_hitdb();
-       if (hitdberr < 0) HIP_DEBUG("Initializing daemon hit database returned 
error\n");
-#endif /* CONFIG_HIP_AGENT */
+    hitdberr = hip_init_daemon_hitdb();
+    if (hitdberr < 0) {
+        HIP_DEBUG("Initializing daemon hit database returned error\n");
+    }
+#endif  /* CONFIG_HIP_AGENT */
 
-       /* Service initialization. */
-       hip_init_services();
+    /* Service initialization. */
+    hip_init_services();
 
 #ifdef CONFIG_HIP_RVS
-       HIP_INFO("Initializing HIP relay / RVS.\n");
-       hip_relay_init();
+    HIP_INFO("Initializing HIP relay / RVS.\n");
+    hip_relay_init();
 #endif
 
 #ifdef CONFIG_HIP_PRIVSEP
-       HIP_IFEL(hip_set_lowcapability(0), -1, "Failed to set capabilities\n");
+    HIP_IFEL(hip_set_lowcapability(0), -1, "Failed to set capabilities\n");
 #endif /* CONFIG_HIP_PRIVSEP */
 
 
 #ifdef CONFIG_HIP_HI3
-       if( hip_use_i3 )
-       {
+    if (hip_use_i3) {
 //             hip_get_default_hit(&peer_hit);
-               hip_i3_init(/*&peer_hit*/);
-       }
+        hip_i3_init(/*&peer_hit*/);
+    }
 #endif
 
-       hip_firewall_sock_lsi_fd = hip_user_sock;
+    hip_firewall_sock_lsi_fd = hip_user_sock;
 
-       if (hip_get_nsupdate_status())
-               nsupdate(1);
+    if (hip_get_nsupdate_status()) {
+        nsupdate(1);
+    }
 
 out_err:
-       return err;
+    return err;
 }
 
 /**
@@ -681,199 +698,203 @@
  */
 int hip_init_dht()
 {
-        int err = 0;
-        
+    int err         = 0;
+
 #ifdef CONFIG_HIP_DHT
-        int i = 0, j = 0, place = 0;
-        char serveraddr_str[INET6_ADDRSTRLEN];
-        char servername_str[HOST_NAME_MAX];
-        char servername_buf[HOST_NAME_MAX];
-        char port_buf[] = "00000";
-        int family;
-
-        HIP_IFEL((hip_opendht_inuse == SO_HIP_DHT_OFF), 0, "No DHT\n");
-
-       /* Init the opendht_queue */
-       HIP_IFEL((hip_init_dht_queue() == -1), -1, "Failed to initialize 
opendht queue\n");
-       
-       hip_opendht_error_count = 0;
-       /* Initializing variable for dht gateway port used in
-          resolve_dht_gateway_info in libhipopendht */
-
-       /* Needs to be init here, because of gateway change after
-         threshold error count*/
-       opendht_serving_gateway_port = OPENDHT_PORT;
-
-       memcpy(opendht_host_name, OPENDHT_GATEWAY, strlen(OPENDHT_GATEWAY)); 
-
-       /* Initialize the HDRR secret for OpenDHT put-rm.*/  
-       HIP_ASSERT(opendht_hdrr_secret != NULL);
-        memset(opendht_hdrr_secret, 0, 40);
-        err = RAND_bytes(opendht_hdrr_secret, 40);
-
-       memset(servername_str, 0, sizeof(servername_str));
-       memset(serveraddr_str, 0, sizeof(serveraddr_str));
-       memset(servername_buf, '\0', sizeof(servername_buf));
-       err = hip_get_random_hostname_id_from_hosts(OPENDHT_SERVERS_FILE,
-                                                   servername_buf, 
serveraddr_str);
-
-       for (i = 0; i < strlen(servername_buf); i++) {
-               if (servername_buf[i] == ':') break;
-               place++;
-       }
-       for (i = 0; i < place; i++) {
-               servername_str[i] = servername_buf[i];
-       }
-       if (place < strlen(servername_buf) - 1) {
-               place++;
-               for (i = 0, j = place; i < strlen(servername_buf); i++, j++) {
-                       port_buf[i] = servername_buf[j];
-               }
-               opendht_serving_gateway_port = atoi(port_buf);
-       }
-
-       HIP_IFEL(err, 0, "Failed to get random dht server\n");
-       HIP_DEBUG("DHT gateway from dhtservers:\n %s (addr = %s, port = %d)\n",
-                 servername_str, serveraddr_str, opendht_serving_gateway_port);
-
-       if (strchr(serveraddr_str, ':') == NULL)
-               family = AF_INET;
-       else
-               family = AF_INET6;
-
-       /* resolve it */
-       memset(opendht_host_name, '\0', sizeof(opendht_host_name));
-       memcpy(opendht_host_name, servername_str, strlen(servername_str));
-       err = resolve_dht_gateway_info(serveraddr_str,
-                                      &opendht_serving_gateway,
-                                      opendht_serving_gateway_port, family);  
-       if (err < 0) 
-       {
-               hip_opendht_error_count++;
-               HIP_DEBUG("Error resolving openDHT gateway!\n");
-       }
-       err = 0;
-
-       /* check the condition of the sockets, we may have come here in middle
-          of something so re-initializing might be needed */
-       if (hip_opendht_sock_fqdn > 0) {
-               close(hip_opendht_sock_fqdn);
-               hip_opendht_sock_fqdn = 
init_dht_gateway_socket_gw(hip_opendht_sock_fqdn, opendht_serving_gateway);
-               hip_opendht_fqdn_sent = STATE_OPENDHT_IDLE;
-       }
-       
-       if (hip_opendht_sock_hit > 0) {
-               close(hip_opendht_sock_hit);
-               hip_opendht_sock_hit = 
init_dht_gateway_socket_gw(hip_opendht_sock_hit, opendht_serving_gateway);
-               hip_opendht_hit_sent = STATE_OPENDHT_IDLE;
-       }
-
-       memset(opendht_name_mapping, '\0',
-              HIP_HOST_ID_HOSTNAME_LEN_MAX);
-       if (gethostname(opendht_name_mapping,
-                       HIP_HOST_ID_HOSTNAME_LEN_MAX))
-               HIP_DEBUG("gethostname failed\n");
-       hip_register_to_dht();
-       hip_init_dht_sockets(&hip_opendht_sock_fqdn, &hip_opendht_fqdn_sent); 
-       hip_init_dht_sockets(&hip_opendht_sock_hit, &hip_opendht_hit_sent);
+    int i           = 0, j = 0, place = 0;
+    char serveraddr_str[INET6_ADDRSTRLEN];
+    char servername_str[HOST_NAME_MAX];
+    char servername_buf[HOST_NAME_MAX];
+    char port_buf[] = "00000";
+    int family;
+
+    HIP_IFEL((hip_opendht_inuse == SO_HIP_DHT_OFF), 0, "No DHT\n");
+
+    /* Init the opendht_queue */
+    HIP_IFEL((hip_init_dht_queue() == -1), -1, "Failed to initialize opendht 
queue\n");
+
+    hip_opendht_error_count      = 0;
+    /* Initializing variable for dht gateway port used in
+     * resolve_dht_gateway_info in libhipopendht */
+
+    /* Needs to be init here, because of gateway change after
+     * threshold error count*/
+    opendht_serving_gateway_port = OPENDHT_PORT;
+
+    memcpy(opendht_host_name, OPENDHT_GATEWAY, strlen(OPENDHT_GATEWAY));
+
+    /* Initialize the HDRR secret for OpenDHT put-rm.*/
+    HIP_ASSERT(opendht_hdrr_secret != NULL);
+    memset(opendht_hdrr_secret, 0, 40);
+    err = RAND_bytes(opendht_hdrr_secret, 40);
+
+    memset(servername_str, 0, sizeof(servername_str));
+    memset(serveraddr_str, 0, sizeof(serveraddr_str));
+    memset(servername_buf, '\0', sizeof(servername_buf));
+    err = hip_get_random_hostname_id_from_hosts(OPENDHT_SERVERS_FILE,
+                                                servername_buf, 
serveraddr_str);
+
+    for (i = 0; i < strlen(servername_buf); i++) {
+        if (servername_buf[i] == ':') {
+            break;
+        }
+        place++;
+    }
+    for (i = 0; i < place; i++) {
+        servername_str[i] = servername_buf[i];
+    }
+    if (place < strlen(servername_buf) - 1) {
+        place++;
+        for (i = 0, j = place; i < strlen(servername_buf); i++, j++) {
+            port_buf[i] = servername_buf[j];
+        }
+        opendht_serving_gateway_port = atoi(port_buf);
+    }
+
+    HIP_IFEL(err, 0, "Failed to get random dht server\n");
+    HIP_DEBUG("DHT gateway from dhtservers:\n %s (addr = %s, port = %d)\n",
+              servername_str, serveraddr_str, opendht_serving_gateway_port);
+
+    if (strchr(serveraddr_str, ':') == NULL) {
+        family = AF_INET;
+    } else {
+        family = AF_INET6;
+    }
+
+    /* resolve it */
+    memset(opendht_host_name, '\0', sizeof(opendht_host_name));
+    memcpy(opendht_host_name, servername_str, strlen(servername_str));
+    err = resolve_dht_gateway_info(serveraddr_str,
+                                   &opendht_serving_gateway,
+                                   opendht_serving_gateway_port, family);
+    if (err < 0) {
+        hip_opendht_error_count++;
+        HIP_DEBUG("Error resolving openDHT gateway!\n");
+    }
+    err = 0;
+
+    /* check the condition of the sockets, we may have come here in middle
+     * of something so re-initializing might be needed */
+    if (hip_opendht_sock_fqdn > 0) {
+        close(hip_opendht_sock_fqdn);
+        hip_opendht_sock_fqdn = 
init_dht_gateway_socket_gw(hip_opendht_sock_fqdn,
+                                                           
opendht_serving_gateway);
+        hip_opendht_fqdn_sent = STATE_OPENDHT_IDLE;
+    }
+
+    if (hip_opendht_sock_hit > 0) {
+        close(hip_opendht_sock_hit);
+        hip_opendht_sock_hit = init_dht_gateway_socket_gw(hip_opendht_sock_hit,
+                                                          
opendht_serving_gateway);
+        hip_opendht_hit_sent = STATE_OPENDHT_IDLE;
+    }
+
+    memset(opendht_name_mapping, '\0', HIP_HOST_ID_HOSTNAME_LEN_MAX);
+    if (gethostname(opendht_name_mapping, HIP_HOST_ID_HOSTNAME_LEN_MAX)) {
+        HIP_DEBUG("gethostname failed\n");
+    }
+    hip_register_to_dht();
+    hip_init_dht_sockets(&hip_opendht_sock_fqdn, &hip_opendht_fqdn_sent);
+    hip_init_dht_sockets(&hip_opendht_sock_hit, &hip_opendht_hit_sent);
 
 /* out_err only used by opendht code */
 out_err:
-#endif /* CONFIG_HIP_DHT */
-       
-        return err;
+#endif  /* CONFIG_HIP_DHT */
+
+    return err;
 }
 
 /**
- * Init host IDs.
+ * Initialize host IDs.
  */
 static int hip_init_host_ids()
 {
-       int err = 0;
-       struct stat status;
-       struct hip_common *user_msg = NULL;
-       hip_hit_t default_hit;
-       hip_lsi_t default_lsi;
-
-       /* We are first serializing a message with HIs and then
-          deserializing it. This building and parsing causes
-          a minor overhead, but as a result we can reuse the code
-          with hipconf. */
-
-       HIP_IFE(!(user_msg = hip_msg_alloc()), -1);
-
-       /* Create default keys if necessary. */
-
-       if (stat(DEFAULT_CONFIG_DIR "/" DEFAULT_HOST_RSA_KEY_FILE_BASE 
DEFAULT_PUB_HI_FILE_NAME_SUFFIX, &status) && errno == ENOENT) {
-               //hip_msg_init(user_msg); already called by hip_msg_alloc()
-
-           HIP_IFEL(hip_serialize_host_id_action(user_msg, ACTION_NEW, 0, 1,
-                       NULL, NULL, RSA_KEY_DEFAULT_BITS, DSA_KEY_DEFAULT_BITS),
-                       1, "Failed to create keys to %s\n", DEFAULT_CONFIG_DIR);
-       }
-
-        /* Retrieve the keys to hipd */
-       /* Three steps because multiple large keys will not fit in the same 
message */
-
-       /* DSA keys and RSA anonymous are not loaded by default until bug id
-          522 is properly solved. Run hipconf add hi default if you want to
-          enable non-default HITs. */
+    int err                     = 0;
+    struct stat status;
+    struct hip_common *user_msg = NULL;
+    hip_hit_t default_hit;
+    hip_lsi_t default_lsi;
+
+    /* We are first serializing a message with HIs and then
+     * deserializing it. This building and parsing causes
+     * a minor overhead, but as a result we can reuse the code
+     * with hipconf. */
+
+    HIP_IFE(!(user_msg = hip_msg_alloc()), -1);
+
+    /* Create default keys if necessary. */
+
+    if (stat(DEFAULT_CONFIG_DIR "/" DEFAULT_HOST_RSA_KEY_FILE_BASE 
DEFAULT_PUB_HI_FILE_NAME_SUFFIX, &status) && errno == ENOENT) {
+        //hip_msg_init(user_msg); already called by hip_msg_alloc()
+
+        HIP_IFEL(hip_serialize_host_id_action(user_msg, ACTION_NEW, 0, 1,
+                                              NULL, NULL, 
RSA_KEY_DEFAULT_BITS, DSA_KEY_DEFAULT_BITS),
+                 1, "Failed to create keys to %s\n", DEFAULT_CONFIG_DIR);
+    }
+
+    /* Retrieve the keys to hipd */
+    /* Three steps because multiple large keys will not fit in the same 
message */
+
+    /* DSA keys and RSA anonymous are not loaded by default until bug id
+     * 522 is properly solved. Run hipconf add hi default if you want to
+     * enable non-default HITs. */
 #if 0
-       /* dsa anon and pub */
-       hip_msg_init(user_msg);
-       if (err = hip_serialize_host_id_action(user_msg, ACTION_ADD,
-                                               0, 1, "dsa", NULL, 0, 0)) {
-               HIP_ERROR("Could not load default keys (DSA)\n");
-               goto out_err;
-       }
-       if (err = hip_handle_add_local_hi(user_msg)) {
-               HIP_ERROR("Adding of keys failed (DSA)\n");
-               goto out_err;
-       }
+    /* dsa anon and pub */
+    hip_msg_init(user_msg);
+    if (err = hip_serialize_host_id_action(user_msg, ACTION_ADD,
+                                           0, 1, "dsa", NULL, 0, 0)) {
+        HIP_ERROR("Could not load default keys (DSA)\n");
+        goto out_err;
+    }
+    if (err = hip_handle_add_local_hi(user_msg)) {
+        HIP_ERROR("Adding of keys failed (DSA)\n");
+        goto out_err;
+    }
 
-       /* rsa anon */
-       hip_msg_init(user_msg);
-       if (err = hip_serialize_host_id_action(user_msg, ACTION_ADD,
-                                               1, 1, "rsa", NULL, 0, 0)) {
-               HIP_ERROR("Could not load default keys (RSA anon)\n");
-               goto out_err;
-       }
-       if (err = hip_handle_add_local_hi(user_msg)) {
-               HIP_ERROR("Adding of keys failed (RSA anon)\n");
-               goto out_err;
-       }
+    /* rsa anon */
+    hip_msg_init(user_msg);
+    if (err = hip_serialize_host_id_action(user_msg, ACTION_ADD,
+                                           1, 1, "rsa", NULL, 0, 0)) {
+        HIP_ERROR("Could not load default keys (RSA anon)\n");
+        goto out_err;
+    }
+    if (err = hip_handle_add_local_hi(user_msg)) {
+        HIP_ERROR("Adding of keys failed (RSA anon)\n");
+        goto out_err;
+    }
 #endif
 
-       /* rsa pub */
-       hip_msg_init(user_msg);
-       if ((err = hip_serialize_host_id_action(user_msg, ACTION_ADD,
-                                               0, 1, "rsa", NULL, 0, 0))) {
-               HIP_ERROR("Could not load default keys (RSA pub)\n");
-               goto out_err;
-       }
-
-       if ((err = hip_handle_add_local_hi(user_msg))) {
-               HIP_ERROR("Adding of keys failed (RSA pub)\n");
-               goto out_err;
-       }
-
-       HIP_DEBUG("Keys added\n");
-       hip_get_default_hit(&default_hit);
-       hip_get_default_lsi(&default_lsi);
-
-       HIP_DEBUG_HIT("default_hit ", &default_hit);
-       HIP_DEBUG_LSI("default_lsi ", &default_lsi);
-       hip_hidb_associate_default_hit_lsi(&default_hit, &default_lsi);
-
-       /*Initializes the hadb with the information contained in 
/etc/hip/hosts*/
-       //hip_init_hadb_hip_host();
-
- out_err:
-
-       if (user_msg)
-               HIP_FREE(user_msg);
-
-       return err;
+    /* rsa pub */
+    hip_msg_init(user_msg);
+    if ((err = hip_serialize_host_id_action(user_msg, ACTION_ADD,
+                                            0, 1, "rsa", NULL, 0, 0))) {
+        HIP_ERROR("Could not load default keys (RSA pub)\n");
+        goto out_err;
+    }
+
+    if ((err = hip_handle_add_local_hi(user_msg))) {
+        HIP_ERROR("Adding of keys failed (RSA pub)\n");
+        goto out_err;
+    }
+
+    HIP_DEBUG("Keys added\n");
+    hip_get_default_hit(&default_hit);
+    hip_get_default_lsi(&default_lsi);
+
+    HIP_DEBUG_HIT("default_hit ", &default_hit);
+    HIP_DEBUG_LSI("default_lsi ", &default_lsi);
+    hip_hidb_associate_default_hit_lsi(&default_hit, &default_lsi);
+
+    /*Initializes the hadb with the information contained in /etc/hip/hosts*/
+    //hip_init_hadb_hip_host();
+
+out_err:
+
+    if (user_msg) {
+        HIP_FREE(user_msg);
+    }
+
+    return err;
 }
 
 /**
@@ -881,96 +902,92 @@
  */
 static int hip_init_raw_sock_v6(int *hip_raw_sock_v6, int proto)
 {
-       int on = 1, off = 0, err = 0;
-
-       *hip_raw_sock_v6 = socket(AF_INET6, SOCK_RAW, proto);
-       set_cloexec_flag(*hip_raw_sock_v6, 1);
-       HIP_IFEL(*hip_raw_sock_v6 <= 0, 1, "Raw socket creation failed. Not 
root?\n");
-
-       /* see bug id 212 why RECV_ERR is off */
-       err = setsockopt(*hip_raw_sock_v6, IPPROTO_IPV6, IPV6_RECVERR, &off, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt recverr failed\n");
-       err = setsockopt(*hip_raw_sock_v6, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt pktinfo failed\n");
-       err = setsockopt(*hip_raw_sock_v6, SOL_SOCKET, SO_REUSEADDR, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt v6 reuseaddr failed\n");
-
- out_err:
-       return err;
+    int on = 1, off = 0, err = 0;
+
+    *hip_raw_sock_v6 = socket(AF_INET6, SOCK_RAW, proto);
+    set_cloexec_flag(*hip_raw_sock_v6, 1);
+    HIP_IFEL(*hip_raw_sock_v6 <= 0, 1, "Raw socket creation failed. Not 
root?\n");
+
+    /* see bug id 212 why RECV_ERR is off */
+    err = setsockopt(*hip_raw_sock_v6, IPPROTO_IPV6, IPV6_RECVERR, &off, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt recverr failed\n");
+    err = setsockopt(*hip_raw_sock_v6, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt pktinfo failed\n");
+    err = setsockopt(*hip_raw_sock_v6, SOL_SOCKET, SO_REUSEADDR, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt v6 reuseaddr failed\n");
+
+out_err:
+    return err;
 }
 
-int hip_create_nat_sock_udp(int *hip_nat_sock_udp, 
-                           struct sockaddr_in* addr,
-                           int is_output)
+int hip_create_nat_sock_udp(int *hip_nat_sock_udp,
+                            struct sockaddr_in *addr,
+                            int is_output)
 {
-       int on = 1, err = 0;
-       int off = 0;
-       struct sockaddr_in myaddr;
-       int type, protocol;
-
-       if (is_output) {
-               type = SOCK_RAW;
-               protocol = IPPROTO_UDP;
-       } else {
-               type = SOCK_DGRAM;
-               protocol = 0;
-       }
-       
-       HIP_DEBUG("\n");
-       
-       if ((*hip_nat_sock_udp = socket(AF_INET, type, protocol))<0)
-       {
-               HIP_ERROR("Can not open socket for UDP\n");
-               return -1;
-       }
-       set_cloexec_flag(*hip_nat_sock_udp, 1);
-       err = setsockopt(*hip_nat_sock_udp, IPPROTO_IP, IP_PKTINFO, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt udp pktinfo failed\n");
-       /* see bug id 212 why RECV_ERR is off */
-       err = setsockopt(*hip_nat_sock_udp, IPPROTO_IP, IP_RECVERR, &off, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt udp recverr failed\n");
-       #ifndef CONFIG_HIP_OPENWRT
-       if (!is_output){
-               int encap_on = HIP_UDP_ENCAP_ESPINUDP;
-               err = setsockopt(*hip_nat_sock_udp, SOL_UDP, HIP_UDP_ENCAP, 
&encap_on, sizeof(encap_on));
-       }
-       HIP_IFEL(err, -1, "setsockopt udp encap failed\n");
-       #endif
-       err = setsockopt(*hip_nat_sock_udp, SOL_SOCKET, SO_REUSEADDR, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt udp reuseaddr failed\n");
-       err = setsockopt(*hip_nat_sock_udp, SOL_SOCKET, SO_BROADCAST, &on, 
sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt udp reuseaddr failed\n");
-       
-       if (is_output)
-               err = setsockopt(*hip_nat_sock_udp, IPPROTO_IP, IP_HDRINCL, 
(char *)&on, sizeof(on));
-       HIP_IFEL(err, -1, "setsockopt hdr include failed\n");
-
-        if (addr)
-        {
-            memcpy(&myaddr, addr, sizeof(struct sockaddr_in));
-        }
-        else
-        {
-            myaddr.sin_family=AF_INET;
-            /** @todo Change this inaddr_any -- Abi */
-            myaddr.sin_addr.s_addr = INADDR_ANY;
-
-            myaddr.sin_port=htons(hip_get_local_nat_udp_port());
-        }
-       
-       err = bind(*hip_nat_sock_udp, (struct sockaddr *)&myaddr, 
sizeof(myaddr));
-       if (err < 0)
-       {
-               HIP_PERROR("Unable to bind udp socket to port\n");
-               err = -1;
-               goto out_err;
-       }
-       
-       HIP_DEBUG_INADDR("UDP socket created and bound to addr", (struct 
in_addr *) &myaddr.sin_addr.s_addr);
-       //return 0;
-       
+    int on  = 1, err = 0;
+    int off = 0;
+    struct sockaddr_in myaddr;
+    int type, protocol;
+
+    if (is_output) {
+        type     = SOCK_RAW;
+        protocol = IPPROTO_UDP;
+    } else {
+        type     = SOCK_DGRAM;
+        protocol = 0;
+    }
+
+    HIP_DEBUG("\n");
+
+    if ((*hip_nat_sock_udp = socket(AF_INET, type, protocol)) < 0) {
+        HIP_ERROR("Can not open socket for UDP\n");
+        return -1;
+    }
+    set_cloexec_flag(*hip_nat_sock_udp, 1);
+    err = setsockopt(*hip_nat_sock_udp, IPPROTO_IP, IP_PKTINFO, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt udp pktinfo failed\n");
+    /* see bug id 212 why RECV_ERR is off */
+    err = setsockopt(*hip_nat_sock_udp, IPPROTO_IP, IP_RECVERR, &off, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt udp recverr failed\n");
+        #ifndef CONFIG_HIP_OPENWRT
+    if (!is_output) {
+        int encap_on = HIP_UDP_ENCAP_ESPINUDP;
+        err = setsockopt(*hip_nat_sock_udp, SOL_UDP, HIP_UDP_ENCAP, &encap_on, 
sizeof(encap_on));
+    }
+    HIP_IFEL(err, -1, "setsockopt udp encap failed\n");
+        #endif
+    err = setsockopt(*hip_nat_sock_udp, SOL_SOCKET, SO_REUSEADDR, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt udp reuseaddr failed\n");
+    err = setsockopt(*hip_nat_sock_udp, SOL_SOCKET, SO_BROADCAST, &on, 
sizeof(on));
+    HIP_IFEL(err, -1, "setsockopt udp reuseaddr failed\n");
+
+    if (is_output) {
+        err = setsockopt(*hip_nat_sock_udp, IPPROTO_IP, IP_HDRINCL, (char *) 
&on, sizeof(on));
+    }
+    HIP_IFEL(err, -1, "setsockopt hdr include failed\n");
+
+    if (addr) {
+        memcpy(&myaddr, addr, sizeof(struct sockaddr_in));
+    } else {
+        myaddr.sin_family      = AF_INET;
+        /** @todo Change this inaddr_any -- Abi */
+        myaddr.sin_addr.s_addr = INADDR_ANY;
+
+        myaddr.sin_port        = htons(hip_get_local_nat_udp_port());
+    }
+
+    err = bind(*hip_nat_sock_udp, (struct sockaddr *) &myaddr, sizeof(myaddr));
+    if (err < 0) {
+        HIP_PERROR("Unable to bind udp socket to port\n");
+        err = -1;
+        goto out_err;
+    }
+
+    HIP_DEBUG_INADDR("UDP socket created and bound to addr", (struct in_addr 
*) &myaddr.sin_addr.s_addr);
+    //return 0;
+
 out_err:
-       return err;
+    return err;
 }
 
 /**
@@ -978,23 +995,23 @@
  */
 void hip_close(int signal)
 {
-       static int terminate = 0;
-
-       HIP_ERROR("Signal: %d\n", signal);
-       terminate++;
-
-       /* Close SAs with all peers */
-       if (terminate == 1) {
-         hip_send_close(NULL, FLUSH_HA_INFO_DB);
-               hipd_set_state(HIPD_STATE_CLOSING);
-               HIP_DEBUG("Starting to close HIP daemon...\n");
-       } else if (terminate == 2) {
-               HIP_DEBUG("Send still once this signal to force daemon 
exit...\n");
-       } else if (terminate > 2) {
-               HIP_DEBUG("Terminating daemon.\n");
-               hip_exit(signal);
-               exit(signal);
-       }
+    static int terminate = 0;
+
+    HIP_ERROR("Signal: %d\n", signal);
+    terminate++;
+
+    /* Close SAs with all peers */
+    if (terminate == 1) {
+        hip_send_close(NULL, FLUSH_HA_INFO_DB);
+        hipd_set_state(HIPD_STATE_CLOSING);
+        HIP_DEBUG("Starting to close HIP daemon...\n");
+    } else if (terminate == 2) {
+        HIP_DEBUG("Send still once this signal to force daemon exit...\n");
+    } else if (terminate > 2) {
+        HIP_DEBUG("Terminating daemon.\n");
+        hip_exit(signal);
+        exit(signal);
+    }
 }
 
 /**
@@ -1003,248 +1020,256 @@
  */
 void hip_exit(int signal)
 {
-       struct hip_common *msg = NULL;
-       HIP_ERROR("Signal: %d\n", signal);
+    struct hip_common *msg = NULL;
+    HIP_ERROR("Signal: %d\n", signal);
 
-       default_ipsec_func_set.hip_delete_default_prefix_sp_pair();
-       /* Close SAs with all peers */
-        // hip_send_close(NULL);
+    default_ipsec_func_set.hip_delete_default_prefix_sp_pair();
+    /* Close SAs with all peers */
+    // hip_send_close(NULL);
 
 #if 0
-       /*reset TCP timeout to be original vaule , added By Tao Wan on 
14.Jan.2008. */
-       reset_default_tcptimeout_parameters_value();
+    /*reset TCP timeout to be original vaule , added By Tao Wan on 
14.Jan.2008. */
+    reset_default_tcptimeout_parameters_value();
 #endif
-       if (hipd_msg)
-               HIP_FREE(hipd_msg);
-        if (hipd_msg_v4)
-               HIP_FREE(hipd_msg_v4);
-
-       hip_delete_all_sp();//empty
-
-       delete_all_addresses();
-
-       set_up_device(HIP_HIT_DEV, 0);
-
-       /* Next line is needed only if RVS or hiprelay is in use. */
-       hip_uninit_services();
+    if (hipd_msg) {
+        HIP_FREE(hipd_msg);
+    }
+    if (hipd_msg_v4) {
+        HIP_FREE(hipd_msg_v4);
+    }
+
+    hip_delete_all_sp();    //empty
+
+    delete_all_addresses();
+
+    set_up_device(HIP_HIT_DEV, 0);
+
+    /* Next line is needed only if RVS or hiprelay is in use. */
+    hip_uninit_services();
 
 #ifdef CONFIG_HIP_OPPORTUNISTIC
-       hip_oppdb_uninit();
+    hip_oppdb_uninit();
 #endif
 
 #ifdef CONFIG_HIP_I3
-       hip_hi3_clean();
+    hip_hi3_clean();
 #endif
 
 #ifdef CONFIG_HIP_RVS
-       HIP_INFO("Uninitializing RVS / HIP relay database and whitelist.\n");
-       hip_relay_uninit();
+    HIP_INFO("Uninitializing RVS / HIP relay database and whitelist.\n");
+    hip_relay_uninit();
 #endif
 
-       if (hip_raw_sock_input_v6){
-               HIP_INFO("hip_raw_sock_input_v6\n");
-               close(hip_raw_sock_input_v6);
-       }
-       
-       if (hip_raw_sock_output_v6){
-               HIP_INFO("hip_raw_sock_output_v6\n");
-               close(hip_raw_sock_output_v6);
-       }
-
-       if (hip_raw_sock_input_v4){
-               HIP_INFO("hip_raw_sock_input_v4\n");
-               close(hip_raw_sock_input_v4);
-       }
-
-       if (hip_raw_sock_output_v4){
-               HIP_INFO("hip_raw_sock_output_v4\n");
-               close(hip_raw_sock_output_v4);
-       }
-       
-       if (hip_nat_sock_input_udp){
-               HIP_INFO("hip_nat_sock_input_udp\n");
-               close(hip_nat_sock_input_udp);
-       }
-
-       if (hip_nat_sock_output_udp){
-               HIP_INFO("hip_nat_sock_output_udp\n");
-               close(hip_nat_sock_output_udp);
-       }
-
-       if (hip_nat_sock_input_udp_v6){
-               HIP_INFO("hip_nat_sock_input_udp_v6\n");
-               close(hip_nat_sock_input_udp_v6);
-       }
-
-       if (hip_nat_sock_output_udp_v6){
-               HIP_INFO("hip_nat_sock_output_udp_v6\n");
-               close(hip_nat_sock_output_udp_v6);
-       }
-       
-       if (hip_user_sock){
-               HIP_INFO("hip_user_sock\n");
-               close(hip_user_sock);
-       }
-       if (hip_nl_ipsec.fd){
-               HIP_INFO("hip_nl_ipsec.fd\n");
-               rtnl_close(&hip_nl_ipsec);
-       }
-       if (hip_nl_route.fd){
-               HIP_INFO("hip_nl_route.fd\n");
-               rtnl_close(&hip_nl_route);
-       }
-
-       hip_uninit_hadb();
-       hip_uninit_host_id_dbs();
-
-       msg = hip_msg_alloc();
-       if (msg)
-       {
-               hip_build_user_hdr(msg, SO_HIP_DAEMON_QUIT, 0);
-               hip_send_agent(msg);
-               free(msg);
-       }
-
-       hip_remove_lock_file(HIP_DAEMON_LOCK_FILE);
-
-       if (opendht_serving_gateway)
-               freeaddrinfo(opendht_serving_gateway);
-
-       if (opendht_current_hdrr)
-               free(opendht_current_hdrr);
+    if (hip_raw_sock_input_v6) {
+        HIP_INFO("hip_raw_sock_input_v6\n");
+        close(hip_raw_sock_input_v6);
+    }
+
+    if (hip_raw_sock_output_v6) {
+        HIP_INFO("hip_raw_sock_output_v6\n");
+        close(hip_raw_sock_output_v6);
+    }
+
+    if (hip_raw_sock_input_v4) {
+        HIP_INFO("hip_raw_sock_input_v4\n");
+        close(hip_raw_sock_input_v4);
+    }
+
+    if (hip_raw_sock_output_v4) {
+        HIP_INFO("hip_raw_sock_output_v4\n");
+        close(hip_raw_sock_output_v4);
+    }
+
+    if (hip_nat_sock_input_udp) {
+        HIP_INFO("hip_nat_sock_input_udp\n");
+        close(hip_nat_sock_input_udp);
+    }
+
+    if (hip_nat_sock_output_udp) {
+        HIP_INFO("hip_nat_sock_output_udp\n");
+        close(hip_nat_sock_output_udp);
+    }
+
+    if (hip_nat_sock_input_udp_v6) {
+        HIP_INFO("hip_nat_sock_input_udp_v6\n");
+        close(hip_nat_sock_input_udp_v6);
+    }
+
+    if (hip_nat_sock_output_udp_v6) {
+        HIP_INFO("hip_nat_sock_output_udp_v6\n");
+        close(hip_nat_sock_output_udp_v6);
+    }
+
+    if (hip_user_sock) {
+        HIP_INFO("hip_user_sock\n");
+        close(hip_user_sock);
+    }
+    if (hip_nl_ipsec.fd) {
+        HIP_INFO("hip_nl_ipsec.fd\n");
+        rtnl_close(&hip_nl_ipsec);
+    }
+    if (hip_nl_route.fd) {
+        HIP_INFO("hip_nl_route.fd\n");
+        rtnl_close(&hip_nl_route);
+    }
+
+    hip_uninit_hadb();
+    hip_uninit_host_id_dbs();
+
+    msg = hip_msg_alloc();
+    if (msg) {
+        hip_build_user_hdr(msg, SO_HIP_DAEMON_QUIT, 0);
+        hip_send_agent(msg);
+        free(msg);
+    }
+
+    hip_remove_lock_file(HIP_DAEMON_LOCK_FILE);
+
+    if (opendht_serving_gateway) {
+        freeaddrinfo(opendht_serving_gateway);
+    }
+
+    if (opendht_current_hdrr) {
+        free(opendht_current_hdrr);
+    }
 
 #ifdef CONFIG_HIP_PERFORMANCE
-       /* Deallocate memory of perf_set after finishing all of tests */
-       hip_perf_destroy(perf_set);
+    /* Deallocate memory of perf_set after finishing all of tests */
+    hip_perf_destroy(perf_set);
 #endif
 
 #ifdef CONFIG_HIP_AGENT
-       if (sqlite3_close(daemon_db))
-               HIP_ERROR("Error closing database: %s\n", 
sqlite3_errmsg(daemon_db));
+    if (sqlite3_close(daemon_db)) {
+        HIP_ERROR("Error closing database: %s\n", sqlite3_errmsg(daemon_db));
+    }
 #endif
 
-       hip_dh_uninit();
-
-       hip_dht_queue_uninit();
-
-       return;
+    hip_dh_uninit();
+
+    hip_dht_queue_uninit();
+
+    return;
 }
 
 /**
- * Initalize random seed.
+ * Initialize random seed.
  */
 static int init_random_seed()
 {
-       struct timeval tv;
-       struct timezone tz;
-       struct {
-               struct timeval tv;
-               pid_t pid;
-               long int rand;
-       } rand_data;
-       int err = 0;
-
-       err = gettimeofday(&tv, &tz);
-       srandom(tv.tv_usec);
-
-       memcpy(&rand_data.tv, &tv, sizeof(tv));
-       rand_data.pid = getpid();
-       rand_data.rand = random();
-
-       RAND_seed(&rand_data, sizeof(rand_data));
-
-       return err;
-}
-
-
-static int hip_init_certs(void) {
-       int err = 0;
-       char hit[41];
-       FILE * conf_file;
-       struct hip_host_id_entry * entry;
-       char hostname[HIP_HOST_ID_HOSTNAME_LEN_MAX];
-
-       memset(hostname, 0, HIP_HOST_ID_HOSTNAME_LEN_MAX);
-       HIP_IFEL(gethostname(hostname, HIP_HOST_ID_HOSTNAME_LEN_MAX - 1), -1,
-                "gethostname failed\n");
-
-       conf_file = fopen(HIP_CERT_CONF_PATH, "r");
-       if (!conf_file) {
-               HIP_DEBUG("Configuration file did NOT exist creating it and "
-                         "filling it with default information\n");
-               HIP_IFEL(!memset(hit, '\0', sizeof(hit)), -1,
-                         "Failed to memset memory for hit presentation 
format\n");
-               /* Fetch the first RSA HIT */
-               entry = hip_return_first_rsa();
-               if (entry == NULL) {
-                       HIP_DEBUG("Failed to get the first RSA HI");
-                       goto out_err;
-               }
-               hip_in6_ntop(&entry->lhi.hit, hit);
-               conf_file = fopen(HIP_CERT_CONF_PATH, "w+");
-               fprintf(conf_file,
-                       "# Section containing SPKI related information\n"
-                       "#\n"
-                       "# issuerhit = what hit is to be used when signing\n"
-                       "# days = how long is this key valid\n"
-                       "\n"
-                       "[ hip_spki ]\n"
-                       "issuerhit = %s\n"
-                       "days = %d\n"
-                       "\n"
-                       "# Section containing HIP related information\n"
-                       "#\n"
-                       "# issuerhit = what hit is to be used when signing\n"
-                       "# days = how long is this key valid\n"
-                       "\n"
-                       "[ hip_x509v3 ]\n"
-                       "issuerhit = %s\n"
-                       "days = %d\n"
-                       "\n"
-                       "#Section containing the name section for the x509v3 
issuer name"
-                       "\n"
-                       "[ hip_x509v3_name ]\n"
-                       "issuerhit = %s\n"
-                        "\n"
-                        "# Uncomment this section to add x509 extensions\n"
-                        "# to the certificate\n"
-                        "#\n"
-                        "# DO NOT use subjectAltName, issuerAltName or\n"
-                        "# basicConstraints implementation uses them already\n"
-                        "# All other extensions are allowed\n"
-                        "\n"
-                        "# [ hip_x509v3_extensions ]\n",
-                       hit, HIP_CERT_INIT_DAYS,
-                        hit, HIP_CERT_INIT_DAYS,
-                       hit /* TODO SAMU: removed because not used:*/  /*, 
hostname*/);
-               fclose(conf_file);
-       } else {
-               HIP_DEBUG("Configuration file existed exiting 
hip_init_certs\n");
-       }
-out_err:
-       return err;
-}
-
-static struct hip_host_id_entry * hip_return_first_rsa(void) {
-       hip_list_t *curr, *iter;
-       struct hip_host_id_entry *tmp = NULL;
-       int c;
-       uint16_t algo = 0;
-
-       HIP_READ_LOCK_DB(hip_local_hostid_db);
-
-       list_for_each_safe(curr, iter, hip_local_hostid_db, c) {
-               tmp = (struct hip_host_id_entry *)list_entry(curr);
-               HIP_DEBUG_HIT("Found HIT", &tmp->lhi.hit);
-               algo = hip_get_host_id_algo(tmp->host_id);
-               HIP_DEBUG("hits algo %d HIP_HI_RSA = %d\n",
-                         algo, HIP_HI_RSA);
-               if (algo == HIP_HI_RSA) goto out_err;
-       }
-
-out_err:
-       HIP_READ_UNLOCK_DB(hip_local_hostid_db);
-       if (algo == HIP_HI_RSA) return (tmp);
-       return NULL;
-}
-
+    struct timeval tv;
+    struct timezone tz;
+    struct {
+        struct timeval tv;
+        pid_t          pid;
+        long int       rand;
+    } rand_data;
+    int err = 0;
+
+    err            = gettimeofday(&tv, &tz);
+    srandom(tv.tv_usec);
+
+    memcpy(&rand_data.tv, &tv, sizeof(tv));
+    rand_data.pid  = getpid();
+    rand_data.rand = random();
+
+    RAND_seed(&rand_data, sizeof(rand_data));
+
+    return err;
+}
+
+static int hip_init_certs(void)
+{
+    int err = 0;
+    char hit[41];
+    FILE *conf_file;
+    struct hip_host_id_entry *entry;
+    char hostname[HIP_HOST_ID_HOSTNAME_LEN_MAX];
+
+    memset(hostname, 0, HIP_HOST_ID_HOSTNAME_LEN_MAX);
+    HIP_IFEL(gethostname(hostname, HIP_HOST_ID_HOSTNAME_LEN_MAX - 1), -1,
+             "gethostname failed\n");
+
+    conf_file = fopen(HIP_CERT_CONF_PATH, "r");
+    if (!conf_file) {
+        HIP_DEBUG("Configuration file did NOT exist creating it and "
+                  "filling it with default information\n");
+        HIP_IFEL(!memset(hit, '\0', sizeof(hit)), -1,
+                 "Failed to memset memory for hit presentation format\n");
+        /* Fetch the first RSA HIT */
+        entry = hip_return_first_rsa();
+        if (entry == NULL) {
+            HIP_DEBUG("Failed to get the first RSA HI");
+            goto out_err;
+        }
+        hip_in6_ntop(&entry->lhi.hit, hit);
+        conf_file = fopen(HIP_CERT_CONF_PATH, "w+");
+        fprintf(conf_file,
+                "# Section containing SPKI related information\n"
+                "#\n"
+                "# issuerhit = what hit is to be used when signing\n"
+                "# days = how long is this key valid\n"
+                "\n"
+                "[ hip_spki ]\n"
+                "issuerhit = %s\n"
+                "days = %d\n"
+                "\n"
+                "# Section containing HIP related information\n"
+                "#\n"
+                "# issuerhit = what hit is to be used when signing\n"
+                "# days = how long is this key valid\n"
+                "\n"
+                "[ hip_x509v3 ]\n"
+                "issuerhit = %s\n"
+                "days = %d\n"
+                "\n"
+                "#Section containing the name section for the x509v3 issuer 
name"
+                "\n"
+                "[ hip_x509v3_name ]\n"
+                "issuerhit = %s\n"
+                "\n"
+                "# Uncomment this section to add x509 extensions\n"
+                "# to the certificate\n"
+                "#\n"
+                "# DO NOT use subjectAltName, issuerAltName or\n"
+                "# basicConstraints implementation uses them already\n"
+                "# All other extensions are allowed\n"
+                "\n"
+                "# [ hip_x509v3_extensions ]\n",
+                hit, HIP_CERT_INIT_DAYS,
+                hit, HIP_CERT_INIT_DAYS,
+                hit /* TODO SAMU: removed because not used:*/  /*, hostname*/);
+        fclose(conf_file);
+    } else {
+        HIP_DEBUG("Configuration file existed exiting hip_init_certs\n");
+    }
+out_err:
+    return err;
+}
+
+static struct hip_host_id_entry *hip_return_first_rsa(void)
+{
+    hip_list_t *curr, *iter;
+    struct hip_host_id_entry *tmp = NULL;
+    int c;
+    uint16_t algo                 = 0;
+
+    HIP_READ_LOCK_DB(hip_local_hostid_db);
+
+    list_for_each_safe(curr, iter, hip_local_hostid_db, c) {
+        tmp  = (struct hip_host_id_entry *) list_entry(curr);
+        HIP_DEBUG_HIT("Found HIT", &tmp->lhi.hit);
+        algo = hip_get_host_id_algo(tmp->host_id);
+        HIP_DEBUG("hits algo %d HIP_HI_RSA = %d\n",
+                  algo, HIP_HI_RSA);
+        if (algo == HIP_HI_RSA) {
+            goto out_err;
+        }
+    }
+
+out_err:
+    HIP_READ_UNLOCK_DB(hip_local_hostid_db);
+    if (algo == HIP_HI_RSA) {
+        return tmp;
+    }
+    return NULL;
+}

=== modified file 'hipd/init.c.doxyme'
--- hipd/init.c.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/init.c.doxyme  2010-02-10 22:32:46 +0000
@@ -14,20 +14,20 @@
 
 
 
- ///  Single line comment for dOxygen.
+///  Single line comment for dOxygen.
 
 /**
  * my_great_function
  *
  * Write description of function here.
  * The function should follow these comments.
- * 
+ *
  * Document the use of each function variable and indicate if this
  * argument is used to return information to the calling context. Use
- * [out] for outut parameters (This is very important). Align the 
+ * [out] for outut parameters (This is very important). Align the
  * descriptions to improve readability.
  *
- *  @note: put important usage informations to notes. 
+ *  @note: put important usage informations to notes.
  *
  *  @param      firstArg     Description of first function argument.
  *  @param[out] secondArg    Description of second function argument.
@@ -35,7 +35,8 @@
  *  @return Description of returned value.
  **/
 
-int my_great_function(int firstArg, char** secondArg, int thirdArg){
+int my_great_function(int firstArg, char **secondArg, int thirdArg)
+{
     .....
 
 /// Now it's your turn.
@@ -44,116 +45,115 @@
 
 
 /**
- * hip_close 
+ * hip_close
  *
  *
  * @param signal
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_create_file_unless_exists 
+ * hip_create_file_unless_exists
  *
  *
  * @param path
  * @param contents
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_create_nat_sock_udp 
+ * hip_create_nat_sock_udp
  *
  *
  * @param hip_nat_sock_udp
  * @param addr
  * @param is_output
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_exit 
+ * hip_exit
  *
  *
  * @param signal
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_certs 
+ * hip_init_certs
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_icmp_v6 
+ * hip_init_icmp_v6
  *
  *
  * @param icmpsockfd
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_raw_sock_v4 
+ * hip_init_raw_sock_v4
  *
  *
  * @param hip_raw_sock_v4
  * @param proto
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_init_raw_sock_v6 
+ * hip_init_raw_sock_v6
  *
  *
  * @param hip_raw_sock_v6
  * @param proto
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_return_first_rsa 
+ * hip_return_first_rsa
  *
  *
  * @param void
- * @return 
+ * @return
  **/
 
 
 /**
- * hip_sig_chld 
+ * hip_sig_chld
  *
  *
  * @param signum
- * @return 
+ * @return
  **/
 
 
 /**
- * hipd_init 
+ * hipd_init
  *
  *
  * @param flush_ipsec
  * @param killold
- * @return 
+ * @return
  **/
 
 
 /**
- * set_cloexec_flag 
+ * set_cloexec_flag
  *
  *
  * @param desc
  * @param value
- * @return 
+ * @return
  **/
-

=== modified file 'hipd/init.h'
--- hipd/init.h 2010-02-08 15:18:06 +0000
+++ hipd/init.h 2010-02-10 22:32:46 +0000
@@ -1,7 +1,7 @@
 #ifndef _HIPD_INIT
 #define _HIPD_INIT
 #include <sys/types.h>
-#include <sys/stat.h> 
+#include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/resource.h>
@@ -43,14 +43,13 @@
  * @param sockaddr_in           the address that will be used to create the
  *      socket. If NULL is passed, INADDR_ANY is used.
  * @param  is_output           1 if the socket is for output, otherwise 0
- * 
+ *
  * @return zero on success, negative error value on error.
  */
-int hip_create_nat_sock_udp(int *hip_nat_sock_udp, 
-       struct sockaddr_in* addr,
-       int is_output);
+int hip_create_nat_sock_udp(int *hip_nat_sock_udp,
+                            struct sockaddr_in *addr,
+                            int is_output);
 void hip_close(int signal);
 void hip_exit(int signal);
 int hip_init_dht(void);
 #endif /* _HIP_INIT */
-

=== modified file 'hipd/init.h.doxyme'
--- hipd/init.h.doxyme  2009-12-12 10:44:54 +0000
+++ hipd/init.h.doxyme  2010-02-10 22:32:46 +0000
@@ -1,8 +1,8 @@
 /**
  * @file ./hipd/init.h
- * 
+ *
  *  <LICENSE TEMLPATE LINE - LEAVE THIS LINE INTACT>
- * 
+ *
  * Write description of header file here for dOxygen. Be as precise as 
possible.
  * Please also note how and by which parts of the code this file should be 
used.
  *

=== modified file 'hipd/input.c'
--- hipd/input.c        2010-02-08 17:56:56 +0000
+++ hipd/input.c        2010-02-10 22:32:46 +0000
@@ -63,24 +63,24 @@
  * @note            Fix the packet len before calling this function!
  */
 static int hip_verify_hmac(struct hip_common *buffer, uint16_t buf_len,
-                          u8 *hmac, void *hmac_key, int hmac_type)
+                           u8 *hmac, void *hmac_key, int hmac_type)
 {
-       int err = 0;
-       u8 hmac_res[HIP_AH_SHA_LEN];
-
-       HIP_HEXDUMP("HMAC data", buffer, buf_len);
-
-       HIP_IFEL(hip_write_hmac(hmac_type, hmac_key, buffer,
-                               buf_len, hmac_res),
-                -EINVAL, "Could not build hmac\n");
-
-       HIP_HEXDUMP("HMAC", hmac_res, HIP_AH_SHA_LEN);
-       HIP_IFE(memcmp(hmac_res, hmac, HIP_AH_SHA_LEN), -EINVAL);
-
-
- out_err:
-
-       return err;
+    int err = 0;
+    u8 hmac_res[HIP_AH_SHA_LEN];
+
+    HIP_HEXDUMP("HMAC data", buffer, buf_len);
+
+    HIP_IFEL(hip_write_hmac(hmac_type, hmac_key, buffer,
+                            buf_len, hmac_res),
+             -EINVAL, "Could not build hmac\n");
+
+    HIP_HEXDUMP("HMAC", hmac_res, HIP_AH_SHA_LEN);
+    HIP_IFE(memcmp(hmac_res, hmac, HIP_AH_SHA_LEN), -EINVAL);
+
+
+out_err:
+
+    return err;
 }
 
 /**
@@ -93,46 +93,46 @@
  * not be validated.
  */
 int hip_verify_packet_hmac_general(struct hip_common *msg,
-                                  const struct hip_crypto_key *crypto_key,
-                                  const hip_tlv_type_t parameter_type)
+                                   const struct hip_crypto_key *crypto_key,
+                                   const hip_tlv_type_t parameter_type)
 {
-       int err = 0, len = 0, orig_len = 0;
-       struct hip_crypto_key tmpkey;
-       struct hip_hmac *hmac = NULL;
-       u8 orig_checksum = 0;
-
-       HIP_DEBUG("hip_verify_packet_hmac() invoked.\n");
-
-       HIP_IFEL(!(hmac = hip_get_param(msg, parameter_type)),
-                -ENOMSG, "No HMAC parameter\n");
-
-       /* hmac verification modifies the msg length temporarily, so we have
-          to restore the length */
-       orig_len = hip_get_msg_total_len(msg);
-
-       /* hmac verification assum