Re: BUG: Unusual TCP Connect() results.
From: Alastair Poole
Date: Fri Jun 10 2005 - 10:58:11 EST
Michal Schmidt wrote:
What is the last version that works as expected for you?
Kernel 2.6.11, though I can't check others due to bandwidth
restrictions. 2.6.11.10 through 2.6.12-rc6 all have the same issue.
Are you testing your scanner only on localhost? Maybe you are just
lucky and connect your TCP socket to itself.
This problem only occurs on localhost. I don't think it is mere luck,
these are too frequent and strange for this.
What are these asterisks doing there? Next time when you copy&paste
code, please make sure you don't mangle it.
I apologise for this, included this time is an attatchment in the hope
you are able to reproduce the strange results I and others have been
able to. Try testing multiple times, I find that 18 out of 20 runs
produces the same type of results.
sincerely
Alastair Poole
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <string.h>
int
main (int argc, char **argv)
{
int sd, result, server_port;
struct hostent *he;
struct sockaddr_in servaddr;
printf ("Test TCP/IP port scanner:\n");
if (argc != 2)
{
printf ("Usage: %s host\n", argv[0]);
exit (1);
}
if ((he = gethostbyname (argv[1])) == NULL)
{
perror ("gethostbyname()");
exit (1);
}
printf ("Scanning %s\n", argv[1]);
for (server_port = 0; server_port < 65536; server_port++)
{
if ((sd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
perror ("socket()");
exit (1);
}
bzero (&servaddr, sizeof servaddr);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons (server_port);
servaddr.sin_addr = *((struct in_addr *) he->h_addr);
result = connect (sd, (struct sockaddr *) &servaddr, sizeof servaddr);
if (result != -1)
{
printf ("open port: %d\n",server_port);
}
close (sd);
}
return result;
}