Archived
3
0

usock => socket unix fonctionnelles + prog de test + benchmark

This commit is contained in:
Philippe PITTOLI 2016-12-18 00:54:43 +01:00
parent b426ab7d7b
commit ee2ff72ed3
9 changed files with 338 additions and 22 deletions

25
core-test/app/Makefile Normal file
View File

@ -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

View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#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;
}

View File

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;
}

View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;
}

View File

@ -6,11 +6,10 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#define LISTEN_BACKLOG 50
#define handle_error(msg) \ #define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0) 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); memset (path, 0, PATH_MAX);
snprintf (path, PATH_MAX, "%s/%s-%d-%d", TMPDIR, sname, index, version); 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 // gets the service path
service_path (srv->spath, sname, srv->index, srv->version); 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; return 0;
} }
@ -43,12 +42,7 @@ int srv_init (int argc, char **argv, char **env
int srv_close (struct service *srv) int srv_close (struct service *srv)
{ {
usock_close (srv->service_fd); usock_close (srv->service_fd);
return usock_remove (srv->spath);
if (unlink (srv->spath)) {
return 1;
}
return 0;
} }
int srv_read (const struct service *srv, char ** buf, size_t *msize) int srv_read (const struct service *srv, char ** buf, size_t *msize)

View File

@ -45,7 +45,7 @@ int srv_init (int argc, char **argv, char **env
, struct service *srv, const char *sname); , struct service *srv, const char *sname);
int srv_close (struct service *srv); 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); int srv_write (const struct service *, const char * buf, size_t);
// APPLICATION // APPLICATION

View File

@ -1,4 +1,8 @@
#include "usocket.h" #include "usocket.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h> #include <assert.h>
#define handle_err(fun,msg)\ #define handle_err(fun,msg)\
@ -66,7 +70,7 @@ int usock_connect (int *fd, const char *path)
struct sockaddr_un my_addr; struct sockaddr_un my_addr;
socklen_t peer_addr_size; socklen_t peer_addr_size;
sfd = socket(AF_UNIX, SOCK_STREAM, 0); sfd = socket (AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) { if (sfd == -1) {
handle_err ("usock_connect", "sfd == -1"); handle_err ("usock_connect", "sfd == -1");
return -1; return -1;
@ -86,20 +90,22 @@ int usock_connect (int *fd, const char *path)
} }
*fd = sfd; *fd = sfd;
return 0;
} }
int usock_listen (int *fd, const char *path) int usock_init (int *fd, const char *path)
{ {
assert (fd != NULL); assert (fd != NULL);
assert (path != NULL); assert (path != NULL);
if (fd == NULL) { if (fd == NULL) {
handle_err ("usock_listen", "fd == NULL"); handle_err ("usock_init", "fd == NULL");
return -1; return -1;
} }
if (path == NULL) { if (path == NULL) {
handle_err ("usock_listen", "path == NULL"); handle_err ("usock_init", "path == NULL");
return -1; return -1;
} }
@ -107,9 +113,9 @@ int usock_listen (int *fd, const char *path)
struct sockaddr_un my_addr; struct sockaddr_un my_addr;
socklen_t peer_addr_size; socklen_t peer_addr_size;
sfd = socket(AF_UNIX, SOCK_STREAM, 0); sfd = socket (AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) { if (sfd == -1) {
handle_err ("usock_listen", "sfd == -1"); handle_err ("usock_init", "sfd == -1");
return -1; return -1;
} }
@ -121,23 +127,47 @@ int usock_listen (int *fd, const char *path)
// TODO FIXME // TODO FIXME
// delete the unix socket if already created // delete the unix socket if already created
unlink(my_addr.sun_path);
peer_addr_size = sizeof(struct sockaddr_un); 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()"); perror("bind()");
return -1; return -1;
} }
if (listen(sfd, LISTEN_BACKLOG) == -1) { if (listen (sfd, LISTEN_BACKLOG) == -1) {
handle_err ("usock_listen", "listen == -1"); handle_err ("usock_init", "listen == -1");
perror("listen()"); perror("listen()");
return -1; return -1;
} }
*fd = sfd; *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) int usock_close (int fd)
@ -150,3 +180,8 @@ int usock_close (int fd)
} }
return ret; return ret;
} }
int usock_remove (const char *path)
{
return unlink (path);
}

View File

@ -5,6 +5,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#define LISTEN_BACKLOG 128
// same as recv(2) // same as recv(2)
int usock_send (int fd, const char *buf, const int m_size); 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) // same as connect(2)
// if fd == NULL => -1 // 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 #endif