Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b848d5b
CMakeLists.txt: always build static library as libntoh_static.a
megahall Mar 22, 2016
e9ec639
common.{c,h}: remove non-working htable implementation
megahall Mar 22, 2016
eec12c2
ipv4defrag.h: switch from htable to uthash
megahall Mar 22, 2016
8ad64eb
ipv6defrag.h: switch from htable to uthash
megahall Mar 22, 2016
275aec2
libntoh.{c,h}: fix prototype of ntoh_get_reason
megahall Mar 22, 2016
bebfa42
tcpreassembly.h: switch from htable to uthash, fix broken indents
megahall Mar 22, 2016
e8111a0
ipv4defrag.c: stub out ntoh_ipv4_get_size
megahall Mar 22, 2016
c4bb3d7
ipv4defrag.c: switch ntoh_ipv4_find_flow to uthash
megahall Mar 22, 2016
eefdeb9
ipv4defrag.c: switch ntoh_ipv4_new_flow to uthash
megahall Mar 22, 2016
240e5f2
ipv4defrag.c: switch __ipv4_free_flow to uthash
megahall Mar 22, 2016
38b95cf
ipv4defrag.c: fix indent in ntoh_ipv4_add_fragment
megahall Mar 22, 2016
7e3d494
ipv4defrag.c: switch ntoh_ipv4_count_flows to uthash
megahall Mar 22, 2016
7f3e20f
ipv4defrag.c: switch ip_check_timeouts to uthash
megahall Mar 22, 2016
d413a0c
ipv4defrag.c: switch ntoh_ipv4_new_session to uthash
megahall Mar 22, 2016
5e4b4af
ipv4defrag.c: switch __ipv4_free_session to uthash
megahall Mar 22, 2016
3f18156
ipv4defrag.c: fix indent in ntoh_ipv4_free_session
megahall Mar 22, 2016
f31d81b
ipv4defrag.c: stub out ntoh_ipv4_resize_session
megahall Mar 22, 2016
7eb7d03
ipv4defrag.c: eliminate unused ip_get_hashkey
megahall Mar 22, 2016
b0f1dd1
ipv6defrag.c: eliminate unused ip_get_hashkey
megahall Mar 22, 2016
8cd07a4
ipv6defrag.c: stub out ntoh_ipv6_get_size
megahall Mar 22, 2016
65650e4
ipv4defrag.c: eliminate unused ipv4_equal_tuple
megahall Mar 22, 2016
44cec6a
ipv6defrag.c: eliminate unused ipv6_equal_tuple
megahall Mar 22, 2016
8eca34b
ipv6defrag.c: switch ntoh_ipv6_find_flow to uthash
megahall Mar 22, 2016
289d622
ipv6defrag.c: switch ntoh_ipv6_new_flow to uthash
megahall Mar 22, 2016
b74dc3c
ipv6defrag.c: switch __ipv6_free_flow to uthash
megahall Mar 22, 2016
70cb52c
ipv6defrag.c: fix indent in ntoh_ipv6_add_fragment
megahall Mar 22, 2016
050ff84
ipv6defrag.c: switch ntoh_ipv6_count_flows to uthash
megahall Mar 22, 2016
2704388
ipv6defrag.c: switch ip_check_timeouts to uthash
megahall Mar 22, 2016
a832805
ipv6defrag.c: switch ntoh_ipv6_new_session to uthash
megahall Mar 22, 2016
c01ec1a
ipv6defrag.c: switch __ipv6_free_session to uthash
megahall Mar 22, 2016
270e320
ipv6defrag.c: stub out ntoh_ipv6_resize_session
megahall Mar 22, 2016
6b42660
sfhash.c: fix collision with a macro in uthash
megahall Mar 22, 2016
0d72a1d
tcpreassembly.c: switch delete_stream to uthash
megahall Mar 22, 2016
9c46736
tcpreassembly.c: switch __tcp_free_session to uthash
megahall Mar 22, 2016
432ae9e
tcpreassembly.c: switch tcp_check_timeouts to uthash
megahall Mar 22, 2016
85629d4
tcpreassembly.c: eliminate unused tcp_equal_tuple
megahall Mar 22, 2016
2b943cd
tcpreassembly.c: stub out ntoh_tcp_get_size
megahall Mar 22, 2016
9adb22a
tcpreassembly.c: switch ntoh_tcp_new_session to uthash
megahall Mar 22, 2016
d76ef8f
tcpreassembly.c: switch ntoh_tcp_find_stream to uthash
megahall Mar 22, 2016
1937496
tcpreassembly.c: switch ntoh_tcp_new_stream to uthash
megahall Mar 22, 2016
74f83fd
tcpreassembly.c: switch ntoh_tcp_new_stream to uthash
megahall Mar 22, 2016
51185ac
tcpreassembly.c: switch handle_closing_connection to uthash
megahall Mar 22, 2016
a76628c
tcpreassembly.c: stub out ntoh_tcp_resize_session
megahall Mar 22, 2016
9df17c4
tcpreassembly.c: PORTS_MISMATCH should release stream->lock
megahall Mar 22, 2016
1e7a752
examples: get_proto_description is outside header file; no inline
megahall Mar 22, 2016
f98da38
examples: flow->key eliminated by uthash; do not try to print it
megahall Mar 22, 2016
752829a
setup.bash: add libntoh local compilation script
megahall Apr 6, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/c/ipv4/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void shandler ( int sign )
/**
* @brief Returns the name of a protocol
*/
inline char *get_proto_description ( unsigned short proto )
char *get_proto_description ( unsigned short proto )
{
switch ( proto )
{
Expand Down Expand Up @@ -135,7 +135,7 @@ void ipv4_callback ( pntoh_ipv4_flow_t flow , pntoh_ipv4_tuple4_t tuple , unsign
unsigned int i = 0;

fprintf( stderr, "\n\n[i] Got an IPv4 datagram! (%s) %s --> ", ntoh_get_reason(reason) , inet_ntoa( *(struct in_addr*) &tuple->source ) );
fprintf( stderr, "%s | %zu/%zu bytes - Key: %04x - ID: %02x - Proto: %d (%s)\n\n", inet_ntoa( *(struct in_addr*) &tuple->destination ), len, flow->total , flow->key, ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );
fprintf( stderr, "%s | %zu/%zu bytes - ID: %02x - Proto: %d (%s)\n\n", inet_ntoa( *(struct in_addr*) &tuple->destination ), len, flow->total , ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );

for ( i = 0; i < flow->total ; i++ )
fprintf( stderr, "%02x ", data[i] );
Expand Down
4 changes: 2 additions & 2 deletions examples/c/ipv6/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void shandler ( int sign )
/**
* @brief Returns the name of a protocol
*/
inline char *get_proto_description ( unsigned short proto )
char *get_proto_description ( unsigned short proto )
{
switch ( proto )
{
Expand Down Expand Up @@ -146,7 +146,7 @@ void ipv6_callback ( pntoh_ipv6_flow_t flow , pntoh_ipv6_tuple4_t tuple , unsign
inet_ntop ( AF_INET6 , (void*) &tuple->destination , src , INET6_ADDRSTRLEN );

fprintf( stderr, "\n\n[i] Got an IPv6 datagram! (%s - %d) %s --> ", ntoh_get_reason(reason) , reason , src );
fprintf( stderr, "%s | %zu/%zu bytes - Key: %04x - ID: %02x - Proto: %d (%s)\n\n", dst , len, flow->total , flow->key, ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );
fprintf( stderr, "%s | %zu/%zu bytes - ID: %02x - Proto: %d (%s)\n\n", dst , len, flow->total , ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );

for ( i = 0; i < flow->total ; i++ )
fprintf( stderr, "%02x ", data[i] );
Expand Down
2 changes: 1 addition & 1 deletion examples/c/tcp_ipv4/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void ipv4_callback ( pntoh_ipv4_flow_t flow , pntoh_ipv4_tuple4_t tuple , unsign
unsigned int i = 0;

fprintf( stderr, "\n\n[i] Got an IPv4 datagram! (%s) %s --> ", ntoh_get_reason(reason) , inet_ntoa( *(struct in_addr*) &tuple->source ) );
fprintf( stderr, "%s | %zu/%zu bytes - Key: %04x - ID: %02x - Proto: %d (%s)\n\n", inet_ntoa( *(struct in_addr*) &tuple->destination ), len, flow->total , flow->key, ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );
fprintf( stderr, "%s | %zu/%zu bytes - ID: %02x - Proto: %d (%s)\n\n", inet_ntoa( *(struct in_addr*) &tuple->destination ), len, flow->total , ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );

if ( tuple->protocol == IPPROTO_TCP )
send_tcp_segment ( (struct ip*) data , &tcp_callback );
Expand Down
4 changes: 2 additions & 2 deletions examples/c/tcp_ipv6/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void free_peer_info ( ppeer_info_t pinfo )
/**
* @brief Returns the name of a protocol
*/
inline char *get_proto_description ( unsigned short proto )
char *get_proto_description ( unsigned short proto )
{
switch ( proto )
{
Expand Down Expand Up @@ -360,7 +360,7 @@ void ipv6_callback ( pntoh_ipv6_flow_t flow , pntoh_ipv6_tuple4_t tuple , unsign
inet_ntop ( AF_INET6 , (void*) &tuple->destination , dst , INET6_ADDRSTRLEN );

fprintf( stderr, "\n\n[i] Got an IPv4 datagram! (%s - %d) %s --> ", ntoh_get_reason(reason) , reason , src );
fprintf( stderr, "%s | %zu/%zu bytes - Key: %04x - ID: %02x - Proto: %d (%s)\n\n", dst , len, flow->total , flow->key, ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );
fprintf( stderr, "%s | %zu/%zu bytes - ID: %02x - Proto: %d (%s)\n\n", dst , len, flow->total , ntohs( tuple->id ), tuple->protocol, get_proto_description( tuple->protocol ) );

if ( tuple->protocol == IPPROTO_TCP )
send_tcp_segment ( (struct ip6_hdr*) data , &tcp_callback );
Expand Down
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ INCLUDE_DIRECTORIES ( ${CMAKE_INCLUDE_PATH})

# set target from source
ADD_LIBRARY( ${OUTPUT_LIB} SHARED ${LIBNTOH_SRCS} )
#ADD_LIBRARY( ${OUTPUT_LIB} STATIC ${LIBNTOH_SRCS} ) // static: comment the line above and uncomment this one to compile as static library (contrib by Di3)
ADD_LIBRARY( ${OUTPUT_LIB}_static STATIC ${LIBNTOH_SRCS} )
SET_PROPERTY(TARGET ${OUTPUT_LIB} PROPERTY POSITION_INDEPENDENT_CODE 1)
SET_PROPERTY(TARGET ${OUTPUT_LIB}_static PROPERTY POSITION_INDEPENDENT_CODE 1)

# pkgconfig file
CONFIGURE_FILE ( ntoh.pc.in ntoh.pc @ONLY )
Expand All @@ -48,7 +50,7 @@ TARGET_LINK_LIBRARIES( ${OUTPUT_LIB} ${CMAKE_THREAD_LIBS_INIT})
###########################
# library
INSTALL ( TARGETS ${OUTPUT_LIB} LIBRARY DESTINATION lib )
#INSTALL ( TARGETS ${OUTPUT_LIB} ARCHIVE DESTINATION lib )// static: comment the line above and uncomment this one to compile as static library (contrib by Di3)
INSTALL ( TARGETS ${OUTPUT_LIB}_static ARCHIVE DESTINATION lib )
# headers
INSTALL ( FILES ${LIBNTOH_INC}/libntoh.h ${LIBNTOH_INC}/tcpreassembly.h ${LIBNTOH_INC}/sfhash.h ${LIBNTOH_INC}/ipv4defrag.h ${LIBNTOH_INC}/ipv6defrag.h ${LIBNTOH_INC}/common.h DESTINATION include/libntoh )
# pkconfig file
Expand Down
202 changes: 0 additions & 202 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,208 +30,6 @@

#include <stdlib.h>
#include <libntoh.h>
#include <ipv4defrag.h>

// Uniqueness test for IP fragments, using their tuples
// @contrib: Eosis - https://github.com/Eosis
/*_HIDDEN inline int ipv4_tuple4_equals_to(pntoh_ipv4_tuple4_t x, pntoh_ipv4_tuple4_t y)
{
if (x->source != y->source)
return 0;

if (x->destination != y->destination)
return 0;

if (x->protocol != y->protocol)
return 0;

if (x->id != y->id)
return 0;

return 1;
}*/

/****************/
/** HASH TABLE **/
/****************/
/* map the hash table */
_HIDDEN phtable_t htable_map ( size_t size , fcmp_t *equal_func )
{
phtable_t ret = 0;

if ( !size )
return 0;

ret = (phtable_t) calloc ( 1 , sizeof ( htable_t ) );
ret->table = (phtnode_t*) calloc ( size , sizeof ( phtnode_t ) );
ret->table_size = size;
ret->equals = equal_func;

return ret;
}

/* insert a pair key-value into the hash table */
_HIDDEN int htable_insert ( phtable_t ht , unsigned int key , void *val )
{
phtnode_t node = 0;
phtnode_t aux = 0;
unsigned int index = 0;

if ( !ht || !val )
return 0;

node = (phtnode_t) calloc ( 1 , sizeof ( htnode_t ) );
node->key = key;
node->val = val;

index = key % ht->table_size;

if ( ht->table[index] == NULL )
{
ht->table[index] = node;
return 1;
}

/* collision resolution by chaining */
aux = ht->table[index];
while ( aux->next != 0 )
aux = aux->next;

aux->next = node;

return 1;
}

/* returns the value associated to the given key */
_HIDDEN void *htable_find ( phtable_t ht , unsigned int key, void* ip_tuple4 )
{
unsigned int index = 0;
phtnode_t node = 0;

if ( !ht )
return 0;

index = key % ht->table_size;

node = ht->table[index];

// @contrib: Eosis - https://github.com/Eosis
if ( ip_tuple4 != 0 ) //if not null
while( node != 0 && ! ht->equals ( ip_tuple4 , node->val ) ) //!(ipv4_tuple4_equals_to((pntoh_ipv4_tuple4_t)ip_tuple4, &(((pntoh_ipv4_flow_t)(node->val))->ident))) )
node = node->next;
else
while ( node != 0 && node->key != key )
node = node->next;

if ( !node )
return 0;

return node->val;
}

/* removes a key-value pair from the hash table */
_HIDDEN void *htable_remove ( phtable_t ht , unsigned int key, void* ip_tuple4 )
{
unsigned int index = 0;
phtnode_t node = 0;
phtnode_t aux = 0;
void *ret = 0;

if ( !ht )
return 0;

index = key % ht->table_size;
node = ht->table[index];

if ( node->key == key )
ht->table[index] = node->next;
else
{
while ( node->next != 0 && node->next->key != key )
node = node->next;

// @contrib: Eosis - https://github.com/Eosis
if (ip_tuple4 != 0 ) //if not null
while( node->next != 0 && ! ht->equals ( ip_tuple4 , node->next->val ) ) //!(ipv4_tuple4_equals_to((pntoh_ipv4_tuple4_t)ip_tuple4, &(((pntoh_ipv4_flow_t)(node->next->val))->ident))) )
node = node->next;
else
while ( node->next != 0 && node->next->key != key )
node = node->next;

if ( node->next != 0 )
{
aux = node;
node = node->next;
aux->next = node->next;
}
}

if ( !node )
return 0;

ret = node->val;
free ( node );

return ret;
}

/* count the key-value pairs in a hash table */
_HIDDEN unsigned int htable_count ( phtable_t ht )
{
unsigned int i = 0;
unsigned int ret = 0;
phtnode_t aux = 0;

if ( !ht )
return ret;

for ( i = 0 ; i < ht->table_size ; i++ )
for ( aux = ht->table[i] ; aux != 0 ; ret++ , aux = aux->next );

return ret;
}

/* gets the first key in a hash table */
_HIDDEN unsigned int htable_first ( phtable_t ht )
{
unsigned int ret = 0;
unsigned int i = 0;

if ( ! ht )
return ret;

for ( i = 0 ; i < ht->table_size && ht->table[i] == 0 ; i++ );

if ( i < ht->table_size )
ret = ht->table[i]->key;

return ret;
}

/* destroys entire hash table */
_HIDDEN void htable_destroy ( phtable_t *ht )
{
unsigned int i = 0;
phtnode_t aux = 0;

if ( !ht || !(*ht) )
return;

for ( i = 0 ; i < (*ht)->table_size ; i++ )
while ( (*ht)->table[i] != 0 )
{
aux = (*ht)->table[i]->next;
free ( (*ht)->table[i] );
(*ht)->table[i] = aux;
}

free ( (*ht)->table );
free ( *ht );

*ht = 0;

return;
}

/********************/
/** ACCESS LOCKING **/
Expand Down
30 changes: 0 additions & 30 deletions src/inc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,6 @@
# define _HIDDEN __attribute__((visibility("hidden")))
#endif

/* linked list */
typedef struct _hash_node_
{
struct _hash_node_ *next;
void *val;
unsigned int key;
} htnode_t , *phtnode_t;

typedef unsigned short fcmp_t (void *a, void *b);

/* hash table definition */
typedef struct
{
size_t table_size;
phtnode_t *table;
fcmp_t *equals;
} htable_t , *phtable_t;

/******************************************************************/
/** Hash Table implementation (collision resolution by chaining) **/
/******************************************************************/
phtable_t htable_map ( size_t size , fcmp_t *equal_func );
int htable_insert ( phtable_t ht , unsigned int key , void *val );
void *htable_find ( phtable_t ht , unsigned int key, void *ip_tuple4 );
void *htable_remove ( phtable_t ht , unsigned int key, void *ip_tuple4 );
unsigned int htable_count ( phtable_t ht );
unsigned int htable_first ( phtable_t ht );
void htable_destroy ( phtable_t *ht );


/** @brief Access locking **/
void lock_access ( pntoh_lock_t lock );
/** @brief Access unlocking **/
Expand Down
12 changes: 5 additions & 7 deletions src/inc/ipv4defrag.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <netinet/in.h>
#include <netinet/ip.h>

#include <uthash.h>

/// macro to verify if an IP datagram is part of a fragmented datagram
#define NTOH_IPV4_IS_FRAGMENT(off) ( ( (8*(ntohs(off) & 0x1FFF)) > 0 || (ntohs(off) & 0x2000) ) && !(ntohs(off) & 0x4000) )

Expand Down Expand Up @@ -71,8 +73,6 @@ typedef struct
{
/// flow identification data
ntoh_ipv4_tuple4_t ident;
/// flow key
ntoh_ipv4_key_t key;
/// fragments list
pntoh_ipv4_fragment_t fragments;
/// total amount of received data
Expand All @@ -88,11 +88,9 @@ typedef struct
/// user-defined data
void *udata;
ntoh_lock_t lock;
UT_hash_handle hh;
} ntoh_ipv4_flow_t, *pntoh_ipv4_flow_t;

typedef htable_t ipv4_flows_table_t;
typedef phtable_t pipv4_flows_table_t;

/** @brief Structure to store global parameters */
typedef struct _ipv4_session_
{
Expand All @@ -102,11 +100,11 @@ typedef struct _ipv4_session_
sem_t max_flows;
sem_t max_fragments;
/// hash table to store IP flows
pipv4_flows_table_t flows;
pntoh_ipv4_flow_t flows;
/// connection tables related
pthread_t tID;
ntoh_lock_t lock;
}ntoh_ipv4_session_t , *pntoh_ipv4_session_t ;
}ntoh_ipv4_session_t , *pntoh_ipv4_session_t;

typedef struct
{
Expand Down
Loading