简体   繁体   English

重新创建并发送nfqueue捕获的数据包

[英]Recreate and send packet captured by nfqueue

I capture all the packets in one side with help of nfqueue , "record" them (all the data: ip info, next protocol info etc) with nfq_get_payload and deliver them into another side with help of udp. 我捕捉一侧的所有数据包与nfqueue的帮助下,“记录”他们(所有数据:IP信息,未来协议信息等)与nfq_get_payload并与UDP的帮助下交付到另一侧。 How can I restore this packet on another side and then send to myself(2 side) like there is no udp-encapsulation between? 我如何才能在另一侧还原此数据包,然后像之间没有udp封装一样发送给自己(2侧)? Should I use some nfqueue API or I have to implement all the protocols packet creation (UDP, ICMP, TCP, etc)? 我应该使用某些nfqueue API还是必须实现所有协议数据包创建(UDP,ICMP,TCP等)? And how should I send this restored packet? 我应该如何发送此恢复的数据包?

Ok, I successfully recreated and sent forward my packet encapsulated in UDP. 好的,我成功地重新创建并转发了封装在UDP中的数据包。 After recreation I needed to send this packet to another IP, but you can use original destination address. 重新娱乐后,我需要将此数据包发送到另一个IP,但是您可以使用原始目标地址。 So the code snippet: 所以代码片段:

char *full_packet;
int size;
// some actions to get full_packet and size from UDP packet
// assume you recreated this: int size = nfq_get_payload(nfa, &full_packet);


int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
// also optional string needed in my case:
setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, "wlan0", strlen("wlan0")); 
if (sock == -1) {
    perror("socket() failed");
    return 1;
}

struct sockaddr_in to;
struct ip *iph = (struct ip *)full_packet;

to.sin_addr.s_addr = inet_addr("192.168.0.107"); // here you can set IP address where you need send this packet
to.sin_family = AF_INET;

int bytes = sendto(sock, full_packet, size, 0, (struct sockaddr*) &to, sizeof(to));
if (bytes == -1) {
    perror("send() failed");
    return 1;
}

I hope this will help somebody 我希望这会对某人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM