2017-05-23 19:15:33 +02:00
|
|
|
/* PipeWire
|
2016-10-17 18:29:05 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2018 Wim Taymans
|
2016-10-17 18:29:05 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2016-10-17 18:29:05 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2016-10-17 18:29:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
2018-08-14 12:33:53 +02:00
|
|
|
#include <spa/debug/pod.h>
|
2017-05-29 10:28:19 +02:00
|
|
|
|
2017-07-11 15:57:20 +02:00
|
|
|
#include <pipewire/pipewire.h>
|
2019-01-14 12:58:23 +01:00
|
|
|
#include "pipewire/private.h"
|
2017-07-11 15:57:20 +02:00
|
|
|
|
2016-10-17 18:29:05 +02:00
|
|
|
#include "connection.h"
|
|
|
|
|
|
2017-07-04 11:08:40 +02:00
|
|
|
#define MAX_BUFFER_SIZE (1024 * 32)
|
2018-09-11 12:01:19 +02:00
|
|
|
#define MAX_FDS 1024
|
|
|
|
|
#define MAX_FDS_MSG 28
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
#define HDR_SIZE 16
|
|
|
|
|
|
2017-05-29 10:28:19 +02:00
|
|
|
static bool debug_messages = 0;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
struct buffer {
|
2017-05-26 08:05:01 +02:00
|
|
|
uint8_t *buffer_data;
|
|
|
|
|
size_t buffer_size;
|
|
|
|
|
size_t buffer_maxsize;
|
|
|
|
|
int fds[MAX_FDS];
|
|
|
|
|
uint32_t n_fds;
|
2016-11-03 19:41:53 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
uint32_t seq;
|
2018-02-20 18:19:11 +01:00
|
|
|
size_t offset;
|
2019-03-19 16:15:20 +01:00
|
|
|
size_t fds_offset;
|
|
|
|
|
struct pw_protocol_native_message msg;
|
2016-11-03 19:41:53 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
bool update;
|
2017-05-23 19:15:33 +02:00
|
|
|
};
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl {
|
2017-07-12 18:04:00 +02:00
|
|
|
struct pw_protocol_native_connection this;
|
2019-03-19 11:44:23 +01:00
|
|
|
struct pw_core *core;
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
struct buffer in, out;
|
2019-03-19 11:44:23 +01:00
|
|
|
struct spa_pod_builder builder;
|
2017-05-23 19:15:33 +02:00
|
|
|
};
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** \endcond */
|
|
|
|
|
|
|
|
|
|
/** Get an fd from a connection
|
|
|
|
|
*
|
|
|
|
|
* \param conn the connection
|
|
|
|
|
* \param index the index of the fd to get
|
2019-06-20 17:31:29 +02:00
|
|
|
* \return the fd at \a index or -ENOENT when no such fd exists
|
2017-05-30 19:46:51 +02:00
|
|
|
*
|
2017-07-12 18:04:00 +02:00
|
|
|
* \memberof pw_protocol_native_connection
|
2017-05-30 19:46:51 +02:00
|
|
|
*/
|
2017-07-12 18:04:00 +02:00
|
|
|
int pw_protocol_native_connection_get_fd(struct pw_protocol_native_connection *conn, uint32_t index)
|
2016-11-24 17:00:42 +01:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2019-03-19 16:15:20 +01:00
|
|
|
struct buffer *buf = &impl->in;
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
if (index >= buf->msg.n_fds)
|
2019-06-20 17:31:29 +02:00
|
|
|
return -ENOENT;
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
return buf->msg.fds[index];
|
2016-11-24 17:00:42 +01:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Add an fd to a connection
|
|
|
|
|
*
|
|
|
|
|
* \param conn the connection
|
|
|
|
|
* \param fd the fd to add
|
|
|
|
|
* \return the index of the fd or -1 when an error occured
|
|
|
|
|
*
|
2017-07-12 18:04:00 +02:00
|
|
|
* \memberof pw_protocol_native_connection
|
2017-05-30 19:46:51 +02:00
|
|
|
*/
|
2017-07-12 18:04:00 +02:00
|
|
|
uint32_t pw_protocol_native_connection_add_fd(struct pw_protocol_native_connection *conn, int fd)
|
2016-11-24 17:00:42 +01:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2019-03-19 16:15:20 +01:00
|
|
|
struct buffer *buf = &impl->out;
|
2017-05-26 08:05:01 +02:00
|
|
|
uint32_t index, i;
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
for (i = 0; i < buf->msg.n_fds; i++) {
|
|
|
|
|
if (buf->msg.fds[i] == fd)
|
2017-05-26 08:05:01 +02:00
|
|
|
return i;
|
|
|
|
|
}
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
index = buf->msg.n_fds;
|
|
|
|
|
if (index + buf->n_fds >= MAX_FDS) {
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_log_error("connection %p: too many fds", conn);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-03-09 19:42:35 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
buf->msg.fds[index] = fd;
|
|
|
|
|
buf->msg.n_fds++;
|
|
|
|
|
pw_log_debug("connection %p: add fd %d at index %d", conn, fd, index);
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
return index;
|
2016-11-24 17:00:42 +01:00
|
|
|
}
|
|
|
|
|
|
2017-07-12 18:04:00 +02:00
|
|
|
static void *connection_ensure_size(struct pw_protocol_native_connection *conn, struct buffer *buf, size_t size)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2019-06-19 16:22:22 +02:00
|
|
|
int res;
|
|
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
if (buf->buffer_size + size > buf->buffer_maxsize) {
|
|
|
|
|
buf->buffer_maxsize = SPA_ROUND_UP_N(buf->buffer_size + size, MAX_BUFFER_SIZE);
|
|
|
|
|
buf->buffer_data = realloc(buf->buffer_data, buf->buffer_maxsize);
|
2017-11-13 11:32:06 +01:00
|
|
|
if (buf->buffer_data == NULL) {
|
2019-06-19 16:22:22 +02:00
|
|
|
res = -errno;
|
2017-11-13 11:32:06 +01:00
|
|
|
buf->buffer_maxsize = 0;
|
2019-03-19 11:44:23 +01:00
|
|
|
spa_hook_list_call(&conn->listener_list,
|
|
|
|
|
struct pw_protocol_native_connection_events,
|
2019-06-19 16:22:22 +02:00
|
|
|
error, 0, -res);
|
|
|
|
|
errno = -res;
|
2017-11-13 11:32:06 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_log_warn("connection %p: resize buffer to %zd %zd %zd",
|
|
|
|
|
conn, buf->buffer_size, size, buf->buffer_maxsize);
|
|
|
|
|
}
|
|
|
|
|
return (uint8_t *) buf->buffer_data + buf->buffer_size;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
static int refill_buffer(struct pw_protocol_native_connection *conn, struct buffer *buf)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
ssize_t len;
|
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
struct msghdr msg = { 0 };
|
|
|
|
|
struct iovec iov[1];
|
2018-09-11 12:01:19 +02:00
|
|
|
char cmsgbuf[CMSG_SPACE(MAX_FDS_MSG * sizeof(int))];
|
2018-09-10 18:50:05 +02:00
|
|
|
int n_fds = 0;
|
2019-05-13 10:08:30 +02:00
|
|
|
size_t avail;
|
|
|
|
|
|
|
|
|
|
avail = buf->buffer_maxsize - buf->buffer_size;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
|
|
|
|
iov[0].iov_base = buf->buffer_data + buf->buffer_size;
|
2019-05-13 10:08:30 +02:00
|
|
|
iov[0].iov_len = avail;
|
2017-05-26 08:05:01 +02:00
|
|
|
msg.msg_iov = iov;
|
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
msg.msg_control = cmsgbuf;
|
|
|
|
|
msg.msg_controllen = sizeof(cmsgbuf);
|
2017-10-17 10:14:56 +02:00
|
|
|
msg.msg_flags = MSG_CMSG_CLOEXEC | MSG_DONTWAIT;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
len = recvmsg(conn->fd, &msg, msg.msg_flags);
|
2019-05-13 10:08:30 +02:00
|
|
|
if (len == 0 && avail != 0)
|
|
|
|
|
return -EPIPE;
|
|
|
|
|
else if (len < 0) {
|
2017-05-26 08:05:01 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
|
continue;
|
2017-10-20 17:03:13 +02:00
|
|
|
if (errno != EAGAIN || errno != EWOULDBLOCK)
|
2017-05-26 08:05:01 +02:00
|
|
|
goto recv_error;
|
2019-03-19 16:15:20 +01:00
|
|
|
return -EAGAIN;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf->buffer_size += len;
|
|
|
|
|
|
|
|
|
|
/* handle control messages */
|
|
|
|
|
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-09-10 18:50:05 +02:00
|
|
|
n_fds =
|
2017-05-26 08:05:01 +02:00
|
|
|
(cmsg->cmsg_len - ((char *) CMSG_DATA(cmsg) - (char *) cmsg)) / sizeof(int);
|
2018-09-10 18:50:05 +02:00
|
|
|
memcpy(&buf->fds[buf->n_fds], CMSG_DATA(cmsg), n_fds * sizeof(int));
|
|
|
|
|
buf->n_fds += n_fds;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
|
|
|
|
pw_log_trace("connection %p: %d read %zd bytes and %d fds", conn, conn->fd, len,
|
2018-09-10 18:50:05 +02:00
|
|
|
n_fds);
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
return 0;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
|
|
|
|
/* ERRORS */
|
2019-06-20 11:04:34 +02:00
|
|
|
recv_error:
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_log_error("could not recvmsg on fd %d: %s", conn->fd, strerror(errno));
|
2019-03-19 16:15:20 +01:00
|
|
|
return -errno;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
static void clear_buffer(struct buffer *buf)
|
2016-10-18 11:11:38 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
buf->n_fds = 0;
|
|
|
|
|
buf->buffer_size = 0;
|
2019-03-19 16:15:20 +01:00
|
|
|
buf->offset = 0;
|
|
|
|
|
buf->fds_offset = 0;
|
2016-10-18 11:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Make a new connection object for the given socket
|
|
|
|
|
*
|
|
|
|
|
* \param fd the socket
|
|
|
|
|
* \returns a newly allocated connection object
|
|
|
|
|
*
|
2017-07-12 18:04:00 +02:00
|
|
|
* \memberof pw_protocol_native_connection
|
2017-05-30 19:46:51 +02:00
|
|
|
*/
|
2018-08-14 12:33:53 +02:00
|
|
|
struct pw_protocol_native_connection *pw_protocol_native_connection_new(struct pw_core *core, int fd)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl;
|
2017-07-12 18:04:00 +02:00
|
|
|
struct pw_protocol_native_connection *this;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
impl = calloc(1, sizeof(struct impl));
|
2017-05-26 08:05:01 +02:00
|
|
|
if (impl == NULL)
|
|
|
|
|
return NULL;
|
2016-12-22 16:50:01 +01:00
|
|
|
|
2017-05-29 10:28:19 +02:00
|
|
|
debug_messages = pw_debug_is_category_enabled("connection");
|
2019-03-19 11:44:23 +01:00
|
|
|
impl->core = core;
|
2017-05-29 10:28:19 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
this = &impl->this;
|
2016-12-22 16:50:01 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_log_debug("connection %p: new", this);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
this->fd = fd;
|
2017-08-08 16:56:29 +02:00
|
|
|
spa_hook_list_init(&this->listener_list);
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2019-03-13 16:02:50 +01:00
|
|
|
impl->out.buffer_data = calloc(1, MAX_BUFFER_SIZE);
|
2017-05-26 08:05:01 +02:00
|
|
|
impl->out.buffer_maxsize = MAX_BUFFER_SIZE;
|
2019-03-13 16:02:50 +01:00
|
|
|
impl->in.buffer_data = calloc(1, MAX_BUFFER_SIZE);
|
2017-05-26 08:05:01 +02:00
|
|
|
impl->in.buffer_maxsize = MAX_BUFFER_SIZE;
|
|
|
|
|
impl->in.update = true;
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
if (impl->out.buffer_data == NULL || impl->in.buffer_data == NULL)
|
|
|
|
|
goto no_mem;
|
2016-12-22 16:50:01 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
return this;
|
2016-12-22 16:50:01 +01:00
|
|
|
|
2019-06-20 11:04:34 +02:00
|
|
|
no_mem:
|
2017-05-26 08:05:01 +02:00
|
|
|
free(impl->out.buffer_data);
|
|
|
|
|
free(impl->in.buffer_data);
|
|
|
|
|
free(impl);
|
|
|
|
|
return NULL;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Destroy a connection
|
|
|
|
|
*
|
|
|
|
|
* \param conn the connection to destroy
|
|
|
|
|
*
|
2017-07-12 18:04:00 +02:00
|
|
|
* \memberof pw_protocol_native_connection
|
2017-05-30 19:46:51 +02:00
|
|
|
*/
|
2017-07-12 18:04:00 +02:00
|
|
|
void pw_protocol_native_connection_destroy(struct pw_protocol_native_connection *conn)
|
2016-10-18 11:11:38 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_log_debug("connection %p: destroy", conn);
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2018-08-01 21:41:25 +02:00
|
|
|
spa_hook_list_call(&conn->listener_list, struct pw_protocol_native_connection_events, destroy, 0);
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
free(impl->out.buffer_data);
|
|
|
|
|
free(impl->in.buffer_data);
|
|
|
|
|
free(impl);
|
2016-10-18 11:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
static int prepare_packet(struct pw_protocol_native_connection *conn, struct buffer *buf)
|
|
|
|
|
{
|
|
|
|
|
uint8_t *data;
|
|
|
|
|
size_t size, len;
|
|
|
|
|
uint32_t *p;
|
|
|
|
|
|
|
|
|
|
data = buf->buffer_data + buf->offset;
|
|
|
|
|
size = buf->buffer_size - buf->offset;
|
|
|
|
|
|
|
|
|
|
if (size < HDR_SIZE)
|
|
|
|
|
return HDR_SIZE;
|
|
|
|
|
|
|
|
|
|
p = (uint32_t *) data;
|
|
|
|
|
data += HDR_SIZE;
|
|
|
|
|
size -= HDR_SIZE;
|
|
|
|
|
|
|
|
|
|
buf->msg.id = p[0];
|
|
|
|
|
buf->msg.opcode = p[1] >> 24;
|
|
|
|
|
len = p[1] & 0xffffff;
|
|
|
|
|
buf->msg.seq = p[2];
|
|
|
|
|
buf->msg.n_fds = p[3];
|
|
|
|
|
buf->msg.fds = &buf->fds[buf->fds_offset];
|
|
|
|
|
|
|
|
|
|
if (size < len)
|
|
|
|
|
return len;
|
|
|
|
|
|
|
|
|
|
buf->msg.size = len;
|
|
|
|
|
buf->msg.data = data;
|
|
|
|
|
|
|
|
|
|
buf->offset += HDR_SIZE + len;
|
|
|
|
|
buf->fds_offset += buf->msg.n_fds;
|
|
|
|
|
|
|
|
|
|
if (buf->offset >= buf->buffer_size)
|
|
|
|
|
clear_buffer(buf);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Move to the next packet in the connection
|
2016-10-17 18:29:05 +02:00
|
|
|
*
|
2017-05-30 19:46:51 +02:00
|
|
|
* \param conn the connection
|
|
|
|
|
* \param opcode addres of result opcode
|
|
|
|
|
* \param dest_id addres of result destination id
|
|
|
|
|
* \param dt pointer to packet data
|
|
|
|
|
* \param sz size of packet data
|
|
|
|
|
* \return true on success
|
2016-10-17 18:29:05 +02:00
|
|
|
*
|
2017-05-30 19:46:51 +02:00
|
|
|
* Get the next packet in \a conn and store the opcode and destination
|
|
|
|
|
* id as well as the packet data and size.
|
|
|
|
|
*
|
2017-07-12 18:04:00 +02:00
|
|
|
* \memberof pw_protocol_native_connection
|
2016-10-17 18:29:05 +02:00
|
|
|
*/
|
2019-03-19 16:15:20 +01:00
|
|
|
int
|
2017-07-12 18:04:00 +02:00
|
|
|
pw_protocol_native_connection_get_next(struct pw_protocol_native_connection *conn,
|
2019-03-19 16:15:20 +01:00
|
|
|
const struct pw_protocol_native_message **msg)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2019-03-19 16:15:20 +01:00
|
|
|
size_t len;
|
|
|
|
|
int res;
|
2017-05-26 08:05:01 +02:00
|
|
|
struct buffer *buf;
|
|
|
|
|
|
|
|
|
|
buf = &impl->in;
|
|
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
while (1) {
|
|
|
|
|
if ((len = prepare_packet(conn, buf)) == 0)
|
|
|
|
|
break;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2017-11-13 11:32:06 +01:00
|
|
|
if (connection_ensure_size(conn, buf, len) == NULL)
|
2019-06-19 16:22:22 +02:00
|
|
|
return -errno;
|
2019-03-19 16:15:20 +01:00
|
|
|
if ((res = refill_buffer(conn, buf)) < 0)
|
|
|
|
|
return res;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
2019-03-19 16:15:20 +01:00
|
|
|
*msg = &buf->msg;
|
|
|
|
|
return 1;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-12 18:04:00 +02:00
|
|
|
static inline void *begin_write(struct pw_protocol_native_connection *conn, uint32_t size)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2017-05-26 08:05:01 +02:00
|
|
|
uint32_t *p;
|
|
|
|
|
struct buffer *buf = &impl->out;
|
2019-02-25 12:29:57 +01:00
|
|
|
/* header and size for payload */
|
|
|
|
|
if ((p = connection_ensure_size(conn, buf, HDR_SIZE + size)) == NULL)
|
2017-11-13 11:32:06 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
return SPA_MEMBER(p, HDR_SIZE, void);
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 11:19:23 +02:00
|
|
|
static int builder_overflow(void *data, uint32_t size)
|
2017-07-11 12:24:03 +02:00
|
|
|
{
|
2019-05-15 11:19:23 +02:00
|
|
|
struct impl *impl = data;
|
2019-01-22 17:38:23 +01:00
|
|
|
struct spa_pod_builder *b = &impl->builder;
|
2017-07-11 12:24:03 +02:00
|
|
|
|
2019-01-22 17:38:23 +01:00
|
|
|
b->size = SPA_ROUND_UP_N(size, 4096);
|
|
|
|
|
if ((b->data = begin_write(&impl->this, b->size)) == NULL)
|
2019-06-19 16:22:22 +02:00
|
|
|
return -errno;
|
2019-01-22 17:38:23 +01:00
|
|
|
return 0;
|
2017-07-11 12:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-22 17:38:23 +01:00
|
|
|
static const struct spa_pod_builder_callbacks builder_callbacks = {
|
|
|
|
|
SPA_VERSION_POD_BUILDER_CALLBACKS,
|
|
|
|
|
.overflow = builder_overflow
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
struct spa_pod_builder *
|
2019-02-20 17:51:05 +01:00
|
|
|
pw_protocol_native_connection_begin(struct pw_protocol_native_connection *conn,
|
2019-03-19 16:15:20 +01:00
|
|
|
uint32_t id, uint8_t opcode,
|
|
|
|
|
struct pw_protocol_native_message **msg)
|
2017-07-11 12:24:03 +02:00
|
|
|
{
|
|
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2019-03-19 16:15:20 +01:00
|
|
|
struct buffer *buf = &impl->out;
|
|
|
|
|
|
|
|
|
|
buf->msg.id = id;
|
|
|
|
|
buf->msg.opcode = opcode;
|
2019-01-22 17:38:23 +01:00
|
|
|
impl->builder = SPA_POD_BUILDER_INIT(NULL, 0);
|
2019-05-15 11:19:23 +02:00
|
|
|
spa_pod_builder_set_callbacks(&impl->builder, &builder_callbacks, impl);
|
2019-03-19 16:15:20 +01:00
|
|
|
buf->msg.n_fds = 0;
|
|
|
|
|
buf->msg.fds = &buf->fds[buf->n_fds];
|
|
|
|
|
buf->msg.seq = buf->seq;
|
|
|
|
|
if (msg)
|
|
|
|
|
*msg = &buf->msg;
|
2017-07-11 12:24:03 +02:00
|
|
|
return &impl->builder;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 12:31:36 +01:00
|
|
|
int
|
2017-07-12 18:04:00 +02:00
|
|
|
pw_protocol_native_connection_end(struct pw_protocol_native_connection *conn,
|
|
|
|
|
struct spa_pod_builder *builder)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2017-11-13 12:30:48 +01:00
|
|
|
uint32_t *p, size = builder->state.offset;
|
2017-05-26 08:05:01 +02:00
|
|
|
struct buffer *buf = &impl->out;
|
2019-03-19 16:15:20 +01:00
|
|
|
int res;
|
2016-11-07 10:24:13 +01:00
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
if ((p = connection_ensure_size(conn, buf, HDR_SIZE + size)) == NULL)
|
2019-06-19 16:22:22 +02:00
|
|
|
return -errno;
|
2019-02-18 12:31:36 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
p[0] = buf->msg.id;
|
|
|
|
|
p[1] = (buf->msg.opcode << 24) | (size & 0xffffff);
|
|
|
|
|
p[2] = buf->msg.seq;
|
|
|
|
|
p[3] = buf->msg.n_fds;
|
2017-03-15 16:16:16 +01:00
|
|
|
|
2019-02-25 12:29:57 +01:00
|
|
|
buf->buffer_size += HDR_SIZE + size;
|
2019-03-19 16:15:20 +01:00
|
|
|
buf->n_fds += buf->msg.n_fds;
|
2017-03-23 12:38:00 +01:00
|
|
|
|
2017-05-29 10:28:19 +02:00
|
|
|
if (debug_messages) {
|
2019-03-19 16:15:20 +01:00
|
|
|
fprintf(stderr, ">>>>>>>>> out: %d %d %d\n", buf->msg.id, buf->msg.opcode, size);
|
2019-02-25 12:29:57 +01:00
|
|
|
spa_debug_pod(0, NULL, SPA_MEMBER(p, HDR_SIZE, struct spa_pod));
|
2017-05-29 10:28:19 +02:00
|
|
|
}
|
2019-02-18 12:31:36 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
buf->seq = (buf->seq + 1) & SPA_ASYNC_SEQ_MASK;
|
|
|
|
|
res = SPA_RESULT_RETURN_ASYNC(buf->msg.seq);
|
|
|
|
|
|
2018-05-17 17:26:09 +02:00
|
|
|
spa_hook_list_call(&conn->listener_list,
|
2018-08-01 21:41:25 +02:00
|
|
|
struct pw_protocol_native_connection_events, need_flush, 0);
|
2019-02-18 12:31:36 +01:00
|
|
|
|
2019-03-19 16:15:20 +01:00
|
|
|
return res;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Flush the connection object
|
|
|
|
|
*
|
|
|
|
|
* \param conn the connection object
|
2018-04-19 20:03:52 +02:00
|
|
|
* \return 0 on success < 0 error code on error
|
2017-05-30 19:46:51 +02:00
|
|
|
*
|
|
|
|
|
* Write the queued messages on the connection to the socket
|
|
|
|
|
*
|
2017-07-12 18:04:00 +02:00
|
|
|
* \memberof pw_protocol_native_connection
|
2017-05-30 19:46:51 +02:00
|
|
|
*/
|
2018-04-19 20:03:52 +02:00
|
|
|
int pw_protocol_native_connection_flush(struct pw_protocol_native_connection *conn)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2018-09-11 12:01:19 +02:00
|
|
|
ssize_t sent, outsize;
|
2017-05-26 08:05:01 +02:00
|
|
|
struct msghdr msg = { 0 };
|
|
|
|
|
struct iovec iov[1];
|
|
|
|
|
struct cmsghdr *cmsg;
|
2018-09-11 12:01:19 +02:00
|
|
|
char cmsgbuf[CMSG_SPACE(MAX_FDS_MSG * sizeof(int))];
|
|
|
|
|
int *cm, res = 0, *fds;
|
|
|
|
|
uint32_t i, fds_len, n_fds, outfds;
|
2017-05-26 08:05:01 +02:00
|
|
|
struct buffer *buf;
|
2018-09-11 12:01:19 +02:00
|
|
|
void *data;
|
|
|
|
|
size_t size;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
|
|
|
|
buf = &impl->out;
|
2018-09-11 12:01:19 +02:00
|
|
|
data = buf->buffer_data;
|
|
|
|
|
size = buf->buffer_size;
|
|
|
|
|
fds = buf->fds;
|
|
|
|
|
n_fds = buf->n_fds;
|
|
|
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
|
if (n_fds > MAX_FDS_MSG) {
|
|
|
|
|
outfds = MAX_FDS_MSG;
|
|
|
|
|
outsize = SPA_MIN(sizeof(uint32_t), size);
|
|
|
|
|
} else {
|
|
|
|
|
outfds = n_fds;
|
|
|
|
|
outsize = size;
|
|
|
|
|
}
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2018-09-11 12:01:19 +02:00
|
|
|
fds_len = outfds * sizeof(int);
|
|
|
|
|
|
|
|
|
|
iov[0].iov_base = data;
|
|
|
|
|
iov[0].iov_len = outsize;
|
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
|
|
if (outfds > 0) {
|
|
|
|
|
msg.msg_control = cmsgbuf;
|
|
|
|
|
msg.msg_controllen = CMSG_SPACE(fds_len);
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(fds_len);
|
|
|
|
|
cm = (int *) CMSG_DATA(cmsg);
|
|
|
|
|
for (i = 0; i < outfds; i++)
|
|
|
|
|
cm[i] = fds[i] > 0 ? fds[i] : -fds[i];
|
|
|
|
|
msg.msg_controllen = cmsg->cmsg_len;
|
|
|
|
|
} else {
|
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
|
}
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2018-09-11 12:01:19 +02:00
|
|
|
while (true) {
|
|
|
|
|
sent = sendmsg(conn->fd, &msg, MSG_NOSIGNAL | MSG_DONTWAIT);
|
|
|
|
|
if (sent < 0) {
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
else
|
|
|
|
|
goto send_error;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
2018-09-11 12:01:19 +02:00
|
|
|
pw_log_trace("connection %p: %d written %zd bytes and %u fds", conn, conn->fd, sent,
|
|
|
|
|
outfds);
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2018-09-11 12:01:19 +02:00
|
|
|
size -= sent;
|
2019-01-08 11:53:36 +01:00
|
|
|
data = SPA_MEMBER(data, sent, void);
|
2018-09-11 12:01:19 +02:00
|
|
|
n_fds -= outfds;
|
|
|
|
|
fds += outfds;
|
|
|
|
|
}
|
|
|
|
|
buf->buffer_size = size;
|
|
|
|
|
buf->n_fds = n_fds;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2018-04-19 20:03:52 +02:00
|
|
|
return 0;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
|
|
|
|
/* ERRORS */
|
2019-06-20 11:04:34 +02:00
|
|
|
send_error:
|
2018-04-19 20:03:52 +02:00
|
|
|
res = -errno;
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_log_error("could not sendmsg: %s", strerror(errno));
|
2018-04-19 20:03:52 +02:00
|
|
|
return res;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Clear the connection object
|
|
|
|
|
*
|
|
|
|
|
* \param conn the connection object
|
2018-04-19 20:03:52 +02:00
|
|
|
* \return 0 on success
|
2017-05-30 19:46:51 +02:00
|
|
|
*
|
|
|
|
|
* Remove all queued messages from \a conn
|
|
|
|
|
*
|
2017-07-12 18:04:00 +02:00
|
|
|
* \memberof pw_protocol_native_connection
|
2017-05-30 19:46:51 +02:00
|
|
|
*/
|
2018-04-19 20:03:52 +02:00
|
|
|
int pw_protocol_native_connection_clear(struct pw_protocol_native_connection *conn)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(conn, struct impl, this);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
clear_buffer(&impl->out);
|
|
|
|
|
clear_buffer(&impl->in);
|
|
|
|
|
impl->in.update = true;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2018-04-19 20:03:52 +02:00
|
|
|
return 0;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|