943,163 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 395
  • C++ RSS
Mar 12th, 2010
0

ip scanner performance issues

Expand Post »
I have been coding an IP scanner for god knows what reason. The program kind of works. It does find active computers on the network but for some reason it does not find my computer using its network ip or its internal ip (127.0.0.1). Also it can not find any of my fios set top boxes. Ping is able to find every computer or device that is active on the network. Another problem I have been seeing with the program is that it finds the two active device that it is able to connect with right away then pause for a long long time (a minute or three) before printing out information about not being able to connect to ip x.x.x.x. I think part of this reason is due to not getting a lock to print to the console but i'm not sure this is the first time I'm using threads. I looked in the task manager and it shows 0% usage of the CPU when the program stalls. Could someone take a look at my code and suggest and changes or enhancements to it.

Thanks in advance

C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <pthread.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8.  
  9. void ipStrToInt(unsigned int (&ip)[4], std::string);
  10. const std::string get_ip();
  11. const std::string inttostr(const unsigned int);
  12. void *scan(void *ptr);
  13.  
  14. unsigned int IP[4] = {0,0,0,0};
  15. unsigned int beginIP[4] = {0,0,0,0};
  16. unsigned int endIP[4] = {0,0,0,0};
  17. unsigned int startPort=0, stopPort=0;
  18. unsigned int thread_cnt = 0, upIPcnt = 0;
  19. bool done = false, first = true;
  20.  
  21. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  22. pthread_mutex_t cntMutex = PTHREAD_MUTEX_INITIALIZER;
  23. pthread_mutex_t prtMutex = PTHREAD_MUTEX_INITIALIZER;
  24.  
  25.  
  26. int main(void) {
  27. std::string startIP, stopIP;
  28. std::string mPorts;
  29.  
  30. std::cout << "Enter IP Range <start end> ";
  31. std::cin >> startIP >> stopIP;
  32.  
  33. while(1) {
  34. std::cout << "Scan Multiple Ports (Y/N) ";
  35. std::cin >> mPorts;
  36. if(mPorts == "Y" || mPorts == "y") {
  37. std::cout << "Enter port range <start end> ";
  38. std::cin >> startPort >> stopPort;
  39. break;
  40. } else if(mPorts == "N" || mPorts == "n") {
  41. std::cout << "Setting scan port to default (80)\n";
  42. startPort = stopPort = 80;
  43. break;
  44. } else {
  45. std::cout << "Bad Input try again";
  46. }
  47. }
  48.  
  49. // Parse begin IP
  50. ipStrToInt(beginIP, startIP);
  51. // Parse end IP
  52. ipStrToInt(endIP, stopIP);
  53.  
  54. // Set IP to starting value
  55. IP[0] = beginIP[0];
  56. IP[1] = beginIP[1];
  57. IP[2] = beginIP[2];
  58. IP[3] = beginIP[3];
  59.  
  60. const unsigned int max_threads = 100;
  61. pthread_t thread[100];
  62.  
  63. while(thread_cnt < max_threads && !done) {
  64. pthread_create(&thread[thread_cnt], NULL, &scan, &thread_cnt);
  65.  
  66. // Lock thread_cnt before incrementing
  67. pthread_mutex_lock(&cntMutex);
  68. thread_cnt++;
  69. pthread_mutex_unlock(&cntMutex);
  70. }
  71.  
  72. // wait for threads to finish before cleaning up
  73. for(unsigned int x=0; x < thread_cnt; ++x) {
  74. pthread_join(thread[x], NULL);
  75. }
  76.  
  77. std::cout << "Finished\nNumber of Active IPs: " << upIPcnt << std::endl;
  78.  
  79. pthread_mutex_destroy(&mutex);
  80. pthread_mutex_destroy(&cntMutex);
  81. pthread_mutex_destroy(&prtMutex);
  82. pthread_exit(NULL);
  83.  
  84. return 0;
  85. }
  86.  
  87. void *scan(void *arg) {
  88. unsigned int port = startPort;
  89. std::string curIP = "";
  90. int sock, cnx;
  91. struct sockaddr_in host;
  92.  
  93. sock = socket(AF_INET, SOCK_STREAM, 0);
  94. if(sock < 0) {
  95. std::cout << "ERROR: Could not open socket\n";
  96. } else {
  97. while(1) {
  98. pthread_mutex_lock(&mutex);
  99. curIP = get_ip(); // get the next ip address
  100. pthread_mutex_unlock(&mutex);
  101.  
  102. if(curIP.empty()) {
  103. done = true;
  104. break;
  105. }
  106.  
  107. host.sin_family = AF_INET;
  108. host.sin_addr.s_addr = inet_addr(curIP.c_str());
  109.  
  110. if(!curIP.empty()) {
  111. while(port <= stopPort) {
  112. host.sin_port = htons(port);
  113.  
  114. cnx = connect(sock, (struct sockaddr*)&host, sizeof(host));
  115. pthread_mutex_lock(&prtMutex);
  116. if(cnx < 0) {
  117. std::cout << inet_ntoa(host.sin_addr) << " ... connection failed. Host possibly down\n";
  118. } else {
  119. std::cout << inet_ntoa(host.sin_addr) << " ... Host appears to be up.\n";
  120.  
  121. pthread_mutex_lock(&cntMutex);
  122. upIPcnt++;
  123. pthread_mutex_unlock(&cntMutex);
  124. }
  125. pthread_mutex_unlock(&prtMutex);
  126. port++;
  127. }
  128. }
  129. }
  130. }
  131. close(sock);
  132.  
  133. // lock thread_cnt before decrementing
  134. pthread_mutex_lock(&cntMutex);
  135. thread_cnt--;
  136. pthread_mutex_unlock(&cntMutex);
  137.  
  138. pthread_exit((void*)0);
  139. }
  140.  
  141. void ipStrToInt(unsigned int (&ip)[4], std::string strIP) {
  142. int found=0, cnt=0;
  143. std::string temp;
  144. temp = strIP;
  145.  
  146. found = temp.find_first_of(".");
  147.  
  148. while(found != std::string::npos && cnt < 4) {
  149. if(found > 0) {
  150. ip[cnt] = (unsigned)atoi(temp.substr(0,found).c_str());
  151. }
  152. temp = temp.substr(found+1);
  153. found = temp.find_first_of(".");
  154. cnt++;
  155. }
  156. if(temp.length() > 0 && cnt < 4) {
  157. ip[cnt] = atoi(temp.c_str());
  158. }
  159. }
  160.  
  161. // Works
  162. const std::string get_ip() {
  163. if(!first) {
  164. if(memcmp(&IP, &endIP, 4*sizeof(int)) != 0) {
  165. if(IP[3] == 255 || IP[3] == endIP[3]) {
  166. if(IP[2] == 255 || IP[2] == endIP[2]) {
  167. if(IP[1] == 255 || IP[1] == endIP[1]) {
  168. if(IP[0] <= 255 || IP[0] <= endIP[0]) {
  169. IP[0]++;
  170. IP[1] = IP[2] = IP[3] = 0;
  171. } else {
  172. return "";
  173. }
  174. } else {
  175. IP[1]++;
  176. IP[2] = IP[3] = 0;
  177. }
  178. } else {
  179. IP[2]++;
  180. IP[3] = 0;
  181. }
  182. } else {
  183. IP[3]++;
  184. }
  185. } else {
  186. done = true;
  187. return "";
  188. }
  189. } else
  190. first = false;
  191. return inttostr(IP[0]) + "." + inttostr(IP[1]) + "." + inttostr(IP[2]) + "." + inttostr(IP[3]);
  192. }
  193.  
  194. const std::string inttostr(const unsigned int x) {
  195. std::ostringstream o;
  196. if (!(o << x))
  197. return "";
  198. return o.str();
  199. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Talguy is offline Offline
96 posts
since Mar 2009

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: dice monopoly-style game c++, please help
Next Thread in C++ Forum Timeline: Password Security Test program





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


Follow us on Twitter


© 2011 DaniWeb® LLC