Thread: please help ! IP validation function

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    Smile please help ! IP validation function

    hi everyone ..

    i must be daft .. (too much programming) please tell me whats wrong in this function i wrote to validate and pass an ip as a pointer..

    Code:
    char* ip_validation()
    {
    	struct sockaddr_in ipStore;					
    	char *ipVal;                      // this ip number value gets returned to the main program
    	int valid_input, exit_flag = 0;	// when valid_input = 1,the date becomes valid and the loop is exited
    	char user_input;				// handles user input, single character menu choice
    	unsigned b1, b2, b3, b4;		// ip 
    	int rc;
    
    
    	while( exit_flag == 0 ) 
    	{
    		valid_input = 0;
    		printf("\nUse the Default ip 192.168.0.1 (Y/N)?: ");
    		scanf("%c",&user_input);
    		user_input = toupper( user_input );
    			if((user_input == 'Y')||(user_input == 'N'))  
    			valid_input = 1;
    			else
    			printf("\007Error: Invalid choice\n\n"); 
    			clear_char();
    
    		switch(user_input)
    			{
    			case 'Y':
    				ipStore.sin_addr.s_addr = inet_addr("127.0.0.1");     // http://cboard.cprogramming.com/showthread.php?t=15179&highlight=store+ip+address
    				ipVal = inet_ntoa(ipStore.sin_addr);
    				memset(&(ipStore.sin_zero), '\0', 8);  // zero the rest of the struct 
    				exit_flag = 1;
    				break;
    			case 'N':
    				valid_input = 0;							// when valid_input = 1,the data becomes valid and the loop is exited 
    				while( valid_input == 0 )
    				{
    				printf("\nEnter a IP address: ");
    				rc = scanf("%3u.%3u.%3u.%3u",&b1, &b2, &b3, &b4);
    				clear_char();
    
    				if ((rc != 4 && rc != 6) || ((b1 | b2 | b3 | b4) > 255) || (strspn(ipVal, "0123456789.:") < strlen(ipVal)))
    					{
    					valid_input = 0;
    					printf("\007Error: Incorrect IP\n");
    					}
    				else
    					{
    					valid_input = 1;
    					ipStore.sin_addr.S_un.S_un_b.s_b1 = b1;
    					ipStore.sin_addr.S_un.S_un_b.s_b2 = b2;
    					ipStore.sin_addr.S_un.S_un_b.s_b3 = b3;
    					ipStore.sin_addr.S_un.S_un_b.s_b4 = b4;														
    					}	
    				}	
    				memset(&(ipStore.sin_zero), '\0', 8);  // zero the rest of the struct 
    				exit_flag = 1;
    				break; //break case 'N'
    			}// end switch
    	}//end while
    ipVal = inet_ntoa(ipStore.sin_addr);
    return ipVal;
    }//end function
    
    
    void clear_char(void) 
    {
    	int ch;
    	while((ch = getchar()) != '\n'); 
    }
    and please ..if you know of any sample codes online which SHOULD be much better than this one .. i would greatly appriciate it thankyou for your time

    Last edited by madcat23; 05-03-2006 at 10:58 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("\007Error: Invalid choice\n\n");
    > clear_char();
    1. Don't use 007 to ring the bell. If you really must, use \a.
    But there is nothing more annoying than trivial beeping code which you can't silence.
    2. The indentation makes it seem clear_char() is part of the else, but it isn't really.

    > ipVal = inet_ntoa(ipStore.sin_addr);
    If ipVal is pointing to the innards of that data, then you're returning a pointer to a local.
    This is a bad thing.

    > char* ip_validation()
    My suggestion would be to separate input from validation.
    Have functions which
    - input an IP into a char buffer
    - validate the IP address in that buffer
    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
    Apr 2006
    Posts
    9
    Quote Originally Posted by Salem
    > printf("\007Error: Invalid choice\n\n");
    > clear_char();
    1. Don't use 007 to ring the bell. If you really must, use \a.
    But there is nothing more annoying than trivial beeping code which you can't silence.
    2. The indentation makes it seem clear_char() is part of the else, but it isn't really.

    > ipVal = inet_ntoa(ipStore.sin_addr);
    If ipVal is pointing to the innards of that data, then you're returning a pointer to a local.
    This is a bad thing.

    > char* ip_validation()
    My suggestion would be to separate input from validation.
    Have functions which
    - input an IP into a char buffer
    - validate the IP address in that buffer
    yeah i think i did get carried away with that beep .. lol

    thanks Salem .. will make the nessecary changes but i`m sorry i dont get what your saying about the clear_char() what am i doing wrong there ? or more specifically .. where can i indent it ?

    thanks again.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    oops - sorry salem dont reply to that .. sometimes you fail to see the obvious .. haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM