github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/portmapper/mapper_linux.go (about)

     1  package portmapper
     2  
     3  import (
     4  	"net"
     5  	"sync"
     6  
     7  	"github.com/docker/docker/libnetwork/iptables"
     8  	"github.com/docker/docker/libnetwork/portallocator"
     9  )
    10  
    11  // PortMapper manages the network address translation
    12  type PortMapper struct {
    13  	bridgeName string
    14  
    15  	// udp:ip:port
    16  	currentMappings map[string]*mapping
    17  	lock            sync.Mutex
    18  
    19  	proxyPath string
    20  
    21  	Allocator *portallocator.PortAllocator
    22  	chain     *iptables.ChainInfo
    23  }
    24  
    25  // SetIptablesChain sets the specified chain into portmapper
    26  func (pm *PortMapper) SetIptablesChain(c *iptables.ChainInfo, bridgeName string) {
    27  	pm.chain = c
    28  	pm.bridgeName = bridgeName
    29  }
    30  
    31  // AppendForwardingTableEntry adds a port mapping to the forwarding table
    32  func (pm *PortMapper) AppendForwardingTableEntry(proto string, sourceIP net.IP, sourcePort int, containerIP string, containerPort int) error {
    33  	return pm.forward(iptables.Append, proto, sourceIP, sourcePort, containerIP, containerPort)
    34  }
    35  
    36  // DeleteForwardingTableEntry removes a port mapping from the forwarding table
    37  func (pm *PortMapper) DeleteForwardingTableEntry(proto string, sourceIP net.IP, sourcePort int, containerIP string, containerPort int) error {
    38  	return pm.forward(iptables.Delete, proto, sourceIP, sourcePort, containerIP, containerPort)
    39  }
    40  
    41  func (pm *PortMapper) forward(action iptables.Action, proto string, sourceIP net.IP, sourcePort int, containerIP string, containerPort int) error {
    42  	if pm.chain == nil {
    43  		return nil
    44  	}
    45  	return pm.chain.Forward(action, sourceIP, sourcePort, proto, containerIP, containerPort, pm.bridgeName)
    46  }