简体   繁体   English

TCP数据包段python

[英]TCP packet segment python

I'm handling TCP packets. 我正在处理TCP数据包。 This application acts as an sniffer and builds up SIP Packets. 该应用程序充当嗅探器并建立SIP数据包。 Some TCP SIP packets come fragmented hence I need to reassemble them. 一些TCP SIP数据包是零散的,因此我需要重新组装它们。

What I do is that Initially I process first TCP connection using: 我要做的是,最初,我使用以下命令处理第一个TCP连接:

packet = s.recvfrom(sipLocatorConfig.NETWORK_MAX_SIZE)

After that I obtain the data and then if fragmented I get the rest but using .recv : 之后,我获取了数据,然后将其分割,剩下的只是使用.recv

socket.recv(sipLocatorConfig.NETWORK_TCP_MAX_SIZE)

When I obtained the second fragment, I get garbled data at the beginning when using .recv , which I assume is the ETH, IP, TCP info: 当我获得第二个片段时,使用.recv ,我在一开始会出现乱码数据,我认为这是ETH,IP,TCP信息:

M*?
M*?@{
QyE

How can I remove this ETH, IP and TCP header info and extract just the data from Packet? 如何删除此ETH,IP和TCP标头信息并仅从Packet中提取数据? Thanks 谢谢

You can parse the packet as: 您可以将数据包解析为:

  • First 14 bytes is ethernet header 前14个字节是以太网头
  • Next 20 bytes is probably ip header 接下来的20个字节可能是ip标头
  • similarly,from ip header, we can extract protocol we are using and finally see the protocol reference to parse data out of it 同样,从ip标头中,我们可以提取正在使用的协议,最后看到协议参考以解析其中的数据

Eg. 例如。 TCP connection packet comes as : eth_header+IP_header+TCP_Header+payload(data) Each header have their specific length and extracting informations out of the headers would require you to use struct modules in python (see struct.unpack("","")). TCP连接数据包如下: eth_header+IP_header+TCP_Header+payload(data)每个标头都有其特定长度,并且从标头中提取信息将需要您在python中使用struct模块(请参见struct.unpack(“”,“”) )。 Extracting header infos also would require knowledge of the structure of headers. 提取标题信息还需要了解标题的结构。 Finally you can calculate the payload position and finally extract it. 最终,您可以计算出有效载荷位置并最终将其提取出来。

Specifically, please see this link for more information. 具体来说,请参阅此链接以获取更多信息。 Hope this helps 希望这可以帮助

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

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