ipconv

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 5 Imported by: 14

README

ipconv

This library provides simple conversion between net.IP and integer (net.IP <--> int). As new feature, library now contains extension of net.ParseIP which returns also byte length of IP address on input.

I hope it will serve you well.

Example

package main

import (
    "fmt"
    "net"
    "github.com/praserx/ipconv"
)

func main() {
    if ip, version, err := net.ParseIP("192.168.1.1"); err != nil && version == 4 {
        fmt.Println(ipconv.IPv4ToInt(ip))
    }
}

Documentation

Overview

Package ipconv provides basic conversion between IP addresses representations. Converter does not require additional settings.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidIPAddress = errors.New("invalid ip address")
View Source
var ErrNotIPv4Address = errors.New("not an IPv4 addres")
View Source
var ErrNotIPv6Address = errors.New("not an IPv6 addres")

Functions

func BigIntToIPv6 added in v1.0.1

func BigIntToIPv6(ipaddr big.Int) net.IP

BigIntToIPv6 converts IP address of version 6 from big integer to net.IP representation.

func IPv4ToInt

func IPv4ToInt(ipaddr net.IP) (uint32, error)

IPv4ToInt converts IP address of version 4 from net.IP to uint32 representation.

func IPv6ToBigInt

func IPv6ToBigInt(ipaddr net.IP) (*big.Int, error)

IPv6ToBigInt converts IP address of version 6 from net.IP to math big integer representation.

func IPv6ToInt

func IPv6ToInt(ipaddr net.IP) ([2]uint64, error)

IPv6ToInt converts IP address of version 6 from net.IP to uint64 array representation. Return value contains high integer value on the first place and low integer value on second place.

func IntToIPv4

func IntToIPv4(ipaddr uint32) net.IP

IntToIPv4 converts IP address of version 4 from integer to net.IP representation.

func IntToIPv6

func IntToIPv6(high, low uint64) net.IP

IntToIPv6 converts IP address of version 6 from integer (high and low value) to net.IP representation.

func ParseIP added in v1.1.0

func ParseIP(s string) (net.IP, int, error)

ParseIP implements extension of net.ParseIP. It returns additional information about IP address bytes length. In general, it works typically as standard net.ParseIP. So if IP is not valid, nil is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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