improve unique name writing

This commit is contained in:
hippoz 2023-01-22 18:18:57 +02:00
parent 2af29378bf
commit 0f69b9b433
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
3 changed files with 28 additions and 5 deletions

View file

@ -215,18 +215,18 @@ BusName *bus_client_assign_unique_name(Bus *s, BusClient *client)
if (client->unique_name) { if (client->unique_name) {
return NULL; 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; return NULL;
} }
char *name_str = malloc(sizeof(char) * 16); char *name_str = malloc(sizeof(char) * UNIQUE_NAME_LENGTH);
if (!name_str) { if (!name_str) {
return NULL; 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); free(name_str);
return NULL; return NULL;
} }

22
wire.c
View file

@ -86,6 +86,26 @@ char *wire_get_string_impl(WireCtx *c, bool as_signature)
return str; 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) char *wire_set_string_impl(WireCtx *c, const char *str, bool as_signature)
{ {
uint32_t len = strlen(str); 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_u8(header_ctx, DBUS_HEADER_FIELD_SENDER));
TRYPTR(wire_set_signature_single_char(header_ctx, 's')); 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)); TRYPTR(wire_write_align(header_ctx, 8));
*header_fields_length = header_ctx->byte_cursor - header_fields_start + msg->header_fields_length; *header_fields_length = header_ctx->byte_cursor - header_fields_start + msg->header_fields_length;

3
wire.h
View file

@ -20,6 +20,8 @@
#define DBUS_FLAG_NO_AUTO_START 0x1 #define DBUS_FLAG_NO_AUTO_START 0x1
#define DBUS_FLAG_ALLOW_INTERACTIVE_AUTHORIZATION 0x1 #define DBUS_FLAG_ALLOW_INTERACTIVE_AUTHORIZATION 0x1
#define UNIQUE_NAME_LENGTH 11
enum { enum {
DBUS_HEADER_FIELD_INVALID = 0, DBUS_HEADER_FIELD_INVALID = 0,
DBUS_HEADER_FIELD_PATH, 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_get_string_impl(WireCtx *c, bool as_signature);
char *wire_set_string_impl(WireCtx *c, const char *str, 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_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_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_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); int wire_compose_error(WireCtx *c, WireMsg *msg, const char *error_name);