diff --git a/src/modules/prefix_route/pr_rpc.c b/src/modules/prefix_route/pr_rpc.c index 2d23b6d9fa7..016dc9b174f 100644 --- a/src/modules/prefix_route/pr_rpc.c +++ b/src/modules/prefix_route/pr_rpc.c @@ -35,7 +35,7 @@ #include "pr.h" -static const char *rpc_dump_doc[2] = {"Dump the prefix route tree", NULL}; +static const char *rpc_dump_doc[2] = {"Dump the prefix route tree", NULL}; static const char *rpc_reload_doc[2] = {"Reload prefix routes from DB", NULL}; @@ -48,7 +48,7 @@ static void rpc_dump(rpc_t *rpc, void *c) FILE *f; f = tmpfile(); - if (!f) { + if(!f) { rpc->fault(c, 500, "failed to open temp file"); return; } @@ -57,12 +57,12 @@ static void rpc_dump(rpc_t *rpc, void *c) rewind(f); - while (!feof(f)) { + while(!feof(f)) { - if (!fgets(buf, sizeof(buf), f)) + if(!fgets(buf, sizeof(buf), f)) break; - buf[strlen(buf)-1] = '\0'; + buf[strlen(buf) - 1] = '\0'; rpc->rpl_printf(c, "%s", buf); } @@ -78,18 +78,14 @@ static void rpc_reload(rpc_t *rpc, void *c) { LM_NOTICE("Reloading prefix route tree from DB\n"); - if (0 != pr_db_load()) { + if(0 != pr_db_load()) { LM_ERR("db load failed\n"); rpc->fault(c, 400, "failed to reload prefix routes"); - } - else { + } else { rpc->rpl_printf(c, "Prefix routes reloaded successfully"); } } -rpc_export_t pr_rpc[] = { - {"prefix_route.reload", rpc_reload, rpc_reload_doc, 0}, - {"prefix_route.dump", rpc_dump, rpc_dump_doc, 0}, - {0, 0, 0, 0} -}; +rpc_export_t pr_rpc[] = {{"prefix_route.reload", rpc_reload, rpc_reload_doc, 0}, + {"prefix_route.dump", rpc_dump, rpc_dump_doc, 0}, {0, 0, 0, 0}}; diff --git a/src/modules/prefix_route/prefix_route.c b/src/modules/prefix_route/prefix_route.c index ec928017e8b..94ba6606fc2 100644 --- a/src/modules/prefix_route/prefix_route.c +++ b/src/modules/prefix_route/prefix_route.c @@ -55,12 +55,12 @@ MODULE_VERSION /* Modules parameters */ -static char *db_url = DEFAULT_DB_URL; +static char *db_url = DEFAULT_DB_URL; static char *db_table = "prefix_route"; static int prefix_route_exit = 1; -static int add_route(struct tree_item *root, const char *prefix, - const char *route) +static int add_route( + struct tree_item *root, const char *prefix, const char *route) { int ix, err; @@ -68,18 +68,18 @@ static int add_route(struct tree_item *root, const char *prefix, * in the prefix_route() routing function. */ ix = route_lookup(&main_rt, (char *)route); - if (ix < 0) { + if(ix < 0) { LM_CRIT("route name '%s' is not defined\n", route); return -1; } - if (ix >= main_rt.entries) { + if(ix >= main_rt.entries) { LM_CRIT("route %d > n_entries (%d)\n", ix, main_rt.entries); return -1; } err = tree_item_add(root, prefix, route, ix); - if (0 != err) { + if(0 != err) { LM_CRIT("tree_item_add() failed (%d)\n", err); return err; } @@ -95,45 +95,43 @@ int pr_db_load(void) { db_ctx_t *ctx; db_cmd_t *cmd = NULL; - db_res_t* res = NULL; - db_rec_t* rec; + db_res_t *res = NULL; + db_rec_t *rec; struct tree_item *root; int err = -1; int count = 0; - db_fld_t match[] = { - { .name = "prefix", .type = DB_CSTR }, - { .name = "route", .type = DB_CSTR }, - { .name = "comment", .type = DB_CSTR }, - { .name = NULL, .type = DB_NONE } - }; + db_fld_t match[] = {{.name = "prefix", .type = DB_CSTR}, + {.name = "route", .type = DB_CSTR}, + {.name = "comment", .type = DB_CSTR}, + {.name = NULL, .type = DB_NONE}}; ctx = db_ctx("prefix_route"); - if (!ctx) { + if(!ctx) { LM_ERR("db_ctx() failed\n"); return -1; } - if (db_add_db(ctx, db_url) < 0) { + if(db_add_db(ctx, db_url) < 0) { LM_ERR(" could not add db\n"); goto out; } - if (db_connect(ctx) < 0) { + if(db_connect(ctx) < 0) { LM_ERR("could not connect\n"); goto out; } cmd = db_cmd(DB_GET, ctx, db_table, match, NULL, NULL); - if (!cmd) { + if(!cmd) { LM_ERR("db_cmd() failed\n"); goto out; } - if (db_exec(&res, cmd) < 0) { + if(db_exec(&res, cmd) < 0) { LM_ERR("db_exec() failed\n"); goto out; } root = tree_item_alloc(); - if (NULL == root) { + if(NULL == root) { LM_ERR("tree alloc failed\n"); err = -1; goto out; @@ -143,26 +141,26 @@ int pr_db_load(void) err = 0; /* Read from DB and build tree */ - for (rec = db_first(res); rec != NULL && !err; rec = db_next(res)) { + for(rec = db_first(res); rec != NULL && !err; rec = db_next(res)) { const char *prefix, *route, *comment; ++count; - if (rec->fld[0].flags & DB_NULL) { + if(rec->fld[0].flags & DB_NULL) { LM_CRIT("bad 'prefix' record in table %s, skipping...\n", db_table); continue; } - if (rec->fld[1].flags & DB_NULL) { + if(rec->fld[1].flags & DB_NULL) { LM_CRIT("bad 'route' record in table %s, skipping...\n", db_table); continue; } - prefix = rec->fld[0].v.cstr; - route = rec->fld[1].v.cstr; + prefix = rec->fld[0].v.cstr; + route = rec->fld[1].v.cstr; comment = rec->fld[2].v.cstr; - LM_DBG(" %d: prefix=%-10s route=%-15s comment=%s\n", - count, prefix, route, comment); + LM_DBG(" %d: prefix=%-10s route=%-15s comment=%s\n", count, prefix, + route, comment); err = add_route(root, prefix, route); } @@ -170,7 +168,7 @@ int pr_db_load(void) LM_NOTICE("Total prefix routes loaded: %d\n", count); /* Error */ - if (0 != err) { + if(0 != err) { LM_ERR("error flushing tree\n"); tree_item_free(root); goto out; @@ -178,18 +176,18 @@ int pr_db_load(void) /* Swap trees */ err = tree_swap(root); - if (0 != err) + if(0 != err) goto out; out: /* Free database results */ - if (res) + if(res) db_res_free(res); - if (cmd) + if(cmd) db_cmd_free(cmd); /* Close database connection */ - if (ctx) + if(ctx) db_ctx_free(ctx); return err; @@ -202,13 +200,13 @@ int pr_db_load(void) static int mod_init(void) { /* Initialise tree */ - if (0 != tree_init()) { + if(0 != tree_init()) { LM_CRIT("tree init failed\n\n"); return -1; } /* Populate database */ - if (0 != pr_db_load()) { + if(0 != pr_db_load()) { LM_CRIT("db load failed\n\n"); return -1; } @@ -231,17 +229,17 @@ static void mod_destroy(void) * First try to look at the original Request URI and if there * is no username use the new Request URI */ -static int get_username(struct sip_msg* msg, str *user) +static int get_username(struct sip_msg *msg, str *user) { - if (!msg || !user) + if(!msg || !user) return -1; - if (parse_sip_msg_uri(msg) < 0){ + if(parse_sip_msg_uri(msg) < 0) { LM_ERR("bad sip msg uri\n"); return -1; /* error, bad uri */ } - if (msg->parsed_uri.user.s == 0){ + if(msg->parsed_uri.user.s == 0) { /* no user in uri */ LM_ERR("no user in uri\n"); return -2; @@ -263,20 +261,20 @@ static int ki_prefix_route(sip_msg_t *msg, str *ruser) int route; route = tree_route_get(ruser); - if (route <= 0) + if(route <= 0) return -1; /* If match send to route[x] */ init_run_actions_ctx(&ra_ctx); err = run_actions(&ra_ctx, main_rt.rlist[route], msg); - if (err < 0) { + if(err < 0) { LM_ERR("run_actions failed (%d)\n", err); return -1; } /* Success */ - return (prefix_route_exit)?0:1; + return (prefix_route_exit) ? 0 : 1; } /** @@ -288,7 +286,7 @@ static int ki_prefix_route_uri(sip_msg_t *msg) int err; err = get_username(msg, &user); - if (0 != err) { + if(0 != err) { LM_ERR("could not get username in Request URI (%d)\n", err); return err; } @@ -308,14 +306,14 @@ static int prefix_route(struct sip_msg *msg, char *p1, char *p2) (void)p2; /* Get request URI */ - if(p1==NULL) { + if(p1 == NULL) { err = get_username(msg, &user); - if (0 != err) { + if(0 != err) { LM_ERR("could not get username in Request URI (%d)\n", err); return err; } } else { - if(fixup_get_svalue(msg, (gparam_t*)p1, &user)<0) { + if(fixup_get_svalue(msg, (gparam_t *)p1, &user) < 0) { LM_ERR("could not get username in parameter\n"); return -1; } @@ -327,37 +325,32 @@ static int prefix_route(struct sip_msg *msg, char *p1, char *p2) * Exported functions */ static cmd_export_t cmds[] = { - {"prefix_route", prefix_route, 0, 0, 0, - REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE}, - {"prefix_route", prefix_route, 1, fixup_spve_null, 0, - ANY_ROUTE}, - {0, 0, 0, 0, 0, 0 } -}; + {"prefix_route", prefix_route, 0, 0, 0, + REQUEST_ROUTE | BRANCH_ROUTE | FAILURE_ROUTE}, + {"prefix_route", prefix_route, 1, fixup_spve_null, 0, ANY_ROUTE}, + {0, 0, 0, 0, 0, 0}}; /* * Exported parameters */ -static param_export_t params[] = { - {"db_url", PARAM_STRING, &db_url }, - {"db_table", PARAM_STRING, &db_table}, - {"exit", INT_PARAM, &prefix_route_exit}, - {0, 0, 0 } -}; +static param_export_t params[] = {{"db_url", PARAM_STRING, &db_url}, + {"db_table", PARAM_STRING, &db_table}, + {"exit", INT_PARAM, &prefix_route_exit}, {0, 0, 0}}; /* * Module description */ struct module_exports exports = { - "prefix_route", /* Module name */ - DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, /* exported functions */ - params, /* exported parameters */ - pr_rpc, /* RPC methods */ - 0, /* pseudo-variables exports */ - 0, /* response function */ - mod_init, /* initialization function */ - 0, /* per-child init function */ - mod_destroy /* module destroy function */ + "prefix_route", /* Module name */ + DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, /* exported functions */ + params, /* exported parameters */ + pr_rpc, /* RPC methods */ + 0, /* pseudo-variables exports */ + 0, /* response function */ + mod_init, /* initialization function */ + 0, /* per-child init function */ + mod_destroy /* module destroy function */ }; /** diff --git a/src/modules/prefix_route/tree.c b/src/modules/prefix_route/tree.c index c5e55826539..ff0fdaf12e7 100644 --- a/src/modules/prefix_route/tree.c +++ b/src/modules/prefix_route/tree.c @@ -37,29 +37,32 @@ #include "tree.h" -enum { +enum +{ DIGITS = 10 }; /** Defines a route item in the prefix tree */ -struct tree_item { - struct tree_item *digits[DIGITS]; /**< Child items for each digit */ - char name[16]; /**< Route name (for dump) */ - int route; /**< Valid route number if >0 */ +struct tree_item +{ + struct tree_item *digits[DIGITS]; /**< Child items for each digit */ + char name[16]; /**< Route name (for dump) */ + int route; /**< Valid route number if >0 */ }; /** Defines a locked prefix tree */ -struct tree { - struct tree_item *root; /**< Root item of tree */ - atomic_t refcnt; /**< Reference counting */ +struct tree +{ + struct tree_item *root; /**< Root item of tree */ + atomic_t refcnt; /**< Reference counting */ }; /* Local variables */ static struct tree **shared_tree = NULL; -static gen_lock_t* shared_tree_lock; +static gen_lock_t *shared_tree_lock; /** @@ -71,12 +74,12 @@ struct tree_item *tree_item_alloc(void) int i; root = (struct tree_item *)shm_malloc(sizeof(*root)); - if (NULL == root) { + if(NULL == root) { SHM_MEM_CRITICAL; return NULL; } - for (i=0; idigits[i] = NULL; root->route = 0; @@ -92,10 +95,10 @@ void tree_item_free(struct tree_item *item) { int i; - if (NULL == item) + if(NULL == item) return; - for (i=0; idigits[i]); } @@ -106,29 +109,29 @@ void tree_item_free(struct tree_item *item) /** * Add a route prefix rule to the tree */ -int tree_item_add(struct tree_item *root, const char *prefix, - const char *route, int route_ix) +int tree_item_add(struct tree_item *root, const char *prefix, const char *route, + int route_ix) { struct tree_item *item; const char *p; int err; - if (NULL == root || NULL == prefix || route_ix <= 0) + if(NULL == root || NULL == prefix || route_ix <= 0) return -1; item = root; - for (p = prefix; '\0' != *p; p++) { + for(p = prefix; '\0' != *p; p++) { int digit; - if (!isdigit(*p)) + if(!isdigit(*p)) continue; digit = *p - '0'; /* exist? */ - if (!item->digits[digit]) { + if(!item->digits[digit]) { item->digits[digit] = tree_item_alloc(); - if (!item->digits[digit]) { + if(!item->digits[digit]) { LM_CRIT("alloc failed\n"); err = -1; goto out; @@ -138,13 +141,13 @@ int tree_item_add(struct tree_item *root, const char *prefix, item = item->digits[digit]; } - if (NULL == item) { + if(NULL == item) { LM_CRIT("internal error (no item)\n"); err = -1; goto out; } - if (item->route > 0) { + if(item->route > 0) { LM_ERR("prefix %s already set to %s\n", prefix, item->name); } @@ -152,8 +155,8 @@ int tree_item_add(struct tree_item *root, const char *prefix, item->route = route_ix; /* Copy the route name (used in tree dump) */ - strncpy(item->name, route, sizeof(item->name)-1); - item->name[sizeof(item->name)-1] = '\0'; + strncpy(item->name, route, sizeof(item->name) - 1); + item->name[sizeof(item->name) - 1] = '\0'; err = 0; @@ -171,27 +174,27 @@ int tree_item_get(const struct tree_item *root, const str *user) const char *p, *pmax; int route = 0; - if (NULL == root || NULL == user || NULL == user->s || !user->len) + if(NULL == root || NULL == user || NULL == user->s || !user->len) return -1; pmax = user->s + user->len; item = root; - for (p = user->s; p < pmax ; p++) { + for(p = user->s; p < pmax; p++) { int digit; - if (!isdigit(*p)) { + if(!isdigit(*p)) { continue; } digit = *p - '0'; /* Update route with best match so far */ - if (item->route > 0) { + if(item->route > 0) { route = item->route; } /* exist? */ - if (NULL == item->digits[digit]) { + if(NULL == item->digits[digit]) { break; } @@ -209,26 +212,26 @@ void tree_item_print(const struct tree_item *item, FILE *f, int level) { int i; - if (NULL == item || NULL == f) + if(NULL == item || NULL == f) return; - if (item->route > 0) { + if(item->route > 0) { fprintf(f, " \t--> route[%s] ", item->name); } - for (i=0; idigits[i]) { + if(!item->digits[i]) { continue; } fputc('\n', f); - for (j=0; jdigits[i], f, level+1); + tree_item_print(item->digits[i], f, level + 1); } } @@ -241,12 +244,12 @@ static struct tree *tree_alloc(void) struct tree *tree; tree = (struct tree *)shm_malloc(sizeof(*tree)); - if (NULL == tree) { + if(NULL == tree) { SHM_MEM_CRITICAL; return NULL; } - tree->root = NULL; + tree->root = NULL; atomic_set(&tree->refcnt, 0); return tree; @@ -258,14 +261,14 @@ static struct tree *tree_alloc(void) */ static void tree_flush(struct tree *tree) { - if (NULL == tree) + if(NULL == tree) return; /* Wait for old tree to be released */ - for (;;) { + for(;;) { const int refcnt = atomic_get(&tree->refcnt); - if (refcnt <= 0) + if(refcnt <= 0) break; LM_NOTICE("waiting refcnt=%d\n", refcnt); @@ -309,7 +312,7 @@ static struct tree *tree_ref(void) struct tree *tree_deref(struct tree *tree) { - if (tree) + if(tree) atomic_dec(&tree->refcnt); return tree; } @@ -319,18 +322,18 @@ int tree_init(void) { /* Initialize lock */ shared_tree_lock = lock_alloc(); - if (NULL == shared_tree_lock) { + if(NULL == shared_tree_lock) { return -1; } lock_init(shared_tree_lock); /* Pointer to global tree must be in shared memory */ shared_tree = (struct tree **)shm_malloc(sizeof(*shared_tree)); - if (NULL == shared_tree) { + if(NULL == shared_tree) { SHM_MEM_ERROR; lock_destroy(shared_tree_lock); lock_dealloc(shared_tree_lock); - shared_tree_lock=0; + shared_tree_lock = 0; return -1; } @@ -342,13 +345,13 @@ int tree_init(void) void tree_close(void) { - if (shared_tree) + if(shared_tree) tree_flush(tree_get()); shared_tree = NULL; - if (shared_tree_lock) { + if(shared_tree_lock) { lock_destroy(shared_tree_lock); lock_dealloc(shared_tree_lock); - shared_tree_lock=0; + shared_tree_lock = 0; } } @@ -358,7 +361,7 @@ int tree_swap(struct tree_item *root) struct tree *new_tree, *old_tree; new_tree = tree_alloc(); - if (NULL == new_tree) + if(NULL == new_tree) return -1; new_tree->root = root; @@ -385,7 +388,7 @@ int tree_route_get(const str *user) /* Find match in tree */ tree = tree_ref(); - if (NULL == tree) { + if(NULL == tree) { return -1; } @@ -404,12 +407,10 @@ void tree_print(FILE *f) fprintf(f, "Prefix route tree:\n"); - if (tree) { - fprintf(f, " reference count: %d\n", - atomic_get(&tree->refcnt)); + if(tree) { + fprintf(f, " reference count: %d\n", atomic_get(&tree->refcnt)); tree_item_print(tree->root, f, 0); - } - else { + } else { fprintf(f, " (no tree)\n"); } diff --git a/src/modules/prefix_route/tree.h b/src/modules/prefix_route/tree.h index f2429486f70..fa24769b70b 100644 --- a/src/modules/prefix_route/tree.h +++ b/src/modules/prefix_route/tree.h @@ -31,17 +31,17 @@ struct tree_item; struct tree_item *tree_item_alloc(void); void tree_item_free(struct tree_item *item); -int tree_item_add(struct tree_item *root, const char *prefix, - const char *route, int route_ix); -int tree_item_get(const struct tree_item *root, const str *user); +int tree_item_add(struct tree_item *root, const char *prefix, const char *route, + int route_ix); +int tree_item_get(const struct tree_item *root, const str *user); void tree_item_print(const struct tree_item *item, FILE *f, int level); struct tree; -int tree_init(void); +int tree_init(void); void tree_close(void); -int tree_swap(struct tree_item *root); -int tree_route_get(const str *user); +int tree_swap(struct tree_item *root); +int tree_route_get(const str *user); void tree_print(FILE *f); #endif