Compare commits
No commits in common. "ce164d8d5da23f9dfeba9ac20e9211a3e9d81aa2" and "de00067fd1907aab7e00a9582f60a360f75bd261" have entirely different histories.
ce164d8d5d
...
de00067fd1
6 changed files with 441 additions and 469 deletions
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
||||||
CC?=gcc
|
CC?=gcc
|
||||||
CFLAGS+=-Wall -Wextra -Wshadow -std=c99 -ggdb -fsanitize=address
|
CFLAGS+=-Wall -Wextra -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
8
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);
|
struct jb_server *srv = jb_server_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 (bus_turn(srv) < 0) {
|
if (jb_server_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));
|
||||||
bus_free(srv);
|
jb_server_free(srv);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bus_free(srv);
|
jb_server_free(srv);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
52
server.h
52
server.h
|
@ -2,58 +2,52 @@
|
||||||
#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 BUS_MAX_CLIENTS 256
|
#define JB_MAX_CLIENTS 256
|
||||||
#define BUS_MAX_NAMES 512
|
#define JB_MAX_NAMES 512
|
||||||
#define BUS_MAX_MATCH 12
|
#define JB_MAX_MATCH 12
|
||||||
#define BUS_BACKLOG 12
|
#define JB_BACKLOG 12
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
BUS_CLIENT_STATE_NONE,
|
JB_CLIENT_STATE_NONE,
|
||||||
BUS_CLIENT_STATE_WAIT_AUTH,
|
JB_CLIENT_STATE_WAIT_AUTH,
|
||||||
BUS_CLIENT_STATE_WAIT_BEGIN,
|
JB_CLIENT_STATE_WAIT_BEGIN,
|
||||||
BUS_CLIENT_STATE_READY
|
JB_CLIENT_STATE_READY
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct bus_client {
|
struct jb_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[BUS_MAX_MATCH];
|
match_rule_t *matches[JB_MAX_MATCH];
|
||||||
} bus_client_t;
|
};
|
||||||
|
|
||||||
typedef struct bus_name {
|
struct jb_name {
|
||||||
char *name;
|
char *name;
|
||||||
int32_t client_index;
|
int32_t client_index;
|
||||||
} bus_name_t;
|
};
|
||||||
|
|
||||||
typedef struct bus {
|
struct jb_server {
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
int fd_num;
|
int fd_num;
|
||||||
int names_count;
|
int names_count;
|
||||||
int clients_count;
|
int clients_count;
|
||||||
bus_client_t clients[BUS_MAX_CLIENTS];
|
struct jb_client clients[JB_MAX_CLIENTS];
|
||||||
bus_name_t names[BUS_MAX_NAMES];
|
struct jb_name names[JB_MAX_NAMES];
|
||||||
struct pollfd fds[BUS_MAX_CLIENTS + 1];
|
struct pollfd fds[JB_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 bus_client_add(bus_t *s, int fd);
|
int jb_server_client_add(struct jb_server *s, int fd);
|
||||||
void bus_client_remove(bus_t *s, int i);
|
void jb_server_client_remove(struct jb_server *s, int i);
|
||||||
void bus_free(bus_t *s);
|
void jb_server_free(struct jb_server *s);
|
||||||
bus_t *bus_create(const char *socket_path);
|
struct jb_server *jb_server_create(const char *socket_path);
|
||||||
int bus_turn(bus_t *s);
|
int jb_server_turn(struct jb_server *s);
|
||||||
|
|
||||||
|
|
||||||
#endif // _JITTERBUG__SERVER_H
|
#endif // _JITTERBUG__SERVER_H
|
7
wire.c
7
wire.c
|
@ -100,13 +100,6 @@ 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
6
wire.h
|
@ -123,7 +123,6 @@ _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);
|
||||||
|
@ -146,10 +145,5 @@ 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
|
||||||
|
|
Loading…
Reference in a new issue