vlc | branch: master | Rémi Denis-Courmont <[email protected]> | Sun Jul 29 12:00:59 2018 +0300| [9541414aea77b995a867c44513d2f600d078c0f9] | committer: Rémi Denis-Courmont
tls: use const struct for callbacks > http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=9541414aea77b995a867c44513d2f600d078c0f9 --- include/vlc_tls.h | 14 +++++--- modules/access/http/chunked_test.c | 7 +++- modules/access/http/h2conn.c | 2 +- modules/access/http/h2output.c | 2 +- modules/access/http/h2output_test.c | 9 +++-- modules/access/http/tunnel.c | 19 +++++++---- modules/misc/gnutls.c | 19 +++++++---- modules/misc/securetransport.c | 19 +++++++---- .../chromecast/chromecast_communication.cpp | 2 +- src/network/httpd.c | 4 +-- src/network/stream.c | 38 ++++++++++++++-------- src/network/tls.c | 2 +- test/modules/misc/tls.c | 4 +-- 13 files changed, 91 insertions(+), 50 deletions(-) diff --git a/include/vlc_tls.h b/include/vlc_tls.h index a13471598f..5243412b1e 100644 --- a/include/vlc_tls.h +++ b/include/vlc_tls.h @@ -42,14 +42,18 @@ /** Transport layer socket */ typedef struct vlc_tls { + const struct vlc_tls_operations *ops; + struct vlc_tls *p; +} vlc_tls_t; + +struct vlc_tls_operations +{ int (*get_fd)(struct vlc_tls *); ssize_t (*readv)(struct vlc_tls *, struct iovec *, unsigned); ssize_t (*writev)(struct vlc_tls *, const struct iovec *, unsigned); int (*shutdown)(struct vlc_tls *, bool duplex); void (*close)(struct vlc_tls *); - - struct vlc_tls *p; -} vlc_tls_t; +}; /** * \defgroup tls Transport Layer Security @@ -186,7 +190,7 @@ VLC_API void vlc_tls_SessionDelete (vlc_tls_t *); static inline int vlc_tls_GetFD(vlc_tls_t *tls) { - return tls->get_fd(tls); + return tls->ops->get_fd(tls); } /** @@ -240,7 +244,7 @@ VLC_API ssize_t vlc_tls_Write(vlc_tls_t *, const void *buf, size_t len); */ static inline int vlc_tls_Shutdown(vlc_tls_t *tls, bool duplex) { - return tls->shutdown(tls, duplex); + return tls->ops->shutdown(tls, duplex); } /** diff --git a/modules/access/http/chunked_test.c b/modules/access/http/chunked_test.c index 0f134666ab..6740d84094 100644 --- a/modules/access/http/chunked_test.c +++ b/modules/access/http/chunked_test.c @@ -76,13 +76,18 @@ static void close_callback(struct vlc_tls *tls) (void) tls; } -static struct vlc_tls chunked_tls = +static const struct vlc_tls_operations chunked_ops = { .get_fd = fd_callback, .readv = recv_callback, .close = close_callback, }; +static struct vlc_tls chunked_tls = +{ + .ops = &chunked_ops, +}; + static void stream_close_callback(struct vlc_http_stream *stream, bool bad) { (void) stream; diff --git a/modules/access/http/h2conn.c b/modules/access/http/h2conn.c index fb66b8ab24..0cbef910a1 100644 --- a/modules/access/http/h2conn.c +++ b/modules/access/http/h2conn.c @@ -563,7 +563,7 @@ static ssize_t vlc_https_recv(vlc_tls_t *tls, void *buf, size_t len) while (iov.iov_len > 0) { int canc = vlc_savecancel(); - ssize_t val = tls->readv(tls, &iov, 1); + ssize_t val = tls->ops->readv(tls, &iov, 1); vlc_restorecancel(canc); diff --git a/modules/access/http/h2output.c b/modules/access/http/h2output.c index c171f1c51e..4ed8e33129 100644 --- a/modules/access/http/h2output.c +++ b/modules/access/http/h2output.c @@ -204,7 +204,7 @@ static ssize_t vlc_https_send(vlc_tls_t *tls, const void *buf, size_t len) while (count < len) { int canc = vlc_savecancel(); - ssize_t val = tls->writev(tls, &iov, 1); + ssize_t val = tls->ops->writev(tls, &iov, 1); vlc_restorecancel(canc); diff --git a/modules/access/http/h2output_test.c b/modules/access/http/h2output_test.c index 1faa155677..3530f5969f 100644 --- a/modules/access/http/h2output_test.c +++ b/modules/access/http/h2output_test.c @@ -51,7 +51,7 @@ static ssize_t send_callback(vlc_tls_t *tls, const struct iovec *iov, unsigned count) { assert(count == 1); - assert(tls->writev == send_callback); + assert(tls->ops->writev == send_callback); const uint8_t *p = iov->iov_base; size_t len = iov->iov_len; @@ -83,12 +83,17 @@ static ssize_t send_callback(vlc_tls_t *tls, const struct iovec *iov, return send_failure ? -1 : (ssize_t)len; } -static vlc_tls_t fake_tls = +static const struct vlc_tls_operations fake_ops = { .get_fd = fd_callback, .writev = send_callback, }; +static vlc_tls_t fake_tls = +{ + .ops = &fake_ops, +}; + static struct vlc_h2_frame *frame(unsigned char c) { struct vlc_h2_frame *f = vlc_h2_frame_data(1, &c, 1, false); diff --git a/modules/access/http/tunnel.c b/modules/access/http/tunnel.c index f311ecff52..6ad53a0e25 100644 --- a/modules/access/http/tunnel.c +++ b/modules/access/http/tunnel.c @@ -107,7 +107,7 @@ static ssize_t vlc_tls_ProxyRead(vlc_tls_t *tls, struct iovec *iov, vlc_tls_proxy_t *proxy = (vlc_tls_proxy_t *)tls; vlc_tls_t *sock = proxy->sock; - return sock->readv(sock, iov, count); + return sock->ops->readv(sock, iov, count); } static ssize_t vlc_tls_ProxyWrite(vlc_tls_t *tls, const struct iovec *iov, @@ -116,7 +116,7 @@ static ssize_t vlc_tls_ProxyWrite(vlc_tls_t *tls, const struct iovec *iov, vlc_tls_proxy_t *proxy = (vlc_tls_proxy_t *)tls; vlc_tls_t *sock = proxy->sock; - return sock->writev(sock, iov, count); + return sock->ops->writev(sock, iov, count); } static int vlc_tls_ProxyShutdown(vlc_tls_t *tls, bool duplex) @@ -133,6 +133,15 @@ static void vlc_tls_ProxyClose(vlc_tls_t *tls) free(proxy); } +static const struct vlc_tls_operations vlc_tls_proxy_ops = +{ + vlc_tls_ProxyGetFD, + vlc_tls_ProxyRead, + vlc_tls_ProxyWrite, + vlc_tls_ProxyShutdown, + vlc_tls_ProxyClose, +}; + vlc_tls_t *vlc_https_connect_proxy(void *ctx, vlc_tls_creds_t *creds, const char *hostname, unsigned port, bool *restrict two, const char *proxy) @@ -181,11 +190,7 @@ vlc_tls_t *vlc_https_connect_proxy(void *ctx, vlc_tls_creds_t *creds, goto error; } - psock->tls.get_fd = vlc_tls_ProxyGetFD; - psock->tls.readv = vlc_tls_ProxyRead; - psock->tls.writev = vlc_tls_ProxyWrite; - psock->tls.shutdown = vlc_tls_ProxyShutdown; - psock->tls.close = vlc_tls_ProxyClose; + psock->tls.ops = &vlc_tls_proxy_ops; psock->tls.p = NULL; psock->sock = sock; diff --git a/modules/misc/gnutls.c b/modules/misc/gnutls.c index 1cca9c14a7..56541af94b 100644 --- a/modules/misc/gnutls.c +++ b/modules/misc/gnutls.c @@ -102,7 +102,7 @@ static ssize_t vlc_gnutls_read(gnutls_transport_ptr_t ptr, void *buf, .iov_len = length, }; - return sock->readv(sock, &iov, 1); + return sock->ops->readv(sock, &iov, 1); } static ssize_t vlc_gnutls_writev(gnutls_transport_ptr_t ptr, @@ -130,7 +130,7 @@ static ssize_t vlc_gnutls_writev(gnutls_transport_ptr_t ptr, iov[i].iov_len = giov[i].iov_len; } - return sock->writev(sock, iov, iovcnt); + return sock->ops->writev(sock, iov, iovcnt); } static int gnutls_GetFD(vlc_tls_t *tls) @@ -217,6 +217,15 @@ static void gnutls_Close (vlc_tls_t *tls) free(priv); } +static const struct vlc_tls_operations gnutls_ops = +{ + gnutls_GetFD, + gnutls_Recv, + gnutls_Send, + gnutls_Shutdown, + gnutls_Close, +}; + static vlc_tls_gnutls_t *gnutls_SessionOpen(vlc_tls_creds_t *creds, int type, gnutls_certificate_credentials_t x509, vlc_tls_t *sock, @@ -298,11 +307,7 @@ static vlc_tls_gnutls_t *gnutls_SessionOpen(vlc_tls_creds_t *creds, int type, vlc_tls_t *tls = &priv->tls; - tls->get_fd = gnutls_GetFD; - tls->readv = gnutls_Recv; - tls->writev = gnutls_Send; - tls->shutdown = gnutls_Shutdown; - tls->close = gnutls_Close; + tls->ops = &gnutls_ops; return priv; error: diff --git a/modules/misc/securetransport.c b/modules/misc/securetransport.c index 4cafc30d3a..6928e9565d 100644 --- a/modules/misc/securetransport.c +++ b/modules/misc/securetransport.c @@ -220,7 +220,7 @@ static OSStatus st_SocketReadFunc (SSLConnectionRef connection, OSStatus retValue = noErr; while (iov.iov_len > 0) { - ssize_t val = sys->sock->readv(sys->sock, &iov, 1); + ssize_t val = sys->sock->ops->readv(sys->sock, &iov, 1); if (val <= 0) { if (val == 0) { msg_Dbg(sys->obj, "found eof"); @@ -274,7 +274,7 @@ static OSStatus st_SocketWriteFunc (SSLConnectionRef connection, OSStatus retValue = noErr; while (iov.iov_len > 0) { - ssize_t val = sys->sock->writev(sys->sock, &iov, 1); + ssize_t val = sys->sock->ops->writev(sys->sock, &iov, 1); if (val < 0) { switch (errno) { case EAGAIN: @@ -691,6 +691,15 @@ static void st_SessionClose (vlc_tls_t *session) { free(sys); } +static const struct vlc_tls_operations st_ops = +{ + st_GetFD, + st_Recv, + st_Send, + st_SessionShutdown, + st_SessionClose, +}; + /** * Initializes a client-side TLS session. */ @@ -714,11 +723,7 @@ static vlc_tls_t *st_SessionOpenCommon(vlc_tls_creds_t *crd, vlc_tls_t *sock, vlc_tls_t *tls = &sys->tls; - tls->get_fd = st_GetFD; - tls->readv = st_Recv; - tls->writev = st_Send; - tls->shutdown = st_SessionShutdown; - tls->close = st_SessionClose; + tls->ops = &st_ops; crd->handshake = st_Handshake; SSLContextRef p_context = NULL; diff --git a/modules/stream_out/chromecast/chromecast_communication.cpp b/modules/stream_out/chromecast/chromecast_communication.cpp index 4d339f5efd..59df59a016 100644 --- a/modules/stream_out/chromecast/chromecast_communication.cpp +++ b/modules/stream_out/chromecast/chromecast_communication.cpp @@ -135,7 +135,7 @@ ssize_t ChromecastCommunication::receive( uint8_t *p_data, size_t i_size, int i_ * connection as dead. */ do { - ssize_t i_ret = m_tls->readv( m_tls, &iov, 1 ); + ssize_t i_ret = m_tls->ops->readv( m_tls, &iov, 1 ); if ( i_ret < 0 ) { #ifdef _WIN32 diff --git a/src/network/httpd.c b/src/network/httpd.c index d3cd9324ad..3d7ae0405d 100644 --- a/src/network/httpd.c +++ b/src/network/httpd.c @@ -1220,7 +1220,7 @@ ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len) { vlc_tls_t *sock = cl->sock; struct iovec iov = { .iov_base = p, .iov_len = i_len }; - return sock->readv(sock, &iov, 1); + return sock->ops->readv(sock, &iov, 1); } static @@ -1228,7 +1228,7 @@ ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len) { vlc_tls_t *sock = cl->sock; const struct iovec iov = { .iov_base = (void *)p, .iov_len = i_len }; - return sock->writev(sock, &iov, 1); + return sock->ops->writev(sock, &iov, 1); } diff --git a/src/network/stream.c b/src/network/stream.c index 90744ac0f0..7d53a6266e 100644 --- a/src/network/stream.c +++ b/src/network/stream.c @@ -69,7 +69,7 @@ ssize_t vlc_tls_Read(vlc_tls_t *session, void *buf, size_t len, bool waitall) return -1; } - ssize_t val = session->readv(session, &iov, 1); + ssize_t val = session->ops->readv(session, &iov, 1); if (val > 0) { if (!waitall) @@ -110,7 +110,7 @@ ssize_t vlc_tls_Write(vlc_tls_t *session, const void *buf, size_t len) return -1; } - ssize_t val = session->writev(session, &iov, 1); + ssize_t val = session->ops->writev(session, &iov, 1); if (val > 0) { iov.iov_base = ((char *)iov.iov_base) + val; @@ -212,6 +212,15 @@ static void vlc_tls_SocketClose(vlc_tls_t *tls) free(tls); } +static const struct vlc_tls_operations vlc_tls_socket_ops = +{ + vlc_tls_SocketGetFD, + vlc_tls_SocketRead, + vlc_tls_SocketWrite, + vlc_tls_SocketShutdown, + vlc_tls_SocketClose, +}; + static vlc_tls_t *vlc_tls_SocketAlloc(int fd, const struct sockaddr *restrict peer, socklen_t peerlen) @@ -222,11 +231,7 @@ static vlc_tls_t *vlc_tls_SocketAlloc(int fd, vlc_tls_t *tls = &sock->tls; - tls->get_fd = vlc_tls_SocketGetFD; - tls->readv = vlc_tls_SocketRead; - tls->writev = vlc_tls_SocketWrite; - tls->shutdown = vlc_tls_SocketShutdown; - tls->close = vlc_tls_SocketClose; + tls->ops = &vlc_tls_socket_ops; tls->p = NULL; sock->fd = fd; @@ -346,6 +351,9 @@ static ssize_t vlc_tls_Connect(vlc_tls_t *tls) static ssize_t vlc_tls_ConnectWrite(vlc_tls_t *tls, const struct iovec *iov,unsigned count) { + /* Next time, write directly. Do not retry to connect. */ + tls->ops = &vlc_tls_socket_ops; + #ifdef MSG_FASTOPEN vlc_tls_socket_t *sock = (vlc_tls_socket_t *)tls; const struct msghdr msg = @@ -357,9 +365,6 @@ static ssize_t vlc_tls_ConnectWrite(vlc_tls_t *tls, }; ssize_t ret; - /* Next time, write directly. Do not retry to connect. */ - tls->writev = vlc_tls_SocketWrite; - ret = sendmsg(vlc_tls_SocketGetFD(tls), &msg, MSG_NOSIGNAL|MSG_FASTOPEN); if (ret >= 0) { /* Fast open in progress */ @@ -375,8 +380,6 @@ static ssize_t vlc_tls_ConnectWrite(vlc_tls_t *tls, if (errno != EOPNOTSUPP) return -1; /* Fast open not supported or disabled... fallback to normal mode */ -#else - tls->writev = vlc_tls_SocketWrite; #endif if (vlc_tls_Connect(tls)) @@ -385,6 +388,15 @@ static ssize_t vlc_tls_ConnectWrite(vlc_tls_t *tls, return vlc_tls_SocketWrite(tls, iov, count); } +static const struct vlc_tls_operations vlc_tls_socket_fastopen_ops = +{ + vlc_tls_SocketGetFD, + vlc_tls_SocketRead, + vlc_tls_ConnectWrite, + vlc_tls_SocketShutdown, + vlc_tls_SocketClose, +}; + vlc_tls_t *vlc_tls_SocketOpenAddrInfo(const struct addrinfo *restrict info, bool defer_connect) { @@ -395,7 +407,7 @@ vlc_tls_t *vlc_tls_SocketOpenAddrInfo(const struct addrinfo *restrict info, if (defer_connect) { /* The socket is not connected yet. * The connection will be triggered on the first send. */ - sock->writev = vlc_tls_ConnectWrite; + sock->ops = &vlc_tls_socket_fastopen_ops; } else { diff --git a/src/network/tls.c b/src/network/tls.c index f94196b5f8..00193eeec8 100644 --- a/src/network/tls.c +++ b/src/network/tls.c @@ -145,7 +145,7 @@ static vlc_tls_t *vlc_tls_SessionCreate(vlc_tls_creds_t *crd, void vlc_tls_SessionDelete (vlc_tls_t *session) { int canc = vlc_savecancel(); - session->close(session); + session->ops->close(session); vlc_restorecancel(canc); } diff --git a/test/modules/misc/tls.c b/test/modules/misc/tls.c index 2860f5ba03..4259151ad8 100644 --- a/test/modules/misc/tls.c +++ b/test/modules/misc/tls.c @@ -192,7 +192,7 @@ int main(void) iov.iov_base = buf; iov.iov_len = sizeof (buf); - val = tls->readv(tls, &iov, 1); + val = tls->ops->readv(tls, &iov, 1); assert(val == -1 && errno == EAGAIN); val = vlc_tls_Write(tls, "Hello ", 6); @@ -230,7 +230,7 @@ int main(void) data[i] = rand_r(&seed); bytes += sizeof (data); } - while ((val = tls->writev(tls, &iov, 1)) == sizeof (data)); + while ((val = tls->ops->writev(tls, &iov, 1)) == sizeof (data)); bytes -= sizeof (data); if (val > 0) _______________________________________________ vlc-commits mailing list [email protected] https://mailman.videolan.org/listinfo/vlc-commits
