github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/tcpip/link/tun/protocol.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tun
    16  
    17  import (
    18  	"encoding/binary"
    19  
    20  	"github.com/ttpreport/gvisor-ligolo/pkg/tcpip"
    21  )
    22  
    23  const (
    24  	// PacketInfoHeaderSize is the size of the packet information header.
    25  	PacketInfoHeaderSize = 4
    26  
    27  	offsetFlags    = 0
    28  	offsetProtocol = 2
    29  )
    30  
    31  // PacketInfoFields contains fields sent through the wire if IFF_NO_PI flag is
    32  // not set.
    33  type PacketInfoFields struct {
    34  	Flags    uint16
    35  	Protocol tcpip.NetworkProtocolNumber
    36  }
    37  
    38  // PacketInfoHeader is the wire representation of the packet information sent if
    39  // IFF_NO_PI flag is not set.
    40  type PacketInfoHeader []byte
    41  
    42  // Encode encodes f into h.
    43  func (h PacketInfoHeader) Encode(f *PacketInfoFields) {
    44  	binary.BigEndian.PutUint16(h[offsetFlags:][:2], f.Flags)
    45  	binary.BigEndian.PutUint16(h[offsetProtocol:][:2], uint16(f.Protocol))
    46  }
    47  
    48  // Flags returns the flag field in h.
    49  func (h PacketInfoHeader) Flags() uint16 {
    50  	return binary.BigEndian.Uint16(h[offsetFlags:])
    51  }
    52  
    53  // Protocol returns the protocol field in h.
    54  func (h PacketInfoHeader) Protocol() tcpip.NetworkProtocolNumber {
    55  	return tcpip.NetworkProtocolNumber(binary.BigEndian.Uint16(h[offsetProtocol:]))
    56  }