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 ¶
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.
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.