diff --git a/daemon/bencode.h b/daemon/bencode.h index 1a1ff771f..296ff41f8 100644 --- a/daemon/bencode.h +++ b/daemon/bencode.h @@ -106,8 +106,10 @@ static inline bencode_item_t *bencode_dictionary_add(bencode_item_t *dict, const /* Identical to bencode_dictionary_add() but doesn't require the key string to be null-terminated */ bencode_item_t *bencode_dictionary_add_len(bencode_item_t *dict, const char *key, int keylen, bencode_item_t *val); -/* Convenience function to add a string value to a dictionary */ +/* Convenience function to add a string value to a dictionary, possibly duplicated into the + * bencode_buffer_t object. */ static inline bencode_item_t *bencode_dictionary_add_string(bencode_item_t *dict, const char *key, const char *val); +static inline bencode_item_t *bencode_dictionary_add_string_dup(bencode_item_t *dict, const char *key, const char *val); /* Ditto, but for a "str" object */ static inline bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, const char *key, const str *val); @@ -117,8 +119,10 @@ static inline bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, c static inline bencode_item_t *bencode_dictionary_add_iovec(bencode_item_t *dict, const char *key, const struct iovec *iov, int iov_cnt, int str_len); -/* Convenience function to add an integer value to a dictionary */ +/* Convenience functions to add the respective (newly created) objects to a dictionary */ static inline bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dict, const char *key, long long int val); +static inline bencode_item_t *bencode_dictionary_add_dictionary(bencode_item_t *dict, const char *key); +static inline bencode_item_t *bencode_dictionary_add_list(bencode_item_t *dict, const char *key); @@ -130,8 +134,10 @@ static inline bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dic * The item to be added must not have been previously linked into any other dictionary or list. */ bencode_item_t *bencode_list_add(bencode_item_t *list, bencode_item_t *item); -/* Convenience function to add a string item to a list */ +/* Convenience function to add the respective (newly created) objects to a list */ static inline bencode_item_t *bencode_list_add_string(bencode_item_t *list, const char *s); +static inline bencode_item_t *bencode_list_add_list(bencode_item_t *list); +static inline bencode_item_t *bencode_list_add_dictionary(bencode_item_t *list); @@ -361,6 +367,12 @@ static inline bencode_item_t *bencode_dictionary_add_string(bencode_item_t *dict return bencode_dictionary_add(dict, key, bencode_string(dict->buffer, val)); } +static inline bencode_item_t *bencode_dictionary_add_string_dup(bencode_item_t *dict, const char *key, const char *val) { + if (!val) + return NULL; + return bencode_dictionary_add(dict, key, bencode_string_dup(dict->buffer, val)); +} + static inline bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, const char *key, const str *val) { if (!val) return NULL; @@ -371,10 +383,26 @@ static inline bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dic return bencode_dictionary_add(dict, key, bencode_integer(dict->buffer, val)); } +static inline bencode_item_t *bencode_dictionary_add_dictionary(bencode_item_t *dict, const char *key) { + return bencode_dictionary_add(dict, key, bencode_dictionary(dict->buffer)); +} + +static inline bencode_item_t *bencode_dictionary_add_list(bencode_item_t *dict, const char *key) { + return bencode_dictionary_add(dict, key, bencode_list(dict->buffer)); +} + static inline bencode_item_t *bencode_list_add_string(bencode_item_t *list, const char *s) { return bencode_list_add(list, bencode_string(list->buffer, s)); } +static inline bencode_item_t *bencode_list_add_list(bencode_item_t *list) { + return bencode_list_add(list, bencode_list(list->buffer)); +} + +static inline bencode_item_t *bencode_list_add_dictionary(bencode_item_t *list) { + return bencode_list_add(list, bencode_dictionary(list->buffer)); +} + static inline bencode_item_t *bencode_dictionary_get(bencode_item_t *dict, const char *key) { if (!key) return NULL; diff --git a/daemon/call.c b/daemon/call.c index 617210b62..387160683 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -2328,7 +2328,7 @@ static bencode_item_t *peer_address(bencode_buffer_t *b, struct stream *s) { bencode_dictionary_add_string(d, "family", "IPv6"); inet_ntop(AF_INET6, &s->ip46, buf, sizeof(buf)); } - bencode_dictionary_add(d, "address", bencode_string_dup(b, buf)); + bencode_dictionary_add_string_dup(d, "address", buf); bencode_dictionary_add_integer(d, "port", s->port); return d; @@ -2339,7 +2339,7 @@ static bencode_item_t *streamrelay_stats(bencode_buffer_t *b, struct streamrelay d = bencode_dictionary(b); - s = bencode_dictionary_add(d, "stats", bencode_dictionary(b)); + s = bencode_dictionary_add_dictionary(d, "stats"); bencode_dictionary_add_integer(s, "packets", r->stats.packets); bencode_dictionary_add_integer(s, "bytes", r->stats.bytes); bencode_dictionary_add_integer(s, "errors", r->stats.errors); @@ -2369,7 +2369,7 @@ static bencode_item_t *peer_stats(bencode_buffer_t *b, struct peer *p) { else bencode_dictionary_add_string(d, "status", "unknown peer address"); - s = bencode_dictionary_add(d, "stats", bencode_dictionary(b)); + s = bencode_dictionary_add_dictionary(d, "stats"); bencode_dictionary_add(s, "rtp", streamrelay_stats(b, &p->rtps[0])); bencode_dictionary_add(s, "rtcp", streamrelay_stats(b, &p->rtps[1])); @@ -2396,7 +2396,7 @@ const char *call_query_ng(bencode_item_t *input, struct callmaster *m, bencode_i bencode_dictionary_add_string(output, "result", "ok"); bencode_dictionary_add_integer(output, "created", call->created); - streams = bencode_dictionary_add(output, "streams", bencode_list(output->buffer)); + streams = bencode_dictionary_add_list(output, "streams"); for (l = call->callstreams->head; l; l = l->next) { cs = l->data; @@ -2418,7 +2418,7 @@ const char *call_query_ng(bencode_item_t *input, struct callmaster *m, bencode_i continue; tag_match: - stream = bencode_list_add(streams, bencode_list(output->buffer)); + stream = bencode_list_add_list(streams); bencode_list_add(stream, peer_stats(output->buffer, p)); bencode_list_add(stream, peer_stats(output->buffer, px));