From nobody@FreeBSD.org  Fri May 14 03:43:54 2004
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id E8F4A16A4CE
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 14 May 2004 03:43:54 -0700 (PDT)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 866CD43D2F
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 14 May 2004 03:43:54 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.11/8.12.11) with ESMTP id i4EAhsPI019056
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 14 May 2004 03:43:54 -0700 (PDT)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.11/8.12.11/Submit) id i4EAhsNW019055;
	Fri, 14 May 2004 03:43:54 -0700 (PDT)
	(envelope-from nobody)
Message-Id: <200405141043.i4EAhsNW019055@www.freebsd.org>
Date: Fri, 14 May 2004 03:43:54 -0700 (PDT)
From: Fabien MENEMENLIS <fabien@iliad.fr>
To: freebsd-gnats-submit@FreeBSD.org
Subject: socket connecting with itself (race condition?)
X-Send-Pr-Version: www-2.3

>Number:         66629
>Category:       misc
>Synopsis:       socket connecting with itself (race condition?)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri May 14 03:50:24 PDT 2004
>Closed-Date:    Sat May 15 07:41:03 PDT 2004
>Last-Modified:  Sat May 15 07:41:03 PDT 2004
>Originator:     Fabien MENEMENLIS
>Release:        4.9-STABLE
>Organization:
Iliad
>Environment:
FreeBSD huge.dir.com 4.9-STABLE FreeBSD 4.9-STABLE #2: Fri Mar  5 15:58:16 CET 2004     fabien@huge.iliad.fr:/usr/obj/usr/src/sys/DUAL  i386

>Description:
When connecting to a closed local port in PF_INET space with many clients, it happens once in a while that connect() succeeds. The output of netstat -A for the given connection is then:
ea2c0e40 tcp4       0      0  localhost.4555     localhost.4555     ESTABLISHED

I've not been able to reproduce the problem on a 5.2.1 environment.
>How-To-Repeat:
I know I shouldn't submit code here but it's pretty simple. For better readability see http://dir.com/testsock.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>


#define MAXFORK 100


int sockconnect(char *nomserv, int noport)
{
  struct sockaddr_in serv;
  struct hostent *host;
  int sock;


  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return(-1);

  if ((host = gethostbyname(nomserv)) == NULL)
    return(-1);
  serv.sin_family = AF_INET;
  serv.sin_port = htons(noport);
  memcpy(&serv.sin_addr, host->h_addr, host->h_length);

  if (connect(sock, (struct sockaddr *)&serv,
	      sizeof(struct sockaddr_in)) == -1) {
    close(sock);
    return(-1);
  }

  return(sock);
}


int main(void)
{
  int i;
  int s;


  for (i = 0; i < MAXFORK; i++) {
    if (fork() == 0) {
      for (;;) {
	s = sockconnect("localhost", 4555);
	if (s != -1) {
	  printf("connected... (s=%d)\n", s);
	}
	sleep(1);
      }
    }
  }
}

>Fix:
      
>Release-Note:
>Audit-Trail:

From: Maxim Konovalov <maxim@macomnet.ru>
To: Fabien MENEMENLIS <fabien@iliad.fr>
Cc: bug-followup@freebsd.org
Subject: Re: misc/66629: socket connecting with itself (race condition?)
Date: Fri, 14 May 2004 14:58:17 +0400 (MSD)

 On Fri, 14 May 2004, 03:43-0700, Fabien MENEMENLIS wrote:
 
 >
 > >Number:         66629
 > >Category:       misc
 > >Synopsis:       socket connecting with itself (race condition?)
 [...]
 > >Environment:
 > FreeBSD huge.dir.com 4.9-STABLE FreeBSD 4.9-STABLE #2: Fri Mar  5 15:58:16 CET 2004     fabien@huge.iliad.fr:/usr/obj/usr/src/sys/DUAL  i386
 >
 > >Description:
 > When connecting to a closed local port in PF_INET space with many clients, it happens once in a while that connect() succeeds. The output of netstat -A for the given connection is then:
 > ea2c0e40 tcp4       0      0  localhost.4555     localhost.4555     ESTABLISHED
 
 It's expected behaviour called "simultaneous open".
 
 > I've not been able to reproduce the problem on a 5.2.1 environment.
 
 Probably due to differences in portranges between HEAD and RELENG_4.
 
 [...]
 
 -- 
 Maxim Konovalov
State-Changed-From-To: open->closed 
State-Changed-By: maxim 
State-Changed-When: Sat May 15 07:40:30 PDT 2004 
State-Changed-Why:  
TCP simultaneous open is not a bug. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=66629 
>Unformatted:
