Author: tjansen Date: Thu Oct 15 13:01:26 2009 New Revision: 1156 Log: Split the timeout code from cdtun.c to a separate file before integrating it into the scheduler later on. Added: trunk/pisacd/cdtimeout.c - copied, changed from r1144, trunk/pisacd/cdtun.c trunk/pisacd/cdtimeout.h - copied, changed from r1144, trunk/pisacd/cdtun.h Modified: trunk/pisacd/Makefile.am trunk/pisacd/cdmain.c trunk/pisacd/cdtun.c trunk/pisacd/cdtun.h Modified: trunk/pisacd/Makefile.am ============================================================================== --- trunk/pisacd/Makefile.am Thu Oct 15 12:14:06 2009 (r1155) +++ trunk/pisacd/Makefile.am Thu Oct 15 13:01:26 2009 (r1156) @@ -33,8 +33,8 @@ LDADD += -lm pisacd_SOURCES = cdmain.c cdconf.c cdconmgr.c cdctx.c cdderegister.c \ cdheartbeat.c cdmsg.c cdpending.c cdregister.c cdservers.c \ - cdtun.c pisacdconf.c cdconfhandlers.c + cdtimeout.c cdtun.c pisacdconf.c cdconfhandlers.c pisacdconf_SOURCES = pisaconftool.c pisacdconf.c include_HEADERS = cdconf.h cdconfhandlers.h cdconmgr.h cdctx.h cdderegister.h \ cdheartbeat.h cdmsg.h cdpending.h cdregister.h cdservers.h \ - cdtun.h pisacdconf.h cdconfhandlers.h + cdtimeout.h cdtun.h pisacdconf.h cdconfhandlers.h Modified: trunk/pisacd/cdmain.c ============================================================================== --- trunk/pisacd/cdmain.c Thu Oct 15 12:14:06 2009 (r1155) +++ trunk/pisacd/cdmain.c Thu Oct 15 13:01:26 2009 (r1156) @@ -32,6 +32,7 @@ #include "pisaconf.h" #include "cdconfhandlers.h" #include "scheduler.h" +#include "cdtimeout.h" extern pisa_conf_handle_func_set conf_handle_func_set; Copied and modified: trunk/pisacd/cdtimeout.c (from r1144, trunk/pisacd/cdtun.c) ============================================================================== --- trunk/pisacd/cdtun.c Wed Oct 14 16:00:02 2009 (r1144, copy source) +++ trunk/pisacd/cdtimeout.c Thu Oct 15 13:01:26 2009 (r1156) @@ -1,21 +1,16 @@ /* - * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen + * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen * All rights reserved. */ /** - * @file cdtun.c - * @brief Data packet handling in the PISA client daemon. + * @file cdtimeout.c + * @brief Timeout checking task for the PISA client daemon. * @author Wolfram Fischer <papierfalter@xxxxxxxxx> * @author Thomas Jansen <mithi@xxxxxxxxx> - * @date Oct. 2008 + * @date Oct. 2009 */ -#include <netinet/ip.h> -#include <errno.h> - -#include "tunnel.h" -#include "nat.h" #include "cdctx.h" #include "cdconf.h" #include "cdderegister.h" @@ -25,149 +20,6 @@ #include "cdservers.h" /** - * Copy a packet from the data socket to the tunnel device. - * @params socks the file descriptors for the socket and tunnel device - */ -void pisa_cd_copy_from_sock_to_tun(void) -{ - char buffer[MAX_PACKET_BUFFER_TUN]; - ssize_t len; - unsigned int addrlen; - struct sockaddr_in6 from; - pisa_nat_mapping *map; - struct iphdr *hdr; - struct in_addr *srcaddr; - pisa_conmgr_entry *entry; - - addrlen = sizeof (from); - - if ((len = recvfrom (cd_ctx.tund, buffer, sizeof(buffer), 0, (struct sockaddr *) &from, &addrlen)) == -1) { - PISA_ERROR("remote -> local: recvfrom failed: %s\n", strerror(errno)); - return; - } - - /* Check if we have an active connection with the origin */ - entry = pisa_conmgr_findby_address(cd_ctx.conlist, &from.sin6_addr); - if (entry != NULL) { - if (entry->status != PISA_CON_CONNECTED) { - PISA_DEBUG(PL_DATA, "Discarding data from a server that is not in CONNECTED state!\n"); - return; - } - - if (entry->data_port != ntohs(from.sin6_port)) { - PISA_DEBUG(PL_DATA, "Discarding data from the wrong port!\n"); - return; - } - - /* Check for IPv4 in the payload, otherwise drop it */ - if ((hdr = pisa_nat_get_iphdr(buffer)) == NULL) - return; - - /* Apply NAT if needed */ - srcaddr = (struct in_addr *)&hdr->saddr; - map = pisa_nat_mapping_find_by_remote(cd_ctx.natlist, &from.sin6_addr, srcaddr); - if (map != NULL) - pisa_nat_apply(hdr, srcaddr, &map->local_private); - - /* Send the packet out to the tunnel device */ - if (write(cd_ctx.tunnel, buffer + 6, len - 6) == -1) - PISA_ERROR("remote -> local: write failed: %s\n", strerror(errno)); - else - PISA_DEBUG(PL_DATA, "remote -> local: %i bytes\n", len - 6); - } else { - inet_ntop(AF_INET6, &(from.sin6_addr), buffer, sizeof(buffer)); - PISA_DEBUG(PL_DATA, "remote -> local: from unknown server %s\n", buffer); - } -} - -/** - * Copy a packet from the the tunnel device to the data socket. Based on the - * result of NAT we know what the destination address is. - * @params socks the file descriptors for the socket and tunnel device - */ -void pisa_cd_copy_from_tun_to_sock(void) -{ - char buffer[MAX_PACKET_BUFFER_TUN]; - ssize_t len; - struct iphdr *hdr; - struct in_addr *dst; - pisa_conmgr_entry *entry; - pisa_nat_mapping *map; - pisa_mac *mac = (pisa_mac *)buffer; - struct sockaddr_in6 addr; - - if ((len = read(cd_ctx.tunnel, buffer + 6, sizeof(buffer) - 6)) < 0) { - PISA_ERROR("local -> remote: read failed: %s\n", strerror(errno)); - return; - } - - /* Check for IPv4 in the payload, otherwise drop it */ - if ((hdr = pisa_nat_get_iphdr(buffer)) == NULL) { - PISA_DEBUG(PL_DATA, "local -> remote: not an IPv4 packet, dropping.\n"); - return; - } - - pisa_arp_from_ipv4((struct in_addr *)&hdr->saddr, mac); - - /* Apply NAT if needed */ - dst = (struct in_addr *)&hdr->daddr; - map = pisa_nat_mapping_find_by_local_private(cd_ctx.natlist, dst); - if (map != NULL) { - /* found a matching NAT entry, change the destination IPv4 */ - pisa_nat_apply(hdr, dst, &map->remote.ipv4); - entry = map->connection; - } else { - /* No NAT mapping found, just find out which server is the - * destination */ - entry = pisa_conmgr_findby_clientip(cd_ctx.conlist, dst); - } - - /* If neither NAT nor a destination server could be found we have to - * fall back to the default route to the Internet, the primary - * connection. If that fails too, we've run out of options. */ - if (entry == NULL) - entry = cd_ctx.defaultroute; - if (entry == NULL) { - PISA_ERROR("local -> remote: no destination.\n"); - return; - } - - if (entry->status != PISA_CON_CONNECTED) { - /* Connect on demand */ - - PISA_DEBUG(PL_DATA, "Connection is not yet established!\n"); - - /* If we haven't sent REGISTER yet, enqueue it now. */ - if (entry->status != PISA_CON_REGISTERING) - pisa_client_connect_to_server(entry); - - /* TODO: buffer packets until connection was established? */ - } else { - /* Send the packet to the data port of the connection entry. - * We use the control socket as sending socket, because - * on the server side this connection is associated with the - * control socket address and port number! */ - pisa_conmgr_build_sockaddr_data(entry, &addr); - if (sendto(cd_ctx.tund, buffer, len + 6, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) == -1) { - PISA_ERROR("local -> remote: sendto failed: %s\n", strerror(errno)); - } else { - /* Set this variable to the current time, we don't - * have to send a heartbeat until our heartbeat - * timeout ran out from THIS point in time - the - * server takes incoming data as heartbeat */ - time(&entry->timeout_heartbeat); - - /* Keep track of the last time we actively used this - * connection. Disconnect after a timeout to save - * resources. */ - time(&entry->timeout_client); - - PISA_DEBUG(PL_DATA, "local -> remote: %i bytes\n", len); - } - } -} - -/** * Check if a connection was idle for too long and disconnect. * @param data pointer to the current time (only provided this way to avoid * having to call time() several times) Copied and modified: trunk/pisacd/cdtimeout.h (from r1144, trunk/pisacd/cdtun.h) ============================================================================== --- trunk/pisacd/cdtun.h Wed Oct 14 16:00:02 2009 (r1144, copy source) +++ trunk/pisacd/cdtimeout.h Thu Oct 15 13:01:26 2009 (r1156) @@ -1,14 +1,11 @@ /* - * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen + * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen * All rights reserved. */ -#ifndef PISA_CDTUN_H -#define PISA_CDTUN_H - -void pisa_cd_copy_from_sock_to_tun(void); -void pisa_cd_copy_from_tun_to_sock(void); +#ifndef PISA_CDTIMEOUT_H +#define PISA_CDTIMEOUT_H void pisa_cd_timeout_collect(void); -#endif /* PISA_CDTUN_H */ +#endif /* PISA_CDTIMEOUT_H */ Modified: trunk/pisacd/cdtun.c ============================================================================== --- trunk/pisacd/cdtun.c Thu Oct 15 12:14:06 2009 (r1155) +++ trunk/pisacd/cdtun.c Thu Oct 15 13:01:26 2009 (r1156) @@ -166,40 +166,3 @@ } } } - -/** - * Check if a connection was idle for too long and disconnect. - * @param data pointer to the current time (only provided this way to avoid - * having to call time() several times) - * @param entry entry connection which is to be examined - */ -static void pisa_cd_check_timeout(void* data, pisa_conmgr_entry* entry) -{ - char buffer[INET6_ADDRSTRLEN]; - time_t currentTime = *((time_t*)data); - - /* We do not want to drop our relay connection */ - if (entry->type == PISA_CONTYPE_RELAY) - return; - - if ((entry->timeout_client + cd_cfg.idle_disconnect_delay) < currentTime) { - inet_ntop(AF_INET6, &entry->hit, buffer, sizeof(buffer)); - PISA_INFO("Removing idle connection to %s\n", buffer); - pisa_client_disconnect_from_server(NULL, entry); - } -} - -/** - * Disconnect idle connections. Only check every 10 seconds for it. - */ -void pisa_cd_timeout_collect(void) -{ - static time_t last = 0; - time_t current; - - time(¤t); - if (last + 10 < current) { - pisa_conmgr_iterate_connected(cd_ctx.conlist, pisa_cd_check_timeout, ¤t); - last = current; - } -} Modified: trunk/pisacd/cdtun.h ============================================================================== --- trunk/pisacd/cdtun.h Thu Oct 15 12:14:06 2009 (r1155) +++ trunk/pisacd/cdtun.h Thu Oct 15 13:01:26 2009 (r1156) @@ -9,6 +9,4 @@ void pisa_cd_copy_from_sock_to_tun(void); void pisa_cd_copy_from_tun_to_sock(void); -void pisa_cd_timeout_collect(void); - #endif /* PISA_CDTUN_H */