Skip to content

Commit

Permalink
Make LocalIP public function so Podman can use it
Browse files Browse the repository at this point in the history
[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Nov 22, 2021
1 parent 78a1d08 commit 1d74137
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
18 changes: 1 addition & 17 deletions run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,23 +719,7 @@ func (b *Builder) generateHosts(rdir, hostname string, addHosts []string, chownO
hosts.Write([]byte(fmt.Sprintf("127.0.0.1 %s %s\n", b.Container, hostname)))
hosts.Write([]byte(fmt.Sprintf("::1 %s %s\n", b.Container, hostname)))

// getLocalIP returns the non loopback local IP of the host
getLocalIP := func() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
if ip := getLocalIP(); ip != "" {
if ip := util.LocalIP(); ip != "" {
hosts.Write([]byte(fmt.Sprintf("%s %s\n", ip, "host.containers.internal")))
}

Expand Down
18 changes: 18 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"fmt"
"io"
"net"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -486,3 +487,20 @@ func VerifyTagName(imageSpec string) (types.ImageReference, error) {
}
return ref, nil
}

// LocalIP returns the non loopback local IP of the host
func LocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}

0 comments on commit 1d74137

Please sign in to comment.