943,691 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 962
  • C++ RSS
Apr 12th, 2009
0

Simple Port Scanner - What Do You Think?

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 46
Solved Threads: 0
Light Poster
f.ben.isaac is offline Offline
34 posts
since Oct 2008
Apr 12th, 2009
0

Re: Simple Port Scanner - What Do You Think?

Not bad..Good example for beginners..Whats 'she' do anyway..Checking for ports that's been opened? or what..(sorry for my bad English)
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Apr 12th, 2009
0

Re: Simple Port Scanner - What Do You Think?

Click to Expand / Collapse  Quote originally posted by cikara21 ...
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.
Reputation Points: 46
Solved Threads: 0
Light Poster
f.ben.isaac is offline Offline
34 posts
since Oct 2008
Apr 12th, 2009
0

Re: Simple Port Scanner - What Do You Think?

Well..Since you already know which one is recommended..Good job dude..
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Confusion with conversion of type1 to type2
Next Thread in C++ Forum Timeline: segfault... WHY!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC