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

R

Richard Taylor

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(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,"eth0")
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,inet_aton("eth0"))
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("s","eth0"))
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("p","eth0"))

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

I do have an "eth0" device :)

Many thanks

Richard
 
R

Richard Taylor

Richard said:
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(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,"eth0")
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,inet_aton("eth0"))
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("s","eth0"))
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("p","eth0"))

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

I do have an "eth0" device :)

Many thanks

Richard

Solved it!

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

The answer is:

s.setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("%ds" %
(len("eth0")+1,), "eth0"))

Richard
 
J

Jeff Epler

The answer is:

s.setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("%ds" %
(len("eth0")+1,), "eth0"))
Richard Taylor wrote:

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
 
R

Richard Taylor

Jeff said:
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

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

Thanks.

Richard
 
G

Guest

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

I have tried various methods:

setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,"eth0")
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,inet_aton("eth0"))
setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("s","eth0"))

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

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top