Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
99 changes: 61 additions & 38 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,34 @@ if(NOT PCAP_FOUND)
set(PCAP_INCLUDE_DIRS "")
endif()

# Find glog
find_package(glog REQUIRED)

# Source files
# Source files for the original executable
set(PKT2FLOW_SOURCES
src/pkt2flow.cpp
src/flow_db.cpp
src/utilities.cpp
src/pkt2flow.c
src/flow_db.c
src/utilities.c
src/main.c
)

# Create the main executable
# Create the original executable
add_executable(pkt2flow ${PKT2FLOW_SOURCES})

# Link libraries
target_link_libraries(pkt2flow
# Link libraries for both executables
target_link_libraries(pkt2flow
${PCAP_LIBRARIES}
glog::glog
)

# Include directories
target_include_directories(pkt2flow PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
# Include directories for both executables
target_include_directories(pkt2flow PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${PCAP_INCLUDE_DIRS}
)

# Set compile definitions
# Set compile definitions for both executables
target_compile_definitions(pkt2flow PRIVATE _GNU_SOURCE)

# Installation
include(GNUInstallDirs)
install(TARGETS pkt2flow
install(TARGETS pkt2flow
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

Expand All @@ -87,68 +84,94 @@ option(BUILD_TESTS "Build unit tests" ON)
if(BUILD_TESTS)
# Find GoogleTest
find_package(GTest REQUIRED)

find_package(Glog REQUIRED)

# Test source files
set(TEST_SOURCES
tests/test_flow_db.cpp
tests/test_utilities.cpp
tests/test_pkt2flow_base.cpp
tests/test_pkt2flow_integration.cpp
tests/test_pkt2flow_tcp_integration.cpp
tests/test_pkt2flow_udp_integration.cpp
tests/test_pkt2flow_udplite_integration.cpp
tests/test_main.cpp
)

# Create test executable
add_executable(pkt2flow_tests ${TEST_SOURCES})

# Link test libraries
target_link_libraries(pkt2flow_tests
${PCAP_LIBRARIES}
glog::glog
GTest::gtest
GTest::gtest_main
)

# Include directories for tests
target_include_directories(pkt2flow_tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
target_include_directories(pkt2flow_tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${PCAP_INCLUDE_DIRS}
)

# Set compile definitions for tests
target_compile_definitions(pkt2flow_tests PRIVATE _GNU_SOURCE)

# Add test
add_test(NAME pkt2flow_unit_tests COMMAND pkt2flow_tests)

# Create a library from the source files for testing
add_library(pkt2flow_lib STATIC
src/flow_db.cpp
src/utilities.cpp
src/flow_db.c
src/utilities.c
)
target_include_directories(pkt2flow_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src

target_include_directories(pkt2flow_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${PCAP_INCLUDE_DIRS}
)
target_link_libraries(pkt2flow_lib

target_link_libraries(pkt2flow_lib
${PCAP_LIBRARIES}
glog::glog
)

target_compile_definitions(pkt2flow_lib PRIVATE _GNU_SOURCE)

# Link the library to tests
target_link_libraries(pkt2flow_tests pkt2flow_lib)

# Create a library that includes the main pkt2flow.c for testing (excluding main)
add_library(pkt2flow_test_lib STATIC
src/pkt2flow.c
)

# Exclude main function from the test library
target_compile_definitions(pkt2flow_test_lib PRIVATE _GNU_SOURCE TESTING_MODE)

target_include_directories(pkt2flow_test_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${PCAP_INCLUDE_DIRS}
)

target_link_libraries(pkt2flow_test_lib
${PCAP_LIBRARIES}
glog::glog
)

target_compile_definitions(pkt2flow_test_lib PRIVATE _GNU_SOURCE)

# Link both libraries to tests
target_link_libraries(pkt2flow_tests pkt2flow_lib pkt2flow_test_lib)
endif()

# Build example in examples/
add_executable(example_pkt2flow examples/example_pkt2flow.c)
target_link_libraries(example_pkt2flow pkt2flow_lib)
target_include_directories(example_pkt2flow PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_include_directories(example_pkt2flow PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)

# CPack configuration for packaging
include(CPack)
set(CPACK_PACKAGE_NAME "pkt2flow")
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_DESCRIPTION}")
set(CPACK_PACKAGE_VENDOR "chenxm")
set(CPACK_PACKAGE_CONTACT "chenxm35@gmail.com")
set(CPACK_PACKAGE_CONTACT "chenxm35@gmail.com")
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,6 @@ Options:
```
This will split all TCP and UDP flows from `input.pcap` into separate files in the `output_flows/` directory.

## Logging

Google glog is used for structured logging. Logs are written to:
- Console (stderr) for important messages
- Log files in `./logs/` for detailed debugging

Control verbosity with environment variables:
```bash
export GLOG_v=2 # Increase verbosity
export GLOG_log_dir=/custom/log/path
./pkt2flow input.pcap
```

## Development

### Running Tests
Expand Down
64 changes: 32 additions & 32 deletions examples/example_pkt2flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@
#include <string.h>

int main(void) {
// Initialize the hash table
init_hash_table();
// Initialize the hash table
init_hash_table();

// Create a 6-tuple for a flow (IPv4, TCP, src: 1.2.3.4:1234, dst: 5.6.7.8:80)
struct af_6tuple tuple;
memset(&tuple, 0, sizeof(tuple));
tuple.af_family = AF_INET;
tuple.protocol = 6; // TCP
tuple.ip1.v4.s_addr = 0x04030201; // 1.2.3.4
tuple.ip2.v4.s_addr = 0x08070605; // 5.6.7.8
tuple.port1 = 1234;
tuple.port2 = 80;
tuple.is_vlan = 0;
// Create a 6-tuple for a flow (IPv4, TCP, src: 1.2.3.4:1234, dst: 5.6.7.8:80)
struct af_6tuple tuple;
memset(&tuple, 0, sizeof(tuple));
tuple.af_family = AF_INET;
tuple.protocol = 6; // TCP
tuple.ip1.v4.s_addr = 0x04030201; // 1.2.3.4
tuple.ip2.v4.s_addr = 0x08070605; // 5.6.7.8
tuple.port1 = 1234;
tuple.port2 = 80;
tuple.is_vlan = 0;

// Register the flow
struct ip_pair *pair = register_ip_pair(tuple);
if (pair) {
printf("Flow registered: src=%x:%u dst=%x:%u\n",
pair->af_6tuple.ip1.v4.s_addr, pair->af_6tuple.port1,
pair->af_6tuple.ip2.v4.s_addr, pair->af_6tuple.port2);
}
// Register the flow
struct ip_pair *pair = register_ip_pair(tuple);
if (pair) {
printf("Flow registered: src=%x:%u dst=%x:%u\n",
pair->af_6tuple.ip1.v4.s_addr, pair->af_6tuple.port1,
pair->af_6tuple.ip2.v4.s_addr, pair->af_6tuple.port2);
}

// Find the flow
struct ip_pair *found = find_ip_pair(tuple);
if (found) {
printf("Flow found!\n");
} else {
printf("Flow not found!\n");
}
// Find the flow
struct ip_pair *found = find_ip_pair(tuple);
if (found) {
printf("Flow found!\n");
} else {
printf("Flow not found!\n");
}

// Reset the flow's packet dump file
reset_pdf(&pair->pdf);
// Reset the flow's packet dump file
reset_pdf(&pair->pdf);

// Free the hash table
free_hash_table();
// Free the hash table
free_hash_table();

return 0;
}
return 0;
}
2 changes: 1 addition & 1 deletion format_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ echo -e "${YELLOW}Using clang-format version: $(clang-format --version | head -n

# Define source directories and file patterns
SOURCE_DIRS=("src" "tests" "examples")
FILE_PATTERNS=("*.cpp" "*.h" "*.hpp")
FILE_PATTERNS=("*.cpp" "*.h" "*.hpp" "*.c")

# Collect all files to format
FILES_TO_FORMAT=()
Expand Down
71 changes: 53 additions & 18 deletions src/pkt2flow.h → include/pkt2flow.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

/* pkt2flow
* Xiaming Chen (chen_xm@sjtu.edu.cn)
*
* Copyright (c) 2012 Xiaming Chen <chen_xm@sjtu.edu.cn>
* Copyright (c) 2012
* Copyright (c) 2014 Sven Eckelmann <sven@narfation.org>
*
* Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -30,32 +31,28 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef PKT2FLOW_H
#define PKT2FLOW_H

#include <netinet/in.h>
#include <netinet/ip6.h>
#include <pcap/pcap.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define __SOURCE_VERSION__ "1.2"
#define __AUTHOR__ "X. Chen (chenxm35@gmail.com)"
#define __SOURCE_VERSION__ "1.4"
#define __AUTHOR__ "Xiaming Chen (chenxm35@gmail.com)"
#define __GLOBAL_NAME__ "pkt2flow"
#define FLOW_TIMEOUT 1800 // seconds
#define HASH_MULTIPLIER 37
#define HASH_TBL_SIZE 48611

#define BIT(bitnr) (1ULL << (bitnr))
#ifndef PKT2FLOW_INLINE_ISSET_BITS
#define PKT2FLOW_INLINE_ISSET_BITS
static inline int isset_bits(uint64_t x, uint64_t bitmask) {
return (x & bitmask) == bitmask;
}
#endif
#define isset_bits(x, bitmask) \
({ \
uint32_t _bitmask = (uint32_t)(bitmask); \
uint32_t _x = (uint32_t)(x); \
(_bitmask & _x) == _bitmask; \
})

enum dump_allow_flags {
DUMP_OTHER_ALLOWED = BIT(0),
Expand All @@ -73,6 +70,7 @@ enum pkt_dump_file_status {
struct pkt_dump_file {
char *file_name;
unsigned long pkts;
pcap_dumper_t *dumper;

enum pkt_dump_file_status status;
unsigned long start_time;
Expand Down Expand Up @@ -106,6 +104,27 @@ struct ip_pair {
/* pkt2flow.c */
extern struct ip_pair *pairs[];

/*
* Open the trace file
*/
void open_trace_file(void);

/*
* Close the trace file
*/
void close_trace_files(void);

/*
* Handle Ethernet packet processing
*/
int pcap_handle_ethernet(struct af_6tuple *af_6tuple,
const struct pcap_pkthdr *hdr, const u_char *pkt);

/*
* Create the full file path for a packet dump file
*/
char *resemble_file_path(struct pkt_dump_file *pdf);

/* utilities.c */

/*
Expand Down Expand Up @@ -149,8 +168,24 @@ struct ip_pair *register_ip_pair(struct af_6tuple af_6tuple);
*/
void reset_pdf(struct pkt_dump_file *f);

#ifdef __cplusplus
}
#endif
/*
* Set the dump allowed flags for controlling which packet types to process
*/
void set_dump_allowed(uint32_t flags);

/*
* Set the input file to process
*/
void set_readfile(const char *filename);

/*
* Set the output directory
*/
void set_outputdir(const char *dir);

/*
* Process the trace file
*/
void process_trace(void);

#endif /* PKT2FLOW_H */
#endif /* PKT2FLOW_H */
Binary file added sample/tpncp_udp.pcap
Binary file not shown.
Binary file added sample/udp_lite_illegal_large-coverage.pcap
Binary file not shown.
Binary file added sample/udp_lite_normal_coverage_8-20.pcap
Binary file not shown.
Loading
Loading