diff --git a/core-test/app/Makefile b/core-test/app/Makefile new file mode 100644 index 0000000..c398f8c --- /dev/null +++ b/core-test/app/Makefile @@ -0,0 +1,25 @@ +CC=gcc +CFLAGS=-Wall -g -Wextra +LDFLAGS= -pthread +CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change +EXEC=$(basename $(wildcard *.c)) +SOURCES=$(wildcard ../lib/*.c ../../core/*.c) +OBJECTS=$(SOURCES:.c=.o) +TESTS=$(addsuffix .test, $(EXEC)) + +all: $(SOURCES) $(EXEC) + +$(EXEC): $(OBJECTS) $(CFILES) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin + +.c.o: + $(CC) -c $(CFLAGS) $< -o $@ + +$(TESTS): + valgrind --show-leak-kinds=all --leak-check=full -v --track-origins=yes ./$(basename $@) + +clean: + @-rm $(OBJECTS) + +mrproper: clean + @-rm *.bin diff --git a/core-test/app/benchmark_usock-client.c b/core-test/app/benchmark_usock-client.c new file mode 100644 index 0000000..ef6fd98 --- /dev/null +++ b/core-test/app/benchmark_usock-client.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "../../core/usocket.h" + +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + +#define UPATH "/tmp/ipc/usock-path.sock" +#define MSG "coucou" + +int main (int argc, char * argv[]) +{ + + argc = argc; + argv = argv; + + int fd; + size_t msize = BUFSIZ; + char *buf = NULL; + + struct timeval t1; + struct timeval t2; + + if ( (buf = malloc (BUFSIZ)) == NULL) { + handle_err ("main", "malloc"); + return EXIT_FAILURE; + } + memset (buf, 0, BUFSIZ); + + if (usock_connect (&fd, UPATH) < 0) { + handle_err("main", "usock_listen < 0"); + return EXIT_FAILURE; + } + + gettimeofday (&t1, NULL); + if (usock_send (fd, MSG, strlen(MSG)) < 0) { + handle_err("main", "usock_send < 0"); + return EXIT_FAILURE; + } + + if (usock_recv (fd, &buf, &msize) < 0) { + handle_err("main", "usock_recv < 0"); + return EXIT_FAILURE; + } + gettimeofday (&t2, NULL); + + printf ("it took %ld µs to send then recv a message\n" + , t2.tv_usec - t1.tv_usec); + + if (usock_close (fd) < 0) { + handle_err("main", "usock_close < 0"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/core-test/app/benchmark_usock-server.c b/core-test/app/benchmark_usock-server.c new file mode 100644 index 0000000..a50bd8f --- /dev/null +++ b/core-test/app/benchmark_usock-server.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "../../core/usocket.h" + +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + +#define UPATH "/tmp/ipc/usock-path.sock" + +int main (int argc, char * argv[]) +{ + + argc = argc; + argv = argv; + + int fd; + int pfd; + size_t msize = BUFSIZ; + char *buf = NULL; + + if ( (buf = malloc (BUFSIZ)) == NULL) { + handle_err ("main", "malloc"); + return EXIT_FAILURE; + } + memset (buf, 0, BUFSIZ); + + // socket + bind + listen + if (usock_init (&fd, UPATH) < 0) { + handle_err("main", "usock_init < 0"); + return EXIT_FAILURE; + } + + if (usock_accept (fd, &pfd) < 0) { + handle_err("main", "usock_accept < 0"); + return EXIT_FAILURE; + } + + if (usock_recv (pfd, &buf, &msize) < 0) { + handle_err("main", "usock_recv < 0"); + return EXIT_FAILURE; + } + + if (usock_send (pfd, buf, msize) < 0) { + handle_err("main", "usock_send < 0"); + return EXIT_FAILURE; + } + + if (usock_close (fd) < 0) { + handle_err("main", "usock_close fd < 0"); + return EXIT_FAILURE; + } + + if (usock_close (pfd) < 0) { + handle_err("main", "usock_close pfd < 0"); + return EXIT_FAILURE; + } + + if (usock_remove (UPATH) < 0) { + handle_err("main", "usock_remove < 0"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/core-test/app/usock-client.c b/core-test/app/usock-client.c new file mode 100644 index 0000000..1ce6514 --- /dev/null +++ b/core-test/app/usock-client.c @@ -0,0 +1,52 @@ +#include +#include +#include + +#include "../../core/usocket.h" + +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + +#define UPATH "/tmp/ipc/usock-path.sock" +#define MSG "coucou" + +int main (int argc, char * argv[]) +{ + + argc = argc; + argv = argv; + + int fd; + size_t msize = BUFSIZ; + char *buf = NULL; + + if ( (buf = malloc (BUFSIZ)) == NULL) { + handle_err ("main", "malloc"); + return EXIT_FAILURE; + } + memset (buf, 0, BUFSIZ); + + if (usock_connect (&fd, UPATH) < 0) { + handle_err("main", "usock_listen < 0"); + return EXIT_FAILURE; + } + + if (usock_send (fd, MSG, strlen(MSG)) < 0) { + handle_err("main", "usock_send < 0"); + return EXIT_FAILURE; + } + + if (usock_recv (fd, &buf, &msize) < 0) { + handle_err("main", "usock_recv < 0"); + return EXIT_FAILURE; + } + + printf ("msg recv: %s\n", buf); + + if (usock_close (fd) < 0) { + handle_err("main", "usock_close < 0"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/core-test/app/usock-server.c b/core-test/app/usock-server.c new file mode 100644 index 0000000..ce3f1ea --- /dev/null +++ b/core-test/app/usock-server.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#include "../../core/usocket.h" + +#define handle_err(fun,msg)\ + fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg); + +#define UPATH "/tmp/ipc/usock-path.sock" + +int main (int argc, char * argv[]) +{ + + argc = argc; + argv = argv; + + int fd; + int pfd; + size_t msize = BUFSIZ; + char *buf = NULL; + + if ( (buf = malloc (BUFSIZ)) == NULL) { + handle_err ("main", "malloc"); + return EXIT_FAILURE; + } + memset (buf, 0, BUFSIZ); + + // socket + bind + listen + if (usock_init (&fd, UPATH) < 0) { + handle_err("main", "usock_init < 0"); + return EXIT_FAILURE; + } + + if (usock_accept (fd, &pfd) < 0) { + handle_err("main", "usock_accept < 0"); + return EXIT_FAILURE; + } + + printf ("new connection\n"); + + if (usock_recv (pfd, &buf, &msize) < 0) { + handle_err("main", "usock_recv < 0"); + return EXIT_FAILURE; + } + + printf ("msg recv: %s\n", buf); + + if (usock_send (pfd, buf, msize) < 0) { + handle_err("main", "usock_send < 0"); + return EXIT_FAILURE; + } + + if (usock_close (fd) < 0) { + handle_err("main", "usock_close fd < 0"); + return EXIT_FAILURE; + } + + if (usock_close (pfd) < 0) { + handle_err("main", "usock_close pfd < 0"); + return EXIT_FAILURE; + } + + if (usock_remove (UPATH) < 0) { + handle_err("main", "usock_remove < 0"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/core/communication.c b/core/communication.c index 1ddaddc..9494a5b 100644 --- a/core/communication.c +++ b/core/communication.c @@ -6,11 +6,10 @@ #include #include -#define LISTEN_BACKLOG 50 #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) -void service_path (char *path, const char *name, int index, int version) +void service_path (char *path, const char *sname, int index, int version) { memset (path, 0, PATH_MAX); snprintf (path, PATH_MAX, "%s/%s-%d-%d", TMPDIR, sname, index, version); @@ -35,7 +34,7 @@ int srv_init (int argc, char **argv, char **env // gets the service path service_path (srv->spath, sname, srv->index, srv->version); - usock_listen (&srv->service_fd, srv->spath); + usock_init (&srv->service_fd, srv->spath); return 0; } @@ -43,12 +42,7 @@ int srv_init (int argc, char **argv, char **env int srv_close (struct service *srv) { usock_close (srv->service_fd); - - if (unlink (srv->spath)) { - return 1; - } - - return 0; + return usock_remove (srv->spath); } int srv_read (const struct service *srv, char ** buf, size_t *msize) diff --git a/core/communication.h b/core/communication.h index 3276ec8..d12fcd8 100644 --- a/core/communication.h +++ b/core/communication.h @@ -45,7 +45,7 @@ int srv_init (int argc, char **argv, char **env , struct service *srv, const char *sname); int srv_close (struct service *srv); -int srv_read (const struct service *, char ** buf); +int srv_read (const struct service *srv, char ** buf, size_t *msize); int srv_write (const struct service *, const char * buf, size_t); // APPLICATION diff --git a/core/usocket.c b/core/usocket.c index 5ce720d..4c42e35 100644 --- a/core/usocket.c +++ b/core/usocket.c @@ -1,4 +1,8 @@ #include "usocket.h" +#include +#include +#include +#include #include #define handle_err(fun,msg)\ @@ -66,7 +70,7 @@ int usock_connect (int *fd, const char *path) struct sockaddr_un my_addr; socklen_t peer_addr_size; - sfd = socket(AF_UNIX, SOCK_STREAM, 0); + sfd = socket (AF_UNIX, SOCK_STREAM, 0); if (sfd == -1) { handle_err ("usock_connect", "sfd == -1"); return -1; @@ -86,20 +90,22 @@ int usock_connect (int *fd, const char *path) } *fd = sfd; + + return 0; } -int usock_listen (int *fd, const char *path) +int usock_init (int *fd, const char *path) { assert (fd != NULL); assert (path != NULL); if (fd == NULL) { - handle_err ("usock_listen", "fd == NULL"); + handle_err ("usock_init", "fd == NULL"); return -1; } if (path == NULL) { - handle_err ("usock_listen", "path == NULL"); + handle_err ("usock_init", "path == NULL"); return -1; } @@ -107,9 +113,9 @@ int usock_listen (int *fd, const char *path) struct sockaddr_un my_addr; socklen_t peer_addr_size; - sfd = socket(AF_UNIX, SOCK_STREAM, 0); + sfd = socket (AF_UNIX, SOCK_STREAM, 0); if (sfd == -1) { - handle_err ("usock_listen", "sfd == -1"); + handle_err ("usock_init", "sfd == -1"); return -1; } @@ -121,23 +127,47 @@ int usock_listen (int *fd, const char *path) // TODO FIXME // delete the unix socket if already created - unlink(my_addr.sun_path); peer_addr_size = sizeof(struct sockaddr_un); - if (bind(sfd, (struct sockaddr *) &my_addr, peer_addr_size) == -1) { - handle_err ("usock_listen", "bind == -1"); + + if (bind (sfd, (struct sockaddr *) &my_addr, peer_addr_size) == -1) { + handle_err ("usock_init", "bind == -1"); perror("bind()"); return -1; } - if (listen(sfd, LISTEN_BACKLOG) == -1) { - handle_err ("usock_listen", "listen == -1"); + if (listen (sfd, LISTEN_BACKLOG) == -1) { + handle_err ("usock_init", "listen == -1"); perror("listen()"); return -1; } *fd = sfd; + return 0; +} + +int usock_accept (int fd, int *pfd) +{ + assert (pfd != NULL); + + if (pfd == NULL) { + handle_err ("usock_accept", "pfd == NULL"); + return -1; + } + + struct sockaddr_un peer_addr; + memset (&peer_addr, 0, sizeof (struct sockaddr_un)); + socklen_t peer_addr_size; + + *pfd = accept (fd, (struct sockaddr *) &peer_addr, &peer_addr_size); + if (*pfd < 0) { + handle_err ("usock_accept", "accept < 0"); + perror("listen()"); + return -1; + } + + return 0; } int usock_close (int fd) @@ -150,3 +180,8 @@ int usock_close (int fd) } return ret; } + +int usock_remove (const char *path) +{ + return unlink (path); +} diff --git a/core/usocket.h b/core/usocket.h index a3b668f..7c61170 100644 --- a/core/usocket.h +++ b/core/usocket.h @@ -5,6 +5,8 @@ #include #include +#define LISTEN_BACKLOG 128 + // same as recv(2) int usock_send (int fd, const char *buf, const int m_size); @@ -19,6 +21,17 @@ int usock_close (int fd); // same as connect(2) // if fd == NULL => -1 -int usock_connect (int *fd, const char *path) +int usock_connect (int *fd, const char *path); + +// if not ok => -1 +// if ok => 0 +int usock_init (int *fd, const char *path); + +// if not ok => -1 +// if ok => 0 +int usock_accept (int fd, int *pfd); + +// same as unlink(2) +int usock_remove (const char *path); #endif