Strange connect behavior
Hello,
While writing some client server application in c, we noticed a strange behavior : if we try to connect endlessly to a given local port where nobody is listening, and if the port is >= to 32768, after several thousands tries ( Connection refused ) connect will return 0. This behavior is not exhibited when port is < 32768.
We confirmed this behavior in kernel 2.6.17-10, 2.6.18-1, 2.6.8, on x86 and 2.4.21-32 on ia64, on several hardware configurations. Distribution is debian or ubuntu.
Attached is a source file that demonstrate this behavior. ./a.out port_number
Sample execution :
./a.out 35489
Out port : 35489
connect try 1 failed : Connection refused
connect try 2 failed : Connection refused
connect try 3 failed : Connection refused
.....
connect try 6089 failed : Connection refused
connect try 6090 failed : Connection refused
Connection success : 6091 try
Connection closed
last error : Connection refused
Is this behavior to be expected?
Can it be disabled?
Thanks in advance.
Regards,
Brice Videau
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
int main(int argc, char **argv) {
unsigned short out_port = 35000;
char * endptr;
struct sockaddr_in server_address;
int out_socket;
int i=1;
if( argc > 1 ) {
out_port = (unsigned short) strtol(argv[1], &endptr, 10);
errno = 0;
if(errno != 0 || out_port == 0 || endptr == argv[1] ) {
perror("Invalid port number");
exit(1);
}
}
printf( "Out port : %hu\n", out_port );
fflush(NULL);
memset( &server_address, 0, sizeof(struct sockaddr_in) );
server_address.sin_family = AF_INET;
server_address.sin_port = htons(out_port);
if ( inet_pton( AF_INET, "127.0.0.1", &(server_address.sin_addr) ) <= 0 ) {
perror("inet_pton error");
exit(1);
}
if ( (out_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket error");
exit(1);
}
while (connect(out_socket, (struct sockaddr *) &server_address, sizeof(struct sockaddr_in)) < 0)
{
fprintf(stderr,"connect try %i failed : ",i);
perror("");
fflush(NULL);
i++;
}
printf("Connection success : %i try\n",i);
fflush(NULL);
if( close(out_socket) < 0 ) {
perror("close error");
exit(1);
}
printf("Connection closed\n");
fflush(NULL);
fprintf(stderr,"last error : ");
perror(""); return 0;
}
Quoted 8 lines · Videau Brice
Hello,
While writing some client server application in c, we noticed a strange behavior : if we try to connect endlessly to a given local port where nobody is listening, and if the port is >= to 32768, after several thousands tries ( Connection refused ) connect will return 0. This behavior is not exhibited when port is < 32768.
Hello Brice
Yes, it's quite possible your attempts are hitting themselves.
Hint :
cat /proc/sys/net/ipv4/ip_local_port_range
When you connect(), TCP stack automatically chose a source port and bind your outgoing socket. If the chosen port happens to be 35489 (your 'destination' port), then the connect succeeds (you're connected to yourself), as specified by TCP specs.
Quoted 30 lines
We confirmed this behavior in kernel 2.6.17-10, 2.6.18-1, 2.6.8, on x86 and 2.4.21-32 on ia64, on several hardware configurations. Distribution is debian or ubuntu.
Attached is a source file that demonstrate this behavior. ./a.out
port_numberSample execution :
./a.out 35489
Out port : 35489
connect try 1 failed : Connection refused
connect try 2 failed : Connection refused
connect try 3 failed : Connection refused
.....
connect try 6089 failed : Connection refused
connect try 6090 failed : Connection refused
Connection success : 6091 try
Connection closed
last error : Connection refusedIs this behavior to be expected?
Can it be disabled?Thanks in advance.
Regards,
Brice Videau
It all makes sense now!
Thank you very much.
Regards,
Brice
Quoted 62 lines · Eric Dumazet
Quoted 9 lines · Videau Brice
Hello,
While writing some client server application in c, we noticed a strange behavior : if we try to connect endlessly to a given local port where nobody is listening, and if the port is >= to 32768, after several thousands tries ( Connection refused ) connect will return 0. This behavior is not exhibited when port is < 32768.
Hello Brice
Yes, it's quite possible your attempts are hitting themselves.
Hint :
cat
/proc/sys/net/ipv4/ip_local_port_rangeWhen you
connect(), TCP stack automatically chose a source port and bind your outgoing socket. If the chosen port happens to be 35489 (your 'destination' port), then the connect succeeds (you're connected to yourself), as specified by TCP specs.Quoted 22 lines
We confirmed this behavior in kernel 2.6.17-10, 2.6.18-1, 2.6.8, on x86 and 2.4.21-32 on ia64, on several hardware configurations. Distribution is debian or ubuntu.
Attached is a source file that demonstrate this behavior. ./a.out
port_numberSample execution :
./a.out 35489
Out port : 35489
connect try 1 failed : Connection refused
connect try 2 failed : Connection refused
connect try 3 failed : Connection refused
.....
connect try 6089 failed : Connection refused
connect try 6090 failed : Connection refused
Connection success : 6091 try
Connection closed
last error : Connection refusedQuoted 9 lines
Is this behavior to be expected?
Can it be disabled?Thanks in advance.
Regards,
Brice Videau
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/