Skip to content

Refactor library with Boost-style conventions, AF_PACKET capture, and C/C++ interfaces#6

Closed
Copilot wants to merge 4 commits intomainfrom
copilot/refactor-library-with-boost
Closed

Refactor library with Boost-style conventions, AF_PACKET capture, and C/C++ interfaces#6
Copilot wants to merge 4 commits intomainfrom
copilot/refactor-library-with-boost

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 27, 2025

Refactors the single-header C library into a modern C++11 library using AF_PACKET for packet capture, with both C and C++ interfaces.

Architecture

  • Header-only C++ library in include/rawsock/
    • capture.hpp - AF_PACKET based capture with protocol filtering
    • packet.hpp - Network header structures with portable packing macros
    • error.hpp - std::error_code integration
    • config.hpp - Platform detection and compiler compatibility
  • C wrapper via rawsock_c.h + src/rawsock_c.cpp
  • CMake build system with test/example targets

Key Changes

  • Replaced SOCK_RAW with AF_PACKET for layer 2 capture
  • Added RAWSOCK_PACKED macro for GCC/Clang/MSVC portability
  • Fixed recursive packet filtering → iterative loop (stack overflow risk)
  • Shared test utilities in tests/test_common.hpp

Usage

#include <rawsock/rawsock.hpp>

rawsock::capture_config config;
config.interface_name = "eth0";
config.filter_protocol = rawsock::protocol::tcp;

rawsock::capture cap;
cap.open(config);

std::vector<uint8_t> buf(65535);
rawsock::packet_info info;
int n = cap.capture_next(buf.data(), buf.size(), &info);
rawsock_capture_t* cap = rawsock_capture_create();
rawsock_config_t config;
rawsock_config_init(&config);
strcpy(config.interface_name, "eth0");
rawsock_capture_open(cap, &config);

Tests

6 test suites covering packet parsing, error handling, capture, and C interface compatibility.

Original prompt

重构下这个库, 编程风格使用boost,不依赖任何外部库, 使用AF_PACKET 封装网络抓包接口,对外提供c/cpp 接口使用,需要完善的ut测试和文档


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits November 27, 2025 09:08
…faces, and unit tests

Co-authored-by: Sphinxes0o0 <29710883+Sphinxes0o0@users.noreply.github.com>
…o, shared test header

Co-authored-by: Sphinxes0o0 <29710883+Sphinxes0o0@users.noreply.github.com>
Co-authored-by: Sphinxes0o0 <29710883+Sphinxes0o0@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor library using boost for network packet capture Refactor library with Boost-style conventions, AF_PACKET capture, and C/C++ interfaces Nov 27, 2025
Copilot AI requested a review from Sphinxes0o0 November 27, 2025 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants