[pisa-src] r1052 - trunk/pisacd

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Thu, 08 Oct 2009 14:05:22 +0200

Author: tjansen
Date: Thu Oct  8 14:05:22 2009
New Revision: 1052

Log:
Splitted cdtun.c into different modules.

cdtun was a multi-purpose file and rather large. Functions have been grouped
by topic and moved into cdregister, cdderegister and cdheartbeat. This
improves readability and ensures that no static functions are revealed to
unrelated modules.

Several includes had to be added or changed in the process.

Added:
   trunk/pisacd/cdderegister.c
      - copied, changed from r1043, trunk/pisacd/cdtun.c
   trunk/pisacd/cdderegister.h
      - copied, changed from r1043, trunk/pisacd/cdtun.h
   trunk/pisacd/cdheartbeat.c
      - copied, changed from r1043, trunk/pisacd/cdtun.c
   trunk/pisacd/cdheartbeat.h
      - copied, changed from r1043, trunk/pisacd/cdtun.h
   trunk/pisacd/cdregister.c
      - copied, changed from r1043, trunk/pisacd/cdtun.c
   trunk/pisacd/cdregister.h
      - copied, changed from r1043, trunk/pisacd/cdtun.h
Modified:
   trunk/pisacd/Makefile.am
   trunk/pisacd/cdmain.c
   trunk/pisacd/cdservers.c
   trunk/pisacd/cdtun.c
   trunk/pisacd/cdtun.h

Modified: trunk/pisacd/Makefile.am
==============================================================================
--- trunk/pisacd/Makefile.am    Thu Oct  8 13:12:57 2009        (r1051)
+++ trunk/pisacd/Makefile.am    Thu Oct  8 14:05:22 2009        (r1052)
@@ -35,8 +35,10 @@
 endif
 
 LDADD += -lm
-pisacd_SOURCES = cdmain.c cdconf.c cdconmgr.c cdctx.c cdmsg.c cdpending.c \
-                cdservers.c cdtun.c pisacdconf.c cdconfhandlers.c
-pisacdconf_SOURCES = pisaconftool.c pisacdconf.c 
-include_HEADERS = cdconf.h cdconmgr.h cdctx.h cdmsg.h cdpending.h cdservers.h \
+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
+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

Copied and modified: trunk/pisacd/cdderegister.c (from r1043, 
trunk/pisacd/cdtun.c)
==============================================================================
--- trunk/pisacd/cdtun.c        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisacd/cdderegister.c Thu Oct  8 14:05:22 2009        (r1052)
@@ -1,14 +1,14 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
- * @file cdtun.c
- * @brief Implementation of tunnel modules in the PISA client daemon.
+ * @file cdderegister.c
+ * @brief Deregister related functions 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>
@@ -39,58 +39,6 @@
 }
 
 /**
- * Handle a pending request of type REGISTER:
- *     1. Log the attempt to connect to that server.
- *     2. Send the REGISTER packet.
- * @param pend pending request
- */
-static void pisa_send_register_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_packet *packet = pisa_alloc_msg();
-
-       assert(pend != NULL);
-       assert(pend->data != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_register_pend in a 
non-REGISTERING state.\n");
-               return;
-       }
-
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_REGISTER, "Sending REGISTER to %s, try %i\n", buffer, 
pend->count);
-
-       memcpy(&packet->body.reg, pend->data, sizeof(pisa_payload_register));
-
-       pisa_set_packet_type(packet, PISA_PKTTYPE_TUN_REGISTER);
-       pisa_set_packet_length(packet, sizeof(pisa_packet_common) + 
sizeof(pisa_payload_register));
-
-       pisa_send_control_packet_ipv6(cd_ctx.tunc, &pend->key.addr, packet);
-       pisa_free_msg(packet);
-}
-
-/**
- * Handle a failed pending request of type REGISTER.
- * @param pend pending request
- */
-static void pisa_send_register_fail(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_register_fail in a 
non-REGISTERING state.\n");
-       } else {
-               inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, 
sizeof(buffer));
-               PISA_DEBUG(PL_REGISTER, "Failed to REGISTER to %s.\n", buffer);
-               pend->entry->status = PISA_CON_DISCONNECTED;
-       }
-}
-
-/**
  * Handle a pending request of type DEREGISTER:
  *     1. Log the attempt to disconnect from that server.
  *     2. Send the DEREGISTER packet.
@@ -160,98 +108,6 @@
 }
 
 /**
- * Connect to a PISA server.
- * @param entry conmgr entry to connect to
- */
-void pisa_client_connect_to_server(pisa_conmgr_entry *entry)
-{
-       pisa_payload_register *reg;
-       pisa_pending *pend;
-
-       assert(entry != NULL);
-
-       /* TODO Thomas: What about the DEREGISTERING state? */
-       if (entry->status != PISA_CON_DISCONNECTED)
-               return;
-
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_REGISTER);
-       pend->func = pisa_send_register_pend;
-       pend->fail = pisa_send_register_fail;
-       pisa_pending_reschedule(pend, 0);
-
-       reg = malloc(sizeof(pisa_payload_register));
-       memset(reg, 0, sizeof(pisa_payload_register));
-       pend->data = reg;
-       reg->type = htons(entry->type);
-       if (entry->type == PISA_CONTYPE_SERVICE)
-               pisa_ipv4_copy(&reg->local, &cd_cfg.local_ipv4);
-
-       entry->status = PISA_CON_REGISTERING;
-}
-
-/**
- * Set up a default route with a gateway.
- * @param ipAddress gateway IPv4 address
- */
-static void pisa_set_default_route(struct in_addr *ipv4)
-{
-       char buffer[1024]={0}, ip[INET_ADDRSTRLEN];
-
-       inet_ntop(AF_INET, ipv4, ip, sizeof(ip));
-       snprintf(buffer, sizeof(buffer), "route add default gw %s", ip);
-       if (system(buffer) < 0)
-               PISA_ERROR("Can't add default route!\n");
-
-}
-
-/**
- * Handle a REGISTER_ACK packet from a server:
- *     1. Add the server to the conmgr hash of active connections.
- *     2. Set up the default route.
- *     3. Log the new connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_register_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_pending *pend;
-       pisa_conmgr_entry *entry;
-
-       entry = pisa_conmgr_findby_address(cd_ctx.conlist, &addr->sin6_addr);
-       if (entry == NULL) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received REGISTER_ACK from unknown 
connection, ignoring it.\n");
-               return;
-       }
-
-       if (entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received REGISTER_ACK in a 
non-REGISTERING state, ignoring it.\n");
-               return;
-       }
-
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_REGISTER);
-       if (pend == NULL) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received unexpected REGISTER_ACK, 
ignoring it.\n");
-               return;
-       }
-
-       pisa_conmgr_connected(cd_ctx.conlist, entry, 
PISASD_DEFAULT_PORTNUM_DATA, &pkt->body.reg_ack.client);
-       entry->status = PISA_CON_CONNECTED;
-       if (entry->type == PISA_CONTYPE_RELAY) {
-               /* set up a second IPv4 address and a default route for the
-                * primary connection */
-               pisa_tunnel_configure_sub(cd_ctx.ifname_tunnel, 
&pkt->body.reg_ack.client);
-               pisa_set_default_route(&pkt->body.reg_ack.server);
-               cd_ctx.defaultroute = entry;
-       }
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_REGISTER, "Connected to PISA server %s\n", buffer);
-
-       pisa_pending_remove(pend);
-}
-
-/**
  * Delete the default route.
  */
 static void pisa_remove_default_route(void)
@@ -288,275 +144,3 @@
        inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
        PISA_INFO("Disconnected from PISA server %s\n", buffer);
 }
-
-/**
- * Handle a HEARTBEAT_ACK packet from a server. Update the last received time
- * for that connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_heartbeat_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&addr->sin6_addr);
-       pisa_pending *pend;
-
-       if (entry == NULL)
-               return;
-
-       time(&entry->timeout_heartbeat);
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_HEARTBEAT);
-       if (pend)
-               pisa_pending_remove(pend);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_HEARTBEAT, "Heartbeat Ack from PISA server %s\n", buffer);
-}
-
-/**
- * Handle a pending request of type HEARTBEAT.
- * @param pend pending request
- */
-static void pisa_send_heartbeat_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry;
-       struct sockaddr_in6 addr = {0};
-
-       assert(pend != NULL);
-
-       entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&pend->key.addr.sin6_addr);
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_HEARTBEAT, "Sending HEARTBEAT to %s, try %i\n", buffer, 
pend->count);
-       pisa_conmgr_build_sockaddr_control(entry, &addr);
-       pisa_send_control_packet_ipv6_type(cd_ctx.tunc, &addr, 
PISA_PKTTYPE_TUN_HEARTBEAT);
-}
-
-/**
- * Handle a failed pending request of type HEARTBEAT.
- * @param pend pending request
- */
-static void pisa_send_heartbeat_fail(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-
-       assert(pend != NULL);
-
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_INFO("HEARTBEAT failed to %s, disconnecting.\n", buffer);
-       pisa_client_disconnect_from_server(NULL, 
pisa_conmgr_findby_address(cd_ctx.conlist, &pend->key.addr.sin6_addr));
-}
-
-/**
- * Check if a connection was idle long enough to send a heartbeat. Current
- * time is passed to avoid multiple time() calls in a row.
- * @param data pointer to the current time in time_t format.
- * @param entry conmgr entry for the server
- */
-static void pisa_client_send_heartbeat(void *data, pisa_conmgr_entry *entry)
-{
-       pisa_pending *pend;
-       time_t *t = (time_t *)data;
-       char buffer[INET6_ADDRSTRLEN]={0};
-
-       inet_ntop(AF_INET6, &entry->hit, buffer, sizeof(buffer));
-       if (entry->timeout_heartbeat + 20 < *t) {
-               pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_HEARTBEAT);
-               pend->func = pisa_send_heartbeat_pend;
-               pend->fail = pisa_send_heartbeat_fail;
-               pisa_pending_reschedule(pend, 0);
-               entry->timeout_heartbeat = *t;
-       }
-}
-
-/**
- * Check all connections for their last received time to see if we need to
- * send a heartbeat.
- */
-void pisa_client_heartbeat_all(void)
-{
-       time_t t;
-
-       time(&t);
-       pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_client_send_heartbeat, &t);
-}
-
-/**
- * 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] = {0};
-       ssize_t len = 0;
-       unsigned int addrlen = 0;
-       struct sockaddr_in6 from = {0};
-       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] = {0};
-       ssize_t len = 0;
-       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 = {0};
-
-       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)
- * @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(&current);
-       if (last + 10 < current) {
-               pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_cd_check_timeout, &current);
-               last = current;
-       }
-}

Copied and modified: trunk/pisacd/cdderegister.h (from r1043, 
trunk/pisacd/cdtun.h)
==============================================================================
--- trunk/pisacd/cdtun.h        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisacd/cdderegister.h Thu Oct  8 14:05:22 2009        (r1052)
@@ -1,27 +1,18 @@
 /*
- * 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
+#ifndef PISA_CDDEREGISTER_H
+#define PISA_CDDEREGISTER_H
 
 #include "conmgr.h"
 #include "packet.h"
 
+void pisa_client_disconnect_from_server(void *data, pisa_conmgr_entry *entry);
 void pisa_client_disconnect_from_all_servers(void);
-void pisa_client_heartbeat_all(void);
-
-void pisa_cd_copy_from_sock_to_tun(void);
-void pisa_cd_copy_from_tun_to_sock(void);
-
-void pisa_client_connect_to_server(pisa_conmgr_entry *entry);
-
-void pisa_cd_timeout_collect(void);
 
 /* handlers for tunnel control packets */
-void pisa_recv_register_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
 void pisa_recv_deregister_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_heartbeat_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
 
-#endif /* PISA_CDTUN_H */
+#endif /* PISA_CDDEREGISTER_H */

Copied and modified: trunk/pisacd/cdheartbeat.c (from r1043, 
trunk/pisacd/cdtun.c)
==============================================================================
--- trunk/pisacd/cdtun.c        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisacd/cdheartbeat.c  Thu Oct  8 14:05:22 2009        (r1052)
@@ -1,14 +1,14 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
  * @file cdtun.c
- * @brief Implementation of tunnel modules in the PISA client daemon.
+ * @brief Heartbeat related functions 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>
@@ -18,277 +18,11 @@
 #include "nat.h"
 #include "cdctx.h"
 #include "cdconf.h"
+#include "cdderegister.h"
 #include "conmgr.h"
 #include "cdpending.h"
 #include "cdservers.h"
 
-static void pisa_remove_default_route(void);
-
-/**
- * Remove a server connection from our conmgr module
- * and take care of the default route.
- * @param entry Non-Null pointer to pisa_conmgr_entry
- */
-static void pisa_remove_server_connection(pisa_conmgr_entry* entry)
-{
-       assert(entry != NULL);
-
-       if (entry == cd_ctx.defaultroute)
-               pisa_remove_default_route();
-       entry->status = PISA_CON_DISCONNECTED;
-}
-
-/**
- * Handle a pending request of type REGISTER:
- *     1. Log the attempt to connect to that server.
- *     2. Send the REGISTER packet.
- * @param pend pending request
- */
-static void pisa_send_register_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_packet *packet = pisa_alloc_msg();
-
-       assert(pend != NULL);
-       assert(pend->data != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_register_pend in a 
non-REGISTERING state.\n");
-               return;
-       }
-
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_REGISTER, "Sending REGISTER to %s, try %i\n", buffer, 
pend->count);
-
-       memcpy(&packet->body.reg, pend->data, sizeof(pisa_payload_register));
-
-       pisa_set_packet_type(packet, PISA_PKTTYPE_TUN_REGISTER);
-       pisa_set_packet_length(packet, sizeof(pisa_packet_common) + 
sizeof(pisa_payload_register));
-
-       pisa_send_control_packet_ipv6(cd_ctx.tunc, &pend->key.addr, packet);
-       pisa_free_msg(packet);
-}
-
-/**
- * Handle a failed pending request of type REGISTER.
- * @param pend pending request
- */
-static void pisa_send_register_fail(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_register_fail in a 
non-REGISTERING state.\n");
-       } else {
-               inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, 
sizeof(buffer));
-               PISA_DEBUG(PL_REGISTER, "Failed to REGISTER to %s.\n", buffer);
-               pend->entry->status = PISA_CON_DISCONNECTED;
-       }
-}
-
-/**
- * Handle a pending request of type DEREGISTER:
- *     1. Log the attempt to disconnect from that server.
- *     2. Send the DEREGISTER packet.
- * @param pend pending request
- */
-static void pisa_send_deregister_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       struct sockaddr_in6 addr = {0};
-
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_DEREGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_deregister_pend in a 
non-DEREGISTERING state.\n");
-       } else {
-               inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, 
sizeof(buffer));
-               PISA_DEBUG(PL_DEREGISTER, "Sending DEREGISTER to %s, try %i\n", 
buffer, pend->count);
-               pisa_conmgr_build_sockaddr_control(pend->entry, &addr);
-               pisa_send_control_packet_ipv6_type(cd_ctx.tunc, &addr, 
PISA_PKTTYPE_TUN_DEREGISTER);
-       }
-}
-
-/**
- * Handle a failed pending request of type DEREGISTER.
- * @param pend pending request
- */
-static void pisa_send_deregister_fail(pisa_pending *pend)
-{
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_DEREGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_deregister_fail in a 
non-DEREGISTERING state.\n");
-       } else {
-               pisa_remove_server_connection(pend->entry);
-       }
-}
-
-/**
- * Add a pending request for disconnecting from a server.
- * @param data NULL, not used, but required to use this function as a callback
- *     for pisa_conmgr_iterate_connected.
- * @param entry conmgr entry for the server
- */
-void pisa_client_disconnect_from_server(void *data, pisa_conmgr_entry *entry)
-{
-       pisa_pending *pend;
-
-       assert(entry != NULL);
-
-       entry->status = PISA_CON_DEREGISTERING;
-
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_DEREGISTER);
-       pend->func = pisa_send_deregister_pend;
-       pend->fail = pisa_send_deregister_fail;
-       pisa_pending_reschedule(pend, 0);
-}
-
-/**
- * Close all connections to PISA servers. Used for cleanup on exit.
- */
-void pisa_client_disconnect_from_all_servers(void)
-{
-       pisa_pending_remove_all();
-       pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_client_disconnect_from_server, NULL);
-}
-
-/**
- * Connect to a PISA server.
- * @param entry conmgr entry to connect to
- */
-void pisa_client_connect_to_server(pisa_conmgr_entry *entry)
-{
-       pisa_payload_register *reg;
-       pisa_pending *pend;
-
-       assert(entry != NULL);
-
-       /* TODO Thomas: What about the DEREGISTERING state? */
-       if (entry->status != PISA_CON_DISCONNECTED)
-               return;
-
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_REGISTER);
-       pend->func = pisa_send_register_pend;
-       pend->fail = pisa_send_register_fail;
-       pisa_pending_reschedule(pend, 0);
-
-       reg = malloc(sizeof(pisa_payload_register));
-       memset(reg, 0, sizeof(pisa_payload_register));
-       pend->data = reg;
-       reg->type = htons(entry->type);
-       if (entry->type == PISA_CONTYPE_SERVICE)
-               pisa_ipv4_copy(&reg->local, &cd_cfg.local_ipv4);
-
-       entry->status = PISA_CON_REGISTERING;
-}
-
-/**
- * Set up a default route with a gateway.
- * @param ipAddress gateway IPv4 address
- */
-static void pisa_set_default_route(struct in_addr *ipv4)
-{
-       char buffer[1024]={0}, ip[INET_ADDRSTRLEN];
-
-       inet_ntop(AF_INET, ipv4, ip, sizeof(ip));
-       snprintf(buffer, sizeof(buffer), "route add default gw %s", ip);
-       if (system(buffer) < 0)
-               PISA_ERROR("Can't add default route!\n");
-
-}
-
-/**
- * Handle a REGISTER_ACK packet from a server:
- *     1. Add the server to the conmgr hash of active connections.
- *     2. Set up the default route.
- *     3. Log the new connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_register_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_pending *pend;
-       pisa_conmgr_entry *entry;
-
-       entry = pisa_conmgr_findby_address(cd_ctx.conlist, &addr->sin6_addr);
-       if (entry == NULL) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received REGISTER_ACK from unknown 
connection, ignoring it.\n");
-               return;
-       }
-
-       if (entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received REGISTER_ACK in a 
non-REGISTERING state, ignoring it.\n");
-               return;
-       }
-
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_REGISTER);
-       if (pend == NULL) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received unexpected REGISTER_ACK, 
ignoring it.\n");
-               return;
-       }
-
-       pisa_conmgr_connected(cd_ctx.conlist, entry, 
PISASD_DEFAULT_PORTNUM_DATA, &pkt->body.reg_ack.client);
-       entry->status = PISA_CON_CONNECTED;
-       if (entry->type == PISA_CONTYPE_RELAY) {
-               /* set up a second IPv4 address and a default route for the
-                * primary connection */
-               pisa_tunnel_configure_sub(cd_ctx.ifname_tunnel, 
&pkt->body.reg_ack.client);
-               pisa_set_default_route(&pkt->body.reg_ack.server);
-               cd_ctx.defaultroute = entry;
-       }
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_REGISTER, "Connected to PISA server %s\n", buffer);
-
-       pisa_pending_remove(pend);
-}
-
-/**
- * Delete the default route.
- */
-static void pisa_remove_default_route(void)
-{
-       const char* deleteDefaultRoute="route del default"; /* Perhaps this 
works only with LINUX */    
-       PISA_DEBUG(PL_GENERIC, "remove default route\n");
-       if (system(deleteDefaultRoute) < 0)
-               PISA_ERROR("ERROR: %s: Can't delete default route!\n", 
__FUNCTION__);
-}
-
-/**
- * Handle a DEREGISTER_ACK packet from a server:
- *     1. Remove the server from the conmgr hash of active connections.
- *     2. Remove the default route.
- *     3. Log the removed connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_deregister_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&addr->sin6_addr);
-       pisa_pending *pend;
-
-       if (entry == NULL)
-               return;
-
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_DEREGISTER);
-       if (pend != NULL)
-               pisa_pending_remove(pend);
-
-       pisa_remove_server_connection(entry);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_INFO("Disconnected from PISA server %s\n", buffer);
-}
-
 /**
  * Handle a HEARTBEAT_ACK packet from a server. Update the last received time
  * for that connection.
@@ -380,183 +114,3 @@
        time(&t);
        pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_client_send_heartbeat, &t);
 }
-
-/**
- * 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] = {0};
-       ssize_t len = 0;
-       unsigned int addrlen = 0;
-       struct sockaddr_in6 from = {0};
-       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] = {0};
-       ssize_t len = 0;
-       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 = {0};
-
-       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)
- * @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(&current);
-       if (last + 10 < current) {
-               pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_cd_check_timeout, &current);
-               last = current;
-       }
-}

Copied and modified: trunk/pisacd/cdheartbeat.h (from r1043, 
trunk/pisacd/cdtun.h)
==============================================================================
--- trunk/pisacd/cdtun.h        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisacd/cdheartbeat.h  Thu Oct  8 14:05:22 2009        (r1052)
@@ -1,27 +1,16 @@
 /*
- * 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
+#ifndef PISA_CDHEARTBEAT_H
+#define PISA_CDHEARTBEAT_H
 
-#include "conmgr.h"
 #include "packet.h"
 
-void pisa_client_disconnect_from_all_servers(void);
 void pisa_client_heartbeat_all(void);
 
-void pisa_cd_copy_from_sock_to_tun(void);
-void pisa_cd_copy_from_tun_to_sock(void);
-
-void pisa_client_connect_to_server(pisa_conmgr_entry *entry);
-
-void pisa_cd_timeout_collect(void);
-
 /* handlers for tunnel control packets */
-void pisa_recv_register_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_deregister_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
 void pisa_recv_heartbeat_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
 
-#endif /* PISA_CDTUN_H */
+#endif /* PISA_CDHEARTBEAT_H */

Modified: trunk/pisacd/cdmain.c
==============================================================================
--- trunk/pisacd/cdmain.c       Thu Oct  8 13:12:57 2009        (r1051)
+++ trunk/pisacd/cdmain.c       Thu Oct  8 14:05:22 2009        (r1052)
@@ -22,6 +22,9 @@
 #include "cdctx.h"
 #include "cdmsg.h"
 #include "cdtun.h"
+#include "cdderegister.h"
+#include "cdheartbeat.h"
+#include "cdregister.h"
 #include "cdpending.h"
 #include "ctrlhandler.h"
 #include "cdservers.h"

Copied and modified: trunk/pisacd/cdregister.c (from r1043, 
trunk/pisacd/cdtun.c)
==============================================================================
--- trunk/pisacd/cdtun.c        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisacd/cdregister.c   Thu Oct  8 14:05:22 2009        (r1052)
@@ -1,14 +1,14 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
- * @file cdtun.c
- * @brief Implementation of tunnel modules in the PISA client daemon.
+ * @file cdregister.c
+ * @brief Register related functions 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>
@@ -22,22 +22,6 @@
 #include "cdpending.h"
 #include "cdservers.h"
 
-static void pisa_remove_default_route(void);
-
-/**
- * Remove a server connection from our conmgr module
- * and take care of the default route.
- * @param entry Non-Null pointer to pisa_conmgr_entry
- */
-static void pisa_remove_server_connection(pisa_conmgr_entry* entry)
-{
-       assert(entry != NULL);
-
-       if (entry == cd_ctx.defaultroute)
-               pisa_remove_default_route();
-       entry->status = PISA_CON_DISCONNECTED;
-}
-
 /**
  * Handle a pending request of type REGISTER:
  *     1. Log the attempt to connect to that server.
@@ -91,75 +75,6 @@
 }
 
 /**
- * Handle a pending request of type DEREGISTER:
- *     1. Log the attempt to disconnect from that server.
- *     2. Send the DEREGISTER packet.
- * @param pend pending request
- */
-static void pisa_send_deregister_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       struct sockaddr_in6 addr = {0};
-
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_DEREGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_deregister_pend in a 
non-DEREGISTERING state.\n");
-       } else {
-               inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, 
sizeof(buffer));
-               PISA_DEBUG(PL_DEREGISTER, "Sending DEREGISTER to %s, try %i\n", 
buffer, pend->count);
-               pisa_conmgr_build_sockaddr_control(pend->entry, &addr);
-               pisa_send_control_packet_ipv6_type(cd_ctx.tunc, &addr, 
PISA_PKTTYPE_TUN_DEREGISTER);
-       }
-}
-
-/**
- * Handle a failed pending request of type DEREGISTER.
- * @param pend pending request
- */
-static void pisa_send_deregister_fail(pisa_pending *pend)
-{
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_DEREGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_deregister_fail in a 
non-DEREGISTERING state.\n");
-       } else {
-               pisa_remove_server_connection(pend->entry);
-       }
-}
-
-/**
- * Add a pending request for disconnecting from a server.
- * @param data NULL, not used, but required to use this function as a callback
- *     for pisa_conmgr_iterate_connected.
- * @param entry conmgr entry for the server
- */
-void pisa_client_disconnect_from_server(void *data, pisa_conmgr_entry *entry)
-{
-       pisa_pending *pend;
-
-       assert(entry != NULL);
-
-       entry->status = PISA_CON_DEREGISTERING;
-
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_DEREGISTER);
-       pend->func = pisa_send_deregister_pend;
-       pend->fail = pisa_send_deregister_fail;
-       pisa_pending_reschedule(pend, 0);
-}
-
-/**
- * Close all connections to PISA servers. Used for cleanup on exit.
- */
-void pisa_client_disconnect_from_all_servers(void)
-{
-       pisa_pending_remove_all();
-       pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_client_disconnect_from_server, NULL);
-}
-
-/**
  * Connect to a PISA server.
  * @param entry conmgr entry to connect to
  */
@@ -250,313 +165,3 @@
 
        pisa_pending_remove(pend);
 }
-
-/**
- * Delete the default route.
- */
-static void pisa_remove_default_route(void)
-{
-       const char* deleteDefaultRoute="route del default"; /* Perhaps this 
works only with LINUX */    
-       PISA_DEBUG(PL_GENERIC, "remove default route\n");
-       if (system(deleteDefaultRoute) < 0)
-               PISA_ERROR("ERROR: %s: Can't delete default route!\n", 
__FUNCTION__);
-}
-
-/**
- * Handle a DEREGISTER_ACK packet from a server:
- *     1. Remove the server from the conmgr hash of active connections.
- *     2. Remove the default route.
- *     3. Log the removed connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_deregister_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&addr->sin6_addr);
-       pisa_pending *pend;
-
-       if (entry == NULL)
-               return;
-
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_DEREGISTER);
-       if (pend != NULL)
-               pisa_pending_remove(pend);
-
-       pisa_remove_server_connection(entry);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_INFO("Disconnected from PISA server %s\n", buffer);
-}
-
-/**
- * Handle a HEARTBEAT_ACK packet from a server. Update the last received time
- * for that connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_heartbeat_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&addr->sin6_addr);
-       pisa_pending *pend;
-
-       if (entry == NULL)
-               return;
-
-       time(&entry->timeout_heartbeat);
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_HEARTBEAT);
-       if (pend)
-               pisa_pending_remove(pend);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_HEARTBEAT, "Heartbeat Ack from PISA server %s\n", buffer);
-}
-
-/**
- * Handle a pending request of type HEARTBEAT.
- * @param pend pending request
- */
-static void pisa_send_heartbeat_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry;
-       struct sockaddr_in6 addr = {0};
-
-       assert(pend != NULL);
-
-       entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&pend->key.addr.sin6_addr);
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_HEARTBEAT, "Sending HEARTBEAT to %s, try %i\n", buffer, 
pend->count);
-       pisa_conmgr_build_sockaddr_control(entry, &addr);
-       pisa_send_control_packet_ipv6_type(cd_ctx.tunc, &addr, 
PISA_PKTTYPE_TUN_HEARTBEAT);
-}
-
-/**
- * Handle a failed pending request of type HEARTBEAT.
- * @param pend pending request
- */
-static void pisa_send_heartbeat_fail(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-
-       assert(pend != NULL);
-
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_INFO("HEARTBEAT failed to %s, disconnecting.\n", buffer);
-       pisa_client_disconnect_from_server(NULL, 
pisa_conmgr_findby_address(cd_ctx.conlist, &pend->key.addr.sin6_addr));
-}
-
-/**
- * Check if a connection was idle long enough to send a heartbeat. Current
- * time is passed to avoid multiple time() calls in a row.
- * @param data pointer to the current time in time_t format.
- * @param entry conmgr entry for the server
- */
-static void pisa_client_send_heartbeat(void *data, pisa_conmgr_entry *entry)
-{
-       pisa_pending *pend;
-       time_t *t = (time_t *)data;
-       char buffer[INET6_ADDRSTRLEN]={0};
-
-       inet_ntop(AF_INET6, &entry->hit, buffer, sizeof(buffer));
-       if (entry->timeout_heartbeat + 20 < *t) {
-               pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_HEARTBEAT);
-               pend->func = pisa_send_heartbeat_pend;
-               pend->fail = pisa_send_heartbeat_fail;
-               pisa_pending_reschedule(pend, 0);
-               entry->timeout_heartbeat = *t;
-       }
-}
-
-/**
- * Check all connections for their last received time to see if we need to
- * send a heartbeat.
- */
-void pisa_client_heartbeat_all(void)
-{
-       time_t t;
-
-       time(&t);
-       pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_client_send_heartbeat, &t);
-}
-
-/**
- * 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] = {0};
-       ssize_t len = 0;
-       unsigned int addrlen = 0;
-       struct sockaddr_in6 from = {0};
-       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] = {0};
-       ssize_t len = 0;
-       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 = {0};
-
-       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)
- * @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(&current);
-       if (last + 10 < current) {
-               pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_cd_check_timeout, &current);
-               last = current;
-       }
-}

Copied and modified: trunk/pisacd/cdregister.h (from r1043, 
trunk/pisacd/cdtun.h)
==============================================================================
--- trunk/pisacd/cdtun.h        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisacd/cdregister.h   Thu Oct  8 14:05:22 2009        (r1052)
@@ -1,27 +1,17 @@
 /*
- * 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
+#ifndef PISA_CDREGISTER_H
+#define PISA_CDREGISTER_H
 
 #include "conmgr.h"
 #include "packet.h"
 
-void pisa_client_disconnect_from_all_servers(void);
-void pisa_client_heartbeat_all(void);
-
-void pisa_cd_copy_from_sock_to_tun(void);
-void pisa_cd_copy_from_tun_to_sock(void);
-
 void pisa_client_connect_to_server(pisa_conmgr_entry *entry);
 
-void pisa_cd_timeout_collect(void);
-
 /* handlers for tunnel control packets */
 void pisa_recv_register_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_deregister_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_heartbeat_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
 
-#endif /* PISA_CDTUN_H */
+#endif /* PISA_CDREGISTER_H */

Modified: trunk/pisacd/cdservers.c
==============================================================================
--- trunk/pisacd/cdservers.c    Thu Oct  8 13:12:57 2009        (r1051)
+++ trunk/pisacd/cdservers.c    Thu Oct  8 14:05:22 2009        (r1052)
@@ -11,7 +11,7 @@
  */
 
 #include "cdctx.h"
-#include "cdtun.h"
+#include "cdregister.h"
 #include "cdservers.h"
 #include "config.h"
 #include "nat.h"

Modified: trunk/pisacd/cdtun.c
==============================================================================
--- trunk/pisacd/cdtun.c        Thu Oct  8 13:12:57 2009        (r1051)
+++ trunk/pisacd/cdtun.c        Thu Oct  8 14:05:22 2009        (r1052)
@@ -5,7 +5,7 @@
 
 /**
  * @file cdtun.c
- * @brief Implementation of tunnel modules in the PISA client daemon.
+ * @brief Data packet handling in the PISA client daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
  * @author Thomas Jansen <mithi@xxxxxxxxx>
  * @date Oct. 2008
@@ -18,369 +18,12 @@
 #include "nat.h"
 #include "cdctx.h"
 #include "cdconf.h"
+#include "cdderegister.h"
+#include "cdregister.h"
 #include "conmgr.h"
 #include "cdpending.h"
 #include "cdservers.h"
 
-static void pisa_remove_default_route(void);
-
-/**
- * Remove a server connection from our conmgr module
- * and take care of the default route.
- * @param entry Non-Null pointer to pisa_conmgr_entry
- */
-static void pisa_remove_server_connection(pisa_conmgr_entry* entry)
-{
-       assert(entry != NULL);
-
-       if (entry == cd_ctx.defaultroute)
-               pisa_remove_default_route();
-       entry->status = PISA_CON_DISCONNECTED;
-}
-
-/**
- * Handle a pending request of type REGISTER:
- *     1. Log the attempt to connect to that server.
- *     2. Send the REGISTER packet.
- * @param pend pending request
- */
-static void pisa_send_register_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_packet *packet = pisa_alloc_msg();
-
-       assert(pend != NULL);
-       assert(pend->data != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_register_pend in a 
non-REGISTERING state.\n");
-               return;
-       }
-
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_REGISTER, "Sending REGISTER to %s, try %i\n", buffer, 
pend->count);
-
-       memcpy(&packet->body.reg, pend->data, sizeof(pisa_payload_register));
-
-       pisa_set_packet_type(packet, PISA_PKTTYPE_TUN_REGISTER);
-       pisa_set_packet_length(packet, sizeof(pisa_packet_common) + 
sizeof(pisa_payload_register));
-
-       pisa_send_control_packet_ipv6(cd_ctx.tunc, &pend->key.addr, packet);
-       pisa_free_msg(packet);
-}
-
-/**
- * Handle a failed pending request of type REGISTER.
- * @param pend pending request
- */
-static void pisa_send_register_fail(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_register_fail in a 
non-REGISTERING state.\n");
-       } else {
-               inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, 
sizeof(buffer));
-               PISA_DEBUG(PL_REGISTER, "Failed to REGISTER to %s.\n", buffer);
-               pend->entry->status = PISA_CON_DISCONNECTED;
-       }
-}
-
-/**
- * Handle a pending request of type DEREGISTER:
- *     1. Log the attempt to disconnect from that server.
- *     2. Send the DEREGISTER packet.
- * @param pend pending request
- */
-static void pisa_send_deregister_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       struct sockaddr_in6 addr = {0};
-
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_DEREGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_deregister_pend in a 
non-DEREGISTERING state.\n");
-       } else {
-               inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, 
sizeof(buffer));
-               PISA_DEBUG(PL_DEREGISTER, "Sending DEREGISTER to %s, try %i\n", 
buffer, pend->count);
-               pisa_conmgr_build_sockaddr_control(pend->entry, &addr);
-               pisa_send_control_packet_ipv6_type(cd_ctx.tunc, &addr, 
PISA_PKTTYPE_TUN_DEREGISTER);
-       }
-}
-
-/**
- * Handle a failed pending request of type DEREGISTER.
- * @param pend pending request
- */
-static void pisa_send_deregister_fail(pisa_pending *pend)
-{
-       assert(pend != NULL);
-       assert(pend->entry != NULL);
-
-       if (pend->entry->status != PISA_CON_DEREGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "pisa_send_deregister_fail in a 
non-DEREGISTERING state.\n");
-       } else {
-               pisa_remove_server_connection(pend->entry);
-       }
-}
-
-/**
- * Add a pending request for disconnecting from a server.
- * @param data NULL, not used, but required to use this function as a callback
- *     for pisa_conmgr_iterate_connected.
- * @param entry conmgr entry for the server
- */
-void pisa_client_disconnect_from_server(void *data, pisa_conmgr_entry *entry)
-{
-       pisa_pending *pend;
-
-       assert(entry != NULL);
-
-       entry->status = PISA_CON_DEREGISTERING;
-
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_DEREGISTER);
-       pend->func = pisa_send_deregister_pend;
-       pend->fail = pisa_send_deregister_fail;
-       pisa_pending_reschedule(pend, 0);
-}
-
-/**
- * Close all connections to PISA servers. Used for cleanup on exit.
- */
-void pisa_client_disconnect_from_all_servers(void)
-{
-       pisa_pending_remove_all();
-       pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_client_disconnect_from_server, NULL);
-}
-
-/**
- * Connect to a PISA server.
- * @param entry conmgr entry to connect to
- */
-void pisa_client_connect_to_server(pisa_conmgr_entry *entry)
-{
-       pisa_payload_register *reg;
-       pisa_pending *pend;
-
-       assert(entry != NULL);
-
-       /* TODO Thomas: What about the DEREGISTERING state? */
-       if (entry->status != PISA_CON_DISCONNECTED)
-               return;
-
-       pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_REGISTER);
-       pend->func = pisa_send_register_pend;
-       pend->fail = pisa_send_register_fail;
-       pisa_pending_reschedule(pend, 0);
-
-       reg = malloc(sizeof(pisa_payload_register));
-       memset(reg, 0, sizeof(pisa_payload_register));
-       pend->data = reg;
-       reg->type = htons(entry->type);
-       if (entry->type == PISA_CONTYPE_SERVICE)
-               pisa_ipv4_copy(&reg->local, &cd_cfg.local_ipv4);
-
-       entry->status = PISA_CON_REGISTERING;
-}
-
-/**
- * Set up a default route with a gateway.
- * @param ipAddress gateway IPv4 address
- */
-static void pisa_set_default_route(struct in_addr *ipv4)
-{
-       char buffer[1024]={0}, ip[INET_ADDRSTRLEN];
-
-       inet_ntop(AF_INET, ipv4, ip, sizeof(ip));
-       snprintf(buffer, sizeof(buffer), "route add default gw %s", ip);
-       if (system(buffer) < 0)
-               PISA_ERROR("Can't add default route!\n");
-
-}
-
-/**
- * Handle a REGISTER_ACK packet from a server:
- *     1. Add the server to the conmgr hash of active connections.
- *     2. Set up the default route.
- *     3. Log the new connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_register_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_pending *pend;
-       pisa_conmgr_entry *entry;
-
-       entry = pisa_conmgr_findby_address(cd_ctx.conlist, &addr->sin6_addr);
-       if (entry == NULL) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received REGISTER_ACK from unknown 
connection, ignoring it.\n");
-               return;
-       }
-
-       if (entry->status != PISA_CON_REGISTERING) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received REGISTER_ACK in a 
non-REGISTERING state, ignoring it.\n");
-               return;
-       }
-
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_REGISTER);
-       if (pend == NULL) {
-               PISA_DEBUG(PL_STATEMACHINE, "Received unexpected REGISTER_ACK, 
ignoring it.\n");
-               return;
-       }
-
-       pisa_conmgr_connected(cd_ctx.conlist, entry, 
PISASD_DEFAULT_PORTNUM_DATA, &pkt->body.reg_ack.client);
-       entry->status = PISA_CON_CONNECTED;
-       if (entry->type == PISA_CONTYPE_RELAY) {
-               /* set up a second IPv4 address and a default route for the
-                * primary connection */
-               pisa_tunnel_configure_sub(cd_ctx.ifname_tunnel, 
&pkt->body.reg_ack.client);
-               pisa_set_default_route(&pkt->body.reg_ack.server);
-               cd_ctx.defaultroute = entry;
-       }
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_REGISTER, "Connected to PISA server %s\n", buffer);
-
-       pisa_pending_remove(pend);
-}
-
-/**
- * Delete the default route.
- */
-static void pisa_remove_default_route(void)
-{
-       const char* deleteDefaultRoute="route del default"; /* Perhaps this 
works only with LINUX */    
-       PISA_DEBUG(PL_GENERIC, "remove default route\n");
-       if (system(deleteDefaultRoute) < 0)
-               PISA_ERROR("ERROR: %s: Can't delete default route!\n", 
__FUNCTION__);
-}
-
-/**
- * Handle a DEREGISTER_ACK packet from a server:
- *     1. Remove the server from the conmgr hash of active connections.
- *     2. Remove the default route.
- *     3. Log the removed connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_deregister_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&addr->sin6_addr);
-       pisa_pending *pend;
-
-       if (entry == NULL)
-               return;
-
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_DEREGISTER);
-       if (pend != NULL)
-               pisa_pending_remove(pend);
-
-       pisa_remove_server_connection(entry);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_INFO("Disconnected from PISA server %s\n", buffer);
-}
-
-/**
- * Handle a HEARTBEAT_ACK packet from a server. Update the last received time
- * for that connection.
- * @param pkt original packet
- * @param addr HIT/port of the server
- */
-void pisa_recv_heartbeat_ack(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&addr->sin6_addr);
-       pisa_pending *pend;
-
-       if (entry == NULL)
-               return;
-
-       time(&entry->timeout_heartbeat);
-       pend = pisa_pending_find(entry, PISA_PKTTYPE_TUN_HEARTBEAT);
-       if (pend)
-               pisa_pending_remove(pend);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_HEARTBEAT, "Heartbeat Ack from PISA server %s\n", buffer);
-}
-
-/**
- * Handle a pending request of type HEARTBEAT.
- * @param pend pending request
- */
-static void pisa_send_heartbeat_pend(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *entry;
-       struct sockaddr_in6 addr = {0};
-
-       assert(pend != NULL);
-
-       entry = pisa_conmgr_findby_address(cd_ctx.conlist, 
&pend->key.addr.sin6_addr);
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_DEBUG(PL_HEARTBEAT, "Sending HEARTBEAT to %s, try %i\n", buffer, 
pend->count);
-       pisa_conmgr_build_sockaddr_control(entry, &addr);
-       pisa_send_control_packet_ipv6_type(cd_ctx.tunc, &addr, 
PISA_PKTTYPE_TUN_HEARTBEAT);
-}
-
-/**
- * Handle a failed pending request of type HEARTBEAT.
- * @param pend pending request
- */
-static void pisa_send_heartbeat_fail(pisa_pending *pend)
-{
-       char buffer[INET6_ADDRSTRLEN];
-
-       assert(pend != NULL);
-
-       inet_ntop(AF_INET6, &pend->key.addr.sin6_addr, buffer, sizeof(buffer));
-       PISA_INFO("HEARTBEAT failed to %s, disconnecting.\n", buffer);
-       pisa_client_disconnect_from_server(NULL, 
pisa_conmgr_findby_address(cd_ctx.conlist, &pend->key.addr.sin6_addr));
-}
-
-/**
- * Check if a connection was idle long enough to send a heartbeat. Current
- * time is passed to avoid multiple time() calls in a row.
- * @param data pointer to the current time in time_t format.
- * @param entry conmgr entry for the server
- */
-static void pisa_client_send_heartbeat(void *data, pisa_conmgr_entry *entry)
-{
-       pisa_pending *pend;
-       time_t *t = (time_t *)data;
-       char buffer[INET6_ADDRSTRLEN]={0};
-
-       inet_ntop(AF_INET6, &entry->hit, buffer, sizeof(buffer));
-       if (entry->timeout_heartbeat + 20 < *t) {
-               pend = pisa_pending_add(entry, PISA_PKTTYPE_TUN_HEARTBEAT);
-               pend->func = pisa_send_heartbeat_pend;
-               pend->fail = pisa_send_heartbeat_fail;
-               pisa_pending_reschedule(pend, 0);
-               entry->timeout_heartbeat = *t;
-       }
-}
-
-/**
- * Check all connections for their last received time to see if we need to
- * send a heartbeat.
- */
-void pisa_client_heartbeat_all(void)
-{
-       time_t t;
-
-       time(&t);
-       pisa_conmgr_iterate_connected(cd_ctx.conlist, 
pisa_client_send_heartbeat, &t);
-}
-
 /**
  * Copy a packet from the data socket to the tunnel device.
  * @params socks the file descriptors for the socket and tunnel device

Modified: trunk/pisacd/cdtun.h
==============================================================================
--- trunk/pisacd/cdtun.h        Thu Oct  8 13:12:57 2009        (r1051)
+++ trunk/pisacd/cdtun.h        Thu Oct  8 14:05:22 2009        (r1052)
@@ -6,22 +6,9 @@
 #ifndef PISA_CDTUN_H
 #define PISA_CDTUN_H
 
-#include "conmgr.h"
-#include "packet.h"
-
-void pisa_client_disconnect_from_all_servers(void);
-void pisa_client_heartbeat_all(void);
-
 void pisa_cd_copy_from_sock_to_tun(void);
 void pisa_cd_copy_from_tun_to_sock(void);
 
-void pisa_client_connect_to_server(pisa_conmgr_entry *entry);
-
 void pisa_cd_timeout_collect(void);
 
-/* handlers for tunnel control packets */
-void pisa_recv_register_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_deregister_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_heartbeat_ack(pisa_packet *pkt, struct sockaddr_in6 *addr);
-
 #endif /* PISA_CDTUN_H */

Other related posts:

  • » [pisa-src] r1052 - trunk/pisacd - Thomas Jansen