From 0586219267f0d3ae8dcfd7b8999dfe4fa9106701 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 25 Dec 2022 20:54:10 +0200 Subject: [PATCH] greatly simplify code --- server.c | 120 ++++++++++++++++---------------------- try.h | 7 ++- wire.c | 172 +++++++++++++++++++++++++++---------------------------- 3 files changed, 141 insertions(+), 158 deletions(-) diff --git a/server.c b/server.c index 3dd116b..01249ac 100644 --- a/server.c +++ b/server.c @@ -189,19 +189,46 @@ ssize_t jb_server_client_recv(struct jb_server *s, int i, void *buf, size_t n) 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) { struct jb_client *client = &s->clients[i]; + wire_message_t msg = {0}; wire_context_t ctx = { .byte_cursor = 0, .data = data, .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]; 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, }; - 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) { case DBUS_MESSAGE_METHOD_CALL: { 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; } - uint32_t *body_length; - TRY_NONNEGATIVE(int, wire_compose_reply(&reply_ctx, &msg, "s", &body_length), -1); - uint32_t body_start = reply_ctx.byte_cursor; - 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; - } + _reply_begin("s") { + TRYPTR(wire_set_string(&reply_ctx, s->names[unique_name_index].name)); + } _reply_end() + printf("assigned unique name '%s' to connection %d\n", s->names[unique_name_index].name, i); } 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); if (name_len < 1 || name_len > 256) { 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; } - uint32_t *body_length; - TRY_NONNEGATIVE(int, wire_compose_reply(&reply_ctx, &msg, "u", &body_length), -1); - uint32_t body_start = reply_ctx.byte_cursor; - 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; - } + _reply_begin("u") { + TRYPTR(wire_set_u32(&reply_ctx, status_code)); + } _reply_end() 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); } } 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); if (name_len < 1 || name_len > 256) { 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); if (!target || target->unique_name_index < 0) { - uint8_t error_reply_data[512]; - 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; - } + _reply_error("org.freedesktop.DBus.Error.NameHasNoOwner"); return 0; } - uint32_t *body_length; - TRY_NONNEGATIVE(int, wire_compose_reply(&reply_ctx, &msg, "s", &body_length), -1); - uint32_t body_start = reply_ctx.byte_cursor; - 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); + _reply_begin("s") { + TRYPTR(wire_set_string(&reply_ctx, s->names[target->unique_name_index].name)); + } _reply_end() } else { - TRY_NONNEGATIVE(int, wire_compose_error(&reply_ctx, &msg, "org.freedesktop.DBus.Error.UnknownMethod"), -1); - if (send(s->fds[i].fd, reply_data, reply_ctx.byte_cursor, 0) != reply_ctx.byte_cursor) { - return -1; - } + _reply_error("org.freedesktop.DBus.Error.UnknownMethod"); + return 0; } } break; @@ -332,11 +326,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data, } break; default: { - printf("xyz.hippoz.jitterbug.NotImplemented\n"); - 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; - } + _reply_error("xyz.hippoz.jitterbug.NotImplemented"); return -1; } 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); if (!target) { - uint8_t error_reply_data[512]; - 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; - } + _reply_error("org.freedesktop.DBus.Error.NameHasNoOwner"); return 0; } - TRY_NONNEGATIVE(int, wire_compose_unicast_reply(&reply_ctx, &ctx, &msg, s->names[client->unique_name_index].name), -1); - TRY_NONNEGATIVE(int, send(target->fd, reply_data, reply_ctx.byte_cursor, 0), -1); + TRYST(wire_compose_unicast_reply(&reply_ctx, &ctx, &msg, s->names[client->unique_name_index].name)); + TRYST(send(target->fd, reply_data, reply_ctx.byte_cursor, 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 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++) { int fd = s->fds[i].fd; @@ -443,7 +423,7 @@ int jb_server_turn(struct jb_server *s) if (fd == s->sock_fd) { // 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) { close(fd); diff --git a/try.h b/try.h index 595cb21..39a5306 100644 --- a/try.h +++ b/try.h @@ -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 TRY_NONNEGATIVE(M_t, M_expr, M_sentinel) ({ M_t _result = (M_expr); if (_result < 0) { return (M_sentinel); } _result; }) +#define TRYPTR_NIL(M_expr) ({ void* _result = (void*)(M_expr); if (!_result) { return (NULL); } _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; }) diff --git a/wire.c b/wire.c index 62cfe50..028691b 100644 --- a/wire.c +++ b/wire.c @@ -43,7 +43,7 @@ _WIRE_DEF_TYPE(boolean, uint32_t, 4); 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) { *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) { 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 */ 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) { - 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) { *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) { 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 */ 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) { // SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages - msg->endianness = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); - msg->type = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); - msg->flags = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); - msg->protocol_version = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); - msg->body_length = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); - msg->serial = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); - msg->header_fields_length = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); + msg->endianness = *(uint8_t*)TRYPTR(wire_get_u8(c)); + msg->type = *(uint8_t*)TRYPTR(wire_get_u8(c)); + msg->flags = *(uint8_t*)TRYPTR(wire_get_u8(c)); + msg->protocol_version = *(uint8_t*)TRYPTR(wire_get_u8(c)); + msg->body_length = *(uint32_t*)TRYPTR(wire_get_u32(c)); + msg->serial = *(uint32_t*)TRYPTR(wire_get_u32(c)); + msg->header_fields_length = *(uint32_t*)TRYPTR(wire_get_u32(c)); size_t header_fields_end = c->byte_cursor + msg->header_fields_length; while (c->byte_cursor < header_fields_end) { - uint8_t field_code = *TRY_NONNULL(uint8_t*, wire_get_u8(c), -1); - TRY_NONNULL(char*, wire_get_signature(c, NULL), -1); + uint8_t field_code = *(uint8_t*)TRYPTR(wire_get_u8(c)); + TRYPTR(wire_get_signature(c, NULL)); switch (field_code) { case DBUS_HEADER_FIELD_PATH: /* through */ case DBUS_HEADER_FIELD_INTERFACE: /* through */ case DBUS_HEADER_FIELD_MEMBER: /* through */ 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); msg->fields[field_code].t.str = str; } break; 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); msg->fields[field_code].t.str = str; } break; 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); msg->fields[field_code].t.u32 = u; } break; @@ -162,7 +162,7 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg) // Structs are always aligned to `8`. // 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) { return -1; @@ -170,7 +170,7 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg) } /* header ends at 8 byte boundry */ - TRY_NONNULL(bool, wire_align(c, 8), -1); + TRYPTR(wire_align(c, 8)); 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) { - TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->endianness), -1); /* endianness */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_MESSAGE_METHOD_RETURN), -1); /* type */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, 0), -1); /* flags */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_PROTOCOL_VERSION), -1); /* protocol_version */ - uint32_t *body_length = TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* body length */ - TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->serial+1), -1); /* serial */ - uint32_t *header_fields_length = TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* header_fields_length */ + TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */ + TRYPTR(wire_set_u8(c, DBUS_MESSAGE_METHOD_RETURN)); /* type */ + TRYPTR(wire_set_u8(c, 0)); /* flags */ + TRYPTR(wire_set_u8(c, DBUS_PROTOCOL_VERSION)); /* protocol_version */ + uint32_t *body_length = TRYPTR(wire_set_u32(c, 0)); /* body length */ + TRYPTR(wire_set_u32(c, msg->serial+1)); /* serial */ + uint32_t *header_fields_length = TRYPTR(wire_set_u32(c, 0)); /* header_fields_length */ 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 */ { /* 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 */ /* signature */ - TRY_NONNULL(char*, wire_set_signature(c, "g"), -1); + TRYPTR(wire_set_signature(c, "g")); /* 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? - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_write_align(c, 8)); } /* REPLY_SERIAL */ { /* 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 */ /* signature */ - TRY_NONNULL(char*, wire_set_signature(c, "u"), -1); + TRYPTR(wire_set_signature(c, "u")); /* 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 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) { *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) { - TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->endianness), -1); /* endianness */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_MESSAGE_ERROR), -1); /* type */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, 0), -1); /* flags */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_PROTOCOL_VERSION), -1); /* protocol_version */ - TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* body length */ - TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->serial+1), -1); /* serial */ - uint32_t *header_fields_length = TRY_NONNULL(uint32_t*, wire_set_u32(c, 0), -1); /* header_fields_length */ + TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */ + TRYPTR(wire_set_u8(c, DBUS_MESSAGE_ERROR)); /* type */ + TRYPTR(wire_set_u8(c, 0)); /* flags */ + TRYPTR(wire_set_u8(c, DBUS_PROTOCOL_VERSION)); /* protocol_version */ + TRYPTR(wire_set_u32(c, 0)); /* body length */ + TRYPTR(wire_set_u32(c, msg->serial+1)); /* serial */ + uint32_t *header_fields_length = TRYPTR(wire_set_u32(c, 0)); /* header_fields_length */ 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 */ { /* 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 */ /* signature */ - TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); + TRYPTR(wire_set_signature(c, "s")); /* 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? - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_write_align(c, 8)); } /* REPLY_SERIAL */ { /* 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 */ /* signature */ - TRY_NONNULL(char*, wire_set_signature(c, "u"), -1); + TRYPTR(wire_set_signature(c, "u")); /* 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 ends on an 8 byte alignment - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_write_align(c, 8)); 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) { - TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->endianness), -1); /* endianness */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, msg->type), -1); /* type */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, 0), -1); /* flags */ - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_PROTOCOL_VERSION), -1); /* protocol_version */ - TRY_NONNULL(uint32_t*, wire_set_u32(c, msg->body_length), -1); /* body length */ - TRY_NONNULL(uint32_t*, 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 */ + TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */ + TRYPTR(wire_set_u8(c, msg->type)); /* type */ + TRYPTR(wire_set_u8(c, 0)); /* flags */ + TRYPTR(wire_set_u8(c, DBUS_PROTOCOL_VERSION)); /* protocol_version */ + TRYPTR(wire_set_u32(c, msg->body_length)); /* body length */ + TRYPTR(wire_set_u32(c, msg->serial)); /* serial */ + uint32_t *header_fields_length = TRYPTR(wire_set_u32(c, 0)); /* header_fields_length */ 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; } - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_PATH), -1); - TRY_NONNULL(char*, wire_set_signature(c, "o"), -1); - TRY_NONNULL(char*, wire_set_string(c, path_field->t.str), -1); - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_PATH)); + TRYPTR(wire_set_signature(c, "o")); + TRYPTR(wire_set_string(c, path_field->t.str)); + TRYPTR(wire_write_align(c, 8)); - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_INTERFACE), -1); - TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); - TRY_NONNULL(char*, wire_set_string(c, interface_field->t.str), -1); - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_INTERFACE)); + TRYPTR(wire_set_signature(c, "s")); + TRYPTR(wire_set_string(c, interface_field->t.str)); + TRYPTR(wire_write_align(c, 8)); - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_MEMBER), -1); - TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); - TRY_NONNULL(char*, wire_set_string(c, member_field->t.str), -1); - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_MEMBER)); + TRYPTR(wire_set_signature(c, "s")); + TRYPTR(wire_set_string(c, member_field->t.str)); + TRYPTR(wire_write_align(c, 8)); if (signature_field->present) { - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE), -1); - TRY_NONNULL(char*, wire_set_signature(c, "g"), -1); - TRY_NONNULL(char*, wire_set_signature(c, signature_field->t.str), -1); - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE)); + TRYPTR(wire_set_signature(c, "g")); + TRYPTR(wire_set_signature(c, signature_field->t.str)); + TRYPTR(wire_write_align(c, 8)); } - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SENDER), -1); - TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); - TRY_NONNULL(char*, wire_set_string(c, sender_unique_name), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SENDER)); + TRYPTR(wire_set_signature(c, "s")); + TRYPTR(wire_set_string(c, sender_unique_name)); // last element, don't align } break; @@ -336,22 +336,22 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me return -1; } - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL), -1); - TRY_NONNULL(char*, wire_set_signature(c, "u"), -1); - TRY_NONNULL(uint32_t*, wire_set_u32(c, reply_serial_field->t.u32), -1); - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_REPLY_SERIAL)); + TRYPTR(wire_set_signature(c, "u")); + TRYPTR(wire_set_u32(c, reply_serial_field->t.u32)); + TRYPTR(wire_write_align(c, 8)); wire_message_field_t *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE]; if (signature_field->present) { - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE), -1); - TRY_NONNULL(char*, wire_set_signature(c, "g"), -1); - TRY_NONNULL(char*, wire_set_string(c, signature_field->t.str), -1); - TRY_NONNULL(bool, wire_write_align(c, 8), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE)); + TRYPTR(wire_set_signature(c, "g")); + TRYPTR(wire_set_string(c, signature_field->t.str)); + TRYPTR(wire_write_align(c, 8)); } - TRY_NONNULL(uint8_t*, wire_set_u8(c, DBUS_HEADER_FIELD_SENDER), -1); - TRY_NONNULL(char*, wire_set_signature(c, "s"), -1); - TRY_NONNULL(char*, wire_set_string(c, sender_unique_name), -1); + TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SENDER)); + TRYPTR(wire_set_signature(c, "s")); + TRYPTR(wire_set_string(c, sender_unique_name)); // last element, don't align } 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 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) { return -1;