littledevnl / iptools
Small package for handling things with IPs and networks
Requires
- php: >=7.4
Requires (Dev)
- infection/infection: ^0.20.2
- phpunit/phpunit: ^9.5
- symplify/easy-coding-standard: ^9.0
This package is auto-updated.
Last update: 2024-11-12 20:58:01 UTC
README
This package is a set of tools for working with IP addresses and networks.
A lot of the internals are inspired by S1lentium/IPTools, but when using that package I noticed I started by wrapping everything I needed in separate classes. Which lead me to create my own package.
The state is pretty alpha at this point but feel free to use it. I do my best to not change the interfaces and facades if possible.
Installation
composer require littledevnl/iptools
Getting started
use Littledev\IPTools\Address; use Littledev\IPTools\Network; use Littledev\IPTools\Iterator\AddressIterator; use Littledev\IPTools\Family\IPFamily; $ip = Address::parse($_SERVER['REMOTE_ADDR']); if ($ip->family() === IPFamily::v6()) { echo 'Good for you!'; } $network6 = Network::parse('2001:db8::/64'); if ($network6->contains($ip)) { echo 'You use an IP which is reserved for documentation.'; } $localNetwork = Network::parse('127.0.0.1/24'); if ($localNetwork->contains($ip)) { echo 'No place like localhost.'; } $iterator = new AddressIterator($localNetwork); foreach ($iterator as $address) { echo 'In local network: ' . $address->address(); }
Goals
- No dependencies.
- A set of tools for parsing and working with IP addresses and networks
- Utility classes should be open for extension through a common parent
Changelog
- [v0.1] Parsing of IP addresses and networks.
- [v0.2]
constains
now accepts networks and addresses. Generalized - [v0.3] Network Iterators
- [v0.4] Network lists
- [v1.0] Reworked interfaces
- [v1.1] Fixed composer PHP requirement, supports PHP7.4 and up
- [v1.2] Fix for shorter inAddr inputs
Limitations
Not yet a complete set of tools, it is just the beginning.
Without external dependencies for things like arbitrary precision numbers, it is impossible to do certain things. For instance calculating the number of hosts in a IPV6 network. You can do it yourself with bcmath, see the example below
use Littledev\IPTools\Network; use Littledev\IPTools\Family\IPFamily; $net = Network::parse('2001:db8::/64'); $numbersOfAddresses = bcpow('2', (string)(IPFamily::v6()->maxPrefix() - $net->subnet()->prefix()));