Skip to content

Commit

Permalink
Merge pull request #7234 from derekmarcotte/dm-freebsd
Browse files Browse the repository at this point in the history
Fix undefined: getEphemeralPortRange error on FreeBSD.
  • Loading branch information
Mahmood Ali authored Jul 24, 2020
2 parents 87e38dd + 7d61855 commit f4eae8a
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions helper/freeport/ephemeral_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//+build freebsd

package freeport

import (
"fmt"
"os/exec"
"regexp"
"strconv"
)

/*
$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535
*/

const (
ephPortFirst = "net.inet.ip.portrange.first"
ephPortLast = "net.inet.ip.portrange.last"
command = "sysctl"
)

var ephPortRe = regexp.MustCompile(`^\s*(\d+)\s+(\d+)\s*$`)

func getEphemeralPortRange() (int, int, error) {
cmd := exec.Command(command, "-n", ephPortFirst, ephPortLast)
out, err := cmd.Output()
if err != nil {
return 0, 0, err
}

val := string(out)

m := ephPortRe.FindStringSubmatch(val)
if m != nil {
min, err1 := strconv.Atoi(m[1])
max, err2 := strconv.Atoi(m[2])

if err1 == nil && err2 == nil {
return min, max, nil
}
}

return 0, 0, fmt.Errorf("unexpected sysctl value %q for keys %q %q", val, ephPortFirst, ephPortLast)
}

0 comments on commit f4eae8a

Please sign in to comment.