ext/ipconverter/ipconverter.c in ipconverter-0.2.0 vs ext/ipconverter/ipconverter.c in ipconverter-0.3.0
- old
+ new
@@ -46,25 +46,25 @@
// Returns 0 if ip is invalid; 1 otherwise.
// takes an ip address string, like "192.168.2.254", finds the 32-bit integer
// value and stores that in result.
static int
ip_string_to_long(char c_string[], uint32_t *result) {
- int32_t i, found_octets;
+ uint32_t i, found_octets;
char junk;
- int32_t octets[4];
+ uint32_t octets[4];
found_octets = sscanf((char*)c_string, "%d.%d.%d.%d%c", (int*)&octets[3], (int*)&octets[2], (int*)&octets[1], (int*)&octets[0], &junk);
// If we didn't find exactly 4 octets, bail out, unless the extra is whitespace
- if (found_octets != 4
+ if (found_octets != 4
&& !(found_octets == 5 && junk == ' ')) {
return 0;
}
// If any of the octets are not in-range, bail out
for (i = 0; i < 4; i++) {
- if (octets[i] > 255 || octets[i] < 0) {
+ if (octets[i] > 255) {
return 0;
}
}
*result = (octets[0] + octets[1] * 256 + octets[2] * 256 * 256 + octets[3] * 256 * 256 * 256);
@@ -74,18 +74,18 @@
/*
* call-seq:
* IpConverter.int_to_str(ip_addr_integer) -> String
*
* Converts the passed integer into an IPv4 address string.
- *
+ *
* Raises ArugmentError if number is negative, or greater than the maximum
* possible value for an IPv4 address (4294967295)
*
* Example:
* IpConverter.int_to_str(3232236033)
* => "192.168.2.1"
- *
+ *
*/
VALUE
method_int_to_str(VALUE _module_, VALUE ip_fixnum) {
char c_string[16];
int64_t ip = NUM2LL(ip_fixnum);
@@ -102,9 +102,9 @@
ip_long_to_string(uint32_t ip, char c_string[]) {
uint8_t bytes[4];
bytes[0] = ip & 0xFF;
bytes[1] = (ip >> 8) & 0xFF;
bytes[2] = (ip >> 16) & 0xFF;
- bytes[3] = (ip >> 24) & 0xFF;
+ bytes[3] = (ip >> 24) & 0xFF;
sprintf(c_string, "%d.%d.%d.%d", bytes[3], bytes[2], bytes[1], bytes[0]);
}