diff --git a/daemon/call_interfaces.c b/daemon/call_interfaces.c index a8738cfab..5d51ddcaa 100644 --- a/daemon/call_interfaces.c +++ b/daemon/call_interfaces.c @@ -69,13 +69,13 @@ static str *str_dup_escape(const str *s); static void call_set_dtmf_block(call_t *call, struct call_monologue *monologue, sdp_ng_flags *flags); -static str *streams_print(medias_arr *s, int start, int end, const char *prefix, enum stream_address_format format) { +static str streams_print(medias_arr *s, int start, int end, const char *prefix, enum stream_address_format format) { GString *o; int i, af, port; struct call_media *media; struct packet_stream *ps; - o = g_string_new_str(); + o = g_string_new(""); if (prefix) g_string_append_printf(o, "%s ", prefix); @@ -168,14 +168,14 @@ static void updated_created_from(call_t *c, const char *addr, const endpoint_t * } } -static str *call_update_lookup_udp(char **out, enum ng_opmode opmode, const char* addr, +static str call_update_lookup_udp(char **out, enum ng_opmode opmode, const char* addr, const endpoint_t *sin) { call_t *c; struct call_monologue *monologues[2]; /* subscriber lists of both monologues */ sdp_streams_q q = TYPED_GQUEUE_INIT; struct stream_params sp; - str *ret; + str ret; int i; g_auto(sdp_ng_flags) flags; @@ -226,7 +226,7 @@ static str *call_update_lookup_udp(char **out, enum ng_opmode opmode, const char gettimeofday(&(from_ml->started), NULL); - ilog(LOG_INFO, "Returning to SIP proxy: "STR_FORMAT"", STR_FMT(ret)); + ilog(LOG_INFO, "Returning to SIP proxy: " STR_FORMAT, STR_FMT(&ret)); goto out; ml_fail: @@ -246,10 +246,10 @@ out: return ret; } -str *call_update_udp(char **out, const char* addr, const endpoint_t *sin) { +str call_update_udp(char **out, const char* addr, const endpoint_t *sin) { return call_update_lookup_udp(out, OP_OFFER, addr, sin); } -str *call_lookup_udp(char **out) { +str call_lookup_udp(char **out) { return call_update_lookup_udp(out, OP_ANSWER, NULL, NULL); } @@ -325,11 +325,11 @@ INLINE void call_unlock_release_update(call_t **c) { -static str *call_request_lookup_tcp(char **out, enum ng_opmode opmode) { +static str call_request_lookup_tcp(char **out, enum ng_opmode opmode) { call_t *c; struct call_monologue *monologues[2]; g_auto(sdp_streams_q) s = TYPED_GQUEUE_INIT; - str *ret = NULL; + str ret = STR_NULL; GHashTable *infohash; g_auto(sdp_ng_flags) flags; @@ -370,21 +370,21 @@ static str *call_request_lookup_tcp(char **out, enum ng_opmode opmode) { out2: call_unlock_release_update(&c); - ilog(LOG_INFO, "Returning to SIP proxy: "STR_FORMAT"", STR_FMT0(ret)); + ilog(LOG_INFO, "Returning to SIP proxy: " STR_FORMAT, STR_FMT(&ret)); out: g_hash_table_destroy(infohash); return ret; } -str *call_request_tcp(char **out) { +str call_request_tcp(char **out) { return call_request_lookup_tcp(out, OP_OFFER); } -str *call_lookup_tcp(char **out) { +str call_lookup_tcp(char **out) { return call_request_lookup_tcp(out, OP_ANSWER); } -str *call_delete_udp(char **out) { +str call_delete_udp(char **out) { __C_DBG("got delete for callid '%s' and viabranch '%s'", out[RE_UDP_DQ_CALLID], out[RE_UDP_DQ_VIABRANCH]); @@ -398,9 +398,9 @@ str *call_delete_udp(char **out) { return str_sprintf("%s 0\n", out[RE_UDP_COOKIE]); } -str *call_query_udp(char **out) { +str call_query_udp(char **out) { g_autoptr(call_t) c = NULL; - str *ret; + str ret; struct call_stats stats; __C_DBG("got query for callid '%s'", out[RE_UDP_DQ_CALLID]); diff --git a/daemon/control_ng.c b/daemon/control_ng.c index 2dd1ddaa6..65f48ef01 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -940,12 +940,12 @@ int control_ng_process(str *buf, const endpoint_t *sin, char *addr, const sockad .local_ep = p1 ? &(((socket_t*)p1)->local) : NULL, .cookie = cookie, .command = cached->command, - .callid = *cached->callid, + .callid = cached->callid, .should_trace = should_trace_msg(cached->command)}; CH(homer_trace_msg_in, &hctx, &data); - cb(&cookie, cached->reply, sin, local, p1); - CH(homer_trace_msg_out, &hctx, cached->reply); + cb(&cookie, &cached->reply, sin, local, p1); + CH(homer_trace_msg_out, &hctx, &cached->reply); cache_entry_free(cached); return 0; @@ -963,7 +963,7 @@ int control_ng_process(str *buf, const endpoint_t *sin, char *addr, const sockad &reply, &data, sin, addr, ref, &ngbuf); cb(&cookie, &reply, sin, local, p1); - cache_entry ce = {.reply = &reply, .command = hctx.command, .callid = &hctx.callid}; + cache_entry ce = {.reply = reply, .command = hctx.command, .callid = hctx.callid}; cookie_cache_insert(&ng_cookie_cache, &cookie, &ce); return 0; diff --git a/daemon/control_tcp.c b/daemon/control_tcp.c index 1ec956c06..e1bf96504 100644 --- a/daemon/control_tcp.c +++ b/daemon/control_tcp.c @@ -60,7 +60,7 @@ static int control_stream_parse(struct streambuf_stream *s, char *line) { int ret; char **out; struct control_tcp *c = (void *) s->parent; - str *output = NULL; + str output = STR_NULL; pcre2_match_data *md = pcre2_match_data_create(20, NULL); ret = pcre2_match(c->parse_re, (PCRE2_SPTR8) line, PCRE2_ZERO_TERMINATED, @@ -97,9 +97,9 @@ static int control_stream_parse(struct streambuf_stream *s, char *line) { else if (!strcmp(out[RE_TCP_DIV_CMD], "quit") || !strcmp(out[RE_TCP_DIV_CMD], "exit")) {} - if (output) { - streambuf_write_str(s->outbuf, output); - free(output); + if (output.len) { + streambuf_write_str(s->outbuf, &output); + free(output.s); } pcre2_substring_list_free((SUBSTRING_FREE_ARG) out); diff --git a/daemon/control_udp.c b/daemon/control_udp.c index 6a44b4b61..289fd632b 100644 --- a/daemon/control_udp.c +++ b/daemon/control_udp.c @@ -26,7 +26,7 @@ static void control_udp_incoming(struct obj *obj, struct udp_buffer *udp_buf) { char **out; struct iovec iov[10]; unsigned int iovlen; - str cookie, *reply = NULL; + str cookie, reply = STR_NULL; cache_entry *ce; pcre2_match_data *md = pcre2_match_data_create(30, NULL); @@ -77,7 +77,7 @@ static void control_udp_incoming(struct obj *obj, struct udp_buffer *udp_buf) { if (ce) { reply = ce->reply; ilogs(control, LOG_INFO, "Detected command from udp:%s as a duplicate", udp_buf->addr); - socket_sendto_from(udp_buf->listener, reply->s, reply->len, &udp_buf->sin, &udp_buf->local_addr); + socket_sendto_from(udp_buf->listener, reply.s, reply.len, &udp_buf->sin, &udp_buf->local_addr); cache_entry_free(ce); goto out; } @@ -123,13 +123,13 @@ static void control_udp_incoming(struct obj *obj, struct udp_buffer *udp_buf) { socket_sendiov(udp_buf->listener, iov, iovlen, &udp_buf->sin, &udp_buf->local_addr); } - if (reply) { - socket_sendto_from(udp_buf->listener, reply->s, reply->len, &udp_buf->sin, &udp_buf->local_addr); + if (reply.len) { + socket_sendto_from(udp_buf->listener, reply.s, reply.len, &udp_buf->sin, &udp_buf->local_addr); str callid = STR_NULL; - cache_entry new_ce = {.reply = reply, .callid = &callid}; + cache_entry new_ce = {.reply = reply, .callid = callid}; cookie_cache_insert(&u->cookie_cache, &cookie, &new_ce); - free(reply); + g_free(reply.s); } else cookie_cache_remove(&u->cookie_cache, &cookie); diff --git a/include/call_interfaces.h b/include/call_interfaces.h index 14aa06b1d..2e019a2fa 100644 --- a/include/call_interfaces.h +++ b/include/call_interfaces.h @@ -274,15 +274,15 @@ extern bool dtls_passive_def; extern str_case_value_ht rtpe_signalling_templates; extern str rtpe_default_signalling_templates[OP_COUNT + 1]; -str *call_request_tcp(char **); -str *call_lookup_tcp(char **); +str call_request_tcp(char **); +str call_lookup_tcp(char **); void call_delete_tcp(char **); void calls_status_tcp(struct streambuf_stream *); -str *call_update_udp(char **, const char*, const endpoint_t *); -str *call_lookup_udp(char **); -str *call_delete_udp(char **); -str *call_query_udp(char **); +str call_update_udp(char **, const char*, const endpoint_t *); +str call_lookup_udp(char **); +str call_delete_udp(char **); +str call_query_udp(char **); const char *call_offer_ng(ng_command_ctx_t *, const char*, const endpoint_t *); diff --git a/include/cookie_cache.h b/include/cookie_cache.h index d31a8ae7a..76954d0ec 100644 --- a/include/cookie_cache.h +++ b/include/cookie_cache.h @@ -13,8 +13,8 @@ struct cookie_cache_state { }; typedef struct cache_entry { - str *reply; - str *callid; + str reply; + str callid; int command; } cache_entry; @@ -23,17 +23,17 @@ INLINE cache_entry *cache_entry_dup(const cache_entry *s) { return NULL; cache_entry *r; r = malloc(sizeof(*r)); - r->reply = str_dup(s->reply); + r->reply = str_dup_str(&s->reply); r->command = s->command; - r->callid = str_dup(s->callid); + r->callid = str_dup_str(&s->callid); return r; } INLINE void cache_entry_free(void *p) { cache_entry *s = p; if (!s) return; - free(s->reply); - free(s->callid); + free(s->reply.s); + free(s->callid.s); free(s); } struct cookie_cache { diff --git a/lib/str.c b/lib/str.c index 152f5c79d..353d6400c 100644 --- a/lib/str.c +++ b/lib/str.c @@ -40,11 +40,11 @@ gboolean str_case_equal(const str *a, const str *b) { return str_casecmp_str((str *) a, (str *) b) == 0; } -str *__str_sprintf(const char *fmt, ...) { - str *ret; +str str_sprintf(const char *fmt, ...) { + str ret; va_list ap; va_start(ap, fmt); - ret = __str_vsprintf(fmt, ap); + ret = str_vsprintf(fmt, ap); va_end(ap); return ret; } diff --git a/lib/str.h b/lib/str.h index b0990600c..1d9eac41c 100644 --- a/lib/str.h +++ b/lib/str.h @@ -161,13 +161,11 @@ ACCESS(read_only, 3) INLINE char *str_ncpy(char *dst, size_t bufsize, const str *src); /* asprintf() analogs */ -#define str_sprintf(fmt, ...) __str_sprintf(STR_MALLOC_PADDING fmt, ##__VA_ARGS__) -#define str_vsprintf(fmt, a) __str_vsprintf(STR_MALLOC_PADDING fmt, a) +str str_sprintf(const char *fmt, ...) __attribute__((format(printf,1,2))); +INLINE str str_vsprintf(const char *fmt, va_list ap); -/* creates a new empty GString that has mem allocated for a new str object */ -INLINE GString *g_string_new_str(void); /* frees the GString object and returns the new str object */ -INLINE str *g_string_free_str(GString *gs); +INLINE str g_string_free_str(GString *gs); /* for GHashTables */ guint str_hash(const str *s); @@ -351,46 +349,18 @@ INLINE void str_slice_free(str *p) { g_free(p); } -#define STR_MALLOC_PADDING "xxxxxxxxxxxxxxxx" -INLINE str *__str_vsprintf(const char *fmt, va_list ap) { +INLINE str str_vsprintf(const char *fmt, va_list ap) { char *r; int l; - size_t pl; - str *ret; - l = vasprintf(&r, fmt, ap); if (l < 0) abort(); - pl = strlen(STR_MALLOC_PADDING); - assert(pl >= sizeof(*ret)); - ret = (void *) r; - ret->s = r + pl; - ret->len = l - pl; - return ret; + return STR_LEN(r, l); } -str *__str_sprintf(const char *fmt, ...) __attribute__((format(printf,1,2))); -INLINE GString *g_string_new_str(void) { - size_t pl; - GString *ret; - - ret = g_string_new(""); - pl = strlen(STR_MALLOC_PADDING); - assert(pl >= sizeof(str)); - g_string_append_len(ret, STR_MALLOC_PADDING, pl); - return ret; -} -INLINE str *g_string_free_str(GString *gs) { - str *ret; - size_t pl; - - pl = strlen(STR_MALLOC_PADDING); - assert(gs->len >= pl); - assert(memcmp(gs->str, STR_MALLOC_PADDING, pl) == 0); - ret = (void *) gs->str; - ret->len = gs->len - pl; - ret->s = g_string_free(gs, FALSE) + pl; - return ret; +INLINE str g_string_free_str(GString *gs) { + size_t len = gs->len; + return STR_LEN(g_string_free(gs, FALSE), len); } INLINE int str_memcmp(const str *s, const void *m) { return memcmp(s->s, m, s->len);