diff options
author | Martin Willi <martin@strongswan.org> | 2005-11-23 09:43:14 +0000 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2005-11-23 09:43:14 +0000 |
commit | 716abc9f8332c040df6e296104fd11ec5ac6b8cc (patch) | |
tree | 030e2efe2f5d8ecc940fd463d9250ee912714b34 /Source/charon/network | |
parent | 7cf14676dbd2d54203b78044b2b8da38d2bfb315 (diff) | |
download | strongswan-716abc9f8332c040df6e296104fd11ec5ac6b8cc.tar.bz2 strongswan-716abc9f8332c040df6e296104fd11ec5ac6b8cc.tar.xz |
- moved packet and socket in new network-package
Diffstat (limited to 'Source/charon/network')
-rw-r--r-- | Source/charon/network/packet.c | 130 | ||||
-rw-r--r-- | Source/charon/network/packet.h | 80 | ||||
-rw-r--r-- | Source/charon/network/socket.c | 186 | ||||
-rw-r--r-- | Source/charon/network/socket.h | 96 |
4 files changed, 492 insertions, 0 deletions
diff --git a/Source/charon/network/packet.c b/Source/charon/network/packet.c new file mode 100644 index 000000000..f09b18ae3 --- /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 <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 index 000000000..10488e0aa --- /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 <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 index 000000000..fc652b557 --- /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 <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 index 000000000..d5fd828ed --- /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 <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_*/ |