Permalink
Browse files

Saving and processing unknown incoming fragments

New data structure -- linked list
Reduced few useless debug outputs
  • Loading branch information...
1 parent b9c6d5c commit 24b09b4d9334ad6366e79d1af0f04793ba77a920 @xHire committed Jul 14, 2012
Showing with 246 additions and 40 deletions.
  1. +1 −0 src/Makefile.am
  2. +0 −1 src/ipv4.c
  3. +0 −1 src/ipv6.c
  4. +104 −0 src/linkedlist.c
  5. +40 −0 src/linkedlist.h
  6. +18 −19 src/nat.c
  7. +3 −4 src/nat.h
  8. +80 −13 src/tcp.c
  9. +0 −2 src/wrapper.c
View
@@ -7,6 +7,7 @@ wrapsix_wrapper_SOURCES = \
icmp.c icmp.h \
ipv4.c ipv4.h \
ipv6.c ipv6.h \
+ linkedlist.c linkedlist.h \
log.c log.h \
nat.c nat.h \
radixtree.c radixtree.h \
View
@@ -47,7 +47,6 @@ int ipv4(struct s_ethernet *eth, char *packet)
/* test if this packet belongs to us */
if (memcmp(&wrapsix_ipv4_addr, &ip->ip_dest, 4) != 0) {
- log_debug("This is unfamiliar IPv4 packet");
return 1;
}
View
@@ -47,7 +47,6 @@ int ipv6(struct s_ethernet *eth, char *packet)
/* test if this packet belongs to us */
if (memcmp(&wrapsix_ipv6_prefix, &ip->ip_dest, 12) != 0 &&
memcmp(&ndp_multicast_addr, &ip->ip_dest, 13) != 0) {
- log_debug("This is unfamiliar IPv6 packet");
return 1;
}
View
@@ -0,0 +1,104 @@
+/*
+ * WrapSix
+ * Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h> /* free, malloc */
+
+#include "linkedlist.h"
+#include "log.h"
+
+/**
+ * Creates root of a linked list.
+ *
+ * @return Root of new linked list
+ */
+linkedlist_t *linkedlist_create(void)
+{
+ linkedlist_t *linkedlist;
+
+ if ((linkedlist = (linkedlist_t *) malloc(sizeof(linkedlist_t))) ==
+ NULL) {
+ log_error("Lack of free memory");
+ return NULL;
+ }
+ linkedlist->first.prev = NULL;
+ linkedlist->first.next = &linkedlist->last;
+ linkedlist->last.prev = &linkedlist->first;
+ linkedlist->last.next = NULL;
+
+ return linkedlist;
+}
+
+/**
+ * Destroys a linked list.
+ *
+ * @param root Root of the linked list to destroy
+ */
+void linkedlist_destroy(linkedlist_t *root)
+{
+ linkedlist_node_t *tmp;
+
+ tmp = root->first.next;
+ while (tmp->next != NULL) {
+ tmp = tmp->next;
+ free(tmp->prev);
+ }
+}
+
+/**
+ * Append new node at the end of the linked list.
+ *
+ * @param root Root of the linked list
+ * @param data Data to append to the list
+ *
+ * @return 0 for success
+ * @return 1 for failure
+ */
+int linkedlist_append(linkedlist_t *root, void *data)
+{
+ linkedlist_node_t *node;
+
+ if ((node = (linkedlist_node_t *) malloc(sizeof(linkedlist_node_t))) ==
+ NULL) {
+ log_error("Lack of free memory");
+ return 1;
+ }
+
+ node->data = data;
+
+ node->prev = root->last.prev;
+ node->next = &root->last;
+ root->last.prev->next = node;
+ root->last.prev = node;
+
+ return 0;
+}
+
+/**
+ * Delete node from beginning of linked list.
+ *
+ * @param root Root of the linked list
+ * @param node Node to delete from the list
+ */
+void linkedlist_delete(linkedlist_t *root, linkedlist_node_t *node)
+{
+ root->first.next = node->next;
+ node->next->prev = node->prev;
+
+ free(node);
+ node = NULL;
+}
View
@@ -0,0 +1,40 @@
+/*
+ * WrapSix
+ * Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LINKEDLIST_H
+#define LINKEDLIST_H
+
+/* Linked list node structure */
+typedef struct s_linkedlist_node {
+ struct s_linkedlist_node *prev;
+ struct s_linkedlist_node *next;
+ void *data;
+} linkedlist_node_t;
+
+/* Linked list root structure */
+typedef struct s_linkedlist {
+ linkedlist_node_t first;
+ linkedlist_node_t last;
+} linkedlist_t;
+
+linkedlist_t *linkedlist_create(void);
+void linkedlist_destroy(linkedlist_t *root);
+int linkedlist_append(linkedlist_t *root, void *data);
+void linkedlist_delete(linkedlist_t *root, linkedlist_node_t *node);
+
+#endif /* LINKEDLIST_H */
View
@@ -48,7 +48,7 @@ struct s_radixtree_fragments4 {
radixtree_t *nat6_tcp, *nat6_udp, *nat6_icmp,
*nat4_tcp, *nat4_udp, *nat4_icmp,
- *nat4_tcp_fragments;
+ *nat4_tcp_fragments, *nat4_saved_fragments;
/**
* Initialization of NAT tables.
@@ -64,6 +64,7 @@ void nat_init(void)
nat4_icmp = radixtree_create();
nat4_tcp_fragments = radixtree_create();
+ nat4_saved_fragments = radixtree_create();
}
/**
@@ -83,6 +84,7 @@ void nat_quit(void)
/* 32 + 16 = 48 / 6 = 8 */
radixtree_destroy(nat4_tcp_fragments, 8);
+ radixtree_destroy(nat4_saved_fragments, 8);
}
/**
@@ -191,42 +193,41 @@ struct s_nat *nat_in(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src,
}
/**
- * Save and retrieve NATted connections via fragment identification.
+ * Save and retrieve data (e.g. NATted connections) via fragment identification.
*
* @param nat_proto4 Radix tree of fragments
* @param ipv4_src Source IPv4 address
* @param id Fragment identification
- * @param nat Connection to save
+ * @param data Data to save
*
- * @return Connection
+ * @return Data (or NULL when nothing found)
*/
-struct s_nat *nat_in_fragments(radixtree_t *nat_proto4,
- struct s_ipv4_addr ipv4_src,
- unsigned short id, struct s_nat *nat)
+void *nat_in_fragments(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src,
+ unsigned short id, void *data)
{
- struct s_nat *result;
+ void *result;
/* create structure to search in the tree */
struct s_radixtree_fragments4 radixsearch4;
radixsearch4.addr = ipv4_src;
radixsearch4.id = id;
- if ((result = (struct s_nat *) radixtree_lookup(nat_proto4,
- radixtree_chunker, &radixsearch4, sizeof(radixsearch4))) == NULL) {
- if (nat != NULL) {
+ if ((result = radixtree_lookup(nat_proto4, radixtree_chunker,
+ &radixsearch4, sizeof(radixsearch4))) == NULL) {
+ if (data != NULL) {
/* when fragmentation is not found, add one */
radixtree_insert(nat_proto4, radixtree_chunker,
&radixsearch4, sizeof(radixsearch4),
- nat);
- return nat;
+ data);
+ return data;
} else {
/* nothing found and nothing to be added */
return NULL;
}
} else {
- if (nat != NULL) {
+ if (data != NULL) {
/* when fragmentation is found, is it the same? */
- if (result == nat) {
+ if (result == data) {
/* OK, return */
return result;
} else {
@@ -238,12 +239,10 @@ struct s_nat *nat_in_fragments(radixtree_t *nat_proto4,
sizeof(radixsearch4));
radixtree_insert(nat_proto4, radixtree_chunker,
&radixsearch4,
- sizeof(radixsearch4), nat);
- return nat;
+ sizeof(radixsearch4), data);
+ return data;
}
} else {
- /* refresh it's connection and return */
- result->last_packet = time(NULL);
return result;
}
}
View
@@ -39,7 +39,7 @@ struct s_nat {
extern radixtree_t *nat6_tcp, *nat6_udp, *nat6_icmp,
*nat4_tcp, *nat4_udp, *nat4_icmp,
- *nat4_tcp_fragments;
+ *nat4_tcp_fragments, *nat4_saved_fragments;
void nat_init(void);
void nat_quit(void);
@@ -50,9 +50,8 @@ struct s_nat *nat_out(radixtree_t *nat_proto6, radixtree_t *nat_proto4,
unsigned short port_src, unsigned short port_dst);
struct s_nat *nat_in(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src,
unsigned short port_src, unsigned short port_dst);
-struct s_nat *nat_in_fragments(radixtree_t *nat_proto4,
- struct s_ipv4_addr ipv4_src,
- unsigned short id, struct s_nat *nat);
+void *nat_in_fragments(radixtree_t *nat_proto4, struct s_ipv4_addr ipv4_src,
+ unsigned short id, void *data);
void nat_in_fragments_cleanup(radixtree_t *nat_proto4,
struct s_ipv4_addr ipv4_src, unsigned short id);
Oops, something went wrong.

0 comments on commit 24b09b4

Please sign in to comment.