简体   繁体   English

通过原始套接字接收“ syn”数据包时,如何响应“ syn ack”数据包?

[英]How do I respond with a “syn ack” packet when receiving a “syn” packet through a raw socket?

I am curious about raw sockets and and how to create them and would like to implement my own TCP mechanism. 我对原始套接字以及如何创建它们感到好奇,并想实现自己的TCP机制。 I have read some examples and have succeeded with sending both custom made TCP packets and UDP packets with my own written IP header (of course influenced by a lot of examples). 我已经阅读了一些示例,并成功发送了带有我自己编写的IP标头的定制TCP数据包和UDP数据包(当然,受许多示例影响)。 I have checked with Wireshark that the packet is reaching its destination, so everything is fine so far. 我已经与Wireshark一起检查了数据包是否已到达目的地,因此到目前为止一切都很好。

But regarding TCP packets, to make the full handshake: 但是关于TCP数据包,要进行完整的握手:

Client     Server
syn  ---> 
     <---  syn ack 
ack  --->

What do I need from the server´s point of view to get the syn packet so I can send the syn ack back to the client? 从服务器的角度来看,我需要什么来获取syn数据包,以便可以将syn ack发送回客户端?

To receive packets on a raw socket, just call recv or recvfrom on it. 要在原始套接字上接收数据包,只需在其上调用recvrecvfrom The OS will return you a copy of the next packet addressed to the machine, with headers and all, which should include address info. 操作系统将向您返回下一个发送到计算机的数据包的副本,其中包括标头和全部,其中应包括地址信息。 Watch the destination address, port, and transport protocol, and ignore any that aren't what you were expecting. 观察目标地址,端口和传输协议,并忽略所有与预期不符的内容。 (Since the point of a raw socket is that there's no built-in notion of ports or anything the OS could use to route packets to sockets, it doesn't know what program to send it to...so every raw socket should receive every packet addressed to the machine. Meaning you might receive lots of crap you have no interest in.) (由于原始套接字的要点是没有内置的端口或操作系统可用于将数据包路由到套接字的任何内容,因此它不知道将其发送到哪个程序...因此每个原始套接字都应接收每个发送到机器的数据包。这意味着您可能会收到很多废话,对此您毫无兴趣。)

Once you see a packet addressed to "you", just build a SYN/ACK packet and send it to the address and port listed as the source in the received packet. 一旦看到发给“ you”的数据包,只需构建一个SYN / ACK数据包,然后将其发送到接收到的数据包中列为源的地址和端口即可。

Note, though: the OS will often do its own processing of TCP and UDP packets (including sending ICMP "port unreachable" or other responses for ports it doesn't have listeners for)...and doing your own processing on top of that is bound to cause wackiness. 但是请注意:操作系统通常会自己处理TCP和UDP数据包(包括发送ICMP“端口无法访问”或针对其没有侦听器的端口的其他响应)...并在此之上进行自己的处理必然会导致古怪。 If you're going to implement your own flavor of TCP, you might want to use a different protocol number. 如果要实现自己的TCP风格,则可能需要使用其他协议号。 (Of course, then most clients won't be able to connect to it...you'd have to make a client as well.) (当然,那么大多数客户端将无法连接到它...您也必须创建一个客户端。)

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

相关问题 当我使用原始套接字发送syn-packet时,为什么服务器不响应syn-ack数据包? - Why does not the server respond with syn-ack packets when I send syn-packets with raw sockets? 发送带有syn标志设置的原始tcp数据包只是通过lo接口,而不是我想要的eth0 - Sending a raw tcp packet with syn flag set just goes through the lo interface, not eth0 as I want 使用Boost :: Asio发送TCP SYN数据包 - Sending TCP SYN packet with Boost::Asio 是否可以使用WFP发送带有有效负载的tcp syn数据包? - Is it possible to send tcp syn packet with payload by using WFP? 在Linux原始套接字中接收完整的修改后的icmp数据包 - Receiving Full modified icmp packet in Linux raw socket 原始套接字在C ++中发送TCP SYN-FIN- .. - Raw Socket send TCP SYN-FIN-.. in c++ 为什么TCP套接字在收到错误的ACK时发送RST数据包而不是重新发送数据? - Why TCP socket send a RST packet instead of resending data when receives an incorrect ACK? 有没有办法阻塞套接字send(),直到我们得到该数据包的确认? - Is there a way to block on a socket send() until we get the ack for that packet? 使用libuv接收UDP数据包时如何知道目标地址和端口? - How to know target address and port when receiving a UDP packet with libuv? C ++通过UDP发送数据包但未接收到它 - C++ Send packet through UDP but not receiving it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM