diff --git a/daemon/call_interfaces.c b/daemon/call_interfaces.c index 808c2a04f..57e3188a7 100644 --- a/daemon/call_interfaces.c +++ b/daemon/call_interfaces.c @@ -171,13 +171,12 @@ static str *call_update_lookup_udp(char **out, enum call_opmode opmode, const ch struct call_subscription *dialogue[2]; GQueue q = G_QUEUE_INIT; struct stream_params sp; - str *ret, callid, viabranch, fromtag, totag = STR_NULL; + str *ret; int i; - str_init(&callid, out[RE_UDP_UL_CALLID]); - str_init(&viabranch, out[RE_UDP_UL_VIABRANCH]); - str_init(&fromtag, out[RE_UDP_UL_FROMTAG]); - str_init(&totag, out[RE_UDP_UL_TOTAG]); + str callid = STR_INIT(out[RE_UDP_UL_CALLID]); + str fromtag = STR_INIT(out[RE_UDP_UL_FROMTAG]); + str totag = STR_INIT(out[RE_UDP_UL_TOTAG]); if (opmode == OP_ANSWER) str_swap(&fromtag, &totag); @@ -320,10 +319,10 @@ static str *call_request_lookup_tcp(char **out, enum call_opmode opmode) { struct call *c; struct call_subscription *dialogue[2]; AUTO_CLEANUP(GQueue s, sdp_streams_free) = G_QUEUE_INIT; - str *ret = NULL, callid, fromtag, totag = STR_NULL; + str *ret = NULL; GHashTable *infohash; - str_init(&callid, out[RE_TCP_RL_CALLID]); + str callid = STR_INIT(out[RE_TCP_RL_CALLID]); infohash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); c = call_get_opmode(&callid, opmode); if (!c) { @@ -333,12 +332,12 @@ static str *call_request_lookup_tcp(char **out, enum call_opmode opmode) { info_parse(out[RE_TCP_RL_INFO], infohash); streams_parse(out[RE_TCP_RL_STREAMS], &s); - str_init(&fromtag, g_hash_table_lookup(infohash, "fromtag")); + str fromtag = STR_INIT(g_hash_table_lookup(infohash, "fromtag")); if (!fromtag.s) { ilog(LOG_WARNING, "No from-tag in message"); goto out2; } - str_init(&totag, g_hash_table_lookup(infohash, "totag")); + str totag = STR_INIT(g_hash_table_lookup(infohash, "totag")); if (opmode == OP_ANSWER) { if (!totag.s) { ilog(LOG_WARNING, "No to-tag in message"); @@ -373,15 +372,13 @@ str *call_lookup_tcp(char **out) { } str *call_delete_udp(char **out) { - str callid, branch, fromtag, totag; - __C_DBG("got delete for callid '%s' and viabranch '%s'", out[RE_UDP_DQ_CALLID], out[RE_UDP_DQ_VIABRANCH]); - str_init(&callid, out[RE_UDP_DQ_CALLID]); - str_init(&branch, out[RE_UDP_DQ_VIABRANCH]); - str_init(&fromtag, out[RE_UDP_DQ_FROMTAG]); - str_init(&totag, out[RE_UDP_DQ_TOTAG]); + str callid = STR_INIT(out[RE_UDP_DQ_CALLID]); + str branch = STR_INIT(out[RE_UDP_DQ_VIABRANCH]); + str fromtag = STR_INIT(out[RE_UDP_DQ_FROMTAG]); + str totag = STR_INIT(out[RE_UDP_DQ_TOTAG]); if (call_delete_branch_by_id(&callid, &branch, &fromtag, &totag, NULL, -1)) return str_sprintf("%s E8\n", out[RE_UDP_COOKIE]); @@ -390,14 +387,14 @@ str *call_delete_udp(char **out) { } str *call_query_udp(char **out) { AUTO_CLEANUP_NULL(struct call *c, call_unlock_release); - str *ret, callid, fromtag, totag; + str *ret; struct call_stats stats; __C_DBG("got query for callid '%s'", out[RE_UDP_DQ_CALLID]); - str_init(&callid, out[RE_UDP_DQ_CALLID]); - str_init(&fromtag, out[RE_UDP_DQ_FROMTAG]); - str_init(&totag, out[RE_UDP_DQ_TOTAG]); + str callid = STR_INIT(out[RE_UDP_DQ_CALLID]); + str fromtag = STR_INIT(out[RE_UDP_DQ_FROMTAG]); + str totag = STR_INIT(out[RE_UDP_DQ_TOTAG]); c = call_get_opmode(&callid, OP_OTHER); if (!c) { @@ -424,9 +421,7 @@ out: } void call_delete_tcp(char **out) { - str callid; - - str_init(&callid, out[RE_TCP_D_CALLID]); + str callid = STR_INIT(out[RE_TCP_D_CALLID]); call_delete_branch_by_id(&callid, NULL, NULL, NULL, NULL, -1); } diff --git a/daemon/codec.c b/daemon/codec.c index 04d9fe27a..d353596b8 100644 --- a/daemon/codec.c +++ b/daemon/codec.c @@ -2204,8 +2204,7 @@ static int packet_dtmf(struct codec_ssrc_handler *ch, struct codec_ssrc_handler // provide an uninitialised buffer as potential output storage for DTMF char buf[sizeof(struct telephone_event_payload)]; - str ev_pl; - str_init_len(&ev_pl, buf, sizeof(buf)); + str ev_pl = STR_INIT_LEN(buf, sizeof(buf)); int is_dtmf = dtmf_event_payload(&ev_pl, &ts, duration, &input_ch->dtmf_event, &input_ch->dtmf_events); @@ -3865,8 +3864,7 @@ void packet_encoded_packetize(AVPacket *pkt, struct codec_ssrc_handler *ch, stru char *buf = malloc(pkt_len); char *payload = buf + sizeof(struct rtp_header); // tell our packetizer how much we want - str inout; - str_init_len(&inout, payload, payload_len); + str inout = STR_INIT_LEN(payload, payload_len); // and request a packet if (in_pkt) ilogs(transcoding, LOG_DEBUG, "Adding %i bytes to packetizer", in_pkt->size); @@ -4643,8 +4641,7 @@ void codec_tracker_update(struct codec_store *cs) { ilogs(codec, LOG_DEBUG, "Adding supplemental codec " STR_FORMAT " for clock rate %u", STR_FMT(supp_codec), clockrate); char *pt_s = g_strdup_printf(STR_FORMAT "/%u", STR_FMT(supp_codec), clockrate); - str pt_str; - str_init(&pt_str, pt_s); + str pt_str = STR_INIT(pt_s); struct rtp_payload_type *pt = codec_add_payload_type(&pt_str, cs->media, NULL, NULL); if (!pt) diff --git a/daemon/janus.c b/daemon/janus.c index d41d962d9..ca6f02a81 100644 --- a/daemon/janus.c +++ b/daemon/janus.c @@ -99,8 +99,7 @@ static struct call_monologue *janus_get_monologue(uint64_t handle_id, struct cal { AUTO_CLEANUP_GBUF(handle_buf); handle_buf = g_strdup_printf("%" PRIu64, handle_id); - str handle_str; - str_init(&handle_str, handle_buf); + str handle_str = STR_INIT(handle_buf); return fn(call, &handle_str); } @@ -844,8 +843,7 @@ static const char *janus_videoroom_configure(struct websocket_message *wm, struc if (strcmp(jsep_type, "offer")) return "Not an offer"; - AUTO_CLEANUP(str sdp_in, str_free_dup) = STR_NULL; - str_init_dup(&sdp_in, jsep_sdp); + AUTO_CLEANUP(str sdp_in, str_free_dup) = STR_INIT_DUP(jsep_sdp); AUTO_CLEANUP(struct sdp_ng_flags flags, call_ng_free_flags); AUTO_CLEANUP(GQueue parsed, sdp_free) = G_QUEUE_INIT; @@ -949,8 +947,7 @@ static const char *janus_videoroom_start(struct websocket_message *wm, struct ja if (strcmp(jsep_type, "answer")) return "Not an answer"; - AUTO_CLEANUP(str sdp_in, str_free_dup) = STR_NULL; - str_init_dup(&sdp_in, jsep_sdp); + AUTO_CLEANUP(str sdp_in, str_free_dup) = STR_INIT_DUP(jsep_sdp); AUTO_CLEANUP(struct sdp_ng_flags flags, call_ng_free_flags); AUTO_CLEANUP(GQueue parsed, sdp_free) = G_QUEUE_INIT; @@ -1080,8 +1077,7 @@ static const char *janus_videoroom(struct websocket_message *wm, struct janus_se const char *req = json_reader_get_string_value(reader); if (!req) goto err; - str req_str; - str_init(&req_str, (char *) req); + str req_str = STR_INIT((char*) req); json_reader_end_member(reader); switch (__csh_lookup(&req_str)) { @@ -1786,8 +1782,7 @@ static const char *websocket_janus_process_json(struct websocket_message *wm, ilog(LOG_DEBUG, "Processing '%s' type Janus message", janus_cmd); - str janus_cmd_str; - str_init(&janus_cmd_str, (char *) janus_cmd); + str janus_cmd_str = STR_INIT((char*) janus_cmd); err = NULL; @@ -1900,8 +1895,7 @@ const char *websocket_janus_process(struct websocket_message *wm) { const char *websocket_janus_get(struct websocket_message *wm) { - str uri; - str_init(&uri, wm->uri); + str uri = STR_INIT(wm->uri); ilog(LOG_DEBUG, "Processing Janus GET: '%s'", wm->uri); @@ -1934,8 +1928,7 @@ const char *websocket_janus_get(struct websocket_message *wm) { const char *websocket_janus_post(struct websocket_message *wm) { - str uri; - str_init(&uri, wm->uri); + str uri = STR_INIT(wm->uri); ilog(LOG_DEBUG, "Processing Janus POST: '%s'", wm->uri); diff --git a/daemon/media_player.c b/daemon/media_player.c index 20fa64277..4d895a961 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -1172,8 +1172,7 @@ success:; goto err; } - str blob; - str_init_len(&blob, row[0], lengths[0]); + str blob = STR_INIT_LEN(row[0], lengths[0]); int ret = media_player_play_blob_id(mp, &blob, repeat, start_pos, id); mysql_free_result(res); diff --git a/daemon/redis.c b/daemon/redis.c index c26e570af..fc2b9cf41 100644 --- a/daemon/redis.c +++ b/daemon/redis.c @@ -2155,8 +2155,7 @@ static void restore_thread(void *call_p, void *ctx_p) { struct thread_ctx *ctx = ctx_p; redisReply *call = call_p; struct redis *r; - str callid; - str_init_len(&callid, call->str, call->len); + str callid = STR_INIT_LEN(call->str, call->len); rlog(LOG_DEBUG, "Processing call ID '%s%.*s%s' from Redis", FMT_M(REDIS_FMT(call))); diff --git a/daemon/rtcp.c b/daemon/rtcp.c index 7ea217aad..5663ad66e 100644 --- a/daemon/rtcp.c +++ b/daemon/rtcp.c @@ -556,11 +556,9 @@ static int rtcp_rr(struct rtcp_chain_element *el, struct rtcp_process_ctx *log_c } static int rtcp_sdes(struct rtcp_chain_element *el, struct rtcp_process_ctx *log_ctx) { - str comp_s; - CAH(sdes_list_start, el->u.sdes); - str_init_len(&comp_s, (void *) el->u.sdes->chunks, el->len - sizeof(el->u.sdes->header)); + str comp_s = STR_INIT_LEN((void *) el->u.sdes->chunks, el->len - sizeof(el->u.sdes->header)); int i = 0; while (1) { struct sdes_chunk *sdes_chunk = (struct sdes_chunk *) comp_s.s; @@ -618,8 +616,7 @@ static void xr_voip_metrics(struct xr_rb_voip_metrics *rb, struct rtcp_process_c static int rtcp_xr(struct rtcp_chain_element *el, struct rtcp_process_ctx *log_ctx) { CAH(common, el->u.rtcp_packet); - str comp_s; - str_init_len(&comp_s, el->u.buf + sizeof(el->u.xr->rtcp), el->len - sizeof(el->u.xr->rtcp)); + str comp_s = STR_INIT_LEN(el->u.buf + sizeof(el->u.xr->rtcp), el->len - sizeof(el->u.xr->rtcp)); while (1) { struct xr_report_block *rb = (void *) comp_s.s; if (comp_s.len < sizeof(*rb)) @@ -1600,14 +1597,14 @@ void rtcp_send_report(struct call_media *media, struct ssrc_ctx *ssrc_out) { // handle crypto - str rtcp_packet = STR_CONST_INIT_LEN(sr->str, sr->len); + str rtcp_packet = STR_INIT_LEN(sr->str, sr->len); const struct streamhandler *crypt_handler = determine_handler(&transport_protocols[PROTO_RTP_AVP], media, true); if (crypt_handler && crypt_handler->out->rtcp_crypt) { g_string_set_size(sr, sr->len + RTP_BUFFER_TAIL_ROOM); - rtcp_packet = STR_CONST_INIT_LEN(sr->str, sr->len - RTP_BUFFER_TAIL_ROOM); + rtcp_packet = STR_INIT_LEN(sr->str, sr->len - RTP_BUFFER_TAIL_ROOM); crypt_handler->out->rtcp_crypt(&rtcp_packet, ps, ssrc_out); } diff --git a/daemon/sdp.c b/daemon/sdp.c index b55113bec..602dde6b2 100644 --- a/daemon/sdp.c +++ b/daemon/sdp.c @@ -1216,8 +1216,7 @@ int sdp_parse(str *body, GQueue *sessions, const struct sdp_ng_flags *flags) { goto new_session; // allowed for trickle ICE SDP fragments } - str value_str; - str_init_len(&value_str, value, line_end - value); + str value_str = STR_INIT_LEN(value, line_end - value); switch (b[0]) { case 'v': @@ -2818,8 +2817,7 @@ const char *sdp_get_sendrecv(struct call_media *media) { static void append_attr_to_gstring(GString *s, char * name, const str * value, struct sdp_ng_flags *flags, enum media_type media_type) { - str attr; - str_init(&attr, name); + str attr = STR_INIT(name); struct sdp_manipulations *sdp_manipulations = sdp_manipulations_get_by_id(flags, media_type); /* take into account SDP arbitrary manipulations */ if (sdp_manipulate_remove(sdp_manipulations, &attr)) { @@ -2842,8 +2840,7 @@ static void append_attr_to_gstring(GString *s, char * name, const str * value, static void append_attr_int_to_gstring(GString *s, char * name, const int * value, struct sdp_ng_flags *flags, enum media_type media_type) { - str attr; - str_init(&attr, name); + str attr = STR_INIT(name); struct sdp_manipulations *sdp_manipulations = sdp_manipulations_get_by_id(flags, media_type); /* take into account SDP arbitrary manipulations */ if (sdp_manipulate_remove(sdp_manipulations, &attr)) { diff --git a/daemon/t38.c b/daemon/t38.c index 38eb7e84d..2860ccf57 100644 --- a/daemon/t38.c +++ b/daemon/t38.c @@ -108,7 +108,7 @@ static int t38_gateway_handler(t38_core_state_t *stat, void *user_data, const ui g_string_append_len(s, (void *) &seq, 2); // add primary IFP packet - str buf = STR_CONST_INIT_LEN((char *) b, len); + str buf = STR_INIT_LEN((char *) b, len); __add_udptl(s, &buf); // add error correction packets @@ -717,7 +717,7 @@ int t38_gateway_input_udptl(struct t38_gateway *tg, const str *buf) { "packet with seq %i from FEC", seq_fec); - str rec_str = STR_CONST_INIT_LEN(rec_s->str, rec_s->len); + str rec_str = STR_INIT_LEN(rec_s->str, rec_s->len); __fec_save(tg, &rec_str, seq_fec); up = __make_udptl_packet(&rec_str, seq_fec); packet_sequencer_insert(&tg->sequencer, &up->p); diff --git a/daemon/websocket.c b/daemon/websocket.c index 4b49d7bb9..0a7fab78c 100644 --- a/daemon/websocket.c +++ b/daemon/websocket.c @@ -432,8 +432,7 @@ static const char *websocket_http_cli(struct websocket_message *wm) { ilogs(http, LOG_DEBUG, "Respoding to GET /cli/%s", uri); - str uri_cmd; - str_init(&uri_cmd, uri); + str uri_cmd = STR_INIT(uri); struct cli_writer cw = { .cw_printf = websocket_queue_printf, @@ -451,8 +450,7 @@ static const char *websocket_http_cli(struct websocket_message *wm) { static const char *websocket_cli_process(struct websocket_message *wm) { ilogs(http, LOG_DEBUG, "Processing websocket CLI req '%s'", wm->body->str); - str uri_cmd; - str_init_len(&uri_cmd, wm->body->str, wm->body->len); + str uri_cmd = STR_INIT_LEN(wm->body->str, wm->body->len); struct cli_writer cw = { .cw_printf = websocket_queue_printf, diff --git a/lib/str.h b/lib/str.h index ccd0079a0..7335e6cc3 100644 --- a/lib/str.h +++ b/lib/str.h @@ -34,7 +34,9 @@ typedef struct _str str; #define STR_NULL ((str) { NULL, 0 }) #define STR_EMPTY ((str) { "", 0 }) #define STR_CONST_INIT(s) ((str) { s, sizeof(s)-1 }) -#define STR_CONST_INIT_LEN(s, len) ((str) { s, len }) +#define STR_INIT(s) ((str) { s, (s) ? strlen(s) : 0 }) +#define STR_INIT_LEN(s, len) ((str) { s, len }) +#define STR_INIT_DUP(s) ((str) { g_strdup(s), strlen(s) }) #define STR_CONST_INIT_BUF(buf) ((str) { (char *) &buf, sizeof(buf) }) diff --git a/recording-daemon/db.c b/recording-daemon/db.c index 5076dbe50..a38b1e9be 100644 --- a/recording-daemon/db.c +++ b/recording-daemon/db.c @@ -272,8 +272,7 @@ static void db_do_call_metadata(metafile_t *mf) { my_ull(&b[0], &mf->db_id); // stays persistent // XXX offload this parsing to proxy module -> bencode list/dictionary - str all_meta; - str_init(&all_meta, mf->metadata_db); + str all_meta = STR_INIT(mf->metadata_db); while (all_meta.len > 1) { str token; if (str_token_sep(&token, &all_meta, '|')) diff --git a/recording-daemon/decoder.c b/recording-daemon/decoder.c index d769f2aef..a16bfce37 100644 --- a/recording-daemon/decoder.c +++ b/recording-daemon/decoder.c @@ -26,14 +26,13 @@ int resample_audio; decode_t *decoder_new(const char *payload_str, const char *format, int ptime, output_t *outp) { - str name; char *slash = strchr(payload_str, '/'); if (!slash) { ilog(LOG_WARN, "Invalid payload format: %s", payload_str); return NULL; } - str_init_len(&name, (char *) payload_str, slash - payload_str); + str name = STR_INIT_LEN((char *) payload_str, slash - payload_str); int clockrate = atoi(slash + 1); if (clockrate <= 0) { ilog(LOG_ERR, "Invalid clock rate %i (parsed from '%.20s'/'%.20s')", @@ -83,8 +82,7 @@ decode_t *decoder_new(const char *payload_str, const char *format, int ptime, ou else out_format.format = AV_SAMPLE_FMT_S16; // needed for TLS-only scenarios - str fmtp; - str_init(&fmtp, (char *) format); + str fmtp = STR_INIT((char*) format); decoder_t *dec = decoder_new_fmtp(def, rtp_clockrate, channels, ptime, &out_format, NULL, &fmtp, NULL); if (!dec) diff --git a/recording-daemon/packet.c b/recording-daemon/packet.c index 57d2e034f..6ccecfdcf 100644 --- a/recording-daemon/packet.c +++ b/recording-daemon/packet.c @@ -366,8 +366,7 @@ void packet_process(stream_t *stream, unsigned char *buf, unsigned len) { packet->buffer = buf; // handing it over // XXX more checking here - str bufstr; - str_init_len(&bufstr, packet->buffer, len); + str bufstr = STR_INIT_LEN(packet->buffer, len); packet->ip = (void *) bufstr.s; // XXX kernel already does this - add metadata? if (packet->ip->version == 4) { diff --git a/t/test-amr-decode.c b/t/test-amr-decode.c index bd58f9770..e085737f9 100644 --- a/t/test-amr-decode.c +++ b/t/test-amr-decode.c @@ -37,8 +37,7 @@ static void do_test_amr_xx(const char *file, int line, char *codec, int clockrate) { printf("running test %s:%i\n", file, line); - str codec_name; - str_init(&codec_name, codec); + str codec_name = STR_INIT(codec); codec_def_t *def = codec_find(&codec_name, MT_AUDIO); assert(def); if (!def->support_encoding || !def->support_decoding) { diff --git a/t/test-amr-encode.c b/t/test-amr-encode.c index 75ff2a6f3..d5a24a336 100644 --- a/t/test-amr-encode.c +++ b/t/test-amr-encode.c @@ -47,8 +47,7 @@ static void do_test_amr_xx(const char *file, int line, int bitrate, char *codec, int clockrate) { printf("running test %s:%i\n", file, line); - str codec_name; - str_init(&codec_name, codec); + str codec_name = STR_INIT(codec); codec_def_t *def = codec_find(&codec_name, MT_AUDIO); assert(def); if (!def->support_encoding || !def->support_decoding) { diff --git a/t/test-bitstr.c b/t/test-bitstr.c index 7f9d77d10..d3fecc97f 100644 --- a/t/test-bitstr.c +++ b/t/test-bitstr.c @@ -39,8 +39,7 @@ int do_test(const char *input, unsigned int input_len, { char in_buf[input_len]; memcpy(in_buf, input, input_len); - str inp; - str_init_len(&inp, in_buf, input_len); + str inp = STR_INIT_LEN(in_buf, input_len); bitstr inp_bs; bitstr_init(&inp_bs, &inp); diff --git a/t/test-transcode.c b/t/test-transcode.c index a869c0f2a..0e879c453 100644 --- a/t/test-transcode.c +++ b/t/test-transcode.c @@ -17,8 +17,7 @@ GString *dtmf_logs; struct control_ng *rtpe_control_ng[2]; static str *sdup(char *s) { - str r; - str_init(&r, s); + str r = STR_INIT(s); return str_dup(&r); } static void queue_dump(GString *s, GQueue *q) { @@ -117,8 +116,7 @@ static void __start(const char *file, int line) { static void codec_set(char *c) { // from call_ng_flags_str_ht_split c = strdup(c); - str s; - str_init(&s, c); + str s = STR_INIT(c); str splitter = s; while (1) {