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