Commit 79f332e2 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

tls: replace fd with get_fd callback

parent 2f5e4399
......@@ -40,12 +40,12 @@ struct vlc_tls
{
vlc_object_t *obj;
void *sys;
int fd;
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)(vlc_tls_t *);
void (*close)(struct vlc_tls *);
};
/**
......@@ -89,6 +89,11 @@ VLC_API vlc_tls_t *vlc_tls_SessionCreate (vlc_tls_creds_t *, int fd,
*/
VLC_API void vlc_tls_SessionDelete (vlc_tls_t *);
static inline int vlc_tls_GetFD(vlc_tls_t *tls)
{
return tls->get_fd(tls);
}
/**
* Receives data through a TLS session.
*/
......@@ -129,7 +134,7 @@ static inline int vlc_tls_Shutdown(vlc_tls_t *tls, bool duplex)
*/
static inline void vlc_tls_Close(vlc_tls_t *session)
{
int fd = session->fd;
int fd = vlc_tls_GetFD(session);
vlc_tls_SessionDelete(session);
shutdown(fd, SHUT_RDWR);
......
......@@ -39,6 +39,12 @@ static const char *stream_content;
static size_t stream_length;
static bool stream_bad;
static int fd_callback(struct vlc_tls *tls)
{
(void) tls;
return -1;
}
static ssize_t recv_callback(struct vlc_tls *tls, struct iovec *iov,
unsigned count)
{
......@@ -72,6 +78,7 @@ static void close_callback(struct vlc_tls *tls)
static struct vlc_tls chunked_tls =
{
.get_fd = fd_callback,
.readv = recv_callback,
.close = close_callback,
};
......
......@@ -528,7 +528,7 @@ static ssize_t vlc_https_recv(vlc_tls_t *tls, void *buf, size_t len)
struct iovec iov;
size_t count = 0;
ufd.fd = tls->fd;
ufd.fd = vlc_tls_GetFD(tls);
ufd.events = POLLIN;
iov.iov_base = buf;
iov.iov_len = len;
......
......@@ -193,7 +193,7 @@ static ssize_t vlc_https_send(vlc_tls_t *tls, const void *buf, size_t len)
struct iovec iov;
size_t count = 0;
ufd.fd = tls->fd;
ufd.fd = vlc_tls_GetFD(tls);
ufd.events = POLLOUT;
iov.iov_base = (void *)buf;
iov.iov_len = len;
......
......@@ -40,6 +40,12 @@ static bool send_failure = false;
static bool expect_hello = true;
static vlc_sem_t rx;
static int fd_callback(vlc_tls_t *tls)
{
(void) tls;
return -1;
}
static ssize_t send_callback(vlc_tls_t *tls, const struct iovec *iov,
unsigned count)
{
......@@ -74,6 +80,7 @@ static ssize_t send_callback(vlc_tls_t *tls, const struct iovec *iov,
static vlc_tls_t fake_tls =
{
.get_fd = fd_callback,
.writev = send_callback,
};
......
......@@ -162,7 +162,7 @@ vlc_tls_t *vlc_https_connect_proxy(vlc_tls_creds_t *creds,
# error ENOSYS
/* TODO: create a vlc_tls_t * from a struct vlc_http_msg *. */
#else
int fd = session->fd;
int fd = vlc_tls_GetFD(session);
session->shutdown = vlc_http_tls_shutdown_ignore;
session->close = vlc_http_tls_close_ignore;
......
......@@ -157,6 +157,13 @@ static ssize_t vlc_gnutls_writev (gnutls_transport_ptr_t ptr,
return sendmsg (fd, &msg, MSG_NOSIGNAL);
}
static int gnutls_GetFD(vlc_tls_t *tls)
{
gnutls_session_t session = tls->sys;
return gnutls_transport_get_int(session);
}
static ssize_t gnutls_Recv(vlc_tls_t *tls, struct iovec *iov, unsigned count)
{
gnutls_session_t session = tls->sys;
......@@ -293,6 +300,7 @@ static int gnutls_SessionOpen(vlc_tls_creds_t *creds, vlc_tls_t *tls, int type,
gnutls_transport_set_int (session, fd);
gnutls_transport_set_vec_push_function (session, vlc_gnutls_writev);
tls->sys = session;
tls->get_fd = gnutls_GetFD;
tls->readv = gnutls_Recv;
tls->writev = gnutls_Send;
tls->shutdown = gnutls_Shutdown;
......
......@@ -424,6 +424,13 @@ static int st_Handshake (vlc_tls_creds_t *crd, vlc_tls_t *session,
}
}
static int st_GetFD (vlc_tls_t *session)
{
vlc_tls_sys_t *sys = session->sys;
return sys->i_fd;
}
/**
* Sends data through a TLS session.
*/
......
......@@ -136,7 +136,6 @@ vlc_tls_t *vlc_tls_SessionCreate (vlc_tls_creds_t *crd, int fd,
return NULL;
session->obj = crd->p_parent;
session->fd = fd;
int val = crd->open (crd, session, fd, host, alpn);
if (val != VLC_SUCCESS)
......@@ -222,7 +221,7 @@ ssize_t vlc_tls_Read(vlc_tls_t *session, void *buf, size_t len, bool waitall)
struct pollfd ufd;
struct iovec iov;
ufd.fd = session->fd;
ufd.fd = vlc_tls_GetFD(session);
ufd.events = POLLIN;
iov.iov_base = buf;
iov.iov_len = len;
......@@ -258,7 +257,7 @@ ssize_t vlc_tls_Write(vlc_tls_t *session, const void *buf, size_t len)
struct pollfd ufd;
struct iovec iov;
ufd.fd = session->fd;
ufd.fd = vlc_tls_GetFD(session);
ufd.events = POLLOUT;
iov.iov_base = (void *)buf;
iov.iov_len = len;
......@@ -318,31 +317,39 @@ error:
return NULL;
}
static int vlc_tls_DummyGetFD(vlc_tls_t *tls)
{
return (intptr_t)tls->sys;
}
static ssize_t vlc_tls_DummyReceive(vlc_tls_t *tls, struct iovec *iov,
unsigned count)
{
int fd = (intptr_t)tls->sys;
struct msghdr msg =
{
.msg_iov = iov,
.msg_iovlen = count,
};
return recvmsg(tls->fd, &msg, 0);
return recvmsg(fd, &msg, 0);
}
static ssize_t vlc_tls_DummySend(vlc_tls_t *tls, const struct iovec *iov,
unsigned count)
{
int fd = (intptr_t)tls->sys;
const struct msghdr msg =
{
.msg_iov = (struct iovec *)iov,
.msg_iovlen = count,
};
return sendmsg(tls->fd, &msg, MSG_NOSIGNAL);
return sendmsg(fd, &msg, MSG_NOSIGNAL);
}
static int vlc_tls_DummyShutdown(vlc_tls_t *tls, bool duplex)
{
return shutdown(tls->fd, duplex ? SHUT_RDWR : SHUT_WR);
int fd = (intptr_t)tls->sys;
return shutdown(fd, duplex ? SHUT_RDWR : SHUT_WR);
}
static void vlc_tls_DummyClose(vlc_tls_t *tls)
......@@ -357,7 +364,8 @@ vlc_tls_t *vlc_tls_DummyCreate(vlc_object_t *obj, int fd)
return NULL;
session->obj = obj;
session->fd = fd;
session->sys = (void *)(intptr_t)fd;
session->get_fd = vlc_tls_DummyGetFD;
session->readv = vlc_tls_DummyReceive;
session->writev = vlc_tls_DummySend;
session->shutdown = vlc_tls_DummyShutdown;
......
......@@ -73,7 +73,7 @@ static void *tls_echo(void *data)
ssize_t val;
char buf[256];
ufd.fd = tls->fd;
ufd.fd = vlc_tls_GetFD(tls);
while ((val = vlc_tls_SessionHandshake(server_creds, tls)) > 0)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment