Skip to content

Commit

Permalink
fix: lookUp for BindingIP
Browse files Browse the repository at this point in the history
  • Loading branch information
adjivas committed Feb 25, 2025
1 parent ca85ce3 commit 0f4a4fb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions internal/context/ausf_context_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func InitAusfContext(context *AUSFContext) {
logger.InitLog.Warn("Error parsing ServerIPv4 address as string. Using the 0.0.0.0 address as default.")
context.BindingIP = "0.0.0.0"
}
context.BindingIP = ausf_utils.BindingLookup(context.BindingIP)

sbiRegisterIp := ausf_utils.RegisterAddr(context.RegisterIP)
sbiPort := uint16(context.SBIPort)
Expand Down
8 changes: 8 additions & 0 deletions internal/utils/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (
"github.com/free5gc/ausf/internal/logger"
)

func BindingLookup(bindIP string) string {
ips, err := net.LookupIP(bindIP)
if err != nil {
logger.InitLog.Errorf("Resolve BindingIP hostname %s failed: %+v", bindIP, err)
}
return ips[0].String()
}

func RegisterAddr(registerIP string) netip.Addr {
ips, err := net.LookupIP(registerIP)
if err != nil {
Expand Down
41 changes: 31 additions & 10 deletions internal/utils/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,60 @@ import (
"testing"
)

func TestBindingLookupWithLocalhost(t *testing.T) {
expectedBindIP := "::1"

bindIP := BindingLookup("localhost")
assert.Equal(t, bindIP, expectedBindIP)
}

func TestBindingLookupWithIpV4(t *testing.T) {
expectedBindIP := "127.0.0.1"

bindIP := BindingLookup("127.0.0.1")
assert.Equal(t, bindIP, expectedBindIP)
}

func TestBindingLookupWithIpV6(t *testing.T) {
expectedBindIP := "2001:db8::1:0:0:1"

bindIP := BindingLookup("2001:db8::1:0:0:1")
assert.Equal(t, bindIP, expectedBindIP)
}

func TestRegisterAddrWithLocalhost(t *testing.T) {
expected_addr, err := netip.ParseAddr("::1")
expectedAddr, err := netip.ParseAddr("::1")
if err != nil {
t.Errorf("invalid expected IP: %+v", expected_addr)
t.Errorf("invalid expected IP: %+v", expectedAddr)
}

addr := RegisterAddr("localhost")
if addr != expected_addr {
if addr != expectedAddr {
t.Errorf("invalid IP: %+v", addr)
}
assert.Equal(t, addr, expected_addr)
assert.Equal(t, addr, expectedAddr)
}

func TestRegisterAddrWithIpV4(t *testing.T) {
expected_addr, err := netip.ParseAddr("127.0.0.1")
expectedAddr, err := netip.ParseAddr("127.0.0.1")
if err != nil {
t.Errorf("invalid expected IP: %+v", expected_addr)
t.Errorf("invalid expected IP: %+v", expectedAddr)
}

addr := RegisterAddr("127.0.0.1")
if addr != expected_addr {
if addr != expectedAddr {
t.Errorf("invalid IP: %+v", addr)
}
}

func TestRegisterAddrWithIpV6(t *testing.T) {
expected_addr, err := netip.ParseAddr("2001:db8::1:0:0:1")
expectedAddr, err := netip.ParseAddr("2001:db8::1:0:0:1")
if err != nil {
t.Errorf("invalid expected IP: %+v", expected_addr)
t.Errorf("invalid expected IP: %+v", expectedAddr)
}

addr := RegisterAddr("2001:db8::1:0:0:1")
if addr != expected_addr {
if addr != expectedAddr {
t.Errorf("invalid IP: %+v", addr)
}
}
3 changes: 2 additions & 1 deletion pkg/factory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/asaskevich/govalidator"

"github.com/free5gc/ausf/internal/logger"
ausf_utils "github.com/free5gc/ausf/internal/utils"
"github.com/free5gc/openapi/models"
)

Expand Down Expand Up @@ -284,7 +285,7 @@ func (c *Config) GetSbiBindingIP() string {
bindIP = c.Configuration.Sbi.BindingIPv4
}
}
return bindIP
return ausf_utils.BindingLookup(bindIP)
}

func (c *Config) GetSbiPort() int {
Expand Down

0 comments on commit 0f4a4fb

Please sign in to comment.