Linux C++ socket programming question

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
EDIT: Sorry that the code isn't indented or anything, it was when I posted it. I tried the "Attach Code" button but it gave me an error every time I tried to attach code.
Code:
 tags didn't work either. Is there something I should be using?

Ok, so my first post somehow didn't post. This is the short version. I have to write a port scanner for class. Very basic. I am a Java programmer but this has to be in C++ and I am new to socket programming to forgive me if the answer is simple. After reading Beej's Guide I came up with this:


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <arpa/inet.h>

int status;
int i;
int s;
int c;
int j;
struct sockaddr_in ip4addr;
char Str[16];

int main(int argc, char *argv[])
{
	for(i = 1; i<=65535; i++)
	{
		ip4addr.sin_family = AF_INET;
		ip4addr.sin_port = htons(i);
		inet_pton(AF_INET, "131.204.36.98", &ip4addr.sin_addr);
		
		if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1)
		{
			//printf("Error creating socket");
		}
		else
		{
			//Set Non-blocking
	      		int opts;
      			opts = fcntl(s, F_GETFL);
			if (1==1) opts = (opts | O_NONBLOCK);
			else opts = (opts & ~O_NONBLOCK);
			fcntl(s, F_SETFL, opts);

			for(j = 0; j<1000; j++)
			{
				if ((c = connect(s, (struct sockaddr*)&ip4addr, sizeof ip4addr)) == -1)
				{
					//if(j==9999){printf("closed: %d\n", i);}
				}
				else
				{
					printf("open: %d\n", i);
					break;
				}
			}
		}
	}
}


The non-blocking section was causing it not to catch open ports hence the loop. This scanner runs fast but is only roughly 97% accurate (sometimes it misses ports). So I did some more reading and looking and came up with this:


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <arpa/inet.h>
#include <unistd.h>

int status;
int i;
int s;
int c;
int j;
struct sockaddr_in ip4addr;
struct hostent *hostaddr;

int main(int argc, char *argv[])
{
	for(i = 1; i<=65535; i++)
	{
		s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		memset( &ip4addr, 0, sizeof(ip4addr));
		ip4addr.sin_family = AF_INET;
		ip4addr.sin_port = htons(i);
		inet_pton(AF_INET, "131.204.2.251", &ip4addr.sin_addr);

		if ((c = connect(s, (struct sockaddr*)&ip4addr, sizeof ip4addr)) != -1)
		{
			printf("open: %d\n", i);
		}
		close(s);
	}
}

This one is 100% solid, but oh my GOSH is it slow. What the heck do I need to do to speed up the second version? Any help is greatly appreciated. Thank you all in advance.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
C++ and sockets... been awhile for me, but what I assume is going on is that when you try to open a port you don't know whether it is successful or not until the request times out. Your non-blocking version wasn't waiting around to see if that happened, whereas the blocking version is.

That's my current theory anyway.
 

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
Originally posted by: Markbnj
C++ and sockets... been awhile for me, but what I assume is going on is that when you try to open a port you don't know whether it is successful or not until the request times out. Your non-blocking version wasn't waiting around to see if that happened, whereas the blocking version is.

That's my current theory anyway.

Yeah, that was my first guess as well I am just not entirely sure what the solution is. I spent a few more hours looking tonight and it looks like if I go back to the non-blocking option I can somehow use select() or poll() to set a timeout. If anyone knows how that would be great otherwise I am sure I will figure it out tomorrow when I have time to look at it again.

I have read there is a way to set an interrupt as well but since I already have a threaded version of this apparently the select() or poll() is the way to go since they can manage multiple simultaneous sockets.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Theory:

Use the blocking method on seperate threads for each socket.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,361
4,067
75
Originally posted by: Common Courtesy
Theory:

Use the blocking method on seperate threads for each socket.

Practice:

You'd have to limit it to a few thousand threads, and wait for the first to time out before proceeding, because a computer can't have too many sockets open at once. Maybe use 1024 threads and have a loop in each, counting by 1024.

Researching this, I saw a forum post elsewhere that said, for the non-blocking method, you should use select(), then determine if select returned because of a connection or because of a timeout. (No, I don't know how to do that.)
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Question:

Why do you need so many open sockets?

Note: My background is to use sockets for dedicated data transfers, not open web interfacing.
 

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
Originally posted by: Common Courtesy
Theory:

Use the blocking method on seperate threads for each socket.

I have a multi-threaded version and it runs about 2x as slow as this one currently does. After some reading apparently there is a lot of overhead switching back and forth between threads to check the timeout.

Originally posted by: Ken g6
Originally posted by: Common Courtesy
Theory:

Use the blocking method on seperate threads for each socket.

Practice:

You'd have to limit it to a few thousand threads, and wait for the first to time out before proceeding, because a computer can't have too many sockets open at once. Maybe use 1024 threads and have a loop in each, counting by 1024.

Researching this, I saw a forum post elsewhere that said, for the non-blocking method, you should use select(), then determine if select returned because of a connection or because of a timeout. (No, I don't know how to do that.)

Yeah, I am trying to figure out how to do that now. I will let you all know if it works.



Originally posted by: Common Courtesy
Question:

Why do you need so many open sockets?

Note: My background is to use sockets for dedicated data transfers, not open web interfacing.

This is a port scanner, I am checking every socket to see if it is open to steal your data. J/K this is for research.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: O.P.
The non-blocking section was causing it not to catch open ports hence the loop.

It sounds like without 'blocking', the connect() times out (as expected, since its a handshake operation). If you're serious about making a good TCP port scanner (UDP is a LOT easier, of course), try sending an IP-layer packet that is formatted to look like a TCP SYN packet instead. Any host/port pair that responds with a SYN+ACK is open. Be careful not to inadvertently SYN-flood yourself.

http://en.wikipedia.org/wiki/T...ssion_Control_Protocol
http://linux.die.net/man/7/ip <--- check out RAW sockets
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |