Compare commits

..

3 commits

Author SHA1 Message Date
hippoz
ce164d8d5d
add convenient functions in wire for getting strings with a limited length 2023-01-10 15:17:50 +02:00
hippoz
095eb8f403
greatly overhaul bus code 2023-01-10 14:57:16 +02:00
hippoz
15a449ee44
improve naming 2023-01-10 13:08:49 +02:00
6 changed files with 681 additions and 653 deletions

View file

@ -1,5 +1,5 @@
CC?=gcc CC?=gcc
CFLAGS+=-Wall -Wextra -std=c99 -ggdb -fsanitize=address CFLAGS+=-Wall -Wextra -Wshadow -std=c99 -ggdb -fsanitize=address
jitterbug: util.c wire.c match.c server.c main.c jitterbug: util.c wire.c match.c server.c main.c
$(CC) $(CFLAGS) -o $@ $^ $(CC) $(CFLAGS) -o $@ $^

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;
} }

1259
server.c

File diff suppressed because it is too large Load diff

View file

@ -2,52 +2,58 @@
#define _JITTERBUG__SERVER_H #define _JITTERBUG__SERVER_H
#include "match.h" #include "match.h"
#include "wire.h"
#include <poll.h> #include <poll.h>
#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;
typedef struct bus_method_handler {
const char *name;
int (*handler)(bus_t *bus, int client_index, wire_message_t *msg, wire_context_t *ctx, wire_context_t *reply_ctx);
} bus_method_handler_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

7
wire.c
View file

@ -100,6 +100,13 @@ 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 *str = TRYPTR_NIL(wire_get_string_impl(c, false));
int length = strlen(str);
if (length < min_length || length > max_length) return NULL;
return str;
}
int wire_parse_message(wire_context_t *c, wire_message_t *msg) int wire_parse_message(wire_context_t *c, wire_message_t *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

6
wire.h
View file

@ -123,6 +123,7 @@ _WIRE_DECL_TYPE(boolean, uint32_t, 4);
char *wire_get_string_impl(wire_context_t *c, bool as_signature); char *wire_get_string_impl(wire_context_t *c, bool as_signature);
char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature); char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature);
char *wire_get_string_check(wire_context_t *c, int min_length, int max_length);
int wire_parse_message(wire_context_t *c, wire_message_t *msg); int wire_parse_message(wire_context_t *c, wire_message_t *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(wire_context_t *c, wire_message_t *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(wire_context_t *c, wire_message_t *msg, const char *error_name);
@ -145,5 +146,10 @@ static inline char *wire_set_string(wire_context_t *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
static inline char *wire_get_name_string(wire_context_t *c) {
return wire_get_string_check(c, 1, 255);
}
#endif // _JITTERBUG__WIRE_H #endif // _JITTERBUG__WIRE_H