- moved packet and socket in new network-package
authorMartin Willi <martin@strongswan.org>
Wed, 23 Nov 2005 09:43:14 +0000 (09:43 -0000)
committerMartin Willi <martin@strongswan.org>
Wed, 23 Nov 2005 09:43:14 +0000 (09:43 -0000)
22 files changed:
Source/charon/daemon.c
Source/charon/globals.h
Source/charon/message.h
Source/charon/network/packet.c [new file with mode: 0644]
Source/charon/network/packet.h [new file with mode: 0644]
Source/charon/network/socket.c [new file with mode: 0644]
Source/charon/network/socket.h [new file with mode: 0644]
Source/charon/packet.c [deleted file]
Source/charon/packet.h [deleted file]
Source/charon/queues/jobs/incoming_packet_job.h
Source/charon/queues/jobs/job.h
Source/charon/queues/send_queue.h
Source/charon/receiver.c
Source/charon/sender.c
Source/charon/socket.c [deleted file]
Source/charon/socket.h [deleted file]
Source/charon/testcases/packet_test.c
Source/charon/testcases/receiver_test.c
Source/charon/testcases/scheduler_test.c
Source/charon/testcases/sender_test.c
Source/charon/testcases/socket_test.c
Source/charon/thread_pool.c

index b3e9b61..277d8c6 100644 (file)
 #include "daemon.h" 
 
 
-#include "types.h"
-#include "socket.h"
-#include "ike_sa_manager.h"
-#include "sender.h"
-#include "receiver.h"
-#include "scheduler.h"
-#include "thread_pool.h"
-#include "utils/allocator.h"
-#include "utils/logger_manager.h"
-#include "queues/event_queue.h"
-#include "queues/job_queue.h"
-#include "queues/send_queue.h"
+#include <types.h>
+#include <ike_sa_manager.h>
+#include <sender.h>
+#include <receiver.h>
+#include <scheduler.h>
+#include <thread_pool.h>
+#include <network/socket.h>
+#include <utils/allocator.h>
+#include <utils/logger_manager.h>
+#include <queues/event_queue.h>
+#include <queues/job_queue.h>
+#include <queues/send_queue.h>
 
 
 /* function declaration (defined and described after main function) */
index 137ac0d..d3cfb08 100644 (file)
 #ifndef GLOBALS_H_
 #define GLOBALS_H_
 
-#include <socket.h>
 #include <ike_sa_manager.h>
 #include <queues/send_queue.h>
 #include <queues/job_queue.h>
+#include <network/socket.h>
 #include <queues/event_queue.h>
 #include <utils/logger_manager.h>
 #include <configuration_manager.h>
index 4850250..0385bd7 100644 (file)
@@ -24,8 +24,8 @@
 #define MESSAGE_H_
 
 #include <types.h>
-#include <packet.h>
 #include <ike_sa_id.h>
+#include <network/packet.h>
 #include <payloads/ike_header.h>
 #include <utils/linked_list.h>
 
diff --git a/Source/charon/network/packet.c b/Source/charon/network/packet.c
new file mode 100644 (file)
index 0000000..f09b18a
--- /dev/null
@@ -0,0 +1,130 @@
+/**
+ * @file packet.c
+ * 
+ * @brief UDP-Packet, contains data, sender and receiver.
+ * 
+ */
+
+/*
+ * 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 "packet.h"
+
+#include <utils/allocator.h>
+
+
+/**
+ * Private data of an packet_t object
+ */
+typedef struct private_packet_s private_packet_t;
+
+struct private_packet_s {
+
+       /**
+        * Public part of a packet_t object
+        */
+       packet_t public;
+};
+
+/**
+ * Implements packet_t's destroy function.
+ * See #packet_s.destroy for description.
+ */
+static status_t destroy(private_packet_t *this)
+{
+       if (this->public.source != NULL)
+       {
+               this->public.source->destroy(this->public.source);
+       }       
+       if (this->public.destination != NULL)
+       {
+               this->public.destination->destroy(this->public.destination);
+       }
+       if (this->public.data.ptr != NULL)
+       {
+               allocator_free(this->public.data.ptr);
+       }
+       allocator_free(this);
+       return SUCCESS;
+}
+
+/**
+ * Implements packet_t's clone function.
+ * See #packet_s.clone for description.
+ */
+static status_t clone (private_packet_t *this, packet_t **clone)
+{
+       packet_t *other;
+       other = packet_create();
+       if (other == NULL)
+       {
+               return OUT_OF_RES;
+       }
+       
+       if (this->public.destination != NULL)
+       {
+               this->public.destination->clone(this->public.destination, &(other->destination));
+       }
+       else {
+               other->destination = NULL;
+       }
+       
+       if (this->public.source != NULL)
+       {
+               this->public.source->clone(this->public.source, &(other->source));
+       }
+       else {
+               other->source = NULL;
+       }
+       
+       /* only clone existing chunks :-) */
+       if (this->public.data.ptr != NULL)
+       {
+               other->data.ptr = allocator_clone_bytes(this->public.data.ptr,this->public.data.len);
+               if (other->data.ptr == NULL)
+               {
+                       other->destroy(other);
+                       return OUT_OF_RES;
+               }
+               other->data.len = this->public.data.len;
+       }
+       else
+       {
+               other->data.ptr = NULL;
+               other->data.len = 0;
+       }
+       *clone = other;
+       return SUCCESS;
+}
+
+
+/*
+ * Documented in header
+ */
+packet_t *packet_create()
+{
+       private_packet_t *this = allocator_alloc_thing(private_packet_t);
+
+       this->public.destroy = (status_t(*) (packet_t *)) destroy;
+       this->public.clone = (status_t(*) (packet_t *,packet_t**))clone;
+       
+       this->public.destination = NULL;
+       this->public.source = NULL;
+
+       this->public.data.len = 0;
+       this->public.data.ptr = NULL;
+       return &(this->public);
+}
diff --git a/Source/charon/network/packet.h b/Source/charon/network/packet.h
new file mode 100644 (file)
index 0000000..10488e0
--- /dev/null
@@ -0,0 +1,80 @@
+/**
+ * @file packet.h
+ * 
+ * @brief UDP-Packet, contains data, sender and receiver.
+ * 
+ */
+
+/*
+ * 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 PACKET_H_
+#define PACKET_H_
+
+
+#include <types.h>
+#include <utils/host.h>
+
+
+
+/**
+ * @brief UDP-Packet, contains data, sender and receiver
+ */
+typedef struct packet_s packet_t;
+struct packet_s {
+
+       /**
+        * source address structure
+        */
+       host_t *source;
+               
+       /**
+        * destination address structure
+        */
+       host_t *destination;
+        
+        /**
+         * message data
+         */
+       chunk_t data;
+               
+       /**
+        * @brief                       Clones a packet_t object
+        *  
+        * @param packet                calling object
+        * @param clone         pointer to a packet_t object pointer where the new object is stored
+        * @return                      - SUCCESS if successful
+        *                                      - OUT_OF_RES
+        */
+       status_t (*clone) (packet_t *packet, packet_t **clone);
+       
+       /**
+        * @brief                       destroy the packet, freeing contained data
+        *  
+        * @param packet        packet to destroy       
+        * @return                      - SUCCESS
+        */
+       status_t (*destroy) (packet_t *packet);
+};
+
+/**
+ * @brief create an empty packet
+ *  
+ * @param family               address-family, such as AF_INET
+ * @return                     - NULL when family not supported
+ */
+packet_t *packet_create();
+
+#endif /*PACKET_H_*/
diff --git a/Source/charon/network/socket.c b/Source/charon/network/socket.c
new file mode 100644 (file)
index 0000000..fc652b5
--- /dev/null
@@ -0,0 +1,186 @@
+/**
+ * @file socket.c
+ *
+ * @brief management of sockets
+ *
+ * receiver reads from here, sender writes to here
+ *
+ */
+
+/*
+ * 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 <pthread.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "socket.h"
+
+#include <globals.h>
+#include <utils/allocator.h>
+#include <utils/logger_manager.h>
+
+typedef struct private_socket_s private_socket_t;
+
+struct private_socket_s{
+       /**
+        * public functions
+        */
+        socket_t public;
+
+        /**
+         * currently we only have one socket, maybe more in the future ?
+         */
+        int socket_fd;
+        /** 
+         * logger for this socket
+         */
+        logger_t *logger;
+};
+
+/**
+ * implementation of socket_t.receive
+ */
+status_t receiver(private_socket_t *this, packet_t **packet)
+{
+       char buffer[MAX_PACKET];
+       int oldstate;
+       packet_t *pkt = packet_create();
+
+       /* add packet destroy handler for cancellation, enable cancellation */
+       pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
+       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
+       
+       pkt->source = host_create(AF_INET, "0.0.0.0", 0);
+       pkt->destination = host_create(AF_INET, "0.0.0.0", 0);
+
+
+       this->logger->log(this->logger, CONTROL|MORE, "going to read from socket");
+       /* do the read */
+       pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
+                                                       pkt->source->get_sockaddr(pkt->source), 
+                                                       pkt->source->get_sockaddr_len(pkt->source));
+
+       /* reset cancellation, remove packet destroy handler (without executing) */
+       pthread_setcancelstate(oldstate, NULL);
+       pthread_cleanup_pop(0);
+
+
+       /* TODO: get senders destination address, using
+        * IP_PKTINFO and recvmsg */
+
+       if (pkt->data.len < 0)
+       {
+               pkt->destroy(pkt);
+               this->logger->log(this->logger, ERROR, "error reading from socket: %s", strerror(errno));
+               return FAILED;
+       }
+       
+       this->logger->log(this->logger, CONTROL, "received packet from %s:%d",
+                                               pkt->source->get_address(pkt->source), 
+                                               pkt->source->get_port(pkt->source));
+
+       /* fill in packet */
+       pkt->data.ptr = allocator_alloc(pkt->data.len);
+       memcpy(pkt->data.ptr, buffer, pkt->data.len);
+
+       /* return packet */
+       *packet = pkt;
+
+       return SUCCESS;
+}
+
+/**
+ * implementation of socket_t.send
+ */
+status_t sender(private_socket_t *this, packet_t *packet)
+{
+       ssize_t bytes_sent;
+
+
+       this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
+                                               packet->destination->get_address(packet->destination), 
+                                               packet->destination->get_port(packet->destination));
+       /* send data */
+       bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
+                                               0, packet->destination->get_sockaddr(packet->destination), 
+                                               *(packet->destination->get_sockaddr_len(packet->destination)));
+
+       if (bytes_sent != packet->data.len)
+       {
+               this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
+               return FAILED;
+       }
+       return SUCCESS;
+}
+
+/**
+ * implementation of socket_t.destroy
+ */
+status_t destroy(private_socket_t *this)
+{
+       close(this->socket_fd);
+       global_logger_manager->destroy_logger(global_logger_manager, this->logger);
+       allocator_free(this);
+
+       return SUCCESS;
+}
+
+socket_t *socket_create(u_int16_t port)
+{
+       private_socket_t *this = allocator_alloc_thing(private_socket_t);
+       struct sockaddr_in addr;
+
+       /* public functions */
+       this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
+       this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
+       this->public.destroy = (status_t(*)(socket_t*))destroy;
+       
+       
+       this->logger = global_logger_manager->create_logger(global_logger_manager, SOCKET, NULL);
+       if (this->logger == NULL)
+       {
+               allocator_free(this);
+               return NULL;
+       }
+
+       /* create default ipv4 socket */
+       this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
+       if (this->socket_fd < 0) 
+       {
+               this->logger->log(this->logger, ERROR, "unable to open socket: %s", strerror(errno));
+               global_logger_manager->destroy_logger(global_logger_manager, this->logger);
+               allocator_free(this);
+               return NULL;
+       }
+
+       /* bind socket to all interfaces */
+       addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = INADDR_ANY;
+    addr.sin_port = htons(port);
+    if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) 
+    {
+               this->logger->log(this->logger, ERROR, "unable to bind socket to port %d: %s", port, strerror(errno));
+               global_logger_manager->destroy_logger(global_logger_manager, this->logger);
+               allocator_free(this);
+        return NULL;
+    }
+
+       return (socket_t*)this;
+}
diff --git a/Source/charon/network/socket.h b/Source/charon/network/socket.h
new file mode 100644 (file)
index 0000000..d5fd828
--- /dev/null
@@ -0,0 +1,96 @@
+/**
+ * @file socket.h
+ * 
+ * @brief management of sockets
+ * 
+ * receiver reads from here, sender writes to here
+ * 
+ */
+
+/*
+ * 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 SOCKET_H_
+#define SOCKET_H_
+
+
+#include <types.h>
+#include <network/packet.h>
+
+
+/**
+ * maximum size of a packet
+ * 3000 Bytes should be sufficient, see IKEv2 draft
+ */
+#define MAX_PACKET 3000
+
+
+/**
+ * @brief abstraction of one (ipv4), or in future, of multiple sockets
+ * 
+ */
+typedef struct socket_s socket_t;
+struct socket_s {
+       /**
+        * @brief receive a packet
+        * 
+        * reads a packet from one of the sockets.
+        * source will be set, dest not implemented
+        * 
+        * 
+        * @param sock                  socket_t object to work on
+        * @param packet                pinter gets address from allocated packet_t
+        * @return                              FAILED when unable to receive
+        *                                              SUCCESS when packet successfully received
+        */
+       status_t (*receive) (socket_t *sock, packet_t **packet);
+       
+       /**
+        * @brief send a packet
+        * 
+        * sends a packet via desired socket.
+        * uses source and dest in packet.
+        * 
+        * @param sock                  socket_t object to work on
+        * @param packet[out]   packet_t to send
+        * @return                              FAILED when unable to send
+        *                                              SUCCESS when packet successfully sent
+        */
+       status_t (*send) (socket_t *sock, packet_t *packet);
+       
+       /**
+        * @brief destroy sockets
+        * 
+        * close sockets and destroy socket_t object
+        * 
+        * @param sock                  socket_t to destroy
+        * @return                              SUCCESS
+        */
+       status_t (*destroy) (socket_t *sock);
+};
+
+/**
+ * @brief socket_t constructor
+ * 
+ * currently creates one socket, listening on all addresses
+ * on port.
+ *  
+ * @param port                         port to bind socket to
+ * @return                             the created socket, or NULL on error
+ */
+socket_t *socket_create(u_int16_t port);
+
+
+#endif /*SOCKET_H_*/
diff --git a/Source/charon/packet.c b/Source/charon/packet.c
deleted file mode 100644 (file)
index f09b18a..0000000
+++ /dev/null
@@ -1,130 +0,0 @@
-/**
- * @file packet.c
- * 
- * @brief UDP-Packet, contains data, sender and receiver.
- * 
- */
-
-/*
- * 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 "packet.h"
-
-#include <utils/allocator.h>
-
-
-/**
- * Private data of an packet_t object
- */
-typedef struct private_packet_s private_packet_t;
-
-struct private_packet_s {
-
-       /**
-        * Public part of a packet_t object
-        */
-       packet_t public;
-};
-
-/**
- * Implements packet_t's destroy function.
- * See #packet_s.destroy for description.
- */
-static status_t destroy(private_packet_t *this)
-{
-       if (this->public.source != NULL)
-       {
-               this->public.source->destroy(this->public.source);
-       }       
-       if (this->public.destination != NULL)
-       {
-               this->public.destination->destroy(this->public.destination);
-       }
-       if (this->public.data.ptr != NULL)
-       {
-               allocator_free(this->public.data.ptr);
-       }
-       allocator_free(this);
-       return SUCCESS;
-}
-
-/**
- * Implements packet_t's clone function.
- * See #packet_s.clone for description.
- */
-static status_t clone (private_packet_t *this, packet_t **clone)
-{
-       packet_t *other;
-       other = packet_create();
-       if (other == NULL)
-       {
-               return OUT_OF_RES;
-       }
-       
-       if (this->public.destination != NULL)
-       {
-               this->public.destination->clone(this->public.destination, &(other->destination));
-       }
-       else {
-               other->destination = NULL;
-       }
-       
-       if (this->public.source != NULL)
-       {
-               this->public.source->clone(this->public.source, &(other->source));
-       }
-       else {
-               other->source = NULL;
-       }
-       
-       /* only clone existing chunks :-) */
-       if (this->public.data.ptr != NULL)
-       {
-               other->data.ptr = allocator_clone_bytes(this->public.data.ptr,this->public.data.len);
-               if (other->data.ptr == NULL)
-               {
-                       other->destroy(other);
-                       return OUT_OF_RES;
-               }
-               other->data.len = this->public.data.len;
-       }
-       else
-       {
-               other->data.ptr = NULL;
-               other->data.len = 0;
-       }
-       *clone = other;
-       return SUCCESS;
-}
-
-
-/*
- * Documented in header
- */
-packet_t *packet_create()
-{
-       private_packet_t *this = allocator_alloc_thing(private_packet_t);
-
-       this->public.destroy = (status_t(*) (packet_t *)) destroy;
-       this->public.clone = (status_t(*) (packet_t *,packet_t**))clone;
-       
-       this->public.destination = NULL;
-       this->public.source = NULL;
-
-       this->public.data.len = 0;
-       this->public.data.ptr = NULL;
-       return &(this->public);
-}
diff --git a/Source/charon/packet.h b/Source/charon/packet.h
deleted file mode 100644 (file)
index 10488e0..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/**
- * @file packet.h
- * 
- * @brief UDP-Packet, contains data, sender and receiver.
- * 
- */
-
-/*
- * 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 PACKET_H_
-#define PACKET_H_
-
-
-#include <types.h>
-#include <utils/host.h>
-
-
-
-/**
- * @brief UDP-Packet, contains data, sender and receiver
- */
-typedef struct packet_s packet_t;
-struct packet_s {
-
-       /**
-        * source address structure
-        */
-       host_t *source;
-               
-       /**
-        * destination address structure
-        */
-       host_t *destination;
-        
-        /**
-         * message data
-         */
-       chunk_t data;
-               
-       /**
-        * @brief                       Clones a packet_t object
-        *  
-        * @param packet                calling object
-        * @param clone         pointer to a packet_t object pointer where the new object is stored
-        * @return                      - SUCCESS if successful
-        *                                      - OUT_OF_RES
-        */
-       status_t (*clone) (packet_t *packet, packet_t **clone);
-       
-       /**
-        * @brief                       destroy the packet, freeing contained data
-        *  
-        * @param packet        packet to destroy       
-        * @return                      - SUCCESS
-        */
-       status_t (*destroy) (packet_t *packet);
-};
-
-/**
- * @brief create an empty packet
- *  
- * @param family               address-family, such as AF_INET
- * @return                     - NULL when family not supported
- */
-packet_t *packet_create();
-
-#endif /*PACKET_H_*/
index 5e5cb34..51842cf 100644 (file)
@@ -24,7 +24,7 @@
 #define INCOMING_PACKET_JOB_H_
 
 #include <types.h>
-#include <packet.h>
+#include <network/packet.h>
 #include <queues/jobs/job.h>
 
 /**
index ad8f59a..59b0f7b 100644 (file)
@@ -95,10 +95,5 @@ struct job_s{
        status_t (*destroy) (job_t *job);
 };
 
-#include "initiate_ike_sa_job.h"
-#include "delete_ike_sa_job.h"
-#include "incoming_packet_job.h"
-
-
 
 #endif /*JOB_H_*/
index 7d2592a..c1f643c 100644 (file)
@@ -24,7 +24,7 @@
 #define SEND_QUEUE_H_
 
 #include <types.h>
-#include <packet.h>
+#include <network/packet.h>
 
 /**
  * @brief Send-Queue
index 3f77eb5..ba7e229 100644 (file)
@@ -25,9 +25,9 @@
 
 #include "receiver.h"
 
-#include <socket.h>
-#include <packet.h>
 #include <globals.h>
+#include <network/socket.h>
+#include <network/packet.h>
 #include <queues/job_queue.h>
 #include <queues/jobs/job.h>
 #include <utils/allocator.h>
index 074d30b..d237696 100644 (file)
@@ -25,9 +25,9 @@
 
 #include "sender.h"
 
-#include <socket.h>
-#include <packet.h>
 #include <globals.h>
+#include <network/socket.h>
+#include <network/packet.h>
 #include <queues/send_queue.h>
 #include <utils/allocator.h>
 #include <utils/logger_manager.h>
diff --git a/Source/charon/socket.c b/Source/charon/socket.c
deleted file mode 100644 (file)
index fc652b5..0000000
+++ /dev/null
@@ -1,186 +0,0 @@
-/**
- * @file socket.c
- *
- * @brief management of sockets
- *
- * receiver reads from here, sender writes to here
- *
- */
-
-/*
- * 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 <pthread.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-#include "socket.h"
-
-#include <globals.h>
-#include <utils/allocator.h>
-#include <utils/logger_manager.h>
-
-typedef struct private_socket_s private_socket_t;
-
-struct private_socket_s{
-       /**
-        * public functions
-        */
-        socket_t public;
-
-        /**
-         * currently we only have one socket, maybe more in the future ?
-         */
-        int socket_fd;
-        /** 
-         * logger for this socket
-         */
-        logger_t *logger;
-};
-
-/**
- * implementation of socket_t.receive
- */
-status_t receiver(private_socket_t *this, packet_t **packet)
-{
-       char buffer[MAX_PACKET];
-       int oldstate;
-       packet_t *pkt = packet_create();
-
-       /* add packet destroy handler for cancellation, enable cancellation */
-       pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
-       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
-       
-       pkt->source = host_create(AF_INET, "0.0.0.0", 0);
-       pkt->destination = host_create(AF_INET, "0.0.0.0", 0);
-
-
-       this->logger->log(this->logger, CONTROL|MORE, "going to read from socket");
-       /* do the read */
-       pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
-                                                       pkt->source->get_sockaddr(pkt->source), 
-                                                       pkt->source->get_sockaddr_len(pkt->source));
-
-       /* reset cancellation, remove packet destroy handler (without executing) */
-       pthread_setcancelstate(oldstate, NULL);
-       pthread_cleanup_pop(0);
-
-
-       /* TODO: get senders destination address, using
-        * IP_PKTINFO and recvmsg */
-
-       if (pkt->data.len < 0)
-       {
-               pkt->destroy(pkt);
-               this->logger->log(this->logger, ERROR, "error reading from socket: %s", strerror(errno));
-               return FAILED;
-       }
-       
-       this->logger->log(this->logger, CONTROL, "received packet from %s:%d",
-                                               pkt->source->get_address(pkt->source), 
-                                               pkt->source->get_port(pkt->source));
-
-       /* fill in packet */
-       pkt->data.ptr = allocator_alloc(pkt->data.len);
-       memcpy(pkt->data.ptr, buffer, pkt->data.len);
-
-       /* return packet */
-       *packet = pkt;
-
-       return SUCCESS;
-}
-
-/**
- * implementation of socket_t.send
- */
-status_t sender(private_socket_t *this, packet_t *packet)
-{
-       ssize_t bytes_sent;
-
-
-       this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
-                                               packet->destination->get_address(packet->destination), 
-                                               packet->destination->get_port(packet->destination));
-       /* send data */
-       bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
-                                               0, packet->destination->get_sockaddr(packet->destination), 
-                                               *(packet->destination->get_sockaddr_len(packet->destination)));
-
-       if (bytes_sent != packet->data.len)
-       {
-               this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
-               return FAILED;
-       }
-       return SUCCESS;
-}
-
-/**
- * implementation of socket_t.destroy
- */
-status_t destroy(private_socket_t *this)
-{
-       close(this->socket_fd);
-       global_logger_manager->destroy_logger(global_logger_manager, this->logger);
-       allocator_free(this);
-
-       return SUCCESS;
-}
-
-socket_t *socket_create(u_int16_t port)
-{
-       private_socket_t *this = allocator_alloc_thing(private_socket_t);
-       struct sockaddr_in addr;
-
-       /* public functions */
-       this->public.send = (status_t(*)(socket_t*, packet_t*))sender;
-       this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver;
-       this->public.destroy = (status_t(*)(socket_t*))destroy;
-       
-       
-       this->logger = global_logger_manager->create_logger(global_logger_manager, SOCKET, NULL);
-       if (this->logger == NULL)
-       {
-               allocator_free(this);
-               return NULL;
-       }
-
-       /* create default ipv4 socket */
-       this->socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
-       if (this->socket_fd < 0) 
-       {
-               this->logger->log(this->logger, ERROR, "unable to open socket: %s", strerror(errno));
-               global_logger_manager->destroy_logger(global_logger_manager, this->logger);
-               allocator_free(this);
-               return NULL;
-       }
-
-       /* bind socket to all interfaces */
-       addr.sin_family = AF_INET;
-    addr.sin_addr.s_addr = INADDR_ANY;
-    addr.sin_port = htons(port);
-    if (bind(this->socket_fd,(struct sockaddr*)&addr, sizeof(addr)) < 0) 
-    {
-               this->logger->log(this->logger, ERROR, "unable to bind socket to port %d: %s", port, strerror(errno));
-               global_logger_manager->destroy_logger(global_logger_manager, this->logger);
-               allocator_free(this);
-        return NULL;
-    }
-
-       return (socket_t*)this;
-}
diff --git a/Source/charon/socket.h b/Source/charon/socket.h
deleted file mode 100644 (file)
index b69c50c..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-/**
- * @file socket.h
- * 
- * @brief management of sockets
- * 
- * receiver reads from here, sender writes to here
- * 
- */
-
-/*
- * 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 SOCKET_H_
-#define SOCKET_H_
-
-
-#include <types.h>
-#include <packet.h>
-
-
-/**
- * maximum size of a packet
- * 3000 Bytes should be sufficient, see IKEv2 draft
- */
-#define MAX_PACKET 3000
-
-
-/**
- * @brief abstraction of one (ipv4), or in future, of multiple sockets
- * 
- */
-typedef struct socket_s socket_t;
-struct socket_s {
-       /**
-        * @brief receive a packet
-        * 
-        * reads a packet from one of the sockets.
-        * source will be set, dest not implemented
-        * 
-        * 
-        * @param sock                  socket_t object to work on
-        * @param packet                pinter gets address from allocated packet_t
-        * @return                              FAILED when unable to receive
-        *                                              SUCCESS when packet successfully received
-        */
-       status_t (*receive) (socket_t *sock, packet_t **packet);
-       
-       /**
-        * @brief send a packet
-        * 
-        * sends a packet via desired socket.
-        * uses source and dest in packet.
-        * 
-        * @param sock                  socket_t object to work on
-        * @param packet[out]   packet_t to send
-        * @return                              FAILED when unable to send
-        *                                              SUCCESS when packet successfully sent
-        */
-       status_t (*send) (socket_t *sock, packet_t *packet);
-       
-       /**
-        * @brief destroy sockets
-        * 
-        * close sockets and destroy socket_t object
-        * 
-        * @param sock                  socket_t to destroy
-        * @return                              SUCCESS
-        */
-       status_t (*destroy) (socket_t *sock);
-};
-
-/**
- * @brief socket_t constructor
- * 
- * currently creates one socket, listening on all addresses
- * on port.
- *  
- * @param port                         port to bind socket to
- * @return                             the created socket, or NULL on error
- */
-socket_t *socket_create(u_int16_t port);
-
-
-#endif /*SOCKET_H_*/
index aaf2218..873b88c 100644 (file)
@@ -24,8 +24,8 @@
 
 #include "packet_test.h"
 
-#include <packet.h>
 #include <globals.h>
+#include <network/packet.h>
 #include <utils/allocator.h>
 #include <utils/logger_manager.h>
 
index e333d38..44ea6f3 100644 (file)
 
 #include <globals.h>
 #include <receiver.h>
-#include <packet.h>
-#include <socket.h>
+#include <network/packet.h>
+#include <network/socket.h>
 #include <queues/send_queue.h>
 #include <queues/job_queue.h>
+#include <queues/jobs/incoming_packet_job.h>
 #include <payloads/encodings.h>
 #include <utils/allocator.h>
 
index 7601115..aeb73b2 100644 (file)
@@ -29,6 +29,7 @@
 #include <scheduler.h>
 #include <queues/event_queue.h>
 #include <queues/job_queue.h>
+#include <queues/jobs/incoming_packet_job.h>
 
 
 /**
index c90eb9d..a4ed41b 100644 (file)
@@ -26,8 +26,8 @@
 
 #include <globals.h>
 #include <sender.h>
-#include <packet.h>
-#include <socket.h>
+#include <network/packet.h>
+#include <network/socket.h>
 #include <queues/send_queue.h>
 #include <queues/job_queue.h>
 #include <utils/allocator.h>
index d73be70..69b2d8e 100644 (file)
@@ -25,7 +25,7 @@
 
 #include "socket_test.h"
 
-#include <socket.h>
+#include <network/socket.h>
 #include <utils/allocator.h>
 
 /*
index 876f6c7..0372228 100644 (file)
@@ -29,6 +29,9 @@
  
 #include <globals.h>
 #include <queues/job_queue.h>
+#include <queues/jobs/delete_ike_sa_job.h>
+#include <queues/jobs/incoming_packet_job.h>
+#include <queues/jobs/initiate_ike_sa_job.h>
 #include <utils/allocator.h>
 #include <utils/logger.h>