From dc19d892cd842dcfaeb589b9f710e5733c3f77a9 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 26 Jul 2024 13:28:22 -0400 Subject: [PATCH] MT#55283 add abstract .dict_add_string() Change-Id: I33eb89241eaa8b29e2ea128bb5c9d34bc25be0e9 --- daemon/call.c | 10 ++--- daemon/call_interfaces.c | 79 +++++++++++++++++++-------------------- daemon/control_ng.c | 12 +++--- include/call.h | 4 +- include/call_interfaces.h | 2 +- include/control_ng.h | 1 + 6 files changed, 55 insertions(+), 53 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index 66e41aa40..afc8ba103 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -4756,7 +4756,7 @@ static void monologue_stop(struct call_monologue *ml, bool stop_media_subsribers // call must be locked in W. // unlocks the call and releases the reference prior to returning, even on error. int call_delete_branch(call_t *c, const str *branch, - const str *fromtag, const str *totag, bencode_item_t *output, int delete_delay) + const str *fromtag, const str *totag, ng_parser_ctx_t *ctx, int delete_delay) { struct call_monologue *ml; int ret; @@ -4820,8 +4820,8 @@ int call_delete_branch(call_t *c, const str *branch, do_delete: c->destroyed = rtpe_now; - if (output) - ng_call_stats(c, fromtag, totag, output, NULL); + if (ctx) + ng_call_stats(ctx, c, fromtag, totag, NULL); /* stop media player and all medias of ml. * same for media subscribers */ @@ -4884,12 +4884,12 @@ out: int call_delete_branch_by_id(const str *callid, const str *branch, - const str *fromtag, const str *totag, bencode_item_t *output, int delete_delay) + const str *fromtag, const str *totag, ng_parser_ctx_t *ctx, int delete_delay) { call_t *c = call_get(callid); if (!c) { ilog(LOG_INFO, "Call-ID to delete not found"); return -1; } - return call_delete_branch(c, branch, fromtag, totag, output, delete_delay); + return call_delete_branch(c, branch, fromtag, totag, ctx, delete_delay); } diff --git a/daemon/call_interfaces.c b/daemon/call_interfaces.c index f613c9e85..367d91911 100644 --- a/daemon/call_interfaces.c +++ b/daemon/call_interfaces.c @@ -422,7 +422,7 @@ str *call_query_udp(char **out) { goto err; } - ng_call_stats(c, &fromtag, &totag, NULL, &stats); + ng_call_stats(NULL, c, &fromtag, &totag, &stats); rwlock_unlock_w(&c->master_lock); @@ -485,14 +485,14 @@ static void call_release_ref(void *p) { call_t *c = p; obj_put(c); } -INLINE void call_bencode_hold_ref(call_t *c, bencode_item_t *bi) { +INLINE void call_bencode_hold_ref(call_t *c, bencode_buffer_t *buf) { /* We cannot guarantee that the "call" structures are still around at the time * when the bencode reply is finally read and sent out. Since we use scatter/gather * to avoid duplication of strings and stuff, we reserve a reference to the call * structs and have it released when the bencode buffer is destroyed. This is * necessary every time the bencode response may reference strings contained * within the call structs. */ - bencode_buffer_destroy_add(bi->buffer, call_release_ref, obj_get(c)); + bencode_buffer_destroy_add(buf, call_release_ref, obj_get(c)); } INLINE void str_hyphenate(str *s_ori) { @@ -2196,7 +2196,7 @@ static const char *call_offer_answer_ng(ng_parser_ctx_t *ctx, enum call_opmode o /* At least the random ICE strings are contained within the call struct, so we * need to hold a ref until we're done sending the reply */ - call_bencode_hold_ref(call, output); + call_bencode_hold_ref(call, &ctx->ngbuf->buffer); errstr = "Invalid dialogue association"; if (call_get_mono_dialogue(monologues, call, &flags.from_tag, &flags.to_tag, @@ -2324,7 +2324,7 @@ const char *call_delete_ng(ng_parser_ctx_t *ctx) { if (discard) recording_discard(c); - if (call_delete_branch(c, &viabranch, &fromtag, &totag, output, delete_delay)) + if (call_delete_branch(c, &viabranch, &fromtag, &totag, ctx, delete_delay)) goto err; return NULL; @@ -2332,7 +2332,7 @@ const char *call_delete_ng(ng_parser_ctx_t *ctx) { err: if (fatal) return "Call-ID not found or tags didn't match"; - bencode_dictionary_add_string(output, "warning", "Call-ID not found or tags didn't match"); + ctx->parser->dict_add_string(output, "warning", "Call-ID not found or tags didn't match"); return NULL; } @@ -2347,10 +2347,10 @@ static void ng_stats(bencode_item_t *d, const struct stream_stats *s, struct str atomic64_add_na(&totals->errors, atomic64_get(&s->errors)); } -static void ng_stats_endpoint(bencode_item_t *dict, const endpoint_t *ep) { +static void ng_stats_endpoint(const ng_parser_t *parser, bencode_item_t *dict, const endpoint_t *ep) { if (!ep->address.family) return; - bencode_dictionary_add_string(dict, "family", ep->address.family->name); + parser->dict_add_string(dict, "family", ep->address.family->name); bencode_dictionary_add_str_dup(dict, "address", &STR(sockaddr_print_buf(&ep->address))); bencode_dictionary_add_integer(dict, "port", ep->port); } @@ -2377,7 +2377,7 @@ static void ng_stats_stream_ssrc(bencode_item_t *dict, struct ssrc_ctx *const ss #define BF_PS(k, f) if (PS_ISSET(ps, f)) bencode_list_add_string(flags, k) -static void ng_stats_stream(bencode_item_t *list, const struct packet_stream *ps, +static void ng_stats_stream(const ng_parser_t *parser, bencode_item_t *list, const struct packet_stream *ps, struct call_stats *totals) { bencode_item_t *dict = NULL, *flags; @@ -2392,13 +2392,13 @@ static void ng_stats_stream(bencode_item_t *list, const struct packet_stream *ps bencode_dictionary_add_integer(dict, "local port", ps->selected_sfd->socket.local.port); bencode_dictionary_add_str_dup(dict, "local address", &STR(sockaddr_print_buf(&ps->selected_sfd->socket.local.address))); - bencode_dictionary_add_string(dict, "family", ps->selected_sfd->socket.local.address.family->name); + parser->dict_add_string(dict, "family", ps->selected_sfd->socket.local.address.family->name); } - ng_stats_endpoint(bencode_dictionary_add_dictionary(dict, "endpoint"), &ps->endpoint); - ng_stats_endpoint(bencode_dictionary_add_dictionary(dict, "advertised endpoint"), + ng_stats_endpoint(parser, bencode_dictionary_add_dictionary(dict, "endpoint"), &ps->endpoint); + ng_stats_endpoint(parser, bencode_dictionary_add_dictionary(dict, "advertised endpoint"), &ps->advertised_endpoint); if (ps->crypto.params.crypto_suite) - bencode_dictionary_add_string(dict, "crypto suite", + parser->dict_add_string(dict, "crypto suite", ps->crypto.params.crypto_suite->name); bencode_dictionary_add_integer(dict, "last packet", packet_stream_last_packet(ps)); bencode_dictionary_add_integer(dict, "last kernel packet", atomic64_get_na(&ps->stats_in->last_packet)); @@ -2435,7 +2435,7 @@ stats: #define BF_M(k, f) if (MEDIA_ISSET(m, f)) bencode_list_add_string(flags, k) -static void ng_stats_media(bencode_item_t *list, const struct call_media *m, +static void ng_stats_media(const ng_parser_t *parser, bencode_item_t *list, const struct call_media *m, struct call_stats *totals) { bencode_item_t *dict, *streams = NULL, *flags; @@ -2452,7 +2452,7 @@ static void ng_stats_media(bencode_item_t *list, const struct call_media *m, bencode_dictionary_add_integer(dict, "index", m->index); bencode_dictionary_add_str(dict, "type", &m->type); if (m->protocol) - bencode_dictionary_add_string(dict, "protocol", m->protocol->name); + parser->dict_add_string(dict, "protocol", m->protocol->name); if (rtp_pt) bencode_dictionary_add_str_dup(dict, "codec", &rtp_pt->encoding_with_params); @@ -2493,11 +2493,11 @@ static void ng_stats_media(bencode_item_t *list, const struct call_media *m, stats: for (auto_iter(l, m->streams.head); l; l = l->next) { ps = l->data; - ng_stats_stream(streams, ps, totals); + ng_stats_stream(parser, streams, ps, totals); } } -static void ng_stats_monologue(bencode_item_t *dict, const struct call_monologue *ml, +static void ng_stats_monologue(const ng_parser_t *parser, bencode_item_t *dict, const struct call_monologue *ml, struct call_stats *totals, bencode_item_t *ssrc) { bencode_item_t *sub, *medias = NULL; @@ -2542,7 +2542,7 @@ static void ng_stats_monologue(bencode_item_t *dict, const struct call_monologue if (!g_queue_find(&mls_subscriptions, ms->monologue)) { bencode_item_t *sub1 = bencode_list_add_dictionary(b_subscriptions); bencode_dictionary_add_str(sub1, "tag", &ms->monologue->tag); - bencode_dictionary_add_string(sub1, "type", ms->attrs.offer_answer ? "offer/answer" : "pub/sub"); + parser->dict_add_string(sub1, "type", ms->attrs.offer_answer ? "offer/answer" : "pub/sub"); g_queue_push_tail(&mls_subscriptions, ms->monologue); } } @@ -2554,7 +2554,7 @@ static void ng_stats_monologue(bencode_item_t *dict, const struct call_monologue if (!g_queue_find(&mls_subscribers, ms->monologue)) { bencode_item_t *sub1 = bencode_list_add_dictionary(b_subscribers); bencode_dictionary_add_str(sub1, "tag", &ms->monologue->tag); - bencode_dictionary_add_string(sub1, "type", ms->attrs.offer_answer ? "offer/answer" : "pub/sub"); + parser->dict_add_string(sub1, "type", ms->attrs.offer_answer ? "offer/answer" : "pub/sub"); g_queue_push_tail(&mls_subscribers, ms->monologue); } } @@ -2572,7 +2572,7 @@ static void ng_stats_monologue(bencode_item_t *dict, const struct call_monologue bencode_item_t *vsc = bencode_list_add_dictionary(list); const char *type = dtmf_trigger_types[state->type]; if (type) - bencode_dictionary_add_string(vsc, "type", type); + parser->dict_add_string(vsc, "type", type); bencode_dictionary_add_str(vsc, "trigger", &state->trigger); bencode_dictionary_add_integer(vsc, "active", !state->inactive); } @@ -2588,7 +2588,7 @@ stats: m = ml->medias->pdata[i]; if (!m) continue; - ng_stats_media(medias, m, totals); + ng_stats_media(parser, medias, m, totals); } } @@ -2665,7 +2665,7 @@ static void ng_stats_ssrc(bencode_item_t *dict, struct ssrc_hash *ht) { } /* call must be locked */ -void ng_call_stats(call_t *call, const str *fromtag, const str *totag, bencode_item_t *output, +void ng_call_stats(ng_parser_ctx_t *ctx, call_t *call, const str *fromtag, const str *totag, struct call_stats *totals) { bencode_item_t *tags = NULL, *dict; @@ -2678,18 +2678,18 @@ void ng_call_stats(call_t *call, const str *fromtag, const str *totag, bencode_i totals = &t_b; ZERO(*totals); - if (!output) + if (!ctx) goto stats; - call_bencode_hold_ref(call, output); + call_bencode_hold_ref(call, &ctx->ngbuf->buffer); - bencode_dictionary_add_integer(output, "created", call->created.tv_sec); - bencode_dictionary_add_integer(output, "created_us", call->created.tv_usec); - bencode_dictionary_add_integer(output, "last signal", call->last_signal); - bencode_dictionary_add_integer(output, "last redis update", atomic64_get_na(&call->last_redis_update)); + bencode_dictionary_add_integer(ctx->resp, "created", call->created.tv_sec); + bencode_dictionary_add_integer(ctx->resp, "created_us", call->created.tv_usec); + bencode_dictionary_add_integer(ctx->resp, "last signal", call->last_signal); + bencode_dictionary_add_integer(ctx->resp, "last redis update", atomic64_get_na(&call->last_redis_update)); - ssrc = bencode_dictionary_add_dictionary(output, "SSRC"); - tags = bencode_dictionary_add_dictionary(output, "tags"); + ssrc = bencode_dictionary_add_dictionary(ctx->resp, "SSRC"); + tags = bencode_dictionary_add_dictionary(ctx->resp, "tags"); stats: match_tag = (totag && totag->s && totag->len) ? totag : fromtag; @@ -2697,13 +2697,13 @@ stats: if (!match_tag || !match_tag->len) { for (__auto_type l = call->monologues.head; l; l = l->next) { ml = l->data; - ng_stats_monologue(tags, ml, totals, ssrc); + ng_stats_monologue(ctx->parser, tags, ml, totals, ssrc); } } else { ml = call_get_monologue(call, match_tag); if (ml) { - ng_stats_monologue(tags, ml, totals, ssrc); + ng_stats_monologue(ctx->parser, tags, ml, totals, ssrc); g_auto(GQueue) mls = G_QUEUE_INIT; /* to avoid duplications */ for (int i = 0; i < ml->medias->len; i++) { @@ -2717,7 +2717,7 @@ stats: { struct media_subscription * ms = subscription->data; if (!g_queue_find(&mls, ms->monologue)) { - ng_stats_monologue(tags, ms->monologue, totals, ssrc); + ng_stats_monologue(ctx->parser, tags, ms->monologue, totals, ssrc); g_queue_push_tail(&mls, ms->monologue); } } @@ -2725,15 +2725,15 @@ stats: } } - if (!output) + if (!ctx) return; - dict = bencode_dictionary_add_dictionary(output, "totals"); + dict = bencode_dictionary_add_dictionary(ctx->resp, "totals"); ng_stats(bencode_dictionary_add_dictionary(dict, "RTP"), &totals->totals[0], NULL); ng_stats(bencode_dictionary_add_dictionary(dict, "RTCP"), &totals->totals[1], NULL); if (call->recording) { - bencode_item_t *rec = bencode_dictionary_add_dictionary(output, "recording"); + bencode_item_t *rec = bencode_dictionary_add_dictionary(ctx->resp, "recording"); bencode_dictionary_add_integer(rec, "call recording", !!CALL_ISSET(call, RECORDING_ON)); bencode_dictionary_add_integer(rec, "forwarding", !!CALL_ISSET(call, REC_FORWARDING)); } @@ -2759,7 +2759,6 @@ const char *call_query_ng(ng_parser_ctx_t *ctx) { str callid, fromtag, totag; call_t *call; bencode_item_t *input = ctx->req; - bencode_item_t *output = ctx->resp; if (!ctx->parser->dict_get_str(input, "call-id", &callid)) return "No call-id in message"; @@ -2769,7 +2768,7 @@ const char *call_query_ng(ng_parser_ctx_t *ctx) { ctx->parser->dict_get_str(input, "from-tag", &fromtag); ctx->parser->dict_get_str(input, "to-tag", &totag); - ng_call_stats(call, &fromtag, &totag, output, NULL); + ng_call_stats(ctx, call, &fromtag, &totag, NULL); rwlock_unlock_w(&call->master_lock); obj_put(call); @@ -3772,7 +3771,7 @@ const char *call_subscribe_request_ng(ng_parser_ctx_t *ctx) { bencode_dictionary_add_integer(med_ent, "index", media->index); bencode_dictionary_add_str(med_ent, "type", &media->type); bencode_dictionary_add_str(med_ent, "label", &media->label); - bencode_dictionary_add_string(med_ent, "mode", sdp_get_sendrecv(media)); + ctx->parser->dict_add_string(med_ent, "mode", sdp_get_sendrecv(media)); if (media_labels) { bencode_item_t *label = @@ -3782,7 +3781,7 @@ const char *call_subscribe_request_ng(ng_parser_ctx_t *ctx) { bencode_dictionary_add_str(label, "type", &media->type); if (source_ml->label.len) bencode_dictionary_add_str(label, "label", &source_ml->label); - bencode_dictionary_add_string(label, "mode", sdp_get_sendrecv(media)); + ctx->parser->dict_add_string(label, "mode", sdp_get_sendrecv(media)); } } } diff --git a/daemon/control_ng.c b/daemon/control_ng.c index 436a3a7eb..06cc08330 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -142,6 +142,7 @@ const ng_parser_t ng_parser_native = { .get_int = bencode_get_int, .dict = __bencode_dict, .dict_get_str = bencode_dictionary_get_str, + .dict_add_string = bencode_dictionary_add_string, }; const ng_parser_t ng_parser_json = { .collapse = bencode_collapse_str_json, @@ -154,6 +155,7 @@ const ng_parser_t ng_parser_json = { .get_int = bencode_get_int, .dict = __bencode_dict, .dict_get_str = bencode_dictionary_get_str, + .dict_add_string = bencode_dictionary_add_string, }; @@ -489,7 +491,7 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons if (errstr) goto err_send; - bencode_dictionary_add_string(parser_ctx.resp, "result", resultstr); + parser_ctx.parser->dict_add_string(parser_ctx.resp, "result", resultstr); // update interval statistics RTPE_STATS_INC(ng_commands[command]); @@ -502,14 +504,14 @@ err_send: if (errstr < magic_load_limit_strings[0] || errstr > magic_load_limit_strings[__LOAD_LIMIT_MAX-1]) { ilogs(control, LOG_WARNING, "Protocol error in packet from %s: %s [" STR_FORMAT_M "]", addr, errstr, STR_FMT_M(data)); - bencode_dictionary_add_string(parser_ctx.resp, "result", "error"); - bencode_dictionary_add_string(parser_ctx.resp, "error-reason", errstr); + parser_ctx.parser->dict_add_string(parser_ctx.resp, "result", "error"); + parser_ctx.parser->dict_add_string(parser_ctx.resp, "error-reason", errstr); g_atomic_int_inc(&cur->errors); cmd = STR_NULL; } else { - bencode_dictionary_add_string(parser_ctx.resp, "result", "load limit"); - bencode_dictionary_add_string(parser_ctx.resp, "message", errstr); + parser_ctx.parser->dict_add_string(parser_ctx.resp, "result", "load limit"); + parser_ctx.parser->dict_add_string(parser_ctx.resp, "message", errstr); } send_resp: diff --git a/include/call.h b/include/call.h index f28939e1c..cff90de58 100644 --- a/include/call.h +++ b/include/call.h @@ -810,9 +810,9 @@ int monologue_subscribe_answer(struct call_monologue *dst, sdp_ng_flags *flags, int monologue_unsubscribe(struct call_monologue *dst, sdp_ng_flags *); void monologue_destroy(struct call_monologue *ml); int call_delete_branch_by_id(const str *callid, const str *branch, - const str *fromtag, const str *totag, bencode_item_t *output, int delete_delay); + const str *fromtag, const str *totag, ng_parser_ctx_t *, int delete_delay); int call_delete_branch(call_t *, const str *branch, - const str *fromtag, const str *totag, bencode_item_t *output, int delete_delay); + const str *fromtag, const str *totag, ng_parser_ctx_t *, int delete_delay); void call_destroy(call_t *); struct call_media *call_media_new(call_t *call); void call_media_free(struct call_media **mdp); diff --git a/include/call_interfaces.h b/include/call_interfaces.h index 8212a7ffe..7a63aaf69 100644 --- a/include/call_interfaces.h +++ b/include/call_interfaces.h @@ -263,7 +263,7 @@ const char *call_unsilence_media_ng(ng_parser_ctx_t *); const char *call_play_media_ng(ng_parser_ctx_t *); const char *call_stop_media_ng(ng_parser_ctx_t *); const char *call_play_dtmf_ng(ng_parser_ctx_t *); -void ng_call_stats(call_t *call, const str *fromtag, const str *totag, bencode_item_t *output, +void ng_call_stats(ng_parser_ctx_t *, call_t *call, const str *fromtag, const str *totag, struct call_stats *totals); const char *call_publish_ng(ng_parser_ctx_t *, const char *, const endpoint_t *); diff --git a/include/control_ng.h b/include/control_ng.h index 27edc57b4..d5f6de964 100644 --- a/include/control_ng.h +++ b/include/control_ng.h @@ -123,6 +123,7 @@ struct ng_parser { long long (*get_int)(bencode_item_t *); bencode_item_t *(*dict)(ng_parser_ctx_t *); char *(*dict_get_str)(bencode_item_t *, const char *, str *); + void (*dict_add_string)(bencode_item_t *, const char *, const char *); }; struct ng_parser_ctx { const ng_parser_t *parser;