icmp - ActiveState ActiveGo 1.8
...

Package icmp

import "golang.org/x/net/icmp"
Overview
Index
Examples

Overview ▾

Package icmp provides basic functions for the manipulation of messages used in the Internet Control Message Protocols, ICMPv4 and ICMPv6.

ICMPv4 and ICMPv6 are defined in RFC 792 and RFC 4443. Multi-part message support for ICMP is defined in RFC 4884. ICMP extensions for MPLS are defined in RFC 4950. ICMP extensions for interface and next-hop identification are defined in RFC 5837.

Index ▾

func IPv6PseudoHeader(src, dst net.IP) []byte
func ParseIPv4Header(b []byte) (*ipv4.Header, error)
type DefaultMessageBody
    func (p *DefaultMessageBody) Len(proto int) int
    func (p *DefaultMessageBody) Marshal(proto int) ([]byte, error)
type DstUnreach
    func (p *DstUnreach) Len(proto int) int
    func (p *DstUnreach) Marshal(proto int) ([]byte, error)
type Echo
    func (p *Echo) Len(proto int) int
    func (p *Echo) Marshal(proto int) ([]byte, error)
type Extension
type InterfaceInfo
    func (ifi *InterfaceInfo) Len(proto int) int
    func (ifi *InterfaceInfo) Marshal(proto int) ([]byte, error)
type MPLSLabel
type MPLSLabelStack
    func (ls *MPLSLabelStack) Len(proto int) int
    func (ls *MPLSLabelStack) Marshal(proto int) ([]byte, error)
type Message
    func ParseMessage(proto int, b []byte) (*Message, error)
    func (m *Message) Marshal(psh []byte) ([]byte, error)
type MessageBody
type PacketConn
    func ListenPacket(network, address string) (*PacketConn, error)
    func (c *PacketConn) Close() error
    func (c *PacketConn) IPv4PacketConn() *ipv4.PacketConn
    func (c *PacketConn) IPv6PacketConn() *ipv6.PacketConn
    func (c *PacketConn) LocalAddr() net.Addr
    func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, 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) WriteTo(b []byte, dst net.Addr) (int, error)
type PacketTooBig
    func (p *PacketTooBig) Len(proto int) int
    func (p *PacketTooBig) Marshal(proto int) ([]byte, error)
type ParamProb
    func (p *ParamProb) Len(proto int) int
    func (p *ParamProb) Marshal(proto int) ([]byte, error)
type TimeExceeded
    func (p *TimeExceeded) Len(proto int) int
    func (p *TimeExceeded) Marshal(proto int) ([]byte, error)
type Type
Bugs

Package files

dstunreach.go echo.go endpoint.go extension.go helper.go helper_posix.go interface.go ipv4.go ipv6.go listen_posix.go message.go messagebody.go mpls.go multipart.go packettoobig.go paramprob.go timeexceeded.go

func IPv6PseudoHeader

func IPv6PseudoHeader(src, dst net.IP) []byte

IPv6PseudoHeader returns an IPv6 pseudo header for checksum calculation.

func ParseIPv4Header

func ParseIPv4Header(b []byte) (*ipv4.Header, error)

ParseIPv4Header parses b as an IPv4 header of ICMP error message invoking packet, which is contained in ICMP error message.

type DefaultMessageBody

A DefaultMessageBody represents the default message body.

type DefaultMessageBody struct {
    Data []byte // data
}

func (*DefaultMessageBody) Len

func (p *DefaultMessageBody) Len(proto int) int

Len implements the Len method of MessageBody interface.

func (*DefaultMessageBody) Marshal

func (p *DefaultMessageBody) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of MessageBody interface.

type DstUnreach

A DstUnreach represents an ICMP destination unreachable message body.

type DstUnreach struct {
    Data       []byte      // data, known as original datagram field
    Extensions []Extension // extensions
}

func (*DstUnreach) Len

func (p *DstUnreach) Len(proto int) int

Len implements the Len method of MessageBody interface.

func (*DstUnreach) Marshal

func (p *DstUnreach) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of MessageBody interface.

type Echo

An Echo represents an ICMP echo request or reply message body.

type Echo struct {
    ID   int    // identifier
    Seq  int    // sequence number
    Data []byte // data
}

func (*Echo) Len

func (p *Echo) Len(proto int) int

Len implements the Len method of MessageBody interface.

func (*Echo) Marshal

func (p *Echo) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of MessageBody interface.

type Extension

An Extension represents an ICMP extension.

type Extension interface {
    // Len returns the length of ICMP extension.
    // Proto must be either the ICMPv4 or ICMPv6 protocol number.
    Len(proto int) int

    // Marshal returns the binary encoding of ICMP extension.
    // Proto must be either the ICMPv4 or ICMPv6 protocol number.
    Marshal(proto int) ([]byte, error)
}

type InterfaceInfo

An InterfaceInfo represents interface and next-hop identification.

type InterfaceInfo struct {
    Class     int // extension object class number
    Type      int // extension object sub-type
    Interface *net.Interface
    Addr      *net.IPAddr
}

func (*InterfaceInfo) Len

func (ifi *InterfaceInfo) Len(proto int) int

Len implements the Len method of Extension interface.

func (*InterfaceInfo) Marshal

func (ifi *InterfaceInfo) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of Extension interface.

type MPLSLabel

A MPLSLabel represents a MPLS label stack entry.

type MPLSLabel struct {
    Label int  // label value
    TC    int  // traffic class; formerly experimental use
    S     bool // bottom of stack
    TTL   int  // time to live
}

type MPLSLabelStack

A MPLSLabelStack represents a MPLS label stack.

type MPLSLabelStack struct {
    Class  int // extension object class number
    Type   int // extension object sub-type
    Labels []MPLSLabel
}

func (*MPLSLabelStack) Len

func (ls *MPLSLabelStack) Len(proto int) int

Len implements the Len method of Extension interface.

func (*MPLSLabelStack) Marshal

func (ls *MPLSLabelStack) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of Extension interface.

type Message

A Message represents an ICMP message.

type Message struct {
    Type     Type        // type, either ipv4.ICMPType or ipv6.ICMPType
    Code     int         // code
    Checksum int         // checksum
    Body     MessageBody // body
}

func ParseMessage

func ParseMessage(proto int, b []byte) (*Message, error)

ParseMessage parses b as an ICMP message. Proto must be either the ICMPv4 or ICMPv6 protocol number.

func (*Message) Marshal

func (m *Message) Marshal(psh []byte) ([]byte, error)

Marshal returns the binary encoding of the ICMP message m.

For an ICMPv4 message, the returned message always contains the calculated checksum field.

For an ICMPv6 message, the returned message contains the calculated checksum field when psh is not nil, otherwise the kernel will compute the checksum field during the message transmission. When psh is not nil, it must be the pseudo header for IPv6.

type MessageBody

A MessageBody represents an ICMP message body.

type MessageBody interface {
    // Len returns the length of ICMP message body.
    // Proto must be either the ICMPv4 or ICMPv6 protocol number.
    Len(proto int) int

    // Marshal returns the binary encoding of ICMP message body.
    // Proto must be either the ICMPv4 or ICMPv6 protocol number.
    Marshal(proto int) ([]byte, error)
}

type PacketConn

A PacketConn represents a packet network endpoint that uses either ICMPv4 or ICMPv6.

type PacketConn struct {
    // contains filtered or unexported fields
}

Example (NonPrivilegedPing)

Code:

switch runtime.GOOS {
case "darwin":
case "linux":
    log.Println("you may need to adjust the net.ipv4.ping_group_range kernel state")
default:
    log.Println("not supported on", runtime.GOOS)
    return
}

c, err := icmp.ListenPacket("udp6", "fe80::1%en0")
if err != nil {
    log.Fatal(err)
}
defer c.Close()

wm := icmp.Message{
    Type: ipv6.ICMPTypeEchoRequest, Code: 0,
    Body: &icmp.Echo{
        ID: os.Getpid() & 0xffff, Seq: 1,
        Data: []byte("HELLO-R-U-THERE"),
    },
}
wb, err := wm.Marshal(nil)
if err != nil {
    log.Fatal(err)
}
if _, err := c.WriteTo(wb, &net.UDPAddr{IP: net.ParseIP("ff02::1"), Zone: "en0"}); err != nil {
    log.Fatal(err)
}

rb := make([]byte, 1500)
n, peer, err := c.ReadFrom(rb)
if err != nil {
    log.Fatal(err)
}
rm, err := icmp.ParseMessage(58, rb[:n])
if err != nil {
    log.Fatal(err)
}
switch rm.Type {
case ipv6.ICMPTypeEchoReply:
    log.Printf("got reflection from %v", peer)
default:
    log.Printf("got %+v; want echo reply", rm)
}

func ListenPacket

func ListenPacket(network, address string) (*PacketConn, error)

ListenPacket listens for incoming ICMP packets addressed to address. See net.Dial for the syntax of address.

For non-privileged datagram-oriented ICMP endpoints, network must be "udp4" or "udp6". The endpoint allows to read, write a few limited ICMP messages such as echo request and echo reply. Currently only Darwin and Linux support this.

Examples:

ListenPacket("udp4", "192.168.0.1")
ListenPacket("udp4", "0.0.0.0")
ListenPacket("udp6", "fe80::1%en0")
ListenPacket("udp6", "::")

For privileged raw ICMP endpoints, network must be "ip4" or "ip6" followed by a colon and an ICMP protocol number or name.

Examples:

ListenPacket("ip4:icmp", "192.168.0.1")
ListenPacket("ip4:1", "0.0.0.0")
ListenPacket("ip6:ipv6-icmp", "fe80::1%en0")
ListenPacket("ip6:58", "::")

func (*PacketConn) Close

func (c *PacketConn) Close() error

Close closes the endpoint.

func (*PacketConn) IPv4PacketConn

func (c *PacketConn) IPv4PacketConn() *ipv4.PacketConn

IPv4PacketConn returns the ipv4.PacketConn of c. It returns nil when c is not created as the endpoint for ICMPv4.

func (*PacketConn) IPv6PacketConn

func (c *PacketConn) IPv6PacketConn() *ipv6.PacketConn

IPv6PacketConn returns the ipv6.PacketConn of c. It returns nil when c is not created as the endpoint for ICMPv6.

func (*PacketConn) LocalAddr

func (c *PacketConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*PacketConn) ReadFrom

func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error)

ReadFrom reads an ICMP message from the connection.

func (*PacketConn) SetDeadline

func (c *PacketConn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the endpoint.

func (*PacketConn) SetReadDeadline

func (c *PacketConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline associated with the endpoint.

func (*PacketConn) SetWriteDeadline

func (c *PacketConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline associated with the endpoint.

func (*PacketConn) WriteTo

func (c *PacketConn) WriteTo(b []byte, dst net.Addr) (int, error)

WriteTo writes the ICMP message b to dst. Dst must be net.UDPAddr when c is a non-privileged datagram-oriented ICMP endpoint. Otherwise it must be net.IPAddr.

type PacketTooBig

A PacketTooBig represents an ICMP packet too big message body.

type PacketTooBig struct {
    MTU  int    // maximum transmission unit of the nexthop link
    Data []byte // data, known as original datagram field
}

func (*PacketTooBig) Len

func (p *PacketTooBig) Len(proto int) int

Len implements the Len method of MessageBody interface.

func (*PacketTooBig) Marshal

func (p *PacketTooBig) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of MessageBody interface.

type ParamProb

A ParamProb represents an ICMP parameter problem message body.

type ParamProb struct {
    Pointer    uintptr     // offset within the data where the error was detected
    Data       []byte      // data, known as original datagram field
    Extensions []Extension // extensions
}

func (*ParamProb) Len

func (p *ParamProb) Len(proto int) int

Len implements the Len method of MessageBody interface.

func (*ParamProb) Marshal

func (p *ParamProb) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of MessageBody interface.

type TimeExceeded

A TimeExceeded represents an ICMP time exceeded message body.

type TimeExceeded struct {
    Data       []byte      // data, known as original datagram field
    Extensions []Extension // extensions
}

func (*TimeExceeded) Len

func (p *TimeExceeded) Len(proto int) int

Len implements the Len method of MessageBody interface.

func (*TimeExceeded) Marshal

func (p *TimeExceeded) Marshal(proto int) ([]byte, error)

Marshal implements the Marshal method of MessageBody interface.

type Type

A Type represents an ICMP message type.

type Type interface {
    Protocol() int
}

Bugs

  • This package is not implemented on NaCl and Plan 9.