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
changes/24/27024/3
Neels Hofmeyr 12 months ago
parent e452dd053e
commit f3270f246f
  1. 25
      src/socket.c

@ -484,8 +484,8 @@ int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto,
}
#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 @@ int osmo_sock_init_osa(uint16_t type, uint8_t proto,
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 @@ int osmo_sock_init_osa(uint16_t type, uint8_t proto,
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 @@ int osmo_sock_init_osa(uint16_t type, uint8_t proto,
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 @@ int osmo_sock_init_osa(uint16_t type, uint8_t proto,
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 @@ int osmo_sock_init_osa(uint16_t type, uint8_t proto,
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;
}

Loading…
Cancel
Save