{"id":13413614,"url":"https://github.com/c-robinson/iplib","last_synced_at":"2025-03-14T19:32:49.408Z","repository":{"id":38421306,"uuid":"185135948","full_name":"c-robinson/iplib","owner":"c-robinson","description":"A library  for working with IP addresses and networks in Go","archived":false,"fork":false,"pushed_at":"2024-04-06T16:54:19.000Z","size":147,"stargazers_count":136,"open_issues_count":0,"forks_count":23,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-07-31T20:52:36.143Z","etag":null,"topics":["cidr","go","golang-library","golang-tools","ip","ipaddress","ipaddresses","ipv4","ipv4-address","ipv6","ipv6-address","ipv6-subnetting","library","subnet","subnet-calculator","subnetting"],"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/c-robinson.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}},"created_at":"2019-05-06T06:23:41.000Z","updated_at":"2024-07-19T13:58:17.000Z","dependencies_parsed_at":"2024-01-19T08:28:45.641Z","dependency_job_id":"b890197f-c419-49b7-a83f-b566e95eace4","html_url":"https://github.com/c-robinson/iplib","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-robinson%2Fiplib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-robinson%2Fiplib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-robinson%2Fiplib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-robinson%2Fiplib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c-robinson","download_url":"https://codeload.github.com/c-robinson/iplib/tar.gz/refs/heads/main","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","go","golang-library","golang-tools","ip","ipaddress","ipaddresses","ipv4","ipv4-address","ipv6","ipv6-address","ipv6-subnetting","library","subnet","subnet-calculator","subnetting"],"created_at":"2024-07-30T20:01:44.560Z","updated_at":"2024-10-26T05:31:10.733Z","avatar_url":"https://github.com/c-robinson.png","language":"Go","readme":"# IPLib\n[![Documentation](https://godoc.org/github.com/c-robinson/iplib?status.svg)](https://pkg.go.dev/github.com/c-robinson/iplib/v2)\n[![Go Report Card](https://goreportcard.com/badge/github.com/c-robinson/iplib)](https://goreportcard.com/report/github.com/c-robinson/iplib)\n[![Tests](https://img.shields.io/github/actions/workflow/status/c-robinson/iplib/test.yml?branch=main\u0026longCache=true\u0026label=Test\u0026logo=github%20actions\u0026logoColor=fff)](https://github.com/c-robinson/iplib/actions?query=workflow%3ATest)\n\n**ATTENTION** version 2.0.0 is a breaking change from previous versions for\nhandling IPv6 addresses (functions for IPv4 are unchanged). Calls that result\nin arithmatic operations against IPv6 now use [uint128.Uint128](https://lukechampine.com/uint128)\ninstead of `*big.Int`. Until now this library restricted itself to using the\nstandard library, but `math/big` is sloooooooooow and the performance gains\nfrom switching were too large to ignore:\n\n| Benchmark | *big.Int | uint128.Uint128 |\n| --- | --- |-----------------|\n| Benchmark_DeltaIP6 | 79.27 ns/op | 2.809 ns/op     |\n| BenchmarkDecrementIP6By | 50.54 ns/op | 13.88 ns/op     |\n| BenchmarkIncrementIP6By | 50.48 ns/op | 13.92 ns/op     |\n| BenchmarkNet_Count6 | 122.2 ns/op | 11.26 ns/op     |\n\nIt would be fantastic to remove this external dependency in some future v3\nthat switched to a native `uint128` but for that to happen [this proposal](https://github.com/golang/go/issues/9455)\n(or something similar) would need to be adopted.\n\n**Okay you can stop paying attention now** \n\nI really enjoy Python's [ipaddress](https://docs.python.org/3/library/ipaddress.html)\nlibrary and Ruby's [ipaddr](https://ruby-doc.org/stdlib-2.5.1/libdoc/ipaddr/rdoc/IPAddr.html),\nI think you can write a lot of neat software if some of the little problems\naround manipulating IP addresses and netblocks are taken care of for you, so I\nset out to write something like them for my language of choice, Go. This is\nwhat I've come up with.\n\n[IPLib](http://godoc.org/github.com/c-robinson/iplib) is a hopefully useful,\naspirationally full-featured library built around and on top of the address\nprimitives found in the [net](https://golang.org/pkg/net/) package, it seeks\nto make them more accessible and easier to manipulate. \n\nIt includes:\n\n##### net.IP tools\n\nSome simple tools for performing common tasks against IP objects:\n\n- compare two addresses\n- make a copy of a net.IP address\n- get the delta between two addresses\n- sort\n- decrement or increment addresses\n- print addresses as binary or hexadecimal strings, or print their addr.ARPA\n  DNS name\n- print v6 in fully expanded form\n- convert between net.IP and integer values\n- get the version of a v4 address or force a IPv4-mapped IPv6address to be a \n  v4 address\n\n##### iplib.Net\n\nAn enhancement of `net.IPNet`, `iplib.Net` is an interface with two, version-\nspecific implementations providing features such as:\n\n- retrieve the first and last usable address\n- retrieve the wildcard mask\n- enumerate all or part of a netblock to `[]net.IP`\n- decrement or increment addresses within the boundaries of the netblock\n- return the supernet of a netblock\n- allocate subnets within the netblock\n- return next- or previous-adjacent netblocks\n\n##### Net4 and Net6 implementations of Net\n\nThe two address versions behave differently in both large and subtle ways,\nand the version-specific implementations seek to account for this. For example\nthe Net4 implementation omits the network and broadcast addresses from\nconsideration during enumeration; while the Net6 implementation introduces the\nconcept of a HostMask, which blocks usable addresses off from the right in the\nsame way that a netmask constrains them from the left\n\nAdditional version-specific considerations described in the [Net4](#using-iplibnet4)\nand [Net6](#using-iplibnet6) sections below.\n\n## Sub-modules\n\n- [iana](https://github.com/c-robinson/iplib/tree/main/iana) - a module for referencing \n  IP netblocks against the [Internet Assigned Numbers Authority's](https://www.iana.org/)\n  Special IP Address Registry\n- [iid](https://github.com/c-robinson/iplib/tree/main/iid) - a module for\n  generating and validating IPv6 Interface Identifiers, including [RFC4291](https://tools.ietf.org/html/rfc4291)\n  modified EUI64 and [RFC7217](https://tools.ietf.org/html/rfc7217)\n  Semantically Opaque addresses\n\n## Installing\n\n```sh\ngo get -u github.com/c-robinson/iplib/v2\n```\n\n## Using iplib\n\nThere are a series of functions for working with v4 or v6 `net.IP` objects:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net\"\n\t\"sort\"\n\t\n\t\"github.com/c-robinson/iplib/v2\"\n)\n\n\nfunc main() {\n\tipa := net.ParseIP(\"192.168.1.1\")\n\tipb := iplib.IncrementIPBy(ipa, 15)      // ipb is 192.168.1.16\n\tipc := iplib.NextIP(ipa)                 // ipc is 192.168.1.2\n\n\tfmt.Println(iplib.CompareIPs(ipa, ipb))  // -1\n    \n\tfmt.Println(iplib.DeltaIP(ipa, ipb))     // 15\n    \n\tfmt.Println(iplib.IPToHexString(ipc))    // \"c0a80102\"\n\n\tiplist := []net.IP{ ipb, ipc, ipa }\n\tsort.Sort(iplib.ByIP(iplist))            // []net.IP{ipa, ipc, ipb}\n\n\tfmt.Println(iplib.IP4ToUint32(ipa))      // 3232235777\n\tfmt.Println(iplib.IPToBinaryString(ipa)) // 11000000.10101000.00000001.00000001\n\tfmt.Println(iplib.IP4ToARPA(ipa))        // 1.1.168.192.in-addr.arpa\n}\n```\n\nAddresses that require or return a count default to using `uint32`, which is\nsufficient for working with the entire IPv4 space. As a rule these functions\nare just lowest-common wrappers around IPv4- or IPv6-specific functions. The\nIPv6-specific variants use `uint128.Uint128` so they can access the entire v6\nspace.\n\n## The iplib.Net interface\n\n`Net` describes an `iplib.Net` object, the exposed functions are those that \nare required for comparison, sorting, generic initialization and for ancillary \nfunctions such as those found in this package's submodules.\n\n## Using iplib.Net4\n\n`Net4` represents an IPv4 network. Since the first and last addresses of a v4\nnetwork are typically not allocated for use these will be omitted by\n`Enumerate()`, `NextIP()` and `PreviousIP()`; they wont show up in `Count()`;\nand `FirstAddress()` and `LastAddress()` show the 2nd and 2nd-to-the-last\naddresses respectively. The v4-specific method `NetworkAddress()` returns the\nfirst address, while `BroadcastAddress()` returns the last. There is an\nexception made for `Net4` networks defined with a 31-bit netmask, since these\nare assumed to be for [RFC3021](https://datatracker.ietf.org/doc/html/rfc3021)\npoint-to-point links.\n\nAdditionally `Net4` contains a `Wildcard()` method which will return the\nnetwork's [wildcard address](https://en.wikipedia.org/wiki/Wildcard_mask).\n\n```go\nn := iplib.NewNet4(net.ParseIP(\"192.168.0.0\"), 16)\nfmt.Println(n.Count())            // 65534 (note: not 65536)\nfmt.Println(n.Enumerate(2, 1024)) // [192.168.4.1 192.168.4.2]\nfmt.Println(n.IP())               // 192.168.0.0\nfmt.Println(n.FirstAddress())     // 192.168.0.1\nfmt.Println(n.LastAddress())      // 192.168.255.254\nfmt.Println(n.BroadcastAddress()) // 192.168.255.255\nfmt.Println(n.Wildcard())         // 0000ffff\nfmt.Println(n.Subnet(0))          // [192.168.0.0/17 192.168.128.0/17] \u003cnil\u003e\nfmt.Println(n.Supernet(0))        // 192.168.0.0/15 \u003cnil\u003e\n```\n\n## Using iplib.Net6\n\n`Net6` represents an IPv6 network. In some ways v6 is simpler than v4, as\nit does away with the special behavior of addresses at the front and back of\nthe netblock. For IPv6 the primary problem is the sheer size of the thing:\nthere are 2^128th addresses in IPv6, which translates to 340 undecillion!\n\n```go\nn := iplib.NewNet6(net.ParseIP(\"2001:db8::\"), 56, 0)\nfmt.Println(n.Count())                  // 4722366482869645213696\nfmt.Println(n.Enumerate(2, 1024))       // [2001:db8::400 2001:db8::401]\nfmt.Println(n.FirstAddress())           // 2001:db8::\nfmt.Println(n.NextIP(n.FirstAddress())) // 2001:db8::1 \u003cnil\u003e\nfmt.Println(n.LastAddress())            // 2001:db8:0:ff:ffff:ffff:ffff:ffff\nfmt.Println(n.Subnet(0, 0))             // [2001:db8::/57 2001:db8:0:80::/57] \u003cnil\u003e\nfmt.Println(n.Supernet(0, 0))           // 2001:db8::/55 \u003cnil\u003e\n```\n\n### HostMasks with Net6\n\nTo manage the address space, `Net6` introduces `HostMask`. This optional\nconstraint can be used to block addresses on the right-side of a netblock\nsomewhat like Netmasks do on the left. `Hostmask` must be specified at\ninitialization time and, if set, will affect the behavior of `Count()`, \n`Enumerate()`, `LastAddress()`, `NextIP()` and `PreviousIP()`. `Subnet()` and\n`Supernet()` generate objects that inherit the hostmask of their parent, while\na hostmask must be specified for `NextNet()` and `PreviousNet()`.\n\n```go\n// this is the same as the previous example, except with a hostmask set\nn := NewNet6(net.ParseIP(\"2001:db8::\"), 56, 60)\nfmt.Println(n.Count())                  // 4096\nfmt.Println(n.Enumerate(2, 1024))       // [2001:db8:0:40:: 2001:db8:0:40:100::]\nfmt.Println(n.FirstAddress())           // 2001:db8::\nfmt.Println(n.NextIP(n.FirstAddress())) // 2001:db8:0:0:100:: \u003cnil\u003e\nfmt.Println(n.LastAddress())            // 2001:db8:0:ff:f00::\nfmt.Println(n.Mask().String())          // ffffffffffffff000000000000000000\nfmt.Println(n.Hostmask.String())        // 0000000000000000f0ffffffffffffff\nfmt.Println(n.Subnet(0, 60))            // [2001:db8::/57 2001:db8:0:80::/57] \u003cnil\u003e\nfmt.Println(n.Supernet(0, 60))          // 2001:db8::/55 \u003cnil\u003e\n```\n\n## Test driving\n[IPfool](github.com/c-robinson/ipfool) is a simple command-line tool that I\nwrote to test many of the features within this library and might be useful in\nevaluating it.","funding_links":[],"categories":["Networking","网络","网络相关库"],"sub_categories":["Transliteration","Strings","音译","暂未分类","暂未分类这些库被放在这里是因为其他类别似乎都不适合。","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc-robinson%2Fiplib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc-robinson%2Fiplib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc-robinson%2Fiplib/lists"}