diff options
author | Mattias Andrée <maandree@kth.se> | 2017-10-22 02:06:03 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2017-10-22 02:06:03 +0200 |
commit | ddc2d09588f366218d48730f5eaa359ec492b44c (patch) | |
tree | 3e2363ab2c468725a32475166f52034c6c3d08a0 /libsbus.c | |
parent | Update readme (diff) | |
download | sbus-ddc2d09588f366218d48730f5eaa359ec492b44c.tar.gz sbus-ddc2d09588f366218d48730f5eaa359ec492b44c.tar.bz2 sbus-ddc2d09588f366218d48730f5eaa359ec492b44c.tar.xz |
Add support for secret messages
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libsbus.c')
-rw-r--r-- | libsbus.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -60,6 +60,20 @@ libsbuf_prepare_message(const char *key, char *buf, size_t *remaining) } int +libsbus_send_cmsg(int fd, const char *key, const char *msg, size_t n, int flags, char *buf) +{ + size_t len = strlen(key) + 1; + if (len + n > LIBSBUS_BUFFER_SIZE - 5) { + errno = EMSGSIZE; + return -1; + } + buf[0] = 'C', buf[1] = 'M', buf[2] = 'S', buf[3] = 'G', buf[4] = ' '; + memcpy(&buf[5], key, len); + memcpy(&buf[5 + len], msg, n); + return -(send(fd, buf, len + n + 5, flags) < 0); +} + +int libsbus_receive(int fd, int flags, char *buf, union libsbus_packet *packet) { ssize_t r; @@ -80,6 +94,14 @@ libsbus_receive(int fd, int flags, char *buf, union libsbus_packet *packet) packet->message.key = &buf[4]; packet->message.msg = p; packet->message.n = (size_t)(r - (p - buf)); + } else if (r >= 5 && !strncmp(buf, "CMSG ", 5)) { + p = memchr(buf, '\0', r); + if (!p++) + goto unknown; + packet->type = LIBSBUS_CONTROL_MESSAGE; + packet->message.key = &buf[4]; + packet->message.msg = p; + packet->message.n = (size_t)(r - (p - buf)); } else { unknown: packet->type = LIBSBUS_UNKNOWN; |