WIP
parent
85b54b0db6
commit
91793c45cc
|
@ -58,7 +58,7 @@ void interactive (char *env[])
|
|||
|
||||
SECURE_DECLARATION (struct ipc_error, ret);
|
||||
SECURE_DECLARATION (struct ipc_event, event);
|
||||
SECURE_DECLARATION (struct ipc_connection_infos, services);
|
||||
SECURE_DECLARATION (struct ipc_ctx, services);
|
||||
|
||||
ipc_add (&services, srv);
|
||||
ipc_add_fd (&services, 0); // add STDIN
|
||||
|
|
|
@ -12,12 +12,28 @@
|
|||
fprintf(stderr, "error while %s: %s\n", msg, ret.error_message);\
|
||||
}
|
||||
|
||||
/**
|
||||
* ipc_ctx:
|
||||
* cinfos: array of ipc_connection_info
|
||||
* pollfd: array of `pollfd` structure
|
||||
* Both arrays share the same indices.
|
||||
*/
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
* Overview of the main loop:
|
||||
* 1. "clients" pointer declaration (struct ipc_ctx).
|
||||
* 1. ipc_init (&clients)
|
||||
* 1. ipc_server_init (env, srv, SERVICE_NAME)
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
int cpt = 0;
|
||||
|
||||
int verbosity = 1;
|
||||
|
||||
struct ipc_connection_info *srv = NULL;
|
||||
struct ipc_connection_infos *clients = NULL;
|
||||
struct ipc_ctx *clients = NULL;
|
||||
|
||||
void main_loop ()
|
||||
{
|
||||
|
@ -25,8 +41,11 @@ void main_loop ()
|
|||
double timer = base_timer;
|
||||
SECURE_DECLARATION (struct ipc_error, ret);
|
||||
|
||||
clients = malloc (sizeof (struct ipc_connection_infos));
|
||||
memset (clients, 0, sizeof (struct ipc_connection_infos));
|
||||
/** TODO: should return something */
|
||||
ipc_init (&clients);
|
||||
|
||||
// clients = malloc (sizeof (struct ipc_ctx));
|
||||
// memset (clients, 0, sizeof (struct ipc_ctx));
|
||||
|
||||
SECURE_DECLARATION (struct ipc_event, event);
|
||||
event.type = IPC_EVENT_TYPE_NOT_SET;
|
||||
|
|
|
@ -50,7 +50,7 @@ void interactive (char *env[])
|
|||
TEST_IPC_Q (ipc_connection (env, srv, SERVICE_NAME), EXIT_FAILURE);
|
||||
|
||||
SECURE_DECLARATION (struct ipc_event, event);
|
||||
SECURE_DECLARATION (struct ipc_connection_infos, services);
|
||||
SECURE_DECLARATION (struct ipc_ctx, services);
|
||||
|
||||
ipc_add (&services, srv);
|
||||
ipc_add_fd (&services, 0); // add STDIN
|
||||
|
|
|
@ -155,7 +155,7 @@ void main_loop (int argc, char **argv, char **env)
|
|||
return;
|
||||
}
|
||||
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ctx->clients, sizeof (struct ipc_connection_infos),, EXIT_FAILURE);
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ctx->clients, sizeof (struct ipc_ctx),, EXIT_FAILURE);
|
||||
SECURE_DECLARATION (struct ipc_event, event);
|
||||
|
||||
ipc_add_fd (ctx->clients, serverfd);
|
||||
|
|
|
@ -40,7 +40,7 @@ void interactive (char *env[])
|
|||
TEST_IPC_Q (ipc_connection (env, srv, service_name), EXIT_FAILURE);
|
||||
|
||||
SECURE_DECLARATION (struct ipc_event, event);
|
||||
SECURE_DECLARATION (struct ipc_connection_infos, services);
|
||||
SECURE_DECLARATION (struct ipc_ctx, services);
|
||||
|
||||
ipc_add (&services, srv);
|
||||
ipc_add_fd (&services, 0); // add STDIN
|
||||
|
|
|
@ -34,17 +34,17 @@ _enum ipc_errors_ **ipc_accept** (*struct ipc_connection_info* \*srv, *struct ip
|
|||
|
||||
_enum ipc_errors_ **ipc_read** (*const struct ipc_connection_info* \*, *struct ipc_message* \*m);++
|
||||
_enum ipc_errors_ **ipc_write** (*const struct ipc_connection_info* \*, *const struct ipc_message* \*m);++
|
||||
_enum ipc_errors_ **ipc_wait_event** (*struct ipc_connection_infos* \*clients, *struct ipc_connection_info* \*srv, *struct ipc_event* \*event);
|
||||
_enum ipc_errors_ **ipc_wait_event** (*struct ipc_ctx* \*clients, *struct ipc_connection_info* \*srv, *struct ipc_event* \*event);
|
||||
|
||||
|
||||
// store and remove only pointers on allocated structures
|
||||
|
||||
_enum ipc_errors_ **ipc_add** (*struct ipc_connection_infos* \*cinfos, *struct ipc_connection_info* \*cinfo);++
|
||||
_enum ipc_errors_ **ipc_del** (*struct ipc_connection_infos* \*cinfos, *struct ipc_connection_info* \*cinfo);
|
||||
_enum ipc_errors_ **ipc_add** (*struct ipc_ctx* \*cinfos, *struct ipc_connection_info* \*cinfo);++
|
||||
_enum ipc_errors_ **ipc_del** (*struct ipc_ctx* \*cinfos, *struct ipc_connection_info* \*cinfo);
|
||||
|
||||
// add an arbitrary file descriptor to read
|
||||
|
||||
_enum ipc_errors_ **ipc_add_fd** (*struct ipc_connection_infos* \*cinfos, *int* fd);
|
||||
_enum ipc_errors_ **ipc_add_fd** (*struct ipc_ctx* \*cinfos, *int* fd);
|
||||
|
||||
|
||||
## Message functions
|
||||
|
@ -75,7 +75,7 @@ _enum ipc_errors_ **ipc_message_empty** (*struct ipc_message* \*m);
|
|||
char *spath; // max size: PATH_MAX, used to store unix socket path
|
||||
};
|
||||
|
||||
struct ipc_connection_infos {
|
||||
struct ipc_ctx {
|
||||
struct ipc_connection_info ** cinfos;
|
||||
int32_t size;
|
||||
};
|
||||
|
@ -106,7 +106,7 @@ _enum ipc_errors_ **ipc_message_empty** (*struct ipc_message* \*m);
|
|||
|
||||
Function **ipc_wait_event** returns an *event type* structure.\
|
||||
The event may be a (dis)connection, received data or an error.\
|
||||
It also can be *IPC_EVENT_TYPE_EXTRA_SOCKET* since an arbitrary file descriptor can be added to the *ipc_connection_infos* structure with **ipc_add_fd**.
|
||||
It also can be *IPC_EVENT_TYPE_EXTRA_SOCKET* since an arbitrary file descriptor can be added to the *ipc_ctx* structure with **ipc_add_fd**.
|
||||
|
||||
```
|
||||
enum ipc_event_type {
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
// print structures
|
||||
#include "message.h"
|
||||
|
||||
struct ipc_error service_path (char *path, const char *sname, int32_t index, int32_t version)
|
||||
struct ipc_error service_path (char *path, const char *sname)
|
||||
{
|
||||
T_R ((path == NULL), IPC_ERROR_SERVICE_PATH__NO_PATH);
|
||||
T_R ((sname == NULL), IPC_ERROR_SERVICE_PATH__NO_SERVICE_NAME);
|
||||
|
@ -26,17 +26,38 @@ struct ipc_error service_path (char *path, const char *sname, int32_t index, int
|
|||
if (rundir == NULL)
|
||||
rundir = RUNDIR;
|
||||
|
||||
snprintf (path, PATH_MAX - 1, "%s/%s-%d-%d", rundir, sname, index, version);
|
||||
snprintf (path, PATH_MAX - 1, "%s/%s", rundir, sname);
|
||||
|
||||
IPC_RETURN_NO_ERROR;
|
||||
}
|
||||
|
||||
struct ipc_error ipc_server_init (char **env, struct ipc_connection_info *srv, const char *sname)
|
||||
struct ipc_error ipc_ctx_init (struct ipc_ctx **ctx)
|
||||
{
|
||||
T_R ((env == NULL), IPC_ERROR_SERVER_INIT__NO_ENVIRONMENT_PARAM);
|
||||
T_R ((srv == NULL), IPC_ERROR_SERVER_INIT__NO_SERVICE_PARAM);
|
||||
T_R ((ctx == NULL), IPC_ERROR_CTX_INIT__NO_CONTEXT_PARAM);
|
||||
T_R ((*ctx != NULL), IPC_ERROR_CTX_INIT__CONTEXT_ALREADY_INIT);
|
||||
|
||||
*ctx = malloc(sizeof(struct ipc_ctx));
|
||||
T_R ((*ctx == NULL), IPC_ERROR_CTX_INIT__MALLOC_CTX);
|
||||
memset (ctx, 0, sizeof(struct ipc_ctx));
|
||||
|
||||
*ctx->pollfd = malloc(sizeof(struct pollfd));
|
||||
T_R ((*ctx->pollfd == NULL), IPC_ERROR_CTX_INIT__MALLOC_POLLFD);
|
||||
|
||||
IPC_RETURN_NO_ERROR;
|
||||
}
|
||||
|
||||
struct ipc_error ipc_server_init (
|
||||
char **env
|
||||
, struct ipc_ctx **ctx
|
||||
, struct ipc_connection_info *srv
|
||||
, const char *sname)
|
||||
{
|
||||
T_R ((env == NULL), IPC_ERROR_SERVER_INIT__NO_ENVIRONMENT_PARAM);
|
||||
T_R ((srv == NULL), IPC_ERROR_SERVER_INIT__NO_SERVICE_PARAM);
|
||||
T_R ((sname == NULL), IPC_ERROR_SERVER_INIT__NO_SERVER_NAME_PARAM);
|
||||
|
||||
ipc_ctx_init(ctx);
|
||||
|
||||
#if 0
|
||||
// For server init, no need for networkd evaluation
|
||||
|
||||
|
@ -51,7 +72,7 @@ struct ipc_error ipc_server_init (char **env, struct ipc_connection_info *srv, c
|
|||
|
||||
// gets the service path
|
||||
SECURE_BUFFER_DECLARATION (char, buf, PATH_MAX);
|
||||
TEST_IPC_RR (service_path (buf, sname, srv->index, srv->version), "cannot get server path");
|
||||
TEST_IPC_RR (service_path (buf, sname), "cannot get server path");
|
||||
|
||||
// gets the service path
|
||||
if (srv->spath != NULL) {
|
||||
|
@ -129,19 +150,22 @@ struct ipc_error ipc_contact_networkd (struct ipc_connection_info *srv, const ch
|
|||
return ret;
|
||||
}
|
||||
|
||||
struct ipc_error ipc_connection (char **env, struct ipc_connection_info *srv, const char *sname)
|
||||
// Create context, contact networkd
|
||||
struct ipc_error ipc_connection (char **env, struct ipc_ctx **ctx, const char *sname)
|
||||
{
|
||||
T_R ((env == NULL), IPC_ERROR_CONNECTION__NO_ENVIRONMENT_PARAM);
|
||||
T_R ((srv == NULL), IPC_ERROR_CONNECTION__NO_SERVER);
|
||||
T_R ((env == NULL), IPC_ERROR_CONNECTION__NO_ENVIRONMENT_PARAM);
|
||||
T_R ((ctx == NULL), IPC_ERROR_CONNECTION__NO_CTX);
|
||||
T_R ((sname == NULL), IPC_ERROR_CONNECTION__NO_SERVICE_NAME);
|
||||
|
||||
ipc_ctx_init(ctx);
|
||||
|
||||
TEST_IPC_P (ipc_contact_networkd (srv, sname), "error during networkd connection");
|
||||
|
||||
// if networkd did not initiate the connection
|
||||
if (srv->pollfd.fd <= 0) {
|
||||
// gets the service path
|
||||
SECURE_BUFFER_DECLARATION (char, buf, PATH_MAX);
|
||||
TEST_IPC_RR (service_path (buf, sname, srv->index, srv->version), "cannot get server path");
|
||||
TEST_IPC_RR (service_path (buf, sname), "cannot get server path");
|
||||
TEST_IPC_RETURN_ON_ERROR (usock_connect (&srv->pollfd.fd, buf));
|
||||
}
|
||||
|
||||
|
@ -171,7 +195,7 @@ struct ipc_error ipc_accept (
|
|||
T_R ((srv == NULL), IPC_ERROR_ACCEPT__NO_SERVICE_PARAM);
|
||||
T_R ((p == NULL), IPC_ERROR_ACCEPT__NO_CLIENT_PARAM);
|
||||
|
||||
TEST_IPC_RR (usock_accept (srv->pollfd.fd, &p->pollfd.fd), "cannot accept IPC connection");
|
||||
TEST_IPC_RR (usock_accept (srv->pollfd.fd, p->pollfd.fd), "cannot accept IPC connection");
|
||||
p->pollfd.events = POLLIN; // Tell to poll(2) to watch for incoming data from this fd.
|
||||
p->type = IPC_CONNECTION_TYPE_IPC;
|
||||
|
||||
|
@ -221,8 +245,9 @@ struct ipc_error ipc_write (const struct ipc_connection_info *p, const struct ip
|
|||
}
|
||||
|
||||
// New connection from a client.
|
||||
struct ipc_error handle_connection (struct ipc_event *event
|
||||
, struct ipc_connection_infos *cinfos
|
||||
struct ipc_error
|
||||
handle_connection (struct ipc_event *event
|
||||
, struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *cinfo)
|
||||
{
|
||||
T_R ((cinfo == NULL), IPC_ERROR_HANDLE_NEW_CONNECTION__NO_CINFO_PARAM);
|
||||
|
@ -233,8 +258,8 @@ struct ipc_error handle_connection (struct ipc_event *event
|
|||
SECURE_BUFFER_HEAP_ALLOCATION_R (new_client, sizeof (struct ipc_connection_info),,
|
||||
IPC_ERROR_HANDLE_NEW_CONNECTION__MALLOC);
|
||||
|
||||
TEST_IPC_RR (ipc_accept (cinfo, new_client), "cannot accept the client during handle_new_connection");
|
||||
TEST_IPC_RR (ipc_add (cinfos, new_client), "cannot add the new accepted client");
|
||||
TEST_IPC_RR (ipc_accept (cinfo, new_client), "cannot accept the client during handle_new_connection");
|
||||
TEST_IPC_RR (ipc_add (cinfos, new_client), "cannot add the new accepted client");
|
||||
|
||||
IPC_EVENT_SET (event, IPC_EVENT_TYPE_CONNECTION, NULL, new_client);
|
||||
IPC_RETURN_NO_ERROR;
|
||||
|
@ -243,7 +268,7 @@ struct ipc_error handle_connection (struct ipc_event *event
|
|||
// new message
|
||||
struct ipc_error handle_message (
|
||||
struct ipc_event *event
|
||||
, struct ipc_connection_infos *cinfos
|
||||
, struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *pc, struct ipc_switchings *switchdb)
|
||||
{
|
||||
// if the socket is associated to another one for networkd
|
||||
|
@ -350,13 +375,14 @@ struct ipc_error handle_message (
|
|||
IPC_RETURN_NO_ERROR;
|
||||
}
|
||||
|
||||
struct ipc_error ipc_events_loop (
|
||||
struct ipc_connection_infos *cinfos
|
||||
struct ipc_error
|
||||
ipc_events_loop (
|
||||
struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *cinfo // NULL for clients
|
||||
, struct ipc_event *event
|
||||
, struct ipc_switchings *switchdb
|
||||
, struct ipc_messages *messages_to_send
|
||||
, double *timer)
|
||||
, int *timer /* in ms */)
|
||||
{
|
||||
T_R ((cinfos == NULL), IPC_ERROR_WAIT_EVENT__NO_CLIENTS_PARAM);
|
||||
T_R ((event == NULL), IPC_ERROR_WAIT_EVENT__NO_EVENT_PARAM);
|
||||
|
@ -364,15 +390,14 @@ struct ipc_error ipc_events_loop (
|
|||
IPC_EVENT_CLEAN (event);
|
||||
|
||||
// struct ipc_connection_info {
|
||||
// uint32_t version;
|
||||
// uint32_t index;
|
||||
// struct pollfd pollfd;
|
||||
// struct pollfd *pollfd;
|
||||
// char type; // server, client, arbitrary fd
|
||||
// char *spath; // max size: PATH_MAX
|
||||
// };
|
||||
|
||||
// struct ipc_connection_infos {
|
||||
// struct ipc_ctx {
|
||||
// struct ipc_connection_info **cinfos;
|
||||
// struct ipc_connection_info *pollfd;
|
||||
// size_t size;
|
||||
// };
|
||||
|
||||
|
@ -414,135 +439,80 @@ struct ipc_error ipc_events_loop (
|
|||
// };
|
||||
// [struct pollfd]
|
||||
|
||||
#if 0
|
||||
int i, n, listenfd, server_fd, nread;
|
||||
int i, n, listenfd, client_fd, nread;
|
||||
char buf[MAXLINE];
|
||||
uid_t uid;
|
||||
struct pollfd *fds_to_poll;
|
||||
int timeout = (int) timer * 1000; // timer = seconds, poll's timeout = milliseconds
|
||||
|
||||
// Generate the array of pollfd structure once, then use it each time.
|
||||
if ( (fds_to_poll = malloc(sizeof(struct pollfd))) == NULL)
|
||||
err_sys("malloc error");
|
||||
// TODO: ça ailleurs: if ( (cinfos->pollfd = malloc(sizeof(struct pollfd))) == NULL)
|
||||
// TODO: ça ailleurs: err_sys("malloc error");
|
||||
// TODO: ça ailleurs: client_add(listenfd, 0); /* we use [0] for listenfd */
|
||||
// TODO: ça ailleurs: cinfos->pollfd[0].fd = listenfd;
|
||||
// TODO: ça ailleurs: cinfos->pollfd[0].events = POLLIN;
|
||||
|
||||
client_add(listenfd, 0); /* we use [0] for listenfd */
|
||||
fds_to_poll[0].fd = listenfd;
|
||||
fds_to_poll[0].events = POLLIN;
|
||||
|
||||
if ((n = poll(fds_to_poll, cinfos->size, INFTIM)) < 0)
|
||||
if ((n = poll(cinfos->pollfd, cinfos->size, INFTIM)) < 0)
|
||||
{
|
||||
log_sys("select error");
|
||||
}
|
||||
|
||||
for (i = 0; i <= cinfos->size; i++) {
|
||||
if (fds_to_poll[i].revents & POLLIN)
|
||||
// Something to read or connection.
|
||||
if (cinfos->pollfd[i].revents & POLLIN)
|
||||
{
|
||||
// In case there is something to read for the server socket: new client.
|
||||
if (cinfo != NULL && i == cinfo->pollfd) {
|
||||
// In case there is a new client connecting.
|
||||
/* accept new client request */
|
||||
if ( (server_fd = serv_accept(listenfd, &uid)) < 0)
|
||||
{
|
||||
printf ("serv_accept error: %d\n", server_fd);
|
||||
}
|
||||
|
||||
// i : Client number
|
||||
return handle_connection (event, cinfos, cinfo);
|
||||
}
|
||||
|
||||
for (j = 0; j < cinfos->size; j++) {
|
||||
if (i == (size_t) cinfos->cinfos[j]->pollfd.fd) {
|
||||
return handle_message (event, cinfos, cinfos->cinfos[j], switchdb);
|
||||
}
|
||||
if (i == (size_t) cinfos->cinfos[j]->pollfd.fd) {
|
||||
return handle_message (event, cinfos, cinfos->cinfos[j], switchdb);
|
||||
}
|
||||
}
|
||||
|
||||
if ( (server_fd = client[i].fd) < 0)
|
||||
continue;
|
||||
if (fds_to_poll[i].revents & POLLHUP)
|
||||
// if ((client_fd = cinfos->cinfos[i].fd) < 0)
|
||||
// continue;
|
||||
|
||||
// Timeout.
|
||||
/** TODO: timeout */
|
||||
|
||||
// Disconnection.
|
||||
if (cinfos->pollfd[i].revents & POLLHUP)
|
||||
goto hungup;
|
||||
else if (fds_to_poll[i].revents & POLLIN) {
|
||||
else if (cinfos->pollfd[i].revents & POLLIN) {
|
||||
return handle_message (event, cinfos, cinfos->cinfos[j], switchdb);
|
||||
/* read argument buffer from client */
|
||||
if ( (nread = read(server_fd, buf, MAXLINE)) < 0)
|
||||
log_sys("read error on fd %d", server_fd);
|
||||
if ( (nread = read(client_fd, buf, MAXLINE)) < 0)
|
||||
log_sys("read error on fd %d", client_fd);
|
||||
else if (nread == 0) {
|
||||
hungup:
|
||||
log_msg("closed: uid %d, fd %d",
|
||||
client[i].uid, server_fd);
|
||||
client_del(server_fd); /* client has closed conn */
|
||||
fds_to_poll[i].fd = -1;
|
||||
close(server_fd);
|
||||
log_msg("closed: uid %d, fd %d", client[i].uid, client_fd);
|
||||
client_del(client_fd); /* client has closed conn */
|
||||
cinfos->pollfd[i].fd = -1;
|
||||
close(client_fd);
|
||||
} else /* process client's rquest */
|
||||
request(buf, nread, server_fd, client[i].uid);
|
||||
request(buf, nread, client_fd, client[i].uid);
|
||||
}
|
||||
|
||||
} /** for loop: end of the message handling */
|
||||
#else
|
||||
#endif
|
||||
|
||||
/* maximum file descriptor number */
|
||||
/* keep track of the biggest file descriptor */
|
||||
|
||||
/* listening socket descriptor */
|
||||
int32_t listener;
|
||||
if (cinfo != NULL) {
|
||||
listener = cinfo->pollfd.fd;
|
||||
|
||||
/* add the listener to the master set */
|
||||
FD_SET (listener, &master);
|
||||
|
||||
/* if listener is max fd */
|
||||
if (fdmax < listener)
|
||||
fdmax = listener;
|
||||
}
|
||||
|
||||
for (i = 0; i < cinfos->size; i++) {
|
||||
FD_SET (cinfos->cinfos[i]->pollfd.fd, &master);
|
||||
}
|
||||
|
||||
readf = master;
|
||||
|
||||
struct timeval *ptimeout = NULL;
|
||||
SECURE_DECLARATION (struct timeval, timeout);
|
||||
|
||||
if (timer != NULL && *timer > 0.0) {
|
||||
timeout.tv_sec = (long) *timer;
|
||||
timeout.tv_usec = (long) ((long)((*timer) * 1000000) % 1000000);
|
||||
ptimeout = &timeout;
|
||||
}
|
||||
|
||||
T_PERROR_RIPC ((select (fdmax + 1, &readf, NULL, NULL, ptimeout) == -1), "select", IPC_ERROR_WAIT_EVENT__SELECT);
|
||||
|
||||
if (ptimeout != NULL) {
|
||||
*timer = (double) timeout.tv_sec + (timeout.tv_usec / 1000000.0);
|
||||
if (*timer == 0) {
|
||||
IPC_EVENT_SET (event, IPC_EVENT_TYPE_TIMER, NULL, NULL);
|
||||
IPC_RETURN_NO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i <= (size_t) fdmax; i++) {
|
||||
if (FD_ISSET (i, &readf)) {
|
||||
if (cinfo != NULL && i == (size_t) listener) {
|
||||
return handle_connection (event, cinfos, cinfo);
|
||||
} else {
|
||||
for (j = 0; j < cinfos->size; j++) {
|
||||
if (i == (size_t) cinfos->cinfos[j]->pollfd.fd) {
|
||||
return handle_message (event, cinfos, cinfos->cinfos[j], switchdb);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// from 0 to cinfos->size
|
||||
// if something to read
|
||||
// if this is the server
|
||||
// return handle_connection
|
||||
// else
|
||||
// from 0 to cinfos->size
|
||||
// if this is the right index in cinfos->cinfos
|
||||
// return handle_message (event, cinfos, cinfos->cinfos[j], switchdb);
|
||||
|
||||
IPC_RETURN_NO_ERROR;
|
||||
}
|
||||
|
||||
struct ipc_error ipc_wait_event_networkd (
|
||||
struct ipc_connection_infos *cinfos
|
||||
struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *cinfo // NULL for clients
|
||||
, struct ipc_event *event
|
||||
, struct ipc_switchings *switchdb
|
||||
, double *timer)
|
||||
, int *timer)
|
||||
{
|
||||
T_R ((cinfos == NULL), IPC_ERROR_WAIT_EVENT__NO_CLIENTS_PARAM);
|
||||
T_R ((event == NULL), IPC_ERROR_WAIT_EVENT__NO_EVENT_PARAM);
|
||||
|
@ -593,7 +563,7 @@ struct ipc_error ipc_wait_event_networkd (
|
|||
T_PERROR_RIPC ((select (fdmax + 1, &readf, NULL, NULL, ptimeout) == -1), "select", IPC_ERROR_WAIT_EVENT__SELECT);
|
||||
|
||||
if (ptimeout != NULL) {
|
||||
*timer = (double) timeout.tv_sec + (timeout.tv_usec / 1000000.0);
|
||||
*timer = timeout.tv_sec + (timeout.tv_usec / 1000000.0);
|
||||
if (*timer == 0) {
|
||||
IPC_EVENT_SET (event, IPC_EVENT_TYPE_TIMER, NULL, NULL);
|
||||
IPC_RETURN_NO_ERROR;
|
||||
|
@ -618,9 +588,10 @@ struct ipc_error ipc_wait_event_networkd (
|
|||
}
|
||||
|
||||
struct ipc_error ipc_wait_event (
|
||||
struct ipc_connection_infos *cinfos
|
||||
struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *cinfo // NULL for clients
|
||||
, struct ipc_event *event, double *timer)
|
||||
, struct ipc_event *event
|
||||
, int *timer)
|
||||
{
|
||||
return ipc_wait_event_networkd (cinfos, cinfo, event, NULL, timer);
|
||||
}
|
||||
|
@ -632,7 +603,7 @@ struct ipc_error ipc_wait_event (
|
|||
* WARNING: Store and remove only pointers on allocated structures.
|
||||
*/
|
||||
struct ipc_error ipc_add (
|
||||
struct ipc_connection_infos *cinfos
|
||||
struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *p)
|
||||
{
|
||||
T_R ((cinfos == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENTS);
|
||||
|
@ -640,21 +611,28 @@ struct ipc_error ipc_add (
|
|||
|
||||
cinfos->size++;
|
||||
// In case this is the first allocation.
|
||||
if (cinfos->size == 1 && cinfos->cinfos == NULL) {
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_R (cinfos->cinfos, sizeof (struct ipc_connection_info),,
|
||||
IPC_ERROR_ADD__MALLOC);
|
||||
if (cinfos->size == 1) {
|
||||
if (cinfos->cinfos == NULL && cinfos->pollfd == NULL) {
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_R (cinfos->cinfos, sizeof (struct ipc_connection_info),,
|
||||
IPC_ERROR_ADD__MALLOC);
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_R (cinfos->pollfd, sizeof (struct pollfd),,
|
||||
IPC_ERROR_ADD__MALLOC_POLLFD);
|
||||
}
|
||||
} else {
|
||||
cinfos->cinfos = realloc (cinfos->cinfos, sizeof (struct ipc_connection_info) * cinfos->size);
|
||||
cinfos->pollfd = realloc (cinfos->pollfd, sizeof (struct pollfd ) * cinfos->size);
|
||||
}
|
||||
|
||||
T_R ((cinfos->cinfos == NULL), IPC_ERROR_ADD__EMPTY_LIST);
|
||||
|
||||
cinfos->cinfos[cinfos->size - 1] = p;
|
||||
cinfos->cinfos[cinfos->size - 1] = p;
|
||||
cinfos->pollfd[cinfos->size - 1] = p->pollfd;
|
||||
|
||||
IPC_RETURN_NO_ERROR;
|
||||
}
|
||||
|
||||
struct ipc_error ipc_del (
|
||||
struct ipc_connection_infos *cinfos
|
||||
struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *p)
|
||||
{
|
||||
T_R ((cinfos == NULL), IPC_ERROR_DEL__NO_CLIENTS_PARAM);
|
||||
|
@ -686,7 +664,7 @@ struct ipc_error ipc_del (
|
|||
IPC_RETURN_ERROR (IPC_ERROR_DEL__CANNOT_FIND_CLIENT);
|
||||
}
|
||||
|
||||
void ipc_connections_close (struct ipc_connection_infos *cinfos)
|
||||
void ipc_connections_close (struct ipc_ctx *cinfos)
|
||||
{
|
||||
if (cinfos->cinfos != NULL) {
|
||||
for (size_t i = 0; i < cinfos->size; i++) {
|
||||
|
@ -699,7 +677,7 @@ void ipc_connections_close (struct ipc_connection_infos *cinfos)
|
|||
cinfos->size = 0;
|
||||
}
|
||||
|
||||
void ipc_connections_free (struct ipc_connection_infos *cinfos)
|
||||
void ipc_connections_free (struct ipc_ctx *cinfos)
|
||||
{
|
||||
if (cinfos->cinfos != NULL) {
|
||||
for (size_t i = 0; i < cinfos->size; i++) {
|
||||
|
@ -711,27 +689,8 @@ void ipc_connections_free (struct ipc_connection_infos *cinfos)
|
|||
cinfos->size = 0;
|
||||
}
|
||||
|
||||
// create the client service structure
|
||||
struct ipc_error ipc_connection_gen (struct ipc_connection_info *cinfo
|
||||
, uint32_t index, uint32_t version
|
||||
, int fd, char type)
|
||||
{
|
||||
T_R ((cinfo == NULL), IPC_ERROR_CONNECTION_GEN__NO_CINFO);
|
||||
|
||||
cinfo->type = type;
|
||||
cinfo->version = version;
|
||||
cinfo->index = index;
|
||||
// cinfo->fd = fd;
|
||||
|
||||
cinfo.pollfd.fd = fd;
|
||||
cinfo.pollfd.events = POLLIN;
|
||||
// cinfo.pollfd.revents == returned events
|
||||
|
||||
IPC_RETURN_NO_ERROR;
|
||||
}
|
||||
|
||||
// add an arbitrary file descriptor to read
|
||||
struct ipc_error ipc_add_fd (struct ipc_connection_infos *cinfos, int fd)
|
||||
struct ipc_error ipc_add_fd (struct ipc_ctx *cinfos, int fd)
|
||||
{
|
||||
T_R ((cinfos == NULL), IPC_ERROR_ADD_FD__NO_PARAM_CINFOS);
|
||||
|
||||
|
@ -740,13 +699,14 @@ struct ipc_error ipc_add_fd (struct ipc_connection_infos *cinfos, int fd)
|
|||
SECURE_BUFFER_HEAP_ALLOCATION_R (cinfo, sizeof (struct ipc_connection_info),,
|
||||
IPC_ERROR_ADD_FD__NOT_ENOUGH_MEMORY);
|
||||
|
||||
ipc_connection_gen (cinfo, 0, 0, fd, IPC_CONNECTION_TYPE_EXTERNAL);
|
||||
cinfo->type = type;
|
||||
ipc_connection_gen (cinfo, fd, IPC_CONNECTION_TYPE_EXTERNAL);
|
||||
|
||||
return ipc_add (cinfos, cinfo);
|
||||
}
|
||||
|
||||
// remove a connection from its file descriptor
|
||||
struct ipc_error ipc_del_fd (struct ipc_connection_infos *cinfos, int fd)
|
||||
struct ipc_error ipc_del_fd (struct ipc_ctx *cinfos, int fd)
|
||||
{
|
||||
T_R ((cinfos == NULL), IPC_ERROR_DEL_FD__NO_PARAM_CINFOS);
|
||||
T_R ((cinfos->cinfos == NULL), IPC_ERROR_DEL_FD__EMPTY_LIST);
|
||||
|
|
50
src/ipc.h
50
src/ipc.h
|
@ -187,15 +187,26 @@ struct ipc_error {
|
|||
const char *ipc_errors_get (enum ipc_error_code e);
|
||||
|
||||
struct ipc_connection_info {
|
||||
uint32_t version;
|
||||
uint32_t index;
|
||||
struct pollfd pollfd;
|
||||
char type; // server, client, arbitrary fd
|
||||
char *spath; // max size: PATH_MAX
|
||||
};
|
||||
|
||||
struct ipc_connection_infos {
|
||||
struct ipc_connection_info **cinfos;
|
||||
/**
|
||||
* Context of the whole networking state.
|
||||
*/
|
||||
struct ipc_ctx {
|
||||
/**
|
||||
* Keep track of connections.
|
||||
*/
|
||||
struct ipc_connection_info *cinfos;
|
||||
/**
|
||||
* List of "pollfd" structures within cinfos, so we can pass it to poll(2).
|
||||
*/
|
||||
struct pollfd *pollfd;
|
||||
/**
|
||||
* Size of the connection list.
|
||||
*/
|
||||
size_t size;
|
||||
};
|
||||
|
||||
|
@ -255,26 +266,24 @@ struct ipc_error ipc_close (struct ipc_connection_info *p);
|
|||
struct ipc_error ipc_read (const struct ipc_connection_info *, struct ipc_message *m);
|
||||
struct ipc_error ipc_write (const struct ipc_connection_info *, const struct ipc_message *m);
|
||||
|
||||
struct ipc_error ipc_wait_event (struct ipc_connection_infos *clients
|
||||
, struct ipc_connection_info *srv
|
||||
, struct ipc_event *event, double *timer);
|
||||
struct ipc_error ipc_wait_event (struct ipc_ctx *, struct ipc_event *, int *timer);
|
||||
struct ipc_error ipc_wait_event (struct ipc_ctx *, struct ipc_event *, int *timer);
|
||||
|
||||
// store and remove only pointers on allocated structures
|
||||
struct ipc_error ipc_add (struct ipc_connection_infos *, struct ipc_connection_info *);
|
||||
struct ipc_error ipc_del (struct ipc_connection_infos *, struct ipc_connection_info *);
|
||||
struct ipc_error ipc_add (struct ipc_ctx *, struct ipc_connection_info *);
|
||||
struct ipc_error ipc_del (struct ipc_ctx *, struct ipc_connection_info *);
|
||||
|
||||
// add an arbitrary file descriptor to read
|
||||
struct ipc_error ipc_add_fd (struct ipc_connection_infos *cinfos, int fd);
|
||||
struct ipc_error ipc_del_fd (struct ipc_connection_infos *cinfos, int fd);
|
||||
struct ipc_error ipc_add_fd (struct ipc_ctx *cinfos, int fd);
|
||||
struct ipc_error ipc_del_fd (struct ipc_ctx *cinfos, int fd);
|
||||
|
||||
void ipc_connections_free (struct ipc_connection_infos *);
|
||||
void ipc_connections_free (struct ipc_ctx *);
|
||||
|
||||
// create the client service structure
|
||||
struct ipc_error ipc_connection_gen (struct ipc_connection_info *cinfo
|
||||
, uint32_t index, uint32_t version
|
||||
, int fd, char type);
|
||||
|
||||
void ipc_connections_close (struct ipc_connection_infos *cinfos);
|
||||
void ipc_connections_close (struct ipc_ctx *cinfos);
|
||||
|
||||
/***
|
||||
* message functions
|
||||
|
@ -303,12 +312,17 @@ void ipc_messages_free (struct ipc_messages *);
|
|||
* non public functions
|
||||
**/
|
||||
|
||||
/**
|
||||
* Used by ipc_server_init and ipc_connection
|
||||
*/
|
||||
struct ipc_error ipc_ctx_init (struct ipc_ctx **);
|
||||
|
||||
void ipc_connection_print (struct ipc_connection_info *cinfo);
|
||||
void ipc_connections_print (struct ipc_connection_infos *cinfos);
|
||||
void ipc_connections_print (struct ipc_ctx *cinfos);
|
||||
|
||||
struct ipc_error ipc_accept (struct ipc_connection_info *srv, struct ipc_connection_info *p);
|
||||
struct ipc_error ipc_contact_networkd (struct ipc_connection_info *srv, const char *sname);
|
||||
struct ipc_error service_path (char *path, const char *sname, int32_t index, int32_t version);
|
||||
struct ipc_error service_path (char *path, const char *sname);
|
||||
|
||||
/***
|
||||
* networkd enumerations, structures and functions
|
||||
|
@ -327,11 +341,11 @@ struct ipc_switchings {
|
|||
struct networkd {
|
||||
int cpt;
|
||||
struct ipc_connection_info *srv;
|
||||
struct ipc_connection_infos *clients;
|
||||
struct ipc_ctx *clients;
|
||||
struct ipc_switchings *TCP_TO_IPC;
|
||||
};
|
||||
|
||||
struct ipc_error ipc_wait_event_networkd (struct ipc_connection_infos *cinfos
|
||||
struct ipc_error ipc_wait_event_networkd (struct ipc_ctx *cinfos
|
||||
, struct ipc_connection_info *cinfo // cinfo is NULL for clients
|
||||
, struct ipc_event *event, struct ipc_switchings *switchdb, double *timer);
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ void ipc_connection_print (struct ipc_connection_info *cinfo)
|
|||
#endif
|
||||
}
|
||||
|
||||
void ipc_connections_print (struct ipc_connection_infos *cinfos)
|
||||
void ipc_connections_print (struct ipc_ctx *cinfos)
|
||||
{
|
||||
for (size_t i = 0; i < cinfos->size; i++) {
|
||||
ipc_connection_print (cinfos->cinfos[i]);
|
||||
|
|
|
@ -18,7 +18,7 @@ int main(int argc, char * argv[], char **env)
|
|||
TEST_IPC_Q(ipc_server_init (env, &srv, SERVICE_NAME), EXIT_FAILURE);
|
||||
|
||||
printf ("func 01 - server init ok\n");
|
||||
SECURE_DECLARATION(struct ipc_connection_infos, clients);
|
||||
SECURE_DECLARATION(struct ipc_ctx, clients);
|
||||
SECURE_DECLARATION(struct ipc_event,event);
|
||||
|
||||
printf ("func 01 - service polling...\n");
|
||||
|
|
|
@ -45,7 +45,7 @@ void interactive (char *env[])
|
|||
TEST_IPC_Q(ipc_connection (env, &srv, SERVICE_NAME), EXIT_FAILURE);
|
||||
|
||||
SECURE_DECLARATION(struct ipc_event, event);
|
||||
SECURE_DECLARATION(struct ipc_connection_infos, services);
|
||||
SECURE_DECLARATION(struct ipc_ctx, services);
|
||||
|
||||
TEST_IPC_Q(ipc_add (&services, &srv), EXIT_FAILURE);
|
||||
|
||||
|
|
|
@ -15,15 +15,15 @@
|
|||
int cpt = 0;
|
||||
|
||||
struct ipc_connection_info *srv = 0;
|
||||
struct ipc_connection_infos *clients;
|
||||
struct ipc_ctx *clients;
|
||||
|
||||
|
||||
void main_loop ()
|
||||
{
|
||||
SECURE_DECLARATION(struct ipc_error, ret);
|
||||
|
||||
clients = malloc (sizeof (struct ipc_connection_infos));
|
||||
memset(clients, 0, sizeof(struct ipc_connection_infos));
|
||||
clients = malloc (sizeof (struct ipc_ctx));
|
||||
memset(clients, 0, sizeof(struct ipc_ctx));
|
||||
|
||||
SECURE_DECLARATION(struct ipc_event,event);
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ void read_message (struct ipc_connection_info *ci)
|
|||
{
|
||||
#if 0
|
||||
SECURE_DECLARATION(struct ipc_event, event);
|
||||
SECURE_DECLARATION(struct ipc_connection_infos, clients);
|
||||
SECURE_DECLARATION(struct ipc_ctx, clients);
|
||||
|
||||
long timer = 10;
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ int main_loop(int argc, char * argv[], char **env)
|
|||
TEST_IPC_Q (ipc_server_init (env, &srv, SERVICE_NAME), EXIT_FAILURE);
|
||||
printf ("func 03 - server init ok\n");
|
||||
|
||||
SECURE_DECLARATION (struct ipc_connection_infos, clients);
|
||||
SECURE_DECLARATION (struct ipc_ctx, clients);
|
||||
SECURE_DECLARATION (struct ipc_event, event);
|
||||
|
||||
printf ("func 01 - service polling...\n");
|
||||
|
|
Loading…
Reference in New Issue