When i put the target IP as 127.0.1.1, the program is working fine, can catch blocked & open ports. But when i try to scan remotely, i get connection timed out! Can you tell me why?
Here is my code - Look at between where i put astriks - at the bottom:
#include<iostream>
#include<string>
#include<sstream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<errno.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
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;
//call get addrinfo()
getAddrIn();
//ask port range from user
cout<<"Enter Starting Port: ";
cin>>startingPort;
cout<<"Enter Ending Port: ";
cin>>endingPort;
cout<<endl;
cout<<"Start Scanning: "<<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;
}
//********************************************************************
//add a specific port, you want to connect to, for the remote host
struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;
specifyPort->sin_port = htons(startingPort);
//make connection to host IP/Port specified
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";
else if(errno == ETIMEDOUT)
cout<<"The attempt to connect TIMED OUT before a connection was made."<<endl;
else
cout<<"Error: "<<strerror(errno)<<endl;
}else{
cout<<"Port "<<startingPort<<" is OPEN.\n";
}
//**********************************************************************
close(socketfd);
//move to the next port in the specified range
startingPort++;
}
//deallocate memory
delete[] remoteIP;
//free linkedlist of struct addrinfo *results
freeaddrinfo(results);
return 0;
}
This is my second project, i should finish it as soon as possible.