From ff21ff42cb00a8dab33a263d18b94d021f80b88c Mon Sep 17 00:00:00 2001 From: Karchnu Date: Fri, 10 Jul 2020 20:18:17 +0200 Subject: [PATCH] almost usable version --- src/communication.c | 18 +++++++++++++++--- src/ipc.h | 15 +++++++++++---- src/message.c | 21 +++++++++++++++++++++ src/network.c | 40 ++++++++++++++++++++-------------------- 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/src/communication.c b/src/communication.c index cf7b34d..9258795 100644 --- a/src/communication.c +++ b/src/communication.c @@ -14,6 +14,9 @@ // print structures #include "message.h" +#include + + struct ipc_error ipc_server_init (struct ipc_ctx *ctx, const char *sname) { T_R ((sname == NULL), IPC_ERROR_SERVER_INIT__NO_SERVER_NAME_PARAM); @@ -126,12 +129,10 @@ struct ipc_error ipc_connection_ (struct ipc_ctx *ctx, const char *sname, enum i int ipc_ctx_fd_type (struct ipc_ctx *ctx, int fd, enum ipc_connection_type type) { - printf ("starting ipc_ctx_fd_type\n"); if (ctx == NULL) { return -1; } - printf ("starting ipc_ctx_fd_type after ctx test\n"); for (size_t i = 0; i < ctx->size; i++) { if (ctx->pollfd[i].fd == fd) { ctx->cinfos[i].type = type; @@ -424,10 +425,13 @@ handle_writing_switched_message (struct ipc_event *event, struct ipc_ctx *ctx, u struct ipc_error handle_switched_message(struct ipc_event *event, struct ipc_ctx *ctx, uint32_t index) { - printf ("handling message comming from a switched fd\n"); return fd_switching_read (event, ctx, index); } +int fd_is_valid(int fd) +{ + return fcntl(fd, F_GETFD) != -1 || errno != EBADF; +} /* timer is in ms */ struct ipc_error ipc_wait_event (struct ipc_ctx *ctx, struct ipc_event *event, int *timer) @@ -490,6 +494,14 @@ struct ipc_error ipc_wait_event (struct ipc_ctx *ctx, struct ipc_event *event, i // fd is switched: using callbacks for IO operations. if (ctx->cinfos[i].type == IPC_CONNECTION_TYPE_SWITCHED) { + int fd_validity = fd_is_valid (ctx->pollfd[i].fd); + if (fd_validity) { + printf ("switch happening in C: %d FD VALID\n", ctx->pollfd[i].fd); + } + else { + printf ("switch happening in C: %d FD IS INVALID:::::::::: IM BROKEN INSIIIIIIIDE\n", ctx->pollfd[i].fd); + } + return handle_switched_message (event, ctx, i); } diff --git a/src/ipc.h b/src/ipc.h index c8c4d03..7fac91c 100644 --- a/src/ipc.h +++ b/src/ipc.h @@ -87,10 +87,11 @@ enum ipc_event_type { // For IO callbacks (switching). enum ipccb { - IPC_CB_NO_ERROR = 0 - , IPC_CB_FD_CLOSING = 1 - , IPC_CB_FD_ERROR = 2 - , IPC_CB_PARSING_ERROR = 3 + IPC_CB_NO_ERROR = 0 // No error. A message was generated. + , IPC_CB_FD_CLOSING = 1 // The fd is closing. + , IPC_CB_FD_ERROR = 2 // Generic error. + , IPC_CB_PARSING_ERROR = 3 // The message was read but with errors. + , IPC_CB_IGNORE = 4 // The message should be ignored (protocol specific). }; /** @@ -258,6 +259,12 @@ struct ipc_switchings { size_t size; }; +void ipc_message_copy (struct ipc_message *m + , uint32_t fd + , uint8_t type + , uint8_t utype + , char *payload + , uint32_t paylen); struct ipc_error ipc_messages_del (struct ipc_messages *messages, uint32_t index); /** diff --git a/src/message.c b/src/message.c index b2c3892..a4a1add 100644 --- a/src/message.c +++ b/src/message.c @@ -197,6 +197,27 @@ struct ipc_error ipc_messages_del (struct ipc_messages *messages, uint32_t inde IPC_RETURN_NO_ERROR; } +void ipc_message_copy (struct ipc_message *m + , uint32_t fd + , uint8_t type + , uint8_t utype + , char *payload + , uint32_t paylen) +{ + // printf("starting the message copy\n"); + m->fd = fd; + m->type = type; + m->user_type = utype; + m->length = paylen; + if (m->payload != NULL) { + free(m->payload); + } + // printf("BEFORE THE PAYLOAD COPY\n"); + m->payload = malloc(sizeof(char) * paylen); + memcpy(m->payload, payload, paylen); + // printf("PAYLOAD COPY DONE\n"); +} + void ipc_messages_free (struct ipc_messages *messages) { if (messages != NULL) diff --git a/src/network.c b/src/network.c index 6c94746..572f694 100644 --- a/src/network.c +++ b/src/network.c @@ -88,14 +88,14 @@ void ipc_ctx_switching_add (struct ipc_ctx *ctx, int orig, int dest) void ipc_switching_add (struct ipc_switchings *is, int orig, int dest) { - printf ("ipc_switching_add START: switchdb has %ld entries\n", is->size); + // printf ("ipc_switching_add START: switchdb has %ld entries\n", is->size); if (is->collection == NULL) { - printf ("switchdb collection is null\n"); + // printf ("switchdb collection is null\n"); is->collection = malloc (sizeof (struct ipc_switching) * (is->size + 1)); } else { - printf ("switchdb collection isn't null\n"); + // printf ("switchdb collection isn't null\n"); is->collection = realloc (is->collection, sizeof (struct ipc_switching) * (is->size + 1)); } @@ -115,7 +115,7 @@ void ipc_switching_add (struct ipc_switchings *is, int orig, int dest) is->collection[is->size - 1].orig_out = NULL; is->collection[is->size - 1].dest_out = NULL; - printf ("ipc_switching_add END: switchdb has %ld entries\n", is->size); + // printf ("ipc_switching_add END: switchdb has %ld entries\n", is->size); } int ipc_switching_del (struct ipc_switchings *is, int fd) @@ -262,25 +262,12 @@ void ipc_switching_callbacks ( , enum ipccb (*cb_in )(int fd, struct ipc_message *m) , enum ipccb (*cb_out)(int fd, struct ipc_message *m)) { - if (ctx == NULL) { - printf ("context is null\n"); - } - else { - printf ("context has a switchdb with %ld entries\n", ctx->switchdb.size); - } - - printf ("\nBEFORE BREACKING THINGS\n"); - ipc_ctx_print (ctx); - printf ("\nLET'S BREAK THINGS\n"); - struct ipc_switching *sw = NULL; int is_valid = ipc_switching_get_ (&ctx->switchdb, fd, &sw); if (is_valid == -1) { return; } - printf ("valid sw in ipc_switching_callbacks\n"); - if (sw->orig == fd) { sw->orig_in = cb_in; sw->orig_out = cb_out; @@ -289,7 +276,6 @@ void ipc_switching_callbacks ( sw->dest_in = cb_in; sw->dest_out = cb_out; } - printf ("ipc_switching_callbacks done\n"); } /** @@ -297,7 +283,7 @@ void ipc_switching_callbacks ( */ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx, int index) { - printf ("fd_switching_read\n"); + // printf ("fd_switching_read\n"); // If the socket is associated to another one for ipcd: // read and write automatically and provide a new IPC_EVENT_TYPE indicating the switch. @@ -337,6 +323,7 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx // Message reception OK: reading the message and put it in the list of messages to send. if (r == IPC_CB_NO_ERROR) { + printf ("NO ERROR?? SURE?? REQUEST\n"); // In case of message reception: // 1. put the message in the list to be sent m.fd = dest_fd; @@ -347,6 +334,19 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx IPC_RETURN_NO_ERROR; } + // Message reception OK: no message to transfer. + // This is applied to protocol-specific messages, for example when the client + // has to communicate with the proxy, not the service. + if (r == IPC_CB_IGNORE) { + printf ("IGNORING REQUEST\n"); + // In case of message reception: + // 1. 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); + // 2. IPC_RETURN_NO_ERROR + IPC_RETURN_NO_ERROR; + } + + printf ("CLOSING (ERROR/CLOSING...) REQUEST\n"); /** * NOTE: In any other case, the fd is, or should be closed. */ @@ -377,7 +377,7 @@ struct ipc_error fd_switching_read (struct ipc_event *event, struct ipc_ctx *ctx */ struct ipc_error fd_switching_write (struct ipc_event *event, struct ipc_ctx *ctx, int index) { - printf ("fd_switching_write\n"); + // printf ("fd_switching_write\n"); // If the socket is associated to another one for ipcd: // read and write automatically and provide a new IPC_EVENT_TYPE indicating the switch.