From 8ccefcc40f6b684997e5d0ca972459b6fcb82628 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Wed, 8 Jul 2020 12:40:24 +0200 Subject: [PATCH] lacking default cb_out --- examples/simple-tcpd.c | 8 +++++--- src/communication.c | 27 +++++++++++++++++++++++++-- src/ipc.h | 3 +++ src/network.c | 20 ++++++++++++++++---- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/examples/simple-tcpd.c b/examples/simple-tcpd.c index 923b7f4..1811021 100644 --- a/examples/simple-tcpd.c +++ b/examples/simple-tcpd.c @@ -90,13 +90,14 @@ void tcp_connection (int fd) T_PERROR_Q ((send (fd, "OK", 2, 0) <= 0), "sending a message", EXIT_FAILURE); printf ("connection to %s\n", buf); - struct ipc_error ret = ipc_connection (ctx, buf); + struct ipc_error ret = ipc_connection_switched (ctx, buf); if (ret.error_code != IPC_ERROR_NONE) { fprintf (stderr, "%s\n", ret.error_message); exit (EXIT_FAILURE); } ipc_switching_add (&ctx->switchdb, fd, ctx->pollfd[ctx->size-1].fd); + ipc_ctx_fd_type (ctx, fd, IPC_CONNECTION_TYPE_SWITCHED); } int accept_new_client (int serverfd) @@ -109,8 +110,9 @@ int accept_new_client (int serverfd) accept (serverfd, (struct sockaddr *)&client, &addrlen)) == -1), "accept new client", EXIT_FAILURE); - // adding a client - ipc_add_fd_switched (ctx, sock_fd_client); + // adding a client, for now not switched: + // tcpd should handle the first message (getting the service name) + ipc_add_fd (ctx, sock_fd_client); return sock_fd_client; } diff --git a/src/communication.c b/src/communication.c index 8983b61..b0901e4 100644 --- a/src/communication.c +++ b/src/communication.c @@ -98,13 +98,13 @@ struct ipc_error ipc_contact_ipcd (int *pfd, const char *sname) } // Create context, contact ipcd, connects to the service. -struct ipc_error ipc_connection (struct ipc_ctx *ctx, const char *sname) +struct ipc_error ipc_connection_ (struct ipc_ctx *ctx, const char *sname, enum ipc_connection_type type) { T_R ((ctx == NULL), IPC_ERROR_CONNECTION__NO_CTX); T_R ((sname == NULL), IPC_ERROR_CONNECTION__NO_SERVICE_NAME); SECURE_DECLARATION(struct ipc_connection_info, srv); - srv.type = IPC_CONNECTION_TYPE_IPC; // Data received on the socket = messages, not new clients. + srv.type = type; SECURE_DECLARATION(struct pollfd, pollfd); pollfd.events = POLLIN; @@ -124,6 +124,29 @@ struct ipc_error ipc_connection (struct ipc_ctx *ctx, const char *sname) IPC_RETURN_NO_ERROR; } +int ipc_ctx_fd_type (struct ipc_ctx *ctx, int fd, enum ipc_connection_type type) +{ + for (size_t i = 0; i < ctx->size; i++) { + if (ctx->pollfd[i].fd == fd) { + ctx->cinfos[i].type = type; + return 0; + } + } + return -1; +} + +struct ipc_error ipc_connection (struct ipc_ctx *ctx, const char *sname) +{ + // Data received on the socket = messages, not new clients, and not switched (no callbacks). + return ipc_connection_ (ctx, sname, IPC_CONNECTION_TYPE_IPC); +} + +struct ipc_error ipc_connection_switched (struct ipc_ctx *ctx, const char *sname) +{ + // Data received are for switched fd (callbacks should be used). + return ipc_connection_ (ctx, sname, IPC_CONNECTION_TYPE_SWITCHED); +} + struct ipc_error ipc_close_all (struct ipc_ctx *ctx) { for (size_t i = 0 ; i < ctx->size ; i++) { diff --git a/src/ipc.h b/src/ipc.h index 09d384d..f487948 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -321,6 +321,7 @@ struct ipc_error ipc_wait_event (struct ipc_ctx *, struct ipc_event *, int *time struct ipc_error ipc_server_init (struct ipc_ctx *ctx, const char *sname); struct ipc_error ipc_connection (struct ipc_ctx *ctx, const char *sname); +struct ipc_error ipc_connection_switched (struct ipc_ctx *ctx, const char *sname); struct ipc_error ipc_close (struct ipc_ctx *ctx, uint32_t index); struct ipc_error ipc_close_all (struct ipc_ctx *ctx); @@ -392,6 +393,8 @@ int ipc_switching_del (struct ipc_switchings *is, int fd); int ipc_switching_get (struct ipc_switchings *is, int fd); void ipc_switching_free (struct ipc_switchings *is); +int ipc_ctx_fd_type (struct ipc_ctx *ctx, int fd, enum ipc_connection_type type); + void ipc_switching_print (struct ipc_switchings *is); struct ipc_error ipc_receive_fd (int sock, int *fd); diff --git a/src/network.c b/src/network.c index a35bbfe..192e945 100644 --- a/src/network.c +++ b/src/network.c @@ -236,7 +236,9 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx // read and write automatically and provide a new IPC_EVENT_TYPE indicating the switch. T_R ((ctx->switchdb.size == 0), IPC_ERROR_FD_SWITCHING__NO_FD_RECORD); + printf ("coucou\n"); int talkingfd = ctx->pollfd[index].fd; + int dest_fd = -1; struct ipc_switching sw; struct ipc_message m; @@ -246,8 +248,10 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx is_valid = ipc_switching_get_ (&ctx->switchdb, talkingfd, &sw); T_R ((is_valid == -1), IPC_ERROR_FD_SWITCHING__NO_FD_RECORD); + printf ("valid\n"); if (sw.orig == talkingfd) { + dest_fd = sw.dest; if (sw.orig_in == NULL) { r = default_cb_in (talkingfd, &m); } @@ -256,6 +260,7 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx } } else { + dest_fd = sw.orig; if (sw.dest_in == NULL) { r = default_cb_in (talkingfd, &m); } @@ -263,11 +268,13 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx r = (*sw.dest_in)(talkingfd, &m); } } + printf ("cb done\n"); // Message reception OK: reading the message and put it in the list of messages to send. if (r == IPC_CB_NO_ERROR) { // In case of message reception: // 1. put the message in the list to be sent + m.fd = dest_fd; ipc_write (ctx, &m); // 2. set event IPC_EVENT_TYPE_SWITCH, inform ipcd of a successful reception. IPC_EVENT_SET (event, IPC_EVENT_TYPE_SWITCH, index, ctx->pollfd[index].fd, NULL); @@ -275,6 +282,8 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx IPC_RETURN_NO_ERROR; } + printf ("error or disconnection\n"); + /** * NOTE: In any other case, the fd is, or should be closed. */ @@ -290,9 +299,11 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx // 2. set event (either error or disconnection) if (r == IPC_CB_FD_CLOSING) { + printf ("DISCONNECTION\n"); IPC_EVENT_SET (event, IPC_EVENT_TYPE_DISCONNECTION, index, talkingfd, NULL); } else { + printf ("ERROR\n"); IPC_EVENT_SET (event, IPC_EVENT_TYPE_ERROR, index, talkingfd, NULL); } @@ -335,21 +346,22 @@ struct ipc_error fd_switching_write (struct ipc_event *event, struct ipc_ctx *ct if (sw.orig == output_fd) { if (sw.orig_in == NULL) { - r = default_cb_out (output_fd, &m); + r = default_cb_out (output_fd, m); } else { - r = (*sw.orig_out)(output_fd, &m); + r = (*sw.orig_out)(output_fd, m); } } else { if (sw.dest_in == NULL) { - r = default_cb_out (output_fd, &m); + r = default_cb_out (output_fd, m); } else { - r = (*sw.dest_out)(output_fd, &m); + r = (*sw.dest_out)(output_fd, m); } } + // Whether or not the message has been sent, it should be removed. // Freeing the message structure. ipc_message_empty (m); // Removing the message from the context.