From fbbe983a05dcddb51751e6762e099e399fa479bc Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Tue, 24 Sep 2024 20:17:34 +0300 Subject: [PATCH] rtp-sap: make the receive socket listen only to the configured sap.ip In line also with da8e207de9d14118e759865a6fd418d4edacf4ae, make sure that the rtp-sap receive socket listens only to the configured sap.ip in unicast mode, instead of accepting all packets on the port. This additionally fixes breakage that was inadvertently introduced in f2f204d60458753308792b3adcb86e443d13fe49. Because the `struct sockaddr_storage *sa` argument in `make_recv_socket` points to `impl->sap_addr`, changing the address to INADDR_ANY in the unicast code path would also silently cause the sender socket to try to connect to INADDR_ANY and fail. --- src/modules/module-rtp-sap.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/modules/module-rtp-sap.c b/src/modules/module-rtp-sap.c index cda09efb4..b7eeca4d4 100644 --- a/src/modules/module-rtp-sap.c +++ b/src/modules/module-rtp-sap.c @@ -455,6 +455,8 @@ static int make_recv_socket(struct sockaddr_storage *sa, socklen_t salen, { int af, fd, val, res; struct ifreq req; + struct sockaddr_storage ba = *sa; + bool do_connect = false; char addr[128]; af = sa->ss_family; @@ -488,7 +490,11 @@ static int make_recv_socket(struct sockaddr_storage *sa, socklen_t salen, pw_log_info("join IPv4 group: %s iface:%d", addr, req.ifr_ifindex); res = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr4, sizeof(mr4)); } else { - sa4->sin_addr.s_addr = INADDR_ANY; + struct sockaddr_in *ba4 = (struct sockaddr_in*)&ba; + if (ba4->sin_addr.s_addr != INADDR_ANY) { + ba4->sin_addr.s_addr = INADDR_ANY; + do_connect = true; + } } } else if (af == AF_INET6) { struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)sa; @@ -501,7 +507,8 @@ static int make_recv_socket(struct sockaddr_storage *sa, socklen_t salen, pw_log_info("join IPv6 group: %s iface:%d", addr, req.ifr_ifindex); res = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mr6, sizeof(mr6)); } else { - sa6->sin6_addr = in6addr_any; + struct sockaddr_in6 *ba6 = (struct sockaddr_in6*)&ba; + ba6->sin6_addr = in6addr_any; } } else { res = -EINVAL; @@ -514,11 +521,18 @@ static int make_recv_socket(struct sockaddr_storage *sa, socklen_t salen, goto error; } - if (bind(fd, (struct sockaddr*)sa, salen) < 0) { + if (bind(fd, (struct sockaddr*)&ba, salen) < 0) { res = -errno; pw_log_error("bind() failed: %m"); goto error; } + if (do_connect) { + if (connect(fd, (struct sockaddr*)sa, salen) < 0) { + res = -errno; + pw_log_error("connect() failed: %m"); + goto error; + } + } return fd; error: close(fd);