Winsock none-blocking sockets. Not detecting if port is open or not.
Nov 18, 2013 at 12:17am UTC
So today I was a bit bored and was thinking I would have a look at the Winsock.
And I made a port scanner and it works but it was so slow I try to make it faster some answers turned up on Google and I ended up with this.
http://stackoverflow.com/questions/16408057/improving-port-scanner-performance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// Fast but returns false / bad results
DWORD WINAPI ScannTargetNoBlock(Target_IP* target)
{
u_long on = 1;
timeval tv = {0, 1000}; //timeout value in microseconds
fd_set fds;
FD_ZERO(&fds);
cout << "// STARTING SCANN OF TARGET " << target->sTargetIP << endl;
int nTemp = target->nStart_Port;
while ( nTemp <= target->nEnd_Port )
{
SOCKET sock;
SOCKADDR_IN sock_Info;
sock = socket( AF_INET, SOCK_STREAM, 0 );
if ( sock == INVALID_SOCKET )
{
cout << "FAILED! To create socket. WSA Error Code: " ;
cout << WSAGetLastError() << endl;
cout << "WIN Error Code: " << GetLastError() << endl;
WSACleanup();
system("PAUSE" );
return 1;
}
FD_SET(sock, &fds);
ioctlsocket(sock, FIONBIO, &on);
sock_Info.sin_family = AF_INET;
sock_Info.sin_port = htons(nTemp);
sock_Info.sin_addr.s_addr = inet_addr( target->sTargetIP.c_str() );
connect( sock, (SOCKADDR*)&sock_Info, sizeof (sock_Info));
int err = select(sock, &fds, &fds, &fds, &tv);
if ( err != SOCKET_ERROR && err != 0)
cout << target->sTargetIP << ":" << nTemp << " - open" << endl;
else
cout << target->sTargetIP << ":" << nTemp << " - closed" << endl;
nTemp++;
}
return 0;
}
My previous method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
// Working but SO SLOW
DWORD WINAPI ScannTarget(Target_IP* target)
{
cout << "// STARTING SCANN OF TARGET " << target->sTargetIP << endl;
int nTemp = target->nStart_Port;
while ( nTemp <= target->nEnd_Port )
{
SOCKET sock;
SOCKADDR_IN sock_Info;
sock = socket( AF_INET, SOCK_STREAM, 0 );
if ( sock == INVALID_SOCKET )
{
cout << "FAILED! To create socket. WSA Error Code: " ;
cout << WSAGetLastError() << endl;
cout << "WIN Error Code: " << GetLastError() << endl;
WSACleanup();
system("PAUSE" );
return 1;
}
sock_Info.sin_family = AF_INET;
sock_Info.sin_port = htons(nTemp);
sock_Info.sin_addr.s_addr = inet_addr( target->sTargetIP.c_str() );
if ( connect( sock, (SOCKADDR*)&sock_Info, sizeof (sock_Info)) != SOCKET_ERROR )
{
cout << target->sTargetIP << ":" << nTemp << " - open" << endl;
} else
cout << target->sTargetIP << ":" << nTemp << " - closed" << endl;
closesocket(sock);
nTemp++;
}
return 0;
}
Am I doing something wrong here? As the only IP I can scan witch returns correct result on port scan is 127.0.0.1.
Any other IP local or public IP will give wrong results when using ScannTargetNoBlock(Target_IP* target)
Any ideas?
Cheers
WetCode
Last edited on Nov 18, 2013 at 12:18am UTC
Nov 18, 2013 at 5:35am UTC
Solution:
1 2 3 4
u_long on = 1;
timeval tv = {0, 900000}; //timeout value in microseconds
fd_set fds;
FD_ZERO(&fds);
Topic archived. No new replies allowed.