Advertisement
Guest User

Untitled

a guest
Sep 15th, 2020
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. typedef union
  10. {
  11.         uint32_t ip_32;
  12.         uint8_t ip[4];
  13. } u_ip_addr;
  14. int is_valid(u_ip_addr *addr)
  15. {
  16.         const int portno = 22;
  17.         char *addr_hostname;
  18.         int sockfd, is_connected, opt;
  19.         struct sockaddr_in serv_addr;
  20.         struct hostent *server;
  21.         fd_set wait_set;
  22.         struct timeval *timeout;
  23.  
  24.         is_connected = 0;
  25.         timeout = malloc(sizeof(struct timeval));
  26.         timeout->tv_sec = 0;
  27.         timeout->tv_usec = 250000;
  28.         addr_hostname = malloc(17);
  29.         sprintf(addr_hostname, "%u.%u.%u.%u", addr->ip[3], addr->ip[2], addr->ip[1], addr->ip[0]);
  30.         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  31.                 goto done;
  32.         opt = fcntl(sockfd, F_GETFL, NULL);
  33.         fcntl(sockfd, F_SETFL, opt | O_NONBLOCK);
  34.         if ((server = gethostbyname(addr_hostname)) == NULL)
  35.                 goto done;
  36.         bzero((char *) &serv_addr, sizeof(serv_addr));
  37.         serv_addr.sin_family = AF_INET;
  38.         bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
  39.         serv_addr.sin_port = htons(portno);
  40.         if ((is_connected = connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))) < 0)
  41.         {
  42.                 if (errno == EINPROGRESS)
  43.                 {
  44.                         printf("\r                                     \rTesting %s", addr_hostname);
  45.                         fflush(stdout);
  46.                         FD_ZERO(&wait_set);
  47.                         FD_SET(sockfd, &wait_set);
  48.                         is_connected = select(sockfd + 1, NULL, &wait_set, NULL, timeout);
  49.                 }
  50.         }
  51.         else
  52.                 is_connected = 1;
  53.         close(sockfd);
  54. done:
  55.         free(addr_hostname);
  56.         free(timeout);
  57.         if (is_connected)
  58.                 printf("\r                                             \r");
  59.         return !is_connected;
  60. }
  61. int main()
  62. {
  63.         u_ip_addr *addr;
  64.         uint8_t first, last;
  65.         addr = malloc(sizeof(u_ip_addr));
  66.         for (addr->ip_32 = 0; addr->ip_32 < UINT32_MAX; ++addr->ip_32)
  67.         {
  68.                 first = addr->ip[3];
  69.                 last = addr->ip[0];
  70.                 if ((first == 0 || first == 10 || first == 127) ||
  71.                     (first == 169 || first == 172 || first == 192) ||
  72.                      first == 185)
  73.                         continue;
  74.                 if ((last == 0 || last == 1 || last == 254) ||
  75.                      last == 255)
  76.                         continue;
  77.                 if (addr->ip[2] == 0 || addr->ip[1] == 0)
  78.                         continue;
  79.                 if (!is_valid(addr))
  80.                 {
  81.                         printf("%u.%u.%u.%u\n", first, addr->ip[2], addr->ip[1], last);
  82.                         break;
  83.                 }
  84.         }
  85.         free(addr);
  86.         return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement