change struct names
This commit is contained in:
parent
796630d74e
commit
2755669b05
8 changed files with 148 additions and 147 deletions
2
main.c
2
main.c
|
@ -27,7 +27,7 @@ int main(int argc, char *argv[])
|
||||||
/* we handle send() errors directly in the code */
|
/* we handle send() errors directly in the code */
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
bus_t *srv = bus_create(socket_path);
|
Bus *srv = bus_create(socket_path);
|
||||||
if (srv == NULL) {
|
if (srv == NULL) {
|
||||||
fprintf(stderr, "server_create failed\n");
|
fprintf(stderr, "server_create failed\n");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
16
match.c
16
match.c
|
@ -8,7 +8,7 @@
|
||||||
#include "try.h"
|
#include "try.h"
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
void match_rule_free(match_rule_t *rule)
|
void match_rule_free(MatchRule *rule)
|
||||||
{
|
{
|
||||||
if (rule) {
|
if (rule) {
|
||||||
free(rule->rule_string);
|
free(rule->rule_string);
|
||||||
|
@ -27,18 +27,18 @@ void match_rule_free(match_rule_t *rule)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match_rule_t *match_rule_from_string(char *d)
|
MatchRule *match_rule_from_string(char *d)
|
||||||
{
|
{
|
||||||
char *key = NULL;
|
char *key = NULL;
|
||||||
char acc[MATCH_RULE_MAX];
|
char acc[MATCH_RULE_MAX];
|
||||||
int acci = 0;
|
int acci = 0;
|
||||||
match_rule_t *rule;
|
MatchRule *rule;
|
||||||
|
|
||||||
if (strlen(d) >= MATCH_RULE_MAX) {
|
if (strlen(d) >= MATCH_RULE_MAX) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
rule = calloc(sizeof(match_rule_t), 1);
|
rule = calloc(sizeof(MatchRule), 1);
|
||||||
if (!rule) {
|
if (!rule) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -178,9 +178,9 @@ fail:
|
||||||
} \
|
} \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
int match_check_sender(bus_t *s, int sender_index, match_rule_t *rule)
|
int match_check_sender(Bus *s, int sender_index, MatchRule *rule)
|
||||||
{
|
{
|
||||||
bus_client_t *sender_client = &s->clients[sender_index];
|
BusClient *sender_client = &s->clients[sender_index];
|
||||||
if (rule->sender) {
|
if (rule->sender) {
|
||||||
if (sender_client->unique_name_index >= 0) {
|
if (sender_client->unique_name_index >= 0) {
|
||||||
if (strcmp(rule->sender, s->names[sender_client->unique_name_index].name) == 0) {
|
if (strcmp(rule->sender, s->names[sender_client->unique_name_index].name) == 0) {
|
||||||
|
@ -197,7 +197,7 @@ int match_check_sender(bus_t *s, int sender_index, match_rule_t *rule)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int match_rule_check(bus_t *s, int sender_index, match_rule_t *rule, wire_message_t *msg, wire_context_t *ctx)
|
int match_rule_check(Bus *s, int sender_index, MatchRule *rule, WireMsg *msg, WireCtx *ctx)
|
||||||
{
|
{
|
||||||
if (rule->type && msg->type != rule->type) {
|
if (rule->type && msg->type != rule->type) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -220,7 +220,7 @@ int match_rule_check(bus_t *s, int sender_index, match_rule_t *rule, wire_messag
|
||||||
/* todo: path_namespace */
|
/* todo: path_namespace */
|
||||||
_check_header_field_str(destination, DBUS_HEADER_FIELD_DESTINATION);
|
_check_header_field_str(destination, DBUS_HEADER_FIELD_DESTINATION);
|
||||||
|
|
||||||
wire_message_body_string_t strings[MATCH_RULE_MAX_ARG];
|
WireMsgBodyString strings[MATCH_RULE_MAX_ARG];
|
||||||
memset(strings, 0, sizeof(strings));
|
memset(strings, 0, sizeof(strings));
|
||||||
/* TODO: handle failure */
|
/* TODO: handle failure */
|
||||||
TRYST(wire_collect_strings(ctx, msg, strings, MATCH_RULE_MAX_ARG));
|
TRYST(wire_collect_strings(ctx, msg, strings, MATCH_RULE_MAX_ARG));
|
||||||
|
|
6
match.h
6
match.h
|
@ -6,8 +6,8 @@
|
||||||
#include "wire.h"
|
#include "wire.h"
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
void match_rule_free(match_rule_t *rule);
|
void match_rule_free(MatchRule *rule);
|
||||||
match_rule_t *match_rule_from_string(char *d);
|
MatchRule *match_rule_from_string(char *d);
|
||||||
int match_rule_check(bus_t *s, int sender_index, match_rule_t *rule, wire_message_t *msg, wire_context_t *ctx);
|
int match_rule_check(Bus *s, int sender_index, MatchRule *rule, WireMsg *msg, WireCtx *ctx);
|
||||||
|
|
||||||
#endif // _JITTERBUG__MATCH_H
|
#endif // _JITTERBUG__MATCH_H
|
||||||
|
|
117
server.c
117
server.c
|
@ -30,7 +30,7 @@ uint64_t hashmap_hash(const char *bytes, size_t bytes_n, size_t map_len)
|
||||||
return (hash % map_len);
|
return (hash % map_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bus_free(bus_t *s)
|
void bus_free(Bus *s)
|
||||||
{
|
{
|
||||||
if (!s) return;
|
if (!s) return;
|
||||||
if (s->sock_fd >= 0) close(s->sock_fd);
|
if (s->sock_fd >= 0) close(s->sock_fd);
|
||||||
|
@ -42,9 +42,9 @@ void bus_free(bus_t *s)
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
bus_t *bus_create(const char *socket_path)
|
Bus *bus_create(const char *socket_path)
|
||||||
{
|
{
|
||||||
bus_t *s = malloc(sizeof(bus_t));
|
Bus *s = malloc(sizeof(Bus));
|
||||||
if (s == NULL) {
|
if (s == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ defer_fail:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_client_add(bus_t *s, int fd)
|
int bus_client_add(Bus *s, int fd)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < BUS_MAX_CLIENTS; i++) {
|
for (int i = 0; i < BUS_MAX_CLIENTS; i++) {
|
||||||
if (s->clients[i].fd < 0) {
|
if (s->clients[i].fd < 0) {
|
||||||
|
@ -137,7 +137,7 @@ int bus_client_add(bus_t *s, int fd)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_name_find(bus_t *s, char *name)
|
int bus_name_find(Bus *s, char *name)
|
||||||
{
|
{
|
||||||
int bucket = hashmap_hash(name, strlen(name), BUS_MAX_NAMES);
|
int bucket = hashmap_hash(name, strlen(name), BUS_MAX_NAMES);
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ int bus_name_find(bus_t *s, char *name)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bus_client_t *bus_name_find_client(bus_t *s, char *name)
|
BusClient *bus_name_find_client(Bus *s, char *name)
|
||||||
{
|
{
|
||||||
int name_index = bus_name_find(s, name);
|
int name_index = bus_name_find(s, name);
|
||||||
if (name_index < 0) {
|
if (name_index < 0) {
|
||||||
|
@ -168,7 +168,7 @@ bus_client_t *bus_name_find_client(bus_t *s, char *name)
|
||||||
return &s->clients[s->names[name_index].client_index];
|
return &s->clients[s->names[name_index].client_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_name_add(bus_t *s, char *name, int client_index)
|
int bus_name_add(Bus *s, char *name, int client_index)
|
||||||
{
|
{
|
||||||
if (bus_name_find(s, name) >= 0) {
|
if (bus_name_find(s, name) >= 0) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -195,9 +195,9 @@ int bus_name_add(bus_t *s, char *name, int client_index)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_client_match_add(bus_t *s, int client_index, char *match)
|
int bus_client_match_add(Bus *s, int client_index, char *match)
|
||||||
{
|
{
|
||||||
bus_client_t *c = &s->clients[client_index];
|
BusClient *c = &s->clients[client_index];
|
||||||
|
|
||||||
for (int i = 0; i < BUS_MAX_MATCH; i++) {
|
for (int i = 0; i < BUS_MAX_MATCH; i++) {
|
||||||
if (!c->matches[i]) {
|
if (!c->matches[i]) {
|
||||||
|
@ -210,9 +210,9 @@ int bus_client_match_add(bus_t *s, int client_index, char *match)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_client_assign_unique_name(bus_t *s, int i)
|
int bus_client_assign_unique_name(Bus *s, int i)
|
||||||
{
|
{
|
||||||
bus_client_t *c = &s->clients[i];
|
BusClient *c = &s->clients[i];
|
||||||
if (c->unique_name_index != -1) {
|
if (c->unique_name_index != -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -228,6 +228,7 @@ int bus_client_assign_unique_name(bus_t *s, int i)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (snprintf(name, 16, ":1.%"PRIu32, id) < 0) {
|
if (snprintf(name, 16, ":1.%"PRIu32, id) < 0) {
|
||||||
|
free(name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,7 +242,7 @@ int bus_client_assign_unique_name(bus_t *s, int i)
|
||||||
return name_index;
|
return name_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_client_assign_own_name(bus_t *s, int i, char *name)
|
int bus_client_assign_own_name(Bus *s, int i, char *name)
|
||||||
{
|
{
|
||||||
if (!name || *name == ':' || *name == '\0') {
|
if (!name || *name == ':' || *name == '\0') {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -252,7 +253,7 @@ int bus_client_assign_own_name(bus_t *s, int i, char *name)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bus_name_remove(bus_t *s, int i)
|
void bus_name_remove(Bus *s, int i)
|
||||||
{
|
{
|
||||||
if (i >= 0) {
|
if (i >= 0) {
|
||||||
if (s->names[i].name) {
|
if (s->names[i].name) {
|
||||||
|
@ -264,9 +265,9 @@ void bus_name_remove(bus_t *s, int i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void bus_client_remove(bus_t *s, int i)
|
void bus_client_remove(Bus *s, int i)
|
||||||
{
|
{
|
||||||
bus_client_t *c = &s->clients[i];
|
BusClient *c = &s->clients[i];
|
||||||
|
|
||||||
if (c->fd >= 0) {
|
if (c->fd >= 0) {
|
||||||
close(c->fd);
|
close(c->fd);
|
||||||
|
@ -294,21 +295,21 @@ void bus_client_remove(bus_t *s, int i)
|
||||||
s->clients_count--;
|
s->clients_count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bus_client_error(bus_t *s, int i, const char *msg)
|
void bus_client_error(Bus *s, int i, const char *msg)
|
||||||
{
|
{
|
||||||
WARN("removing client %d due to error: %s\n", i, msg);
|
WARN("removing client %d due to error: %s\n", i, msg);
|
||||||
send(s->clients[i].fd, msg, strlen(msg), 0);
|
send(s->clients[i].fd, msg, strlen(msg), 0);
|
||||||
bus_client_remove(s, i);
|
bus_client_remove(s, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_broadcast_message(bus_t *s, int sender_index, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx)
|
int bus_broadcast_message(Bus *s, int sender_index, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx)
|
||||||
{
|
{
|
||||||
uint32_t body_end = ctx->byte_cursor + msg->body_length;
|
uint32_t body_end = ctx->byte_cursor + msg->body_length;
|
||||||
if (body_end >= ctx->data_len) {
|
if (body_end >= ctx->data_len) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bus_client_t *sender_client = &s->clients[sender_index];
|
BusClient *sender_client = &s->clients[sender_index];
|
||||||
|
|
||||||
int left = s->clients_count;
|
int left = s->clients_count;
|
||||||
for (int i = 0; i < BUS_MAX_CLIENTS && left > 0; i++) {
|
for (int i = 0; i < BUS_MAX_CLIENTS && left > 0; i++) {
|
||||||
|
@ -318,7 +319,7 @@ int bus_broadcast_message(bus_t *s, int sender_index, wire_message_t *msg, wire_
|
||||||
|
|
||||||
left--;
|
left--;
|
||||||
|
|
||||||
bus_client_t *c = &s->clients[i];
|
BusClient *c = &s->clients[i];
|
||||||
int match_left = c->match_count;
|
int match_left = c->match_count;
|
||||||
for (int j = 0; j < BUS_MAX_MATCH && match_left > 0; j++) {
|
for (int j = 0; j < BUS_MAX_MATCH && match_left > 0; j++) {
|
||||||
if (!c->matches[j]) {
|
if (!c->matches[j]) {
|
||||||
|
@ -345,7 +346,7 @@ int bus_broadcast_message(bus_t *s, int sender_index, wire_message_t *msg, wire_
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_broadcast_signal(bus_t *s, int sender_index, wire_context_t *ctx, wire_message_t *msg)
|
int bus_broadcast_signal(Bus *s, int sender_index, WireCtx *ctx, WireMsg *msg)
|
||||||
{
|
{
|
||||||
int left = s->clients_count;
|
int left = s->clients_count;
|
||||||
for (int i = 0; i < BUS_MAX_CLIENTS && left > 0; i++) {
|
for (int i = 0; i < BUS_MAX_CLIENTS && left > 0; i++) {
|
||||||
|
@ -355,7 +356,7 @@ int bus_broadcast_signal(bus_t *s, int sender_index, wire_context_t *ctx, wire_m
|
||||||
|
|
||||||
left--;
|
left--;
|
||||||
|
|
||||||
bus_client_t *c = &s->clients[i];
|
BusClient *c = &s->clients[i];
|
||||||
int match_left = c->match_count;
|
int match_left = c->match_count;
|
||||||
for (int j = 0; j < BUS_MAX_MATCH && match_left > 0; j++) {
|
for (int j = 0; j < BUS_MAX_MATCH && match_left > 0; j++) {
|
||||||
if (!c->matches[j]) {
|
if (!c->matches[j]) {
|
||||||
|
@ -374,9 +375,9 @@ int bus_broadcast_signal(bus_t *s, int sender_index, wire_context_t *ctx, wire_m
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
int bus_unicast_message(Bus *s, WireMsg *msg, WireCtx *ctx, char *target_name, char *sender_unique_name, WireCtx *reply_ctx)
|
||||||
{
|
{
|
||||||
bus_client_t *target = TRYPTR(bus_name_find_client(s, target_name));
|
BusClient *target = TRYPTR(bus_name_find_client(s, target_name));
|
||||||
TRYST(wire_compose_unicast_reply(reply_ctx, ctx, msg, sender_unique_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));
|
TRYST(send(target->fd, reply_ctx->data, reply_ctx->byte_cursor, 0));
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -387,12 +388,12 @@ int bus_unicast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char
|
||||||
uint32_t signal_body_start = 0; \
|
uint32_t signal_body_start = 0; \
|
||||||
uint8_t signal_reply_data[8192]; \
|
uint8_t signal_reply_data[8192]; \
|
||||||
memset(signal_reply_data, 0, 8192); \
|
memset(signal_reply_data, 0, 8192); \
|
||||||
wire_context_t signal_reply_ctx = { \
|
WireCtx signal_reply_ctx = { \
|
||||||
.byte_cursor = 0, \
|
.byte_cursor = 0, \
|
||||||
.data = signal_reply_data, \
|
.data = signal_reply_data, \
|
||||||
.data_len = 8192, \
|
.data_len = 8192, \
|
||||||
}; \
|
}; \
|
||||||
wire_message_t signal_reply_msg = {0}; \
|
WireMsg signal_reply_msg = {0}; \
|
||||||
TRYST(wire_compose_signal(&signal_reply_ctx, &signal_reply_msg, (M_sig), (M_member), &signal_body_length)); \
|
TRYST(wire_compose_signal(&signal_reply_ctx, &signal_reply_msg, (M_sig), (M_member), &signal_body_length)); \
|
||||||
signal_body_start = signal_reply_ctx.byte_cursor;
|
signal_body_start = signal_reply_ctx.byte_cursor;
|
||||||
|
|
||||||
|
@ -426,7 +427,7 @@ int bus_unicast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char
|
||||||
} \
|
} \
|
||||||
} while(0) \
|
} while(0) \
|
||||||
|
|
||||||
int handle_hello(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_hello(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
(void)ctx;
|
(void)ctx;
|
||||||
|
|
||||||
int unique_name_index = TRYST(bus_client_assign_unique_name(s, i));
|
int unique_name_index = TRYST(bus_client_assign_unique_name(s, i));
|
||||||
|
@ -448,7 +449,7 @@ int handle_hello(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_request_name(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_request_name(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
if (s->clients[i].unique_name_index < 0) {
|
if (s->clients[i].unique_name_index < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -488,10 +489,10 @@ int handle_request_name(bus_t *s, int i, wire_message_t *msg, wire_context_t *ct
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_get_name_owner(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_get_name_owner(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
char *name = TRYPTR(wire_get_name_string(ctx));
|
char *name = TRYPTR(wire_get_name_string(ctx));
|
||||||
|
|
||||||
bus_client_t *target = bus_name_find_client(s, name);
|
BusClient *target = bus_name_find_client(s, name);
|
||||||
if (!target || target->unique_name_index < 0) {
|
if (!target || target->unique_name_index < 0) {
|
||||||
_reply_error("org.freedesktop.DBus.Error.NameHasNoOwner");
|
_reply_error("org.freedesktop.DBus.Error.NameHasNoOwner");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -503,10 +504,10 @@ int handle_get_name_owner(bus_t *s, int i, wire_message_t *msg, wire_context_t *
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_name_has_owner(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_name_has_owner(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
char *name = TRYPTR(wire_get_name_string(ctx));
|
char *name = TRYPTR(wire_get_name_string(ctx));
|
||||||
|
|
||||||
bus_client_t *target = bus_name_find_client(s, name);
|
BusClient *target = bus_name_find_client(s, name);
|
||||||
|
|
||||||
_reply_begin("b") {
|
_reply_begin("b") {
|
||||||
TRYPTR(wire_set_u32(reply_ctx, target ? 1 : 0));
|
TRYPTR(wire_set_u32(reply_ctx, target ? 1 : 0));
|
||||||
|
@ -514,7 +515,7 @@ int handle_name_has_owner(bus_t *s, int i, wire_message_t *msg, wire_context_t *
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_list_names(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_list_names(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
(void)ctx;
|
(void)ctx;
|
||||||
|
|
||||||
_reply_begin("as") {
|
_reply_begin("as") {
|
||||||
|
@ -540,7 +541,7 @@ int handle_list_names(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_list_activatable_names(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_list_activatable_names(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
(void)ctx;
|
(void)ctx;
|
||||||
|
|
||||||
STUB("handle_list_activatable_names", "always returns empty array");
|
STUB("handle_list_activatable_names", "always returns empty array");
|
||||||
|
@ -553,7 +554,7 @@ int handle_list_activatable_names(bus_t *s, int i, wire_message_t *msg, wire_con
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_start_service_by_name(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_start_service_by_name(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
TRYPTR(wire_get_name_string(ctx));
|
TRYPTR(wire_get_name_string(ctx));
|
||||||
|
|
||||||
/* unused flags value */
|
/* unused flags value */
|
||||||
|
@ -567,7 +568,7 @@ int handle_start_service_by_name(bus_t *s, int i, wire_message_t *msg, wire_cont
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_add_match(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_add_match(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
char *match = TRYPTR(wire_get_string_check(ctx, 1, MATCH_RULE_MAX));
|
char *match = TRYPTR(wire_get_string_check(ctx, 1, MATCH_RULE_MAX));
|
||||||
|
|
||||||
VERBOSE("client index %d adding match rule: '%s'\n", i, match);
|
VERBOSE("client index %d adding match rule: '%s'\n", i, match);
|
||||||
|
@ -578,10 +579,10 @@ int handle_add_match(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_remove_match(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_remove_match(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
char *match = TRYPTR(wire_get_string_check(ctx, 1, MATCH_RULE_MAX));
|
char *match = TRYPTR(wire_get_string_check(ctx, 1, MATCH_RULE_MAX));
|
||||||
|
|
||||||
bus_client_t *client = &s->clients[i];
|
BusClient *client = &s->clients[i];
|
||||||
int left = client->match_count;
|
int left = client->match_count;
|
||||||
for (int j = 0; j < BUS_MAX_MATCH && left > 0; j++) {
|
for (int j = 0; j < BUS_MAX_MATCH && left > 0; j++) {
|
||||||
left--;
|
left--;
|
||||||
|
@ -598,7 +599,7 @@ int handle_remove_match(bus_t *s, int i, wire_message_t *msg, wire_context_t *ct
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_get_connection_stats(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_get_connection_stats(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
(void)ctx;
|
(void)ctx;
|
||||||
|
|
||||||
TRYPTR(wire_get_name_string(ctx));
|
TRYPTR(wire_get_name_string(ctx));
|
||||||
|
@ -613,12 +614,12 @@ int handle_get_connection_stats(bus_t *s, int i, wire_message_t *msg, wire_conte
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_get_connection_unix_process_id(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_get_connection_unix_process_id(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
(void)ctx;
|
(void)ctx;
|
||||||
|
|
||||||
char *name = TRYPTR(wire_get_name_string(ctx));
|
char *name = TRYPTR(wire_get_name_string(ctx));
|
||||||
|
|
||||||
bus_client_t *target = bus_name_find_client(s, name);
|
BusClient *target = bus_name_find_client(s, name);
|
||||||
if (!target || target->unique_name_index < 0 || target->fd < 0) {
|
if (!target || target->unique_name_index < 0 || target->fd < 0) {
|
||||||
_reply_error("org.freedesktop.DBus.Error.NameHasNoOwner");
|
_reply_error("org.freedesktop.DBus.Error.NameHasNoOwner");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -638,7 +639,7 @@ int handle_get_connection_unix_process_id(bus_t *s, int i, wire_message_t *msg,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_get_all_match_rules(bus_t *s, int i, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx) {
|
int handle_get_all_match_rules(Bus *s, int i, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx) {
|
||||||
(void)ctx;
|
(void)ctx;
|
||||||
_reply_begin("a{sas}") {
|
_reply_begin("a{sas}") {
|
||||||
uint32_t *outer_array_len = TRYPTR(wire_set_u32(reply_ctx, 0));
|
uint32_t *outer_array_len = TRYPTR(wire_set_u32(reply_ctx, 0));
|
||||||
|
@ -647,7 +648,7 @@ int handle_get_all_match_rules(bus_t *s, int i, wire_message_t *msg, wire_contex
|
||||||
|
|
||||||
int left = s->names_count;
|
int left = s->names_count;
|
||||||
for (int j = 0; j < BUS_MAX_NAMES && left > 0; j++) {
|
for (int j = 0; j < BUS_MAX_NAMES && left > 0; j++) {
|
||||||
bus_name_t *n = &s->names[j];
|
BusName *n = &s->names[j];
|
||||||
if (!n->name || n->client_index < 0 || *n->name != ':') {
|
if (!n->name || n->client_index < 0 || *n->name != ':') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -655,7 +656,7 @@ int handle_get_all_match_rules(bus_t *s, int i, wire_message_t *msg, wire_contex
|
||||||
|
|
||||||
TRYPTR(wire_write_align(reply_ctx, 8)); /* structs always aligned to 8 */
|
TRYPTR(wire_write_align(reply_ctx, 8)); /* structs always aligned to 8 */
|
||||||
|
|
||||||
bus_client_t *c = &s->clients[s->names[j].client_index];
|
BusClient *c = &s->clients[s->names[j].client_index];
|
||||||
|
|
||||||
TRYPTR(wire_set_string(reply_ctx, s->names[j].name)); /* unique name */
|
TRYPTR(wire_set_string(reply_ctx, s->names[j].name)); /* unique name */
|
||||||
|
|
||||||
|
@ -681,7 +682,7 @@ int handle_get_all_match_rules(bus_t *s, int i, wire_message_t *msg, wire_contex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const bus_method_handler_t bus_method_handlers[] = {
|
static const BusMethodHandler bus_method_handlers[] = {
|
||||||
{ "Hello", handle_hello },
|
{ "Hello", handle_hello },
|
||||||
{ "RequestName", handle_request_name },
|
{ "RequestName", handle_request_name },
|
||||||
{ "GetNameOwner", handle_get_name_owner },
|
{ "GetNameOwner", handle_get_name_owner },
|
||||||
|
@ -708,11 +709,11 @@ static const int bus_method_handlers_count = sizeof(bus_method_handlers) / sizeo
|
||||||
} \
|
} \
|
||||||
} while(0) \
|
} while(0) \
|
||||||
|
|
||||||
int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx)
|
int bus_client_process_message(Bus *s, int i, WireCtx *ctx)
|
||||||
{
|
{
|
||||||
bus_client_t *client = &s->clients[i];
|
BusClient *client = &s->clients[i];
|
||||||
|
|
||||||
wire_message_t msg = {0};
|
WireMsg msg = {0};
|
||||||
|
|
||||||
TRYST(wire_parse_message(ctx, &msg));
|
TRYST(wire_parse_message(ctx, &msg));
|
||||||
|
|
||||||
|
@ -721,11 +722,11 @@ int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
wire_message_field_t *destination_field = &msg.fields[DBUS_HEADER_FIELD_DESTINATION];
|
WireMsgField *destination_field = &msg.fields[DBUS_HEADER_FIELD_DESTINATION];
|
||||||
wire_message_field_t *member_field = &msg.fields[DBUS_HEADER_FIELD_MEMBER];
|
WireMsgField *member_field = &msg.fields[DBUS_HEADER_FIELD_MEMBER];
|
||||||
|
|
||||||
uint8_t reply_data[8192];
|
uint8_t reply_data[8192];
|
||||||
wire_context_t reply_ctx = {
|
WireCtx reply_ctx = {
|
||||||
.byte_cursor = 0,
|
.byte_cursor = 0,
|
||||||
.data = reply_data,
|
.data = reply_data,
|
||||||
.data_len = 8192,
|
.data_len = 8192,
|
||||||
|
@ -734,7 +735,7 @@ int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx)
|
||||||
if (msg.type == DBUS_MESSAGE_METHOD_CALL && destination_field->present && member_field->present && strcmp(destination_field->t.str, "org.freedesktop.DBus") == 0) {
|
if (msg.type == DBUS_MESSAGE_METHOD_CALL && destination_field->present && member_field->present && strcmp(destination_field->t.str, "org.freedesktop.DBus") == 0) {
|
||||||
// method call to the bus
|
// method call to the bus
|
||||||
for (int j = 0; j < bus_method_handlers_count; j++) {
|
for (int j = 0; j < bus_method_handlers_count; j++) {
|
||||||
const bus_method_handler_t *handler = &bus_method_handlers[j];
|
const BusMethodHandler *handler = &bus_method_handlers[j];
|
||||||
if (strcmp(handler->name, member_field->t.str) == 0) {
|
if (strcmp(handler->name, member_field->t.str) == 0) {
|
||||||
TRYST(handler->handler(s, i, &msg, ctx, &reply_ctx));
|
TRYST(handler->handler(s, i, &msg, ctx, &reply_ctx));
|
||||||
goto end_nonfatal;
|
goto end_nonfatal;
|
||||||
|
@ -784,9 +785,9 @@ end_nonfatal:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bus_client_drain_messages(bus_t *s, int ci, uint8_t *data, uint32_t data_len)
|
int bus_client_drain_messages(Bus *s, int ci, uint8_t *data, uint32_t data_len)
|
||||||
{
|
{
|
||||||
wire_context_t ctx = {
|
WireCtx ctx = {
|
||||||
.byte_cursor = 0,
|
.byte_cursor = 0,
|
||||||
.data = data,
|
.data = data,
|
||||||
.data_len = data_len
|
.data_len = data_len
|
||||||
|
@ -812,7 +813,7 @@ int bus_client_drain_messages(bus_t *s, int ci, uint8_t *data, uint32_t data_len
|
||||||
}
|
}
|
||||||
|
|
||||||
#define _client_die(m) do { bus_client_error(s,i,m); goto done; } while(0)
|
#define _client_die(m) do { bus_client_error(s,i,m); goto done; } while(0)
|
||||||
int bus_turn(bus_t *s)
|
int bus_turn(Bus *s)
|
||||||
{
|
{
|
||||||
static const char agree_unix_fd[] = "ERROR\r\n";
|
static const char agree_unix_fd[] = "ERROR\r\n";
|
||||||
static const char auth_ok[] = "OK aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n";
|
static const char auth_ok[] = "OK aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n";
|
||||||
|
@ -835,12 +836,12 @@ int bus_turn(bus_t *s)
|
||||||
for (int i = 0; i < s->fd_num; i++) {
|
for (int i = 0; i < s->fd_num; i++) {
|
||||||
int fd = s->fds[i].fd;
|
int fd = s->fds[i].fd;
|
||||||
if (s->fds[i].revents & POLLNVAL) {
|
if (s->fds[i].revents & POLLNVAL) {
|
||||||
ERROR("bus_turn: error: got POLLNVAL for fds[%d]. This is considered a bug in the server implementation.\n", i);
|
ERROR("Busurn: error: got POLLNVAL for fds[%d]. This is considered a bug in the server implementation.\n", i);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (s->fds[i].revents & POLLERR) {
|
if (s->fds[i].revents & POLLERR) {
|
||||||
if (fd == s->sock_fd) {
|
if (fd == s->sock_fd) {
|
||||||
ERROR("bus_turn: error: got POLLERR for sock_fd\n");
|
ERROR("Busurn: error: got POLLERR for sock_fd\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
bus_client_remove(s, i);
|
bus_client_remove(s, i);
|
||||||
|
@ -848,7 +849,7 @@ int bus_turn(bus_t *s)
|
||||||
}
|
}
|
||||||
if (s->fds[i].revents & POLLHUP) {
|
if (s->fds[i].revents & POLLHUP) {
|
||||||
if (fd == s->sock_fd) {
|
if (fd == s->sock_fd) {
|
||||||
ERROR("bus_turn: error: got POLLHUP for sock_fd\n");
|
ERROR("Busurn: error: got POLLHUP for sock_fd\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
bus_client_remove(s, i);
|
bus_client_remove(s, i);
|
||||||
|
@ -881,7 +882,7 @@ int bus_turn(bus_t *s)
|
||||||
|
|
||||||
VERBOSE("\nrecv: got %zd bytes\n", bytes);
|
VERBOSE("\nrecv: got %zd bytes\n", bytes);
|
||||||
|
|
||||||
bus_client_t *c = &s->clients[i];
|
BusClient *c = &s->clients[i];
|
||||||
switch (c->state) {
|
switch (c->state) {
|
||||||
case BUS_CLIENT_STATE_WAIT_AUTH: {
|
case BUS_CLIENT_STATE_WAIT_AUTH: {
|
||||||
// The D-Bus authentication protocol is a simple plain text protocol.
|
// The D-Bus authentication protocol is a simple plain text protocol.
|
||||||
|
|
38
server.h
38
server.h
|
@ -8,13 +8,13 @@
|
||||||
#define MATCH_RULE_MAX 1024
|
#define MATCH_RULE_MAX 1024
|
||||||
#define MATCH_RULE_MAX_ARG 12
|
#define MATCH_RULE_MAX_ARG 12
|
||||||
|
|
||||||
typedef struct match_rule {
|
typedef struct {
|
||||||
uint8_t type;
|
uint8_t type;
|
||||||
bool eavesdrop;
|
bool eavesdrop;
|
||||||
char *sender, *interface, *member, *path, *path_namespace, *destination, *arg0namespace, *rule_string;
|
char *sender, *interface, *member, *path, *path_namespace, *destination, *arg0namespace, *rule_string;
|
||||||
char *arg[MATCH_RULE_MAX_ARG]; /* TODO: spec states that indexes 0 to 63 should be supported */
|
char *arg[MATCH_RULE_MAX_ARG]; /* TODO: spec states that indexes 0 to 63 should be supported */
|
||||||
char *arg_path[MATCH_RULE_MAX_ARG]; /* TODO: spec states that indexes 0 to 63 should be supported */
|
char *arg_path[MATCH_RULE_MAX_ARG]; /* TODO: spec states that indexes 0 to 63 should be supported */
|
||||||
} match_rule_t;
|
} MatchRule;
|
||||||
|
|
||||||
// TODO: dynamically size the arrays
|
// TODO: dynamically size the arrays
|
||||||
#define BUS_MAX_CLIENTS 256
|
#define BUS_MAX_CLIENTS 256
|
||||||
|
@ -30,42 +30,42 @@ enum {
|
||||||
BUS_CLIENT_STATE_READY
|
BUS_CLIENT_STATE_READY
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct bus_client {
|
typedef struct {
|
||||||
int fd;
|
int fd;
|
||||||
int16_t unique_name_index;
|
int16_t unique_name_index;
|
||||||
uint8_t state;
|
uint8_t state;
|
||||||
int8_t match_count;
|
int8_t match_count;
|
||||||
match_rule_t *matches[BUS_MAX_MATCH];
|
MatchRule *matches[BUS_MAX_MATCH];
|
||||||
int16_t owned_names[BUS_NAMES_PER_CLIENT];
|
int16_t owned_names[BUS_NAMES_PER_CLIENT];
|
||||||
} bus_client_t;
|
} BusClient;
|
||||||
|
|
||||||
typedef struct bus_name {
|
typedef struct {
|
||||||
char *name;
|
char *name;
|
||||||
int32_t client_index;
|
int32_t client_index;
|
||||||
} bus_name_t;
|
} BusName;
|
||||||
|
|
||||||
typedef struct bus {
|
typedef struct {
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
int fd_num;
|
int fd_num;
|
||||||
int names_count;
|
int names_count;
|
||||||
int clients_count;
|
int clients_count;
|
||||||
int urandom_fd;
|
int urandom_fd;
|
||||||
bus_client_t clients[BUS_MAX_CLIENTS];
|
BusClient clients[BUS_MAX_CLIENTS];
|
||||||
bus_name_t names[BUS_MAX_NAMES];
|
BusName names[BUS_MAX_NAMES];
|
||||||
struct pollfd fds[BUS_MAX_CLIENTS + 1];
|
struct pollfd fds[BUS_MAX_CLIENTS + 1];
|
||||||
} bus_t;
|
} Bus;
|
||||||
|
|
||||||
typedef struct bus_method_handler {
|
typedef struct {
|
||||||
const char *name;
|
const char *name;
|
||||||
int (*handler)(bus_t *bus, int client_index, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx);
|
int (*handler)(Bus *bus, int client_index, WireMsg *msg, WireCtx *ctx, WireCtx *reply_ctx);
|
||||||
} bus_method_handler_t;
|
} BusMethodHandler;
|
||||||
|
|
||||||
|
|
||||||
int bus_client_add(bus_t *s, int fd);
|
int bus_client_add(Bus *s, int fd);
|
||||||
void bus_client_remove(bus_t *s, int i);
|
void bus_client_remove(Bus *s, int i);
|
||||||
void bus_free(bus_t *s);
|
void bus_free(Bus *s);
|
||||||
bus_t *bus_create(const char *socket_path);
|
Bus *bus_create(const char *socket_path);
|
||||||
int bus_turn(bus_t *s);
|
int bus_turn(Bus *s);
|
||||||
|
|
||||||
|
|
||||||
#endif // _JITTERBUG__SERVER_H
|
#endif // _JITTERBUG__SERVER_H
|
10
try.h
10
try.h
|
@ -7,7 +7,7 @@
|
||||||
#ifdef JB_ENABLE_TRY_TRACE
|
#ifdef JB_ENABLE_TRY_TRACE
|
||||||
#define __TRY_TRACE() do { \
|
#define __TRY_TRACE() do { \
|
||||||
printf("trace: %s:%d\n", __FILE__, __LINE__); \
|
printf("trace: %s:%d\n", __FILE__, __LINE__); \
|
||||||
} while(0)
|
} while(0);
|
||||||
#else
|
#else
|
||||||
#define __TRY_TRACE()
|
#define __TRY_TRACE()
|
||||||
#endif // JB_ENABLE_TRY_TRACE
|
#endif // JB_ENABLE_TRY_TRACE
|
||||||
|
@ -16,10 +16,10 @@
|
||||||
// NOTE: The macro(s) below make use of a GCC extension called "Statement Exprs", which is nonstandard.
|
// NOTE: The macro(s) below make use of a GCC extension called "Statement Exprs", which is nonstandard.
|
||||||
// More info: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
|
// More info: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
|
||||||
|
|
||||||
#define TRYPTR_NIL(M_expr) __extension__({ void* _result = (void*)(M_expr); if (unlikely(!_result)) { __TRY_TRACE(); return (NULL); } _result; })
|
#define TRYPTR_NIL(M_expr) __extension__({ void* _result = (void*)(M_expr); if (unlikely(!_result)) { __TRY_TRACE() return (NULL); } _result; })
|
||||||
#define TRYPTR(M_expr) __extension__({ void* _result = (void*)(M_expr); if (unlikely(!_result)) { __TRY_TRACE(); return (-1); } _result; })
|
#define TRYPTR(M_expr) __extension__({ void* _result = (void*)(M_expr); if (unlikely(!_result)) { __TRY_TRACE() return (-1); } _result; })
|
||||||
|
|
||||||
#define TRYST_NIL(M_expr) __extension__({ int _result = (int)(M_expr); if (unlikely(_result < 0)) { __TRY_TRACE(); return (NULL); } _result; })
|
#define TRYST_NIL(M_expr) __extension__({ int _result = (int)(M_expr); if (unlikely(_result < 0)) { __TRY_TRACE() return (NULL); } _result; })
|
||||||
#define TRYST(M_expr) __extension__({ int _result = (int)(M_expr); if (unlikely(_result < 0)) { __TRY_TRACE(); return (-1); } _result; })
|
#define TRYST(M_expr) __extension__({ int _result = (int)(M_expr); if (unlikely(_result < 0)) { __TRY_TRACE() return (-1); } _result; })
|
||||||
|
|
||||||
#endif // __JITTERBUG_TRY_H
|
#endif // __JITTERBUG_TRY_H
|
||||||
|
|
40
wire.c
40
wire.c
|
@ -4,7 +4,7 @@
|
||||||
#include "wire.h"
|
#include "wire.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
bool wire_align(wire_context_t *c, uint32_t alignment)
|
bool wire_align(WireCtx *c, uint32_t alignment)
|
||||||
{
|
{
|
||||||
if ((c->byte_cursor % alignment) == 0) {
|
if ((c->byte_cursor % alignment) == 0) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -17,7 +17,7 @@ bool wire_align(wire_context_t *c, uint32_t alignment)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wire_write_align(wire_context_t *c, uint32_t alignment)
|
bool wire_write_align(WireCtx *c, uint32_t alignment)
|
||||||
{
|
{
|
||||||
if ((c->byte_cursor % alignment) == 0) {
|
if ((c->byte_cursor % alignment) == 0) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -41,7 +41,7 @@ _WIRE_DEF_TYPE(i32, int32_t, 4)
|
||||||
_WIRE_DEF_TYPE(u32, uint32_t, 4)
|
_WIRE_DEF_TYPE(u32, uint32_t, 4)
|
||||||
_WIRE_DEF_TYPE(boolean, uint32_t, 4)
|
_WIRE_DEF_TYPE(boolean, uint32_t, 4)
|
||||||
|
|
||||||
char *wire_get_string_impl(wire_context_t *c, bool as_signature)
|
char *wire_get_string_impl(WireCtx *c, bool as_signature)
|
||||||
{
|
{
|
||||||
uint32_t len = 0;
|
uint32_t len = 0;
|
||||||
if (as_signature) {
|
if (as_signature) {
|
||||||
|
@ -67,7 +67,7 @@ char *wire_get_string_impl(wire_context_t *c, bool as_signature)
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *wire_set_string_impl(wire_context_t *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);
|
||||||
if (as_signature) {
|
if (as_signature) {
|
||||||
|
@ -90,14 +90,14 @@ char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *wire_get_string_check(wire_context_t *c, int min_length, int max_length) {
|
char *wire_get_string_check(WireCtx *c, int min_length, int max_length) {
|
||||||
char *str = TRYPTR_NIL(wire_get_string_impl(c, false));
|
char *str = TRYPTR_NIL(wire_get_string_impl(c, false));
|
||||||
int length = strlen(str);
|
int length = strlen(str);
|
||||||
if (unlikely(length < min_length || length > max_length)) return NULL;
|
if (unlikely(length < min_length || length > max_length)) return NULL;
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wire_parse_message(wire_context_t *c, wire_message_t *msg)
|
int wire_parse_message(WireCtx *c, WireMsg *msg)
|
||||||
{
|
{
|
||||||
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
|
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
|
||||||
msg->endianness = *(uint8_t*)TRYPTR(wire_get_u8(c));
|
msg->endianness = *(uint8_t*)TRYPTR(wire_get_u8(c));
|
||||||
|
@ -165,7 +165,7 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wire_compose_signal(wire_context_t *c, wire_message_t *out_msg, const char *signature, const char *member, uint32_t **out_body_length)
|
int wire_compose_signal(WireCtx *c, WireMsg *out_msg, const char *signature, const char *member, uint32_t **out_body_length)
|
||||||
{
|
{
|
||||||
out_msg->endianness = 'l';
|
out_msg->endianness = 'l';
|
||||||
TRYPTR(wire_set_u8(c, 'l')); /* endianness */
|
TRYPTR(wire_set_u8(c, 'l')); /* endianness */
|
||||||
|
@ -278,7 +278,7 @@ int wire_compose_signal(wire_context_t *c, wire_message_t *out_msg, const char *
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wire_compose_reply(wire_context_t *c, wire_message_t *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)
|
||||||
{
|
{
|
||||||
|
|
||||||
TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
|
TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
|
||||||
|
@ -345,7 +345,7 @@ int wire_compose_reply(wire_context_t *c, wire_message_t *msg, const char *signa
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error_name)
|
int wire_compose_error(WireCtx *c, WireMsg *msg, const char *error_name)
|
||||||
{
|
{
|
||||||
|
|
||||||
TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
|
TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
|
||||||
|
@ -394,7 +394,7 @@ int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_message_t *msg, char *sender_unique_name)
|
int wire_compose_unicast_reply(WireCtx *c, WireCtx *msg_c, WireMsg *msg, char *sender_unique_name)
|
||||||
{
|
{
|
||||||
TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
|
TRYPTR(wire_set_u8(c, msg->endianness)); /* endianness */
|
||||||
TRYPTR(wire_set_u8(c, msg->type)); /* type */
|
TRYPTR(wire_set_u8(c, msg->type)); /* type */
|
||||||
|
@ -411,10 +411,10 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
|
||||||
case DBUS_MESSAGE_METHOD_CALL: {
|
case DBUS_MESSAGE_METHOD_CALL: {
|
||||||
// these fields are required for DBUS_MESSAGE_METHOD_CALL, so we must (hackily) check for their presence
|
// these fields are required for DBUS_MESSAGE_METHOD_CALL, so we must (hackily) check for their presence
|
||||||
// interface is not strictly required by the spec, however we require it here anyways
|
// interface is not strictly required by the spec, however we require it here anyways
|
||||||
wire_message_field_t *path_field = &msg->fields[DBUS_HEADER_FIELD_PATH];
|
WireMsgField *path_field = &msg->fields[DBUS_HEADER_FIELD_PATH];
|
||||||
wire_message_field_t *interface_field = &msg->fields[DBUS_HEADER_FIELD_INTERFACE];
|
WireMsgField *interface_field = &msg->fields[DBUS_HEADER_FIELD_INTERFACE];
|
||||||
wire_message_field_t *member_field = &msg->fields[DBUS_HEADER_FIELD_MEMBER];
|
WireMsgField *member_field = &msg->fields[DBUS_HEADER_FIELD_MEMBER];
|
||||||
wire_message_field_t *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE];
|
WireMsgField *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE];
|
||||||
if (!path_field->present || !interface_field->present || !interface_field->present) {
|
if (!path_field->present || !interface_field->present || !interface_field->present) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -448,7 +448,7 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case DBUS_MESSAGE_METHOD_RETURN: {
|
case DBUS_MESSAGE_METHOD_RETURN: {
|
||||||
wire_message_field_t *reply_serial_field = &msg->fields[DBUS_HEADER_FIELD_REPLY_SERIAL];
|
WireMsgField *reply_serial_field = &msg->fields[DBUS_HEADER_FIELD_REPLY_SERIAL];
|
||||||
if (!reply_serial_field->present) {
|
if (!reply_serial_field->present) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -458,7 +458,7 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
|
||||||
TRYPTR(wire_set_u32(c, reply_serial_field->t.u32));
|
TRYPTR(wire_set_u32(c, reply_serial_field->t.u32));
|
||||||
TRYPTR(wire_write_align(c, 8));
|
TRYPTR(wire_write_align(c, 8));
|
||||||
|
|
||||||
wire_message_field_t *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE];
|
WireMsgField *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE];
|
||||||
if (signature_field->present) {
|
if (signature_field->present) {
|
||||||
TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE));
|
TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE));
|
||||||
TRYPTR(wire_set_signature(c, "g"));
|
TRYPTR(wire_set_signature(c, "g"));
|
||||||
|
@ -473,8 +473,8 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case DBUS_MESSAGE_ERROR: {
|
case DBUS_MESSAGE_ERROR: {
|
||||||
wire_message_field_t *reply_serial_field = &msg->fields[DBUS_HEADER_FIELD_REPLY_SERIAL];
|
WireMsgField *reply_serial_field = &msg->fields[DBUS_HEADER_FIELD_REPLY_SERIAL];
|
||||||
wire_message_field_t *error_name_field = &msg->fields[DBUS_HEADER_FIELD_ERROR_NAME];
|
WireMsgField *error_name_field = &msg->fields[DBUS_HEADER_FIELD_ERROR_NAME];
|
||||||
if (!reply_serial_field->present || !error_name_field->present) {
|
if (!reply_serial_field->present || !error_name_field->present) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -484,7 +484,7 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
|
||||||
TRYPTR(wire_set_u32(c, reply_serial_field->t.u32));
|
TRYPTR(wire_set_u32(c, reply_serial_field->t.u32));
|
||||||
TRYPTR(wire_write_align(c, 8));
|
TRYPTR(wire_write_align(c, 8));
|
||||||
|
|
||||||
wire_message_field_t *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE];
|
WireMsgField *signature_field = &msg->fields[DBUS_HEADER_FIELD_SIGNATURE];
|
||||||
if (signature_field->present) {
|
if (signature_field->present) {
|
||||||
TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE));
|
TRYPTR(wire_set_u8(c, DBUS_HEADER_FIELD_SIGNATURE));
|
||||||
TRYPTR(wire_set_signature(c, "g"));
|
TRYPTR(wire_set_signature(c, "g"));
|
||||||
|
@ -524,7 +524,7 @@ int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_me
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wire_collect_strings(wire_context_t *c, wire_message_t *msg, wire_message_body_string_t *strings, int strings_count)
|
int wire_collect_strings(WireCtx *c, WireMsg *msg, WireMsgBodyString *strings, int strings_count)
|
||||||
{
|
{
|
||||||
if (!msg->fields[DBUS_HEADER_FIELD_SIGNATURE].present) {
|
if (!msg->fields[DBUS_HEADER_FIELD_SIGNATURE].present) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
66
wire.h
66
wire.h
|
@ -38,47 +38,47 @@ enum {
|
||||||
DBUS_MESSAGE_SIGNAL,
|
DBUS_MESSAGE_SIGNAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct wire_context {
|
typedef struct {
|
||||||
uint32_t byte_cursor;
|
uint32_t byte_cursor;
|
||||||
uint8_t *data;
|
uint8_t *data;
|
||||||
size_t data_len;
|
size_t data_len;
|
||||||
} wire_context_t;
|
} WireCtx;
|
||||||
|
|
||||||
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
|
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
|
||||||
typedef struct wire_message_field {
|
typedef struct {
|
||||||
bool present;
|
bool present;
|
||||||
union {
|
union {
|
||||||
char *str;
|
char *str;
|
||||||
uint32_t u32;
|
uint32_t u32;
|
||||||
} t;
|
} t;
|
||||||
} wire_message_field_t;
|
} WireMsgField;
|
||||||
#define WIRE_MESSAGE_MAX_HEADER_FIELDS 9
|
#define WIRE_MESSAGE_MAX_HEADER_FIELDS 9
|
||||||
typedef struct wire_message {
|
typedef struct {
|
||||||
uint8_t endianness, type, flags, protocol_version;
|
uint8_t endianness, type, flags, protocol_version;
|
||||||
uint32_t body_length, serial, header_fields_length;
|
uint32_t body_length, serial, header_fields_length;
|
||||||
uint8_t header_fields_count;
|
uint8_t header_fields_count;
|
||||||
wire_message_field_t fields[WIRE_MESSAGE_MAX_HEADER_FIELDS]; /* cannot have more than 9 header fields */
|
WireMsgField fields[WIRE_MESSAGE_MAX_HEADER_FIELDS]; /* cannot have more than 9 header fields */
|
||||||
} wire_message_t;
|
} WireMsg;
|
||||||
typedef struct wire_message_body_string {
|
typedef struct {
|
||||||
uint16_t index;
|
uint16_t index;
|
||||||
char *str;
|
char *str;
|
||||||
} wire_message_body_string_t;
|
} WireMsgBodyString;
|
||||||
|
|
||||||
#define _WIRE_DECL_BYTE_TYPE(M_mnemonic, M_type) \
|
#define _WIRE_DECL_BYTE_TYPE(M_mnemonic, M_type) \
|
||||||
M_type *wire_get_##M_mnemonic(wire_context_t *c); \
|
M_type *wire_get_##M_mnemonic(WireCtx *c); \
|
||||||
M_type *wire_set_##M_mnemonic(wire_context_t *c, M_type val); \
|
M_type *wire_set_##M_mnemonic(WireCtx *c, M_type val); \
|
||||||
|
|
||||||
#define _WIRE_DECL_TYPE(M_mnemonic, M_type, M_alignment) \
|
#define _WIRE_DECL_TYPE(M_mnemonic, M_type, M_alignment) \
|
||||||
M_type *wire_get_##M_mnemonic(wire_context_t *c); \
|
M_type *wire_get_##M_mnemonic(WireCtx *c); \
|
||||||
M_type *wire_set_##M_mnemonic(wire_context_t *c, M_type val); \
|
M_type *wire_set_##M_mnemonic(WireCtx *c, M_type val); \
|
||||||
|
|
||||||
#define _WIRE_DEF_BYTE_TYPE(M_mnemonic, M_type) \
|
#define _WIRE_DEF_BYTE_TYPE(M_mnemonic, M_type) \
|
||||||
M_type *wire_get_##M_mnemonic(wire_context_t *restrict c) { \
|
M_type *wire_get_##M_mnemonic(WireCtx *restrict c) { \
|
||||||
if (unlikely(c->byte_cursor + 1 >= c->data_len)) return NULL; \
|
if (unlikely(c->byte_cursor + 1 >= c->data_len)) return NULL; \
|
||||||
M_type *value = (M_type*)&c->data[c->byte_cursor++]; \
|
M_type *value = (M_type*)&c->data[c->byte_cursor++]; \
|
||||||
return value; \
|
return value; \
|
||||||
} \
|
} \
|
||||||
M_type *wire_set_##M_mnemonic(wire_context_t *restrict c, M_type val) { \
|
M_type *wire_set_##M_mnemonic(WireCtx *restrict c, M_type val) { \
|
||||||
if (unlikely(c->byte_cursor + 1 >= c->data_len)) return NULL; \
|
if (unlikely(c->byte_cursor + 1 >= c->data_len)) return NULL; \
|
||||||
M_type *dest = (M_type*)&c->data[c->byte_cursor++]; \
|
M_type *dest = (M_type*)&c->data[c->byte_cursor++]; \
|
||||||
*dest = val; \
|
*dest = val; \
|
||||||
|
@ -86,7 +86,7 @@ typedef struct wire_message_body_string {
|
||||||
} \
|
} \
|
||||||
|
|
||||||
#define _WIRE_DEF_TYPE(M_mnemonic, M_type, M_alignment) \
|
#define _WIRE_DEF_TYPE(M_mnemonic, M_type, M_alignment) \
|
||||||
M_type *wire_get_##M_mnemonic(wire_context_t *restrict c) { \
|
M_type *wire_get_##M_mnemonic(WireCtx *restrict c) { \
|
||||||
if (unlikely(!wire_align(c, M_alignment))) return NULL; \
|
if (unlikely(!wire_align(c, M_alignment))) return NULL; \
|
||||||
uint32_t new_cursor = c->byte_cursor + sizeof(M_type); \
|
uint32_t new_cursor = c->byte_cursor + sizeof(M_type); \
|
||||||
if (unlikely(new_cursor >= c->data_len)) return NULL; \
|
if (unlikely(new_cursor >= c->data_len)) return NULL; \
|
||||||
|
@ -94,7 +94,7 @@ typedef struct wire_message_body_string {
|
||||||
c->byte_cursor = new_cursor; \
|
c->byte_cursor = new_cursor; \
|
||||||
return value; \
|
return value; \
|
||||||
} \
|
} \
|
||||||
M_type *wire_set_##M_mnemonic(wire_context_t *restrict c, M_type val) { \
|
M_type *wire_set_##M_mnemonic(WireCtx *restrict c, M_type val) { \
|
||||||
if (unlikely(!wire_write_align(c, M_alignment))) return NULL; \
|
if (unlikely(!wire_write_align(c, M_alignment))) return NULL; \
|
||||||
uint32_t new_cursor = c->byte_cursor + sizeof(M_type); \
|
uint32_t new_cursor = c->byte_cursor + sizeof(M_type); \
|
||||||
if (unlikely(new_cursor >= c->data_len)) return NULL; \
|
if (unlikely(new_cursor >= c->data_len)) return NULL; \
|
||||||
|
@ -104,8 +104,8 @@ typedef struct wire_message_body_string {
|
||||||
return dest; \
|
return dest; \
|
||||||
} \
|
} \
|
||||||
|
|
||||||
bool wire_align(wire_context_t *c, uint32_t alignment);
|
bool wire_align(WireCtx *c, uint32_t alignment);
|
||||||
bool wire_write_align(wire_context_t *c, uint32_t alignment);
|
bool wire_write_align(WireCtx *c, uint32_t alignment);
|
||||||
|
|
||||||
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#id-1.4.6
|
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#id-1.4.6
|
||||||
_WIRE_DECL_BYTE_TYPE(i8, int8_t)
|
_WIRE_DECL_BYTE_TYPE(i8, int8_t)
|
||||||
|
@ -117,34 +117,34 @@ _WIRE_DECL_TYPE(u32, uint32_t, 4)
|
||||||
_WIRE_DECL_TYPE(boolean, uint32_t, 4)
|
_WIRE_DECL_TYPE(boolean, uint32_t, 4)
|
||||||
|
|
||||||
|
|
||||||
char *wire_get_string_impl(wire_context_t *c, bool as_signature);
|
char *wire_get_string_impl(WireCtx *c, bool as_signature);
|
||||||
char *wire_set_string_impl(wire_context_t *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(wire_context_t *c, int min_length, int max_length);
|
char *wire_get_string_check(WireCtx *c, int min_length, int max_length);
|
||||||
int wire_parse_message(wire_context_t *c, wire_message_t *msg);
|
int wire_parse_message(WireCtx *c, WireMsg *msg);
|
||||||
int wire_compose_reply(wire_context_t *c, wire_message_t *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(wire_context_t *c, wire_message_t *msg, const char *error_name);
|
int wire_compose_error(WireCtx *c, WireMsg *msg, const char *error_name);
|
||||||
int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_message_t *msg, char *sender_unique_name);
|
int wire_compose_unicast_reply(WireCtx *c, WireCtx *msg_c, WireMsg *msg, char *sender_unique_name);
|
||||||
int wire_compose_signal(wire_context_t *c, wire_message_t *out_msg, const char *signature, const char *member, uint32_t **out_body_length);
|
int wire_compose_signal(WireCtx *c, WireMsg *out_msg, const char *signature, const char *member, uint32_t **out_body_length);
|
||||||
int wire_collect_strings(wire_context_t *c, wire_message_t *msg, wire_message_body_string_t *strings, int strings_count);
|
int wire_collect_strings(WireCtx *c, WireMsg *msg, WireMsgBodyString *strings, int strings_count);
|
||||||
|
|
||||||
static inline char *wire_get_signature(wire_context_t *c) {
|
static inline char *wire_get_signature(WireCtx *c) {
|
||||||
return wire_get_string_impl(c, true);
|
return wire_get_string_impl(c, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline char *wire_set_signature(wire_context_t *c, const char *str) {
|
static inline char *wire_set_signature(WireCtx *c, const char *str) {
|
||||||
return wire_set_string_impl(c, str, true);
|
return wire_set_string_impl(c, str, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline char *wire_get_string(wire_context_t *c) {
|
static inline char *wire_get_string(WireCtx *c) {
|
||||||
return wire_get_string_impl(c, false);
|
return wire_get_string_impl(c, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline char *wire_set_string(wire_context_t *c, const char *str) {
|
static inline char *wire_set_string(WireCtx *c, const char *str) {
|
||||||
return wire_set_string_impl(c, str, false);
|
return wire_set_string_impl(c, str, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// maximum name length is 255
|
// maximum name length is 255
|
||||||
static inline char *wire_get_name_string(wire_context_t *c) {
|
static inline char *wire_get_name_string(WireCtx *c) {
|
||||||
return wire_get_string_check(c, 1, 255);
|
return wire_get_string_check(c, 1, 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue