github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/vif/buffer/data_other.go (about)

     1  //go:build !darwin
     2  // +build !darwin
     3  
     4  package buffer
     5  
     6  type Data struct {
     7  	buf []byte
     8  }
     9  
    10  // Buf returns this Data's buffer. This is the buffer that should be used everywhere
    11  // except for the tun.Device ReadPacket and WritePacket methods.
    12  func (d *Data) Buf() []byte {
    13  	return d.buf
    14  }
    15  
    16  // Copy copies n bytes from the given Data buffer into a new Data and returns it.
    17  func (d *Data) Copy(n int) *Data {
    18  	c := NewData(n)
    19  	copy(c.buf, d.buf)
    20  	return c
    21  }
    22  
    23  // Raw returns this Data's raw buffer. This is the buffer that should be used by the tun.Device
    24  // ReadPacket and WritePacket methods. It uses the same underlying byte array as Buf but might be
    25  // offset before Buf to allow for leading bytes that are provided before the IP header.
    26  func (d *Data) Raw() []byte {
    27  	return d.buf
    28  }
    29  
    30  func NewData(sz int) *Data {
    31  	return &Data{buf: make([]byte, sz)}
    32  }
    33  
    34  func (d *Data) Resize(size int) {
    35  	if size <= cap(d.buf) {
    36  		d.buf = d.buf[:size]
    37  	} else {
    38  		d.buf = make([]byte, size)
    39  	}
    40  }