~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/golang.org/x/net/icmp/echo.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}