-
Notifications
You must be signed in to change notification settings - Fork 35
5. IPv4 API Description
Structure to identify an IPv4 flow:
/* @brief Struct to generate the flow key **/
typedef struct
{
/// source IP address
unsigned int source;
/// destination IP address
unsigned int destination;
/// Transport layer protocol
unsigned char protocol;
/// Identification
unsigned short id;
} ntoh_ipv4_tuple4_t, *pntoh_ipv4_tuple4_t;
Structure to store an IPv4 fragment:
/** @brief Struct to store the information of each fragment */
typedef struct _ipv4_fragment_
{
/// pointer to the next fragment
struct _ipv4_fragment_ *next;
/// fragment offset
unsigned int offset;
/// fragment data length
unsigned int len;
/// fragment data
unsigned char *data;
} ntoh_ipv4_fragment_t , *pntoh_ipv4_fragment_t;
Structure to store all information of an IPv4 flow:
/** @brief Struct to store the information of each IPv4 flow */
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
size_t meat;
/// total amount of expected data
size_t total;
/// final fragment received?
struct ip *final_iphdr;
/// user defined function to receive defragmented packets
void *function;
/// last activity
struct timeval last_activ;
/// user-defined data
void *udata;
ntoh_lock_t lock;
} ntoh_ipv4_flow_t, *pntoh_ipv4_flow_t;
Structure to store all information needed for an IPv4 session:
/** @brief Structure to store global parameters */
typedef struct _ipv4_session_
{
struct _ipv4_session_ *next;
/// max. number of IP flows
sem_t max_flows;
sem_t max_fragments;
/// hash table to store IP flows
pipv4_flows_table_t flows;
/// connection tables related
pthread_t tID;
ntoh_lock_t lock;
}ntoh_ipv4_session_t , *pntoh_ipv4_session_t ;
Function to initialize IPv4 defragmentation
void ntoh_ipv4_init ( void );
Function to flush all IPv4 sessions and release all resources
void ntoh_ipv4_exit ( void );
Function to create a new session:
pntoh_ipv4_session_t ntoh_ipv4_new_session ( unsigned int max_flows , unsigned long max_mem , unsigned int *error );
- max_flows: Max. number of allowed flows in this session
- max_mem: Max. amount of memory to be used to store fragments
- *error: Returned error code
This function returns a pointer to the new session, or NULL on error.
Function to resize an existing session:
int ntoh_ipv4_resize_session ( pntoh_ipv4_session_t session , size_t size );
- session: Pointer to the IPv4 session to be resized
- size: New size
This function returns NTOH_OK on success and the error code when it fails.
Function to free a session:
void ntoh_ipv4_free_session ( pntoh_ipv4_session_t session );
- session: Session pointer to be released
Function to create a new flow:
pntoh_ipv4_flow_t ntoh_ipv4_new_flow ( pntoh_ipv4_session_t session , pntoh_ipv4_tuple4_t tuple4 , pipv4_dfcallback_t function , void *udata , unsigned int *error);
- session: IPv4 session
- tuple4: Tuple4 identifying the flow
- function: User defined callback function to receive the defragmented datagrams of this flow
- udata: User data linked to this flow
- *error: Returned error code
This function returns a pointer to the new created IPv4 flow, or NULL on error.
Function to find an IPv4 flow:
pntoh_ipv4_flow_t ntoh_ipv4_find_flow ( pntoh_ipv4_session_t session , pntoh_ipv4_tuple4_t tuple4 );
- session: IPv4 session
- tuple4: Tuple4 structure identifying the flow
This function returns a pointer to the flow if it was found, or NULL if it was not found.
Function to free an IPv4 flow:
void ntoh_ipv4_free_flow ( pntoh_ipv4_session_t session , pntoh_ipv4_flow_t flow , unsigned short reason );
- session: IPv4 session
- flow: Flow to be free'd
- reason: Value to be sent to the user-defined callback function
Function to add a fragment to a given IPv4 flow:
int ntoh_ipv4_add_fragment ( pntoh_ipv4_session_t session , pntoh_ipv4_flow_t flow , struct ip *iphdr );
- session: IPv4 session
- flow: IPv4 flow where the new fragment will be added
- iphdr: IPv4 header
This function returns NTOH_OK on success and the error code on failure.
Function to get the number of stored IPv4 flows in a session:
unsigned int ntoh_ipv4_count_flows ( pntoh_ipv4_session_t session );
- session: IPv4 session
This function returns the number of IPv4 flows stored in the given session.
Function to get the size of the IPv4 stream table (max allowed flows):
unsigned int *ntoh_ipv4_get_size ( pntoh_ipv4_session_t session );
- session: IPv4 session
This function returns the size of the IPv4 sessions table.
Function to get the tuple4:
unsigned int ntoh_ipv4_get_tuple4 ( struct ip *ip , pntoh_ipv4_tuple4_t tuple );
- ip: IPv4 Header
- tuple: Pointer to the output tuple4 struct
This function returns NTOH_OK on success and the error code on failure.
Macro to check if an IPv4 datagram is part of a fragmented datagram:
#define NTOH_IPV4_IS_FRAGMENT(off) ( ( (8*(ntohs(off) & 0x1FFF)) > 0 || (ntohs(off) & 0x2000) ) && !(ntohs(off) & 0x4000) )
- off: IPv4 header field (iphdr->ip_off)
Typedef defining the user defined callback function:
typedef void(*pipv4_dfcallback_t) ( pntoh_ipv4_flow_t flow , pntoh_ipv4_tuple4_t tuple4, unsigned char* data, size_t len, unsigned short reason);
- flow: IPv4 flow where the defragmented datagrams of the sent datagram where stored
- tuple4: Tuple4 identifying the flow
- data: Defragmented datagram
- len: Total length of the defragmented datagram (IPv4 header + payload)
- reason: Why the datagram is sent?
Unlike TCP reassembly, IPv4 defragmentation only store the information of the sender.
Possible values for "reason" parameter:
- NTOH_REASON_DEFRAGMENTED_DATAGRAM
- NTOH_REASON_TIMEDOUT_FRAGMENTS