Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/idoqo/ipgalc
An awfully naive tool that calculates the broadcast IP, network ID, and hosts size - given an IPv4 address and a netmask
https://github.com/idoqo/ipgalc
Last synced: 15 days ago
JSON representation
An awfully naive tool that calculates the broadcast IP, network ID, and hosts size - given an IPv4 address and a netmask
- Host: GitHub
- URL: https://github.com/idoqo/ipgalc
- Owner: idoqo
- Created: 2020-10-02T20:03:15.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-07T06:58:06.000Z (over 4 years ago)
- Last Synced: 2023-03-06T11:12:42.937Z (almost 2 years ago)
- Language: Go
- Size: 1.13 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## idoqo/ipgalc
Given an IPv4 address and a network prefix, ipgalc calculates the corresponding
- Broadcast IP
- Network ID
- First Host
- Last Host
- Group Size and
- Hosts Size## Usage
```
go run ./main.go --ip $IP --prefix-bit $PREFIX
e.g
go run ./main.go --ip 127.0.0.1 --prefix-bit 24
```## How it works
ipgalc works by first splitting up the given IP address into the individual
octets, and uses the prefix to resolve the correct subnet (as well as the subnet
octets).
Both octets are represented as integer arrays (of length 4), and the calculation
is performed thus:### Network ID
Each element (called octet) in the target IP is _bitwise ANDed_ with the element at same
index in the subnet mask to give the element at that index in the network ID
e.g., say the IP address is "127.0.0.1" and prefix is 24 (i.e `/24`), the subnet mask is "255.255.255.0",
then:
```
networkID[0] = 127 & 255 // 127
networkID[1] = 0 & 255 // 0
networkID[2] = 0 & 255 // 0
networkID[3] = 1 & 0 // 0
```
which gives the network id as 127.0.0.0
### Broadcast IP
Each octet in the target IP is _bitwise ORed_ with the _bitwise complement_ of
the element at same index in the subnet mask. The (negative) result is added to
256 to wrap-around the value, giving the correct value (PS: I can't figure out why
it *needs* the extra 256). Using the previous example, we get:
```
broadcastIP[0] = 256 + (127 | (^255)) // 127
broadcastIP[1] = 256 + (0 | (^255)) // 0
broadcastIP[2] = 256 + (0 | (^255)) // 0
broadcastIP[3] = 256 + (1 | (^255)) // 255
```
giving broadcast IP as 127.0.0.255.### First Host
// todo :/
### Last Host
// todo :/
### Group Size
// todo :/
### Host Size
Host size represents the number of hosts possible in the subnet (without regards
for the Network ID and the Broadcast). Ipgalc resolves it by calculating: `2 ^ (32 - $PrefixBits)` and substracting 2 from the result. The host size for the above example would be:
```
total = 2 ^ (32 - 24) // 256
hostSize = total - 2 // 254
```## Resources
- [ipcalc](http://jodies.de/ipcalc): an online IP address calculator.
- [Practical Networking on
YouTube](https://www.youtube.com/watch?v=SM0kdVfhxZ0): Teaches
a human-friendly way to do subnet magic.## License
[Do What the Fuck you Want to](http://www.wtfpl.net/).