Class: Subnets::IP6
Class Method Summary collapse
- .new(hextets) ⇒ Object
-
.random(rand = Random.new, opts = {}) ⇒ IP6
A random IP6 address.
Instance Method Summary collapse
-
#&(other) ⇒ Subnets::IP4
Bitwise AND of
selfandother. - #==(other) ⇒ Boolean (also: #eql?)
-
#^(other) ⇒ Subnets::IP4
Bitwise XOR of
selfandother. - #hash ⇒ Integer
-
#hextets ⇒ Array<Fixnum>
16-bit hextets, most significant first.
-
#to_i ⇒ Numeric
The 128 bit integer representing this address.
-
#to_s ⇒ String
Return a String of colon-separated hexadecimal parts with multiple zeros compresses with a double-colon.
-
#|(other) ⇒ Subnets::IP4
Bitwise OR of
selfandother. -
#~ ⇒ Subnets::IP6
Bitwise NOT of
self.
Methods inherited from IP
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.
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.
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?
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.
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));
}
|
#hash ⇒ 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;
}
|
#hextets ⇒ Array<Fixnum>
Returns 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_i ⇒ Numeric
Returns 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_s ⇒ String
Return a String of colon-separated hexadecimal parts with multiple zeros compresses with a double-colon.
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.
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.
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)); } |