diff --git a/server.c b/server.c index 458989c..f4f85b2 100644 --- a/server.c +++ b/server.c @@ -286,7 +286,7 @@ ssize_t bus_client_recv(bus_t *s, int i, void *buf, size_t n) return status; } -int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *sender_unique_name) +int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *sender_unique_name, wire_context_t *reply_ctx) { uint32_t body_end = ctx->byte_cursor + msg->body_length; if (body_end >= ctx->data_len) { @@ -310,18 +310,13 @@ int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, ch match_left--; if (match_rule_check(c->matches[j], msg, ctx) >= 0) { - uint8_t reply_data[4096]; - memset(reply_data, 0, 4096); - wire_context_t reply_ctx = { - .byte_cursor = 0, - .data = reply_data, - .data_len = 4096, - }; - uint32_t previous_cursor = ctx->byte_cursor; - TRYST(wire_compose_unicast_reply(&reply_ctx, ctx, msg, sender_unique_name)); - TRYST(send(c->fd, reply_data, reply_ctx.byte_cursor, 0)); + TRYST(wire_compose_unicast_reply(reply_ctx, ctx, msg, sender_unique_name)); + TRYST(send(c->fd, reply_ctx->data, reply_ctx->byte_cursor, 0)); ctx->byte_cursor = previous_cursor; + if (match_left) { + memset(reply_ctx->data, 0, reply_ctx->data_len); + } } } } @@ -332,24 +327,11 @@ int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, ch return 0; } -int bus_unicast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *target_name, char *sender_unique_name) +int bus_unicast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *target_name, char *sender_unique_name, wire_context_t *reply_ctx) { - bus_client_t *target = bus_name_find_client(s, target_name); - if (!target) { - return -1; - } - - uint8_t reply_data[8192]; - memset(reply_data, 0, 8192); - wire_context_t reply_ctx = { - .byte_cursor = 0, - .data = reply_data, - .data_len = 8192, - }; - - TRYST(wire_compose_unicast_reply(&reply_ctx, ctx, msg, sender_unique_name)); - TRYST(send(target->fd, reply_data, reply_ctx.byte_cursor, 0)); - + bus_client_t *target = TRYPTR(bus_name_find_client(s, target_name)); + TRYST(wire_compose_unicast_reply(reply_ctx, ctx, msg, sender_unique_name)); + TRYST(send(target->fd, reply_ctx->data, reply_ctx->byte_cursor, 0)); return 0; } @@ -647,12 +629,12 @@ int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx) wire_message_field_t *destination_field = &msg.fields[DBUS_HEADER_FIELD_DESTINATION]; wire_message_field_t *member_field = &msg.fields[DBUS_HEADER_FIELD_MEMBER]; - uint8_t reply_data[4096]; - memset(reply_data, 0, 4096); + uint8_t reply_data[8192]; + memset(reply_data, 0, 8192); wire_context_t reply_ctx = { .byte_cursor = 0, .data = reply_data, - .data_len = 4096, + .data_len = 8192, }; if (msg.type == DBUS_MESSAGE_METHOD_CALL && destination_field->present && member_field->present && strcmp(destination_field->t.str, "org.freedesktop.DBus") == 0) { @@ -677,12 +659,12 @@ int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx) } if (destination_field->present) { - if (bus_unicast_message(s, &msg, ctx, destination_field->t.str, s->names[client->unique_name_index].name) < 0) { + if (bus_unicast_message(s, &msg, ctx, destination_field->t.str, s->names[client->unique_name_index].name, &reply_ctx) < 0) { _process_message_reply_error("xyz.hippoz.jitterbug.UnicastFailed"); goto end_nonfatal; } } else { - if (bus_broadcast_message(s, &msg, ctx, s->names[client->unique_name_index].name) < 0) { + if (bus_broadcast_message(s, &msg, ctx, s->names[client->unique_name_index].name, &reply_ctx) < 0) { _process_message_reply_error("xyz.hippoz.jitterbug.BroadcastFailed"); goto end_nonfatal; }