net/packet: documentation pass.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2020-11-10 22:26:00 -08:00
parent c2cc3acbaf
commit a38e28da07
6 changed files with 116 additions and 83 deletions

View File

@ -6,6 +6,13 @@ package packet
import "encoding/binary"
// icmp4HeaderLength is the size of the ICMPv4 packet header, not
// including the outer IP layer or the variable "response data"
// trailer.
const icmp4HeaderLength = 4
// ICMP4Type is an ICMPv4 type, as specified in
// https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml
type ICMP4Type uint8
const (
@ -30,31 +37,29 @@ func (t ICMP4Type) String() string {
}
}
// ICMP4Code is an ICMPv4 code, as specified in
// https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml
type ICMP4Code uint8
const (
ICMP4NoCode ICMP4Code = 0
)
// ICMP4Header represents an ICMPv4 packet header.
// ICMP4Header is an IPv4+ICMPv4 header.
type ICMP4Header struct {
IP4Header
Type ICMP4Type
Code ICMP4Code
}
const (
icmp4HeaderLength = 4
// icmp4AllHeadersLength is the length of all headers in a ICMPv4 packet.
icmp4AllHeadersLength = ip4HeaderLength + icmp4HeaderLength
)
func (ICMP4Header) Len() int {
return icmp4AllHeadersLength
// Len implements Header.
func (h ICMP4Header) Len() int {
return h.IP4Header.Len() + icmp4HeaderLength
}
// Marshal implements Header.
func (h ICMP4Header) Marshal(buf []byte) error {
if len(buf) < icmp4AllHeadersLength {
if len(buf) < h.Len() {
return errSmallBuffer
}
if len(buf) > maxPacketLength {
@ -68,11 +73,14 @@ func (h ICMP4Header) Marshal(buf []byte) error {
h.IP4Header.Marshal(buf)
binary.BigEndian.PutUint16(buf[22:24], ipChecksum(buf))
binary.BigEndian.PutUint16(buf[22:24], ip4Checksum(buf))
return nil
}
// ToResponse implements Header. TODO: it doesn't implement it
// correctly, instead it statically generates an ICMP Echo Reply
// packet.
func (h *ICMP4Header) ToResponse() {
// TODO: this doesn't implement ToResponse correctly, as it
// assumes the ICMP request type.