Documentation ¶
Overview ¶
Package ipv4 implements IP-level socket options for the Internet Protocol version 4.
The package provides IP-level socket options that allow manipulation of IPv4 facilities.
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. IGMPv1, IGMPv2 and IGMPv3 are defined in RFC 1112, RFC 2236 and RFC 3376. Source-specific multicast is defined in RFC 4607.
Unicasting ¶
The options for unicasting are available for net.TCPConn, net.UDPConn and net.IPConn which are created as network connections that use the IPv4 transport. When a single TCP connection carrying a data flow of multiple packets needs to indicate the flow is important, Conn is used to set the type-of-service field on the IPv4 header for each packet.
ln, err := net.Listen("tcp4", "0.0.0.0:1024") if err != nil { // error handling } defer ln.Close() for { c, err := ln.Accept() if err != nil { // error handling } go func(c net.Conn) { defer c.Close()
The outgoing packets will be labeled DiffServ assured forwarding class 1 low drop precedence, known as AF11 packets.
if err := ipv4.NewConn(c).SetTOS(0x28); err != nil { // error handling } if _, err := c.Write(data); err != nil { // error handling } }(c) }
Multicasting ¶
The options for multicasting are available for net.UDPConn and net.IPConn which are created as network connections that use the IPv4 transport. A few network facilities must be prepared before you begin multicasting, at a minimum joining network interfaces and multicast groups.
en0, err := net.InterfaceByName("en0") if err != nil { // error handling } en1, err := net.InterfaceByIndex(911) if err != nil { // error handling } group := net.IPv4(224, 0, 0, 250)
First, an application listens to an appropriate address with an appropriate service port.
c, err := net.ListenPacket("udp4", "0.0.0.0:1024") if err != nil { // error handling } defer c.Close()
Second, the application joins multicast groups, starts listening to the groups on the specified network interfaces. Note that the service port for transport layer protocol does not matter with this operation as joining groups affects only network and link layer protocols, such as IPv4 and Ethernet.
p := ipv4.NewPacketConn(c) if err := p.JoinGroup(en0, &net.UDPAddr{IP: group}); err != nil { // error handling } if err := p.JoinGroup(en1, &net.UDPAddr{IP: group}); err != nil { // error handling }
The application might set per packet control message transmissions between the protocol stack within the kernel. When the application needs a destination address on an incoming packet, SetControlMessage of PacketConn is used to enable control message transmissions.
if err := p.SetControlMessage(ipv4.FlagDst, true); err != nil { // error handling }
The application could identify whether the received packets are of interest by using the control message that contains the destination address of the received packet.
b := make([]byte, 1500) for { n, cm, src, err := p.ReadFrom(b) if err != nil { // error handling } if cm.Dst.IsMulticast() { if cm.Dst.Equal(group) { // joined group, do something } else { // unknown group, discard continue } }
The application can also send both unicast and multicast packets.
p.SetTOS(0x0) p.SetTTL(16) if _, err := p.WriteTo(data, nil, src); err != nil { // error handling } dst := &net.UDPAddr{IP: group, Port: 1024} for _, ifi := range []*net.Interface{en0, en1} { if err := p.SetMulticastInterface(ifi); err != nil { // error handling } p.SetMulticastTTL(2) if _, err := p.WriteTo(data, nil, dst); err != nil { // error handling } } }
More multicasting ¶
An application that uses PacketConn or RawConn may join multiple multicast groups. For example, a UDP listener with port 1024 might join two different groups across over two different network interfaces by using:
c, err := net.ListenPacket("udp4", "0.0.0.0:1024") if err != nil { // error handling } defer c.Close() p := ipv4.NewPacketConn(c) if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil { // error handling } if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil { // error handling } if err := p.JoinGroup(en1, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}); err != nil { // error handling }
It is possible for multiple UDP listeners that listen on the same UDP port to join the same multicast group. The net package will provide a socket that listens to a wildcard address with reusable UDP port when an appropriate multicast address prefix is passed to the net.ListenPacket or net.ListenUDP.
c1, err := net.ListenPacket("udp4", "224.0.0.0:1024") if err != nil { // error handling } defer c1.Close() c2, err := net.ListenPacket("udp4", "224.0.0.0:1024") if err != nil { // error handling } defer c2.Close() p1 := ipv4.NewPacketConn(c1) if err := p1.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil { // error handling } p2 := ipv4.NewPacketConn(c2) if err := p2.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil { // error handling }
Also it is possible for the application to leave or rejoin a multicast group on the network interface.
if err := p.LeaveGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 248)}); err != nil { // error handling } if err := p.JoinGroup(en0, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}); err != nil { // error handling }
Source-specific multicasting ¶
An application that uses PacketConn or RawConn on IGMPv3 supported platform is able to join source-specific multicast groups. The application may use JoinSourceSpecificGroup and LeaveSourceSpecificGroup for the operation known as "include" mode,
ssmgroup := net.UDPAddr{IP: net.IPv4(232, 7, 8, 9)} ssmsource := net.UDPAddr{IP: net.IPv4(192, 168, 0, 1)} if err := p.JoinSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil { // error handling } if err := p.LeaveSourceSpecificGroup(en0, &ssmgroup, &ssmsource); err != nil { // error handling }
or JoinGroup, ExcludeSourceSpecificGroup, IncludeSourceSpecificGroup and LeaveGroup for the operation known as "exclude" mode.
exclsource := net.UDPAddr{IP: net.IPv4(192, 168, 0, 254)} if err := p.JoinGroup(en0, &ssmgroup); err != nil { // error handling } if err := p.ExcludeSourceSpecificGroup(en0, &ssmgroup, &exclsource); err != nil { // error handling } if err := p.LeaveGroup(en0, &ssmgroup); err != nil { // error handling }
Note that it depends on each platform implementation what happens when an application which runs on IGMPv3 unsupported platform uses JoinSourceSpecificGroup and LeaveSourceSpecificGroup. In general the platform tries to fall back to conversations using IGMPv1 or IGMPv2 and starts to listen to multicast traffic. In the fallback case, ExcludeSourceSpecificGroup and IncludeSourceSpecificGroup may return an error.
Index ¶
- Constants
- func NewControlMessage(cf ControlFlags) []byte
- type Conn
- type ControlFlags
- type ControlMessage
- type Header
- type HeaderFlags
- type ICMPFilter
- type ICMPType
- type Message
- type PacketConn
- func (c *PacketConn) Close() error
- func (c *PacketConn) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *PacketConn) ICMPFilter() (*ICMPFilter, error)
- func (c *PacketConn) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *PacketConn) JoinGroup(ifi *net.Interface, group net.Addr) error
- func (c *PacketConn) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *PacketConn) LeaveGroup(ifi *net.Interface, group net.Addr) error
- func (c *PacketConn) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *PacketConn) MulticastInterface() (*net.Interface, error)
- func (c *PacketConn) MulticastLoopback() (bool, error)
- func (c *PacketConn) MulticastTTL() (int, error)
- func (c *PacketConn) ReadBatch(ms []Message, flags int) (int, error)
- func (c *PacketConn) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error)
- func (c *PacketConn) SetBPF(filter []bpf.RawInstruction) error
- func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error
- func (c *PacketConn) SetDeadline(t time.Time) error
- func (c *PacketConn) SetICMPFilter(f *ICMPFilter) error
- func (c *PacketConn) SetMulticastInterface(ifi *net.Interface) error
- func (c *PacketConn) SetMulticastLoopback(on bool) error
- func (c *PacketConn) SetMulticastTTL(ttl int) error
- func (c *PacketConn) SetReadDeadline(t time.Time) error
- func (c *PacketConn) SetTOS(tos int) error
- func (c *PacketConn) SetTTL(ttl int) error
- func (c *PacketConn) SetWriteDeadline(t time.Time) error
- func (c *PacketConn) TOS() (int, error)
- func (c *PacketConn) TTL() (int, error)
- func (c *PacketConn) WriteBatch(ms []Message, flags int) (int, error)
- func (c *PacketConn) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error)
- type RawConn
- func (c *RawConn) Close() error
- func (c *RawConn) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *RawConn) ICMPFilter() (*ICMPFilter, error)
- func (c *RawConn) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *RawConn) JoinGroup(ifi *net.Interface, group net.Addr) error
- func (c *RawConn) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *RawConn) LeaveGroup(ifi *net.Interface, group net.Addr) error
- func (c *RawConn) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error
- func (c *RawConn) MulticastInterface() (*net.Interface, error)
- func (c *RawConn) MulticastLoopback() (bool, error)
- func (c *RawConn) MulticastTTL() (int, error)
- func (c *RawConn) ReadBatch(ms []Message, flags int) (int, error)
- func (c *RawConn) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error)
- func (c *RawConn) SetBPF(filter []bpf.RawInstruction) error
- func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error
- func (c *RawConn) SetDeadline(t time.Time) error
- func (c *RawConn) SetICMPFilter(f *ICMPFilter) error
- func (c *RawConn) SetMulticastInterface(ifi *net.Interface) error
- func (c *RawConn) SetMulticastLoopback(on bool) error
- func (c *RawConn) SetMulticastTTL(ttl int) error
- func (c *RawConn) SetReadDeadline(t time.Time) error
- func (c *RawConn) SetTOS(tos int) error
- func (c *RawConn) SetTTL(ttl int) error
- func (c *RawConn) SetWriteDeadline(t time.Time) error
- func (c *RawConn) TOS() (int, error)
- func (c *RawConn) TTL() (int, error)
- func (c *RawConn) WriteBatch(ms []Message, flags int) (int, error)
- func (c *RawConn) WriteTo(h *Header, p []byte, cm *ControlMessage) error
- Bugs
Examples ¶
Constants ¶
const ( Version = 4 // protocol version HeaderLen = 20 // header length without extension headers )
Variables ¶
This section is empty.
Functions ¶
func NewControlMessage ¶
func NewControlMessage(cf ControlFlags) []byte
NewControlMessage returns a new control message.
The returned message is large enough for options specified by cf.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
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.
Example (MarkingTCP) ¶
package main import ( "log" "net" "github.com/SandwichDev/net/ipv4" ) func main() { ln, err := net.Listen("tcp", "0.0.0.0:1024") if err != nil { log.Fatal(err) } defer ln.Close() for { c, err := ln.Accept() if err != nil { log.Fatal(err) } go func(c net.Conn) { defer c.Close() if c.RemoteAddr().(*net.TCPAddr).IP.To4() != nil { p := ipv4.NewConn(c) if err := p.SetTOS(0x28); err != nil { // DSCP AF11 log.Fatal(err) } if err := p.SetTTL(128); err != nil { log.Fatal(err) } } if _, err := c.Write([]byte("HELLO-R-U-THERE-ACK")); err != nil { log.Fatal(err) } }(c) } }
Output:
type ControlFlags ¶
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 )
type ControlMessage ¶
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 }
A ControlMessage represents per packet basis IP-level socket options.
func (*ControlMessage) Marshal ¶
func (cm *ControlMessage) Marshal() []byte
Marshal returns the binary encoding of cm.
func (*ControlMessage) Parse ¶
func (cm *ControlMessage) Parse(b []byte) error
Parse parses b as a control message and stores the result in cm.
func (*ControlMessage) String ¶
func (cm *ControlMessage) String() string
type 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 }
A Header represents an IPv4 header.
func ParseHeader ¶
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 (*Header) Marshal ¶
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.
type HeaderFlags ¶
type HeaderFlags int
const ( MoreFragments HeaderFlags = 1 << iota // more fragments flag DontFragment // don't fragment flag )
type ICMPFilter ¶
type ICMPFilter struct {
// contains filtered or unexported fields
}
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.
func (*ICMPFilter) Accept ¶
func (f *ICMPFilter) Accept(typ ICMPType)
Accept accepts incoming ICMP packets including the type field value typ.
func (*ICMPFilter) Block ¶
func (f *ICMPFilter) Block(typ ICMPType)
Block blocks incoming ICMP packets including the type field value typ.
func (*ICMPFilter) SetAll ¶
func (f *ICMPFilter) SetAll(block bool)
SetAll sets the filter action to the filter.
func (*ICMPFilter) WillBlock ¶
func (f *ICMPFilter) WillBlock(typ ICMPType) bool
WillBlock reports whether the ICMP type will be blocked.
type ICMPType ¶
type ICMPType int
An ICMPType represents a type of ICMP message.
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 )
Internet Control Message Protocol (ICMP) Parameters, Updated: 2018-02-26
type Message ¶
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 PacketConn ¶
type PacketConn struct {
// contains filtered or unexported fields
}
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.
Example (ServingOneShotMulticastDNS) ¶
package main import ( "log" "net" "github.com/SandwichDev/net/ipv4" ) func main() { c, err := net.ListenPacket("udp4", "0.0.0.0:5353") // mDNS over UDP if err != nil { log.Fatal(err) } defer c.Close() p := ipv4.NewPacketConn(c) en0, err := net.InterfaceByName("en0") if err != nil { log.Fatal(err) } mDNSLinkLocal := net.UDPAddr{IP: net.IPv4(224, 0, 0, 251)} if err := p.JoinGroup(en0, &mDNSLinkLocal); err != nil { log.Fatal(err) } defer p.LeaveGroup(en0, &mDNSLinkLocal) if err := p.SetControlMessage(ipv4.FlagDst, true); err != nil { log.Fatal(err) } b := make([]byte, 1500) for { _, cm, peer, err := p.ReadFrom(b) if err != nil { log.Fatal(err) } if !cm.Dst.IsMulticast() || !cm.Dst.Equal(mDNSLinkLocal.IP) { continue } answers := []byte("FAKE-MDNS-ANSWERS") // fake mDNS answers, you need to implement this if _, err := p.WriteTo(answers, nil, peer); err != nil { log.Fatal(err) } } }
Output:
Example (TracingIPPacketRoute) ¶
package main import ( "fmt" "log" "net" "os" "time" "github.com/SandwichDev/net/icmp" "github.com/SandwichDev/net/ipv4" ) func main() { // Tracing an IP packet route to www.google.com. const host = "www.google.com" ips, err := net.LookupIP(host) if err != nil { log.Fatal(err) } var dst net.IPAddr for _, ip := range ips { if ip.To4() != nil { dst.IP = ip fmt.Printf("using %v for tracing an IP packet route to %s\n", dst.IP, host) break } } if dst.IP == nil { log.Fatal("no A record found") } c, err := net.ListenPacket("ip4:1", "0.0.0.0") // ICMP for IPv4 if err != nil { log.Fatal(err) } defer c.Close() p := ipv4.NewPacketConn(c) if err := p.SetControlMessage(ipv4.FlagTTL|ipv4.FlagSrc|ipv4.FlagDst|ipv4.FlagInterface, true); err != nil { log.Fatal(err) } wm := icmp.Message{ Type: ipv4.ICMPTypeEcho, Code: 0, Body: &icmp.Echo{ ID: os.Getpid() & 0xffff, Data: []byte("HELLO-R-U-THERE"), }, } rb := make([]byte, 1500) for i := 1; i <= 64; i++ { // up to 64 hops wm.Body.(*icmp.Echo).Seq = i wb, err := wm.Marshal(nil) if err != nil { log.Fatal(err) } if err := p.SetTTL(i); err != nil { log.Fatal(err) } // In the real world usually there are several // multiple traffic-engineered paths for each hop. // You may need to probe a few times to each hop. begin := time.Now() if _, err := p.WriteTo(wb, nil, &dst); err != nil { log.Fatal(err) } if err := p.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil { log.Fatal(err) } n, cm, peer, err := p.ReadFrom(rb) if err != nil { if err, ok := err.(net.Error); ok && err.Timeout() { fmt.Printf("%v\t*\n", i) continue } log.Fatal(err) } rm, err := icmp.ParseMessage(1, rb[:n]) if err != nil { log.Fatal(err) } rtt := time.Since(begin) // In the real world you need to determine whether the // received message is yours using ControlMessage.Src, // ControlMessage.Dst, icmp.Echo.ID and icmp.Echo.Seq. switch rm.Type { case ipv4.ICMPTypeTimeExceeded: names, _ := net.LookupAddr(peer.String()) fmt.Printf("%d\t%v %+v %v\n\t%+v\n", i, peer, names, rtt, cm) case ipv4.ICMPTypeEchoReply: names, _ := net.LookupAddr(peer.String()) fmt.Printf("%d\t%v %+v %v\n\t%+v\n", i, peer, names, rtt, cm) return default: log.Printf("unknown ICMP message: %+v\n", rm) } } }
Output:
func NewPacketConn ¶
func NewPacketConn(c net.PacketConn) *PacketConn
NewPacketConn returns a new PacketConn using c as its underlying transport.
func (*PacketConn) ExcludeSourceSpecificGroup ¶
ExcludeSourceSpecificGroup excludes the source-specific group from the already joined any-source groups by JoinGroup on the interface ifi.
func (*PacketConn) ICMPFilter ¶
func (c *PacketConn) ICMPFilter() (*ICMPFilter, error)
ICMPFilter returns an ICMP filter. Currently only Linux supports this.
func (*PacketConn) IncludeSourceSpecificGroup ¶
IncludeSourceSpecificGroup includes the excluded source-specific group by ExcludeSourceSpecificGroup again on the interface ifi.
func (*PacketConn) JoinGroup ¶
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 (*PacketConn) JoinSourceSpecificGroup ¶
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 (*PacketConn) LeaveGroup ¶
LeaveGroup leaves the group address group on the interface ifi regardless of whether the group is any-source group or source-specific group.
func (*PacketConn) LeaveSourceSpecificGroup ¶
LeaveSourceSpecificGroup leaves the source-specific group on the interface ifi.
func (*PacketConn) MulticastInterface ¶
MulticastInterface returns the default interface for multicast packet transmissions.
func (*PacketConn) MulticastLoopback ¶
MulticastLoopback reports whether transmitted multicast packets should be copied and send back to the originator.
func (*PacketConn) MulticastTTL ¶
MulticastTTL returns the time-to-live field value for outgoing multicast packets.
func (*PacketConn) ReadBatch ¶
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 (*PacketConn) ReadFrom ¶
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 (*PacketConn) SetBPF ¶
func (c *PacketConn) SetBPF(filter []bpf.RawInstruction) error
SetBPF attaches a BPF program to the connection.
Only supported on Linux.
func (*PacketConn) SetControlMessage ¶
func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error
SetControlMessage sets the per packet IP-level socket options.
func (*PacketConn) SetDeadline ¶
func (c *PacketConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines associated with the endpoint.
func (*PacketConn) SetICMPFilter ¶
func (c *PacketConn) SetICMPFilter(f *ICMPFilter) error
SetICMPFilter deploys the ICMP filter. Currently only Linux supports this.
func (*PacketConn) SetMulticastInterface ¶
SetMulticastInterface sets the default interface for future multicast packet transmissions.
func (*PacketConn) SetMulticastLoopback ¶
SetMulticastLoopback sets whether transmitted multicast packets should be copied and send back to the originator.
func (*PacketConn) SetMulticastTTL ¶
SetMulticastTTL sets the time-to-live field value for future outgoing multicast packets.
func (*PacketConn) SetReadDeadline ¶
func (c *PacketConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline associated with the endpoint.
func (*PacketConn) SetTOS ¶
SetTOS sets the type-of-service field value for future outgoing packets.
func (*PacketConn) SetWriteDeadline ¶
func (c *PacketConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline associated with the endpoint.
func (*PacketConn) WriteBatch ¶
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 (*PacketConn) WriteTo ¶
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.
type RawConn ¶
type RawConn struct {
// contains filtered or unexported fields
}
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.
Example (AdvertisingOSPFHello) ¶
package main import ( "log" "net" "runtime" "github.com/SandwichDev/net/ipv4" ) func main() { c, err := net.ListenPacket("ip4:89", "0.0.0.0") // OSPF for IPv4 if err != nil { log.Fatal(err) } defer c.Close() r, err := ipv4.NewRawConn(c) if err != nil { log.Fatal(err) } en0, err := net.InterfaceByName("en0") if err != nil { log.Fatal(err) } allSPFRouters := net.IPAddr{IP: net.IPv4(224, 0, 0, 5)} if err := r.JoinGroup(en0, &allSPFRouters); err != nil { log.Fatal(err) } defer r.LeaveGroup(en0, &allSPFRouters) hello := make([]byte, 24) // fake hello data, you need to implement this ospf := make([]byte, 24) // fake ospf header, you need to implement this ospf[0] = 2 // version 2 ospf[1] = 1 // hello packet ospf = append(ospf, hello...) iph := &ipv4.Header{ Version: ipv4.Version, Len: ipv4.HeaderLen, TOS: 0xc0, // DSCP CS6 TotalLen: ipv4.HeaderLen + len(ospf), TTL: 1, Protocol: 89, Dst: allSPFRouters.IP.To4(), } var cm *ipv4.ControlMessage switch runtime.GOOS { case "darwin", "ios", "linux": cm = &ipv4.ControlMessage{IfIndex: en0.Index} default: if err := r.SetMulticastInterface(en0); err != nil { log.Fatal(err) } } if err := r.WriteTo(iph, ospf, cm); err != nil { log.Fatal(err) } }
Output:
func NewRawConn ¶
func NewRawConn(c net.PacketConn) (*RawConn, error)
NewRawConn returns a new RawConn using c as its underlying transport.
func (*RawConn) ExcludeSourceSpecificGroup ¶
ExcludeSourceSpecificGroup excludes the source-specific group from the already joined any-source groups by JoinGroup on the interface ifi.
func (*RawConn) ICMPFilter ¶
func (c *RawConn) ICMPFilter() (*ICMPFilter, error)
ICMPFilter returns an ICMP filter. Currently only Linux supports this.
func (*RawConn) IncludeSourceSpecificGroup ¶
IncludeSourceSpecificGroup includes the excluded source-specific group by ExcludeSourceSpecificGroup again on the interface ifi.
func (*RawConn) JoinGroup ¶
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 (*RawConn) JoinSourceSpecificGroup ¶
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 (*RawConn) LeaveGroup ¶
LeaveGroup leaves the group address group on the interface ifi regardless of whether the group is any-source group or source-specific group.
func (*RawConn) LeaveSourceSpecificGroup ¶
LeaveSourceSpecificGroup leaves the source-specific group on the interface ifi.
func (*RawConn) MulticastInterface ¶
MulticastInterface returns the default interface for multicast packet transmissions.
func (*RawConn) MulticastLoopback ¶
MulticastLoopback reports whether transmitted multicast packets should be copied and send back to the originator.
func (*RawConn) MulticastTTL ¶
MulticastTTL returns the time-to-live field value for outgoing multicast packets.
func (*RawConn) ReadBatch ¶
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 (*RawConn) ReadFrom ¶
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 (*RawConn) SetBPF ¶
func (c *RawConn) SetBPF(filter []bpf.RawInstruction) error
SetBPF attaches a BPF program to the connection.
Only supported on Linux.
func (*RawConn) SetControlMessage ¶
func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error
SetControlMessage sets the per packet IP-level socket options.
func (*RawConn) SetDeadline ¶
SetDeadline sets the read and write deadlines associated with the endpoint.
func (*RawConn) SetICMPFilter ¶
func (c *RawConn) SetICMPFilter(f *ICMPFilter) error
SetICMPFilter deploys the ICMP filter. Currently only Linux supports this.
func (*RawConn) SetMulticastInterface ¶
SetMulticastInterface sets the default interface for future multicast packet transmissions.
func (*RawConn) SetMulticastLoopback ¶
SetMulticastLoopback sets whether transmitted multicast packets should be copied and send back to the originator.
func (*RawConn) SetMulticastTTL ¶
SetMulticastTTL sets the time-to-live field value for future outgoing multicast packets.
func (*RawConn) SetReadDeadline ¶
SetReadDeadline sets the read deadline associated with the endpoint.
func (*RawConn) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline associated with the endpoint.
func (*RawConn) WriteBatch ¶
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 (*RawConn) WriteTo ¶
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
Notes ¶
Bugs ¶
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.
Source Files ¶
- batch.go
- control.go
- control_pktinfo.go
- control_unix.go
- dgramopt.go
- doc.go
- endpoint.go
- genericopt.go
- header.go
- helper.go
- iana.go
- icmp.go
- icmp_linux.go
- packet.go
- payload.go
- payload_cmsg.go
- sockopt.go
- sockopt_posix.go
- sys_asmreq_stub.go
- sys_asmreqn.go
- sys_bpf.go
- sys_linux.go
- sys_ssmreq.go
- zsys_linux_amd64.go