github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/icmp/echo.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package icmp 6 7 // An Echo represents an ICMP echo request or reply message body. 8 type Echo struct { 9 ID int // identifier 10 Seq int // sequence number 11 Data []byte // data 12 } 13 14 // Len implements the Len method of MessageBody interface. 15 func (p *Echo) Len(proto int) int { 16 if p == nil { 17 return 0 18 } 19 return 4 + len(p.Data) 20 } 21 22 // Marshal implements the Marshal method of MessageBody interface. 23 func (p *Echo) Marshal(proto int) ([]byte, error) { 24 b := make([]byte, 4+len(p.Data)) 25 b[0], b[1] = byte(p.ID>>8), byte(p.ID) 26 b[2], b[3] = byte(p.Seq>>8), byte(p.Seq) 27 copy(b[4:], p.Data) 28 return b, nil 29 } 30 31 // parseEcho parses b as an ICMP echo request or reply message body. 32 func parseEcho(proto int, b []byte) (MessageBody, error) { 33 bodyLen := len(b) 34 if bodyLen < 4 { 35 return nil, errMessageTooShort 36 } 37 p := &Echo{ID: int(b[0])<<8 | int(b[1]), Seq: int(b[2])<<8 | int(b[3])} 38 if bodyLen > 4 { 39 p.Data = make([]byte, bodyLen-4) 40 copy(p.Data, b[4:]) 41 } 42 return p, nil 43 }