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

A

Ashwin Rao

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
 
R

Roy Smith

Ashwin Rao said:
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));
}

In general, the right way to do stuff like this in Python is to use the
struct module. In particular, struct.calcsize() will tell you how long the
string has to be. Try that and see if you come up with the same value.
 
M

Martien Verbruggen

tcp_info = sock.getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, 104)

In C, to get tcp_info, SOL_SOCKET should be IPPROTO_TCP. I'm assuming
it's the same in Python, although I've never tried it.

Martien
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top