github.com/cilium/cilium@v1.16.2/pkg/maps/nat/ipv4.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package nat
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/cilium/cilium/pkg/bpf"
    10  	"github.com/cilium/cilium/pkg/byteorder"
    11  	"github.com/cilium/cilium/pkg/tuple"
    12  	"github.com/cilium/cilium/pkg/types"
    13  )
    14  
    15  // NatEntry4 represents an IPv4 entry in the NAT table.
    16  type NatEntry4 struct {
    17  	Created uint64     `align:"created"`
    18  	NeedsCT uint64     `align:"needs_ct"`
    19  	Pad1    uint64     `align:"pad1"`
    20  	Pad2    uint64     `align:"pad2"`
    21  	Addr    types.IPv4 `align:"to_saddr"`
    22  	Port    uint16     `align:"to_sport"`
    23  	_       uint16
    24  }
    25  
    26  // String returns the readable format.
    27  func (n *NatEntry4) String() string {
    28  	return fmt.Sprintf("Addr=%s Port=%d Created=%d NeedsCT=%d\n",
    29  		n.Addr,
    30  		n.Port,
    31  		n.Created,
    32  		n.NeedsCT)
    33  }
    34  
    35  // Dump dumps NAT entry to string.
    36  func (n *NatEntry4) Dump(key NatKey, toDeltaSecs func(uint64) string) string {
    37  	var which string
    38  
    39  	if key.GetFlags()&tuple.TUPLE_F_IN != 0 {
    40  		which = "DST"
    41  	} else {
    42  		which = "SRC"
    43  	}
    44  	return fmt.Sprintf("XLATE_%s %s:%d Created=%s NeedsCT=%d\n",
    45  		which,
    46  		n.Addr,
    47  		n.Port,
    48  		toDeltaSecs(n.Created),
    49  		n.NeedsCT)
    50  }
    51  
    52  // ToHost converts NatEntry4 ports to host byte order.
    53  func (n *NatEntry4) ToHost() NatEntry {
    54  	x := *n
    55  	x.Port = byteorder.NetworkToHost16(n.Port)
    56  	return &x
    57  }
    58  
    59  func (n *NatEntry4) New() bpf.MapValue { return &NatEntry4{} }