arp.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "github.com/google/gopacket"
  12. )
  13. // Potential values for ARP.Operation.
  14. const (
  15. ARPRequest = 1
  16. ARPReply = 2
  17. )
  18. // ARP is a ARP packet header.
  19. type ARP struct {
  20. BaseLayer
  21. AddrType LinkType
  22. Protocol EthernetType
  23. HwAddressSize uint8
  24. ProtAddressSize uint8
  25. Operation uint16
  26. SourceHwAddress []byte
  27. SourceProtAddress []byte
  28. DstHwAddress []byte
  29. DstProtAddress []byte
  30. }
  31. // LayerType returns LayerTypeARP
  32. func (arp *ARP) LayerType() gopacket.LayerType { return LayerTypeARP }
  33. // DecodeFromBytes decodes the given bytes into this layer.
  34. func (arp *ARP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  35. arp.AddrType = LinkType(binary.BigEndian.Uint16(data[0:2]))
  36. arp.Protocol = EthernetType(binary.BigEndian.Uint16(data[2:4]))
  37. arp.HwAddressSize = data[4]
  38. arp.ProtAddressSize = data[5]
  39. arp.Operation = binary.BigEndian.Uint16(data[6:8])
  40. arp.SourceHwAddress = data[8 : 8+arp.HwAddressSize]
  41. arp.SourceProtAddress = data[8+arp.HwAddressSize : 8+arp.HwAddressSize+arp.ProtAddressSize]
  42. arp.DstHwAddress = data[8+arp.HwAddressSize+arp.ProtAddressSize : 8+2*arp.HwAddressSize+arp.ProtAddressSize]
  43. arp.DstProtAddress = data[8+2*arp.HwAddressSize+arp.ProtAddressSize : 8+2*arp.HwAddressSize+2*arp.ProtAddressSize]
  44. arpLength := 8 + 2*arp.HwAddressSize + 2*arp.ProtAddressSize
  45. arp.Contents = data[:arpLength]
  46. arp.Payload = data[arpLength:]
  47. return nil
  48. }
  49. // SerializeTo writes the serialized form of this layer into the
  50. // SerializationBuffer, implementing gopacket.SerializableLayer.
  51. // See the docs for gopacket.SerializableLayer for more info.
  52. func (arp *ARP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  53. size := 8 + len(arp.SourceHwAddress) + len(arp.SourceProtAddress) + len(arp.DstHwAddress) + len(arp.DstProtAddress)
  54. bytes, err := b.PrependBytes(size)
  55. if err != nil {
  56. return err
  57. }
  58. if opts.FixLengths {
  59. if len(arp.SourceHwAddress) != len(arp.DstHwAddress) {
  60. return errors.New("mismatched hardware address sizes")
  61. }
  62. arp.HwAddressSize = uint8(len(arp.SourceHwAddress))
  63. if len(arp.SourceProtAddress) != len(arp.DstProtAddress) {
  64. return errors.New("mismatched prot address sizes")
  65. }
  66. arp.ProtAddressSize = uint8(len(arp.SourceProtAddress))
  67. }
  68. binary.BigEndian.PutUint16(bytes, uint16(arp.AddrType))
  69. binary.BigEndian.PutUint16(bytes[2:], uint16(arp.Protocol))
  70. bytes[4] = arp.HwAddressSize
  71. bytes[5] = arp.ProtAddressSize
  72. binary.BigEndian.PutUint16(bytes[6:], arp.Operation)
  73. start := 8
  74. for _, addr := range [][]byte{
  75. arp.SourceHwAddress,
  76. arp.SourceProtAddress,
  77. arp.DstHwAddress,
  78. arp.DstProtAddress,
  79. } {
  80. copy(bytes[start:], addr)
  81. start += len(addr)
  82. }
  83. return nil
  84. }
  85. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  86. func (arp *ARP) CanDecode() gopacket.LayerClass {
  87. return LayerTypeARP
  88. }
  89. // NextLayerType returns the layer type contained by this DecodingLayer.
  90. func (arp *ARP) NextLayerType() gopacket.LayerType {
  91. return gopacket.LayerTypePayload
  92. }
  93. func decodeARP(data []byte, p gopacket.PacketBuilder) error {
  94. arp := &ARP{}
  95. return decodingLayerDecoder(arp, data, p)
  96. }