[英]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. 要在原始套接字上接收数据包,只需在其上调用recv
或recvfrom
。 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.