implement GetNameOwner

This commit is contained in:
hippoz 2022-12-24 16:03:55 +02:00
parent 3da74dd1ae
commit 05f7a1ae41
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
2 changed files with 42 additions and 4 deletions

View file

@ -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) { 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); 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 { } else {
TRY_NONNEGATIVE(int, wire_compose_error(&reply_ctx, &msg, "org.freedesktop.DBus.Error.UnknownMethod"), -1); 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) { 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 = error_reply_data,
.data_len = 511, .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) { if (send(s->fds[i].fd, error_reply_data, error_reply_ctx.byte_cursor, 0) != error_reply_ctx.byte_cursor) {
return -1; return -1;
} }

6
wire.c
View file

@ -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_MEMBER: /* through */
case DBUS_HEADER_FIELD_DESTINATION: { case DBUS_HEADER_FIELD_DESTINATION: {
char *str = TRY_NONNULL(char*, wire_get_string(c, NULL), -1); 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; msg->fields[field_code].t.str = str;
} break; } break;
case DBUS_HEADER_FIELD_SIGNATURE: { case DBUS_HEADER_FIELD_SIGNATURE: {
char *str = TRY_NONNULL(char*, wire_get_signature(c, NULL), -1); 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; msg->fields[field_code].t.str = str;
} break; } break;
case DBUS_HEADER_FIELD_REPLY_SERIAL: { case DBUS_HEADER_FIELD_REPLY_SERIAL: {
uint32_t u = *TRY_NONNULL(uint32_t*, wire_get_u32(c), -1); 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; msg->fields[field_code].t.u32 = u;
} break; } break;
default: { default: {