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