eapol.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. "github.com/google/gopacket"
  10. )
  11. // EAPOL defines an EAP over LAN (802.1x) layer.
  12. type EAPOL struct {
  13. BaseLayer
  14. Version uint8
  15. Type EAPOLType
  16. Length uint16
  17. }
  18. // LayerType returns LayerTypeEAPOL.
  19. func (e *EAPOL) LayerType() gopacket.LayerType { return LayerTypeEAPOL }
  20. // DecodeFromBytes decodes the given bytes into this layer.
  21. func (e *EAPOL) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  22. e.Version = data[0]
  23. e.Type = EAPOLType(data[1])
  24. e.Length = binary.BigEndian.Uint16(data[2:4])
  25. e.BaseLayer = BaseLayer{data[:4], data[4:]}
  26. return nil
  27. }
  28. // SerializeTo writes the serialized form of this layer into the
  29. // SerializationBuffer, implementing gopacket.SerializableLayer
  30. func (e *EAPOL) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  31. bytes, _ := b.PrependBytes(4)
  32. bytes[0] = e.Version
  33. bytes[1] = byte(e.Type)
  34. binary.BigEndian.PutUint16(bytes[2:], e.Length)
  35. return nil
  36. }
  37. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  38. func (e *EAPOL) CanDecode() gopacket.LayerClass {
  39. return LayerTypeEAPOL
  40. }
  41. // NextLayerType returns the layer type contained by this DecodingLayer.
  42. func (e *EAPOL) NextLayerType() gopacket.LayerType {
  43. return e.Type.LayerType()
  44. }
  45. func decodeEAPOL(data []byte, p gopacket.PacketBuilder) error {
  46. e := &EAPOL{}
  47. return decodingLayerDecoder(e, data, p)
  48. }