Simple Port Scanner - What Do You Think?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 33
Reputation: f.ben.isaac is an unknown quantity at this point 
Solved Threads: 0
f.ben.isaac f.ben.isaac is offline Offline
Light Poster

Simple Port Scanner - What Do You Think?

 
0
  #1
Apr 12th, 2009
How good this code in terms of clarity, readability, and overall design? Easy to understand them or not?

Note: i put system("pause") which is not recommended. cin.get() is a better one in case you didn't know.

First: Port Scanner

  1. /**program description:
  2.  *
  3.  * this is a simple port scanner
  4.  * based on TCP connection. You
  5.  * should insert IP and starting
  6.  * port as well as ending port.
  7.  * IPv4 is accepted only. Works
  8.  * on Windows OS
  9.  *
  10.  * exercise: do input validation
  11.  *
  12.  * Author: X-Shab
  13.  * Copyright 2008 - 2009
  14.  *
  15.  */
  16.  
  17. #include<iostream>
  18. #include<string>
  19. #include<sstream>
  20. #include<Ws2tcpip.h> //used instead of "Winsock2.h"
  21. using namespace std;
  22.  
  23. struct addrinfo hints; //input connection type and ip type
  24. struct addrinfo *fullInfo; //holds full address information of the target host
  25. string ip;
  26. string port;
  27.  
  28. void initiateWinsockFacility()
  29. {
  30. WORD wVersionRequested;
  31. WSADATA wsaData;
  32. int error;
  33.  
  34. wVersionRequested = MAKEWORD(2, 2);
  35.  
  36. error = WSAStartup(wVersionRequested, &wsaData);
  37.  
  38. if (error != 0)
  39. {
  40. cout<<"WSAStartup failed with error: "<<error<<endl;
  41.  
  42. system("pause");
  43. exit(-1);
  44. }
  45. }
  46.  
  47. //get full information about the connection to be made - fullInfo carries the output
  48. void getFullAddressInformation(const char *ipAddress, const char *portNumber)
  49. {
  50. int status = getaddrinfo(ipAddress, portNumber, &hints, &fullInfo);
  51.  
  52. if(status != 0)
  53. {
  54. cout<<"getaddrinfo() error: "<< gai_strerror(status) <<endl;
  55. WSACleanup();
  56. exit(-1);
  57. }
  58. }
  59.  
  60. string convertIntToString(int number)
  61. {
  62. string stringOutput;
  63.  
  64. stringstream ss;
  65. ss << number;
  66. ss >> stringOutput;
  67.  
  68. return stringOutput;
  69. }
  70.  
  71. int convertStringToInt(string stringStream)
  72. {
  73. int integerOutput;
  74.  
  75. stringstream ss;
  76. ss << stringStream;
  77. ss >> integerOutput;
  78.  
  79. return integerOutput;
  80. }
  81.  
  82. int convertConstCharToInt(const char *characters)
  83. {
  84. int integerOutput;
  85. string stringFromConstChar(characters);
  86.  
  87. stringstream ss;
  88. ss << stringFromConstChar;
  89. ss >> integerOutput;
  90.  
  91. return integerOutput;
  92. }
  93.  
  94. int main()
  95. {
  96. initiateWinsockFacility();
  97.  
  98. int endingPort;
  99. int socketFileDescriptor;
  100.  
  101. system("cls");
  102.  
  103. memset(&hints, 0, sizeof(hints));
  104.  
  105. hints.ai_family = AF_INET;
  106. hints.ai_socktype = SOCK_STREAM;
  107.  
  108. cout<<"Please enter your target IP: ";
  109. cin>>ip;
  110.  
  111. cout<<"Enter Starting Port: ";
  112. cin>>port;
  113.  
  114. cout<<"Enter Ending Port: ";
  115. cin>>endingPort;
  116.  
  117. cout<<"Start Checking for Open Ports: "<<endl;
  118.  
  119. int incrementingPort = convertStringToInt(port);
  120.  
  121. while(incrementingPort <= endingPort)
  122. {
  123. getFullAddressInformation(ip.c_str(), port.c_str());
  124.  
  125. socketFileDescriptor = socket(fullInfo->ai_family, fullInfo->ai_socktype, fullInfo->ai_protocol);
  126.  
  127. if(socketFileDescriptor == INVALID_SOCKET )
  128. {
  129. cout<<"Error: failed to create a socket.\n";
  130.  
  131. freeaddrinfo(fullInfo);
  132. WSACleanup();
  133.  
  134. system("pause");
  135. exit(-1);
  136. }
  137.  
  138. int connectStatus = connect(socketFileDescriptor, fullInfo->ai_addr, fullInfo->ai_addrlen);
  139.  
  140. if(connectStatus == SOCKET_ERROR)
  141. {
  142. //check type of error you get
  143. if(WSAGetLastError() == 10060)
  144. {
  145. cout<<"Connection Timed Out - Please check your IP"<<endl;
  146.  
  147. closesocket(socketFileDescriptor);
  148. freeaddrinfo(fullInfo);
  149. WSACleanup();
  150.  
  151. system("pause");
  152. exit(-1);
  153. }
  154.  
  155. cout<<"Port No. "<< port <<" is Closed"<<endl;
  156.  
  157. }else{
  158. cout<<"Port No. "<< port <<" is Open"<<endl;
  159. }
  160.  
  161. closesocket(socketFileDescriptor);
  162. freeaddrinfo(fullInfo);
  163.  
  164. incrementingPort++;
  165.  
  166. port = convertIntToString(incrementingPort);
  167. }
  168.  
  169. /**not needed since its been implemented
  170. * on the last loop above
  171. *
  172. * closesocket(socketFileDescriptor);
  173. * freeaddrinfo(fullInfo);
  174. *
  175. */
  176.  
  177. WSACleanup();
  178. system("pause");
  179.  
  180. return 0;
  181. }
Last edited by f.ben.isaac; Apr 12th, 2009 at 7:52 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Simple Port Scanner - What Do You Think?

 
0
  #2
Apr 12th, 2009
Not bad..Good example for beginners..Whats 'she' do anyway..Checking for ports that's been opened? or what..(sorry for my bad English)
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 33
Reputation: f.ben.isaac is an unknown quantity at this point 
Solved Threads: 0
f.ben.isaac f.ben.isaac is offline Offline
Light Poster

Re: Simple Port Scanner - What Do You Think?

 
0
  #3
Apr 12th, 2009
Originally Posted by cikara21 View Post
Not bad..Good example for beginners..Whats 'she' do anyway..Checking for ports that's been opened? or what..(sorry for my bad English)
Yes it checks which ports are opened and which are closed...You specify starting port and ending port.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Simple Port Scanner - What Do You Think?

 
0
  #4
Apr 12th, 2009
Well..Since you already know which one is recommended..Good job dude..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC