module IpConverter

Contains the methods for doing IP Address conversions

Example:

IpConverter.str_to_int('192.168.2.1') # 3232236033

Constants

VERSION

Public Class Methods

int_to_str(ip_addr_integer) → String click to toggle source

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_integer) {
  char c_string[16];
  int64_t ip = NUM2LL(ip_integer);
  if (ip > MAX_IP_INT || ip < 0) {
     rb_raise(rb_eArgError, "IP address integer out of range");
  }
  ip_long_to_string((uint32_t)ip, c_string);
  return rb_str_new2(c_string);
}
str_to_int(ip_addr_string) → Integer click to toggle source

Converts the passed IP address String into an Integer.

Raises ArgumentError if ip address is not valid. Leading and trailing whitespace is ignored.

Example:

IpConverter.str_to_int("192.168.2.1")
  => 3232236033
VALUE method_str_to_int(VALUE _module_, VALUE ip_string) {

  // C string version of the ip_string
  char* c_string = RSTRING_PTR(StringValue(ip_string));

  uint32_t result;

  int success = ip_string_to_long(c_string, &result);

  if (success) {
    // Convert the uint32_t back to a ruby Integer.
    return UINT2NUM(result);
  } else {
    rb_raise(rb_eArgError, "Invalid IP Address String");
  }
}

Private Instance Methods

int_to_str(ip_addr_integer) → String click to toggle source

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_integer) {
  char c_string[16];
  int64_t ip = NUM2LL(ip_integer);
  if (ip > MAX_IP_INT || ip < 0) {
     rb_raise(rb_eArgError, "IP address integer out of range");
  }
  ip_long_to_string((uint32_t)ip, c_string);
  return rb_str_new2(c_string);
}
str_to_int(ip_addr_string) → Integer click to toggle source

Converts the passed IP address String into an Integer.

Raises ArgumentError if ip address is not valid. Leading and trailing whitespace is ignored.

Example:

IpConverter.str_to_int("192.168.2.1")
  => 3232236033
VALUE method_str_to_int(VALUE _module_, VALUE ip_string) {

  // C string version of the ip_string
  char* c_string = RSTRING_PTR(StringValue(ip_string));

  uint32_t result;

  int success = ip_string_to_long(c_string, &result);

  if (success) {
    // Convert the uint32_t back to a ruby Integer.
    return UINT2NUM(result);
  } else {
    rb_raise(rb_eArgError, "Invalid IP Address String");
  }
}