layerclass.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 gopacket
  7. // LayerClass is a set of LayerTypes, used for grabbing one of a number of
  8. // different types from a packet.
  9. type LayerClass interface {
  10. // Contains returns true if the given layer type should be considered part
  11. // of this layer class.
  12. Contains(LayerType) bool
  13. // LayerTypes returns the set of all layer types in this layer class.
  14. // Note that this may not be a fast operation on all LayerClass
  15. // implementations.
  16. LayerTypes() []LayerType
  17. }
  18. // Contains implements LayerClass.
  19. func (l LayerType) Contains(a LayerType) bool {
  20. return l == a
  21. }
  22. // LayerTypes implements LayerClass.
  23. func (l LayerType) LayerTypes() []LayerType {
  24. return []LayerType{l}
  25. }
  26. // LayerClassSlice implements a LayerClass with a slice.
  27. type LayerClassSlice []bool
  28. // Contains returns true if the given layer type should be considered part
  29. // of this layer class.
  30. func (s LayerClassSlice) Contains(t LayerType) bool {
  31. return int(t) < len(s) && s[t]
  32. }
  33. // LayerTypes returns all layer types in this LayerClassSlice.
  34. // Because of LayerClassSlice's implementation, this could be quite slow.
  35. func (s LayerClassSlice) LayerTypes() (all []LayerType) {
  36. for i := 0; i < len(s); i++ {
  37. if s[i] {
  38. all = append(all, LayerType(i))
  39. }
  40. }
  41. return
  42. }
  43. // NewLayerClassSlice creates a new LayerClassSlice by creating a slice of
  44. // size max(types) and setting slice[t] to true for each type t. Note, if
  45. // you implement your own LayerType and give it a high value, this WILL create
  46. // a very large slice.
  47. func NewLayerClassSlice(types []LayerType) LayerClassSlice {
  48. var max LayerType
  49. for _, typ := range types {
  50. if typ > max {
  51. max = typ
  52. }
  53. }
  54. t := make([]bool, int(max+1))
  55. for _, typ := range types {
  56. t[typ] = true
  57. }
  58. return t
  59. }
  60. // LayerClassMap implements a LayerClass with a map.
  61. type LayerClassMap map[LayerType]bool
  62. // Contains returns true if the given layer type should be considered part
  63. // of this layer class.
  64. func (m LayerClassMap) Contains(t LayerType) bool {
  65. return m[t]
  66. }
  67. // LayerTypes returns all layer types in this LayerClassMap.
  68. func (m LayerClassMap) LayerTypes() (all []LayerType) {
  69. for t := range m {
  70. all = append(all, t)
  71. }
  72. return
  73. }
  74. // NewLayerClassMap creates a LayerClassMap and sets map[t] to true for each
  75. // type in types.
  76. func NewLayerClassMap(types []LayerType) LayerClassMap {
  77. m := LayerClassMap{}
  78. for _, typ := range types {
  79. m[typ] = true
  80. }
  81. return m
  82. }
  83. // NewLayerClass creates a LayerClass, attempting to be smart about which type
  84. // it creates based on which types are passed in.
  85. func NewLayerClass(types []LayerType) LayerClass {
  86. for _, typ := range types {
  87. if typ > maxLayerType {
  88. // NewLayerClassSlice could create a very large object, so instead create
  89. // a map.
  90. return NewLayerClassMap(types)
  91. }
  92. }
  93. return NewLayerClassSlice(types)
  94. }