neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/27024 )
Change subject: log: socket.c: rather use the osmo_sockaddr_str _FMT ......................................................................
log: socket.c: rather use the osmo_sockaddr_str _FMT
The OSMO_SOCKADDR_STR_FMT() and _ARGS() macros properly place square braces around IPv6 addresses, so that the port nr is clearly distinguishable.
before: 1:2::3:4:5 after: [1:2::3:4]:5
When using a struct reference, the macro resolves to '(&sastr) ? .. : ..', which the compiler complains about as "condition is always true". Shim around that error with a pointer variable.
I considered using osmo_sockaddr_to_str_c() instead, but here in socket.c we cannot assume that osmo_select_main_ctx() is being used and hence can't just use OTC_SELECT for log string composition. The struct osmo_sockaddr_str is a string representation in a local variable, and hence doesn't need talloc for log strings.
I considered adding log_check_level() around the log string conversion, but since all of these instances are on LOGL_ERROR, I didn't bother.
Related: SYS#5599 Change-Id: Idbe7582b2b7f14540919e911dad08af6d491f68f --- M src/socket.c 1 file changed, 12 insertions(+), 13 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/24/27024/1
diff --git a/src/socket.c b/src/socket.c index 0ffa11d..882ff02 100644 --- a/src/socket.c +++ b/src/socket.c @@ -484,8 +484,8 @@ }
#define _SOCKADDR_TO_STR(dest, sockaddr) do { \ - if (osmo_sockaddr_str_from_sockaddr(&dest, &sockaddr->u.sas)) \ - osmo_strlcpy(dest.ip, "Invalid IP", 11); \ + if (osmo_sockaddr_str_from_sockaddr(dest, &sockaddr->u.sas)) \ + osmo_strlcpy(dest->ip, "Invalid IP", 11); \ } while (0)
/*! Initialize a socket (including bind and/or connect) @@ -520,7 +520,8 @@ unsigned int flags) { int sfd = -1, rc, on = 1; - struct osmo_sockaddr_str sastr = {}; + struct osmo_sockaddr_str _sastr = {}; + struct osmo_sockaddr_str *sastr = &_sastr;
if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " @@ -551,8 +552,8 @@ sfd = socket_helper_osa(local, type, proto, flags); if (sfd < 0) { _SOCKADDR_TO_STR(sastr, local); - LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: %s:%u\n", - sastr.ip, sastr.port); + LOGP(DLGLOBAL, LOGL_ERROR, "no suitable local addr found for: " OSMO_SOCKADDR_STR_FMT "\n", + OSMO_SOCKADDR_STR_FMT_ARGS(sastr)); return -ENODEV; }
@@ -562,10 +563,8 @@ if (rc < 0) { _SOCKADDR_TO_STR(sastr, local); LOGP(DLGLOBAL, LOGL_ERROR, - "cannot setsockopt socket:" - " %s:%u: %s\n", - sastr.ip, sastr.port, - strerror(errno)); + "cannot setsockopt socket: " OSMO_SOCKADDR_STR_FMT ": %s\n", + OSMO_SOCKADDR_STR_FMT_ARGS(sastr), strerror(errno)); close(sfd); return rc; } @@ -573,8 +572,8 @@
if (bind(sfd, &local->u.sa, sizeof(struct osmo_sockaddr)) == -1) { _SOCKADDR_TO_STR(sastr, local); - LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: %s:%u: %s\n", - sastr.ip, sastr.port, strerror(errno)); + LOGP(DLGLOBAL, LOGL_ERROR, "unable to bind socket: " OSMO_SOCKADDR_STR_FMT ": %s\n", + OSMO_SOCKADDR_STR_FMT_ARGS(sastr), strerror(errno)); close(sfd); return -1; } @@ -596,8 +595,8 @@ rc = connect(sfd, &remote->u.sa, sizeof(struct osmo_sockaddr)); if (rc != 0 && errno != EINPROGRESS) { _SOCKADDR_TO_STR(sastr, remote); - LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n", - sastr.ip, sastr.port, strerror(errno)); + LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: " OSMO_SOCKADDR_STR_FMT ": %s\n", + OSMO_SOCKADDR_STR_FMT_ARGS(sastr), strerror(errno)); close(sfd); return rc; }