Re: connecting an unbound socket to itself

From: David Miller
Date: Fri Apr 18 2008 - 18:49:40 EST


From: Serge Belyshev <belyshev@xxxxxxxxxxxxxxxxx>
Date: Sat, 19 Apr 2008 02:44:48 +0400

> David Miller <davem@xxxxxxxxxxxxx> writes:
>
> > From: Serge Belyshev <belyshev@xxxxxxxxxxxxxxxxx>
> > Date: Sat, 19 Apr 2008 02:24:04 +0400
> >
> >> But my socket is unbound and I'm connecting to a loopback address, so
> >> I expect the connection to fail if nobody listens on the port I am
> >> connecting to. Yet the connections succeedes on the rare occasion when
> >> the kernel chooses the source port number that matches the destination.
> >
> > At the point you do a connect() you are "listening" on that
> > port and can be connected to.
>
> Okay, so to reliably detect the case when nobody else but me is
> "listening" on that port I should do bind() (with a different port)
> instead of relying on the kernel, right?

If you bind to a local port, then connect to the same local port
it's still going to work.

You're only going to avoid the situation if you bind to a local
port and specifically connect to a different port.
--
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html