-1

将来自接口的数据包发送回同一接口而不更改数据包中的任何内容的最佳方法是什么?我想对来自我的一个接口的实际流量产生环回效果,例如eth0

4

2 回答 2

1

我认为您不能使用物理接口轻松做到这一点。不过,我为此使用了 tap 模块。这很简单:我创建了一个新的点击界面,我的程序写回了从设备读取的所有内容。我用它来测试一个专有的网络协议——所以它可能适用于你打算做的事情,也可能不适用。代码很简单:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <net/if.h>

#include <linux/if_tun.h>

#define DEVNAME "gnlo0"

static int tun_alloc(char *dev)
{
    struct ifreq ifr;
    int fd, ret;

    if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
        perror("open");
        return -1;
    }

    memset(&ifr, 0, sizeof(ifr));

    ifr.ifr_flags = IFF_TAP;
    if (*dev)
        strncpy(ifr.ifr_name, dev, IFNAMSIZ);

    ret = ioctl(fd, TUNSETIFF, (void *)&ifr);
    if (ret < 0) {
        close(fd);
        perror("ioctl TUNSETIFF");
        return ret;
    }
    strcpy(dev, ifr.ifr_name);
    return fd;
}

int main(int argc, char **argv)
{
    int fd = -1;
    int ret = 1;
    char dev[IFNAMSIZ];
    strncpy(dev, DEVNAME, IFNAMSIZ - 1);
    printf("opening %s\n", dev);

    fd = tun_alloc(dev);
    if (fd < 0)
        goto out;

    char buf[512];
    snprintf(buf, sizeof(buf) - 1,
             "ip addr flush dev %s; ip link set dev %s up", dev, dev);
    if (system(buf) < 0) {
        perror("system");
        goto out;
    }

    while (1) {
        unsigned char packet[65535];
        int len = read(fd, packet, sizeof(packet));
        if (len < 0) {
            perror("read");
            goto out;
        }
        printf("incoming packet [%d octets]\n", len);

        len = write(fd, packet, len);
        printf("fed back packet [%d octets]\n", len);
    }
    ret = 0;

out:
    if (fd >= 0)
        close(fd);
    return ret;
}
于 2013-01-29T17:25:02.880 回答
1

我认为您可以使用 Python/Scapy 轻松实现这一目标。就像是

sniff(iface="eth0", prn=lambda x: sendp(x, iface="eth0"))

应该这样做。

于 2013-01-30T00:10:51.723 回答