Skip to content

Commit

Permalink
Use C99 sized integer types to store ip/port
Browse files Browse the repository at this point in the history
  • Loading branch information
ecsv committed Feb 12, 2014
1 parent e59f82b commit 9c36559
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
8 changes: 4 additions & 4 deletions flow_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ void reset_pdf(struct pkt_dump_file *f)
memset(f->file_name, '\0', FILE_NAME_LENGTH);
}

struct ip_pair *find_ip_pair(unsigned int src_ip, unsigned int dst_ip,
unsigned short src_tcp, unsigned short dst_tcp)
struct ip_pair *find_ip_pair(uint32_t src_ip, uint32_t dst_ip,
uint16_t src_tcp, uint16_t dst_tcp)
{
struct ip_pair *p;
unsigned int hash = 0;
Expand Down Expand Up @@ -106,8 +106,8 @@ struct ip_pair *find_ip_pair(unsigned int src_ip, unsigned int dst_ip,
return NULL;
}

struct ip_pair *register_ip_pair(unsigned int src_ip, unsigned int dst_ip,
unsigned short src_tcp, unsigned short dst_tcp)
struct ip_pair *register_ip_pair(uint32_t src_ip, uint32_t dst_ip,
uint16_t src_tcp, uint16_t dst_tcp)
{
struct ip_pair *newp;
unsigned int hash = 0;
Expand Down
4 changes: 2 additions & 2 deletions pkt2flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ static void process_trace(void)
u_char *pkt = NULL;
char *fname = NULL;
unsigned short offset;
unsigned long src_ip, dst_ip;
unsigned short src_port, dst_port;
uint32_t src_ip, dst_ip;
uint16_t src_port, dst_port;

while ((pkt = (u_char *)pcap_next(inputp, &hdr)) != NULL) {
// Get IP layer information
Expand Down
18 changes: 10 additions & 8 deletions pkt2flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* SOFTWARE.
*/

#include <stdint.h>

#define __SOURCE_VERSION__ "1.2"
#define __AUTHOR__ "X. Chen ([email protected])"
#define __GLOBAL_NAME__ "pkt2flow"
Expand Down Expand Up @@ -68,8 +70,8 @@ struct pkt_dump_file {
};

struct ip_pair {
unsigned int ip1, ip2;
unsigned short port1, port2;
uint32_t ip1, ip2;
uint16_t port1, port2;
struct pkt_dump_file pdf;
struct ip_pair *next;
};
Expand All @@ -82,8 +84,8 @@ extern struct ip_pair *pairs[];
/*
* Generate a new file name for flow with 4-tuple and timestamp
*/
char *new_file_name(unsigned int src_ip, unsigned int dst_ip,
unsigned short src_tcp, unsigned short dst_tcp,
char *new_file_name(uint32_t src_ip, uint32_t dst_ip,
uint16_t src_tcp, uint16_t dst_tcp,
unsigned long timestamp);

/* flow_db.c */
Expand All @@ -99,17 +101,17 @@ void init_hash_table(void);
* returned.
* Otherwise, NULL returned;
*/
struct ip_pair *find_ip_pair(unsigned int src_ip, unsigned int dst_ip,
unsigned short src_tcp, unsigned short dst_tcp);
struct ip_pair *find_ip_pair(uint32_t src_ip, uint32_t dst_ip,
uint16_t src_tcp, uint16_t dst_tcp);

/*
* To register a new flow item in the flow hash table. This is uaually called
* after finding the flow item with NULL returned.
* The pointer to the new registerd ip_pair will be returned; and the pdf will
* be reset as empty.
*/
struct ip_pair *register_ip_pair(unsigned int src_ip, unsigned int dst_ip,
unsigned short src_tcp, unsigned short dst_tcp);
struct ip_pair *register_ip_pair(uint32_t src_ip, uint32_t dst_ip,
uint16_t src_tcp, uint16_t dst_tcp);

/*
* Reset the packet dump file (pdf) for: 1) a new ip_pair created;
Expand Down
12 changes: 7 additions & 5 deletions utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@
*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "pkt2flow.h"

#define IP_LENGTH sizeof("aaa.bbb.ccc.ddd")
#define FILE_NAME_LEGNTH 64
static const char flow_record[] = "flow_names.txt";

static char *ip_ntos(unsigned int n)
static char *ip_ntos(uint32_t n)
{
char *buf = (char *)malloc(IP_LENGTH);
memset(buf, '\0', IP_LENGTH);
Expand All @@ -51,16 +53,16 @@ static char *ip_ntos(unsigned int n)
return buf;
}

char *new_file_name(unsigned int src_ip, unsigned int dst_ip,
unsigned short src_tcp, unsigned short dst_tcp,
char *new_file_name(uint32_t src_ip, uint32_t dst_ip,
uint16_t src_tcp, uint16_t dst_tcp,
unsigned long timestamp)
{
char *src_ip_str = ip_ntos(src_ip);
char *dst_ip_str = ip_ntos(dst_ip);
char *fname = (char *)malloc(FILE_NAME_LEGNTH);
memset(fname, '\0', FILE_NAME_LEGNTH);
sprintf(fname, "%s_%d_%s_%u_%lu.pcap", src_ip_str, src_tcp, dst_ip_str,
dst_tcp, timestamp);
sprintf(fname, "%s_%"PRIu16"_%s_%"PRIu16"_%lu.pcap",
src_ip_str, src_tcp, dst_ip_str, dst_tcp, timestamp);
//fprintf(stderr, "%s\n", buf);
free(src_ip_str);
free(dst_ip_str);
Expand Down

0 comments on commit 9c36559

Please sign in to comment.