ipaddress 0.2.3 → 0.2.6

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