From 828ea40aa835233527631f5225e52cd6b52bcbe7 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 29 Jan 2013 20:52:28 +0000 Subject: [PATCH] add error flag to bencode lib for easier error handling --- modules/rtpproxy-ng/bencode.c | 28 +++++-- modules/rtpproxy-ng/bencode.h | 1 + modules/rtpproxy-ng/rtpproxy.c | 143 +++++++++++---------------------- 3 files changed, 69 insertions(+), 103 deletions(-) diff --git a/modules/rtpproxy-ng/bencode.c b/modules/rtpproxy-ng/bencode.c index dd417602b..fb6f8a9a8 100644 --- a/modules/rtpproxy-ng/bencode.c +++ b/modules/rtpproxy-ng/bencode.c @@ -85,6 +85,7 @@ int bencode_buffer_init(bencode_buffer_t *buf) { if (!buf->pieces) return -1; buf->free_list = NULL; + buf->error = 0; return 0; } @@ -92,14 +93,19 @@ static void *__bencode_alloc(bencode_buffer_t *buf, unsigned int size) { struct __bencode_buffer_piece *piece; void *ret; + if (buf->error) + return NULL; + piece = buf->pieces; if (size <= piece->left) goto alloc; piece = __bencode_piece_new(size); - if (!piece) + if (!piece) { + buf->error = 1; return NULL; + } piece->next = buf->pieces; buf->pieces = piece; @@ -157,6 +163,11 @@ bencode_item_t *bencode_list(bencode_buffer_t *buf) { } static void __bencode_container_add(bencode_item_t *parent, bencode_item_t *child) { + if (!parent) + return; + if (!child) + return; + assert(child->parent == NULL); assert(child->sibling == NULL); @@ -221,9 +232,9 @@ bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i) { bencode_item_t *bencode_dictionary_add_len(bencode_item_t *dict, const char *key, int keylen, bencode_item_t *val) { bencode_item_t *str; - assert(dict->type == BENCODE_DICTIONARY); - if (!val) + if (!dict || !val) return NULL; + assert(dict->type == BENCODE_DICTIONARY); str = bencode_string_len(dict->buffer, key, keylen); if (!str) @@ -234,6 +245,8 @@ bencode_item_t *bencode_dictionary_add_len(bencode_item_t *dict, const char *key } bencode_item_t *bencode_list_add(bencode_item_t *list, bencode_item_t *item) { + if (!list || !item) + return NULL; assert(list->type == BENCODE_LIST); __bencode_container_add(list, item); return item; @@ -309,7 +322,8 @@ static int __bencode_str_dump(char *out, bencode_item_t *item) { struct iovec *bencode_iovec(bencode_item_t *root, int *cnt, unsigned int head, unsigned int tail) { struct iovec *ret; - assert(root != NULL); + if (!root) + return NULL; assert(cnt != NULL); assert(root->iov_cnt > 0); @@ -324,7 +338,8 @@ char *bencode_collapse(bencode_item_t *root, int *len) { char *ret; int l; - assert(root != NULL); + if (!root) + return NULL; assert(root->str_len > 0); ret = __bencode_alloc(root->buffer, root->str_len + 1); @@ -340,7 +355,8 @@ char *bencode_collapse_dup(bencode_item_t *root, int *len) { char *ret; int l; - assert(root != NULL); + if (!root) + return NULL; assert(root->str_len > 0); ret = BENCODE_MALLOC(root->str_len + 1); diff --git a/modules/rtpproxy-ng/bencode.h b/modules/rtpproxy-ng/bencode.h index d31167133..3c6ba750a 100644 --- a/modules/rtpproxy-ng/bencode.h +++ b/modules/rtpproxy-ng/bencode.h @@ -54,6 +54,7 @@ struct bencode_item { struct bencode_buffer { struct __bencode_buffer_piece *pieces; struct __bencode_free_list *free_list; + int error:1; }; diff --git a/modules/rtpproxy-ng/rtpproxy.c b/modules/rtpproxy-ng/rtpproxy.c index cbd62feaa..bd6de5615 100644 --- a/modules/rtpproxy-ng/rtpproxy.c +++ b/modules/rtpproxy-ng/rtpproxy.c @@ -1082,9 +1082,8 @@ rtpp_test(struct rtpp_node *node, int isdisabled, int force) return 1; } dict = bencode_dictionary(&bencbuf); - if (!dict) - goto benc_error; - if (!bencode_dictionary_add_string(dict, "command", "ping")) + bencode_dictionary_add_string(dict, "command", "ping"); + if (bencbuf.error) goto benc_error; cp = send_rtpp_command(node, dict, &ret); @@ -1398,75 +1397,50 @@ static inline int parse_rtpproxy_flags(const char *str1, struct rtpproxy_flags * case 'a': case 'A': - if (flags->flags) { - if (!bencode_list_add_string(flags->flags, "asymmetric")) - goto benc_error; - if (!bencode_list_add_string(flags->flags, "trust-address")) - goto benc_error; - } + bencode_list_add_string(flags->flags, "asymmetric"); + bencode_list_add_string(flags->flags, "trust-address"); break; case 'i': case 'I': - if (flags->direction) { - if (!bencode_list_add_string(flags->direction, "internal")) - goto benc_error; - } + bencode_list_add_string(flags->direction, "internal"); break; case 'e': case 'E': - if (flags->direction) { - if (!bencode_list_add_string(flags->direction, "external")) - goto benc_error; - } + bencode_list_add_string(flags->direction, "external"); break; case 'l': case 'L': if (offer == 0) - goto error; + return -1; flags->flookup = 1; break; case 'r': case 'R': - if (flags->flags) { - if (!bencode_list_add_string(flags->flags, "trust-address")) - goto benc_error; - } + bencode_list_add_string(flags->flags, "trust-address"); break; case 'o': case 'O': - if (flags->replace) { - if (!bencode_list_add_string(flags->replace, "origin")) - goto benc_error; - } + bencode_list_add_string(flags->replace, "origin"); break; case 'c': case 'C': - if (flags->replace) { - if (!bencode_list_add_string(flags->replace, "session-connection")) - goto benc_error; - } + bencode_list_add_string(flags->replace, "session-connection"); break; case 'f': case 'F': - if (flags->flags) { - if (!bencode_list_add_string(flags->flags, "force")) - goto benc_error; - } + bencode_list_add_string(flags->flags, "force"); break; case 'w': case 'W': - if (flags->flags) { - if (!bencode_list_add_string(flags->flags, "symmetric")) - goto benc_error; - } + bencode_list_add_string(flags->flags, "symmetric"); break; #if 0 @@ -1488,30 +1462,19 @@ static inline int parse_rtpproxy_flags(const char *str1, struct rtpproxy_flags * default: LM_ERR("unknown option `%c'\n", *cp); - goto error; + return -1; } } /* only add those if any flags were given at all */ - if (flags->direction && flags->direction->child) { - if (!bencode_dictionary_add(dict, "direction", flags->direction)) - goto benc_error; - } - if (flags->flags && flags->flags->child) { - if (!bencode_dictionary_add(dict, "flags", flags->flags)) - goto benc_error; - } - if (flags->replace && flags->replace->child) { - if (!bencode_dictionary_add(dict, "replace", flags->replace)) - goto benc_error; - } + if (flags->direction && flags->direction->child) + bencode_dictionary_add(dict, "direction", flags->direction); + if (flags->flags && flags->flags->child) + bencode_dictionary_add(dict, "flags", flags->flags); + if (flags->replace && flags->replace->child) + bencode_dictionary_add(dict, "replace", flags->replace); return 0; - -benc_error: - LM_ERR("out of memory - bencode failed\n"); -error: - return -1; } @@ -1532,8 +1495,6 @@ unforce_rtp_proxy1_f(struct sip_msg* msg, char* str1, char* str2) goto error_nb; } dict = bencode_dictionary(&bencbuf); - if (!dict) - goto benc_error; memset(&flags, 0, sizeof(flags)); @@ -1544,8 +1505,7 @@ unforce_rtp_proxy1_f(struct sip_msg* msg, char* str1, char* str2) LM_ERR("can't get Call-Id field\n"); goto error; } - if (!bencode_dictionary_add_str(dict, "call-id", &callid)) - goto benc_error; + bencode_dictionary_add_str(dict, "call-id", &callid); to_tag.s = 0; if (get_to_tag(msg, &to_tag) == -1) { LM_ERR("can't get To tag\n"); @@ -1564,18 +1524,18 @@ unforce_rtp_proxy1_f(struct sip_msg* msg, char* str1, char* str2) LM_ERR("can't get Via branch\n"); goto error; } - if (!bencode_dictionary_add_str(dict, "via-branch", &viabranch)) - goto benc_error; + bencode_dictionary_add_str(dict, "via-branch", &viabranch); } - if (!bencode_dictionary_add_str(dict, "from-tag", &from_tag)) - goto benc_error; - if (to_tag.s && to_tag.len && !bencode_dictionary_add_str(dict, "to-tag", &to_tag)) - goto benc_error; + bencode_dictionary_add_str(dict, "from-tag", &from_tag); + if (to_tag.s && to_tag.len) + bencode_dictionary_add_str(dict, "to-tag", &to_tag); if(msg->id != current_msg_id) selected_rtpp_set = default_rtpp_set; - if (!bencode_dictionary_add_string(dict, "command", "delete")) + bencode_dictionary_add_string(dict, "command", "delete"); + + if (bencbuf.error) goto benc_error; node = select_rtpp_node(callid, 1); @@ -1777,17 +1737,12 @@ force_rtp_proxy(struct sip_msg* msg, char* str1, char* str2, int offer, int forc goto error_nb; } dict = bencode_dictionary(&bencbuf); - if (!dict) - goto benc_error; memset(&flags, 0, sizeof(flags)); - if (!(flags.flags = bencode_list(&bencbuf))) - goto benc_error; - if (!(flags.direction = bencode_list(&bencbuf))) - goto benc_error; - if (!(flags.replace = bencode_list(&bencbuf))) - goto benc_error; + flags.flags = bencode_list(&bencbuf); + flags.direction = bencode_list(&bencbuf); + flags.replace = bencode_list(&bencbuf); if (parse_rtpproxy_flags(str1, &flags, dict, offer)) goto error; @@ -1802,29 +1757,23 @@ force_rtp_proxy(struct sip_msg* msg, char* str1, char* str2, int offer, int forc goto error; } - if (!(item = bencode_list(&bencbuf))) - goto benc_error; + item = bencode_list(&bencbuf); - if (!bencode_dictionary_add(dict, "received-from", item)) - goto benc_error; - if (!bencode_list_add_string(item, (msg->rcv.src_ip.af == AF_INET) ? "IP4" : ( + bencode_dictionary_add(dict, "received-from", item); + bencode_list_add_string(item, (msg->rcv.src_ip.af == AF_INET) ? "IP4" : ( #ifdef USE_IPV6 - (msg->rcv.src_ip.af == AF_INET6) ? "IP6" : + (msg->rcv.src_ip.af == AF_INET6) ? "IP6" : #endif - "?" - ) )) - goto benc_error; - if (!bencode_list_add_string(item, ip_addr2a(&msg->rcv.src_ip))) - goto benc_error; + "?" + ) ); + bencode_list_add_string(item, ip_addr2a(&msg->rcv.src_ip)); - if (!bencode_dictionary_add_str(dict, "sdp", &body)) - goto benc_error; + bencode_dictionary_add_str(dict, "sdp", &body); if (get_callid(msg, &callid) == -1 || callid.len == 0) { LM_ERR("can't get Call-Id field\n"); goto error; } - if (!bencode_dictionary_add_str(dict, "call-id", &callid)) - goto benc_error; + bencode_dictionary_add_str(dict, "call-id", &callid); to_tag.s = 0; if (get_to_tag(msg, &to_tag) == -1) { LM_ERR("can't get To tag\n"); @@ -1843,8 +1792,7 @@ force_rtp_proxy(struct sip_msg* msg, char* str1, char* str2, int offer, int forc LM_ERR("can't get Via branch\n"); goto error; } - if (!bencode_dictionary_add_str(dict, "via-branch", &viabranch)) - goto benc_error; + bencode_dictionary_add_str(dict, "via-branch", &viabranch); } if (flags.flookup != 0) { if (to_tag.len == 0) { @@ -1860,15 +1808,16 @@ force_rtp_proxy(struct sip_msg* msg, char* str1, char* str2, int offer, int forc from_tag = to_tag; to_tag = tmp; } - if (!bencode_dictionary_add_str(dict, "from-tag", &from_tag)) - goto benc_error; - if (to_tag.s && to_tag.len && !bencode_dictionary_add_str(dict, "to-tag", &to_tag)) - goto benc_error; + bencode_dictionary_add_str(dict, "from-tag", &from_tag); + if (to_tag.s && to_tag.len) + bencode_dictionary_add_str(dict, "to-tag", &to_tag); if(msg->id != current_msg_id) selected_rtpp_set = default_rtpp_set; - if (!bencode_dictionary_add_string(dict, "command", (create == 0) ? "answer" : "offer")) + bencode_dictionary_add_string(dict, "command", (create == 0) ? "answer" : "offer"); + + if (bencbuf.error) goto benc_error; do {