Package ipv6 implements IP-level socket options for the Internet Protocol version 6.
The IPv6 protocol is defined in RFC 8200. Socket interface extensions are defined in RFC 3493, RFC 3542 and RFC 3678. MLDv1 and MLDv2 are defined in RFC 2710 and RFC 3810.
import "golang.org/x/net/ipv6"const (
Version = 6 // protocol version
HeaderLen = 40 // header length
)// Conn represents a network endpoint that uses IPv6 transport
type Conn struct {
// Has unexported fields
}
func NewConn(c net.Conn) *Conn
func (c *Conn) SetTrafficClass(tclass int) error
func (c *Conn) SetHopLimit(hoplim int) error
func (c *Conn) TrafficClass() (int, error)
func (c *Conn) HopLimit() (int, error)// PacketConn represents a packet-oriented IPv6 endpoint
type PacketConn struct {
// Has unexported fields
}
func NewPacketConn(c net.PacketConn) *PacketConn
func (c *PacketConn) Close() error
func (c *PacketConn) SetTrafficClass(tclass int) error
func (c *PacketConn) SetHopLimit(hoplim int) error
func (c *PacketConn) TrafficClass() (int, error)
func (c *PacketConn) HopLimit() (int, error)
func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error
func (c *PacketConn) SetDeadline(t time.Time) error
func (c *PacketConn) SetReadDeadline(t time.Time) error
func (c *PacketConn) SetWriteDeadline(t time.Time) error
func (c *PacketConn) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error)
func (c *PacketConn) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error)
func (c *PacketConn) ReadBatch(ms []Message, flags int) (int, error)
func (c *PacketConn) WriteBatch(ms []Message, flags int) (int, error)
// Multicast methods
func (c *PacketConn) JoinGroup(ifi *net.Interface, group net.Addr) error
func (c *PacketConn) LeaveGroup(ifi *net.Interface, group net.Addr) error
func (c *PacketConn) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *PacketConn) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *PacketConn) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *PacketConn) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *PacketConn) MulticastInterface() (*net.Interface, error)
func (c *PacketConn) SetMulticastInterface(ifi *net.Interface) error
func (c *PacketConn) MulticastLoopback() (bool, error)
func (c *PacketConn) SetMulticastLoopback(on bool) error
func (c *PacketConn) MulticastHopLimit() (int, error)
func (c *PacketConn) SetMulticastHopLimit(hoplim int) error
// ICMPv6 filter
func (c *PacketConn) ICMPFilter() (*ICMPFilter, error)
func (c *PacketConn) SetICMPFilter(f *ICMPFilter) error
// BPF filter
func (c *PacketConn) SetBPF(filter []bpf.RawInstruction) error
// Checksum control
func (c *PacketConn) Checksum() (on bool, offset int, err error)
func (c *PacketConn) SetChecksum(on bool, offset int) error// RawConn represents a packet network endpoint for raw IPv6 sockets
type RawConn struct {
// Has unexported fields
}
func NewRawConn(c net.PacketConn) (*RawConn, error)
func (c *RawConn) Close() error
func (c *RawConn) SetTrafficClass(tclass int) error
func (c *RawConn) SetHopLimit(hoplim int) error
func (c *RawConn) TrafficClass() (int, error)
func (c *RawConn) HopLimit() (int, error)
func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error
func (c *RawConn) SetDeadline(t time.Time) error
func (c *RawConn) SetReadDeadline(t time.Time) error
func (c *RawConn) SetWriteDeadline(t time.Time) error
func (c *RawConn) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error)
func (c *RawConn) WriteTo(h *Header, p []byte, cm *ControlMessage) error
func (c *RawConn) ReadBatch(ms []Message, flags int) (int, error)
func (c *RawConn) WriteBatch(ms []Message, flags int) (int, error)
// Multicast and filter methods (same as PacketConn)
func (c *RawConn) JoinGroup(ifi *net.Interface, group net.Addr) error
func (c *RawConn) LeaveGroup(ifi *net.Interface, group net.Addr) error
func (c *RawConn) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *RawConn) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *RawConn) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *RawConn) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
func (c *RawConn) MulticastInterface() (*net.Interface, error)
func (c *RawConn) SetMulticastInterface(ifi *net.Interface) error
func (c *RawConn) MulticastLoopback() (bool, error)
func (c *RawConn) SetMulticastLoopback(on bool) error
func (c *RawConn) MulticastHopLimit() (int, error)
func (c *RawConn) SetMulticastHopLimit(hoplim int) error
func (c *RawConn) ICMPFilter() (*ICMPFilter, error)
func (c *RawConn) SetICMPFilter(f *ICMPFilter) error
func (c *RawConn) SetBPF(filter []bpf.RawInstruction) error
func (c *RawConn) Checksum() (on bool, offset int, err error)
func (c *RawConn) SetChecksum(on bool, offset int) error// Header represents an IPv6 base header
type Header struct {
Version int // protocol version
TrafficClass int // traffic class
FlowLabel int // flow label
PayloadLen int // payload length
NextHeader int // next header
HopLimit int // hop limit
Src net.IP // source address
Dst net.IP // destination address
}
func ParseHeader(b []byte) (*Header, error)
func (h *Header) Marshal() ([]byte, error)
func (h *Header) String() string// ControlMessage represents per-packet IPv6 socket options
type ControlMessage struct {
TrafficClass int // traffic class (receiving only)
HopLimit int // hop limit (receiving only)
Src net.IP // source address (specifying only)
Dst net.IP // destination address (receiving only)
IfIndex int // interface index (must be >= 1 when specifying)
NextHop net.IP // next hop address (specifying only)
}
func NewControlMessage(cf ControlFlags) []byte
func (cm *ControlMessage) Marshal() []byte
func (cm *ControlMessage) Parse(b []byte) error
func (cm *ControlMessage) String() string
type ControlFlags uint
const (
FlagTrafficClass ControlFlags = 1 << iota // pass traffic class on received packet
FlagHopLimit // pass hop limit on received packet
FlagSrc // pass source address on received packet
FlagDst // pass destination address on received packet
FlagInterface // pass interface index on received packet
FlagPathMTU // pass path MTU on received packet
)// ICMPFilter represents an ICMPv6 message filter
type ICMPFilter struct {
// Has unexported fields
}
func (f *ICMPFilter) Accept(typ ICMPType)
func (f *ICMPFilter) Block(typ ICMPType)
func (f *ICMPFilter) SetAll(block bool)
func (f *ICMPFilter) WillBlock(typ ICMPType) bool// ICMPType represents an ICMPv6 message type
type ICMPType int
const (
ICMPTypeDestinationUnreachable ICMPType = 1
ICMPTypePacketTooBig ICMPType = 2
ICMPTypeTimeExceeded ICMPType = 3
ICMPTypeParameterProblem ICMPType = 4
ICMPTypeEchoRequest ICMPType = 128
ICMPTypeEchoReply ICMPType = 129
ICMPTypeMulticastListenerQuery ICMPType = 130
ICMPTypeMulticastListenerReport ICMPType = 131
ICMPTypeMulticastListenerDone ICMPType = 132
ICMPTypeRouterSolicitation ICMPType = 133
ICMPTypeRouterAdvertisement ICMPType = 134
ICMPTypeNeighborSolicitation ICMPType = 135
ICMPTypeNeighborAdvertisement ICMPType = 136
ICMPTypeRedirect ICMPType = 137
ICMPTypeExtendedEchoRequest ICMPType = 160
ICMPTypeExtendedEchoReply ICMPType = 161
)
func (typ ICMPType) Protocol() int
func (typ ICMPType) String() stringtype Message = socket.Messageimport (
"golang.org/x/net/ipv6"
"net"
)
func setIPv6Options(conn net.Conn) error {
ipv6Conn := ipv6.NewConn(conn)
// Set traffic class (DSCP)
if err := ipv6Conn.SetTrafficClass(0x28); err != nil {
return err
}
// Set hop limit
if err := ipv6Conn.SetHopLimit(64); err != nil {
return err
}
return nil
}func joinIPv6MulticastGroup() error {
group := net.ParseIP("ff02::1:ff00:0")
conn, err := net.ListenPacket("udp6", "[::]:5353")
if err != nil {
return err
}
defer conn.Close()
p := ipv6.NewPacketConn(conn)
// Enable control message reception
if err := p.SetControlMessage(ipv6.FlagHopLimit|ipv6.FlagSrc|ipv6.FlagDst, true); err != nil {
return err
}
// Join multicast group
if err := p.JoinGroup(nil, &net.UDPAddr{IP: group}); err != nil {
return err
}
defer p.LeaveGroup(nil, &net.UDPAddr{IP: group})
// Read packets
buf := make([]byte, 1500)
for {
n, cm, src, err := p.ReadFrom(buf)
if err != nil {
return err
}
fmt.Printf("Received %d bytes from %v (HopLimit=%d, Dst=%v)\n",
n, src, cm.HopLimit, cm.Dst)
}
}