From 1e44f3d00f9f4d8b445adf100df1beca561462af Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Wed, 21 Dec 2016 01:26:47 +0100
Subject: [PATCH] msg format
---
core/communication.c | 90 ++++++++++++++++-----------
core/communication.h | 15 ++---
core/error.h | 10 +++
core/msg-format.c | 39 ------------
core/msg-format.h | 13 ----
core/msg.c | 145 +++++++++++++++++++++++++++++++++++++++++++
core/msg.h | 36 +++++++++++
7 files changed, 249 insertions(+), 99 deletions(-)
create mode 100644 core/error.h
delete mode 100644 core/msg-format.c
delete mode 100644 core/msg-format.h
create mode 100644 core/msg.c
create mode 100644 core/msg.h
diff --git a/core/communication.c b/core/communication.c
index a42b774..2c2e9f2 100644
--- a/core/communication.c
+++ b/core/communication.c
@@ -1,20 +1,16 @@
#include "communication.h"
#include "usocket.h"
-#include "msg-format.h"
#include "utils.h"
+#include "error.h"
#include
#include
#include
-#define handle_error(msg) \
- do { perror(msg); exit(EXIT_FAILURE); } while (0)
-
-#define handle_err(fun,msg)\
- fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
-
void service_path (char *path, const char *sname, int index, int version)
{
+ assert (path != NULL);
+ assert (sname != NULL);
memset (path, 0, PATH_MAX);
snprintf (path, PATH_MAX, "%s/%s-%d-%d", TMPDIR, sname, index, version);
}
@@ -43,21 +39,24 @@ int srv_init (int argc, char **argv, char **env
return 0;
}
-// TODO
int srv_accept (struct service *srv, struct process *p)
{
+ assert (srv != NULL);
+ assert (p != NULL);
+
usock_accept (srv->service_fd, &p->proc_fd);
- char *buf = NULL;
- size_t msgsize = BUFSIZ;
- srv_read (p, &buf, &msgsize);
+ struct msg m_con;
+ memset (&m_con, 0, sizeof (struct msg));
+ srv_read (p, &m_con);
+ // TODO: handle the parameters in the first message
+ msg_free (&m_con);
- msgsize = 0;
- msg_format_ack (buf, NULL, &msgsize);
-
- srv_write (p, buf, msgsize);
-
- free (buf);
+ struct msg m_ack;
+ memset (&m_ack, 0, sizeof (struct msg));
+ msg_format_ack (&m_ack, NULL, 0);
+ srv_write (p, &m_ack);
+ msg_free (&m_ack);
return 0;
}
@@ -70,17 +69,24 @@ int srv_close (struct service *srv)
int srv_close_proc (struct process *p)
{
+ struct msg m;
+ memset (&m, 0, sizeof (struct msg));
+ m.type = MSG_TYPE_DIS;
+ if (msg_write (p->proc_fd, &m) < 0) {
+ handle_err ("srv_close_proc", "msg_write < 0");
+ }
+
return usock_close (p->proc_fd);
}
-int srv_read (const struct process *p, char **buf, size_t *msize)
+int srv_read (const struct process *p, struct msg *m)
{
- return usock_recv (p->proc_fd, buf, msize);
+ return msg_read (p->proc_fd, m);
}
-int srv_write (const struct process *p, const char * buf, size_t msize)
+int srv_write (const struct process *p, const struct msg *m)
{
- return usock_send (p->proc_fd, buf, msize);
+ return msg_write (p->proc_fd, m);
}
int app_connection (int argc, char **argv, char **env
@@ -103,40 +109,50 @@ int app_connection (int argc, char **argv, char **env
usock_connect (&srv->service_fd, srv->spath);
- // TODO: connection algorithm
// send connection string and receive acknowledgement
- char send_buffer [BUFSIZ];
- memset (send_buffer, 0, BUFSIZ);
- if (msg_format_con (send_buffer, connectionstr, &msize) < 0) {
+ struct msg m_con;
+ memset (&m_con, 0, sizeof (struct msg));
+
+ // format the connection msg
+ if (msg_format_con (&m_con, connectionstr, msize) < 0) {
handle_err ("app_connection", "msg_format_con");
return -1;
}
- app_write (srv, send_buffer, msize);
- char *buffer = NULL;
- size_t read_msg_size = BUFSIZ;
+ // send connection msg
+ app_write (srv, &m_con);
+ msg_free (&m_con);
- app_read (srv, &buffer, &read_msg_size);
+ // receive ack msg
+ struct msg m_ack;
+ memset (&m_ack, 0, sizeof (struct msg));
+ app_read (srv, &m_ack);
- assert (buffer[0] == MSG_TYPE_ACK);
- assert (read_msg_size == 3);
-
- free (buffer);
+ assert (m_ack.type == MSG_TYPE_ACK);
+ assert (m_ack.valsize == 0);
+ msg_free (&m_ack);
return 0;
}
int app_close (struct service *srv)
{
+ struct msg m;
+ memset (&m, 0, sizeof (struct msg));
+ m.type = MSG_TYPE_DIS;
+ if (msg_write (srv->service_fd, &m) < 0) {
+ handle_err ("app_close", "msg_write < 0");
+ }
+
return usock_close (srv->service_fd);
}
-int app_read (struct service *srv, char ** buf, size_t *msize)
+int app_read (struct service *srv, struct msg *m)
{
- return usock_recv (srv->service_fd, buf, msize);
+ return msg_read (srv->service_fd, m);
}
-int app_write (struct service *srv, char * buf, size_t msize)
+int app_write (struct service *srv, const struct msg *m)
{
- return usock_send (srv->service_fd, buf, msize);
+ return msg_write (srv->service_fd, m);
}
diff --git a/core/communication.h b/core/communication.h
index 22da922..17b71dc 100644
--- a/core/communication.h
+++ b/core/communication.h
@@ -5,17 +5,12 @@
#include
#include
#include // error numbers
+#include "msg.h"
#include "process.h"
#define COMMUNICATION_VERSION 1
-#define ER_FILE_OPEN 1
-#define ER_FILE_CLOSE 2
-#define ER_FILE_READ 3
-#define ER_FILE_WRITE 4
-#define ER_FILE_WRITE_PARAMS 5
-
#define ER_MEM_ALLOC 100
#define ER_PARAMS 101
@@ -41,8 +36,8 @@ int srv_close (struct service *srv);
int srv_close_proc (struct process *p);
int srv_accept (struct service *srv, struct process *p);
-int srv_read (const struct process *, char **buf, size_t *msize);
-int srv_write (const struct process *, const char * buf, size_t);
+int srv_read (const struct process *, struct msg *m);
+int srv_write (const struct process *, const struct msg *m);
// APPLICATION
@@ -53,7 +48,7 @@ int app_connection (int argc, char **argv, char **env
, struct service *, const char *, const char *, size_t);
int app_close (struct service *);
-int app_read (struct service *srv, char ** buf, size_t *msize);
-int app_write (struct service *, char * buf, size_t msize);
+int app_read (struct service *srv, struct msg *m);
+int app_write (struct service *, const struct msg *m);
#endif
diff --git a/core/error.h b/core/error.h
new file mode 100644
index 0000000..7eda28d
--- /dev/null
+++ b/core/error.h
@@ -0,0 +1,10 @@
+#ifndef __ERROR_H__
+#define __ERROR_H__
+
+#define handle_error(msg) \
+ do { perror(msg); exit(EXIT_FAILURE); } while (0)
+
+#define handle_err(fun,msg)\
+ fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
+
+#endif
diff --git a/core/msg-format.c b/core/msg-format.c
deleted file mode 100644
index 2e55ba6..0000000
--- a/core/msg-format.c
+++ /dev/null
@@ -1,39 +0,0 @@
-#include
-#include
-#include
-#include "msg-format.h"
-
-#define handle_err(fun,msg)\
- fprintf (stderr, "%s: file %s line %d %s\n", fun, __FILE__, __LINE__, msg);
-
-// [type] [len] [val]
-// 3 valsize constr
-int msg_format_con (char *buf, const char *constr, size_t *msgsize)
-{
- assert (buf != NULL);
- assert (*msgsize + 3 <= BUFSIZ);
-
- if (*msgsize + 3 > BUFSIZ) {
- handle_err ("msg_format_con", "msgsize > BUFSIZ");
- return 1;
- }
-
- buf[0] = MSG_TYPE_CON;
- short final_size = (short) *msgsize;
- memcpy (buf + 1, &final_size, 2);
- memcpy (buf + 3, constr, *msgsize);
- *msgsize += 3;
- return 0;
-}
-
-// [type] [len]
-// 4 0
-int msg_format_ack (char *buf, const char *constr, size_t *msgsize)
-{
- assert (buf != NULL);
- memset(buf, 0, 3);
- buf[0] = MSG_TYPE_ACK;
-
- *msgsize += 3;
- return 0;
-}
diff --git a/core/msg-format.h b/core/msg-format.h
deleted file mode 100644
index 26424bf..0000000
--- a/core/msg-format.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef __MSG_FORMAT_H__
-#define __MSG_FORMAT_H__
-
-#include
-
-#define MSG_TYPE_CON 1
-#define MSG_TYPE_ERR 2
-#define MSG_TYPE_ACK 4
-
-int msg_format_con (char *buf, const char *constr, size_t *msgsize);
-int msg_format_ack (char *buf, const char *constr, size_t *msgsize);
-
-#endif
diff --git a/core/msg.c b/core/msg.c
new file mode 100644
index 0000000..98eb9ed
--- /dev/null
+++ b/core/msg.c
@@ -0,0 +1,145 @@
+#include "msg.h"
+#include "error.h"
+#include "usocket.h"
+
+#include
+
+int msg_format_read (struct msg *m, const char *buf, size_t msize)
+{
+ assert (m != NULL);
+ assert (buf != NULL);
+ assert (msize >= 3);
+
+ assert (msize <= BUFSIZ - 3);
+
+ if (m == NULL)
+ return -1;
+
+ m->type = buf[0];
+ memcpy (&m->valsize, buf+1, 2);
+
+ assert (m->valsize <= BUFSIZ -3);
+
+ if (m->val != NULL)
+ free (m->val), m->val = NULL;
+
+ if (m->val == NULL) {
+ m->val = malloc (m->valsize);
+ memcpy (m->val, buf+3, m->valsize);
+ }
+
+ return 0;
+}
+
+int msg_format_write (struct msg *m, char **buf, size_t *msize)
+{
+ assert (m != NULL);
+ assert (buf != NULL);
+ assert (msize != NULL);
+ assert (m->valsize <= BUFSIZ -3);
+
+ if (m == NULL)
+ return -1;
+
+ if (buf == NULL)
+ return -2;
+
+ if (msize == NULL)
+ return -3;
+
+ char *buffer = *buf;
+
+ buffer[0] = m->type;
+ memcpy (buffer + 1, &m->valsize, 2);
+ memcpy (buffer + 3, &m->val, (m->valsize <= BUFSIZ) ? m->valsize : BUFSIZ);
+
+ return 0;
+}
+
+int msg_read (int fd, struct msg *m)
+{
+ assert (m != NULL);
+
+ char *buf = NULL;
+ size_t msize = 0;
+
+ int ret = usock_recv (fd, &buf, &msize);
+ if (ret < 0) {
+ handle_err ("msg_read", "usock_recv");
+ return ret;
+ }
+
+ if (msg_format_read (m, buf, msize) < 0) {
+ return -1;
+ }
+ free (buf);
+
+ return 0;
+}
+
+int msg_write (int fd, const struct msg *m)
+{
+ assert (m != NULL);
+
+ char *buf = NULL;
+ size_t msize = 0;
+ msg_format_write (m, &buf, &msize);
+
+ int ret = usock_send (fd, buf, msize);
+ if (ret < 0) {
+ handle_err ("msg_write", "usock_send");
+ return ret;
+ }
+
+ if (buf != NULL)
+ free (buf);
+
+ return 0;
+}
+
+// MSG FORMAT
+
+int msg_format (struct msg *m, char type, const char *val, size_t valsize)
+{
+ assert (m != NULL);
+ assert (valsize + 3 <= BUFSIZ);
+ assert (valsize == 0 && val == NULL || valsize > 0 && val != NULL);
+
+ if (valsize + 3 > BUFSIZ) {
+ handle_err ("msg_format_con", "msgsize > BUFSIZ");
+ return -1;
+ }
+
+ m->type = type;
+ m->valsize = (short) valsize;
+ memcpy (m->val, val, valsize);
+ return 0;
+}
+
+int msg_format_con (struct msg *m, const char *val, size_t valsize)
+{
+ return msg_format (m, MSG_TYPE_CON, val, valsize);
+}
+
+int msg_format_data (struct msg *m, const char *val, size_t valsize)
+{
+ return msg_format (m, MSG_TYPE_DATA, val, valsize);
+}
+
+int msg_format_ack (struct msg *m, const char *val, size_t valsize)
+{
+ return msg_format (m, MSG_TYPE_ACK, val, valsize);
+}
+
+int msg_free (struct msg *m)
+{
+ assert (m != NULL);
+
+ if (m == NULL)
+ return -1;
+
+ if (m->val != NULL)
+ free (m->val);
+
+ return 0;
+}
diff --git a/core/msg.h b/core/msg.h
new file mode 100644
index 0000000..c2ac282
--- /dev/null
+++ b/core/msg.h
@@ -0,0 +1,36 @@
+#ifndef __MSG_H__
+#define __MSG_H__
+
+#include
+#include
+#include
+
+#define MSG_TYPE_CON 1
+#define MSG_TYPE_DIS 2
+#define MSG_TYPE_ERR 3
+#define MSG_TYPE_ACK 4
+#define MSG_TYPE_DATA 5
+
+struct msg {
+ char type;
+ short valsize;
+ char *val;
+};
+
+// used to create msg structure from buffer
+int msg_format_read (struct msg *m, const char *buf, size_t msize);
+// used to create buffer from msg structure
+int msg_format_write (struct msg *m, char **buf, size_t *msize);
+
+// read a structure msg from fd
+int msg_read (int fd, struct msg *m);
+// write a structure msg to fd
+int msg_write (int fd, const struct msg *m);
+
+int msg_format_con (struct msg *m, const char *val, size_t valsize);
+int msg_format_data (struct msg *m, const char *val, size_t valsize);
+int msg_format_ack (struct msg *m, const char *val, size_t valsize);
+
+int msg_free (struct msg *m);
+
+#endif