How to bind a listening socket to an address?
Ian Piumarta
ian.piumarta at inria.fr
Mon Sep 1 17:38:19 UTC 2003
On Mon, 1 Sep 2003, Bert Freudenberg wrote:
> Andreas Raab wrote:
>
> > The listening socket is always bound to the local network address (how could
> > it be otherwise?)
>
> No, a socket is always bound to a network interface (how could it be
> otherwise?).
No, a socket can be bound to an object in the filesystem too. (But I
digress... ;)
Since an interface (ignoring complications from ppp etc.) tends always to
have an assigned IP host address, the two statements are near enough
equivalent. For a listening stream socket there is always an address
component (the port number).
The situation with dgram sockets is a little more complex, since in
addition to bind()ing the socket to a local i/f address (using
listenOnPort with a backlog of 0) you can also (reversibly) connect() it
to an address -- which will cause it to send/recv only to/from the
"connected" address. (The VM doesn't actually do this, but it does an
equivalent thing in sqSocketConnectToPort(), which is legal for a dgram
socket, replacing the initial peer address INADDR_ANY:0 with the specified
peer address. The use of recvfrom() and sendto() to read/write data does
the rest. The end effect is the same.)
> Squeak does unfortunately not support this. In the plain VM, a listening
> socket is always bound to _all_ network interfaces. I think there have
> been hacks to bind to specific interfaces, but I do not remember where
> exactly I saw this.
In the VM the change needed to allow binding either a stream or a dgram
socket to an arbitrary interface address is utterly trivial (and backwards
compatible): replace the constant INADDR_ANY at line 20 of
sqListenOnPortBacklogSize() with the address of the interface to which you
want to bind. We'd need to pass this address in as an extra parameter,
meaning a new primitive, but this isn't so bad because the original
primitive would become a one-liner (punting immediately to the "extended"
primitive, adding INADDR_ANY as the additional i/f address argument).
Total VM support code upheaval: 2 lines modified and 3 added.
Ciao,
Ian
More information about the Squeak-dev
mailing list
|