|
| 1 | +// Addresses, and the rule that keeps the way back exact. |
| 2 | +// |
| 3 | +// Every draw here is made in the same order and from the same ranges the |
| 4 | +// generator used before entry formats existed. That is not tidiness: with |
| 5 | +// timestamps=fixed and the settings left alone, the file has to come out byte |
| 6 | +// for byte as it did, and a single extra call to the generator would shift |
| 7 | +// every entry after it. |
| 8 | +// |
| 9 | +// Which is why pick does not draw when there is only one thing to choose. A |
| 10 | +// list of one is not a choice, and asking for one costs a number out of the |
| 11 | +// stream that the old code never spent. |
| 12 | +package logfile |
| 13 | + |
| 14 | +import ( |
| 15 | + // nosemgrep: go.lang.security.audit.crypto.math_random.math-random-used |
| 16 | + "math/rand/v2" |
| 17 | + "strconv" |
| 18 | +) |
| 19 | + |
| 20 | +// pick chooses one of a list, without spending a draw on a list of one. |
| 21 | +func pick[T any](rng *rand.Rand, xs []T) T { |
| 22 | + if len(xs) == 1 { |
| 23 | + return xs[0] |
| 24 | + } |
| 25 | + return xs[rng.IntN(len(xs))] |
| 26 | +} |
| 27 | + |
| 28 | +const ( |
| 29 | + // v4Longest is 255.255.255.255 and v6Longest is eight groups of four hex |
| 30 | + // digits with seven colons. Both are the worst case, which is what the |
| 31 | + // minimum has to be built from. |
| 32 | + v4Longest = 15 |
| 33 | + v6Longest = 39 |
| 34 | +) |
| 35 | + |
| 36 | +// address is one client address, drawn but not yet written. |
| 37 | +// |
| 38 | +// A value rather than a string, because a log of any size is millions of |
| 39 | +// entries and one string per entry is a multiple of the file in garbage. The |
| 40 | +// resource guard measures exactly that. |
| 41 | +type address struct { |
| 42 | + v6 bool |
| 43 | + parts [8]uint32 |
| 44 | +} |
| 45 | + |
| 46 | +func drawAddress(rng *rand.Rand, o options) address { |
| 47 | + v6 := o.ipv6 |
| 48 | + if o.ipMixed { |
| 49 | + // One draw, so the stream stays predictable, and it is only ever |
| 50 | + // reached when the settings asked for a mixture. |
| 51 | + v6 = rng.IntN(2) == 1 |
| 52 | + } |
| 53 | + var a address |
| 54 | + a.v6 = v6 |
| 55 | + if !v6 { |
| 56 | + // The same ranges, in the same order, as before entry formats |
| 57 | + // existed. Nothing here may change or the way back stops being exact. |
| 58 | + a.parts[0] = uint32(10 + rng.IntN(240)) |
| 59 | + a.parts[1] = uint32(rng.IntN(256)) |
| 60 | + a.parts[2] = uint32(rng.IntN(256)) |
| 61 | + a.parts[3] = uint32(1 + rng.IntN(254)) |
| 62 | + return a |
| 63 | + } |
| 64 | + // Documentation range, so a generated log never names somebody's real |
| 65 | + // network. Groups are written the way a reader sees them, with leading |
| 66 | + // zeros suppressed, which is why the length varies. |
| 67 | + a.parts[0], a.parts[1] = 0x2001, 0x0db8 |
| 68 | + for i := 2; i < 8; i++ { |
| 69 | + a.parts[i] = uint32(rng.IntN(0x10000)) |
| 70 | + } |
| 71 | + return a |
| 72 | +} |
| 73 | + |
| 74 | +// length is how many bytes this address takes when written. |
| 75 | +func (a address) length() int { |
| 76 | + if !a.v6 { |
| 77 | + return decDigits(a.parts[0]) + decDigits(a.parts[1]) + |
| 78 | + decDigits(a.parts[2]) + decDigits(a.parts[3]) + 3 |
| 79 | + } |
| 80 | + n := 7 // the colons |
| 81 | + for _, p := range a.parts { |
| 82 | + n += hexDigits(p) |
| 83 | + } |
| 84 | + return n |
| 85 | +} |
| 86 | + |
| 87 | +func (a address) append(dst []byte) []byte { |
| 88 | + if !a.v6 { |
| 89 | + // No leading zeros. Padding octets to three digits made the line |
| 90 | + // length trivial to predict and produced addresses no real log |
| 91 | + // contains - and a leading zero is read as octal by some address |
| 92 | + // parsers, where 069 is not even valid octal. |
| 93 | + for i := 0; i < 4; i++ { |
| 94 | + if i > 0 { |
| 95 | + dst = append(dst, '.') |
| 96 | + } |
| 97 | + dst = strconv.AppendInt(dst, int64(a.parts[i]), 10) |
| 98 | + } |
| 99 | + return dst |
| 100 | + } |
| 101 | + for i, p := range a.parts { |
| 102 | + if i > 0 { |
| 103 | + dst = append(dst, ':') |
| 104 | + } |
| 105 | + dst = strconv.AppendInt(dst, int64(p), 16) |
| 106 | + } |
| 107 | + return dst |
| 108 | +} |
| 109 | + |
| 110 | +// decDigits is how many characters a byte sized number takes. |
| 111 | +func decDigits(n uint32) int { |
| 112 | + switch { |
| 113 | + case n < 10: |
| 114 | + return 1 |
| 115 | + case n < 100: |
| 116 | + return 2 |
| 117 | + default: |
| 118 | + return 3 |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// hexDigits is how many characters a sixteen bit group takes in hex, written |
| 123 | +// without leading zeros the way every reader shows it. |
| 124 | +func hexDigits(n uint32) int { |
| 125 | + switch { |
| 126 | + case n < 0x10: |
| 127 | + return 1 |
| 128 | + case n < 0x100: |
| 129 | + return 2 |
| 130 | + case n < 0x1000: |
| 131 | + return 3 |
| 132 | + default: |
| 133 | + return 4 |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// longestAddress is the worst case for the settings in force, which is what |
| 138 | +// the minimum entry has to leave room for. |
| 139 | +func longestAddress(o options) int { |
| 140 | + if o.ipv6 || o.ipMixed { |
| 141 | + return v6Longest |
| 142 | + } |
| 143 | + return v4Longest |
| 144 | +} |
| 145 | + |
| 146 | +func longestMethod(o options) int { return longest(o.methods) } |
| 147 | +func longestAgent() int { return longest(agents) } |
| 148 | +func longestTag() int { return longest(tags) } |
| 149 | +func longestLevel() int { return longest(levels) } |
| 150 | + |
| 151 | +func longest(xs []string) int { |
| 152 | + n := 0 |
| 153 | + for _, x := range xs { |
| 154 | + if len(x) > n { |
| 155 | + n = len(x) |
| 156 | + } |
| 157 | + } |
| 158 | + return n |
| 159 | +} |
0 commit comments