UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2010-03-10 11:33 AM

ravi_link87
Member
Registered: 2010-03-06
Posts: 41

Re: Problem with goto

This is my  main program

#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

#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

Offline

#2 2010-03-10 03:22 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Problem with goto

This is completely wrong:

char ans;
...
scanf("%s",&ans);

You're overflowing that single char every time, because scanf("%s") will append a
null character, since it's supposed to be reading a null-terminated STRING, not just a
single character...  So, you're trashing your stack, and at that point, anything can
happen...

But, note: just changing to scanf("%c") won't necessarily do what you want, either...
Then, you might end up reading the trailing return/newline from the previously read
entry...  Doing " %c" (a space in front of it) should work...  But, really, scanf() is a
horrible way to read input, in general...  For something like this, you're much better
off to just fgets() into a line buffer, then parse up that line however you want...

Offline

#3 2010-03-10 05:08 PM

ravi_link87
Member
Registered: 2010-03-06
Posts: 41

Re: Problem with goto

Ok just figured it out .It was buffer problem.Had to close socket descriptor net inside the for loop.Yeah but you are right about not using scanf.But for now this works fine.

void simple_port_scanner()
{
	char ip[17];

    	int i,net,new;
    
    struct sockaddr_in sa;
    
	
 
	printf("enter the ip   : ");
getchar();

	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(net);
    }
    printf("now exit scanner   \n");


 }

Offline

Board footer

Powered by FluxBB