improve naming

This commit is contained in:
hippoz 2023-01-10 13:08:49 +02:00
parent de00067fd1
commit 15a449ee44
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
3 changed files with 116 additions and 116 deletions

8
main.c
View file

@ -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);
struct jb_server *srv = jb_server_create(socket_path); bus_t *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;
@ -36,14 +36,14 @@ int main(int argc, char *argv[])
printf("Listening on %s\n", socket_path); printf("Listening on %s\n", socket_path);
while (1) { while (1) {
if (jb_server_turn(srv) < 0) { if (bus_turn(srv) < 0) {
fprintf(stderr, "server_turn failed (errno=%d, strerror=%s)\n", errno, strerror(errno)); fprintf(stderr, "server_turn failed (errno=%d, strerror=%s)\n", errno, strerror(errno));
jb_server_free(srv); bus_free(srv);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
jb_server_free(srv); bus_free(srv);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

178
server.c
View file

@ -28,14 +28,14 @@ uint64_t hashmap_hash(const char *bytes, size_t bytes_n, size_t map_len)
return (hash % map_len); return (hash % map_len);
} }
int jb_server_client_add(struct jb_server *s, int fd) int bus_client_add(bus_t *s, int fd)
{ {
for (int i = 0; i < JB_MAX_CLIENTS; i++) { for (int i = 0; i < BUS_MAX_CLIENTS; i++) {
if (s->clients[i].fd < 0) { if (s->clients[i].fd < 0) {
s->clients[i].fd = fd; s->clients[i].fd = fd;
s->clients[i].owned_name_index = -1; s->clients[i].owned_name_index = -1;
s->clients[i].unique_name_index = -1; s->clients[i].unique_name_index = -1;
s->clients[i].state = JB_CLIENT_STATE_WAIT_AUTH; s->clients[i].state = BUS_CLIENT_STATE_WAIT_AUTH;
s->fds[i].fd = fd; s->fds[i].fd = fd;
s->fds[i].events = POLLIN; s->fds[i].events = POLLIN;
s->clients_count++; s->clients_count++;
@ -45,11 +45,11 @@ int jb_server_client_add(struct jb_server *s, int fd)
return -1; return -1;
} }
int jb_server_name_find(struct jb_server *s, char *name) int bus_name_find(bus_t *s, char *name)
{ {
int bucket = hashmap_hash(name, strlen(name), JB_MAX_NAMES); int bucket = hashmap_hash(name, strlen(name), BUS_MAX_NAMES);
for (int i = bucket; i < bucket + 12 && i < JB_MAX_NAMES; i++) { for (int i = bucket; i < bucket + 12 && i < BUS_MAX_NAMES; i++) {
if (s->names[i].name && strcmp(s->names[i].name, name) == 0) { if (s->names[i].name && strcmp(s->names[i].name, name) == 0) {
return i; return i;
} }
@ -58,9 +58,9 @@ int jb_server_name_find(struct jb_server *s, char *name)
return -1; return -1;
} }
struct jb_client *jb_server_name_find_client(struct jb_server *s, char *name) bus_client_t *bus_name_find_client(bus_t *s, char *name)
{ {
int name_index = jb_server_name_find(s, name); int name_index = bus_name_find(s, name);
if (name_index < 0) { if (name_index < 0) {
return NULL; return NULL;
} }
@ -69,22 +69,22 @@ struct jb_client *jb_server_name_find_client(struct jb_server *s, char *name)
return NULL; return NULL;
} }
if (s->clients[s->names[name_index].client_index].state != JB_CLIENT_STATE_READY) { if (s->clients[s->names[name_index].client_index].state != BUS_CLIENT_STATE_READY) {
return NULL; return NULL;
} }
return &s->clients[s->names[name_index].client_index]; return &s->clients[s->names[name_index].client_index];
} }
int jb_server_name_add(struct jb_server *s, char *name, int client_index) int bus_name_add(bus_t *s, char *name, int client_index)
{ {
if (jb_server_name_find(s, name) >= 0) { if (bus_name_find(s, name) >= 0) {
return -1; return -1;
} }
int bucket = hashmap_hash(name, strlen(name), JB_MAX_NAMES); int bucket = hashmap_hash(name, strlen(name), BUS_MAX_NAMES);
for (int i = bucket; i < bucket + 12 && i < JB_MAX_NAMES; i++) { for (int i = bucket; i < bucket + 12 && i < BUS_MAX_NAMES; i++) {
if (s->names[i].client_index == -1) { if (s->names[i].client_index == -1) {
s->names[i].client_index = client_index; s->names[i].client_index = client_index;
s->names[i].name = name; s->names[i].name = name;
@ -96,11 +96,11 @@ int jb_server_name_add(struct jb_server *s, char *name, int client_index)
return -1; return -1;
} }
int jb_server_client_match_add(struct jb_server *s, int client_index, char *match) int bus_client_match_add(bus_t *s, int client_index, char *match)
{ {
struct jb_client *c = &s->clients[client_index]; bus_client_t *c = &s->clients[client_index];
for (int i = 0; i < JB_MAX_MATCH; i++) { for (int i = 0; i < BUS_MAX_MATCH; i++) {
if (!c->matches[i]) { if (!c->matches[i]) {
c->matches[i] = match_rule_from_string(match); c->matches[i] = match_rule_from_string(match);
c->match_count++; c->match_count++;
@ -111,9 +111,9 @@ int jb_server_client_match_add(struct jb_server *s, int client_index, char *matc
return -1; return -1;
} }
int jb_server_client_assign_unique_name(struct jb_server *s, int i) int bus_client_assign_unique_name(bus_t *s, int i)
{ {
struct jb_client *c = &s->clients[i]; bus_client_t *c = &s->clients[i];
if (c->unique_name_index != -1) { if (c->unique_name_index != -1) {
return -1; return -1;
} }
@ -136,7 +136,7 @@ int jb_server_client_assign_unique_name(struct jb_server *s, int i)
return -1; return -1;
} }
int name_index = jb_server_name_add(s, name, i); int name_index = bus_name_add(s, name, i);
if (name_index < 0) { if (name_index < 0) {
free(name); free(name);
return -1; return -1;
@ -146,14 +146,14 @@ int jb_server_client_assign_unique_name(struct jb_server *s, int i)
return name_index; return name_index;
} }
int jb_server_client_assign_own_name(struct jb_server *s, int i, char *name) int bus_client_assign_own_name(bus_t *s, int i, char *name)
{ {
struct jb_client *c = &s->clients[i]; bus_client_t *c = &s->clients[i];
if (c->owned_name_index != -1 || !name || *name == ':' || *name == '\0') { if (c->owned_name_index != -1 || !name || *name == ':' || *name == '\0') {
return -1; return -1;
} }
int name_index = jb_server_name_add(s, name, i); int name_index = bus_name_add(s, name, i);
if (name_index < 0) { if (name_index < 0) {
return -1; return -1;
} }
@ -162,7 +162,7 @@ int jb_server_client_assign_own_name(struct jb_server *s, int i, char *name)
return 0; return 0;
} }
void jb_server_name_remove(struct jb_server *s, int i) void bus_name_remove(bus_t *s, int i)
{ {
if (i >= 0) { if (i >= 0) {
if (s->names[i].name) { if (s->names[i].name) {
@ -174,61 +174,61 @@ void jb_server_name_remove(struct jb_server *s, int i)
} }
} }
void jb_server_client_remove(struct jb_server *s, int i) void bus_client_remove(bus_t *s, int i)
{ {
struct jb_client *c = &s->clients[i]; bus_client_t *c = &s->clients[i];
if (c->fd >= 0) { if (c->fd >= 0) {
close(c->fd); close(c->fd);
} }
for (int i = 0; i < JB_MAX_MATCH; i++) { for (int i = 0; i < BUS_MAX_MATCH; i++) {
if (c->matches[i]) { if (c->matches[i]) {
match_rule_free(c->matches[i]); match_rule_free(c->matches[i]);
c->matches[i] = NULL; c->matches[i] = NULL;
} }
} }
jb_server_name_remove(s, c->unique_name_index); bus_name_remove(s, c->unique_name_index);
jb_server_name_remove(s, c->owned_name_index); bus_name_remove(s, c->owned_name_index);
c->unique_name_index = -1; c->unique_name_index = -1;
c->owned_name_index = -1; c->owned_name_index = -1;
c->match_count = 0; c->match_count = 0;
c->fd = -1; c->fd = -1;
c->state = JB_CLIENT_STATE_NONE; c->state = BUS_CLIENT_STATE_NONE;
s->fds[i].fd = -1; s->fds[i].fd = -1;
s->fds[i].events = 0; s->fds[i].events = 0;
s->fds[i].revents = 0; s->fds[i].revents = 0;
s->clients_count--; s->clients_count--;
} }
void jb_server_client_error(struct jb_server *s, int i, const char *msg) void bus_client_error(bus_t *s, int i, const char *msg)
{ {
printf("jb_server_client_error: %s\n", msg); printf("bus_client_error: %s\n", msg);
send(s->clients[i].fd, msg, strlen(msg), 0); send(s->clients[i].fd, msg, strlen(msg), 0);
jb_server_client_remove(s, i); bus_client_remove(s, i);
} }
ssize_t jb_server_client_recv(struct jb_server *s, int i, void *buf, size_t n) ssize_t bus_client_recv(bus_t *s, int i, void *buf, size_t n)
{ {
ssize_t status = recv(s->fds[i].fd, buf, n, 0); ssize_t status = recv(s->fds[i].fd, buf, n, 0);
if (status <= 0) { if (status <= 0) {
jb_server_client_remove(s, i); bus_client_remove(s, i);
} }
return status; return status;
} }
int jb_server_broadcast_message(struct jb_server *s, wire_message_t *msg, wire_context_t *ctx, char *sender_unique_name) int bus_broadcast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *sender_unique_name)
{ {
int left = s->clients_count; int left = s->clients_count;
for (int i = 0; i < JB_MAX_CLIENTS && left > 0; i++) { for (int i = 0; i < BUS_MAX_CLIENTS && left > 0; i++) {
if (s->clients[i].match_count <= 0) { if (s->clients[i].match_count <= 0) {
continue; continue;
} }
left--; left--;
struct jb_client *c = &s->clients[i]; bus_client_t *c = &s->clients[i];
int match_left = c->match_count; int match_left = c->match_count;
for (int i = 0; i < JB_MAX_MATCH && match_left > 0; i++) { for (int i = 0; i < BUS_MAX_MATCH && match_left > 0; i++) {
if (!c->matches[i]) { if (!c->matches[i]) {
continue; continue;
} }
@ -253,9 +253,9 @@ int jb_server_broadcast_message(struct jb_server *s, wire_message_t *msg, wire_c
return 0; return 0;
} }
int jb_server_unicast_message(struct jb_server *s, wire_message_t *msg, wire_context_t *ctx, char *target_name, char *sender_unique_name) int bus_unicast_message(bus_t *s, wire_message_t *msg, wire_context_t *ctx, char *target_name, char *sender_unique_name)
{ {
struct jb_client *target = jb_server_name_find_client(s, target_name); bus_client_t *target = bus_name_find_client(s, target_name);
if (!target) { if (!target) {
return -1; return -1;
} }
@ -297,9 +297,9 @@ int jb_server_unicast_message(struct jb_server *s, wire_message_t *msg, wire_con
} \ } \
} while(0) \ } while(0) \
int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t *ctx) int bus_client_process_message(bus_t *s, int i, wire_context_t *ctx)
{ {
struct jb_client *client = &s->clients[i]; bus_client_t *client = &s->clients[i];
wire_message_t msg = {0}; wire_message_t msg = {0};
@ -344,7 +344,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
} }
if (strcmp(member, "Hello") == 0) { if (strcmp(member, "Hello") == 0) {
int unique_name_index = jb_server_client_assign_unique_name(s, i); int unique_name_index = bus_client_assign_unique_name(s, i);
if (unique_name_index < 0) { if (unique_name_index < 0) {
return -1; return -1;
} }
@ -365,7 +365,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
int status_code = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; int status_code = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
if (jb_server_client_assign_own_name(s, i, name_str) < 0) { if (bus_client_assign_own_name(s, i, name_str) < 0) {
free(name_str); free(name_str);
// TODO: report the actual error // TODO: report the actual error
status_code = DBUS_REQUEST_NAME_REPLY_EXISTS; status_code = DBUS_REQUEST_NAME_REPLY_EXISTS;
@ -385,7 +385,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
return -1; return -1;
} }
struct jb_client *target = jb_server_name_find_client(s, name); bus_client_t *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;
@ -401,7 +401,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
return -1; return -1;
} }
struct jb_client *target = jb_server_name_find_client(s, name); bus_client_t *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));
@ -418,7 +418,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
TRYPTR(wire_set_string(&reply_ctx, "org.freedesktop.DBus")); TRYPTR(wire_set_string(&reply_ctx, "org.freedesktop.DBus"));
int left = s->names_count; int left = s->names_count;
for (int i = 0; i < JB_MAX_NAMES && left > 0; i++) { for (int i = 0; i < BUS_MAX_NAMES && left > 0; i++) {
if (s->names[i].name && s->names[i].client_index >= 0) { if (s->names[i].name && s->names[i].client_index >= 0) {
left--; left--;
TRYPTR(wire_set_string(&reply_ctx, s->names[i].name)); TRYPTR(wire_set_string(&reply_ctx, s->names[i].name));
@ -461,7 +461,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
printf("client index %d adding match rule: '%s'\n", i, match); printf("client index %d adding match rule: '%s'\n", i, match);
TRYST(jb_server_client_match_add(s, i, match)); TRYST(bus_client_match_add(s, i, match));
_reply_begin("") {} _reply_end() _reply_begin("") {} _reply_end()
} else if (strcmp(member, "RemoveMatch") == 0) { } else if (strcmp(member, "RemoveMatch") == 0) {
@ -492,7 +492,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
return -1; return -1;
} }
struct jb_client *target = jb_server_name_find_client(s, name); bus_client_t *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;
@ -516,8 +516,8 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
uint32_t outer_array_start = reply_ctx.byte_cursor; uint32_t outer_array_start = reply_ctx.byte_cursor;
int left = s->names_count; int left = s->names_count;
for (int i = 0; i < JB_MAX_NAMES && left > 0; i++) { for (int i = 0; i < BUS_MAX_NAMES && left > 0; i++) {
struct jb_name *n = &s->names[i]; bus_name_t *n = &s->names[i];
if (!n->name || n->client_index < 0 || *n->name != ':') { if (!n->name || n->client_index < 0 || *n->name != ':') {
continue; continue;
} }
@ -525,7 +525,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
TRYPTR(wire_write_align(&reply_ctx, 8)); /* structs always aligned to 8 */ TRYPTR(wire_write_align(&reply_ctx, 8)); /* structs always aligned to 8 */
struct jb_client *c = &s->clients[s->names[i].client_index]; bus_client_t *c = &s->clients[s->names[i].client_index];
TRYPTR(wire_set_string(&reply_ctx, s->names[i].name)); /* unique name */ TRYPTR(wire_set_string(&reply_ctx, s->names[i].name)); /* unique name */
@ -534,7 +534,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
uint32_t *name_array_len = TRYPTR(wire_set_u32(&reply_ctx, 0)); uint32_t *name_array_len = TRYPTR(wire_set_u32(&reply_ctx, 0));
TRYPTR(wire_write_align(&reply_ctx, 4)); /* arrays start with the alignment of the type they contain */ TRYPTR(wire_write_align(&reply_ctx, 4)); /* arrays start with the alignment of the type they contain */
uint32_t name_array_start = reply_ctx.byte_cursor; uint32_t name_array_start = reply_ctx.byte_cursor;
for (int i = 0; i < JB_MAX_MATCH && match_left > 0; i++) { for (int i = 0; i < BUS_MAX_MATCH && match_left > 0; i++) {
if (!c->matches[i]) { if (!c->matches[i]) {
continue; continue;
} }
@ -580,13 +580,13 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
} }
if (destination_field->present) { if (destination_field->present) {
/* unicast */ /* unicast */
if (jb_server_unicast_message(s, &msg, ctx, destination_field->t.str, s->names[client->unique_name_index].name) < 0) { if (bus_unicast_message(s, &msg, ctx, destination_field->t.str, s->names[client->unique_name_index].name) < 0) {
_reply_error("xyz.hippoz.jitterbug.UnicastFailed"); _reply_error("xyz.hippoz.jitterbug.UnicastFailed");
return 0; return 0;
} }
} else { } else {
/* broadcast */ /* broadcast */
if (jb_server_broadcast_message(s, &msg, ctx, s->names[client->unique_name_index].name) < 0) { if (bus_broadcast_message(s, &msg, ctx, s->names[client->unique_name_index].name) < 0) {
_reply_error("xyz.hippoz.jitterbug.BroadcastFailed"); _reply_error("xyz.hippoz.jitterbug.BroadcastFailed");
return 0; return 0;
} }
@ -596,7 +596,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, wire_context_t
return 0; return 0;
} }
int jb_server_client_drain_messages(struct jb_server *s, int ci, uint8_t *data, uint32_t data_len) int bus_client_drain_messages(bus_t *s, int ci, uint8_t *data, uint32_t data_len)
{ {
wire_context_t ctx = { wire_context_t ctx = {
.byte_cursor = 0, .byte_cursor = 0,
@ -604,7 +604,7 @@ int jb_server_client_drain_messages(struct jb_server *s, int ci, uint8_t *data,
.data_len = data_len .data_len = data_len
}; };
TRYST(jb_server_client_process_message(s, ci, &ctx)); TRYST(bus_client_process_message(s, ci, &ctx));
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
if (ctx.byte_cursor >= ctx.data_len || ctx.data[ctx.byte_cursor] != 'l') { if (ctx.byte_cursor >= ctx.data_len || ctx.data[ctx.byte_cursor] != 'l') {
@ -614,26 +614,26 @@ int jb_server_client_drain_messages(struct jb_server *s, int ci, uint8_t *data,
ctx.data = ctx.data + ctx.byte_cursor; ctx.data = ctx.data + ctx.byte_cursor;
ctx.data_len = ctx.data_len - ctx.byte_cursor; ctx.data_len = ctx.data_len - ctx.byte_cursor;
ctx.byte_cursor = 0; ctx.byte_cursor = 0;
TRYST(jb_server_client_process_message(s, ci, &ctx)); TRYST(bus_client_process_message(s, ci, &ctx));
} }
return 0; return 0;
} }
void jb_server_free(struct jb_server *s) void bus_free(bus_t *s)
{ {
if (!s) return; if (!s) return;
if (s->sock_fd) close(s->sock_fd); if (s->sock_fd) close(s->sock_fd);
// freeing all clients will also free all matches and names // freeing all clients will also free all matches and names
for (int i = 0; i < JB_MAX_CLIENTS; i++) { for (int i = 0; i < BUS_MAX_CLIENTS; i++) {
jb_server_client_remove(s, i); bus_client_remove(s, i);
} }
free(s); free(s);
} }
struct jb_server *jb_server_create(const char *socket_path) bus_t *bus_create(const char *socket_path)
{ {
struct jb_server *s = malloc(sizeof(struct jb_server)); bus_t *s = malloc(sizeof(bus_t));
if (s == NULL) { if (s == NULL) {
return NULL; return NULL;
} }
@ -656,42 +656,42 @@ struct jb_server *jb_server_create(const char *socket_path)
free(s); free(s);
return NULL; return NULL;
} }
if (listen(s->sock_fd, JB_BACKLOG) == -1) { if (listen(s->sock_fd, BUS_BACKLOG) == -1) {
close(s->sock_fd); close(s->sock_fd);
free(s); free(s);
return NULL; return NULL;
} }
for (int i = 0; i < JB_MAX_CLIENTS; i++) { for (int i = 0; i < BUS_MAX_CLIENTS; i++) {
s->clients[i].fd = -1; s->clients[i].fd = -1;
s->clients[i].owned_name_index = -1; s->clients[i].owned_name_index = -1;
s->clients[i].unique_name_index = -1; s->clients[i].unique_name_index = -1;
s->clients[i].state = JB_CLIENT_STATE_NONE; s->clients[i].state = BUS_CLIENT_STATE_NONE;
s->clients[i].match_count = 0; s->clients[i].match_count = 0;
s->fds[i].fd = -1; s->fds[i].fd = -1;
s->fds[i].events = 0; s->fds[i].events = 0;
s->fds[i].revents = 0; s->fds[i].revents = 0;
for (int j = 0; j < JB_MAX_MATCH; j++) { for (int j = 0; j < BUS_MAX_MATCH; j++) {
s->clients[i].matches[j] = NULL; s->clients[i].matches[j] = NULL;
} }
} }
for (int i = 0; i < JB_MAX_NAMES; i++) { for (int i = 0; i < BUS_MAX_NAMES; i++) {
s->names[i].client_index = -1; s->names[i].client_index = -1;
s->names[i].name = NULL; s->names[i].name = NULL;
} }
s->names_count = 0; s->names_count = 0;
s->clients_count = 0; s->clients_count = 0;
s->fds[JB_MAX_CLIENTS].fd = s->sock_fd; s->fds[BUS_MAX_CLIENTS].fd = s->sock_fd;
s->fds[JB_MAX_CLIENTS].events = POLLIN; s->fds[BUS_MAX_CLIENTS].events = POLLIN;
s->fd_num = JB_MAX_CLIENTS + 1; s->fd_num = BUS_MAX_CLIENTS + 1;
return s; return s;
} }
#define _client_die(m) ({jb_server_client_error(s,i,m);continue;}) #define _client_die(m) ({bus_client_error(s,i,m);continue;})
int jb_server_turn(struct jb_server *s) int bus_turn(bus_t *s)
{ {
static const char agree_unix_fd[] = "AGREE_UNIX_FD\r\n"; static const char agree_unix_fd[] = "AGREE_UNIX_FD\r\n";
static const char auth_ok[] = "OK aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"; static const char auth_ok[] = "OK aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n";
@ -714,23 +714,23 @@ int jb_server_turn(struct jb_server *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) {
fprintf(stderr, "jb_server_turn: error: got POLLNVAL for fds[%d]. This is considered a bug in the server implementation.\n", i); fprintf(stderr, "bus_turn: 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) {
fprintf(stderr, "jb_server_turn: error: got POLLERR for sock_fd\n"); fprintf(stderr, "bus_turn: error: got POLLERR for sock_fd\n");
return -1; return -1;
} }
jb_server_client_remove(s, i); bus_client_remove(s, i);
continue; continue;
} }
if (s->fds[i].revents & POLLHUP) { if (s->fds[i].revents & POLLHUP) {
if (fd == s->sock_fd) { if (fd == s->sock_fd) {
fprintf(stderr, "jb_server_turn: error: got POLLHUP for sock_fd\n"); fprintf(stderr, "bus_turn: error: got POLLHUP for sock_fd\n");
return -1; return -1;
} }
jb_server_client_remove(s, i); bus_client_remove(s, i);
continue; continue;
} }
if (s->fds[i].revents & POLLIN) { if (s->fds[i].revents & POLLIN) {
@ -740,7 +740,7 @@ int jb_server_turn(struct jb_server *s)
// new connection // new connection
int fd = TRYST(accept(s->sock_fd, NULL, NULL)); int fd = TRYST(accept(s->sock_fd, NULL, NULL));
if (jb_server_client_add(s, fd) == -1) { if (bus_client_add(s, fd) == -1) {
close(fd); close(fd);
} }
continue; continue;
@ -753,15 +753,15 @@ int jb_server_turn(struct jb_server *s)
if (bytes <= 0) { if (bytes <= 0) {
// error during recv() OR client disconnected, disconnect the client // error during recv() OR client disconnected, disconnect the client
// TODO: should we actually do this? // TODO: should we actually do this?
jb_server_client_remove(s, i); bus_client_remove(s, i);
continue; continue;
} }
printf("\nrecv: got %zd bytes\n", bytes); printf("\nrecv: got %zd bytes\n", bytes);
struct jb_client *c = &s->clients[i]; bus_client_t *c = &s->clients[i];
switch (c->state) { switch (c->state) {
case JB_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.
// Before the flow of messages can begin, the two applications must authenticate. // Before the flow of messages can begin, the two applications must authenticate.
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol // SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol
@ -778,23 +778,23 @@ int jb_server_turn(struct jb_server *s)
send(fd, auth_list, sizeof(auth_list) - 1, 0); send(fd, auth_list, sizeof(auth_list) - 1, 0);
} else if (strcmp(auth_string, "AUTH EXTERNAL 31303030\r\n") == 0 || strcmp(auth_string, "DATA\r\n") == 0) { } else if (strcmp(auth_string, "AUTH EXTERNAL 31303030\r\n") == 0 || strcmp(auth_string, "DATA\r\n") == 0) {
send(fd, auth_ok, sizeof(auth_ok) - 1, 0); send(fd, auth_ok, sizeof(auth_ok) - 1, 0);
c->state = JB_CLIENT_STATE_WAIT_BEGIN; c->state = BUS_CLIENT_STATE_WAIT_BEGIN;
} else if (strcmp(auth_string, "AUTH EXTERNAL\r\n") == 0) { } else if (strcmp(auth_string, "AUTH EXTERNAL\r\n") == 0) {
send(fd, auth_data, sizeof(auth_data) - 1, 0); send(fd, auth_data, sizeof(auth_data) - 1, 0);
} }
} break; } break;
case JB_CLIENT_STATE_WAIT_BEGIN: { case BUS_CLIENT_STATE_WAIT_BEGIN: {
// Right now, we're expecting the client to either immediately begin the connection, // Right now, we're expecting the client to either immediately begin the connection,
// or to negotiate UNIX file descriptor passing. // or to negotiate UNIX file descriptor passing.
if (strncmp(data, "BEGIN\r\n", 7) == 0) { if (strncmp(data, "BEGIN\r\n", 7) == 0) {
c->state = JB_CLIENT_STATE_READY; c->state = BUS_CLIENT_STATE_READY;
// At this point, a D-Bus connection has been established. // At this point, a D-Bus connection has been established.
// The first octet after the \r\n of the BEGIN command is the first octet of the D-Bus communication. // The first octet after the \r\n of the BEGIN command is the first octet of the D-Bus communication.
// SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#auth-command-begin // SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#auth-command-begin
char *first_message_begin = data + 7; /* 7 = length of "BEGIN\r\n" */ char *first_message_begin = data + 7; /* 7 = length of "BEGIN\r\n" */
if (*first_message_begin == 'l' || *first_message_begin == 'B') { if (*first_message_begin == 'l' || *first_message_begin == 'B') {
// This looks like a D-Bus message. Let's process it! // This looks like a D-Bus message. Let's process it!
if (jb_server_client_drain_messages(s, i, (uint8_t *)first_message_begin, data_buffer_len - 7) < 0) { if (bus_client_drain_messages(s, i, (uint8_t *)first_message_begin, data_buffer_len - 7) < 0) {
_client_die("failed to process message"); _client_die("failed to process message");
} }
} }
@ -806,13 +806,13 @@ int jb_server_turn(struct jb_server *s)
} }
} break; } break;
case JB_CLIENT_STATE_READY: { case BUS_CLIENT_STATE_READY: {
if (jb_server_client_drain_messages(s, i, (uint8_t*)data, data_buffer_len) < 0) { if (bus_client_drain_messages(s, i, (uint8_t*)data, data_buffer_len) < 0) {
_client_die("failed to process message"); _client_die("failed to process message");
} }
} break; } break;
case JB_CLIENT_STATE_NONE: {} /* through */ case BUS_CLIENT_STATE_NONE: {} /* through */
default: { default: {
_client_die("bad state"); _client_die("bad state");
} break; } break;

View file

@ -6,48 +6,48 @@
#include <stdint.h> #include <stdint.h>
// TODO: dynamically size the arrays // TODO: dynamically size the arrays
#define JB_MAX_CLIENTS 256 #define BUS_MAX_CLIENTS 256
#define JB_MAX_NAMES 512 #define BUS_MAX_NAMES 512
#define JB_MAX_MATCH 12 #define BUS_MAX_MATCH 12
#define JB_BACKLOG 12 #define BUS_BACKLOG 12
enum { enum {
JB_CLIENT_STATE_NONE, BUS_CLIENT_STATE_NONE,
JB_CLIENT_STATE_WAIT_AUTH, BUS_CLIENT_STATE_WAIT_AUTH,
JB_CLIENT_STATE_WAIT_BEGIN, BUS_CLIENT_STATE_WAIT_BEGIN,
JB_CLIENT_STATE_READY BUS_CLIENT_STATE_READY
}; };
struct jb_client { typedef struct bus_client {
int fd; int fd;
uint8_t state; uint8_t state;
int16_t unique_name_index; int16_t unique_name_index;
int16_t owned_name_index; int16_t owned_name_index;
int8_t match_count; int8_t match_count;
match_rule_t *matches[JB_MAX_MATCH]; match_rule_t *matches[BUS_MAX_MATCH];
}; } bus_client_t;
struct jb_name { typedef struct bus_name {
char *name; char *name;
int32_t client_index; int32_t client_index;
}; } bus_name_t;
struct jb_server { typedef struct bus {
int sock_fd; int sock_fd;
int fd_num; int fd_num;
int names_count; int names_count;
int clients_count; int clients_count;
struct jb_client clients[JB_MAX_CLIENTS]; bus_client_t clients[BUS_MAX_CLIENTS];
struct jb_name names[JB_MAX_NAMES]; bus_name_t names[BUS_MAX_NAMES];
struct pollfd fds[JB_MAX_CLIENTS + 1]; struct pollfd fds[BUS_MAX_CLIENTS + 1];
}; } bus_t;
int jb_server_client_add(struct jb_server *s, int fd); int bus_client_add(bus_t *s, int fd);
void jb_server_client_remove(struct jb_server *s, int i); void bus_client_remove(bus_t *s, int i);
void jb_server_free(struct jb_server *s); void bus_free(bus_t *s);
struct jb_server *jb_server_create(const char *socket_path); bus_t *bus_create(const char *socket_path);
int jb_server_turn(struct jb_server *s); int bus_turn(bus_t *s);
#endif // _JITTERBUG__SERVER_H #endif // _JITTERBUG__SERVER_H