Hello, first of all i'm glad to be a member here. Nice to be with you!

I wrote a simple dum port scanner. For practicing purposes. It worked fine under linux, i ported to windows, i linked it to winsock libraries. I keep getting errors, such as

C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found|

Anyway here is my code if you want to see it:

#include<iostream>
#include<string>
#include<sstream>
#include"WinSock2.h"
using namespace std;

struct addrinfo hints;      	//fill your host info - hints is input
struct addrinfo *results;	//gets your host info - results is output
char *remoteIP = new char[40];  //holds inserted remote IP
string tempPort;                //holds ports temporarly
int startingPort;               //stores the starting range of port number
int status;                     //receives the status of your pc address
int currentPort;           //holds current port


//program description
void progDesc()
{
     cout<<"This is a simple port scanner, scan range of\n";
     cout<<"ports on your local machine...\n"<<endl;

}


//getaddrinfe locates your machine, more specific
//details of your host address is returned to results.
void getAddrIn()
{
	status = getaddrinfo(remoteIP, NULL , &hints, &results);

	if(status != 0)
	{
		fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
		exit(1);
	}
}


//Run the program
int main()
{
	int endingPort;                 //stores the ending rage of port number

    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

	/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
    wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        return 1;
    }

	system("clear");

	//tell user what program does
	progDesc();

	//set size of hints to zero
	memset(&hints, 0, sizeof hints);

	//fill some of your host address info
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	//ask for remote IP
	cout<<"Please enter your target IP: ";
	cin>>remoteIP;

	getAddrIn();

	//ask port range from user
	cout<<"Enter Starting Port: ";
	cin>>startingPort;

	cout<<"Enter Ending Port: ";
	cin>>endingPort;

	cout<<endl;

	cout<<"Start Checking: "<<endl;


	//check the status
	while(startingPort <= endingPort)
	{
		//call getaddrinfo()
		getAddrIn();

		//create a socket.
		int socketfd;
		socketfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);

		if(socketfd == -1 )
		{
			cout<<"Error: failed to create a socket.\n";
			return 2;
		}

		struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;

		specifyPort->sin_port = htons(startingPort);

		int connectStatus;
		connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);

		if(connectStatus == -1 )
		{
			if(errno == ECONNREFUSED)
				cout<<"Port "<<startingPort<<" is Closed or Blocked.\n";
			//Alternatively, you can do:
			//cout << "connect failed; reason = " << strerror(errno) <<endl;
		}else{
			cout<<"Port "<<startingPort<<" is OPEN.\n";
		}


		closesocket(socketfd);

		//move to the next port in the specified range
		startingPort++;

	}

	//deallocate memory
	delete[] remoteIP;

	//free linkedlist of struct addrinfo *results
	freeaddrinfo(results);
    WSACleanup();
	return 0;
}

Errors:

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\xlocale|342|warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|7|error C2079: 'hints' uses undefined struct 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|33|error C3861: 'gai_strerror': identifier not found|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|53|error C2228: left of '.ai_family' must have class/struct/union|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|54|error C2228: left of '.ai_socktype' must have class/struct/union|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_family' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_socktype' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_protocol' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2227: left of '->ai_addr' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addr' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addrlen' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|99|error C2065: 'ECONNREFUSED' : undeclared identifier|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|119|error C3861: 'freeaddrinfo': identifier not found|
||=== Build finished: 19 errors, 1 warnings ===|

PLZ HELP.... I ALREADY DID ALL I CAN. I'm about to give up....

Comments
thanks for using code tags :)

I managed to make the code compile free of errors and warnings, here are the changes I made.

#include<iostream>
#include<string>
#include<sstream>
#include<winsock2.h>
[B]#include<ws2tcpip.h>[/B]

[B]#pragma comment(lib, "Ws2_32.lib")[/B]

using namespace std;

struct addrinfo hints;      	//fill your host info - hints is input
struct addrinfo *results;	//gets your host info - results is output
char *remoteIP = new char[40];  //holds inserted remote IP
string tempPort;                //holds ports temporarly
int startingPort;               //stores the starting range of port number
int status;                     //receives the status of your pc address
int currentPort;           //holds current port


//program description
void progDesc()
{
     cout<<"This is a simple port scanner, scan range of\n";
     cout<<"ports on your local machine...\n"<<endl;

}


//getaddrinfe locates your machine, more specific
//details of your host address is returned to results.
void getAddrIn()
{
	status = getaddrinfo(remoteIP, NULL , &hints, &results);

	if(status != 0)
	{
		fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
		exit(1);
	}
}


//Run the program
int main()
{
	int endingPort;                 //stores the ending rage of port number

    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

	/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
    wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        return 1;
    }

	system("[B]cls[/B]");

	//tell user what program does
	progDesc();

	//set size of hints to zero
	memset(&hints, 0, sizeof hints);

	//fill some of your host address info
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	//ask for remote IP
	cout<<"Please enter your target IP: ";
	cin>>remoteIP;

	getAddrIn();

	//ask port range from user
	cout<<"Enter Starting Port: ";
	cin>>startingPort;

	cout<<"Enter Ending Port: ";
	cin>>endingPort;

	cout<<endl;

	cout<<"Start Checking: "<<endl;


	//check the status
	while(startingPort <= endingPort)
	{
		//call getaddrinfo()
		getAddrIn();

		//create a socket.
		[B]SOCKET[/B] socketfd;
		socketfd = (int) socket(results->ai_family, results->ai_socktype, results->ai_protocol);

		if(socketfd == -1 )
		{
			cout<<"Error: failed to create a socket.\n";
			return 2;
		}

		struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;

		specifyPort->sin_port = htons(startingPort);

		int connectStatus;
		connectStatus = connect(socketfd, results->ai_addr, [B](int)[/B]results->ai_addrlen);

		if(connectStatus == -1 )
		{
			if(errno == [B]WSAECONNREFUSED[/B])
				cout<<"Port "<<startingPort<<" is Closed or Blocked.\n";
			//Alternatively, you can do:
			//cout << "connect failed; reason = " << strerror(errno) <<endl;
		}else{
			cout<<"Port "<<startingPort<<" is OPEN.\n";
		}


		closesocket(socketfd);

		//move to the next port in the specified range
		startingPort++;

	}

	//deallocate memory
	delete[] remoteIP;

	//free linkedlist of struct addrinfo *results
	freeaddrinfo(results);
    WSACleanup();
	return 0;
}

Hope this helps :]

I did not do this #pragma comment(lib, "Ws2_32.lib")

I went to the linker & put the path that leads to Ws2_32.lib....

THANKS

This article has been dead for over six months. Start a new discussion instead.