From 716abc9f8332c040df6e296104fd11ec5ac6b8cc Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 23 Nov 2005 09:43:14 +0000 Subject: [PATCH] - moved packet and socket in new network-package --- Source/charon/daemon.c | 24 +-- Source/charon/globals.h | 2 +- Source/charon/message.h | 2 +- Source/charon/network/packet.c | 130 +++++++++++++++++ Source/charon/network/packet.h | 80 ++++++++++ Source/charon/network/socket.c | 186 ++++++++++++++++++++++++ Source/charon/network/socket.h | 96 ++++++++++++ Source/charon/packet.c | 130 ----------------- Source/charon/packet.h | 80 ---------- Source/charon/queues/jobs/incoming_packet_job.h | 2 +- Source/charon/queues/jobs/job.h | 5 - Source/charon/queues/send_queue.h | 2 +- Source/charon/receiver.c | 4 +- Source/charon/sender.c | 4 +- Source/charon/socket.c | 186 ------------------------ Source/charon/socket.h | 96 ------------ Source/charon/testcases/packet_test.c | 2 +- Source/charon/testcases/receiver_test.c | 5 +- Source/charon/testcases/scheduler_test.c | 1 + Source/charon/testcases/sender_test.c | 4 +- Source/charon/testcases/socket_test.c | 2 +- Source/charon/thread_pool.c | 3 + 22 files changed, 523 insertions(+), 523 deletions(-) create mode 100644 Source/charon/network/packet.c create mode 100644 Source/charon/network/packet.h create mode 100644 Source/charon/network/socket.c create mode 100644 Source/charon/network/socket.h delete mode 100644 Source/charon/packet.c delete mode 100644 Source/charon/packet.h delete mode 100644 Source/charon/socket.c delete mode 100644 Source/charon/socket.h diff --git a/Source/charon/daemon.c b/Source/charon/daemon.c index b3e9b61..277d8c6 100644 --- a/Source/charon/daemon.c +++ b/Source/charon/daemon.c @@ -27,18 +27,18 @@ #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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* function declaration (defined and described after main function) */ diff --git a/Source/charon/globals.h b/Source/charon/globals.h index 137ac0d..d3cfb08 100644 --- a/Source/charon/globals.h +++ b/Source/charon/globals.h @@ -23,10 +23,10 @@ #ifndef GLOBALS_H_ #define GLOBALS_H_ -#include #include #include #include +#include #include #include #include diff --git a/Source/charon/message.h b/Source/charon/message.h index 4850250..0385bd7 100644 --- a/Source/charon/message.h +++ b/Source/charon/message.h @@ -24,8 +24,8 @@ #define MESSAGE_H_ #include -#include #include +#include #include #include diff --git a/Source/charon/network/packet.c b/Source/charon/network/packet.c new file mode 100644 index 0000000..f09b18a --- /dev/null +++ b/Source/charon/network/packet.c @@ -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 . + * + * 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 + + +/** + * 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 index 0000000..10488e0 --- /dev/null +++ b/Source/charon/network/packet.h @@ -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 . + * + * 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 +#include + + + +/** + * @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 index 0000000..fc652b5 --- /dev/null +++ b/Source/charon/network/socket.c @@ -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 . + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "socket.h" + +#include +#include +#include + +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 index 0000000..d5fd828 --- /dev/null +++ b/Source/charon/network/socket.h @@ -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 . + * + * 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 +#include + + +/** + * 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 index f09b18a..0000000 --- a/Source/charon/packet.c +++ /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 . - * - * 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 - - -/** - * 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 index 10488e0..0000000 --- a/Source/charon/packet.h +++ /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 . - * - * 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 -#include - - - -/** - * @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/queues/jobs/incoming_packet_job.h b/Source/charon/queues/jobs/incoming_packet_job.h index 5e5cb34..51842cf 100644 --- a/Source/charon/queues/jobs/incoming_packet_job.h +++ b/Source/charon/queues/jobs/incoming_packet_job.h @@ -24,7 +24,7 @@ #define INCOMING_PACKET_JOB_H_ #include -#include +#include #include /** diff --git a/Source/charon/queues/jobs/job.h b/Source/charon/queues/jobs/job.h index ad8f59a..59b0f7b 100644 --- a/Source/charon/queues/jobs/job.h +++ b/Source/charon/queues/jobs/job.h @@ -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_*/ diff --git a/Source/charon/queues/send_queue.h b/Source/charon/queues/send_queue.h index 7d2592a..c1f643c 100644 --- a/Source/charon/queues/send_queue.h +++ b/Source/charon/queues/send_queue.h @@ -24,7 +24,7 @@ #define SEND_QUEUE_H_ #include -#include +#include /** * @brief Send-Queue diff --git a/Source/charon/receiver.c b/Source/charon/receiver.c index 3f77eb5..ba7e229 100644 --- a/Source/charon/receiver.c +++ b/Source/charon/receiver.c @@ -25,9 +25,9 @@ #include "receiver.h" -#include -#include #include +#include +#include #include #include #include diff --git a/Source/charon/sender.c b/Source/charon/sender.c index 074d30b..d237696 100644 --- a/Source/charon/sender.c +++ b/Source/charon/sender.c @@ -25,9 +25,9 @@ #include "sender.h" -#include -#include #include +#include +#include #include #include #include diff --git a/Source/charon/socket.c b/Source/charon/socket.c deleted file mode 100644 index fc652b5..0000000 --- a/Source/charon/socket.c +++ /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 . - * - * 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 -#include -#include -#include -#include -#include -#include - -#include "socket.h" - -#include -#include -#include - -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 index b69c50c..0000000 --- a/Source/charon/socket.h +++ /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 . - * - * 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 -#include - - -/** - * 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/testcases/packet_test.c b/Source/charon/testcases/packet_test.c index aaf2218..873b88c 100644 --- a/Source/charon/testcases/packet_test.c +++ b/Source/charon/testcases/packet_test.c @@ -24,8 +24,8 @@ #include "packet_test.h" -#include #include +#include #include #include diff --git a/Source/charon/testcases/receiver_test.c b/Source/charon/testcases/receiver_test.c index e333d38..44ea6f3 100644 --- a/Source/charon/testcases/receiver_test.c +++ b/Source/charon/testcases/receiver_test.c @@ -27,10 +27,11 @@ #include #include -#include -#include +#include +#include #include #include +#include #include #include diff --git a/Source/charon/testcases/scheduler_test.c b/Source/charon/testcases/scheduler_test.c index 7601115..aeb73b2 100644 --- a/Source/charon/testcases/scheduler_test.c +++ b/Source/charon/testcases/scheduler_test.c @@ -29,6 +29,7 @@ #include #include #include +#include /** diff --git a/Source/charon/testcases/sender_test.c b/Source/charon/testcases/sender_test.c index c90eb9d..a4ed41b 100644 --- a/Source/charon/testcases/sender_test.c +++ b/Source/charon/testcases/sender_test.c @@ -26,8 +26,8 @@ #include #include -#include -#include +#include +#include #include #include #include diff --git a/Source/charon/testcases/socket_test.c b/Source/charon/testcases/socket_test.c index d73be70..69b2d8e 100644 --- a/Source/charon/testcases/socket_test.c +++ b/Source/charon/testcases/socket_test.c @@ -25,7 +25,7 @@ #include "socket_test.h" -#include +#include #include /* diff --git a/Source/charon/thread_pool.c b/Source/charon/thread_pool.c index 876f6c7..0372228 100644 --- a/Source/charon/thread_pool.c +++ b/Source/charon/thread_pool.c @@ -29,6 +29,9 @@ #include #include +#include +#include +#include #include #include -- 2.7.4