You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.8 KiB

7 years ago
  1. # ipaddr.js — an IPv6 and IPv4 address manipulation library [![Build Status](https://travis-ci.org/whitequark/ipaddr.js.svg)](https://travis-ci.org/whitequark/ipaddr.js)
  2. ipaddr.js is a small (1.9K minified and gzipped) library for manipulating
  3. IP addresses in JavaScript environments. It runs on both CommonJS runtimes
  4. (e.g. [nodejs]) and in a web browser.
  5. ipaddr.js allows you to verify and parse string representation of an IP
  6. address, match it against a CIDR range or range list, determine if it falls
  7. into some reserved ranges (examples include loopback and private ranges),
  8. and convert between IPv4 and IPv4-mapped IPv6 addresses.
  9. [nodejs]: http://nodejs.org
  10. ## Installation
  11. `npm install ipaddr.js`
  12. ## API
  13. ipaddr.js defines one object in the global scope: `ipaddr`. In CommonJS,
  14. it is exported from the module:
  15. ```js
  16. var ipaddr = require('ipaddr.js');
  17. ```
  18. The API consists of several global methods and two classes: ipaddr.IPv6 and ipaddr.IPv4.
  19. ### Global methods
  20. There are three global methods defined: `ipaddr.isValid`, `ipaddr.parse` and
  21. `ipaddr.process`. All of them receive a string as a single parameter.
  22. The `ipaddr.isValid` method returns `true` if the address is a valid IPv4 or
  23. IPv6 address, and `false` otherwise. It does not throw any exceptions.
  24. The `ipaddr.parse` method returns an object representing the IP address,
  25. or throws an `Error` if the passed string is not a valid representation of an
  26. IP address.
  27. The `ipaddr.process` method works just like the `ipaddr.parse` one, but it
  28. automatically converts IPv4-mapped IPv6 addresses to their IPv4 couterparts
  29. before returning. It is useful when you have a Node.js instance listening
  30. on an IPv6 socket, and the `net.ivp6.bindv6only` sysctl parameter (or its
  31. equivalent on non-Linux OS) is set to 0. In this case, you can accept IPv4
  32. connections on your IPv6-only socket, but the remote address will be mangled.
  33. Use `ipaddr.process` method to automatically demangle it.
  34. ### Object representation
  35. Parsing methods return an object which descends from `ipaddr.IPv6` or
  36. `ipaddr.IPv4`. These objects share some properties, but most of them differ.
  37. #### Shared properties
  38. One can determine the type of address by calling `addr.kind()`. It will return
  39. either `"ipv6"` or `"ipv4"`.
  40. An address can be converted back to its string representation with `addr.toString()`.
  41. Note that this method:
  42. * does not return the original string used to create the object (in fact, there is
  43. no way of getting that string)
  44. * returns a compact representation (when it is applicable)
  45. A `match(range, bits)` method can be used to check if the address falls into a
  46. certain CIDR range.
  47. Note that an address can be (obviously) matched only against an address of the same type.
  48. For example:
  49. ```js
  50. var addr = ipaddr.parse("2001:db8:1234::1");
  51. var range = ipaddr.parse("2001:db8::");
  52. addr.match(range, 32); // => true
  53. ```
  54. Alternatively, `match` can also be called as `match([range, bits])`. In this way,
  55. it can be used together with the `parseCIDR(string)` method, which parses an IP
  56. address together with a CIDR range.
  57. For example:
  58. ```js
  59. var addr = ipaddr.parse("2001:db8:1234::1");
  60. addr.match(ipaddr.parseCIDR("2001:db8::/32")); // => true
  61. ```
  62. A `range()` method returns one of predefined names for several special ranges defined
  63. by IP protocols. The exact names (and their respective CIDR ranges) can be looked up
  64. in the source: [IPv6 ranges] and [IPv4 ranges]. Some common ones include `"unicast"`
  65. (the default one) and `"reserved"`.
  66. You can match against your own range list by using
  67. `ipaddr.subnetMatch(address, rangeList, defaultName)` method. It can work with both
  68. IPv6 and IPv4 addresses, and accepts a name-to-subnet map as the range list. For example:
  69. ```js
  70. var rangeList = {
  71. documentationOnly: [ ipaddr.parse('2001:db8::'), 32 ],
  72. tunnelProviders: [
  73. [ ipaddr.parse('2001:470::'), 32 ], // he.net
  74. [ ipaddr.parse('2001:5c0::'), 32 ] // freenet6
  75. ]
  76. };
  77. ipaddr.subnetMatch(ipaddr.parse('2001:470:8:66::1'), rangeList, 'unknown'); // => "he.net"
  78. ```
  79. The addresses can be converted to their byte representation with `toByteArray()`.
  80. (Actually, JavaScript mostly does not know about byte buffers. They are emulated with
  81. arrays of numbers, each in range of 0..255.)
  82. ```js
  83. var bytes = ipaddr.parse('2a00:1450:8007::68').toByteArray(); // ipv6.google.com
  84. bytes // => [42, 0x00, 0x14, 0x50, 0x80, 0x07, 0x00, <zeroes...>, 0x00, 0x68 ]
  85. ```
  86. The `ipaddr.IPv4` and `ipaddr.IPv6` objects have some methods defined, too. All of them
  87. have the same interface for both protocols, and are similar to global methods.
  88. `ipaddr.IPvX.isValid(string)` can be used to check if the string is a valid address
  89. for particular protocol, and `ipaddr.IPvX.parse(string)` is the error-throwing parser.
  90. [IPv6 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/src/ipaddr.coffee#L186
  91. [IPv4 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/src/ipaddr.coffee#L71
  92. #### IPv6 properties
  93. Sometimes you will want to convert IPv6 not to a compact string representation (with
  94. the `::` substitution); the `toNormalizedString()` method will return an address where
  95. all zeroes are explicit.
  96. For example:
  97. ```js
  98. var addr = ipaddr.parse("2001:0db8::0001");
  99. addr.toString(); // => "2001:db8::1"
  100. addr.toNormalizedString(); // => "2001:db8:0:0:0:0:0:1"
  101. ```
  102. The `isIPv4MappedAddress()` method will return `true` if this address is an IPv4-mapped
  103. one, and `toIPv4Address()` will return an IPv4 object address.
  104. To access the underlying binary representation of the address, use `addr.parts`.
  105. ```js
  106. var addr = ipaddr.parse("2001:db8:10::1234:DEAD");
  107. addr.parts // => [0x2001, 0xdb8, 0x10, 0, 0, 0, 0x1234, 0xdead]
  108. ```
  109. #### IPv4 properties
  110. `toIPv4MappedAddress()` will return a corresponding IPv4-mapped IPv6 address.
  111. To access the underlying representation of the address, use `addr.octets`.
  112. ```js
  113. var addr = ipaddr.parse("192.168.1.1");
  114. addr.octets // => [192, 168, 1, 1]
  115. ```