arp.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2012 Google, Inc. All rights reserved.
  2. // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license
  5. // that can be found in the LICENSE file in the root of the source
  6. // tree.
  7. package layers
  8. import (
  9. "encoding/binary"
  10. "errors"
  11. "fmt"
  12. "github.com/google/gopacket"
  13. )
  14. // Potential values for ARP.Operation.
  15. const (
  16. ARPRequest = 1
  17. ARPReply = 2
  18. )
  19. // ARP is a ARP packet header.
  20. type ARP struct {
  21. BaseLayer
  22. AddrType LinkType
  23. Protocol EthernetType
  24. HwAddressSize uint8
  25. ProtAddressSize uint8
  26. Operation uint16
  27. SourceHwAddress []byte
  28. SourceProtAddress []byte
  29. DstHwAddress []byte
  30. DstProtAddress []byte
  31. }
  32. // LayerType returns LayerTypeARP
  33. func (arp *ARP) LayerType() gopacket.LayerType { return LayerTypeARP }
  34. // DecodeFromBytes decodes the given bytes into this layer.
  35. func (arp *ARP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  36. if len(data) < 8 {
  37. df.SetTruncated()
  38. return fmt.Errorf("ARP length %d too short", len(data))
  39. }
  40. arp.AddrType = LinkType(binary.BigEndian.Uint16(data[0:2]))
  41. arp.Protocol = EthernetType(binary.BigEndian.Uint16(data[2:4]))
  42. arp.HwAddressSize = data[4]
  43. arp.ProtAddressSize = data[5]
  44. arp.Operation = binary.BigEndian.Uint16(data[6:8])
  45. arpLength := 8 + 2*arp.HwAddressSize + 2*arp.ProtAddressSize
  46. if len(data) < int(arpLength) {
  47. df.SetTruncated()
  48. return fmt.Errorf("ARP length %d too short, %d expected", len(data), arpLength)
  49. }
  50. arp.SourceHwAddress = data[8 : 8+arp.HwAddressSize]
  51. arp.SourceProtAddress = data[8+arp.HwAddressSize : 8+arp.HwAddressSize+arp.ProtAddressSize]
  52. arp.DstHwAddress = data[8+arp.HwAddressSize+arp.ProtAddressSize : 8+2*arp.HwAddressSize+arp.ProtAddressSize]
  53. arp.DstProtAddress = data[8+2*arp.HwAddressSize+arp.ProtAddressSize : 8+2*arp.HwAddressSize+2*arp.ProtAddressSize]
  54. arp.Contents = data[:arpLength]
  55. arp.Payload = data[arpLength:]
  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 (arp *ARP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  62. size := 8 + len(arp.SourceHwAddress) + len(arp.SourceProtAddress) + len(arp.DstHwAddress) + len(arp.DstProtAddress)
  63. bytes, err := b.PrependBytes(size)
  64. if err != nil {
  65. return err
  66. }
  67. if opts.FixLengths {
  68. if len(arp.SourceHwAddress) != len(arp.DstHwAddress) {
  69. return errors.New("mismatched hardware address sizes")
  70. }
  71. arp.HwAddressSize = uint8(len(arp.SourceHwAddress))
  72. if len(arp.SourceProtAddress) != len(arp.DstProtAddress) {
  73. return errors.New("mismatched prot address sizes")
  74. }
  75. arp.ProtAddressSize = uint8(len(arp.SourceProtAddress))
  76. }
  77. binary.BigEndian.PutUint16(bytes, uint16(arp.AddrType))
  78. binary.BigEndian.PutUint16(bytes[2:], uint16(arp.Protocol))
  79. bytes[4] = arp.HwAddressSize
  80. bytes[5] = arp.ProtAddressSize
  81. binary.BigEndian.PutUint16(bytes[6:], arp.Operation)
  82. start := 8
  83. for _, addr := range [][]byte{
  84. arp.SourceHwAddress,
  85. arp.SourceProtAddress,
  86. arp.DstHwAddress,
  87. arp.DstProtAddress,
  88. } {
  89. copy(bytes[start:], addr)
  90. start += len(addr)
  91. }
  92. return nil
  93. }
  94. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  95. func (arp *ARP) CanDecode() gopacket.LayerClass {
  96. return LayerTypeARP
  97. }
  98. // NextLayerType returns the layer type contained by this DecodingLayer.
  99. func (arp *ARP) NextLayerType() gopacket.LayerType {
  100. return gopacket.LayerTypePayload
  101. }
  102. func decodeARP(data []byte, p gopacket.PacketBuilder) error {
  103. arp := &ARP{}
  104. return decodingLayerDecoder(arp, data, p)
  105. }