github.com/polevpn/netstack@v1.10.9/tcpip/header/udp.go (about)

     1  // Copyright 2018 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 header
    16  
    17  import (
    18  	"encoding/binary"
    19  
    20  	"github.com/polevpn/netstack/tcpip"
    21  )
    22  
    23  const (
    24  	udpSrcPort  = 0
    25  	udpDstPort  = 2
    26  	udpLength   = 4
    27  	udpChecksum = 6
    28  )
    29  
    30  const (
    31  	// UDPMaximumPacketSize is the largest possible UDP packet.
    32  	UDPMaximumPacketSize = 0xffff
    33  )
    34  
    35  // UDPFields contains the fields of a UDP packet. It is used to describe the
    36  // fields of a packet that needs to be encoded.
    37  type UDPFields struct {
    38  	// SrcPort is the "source port" field of a UDP packet.
    39  	SrcPort uint16
    40  
    41  	// DstPort is the "destination port" field of a UDP packet.
    42  	DstPort uint16
    43  
    44  	// Length is the "length" field of a UDP packet.
    45  	Length uint16
    46  
    47  	// Checksum is the "checksum" field of a UDP packet.
    48  	Checksum uint16
    49  }
    50  
    51  // UDP represents a UDP header stored in a byte array.
    52  type UDP []byte
    53  
    54  const (
    55  	// UDPMinimumSize is the minimum size of a valid UDP packet.
    56  	UDPMinimumSize = 8
    57  
    58  	// UDPProtocolNumber is UDP's transport protocol number.
    59  	UDPProtocolNumber tcpip.TransportProtocolNumber = 17
    60  )
    61  
    62  // SourcePort returns the "source port" field of the udp header.
    63  func (b UDP) SourcePort() uint16 {
    64  	return binary.BigEndian.Uint16(b[udpSrcPort:])
    65  }
    66  
    67  // DestinationPort returns the "destination port" field of the udp header.
    68  func (b UDP) DestinationPort() uint16 {
    69  	return binary.BigEndian.Uint16(b[udpDstPort:])
    70  }
    71  
    72  // Length returns the "length" field of the udp header.
    73  func (b UDP) Length() uint16 {
    74  	return binary.BigEndian.Uint16(b[udpLength:])
    75  }
    76  
    77  // Payload returns the data contained in the UDP datagram.
    78  func (b UDP) Payload() []byte {
    79  	return b[UDPMinimumSize:]
    80  }
    81  
    82  // Checksum returns the "checksum" field of the udp header.
    83  func (b UDP) Checksum() uint16 {
    84  	return binary.BigEndian.Uint16(b[udpChecksum:])
    85  }
    86  
    87  // SetSourcePort sets the "source port" field of the udp header.
    88  func (b UDP) SetSourcePort(port uint16) {
    89  	binary.BigEndian.PutUint16(b[udpSrcPort:], port)
    90  }
    91  
    92  // SetDestinationPort sets the "destination port" field of the udp header.
    93  func (b UDP) SetDestinationPort(port uint16) {
    94  	binary.BigEndian.PutUint16(b[udpDstPort:], port)
    95  }
    96  
    97  // SetChecksum sets the "checksum" field of the udp header.
    98  func (b UDP) SetChecksum(checksum uint16) {
    99  	binary.BigEndian.PutUint16(b[udpChecksum:], checksum)
   100  }
   101  
   102  // CalculateChecksum calculates the checksum of the udp packet, given the
   103  // checksum of the network-layer pseudo-header and the checksum of the payload.
   104  func (b UDP) CalculateChecksum(partialChecksum uint16) uint16 {
   105  	// Calculate the rest of the checksum.
   106  	return Checksum(b[:UDPMinimumSize], partialChecksum)
   107  }
   108  
   109  // Encode encodes all the fields of the udp header.
   110  func (b UDP) Encode(u *UDPFields) {
   111  	binary.BigEndian.PutUint16(b[udpSrcPort:], u.SrcPort)
   112  	binary.BigEndian.PutUint16(b[udpDstPort:], u.DstPort)
   113  	binary.BigEndian.PutUint16(b[udpLength:], u.Length)
   114  	binary.BigEndian.PutUint16(b[udpChecksum:], u.Checksum)
   115  }