ipaddress 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/dist/src/crunchy.d.ts +130 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +616 -0
  4. package/dist/src/index.d.ts +13 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +21 -0
  7. package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
  8. package/dist/src/ip_bits.d.ts.map +1 -0
  9. package/dist/src/ip_bits.js +133 -0
  10. package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
  11. package/dist/src/ip_version.d.ts.map +1 -0
  12. package/dist/src/ip_version.js +6 -0
  13. package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1243 -0
  16. package/dist/src/ipv4.d.ts +15 -0
  17. package/dist/src/ipv4.d.ts.map +1 -0
  18. package/dist/src/ipv4.js +187 -0
  19. package/dist/src/ipv6.d.ts +13 -0
  20. package/dist/src/ipv6.d.ts.map +1 -0
  21. package/dist/src/ipv6.js +190 -0
  22. package/dist/src/ipv6_loopback.d.ts +6 -0
  23. package/dist/src/ipv6_loopback.d.ts.map +1 -0
  24. package/dist/src/ipv6_loopback.js +51 -0
  25. package/dist/src/ipv6_mapped.d.ts +6 -0
  26. package/dist/src/ipv6_mapped.d.ts.map +1 -0
  27. package/dist/src/ipv6_mapped.js +159 -0
  28. package/dist/src/ipv6_unspec.d.ts +6 -0
  29. package/dist/src/ipv6_unspec.d.ts.map +1 -0
  30. package/dist/src/ipv6_unspec.js +57 -0
  31. package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
  32. package/dist/src/prefix.d.ts.map +1 -0
  33. package/dist/src/prefix.js +142 -0
  34. package/dist/src/prefix128.d.ts +7 -0
  35. package/dist/src/prefix128.d.ts.map +1 -0
  36. package/dist/src/prefix128.js +39 -0
  37. package/dist/src/prefix32.d.ts +7 -0
  38. package/dist/src/prefix32.d.ts.map +1 -0
  39. package/dist/src/prefix32.js +29 -0
  40. package/dist/src/rle.d.ts +19 -0
  41. package/dist/src/rle.d.ts.map +1 -0
  42. package/dist/src/rle.js +73 -0
  43. package/dist/test/test_crunchy_impls.d.ts +2 -0
  44. package/dist/test/test_crunchy_impls.d.ts.map +1 -0
  45. package/dist/test/test_crunchy_impls.js +150 -0
  46. package/dist/test/test_crunchy_with32bit.d.ts +2 -0
  47. package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
  48. package/dist/test/test_crunchy_with32bit.js +382 -0
  49. package/dist/test/test_ipaddress.d.ts +2 -0
  50. package/dist/test/test_ipaddress.d.ts.map +1 -0
  51. package/dist/test/test_ipaddress.js +182 -0
  52. package/dist/test/test_ipv4.d.ts +2 -0
  53. package/dist/test/test_ipv4.d.ts.map +1 -0
  54. package/dist/test/test_ipv4.js +561 -0
  55. package/dist/test/test_ipv6.d.ts +2 -0
  56. package/dist/test/test_ipv6.d.ts.map +1 -0
  57. package/dist/test/test_ipv6.js +313 -0
  58. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  59. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_loopback.js +40 -0
  61. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  62. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_mapped.js +89 -0
  64. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  65. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  66. package/dist/test/test_ipv6_unspec.js +39 -0
  67. package/dist/test/test_prefix128.d.ts +2 -0
  68. package/dist/test/test_prefix128.d.ts.map +1 -0
  69. package/dist/test/test_prefix128.js +35 -0
  70. package/dist/test/test_prefix32.d.ts +2 -0
  71. package/dist/test/test_prefix32.d.ts.map +1 -0
  72. package/dist/test/test_prefix32.js +126 -0
  73. package/dist/test/test_rle.d.ts +2 -0
  74. package/dist/test/test_rle.d.ts.map +1 -0
  75. package/dist/test/test_rle.js +141 -0
  76. package/dist/vitest.config.d.ts +3 -0
  77. package/dist/vitest.config.d.ts.map +1 -0
  78. package/dist/vitest.config.js +11 -0
  79. package/package.json +41 -37
  80. package/src/crunchy.ts +731 -0
  81. package/src/index.ts +23 -0
  82. package/src/ip_bits.ts +148 -0
  83. package/src/ip_version.ts +6 -0
  84. package/src/ipaddress.ts +1347 -0
  85. package/src/ipv4.ts +203 -0
  86. package/src/ipv6.ts +202 -0
  87. package/src/ipv6_loopback.ts +55 -0
  88. package/src/ipv6_mapped.ts +163 -0
  89. package/src/ipv6_unspec.ts +59 -0
  90. package/src/prefix.ts +170 -0
  91. package/src/prefix128.ts +42 -0
  92. package/src/prefix32.ts +32 -0
  93. package/src/rle.ts +85 -0
  94. package/test/test_crunchy_impls.ts +179 -0
  95. package/test/test_crunchy_with32bit.ts +510 -0
  96. package/test/test_ipaddress.ts +208 -0
  97. package/test/test_ipv4.ts +596 -0
  98. package/test/test_ipv6.ts +325 -0
  99. package/test/test_ipv6_loopback.ts +52 -0
  100. package/test/test_ipv6_mapped.ts +101 -0
  101. package/test/test_ipv6_unspec.ts +51 -0
  102. package/test/test_prefix128.ts +39 -0
  103. package/test/test_prefix32.ts +130 -0
  104. package/test/test_rle.ts +144 -0
  105. package/LICENSE +0 -20
  106. package/README.md +0 -960
  107. package/crunchy.d.ts +0 -55
  108. package/index.d.ts +0 -12
  109. package/ipaddress.js +0 -2869
  110. package/ipaddress.js.map +0 -1
  111. package/ipv4.d.ts +0 -14
  112. package/ipv6.d.ts +0 -12
  113. package/ipv6_loopback.d.ts +0 -5
  114. package/ipv6_mapped.d.ts +0 -5
  115. package/ipv6_unspec.d.ts +0 -5
  116. package/prefix128.d.ts +0 -6
  117. package/prefix32.d.ts +0 -6
  118. package/rle.d.ts +0 -14
package/src/ipv4.ts ADDED
@@ -0,0 +1,203 @@
1
+ import Crunchy from "./crunchy.js";
2
+ import Prefix32 from "./prefix32.js";
3
+ import IPAddress from "./ipaddress.js";
4
+ import IpBits from "./ip_bits.js";
5
+ import Prefix128 from "./prefix128.js";
6
+ import Ipv6 from "./ipv6.js";
7
+ import Prefix from "./prefix.js";
8
+
9
+ function prefix32_or_throw(num: number): Prefix {
10
+ const ret = Prefix32.create(num);
11
+ if (ret === null) {
12
+ throw new Error(`invalid ipv4 prefix: ${num}`);
13
+ }
14
+ return ret;
15
+ }
16
+
17
+ export const Ipv4 = {
18
+ from_number(addr: Crunchy, prefix_num: number): IPAddress | null {
19
+ const prefix = Prefix32.create(prefix_num);
20
+ if (!prefix) {
21
+ return null;
22
+ }
23
+ return new IPAddress({
24
+ ip_bits: IpBits.v4(),
25
+ host_address: addr.clone(),
26
+ prefix,
27
+ mapped: null,
28
+ vt_is_private: Ipv4.ipv4_is_private,
29
+ vt_is_loopback: Ipv4.ipv4_is_loopback,
30
+ vt_to_ipv6: Ipv4.to_ipv6,
31
+ });
32
+ },
33
+
34
+ create(str: string): IPAddress | null {
35
+ // console.log("create:v4:", str);
36
+ // let enable = str == "0.0.0.0/0";
37
+ const tmp = IPAddress.split_at_slash(str);
38
+ const ip = tmp[0];
39
+ const netmask = tmp[1];
40
+ if (!IPAddress.is_valid_ipv4(ip)) {
41
+ // enable && console.log("xx1");
42
+ return null;
43
+ }
44
+ let ip_prefix_num: number | null = 32;
45
+ if (netmask) {
46
+ // netmask is defined
47
+ ip_prefix_num = IPAddress.parse_netmask_to_prefix(netmask);
48
+ if (ip_prefix_num === null) {
49
+ // enable && console.log("xx2");
50
+ return null;
51
+ }
52
+ // if ip_prefix.ip_bits.version
53
+ }
54
+ const ip_prefix = Prefix32.create(ip_prefix_num);
55
+ if (ip_prefix === null) {
56
+ // enable && console.log("xx3");
57
+ return null;
58
+ }
59
+ const split_number = IPAddress.split_to_u32(ip);
60
+ if (split_number === null) {
61
+ // enable && console.log("xx4");
62
+ return null;
63
+ }
64
+ // console.log(">>>>>>>", ip, ip_prefix);
65
+ return new IPAddress({
66
+ ip_bits: IpBits.v4(),
67
+ host_address: split_number,
68
+ prefix: ip_prefix,
69
+ mapped: null,
70
+ vt_is_private: Ipv4.ipv4_is_private,
71
+ vt_is_loopback: Ipv4.ipv4_is_loopback,
72
+ vt_to_ipv6: Ipv4.to_ipv6,
73
+ });
74
+ },
75
+
76
+ ipv4_is_private(my: IPAddress): boolean {
77
+ return (
78
+ [
79
+ IPAddress.parse_or_throw("10.0.0.0/8"),
80
+ IPAddress.parse_or_throw("169.254.0.0/16"),
81
+ IPAddress.parse_or_throw("172.16.0.0/12"),
82
+ IPAddress.parse_or_throw("192.168.0.0/16"),
83
+ ].find((i) => i.includes(my)) != null
84
+ );
85
+ },
86
+
87
+ ipv4_is_loopback(my: IPAddress): boolean {
88
+ return IPAddress.parse_or_throw("127.0.0.0/8").includes(my);
89
+ },
90
+
91
+ to_ipv6(ia: IPAddress): IPAddress {
92
+ const prefix = Prefix128.create(ia.prefix.num);
93
+ if (prefix === null) {
94
+ throw new Error(`invalid ipv6 prefix: ${ia.prefix.num}`);
95
+ }
96
+ return new IPAddress({
97
+ ip_bits: IpBits.v6(),
98
+ host_address: ia.host_address.clone(),
99
+ prefix,
100
+ mapped: null,
101
+ vt_is_private: Ipv6.ipv6_is_private,
102
+ vt_is_loopback: Ipv6.ipv6_is_loopback,
103
+ vt_to_ipv6: Ipv6.to_ipv6,
104
+ });
105
+ },
106
+
107
+ // Checks whether the ip address belongs to a
108
+ // RFC 791 CLASS A network, no matter
109
+ // what the subnet mask is.
110
+ //
111
+ // Example:
112
+ //
113
+ // ip = IPAddress("10.0.0.1/24")
114
+ //
115
+ // ip.a?
116
+ // // => true
117
+ //
118
+ is_class_a(my: IPAddress): boolean {
119
+ // console.log("is_class_a:", my.to_string(), Crunchy.from_string_or_throw("80000000", 16), my.is_ipv4());
120
+ return my.is_ipv4() && my.host_address.lt(Crunchy.from_string_or_throw("80000000", 16));
121
+ },
122
+
123
+ // Checks whether the ip address belongs to a
124
+ // RFC 791 CLASS B network, no matter
125
+ // what the subnet mask is.
126
+ //
127
+ // Example:
128
+ //
129
+ // ip = IPAddress("172.16.10.1/24")
130
+ //
131
+ // ip.b?
132
+ // // => true
133
+ //
134
+ is_class_b(my: IPAddress): boolean {
135
+ return (
136
+ my.is_ipv4() &&
137
+ Crunchy.from_string_or_throw("80000000", 16).lte(my.host_address) &&
138
+ my.host_address.lt(Crunchy.from_string_or_throw("c0000000", 16))
139
+ );
140
+ },
141
+
142
+ // Checks whether the ip address belongs to a
143
+ // RFC 791 CLASS C network, no matter
144
+ // what the subnet mask is.
145
+ //
146
+ // Example:
147
+ //
148
+ // ip = IPAddress("192.168.1.1/30")
149
+ //
150
+ // ip.c?
151
+ // // => true
152
+ //
153
+ is_class_c(my: IPAddress): boolean {
154
+ return (
155
+ my.is_ipv4() &&
156
+ Crunchy.from_string_or_throw("c0000000", 16).lte(my.host_address) &&
157
+ my.host_address.lt(Crunchy.from_string_or_throw("e0000000", 16))
158
+ );
159
+ },
160
+
161
+ // Creates a new IPv4 address object by parsing the
162
+ // address in a classful way.
163
+ //
164
+ // Classful addresses have a fixed netmask based on the
165
+ // class they belong to:
166
+ //
167
+ // * Class A, from 0.0.0.0 to 127.255.255.255
168
+ // * Class B, from 128.0.0.0 to 191.255.255.255
169
+ // * Class C, D and E, from 192.0.0.0 to 255.255.255.254
170
+ //
171
+ // Example:
172
+ //
173
+ // ip = IPAddress::IPv4.parse_classful "10.0.0.1"
174
+ //
175
+ // ip.netmask
176
+ // // => "255.0.0.0"
177
+ // ip.a?
178
+ // // => true
179
+ //
180
+ // Note that classes C, D and E will all have a default
181
+ // prefix of /24 or 255.255.255.0
182
+ //
183
+ parse_classful(ip_si: string): IPAddress | null {
184
+ if (!IPAddress.is_valid_ipv4(ip_si)) {
185
+ return null;
186
+ }
187
+ const o_ip = IPAddress.parse(ip_si);
188
+ if (o_ip === null) {
189
+ return o_ip;
190
+ }
191
+ const ip = o_ip;
192
+ if (Ipv4.is_class_a(ip)) {
193
+ ip.prefix = prefix32_or_throw(8);
194
+ } else if (Ipv4.is_class_b(ip)) {
195
+ ip.prefix = prefix32_or_throw(16);
196
+ } else if (Ipv4.is_class_c(ip)) {
197
+ ip.prefix = prefix32_or_throw(24);
198
+ }
199
+ return ip;
200
+ },
201
+ };
202
+
203
+ export default Ipv4;
package/src/ipv6.ts ADDED
@@ -0,0 +1,202 @@
1
+ import IpBits from "./ip_bits.js";
2
+ import IPAddress from "./ipaddress.js";
3
+ import Ipv4 from "./ipv4.js";
4
+ import Prefix128 from "./prefix128.js";
5
+ import Crunchy from "./crunchy.js";
6
+
7
+ export const Ipv6 = {
8
+ // =Name
9
+ //
10
+ // IPAddress::IPv6 - IP version 6 address manipulation library
11
+ //
12
+ // =Synopsis
13
+ //
14
+ // require 'ipaddress'
15
+ //
16
+ // =Description
17
+ //
18
+ // Class IPAddress::IPv6 is used to handle IPv6 type addresses.
19
+ //
20
+ // == IPv6 addresses
21
+ //
22
+ // IPv6 addresses are 128 bits long, in contrast with IPv4 addresses
23
+ // which are only 32 bits long. An IPv6 address is generally written as
24
+ // eight groups of four hexadecimal digits, each group representing 16
25
+ // bits or two octect. For example, the following is a valid IPv6
26
+ // address:
27
+ //
28
+ // 2001:0db8:0000:0000:0008:0800:200c:417a
29
+ //
30
+ // Letters in an IPv6 address are usually written downcase, as per
31
+ // RFC. You can create a new IPv6 object using uppercase letters, but
32
+ // they will be converted.
33
+ //
34
+ // === Compression
35
+ //
36
+ // Since IPv6 addresses are very long to write, there are some
37
+ // semplifications and compressions that you can use to shorten them.
38
+ //
39
+ // * Leading zeroes: all the leading zeroes within a group can be
40
+ // omitted: "0008" would become "8"
41
+ //
42
+ // * A string of consecutive zeroes can be replaced by the string
43
+ // "::". This can be only applied once.
44
+ //
45
+ // Using compression, the IPv6 address written above can be shorten into
46
+ // the following, equivalent, address
47
+ //
48
+ // 2001:db8::8:800:200c:417a
49
+ //
50
+ // This short version is often used in human representation.
51
+ //
52
+ // === Network Mask
53
+ //
54
+ // As we used to do with IPv4 addresses, an IPv6 address can be written
55
+ // using the prefix notation to specify the subnet mask:
56
+ //
57
+ // 2001:db8::8:800:200c:417a/64
58
+ //
59
+ // The /64 part means that the first 64 bits of the address are
60
+ // representing the network portion, and the last 64 bits are the host
61
+ // portion.
62
+ //
63
+ //
64
+
65
+ from_str(str: string, radix: number, prefix: number): IPAddress | null {
66
+ const num = Crunchy.from_string(str, radix);
67
+ if (!num) {
68
+ return null;
69
+ }
70
+ return Ipv6.from_int(num, prefix);
71
+ },
72
+
73
+ enhance_if_mapped(ip: IPAddress): IPAddress | null {
74
+ // console.log("------A");
75
+ // println!("real mapped {:x} {:x}", &ip.host_address, ip.host_address.clone().shr(32));
76
+ if (ip.is_mapped()) {
77
+ // console.log("------B");
78
+ return ip;
79
+ }
80
+ // console.log("------C", ip);
81
+ const ipv6_top_96bit = ip.host_address.shr(32);
82
+ // console.log("------D", ip);
83
+ if (ipv6_top_96bit.eq(Crunchy.from_number(0xffff))) {
84
+ // console.log("------E");
85
+ const num = ip.host_address.mod(Crunchy.one().shl(32));
86
+ // console.log("------F");
87
+ if (num.eq(Crunchy.zero())) {
88
+ return ip;
89
+ }
90
+ // println!("ip:{},{:x}", ip.to_string(), num);
91
+ const ipv4_bits = IpBits.v4();
92
+ if (ipv4_bits.bits < ip.prefix.host_prefix()) {
93
+ // println!("enhance_if_mapped-2:{}:{}", ip.to_string(), ip.prefix.host_prefix());
94
+ return null;
95
+ }
96
+ // console.log("------G");
97
+ const mapped = Ipv4.from_number(num, ipv4_bits.bits - ip.prefix.host_prefix());
98
+ // console.log("------H");
99
+ if (!mapped) {
100
+ // println!("enhance_if_mapped-3");
101
+ return mapped;
102
+ }
103
+ // println!("real mapped!!!!!={}", mapped.clone().to_string());
104
+ ip.mapped = mapped;
105
+ }
106
+ return ip;
107
+ },
108
+
109
+ from_int(adr: Crunchy, prefix_num: number): IPAddress | null {
110
+ const prefix = Prefix128.create(prefix_num);
111
+ if (prefix === null) {
112
+ return null;
113
+ }
114
+ const ret = Ipv6.enhance_if_mapped(
115
+ new IPAddress({
116
+ ip_bits: IpBits.v6(),
117
+ host_address: adr.clone(),
118
+ prefix,
119
+ mapped: null,
120
+ vt_is_private: Ipv6.ipv6_is_private,
121
+ vt_is_loopback: Ipv6.ipv6_is_loopback,
122
+ vt_to_ipv6: Ipv6.to_ipv6,
123
+ }),
124
+ );
125
+ // console.log("from_int:", adr, prefix, ret);
126
+ return ret;
127
+ },
128
+
129
+ // Creates a new IPv6 address object.
130
+ //
131
+ // An IPv6 address can be expressed in any of the following forms:
132
+ //
133
+ // * "2001:0db8:0000:0000:0008:0800:200C:417A": IPv6 address with no compression
134
+ // * "2001:db8:0:0:8:800:200C:417A": IPv6 address with leading zeros compression
135
+ // * "2001:db8::8:800:200C:417A": IPv6 address with full compression
136
+ //
137
+ // In all these 3 cases, a new IPv6 address object will be created, using the default
138
+ // subnet mask /128
139
+ //
140
+ // You can also specify the subnet mask as with IPv4 addresses:
141
+ //
142
+ // ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
143
+ //
144
+ create(str: string): IPAddress | null {
145
+ // console.log("1>>>>>>>>>", str);
146
+ const [ip, o_netmask] = IPAddress.split_at_slash(str);
147
+ // console.log("2>>>>>>>>>", str);
148
+ if (IPAddress.is_valid_ipv6(ip)) {
149
+ // console.log("3>>>>>>>>>", str);
150
+ const o_num = IPAddress.split_to_num(ip);
151
+ if (o_num === null) {
152
+ // console.log("ipv6_create-1", str);
153
+ return null;
154
+ }
155
+ // console.log("4>>>>>>>>>", str);
156
+ let netmask: number | null = 128;
157
+ if (o_netmask !== null) {
158
+ netmask = IPAddress.parse_dec_str(o_netmask);
159
+ if (netmask === null) {
160
+ // console.log("ipv6_create-2", str);
161
+ return null;
162
+ }
163
+ }
164
+ // console.log("5>>>>>>>>>", str);
165
+ const prefix = Prefix128.create(netmask);
166
+ if (prefix === null) {
167
+ // console.log("ipv6_create-3", str);
168
+ return null;
169
+ }
170
+ // console.log("6>>>>>>>>>", str, prefix.num, o_netmask, netmask);
171
+ return Ipv6.enhance_if_mapped(
172
+ new IPAddress({
173
+ ip_bits: IpBits.v6(),
174
+ host_address: o_num.crunchy,
175
+ prefix,
176
+ mapped: null,
177
+ vt_is_private: Ipv6.ipv6_is_private,
178
+ vt_is_loopback: Ipv6.ipv6_is_loopback,
179
+ vt_to_ipv6: Ipv6.to_ipv6,
180
+ }),
181
+ );
182
+ } else {
183
+ // console.log("ipv6_create-4", str);
184
+ return null;
185
+ }
186
+ }, // pub fn initialize
187
+
188
+ to_ipv6(ia: IPAddress): IPAddress {
189
+ return ia.clone();
190
+ },
191
+
192
+ ipv6_is_loopback(my: IPAddress): boolean {
193
+ // console.log("*************", my.host_address, Crunchy.one());
194
+ return my.host_address.eq(Crunchy.one());
195
+ },
196
+
197
+ ipv6_is_private(my: IPAddress): boolean {
198
+ return IPAddress.parse_or_throw("fd00::/8").includes(my);
199
+ },
200
+ };
201
+
202
+ export default Ipv6;
@@ -0,0 +1,55 @@
1
+ import IPAddress from "./ipaddress.js";
2
+ import Ipv6 from "./ipv6.js";
3
+
4
+ import Crunchy from "./crunchy.js";
5
+
6
+ export const Ipv6Loopback = {
7
+ // The loopback address is a unicast localhost address. If an
8
+ // application in a host sends packets to this address, the IPv6 stack
9
+ // will loop these packets back on the same virtual interface.
10
+ //
11
+ // Loopback addresses are expressed in the following form:
12
+ //
13
+ // ::1
14
+ //
15
+ // or, with their appropriate prefix,
16
+ //
17
+ // ::1/128
18
+ //
19
+ // As for the unspecified addresses, IPv6 loopbacks can be created with
20
+ // IPAddress calling their own class:
21
+ //
22
+ // ip = IPAddress::IPv6::Loopback.new
23
+ //
24
+ // ip.to_string
25
+ // // => "::1/128"
26
+ //
27
+ // or by using the wrapper:
28
+ //
29
+ // ip = IPAddress "::1"
30
+ //
31
+ // ip.to_string
32
+ // // => "::1/128"
33
+ //
34
+ // Checking if an address is loopback is easy with the IPv6// loopback?
35
+ // method:
36
+ //
37
+ // ip.loopback?
38
+ // // => true
39
+ //
40
+ // The IPv6 loopback address corresponds to 127.0.0.1 in IPv4.
41
+ //
42
+ //
43
+ // Creates a new IPv6 unspecified address
44
+ //
45
+ // ip = IPAddress::IPv6::Loopback.new
46
+ //
47
+ // ip.to_string
48
+ // // => "::1/128"
49
+ //
50
+ create(): IPAddress | null {
51
+ return Ipv6.from_int(Crunchy.one(), 128);
52
+ },
53
+ };
54
+
55
+ export default Ipv6Loopback;
@@ -0,0 +1,163 @@
1
+ import Crunchy from "./crunchy.js";
2
+ import IpBits from "./ip_bits.js";
3
+ import IPAddress from "./ipaddress.js";
4
+
5
+ export const Ipv6Mapped = {
6
+ // Ac
7
+ // It is usually identified as a IPv4 mapped IPv6 address, a particular
8
+ // IPv6 address which aids the transition from IPv4 to IPv6. The
9
+ // structure of the address is
10
+ //
11
+ // ::ffff:w.y.x.z
12
+ //
13
+ // where w.x.y.z is a normal IPv4 address. For example, the following is
14
+ // a mapped IPv6 address:
15
+ //
16
+ // ::ffff:192.168.100.1
17
+ //
18
+ // IPAddress is very powerful in handling mapped IPv6 addresses, as the
19
+ // IPv4 portion is stored internally as a normal IPv4 object. Let's have
20
+ // a look at some examples. To create a new mapped address, just use the
21
+ // class builder itself
22
+ //
23
+ // ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
24
+ //
25
+ // or just use the wrapper method
26
+ //
27
+ // ip6 = IPAddress "::ffff:172.16.10.1/128"
28
+ //
29
+ // Let's check it's really a mapped address:
30
+ //
31
+ // ip6.mapped?
32
+ // // => true
33
+ //
34
+ // ip6.to_string
35
+ // // => "::FFFF:172.16.10.1/128"
36
+ //
37
+ // Now with the +ipv4+ attribute, we can easily access the IPv4 portion
38
+ // of the mapped IPv6 address:
39
+ //
40
+ // ip6.ipv4.address
41
+ // // => "172.16.10.1"
42
+ //
43
+ // Internally, the IPv4 address is stored as two 16 bits
44
+ // groups. Therefore all the usual methods for an IPv6 address are
45
+ // working perfectly fine:
46
+ //
47
+ // ip6.to_hex
48
+ // // => "00000000000000000000ffffac100a01"
49
+ //
50
+ // ip6.address
51
+ // // => "0000:0000:0000:0000:0000:ffff:ac10:0a01"
52
+ //
53
+ // A mapped IPv6 can also be created just by specify the address in the
54
+ // following format:
55
+ //
56
+ // ip6 = IPAddress "::172.16.10.1"
57
+ //
58
+ // That is, two colons and the IPv4 address. However, as by RFC, the ffff
59
+ // group will be automatically added at the beginning
60
+ //
61
+ // ip6.to_string
62
+ // => "::ffff:172.16.10.1/128"
63
+ //
64
+ // making it a mapped IPv6 compatible address.
65
+ //
66
+ //
67
+ // Creates a new IPv6 IPv4-mapped address
68
+ //
69
+ // ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
70
+ //
71
+ // ipv6.ipv4.class
72
+ // // => IPAddress::IPv4
73
+ //
74
+ // An IPv6 IPv4-mapped address can also be created using the
75
+ // IPv6 only format of the address:
76
+ //
77
+ // ip6 = IPAddress::IPv6::Mapped.new "::0d01:4403"
78
+ //
79
+ // ip6.to_string
80
+ // // => "::ffff:13.1.68.3"
81
+ //
82
+ create(str: string): IPAddress | null {
83
+ // console.log("mapped-1");
84
+ const i = IPAddress.split_at_slash(str);
85
+ const ip = i[0];
86
+ const o_netmask = i[1];
87
+ const split_colon = ip.split(":");
88
+ if (split_colon.length <= 1) {
89
+ // console.log("mapped-2");
90
+ return null;
91
+ }
92
+ // if split_colon.get(0).unwrap().len() > 0 {
93
+ // // println!("---1a");
94
+ // return Err(format!("not mapped format-2: {}", &str));
95
+ // }
96
+ // let mapped: Option<IPAddress> = None;
97
+ let netmask = "";
98
+ if (o_netmask !== null) {
99
+ netmask = `/${o_netmask}`;
100
+ }
101
+ const ipv4_str = split_colon[split_colon.length - 1];
102
+ if (IPAddress.is_valid_ipv4(ipv4_str)) {
103
+ const ipv4 = IPAddress.parse(`${ipv4_str}${netmask}`);
104
+ if (ipv4 === null) {
105
+ // console.log("mapped-3");
106
+ return ipv4;
107
+ }
108
+ // mapped = Some(ipv4.unwrap());
109
+ const addr = ipv4;
110
+ const ipv6_bits = IpBits.v6();
111
+ const part_mod = Crunchy.from_number(ipv6_bits.part_mod);
112
+ const up_addr = addr.host_address.clone();
113
+ const down_addr = addr.host_address.clone();
114
+
115
+ let rebuild_ipv6 = "";
116
+ let colon = "";
117
+ for (let i = 0; i < split_colon.length - 1; ++i) {
118
+ rebuild_ipv6 += colon;
119
+ rebuild_ipv6 += split_colon[i];
120
+ colon = ":";
121
+ }
122
+ rebuild_ipv6 += colon;
123
+ const high_part = up_addr.shr(IpBits.v6().part_bits).mod(part_mod).toString(16);
124
+ const low_part = down_addr.mod(part_mod).toString(16);
125
+ const bits = ipv6_bits.bits - addr.prefix.host_prefix();
126
+ const rebuild_ipv4 = `${high_part}:${low_part}/${bits}`;
127
+ rebuild_ipv6 += rebuild_ipv4;
128
+
129
+ // console.log("-----A", rebuild_ipv6, part_mod);
130
+ const r_ipv6 = IPAddress.parse(rebuild_ipv6);
131
+ if (r_ipv6 === null) {
132
+ // println!("---3|{}", &rebuild_ipv6);
133
+ // console.log("mapped-4");
134
+ return r_ipv6;
135
+ }
136
+ if (r_ipv6.is_mapped()) {
137
+ // console.log("mapped-5");
138
+ return r_ipv6;
139
+ }
140
+ const ipv6 = r_ipv6;
141
+ const p96bit = ipv6.host_address.shr(32);
142
+ if (!p96bit.eq(Crunchy.zero())) {
143
+ // println!("---4|{}", &rebuild_ipv6);
144
+ // console.log("mapped-6",ipv6.host_address, p96bit, Crunchy.zero());
145
+ return null;
146
+ }
147
+ {
148
+ const r_ipv6 = IPAddress.parse(`::ffff:${rebuild_ipv4}`);
149
+ if (r_ipv6 === null) {
150
+ // println!("---3|{}", &rebuild_ipv6);
151
+ // console.log("mapped-7");
152
+ return null;
153
+ }
154
+ // console.log("mapped-8");
155
+ return r_ipv6;
156
+ }
157
+ }
158
+ // console.log("mapped-9");
159
+ return null;
160
+ },
161
+ };
162
+
163
+ export default Ipv6Mapped;
@@ -0,0 +1,59 @@
1
+ import IPAddress from "./ipaddress.js";
2
+
3
+ export const Ipv6Unspec = {
4
+ // The address with all zero bits is called the +unspecified+ address
5
+ // (corresponding to 0.0.0.0 in IPv4). It should be something like this:
6
+ //
7
+ // 0000:0000:0000:0000:0000:0000:0000:0000
8
+ //
9
+ // but, with the use of compression, it is usually written as just two
10
+ // colons:
11
+ //
12
+ // ::
13
+ //
14
+ // or, specifying the netmask:
15
+ //
16
+ // ::/128
17
+ //
18
+ // With IPAddress, create a new unspecified IPv6 address using its own
19
+ // subclass:
20
+ //
21
+ // ip = IPAddress::IPv6::Unspecified.new
22
+ //
23
+ // ip.to_s
24
+ // // => => "::/128"
25
+ //
26
+ // You can easily check if an IPv6 object is an unspecified address by
27
+ // using the IPv6// unspecified? method
28
+ //
29
+ // ip.unspecified?
30
+ // // => true
31
+ //
32
+ // An unspecified IPv6 address can also be created with the wrapper
33
+ // method, like we've seen before
34
+ //
35
+ // ip = IPAddress "::"
36
+ //
37
+ // ip.unspecified?
38
+ // // => true
39
+ //
40
+ // This address must never be assigned to an interface and is to be used
41
+ // only in software before the application has learned its host's source
42
+ // address appropriate for a pending connection. Routers must not forward
43
+ // packets with the unspecified address.
44
+ //
45
+ //
46
+ // Creates a new IPv6 unspecified address
47
+ //
48
+ // ip = IPAddress::IPv6::Unspecified.new
49
+ //
50
+ // ip.to_s
51
+ // // => => "::/128"
52
+ //
53
+ create(): IPAddress | null {
54
+ return IPAddress.parse("::");
55
+ },
56
+ // class IPv6::Unspecified
57
+ };
58
+
59
+ export default Ipv6Unspec;