forked from google/gopacket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheap.go
114 lines (101 loc) · 2.85 KB
/
eap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package layers
import (
"encoding/binary"
"fmt"
"github.com/kubeshark/gopacket"
)
type EAPCode uint8
type EAPType uint8
const (
EAPCodeRequest EAPCode = 1
EAPCodeResponse EAPCode = 2
EAPCodeSuccess EAPCode = 3
EAPCodeFailure EAPCode = 4
// EAPTypeNone means that this EAP layer has no Type or TypeData.
// Success and Failure EAPs will have this set.
EAPTypeNone EAPType = 0
EAPTypeIdentity EAPType = 1
EAPTypeNotification EAPType = 2
EAPTypeNACK EAPType = 3
EAPTypeOTP EAPType = 4
EAPTypeTokenCard EAPType = 5
)
// EAP defines an Extensible Authentication Protocol (rfc 3748) layer.
type EAP struct {
BaseLayer
Code EAPCode
Id uint8
Length uint16
Type EAPType
TypeData []byte
}
// LayerType returns LayerTypeEAP.
func (e *EAP) LayerType() gopacket.LayerType { return LayerTypeEAP }
// DecodeFromBytes decodes the given bytes into this layer.
func (e *EAP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < 4 {
df.SetTruncated()
return fmt.Errorf("EAP length %d too short", len(data))
}
e.Code = EAPCode(data[0])
e.Id = data[1]
e.Length = binary.BigEndian.Uint16(data[2:4])
if len(data) < int(e.Length) {
df.SetTruncated()
return fmt.Errorf("EAP length %d too short, %d expected", len(data), e.Length)
}
switch {
case e.Length > 4:
e.Type = EAPType(data[4])
e.TypeData = data[5:]
case e.Length == 4:
e.Type = 0
e.TypeData = nil
default:
return fmt.Errorf("invalid EAP length %d", e.Length)
}
e.BaseLayer.Contents = data[:e.Length]
e.BaseLayer.Payload = data[e.Length:] // Should be 0 bytes
return nil
}
// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (e *EAP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
if opts.FixLengths {
e.Length = uint16(len(e.TypeData) + 1)
}
size := len(e.TypeData) + 4
if size > 4 {
size++
}
bytes, err := b.PrependBytes(size)
if err != nil {
return err
}
bytes[0] = byte(e.Code)
bytes[1] = e.Id
binary.BigEndian.PutUint16(bytes[2:], e.Length)
if size > 4 {
bytes[4] = byte(e.Type)
copy(bytes[5:], e.TypeData)
}
return nil
}
// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (e *EAP) CanDecode() gopacket.LayerClass {
return LayerTypeEAP
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (e *EAP) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypeZero
}
func decodeEAP(data []byte, p gopacket.PacketBuilder) error {
e := &EAP{}
return decodingLayerDecoder(e, data, p)
}