summaryrefslogtreecommitdiff
path: root/c_src/erlport.c
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2016-03-27 19:27:30 +0200
committerLinus Nordberg <linus@nordu.net>2016-03-27 19:44:25 +0200
commit56d70baa79ae5907b11445364bbea9b31ee4cd20 (patch)
treeef0403255a04de75736da82385d282a0198c52c2 /c_src/erlport.c
parent9f723f6f1d79c1be460ece10555945346045b175 (diff)
WIP
Diffstat (limited to 'c_src/erlport.c')
-rw-r--r--c_src/erlport.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/c_src/erlport.c b/c_src/erlport.c
new file mode 100644
index 0000000..372f98d
--- /dev/null
+++ b/c_src/erlport.c
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2014-2015, NORDUnet A/S.
+ * See LICENSE for licensing information.
+ */
+
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "net_read_write.h"
+#include "erlport.h"
+
+static ssize_t
+read_length(size_t length_size)
+{
+ unsigned char buf[4];
+
+ if (length_size != 2 && length_size != 4) {
+ return -1;
+ }
+
+ if (length_size > sizeof(buf)) {
+ return -1;
+ }
+
+ ssize_t ret;
+
+ ret = net_read(0, (char *)buf, length_size);
+
+ if (ret != (ssize_t) length_size) {
+ return -1;
+ }
+
+ if (length_size == 2) {
+ return (ssize_t)(((unsigned long)buf[0] << 8) | (unsigned long)buf[1]);
+ } else {
+ return (ssize_t)(((unsigned long)buf[0] << 24) |
+ ((unsigned long)buf[1] << 16) |
+ ((unsigned long)buf[2] << 8) |
+ (unsigned long)buf[3]);
+ }
+}
+
+ssize_t
+read_command(unsigned char *buf, size_t maxlen, size_t length_size)
+{
+ ssize_t len;
+
+ len = read_length(length_size);
+
+ if (len < 0) {
+ return -1;
+ }
+
+ if (len > (ssize_t) maxlen) {
+ return -1;
+ }
+ return net_read(0, (char *)buf, (size_t)len);
+}
+
+static int
+write_length(size_t len, size_t length_size)
+{
+ unsigned char buf[4];
+
+ if (length_size != 2 && length_size != 4) {
+ return -1;
+ }
+
+ if (length_size == 2) {
+ buf[0] = (len >> 8) & 0xff;
+ buf[1] = len & 0xff;
+ } else {
+ buf[0] = (len >> 24) & 0xff;
+ buf[1] = (len >> 16) & 0xff;
+ buf[2] = (len >> 8) & 0xff;
+ buf[3] = len & 0xff;
+ }
+
+ ssize_t ret;
+
+ ret = net_write(1, (char *)buf, length_size);
+
+ if (ret < 0) {
+ return -1;
+ }
+
+ if (ret != (ssize_t) length_size) {
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+write_reply(unsigned char *msg, size_t len, size_t length_size)
+{
+ ssize_t ret;
+
+ ret = write_length(len, length_size);
+ if (ret < 0) {
+ return -1;
+ }
+ ret = net_write(1, (char *)msg, len);
+ if (ret < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+write_status(char *msg)
+{
+ return write_reply((unsigned char *)msg, strlen(msg), 2);
+}