I'm using a MOD5282 and running NBEclipse 2.8.2. I've written the following method to convert a string to an IPADDR4 object:
Code: Select all
bool IP_From_String(char *ip_string, IPADDR4 *ip)
{
iprintf("Entered IP_From_String. String is: %s\n", ip_string);
// Check to make sure the format is correct
//TODO: undefined behavior when values > 255 in ip_string.
unsigned char ip1, ip2, ip3, ip4;
DWORD ip_long;
if(sscanf(ip_string, "%hhu.%hhu.%hhu.%hhu", &ip1, &ip2, &ip3, &ip4) != 4)
{ // Check for format
iprintf("received invalid IP: %s\n", ip_string);
return false;
}
else
{ // everything looks correct
iprintf("ip: %d.%d.%d.%d\n", ip1, ip2, ip3, ip4);
ip_long = (ip1 << 24) | (ip2 << 16) | (ip3 << 8) | ip4;
iprintf("ip_long: %lu\n", ip_long);
}
*ip = IPADDR4(ip_long);
return true;
}
Code: Select all
Entered IP_From_String. String is: 192.168.1.20
ip: 0.0.0.0
ip_long: 0
Any ideas on why sscanf isn't behaving as expected here? The code works with other compilers.
If there's a better way to create an IPADDR4 object from a string, I'd be interested in that as well. I searched the forum but wasn't able to find anything.