简体   繁体   English

如何使用 scapy 收到一个数据包的两个答案?

[英]How can I receive two answers to one packet using scapy?

I am developing something like TCP Server in Python using scapy.我正在使用 scapy 在 Python 中开发类似 TCP 服务器的东西。 A typical TCP server should work like this.一个典型的 TCP 服务器应该是这样工作的。

  • First, it waits for an incoming SYN packet by calling sniff .首先,它通过调用sniff等待传入的 SYN 数据包。
  • Second, it sends a SYNACK and waits for an ACK response in a single call, this is done with sr1 call.其次,它在单个调用中发送 SYNACK 并等待 ACK 响应,这是通过sr1调用完成的。
  • Third, it waits for an incoming packet with a client request by calling sniff .第三,它通过调用sniff等待带有客户端请求的传入数据包。

But the problem is that request packet can arrive between the sr1 and second sniff and thus get lost, because sniff does not capture packets that arrived before it is called.但问题是请求数据包可以在sr1和第二次sniff之间到达并因此丢失,因为sniff不会捕获在它被调用之前到达的数据包。 (I can see with Wireshark that a packet is arriving). (我可以通过 Wireshark 看到数据包正在到达)。

How can I send SYNACK and receive both ACK and a request in one call, 'atomically'?如何在一次调用中“自动”发送 SYNACK 并接收 ACK 和请求?

(In a typical TCP connection, the client will resend the packet with the request after some timeout, but according to the conditions of my task there is no re-sending of packets, packets cannot be lost). (在典型的TCP连接中,客户端会在超时后重新发送带有请求的数据包,但根据我的任务条件,没有重新发送数据包,数据包不会丢失)。

You can create a scapy socket and call sr1 or sniff on it.您可以创建一个 scapy 套接字并在其上调用sr1sniff For instance例如

from scapy.config import conf
sock = conf.L3socket()
sock.sr1(....)
sock.sniff(...)

Because it is the same socket, you won't be losing any packets因为它是同一个套接字,所以您不会丢失任何数据包

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

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