eap.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package layers
  7. import (
  8. "encoding/binary"
  9. "fmt"
  10. "github.com/google/gopacket"
  11. )
  12. type EAPCode uint8
  13. type EAPType uint8
  14. const (
  15. EAPCodeRequest EAPCode = 1
  16. EAPCodeResponse EAPCode = 2
  17. EAPCodeSuccess EAPCode = 3
  18. EAPCodeFailure EAPCode = 4
  19. // EAPTypeNone means that this EAP layer has no Type or TypeData.
  20. // Success and Failure EAPs will have this set.
  21. EAPTypeNone EAPType = 0
  22. EAPTypeIdentity EAPType = 1
  23. EAPTypeNotification EAPType = 2
  24. EAPTypeNACK EAPType = 3
  25. EAPTypeOTP EAPType = 4
  26. EAPTypeTokenCard EAPType = 5
  27. )
  28. // EAP defines an Extensible Authentication Protocol (rfc 3748) layer.
  29. type EAP struct {
  30. BaseLayer
  31. Code EAPCode
  32. Id uint8
  33. Length uint16
  34. Type EAPType
  35. TypeData []byte
  36. }
  37. // LayerType returns LayerTypeEAP.
  38. func (e *EAP) LayerType() gopacket.LayerType { return LayerTypeEAP }
  39. // DecodeFromBytes decodes the given bytes into this layer.
  40. func (e *EAP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  41. e.Code = EAPCode(data[0])
  42. e.Id = data[1]
  43. e.Length = binary.BigEndian.Uint16(data[2:4])
  44. switch {
  45. case e.Length > 4:
  46. e.Type = EAPType(data[4])
  47. e.TypeData = data[5:]
  48. case e.Length == 4:
  49. e.Type = 0
  50. e.TypeData = nil
  51. default:
  52. return fmt.Errorf("invalid EAP length %d", e.Length)
  53. }
  54. e.BaseLayer.Contents = data[:e.Length]
  55. e.BaseLayer.Payload = data[e.Length:] // Should be 0 bytes
  56. return nil
  57. }
  58. // SerializeTo writes the serialized form of this layer into the
  59. // SerializationBuffer, implementing gopacket.SerializableLayer.
  60. // See the docs for gopacket.SerializableLayer for more info.
  61. func (e *EAP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  62. if opts.FixLengths {
  63. e.Length = uint16(len(e.TypeData) + 1)
  64. }
  65. size := len(e.TypeData) + 4
  66. if size > 4 {
  67. size++
  68. }
  69. bytes, err := b.PrependBytes(size)
  70. if err != nil {
  71. return err
  72. }
  73. bytes[0] = byte(e.Code)
  74. bytes[1] = e.Id
  75. binary.BigEndian.PutUint16(bytes[2:], e.Length)
  76. if size > 4 {
  77. bytes[4] = byte(e.Type)
  78. copy(bytes[5:], e.TypeData)
  79. }
  80. return nil
  81. }
  82. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  83. func (e *EAP) CanDecode() gopacket.LayerClass {
  84. return LayerTypeEAP
  85. }
  86. // NextLayerType returns the layer type contained by this DecodingLayer.
  87. func (e *EAP) NextLayerType() gopacket.LayerType {
  88. return gopacket.LayerTypeZero
  89. }
  90. func decodeEAP(data []byte, p gopacket.PacketBuilder) error {
  91. e := &EAP{}
  92. return decodingLayerDecoder(e, data, p)
  93. }