2016-12-21 01:26:47 +01:00
|
|
|
#include "msg.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "usocket.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2018-10-03 22:02:37 +02:00
|
|
|
void ipc_message_print (const struct ipc_message *m)
|
2016-12-21 02:56:52 +01:00
|
|
|
{
|
|
|
|
assert (m != NULL);
|
2018-10-04 01:54:12 +02:00
|
|
|
printf ("msg: type %d len %d\n", m->type, m->length);
|
2016-12-21 02:56:52 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 22:02:37 +02:00
|
|
|
int ipc_message_format_read (struct ipc_message *m, const char *buf, size_t msize)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
|
|
|
assert (m != NULL);
|
|
|
|
assert (buf != NULL);
|
|
|
|
assert (msize <= BUFSIZ - 3);
|
|
|
|
|
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
m->type = buf[0];
|
2018-10-04 01:54:12 +02:00
|
|
|
memcpy (&m->length, buf+1, 2);
|
2016-12-21 01:26:47 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
assert (m->length <= BUFSIZ -3);
|
|
|
|
// printf ("type %d : msize = %ld, length = %d\n", m->type, msize, m->length);
|
|
|
|
assert (m->length == msize - 3);
|
2016-12-21 01:26:47 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
if (m->payload != NULL)
|
|
|
|
free (m->payload), m->payload = NULL;
|
2016-12-21 01:26:47 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
if (m->payload == NULL && m->length > 0) {
|
|
|
|
m->payload = malloc (m->length);
|
|
|
|
memcpy (m->payload, buf+3, m->length);
|
2016-12-21 01:26:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-03 22:02:37 +02:00
|
|
|
int ipc_message_format_write (const struct ipc_message *m, char **buf, size_t *msize)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
|
|
|
assert (m != NULL);
|
|
|
|
assert (buf != NULL);
|
|
|
|
assert (msize != NULL);
|
2018-10-04 01:54:12 +02:00
|
|
|
assert (m->length <= BUFSIZ -3);
|
2016-12-21 01:26:47 +01:00
|
|
|
|
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (buf == NULL)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (msize == NULL)
|
|
|
|
return -3;
|
|
|
|
|
2016-12-21 02:56:52 +01:00
|
|
|
if (*buf == NULL) {
|
2018-10-04 01:54:12 +02:00
|
|
|
*buf = malloc (3 + m->length);
|
2016-12-21 02:56:52 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:26:47 +01:00
|
|
|
char *buffer = *buf;
|
|
|
|
|
|
|
|
buffer[0] = m->type;
|
2018-10-04 01:54:12 +02:00
|
|
|
memcpy (buffer + 1, &m->length, 2);
|
|
|
|
memcpy (buffer + 3, m->payload, m->length);
|
2016-12-21 02:56:52 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
*msize = 3 + m->length;
|
2016-12-21 01:26:47 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-03 22:02:37 +02:00
|
|
|
int ipc_message_read (int fd, struct ipc_message *m)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
|
|
|
assert (m != NULL);
|
|
|
|
|
|
|
|
char *buf = NULL;
|
2016-12-21 02:56:52 +01:00
|
|
|
size_t msize = BUFSIZ;
|
2016-12-21 01:26:47 +01:00
|
|
|
|
|
|
|
int ret = usock_recv (fd, &buf, &msize);
|
|
|
|
if (ret < 0) {
|
|
|
|
handle_err ("msg_read", "usock_recv");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
if (ipc_message_format_read (m, buf, msize) < 0) {
|
2016-12-21 01:26:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free (buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-03 22:02:37 +02:00
|
|
|
int ipc_message_write (int fd, const struct ipc_message *m)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
|
|
|
assert (m != NULL);
|
|
|
|
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
2018-10-03 21:52:11 +02:00
|
|
|
ipc_message_format_write (m, &buf, &msize);
|
2016-12-21 01:26:47 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
int ipc_message_format (struct ipc_message *m, char type, const char *payload, size_t length)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
|
|
|
assert (m != NULL);
|
2018-10-04 01:54:12 +02:00
|
|
|
assert (length + 3 <= BUFSIZ);
|
|
|
|
assert ((length == 0 && payload == NULL) || (length > 0 && payload != NULL));
|
2016-12-21 01:26:47 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
if (length + 3 > BUFSIZ) {
|
2016-12-21 01:26:47 +01:00
|
|
|
handle_err ("msg_format_con", "msgsize > BUFSIZ");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->type = type;
|
2018-10-04 01:54:12 +02:00
|
|
|
m->length = (short) length;
|
2016-12-21 02:56:52 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
if (m->payload == NULL)
|
|
|
|
m->payload = malloc (length);
|
2016-12-21 02:56:52 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
memcpy (m->payload, payload, length);
|
2016-12-21 01:26:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
int ipc_message_format_con (struct ipc_message *m, const char *payload, size_t length)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
2018-10-04 01:54:12 +02:00
|
|
|
return ipc_message_format (m, MSG_TYPE_CON, payload, length);
|
2016-12-21 01:26:47 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
int ipc_message_format_data (struct ipc_message *m, const char *payload, size_t length)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
2018-10-04 01:54:12 +02:00
|
|
|
return ipc_message_format (m, MSG_TYPE_DATA, payload, length);
|
2016-12-21 01:26:47 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
int ipc_message_format_ack (struct ipc_message *m, const char *payload, size_t length)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
2018-10-04 01:54:12 +02:00
|
|
|
return ipc_message_format (m, MSG_TYPE_ACK, payload, length);
|
2016-12-21 01:26:47 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 22:02:37 +02:00
|
|
|
int ipc_message_free (struct ipc_message *m)
|
2016-12-21 01:26:47 +01:00
|
|
|
{
|
|
|
|
assert (m != NULL);
|
|
|
|
|
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
if (m->payload != NULL)
|
|
|
|
free (m->payload), m->payload = NULL;
|
2016-12-21 02:56:52 +01:00
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
m->length = 0;
|
2016-12-21 01:26:47 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|