Skip to content

Commit

Permalink
【bug-fix】:get local ipv4 addr (#410)
Browse files Browse the repository at this point in the history
【bug-fix】:get local ipv4 addr (#410)
  • Loading branch information
scf0220 authored Oct 22, 2019
1 parent bb480eb commit d21a015
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"encoding/gob"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"math/big"
"math/bits"
"net"
Expand Down Expand Up @@ -63,13 +64,15 @@ func GetIPV4Addr() (string, error) {
}

for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
ones, bits := ipnet.Mask.Size()
if bits == 32 && ones == 24 {
return ipnet.IP.To4().String(), nil
ipNet, isIpNet := addr.(*net.IPNet)
if isIpNet && !ipNet.IP.IsLoopback() {
ipv4 := ipNet.IP.To4()
if ipv4 != nil {
return ipv4.String(), nil
}
}
}
log.Error("ipv4 addr not found", "addr", addrs)
return "127.0.0.1", nil
}

Expand Down

0 comments on commit d21a015

Please sign in to comment.