cidr

package module
v0.0.0-...-7eba088 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2020 License: MIT Imports: 3 Imported by: 1

README

CIDR (Classless inter-domain routing)

This Package converts IP CIDR to range and return First IP, Last IP, First IP decimal, Last IP decimal and Total Host count for IPv4 and IPv6

Install

go get -u github.com/m7shapan/cidr

How to use

See the GoDoc

package main

import (
	"fmt"
	"log"

	"github.com/m7shapan/cidr"
)

func main() {
	p, err := cidr.ParseCIDR("1.0.0.0/24")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("First IP:", p.FirstIP)
	if p.IsIPv4 {
		fmt.Println("First IP (Decimal):", p.FirstIPv4())
	} else {
		fmt.Println("First IP (Decimal):", p.FirstIPv6())
	}

	fmt.Println("Last IP:", p.LastIP)

	if p.IsIPv4 {
		fmt.Println("Last IP (Decimal):", p.LastIPv4())
	} else {
		fmt.Println("Last IP (Decimal):", p.LastIPv6())
	}

	if p.IsIPv4 {
		fmt.Println("Total Host:", p.HostCountIPv4())
	} else {
		fmt.Println("Total Host:", p.HostCountIPv6())
	}

	// First IP: 1.0.0.0
	// First IP (Decimal): 16777216
	// Last IP: 1.0.0.255
	// Last IP (Decimal): 16777471
	// Total Host: 256


	ip := net.ParseIP("1.0.0.0")
	fmt.Println(IPv4tod(ip)) // 16777216

	ip := net.ParseIP("2001:4860:4860::8888")
	fmt.Println(IPv6tod(ip)) // 42541956123769884636017138956568135816

	var i uint32 = 16777216
	fmt.Println(DtoIPv4(i)) // 1.0.0.0

	b := new(big.Int)
	b.SetString("42541956123769884636017138956568135816", 10)
	fmt.Println(DtoIPv6(b)) // 2001:4860:4860::8888

}

Package usage for SEO

  • get ip from ip range
  • convert ip range to ip
  • golang convert ip range to decimal
  • How to get first/last IP address of CIDR
  • Convert IP version 6 address to integer or decimal number
  • IPv4 to IP Decimal Conversion
  • ipv4 to decimal golang

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DtoIPv4

func DtoIPv4(i uint32) net.IP

func DtoIPv6

func DtoIPv6(i *big.Int) net.IP

func IPv4tod

func IPv4tod(ip net.IP) uint32

IPv4tod convert IPv4 to decimal in case IP is not IPv4 will return 0

func IPv6tod

func IPv6tod(ip net.IP) *big.Int

IPv6tod convert IPv6 to decimal in case IP is not IPv6 will return nil

Types

type ParsedCIDR

type ParsedCIDR struct {
	FirstIP net.IP
	LastIP  net.IP
	IPNet   *net.IPNet
	IsIPv4  bool
	IsIPv6  bool
}

An ParsedCIDR contains FirstIP the first ip of the parsed range LasttIP the last ip of the parsed range IPNet represents an IP network IsIPv4 check for IPv4 IsIPv6 check for IPv6

func ParseCIDR

func ParseCIDR(s string) (*ParsedCIDR, error)

ParseCIDR return ParsedCIDR for the provided IP Range

func (*ParsedCIDR) FirstIPv4

func (p *ParsedCIDR) FirstIPv4() uint32

FirstIPv4 return Decimal FirstIP

func (*ParsedCIDR) FirstIPv6

func (p *ParsedCIDR) FirstIPv6() *big.Int

FirstIPv6 return Decimal FirstIP

func (*ParsedCIDR) HostCountIPv4

func (p *ParsedCIDR) HostCountIPv4() uint32

HostCountIPv4 return number of IPs on the parsed range

func (*ParsedCIDR) HostCountIPv6

func (p *ParsedCIDR) HostCountIPv6() *big.Int

HostCountIPv6 return number of IPs on the parsed range

func (*ParsedCIDR) LastIPv4

func (p *ParsedCIDR) LastIPv4() uint32

LastIPv4 return Decimal LastIP

func (*ParsedCIDR) LastIPv6

func (p *ParsedCIDR) LastIPv6() *big.Int

LastIPv6 return Decimal LastIP

Jump to

Keyboard shortcuts

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