const ( Version = 4 // protocol version HeaderLen = 20 // header length without extension headers )
func NewControlMessage(cf ControlFlags) []byte
NewControlMessage returns a new control message.
The returned message is large enough for options specified by cf.
A Conn represents a network endpoint that uses the IPv4 transport. It is used to control basic IP-level socket options such as TOS and TTL.
type Conn struct {
// contains filtered or unexported fields
}
▹ Example (MarkingTCP)
func NewConn(c net.Conn) *Conn
NewConn returns a new Conn.
func (c *Conn) SetTOS(tos int) error
SetTOS sets the type-of-service field value for future outgoing packets.
func (c *Conn) SetTTL(ttl int) error
SetTTL sets the time-to-live field value for future outgoing packets.
func (c *Conn) TOS() (int, error)
TOS returns the type-of-service field value for outgoing packets.
func (c *Conn) TTL() (int, error)
TTL returns the time-to-live field value for outgoing packets.
type ControlFlags uint
const ( FlagTTL ControlFlags = 1 << iota // pass the TTL on the received packet FlagSrc // pass the source address on the received packet FlagDst // pass the destination address on the received packet FlagInterface // pass the interface index on the received packet )
A ControlMessage represents per packet basis IP-level socket options.
type ControlMessage struct { // Receiving socket options: SetControlMessage allows to // receive the options from the protocol stack using ReadFrom // method of PacketConn or RawConn. // // Specifying socket options: ControlMessage for WriteTo // method of PacketConn or RawConn allows to send the options // to the protocol stack. // 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 <= value when specifying }
func (cm *ControlMessage) Marshal() []byte
Marshal returns the binary encoding of cm.
func (cm *ControlMessage) Parse(b []byte) error
Parse parses b as a control message and stores the result in cm.
func (cm *ControlMessage) String() string
A 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)
ParseHeader parses b as an IPv4 header.
The provided b must be in the format used by a raw IP socket on the local system. This may differ from the wire format, depending on the system.
func (h *Header) Marshal() ([]byte, error)
Marshal returns the binary encoding of h.
The returned slice is in the format used by a raw IP socket on the local system. This may differ from the wire format, depending on the system.
func (h *Header) Parse(b []byte) error
Parse parses b as an IPv4 header and stores the result in h.
The provided b must be in the format used by a raw IP socket on the local system. This may differ from the wire format, depending on the system.
func (h *Header) String() string
type HeaderFlags int
const ( MoreFragments HeaderFlags = 1 << iota // more fragments flag DontFragment // don't fragment flag )
An ICMPFilter represents an ICMP message filter for incoming packets. The filter belongs to a packet delivery path on a host and it cannot interact with forwarding packets or tunnel-outer packets.
Note: RFC 8200 defines a reasonable role model and it works not only for IPv6 but IPv4. A node means a device that implements IP. A router means a node that forwards IP packets not explicitly addressed to itself, and a host means a node that is not a router.
type ICMPFilter struct {
// contains filtered or unexported fields
}
func (f *ICMPFilter) Accept(typ ICMPType)
Accept accepts incoming ICMP packets including the type field value typ.
func (f *ICMPFilter) Block(typ ICMPType)
Block blocks incoming ICMP packets including the type field value typ.
func (f *ICMPFilter) SetAll(block bool)
SetAll sets the filter action to the filter.
func (f *ICMPFilter) WillBlock(typ ICMPType) bool
WillBlock reports whether the ICMP type will be blocked.
An ICMPType represents a type of ICMP message.
type ICMPType int
Internet Control Message Protocol (ICMP) Parameters, Updated: 2018-02-26
const ( ICMPTypeEchoReply ICMPType = 0 // Echo Reply ICMPTypeDestinationUnreachable ICMPType = 3 // Destination Unreachable ICMPTypeRedirect ICMPType = 5 // Redirect ICMPTypeEcho ICMPType = 8 // Echo ICMPTypeRouterAdvertisement ICMPType = 9 // Router Advertisement ICMPTypeRouterSolicitation ICMPType = 10 // Router Solicitation ICMPTypeTimeExceeded ICMPType = 11 // Time Exceeded ICMPTypeParameterProblem ICMPType = 12 // Parameter Problem ICMPTypeTimestamp ICMPType = 13 // Timestamp ICMPTypeTimestampReply ICMPType = 14 // Timestamp Reply ICMPTypePhoturis ICMPType = 40 // Photuris ICMPTypeExtendedEchoRequest ICMPType = 42 // Extended Echo Request ICMPTypeExtendedEchoReply ICMPType = 43 // Extended Echo Reply )
func (typ ICMPType) Protocol() int
Protocol returns the ICMPv4 protocol number.
func (typ ICMPType) String() string
A Message represents an IO message.
type Message struct { Buffers [][]byte OOB []byte Addr net.Addr N int NN int Flags int }
The Buffers fields represents a list of contiguous buffers, which can be used for vectored IO, for example, putting a header and a payload in each slice. When writing, the Buffers field must contain at least one byte to write. When reading, the Buffers field will always contain a byte to read.
The OOB field contains protocol-specific control or miscellaneous ancillary data known as out-of-band data. It can be nil when not required.
The Addr field specifies a destination address when writing. It can be nil when the underlying protocol of the endpoint uses connection-oriented communication. After a successful read, it may contain the source address on the received packet.
The N field indicates the number of bytes read or written from/to Buffers.
The NN field indicates the number of bytes read or written from/to OOB.
The Flags field contains protocol-specific information on the received message.
type Message = socket.Message
A PacketConn represents a packet network endpoint that uses the IPv4 transport. It is used to control several IP-level socket options including multicasting. It also provides datagram based network I/O methods specific to the IPv4 and higher layer protocols such as UDP.
type PacketConn struct {
// contains filtered or unexported fields
}
▹ Example (ServingOneShotMulticastDNS)
▹ Example (TracingIPPacketRoute)
func NewPacketConn(c net.PacketConn) *PacketConn
NewPacketConn returns a new PacketConn using c as its underlying transport.
func (c *PacketConn) Close() error
Close closes the endpoint.
func (c *PacketConn) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
ExcludeSourceSpecificGroup excludes the source-specific group from the already joined any-source groups by JoinGroup on the interface ifi.
func (c *PacketConn) ICMPFilter() (*ICMPFilter, error)
ICMPFilter returns an ICMP filter. Currently only Linux supports this.
func (c *PacketConn) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
IncludeSourceSpecificGroup includes the excluded source-specific group by ExcludeSourceSpecificGroup again on the interface ifi.
func (c *PacketConn) JoinGroup(ifi *net.Interface, group net.Addr) error
JoinGroup joins the group address group on the interface ifi. By default all sources that can cast data to group are accepted. It's possible to mute and unmute data transmission from a specific source by using ExcludeSourceSpecificGroup and IncludeSourceSpecificGroup. JoinGroup uses the system assigned multicast interface when ifi is nil, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration.
func (c *PacketConn) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
JoinSourceSpecificGroup joins the source-specific group comprising group and source on the interface ifi. JoinSourceSpecificGroup uses the system assigned multicast interface when ifi is nil, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration.
func (c *PacketConn) LeaveGroup(ifi *net.Interface, group net.Addr) error
LeaveGroup leaves the group address group on the interface ifi regardless of whether the group is any-source group or source-specific group.
func (c *PacketConn) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
LeaveSourceSpecificGroup leaves the source-specific group on the interface ifi.
func (c *PacketConn) MulticastInterface() (*net.Interface, error)
MulticastInterface returns the default interface for multicast packet transmissions.
func (c *PacketConn) MulticastLoopback() (bool, error)
MulticastLoopback reports whether transmitted multicast packets should be copied and send back to the originator.
func (c *PacketConn) MulticastTTL() (int, error)
MulticastTTL returns the time-to-live field value for outgoing multicast packets.
func (c *PacketConn) ReadBatch(ms []Message, flags int) (int, error)
ReadBatch reads a batch of messages.
The provided flags is a set of platform-dependent flags, such as syscall.MSG_PEEK.
On a successful read it returns the number of messages received, up to len(ms).
On Linux, a batch read will be optimized. On other platforms, this method will read only a single message.
Unlike the ReadFrom method, it doesn't strip the IPv4 header followed by option headers from the received IPv4 datagram when the underlying transport is net.IPConn. Each Buffers field of Message must be large enough to accommodate an IPv4 header and option headers.
func (c *PacketConn) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error)
ReadFrom reads a payload of the received IPv4 datagram, from the endpoint c, copying the payload into b. It returns the number of bytes copied into b, the control message cm and the source address src of the received datagram.
func (c *PacketConn) SetBPF(filter []bpf.RawInstruction) error
SetBPF attaches a BPF program to the connection.
Only supported on Linux.
func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error
SetControlMessage sets the per packet IP-level socket options.
func (c *PacketConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines associated with the endpoint.
func (c *PacketConn) SetICMPFilter(f *ICMPFilter) error
SetICMPFilter deploys the ICMP filter. Currently only Linux supports this.
func (c *PacketConn) SetMulticastInterface(ifi *net.Interface) error
SetMulticastInterface sets the default interface for future multicast packet transmissions.
func (c *PacketConn) SetMulticastLoopback(on bool) error
SetMulticastLoopback sets whether transmitted multicast packets should be copied and send back to the originator.
func (c *PacketConn) SetMulticastTTL(ttl int) error
SetMulticastTTL sets the time-to-live field value for future outgoing multicast packets.
func (c *PacketConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline associated with the endpoint.
func (c *PacketConn) SetTOS(tos int) error
SetTOS sets the type-of-service field value for future outgoing packets.
func (c *PacketConn) SetTTL(ttl int) error
SetTTL sets the time-to-live field value for future outgoing packets.
func (c *PacketConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline associated with the endpoint.
func (c *PacketConn) TOS() (int, error)
TOS returns the type-of-service field value for outgoing packets.
func (c *PacketConn) TTL() (int, error)
TTL returns the time-to-live field value for outgoing packets.
func (c *PacketConn) WriteBatch(ms []Message, flags int) (int, error)
WriteBatch writes a batch of messages.
The provided flags is a set of platform-dependent flags, such as syscall.MSG_DONTROUTE.
It returns the number of messages written on a successful write.
On Linux, a batch write will be optimized. On other platforms, this method will write only a single message.
func (c *PacketConn) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error)
WriteTo writes a payload of the IPv4 datagram, to the destination address dst through the endpoint c, copying the payload from b. It returns the number of bytes written. The control message cm allows the datagram path and the outgoing interface to be specified. Currently only Darwin and Linux support this. The cm may be nil if control of the outgoing datagram is not required.
A RawConn represents a packet network endpoint that uses the IPv4 transport. It is used to control several IP-level socket options including IPv4 header manipulation. It also provides datagram based network I/O methods specific to the IPv4 and higher layer protocols that handle IPv4 datagram directly such as OSPF, GRE.
type RawConn struct {
// contains filtered or unexported fields
}
▹ Example (AdvertisingOSPFHello)
func NewRawConn(c net.PacketConn) (*RawConn, error)
NewRawConn returns a new RawConn using c as its underlying transport.
func (c *RawConn) Close() error
Close closes the endpoint.
func (c *RawConn) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
ExcludeSourceSpecificGroup excludes the source-specific group from the already joined any-source groups by JoinGroup on the interface ifi.
func (c *RawConn) ICMPFilter() (*ICMPFilter, error)
ICMPFilter returns an ICMP filter. Currently only Linux supports this.
func (c *RawConn) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
IncludeSourceSpecificGroup includes the excluded source-specific group by ExcludeSourceSpecificGroup again on the interface ifi.
func (c *RawConn) JoinGroup(ifi *net.Interface, group net.Addr) error
JoinGroup joins the group address group on the interface ifi. By default all sources that can cast data to group are accepted. It's possible to mute and unmute data transmission from a specific source by using ExcludeSourceSpecificGroup and IncludeSourceSpecificGroup. JoinGroup uses the system assigned multicast interface when ifi is nil, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration.
func (c *RawConn) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
JoinSourceSpecificGroup joins the source-specific group comprising group and source on the interface ifi. JoinSourceSpecificGroup uses the system assigned multicast interface when ifi is nil, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration.
func (c *RawConn) LeaveGroup(ifi *net.Interface, group net.Addr) error
LeaveGroup leaves the group address group on the interface ifi regardless of whether the group is any-source group or source-specific group.
func (c *RawConn) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
LeaveSourceSpecificGroup leaves the source-specific group on the interface ifi.
func (c *RawConn) MulticastInterface() (*net.Interface, error)
MulticastInterface returns the default interface for multicast packet transmissions.
func (c *RawConn) MulticastLoopback() (bool, error)
MulticastLoopback reports whether transmitted multicast packets should be copied and send back to the originator.
func (c *RawConn) MulticastTTL() (int, error)
MulticastTTL returns the time-to-live field value for outgoing multicast packets.
func (c *RawConn) ReadBatch(ms []Message, flags int) (int, error)
ReadBatch reads a batch of messages.
The provided flags is a set of platform-dependent flags, such as syscall.MSG_PEEK.
On a successful read it returns the number of messages received, up to len(ms).
On Linux, a batch read will be optimized. On other platforms, this method will read only a single message.
func (c *RawConn) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error)
ReadFrom reads an IPv4 datagram from the endpoint c, copying the datagram into b. It returns the received datagram as the IPv4 header h, the payload p and the control message cm.
func (c *RawConn) SetBPF(filter []bpf.RawInstruction) error
SetBPF attaches a BPF program to the connection.
Only supported on Linux.
func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error
SetControlMessage sets the per packet IP-level socket options.
func (c *RawConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines associated with the endpoint.
func (c *RawConn) SetICMPFilter(f *ICMPFilter) error
SetICMPFilter deploys the ICMP filter. Currently only Linux supports this.
func (c *RawConn) SetMulticastInterface(ifi *net.Interface) error
SetMulticastInterface sets the default interface for future multicast packet transmissions.
func (c *RawConn) SetMulticastLoopback(on bool) error
SetMulticastLoopback sets whether transmitted multicast packets should be copied and send back to the originator.
func (c *RawConn) SetMulticastTTL(ttl int) error
SetMulticastTTL sets the time-to-live field value for future outgoing multicast packets.
func (c *RawConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline associated with the endpoint.
func (c *RawConn) SetTOS(tos int) error
SetTOS sets the type-of-service field value for future outgoing packets.
func (c *RawConn) SetTTL(ttl int) error
SetTTL sets the time-to-live field value for future outgoing packets.
func (c *RawConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline associated with the endpoint.
func (c *RawConn) TOS() (int, error)
TOS returns the type-of-service field value for outgoing packets.
func (c *RawConn) TTL() (int, error)
TTL returns the time-to-live field value for outgoing packets.
func (c *RawConn) WriteBatch(ms []Message, flags int) (int, error)
WriteBatch writes a batch of messages.
The provided flags is a set of platform-dependent flags, such as syscall.MSG_DONTROUTE.
It returns the number of messages written on a successful write.
On Linux, a batch write will be optimized. On other platforms, this method will write only a single message.
func (c *RawConn) WriteTo(h *Header, p []byte, cm *ControlMessage) error
WriteTo writes an IPv4 datagram through the endpoint c, copying the datagram from the IPv4 header h and the payload p. The control message cm allows the datagram path and the outgoing interface to be specified. Currently only Darwin and Linux support this. The cm may be nil if control of the outgoing datagram is not required.
The IPv4 header h must contain appropriate fields that include:
Version = <must be specified> Len = <must be specified> TOS = <must be specified> TotalLen = <must be specified> ID = platform sets an appropriate value if ID is zero FragOff = <must be specified> TTL = <must be specified> Protocol = <must be specified> Checksum = platform sets an appropriate value if Checksum is zero Src = platform sets an appropriate value if Src is nil Dst = <must be specified> Options = optional
On Windows, the ReadBatch and WriteBatch methods of PacketConn are not implemented.
On Windows, the ReadBatch and WriteBatch methods of RawConn are not implemented.
This package is not implemented on JS, NaCl and Plan 9.
On Windows, the JoinSourceSpecificGroup, LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and IncludeSourceSpecificGroup methods of PacketConn and RawConn are not implemented.
On Windows, the ReadFrom and WriteTo methods of RawConn are not implemented.
On Windows, the ControlMessage for ReadFrom and WriteTo methods of PacketConn is not implemented.