Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cidr-matcher

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cidr-matcher

Fast CIDR matcher. Given an input IPv4 address, it checks if it's inside a set of IP ranges, expressed in CIDR notation.

  • 2.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
36K
increased by7.98%
Maintainers
2
Weekly downloads
 
Created
Source

CIDR Matcher

Fast CIDR matcher. Given an input IPv4 or IPv6 address, it checks if it's inside a set of IP ranges, expressed in CIDR notation. This module is based upon the great ip6addr module.

Installation

npm

npm install cidr-matcher

git

git clone https://github.com/pracucci/node-cidr-matcher.git

Usage


var CIDRMatcher = require('cidr-matcher');

var matcher = new CIDRMatcher([ '2a05:d07c:2000:0:0:0:0:0/120', '192.168.1.0/24', '192.168.2.3/32', '192.168.3.2/32' ]);

matcher.contains('192.168.1.1'); // returns true
matcher.contains('192.168.1.2'); // returns true

matcher.contains('192.168.2.1'); // returns true
matcher.contains('192.168.2.2'); // returns false

matcher.contains('192.168.3.1'); // returns false
matcher.contains('192.168.3.2'); // returns false

matcher.containsAny([ '192.168.1.1', '192.168.1.2' ]); // return true
matcher.containsAny([ '192.168.2.1', '192.168.2.2' ]); // return true
matcher.containsAny([ '192.168.3.1', '192.168.3.2' ]); // return false

assert.ok(matcher.contains('2a05:d07c:2000:0:0:0:0:0'));  // return true
assert.ok(matcher.contains('2a05:d07c:2000:0:0:0:0:ff')); // return true
assert.ok(matcher.contains('2a05:d07c:3000:0:0:0:0:0'));  // return false

// You can also add / remove network classes on-the-fly
matcher.addNetworkClass('192.168.5.0/24');
matcher.contains('192.168.5.1'); // returns true

matcher.removeNetworkClass('192.168.5.0/24');
matcher.contains('192.168.5.1'); // returns false

Benchmark

The following table shows the execution of benchmark/ across different versions of this module and other modules you can find on npm. Each benchmark is executed with a random set of 25000 IP addresses, where each IP is checked against each network range (CIDR) in the test dataset.

ModuleVersionDatasetExecution time
This one2.0.0AWS IPv4 (1385 CIDRs)228 ms
This one2.0.0AWS IPv6 (474 CIDRs)1799 ms
This one1.0.5AWS IPv4 (1385 CIDRs)2895 ms (without IPv4 over IPv6 support)
This one1.0.5AWS IPv6 (474 CIDRs)Unsupported
is-in-subnet1.9.0AWS IPv4 (1385 CIDRs)96106 ms
is-in-subnet1.9.0AWS IPv6 (474 CIDRs)33482 ms
ip-range-check0.0.2AWS IPv4 (1385 CIDRs)390134 ms
ip-range-check0.0.2AWS IPv6 (474 CIDRs)73083 ms

If you're aware of any other module that implements the IP in CIDR and you wanna see it benchmarked, please open an Issue or submit a PR.

Contribute

Run tests

npm test

License

This software is licensed under the MIT License.

Copyright (c) 2015, Marco Pracucci marco@pracucci.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 17 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc