ipcalc

package module
v0.0.0-...-55e0ded Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2017 License: MIT Imports: 3 Imported by: 0

README

ipcalc

Build Status GoDoc

IP arithmetic utilities for Go, it supports both IPv4/IPv6 addresses.

Installation

go get -u github.com/hazaelsan/ipcalc

Import

import "github.com/hazaelsan/ipcalc"

Package ipcalc

This package provides basic IP arithmetic utilities.

// Get the next IP address
next := ipcalc.NextIP(net.ParseIP("192.0.2.1")) // 192.0.2.2

Note that IP wrapping is entirely possible in most functions

next := ipcalc.NextIP(net.ParseIP("255.255.255.255")) // 0.0.0.0

Package wildcard

This package provides utilities for working with Wildcard Masks, commonly used in Cisco IOS devices.

Wildcard masks are often thought of as "inverse subnet masks", there are some differences:

  • Matching logic is reversed from subnet masks, meaning
    • 0 means the equivalent bit must match
    • 1 means the equivalent bit does not matter
  • 0s and 1s need not be contiguous

Examples below, format is IP/Wildcard:

  • 192.0.2.0/0.0.0.255 matches 192.0.2.*
  • 192.0.2.10/0.0.255.0 matches 192.0.*.10
  • 192.0.2.1/0.0.255.254 matches 192.0.*.{1,3,5,7,...,255}

Use ipcalc.Complement to convert a subnet mask to its wildcard counterpart.

Documentation

Overview

Package ipcalc provides network utilities for performing IP address arithmetic.

Note that next/prev functions are subject to wrapping, e.g., NextIP(255.255.255.255) -> 0.0.0.0.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a, b net.IP, mask net.IPMask) net.IP

Add returns the sum of two net.IP addresses with the given mask. e.g., Add(192.168.0.1, 192.168.0.2, 0.0.0.255) -> 192.168.0.3.

func And

func And(a, b net.IP) net.IP

And returns the bitwise AND of two net.IP addresses. e.g., And(192.168.0.255, 192.168.255.128) -> 192.168.0.128.

func Broadcast

func Broadcast(n net.IPNet) net.IP

Broadcast returns the broadcast IP address for the given IPNet.

func Complement

func Complement(mask net.IPMask) net.IPMask

Complement returns the complement of a given net.IPMask, commonly used as a Wildcard Mask. e.g., Complement(255.255.254.0) -> 0.0.1.255.

func Contains

func Contains(a, b net.IPNet) bool

Contains returns whether the first net.IPNet wholly contains the second one. If either net has a non-standard mask then the result is undefined.

func CopyIP

func CopyIP(ip net.IP) net.IP

CopyIP returns a copy of a net.IP address.

func IP

func IP(ip net.IP) net.IP

IP returns an IP address of the correct byte length.

func IPSize

func IPSize(ip net.IP) int

IPSize returns the address size in bytes for the given net.IP.

func IPVersion

func IPVersion(ip net.IP) int

IPVersion returns the IP address version for the given net.IP.

func Merge

func Merge(a, b net.IP, mask net.IPMask) net.IP

Merge combines two net.IP addresses with the given mask. For bit i, if mask[i] is set then b[i] is returned, otherwise a[i] is returned. e.g., Merge(192.168.0.1, 172.16.32.100, 0.0.0.255) -> 192.168.0.100.

func NextIP

func NextIP(ip net.IP) net.IP

NextIP returns the next IP address. e.g., NextIP(192.168.0.0) -> 192.168.0.1.

func NextSubnet

func NextSubnet(n net.IPNet) net.IPNet

NextSubnet returns the next subnet. e.g., NextSubnet(192.168.0.0/24) -> 192.168.1.0/24.

func Or

func Or(a, b net.IP) net.IP

Or returns the bitwise OR of two net.IP addresses. e.g., Or(192.168.0.15, 192.168.10.128) -> 192.168.0.128.

func ParseIPMask

func ParseIPMask(addr string) (net.IP, net.IPMask, error)

ParseIPMask returns a net.IP and net.IPMask from an ip[/mask] string representation. The IPMask need not be in CIDR format, if it starts with ~ then it will be inverted.

func ParseMask

func ParseMask(mask string) net.IPMask

ParseMask returns a net.IPMask from a string representation.

func PrevIP

func PrevIP(ip net.IP) net.IP

PrevIP returns the previous IP address. e.g., PrevIP(192.168.0.1) -> 192.168.0.0.

func PrevSubnet

func PrevSubnet(n net.IPNet) net.IPNet

PrevSubnet returns the previous subnet. e.g., PrevSubnet(192.168.1.0/24) -> 192.168.0.0/24.

func Substract

func Substract(a, b net.IP, mask net.IPMask) net.IP

Substract returns the difference of two net.IP addresses with the given mask. e.g., Substract(192.168.0.3, 192.168.0.1, 0.0.0.255) -> 192.168.0.2.

func Xor

func Xor(a, b net.IP) net.IP

Xor returns the bitwise XOR of two net.IP addresses. e.g., Xor(192.0.2.1, 172.31.128.17) -> 108.31.130.16.

Types

This section is empty.

Directories

Path Synopsis
Package wildcard provides utilities for working with Wildcard Masks.
Package wildcard provides utilities for working with Wildcard Masks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL