Package ipv4 implements IP-level socket options for the Internet Protocol version 4.
The IPv4 protocol and basic host requirements for IPv4 are defined in RFC 791 and RFC 1122. Host extensions for multicasting and socket interface extensions for multicast source filters are defined in RFC 1112 and RFC 3678.
import "golang.org/x/net/ipv4"const (
Version = 4 // protocol version
HeaderLen = 20 // header length without extension headers
)// Conn represents a network endpoint that uses IPv4 transport
type Conn struct {
// Has unexported fields
}
func NewConn(c net.Conn) *Conn
func (c *Conn) SetTOS(tos int) error
func (c *Conn) SetTTL(ttl int) error
func (c *Conn) TOS() (int, error)
func (c *Conn) TTL() (int, error)// PacketConn represents a packet-oriented IPv4 endpoint
type PacketConn struct {
// Has unexported fields
}
func NewPacketConn(c net.PacketConn) *PacketConn
func (c *PacketConn) Close() error
func (c *PacketConn) SetTOS(tos int) error
func (c *PacketConn) SetTTL(ttl int) error
func (c *PacketConn) TOS() (int, error)
func (c *PacketConn) TTL() (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) MulticastTTL() (int, error)
func (c *PacketConn) SetMulticastTTL(ttl int) error
// ICMP filter
func (c *PacketConn) ICMPFilter() (*ICMPFilter, error)
func (c *PacketConn) SetICMPFilter(f *ICMPFilter) error
// BPF filter
func (c *PacketConn) SetBPF(filter []bpf.RawInstruction) error// RawConn represents a packet network endpoint for raw IPv4 sockets
type RawConn struct {
// Has unexported fields
}
func NewRawConn(c net.PacketConn) (*RawConn, error)
func (c *RawConn) Close() error
func (c *RawConn) SetTOS(tos int) error
func (c *RawConn) SetTTL(ttl int) error
func (c *RawConn) TOS() (int, error)
func (c *RawConn) TTL() (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 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) MulticastTTL() (int, error)
func (c *RawConn) SetMulticastTTL(ttl int) error
func (c *RawConn) ICMPFilter() (*ICMPFilter, error)
func (c *RawConn) SetICMPFilter(f *ICMPFilter) error
func (c *RawConn) SetBPF(filter []bpf.RawInstruction) error// Header represents an IPv4 header
type Header struct {
Version int // protocol version
Len int // header length
TOS int // type-of-service
TotalLen int // packet total length
ID int // identification
Flags HeaderFlags // flags
FragOff int // fragment offset
TTL int // time-to-live
Protocol int // next protocol
Checksum int // checksum
Src net.IP // source address
Dst net.IP // destination address
Options []byte // options, extension headers
}
func ParseHeader(b []byte) (*Header, error)
func (h *Header) Marshal() ([]byte, error)
func (h *Header) Parse(b []byte) error
func (h *Header) String() string
type HeaderFlags int
const (
MoreFragments HeaderFlags = 1 << iota // more fragments flag
DontFragment // don't fragment flag
)// ControlMessage represents per-packet IP-level socket options
type ControlMessage struct {
TTL int // time-to-live (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)
}
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 (
FlagTTL ControlFlags = 1 << iota // pass TTL on received packet
FlagSrc // pass source address on received packet
FlagDst // pass destination address on received packet
FlagInterface // pass interface index on received packet
)// ICMPFilter represents an ICMP 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 ICMP message type
type ICMPType int
const (
ICMPTypeEchoReply ICMPType = 0
ICMPTypeDestinationUnreachable ICMPType = 3
ICMPTypeRedirect ICMPType = 5
ICMPTypeEcho ICMPType = 8
ICMPTypeRouterAdvertisement ICMPType = 9
ICMPTypeRouterSolicitation ICMPType = 10
ICMPTypeTimeExceeded ICMPType = 11
ICMPTypeParameterProblem ICMPType = 12
ICMPTypeTimestamp ICMPType = 13
ICMPTypeTimestampReply ICMPType = 14
ICMPTypePhoturis ICMPType = 40
ICMPTypeExtendedEchoRequest ICMPType = 42
ICMPTypeExtendedEchoReply ICMPType = 43
)
func (typ ICMPType) Protocol() int
func (typ ICMPType) String() stringtype Message = socket.Messageimport (
"golang.org/x/net/ipv4"
"net"
)
func setSocketOptions(conn net.Conn) error {
ipv4Conn := ipv4.NewConn(conn)
// Set type-of-service (DSCP)
if err := ipv4Conn.SetTOS(0x28); err != nil {
return err
}
// Set time-to-live
if err := ipv4Conn.SetTTL(64); err != nil {
return err
}
return nil
}func joinMulticastGroup() error {
group := net.IPv4(224, 0, 0, 251)
conn, err := net.ListenPacket("udp4", "0.0.0.0:5353")
if err != nil {
return err
}
defer conn.Close()
p := ipv4.NewPacketConn(conn)
// Join multicast group on all interfaces
if err := p.JoinGroup(nil, &net.UDPAddr{IP: group}); err != nil {
return err
}
defer p.LeaveGroup(nil, &net.UDPAddr{IP: group})
// Set multicast options
if err := p.SetMulticastLoopback(true); err != nil {
return err
}
// 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 (TTL=%d, Dst=%v)\n",
n, src, cm.TTL, cm.Dst)
}
}func joinSSM() error {
group := &net.UDPAddr{IP: net.IPv4(232, 1, 1, 1)}
source := &net.UDPAddr{IP: net.IPv4(192, 168, 1, 100)}
conn, err := net.ListenPacket("udp4", "0.0.0.0:9000")
if err != nil {
return err
}
defer conn.Close()
p := ipv4.NewPacketConn(conn)
// Join source-specific multicast group
if err := p.JoinSourceSpecificGroup(nil, group, source); err != nil {
return err
}
defer p.LeaveSourceSpecificGroup(nil, group, source)
// Read from source-specific group
buf := make([]byte, 1500)
n, _, src, err := p.ReadFrom(buf)
if err != nil {
return err
}
fmt.Printf("Received %d bytes from %v\n", n, src)
return nil
}