[vlc-commits] http: use vlc_tls_SocketOpenTCP()

Rémi Denis-Courmont git at videolan.org
Wed Feb 22 21:37:36 CET 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Feb 22 22:16:04 2017 +0200| [3188acf806081b286e474a4d249dc7de6caf89e6] | committer: Rémi Denis-Courmont

http: use vlc_tls_SocketOpenTCP()

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=3188acf806081b286e474a4d249dc7de6caf89e6
---

 modules/access/http/transport.c | 97 +++--------------------------------------
 1 file changed, 7 insertions(+), 90 deletions(-)

diff --git a/modules/access/http/transport.c b/modules/access/http/transport.c
index cf51544..50138d6 100644
--- a/modules/access/http/transport.c
+++ b/modules/access/http/transport.c
@@ -22,103 +22,18 @@
 # include <config.h>
 #endif
 
-#include <assert.h>
-#include <errno.h>
-#include <stdint.h>
 #include <stdlib.h>
-#ifdef HAVE_POLL
-#include <poll.h>
-#endif
-#include <fcntl.h>
 #include <vlc_common.h>
-#include <vlc_network.h>
 #include <vlc_tls.h>
 
 #include "transport.h"
 
-static void cleanup_addrinfo(void *data)
-{
-    freeaddrinfo(data);
-}
-
-static void cleanup_fd(void *data)
-{
-    net_Close((intptr_t)data);
-}
-
-static int vlc_tcp_connect(vlc_object_t *obj, const char *name, unsigned port)
-{
-    struct addrinfo hints =
-    {
-        .ai_socktype = SOCK_STREAM,
-        .ai_protocol = IPPROTO_TCP,
-    }, *res;
-
-    assert(name != NULL);
-    msg_Dbg(obj, "resolving %s ...", name);
-
-    int val = vlc_getaddrinfo(name, port, &hints, &res);
-    if (val != 0)
-    {   /* TODO: C locale for gai_strerror() */
-        msg_Err(obj, "cannot resolve %s port %u: %s", name, port,
-                gai_strerror(val));
-        return -1;
-    }
-
-    int fd = -1;
-
-    vlc_cleanup_push(cleanup_addrinfo, res);
-    msg_Dbg(obj, "connecting to %s port %u ...", name, port);
-
-    for (const struct addrinfo *p = res; p != NULL; p = p->ai_next)
-    {
-        fd = vlc_socket(p->ai_family, p->ai_socktype, p->ai_protocol, false);
-        if (fd == -1)
-        {
-            msg_Warn(obj, "cannot create socket: %s", vlc_strerror_c(errno));
-            continue;
-        }
-
-        vlc_cleanup_push(cleanup_fd, (void *)(intptr_t)fd);
-        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));
-
-        val = connect(fd, p->ai_addr, p->ai_addrlen);
-        vlc_cleanup_pop();
-
-        if (val == 0)
-            break; /* success! */
-
-        msg_Err(obj, "cannot connect to %s port %u: %s", name, port,
-                vlc_strerror_c(errno));
-        net_Close(fd);
-        fd = -1;
-    }
-
-    vlc_cleanup_pop();
-    freeaddrinfo(res);
-
-    if (fd != -1)
-#ifndef _WIN32
-        fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
-#else
-        ioctlsocket(fd, FIONBIO, &(unsigned long){ 1 });
-#endif
-    return fd;
-}
-
 vlc_tls_t *vlc_http_connect(vlc_object_t *obj, const char *name, unsigned port)
 {
     if (port == 0)
         port = 80;
 
-    int fd = vlc_tcp_connect(obj, name, port);
-    if (fd == -1)
-        return NULL;
-
-    vlc_tls_t *tls = vlc_tls_SocketOpen(obj, fd);
-    if (tls == NULL)
-        net_Close(fd);
-    return tls;
+    return vlc_tls_SocketOpenTCP(obj, name, port);
 }
 
 vlc_tls_t *vlc_https_connect(vlc_tls_creds_t *creds, const char *name,
@@ -127,19 +42,21 @@ vlc_tls_t *vlc_https_connect(vlc_tls_creds_t *creds, const char *name,
     if (port == 0)
         port = 443;
 
-    int fd = vlc_tcp_connect(creds->obj.parent, name, port);
-    if (fd == -1)
+    /* TODO: implement fast open. This requires merging vlc_tls_SocketOpenTCP()
+     * and vlc_tls_ClientSessionCreate() though. */
+    vlc_tls_t *sock = vlc_tls_SocketOpenTCP(creds->obj.parent, name, port);
+    if (sock == NULL)
         return NULL;
 
     /* TLS with ALPN */
     const char *alpn[] = { "h2", "http/1.1", NULL };
     char *alp;
 
-    vlc_tls_t *tls = vlc_tls_ClientSessionCreateFD(creds, fd, name, "https",
+    vlc_tls_t *tls = vlc_tls_ClientSessionCreate(creds, sock, name, "https",
                                                  alpn + !*two, &alp);
     if (tls == NULL)
     {
-        net_Close(fd);
+        vlc_tls_Close(sock);
         return NULL;
     }
 



More information about the vlc-commits mailing list