From 05f7a1ae41d224c47b6cd26b814a7f86c0e96ba3 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sat, 24 Dec 2022 16:03:55 +0200 Subject: [PATCH] implement GetNameOwner --- server.c | 40 +++++++++++++++++++++++++++++++++++++++- wire.c | 6 +++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/server.c b/server.c index 62251c8..3dd116b 100644 --- a/server.c +++ b/server.c @@ -277,6 +277,44 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data, 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); + int name_len = strlen(name); + if (name_len < 1 || name_len > 256) { + return -1; + } + + char *name_str = string_dup(name); + if (!name_str) { + return -1; + } + + 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; + } + 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); } 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) { @@ -316,7 +354,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data, .data = error_reply_data, .data_len = 511, }; - TRY_NONNEGATIVE(int, wire_compose_error(&error_reply_ctx, &msg, "xyz.hippoz.jitterbug.NotImplemented"), -1); + 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; } diff --git a/wire.c b/wire.c index 74bd137..62cfe50 100644 --- a/wire.c +++ b/wire.c @@ -140,17 +140,17 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg) case DBUS_HEADER_FIELD_MEMBER: /* through */ case DBUS_HEADER_FIELD_DESTINATION: { char *str = TRY_NONNULL(char*, wire_get_string(c, NULL), -1); - //printf("field: %s\n", str); + 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); - //printf("field: %s\n", str); + 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); - //printf("field: %d\n", u); + printf("field: %d\n", u); msg->fields[field_code].t.u32 = u; } break; default: {