Help with goto statement

Discussion in 'C' started by en_7123, Mar 10, 2010.

  1. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    This is my main program
    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<stdlib.h>
    #include<netdb.h>
    #include"simple_port_scanner.h"
    #include"dev_net.h"
    #include"sniff.h"
    #include<pcap.h>
    
    
    int main()
    {
    char ans;
    int choice;
    again:
    
    printf("***************WELCOME************* \n");
    
    printf("\nPlease look at the menu and enter the corresponding choice\n");
    printf("\n");
    printf("1.Simple Port Scan   :\n");
    printf("2.Check you device name,net address and subnet mask\n");
    printf("3.Let's Sniff\n");
    printf("\n");
    scanf("%d",&choice);
    
    switch(choice)
    
    {
    
    case(1):
    {
    simple_port_scanner();
    break;
    }
    
    case(2):
    {
    dev_net();
    break;
    }
    case(3):
    {
    sniff();
    break;
    }
    
    }
    printf("Do you wish to continue ??");
    scanf("%s",&ans);
    if(ans=='y')
    {
    goto again;
    }
    else
    {
    printf("exit");
    exit(1);
    }
    return 0;
    }
    
    
    Now in this the program should run until user enters some other key other than y.The code seems to run fine until I choose option 1 in which the program jumps to function simple_port_scanner().The code for which is

    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<stdlib.h>
    #include<netdb.h>
    
    void simple_port_scanner()
    {
        char ip[16];
    
            int i,net,new;
        
        struct sockaddr_in sa;
        
        
     
        printf("enter the ip");
        scanf("%s",ip);
        sa.sin_addr.s_addr=inet_addr(ip);
            for(i=1;i<20000;i++)
                {          
                             sa.sin_family=AF_INET;
                        sa.sin_port=htons(i);
                             net=socket(AF_INET,SOCK_STREAM,0);
            
                if((new=connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
            
            {
                printf("\n%d is open",i);
                close(new);  
            }
        }
        printf("now exit scanner   \n");
    
    
     }
    
    
    
    Output of main

    Please look at the menu and enter the corresponding choice

    1.Simple Port Scan :
    2.Check you device name,net address and subnet mask
    3.Let's Sniff

    1
    enter the ip127.0.0.1

    22 is open
    25 is open
    111 is open
    631 is open
    now exit scanner
    Do you wish to continue ??exit
    //end of output
    The problem is after do you wish to continue it does not wait for an input and just exits.This problem is only there when I choose option 1 that is simple port scan otherwise on choosing any other option the program keeps workin fine.Any clue or suggestion would be great.Thanks
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    do these changes in your code
    =========================
    Code:
    ...............
    char ans[2];    
    .............
    if (ans[0]=='y')....
    ...........
     
  3. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    give me the other two files "dev_net.h" and "sniff.h".
    It will help me to find the problem.
     
  4. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    Its not the problem with the goto , its problem with the buffer . I made some small changes in your program and now its working fine.Just close the socket fd ( net ) .

    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<stdlib.h>
    #include<netdb.h>
    
    
    void simple_port_scanner();
    
    int main()
    {
            char ans;
            int choice;
    
            printf("***************WELCOME************* \n");
    
    again:
            printf("\nPlease look at the menu and enter the corresponding choice\n");
            printf("\n");
            printf("1.Simple Port Scan   :\n");
            printf("2.Check you device name,net address and subnet mask\n");
            printf("3.Let's Sniff\n");
            printf("\n");
            scanf("%d",&choice);
    
            switch(choice)
            {
                    case(1):
                            {
                                    simple_port_scanner();
                                    break;
                            }
    
                    case(2):
                            {
                                    printf("Choice 2\n");
                                    break;
                            }
                    case(3):
                            {
                                    printf("Choice 3\n");
                                    break;
                            }
    
            }
            printf("Do you wish to continue : ");
            getchar();
            scanf("%c",&ans);
            if(ans=='y')
            {
                    goto again;
            }
            else
            {
                    printf("exit");
                    exit(1);
            }
            return 0;
    }
    
    
    void simple_port_scanner()
    {
            char ip[17];
    
            int i,net,new;
    
            struct sockaddr_in sa;
    
    
    
            printf("Enter the ip : ");
            scanf("%s",ip);
            sa.sin_addr.s_addr=inet_addr(ip);
            for(i=1;i<20000;i++)
            {
                    sa.sin_family=AF_INET;
                    sa.sin_port=htons(i);
                    net=socket(AF_INET,SOCK_STREAM,0);
    
                    if((new=connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
                    {
                            printf("%d is open\n",i);
                    }
            }
            close(net);
            printf("now exit scanner\n");
    
    }
    
    But using goto is not a good practice . It is also suggest in kernighan and ritchie's C guide.
     
  5. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Nope that doesn't help
     
  6. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Ok lemme try that karthigayan...
     
  7. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Ok could you also tell me why use getchar() and than scanf in next line.getchar() takes input than what is scanf for
     
  8. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Code:
     if((new=connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
                    {
                            printf("%d is open\n",i);
                    }
    
    
    Also why have you removed close(new).And yeah i added close(net) at the end
     
  9. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    Its to avoid the buffer problem.Some times the buffer may contain '\n'.Because the previous scanf will not consider the '\n' which you gave after typing the input.So when the next scanf (Here for ans ) will look on the '\n' and consider that the input for ans is available , so it will process with that .To avoid that I used getchar() ,so now the getchar will get the '\n',so the scanf can get the fresh input from the user.

    I hope you understand.
     
  10. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Made the changes in my code.Now it is taking the input but second time when you select 1.It simply exits.Same problem in your code karthigayan.

    output

    Please look at the menu and enter the corresponding choice

    1.Simple Port Scan :
    2.Check you device name,net address and subnet mask
    3.Let's Sniff

    1
    Enter the ip : 127.0.0.1
    22 is open
    25 is open
    111 is open
    631 is open
    now exit scanner
    Do you wish to continue : y

    Please look at the menu and enter the corresponding choice

    1.Simple Port Scan :
    2.Check you device name,net address and subnet mask
    3.Let's Sniff

    1
    Enter the ip : 209.85.231.104
    now exit scanner
    Do you wish to continue : n
    ----------------------------------------------------------------------------------------------------------------
    Second time it doesn't do anything .Something wrong in the scanner function ?
     
  11. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Any other suggestions ????
     
  12. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    Sorry , I forgot to explain the reason for removing the close(new) .I removed because the connect will always return 0 for success and -1 for error. So when the successful connect the value of new will be 0,and you close the 0 ( it means stdin ).So only those problem were happen .(connect will not return the file descriptor refer : man connect ) .


    Then I add the statement
    Code:
     close(net);
    
    Actually I misplaced this statement .I explained it below .
     
    Last edited: Mar 10, 2010
  13. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    There is big mistake in your port scanner function ,because of that only it doesn't work for the next attempt . I too missed to debug it in the previous code.

    Actually you tried to establish socket connection 20000 times.Some got succeed . For some time you opened some number of socket descriptors ,that exceeds the system limits.So it got fail the later iteration .You need to close the socket descriptor after checking the connect ion.

    Here is the debugger port scanner function , now the problems will not occur ,try this.

    Code:
    
     void simple_port_scanner()
    {
            char ip[17];
    
            int i,net,new;
    
            struct sockaddr_in sa;
    
    
    
            printf("Enter the ip : ");
            scanf("%s",ip);
            sa.sin_addr.s_addr=inet_addr(ip);
            for(i=1;i<20000;i++)
            {
                    sa.sin_family=AF_INET;
                    sa.sin_port=htons(i);
                    net=socket(AF_INET,SOCK_STREAM,0);
    
                    if((new=connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
                    {
                            printf("%d is open:%d\n",i);
                    }
                    [COLOR="Red"]close(net);[/COLOR] //Close the socket other wise it will exceed system limits
            }
            printf("now exit scanner\n");
    
    }
    
    Verify this output with your previous scanner outputs . You will see some big different .

    cheers
     
    Last edited: Mar 10, 2010
  14. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Oh finally I thinks that's the problem.Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice