diff --git a/core/client.c b/core/client.c index 1227cd6..46cefc4 100644 --- a/core/client.c +++ b/core/client.c @@ -17,20 +17,20 @@ struct ipc_client * ipc_server_client_copy (const struct ipc_client *p) return copy; } -int ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2) +int32_t ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2) { return (p1->version == p2->version && p1->index == p2->index && p1->proc_fd == p2->proc_fd); } void ipc_server_client_gen (struct ipc_client *p - , unsigned int index, unsigned int version) + , uint32_t index, uint32_t version) { p->version = version; p->index = index; } -int ipc_client_add (struct ipc_clients *clients, struct ipc_client *p) +int32_t ipc_clients_add (struct ipc_clients *clients, struct ipc_client *p) { assert(clients != NULL); assert(p != NULL); @@ -47,7 +47,7 @@ int ipc_client_add (struct ipc_clients *clients, struct ipc_client *p) return 0; } -int ipc_client_del (struct ipc_clients *clients, struct ipc_client *p) +int32_t ipc_clients_del (struct ipc_clients *clients, struct ipc_client *p) { assert(clients != NULL); assert(p != NULL); @@ -56,10 +56,9 @@ int ipc_client_del (struct ipc_clients *clients, struct ipc_client *p) return -1; } - int i; + int32_t i; for (i = 0; i < clients->size; i++) { if (clients->clients[i] == p) { - clients->clients[i] = clients->clients[clients->size-1]; clients->size--; if (clients->size == 0) { @@ -90,7 +89,7 @@ void client_print (struct ipc_client *p) void ipc_clients_print (struct ipc_clients *ap) { - int i; + int32_t i; for (i = 0; i < ap->size; i++) { printf("%d : ", i); client_print(ap->clients[i]); diff --git a/core/client.h b/core/client.h index 08d663c..8366fe0 100644 --- a/core/client.h +++ b/core/client.h @@ -1,29 +1,10 @@ #ifndef __IPC_CLIENT_H__ #define __IPC_CLIENT_H__ -struct ipc_client { - unsigned int version; - unsigned int index; - int proc_fd; -}; - -struct ipc_clients { - struct ipc_client **clients; - int size; -}; - -// store and remove only pointers on allocated structures -int ipc_client_add (struct ipc_clients *, struct ipc_client *); -int ipc_client_del (struct ipc_clients *, struct ipc_client *); +#include +#include "ipc.h" void ipc_clients_print (struct ipc_clients *); -void ipc_clients_free (struct ipc_clients *); - -struct ipc_client * ipc_server_client_copy (const struct ipc_client *p); -int ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2); -// create the service client structure -void ipc_server_client_gen (struct ipc_client *p - , unsigned int index, unsigned int version); - void client_print (struct ipc_client *); + #endif diff --git a/core/communication.c b/core/communication.c index 981fcb4..f56447a 100644 --- a/core/communication.c +++ b/core/communication.c @@ -8,7 +8,7 @@ #include #include -void service_path (char *path, const char *sname, int index, int version) +void service_path (char *path, const char *sname, int32_t index, int32_t version) { assert (path != NULL); assert (sname != NULL); @@ -16,7 +16,7 @@ void service_path (char *path, const char *sname, int index, int version) snprintf (path, PATH_MAX, "%s/%s-%d-%d", RUNDIR, sname, index, version); } -int ipc_server_init (char **env +int32_t ipc_server_init (char **env , struct ipc_service *srv, const char *sname) { if (srv == NULL) @@ -33,7 +33,7 @@ int ipc_server_init (char **env // gets the service path service_path (srv->spath, sname, srv->index, srv->version); - int ret = usock_init (&srv->service_fd, srv->spath); + int32_t ret = usock_init (&srv->service_fd, srv->spath); if (ret < 0) { handle_err ("ipc_server_init", "usock_init ret < 0"); return -1; @@ -42,12 +42,12 @@ int ipc_server_init (char **env return 0; } -int ipc_server_accept (struct ipc_service *srv, struct ipc_client *p) +int32_t ipc_server_accept (struct ipc_service *srv, struct ipc_client *p) { assert (srv != NULL); assert (p != NULL); - int ret = usock_accept (srv->service_fd, &p->proc_fd); + int32_t ret = usock_accept (srv->service_fd, &p->proc_fd); if (ret < 0) { handle_err ("ipc_server_accept", "usock_accept < 0"); return -1; @@ -57,30 +57,30 @@ int ipc_server_accept (struct ipc_service *srv, struct ipc_client *p) } // empty the srv structure -int ipc_server_close (struct ipc_service *srv) +int32_t ipc_server_close (struct ipc_service *srv) { usock_close (srv->service_fd); - int ret = usock_remove (srv->spath); + int32_t ret = usock_remove (srv->spath); ipc_service_empty (srv); return ret; } -int ipc_server_close_client (struct ipc_client *p) +int32_t ipc_server_close_client (struct ipc_client *p) { return usock_close (p->proc_fd); } -int ipc_server_read (const struct ipc_client *p, struct ipc_message *m) +int32_t ipc_server_read (const struct ipc_client *p, struct ipc_message *m) { return ipc_message_read (p->proc_fd, m); } -int ipc_server_write (const struct ipc_client *p, const struct ipc_message *m) +int32_t ipc_server_write (const struct ipc_client *p, const struct ipc_message *m) { return ipc_message_write (p->proc_fd, m); } -int ipc_application_connection (char **env +int32_t ipc_application_connection (char **env , struct ipc_service *srv, const char *sname) { // TODO @@ -101,7 +101,7 @@ int ipc_application_connection (char **env // gets the service path service_path (srv->spath, sname, srv->index, srv->version); - int ret = usock_connect (&srv->service_fd, srv->spath); + int32_t ret = usock_connect (&srv->service_fd, srv->spath); if (ret < 0) { handle_err ("ipc_application_connection", "usock_connect ret <= 0"); return -1; @@ -111,27 +111,27 @@ int ipc_application_connection (char **env } // close the socket -int ipc_application_close (struct ipc_service *srv) +int32_t ipc_application_close (struct ipc_service *srv) { return usock_close (srv->service_fd); } -int ipc_application_read (struct ipc_service *srv, struct ipc_message *m) +int32_t ipc_application_read (struct ipc_service *srv, struct ipc_message *m) { return ipc_message_read (srv->service_fd, m); } -int ipc_application_write (struct ipc_service *srv, const struct ipc_message *m) +int32_t ipc_application_write (struct ipc_service *srv, const struct ipc_message *m) { return ipc_message_write (srv->service_fd, m); } /*calculer le max filedescriptor*/ -static int get_max_fd_from_ipc_clients_ (struct ipc_clients *clients) +static int32_t get_max_fd_from_ipc_clients_ (struct ipc_clients *clients) { - int i; - int max = 0; + int32_t i; + int32_t max = 0; for (i = 0; i < clients->size; i++ ) { if (clients->clients[i]->proc_fd > max) { @@ -142,10 +142,10 @@ static int get_max_fd_from_ipc_clients_ (struct ipc_clients *clients) return max; } -static int get_max_fd_from_ipc_services_ (struct ipc_services *services) +static int32_t get_max_fd_from_ipc_services_ (struct ipc_services *services) { - int i; - int max = 0; + int32_t i; + int32_t max = 0; for (i = 0; i < services->size; i++ ) { if (services->services[i]->service_fd > max) { @@ -166,8 +166,8 @@ static int get_max_fd_from_ipc_services_ (struct ipc_services *services) * -1 = error */ -int ipc_server_select (struct ipc_clients *clients, struct ipc_service *srv - , struct ipc_clients *active_clients, int *new_connection) +int32_t ipc_server_select (struct ipc_clients *clients, struct ipc_service *srv + , struct ipc_clients *active_clients, int32_t *new_connection) { *new_connection = 0; assert (clients != NULL); @@ -176,15 +176,15 @@ int ipc_server_select (struct ipc_clients *clients, struct ipc_service *srv // delete previous read active_clients array ipc_clients_free (active_clients); - int i, j; + int32_t i, j; /* master file descriptor list */ fd_set master; fd_set readf; /* maximum file descriptor number */ - int fdmax; + int32_t fdmax; /* listening socket descriptor */ - int listener = srv->service_fd; + int32_t listener = srv->service_fd; /* clear the master and temp sets */ FD_ZERO(&master); @@ -212,7 +212,7 @@ int ipc_server_select (struct ipc_clients *clients, struct ipc_service *srv } else { for(j = 0; j < clients->size; j++) { if(i == clients->clients[j]->proc_fd ) { - ipc_client_add (active_clients, clients->clients[j]); + ipc_clients_add (active_clients, clients->clients[j]); } } } @@ -232,7 +232,7 @@ int ipc_server_select (struct ipc_clients *clients, struct ipc_service *srv * -1 = error */ -int ipc_application_select (struct ipc_services *services, struct ipc_services *active_services) +int32_t ipc_application_select (struct ipc_services *services, struct ipc_services *active_services) { assert (services != NULL); assert (active_services != NULL); @@ -240,13 +240,13 @@ int ipc_application_select (struct ipc_services *services, struct ipc_services * // delete previous read active_services array ipc_services_free (active_services); - int i, j; + int32_t i, j; /* master file descriptor list */ fd_set master; fd_set readf; /* maximum file descriptor number */ - int fdmax; + int32_t fdmax; /* clear the master and temp sets */ FD_ZERO(&master); @@ -269,7 +269,7 @@ int ipc_application_select (struct ipc_services *services, struct ipc_services * if (FD_ISSET(i, &readf)) { for(j = 0; j < services->size; j++) { if(i == services->services[j]->service_fd ) { - ipc_service_add (active_services, services->services[j]); + ipc_services_add (active_services, services->services[j]); } } } @@ -278,7 +278,7 @@ int ipc_application_select (struct ipc_services *services, struct ipc_services * return 0; } -int handle_new_connection (struct ipc_service *srv +int32_t handle_new_connection (struct ipc_service *srv , struct ipc_clients *clients , struct ipc_client **new_client) { @@ -289,33 +289,33 @@ int handle_new_connection (struct ipc_service *srv handle_error("server_accept < 0"); return 1; } else { - printf("new connection\n"); + // printf("new connection\n"); } - if (ipc_client_add (clients, *new_client) < 0) { - handle_error("ipc_client_add < 0"); + if (ipc_clients_add (clients, *new_client) < 0) { + handle_error("ipc_clients_add < 0"); return 1; } return 0; } -int ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv +int32_t ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv , struct ipc_event *event) { assert (clients != NULL); IPC_EVENT_CLEAN(event); - int i, j; + int32_t i, j; /* master file descriptor list */ fd_set master; fd_set readf; /* maximum file descriptor number */ - int fdmax; + int32_t fdmax; /* listening socket descriptor */ - int listener = srv->service_fd; + int32_t listener = srv->service_fd; /* clear the master and temp sets */ FD_ZERO(&master); @@ -349,7 +349,7 @@ int ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv if(i == clients->clients[j]->proc_fd ) { // listen to what they have to say (disconnection or message) // then add a client to `event`, the ipc_event structure - int ret = 0; + int32_t ret = 0; struct ipc_message *m = NULL; m = malloc (sizeof(struct ipc_message)); if (m == NULL) { @@ -374,8 +374,8 @@ int ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv if (ipc_server_close_client (pc) < 0) { handle_err( "ipc_service_poll_event", "ipc_server_close_client < 0"); } - if (ipc_client_del (clients, pc) < 0) { - handle_err( "ipc_service_poll_event", "ipc_client_del < 0"); + if (ipc_clients_del (clients, pc) < 0) { + handle_err( "ipc_service_poll_event", "ipc_clients_del < 0"); } ipc_message_empty (m); free (m); @@ -398,19 +398,19 @@ int ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv return 0; } -int ipc_application_poll_event_ (struct ipc_services *services, struct ipc_event *event, int interactive) +int32_t ipc_application_poll_event_ (struct ipc_services *services, struct ipc_event *event, int32_t interactive) { assert (services != NULL); IPC_EVENT_CLEAN(event); - int i, j; + int32_t i, j; /* master file descriptor list */ fd_set master; fd_set readf; /* maximum file descriptor number */ - int fdmax; + int32_t fdmax; /* clear the master and temp sets */ FD_ZERO(&master); @@ -455,7 +455,7 @@ int ipc_application_poll_event_ (struct ipc_services *services, struct ipc_event if(i == services->services[j]->service_fd ) { // listen to what they have to say (disconnection or message) // then add a client to `event`, the ipc_event structure - int ret = 0; + int32_t ret = 0; struct ipc_message *m = NULL; m = malloc (sizeof(struct ipc_message)); if (m == NULL) { @@ -480,8 +480,8 @@ int ipc_application_poll_event_ (struct ipc_services *services, struct ipc_event if (ipc_application_close (ps) < 0) { handle_err( "ipc_application_poll_event", "ipc_application_close < 0"); } - if (ipc_service_del (services, ps) < 0) { - handle_err( "ipc_application_poll_event", "ipc_service_del < 0"); + if (ipc_services_del (services, ps) < 0) { + handle_err( "ipc_application_poll_event", "ipc_services_del < 0"); } ipc_message_empty (m); free (m); @@ -504,10 +504,10 @@ int ipc_application_poll_event_ (struct ipc_services *services, struct ipc_event return 0; } -int ipc_application_poll_event (struct ipc_services *services, struct ipc_event *event) { +int32_t ipc_application_poll_event (struct ipc_services *services, struct ipc_event *event) { return ipc_application_poll_event_ (services, event, 0); } -int ipc_application_peek_event (struct ipc_services *services, struct ipc_event *event) { +int32_t ipc_application_peek_event (struct ipc_services *services, struct ipc_event *event) { return ipc_application_poll_event_ (services, event, 1); } diff --git a/core/communication.h b/core/communication.h index 8488c7e..c98ee75 100644 --- a/core/communication.h +++ b/core/communication.h @@ -5,53 +5,16 @@ #include #include #include // error numbers -#include "message.h" + +#include "ipc.h" #include "client.h" #include "event.h" - -#define COMMUNICATION_VERSION 1 +#include "message.h" #define IPC_WITH_UNIX_SOCKETS #ifdef IPC_WITH_UNIX_SOCKETS #include "usocket.h" #endif -// SERVICE - -// srv->version and srv->index must be already set -// init unix socket + fill srv->spath -int ipc_server_init (char **env - , struct ipc_service *srv, const char *sname); -int ipc_server_close (struct ipc_service *srv); -int ipc_server_close_client (struct ipc_client *p); -int ipc_server_accept (struct ipc_service *srv, struct ipc_client *p); - -// 1 on a recipient socket close -int ipc_server_read (const struct ipc_client *, struct ipc_message *m); -int ipc_server_write (const struct ipc_client *, const struct ipc_message *m); - -int ipc_server_select (struct ipc_clients * clients, struct ipc_service *srv - , struct ipc_clients *active_clients, int *new_connection); - -int ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv - , struct ipc_event *event); - -// APPLICATION - -// Initialize connection with unix socket -// send the connection string to $TMP/ -// fill srv->spath && srv->service_fd -int ipc_application_connection (char **env - , struct ipc_service *, const char *); -int ipc_application_close (struct ipc_service *); - -// 1 on a recipient socket close -int ipc_application_read (struct ipc_service *srv, struct ipc_message *m); -int ipc_application_write (struct ipc_service *, const struct ipc_message *m); - -int ipc_application_select (struct ipc_services *services, struct ipc_services *active_services); - -int ipc_application_poll_event (struct ipc_services *services, struct ipc_event *event); -int ipc_application_peek_event (struct ipc_services *services, struct ipc_event *event); #endif diff --git a/core/error.h b/core/error.h index 41b2c6b..cd1c780 100644 --- a/core/error.h +++ b/core/error.h @@ -1,17 +1,10 @@ #ifndef __IPC_ERROR_H__ #define __IPC_ERROR_H__ -#include "logger.h" - -enum ipc_errors { - IPC_ERROR_NOT_ENOUGH_MEMORY - , IPC_ERROR_WRONG_PARAMETERS - , IPC_ERROR_READ -}; - // #define IPC_WITH_ERRORS 3 #ifdef IPC_WITH_ERRORS +#include "logger.h" #define handle_error(msg) \ do { log_error (msg); exit(EXIT_FAILURE); } while (0) diff --git a/core/event.h b/core/event.h index 9ce46eb..eb97be6 100644 --- a/core/event.h +++ b/core/event.h @@ -1,23 +1,9 @@ #ifndef __IPC_EVENT__ #define __IPC_EVENT__ +#include "ipc.h" #include "message.h" -enum ipc_event_type { - IPC_EVENT_TYPE_NOT_SET - , IPC_EVENT_TYPE_ERROR - , IPC_EVENT_TYPE_STDIN - , IPC_EVENT_TYPE_CONNECTION - , IPC_EVENT_TYPE_DISCONNECTION - , IPC_EVENT_TYPE_MESSAGE -}; - -struct ipc_event { - enum ipc_event_type type; - void* origin; // currently used as an client or service pointer - void* m; // message pointer -}; - #define IPC_EVENT_SET(pevent,type_,message_,origin_) {\ pevent->type = type_; \ pevent->m = message_; \ diff --git a/core/ipc.h b/core/ipc.h index 5bd680c..c073a85 100644 --- a/core/ipc.h +++ b/core/ipc.h @@ -1,14 +1,181 @@ #ifndef __IPC_H__ #define __IPC_H__ -#include "client.h" -#include "communication.h" -#include "error.h" -#include "event.h" -#include "logger.h" -#include "message.h" -#include "queue.h" -#include "usocket.h" -#include "utils.h" +#include +#include +#include +#include +#include +#include // error numbers +#include + +#define RUNDIR "/run/ipc/" +#define PATH_MAX 4096 +#define IPC_HEADER_SIZE 5 +#define IPC_MAX_MESSAGE_SIZE 8000000-IPC_HEADER_SIZE +// #define IPC_MAX_MESSAGE_SIZE 100-IPC_HEADER_SIZE +// #include "queue.h" + +#define IPC_VERSION 1 + + +enum msg_types { + MSG_TYPE_SERVER_CLOSE = 0 + , MSG_TYPE_ERR + , MSG_TYPE_DATA +} message_types; + +enum ipc_event_type { + IPC_EVENT_TYPE_NOT_SET + , IPC_EVENT_TYPE_ERROR + , IPC_EVENT_TYPE_STDIN + , IPC_EVENT_TYPE_CONNECTION + , IPC_EVENT_TYPE_DISCONNECTION + , IPC_EVENT_TYPE_MESSAGE +}; + +enum ipc_errors { + IPC_ERROR_NOT_ENOUGH_MEMORY + , IPC_ERROR_WRONG_PARAMETERS + , IPC_ERROR_READ +}; + +struct ipc_service { + uint32_t version; + uint32_t index; + char spath[PATH_MAX]; + int32_t service_fd; +}; + +struct ipc_services { + struct ipc_service ** services; + int32_t size; +}; + +struct ipc_client { + uint32_t version; + uint32_t index; + int32_t proc_fd; +}; + +struct ipc_clients { + struct ipc_client **clients; + int32_t size; +}; + +struct ipc_message { + char type; + uint32_t length; + char *payload; +}; + +struct ipc_event { + enum ipc_event_type type; + void* origin; // currently used as an client or service pointer + void* m; // message pointer +}; + + + +/* + * SERVICE + * + **/ + +// srv->version and srv->index must be already set +// init unix socket + fill srv->spath +int32_t ipc_server_init (char **env , struct ipc_service *srv, const char *sname); +int32_t ipc_server_close (struct ipc_service *srv); +int32_t ipc_server_close_client (struct ipc_client *p); +int32_t ipc_server_accept (struct ipc_service *srv, struct ipc_client *p); + +// 1 on a recipient socket close +int32_t ipc_server_read (const struct ipc_client *, struct ipc_message *m); +int32_t ipc_server_write (const struct ipc_client *, const struct ipc_message *m); + +int32_t ipc_server_select (struct ipc_clients * clients, struct ipc_service *srv + , struct ipc_clients *active_clients, int32_t *new_connection); + +int32_t ipc_service_poll_event (struct ipc_clients *clients, struct ipc_service *srv + , struct ipc_event *event); + +/** + * SERVICES + */ + +// store and remove only pointers on allocated structures +int32_t ipc_services_add (struct ipc_services *, struct ipc_service *); +int32_t ipc_services_del (struct ipc_services *, struct ipc_service *); + +void ipc_services_free (struct ipc_services *); + +struct ipc_service * ipc_client_server_copy (const struct ipc_service *p); +int32_t ipc_service_eq (const struct ipc_service *p1, const struct ipc_service *p2); +// create the client service structure +void ipc_client_server_gen (struct ipc_service *p, uint32_t index, uint32_t version); + +static inline int32_t ipc_service_empty (struct ipc_service *srv) { srv = srv; return 0 ;}; + + +/* + * APPLICATION + * + **/ + +// Initialize connection with unix socket +// send the connection string to $TMP/ +// fill srv->spath && srv->service_fd +int32_t ipc_application_connection (char **env, struct ipc_service *, const char *); +int32_t ipc_application_close (struct ipc_service *); + +// 1 on a recipient socket close +int32_t ipc_application_read (struct ipc_service *srv, struct ipc_message *m); +int32_t ipc_application_write (struct ipc_service *, const struct ipc_message *m); + +int32_t ipc_application_select (struct ipc_services *services, struct ipc_services *active_services); +int32_t ipc_application_poll_event (struct ipc_services *services, struct ipc_event *event); +int32_t ipc_application_peek_event (struct ipc_services *services, struct ipc_event *event); + + + +/* + * MESSAGE + * + **/ + +// used to create msg structure from buffer +int32_t ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize); +// used to create buffer from msg structure +int32_t ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize); + +// read a structure msg from fd +int32_t ipc_message_read (int32_t fd, struct ipc_message *m); +// write a structure msg to fd +int32_t ipc_message_write (int32_t fd, const struct ipc_message *m); + +int32_t ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length); +int32_t ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length); +int32_t ipc_message_format_server_close (struct ipc_message *m); + +int32_t ipc_message_empty (struct ipc_message *m); + + + +/* + * CLIENT + * + **/ + +// store and remove only pointers on allocated structures +int32_t ipc_clients_add (struct ipc_clients *, struct ipc_client *); +int32_t ipc_clients_del (struct ipc_clients *, struct ipc_client *); + +void ipc_clients_free (struct ipc_clients *); + +struct ipc_client * ipc_server_client_copy (const struct ipc_client *p); +int32_t ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2); +// create the service client structure +void ipc_server_client_gen (struct ipc_client *p, uint32_t index, uint32_t version); + #endif diff --git a/core/message.c b/core/message.c index 613f078..f1dcff5 100644 --- a/core/message.c +++ b/core/message.c @@ -12,7 +12,7 @@ void ipc_message_print (const struct ipc_message *m) #endif } -int ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize) +int32_t ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize) { assert (m != NULL); assert (buf != NULL); @@ -43,7 +43,7 @@ int ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msi return 0; } -int ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize) +int32_t ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize) { assert (m != NULL); assert (buf != NULL); @@ -82,14 +82,14 @@ int ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t * } // 1 on a recipient socket close -int ipc_message_read (int fd, struct ipc_message *m) +int32_t ipc_message_read (int32_t fd, struct ipc_message *m) { assert (m != NULL); char *buf = NULL; ssize_t msize = IPC_MAX_MESSAGE_SIZE; - int ret = usock_recv (fd, &buf, &msize); + int32_t ret = usock_recv (fd, &buf, &msize); if (ret < 0) { // on error, buffer already freed handle_err ("msg_read", "usock_recv"); @@ -109,7 +109,7 @@ int ipc_message_read (int fd, struct ipc_message *m) return 0; } -int ipc_message_write (int fd, const struct ipc_message *m) +int32_t ipc_message_write (int32_t fd, const struct ipc_message *m) { assert (m != NULL); @@ -118,7 +118,7 @@ int ipc_message_write (int fd, const struct ipc_message *m) ipc_message_format_write (m, &buf, &msize); ssize_t nbytes_sent = 0; - int ret = usock_send (fd, buf, msize, &nbytes_sent); + int32_t ret = usock_send (fd, buf, msize, &nbytes_sent); if (ret < 0) { if (buf != NULL) { free (buf); @@ -145,7 +145,7 @@ int ipc_message_write (int fd, const struct ipc_message *m) // MSG FORMAT -int ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length) +int32_t ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length) { assert (m != NULL); assert (length <= IPC_MAX_MESSAGE_SIZE); @@ -157,7 +157,7 @@ int ipc_message_format (struct ipc_message *m, char type, const char *payload, s } m->type = type; - m->length = (unsigned int) length; + m->length = (uint32_t) length; if (payload != NULL) { if (m->payload != NULL) { @@ -177,17 +177,17 @@ int ipc_message_format (struct ipc_message *m, char type, const char *payload, s return 0; } -int ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length) +int32_t ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length) { return ipc_message_format (m, MSG_TYPE_DATA, payload, length); } -int ipc_message_format_server_close (struct ipc_message *m) +int32_t ipc_message_format_server_close (struct ipc_message *m) { return ipc_message_format (m, MSG_TYPE_SERVER_CLOSE, NULL, 0); } -int ipc_message_empty (struct ipc_message *m) +int32_t ipc_message_empty (struct ipc_message *m) { assert (m != NULL); diff --git a/core/message.h b/core/message.h index 5f6216a..2aeac2a 100644 --- a/core/message.h +++ b/core/message.h @@ -5,37 +5,9 @@ #include #include -// the underlying communication must always correctly handled by the system -// (currently: unix sockets) +#include -enum msg_types { - MSG_TYPE_SERVER_CLOSE = 0 - , MSG_TYPE_ERR - , MSG_TYPE_DATA -} message_types; - -struct ipc_message { - char type; - unsigned int length; - char *payload; -}; - -// used to create msg structure from buffer -int ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize); -// used to create buffer from msg structure -int ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize); - -// read a structure msg from fd -// 1 on a recipient socket close -int ipc_message_read (int fd, struct ipc_message *m); -// write a structure msg to fd -int ipc_message_write (int fd, const struct ipc_message *m); - -int ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length); -int ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length); -int ipc_message_format_server_close (struct ipc_message *m); - -int ipc_message_empty (struct ipc_message *m); +#include "ipc.h" void ipc_message_print (const struct ipc_message *m); #endif diff --git a/core/usocket.c b/core/usocket.c index ce15cb6..d553c66 100644 --- a/core/usocket.c +++ b/core/usocket.c @@ -8,7 +8,7 @@ #include #include -int usock_send (const int fd, const char *buf, ssize_t len, ssize_t *sent) +int32_t usock_send (const int32_t fd, const char *buf, ssize_t len, ssize_t *sent) { ssize_t ret = 0; ret = send (fd, buf, len, MSG_NOSIGNAL); @@ -21,7 +21,7 @@ int usock_send (const int fd, const char *buf, ssize_t len, ssize_t *sent) } // *len is changed to the total message size read (header + payload) -int usock_recv (const int fd, char **buf, ssize_t *len) +int32_t usock_recv (const int32_t fd, char **buf, ssize_t *len) { assert(buf != NULL); assert(len != NULL); @@ -47,8 +47,8 @@ int usock_recv (const int fd, char **buf, ssize_t *len) *buf = malloc (*len + IPC_HEADER_SIZE); } - unsigned int msize = 0; - unsigned int msize_read = 0; + uint32_t msize = 0; + uint32_t msize_read = 0; do { ret = recv (fd, *buf, *len, 0); @@ -135,12 +135,12 @@ int usock_recv (const int fd, char **buf, ssize_t *len) return 1; } - // print_hexa ("msg recv", (unsigned char *)*buf, *len); + // print_hexa ("msg recv", (uint8_t *)*buf, *len); // fflush(stdout); return 0; } -int usock_connect (int *fd, const char *path) +int32_t usock_connect (int32_t *fd, const char *path) { assert (fd != NULL); assert (path != NULL); @@ -155,7 +155,7 @@ int usock_connect (int *fd, const char *path) return -1; } - int sfd; + int32_t sfd; struct sockaddr_un my_addr; socklen_t peer_addr_size; @@ -183,7 +183,7 @@ int usock_connect (int *fd, const char *path) return 0; } -int usock_init (int *fd, const char *path) +int32_t usock_init (int32_t *fd, const char *path) { assert (fd != NULL); assert (path != NULL); @@ -198,7 +198,7 @@ int usock_init (int *fd, const char *path) return -1; } - int sfd; + int32_t sfd; struct sockaddr_un my_addr; socklen_t peer_addr_size; @@ -236,7 +236,7 @@ int usock_init (int *fd, const char *path) return 0; } -int usock_accept (int fd, int *pfd) +int32_t usock_accept (int32_t fd, int32_t *pfd) { assert (pfd != NULL); @@ -259,9 +259,9 @@ int usock_accept (int fd, int *pfd) return 0; } -int usock_close (int fd) +int32_t usock_close (int32_t fd) { - int ret = 0; + int32_t ret = 0; ret = close (fd); if (ret < 0) { @@ -272,7 +272,7 @@ int usock_close (int fd) return 0; } -int usock_remove (const char *path) +int32_t usock_remove (const char *path) { return unlink (path); } @@ -292,20 +292,20 @@ struct ipc_service * ipc_client_server_copy (const struct ipc_service *p) return copy; } -int ipc_client_server_eq (const struct ipc_service *p1, const struct ipc_service *p2) +int32_t ipc_client_server_eq (const struct ipc_service *p1, const struct ipc_service *p2) { return (p1->version == p2->version && p1->index == p2->index && p1->service_fd == p2->service_fd && memcmp(p1->spath, p1->spath, PATH_MAX) == 0 ); } void ipc_client_server_gen (struct ipc_service *p - , unsigned int index, unsigned int version) + , uint32_t index, uint32_t version) { p->version = version; p->index = index; } -int ipc_service_add (struct ipc_services *services, struct ipc_service *p) +int32_t ipc_services_add (struct ipc_services *services, struct ipc_service *p) { assert(services != NULL); assert(p != NULL); @@ -322,7 +322,7 @@ int ipc_service_add (struct ipc_services *services, struct ipc_service *p) return 0; } -int ipc_service_del (struct ipc_services *services, struct ipc_service *p) +int32_t ipc_services_del (struct ipc_services *services, struct ipc_service *p) { assert(services != NULL); assert(p != NULL); @@ -331,7 +331,7 @@ int ipc_service_del (struct ipc_services *services, struct ipc_service *p) return -1; } - int i; + int32_t i; for (i = 0; i < services->size; i++) { if (services->services[i] == p) { @@ -365,7 +365,7 @@ void service_print (struct ipc_service *p) void ipc_services_print (struct ipc_services *ap) { - int i; + int32_t i; for (i = 0; i < ap->size; i++) { printf("%d : ", i); service_print(ap->services[i]); diff --git a/core/usocket.h b/core/usocket.h index 4dbc8e5..370eebb 100644 --- a/core/usocket.h +++ b/core/usocket.h @@ -5,33 +5,18 @@ #include #include +#include "ipc.h" +#include + #define LISTEN_BACKLOG 128 -#define RUNDIR "/run/ipc/" -#define PATH_MAX 4096 - -#define IPC_HEADER_SIZE 5 -#define IPC_MAX_MESSAGE_SIZE 8000000-IPC_HEADER_SIZE - -struct ipc_service { - unsigned int version; - unsigned int index; - char spath[PATH_MAX]; - int service_fd; -}; - -struct ipc_services { - struct ipc_service ** services; - int size; -}; - /** * for all functions: 0 ok, < 0 not ok */ // input: len = max buf size // output: *sent = nb received bytes -int usock_send (const int fd, const char *buf, ssize_t len, ssize_t *sent); +int32_t usock_send (const int32_t fd, const char *buf, ssize_t len, ssize_t *sent); // -1 on msize == NULL or buf == NULL // -1 on unsupported errors from read(2) @@ -40,38 +25,23 @@ int usock_send (const int fd, const char *buf, ssize_t len, ssize_t *sent); // allocation of *len bytes on *buf == NULL // // output: *len = nb sent bytes -int usock_recv (int fd, char **buf, ssize_t *len); +int32_t usock_recv (int32_t fd, char **buf, ssize_t *len); // -1 on close(2) < 0 -int usock_close (int fd); +int32_t usock_close (int32_t fd); // same as connect(2) // -1 on fd == NULL -int usock_connect (int *fd, const char *path); +int32_t usock_connect (int32_t *fd, const char *path); -int usock_init (int *fd, const char *path); +int32_t usock_init (int32_t *fd, const char *path); -int usock_accept (int fd, int *pfd); +int32_t usock_accept (int32_t fd, int32_t *pfd); // same as unlink(2) -int usock_remove (const char *path); - -static inline int ipc_service_empty (struct ipc_service *srv) { srv = srv; return 0 ;}; - - -// store and remove only pointers on allocated structures -int ipc_service_add (struct ipc_services *, struct ipc_service *); -int ipc_service_del (struct ipc_services *, struct ipc_service *); +int32_t usock_remove (const char *path); void ipc_services_print (struct ipc_services *); -void ipc_services_free (struct ipc_services *); - -struct ipc_service * ipc_client_server_copy (const struct ipc_service *p); -int ipc_service_eq (const struct ipc_service *p1, const struct ipc_service *p2); -// create the client service structure -void ipc_client_server_gen (struct ipc_service *p - , unsigned int index, unsigned int version); - void service_print (struct ipc_service *); #endif diff --git a/core/utils.c b/core/utils.c index fe97b83..d6c5f37 100644 --- a/core/utils.c +++ b/core/utils.c @@ -1,6 +1,6 @@ #include "utils.h" -void print_hexa (const char *prefix, unsigned char *payload, size_t size) +void print_hexa (const char *prefix, uint8_t *payload, size_t size) { if (! payload) return ; @@ -16,7 +16,7 @@ void print_hexa (const char *prefix, unsigned char *payload, size_t size) } -void mprint_hexa (char *prefix, unsigned char *buf, size_t length) +void mprint_hexa (char *prefix, uint8_t *buf, size_t length) { print_hexa (prefix, buf, length); } diff --git a/core/utils.h b/core/utils.h index 486575d..352b78f 100644 --- a/core/utils.h +++ b/core/utils.h @@ -5,7 +5,9 @@ #include #include -void print_hexa (const char *prefix, unsigned char *payload, size_t size); -void mprint_hexa (char *prefix, unsigned char *buf, size_t length); +#include + +void print_hexa (const char *prefix, uint8_t *payload, size_t size); +void mprint_hexa (char *prefix, uint8_t *buf, size_t length); #endif diff --git a/drop/init-connection.c b/drop/init-connection.c index 8cfb268..16f28aa 100644 --- a/drop/init-connection.c +++ b/drop/init-connection.c @@ -3,12 +3,12 @@ #define SERVICE "windowing" void -ohshit(int rvalue, const char* str) { +ohshit(int32_t rvalue, const char* str) { fprintf(stderr, "%s\n", str); exit(rvalue); } -int main(int argc, char * argv[], char *env[]) +int32_t main(int32_t argc, char * argv[], char *env[]) { struct ipc_service srv; memset (&srv, 0, sizeof (struct ipc_service)); @@ -26,8 +26,8 @@ int main(int argc, char * argv[], char *env[]) struct ipc_client p; memset (&p, 0, sizeof (struct ipc_client)); - int index = 0; // first time we communication with the service - int version = 1; + int32_t index = 0; // first time we communication with the service + int32_t version = 1; printf ("app creation\n"); if (application_create (&p, index, version)) // called by the application diff --git a/drop/list.c b/drop/list.c index 71dbda0..501e6bd 100644 --- a/drop/list.c +++ b/drop/list.c @@ -7,11 +7,11 @@ LIST_HEAD(mlist, node); // elements structure of the list struct node { - int content; + int32_t content; LIST_ENTRY(node) entries; }; -int main(int argc, char * argv[]) +int32_t main(int32_t argc, char * argv[]) { (void) argc; (void) argv; diff --git a/drop/msg.c b/drop/msg.c index 4452934..1edde58 100644 --- a/drop/msg.c +++ b/drop/msg.c @@ -8,7 +8,7 @@ #define PKT_ERROR 2 void -ohshit(int rvalue, const char* str) { +ohshit(int32_t rvalue, const char* str) { fprintf (stderr, "\033[31merr: %s\033[00m\n", str); exit (rvalue); } @@ -21,15 +21,15 @@ void usage (char **argv) printf ( " This sends a CBOR msg [ 1, \"data\" ]\n"); } -int -main(int argc, char **argv) +int32_t +main(int32_t argc, char **argv) { if (argc == 2 && strcmp ("-h", argv[1]) == 0) { usage (argv); exit (1); } - unsigned char buf[BUFSIZ]; + uint8_t buf[BUFSIZ]; memset (buf, 0, BUFSIZ); ssize_t buflen = read (0, buf, BUFSIZ); @@ -42,7 +42,7 @@ main(int argc, char **argv) .value = cbor_move(cbor_build_bytestring(buf, buflen)) }); /* Output: `length` bytes of data in the `buffer` */ - unsigned char * buffer; + uint8_t * buffer; size_t buffer_size, length = cbor_serialize_alloc (root, &buffer, &buffer_size); fwrite(buffer, 1, length, stdout); diff --git a/drop/open-read-close-fifo.c b/drop/open-read-close-fifo.c index ca2f396..ada2cd1 100644 --- a/drop/open-read-close-fifo.c +++ b/drop/open-read-close-fifo.c @@ -3,7 +3,7 @@ #include #include -int main(int argc, char * argv[]) +int32_t main(int32_t argc, char * argv[]) { (void) argc; diff --git a/drop/open-write-close-fifo.c b/drop/open-write-close-fifo.c index ca4c950..d8e94e4 100644 --- a/drop/open-write-close-fifo.c +++ b/drop/open-write-close-fifo.c @@ -3,7 +3,7 @@ #include #include -int main(int argc, char * argv[]) +int32_t main(int32_t argc, char * argv[]) { (void) argc; (void) argv; diff --git a/drop/pass-socket_client.c b/drop/pass-socket_client.c index ebe3d8d..3fa2d76 100644 --- a/drop/pass-socket_client.c +++ b/drop/pass-socket_client.c @@ -64,7 +64,7 @@ void log_debug (const char* message, ...) { } static -int recvsockfd (int socket) // receive fd from socket +int32_t recvsockfd (int32_t socket) // receive fd from socket { struct ipc_messagehdr msg = {0}; @@ -84,14 +84,14 @@ int recvsockfd (int socket) // receive fd from socket struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); printf ("About to extract fd\n"); - int fd; + int32_t fd; memmove(&fd, CMSG_DATA(cmsg), sizeof(fd)); printf ("Extracted fd %d\n", fd); return fd; } -int usock_connect (int *fd, const char *path) +int32_t usock_connect (int32_t *fd, const char *path) { assert (fd != NULL); assert (path != NULL); @@ -106,7 +106,7 @@ int usock_connect (int *fd, const char *path) return -1; } - int sfd; + int32_t sfd; struct sockaddr_un my_addr; socklen_t peer_addr_size; @@ -134,7 +134,7 @@ int usock_connect (int *fd, const char *path) return 0; } -int usock_init (int *fd, const char *path) +int32_t usock_init (int32_t *fd, const char *path) { assert (fd != NULL); assert (path != NULL); @@ -149,7 +149,7 @@ int usock_init (int *fd, const char *path) return -1; } - int sfd; + int32_t sfd; struct sockaddr_un my_addr; socklen_t peer_addr_size; @@ -187,7 +187,7 @@ int usock_init (int *fd, const char *path) return 0; } -int usock_accept (int fd, int *pfd) +int32_t usock_accept (int32_t fd, int32_t *pfd) { assert (pfd != NULL); @@ -210,9 +210,9 @@ int usock_accept (int fd, int *pfd) return 0; } -int usock_close (int fd) +int32_t usock_close (int32_t fd) { - int ret; + int32_t ret; ret = close (fd); if (ret < 0) { handle_err ("usock_close", "close ret < 0"); @@ -221,15 +221,15 @@ int usock_close (int fd) return ret; } -int usock_remove (const char *path) +int32_t usock_remove (const char *path) { return unlink (path); } -int main(int argc, char * argv[]) +int32_t main(int32_t argc, char * argv[]) { - int tcpsockfd; - int usockfd; + int32_t tcpsockfd; + int32_t usockfd; // check the number of args on command line if(argc != 1) { @@ -240,7 +240,7 @@ int main(int argc, char * argv[]) printf("Connection to the unix socket\n"); // 1. unix socket connection - int ret = usock_connect (&usockfd, USOCK); + int32_t ret = usock_connect (&usockfd, USOCK); if (ret != 0) { fprintf (stderr, "error: usock_connect\n"); exit(EXIT_FAILURE); diff --git a/drop/pass-socket_server.c b/drop/pass-socket_server.c index e1259f5..19d0002 100644 --- a/drop/pass-socket_server.c +++ b/drop/pass-socket_server.c @@ -63,9 +63,9 @@ void log_debug (const char* message, ...) { va_end(args); } -int build_socket (char *servername, char * serverport) +int32_t build_socket (char *servername, char * serverport) { - int sockfd; + int32_t sockfd; struct sockaddr_in6 server; socklen_t addrlen; @@ -99,7 +99,7 @@ int build_socket (char *servername, char * serverport) return sockfd; } -int usock_connect (int *fd, const char *path) +int32_t usock_connect (int32_t *fd, const char *path) { assert (fd != NULL); assert (path != NULL); @@ -114,7 +114,7 @@ int usock_connect (int *fd, const char *path) return -1; } - int sfd; + int32_t sfd; struct sockaddr_un my_addr; socklen_t peer_addr_size; @@ -142,7 +142,7 @@ int usock_connect (int *fd, const char *path) return 0; } -int usock_init (int *fd, const char *path) +int32_t usock_init (int32_t *fd, const char *path) { assert (fd != NULL); assert (path != NULL); @@ -157,7 +157,7 @@ int usock_init (int *fd, const char *path) return -1; } - int sfd; + int32_t sfd; struct sockaddr_un my_addr; socklen_t peer_addr_size; @@ -195,7 +195,7 @@ int usock_init (int *fd, const char *path) return 0; } -int usock_accept (int fd, int *pfd) +int32_t usock_accept (int32_t fd, int32_t *pfd) { assert (pfd != NULL); @@ -218,9 +218,9 @@ int usock_accept (int fd, int *pfd) return 0; } -int usock_close (int fd) +int32_t usock_close (int32_t fd) { - int ret; + int32_t ret; ret = close (fd); if (ret < 0) { handle_err ("usock_close", "close ret < 0"); @@ -229,14 +229,14 @@ int usock_close (int fd) return ret; } -int usock_remove (const char *path) +int32_t usock_remove (const char *path) { return unlink (path); } -int build_unix_socket (char * path) +int32_t build_unix_socket (char * path) { - int remotefd, localfd; + int32_t remotefd, localfd; usock_init (&localfd, path); usock_accept (localfd, &remotefd); @@ -245,7 +245,7 @@ int build_unix_socket (char * path) } static -void sendfd (int socket, int fd) // send fd by socket +void sendfd (int32_t socket, int32_t fd) // send fd by socket { struct ipc_messagehdr msg = { 0 }; char buf[CMSG_SPACE(sizeof(fd))]; @@ -272,7 +272,7 @@ void sendfd (int socket, int fd) // send fd by socket handle_err("sendfd", "Failed to send message\n"); } -int main(int argc, char * argv[]) +int32_t main(int32_t argc, char * argv[]) { // check the number of args on command line if(argc != 3) @@ -285,7 +285,7 @@ int main(int argc, char * argv[]) printf("Connection to the tcp socket\n"); // 1. socket creation (tcp), connection to the server - int sockfd = build_socket (servername, serverport); + int32_t sockfd = build_socket (servername, serverport); printf("Sending 'coucou' to the tcp socket\n"); // send a message to check the connection is effective @@ -297,7 +297,7 @@ int main(int argc, char * argv[]) printf ("Connection to the unix socket\n"); // 2. socket creation (unix) - int usockfd = build_unix_socket (USOCK); + int32_t usockfd = build_unix_socket (USOCK); printf ("Passing the tcp socket to the unix socket\n"); // 3. tcp socket passing to the client diff --git a/drop/readmsg.c b/drop/readmsg.c index fb170e9..cb16edb 100644 --- a/drop/readmsg.c +++ b/drop/readmsg.c @@ -12,7 +12,7 @@ void usage (char **argv) { printf ("usage: echo something | msg | %s\n", argv[0]); } -int main(int argc, char * argv[]) +int32_t main(int32_t argc, char * argv[]) { if (argc == 2 && strcmp ("-h", argv[1]) == 0) { usage (argv); @@ -21,7 +21,7 @@ int main(int argc, char * argv[]) // read the message from the client size_t mlen = 0; - unsigned char buf[BUFSIZ]; + uint8_t buf[BUFSIZ]; mlen = read (0, buf, BUFSIZ); /* Assuming `buffer` contains `info.st_size` bytes of input data */ diff --git a/drop/tcpselect.c b/drop/tcpselect.c index e1c2479..0065828 100644 --- a/drop/tcpselect.c +++ b/drop/tcpselect.c @@ -11,7 +11,7 @@ #define PORT 2020 -int main(int argc, char *argv[]) +int32_t main(int32_t argc, char *argv[]) { /* master file descriptor list */ fd_set master; @@ -23,19 +23,19 @@ int main(int argc, char *argv[]) struct sockaddr_in clientaddr; /* maximum file descriptor number */ - int fdmax; + int32_t fdmax; /* listening socket descriptor */ - int listener; + int32_t listener; /* newly accept()ed socket descriptor */ - int newfd; + int32_t newfd; /* buffer for client data */ char buf[1024]; - int nbytes; + int32_t nbytes; /* for setsockopt() SO_REUSEADDR, below */ - int yes = 1; - int addrlen; - int i, j; + int32_t yes = 1; + int32_t addrlen; + int32_t i, j; /* clear the master and temp sets */ FD_ZERO(&master); @@ -50,7 +50,7 @@ int main(int argc, char *argv[]) printf("Server-socket() is OK...\n"); /*"address already in use" error message */ - if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) + if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int32_t)) == -1) { perror("Server-setsockopt() error lol!"); exit(1); diff --git a/man/communication.h.3 b/man/communication.h.3 index ccdb14c..f3b9db5 100644 --- a/man/communication.h.3 +++ b/man/communication.h.3 @@ -5,25 +5,25 @@ communication.h \- all functions explained .nf .B #include .sp -.BI "int ipc_server_init (int "argc ", char **" argv ", char **" env ", struct ipc_service *" srv " +.BI "int32_t ipc_server_init (int32_t "argc ", char **" argv ", char **" env ", struct ipc_service *" srv " .BI " , const char *" service_name ); -.BI "int ipc_server_accept (struct ipc_service *" srv ", struct ipc_client *" p ); +.BI "int32_t ipc_server_accept (struct ipc_service *" srv ", struct ipc_client *" p ); .sp -.BI "int ipc_server_read (const struct ipc_client *" p ", struct ipc_message *" message ); -.BI "int ipc_server_write (const struct ipc_client *" p ", const struct ipc_message *" message ); +.BI "int32_t ipc_server_read (const struct ipc_client *" p ", struct ipc_message *" message ); +.BI "int32_t ipc_server_write (const struct ipc_client *" p ", const struct ipc_message *" message ); .sp -.BI "int ipc_server_close (struct ipc_service *" srv ); -.BI "int ipc_server_close_client (struct ipc_client *" p ); -.BI "int ipc_server_select (struct ipc_clients *" fds ", struct ipc_service *" srv ", struct ipc_clients *" readfds ); +.BI "int32_t ipc_server_close (struct ipc_service *" srv ); +.BI "int32_t ipc_server_close_client (struct ipc_client *" p ); +.BI "int32_t ipc_server_select (struct ipc_clients *" fds ", struct ipc_service *" srv ", struct ipc_clients *" readfds ); -.BI "int ipc_application_connection (int " argc ", char **" argv ", char **" env ", struct ipc_service *" srv +.BI "int32_t ipc_application_connection (int32_t " argc ", char **" argv ", char **" env ", struct ipc_service *" srv .BI " , const char *" service_name " .BI " , const char *" connection_buffer ", size_t " bufsize ); .sp -.BI "int ipc_application_read (const struct ipc_service *" srv ", struct ipc_message *" message ); -.BI "int ipc_application_write (const struct ipc_service *" srv ", const struct ipc_message *" message ); +.BI "int32_t ipc_application_read (const struct ipc_service *" srv ", struct ipc_message *" message ); +.BI "int32_t ipc_application_write (const struct ipc_service *" srv ", const struct ipc_message *" message ); .sp -.BI "int ipc_application_close (struct ipc_service *" srv ); +.BI "int32_t ipc_application_close (struct ipc_service *" srv ); .fi diff --git a/pong/app/ipc-debug.c b/pong/app/ipc-debug.c index d4b4d14..28701a9 100644 --- a/pong/app/ipc-debug.c +++ b/pong/app/ipc-debug.c @@ -5,6 +5,8 @@ #include #include +#include + #include "../../core/communication.h" #include "../../core/error.h" @@ -35,7 +37,7 @@ void interactive (char * service_name, char *env[]) struct ipc_services services; memset (&services, 0, sizeof (struct ipc_services)); - ipc_service_add (&services, &srv); + ipc_services_add (&services, &srv); int ret = 0; while (1) { diff --git a/pong/app/pong.c b/pong/app/pong.c index f863238..3a458f6 100644 --- a/pong/app/pong.c +++ b/pong/app/pong.c @@ -4,6 +4,7 @@ #include #include "../../core/ipc.h" +#include "../../core/error.h" #define MSG "coucou" #define SERVICE_NAME "pongd" @@ -70,7 +71,7 @@ void interactive (char *env[]) struct ipc_services services; memset (&services, 0, sizeof (struct ipc_services)); - ipc_service_add (&services, &srv); + ipc_services_add (&services, &srv); while (1) { printf ("msg to send: "); diff --git a/pong/app/pongd.c b/pong/app/pongd.c index 0c09780..8d83e45 100644 --- a/pong/app/pongd.c +++ b/pong/app/pongd.c @@ -1,4 +1,5 @@ #include "../../core/ipc.h" +#include "../../core/error.h" #include #include @@ -6,6 +7,7 @@ #include #define PONGD_SERVICE_NAME "pongd" +#define PONGD_PRINT_MESSAGES int cpt = 0; @@ -41,14 +43,18 @@ void main_loop () case IPC_EVENT_TYPE_CONNECTION: { cpt++; - printf ("connection: %d clients connected\n", cpt); - printf ("new client has the fd %d\n", ((struct ipc_client*) event.origin)->proc_fd); +#ifdef PONGD_PRINT_MESSAGES + printf ("connection: client fd %d, %d clients connected\n" + , ((struct ipc_client*) event.origin)->proc_fd, cpt); +#endif }; break; case IPC_EVENT_TYPE_DISCONNECTION: { cpt--; +#ifdef PONGD_PRINT_MESSAGES printf ("disconnection: %d clients remaining\n", cpt); +#endif // free the ipc_client structure free (event.origin); @@ -57,9 +63,11 @@ void main_loop () case IPC_EVENT_TYPE_MESSAGE: { struct ipc_message *m = event.m; +#ifdef PONGD_PRINT_MESSAGES if (m->length > 0) { printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload); } +#endif if (ipc_server_write (event.origin, m) < 0) { handle_err( "handle_new_msg", "server_write < 0"); } diff --git a/pong/app/test-exchange.c b/pong/app/test-exchange.c new file mode 100644 index 0000000..b90f67a --- /dev/null +++ b/pong/app/test-exchange.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + +#include + +#include "../../core/communication.h" +#include "../../core/error.h" + +#define SERVICE_NAME "pongd" + +#define NUMBER_OF_MESSAGES 1000 +#define MAX_MESSAGE_SIZE IPC_MAX_MESSAGE_SIZE +#define MESSAGE "salut ça va ?" + +void non_interactive (char msg_type, char *msg, char * service_name, char *env[]) +{ + struct ipc_message m; + memset (&m, 0, sizeof (struct ipc_message)); + struct ipc_service srv; + memset (&srv, 0, sizeof (struct ipc_service)); + + // index and version should be filled + srv.index = 0; + srv.version = 0; + + // init service + if (ipc_application_connection (env, &srv, service_name) < 0) { + handle_err ("main", "ipc_application_connection < 0"); + exit (EXIT_FAILURE); + } + + for (int i = 0 ; i < NUMBER_OF_MESSAGES ; i++) { + ipc_message_format (&m, msg_type, msg, strlen(msg) + 1); + // print_msg (&m); + + if (ipc_application_write (&srv, &m) < 0) { + handle_err("main", "application_write < 0"); + exit (EXIT_FAILURE); + } + ipc_message_empty (&m); + + if (ipc_application_read (&srv, &m) < 0) { + handle_err("main", "application_read < 0"); + exit (EXIT_FAILURE); + } + +#ifdef WITH_PRINT_MESSAGES + if (m.length > 0) { + printf ("msg recv: %.*s\n", m.length, m.payload); + } +#endif + ipc_message_empty (&m); + } + + if (ipc_application_close (&srv) < 0) { + handle_err("main", "application_close < 0"); + exit (EXIT_FAILURE); + } + ipc_message_empty (&m); +} + +// usage: ipc-debug [service-name] +int main (int argc, char *argv[], char *env[]) +{ + if (argc == 1) { + printf ("usage: %s service_name [message-type [message]]\n", argv[0]); + exit (EXIT_SUCCESS); + } + + char service_name[100]; + memset (service_name, 0, 100); + + int current_param = 1; + + if (argc != 1) { + ssize_t t = strlen(argv[current_param]) > 100 ? 100 : strlen(argv[current_param]); + memcpy(service_name, argv[current_param], t); + current_param++; + } + else { memcpy(service_name, SERVICE_NAME, strlen(SERVICE_NAME)); } + + char mtype = 2; + if (argc > 2) { + mtype = atoi(argv[current_param]); + current_param++; + } + + char *msg = malloc (MAX_MESSAGE_SIZE); + if (msg == NULL) { + handle_err("main", "not enough memory"); + exit (EXIT_FAILURE); + } + memset(msg, 0, MAX_MESSAGE_SIZE); + + if (argc > 3) { memcpy(msg, argv[current_param], strlen(argv[current_param])); } + else { memcpy(msg, MESSAGE, strlen(MESSAGE)); } + + non_interactive (mtype, msg, service_name, env); + free (msg); + + return EXIT_SUCCESS; +}