[rtmpdump] [PATCH] replace gethostbyname with getaddrinfo

Florian Wickert fw at ferncast.de
Thu Jan 30 13:29:06 EET 2020


 From c8bbf52e7a46ab238da1bd59d2afb98f6b46359c Mon Sep 17 00:00:00 2001
From: Florian Wickert <fw at 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 at 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



More information about the rtmpdump mailing list