connecting a socket to itself

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux