-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_request.go
More file actions
37 lines (33 loc) · 1.59 KB
/
Copy pathserver_request.go
File metadata and controls
37 lines (33 loc) · 1.59 KB
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
package acidns
import "context"
// rawRequestKey is the unexported context key under which the Server
// framework stashes the raw bytes of the incoming DNS message before
// dispatching to a Handler.
type rawRequestKey struct{}
// RawRequest returns the raw wire bytes of the inbound DNS message
// associated with ctx. The Server framework attaches the bytes before
// calling the Handler; handlers (and middleware/policies invoked from
// them) can use this to perform TSIG / SIG(0) verification, which
// signs over the original wire encoding rather than a re-marshalled
// view (compression isn't byte-stable).
//
// If ctx was not produced by the Server framework (e.g. a Handler is
// being unit-tested in isolation), the second return is false and the
// caller should fall back to re-marshalling [wire.Message] or refusing
// to verify a signature.
func RawRequest(ctx context.Context) ([]byte, bool) {
b, ok := ctx.Value(rawRequestKey{}).([]byte)
return b, ok
}
// contextWithRawRequest returns ctx with a copy of raw attached. The
// copy is required because UDP listeners read into a sync.Pool-backed
// buffer that's recycled as soon as the handler returns; a Handler
// (or middleware) that stashes the slice into a goroutine or queue
// would otherwise see the next inbound request's bytes overwriting
// its captured "request". TCP follows the same convention so the
// public RawRequest contract is uniform across transports.
func contextWithRawRequest(ctx context.Context, raw []byte) context.Context {
cp := make([]byte, len(raw))
copy(cp, raw)
return context.WithValue(ctx, rawRequestKey{}, cp)
}