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

Discussion in 'Python' started by Ashwin Rao, Dec 3, 2009.

  1. Ashwin Rao

    Ashwin Rao Guest

    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
     
    Ashwin Rao, Dec 3, 2009
    #1
    1. Advertising

  2. Ashwin Rao

    Roy Smith Guest

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

    In article
    <>,
    Ashwin Rao <> wrote:

    > 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.
     
    Roy Smith, Dec 3, 2009
    #2
    1. Advertising

  3. Re: Incorrect number of bytes returned bygetsockopt(socket.SOL_SOCKET, socket.TCP_INFO, buflen)

    On Thu, 3 Dec 2009 10:29:13 -0800 (PST),
    Ashwin Rao <> wrote:

    > 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
    --
    |
    Martien Verbruggen | Never hire a poor lawyer. Never buy
    | from a rich salesperson.
    |
     
    Martien Verbruggen, Dec 3, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Taylor
    Replies:
    4
    Views:
    2,130
    =?UTF-8?B?0LTQsNC80ZjQsNC9INCzLg==?=
    Apr 21, 2004
  2. Fabian Deutsch

    getsockopt

    Fabian Deutsch, Dec 16, 2005, in forum: Python
    Replies:
    0
    Views:
    537
    Fabian Deutsch
    Dec 16, 2005
  3. Steve Holden

    Re: getsockopt

    Steve Holden, Dec 16, 2005, in forum: Python
    Replies:
    0
    Views:
    430
    Steve Holden
    Dec 16, 2005
  4. chris
    Replies:
    0
    Views:
    864
    chris
    May 21, 2010
  5. chris
    Replies:
    0
    Views:
    1,224
    chris
    May 21, 2010
Loading...

Share This Page