aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <goliath@infraroot.at>2019-03-29 11:02:22 +0100
committerDavid Oberhollenzer <goliath@infraroot.at>2019-03-29 21:00:53 +0100
commitbe066419049587e1349ada03306d004c30c18da6 (patch)
treed7262525f81370c865ef473f3c8e5649cc306611
parentc3d14cbfa863ea3af8aaa253f1d7d3909adf1547 (diff)
cleanup: init socket wire format
Replace array adhockery with structs and make use of the handy endianness conversion macros. Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
-rw-r--r--initd/initsock.c31
-rw-r--r--lib/include/initsock.h7
-rw-r--r--lib/init/init_socket_recv_status.c22
3 files changed, 30 insertions, 30 deletions
diff --git a/initd/initsock.c b/initd/initsock.c
index 718ab61..7016ef1 100644
--- a/initd/initsock.c
+++ b/initd/initsock.c
@@ -20,12 +20,13 @@ static int send_string(int fd, const void *dst, size_t addrlen,
const char *str)
{
size_t len = strlen(str);
- uint8_t raw_len[2];
+ uint16_t raw;
- raw_len[0] = (len >> 8) & 0xFF;
- raw_len[1] = len & 0xFF;
+ if (len > 0xFFFF)
+ return -1;
- if (send_retry(fd, dst, addrlen, raw_len, 2))
+ raw = htobe16(len);
+ if (send_retry(fd, dst, addrlen, &raw, 2))
return -1;
return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0;
@@ -61,24 +62,20 @@ int init_socket_create(void)
int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
E_SERVICE_STATE state, service_t *svc)
{
- uint8_t info[8];
+ init_response_status_t info;
+
+ memset(&info, 0, sizeof(info));
if (svc == NULL || state == ESS_NONE) {
- info[0] = ESS_NONE;
- info[1] = 0;
- info[2] = info[3] = 0;
- info[4] = info[5] = info[6] = info[7] = 0xFF;
+ info.state = ESS_NONE;
+ info.id = -1;
} else {
- info[0] = state;
- info[1] = svc->status & 0xFF;
- info[2] = info[3] = 0;
- info[4] = (svc->id >> 24) & 0xFF;
- info[5] = (svc->id >> 16) & 0xFF;
- info[6] = (svc->id >> 8) & 0xFF;
- info[7] = svc->id & 0xFF;
+ info.state = state;
+ info.exit_status = svc->status & 0xFF;
+ info.id = htobe32(svc->id);
}
- if (send_retry(fd, dest_addr, addrlen, info, sizeof(info)))
+ if (send_retry(fd, dest_addr, addrlen, &info, sizeof(info)))
return -1;
if (svc != NULL && state != ESS_NONE) {
diff --git a/lib/include/initsock.h b/lib/include/initsock.h
index 04b8ffd..ef8b9e3 100644
--- a/lib/include/initsock.h
+++ b/lib/include/initsock.h
@@ -40,6 +40,13 @@ typedef struct {
} init_request_t;
typedef struct {
+ uint8_t state;
+ uint8_t exit_status;
+ uint8_t padd[2];
+ int32_t id;
+} init_response_status_t;
+
+typedef struct {
E_SERVICE_STATE state;
int exit_status;
int id;
diff --git a/lib/init/init_socket_recv_status.c b/lib/init/init_socket_recv_status.c
index d5ca2e6..996f8de 100644
--- a/lib/init/init_socket_recv_status.c
+++ b/lib/init/init_socket_recv_status.c
@@ -31,18 +31,17 @@ retry:
static char *read_string(int fd)
{
- uint8_t size_raw[2];
+ uint16_t len;
char *buffer;
- size_t len;
int ret;
- ret = read_retry(fd, size_raw, 2);
+ ret = read_retry(fd, &len, sizeof(len));
if (ret <= 0)
return NULL;
- len = (((size_t)size_raw[0]) << 8) | (size_t)size_raw[1];
+ len = be16toh(len);
- buffer = malloc(len + 1);
+ buffer = calloc(1, len + 1);
if (buffer == NULL)
return NULL;
@@ -57,24 +56,21 @@ static char *read_string(int fd)
}
}
- buffer[len] = '\0';
return buffer;
}
int init_socket_recv_status(int fd, init_status_t *resp)
{
- uint8_t info[8];
+ init_response_status_t info;
memset(resp, 0, sizeof(*resp));
- if (read_retry(fd, info, sizeof(info)) <= 0)
+ if (read_retry(fd, &info, sizeof(info)) <= 0)
return -1;
- resp->state = info[0];
- resp->exit_status = info[1];
-
- resp->id = ((int)info[4] << 24) | ((int)info[5] << 16) |
- ((int)info[6] << 8) | (int)info[7];
+ resp->state = info.state;
+ resp->exit_status = info.exit_status;
+ resp->id = be32toh(info.id);
if (resp->state == ESS_NONE)
return 0;