Commit 39c1e8c9 authored by Kirill Smelkov's avatar Kirill Smelkov

xnet: Expose Tracer in public API

NetTrace now returns Tracer instead of general Networker. The reason we
need this is because in the next patch Tracer will implement methods,
additional to just what is required by Networker, to control itself.
parent dde20000
...@@ -41,8 +41,8 @@ import ( ...@@ -41,8 +41,8 @@ import (
// - for similar reasons. // - for similar reasons.
// //
// WARNING NetTrace functionality is currently very draft. // WARNING NetTrace functionality is currently very draft.
func NetTrace(inner Networker, tracerx TraceReceiver) Networker { func NetTrace(inner Networker, tracerx TraceReceiver) *Tracer {
return &netTrace{inner, tracerx} return &Tracer{inner, tracerx}
} }
// TraceReceiver is the interface that needs to be implemented by network trace receivers. // TraceReceiver is the interface that needs to be implemented by network trace receivers.
...@@ -80,49 +80,55 @@ type TraceTx struct { ...@@ -80,49 +80,55 @@ type TraceTx struct {
Pkt []byte Pkt []byte
} }
// netTrace wraps underlying Networker such that whenever a connection is created // Tracer wraps underlying Networker to emit events on networking operations.
// it is wrapped with traceConn. //
type netTrace struct { // Create it via NetTrace.
type Tracer struct {
inner Networker inner Networker
rx TraceReceiver rx TraceReceiver
} }
func (nt *netTrace) Network() string { // Network implements Networker.
return nt.inner.Network() // XXX + "+trace" ? func (t *Tracer) Network() string {
return t.inner.Network() // XXX + "+trace" ?
} }
func (nt *netTrace) Name() string { // Name implements Networker.
return nt.inner.Name() func (t *Tracer) Name() string {
return t.inner.Name()
} }
func (nt *netTrace) Close() error { // Close implements Networker.
func (t *Tracer) Close() error {
// XXX +trace? // XXX +trace?
return nt.inner.Close() return t.inner.Close()
} }
func (nt *netTrace) Dial(ctx context.Context, addr string) (net.Conn, error) { // Dial implements Networker.
nt.rx.TraceNetDial(&TraceDial{Dialer: nt.inner.Name(), Addr: addr}) func (t *Tracer) Dial(ctx context.Context, addr string) (net.Conn, error) {
c, err := nt.inner.Dial(ctx, addr) t.rx.TraceNetDial(&TraceDial{Dialer: t.inner.Name(), Addr: addr})
c, err := t.inner.Dial(ctx, addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
nt.rx.TraceNetConnect(&TraceConnect{Src: c.LocalAddr(), Dst: c.RemoteAddr(), Dialed: addr}) t.rx.TraceNetConnect(&TraceConnect{Src: c.LocalAddr(), Dst: c.RemoteAddr(), Dialed: addr})
return &traceConn{nt, c}, nil return &traceConn{t, c}, nil
} }
func (nt *netTrace) Listen(ctx context.Context, laddr string) (Listener, error) { // Listen implements Networker.
func (t *Tracer) Listen(ctx context.Context, laddr string) (Listener, error) {
// XXX +TraceNetListenPre ? // XXX +TraceNetListenPre ?
l, err := nt.inner.Listen(ctx, laddr) l, err := t.inner.Listen(ctx, laddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
nt.rx.TraceNetListen(&TraceListen{Laddr: l.Addr()}) t.rx.TraceNetListen(&TraceListen{Laddr: l.Addr()})
return &netTraceListener{nt, l}, nil return &netTraceListener{t, l}, nil
} }
// netTraceListener wraps net.Listener to wrap accepted connections with traceConn. // netTraceListener wraps net.Listener to wrap accepted connections with traceConn.
type netTraceListener struct { type netTraceListener struct {
nt *netTrace t *Tracer
Listener Listener
} }
...@@ -131,12 +137,12 @@ func (ntl *netTraceListener) Accept(ctx context.Context) (net.Conn, error) { ...@@ -131,12 +137,12 @@ func (ntl *netTraceListener) Accept(ctx context.Context) (net.Conn, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &traceConn{ntl.nt, c}, nil return &traceConn{ntl.t, c}, nil
} }
// traceConn wraps net.Conn and notifies tracer on Writes. // traceConn wraps net.Conn and notifies tracer on Writes.
type traceConn struct { type traceConn struct {
nt *netTrace t *Tracer
net.Conn net.Conn
} }
...@@ -144,7 +150,7 @@ func (tc *traceConn) Write(b []byte) (int, error) { ...@@ -144,7 +150,7 @@ func (tc *traceConn) Write(b []byte) (int, error) {
// XXX +TraceNetTxPre ? // XXX +TraceNetTxPre ?
n, err := tc.Conn.Write(b) n, err := tc.Conn.Write(b)
if err == nil { if err == nil {
tc.nt.rx.TraceNetTx(&TraceTx{Src: tc.LocalAddr(), Dst: tc.RemoteAddr(), Pkt: b}) tc.t.rx.TraceNetTx(&TraceTx{Src: tc.LocalAddr(), Dst: tc.RemoteAddr(), Pkt: b})
} }
return n, err return n, err
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment