diff --git a/examples/simple-tcpd.c b/examples/simple-tcpd.c index 8857dc7..075a6e8 100644 --- a/examples/simple-tcpd.c +++ b/examples/simple-tcpd.c @@ -52,23 +52,23 @@ void chomp (char *str, size_t len) #define SERVICE_NAME "simpletcp" -struct networkd *ctx; +struct networkd *ipcd_ctx; void handle_disconnection (int fd) { int delfd; - delfd = ipc_switching_del (ctx->TCP_TO_IPC, fd); + delfd = ipc_switching_del (ipcd_ctx->TCP_TO_IPC, fd); if (delfd >= 0) { close (delfd); - ipc_del_fd (ctx->clients, delfd); + ipc_del_fd (ipcd_ctx->clients, delfd); } close (fd); - ipc_del_fd (ctx->clients, fd); + ipc_del_fd (ipcd_ctx->clients, fd); // printf ("TCP_TO_IPC\n"); - ipc_switching_print (ctx->TCP_TO_IPC); + ipc_switching_print (ipcd_ctx->TCP_TO_IPC); } void tcp_connection (char **env, int fd) @@ -89,7 +89,7 @@ void tcp_connection (char **env, int fd) // TODO: tests T_PERROR_Q ((send (fd, "OK", 2, 0) <= 0), "sending a message", EXIT_FAILURE); - SECURE_DECLARATION (struct ipc_connection_info, tcp_to_ipc_ci); + SECURE_DECLARATION (struct ipc_ctx, tcp_to_ipc_ci); struct ipc_error ret = ipc_connection (env, &tcp_to_ipc_ci, buf); if (ret.error_code != IPC_ERROR_NONE) { @@ -97,8 +97,8 @@ void tcp_connection (char **env, int fd) exit (EXIT_FAILURE); } - ipc_switching_add (ctx->TCP_TO_IPC, fd, tcp_to_ipc_ci.fd); - ipc_add_fd (ctx->clients, tcp_to_ipc_ci.fd); + ipc_switching_add (ipcd_ctx->TCP_TO_IPC, fd, tcp_to_ipc_ci.fd); + ipc_add_fd (ipcd_ctx->clients, tcp_to_ipc_ci.fd); } int accept_new_client (int serverfd) @@ -112,7 +112,7 @@ int accept_new_client (int serverfd) EXIT_FAILURE); // adding a client - ipc_add_fd (ctx->clients, sock_fd_client); + ipc_add_fd (ipcd_ctx->clients, sock_fd_client); return sock_fd_client; } @@ -155,17 +155,17 @@ void main_loop (int argc, char **argv, char **env) return; } - SECURE_BUFFER_HEAP_ALLOCATION_Q (ctx->clients, sizeof (struct ipc_ctx),, EXIT_FAILURE); + SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->clients, sizeof (struct ipc_ctx),, EXIT_FAILURE); SECURE_DECLARATION (struct ipc_event, event); - ipc_add_fd (ctx->clients, serverfd); + ipc_add_fd (ipcd_ctx->clients, serverfd); while (1) { // ipc_wait_event provides one event at a time // warning: event->m is free'ed if not NULL long timer = 10; - TEST_IPC_WAIT_EVENT_Q (ipc_wait_event_networkd (ctx->clients, ctx->srv, &event, ctx->TCP_TO_IPC, &timer) + TEST_IPC_WAIT_EVENT_Q (ipc_wait_event_networkd (ipcd_ctx->clients, ipcd_ctx->srv, &event, ipcd_ctx->TCP_TO_IPC, &timer) , EXIT_FAILURE); switch (event.type) { @@ -183,35 +183,31 @@ void main_loop (int argc, char **argv, char **env) case IPC_EVENT_TYPE_EXTRA_SOCKET: { // NEW CLIENT - if (event.origin->fd == serverfd) { + if (event.origin == serverfd) { int sock_fd_client = accept_new_client (serverfd); - ctx->cpt++; - printf ("TCP connection: %d clients connected\n", ctx->cpt); + ipcd_ctx->cpt++; + printf ("TCP connection: %d clients connected\n", ipcd_ctx->cpt); printf ("new TCP client has the fd %d\n", sock_fd_client); } // CLIENT IS TALKING else { - tcp_connection (env, event.origin->fd); + tcp_connection (env, event.origin); } } break; case IPC_EVENT_TYPE_CONNECTION: { - ctx->cpt++; - printf ("connection: %d clients connected\n", ctx->cpt); - printf ("new client has the fd %d\n", (event.origin)->fd); + ipcd_ctx->cpt++; + printf ("connection: %d clients connected\n", ipcd_ctx->cpt); + printf ("new client has the fd %d\n", event.origin); }; break; case IPC_EVENT_TYPE_DISCONNECTION: { - ctx->cpt--; - printf ("disconnection: %d clients remaining\n", ctx->cpt); - - // free the ipc_client structure - // if (event.origin != NULL) - // free (event.origin); + ipcd_ctx->cpt--; + printf ("disconnection: %d clients remaining\n", ipcd_ctx->cpt); }; break; case IPC_EVENT_TYPE_MESSAGE: @@ -221,9 +217,15 @@ void main_loop (int argc, char **argv, char **env) printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload); } - TEST_IPC_P (ipc_write (event.origin, m), "server write"); + // m->fd = 3; + TEST_IPC_P (ipc_write (event.clients, m), "server write"); }; break; + case IPC_EVENT_TYPE_ERROR: + { + printf ("a message was sent\n"); + } + break; case IPC_EVENT_TYPE_ERROR: fprintf (stderr, "a problem happened with client %d\n", (event.origin)->fd); break; @@ -240,26 +242,16 @@ void exit_program (int signal) { printf ("Quitting, signal: %d\n", signal); - // free remaining clients - for (size_t i = 0; i < ctx->clients->size; i++) { - struct ipc_connection_info *cli = ctx->clients->cinfos[i]; - if (cli != NULL) { - free (cli); - } - ctx->clients->cinfos[i] = NULL; - } - - ipc_connections_free (ctx->clients); - - // the application will shut down, and close the service - TEST_IPC_P (ipc_server_close (ctx->srv), "server close"); + // Close then free remaining clients. + ipc_close_all (ipcd_ctx->clients); + ipc_ctx_free (ipcd_ctx->clients); // free, free everything! - free (ctx->clients); - free (ctx->srv); - free (ctx->TCP_TO_IPC->collection); - free (ctx->TCP_TO_IPC); - free (ctx); + free (ipcd_ctx->clients); + free (ipcd_ctx->srv); + free (ipcd_ctx->TCP_TO_IPC->collection); + free (ipcd_ctx->TCP_TO_IPC); + free (ipcd_ctx); exit (EXIT_SUCCESS); } @@ -279,17 +271,17 @@ int main (int argc, char *argv[], char **env) printf ("pid = %d\n", getpid ()); - SECURE_BUFFER_HEAP_ALLOCATION_Q (ctx, sizeof (struct networkd) ,, EXIT_FAILURE); - SECURE_BUFFER_HEAP_ALLOCATION_Q (ctx->TCP_TO_IPC, sizeof (struct ipc_switchings) ,, EXIT_FAILURE); - SECURE_BUFFER_HEAP_ALLOCATION_Q (ctx->TCP_TO_IPC->collection, sizeof (struct ipc_switching) ,, EXIT_FAILURE); - SECURE_BUFFER_HEAP_ALLOCATION_Q (ctx->srv, sizeof (struct ipc_connection_info),, EXIT_FAILURE); + SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx, sizeof (struct networkd) ,, EXIT_FAILURE); + SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->TCP_TO_IPC, sizeof (struct ipc_switchings) ,, EXIT_FAILURE); + SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->TCP_TO_IPC->collection, sizeof (struct ipc_switching) ,, EXIT_FAILURE); + SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->srv, sizeof (struct ipc_ctx),, EXIT_FAILURE); - struct ipc_error ret = ipc_server_init (env, ctx->srv, SERVICE_NAME); + struct ipc_error ret = ipc_server_init (env, ipcd_ctx->srv, SERVICE_NAME); if (ret.error_code != IPC_ERROR_NONE) { fprintf (stderr, "%s\n", ret.error_message); return EXIT_FAILURE; } - printf ("Listening on [%s].\n", ctx->srv->spath); + printf ("Listening on [%s].\n", ipcd_ctx->srv->spath); printf ("MAIN: server created\n");