https://github.com/rockcavera/nim-iputils
Utilities for use with IP. It has functions for IPv4, IPv6 and CIDR.
https://github.com/rockcavera/nim-iputils
cidr ip ipv4 ipv6 nim nim-lang parser range
Last synced: 2 months ago
JSON representation
Utilities for use with IP. It has functions for IPv4, IPv6 and CIDR.
- Host: GitHub
- URL: https://github.com/rockcavera/nim-iputils
- Owner: rockcavera
- License: mit
- Created: 2020-04-16T01:30:18.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-31T05:45:56.000Z (over 1 year ago)
- Last Synced: 2024-05-31T06:48:35.553Z (over 1 year ago)
- Topics: cidr, ip, ipv4, ipv6, nim, nim-lang, parser, range
- Language: Nim
- Homepage: https://rockcavera.github.io/nim-iputils/theindex.html
- Size: 24.4 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# iputils
Utilities for use with IP. It has functions for IPv4, IPv6 and CIDR.By rockcavera (rockcavera@gmail.com)
# Install
Run the Nimble install command``nimble install iputils``
# Basic usage
```nim
import iputilslet stringsIps = @["192.168.0.63", "0.0.0", "0", "256.0.0.1", "::1", "0:0:0:0:0:0:0:0", "2607:5300:60:37df::c4f3", ":::1"]
for ip in stringsIps:
if isIpv4(ip):
echo ip, " is IPv4!"
elif isIpv6(ip):
echo ip, " is IPv6!"
else:
echo ip, " not is IPv4 or IPv6!"for ip in stringsIps:
try:
let parsed = parseIpv4(ip)
except:
echo ip, " could not parse how IPv4."
try:
let parsed = parseIpv6(ip)
except:
echo ip, " could not parse how IPv6."for ip in stringsIps:
var
ipv4: Ipv4
ipv6: Ipv6if isIpv4AndStore(ip, ipv4):
echo "Stored: ", ipv4elif isIpv6AndStore(ip, ipv6):
echo "Stored: ", ipv6let
startIpv4 = parseIpv4("192.168.0.0")
endIpv4 = parseIpv4("192.168.5.100")
cidrs4 = ipv4RangeToCidr(startIpv4, endIpv4)for cidr in cidrs4:
let (i, e) = cidrToIpv4Range(cidr)echo cidr, " contains IPv4 between ", i, " - ", e
let
startIpv6 = parseIpv6("2607:5300:60:37df::c4f3")
endIpv6 = parseIpv6("2607:5300:60:37df::ff:ffff")
cidrs6 = ipv6RangeToCidr(startIpv6, endIpv6)for cidr in cidrs6:
let (i, e) = cidrToIpv6Range(cidr)echo cidr, " contains IPv6 between ", i, " - ", e
```# Documentation
[https://rockcavera.github.io/nim-iputils/theindex.html](https://rockcavera.github.io/nim-iputils/theindex.html "https://rockcavera.github.io/nim-iputils/theindex.html")