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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include<iostream>
#include<string>
#include<sstream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.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 *pcName = new char[40]; //holds your pc name
string tempPort; //holds ports temporarly
int startingPort; //stores the starting range of port number
int status; //receives the status of your pc address
char currentPort[10]; //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;
}
//call host (local machine) name
void callHostName()
{
size_t pcNameSize; //holds size of the name
int hostNameStatus; //holds status of the function whether it succeeded or failed
hostNameStatus = gethostname(pcName, pcNameSize); //gethostname grabs your pc name & pass it to pcName
//make sure function host name returned successfully
if(hostNameStatus == -1 )
{
cout<<"Error: could not locate your host name\n";
exit(1);
}
//print machine name
cout<<"Your Machine Name: ";
cout<<pcName<<endl;
cout<<endl;
//deallocate memory
//delete[] pcName; - i put it inside main()
}
//getaddrinfe locates your machine, more specific
//details of your host address is returned to results.
void getAddrIn()
{
status = getaddrinfo(pcName, currentPort, &hints, &results);
if(status != 0)
{
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
}
//get local IP which is usually version 4
void grabLocalIP()
{
//carries your local IP
char ipString[INET6_ADDRSTRLEN];
struct addrinfo *p;
cout<<"IP Address for "<<pcName<<" is of type ";
//print all your local IP Addresses
for(p = results; p != NULL; p = p->ai_next)
{
void *addr;
string ipVer;
if(p->ai_family == AF_INET)
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipVer = "IPv4";
}else{
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipVer = "IPv6";
}
inet_ntop(p->ai_family, addr, ipString, sizeof ipString);
cout<<""<<ipVer<<": ";
printf("%s\n", ipString);
cout<<endl;
}
}
//convert a port from int to *char, so it can be passed into getaddrinfo()
//call this function before the loop starts
void convertPortsToString()
{
//convert startingPort to string. After that, convert the string into *char...
//string tempPort; make it static
stringstream out;
out<<startingPort;
tempPort = out.str(); //tempPort carries startingPort in string format
//convert tempPort to *char - currentPort going to be passed into getaddrinfo()
//char currentPort[10]; - i made it static
strcpy(currentPort, tempPort.c_str());
}
//convert a port from int to *char, so it can be passed into getaddrinfo()
//call this function at the bottom of the loop
void convertIncrementedPortsToString()
{
//you have to declare out in every loop, otherwise the
//incremented ports will concatenated with the previous ports.
stringstream out;
out<<startingPort;
tempPort = out.str(); //tempPort carries startingPort in string format
strcpy(currentPort, tempPort.c_str()); //convert the string to *char
}
//Run the program
int main()
{
int endingPort; //stores the ending rage of port number
system("clear");
//tell user what program does
progDesc();
//grab the name of the local machine (host)
callHostName();
//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;
//call getaddrinfo() to output to "results"
//so you can grab the local IP from results
getAddrIn();
//grab your machine local IP
grabLocalIP();
//ask port range from user
cout<<"Enter Starting Port: ";
cin>>startingPort;
cout<<"Enter Ending Port: ";
cin>>endingPort;
cout<<endl;
cout<<"Start Checking: "<<endl;
//convert a port from int to *char, so it can be passed into getaddrinfo()
convertPortsToString();
//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;
}
//connect to your own IP address in every loop with
//a new port, connect() will associate your socket
//with the "current port number & your local IP address".
int connectStatus;
connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);
if(connectStatus == -1 )
{
cout<<"Port "<<currentPort<<" is Closed or Blocked.\n";
}else{
cout<<"Port "<<currentPort<<" is OPEN.\n";
}
close(socketfd);
//move to the next port in the specified range
startingPort++;
/***convert the incremented port to *char***/
convertIncrementedPortsToString();
}
//deallocate memory
delete[] pcName;
//free linkedlist of struct addrinfo *results
freeaddrinfo(results);
return 0;
}
|