connecting a socket to itself

From: Serge Belyshev
Date: Fri Apr 18 2008 - 08:38:24 EST


Doing "connect (s, 127.0.0.1:port)" where port is in
net.ipv4.ip_local_port_range may result in a connected to
itself socket.

Looks like a bug.

Below is an example, usage: "./a.out port_number":

#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>

#include <sys/socket.h>
#include <netinet/in.h>


int main (int argc, char *argv[])
{
int s, e, port, n;
struct sockaddr_in addr;

if (argc < 2)
return 1;

port = atoi (argv [1]);
fprintf (stderr, "connecting to localhost:%i... ", port);

s = socket (PF_INET, SOCK_STREAM, 0);

addr.sin_family = AF_INET;
addr.sin_port = htons (port);
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);


for (e = -1, n = 0; -1 == e; n ++)
e = connect (s, (struct sockaddr *) &addr, sizeof (struct sockaddr_in));

fprintf (stderr, "ok, connected after %i tries\n", n);

sleep (86400);
return 0;
}
--
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