From a272eba74042b2a48b48d05275dcefeb07e0ee37 Mon Sep 17 00:00:00 2001 From: Rolf Neugebauer Date: Wed, 8 Nov 2017 15:18:20 +0000 Subject: [PATCH] tool: Make ports uint16 The type of the guest and host ports should be uint16 not int. Also make them public member of the PublishPort structure. Signed-off-by: Rolf Neugebauer --- src/cmd/linuxkit/run_qemu.go | 8 ++++---- src/cmd/linuxkit/util.go | 23 ++++++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/cmd/linuxkit/run_qemu.go b/src/cmd/linuxkit/run_qemu.go index e1a6cd4ab..86b6ff119 100644 --- a/src/cmd/linuxkit/run_qemu.go +++ b/src/cmd/linuxkit/run_qemu.go @@ -640,13 +640,13 @@ func buildQemuForwardings(publishFlags multipleFlag, containerized bool) (string return "", err } - hostPort := p.host - guestPort := p.guest + hostPort := p.Host + guestPort := p.Guest if containerized { hostPort = guestPort } - forwardings = fmt.Sprintf("%s,hostfwd=%s::%d-:%d", forwardings, p.protocol, hostPort, guestPort) + forwardings = fmt.Sprintf("%s,hostfwd=%s::%d-:%d", forwardings, p.Protocol, hostPort, guestPort) } return forwardings, nil @@ -659,7 +659,7 @@ func buildDockerForwardings(publishedPorts []string) ([]string, error) { if err != nil { return nil, err } - pmap = append(pmap, "-p", fmt.Sprintf("%d:%d/%s", s.host, s.guest, s.protocol)) + pmap = append(pmap, "-p", fmt.Sprintf("%d:%d/%s", s.Host, s.Guest, s.Protocol)) } return pmap, nil } diff --git a/src/cmd/linuxkit/util.go b/src/cmd/linuxkit/util.go index 1f02fa622..293bc95e2 100644 --- a/src/cmd/linuxkit/util.go +++ b/src/cmd/linuxkit/util.go @@ -194,9 +194,9 @@ func (l *Disks) Set(value string) error { // PublishedPort is used by some backends to expose a VMs port on the host type PublishedPort struct { - guest int - host int - protocol string + Guest uint16 + Host uint16 + Protocol string } // NewPublishedPort parses a string of the form :[/] and returns a PublishedPort structure @@ -208,10 +208,9 @@ func NewPublishedPort(publish string) (PublishedPort, error) { return p, fmt.Errorf("Unable to parse the ports to be published, should be in format : or :/") } - hostPort, err := strconv.Atoi(slice[0]) - + hostPort, err := strconv.ParseUint(slice[0], 10, 16) if err != nil { - return p, fmt.Errorf("The provided hostPort can't be converted to int") + return p, fmt.Errorf("The provided hostPort can't be converted to uint16") } right := strings.Split(slice[1], "/") @@ -220,26 +219,24 @@ func NewPublishedPort(publish string) (PublishedPort, error) { if len(right) == 2 { protocol = strings.TrimSpace(strings.ToLower(right[1])) } - if protocol != "tcp" && protocol != "udp" { return p, fmt.Errorf("Provided protocol is not valid, valid options are: udp and tcp") } - guestPort, err := strconv.Atoi(right[0]) + guestPort, err := strconv.ParseUint(right[0], 10, 16) if err != nil { - return p, fmt.Errorf("The provided guestPort can't be converted to int") + return p, fmt.Errorf("The provided guestPort can't be converted to uint16") } if hostPort < 1 || hostPort > 65535 { return p, fmt.Errorf("Invalid hostPort: %d", hostPort) } - if guestPort < 1 || guestPort > 65535 { return p, fmt.Errorf("Invalid guestPort: %d", guestPort) } - p.guest = guestPort - p.host = hostPort - p.protocol = protocol + p.Guest = uint16(guestPort) + p.Host = uint16(hostPort) + p.Protocol = protocol return p, nil }