2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-05-13 13:16:17 +00:00
|
|
|
|
|
|
|
package tstun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2022-12-09 23:12:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2020-05-13 13:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeTUN struct {
|
|
|
|
evchan chan tun.Event
|
|
|
|
closechan chan struct{}
|
|
|
|
}
|
|
|
|
|
2021-03-27 05:46:37 +00:00
|
|
|
// NewFake returns a tun.Device that does nothing.
|
|
|
|
func NewFake() tun.Device {
|
2020-05-13 13:16:17 +00:00
|
|
|
return &fakeTUN{
|
|
|
|
evchan: make(chan tun.Event),
|
|
|
|
closechan: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *fakeTUN) File() *os.File {
|
|
|
|
panic("fakeTUN.File() called, which makes no sense")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *fakeTUN) Close() error {
|
|
|
|
close(t.closechan)
|
|
|
|
close(t.evchan)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *fakeTUN) Read(out [][]byte, sizes []int, offset int) (int, error) {
|
2020-10-06 02:36:48 +00:00
|
|
|
<-t.closechan
|
|
|
|
return 0, io.EOF
|
2020-05-13 13:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *fakeTUN) Write(b [][]byte, n int) (int, error) {
|
2020-05-13 13:16:17 +00:00
|
|
|
select {
|
|
|
|
case <-t.closechan:
|
|
|
|
return 0, ErrClosed
|
2020-10-06 02:36:48 +00:00
|
|
|
default:
|
2020-05-13 13:16:17 +00:00
|
|
|
}
|
2022-12-09 01:58:14 +00:00
|
|
|
return 1, nil
|
2020-05-13 13:16:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 20:57:17 +00:00
|
|
|
// FakeTUNName is the name of the fake TUN device.
|
|
|
|
const FakeTUNName = "FakeTUN"
|
|
|
|
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *fakeTUN) Flush() error { return nil }
|
|
|
|
func (t *fakeTUN) MTU() (int, error) { return 1500, nil }
|
2023-05-03 20:57:17 +00:00
|
|
|
func (t *fakeTUN) Name() (string, error) { return FakeTUNName, nil }
|
2022-12-09 01:58:14 +00:00
|
|
|
func (t *fakeTUN) Events() <-chan tun.Event { return t.evchan }
|
|
|
|
func (t *fakeTUN) BatchSize() int { return 1 }
|
2023-10-13 19:41:10 +00:00
|
|
|
func (t *fakeTUN) IsFakeTun() bool { return true }
|