github.com/slackhq/nebula@v1.9.0/overlay/user.go (about)

     1  package overlay
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/slackhq/nebula/config"
     9  	"github.com/slackhq/nebula/iputil"
    10  )
    11  
    12  func NewUserDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
    13  	return NewUserDevice(tunCidr)
    14  }
    15  
    16  func NewUserDevice(tunCidr *net.IPNet) (Device, error) {
    17  	// these pipes guarantee each write/read will match 1:1
    18  	or, ow := io.Pipe()
    19  	ir, iw := io.Pipe()
    20  	return &UserDevice{
    21  		tunCidr:        tunCidr,
    22  		outboundReader: or,
    23  		outboundWriter: ow,
    24  		inboundReader:  ir,
    25  		inboundWriter:  iw,
    26  	}, nil
    27  }
    28  
    29  type UserDevice struct {
    30  	tunCidr *net.IPNet
    31  
    32  	outboundReader *io.PipeReader
    33  	outboundWriter *io.PipeWriter
    34  
    35  	inboundReader *io.PipeReader
    36  	inboundWriter *io.PipeWriter
    37  }
    38  
    39  func (d *UserDevice) Activate() error {
    40  	return nil
    41  }
    42  func (d *UserDevice) Cidr() *net.IPNet                      { return d.tunCidr }
    43  func (d *UserDevice) Name() string                          { return "faketun0" }
    44  func (d *UserDevice) RouteFor(ip iputil.VpnIp) iputil.VpnIp { return ip }
    45  func (d *UserDevice) NewMultiQueueReader() (io.ReadWriteCloser, error) {
    46  	return d, nil
    47  }
    48  
    49  func (d *UserDevice) Pipe() (*io.PipeReader, *io.PipeWriter) {
    50  	return d.inboundReader, d.outboundWriter
    51  }
    52  
    53  func (d *UserDevice) Read(p []byte) (n int, err error) {
    54  	return d.outboundReader.Read(p)
    55  }
    56  func (d *UserDevice) Write(p []byte) (n int, err error) {
    57  	return d.inboundWriter.Write(p)
    58  }
    59  func (d *UserDevice) Close() error {
    60  	d.inboundWriter.Close()
    61  	d.outboundWriter.Close()
    62  	return nil
    63  }