From 0f69b9b433403e11e811426b619addb515d87b5d Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 22 Jan 2023 18:18:57 +0200 Subject: [PATCH] improve unique name writing --- server.c | 8 ++++---- wire.c | 22 +++++++++++++++++++++- wire.h | 3 +++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/server.c b/server.c index 269c824..6a11171 100644 --- a/server.c +++ b/server.c @@ -215,18 +215,18 @@ BusName *bus_client_assign_unique_name(Bus *s, BusClient *client) if (client->unique_name) { return NULL; } - uint32_t id = 0; + uint8_t id = 0; - if (read(s->urandom_fd, &id, sizeof(uint32_t)) != sizeof(uint32_t)) { + if (read(s->urandom_fd, &id, sizeof(uint8_t)) != sizeof(uint8_t)) { return NULL; } - char *name_str = malloc(sizeof(char) * 16); + char *name_str = malloc(sizeof(char) * UNIQUE_NAME_LENGTH); if (!name_str) { return NULL; } - if (snprintf(name_str, 16, ":1.%"PRIu32, id) < 0) { + if (snprintf(name_str, UNIQUE_NAME_LENGTH, ":%05"PRIu16".%03"PRIu8, client->fd_index, id) != (UNIQUE_NAME_LENGTH - 1)) { free(name_str); return NULL; } diff --git a/wire.c b/wire.c index 63e2ed4..c728877 100644 --- a/wire.c +++ b/wire.c @@ -86,6 +86,26 @@ char *wire_get_string_impl(WireCtx *c, bool as_signature) return str; } +char *wire_set_string_fixed(WireCtx *c, const char *str, uint32_t len) +{ + // len includes the null byte + + TRYPTR_NIL(wire_set_u32(c, len - 1)); + + uint32_t new_cursor = c->byte_cursor + len; + if (unlikely(new_cursor >= c->data_len)) { + return NULL; + } + + // TODO? + char *dest = (char*)&c->data[c->byte_cursor]; + memcpy(dest, str, len); + + c->byte_cursor = new_cursor; + + return dest; +} + char *wire_set_string_impl(WireCtx *c, const char *str, bool as_signature) { uint32_t len = strlen(str); @@ -435,7 +455,7 @@ int wire_compose_unicast_reply(WireCtx *msg_c, WireMsg *msg, char *sender_unique TRYPTR(wire_set_u8(header_ctx, DBUS_HEADER_FIELD_SENDER)); TRYPTR(wire_set_signature_single_char(header_ctx, 's')); - TRYPTR(wire_set_string(header_ctx, sender_unique_name)); + TRYPTR(wire_set_string_fixed(header_ctx, sender_unique_name, UNIQUE_NAME_LENGTH)); TRYPTR(wire_write_align(header_ctx, 8)); *header_fields_length = header_ctx->byte_cursor - header_fields_start + msg->header_fields_length; diff --git a/wire.h b/wire.h index 447d270..abd3d4c 100644 --- a/wire.h +++ b/wire.h @@ -20,6 +20,8 @@ #define DBUS_FLAG_NO_AUTO_START 0x1 #define DBUS_FLAG_ALLOW_INTERACTIVE_AUTHORIZATION 0x1 +#define UNIQUE_NAME_LENGTH 11 + enum { DBUS_HEADER_FIELD_INVALID = 0, DBUS_HEADER_FIELD_PATH, @@ -126,6 +128,7 @@ _WIRE_DECL_TYPE(boolean, uint32_t, 4) char *wire_get_string_impl(WireCtx *c, bool as_signature); char *wire_set_string_impl(WireCtx *c, const char *str, bool as_signature); char *wire_get_string_check(WireCtx *c, int min_length, int max_length); +char *wire_set_string_fixed(WireCtx *c, const char *str, uint32_t len); int wire_parse_message(WireCtx *c, WireMsg *msg); int wire_compose_reply(WireCtx *c, WireMsg *msg, const char *signature, uint32_t **out_body_length); int wire_compose_error(WireCtx *c, WireMsg *msg, const char *error_name);