connect(2) behavior with unreacheable hosts
    M. Warner Losh 
    imp at bsdimp.com
       
    Sat Apr 12 20:01:24 PDT 2003
    
    
  
In message: <20030412.204912.76964336.imp at bsdimp.com>
            "M. Warner Losh" <imp at bsdimp.com> writes:
: In message: <109.225ca595.2bc723f2 at aol.com>
:             BelletJr at aol.com writes:
: : Why does not connect(2) return any error when trying to connect to a host 
: : unreachable because of an infinite loop in the routes? No time-out occurs and 
: : the value 0 is returned by connect(2).
: 
: Hmmmmm, you are correct.  I was sure that you were nuts, but on
: -current the following program returns no error at all...  Telnet
: shows the same behavior.  This is clearly wrong.
: 
: #include <sys/types.h>
: #include <sys/socket.h>
: #include <netinet/in.h>
: #include <arpa/inet.h>
: #include <err.h>
: 
: int main(void)
: {
:     int fd;
:     struct in_addr addr;
:     struct sockaddr_in sa;
:     int flags;
: 
:     fd = socket(PF_INET, SOCK_STREAM, 0);
:     inet_aton("1.2.3.4", &addr);
: 
:     sa.sin_len = sizeof(sa);
:     sa.sin_family = AF_INET;
:     sa.sin_port = htons(1234);
:     memcpy(&sa.sin_addr, &addr, sizeof(addr));
:     if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)))
: 	err(1, "connect");
:     exit(0);
: }
I just read the rest of the thread:
1) route loops don't matter.  connect should timeout after a while,
   and on stable it is like 75 seconds.  loop elsewhere doesn't matter
   because it is the same thing as a black hole.  You don't get the
   handshake back, you timeout.  Doesn't matter why (and the sending
   host can't tell them apart).
2) This is clearly TCP.
3) telnet 1.2.3.4 does the following bogusly:
	Trying 1.2.3.4...
	Connected to 1.2.3.4.
	Escape character is '^]'.
	Connection closed by foreign host.
All the lines are printed in < 1s.
However, this may be because I live behind a NAT box.  Any chance you
did your testing in a similar environment, which is causing your
confusion.  On my non-NAT'd boxes, this works as expected.
Warner
    
    
More information about the freebsd-current
mailing list