Passing argument to setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,intr)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Taylor

    Passing argument to setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,intr)


    Hi

    Does anyone know the correct way to pass the 'device' argument to setsockopt
    with the SO_BINDTODEVICE flag?

    I have tried various methods:

    setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,"eth0")
    setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,inet_aton("e th0"))
    setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,struct.pack( "s","eth0") )
    setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,struct.pack( "p","eth0") )

    None of these work. I just get a "socket.err or: (19, 'No such device')".

    I do have an "eth0" device :-)

    Many thanks

    Richard





  • Richard Taylor

    #2
    Re: Passing argument to setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,intr)

    Richard Taylor wrote:
    [color=blue]
    >
    > Hi
    >
    > Does anyone know the correct way to pass the 'device' argument to
    > setsockopt with the SO_BINDTODEVICE flag?
    >
    > I have tried various methods:
    >
    > setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,"eth0")
    > setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,inet_aton("e th0"))
    > setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,struct.pack( "s","eth0") )
    > setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,struct.pack( "p","eth0") )
    >
    > None of these work. I just get a "socket.err or: (19, 'No such device')".
    >
    > I do have an "eth0" device :-)
    >
    > Many thanks
    >
    > Richard[/color]

    Solved it!

    So for others that find this in the news archive...

    The answer is:

    s.setsockopt(so cket.SOL_SOCKET ,IN.SO_BINDTODE VICE,struct.pac k("%ds" %
    (len("eth0")+1, ), "eth0"))

    Richard


    Comment

    • Jeff Epler

      #3
      Re: Passing argument to setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE, intr)

      On Wed, Apr 21, 2004 at 12:22:41PM +0000, Richard Taylor wrote:[color=blue]
      > The answer is:
      >
      > s.setsockopt(so cket.SOL_SOCKET ,IN.SO_BINDTODE VICE,struct.pac k("%ds" %
      > (len("eth0")+1, ), "eth0"))
      > Richard Taylor wrote:[/color]

      Maybe this is simpler:
      #dev = "eth0"
      s.setsockopt(.. ., dev + '\0')
      I think it has the same effect as your code, but it is a little clearer
      to me that the setsockopt call needs a zero-terminated C string as its
      argument.

      Jeff

      Comment

      • Richard Taylor

        #4
        Re: Passing argument to setsockopt(sock et.SOL_SOCKET, IN.SO_BINDTODEV ICE, intr)

        Jeff Epler wrote:
        [color=blue]
        > On Wed, Apr 21, 2004 at 12:22:41PM +0000, Richard Taylor wrote:[color=green]
        >> The answer is:
        >>
        >> s.setsockopt(so cket.SOL_SOCKET ,IN.SO_BINDTODE VICE,struct.pac k("%ds" %
        >> (len("eth0")+1, ), "eth0"))
        >> Richard Taylor wrote:[/color]
        >
        > Maybe this is simpler:
        > #dev = "eth0"
        > s.setsockopt(.. ., dev + '\0')
        > I think it has the same effect as your code, but it is a little clearer
        > to me that the setsockopt call needs a zero-terminated C string as its
        > argument.
        >
        > Jeff[/color]

        You are right. I did not realise that you could append a null in that way.

        Thanks.

        Richard

        Comment

        • дамјан г.

          #5
          Re: Passing argument to setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,intr)

          > Does anyone know the correct way to pass the 'device' argument to[color=blue]
          > setsockopt with the SO_BINDTODEVICE flag?
          >
          > I have tried various methods:
          >
          > setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,"eth0")
          > setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,inet_aton("e th0"))
          > setsockopt(sock et.SOL_SOCKET,I N.SO_BINDTODEVI CE,struct.pack( "s","eth0") )[/color]

          I think you need
          struct.pack("5s ","eth0")


          --
          Дамјан (jabberID:damja n@bagra.net.mk)

          A: Because it reverses the logical flow of converstion.
          Q: Why is top posting frowned upon?

          Comment

          Working...