From 179f35702ece13f4ab7fd1b331bef664834d8473 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 18 Mar 2019 18:38:47 +0100 Subject: Add _c versions of functions that otherwise return static buffers We have a habit of returning static buffers from some functions, particularly when generating some kind of string values. This is convenient in terms of memory management, but it comes at the expense of not being thread-safe, and not allowing for two calls of the related function within one printf() statement. Let's introduce _c suffix versions of those functions where the caller passes in a talloc context from which the output buffer shall be allocated. Change-Id: I8481c19b68ff67cfa22abb93c405ebcfcb0ab19b --- src/socket.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/socket.c') diff --git a/src/socket.c b/src/socket.c index 3a46ad02..c817e723 100644 --- a/src/socket.c +++ b/src/socket.c @@ -827,6 +827,20 @@ const char *osmo_sock_get_name2(int fd) return str; } +/*! Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10". + * This does not include braces like osmo_sock_get_name(). + * \param[in] fd File descriptor of socket. + * \return Static string buffer containing the result. + */ +char *osmo_sock_get_name2_c(const void *ctx, int fd) +{ + char *str = talloc_size(ctx, OSMO_SOCK_NAME_MAXLEN); + if (!str) + return NULL; + osmo_sock_get_name_buf(str, sizeof(str), fd); + return str; +} + static int sock_get_domain(int fd) { int domain; -- cgit v1.2.3