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.

README.md 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # IP
  2. [![](https://badge.fury.io/js/ip.svg)](https://www.npmjs.com/package/ip)
  3. IP address utilities for node.js
  4. ## Installation
  5. ### npm
  6. ```shell
  7. npm install ip
  8. ```
  9. ### git
  10. ```shell
  11. git clone https://github.com/indutny/node-ip.git
  12. ```
  13. ## Usage
  14. Get your ip address, compare ip addresses, validate ip addresses, etc.
  15. ```js
  16. var ip = require('ip');
  17. ip.address() // my ip address
  18. ip.isEqual('::1', '::0:1'); // true
  19. ip.toBuffer('127.0.0.1') // Buffer([127, 0, 0, 1])
  20. ip.toString(new Buffer([127, 0, 0, 1])) // 127.0.0.1
  21. ip.fromPrefixLen(24) // 255.255.255.0
  22. ip.mask('192.168.1.134', '255.255.255.0') // 192.168.1.0
  23. ip.cidr('192.168.1.134/26') // 192.168.1.128
  24. ip.not('255.255.255.0') // 0.0.0.255
  25. ip.or('192.168.1.134', '0.0.0.255') // 192.168.1.255
  26. ip.isPrivate('127.0.0.1') // true
  27. ip.isV4Format('127.0.0.1'); // true
  28. ip.isV6Format('::ffff:127.0.0.1'); // true
  29. // operate on buffers in-place
  30. var buf = new Buffer(128);
  31. var offset = 64;
  32. ip.toBuffer('127.0.0.1', buf, offset); // [127, 0, 0, 1] at offset 64
  33. ip.toString(buf, offset, 4); // '127.0.0.1'
  34. // subnet information
  35. ip.subnet('192.168.1.134', '255.255.255.192')
  36. // { networkAddress: '192.168.1.128',
  37. // firstAddress: '192.168.1.129',
  38. // lastAddress: '192.168.1.190',
  39. // broadcastAddress: '192.168.1.191',
  40. // subnetMask: '255.255.255.192',
  41. // subnetMaskLength: 26,
  42. // numHosts: 62,
  43. // length: 64,
  44. // contains: function(addr){...} }
  45. ip.cidrSubnet('192.168.1.134/26')
  46. // Same as previous.
  47. // range checking
  48. ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true
  49. // ipv4 long conversion
  50. ip.toLong('127.0.0.1'); // 2130706433
  51. ip.fromLong(2130706433); // '127.0.0.1'
  52. ```
  53. ### License
  54. This software is licensed under the MIT License.
  55. Copyright Fedor Indutny, 2012.
  56. Permission is hereby granted, free of charge, to any person obtaining a
  57. copy of this software and associated documentation files (the
  58. "Software"), to deal in the Software without restriction, including
  59. without limitation the rights to use, copy, modify, merge, publish,
  60. distribute, sublicense, and/or sell copies of the Software, and to permit
  61. persons to whom the Software is furnished to do so, subject to the
  62. following conditions:
  63. The above copyright notice and this permission notice shall be included
  64. in all copies or substantial portions of the Software.
  65. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  66. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  67. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  68. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  69. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  70. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  71. USE OR OTHER DEALINGS IN THE SOFTWARE.