From 7e5e18520a2b9d0a6429feaad7c7079dbc017de1 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Fri, 16 Jun 2023 14:21:43 +0200 Subject: stream: srv cb: Use osmo_sockaddr and improve logging when cli connects Specially important is the change where we now print the remote port which connected to us, not our local port (which is already printed as part of LOGSLNK). Change-Id: I6e556f8263496ded41bb221ccc8e193c5361697f --- src/stream.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/stream.c b/src/stream.c index b646c3d..fdc1cc1 100644 --- a/src/stream.c +++ b/src/stream.c @@ -1155,36 +1155,27 @@ static int osmo_stream_srv_ofd_cb(struct osmo_fd *ofd, unsigned int what) { int ret; int sock_fd; - char addrstr[128]; - bool is_ipv6 = false; - struct sockaddr_storage sa; - socklen_t sa_len = sizeof(sa); + struct osmo_sockaddr osa; + socklen_t sa_len = sizeof(osa.u.sas); struct osmo_stream_srv_link *link = ofd->data; - ret = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len); + ret = accept(ofd->fd, &osa.u.sa, &sa_len); if (ret < 0) { - LOGP(DLINP, LOGL_ERROR, "failed to accept from origin " - "peer, reason=`%s'\n", strerror(errno)); + LOGP(DLINP, LOGL_ERROR, "failed to accept from origin peer, reason=`%s'\n", + strerror(errno)); return ret; } sock_fd = ret; - is_ipv6 = false; - switch (((struct sockaddr *)&sa)->sa_family) { + switch (osa.u.sa.sa_family) { case AF_UNIX: LOGSLNK(link, LOGL_DEBUG, "accept()ed new link on fd %d\n", sock_fd); break; case AF_INET6: - is_ipv6 = true; - /* fall through */ case AF_INET: - LOGSLNK(link, LOGL_DEBUG, "accept()ed new link from %s to port %u\n", - inet_ntop(is_ipv6 ? AF_INET6 : AF_INET, - is_ipv6 ? (void *)&(((struct sockaddr_in6 *)&sa)->sin6_addr) : - (void *)&(((struct sockaddr_in *)&sa)->sin_addr), - addrstr, sizeof(addrstr)), - link->port); + LOGSLNK(link, LOGL_DEBUG, "accept()ed new link from %s\n", + osmo_sockaddr_to_str(&osa)); if (link->proto == IPPROTO_SCTP) { ret = sctp_sock_activate_events(sock_fd); @@ -1194,7 +1185,7 @@ static int osmo_stream_srv_ofd_cb(struct osmo_fd *ofd, unsigned int what) break; default: LOGSLNK(link, LOGL_DEBUG, "accept()ed unexpected address family %d\n", - ((struct sockaddr *)&sa)->sa_family); + osa.u.sa.sa_family); goto error_close_socket; } -- cgit v1.2.3