mirror of
https://github.com/moby/moby.git
synced 2026-06-24 08:48:23 +00:00
api/types/network: fix handling of unmapped ports (ephemeral ports)
commit 4c24542e95 changed `PortRange.All()`
to omit zero values for ports, but this caused a regression; the zero-value
is used in some places to assign an ephemeral port, e.g.: `--port 80` is an
implicit `--port 0:80`, or `--port <ephemeral port>:80`, where the daemon
picks a random port number from the ephemeral port range as host-port.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -328,9 +328,11 @@ func (pr PortRange) Range() PortRange {
|
||||
// }
|
||||
func (pr PortRange) All() iter.Seq[Port] {
|
||||
return func(yield func(Port) bool) {
|
||||
if pr.proto == protoZero {
|
||||
return
|
||||
}
|
||||
// Do not skip zero values here, because a zero-value means
|
||||
// "map the port to an ephemeral host port".
|
||||
//
|
||||
// For example, "--port 80" is shorthand for "--port 0:80"
|
||||
// ("--port <ephemeral port>:80").
|
||||
for i := uint32(pr.Start()); i <= uint32(pr.End()); i++ {
|
||||
if !yield(Port{num: uint16(i), proto: pr.proto}) {
|
||||
return
|
||||
|
||||
@@ -278,7 +278,8 @@ func TestPortRange(t *testing.T) {
|
||||
assert.Equal(t, pr.End(), uint16(0))
|
||||
assert.Equal(t, pr.Proto(), network.IPProtocol(""))
|
||||
assert.Equal(t, pr.Range(), pr)
|
||||
assert.Check(t, slices.Equal(slices.Collect(pr.All()), []network.Port{}))
|
||||
var ephemeralPort network.Port
|
||||
assert.Check(t, slices.Equal(slices.Collect(pr.All()), []network.Port{ephemeralPort}))
|
||||
|
||||
t.Run("Marshal Unmarshal", func(t *testing.T) {
|
||||
var pr network.PortRange
|
||||
|
||||
8
vendor/github.com/moby/moby/api/types/network/port.go
generated
vendored
8
vendor/github.com/moby/moby/api/types/network/port.go
generated
vendored
@@ -328,9 +328,11 @@ func (pr PortRange) Range() PortRange {
|
||||
// }
|
||||
func (pr PortRange) All() iter.Seq[Port] {
|
||||
return func(yield func(Port) bool) {
|
||||
if pr.proto == protoZero {
|
||||
return
|
||||
}
|
||||
// Do not skip zero values here, because a zero-value means
|
||||
// "map the port to an ephemeral host port".
|
||||
//
|
||||
// For example, "--port 80" is shorthand for "--port 0:80"
|
||||
// ("--port <ephemeral port>:80").
|
||||
for i := uint32(pr.Start()); i <= uint32(pr.End()); i++ {
|
||||
if !yield(Port{num: uint16(i), proto: pr.proto}) {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user