[PATCH] replace gethostbyname with getaddrinfo
From c8bbf52e7a46ab238da1bd59d2afb98f6b46359c Mon Sep 17 00:00:00 2001 From: Florian Wickert <fw@ferncast.de> Date: Thu, 30 Jan 2020 10:47:19 +0100 Subject: [PATCH 1/1] replace gethostbyname with getaddrinfo getaddrinfo is guaranteed to be thread-safe in contrast to gethostbyname. Signed-off-by: Florian Wickert <fw@ferncast.de> --- librtmp/hashswf.c | 19 +++++++++++++++---- librtmp/rtmp.c | 15 ++++++++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/librtmp/hashswf.c b/librtmp/hashswf.c index 9f4e2c0..ab7d159 100644 --- a/librtmp/hashswf.c +++ b/librtmp/hashswf.c @@ -130,10 +130,21 @@ HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb) sa.sin_addr.s_addr = inet_addr(host); if (sa.sin_addr.s_addr == INADDR_NONE) { - struct hostent *hp = gethostbyname(host); - if (!hp || !hp->h_addr) - return HTTPRES_LOST_CONNECTION; - sa.sin_addr = *(struct in_addr *)hp->h_addr; + struct addrinfo hints; + struct addrinfo *res; + int err; + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_CANONNAME; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_RAW; + if ((err = getaddrinfo(host, NULL, &hints, &res)) != 0 || !res || !res->ai_addr) + { + if (res) + freeaddrinfo(res); + return HTTPRES_LOST_CONNECTION; + } + memcpy(&sa.sin_addr, &((const struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(struct in_addr)); + freeaddrinfo(res); } sa.sin_port = htons(port); sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); diff --git a/librtmp/rtmp.c b/librtmp/rtmp.c index a2863b0..361d4fc 100644 --- a/librtmp/rtmp.c +++ b/librtmp/rtmp.c @@ -885,14 +885,23 @@ add_addr_info(struct sockaddr_in *service, AVal *host, int port) service->sin_addr.s_addr = inet_addr(hostname); if (service->sin_addr.s_addr == INADDR_NONE) { - struct hostent *host = gethostbyname(hostname); - if (host == NULL || host->h_addr == NULL) + struct addrinfo hints; + struct addrinfo *res; + int err; + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_CANONNAME; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_RAW; + if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0 || !res || !res->ai_addr) { + if (res) + freeaddrinfo(res); RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname); ret = FALSE; goto finish; } - service->sin_addr = *(struct in_addr *)host->h_addr; + memcpy(&service->sin_addr, &((const struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(struct in_addr)); + freeaddrinfo(res); } service->sin_port = htons(port); -- 2.24.1
On Thu, 2020-01-30 at 12:29 +0100, Florian Wickert wrote:
From c8bbf52e7a46ab238da1bd59d2afb98f6b46359c Mon Sep 17 00:00:00 2001 From: Florian Wickert <fw@ferncast.de> Date: Thu, 30 Jan 2020 10:47:19 +0100 Subject: [PATCH 1/1] replace gethostbyname with getaddrinfo
getaddrinfo is guaranteed to be thread-safe in contrast to gethostbyname.
It also allows us to support IPv6, but I note your patch doesn't do that. Could it? This year marks the 25th anniversary of the publication of RFC1883, so it would only be a quarter of a century behind the times... :)
Thats right, getaddrinfo also supports IPv6 but my patch is only a drop-in replacement to fix threading problems. Due to a serious lack of time I have to leave this to someone else unfortunately. On 2/28/20 1:13 PM, David Woodhouse wrote:
From c8bbf52e7a46ab238da1bd59d2afb98f6b46359c Mon Sep 17 00:00:00 2001 From: Florian Wickert <fw@ferncast.de> Date: Thu, 30 Jan 2020 10:47:19 +0100 Subject: [PATCH 1/1] replace gethostbyname with getaddrinfo
getaddrinfo is guaranteed to be thread-safe in contrast to gethostbyname. It also allows us to support IPv6, but I note your patch doesn't do
On Thu, 2020-01-30 at 12:29 +0100, Florian Wickert wrote: that. Could it?
This year marks the 25th anniversary of the publication of RFC1883, so it would only be a quarter of a century behind the times... :)
-- Florian Wickert, M.Sc. Ferncast GmbH Gallierstr. 41a, 52074 Aachen Germany Phone: +49 241 99034567 Web: www.ferncast.de
participants (2)
-
David Woodhouse -
Florian Wickert