1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-12-22 15:20:49 +01:00

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>
This commit is contained in:
David Oberhollenzer 2019-03-29 11:02:22 +01:00
parent c3d14cbfa8
commit be06641904
3 changed files with 30 additions and 30 deletions

View file

@ -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) {

View file

@ -39,6 +39,13 @@ typedef struct {
} arg;
} 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;

View file

@ -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;