- kernel interface hacks, works partially
authorMartin Willi <martin@strongswan.org>
Mon, 5 Dec 2005 16:09:42 +0000 (16:09 -0000)
committerMartin Willi <martin@strongswan.org>
Mon, 5 Dec 2005 16:09:42 +0000 (16:09 -0000)
- interface must be redefined

Source/charon/testcases/Makefile.testcases
Source/charon/testcases/kernel_interface_test.c [new file with mode: 0644]
Source/charon/testcases/kernel_interface_test.h [new file with mode: 0644]
Source/charon/testcases/testcases.c
Source/charon/threads/kernel_interface.c
Source/charon/threads/kernel_interface.h
Source/charon/threads/receiver.c
Source/charon/threads/scheduler.c
Source/charon/threads/sender.c
Source/charon/utils/identification.c

index bdb54c2..e9f07f5 100644 (file)
@@ -123,3 +123,9 @@ $(BUILD_DIR)rsa_test.o :                    $(TESTCASES_DIR)rsa_test.c $(TESTCASES_DIR)rsa_test.h
 TEST_OBJS+= $(BUILD_DIR)prime_pool_test.o
 $(BUILD_DIR)prime_pool_test.o :                $(TESTCASES_DIR)prime_pool_test.c $(TESTCASES_DIR)prime_pool_test.h
                                                                        $(CC) $(CFLAGS) -c -o $@ $<
+
+
+TEST_OBJS+= $(BUILD_DIR)kernel_interface_test.o
+$(BUILD_DIR)kernel_interface_test.o :  $(TESTCASES_DIR)kernel_interface_test.c $(TESTCASES_DIR)kernel_interface_test.h
+                                                                       $(CC) $(CFLAGS) -c -o $@ $<
+                                                                       
\ No newline at end of file
diff --git a/Source/charon/testcases/kernel_interface_test.c b/Source/charon/testcases/kernel_interface_test.c
new file mode 100644 (file)
index 0000000..1c475ec
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+ * @file kernel_interface_test.h
+ * 
+ * @brief Tests for the kernel_interface_t class.
+ * 
+ */
+
+/*
+ * Copyright (C) 2005 Jan Hutter, Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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 the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include "kernel_interface_test.h"
+
+#include <daemon.h>
+#include <threads/kernel_interface.h>
+#include <utils/allocator.h>
+#include <utils/logger.h>
+#include <network/host.h>
+
+
+/* 
+ * described in Header-File
+ */
+void test_kernel_interface(tester_t *tester)
+{
+       kernel_interface_t *kernel_interface;
+       u_int32_t spi;
+       host_t *me, *other;
+       status_t status;
+       
+       u_int8_t enc_key_bytes[] = {
+               0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
+               0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
+       };
+       
+       u_int8_t inc_key_bytes[] = {
+               0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
+               0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
+       };
+       
+       chunk_t enc_key,inc_key;
+       enc_key.ptr = enc_key_bytes;
+       enc_key.len = sizeof(enc_key_bytes);
+       inc_key.ptr = inc_key_bytes;
+       inc_key.len = sizeof(inc_key_bytes);
+       
+       
+       
+       kernel_interface = kernel_interface_create();
+       
+       me = host_create(AF_INET, "152.96.193.130", 500);
+       other = host_create(AF_INET, "152.96.193.130", 500);
+
+
+
+       //status = kernel_interface->get_spi(kernel_interface, me, other, 51, TRUE, &spi);
+       //status |= kernel_interface->get_spi(kernel_interface, me, other, 50, TRUE, &spi);
+       //tester->assert_true(tester, status == SUCCESS, "spi get");
+
+       status = kernel_interface->add_sa(kernel_interface, me, other, spi, 50, TRUE, ENCR_AES_CBC, 16, enc_key,AUTH_HMAC_MD5_96,16,inc_key,FALSE);     
+       tester->assert_true(tester, status == SUCCESS, "build sa");
+       
+
+       me->destroy(me);
+       other->destroy(other);
+       
+       kernel_interface->destroy(kernel_interface);
+       
+}
diff --git a/Source/charon/testcases/kernel_interface_test.h b/Source/charon/testcases/kernel_interface_test.h
new file mode 100644 (file)
index 0000000..e606283
--- /dev/null
@@ -0,0 +1,38 @@
+/**
+ * @file kernel_interface_test.h
+ * 
+ * @brief Tests for the kernel_interface_t class.
+ * 
+ */
+
+/*
+ * Copyright (C) 2005 Jan Hutter, Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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 the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+#ifndef KERNEL_INTERFACE_TEST_H_
+#define KERNEL_INTERFACE_TEST_H_
+
+#include <utils/tester.h>
+
+/**
+ * @brief Test function used to test the kernel_interface functionality.
+ *
+ * @param tester associated tester object
+ * 
+ * @ingroup testcases
+ */
+void test_kernel_interface(tester_t *tester);
+
+
+#endif /*KERNEL_INTERFACE_TEST_H_*/
index 3e3c850..3aad8e4 100644 (file)
@@ -60,6 +60,7 @@
 #include <testcases/sa_config_test.h>
 #include <testcases/rsa_test.h>
 #include <testcases/prime_pool_test.h>
+#include <testcases/kernel_interface_test.h>
 
 /* output for test messages */
 extern FILE * stderr;
@@ -120,6 +121,7 @@ test_t init_config_test = {test_init_config, "init_config_t test"};
 test_t sa_config_test = {test_sa_config, "sa_config_t test"};
 test_t rsa_test = {test_rsa, "RSA private/public key test"};
 test_t prime_pool_test = {test_prime_pool, "Prime pool"};
+test_t kernel_interface_test = {test_kernel_interface, "Kernel Interface"};
 
 
 daemon_t* charon;
index ffc8390..b951545 100644 (file)
 #include <linux/netlink.h>
 #include <linux/xfrm.h>
 #include <pthread.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
 
 #include "kernel_interface.h"
 
+#include <daemon.h>
 #include <utils/allocator.h>
 #include <utils/linked_list.h>
-#include <network/host.h>
-#include <encoding/payloads/proposal_substructure.h>
 
 
 typedef struct netlink_message_t netlink_message_t;
@@ -46,12 +49,20 @@ struct netlink_message_t {
        union {
                struct nlmsgerr e;
                struct xfrm_userspi_info spi;                       
-               struct xfrm_usersa_info sa;
+               struct {
+                       struct xfrm_usersa_info sa;
+                       u_int8_t data[512];
+               };
        };
-       
-       u_int8_t data[];
 };
 
+typedef struct netlink_algo_t netlink_algo_t;
+
+struct netlink_algo_t {
+       u_int16_t length;
+       u_int16_t type;
+       struct xfrm_algo algo;
+};
 
 typedef struct private_kernel_interface_t private_kernel_interface_t;
 
@@ -69,22 +80,34 @@ struct private_kernel_interface_t {
         * netlink communication socket
         */
        int socket;
+       int bc_socket;
+
+       pid_t pid;
+       /**
+        * sequence number for messages
+        */
+       u_int32_t seq;
+       
+       /** 
+        * list of responded messages
+        */
+       linked_list_t *responses;
        
        /**
-        * since we use multiple threads, we can't call
-        * getpid multiple times. The pid is set once.
+        * thread which receives messages
         */
-       pid_t pid;
+       pthread_t thread;
        
        /**
-        * sequence number for messages
+        * mutex locks access to replies list 
         */
-       u_int32_t seq;
+       pthread_mutex_t mutex;
        
-       /** 
-        * list of replies messages
+       /**
+        * Condvar allows signaling of threads waiting for a reply
         */
-       linked_list_t *replies;
+       pthread_cond_t condvar;
+       logger_t *logger;
        
        /**
         * Function for the thread, receives messages
@@ -94,171 +117,293 @@ struct private_kernel_interface_t {
        /**
         * Sends a netlink_message_t down to the kernel
         */
-       void (*send_message) (private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t *response);
+       status_t (*send_message) (private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response);
+};
+
+mapping_t kernel_encryption_algs_m[] = {
+       {ENCR_DES_IV64, ""},
+       {ENCR_DES, "des"},
+       {ENCR_3DES, "3des"},
+       {ENCR_RC5, ""},
+       {ENCR_IDEA, ""},
+       {ENCR_CAST, ""},
+       {ENCR_BLOWFISH, ""},
+       {ENCR_3IDEA, ""},
+       {ENCR_DES_IV32, ""},
+       {ENCR_NULL, ""},
+       {ENCR_AES_CBC, "aes"},
+       {ENCR_AES_CTR, ""},
+       {MAPPING_END, NULL}
+};
+
+mapping_t kernel_integrity_algs_m[] = {
+       {AUTH_HMAC_MD5_96, "md5"},
+       {AUTH_HMAC_SHA1_96, "sha1"},
+       {AUTH_DES_MAC, ""},
+       {AUTH_KPDK_MD5, ""},
+       {AUTH_AES_XCBC_96, ""},
+       {MAPPING_END, NULL}
 };
 
 
-//static u_int32_t get_spi(private_kernel_interface_t *this, host_t *src, host_t *dest, protocol_id_t protocol, bool tunnel_mode)
-//{
-//     netlink_message_t request, response;
-//     
-//    memset(&request, 0, sizeof(request));
-//    request.hdr.nlmsg_flags = NLM_F_REQUEST;
-//    request.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
-//     request.spi.info.saddr = src->get_xfrm_addr(src);
-//     request.spi.info.id.daddr = dest->get_xfrm_addr(dest);
-//    request.spi.info.mode = tunnel_mode;
-//    request.spi.info.id.proto = protocol;
-//    request.spi.info.family = src->get_family(src);
-//    request.spi.min = 0;
-//    request.spi.max = 50000;
-//    request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.spi)));
-//
-//    this->send_message(this, &request, &response);
-//
-//    if (response.hdr.nlmsg_type == NLMSG_ERROR)
-//    {
-//             /* error handling */
-//    }
-//    else if (response.hdr.nlmsg_len < NLMSG_LENGTH(sizeof(response.sa)))
-//    {
-//             /* error handling */
-//    }
-//
-//    return response.sa.id.spi;
-//}
-//
-//
-//
-//static status_t send_message(private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t *response)
-//{
-//     size_t length;
-//     ssize_t r;
-//     
-//     length = request->hdr.nlmsg_len;
-//     
-//     
-//     
-//
-//
-//    size_t len;
-//    ssize_t r;
-//
-//
-//
-//    request->hdr.nlmsg_seq = ++this->seq;
-//     length = request->hdr.nlmsg_len;
-//     
-//    do {
-//             r = write(netlinkfd, hdr, len);
-//    } while (r < 0 && errno == EINTR);
-//    
-//    if (r < 0)
-//    {
-//             return FAILED;
-//    }
-//    else if ((size_t)r != len)
-//    {
-//             return FAILED;
-//    }
-//    
-//    
-//    /* wait for receiver thread */
-//
-//    return TRUE;
-//}
-//
-//
-//static void receive_messages(private_kernel_interface_t *this)
-//{
-//     while(TRUE) 
-//     {
-//             netlink_message_t *response;
-//             
-//             
-//             socklen_t addr_length;
-//     struct sockaddr_nl addr;
-//             size_t length;
-//
-//             addr_length = sizeof(addr);
-//             length = recvfrom(netlinkfd, &rsp, sizeof(rsp), 0, (struct sockaddr*)&addr, &addr_length);
-//             if (r < 0)
-//             {
-//                     if (errno == EINTR)
-//             {
-//                             continue;
-//             }
-//             return FAILED;
-//             }
-//             else if ((size_t) r < sizeof(rsp.n))
-//             {
-//                     /* not enought bytes for header */
-//                     continue;
-//             }
-//             else if (addr.nl_pid != 0)
-//             {
-//                     /* not interested */
-//                 continue;
-//             }
-//             else if (rsp.n.nlmsg_seq != seq)
-//             {
-//                 DBG(DBG_KLIPS,
-//                     DBG_log("netlink: ignoring out of sequence (%u/%u) message %s"
-//                         , rsp.n.nlmsg_seq, seq
-//                         , sparse_val_show(xfrm_type_names, rsp.n.nlmsg_type)));
-//                 continue;
-//             }
-//             break;
-//    }
-//
-//    if (rsp.n.nlmsg_len > (size_t) r)
-//    {
-//             return FALSE;
-//    }
-//    else if (rsp.n.nlmsg_type != NLMSG_ERROR
-//    && (rbuf && rsp.n.nlmsg_type != rbuf->nlmsg_type))
-//    {
-//     loglog(RC_LOG_SERIOUS
-//         , "netlink recvfrom() of response to our %s message"
-//           " for %s %s was of wrong type (%s)"
-//         , sparse_val_show(xfrm_type_names, hdr->nlmsg_type)
-//         , description, text_said
-//         , sparse_val_show(xfrm_type_names, rsp.n.nlmsg_type));
-//     return FALSE;
-//    }
-//    else if (rbuf)
-//    {
-//     if ((size_t) r > rbuf_len)
-//     {
-//         loglog(RC_LOG_SERIOUS
-//             , "netlink recvfrom() of response to our %s message"
-//               " for %s %s was too long: %ld > %lu"
-//             , sparse_val_show(xfrm_type_names, hdr->nlmsg_type)
-//             , description, text_said
-//             , (long)r, (unsigned long)rbuf_len);
-//         return FALSE;
-//     }
-//     memcpy(rbuf, &rsp, r);
-//     return TRUE;
-//    }
-//    else if (rsp.n.nlmsg_type == NLMSG_ERROR && rsp.e.error)
-//    {
-//     loglog(RC_LOG_SERIOUS
-//         , "ERROR: netlink response for %s %s included errno %d: %s"
-//         , description, text_said
-//         , -rsp.e.error
-//         , strerror(-rsp.e.error));
-//     return FALSE;
-//    }        
-//}
+static status_t get_spi(private_kernel_interface_t *this, host_t *src, host_t *dest, protocol_id_t protocol, bool tunnel_mode, u_int32_t *spi)
+{
+       netlink_message_t request, *response;
+       
+    memset(&request, 0, sizeof(request));
+    request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.spi)));
+    request.hdr.nlmsg_flags = NLM_F_REQUEST;
+    request.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
+       request.spi.info.saddr = src->get_xfrm_addr(src);
+       request.spi.info.id.daddr = dest->get_xfrm_addr(dest);
+    request.spi.info.mode = tunnel_mode;
+    request.spi.info.id.proto = protocol;
+    request.spi.info.family = PF_INET;
+    request.spi.min = 100;
+    request.spi.max = 200;
 
+       if (this->send_message(this, &request, &response) != SUCCESS)
+       {
+               return FAILED;
+       }
+    
+    if (response->hdr.nlmsg_type == NLMSG_ERROR)
+    {
+       return FAILED;
+    }
+    
+    if (response->hdr.nlmsg_type != XFRM_MSG_NEWSA)
+    {
+       return FAILED;
+    }
+    else if (response->hdr.nlmsg_len < NLMSG_LENGTH(sizeof(response->sa)))
+    {
+               return FAILED;
+    }
+       
+       *spi = response->sa.id.spi;
+       allocator_free(response);
 
+    return SUCCESS;
+}
 
- /**
+static status_t add_sa(        private_kernel_interface_t *this,
+                                               host_t *me, 
+                                               host_t *other, 
+                                               u_int32_t spi, 
+                                               int protocol, 
+                                               bool tunnel_mode,
+                                               encryption_algorithm_t enc_alg,
+                                               size_t enc_size,
+                                               chunk_t enc_key,
+                                               integrity_algorithm_t int_alg,
+                                               size_t int_size,
+                                               chunk_t int_key,
+                                               bool replace)
+{
+    netlink_message_t request, *response;
+    
+    memset(&request, 0, sizeof(request));
+    
+    request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+    request.hdr.nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
+
+    request.sa.saddr = me->get_xfrm_addr(me);
+    request.sa.id.daddr = other->get_xfrm_addr(other);
+
+    request.sa.id.spi = spi;
+    request.sa.id.proto = protocol;
+    request.sa.family = me->get_family(me);
+    request.sa.mode = tunnel_mode;
+    request.sa.replay_window = 0; //sa->replay_window; ???
+    request.sa.reqid = 0; //sa->reqid; ???
+    request.sa.lft.soft_byte_limit = XFRM_INF;
+    request.sa.lft.soft_packet_limit = XFRM_INF;
+    request.sa.lft.hard_byte_limit = XFRM_INF;
+    request.sa.lft.hard_packet_limit = XFRM_INF;
+
+    request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.sa)));
+
+    if (enc_alg != ENCR_UNDEFINED)
+    {
+               netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
+       
+       nla->type = XFRMA_ALG_CRYPT;
+       nla->length = sizeof(netlink_algo_t) + enc_size;
+               nla->algo.alg_key_len = enc_size * 8;
+               
+               strcpy(nla->algo.alg_name, mapping_find(kernel_encryption_algs_m, enc_alg));
+               memcpy(nla->algo.alg_key, enc_key.ptr, enc_key.len);
+
+               request.hdr.nlmsg_len += nla->length;
+    }
+
+    if (int_alg != AUTH_UNDEFINED)
+    {
+               netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
+               
+               nla->type = XFRMA_ALG_AUTH;
+       nla->length = sizeof(netlink_algo_t) + int_size;
+               nla->algo.alg_key_len = int_size * 8;
+               strcpy(nla->algo.alg_name, mapping_find(kernel_integrity_algs_m, int_alg));
+               memcpy(nla->algo.alg_key, int_key.ptr, int_key.len);
+
+               request.hdr.nlmsg_len += nla->length;
+    }
+    
+       /* add IPComp */
+    
+    if (this->send_message(this, &request, &response) != SUCCESS)
+    {
+       allocator_free(response);
+       return FAILED;  
+    }
+
+       printf("received message type: %d\n", response->hdr.nlmsg_type);
+       
+       if (response->hdr.nlmsg_type == NLMSG_ERROR)
+       {
+               printf("received error: %s\n", strerror(-response->e.error));
+       }
+       
+    allocator_free(response);
+    return SUCCESS;
+}
+
+
+
+static status_t send_message(private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response)
+{
+       size_t length;
+       struct sockaddr_nl addr;
+       
+       request->hdr.nlmsg_seq = ++this->seq;
+       request->hdr.nlmsg_pid = this->pid;
+
+       memset(&addr, 0, sizeof(struct sockaddr_nl));
+       addr.nl_family = AF_NETLINK;
+       addr.nl_pid = 0;
+       addr.nl_groups = 0;
+       
+       length = sendto(this->socket,(void *)request, request->hdr.nlmsg_len, 0, (struct sockaddr *)&addr, sizeof(addr));
+       
+       if (length < 0)
+       {
+               return FAILED;
+       }
+       else if (length != request->hdr.nlmsg_len)
+       {
+               return FAILED;
+       }
+       
+       pthread_mutex_lock(&(this->mutex));
+       
+       while (TRUE)
+       {
+               iterator_t *iterator; 
+               bool found = FALSE;
+               /* search list, break if found */
+               iterator = this->responses->create_iterator(this->responses, TRUE);
+               while (iterator->has_next(iterator))
+               {
+                       netlink_message_t *listed_response;
+                       iterator->current(iterator, (void**)&listed_response);
+                       if (listed_response->hdr.nlmsg_seq == request->hdr.nlmsg_seq)
+                       {
+                               /* matches our request, this is the reply */
+                                       *response = listed_response;
+                                       found = TRUE;
+                                       break;
+                               }
+                       }
+                       iterator->destroy(iterator);
+                       
+                       if (found)
+                       {
+                               break;  
+                       }
+               /* we should time out, if something goes wrong */
+               pthread_cond_wait(&(this->condvar), &(this->mutex));
+       }
+       
+       pthread_mutex_unlock(&(this->mutex));
+       
+       return SUCCESS;
+}
+
+
+static void receive_messages(private_kernel_interface_t *this)
+{      
+       while(TRUE) 
+       {
+               netlink_message_t response, *listed_response;
+               while (TRUE)
+               {
+               struct sockaddr_nl addr;
+                       socklen_t addr_length;
+                       size_t length;
+       
+                       addr_length = sizeof(addr);
+                       
+                       response.hdr.nlmsg_type = XFRM_MSG_NEWSA;
+                       length = recvfrom(this->socket, &response, sizeof(response), 0, (struct sockaddr*)&addr, &addr_length);
+                       if (length < 0)
+                       {
+                               if (errno == EINTR)
+                       {
+                               /* interrupted, try again */
+                                       continue;
+                       }
+                       charon->kill(charon, "receiving from netlink socket failed");
+                       }
+                       if (!NLMSG_OK(&response.hdr, length))
+                       {
+                               /* bad netlink message */
+                               continue;
+                       }
+                       if (addr.nl_pid != 0)
+                       {
+                               /* not interested, try another one */
+                           continue;
+                       }
+                       break;
+               }
+               
+               /* got a valid message.
+                * requests are handled on or own, 
+                * responses are listed for the requesters
+                */
+               if (response.hdr.nlmsg_flags & NLM_F_REQUEST)
+               {
+                       /* handle request */    
+               }
+               else
+               {
+                       /* add response to queue */                     
+                       listed_response = allocator_alloc(sizeof(response));
+                       memcpy(listed_response, &response, sizeof(response));
+
+                       pthread_mutex_lock(&(this->mutex));
+                       this->responses->insert_last(this->responses, (void*)listed_response);
+                       pthread_mutex_unlock(&(this->mutex));
+                       /* signal ALL waiting threads */
+                       pthread_cond_broadcast(&(this->condvar));
+               }
+               /* get the next one */
+       }
+}
+
+
+
+/**
  * implements kernel_interface_t.destroy
  */
-static void destroy (private_kernel_interface_t *this)
+static void destroy(private_kernel_interface_t *this)
 {      
+       pthread_cancel(this->thread);
+       pthread_join(this->thread, NULL);
+       close(this->socket);
+       this->responses->destroy(this->responses);
        allocator_free(this);
 }
 
@@ -270,10 +415,36 @@ kernel_interface_t *kernel_interface_create()
        private_kernel_interface_t *this = allocator_alloc_thing(private_kernel_interface_t);
        
        /* public functions */
+       this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,bool,u_int32_t*))get_spi;
+       
+       this->public.add_sa  = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,int,bool,encryption_algorithm_t,size_t,chunk_t,integrity_algorithm_t,size_t,chunk_t,bool))add_sa;
+       
+       
        this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
 
        /* private members */
+       this->receive_messages = receive_messages;
+       this->send_message = send_message;
+       this->pid = getpid();
+       this->responses = linked_list_create();
+       pthread_mutex_init(&(this->mutex),NULL);
+       pthread_cond_init(&(this->condvar),NULL);
+       this->seq = 0;
        this->socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
+       if (this->socket <= 0)
+       {
+               allocator_free(this);
+               charon->kill(charon, "Unable to create netlink socket");        
+       }
+       
+       if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->receive_messages, this) != 0)
+       {
+               close(this->socket);
+               allocator_free(this);
+               charon->kill(charon, "Unable to create netlink thread");
+       }
        
+       this->logger = charon->logger_manager->create_logger(charon->logger_manager, TESTER, NULL);
+       charon->logger_manager->enable_logger_level(charon->logger_manager, TESTER, FULL);
        return (&this->public);
 }
index 3ade974..fe32e36 100644 (file)
@@ -24,6 +24,8 @@
 #define KERNEL_INTERFACE_H_
 
 
+#include <network/host.h>
+#include <encoding/payloads/proposal_substructure.h>
 
 typedef struct kernel_interface_t kernel_interface_t;
 
@@ -34,6 +36,23 @@ typedef struct kernel_interface_t kernel_interface_t;
  */
 struct kernel_interface_t {
 
+       status_t (*get_spi) (kernel_interface_t *this, host_t *src, host_t *dest, protocol_id_t protocol, bool tunnel_mode, u_int32_t *spi);
+       
+       status_t (*add_sa)(kernel_interface_t *this,
+                                               host_t *me, 
+                                               host_t *other, 
+                                               u_int32_t spi, 
+                                               int protocol, 
+                                               bool tunnel_mode,
+                                               encryption_algorithm_t enc_alg,
+                                               size_t enc_size,
+                                               chunk_t enc_key,
+                                               integrity_algorithm_t int_alg,
+                                               size_t int_size,
+                                               chunk_t int_key,
+                                               bool replace);
+       
+       
        /**
         * @brief Destroys a kernel_interface object.
         *
index 85410d2..db22542 100644 (file)
@@ -123,7 +123,7 @@ receiver_t * receiver_create()
                this->logger->log(this->logger, ERROR, "Receiver thread could not be started");
                charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
                allocator_free(this);
-               return NULL;
+               charon->kill(charon, "Unable to create receiver thread");
        }
 
        return &(this->public);
index e4f9ab3..b6d3fab 100644 (file)
@@ -118,7 +118,7 @@ scheduler_t * scheduler_create()
                this->logger->log(this->logger, ERROR, "Scheduler thread could not be created!");
                charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
                allocator_free(this);
-               return NULL;
+               charon->kill(charon, "Unable to create scheduler thread");
        }
 
        return &(this->public);
index c47c4e2..cb33d84 100644 (file)
@@ -121,7 +121,7 @@ sender_t * sender_create()
        {
                this->logger->log(this->logger, ERROR, "Sender thread could not be created");
                allocator_free(this);
-               return NULL;
+               charon->kill(charon, "Unable to create sender thread");
        }
 
        return &(this->public);
index 50826cb..2e594dd 100644 (file)
@@ -191,6 +191,7 @@ identification_t *identification_create_from_string(id_type_t type, char *string
  */
 identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
 {
+       char *string;
        private_identification_t *this = identification_create();
        
        this->encoded = allocator_clone_chunk(encoded);
@@ -200,48 +201,48 @@ identification_t *identification_create_from_encoding(id_type_t type, chunk_t en
        {
                case ID_IPV4_ADDR:
                {
-                       char *tmp;
-                       tmp = inet_ntoa(*((struct in_addr*)(encoded.ptr)));
-                       /* build string, must be cloned */
-                       this->string = allocator_alloc(strlen(tmp)+1);
-                       strcpy(this->string, tmp);
+                       string = inet_ntoa(*((struct in_addr*)(encoded.ptr)));
                        break;
                }
                case ID_IPV6_ADDR:
                {
-                       this->string = "[ID_IPV6_ADDR]";
+                       string = "[ID_IPV6_ADDR]";
                        break;
                }
                case ID_FQDN:
                {
-                       this->string = "[ID_FQDN]";
+                       string = "[ID_FQDN]";
                        break;
                }
                case ID_RFC822_ADDR:
                {
-                       this->string = "[ID_RFC822_ADDR]";
+                       string = "[ID_RFC822_ADDR]";
                        break;
                }
                case ID_DER_ASN1_DN:
                {
-                       this->string = "[ID_DER_ASN1_DN]";
+                       string = "[ID_DER_ASN1_DN]";
                        break;
                }
                case ID_DER_ASN1_GN:
                {
-                       this->string = "[ID_DER_ASN1_GN]";
+                       string = "[ID_DER_ASN1_GN]";
                        break;
                }
                case ID_KEY_ID:
                {
-                       this->string = "[ID_KEY_ID]";
+                       string = "[ID_KEY_ID]";
                        break;
                }
                default:
                {
-                       this->string = "[unknown id_type_t]";
+                       string = "[unknown id_type_t]";
                }
-       }
+       }                       
+       
+       /* build string, must be cloned */
+       this->string = allocator_alloc(strlen(string)+1);
+       strcpy(this->string, string);
        
        return &(this->public);
 }