From 37af6dfbe6756d6a3991fc757cd3e72c9e76f750 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 17 May 2023 16:37:11 +0200 Subject: [PATCH] rtpengine: clang-format for coherent indentation and coding style --- src/modules/rtpengine/bencode.c | 392 ++-- src/modules/rtpengine/bencode.h | 342 +-- src/modules/rtpengine/config.c | 51 +- src/modules/rtpengine/config.h | 23 +- src/modules/rtpengine/rtpengine.c | 2794 ++++++++++++----------- src/modules/rtpengine/rtpengine.h | 110 +- src/modules/rtpengine/rtpengine_db.c | 65 +- src/modules/rtpengine/rtpengine_funcs.c | 332 ++- src/modules/rtpengine/rtpengine_funcs.h | 4 +- src/modules/rtpengine/rtpengine_hash.c | 233 +- src/modules/rtpengine/rtpengine_hash.h | 33 +- 11 files changed, 2346 insertions(+), 2033 deletions(-) diff --git a/src/modules/rtpengine/bencode.c b/src/modules/rtpengine/bencode.c index cca17326242..ee1c1753caa 100644 --- a/src/modules/rtpengine/bencode.c +++ b/src/modules/rtpengine/bencode.c @@ -8,51 +8,51 @@ /* set to 0 for alloc debugging, e.g. through valgrind */ -#define BENCODE_MIN_BUFFER_PIECE_LEN 512 +#define BENCODE_MIN_BUFFER_PIECE_LEN 512 -#define BENCODE_HASH_BUCKETS 31 /* prime numbers work best */ +#define BENCODE_HASH_BUCKETS 31 /* prime numbers work best */ #define BENCODE_ALLOC_ALIGN 8 -struct __bencode_buffer_piece { +struct __bencode_buffer_piece +{ char *tail; unsigned int left; struct __bencode_buffer_piece *next; char buf[0]; }; -struct __bencode_free_list { +struct __bencode_free_list +{ void *ptr; free_func_t func; struct __bencode_free_list *next; }; -struct __bencode_hash { +struct __bencode_hash +{ struct bencode_item *buckets[BENCODE_HASH_BUCKETS]; }; - - - static bencode_item_t __bencode_end_marker = { - .type = BENCODE_END_MARKER, - .iov[0].iov_base = "e", - .iov[0].iov_len = 1, - .iov_cnt = 1, - .str_len = 1, + .type = BENCODE_END_MARKER, + .iov[0].iov_base = "e", + .iov[0].iov_len = 1, + .iov_cnt = 1, + .str_len = 1, }; +static bencode_item_t *__bencode_decode( + bencode_buffer_t *buf, const char *s, const char *end); -static bencode_item_t *__bencode_decode(bencode_buffer_t *buf, const char *s, const char *end); - - - -static void __bencode_item_init(bencode_item_t *item) { +static void __bencode_item_init(bencode_item_t *item) +{ item->last_child = item->parent = item->child = item->sibling = NULL; } -static void __bencode_container_init(bencode_item_t *cont) { +static void __bencode_container_init(bencode_item_t *cont) +{ cont->iov[0].iov_len = 1; cont->iov[1].iov_base = "e"; cont->iov[1].iov_len = 1; @@ -60,26 +60,29 @@ static void __bencode_container_init(bencode_item_t *cont) { cont->str_len = 2; } -static void __bencode_dictionary_init(bencode_item_t *dict) { +static void __bencode_dictionary_init(bencode_item_t *dict) +{ dict->type = BENCODE_DICTIONARY; dict->iov[0].iov_base = "d"; dict->value = 0; __bencode_container_init(dict); } -static void __bencode_list_init(bencode_item_t *list) { +static void __bencode_list_init(bencode_item_t *list) +{ list->type = BENCODE_LIST; list->iov[0].iov_base = "l"; __bencode_container_init(list); } -static struct __bencode_buffer_piece *__bencode_piece_new(unsigned int size) { +static struct __bencode_buffer_piece *__bencode_piece_new(unsigned int size) +{ struct __bencode_buffer_piece *ret; - if (size < BENCODE_MIN_BUFFER_PIECE_LEN) + if(size < BENCODE_MIN_BUFFER_PIECE_LEN) size = BENCODE_MIN_BUFFER_PIECE_LEN; ret = BENCODE_MALLOC(sizeof(*ret) + size + BENCODE_ALLOC_ALIGN); - if (!ret) + if(!ret) return NULL; ret->tail = ret->buf; @@ -89,32 +92,36 @@ static struct __bencode_buffer_piece *__bencode_piece_new(unsigned int size) { return ret; } -int bencode_buffer_init(bencode_buffer_t *buf) { +int bencode_buffer_init(bencode_buffer_t *buf) +{ buf->pieces = __bencode_piece_new(0); - if (!buf->pieces) + if(!buf->pieces) return -1; buf->free_list = NULL; buf->error = 0; return 0; } -static void *__bencode_alloc(bencode_buffer_t *buf, unsigned int size) { +static void *__bencode_alloc(bencode_buffer_t *buf, unsigned int size) +{ struct __bencode_buffer_piece *piece; void *ret; - unsigned int align_size = ((size + BENCODE_ALLOC_ALIGN - 1) / BENCODE_ALLOC_ALIGN) * BENCODE_ALLOC_ALIGN; + unsigned int align_size = + ((size + BENCODE_ALLOC_ALIGN - 1) / BENCODE_ALLOC_ALIGN) + * BENCODE_ALLOC_ALIGN; - if (!buf) + if(!buf) return NULL; - if (buf->error) + if(buf->error) return NULL; piece = buf->pieces; - if (size <= piece->left) + if(size <= piece->left) goto alloc; piece = __bencode_piece_new(size); - if (!piece) { + if(!piece) { buf->error = 1; return NULL; } @@ -124,7 +131,7 @@ static void *__bencode_alloc(bencode_buffer_t *buf, unsigned int size) { assert(size <= piece->left); alloc: - if (piece->left >= align_size) + if(piece->left >= align_size) piece->left -= align_size; else piece->left = 0; @@ -133,89 +140,97 @@ static void *__bencode_alloc(bencode_buffer_t *buf, unsigned int size) { return ret; } -void bencode_buffer_free(bencode_buffer_t *buf) { +void bencode_buffer_free(bencode_buffer_t *buf) +{ struct __bencode_free_list *fl; struct __bencode_buffer_piece *piece, *next; - for (fl = buf->free_list; fl; fl = fl->next) + for(fl = buf->free_list; fl; fl = fl->next) fl->func(fl->ptr); - for (piece = buf->pieces; piece; piece = next) { + for(piece = buf->pieces; piece; piece = next) { next = piece->next; BENCODE_FREE(piece); } } -static bencode_item_t *__bencode_item_alloc(bencode_buffer_t *buf, unsigned int payload) { +static bencode_item_t *__bencode_item_alloc( + bencode_buffer_t *buf, unsigned int payload) +{ bencode_item_t *ret; ret = __bencode_alloc(buf, sizeof(struct bencode_item) + payload); - if (!ret) + if(!ret) return NULL; ret->buffer = buf; __bencode_item_init(ret); return ret; } -bencode_item_t *bencode_dictionary(bencode_buffer_t *buf) { +bencode_item_t *bencode_dictionary(bencode_buffer_t *buf) +{ bencode_item_t *ret; ret = __bencode_item_alloc(buf, 0); - if (!ret) + if(!ret) return NULL; __bencode_dictionary_init(ret); return ret; } -bencode_item_t *bencode_list(bencode_buffer_t *buf) { +bencode_item_t *bencode_list(bencode_buffer_t *buf) +{ bencode_item_t *ret; ret = __bencode_item_alloc(buf, 0); - if (!ret) + if(!ret) return NULL; __bencode_list_init(ret); return ret; } -static void __bencode_container_add(bencode_item_t *parent, bencode_item_t *child) { - if (!parent) +static void __bencode_container_add( + bencode_item_t *parent, bencode_item_t *child) +{ + if(!parent) return; - if (!child) + if(!child) return; assert(child->parent == NULL); assert(child->sibling == NULL); child->parent = parent; - if (parent->last_child) + if(parent->last_child) parent->last_child->sibling = child; parent->last_child = child; - if (!parent->child) + if(!parent->child) parent->child = child; - while (parent) { + while(parent) { parent->iov_cnt += child->iov_cnt; parent->str_len += child->str_len; parent = parent->parent; } } -static bencode_item_t *__bencode_string_alloc(bencode_buffer_t *buf, const void *base, - int str_len, int iov_len, int iov_cnt, bencode_type_t type) +static bencode_item_t *__bencode_string_alloc(bencode_buffer_t *buf, + const void *base, int str_len, int iov_len, int iov_cnt, + bencode_type_t type) { bencode_item_t *ret; int len_len; assert((str_len <= 99999) && (str_len >= 0)); ret = __bencode_item_alloc(buf, 7); - if (!ret) + if(!ret) return NULL; len_len = sprintf(ret->__buf, "%d:", str_len); ret->type = type; ret->iov[0].iov_base = ret->__buf; ret->iov[0].iov_len = len_len; - ret->iov[1].iov_base = (void *) base; + ret->iov[1].iov_base = (void *)base; ret->iov[1].iov_len = iov_len; ret->iov_cnt = iov_cnt + 1; ret->str_len = len_len + str_len; @@ -223,43 +238,51 @@ static bencode_item_t *__bencode_string_alloc(bencode_buffer_t *buf, const void return ret; } -bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, int len) { +bencode_item_t *bencode_string_len_dup( + bencode_buffer_t *buf, const char *s, int len) +{ char *sd = __bencode_alloc(buf, len); - if (!sd) + if(!sd) return NULL; memcpy(sd, s, len); return bencode_string_len(buf, sd, len); } -bencode_item_t *bencode_string_len(bencode_buffer_t *buf, const char *s, int len) { +bencode_item_t *bencode_string_len( + bencode_buffer_t *buf, const char *s, int len) +{ return __bencode_string_alloc(buf, s, len, len, 1, BENCODE_STRING); } -bencode_item_t *bencode_string_iovec(bencode_buffer_t *buf, const struct iovec *iov, int iov_cnt, int str_len) { +bencode_item_t *bencode_string_iovec(bencode_buffer_t *buf, + const struct iovec *iov, int iov_cnt, int str_len) +{ int i; - if (iov_cnt < 0) + if(iov_cnt < 0) return NULL; - if (str_len < 0) { + if(str_len < 0) { str_len = 0; - for (i = 0; i < iov_cnt; i++) + for(i = 0; i < iov_cnt; i++) str_len += iov[i].iov_len; } - return __bencode_string_alloc(buf, iov, str_len, iov_cnt, iov_cnt, BENCODE_IOVEC); + return __bencode_string_alloc( + buf, iov, str_len, iov_cnt, iov_cnt, BENCODE_IOVEC); } -bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i) { +bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i) +{ bencode_item_t *ret; int alen, rlen; alen = 8; - while (1) { + while(1) { ret = __bencode_item_alloc(buf, alen + 3); - if (!ret) + if(!ret) return NULL; rlen = snprintf(ret->__buf, alen, "i%llde", i); - if (rlen < alen) + if(rlen < alen) break; alen <<= 1; } @@ -275,38 +298,44 @@ bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i) { return ret; } -bencode_item_t *bencode_dictionary_add_len(bencode_item_t *dict, const char *key, int keylen, bencode_item_t *val) { +bencode_item_t *bencode_dictionary_add_len( + bencode_item_t *dict, const char *key, int keylen, bencode_item_t *val) +{ bencode_item_t *str; - if (!dict || !val) + if(!dict || !val) return NULL; assert(dict->type == BENCODE_DICTIONARY); str = bencode_string_len(dict->buffer, key, keylen); - if (!str) + if(!str) return NULL; __bencode_container_add(dict, str); __bencode_container_add(dict, val); return val; } -bencode_item_t *bencode_list_add(bencode_item_t *list, bencode_item_t *item) { - if (!list || !item) +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; } -static int __bencode_iovec_cpy(struct iovec *out, const struct iovec *in, int num) { +static int __bencode_iovec_cpy( + struct iovec *out, const struct iovec *in, int num) +{ memcpy(out, in, num * sizeof(*out)); return num; } -static int __bencode_str_cpy(char *out, const struct iovec *in, int num) { +static int __bencode_str_cpy(char *out, const struct iovec *in, int num) +{ char *orig = out; - while (--num >= 0) { + while(--num >= 0) { memcpy(out, in->iov_base, in->iov_len); out += in->iov_len; in++; @@ -314,7 +343,8 @@ static int __bencode_str_cpy(char *out, const struct iovec *in, int num) { return out - orig; } -static int __bencode_iovec_dump(struct iovec *out, bencode_item_t *item) { +static int __bencode_iovec_dump(struct iovec *out, bencode_item_t *item) +{ bencode_item_t *child; struct iovec *orig = out; @@ -322,21 +352,23 @@ static int __bencode_iovec_dump(struct iovec *out, bencode_item_t *item) { out += __bencode_iovec_cpy(out, &item->iov[0], 1); child = item->child; - while (child) { + while(child) { out += __bencode_iovec_dump(out, child); child = child->sibling; } - if (item->type == BENCODE_IOVEC) - out += __bencode_iovec_cpy(out, item->iov[1].iov_base, item->iov[1].iov_len); - else if (item->iov[1].iov_base) + if(item->type == BENCODE_IOVEC) + out += __bencode_iovec_cpy( + out, item->iov[1].iov_base, item->iov[1].iov_len); + else if(item->iov[1].iov_base) out += __bencode_iovec_cpy(out, &item->iov[1], 1); assert((out - orig) == item->iov_cnt); return item->iov_cnt; } -static int __bencode_str_dump(char *out, bencode_item_t *item) { +static int __bencode_str_dump(char *out, bencode_item_t *item) +{ char *orig = out; bencode_item_t *child; @@ -344,14 +376,15 @@ static int __bencode_str_dump(char *out, bencode_item_t *item) { out += __bencode_str_cpy(out, &item->iov[0], 1); child = item->child; - while (child) { + while(child) { out += __bencode_str_dump(out, child); child = child->sibling; } - if (item->type == BENCODE_IOVEC) - out += __bencode_str_cpy(out, item->iov[1].iov_base, item->iov[1].iov_len); - else if (item->iov[1].iov_base) + if(item->type == BENCODE_IOVEC) + out += __bencode_str_cpy( + out, item->iov[1].iov_base, item->iov[1].iov_len); + else if(item->iov[1].iov_base) out += __bencode_str_cpy(out, &item->iov[1], 1); assert((out - orig) == item->str_len); @@ -359,136 +392,147 @@ static int __bencode_str_dump(char *out, bencode_item_t *item) { return item->str_len; } -struct iovec *bencode_iovec(bencode_item_t *root, int *cnt, unsigned int head, unsigned int tail) { +struct iovec *bencode_iovec( + bencode_item_t *root, int *cnt, unsigned int head, unsigned int tail) +{ struct iovec *ret; - if (!root) + if(!root) return NULL; assert(cnt != NULL); assert(root->iov_cnt > 0); - ret = __bencode_alloc(root->buffer, sizeof(*ret) * (root->iov_cnt + head + tail)); - if (!ret) + ret = __bencode_alloc( + root->buffer, sizeof(*ret) * (root->iov_cnt + head + tail)); + if(!ret) return NULL; *cnt = __bencode_iovec_dump(ret + head, root); return ret; } -char *bencode_collapse(bencode_item_t *root, int *len) { +char *bencode_collapse(bencode_item_t *root, int *len) +{ char *ret; int l; - if (!root) + if(!root) return NULL; assert(root->str_len > 0); ret = __bencode_alloc(root->buffer, root->str_len + 1); - if (!ret) + if(!ret) return NULL; l = __bencode_str_dump(ret, root); - if (len) + if(len) *len = l; return ret; } -char *bencode_collapse_dup(bencode_item_t *root, int *len) { +char *bencode_collapse_dup(bencode_item_t *root, int *len) +{ char *ret; int l; - if (!root) + if(!root) return NULL; assert(root->str_len > 0); ret = BENCODE_MALLOC(root->str_len + 1); - if (!ret) + if(!ret) return NULL; l = __bencode_str_dump(ret, root); - if (len) + if(len) *len = l; return ret; } -static unsigned int __bencode_hash_str_len(const unsigned char *s, int len) { +static unsigned int __bencode_hash_str_len(const unsigned char *s, int len) +{ unsigned long *ul; unsigned int *ui; unsigned short *us; - if (len >= sizeof(*ul)) { - ul = (void *) s; + if(len >= sizeof(*ul)) { + ul = (void *)s; return *ul % BENCODE_HASH_BUCKETS; } - if (len >= sizeof(*ui)) { - ui = (void *) s; + if(len >= sizeof(*ui)) { + ui = (void *)s; return *ui % BENCODE_HASH_BUCKETS; } - if (len >= sizeof(*us)) { - us = (void *) s; + if(len >= sizeof(*us)) { + us = (void *)s; return *us % BENCODE_HASH_BUCKETS; } - if (len >= sizeof(*s)) + if(len >= sizeof(*s)) return *s % BENCODE_HASH_BUCKETS; return 0; } -static unsigned int __bencode_hash_str(bencode_item_t *str) { +static unsigned int __bencode_hash_str(bencode_item_t *str) +{ assert(str->type == BENCODE_STRING); return __bencode_hash_str_len(str->iov[1].iov_base, str->iov[1].iov_len); } -static void __bencode_hash_insert(bencode_item_t *key, struct __bencode_hash *hash) { +static void __bencode_hash_insert( + bencode_item_t *key, struct __bencode_hash *hash) +{ unsigned int bucket, i; i = bucket = __bencode_hash_str(key); - while (1) { - if (!hash->buckets[i]) { + while(1) { + if(!hash->buckets[i]) { hash->buckets[i] = key; break; } i++; - if (i >= BENCODE_HASH_BUCKETS) + if(i >= BENCODE_HASH_BUCKETS) i = 0; - if (i == bucket) + if(i == bucket) break; } } -static bencode_item_t *__bencode_decode_dictionary(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_dictionary( + bencode_buffer_t *buf, const char *s, const char *end) +{ bencode_item_t *ret, *key, *value; struct __bencode_hash *hash; - if (*s != 'd') + if(*s != 'd') return NULL; s++; ret = __bencode_item_alloc(buf, sizeof(*hash)); - if (!ret) + if(!ret) return NULL; __bencode_dictionary_init(ret); ret->value = 1; - hash = (void *) ret->__buf; + hash = (void *)ret->__buf; memset(hash, 0, sizeof(*hash)); - while (s < end) { + while(s < end) { key = __bencode_decode(buf, s, end); - if (!key) + if(!key) return NULL; s += key->str_len; - if (key->type == BENCODE_END_MARKER) + if(key->type == BENCODE_END_MARKER) break; - if (key->type != BENCODE_STRING) + if(key->type != BENCODE_STRING) return NULL; __bencode_container_add(ret, key); - if (s >= end) + if(s >= end) return NULL; value = __bencode_decode(buf, s, end); - if (!value) + if(!value) return NULL; s += value->str_len; - if (value->type == BENCODE_END_MARKER) + if(value->type == BENCODE_END_MARKER) return NULL; __bencode_container_add(ret, value); @@ -498,24 +542,26 @@ static bencode_item_t *__bencode_decode_dictionary(bencode_buffer_t *buf, const return ret; } -static bencode_item_t *__bencode_decode_list(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_list( + bencode_buffer_t *buf, const char *s, const char *end) +{ bencode_item_t *ret, *item; - if (*s != 'l') + if(*s != 'l') return NULL; s++; ret = __bencode_item_alloc(buf, 0); - if (!ret) + if(!ret) return NULL; __bencode_list_init(ret); - while (s < end) { + while(s < end) { item = __bencode_decode(buf, s, end); - if (!item) + if(!item) return NULL; s += item->str_len; - if (item->type == BENCODE_END_MARKER) + if(item->type == BENCODE_END_MARKER) break; __bencode_container_add(ret, item); } @@ -523,42 +569,44 @@ static bencode_item_t *__bencode_decode_list(bencode_buffer_t *buf, const char * return ret; } -static bencode_item_t *__bencode_decode_integer(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_integer( + bencode_buffer_t *buf, const char *s, const char *end) +{ long long int i; const char *orig = s; char *convend; bencode_item_t *ret; - if (*s != 'i') + if(*s != 'i') return NULL; s++; - if (s >= end) + if(s >= end) return NULL; - if (*s == '0') { + if(*s == '0') { i = 0; s++; goto done; } i = strtoll(s, &convend, 10); - if (convend == s) + if(convend == s) return NULL; s += (convend - s); done: - if (s >= end) + if(s >= end) return NULL; - if (*s != 'e') + if(*s != 'e') return NULL; s++; ret = __bencode_item_alloc(buf, 0); - if (!ret) + if(!ret) return NULL; ret->type = BENCODE_INTEGER; - ret->iov[0].iov_base = (void *) orig; + ret->iov[0].iov_base = (void *)orig; ret->iov[0].iov_len = s - orig; ret->iov[1].iov_base = NULL; ret->iov[1].iov_len = 0; @@ -569,40 +617,42 @@ static bencode_item_t *__bencode_decode_integer(bencode_buffer_t *buf, const cha return ret; } -static bencode_item_t *__bencode_decode_string(bencode_buffer_t *buf, const char *s, const char *end) { +static bencode_item_t *__bencode_decode_string( + bencode_buffer_t *buf, const char *s, const char *end) +{ unsigned long int sl; char *convend; const char *orig = s; bencode_item_t *ret; - if (*s == '0') { + if(*s == '0') { sl = 0; s++; goto colon; } sl = strtoul(s, &convend, 10); - if (convend == s) + if(convend == s) return NULL; s += (convend - s); colon: - if (s >= end) + if(s >= end) return NULL; - if (*s != ':') + if(*s != ':') return NULL; s++; - if (s + sl > end) + if(s + sl > end) return NULL; ret = __bencode_item_alloc(buf, 0); - if (!ret) + if(!ret) return NULL; ret->type = BENCODE_STRING; - ret->iov[0].iov_base = (void *) orig; + ret->iov[0].iov_base = (void *)orig; ret->iov[0].iov_len = s - orig; - ret->iov[1].iov_base = (void *) s; + ret->iov[1].iov_base = (void *)s; ret->iov[1].iov_len = sl; ret->iov_cnt = 2; ret->str_len = s - orig + sl; @@ -610,11 +660,13 @@ static bencode_item_t *__bencode_decode_string(bencode_buffer_t *buf, const char return ret; } -static bencode_item_t *__bencode_decode(bencode_buffer_t *buf, const char *s, const char *end) { - if (s >= end) +static bencode_item_t *__bencode_decode( + bencode_buffer_t *buf, const char *s, const char *end) +{ + if(s >= end) return NULL; - switch (*s) { + switch(*s) { case 'd': return __bencode_decode_dictionary(buf, s, end); case 'l': @@ -639,68 +691,76 @@ static bencode_item_t *__bencode_decode(bencode_buffer_t *buf, const char *s, co } } -bencode_item_t *bencode_decode(bencode_buffer_t *buf, const char *s, int len) { +bencode_item_t *bencode_decode(bencode_buffer_t *buf, const char *s, int len) +{ assert(s != NULL); return __bencode_decode(buf, s, s + len); } -static int __bencode_dictionary_key_match(bencode_item_t *key, const char *keystr, int keylen) { +static int __bencode_dictionary_key_match( + bencode_item_t *key, const char *keystr, int keylen) +{ assert(key->type == BENCODE_STRING); - if (keylen != key->iov[1].iov_len) + if(keylen != key->iov[1].iov_len) return 0; - if (memcmp(keystr, key->iov[1].iov_base, keylen)) + if(memcmp(keystr, key->iov[1].iov_base, keylen)) return 0; return 1; } -bencode_item_t *bencode_dictionary_get_len(bencode_item_t *dict, const char *keystr, int keylen) { +bencode_item_t *bencode_dictionary_get_len( + bencode_item_t *dict, const char *keystr, int keylen) +{ bencode_item_t *key; unsigned int bucket, i; struct __bencode_hash *hash; - if (!dict) + if(!dict) return NULL; - if (dict->type != BENCODE_DICTIONARY) + if(dict->type != BENCODE_DICTIONARY) return NULL; /* try hash lookup first if possible */ - if (dict->value == 1) { - hash = (void *) dict->__buf; - i = bucket = __bencode_hash_str_len((const unsigned char *) keystr, keylen); - while (1) { + if(dict->value == 1) { + hash = (void *)dict->__buf; + i = bucket = + __bencode_hash_str_len((const unsigned char *)keystr, keylen); + while(1) { key = hash->buckets[i]; - if (!key) + if(!key) return NULL; /* would be there, but isn't */ assert(key->sibling != NULL); - if (__bencode_dictionary_key_match(key, keystr, keylen)) + if(__bencode_dictionary_key_match(key, keystr, keylen)) return key->sibling; i++; - if (i >= BENCODE_HASH_BUCKETS) + if(i >= BENCODE_HASH_BUCKETS) i = 0; - if (i == bucket) + if(i == bucket) break; /* fall back to regular lookup */ } } - for (key = dict->child; key; key = key->sibling->sibling) { + for(key = dict->child; key; key = key->sibling->sibling) { assert(key->sibling != NULL); - if (__bencode_dictionary_key_match(key, keystr, keylen)) + if(__bencode_dictionary_key_match(key, keystr, keylen)) return key->sibling; } return NULL; } -void bencode_buffer_destroy_add(bencode_buffer_t *buf, free_func_t func, void *p) { +void bencode_buffer_destroy_add( + bencode_buffer_t *buf, free_func_t func, void *p) +{ struct __bencode_free_list *li; - if (!p) + if(!p) return; li = __bencode_alloc(buf, sizeof(*li)); - if (!li) + if(!li) return; li->ptr = p; li->func = func; diff --git a/src/modules/rtpengine/bencode.h b/src/modules/rtpengine/bencode.h index df7fa8be66b..8f3b434e7ac 100644 --- a/src/modules/rtpengine/bencode.h +++ b/src/modules/rtpengine/bencode.h @@ -17,42 +17,43 @@ typedef struct bencode_buffer bencode_buffer_t; typedef struct bencode_item bencode_item_t; typedef void (*free_func_t)(void *); -enum bencode_type { +enum bencode_type +{ BENCODE_INVALID = 0, - BENCODE_STRING, /* byte string */ - BENCODE_INTEGER, /* long long int */ - BENCODE_LIST, /* flat list of other objects */ - BENCODE_DICTIONARY, /* dictionary of key/values pairs. keys are always strings */ - BENCODE_IOVEC, /* special case of a string, built through bencode_string_iovec() */ - BENCODE_END_MARKER, /* used internally only */ + BENCODE_STRING, /* byte string */ + BENCODE_INTEGER, /* long long int */ + BENCODE_LIST, /* flat list of other objects */ + BENCODE_DICTIONARY, /* dictionary of key/values pairs. keys are always strings */ + BENCODE_IOVEC, /* special case of a string, built through bencode_string_iovec() */ + BENCODE_END_MARKER, /* used internally only */ }; -struct bencode_item { +struct bencode_item +{ bencode_type_t type; - struct iovec iov[2]; /* when decoding, iov[1] contains the contents of a string object */ + struct iovec iov + [2]; /* when decoding, iov[1] contains the contents of a string object */ unsigned int iov_cnt; - unsigned int str_len; /* length of the whole ENCODED object. NOT the length of a byte string */ - long long int value; /* when decoding an integer, contains the value; otherwise used internally */ + unsigned int + str_len; /* length of the whole ENCODED object. NOT the length of a byte string */ + long long int + value; /* when decoding an integer, contains the value; otherwise used internally */ bencode_item_t *parent, *child, *last_child, *sibling; bencode_buffer_t *buffer; char __buf[0]; }; -struct bencode_buffer { +struct bencode_buffer +{ struct __bencode_buffer_piece *pieces; struct __bencode_free_list *free_list; - int error:1; /* set to !0 if allocation failed at any point */ + int error : 1; /* set to !0 if allocation failed at any point */ }; - - - /* to embed BENCODE_STRING objects into printf-like functions */ #define BENCODE_FORMAT "%.*s" -#define BENCODE_FMT(b) (int) (b)->iov[1].iov_len, (char *) (b)->iov[1].iov_base - - +#define BENCODE_FMT(b) (int)(b)->iov[1].iov_len, (char *)(b)->iov[1].iov_base /*** INIT & DESTROY ***/ @@ -87,9 +88,6 @@ void bencode_buffer_destroy_add(bencode_buffer_t *buf, free_func_t, void *); INLINE bencode_buffer_t *bencode_item_buffer(bencode_item_t *); - - - /*** DICTIONARY BUILDING ***/ /* Adds a new key/value pair to a dictionary. Memory will be allocated from the same bencode_buffer_t @@ -99,34 +97,42 @@ INLINE bencode_buffer_t *bencode_item_buffer(bencode_item_t *); * Also, the function does not reorder keys into lexicographical order; keys will be encoded in * the same order as they've been added. The key must a null-terminated string. * The value to be added must not have been previously linked into any other dictionary or list. */ -INLINE bencode_item_t *bencode_dictionary_add(bencode_item_t *dict, const char *key, bencode_item_t *val); -INLINE bencode_item_t *bencode_dictionary_str_add(bencode_item_t *dict, const str *key, bencode_item_t *val); +INLINE bencode_item_t *bencode_dictionary_add( + bencode_item_t *dict, const char *key, bencode_item_t *val); +INLINE bencode_item_t *bencode_dictionary_str_add( + bencode_item_t *dict, const str *key, bencode_item_t *val); /* 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); +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, possibly duplicated into the * bencode_buffer_t object. */ -INLINE bencode_item_t *bencode_dictionary_add_string(bencode_item_t *dict, const char *key, const char *val); -INLINE bencode_item_t *bencode_dictionary_add_string_dup(bencode_item_t *dict, const char *key, const char *val); +INLINE bencode_item_t *bencode_dictionary_add_string( + bencode_item_t *dict, const char *key, const char *val); +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 */ -INLINE bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, const char *key, const str *val); -INLINE bencode_item_t *bencode_dictionary_str_add_str(bencode_item_t *dict, const str *key, const str *val); -INLINE bencode_item_t *bencode_dictionary_add_str_dup(bencode_item_t *dict, const char *key, const str *val); +INLINE bencode_item_t *bencode_dictionary_add_str( + bencode_item_t *dict, const char *key, const str *val); +INLINE bencode_item_t *bencode_dictionary_str_add_str( + bencode_item_t *dict, const str *key, const str *val); +INLINE bencode_item_t *bencode_dictionary_add_str_dup( + bencode_item_t *dict, const char *key, const str *val); /* Ditto, but adds a string created through an iovec array to the dictionary. See * bencode_string_iovec(). */ -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); +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 functions to add the respective (newly created) objects to a dictionary */ -INLINE bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dict, const char *key, long long int val); -INLINE bencode_item_t *bencode_dictionary_add_dictionary(bencode_item_t *dict, const char *key); -INLINE bencode_item_t *bencode_dictionary_add_list(bencode_item_t *dict, const char *key); - - - +INLINE bencode_item_t *bencode_dictionary_add_integer( + bencode_item_t *dict, const char *key, long long int val); +INLINE bencode_item_t *bencode_dictionary_add_dictionary( + bencode_item_t *dict, const char *key); +INLINE bencode_item_t *bencode_dictionary_add_list( + bencode_item_t *dict, const char *key); /*** LIST BUILDING ***/ @@ -136,15 +142,13 @@ INLINE bencode_item_t *bencode_dictionary_add_list(bencode_item_t *dict, const c bencode_item_t *bencode_list_add(bencode_item_t *list, bencode_item_t *item); /* Convenience function to add the respective (newly created) objects to a list */ -INLINE bencode_item_t *bencode_list_add_string(bencode_item_t *list, const char *s); +INLINE bencode_item_t *bencode_list_add_string( + bencode_item_t *list, const char *s); INLINE bencode_item_t *bencode_list_add_str(bencode_item_t *list, const str *s); INLINE bencode_item_t *bencode_list_add_list(bencode_item_t *list); INLINE bencode_item_t *bencode_list_add_dictionary(bencode_item_t *list); - - - /*** STRING BUILDING & HANDLING ***/ /* Creates a new byte-string object. The given string does not have to be null-terminated, instead @@ -152,7 +156,8 @@ INLINE bencode_item_t *bencode_list_add_dictionary(bencode_item_t *list); * be allocated. * Strings are not copied or duplicated, so the string pointed to by "s" must remain valid until * the complete document is finally encoded or sent out. */ -bencode_item_t *bencode_string_len(bencode_buffer_t *buf, const char *s, int len); +bencode_item_t *bencode_string_len( + bencode_buffer_t *buf, const char *s, int len); /* Creates a new byte-string object. The given string must be null-terminated. Otherwise identical * to bencode_string_len(). */ @@ -164,7 +169,8 @@ INLINE bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s); /* Identical to the above three functions, but copies the string into the bencode_buffer_t object. * Thus, the given string doesn't have to remain valid and accessible afterwards. */ -bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, int len); +bencode_item_t *bencode_string_len_dup( + bencode_buffer_t *buf, const char *s, int len); INLINE bencode_item_t *bencode_string_dup(bencode_buffer_t *buf, const char *s); INLINE bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s); @@ -174,7 +180,8 @@ INLINE bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s); * document is encoded. The full length of the string composed of the iovec array is given in the * "str_len" parameter, which can be negative, in which case the array is iterated to calculate the * length. */ -bencode_item_t *bencode_string_iovec(bencode_buffer_t *buf, const struct iovec *iov, int iov_cnt, int str_len); +bencode_item_t *bencode_string_iovec(bencode_buffer_t *buf, + const struct iovec *iov, int iov_cnt, int str_len); /* Convenience function to compare a string object to a regular C string. Returns 2 if object * isn't a string object, otherwise returns according to strcmp(). */ @@ -185,18 +192,12 @@ INLINE int bencode_strcmp(bencode_item_t *a, const char *b); INLINE str *bencode_get_str(bencode_item_t *in, str *out); - - - /*** INTEGER BUILDING ***/ /* Creates a new integer object. Returns NULL if no memory could be allocated. */ bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i); - - - /*** COLLAPSING & ENCODING ***/ /* Collapses and encodes the complete document structure under the "root" element (which normally @@ -216,7 +217,8 @@ bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i); * [(head + cnt) .. (head + cnt + tail - 1)] = unused and uninitialized iovec structures * * The returned array will be freed when the corresponding bencode_buffer_t object is destroyed. */ -struct iovec *bencode_iovec(bencode_item_t *root, int *cnt, unsigned int head, unsigned int tail); +struct iovec *bencode_iovec( + bencode_item_t *root, int *cnt, unsigned int head, unsigned int tail); /* Similar to bencode_iovec(), but instead returns the encoded document as a null-terminated string. * Memory for the string is allocated from the same bencode_buffer_t object as the "root" object @@ -236,9 +238,6 @@ static str *bencode_collapse_str(bencode_item_t *root, str *out); char *bencode_collapse_dup(bencode_item_t *root, int *len); - - - /*** DECODING ***/ /* Decodes an encoded document from a string into a tree of bencode_item_t objects. The string does @@ -290,13 +289,12 @@ bencode_item_t *bencode_decode(bencode_buffer_t *buf, const char *s, int len); /* Identical to bencode_decode(), but returns successfully only if the type of the decoded object match * "expect". */ -INLINE bencode_item_t *bencode_decode_expect(bencode_buffer_t *buf, const char *s, int len, bencode_type_t expect); +INLINE bencode_item_t *bencode_decode_expect( + bencode_buffer_t *buf, const char *s, int len, bencode_type_t expect); /* Identical to bencode_decode_expect() but takes a "str" argument. */ -INLINE bencode_item_t *bencode_decode_expect_str(bencode_buffer_t *buf, const str *s, bencode_type_t expect); - - - +INLINE bencode_item_t *bencode_decode_expect_str( + bencode_buffer_t *buf, const str *s, bencode_type_t expect); /*** DICTIONARY LOOKUP & EXTRACTION ***/ @@ -304,241 +302,311 @@ INLINE bencode_item_t *bencode_decode_expect_str(bencode_buffer_t *buf, const st /* Searches the given dictionary object for the given key and returns the respective value. Returns * NULL if the given object isn't a dictionary or if the key doesn't exist. The key must be a * null-terminated string. */ -INLINE bencode_item_t *bencode_dictionary_get(bencode_item_t *dict, const char *key); +INLINE bencode_item_t *bencode_dictionary_get( + bencode_item_t *dict, const char *key); /* Identical to bencode_dictionary_get() but doesn't require the key to be null-terminated. */ -bencode_item_t *bencode_dictionary_get_len(bencode_item_t *dict, const char *key, int key_len); +bencode_item_t *bencode_dictionary_get_len( + bencode_item_t *dict, const char *key, int key_len); /* Identical to bencode_dictionary_get() but returns the value only if its type is a string, and * returns it as a pointer to the string itself. Returns NULL if the value is of some other type. The * returned string is NOT null-terminated. Length of the string is returned in *len, which must be a * valid pointer. The returned string will be valid until dict's bencode_buffer_t object is destroyed. */ -INLINE char *bencode_dictionary_get_string(bencode_item_t *dict, const char *key, int *len); +INLINE char *bencode_dictionary_get_string( + bencode_item_t *dict, const char *key, int *len); /* Identical to bencode_dictionary_get_string() but fills in a "str" struct. Returns str->s, which * may be NULL. */ -INLINE char *bencode_dictionary_get_str(bencode_item_t *dict, const char *key, str *str); +INLINE char *bencode_dictionary_get_str( + bencode_item_t *dict, const char *key, str *str); /* Looks up the given key in the dictionary and compares the corresponding value to the given * null-terminated string. Returns 2 if the key isn't found or if the value isn't a string, otherwise * returns according to strcmp(). */ -INLINE int bencode_dictionary_get_strcmp(bencode_item_t *dict, const char *key, const char *str); +INLINE int bencode_dictionary_get_strcmp( + bencode_item_t *dict, const char *key, const char *str); /* Identical to bencode_dictionary_get() but returns the string in a newly allocated buffer (using the * BENCODE_MALLOC function), which remains valid even after bencode_buffer_t is destroyed. */ -INLINE char *bencode_dictionary_get_string_dup(bencode_item_t *dict, const char *key, int *len); +INLINE char *bencode_dictionary_get_string_dup( + bencode_item_t *dict, const char *key, int *len); /* Combines bencode_dictionary_get_str() and bencode_dictionary_get_string_dup(). Fills in a "str" * struct, but copies the string into a newly allocated buffer. Returns str->s. */ -INLINE char *bencode_dictionary_get_str_dup(bencode_item_t *dict, const char *key, str *str); +INLINE char *bencode_dictionary_get_str_dup( + bencode_item_t *dict, const char *key, str *str); /* Identical to bencode_dictionary_get_string() but expects an integer object. The parameter "defval" * specified which value should be returned if the key is not found or if the value is not an integer. */ -INLINE long long int bencode_dictionary_get_integer(bencode_item_t *dict, const char *key, long long int defval); +INLINE long long int bencode_dictionary_get_integer( + bencode_item_t *dict, const char *key, long long int defval); /* Identical to bencode_dictionary_get(), but returns the object only if its type matches "expect". */ -INLINE bencode_item_t *bencode_dictionary_get_expect(bencode_item_t *dict, const char *key, bencode_type_t expect); - - - +INLINE bencode_item_t *bencode_dictionary_get_expect( + bencode_item_t *dict, const char *key, bencode_type_t expect); /**************************/ -INLINE bencode_buffer_t *bencode_item_buffer(bencode_item_t *i) { - if (!i) +INLINE bencode_buffer_t *bencode_item_buffer(bencode_item_t *i) +{ + if(!i) return NULL; return i->buffer; } -INLINE bencode_item_t *bencode_string(bencode_buffer_t *buf, const char *s) { +INLINE bencode_item_t *bencode_string(bencode_buffer_t *buf, const char *s) +{ return bencode_string_len(buf, s, strlen(s)); } -INLINE bencode_item_t *bencode_string_dup(bencode_buffer_t *buf, const char *s) { +INLINE bencode_item_t *bencode_string_dup(bencode_buffer_t *buf, const char *s) +{ return bencode_string_len_dup(buf, s, strlen(s)); } -INLINE bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s) { +INLINE bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s) +{ return bencode_string_len(buf, s->s, s->len); } -INLINE bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s) { +INLINE bencode_item_t *bencode_str_dup(bencode_buffer_t *buf, const str *s) +{ return bencode_string_len_dup(buf, s->s, s->len); } -INLINE bencode_item_t *bencode_dictionary_add(bencode_item_t *dict, const char *key, bencode_item_t *val) { - if (!key) +INLINE bencode_item_t *bencode_dictionary_add( + bencode_item_t *dict, const char *key, bencode_item_t *val) +{ + if(!key) return NULL; return bencode_dictionary_add_len(dict, key, strlen(key), val); } -INLINE bencode_item_t *bencode_dictionary_str_add(bencode_item_t *dict, const str *key, bencode_item_t *val) { - if (!key) +INLINE bencode_item_t *bencode_dictionary_str_add( + bencode_item_t *dict, const str *key, bencode_item_t *val) +{ + if(!key) return NULL; return bencode_dictionary_add_len(dict, key->s, key->len, val); } -INLINE bencode_item_t *bencode_dictionary_add_string(bencode_item_t *dict, const char *key, const char *val) { - if (!val) +INLINE bencode_item_t *bencode_dictionary_add_string( + bencode_item_t *dict, const char *key, const char *val) +{ + if(!val) return NULL; - return bencode_dictionary_add(dict, key, bencode_string(bencode_item_buffer(dict), val)); + return bencode_dictionary_add( + dict, key, bencode_string(bencode_item_buffer(dict), val)); } -INLINE bencode_item_t *bencode_dictionary_add_string_dup(bencode_item_t *dict, const char *key, const char *val) { - if (!val) +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(bencode_item_buffer(dict), val)); + return bencode_dictionary_add( + dict, key, bencode_string_dup(bencode_item_buffer(dict), val)); } -INLINE bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, const char *key, const str *val) { - if (!val) +INLINE bencode_item_t *bencode_dictionary_add_str( + bencode_item_t *dict, const char *key, const str *val) +{ + if(!val) return NULL; - return bencode_dictionary_add(dict, key, bencode_str(bencode_item_buffer(dict), val)); + return bencode_dictionary_add( + dict, key, bencode_str(bencode_item_buffer(dict), val)); } -INLINE bencode_item_t *bencode_dictionary_str_add_str(bencode_item_t *dict, const str *key, const str *val) { - if (!val) +INLINE bencode_item_t *bencode_dictionary_str_add_str( + bencode_item_t *dict, const str *key, const str *val) +{ + if(!val) return NULL; - return bencode_dictionary_str_add(dict, key, bencode_str(bencode_item_buffer(dict), val)); + return bencode_dictionary_str_add( + dict, key, bencode_str(bencode_item_buffer(dict), val)); } -INLINE bencode_item_t *bencode_dictionary_add_str_dup(bencode_item_t *dict, const char *key, const str *val) { - if (!val) +INLINE bencode_item_t *bencode_dictionary_add_str_dup( + bencode_item_t *dict, const char *key, const str *val) +{ + if(!val) return NULL; - return bencode_dictionary_add(dict, key, bencode_str_dup(bencode_item_buffer(dict), val)); + return bencode_dictionary_add( + dict, key, bencode_str_dup(bencode_item_buffer(dict), val)); } -INLINE bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dict, const char *key, long long int val) { - return bencode_dictionary_add(dict, key, bencode_integer(bencode_item_buffer(dict), val)); +INLINE bencode_item_t *bencode_dictionary_add_integer( + bencode_item_t *dict, const char *key, long long int val) +{ + return bencode_dictionary_add( + dict, key, bencode_integer(bencode_item_buffer(dict), val)); } -INLINE bencode_item_t *bencode_dictionary_add_dictionary(bencode_item_t *dict, const char *key) { - return bencode_dictionary_add(dict, key, bencode_dictionary(bencode_item_buffer(dict))); +INLINE bencode_item_t *bencode_dictionary_add_dictionary( + bencode_item_t *dict, const char *key) +{ + return bencode_dictionary_add( + dict, key, bencode_dictionary(bencode_item_buffer(dict))); } -INLINE bencode_item_t *bencode_dictionary_add_list(bencode_item_t *dict, const char *key) { - return bencode_dictionary_add(dict, key, bencode_list(bencode_item_buffer(dict))); +INLINE bencode_item_t *bencode_dictionary_add_list( + bencode_item_t *dict, const char *key) +{ + return bencode_dictionary_add( + dict, key, bencode_list(bencode_item_buffer(dict))); } -INLINE bencode_item_t *bencode_list_add_string(bencode_item_t *list, const char *s) { +INLINE bencode_item_t *bencode_list_add_string( + bencode_item_t *list, const char *s) +{ return bencode_list_add(list, bencode_string(bencode_item_buffer(list), s)); } -INLINE bencode_item_t *bencode_list_add_str(bencode_item_t *list, const str *s) { +INLINE bencode_item_t *bencode_list_add_str(bencode_item_t *list, const str *s) +{ return bencode_list_add(list, bencode_str(bencode_item_buffer(list), s)); } -INLINE bencode_item_t *bencode_list_add_list(bencode_item_t *list) { +INLINE bencode_item_t *bencode_list_add_list(bencode_item_t *list) +{ return bencode_list_add(list, bencode_list(bencode_item_buffer(list))); } -INLINE bencode_item_t *bencode_list_add_dictionary(bencode_item_t *list) { - return bencode_list_add(list, bencode_dictionary(bencode_item_buffer(list))); +INLINE bencode_item_t *bencode_list_add_dictionary(bencode_item_t *list) +{ + return bencode_list_add( + list, bencode_dictionary(bencode_item_buffer(list))); } -INLINE bencode_item_t *bencode_dictionary_get(bencode_item_t *dict, const char *key) { - if (!key) +INLINE bencode_item_t *bencode_dictionary_get( + bencode_item_t *dict, const char *key) +{ + if(!key) return NULL; return bencode_dictionary_get_len(dict, key, strlen(key)); } -INLINE char *bencode_dictionary_get_string(bencode_item_t *dict, const char *key, int *len) { +INLINE char *bencode_dictionary_get_string( + bencode_item_t *dict, const char *key, int *len) +{ bencode_item_t *val; val = bencode_dictionary_get(dict, key); - if (!val || val->type != BENCODE_STRING) + if(!val || val->type != BENCODE_STRING) return NULL; *len = val->iov[1].iov_len; return val->iov[1].iov_base; } -INLINE char *bencode_dictionary_get_str(bencode_item_t *dict, const char *key, str *str) { +INLINE char *bencode_dictionary_get_str( + bencode_item_t *dict, const char *key, str *str) +{ str->s = bencode_dictionary_get_string(dict, key, &str->len); - if (!str->s) + if(!str->s) str->len = 0; return str->s; } -INLINE char *bencode_dictionary_get_string_dup(bencode_item_t *dict, const char *key, int *len) { +INLINE char *bencode_dictionary_get_string_dup( + bencode_item_t *dict, const char *key, int *len) +{ const char *s; char *ret; s = bencode_dictionary_get_string(dict, key, len); - if (!s) + if(!s) return NULL; ret = BENCODE_MALLOC(*len); - if (!ret) + if(!ret) return NULL; memcpy(ret, s, *len); return ret; } -INLINE char *bencode_dictionary_get_str_dup(bencode_item_t *dict, const char *key, str *str) { +INLINE char *bencode_dictionary_get_str_dup( + bencode_item_t *dict, const char *key, str *str) +{ str->s = bencode_dictionary_get_string_dup(dict, key, &str->len); return str->s; } -INLINE long long int bencode_dictionary_get_integer(bencode_item_t *dict, const char *key, long long int defval) { +INLINE long long int bencode_dictionary_get_integer( + bencode_item_t *dict, const char *key, long long int defval) +{ bencode_item_t *val; val = bencode_dictionary_get(dict, key); - if (!val || val->type != BENCODE_INTEGER) + if(!val || val->type != BENCODE_INTEGER) return defval; return val->value; } -INLINE bencode_item_t *bencode_decode_expect(bencode_buffer_t *buf, const char *s, int len, bencode_type_t expect) { +INLINE bencode_item_t *bencode_decode_expect( + bencode_buffer_t *buf, const char *s, int len, bencode_type_t expect) +{ bencode_item_t *ret; ret = bencode_decode(buf, s, len); - if (!ret || ret->type != expect) + if(!ret || ret->type != expect) return NULL; return ret; } -INLINE bencode_item_t *bencode_decode_expect_str(bencode_buffer_t *buf, const str *s, bencode_type_t expect) { +INLINE bencode_item_t *bencode_decode_expect_str( + bencode_buffer_t *buf, const str *s, bencode_type_t expect) +{ return bencode_decode_expect(buf, s->s, s->len, expect); } -INLINE bencode_item_t *bencode_dictionary_get_expect(bencode_item_t *dict, const char *key, bencode_type_t expect) { +INLINE bencode_item_t *bencode_dictionary_get_expect( + bencode_item_t *dict, const char *key, bencode_type_t expect) +{ bencode_item_t *ret; ret = bencode_dictionary_get(dict, key); - if (!ret || ret->type != expect) + if(!ret || ret->type != expect) return NULL; return ret; } -INLINE str *bencode_collapse_str(bencode_item_t *root, str *out) { +INLINE str *bencode_collapse_str(bencode_item_t *root, str *out) +{ out->s = bencode_collapse(root, &out->len); return out; } -INLINE int bencode_strcmp(bencode_item_t *a, const char *b) { +INLINE int bencode_strcmp(bencode_item_t *a, const char *b) +{ int len; - if (a->type != BENCODE_STRING) + if(a->type != BENCODE_STRING) return 2; len = strlen(b); - if (a->iov[1].iov_len < len) + if(a->iov[1].iov_len < len) return -1; - if (a->iov[1].iov_len > len) + if(a->iov[1].iov_len > len) return 1; return memcmp(a->iov[1].iov_base, b, len); } -INLINE int bencode_dictionary_get_strcmp(bencode_item_t *dict, const char *key, const char *str) { +INLINE int bencode_dictionary_get_strcmp( + bencode_item_t *dict, const char *key, const char *str) +{ bencode_item_t *i; i = bencode_dictionary_get(dict, key); - if (!i) + if(!i) return 2; return bencode_strcmp(i, str); } -INLINE str *bencode_get_str(bencode_item_t *in, str *out) { - if (!in || in->type != BENCODE_STRING) +INLINE str *bencode_get_str(bencode_item_t *in, str *out) +{ + if(!in || in->type != BENCODE_STRING) return NULL; out->s = in->iov[1].iov_base; out->len = in->iov[1].iov_len; return out; } -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) +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) { - return bencode_dictionary_add(dict, key, bencode_string_iovec(bencode_item_buffer(dict), iov, iov_cnt, str_len)); + return bencode_dictionary_add(dict, key, + bencode_string_iovec( + bencode_item_buffer(dict), iov, iov_cnt, str_len)); } #endif diff --git a/src/modules/rtpengine/config.c b/src/modules/rtpengine/config.c index 82cbffa2178..f002f027332 100644 --- a/src/modules/rtpengine/config.c +++ b/src/modules/rtpengine/config.c @@ -30,29 +30,32 @@ #include "config.h" -struct cfg_group_rtpengine default_rtpengine_cfg = { - 60, /* default disable timeout in seconds */ - 1, /* default aggressive_redetection enabled */ - 1000, /* default wait timeout in milliseconds */ - MAX_RTPP_TRIED_NODES, - 5, /* rtprengine retries */ - }; +struct cfg_group_rtpengine default_rtpengine_cfg = { + 60, /* default disable timeout in seconds */ + 1, /* default aggressive_redetection enabled */ + 1000, /* default wait timeout in milliseconds */ + MAX_RTPP_TRIED_NODES, 5, /* rtprengine retries */ +}; -void *rtpengine_cfg = &default_rtpengine_cfg; +void *rtpengine_cfg = &default_rtpengine_cfg; -cfg_def_t rtpengine_cfg_def[] = { - {"rtpengine_disable_tout", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, - "The time after which rtpengine module will try to communicate" - " with an RTPEngine instance after it has been marked disabled automatically. "}, - {"aggressive_redetection", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0, - "Determines if the sip proxy should force a query of all nodes" - " when all RTPEngine instances seem unavailable."}, - {"rtpengine_tout_ms", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, - "The total number of nodes inside a set to be queried before giving up" - " establishing a session"}, - {"queried_nodes_limit", CFG_VAR_INT | CFG_ATOMIC, 0, MAX_RTPP_TRIED_NODES, 0, 0, - "Timeout value expressed in milliseconds to wait for reply from RTPEngine"}, - {"rtpengine_retr", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, - "How many times the module should retry to send and receive after timeout was generated"}, - {0, 0, 0, 0, 0, 0} -}; +cfg_def_t rtpengine_cfg_def[] = { + {"rtpengine_disable_tout", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "The time after which rtpengine module will try to communicate" + " with an RTPEngine instance after it has been marked disabled " + "automatically. "}, + {"aggressive_redetection", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0, + "Determines if the sip proxy should force a query of all nodes" + " when all RTPEngine instances seem unavailable."}, + {"rtpengine_tout_ms", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "The total number of nodes inside a set to be queried before " + "giving up" + " establishing a session"}, + {"queried_nodes_limit", CFG_VAR_INT | CFG_ATOMIC, 0, + MAX_RTPP_TRIED_NODES, 0, 0, + "Timeout value expressed in milliseconds to wait for reply " + "from RTPEngine"}, + {"rtpengine_retr", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "How many times the module should retry to send and receive " + "after timeout was generated"}, + {0, 0, 0, 0, 0, 0}}; diff --git a/src/modules/rtpengine/config.h b/src/modules/rtpengine/config.h index d6c35b71ac5..b2891ead28c 100644 --- a/src/modules/rtpengine/config.h +++ b/src/modules/rtpengine/config.h @@ -24,19 +24,20 @@ #include "../../core/cfg/cfg.h" -#define MAX_RTPP_TRIED_NODES 30 - -struct cfg_group_rtpengine { - unsigned int rtpengine_disable_tout; - unsigned int aggressive_redetection; - unsigned int rtpengine_tout_ms; - unsigned int queried_nodes_limit; - unsigned int rtpengine_retr; +#define MAX_RTPP_TRIED_NODES 30 + +struct cfg_group_rtpengine +{ + unsigned int rtpengine_disable_tout; + unsigned int aggressive_redetection; + unsigned int rtpengine_tout_ms; + unsigned int queried_nodes_limit; + unsigned int rtpengine_retr; }; -extern struct cfg_group_rtpengine default_rtpengine_cfg; -extern void *rtpengine_cfg; -extern cfg_def_t rtpengine_cfg_def[]; +extern struct cfg_group_rtpengine default_rtpengine_cfg; +extern void *rtpengine_cfg; +extern cfg_def_t rtpengine_cfg_def[]; #endif diff --git a/src/modules/rtpengine/rtpengine.c b/src/modules/rtpengine/rtpengine.c index 51bd9e380a3..442df8ddd77 100644 --- a/src/modules/rtpengine/rtpengine.c +++ b/src/modules/rtpengine/rtpengine.c @@ -26,7 +26,7 @@ #include #include #ifndef __USE_BSD -#define __USE_BSD +#define __USE_BSD #endif #include #ifndef __FAVOR_BSD @@ -91,63 +91,65 @@ MODULE_VERSION #if !defined(AF_LOCAL) -#define AF_LOCAL AF_UNIX +#define AF_LOCAL AF_UNIX #endif #if !defined(PF_LOCAL) -#define PF_LOCAL PF_UNIX +#define PF_LOCAL PF_UNIX #endif /* NAT UAC test constants */ -#define NAT_UAC_TEST_C_1918 0x01 -#define NAT_UAC_TEST_RCVD 0x02 -#define NAT_UAC_TEST_V_1918 0x04 -#define NAT_UAC_TEST_S_1918 0x08 -#define NAT_UAC_TEST_RPORT 0x10 +#define NAT_UAC_TEST_C_1918 0x01 +#define NAT_UAC_TEST_RCVD 0x02 +#define NAT_UAC_TEST_V_1918 0x04 +#define NAT_UAC_TEST_S_1918 0x08 +#define NAT_UAC_TEST_RPORT 0x10 -#define COOKIE_SIZE 128 -#define HOSTNAME_SIZE 100 +#define COOKIE_SIZE 128 +#define HOSTNAME_SIZE 100 -#define DEFAULT_RTPP_SET_ID 0 +#define DEFAULT_RTPP_SET_ID 0 -enum { +enum +{ RPC_FOUND_ALL = 2, RPC_FOUND_ONE = 1, RPC_FOUND_NONE = 0, }; -#define CPORT "22222" +#define CPORT "22222" -struct ng_flags_parse { +struct ng_flags_parse +{ int via, to, packetize, transport, directional; - bencode_item_t *dict, *flags, *direction, *replace, *rtcp_mux, *sdes, - *t38,*received_from, - *codec, *codec_strip, *codec_offer, *codec_transcode, *codec_mask, - *codec_set, *codec_except; + bencode_item_t *dict, *flags, *direction, *replace, *rtcp_mux, *sdes, *t38, + *received_from, *codec, *codec_strip, *codec_offer, + *codec_transcode, *codec_mask, *codec_set, *codec_except; str call_id, from_tag, to_tag; }; static const char *command_strings[] = { - [OP_OFFER] = "offer", - [OP_ANSWER] = "answer", - [OP_DELETE] = "delete", - [OP_START_RECORDING] = "start recording", - [OP_QUERY] = "query", - [OP_PING] = "ping", - [OP_STOP_RECORDING] = "stop recording", - [OP_BLOCK_DTMF] = "block DTMF", - [OP_UNBLOCK_DTMF] = "unblock DTMF", - [OP_BLOCK_MEDIA] = "block media", - [OP_UNBLOCK_MEDIA] = "unblock media", - [OP_SILENCE_MEDIA] = "silence media", - [OP_UNSILENCE_MEDIA] = "unsilence media", - [OP_START_FORWARDING] = "start forwarding", - [OP_STOP_FORWARDING] = "stop forwarding", - [OP_PLAY_MEDIA] = "play media", - [OP_STOP_MEDIA] = "stop media", - [OP_PLAY_DTMF] = "play DTMF", + [OP_OFFER] = "offer", + [OP_ANSWER] = "answer", + [OP_DELETE] = "delete", + [OP_START_RECORDING] = "start recording", + [OP_QUERY] = "query", + [OP_PING] = "ping", + [OP_STOP_RECORDING] = "stop recording", + [OP_BLOCK_DTMF] = "block DTMF", + [OP_UNBLOCK_DTMF] = "unblock DTMF", + [OP_BLOCK_MEDIA] = "block media", + [OP_UNBLOCK_MEDIA] = "unblock media", + [OP_SILENCE_MEDIA] = "silence media", + [OP_UNSILENCE_MEDIA] = "unsilence media", + [OP_START_FORWARDING] = "start forwarding", + [OP_STOP_FORWARDING] = "stop forwarding", + [OP_PLAY_MEDIA] = "play media", + [OP_STOP_MEDIA] = "stop media", + [OP_PLAY_DTMF] = "play DTMF", }; -struct minmax_mos_stats { +struct minmax_mos_stats +{ str mos_param; str at_param; str packetloss_param; @@ -164,17 +166,17 @@ struct minmax_mos_stats { pv_elem_t *roundtrip_leg_pv; pv_elem_t *samples_pv; }; -struct minmax_mos_label_stats { +struct minmax_mos_label_stats +{ int got_any_pvs; str label_param; pv_elem_t *label_pv; - struct minmax_mos_stats min, - max, - average; + struct minmax_mos_stats min, max, average; }; -struct minmax_stats_vals { +struct minmax_stats_vals +{ long long mos; long long at; long long packetloss; @@ -185,9 +187,10 @@ struct minmax_stats_vals { long long avg_samples; /* our own running count to average the averages */ }; -#define RTPE_LIST_VERSION_DELAY 10 +#define RTPE_LIST_VERSION_DELAY 10 -typedef struct rtpe_list_version { +typedef struct rtpe_list_version +{ int vernum; time_t vertime; } rtpe_list_version_t; @@ -196,7 +199,7 @@ static rtpe_list_version_t *_rtpe_list_version = NULL; static int _rtpe_list_vernum_local = 0; static char *gencookie(); -static int rtpp_test(struct rtpp_node*, int, int); +static int rtpp_test(struct rtpp_node *, int, int); static int start_recording_f(struct sip_msg *, char *, char *); static int stop_recording_f(struct sip_msg *, char *, char *); static int block_dtmf_f(struct sip_msg *, char *, char *); @@ -221,22 +224,28 @@ static int w_rtpengine_query_v(sip_msg_t *msg, char *pfmt, char *pvar); static int fixup_rtpengine_query_v(void **param, int param_no); static int fixup_free_rtpengine_query_v(void **param, int param_no); -static int parse_flags(struct ng_flags_parse *, struct sip_msg *, enum rtpe_operation *, const char *); - -static int rtpengine_offer_answer(struct sip_msg *msg, const char *flags, enum rtpe_operation op, int more); -static int fixup_set_id(void ** param, int param_no); -static int set_rtpengine_set_f(struct sip_msg * msg, char * str1, char * str2); -static struct rtpp_set * select_rtpp_set(unsigned int id_set); -static struct rtpp_node *select_rtpp_node_new(str, str, int, struct rtpp_node **, int); -static struct rtpp_node *select_rtpp_node_old(str, str, int, enum rtpe_operation); -static struct rtpp_node *select_rtpp_node(str, str, int, struct rtpp_node **, int, enum rtpe_operation); +static int parse_flags(struct ng_flags_parse *, struct sip_msg *, + enum rtpe_operation *, const char *); + +static int rtpengine_offer_answer(struct sip_msg *msg, const char *flags, + enum rtpe_operation op, int more); +static int fixup_set_id(void **param, int param_no); +static int set_rtpengine_set_f(struct sip_msg *msg, char *str1, char *str2); +static struct rtpp_set *select_rtpp_set(unsigned int id_set); +static struct rtpp_node *select_rtpp_node_new( + str, str, int, struct rtpp_node **, int); +static struct rtpp_node *select_rtpp_node_old( + str, str, int, enum rtpe_operation); +static struct rtpp_node *select_rtpp_node( + str, str, int, struct rtpp_node **, int, enum rtpe_operation); static int is_queried_node(struct rtpp_node *, struct rtpp_node **, int); static int build_rtpp_socks(int lmode, int rtest); static char *send_rtpp_command(struct rtpp_node *, bencode_item_t *, int *); -static int get_extra_id(struct sip_msg* msg, str *id_str); +static int get_extra_id(struct sip_msg *msg, str *id_str); -static int rtpengine_set_store(modparam_t type, void * val); -static int rtpengine_add_rtpengine_set(char * rtp_proxies, unsigned int weight, int disabled, unsigned int ticks); +static int rtpengine_set_store(modparam_t type, void *val); +static int rtpengine_add_rtpengine_set(char *rtp_proxies, unsigned int weight, + int disabled, unsigned int ticks); static int mod_init(void); static int child_init(int); @@ -246,12 +255,13 @@ static int get_ip_type(char *str_addr); static int get_ip_scope(char *str_addr); // useful for link-local ipv6 static int bind_force_send_ip(int sock_idx); -static int add_rtpp_node_info(void *ptrs, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list); +static int add_rtpp_node_info( + void *ptrs, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list); static int rtpp_test_ping(struct rtpp_node *node); /* Pseudo-Variables */ static int pv_get_rtpestat_f(struct sip_msg *, pv_param_t *, pv_value_t *); -static int set_rtp_inst_pvar(struct sip_msg *msg, const str * const uri); +static int set_rtp_inst_pvar(struct sip_msg *msg, const str *const uri); static int pv_parse_var(str *inp, pv_elem_t **outp, int *got_any); static int mos_label_stats_parse(struct minmax_mos_label_stats *mmls); static void parse_call_stats(bencode_item_t *, struct sip_msg *); @@ -267,16 +277,16 @@ static int hash_table_tout = 3600; static int hash_table_size = 256; static unsigned int setid_default = DEFAULT_RTPP_SET_ID; -static char ** rtpp_strings=0; -static int rtpp_sets=0; /*used in rtpengine_set_store()*/ +static char **rtpp_strings = 0; +static int rtpp_sets = 0; /*used in rtpengine_set_store()*/ static int rtpp_set_count = 0; static unsigned int current_msg_id = (unsigned int)-1; /* RTPEngine balancing list */ -static struct rtpp_set_head * rtpp_set_list =0; -static struct rtpp_set * active_rtpp_set =0; -static struct rtpp_set * selected_rtpp_set_1 =0; -static struct rtpp_set * selected_rtpp_set_2 =0; -static struct rtpp_set * default_rtpp_set=0; +static struct rtpp_set_head *rtpp_set_list = 0; +static struct rtpp_set *active_rtpp_set = 0; +static struct rtpp_set *selected_rtpp_set_1 = 0; +static struct rtpp_set *selected_rtpp_set_2 = 0; +static struct rtpp_set *default_rtpp_set = 0; static str body_intermediate; @@ -302,12 +312,13 @@ static str media_duration_pvar_str = {NULL, 0}; static pv_spec_t *media_duration_pvar = NULL; #define RTPENGINE_SESS_LIMIT_MSG "Parallel session limit reached" -#define RTPENGINE_SESS_LIMIT_MSG_LEN (sizeof(RTPENGINE_SESS_LIMIT_MSG)-1) +#define RTPENGINE_SESS_LIMIT_MSG_LEN (sizeof(RTPENGINE_SESS_LIMIT_MSG) - 1) #define RTPENGINE_SESS_OUT_OF_PORTS_MSG "Ran out of ports" -#define RTPENGINE_SESS_OUT_OF_PORTS_MSG_LEN (sizeof(RTPENGINE_SESS_OUT_OF_PORTS_MSG)-1) +#define RTPENGINE_SESS_OUT_OF_PORTS_MSG_LEN \ + (sizeof(RTPENGINE_SESS_OUT_OF_PORTS_MSG) - 1) -char* force_send_ip_str=""; +char *force_send_ip_str = ""; int force_send_ip_af = AF_UNSPEC; static str _rtpe_wsapi = STR_NULL; @@ -315,7 +326,8 @@ lwsc_api_t _rtpe_lwscb = {0}; static enum hash_algo_t hash_algo = RTP_HASH_CALLID; -typedef struct rtpp_set_link { +typedef struct rtpp_set_link +{ struct rtpp_set *rset; pv_spec_t *rpv; } rtpp_set_link_t; @@ -326,262 +338,250 @@ static struct tm_binds tmb; static pv_elem_t *extra_id_pv = NULL; -static struct minmax_mos_label_stats global_mos_stats, - side_A_mos_stats, - side_B_mos_stats; +static struct minmax_mos_label_stats global_mos_stats, side_A_mos_stats, + side_B_mos_stats; int got_any_mos_pvs; struct crypto_binds rtpengine_cb; static cmd_export_t cmds[] = { - {"set_rtpengine_set", (cmd_function)set_rtpengine_set_f, 1, - fixup_set_id, 0, - ANY_ROUTE}, - {"set_rtpengine_set", (cmd_function)set_rtpengine_set_f, 2, - fixup_set_id, 0, - ANY_ROUTE}, - {"start_recording", (cmd_function)start_recording_f, 0, - 0, 0, - ANY_ROUTE }, - {"start_recording", (cmd_function)start_recording_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"stop_recording", (cmd_function)stop_recording_f, 0, - 0, 0, - ANY_ROUTE }, - {"stop_recording", (cmd_function)stop_recording_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"block_dtmf", (cmd_function)block_dtmf_f, 0, - 0, 0, - ANY_ROUTE }, - {"unblock_dtmf", (cmd_function)unblock_dtmf_f, 0, - 0, 0, - ANY_ROUTE}, - {"block_media", (cmd_function)block_media_f, 0, - 0, 0, - ANY_ROUTE }, - {"unblock_media", (cmd_function)unblock_media_f, 0, - 0, 0, - ANY_ROUTE}, - {"silence_media", (cmd_function)silence_media_f, 0, - 0, 0, - ANY_ROUTE }, - {"unsilence_media", (cmd_function)unsilence_media_f, 0, - 0, 0, - ANY_ROUTE}, - {"block_dtmf", (cmd_function)block_dtmf_f, 1, - fixup_spve_null, 0, - ANY_ROUTE }, - {"unblock_dtmf", (cmd_function)unblock_dtmf_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"block_media", (cmd_function)block_media_f, 1, - fixup_spve_null, 0, - ANY_ROUTE }, - {"unblock_media", (cmd_function)unblock_media_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"silence_media", (cmd_function)silence_media_f, 1, - fixup_spve_null, 0, - ANY_ROUTE }, - {"unsilence_media", (cmd_function)unsilence_media_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"start_forwarding", (cmd_function)start_forwarding_f, 0, - 0, 0, - ANY_ROUTE }, - {"stop_forwarding", (cmd_function)stop_forwarding_f, 0, - 0, 0, - ANY_ROUTE}, - {"start_forwarding", (cmd_function)start_forwarding_f, 1, - fixup_spve_null, 0, - ANY_ROUTE }, - {"stop_forwarding", (cmd_function)stop_forwarding_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"play_media", (cmd_function)play_media_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"stop_media", (cmd_function)stop_media_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"stop_media", (cmd_function)stop_media_f, 0, - 0, 0, - ANY_ROUTE}, - {"play_dtmf", (cmd_function)play_dtmf_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"rtpengine_offer", (cmd_function)rtpengine_offer1_f, 0, - 0, 0, - ANY_ROUTE}, - {"rtpengine_offer", (cmd_function)rtpengine_offer1_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"rtpengine_answer", (cmd_function)rtpengine_answer1_f, 0, - 0, 0, - ANY_ROUTE}, - {"rtpengine_answer", (cmd_function)rtpengine_answer1_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"rtpengine_info", (cmd_function)rtpengine_info1_f, 0, - 0, 0, - ANY_ROUTE}, - {"rtpengine_info", (cmd_function)rtpengine_info1_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"rtpengine_manage", (cmd_function)rtpengine_manage1_f, 0, - 0, 0, - ANY_ROUTE}, - {"rtpengine_manage", (cmd_function)rtpengine_manage1_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"rtpengine_delete", (cmd_function)rtpengine_delete1_f, 0, - 0, 0, - ANY_ROUTE}, - {"rtpengine_delete", (cmd_function)rtpengine_delete1_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"rtpengine_query", (cmd_function)rtpengine_query1_f, 0, - 0, 0, - ANY_ROUTE}, - {"rtpengine_query", (cmd_function)rtpengine_query1_f, 1, - fixup_spve_null, 0, - ANY_ROUTE}, - {"rtpengine_query_v", (cmd_function)w_rtpengine_query_v, 2, - fixup_rtpengine_query_v, fixup_free_rtpengine_query_v, - ANY_ROUTE}, - {0, 0, 0, 0, 0, 0} -}; + {"set_rtpengine_set", (cmd_function)set_rtpengine_set_f, 1, + fixup_set_id, 0, ANY_ROUTE}, + {"set_rtpengine_set", (cmd_function)set_rtpengine_set_f, 2, + fixup_set_id, 0, ANY_ROUTE}, + {"start_recording", (cmd_function)start_recording_f, 0, 0, 0, + ANY_ROUTE}, + {"start_recording", (cmd_function)start_recording_f, 1, fixup_spve_null, + 0, ANY_ROUTE}, + {"stop_recording", (cmd_function)stop_recording_f, 0, 0, 0, ANY_ROUTE}, + {"stop_recording", (cmd_function)stop_recording_f, 1, fixup_spve_null, + 0, ANY_ROUTE}, + {"block_dtmf", (cmd_function)block_dtmf_f, 0, 0, 0, ANY_ROUTE}, + {"unblock_dtmf", (cmd_function)unblock_dtmf_f, 0, 0, 0, ANY_ROUTE}, + {"block_media", (cmd_function)block_media_f, 0, 0, 0, ANY_ROUTE}, + {"unblock_media", (cmd_function)unblock_media_f, 0, 0, 0, ANY_ROUTE}, + {"silence_media", (cmd_function)silence_media_f, 0, 0, 0, ANY_ROUTE}, + {"unsilence_media", (cmd_function)unsilence_media_f, 0, 0, 0, + ANY_ROUTE}, + {"block_dtmf", (cmd_function)block_dtmf_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"unblock_dtmf", (cmd_function)unblock_dtmf_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"block_media", (cmd_function)block_media_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"unblock_media", (cmd_function)unblock_media_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"silence_media", (cmd_function)silence_media_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"unsilence_media", (cmd_function)unsilence_media_f, 1, fixup_spve_null, + 0, ANY_ROUTE}, + {"start_forwarding", (cmd_function)start_forwarding_f, 0, 0, 0, + ANY_ROUTE}, + {"stop_forwarding", (cmd_function)stop_forwarding_f, 0, 0, 0, + ANY_ROUTE}, + {"start_forwarding", (cmd_function)start_forwarding_f, 1, + fixup_spve_null, 0, ANY_ROUTE}, + {"stop_forwarding", (cmd_function)stop_forwarding_f, 1, fixup_spve_null, + 0, ANY_ROUTE}, + {"play_media", (cmd_function)play_media_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"stop_media", (cmd_function)stop_media_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"stop_media", (cmd_function)stop_media_f, 0, 0, 0, ANY_ROUTE}, + {"play_dtmf", (cmd_function)play_dtmf_f, 1, fixup_spve_null, 0, + ANY_ROUTE}, + {"rtpengine_offer", (cmd_function)rtpengine_offer1_f, 0, 0, 0, + ANY_ROUTE}, + {"rtpengine_offer", (cmd_function)rtpengine_offer1_f, 1, + fixup_spve_null, 0, ANY_ROUTE}, + {"rtpengine_answer", (cmd_function)rtpengine_answer1_f, 0, 0, 0, + ANY_ROUTE}, + {"rtpengine_answer", (cmd_function)rtpengine_answer1_f, 1, + fixup_spve_null, 0, ANY_ROUTE}, + {"rtpengine_info", (cmd_function)rtpengine_info1_f, 0, 0, 0, ANY_ROUTE}, + {"rtpengine_info", (cmd_function)rtpengine_info1_f, 1, fixup_spve_null, + 0, ANY_ROUTE}, + {"rtpengine_manage", (cmd_function)rtpengine_manage1_f, 0, 0, 0, + ANY_ROUTE}, + {"rtpengine_manage", (cmd_function)rtpengine_manage1_f, 1, + fixup_spve_null, 0, ANY_ROUTE}, + {"rtpengine_delete", (cmd_function)rtpengine_delete1_f, 0, 0, 0, + ANY_ROUTE}, + {"rtpengine_delete", (cmd_function)rtpengine_delete1_f, 1, + fixup_spve_null, 0, ANY_ROUTE}, + {"rtpengine_query", (cmd_function)rtpengine_query1_f, 0, 0, 0, + ANY_ROUTE}, + {"rtpengine_query", (cmd_function)rtpengine_query1_f, 1, + fixup_spve_null, 0, ANY_ROUTE}, + {"rtpengine_query_v", (cmd_function)w_rtpengine_query_v, 2, + fixup_rtpengine_query_v, fixup_free_rtpengine_query_v, + ANY_ROUTE}, + {0, 0, 0, 0, 0, 0}}; static pv_export_t mod_pvs[] = { - {{"rtpstat", (sizeof("rtpstat")-1)}, /* RTP-Statistics */ - PVT_OTHER, pv_get_rtpestat_f, 0, 0, 0, 0, 0}, - {{"rtpestat", (sizeof("rtpestat")-1)}, /* RTP-Statistics */ - PVT_OTHER, pv_get_rtpestat_f, 0, 0, 0, 0, 0}, - {{0, 0}, 0, 0, 0, 0, 0, 0, 0} -}; + {{"rtpstat", (sizeof("rtpstat") - 1)}, /* RTP-Statistics */ + PVT_OTHER, pv_get_rtpestat_f, 0, 0, 0, 0, 0}, + {{"rtpestat", (sizeof("rtpestat") - 1)}, /* RTP-Statistics */ + PVT_OTHER, pv_get_rtpestat_f, 0, 0, 0, 0, 0}, + {{0, 0}, 0, 0, 0, 0, 0, 0, 0}}; static param_export_t params[] = { - {"rtpengine_sock", PARAM_STRING|USE_FUNC_PARAM, - (void*)rtpengine_set_store }, - {"rtpengine_disable_tout",INT_PARAM, &default_rtpengine_cfg.rtpengine_disable_tout }, - {"aggressive_redetection",INT_PARAM, &default_rtpengine_cfg.aggressive_redetection }, - {"rtpengine_retr", INT_PARAM, &default_rtpengine_cfg.rtpengine_retr }, - {"queried_nodes_limit", INT_PARAM, &default_rtpengine_cfg.queried_nodes_limit }, - {"rtpengine_tout_ms", INT_PARAM, &default_rtpengine_cfg.rtpengine_tout_ms }, - {"rtpengine_allow_op", INT_PARAM, &rtpengine_allow_op }, - {"control_cmd_tos", INT_PARAM, &control_cmd_tos }, - {"db_url", PARAM_STR, &rtpp_db_url }, - {"table_name", PARAM_STR, &rtpp_table_name }, - {"setid_col", PARAM_STR, &rtpp_setid_col }, - {"url_col", PARAM_STR, &rtpp_url_col }, - {"weight_col", PARAM_STR, &rtpp_weight_col }, - {"disabled_col", PARAM_STR, &rtpp_disabled_col }, - {"extra_id_pv", PARAM_STR, &extra_id_pv_param }, - {"setid_avp", PARAM_STRING, &setid_avp_param }, - {"force_send_interface", PARAM_STRING, &force_send_ip_str }, - {"rtp_inst_pvar", PARAM_STR, &rtp_inst_pv_param }, - {"write_sdp_pv", PARAM_STR, &write_sdp_pvar_str }, - {"read_sdp_pv", PARAM_STR, &read_sdp_pvar_str }, - {"hash_table_tout", INT_PARAM, &hash_table_tout }, - {"hash_table_size", INT_PARAM, &hash_table_size }, - {"setid_default", INT_PARAM, &setid_default }, - {"media_duration", PARAM_STR, &media_duration_pvar_str}, - {"hash_algo", INT_PARAM, &hash_algo}, - - /* MOS stats output */ - /* global averages */ - {"mos_min_pv", PARAM_STR, &global_mos_stats.min.mos_param }, - {"mos_min_at_pv", PARAM_STR, &global_mos_stats.min.at_param }, - {"mos_min_packetloss_pv", PARAM_STR, &global_mos_stats.min.packetloss_param }, - {"mos_min_jitter_pv", PARAM_STR, &global_mos_stats.min.jitter_param }, - {"mos_min_roundtrip_pv", PARAM_STR, &global_mos_stats.min.roundtrip_param }, - {"mos_min_roundtrip_leg_pv", PARAM_STR, &global_mos_stats.min.roundtrip_leg_param }, - {"mos_max_pv", PARAM_STR, &global_mos_stats.max.mos_param }, - {"mos_max_at_pv", PARAM_STR, &global_mos_stats.max.at_param }, - {"mos_max_packetloss_pv", PARAM_STR, &global_mos_stats.max.packetloss_param }, - {"mos_max_jitter_pv", PARAM_STR, &global_mos_stats.max.jitter_param }, - {"mos_max_roundtrip_pv", PARAM_STR, &global_mos_stats.max.roundtrip_param }, - {"mos_max_roundtrip_leg_pv", PARAM_STR, &global_mos_stats.max.roundtrip_leg_param }, - {"mos_average_pv", PARAM_STR, &global_mos_stats.average.mos_param }, - {"mos_average_packetloss_pv", PARAM_STR, &global_mos_stats.average.packetloss_param }, - {"mos_average_jitter_pv", PARAM_STR, &global_mos_stats.average.jitter_param }, - {"mos_average_roundtrip_pv", PARAM_STR, &global_mos_stats.average.roundtrip_param }, - {"mos_average_roundtrip_leg_pv", PARAM_STR, &global_mos_stats.average.roundtrip_leg_param }, - {"mos_average_samples_pv", PARAM_STR, &global_mos_stats.average.samples_param }, - - /* designated side A */ - {"mos_A_label_pv", PARAM_STR, &side_A_mos_stats.label_param }, - {"mos_min_A_pv", PARAM_STR, &side_A_mos_stats.min.mos_param }, - {"mos_min_at_A_pv", PARAM_STR, &side_A_mos_stats.min.at_param }, - {"mos_min_packetloss_A_pv", PARAM_STR, &side_A_mos_stats.min.packetloss_param }, - {"mos_min_jitter_A_pv", PARAM_STR, &side_A_mos_stats.min.jitter_param }, - {"mos_min_roundtrip_A_pv", PARAM_STR, &side_A_mos_stats.min.roundtrip_param }, - {"mos_min_roundtrip_leg_A_pv", PARAM_STR, &side_A_mos_stats.min.roundtrip_leg_param }, - {"mos_max_A_pv", PARAM_STR, &side_A_mos_stats.max.mos_param }, - {"mos_max_at_A_pv", PARAM_STR, &side_A_mos_stats.max.at_param }, - {"mos_max_packetloss_A_pv", PARAM_STR, &side_A_mos_stats.max.packetloss_param }, - {"mos_max_jitter_A_pv", PARAM_STR, &side_A_mos_stats.max.jitter_param }, - {"mos_max_roundtrip_A_pv", PARAM_STR, &side_A_mos_stats.max.roundtrip_param }, - {"mos_max_roundtrip_leg_A_pv", PARAM_STR, &side_A_mos_stats.max.roundtrip_leg_param }, - {"mos_average_A_pv", PARAM_STR, &side_A_mos_stats.average.mos_param }, - {"mos_average_packetloss_A_pv", PARAM_STR, &side_A_mos_stats.average.packetloss_param }, - {"mos_average_jitter_A_pv", PARAM_STR, &side_A_mos_stats.average.jitter_param }, - {"mos_average_roundtrip_A_pv", PARAM_STR, &side_A_mos_stats.average.roundtrip_param }, - {"mos_average_roundtrip_leg_A_pv", PARAM_STR, &side_A_mos_stats.average.roundtrip_leg_param }, - {"mos_average_samples_A_pv", PARAM_STR, &side_A_mos_stats.average.samples_param }, - - /* designated side B */ - {"mos_B_label_pv", PARAM_STR, &side_B_mos_stats.label_param }, - {"mos_min_B_pv", PARAM_STR, &side_B_mos_stats.min.mos_param }, - {"mos_min_at_B_pv", PARAM_STR, &side_B_mos_stats.min.at_param }, - {"mos_min_packetloss_B_pv", PARAM_STR, &side_B_mos_stats.min.packetloss_param }, - {"mos_min_jitter_B_pv", PARAM_STR, &side_B_mos_stats.min.jitter_param }, - {"mos_min_roundtrip_B_pv", PARAM_STR, &side_B_mos_stats.min.roundtrip_param }, - {"mos_min_roundtrip_leg_B_pv", PARAM_STR, &side_B_mos_stats.min.roundtrip_leg_param }, - {"mos_max_B_pv", PARAM_STR, &side_B_mos_stats.max.mos_param }, - {"mos_max_at_B_pv", PARAM_STR, &side_B_mos_stats.max.at_param }, - {"mos_max_packetloss_B_pv", PARAM_STR, &side_B_mos_stats.max.packetloss_param }, - {"mos_max_jitter_B_pv", PARAM_STR, &side_B_mos_stats.max.jitter_param }, - {"mos_max_roundtrip_B_pv", PARAM_STR, &side_B_mos_stats.max.roundtrip_param }, - {"mos_max_roundtrip_leg_B_pv", PARAM_STR, &side_B_mos_stats.max.roundtrip_leg_param }, - {"mos_average_B_pv", PARAM_STR, &side_B_mos_stats.average.mos_param }, - {"mos_average_packetloss_B_pv", PARAM_STR, &side_B_mos_stats.average.packetloss_param }, - {"mos_average_jitter_B_pv", PARAM_STR, &side_B_mos_stats.average.jitter_param }, - {"mos_average_roundtrip_B_pv", PARAM_STR, &side_B_mos_stats.average.roundtrip_param }, - {"mos_average_roundtrip_leg_B_pv", PARAM_STR, &side_B_mos_stats.average.roundtrip_leg_param }, - {"mos_average_samples_B_pv", PARAM_STR, &side_B_mos_stats.average.samples_param }, - - {"wsapi", PARAM_STR, &_rtpe_wsapi }, - - {0, 0, 0} -}; + {"rtpengine_sock", PARAM_STRING | USE_FUNC_PARAM, + (void *)rtpengine_set_store}, + {"rtpengine_disable_tout", INT_PARAM, + &default_rtpengine_cfg.rtpengine_disable_tout}, + {"aggressive_redetection", INT_PARAM, + &default_rtpengine_cfg.aggressive_redetection}, + {"rtpengine_retr", INT_PARAM, &default_rtpengine_cfg.rtpengine_retr}, + {"queried_nodes_limit", INT_PARAM, + &default_rtpengine_cfg.queried_nodes_limit}, + {"rtpengine_tout_ms", INT_PARAM, + &default_rtpengine_cfg.rtpengine_tout_ms}, + {"rtpengine_allow_op", INT_PARAM, &rtpengine_allow_op}, + {"control_cmd_tos", INT_PARAM, &control_cmd_tos}, + {"db_url", PARAM_STR, &rtpp_db_url}, + {"table_name", PARAM_STR, &rtpp_table_name}, + {"setid_col", PARAM_STR, &rtpp_setid_col}, + {"url_col", PARAM_STR, &rtpp_url_col}, + {"weight_col", PARAM_STR, &rtpp_weight_col}, + {"disabled_col", PARAM_STR, &rtpp_disabled_col}, + {"extra_id_pv", PARAM_STR, &extra_id_pv_param}, + {"setid_avp", PARAM_STRING, &setid_avp_param}, + {"force_send_interface", PARAM_STRING, &force_send_ip_str}, + {"rtp_inst_pvar", PARAM_STR, &rtp_inst_pv_param}, + {"write_sdp_pv", PARAM_STR, &write_sdp_pvar_str}, + {"read_sdp_pv", PARAM_STR, &read_sdp_pvar_str}, + {"hash_table_tout", INT_PARAM, &hash_table_tout}, + {"hash_table_size", INT_PARAM, &hash_table_size}, + {"setid_default", INT_PARAM, &setid_default}, + {"media_duration", PARAM_STR, &media_duration_pvar_str}, + {"hash_algo", INT_PARAM, &hash_algo}, + + /* MOS stats output */ + /* global averages */ + {"mos_min_pv", PARAM_STR, &global_mos_stats.min.mos_param}, + {"mos_min_at_pv", PARAM_STR, &global_mos_stats.min.at_param}, + {"mos_min_packetloss_pv", PARAM_STR, + &global_mos_stats.min.packetloss_param}, + {"mos_min_jitter_pv", PARAM_STR, &global_mos_stats.min.jitter_param}, + {"mos_min_roundtrip_pv", PARAM_STR, + &global_mos_stats.min.roundtrip_param}, + {"mos_min_roundtrip_leg_pv", PARAM_STR, + &global_mos_stats.min.roundtrip_leg_param}, + {"mos_max_pv", PARAM_STR, &global_mos_stats.max.mos_param}, + {"mos_max_at_pv", PARAM_STR, &global_mos_stats.max.at_param}, + {"mos_max_packetloss_pv", PARAM_STR, + &global_mos_stats.max.packetloss_param}, + {"mos_max_jitter_pv", PARAM_STR, &global_mos_stats.max.jitter_param}, + {"mos_max_roundtrip_pv", PARAM_STR, + &global_mos_stats.max.roundtrip_param}, + {"mos_max_roundtrip_leg_pv", PARAM_STR, + &global_mos_stats.max.roundtrip_leg_param}, + {"mos_average_pv", PARAM_STR, &global_mos_stats.average.mos_param}, + {"mos_average_packetloss_pv", PARAM_STR, + &global_mos_stats.average.packetloss_param}, + {"mos_average_jitter_pv", PARAM_STR, + &global_mos_stats.average.jitter_param}, + {"mos_average_roundtrip_pv", PARAM_STR, + &global_mos_stats.average.roundtrip_param}, + {"mos_average_roundtrip_leg_pv", PARAM_STR, + &global_mos_stats.average.roundtrip_leg_param}, + {"mos_average_samples_pv", PARAM_STR, + &global_mos_stats.average.samples_param}, + + /* designated side A */ + {"mos_A_label_pv", PARAM_STR, &side_A_mos_stats.label_param}, + {"mos_min_A_pv", PARAM_STR, &side_A_mos_stats.min.mos_param}, + {"mos_min_at_A_pv", PARAM_STR, &side_A_mos_stats.min.at_param}, + {"mos_min_packetloss_A_pv", PARAM_STR, + &side_A_mos_stats.min.packetloss_param}, + {"mos_min_jitter_A_pv", PARAM_STR, &side_A_mos_stats.min.jitter_param}, + {"mos_min_roundtrip_A_pv", PARAM_STR, + &side_A_mos_stats.min.roundtrip_param}, + {"mos_min_roundtrip_leg_A_pv", PARAM_STR, + &side_A_mos_stats.min.roundtrip_leg_param}, + {"mos_max_A_pv", PARAM_STR, &side_A_mos_stats.max.mos_param}, + {"mos_max_at_A_pv", PARAM_STR, &side_A_mos_stats.max.at_param}, + {"mos_max_packetloss_A_pv", PARAM_STR, + &side_A_mos_stats.max.packetloss_param}, + {"mos_max_jitter_A_pv", PARAM_STR, &side_A_mos_stats.max.jitter_param}, + {"mos_max_roundtrip_A_pv", PARAM_STR, + &side_A_mos_stats.max.roundtrip_param}, + {"mos_max_roundtrip_leg_A_pv", PARAM_STR, + &side_A_mos_stats.max.roundtrip_leg_param}, + {"mos_average_A_pv", PARAM_STR, &side_A_mos_stats.average.mos_param}, + {"mos_average_packetloss_A_pv", PARAM_STR, + &side_A_mos_stats.average.packetloss_param}, + {"mos_average_jitter_A_pv", PARAM_STR, + &side_A_mos_stats.average.jitter_param}, + {"mos_average_roundtrip_A_pv", PARAM_STR, + &side_A_mos_stats.average.roundtrip_param}, + {"mos_average_roundtrip_leg_A_pv", PARAM_STR, + &side_A_mos_stats.average.roundtrip_leg_param}, + {"mos_average_samples_A_pv", PARAM_STR, + &side_A_mos_stats.average.samples_param}, + + /* designated side B */ + {"mos_B_label_pv", PARAM_STR, &side_B_mos_stats.label_param}, + {"mos_min_B_pv", PARAM_STR, &side_B_mos_stats.min.mos_param}, + {"mos_min_at_B_pv", PARAM_STR, &side_B_mos_stats.min.at_param}, + {"mos_min_packetloss_B_pv", PARAM_STR, + &side_B_mos_stats.min.packetloss_param}, + {"mos_min_jitter_B_pv", PARAM_STR, &side_B_mos_stats.min.jitter_param}, + {"mos_min_roundtrip_B_pv", PARAM_STR, + &side_B_mos_stats.min.roundtrip_param}, + {"mos_min_roundtrip_leg_B_pv", PARAM_STR, + &side_B_mos_stats.min.roundtrip_leg_param}, + {"mos_max_B_pv", PARAM_STR, &side_B_mos_stats.max.mos_param}, + {"mos_max_at_B_pv", PARAM_STR, &side_B_mos_stats.max.at_param}, + {"mos_max_packetloss_B_pv", PARAM_STR, + &side_B_mos_stats.max.packetloss_param}, + {"mos_max_jitter_B_pv", PARAM_STR, &side_B_mos_stats.max.jitter_param}, + {"mos_max_roundtrip_B_pv", PARAM_STR, + &side_B_mos_stats.max.roundtrip_param}, + {"mos_max_roundtrip_leg_B_pv", PARAM_STR, + &side_B_mos_stats.max.roundtrip_leg_param}, + {"mos_average_B_pv", PARAM_STR, &side_B_mos_stats.average.mos_param}, + {"mos_average_packetloss_B_pv", PARAM_STR, + &side_B_mos_stats.average.packetloss_param}, + {"mos_average_jitter_B_pv", PARAM_STR, + &side_B_mos_stats.average.jitter_param}, + {"mos_average_roundtrip_B_pv", PARAM_STR, + &side_B_mos_stats.average.roundtrip_param}, + {"mos_average_roundtrip_leg_B_pv", PARAM_STR, + &side_B_mos_stats.average.roundtrip_leg_param}, + {"mos_average_samples_B_pv", PARAM_STR, + &side_B_mos_stats.average.samples_param}, + + {"wsapi", PARAM_STR, &_rtpe_wsapi}, + + {0, 0, 0}}; struct module_exports exports = { - "rtpengine", /* module name */ - DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, /* cmd (cfg function) exports */ - params, /* param exports */ - 0, /* RPC method exports */ - mod_pvs, /* pseudo-variables exports */ - 0, /* response handling function */ - mod_init, /* module init function */ - child_init, /* per-child init function */ - mod_destroy /* module destroy function */ + "rtpengine", /* module name */ + DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, /* cmd (cfg function) exports */ + params, /* param exports */ + 0, /* RPC method exports */ + mod_pvs, /* pseudo-variables exports */ + 0, /* response handling function */ + mod_init, /* module init function */ + child_init, /* per-child init function */ + mod_destroy /* module destroy function */ }; /* check if the node is already queried */ -static int is_queried_node(struct rtpp_node *node, struct rtpp_node **queried_nodes_ptr, int queried_nodes) +static int is_queried_node(struct rtpp_node *node, + struct rtpp_node **queried_nodes_ptr, int queried_nodes) { int i; - if (!queried_nodes_ptr) { + if(!queried_nodes_ptr) { return 0; } - for (i = 0; i < queried_nodes; i++) { - if (node == queried_nodes_ptr[i]) { + for(i = 0; i < queried_nodes; i++) { + if(node == queried_nodes_ptr[i]) { return 1; } } @@ -619,7 +619,7 @@ int rtpengine_delete_node_all() { struct rtpp_set *rtpp_list; - if (!rtpp_set_list) { + if(!rtpp_set_list) { return 1; } @@ -644,17 +644,18 @@ static int get_ip_type(char *str_addr) hint.ai_flags = AI_NUMERICHOST; ret = getaddrinfo(str_addr, NULL, &hint, &info); - if (ret) { + if(ret) { /* Invalid ip addinfos */ return -1; } if(info->ai_family == AF_INET) { LM_DBG("%s is an ipv4 addinfos\n", str_addr); - } else if (info->ai_family == AF_INET6) { + } else if(info->ai_family == AF_INET6) { LM_DBG("%s is an ipv6 addinfos\n", str_addr); } else { - LM_DBG("%s is an unknown addinfos format AF=%d\n",str_addr, info->ai_family); + LM_DBG("%s is an unknown addinfos format AF=%d\n", str_addr, + info->ai_family); freeaddrinfo(info); return -1; } @@ -674,30 +675,30 @@ static int get_ip_scope(char *str_addr) char str_if_ip[NI_MAXHOST]; int ret = -1; - if (getifaddrs(&ifaddr) == -1) { + if(getifaddrs(&ifaddr) == -1) { LM_ERR("getifaddrs() failed: %s\n", gai_strerror(ret)); return -1; } - for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { in6 = (struct sockaddr_in6 *)ifa->ifa_addr; - if (ifa->ifa_addr == NULL) + if(ifa->ifa_addr == NULL) continue; - if (ifa->ifa_addr->sa_family != AF_INET6) + if(ifa->ifa_addr->sa_family != AF_INET6) continue; - ret = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), - str_if_ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); - if (ret != 0) { + ret = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), str_if_ip, + NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if(ret != 0) { LM_ERR("getnameinfo() failed: %s\n", gai_strerror(ret)); return -1; } - if (strstr(str_if_ip, str_addr)) { - LM_INFO("dev: %-8s address: <%s> scope %d\n", - ifa->ifa_name, str_if_ip, in6->sin6_scope_id); + if(strstr(str_if_ip, str_addr)) { + LM_INFO("dev: %-8s address: <%s> scope %d\n", ifa->ifa_name, + str_if_ip, in6->sin6_scope_id); ret = in6->sin6_scope_id; break; } @@ -718,23 +719,26 @@ static int bind_force_send_ip(int sock_idx) socklen_t sock_len = sizeof(struct sockaddr); int ret, scope; - switch (force_send_ip_af) { + switch(force_send_ip_af) { case AF_INET: memset(&ip4addr, 0, sizeof(ip4addr)); ip4addr.sin_family = AF_INET; ip4addr.sin_port = htons(0); - if (inet_pton(AF_INET, force_send_ip_str, &ip4addr.sin_addr) != 1) { + if(inet_pton(AF_INET, force_send_ip_str, &ip4addr.sin_addr) != 1) { LM_ERR("failed to parse IPv4 address %s\n", force_send_ip_str); return -1; } - if (bind(rtpp_socks[sock_idx], (struct sockaddr*)&ip4addr, sizeof(ip4addr)) < 0) { + if(bind(rtpp_socks[sock_idx], (struct sockaddr *)&ip4addr, + sizeof(ip4addr)) + < 0) { LM_ERR("can't bind socket to required ipv4 interface\n"); return -1; } memset(&tmp, 0, sizeof(tmp)); - if (getsockname(rtpp_socks[sock_idx], (struct sockaddr *) &tmp, &sock_len)) + if(getsockname(rtpp_socks[sock_idx], (struct sockaddr *)&tmp, + &sock_len)) LM_ERR("could not determine local socket name\n"); else { inet_ntop(AF_INET, &tmp.sin_addr, str_addr, INET_ADDRSTRLEN); @@ -744,7 +748,7 @@ static int bind_force_send_ip(int sock_idx) break; case AF_INET6: - if ((scope = get_ip_scope(force_send_ip_str)) < 0) { + if((scope = get_ip_scope(force_send_ip_str)) < 0) { LM_ERR("can't get the ipv6 interface scope\n"); return -1; } @@ -752,23 +756,29 @@ static int bind_force_send_ip(int sock_idx) ip6addr.sin6_family = AF_INET6; ip6addr.sin6_port = htons(0); ip6addr.sin6_scope_id = scope; - if (inet_pton(AF_INET6, force_send_ip_str, &ip6addr.sin6_addr) != 1) { + if(inet_pton(AF_INET6, force_send_ip_str, &ip6addr.sin6_addr) + != 1) { LM_ERR("failed to parse IPv6 address %s\n", force_send_ip_str); return -1; } - if ((ret = bind(rtpp_socks[sock_idx], (struct sockaddr*)&ip6addr, sizeof(ip6addr))) < 0) { + if((ret = bind(rtpp_socks[sock_idx], (struct sockaddr *)&ip6addr, + sizeof(ip6addr))) + < 0) { LM_ERR("can't bind socket to required ipv6 interface\n"); LM_ERR("ret=%d errno=%d\n", ret, errno); return -1; } memset(&tmp6, 0, sizeof(tmp6)); - if (getsockname(rtpp_socks[sock_idx], (struct sockaddr *) &tmp6, &sock_len)) + if(getsockname(rtpp_socks[sock_idx], (struct sockaddr *)&tmp6, + &sock_len)) LM_ERR("could not determine local socket name\n"); else { - inet_ntop(AF_INET6, &tmp6.sin6_addr, str_addr6, INET6_ADDRSTRLEN); - LM_DBG("Binding on ipv6 %s:%d\n", str_addr6, ntohs(tmp6.sin6_port)); + inet_ntop( + AF_INET6, &tmp6.sin6_addr, str_addr6, INET6_ADDRSTRLEN); + LM_DBG("Binding on ipv6 %s:%d\n", str_addr6, + ntohs(tmp6.sin6_port)); } break; @@ -781,24 +791,27 @@ static int bind_force_send_ip(int sock_idx) return 0; } -static inline int str_cmp(const str *a , const str *b) { - return ! (a->len == b->len && ! strncmp(a->s, b->s, a->len)); +static inline int str_cmp(const str *a, const str *b) +{ + return !(a->len == b->len && !strncmp(a->s, b->s, a->len)); } -static inline int str_eq(const str *p, const char *q) { +static inline int str_eq(const str *p, const char *q) +{ int l = strlen(q); - if (p->len != l) + if(p->len != l) return 0; - if (memcmp(p->s, q, l)) + if(memcmp(p->s, q, l)) return 0; return 1; } -static inline int str_prefix(const str *p, const char *q, str *out) { +static inline int str_prefix(const str *p, const char *q, str *out) +{ int l = strlen(q); - if (p->len < l) + if(p->len < l) return 0; - if (memcmp(p->s, q, l)) + if(memcmp(p->s, q, l)) return 0; *out = *p; out->s += l; @@ -806,19 +819,21 @@ static inline int str_prefix(const str *p, const char *q, str *out) { return 1; } /* handle either "foo-bar" or "foo=bar" from flags */ -static inline int str_key_val_prefix(const str *p, const char *q, const str *v, str *out) { - if (str_eq(p, q)) { +static inline int str_key_val_prefix( + const str *p, const char *q, const str *v, str *out) +{ + if(str_eq(p, q)) { if(!v->s || !v->len) return 0; *out = *v; return 1; } - if (!str_prefix(p, q, out)) + if(!str_prefix(p, q, out)) return 0; - if (out->len < 2) + if(out->len < 2) return 0; - if (*out->s != '-') + if(*out->s != '-') return 0; out->s++; out->len--; @@ -826,26 +841,28 @@ static inline int str_key_val_prefix(const str *p, const char *q, const str *v, } -static int rtpengine_set_store(modparam_t type, void * val){ +static int rtpengine_set_store(modparam_t type, void *val) +{ - char * p; + char *p; int len; - p = (char* )val; + p = (char *)val; - if(p==0 || *p=='\0'){ + if(p == 0 || *p == '\0') { return 0; } - if(rtpp_sets==0){ - rtpp_strings = (char**)pkg_malloc(sizeof(char*)); - if(!rtpp_strings){ + if(rtpp_sets == 0) { + rtpp_strings = (char **)pkg_malloc(sizeof(char *)); + if(!rtpp_strings) { LM_ERR("no pkg memory left\n"); return -1; } - } else {/*realloc to make room for the current set*/ - rtpp_strings = (char**)pkg_reallocxf(rtpp_strings, (rtpp_sets+1)* sizeof(char*)); - if(!rtpp_strings){ + } else { /*realloc to make room for the current set*/ + rtpp_strings = (char **)pkg_reallocxf( + rtpp_strings, (rtpp_sets + 1) * sizeof(char *)); + if(!rtpp_strings) { LM_ERR("no pkg memory left\n"); return -1; } @@ -853,9 +870,9 @@ static int rtpengine_set_store(modparam_t type, void * val){ /*allocate for the current set of urls*/ len = strlen(p); - rtpp_strings[rtpp_sets] = (char*)pkg_malloc((len+1)*sizeof(char)); + rtpp_strings[rtpp_sets] = (char *)pkg_malloc((len + 1) * sizeof(char)); - if(!rtpp_strings[rtpp_sets]){ + if(!rtpp_strings[rtpp_sets]) { LM_ERR("no pkg memory left\n"); return -1; } @@ -871,14 +888,14 @@ struct rtpp_node *get_rtpp_node(struct rtpp_set *rtpp_list, str *url) { struct rtpp_node *rtpp_node; - if (rtpp_list == NULL) { + if(rtpp_list == NULL) { return NULL; } lock_get(rtpp_list->rset_lock); rtpp_node = rtpp_list->rn_first; - while (rtpp_node) { - if (str_cmp(&rtpp_node->rn_url, url) == 0) { + while(rtpp_node) { + if(str_cmp(&rtpp_node->rn_url, url) == 0) { lock_release(rtpp_list->rset_lock); return rtpp_node; } @@ -891,7 +908,7 @@ struct rtpp_node *get_rtpp_node(struct rtpp_set *rtpp_list, str *url) struct rtpp_set *get_rtpp_set(unsigned int set_id) { - struct rtpp_set * rtpp_list; + struct rtpp_set *rtpp_list; unsigned int my_current_id = 0; int new_list; @@ -899,59 +916,53 @@ struct rtpp_set *get_rtpp_set(unsigned int set_id) /*search for the current_id*/ lock_get(rtpp_set_list->rset_head_lock); rtpp_list = rtpp_set_list ? rtpp_set_list->rset_first : 0; - while (rtpp_list != 0 && rtpp_list->id_set!=my_current_id) + while(rtpp_list != 0 && rtpp_list->id_set != my_current_id) rtpp_list = rtpp_list->rset_next; - if (rtpp_list==NULL) - { /*if a new id_set : add a new set of rtpp*/ + if(rtpp_list == NULL) { /*if a new id_set : add a new set of rtpp*/ rtpp_list = shm_malloc(sizeof(struct rtpp_set)); - if(!rtpp_list) - { + if(!rtpp_list) { lock_release(rtpp_set_list->rset_head_lock); - LM_ERR("no shm memory left to create new rtpengine set %u\n", my_current_id); + LM_ERR("no shm memory left to create new rtpengine set %u\n", + my_current_id); return NULL; } memset(rtpp_list, 0, sizeof(struct rtpp_set)); rtpp_list->id_set = my_current_id; rtpp_list->rset_lock = lock_alloc(); - if (!rtpp_list->rset_lock) { + if(!rtpp_list->rset_lock) { lock_release(rtpp_set_list->rset_head_lock); LM_ERR("no shm memory left to create rtpengine set lock\n"); shm_free(rtpp_list); rtpp_list = NULL; return NULL; } - if (lock_init(rtpp_list->rset_lock) == 0) { + if(lock_init(rtpp_list->rset_lock) == 0) { lock_release(rtpp_set_list->rset_head_lock); LM_ERR("could not init rtpengine set lock\n"); - lock_dealloc((void*)rtpp_list->rset_lock); + lock_dealloc((void *)rtpp_list->rset_lock); rtpp_list->rset_lock = NULL; shm_free(rtpp_list); rtpp_list = NULL; return NULL; } new_list = 1; - } - else { + } else { new_list = 0; } - if (new_list) - { + if(new_list) { /*update the list of set info*/ - if (!rtpp_set_list->rset_first) - { + if(!rtpp_set_list->rset_first) { rtpp_set_list->rset_first = rtpp_list; - } - else - { + } else { rtpp_set_list->rset_last->rset_next = rtpp_list; } rtpp_set_list->rset_last = rtpp_list; rtpp_set_count++; - if(my_current_id == setid_default){ + if(my_current_id == setid_default) { default_rtpp_set = rtpp_list; } } @@ -962,7 +973,7 @@ struct rtpp_set *get_rtpp_set(unsigned int set_id) int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, - unsigned int weight, int disabled, unsigned int ticks, int isDB) + unsigned int weight, int disabled, unsigned int ticks, int isDB) { /* Make rtpengine instances list. */ char *p, *p1, *p2, *plim; @@ -976,23 +987,23 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, for(;;) { local_weight = weight; - while (*p && isspace((int)*p)) + while(*p && isspace((int)*p)) ++p; - if (p >= plim) + if(p >= plim) break; p1 = p; - while (*p && !isspace((int)*p)) + while(*p && !isspace((int)*p)) ++p; - if (p <= p1) + if(p <= p1) break; /* may happen??? */ p2 = p; /* if called for database, consider simple, single char *URL */ /* if called for config, consider weight URL */ - if (!isDB) { + if(!isDB) { /* Have weight specified? If yes, scan it */ p2 = memchr(p1, '=', p - p1); - if (p2 != NULL) { + if(p2 != NULL) { local_weight = strtoul(p2 + 1, NULL, 10); } else { p2 = p; @@ -1000,7 +1011,7 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, } pnode = shm_malloc(sizeof(struct rtpp_node)); - if (pnode == NULL) { + if(pnode == NULL) { LM_ERR("no shm memory left\n"); return -1; } @@ -1009,7 +1020,7 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, lock_get(rtpp_no_lock); pnode->idx = *rtpp_no; - if (ticks == RTPENGINE_MAX_RECHECK_TICKS) { + if(ticks == RTPENGINE_MAX_RECHECK_TICKS) { pnode->rn_recheck_ticks = ticks; } else { pnode->rn_recheck_ticks = ticks + get_ticks(); @@ -1019,7 +1030,7 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, pnode->rn_disabled = disabled; pnode->rn_displayed = 1; pnode->rn_url.s = shm_malloc(p2 - p1 + 1); - if (pnode->rn_url.s == NULL) { + if(pnode->rn_url.s == NULL) { lock_release(rtpp_no_lock); shm_free(pnode); LM_ERR("no shm memory left\n"); @@ -1027,37 +1038,39 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, } memmove(pnode->rn_url.s, p1, p2 - p1); pnode->rn_url.s[p2 - p1] = 0; - pnode->rn_url.len = p2-p1; + pnode->rn_url.len = p2 - p1; /* Leave only address in rn_address */ pnode->rn_address = pnode->rn_url.s; - if (strncasecmp(pnode->rn_address, "udp:", 4) == 0) { + if(strncasecmp(pnode->rn_address, "udp:", 4) == 0) { pnode->rn_umode = RNU_UDP; pnode->rn_address += 4; - } else if (strncasecmp(pnode->rn_address, "udp6:", 5) == 0) { + } else if(strncasecmp(pnode->rn_address, "udp6:", 5) == 0) { pnode->rn_umode = RNU_UDP6; pnode->rn_address += 5; - } else if (strncasecmp(pnode->rn_address, "unix:", 5) == 0) { + } else if(strncasecmp(pnode->rn_address, "unix:", 5) == 0) { pnode->rn_umode = RNU_LOCAL; pnode->rn_address += 5; - } else if (strncasecmp(pnode->rn_address, "ws://", 5) == 0) { + } else if(strncasecmp(pnode->rn_address, "ws://", 5) == 0) { pnode->rn_umode = RNU_WS; - } else if (strncasecmp(pnode->rn_address, "wss://", 6) == 0) { + } else if(strncasecmp(pnode->rn_address, "wss://", 6) == 0) { pnode->rn_umode = RNU_WSS; } else { lock_release(rtpp_no_lock); - LM_WARN("Node address must start with 'udp:' or 'udp6:' or 'unix:' or 'ws://' or 'wss://'. Ignore '%s'.\n", pnode->rn_address); + LM_WARN("Node address must start with 'udp:' or 'udp6:' or 'unix:' " + "or 'ws://' or 'wss://'. Ignore '%s'.\n", + pnode->rn_address); shm_free(pnode->rn_url.s); shm_free(pnode); - if (!isDB) { + if(!isDB) { continue; } else { return 0; } } - if (pnode->rn_umode != RNU_WS && pnode->rn_umode != RNU_WSS) { + if(pnode->rn_umode != RNU_WS && pnode->rn_umode != RNU_WSS) { /* Check the rn_address is 'hostname:port' */ /* Check the rn_address port is valid */ if(pnode->rn_umode == RNU_UDP6) { @@ -1068,20 +1081,22 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, } else { p1 = strchr(pnode->rn_address, ':'); } - if (p1 != NULL) { + if(p1 != NULL) { p1++; } - if (p1 != NULL && p1[0] != '\0') { + if(p1 != NULL && p1[0] != '\0') { s1.s = p1; s1.len = strlen(p1); - if (str2int(&s1, &port) < 0 || port > 0xFFFF) { + if(str2int(&s1, &port) < 0 || port > 0xFFFF) { lock_release(rtpp_no_lock); - LM_WARN("Node address must end with a valid port number. Ignore '%s'.\n", pnode->rn_address); + LM_WARN("Node address must end with a valid port number. " + "Ignore '%s'.\n", + pnode->rn_address); shm_free(pnode->rn_url.s); shm_free(pnode); - if (!isDB) { + if(!isDB) { continue; } else { return 0; @@ -1090,13 +1105,15 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, } } else { /* websocket */ - if (_rtpe_lwscb.loaded == 0) { + if(_rtpe_lwscb.loaded == 0) { lock_release(rtpp_no_lock); - LM_WARN("Websocket protocol requested, but no websocket API loaded. Ignore '%s'.\n", pnode->rn_address); + LM_WARN("Websocket protocol requested, but no websocket API " + "loaded. Ignore '%s'.\n", + pnode->rn_address); shm_free(pnode->rn_url.s); shm_free(pnode); - if (!isDB) { + if(!isDB) { continue; } else { return 0; @@ -1108,7 +1125,7 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, rtpp_node = get_rtpp_node(rtpp_list, &pnode->rn_url); lock_get(rtpp_list->rset_lock); - if (rtpp_node) { + if(rtpp_node) { rtpp_node->rn_disabled = pnode->rn_disabled; rtpp_node->rn_displayed = pnode->rn_displayed; rtpp_node->rn_recheck_ticks = pnode->rn_recheck_ticks; @@ -1119,14 +1136,14 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, shm_free(pnode->rn_url.s); shm_free(pnode); - if (!isDB) { + if(!isDB) { continue; } else { return 0; } } - if (rtpp_list->rn_first == NULL) { + if(rtpp_list->rn_first == NULL) { rtpp_list->rn_first = pnode; } else { rtpp_list->rn_last->rn_next = pnode; @@ -1139,7 +1156,7 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, *rtpp_no = *rtpp_no + 1; lock_release(rtpp_no_lock); - if (!isDB) { + if(!isDB) { continue; } else { return 0; @@ -1152,61 +1169,68 @@ int add_rtpengine_socks(struct rtpp_set *rtpp_list, char *rtpengine, /* 0 - success * -1 - error * */ -static int rtpengine_add_rtpengine_set(char * rtp_proxies, unsigned int weight, int disabled, unsigned int ticks) +static int rtpengine_add_rtpengine_set(char *rtp_proxies, unsigned int weight, + int disabled, unsigned int ticks) { - char *p,*p2; - struct rtpp_set * rtpp_list; + char *p, *p2; + struct rtpp_set *rtpp_list; unsigned int my_current_id; str id_set; /* empty definition? */ - p= rtp_proxies; - if(!p || *p=='\0'){ + p = rtp_proxies; + if(!p || *p == '\0') { return 0; } - for(;*p && isspace(*p);p++); - if(*p=='\0'){ + for(; *p && isspace(*p); p++) + ; + if(*p == '\0') { return 0; } rtp_proxies = strstr(p, "=="); - if(rtp_proxies){ - if(*(rtp_proxies +2)=='\0'){ + if(rtp_proxies) { + if(*(rtp_proxies + 2) == '\0') { LM_ERR("script error -invalid rtpengine list!\n"); return -1; } *rtp_proxies = '\0'; - p2 = rtp_proxies-1; - for(;isspace(*p2); *p2 = '\0',p2--); - id_set.s = p; id_set.len = p2 - p+1; - - if(id_set.len <= 0 ||str2int(&id_set, &my_current_id)<0 ){ - LM_ERR("script error -invalid set_id value!\n"); + p2 = rtp_proxies - 1; + for(; isspace(*p2); *p2 = '\0', p2--) + ; + id_set.s = p; + id_set.len = p2 - p + 1; + + if(id_set.len <= 0 || str2int(&id_set, &my_current_id) < 0) { + LM_ERR("script error -invalid set_id value!\n"); return -1; } - rtp_proxies+=2; - }else{ + rtp_proxies += 2; + } else { rtp_proxies = p; my_current_id = setid_default; } - for(;*rtp_proxies && isspace(*rtp_proxies);rtp_proxies++); + for(; *rtp_proxies && isspace(*rtp_proxies); rtp_proxies++) + ; - if(!(*rtp_proxies)){ + if(!(*rtp_proxies)) { LM_ERR("script error -empty rtp_proxy list\n"); - return -1;; + return -1; + ; } /*search for the current_id*/ rtpp_list = get_rtpp_set(my_current_id); - if (rtpp_list != NULL) - { + if(rtpp_list != NULL) { - if (add_rtpengine_socks(rtpp_list, rtp_proxies, weight, disabled, ticks, 0) != 0) + if(add_rtpengine_socks( + rtpp_list, rtp_proxies, weight, disabled, ticks, 0) + != 0) goto error; else return 0; @@ -1217,26 +1241,26 @@ static int rtpengine_add_rtpengine_set(char * rtp_proxies, unsigned int weight, } -static int fixup_set_id(void ** param, int param_no) +static int fixup_set_id(void **param, int param_no) { int int_val; unsigned int set_id; - struct rtpp_set* rtpp_list; + struct rtpp_set *rtpp_list; rtpp_set_link_t *rtpl = NULL; str s; - rtpl = (rtpp_set_link_t*)pkg_malloc(sizeof(rtpp_set_link_t)); - if(rtpl==NULL) { + rtpl = (rtpp_set_link_t *)pkg_malloc(sizeof(rtpp_set_link_t)); + if(rtpl == NULL) { LM_ERR("no more pkg memory\n"); return -1; } memset(rtpl, 0, sizeof(rtpp_set_link_t)); - s.s = (char*)*param; + s.s = (char *)*param; s.len = strlen(s.s); if(s.s[0] == PV_MARKER) { int_val = pv_locate_name(&s); - if(int_val<0 || int_val!=s.len) { + if(int_val < 0 || int_val != s.len) { LM_ERR("invalid parameter %s\n", s.s); pkg_free(rtpl); return -1; @@ -1249,21 +1273,21 @@ static int fixup_set_id(void ** param, int param_no) } } else { int_val = str2int(&s, &set_id); - if (int_val == 0) { + if(int_val == 0) { pkg_free(*param); - if((rtpp_list = select_rtpp_set(set_id)) ==0){ + if((rtpp_list = select_rtpp_set(set_id)) == 0) { LM_ERR("rtpp_proxy set %u not configured\n", set_id); pkg_free(rtpl); return E_CFG; } rtpl->rset = rtpp_list; } else { - LM_ERR("bad number <%s>\n", (char *)(*param)); + LM_ERR("bad number <%s>\n", (char *)(*param)); pkg_free(rtpl); return E_CFG; } } - *param = (void*)rtpl; + *param = (void *)rtpl; return 0; } @@ -1274,23 +1298,23 @@ static int rtpp_test_ping(struct rtpp_node *node) char *cp; int ret; - if (bencode_buffer_init(&bencbuf)) { + if(bencode_buffer_init(&bencbuf)) { return -1; } dict = bencode_dictionary(&bencbuf); bencode_dictionary_add_string(dict, "command", command_strings[OP_PING]); - if (bencbuf.error) { + if(bencbuf.error) { goto error; } cp = send_rtpp_command(node, dict, &ret); - if (!cp) { + if(!cp) { goto error; } dict = bencode_decode_expect(&bencbuf, cp, ret, BENCODE_DICTIONARY); - if (!dict || bencode_dictionary_get_strcmp(dict, "result", "pong")) { + if(!dict || bencode_dictionary_get_strcmp(dict, "result", "pong")) { goto error; } @@ -1303,11 +1327,11 @@ static int rtpp_test_ping(struct rtpp_node *node) } -static void rtpengine_rpc_reload(rpc_t* rpc, void* ctx) +static void rtpengine_rpc_reload(rpc_t *rpc, void *ctx) { time_t tnow; - if (rtpp_db_url.s == NULL) { + if(rtpp_db_url.s == NULL) { // no database rpc->fault(ctx, 500, "No Database URL"); return; @@ -1325,13 +1349,13 @@ static void rtpengine_rpc_reload(rpc_t* rpc, void* ctx) } _rtpe_list_version->vertime = tnow; - if (init_rtpengine_db() < 0) { + if(init_rtpengine_db() < 0) { // fail reloading from database rpc->fault(ctx, 500, "Failed reloading db"); return; } - if (build_rtpp_socks(1, 1)) { + if(build_rtpp_socks(1, 1)) { rpc->fault(ctx, 500, "Failed to build rtpengine sockets"); return; } @@ -1339,12 +1363,11 @@ static void rtpengine_rpc_reload(rpc_t* rpc, void* ctx) _rtpe_list_version->vernum += 1; _rtpe_list_version->vertime = time(NULL); LM_DBG("current rtpengines list version: %d (%" PRIu64 ")\n", - _rtpe_list_version->vernum, - (uint64_t)_rtpe_list_version->vertime); + _rtpe_list_version->vernum, (uint64_t)_rtpe_list_version->vertime); rpc->rpl_printf(ctx, "Ok. Reload successful."); } -static int rtpengine_rpc_iterate(rpc_t* rpc, void* ctx, const str *rtpp_url, +static int rtpengine_rpc_iterate(rpc_t *rpc, void *ctx, const str *rtpp_url, int (*cb)(struct rtpp_node *, struct rtpp_set *, void *), void *data) { struct rtpp_set *rtpp_list; @@ -1357,60 +1380,62 @@ static int rtpengine_rpc_iterate(rpc_t* rpc, void* ctx, const str *rtpp_url, return -1; } - if (build_rtpp_socks(1, 1)) { + if(build_rtpp_socks(1, 1)) { rpc->fault(ctx, 500, "Out of memory"); return -1; } - if (!rtpp_set_list) { + if(!rtpp_set_list) { rpc->fault(ctx, 404, "Instance not found (no sets loaded)"); return -1; } /* found a matching all - show all rtpp */ - if (strncmp("all", rtpp_url->s, 3) == 0) { + if(strncmp("all", rtpp_url->s, 3) == 0) { found = RPC_FOUND_ALL; } lock_get(rtpp_set_list->rset_head_lock); - for (rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL; + for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL; rtpp_list = rtpp_list->rset_next) { lock_get(rtpp_list->rset_lock); - for (crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL; + for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL; crt_rtpp = crt_rtpp->rn_next) { - if (!crt_rtpp->rn_displayed) { + if(!crt_rtpp->rn_displayed) { continue; } /* found a matching rtpp - ping it */ - if (found == RPC_FOUND_ALL || - (crt_rtpp->rn_url.len == rtpp_url->len && - strncmp(crt_rtpp->rn_url.s, rtpp_url->s, rtpp_url->len) == 0)) { + if(found == RPC_FOUND_ALL + || (crt_rtpp->rn_url.len == rtpp_url->len + && strncmp(crt_rtpp->rn_url.s, rtpp_url->s, + rtpp_url->len) + == 0)) { ret = cb(crt_rtpp, rtpp_list, data); - if (ret) { + if(ret) { err = 1; break; } - if (found == RPC_FOUND_NONE) { + if(found == RPC_FOUND_NONE) { found = RPC_FOUND_ONE; } } } lock_release(rtpp_list->rset_lock); - if (err) + if(err) break; } lock_release(rtpp_set_list->rset_head_lock); - if (err) + if(err) return -1; - if (found == RPC_FOUND_NONE) { + if(found == RPC_FOUND_NONE) { rpc->fault(ctx, 404, "Instance not found"); return -1; } @@ -1418,7 +1443,8 @@ static int rtpengine_rpc_iterate(rpc_t* rpc, void* ctx, const str *rtpp_url, return found; } -static int add_rtpp_node_info (void *ptrsp, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list) +static int add_rtpp_node_info( + void *ptrsp, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list) { void *vh; void **ptrs = ptrsp; @@ -1429,24 +1455,23 @@ static int add_rtpp_node_info (void *ptrsp, struct rtpp_node *crt_rtpp, struct r rpc = ptrs[0]; ctx = ptrs[1]; - if (rpc->add(ctx, "{", &vh) < 0) { + if(rpc->add(ctx, "{", &vh) < 0) { rpc->fault(ctx, 500, "Server error"); return -1; } - rpc->struct_add(vh, "Sddd", - "url", &crt_rtpp->rn_url, - "set", rtpp_list->id_set, - "index", crt_rtpp->idx, - "weight", crt_rtpp->rn_weight); + rpc->struct_add(vh, "Sddd", "url", &crt_rtpp->rn_url, "set", + rtpp_list->id_set, "index", crt_rtpp->idx, "weight", + crt_rtpp->rn_weight); - if ((1 == crt_rtpp->rn_disabled ) && (crt_rtpp->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS)) { + if((1 == crt_rtpp->rn_disabled) + && (crt_rtpp->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS)) { rpc->struct_add(vh, "s", "disabled", "1(permanent)"); } else { rpc->struct_add(vh, "d", "disabled", crt_rtpp->rn_disabled); } - if (crt_rtpp->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS) { + if(crt_rtpp->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS) { rpc->struct_add(vh, "s", "recheck_ticks", "N/A"); } else { rtpp_ticks = crt_rtpp->rn_recheck_ticks - get_ticks(); @@ -1457,26 +1482,29 @@ static int add_rtpp_node_info (void *ptrsp, struct rtpp_node *crt_rtpp, struct r return 0; } -static int rtpengine_iter_cb_enable(struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *flagp) +static int rtpengine_iter_cb_enable( + struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *flagp) { int *flag = flagp; /* do ping when try to enable the rtpp */ - if (*flag) { + if(*flag) { /* if ping success, enable the rtpp and reset ticks */ - if (rtpp_test_ping(crt_rtpp) == 0) { + if(rtpp_test_ping(crt_rtpp) == 0) { crt_rtpp->rn_disabled = 0; crt_rtpp->rn_recheck_ticks = RTPENGINE_MIN_RECHECK_TICKS; - /* if ping fail, disable the rtpps but _not_ permanently*/ + /* if ping fail, disable the rtpps but _not_ permanently*/ } else { - crt_rtpp->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout); + crt_rtpp->rn_recheck_ticks = + get_ticks() + + cfg_get(rtpengine, rtpengine_cfg, rtpengine_disable_tout); crt_rtpp->rn_disabled = 1; *flag = 2; /* return value to caller */ } - /* do not ping when disable the rtpp; disable it permanenty */ + /* do not ping when disable the rtpp; disable it permanenty */ } else { crt_rtpp->rn_disabled = 1; crt_rtpp->rn_recheck_ticks = RTPENGINE_MAX_RECHECK_TICKS; @@ -1485,51 +1513,53 @@ static int rtpengine_iter_cb_enable(struct rtpp_node *crt_rtpp, struct rtpp_set return 0; } -static void rtpengine_rpc_enable(rpc_t* rpc, void* ctx) +static void rtpengine_rpc_enable(rpc_t *rpc, void *ctx) { void *vh; str rtpp_url; int flag, found; - if (rpc->scan(ctx, "Sd", &rtpp_url, &flag) < 2) { + if(rpc->scan(ctx, "Sd", &rtpp_url, &flag) < 2) { rpc->fault(ctx, 500, "Not enough parameters"); return; } flag = flag ? 1 : 0; /* also used as a return value */ - found = rtpengine_rpc_iterate(rpc, ctx, &rtpp_url, rtpengine_iter_cb_enable, &flag); - if (found == -1) + found = rtpengine_rpc_iterate( + rpc, ctx, &rtpp_url, rtpengine_iter_cb_enable, &flag); + if(found == -1) return; - if (rpc->add(ctx, "{", &vh) < 0) { + if(rpc->add(ctx, "{", &vh) < 0) { rpc->fault(ctx, 500, "Server error"); return; } rpc->struct_add(vh, "S", "url", &rtpp_url); - if (flag == 0) + if(flag == 0) rpc->struct_add(vh, "s", "status", "disable"); - else if (flag == 1) + else if(flag == 1) rpc->struct_add(vh, "s", "status", "enable"); else rpc->struct_add(vh, "s", "status", "fail"); } -static int rtpengine_iter_cb_show(struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *ptrsp) +static int rtpengine_iter_cb_show( + struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *ptrsp) { - if (add_rtpp_node_info(ptrsp, crt_rtpp, rtpp_list) < 0) + if(add_rtpp_node_info(ptrsp, crt_rtpp, rtpp_list) < 0) return -1; return 0; } -static void rtpengine_rpc_show(rpc_t* rpc, void* ctx) +static void rtpengine_rpc_show(rpc_t *rpc, void *ctx) { str rtpp_url; void *ptrs[2] = {rpc, ctx}; - if (rpc->scan(ctx, "S", &rtpp_url) < 1) { + if(rpc->scan(ctx, "S", &rtpp_url) < 1) { rpc->fault(ctx, 500, "Not enough parameters"); return; } @@ -1537,13 +1567,16 @@ static void rtpengine_rpc_show(rpc_t* rpc, void* ctx) rtpengine_rpc_iterate(rpc, ctx, &rtpp_url, rtpengine_iter_cb_show, ptrs); } -static int rtpengine_iter_cb_ping(struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *data) +static int rtpengine_iter_cb_ping( + struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *data) { int *found_rtpp_disabled = data; /* if ping fail */ - if (rtpp_test_ping(crt_rtpp) < 0) { - crt_rtpp->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout); + if(rtpp_test_ping(crt_rtpp) < 0) { + crt_rtpp->rn_recheck_ticks = + get_ticks() + + cfg_get(rtpengine, rtpengine_cfg, rtpengine_disable_tout); *found_rtpp_disabled = 1; crt_rtpp->rn_disabled = 1; } @@ -1551,153 +1584,146 @@ static int rtpengine_iter_cb_ping(struct rtpp_node *crt_rtpp, struct rtpp_set *r return 0; } -static void rtpengine_rpc_ping(rpc_t* rpc, void* ctx) +static void rtpengine_rpc_ping(rpc_t *rpc, void *ctx) { void *vh; int found; int found_rtpp_disabled = 0; str rtpp_url; - if (rpc->scan(ctx, "S", &rtpp_url) < 1) { + if(rpc->scan(ctx, "S", &rtpp_url) < 1) { rpc->fault(ctx, 500, "Not enough parameters"); return; } - found = rtpengine_rpc_iterate(rpc, ctx, &rtpp_url, rtpengine_iter_cb_ping, &found_rtpp_disabled); - if (found == -1) + found = rtpengine_rpc_iterate( + rpc, ctx, &rtpp_url, rtpengine_iter_cb_ping, &found_rtpp_disabled); + if(found == -1) return; - if (rpc->add(ctx, "{", &vh) < 0) { + if(rpc->add(ctx, "{", &vh) < 0) { rpc->fault(ctx, 500, "Server error"); return; } - rpc->struct_add(vh, "Ss", - "url", &rtpp_url, - "status", (found_rtpp_disabled ? "fail" : "success")); + rpc->struct_add(vh, "Ss", "url", &rtpp_url, "status", + (found_rtpp_disabled ? "fail" : "success")); } -static void rtpengine_rpc_get_hash_total(rpc_t* rpc, void* ctx) +static void rtpengine_rpc_get_hash_total(rpc_t *rpc, void *ctx) { rpc->add(ctx, "u", rtpengine_hash_table_total()); } -static const char* rtpengine_rpc_reload_doc[2] = { - "Reload rtpengine proxies from database", 0 -}; -static const char* rtpengine_rpc_ping_doc[2] = { - "Ping an rtpengine instance", 0 -}; -static const char* rtpengine_rpc_show_doc[2] = { - "Get details about an rtpengine instance", 0 -}; -static const char* rtpengine_rpc_enable_doc[2] = { - "Enable or disable an rtpengine instance", 0 -}; -static const char* rtpengine_rpc_get_hash_total_doc[2] = { - "Get total number of entries in hash table", 0 -}; +static const char *rtpengine_rpc_reload_doc[2] = { + "Reload rtpengine proxies from database", 0}; +static const char *rtpengine_rpc_ping_doc[2] = { + "Ping an rtpengine instance", 0}; +static const char *rtpengine_rpc_show_doc[2] = { + "Get details about an rtpengine instance", 0}; +static const char *rtpengine_rpc_enable_doc[2] = { + "Enable or disable an rtpengine instance", 0}; +static const char *rtpengine_rpc_get_hash_total_doc[2] = { + "Get total number of entries in hash table", 0}; rpc_export_t rtpengine_rpc[] = { - {"rtpengine.reload", rtpengine_rpc_reload, rtpengine_rpc_reload_doc, 0}, - {"rtpengine.ping", rtpengine_rpc_ping, rtpengine_rpc_ping_doc, 0}, - {"rtpengine.show", rtpengine_rpc_show, rtpengine_rpc_show_doc, RET_ARRAY}, - {"rtpengine.enable", rtpengine_rpc_enable, rtpengine_rpc_enable_doc, 0}, - {"rtpengine.get_hash_total", rtpengine_rpc_get_hash_total, rtpengine_rpc_get_hash_total_doc, 0}, - {0, 0, 0, 0} -}; + {"rtpengine.reload", rtpengine_rpc_reload, rtpengine_rpc_reload_doc, 0}, + {"rtpengine.ping", rtpengine_rpc_ping, rtpengine_rpc_ping_doc, 0}, + {"rtpengine.show", rtpengine_rpc_show, rtpengine_rpc_show_doc, + RET_ARRAY}, + {"rtpengine.enable", rtpengine_rpc_enable, rtpengine_rpc_enable_doc, 0}, + {"rtpengine.get_hash_total", rtpengine_rpc_get_hash_total, + rtpengine_rpc_get_hash_total_doc, 0}, + {0, 0, 0, 0}}; static int rtpengine_rpc_init(void) { - if (rpc_register_array(rtpengine_rpc)!=0) - { + if(rpc_register_array(rtpengine_rpc) != 0) { LM_ERR("failed to register RPC commands\n"); return -1; } return 0; } -static int -mod_init(void) +static int mod_init(void) { int i; pv_spec_t *avp_spec; unsigned short avp_flags; str s; - _rtpe_list_version = (rtpe_list_version_t*)shm_mallocxz(sizeof(rtpe_list_version_t)); - if(_rtpe_list_version==NULL) { + _rtpe_list_version = + (rtpe_list_version_t *)shm_mallocxz(sizeof(rtpe_list_version_t)); + if(_rtpe_list_version == NULL) { LM_ERR("no more shm memory for rtpe list version\n"); return -1; } _rtpe_list_version->vernum = 1; _rtpe_list_version->vertime = time(NULL); - if(rtpengine_rpc_init()<0) - { + if(rtpengine_rpc_init() < 0) { LM_ERR("failed to register RPC commands\n"); return -1; } - rtpp_no = (unsigned int*)shm_malloc(sizeof(unsigned int)); - if (!rtpp_no) { + rtpp_no = (unsigned int *)shm_malloc(sizeof(unsigned int)); + if(!rtpp_no) { LM_ERR("no more shm memory for rtpp_no\n"); return -1; } *rtpp_no = 0; rtpp_no_lock = lock_alloc(); - if (!rtpp_no_lock) { + if(!rtpp_no_lock) { LM_ERR("no more shm memory for rtpp_no_lock\n"); return -1; } - if (lock_init(rtpp_no_lock) == 0) { + if(lock_init(rtpp_no_lock) == 0) { LM_ERR("could not init rtpp_no_lock\n"); return -1; } - if(_rtpe_wsapi.s!=NULL && _rtpe_wsapi.len==4 - && strncasecmp(_rtpe_wsapi.s, "lwsc", 4)==0) { + if(_rtpe_wsapi.s != NULL && _rtpe_wsapi.len == 4 + && strncasecmp(_rtpe_wsapi.s, "lwsc", 4) == 0) { if(lwsc_load_api(&_rtpe_lwscb)) { LM_ERR("failed to load WS API: %s\n", _rtpe_wsapi.s); return -1; } } else { - if(_rtpe_wsapi.s!=NULL && _rtpe_wsapi.len>0) { + if(_rtpe_wsapi.s != NULL && _rtpe_wsapi.len > 0) { LM_ERR("unsupported WS API: %s\n", _rtpe_wsapi.s); return -1; } } /* initialize the list of set; mod_destroy does shm_free() if fail */ - if (!rtpp_set_list) { + if(!rtpp_set_list) { rtpp_set_list = shm_malloc(sizeof(struct rtpp_set_head)); - if(!rtpp_set_list){ + if(!rtpp_set_list) { LM_ERR("no shm memory left to create list of proxysets\n"); return -1; } memset(rtpp_set_list, 0, sizeof(struct rtpp_set_head)); rtpp_set_list->rset_head_lock = lock_alloc(); - if (!rtpp_set_list->rset_head_lock) { + if(!rtpp_set_list->rset_head_lock) { LM_ERR("no shm memory left to create list of proxysets lock\n"); return -1; } - if (lock_init(rtpp_set_list->rset_head_lock) == 0) { + if(lock_init(rtpp_set_list->rset_head_lock) == 0) { LM_ERR("could not init lock sets for rtpengine list\n"); return -1; } } - if (rtpp_db_url.s == NULL) - { + if(rtpp_db_url.s == NULL) { /* storing the list of rtpengine sets in shared memory*/ - for(i=0;itype != PVT_AVP) && - (rtp_inst_pvar->type != PVT_XAVP) && - (rtp_inst_pvar->type != PVT_SCRIPTVAR))) { - LM_ERR("Invalid pvar name <%.*s>\n", rtp_inst_pv_param.len, rtp_inst_pv_param.s); + if((rtp_inst_pvar == NULL) + || ((rtp_inst_pvar->type != PVT_AVP) + && (rtp_inst_pvar->type != PVT_XAVP) + && (rtp_inst_pvar->type != PVT_SCRIPTVAR))) { + LM_ERR("Invalid pvar name <%.*s>\n", rtp_inst_pv_param.len, + rtp_inst_pv_param.s); return -1; } } - if (pv_parse_var(&extra_id_pv_param, &extra_id_pv, NULL)) + if(pv_parse_var(&extra_id_pv_param, &extra_id_pv, NULL)) return -1; - if (mos_label_stats_parse(&global_mos_stats)) + if(mos_label_stats_parse(&global_mos_stats)) return -1; - if (mos_label_stats_parse(&side_A_mos_stats)) + if(mos_label_stats_parse(&side_A_mos_stats)) return -1; - if (mos_label_stats_parse(&side_B_mos_stats)) + if(mos_label_stats_parse(&side_B_mos_stats)) return -1; - if (setid_avp_param) { - s.s = setid_avp_param; s.len = strlen(s.s); + if(setid_avp_param) { + s.s = setid_avp_param; + s.len = strlen(s.s); avp_spec = pv_cache_get(&s); - if (avp_spec==NULL || (avp_spec->type != PVT_AVP)) { + if(avp_spec == NULL || (avp_spec->type != PVT_AVP)) { LM_ERR("malformed or non AVP definition <%s>\n", setid_avp_param); return -1; } - if (pv_get_avp_name(0, &(avp_spec->pvp), &setid_avp, &avp_flags) != 0) { + if(pv_get_avp_name(0, &(avp_spec->pvp), &setid_avp, &avp_flags) != 0) { LM_ERR("invalid AVP definition <%s>\n", setid_avp_param); return -1; } setid_avp_type = avp_flags; } - if (write_sdp_pvar_str.len > 0) { + if(write_sdp_pvar_str.len > 0) { write_sdp_pvar = pv_cache_get(&write_sdp_pvar_str); - if (write_sdp_pvar == NULL - || (write_sdp_pvar->type != PVT_AVP && write_sdp_pvar->type != PVT_SCRIPTVAR) ) { + if(write_sdp_pvar == NULL + || (write_sdp_pvar->type != PVT_AVP + && write_sdp_pvar->type != PVT_SCRIPTVAR)) { LM_ERR("write_sdp_pv: not a valid AVP or VAR definition <%.*s>\n", - write_sdp_pvar_str.len, write_sdp_pvar_str.s); + write_sdp_pvar_str.len, write_sdp_pvar_str.s); return -1; } } - if (read_sdp_pvar_str.len > 0) { + if(read_sdp_pvar_str.len > 0) { read_sdp_pvar = pv_cache_get(&read_sdp_pvar_str); - if (read_sdp_pvar == NULL - || (read_sdp_pvar->type != PVT_AVP && read_sdp_pvar->type != PVT_SCRIPTVAR) ) { + if(read_sdp_pvar == NULL + || (read_sdp_pvar->type != PVT_AVP + && read_sdp_pvar->type != PVT_SCRIPTVAR)) { LM_ERR("read_sdp_pv: not a valid AVP or VAR definition <%.*s>\n", - read_sdp_pvar_str.len, read_sdp_pvar_str.s); + read_sdp_pvar_str.len, read_sdp_pvar_str.s); return -1; } } - if (media_duration_pvar_str.len > 0) { + if(media_duration_pvar_str.len > 0) { media_duration_pvar = pv_cache_get(&media_duration_pvar_str); - if (media_duration_pvar == NULL - || (media_duration_pvar->type != PVT_AVP && media_duration_pvar->type != PVT_SCRIPTVAR) ) { - LM_ERR("media_duration_pv: not a valid AVP or VAR definition <%.*s>\n", - media_duration_pvar_str.len, media_duration_pvar_str.s); + if(media_duration_pvar == NULL + || (media_duration_pvar->type != PVT_AVP + && media_duration_pvar->type != PVT_SCRIPTVAR)) { + LM_ERR("media_duration_pv: not a valid AVP or VAR definition " + "<%.*s>\n", + media_duration_pvar_str.len, media_duration_pvar_str.s); return -1; } } - if (rtpp_strings) + if(rtpp_strings) pkg_free(rtpp_strings); - if (load_tm_api( &tmb ) < 0) - { + if(load_tm_api(&tmb) < 0) { LM_DBG("could not load the TM-functions - answer-offer model" - " auto-detection is disabled\n"); + " auto-detection is disabled\n"); memset(&tmb, 0, sizeof(struct tm_binds)); } /* Determine IP addr type (IPv4 or IPv6 allowed) */ force_send_ip_af = get_ip_type(force_send_ip_str); - if (force_send_ip_af != AF_INET && force_send_ip_af != AF_INET6 && - strlen(force_send_ip_str) > 0) { + if(force_send_ip_af != AF_INET && force_send_ip_af != AF_INET6 + && strlen(force_send_ip_str) > 0) { LM_ERR("%s is an unknown address\n", force_send_ip_str); return -1; } /* init the hastable which keeps the call-id <-> selected_node relation */ - if (!rtpengine_hash_table_init(hash_table_size)) { + if(!rtpengine_hash_table_init(hash_table_size)) { LM_ERR("rtpengine_hash_table_init(%d) failed!\n", hash_table_size); return -1; } else { @@ -1811,19 +1839,20 @@ mod_init(void) /* select the default set */ default_rtpp_set = select_rtpp_set(setid_default); - if (!default_rtpp_set) { + if(!default_rtpp_set) { LM_NOTICE("Default rtpp set %u NOT found\n", setid_default); } else { LM_DBG("Default rtpp set %u found\n", setid_default); } - if(cfg_declare("rtpengine", rtpengine_cfg_def, &default_rtpengine_cfg, cfg_sizeof(rtpengine), &rtpengine_cfg)){ - LM_ERR("Failed to declare the configuration\n"); - return -1; - } + if(cfg_declare("rtpengine", rtpengine_cfg_def, &default_rtpengine_cfg, + cfg_sizeof(rtpengine), &rtpengine_cfg)) { + LM_ERR("Failed to declare the configuration\n"); + return -1; + } - if (hash_algo == RTP_HASH_SHA1_CALLID) { - if (load_crypto_api(&rtpengine_cb) != 0) { + if(hash_algo == RTP_HASH_SHA1_CALLID) { + if(load_crypto_api(&rtpengine_cb) != 0) { LM_ERR("Crypto module required in order to have SHA1 hashing!\n"); return -1; } @@ -1832,24 +1861,29 @@ mod_init(void) return 0; } -#define rtpe_reload_lock_get(plock) do { \ - if (rtpp_db_url.s != NULL && lmode != 0) lock_get(plock); \ -} while(0) +#define rtpe_reload_lock_get(plock) \ + do { \ + if(rtpp_db_url.s != NULL && lmode != 0) \ + lock_get(plock); \ + } while(0) -#define rtpe_reload_lock_release(plock) do { \ - if (rtpp_db_url.s != NULL && lmode != 0) lock_release(plock); \ -} while(0) +#define rtpe_reload_lock_release(plock) \ + do { \ + if(rtpp_db_url.s != NULL && lmode != 0) \ + lock_release(plock); \ + } while(0) /** * build rtp enigne sockets * - lmode: locking mode (1 - lock if needed; 0 - no locking) * - rtest: rtpengine testing (1 - test if active; 0 - no test done) */ -static int build_rtpp_socks(int lmode, int rtest) { +static int build_rtpp_socks(int lmode, int rtest) +{ int n, i; char *cp; struct addrinfo hints, *res; - struct rtpp_set *rtpp_list; + struct rtpp_set *rtpp_list; struct rtpp_node *pnode; unsigned int current_rtpp_no; #ifdef IP_MTU_DISCOVER @@ -1859,8 +1893,8 @@ static int build_rtpp_socks(int lmode, int rtest) { if(_rtpe_list_vernum_local == _rtpe_list_version->vernum) { /* same version for the list of rtpengines */ LM_DBG("same rtpengines list version: %d (%" PRIu64 ")\n", - _rtpe_list_version->vernum, - (uint64_t)_rtpe_list_version->vertime); + _rtpe_list_version->vernum, + (uint64_t)_rtpe_list_version->vertime); return 0; } @@ -1869,8 +1903,8 @@ static int build_rtpp_socks(int lmode, int rtest) { rtpe_reload_lock_release(rtpp_no_lock); // close current sockets - for (i = 0; i < rtpp_socks_size; i++) { - if (rtpp_socks[i] >= 0) { + for(i = 0; i < rtpp_socks_size; i++) { + if(rtpp_socks[i] >= 0) { close(rtpp_socks[i]); rtpp_socks[i] = -1; } @@ -1878,24 +1912,25 @@ static int build_rtpp_socks(int lmode, int rtest) { rtpp_socks_size = current_rtpp_no; /* allocate one more to have a safety end place holder */ - rtpp_socks = (int*)pkg_reallocxf(rtpp_socks, sizeof(int)*(rtpp_socks_size+1)); - if (!rtpp_socks) { + rtpp_socks = (int *)pkg_reallocxf( + rtpp_socks, sizeof(int) * (rtpp_socks_size + 1)); + if(!rtpp_socks) { LM_ERR("no more pkg memory for rtpp_socks\n"); return -1; } - memset(rtpp_socks, -1, sizeof(int)*(rtpp_socks_size+1)); + memset(rtpp_socks, -1, sizeof(int) * (rtpp_socks_size + 1)); rtpe_reload_lock_get(rtpp_set_list->rset_head_lock); _rtpe_list_vernum_local = _rtpe_list_version->vernum; - for (rtpp_list = rtpp_set_list->rset_first; rtpp_list != 0; - rtpp_list = rtpp_list->rset_next) { + for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != 0; + rtpp_list = rtpp_list->rset_next) { rtpe_reload_lock_get(rtpp_list->rset_lock); - for (pnode=rtpp_list->rn_first; pnode!=0; pnode = pnode->rn_next) { + for(pnode = rtpp_list->rn_first; pnode != 0; pnode = pnode->rn_next) { char *hostname; char *hp; - if (pnode->rn_umode == RNU_LOCAL || pnode->rn_umode == RNU_WS + if(pnode->rn_umode == RNU_LOCAL || pnode->rn_umode == RNU_WS || pnode->rn_umode == RNU_WSS) { rtpp_socks[pnode->idx] = -1; goto rptest; @@ -1905,8 +1940,9 @@ static int build_rtpp_socks(int lmode, int rtest) { * This is UDP or UDP6. Detect host and port; lookup host; * do connect() in order to specify peer address */ - hostname = (char*)pkg_malloc(sizeof(char) * (strlen(pnode->rn_address) + 1)); - if (hostname==NULL) { + hostname = (char *)pkg_malloc( + sizeof(char) * (strlen(pnode->rn_address) + 1)); + if(hostname == NULL) { LM_ERR("no more pkg memory\n"); rtpp_socks[pnode->idx] = -1; @@ -1919,11 +1955,11 @@ static int build_rtpp_socks(int lmode, int rtest) { strcpy(hostname, pnode->rn_address); cp = strrchr(hostname, ':'); - if (cp != NULL) { + if(cp != NULL) { *cp = '\0'; cp++; } - if (cp == NULL || *cp == '\0') + if(cp == NULL || *cp == '\0') cp = CPORT; if(pnode->rn_umode == RNU_UDP6) { @@ -1940,9 +1976,10 @@ static int build_rtpp_socks(int lmode, int rtest) { memset(&hints, 0, sizeof(hints)); hints.ai_flags = 0; - hints.ai_family = (pnode->rn_umode == RNU_UDP6) ? AF_INET6 : AF_INET; + hints.ai_family = + (pnode->rn_umode == RNU_UDP6) ? AF_INET6 : AF_INET; hints.ai_socktype = SOCK_DGRAM; - if ((n = getaddrinfo(hp, cp, &hints, &res)) != 0) { + if((n = getaddrinfo(hp, cp, &hints, &res)) != 0) { LM_ERR("%s\n", gai_strerror(n)); pkg_free(hostname); rtpp_socks[pnode->idx] = -1; @@ -1955,9 +1992,10 @@ static int build_rtpp_socks(int lmode, int rtest) { } pkg_free(hostname); - rtpp_socks[pnode->idx] = socket((pnode->rn_umode == RNU_UDP6) - ? AF_INET6 : AF_INET, SOCK_DGRAM, 0); - if (rtpp_socks[pnode->idx] == -1) { + rtpp_socks[pnode->idx] = + socket((pnode->rn_umode == RNU_UDP6) ? AF_INET6 : AF_INET, + SOCK_DGRAM, 0); + if(rtpp_socks[pnode->idx] == -1) { LM_ERR("can't create socket\n"); freeaddrinfo(res); @@ -1969,28 +2007,27 @@ static int build_rtpp_socks(int lmode, int rtest) { } #ifdef IP_MTU_DISCOVER - if (setsockopt(rtpp_socks[pnode->idx], IPPROTO_IP, - IP_MTU_DISCOVER, &ip_mtu_discover, - sizeof(ip_mtu_discover))) + if(setsockopt(rtpp_socks[pnode->idx], IPPROTO_IP, IP_MTU_DISCOVER, + &ip_mtu_discover, sizeof(ip_mtu_discover))) LM_WARN("Failed enable set MTU discovery socket option\n"); #endif if((0 <= control_cmd_tos) && (control_cmd_tos < 256)) { unsigned char tos = control_cmd_tos; - if (pnode->rn_umode == RNU_UDP6) { + if(pnode->rn_umode == RNU_UDP6) { if(setsockopt(rtpp_socks[pnode->idx], IPPROTO_IPV6, - IPV6_TCLASS, &control_cmd_tos, - sizeof(control_cmd_tos))) + IPV6_TCLASS, &control_cmd_tos, + sizeof(control_cmd_tos))) LM_WARN("Failed to set IPv6 TOS socket option\n"); } else { - if(setsockopt(rtpp_socks[pnode->idx], IPPROTO_IP, - IP_TOS, &tos, sizeof(tos))) + if(setsockopt(rtpp_socks[pnode->idx], IPPROTO_IP, IP_TOS, + &tos, sizeof(tos))) LM_WARN("Failed to set IPv4 TOS socket option\n"); } } - if (bind_force_send_ip(pnode->idx) == -1) { + if(bind_force_send_ip(pnode->idx) == -1) { LM_ERR("can't bind socket\n"); close(rtpp_socks[pnode->idx]); rtpp_socks[pnode->idx] = -1; @@ -2003,7 +2040,8 @@ static int build_rtpp_socks(int lmode, int rtest) { continue; } - if (connect(rtpp_socks[pnode->idx], res->ai_addr, res->ai_addrlen) == -1) { + if(connect(rtpp_socks[pnode->idx], res->ai_addr, res->ai_addrlen) + == -1) { LM_ERR("can't connect to a RTPEngine instance\n"); close(rtpp_socks[pnode->idx]); rtpp_socks[pnode->idx] = -1; @@ -2017,8 +2055,9 @@ static int build_rtpp_socks(int lmode, int rtest) { } freeaddrinfo(res); -rptest: - if(rtest) pnode->rn_disabled = rtpp_test(pnode, 0, 1); + rptest: + if(rtest) + pnode->rn_disabled = rtpp_test(pnode, 0, 1); } rtpe_reload_lock_release(rtpp_list->rset_lock); } @@ -2027,14 +2066,15 @@ static int build_rtpp_socks(int lmode, int rtest) { return 0; } -static int pv_parse_var(str *inp, pv_elem_t **outp, int *got_any) { - if (inp->s && *inp->s) { +static int pv_parse_var(str *inp, pv_elem_t **outp, int *got_any) +{ + if(inp->s && *inp->s) { inp->len = strlen(inp->s); if(pv_parse_format(inp, outp) < 0) { LM_ERR("malformed PV string: %s\n", inp->s); return -1; } - if (got_any) + if(got_any) *got_any = 1; } else { *outp = NULL; @@ -2042,70 +2082,73 @@ static int pv_parse_var(str *inp, pv_elem_t **outp, int *got_any) { return 0; } -static int minmax_pv_parse(struct minmax_mos_stats *s, int *got_any) { - if (pv_parse_var(&s->mos_param, &s->mos_pv, got_any)) +static int minmax_pv_parse(struct minmax_mos_stats *s, int *got_any) +{ + if(pv_parse_var(&s->mos_param, &s->mos_pv, got_any)) return -1; - if (pv_parse_var(&s->at_param, &s->at_pv, got_any)) + if(pv_parse_var(&s->at_param, &s->at_pv, got_any)) return -1; - if (pv_parse_var(&s->packetloss_param, &s->packetloss_pv, got_any)) + if(pv_parse_var(&s->packetloss_param, &s->packetloss_pv, got_any)) return -1; - if (pv_parse_var(&s->jitter_param, &s->jitter_pv, got_any)) + if(pv_parse_var(&s->jitter_param, &s->jitter_pv, got_any)) return -1; - if (pv_parse_var(&s->roundtrip_param, &s->roundtrip_pv, got_any)) + if(pv_parse_var(&s->roundtrip_param, &s->roundtrip_pv, got_any)) return -1; - if (pv_parse_var(&s->roundtrip_leg_param, &s->roundtrip_leg_pv, got_any)) + if(pv_parse_var(&s->roundtrip_leg_param, &s->roundtrip_leg_pv, got_any)) return -1; - if (pv_parse_var(&s->samples_param, &s->samples_pv, got_any)) + if(pv_parse_var(&s->samples_param, &s->samples_pv, got_any)) return -1; return 0; } -static int mos_label_stats_parse(struct minmax_mos_label_stats *mmls) { - if (pv_parse_var(&mmls->label_param, &mmls->label_pv, &mmls->got_any_pvs)) +static int mos_label_stats_parse(struct minmax_mos_label_stats *mmls) +{ + if(pv_parse_var(&mmls->label_param, &mmls->label_pv, &mmls->got_any_pvs)) return -1; - if (minmax_pv_parse(&mmls->min, &mmls->got_any_pvs)) + if(minmax_pv_parse(&mmls->min, &mmls->got_any_pvs)) return -1; - if (minmax_pv_parse(&mmls->max, &mmls->got_any_pvs)) + if(minmax_pv_parse(&mmls->max, &mmls->got_any_pvs)) return -1; - if (minmax_pv_parse(&mmls->average, &mmls->got_any_pvs)) + if(minmax_pv_parse(&mmls->average, &mmls->got_any_pvs)) return -1; - if (mmls->got_any_pvs) + if(mmls->got_any_pvs) got_any_mos_pvs = 1; return 0; } -static int -child_init(int rank) +static int child_init(int rank) { if(!rtpp_set_list) return 0; /* do not init sockets for PROC_INIT and main process when fork=yes */ - if(rank==PROC_INIT || (rank==PROC_MAIN && dont_fork==0)) { + if(rank == PROC_INIT || (rank == PROC_MAIN && dont_fork == 0)) { return 0; } mypid = getpid(); // vector of pointers to queried nodes - queried_nodes_ptr = (struct rtpp_node**)pkg_malloc(MAX_RTPP_TRIED_NODES * sizeof(struct rtpp_node*)); - if (!queried_nodes_ptr) { + queried_nodes_ptr = (struct rtpp_node **)pkg_malloc( + MAX_RTPP_TRIED_NODES * sizeof(struct rtpp_node *)); + if(!queried_nodes_ptr) { LM_ERR("no more pkg memory for queried_nodes_ptr\n"); return -1; } - memset(queried_nodes_ptr, 0, MAX_RTPP_TRIED_NODES * sizeof(struct rtpp_node*)); + memset(queried_nodes_ptr, 0, + MAX_RTPP_TRIED_NODES * sizeof(struct rtpp_node *)); /* Iterate known RTPEngine instances - create sockets */ - if(rank==PROC_SIPINIT) { + if(rank == PROC_SIPINIT) { /* probe rtpengines only in first worker */ - if (build_rtpp_socks(0, 1)) + if(build_rtpp_socks(0, 1)) return -1; } else { - if (build_rtpp_socks(0, 0)) + if(build_rtpp_socks(0, 0)) return -1; } @@ -2115,36 +2158,36 @@ child_init(int rank) static void mod_destroy(void) { - struct rtpp_set * crt_list, * last_list; - struct rtpp_node * crt_rtpp, *last_rtpp; + struct rtpp_set *crt_list, *last_list; + struct rtpp_node *crt_rtpp, *last_rtpp; /*free the shared memory*/ - if (rtpp_no) { + if(rtpp_no) { shm_free(rtpp_no); rtpp_no = NULL; } - if (rtpp_no_lock) { + if(rtpp_no_lock) { lock_destroy(rtpp_no_lock); lock_dealloc(rtpp_no_lock); rtpp_no_lock = NULL; } - if (!rtpp_set_list) { + if(!rtpp_set_list) { return; } - if (!rtpp_set_list->rset_head_lock) { + if(!rtpp_set_list->rset_head_lock) { shm_free(rtpp_set_list); rtpp_set_list = NULL; return; } lock_get(rtpp_set_list->rset_head_lock); - for(crt_list = rtpp_set_list->rset_first; crt_list != NULL; ){ + for(crt_list = rtpp_set_list->rset_first; crt_list != NULL;) { last_list = crt_list; - if (!crt_list->rset_lock) { + if(!crt_list->rset_lock) { crt_list = last_list->rset_next; shm_free(last_list); last_list = NULL; @@ -2152,7 +2195,7 @@ static void mod_destroy(void) } lock_get(last_list->rset_lock); - for(crt_rtpp = crt_list->rn_first; crt_rtpp != NULL; ){ + for(crt_rtpp = crt_list->rn_first; crt_rtpp != NULL;) { if(crt_rtpp->rn_url.s) shm_free(crt_rtpp->rn_url.s); @@ -2165,7 +2208,7 @@ static void mod_destroy(void) lock_release(last_list->rset_lock); lock_destroy(last_list->rset_lock); - lock_dealloc((void*)last_list->rset_lock); + lock_dealloc((void *)last_list->rset_lock); last_list->rset_lock = NULL; shm_free(last_list); @@ -2174,26 +2217,26 @@ static void mod_destroy(void) lock_release(rtpp_set_list->rset_head_lock); lock_destroy(rtpp_set_list->rset_head_lock); - lock_dealloc((void*)rtpp_set_list->rset_head_lock); + lock_dealloc((void *)rtpp_set_list->rset_head_lock); rtpp_set_list->rset_head_lock = NULL; shm_free(rtpp_set_list); rtpp_set_list = NULL; /* destroy the hastable which keeps the call-id <-> selected_node relation */ - if (!rtpengine_hash_table_destroy()) { + if(!rtpengine_hash_table_destroy()) { LM_ERR("rtpengine_hash_table_destroy() failed!\n"); } else { LM_DBG("rtpengine_hash_table_destroy() success!\n"); } - if(_rtpe_list_version!=NULL) { + if(_rtpe_list_version != NULL) { shm_free(_rtpe_list_version); _rtpe_list_version = NULL; } } -static char * gencookie(void) +static char *gencookie(void) { static char cook[34]; @@ -2203,150 +2246,151 @@ static char * gencookie(void) } - static const char *transports[] = { - [0x00] = "RTP/AVP", - [0x01] = "RTP/SAVP", - [0x02] = "RTP/AVPF", - [0x03] = "RTP/SAVPF", - [0x04] = "UDP/TLS/RTP/SAVP", - [0x06] = "UDP/TLS/RTP/SAVPF", + [0x00] = "RTP/AVP", + [0x01] = "RTP/SAVP", + [0x02] = "RTP/AVPF", + [0x03] = "RTP/SAVPF", + [0x04] = "UDP/TLS/RTP/SAVP", + [0x06] = "UDP/TLS/RTP/SAVPF", }; -static int parse_codec_flag(struct ng_flags_parse *ng_flags, const str *key, const str *val, - const char *cmp1, const char *cmp2, const char *dictstr, +static int parse_codec_flag(struct ng_flags_parse *ng_flags, const str *key, + const str *val, const char *cmp1, const char *cmp2, const char *dictstr, bencode_item_t **dictp) { str s; - if (!str_key_val_prefix(key, cmp1, val, &s)) { - if (!cmp2) + if(!str_key_val_prefix(key, cmp1, val, &s)) { + if(!cmp2) return 0; - if (!str_key_val_prefix(key, cmp2, val, &s)) + if(!str_key_val_prefix(key, cmp2, val, &s)) return 0; } - if (!*dictp) { + if(!*dictp) { *dictp = bencode_list(ng_flags->dict->buffer); - bencode_dictionary_add(ng_flags->codec, dictstr, - *dictp); + bencode_dictionary_add(ng_flags->codec, dictstr, *dictp); } bencode_list_add_str(*dictp, &s); return 1; } -static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, enum rtpe_operation *op, - const char *flags_str) +static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, + enum rtpe_operation *op, const char *flags_str) { char *e; const char *err; - str key, val, s , s1; + str key, val, s, s1; int ip_af = AF_UNSPEC; - if (!flags_str) + if(!flags_str) return 0; - while (1) { - while (*flags_str == ' ') + while(1) { + while(*flags_str == ' ') flags_str++; - key.s = (void *) flags_str; + key.s = (void *)flags_str; val.len = key.len = -1; val.s = NULL; e = strpbrk(key.s, " ="); - if (!e) + if(!e) e = key.s + strlen(key.s); - else if (*e == '=') { + else if(*e == '=') { key.len = e - key.s; val.s = e + 1; e = strchr(val.s, ' '); - if (!e) + if(!e) e = val.s + strlen(val.s); val.len = e - val.s; } - if (key.len == -1) + if(key.len == -1) key.len = e - key.s; - if (!key.len) + if(!key.len) break; /* check for items which have their own sub-list */ - if (str_key_val_prefix(&key, "replace", &val, &s)) { + if(str_key_val_prefix(&key, "replace", &val, &s)) { bencode_list_add_str(ng_flags->replace, &s); goto next; } - if (str_key_val_prefix(&key, "received-from", &val, &s)) { + if(str_key_val_prefix(&key, "received-from", &val, &s)) { ip_af = get_ip_type(s.s); - if (ip_af == AF_INET) - { - s1.s="IP4"; - s1.len=3; + if(ip_af == AF_INET) { + s1.s = "IP4"; + s1.len = 3; bencode_list_add_str(ng_flags->received_from, &s1); bencode_list_add_str(ng_flags->received_from, &s); - - }else if (ip_af == AF_INET6) - { - s1.s="IP6"; - s1.len=3; + + } else if(ip_af == AF_INET6) { + s1.s = "IP6"; + s1.len = 3; bencode_list_add_str(ng_flags->received_from, &s1); bencode_list_add_str(ng_flags->received_from, &s); - } - - + + goto next; } - if (str_key_val_prefix(&key, "SDES", &val, &s)) { + if(str_key_val_prefix(&key, "SDES", &val, &s)) { bencode_list_add_str(ng_flags->sdes, &s); goto next; } - if (str_key_val_prefix(&key, "T38", &val, &s) || str_key_val_prefix(&key, "T.38", &val, &s)) { + if(str_key_val_prefix(&key, "T38", &val, &s) + || str_key_val_prefix(&key, "T.38", &val, &s)) { bencode_list_add_str(ng_flags->t38, &s); goto next; } - if (str_key_val_prefix(&key, "rtcp-mux", &val, &s)) { + if(str_key_val_prefix(&key, "rtcp-mux", &val, &s)) { bencode_list_add_str(ng_flags->rtcp_mux, &s); goto next; } - if (parse_codec_flag(ng_flags, &key, &val, "transcode", "codec-transcode", "transcode", &ng_flags->codec_transcode)) + if(parse_codec_flag(ng_flags, &key, &val, "transcode", + "codec-transcode", "transcode", &ng_flags->codec_transcode)) goto next; - if (parse_codec_flag(ng_flags, &key, &val, "codec-strip", NULL, "strip", &ng_flags->codec_strip)) + if(parse_codec_flag(ng_flags, &key, &val, "codec-strip", NULL, "strip", + &ng_flags->codec_strip)) goto next; - if (parse_codec_flag(ng_flags, &key, &val, "codec-offer", NULL, "offer", &ng_flags->codec_offer)) + if(parse_codec_flag(ng_flags, &key, &val, "codec-offer", NULL, "offer", + &ng_flags->codec_offer)) goto next; - if (parse_codec_flag(ng_flags, &key, &val, "codec-mask", NULL, "mask", &ng_flags->codec_mask)) + if(parse_codec_flag(ng_flags, &key, &val, "codec-mask", NULL, "mask", + &ng_flags->codec_mask)) goto next; - if (parse_codec_flag(ng_flags, &key, &val, "codec-set", NULL, "set", &ng_flags->codec_set)) + if(parse_codec_flag(ng_flags, &key, &val, "codec-set", NULL, "set", + &ng_flags->codec_set)) goto next; - if (parse_codec_flag(ng_flags, &key, &val, "codec-except", NULL, "except", &ng_flags->codec_except)) + if(parse_codec_flag(ng_flags, &key, &val, "codec-except", NULL, + "except", &ng_flags->codec_except)) goto next; /* check for specially handled items */ - switch (key.len) { + switch(key.len) { case 3: - if (str_eq(&key, "RTP") && !val.s) { + if(str_eq(&key, "RTP") && !val.s) { ng_flags->transport |= 0x100; ng_flags->transport &= ~0x001; - } - else if (str_eq(&key, "AVP") && !val.s) { + } else if(str_eq(&key, "AVP") && !val.s) { ng_flags->transport |= 0x100; ng_flags->transport &= ~0x002; - } - else if (str_eq(&key, "TOS") && val.s) - bencode_dictionary_add_integer(ng_flags->dict, "TOS", atoi(val.s)); + } else if(str_eq(&key, "TOS") && val.s) + bencode_dictionary_add_integer( + ng_flags->dict, "TOS", atoi(val.s)); else goto generic; goto next; break; case 4: - if (str_eq(&key, "SRTP") && !val.s) + if(str_eq(&key, "SRTP") && !val.s) ng_flags->transport |= 0x101; - else if (str_eq(&key, "AVPF") && !val.s) + else if(str_eq(&key, "AVPF") && !val.s) ng_flags->transport |= 0x102; - else if (str_eq(&key, "DTLS") && !val.s) + else if(str_eq(&key, "DTLS") && !val.s) ng_flags->transport |= 0x104; else goto generic; @@ -2354,8 +2398,8 @@ static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, enu break; case 6: - if (str_eq(&key, "to-tag")) { - if (val.s) + if(str_eq(&key, "to-tag")) { + if(val.s) ng_flags->to_tag = val; ng_flags->to = 1; goto next; @@ -2363,42 +2407,40 @@ static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, enu break; case 7: - if (str_eq(&key, "RTP/AVP") && !val.s) + if(str_eq(&key, "RTP/AVP") && !val.s) ng_flags->transport = 0x100; - else if (str_eq(&key, "call-id")) { + else if(str_eq(&key, "call-id")) { err = "missing value"; - if (!val.s) + if(!val.s) goto error; ng_flags->call_id = val; - } - else + } else goto generic; goto next; break; case 8: - if (str_eq(&key, "internal") || str_eq(&key, "external")) + if(str_eq(&key, "internal") || str_eq(&key, "external")) bencode_list_add_str(ng_flags->direction, &key); - else if (str_eq(&key, "RTP/AVPF") && !val.s) + else if(str_eq(&key, "RTP/AVPF") && !val.s) ng_flags->transport = 0x102; - else if (str_eq(&key, "RTP/SAVP") && !val.s) + else if(str_eq(&key, "RTP/SAVP") && !val.s) ng_flags->transport = 0x101; - else if (str_eq(&key, "from-tag")) { + else if(str_eq(&key, "from-tag")) { err = "missing value"; - if (!val.s) + if(!val.s) goto error; ng_flags->from_tag = val; ng_flags->directional = 1; - } - else + } else goto generic; goto next; break; case 9: - if (str_eq(&key, "RTP/SAVPF") && !val.s) + if(str_eq(&key, "RTP/SAVPF") && !val.s) ng_flags->transport = 0x103; - else if (str_eq(&key, "direction")) + else if(str_eq(&key, "direction")) bencode_list_add_str(ng_flags->direction, &val); else goto generic; @@ -2406,22 +2448,24 @@ static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, enu break; case 10: - if (str_eq(&key, "via-branch")) { + if(str_eq(&key, "via-branch")) { err = "missing value"; - if (!val.s) + if(!val.s) goto error; err = "invalid value"; - if (*val.s == '1' || *val.s == '2') + if(*val.s == '1' || *val.s == '2') ng_flags->via = *val.s - '0'; - else if (str_eq(&val, "auto")) + else if(str_eq(&val, "auto")) ng_flags->via = 3; - else if (str_eq(&val, "extra")) + else if(str_eq(&val, "extra")) ng_flags->via = -1; - else if (str_eq(&val, "next")) + else if(str_eq(&val, "next")) ng_flags->via = -2; - else if (str_eq(&val, "auto-next") || str_eq(&val, "next-auto")) + else if(str_eq(&val, "auto-next") + || str_eq(&val, "next-auto")) ng_flags->via = -3; - else if (str_eq(&val, "auto-extra") || str_eq(&val, "extra-auto")) + else if(str_eq(&val, "auto-extra") + || str_eq(&val, "extra-auto")) ng_flags->via = -4; else goto error; @@ -2430,22 +2474,22 @@ static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, enu break; case 11: - if (str_eq(&key, "repacketize")) { + if(str_eq(&key, "repacketize")) { err = "missing value"; - if (!val.s) + if(!val.s) goto error; ng_flags->packetize = 0; - while (isdigit(*val.s)) { + while(isdigit(*val.s)) { ng_flags->packetize *= 10; ng_flags->packetize += *val.s - '0'; val.s++; } err = "invalid value"; - if (!ng_flags->packetize) + if(!ng_flags->packetize) goto error; - bencode_dictionary_add_integer(ng_flags->dict, "repacketize", ng_flags->packetize); - } - else if (str_eq(&key, "directional")) + bencode_dictionary_add_integer( + ng_flags->dict, "repacketize", ng_flags->packetize); + } else if(str_eq(&key, "directional")) ng_flags->directional = 1; else goto generic; @@ -2453,20 +2497,20 @@ static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, enu break; case 12: - if (str_eq(&key, "force-answer")) { + if(str_eq(&key, "force-answer")) { err = "cannot force answer in non-offer command"; - if (*op != OP_OFFER) + if(*op != OP_OFFER) goto error; *op = OP_ANSWER; goto next; - } - else if (str_eq(&key, "delete-delay") && val.s) - bencode_dictionary_add_integer(ng_flags->dict, "delete delay", atoi(val.s)); + } else if(str_eq(&key, "delete-delay") && val.s) + bencode_dictionary_add_integer( + ng_flags->dict, "delete delay", atoi(val.s)); break; - + case 16: - if (str_eq(&key, "UDP/TLS/RTP/SAVP") && !val.s) + if(str_eq(&key, "UDP/TLS/RTP/SAVP") && !val.s) ng_flags->transport = 0x104; else goto generic; @@ -2474,39 +2518,39 @@ static int parse_flags(struct ng_flags_parse *ng_flags, struct sip_msg *msg, enu break; case 17: - if (str_eq(&key, "UDP/TLS/RTP/SAVPF") && !val.s) + if(str_eq(&key, "UDP/TLS/RTP/SAVPF") && !val.s) ng_flags->transport = 0x106; else goto generic; goto next; break; - } -generic: - if (!val.s) + generic: + if(!val.s) bencode_list_add_str(ng_flags->flags, &key); else bencode_dictionary_str_add_str(ng_flags->dict, &key, &val); goto next; -next: + next: flags_str = e; } return 0; error: - if (val.s) - LM_ERR("error processing flag `%.*s' (value '%.*s'): %s\n", key.len, key.s, - val.len, val.s, err); + if(val.s) + LM_ERR("error processing flag `%.*s' (value '%.*s'): %s\n", key.len, + key.s, val.len, val.s, err); else LM_ERR("error processing flag `%.*s': %s\n", key.len, key.s, err); return -1; } -static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_msg *msg, - enum rtpe_operation op, const char *flags_str, str *body_out) +static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, + struct sip_msg *msg, enum rtpe_operation op, const char *flags_str, + str *body_out) { struct ng_flags_parse ng_flags; bencode_item_t *item, *resp; @@ -2527,20 +2571,22 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ memset(&ng_flags, 0, sizeof(ng_flags)); if(IS_SIP(msg) || IS_SIP_REPLY(msg)) { - if (get_callid(msg, &ng_flags.call_id) == -1 || ng_flags.call_id.len == 0) { + if(get_callid(msg, &ng_flags.call_id) == -1 + || ng_flags.call_id.len == 0) { LM_ERR("can't get Call-Id field\n"); return NULL; } - if (get_to_tag(msg, &ng_flags.to_tag) == -1) { + if(get_to_tag(msg, &ng_flags.to_tag) == -1) { LM_ERR("can't get To tag\n"); return NULL; } - if (get_from_tag(msg, &ng_flags.from_tag) == -1 || ng_flags.from_tag.len == 0) { + if(get_from_tag(msg, &ng_flags.from_tag) == -1 + || ng_flags.from_tag.len == 0) { LM_ERR("can't get From tag\n"); return NULL; } } - if (bencode_buffer_init(bencbuf)) { + if(bencode_buffer_init(bencbuf)) { LM_ERR("could not initialize bencode_buffer_t\n"); return NULL; } @@ -2553,7 +2599,7 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ ng_flags.flags = bencode_list(bencbuf); ng_flags.received_from = bencode_list(bencbuf); - if (op == OP_OFFER || op == OP_ANSWER) { + if(op == OP_OFFER || op == OP_ANSWER) { ng_flags.direction = bencode_list(bencbuf); ng_flags.replace = bencode_list(bencbuf); ng_flags.rtcp_mux = bencode_list(bencbuf); @@ -2561,21 +2607,22 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ ng_flags.t38 = bencode_list(bencbuf); ng_flags.codec = bencode_dictionary(bencbuf); - if (read_sdp_pvar!= NULL) { - if (read_sdp_pvar->getf(msg,&read_sdp_pvar->pvp, &pv_val) < 0) - { - LM_ERR("error getting pvar value <%.*s>\n", read_sdp_pvar_str.len, read_sdp_pvar_str.s); + if(read_sdp_pvar != NULL) { + if(read_sdp_pvar->getf(msg, &read_sdp_pvar->pvp, &pv_val) < 0) { + LM_ERR("error getting pvar value <%.*s>\n", + read_sdp_pvar_str.len, read_sdp_pvar_str.s); goto error; } else { body = pv_val.rs; } - } else if ((cont_type = extract_body(msg, &body)) == -1) { + } else if((cont_type = extract_body(msg, &body)) == -1) { LM_ERR("can't extract body from the message\n"); goto error; } - if (body_intermediate.s) - bencode_dictionary_add_str(ng_flags.dict, "sdp", &body_intermediate); + if(body_intermediate.s) + bencode_dictionary_add_str( + ng_flags.dict, "sdp", &body_intermediate); else bencode_dictionary_add_str(ng_flags.dict, "sdp", &body); } @@ -2584,49 +2631,49 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ ng_flags.to = (op == OP_DELETE) ? 0 : 1; - if (parse_flags(&ng_flags, msg, &op, flags_str)) + if(parse_flags(&ng_flags, msg, &op, flags_str)) goto error; if(!IS_SIP(msg) && !IS_SIP_REPLY(msg)) { /* check required values */ - if (ng_flags.call_id.len == 0) { + if(ng_flags.call_id.len == 0) { LM_ERR("can't get Call-Id field\n"); return NULL; } - if (ng_flags.from_tag.len == 0) { + if(ng_flags.from_tag.len == 0) { LM_ERR("can't get From tag\n"); return NULL; } } /* trickle ice sdp fragment? */ - if (cont_type == 3) + if(cont_type == 3) bencode_list_add_string(ng_flags.flags, "fragment"); /* only add those if any flags were given at all */ - if (ng_flags.direction && ng_flags.direction->child) + if(ng_flags.direction && ng_flags.direction->child) bencode_dictionary_add(ng_flags.dict, "direction", ng_flags.direction); - if (ng_flags.flags && ng_flags.flags->child) + if(ng_flags.flags && ng_flags.flags->child) bencode_dictionary_add(ng_flags.dict, "flags", ng_flags.flags); - if (ng_flags.replace && ng_flags.replace->child) + if(ng_flags.replace && ng_flags.replace->child) bencode_dictionary_add(ng_flags.dict, "replace", ng_flags.replace); - if (ng_flags.codec && ng_flags.codec->child) + if(ng_flags.codec && ng_flags.codec->child) bencode_dictionary_add(ng_flags.dict, "codec", ng_flags.codec); - if ((ng_flags.transport & 0x100)) + if((ng_flags.transport & 0x100)) bencode_dictionary_add_string(ng_flags.dict, "transport-protocol", transports[ng_flags.transport & 0x007]); - if (ng_flags.rtcp_mux && ng_flags.rtcp_mux->child) + if(ng_flags.rtcp_mux && ng_flags.rtcp_mux->child) bencode_dictionary_add(ng_flags.dict, "rtcp-mux", ng_flags.rtcp_mux); - if (ng_flags.sdes && ng_flags.sdes->child) + if(ng_flags.sdes && ng_flags.sdes->child) bencode_dictionary_add(ng_flags.dict, "SDES", ng_flags.sdes); - if (ng_flags.t38 && ng_flags.t38->child) + if(ng_flags.t38 && ng_flags.t38->child) bencode_dictionary_add(ng_flags.dict, "T.38", ng_flags.t38); bencode_dictionary_add_str(ng_flags.dict, "call-id", &ng_flags.call_id); - if (ng_flags.via) { + if(ng_flags.via) { /* pre-process */ - switch (ng_flags.via) { + switch(ng_flags.via) { case 3: ng_flags.via = (msg->first_line.type == SIP_REPLY) ? 2 : 1; break; @@ -2639,73 +2686,79 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ } ret = -1; - switch (ng_flags.via) { + switch(ng_flags.via) { case 1: case 2: ret = get_via_branch(msg, ng_flags.via, &viabranch); break; case -1: - if (extra_id_pv) + if(extra_id_pv) ret = get_extra_id(msg, &viabranch); break; case -2: - if (!char_msg_val(msg, md5)) + if(!char_msg_val(msg, md5)) break; branch_idx = 0; - if (tmb.t_gett) { + if(tmb.t_gett) { t = tmb.t_gett(); - if (t && t != T_UNDEFINED) + if(t && t != T_UNDEFINED) branch_idx = t->nr_of_outgoings; } - msg->hash_index = hash(msg->callid->body, get_cseq(msg)->number); + msg->hash_index = + hash(msg->callid->body, get_cseq(msg)->number); viabranch.s = branch_buf; - if (branch_builder(msg->hash_index, 0, md5, branch_idx, branch_buf, &viabranch.len)) + if(branch_builder(msg->hash_index, 0, md5, branch_idx, + branch_buf, &viabranch.len)) ret = 0; break; } - if (ret == -1 || viabranch.len == 0) { + if(ret == -1 || viabranch.len == 0) { LM_ERR("can't get Via branch/extra ID\n"); goto error; } bencode_dictionary_add_str(ng_flags.dict, "via-branch", &viabranch); } - if (ng_flags.received_from && ng_flags.received_from->child) { - bencode_dictionary_add(ng_flags.dict, "received-from", ng_flags.received_from); - } - else { + if(ng_flags.received_from && ng_flags.received_from->child) { + bencode_dictionary_add( + ng_flags.dict, "received-from", ng_flags.received_from); + } else { //item = bencode_list(bencbuf); - bencode_dictionary_add(ng_flags.dict, "received-from", ng_flags.received_from); - bencode_list_add_string(ng_flags.received_from, (msg->rcv.src_ip.af == AF_INET) ? "IP4" : ( - (msg->rcv.src_ip.af == AF_INET6) ? "IP6" : - "?" - ) ); - bencode_list_add_string(ng_flags.received_from, ip_addr2a(&msg->rcv.src_ip)); - } - - if (op == OP_BLOCK_DTMF || op == OP_BLOCK_MEDIA || op == OP_UNBLOCK_DTMF - || op == OP_UNBLOCK_MEDIA || op == OP_START_FORWARDING || op == OP_STOP_FORWARDING - || op == OP_SILENCE_MEDIA || op == OP_UNSILENCE_MEDIA) - { - if (ng_flags.directional) { - bencode_dictionary_add_str(ng_flags.dict, "from-tag", &ng_flags.from_tag); - if (ng_flags.to && ng_flags.to_tag.s && ng_flags.to_tag.len) - bencode_dictionary_add_str(ng_flags.dict, "to-tag", &ng_flags.to_tag); + bencode_dictionary_add( + ng_flags.dict, "received-from", ng_flags.received_from); + bencode_list_add_string(ng_flags.received_from, + (msg->rcv.src_ip.af == AF_INET) + ? "IP4" + : ((msg->rcv.src_ip.af == AF_INET6) ? "IP6" : "?")); + bencode_list_add_string( + ng_flags.received_from, ip_addr2a(&msg->rcv.src_ip)); + } + + if(op == OP_BLOCK_DTMF || op == OP_BLOCK_MEDIA || op == OP_UNBLOCK_DTMF + || op == OP_UNBLOCK_MEDIA || op == OP_START_FORWARDING + || op == OP_STOP_FORWARDING || op == OP_SILENCE_MEDIA + || op == OP_UNSILENCE_MEDIA) { + if(ng_flags.directional) { + bencode_dictionary_add_str( + ng_flags.dict, "from-tag", &ng_flags.from_tag); + if(ng_flags.to && ng_flags.to_tag.s && ng_flags.to_tag.len) + bencode_dictionary_add_str( + ng_flags.dict, "to-tag", &ng_flags.to_tag); } - } - else if ((msg->first_line.type == SIP_REQUEST && op != OP_ANSWER) - || (msg->first_line.type == SIP_REPLY && op == OP_DELETE) - || (msg->first_line.type == SIP_REPLY && op == OP_ANSWER) - || ng_flags.directional) /* set if from-tag was set manually */ + } else if((msg->first_line.type == SIP_REQUEST && op != OP_ANSWER) + || (msg->first_line.type == SIP_REPLY && op == OP_DELETE) + || (msg->first_line.type == SIP_REPLY && op == OP_ANSWER) + || ng_flags.directional) /* set if from-tag was set manually */ { - bencode_dictionary_add_str(ng_flags.dict, "from-tag", &ng_flags.from_tag); - if (ng_flags.to && ng_flags.to_tag.s && ng_flags.to_tag.len) - bencode_dictionary_add_str(ng_flags.dict, "to-tag", &ng_flags.to_tag); - } - else { - if (!ng_flags.to_tag.s || !ng_flags.to_tag.len) { + bencode_dictionary_add_str( + ng_flags.dict, "from-tag", &ng_flags.from_tag); + if(ng_flags.to && ng_flags.to_tag.s && ng_flags.to_tag.len) + bencode_dictionary_add_str( + ng_flags.dict, "to-tag", &ng_flags.to_tag); + } else { + if(!ng_flags.to_tag.s || !ng_flags.to_tag.len) { LM_ERR("No to-tag present\n"); goto error; } @@ -2713,11 +2766,12 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ bencode_dictionary_add_str(ng_flags.dict, "to-tag", &ng_flags.from_tag); } - bencode_dictionary_add_string(ng_flags.dict, "command", command_strings[op]); + bencode_dictionary_add_string( + ng_flags.dict, "command", command_strings[op]); /*** send it out ***/ - if (bencbuf->error) { + if(bencbuf->error) { LM_ERR("out of memory - bencode failed\n"); goto error; } @@ -2727,26 +2781,31 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ select_node: do { - if (queried_nodes >= cfg_get(rtpengine,rtpengine_cfg,queried_nodes_limit)) { + if(queried_nodes + >= cfg_get(rtpengine, rtpengine_cfg, queried_nodes_limit)) { LM_ERR("queried nodes limit reached\n"); goto error; } - node = select_rtpp_node(ng_flags.call_id, viabranch, 1, queried_nodes_ptr, queried_nodes, op); - if (!node) { + node = select_rtpp_node(ng_flags.call_id, viabranch, 1, + queried_nodes_ptr, queried_nodes, op); + if(!node) { LM_ERR("no available proxies\n"); goto error; } cp = send_rtpp_command(node, ng_flags.dict, &ret); /* if node is disabled permanent, don't recheck it later */ - if (cp == NULL && node->rn_recheck_ticks != RTPENGINE_MAX_RECHECK_TICKS) { + if(cp == NULL + && node->rn_recheck_ticks != RTPENGINE_MAX_RECHECK_TICKS) { node->rn_disabled = 1; - node->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout); + node->rn_recheck_ticks = + get_ticks() + + cfg_get(rtpengine, rtpengine_cfg, rtpengine_disable_tout); } queried_nodes_ptr[queried_nodes++] = node; - } while (cp == NULL); + } while(cp == NULL); LM_DBG("proxy reply: %.*s\n", ret, cp); @@ -2754,44 +2813,50 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ /*** process reply ***/ resp = bencode_decode_expect(bencbuf, cp, ret, BENCODE_DICTIONARY); - if (!resp) { + if(!resp) { LM_ERR("failed to decode bencoded reply from proxy: %.*s\n", ret, cp); goto error; } result = bencode_dictionary_get_expect(resp, "result", BENCODE_STRING); - if (!result) { + if(!result) { LM_ERR("No 'result' dictionary entry in response from proxy %.*s", node->rn_url.len, node->rn_url.s); goto error; } - if (!bencode_strcmp(result, "load limit")) { + if(!bencode_strcmp(result, "load limit")) { item = bencode_dictionary_get_expect(resp, "message", BENCODE_STRING); - if (!item) + if(!item) LM_INFO("proxy %.*s has reached its load limit - trying next one", node->rn_url.len, node->rn_url.s); else - LM_INFO("proxy %.*s has reached its load limit (%.*s) - trying next one", - node->rn_url.len, node->rn_url.s, - (int) item->iov[1].iov_len, (char *) item->iov[1].iov_base); + LM_INFO("proxy %.*s has reached its load limit (%.*s) - trying " + "next one", + node->rn_url.len, node->rn_url.s, (int)item->iov[1].iov_len, + (char *)item->iov[1].iov_base); goto select_node; } - if (!bencode_strcmp(result, "error")) { - if (!bencode_dictionary_get_str(resp, "error-reason", &error)) { - LM_ERR("proxy return error but didn't give an error reason: %.*s\n", ret, cp); + if(!bencode_strcmp(result, "error")) { + if(!bencode_dictionary_get_str(resp, "error-reason", &error)) { + LM_ERR("proxy return error but didn't give an error reason: %.*s\n", + ret, cp); } else { - if ((RTPENGINE_SESS_LIMIT_MSG_LEN == error.len) && - (strncmp(error.s, RTPENGINE_SESS_LIMIT_MSG, RTPENGINE_SESS_LIMIT_MSG_LEN) == 0)) - { - LM_WARN("proxy %.*s: %.*s", node->rn_url.len, node->rn_url.s , error.len, error.s); + if((RTPENGINE_SESS_LIMIT_MSG_LEN == error.len) + && (strncmp(error.s, RTPENGINE_SESS_LIMIT_MSG, + RTPENGINE_SESS_LIMIT_MSG_LEN) + == 0)) { + LM_WARN("proxy %.*s: %.*s", node->rn_url.len, node->rn_url.s, + error.len, error.s); goto select_node; } - if ((RTPENGINE_SESS_OUT_OF_PORTS_MSG_LEN == error.len) && - (strncmp(error.s, RTPENGINE_SESS_OUT_OF_PORTS_MSG, RTPENGINE_SESS_OUT_OF_PORTS_MSG_LEN) == 0)) - { - LM_WARN("proxy %.*s: %.*s", node->rn_url.len, node->rn_url.s , error.len, error.s); + if((RTPENGINE_SESS_OUT_OF_PORTS_MSG_LEN == error.len) + && (strncmp(error.s, RTPENGINE_SESS_OUT_OF_PORTS_MSG, + RTPENGINE_SESS_OUT_OF_PORTS_MSG_LEN) + == 0)) { + LM_WARN("proxy %.*s: %.*s", node->rn_url.len, node->rn_url.s, + error.len, error.s); goto select_node; } @@ -2801,30 +2866,35 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ } /* add hastable entry with the node => */ - if (!rtpengine_hash_table_lookup(ng_flags.call_id, viabranch, op)) { + if(!rtpengine_hash_table_lookup(ng_flags.call_id, viabranch, op)) { // build the entry - struct rtpengine_hash_entry *entry = shm_malloc(sizeof(struct rtpengine_hash_entry)); - if (!entry) { - LM_ERR("rtpengine hash table fail to create entry for calllen=%d callid=%.*s viabranch=%.*s\n", - ng_flags.call_id.len, ng_flags.call_id.len, ng_flags.call_id.s, - viabranch.len, viabranch.s); + struct rtpengine_hash_entry *entry = + shm_malloc(sizeof(struct rtpengine_hash_entry)); + if(!entry) { + LM_ERR("rtpengine hash table fail to create entry for calllen=%d " + "callid=%.*s viabranch=%.*s\n", + ng_flags.call_id.len, ng_flags.call_id.len, + ng_flags.call_id.s, viabranch.len, viabranch.s); goto skip_hash_table_insert; } memset(entry, 0, sizeof(struct rtpengine_hash_entry)); // fill the entry - if (ng_flags.call_id.s && ng_flags.call_id.len > 0) { - if (shm_str_dup(&entry->callid, &ng_flags.call_id) < 0) { - LM_ERR("rtpengine hash table fail to duplicate calllen=%d callid=%.*s\n", - ng_flags.call_id.len, ng_flags.call_id.len, ng_flags.call_id.s); + if(ng_flags.call_id.s && ng_flags.call_id.len > 0) { + if(shm_str_dup(&entry->callid, &ng_flags.call_id) < 0) { + LM_ERR("rtpengine hash table fail to duplicate calllen=%d " + "callid=%.*s\n", + ng_flags.call_id.len, ng_flags.call_id.len, + ng_flags.call_id.s); rtpengine_hash_table_free_entry(entry); goto skip_hash_table_insert; } } - if (viabranch.s && viabranch.len > 0) { - if (shm_str_dup(&entry->viabranch, &viabranch) < 0) { - LM_ERR("rtpengine hash table fail to duplicate calllen=%d viabranch=%.*s\n", - ng_flags.call_id.len, viabranch.len, viabranch.s); + if(viabranch.s && viabranch.len > 0) { + if(shm_str_dup(&entry->viabranch, &viabranch) < 0) { + LM_ERR("rtpengine hash table fail to duplicate calllen=%d " + "viabranch=%.*s\n", + ng_flags.call_id.len, viabranch.len, viabranch.s); rtpengine_hash_table_free_entry(entry); goto skip_hash_table_insert; } @@ -2834,33 +2904,39 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ entry->tout = get_ticks() + hash_table_tout; // insert the key<->entry from the hashtable - if (!rtpengine_hash_table_insert(ng_flags.call_id, viabranch, entry)) { - LM_ERR("rtpengine hash table fail to insert node=%.*s for calllen=%d callid=%.*s viabranch=%.*s\n", - node->rn_url.len, node->rn_url.s, ng_flags.call_id.len, - ng_flags.call_id.len, ng_flags.call_id.s, viabranch.len, viabranch.s); + if(!rtpengine_hash_table_insert(ng_flags.call_id, viabranch, entry)) { + LM_ERR("rtpengine hash table fail to insert node=%.*s for " + "calllen=%d callid=%.*s viabranch=%.*s\n", + node->rn_url.len, node->rn_url.s, ng_flags.call_id.len, + ng_flags.call_id.len, ng_flags.call_id.s, viabranch.len, + viabranch.s); rtpengine_hash_table_free_entry(entry); goto skip_hash_table_insert; } else { - LM_DBG("rtpengine hash table insert node=%.*s for calllen=%d callid=%.*s viabranch=%.*s\n", - node->rn_url.len, node->rn_url.s, ng_flags.call_id.len, - ng_flags.call_id.len, ng_flags.call_id.s, viabranch.len, viabranch.s); + LM_DBG("rtpengine hash table insert node=%.*s for calllen=%d " + "callid=%.*s viabranch=%.*s\n", + node->rn_url.len, node->rn_url.s, ng_flags.call_id.len, + ng_flags.call_id.len, ng_flags.call_id.s, viabranch.len, + viabranch.s); } } skip_hash_table_insert: - if (body_out) + if(body_out) *body_out = body; - if (op == OP_DELETE) { + if(op == OP_DELETE) { /* Delete the key<->value from the hashtable */ - if (!rtpengine_hash_table_remove(ng_flags.call_id, viabranch, op)) { - LM_ERR("rtpengine hash table failed to remove entry for callen=%d callid=%.*s viabranch=%.*s\n", - ng_flags.call_id.len, ng_flags.call_id.len, ng_flags.call_id.s, - viabranch.len, viabranch.s); + if(!rtpengine_hash_table_remove(ng_flags.call_id, viabranch, op)) { + LM_ERR("rtpengine hash table failed to remove entry for callen=%d " + "callid=%.*s viabranch=%.*s\n", + ng_flags.call_id.len, ng_flags.call_id.len, + ng_flags.call_id.s, viabranch.len, viabranch.s); } else { - LM_DBG("rtpengine hash table remove entry for callen=%d callid=%.*s viabranch=%.*s\n", - ng_flags.call_id.len, ng_flags.call_id.len, ng_flags.call_id.s, - viabranch.len, viabranch.s); + LM_DBG("rtpengine hash table remove entry for callen=%d " + "callid=%.*s viabranch=%.*s\n", + ng_flags.call_id.len, ng_flags.call_id.len, + ng_flags.call_id.s, viabranch.len, viabranch.s); } } @@ -2871,16 +2947,17 @@ static bencode_item_t *rtpp_function_call(bencode_buffer_t *bencbuf, struct sip_ return NULL; } -static int rtpp_function_call_simple(struct sip_msg *msg, enum rtpe_operation op, const char *flags_str) +static int rtpp_function_call_simple( + struct sip_msg *msg, enum rtpe_operation op, const char *flags_str) { bencode_buffer_t bencbuf; bencode_item_t *ret; ret = rtpp_function_call(&bencbuf, msg, op, flags_str, NULL); - if (!ret) + if(!ret) return -1; - if (bencode_dictionary_get_strcmp(ret, "result", "ok")) { + if(bencode_dictionary_get_strcmp(ret, "result", "ok")) { LM_ERR("proxy didn't return \"ok\" result\n"); bencode_buffer_free(&bencbuf); return -1; @@ -2890,21 +2967,24 @@ static int rtpp_function_call_simple(struct sip_msg *msg, enum rtpe_operation op return 1; } -static int rtpengine_simple_wrap(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_simple_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ return rtpp_function_call_simple(msg, op, d); } -static bencode_item_t *rtpp_function_call_ok(bencode_buffer_t *bencbuf, struct sip_msg *msg, - enum rtpe_operation op, const char *flags_str, str *body) +static bencode_item_t *rtpp_function_call_ok(bencode_buffer_t *bencbuf, + struct sip_msg *msg, enum rtpe_operation op, const char *flags_str, + str *body) { bencode_item_t *ret; ret = rtpp_function_call(bencbuf, msg, op, flags_str, body); - if (!ret) + if(!ret) return NULL; - if (bencode_dictionary_get_strcmp(ret, "result", "ok")) { + if(bencode_dictionary_get_strcmp(ret, "result", "ok")) { LM_ERR("proxy didn't return \"ok\" result\n"); bencode_buffer_free(bencbuf); return NULL; @@ -2914,51 +2994,51 @@ static bencode_item_t *rtpp_function_call_ok(bencode_buffer_t *bencbuf, struct s } - -static int -rtpp_test(struct rtpp_node *node, int isdisabled, int force) +static int rtpp_test(struct rtpp_node *node, int isdisabled, int force) { bencode_buffer_t bencbuf; bencode_item_t *dict; char *cp; int ret; - if(node->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS){ + if(node->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS) { LM_DBG("rtpp %s disabled for ever\n", node->rn_url.s); return 1; } - if (force == 0) { - if (isdisabled == 0) + if(force == 0) { + if(isdisabled == 0) return 0; - if (node->rn_recheck_ticks > get_ticks()) + if(node->rn_recheck_ticks > get_ticks()) return 1; } - if (bencode_buffer_init(&bencbuf)) { + if(bencode_buffer_init(&bencbuf)) { LM_ERR("could not initialized bencode_buffer_t\n"); return 1; } dict = bencode_dictionary(&bencbuf); bencode_dictionary_add_string(dict, "command", "ping"); - if (bencbuf.error) + if(bencbuf.error) goto benc_error; cp = send_rtpp_command(node, dict, &ret); - if (!cp) { + if(!cp) { node->rn_disabled = 1; - node->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout); + node->rn_recheck_ticks = + get_ticks() + + cfg_get(rtpengine, rtpengine_cfg, rtpengine_disable_tout); LM_ERR("proxy did not respond to ping\n"); goto error; } dict = bencode_decode_expect(&bencbuf, cp, ret, BENCODE_DICTIONARY); - if (!dict || bencode_dictionary_get_strcmp(dict, "result", "pong")) { + if(!dict || bencode_dictionary_get_strcmp(dict, "result", "pong")) { LM_ERR("proxy responded with invalid response\n"); goto error; } LM_INFO("rtpengine instance <%s> found, support for it %senabled\n", - node->rn_url.s, force == 0 ? "re-" : ""); + node->rn_url.s, force == 0 ? "re-" : ""); bencode_buffer_free(&bencbuf); return 0; @@ -2970,8 +3050,8 @@ rtpp_test(struct rtpp_node *node, int isdisabled, int force) return 1; } -static char * -send_rtpp_command(struct rtpp_node *node, bencode_item_t *dict, int *outlen) +static char *send_rtpp_command( + struct rtpp_node *node, bencode_item_t *dict, int *outlen) { struct sockaddr_un addr; int fd, len, i, vcnt; @@ -2981,34 +3061,33 @@ send_rtpp_command(struct rtpp_node *node, bencode_item_t *dict, int *outlen) struct pollfd fds[1]; struct iovec *v; str cmd = STR_NULL; - const static str rtpe_proto = { "ng.rtpengine.com", 16 }; + const static str rtpe_proto = {"ng.rtpengine.com", 16}; str request, response; v = bencode_iovec(dict, &vcnt, 1, 0); - if (!v) { + if(!v) { LM_ERR("error converting bencode to iovec\n"); return NULL; } len = 0; cp = buf; - rtpengine_tout_ms = cfg_get(rtpengine,rtpengine_cfg,rtpengine_tout_ms); + rtpengine_tout_ms = cfg_get(rtpengine, rtpengine_cfg, rtpengine_tout_ms); - if (node->rn_umode == RNU_LOCAL) { + if(node->rn_umode == RNU_LOCAL) { memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_LOCAL; - strncpy(addr.sun_path, node->rn_address, - sizeof(addr.sun_path) - 1); + strncpy(addr.sun_path, node->rn_address, sizeof(addr.sun_path) - 1); #ifdef HAVE_SOCKADDR_SA_LEN addr.sun_len = strlen(addr.sun_path); #endif fd = socket(AF_LOCAL, SOCK_STREAM, 0); - if (fd < 0) { + if(fd < 0) { LM_ERR("can't create socket\n"); goto badproxy; } - if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + if(connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { close(fd); LM_ERR("can't connect to RTPEngine <%s>\n", node->rn_url.s); goto badproxy; @@ -3016,61 +3095,61 @@ send_rtpp_command(struct rtpp_node *node, bencode_item_t *dict, int *outlen) do { len = writev(fd, v + 1, vcnt); - } while (len == -1 && errno == EINTR); - if (len <= 0) { + } while(len == -1 && errno == EINTR); + if(len <= 0) { close(fd); LM_ERR("can't send command to RTPEngine <%s>\n", node->rn_url.s); goto badproxy; } do { len = read(fd, buf, sizeof(buf) - 1); - } while (len == -1 && errno == EINTR); + } while(len == -1 && errno == EINTR); close(fd); - if (len <= 0) { + if(len <= 0) { LM_ERR("can't read reply from RTPEngine <%s>\n", node->rn_url.s); goto badproxy; } - } else if (node->rn_umode == RNU_WS || node->rn_umode == RNU_WSS) { + } else if(node->rn_umode == RNU_WS || node->rn_umode == RNU_WSS) { /* assemble full request string, flatten iovec */ v[0].iov_base = gencookie(); v[0].iov_len = strlen(v[0].iov_base); len = 0; - for (i = 0; i <= vcnt; i++) + for(i = 0; i <= vcnt; i++) len += v[i].iov_len; request.s = pkg_malloc(len + 1); - if (!request.s) { + if(!request.s) { LM_ERR("out of memory\n"); goto badproxy; } len = 0; - for (i = 0; i <= vcnt; i++) { + for(i = 0; i <= vcnt; i++) { memcpy(request.s + len, v[i].iov_base, v[i].iov_len); len += v[i].iov_len; } request.s[len] = '\0'; request.len = len; - len = _rtpe_lwscb.request(&node->rn_url, (str *) &rtpe_proto, &request, &response, - rtpengine_tout_ms * 1000); + len = _rtpe_lwscb.request(&node->rn_url, (str *)&rtpe_proto, &request, + &response, rtpengine_tout_ms * 1000); - if (len < 0) { + if(len < 0) { LM_ERR("failed to do websocket request\n"); goto badproxy; } /* process/copy response; verify cookie */ - if (response.len < v[0].iov_len) { + if(response.len < v[0].iov_len) { LM_ERR("empty or short websocket response\n"); pkg_free(response.s); goto badproxy; } - if (memcmp(response.s, v[0].iov_base, v[0].iov_len)) { + if(memcmp(response.s, v[0].iov_base, v[0].iov_len)) { LM_ERR("mismatched cookie in websocket response\n"); pkg_free(response.s); goto badproxy; } len = response.len - v[0].iov_len; - if (len >= sizeof(buf)) { + if(len >= sizeof(buf)) { LM_ERR("websocket response too large\n"); pkg_free(response.s); goto badproxy; @@ -3083,41 +3162,43 @@ send_rtpp_command(struct rtpp_node *node, bencode_item_t *dict, int *outlen) fds[0].events = POLLIN; fds[0].revents = 0; /* Drain input buffer */ - while ((poll(fds, 1, 0) == 1) && - ((fds[0].revents & POLLIN) != 0)) { + while((poll(fds, 1, 0) == 1) && ((fds[0].revents & POLLIN) != 0)) { /* coverity[check_return : FALSE] */ recv(rtpp_socks[node->idx], buf, sizeof(buf) - 1, 0); fds[0].revents = 0; } v[0].iov_base = gencookie(); v[0].iov_len = strlen(v[0].iov_base); - rtpengine_retr = cfg_get(rtpengine,rtpengine_cfg,rtpengine_retr); - for (i = 0; i < rtpengine_retr; i++) { + rtpengine_retr = cfg_get(rtpengine, rtpengine_cfg, rtpengine_retr); + for(i = 0; i < rtpengine_retr; i++) { do { len = writev(rtpp_socks[node->idx], v, vcnt + 1); - } while (len == -1 && (errno == EINTR || errno == ENOBUFS)); - if (len <= 0) { + } while(len == -1 && (errno == EINTR || errno == ENOBUFS)); + if(len <= 0) { bencode_get_str(bencode_dictionary_get(dict, "command"), &cmd); LM_ERR("can't send command \"%.*s\" to RTPEngine <%s>\n", - cmd.len, cmd.s, node->rn_url.s); + cmd.len, cmd.s, node->rn_url.s); goto badproxy; } - while ((poll(fds, 1, rtpengine_tout_ms) == 1) && - (fds[0].revents & POLLIN) != 0) { + while((poll(fds, 1, rtpengine_tout_ms) == 1) + && (fds[0].revents & POLLIN) != 0) { do { - len = recv(rtpp_socks[node->idx], buf, sizeof(buf)-1, 0); - } while (len == -1 && errno == EINTR); - if (len <= 0) { - bencode_get_str(bencode_dictionary_get(dict, "command"), &cmd); - LM_ERR("can't read reply for command \"%.*s\" from RTPEngine <%s>\n", - cmd.len, cmd.s, node->rn_url.s); + len = recv(rtpp_socks[node->idx], buf, sizeof(buf) - 1, 0); + } while(len == -1 && errno == EINTR); + if(len <= 0) { + bencode_get_str( + bencode_dictionary_get(dict, "command"), &cmd); + LM_ERR("can't read reply for command \"%.*s\" from " + "RTPEngine <%s>\n", + cmd.len, cmd.s, node->rn_url.s); goto badproxy; } - if (len >= (v[0].iov_len - 1) && - memcmp(buf, v[0].iov_base, (v[0].iov_len - 1)) == 0) { + if(len >= (v[0].iov_len - 1) + && memcmp(buf, v[0].iov_base, (v[0].iov_len - 1)) + == 0) { len -= (v[0].iov_len - 1); cp += (v[0].iov_len - 1); - if (len != 0) { + if(len != 0) { len--; cp++; } @@ -3126,10 +3207,11 @@ send_rtpp_command(struct rtpp_node *node, bencode_item_t *dict, int *outlen) fds[0].revents = 0; } } - if (i == rtpengine_retr) { + if(i == rtpengine_retr) { bencode_get_str(bencode_dictionary_get(dict, "command"), &cmd); - LM_ERR("timeout waiting reply for command \"%.*s\" from RTPEngine <%s>\n", - cmd.len, cmd.s, node->rn_url.s); + LM_ERR("timeout waiting reply for command \"%.*s\" from RTPEngine " + "<%s>\n", + cmd.len, cmd.s, node->rn_url.s); goto badproxy; } } @@ -3147,26 +3229,29 @@ send_rtpp_command(struct rtpp_node *node, bencode_item_t *dict, int *outlen) * select the set with the id_set id */ -static struct rtpp_set * select_rtpp_set(unsigned int id_set ){ +static struct rtpp_set *select_rtpp_set(unsigned int id_set) +{ - struct rtpp_set * rtpp_list; + struct rtpp_set *rtpp_list; /*is it a valid set_id?*/ - if (!rtpp_set_list) { + if(!rtpp_set_list) { LM_ERR("no rtpp_set_list\n"); return 0; } lock_get(rtpp_set_list->rset_head_lock); - if (!rtpp_set_list->rset_first) { + if(!rtpp_set_list->rset_first) { LM_ERR("no rtpp_set_list->rset_first\n"); lock_release(rtpp_set_list->rset_head_lock); return 0; } - for (rtpp_list=rtpp_set_list->rset_first; rtpp_list!=0 && - rtpp_list->id_set!=id_set; rtpp_list=rtpp_list->rset_next); - if (!rtpp_list) { + for(rtpp_list = rtpp_set_list->rset_first; + rtpp_list != 0 && rtpp_list->id_set != id_set; + rtpp_list = rtpp_list->rset_next) + ; + if(!rtpp_list) { LM_ERR(" script error-invalid id_set to be selected\n"); } lock_release(rtpp_set_list->rset_head_lock); @@ -3177,28 +3262,29 @@ static struct rtpp_set * select_rtpp_set(unsigned int id_set ){ /* * run the selection algorithm and return the new selected node */ -static struct rtpp_node * -select_rtpp_node_new(str callid, str viabranch, int do_test, struct rtpp_node **queried_nodes_ptr, int queried_nodes) +static struct rtpp_node *select_rtpp_node_new(str callid, str viabranch, + int do_test, struct rtpp_node **queried_nodes_ptr, int queried_nodes) { - struct rtpp_node* node; + struct rtpp_node *node; unsigned i, sum, sumcut, weight_sum; int was_forced = 0; str hash_data; - switch (hash_algo) { + switch(hash_algo) { case RTP_HASH_CALLID: hash_data = callid; break; case RTP_HASH_SHA1_CALLID: - if (rtpengine_cb.SHA1 == NULL) { + if(rtpengine_cb.SHA1 == NULL) { /* don't throw warning here; there is already a warni*/ - LM_BUG("SHA1 algo set but crypto not loaded! Program shouldn't have started!"); + LM_BUG("SHA1 algo set but crypto not loaded! Program shouldn't " + "have started!"); return NULL; } - if (rtpengine_cb.SHA1(&callid, &hash_data) < 0) { + if(rtpengine_cb.SHA1(&callid, &hash_data) < 0) { LM_ERR("SHA1 hash in crypto module failed!\n"); return NULL; } @@ -3219,7 +3305,7 @@ select_rtpp_node_new(str callid, str viabranch, int do_test, struct rtpp_node ** /* FIXME this seems to affect the algorithm in a negative way * legacy code uses it; disable it for other algos */ - if (hash_algo == RTP_HASH_CALLID) { + if(hash_algo == RTP_HASH_CALLID) { sum &= 0xff; } @@ -3228,40 +3314,42 @@ select_rtpp_node_new(str callid, str viabranch, int do_test, struct rtpp_node ** weight_sum = 0; lock_get(active_rtpp_set->rset_lock); - for (node=active_rtpp_set->rn_first; node!=NULL; node=node->rn_next) { + for(node = active_rtpp_set->rn_first; node != NULL; node = node->rn_next) { /* Select only between displayed machines */ - if (!node->rn_displayed) { + if(!node->rn_displayed) { continue; } /* Try to enable if it's time to try. */ - if (node->rn_disabled && node->rn_recheck_ticks <= get_ticks()){ + if(node->rn_disabled && node->rn_recheck_ticks <= get_ticks()) { node->rn_disabled = rtpp_test(node, 1, 0); } /* Select only between enabled machines */ - if (!node->rn_disabled && !is_queried_node(node, queried_nodes_ptr, queried_nodes)) { + if(!node->rn_disabled + && !is_queried_node(node, queried_nodes_ptr, queried_nodes)) { weight_sum += node->rn_weight; } } lock_release(active_rtpp_set->rset_lock); /* No proxies? Force all to be redetected, if not yet */ - if (weight_sum == 0) { - if (!cfg_get(rtpengine,rtpengine_cfg,aggressive_redetection)) { + if(weight_sum == 0) { + if(!cfg_get(rtpengine, rtpengine_cfg, aggressive_redetection)) { return NULL; } - if (was_forced) { + if(was_forced) { return NULL; } was_forced = 1; lock_get(active_rtpp_set->rset_lock); - for(node=active_rtpp_set->rn_first; node!=NULL; node=node->rn_next) { + for(node = active_rtpp_set->rn_first; node != NULL; + node = node->rn_next) { /* Select only between displayed machines */ - if (!node->rn_displayed) { + if(!node->rn_displayed) { continue; } @@ -3279,22 +3367,22 @@ select_rtpp_node_new(str callid, str viabranch, int do_test, struct rtpp_node ** * Scan proxy list and decrease until appropriate proxy is found. */ lock_get(active_rtpp_set->rset_lock); - for (node=active_rtpp_set->rn_first; node!=NULL; node=node->rn_next) { + for(node = active_rtpp_set->rn_first; node != NULL; node = node->rn_next) { /* Select only between displayed machines */ - if (!node->rn_displayed) { + if(!node->rn_displayed) { continue; } /* Select only between enabled machines */ - if (node->rn_disabled) + if(node->rn_disabled) continue; /* Select only between not already queried machines */ - if (is_queried_node(node, queried_nodes_ptr, queried_nodes)) + if(is_queried_node(node, queried_nodes_ptr, queried_nodes)) continue; /* Found machine */ - if (sumcut < node->rn_weight) { + if(sumcut < node->rn_weight) { lock_release(active_rtpp_set->rset_lock); goto found; } @@ -3308,10 +3396,10 @@ select_rtpp_node_new(str callid, str viabranch, int do_test, struct rtpp_node ** return NULL; found: - if (do_test) { + if(do_test) { lock_get(active_rtpp_set->rset_lock); node->rn_disabled = rtpp_test(node, node->rn_disabled, 0); - if (node->rn_disabled) { + if(node->rn_disabled) { lock_release(active_rtpp_set->rset_lock); goto retry; } @@ -3325,29 +3413,34 @@ select_rtpp_node_new(str callid, str viabranch, int do_test, struct rtpp_node ** /* * lookup the hastable (key=callid value=node) and get the old node (e.g. for answer/delete) */ -static struct rtpp_node * -select_rtpp_node_old(str callid, str viabranch, int do_test, enum rtpe_operation op) +static struct rtpp_node *select_rtpp_node_old( + str callid, str viabranch, int do_test, enum rtpe_operation op) { struct rtpp_node *node = NULL; node = rtpengine_hash_table_lookup(callid, viabranch, op); - if (!node) { - LM_DBG("rtpengine hash table lookup failed to find node for calllen=%d callid=%.*s viabranch=%.*s\n", - callid.len, callid.len, callid.s, viabranch.len, viabranch.s); + if(!node) { + LM_DBG("rtpengine hash table lookup failed to find node for calllen=%d " + "callid=%.*s viabranch=%.*s\n", + callid.len, callid.len, callid.s, viabranch.len, viabranch.s); return NULL; } else { - LM_DBG("rtpengine hash table lookup find node=%.*s for calllen=%d callid=%.*s viabranch=%.*s\n", - node->rn_url.len, node->rn_url.s, callid.len, callid.len, callid.s, viabranch.len, viabranch.s); + LM_DBG("rtpengine hash table lookup find node=%.*s for calllen=%d " + "callid=%.*s viabranch=%.*s\n", + node->rn_url.len, node->rn_url.s, callid.len, callid.len, + callid.s, viabranch.len, viabranch.s); } return node; } -unsigned int node_in_set(struct rtpp_node *node, struct rtpp_set *set) { +unsigned int node_in_set(struct rtpp_node *node, struct rtpp_set *set) +{ struct rtpp_node *current = set->rn_first; - while (current) { - if (current->idx == node->idx) return 1; + while(current) { + if(current->idx == node->idx) + return 1; current = current->rn_next; } return 0; @@ -3357,22 +3450,23 @@ unsigned int node_in_set(struct rtpp_node *node, struct rtpp_set *set) { * Main balancing routine. This DO try to keep the same proxy for * the call if some proxies were disabled or enabled (e.g. kamctl command) */ -static struct rtpp_node * -select_rtpp_node(str callid, str viabranch, int do_test, struct rtpp_node **queried_nodes_ptr, int queried_nodes, enum rtpe_operation op) +static struct rtpp_node *select_rtpp_node(str callid, str viabranch, + int do_test, struct rtpp_node **queried_nodes_ptr, int queried_nodes, + enum rtpe_operation op) { struct rtpp_node *node = NULL; - if (build_rtpp_socks(1, 0)) { + if(build_rtpp_socks(1, 0)) { LM_ERR("out of memory\n"); return NULL; } - if (!active_rtpp_set) { + if(!active_rtpp_set) { default_rtpp_set = select_rtpp_set(setid_default); active_rtpp_set = default_rtpp_set; } - if (!active_rtpp_set) { + if(!active_rtpp_set) { LM_ERR("script error - no valid set selected\n"); return NULL; } @@ -3380,41 +3474,48 @@ select_rtpp_node(str callid, str viabranch, int do_test, struct rtpp_node **quer // lookup node node = select_rtpp_node_old(callid, viabranch, do_test, op); - if (node && is_queried_node(node, queried_nodes_ptr, queried_nodes)) { - LM_ERR("rtpengine node for callid=%.*s is known (%.*s) but it has already been queried, therefore not returning it\n", - callid.len, callid.s, node->rn_url.len, node->rn_url.s); + if(node && is_queried_node(node, queried_nodes_ptr, queried_nodes)) { + LM_ERR("rtpengine node for callid=%.*s is known (%.*s) but it has " + "already been queried, therefore not returning it\n", + callid.len, callid.s, node->rn_url.len, node->rn_url.s); return NULL; } // check node - if (!node || (node_in_set(node, active_rtpp_set) == 0)) { + if(!node || (node_in_set(node, active_rtpp_set) == 0)) { // run the selection algorithm - node = select_rtpp_node_new(callid, viabranch, do_test, queried_nodes_ptr, queried_nodes); + node = select_rtpp_node_new( + callid, viabranch, do_test, queried_nodes_ptr, queried_nodes); // check node - if (!node) { - LM_ERR("rtpengine failed to select new for calllen=%d callid=%.*s\n", - callid.len, callid.len, callid.s); + if(!node) { + LM_ERR("rtpengine failed to select new for calllen=%d " + "callid=%.*s\n", + callid.len, callid.len, callid.s); return NULL; } } // if node enabled, return it - if (!node->rn_disabled) { + if(!node->rn_disabled) { return node; } // if proper configuration and node manually or timeout disabled, return it - if (rtpengine_allow_op) { - if (node->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS) { - LM_DBG("node=%.*s for calllen=%d callid=%.*s is disabled(permanent) (probably still UP)! Return it\n", - node->rn_url.len, node->rn_url.s, callid.len, callid.len, callid.s); + if(rtpengine_allow_op) { + if(node->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS) { + LM_DBG("node=%.*s for calllen=%d callid=%.*s is " + "disabled(permanent) (probably still UP)! Return it\n", + node->rn_url.len, node->rn_url.s, callid.len, callid.len, + callid.s); return node; } else { - LM_DBG("node=%.*s for calllen=%d callid=%.*s is disabled, either broke or timeout disabled!\n", - node->rn_url.len, node->rn_url.s, callid.len, callid.len, callid.s); - if (rtpengine_allow_op == 1) { - LM_DBG("Return it\n"); + LM_DBG("node=%.*s for calllen=%d callid=%.*s is disabled, either " + "broke or timeout disabled!\n", + node->rn_url.len, node->rn_url.s, callid.len, callid.len, + callid.s); + if(rtpengine_allow_op == 1) { + LM_DBG("Return it\n"); return node; } } @@ -3423,37 +3524,37 @@ select_rtpp_node(str callid, str viabranch, int do_test, struct rtpp_node **quer return NULL; } -static int -get_extra_id(struct sip_msg* msg, str *id_str) { - if (msg == NULL || extra_id_pv == NULL || id_str == NULL) { +static int get_extra_id(struct sip_msg *msg, str *id_str) +{ + if(msg == NULL || extra_id_pv == NULL || id_str == NULL) { LM_ERR("bad parameters\n"); return -1; } - if (pv_printf_s(msg, extra_id_pv, id_str) < 0) { + if(pv_printf_s(msg, extra_id_pv, id_str) < 0) { LM_ERR("cannot print the additional id\n"); return -1; } return 1; - } -static int -set_rtpengine_set_from_avp(struct sip_msg *msg, int direction) +static int set_rtpengine_set_from_avp(struct sip_msg *msg, int direction) { struct usr_avp *avp; int_str setid_val; - if ((setid_avp_param == NULL) || - (avp = search_first_avp(setid_avp_type, setid_avp, &setid_val, 0)) == NULL) { - if (direction == 1 || !selected_rtpp_set_2) + if((setid_avp_param == NULL) + || (avp = search_first_avp( + setid_avp_type, setid_avp, &setid_val, 0)) + == NULL) { + if(direction == 1 || !selected_rtpp_set_2) active_rtpp_set = selected_rtpp_set_1; else active_rtpp_set = selected_rtpp_set_2; return 1; } - if (avp->flags&AVP_VAL_STR) { + if(avp->flags & AVP_VAL_STR) { LM_ERR("setid_avp must hold an integer value\n"); return -1; } @@ -3471,10 +3572,11 @@ set_rtpengine_set_from_avp(struct sip_msg *msg, int direction) return 1; } -static void avp_print_s(pv_elem_t *pv, char *str, int len, struct sip_msg *msg) { +static void avp_print_s(pv_elem_t *pv, char *str, int len, struct sip_msg *msg) +{ pv_value_t val; - if (!pv) + if(!pv) return; memset(&val, 0, sizeof(val)); @@ -3484,21 +3586,24 @@ static void avp_print_s(pv_elem_t *pv, char *str, int len, struct sip_msg *msg) pv->spec->setf(msg, &pv->spec->pvp, EQ_T, &val); } -static void avp_print_decimal(pv_elem_t *pv, int num, struct sip_msg *msg) { +static void avp_print_decimal(pv_elem_t *pv, int num, struct sip_msg *msg) +{ int len; char buf[8]; len = snprintf(buf, sizeof(buf), "%i.%i", num / 10, abs(num % 10)); avp_print_s(pv, buf, len, msg); } -static void avp_print_int(pv_elem_t *pv, int num, struct sip_msg *msg) { +static void avp_print_int(pv_elem_t *pv, int num, struct sip_msg *msg) +{ int len; char buf[8]; len = snprintf(buf, sizeof(buf), "%i", num); avp_print_s(pv, buf, len, msg); } -static void avp_print_time(pv_elem_t *pv, int num, struct sip_msg *msg) { +static void avp_print_time(pv_elem_t *pv, int num, struct sip_msg *msg) +{ int len; char buf[8]; @@ -3506,10 +3611,10 @@ static void avp_print_time(pv_elem_t *pv, int num, struct sip_msg *msg) { avp_print_s(pv, buf, len, msg); } -static void avp_print_mos(struct minmax_mos_stats *s, struct minmax_stats_vals *vals, long long created, - struct sip_msg *msg) +static void avp_print_mos(struct minmax_mos_stats *s, + struct minmax_stats_vals *vals, long long created, struct sip_msg *msg) { - if (!vals->avg_samples) + if(!vals->avg_samples) return; avp_print_decimal(s->mos_pv, vals->mos / vals->avg_samples, msg); @@ -3517,31 +3622,37 @@ static void avp_print_mos(struct minmax_mos_stats *s, struct minmax_stats_vals * avp_print_int(s->packetloss_pv, vals->packetloss / vals->avg_samples, msg); avp_print_int(s->jitter_pv, vals->jitter / vals->avg_samples, msg); avp_print_int(s->roundtrip_pv, vals->roundtrip / vals->avg_samples, msg); - avp_print_int(s->roundtrip_leg_pv, vals->roundtrip_leg / vals->avg_samples, msg); + avp_print_int( + s->roundtrip_leg_pv, vals->roundtrip_leg / vals->avg_samples, msg); avp_print_int(s->samples_pv, vals->samples / vals->avg_samples, msg); } -static int decode_mos_vals_dict(struct minmax_stats_vals *vals, bencode_item_t *dict, const char *key) { +static int decode_mos_vals_dict( + struct minmax_stats_vals *vals, bencode_item_t *dict, const char *key) +{ bencode_item_t *mos_ent; mos_ent = bencode_dictionary_get_expect(dict, key, BENCODE_DICTIONARY); - if (!mos_ent) + if(!mos_ent) return 0; vals->mos = bencode_dictionary_get_integer(mos_ent, "MOS", -1); vals->at = bencode_dictionary_get_integer(mos_ent, "reported at", -1); - vals->packetloss = bencode_dictionary_get_integer(mos_ent, "packet loss", -1); + vals->packetloss = + bencode_dictionary_get_integer(mos_ent, "packet loss", -1); vals->jitter = bencode_dictionary_get_integer(mos_ent, "jitter", -1); - vals->roundtrip = bencode_dictionary_get_integer(mos_ent, "round-trip time", -1); - vals->roundtrip_leg = bencode_dictionary_get_integer(mos_ent, "round-trip time leg", -1); + vals->roundtrip = + bencode_dictionary_get_integer(mos_ent, "round-trip time", -1); + vals->roundtrip_leg = + bencode_dictionary_get_integer(mos_ent, "round-trip time leg", -1); vals->samples = bencode_dictionary_get_integer(mos_ent, "samples", -1); vals->avg_samples = 1; return 1; } -static void parse_call_stats_1(struct minmax_mos_label_stats *mmls, bencode_item_t *dict, - struct sip_msg *msg) +static void parse_call_stats_1(struct minmax_mos_label_stats *mmls, + bencode_item_t *dict, struct sip_msg *msg) { long long created; str label, check; @@ -3549,29 +3660,19 @@ static void parse_call_stats_1(struct minmax_mos_label_stats *mmls, bencode_item unsigned int num_ssrcs = 0, i; long long ssrc; char *endp; - bencode_item_t *ssrc_list, - *ssrc_key, - *ssrc_dict, - *tags, - *tag_key, - *tag_dict, - *medias, - *media, - *streams, - *stream; - struct minmax_stats_vals min_vals = { .mos = 100 }, - max_vals = { .mos = -1 }, - average_vals = { .avg_samples = 0 }, - vals_decoded; - - if (!mmls->got_any_pvs) + bencode_item_t *ssrc_list, *ssrc_key, *ssrc_dict, *tags, *tag_key, + *tag_dict, *medias, *media, *streams, *stream; + struct minmax_stats_vals min_vals = {.mos = 100}, max_vals = {.mos = -1}, + average_vals = {.avg_samples = 0}, vals_decoded; + + if(!mmls->got_any_pvs) return; /* check if only a subset of info is requested */ - if (!mmls->label_pv) + if(!mmls->label_pv) goto ssrcs_done; - if (pv_printf_s(msg, mmls->label_pv, &label)) { + if(pv_printf_s(msg, mmls->label_pv, &label)) { LM_ERR("error printing label PV\n"); return; } @@ -3579,86 +3680,91 @@ static void parse_call_stats_1(struct minmax_mos_label_stats *mmls, bencode_item /* walk through tags to find the label we're looking for */ tags = bencode_dictionary_get_expect(dict, "tags", BENCODE_DICTIONARY); - if (!tags) + if(!tags) return; /* label wanted but no tags found - return nothing */ LM_DBG("rtpengine: XXX got tags\n"); - for (tag_key = tags->child; tag_key; tag_key = tag_key->sibling->sibling) { + for(tag_key = tags->child; tag_key; tag_key = tag_key->sibling->sibling) { LM_DBG("rtpengine: XXX got tag\n"); tag_dict = tag_key->sibling; /* compare label */ - if (!bencode_dictionary_get_str(tag_dict, "label", &check)) + if(!bencode_dictionary_get_str(tag_dict, "label", &check)) continue; LM_DBG("rtpengine: XXX got label %.*s\n", check.len, check.s); - if (str_cmp(&check, &label)) + if(str_cmp(&check, &label)) continue; LM_DBG("rtpengine: XXX label match\n"); - medias = bencode_dictionary_get_expect(tag_dict, "medias", BENCODE_LIST); - if (!medias) + medias = + bencode_dictionary_get_expect(tag_dict, "medias", BENCODE_LIST); + if(!medias) continue; LM_DBG("rtpengine: XXX got medias\n"); - for (media = medias->child; media; media = media->sibling) { + for(media = medias->child; media; media = media->sibling) { LM_DBG("rtpengine: XXX got media\n"); - streams = bencode_dictionary_get_expect(media, "streams", BENCODE_LIST); - if (!streams) + streams = bencode_dictionary_get_expect( + media, "streams", BENCODE_LIST); + if(!streams) continue; LM_DBG("rtpengine: XXX got streams\n"); /* only check the first stream (RTP) */ stream = streams->child; - if (!stream) + if(!stream) continue; LM_DBG("rtpengine: XXX got stream type %i\n", stream->type); - LM_DBG("rtpengine: XXX stream child '%.*s'\n", (int) stream->child->iov[1].iov_len, (char *) stream->child->iov[1].iov_base); - LM_DBG("rtpengine: XXX stream child val type %i\n", stream->child->sibling->type); - if ((ssrc = bencode_dictionary_get_integer(stream, "SSRC", -1)) == -1) + LM_DBG("rtpengine: XXX stream child '%.*s'\n", + (int)stream->child->iov[1].iov_len, + (char *)stream->child->iov[1].iov_base); + LM_DBG("rtpengine: XXX stream child val type %i\n", + stream->child->sibling->type); + if((ssrc = bencode_dictionary_get_integer(stream, "SSRC", -1)) + == -1) continue; /* got a valid SSRC to watch for */ ssrcs[num_ssrcs] = ssrc; - LM_DBG("rtpengine: found SSRC '%lli' for label '%.*s'\n", - ssrc, + LM_DBG("rtpengine: found SSRC '%lli' for label '%.*s'\n", ssrc, label.len, label.s); num_ssrcs++; /* see if we can do more */ - if (num_ssrcs >= (sizeof(ssrcs) / sizeof(*ssrcs))) + if(num_ssrcs >= (sizeof(ssrcs) / sizeof(*ssrcs))) goto ssrcs_done; } } /* if we get here, we were looking for label. see if we found one. if not, return nothing */ - if (num_ssrcs == 0) + if(num_ssrcs == 0) return; ssrcs_done: /* now look for the stats values */ created = bencode_dictionary_get_integer(dict, "created", 0); ssrc_list = bencode_dictionary_get_expect(dict, "SSRC", BENCODE_DICTIONARY); - if (!ssrc_list) + if(!ssrc_list) return; - for (ssrc_key = ssrc_list->child; ssrc_key; ssrc_key = ssrc_key->sibling->sibling) { + for(ssrc_key = ssrc_list->child; ssrc_key; + ssrc_key = ssrc_key->sibling->sibling) { /* see if this is a SSRC we're interested in */ - if (num_ssrcs == 0) + if(num_ssrcs == 0) goto ssrc_ok; - if (!bencode_get_str(ssrc_key, &check)) + if(!bencode_get_str(ssrc_key, &check)) continue; ssrc = strtoll(check.s, &endp, 10); - for (i = 0; i < num_ssrcs; i++) { - if (ssrcs[i] != ssrc) + for(i = 0; i < num_ssrcs; i++) { + if(ssrcs[i] != ssrc) continue; /* it's a match */ - LM_DBG("rtpengine: considering SSRC '%.*s'\n", - check.len, check.s); + LM_DBG("rtpengine: considering SSRC '%.*s'\n", check.len, check.s); goto ssrc_ok; } /* no match */ continue; -ssrc_ok: + ssrc_ok: ssrc_dict = ssrc_key->sibling; - if (!ssrc_dict) + if(!ssrc_dict) continue; - if (decode_mos_vals_dict(&vals_decoded, ssrc_dict, "average MOS")) { - if (vals_decoded.mos > 0) { + if(decode_mos_vals_dict(&vals_decoded, ssrc_dict, "average MOS")) { + if(vals_decoded.mos > 0) { average_vals.avg_samples++; average_vals.mos += vals_decoded.mos; average_vals.packetloss += vals_decoded.packetloss; @@ -3669,12 +3775,12 @@ static void parse_call_stats_1(struct minmax_mos_label_stats *mmls, bencode_item } } - if (decode_mos_vals_dict(&vals_decoded, ssrc_dict, "highest MOS")) { - if (vals_decoded.mos > max_vals.mos) + if(decode_mos_vals_dict(&vals_decoded, ssrc_dict, "highest MOS")) { + if(vals_decoded.mos > max_vals.mos) max_vals = vals_decoded; } - if (decode_mos_vals_dict(&vals_decoded, ssrc_dict, "lowest MOS")) { - if (vals_decoded.mos > 0 && vals_decoded.mos < min_vals.mos) + if(decode_mos_vals_dict(&vals_decoded, ssrc_dict, "lowest MOS")) { + if(vals_decoded.mos > 0 && vals_decoded.mos < min_vals.mos) min_vals = vals_decoded; } } @@ -3684,8 +3790,9 @@ static void parse_call_stats_1(struct minmax_mos_label_stats *mmls, bencode_item avp_print_mos(&mmls->average, &average_vals, created, msg); } -static void parse_call_stats(bencode_item_t *dict, struct sip_msg *msg) { - if (!got_any_mos_pvs) +static void parse_call_stats(bencode_item_t *dict, struct sip_msg *msg) +{ + if(!got_any_mos_pvs) return; parse_call_stats_1(&global_mos_stats, dict, msg); @@ -3693,50 +3800,54 @@ static void parse_call_stats(bencode_item_t *dict, struct sip_msg *msg) { parse_call_stats_1(&side_B_mos_stats, dict, msg); } -static int rtpengine_delete(struct sip_msg *msg, const char *flags) { +static int rtpengine_delete(struct sip_msg *msg, const char *flags) +{ bencode_buffer_t bencbuf; - bencode_item_t *ret = rtpp_function_call_ok(&bencbuf, msg, OP_DELETE, flags, NULL); - if (!ret) + bencode_item_t *ret = + rtpp_function_call_ok(&bencbuf, msg, OP_DELETE, flags, NULL); + if(!ret) return -1; parse_call_stats(ret, msg); bencode_buffer_free(&bencbuf); return 1; } -static int rtpengine_query(struct sip_msg *msg, const char *flags) { +static int rtpengine_query(struct sip_msg *msg, const char *flags) +{ bencode_buffer_t bencbuf; - bencode_item_t *ret = rtpp_function_call_ok(&bencbuf, msg, OP_QUERY, flags, NULL); - if (!ret) + bencode_item_t *ret = + rtpp_function_call_ok(&bencbuf, msg, OP_QUERY, flags, NULL); + if(!ret) return -1; parse_call_stats(ret, msg); bencode_buffer_free(&bencbuf); return 1; } -static int rtpengine_rtpp_set_wrap(struct sip_msg *msg, int (*func)(struct sip_msg *msg, void *, int, - enum rtpe_operation), +static int rtpengine_rtpp_set_wrap(struct sip_msg *msg, + int (*func)(struct sip_msg *msg, void *, int, enum rtpe_operation), void *data, int direction, enum rtpe_operation op) { int ret, more; body_intermediate.s = NULL; - if (set_rtpengine_set_from_avp(msg, direction) == -1) + if(set_rtpengine_set_from_avp(msg, direction) == -1) return -1; more = 1; - if (!selected_rtpp_set_2 || selected_rtpp_set_2 == selected_rtpp_set_1) + if(!selected_rtpp_set_2 || selected_rtpp_set_2 == selected_rtpp_set_1) more = 0; ret = func(msg, data, more, op); - if (ret < 0) + if(ret < 0) return ret; - if (!more) + if(!more) return ret; direction = (direction == 1) ? 2 : 1; - if (set_rtpengine_set_from_avp(msg, direction) == -1) + if(set_rtpengine_set_from_avp(msg, direction) == -1) return -1; ret = func(msg, data, 0, op); @@ -3744,19 +3855,21 @@ static int rtpengine_rtpp_set_wrap(struct sip_msg *msg, int (*func)(struct sip_m return ret; } -static int rtpengine_delete_wrap(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_delete_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ return rtpengine_delete(msg, d); } -static int rtpengine_rtpp_set_wrap_fparam(struct sip_msg *msg, int (*func)(struct sip_msg *msg, void *, int, - enum rtpe_operation), +static int rtpengine_rtpp_set_wrap_fparam(struct sip_msg *msg, + int (*func)(struct sip_msg *msg, void *, int, enum rtpe_operation), char *str1, int direction, enum rtpe_operation op) { str flags; flags.s = NULL; - if (str1) { - if (get_str_fparam(&flags, msg, (fparam_t *) str1)) { + if(str1) { + if(get_str_fparam(&flags, msg, (fparam_t *)str1)) { LM_ERR("Error getting string parameter\n"); return -1; } @@ -3765,27 +3878,29 @@ static int rtpengine_rtpp_set_wrap_fparam(struct sip_msg *msg, int (*func)(struc return rtpengine_rtpp_set_wrap(msg, func, flags.s, direction, op); } -static int -rtpengine_delete1_f(struct sip_msg* msg, char* str1, char* str2) +static int rtpengine_delete1_f(struct sip_msg *msg, char *str1, char *str2) { - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_delete_wrap, str1, 1, OP_DELETE); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_delete_wrap, str1, 1, OP_DELETE); } -static int rtpengine_query_wrap(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_query_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ return rtpengine_query(msg, d); } -static int -rtpengine_query1_f(struct sip_msg* msg, char* str1, char* str2) +static int rtpengine_query1_f(struct sip_msg *msg, char *str1, char *str2) { - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_query_wrap, str1, 1, OP_QUERY); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_query_wrap, str1, 1, OP_QUERY); } /* This function assumes p points to a line of requested type. */ -static int -set_rtpengine_set_n(struct sip_msg *msg, rtpp_set_link_t *rtpl, struct rtpp_set **out) +static int set_rtpengine_set_n( + struct sip_msg *msg, rtpp_set_link_t *rtpl, struct rtpp_set **out) { pv_value_t val; struct rtpp_node *node; @@ -3797,7 +3912,7 @@ set_rtpengine_set_n(struct sip_msg *msg, rtpp_set_link_t *rtpl, struct rtpp_set return 1; } - if(pv_get_spec_value(msg, rtpl->rpv, &val)<0) { + if(pv_get_spec_value(msg, rtpl->rpv, &val) < 0) { LM_ERR("cannot evaluate pv param\n"); return -1; } @@ -3806,7 +3921,7 @@ set_rtpengine_set_n(struct sip_msg *msg, rtpp_set_link_t *rtpl, struct rtpp_set return -1; } *out = select_rtpp_set(val.ri); - if(*out==NULL) { + if(*out == NULL) { LM_ERR("could not locate rtpengine set %ld\n", val.ri); return -1; } @@ -3814,35 +3929,31 @@ set_rtpengine_set_n(struct sip_msg *msg, rtpp_set_link_t *rtpl, struct rtpp_set lock_get((*out)->rset_lock); node = (*out)->rn_first; - while (node != NULL) - { - if (node->rn_disabled == 0) nb_active_nodes++; + while(node != NULL) { + if(node->rn_disabled == 0) + nb_active_nodes++; node = node->rn_next; } lock_release((*out)->rset_lock); - if ( nb_active_nodes > 0 ) - { + if(nb_active_nodes > 0) { LM_DBG("rtpp: selected proxy set ID %d with %d active nodes.\n", - (*out)->id_set, nb_active_nodes); + (*out)->id_set, nb_active_nodes); return nb_active_nodes; - } - else - { + } else { LM_WARN("rtpp: selected proxy set ID %d but it has no active node.\n", - (*out)->id_set); + (*out)->id_set); return -2; } } -static int -set_rtpengine_set_f(struct sip_msg * msg, char * str1, char * str2) +static int set_rtpengine_set_f(struct sip_msg *msg, char *str1, char *str2) { rtpp_set_link_t *rtpl1, *rtpl2; int ret; - rtpl1 = (rtpp_set_link_t*)str1; - rtpl2 = (rtpp_set_link_t*)str2; + rtpl1 = (rtpp_set_link_t *)str1; + rtpl2 = (rtpp_set_link_t *)str2; current_msg_id = 0; active_rtpp_set = 0; @@ -3850,78 +3961,79 @@ set_rtpengine_set_f(struct sip_msg * msg, char * str1, char * str2) selected_rtpp_set_2 = 0; ret = set_rtpengine_set_n(msg, rtpl1, &selected_rtpp_set_1); - if (ret < 0) + if(ret < 0) return ret; - if (rtpl2) { + if(rtpl2) { ret = set_rtpengine_set_n(msg, rtpl2, &selected_rtpp_set_2); - if (ret < 0) + if(ret < 0) return ret; } return 1; } -static int -rtpengine_manage(struct sip_msg *msg, const char *flags) +static int rtpengine_manage(struct sip_msg *msg, const char *flags) { int method; int nosdp; tm_cell_t *t = NULL; - if(route_type==BRANCH_FAILURE_ROUTE) { + if(route_type == BRANCH_FAILURE_ROUTE) { /* do nothing in branch failure event route * - delete done on transaction failure route */ return 1; } - if (msg->cseq==NULL && ((parse_headers(msg, HDR_CSEQ_F, 0)==-1) || - (msg->cseq==NULL))) { + if(msg->cseq == NULL + && ((parse_headers(msg, HDR_CSEQ_F, 0) == -1) + || (msg->cseq == NULL))) { LM_ERR("no CSEQ header\n"); return -1; } method = get_cseq(msg)->method_id; - if (!(method & (METHOD_INVITE|METHOD_ACK|METHOD_CANCEL|METHOD_BYE - |METHOD_UPDATE|METHOD_PRACK))) + if(!(method + & (METHOD_INVITE | METHOD_ACK | METHOD_CANCEL | METHOD_BYE + | METHOD_UPDATE | METHOD_PRACK))) return -1; - if (method & (METHOD_CANCEL|METHOD_BYE)) + if(method & (METHOD_CANCEL | METHOD_BYE)) return rtpengine_delete(msg, flags); - if (msg->msg_flags & FL_SDP_BODY) + if(msg->msg_flags & FL_SDP_BODY) nosdp = 0; else nosdp = parse_sdp(msg); - if (msg->first_line.type == SIP_REQUEST) { - if((method & (METHOD_ACK|METHOD_PRACK)) && nosdp==0) + if(msg->first_line.type == SIP_REQUEST) { + if((method & (METHOD_ACK | METHOD_PRACK)) && nosdp == 0) return rtpengine_offer_answer(msg, flags, OP_ANSWER, 0); - if(method==METHOD_UPDATE && nosdp==0) + if(method == METHOD_UPDATE && nosdp == 0) return rtpengine_offer_answer(msg, flags, OP_OFFER, 0); - if(method==METHOD_INVITE && nosdp==0) { + if(method == METHOD_INVITE && nosdp == 0) { msg->msg_flags |= FL_SDP_BODY; - if(tmb.t_gett!=NULL) { + if(tmb.t_gett != NULL) { t = tmb.t_gett(); - if(t!=NULL && t!=T_UNDEFINED && t->uas.request!=NULL) { + if(t != NULL && t != T_UNDEFINED && t->uas.request != NULL) { t->uas.request->msg_flags |= FL_SDP_BODY; } } - if(route_type==FAILURE_ROUTE) + if(route_type == FAILURE_ROUTE) return rtpengine_delete(msg, flags); return rtpengine_offer_answer(msg, flags, OP_OFFER, 0); } - } else if (msg->first_line.type == SIP_REPLY) { - if (msg->first_line.u.reply.statuscode>=300) + } else if(msg->first_line.type == SIP_REPLY) { + if(msg->first_line.u.reply.statuscode >= 300) return rtpengine_delete(msg, flags); - if (nosdp==0) { - if (method==METHOD_UPDATE) + if(nosdp == 0) { + if(method == METHOD_UPDATE) return rtpengine_offer_answer(msg, flags, OP_ANSWER, 0); - if (tmb.t_gett==NULL || tmb.t_gett()==NULL - || tmb.t_gett()==T_UNDEFINED) + if(tmb.t_gett == NULL || tmb.t_gett() == NULL + || tmb.t_gett() == T_UNDEFINED) return rtpengine_offer_answer(msg, flags, OP_ANSWER, 0); - if (tmb.t_gett()->uas.request->msg_flags & FL_SDP_BODY) + if(tmb.t_gett()->uas.request->msg_flags & FL_SDP_BODY) return rtpengine_offer_answer(msg, flags, OP_ANSWER, 0); return rtpengine_offer_answer(msg, flags, OP_OFFER, 0); } @@ -3929,50 +4041,56 @@ rtpengine_manage(struct sip_msg *msg, const char *flags) return -1; } -static int rtpengine_manage_wrap(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_manage_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ return rtpengine_manage(msg, d); } -static int -rtpengine_manage1_f(struct sip_msg *msg, char *str1, char *str2) +static int rtpengine_manage1_f(struct sip_msg *msg, char *str1, char *str2) { - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_manage_wrap, str1, 1, OP_ANY); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_manage_wrap, str1, 1, OP_ANY); } -static int -rtpengine_info1_f(struct sip_msg *msg, char *str1, char *str2) +static int rtpengine_info1_f(struct sip_msg *msg, char *str1, char *str2) { - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_simple_wrap, str1, 1, OP_OFFER); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_simple_wrap, str1, 1, OP_OFFER); } -static int rtpengine_offer_wrap(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_offer_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ return rtpengine_offer_answer(msg, d, OP_OFFER, more); } -static int -rtpengine_offer1_f(struct sip_msg *msg, char *str1, char *str2) +static int rtpengine_offer1_f(struct sip_msg *msg, char *str1, char *str2) { - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_offer_wrap, str1, 1, OP_OFFER); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_offer_wrap, str1, 1, OP_OFFER); } -static int rtpengine_answer_wrap(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_answer_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ return rtpengine_offer_answer(msg, d, OP_ANSWER, more); } -static int -rtpengine_answer1_f(struct sip_msg *msg, char *str1, char *str2) +static int rtpengine_answer1_f(struct sip_msg *msg, char *str1, char *str2) { - if (msg->first_line.type == SIP_REQUEST) - if (!(msg->first_line.u.request.method_value - & (METHOD_ACK | METHOD_PRACK))) + if(msg->first_line.type == SIP_REQUEST) + if(!(msg->first_line.u.request.method_value + & (METHOD_ACK | METHOD_PRACK))) return -1; - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_answer_wrap, str1, 2, OP_ANSWER); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_answer_wrap, str1, 2, OP_ANSWER); } -static int -rtpengine_offer_answer(struct sip_msg *msg, const char *flags, enum rtpe_operation op, int more) +static int rtpengine_offer_answer(struct sip_msg *msg, const char *flags, + enum rtpe_operation op, int more) { bencode_buffer_t bencbuf; bencode_item_t *dict; @@ -3982,34 +4100,36 @@ rtpengine_offer_answer(struct sip_msg *msg, const char *flags, enum rtpe_operati str cur_body = {0, 0}; dict = rtpp_function_call_ok(&bencbuf, msg, op, flags, &body); - if (!dict) + if(!dict) return -1; - if (!bencode_dictionary_get_str_dup(dict, "sdp", &newbody)) { + if(!bencode_dictionary_get_str_dup(dict, "sdp", &newbody)) { LM_ERR("failed to extract sdp body from proxy reply\n"); goto error; } - if (body_intermediate.s) + if(body_intermediate.s) pkg_free(body_intermediate.s); - if (more) + if(more) body_intermediate = newbody; else { - if (write_sdp_pvar!= NULL) { + if(write_sdp_pvar != NULL) { pv_val.rs = newbody; pv_val.flags = PV_VAL_STR; - if (write_sdp_pvar->setf(msg,&write_sdp_pvar->pvp, (int)EQ_T, &pv_val) < 0) - { - LM_ERR("error setting pvar <%.*s>\n", write_sdp_pvar_str.len, write_sdp_pvar_str.s); + if(write_sdp_pvar->setf( + msg, &write_sdp_pvar->pvp, (int)EQ_T, &pv_val) + < 0) { + LM_ERR("error setting pvar <%.*s>\n", write_sdp_pvar_str.len, + write_sdp_pvar_str.s); goto error_free; } pkg_free(newbody.s); } else { - if (read_sdp_pvar_str.len > 0) { + if(read_sdp_pvar_str.len > 0) { /* get the body from the message as body ptr may have changed * when using read_sdp_pv */ cur_body.len = 0; @@ -4020,11 +4140,11 @@ rtpengine_offer_answer(struct sip_msg *msg, const char *flags, enum rtpe_operati } else { anchor = del_lump(msg, body.s - msg->buf, body.len, 0); } - if (!anchor) { + if(!anchor) { LM_ERR("del_lump failed\n"); goto error_free; } - if (!insert_new_lump_after(anchor, newbody.s, newbody.len, 0)) { + if(!insert_new_lump_after(anchor, newbody.s, newbody.len, 0)) { LM_ERR("insert_new_lump_after failed\n"); goto error_free; } @@ -4042,61 +4162,56 @@ rtpengine_offer_answer(struct sip_msg *msg, const char *flags, enum rtpe_operati } -static int -rtpengine_generic_f(struct sip_msg* msg, char *str1, enum rtpe_operation op) +static int rtpengine_generic_f( + struct sip_msg *msg, char *str1, enum rtpe_operation op) { - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_simple_wrap, str1, 1, op); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_simple_wrap, str1, 1, op); } -static int -start_recording_f(struct sip_msg* msg, char *str1, char *str2) +static int start_recording_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_START_RECORDING); } -static int -stop_recording_f(struct sip_msg* msg, char *str1, char *str2) +static int stop_recording_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_STOP_RECORDING); } -static int -block_dtmf_f(struct sip_msg* msg, char *str1, char *str2) +static int block_dtmf_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_BLOCK_DTMF); } -static int -unblock_dtmf_f(struct sip_msg* msg, char *str1, char *str2) +static int unblock_dtmf_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_UNBLOCK_DTMF); } -static int -block_media_f(struct sip_msg* msg, char *str1, char *str2) +static int block_media_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_BLOCK_MEDIA); } -static int -unblock_media_f(struct sip_msg* msg, char *str1, char *str2) +static int unblock_media_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_UNBLOCK_MEDIA); } -static int -silence_media_f(struct sip_msg* msg, char *str1, char *str2) +static int silence_media_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_SILENCE_MEDIA); } -static int -unsilence_media_f(struct sip_msg* msg, char *str1, char *str2) +static int unsilence_media_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_UNSILENCE_MEDIA); } -static int rtpengine_play_media(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_play_media( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ bencode_buffer_t bencbuf; long long duration; bencode_item_t *ret; @@ -4105,18 +4220,20 @@ static int rtpengine_play_media(struct sip_msg *msg, void *d, int more, enum rtp int retval = 1; ret = rtpp_function_call_ok(&bencbuf, msg, OP_PLAY_MEDIA, d, NULL); - if (!ret) + if(!ret) return -1; - if (media_duration_pvar) { + if(media_duration_pvar) { duration = bencode_dictionary_get_integer(ret, "duration", -1); snprintf(intbuf, sizeof(intbuf), "%lli", duration); memset(&val, 0, sizeof(val)); val.flags = PV_VAL_STR; val.rs.s = intbuf; val.rs.len = strlen(intbuf); - if (media_duration_pvar->setf(msg, &media_duration_pvar->pvp, (int)EQ_T, &val) < 0) - { - LM_ERR("error setting pvar <%.*s>\n", media_duration_pvar_str.len, media_duration_pvar_str.s); + if(media_duration_pvar->setf( + msg, &media_duration_pvar->pvp, (int)EQ_T, &val) + < 0) { + LM_ERR("error setting pvar <%.*s>\n", media_duration_pvar_str.len, + media_duration_pvar_str.s); retval = -1; } } @@ -4125,37 +4242,35 @@ static int rtpengine_play_media(struct sip_msg *msg, void *d, int more, enum rtp return retval; } -static int -play_media_f(struct sip_msg* msg, char *str1, char *str2) +static int play_media_f(struct sip_msg *msg, char *str1, char *str2) { - return rtpengine_rtpp_set_wrap_fparam(msg, rtpengine_play_media, str1, 1, OP_PLAY_MEDIA); + return rtpengine_rtpp_set_wrap_fparam( + msg, rtpengine_play_media, str1, 1, OP_PLAY_MEDIA); } -static int -stop_media_f(struct sip_msg* msg, char *str1, char *str2) +static int stop_media_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_STOP_MEDIA); } -static int -play_dtmf_f(struct sip_msg* msg, char *str1, char *str2) +static int play_dtmf_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_PLAY_DTMF); } -static int -start_forwarding_f(struct sip_msg* msg, char *str1, char *str2) +static int start_forwarding_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_START_FORWARDING); } -static int -stop_forwarding_f(struct sip_msg* msg, char *str1, char *str2) +static int stop_forwarding_f(struct sip_msg *msg, char *str1, char *str2) { return rtpengine_generic_f(msg, str1, OP_STOP_FORWARDING); } -static int rtpengine_rtpstat_wrap(struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { +static int rtpengine_rtpstat_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) +{ void **parms; pv_param_t *param; pv_value_t *res; @@ -4169,14 +4284,14 @@ static int rtpengine_rtpstat_wrap(struct sip_msg *msg, void *d, int more, enum r res = parms[1]; dict = rtpp_function_call_ok(&bencbuf, msg, OP_QUERY, NULL, NULL); - if (!dict) + if(!dict) return -1; tot = bencode_dictionary_get_expect(dict, "totals", BENCODE_DICTIONARY); rtp = bencode_dictionary_get_expect(tot, "RTP", BENCODE_DICTIONARY); rtcp = bencode_dictionary_get_expect(tot, "RTCP", BENCODE_DICTIONARY); - if (!rtp || !rtcp) + if(!rtp || !rtcp) goto error; ret.s = buf; @@ -4201,49 +4316,51 @@ static int rtpengine_rtpstat_wrap(struct sip_msg *msg, void *d, int more, enum r /* * Returns the current RTP-Statistics from the RTP-Proxy */ -static int -pv_get_rtpestat_f(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) +static int pv_get_rtpestat_f( + struct sip_msg *msg, pv_param_t *param, pv_value_t *res) { void *parms[2]; parms[0] = param; parms[1] = res; - return rtpengine_rtpp_set_wrap(msg, rtpengine_rtpstat_wrap, parms, 1, OP_ANY); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_rtpstat_wrap, parms, 1, OP_ANY); } /** * */ -static srjson_t *rtpengine_query_v_build_json(srjson_doc_t *jdoc, - bencode_item_t *dict) +static srjson_t *rtpengine_query_v_build_json( + srjson_doc_t *jdoc, bencode_item_t *dict) { srjson_t *vnode; srjson_t *tnode; bencode_item_t *it; str sval; - if(jdoc==NULL || dict==NULL) { + if(jdoc == NULL || dict == NULL) { LM_ERR("invalid parameters\n"); return NULL; } - switch (dict->type) { + switch(dict->type) { case BENCODE_STRING: - return srjson_CreateStr(jdoc, dict->iov[1].iov_base, dict->iov[1].iov_len); + return srjson_CreateStr( + jdoc, dict->iov[1].iov_base, dict->iov[1].iov_len); case BENCODE_INTEGER: return srjson_CreateNumber(jdoc, dict->value); case BENCODE_LIST: vnode = srjson_CreateArray(jdoc); - if(vnode==NULL) { + if(vnode == NULL) { LM_ERR("failed to create the array node\n"); return NULL; } - for (it = dict->child; it; it = it->sibling) { + for(it = dict->child; it; it = it->sibling) { tnode = rtpengine_query_v_build_json(jdoc, it); - if (!tnode) { + if(!tnode) { srjson_Delete(jdoc, vnode); return NULL; } @@ -4253,18 +4370,18 @@ static srjson_t *rtpengine_query_v_build_json(srjson_doc_t *jdoc, case BENCODE_DICTIONARY: vnode = srjson_CreateObject(jdoc); - if(vnode==NULL) { + if(vnode == NULL) { LM_ERR("failed to create the object node\n"); return NULL; } - for (it = dict->child; it; it = it->sibling) { + for(it = dict->child; it; it = it->sibling) { /* name of the item */ sval.s = it->iov[1].iov_base; sval.len = it->iov[1].iov_len; /* value of the item */ it = it->sibling; tnode = rtpengine_query_v_build_json(jdoc, it); - if (!tnode) { + if(!tnode) { srjson_Delete(jdoc, vnode); return NULL; } @@ -4281,16 +4398,15 @@ static srjson_t *rtpengine_query_v_build_json(srjson_doc_t *jdoc, /** * */ -static int rtpengine_query_v_print(bencode_item_t *dict, str *fmt, - str *bout) +static int rtpengine_query_v_print(bencode_item_t *dict, str *fmt, str *bout) { srjson_doc_t jdoc; - if(fmt==NULL || fmt->s==NULL || fmt->len<=0) { + if(fmt == NULL || fmt->s == NULL || fmt->len <= 0) { LM_ERR("invalid format parameter\n"); return -1; } - if(fmt->s[0]!='j' && fmt->s[0]!='J') { + if(fmt->s[0] != 'j' && fmt->s[0] != 'J') { LM_ERR("invalid format parameter value: %.*s\n", fmt->len, fmt->s); return -1; } @@ -4298,17 +4414,17 @@ static int rtpengine_query_v_print(bencode_item_t *dict, str *fmt, srjson_InitDoc(&jdoc, NULL); jdoc.root = rtpengine_query_v_build_json(&jdoc, dict); - if(jdoc.root==NULL) { + if(jdoc.root == NULL) { LM_ERR("failed to build json document\n"); return -1; } - if(fmt->len>1 && (fmt->s[1]=='p' || fmt->s[1]=='P')) { + if(fmt->len > 1 && (fmt->s[1] == 'p' || fmt->s[1] == 'P')) { bout->s = srjson_Print(&jdoc, jdoc.root); } else { bout->s = srjson_PrintUnformatted(&jdoc, jdoc.root); } - if(bout->s==NULL) { + if(bout->s == NULL) { LM_ERR("unable to serialize json document\n"); srjson_DestroyDoc(&jdoc); return -1; @@ -4322,8 +4438,8 @@ static int rtpengine_query_v_print(bencode_item_t *dict, str *fmt, /** * */ -static int rtpengine_query_v_wrap(struct sip_msg *msg, void *d, int more, - enum rtpe_operation op) +static int rtpengine_query_v_wrap( + struct sip_msg *msg, void *d, int more, enum rtpe_operation op) { void **parms; str *fmt = NULL; @@ -4337,10 +4453,10 @@ static int rtpengine_query_v_wrap(struct sip_msg *msg, void *d, int more, dst = parms[1]; dict = rtpp_function_call_ok(&bencbuf, msg, OP_QUERY, NULL, NULL); - if (!dict) { + if(!dict) { return -1; } - if(rtpengine_query_v_print(dict, fmt, &val.rs)<0) { + if(rtpengine_query_v_print(dict, fmt, &val.rs) < 0) { goto error; } @@ -4371,17 +4487,18 @@ static int ki_rtpengine_query_v(sip_msg_t *msg, str *fmt, str *dpv) pv_spec_t *dst; dst = pv_cache_get(dpv); - if(dst==NULL) { + if(dst == NULL) { LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s); return -1; } - if(dst->setf==NULL) { + if(dst->setf == NULL) { LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s); return -1; } parms[0] = fmt; parms[1] = dst; - return rtpengine_rtpp_set_wrap(msg, rtpengine_query_v_wrap, parms, 1, OP_ANY); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_query_v_wrap, parms, 1, OP_ANY); } /* @@ -4393,7 +4510,7 @@ static int w_rtpengine_query_v(sip_msg_t *msg, char *pfmt, char *pvar) str fmt = {NULL, 0}; pv_spec_t *dst; - if(fixup_get_svalue(msg, (gparam_t*)pfmt, &fmt) < 0 || fmt.len <= 0) { + if(fixup_get_svalue(msg, (gparam_t *)pfmt, &fmt) < 0 || fmt.len <= 0) { LM_ERR("fmt has no value\n"); return -1; } @@ -4402,7 +4519,8 @@ static int w_rtpengine_query_v(sip_msg_t *msg, char *pfmt, char *pvar) parms[0] = &fmt; parms[1] = dst; - return rtpengine_rtpp_set_wrap(msg, rtpengine_query_v_wrap, parms, 1, OP_ANY); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_query_v_wrap, parms, 1, OP_ANY); } /** @@ -4447,18 +4565,18 @@ static int fixup_free_rtpengine_query_v(void **param, int param_no) return -1; } -static int -set_rtp_inst_pvar(struct sip_msg *msg, const str * const uri) { +static int set_rtp_inst_pvar(struct sip_msg *msg, const str *const uri) +{ pv_value_t val; - if (rtp_inst_pvar == NULL) + if(rtp_inst_pvar == NULL) return 0; memset(&val, 0, sizeof(pv_value_t)); val.flags = PV_VAL_STR; val.rs = *uri; - if (rtp_inst_pvar->setf(msg, &rtp_inst_pvar->pvp, (int)EQ_T, &val) < 0) { + if(rtp_inst_pvar->setf(msg, &rtp_inst_pvar->pvp, (int)EQ_T, &val) < 0) { LM_ERR("Failed to add RTP Engine URI to pvar\n"); return -1; } @@ -4468,16 +4586,18 @@ set_rtp_inst_pvar(struct sip_msg *msg, const str * const uri) { /** * */ -static int ki_rtpengine_manage0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_manage_wrap, NULL, 1, OP_ANY); +static int ki_rtpengine_manage0(sip_msg_t *msg) +{ + return rtpengine_rtpp_set_wrap(msg, rtpengine_manage_wrap, NULL, 1, OP_ANY); } /** * */ -static int ki_rtpengine_manage(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_manage_wrap, ((flags && flags->len > 0) ? flags->s : NULL), 1, - OP_ANY); +static int ki_rtpengine_manage(sip_msg_t *msg, str *flags) +{ + return rtpengine_rtpp_set_wrap(msg, rtpengine_manage_wrap, + ((flags && flags->len > 0) ? flags->s : NULL), 1, OP_ANY); } static int ki_rtpengine_offer0(sip_msg_t *msg) @@ -4487,7 +4607,8 @@ static int ki_rtpengine_offer0(sip_msg_t *msg) static int ki_rtpengine_offer(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_offer_wrap, flags->s, 1, OP_ANY); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_offer_wrap, flags->s, 1, OP_ANY); } static int ki_rtpengine_answer0(sip_msg_t *msg) @@ -4497,7 +4618,8 @@ static int ki_rtpengine_answer0(sip_msg_t *msg) static int ki_rtpengine_answer(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_answer_wrap, flags->s, 2, OP_ANY); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_answer_wrap, flags->s, 2, OP_ANY); } static int ki_rtpengine_delete0(sip_msg_t *msg) @@ -4507,92 +4629,111 @@ static int ki_rtpengine_delete0(sip_msg_t *msg) static int ki_rtpengine_delete(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_delete_wrap, flags->s, 1, OP_ANY); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_delete_wrap, flags->s, 1, OP_ANY); } static int ki_rtpengine_query0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_query_wrap, NULL, 1, OP_ANY); + return rtpengine_rtpp_set_wrap(msg, rtpengine_query_wrap, NULL, 1, OP_ANY); } static int ki_rtpengine_query(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_query_wrap, flags->s, 1, OP_ANY); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_query_wrap, flags->s, 1, OP_ANY); } static int ki_start_recording(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_START_RECORDING); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_START_RECORDING); } static int ki_stop_recording(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_STOP_RECORDING); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_STOP_RECORDING); } static int ki_block_media0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_BLOCK_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_BLOCK_MEDIA); } static int ki_block_media(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_BLOCK_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_BLOCK_MEDIA); } static int ki_unblock_media0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_UNBLOCK_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_UNBLOCK_MEDIA); } -static int ki_unblock_media(sip_msg_t *msg , str *flags) +static int ki_unblock_media(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_UNBLOCK_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_UNBLOCK_MEDIA); } static int ki_silence_media0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_SILENCE_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_SILENCE_MEDIA); } static int ki_silence_media(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_SILENCE_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_SILENCE_MEDIA); } static int ki_unsilence_media0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_UNSILENCE_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_UNSILENCE_MEDIA); } -static int ki_unsilence_media(sip_msg_t *msg , str *flags) +static int ki_unsilence_media(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_UNSILENCE_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_UNSILENCE_MEDIA); } static int ki_block_dtmf0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_BLOCK_DTMF); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_BLOCK_DTMF); } static int ki_block_dtmf(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_BLOCK_DTMF); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_BLOCK_DTMF); } static int ki_unblock_dtmf0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_UNBLOCK_DTMF); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_UNBLOCK_DTMF); } static int ki_unblock_dtmf(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_UNBLOCK_DTMF); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_UNBLOCK_DTMF); } static int ki_play_media(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_PLAY_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_PLAY_MEDIA); } static int ki_stop_media0(sip_msg_t *msg) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, NULL, 1, OP_STOP_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, NULL, 1, OP_STOP_MEDIA); } static int ki_stop_media(sip_msg_t *msg, str *flags) { - return rtpengine_rtpp_set_wrap(msg, rtpengine_simple_wrap, flags->s, 1, OP_STOP_MEDIA); + return rtpengine_rtpp_set_wrap( + msg, rtpengine_simple_wrap, flags->s, 1, OP_STOP_MEDIA); } @@ -4605,7 +4746,7 @@ static int ki_set_rtpengine_set(sip_msg_t *msg, int r1) memset(&rtpl1, 0, sizeof(rtpp_set_link_t)); memset(&rtpl2, 0, sizeof(rtpp_set_link_t)); - if((rtpl1.rset = select_rtpp_set((unsigned int)r1)) ==0){ + if((rtpl1.rset = select_rtpp_set((unsigned int)r1)) == 0) { LM_ERR("rtpp_proxy set %d not configured\n", r1); return -1; } @@ -4616,7 +4757,7 @@ static int ki_set_rtpengine_set(sip_msg_t *msg, int r1) selected_rtpp_set_2 = 0; ret = set_rtpengine_set_n(msg, &rtpl1, &selected_rtpp_set_1); - if (ret < 0) + if(ret < 0) return ret; return 1; @@ -4631,11 +4772,11 @@ static int ki_set_rtpengine_set2(sip_msg_t *msg, int r1, int r2) memset(&rtpl1, 0, sizeof(rtpp_set_link_t)); memset(&rtpl2, 0, sizeof(rtpp_set_link_t)); - if((rtpl1.rset = select_rtpp_set((unsigned int)r1)) ==0){ + if((rtpl1.rset = select_rtpp_set((unsigned int)r1)) == 0) { LM_ERR("rtpp_proxy set %d not configured\n", r1); return -1; } - if((rtpl2.rset = select_rtpp_set((unsigned int)r2)) ==0){ + if((rtpl2.rset = select_rtpp_set((unsigned int)r2)) == 0) { LM_ERR("rtpp_proxy set %d not configured\n", r2); return -1; } @@ -4646,11 +4787,11 @@ static int ki_set_rtpengine_set2(sip_msg_t *msg, int r1, int r2) selected_rtpp_set_2 = 0; ret = set_rtpengine_set_n(msg, &rtpl1, &selected_rtpp_set_1); - if (ret < 0) + if(ret < 0) return ret; ret = set_rtpengine_set_n(msg, &rtpl2, &selected_rtpp_set_2); - if (ret < 0) + if(ret < 0) return ret; return 1; @@ -4821,7 +4962,8 @@ static sr_kemi_t sr_kemi_rtpengine_exports[] = { }; /* clang-format on */ -int mod_register(char *path, int *dlflags, void *p1, void *p2) { - sr_kemi_modules_add(sr_kemi_rtpengine_exports); - return 0; +int mod_register(char *path, int *dlflags, void *p1, void *p2) +{ + sr_kemi_modules_add(sr_kemi_rtpengine_exports); + return 0; } diff --git a/src/modules/rtpengine/rtpengine.h b/src/modules/rtpengine/rtpengine.h index 9092efbd96a..0adf16ecbef 100644 --- a/src/modules/rtpengine/rtpengine.h +++ b/src/modules/rtpengine/rtpengine.h @@ -27,69 +27,74 @@ #include "../../core/str.h" #include "../../core/locking.h" -#define RTPENGINE_MIN_RECHECK_TICKS 0 -#define RTPENGINE_MAX_RECHECK_TICKS ((unsigned int)-1) - -enum rtpe_operation { - OP_OFFER = 1, - OP_ANSWER, - OP_DELETE, - OP_START_RECORDING, - OP_STOP_RECORDING, - OP_QUERY, - OP_PING, - OP_BLOCK_DTMF, - OP_UNBLOCK_DTMF, - OP_BLOCK_MEDIA, - OP_UNBLOCK_MEDIA, - OP_SILENCE_MEDIA, - OP_UNSILENCE_MEDIA, - OP_START_FORWARDING, - OP_STOP_FORWARDING, - OP_PLAY_MEDIA, - OP_STOP_MEDIA, - OP_PLAY_DTMF, - - OP_ANY, +#define RTPENGINE_MIN_RECHECK_TICKS 0 +#define RTPENGINE_MAX_RECHECK_TICKS ((unsigned int)-1) + +enum rtpe_operation +{ + OP_OFFER = 1, + OP_ANSWER, + OP_DELETE, + OP_START_RECORDING, + OP_STOP_RECORDING, + OP_QUERY, + OP_PING, + OP_BLOCK_DTMF, + OP_UNBLOCK_DTMF, + OP_BLOCK_MEDIA, + OP_UNBLOCK_MEDIA, + OP_SILENCE_MEDIA, + OP_UNSILENCE_MEDIA, + OP_START_FORWARDING, + OP_STOP_FORWARDING, + OP_PLAY_MEDIA, + OP_STOP_MEDIA, + OP_PLAY_DTMF, + + OP_ANY, }; -struct rtpp_node { - unsigned int idx; /* overall index */ - str rn_url; /* unparsed, deletable */ - enum { +struct rtpp_node +{ + unsigned int idx; /* overall index */ + str rn_url; /* unparsed, deletable */ + enum + { RNU_UNKNOWN = -1, RNU_LOCAL = 0, RNU_UDP = 1, RNU_UDP6 = 6, RNU_WS = 2, RNU_WSS = 3, - } rn_umode; - char *rn_address; /* substring of rn_url */ - int rn_disabled; /* found unaccessible? */ - unsigned int rn_weight; /* for load balancing */ - unsigned int rn_displayed; /* for delete at db reload */ - unsigned int rn_recheck_ticks; - struct rtpp_node *rn_next; + } rn_umode; + char *rn_address; /* substring of rn_url */ + int rn_disabled; /* found unaccessible? */ + unsigned int rn_weight; /* for load balancing */ + unsigned int rn_displayed; /* for delete at db reload */ + unsigned int rn_recheck_ticks; + struct rtpp_node *rn_next; }; -struct rtpp_set { - unsigned int id_set; - unsigned int weight_sum; - unsigned int rtpp_node_count; - int set_disabled; - unsigned int set_recheck_ticks; - struct rtpp_node *rn_first; - struct rtpp_node *rn_last; - struct rtpp_set *rset_next; - gen_lock_t *rset_lock; +struct rtpp_set +{ + unsigned int id_set; + unsigned int weight_sum; + unsigned int rtpp_node_count; + int set_disabled; + unsigned int set_recheck_ticks; + struct rtpp_node *rn_first; + struct rtpp_node *rn_last; + struct rtpp_set *rset_next; + gen_lock_t *rset_lock; }; -struct rtpp_set_head { - struct rtpp_set *rset_first; - struct rtpp_set *rset_last; - gen_lock_t *rset_head_lock; +struct rtpp_set_head +{ + struct rtpp_set *rset_first; + struct rtpp_set *rset_last; + gen_lock_t *rset_head_lock; }; @@ -112,6 +117,11 @@ extern str rtpp_url_col; extern str rtpp_weight_col; extern str rtpp_disabled_col; -enum hash_algo_t { RTP_HASH_CALLID, RTP_HASH_SHA1_CALLID, RTP_HASH_CRC32_CALLID }; +enum hash_algo_t +{ + RTP_HASH_CALLID, + RTP_HASH_SHA1_CALLID, + RTP_HASH_CRC32_CALLID +}; #endif diff --git a/src/modules/rtpengine/rtpengine_db.c b/src/modules/rtpengine/rtpengine_db.c index f83be25e0c3..2f239d4a511 100644 --- a/src/modules/rtpengine/rtpengine_db.c +++ b/src/modules/rtpengine/rtpengine_db.c @@ -39,10 +39,9 @@ str rtpp_disabled_col = str_init("disabled"); static int rtpp_connect_db(void) { - if ((rtpp_db_url.s == NULL) || (rtpp_db_url.len == 0)) + if((rtpp_db_url.s == NULL) || (rtpp_db_url.len == 0)) return -1; - if ((rtpp_db_handle = rtpp_dbf.init(&rtpp_db_url)) == NULL) - { + if((rtpp_db_handle = rtpp_dbf.init(&rtpp_db_url)) == NULL) { LM_ERR("Cannot initialize db connection\n"); return -1; } @@ -51,8 +50,7 @@ static int rtpp_connect_db(void) static void rtpp_disconnect_db(void) { - if (rtpp_db_handle) - { + if(rtpp_db_handle) { rtpp_dbf.close(rtpp_db_handle); rtpp_db_handle = NULL; } @@ -65,7 +63,8 @@ static int rtpp_load_db(void) db1_res_t *res = NULL; db_val_t *values = NULL; db_row_t *rows = NULL; - db_key_t query_cols[] = {&rtpp_setid_col, &rtpp_url_col, &rtpp_weight_col, &rtpp_disabled_col}; + db_key_t query_cols[] = {&rtpp_setid_col, &rtpp_url_col, &rtpp_weight_col, + &rtpp_disabled_col}; str url; int disabled; @@ -75,18 +74,17 @@ static int rtpp_load_db(void) int n_rows = 0; int n_cols = 4; - if (rtpp_db_handle == NULL) - { + if(rtpp_db_handle == NULL) { LM_ERR("invalid db handle\n"); return -1; } - if (rtpp_dbf.use_table(rtpp_db_handle, &rtpp_table_name) < 0) - { - LM_ERR("unable to use table '%.*s'\n", rtpp_table_name.len, rtpp_table_name.s); + if(rtpp_dbf.use_table(rtpp_db_handle, &rtpp_table_name) < 0) { + LM_ERR("unable to use table '%.*s'\n", rtpp_table_name.len, + rtpp_table_name.s); return -1; } - if (rtpp_dbf.query(rtpp_db_handle, 0, 0, 0, query_cols, 0, n_cols, 0, &res) < 0) - { + if(rtpp_dbf.query(rtpp_db_handle, 0, 0, 0, query_cols, 0, n_cols, 0, &res) + < 0) { LM_ERR("error while running db query\n"); return -1; } @@ -95,23 +93,21 @@ static int rtpp_load_db(void) n_rows = RES_ROW_N(res); rows = RES_ROWS(res); - if (n_rows == 0) - { + if(n_rows == 0) { LM_WARN("No rtpengine instances in database\n"); rtpp_dbf.free_result(rtpp_db_handle, res); return 0; } - for (i=0; i(_str).s+(_str).len)\ - goto _error;\ - (_ptr) = (_ptr) + (_n);\ - }while(0); -#define one_of_16( _x , _t ) \ - (_x==_t[0]||_x==_t[15]||_x==_t[8]||_x==_t[2]||_x==_t[3]||_x==_t[4]\ - ||_x==_t[5]||_x==_t[6]||_x==_t[7]||_x==_t[1]||_x==_t[9]||_x==_t[10]\ - ||_x==_t[11]||_x==_t[12]||_x==_t[13]||_x==_t[14]) -#define one_of_8( _x , _t ) \ - (_x==_t[0]||_x==_t[7]||_x==_t[1]||_x==_t[2]||_x==_t[3]||_x==_t[4]\ - ||_x==_t[5]||_x==_t[6]) - +#define advance(_ptr, _n, _str, _error) \ + do { \ + if((_ptr) + (_n) > (_str).s + (_str).len) \ + goto _error; \ + (_ptr) = (_ptr) + (_n); \ + } while(0); +#define one_of_16(_x, _t) \ + (_x == _t[0] || _x == _t[15] || _x == _t[8] || _x == _t[2] || _x == _t[3] \ + || _x == _t[4] || _x == _t[5] || _x == _t[6] || _x == _t[7] \ + || _x == _t[1] || _x == _t[9] || _x == _t[10] || _x == _t[11] \ + || _x == _t[12] || _x == _t[13] || _x == _t[14]) +#define one_of_8(_x, _t) \ + (_x == _t[0] || _x == _t[7] || _x == _t[1] || _x == _t[2] || _x == _t[3] \ + || _x == _t[4] || _x == _t[5] || _x == _t[6]) /** @@ -68,80 +68,75 @@ */ int check_content_type(struct sip_msg *msg) { - static unsigned int appl[16] = { - 0x6c707061/*appl*/,0x6c707041/*Appl*/,0x6c705061/*aPpl*/, - 0x6c705041/*APpl*/,0x6c507061/*apPl*/,0x6c507041/*ApPl*/, - 0x6c505061/*aPPl*/,0x6c505041/*APPl*/,0x4c707061/*appL*/, - 0x4c707041/*AppL*/,0x4c705061/*aPpL*/,0x4c705041/*APpL*/, - 0x4c507061/*apPL*/,0x4c507041/*ApPL*/,0x4c505061/*aPPL*/, - 0x4c505041/*APPL*/}; - static unsigned int icat[16] = { - 0x74616369/*icat*/,0x74616349/*Icat*/,0x74614369/*iCat*/, - 0x74614349/*ICat*/,0x74416369/*icAt*/,0x74416349/*IcAt*/, - 0x74414369/*iCAt*/,0x74414349/*ICAt*/,0x54616369/*icaT*/, - 0x54616349/*IcaT*/,0x54614369/*iCaT*/,0x54614349/*ICaT*/, - 0x54416369/*icAT*/,0x54416349/*IcAT*/,0x54414369/*iCAT*/, - 0x54414349/*ICAT*/}; - static unsigned int ion_[8] = { - 0x006e6f69/*ion_*/,0x006e6f49/*Ion_*/,0x006e4f69/*iOn_*/, - 0x006e4f49/*IOn_*/,0x004e6f69/*ioN_*/,0x004e6f49/*IoN_*/, - 0x004e4f69/*iON_*/,0x004e4f49/*ION_*/}; - static unsigned int sdp_[8] = { - 0x00706473/*sdp_*/,0x00706453/*Sdp_*/,0x00704473/*sDp_*/, - 0x00704453/*SDp_*/,0x00506473/*sdP_*/,0x00506453/*SdP_*/, - 0x00504473/*sDP_*/,0x00504453/*SDP_*/}; - str str_type; - unsigned int x; - char *p; - - if (!msg->content_type) - { + static unsigned int appl[16] = {0x6c707061 /*appl*/, 0x6c707041 /*Appl*/, + 0x6c705061 /*aPpl*/, 0x6c705041 /*APpl*/, 0x6c507061 /*apPl*/, + 0x6c507041 /*ApPl*/, 0x6c505061 /*aPPl*/, 0x6c505041 /*APPl*/, + 0x4c707061 /*appL*/, 0x4c707041 /*AppL*/, 0x4c705061 /*aPpL*/, + 0x4c705041 /*APpL*/, 0x4c507061 /*apPL*/, 0x4c507041 /*ApPL*/, + 0x4c505061 /*aPPL*/, 0x4c505041 /*APPL*/}; + static unsigned int icat[16] = {0x74616369 /*icat*/, 0x74616349 /*Icat*/, + 0x74614369 /*iCat*/, 0x74614349 /*ICat*/, 0x74416369 /*icAt*/, + 0x74416349 /*IcAt*/, 0x74414369 /*iCAt*/, 0x74414349 /*ICAt*/, + 0x54616369 /*icaT*/, 0x54616349 /*IcaT*/, 0x54614369 /*iCaT*/, + 0x54614349 /*ICaT*/, 0x54416369 /*icAT*/, 0x54416349 /*IcAT*/, + 0x54414369 /*iCAT*/, 0x54414349 /*ICAT*/}; + static unsigned int ion_[8] = {0x006e6f69 /*ion_*/, 0x006e6f49 /*Ion_*/, + 0x006e4f69 /*iOn_*/, 0x006e4f49 /*IOn_*/, 0x004e6f69 /*ioN_*/, + 0x004e6f49 /*IoN_*/, 0x004e4f69 /*iON_*/, 0x004e4f49 /*ION_*/}; + static unsigned int sdp_[8] = {0x00706473 /*sdp_*/, 0x00706453 /*Sdp_*/, + 0x00704473 /*sDp_*/, 0x00704453 /*SDp_*/, 0x00506473 /*sdP_*/, + 0x00506453 /*SdP_*/, 0x00504473 /*sDP_*/, 0x00504453 /*SDP_*/}; + str str_type; + unsigned int x; + char *p; + + if(!msg->content_type) { LM_WARN("the header Content-TYPE is absent!" - "let's assume the content is text/plain ;-)\n"); + "let's assume the content is text/plain ;-)\n"); return 1; } - trim_len(str_type.len,str_type.s,msg->content_type->body); - if (str_type.len>=15 && (*str_type.s=='m' || *str_type.s=='M') + trim_len(str_type.len, str_type.s, msg->content_type->body); + if(str_type.len >= 15 && (*str_type.s == 'm' || *str_type.s == 'M') && strncasecmp(str_type.s, "multipart/mixed", 15) == 0) { return 2; - } + } p = str_type.s; - advance(p,4,str_type,error_1); - x = READ(p-4); - if (!one_of_16(x,appl)) + advance(p, 4, str_type, error_1); + x = READ(p - 4); + if(!one_of_16(x, appl)) goto other; - advance(p,4,str_type,error_1); - x = READ(p-4); - if (!one_of_16(x,icat)) + advance(p, 4, str_type, error_1); + x = READ(p - 4); + if(!one_of_16(x, icat)) goto other; - advance(p,3,str_type,error_1); - x = READ(p-3) & 0x00ffffff; - if (!one_of_8(x,ion_)) + advance(p, 3, str_type, error_1); + x = READ(p - 3) & 0x00ffffff; + if(!one_of_8(x, ion_)) goto other; /* skip spaces and tabs if any */ - while (*p==' ' || *p=='\t') - advance(p,1,str_type,error_1); - if (*p!='/') - { + while(*p == ' ' || *p == '\t') + advance(p, 1, str_type, error_1); + if(*p != '/') { LM_ERR("no / found after primary type\n"); goto error; } - advance(p,1,str_type,error_1); - while ((*p==' ' || *p=='\t') && p+1 found valid\n", (int)(p-str_type.s), str_type.s); + if(*p == ';' || *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' + || *p == 0) { + LM_DBG("type <%.*s> found valid\n", (int)(p - str_type.s), str_type.s); return 1; } else { LM_ERR("bad end for type!\n"); @@ -161,7 +156,7 @@ int check_content_type(struct sip_msg *msg) /* * Get message body and check Content-Type header field */ -int extract_body(struct sip_msg *msg, str *body ) +int extract_body(struct sip_msg *msg, str *body) { char c; int ret; @@ -171,7 +166,7 @@ int extract_body(struct sip_msg *msg, str *body ) unsigned int mime; body->s = get_body(msg); - if (body->s==0) { + if(body->s == 0) { LM_ERR("failed to get the message body\n"); goto error; } @@ -181,18 +176,18 @@ int extract_body(struct sip_msg *msg, str *body ) * parsing as get_body() parsed all headers and Content-Length * body header is automatically parsed when found. */ - if (msg->content_length==0) { + if(msg->content_length == 0) { LM_ERR("failed to get the content length in message\n"); goto error; } body->len = get_content_length(msg); - if (body->len==0) { + if(body->len == 0) { LM_ERR("message body has length zero\n"); goto error; } - if (body->len + body->s > msg->buf + msg->len) { + if(body->len + body->s > msg->buf + msg->len) { LM_ERR("content-length exceeds packet-length by %d\n", (int)((body->len + body->s) - (msg->buf + msg->len))); goto error; @@ -201,64 +196,65 @@ int extract_body(struct sip_msg *msg, str *body ) /* no need for parse_headers(msg, EOH), get_body will * parse everything */ /*is the content type correct?*/ - if((ret = check_content_type(msg))==-1) - { + if((ret = check_content_type(msg)) == -1) { LM_ERR("content type mismatching\n"); goto error; } - if(ret!=2) + if(ret != 2) goto done; /* multipart body */ - if(get_mixed_part_delimiter(&msg->content_type->body,&mpdel) < 0) { + if(get_mixed_part_delimiter(&msg->content_type->body, &mpdel) < 0) { goto error; } - p1 = find_sdp_line_delimiter(body->s, body->s+body->len, mpdel); - if (p1 == NULL) { + p1 = find_sdp_line_delimiter(body->s, body->s + body->len, mpdel); + if(p1 == NULL) { LM_ERR("empty multipart content\n"); return -1; } - p2=p1; + p2 = p1; c = 0; - for(;;) - { + for(;;) { p1 = p2; - if (p1 == NULL || p1 >= body->s+body->len) + if(p1 == NULL || p1 >= body->s + body->len) break; /* No parts left */ - p2 = find_next_sdp_line_delimiter(p1, body->s+body->len, - mpdel, body->s+body->len); + p2 = find_next_sdp_line_delimiter( + p1, body->s + body->len, mpdel, body->s + body->len); /* p2 is text limit for application parsing */ rest = eat_line(p1 + mpdel.len + 2, p2 - p1 - mpdel.len - 2); - if ( rest > p2 ) { - LM_ERR("Unparsable <%.*s>\n", (int)(p2-p1), p1); + if(rest > p2) { + LM_ERR("Unparsable <%.*s>\n", (int)(p2 - p1), p1); return -1; } - while( rest>16) == TYPE_APPLICATION) - && ((mime&0x00ff) == SUBTYPE_SDP)) { + if(((((unsigned int)mime) >> 16) == TYPE_APPLICATION) + && ((mime & 0x00ff) == SUBTYPE_SDP)) { c = 1; } } } /* end of while */ - if(c==1) - { - if (rest < p2 && *rest == '\r') rest++; - if (rest < p2 && *rest == '\n') rest++; - if (rest < p2 && p2[-1] == '\n') p2--; - if (rest < p2 && p2[-1] == '\r') p2--; + if(c == 1) { + if(rest < p2 && *rest == '\r') + rest++; + if(rest < p2 && *rest == '\n') + rest++; + if(rest < p2 && p2[-1] == '\n') + p2--; + if(rest < p2 && p2[-1] == '\r') + p2--; body->s = rest; - body->len = p2-rest; + body->len = p2 - rest; goto done; } } @@ -281,113 +277,107 @@ int extract_body(struct sip_msg *msg, str *body ) * (so make sure it is, before calling this function or * it might fail even if the message _has_ a callid) */ -int -get_callid(struct sip_msg* _m, str* _cid) +int get_callid(struct sip_msg *_m, str *_cid) { - if ((parse_headers(_m, HDR_CALLID_F, 0) == -1)) { - LM_ERR("failed to parse call-id header\n"); - return -1; - } + if((parse_headers(_m, HDR_CALLID_F, 0) == -1)) { + LM_ERR("failed to parse call-id header\n"); + return -1; + } - if (_m->callid == NULL) { - LM_ERR("call-id not found\n"); - return -1; - } + if(_m->callid == NULL) { + LM_ERR("call-id not found\n"); + return -1; + } - _cid->s = _m->callid->body.s; - _cid->len = _m->callid->body.len; - trim(_cid); - return 0; + _cid->s = _m->callid->body.s; + _cid->len = _m->callid->body.len; + trim(_cid); + return 0; } /* * Extract tag from To header field of a response */ -int -get_to_tag(struct sip_msg* _m, str* _tag) +int get_to_tag(struct sip_msg *_m, str *_tag) { - if (parse_to_header(_m) < 0) { - LM_ERR("To header field missing\n"); - return -1; - } + if(parse_to_header(_m) < 0) { + LM_ERR("To header field missing\n"); + return -1; + } - if (get_to(_m)->tag_value.len) { - _tag->s = get_to(_m)->tag_value.s; - _tag->len = get_to(_m)->tag_value.len; - } else { - _tag->s = NULL; /* fixes gcc 4.0 warnings */ - _tag->len = 0; - } + if(get_to(_m)->tag_value.len) { + _tag->s = get_to(_m)->tag_value.s; + _tag->len = get_to(_m)->tag_value.len; + } else { + _tag->s = NULL; /* fixes gcc 4.0 warnings */ + _tag->len = 0; + } - return 0; + return 0; } /* * Extract tag from From header field of a request */ -int -get_from_tag(struct sip_msg* _m, str* _tag) +int get_from_tag(struct sip_msg *_m, str *_tag) { - if (parse_from_header(_m)<0) { - LM_ERR("failed to parse From header\n"); - return -1; - } + if(parse_from_header(_m) < 0) { + LM_ERR("failed to parse From header\n"); + return -1; + } - if (get_from(_m)->tag_value.len) { - _tag->s = get_from(_m)->tag_value.s; - _tag->len = get_from(_m)->tag_value.len; - } else { - _tag->s = NULL; /* fixes gcc 4.0 warnings */ - _tag->len = 0; - } + if(get_from(_m)->tag_value.len) { + _tag->s = get_from(_m)->tag_value.s; + _tag->len = get_from(_m)->tag_value.len; + } else { + _tag->s = NULL; /* fixes gcc 4.0 warnings */ + _tag->len = 0; + } - return 0; + return 0; } /* * Extract URI from the Contact header field */ -int -get_contact_uri(struct sip_msg* _m, struct sip_uri *uri, contact_t** _c) +int get_contact_uri(struct sip_msg *_m, struct sip_uri *uri, contact_t **_c) { - if ((parse_headers(_m, HDR_CONTACT_F, 0) == -1) || !_m->contact) - return -1; - if (!_m->contact->parsed && parse_contact(_m->contact) < 0) { - LM_ERR("failed to parse Contact body\n"); - return -1; - } - *_c = ((contact_body_t*)_m->contact->parsed)->contacts; - if (*_c == NULL) - /* no contacts found */ - return -1; - - if (parse_uri((*_c)->uri.s, (*_c)->uri.len, uri) < 0 || uri->host.len <= 0) { - LM_ERR("failed to parse Contact URI [%.*s]\n", - (*_c)->uri.len, ((*_c)->uri.s)?(*_c)->uri.s:""); - return -1; - } - return 0; + if((parse_headers(_m, HDR_CONTACT_F, 0) == -1) || !_m->contact) + return -1; + if(!_m->contact->parsed && parse_contact(_m->contact) < 0) { + LM_ERR("failed to parse Contact body\n"); + return -1; + } + *_c = ((contact_body_t *)_m->contact->parsed)->contacts; + if(*_c == NULL) + /* no contacts found */ + return -1; + + if(parse_uri((*_c)->uri.s, (*_c)->uri.len, uri) < 0 || uri->host.len <= 0) { + LM_ERR("failed to parse Contact URI [%.*s]\n", (*_c)->uri.len, + ((*_c)->uri.s) ? (*_c)->uri.s : ""); + return -1; + } + return 0; } /* * Extract branch from Via header */ -int -get_via_branch(struct sip_msg* msg, int vianum, str* _branch) +int get_via_branch(struct sip_msg *msg, int vianum, str *_branch) { struct via_body *via; struct via_param *p; - if (parse_via_header(msg, vianum, &via) < 0) + if(parse_via_header(msg, vianum, &via) < 0) return -1; - for (p = via->param_lst; p; p = p->next) - { - if (p->name.len == strlen("branch") + for(p = via->param_lst; p; p = p->next) { + if(p->name.len == strlen("branch") && strncasecmp(p->name.s, "branch", strlen("branch")) == 0) { _branch->s = p->value.s; _branch->len = p->value.len; diff --git a/src/modules/rtpengine/rtpengine_funcs.h b/src/modules/rtpengine/rtpengine_funcs.h index 6071fb95d08..a1221bd2055 100644 --- a/src/modules/rtpengine/rtpengine_funcs.h +++ b/src/modules/rtpengine/rtpengine_funcs.h @@ -27,8 +27,8 @@ #include "../../core/parser/msg_parser.h" #include "../../core/parser/contact/contact.h" -int extract_body(struct sip_msg * , str *); -int check_content_type(struct sip_msg * ); +int extract_body(struct sip_msg *, str *); +int check_content_type(struct sip_msg *); int get_callid(struct sip_msg *, str *); int get_to_tag(struct sip_msg *, str *); int get_from_tag(struct sip_msg *, str *); diff --git a/src/modules/rtpengine/rtpengine_hash.c b/src/modules/rtpengine/rtpengine_hash.c index 770bf494e01..de6dbc9d5ed 100644 --- a/src/modules/rtpengine/rtpengine_hash.c +++ b/src/modules/rtpengine/rtpengine_hash.c @@ -12,27 +12,30 @@ static void rtpengine_hash_table_free_row_lock(gen_lock_t *row_lock); static struct rtpengine_hash_table *rtpengine_hash_table; /* from sipwise rtpengine */ -static int str_cmp_str(const str a, const str b) { - if (a.len < b.len) +static int str_cmp_str(const str a, const str b) +{ + if(a.len < b.len) return -1; - if (a.len > b.len) + if(a.len > b.len) return 1; - if (a.len == 0 && b.len == 0) + if(a.len == 0 && b.len == 0) return 0; return memcmp(a.s, b.s, a.len); } /* from sipwise rtpengine */ -static int str_equal(str a, str b) { +static int str_equal(str a, str b) +{ return (str_cmp_str(a, b) == 0); } /* from sipwise rtpengine */ -static unsigned int str_hash(str s) { +static unsigned int str_hash(str s) +{ unsigned int ret = 5381; str it = s; - while (it.len > 0) { + while(it.len > 0) { ret = (ret << 5) + ret + *it.s; it.s++; it.len--; @@ -42,12 +45,13 @@ static unsigned int str_hash(str s) { } /* rtpengine hash API */ -int rtpengine_hash_table_init(int size) { +int rtpengine_hash_table_init(int size) +{ int i; int hash_table_size; // init hash table size - if (size < 1) { + if(size < 1) { hash_table_size = 1; } else { hash_table_size = size; @@ -56,7 +60,7 @@ int rtpengine_hash_table_init(int size) { // init hashtable rtpengine_hash_table = shm_malloc(sizeof(struct rtpengine_hash_table)); - if (!rtpengine_hash_table) { + if(!rtpengine_hash_table) { LM_ERR("no shm left to create rtpengine_hash_table\n"); return 0; } @@ -64,57 +68,69 @@ int rtpengine_hash_table_init(int size) { rtpengine_hash_table->size = hash_table_size; // init hashtable row_locks - rtpengine_hash_table->row_locks = shm_malloc(hash_table_size * sizeof(gen_lock_t*)); - if (!rtpengine_hash_table->row_locks) { + rtpengine_hash_table->row_locks = + shm_malloc(hash_table_size * sizeof(gen_lock_t *)); + if(!rtpengine_hash_table->row_locks) { LM_ERR("no shm left to create rtpengine_hash_table->row_locks\n"); rtpengine_hash_table_destroy(); return 0; } - memset(rtpengine_hash_table->row_locks, 0, hash_table_size * sizeof(gen_lock_t*)); + memset(rtpengine_hash_table->row_locks, 0, + hash_table_size * sizeof(gen_lock_t *)); // init hashtable row_entry_list - rtpengine_hash_table->row_entry_list = shm_malloc(rtpengine_hash_table->size * sizeof(struct rtpengine_hash_entry*)); - if (!rtpengine_hash_table->row_entry_list) { + rtpengine_hash_table->row_entry_list = shm_malloc( + rtpengine_hash_table->size * sizeof(struct rtpengine_hash_entry *)); + if(!rtpengine_hash_table->row_entry_list) { LM_ERR("no shm left to create rtpengine_hash_table->row_entry_list\n"); rtpengine_hash_table_destroy(); return 0; } - memset(rtpengine_hash_table->row_entry_list, 0, rtpengine_hash_table->size * sizeof(struct rtpengine_hash_entry*)); + memset(rtpengine_hash_table->row_entry_list, 0, + rtpengine_hash_table->size * sizeof(struct rtpengine_hash_entry *)); // init hashtable row_totals - rtpengine_hash_table->row_totals = shm_malloc(hash_table_size * sizeof(unsigned int)); - if (!rtpengine_hash_table->row_totals) { + rtpengine_hash_table->row_totals = + shm_malloc(hash_table_size * sizeof(unsigned int)); + if(!rtpengine_hash_table->row_totals) { LM_ERR("no shm left to create rtpengine_hash_table->row_totals\n"); rtpengine_hash_table_destroy(); return 0; } - memset(rtpengine_hash_table->row_totals, 0, hash_table_size * sizeof(unsigned int)); + memset(rtpengine_hash_table->row_totals, 0, + hash_table_size * sizeof(unsigned int)); // init hashtable row_locks[i], row_entry_list[i] and row_totals[i] - for (i = 0; i < hash_table_size; i++) { + for(i = 0; i < hash_table_size; i++) { // alloc hashtable row_locks[i] rtpengine_hash_table->row_locks[i] = lock_alloc(); - if (!rtpengine_hash_table->row_locks[i]) { - LM_ERR("no shm left to create rtpengine_hash_table->row_locks[%d]\n", i); + if(!rtpengine_hash_table->row_locks[i]) { + LM_ERR("no shm left to create " + "rtpengine_hash_table->row_locks[%d]\n", + i); rtpengine_hash_table_destroy(); return 0; } // init hashtable row_locks[i] - if (!lock_init(rtpengine_hash_table->row_locks[i])) { + if(!lock_init(rtpengine_hash_table->row_locks[i])) { LM_ERR("fail to init rtpengine_hash_table->row_locks[%d]\n", i); rtpengine_hash_table_destroy(); return 0; } // init hashtable row_entry_list[i] - rtpengine_hash_table->row_entry_list[i] = shm_malloc(sizeof(struct rtpengine_hash_entry)); - if (!rtpengine_hash_table->row_entry_list[i]) { - LM_ERR("no shm left to create rtpengine_hash_table->row_entry_list[%d]\n", i); + rtpengine_hash_table->row_entry_list[i] = + shm_malloc(sizeof(struct rtpengine_hash_entry)); + if(!rtpengine_hash_table->row_entry_list[i]) { + LM_ERR("no shm left to create " + "rtpengine_hash_table->row_entry_list[%d]\n", + i); rtpengine_hash_table_destroy(); return 0; } - memset(rtpengine_hash_table->row_entry_list[i], 0, sizeof(struct rtpengine_hash_entry)); + memset(rtpengine_hash_table->row_entry_list[i], 0, + sizeof(struct rtpengine_hash_entry)); rtpengine_hash_table->row_entry_list[i]->tout = -1; rtpengine_hash_table->row_entry_list[i]->next = NULL; @@ -126,17 +142,18 @@ int rtpengine_hash_table_init(int size) { return 1; } -int rtpengine_hash_table_destroy() { +int rtpengine_hash_table_destroy() +{ int i; // check rtpengine hashtable - if (!rtpengine_hash_table) { + if(!rtpengine_hash_table) { LM_ERR("NULL rtpengine_hash_table\n"); return 1; } // check rtpengine hashtable->row_locks - if (!rtpengine_hash_table->row_locks) { + if(!rtpengine_hash_table->row_locks) { LM_ERR("NULL rtpengine_hash_table->row_locks\n"); shm_free(rtpengine_hash_table); rtpengine_hash_table = NULL; @@ -144,9 +161,9 @@ int rtpengine_hash_table_destroy() { } // destroy hashtable content - for (i = 0; i < rtpengine_hash_table->size; i++) { + for(i = 0; i < rtpengine_hash_table->size; i++) { // lock - if (!rtpengine_hash_table->row_locks[i]) { + if(!rtpengine_hash_table->row_locks[i]) { LM_ERR("NULL rtpengine_hash_table->row_locks[%d]\n", i); continue; } else { @@ -154,11 +171,12 @@ int rtpengine_hash_table_destroy() { } // check rtpengine hashtable->row_entry_list - if (!rtpengine_hash_table->row_entry_list) { + if(!rtpengine_hash_table->row_entry_list) { LM_ERR("NULL rtpengine_hash_table->row_entry_list\n"); } else { // destroy hashtable row_entry_list[i] - rtpengine_hash_table_free_row_entry_list(rtpengine_hash_table->row_entry_list[i]); + rtpengine_hash_table_free_row_entry_list( + rtpengine_hash_table->row_entry_list[i]); rtpengine_hash_table->row_entry_list[i] = NULL; } @@ -171,7 +189,7 @@ int rtpengine_hash_table_destroy() { } // destroy hashtable row_entry_list - if (!rtpengine_hash_table->row_entry_list) { + if(!rtpengine_hash_table->row_entry_list) { LM_ERR("NULL rtpengine_hash_table->row_entry_list\n"); } else { shm_free(rtpengine_hash_table->row_entry_list); @@ -179,7 +197,7 @@ int rtpengine_hash_table_destroy() { } // destroy hashtable row_totals - if (!rtpengine_hash_table->row_totals) { + if(!rtpengine_hash_table->row_totals) { LM_ERR("NULL rtpengine_hash_table->row_totals\n"); } else { shm_free(rtpengine_hash_table->row_totals); @@ -187,7 +205,7 @@ int rtpengine_hash_table_destroy() { } // destroy hashtable row_locks - if (!rtpengine_hash_table->row_locks) { + if(!rtpengine_hash_table->row_locks) { // should not be the case; just for code symmetry LM_ERR("NULL rtpengine_hash_table->row_locks\n"); } else { @@ -196,7 +214,7 @@ int rtpengine_hash_table_destroy() { } // destroy hashtable - if (!rtpengine_hash_table) { + if(!rtpengine_hash_table) { // should not be the case; just for code symmetry LM_ERR("NULL rtpengine_hash_table\n"); } else { @@ -207,13 +225,16 @@ int rtpengine_hash_table_destroy() { return 1; } -int rtpengine_hash_table_insert(str callid, str viabranch, struct rtpengine_hash_entry *value) { +int rtpengine_hash_table_insert( + str callid, str viabranch, struct rtpengine_hash_entry *value) +{ struct rtpengine_hash_entry *entry, *last_entry; - struct rtpengine_hash_entry *new_entry = (struct rtpengine_hash_entry *) value; + struct rtpengine_hash_entry *new_entry = + (struct rtpengine_hash_entry *)value; unsigned int hash_index; // sanity checks - if (!rtpengine_hash_table_sanity_checks()) { + if(!rtpengine_hash_table_sanity_checks()) { LM_ERR("sanity checks failed\n"); return 0; } @@ -222,7 +243,7 @@ int rtpengine_hash_table_insert(str callid, str viabranch, struct rtpengine_hash hash_index = str_hash(callid); entry = rtpengine_hash_table->row_entry_list[hash_index]; - if (entry==NULL || rtpengine_hash_table->row_locks[hash_index]==NULL) { + if(entry == NULL || rtpengine_hash_table->row_locks[hash_index] == NULL) { LM_ERR("NULL entry or lock for hash table slot[%d]\n", hash_index); return 0; } @@ -231,20 +252,21 @@ int rtpengine_hash_table_insert(str callid, str viabranch, struct rtpengine_hash // lock lock_get(rtpengine_hash_table->row_locks[hash_index]); - while (entry) { + while(entry) { // if found, don't add new entry - if (str_equal(entry->callid, new_entry->callid) && - str_equal(entry->viabranch, new_entry->viabranch)) { + if(str_equal(entry->callid, new_entry->callid) + && str_equal(entry->viabranch, new_entry->viabranch)) { // unlock lock_release(rtpengine_hash_table->row_locks[hash_index]); - LM_NOTICE("callid=%.*s, viabranch=%.*s already in hashtable, ignore new value\n", - entry->callid.len, entry->callid.s, - entry->viabranch.len, entry->viabranch.s); + LM_NOTICE("callid=%.*s, viabranch=%.*s already in hashtable, " + "ignore new value\n", + entry->callid.len, entry->callid.s, entry->viabranch.len, + entry->viabranch.s); return 0; } // if expired entry discovered, delete it - if (entry->tout < get_ticks()) { + if(entry->tout < get_ticks()) { // set pointers; exclude entry last_entry->next = entry->next; @@ -274,13 +296,15 @@ int rtpengine_hash_table_insert(str callid, str viabranch, struct rtpengine_hash return 1; } -int rtpengine_hash_table_remove(str callid, str viabranch, enum rtpe_operation op) { +int rtpengine_hash_table_remove( + str callid, str viabranch, enum rtpe_operation op) +{ struct rtpengine_hash_entry *entry, *last_entry; unsigned int hash_index; int found = 0; // sanity checks - if (!rtpengine_hash_table_sanity_checks()) { + if(!rtpengine_hash_table_sanity_checks()) { LM_ERR("sanity checks failed\n"); return 0; } @@ -291,17 +315,19 @@ int rtpengine_hash_table_remove(str callid, str viabranch, enum rtpe_operation o last_entry = entry; // lock - if (rtpengine_hash_table->row_locks[hash_index]) { + if(rtpengine_hash_table->row_locks[hash_index]) { lock_get(rtpengine_hash_table->row_locks[hash_index]); } else { LM_ERR("NULL rtpengine_hash_table->row_locks[%d]\n", hash_index); return 0; } - while (entry) { + while(entry) { // if callid found, delete entry - if ((str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) || - (str_equal(entry->callid, callid) && viabranch.len == 0 && op == OP_DELETE)) { + if((str_equal(entry->callid, callid) + && str_equal(entry->viabranch, viabranch)) + || (str_equal(entry->callid, callid) && viabranch.len == 0 + && op == OP_DELETE)) { // set pointers; exclude entry last_entry->next = entry->next; @@ -316,7 +342,7 @@ int rtpengine_hash_table_remove(str callid, str viabranch, enum rtpe_operation o found = 1; - if (!(viabranch.len == 0 && op == OP_DELETE)) { + if(!(viabranch.len == 0 && op == OP_DELETE)) { // unlock lock_release(rtpengine_hash_table->row_locks[hash_index]); return found; @@ -329,7 +355,7 @@ int rtpengine_hash_table_remove(str callid, str viabranch, enum rtpe_operation o } // if expired entry discovered, delete it - if (entry->tout < get_ticks()) { + if(entry->tout < get_ticks()) { // set pointers; exclude entry last_entry->next = entry->next; @@ -353,13 +379,15 @@ int rtpengine_hash_table_remove(str callid, str viabranch, enum rtpe_operation o return found; } -struct rtpp_node *rtpengine_hash_table_lookup(str callid, str viabranch, enum rtpe_operation op) { +struct rtpp_node *rtpengine_hash_table_lookup( + str callid, str viabranch, enum rtpe_operation op) +{ struct rtpengine_hash_entry *entry, *last_entry; unsigned int hash_index; struct rtpp_node *node; // sanity checks - if (!rtpengine_hash_table_sanity_checks()) { + if(!rtpengine_hash_table_sanity_checks()) { LM_ERR("sanity checks failed\n"); return 0; } @@ -370,17 +398,19 @@ struct rtpp_node *rtpengine_hash_table_lookup(str callid, str viabranch, enum rt last_entry = entry; // lock - if (rtpengine_hash_table->row_locks[hash_index]) { + if(rtpengine_hash_table->row_locks[hash_index]) { lock_get(rtpengine_hash_table->row_locks[hash_index]); } else { LM_ERR("NULL rtpengine_hash_table->row_locks[%d]\n", hash_index); return 0; } - while (entry) { + while(entry) { // if callid found, return entry - if ((str_equal(entry->callid, callid) && str_equal(entry->viabranch, viabranch)) || - (str_equal(entry->callid, callid) && viabranch.len == 0 && op == OP_DELETE)) { + if((str_equal(entry->callid, callid) + && str_equal(entry->viabranch, viabranch)) + || (str_equal(entry->callid, callid) && viabranch.len == 0 + && op == OP_DELETE)) { node = entry->node; // unlock @@ -390,7 +420,7 @@ struct rtpp_node *rtpengine_hash_table_lookup(str callid, str viabranch, enum rt } // if expired entry discovered, delete it - if (entry->tout < get_ticks()) { + if(entry->tout < get_ticks()) { // set pointers; exclude entry last_entry->next = entry->next; @@ -415,32 +445,33 @@ struct rtpp_node *rtpengine_hash_table_lookup(str callid, str viabranch, enum rt } // print hash table entries while deleting expired entries -void rtpengine_hash_table_print() { +void rtpengine_hash_table_print() +{ int i; struct rtpengine_hash_entry *entry, *last_entry; // sanity checks - if (!rtpengine_hash_table_sanity_checks()) { + if(!rtpengine_hash_table_sanity_checks()) { LM_ERR("sanity checks failed\n"); - return ; + return; } // print hashtable - for (i = 0; i < rtpengine_hash_table->size; i++) { + for(i = 0; i < rtpengine_hash_table->size; i++) { // lock - if (rtpengine_hash_table->row_locks[i]) { + if(rtpengine_hash_table->row_locks[i]) { lock_get(rtpengine_hash_table->row_locks[i]); } else { LM_ERR("NULL rtpengine_hash_table->row_locks[%d]\n", i); - return ; + return; } entry = rtpengine_hash_table->row_entry_list[i]; last_entry = entry; - while (entry) { + while(entry) { // if expired entry discovered, delete it - if (entry->tout < get_ticks()) { + if(entry->tout < get_ticks()) { // set pointers; exclude entry last_entry->next = entry->next; @@ -453,8 +484,9 @@ void rtpengine_hash_table_print() { // update total rtpengine_hash_table->row_totals[i]--; } else { - LM_DBG("hash_index=%d callid=%.*s tout=%u\n", - i, entry->callid.len, entry->callid.s, entry->tout - get_ticks()); + LM_DBG("hash_index=%d callid=%.*s tout=%u\n", i, + entry->callid.len, entry->callid.s, + entry->tout - get_ticks()); } last_entry = entry; @@ -464,100 +496,105 @@ void rtpengine_hash_table_print() { // unlock lock_release(rtpengine_hash_table->row_locks[i]); } - } -unsigned int rtpengine_hash_table_total() { +unsigned int rtpengine_hash_table_total() +{ int i; unsigned int total = 0; // sanity checks - if (!rtpengine_hash_table_sanity_checks()) { + if(!rtpengine_hash_table_sanity_checks()) { LM_ERR("sanity checks failed\n"); return 0; } - for (i = 0; i < rtpengine_hash_table->size; i++) { + for(i = 0; i < rtpengine_hash_table->size; i++) { total += rtpengine_hash_table->row_totals[i]; } return total; } -void rtpengine_hash_table_free_entry(struct rtpengine_hash_entry *entry) { - if (!entry) { +void rtpengine_hash_table_free_entry(struct rtpengine_hash_entry *entry) +{ + if(!entry) { LM_ERR("try to free a NULL entry\n"); - return ; + return; } // free callid - if (entry->callid.s) { + if(entry->callid.s) { shm_free(entry->callid.s); } // free viabranch - if (entry->viabranch.s) { + if(entry->viabranch.s) { shm_free(entry->viabranch.s); } // free entry shm_free(entry); - return ; + return; } -void rtpengine_hash_table_free_row_entry_list(struct rtpengine_hash_entry *row_entry_list) { +void rtpengine_hash_table_free_row_entry_list( + struct rtpengine_hash_entry *row_entry_list) +{ struct rtpengine_hash_entry *entry, *last_entry; - if (!row_entry_list) { + if(!row_entry_list) { LM_ERR("try to free a NULL row_entry_list\n"); - return ; + return; } entry = row_entry_list; - while (entry) { + while(entry) { last_entry = entry; entry = entry->next; rtpengine_hash_table_free_entry(last_entry); last_entry = NULL; } - return ; + return; } -static void rtpengine_hash_table_free_row_lock(gen_lock_t *row_lock) { - if (!row_lock) { +static void rtpengine_hash_table_free_row_lock(gen_lock_t *row_lock) +{ + if(!row_lock) { LM_ERR("try to free a NULL lock\n"); - return ; + return; } lock_destroy(row_lock); lock_dealloc(row_lock); - return ; + return; } -int rtpengine_hash_table_sanity_checks() { +int rtpengine_hash_table_sanity_checks() +{ // check rtpengine hashtable - if (!rtpengine_hash_table) { + if(!rtpengine_hash_table) { LM_ERR("NULL rtpengine_hash_table\n"); return 0; } // check rtpengine hashtable->row_locks - if (!rtpengine_hash_table->row_locks) { + if(!rtpengine_hash_table->row_locks) { LM_ERR("NULL rtpengine_hash_table->row_locks\n"); return 0; } // check rtpengine hashtable->row_entry_list - if (!rtpengine_hash_table->row_entry_list) { + if(!rtpengine_hash_table->row_entry_list) { LM_ERR("NULL rtpengine_hash_table->row_entry_list\n"); return 0; } // check rtpengine hashtable->row_totals - if (!rtpengine_hash_table->row_totals) { + if(!rtpengine_hash_table->row_totals) { LM_ERR("NULL rtpengine_hash_table->row_totals\n"); return 0; } diff --git a/src/modules/rtpengine/rtpengine_hash.h b/src/modules/rtpengine/rtpengine_hash.h index aed6dfcd2de..9b360553493 100644 --- a/src/modules/rtpengine/rtpengine_hash.h +++ b/src/modules/rtpengine/rtpengine_hash.h @@ -6,34 +6,41 @@ /* table entry */ -struct rtpengine_hash_entry { +struct rtpengine_hash_entry +{ str callid; // call callid - str viabranch; // call viabranch - struct rtpp_node *node; // call selected node + str viabranch; // call viabranch + struct rtpp_node *node; // call selected node - unsigned int tout; // call timeout - struct rtpengine_hash_entry *next; // call next + unsigned int tout; // call timeout + struct rtpengine_hash_entry *next; // call next }; /* table */ -struct rtpengine_hash_table { - struct rtpengine_hash_entry **row_entry_list; // vector of size pointers to entry - gen_lock_t **row_locks; // vector of size pointers to locks - unsigned int *row_totals; // vector of size numbers of entries in the hashtable rows - unsigned int size; // hash table size +struct rtpengine_hash_table +{ + struct rtpengine_hash_entry * + *row_entry_list; // vector of size pointers to entry + gen_lock_t **row_locks; // vector of size pointers to locks + unsigned int * + row_totals; // vector of size numbers of entries in the hashtable rows + unsigned int size; // hash table size }; int rtpengine_hash_table_init(int size); int rtpengine_hash_table_destroy(); -int rtpengine_hash_table_insert(str callid, str viabranch, struct rtpengine_hash_entry *value); +int rtpengine_hash_table_insert( + str callid, str viabranch, struct rtpengine_hash_entry *value); int rtpengine_hash_table_remove(str callid, str viabranch, enum rtpe_operation); -struct rtpp_node *rtpengine_hash_table_lookup(str callid, str viabranch, enum rtpe_operation); +struct rtpp_node *rtpengine_hash_table_lookup( + str callid, str viabranch, enum rtpe_operation); void rtpengine_hash_table_print(); unsigned int rtpengine_hash_table_total(); void rtpengine_hash_table_free_entry(struct rtpengine_hash_entry *entry); -void rtpengine_hash_table_free_row_entry_list(struct rtpengine_hash_entry *row_entry_list); +void rtpengine_hash_table_free_row_entry_list( + struct rtpengine_hash_entry *row_entry_list); int rtpengine_hash_table_sanity_checks();