ipvx

package module
v0.0.0-...-5852bd2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2018 License: MIT Imports: 5 Imported by: 0

README

ipvx

ipvx is a light-weight wrapper around net/ipv4 and net/ipv6, to make the task of writing IP version-agnostic programs just a little bit easier when you need to also make use of protocol-specific features like multicast.

Documentation

Documentation

Overview

Package ipvx is a light-weight wrapper around the ipv4 and ipv6 packages, allowing applications to be more protocol agnostic.

The Conn and PacketConn interfaces are largely drop-in replacements for the corresponding structs from the ipv6 package, and translate to ipv4 where needed. Not all methods are implemented, where they seem to be tightly coupled to the specific IP version.

Example
package main

import (
	"net"

	"github.com/dnesting/ipvx"

	"golang.org/x/net/ipv4"
	"golang.org/x/net/ipv6"
)

func main() {
	var conn net.Conn

	// Where you might have done something like this in the past:
	if ipaddr, ok := conn.LocalAddr().(*net.IPAddr); ok {
		if ipaddr.IP.To4() == nil {
			ipv6.NewConn(conn).SetHopLimit(2)
		} else {
			ipv4.NewConn(conn).SetTTL(2)
		}
	}

	// Now you can just do:
	ipvx.NewConn(conn).SetHopLimit(2)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIP

func GetIP(addr net.Addr) (ip net.IP)

GetIP returns the IP address from addr. If addr does not contain an IP address, returns nil. Supports *net.IPAddr, *net.IPNet, *net.TCPAddr, and *net.UDPAddr concrete types.

func Is4

func Is4(addr net.Addr) bool

Is4 returns true if addr represents an IPv4 address.

Types

type Conn

type Conn interface {
	HopLimit() (int, error)
	SetHopLimit(hoplim int) error

	// To4 returns the underlying ipv4.Conn, or nil if this is an IPv6 connection.
	To4() *ipv4.Conn
	// To6 returns the underlying ipv6.Conn, or nil if this is an IPv4 connection.
	To6() *ipv6.Conn
}

Conn represents the set of ipv{4,6}.Conn methods that are protocol-agnostic. See golang.org/x/net/ipv6.Conn for details about each of these methods.

func NewConn

func NewConn(c net.Conn) Conn

NewConn creates an IP protocol-agnostic Conn instance from c. If c does not seem to be an IPv4 connection, it is assumed to be IPv6.

type PacketConn

type PacketConn interface {
	Close() error
	ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
	HopLimit() (int, error)
	IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
	JoinGroup(ifi *net.Interface, group net.Addr) error
	JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
	LeaveGroup(ifi *net.Interface, group net.Addr) error
	LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
	MulticastHopLimit() (int, error)
	MulticastInterface() (*net.Interface, error)
	MulticastLoopback() (bool, error)
	SetBPF(filter []bpf.RawInstruction) error
	SetDeadline(t time.Time) error
	SetHopLimit(hoplim int) error
	SetMulticastHopLimit(hoplim int) error
	SetMulticastInterface(ifi *net.Interface) error
	SetMulticastLoopback(on bool) error
	SetReadDeadline(t time.Time) error
	SetWriteDeadline(t time.Time) error

	// To4 returns the underlying ipv4.PacketConn, or nil if this is an IPv6 PacketConn.
	To4() *ipv4.PacketConn
	// To6 returns the underlying ipv6.PacketConn, or nil if this is an IPv4 PacketConn.
	To6() *ipv6.PacketConn
}

PacketConn represents the set of ipv{4,6}.PacketConn methods that are protocol-agnostic. See golang.org/x/net/ipv6.PacketConn for details about each of these methods.

func NewPacketConn

func NewPacketConn(c net.PacketConn) PacketConn

NewPacketConn creates an IP protocol-agnostic PacketConn instance from c. If c does not seem to be an IPv4 connection, it is assumed to be IPv6.

Jump to

Keyboard shortcuts

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