Module: Subnets
- Defined in:
- ext/subnets/ext.c
Defined Under Namespace
Classes: IP, IP4, IP6, Net, Net4, Net6, ParseError
Class Method Summary collapse
-
.include?(nets, v) ⇒ Boolean
Test if any element in
nets
includesv
. -
.parse(str) ⇒ Net4, ...
Try parsing
str
as Net4, Net6, IP4, IP6.
Class Method Details
.include?(nets, v) ⇒ Boolean
Test if any element in nets
includes v
. For array elements obj
that are not Net4 or Net6, calls obj#===(v) to test for inclusion.
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 |
# File 'ext/subnets/ext.c', line 936
VALUE
method_subnets_include_p(VALUE self, VALUE nets, VALUE v) {
int is_net4 = 0, is_net6 = 0, is_ip4 = 0, is_ip6 = 0;
net4_t net4;
net6_t net6;
ip4_t ip4;
ip6_t ip6;
if (CLASS_OF(v) == IP4) {
ip4_t *_ip4;
is_ip4 = !0;
Data_Get_Struct(v, ip4_t, _ip4);
ip4 = *_ip4;
} else if (CLASS_OF(v) == IP6) {
ip6_t *_ip6;
is_ip6 = !0;
Data_Get_Struct(v, ip6_t, _ip6);
ip6 = *_ip6;
} else if (CLASS_OF(v) == Net4) {
net4_t *_net4;
is_net4 = !0;
Data_Get_Struct(v, net4_t, _net4);
net4 = *_net4;
} else if (CLASS_OF(v) == Net6) {
net6_t *_net6;
is_net6 = !0;
Data_Get_Struct(v, net6_t, _net6);
net6 = *_net6;
} else {
const char *buf = StringValueCStr(v);
if (read_net4_strict(buf, &net4)) is_net4 = !0;
else if (read_net6_strict(buf, &net6)) is_net6 = !0;
else if (read_ip4_strict(buf, &ip4)) is_ip4 = !0;
else if (read_ip6_strict(buf, &ip6)) is_ip6 = !0;
}
for (ssize_t i = 0; i < RARRAY_LEN(nets); i++) {
VALUE rbnet = RARRAY_AREF(nets, i);
if (CLASS_OF(rbnet) == Net4) {
if (is_net4) {
net4_t *net;
Data_Get_Struct(rbnet, net4_t, net);
if (net4_include_net4_p(*net, net4)) {
return Qtrue;
}
} else if (is_ip4) {
net4_t *net;
Data_Get_Struct(rbnet, net4_t, net);
if (net4_include_p(*net, ip4)) {
return Qtrue;
}
}
}
else if (CLASS_OF(rbnet) == Net6) {
if (is_net6) {
net6_t *net;
Data_Get_Struct(rbnet, net6_t, net);
if (net6_include_net6_p(*net, net6)) {
return Qtrue;
}
} else if (is_ip6) {
net6_t *net;
Data_Get_Struct(rbnet, net6_t, net);
if (net6_include_p(*net, ip6)) {
return Qtrue;
}
}
}
else {
VALUE ret = rb_funcall(rbnet, rb_intern("==="), 1, v);
if (RTEST(ret)) return Qtrue;
}
}
return Qfalse;
}
|
.parse(str) ⇒ Net4, ...
Try parsing str
as Net4, Net6, IP4, IP6.
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 |
# File 'ext/subnets/ext.c', line 900
VALUE
method_subnets_parse(VALUE mod, VALUE str) {
const char *s;
str = StringValue(str);
s = StringValueCStr(str);
{
net4_t net;
if (read_net4_strict(s, &net)) return net4_new(Net4, net);
}
{
net6_t net;
if (read_net6_strict(s, &net)) return net6_new(Net6, net);
}
{
ip4_t ip;
if (read_ip4_strict(s, &ip)) return ip4_new(IP4, ip);
}
{
ip6_t ip;
if (read_ip6_strict(s, &ip)) return ip6_new(IP6, ip);
}
raise_parse_error("{v4,v6}{net,ip}", s);
return Qnil;
}
|