-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loosely inspired by nettrace/httptrace, allows functions to be called when sockets are read from or written to. The hooks are specified via the context (with a WithSockTrace function). Only implemented for network sockets on POSIX systems. Updates tailscale/corp#9230 Updates #58 Signed-off-by: Jenny Zhang <jz@tailscale.com> (Cherry-picked from fb11c0d)
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
pkg net, func SetDialEnforcer(func(context.Context, []Addr) error) #55 | ||
pkg net, func SetResolveEnforcer(func(context.Context, string, string, string, Addr) error) #55 | ||
pkg net, func WithSockTrace(context.Context, *SockTrace) context.Context #58 | ||
pkg net, func ContextSockTrace(context.Context) *SockTrace #58 | ||
pkg net, type SockTrace struct #58 | ||
pkg net, type SockTrace struct, DidRead func(int) #58 | ||
pkg net, type SockTrace struct, DidWrite func(int) #58 | ||
pkg net, type SockTrace struct, WillOverwrite func(*SockTrace) #58 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package net | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// SockTrace is a set of hooks to run at various operations on a network socket. | ||
// Any particular hook may be nil. Functions may be called concurrently from | ||
// different goroutines. | ||
type SockTrace struct { | ||
// DidRead is called after a successful read from the socket, where n bytes | ||
// were read. | ||
DidRead func(n int) | ||
// DidWrite is called after a successful write to the socket, where n bytes | ||
// were written. | ||
DidWrite func(n int) | ||
// WillOverwrite is called when the registered trace is overwritten by a | ||
// subsequent call to WithSockTrace. The provided trace is the new trace | ||
// that will be used. | ||
WillOverwrite func(trace *SockTrace) | ||
} | ||
|
||
// WithSockTrace returns a new context based on the provided parent | ||
// ctx. Socket reads and writes made with the returned context will use | ||
// the provided trace hooks. Any previous hooks registered with ctx are | ||
// ovewritten (their WillOverwrite hook will be called). | ||
func WithSockTrace(ctx context.Context, trace *SockTrace) context.Context { | ||
if previous := ContextSockTrace(ctx); previous != nil && previous.WillOverwrite != nil { | ||
previous.WillOverwrite(trace) | ||
} | ||
return context.WithValue(ctx, sockTraceKey{}, trace) | ||
} | ||
|
||
// ContextSockTrace returns the SockTrace associated with the | ||
// provided context. If none, it returns nil. | ||
func ContextSockTrace(ctx context.Context) *SockTrace { | ||
trace, _ := ctx.Value(sockTraceKey{}).(*SockTrace) | ||
return trace | ||
} | ||
|
||
// unique type to prevent assignment. | ||
type sockTraceKey struct{} |