[pisa-src] r1053 - trunk/pisasd

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Thu, 08 Oct 2009 15:02:48 +0200

Author: tjansen
Date: Thu Oct  8 15:02:47 2009
New Revision: 1053

Log:
Splitted sdtun.c into different modules.

sdtun was a multi-purpose file and rather large. Functions have been grouped
by topic and moved into sdregister, sdderegister and sdheartbeat. 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/pisasd/sdderegister.c
      - copied, changed from r1043, trunk/pisasd/sdtun.c
   trunk/pisasd/sdderegister.h
      - copied, changed from r1043, trunk/pisasd/sdtun.h
   trunk/pisasd/sdheartbeat.c
      - copied, changed from r1043, trunk/pisasd/sdtun.c
   trunk/pisasd/sdheartbeat.h
      - copied, changed from r1043, trunk/pisasd/sdtun.h
   trunk/pisasd/sdregister.c
      - copied, changed from r1043, trunk/pisasd/sdtun.c
   trunk/pisasd/sdregister.h
      - copied, changed from r1043, trunk/pisasd/sdtun.h
Modified:
   trunk/pisasd/Makefile.am
   trunk/pisasd/sdmain.c
   trunk/pisasd/sdtun.c
   trunk/pisasd/sdtun.h

Modified: trunk/pisasd/Makefile.am
==============================================================================
--- trunk/pisasd/Makefile.am    Thu Oct  8 14:05:22 2009        (r1052)
+++ trunk/pisasd/Makefile.am    Thu Oct  8 15:02:47 2009        (r1053)
@@ -36,6 +36,8 @@
 endif
 
 LDADD += -lm
-pisasd_SOURCES = sdmain.c sdclients.c sdconf.c sdctx.c sdmsg.c sdtun.c 
pisasdconf.c
+pisasd_SOURCES = sdmain.c sdclients.c sdconf.c sdctx.c sdderegister.c \
+                sdheartbeat.c sdmsg.c sdregister.c sdtun.c pisasdconf.c
 pisasdconf_SOURCES = pisaconftool.c pisasdconf.c
-include_HEADERS = sdclients.h sdconf.h sdctx.h sdmsg.h sdtun.h pisasdconf.h
+include_HEADERS = sdclients.h sdconf.h sdctx.h sdderegister.h sdheartbeat.h \
+                 sdmsg.h sdregister.h sdtun.h pisasdconf.h

Copied and modified: trunk/pisasd/sdderegister.c (from r1043, 
trunk/pisasd/sdtun.c)
==============================================================================
--- trunk/pisasd/sdtun.c        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisasd/sdderegister.c Thu Oct  8 15:02:47 2009        (r1053)
@@ -1,245 +1,18 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
- * @file sdtun.c
+ * @file sdderegister.c
  * @brief Implementation of tunnel modules in the PISA server daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
- * @date Oct. 2008
+ * @author Thomas Jansen <mithi@xxxxxxxxx>
+ * @date Oct. 2009
  */
 
-#include <netinet/ip.h>
-
-#include "socket.h"
 #include "conmgr.h"
-#include "hitlist.h"
 #include "sdctx.h"
-#include "sdtun.h"
-#include "sdconf.h"
-#include "nat.h"
-#include "sdclients.h"
-#include "arp.h"
-
-#ifdef CONFIG_PISA_LOGGING
-# include "log.h"
-#endif
-
-#ifdef CONFIG_PISA_PERFORMANCE
-# include "pisaperf.h"
-#endif
-
-#define DEFAULT_BUFF_SIZE 15000
-
-/**
- * Time to wait until a connection is removed from the internal list of life 
connections.
- * @sa removeTimedoutConnectionsFlag
- * @sa process
- */
-static time_t waitingPeriod=30;
-
-#ifdef CONFIG_PISA_PERFORMANCE
-static int pisasd_bench_tv_on = FALSE;
-static void sd_perf_start_benchmark_tokvrfy(void);
-static void sd_perf_stop_benchmark_tokvrfy(void);
-static void sd_perf_write_benchmark_tokvrfy(void);
-#endif
-
-extern pisa_packet_handle_func_set pisasd_packet_handle_func_set;
-
-/**
- * Check if a connection has timed out and remove it.
- * @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 cb_check_timeout(void* data,pisa_conmgr_entry* entry)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       time_t currentTime = *((time_t*)data);
-
-       if ((entry->timeout_heartbeat + waitingPeriod) < currentTime) {
-               inet_ntop(AF_INET6, &entry->hit, buffer, sizeof(buffer));
-               PISA_INFO("Removing timed out client %s\n", buffer);
-               pisa_conmgr_remove(sd_ctx.conlist, sd_ctx.natlist, entry);
-       }
-}
-
-/**
- * Remove all connections from our internal list that have timed out.
- * Only check every 10 seconds for it.
- */
-void pisa_sd_timeout_collect(void)
-{
-       static time_t last = 0;
-       time_t current;
-
-       time(&current);
-       if (last + 10 < current) {
-               pisa_conmgr_iterate(sd_ctx.conlist, cb_check_timeout, &current);
-               last = current;
-       }
-}
-
-/**
- * send an RegisterAck to a client.
- * consult the client/server communication graphics /tables for details.
- * @param e client connection
- */
-static void pisa_send_register_ack(pisa_conmgr_entry* e)
-{
-       struct sockaddr_in6 addr = {0};
-       pisa_packet *packet = pisa_alloc_msg();
-
-       pisa_ipv4_copy(&packet->body.reg_ack.client, &e->ipv4);
-       pisa_ipv4_copy(&packet->body.reg_ack.server, &sd_cfg.ipaddr);
-
-       pisa_set_packet_type(packet, PISA_PKTTYPE_TUN_REGISTER_ACK);
-       pisa_set_packet_length(packet, sizeof(pisa_packet_common) + 
sizeof(pisa_payload_registerack));
-
-       pisa_conmgr_build_sockaddr_control(e, &addr);
-       pisa_send_control_packet_ipv6(sd_ctx.tunc, &addr, packet);
-       pisa_free_msg(packet);
-}
-
-/**
- * Accept a new client into our conmgr list and send REGISTER_ACK.
- * @param addr control port and HIT of the new client
- * @param type connection type
- * @param client_ipv4 IPv4 for the client
- * @return the new conmgr entry
- */
-static pisa_conmgr_entry *pisa_register_client_accept(struct sockaddr_in6 
*addr, uint16_t type, struct in_addr *client_ipv4)
-{
-       pisa_conmgr_entry *entry;
-
-       entry = pisa_conmgr_add(sd_ctx.conlist, &addr->sin6_addr, 
ntohs(addr->sin6_port));
-       pisa_conmgr_connected(sd_ctx.conlist, entry, 
PISASD_DEFAULT_PORTNUM_DATA, client_ipv4);
-       entry->type = type;
-       pisa_send_register_ack(entry);
-       entry->status = PISA_CON_CONNECTED;
-
-       return entry;
-}
-
-/**
- * Register a client which is in the set of allowed HIT's
- * and send a RegisterAck message to the client.
- * consult the client/server communication graphics /tables for details.
- * @param addr HIT/control port to associate our new connection with
- * @param pkt REGISTER packet for additional information
- */
-static void pisa_register_client(struct sockaddr_in6* addr, pisa_packet *pkt)
-{
-       char buffer[INET6_ADDRSTRLEN], ip_local[INET6_ADDRSTRLEN], 
ip_remote[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry* e=NULL;
-       pisa_hitlist_entry *con;
-       struct in_addr client_ipv4;
-       uint16_t type;
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-
-
-       if (pisa_client_get_local_ipv4(&addr->sin6_addr, &client_ipv4) != 1) {
-               PISA_ERROR("Could not find a local IPv4 for client %s\n", 
buffer);
-               return;
-               /* @todo some sort of error message for the client? */
-       }
-
-       inet_ntop(AF_INET, &client_ipv4, ip_local, sizeof(ip_local));
-       type = ntohs(pkt->body.reg.type);
-       switch (type) {
-               case PISA_CONTYPE_RELAY:
-                       con = pisa_hitlist_find(sd_cfg.hit_allowed, 
&addr->sin6_addr);
-
-                       if (con == NULL) {
-                               PISA_ERROR("HIT not allowed.\n");
-                               return;
-                       }
-
-                       if (pisa_hitlist_entry_expired(con)) {
-                               PISA_ERROR("HIT expired.\n");
-                               return;
-                       }
-
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_INFO("Accepted relay client %s as %s\n", buffer, 
ip_local);
-#ifdef CONFIG_PISA_LOGGING
-                       /* Save start of connection time and log the start of a 
new connection. */
-                       e->start_time = time(NULL);
-                       log_start_connection(buffer, &client_ipv4);
-#endif
-                       break;
-               case PISA_CONTYPE_SERVICE:
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       pisa_nat_add_mapping(sd_ctx.natlist, &client_ipv4, 
&pkt->body.reg.local, e, NULL);
-                       inet_ntop(AF_INET, &pkt->body.reg.local, ip_remote, 
sizeof(ip_remote));
-                       PISA_INFO("Accepted service client %s as %s with remote 
%s\n", buffer, ip_local, ip_remote);
-                       break;
-               case PISA_CONTYPE_LEGACY_ROUTER:
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_INFO("Accepted legacy router client %s as %s\n", 
buffer, ip_local);
-                       break;
-               default:
-                       /* @TODO: do we really want to accept incorrect 
CONTYPEs? */
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_ERROR("Accepted unknown type %i client %s\n", 
type, buffer);
-                       break;
-       }
-}
-
-/**
- * Handle a tunnel control packet of type data. This was obsoleted by
- * splitting data and control socket and using a different port
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_data(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       PISA_DEBUG(PL_STATEMACHINE, "Received obsolete PISA_PKTTYPE_TUN_DATA 
packet.\n");
-}
-
-/**
- * Handle a tunnel control packet of type register. Register a new client or
- * just send an REGISTER_ACK if the client is already registered. This may
- * happen if the connection is slow and the client sent another REGISTER
- * before he received our first REGISTER_ACK.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_register(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       if (e!=NULL) {
-               inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-               PISA_DEBUG(PL_STATEMACHINE|PL_REGISTER, "Register from already 
connected client %s\n", buffer);
-               pisa_send_register_ack(e);
-       } else {
-               pisa_register_client(addr, pkt);
-       }
-}
-
-/**
- * Handle a tunnel control packet of type heartbeat. Update the last received
- * time for that connection or ignore it if no connection matches.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_heartbeat(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       if (e) {
-               time(&e->timeout_heartbeat);
-               pisa_send_control_packet_ipv6_type(sd_ctx.tunc, addr, 
PISA_PKTTYPE_TUN_HEARTBEAT_ACK);
-               inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-               PISA_INFO("Heartbeat from client %s\n", buffer);
-       }
-}
 
 /**
  * Handle a tunnel control packet of type deregister. Deregister a client or
@@ -264,375 +37,3 @@
        }
        pisa_send_control_packet_ipv6_type(sd_ctx.tunc, addr, 
PISA_PKTTYPE_TUN_DEREGISTER_ACK);
 }
-
-/**
- * Copy a packet from the data socket to the tunnel device.
- */
-void pisa_sd_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_conmgr_entry *entry;
-       pisa_nat_mapping *map;
-       pisa_mac *mac = (pisa_mac *)buffer;
-       struct iphdr *hdr;
-       struct in_addr *srcaddr;
-
-       addrlen = sizeof(from);
-
-       if ((len = recvfrom(sd_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 */
-       if ((entry = pisa_conmgr_findby_address(sd_ctx.conlist, 
&from.sin6_addr)) != NULL) {
-               if (entry->status != PISA_CON_CONNECTED) {
-                       PISA_DEBUG(PL_STATEMACHINE, "Connection exists, but is 
not in CONNECTED state. Should not be possible!\n");
-                       return;
-               }
-
-               if (entry->data_port != ntohs(from.sin6_port)) {
-                       PISA_DEBUG(PL_DATA, "Discarding data from the wrong 
port! expected: %i, is: %i\n", entry->data_port, ntohs(from.sin6_port));
-                       return;
-               }
-
-               /* Check for IPv4 in the payload, otherwise drop it */
-               if ((hdr = pisa_nat_get_iphdr(buffer)) == NULL)
-                       return;
-               srcaddr = (struct in_addr *)&hdr->saddr;
-
-               /* Update the timeout for the connection */
-               time(&entry->timeout_heartbeat);
-
-               if (entry->type == PISA_CONTYPE_LEGACY_ROUTER) {
-                       char mac_str[ARP_STRLEN];
-                       pisa_mac_write_string(mac, mac_str);
-                       map = pisa_nat_mapping_find_by_mac(sd_ctx.natlist, mac);
-                       if ((mac->m[0] == 0) && (mac->m[1] == 0) && (mac->m[2] 
== 0) && (mac->m[3] == 0) && (mac->m[4] == 0) && (mac->m[5] == 0)) {
-                               PISA_DEBUG(PL_DATA, "remote -> local: invalid 
MAC, dropping\n");
-                               return;
-                       }
-                       if (map == NULL) {
-                               struct in_addr local = {0};
-                               char ip_str[INET_ADDRSTRLEN];
-                               if (pisa_client_get_legacy_client_ipv4(&local) 
== 0) {
-                                       PISA_DEBUG(PL_LEGACY, "Could not get a 
dynamic IPv4 for a new legacy client.\n");
-                                       return;
-                               }
-                               map = pisa_nat_add_mapping(sd_ctx.natlist, 
&local, srcaddr, entry, mac);
-                               inet_ntop(AF_INET, &local, ip_str, 
sizeof(ip_str));
-                               PISA_DEBUG(PL_LEGACY, "Adding mapping for 
unknown legacy client: MAC %s IP %s\n", mac_str, ip_str);
-                       } else {
-                               if (map->connection != entry) {
-                                       PISA_DEBUG(PL_LEGACY, "Legacy client 
handover detected for MAC %s\n", mac_str);
-                                       
pisa_nat_update_connection(sd_ctx.natlist, map, entry);
-                               }
-                       }
-               } else {
-                       map = pisa_nat_mapping_find_by_remote(sd_ctx.natlist, 
&from.sin6_addr, srcaddr);
-               }
-
-               /* Change the source IPv4 if a NAT mapping should be applied */
-               if (map != NULL)
-                       pisa_nat_apply(hdr, srcaddr, &map->local_private);
-
-               /* Send the packet out to the tunnel device */
-               if (write(sd_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);
-#ifdef CONFIG_PISA_LOGGING
-                       entry->transferred_bytes+=len;
-                       PISA_DEBUG(PL_DATA, "TOTAL BYTES: 
%i\n",entry->transferred_bytes);
-#endif
-               }
-       } 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.
- */
-void pisa_sd_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;
-       struct sockaddr_in6 addr = {0};
-
-       if ((len = read(sd_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;
-       }
-
-       /* Apply NAT if needed */
-       dst = (struct in_addr *)&hdr->daddr;
-       map = pisa_nat_mapping_find_by_local_private(sd_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;
-               /* @todo: first 6 bytes should be MAC of the receiver */
-       } else {
-               /* No NAT mapping found, just find out which server is the
-                * destination */
-               entry = pisa_conmgr_findby_clientip(sd_ctx.conlist, dst);
-       }
-
-       /* If neither NAT nor a destination server could be found we've run
-        * out of options. The server does not have a default route. */
-       if (entry == NULL) {
-               PISA_ERROR("local -> remote: no destination\n");
-               return;
-       }
-
-       if (entry->status != PISA_CON_CONNECTED) {
-               PISA_DEBUG(PL_STATEMACHINE, "Connection exists, but is not in 
CONNECTED state. Should not be possible!\n");
-               return;
-       } else {
-               /* Send the packet out to the data port of the connection entry 
*/
-               pisa_conmgr_build_sockaddr_data(entry, &addr);
-               if (sendto(sd_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 {
-                       PISA_DEBUG(PL_DATA, "local -> remote: %i bytes\n", len);
-#ifdef CONFIG_PISA_LOGGING
-                       entry->transferred_bytes+=len;
-                       PISA_DEBUG(PL_DATA, "TOTAL BYTES: 
%i\n",entry->transferred_bytes);
-#endif
-               }
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-static void pisa_message_pstun_new_client(char* buffer, struct sockaddr_in6* 
clientIp)
-{
-       pisa_hitlist_entry *con;
-       pisa_conmgr_entry *entry;
-       pisa_packet *pkt = (pisa_packet *)buffer;
-
-       con = pisa_hitlist_find(sd_cfg.hit_allowed, &clientIp->sin6_addr);
-
-       if (!con) {
-               PISA_DEBUG(PL_PREAUTH, "Received a HIT that is not allowed:\n");
-               dump_ipv6_addr(&clientIp->sin6_addr);
-
-               pisa_send_control_packet_ipv6_type(sd_ctx.tunc, clientIp, 
PISA_PKTTYPE_TUN_UNKNOWN_HIT);
-               return;
-       }
-
-       entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-
-       /* General non-tunnel PISA packets */
-       sd_process_pisa_packets_pstun(entry, pkt, clientIp);
-}
-
-/**
- * main logic to processs an incoming packet from an ip, for which we have an
- * association.
- * @sa processIncomingMessage
- * @param e our association structure
- * @param buffer the raw bytes of our message
- * @param len the amount of bytes used in our buffer
- */
-static void pisa_message_pstun_known(pisa_conmgr_entry* e, char* buffer,size_t 
len)
-{
-       pisa_packet *pkt = (pisa_packet *)buffer;
-       struct sockaddr_in6 addr = {0};
-
-       /* General non-tunnel PISA packets */
-       pisa_conmgr_build_sockaddr_data(e, &addr);
-       sd_process_pisa_packets_pstun(e, pkt, &addr);
-}
-
-/**
- * main logic for every message, arriving at our local socket.
- */
-void pisa_message_pstun(int fd)
-{
-       char buffer[1024]={0};
-       unsigned int addrlen=0;
-       ssize_t  len=0;
-
-       struct sockaddr_in6 from= {
-               0
-       };
-
-       addrlen = sizeof (from);
-
-       if ((len=recvfrom (fd, buffer, sizeof (buffer), 0, (struct sockaddr *) 
&from, &addrlen))==-1) {
-               PISA_ERROR("server:main:recvfrom");
-               return;
-       }
-
-       if (!is_pisa_packet(buffer)) {
-               PISA_ERROR("The incoming packet is not from a valid PISA client 
daemon.\n");
-               return;
-       }
-
-       pisa_conmgr_entry *e=pisa_conmgr_findby_address_pstun(sd_ctx.conlist, 
&from.sin6_addr);
-
-       if (e==NULL) { /* case 1: data from new client */
-               pisa_message_pstun_new_client(buffer,&from);
-       } else { /* case 2: data from known client */
-               time(&e->timeout_heartbeat);
-               pisa_message_pstun_known(e,buffer,len);
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-void sd_process_pisa_packets_tunc(pisa_packet *pkt, struct sockaddr_in6* 
clientIp)
-{
-       pisa_conmgr_entry *entry = NULL;
-       pisa_tlv_type pkttype = PISA_PKTTYPE_BU_UNKNOWN;
-
-       pkttype = pisa_get_packet_type(pkt);
-
-       PISA_DEBUG(PL_PREAUTH, "Packet type = %d\n", pisa_get_packet_type(pkt));
-
-       switch (pkttype) {
-       case PISA_PKTTYPE_BU_REQUEST:
-               entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-#ifdef CONFIG_PISA_PERFORMANCE
-               sd_perf_start_benchmark_tokvrfy();
-#endif
-
-               pisasd_packet_handle_func_set.handle_bureq(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_BU_RESPONSE:
-               entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-               pisasd_packet_handle_func_set.handle_bures(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_NE_UNKNOWN:
-       case PISA_PKTTYPE_PA_UNKNOWN:
-       case PISA_PKTTYPE_BU_UNKNOWN:
-               /* drop */
-               PISA_ERROR("Cannot recognize type of the incoming pisasd 
packet.\n");
-               break;
-       default:
-               /* TODO: For now, do nothing.
-                * But we need to find some way to deal with it. */
-               break;
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-void sd_process_pisa_packets_pstun(pisa_conmgr_entry *entry, pisa_packet *pkt, 
struct sockaddr_in6* clientIp)
-{
-       pisa_tlv_type pkttype = PISA_PKTTYPE_BU_UNKNOWN;
-
-       pkttype = pisa_get_packet_type(pkt);
-
-       PISA_DEBUG(PL_PREAUTH, "Packet type = %d\n", pisa_get_packet_type(pkt));
-
-       switch (pkttype) {
-       case PISA_PKTTYPE_VRFY_REQUEST1:
-               pisasd_packet_handle_func_set.handle_vrfyreq1(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_RESPONSE1:
-               pisasd_packet_handle_func_set.handle_vrfyres1(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_REQUEST2:
-               pisasd_packet_handle_func_set.handle_vrfyreq2(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_RESPONSE2:
-#ifdef CONFIG_PISA_PERFORMANCE
-               sd_perf_stop_benchmark_tokvrfy();
-               sd_perf_write_benchmark_tokvrfy();
-#endif
-
-               pisasd_packet_handle_func_set.handle_vrfyres2(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_NE_UNKNOWN:
-       case PISA_PKTTYPE_PA_UNKNOWN:
-       case PISA_PKTTYPE_BU_UNKNOWN:
-               /* drop */
-               PISA_ERROR("Cannot recognize type of the incoming pisasd 
packet.\n");
-               break;
-       default:
-               break;
-       }
-}
-
-#ifdef CONFIG_PISA_PERFORMANCE
-
-/**
- * Start token verification benchmark
- */
-static void sd_perf_start_benchmark_tokvrfy(void)
-{
-       if (!pisasd_bench_tv_on) {
-               PISA_DEBUG(PL_PREAUTH, "Starting PERF_TOKEN_VERIFICATION\n");
-               pisa_perf_start_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-
-               pisasd_bench_tv_on = TRUE;
-       }
-}
-
-/**
- * Stop token verification benchmark
- */
-static void sd_perf_stop_benchmark_tokvrfy(void)
-{
-       PISA_DEBUG(PL_PREAUTH, "Stopping PERF_TOKEN_VERIFICATION\n");
-       pisa_perf_stop_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-
-       pisasd_bench_tv_on = FALSE;
-}
-
-/**
- * Write token verification benchmark results
- */
-static void sd_perf_write_benchmark_tokvrfy(void)
-{
-       PISA_DEBUG(PL_PREAUTH, "Writing PERF_TOKEN_VERIFICATION\n");
-       pisa_perf_write_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-}
-
-#endif

Copied and modified: trunk/pisasd/sdderegister.h (from r1043, 
trunk/pisasd/sdtun.h)
==============================================================================
--- trunk/pisasd/sdtun.h        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisasd/sdderegister.h Thu Oct  8 15:02:47 2009        (r1053)
@@ -1,42 +1,23 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
- * @file sdtun.h
- * @brief Header file of the context information for the PISA server daemon.
+ * @file sdderegister.h
+ * @brief Header for deregister related functions for the PISA server daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
  * @author Dongsu Park <dpark1978@xxxxxxxxx>
+ * @author Thomas Jansen <mithi@xxxxxxxxx>
  * @date Mar. 2009
  */
 
-#ifndef PISA_SDTUN_H
-#define PISA_SDTUN_H
+#ifndef PISA_SDDEREGISTER_H
+#define PISA_SDDEREGISTER_H
 
 #include "packet.h"
-#include "conmgr.h"
 
-/**
- * Function prototypes
- */
-
-void pisa_message_pstun(int fd);
-void sd_process_pisa_packets_tunc(pisa_packet *pkt, struct sockaddr_in6* 
clientIp);
-void sd_process_pisa_packets_pstun(pisa_conmgr_entry *entry, pisa_packet *pkt, 
struct sockaddr_in6* clientIp);
-
-void processTunnelCtrlFDs(void* data, pisa_conmgr_entry* e);
-void processTunnelDataFDs(void* data, pisa_conmgr_entry* e);
-
-void pisa_sd_timeout_collect(void);
-
-void pisa_sd_copy_from_sock_to_tun(void);
-void pisa_sd_copy_from_tun_to_sock(void);
-
-/* handlers for tunnel control packets */
-void pisa_recv_data(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_register(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_heartbeat(pisa_packet *pkt, struct sockaddr_in6 *addr);
+/* handler for deregister tunnel control packets */
 void pisa_recv_deregister(pisa_packet *pkt, struct sockaddr_in6 *addr);
 
-#endif /* PISA_SDTUN_H */
+#endif /* PISA_SDDEREGISTER_H */

Copied and modified: trunk/pisasd/sdheartbeat.c (from r1043, 
trunk/pisasd/sdtun.c)
==============================================================================
--- trunk/pisasd/sdtun.c        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisasd/sdheartbeat.c  Thu Oct  8 15:02:47 2009        (r1053)
@@ -1,13 +1,14 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
- * @file sdtun.c
- * @brief Implementation of tunnel modules in the PISA server daemon.
+ * @file sdheartbeat.c
+ * @brief Heartbeat and timeout related functions in the PISA server daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
- * @date Oct. 2008
+ * @author Thomas Jansen <mithi@xxxxxxxxx>
+ * @date Oct. 2009
  */
 
 #include <netinet/ip.h>
@@ -22,16 +23,6 @@
 #include "sdclients.h"
 #include "arp.h"
 
-#ifdef CONFIG_PISA_LOGGING
-# include "log.h"
-#endif
-
-#ifdef CONFIG_PISA_PERFORMANCE
-# include "pisaperf.h"
-#endif
-
-#define DEFAULT_BUFF_SIZE 15000
-
 /**
  * Time to wait until a connection is removed from the internal list of life 
connections.
  * @sa removeTimedoutConnectionsFlag
@@ -39,15 +30,6 @@
  */
 static time_t waitingPeriod=30;
 
-#ifdef CONFIG_PISA_PERFORMANCE
-static int pisasd_bench_tv_on = FALSE;
-static void sd_perf_start_benchmark_tokvrfy(void);
-static void sd_perf_stop_benchmark_tokvrfy(void);
-static void sd_perf_write_benchmark_tokvrfy(void);
-#endif
-
-extern pisa_packet_handle_func_set pisasd_packet_handle_func_set;
-
 /**
  * Check if a connection has timed out and remove it.
  * @param data  pointer to the current time (only provided this way to avoid 
having to call
@@ -83,146 +65,6 @@
 }
 
 /**
- * send an RegisterAck to a client.
- * consult the client/server communication graphics /tables for details.
- * @param e client connection
- */
-static void pisa_send_register_ack(pisa_conmgr_entry* e)
-{
-       struct sockaddr_in6 addr = {0};
-       pisa_packet *packet = pisa_alloc_msg();
-
-       pisa_ipv4_copy(&packet->body.reg_ack.client, &e->ipv4);
-       pisa_ipv4_copy(&packet->body.reg_ack.server, &sd_cfg.ipaddr);
-
-       pisa_set_packet_type(packet, PISA_PKTTYPE_TUN_REGISTER_ACK);
-       pisa_set_packet_length(packet, sizeof(pisa_packet_common) + 
sizeof(pisa_payload_registerack));
-
-       pisa_conmgr_build_sockaddr_control(e, &addr);
-       pisa_send_control_packet_ipv6(sd_ctx.tunc, &addr, packet);
-       pisa_free_msg(packet);
-}
-
-/**
- * Accept a new client into our conmgr list and send REGISTER_ACK.
- * @param addr control port and HIT of the new client
- * @param type connection type
- * @param client_ipv4 IPv4 for the client
- * @return the new conmgr entry
- */
-static pisa_conmgr_entry *pisa_register_client_accept(struct sockaddr_in6 
*addr, uint16_t type, struct in_addr *client_ipv4)
-{
-       pisa_conmgr_entry *entry;
-
-       entry = pisa_conmgr_add(sd_ctx.conlist, &addr->sin6_addr, 
ntohs(addr->sin6_port));
-       pisa_conmgr_connected(sd_ctx.conlist, entry, 
PISASD_DEFAULT_PORTNUM_DATA, client_ipv4);
-       entry->type = type;
-       pisa_send_register_ack(entry);
-       entry->status = PISA_CON_CONNECTED;
-
-       return entry;
-}
-
-/**
- * Register a client which is in the set of allowed HIT's
- * and send a RegisterAck message to the client.
- * consult the client/server communication graphics /tables for details.
- * @param addr HIT/control port to associate our new connection with
- * @param pkt REGISTER packet for additional information
- */
-static void pisa_register_client(struct sockaddr_in6* addr, pisa_packet *pkt)
-{
-       char buffer[INET6_ADDRSTRLEN], ip_local[INET6_ADDRSTRLEN], 
ip_remote[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry* e=NULL;
-       pisa_hitlist_entry *con;
-       struct in_addr client_ipv4;
-       uint16_t type;
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-
-
-       if (pisa_client_get_local_ipv4(&addr->sin6_addr, &client_ipv4) != 1) {
-               PISA_ERROR("Could not find a local IPv4 for client %s\n", 
buffer);
-               return;
-               /* @todo some sort of error message for the client? */
-       }
-
-       inet_ntop(AF_INET, &client_ipv4, ip_local, sizeof(ip_local));
-       type = ntohs(pkt->body.reg.type);
-       switch (type) {
-               case PISA_CONTYPE_RELAY:
-                       con = pisa_hitlist_find(sd_cfg.hit_allowed, 
&addr->sin6_addr);
-
-                       if (con == NULL) {
-                               PISA_ERROR("HIT not allowed.\n");
-                               return;
-                       }
-
-                       if (pisa_hitlist_entry_expired(con)) {
-                               PISA_ERROR("HIT expired.\n");
-                               return;
-                       }
-
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_INFO("Accepted relay client %s as %s\n", buffer, 
ip_local);
-#ifdef CONFIG_PISA_LOGGING
-                       /* Save start of connection time and log the start of a 
new connection. */
-                       e->start_time = time(NULL);
-                       log_start_connection(buffer, &client_ipv4);
-#endif
-                       break;
-               case PISA_CONTYPE_SERVICE:
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       pisa_nat_add_mapping(sd_ctx.natlist, &client_ipv4, 
&pkt->body.reg.local, e, NULL);
-                       inet_ntop(AF_INET, &pkt->body.reg.local, ip_remote, 
sizeof(ip_remote));
-                       PISA_INFO("Accepted service client %s as %s with remote 
%s\n", buffer, ip_local, ip_remote);
-                       break;
-               case PISA_CONTYPE_LEGACY_ROUTER:
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_INFO("Accepted legacy router client %s as %s\n", 
buffer, ip_local);
-                       break;
-               default:
-                       /* @TODO: do we really want to accept incorrect 
CONTYPEs? */
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_ERROR("Accepted unknown type %i client %s\n", 
type, buffer);
-                       break;
-       }
-}
-
-/**
- * Handle a tunnel control packet of type data. This was obsoleted by
- * splitting data and control socket and using a different port
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_data(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       PISA_DEBUG(PL_STATEMACHINE, "Received obsolete PISA_PKTTYPE_TUN_DATA 
packet.\n");
-}
-
-/**
- * Handle a tunnel control packet of type register. Register a new client or
- * just send an REGISTER_ACK if the client is already registered. This may
- * happen if the connection is slow and the client sent another REGISTER
- * before he received our first REGISTER_ACK.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_register(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       if (e!=NULL) {
-               inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-               PISA_DEBUG(PL_STATEMACHINE|PL_REGISTER, "Register from already 
connected client %s\n", buffer);
-               pisa_send_register_ack(e);
-       } else {
-               pisa_register_client(addr, pkt);
-       }
-}
-
-/**
  * Handle a tunnel control packet of type heartbeat. Update the last received
  * time for that connection or ignore it if no connection matches.
  * @param pkt PISA control packet
@@ -240,399 +82,3 @@
                PISA_INFO("Heartbeat from client %s\n", buffer);
        }
 }
-
-/**
- * Handle a tunnel control packet of type deregister. Deregister a client or
- * just send a DEREGISTER_ACK if the client is not registered. This may happen
- * if the connection is slow and the client sent another DEREGISTER before he
- * received our first REGISTER_ACK.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_deregister(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-
-       if (e) {
-               pisa_conmgr_remove(sd_ctx.conlist, sd_ctx.natlist, e);
-               PISA_INFO("Deregistered client %s\n", buffer);
-       } else {
-               PISA_DEBUG(PL_STATEMACHINE|PL_DEREGISTER, "Deregister from 
unconnected client %s\n", buffer);
-       }
-       pisa_send_control_packet_ipv6_type(sd_ctx.tunc, addr, 
PISA_PKTTYPE_TUN_DEREGISTER_ACK);
-}
-
-/**
- * Copy a packet from the data socket to the tunnel device.
- */
-void pisa_sd_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_conmgr_entry *entry;
-       pisa_nat_mapping *map;
-       pisa_mac *mac = (pisa_mac *)buffer;
-       struct iphdr *hdr;
-       struct in_addr *srcaddr;
-
-       addrlen = sizeof(from);
-
-       if ((len = recvfrom(sd_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 */
-       if ((entry = pisa_conmgr_findby_address(sd_ctx.conlist, 
&from.sin6_addr)) != NULL) {
-               if (entry->status != PISA_CON_CONNECTED) {
-                       PISA_DEBUG(PL_STATEMACHINE, "Connection exists, but is 
not in CONNECTED state. Should not be possible!\n");
-                       return;
-               }
-
-               if (entry->data_port != ntohs(from.sin6_port)) {
-                       PISA_DEBUG(PL_DATA, "Discarding data from the wrong 
port! expected: %i, is: %i\n", entry->data_port, ntohs(from.sin6_port));
-                       return;
-               }
-
-               /* Check for IPv4 in the payload, otherwise drop it */
-               if ((hdr = pisa_nat_get_iphdr(buffer)) == NULL)
-                       return;
-               srcaddr = (struct in_addr *)&hdr->saddr;
-
-               /* Update the timeout for the connection */
-               time(&entry->timeout_heartbeat);
-
-               if (entry->type == PISA_CONTYPE_LEGACY_ROUTER) {
-                       char mac_str[ARP_STRLEN];
-                       pisa_mac_write_string(mac, mac_str);
-                       map = pisa_nat_mapping_find_by_mac(sd_ctx.natlist, mac);
-                       if ((mac->m[0] == 0) && (mac->m[1] == 0) && (mac->m[2] 
== 0) && (mac->m[3] == 0) && (mac->m[4] == 0) && (mac->m[5] == 0)) {
-                               PISA_DEBUG(PL_DATA, "remote -> local: invalid 
MAC, dropping\n");
-                               return;
-                       }
-                       if (map == NULL) {
-                               struct in_addr local = {0};
-                               char ip_str[INET_ADDRSTRLEN];
-                               if (pisa_client_get_legacy_client_ipv4(&local) 
== 0) {
-                                       PISA_DEBUG(PL_LEGACY, "Could not get a 
dynamic IPv4 for a new legacy client.\n");
-                                       return;
-                               }
-                               map = pisa_nat_add_mapping(sd_ctx.natlist, 
&local, srcaddr, entry, mac);
-                               inet_ntop(AF_INET, &local, ip_str, 
sizeof(ip_str));
-                               PISA_DEBUG(PL_LEGACY, "Adding mapping for 
unknown legacy client: MAC %s IP %s\n", mac_str, ip_str);
-                       } else {
-                               if (map->connection != entry) {
-                                       PISA_DEBUG(PL_LEGACY, "Legacy client 
handover detected for MAC %s\n", mac_str);
-                                       
pisa_nat_update_connection(sd_ctx.natlist, map, entry);
-                               }
-                       }
-               } else {
-                       map = pisa_nat_mapping_find_by_remote(sd_ctx.natlist, 
&from.sin6_addr, srcaddr);
-               }
-
-               /* Change the source IPv4 if a NAT mapping should be applied */
-               if (map != NULL)
-                       pisa_nat_apply(hdr, srcaddr, &map->local_private);
-
-               /* Send the packet out to the tunnel device */
-               if (write(sd_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);
-#ifdef CONFIG_PISA_LOGGING
-                       entry->transferred_bytes+=len;
-                       PISA_DEBUG(PL_DATA, "TOTAL BYTES: 
%i\n",entry->transferred_bytes);
-#endif
-               }
-       } 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.
- */
-void pisa_sd_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;
-       struct sockaddr_in6 addr = {0};
-
-       if ((len = read(sd_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;
-       }
-
-       /* Apply NAT if needed */
-       dst = (struct in_addr *)&hdr->daddr;
-       map = pisa_nat_mapping_find_by_local_private(sd_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;
-               /* @todo: first 6 bytes should be MAC of the receiver */
-       } else {
-               /* No NAT mapping found, just find out which server is the
-                * destination */
-               entry = pisa_conmgr_findby_clientip(sd_ctx.conlist, dst);
-       }
-
-       /* If neither NAT nor a destination server could be found we've run
-        * out of options. The server does not have a default route. */
-       if (entry == NULL) {
-               PISA_ERROR("local -> remote: no destination\n");
-               return;
-       }
-
-       if (entry->status != PISA_CON_CONNECTED) {
-               PISA_DEBUG(PL_STATEMACHINE, "Connection exists, but is not in 
CONNECTED state. Should not be possible!\n");
-               return;
-       } else {
-               /* Send the packet out to the data port of the connection entry 
*/
-               pisa_conmgr_build_sockaddr_data(entry, &addr);
-               if (sendto(sd_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 {
-                       PISA_DEBUG(PL_DATA, "local -> remote: %i bytes\n", len);
-#ifdef CONFIG_PISA_LOGGING
-                       entry->transferred_bytes+=len;
-                       PISA_DEBUG(PL_DATA, "TOTAL BYTES: 
%i\n",entry->transferred_bytes);
-#endif
-               }
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-static void pisa_message_pstun_new_client(char* buffer, struct sockaddr_in6* 
clientIp)
-{
-       pisa_hitlist_entry *con;
-       pisa_conmgr_entry *entry;
-       pisa_packet *pkt = (pisa_packet *)buffer;
-
-       con = pisa_hitlist_find(sd_cfg.hit_allowed, &clientIp->sin6_addr);
-
-       if (!con) {
-               PISA_DEBUG(PL_PREAUTH, "Received a HIT that is not allowed:\n");
-               dump_ipv6_addr(&clientIp->sin6_addr);
-
-               pisa_send_control_packet_ipv6_type(sd_ctx.tunc, clientIp, 
PISA_PKTTYPE_TUN_UNKNOWN_HIT);
-               return;
-       }
-
-       entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-
-       /* General non-tunnel PISA packets */
-       sd_process_pisa_packets_pstun(entry, pkt, clientIp);
-}
-
-/**
- * main logic to processs an incoming packet from an ip, for which we have an
- * association.
- * @sa processIncomingMessage
- * @param e our association structure
- * @param buffer the raw bytes of our message
- * @param len the amount of bytes used in our buffer
- */
-static void pisa_message_pstun_known(pisa_conmgr_entry* e, char* buffer,size_t 
len)
-{
-       pisa_packet *pkt = (pisa_packet *)buffer;
-       struct sockaddr_in6 addr = {0};
-
-       /* General non-tunnel PISA packets */
-       pisa_conmgr_build_sockaddr_data(e, &addr);
-       sd_process_pisa_packets_pstun(e, pkt, &addr);
-}
-
-/**
- * main logic for every message, arriving at our local socket.
- */
-void pisa_message_pstun(int fd)
-{
-       char buffer[1024]={0};
-       unsigned int addrlen=0;
-       ssize_t  len=0;
-
-       struct sockaddr_in6 from= {
-               0
-       };
-
-       addrlen = sizeof (from);
-
-       if ((len=recvfrom (fd, buffer, sizeof (buffer), 0, (struct sockaddr *) 
&from, &addrlen))==-1) {
-               PISA_ERROR("server:main:recvfrom");
-               return;
-       }
-
-       if (!is_pisa_packet(buffer)) {
-               PISA_ERROR("The incoming packet is not from a valid PISA client 
daemon.\n");
-               return;
-       }
-
-       pisa_conmgr_entry *e=pisa_conmgr_findby_address_pstun(sd_ctx.conlist, 
&from.sin6_addr);
-
-       if (e==NULL) { /* case 1: data from new client */
-               pisa_message_pstun_new_client(buffer,&from);
-       } else { /* case 2: data from known client */
-               time(&e->timeout_heartbeat);
-               pisa_message_pstun_known(e,buffer,len);
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-void sd_process_pisa_packets_tunc(pisa_packet *pkt, struct sockaddr_in6* 
clientIp)
-{
-       pisa_conmgr_entry *entry = NULL;
-       pisa_tlv_type pkttype = PISA_PKTTYPE_BU_UNKNOWN;
-
-       pkttype = pisa_get_packet_type(pkt);
-
-       PISA_DEBUG(PL_PREAUTH, "Packet type = %d\n", pisa_get_packet_type(pkt));
-
-       switch (pkttype) {
-       case PISA_PKTTYPE_BU_REQUEST:
-               entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-#ifdef CONFIG_PISA_PERFORMANCE
-               sd_perf_start_benchmark_tokvrfy();
-#endif
-
-               pisasd_packet_handle_func_set.handle_bureq(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_BU_RESPONSE:
-               entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-               pisasd_packet_handle_func_set.handle_bures(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_NE_UNKNOWN:
-       case PISA_PKTTYPE_PA_UNKNOWN:
-       case PISA_PKTTYPE_BU_UNKNOWN:
-               /* drop */
-               PISA_ERROR("Cannot recognize type of the incoming pisasd 
packet.\n");
-               break;
-       default:
-               /* TODO: For now, do nothing.
-                * But we need to find some way to deal with it. */
-               break;
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-void sd_process_pisa_packets_pstun(pisa_conmgr_entry *entry, pisa_packet *pkt, 
struct sockaddr_in6* clientIp)
-{
-       pisa_tlv_type pkttype = PISA_PKTTYPE_BU_UNKNOWN;
-
-       pkttype = pisa_get_packet_type(pkt);
-
-       PISA_DEBUG(PL_PREAUTH, "Packet type = %d\n", pisa_get_packet_type(pkt));
-
-       switch (pkttype) {
-       case PISA_PKTTYPE_VRFY_REQUEST1:
-               pisasd_packet_handle_func_set.handle_vrfyreq1(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_RESPONSE1:
-               pisasd_packet_handle_func_set.handle_vrfyres1(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_REQUEST2:
-               pisasd_packet_handle_func_set.handle_vrfyreq2(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_RESPONSE2:
-#ifdef CONFIG_PISA_PERFORMANCE
-               sd_perf_stop_benchmark_tokvrfy();
-               sd_perf_write_benchmark_tokvrfy();
-#endif
-
-               pisasd_packet_handle_func_set.handle_vrfyres2(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_NE_UNKNOWN:
-       case PISA_PKTTYPE_PA_UNKNOWN:
-       case PISA_PKTTYPE_BU_UNKNOWN:
-               /* drop */
-               PISA_ERROR("Cannot recognize type of the incoming pisasd 
packet.\n");
-               break;
-       default:
-               break;
-       }
-}
-
-#ifdef CONFIG_PISA_PERFORMANCE
-
-/**
- * Start token verification benchmark
- */
-static void sd_perf_start_benchmark_tokvrfy(void)
-{
-       if (!pisasd_bench_tv_on) {
-               PISA_DEBUG(PL_PREAUTH, "Starting PERF_TOKEN_VERIFICATION\n");
-               pisa_perf_start_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-
-               pisasd_bench_tv_on = TRUE;
-       }
-}
-
-/**
- * Stop token verification benchmark
- */
-static void sd_perf_stop_benchmark_tokvrfy(void)
-{
-       PISA_DEBUG(PL_PREAUTH, "Stopping PERF_TOKEN_VERIFICATION\n");
-       pisa_perf_stop_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-
-       pisasd_bench_tv_on = FALSE;
-}
-
-/**
- * Write token verification benchmark results
- */
-static void sd_perf_write_benchmark_tokvrfy(void)
-{
-       PISA_DEBUG(PL_PREAUTH, "Writing PERF_TOKEN_VERIFICATION\n");
-       pisa_perf_write_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-}
-
-#endif

Copied and modified: trunk/pisasd/sdheartbeat.h (from r1043, 
trunk/pisasd/sdtun.h)
==============================================================================
--- trunk/pisasd/sdtun.h        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisasd/sdheartbeat.h  Thu Oct  8 15:02:47 2009        (r1053)
@@ -4,15 +4,16 @@
  */
 
 /**
- * @file sdtun.h
- * @brief Header file of the context information for the PISA server daemon.
+ * @file sdheartbeat.h
+ * @brief Header for heartbeat related functions for the PISA server daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
  * @author Dongsu Park <dpark1978@xxxxxxxxx>
- * @date Mar. 2009
+ * @author Thomas Jansen <mithi@xxxxxxxxx>
+ * @date Oct. 2009
  */
 
-#ifndef PISA_SDTUN_H
-#define PISA_SDTUN_H
+#ifndef PISA_SDHEARTBEAT_H
+#define PISA_SDHEARTBEAT_H
 
 #include "packet.h"
 #include "conmgr.h"
@@ -20,23 +21,9 @@
 /**
  * Function prototypes
  */
-
-void pisa_message_pstun(int fd);
-void sd_process_pisa_packets_tunc(pisa_packet *pkt, struct sockaddr_in6* 
clientIp);
-void sd_process_pisa_packets_pstun(pisa_conmgr_entry *entry, pisa_packet *pkt, 
struct sockaddr_in6* clientIp);
-
-void processTunnelCtrlFDs(void* data, pisa_conmgr_entry* e);
-void processTunnelDataFDs(void* data, pisa_conmgr_entry* e);
-
 void pisa_sd_timeout_collect(void);
 
-void pisa_sd_copy_from_sock_to_tun(void);
-void pisa_sd_copy_from_tun_to_sock(void);
-
 /* handlers for tunnel control packets */
-void pisa_recv_data(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_register(pisa_packet *pkt, struct sockaddr_in6 *addr);
 void pisa_recv_heartbeat(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_deregister(pisa_packet *pkt, struct sockaddr_in6 *addr);
 
-#endif /* PISA_SDTUN_H */
+#endif /* PISA_SDHEARTBEAT_H */

Modified: trunk/pisasd/sdmain.c
==============================================================================
--- trunk/pisasd/sdmain.c       Thu Oct  8 14:05:22 2009        (r1052)
+++ trunk/pisasd/sdmain.c       Thu Oct  8 15:02:47 2009        (r1053)
@@ -31,7 +31,10 @@
 
 #include "sdconf.h"
 #include "sdctx.h"
+#include "sdderegister.h"
+#include "sdheartbeat.h"
 #include "sdmsg.h"
+#include "sdregister.h"
 #include "sdtun.h"
 #include "ctrlhandler.h"
 #include "pisaconf.h"

Copied and modified: trunk/pisasd/sdregister.c (from r1043, 
trunk/pisasd/sdtun.c)
==============================================================================
--- trunk/pisasd/sdtun.c        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisasd/sdregister.c   Thu Oct  8 15:02:47 2009        (r1053)
@@ -1,86 +1,21 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
- * @file sdtun.c
+ * @file sdregister.c
  * @brief Implementation of tunnel modules in the PISA server daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
- * @date Oct. 2008
+ * @author Thomas Jansen <mithi@xxxxxxxxx>
+ * @date Oct. 2009
  */
 
-#include <netinet/ip.h>
-
-#include "socket.h"
 #include "conmgr.h"
 #include "hitlist.h"
 #include "sdctx.h"
-#include "sdtun.h"
 #include "sdconf.h"
-#include "nat.h"
 #include "sdclients.h"
-#include "arp.h"
-
-#ifdef CONFIG_PISA_LOGGING
-# include "log.h"
-#endif
-
-#ifdef CONFIG_PISA_PERFORMANCE
-# include "pisaperf.h"
-#endif
-
-#define DEFAULT_BUFF_SIZE 15000
-
-/**
- * Time to wait until a connection is removed from the internal list of life 
connections.
- * @sa removeTimedoutConnectionsFlag
- * @sa process
- */
-static time_t waitingPeriod=30;
-
-#ifdef CONFIG_PISA_PERFORMANCE
-static int pisasd_bench_tv_on = FALSE;
-static void sd_perf_start_benchmark_tokvrfy(void);
-static void sd_perf_stop_benchmark_tokvrfy(void);
-static void sd_perf_write_benchmark_tokvrfy(void);
-#endif
-
-extern pisa_packet_handle_func_set pisasd_packet_handle_func_set;
-
-/**
- * Check if a connection has timed out and remove it.
- * @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 cb_check_timeout(void* data,pisa_conmgr_entry* entry)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       time_t currentTime = *((time_t*)data);
-
-       if ((entry->timeout_heartbeat + waitingPeriod) < currentTime) {
-               inet_ntop(AF_INET6, &entry->hit, buffer, sizeof(buffer));
-               PISA_INFO("Removing timed out client %s\n", buffer);
-               pisa_conmgr_remove(sd_ctx.conlist, sd_ctx.natlist, entry);
-       }
-}
-
-/**
- * Remove all connections from our internal list that have timed out.
- * Only check every 10 seconds for it.
- */
-void pisa_sd_timeout_collect(void)
-{
-       static time_t last = 0;
-       time_t current;
-
-       time(&current);
-       if (last + 10 < current) {
-               pisa_conmgr_iterate(sd_ctx.conlist, cb_check_timeout, &current);
-               last = current;
-       }
-}
 
 /**
  * send an RegisterAck to a client.
@@ -190,17 +125,6 @@
 }
 
 /**
- * Handle a tunnel control packet of type data. This was obsoleted by
- * splitting data and control socket and using a different port
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_data(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       PISA_DEBUG(PL_STATEMACHINE, "Received obsolete PISA_PKTTYPE_TUN_DATA 
packet.\n");
-}
-
-/**
  * Handle a tunnel control packet of type register. Register a new client or
  * just send an REGISTER_ACK if the client is already registered. This may
  * happen if the connection is slow and the client sent another REGISTER
@@ -221,418 +145,3 @@
                pisa_register_client(addr, pkt);
        }
 }
-
-/**
- * Handle a tunnel control packet of type heartbeat. Update the last received
- * time for that connection or ignore it if no connection matches.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_heartbeat(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       if (e) {
-               time(&e->timeout_heartbeat);
-               pisa_send_control_packet_ipv6_type(sd_ctx.tunc, addr, 
PISA_PKTTYPE_TUN_HEARTBEAT_ACK);
-               inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-               PISA_INFO("Heartbeat from client %s\n", buffer);
-       }
-}
-
-/**
- * Handle a tunnel control packet of type deregister. Deregister a client or
- * just send a DEREGISTER_ACK if the client is not registered. This may happen
- * if the connection is slow and the client sent another DEREGISTER before he
- * received our first REGISTER_ACK.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_deregister(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-
-       if (e) {
-               pisa_conmgr_remove(sd_ctx.conlist, sd_ctx.natlist, e);
-               PISA_INFO("Deregistered client %s\n", buffer);
-       } else {
-               PISA_DEBUG(PL_STATEMACHINE|PL_DEREGISTER, "Deregister from 
unconnected client %s\n", buffer);
-       }
-       pisa_send_control_packet_ipv6_type(sd_ctx.tunc, addr, 
PISA_PKTTYPE_TUN_DEREGISTER_ACK);
-}
-
-/**
- * Copy a packet from the data socket to the tunnel device.
- */
-void pisa_sd_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_conmgr_entry *entry;
-       pisa_nat_mapping *map;
-       pisa_mac *mac = (pisa_mac *)buffer;
-       struct iphdr *hdr;
-       struct in_addr *srcaddr;
-
-       addrlen = sizeof(from);
-
-       if ((len = recvfrom(sd_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 */
-       if ((entry = pisa_conmgr_findby_address(sd_ctx.conlist, 
&from.sin6_addr)) != NULL) {
-               if (entry->status != PISA_CON_CONNECTED) {
-                       PISA_DEBUG(PL_STATEMACHINE, "Connection exists, but is 
not in CONNECTED state. Should not be possible!\n");
-                       return;
-               }
-
-               if (entry->data_port != ntohs(from.sin6_port)) {
-                       PISA_DEBUG(PL_DATA, "Discarding data from the wrong 
port! expected: %i, is: %i\n", entry->data_port, ntohs(from.sin6_port));
-                       return;
-               }
-
-               /* Check for IPv4 in the payload, otherwise drop it */
-               if ((hdr = pisa_nat_get_iphdr(buffer)) == NULL)
-                       return;
-               srcaddr = (struct in_addr *)&hdr->saddr;
-
-               /* Update the timeout for the connection */
-               time(&entry->timeout_heartbeat);
-
-               if (entry->type == PISA_CONTYPE_LEGACY_ROUTER) {
-                       char mac_str[ARP_STRLEN];
-                       pisa_mac_write_string(mac, mac_str);
-                       map = pisa_nat_mapping_find_by_mac(sd_ctx.natlist, mac);
-                       if ((mac->m[0] == 0) && (mac->m[1] == 0) && (mac->m[2] 
== 0) && (mac->m[3] == 0) && (mac->m[4] == 0) && (mac->m[5] == 0)) {
-                               PISA_DEBUG(PL_DATA, "remote -> local: invalid 
MAC, dropping\n");
-                               return;
-                       }
-                       if (map == NULL) {
-                               struct in_addr local = {0};
-                               char ip_str[INET_ADDRSTRLEN];
-                               if (pisa_client_get_legacy_client_ipv4(&local) 
== 0) {
-                                       PISA_DEBUG(PL_LEGACY, "Could not get a 
dynamic IPv4 for a new legacy client.\n");
-                                       return;
-                               }
-                               map = pisa_nat_add_mapping(sd_ctx.natlist, 
&local, srcaddr, entry, mac);
-                               inet_ntop(AF_INET, &local, ip_str, 
sizeof(ip_str));
-                               PISA_DEBUG(PL_LEGACY, "Adding mapping for 
unknown legacy client: MAC %s IP %s\n", mac_str, ip_str);
-                       } else {
-                               if (map->connection != entry) {
-                                       PISA_DEBUG(PL_LEGACY, "Legacy client 
handover detected for MAC %s\n", mac_str);
-                                       
pisa_nat_update_connection(sd_ctx.natlist, map, entry);
-                               }
-                       }
-               } else {
-                       map = pisa_nat_mapping_find_by_remote(sd_ctx.natlist, 
&from.sin6_addr, srcaddr);
-               }
-
-               /* Change the source IPv4 if a NAT mapping should be applied */
-               if (map != NULL)
-                       pisa_nat_apply(hdr, srcaddr, &map->local_private);
-
-               /* Send the packet out to the tunnel device */
-               if (write(sd_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);
-#ifdef CONFIG_PISA_LOGGING
-                       entry->transferred_bytes+=len;
-                       PISA_DEBUG(PL_DATA, "TOTAL BYTES: 
%i\n",entry->transferred_bytes);
-#endif
-               }
-       } 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.
- */
-void pisa_sd_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;
-       struct sockaddr_in6 addr = {0};
-
-       if ((len = read(sd_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;
-       }
-
-       /* Apply NAT if needed */
-       dst = (struct in_addr *)&hdr->daddr;
-       map = pisa_nat_mapping_find_by_local_private(sd_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;
-               /* @todo: first 6 bytes should be MAC of the receiver */
-       } else {
-               /* No NAT mapping found, just find out which server is the
-                * destination */
-               entry = pisa_conmgr_findby_clientip(sd_ctx.conlist, dst);
-       }
-
-       /* If neither NAT nor a destination server could be found we've run
-        * out of options. The server does not have a default route. */
-       if (entry == NULL) {
-               PISA_ERROR("local -> remote: no destination\n");
-               return;
-       }
-
-       if (entry->status != PISA_CON_CONNECTED) {
-               PISA_DEBUG(PL_STATEMACHINE, "Connection exists, but is not in 
CONNECTED state. Should not be possible!\n");
-               return;
-       } else {
-               /* Send the packet out to the data port of the connection entry 
*/
-               pisa_conmgr_build_sockaddr_data(entry, &addr);
-               if (sendto(sd_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 {
-                       PISA_DEBUG(PL_DATA, "local -> remote: %i bytes\n", len);
-#ifdef CONFIG_PISA_LOGGING
-                       entry->transferred_bytes+=len;
-                       PISA_DEBUG(PL_DATA, "TOTAL BYTES: 
%i\n",entry->transferred_bytes);
-#endif
-               }
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-static void pisa_message_pstun_new_client(char* buffer, struct sockaddr_in6* 
clientIp)
-{
-       pisa_hitlist_entry *con;
-       pisa_conmgr_entry *entry;
-       pisa_packet *pkt = (pisa_packet *)buffer;
-
-       con = pisa_hitlist_find(sd_cfg.hit_allowed, &clientIp->sin6_addr);
-
-       if (!con) {
-               PISA_DEBUG(PL_PREAUTH, "Received a HIT that is not allowed:\n");
-               dump_ipv6_addr(&clientIp->sin6_addr);
-
-               pisa_send_control_packet_ipv6_type(sd_ctx.tunc, clientIp, 
PISA_PKTTYPE_TUN_UNKNOWN_HIT);
-               return;
-       }
-
-       entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-
-       /* General non-tunnel PISA packets */
-       sd_process_pisa_packets_pstun(entry, pkt, clientIp);
-}
-
-/**
- * main logic to processs an incoming packet from an ip, for which we have an
- * association.
- * @sa processIncomingMessage
- * @param e our association structure
- * @param buffer the raw bytes of our message
- * @param len the amount of bytes used in our buffer
- */
-static void pisa_message_pstun_known(pisa_conmgr_entry* e, char* buffer,size_t 
len)
-{
-       pisa_packet *pkt = (pisa_packet *)buffer;
-       struct sockaddr_in6 addr = {0};
-
-       /* General non-tunnel PISA packets */
-       pisa_conmgr_build_sockaddr_data(e, &addr);
-       sd_process_pisa_packets_pstun(e, pkt, &addr);
-}
-
-/**
- * main logic for every message, arriving at our local socket.
- */
-void pisa_message_pstun(int fd)
-{
-       char buffer[1024]={0};
-       unsigned int addrlen=0;
-       ssize_t  len=0;
-
-       struct sockaddr_in6 from= {
-               0
-       };
-
-       addrlen = sizeof (from);
-
-       if ((len=recvfrom (fd, buffer, sizeof (buffer), 0, (struct sockaddr *) 
&from, &addrlen))==-1) {
-               PISA_ERROR("server:main:recvfrom");
-               return;
-       }
-
-       if (!is_pisa_packet(buffer)) {
-               PISA_ERROR("The incoming packet is not from a valid PISA client 
daemon.\n");
-               return;
-       }
-
-       pisa_conmgr_entry *e=pisa_conmgr_findby_address_pstun(sd_ctx.conlist, 
&from.sin6_addr);
-
-       if (e==NULL) { /* case 1: data from new client */
-               pisa_message_pstun_new_client(buffer,&from);
-       } else { /* case 2: data from known client */
-               time(&e->timeout_heartbeat);
-               pisa_message_pstun_known(e,buffer,len);
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-void sd_process_pisa_packets_tunc(pisa_packet *pkt, struct sockaddr_in6* 
clientIp)
-{
-       pisa_conmgr_entry *entry = NULL;
-       pisa_tlv_type pkttype = PISA_PKTTYPE_BU_UNKNOWN;
-
-       pkttype = pisa_get_packet_type(pkt);
-
-       PISA_DEBUG(PL_PREAUTH, "Packet type = %d\n", pisa_get_packet_type(pkt));
-
-       switch (pkttype) {
-       case PISA_PKTTYPE_BU_REQUEST:
-               entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-#ifdef CONFIG_PISA_PERFORMANCE
-               sd_perf_start_benchmark_tokvrfy();
-#endif
-
-               pisasd_packet_handle_func_set.handle_bureq(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_BU_RESPONSE:
-               entry = pisa_conmgr_add_pstun(sd_ctx.conlist, clientIp);
-               pisasd_packet_handle_func_set.handle_bures(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_NE_UNKNOWN:
-       case PISA_PKTTYPE_PA_UNKNOWN:
-       case PISA_PKTTYPE_BU_UNKNOWN:
-               /* drop */
-               PISA_ERROR("Cannot recognize type of the incoming pisasd 
packet.\n");
-               break;
-       default:
-               /* TODO: For now, do nothing.
-                * But we need to find some way to deal with it. */
-               break;
-       }
-}
-
-/**
- * main logic for an incoming message from an unassociated ipv6 address.
- * if the address is in the list of known hits, the client will be added
- * to our internal list... else, an UnkownHit Message will be send.
- * consult the client/server communication graphics /tables for details.
- * @sa processIncomingMessage
- * @param buffer this points to the raw bytes of our incoming message
- * @param clientIp the ip from where the incoming message originated.
- */
-void sd_process_pisa_packets_pstun(pisa_conmgr_entry *entry, pisa_packet *pkt, 
struct sockaddr_in6* clientIp)
-{
-       pisa_tlv_type pkttype = PISA_PKTTYPE_BU_UNKNOWN;
-
-       pkttype = pisa_get_packet_type(pkt);
-
-       PISA_DEBUG(PL_PREAUTH, "Packet type = %d\n", pisa_get_packet_type(pkt));
-
-       switch (pkttype) {
-       case PISA_PKTTYPE_VRFY_REQUEST1:
-               pisasd_packet_handle_func_set.handle_vrfyreq1(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_RESPONSE1:
-               pisasd_packet_handle_func_set.handle_vrfyres1(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_REQUEST2:
-               pisasd_packet_handle_func_set.handle_vrfyreq2(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_VRFY_RESPONSE2:
-#ifdef CONFIG_PISA_PERFORMANCE
-               sd_perf_stop_benchmark_tokvrfy();
-               sd_perf_write_benchmark_tokvrfy();
-#endif
-
-               pisasd_packet_handle_func_set.handle_vrfyres2(entry, pkt);
-               break;
-
-       case PISA_PKTTYPE_NE_UNKNOWN:
-       case PISA_PKTTYPE_PA_UNKNOWN:
-       case PISA_PKTTYPE_BU_UNKNOWN:
-               /* drop */
-               PISA_ERROR("Cannot recognize type of the incoming pisasd 
packet.\n");
-               break;
-       default:
-               break;
-       }
-}
-
-#ifdef CONFIG_PISA_PERFORMANCE
-
-/**
- * Start token verification benchmark
- */
-static void sd_perf_start_benchmark_tokvrfy(void)
-{
-       if (!pisasd_bench_tv_on) {
-               PISA_DEBUG(PL_PREAUTH, "Starting PERF_TOKEN_VERIFICATION\n");
-               pisa_perf_start_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-
-               pisasd_bench_tv_on = TRUE;
-       }
-}
-
-/**
- * Stop token verification benchmark
- */
-static void sd_perf_stop_benchmark_tokvrfy(void)
-{
-       PISA_DEBUG(PL_PREAUTH, "Stopping PERF_TOKEN_VERIFICATION\n");
-       pisa_perf_stop_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-
-       pisasd_bench_tv_on = FALSE;
-}
-
-/**
- * Write token verification benchmark results
- */
-static void sd_perf_write_benchmark_tokvrfy(void)
-{
-       PISA_DEBUG(PL_PREAUTH, "Writing PERF_TOKEN_VERIFICATION\n");
-       pisa_perf_write_benchmark(pisa_perf, PERF_TOKEN_VERIFICATION);
-}
-
-#endif

Copied and modified: trunk/pisasd/sdregister.h (from r1043, 
trunk/pisasd/sdtun.h)
==============================================================================
--- trunk/pisasd/sdtun.h        Wed Oct  7 19:25:53 2009        (r1043, copy 
source)
+++ trunk/pisasd/sdregister.h   Thu Oct  8 15:02:47 2009        (r1053)
@@ -1,42 +1,23 @@
 /*
- * Copyright (c) 2008, Distributed Systems Group, RWTH Aachen
+ * Copyright (c) 2009, Distributed Systems Group, RWTH Aachen
  * All rights reserved.
  */
 
 /**
- * @file sdtun.h
- * @brief Header file of the context information for the PISA server daemon.
+ * @file sdregister.h
+ * @brief Header for register related functions for the PISA server daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
  * @author Dongsu Park <dpark1978@xxxxxxxxx>
- * @date Mar. 2009
+ * @author Thomas Jansen <mithi@xxxxxxxxx>
+ * @date Oct. 2009
  */
 
-#ifndef PISA_SDTUN_H
-#define PISA_SDTUN_H
+#ifndef PISA_SDREGISTER_H
+#define PISA_SDREGISTER_H
 
 #include "packet.h"
-#include "conmgr.h"
 
-/**
- * Function prototypes
- */
-
-void pisa_message_pstun(int fd);
-void sd_process_pisa_packets_tunc(pisa_packet *pkt, struct sockaddr_in6* 
clientIp);
-void sd_process_pisa_packets_pstun(pisa_conmgr_entry *entry, pisa_packet *pkt, 
struct sockaddr_in6* clientIp);
-
-void processTunnelCtrlFDs(void* data, pisa_conmgr_entry* e);
-void processTunnelDataFDs(void* data, pisa_conmgr_entry* e);
-
-void pisa_sd_timeout_collect(void);
-
-void pisa_sd_copy_from_sock_to_tun(void);
-void pisa_sd_copy_from_tun_to_sock(void);
-
-/* handlers for tunnel control packets */
-void pisa_recv_data(pisa_packet *pkt, struct sockaddr_in6 *addr);
+/* handler for register tunnel control packets */
 void pisa_recv_register(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_heartbeat(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_deregister(pisa_packet *pkt, struct sockaddr_in6 *addr);
 
-#endif /* PISA_SDTUN_H */
+#endif /* PISA_SDREGISTER_H */

Modified: trunk/pisasd/sdtun.c
==============================================================================
--- trunk/pisasd/sdtun.c        Thu Oct  8 14:05:22 2009        (r1052)
+++ trunk/pisasd/sdtun.c        Thu Oct  8 15:02:47 2009        (r1053)
@@ -32,13 +32,6 @@
 
 #define DEFAULT_BUFF_SIZE 15000
 
-/**
- * Time to wait until a connection is removed from the internal list of life 
connections.
- * @sa removeTimedoutConnectionsFlag
- * @sa process
- */
-static time_t waitingPeriod=30;
-
 #ifdef CONFIG_PISA_PERFORMANCE
 static int pisasd_bench_tv_on = FALSE;
 static void sd_perf_start_benchmark_tokvrfy(void);
@@ -49,147 +42,6 @@
 extern pisa_packet_handle_func_set pisasd_packet_handle_func_set;
 
 /**
- * Check if a connection has timed out and remove it.
- * @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 cb_check_timeout(void* data,pisa_conmgr_entry* entry)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       time_t currentTime = *((time_t*)data);
-
-       if ((entry->timeout_heartbeat + waitingPeriod) < currentTime) {
-               inet_ntop(AF_INET6, &entry->hit, buffer, sizeof(buffer));
-               PISA_INFO("Removing timed out client %s\n", buffer);
-               pisa_conmgr_remove(sd_ctx.conlist, sd_ctx.natlist, entry);
-       }
-}
-
-/**
- * Remove all connections from our internal list that have timed out.
- * Only check every 10 seconds for it.
- */
-void pisa_sd_timeout_collect(void)
-{
-       static time_t last = 0;
-       time_t current;
-
-       time(&current);
-       if (last + 10 < current) {
-               pisa_conmgr_iterate(sd_ctx.conlist, cb_check_timeout, &current);
-               last = current;
-       }
-}
-
-/**
- * send an RegisterAck to a client.
- * consult the client/server communication graphics /tables for details.
- * @param e client connection
- */
-static void pisa_send_register_ack(pisa_conmgr_entry* e)
-{
-       struct sockaddr_in6 addr = {0};
-       pisa_packet *packet = pisa_alloc_msg();
-
-       pisa_ipv4_copy(&packet->body.reg_ack.client, &e->ipv4);
-       pisa_ipv4_copy(&packet->body.reg_ack.server, &sd_cfg.ipaddr);
-
-       pisa_set_packet_type(packet, PISA_PKTTYPE_TUN_REGISTER_ACK);
-       pisa_set_packet_length(packet, sizeof(pisa_packet_common) + 
sizeof(pisa_payload_registerack));
-
-       pisa_conmgr_build_sockaddr_control(e, &addr);
-       pisa_send_control_packet_ipv6(sd_ctx.tunc, &addr, packet);
-       pisa_free_msg(packet);
-}
-
-/**
- * Accept a new client into our conmgr list and send REGISTER_ACK.
- * @param addr control port and HIT of the new client
- * @param type connection type
- * @param client_ipv4 IPv4 for the client
- * @return the new conmgr entry
- */
-static pisa_conmgr_entry *pisa_register_client_accept(struct sockaddr_in6 
*addr, uint16_t type, struct in_addr *client_ipv4)
-{
-       pisa_conmgr_entry *entry;
-
-       entry = pisa_conmgr_add(sd_ctx.conlist, &addr->sin6_addr, 
ntohs(addr->sin6_port));
-       pisa_conmgr_connected(sd_ctx.conlist, entry, 
PISASD_DEFAULT_PORTNUM_DATA, client_ipv4);
-       entry->type = type;
-       pisa_send_register_ack(entry);
-       entry->status = PISA_CON_CONNECTED;
-
-       return entry;
-}
-
-/**
- * Register a client which is in the set of allowed HIT's
- * and send a RegisterAck message to the client.
- * consult the client/server communication graphics /tables for details.
- * @param addr HIT/control port to associate our new connection with
- * @param pkt REGISTER packet for additional information
- */
-static void pisa_register_client(struct sockaddr_in6* addr, pisa_packet *pkt)
-{
-       char buffer[INET6_ADDRSTRLEN], ip_local[INET6_ADDRSTRLEN], 
ip_remote[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry* e=NULL;
-       pisa_hitlist_entry *con;
-       struct in_addr client_ipv4;
-       uint16_t type;
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-
-
-       if (pisa_client_get_local_ipv4(&addr->sin6_addr, &client_ipv4) != 1) {
-               PISA_ERROR("Could not find a local IPv4 for client %s\n", 
buffer);
-               return;
-               /* @todo some sort of error message for the client? */
-       }
-
-       inet_ntop(AF_INET, &client_ipv4, ip_local, sizeof(ip_local));
-       type = ntohs(pkt->body.reg.type);
-       switch (type) {
-               case PISA_CONTYPE_RELAY:
-                       con = pisa_hitlist_find(sd_cfg.hit_allowed, 
&addr->sin6_addr);
-
-                       if (con == NULL) {
-                               PISA_ERROR("HIT not allowed.\n");
-                               return;
-                       }
-
-                       if (pisa_hitlist_entry_expired(con)) {
-                               PISA_ERROR("HIT expired.\n");
-                               return;
-                       }
-
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_INFO("Accepted relay client %s as %s\n", buffer, 
ip_local);
-#ifdef CONFIG_PISA_LOGGING
-                       /* Save start of connection time and log the start of a 
new connection. */
-                       e->start_time = time(NULL);
-                       log_start_connection(buffer, &client_ipv4);
-#endif
-                       break;
-               case PISA_CONTYPE_SERVICE:
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       pisa_nat_add_mapping(sd_ctx.natlist, &client_ipv4, 
&pkt->body.reg.local, e, NULL);
-                       inet_ntop(AF_INET, &pkt->body.reg.local, ip_remote, 
sizeof(ip_remote));
-                       PISA_INFO("Accepted service client %s as %s with remote 
%s\n", buffer, ip_local, ip_remote);
-                       break;
-               case PISA_CONTYPE_LEGACY_ROUTER:
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_INFO("Accepted legacy router client %s as %s\n", 
buffer, ip_local);
-                       break;
-               default:
-                       /* @TODO: do we really want to accept incorrect 
CONTYPEs? */
-                       e = pisa_register_client_accept(addr, type, 
&client_ipv4);
-                       PISA_ERROR("Accepted unknown type %i client %s\n", 
type, buffer);
-                       break;
-       }
-}
-
-/**
  * Handle a tunnel control packet of type data. This was obsoleted by
  * splitting data and control socket and using a different port
  * @param pkt PISA control packet
@@ -201,71 +53,6 @@
 }
 
 /**
- * Handle a tunnel control packet of type register. Register a new client or
- * just send an REGISTER_ACK if the client is already registered. This may
- * happen if the connection is slow and the client sent another REGISTER
- * before he received our first REGISTER_ACK.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_register(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       if (e!=NULL) {
-               inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-               PISA_DEBUG(PL_STATEMACHINE|PL_REGISTER, "Register from already 
connected client %s\n", buffer);
-               pisa_send_register_ack(e);
-       } else {
-               pisa_register_client(addr, pkt);
-       }
-}
-
-/**
- * Handle a tunnel control packet of type heartbeat. Update the last received
- * time for that connection or ignore it if no connection matches.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_heartbeat(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       if (e) {
-               time(&e->timeout_heartbeat);
-               pisa_send_control_packet_ipv6_type(sd_ctx.tunc, addr, 
PISA_PKTTYPE_TUN_HEARTBEAT_ACK);
-               inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-               PISA_INFO("Heartbeat from client %s\n", buffer);
-       }
-}
-
-/**
- * Handle a tunnel control packet of type deregister. Deregister a client or
- * just send a DEREGISTER_ACK if the client is not registered. This may happen
- * if the connection is slow and the client sent another DEREGISTER before he
- * received our first REGISTER_ACK.
- * @param pkt PISA control packet
- * @param addr HIT/port of the sender
- */
-void pisa_recv_deregister(pisa_packet *pkt, struct sockaddr_in6 *addr)
-{
-       char buffer[INET6_ADDRSTRLEN];
-       pisa_conmgr_entry *e = pisa_conmgr_findby_address(sd_ctx.conlist, 
&addr->sin6_addr);
-
-       inet_ntop(AF_INET6, &addr->sin6_addr, buffer, sizeof(buffer));
-
-       if (e) {
-               pisa_conmgr_remove(sd_ctx.conlist, sd_ctx.natlist, e);
-               PISA_INFO("Deregistered client %s\n", buffer);
-       } else {
-               PISA_DEBUG(PL_STATEMACHINE|PL_DEREGISTER, "Deregister from 
unconnected client %s\n", buffer);
-       }
-       pisa_send_control_packet_ipv6_type(sd_ctx.tunc, addr, 
PISA_PKTTYPE_TUN_DEREGISTER_ACK);
-}
-
-/**
  * Copy a packet from the data socket to the tunnel device.
  */
 void pisa_sd_copy_from_sock_to_tun(void)

Modified: trunk/pisasd/sdtun.h
==============================================================================
--- trunk/pisasd/sdtun.h        Thu Oct  8 14:05:22 2009        (r1052)
+++ trunk/pisasd/sdtun.h        Thu Oct  8 15:02:47 2009        (r1053)
@@ -8,6 +8,7 @@
  * @brief Header file of the context information for the PISA server daemon.
  * @author Wolfram Fischer <papierfalter@xxxxxxxxx>
  * @author Dongsu Park <dpark1978@xxxxxxxxx>
+ * @author Thomas Jansen <mithi@xxxxxxxxx>
  * @date Mar. 2009
  */
 
@@ -28,15 +29,10 @@
 void processTunnelCtrlFDs(void* data, pisa_conmgr_entry* e);
 void processTunnelDataFDs(void* data, pisa_conmgr_entry* e);
 
-void pisa_sd_timeout_collect(void);
-
 void pisa_sd_copy_from_sock_to_tun(void);
 void pisa_sd_copy_from_tun_to_sock(void);
 
-/* handlers for tunnel control packets */
+/* handler for (obsolete) data tunnel control packets */
 void pisa_recv_data(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_register(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_heartbeat(pisa_packet *pkt, struct sockaddr_in6 *addr);
-void pisa_recv_deregister(pisa_packet *pkt, struct sockaddr_in6 *addr);
 
 #endif /* PISA_SDTUN_H */

Other related posts:

  • » [pisa-src] r1053 - trunk/pisasd - Thomas Jansen