Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <sys/signal.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8.  
  9. #define DEFAULT_PORT 80
  10.  
  11. int main (int argc, char *argv[])
  12. {
  13.  
  14. int sockfd, result, counter = 0;
  15. char host[15];
  16. char *ip;
  17. int port;
  18.  
  19. struct sockaddr_in address;
  20. if (argc < 2)
  21. {
  22. printf ("Usage: %s [X.X.X] <port>\n", argv[0]);
  23. exit (1);
  24. }
  25.  
  26. if(argv[2] != NULL) { port = atoi(argv[2]); }
  27. else { port = DEFAULT_PORT; }
  28.  
  29. ip = argv[1];
  30. sprintf(host, "%s.255", ip);
  31. if(!inet_aton(host, &address.sin_addr))
  32. { printf("error: invalid class c\n"); exit(1); }
  33.  
  34. while (counter < 255)
  35. {
  36. ++counter;
  37. sprintf (host, "%s.%d\n", ip, counter);
  38. if ((fork ()) == 0)
  39. {
  40. address.sin_family = AF_INET;
  41. address.sin_port = htons (port);
  42. address.sin_addr.s_addr = inet_addr (host);
  43. sockfd = socket (AF_INET, SOCK_STREAM, 0);
  44. if (sockfd < 0)
  45. {
  46. perror ("Socket");
  47. exit (2);
  48. }
  49. alarm (3);
  50. result = connect (sockfd, (struct sockaddr *) &address, sizeof (addres
  51. s));
  52. if (result == 0)
  53. {
  54. printf ("%s", host);
  55. close (sockfd);
  56. exit (0);
  57. continue;
  58. }
  59. exit (0);
  60. }
  61. }
  62. sleep(1);
  63. close (sockfd);
  64. exit (0);
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement