From f8b90105824ac66a30e56efb32a44dc896f2b2c5 Mon Sep 17 00:00:00 2001 From: Alistair Leslie-Hughes Date: Mon, 2 Jan 2023 21:05:15 +1100 Subject: rtp-send: Use getaddrinfo to improve support for ipv6. inet_pton isn't guarantee to support IPV6 address when a scope has been specified. Using getaddrinfo instead, we can safely pass through INET6+scope and have it translated to a usable address. Part-of: --- src/modules/rtp/module-rtp-send.c | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/modules/rtp/module-rtp-send.c b/src/modules/rtp/module-rtp-send.c index fd5b63abe..e917105c7 100644 --- a/src/modules/rtp/module-rtp-send.c +++ b/src/modules/rtp/module-rtp-send.c @@ -26,6 +26,9 @@ #include #include #include +#ifdef HAVE_NETDB_H +#include +#endif #include #include @@ -338,6 +341,44 @@ int pa__init(pa_module*m) { if (dst_addr == NULL) dst_addr = pa_modargs_get_value(ma, "destination_ip", DEFAULT_DESTINATION_IP); +#if defined(HAVE_GETADDRINFO) + { + struct addrinfo *dst_addrinfo = NULL; + struct addrinfo hints; + + pa_zero(hints); + + hints.ai_flags = AI_NUMERICHOST; + if (getaddrinfo(dst_addr, NULL, &hints, &dst_addrinfo) != 0) { + pa_log("Invalid destination '%s'", dst_addr); + goto fail; + } + + af = dst_addrinfo->ai_family; + if (af == AF_INET) { + memcpy(&dst_sa4, dst_addrinfo->ai_addr, dst_addrinfo->ai_addrlen); + dst_sa4.sin_port = htons((uint16_t) port); + dst_sap_sa4 = dst_sa4; + dst_sap_sa4.sin_port = htons(SAP_PORT); + } +#ifdef HAVE_IPV6 + else if (af == AF_INET6) { + memcpy(&dst_sa6, dst_addrinfo->ai_addr, dst_addrinfo->ai_addrlen); + dst_sa6.sin6_port = htons((uint16_t) port); + dst_sap_sa6 = dst_sa6; + dst_sap_sa6.sin6_port = htons(SAP_PORT); + } +#endif + else + { + freeaddrinfo(dst_addrinfo); + pa_log("Invalid destination '%s'", dst_addr); + goto fail; + } + + freeaddrinfo(dst_addrinfo); + } +#else if (inet_pton(AF_INET, dst_addr, &dst_sa4.sin_addr) > 0) { dst_sa4.sin_family = af = AF_INET; dst_sa4.sin_port = htons((uint16_t) port); @@ -357,6 +398,7 @@ int pa__init(pa_module*m) { pa_log("Invalid destination '%s'", dst_addr); goto fail; } +#endif /* HAVE_GETADDRINFO */ if ((fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) { pa_log("socket() failed: %s", pa_cstrerror(errno)); -- cgit v1.2.1