2019-07-27 15:46:04 +02:00
|
|
|
#include <sys/select.h>
|
2020-07-03 22:27:56 +02:00
|
|
|
#include <sys/time.h>
|
2018-10-28 18:12:17 +01:00
|
|
|
#include <unistd.h>
|
2016-12-17 18:00:04 +01:00
|
|
|
|
2016-05-30 01:54:19 +02:00
|
|
|
#include <stdio.h>
|
2020-01-01 12:11:34 +01:00
|
|
|
#include <errno.h> // error numbers
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-07-27 15:46:04 +02:00
|
|
|
#include "ipc.h"
|
|
|
|
#include "utils.h"
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2019-07-27 15:46:04 +02:00
|
|
|
// print structures
|
|
|
|
#include "message.h"
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-06-29 16:49:54 +02:00
|
|
|
struct ipc_error ipc_server_init (struct ipc_ctx *ctx, const char *sname)
|
2020-06-28 15:40:02 +02:00
|
|
|
{
|
|
|
|
T_R ((sname == NULL), IPC_ERROR_SERVER_INIT__NO_SERVER_NAME_PARAM);
|
2020-06-28 01:13:48 +02:00
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
// Declaration and instanciation of the new connection (ipc_connection_info + pollfd).
|
2020-06-28 15:40:02 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_connection_info, srv);
|
|
|
|
srv.type = IPC_CONNECTION_TYPE_SERVER;
|
|
|
|
SECURE_DECLARATION(struct pollfd, pollfd);
|
|
|
|
pollfd.events = POLLIN;
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
// Get the service path.
|
|
|
|
SECURE_BUFFER_DECLARATION (char, buf, PATH_MAX);
|
|
|
|
TEST_IPC_RR (service_path (buf, sname), "cannot get server path");
|
|
|
|
size_t s = strlen (buf);
|
|
|
|
if (s > PATH_MAX)
|
|
|
|
s = PATH_MAX;
|
2020-06-28 15:40:02 +02:00
|
|
|
SECURE_BUFFER_HEAP_ALLOCATION_R (srv.spath, s + 1,, IPC_ERROR_SERVER_INIT__MALLOC);
|
|
|
|
memcpy (srv.spath, buf, s);
|
|
|
|
srv.spath[s] = '\0'; // to be sure
|
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
// Socket initialisation for the service.
|
2020-06-28 15:40:02 +02:00
|
|
|
TEST_IPC_RETURN_ON_ERROR (usock_init (&pollfd.fd, srv.spath));
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-06-28 01:13:48 +02:00
|
|
|
// Add the server to the listened file descriptors.
|
2020-06-28 17:22:58 +02:00
|
|
|
// ipc_add allocate memory then copy the data of srv and pollfd in ctx.
|
2020-06-29 16:49:54 +02:00
|
|
|
TEST_IPC_RR (ipc_add (ctx, &srv, &pollfd), "cannot add the server in the context");
|
2020-06-28 01:13:48 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2019-07-27 15:46:04 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
// when ipcd is not working properly (or do not retrieve the service): srv->fd = 0
|
|
|
|
struct ipc_error ipc_contact_ipcd (int *pfd, const char *sname)
|
2019-07-27 15:46:04 +02:00
|
|
|
{
|
2020-07-01 14:24:45 +02:00
|
|
|
T_R ((pfd == NULL), IPC_ERROR_CONTACT_IPCD__NO_FD_PARAM);
|
|
|
|
T_R ((sname == NULL), IPC_ERROR_CONTACT_IPCD__NO_SERVICE_NAME_PARAM);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
char *ipcd_var = getenv ("IPC_NETWORK");
|
|
|
|
if (ipcd_var == NULL) {
|
2020-06-28 15:40:02 +02:00
|
|
|
*pfd = 0;
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2018-10-08 15:18:56 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
// TODO: is there another, more interesting way to do this?
|
|
|
|
// currently, IPC_NETWORK is shared with the network service
|
|
|
|
// in order to route requests over any chosen protocol stack
|
|
|
|
// ex: IPC_NETWORK="audio tor://some.example.com/audio ;pong tls://pong.example.com/pong"
|
2019-07-27 15:46:04 +02:00
|
|
|
|
|
|
|
SECURE_BUFFER_DECLARATION (char, columnthensname, BUFSIZ);
|
|
|
|
columnthensname[0] = ';';
|
2020-01-01 12:11:34 +01:00
|
|
|
memcpy (columnthensname + 1, sname, strlen (sname));
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
if (strncmp (ipcd_var, sname, strlen (sname)) != 0 && strstr (ipcd_var, columnthensname) == NULL) {
|
2020-06-28 18:05:28 +02:00
|
|
|
*pfd = 0;
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2018-10-08 15:18:56 +02:00
|
|
|
}
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-06-28 01:13:48 +02:00
|
|
|
// Get the service path.
|
2020-01-01 12:11:34 +01:00
|
|
|
SECURE_BUFFER_DECLARATION (char, buf, PATH_MAX);
|
2020-06-28 01:13:48 +02:00
|
|
|
TEST_IPC_RR (service_path (buf, "network"), "cannot get network service path");
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
int ipcd_fd = 0;
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
TEST_IPC_RETURN_ON_ERROR (usock_connect (&ipcd_fd, buf));
|
2019-07-27 15:46:04 +02:00
|
|
|
|
|
|
|
SECURE_DECLARATION (struct ipc_message, msg);
|
|
|
|
msg.type = MSG_TYPE_NETWORK_LOOKUP;
|
|
|
|
msg.user_type = MSG_TYPE_NETWORK_LOOKUP;
|
2016-12-19 19:20:27 +01:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
SECURE_BUFFER_DECLARATION (char, content, BUFSIZ);
|
2020-07-01 14:24:45 +02:00
|
|
|
snprintf (content, BUFSIZ, "%s;%s", sname, ipcd_var);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
|
|
|
msg.length = strlen (content);
|
|
|
|
msg.payload = content;
|
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
TEST_IPC_RR (ipc_write_fd (ipcd_fd, &msg), "cannot send a message to networkd");
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
struct ipc_error ret = ipc_receive_fd (ipcd_fd, pfd);
|
2020-01-01 12:11:34 +01:00
|
|
|
if (ret.error_code == IPC_ERROR_NONE) {
|
2020-07-01 14:24:45 +02:00
|
|
|
usock_close (ipcd_fd);
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2019-07-27 15:46:04 +02:00
|
|
|
return ret;
|
2016-12-19 18:16:38 +01:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
// Create context, contact ipcd, connects to the service.
|
2020-06-29 16:49:54 +02:00
|
|
|
struct ipc_error ipc_connection (struct ipc_ctx *ctx, const char *sname)
|
2016-06-10 01:21:04 +02:00
|
|
|
{
|
2020-06-27 19:16:07 +02:00
|
|
|
T_R ((ctx == NULL), IPC_ERROR_CONNECTION__NO_CTX);
|
2020-01-01 12:11:34 +01:00
|
|
|
T_R ((sname == NULL), IPC_ERROR_CONNECTION__NO_SERVICE_NAME);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-06-28 15:40:02 +02:00
|
|
|
SECURE_DECLARATION(struct ipc_connection_info, srv);
|
|
|
|
srv.type = IPC_CONNECTION_TYPE_IPC; // Data received on the socket = messages, not new clients.
|
|
|
|
SECURE_DECLARATION(struct pollfd, pollfd);
|
|
|
|
pollfd.events = POLLIN;
|
2020-06-28 01:13:48 +02:00
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
TEST_IPC_P (ipc_contact_ipcd (&pollfd.fd, sname), "error during networkd connection");
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-07-01 14:24:45 +02:00
|
|
|
// if ipcd did not initiate the connection
|
2020-06-28 15:40:02 +02:00
|
|
|
if (pollfd.fd <= 0) {
|
2020-01-01 12:11:34 +01:00
|
|
|
// gets the service path
|
|
|
|
SECURE_BUFFER_DECLARATION (char, buf, PATH_MAX);
|
2020-06-27 19:16:07 +02:00
|
|
|
TEST_IPC_RR (service_path (buf, sname), "cannot get server path");
|
2020-06-28 15:40:02 +02:00
|
|
|
TEST_IPC_RETURN_ON_ERROR (usock_connect (&pollfd.fd, buf));
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
2016-10-28 13:58:04 +02:00
|
|
|
|
2020-06-28 15:40:02 +02:00
|
|
|
// Add the server to the listened file descriptors.
|
2020-06-29 16:49:54 +02:00
|
|
|
TEST_IPC_RR (ipc_add (ctx, &srv, &pollfd), "cannot add the server in the context");
|
2020-06-28 15:40:02 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2016-06-10 01:21:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
struct ipc_error ipc_close_all (struct ipc_ctx *ctx)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2020-06-28 17:22:58 +02:00
|
|
|
for (size_t i = 0 ; i < ctx->size ; i++) {
|
|
|
|
TEST_IPC_P (ipc_close (ctx, i), "cannot close a connection in handle_message");
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
2020-06-28 17:22:58 +02:00
|
|
|
|
|
|
|
IPC_RETURN_NO_ERROR;
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
struct ipc_error ipc_close (struct ipc_ctx *ctx, uint32_t index)
|
2019-06-03 21:25:59 +02:00
|
|
|
{
|
2020-06-28 17:22:58 +02:00
|
|
|
struct ipc_error ret = usock_close (ctx->pollfd[index].fd);
|
|
|
|
|
2020-06-28 18:05:28 +02:00
|
|
|
// TODO: verify that the close was OK??
|
|
|
|
if (ctx->cinfos[index].type == IPC_CONNECTION_TYPE_SERVER) {
|
|
|
|
ret = usock_remove (ctx->cinfos[index].spath);
|
|
|
|
if (ctx->cinfos[index].spath != NULL) {
|
|
|
|
free (ctx->cinfos[index].spath);
|
|
|
|
ctx->cinfos[index].spath = NULL;
|
2020-06-28 17:22:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
// New connection from a client.
|
|
|
|
struct ipc_error ipc_accept_add (struct ipc_event *event, struct ipc_ctx *ctx, uint32_t index)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2020-06-28 17:22:58 +02:00
|
|
|
T_R ((ctx == NULL), IPC_ERROR_HANDLE_NEW_CONNECTION__NO_CINFOS_PARAM);
|
|
|
|
T_R ((index >= ctx->size), IPC_ERROR_HANDLE_NEW_CONNECTION__INCONSISTENT_INDEX);
|
|
|
|
|
|
|
|
// Memory reallocation.
|
|
|
|
ipc_ctx_new_alloc (ctx);
|
2016-12-20 23:36:00 +01:00
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
int server_fd = ctx->pollfd[index].fd;
|
|
|
|
int *client_fd = &ctx->pollfd[ctx->size -1].fd;
|
2016-12-20 23:36:00 +01:00
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
TEST_IPC_RR (usock_accept (server_fd, client_fd), "cannot accept IPC connection");
|
|
|
|
ctx->pollfd[ctx->size -1].events = POLLIN; // Tell to poll(2) to watch for incoming data from this fd.
|
|
|
|
ctx->cinfos[ctx->size -1].type = IPC_CONNECTION_TYPE_IPC;
|
|
|
|
|
2020-06-28 18:05:28 +02:00
|
|
|
// Set the event structure.
|
|
|
|
uint32_t client_index = ctx->size - 1;
|
2020-06-29 00:53:40 +02:00
|
|
|
IPC_EVENT_SET (event, IPC_EVENT_TYPE_CONNECTION, client_index, *client_fd, NULL);
|
2020-06-28 18:05:28 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2019-07-27 15:46:04 +02:00
|
|
|
}
|
2017-01-19 22:07:52 +01:00
|
|
|
|
2019-07-27 15:46:04 +02:00
|
|
|
// receive then format in an ipc_message structure
|
2020-06-28 17:22:58 +02:00
|
|
|
struct ipc_error ipc_read (const struct ipc_ctx *ctx, uint32_t index, struct ipc_message *m)
|
2019-07-27 15:46:04 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
T_R ((m == NULL), IPC_ERROR_READ__NO_MESSAGE_PARAM);
|
2017-01-19 22:07:52 +01:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = IPC_MAX_MESSAGE_SIZE;
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
// On error or closed recipient, the buffer already freed.
|
|
|
|
TEST_IPC_RETURN_ON_ERROR (usock_recv (ctx->pollfd[index].fd, &buf, &msize));
|
2020-01-01 12:11:34 +01:00
|
|
|
TEST_IPC_RETURN_ON_ERROR_FREE (ipc_message_format_read (m, buf, msize), buf);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
|
|
|
free (buf);
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR; // propagates ipc_message_format return
|
2017-01-19 22:07:52 +01:00
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
struct ipc_error ipc_write_fd (int fd, const struct ipc_message *m)
|
2018-10-28 17:09:35 +01:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
|
|
|
ipc_message_format_write (m, &buf, &msize);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
size_t nbytes_sent = 0;
|
|
|
|
TEST_IPC_RETURN_ON_ERROR_FREE (usock_send (fd, buf, msize, &nbytes_sent), buf);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
// what was sent != what should have been sent
|
2020-06-30 12:59:05 +02:00
|
|
|
T_R ((nbytes_sent != msize), IPC_ERROR_WRITE_FD__NOT_ENOUGH_DATA);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2018-10-28 17:09:35 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 16:22:14 +02:00
|
|
|
// Put the message in the list of messages to send.
|
|
|
|
struct ipc_error ipc_write (struct ipc_ctx *ctx, const struct ipc_message *m)
|
2016-12-22 21:48:35 +01:00
|
|
|
{
|
2020-06-30 12:59:05 +02:00
|
|
|
int found = 0;
|
|
|
|
for (size_t i = 0; i < ctx->size; i++) {
|
|
|
|
if (ctx->pollfd[i].fd == m->fd) {
|
|
|
|
ctx->pollfd[i].events |= POLLOUT;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T_R ((found == 0), IPC_ERROR_WRITE__FD_NOT_FOUND);
|
|
|
|
|
2020-06-30 13:49:21 +02:00
|
|
|
// Performs a deep copy of the structure.
|
2020-06-29 16:22:14 +02:00
|
|
|
return ipc_messages_add (&ctx->tx, m);
|
2018-10-05 22:33:43 +02:00
|
|
|
}
|
2018-10-10 23:08:58 +02:00
|
|
|
|
2020-06-28 17:22:58 +02:00
|
|
|
/**
|
|
|
|
* Allocate memory then add a new connection to the context.
|
|
|
|
*/
|
|
|
|
struct ipc_error ipc_add ( struct ipc_ctx *ctx, struct ipc_connection_info *p, struct pollfd *pollfd)
|
2019-07-27 15:46:04 +02:00
|
|
|
{
|
2020-06-30 12:59:05 +02:00
|
|
|
T_R ((ctx == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENTS);
|
|
|
|
T_R ((p == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENT);
|
|
|
|
T_R ((pollfd == NULL), IPC_ERROR_ADD__NO_PARAM_POLLFD);
|
|
|
|
|
2020-06-28 15:40:02 +02:00
|
|
|
// Memory reallocation.
|
|
|
|
ipc_ctx_new_alloc (ctx);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-07-01 13:26:22 +02:00
|
|
|
T_R ((ctx->size <= 0), IPC_ERROR_ADD__NOT_ENOUGH_MEMORY);
|
2020-07-01 12:47:04 +02:00
|
|
|
|
2020-06-28 18:05:28 +02:00
|
|
|
ctx->cinfos[ctx->size - 1] = *p;
|
|
|
|
ctx->pollfd[ctx->size - 1] = *pollfd;
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2019-07-27 15:46:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 18:05:28 +02:00
|
|
|
struct ipc_error ipc_del (struct ipc_ctx *ctx, uint32_t index)
|
2020-06-28 17:22:58 +02:00
|
|
|
{
|
2020-06-28 18:05:28 +02:00
|
|
|
T_R ((ctx == NULL), IPC_ERROR_DEL__NO_CLIENTS_PARAM);
|
|
|
|
T_R ((ctx->cinfos == NULL || ctx->pollfd == NULL), IPC_ERROR_DEL__EMPTY_LIST);
|
2020-07-01 12:47:04 +02:00
|
|
|
T_R ((index >= ctx->size), IPC_ERROR_DEL__CANNOT_FIND_CLIENT);
|
2020-06-28 18:05:28 +02:00
|
|
|
|
2020-07-01 12:47:04 +02:00
|
|
|
if (ctx->cinfos[index].spath != NULL) {
|
2020-06-28 18:05:28 +02:00
|
|
|
free (ctx->cinfos[index].spath);
|
2020-07-01 12:47:04 +02:00
|
|
|
ctx->cinfos[index].spath = NULL;
|
|
|
|
}
|
2020-06-28 18:05:28 +02:00
|
|
|
|
|
|
|
ctx->size--;
|
|
|
|
|
|
|
|
if (ctx->size == 0) {
|
|
|
|
// free ctx->cinfos and ctx->pollfd
|
2020-07-01 09:53:24 +02:00
|
|
|
ipc_ctx_free (ctx);
|
2020-06-28 18:05:28 +02:00
|
|
|
IPC_RETURN_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last element in the array replaces the removed one.
|
|
|
|
ctx->cinfos[index] = ctx->cinfos[ctx->size];
|
|
|
|
ctx->pollfd[index] = ctx->pollfd[ctx->size];
|
|
|
|
|
|
|
|
// Reallocation of the arrays. TODO: should be optimised someday.
|
|
|
|
ctx->cinfos = realloc (ctx->cinfos, sizeof (struct ipc_connection_info) * ctx->size);
|
|
|
|
ctx->pollfd = realloc (ctx->pollfd, sizeof (struct pollfd ) * ctx->size);
|
|
|
|
|
|
|
|
if (ctx->cinfos == NULL || ctx->pollfd == NULL) {
|
|
|
|
IPC_RETURN_ERROR (IPC_ERROR_DEL__EMPTIED_LIST);
|
2020-06-28 17:22:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 18:05:28 +02:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2020-06-28 17:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// add an arbitrary file descriptor to read
|
|
|
|
struct ipc_error ipc_add_fd (struct ipc_ctx *ctx, int fd)
|
|
|
|
{
|
|
|
|
T_R ((ctx == NULL), IPC_ERROR_ADD_FD__NO_PARAM_CINFOS);
|
|
|
|
|
|
|
|
SECURE_DECLARATION (struct ipc_connection_info, cinfo);
|
|
|
|
cinfo.type = IPC_CONNECTION_TYPE_EXTERNAL;
|
|
|
|
|
|
|
|
SECURE_DECLARATION (struct pollfd, pollfd);
|
|
|
|
pollfd.fd = fd;
|
|
|
|
pollfd.events = POLLIN;
|
|
|
|
|
|
|
|
return ipc_add (ctx, &cinfo, &pollfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove a connection from its file descriptor
|
|
|
|
struct ipc_error ipc_del_fd (struct ipc_ctx *ctx, int fd)
|
|
|
|
{
|
2020-07-04 19:03:21 +02:00
|
|
|
T_R ((ctx == NULL), IPC_ERROR_DEL_FD__NO_PARAM_CINFOS);
|
|
|
|
T_R ((ctx->cinfos == NULL || ctx->pollfd == NULL), IPC_ERROR_DEL_FD__EMPTY_LIST);
|
2020-06-28 17:22:58 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < ctx->size; i++) {
|
|
|
|
if (ctx->pollfd[i].fd == fd) {
|
2020-06-28 18:05:28 +02:00
|
|
|
return ipc_del (ctx, i);
|
2020-06-28 17:22:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC_RETURN_ERROR (IPC_ERROR_DEL_FD__CANNOT_FIND_CLIENT);
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:53:40 +02:00
|
|
|
|
|
|
|
struct ipc_error handle_writing_message (struct ipc_event *event, struct ipc_ctx *ctx, uint32_t index)
|
|
|
|
{
|
2020-06-29 16:22:14 +02:00
|
|
|
int txfd = ctx->pollfd[index].fd;
|
|
|
|
int mfd;
|
|
|
|
struct ipc_message *m;
|
2020-06-29 00:53:40 +02:00
|
|
|
for (size_t i = 0; ctx->tx.size ; i++) {
|
2020-06-29 16:22:14 +02:00
|
|
|
m = &ctx->tx.messages[i];
|
|
|
|
mfd = m->fd;
|
|
|
|
if (txfd == mfd) {
|
2020-06-30 13:49:21 +02:00
|
|
|
TEST_IPC_RR (ipc_write_fd (txfd, m), "cannot send a message to the client");
|
2020-06-29 16:22:14 +02:00
|
|
|
|
2020-07-01 13:26:22 +02:00
|
|
|
// Freeing the message structure.
|
2020-06-30 13:49:21 +02:00
|
|
|
ipc_message_empty (m);
|
2020-07-01 13:26:22 +02:00
|
|
|
// Removing the message from the context.
|
2020-06-29 16:22:14 +02:00
|
|
|
ipc_messages_del (&ctx->tx, i); // remove the message indexed by i
|
2020-07-01 12:47:04 +02:00
|
|
|
|
|
|
|
break; // The message has been sent
|
2020-06-29 16:22:14 +02:00
|
|
|
}
|
2020-06-29 00:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IPC_EVENT_SET (event, IPC_EVENT_TYPE_TX, index, ctx->pollfd[index].fd, NULL);
|
|
|
|
IPC_RETURN_NO_ERROR;
|
|
|
|
}
|
2020-06-28 17:22:58 +02:00
|
|
|
|
2020-06-29 00:53:40 +02:00
|
|
|
struct ipc_error handle_new_message (struct ipc_event *event, struct ipc_ctx *ctx, int index)
|
2018-10-10 23:08:58 +02:00
|
|
|
{
|
2020-07-06 08:42:05 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_error, ret);
|
2020-06-29 00:53:40 +02:00
|
|
|
|
2020-07-06 08:42:05 +02:00
|
|
|
{ /* First test: should the message be switched? */
|
|
|
|
ret = fd_switching (event, ctx, index);
|
|
|
|
if (ret.error_code != IPC_ERROR_FD_SWITCHING__NO_FD_RECORD) {
|
|
|
|
return ret;
|
2019-07-27 15:46:04 +02:00
|
|
|
}
|
2018-10-10 23:08:58 +02:00
|
|
|
}
|
2020-06-29 00:53:40 +02:00
|
|
|
|
2020-07-06 08:42:05 +02:00
|
|
|
// No treatment of the socket if external socket: the libipc user should handle it himself.
|
2020-06-29 00:53:40 +02:00
|
|
|
if (ctx->cinfos[index].type == IPC_CONNECTION_TYPE_EXTERNAL) {
|
|
|
|
IPC_EVENT_SET (event, IPC_EVENT_TYPE_EXTRA_SOCKET, index, ctx->pollfd[index].fd, NULL);
|
2020-01-01 12:11:34 +01:00
|
|
|
IPC_RETURN_NO_ERROR;
|
2018-10-10 23:08:58 +02:00
|
|
|
}
|
2020-06-29 00:53:40 +02:00
|
|
|
|
2020-07-06 08:42:05 +02:00
|
|
|
// Listen to what they have to say (disconnection or message)
|
|
|
|
// then add a client to `event`, the ipc_event structure.
|
2020-01-01 12:11:34 +01:00
|
|
|
struct ipc_message *m = NULL;
|
|
|
|
SECURE_BUFFER_HEAP_ALLOCATION_R (m, sizeof (struct ipc_message),, IPC_ERROR_HANDLE_MESSAGE__NOT_ENOUGH_MEMORY);
|
|
|
|
|
|
|
|
// current talking client
|
2020-06-29 00:53:40 +02:00
|
|
|
ret = ipc_read (ctx, index, m);
|
2020-01-01 12:11:34 +01:00
|
|
|
if (ret.error_code != IPC_ERROR_NONE && ret.error_code != IPC_ERROR_CLOSED_RECIPIENT) {
|
|
|
|
struct ipc_error rvalue = ret; // store the final return value
|
|
|
|
ipc_message_empty (m);
|
|
|
|
free (m);
|
|
|
|
|
|
|
|
// if there is a problem, just remove the client
|
2020-06-29 00:53:40 +02:00
|
|
|
TEST_IPC_P (ipc_close (ctx, index), "cannot close a connection in handle_message");
|
|
|
|
TEST_IPC_P (ipc_del (ctx, index), "cannot delete a connection in handle_message");
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-06-29 00:53:40 +02:00
|
|
|
IPC_EVENT_SET (event, IPC_EVENT_TYPE_ERROR, index, ctx->pollfd[index].fd, NULL);
|
2020-01-01 12:11:34 +01:00
|
|
|
return rvalue;
|
|
|
|
}
|
2020-06-28 17:22:58 +02:00
|
|
|
|
2020-06-28 15:40:02 +02:00
|
|
|
// disconnection: close the client then delete it from ctx
|
2020-01-01 12:11:34 +01:00
|
|
|
if (ret.error_code == IPC_ERROR_CLOSED_RECIPIENT) {
|
2020-06-30 13:49:21 +02:00
|
|
|
|
|
|
|
IPC_EVENT_SET (event, IPC_EVENT_TYPE_DISCONNECTION, index, ctx->pollfd[index].fd, NULL);
|
|
|
|
|
2020-06-29 00:53:40 +02:00
|
|
|
TEST_IPC_P (ipc_close (ctx, index), "cannot close a connection on closed recipient in handle_message");
|
|
|
|
TEST_IPC_P (ipc_del (ctx, index), "cannot delete a connection on closed recipient in handle_message");
|
2018-10-28 17:09:35 +01:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
ipc_message_empty (m);
|
|
|
|
free (m);
|
2019-07-27 15:46:04 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// warning: do not forget to free the ipc_client structure
|
|
|
|
IPC_RETURN_NO_ERROR;
|
|
|
|
}
|
2018-10-28 17:09:35 +01:00
|
|
|
|
2020-06-30 12:59:05 +02:00
|
|
|
// The message carries the fd it was received on.
|
|
|
|
m->fd = ctx->pollfd[index].fd;
|
2020-06-29 00:53:40 +02:00
|
|
|
IPC_EVENT_SET (event, IPC_EVENT_TYPE_MESSAGE, index, ctx->pollfd[index].fd, m);
|
2020-06-28 15:40:02 +02:00
|
|
|
IPC_RETURN_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:53:40 +02:00
|
|
|
/* timer is in ms */
|
2020-07-06 08:42:05 +02:00
|
|
|
struct ipc_error ipc_wait_event (struct ipc_ctx *ctx, struct ipc_event *event, int *timer)
|
2020-06-27 04:45:43 +02:00
|
|
|
{
|
2020-06-28 15:40:02 +02:00
|
|
|
T_R ((ctx == NULL), IPC_ERROR_WAIT_EVENT__NO_CLIENTS_PARAM);
|
2020-06-27 04:45:43 +02:00
|
|
|
T_R ((event == NULL), IPC_ERROR_WAIT_EVENT__NO_EVENT_PARAM);
|
|
|
|
|
|
|
|
IPC_EVENT_CLEAN (event);
|
|
|
|
|
2020-06-29 00:53:40 +02:00
|
|
|
int32_t n;
|
2020-06-27 04:45:43 +02:00
|
|
|
|
2020-06-30 12:59:05 +02:00
|
|
|
for (size_t i = 0; i < ctx->tx.size; i++) {
|
|
|
|
for (size_t y = 0; y < ctx->size; y++) {
|
|
|
|
if (ctx->pollfd[y].fd == ctx->tx.messages[i].fd) {
|
|
|
|
ctx->pollfd[y].events |= POLLOUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 22:27:56 +02:00
|
|
|
struct timeval tv_1;
|
|
|
|
memset (&tv_1, 0, sizeof(struct timeval));
|
|
|
|
|
|
|
|
struct timeval tv_2;
|
|
|
|
memset (&tv_2, 0, sizeof(struct timeval));
|
|
|
|
|
|
|
|
gettimeofday(&tv_1, NULL);
|
|
|
|
|
2020-06-29 00:53:40 +02:00
|
|
|
if ((n = poll(ctx->pollfd, ctx->size, *timer)) < 0) {
|
2020-07-01 13:26:22 +02:00
|
|
|
IPC_RETURN_ERROR (IPC_ERROR_WAIT_EVENT__POLL);
|
2020-06-27 04:45:43 +02:00
|
|
|
}
|