HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Scanning for open ports

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
openscanningportsfor

Problem

I made what started out as a web browser but now is a scanner for open ports. I wanted to know if you think this is a realistic or a unreliable tool for port testing. I found some open ports on my computer and was able to send data through them.

```
#include
#include
#include
#include
#include
using namespace std;
#define SCK_VERSION2 0x0202
#define DEFAULT_BUFLEN 2000
#define DEFAULT_PORT 27015

namespace Globals{
u_short PORT;
int i = 135;
}
using namespace Globals;

int sck() {
//----------------------
// Declare and initialize variables.
WSADATA wsaData;
int iResult;

SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;

char name[500] = "";
char ipADDRESS[500] = "";
char sPORT[500] = "";

sockaddr_in sName;
int sNameSize = sizeof(sName);

char const* sendbuf = "GET HTTP/1.1\r\n"
"Host: 97-80-226-196.dhcp.leds.al.charter.com\r\n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Keep-Alive: 300\r\n"
"Connection: keep-alive\r\n"
"Pragma: no-cache\r\n"
"DNT: 1"
"Cache-Control: no-cache\r\n\r\n";
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN; //23.214.132.132 GoDaddy.com
int WSAERROR = WSAGetLastError();
//----------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

//----------------------
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Connect

Solution

Servers can keep sockets open indefinitely.

// Receive until the peer closes the connection


So this will hang your application. Try connecting to port 23 see what happens. Even web-servers can keep the port open longer for chaining (wrong word) requests and not requiring the cost of re-connecting.
Global variables!

PORT = i;
        sck();


Global variables. What. Don't do it. Functions should be self contained (anything that relies on external state is likely to get something wrong as somebody else changes that state when you least expect it. Pass the port as a parameter.

sck(i);


Initialization

iResult = WSAStartup(MAKEWORD(2,2), &wsaData);


This only needs to be done once in the application. So you can move this out of the sck() function.
Don't need to close a connection that was never opened.

iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if ( iResult == SOCKET_ERROR) {
    closesocket (ConnectSocket);  // Its not open at this point.


Not all error's are errors.

iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );


See man send: If send() returns -1 as an error you need to check errno to check the actual error. Not all errors need to be terminal. Eg EINTR is not a problem.
Don't wait for the server to close the conection.

Code Snippets

// Receive until the peer closes the connection
PORT = i;
        sck();
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if ( iResult == SOCKET_ERROR) {
    closesocket (ConnectSocket);  // Its not open at this point.
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );

Context

StackExchange Code Review Q#101250, answer score: 3

Revisions (0)

No revisions yet.