Jump to content

Failing to reopen socket?


MS3FGX
 Share

Recommended Posts

I am writing my first simple program making use of the Wifilib, and I have run into a problem.

 

Here is the code in question:

 

#define MAXPORT 200
#define STARTPORT 1

...

int sockfd, port;
struct sockaddr_in sain;
struct hostent *h;
int x, y;

// Static assignment of scan parameters
h = gethostbyname("rgc.homelinux.org");
x = STARTPORT;
y = MAXPORT;

// Create the socket
sain.sin_family = AF_INET;
sain.sin_addr = *((struct in_addr *)h->h_addr_list[0]);

printf("Start Port: %i\n", x);
printf("End Port: %i\n",y);
printf("--------------------------------\n");
printf("Now Scanning...\n");
for(port = x; port <= y; port++)
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
sain.sin_port = htons(port);
if(connect(sockfd, (struct sockaddr *)&sain, sizeof(sain)) == 0)
{
	printf("Port %i is open!\n",port);
	close(sockfd);
}

close(sockfd);
}
shutdown(sockfd,0);
close(sockfd);
printf("Done!\n");

 

Obviously, the video setup, connecting to the AP, etc, has been omitted here.

 

Anyway, this is a very simple port scanner. As you can see, this simply increments the port variable, creates a socket, sees if it gets an answer, and if it does, prints a little message.

 

If I take this code and build it on my Linux machine, it scans the target fine. But, when I am running it on the DS, it scans up to the first open port on the target, and then stops. Well not exactly stops, but after the first hit the loop takes something like 30 seconds to go though before it moves on to the next port. I have not waited around to see if it actually picks up on the next open port or not (the target, my external IP, only has ports 22 and 80 open).

 

I am no expert with sockets, much less DS programming, but I assume what is happening here is that once the socket actually connects up to the target once, it is refusing to be reopened against the new port.

 

I thought perhaps close() was behaving differently here, so I tried with shutdown() in it's place, but as I recall, that only made matters worse.

 

Anyone know what the problem might be?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...