title: UDP port scanner issues, C
body: <p>So I am trying to create a functional udp port scanner. The code I have so far seems to be missing something such maybe switch, use of iwreq or simply a linked list for the scanned ports, not sure. I am new to C programming so would appreciate any help.</p>
  
  <pre><code>#include &lt;erno.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/ip.h&gt;
  #include &lt;netinet/ip_icmp.h&gt;
  #include &lt;iwlib.h&gt;
  #include &lt;wireless.h&gt;
  
  #define MAXSIZE 0
  
  int main(){
  
  struct timeval timesec;
  int sockfd;
  int ipleng;
  
  int port= 1020;
  struct hostent hostent;
  struct sockaddr my_addre;
  struct imcmphdr IC;
  struct sockaddr_in my_addr;
  struct ip ipp;
  struct iw_range rang;
  struct iwreq iwq;
  char message="  ";
  h_addr_list[0];
  
  
  
  scan();
  if(scan==NULL){
  perror("scan not done");
  exit(1);
  
  }
  int scan(){
  ioctl(socket,SIOCSIWSCAN,&amp;iwq);
  iw_get_range_info(sockfd,wlan0,&amp;rang);
    if(iw_get_range_info(sockfd, wlan0, &amp;rang)&gt;0){
      rang=1;
    }
     r=iw_get_range_info(sockfd,wlan0,&amp;rang);
  char serve [50];
  int hlen;
  int slen;
  for(int i=0; port&lt;r; i++){
  if(hostt=getnameinfo(struct hostent  *host, MAXSIZE, host, hlen, serve, 
  slen, NULL)==NULL){
  
  if(haddrinfo=getaddrinfo(struct hostent *host, MAXSIZE, host, hlen, serve, 
  slen, NULL)==NULL){
  exit(1);
  }
  else{
  printf("IP address");
  addrlist=h_addr_list;
  for(i=0; addrlist[i]&gt;0; i++){
  printf("host: %s\n", arrdlist[i],  hostt);
  printf("server: %s\n", haddrinfo);
  
  if(sockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))&lt;0 {
     perror("socket not created");
     return -1;
  
  if(rsock=sockfd(AF_INET, SOCK_RAW, IPPROTO_ICMP))&lt;0){
      perror("socket fail"); 
      exit(1);
      else{
  
  
  memset(&amp;my_addr,0,sizeof(my_addr));
  my_addr.sin_family=AF_INET;
  my_addr.sin_port=htons(1020);
  my_addr.sin_addr.s_addr=inet_addr();
  my_addr.sin_addr(struct in_addr*)hostt-&gt;h_addr;
  
  
  
  if(sendto(sockfd,MAXSIZE,0,0,(struct sockaddr*)&amp;my_addre,sizeof(my_addre))
  &lt;0){
      perror("can't send");
      return -1;
     }
    }
   }
  }
  if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&amp;host,sizeof(host))== -1){
      perror("setsockopt(SOL_BROADCAST)");
      return -1;
      else
          timesec.tv_sec=3;
       while(1);
      {
      FD_ISSET(&amp;my_addr);
      FD_ZERO(&amp;readfds);
      FD_SET(&amp;rsock,&amp;readfds);
      ipleng=ip-&gt;ip_hl&lt;&lt;2;
      if(select(rsock+1,&amp;readfds,NULL,NULL,&amp;timsec);
      for(;;){
          if(select(rsock+1,&amp;readfds,NULL,NULL,&amp;timesec){
          if(FD_ISSET(rsock,&amp;readfds0{
              siz=read(rsock,(struct icmphdr*)&amp;IC,sizeof(IC));
              if(siz=0){
                  printf("can't read");
              }
  
           }
  
  
          }
    } 
          ipp=(struct ip*)MAXSIZE;
          message=(struct icmp*)(MAXSIZE+ipleng);
          if(recvfrom(rsock,MAXSIZE,sizeof(MAXSIZE),NULL,my_addre,hlen)&lt;0){
              perror("ICMP error");
              printf("port closed");
              if(message-&gt;icmp_type==ICMP_UNREACH)&amp;&amp;message-&gt;icmp_code==ICMP_UNREACH_PORT{
                  exit(1);
              }
              else{
                  if(message==0){
                      printf("p;ort open");
                      port=getservbyport(htons(addrlist[i], "port");
                      printf("udp port %d %s is open\n", addrlist[i], port-&gt;s_addr);
                      if(port==NULL){
                          printf("unknown");
                      }
  
  
  
  
                  }
  
  
  
  
  
              }
  
  
            }
  
  
          }
         }
  
        }
  
      }
  
  
  
    }
  
  
  
  
    }
   }
  
  
  fflush(sockfd);
  return 0;
  
  
  
  }
  </code></pre>