Class: Subnets::IP6

Inherits:
IP
  • Object
show all
Defined in:
ext/subnets/ext.c

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IP

#inspect

Class Method Details

.new(hextets) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/subnets/ext.c', line 86

VALUE
method_ip6_new(VALUE class, VALUE hextets) {
  ip6_t ip;

  if (RARRAY_LEN(hextets) != 8) {
    rb_raise(rb_eArgError, "hextets must be size=8, was %ld", RARRAY_LEN(hextets));
  }

  for (ssize_t i = 0; i < RARRAY_LEN(hextets); i++) {
    ip.x[i] = NUM2INT(RARRAY_AREF(hextets, i)) & 0xffff;
  }

  return ip6_new(class, ip);
}

.random(rand = Random.new, opts = {}) ⇒ IP6

Returns a random IP6 address.

Parameters:

  • rand (#rand) (defaults to: Random.new)

    (optional) a random number generator

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :zeros (Boolean)

    include a random string of zeros at a random position rather than simply randomizing the entire address

Returns:

  • (IP6)

    a random IP6 address



265
266
267
268
269
270
271
272
273
274
# File 'ext/subnets/ext.c', line 265

VALUE
method_ip6_random(int argc, VALUE *argv, VALUE class) {
  ip6_t ip;
  VALUE rng;
  VALUE opts;

  rb_scan_args(argc, argv, "01:", &rng, &opts);
  ip6_fill_random(&ip, rng, opts);
  return ip6_new(class, ip);
}

Instance Method Details

#&(other) ⇒ Subnets::IP4

Returns bitwise AND of self and other.

Parameters:

Returns:



398
399
400
401
402
403
404
405
406
407
408
# File 'ext/subnets/ext.c', line 398

VALUE
method_ip6_band(VALUE self, VALUE other) {
  ip6_t *a, *b;

  assert_kind_of(other, IP6);

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

  return ip6_new(IP6, ip6_band(*a, *b));
}

#==(other) ⇒ Boolean Also known as: eql?

Returns:

  • (Boolean)


662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'ext/subnets/ext.c', line 662

VALUE
method_ip6_eql_p(VALUE self, VALUE other) {
  ip6_t *a, *b;

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

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

  return ip6_eql_p(*a, *b) ? Qtrue : Qfalse;
}

#^(other) ⇒ Subnets::IP4

Returns bitwise XOR of self and other.

Parameters:

Returns:



382
383
384
385
386
387
388
389
390
391
392
# File 'ext/subnets/ext.c', line 382

VALUE
method_ip6_xor(VALUE self, VALUE other) {
  ip6_t *a, *b;

  assert_kind_of(other, IP6);

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

  return ip6_new(IP6, ip6_xor(*a, *b));
}

#hashInteger

Returns:

  • (Integer)


723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'ext/subnets/ext.c', line 723

VALUE
method_ip6_hash(VALUE self) {
  ip6_t *ip;
  VALUE ret;

  Data_Get_Struct(self, ip6_t, ip);

  ret = hash(INT2FIX(ip->x[0]));
  for (int i=1; i<8; i++) {
    ret = xor(ret, hash(INT2FIX(ip->x[i])));
  }
  return ret;
}

#hextetsArray<Fixnum>

Returns 16-bit hextets, most significant first.

Returns:

  • (Array<Fixnum>)

    16-bit hextets, most significant first



793
794
795
796
797
798
799
800
801
802
803
804
805
# File 'ext/subnets/ext.c', line 793

VALUE
method_ip6_hextets(VALUE self) {
  ip6_t *ip;
  VALUE hextets;

  Data_Get_Struct(self, ip6_t, ip);

  hextets = rb_ary_new();
  for (int i=0; i<8; i++) {
    rb_ary_push(hextets, INT2FIX(ip->x[i]));
  }
  return hextets;
}

#to_iNumeric

Returns the 128 bit integer representing this address.

Returns:

  • (Numeric)

    the 128 bit integer representing this address



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'ext/subnets/ext.c', line 597

VALUE
method_ip6_to_i(VALUE self) {
  VALUE ret;
  ip6_t *ip;
  ID lshift, plus;

  Data_Get_Struct(self, ip6_t, ip);

  lshift = rb_intern("<<");
  plus = rb_intern("+");

  ret = RB_INT2NUM(0);

  for (int i=0; i<8; i++) {
    VALUE hextet = RB_UINT2NUM(ip->x[i]);
    VALUE inc = rb_funcall(hextet, lshift, 1, RB_INT2NUM(16*(7-i)));
    ret = rb_funcall(ret, plus, 1, inc);
  }

  return ret;
}

#to_sString

Return a String of colon-separated hexadecimal parts with multiple zeros compresses with a double-colon.

Returns:

  • (String)


544
545
546
547
548
549
550
551
552
# File 'ext/subnets/ext.c', line 544

VALUE
method_ip6_to_s(VALUE self) {
  ip6_t *ip;
  char buf[64];

  Data_Get_Struct(self, ip6_t, ip);
  ip6_snprint(*ip, buf, 64);
  return rb_str_new2(buf);
}

#|(other) ⇒ Subnets::IP4

Returns bitwise OR of self and other.

Parameters:

Returns:



366
367
368
369
370
371
372
373
374
375
376
# File 'ext/subnets/ext.c', line 366

VALUE
method_ip6_bor(VALUE self, VALUE other) {
  ip6_t *a, *b;

  assert_kind_of(other, IP6);

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

  return ip6_new(IP6, ip6_bor(*a, *b));
}

#~Subnets::IP6

Returns bitwise NOT of self.

Returns:



355
356
357
358
359
360
# File 'ext/subnets/ext.c', line 355

VALUE
method_ip6_not(VALUE self) {
  ip6_t *ip;
  Data_Get_Struct(self, ip6_t, ip);
  return ip6_new(IP6, ip6_not(*ip));
}