TCP/IP C++ 的解析器和响应器

TCP/IP parser and responder for C++

我必须在 C++ 中处理原始 IP 数据包,因为我使用 Open VPN 作为库来传送 IP 数据包。因此,我需要一个不断为我响应并只提供 TCP/UDP 有效负载的库。

我找到了 libtrace 但它仅用于解析。但是,我仍然需要手动确认每个 TCP 数据包并跟踪 TCP 计数等。

是否有一个库可以为我处理 IP、TCP 和 UDP 层,并且只提供有效负载以便我使用?

正如我在评论中提到的,并向您展示取自 Libtins. 的示例,正​​如您从我的评论中了解到的,它具有监控数据包的能力。它还具有制作您自己的有效负载的能力,这两种组合允许您使用一个库进行响应、跟踪和监控。

一个syn scanner is also provided along with other examples such as an arp monitor also. Filtering的例子很简单。

创建数据包并发送,一行即可完成

EthernetII eth = EthernetII("77:22:33:11:ad:ad", info.hw_addr) / 
                 IP("192.168.0.1", info.ip_addr) /
                 TCP(13, 15) /
                 RawPDU("PAYLOAD");

你可以再次扩展这个

#include <tins/tins.h>
#include <cassert>
#include <iostream>
#include <string>

using namespace Tins;

int main() {
    // We'll use the default interface(default gateway)
    NetworkInterface iface = NetworkInterface::default_interface();

    /* Retrieve this structure which holds the interface's IP, 
     * broadcast, hardware address and the network mask.
     */
    NetworkInterface::Info info = iface.addresses();

    /* Create an Ethernet II PDU which will be sent to 
     * 77:22:33:11:ad:ad using the default interface's hardware 
     * address as the sender.
     */
    EthernetII eth("77:22:33:11:ad:ad", info.hw_addr);

    /* Create an IP PDU, with 192.168.0.1 as the destination address
     * and the default interface's IP address as the sender.
     */
    eth /= IP("192.168.0.1", info.ip_addr);

    /* Create a TCP PDU using 13 as the destination port, and 15 
     * as the source port.
     */
    eth /= TCP(13, 15);

    /* Create a RawPDU containing the string "I'm a payload!".
     */
    eth /= RawPDU("I'm a payload!");

    // The actual sender
    PacketSender sender;

    // Send the packet through the default interface
    sender.send(eth, iface);
}