class Subnets::IP4

Public Class Methods

new(p1) click to toggle source
VALUE
method_ip4_new(VALUE class, VALUE address) {
  return ip4_new(class, RB_NUM2UINT(address));
}
random(p1 = v1) click to toggle source

@overload random(rng=Random.new)

@param rng [#rand] (optional) a random number generator

@return [IP4] a random IP4 address

VALUE
method_ip4_random(int argc, VALUE *argv, VALUE class) {
  ip4_t ip;
  VALUE rng, rand;

  rb_scan_args(argc, argv, "01", &rng);
  if (Qnil == rng) {
    rng = rb_funcall(rb_cRandom, rb_intern("new"), 0);
  }

  rand = rb_intern("rand");
  ip = FIX2INT(rb_funcall(rng, rand, 1, INT2FIX(0xffff+1)));
  ip |= FIX2INT(rb_funcall(rng, rand, 1, INT2FIX(0xffff+1))) << 16;

  return ip4_new(class, ip);
}

Public Instance Methods

&(p1) click to toggle source

@param other [Subnets::IP4] @return [Subnets::IP4] bitwise AND of self and other

VALUE
method_ip4_band(VALUE self, VALUE other) {
  ip4_t *a, *b;

  assert_kind_of(other, IP4);

  Data_Get_Struct(self, ip4_t, a);
  Data_Get_Struct(other, ip4_t, b);
  return ip4_new(IP4, *a & *b);
}
==(p1) click to toggle source

@return [Boolean]

VALUE
method_ip4_eql_p(VALUE self, VALUE other) {
  ip4_t *a, *b;

  if (CLASS_OF(other) != CLASS_OF(self)) {
    return Qfalse;
  }

  Data_Get_Struct(self, ip4_t, a);
  Data_Get_Struct(other, ip4_t, b);

  return (*a == *b) ? Qtrue : Qfalse;
}
Also aliased as: eql?
^(p1) click to toggle source

@param other [Subnets::IP4] @return [Subnets::IP4] bitwise exclusive XOR of self and other

VALUE
method_ip4_xor(VALUE self, VALUE other) {
  ip4_t *a, *b;

  assert_kind_of(other, IP4);

  Data_Get_Struct(self, ip4_t, a);
  Data_Get_Struct(other, ip4_t, b);
  return ip4_new(IP4, *a ^ *b);
}
eql?(p1)

@return [Boolean]

Alias for: ==
hash() click to toggle source

@return [Integer]

VALUE
method_ip4_hash(VALUE self) {
  ip4_t *ip;
  Data_Get_Struct(self, ip4_t, ip);
  return hash(UINT2NUM(*ip));
}
to_i() click to toggle source

@return [Numeric] the 32 bit integer representing this address

VALUE
method_ip4_to_i(VALUE self) {
  ip4_t *ip;
  Data_Get_Struct(self, ip4_t, ip);
  return RB_UINT2NUM(*ip);
}
to_s() click to toggle source

Return a String in dotted-decimal notation.

@return [String]

VALUE
method_ip4_to_s(VALUE self) {
  ip4_t *ip;
  char buf[16];

  Data_Get_Struct(self, ip4_t, ip);
  ip4_snprint(*ip, buf, 16);
  return rb_str_new2(buf);
}
|(p1) click to toggle source

@param other [Subnets::IP4] @return [Subnets::IP4] bitwise OR of self and other

VALUE
method_ip4_bor(VALUE self, VALUE other) {
  ip4_t *a, *b;

  assert_kind_of(other, IP4);

  Data_Get_Struct(self, ip4_t, a);
  Data_Get_Struct(other, ip4_t, b);
  return ip4_new(IP4, *a | *b);
}
~() click to toggle source

@return [IP4] bitwise NOT of self

VALUE
method_ip4_not(VALUE self) {
  ip4_t *ip;
  Data_Get_Struct(self, ip4_t, ip);
  return ip4_new(IP4, ~ *ip);
}