github.com/sagernet/wireguard-go@v0.0.0-20231215174105-89dec3b2f3e8/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  type Device interface {
    21  	// File returns the file descriptor of the device.
    22  	File() *os.File
    23  
    24  	// Read one or more packets from the Device (without any additional headers).
    25  	// On a successful read it returns the number of packets read, and sets
    26  	// packet lengths within the sizes slice. len(sizes) must be >= len(bufs).
    27  	// A nonzero offset can be used to instruct the Device on where to begin
    28  	// reading into each element of the bufs slice.
    29  	Read(bufs [][]byte, sizes []int, offset int) (n int, err error)
    30  
    31  	// Write one or more packets to the device (without any additional headers).
    32  	// On a successful write it returns the number of packets written. A nonzero
    33  	// offset can be used to instruct the Device on where to begin writing from
    34  	// each packet contained within the bufs slice.
    35  	Write(bufs [][]byte, offset int) (int, error)
    36  
    37  	// MTU returns the MTU of the Device.
    38  	MTU() (int, error)
    39  
    40  	// Name returns the current name of the Device.
    41  	Name() (string, error)
    42  
    43  	// Events returns a channel of type Event, which is fed Device events.
    44  	Events() <-chan Event
    45  
    46  	// Close stops the Device and closes the Event channel.
    47  	Close() error
    48  
    49  	// BatchSize returns the preferred/max number of packets that can be read or
    50  	// written in a single read/write call. BatchSize must not change over the
    51  	// lifetime of a Device.
    52  	BatchSize() int
    53  }