sockets: bind to external interface

  • Thread starter Hans Georg Schaathun
  • Start date
H

Hans Georg Schaathun

Is there a simple way to find the external interface and bind a
socket to it, when the hostname returned by socket.gethostname()
maps to localhost?

What seems to be the standard ubuntu configuration lists the local
hostname with 127.0.0.1 in /etc/hosts. (I checked this on two ubuntu
boxen, on only one of which I am root.) Thus, the standard solution
of binding to whatever socket.gethostname() returns does not work.

Has anyone found a simple solution that can be administered without
root privileges? I mean simpler than passing the ip address
manually :)

TIA
 
C

Chris Angelico

Has anyone found a simple solution that can be administered without
root privileges?  I mean simpler than passing the ip address
manually :)

You can run 'ifconfig' without being root, so there must be a way. At
very worst, parse ifconfig's output.

The way you talk of "the" external interface, I'm assuming this
computer has only one. Is there a reason for not simply binding to
INADDR_ANY aka 0.0.0.0? Do you specifically need to *not* bind to
127.0.0.1?

Chris Angelico
 
J

Jean-Paul Calderone

You can run 'ifconfig' without being root, so there must be a way. At
very worst, parse ifconfig's output.

The way you talk of "the" external interface, I'm assuming this
computer has only one. Is there a reason for not simply binding to
INADDR_ANY aka 0.0.0.0? Do you specifically need to *not* bind to
127.0.0.1?

Chris Angelico

Binding to 0.0.0.0 is usually the right thing to do. The OP should
probably do that unless he has some particular reason for doing
otherwise. The comment about "the standard solution of binding to
whatever socket.gethostname() returns" suggests that perhaps he wasn't
aware that actually the standard solution is to bind to 0.0.0.0.

However, the system stack can usually be tricked into revealing some
more information this way:
('192.168.1.148', 47679)

Jean-Paul
 
H

Hans Georg Schaathun

You can run 'ifconfig' without being root, so there must be a way. At
: very worst, parse ifconfig's output.

Of course, but I am not sure that's simpler than the manual solution.
Especially since there is more than one version of ifconfig ...

: The way you talk of "the" external interface, I'm assuming this
: computer has only one. Is there a reason for not simply binding to
: INADDR_ANY aka 0.0.0.0?

Ah. That's what I really wanted. Thanks a lot. I wonder why that
was not mentioned in the tutorial I used ...
 
H

Hans Georg Schaathun

: The way you talk of "the" external interface, I'm assuming this
: : computer has only one. Is there a reason for not simply binding to
: : INADDR_ANY aka 0.0.0.0?
:
: Ah. That's what I really wanted. Thanks a lot. I wonder why that
: was not mentioned in the tutorial I used ...

Hmmm. socket.INADDR_ANY is an integer and bind insists on a string
for the hostname (Python 2.6). Is there any use for the integer
constant? "0.0.0.0" does exactly what I wanted though. Thanks again.
 
C

Chris Angelico

:  The way you talk of "the" external interface, I'm assuming this
:  computer has only one. Is there a reason for not simply binding to
:  INADDR_ANY aka 0.0.0.0?

Ah.  That's what I really wanted.  Thanks a lot.  I wonder why that
was not mentioned in the tutorial I used ...

If you don't care what port you use, you don't need to bind at all.
That may be why it's not mentioned - the classic TCP socket server
involves bind/listen/accept, and the classic TCP client has just
connect; bind/connect is a lot less common.

Incidentally, interfaces don't have to correspond 1:1 to network
cards. At work, we have a system of four IP addresses for each server,
even though it has only one NIC - it's used for traffic management and
routing. Binding to a specific address is sometimes important there.

Chris Angelico
 
T

Thomas Rachel

Am 25.04.2011 22:14 schrieb Hans Georg Schaathun:
: The way you talk of "the" external interface, I'm assuming this
: computer has only one. Is there a reason for not simply binding to
: INADDR_ANY aka 0.0.0.0?

Ah. That's what I really wanted. Thanks a lot. I wonder why that
was not mentioned in the tutorial I used ...

Generally, it seems better to use '' instead of '0.0.0.0' in this case
in order to stay compatible with other address families, especially INET6.


Thomas
 
T

Thomas Rachel

Am 25.04.2011 22:30, schrieb Chris Angelico:
If you don't care what port you use, you don't need to bind at all.
That may be why it's not mentioned - the classic TCP socket server
involves bind/listen/accept, and the classic TCP client has just
connect; bind/connect is a lot less common.

That is right, but I cannot see where he mentions the "direction" of the
socket. My fist thought was that he tries to have a server socket...

(BTW: bind can be omitted on server sockets as well; listen() seems to
includes a bind(('', 0)) if not called explicitly before. In this case,
the port is assigned randomly. Can be useful in some cases, where the
port number is not fixed...)

Incidentally, interfaces don't have to correspond 1:1 to network
cards. At work, we have a system of four IP addresses for each server,
even though it has only one NIC - it's used for traffic management and
routing. Binding to a specific address is sometimes important there.

If you use IPv6 and activate the privacy extensions (in order to
periodically create a new IP address), a NIC will have even more
addresses - the ones which aren't used any longer will be kept for a
certain time on order not to kill any existing connections.


Thomas
 
C

Chris Angelico

Hmmm.  socket.INADDR_ANY is an integer and bind insists on a string
for the hostname (Python 2.6).  Is there any use for the integer
constant?  "0.0.0.0" does exactly what I wanted though.  Thanks again..

Apologies - I've done most of my sockets programming in C, where the
integer constant is applicable. 0.0.0.0 means the exact same thing.

Chris Angelico
 
C

Chris Angelico

Am 25.04.2011 22:30, schrieb Chris Angelico:


That is right, but I cannot see where he mentions the "direction" of the
socket. My fist thought was that he tries to have a server socket...

(BTW: bind can be omitted on server sockets as well; listen() seems to
includes a bind(('', 0)) if not called explicitly before. In this case, the
port is assigned randomly. Can be useful in some cases, where the port
number is not fixed...)

Yes; for FTP data sockets, it doesn't matter what the port is, as long
as you tell the other end. Same as you can bind/connect, you can
not-bind and listen/accept. This is why I'm glad the socket subsystem
allows unusual behaviours (I've used bind/connect in a few places).
Give the programmer the tools and let him do what he chooses!

Chris Angelico
 
H

Hans Georg Schaathun

That is right, but I cannot see where he mentions the "direction" of the
: socket. My fist thought was that he tries to have a server socket...

Quite right. I thought it was implied by the need to bind :)
Sorry for the lack of detail.
 
J

Jean-Michel Pichavant

Hans said:
Is there a simple way to find the external interface and bind a
socket to it, when the hostname returned by socket.gethostname()
maps to localhost?

What seems to be the standard ubuntu configuration lists the local
hostname with 127.0.0.1 in /etc/hosts. (I checked this on two ubuntu
boxen, on only one of which I am root.) Thus, the standard solution
of binding to whatever socket.gethostname() returns does not work.

Has anyone found a simple solution that can be administered without
root privileges? I mean simpler than passing the ip address
manually :)

TIA
Hi,

Use the address 0.0.0.0

JM
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top