Fix unit tests
[govpp.git] / vendor / github.com / google / gopacket / layers / ppp.go
1 // Copyright 2012 Google, Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree.
6
7 package layers
8
9 import (
10         "encoding/binary"
11         "errors"
12         "github.com/google/gopacket"
13 )
14
15 // PPP is the layer for PPP encapsulation headers.
16 type PPP struct {
17         BaseLayer
18         PPPType PPPType
19 }
20
21 // PPPEndpoint is a singleton endpoint for PPP.  Since there is no actual
22 // addressing for the two ends of a PPP connection, we use a singleton value
23 // named 'point' for each endpoint.
24 var PPPEndpoint = gopacket.NewEndpoint(EndpointPPP, nil)
25
26 // PPPFlow is a singleton flow for PPP.  Since there is no actual addressing for
27 // the two ends of a PPP connection, we use a singleton value to represent the
28 // flow for all PPP connections.
29 var PPPFlow = gopacket.NewFlow(EndpointPPP, nil, nil)
30
31 // LayerType returns LayerTypePPP
32 func (p *PPP) LayerType() gopacket.LayerType { return LayerTypePPP }
33
34 // LinkFlow returns PPPFlow.
35 func (p *PPP) LinkFlow() gopacket.Flow { return PPPFlow }
36
37 func decodePPP(data []byte, p gopacket.PacketBuilder) error {
38         ppp := &PPP{}
39         if data[0]&0x1 == 0 {
40                 if data[1]&0x1 == 0 {
41                         return errors.New("PPP has invalid type")
42                 }
43                 ppp.PPPType = PPPType(binary.BigEndian.Uint16(data[:2]))
44                 ppp.Contents = data[:2]
45                 ppp.Payload = data[2:]
46         } else {
47                 ppp.PPPType = PPPType(data[0])
48                 ppp.Contents = data[:1]
49                 ppp.Payload = data[1:]
50         }
51         p.AddLayer(ppp)
52         p.SetLinkLayer(ppp)
53         return p.NextDecoder(ppp.PPPType)
54 }
55
56 // SerializeTo writes the serialized form of this layer into the
57 // SerializationBuffer, implementing gopacket.SerializableLayer.
58 // See the docs for gopacket.SerializableLayer for more info.
59 func (p *PPP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
60         if p.PPPType&0x100 == 0 {
61                 bytes, err := b.PrependBytes(2)
62                 if err != nil {
63                         return err
64                 }
65                 binary.BigEndian.PutUint16(bytes, uint16(p.PPPType))
66         } else {
67                 bytes, err := b.PrependBytes(1)
68                 if err != nil {
69                         return err
70                 }
71                 bytes[0] = uint8(p.PPPType)
72         }
73         return nil
74 }