简体   繁体   中英

how to get IP ID, SEQ id, ACK id in python tcp socket?

Python socket in linux (bsd socket)

import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('www.google.com', 80))
s.send('GET / HTTP/1.1\r\n\r\n')
s.recv(1024)

Is it possible to get IP ID, SEQ id, ACK id for the curren s object?

  • best if not tampering too much with normal tcp socket internals.
  • best if the tracking could work in real-time.
  • replacing normal AF_INE/SOCK_STREAM with raw packets is inefficient because you have to re-implement the whole TCP stack in user space.
  • The purpose of the question is that I am adding diagnosis code, would be attached to an existing project, they use normal TCP socket extensively.
  • I tried to use AF_PACKET to capture raw incoming & outgoing packets, but feels a really ugly hack.
  • Besides, if multi-threaded sockets are sending the same request concurrently in TCP, you can't tell which captured packet belongs to which, because the tuple (srcip, srcport, dstip, dstport, proto_num) are all the same.

An althernative way to asking the question is how to label the packet stream tuple (srcip, srcport, dstip, dstport, proto_num) to inode/fd on Linux?

Is this possible at all? Maybe using netlink ? I am sure there's a seq/ack id to inode table somewhere at kernel which could be exposed.

Under Ubuntu 12.04 LTS + python 2.7 with root of course.

Internal TCP state is generally not made available to application programs.

Your options are:

  • modify the kernel to allow access to this via getsockopt()
  • use a user-space TCP library that is implemented on top of raw IP datagrams (perhaps ioremap though I've never used it)
  • find some way to do what you want without this information

Personally, I recommend the last.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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