greatly simplify code

This commit is contained in:
hippoz 2022-12-25 20:54:10 +02:00
parent 05f7a1ae41
commit 0586219267
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
3 changed files with 141 additions and 158 deletions

120
server.c
View file

@ -189,19 +189,46 @@ ssize_t jb_server_client_recv(struct jb_server *s, int i, void *buf, size_t n)
return status; return status;
} }
#define _reply_begin(M_sig) \
do { \
TRYST(wire_compose_reply(&reply_ctx, &msg, (M_sig), &body_length)); \
body_start = reply_ctx.byte_cursor; \
} while(0); \
#define _reply_end() \
do { \
*body_length = reply_ctx.byte_cursor - body_start; \
if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) { \
return -1; \
} \
} while(0); \
#define _reply_error(message) \
do { \
TRYST(wire_compose_error(&reply_ctx, &msg, (message))); \
if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) { \
return -1; \
} \
} while(0) \
int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data, size_t data_len) int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data, size_t data_len)
{ {
struct jb_client *client = &s->clients[i]; struct jb_client *client = &s->clients[i];
wire_message_t msg = {0};
wire_context_t ctx = { wire_context_t ctx = {
.byte_cursor = 0, .byte_cursor = 0,
.data = data, .data = data,
.data_len = data_len, .data_len = data_len,
}; };
wire_message_t msg = {0}; TRYST(wire_parse_message(&ctx, &msg));
TRY_NONNEGATIVE(int, wire_parse_message(&ctx, &msg), -1); wire_message_field_t *destination_field = &msg.fields[DBUS_HEADER_FIELD_DESTINATION];
wire_message_field_t *member_field = &msg.fields[DBUS_HEADER_FIELD_MEMBER];
bool should_unicast = false;
uint32_t *body_length;
uint32_t body_start;
uint8_t reply_data[4096]; uint8_t reply_data[4096];
memset(reply_data, 0, 4096); memset(reply_data, 0, 4096);
@ -211,11 +238,6 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
.data_len = 4096, .data_len = 4096,
}; };
wire_message_field_t *destination_field = &msg.fields[DBUS_HEADER_FIELD_DESTINATION];
wire_message_field_t *member_field = &msg.fields[DBUS_HEADER_FIELD_MEMBER];
bool should_unicast = false;
switch (msg.type) { switch (msg.type) {
case DBUS_MESSAGE_METHOD_CALL: { case DBUS_MESSAGE_METHOD_CALL: {
if (!destination_field->present || !member_field->present) { if (!destination_field->present || !member_field->present) {
@ -236,17 +258,13 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
return -1; return -1;
} }
uint32_t *body_length; _reply_begin("s") {
TRY_NONNEGATIVE(int, wire_compose_reply(&reply_ctx, &msg, "s", &body_length), -1); TRYPTR(wire_set_string(&reply_ctx, s->names[unique_name_index].name));
uint32_t body_start = reply_ctx.byte_cursor; } _reply_end()
TRY_NONNULL(char*, wire_set_string(&reply_ctx, s->names[unique_name_index].name), -1);
*body_length = reply_ctx.byte_cursor - body_start;
if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) {
return -1;
}
printf("assigned unique name '%s' to connection %d\n", s->names[unique_name_index].name, i); printf("assigned unique name '%s' to connection %d\n", s->names[unique_name_index].name, i);
} else if (strcmp(member, "RequestName") == 0) { } else if (strcmp(member, "RequestName") == 0) {
char *name = TRY_NONNULL(char*, wire_get_string(&ctx, NULL), -1); char *name = TRYPTR(wire_get_string(&ctx, NULL));
int name_len = strlen(name); int name_len = strlen(name);
if (name_len < 1 || name_len > 256) { if (name_len < 1 || name_len > 256) {
return -1; return -1;
@ -265,20 +283,15 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
status_code = DBUS_REQUEST_NAME_REPLY_EXISTS; status_code = DBUS_REQUEST_NAME_REPLY_EXISTS;
} }
uint32_t *body_length; _reply_begin("u") {
TRY_NONNEGATIVE(int, wire_compose_reply(&reply_ctx, &msg, "u", &body_length), -1); TRYPTR(wire_set_u32(&reply_ctx, status_code));
uint32_t body_start = reply_ctx.byte_cursor; } _reply_end()
TRY_NONNULL(uint32_t*, wire_set_u32(&reply_ctx, status_code), -1);
*body_length = reply_ctx.byte_cursor - body_start;
if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) {
return -1;
}
if (status_code == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { if (status_code == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
printf("client '%s' (index=%d) now owns name '%s'\n", s->names[client->unique_name_index].name, i, name_str); printf("client '%s' (index=%d) now owns name '%s'\n", s->names[client->unique_name_index].name, i, name_str);
} }
} else if (strcmp(member, "GetNameOwner") == 0) { } else if (strcmp(member, "GetNameOwner") == 0) {
char *name = TRY_NONNULL(char*, wire_get_string(&ctx, NULL), -1); char *name = TRYPTR(wire_get_string(&ctx, NULL));
int name_len = strlen(name); int name_len = strlen(name);
if (name_len < 1 || name_len > 256) { if (name_len < 1 || name_len > 256) {
return -1; return -1;
@ -291,35 +304,16 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
struct jb_client *target = jb_server_name_find_client(s, destination_field->t.str); struct jb_client *target = jb_server_name_find_client(s, destination_field->t.str);
if (!target || target->unique_name_index < 0) { if (!target || target->unique_name_index < 0) {
uint8_t error_reply_data[512]; _reply_error("org.freedesktop.DBus.Error.NameHasNoOwner");
memset(error_reply_data, 0, 512);
wire_context_t error_reply_ctx = {
.byte_cursor = 0,
.data = error_reply_data,
.data_len = 511,
};
TRY_NONNEGATIVE(int, wire_compose_error(&error_reply_ctx, &msg, "org.freedesktop.DBus.Error.NameHasNoOwner"), -1);
if (send(s->fds[i].fd, error_reply_data, error_reply_ctx.byte_cursor, 0) != error_reply_ctx.byte_cursor) {
return -1;
}
return 0; return 0;
} }
uint32_t *body_length; _reply_begin("s") {
TRY_NONNEGATIVE(int, wire_compose_reply(&reply_ctx, &msg, "s", &body_length), -1); TRYPTR(wire_set_string(&reply_ctx, s->names[target->unique_name_index].name));
uint32_t body_start = reply_ctx.byte_cursor; } _reply_end()
TRY_NONNULL(char*, wire_set_string(&reply_ctx, s->names[target->unique_name_index].name), -1);
*body_length = reply_ctx.byte_cursor - body_start;
if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) {
return -1;
}
printf("%s\n", s->names[target->unique_name_index].name);
} else { } else {
TRY_NONNEGATIVE(int, wire_compose_error(&reply_ctx, &msg, "org.freedesktop.DBus.Error.UnknownMethod"), -1); _reply_error("org.freedesktop.DBus.Error.UnknownMethod");
if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) { return 0;
return -1;
}
} }
} break; } break;
@ -332,11 +326,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
} break; } break;
default: { default: {
printf("xyz.hippoz.jitterbug.NotImplemented\n"); _reply_error("xyz.hippoz.jitterbug.NotImplemented");
TRY_NONNEGATIVE(int, wire_compose_error(&reply_ctx, &msg, "xyz.hippoz.jitterbug.NotImplemented"), -1);
if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) {
return -1;
}
return -1; return -1;
} break; } break;
} }
@ -347,21 +337,11 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
} }
struct jb_client *target = jb_server_name_find_client(s, destination_field->t.str); struct jb_client *target = jb_server_name_find_client(s, destination_field->t.str);
if (!target) { if (!target) {
uint8_t error_reply_data[512]; _reply_error("org.freedesktop.DBus.Error.NameHasNoOwner");
memset(error_reply_data, 0, 512);
wire_context_t error_reply_ctx = {
.byte_cursor = 0,
.data = error_reply_data,
.data_len = 511,
};
TRY_NONNEGATIVE(int, wire_compose_error(&error_reply_ctx, &msg, "org.freedesktop.DBus.Error.NameHasNoOwner"), -1);
if (send(s->fds[i].fd, error_reply_data, error_reply_ctx.byte_cursor, 0) != error_reply_ctx.byte_cursor) {
return -1;
}
return 0; return 0;
} }
TRY_NONNEGATIVE(int, wire_compose_unicast_reply(&reply_ctx, &ctx, &msg, s->names[client->unique_name_index].name), -1); TRYST(wire_compose_unicast_reply(&reply_ctx, &ctx, &msg, s->names[client->unique_name_index].name));
TRY_NONNEGATIVE(int, send(target->fd, reply_data, reply_ctx.byte_cursor, 0), -1); TRYST(send(target->fd, reply_data, reply_ctx.byte_cursor, 0));
} }
return 0; return 0;
@ -434,7 +414,7 @@ int jb_server_turn(struct jb_server *s)
static const char auth_ok[] = "OK 1234deadbeef\r\n"; static const char auth_ok[] = "OK 1234deadbeef\r\n";
static const int data_buffer_len = 4096; static const int data_buffer_len = 4096;
TRY_NONNEGATIVE(int, poll(s->fds, s->fd_num, -1), -1); TRYST(poll(s->fds, s->fd_num, -1));
for (int i = 0; i < s->fd_num; i++) { for (int i = 0; i < s->fd_num; i++) {
int fd = s->fds[i].fd; int fd = s->fds[i].fd;
@ -443,7 +423,7 @@ int jb_server_turn(struct jb_server *s)
if (fd == s->sock_fd) { if (fd == s->sock_fd) {
// new connection // new connection
int fd = TRY_NONNEGATIVE(int, accept(s->sock_fd, NULL, NULL), -1); int fd = TRYST(accept(s->sock_fd, NULL, NULL));
if (jb_server_client_add(s, fd) == -1) { if (jb_server_client_add(s, fd) == -1) {
close(fd); close(fd);

7
try.h
View file

@ -1,2 +1,5 @@
#define TRY_NONNULL(M_t, M_expr, M_sentinel) ({ M_t _result = (M_expr); if (!_result) { return (M_sentinel); } _result; }) #define TRYPTR_NIL(M_expr) ({ void* _result = (void*)(M_expr); if (!_result) { return (NULL); } _result; })
#define TRY_NONNEGATIVE(M_t, M_expr, M_sentinel) ({ M_t _result = (M_expr); if (_result < 0) { return (M_sentinel); } _result; }) #define TRYPTR(M_expr) ({ void* _result = (void*)(M_expr); if (!_result) { return (-1); } _result; })
#define TRYST_NIL(M_expr) ({ int _result = (int)(M_expr); if (_result < 0) { return (NULL); } _result; })
#define TRYST(M_expr) ({ int _result = (int)(M_expr); if (_result < 0) { return (-1); } _result; })

172
wire.c
View file

@ -43,7 +43,7 @@ _WIRE_DEF_TYPE(boolean, uint32_t, 4);
char *wire_get_string(wire_context_t *c, uint32_t *out_len) char *wire_get_string(wire_context_t *c, uint32_t *out_len)
{ {
uint32_t len = *TRY_NONNULL(uint32_t*, wire_get_u32(c), NULL); uint32_t len = *(uint32_t*)TRYPTR_NIL(wire_get_u32(c));
if (out_len) { if (out_len) {
*out_len = len; *out_len = len;
} }
@ -63,7 +63,7 @@ char *wire_get_string(wire_context_t *c, uint32_t *out_len)
char *wire_set_string(wire_context_t *c, const char *str) char *wire_set_string(wire_context_t *c, const char *str)
{ {
uint32_t len = strlen(str); uint32_t len = strlen(str);
TRY_NONNULL(uint32_t*, wire_set_u32(c, len), NULL); TRYPTR_NIL(wire_set_u32(c, len));
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */ uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
if (new_cursor >= c->data_len) { if (new_cursor >= c->data_len) {
@ -81,7 +81,7 @@ char *wire_set_string(wire_context_t *c, const char *str)
char *wire_get_signature(wire_context_t *c, uint8_t *out_len) char *wire_get_signature(wire_context_t *c, uint8_t *out_len)
{ {
uint8_t len = *TRY_NONNULL(uint8_t*, wire_get_u8(c), NULL); uint8_t len = *(uint8_t*)TRYPTR_NIL(wire_get_u8(c));
if (out_len) { if (out_len) {
*out_len = len; *out_len = len;
} }
@ -101,7 +101,7 @@ char *wire_get_signature(wire_context_t *c, uint8_t *out_len)
char *wire_set_signature(wire_context_t *c, const char *str) char *wire_set_signature(wire_context_t *c, const char *str)
{ {
uint8_t len = strlen(str); uint8_t len = strlen(str);
TRY_NONNULL(uint8_t*, wire_set_u8(c, len), NULL); TRYPTR_NIL(wire_set_u8(c, len));
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */ uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
if (new_cursor >= c->data_len) { if (new_cursor >= c->data_len) {
@ -120,36 +120,36 @@ char *wire_set_signature(wire_context_t *c, const char *str)
int wire_parse_message(wire_context_t *c, wire_message_t *msg) int wire_parse_message(wire_context_t *c, wire_message_t *msg)
{ {
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages // SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
msg->endianness = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); msg->endianness = *(uint8_t*)TRYPTR(wire_get_u8(c));
msg->type = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); msg->type = *(uint8_t*)TRYPTR(wire_get_u8(c));
msg->flags = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); msg->flags = *(uint8_t*)TRYPTR(wire_get_u8(c));
msg->protocol_version = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); msg->protocol_version = *(uint8_t*)TRYPTR(wire_get_u8(c));
msg->body_length = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); msg->body_length = *(uint32_t*)TRYPTR(wire_get_u32(c));
msg->serial = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); msg->serial = *(uint32_t*)TRYPTR(wire_get_u32(c));
msg->header_fields_length = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); msg->header_fields_length = *(uint32_t*)TRYPTR(wire_get_u32(c));
size_t header_fields_end = c->byte_cursor + msg->header_fields_length; size_t header_fields_end = c->byte_cursor + msg->header_fields_length;
while (c->byte_cursor < header_fields_end) { while (c->byte_cursor < header_fields_end) {
uint8_t field_code = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); uint8_t field_code = *(uint8_t*)TRYPTR(wire_get_u8(c));
TRY_NONNULL(char*, wire_get_signature(c, NULL), -1); TRYPTR(wire_get_signature(c, NULL));
switch (field_code) { switch (field_code) {
case DBUS_HEADER_FIELD_PATH: /* through */ case DBUS_HEADER_FIELD_PATH: /* through */
case DBUS_HEADER_FIELD_INTERFACE: /* through */ case DBUS_HEADER_FIELD_INTERFACE: /* through */
case DBUS_HEADER_FIELD_MEMBER: /* through */ case DBUS_HEADER_FIELD_MEMBER: /* through */
case DBUS_HEADER_FIELD_DESTINATION: { case DBUS_HEADER_FIELD_DESTINATION: {
char *str = TRY_NONNULL(char*, wire_get_string(c, NULL), -1); char *str = TRYPTR(wire_get_string(c, NULL));
printf("field: %s\n", str); printf("field: %s\n", str);
msg->fields[field_code].t.str = str; msg->fields[field_code].t.str = str;
} break; } break;
case DBUS_HEADER_FIELD_SIGNATURE: { case DBUS_HEADER_FIELD_SIGNATURE: {
char *str = TRY_NONNULL(char*, wire_get_signature(c, NULL), -1); char *str = TRYPTR(wire_get_signature(c, NULL));
printf("field: %s\n", str); printf("field: %s\n", str);
msg->fields[field_code].t.str = str; msg->fields[field_code].t.str = str;
} break; } break;
case DBUS_HEADER_FIELD_REPLY_SERIAL: { case DBUS_HEADER_FIELD_REPLY_SERIAL: {
uint32_t u = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); uint32_t u = *(uint32_t*)TRYPTR(wire_get_u32(c));
printf("field: %d\n", u); printf("field: %d\n", u);
msg->fields[field_code].t.u32 = u; msg->fields[field_code].t.u32 = u;
} break; } break;
@ -162,7 +162,7 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg)
// Structs are always aligned to `8`. // Structs are always aligned to `8`.
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#id-1.4.6 // SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#id-1.4.6
TRY_NONNULL(bool, wire_align(c, 8), -1); TRYPTR(wire_align(c, 8));
if (msg->header_fields_count++ > WIRE_MESSAGE_MAX_HEADER_FIELDS) { if (msg->header_fields_count++ > WIRE_MESSAGE_MAX_HEADER_FIELDS) {
return -1; return -1;
@ -170,7 +170,7 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg)
} }
/* header ends at 8 byte boundry */ /* header ends at 8 byte boundry */
TRY_NONNULL(bool, wire_align(c, 8), -1); TRYPTR(wire_align(c, 8));
return 0; return 0;
} }
@ -178,13 +178,13 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg)
int wire_compose_reply(wire_context_t *c, wire_message_t *msg, const char *signature, uint32_t **out_body_length) int wire_compose_reply(wire_context_t *c, wire_message_t *msg, const char *signature, uint32_t **out_body_length)
{ {
TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->endianness), -1); /* endianness */ TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_MESSAGE_METHOD_RETURN), -1); /* type */ TRYPTR(wire_set_u8(c, DBUS_MESSAGE_METHOD_RETURN)); /* type */
TRY_NONNULL(uint8_t*, wire_set_u8(c, 0), -1); /* flags */ TRYPTR(wire_set_u8(c, 0)); /* flags */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_PROTOCOL_VERSION), -1); /* protocol_version */ TRYPTR(wire_set_u8(c, DBUS_PROTOCOL_VERSION)); /* protocol_version */
uint32_t *body_length = TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* body length */ uint32_t *body_length = TRYPTR(wire_set_u32(c, 0)); /* body length */
TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->serial+1), -1); /* serial */ TRYPTR(wire_set_u32(c, msg->serial+1)); /* serial */
uint32_t *header_fields_length = TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* header_fields_length */ uint32_t *header_fields_length = TRYPTR(wire_set_u32(c, 0)); /* header_fields_length */
uint32_t header_fields_start = c->byte_cursor; uint32_t header_fields_start = c->byte_cursor;
@ -193,33 +193,33 @@ int wire_compose_reply(wire_context_t *c, wire_message_t *msg, const char *signa
/* SIGNATURE */ /* SIGNATURE */
{ {
/* byte (field code) */ /* byte (field code) */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE));
/* variant */ /* variant */
/* signature */ /* signature */
TRY_NONNULL(char*, wire_set_signature(c, "g"), -1); TRYPTR(wire_set_signature(c, "g"));
/* signature */ /* signature */
TRY_NONNULL(char*, wire_set_signature(c, signature), -1); TRYPTR(wire_set_signature(c, signature));
// need to align to 8 byte boundry after each array element? // need to align to 8 byte boundry after each array element?
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
} }
/* REPLY_SERIAL */ /* REPLY_SERIAL */
{ {
/* byte (field code) */ /* byte (field code) */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL));
/* variant */ /* variant */
/* signature */ /* signature */
TRY_NONNULL(char*, wire_set_signature(c, "u"), -1); TRYPTR(wire_set_signature(c, "u"));
/* reply serial */ /* reply serial */
TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->serial), -1); TRYPTR(wire_set_u32(c, msg->serial));
} }
} }
*header_fields_length = c->byte_cursor - header_fields_start; *header_fields_length = c->byte_cursor - header_fields_start;
// header ends on an 8 byte alignment // header ends on an 8 byte alignment
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
if (out_body_length) { if (out_body_length) {
*out_body_length = body_length; *out_body_length = body_length;
@ -231,13 +231,13 @@ int wire_compose_reply(wire_context_t *c, wire_message_t *msg, const char *signa
int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error_name) int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error_name)
{ {
TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->endianness), -1); /* endianness */ TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_MESSAGE_ERROR), -1); /* type */ TRYPTR(wire_set_u8(c, DBUS_MESSAGE_ERROR)); /* type */
TRY_NONNULL(uint8_t*, wire_set_u8(c, 0), -1); /* flags */ TRYPTR(wire_set_u8(c, 0)); /* flags */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_PROTOCOL_VERSION), -1); /* protocol_version */ TRYPTR(wire_set_u8(c, DBUS_PROTOCOL_VERSION)); /* protocol_version */
TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* body length */ TRYPTR(wire_set_u32(c, 0)); /* body length */
TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->serial+1), -1); /* serial */ TRYPTR(wire_set_u32(c, msg->serial+1)); /* serial */
uint32_t *header_fields_length = TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* header_fields_length */ uint32_t *header_fields_length = TRYPTR(wire_set_u32(c, 0)); /* header_fields_length */
uint32_t header_fields_start = c->byte_cursor; uint32_t header_fields_start = c->byte_cursor;
@ -246,33 +246,33 @@ int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error
/* ERROR_NAME */ /* ERROR_NAME */
{ {
/* byte (field code) */ /* byte (field code) */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_ERROR_NAME), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_ERROR_NAME));
/* variant */ /* variant */
/* signature */ /* signature */
TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); TRYPTR(wire_set_signature(c, "s"));
/* string */ /* string */
TRY_NONNULL(char*, wire_set_string(c, error_name), -1); TRYPTR(wire_set_string(c, error_name));
// need to align to 8 byte boundry after each array element? // need to align to 8 byte boundry after each array element?
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
} }
/* REPLY_SERIAL */ /* REPLY_SERIAL */
{ {
/* byte (field code) */ /* byte (field code) */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL));
/* variant */ /* variant */
/* signature */ /* signature */
TRY_NONNULL(char*, wire_set_signature(c, "u"), -1); TRYPTR(wire_set_signature(c, "u"));
/* reply serial */ /* reply serial */
TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->serial), -1); TRYPTR(wire_set_u32(c, msg->serial));
} }
} }
*header_fields_length = c->byte_cursor - header_fields_start; *header_fields_length = c->byte_cursor - header_fields_start;
// header ends on an 8 byte alignment // header ends on an 8 byte alignment
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
return 0; return 0;
} }
@ -280,13 +280,13 @@ int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error
int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_message_t *msg, char *sender_unique_name) int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_message_t *msg, char *sender_unique_name)
{ {
TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->endianness), -1); /* endianness */ TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->type), -1); /* type */ TRYPTR(wire_set_u8(c, msg->type)); /* type */
TRY_NONNULL(uint8_t*, wire_set_u8(c, 0), -1); /* flags */ TRYPTR(wire_set_u8(c, 0)); /* flags */
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_PROTOCOL_VERSION), -1); /* protocol_version */ TRYPTR(wire_set_u8(c, DBUS_PROTOCOL_VERSION)); /* protocol_version */
TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->body_length), -1); /* body length */ TRYPTR(wire_set_u32(c, msg->body_length)); /* body length */
TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->serial), -1); /* serial */ TRYPTR(wire_set_u32(c, msg->serial)); /* serial */
uint32_t *header_fields_length = TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* header_fields_length */ uint32_t *header_fields_length = TRYPTR(wire_set_u32(c, 0)); /* header_fields_length */
uint32_t header_fields_start = c->byte_cursor; uint32_t header_fields_start = c->byte_cursor;
@ -302,31 +302,31 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
return -1; return -1;
} }
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_PATH), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_PATH));
TRY_NONNULL(char*, wire_set_signature(c, "o"), -1); TRYPTR(wire_set_signature(c, "o"));
TRY_NONNULL(char*, wire_set_string(c, path_field->t.str), -1); TRYPTR(wire_set_string(c, path_field->t.str));
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_INTERFACE), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_INTERFACE));
TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); TRYPTR(wire_set_signature(c, "s"));
TRY_NONNULL(char*, wire_set_string(c, interface_field->t.str), -1); TRYPTR(wire_set_string(c, interface_field->t.str));
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_MEMBER), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_MEMBER));
TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); TRYPTR(wire_set_signature(c, "s"));
TRY_NONNULL(char*, wire_set_string(c, member_field->t.str), -1); TRYPTR(wire_set_string(c, member_field->t.str));
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
if (signature_field->present) { if (signature_field->present) {
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE));
TRY_NONNULL(char*, wire_set_signature(c, "g"), -1); TRYPTR(wire_set_signature(c, "g"));
TRY_NONNULL(char*, wire_set_signature(c, signature_field->t.str), -1); TRYPTR(wire_set_signature(c, signature_field->t.str));
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
} }
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SENDER), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SENDER));
TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); TRYPTR(wire_set_signature(c, "s"));
TRY_NONNULL(char*, wire_set_string(c, sender_unique_name), -1); TRYPTR(wire_set_string(c, sender_unique_name));
// last element, don't align // last element, don't align
} break; } break;
@ -336,22 +336,22 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
return -1; return -1;
} }
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL));
TRY_NONNULL(char*, wire_set_signature(c, "u"), -1); TRYPTR(wire_set_signature(c, "u"));
TRY_NONNULL(uint32_t*, wire_set_u32(c, reply_serial_field->t.u32), -1); TRYPTR(wire_set_u32(c, reply_serial_field->t.u32));
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
wire_message_field_t *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE]; wire_message_field_t *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE];
if (signature_field->present) { if (signature_field->present) {
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE));
TRY_NONNULL(char*, wire_set_signature(c, "g"), -1); TRYPTR(wire_set_signature(c, "g"));
TRY_NONNULL(char*, wire_set_string(c, signature_field->t.str), -1); TRYPTR(wire_set_string(c, signature_field->t.str));
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
} }
TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SENDER), -1); TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SENDER));
TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); TRYPTR(wire_set_signature(c, "s"));
TRY_NONNULL(char*, wire_set_string(c, sender_unique_name), -1); TRYPTR(wire_set_string(c, sender_unique_name));
// last element, don't align // last element, don't align
} break; } break;
@ -363,7 +363,7 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
*header_fields_length = c->byte_cursor - header_fields_start; *header_fields_length = c->byte_cursor - header_fields_start;
// header ends on an 8 byte alignment // header ends on an 8 byte alignment
TRY_NONNULL(bool, wire_write_align(c, 8), -1); TRYPTR(wire_write_align(c, 8));
if (c->byte_cursor + msg->body_length > c->data_len) { if (c->byte_cursor + msg->body_length > c->data_len) {
return -1; return -1;