{"id":13413581,"url":"https://github.com/yl2chen/cidranger","last_synced_at":"2025-03-14T19:32:41.220Z","repository":{"id":46057818,"uuid":"100917065","full_name":"yl2chen/cidranger","owner":"yl2chen","description":"Fast IP to CIDR lookup in Golang","archived":false,"fork":false,"pushed_at":"2023-06-05T20:01:20.000Z","size":209,"stargazers_count":896,"open_issues_count":9,"forks_count":104,"subscribers_count":20,"default_branch":"master","last_synced_at":"2024-07-31T20:52:34.675Z","etag":null,"topics":["cidr","ip","network-analysis"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yl2chen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-08-21T05:50:14.000Z","updated_at":"2024-07-23T07:34:27.000Z","dependencies_parsed_at":"2023-01-31T18:31:35.955Z","dependency_job_id":"54aeb053-20de-400c-9df7-38cc33d49e5c","html_url":"https://github.com/yl2chen/cidranger","commit_stats":{"total_commits":62,"total_committers":11,"mean_commits":5.636363636363637,"dds":0.3870967741935484,"last_synced_commit":"d1cb2c52f37ac257db665ddfaf4af22e89863471"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yl2chen%2Fcidranger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yl2chen%2Fcidranger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yl2chen%2Fcidranger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yl2chen%2Fcidranger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yl2chen","download_url":"https://codeload.github.com/yl2chen/cidranger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221498771,"owners_count":16833059,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cidr","ip","network-analysis"],"created_at":"2024-07-30T20:01:43.726Z","updated_at":"2024-10-26T05:31:06.933Z","avatar_url":"https://github.com/yl2chen.png","language":"Go","funding_links":[],"categories":["Go","Networking","网络","网络相关库","Relational Databases","\u003cspan id=\"网络-networking\"\u003e网络 Networking\u003c/span\u003e"],"sub_categories":["Transliteration","Advanced Console UIs","音译","Uncategorized","暂未分类这些库被放在这里是因为其他类别似乎都不适合。","交流","暂未分类","Strings","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# cidranger\nFast 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.\n\n[![GoDoc Reference](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/yl2chen/cidranger)\n[![Build Status](https://img.shields.io/travis/yl2chen/cidranger.svg?branch=master\u0026style=flat-square)](https://travis-ci.org/yl2chen/cidranger)\n[![Coverage Status](https://img.shields.io/coveralls/yl2chen/cidranger.svg?branch=master\u0026style=flat-square)](https://coveralls.io/github/yl2chen/cidranger?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/yl2chen/cidranger?\u0026style=flat-square)](https://goreportcard.com/report/github.com/yl2chen/cidranger)\n\nThis 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.\n\u003cp align=\"left\"\u003e\u003cimg src=\"http://i.imgur.com/vSKTEBb.png\" width=\"600\" /\u003e\u003c/p\u003e\n\nVisualization of trie storing same CIDR blocks with path compression, improving both lookup speed and memory footprint.\n\u003cp align=\"left\"\u003e\u003cimg src=\"http://i.imgur.com/JtaDlD4.png\" width=\"600\" /\u003e\u003c/p\u003e\n\n## Getting Started\nConfigure imports.\n```go\nimport (\n  \"net\"\n\n  \"github.com/yl2chen/cidranger\"\n)\n```\nCreate a new ranger implemented using Path-Compressed prefix trie.\n```go\nranger := NewPCTrieRanger()\n```\nInserts CIDR blocks.\n```go\n_, network1, _ := net.ParseCIDR(\"192.168.1.0/24\")\n_, network2, _ := net.ParseCIDR(\"128.168.1.0/24\")\nranger.Insert(NewBasicRangerEntry(*network1))\nranger.Insert(NewBasicRangerEntry(*network2))\n```\nTo attach any additional value(s) to the entry, simply create custom struct\nstoring the desired value(s) that implements the RangerEntry interface:\n```go\ntype RangerEntry interface {\n\tNetwork() net.IPNet\n}\n```\nThe prefix trie can be visualized as:\n```\n0.0.0.0/0 (target_pos:31:has_entry:false)\n| 1--\u003e 128.0.0.0/1 (target_pos:30:has_entry:false)\n| | 0--\u003e 128.168.1.0/24 (target_pos:7:has_entry:true)\n| | 1--\u003e 192.168.1.0/24 (target_pos:7:has_entry:true)\n```\nTo test if given IP is contained in constructed ranger,\n```go\ncontains, err = ranger.Contains(net.ParseIP(\"128.168.1.0\")) // returns true, nil\ncontains, err = ranger.Contains(net.ParseIP(\"192.168.2.0\")) // returns false, nil\n```\nTo get all the networks given is contained in,\n```go\ncontainingNetworks, err = ranger.ContainingNetworks(net.ParseIP(\"128.168.1.0\"))\n```\nTo get all networks in ranger,\n```go\nentries, err := ranger.CoveredNetworks(*AllIPv4) // for IPv4\nentries, err := ranger.CoveredNetworks(*AllIPv6) // for IPv6\n```\n\n## Benchmark\nCompare 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)\n```go\n// Ipv4 lookup hit scenario\nBenchmarkPCTrieHitIPv4UsingAWSRanges-4         \t 5000000\t       353   ns/op\nBenchmarkBruteRangerHitIPv4UsingAWSRanges-4    \t  100000\t     13719   ns/op\n\n// Ipv6 lookup hit scenario, counter-intuitively faster then IPv4 due to less IPv6 CIDR\n// blocks in the AWS dataset, hence the constructed trie has less path splits and depth.\nBenchmarkPCTrieHitIPv6UsingAWSRanges-4         \t10000000\t       143   ns/op\nBenchmarkBruteRangerHitIPv6UsingAWSRanges-4    \t  300000\t      5178   ns/op\n\n// Ipv4 lookup miss scenario\nBenchmarkPCTrieMissIPv4UsingAWSRanges-4        \t20000000\t        96.5 ns/op\nBenchmarkBruteRangerMissIPv4UsingAWSRanges-4   \t   50000\t     24781   ns/op\n\n// Ipv6 lookup miss scenario\nBenchmarkPCTrieHMissIPv6UsingAWSRanges-4       \t10000000\t       115   ns/op\nBenchmarkBruteRangerMissIPv6UsingAWSRanges-4   \t  100000\t     10824   ns/op\n```\n\n## Example of IPv6 trie:\n```\n::/0 (target_pos:127:has_entry:false)\n| 0--\u003e 2400::/14 (target_pos:113:has_entry:false)\n| | 0--\u003e 2400:6400::/22 (target_pos:105:has_entry:false)\n| | | 0--\u003e 2400:6500::/32 (target_pos:95:has_entry:false)\n| | | | 0--\u003e 2400:6500::/39 (target_pos:88:has_entry:false)\n| | | | | 0--\u003e 2400:6500:0:7000::/53 (target_pos:74:has_entry:false)\n| | | | | | 0--\u003e 2400:6500:0:7000::/54 (target_pos:73:has_entry:false)\n| | | | | | | 0--\u003e 2400:6500:0:7000::/55 (target_pos:72:has_entry:false)\n| | | | | | | | 0--\u003e 2400:6500:0:7000::/56 (target_pos:71:has_entry:true)\n| | | | | | | | 1--\u003e 2400:6500:0:7100::/56 (target_pos:71:has_entry:true)\n| | | | | | | 1--\u003e 2400:6500:0:7200::/56 (target_pos:71:has_entry:true)\n| | | | | | 1--\u003e 2400:6500:0:7400::/55 (target_pos:72:has_entry:false)\n| | | | | | | 0--\u003e 2400:6500:0:7400::/56 (target_pos:71:has_entry:true)\n| | | | | | | 1--\u003e 2400:6500:0:7500::/56 (target_pos:71:has_entry:true)\n| | | | | 1--\u003e 2400:6500:100:7000::/54 (target_pos:73:has_entry:false)\n| | | | | | 0--\u003e 2400:6500:100:7100::/56 (target_pos:71:has_entry:true)\n| | | | | | 1--\u003e 2400:6500:100:7200::/56 (target_pos:71:has_entry:true)\n| | | | 1--\u003e 2400:6500:ff00::/64 (target_pos:63:has_entry:true)\n| | | 1--\u003e 2400:6700:ff00::/64 (target_pos:63:has_entry:true)\n| | 1--\u003e 2403:b300:ff00::/64 (target_pos:63:has_entry:true)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyl2chen%2Fcidranger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyl2chen%2Fcidranger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyl2chen%2Fcidranger/lists"}