ipx provides general purpose extensions to golang's IP functions in net package

ipx

Coverage Status Go Report Card Godoc license

ipx is a library which provides a set of extensions on go's standart IP functions in net package.

compability with net package

ipx is fully compatible with net package. Also, it implements the necessary functions in net package such as ParseIP, ParseCIDR, CIDRMask etc. Therefore, don't need to import net package additionally.

install

go get github.com/hakansa/ipx

usage

The following examples shows some common use cases for ipx.

You can access the example in The Go Playground

IP

package main

import (
	"fmt"

	"github.com/hakansa/ipx"
)

func main() {
	// ParseIP throws ErrInvalidIP if given ip is not valid
	ip, err := ipx.ParseIP("256.256.256.256")
	if err != nil {
		// invalid ip
	}

	// MustParseIP throws a panic if the given string is not a valid IP address
	ip = ipx.MustParseIP("172.16.16.1")

	// IPv4 returns the IP address (in 16-byte form) of the IPv4 address a.b.c.d.
	ip = ipx.IPv4(172, 16, 16, 1)

	// IsV4 returns true for ipv4 addresses
	ip.IsV4() // true

	// IsV6 returns true for ipv6 addresses
	ip.IsV6() // false 

	// Before checks i is before than given ip 
	ip.Before(ipx.IPv4(172, 16, 16, 2)) // true 

	// IsPrivate returns true if ip is in a private network
	ip.IsPrivate() // true

	// ToInt returns the decimal representation of ip
	// ToInt returns 0 for ipv6 addresses
	ip.ToInt() // 2886733825

	// ToBigInt returns the decimal representation of ip in math.BigInt format
	// ToBigInt can be used for ipv4 and ipv6 addresses
	ip.ToBigInt() // 2886733825

	// ToBinary returns the binary representation of ip
	ip.ToBinary() // 10101100000100000001000000000001

	// ToHex returns the hex representation of ip
	ip.ToHex() // AC101001

	// GetNext returns next IP
	ip.GetNext() // 172.16.16.2

	// GetNextN returns n'th next IP
	ip.GetNextN(uint32(10)) // 172.16.16.11

	// GetAllNextN returns an IP array
	// that contains all IP's until n'th next IP
	ip.GetAllNextN(uint32(3)) // []IP{ 172.16.16.2 , 172.16.16.3 , 172.16.16.4 }

	// GetPrevious returns previous IP
	ip.GetPrevious() // 172.16.16.0

	// GetPreviousN returns n'th previous IP
	ip.GetPreviousN(uint32(2)) // 172.16.15.255

	// GetAllPreviousN returns an IP array
	// that contains all IP's from n'th previous IP
	ip.GetAllPreviousN(uint32(3)) // []IP{ 172.16.15.254 , 172.16.15.255 , 172.16.16.0 }

	// FromInt returns IP address for given integer
	ip = ipx.FromInt(uint32(2886733825)) // 172.16.16.1

	// RandomIPv4 returns a random IPv4 address
	ip = ipx.RandomIPv4() // x.y.z.t

	
	// All the other methods that the net package provides can be used with ipx
	ip.DefaultMask()
	ip.Equal(ipx.IPv4(172, 16, 16, 1))
	ip.IsGlobalUnicast()
	ip.IsInterfaceLocalMulticast()
	ip.IsLinkLocalMulticast()
	ip.IsLinkLocalUnicast()
	ip.IsLoopback()
	ip.IsMulticast()
	ip.IsUnspecified()
	ip.MarshalText()
	ip.Mask(IPMask{})
	ip.String()
	ip.To4()
	ip.To16()
	ip.UnmarshalText()
}

IPNet

package main

import (
	"fmt"

	"github.com/hakansa/ipx"
)

func main() {

	// ParseCIDR parses a string in CIDR notation
	_, ipNet, _ := ipx.ParseCIDR("172.16.16.0/24")

	// MustParseIP throws a panic if the given string is not a valid IP Network
	ipNet = ipx.MustParseCIDR("172.16.16.0/24")

	// IPNumber returns the number of ip addresses in the network
	ipNet.IPNumber() // 256

	// UsableIPNumber returns the number of usable ip addresses in the network
	// Basically it excludes the network address and broadcast address
	ipNet.UsableIPNumber() // 254

	// NetworkSize returns the network size
	ipNet.NetworkSize() // 24

	// FirstIP returns the first IP in network 
	ipNet.FirstIP() // 172.16.16.0

	// FirstUsableIP returns the first usable (addressable) IP in network
	ipNet.FirstUsableIP() // 172.16.16.1

	// LastIP returns the list IP in network
	ipNet.LastIP() // 172.16.16.255

	// LastUsableIP returns the last usable (addressable) IP in network
	ipNet.LastUsableIP() // 172.16.16.254

	// GetAllIP returns all IP's in the network as an array
	ipNet.GetAllIP() // []IP{ 172.16.16.0, 172.16.16.1, ... , 172.16.16.255 }

	// GetAllUsableIP returns all usable IP's in the network as an array
	ipNet.GetAllUsableIP() // []IP{ 172.16.16.1, 172.16.16.2, ... , 172.16.16.254 }

	// RandomIP returns a random ip in the network
	ipNet.RandomIP() // 172.16.16.X

	// Intersects whether the networks intersects the other network
	ipNet.Intersects(ipx.MustParseCIDR("172.16.15.0/23")) // true

	// All the other methods that the net package provides can be used with ipx
	ipNet.Contains(ipx.IPv4(172, 16, 16, 23))
	ipNet.Network()
	ipNet.String()

}

IPRange

package main

import (
	"fmt"

	"github.com/hakansa/ipx"
)

func main() {

	// ParseIPRange parses x and y as an IPRange
	// Upper ip's boundary is excluded
	ipRange, _ := ipx.ParseIPRange("172.16.16.0", "172.16.16.100")

	// ParseIPRange throws a panic if the given strings is not valid IP addresses
	ipRange = ipx.MustParseIPRange("172.16.16.0", "172.16.16.100")

	// NewIPRange creates a new IPRange
	ipRange = ipx.NewIPRange(ipx.IPv4(172, 16, 16, 0), ipx.IPv4(172, 16, 16, 100))

	// Order is not important when creating IPRange
	ipRange = ipx.MustParseIPRange("172.16.16.100", "172.16.16.0")

	// Contains checks if ip is in range
	ipRange.Contains(ipx.IPv4(172, 16, 16, 75)) // true

	// IPNumber returns the number of ip addresses in IPRange
	ipRange.IPNumber() // 100

	// FirstIP returns the first IP in IPRange 
	ipRange.FirstIP() // 172.16.16.0

	// LastIP returns the list IP in IPRange
	ipRange.LastIP() // 172.16.16.99

	// GetAllIP returns all IP's in IPRange as an array
	ipRange.GetAllIP() // []IP{ 172.16.16.0, 172.16.16.1, ... , 172.16.16.99 }

	// RandomIP returns a random ip in IPRange
	ipRange.RandomIP() // 172.16.16.X (0 <= X < 100)

	// Intersects whether the IPRange intersects other IPRange
	ipRange.Intersects(ipx.MustParseIPRange("172.16.16.50", "172.16.16.150")) // true
}
Similar Resources

screen sharing for developers https://screego.net/

screen sharing for developers https://screego.net/

screego/server screen sharing for developers Huge thanks to sipgate for sponsoring this project! Intro In the past I've had some problems sharing my s

Jan 1, 2023

IPIP.net officially supported IP database ipdb format parsing library

IPIP.net officially supported IP database ipdb format parsing library

Dec 27, 2022

ConnPool is a thread safe connection pool for net.Conn interface.

ConnPool is a thread safe connection pool for net.Conn interface. It can be used to manage and reuse connections.

Dec 22, 2022

Fork of Go stdlib's net/http that works with alternative TLS libraries like refraction-networking/utls.

github.com/ooni/oohttp This repository contains a fork of Go's standard library net/http package including patches to allow using this HTTP code with

Sep 29, 2022

Drop-in replacement for Go net/http when running in AWS Lambda & API Gateway

Drop-in replacement for Go net/http when running in AWS Lambda & API Gateway

Package gateway provides a drop-in replacement for net/http's ListenAndServe for use in AWS Lambda & API Gateway, simply swap it out for gateway.Liste

Nov 24, 2022

Cat Balancer is line based load balancer for net cat nc.

Cat Balancer is line based load balancer for net cat nc.

Cat Balancer Cat Balancer is line based load balancer for net cat nc. Usage cb [-p producers-port] [-c consumers-port] One Producer to One Consum

Jul 6, 2022

Go net wrappers that enable TCP Fast Open.

tfo-go tfo-go provides a series of wrappers around net.Listen, net.ListenTCP, net.DialContext, net.Dial, net.DialTCP that seamlessly enable TCP Fast O

Nov 7, 2022

Golang `net/rpc` over SSH using installed SSH program

Golang net/rpc over SSH using installed SSH program This package implements a helper functions to launch an RPC client and server. It uses the install

Nov 16, 2022

A Discord ratelimiter for net/http

A Discord ratelimiter intended to be used with net/http clients using time/x/rate.

Nov 6, 2021
Related tags
A wrapper for exposing a shared endpoint for Google Cloud Functions in go. API styled after Node.JS firebase-functions package.

firebase-fx A wrapper for Google Cloud Functions that simplifies the deployment of serverless applications. Meant to expose a similar API to the Fireb

Nov 7, 2022
🌍 Package tcplisten provides a customizable TCP net.Listener with various performance-related options

Package tcplisten provides customizable TCP net.Listener with various performance-related options: SO_REUSEPORT. This option allows linear scaling ser

Nov 14, 2022
Nov 9, 2022
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http

fasthttp Fast HTTP implementation for Go. Currently fasthttp is successfully used by VertaMedia in a production serving up to 200K rps from more than

Jan 5, 2023
EasyTCP is a light-weight and less painful TCP server framework written in Go (Golang) based on the standard net package.

EasyTCP is a light-weight TCP framework written in Go (Golang), built with message router. EasyTCP helps you build a TCP server easily fast and less painful.

Jan 7, 2023
httpx is a fast and multi-purpose HTTP toolkit allows to run multiple probers using retryablehttp library, it is designed to maintain the result reliability with increased threads.
httpx is a fast and multi-purpose HTTP toolkit allows to run multiple probers using retryablehttp library, it is designed to maintain the result reliability with increased threads.

Features • Installation • Usage • Running httpx • Notes • Join Discord httpx is a fast and multi-purpose HTTP toolkit allow to run multiple probers us

Jan 8, 2023
gRPC dummy service for testing purpose.

gRPC example Pre-requisite Protocol buffer installation $: brew install protobuf $: protoc --version # Ensure compiler version is 3+ Server go Gener

Mar 3, 2022
GoScan is a port-scanner made entirely in Go-lang. The purpose of the tool is to be fast, dynamic and simple so that a professional in the CyberSecurity area can make an optimized list of ports
GoScan is a port-scanner made entirely in Go-lang. The purpose of the tool is to be fast, dynamic and simple so that a professional in the CyberSecurity area can make an optimized list of ports

?? GoScan GoScan is a port-scanner made entirely in Go-lang. The purpose of the tool is to be fast, dynamic and simple so that a professional in the C

Jul 19, 2022
An experimental package that rely on go generics to implement collection functions utilities

go-underscore go-underscore is a utility-belt library for Golang that provides s

Mar 20, 2022