forked from david415/HoneyBadger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
72 lines (61 loc) · 2.12 KB
/
sequence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* sequence.go - this here file is composed of source code directly from gopacket.tcpassembly...
included here for use with HoneyBadger.
*/
package types
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
const (
InvalidSequence Sequence = Sequence(-1)
uint32Max = 0xFFFFFFFF
)
// Sequence is a TCP sequence number. It provides a few convenience functions
// for handling TCP wrap-around. The sequence should always be in the range
// [0,0xFFFFFFFF]... its other bits are simply used in wrap-around calculations
// and should never be set.
type Sequence int64
// Difference defines an ordering for comparing TCP sequences that's safe for
// roll-overs. It returns:
// > 0 : if t comes after s
// < 0 : if t comes before s
// 0 : if t == s
// The number returned is the sequence difference, so 4.Difference(8) will
// return 4.
//
// It handles rollovers by considering any sequence in the first quarter of the
// uint32 space to be after any sequence in the last quarter of that space, thus
// wrapping the uint32 space.
func (s Sequence) Difference(t Sequence) int {
if s > uint32Max-uint32Max/4 && t < uint32Max/4 {
t += uint32Max
} else if t > uint32Max-uint32Max/4 && s < uint32Max/4 {
s += uint32Max
}
return int(t - s)
}
// LessThan returns true if s < t
func (s Sequence) LessThan(t Sequence) bool {
return s.Difference(t) > 0
}
// LessThanOrEqual returns true if s <= t
func (s Sequence) LessThanOrEqual(t Sequence) bool {
return s.Difference(t) >= 0
}
// Equals returns true if s == t
func (s Sequence) Equals(t Sequence) bool {
return s.Difference(t) == 0
}
// GreaterThan returns true if s > t
func (s Sequence) GreaterThan(t Sequence) bool {
return s.Difference(t) < 0
}
// GreaterThanOrEqual returns true if s >= t
func (s Sequence) GreaterThanOrEqual(t Sequence) bool {
return s.Difference(t) <= 0
}
// Add adds an integer to a sequence and returns the resulting sequence.
func (s Sequence) Add(t int) Sequence {
return (s + Sequence(t)) & uint32Max
}