arguments passed to main?!?

This is a discussion on arguments passed to main?!? within the C++ Programming forums, part of the General Programming Boards category; hi heres some basic question why this darn scanner isnt working. Code: #include <stdio.h> #include <sys/types.h> #include <sys/types.h> #include <netdb.h> ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    arguments passed to main?!?

    hi heres some basic question why this darn scanner isnt working.
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/types.h>
    #include <netdb.h>
    #include <netinet/in.h>
    
    //void print(int status);
    
    int main (int argc, char *argv[])
    {
        int sockfd;
        struct sockaddr_in dest;
        struct hostent *he;
        int i = 0;
        int start_port = atoi(argv[2]);
        
        
        if (argc < 2);
        {
    	printf("usage: %s <hostname/ip> <startport>\n", argv[0]);
    	exit(-1);
        }
        
        if ((he = gethostbyname(argv[1])) == NULL)
        {
    	perror("gethostbyname");
    	exit (-1);
        }
        for(i= start_port; i < 65535; i++)
        {
    	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == - 1)
    	{
    	    perror("socket");
    	    exit(-1);
    	}
    	dest.sin_family = AF_INET;
        	dest.sin_port = htons(i);
        	dest.sin_addr = *((struct in_addr *)he->h_addr);
        
        	if(connect(sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr)) == -1)
        	{	
        	    printf("port %d\t Closed\n", i);
    	    close(sockfd);
    	    sleep(1);
    	}
    	else
    	{
    	printf("port %d\t opened\n", i);
    	close(sockfd);
    	sleep(1);
    	}
        }
        
        return 0;
    }
    even when i am using it correct (with 2 arguments)
    it prints out the string it is supposed to do when to
    few arguments were given.

    whats wrong?
    thank you

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    oh thats wasnt my intention!
    i changed it but still get an error
    when executing the program with excatly one argument.
    like
    Code:
    ./scan 192.168.0.1
    the error is a segmentation fault.
    why is this happening?

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Dunno if this is way off base or not, but what about your int i?

    I'd think that it would be in overflow mode since you're using a signed int, but you're running 'i' up to 65535. What's your MAX_INT range?

    Perhaps a long or unsigned int?

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    no it wasnt that one an overflow related to i.

    well....


  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    so whats that supposed to mean?
    my code looks like this now:
    Code:
    main ()
    ......
    int start_port;
    
    if (argc <2)
    {
       printf("usage: %s <hostname> <start_port>", argv[0]);
       exit(-1);
    }
    
    start_port = atoi(argv[2]);
    
    
    code.....
    when i run strace on my compiled program it says the error is cause by the exit call of the if loop.
    whats wrong? wha

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Change exit(-1) to exit(1) or return 1.

  7. #7
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    ill try that. thanks

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    According to this:
    >>printf("usage: %s <hostname> <start_port>", argv[0]);

    this part :
    >if (argc <2)
    should really be
    >if (argc <3)

    argc will always be at least 1, as argv[0] is the program name.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >argc will always be at least 1, as argv[0] is the program name.
    Not necessarily, argc can be 0 if the program name is not available from the host environment.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    hey thats almost what i got...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
    	if (argc !=  5)
    	{
    		fprintf(stderr, "Usage: scan [target] [start port] [end port] [log filename]\n");
    		exit(1);
    	}
    	if (atoi(argv[3]) > 65535)
    	{
    		fprintf(stderr, "There is no port larger than 65535\n");
    		exit(1);
    	}
    	if (atoi(argv[2]) < 1)
    	{
    		fprintf(stderr, "There is no port smaller than 1\n");
    		exit(1);
    	}
    	ofstream fileOut(argv[4]);
    	printf("*************** Port Scan 0.01 ***************\n");
    	fileOut << "*************** Port Scan 0.01 ***************" << endl;
    	struct hostent *h;
    	h = gethostbyname(argv[1]);
    	if (h == NULL)
    	{
    		herror("gethostbyname");
    		exit(1);
    	}
    	char *ip = inet_ntoa(*((struct in_addr *) h -> h_addr));
    	int start = atoi(argv[2]);
    	int end = atoi(argv[3]);
    	int sockfd;
    	struct sockaddr_in dest_addr;
    	sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	for(int i = start; i < (end + 1) ; i++)
    	{
    		if( (sockfd == -1))
    	 	{
    		herror("socket");
    		exit(1);
    		}
    		dest_addr.sin_family = AF_INET;
    		dest_addr.sin_port = htons(i);
    		dest_addr.sin_addr.s_addr = inet_addr(ip);
    		if(connect(sockfd, (struct sockaddr *)& dest_addr, sizeof(struct sockaddr)) == -1)
    		{
    		printf("Port     %d      closed!\n", i);
    		}
    		else
    		{
    		printf("Port     %d      open!\n", i);
    		fileOut << "Port " <<  i  << " open!" << endl;
    		}
    	}
    	close(sockfd);
    	return 0;
    }
    the for loop at the end doesnt work because it just gets to the first port that is open and sez that the rest are closed when i'm sure they are not. help?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    God that's just ugly code. Stick with C or C++. Stop mixing streams. It's bad form and it looks like crap. Furthermore, since this is actually C++, it is in the wrong place.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> it is in the wrong place.
    Moving....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    i know that its ugly and i was gonna make it nice but then i said "meh"

    so any help with that loop or should i repost in C++?

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>should i repost in C++?
    No.

    You can use perror() after a failed connect call, that may help you work out whats going wrong.

    Also, shouldn't you be closing the socket within the loop, not outside it?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    i figured it out, i used the original if statement from this code cuz i saw perror gimme bad file descriptor errors so i figured restating sockfd fiile descriptor in the if would work...thanx that perror is some good advice and thats what im here for

    /*Edit*/
    i do have one question tho, i realized it always stops at port 136, how come?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. commands and arguments problem
    By Martin Kovac in forum C Programming
    Replies: 2
    Last Post: 03-20-2009, 05:18 PM
  2. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 07:54 AM
  3. Passing arguments to main?
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 04:59 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 04:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21