github.com/forest33/wtun@v0.3.1/tun/tun.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package tun
     7  
     8  import (
     9  	"os"
    10  )
    11  
    12  type Event int
    13  
    14  const (
    15  	EventUp = 1 << iota
    16  	EventDown
    17  	EventMTUUpdate
    18  )
    19  
    20  const DefaultMTU = 1500
    21  
    22  type Device interface {
    23  	// File returns the file descriptor of the device.
    24  	File() *os.File
    25  
    26  	// ReadPackets one or more packets from the Device (without any additional headers).
    27  	// On a successful read it returns the number of packets read, and sets
    28  	// packet lengths within the sizes slice. len(sizes) must be >= len(bufs).
    29  	// A nonzero offset can be used to instruct the Device on where to begin
    30  	// reading into each element of the bufs slice.
    31  	ReadPackets(bufs [][]byte, sizes []int, offset int) (n int, err error)
    32  
    33  	// WritePackets one or more packets to the device (without any additional headers).
    34  	// On a successful write it returns the number of packets written. A nonzero
    35  	// offset can be used to instruct the Device on where to begin writing from
    36  	// each packet contained within the bufs slice.
    37  	WritePackets(bufs [][]byte, offset int) (int, error)
    38  
    39  	// MTU returns the MTU of the Device.
    40  	MTU() (int, error)
    41  
    42  	// Name returns the current name of the Device.
    43  	Name() (string, error)
    44  
    45  	// Events returns a channel of type Event, which is fed Device events.
    46  	Events() <-chan Event
    47  
    48  	// Close stops the Device and closes the Event channel.
    49  	Close() error
    50  
    51  	// BatchSize returns the preferred/max number of packets that can be read or
    52  	// written in a single read/write call. BatchSize must not change over the
    53  	// lifetime of a Device.
    54  	BatchSize() int
    55  
    56  	Read(p []byte) (n int, err error)
    57  	Write(p []byte) (n int, err error)
    58  }