Incorrect number of bytes returned by getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, buflen)

Ashwin Rao ashwin.shirvanthe at gmail.com
Thu Dec 3 13:29:13 EST 2009


Hi,

I need to retrieve the tcp_info from a socket using getsockopt.  The
details of structure tcp_info are available at [http://src.gnu-
darwin.org/src/sys/netinet/tcp.h.html]. The getsockopt method for a
socket whose documentation is available at [http://docs.python.org/
library/socket.html#socket.socket.getsockopt] specifies that - "if
buflen is present, it specifies the maximum length of the buffer used
to receive the option in, and this buffer is returned as a string". To
confirm my calculation, I computed the size of the tcp_info using the
following program, which returned 104.
---
#include <stdio.h>
#include <netinet/tcp.h>

int main()
{
  printf("%d", sizeof(struct tcp_info));
}
---
I then added the following lines to my program

tcp_info = sock.getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, 104)
print "Return len"+str(len(tcp_info))
for cnt in range(len(tcp_info)):
    print tcp_info[cnt]


The output for the length is 4, which is less than 104. I would like
to know the technique to obtain the tcp_info structure using the
getsocktopt method. I am currently using python 2.5.2.

Regards,
Ashwin



More information about the Python-list mailing list