Files
linuxkit/alpine/packages/proxy/proxy.go
David Scott 969760395d proxy: map TCP ports at 0x10000-0x20000, and UDP at 0x20000-
The proxy process command-line arguments assume we're exposing TCP
or UDP ports on Moby's public IPs. Instead we're forwarding over vsock
where we must map the Moby ports onto vsock ports. Normally TCP and
UDP ports are different, but with vsock there is only one space of
port numbers so we have to map them into different ranges.

This patch maps Moby ports as follows:

- TCP port x onto vsock port 0x10000 + x
- UDP port x onto vsock port 0x20000 + x

Signed-off-by: David Scott <dave.scott@docker.com>
2016-04-21 14:45:55 +01:00

78 lines
1.9 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"proxy/libproxy"
)
// sendError signals the error to the parent and quits the process.
func sendError(err error) {
f := os.NewFile(3, "signal-parent")
fmt.Fprintf(f, "1\n%s", err)
f.Close()
os.Exit(1)
}
// sendOK signals the parent that the forward is running.
func sendOK() {
f := os.NewFile(3, "signal-parent")
fmt.Fprint(f, "0\n")
f.Close()
}
// Map dynamic TCP ports onto vsock ports over this offset
var vSockTCPPortOffset = 0x10000
// Map dynamic UDP ports onto vsock ports over this offset
var vSockUDPPortOffset = 0x20000
// From docker/libnetwork/portmapper/proxy.go:
// parseHostContainerAddrs parses the flags passed on reexec to create the TCP or UDP
// net.Addrs to map the host and container ports
func parseHostContainerAddrs() (host net.Addr, port int, container net.Addr) {
var (
proto = flag.String("proto", "tcp", "proxy protocol")
hostIP = flag.String("host-ip", "", "host ip")
hostPort = flag.Int("host-port", -1, "host port")
containerIP = flag.String("container-ip", "", "container ip")
containerPort = flag.Int("container-port", -1, "container port")
)
flag.Parse()
switch *proto {
case "tcp":
host = &net.TCPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
port = vSockTCPPortOffset + *hostPort
container = &net.TCPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
case "udp":
host = &net.UDPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
port = vSockUDPPortOffset + *hostPort
container = &net.UDPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
default:
log.Fatalf("unsupported protocol %s", *proto)
}
return host, port, container
}
func handleStopSignals(p libproxy.Proxy) {
s := make(chan os.Signal, 10)
signal.Notify(s, os.Interrupt, syscall.SIGTERM, syscall.SIGSTOP)
for range s {
os.Exit(0)
// The vsock proxy cannot be shutdown the same way as the TCP one:
//p.Close()
}
}