{"id":16304775,"url":"https://github.com/moznion/go-iprtb","last_synced_at":"2025-05-07T09:45:16.947Z","repository":{"id":63110704,"uuid":"565022564","full_name":"moznion/go-iprtb","owner":"moznion","description":"Pure go implementation of the IP routing table powered by prefix tree","archived":false,"fork":false,"pushed_at":"2025-04-22T06:08:07.000Z","size":55,"stargazers_count":55,"open_issues_count":3,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-22T07:39:33.297Z","etag":null,"topics":["golang","routing-tables"],"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/moznion.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-11-12T04:55:45.000Z","updated_at":"2025-02-28T14:04:15.000Z","dependencies_parsed_at":"2024-12-22T18:20:16.954Z","dependency_job_id":"2319f185-2bff-45e4-85db-72a8b66f7199","html_url":"https://github.com/moznion/go-iprtb","commit_stats":{"total_commits":38,"total_committers":2,"mean_commits":19.0,"dds":0.1842105263157895,"last_synced_commit":"3ca4d947ab346a862642a04db5d257cf6a95cce8"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-iprtb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-iprtb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-iprtb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fgo-iprtb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moznion","download_url":"https://codeload.github.com/moznion/go-iprtb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252853845,"owners_count":21814594,"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":["golang","routing-tables"],"created_at":"2024-10-10T21:04:47.716Z","updated_at":"2025-05-07T09:45:16.928Z","avatar_url":"https://github.com/moznion.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-iprtb [![.github/workflows/check.yml](https://github.com/moznion/go-iprtb/actions/workflows/check.yml/badge.svg)](https://github.com/moznion/go-iprtb/actions/workflows/check.yml) [![codecov](https://codecov.io/gh/moznion/go-iprtb/branch/main/graph/badge.svg?token=S3UWM0Y3LF)](https://codecov.io/gh/moznion/go-iprtb) [![GoDoc](https://godoc.org/github.com/moznion/go-iprtb?status.svg)](https://godoc.org/github.com/moznion/go-iprtb)\n\nPure go implementation of the IP routing table. This implementation uses a prefix tree as a backend data structure to find/match the routes.\n\nNOTE: This library is isolated from the OS-level routing table. This intends to provide the routing table function on the user-level code.\n\n## Synopsis\n\n```go\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"net\"\n\n\t\"github.com/moznion/go-iprtb\"\n)\n\nfunc main() {\n\tctx := context.Background()\n\trtb := iprtb.NewRouteTable()\n\n\terr := rtb.AddRoute(ctx, \u0026Route{\n\t\tDestination: \u0026net.IPNet{\n\t\t\tIP:   net.IPv4(192, 0, 2, 0),\n\t\t\tMask: net.IPv4Mask(255, 255, 255, 0),\n\t\t},\n\t\tGateway:          net.IPv4(192, 0, 2, 1),\n\t\tNetworkInterface: \"ifb0\",\n\t\tMetric:           1,\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\terr = rtb.AddRoute(ctx, \u0026Route{\n\t\tDestination: \u0026net.IPNet{\n\t\t\tIP:   net.IPv4(192, 0, 2, 255),\n\t\t\tMask: net.IPv4Mask(255, 255, 255, 255),\n\t\t},\n\t\tGateway:          net.IPv4(192, 0, 2, 255),\n\t\tNetworkInterface: \"ifb0\",\n\t\tMetric:           1,\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tmaybeRoute, err := rtb.MatchRoute(ctx, net.IPv4(192, 0, 2, 100))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(maybeRoute.IsSome()) // =\u003e true\n\tfmt.Println(maybeRoute.Unwrap().String()) // =\u003e 192.0.2.0/24\t192.0.2.1\tifb0\t1\n\n\tmaybeRoute, err = rtb.MatchRoute(ctx, net.IPv4(192, 0, 2, 254))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(maybeRoute.IsSome()) // =\u003e true\n\tfmt.Println(maybeRoute.Unwrap().String()) // =\u003e 192.0.2.0/24\t192.0.2.1\tifb0\t1\n\n\t// longest match\n\tmaybeRoute, err = rtb.MatchRoute(ctx, net.IPv4(192, 0, 2, 255))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(maybeRoute.IsSome()) // =\u003e true\n\tfmt.Println(maybeRoute.Unwrap().String()) // =\u003e 192.0.2.255/32\t192.0.2.255\tifb0\t1\n\n\t// not routes\n\tmaybeRoute, err = rtb.MatchRoute(ctx, net.IPv4(198, 51, 100, 123))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(maybeRoute.IsSome()) // =\u003e false\n}\n```\n\nAnd please see also [examples_test.go](./examples_test.go).\n\n## Docs\n\n[![GoDoc](https://godoc.org/github.com/moznion/go-iprtb?status.svg)](https://godoc.org/github.com/moznion/go-iprtb)\n\n### Longest matching by prefix tree\n\nIn the scenario that the routing table has the following three routes;\n\n| Route          | Route Binary                            | Gateway |\n|----------------|-----------------------------------------|---------|\n| 10.0.0.0/8     | **00001010** 00000000 00000000 00000000 | GW1     |\n| 192.0.0.0/8    | **11000000** 00000000 00000000 00000000 | GW2     |\n| 192.128.0.0/9  | **11000000 1**0000000 00000000 00000000 | GW3     |\n\nThis route table can transform into the following prefix tree:\n\n```\n                 R\n                / \\\n              /     \\\n            0         1\n           /           \\\n          0             1\n         /             /\n        0             0\n       /             /\n      0             0\n       \\           /\n        1         0\n       /         /\n      0         0\n       \\       /\n        1     0\n       /     /\n GW1 [0]   [0] GW2\n             \\\n             [1] GW3\n\n† R: Root Node\n†† [n]: Terminal Node\n```\n\nThen the target IP address only has to traverse this tree as much as longer to look up a route. It derives the result like the following.\n\n| Target IP     | Target IP Binary                      | Found Gateway |\n|---------------|---------------------------------------|---------------|\n| 10.10.10.10   | 0000101[0] 00001010 00001010 00001010 | GW1           |\n| 192.10.10.10  | 1100000[0] 00001010 00001010 00001010 | GW2           |\n| 192.192.10.10 | 11000000 [1]1000000 00001010 00001010 | GW3           |\n| 127.0.0.1     | 01111111 00000000 00000000 00000001   | N/A           |\n\nThis tree implementation contributes to good performance for route matching. This describes the result of the benchmark against the linear search based simple implementation (that comes from [e9a01d251b4388d77f27d3e7ff51d217cecaf7d9](https://github.com/moznion/go-iprtb/tree/e9a01d251b4388d77f27d3e7ff51d217cecaf7d9)):\n\n```\n$ go test -bench=. -benchmem\ngoos: linux\ngoarch: amd64\npkg: github.com/moznion/go-iprtb\ncpu: 12th Gen Intel(R) Core(TM) i7-1280P\nBenchmark_PrefixTreeAlgorithm-20        18535410                81.68 ns/op           64 B/op          1 allocs/op\nBenchmark_LinearSearch-20                6148155               192.1 ns/op            96 B/op          1 allocs/op\nPASS\nok      github.com/moznion/go-iprtb     2.968s\n```\n\nThe benchmark code is here: https://gist.github.com/moznion/eb867c8dd2a708f0acd184ee8d5c758b\n\n### Label support\n\nThis library provides \"label\" support on `AddRouteWithLabel()`, `UpdateRouteByLabel()`, and `RemoveRouteByLabel()`.\n\n`AddRouteWithLabel()` function registers a route with a label and that label can be used to update and remove the route\nby `UpdateRouteByLabel()` and `RemoveRouteByLabel()` instead of passing the actual destination information.\n\nIf there is no associated label, those updating functions with the label do nothing.\n\n## Author\n\nmoznion (\u003cmoznion@mail.moznion.net\u003e)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoznion%2Fgo-iprtb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoznion%2Fgo-iprtb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoznion%2Fgo-iprtb/lists"}