Use osmo_sockaddr_strs_to_str() for multiaddr helper

Related: OS#5581
Change-Id: Icef53fe4b6e51563d97a1bc48001d67679b3b6e9
This commit is contained in:
Max 2022-08-08 20:47:12 +07:00
parent 261619cb7b
commit 3c4f62f485
1 changed files with 11 additions and 15 deletions

View File

@ -209,30 +209,26 @@ static int socket_helper_osa(const struct osmo_sockaddr *addr, uint16_t type, ui
*/
static int multiaddr_snprintf(char* buf, size_t buf_len, const char **hosts, size_t host_cnt)
{
int len = 0, offset = 0, rem = buf_len;
struct osmo_sockaddr_str *sa_strs[host_cnt];
void *ctx = talloc_named_const(NULL, 0, "multiaddr_snprintf");
size_t i;
int ret;
char *after;
if (buf_len < 3)
return -EINVAL;
if (host_cnt != 1) {
ret = snprintf(buf, rem, "(");
if (ret < 0)
return ret;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
}
for (i = 0; i < host_cnt; i++) {
if (host_cnt == 1)
after = "";
else
after = (i == (host_cnt - 1)) ? ")" : "|";
ret = snprintf(buf + offset, rem, "%s%s", hosts[i] ? : "0.0.0.0", after);
OSMO_SNPRINTF_RET(ret, rem, offset, len);
sa_strs[i] = talloc_zero(ctx, struct osmo_sockaddr_str);
ret = osmo_sockaddr_str_from_str2(sa_strs[i], hosts[i]);
if (ret < 0) {
talloc_free(ctx);
return ret;
}
}
return len;
ret = osmo_sockaddr_strs_to_str(buf, buf_len, (const struct osmo_sockaddr_str **)sa_strs, host_cnt);
talloc_free(ctx);
return ret;
}
#endif /* HAVE_LIBSCTP */