mapper_linux.go 1.5 KB

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