README.md 5.32 KB
Newer Older
Yulin's avatar
Yulin committed
1
# cidranger
2
Fast IP to CIDR block(s) lookup using trie in Golang, inspired by [IPv4 route lookup linux](https://vincent.bernat.im/en/blog/2017-ipv4-route-lookup-linux).  Possible use cases include detecting if a IP address is from published cloud provider CIDR blocks (e.g. 52.95.110.1 is contained in published AWS Route53 CIDR 52.95.110.0/24), IP routing rules, etc.
Yulin's avatar
Yulin committed
3

Yulin's avatar
Yulin committed
4
[![GoDoc Reference](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/yl2chen/cidranger)
Yulin's avatar
Yulin committed
5
[![Build Status](https://img.shields.io/travis/yl2chen/cidranger.svg?branch=master&style=flat-square)](https://travis-ci.org/yl2chen/cidranger)
6
[![Coverage Status](https://img.shields.io/coveralls/yl2chen/cidranger.svg?branch=master&style=flat-square)](https://coveralls.io/github/yl2chen/cidranger?branch=master)
Yulin's avatar
Yulin committed
7
[![Go Report Card](https://goreportcard.com/badge/github.com/yl2chen/cidranger?&style=flat-square)](https://goreportcard.com/report/github.com/yl2chen/cidranger)
Yulin's avatar
Yulin committed
8

神楽坂帕琪's avatar
神楽坂帕琪 committed
9
This is visualization of a trie storing CIDR blocks `128.0.0.0/2` `192.0.0.0/2` `200.0.0.0/5` without path compression, the 0/1 number on the path indicates the bit value of the IP address at specified bit position, hence the path from root node to a child node represents a CIDR block that contains all IP ranges of its children, and children's children.
10 11
<p align="left"><img src="http://i.imgur.com/vSKTEBb.png" width="600" /></p>

12
Visualization of trie storing same CIDR blocks with path compression, improving both lookup speed and memory footprint.
13 14 15
<p align="left"><img src="http://i.imgur.com/JtaDlD4.png" width="600" /></p>

## Getting Started
Yulin's avatar
Yulin committed
16
Configure imports.
Yulin Chen's avatar
Yulin Chen committed
17
```go
Yulin's avatar
Yulin committed
18
import (
Yulin Chen's avatar
Yulin Chen committed
19
  "net"
20

Yulin's avatar
Yulin committed
21 22 23
  "github.com/yl2chen/cidranger"
)
```
24
Create a new ranger implemented using Path-Compressed prefix trie.
Yulin Chen's avatar
Yulin Chen committed
25
```go
26
ranger := NewPCTrieRanger()
Yulin's avatar
Yulin committed
27 28
```
Inserts CIDR blocks.
Yulin Chen's avatar
Yulin Chen committed
29
```go
Yulin's avatar
Yulin committed
30 31
_, network1, _ := net.ParseCIDR("192.168.1.0/24")
_, network2, _ := net.ParseCIDR("128.168.1.0/24")
32 33 34 35 36 37 38 39 40
ranger.Insert(NewBasicRangerEntry(*network1))
ranger.Insert(NewBasicRangerEntry(*network2))
```
To attach any additional value(s) to the entry, simply create custom struct
storing the desired value(s) that implements the RangerEntry interface:
```go
type RangerEntry interface {
	Network() net.IPNet
}
Yulin's avatar
Yulin committed
41 42
```
The prefix trie can be visualized as:
43
```
Yulin's avatar
Yulin committed
44 45 46 47 48
0.0.0.0/0 (target_pos:31:has_entry:false)
| 1--> 128.0.0.0/1 (target_pos:30:has_entry:false)
| | 0--> 128.168.1.0/24 (target_pos:7:has_entry:true)
| | 1--> 192.168.1.0/24 (target_pos:7:has_entry:true)
```
Yulin Chen's avatar
Yulin Chen committed
49 50
To test if given IP is contained in constructed ranger,
```go
Yulin's avatar
Yulin committed
51 52 53 54
contains, err = ranger.Contains(net.ParseIP("128.168.1.0")) // returns true, nil
contains, err = ranger.Contains(net.ParseIP("192.168.2.0")) // returns false, nil
```
To get all the networks given is contained in,
Yulin Chen's avatar
Yulin Chen committed
55
```go
Yulin's avatar
Yulin committed
56 57
containingNetworks, err = ranger.ContainingNetworks(net.ParseIP("128.168.1.0"))
```
58 59 60 61 62
To get all networks in ranger,
```go
entries, err := ranger.CoveredNetworks(*AllIPv4) // for IPv4
entries, err := ranger.CoveredNetworks(*AllIPv6) // for IPv6
```
Yulin's avatar
Yulin committed
63

64
## Benchmark
65
Compare hit/miss case for IPv4/IPv6 using PC trie vs brute force implementation, Ranger is initialized with published AWS ip ranges (889 IPv4 CIDR blocks and 360 IPv6)
Yulin Chen's avatar
Yulin Chen committed
66
```go
67 68 69
// Ipv4 lookup hit scenario
BenchmarkPCTrieHitIPv4UsingAWSRanges-4         	 5000000	       353   ns/op
BenchmarkBruteRangerHitIPv4UsingAWSRanges-4    	  100000	     13719   ns/op
Yulin's avatar
Yulin committed
70

71 72
// Ipv6 lookup hit scenario, counter-intuitively faster then IPv4 due to less IPv6 CIDR
// blocks in the AWS dataset, hence the constructed trie has less path splits and depth.
73 74
BenchmarkPCTrieHitIPv6UsingAWSRanges-4         	10000000	       143   ns/op
BenchmarkBruteRangerHitIPv6UsingAWSRanges-4    	  300000	      5178   ns/op
Yulin's avatar
Yulin committed
75

76 77 78
// Ipv4 lookup miss scenario
BenchmarkPCTrieMissIPv4UsingAWSRanges-4        	20000000	        96.5 ns/op
BenchmarkBruteRangerMissIPv4UsingAWSRanges-4   	   50000	     24781   ns/op
Yulin's avatar
Yulin committed
79

80 81 82
// Ipv6 lookup miss scenario
BenchmarkPCTrieHMissIPv6UsingAWSRanges-4       	10000000	       115   ns/op
BenchmarkBruteRangerMissIPv6UsingAWSRanges-4   	  100000	     10824   ns/op
Yulin's avatar
Yulin committed
83 84
```

85 86
## Example of IPv6 trie:
```
Yulin's avatar
Yulin committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
::/0 (target_pos:127:has_entry:false)
| 0--> 2400::/14 (target_pos:113:has_entry:false)
| | 0--> 2400:6400::/22 (target_pos:105:has_entry:false)
| | | 0--> 2400:6500::/32 (target_pos:95:has_entry:false)
| | | | 0--> 2400:6500::/39 (target_pos:88:has_entry:false)
| | | | | 0--> 2400:6500:0:7000::/53 (target_pos:74:has_entry:false)
| | | | | | 0--> 2400:6500:0:7000::/54 (target_pos:73:has_entry:false)
| | | | | | | 0--> 2400:6500:0:7000::/55 (target_pos:72:has_entry:false)
| | | | | | | | 0--> 2400:6500:0:7000::/56 (target_pos:71:has_entry:true)
| | | | | | | | 1--> 2400:6500:0:7100::/56 (target_pos:71:has_entry:true)
| | | | | | | 1--> 2400:6500:0:7200::/56 (target_pos:71:has_entry:true)
| | | | | | 1--> 2400:6500:0:7400::/55 (target_pos:72:has_entry:false)
| | | | | | | 0--> 2400:6500:0:7400::/56 (target_pos:71:has_entry:true)
| | | | | | | 1--> 2400:6500:0:7500::/56 (target_pos:71:has_entry:true)
| | | | | 1--> 2400:6500:100:7000::/54 (target_pos:73:has_entry:false)
| | | | | | 0--> 2400:6500:100:7100::/56 (target_pos:71:has_entry:true)
| | | | | | 1--> 2400:6500:100:7200::/56 (target_pos:71:has_entry:true)
| | | | 1--> 2400:6500:ff00::/64 (target_pos:63:has_entry:true)
| | | 1--> 2400:6700:ff00::/64 (target_pos:63:has_entry:true)
| | 1--> 2403:b300:ff00::/64 (target_pos:63:has_entry:true)
Yulin's avatar
Yulin committed
107
```