improve string handling
This commit is contained in:
parent
3ea788d1a7
commit
dd62b8570a
3 changed files with 41 additions and 58 deletions
9
server.c
9
server.c
|
@ -266,7 +266,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
|
||||||
|
|
||||||
printf("assigned unique name '%s' to connection %d\n", s->names[unique_name_index].name, i);
|
printf("assigned unique name '%s' to connection %d\n", s->names[unique_name_index].name, i);
|
||||||
} else if (strcmp(member, "RequestName") == 0) {
|
} else if (strcmp(member, "RequestName") == 0) {
|
||||||
char *name = TRYPTR(wire_get_string(&ctx, NULL));
|
char *name = TRYPTR(wire_get_string(&ctx));
|
||||||
int name_len = strlen(name);
|
int name_len = strlen(name);
|
||||||
if (name_len < 1 || name_len > 256) {
|
if (name_len < 1 || name_len > 256) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -293,7 +293,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
|
||||||
printf("client '%s' (index=%d) now owns name '%s'\n", s->names[client->unique_name_index].name, i, name_str);
|
printf("client '%s' (index=%d) now owns name '%s'\n", s->names[client->unique_name_index].name, i, name_str);
|
||||||
}
|
}
|
||||||
} else if (strcmp(member, "GetNameOwner") == 0) {
|
} else if (strcmp(member, "GetNameOwner") == 0) {
|
||||||
char *name = TRYPTR(wire_get_string(&ctx, NULL));
|
char *name = TRYPTR(wire_get_string(&ctx));
|
||||||
int name_len = strlen(name);
|
int name_len = strlen(name);
|
||||||
if (name_len < 1 || name_len > 256) {
|
if (name_len < 1 || name_len > 256) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -311,7 +311,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
|
||||||
TRYPTR(wire_set_string(&reply_ctx, s->names[target->unique_name_index].name));
|
TRYPTR(wire_set_string(&reply_ctx, s->names[target->unique_name_index].name));
|
||||||
} _reply_end()
|
} _reply_end()
|
||||||
} else if (strcmp(member, "NameHasOwner") == 0) {
|
} else if (strcmp(member, "NameHasOwner") == 0) {
|
||||||
char *name = TRYPTR(wire_get_string(&ctx, NULL));
|
char *name = TRYPTR(wire_get_string(&ctx));
|
||||||
int name_len = strlen(name);
|
int name_len = strlen(name);
|
||||||
if (name_len < 1 || name_len > 256) {
|
if (name_len < 1 || name_len > 256) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -351,7 +351,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
|
||||||
} else if (strcmp(member, "StartServiceByName") == 0) {
|
} else if (strcmp(member, "StartServiceByName") == 0) {
|
||||||
// TODO: stub (does nothing and always returns success)
|
// TODO: stub (does nothing and always returns success)
|
||||||
|
|
||||||
char *name = TRYPTR(wire_get_string(&ctx, NULL));
|
char *name = TRYPTR(wire_get_string(&ctx));
|
||||||
int name_len = strlen(name);
|
int name_len = strlen(name);
|
||||||
if (name_len < 1 || name_len > 256) {
|
if (name_len < 1 || name_len > 256) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -487,7 +487,6 @@ int jb_server_turn(struct jb_server *s)
|
||||||
fprintf(stderr, "jb_server_turn: error: got POLLERR for sock_fd\n");
|
fprintf(stderr, "jb_server_turn: error: got POLLERR for sock_fd\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
fprintf(stderr, "jb_server_turn: warn: got POLLERR for fds[%d], removing...\n", i);
|
|
||||||
jb_server_client_remove(s, i);
|
jb_server_client_remove(s, i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
60
wire.c
60
wire.c
|
@ -48,11 +48,13 @@ _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(wire_context_t *c, uint32_t *out_len)
|
char *wire_get_string_impl(wire_context_t *c, bool as_signature)
|
||||||
{
|
{
|
||||||
uint32_t len = *(uint32_t*)TRYPTR_NIL(wire_get_u32(c));
|
uint32_t len = 0;
|
||||||
if (out_len) {
|
if (as_signature) {
|
||||||
*out_len = len;
|
len = *(uint8_t*)TRYPTR_NIL(wire_get_u8(c));
|
||||||
|
} else {
|
||||||
|
len = *(uint32_t*)TRYPTR_NIL(wire_get_u32(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
|
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
|
||||||
|
@ -67,48 +69,14 @@ char *wire_get_string(wire_context_t *c, uint32_t *out_len)
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *wire_set_string(wire_context_t *c, const char *str)
|
char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature)
|
||||||
{
|
{
|
||||||
uint32_t len = strlen(str);
|
uint32_t len = strlen(str);
|
||||||
TRYPTR_NIL(wire_set_u32(c, len));
|
if (as_signature) {
|
||||||
|
|
||||||
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
|
|
||||||
if (new_cursor >= c->data_len) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO?
|
|
||||||
char *dest = (char*)&c->data[c->byte_cursor];
|
|
||||||
memcpy(dest, str, len + 1);
|
|
||||||
|
|
||||||
c->byte_cursor = new_cursor;
|
|
||||||
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *wire_get_signature(wire_context_t *c, uint8_t *out_len)
|
|
||||||
{
|
|
||||||
uint8_t len = *(uint8_t*)TRYPTR_NIL(wire_get_u8(c));
|
|
||||||
if (out_len) {
|
|
||||||
*out_len = len;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
|
|
||||||
if (new_cursor >= c->data_len) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *str = (char *)&c->data[c->byte_cursor];
|
|
||||||
|
|
||||||
c->byte_cursor = new_cursor;
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *wire_set_signature(wire_context_t *c, const char *str)
|
|
||||||
{
|
|
||||||
uint8_t len = strlen(str);
|
|
||||||
TRYPTR_NIL(wire_set_u8(c, len));
|
TRYPTR_NIL(wire_set_u8(c, len));
|
||||||
|
} else {
|
||||||
|
TRYPTR_NIL(wire_set_u32(c, len));
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
|
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
|
||||||
if (new_cursor >= c->data_len) {
|
if (new_cursor >= c->data_len) {
|
||||||
|
@ -146,19 +114,19 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg)
|
||||||
|
|
||||||
while (c->byte_cursor < header_fields_end) {
|
while (c->byte_cursor < header_fields_end) {
|
||||||
uint8_t field_code = *(uint8_t*)TRYPTR(wire_get_u8(c));
|
uint8_t field_code = *(uint8_t*)TRYPTR(wire_get_u8(c));
|
||||||
TRYPTR(wire_get_signature(c, NULL));
|
TRYPTR(wire_get_signature(c));
|
||||||
|
|
||||||
switch (field_code) {
|
switch (field_code) {
|
||||||
case DBUS_HEADER_FIELD_PATH: /* through */
|
case DBUS_HEADER_FIELD_PATH: /* through */
|
||||||
case DBUS_HEADER_FIELD_INTERFACE: /* through */
|
case DBUS_HEADER_FIELD_INTERFACE: /* through */
|
||||||
case DBUS_HEADER_FIELD_MEMBER: /* through */
|
case DBUS_HEADER_FIELD_MEMBER: /* through */
|
||||||
case DBUS_HEADER_FIELD_DESTINATION: {
|
case DBUS_HEADER_FIELD_DESTINATION: {
|
||||||
char *str = TRYPTR(wire_get_string(c, NULL));
|
char *str = TRYPTR(wire_get_string(c));
|
||||||
WIRE_VERBOSE("field: %s\n", str);
|
WIRE_VERBOSE("field: %s\n", str);
|
||||||
msg->fields[field_code].t.str = str;
|
msg->fields[field_code].t.str = str;
|
||||||
} break;
|
} break;
|
||||||
case DBUS_HEADER_FIELD_SIGNATURE: {
|
case DBUS_HEADER_FIELD_SIGNATURE: {
|
||||||
char *str = TRYPTR(wire_get_signature(c, NULL));
|
char *str = TRYPTR(wire_get_signature(c));
|
||||||
WIRE_VERBOSE("field: %s\n", str);
|
WIRE_VERBOSE("field: %s\n", str);
|
||||||
msg->fields[field_code].t.str = str;
|
msg->fields[field_code].t.str = str;
|
||||||
} break;
|
} break;
|
||||||
|
|
24
wire.h
24
wire.h
|
@ -122,13 +122,29 @@ _WIRE_DECL_TYPE(i32, int32_t, 4);
|
||||||
_WIRE_DECL_TYPE(u32, uint32_t, 4);
|
_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(wire_context_t *c, uint32_t *out_len);
|
|
||||||
char *wire_set_string(wire_context_t *c, const char *str);
|
char *wire_get_string_impl(wire_context_t *c, bool as_signature);
|
||||||
char *wire_get_signature(wire_context_t *c, uint8_t *out_len);
|
char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature);
|
||||||
char *wire_set_signature(wire_context_t *c, const char *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);
|
||||||
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);
|
||||||
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(wire_context_t *c, wire_context_t *msg_c, wire_message_t *msg, char *sender_unique_name);
|
||||||
|
|
||||||
|
static inline char *wire_get_signature(wire_context_t *c) {
|
||||||
|
return wire_get_string_impl(c, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char *wire_set_signature(wire_context_t *c, const char *str) {
|
||||||
|
return wire_set_string_impl(c, str, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char *wire_get_string(wire_context_t *c) {
|
||||||
|
return wire_get_string_impl(c, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char *wire_set_string(wire_context_t *c, const char *str) {
|
||||||
|
return wire_set_string_impl(c, str, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // _JITTERBUG__WIRE_H
|
#endif // _JITTERBUG__WIRE_H
|
||||||
|
|
Loading…
Reference in a new issue