Advertisement
Guest User

Untitled

a guest
Aug 30th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <sys/select.h>
  12. #include <errno.h>
  13. #include <inttypes.h>
  14.  
  15. void error(char *msg)
  16. {
  17.   perror(msg);
  18.   exit(1);
  19. }
  20.  
  21. char* scan(int begin, int end, char* ip, int timeout)
  22. {
  23.     int socketfd, retval;
  24.     fd_set rfds;
  25.  
  26.     //set timeout
  27.     struct timeval tv;
  28.     tv.tv_sec = 0;
  29.    
  30.     //create address
  31.     struct sockaddr_in address;
  32.    
  33.     //zero address memory
  34.     memset(&address,0, sizeof(address));
  35.  
  36.     //initialize address
  37.     address.sin_family = AF_INET;
  38.     address.sin_addr.s_addr = inet_addr(ip);
  39.     if (address.sin_addr.s_addr == -1)
  40.         error("Invalid IP address format");
  41.    
  42.     int port;
  43.     for (port = begin; port <= end; ++port)
  44.     {
  45.         //set port in network bit order
  46.         address.sin_port = htons(port);
  47.  
  48.         //get socket file descriptor
  49.         socketfd = socket(AF_INET, SOCK_STREAM, 0);
  50.         if (socketfd < 0)
  51.             error("ERROR opening socket");
  52.  
  53.         //add socket descriptor to set
  54.         FD_ZERO(&rfds);
  55.         FD_SET(socketfd, &rfds);
  56.  
  57.         //make socket non-blocking
  58.         int flags = fcntl(socketfd, F_GETFL, 0);
  59.         fcntl(socketfd, F_SETFL, flags | O_NONBLOCK);
  60.  
  61.         //try to connect
  62.         retval = connect(socketfd,(struct sockaddr *)&address,sizeof(address));
  63.  
  64.         //on failure to connect
  65.         if (retval == -1)
  66.         {
  67.             //if connection is waiting
  68.             if (errno == EINPROGRESS)
  69.             {
  70.                 //reset timeout value
  71.                 tv.tv_usec = timeout;
  72.  
  73.                 //wait until descriptor in set becomes writable, or operation times out
  74.                 retval = select(socketfd+1,NULL,&rfds,NULL,&tv);
  75.                 if (retval == -1)
  76.                 {
  77.                     error("select() failed");
  78.                 }
  79.                 else if (retval)
  80.                 {
  81.                     //check if something went wrong
  82.                     int optval, len;
  83.                     getsockopt(socketfd,SOL_SOCKET,SO_ERROR,&optval,&len);
  84.  
  85.                     //print appropriate message
  86.                     if (optval)
  87.                     {
  88.                         if (optval == ECONNREFUSED)
  89.                             printf("%d::closed\n",port);
  90.                         else
  91.                         {
  92.                             printf("Error value:%d %d::error\n",optval,port);
  93.                         }
  94.                     }
  95.                     else
  96.                         printf("%d::open\n",port);
  97.               }
  98.               else
  99.               {
  100.                   printf("%d::timed out\n",port);
  101.               }
  102.             }
  103.         }
  104.         close(socketfd);
  105.     }
  106.     return NULL;
  107. }
  108.  
  109. int main(int argc, char* argv[])
  110. {      
  111.     int begin, end, timeout;
  112.     char ip[16];
  113.    
  114.     int i;
  115.     for (i = 0; i < argc; ++i)
  116.     {
  117.         //if argument starts with '-'
  118.         if (argv[i][0] == '-')
  119.         {
  120.             switch (argv[i][1])
  121.             {
  122.                 case 'a':
  123.                 {
  124.                     strncpy(ip,argv[++i],15);
  125.                     break;
  126.                 }
  127.                 case 'b':
  128.                 {
  129.                     begin = atoi(argv[++i]);
  130.                     break;
  131.                 }
  132.                 case 'e':
  133.                 {
  134.                     end = atoi(argv[++i]);
  135.                     break;
  136.                 }
  137.                 case 't':
  138.                 {
  139.                     timeout = atoi(argv[++i]);
  140.                     break;
  141.                 }
  142.                 default:
  143.                 {
  144.                     error("Invalid argument");
  145.                 }
  146.             };
  147.         }
  148.     }
  149.    
  150.     //printf("begin: %d\nend: %d\naddress: %s\ntimeout: %d\n",begin,end,ip,timeout);
  151.  
  152.     char* result = scan(begin,end,ip,timeout);
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement