Thread: udp port scanner errors

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    7

    udp port scanner errors

    hi guys please help out, i have not put in the code for DNS query as yet however help me out.

    Code:
    
    
    
    #include <errno.h>
    #include <stdio.h>
    #include <netdb.h>
    #include <sys/time.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <netinet/ip.h>
    #include <netinet/in.h>
    #include <netinet/ip_icmp.h>
    #include <stdlib.h>
    #include <inttypes.h>
    #include <string.h>
    
    
    #define MAXSIZE 0
    #define resend 10
    #define udppac ('4030')
    #define synst sizeof(struct iphdr)
    
    
    
    
    
    
    void main(int argc, char **argv[]){
        
    
    
     
    
    
    if(argc<2){
          fprintf(stderr, "error, no port") ; 
          exit (1);
        }
       
    int scan();
    }
    
    
    
    
    
    
    int scan(){
        
    unsigned char buffe[udppac];
    char udppac=(char*)malloc(synst);
        
    struct timeval timesec;
    int sockfd;
    int ipleng;
    int re;
    int port= 1020;
    struct hostent *host;
    struct sockaddr my_addre;
    struct imcphdr *IC;
    struct sockaddr_in *my_addr;
    struct ip *ipp;
    struct iw_range *rang;
    struct iwreq *iwq;
    struct addrinfo adin, **adinfo, ***t;
    char *message;
    static char h_addr_list[0];
    static char addrlist[0];
    char r;
    struct FD_SET readfds;
    errno=0;
    int SIOCSIWSCAN();
    int ioctl();
    void iw_get_range_info();
    int io;
    io=ioctl(socket,SIOCSIWSCAN, &iwq);
    
    
    extern int wlan0;
    struct iw_range *rang;
    struct iwreq *iwq;
    
    
    iw_get_range_info(socket,"wlan0",&rang);
    if(r=iw_get_range_info(sockfd, "wlan0", &rang)>0){
        rang+1;
        exit(1);
    }
    
    
    struct servent serve;
    //char serve [50];
    int hlen;
    int slen;
    int portcounter;
    
    
    int haddrinfo;
    
    
    for(int i=0, portcounter=0; port<r; i++,portcounter++ ){
    int hostt;
    struct hostent host;
    *if(hostt=getnameinfo((struct sockaddr)&my_addre, MAXSIZE, &host, hlen, serve, slen, NULL)==NULL){
    
    
    memset(&adin,0,sizeof(adin));
    adin.ai_family=AF_UNSPEC;
    adin.ai_sock_type=SOCK_DGRAM;
    adin.ai_protocol=0;
    adin.ai_cannonname=INADDR_ANY;
    if(haddrinfo=getaddrinfo(NULL, 1020, &host, &adin, &adinfo)!=0){
        
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(haddrinfo));
        exit(1);
    }
    for(t = adinfo; t != NULL; t = t->ai_next) {
        if ((sockfd = socket(t->ai_family, t->ai_socktype,
               t->ai_protocol)) == -1) {
            perror("socket fail");
    }
    if (bind(sockfd, t->ai_addr, t->ai_addrlen) == -1) {
            close(sockfd);
            perror("bind failed");
            continue;
    }
    else
    printf("IP address");
    
    
    float addrlist[0];
    //addrlist=h_addr_list;
    for(i=0; portcounter>0; i++){
       int i;
       int port;
     port=i;
    printf("host: %d\n", portcounter);//double check
    printf("server: %d \n", haddrinfo);
    
    
    void bzero();
    struct sockaddr_in *my_addr;
    bzero((char*)&my_addr, sizeof(my_addr));
    atoi(argv);
    port=atoi(argv[1]);
    memset(&my_addr,0,sizeof(my_addr));
    my_addr.sin_family=AF_INET;
    my_addr.sin_port=htons(1020);
    my_addr.sin_addr.s_addr=INADDR_ANY;
    //my_addr.sin_addr(struct in_addr*)&host;
    
    
    
    
    sockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(sockfd<0){
    perror("socket not created");
    return -1;
    }
    
    
    int rsock;
    if((rsock=socket(AF_INET, SOCK_RAW, IPPROTO_ICMP))<0)) {
        perror(" socket fail [%s]\n");
        exit(-1);
    }
     memset (&ipp,0x0,sizeof(struct iphdr));
     
    my_addr.sin_family=AF_INET;
       my_addr.sin_port=1020;
      my_addr.s_addr=INADDR_ANY;
        
     re=0;   
    while(re++ < resend){
        
    if((sendto(sockfd,MAXSIZE,sizeof(MAXSIZE),0x0,(struct sockaddr*)&my_addre,sizeof(my_addre))<0)){
        perror("[ERROR] can't send");
        exit(-1);
    }
    if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&host,sizeof(host))<0){
        perror("setsockopt(SOL_BROADCAST)");
        exit(0);
        
    }  
            timesec.tv_sec=3;
    
    
            FD_SET *readfds;
            FD_ISSET(rsock,&my_addr);
            FD_ZERO(&readfds);
            FD_SET(rsock,&readfds);
            ipleng=ipp.ip_hl<<2;
            
            
            for(;;){
                if((select(rsock+1,NULL,&readfds,NULL,&timesec))>0){
                if((FD_ISSET(rsock,&readfds))!=0){
                    siz=read(rsock,(struct icmphdr*)&IC,sizeof(IC));
                    if(siz=0){
                        printf("can't read");
                    
                
                
                if(recvfrom(rsock,buffe,sizeof(buffe),0,&my_addre,NULL)<0){
                    perror("[ERROR] UDP:");
                    exit(-1);
                    
                }
                ipp=(struct ip*)buffe;
                ipleng=ipp->ip_hl <<2;
                message=(struct icmp*)(buffe+ipleng);
                    if(message->type==ICMP_UNREACH)&&message->code==ICMP_UNREACH_PORT{
                        exit(1);
                    }
                    else{
                        if(message==0){
                            printf("port open");
                            port=getservbyport(htons(addrlist[i]), "port");
                            printf("udp port %d %s is open\n", addrlist[i], port->s_addr);
                            if(port==NULL){
                                printf("unknown");
                            }
                            fflush(stdout);
                            }
                    
                     }
                
    
    
                     
                            
                            
                        }
                        
                        
                        
                        
                        
                    }
                    
                    
                }
    
    
        
            }
    }
    
    
    }
    
    
    }
    
    
    
    
    
    
    }
    
    
    
    
    
    
    
    
    }
    }
    
    
    
    
    close(sockfd);
    return 0;
    
    
    
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void main(int argc, char **argv[])
    It's not a good sign when you can't even get main anywhere near right.

    To be honest, you need to go back to page 1 of your book and start learning C properly this time.
    Instead of this headlong rush to copy/paste together random bits of crap code you found on the web.

    > i have not put in the code for DNS query as yet however
    You're not even in the ball park to be able to do this yet.

    The short process is
    - add small sections of code
    - does it compile? no, fix your mistakes
    - does it work, no, fix your mistakes
    - rinse and repeat until you finish the program.

    Your approach of dumping 100's of lines of code into a text editor, then dumping those same 100's of lines on a forum for someone else to sort out doesn't work.

    It isn't even CLOSE to being in a compilable state!.
    Code:
    $ gcc -Wall foo.c
    foo.c:26:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
     void main(int argc, char **argv[]){
          ^
    foo.c:26:6: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
    foo.c: In function ‘scan’:
    foo.c:48:21: warning: multi-character character constant [-Wmultichar]
     unsigned char buffe[udppac];
                         ^
    foo.c:49:6: warning: multi-character character constant [-Wmultichar]
     char udppac=(char*)malloc(synst);
          ^
    foo.c:18:17: error: expected identifier or ‘(’ before '\x34303330'
     #define udppac ('4030')
                     ^
    foo.c:49:6: note: in expansion of macro ‘udppac’
     char udppac=(char*)malloc(synst);
          ^
    foo.c:68:15: error: storage size of ‘readfds’ isn’t known
     struct FD_SET readfds;
                   ^
    foo.c:78:18: error: redeclaration of ‘rang’ with no linkage
     struct iw_range *rang;
                      ^
    foo.c:61:18: note: previous declaration of ‘rang’ was here
     struct iw_range *rang;
                      ^
    foo.c:79:15: error: redeclaration of ‘iwq’ with no linkage
     struct iwreq *iwq;
                   ^
    foo.c:62:15: note: previous declaration of ‘iwq’ was here
     struct iwreq *iwq;
                   ^
    foo.c:83:6: error: void value not ignored as it ought to be
     if(r=iw_get_range_info(sockfd, "wlan0", &rang)>0){
          ^
    foo.c:84:5: error: invalid use of undefined type ‘struct iw_range’
         rang+1;
         ^
    foo.c:84:5: warning: statement with no effect [-Wunused-value]
    foo.c:102:2: error: expected expression before ‘if’
     *if(hostt=getnameinfo((struct sockaddr)&my_addre, MAXSIZE, &host, hlen, serve, slen, NULL)==NULL){
      ^
    foo.c:101:16: warning: unused variable ‘host’ [-Wunused-variable]
     struct hostent host;
                    ^
    foo.c:100:5: warning: unused variable ‘hostt’ [-Wunused-variable]
     int hostt;
         ^
    foo.c:96:5: warning: unused variable ‘haddrinfo’ [-Wunused-variable]
     int haddrinfo;
         ^
    foo.c:93:5: warning: unused variable ‘portcounter’ [-Wunused-variable]
     int portcounter;
         ^
    foo.c:92:5: warning: unused variable ‘slen’ [-Wunused-variable]
     int slen;
         ^
    foo.c:91:5: warning: unused variable ‘hlen’ [-Wunused-variable]
     int hlen;
         ^
    foo.c:89:16: warning: unused variable ‘serve’ [-Wunused-variable]
     struct servent serve;
                    ^
    foo.c:79:15: warning: unused variable ‘iwq’ [-Wunused-variable]
     struct iwreq *iwq;
                   ^
    foo.c:77:12: warning: unused variable ‘wlan0’ [-Wunused-variable]
     extern int wlan0;
                ^
    foo.c:73:5: warning: variable ‘io’ set but not used [-Wunused-but-set-variable]
     int io;
         ^
    foo.c:68:15: warning: unused variable ‘readfds’ [-Wunused-variable]
     struct FD_SET readfds;
                   ^
    foo.c:66:13: warning: unused variable ‘addrlist’ [-Wunused-variable]
     static char addrlist[0];
                 ^
    foo.c:65:13: warning: unused variable ‘h_addr_list’ [-Wunused-variable]
     static char h_addr_list[0];
                 ^
    foo.c:64:7: warning: unused variable ‘message’ [-Wunused-variable]
     char *message;
           ^
    foo.c:63:36: warning: unused variable ‘t’ [-Wunused-variable]
     struct addrinfo adin, **adinfo, ***t;
                                        ^
    foo.c:63:25: warning: unused variable ‘adinfo’ [-Wunused-variable]
     struct addrinfo adin, **adinfo, ***t;
                             ^
    foo.c:63:17: warning: unused variable ‘adin’ [-Wunused-variable]
     struct addrinfo adin, **adinfo, ***t;
                     ^
    foo.c:60:12: warning: unused variable ‘ipp’ [-Wunused-variable]
     struct ip *ipp;
                ^
    foo.c:59:21: warning: unused variable ‘my_addr’ [-Wunused-variable]
     struct sockaddr_in *my_addr;
                         ^
    foo.c:58:17: warning: unused variable ‘IC’ [-Wunused-variable]
     struct imcphdr *IC;
                     ^
    foo.c:57:17: warning: unused variable ‘my_addre’ [-Wunused-variable]
     struct sockaddr my_addre;
                     ^
    foo.c:56:17: warning: unused variable ‘host’ [-Wunused-variable]
     struct hostent *host;
                     ^
    foo.c:54:5: warning: unused variable ‘re’ [-Wunused-variable]
     int re;
         ^
    foo.c:53:5: warning: unused variable ‘ipleng’ [-Wunused-variable]
     int ipleng;
         ^
    foo.c:51:16: warning: unused variable ‘timesec’ [-Wunused-variable]
     struct timeval timesec;
                    ^
    foo.c:48:15: warning: unused variable ‘buffe’ [-Wunused-variable]
     unsigned char buffe[udppac];
                   ^
    foo.c: At top level:
    foo.c:268:1: error: expected identifier or ‘(’ before ‘}’ token
     }
     ^
    foo.c:273:1: warning: data definition has no type or storage class
     close(sockfd);
     ^
    foo.c:273:1: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
    foo.c:273:1: warning: parameter names (without types) in function declaration
    foo.c:274:1: error: expected identifier or ‘(’ before ‘return’
     return 0;
     ^
    foo.c:281:1: error: expected identifier or ‘(’ before ‘}’ token
     }
     ^
    foo.c: In function ‘scan’:
    foo.c:267:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    Not to mention that it's a visual eyesore.
    Indent style - Wikipedia

    The best thing to do is hit the delete key and start again - yes, it's that awful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    7
    It was not a copy and paste thing I did my research wrote and rewrote stuff bit adapted my original according to what seemed right from what I saw.

    I researched and tried to create an algorithm for this code which was: scan for number of available ports, then using a port counter do the actually scanning. I tried to create two sockets one being a raw one for ICMP errors.

    I also tried to create code to gethostname and also to send the sockets separately then receive the sockdgram socket. I really did research a lot however my inability to declare and call functions as well as errors in pointer usage and structs caused me to fail.

    You are right that I haven't learnt the basics however. I'll slow down and learn c syntax etc better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A more advanced port scanner
    By fssp in forum C Programming
    Replies: 6
    Last Post: 03-23-2009, 01:14 AM
  2. C port scanner
    By xchg in forum Linux Programming
    Replies: 5
    Last Post: 02-18-2009, 04:34 PM
  3. My TCP Port Scanner in C
    By billy786 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-28-2008, 07:12 PM
  4. Port Scanner
    By Stabbsy in forum Networking/Device Communication
    Replies: 11
    Last Post: 11-28-2006, 09:45 AM
  5. Port Scanner
    By w4ck0z in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-22-2004, 04:08 PM

Tags for this Thread