Help with goto statement

en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
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
virxen's Avatar, Join Date: Nov 2009
Pro contributor
do these changes in your code
=========================
Code:
...............
char ans[2];    
.............
if (ans[0]=='y')....
...........
ungalnanban's Avatar, Join Date: Feb 2010
Go4Expert Member
give me the other two files "dev_net.h" and "sniff.h".
It will help me to find the problem.
karthigayan's Avatar, Join Date: Feb 2010
Go4Expert Member
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.
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Quote:
Originally Posted by virxen View Post
do these changes in your code
=========================
Code:
...............
char ans[2];    
.............
if (ans[0]=='y')....
...........
Nope that doesn't help
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Ok lemme try that karthigayan...
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Ok could you also tell me why use getchar() and than scanf in next line.getchar() takes input than what is scanf for
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
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
karthigayan's Avatar, Join Date: Feb 2010
Go4Expert Member
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.
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
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 ?