tags:

views:

116

answers:

1

I'm trying to work with sockets and I have such problem

In code example:

setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,self.listen_address+'\0')

I have error

AttributeError: 'module' object has no attribute 'SO_BINDTODEVICE'

On Linux machine this attribute is OK but on FreeBSD trere are no any SO_* attributes in module IN. What port should I install to resolve this problem on FreeBDS machine?

Python versions on Linux tested: 2.5.4 and 2.6.4; on FreeBSD: 2.5.5

I can't find anything about this module in my book, and googling keyword IN looks like seamless ...

update:

I can only bind to address, not to device.

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.bind(("eth0",3040))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in bind
socket.gaierror: [Errno -2] Name or service not known
>>> s.bind(("192.168.33.152",3040))
>>> s.close()

update 2:

... but I'm working with broadcast packets. I'm trying to write daemon which acts like DHCP server. If I bind to address would I catch broadcast packets? And if I'll set promiscuous mode on?

+1  A: 

SO_BINDTODEVICE socket option is not standard and is not supported on FreeBSD. Why can't you just use regular bind(2) for assigning local address/interface?

Edit:

Take a look at the socket object docs.
Here's an example.

Edit 2:

You didn't say what exactly you are trying to achieve, so assuming regular TCP/IP client-server.

IP, being a network-layer protocol (vs. for example, Ethernet, which is a data-link protocol), is not concerned with devices, but addresses. The idea is that you don't need to bind to a device - the OS takes care of mapping addresses to devices, and maintains a routing table. The only time you need explicit relationship between a socket and a device is when working with broadcast and multicast, where mapping between addresses and interfaces is not obvious.

Each network interface known to TCP/IP stack is assigned an IP address (see ifconfig(8)). Bind your socket to that IP address and you'll be all set.

Hope this helps.

Edit 3:

Have you looked into SO_BROADCAST option? Also check out this SO question about raw sockets.

Nikolai N Fetissov
How can use it in my python program for binding socket?
Shamanu4
Still can not bind to device. see updated.
Shamanu4
... but I'm working with broadcast packets. I'm trying to write daemon which acts like DHCP server. If I bind to address would I catch broadcast packets? And if I'll set promiscuous mode on?
Shamanu4