From be5a7e1ded658823e7d6d285ec53f7040a0dba87 Mon Sep 17 00:00:00 2001 From: Sukchan Lee Date: Tue, 31 Dec 2024 21:49:36 +0900 Subject: [PATCH] [SBI] Added support for custom port numbers in SBI configuration with FQDN.(#3385) This update allows the parsing and handling of user-defined port numbers in the `advertise` field or explicitly in the `server` configuration for SBI. Users can now specify non-default ports for both binding and advertising while maintaining compatibility with existing configurations. The feature includes logic to handle FQDNs with embedded port numbers (e.g., `example.com:8080`) and ensures proper memory management during parsing. Updated the client association logic to utilize custom ports when specified. Examples: - Bind to the address on the eth0 and advertise as open5gs-amf.svc.local ``` sbi: server: - dev:eth0 advertise: open5gs-amf.svc.local ``` - Specify a custom port number 7777 while binding to the given address ``` sbi: server: - address: amf.localdomain port: 7777 ``` - Bind to 127.0.0.5 and advertise as open5gs-amf.svc.local ``` sbi: server: - address: 127.0.0.5 port: 7777 advertise: open5gs-amf.svc.local ``` - Bind to port 7777 but advertise with a different port number 8888 ``` sbi: server: - address: 127.0.0.5 port: 7777 advertise: open5gs-amf.svc.local:8888 ``` --- configs/open5gs/amf.yaml.in | 23 +++++++++++--- configs/open5gs/ausf.yaml.in | 25 +++++++++++---- configs/open5gs/bsf.yaml.in | 25 +++++++++++---- configs/open5gs/nrf.yaml.in | 25 +++++++++++---- configs/open5gs/nssf.yaml.in | 25 +++++++++++---- configs/open5gs/pcf.yaml.in | 25 +++++++++++---- configs/open5gs/scp.yaml.in | 25 +++++++++++---- configs/open5gs/smf.yaml.in | 25 +++++++++++---- configs/open5gs/udm.yaml.in | 25 +++++++++++---- configs/open5gs/udr.yaml.in | 25 +++++++++++---- lib/sbi/context.c | 59 +++++++++++++++++++++++++++++------- 11 files changed, 237 insertions(+), 70 deletions(-) diff --git a/configs/open5gs/amf.yaml.in b/configs/open5gs/amf.yaml.in index 0970736cd..3b3123f1c 100644 --- a/configs/open5gs/amf.yaml.in +++ b/configs/open5gs/amf.yaml.in @@ -59,18 +59,31 @@ amf: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-amf.svc.local # sbi: # server: # - dev:eth0 # advertise: open5gs-amf.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: amf.localdomain +# port: 7777 +# +# o Bind to 127.0.0.5 and advertise as open5gs-amf.svc.local +# sbi: +# server: +# - address: 127.0.0.5 +# port: 7777 +# advertise: open5gs-amf.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.5 +# port: 7777 +# advertise: open5gs-amf.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/ausf.yaml.in b/configs/open5gs/ausf.yaml.in index e20b89b5b..58a8f77a8 100644 --- a/configs/open5gs/ausf.yaml.in +++ b/configs/open5gs/ausf.yaml.in @@ -22,18 +22,31 @@ ausf: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-ausf.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-ausf.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: ausf.localdomain +# port: 7777 +# +# o Bind to 127.0.0.11 and advertise as open5gs-ausf.svc.local +# sbi: +# server: +# - address: 127.0.0.11 +# port: 7777 +# advertise: open5gs-ausf.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.11 +# port: 7777 +# advertise: open5gs-ausf.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/bsf.yaml.in b/configs/open5gs/bsf.yaml.in index 88d166770..fa989be40 100644 --- a/configs/open5gs/bsf.yaml.in +++ b/configs/open5gs/bsf.yaml.in @@ -22,18 +22,31 @@ bsf: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-bsf.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-bsf.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: bsf.localdomain +# port: 7777 +# +# o Bind to 127.0.0.15 and advertise as open5gs-bsf.svc.local +# sbi: +# server: +# - address: 127.0.0.15 +# port: 7777 +# advertise: open5gs-bsf.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.15 +# port: 7777 +# advertise: open5gs-bsf.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/nrf.yaml.in b/configs/open5gs/nrf.yaml.in index 1a612080c..c69f8c40c 100644 --- a/configs/open5gs/nrf.yaml.in +++ b/configs/open5gs/nrf.yaml.in @@ -21,18 +21,31 @@ nrf: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-nrf.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-nrf.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: nrf.localdomain +# port: 7777 +# +# o Bind to 127.0.0.10 and advertise as open5gs-nrf.svc.local +# sbi: +# server: +# - address: 127.0.0.10 +# port: 7777 +# advertise: open5gs-nrf.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.10 +# port: 7777 +# advertise: open5gs-nrf.svc.local:8888 # ################################################################################ # HTTPS scheme with TLS diff --git a/configs/open5gs/nssf.yaml.in b/configs/open5gs/nssf.yaml.in index 6686bc7a3..86f826a5a 100644 --- a/configs/open5gs/nssf.yaml.in +++ b/configs/open5gs/nssf.yaml.in @@ -25,18 +25,31 @@ nssf: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-nssf.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-nssf.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: nssf.localdomain +# port: 7777 +# +# o Bind to 127.0.0.14 and advertise as open5gs-nssf.svc.local +# sbi: +# server: +# - address: 127.0.0.14 +# port: 7777 +# advertise: open5gs-nssf.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.14 +# port: 7777 +# advertise: open5gs-nssf.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/pcf.yaml.in b/configs/open5gs/pcf.yaml.in index 0bfc6c994..02833669d 100644 --- a/configs/open5gs/pcf.yaml.in +++ b/configs/open5gs/pcf.yaml.in @@ -148,18 +148,31 @@ pcf: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-pcf.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-pcf.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: pcf.localdomain +# port: 7777 +# +# o Bind to 127.0.0.13 and advertise as open5gs-pcf.svc.local +# sbi: +# server: +# - address: 127.0.0.13 +# port: 7777 +# advertise: open5gs-pcf.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.13 +# port: 7777 +# advertise: open5gs-pcf.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/scp.yaml.in b/configs/open5gs/scp.yaml.in index 1a5e384ba..737be86ea 100644 --- a/configs/open5gs/scp.yaml.in +++ b/configs/open5gs/scp.yaml.in @@ -63,18 +63,31 @@ scp: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-scp.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-scp.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: scp.localdomain +# port: 7777 +# +# o Bind to 127.0.0.200 and advertise as open5gs-scp.svc.local +# sbi: +# server: +# - address: 127.0.0.200 +# port: 7777 +# advertise: open5gs-scp.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.200 +# port: 7777 +# advertise: open5gs-scp.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/smf.yaml.in b/configs/open5gs/smf.yaml.in index fc7b4fd5e..5c92d545d 100644 --- a/configs/open5gs/smf.yaml.in +++ b/configs/open5gs/smf.yaml.in @@ -186,18 +186,31 @@ smf: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-smf.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-smf.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: smf.localdomain +# port: 7777 +# +# o Bind to 127.0.0.4 and advertise as open5gs-smf.svc.local +# sbi: +# server: +# - address: 127.0.0.4 +# port: 7777 +# advertise: open5gs-smf.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.4 +# port: 7777 +# advertise: open5gs-smf.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/udm.yaml.in b/configs/open5gs/udm.yaml.in index 9b2ba0853..6b7ec48ff 100644 --- a/configs/open5gs/udm.yaml.in +++ b/configs/open5gs/udm.yaml.in @@ -81,18 +81,31 @@ udm: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-udm.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-udm.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: udm.localdomain +# port: 7777 +# +# o Bind to 127.0.0.12 and advertise as open5gs-udm.svc.local +# sbi: +# server: +# - address: 127.0.0.12 +# port: 7777 +# advertise: open5gs-udm.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.12 +# port: 7777 +# advertise: open5gs-udm.svc.local:8888 # ################################################################################ # SBI Client diff --git a/configs/open5gs/udr.yaml.in b/configs/open5gs/udr.yaml.in index 6308d3ce2..e3c3b82a5 100644 --- a/configs/open5gs/udr.yaml.in +++ b/configs/open5gs/udr.yaml.in @@ -23,18 +23,31 @@ udr: ################################################################################ # SBI Server ################################################################################ -# o Override SBI address to be advertised to NRF +# o Bind to the address on the eth0 and advertise as open5gs-udr.svc.local # sbi: # server: -# - dev: eth0 +# - dev:eth0 # advertise: open5gs-udr.svc.local # +# o Specify a custom port number 7777 while binding to the given address # sbi: # server: -# - address: localhost -# advertise: -# - 127.0.0.99 -# - ::1 +# - address: udr.localdomain +# port: 7777 +# +# o Bind to 127.0.0.20 and advertise as open5gs-udr.svc.local +# sbi: +# server: +# - address: 127.0.0.20 +# port: 7777 +# advertise: open5gs-udr.svc.local +# +# o Bind to port 7777 but advertise with a different port number 8888 +# sbi: +# server: +# - address: 127.0.0.20 +# port: 7777 +# advertise: open5gs-udr.svc.local:8888 # ################################################################################ # SBI Client diff --git a/lib/sbi/context.c b/lib/sbi/context.c index b35ab7f29..f4581bd98 100644 --- a/lib/sbi/context.c +++ b/lib/sbi/context.c @@ -823,9 +823,38 @@ int ogs_sbi_context_parse_server_config( } addr = NULL; + /* ----- Process advertise (Host+Port Parsing Logic) ----- */ for (i = 0; i < num_of_advertise; i++) { - rv = ogs_addaddrinfo(&addr, family, advertise[i], port, 0); + uint16_t adv_port = port; /* Default to server's port */ + char *hostbuf = NULL; + const char *colon_pos = strchr(advertise[i], ':'); + + if (colon_pos) { + /* If a colon exists, split host and port */ + size_t host_len = colon_pos - advertise[i]; + + /* Allocate memory for the host part */ + hostbuf = (char *)ogs_malloc(host_len + 1); + ogs_assert(hostbuf); + + /* Copy the host part into hostbuf */ + memcpy(hostbuf, advertise[i], host_len); + hostbuf[host_len] = '\0'; + + /* Parse the port part */ + adv_port = (uint16_t)atoi(colon_pos + 1); + } else { + /* If no colon, treat the entire string as the host */ + hostbuf = ogs_strdup(advertise[i]); + ogs_assert(hostbuf); + } + + /* Add the parsed address info */ + rv = ogs_addaddrinfo(&addr, family, hostbuf, adv_port, 0); ogs_assert(rv == OGS_OK); + + /* Free the allocated memory */ + ogs_free(hostbuf); } node = ogs_list_first(&list); @@ -1752,11 +1781,8 @@ void ogs_sbi_nf_instance_build_default(ogs_sbi_nf_instance_t *nf_instance) ogs_assert(advertise); /* First FQDN is selected */ - if (!hostname) { + if (!hostname) hostname = ogs_gethostname(advertise); - if (hostname) - continue; - } if (nf_instance->num_of_ipv4 < OGS_SBI_MAX_NUM_OF_IP_ADDRESS) { ogs_sockaddr_t *addr = NULL; @@ -1826,11 +1852,8 @@ ogs_sbi_nf_service_t *ogs_sbi_nf_service_build_default( ogs_assert(advertise); /* First FQDN is selected */ - if (!hostname) { + if (!hostname) hostname = ogs_gethostname(advertise); - if (hostname) - continue; - } if (nf_service->num_of_addr < OGS_SBI_MAX_NUM_OF_IP_ADDRESS) { bool is_port = true; @@ -1943,6 +1966,7 @@ static void nf_service_associate_client(ogs_sbi_nf_service_t *nf_service) { ogs_sbi_client_t *client = NULL; ogs_sockaddr_t *addr = NULL, *addr6 = NULL; + uint16_t port = 0; ogs_assert(nf_service->scheme); @@ -1950,15 +1974,28 @@ static void nf_service_associate_client(ogs_sbi_nf_service_t *nf_service) if (nf_service->num_of_addr) { addr = nf_service->addr[0].ipv4; addr6 = nf_service->addr[0].ipv6; + + /* + * Added support for using custom port numbers with FQDN. + * + * This code checks if a user-defined port number is provided in the + * NFService structure. If the is_port flag is set, the port is + * assigned the specified value. + * + * This ensures that services using non-default ports can be accessed + * correctly, regardless of whether TLS is enabled or not. + */ + if (nf_service->addr[0].is_port) + port = nf_service->addr[0].port; } if (nf_service->fqdn || addr || addr6) { client = ogs_sbi_client_find( - nf_service->scheme, nf_service->fqdn, 0, addr, addr6); + nf_service->scheme, nf_service->fqdn, port, addr, addr6); if (!client) { ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC); client = ogs_sbi_client_add( - nf_service->scheme, nf_service->fqdn, 0, addr, addr6); + nf_service->scheme, nf_service->fqdn, port, addr, addr6); if (!client) { ogs_error("%s: ogs_sbi_client_add() failed", OGS_FUNC); return;