Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add IPv6 support #34

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/google/uuid v1.3.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.8.3
github.com/urfave/cli v1.22.5
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -21,6 +22,7 @@ require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -38,6 +40,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
Expand Down
79 changes: 49 additions & 30 deletions internal/context/ausf_context_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package context

import (
"fmt"
"net/netip"
"os"
"strconv"

"github.com/google/uuid"

"github.com/free5gc/ausf/internal/logger"
ausf_utils "github.com/free5gc/ausf/internal/utils"
"github.com/free5gc/ausf/pkg/factory"
"github.com/free5gc/openapi/models"
)
Expand All @@ -23,36 +24,47 @@ func InitAusfContext(context *AUSFContext) {
context.GroupID = configuration.GroupId
context.NrfUri = configuration.NrfUri
context.NrfCertPem = configuration.NrfCertPem
context.UriScheme = models.UriScheme(configuration.Sbi.Scheme) // default uri scheme
context.RegisterIPv4 = factory.AusfSbiDefaultIPv4 // default localhost
context.SBIPort = factory.AusfSbiDefaultPort // default port
if sbi != nil {
if sbi.RegisterIPv4 != "" {
context.RegisterIPv4 = sbi.RegisterIPv4
}
if sbi.Port != 0 {
context.SBIPort = sbi.Port
}

if sbi.Scheme == "https" {
context.UriScheme = models.UriScheme_HTTPS
} else {
context.UriScheme = models.UriScheme_HTTP
}

context.BindingIPv4 = os.Getenv(sbi.BindingIPv4)
if context.BindingIPv4 != "" {
logger.InitLog.Info("Parsing ServerIPv4 address from ENV Variable.")
} else {
context.BindingIPv4 = sbi.BindingIPv4
if context.BindingIPv4 == "" {
logger.InitLog.Warn("Error parsing ServerIPv4 address as string. Using the 0.0.0.0 address as default.")
context.BindingIPv4 = "0.0.0.0"
}
}

if sbi.RegisterIP != "" {
context.RegisterIP = sbi.RegisterIP
} else if sbi.RegisterIPv4 != "" {
context.RegisterIP = sbi.RegisterIPv4
} else {
context.RegisterIP = factory.AusfSbiDefaultIPv4 // default uri scheme
}

if sbi.Port != 0 {
context.SBIPort = sbi.Port
} else {
context.SBIPort = factory.AusfSbiDefaultPort // default port
}

context.Url = string(context.UriScheme) + "://" + context.RegisterIPv4 + ":" + strconv.Itoa(context.SBIPort)
if sbi.Scheme == "https" {
context.UriScheme = models.UriScheme_HTTPS
} else {
context.UriScheme = models.UriScheme_HTTP
}

if bindingIP := os.Getenv(sbi.BindingIP); bindingIP != "" {
context.BindingIP = bindingIP
logger.InitLog.Info("Parsing ServerIP address from ENV Variable.")
} else if bindingIP := sbi.BindingIP; bindingIP != "" {
context.BindingIP = bindingIP
} else if bindingIPv4 := os.Getenv(sbi.BindingIPv4); bindingIPv4 != "" {
context.BindingIP = bindingIPv4
logger.InitLog.Info("Parsing ServerIPv4 address from ENV Variable.")
} else if bindingIPv4 := sbi.BindingIPv4; bindingIPv4 != "" {
context.BindingIP = bindingIPv4
} else {
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)

context.Url = string(context.UriScheme) + "://" + netip.AddrPortFrom(sbiRegisterIp, sbiPort).String()
context.PlmnList = append(context.PlmnList, configuration.PlmnSupportList...)

// context.NfService
Expand All @@ -74,8 +86,15 @@ func AddNfServices(serviceMap *map[models.ServiceName]models.NfService, config *
nfService.ServiceName = models.ServiceName_NAUSF_AUTH

var ipEndPoint models.IpEndPoint
ipEndPoint.Ipv4Address = context.RegisterIPv4
ipEndPoint.Port = int32(context.SBIPort)

registerAddr := ausf_utils.RegisterAddr(context.RegisterIP)
if registerAddr.Is6() {
ipEndPoint.Ipv6Address = context.RegisterIP
} else if registerAddr.Is4() {
ipEndPoint.Ipv4Address = context.RegisterIP
}

ipEndPoints = append(ipEndPoints, ipEndPoint)

var nfServiceVersion models.NfServiceVersion
Expand Down
4 changes: 2 additions & 2 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type AUSFContext struct {
NfId string
GroupID string
SBIPort int
RegisterIPv4 string
BindingIPv4 string
RegisterIP string
BindingIP string
Url string
UriScheme models.UriScheme
NrfUri string
Expand Down
28 changes: 20 additions & 8 deletions internal/sbi/consumer/nrf_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"net/netip"
"strings"
"sync"
"time"
Expand All @@ -14,6 +14,7 @@ import (

ausf_context "github.com/free5gc/ausf/internal/context"
"github.com/free5gc/ausf/internal/logger"
ausf_utils "github.com/free5gc/ausf/internal/utils"
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/Nnrf_NFDiscovery"
"github.com/free5gc/openapi/Nnrf_NFManagement"
Expand Down Expand Up @@ -200,7 +201,13 @@ func (s *nnrfService) buildNfProfile(ausfContext *ausf_context.AUSFContext) (pro
profile.NfInstanceId = ausfContext.NfId
profile.NfType = models.NfType_AUSF
profile.NfStatus = models.NfStatus_REGISTERED
profile.Ipv4Addresses = append(profile.Ipv4Addresses, ausfContext.RegisterIPv4)

registerAddr := ausf_utils.RegisterAddr(ausfContext.RegisterIP)
if registerAddr.Is6() {
profile.Ipv6Addresses = append(profile.Ipv6Addresses, ausfContext.RegisterIP)
} else if registerAddr.Is4() {
profile.Ipv4Addresses = append(profile.Ipv4Addresses, ausfContext.RegisterIP)
}
services := []models.NfService{}
for _, nfService := range ausfContext.NfService {
services = append(services, nfService)
Expand All @@ -220,7 +227,7 @@ func (s *nnrfService) buildNfProfile(ausfContext *ausf_context.AUSFContext) (pro
// },
// },
}
return
return profile, nil
}

func (s *nnrfService) GetUdmUrl(nrfUri string) string {
Expand All @@ -235,13 +242,18 @@ func (s *nnrfService) GetUdmUrl(nrfUri string) string {
nfDiscoverParam,
)
if err != nil {
logger.ConsumerLog.Errorln("[Search UDM UEAU] ", err.Error(), "use defalt udmUrl", udmUrl)
logger.ConsumerLog.Errorln("[Search UDM UEAU] ", err.Error(), "use default udmUrl", udmUrl)
} else if len(res.NfInstances) > 0 {
udmInstance := res.NfInstances[0]
if len(udmInstance.Ipv4Addresses) > 0 && udmInstance.NfServices != nil {
ueauService := (*udmInstance.NfServices)[0]
ueauEndPoint := (*ueauService.IpEndPoints)[0]
udmUrl = string(ueauService.Scheme) + "://" + ueauEndPoint.Ipv4Address + ":" + strconv.Itoa(int(ueauEndPoint.Port))
ueauService := (*udmInstance.NfServices)[0]
ueauEndPoint := (*ueauService.IpEndPoints)[0]
port := uint16(ueauEndPoint.Port)
if len(udmInstance.Ipv6Addresses) > 0 && udmInstance.NfServices != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UDM should register with IPv6 for this code to work.
I think this change can be done in another PR or wait for UDM to have a PR ready.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That a work in progress, I begin to work on the IPv6 support for UDM adjivas/udm#1
I will open this PR to the Free5gc's UDM later

registerIp := ausf_utils.RegisterAddr(ueauEndPoint.Ipv6Address)
udmUrl = string(ueauService.Scheme) + "://" + netip.AddrPortFrom(registerIp, port).String()
} else if len(udmInstance.Ipv4Addresses) > 0 && udmInstance.NfServices != nil {
registerIp := ausf_utils.RegisterAddr(ueauEndPoint.Ipv4Address)
udmUrl = string(ueauService.Scheme) + "://" + netip.AddrPortFrom(registerIp, port).String()
}
} else {
logger.ConsumerLog.Errorln("[Search UDM UEAU] len(NfInstances) = 0")
Expand Down
28 changes: 28 additions & 0 deletions internal/utils/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net

import (
"net"
"net/netip"

"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 {
logger.InitLog.Errorf("Resolve RegisterIP hostname %s failed: %+v", registerIP, err)
}
ip, err := netip.ParseAddr(ips[0].String())
if err != nil {
logger.InitLog.Errorf("Parse RegisterIP hostname %s failed: %+v", registerIP, err)
}
return ip
}
66 changes: 66 additions & 0 deletions internal/utils/net_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package net

import (
"net/netip"
"testing"

"github.com/stretchr/testify/assert"
)

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) {
expectedAddr, err := netip.ParseAddr("::1")
if err != nil {
t.Errorf("invalid expected IP: %+v", expectedAddr)
}

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

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

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

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

addr := RegisterAddr("2001:db8::1:0:0:1")
if addr != expectedAddr {
t.Errorf("invalid IP: %+v", addr)
}
}
Loading
Loading