diff --git a/src/modules/p_usrloc/config.c b/src/modules/p_usrloc/config.c index c95be9ec701..d73fe708adb 100644 --- a/src/modules/p_usrloc/config.c +++ b/src/modules/p_usrloc/config.c @@ -29,32 +29,38 @@ #include "config.h" -struct cfg_group_p_usrloc default_p_usrloc_cfg = { - DEFAULT_EXPIRE, /* expire_time */ +struct cfg_group_p_usrloc default_p_usrloc_cfg = { + DEFAULT_EXPIRE, /* expire_time */ DEFAULT_ERR_THRESHOLD, /* db_err_threshold */ - DEFAULT_FAILOVER_LEVEL, /* failover_level */ - 0, /* db_ops_ruid */ - 1, /* db_update_as_insert */ - CONTACT_ONLY, /* matching_mode */ - 0, /* utc_timestamps */ - }; + DEFAULT_FAILOVER_LEVEL, /* failover_level */ + 0, /* db_ops_ruid */ + 1, /* db_update_as_insert */ + CONTACT_ONLY, /* matching_mode */ + 0, /* utc_timestamps */ +}; -void *p_usrloc_cfg = &default_p_usrloc_cfg; +void *p_usrloc_cfg = &default_p_usrloc_cfg; -cfg_def_t p_usrloc_cfg_def[] = { - {"expire_time", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, - "Contains number of second to expire if no expire hf or contact expire present" }, - {"db_err_threshold", CFG_VAR_INT | CFG_ATOMIC, 0, 100, 0, 0, - " Specifies the error value on which a database shall be turned off. "}, - {"failover_level", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, - "Specifies the manner a failover is done (1 = turn off, 2 = find a spare) "}, - {"db_ops_ruid", CFG_VAR_INT | CFG_ATOMIC, 0, 2, 0, 0, - "Set this if you want to update / delete from DB using ruid value "}, - {"db_update_as_insert", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0, - "Set this parameter if you want to do INSERT DB operations instead of UPDATE DB operations. "}, - {"matching_mode", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, - "Specified which contact maching algorithm to be used (0 - Contact only / 1 - Contact and Call-ID / 2 - Contact and Path)"}, - {"utc_timestamps", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, - "Expires and last_modified timestamps in UTC time format"}, - {0, 0, 0, 0, 0, 0} -}; +cfg_def_t p_usrloc_cfg_def[] = { + {"expire_time", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Contains number of second to expire if no expire hf or " + "contact expire present"}, + {"db_err_threshold", CFG_VAR_INT | CFG_ATOMIC, 0, 100, 0, 0, + " Specifies the error value on which a database shall be " + "turned off. "}, + {"failover_level", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Specifies the manner a failover is done (1 = turn off, 2 = " + "find a spare) "}, + {"db_ops_ruid", CFG_VAR_INT | CFG_ATOMIC, 0, 2, 0, 0, + "Set this if you want to update / delete from DB using ruid " + "value "}, + {"db_update_as_insert", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0, + "Set this parameter if you want to do INSERT DB operations " + "instead of UPDATE DB operations. "}, + {"matching_mode", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Specified which contact maching algorithm to be used (0 - " + "Contact only / 1 - Contact and Call-ID / 2 - Contact and " + "Path)"}, + {"utc_timestamps", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Expires and last_modified timestamps in UTC time format"}, + {0, 0, 0, 0, 0, 0}}; diff --git a/src/modules/p_usrloc/config.h b/src/modules/p_usrloc/config.h index 396bcded49b..a4f72d25f27 100644 --- a/src/modules/p_usrloc/config.h +++ b/src/modules/p_usrloc/config.h @@ -26,22 +26,23 @@ #include "../../core/str.h" #include "p_usrloc_mod.h" -struct cfg_group_p_usrloc { - unsigned int expire_time; - unsigned int db_err_threshold; - unsigned int failover_level; - unsigned int db_ops_ruid; - unsigned int db_update_as_insert; - unsigned int matching_mode; - unsigned int utc_timestamps; +struct cfg_group_p_usrloc +{ + unsigned int expire_time; + unsigned int db_err_threshold; + unsigned int failover_level; + unsigned int db_ops_ruid; + unsigned int db_update_as_insert; + unsigned int matching_mode; + unsigned int utc_timestamps; }; -extern struct cfg_group_p_usrloc default_p_usrloc_cfg; -extern void *p_usrloc_cfg; -extern cfg_def_t p_usrloc_cfg_def[]; +extern struct cfg_group_p_usrloc default_p_usrloc_cfg; +extern void *p_usrloc_cfg; +extern cfg_def_t p_usrloc_cfg_def[]; -extern void default_expires_stats_update(str*, str*); -extern void default_expires_range_update(str*, str*); -extern void max_expires_stats_update(str*, str*); +extern void default_expires_stats_update(str *, str *); +extern void default_expires_range_update(str *, str *); +extern void max_expires_stats_update(str *, str *); #endif diff --git a/src/modules/p_usrloc/dlist.c b/src/modules/p_usrloc/dlist.c index 895eca45e1c..4c3132509e4 100644 --- a/src/modules/p_usrloc/dlist.c +++ b/src/modules/p_usrloc/dlist.c @@ -28,16 +28,16 @@ #include "dlist.h" -#include /* abort */ -#include /* strlen, memcmp */ -#include /* printf */ +#include /* abort */ +#include /* strlen, memcmp */ +#include /* printf */ #include "../../core/ut.h" #include "../../lib/srdb1/db.h" #include "../../core/mem/shm_mem.h" #include "../../core/dprint.h" #include "../../core/ip_addr.h" #include "../../core/socket_info.h" -#include "udomain.h" /* new_udomain, free_udomain */ +#include "udomain.h" /* new_udomain, free_udomain */ #include "utime.h" #include "p_usrloc_mod.h" @@ -46,13 +46,13 @@ static struct domain_list_item *domain_list; - -static inline struct domain_list_item * find_dlist (str *name) { +static inline struct domain_list_item *find_dlist(str *name) +{ struct domain_list_item *item; - for (item = domain_list; item != NULL; item = item->next) { - if (item->name.len == name->len - && memcmp (item->name.s, name->s, name->len) == 0) { + for(item = domain_list; item != NULL; item = item->next) { + if(item->name.len == name->len + && memcmp(item->name.s, name->s, name->len) == 0) { return item; } } @@ -60,33 +60,32 @@ static inline struct domain_list_item * find_dlist (str *name) { } - - -static inline struct domain_list_item * add_to_dlist (str *name, int type) { +static inline struct domain_list_item *add_to_dlist(str *name, int type) +{ struct domain_list_item *item; int i; - item = (struct domain_list_item *) - pkg_malloc (sizeof (struct domain_list_item)); - if (item == NULL) { + item = (struct domain_list_item *)pkg_malloc( + sizeof(struct domain_list_item)); + if(item == NULL) { LM_ERR("Out of pkg memory.\n"); return NULL; } - item->name.s = (char *) pkg_malloc (name->len + 1); - if (item->name.s == NULL) { + item->name.s = (char *)pkg_malloc(name->len + 1); + if(item->name.s == NULL) { LM_ERR("Out of pkg memory (1).\n"); pkg_free(item); return NULL; } - memcpy (item->name.s, name->s, name->len); + memcpy(item->name.s, name->s, name->len); item->name.s[name->len] = '\0'; item->name.len = name->len; - memset (&item->domain, 0, sizeof (struct udomain)); + memset(&item->domain, 0, sizeof(struct udomain)); item->domain.name = &item->name; item->domain.dbt = type; - item->domain.table = (hslot_t*)pkg_malloc(sizeof(hslot_t) * ul_hash_size); - if (!item->domain.table) { + item->domain.table = (hslot_t *)pkg_malloc(sizeof(hslot_t) * ul_hash_size); + if(!item->domain.table) { LM_ERR("Out of pkg memory (2)\n"); pkg_free(item->name.s); pkg_free(item); @@ -117,32 +116,33 @@ static inline struct domain_list_item * add_to_dlist (str *name, int type) { * \param _d new created domain * \return 0 on success, -1 on failure */ -int register_udomain(const char *name, udomain_t **domain) { +int register_udomain(const char *name, udomain_t **domain) +{ struct domain_list_item *item; str name_str; - ul_domain_db_t * d; + ul_domain_db_t *d; - name_str.s = (char *) name; - name_str.len = strlen (name); - item = find_dlist (&name_str); - if (item == NULL) { - if((d = ul_find_domain(name)) == NULL){ + name_str.s = (char *)name; + name_str.len = strlen(name); + item = find_dlist(&name_str); + if(item == NULL) { + if((d = ul_find_domain(name)) == NULL) { LM_ERR("domain %s not found.\n", name); return -1; } - item = add_to_dlist (&name_str, d->dbt); + item = add_to_dlist(&name_str, d->dbt); } - if (item == NULL) { + if(item == NULL) { return -1; } *domain = &item->domain; - LM_DBG("found domain %.*s, type: %s\n", (*domain)->name->len, (*domain)->name->s, (((*domain)->dbt) == DB_TYPE_CLUSTER ? "cluster" : "single")); + LM_DBG("found domain %.*s, type: %s\n", (*domain)->name->len, + (*domain)->name->s, + (((*domain)->dbt) == DB_TYPE_CLUSTER ? "cluster" : "single")); return 0; } - - /*! * \brief Loops through all domains summing up the number of users * \return the number of users, could be zero @@ -156,7 +156,7 @@ unsigned long get_number_of_users(void) int get_all_ucontacts(void *buf, int len, unsigned int flags, - unsigned int part_idx, unsigned int part_max, int options) + unsigned int part_idx, unsigned int part_max, int options) { LM_INFO("not available with partitioned interface\n"); return -1; @@ -171,5 +171,3 @@ int synchronize_all_udomains(void) LM_INFO("not available with partitioned interface\n"); return res; } - - diff --git a/src/modules/p_usrloc/dlist.h b/src/modules/p_usrloc/dlist.h index 901b9354972..6943f2f652d 100644 --- a/src/modules/p_usrloc/dlist.h +++ b/src/modules/p_usrloc/dlist.h @@ -36,10 +36,11 @@ /*! * List of all domains registered with usrloc */ -struct domain_list_item { +struct domain_list_item +{ str name; udomain_t domain; - struct domain_list_item*next; + struct domain_list_item *next; }; @@ -72,7 +73,7 @@ unsigned long get_number_of_users(void); * \param _d new created domain * \return 0 on success, -1 on failure */ -int register_udomain(const char* _n, udomain_t** _d); +int register_udomain(const char *_n, udomain_t **_d); /*! * \brief Get all contacts from the usrloc, in partitions if wanted @@ -106,8 +107,8 @@ int register_udomain(const char* _n, udomain_t** _d); * \param part_max maximal part * \return 0 on success, positive if buffer size was not sufficient, negative on failure */ -int get_all_ucontacts(void *, int, unsigned int, - unsigned int part_idx, unsigned int part_max, int options); +int get_all_ucontacts(void *, int, unsigned int, unsigned int part_idx, + unsigned int part_max, int options); /*! * \brief Find a particular domain, small wrapper around find_dlist @@ -115,7 +116,7 @@ int get_all_ucontacts(void *, int, unsigned int, * \param _p pointer to domain if found * \return 1 if domain was found, 0 otherwise */ -int find_domain(str* _d, udomain_t** _p); +int find_domain(str *_d, udomain_t **_p); #endif diff --git a/src/modules/p_usrloc/hslot.c b/src/modules/p_usrloc/hslot.c index 909610cfc7e..b0f7d25426e 100644 --- a/src/modules/p_usrloc/hslot.c +++ b/src/modules/p_usrloc/hslot.c @@ -26,13 +26,12 @@ */ - #include "hslot.h" /*! number of locks */ -int ul_locks_no=4; +int ul_locks_no = 4; /*! global list of locks */ -gen_lock_set_t* ul_locks=0; +gen_lock_set_t *ul_locks = 0; /*! @@ -44,25 +43,22 @@ int ul_init_locks(void) int i; i = ul_locks_no; do { - if ((( ul_locks=lock_set_alloc(i))!=0)&& - (lock_set_init(ul_locks)!=0)) - { + if(((ul_locks = lock_set_alloc(i)) != 0) + && (lock_set_init(ul_locks) != 0)) { ul_locks_no = i; LM_INFO("locks array size %d\n", ul_locks_no); return 0; - } - if (ul_locks){ + if(ul_locks) { lock_set_dealloc(ul_locks); - ul_locks=0; + ul_locks = 0; } i--; - if(i==0) - { + if(i == 0) { LM_ERR("failed to allocate locks\n"); return -1; } - } while (1); + } while(1); } @@ -73,10 +69,10 @@ void ul_unlock_locks(void) { unsigned int i; - if (ul_locks==0) + if(ul_locks == 0) return; - for (i=0;ilocks[i]); #else @@ -91,7 +87,7 @@ void ul_unlock_locks(void) */ void ul_destroy_locks(void) { - if (ul_locks !=0){ + if(ul_locks != 0) { lock_set_destroy(ul_locks); lock_set_dealloc(ul_locks); }; @@ -124,7 +120,7 @@ void ul_release_idx(int idx) * \param _s hash slot * \param n used to get the slot number (modulo number or locks) */ -void init_slot(struct udomain* _d, hslot_t* _s, int n) +void init_slot(struct udomain *_d, hslot_t *_s, int n) { _s->n = 0; _s->first = 0; @@ -132,9 +128,9 @@ void init_slot(struct udomain* _d, hslot_t* _s, int n) _s->d = _d; #ifdef GEN_LOCK_T_PREFERED - _s->lock = &ul_locks->locks[n%ul_locks_no]; + _s->lock = &ul_locks->locks[n % ul_locks_no]; #else - _s->lockidx = n%ul_locks_no; + _s->lockidx = n % ul_locks_no; #endif } @@ -143,20 +139,20 @@ void init_slot(struct udomain* _d, hslot_t* _s, int n) * \brief Deinitialize given slot structure * \param _s hash slot */ -void deinit_slot(hslot_t* _s) +void deinit_slot(hslot_t *_s) { - struct urecord* ptr; - - /* Remove all elements */ + struct urecord *ptr; + + /* Remove all elements */ while(_s->first) { ptr = _s->first; _s->first = _s->first->next; free_urecord(ptr); } - + _s->n = 0; _s->last = 0; - _s->d = 0; + _s->d = 0; } @@ -165,9 +161,9 @@ void deinit_slot(hslot_t* _s) * \param _s hash slot * \param _r added record */ -void slot_add(hslot_t* _s, struct urecord* _r) +void slot_add(hslot_t *_s, struct urecord *_r) { - if (_s->n == 0) { + if(_s->n == 0) { _s->first = _s->last = _r; } else { _r->prev = _s->last; @@ -184,15 +180,15 @@ void slot_add(hslot_t* _s, struct urecord* _r) * \param _s hash slot * \param _r removed record */ -void slot_rem(hslot_t* _s, struct urecord* _r) +void slot_rem(hslot_t *_s, struct urecord *_r) { - if (_r->prev) { + if(_r->prev) { _r->prev->next = _r->next; } else { _s->first = _r->next; } - if (_r->next) { + if(_r->next) { _r->next->prev = _r->prev; } else { _s->last = _r->prev; diff --git a/src/modules/p_usrloc/hslot.h b/src/modules/p_usrloc/hslot.h index b9a8460ea98..cfae1b0635f 100644 --- a/src/modules/p_usrloc/hslot.h +++ b/src/modules/p_usrloc/hslot.h @@ -24,7 +24,6 @@ */ - #ifndef HSLOT_H #define HSLOT_H @@ -38,40 +37,41 @@ struct udomain; struct urecord; -typedef struct hslot { - int n; /*!< Number of elements in the collision slot */ - struct urecord* first; /*!< First element in the list */ - struct urecord* last; /*!< Last element in the list */ - struct udomain* d; /*!< Domain we belong to */ +typedef struct hslot +{ + int n; /*!< Number of elements in the collision slot */ + struct urecord *first; /*!< First element in the list */ + struct urecord *last; /*!< Last element in the list */ + struct udomain *d; /*!< Domain we belong to */ #ifdef GEN_LOCK_T_PREFERED - gen_lock_t *lock; /*!< Lock for hash entry - fastlock */ + gen_lock_t *lock; /*!< Lock for hash entry - fastlock */ #else - int lockidx; /*!< Lock index for hash entry - the rest*/ + int lockidx; /*!< Lock index for hash entry - the rest*/ #endif } hslot_t; /*! \brief * Initialize slot structure */ -void init_slot(struct udomain* _d, hslot_t* _s, int n); +void init_slot(struct udomain *_d, hslot_t *_s, int n); /*! \brief * Deinitialize given slot structure */ -void deinit_slot(hslot_t* _s); +void deinit_slot(hslot_t *_s); /*! \brief * Add an element to slot linked list */ -void slot_add(hslot_t* _s, struct urecord* _r); +void slot_add(hslot_t *_s, struct urecord *_r); /*! \brief * Remove an element from slot linked list */ -void slot_rem(hslot_t* _s, struct urecord* _r); +void slot_rem(hslot_t *_s, struct urecord *_r); /*! diff --git a/src/modules/p_usrloc/p_usrloc_mod.c b/src/modules/p_usrloc/p_usrloc_mod.c index 20d4b13f7fb..fec170a320c 100644 --- a/src/modules/p_usrloc/p_usrloc_mod.c +++ b/src/modules/p_usrloc/p_usrloc_mod.c @@ -46,10 +46,10 @@ #include "../../core/rpc_lookup.h" #include "../../core/timer_proc.h" #include "../../core/globals.h" -#include "../../core/ut.h" /* str_init */ -#include "udomain.h" /* {insert,delete,get,release}_urecord */ -#include "urecord.h" /* {insert,delete,get}_ucontact */ -#include "ucontact.h" /* update_ucontact */ +#include "../../core/ut.h" /* str_init */ +#include "udomain.h" /* {insert,delete,get,release}_urecord */ +#include "urecord.h" /* {insert,delete,get}_ucontact */ +#include "ucontact.h" /* update_ucontact */ #include "ul_mi.h" #include "../usrloc/ul_callback.h" #include "ul_db_api.h" @@ -62,30 +62,30 @@ MODULE_VERSION -#define RUID_COL "ruid" -#define USER_COL "username" -#define DOMAIN_COL "domain" -#define CONTACT_COL "contact" -#define EXPIRES_COL "expires" -#define Q_COL "q" -#define CALLID_COL "callid" -#define CSEQ_COL "cseq" -#define FLAGS_COL "flags" -#define CFLAGS_COL "cflags" +#define RUID_COL "ruid" +#define USER_COL "username" +#define DOMAIN_COL "domain" +#define CONTACT_COL "contact" +#define EXPIRES_COL "expires" +#define Q_COL "q" +#define CALLID_COL "callid" +#define CSEQ_COL "cseq" +#define FLAGS_COL "flags" +#define CFLAGS_COL "cflags" #define USER_AGENT_COL "user_agent" -#define RECEIVED_COL "received" -#define PATH_COL "path" -#define SOCK_COL "socket" -#define METHODS_COL "methods" -#define INSTANCE_COL "instance" -#define REG_ID_COL "reg_id" -#define LAST_MOD_COL "last_modified" -#define UNIQ_COL "uniq" - -static int mod_init(void); /*!< Module initialization function */ -static void destroy(void); /*!< Module destroy function */ -static int child_init(int rank); /*!< Per-child init function */ -extern int bind_usrloc(usrloc_api_t* api); +#define RECEIVED_COL "received" +#define PATH_COL "path" +#define SOCK_COL "socket" +#define METHODS_COL "methods" +#define INSTANCE_COL "instance" +#define REG_ID_COL "reg_id" +#define LAST_MOD_COL "last_modified" +#define UNIQ_COL "uniq" + +static int mod_init(void); /*!< Module initialization function */ +static void destroy(void); /*!< Module destroy function */ +static int child_init(int rank); /*!< Per-child init function */ +extern int bind_usrloc(usrloc_api_t *api); extern int ul_locks_no; /* @@ -126,176 +126,182 @@ extern int ul_locks_no; * @param alg_location defines the algorithm for the location matching - based on crc32 for now */ -str ruid_col = str_init(RUID_COL); /*!< Name of column containing record unique id */ -str user_col = str_init(USER_COL); /*!< Name of column containing usernames */ -str domain_col = str_init(DOMAIN_COL); /*!< Name of column containing domains */ -str contact_col = str_init(CONTACT_COL); /*!< Name of column containing contact addresses */ -str expires_col = str_init(EXPIRES_COL); /*!< Name of column containing expires values */ -str q_col = str_init(Q_COL); /*!< Name of column containing q values */ -str callid_col = str_init(CALLID_COL); /*!< Name of column containing callid string */ -str cseq_col = str_init(CSEQ_COL); /*!< Name of column containing cseq values */ -str flags_col = str_init(FLAGS_COL); /*!< Name of column containing internal flags */ -str cflags_col = str_init(CFLAGS_COL); -str user_agent_col = str_init(USER_AGENT_COL); /*!< Name of column containing user agent string */ -str received_col = str_init(RECEIVED_COL); /*!< Name of column containing transport info of REGISTER */ -str path_col = str_init(PATH_COL); /*!< Name of column containing the Path header */ -str sock_col = str_init(SOCK_COL); /*!< Name of column containing the received socket */ -str methods_col = str_init(METHODS_COL); /*!< Name of column containing the supported methods */ -str instance_col = str_init(INSTANCE_COL); /*!< Name of column containing the SIP instance value */ -str reg_id_col = str_init(REG_ID_COL); /*!< Name of column containing the reg-id value */ -str last_mod_col = str_init(LAST_MOD_COL); /*!< Name of column containing the last modified date */ -str uniq_col = str_init(UNIQ_COL); /*!< Name of column containing the uniq value*/ -int db_mode = 3; /*!< Database sync scheme: 1-write through, 2-write back, 3-only db */ -int use_domain = 0; /*!< Whether usrloc should use domain part of aor */ -int use_domain_crc32 = 1; /*!< Whether usrloc should use domain part of aor when calculating crc32 */ -int desc_time_order = 0; /*!< By default do not enable timestamp ordering */ - -int ul_fetch_rows = 2000; /*!< number of rows to fetch from result */ +str ruid_col = + str_init(RUID_COL); /*!< Name of column containing record unique id */ +str user_col = str_init(USER_COL); /*!< Name of column containing usernames */ +str domain_col = str_init(DOMAIN_COL); /*!< Name of column containing domains */ +str contact_col = str_init( + CONTACT_COL); /*!< Name of column containing contact addresses */ +str expires_col = + str_init(EXPIRES_COL); /*!< Name of column containing expires values */ +str q_col = str_init(Q_COL); /*!< Name of column containing q values */ +str callid_col = + str_init(CALLID_COL); /*!< Name of column containing callid string */ +str cseq_col = str_init(CSEQ_COL); /*!< Name of column containing cseq values */ +str flags_col = + str_init(FLAGS_COL); /*!< Name of column containing internal flags */ +str cflags_col = str_init(CFLAGS_COL); +str user_agent_col = str_init( + USER_AGENT_COL); /*!< Name of column containing user agent string */ +str received_col = str_init( + RECEIVED_COL); /*!< Name of column containing transport info of REGISTER */ +str path_col = + str_init(PATH_COL); /*!< Name of column containing the Path header */ +str sock_col = str_init( + SOCK_COL); /*!< Name of column containing the received socket */ +str methods_col = str_init( + METHODS_COL); /*!< Name of column containing the supported methods */ +str instance_col = str_init( + INSTANCE_COL); /*!< Name of column containing the SIP instance value */ +str reg_id_col = + str_init(REG_ID_COL); /*!< Name of column containing the reg-id value */ +str last_mod_col = str_init( + LAST_MOD_COL); /*!< Name of column containing the last modified date */ +str uniq_col = + str_init(UNIQ_COL); /*!< Name of column containing the uniq value*/ +int db_mode = + 3; /*!< Database sync scheme: 1-write through, 2-write back, 3-only db */ +int use_domain = 0; /*!< Whether usrloc should use domain part of aor */ +int use_domain_crc32 = + 1; /*!< Whether usrloc should use domain part of aor when calculating crc32 */ +int desc_time_order = 0; /*!< By default do not enable timestamp ordering */ + +int ul_fetch_rows = 2000; /*!< number of rows to fetch from result */ int ul_hash_size = 9; -str write_db_url = str_init(DEFAULT_DB_URL); -str read_db_url = str_init(DEFAULT_DB_URL); -str reg_table = str_init(REG_TABLE); -str id_col = str_init(ID_COL); -str url_col = str_init(URL_COL); -str num_col = str_init(NUM_COL); -str status_col = str_init(STATUS_COL); -str failover_time_col = str_init(FAILOVER_T_COL); -str spare_col = str_init(SPARE_COL); -str error_col = str_init(ERROR_COL); -str risk_group_col = str_init(RISK_GROUP_COL); -int retry_interval = DB_RETRY; -int policy = DB_DEFAULT_POLICY; -int db_write = 0; -int db_master_write = 0; -int alg_location = 0; +str write_db_url = str_init(DEFAULT_DB_URL); +str read_db_url = str_init(DEFAULT_DB_URL); +str reg_table = str_init(REG_TABLE); +str id_col = str_init(ID_COL); +str url_col = str_init(URL_COL); +str num_col = str_init(NUM_COL); +str status_col = str_init(STATUS_COL); +str failover_time_col = str_init(FAILOVER_T_COL); +str spare_col = str_init(SPARE_COL); +str error_col = str_init(ERROR_COL); +str risk_group_col = str_init(RISK_GROUP_COL); +int retry_interval = DB_RETRY; +int policy = DB_DEFAULT_POLICY; +int db_write = 0; +int db_master_write = 0; +int alg_location = 0; int mdb_availability_control = 0; -int db_use_transactions = 0; +int db_use_transactions = 0; str db_transaction_level = str_init(DB_DEFAULT_TRANSACTION_LEVEL); -char * isolation_level; -int connection_expires = DB_DEFAULT_CONNECTION_EXPIRES; -int max_loc_nr = 0 ; +char *isolation_level; +int connection_expires = DB_DEFAULT_CONNECTION_EXPIRES; +int max_loc_nr = 0; /* flags */ unsigned int nat_bflag = (unsigned int)-1; unsigned int init_flag = 0; -str default_db_url = str_init(DEFAULT_DB_URL); -str default_db_type = str_init(DEFAULT_DB_TYPE); -str domain_db = str_init(DEFAULT_DOMAIN_DB); -int default_dbt = 0; -int expire = 0; +str default_db_url = str_init(DEFAULT_DB_URL); +str default_db_type = str_init(DEFAULT_DB_TYPE); +str domain_db = str_init(DEFAULT_DOMAIN_DB); +int default_dbt = 0; +int expire = 0; int *mdb_w_available; /*! \brief * Exported functions */ static cmd_export_t cmds[] = { - {"ul_bind_usrloc", (cmd_function)bind_usrloc, 1, 0, 0, 0}, - {0, 0, 0, 0, 0, 0} -}; + {"ul_bind_usrloc", (cmd_function)bind_usrloc, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}}; /*! \brief * Exported parameters */ -static param_export_t params[] = { - {"ruid_column", PARAM_STR, &ruid_col }, - {"user_column", PARAM_STR, &user_col }, - {"domain_column", PARAM_STR, &domain_col }, - {"contact_column", PARAM_STR, &contact_col }, - {"expires_column", PARAM_STR, &expires_col }, - {"q_column", PARAM_STR, &q_col }, - {"callid_column", PARAM_STR, &callid_col }, - {"cseq_column", PARAM_STR, &cseq_col }, - {"flags_column", PARAM_STR, &flags_col }, - {"cflags_column", PARAM_STR, &cflags_col }, - {"db_mode", INT_PARAM, &db_mode }, - {"use_domain", INT_PARAM, &use_domain }, - {"use_domain_crc32", INT_PARAM, &use_domain_crc32 }, - {"desc_time_order", INT_PARAM, &desc_time_order }, - {"user_agent_column", PARAM_STR, &user_agent_col}, - {"received_column", PARAM_STR, &received_col }, - {"path_column", PARAM_STR, &path_col }, - {"socket_column", PARAM_STR, &sock_col }, - {"methods_column", PARAM_STR, &methods_col }, - {"matching_mode", INT_PARAM, &default_p_usrloc_cfg.matching_mode}, - {"cseq_delay", INT_PARAM, &cseq_delay }, - {"fetch_rows", INT_PARAM, &ul_fetch_rows }, - {"hash_size", INT_PARAM, &ul_hash_size }, - {"nat_bflag", INT_PARAM, &nat_bflag }, - {"default_db_url", PARAM_STR, &default_db_url }, - {"default_db_type", PARAM_STR, &default_db_type }, - {"domain_db", PARAM_STR, &domain_db }, - {"instance_column", PARAM_STR, &instance_col }, - {"reg_id_column", PARAM_STR, ®_id_col }, - {"write_db_url", PARAM_STR, &write_db_url }, - {"read_db_url", PARAM_STR, &read_db_url }, - {"reg_db_table", PARAM_STR, ®_table }, - {"id_column", PARAM_STR, &id_col }, - {"num_column", PARAM_STR, &num_col }, - {"url_column", PARAM_STR, &url_col }, - {"status_column", PARAM_STR, &status_col }, - {"failover_time_column", PARAM_STR, &failover_time_col }, - {"spare_flag_column", PARAM_STR, &spare_col }, - {"error_column", PARAM_STR, &error_col }, - {"risk_group_column", PARAM_STR, &risk_group_col }, - {"expire_time", INT_PARAM, &default_p_usrloc_cfg.expire_time}, - {"UTC_timestamps", INT_PARAM, &default_p_usrloc_cfg.utc_timestamps}, - {"db_err_threshold", INT_PARAM, &default_p_usrloc_cfg.db_err_threshold}, - {"failover_level", INT_PARAM, &default_p_usrloc_cfg.failover_level}, - {"db_retry_interval", INT_PARAM, &retry_interval }, - {"db_use_transactions", INT_PARAM, &db_use_transactions }, - {"db_transaction_level", INT_PARAM, &db_transaction_level}, - {"write_on_db", INT_PARAM, &db_write }, - {"write_on_master_db", INT_PARAM, &db_master_write }, - {"connection_expires", INT_PARAM, &connection_expires }, - {"alg_location", INT_PARAM, &alg_location }, - {"db_ops_ruid", INT_PARAM, &default_p_usrloc_cfg.db_ops_ruid}, - {"db_update_as_insert", INT_PARAM, &default_p_usrloc_cfg.db_update_as_insert}, - {"mdb_availability_control", INT_PARAM, &mdb_availability_control}, - {0, 0, 0} -}; +static param_export_t params[] = {{"ruid_column", PARAM_STR, &ruid_col}, + {"user_column", PARAM_STR, &user_col}, + {"domain_column", PARAM_STR, &domain_col}, + {"contact_column", PARAM_STR, &contact_col}, + {"expires_column", PARAM_STR, &expires_col}, + {"q_column", PARAM_STR, &q_col}, + {"callid_column", PARAM_STR, &callid_col}, + {"cseq_column", PARAM_STR, &cseq_col}, + {"flags_column", PARAM_STR, &flags_col}, + {"cflags_column", PARAM_STR, &cflags_col}, + {"db_mode", INT_PARAM, &db_mode}, + {"use_domain", INT_PARAM, &use_domain}, + {"use_domain_crc32", INT_PARAM, &use_domain_crc32}, + {"desc_time_order", INT_PARAM, &desc_time_order}, + {"user_agent_column", PARAM_STR, &user_agent_col}, + {"received_column", PARAM_STR, &received_col}, + {"path_column", PARAM_STR, &path_col}, + {"socket_column", PARAM_STR, &sock_col}, + {"methods_column", PARAM_STR, &methods_col}, + {"matching_mode", INT_PARAM, &default_p_usrloc_cfg.matching_mode}, + {"cseq_delay", INT_PARAM, &cseq_delay}, + {"fetch_rows", INT_PARAM, &ul_fetch_rows}, + {"hash_size", INT_PARAM, &ul_hash_size}, + {"nat_bflag", INT_PARAM, &nat_bflag}, + {"default_db_url", PARAM_STR, &default_db_url}, + {"default_db_type", PARAM_STR, &default_db_type}, + {"domain_db", PARAM_STR, &domain_db}, + {"instance_column", PARAM_STR, &instance_col}, + {"reg_id_column", PARAM_STR, ®_id_col}, + {"write_db_url", PARAM_STR, &write_db_url}, + {"read_db_url", PARAM_STR, &read_db_url}, + {"reg_db_table", PARAM_STR, ®_table}, + {"id_column", PARAM_STR, &id_col}, {"num_column", PARAM_STR, &num_col}, + {"url_column", PARAM_STR, &url_col}, + {"status_column", PARAM_STR, &status_col}, + {"failover_time_column", PARAM_STR, &failover_time_col}, + {"spare_flag_column", PARAM_STR, &spare_col}, + {"error_column", PARAM_STR, &error_col}, + {"risk_group_column", PARAM_STR, &risk_group_col}, + {"expire_time", INT_PARAM, &default_p_usrloc_cfg.expire_time}, + {"UTC_timestamps", INT_PARAM, &default_p_usrloc_cfg.utc_timestamps}, + {"db_err_threshold", INT_PARAM, &default_p_usrloc_cfg.db_err_threshold}, + {"failover_level", INT_PARAM, &default_p_usrloc_cfg.failover_level}, + {"db_retry_interval", INT_PARAM, &retry_interval}, + {"db_use_transactions", INT_PARAM, &db_use_transactions}, + {"db_transaction_level", INT_PARAM, &db_transaction_level}, + {"write_on_db", INT_PARAM, &db_write}, + {"write_on_master_db", INT_PARAM, &db_master_write}, + {"connection_expires", INT_PARAM, &connection_expires}, + {"alg_location", INT_PARAM, &alg_location}, + {"db_ops_ruid", INT_PARAM, &default_p_usrloc_cfg.db_ops_ruid}, + {"db_update_as_insert", INT_PARAM, + &default_p_usrloc_cfg.db_update_as_insert}, + {"mdb_availability_control", INT_PARAM, &mdb_availability_control}, + {0, 0, 0}}; stat_export_t mod_stats[] = { - {"registered_users" , STAT_IS_FUNC, (stat_var**)get_number_of_users }, - {0,0,0} -}; + {"registered_users", STAT_IS_FUNC, (stat_var **)get_number_of_users}, + {0, 0, 0}}; #ifdef MI_REMOVED static mi_export_t mi_cmds[] = { - { MI_USRLOC_RM, mi_usrloc_rm_aor, 0, 0, - mi_child_init }, - { MI_USRLOC_RM_CONTACT, mi_usrloc_rm_contact, 0, 0, - mi_child_init }, - { MI_USRLOC_DUMP, mi_usrloc_dump, 0, 0, - 0 }, - { MI_USRLOC_FLUSH, mi_usrloc_flush, MI_NO_INPUT_FLAG, 0, - mi_child_init }, - { MI_USRLOC_ADD, mi_usrloc_add, 0, 0, - mi_child_init }, - { MI_USRLOC_SHOW_CONTACT, mi_usrloc_show_contact, 0, 0, - mi_child_init }, - { "loc_refresh", mi_ul_db_refresh, MI_NO_INPUT_FLAG, 0, mi_child_init }, - { "loc_nr_refresh", mi_loc_nr_refresh, MI_NO_INPUT_FLAG, 0, mi_child_loc_nr_init }, - { 0, 0, 0, 0, 0} -}; + {MI_USRLOC_RM, mi_usrloc_rm_aor, 0, 0, mi_child_init}, + {MI_USRLOC_RM_CONTACT, mi_usrloc_rm_contact, 0, 0, mi_child_init}, + {MI_USRLOC_DUMP, mi_usrloc_dump, 0, 0, 0}, + {MI_USRLOC_FLUSH, mi_usrloc_flush, MI_NO_INPUT_FLAG, 0, mi_child_init}, + {MI_USRLOC_ADD, mi_usrloc_add, 0, 0, mi_child_init}, + {MI_USRLOC_SHOW_CONTACT, mi_usrloc_show_contact, 0, 0, mi_child_init}, + {"loc_refresh", mi_ul_db_refresh, MI_NO_INPUT_FLAG, 0, mi_child_init}, + {"loc_nr_refresh", mi_loc_nr_refresh, MI_NO_INPUT_FLAG, 0, + mi_child_loc_nr_init}, + {0, 0, 0, 0, 0}}; #endif struct module_exports exports = { - "p_usrloc", /*!< module name */ - DEFAULT_DLFLAGS, /*!< dlopen flags */ - cmds, /*!< exported functions */ - params, /*!< export parameters */ - 0, /*!< exported rpc functions */ - 0, /*!< exported pseudo-variables */ - 0, /*!< response handling function */ - mod_init, /*!< module init function */ - child_init, /*!< child init function */ - destroy /*!< module destroy function */ + "p_usrloc", /*!< module name */ + DEFAULT_DLFLAGS, /*!< dlopen flags */ + cmds, /*!< exported functions */ + params, /*!< export parameters */ + 0, /*!< exported rpc functions */ + 0, /*!< exported pseudo-variables */ + 0, /*!< response handling function */ + mod_init, /*!< module init function */ + child_init, /*!< child init function */ + destroy /*!< module destroy function */ }; @@ -308,24 +314,25 @@ static int mod_init(void) #ifdef STATISTICS /* register statistics */ - if (register_module_stats( exports.name, mod_stats)!=0 ) { + if(register_module_stats(exports.name, mod_stats) != 0) { LM_ERR("failed to register core statistics\n"); return -1; } #endif - if(ul_hash_size<=1) + if(ul_hash_size <= 1) ul_hash_size = 512; else - ul_hash_size = 1<=8*sizeof(nat_bflag) ) { + } else if(nat_bflag >= 8 * sizeof(nat_bflag)) { LM_ERR("bflag index (%d) too big!\n", nat_bflag); return -1; } else { - nat_bflag = 1< /* memcpy */ +#include /* memcpy */ #include "../../core/mem/shm_mem.h" #include "../../core/ut.h" #include "../../core/ip_addr.h" @@ -50,43 +50,51 @@ * \param _ci contact informations * \return new created contact on success, 0 on failure */ -ucontact_t* new_ucontact(str* _dom, str* _aor, str* _contact, ucontact_info_t* _ci) +ucontact_t *new_ucontact( + str *_dom, str *_aor, str *_contact, ucontact_info_t *_ci) { ucontact_t *c; - c = (ucontact_t*)shm_malloc(sizeof(ucontact_t)); - if (!c) { + c = (ucontact_t *)shm_malloc(sizeof(ucontact_t)); + if(!c) { LM_ERR("no more shm memory\n"); return 0; } memset(c, 0, sizeof(ucontact_t)); - if (_contact->s && _contact->len > 0) { - if (shm_str_dup( &c->c, _contact) < 0) goto error; + if(_contact->s && _contact->len > 0) { + if(shm_str_dup(&c->c, _contact) < 0) + goto error; } - if (_ci->callid->s && _ci->callid->len > 0) { - if (shm_str_dup( &c->callid, _ci->callid) < 0) goto error; + if(_ci->callid->s && _ci->callid->len > 0) { + if(shm_str_dup(&c->callid, _ci->callid) < 0) + goto error; } - if (_ci->user_agent->s && _ci->user_agent->len > 0) { - if (shm_str_dup( &c->user_agent, _ci->user_agent) < 0) goto error; + if(_ci->user_agent->s && _ci->user_agent->len > 0) { + if(shm_str_dup(&c->user_agent, _ci->user_agent) < 0) + goto error; } - if (_ci->received.s && _ci->received.len > 0) { - if (shm_str_dup( &c->received, &_ci->received) < 0) goto error; + if(_ci->received.s && _ci->received.len > 0) { + if(shm_str_dup(&c->received, &_ci->received) < 0) + goto error; } - if (_ci->path && _ci->path->len > 0) { - if (shm_str_dup( &c->path, _ci->path) < 0) goto error; + if(_ci->path && _ci->path->len > 0) { + if(shm_str_dup(&c->path, _ci->path) < 0) + goto error; } - if (_ci->ruid.s && _ci->ruid.len > 0) { - if (shm_str_dup( &c->ruid, &_ci->ruid) < 0) goto error; + if(_ci->ruid.s && _ci->ruid.len > 0) { + if(shm_str_dup(&c->ruid, &_ci->ruid) < 0) + goto error; } - if (_ci->instance.s && _ci->instance.len > 0) { - if (shm_str_dup( &c->instance, &_ci->instance) < 0) goto error; + if(_ci->instance.s && _ci->instance.len > 0) { + if(shm_str_dup(&c->instance, &_ci->instance) < 0) + goto error; } c->domain = _dom; @@ -105,34 +113,48 @@ ucontact_t* new_ucontact(str* _dom, str* _aor, str* _contact, ucontact_info_t* _ return c; error: LM_ERR("no more shm memory\n"); - if (c->path.s) shm_free(c->path.s); - if (c->received.s) shm_free(c->received.s); - if (c->user_agent.s) shm_free(c->user_agent.s); - if (c->callid.s) shm_free(c->callid.s); - if (c->c.s) shm_free(c->c.s); - if (c->ruid.s) shm_free(c->ruid.s); - if (c->instance.s) shm_free(c->instance.s); + if(c->path.s) + shm_free(c->path.s); + if(c->received.s) + shm_free(c->received.s); + if(c->user_agent.s) + shm_free(c->user_agent.s); + if(c->callid.s) + shm_free(c->callid.s); + if(c->c.s) + shm_free(c->c.s); + if(c->ruid.s) + shm_free(c->ruid.s); + if(c->instance.s) + shm_free(c->instance.s); shm_free(c); return 0; } - /*! * \brief Free all memory associated with given contact structure * \param _c freed contact */ -void free_ucontact(ucontact_t* _c) +void free_ucontact(ucontact_t *_c) { - if (!_c) return; - if (_c->path.s) shm_free(_c->path.s); - if (_c->received.s) shm_free(_c->received.s); - if (_c->user_agent.s) shm_free(_c->user_agent.s); - if (_c->callid.s) shm_free(_c->callid.s); - if (_c->c.s) shm_free(_c->c.s); - if (_c->ruid.s) shm_free(_c->ruid.s); - if (_c->instance.s) shm_free(_c->instance.s); - shm_free( _c ); + if(!_c) + return; + if(_c->path.s) + shm_free(_c->path.s); + if(_c->received.s) + shm_free(_c->received.s); + if(_c->user_agent.s) + shm_free(_c->user_agent.s); + if(_c->callid.s) + shm_free(_c->callid.s); + if(_c->c.s) + shm_free(_c->c.s); + if(_c->ruid.s) + shm_free(_c->ruid.s); + if(_c->instance.s) + shm_free(_c->instance.s); + shm_free(_c); } @@ -141,16 +163,24 @@ void free_ucontact(ucontact_t* _c) * \param _f output file * \param _c printed contact */ -void print_ucontact(FILE* _f, ucontact_t* _c) +void print_ucontact(FILE *_f, ucontact_t *_c) { time_t t = time(0); - char* st; + char *st; switch(_c->state) { - case CS_NEW: st = "CS_NEW"; break; - case CS_SYNC: st = "CS_SYNC"; break; - case CS_DIRTY: st = "CS_DIRTY"; break; - default: st = "CS_UNKNOWN"; break; + case CS_NEW: + st = "CS_NEW"; + break; + case CS_SYNC: + st = "CS_SYNC"; + break; + case CS_DIRTY: + st = "CS_DIRTY"; + break; + default: + st = "CS_UNKNOWN"; + break; } fprintf(_f, "~~~Contact(%p)~~~\n", _c); @@ -158,11 +188,11 @@ void print_ucontact(FILE* _f, ucontact_t* _c) fprintf(_f, "aor : '%.*s'\n", _c->aor->len, ZSW(_c->aor->s)); fprintf(_f, "Contact : '%.*s'\n", _c->c.len, ZSW(_c->c.s)); fprintf(_f, "Expires : "); - if (_c->expires == 0) { + if(_c->expires == 0) { fprintf(_f, "Permanent\n"); - } else if (_c->expires == UL_EXPIRED_TIME) { + } else if(_c->expires == UL_EXPIRED_TIME) { fprintf(_f, "Deleted\n"); - } else if (t > _c->expires) { + } else if(t > _c->expires) { fprintf(_f, "Expired\n"); } else { fprintf(_f, "%u\n", (unsigned int)(_c->expires - t)); @@ -170,25 +200,21 @@ void print_ucontact(FILE* _f, ucontact_t* _c) fprintf(_f, "q : %s\n", q2str(_c->q, 0)); fprintf(_f, "Call-ID : '%.*s'\n", _c->callid.len, ZSW(_c->callid.s)); fprintf(_f, "CSeq : %d\n", _c->cseq); - fprintf(_f, "User-Agent: '%.*s'\n", - _c->user_agent.len, ZSW(_c->user_agent.s)); - fprintf(_f, "received : '%.*s'\n", - _c->received.len, ZSW(_c->received.s)); - fprintf(_f, "Path : '%.*s'\n", - _c->path.len, ZSW(_c->path.s)); + fprintf(_f, "User-Agent: '%.*s'\n", _c->user_agent.len, + ZSW(_c->user_agent.s)); + fprintf(_f, "received : '%.*s'\n", _c->received.len, ZSW(_c->received.s)); + fprintf(_f, "Path : '%.*s'\n", _c->path.len, ZSW(_c->path.s)); fprintf(_f, "State : %s\n", st); fprintf(_f, "Flags : %u\n", _c->flags); - if (_c->sock) { - fprintf(_f, "Sock : %.*s (%p)\n", - _c->sock->sock_str.len,_c->sock->sock_str.s,_c->sock); + if(_c->sock) { + fprintf(_f, "Sock : %.*s (%p)\n", _c->sock->sock_str.len, + _c->sock->sock_str.s, _c->sock); } else { fprintf(_f, "Sock : none (null)\n"); } fprintf(_f, "Methods : %u\n", _c->methods); - fprintf(_f, "ruid : '%.*s'\n", - _c->ruid.len, ZSW(_c->ruid.s)); - fprintf(_f, "instance : '%.*s'\n", - _c->instance.len, ZSW(_c->instance.s)); + fprintf(_f, "ruid : '%.*s'\n", _c->ruid.len, ZSW(_c->ruid.s)); + fprintf(_f, "instance : '%.*s'\n", _c->instance.len, ZSW(_c->instance.s)); fprintf(_f, "reg-id : %u\n", _c->reg_id); fprintf(_f, "next : %p\n", _c->next); fprintf(_f, "prev : %p\n", _c->prev); @@ -202,57 +228,60 @@ void print_ucontact(FILE* _f, ucontact_t* _c) * \param _ci contact informations * \return 0 on success, -1 on failure */ -int mem_update_ucontact(ucontact_t* _c, ucontact_info_t* _ci) +int mem_update_ucontact(ucontact_t *_c, ucontact_info_t *_ci) { -#define update_str(_old,_new) \ - do{\ - if ((_old)->len < (_new)->len) { \ - ptr = (char*)shm_malloc((_new)->len); \ - if (ptr == 0) { \ - LM_ERR("no more shm memory\n"); \ - return -1; \ - }\ - memcpy(ptr, (_new)->s, (_new)->len);\ - if ((_old)->s) shm_free((_old)->s);\ - (_old)->s = ptr;\ - } else {\ - memcpy((_old)->s, (_new)->s, (_new)->len);\ - }\ - (_old)->len = (_new)->len;\ +#define update_str(_old, _new) \ + do { \ + if((_old)->len < (_new)->len) { \ + ptr = (char *)shm_malloc((_new)->len); \ + if(ptr == 0) { \ + LM_ERR("no more shm memory\n"); \ + return -1; \ + } \ + memcpy(ptr, (_new)->s, (_new)->len); \ + if((_old)->s) \ + shm_free((_old)->s); \ + (_old)->s = ptr; \ + } else { \ + memcpy((_old)->s, (_new)->s, (_new)->len); \ + } \ + (_old)->len = (_new)->len; \ } while(0) - char* ptr; + char *ptr; /* Update gruu */ - if(_ci->instance.s!=NULL && _ci->instance.len>0) { - update_str (& _c->instance, &_ci->instance); + if(_ci->instance.s != NULL && _ci->instance.len > 0) { + update_str(&_c->instance, &_ci->instance); } /* Update contact */ - if(_ci->c!=NULL && _ci->c->s!=NULL && _ci->c->len>0) { - update_str( &_c->c, _ci->c); + if(_ci->c != NULL && _ci->c->s != NULL && _ci->c->len > 0) { + update_str(&_c->c, _ci->c); } /* Update callid */ - if(_ci->callid!=NULL && _ci->callid->s!=NULL && _ci->callid->len>0) { - update_str( &_c->callid, _ci->callid); + if(_ci->callid != NULL && _ci->callid->s != NULL && _ci->callid->len > 0) { + update_str(&_c->callid, _ci->callid); } /* No need to update Callid as it is constant * per ucontact (set at insert time) -bogdan */ - update_str( &_c->user_agent, _ci->user_agent); + update_str(&_c->user_agent, _ci->user_agent); - if (_ci->received.s && _ci->received.len) { - update_str( &_c->received, &_ci->received); + if(_ci->received.s && _ci->received.len) { + update_str(&_c->received, &_ci->received); } else { - if (_c->received.s) shm_free(_c->received.s); + if(_c->received.s) + shm_free(_c->received.s); _c->received.s = 0; _c->received.len = 0; } - - if (_ci->path) { - update_str( &_c->path, _ci->path); + + if(_ci->path) { + update_str(&_c->path, _ci->path); } else { - if (_c->path.s) shm_free(_c->path.s); + if(_c->path.s) + shm_free(_c->path.s); _c->path.s = 0; _c->path.len = 0; } @@ -276,33 +305,33 @@ int mem_update_ucontact(ucontact_t* _c, ucontact_info_t* _ci) * \brief Update state of the contact if we are using write-back scheme * \param _c updated contact */ -void st_update_ucontact(ucontact_t* _c) +void st_update_ucontact(ucontact_t *_c) { switch(_c->state) { - case CS_NEW: - /* Contact is new and is not in the database yet, + case CS_NEW: + /* Contact is new and is not in the database yet, * we remain in the same state here because the * contact must be inserted later in the timer */ - break; + break; - case CS_SYNC: - /* For db mode 1 & 2 a modified contact needs to be + case CS_SYNC: + /* For db mode 1 & 2 a modified contact needs to be * updated also in the database, so transit into * CS_DIRTY and let the timer to do the update * again. For db mode 1 we try to update right * now and if fails, let the timer to do the job */ - if (db_mode == WRITE_BACK || db_mode == WRITE_THROUGH) { - _c->state = CS_DIRTY; - } - break; + if(db_mode == WRITE_BACK || db_mode == WRITE_THROUGH) { + _c->state = CS_DIRTY; + } + break; - case CS_DIRTY: - /* Modification of dirty contact results in + case CS_DIRTY: + /* Modification of dirty contact results in * dirty contact again, don't change anything */ - break; + break; } } @@ -312,36 +341,36 @@ void st_update_ucontact(ucontact_t* _c) * \param _c updated contact * \return 1 if the contact should be deleted from memory immediately, 0 otherwise */ -int st_delete_ucontact(ucontact_t* _c) +int st_delete_ucontact(ucontact_t *_c) { switch(_c->state) { - case CS_NEW: - /* Contact is new and isn't in the database + case CS_NEW: + /* Contact is new and isn't in the database * yet, we can delete it from the memory * safely. */ - return 1; + return 1; - case CS_SYNC: - case CS_DIRTY: - /* Contact is in the database, + case CS_SYNC: + case CS_DIRTY: + /* Contact is in the database, * we cannot remove it from the memory * directly, but we can set expires to zero * and the timer will take care of deleting * the contact from the memory as well as * from the database */ - if (db_mode == WRITE_BACK) { - _c->expires = UL_EXPIRED_TIME; - return 0; - } else { - /* WRITE_THROUGH or NO_DB -- we can + if(db_mode == WRITE_BACK) { + _c->expires = UL_EXPIRED_TIME; + return 0; + } else { + /* WRITE_THROUGH or NO_DB -- we can * remove it from memory immediately and * the calling function would also remove * it from the database if needed */ - return 1; - } + return 1; + } } return 0; /* Makes gcc happy */ @@ -353,24 +382,24 @@ int st_delete_ucontact(ucontact_t* _c) * \param _c expired contact * \return 1 if the contact should be removed from the database and 0 otherwise */ -int st_expired_ucontact(ucontact_t* _c) +int st_expired_ucontact(ucontact_t *_c) { - /* There is no need to change contact + /* There is no need to change contact * state, because the contact will * be deleted anyway */ switch(_c->state) { - case CS_NEW: - /* Contact is not in the database + case CS_NEW: + /* Contact is not in the database * yet, remove it from memory only */ - return 0; + return 0; - case CS_SYNC: - case CS_DIRTY: - /* Remove from database here */ - return 1; + case CS_SYNC: + case CS_DIRTY: + /* Remove from database here */ + return 1; } return 0; /* Makes gcc happy */ @@ -382,30 +411,30 @@ int st_expired_ucontact(ucontact_t* _c) * \param _c flushed contact * \return 1 if the contact should be inserted, 2 if update and 0 otherwise */ -int st_flush_ucontact(ucontact_t* _c) +int st_flush_ucontact(ucontact_t *_c) { switch(_c->state) { - case CS_NEW: - /* Contact is new and is not in + case CS_NEW: + /* Contact is new and is not in * the database yet so we have * to insert it */ - _c->state = CS_SYNC; - return 1; + _c->state = CS_SYNC; + return 1; - case CS_SYNC: - /* Contact is synchronized, do + case CS_SYNC: + /* Contact is synchronized, do * nothing */ - return 0; + return 0; - case CS_DIRTY: - /* Contact has been modified and + case CS_DIRTY: + /* Contact has been modified and * is in the db already so we * have to update it */ - _c->state = CS_SYNC; - return 2; + _c->state = CS_SYNC; + return 2; } return 0; /* Makes gcc happy */ @@ -419,23 +448,23 @@ int st_flush_ucontact(ucontact_t* _c) * \param _c inserted contact * \return 0 on success, -1 on failure */ -int db_insert_ucontact(ucontact_t* _c) +int db_insert_ucontact(ucontact_t *_c) { - char* dom; + char *dom; db_key_t keys[18]; db_val_t vals[18]; int nr_cols = 0; int nr_cols_key = 0; - struct udomain * _d; - str user={0, 0}; - str domain={0, 0}; + struct udomain *_d; + str user = {0, 0}; + str domain = {0, 0}; - if (_c->flags & FL_MEM) { + if(_c->flags & FL_MEM) { return 0; } - if(register_udomain(_c->domain->s, &_d) < 0){ + if(register_udomain(_c->domain->s, &_d) < 0) { return -1; } LM_INFO("Domain set for contact %.*s\n", _c->domain->len, _c->domain->s); @@ -458,7 +487,7 @@ int db_insert_ucontact(ucontact_t* _c) vals[nr_cols].nul = 0; dom = memchr(_c->aor->s, '@', _c->aor->len); - if (dom==0) { + if(dom == 0) { LM_INFO("*** use domain and AOR does not contain @\n"); vals[nr_cols].val.str_val.len = 0; vals[nr_cols].val.str_val.s = 0; @@ -468,8 +497,9 @@ int db_insert_ucontact(ucontact_t* _c) vals[nr_cols].val.str_val.len = _c->aor->s + _c->aor->len - dom - 1; } domain = vals[nr_cols].val.str_val; - LM_INFO("** Username=%.*s Domain=%.*s\n", vals[0].val.str_val.len, vals[0].val.str_val.s, - vals[nr_cols].val.str_val.len, vals[nr_cols].val.str_val.s); + LM_INFO("** Username=%.*s Domain=%.*s\n", vals[0].val.str_val.len, + vals[0].val.str_val.s, vals[nr_cols].val.str_val.len, + vals[nr_cols].val.str_val.s); nr_cols++; } user = vals[0].val.str_val; @@ -518,7 +548,7 @@ int db_insert_ucontact(ucontact_t* _c) keys[nr_cols] = &received_col; vals[nr_cols].type = DB1_STR; - if (_c->received.s == 0) { + if(_c->received.s == 0) { vals[nr_cols].nul = 1; } else { vals[nr_cols].nul = 0; @@ -528,7 +558,7 @@ int db_insert_ucontact(ucontact_t* _c) keys[nr_cols] = &path_col; vals[nr_cols].type = DB1_STR; - if (_c->path.s == 0) { + if(_c->path.s == 0) { vals[nr_cols].nul = 1; } else { vals[nr_cols].nul = 0; @@ -538,7 +568,7 @@ int db_insert_ucontact(ucontact_t* _c) keys[nr_cols] = &sock_col; vals[nr_cols].type = DB1_STR; - if (_c->sock) { + if(_c->sock) { vals[nr_cols].val.str_val = _c->sock->sock_str; vals[nr_cols].nul = 0; } else { @@ -548,7 +578,7 @@ int db_insert_ucontact(ucontact_t* _c) keys[nr_cols] = &methods_col; vals[nr_cols].type = DB1_BITMAP; - if (_c->methods == 0xFFFFFFFF) { + if(_c->methods == 0xFFFFFFFF) { vals[nr_cols].nul = 1; } else { vals[nr_cols].val.bitmap_val = _c->methods; @@ -563,8 +593,7 @@ int db_insert_ucontact(ucontact_t* _c) nr_cols++; keys[nr_cols] = &ruid_col; - if(_c->ruid.len>0) - { + if(_c->ruid.len > 0) { vals[nr_cols].type = DB1_STR; vals[nr_cols].nul = 0; vals[nr_cols].val.str_val = _c->ruid; @@ -574,8 +603,7 @@ int db_insert_ucontact(ucontact_t* _c) nr_cols++; keys[nr_cols] = &instance_col; - if(_c->instance.len>0) - { + if(_c->instance.len > 0) { vals[nr_cols].type = DB1_STR; vals[nr_cols].nul = 0; vals[nr_cols].val.str_val = _c->instance; @@ -590,10 +618,11 @@ int db_insert_ucontact(ucontact_t* _c) vals[nr_cols].val.int_val = (int)_c->reg_id; nr_cols++; - nr_cols_key = nr_cols; + nr_cols_key = nr_cols; /* to prevent errors from the DB because of duplicated entries */ - - if (ul_db_layer_replace(_d, &user, &domain, keys, vals, nr_cols, nr_cols_key) <0) { + + if(ul_db_layer_replace(_d, &user, &domain, keys, vals, nr_cols, nr_cols_key) + < 0) { LM_ERR("inserting contact in db failed\n"); return -1; } @@ -608,9 +637,9 @@ int db_insert_ucontact(ucontact_t* _c) * \return 0 on success, -1 on failure */ -int db_update_ucontact_addr_new(ucontact_t* _c) +int db_update_ucontact_addr_new(ucontact_t *_c) { - char* dom; + char *dom; db_key_t keys1[4]; db_val_t vals1[4]; int n1 = 0; @@ -620,12 +649,12 @@ int db_update_ucontact_addr_new(ucontact_t* _c) int nr_cols2 = 0; - if (_c->flags & FL_MEM) { + if(_c->flags & FL_MEM) { return 0; } - struct udomain * _d; - if(register_udomain(_c->domain->s, &_d) < 0){ + struct udomain *_d; + if(register_udomain(_c->domain->s, &_d) < 0) { return -1; } @@ -640,10 +669,11 @@ int db_update_ucontact_addr_new(ucontact_t* _c) vals1[n1].type = DB1_STR; vals1[n1].nul = 0; vals1[n1].val.str_val = _c->c; - LM_DBG("contact:%.*s\n", vals1[n1].val.str_val.len, vals1[n1].val.str_val.s); + LM_DBG("contact:%.*s\n", vals1[n1].val.str_val.len, + vals1[n1].val.str_val.s); n1++; - switch (cfg_get(p_usrloc,p_usrloc_cfg,matching_mode)) { + switch(cfg_get(p_usrloc, p_usrloc_cfg, matching_mode)) { case CONTACT_ONLY: /* update call-id */ keys2[nr_cols2] = &callid_col; @@ -654,7 +684,7 @@ int db_update_ucontact_addr_new(ucontact_t* _c) /* update path */ keys2[nr_cols2] = &path_col; vals2[nr_cols2].type = DB1_STR; - if (_c->path.s == 0) { + if(_c->path.s == 0) { vals2[nr_cols2].nul = 1; } else { vals2[nr_cols2].nul = 0; @@ -667,12 +697,13 @@ int db_update_ucontact_addr_new(ucontact_t* _c) vals1[n1].type = DB1_STR; vals1[n1].nul = 0; vals1[n1].val.str_val = _c->callid; - LM_DBG("callid:%.*s\n", vals1[n1].val.str_val.len, vals1[n1].val.str_val.s); + LM_DBG("callid:%.*s\n", vals1[n1].val.str_val.len, + vals1[n1].val.str_val.s); n1++; /* update path */ keys2[nr_cols2] = &path_col; vals2[nr_cols2].type = DB1_STR; - if (_c->path.s == 0) { + if(_c->path.s == 0) { vals2[nr_cols2].nul = 1; } else { vals2[nr_cols2].nul = 0; @@ -683,13 +714,14 @@ int db_update_ucontact_addr_new(ucontact_t* _c) case CONTACT_PATH: keys1[n1] = &path_col; vals1[n1].type = DB1_STR; - if (_c->path.s == 0) { + if(_c->path.s == 0) { vals1[n1].nul = 1; LM_DBG("path: NULL\n"); } else { vals1[n1].nul = 0; vals1[n1].val.str_val = _c->path; - LM_DBG("path:%.*s\n", vals1[n1].val.str_val.len, vals1[n1].val.str_val.s); + LM_DBG("path:%.*s\n", vals1[n1].val.str_val.len, + vals1[n1].val.str_val.s); } n1++; /* update call-id */ @@ -700,7 +732,8 @@ int db_update_ucontact_addr_new(ucontact_t* _c) nr_cols2++; break; default: - LM_CRIT("unknown matching_mode %d\n", cfg_get(p_usrloc,p_usrloc_cfg,matching_mode)); + LM_CRIT("unknown matching_mode %d\n", + cfg_get(p_usrloc, p_usrloc_cfg, matching_mode)); return -1; } @@ -742,7 +775,7 @@ int db_update_ucontact_addr_new(ucontact_t* _c) keys2[nr_cols2] = &received_col; vals2[nr_cols2].type = DB1_STR; - if (_c->received.s == 0) { + if(_c->received.s == 0) { vals2[nr_cols2].nul = 1; } else { vals2[nr_cols2].nul = 0; @@ -752,7 +785,7 @@ int db_update_ucontact_addr_new(ucontact_t* _c) keys2[nr_cols2] = &sock_col; vals2[nr_cols2].type = DB1_STR; - if (_c->sock) { + if(_c->sock) { vals2[nr_cols2].val.str_val = _c->sock->sock_str; vals2[nr_cols2].nul = 0; } else { @@ -762,7 +795,7 @@ int db_update_ucontact_addr_new(ucontact_t* _c) keys2[nr_cols2] = &methods_col; vals2[nr_cols2].type = DB1_BITMAP; - if (_c->methods == 0xFFFFFFFF) { + if(_c->methods == 0xFFFFFFFF) { vals2[nr_cols2].nul = 1; } else { vals2[nr_cols2].val.bitmap_val = _c->methods; @@ -777,8 +810,7 @@ int db_update_ucontact_addr_new(ucontact_t* _c) nr_cols2++; keys2[nr_cols2] = &ruid_col; - if(_c->ruid.len>0) - { + if(_c->ruid.len > 0) { vals2[nr_cols2].type = DB1_STR; vals2[nr_cols2].nul = 0; vals2[nr_cols2].val.str_val = _c->ruid; @@ -788,8 +820,7 @@ int db_update_ucontact_addr_new(ucontact_t* _c) nr_cols2++; keys2[nr_cols2] = &instance_col; - if(_c->instance.len>0) - { + if(_c->instance.len > 0) { vals2[nr_cols2].type = DB1_STR; vals2[nr_cols2].nul = 0; vals2[nr_cols2].val.str_val = _c->instance; @@ -804,35 +835,36 @@ int db_update_ucontact_addr_new(ucontact_t* _c) vals2[nr_cols2].val.int_val = (int)_c->reg_id; nr_cols2++; - keys2[nr_cols2] = &uniq_col; - if(_c->uniq.len>0) - { - vals2[nr_cols2].type = DB1_STR; - vals2[nr_cols2].nul = 0; - vals2[nr_cols2].val.str_val = _c->uniq; + keys2[nr_cols2] = &uniq_col; + if(_c->uniq.len > 0) { + vals2[nr_cols2].type = DB1_STR; + vals2[nr_cols2].nul = 0; + vals2[nr_cols2].val.str_val = _c->uniq; } else { - vals2[nr_cols2].nul = 1; + vals2[nr_cols2].nul = 1; } nr_cols2++; keys2[nr_cols2] = &contact_col; vals2[nr_cols2].type = DB1_STR; vals2[nr_cols2].nul = 0; vals2[nr_cols2].val.str_val = _c->c; - LM_DBG("contact:%.*s\n", vals2[nr_cols2].val.str_val.len, vals2[nr_cols2].val.str_val.s); + LM_DBG("contact:%.*s\n", vals2[nr_cols2].val.str_val.len, + vals2[nr_cols2].val.str_val.s); nr_cols2++; keys2[nr_cols2] = &user_col; vals2[nr_cols2].type = DB1_STR; vals2[nr_cols2].nul = 0; vals2[nr_cols2].val.str_val = *_c->aor; - LM_DBG("aor:%.*s\n", vals2[nr_cols2].val.str_val.len, vals2[nr_cols2].val.str_val.s); + LM_DBG("aor:%.*s\n", vals2[nr_cols2].val.str_val.len, + vals2[nr_cols2].val.str_val.s); nr_cols2++; - if (use_domain) { + if(use_domain) { keys1[n1] = &domain_col; vals1[n1].type = DB1_STR; vals1[n1].nul = 0; dom = memchr(_c->aor->s, '@', _c->aor->len); - if (dom==0) { + if(dom == 0) { vals1[0].val.str_val.len = 0; vals1[n1].val.str_val = *_c->aor; } else { @@ -842,16 +874,16 @@ int db_update_ucontact_addr_new(ucontact_t* _c) } n1++; } -/* + /* if (ul_dbf.use_table(ul_dbh, _c->domain) < 0) { LM_ERR("sql use_table failed\n"); return -1; } */ - if (ul_db_layer_update(_d, &vals1[0].val.str_val, &vals1[n1-1].val.str_val, keys1, 0, vals1, keys2, vals2, - n1, nr_cols2) < 0) - { + if(ul_db_layer_update(_d, &vals1[0].val.str_val, &vals1[n1 - 1].val.str_val, + keys1, 0, vals1, keys2, vals2, n1, nr_cols2) + < 0) { LM_ERR("updating database failed\n"); return -1; } @@ -860,9 +892,9 @@ int db_update_ucontact_addr_new(ucontact_t* _c) return 0; } -int db_update_ucontact_addr(ucontact_t* _c) +int db_update_ucontact_addr(ucontact_t *_c) { - char* dom; + char *dom; db_key_t keys1[4]; db_val_t vals1[4]; int n1; @@ -871,11 +903,11 @@ int db_update_ucontact_addr(ucontact_t* _c) db_val_t vals2[14]; int nr_cols2; - if (_c->flags & FL_MEM) { + if(_c->flags & FL_MEM) { return 0; } - struct udomain * _d; - if(register_udomain(_c->domain->s, &_d) < 0){ + struct udomain *_d; + if(register_udomain(_c->domain->s, &_d) < 0) { return -1; } @@ -905,9 +937,9 @@ int db_update_ucontact_addr(ucontact_t* _c) vals1[n1].type = DB1_STR; vals1[n1].nul = 0; vals1[n1].val.str_val = _c->c; - - n1++; - keys1[n1] = &callid_col; + + n1++; + keys1[n1] = &callid_col; vals1[n1].type = DB1_STR; vals1[n1].nul = 0; vals1[n1].val.str_val = _c->callid; @@ -939,15 +971,15 @@ int db_update_ucontact_addr(ucontact_t* _c) vals2[5].val.str_val = _c->user_agent; vals2[6].type = DB1_STR; - if (_c->received.s == 0) { + if(_c->received.s == 0) { vals2[6].nul = 1; } else { vals2[6].nul = 0; vals2[6].val.str_val = _c->received; } - + vals2[7].type = DB1_STR; - if (_c->path.s == 0) { + if(_c->path.s == 0) { vals2[7].nul = 1; } else { vals2[7].nul = 0; @@ -955,7 +987,7 @@ int db_update_ucontact_addr(ucontact_t* _c) } vals2[8].type = DB1_STR; - if (_c->sock) { + if(_c->sock) { vals2[8].val.str_val = _c->sock->sock_str; vals2[8].nul = 0; } else { @@ -963,7 +995,7 @@ int db_update_ucontact_addr(ucontact_t* _c) } vals2[9].type = DB1_BITMAP; - if (_c->methods == 0xFFFFFFFF) { + if(_c->methods == 0xFFFFFFFF) { vals2[9].nul = 1; } else { vals2[9].val.bitmap_val = _c->methods; @@ -974,19 +1006,17 @@ int db_update_ucontact_addr(ucontact_t* _c) vals2[10].nul = 0; vals2[10].val.time_val = ul_db_datetime_set(_c->last_modified); - nr_cols2 = 11; - if(_c->ruid.len>0) - { + nr_cols2 = 11; + if(_c->ruid.len > 0) { vals2[nr_cols2].type = DB1_STR; vals2[nr_cols2].nul = 0; vals2[nr_cols2].val.str_val = _c->ruid; } else { vals2[nr_cols2].nul = 1; } - - nr_cols2++; - if(_c->instance.len>0) - { + + nr_cols2++; + if(_c->instance.len > 0) { vals2[nr_cols2].type = DB1_STR; vals2[nr_cols2].nul = 0; vals2[nr_cols2].val.str_val = _c->instance; @@ -994,19 +1024,19 @@ int db_update_ucontact_addr(ucontact_t* _c) vals2[nr_cols2].nul = 1; } - nr_cols2++; + nr_cols2++; vals2[nr_cols2].type = DB1_INT; vals2[nr_cols2].nul = 0; vals2[nr_cols2].val.int_val = (int)_c->reg_id; nr_cols2++; - if (use_domain) { - keys1[n1] = &domain_col; - vals1[n1].type = DB1_STR; - vals1[n1].nul = 0; + if(use_domain) { + keys1[n1] = &domain_col; + vals1[n1].type = DB1_STR; + vals1[n1].nul = 0; dom = memchr(_c->aor->s, '@', _c->aor->len); - if (dom==0) { + if(dom == 0) { vals1[0].val.str_val.len = 0; vals1[n1].val.str_val = *_c->aor; } else { @@ -1017,9 +1047,9 @@ int db_update_ucontact_addr(ucontact_t* _c) n1++; } - if (ul_db_layer_update(_d, &vals1[0].val.str_val, &vals1[n1-1].val.str_val, keys1, 0, vals1, keys2, vals2, - n1, nr_cols2) < 0) - { + if(ul_db_layer_update(_d, &vals1[0].val.str_val, &vals1[n1 - 1].val.str_val, + keys1, 0, vals1, keys2, vals2, n1, nr_cols2) + < 0) { LM_ERR("updating database failed\n"); return -1; } @@ -1027,181 +1057,180 @@ int db_update_ucontact_addr(ucontact_t* _c) return 0; } - /*! +/*! * \brief Update contact in the database by ruid * \param _c updated contact * \return 0 on success, -1 on failure */ -int db_update_ucontact_ruid(ucontact_t* _c) +int db_update_ucontact_ruid(ucontact_t *_c) { - db_key_t keys1[1]; - db_val_t vals1[1]; - int n1; - - db_key_t keys2[17]; - db_val_t vals2[17]; - int n2; - - char *dom; - str user, domain; - - if (_c->flags & FL_MEM) { - return 0; - } - - if(_c->ruid.len<=0) { - LM_ERR("updating record in database failed - empty ruid\n"); - return -1; - } - - struct udomain * _d; - if(register_udomain(_c->domain->s, &_d) < 0){ - return -1; - } - - n1 = 0; - keys1[n1] = &ruid_col; - vals1[n1].type = DB1_STR; - vals1[n1].nul = 0; - vals1[n1].val.str_val = _c->ruid; - n1++; - - n2 = 0; - keys2[n2] = &contact_col; - vals2[n2].type = DB1_STR; - vals2[n2].nul = 0; - vals2[n2].val.str_val = _c->c; - n2++; - - keys2[n2] = &expires_col; - vals2[n2].type = DB1_DATETIME; - vals2[n2].nul = 0; - vals2[n2].val.time_val = ul_db_datetime_set(_c->expires); - n2++; - - keys2[n2] = &q_col; - vals2[n2].type = DB1_DOUBLE; - vals2[n2].nul = 0; - vals2[n2].val.double_val = q2double(_c->q); - n2++; - - keys2[n2] = &cseq_col; - vals2[n2].type = DB1_INT; - vals2[n2].nul = 0; - vals2[n2].val.int_val = _c->cseq; - n2++; - - keys2[n2] = &flags_col; - vals2[n2].type = DB1_INT; - vals2[n2].nul = 0; - vals2[n2].val.bitmap_val = _c->flags; - n2++; - - keys2[n2] = &cflags_col; - vals2[n2].type = DB1_INT; - vals2[n2].nul = 0; - vals2[n2].val.bitmap_val = _c->cflags; - n2++; - - keys2[n2] = &user_agent_col; - vals2[n2].type = DB1_STR; - vals2[n2].nul = 0; - vals2[n2].val.str_val = _c->user_agent; - n2++; - - keys2[n2] = &received_col; - vals2[n2].type = DB1_STR; - if (_c->received.s == 0) { - vals2[n2].nul = 1; - } else { - vals2[n2].nul = 0; - vals2[n2].val.str_val = _c->received; - } - n2++; - - keys2[n2] = &path_col; - vals2[n2].type = DB1_STR; - if (_c->path.s == 0) { - vals2[n2].nul = 1; - } else { - vals2[n2].nul = 0; - vals2[n2].val.str_val = _c->path; - } - n2++; - - keys2[n2] = &sock_col; - vals2[n2].type = DB1_STR; - if (_c->sock) { - vals2[n2].val.str_val = _c->sock->sock_str; - vals2[n2].nul = 0; - } else { - vals2[n2].nul = 1; - } - n2++; - - keys2[n2] = &methods_col; - vals2[n2].type = DB1_BITMAP; - if (_c->methods == 0xFFFFFFFF) { - vals2[n2].nul = 1; - } else { - vals2[n2].val.bitmap_val = _c->methods; - vals2[n2].nul = 0; - } - n2++; - - keys2[n2] = &last_mod_col; - vals2[n2].type = DB1_DATETIME; - vals2[n2].nul = 0; - vals2[n2].val.time_val = ul_db_datetime_set(_c->last_modified); - n2++; - - keys2[n2] = &callid_col; - vals2[n2].type = DB1_STR; - vals2[n2].nul = 0; - vals2[n2].val.str_val = _c->callid; - n2++; - - keys2[n2] = &instance_col; - if(_c->instance.len>0) - { - vals2[n2].type = DB1_STR; - vals2[n2].nul = 0; - vals2[n2].val.str_val = _c->instance; - } else { - vals2[n2].nul = 1; - } - n2++; - - keys2[n2] = ®_id_col; - vals2[n2].type = DB1_INT; - vals2[n2].nul = 0; - vals2[n2].val.int_val = (int)_c->reg_id; - n2++; - - user = *_c->aor; - - if (use_domain) { - dom = memchr(_c->aor->s, '@', _c->aor->len); - if (dom==0) { - user.len = 0; - domain = *_c->aor; - } else { - user.len = dom - _c->aor->s; - domain.s = dom + 1; - domain.len = _c->aor->s + _c->aor->len - dom - 1; - } - } - - - if (ul_db_layer_update(_d, &user, &domain, keys1, 0, vals1, keys2, vals2, - n1, n2) < 0) { - LM_ERR("updating database failed\n"); - return -1; - } - - - - return 0; + db_key_t keys1[1]; + db_val_t vals1[1]; + int n1; + + db_key_t keys2[17]; + db_val_t vals2[17]; + int n2; + + char *dom; + str user, domain; + + if(_c->flags & FL_MEM) { + return 0; + } + + if(_c->ruid.len <= 0) { + LM_ERR("updating record in database failed - empty ruid\n"); + return -1; + } + + struct udomain *_d; + if(register_udomain(_c->domain->s, &_d) < 0) { + return -1; + } + + n1 = 0; + keys1[n1] = &ruid_col; + vals1[n1].type = DB1_STR; + vals1[n1].nul = 0; + vals1[n1].val.str_val = _c->ruid; + n1++; + + n2 = 0; + keys2[n2] = &contact_col; + vals2[n2].type = DB1_STR; + vals2[n2].nul = 0; + vals2[n2].val.str_val = _c->c; + n2++; + + keys2[n2] = &expires_col; + vals2[n2].type = DB1_DATETIME; + vals2[n2].nul = 0; + vals2[n2].val.time_val = ul_db_datetime_set(_c->expires); + n2++; + + keys2[n2] = &q_col; + vals2[n2].type = DB1_DOUBLE; + vals2[n2].nul = 0; + vals2[n2].val.double_val = q2double(_c->q); + n2++; + + keys2[n2] = &cseq_col; + vals2[n2].type = DB1_INT; + vals2[n2].nul = 0; + vals2[n2].val.int_val = _c->cseq; + n2++; + + keys2[n2] = &flags_col; + vals2[n2].type = DB1_INT; + vals2[n2].nul = 0; + vals2[n2].val.bitmap_val = _c->flags; + n2++; + + keys2[n2] = &cflags_col; + vals2[n2].type = DB1_INT; + vals2[n2].nul = 0; + vals2[n2].val.bitmap_val = _c->cflags; + n2++; + + keys2[n2] = &user_agent_col; + vals2[n2].type = DB1_STR; + vals2[n2].nul = 0; + vals2[n2].val.str_val = _c->user_agent; + n2++; + + keys2[n2] = &received_col; + vals2[n2].type = DB1_STR; + if(_c->received.s == 0) { + vals2[n2].nul = 1; + } else { + vals2[n2].nul = 0; + vals2[n2].val.str_val = _c->received; + } + n2++; + + keys2[n2] = &path_col; + vals2[n2].type = DB1_STR; + if(_c->path.s == 0) { + vals2[n2].nul = 1; + } else { + vals2[n2].nul = 0; + vals2[n2].val.str_val = _c->path; + } + n2++; + + keys2[n2] = &sock_col; + vals2[n2].type = DB1_STR; + if(_c->sock) { + vals2[n2].val.str_val = _c->sock->sock_str; + vals2[n2].nul = 0; + } else { + vals2[n2].nul = 1; + } + n2++; + + keys2[n2] = &methods_col; + vals2[n2].type = DB1_BITMAP; + if(_c->methods == 0xFFFFFFFF) { + vals2[n2].nul = 1; + } else { + vals2[n2].val.bitmap_val = _c->methods; + vals2[n2].nul = 0; + } + n2++; + + keys2[n2] = &last_mod_col; + vals2[n2].type = DB1_DATETIME; + vals2[n2].nul = 0; + vals2[n2].val.time_val = ul_db_datetime_set(_c->last_modified); + n2++; + + keys2[n2] = &callid_col; + vals2[n2].type = DB1_STR; + vals2[n2].nul = 0; + vals2[n2].val.str_val = _c->callid; + n2++; + + keys2[n2] = &instance_col; + if(_c->instance.len > 0) { + vals2[n2].type = DB1_STR; + vals2[n2].nul = 0; + vals2[n2].val.str_val = _c->instance; + } else { + vals2[n2].nul = 1; + } + n2++; + + keys2[n2] = ®_id_col; + vals2[n2].type = DB1_INT; + vals2[n2].nul = 0; + vals2[n2].val.int_val = (int)_c->reg_id; + n2++; + + user = *_c->aor; + + if(use_domain) { + dom = memchr(_c->aor->s, '@', _c->aor->len); + if(dom == 0) { + user.len = 0; + domain = *_c->aor; + } else { + user.len = dom - _c->aor->s; + domain.s = dom + 1; + domain.len = _c->aor->s + _c->aor->len - dom - 1; + } + } + + + if(ul_db_layer_update( + _d, &user, &domain, keys1, 0, vals1, keys2, vals2, n1, n2) + < 0) { + LM_ERR("updating database failed\n"); + return -1; + } + + + return 0; } /*! @@ -1209,18 +1238,18 @@ int db_update_ucontact_ruid(ucontact_t* _c) * \param _c updated contact * \return 0 on success, -1 on failure */ -int db_update_ucontact(ucontact_t* _c) +int db_update_ucontact(ucontact_t *_c) { - unsigned int db_ops_ruid_cfg = cfg_get(p_usrloc, p_usrloc_cfg, db_ops_ruid); + unsigned int db_ops_ruid_cfg = cfg_get(p_usrloc, p_usrloc_cfg, db_ops_ruid); - if (db_ops_ruid_cfg == 0) - return db_update_ucontact_addr(_c); - else if (db_ops_ruid_cfg == 1) - return db_update_ucontact_ruid(_c); - else if (db_ops_ruid_cfg == 2) - return db_update_ucontact_addr_new(_c); + if(db_ops_ruid_cfg == 0) + return db_update_ucontact_addr(_c); + else if(db_ops_ruid_cfg == 1) + return db_update_ucontact_ruid(_c); + else if(db_ops_ruid_cfg == 2) + return db_update_ucontact_addr_new(_c); - return -1; + return -1; } @@ -1229,46 +1258,46 @@ int db_update_ucontact(ucontact_t* _c) * \param _c deleted contact * \return 0 on success, -1 on failure */ -int db_delete_ucontact_addr(ucontact_t* _c) +int db_delete_ucontact_addr(ucontact_t *_c) { - char* dom; + char *dom; db_key_t keys[4]; db_val_t vals[4]; int n; - if (_c->flags & FL_MEM) { + if(_c->flags & FL_MEM) { return 0; } - struct udomain * _d; - if(register_udomain(_c->domain->s, &_d) < 0){ + struct udomain *_d; + if(register_udomain(_c->domain->s, &_d) < 0) { return -1; } - n = 0; + n = 0; keys[n] = &user_col; vals[n].type = DB1_STR; vals[n].nul = 0; vals[n].val.str_val = *_c->aor; n++; - keys[n] = &contact_col; + keys[n] = &contact_col; vals[n].type = DB1_STR; vals[n].nul = 0; vals[n].val.str_val = _c->c; n++; - + keys[n] = &callid_col; vals[n].type = DB1_STR; vals[n].nul = 0; vals[n].val.str_val = _c->callid; n++; - if (use_domain) { - keys[n] = &domain_col; + if(use_domain) { + keys[n] = &domain_col; vals[n].type = DB1_STR; vals[n].nul = 0; dom = memchr(_c->aor->s, '@', _c->aor->len); - if (dom==0) { + if(dom == 0) { vals[0].val.str_val.len = 0; vals[n].val.str_val = *_c->aor; } else { @@ -1279,7 +1308,9 @@ int db_delete_ucontact_addr(ucontact_t* _c) n++; } - if (ul_db_layer_delete(_d, &vals[0].val.str_val, &vals[3].val.str_val, keys, 0, vals, (use_domain) ? (4) : (3)) < 0) { + if(ul_db_layer_delete(_d, &vals[0].val.str_val, &vals[3].val.str_val, keys, + 0, vals, (use_domain) ? (4) : (3)) + < 0) { LM_ERR("deleting from database failed\n"); return -1; } @@ -1292,55 +1323,55 @@ int db_delete_ucontact_addr(ucontact_t* _c) * \param _c deleted contact * \return 0 on success, -1 on failure */ -int db_delete_ucontact_ruid(ucontact_t* _c) +int db_delete_ucontact_ruid(ucontact_t *_c) { - db_key_t keys[1]; - db_val_t vals[1]; - int n; - char * dom; - str user, domain; - - if (_c->flags & FL_MEM) { - return 0; - } - - if(_c->ruid.len<=0) { - LM_ERR("deleting from database failed - empty ruid\n"); - return -1; - } - - struct udomain * _d; - if(register_udomain(_c->domain->s, &_d) < 0){ - return -1; - } - - n = 0; - keys[n] = &ruid_col; - vals[n].type = DB1_STR; - vals[n].nul = 0; - vals[n].val.str_val = _c->ruid; - n++; - - user = *_c->aor; - - if (use_domain) { - dom = memchr(_c->aor->s, '@', _c->aor->len); - if (dom==0) { - user.len = 0; - domain = *_c->aor; - } else { - user.len = dom - _c->aor->s; - domain.s = dom + 1; - domain.len = _c->aor->s + _c->aor->len - dom - 1; - } - } - - if (ul_db_layer_delete(_d, &user, &domain, keys, 0, vals, n) < 0) { - LM_ERR("deleting from database failed\n"); - return -1; - } - - return 0; + db_key_t keys[1]; + db_val_t vals[1]; + int n; + char *dom; + str user, domain; + + if(_c->flags & FL_MEM) { + return 0; + } + + if(_c->ruid.len <= 0) { + LM_ERR("deleting from database failed - empty ruid\n"); + return -1; + } + + struct udomain *_d; + if(register_udomain(_c->domain->s, &_d) < 0) { + return -1; + } + + n = 0; + keys[n] = &ruid_col; + vals[n].type = DB1_STR; + vals[n].nul = 0; + vals[n].val.str_val = _c->ruid; + n++; + + user = *_c->aor; + + if(use_domain) { + dom = memchr(_c->aor->s, '@', _c->aor->len); + if(dom == 0) { + user.len = 0; + domain = *_c->aor; + } else { + user.len = dom - _c->aor->s; + domain.s = dom + 1; + domain.len = _c->aor->s + _c->aor->len - dom - 1; + } + } + + if(ul_db_layer_delete(_d, &user, &domain, keys, 0, vals, n) < 0) { + LM_ERR("deleting from database failed\n"); + return -1; + } + + return 0; } /*! @@ -1348,18 +1379,18 @@ int db_delete_ucontact_ruid(ucontact_t* _c) * \param _c deleted contact * \return 0 on success, -1 on failure */ -int db_delete_ucontact(ucontact_t* _c) +int db_delete_ucontact(ucontact_t *_c) { - unsigned int db_ops_ruid_cfg = cfg_get(p_usrloc, p_usrloc_cfg, db_ops_ruid); + unsigned int db_ops_ruid_cfg = cfg_get(p_usrloc, p_usrloc_cfg, db_ops_ruid); - if (db_ops_ruid_cfg == 0) - return db_delete_ucontact_addr(_c); - else if (db_ops_ruid_cfg == 1) - return db_delete_ucontact_ruid(_c); - else if (db_ops_ruid_cfg == 2) - return db_delete_ucontact_addr(_c); + if(db_ops_ruid_cfg == 0) + return db_delete_ucontact_addr(_c); + else if(db_ops_ruid_cfg == 1) + return db_delete_ucontact_ruid(_c); + else if(db_ops_ruid_cfg == 2) + return db_delete_ucontact_addr(_c); - return -1; + return -1; } /*! @@ -1367,16 +1398,16 @@ int db_delete_ucontact(ucontact_t* _c) * \param _r record the contact belongs * \param _c removed contact */ -static inline void unlink_contact(struct urecord* _r, ucontact_t* _c) +static inline void unlink_contact(struct urecord *_r, ucontact_t *_c) { - if (_c->prev) { + if(_c->prev) { _c->prev->next = _c->next; - if (_c->next) { + if(_c->next) { _c->next->prev = _c->prev; } } else { _r->contacts = _c->next; - if (_c->next) { + if(_c->next) { _c->next->prev = 0; } } @@ -1388,13 +1419,13 @@ static inline void unlink_contact(struct urecord* _r, ucontact_t* _c) * \param _r record that holds the sorted contacts * \param _c new contact */ -static inline void update_contact_pos(struct urecord* _r, ucontact_t* _c) +static inline void update_contact_pos(struct urecord *_r, ucontact_t *_c) { ucontact_t *pos, *ppos; - if (desc_time_order) { + if(desc_time_order) { /* order by time - first the newest */ - if (_c->prev==0) + if(_c->prev == 0) return; unlink_contact(_r, _c); /* insert it at the beginning */ @@ -1404,15 +1435,17 @@ static inline void update_contact_pos(struct urecord* _r, ucontact_t* _c) _r->contacts = _c; } else { /* order by q - first the smaller q */ - if ( (_c->prev==0 || _c->q<=_c->prev->q) - && (_c->next==0 || _c->q>=_c->next->q) ) + if((_c->prev == 0 || _c->q <= _c->prev->q) + && (_c->next == 0 || _c->q >= _c->next->q)) return; /* need to move , but where? */ unlink_contact(_r, _c); _c->next = _c->prev = 0; - for(pos=_r->contacts,ppos=0;pos&&pos->q<_c->q;ppos=pos,pos=pos->next); - if (pos) { - if (!pos->prev) { + for(pos = _r->contacts, ppos = 0; pos && pos->q < _c->q; + ppos = pos, pos = pos->next) + ; + if(pos) { + if(!pos->prev) { pos->prev = _c; _c->next = pos; _r->contacts = _c; @@ -1422,7 +1455,7 @@ static inline void update_contact_pos(struct urecord* _r, ucontact_t* _c) pos->prev->next = _c; pos->prev = _c; } - } else if (ppos) { + } else if(ppos) { ppos->next = _c; _c->prev = ppos; } else { @@ -1439,30 +1472,29 @@ static inline void update_contact_pos(struct urecord* _r, ucontact_t* _c) * \param _ci new contact informations * \return 0 on success, -1 on failure */ -int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci) +int update_ucontact(struct urecord *_r, ucontact_t *_c, ucontact_info_t *_ci) { - int res; + int res; /* we have to update memory in any case, but database directly * only in db_mode 1 */ - if (mem_update_ucontact( _c, _ci) < 0) { + if(mem_update_ucontact(_c, _ci) < 0) { LM_ERR("failed to update memory\n"); return -1; } /* run callbacks for UPDATE event */ - if (exists_ulcb_type(UL_CONTACT_UPDATE)) - { + if(exists_ulcb_type(UL_CONTACT_UPDATE)) { LM_DBG("exists callback for type= UL_CONTACT_UPDATE\n"); - run_ul_callbacks( UL_CONTACT_UPDATE, _c); + run_ul_callbacks(UL_CONTACT_UPDATE, _c); } - if (_r && db_mode!=DB_ONLY) - update_contact_pos( _r, _c); + if(_r && db_mode != DB_ONLY) + update_contact_pos(_r, _c); st_update_ucontact(_c); - if (db_mode == WRITE_THROUGH || db_mode==DB_ONLY) { + if(db_mode == WRITE_THROUGH || db_mode == DB_ONLY) { /* * prevent problems when we're in a failover situation: the first DB contains * the complete location entries, the other misses some of them. Before the @@ -1470,15 +1502,14 @@ int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci) * in the second DB will not work. Thus the expire mechanism don't work, it * takes too long until both DBs have the same number of entries again. */ - if (cfg_get(p_usrloc, p_usrloc_cfg, db_update_as_insert)) - res = db_insert_ucontact(_c); - else - res = db_update_ucontact(_c); - if (res < 0 ) - { - LM_ERR("failed to update database\n"); - return -1; - } else { + if(cfg_get(p_usrloc, p_usrloc_cfg, db_update_as_insert)) + res = db_insert_ucontact(_c); + else + res = db_update_ucontact(_c); + if(res < 0) { + LM_ERR("failed to update database\n"); + return -1; + } else { _c->state = CS_SYNC; } } diff --git a/src/modules/p_usrloc/ucontact.h b/src/modules/p_usrloc/ucontact.h index b8aaa9e8eb7..7fc44f7c620 100644 --- a/src/modules/p_usrloc/ucontact.h +++ b/src/modules/p_usrloc/ucontact.h @@ -47,15 +47,15 @@ * \param _ci contact informations * \return new created contact on success, 0 on failure */ -ucontact_t* new_ucontact(str* _dom, str* _aor, str* _contact, - ucontact_info_t* _ci); +ucontact_t *new_ucontact( + str *_dom, str *_aor, str *_contact, ucontact_info_t *_ci); /*! * \brief Free all memory associated with given contact structure * \param _c freed contact */ -void free_ucontact(ucontact_t* _c); +void free_ucontact(ucontact_t *_c); /*! @@ -63,7 +63,7 @@ void free_ucontact(ucontact_t* _c); * \param _f output file * \param _c printed contact */ -void print_ucontact(FILE* _f, ucontact_t* _c); +void print_ucontact(FILE *_f, ucontact_t *_c); /*! @@ -72,7 +72,7 @@ void print_ucontact(FILE* _f, ucontact_t* _c); * \param _ci contact informations * \return 0 */ -int mem_update_ucontact(ucontact_t* _c, ucontact_info_t *_ci); +int mem_update_ucontact(ucontact_t *_c, ucontact_info_t *_ci); /* ===== State transition functions - for write back cache scheme ======== */ @@ -81,7 +81,7 @@ int mem_update_ucontact(ucontact_t* _c, ucontact_info_t *_ci); * \brief Update state of the contact if we are using write-back scheme * \param _c updated contact */ -void st_update_ucontact(ucontact_t* _c); +void st_update_ucontact(ucontact_t *_c); /*! @@ -89,7 +89,7 @@ void st_update_ucontact(ucontact_t* _c); * \param _c updated contact * \return 1 if the contact should be deleted from memory immediately, 0 otherwise */ -int st_delete_ucontact(ucontact_t* _c); +int st_delete_ucontact(ucontact_t *_c); /*! @@ -97,7 +97,7 @@ int st_delete_ucontact(ucontact_t* _c); * \param _c expired contact * \return 1 if the contact should be removed from the database and 0 otherwise */ -int st_expired_ucontact(ucontact_t* _c); +int st_expired_ucontact(ucontact_t *_c); /*! @@ -105,7 +105,7 @@ int st_expired_ucontact(ucontact_t* _c); * \param _c flushed contact * \return 1 if the contact should be inserted, 2 if update and 0 otherwise */ -int st_flush_ucontact(ucontact_t* _c); +int st_flush_ucontact(ucontact_t *_c); /* ==== Database related functions ====== */ @@ -115,7 +115,7 @@ int st_flush_ucontact(ucontact_t* _c); * \param _c inserted contact * \return 0 on success, -1 on failure */ -int db_insert_ucontact(ucontact_t* _c); +int db_insert_ucontact(ucontact_t *_c); /*! @@ -123,7 +123,7 @@ int db_insert_ucontact(ucontact_t* _c); * \param _c updated contact * \return 0 on success, -1 on failure */ -int db_update_ucontact(ucontact_t* _c); +int db_update_ucontact(ucontact_t *_c); /*! @@ -131,7 +131,7 @@ int db_update_ucontact(ucontact_t* _c); * \param _c deleted contact * \return 0 on success, -1 on failure */ -int db_delete_ucontact(ucontact_t* _c); +int db_delete_ucontact(ucontact_t *_c); /* ====== Module interface ====== */ @@ -145,8 +145,7 @@ struct urecord; * \param _ci new contact informations * \return 0 on success, -1 on failure */ -int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci); - +int update_ucontact(struct urecord *_r, ucontact_t *_c, ucontact_info_t *_ci); #endif diff --git a/src/modules/p_usrloc/udomain.c b/src/modules/p_usrloc/udomain.c index e173b308496..bf370481f8b 100644 --- a/src/modules/p_usrloc/udomain.c +++ b/src/modules/p_usrloc/udomain.c @@ -35,28 +35,28 @@ #include "../../core/socket_info.h" #include "../../core/ut.h" #include "../../core/hashes.h" -#include "p_usrloc_mod.h" /* usrloc module parameters */ +#include "p_usrloc_mod.h" /* usrloc module parameters */ #include "utime.h" #include "ul_db_layer.h" #ifdef STATISTICS -static char *build_stat_name( str* domain, char *var_name) +static char *build_stat_name(str *domain, char *var_name) { int n; char *s; char *p; n = domain->len + 1 + strlen(var_name) + 1; - s = (char*)shm_malloc( n ); - if (s==0) { + s = (char *)shm_malloc(n); + if(s == 0) { LM_ERR("no more shm mem\n"); return 0; } - memcpy( s, domain->s, domain->len); + memcpy(s, domain->s, domain->len); p = s + domain->len; *(p++) = *ksr_stats_namesep; - memcpy( p , var_name, strlen(var_name)); + memcpy(p, var_name, strlen(var_name)); p += strlen(var_name); *(p++) = 0; return s; @@ -72,32 +72,32 @@ static char *build_stat_name( str* domain, char *var_name) * \param _d new created domain * \return 0 on success, -1 on failure */ -int new_udomain(str* _n, int _s, udomain_t** _d) +int new_udomain(str *_n, int _s, udomain_t **_d) { int i; #ifdef STATISTICS char *name; #endif - + /* Must be always in shared memory, since * the cache is accessed from timer which * lives in a separate process */ - *_d = (udomain_t*)shm_malloc(sizeof(udomain_t)); - if (!(*_d)) { + *_d = (udomain_t *)shm_malloc(sizeof(udomain_t)); + if(!(*_d)) { LM_ERR("new_udomain(): No memory left\n"); goto error0; } memset(*_d, 0, sizeof(udomain_t)); - - (*_d)->table = (hslot_t*)shm_malloc(sizeof(hslot_t) * _s); - if (!(*_d)->table) { + + (*_d)->table = (hslot_t *)shm_malloc(sizeof(hslot_t) * _s); + if(!(*_d)->table) { LM_ERR("no memory left 2\n"); goto error1; } (*_d)->name = _n; - + for(i = 0; i < _s; i++) { init_slot(*_d, &((*_d)->table[i]), i); } @@ -106,18 +106,23 @@ int new_udomain(str* _n, int _s, udomain_t** _d) #ifdef STATISTICS /* register the statistics */ - if ( (name=build_stat_name(_n,"users"))==0 || register_stat("usrloc", - name, &(*_d)->users, STAT_NO_RESET|STAT_SHM_NAME)!=0 ) { + if((name = build_stat_name(_n, "users")) == 0 + || register_stat("usrloc", name, &(*_d)->users, + STAT_NO_RESET | STAT_SHM_NAME) + != 0) { LM_ERR("failed to add stat variable\n"); goto error2; } - if ( (name=build_stat_name(_n,"contacts"))==0 || register_stat("usrloc", - name, &(*_d)->contacts, STAT_NO_RESET|STAT_SHM_NAME)!=0 ) { + if((name = build_stat_name(_n, "contacts")) == 0 + || register_stat("usrloc", name, &(*_d)->contacts, + STAT_NO_RESET | STAT_SHM_NAME) + != 0) { LM_ERR("failed to add stat variable\n"); goto error2; } - if ( (name=build_stat_name(_n,"expires"))==0 || register_stat("usrloc", - name, &(*_d)->expires, STAT_SHM_NAME)!=0 ) { + if((name = build_stat_name(_n, "expires")) == 0 + || register_stat("usrloc", name, &(*_d)->expires, STAT_SHM_NAME) + != 0) { LM_ERR("failed to add stat variable\n"); goto error2; } @@ -140,11 +145,11 @@ int new_udomain(str* _n, int _s, udomain_t** _d) * \brief Free all memory allocated for the domain * \param _d freed domain */ -void free_udomain(udomain_t* _d) +void free_udomain(udomain_t *_d) { int i; - - if (_d->table) { + + if(_d->table) { for(i = 0; i < _d->size; i++) { lock_ulslot(_d, i); deinit_slot(_d->table + i); @@ -162,12 +167,12 @@ void free_udomain(udomain_t* _d) * \param _aor address of record * \param _r new created urecord */ -static inline void get_static_urecord(udomain_t* _d, str* _aor, - struct urecord** _r) +static inline void get_static_urecord( + udomain_t *_d, str *_aor, struct urecord **_r) { static struct urecord r; - memset( &r, 0, sizeof(struct urecord) ); + memset(&r, 0, sizeof(struct urecord)); r.aor = *_aor; r.aorhash = ul_get_aorhash(_aor); r.domain = _d->name; @@ -178,23 +183,22 @@ static inline void get_static_urecord(udomain_t* _d, str* _aor, /*! * \brief Debugging helper function */ -void print_udomain(FILE* _f, udomain_t* _d) +void print_udomain(FILE *_f, udomain_t *_d) { int i; - int max=0, slot=0, n=0; - struct urecord* r; + int max = 0, slot = 0, n = 0; + struct urecord *r; fprintf(_f, "---Domain---\n"); fprintf(_f, "name : '%.*s'\n", _d->name->len, ZSW(_d->name->s)); fprintf(_f, "size : %d\n", _d->size); fprintf(_f, "table: %p\n", _d->table); /*fprintf(_f, "lock : %d\n", _d->lock); -- can be a structure --andrei*/ fprintf(_f, "\n"); - for(i=0; i<_d->size; i++) - { + for(i = 0; i < _d->size; i++) { r = _d->table[i].first; n += _d->table[i].n; - if(max<_d->table[i].n){ - max= _d->table[i].n; + if(max < _d->table[i].n) { + max = _d->table[i].n; slot = i; } while(r) { @@ -217,62 +221,62 @@ void print_udomain(FILE* _f, udomain_t* _d) * \param contact contact * \return pointer to the ucontact_info on success, 0 on failure */ -static inline ucontact_info_t* dbrow2info( db_val_t *vals, str *contact) +static inline ucontact_info_t *dbrow2info(db_val_t *vals, str *contact) { static ucontact_info_t ci; static str callid, ua, received, host, path; int port, proto; char *p; - memset( &ci, 0, sizeof(ucontact_info_t)); + memset(&ci, 0, sizeof(ucontact_info_t)); - contact->s = (char*)VAL_STRING(vals); - if (VAL_NULL(vals) || contact->s==0 || contact->s[0]==0) { + contact->s = (char *)VAL_STRING(vals); + if(VAL_NULL(vals) || contact->s == 0 || contact->s[0] == 0) { LM_CRIT("bad contact\n"); return 0; } contact->len = strlen(contact->s); - if (VAL_NULL(vals+1)) { + if(VAL_NULL(vals + 1)) { LM_CRIT("empty expire\n"); return 0; } - ci.expires = ul_db_datetime_get(VAL_TIME(vals+1)); + ci.expires = ul_db_datetime_get(VAL_TIME(vals + 1)); - if (VAL_NULL(vals+2)) { + if(VAL_NULL(vals + 2)) { LM_CRIT("empty q\n"); return 0; } - ci.q = double2q(VAL_DOUBLE(vals+2)); + ci.q = double2q(VAL_DOUBLE(vals + 2)); - if (VAL_NULL(vals+4)) { + if(VAL_NULL(vals + 4)) { LM_CRIT("empty cseq_nr\n"); return 0; } - ci.cseq = VAL_INT(vals+4); + ci.cseq = VAL_INT(vals + 4); - callid.s = (char*)VAL_STRING(vals+3); - if (VAL_NULL(vals+3) || !callid.s || !callid.s[0]) { + callid.s = (char *)VAL_STRING(vals + 3); + if(VAL_NULL(vals + 3) || !callid.s || !callid.s[0]) { LM_CRIT("bad callid\n"); return 0; } - callid.len = strlen(callid.s); + callid.len = strlen(callid.s); ci.callid = &callid; - if (VAL_NULL(vals+5)) { + if(VAL_NULL(vals + 5)) { LM_CRIT("empty flag\n"); return 0; } - ci.flags = VAL_BITMAP(vals+5); + ci.flags = VAL_BITMAP(vals + 5); - if (VAL_NULL(vals+6)) { + if(VAL_NULL(vals + 6)) { LM_CRIT("empty cflag\n"); return 0; } - ci.cflags = VAL_BITMAP(vals+6); + ci.cflags = VAL_BITMAP(vals + 6); - ua.s = (char*)VAL_STRING(vals+7); - if (VAL_NULL(vals+7) || !ua.s || !ua.s[0]) { + ua.s = (char *)VAL_STRING(vals + 7); + if(VAL_NULL(vals + 7) || !ua.s || !ua.s[0]) { ua.s = 0; ua.len = 0; } else { @@ -280,74 +284,72 @@ static inline ucontact_info_t* dbrow2info( db_val_t *vals, str *contact) } ci.user_agent = &ua; - received.s = (char*)VAL_STRING(vals+8); - if (VAL_NULL(vals+8) || !received.s || !received.s[0]) { + received.s = (char *)VAL_STRING(vals + 8); + if(VAL_NULL(vals + 8) || !received.s || !received.s[0]) { received.len = 0; received.s = 0; } else { received.len = strlen(received.s); } ci.received = received; - - path.s = (char*)VAL_STRING(vals+9); - if (VAL_NULL(vals+9) || !path.s || !path.s[0]) { - path.len = 0; - path.s = 0; - } else { - path.len = strlen(path.s); - } - ci.path= &path; + + path.s = (char *)VAL_STRING(vals + 9); + if(VAL_NULL(vals + 9) || !path.s || !path.s[0]) { + path.len = 0; + path.s = 0; + } else { + path.len = strlen(path.s); + } + ci.path = &path; /* socket name */ - p = (char*)VAL_STRING(vals+10); - if (VAL_NULL(vals+10) || p==0 || p[0]==0){ + p = (char *)VAL_STRING(vals + 10); + if(VAL_NULL(vals + 10) || p == 0 || p[0] == 0) { ci.sock = 0; } else { - if (parse_phostport( p, &host.s, &host.len, - &port, &proto)!=0){ + if(parse_phostport(p, &host.s, &host.len, &port, &proto) != 0) { LM_ERR("bad socket <%s>\n", p); return 0; } - ci.sock = grep_sock_info( &host, (unsigned short)port, proto); - if (ci.sock==0) { + ci.sock = grep_sock_info(&host, (unsigned short)port, proto); + if(ci.sock == 0) { LM_INFO("non-local socket <%s>...ignoring\n", p); } } /* supported methods */ - if (VAL_NULL(vals+11)) { + if(VAL_NULL(vals + 11)) { ci.methods = ALL_METHODS; } else { - ci.methods = VAL_BITMAP(vals+11); + ci.methods = VAL_BITMAP(vals + 11); } /* last modified time */ - if (!VAL_NULL(vals+12)) { - ci.last_modified = ul_db_datetime_get(VAL_TIME(vals+12)); + if(!VAL_NULL(vals + 12)) { + ci.last_modified = ul_db_datetime_get(VAL_TIME(vals + 12)); } /* record internal uid */ - if (!VAL_NULL(vals+13)) { - ci.ruid.s = (char*)VAL_STRING(vals+13); + if(!VAL_NULL(vals + 13)) { + ci.ruid.s = (char *)VAL_STRING(vals + 13); ci.ruid.len = strlen(ci.ruid.s); } /* sip instance */ - if (!VAL_NULL(vals+14)) { - ci.instance.s = (char*)VAL_STRING(vals+14); + if(!VAL_NULL(vals + 14)) { + ci.instance.s = (char *)VAL_STRING(vals + 14); ci.instance.len = strlen(ci.instance.s); } /* reg-id */ - if (!VAL_NULL(vals+15)) { - ci.reg_id = VAL_UINT(vals+15); + if(!VAL_NULL(vals + 15)) { + ci.reg_id = VAL_UINT(vals + 15); } return &ci; } - /*! * \brief Loads from DB all contacts for an AOR * \param _c database connection @@ -355,36 +357,36 @@ static inline ucontact_info_t* dbrow2info( db_val_t *vals, str *contact) * \param _aor address of record * \return pointer to the record on success, 0 on errors or if nothing is found */ -urecord_t* db_load_urecord(udomain_t* _d, str *_aor) +urecord_t *db_load_urecord(udomain_t *_d, str *_aor) { ucontact_info_t *ci; db_key_t columns[16]; db_key_t keys[2]; db_key_t order; db_val_t vals[2]; - db1_res_t* res = NULL; + db1_res_t *res = NULL; str contact; char *domain; int i; - urecord_t* r; - ucontact_t* c; + urecord_t *r; + ucontact_t *c; keys[0] = &user_col; vals[0].type = DB1_STR; vals[0].nul = 0; - if (use_domain) { + if(use_domain) { keys[1] = &domain_col; vals[1].type = DB1_STR; vals[1].nul = 0; domain = memchr(_aor->s, '@', _aor->len); - vals[0].val.str_val.s = _aor->s; - if (domain==0) { + vals[0].val.str_val.s = _aor->s; + if(domain == 0) { vals[0].val.str_val.len = 0; vals[1].val.str_val = *_aor; } else { vals[0].val.str_val.len = domain - _aor->s; - vals[1].val.str_val.s = domain+1; + vals[1].val.str_val.s = domain + 1; vals[1].val.str_val.len = _aor->s + _aor->len - domain - 1; } } else { @@ -408,19 +410,21 @@ urecord_t* db_load_urecord(udomain_t* _d, str *_aor) columns[14] = &instance_col; columns[15] = ®_id_col; - if (desc_time_order) + if(desc_time_order) order = &last_mod_col; else order = &q_col; - if (ul_db_layer_query(_d, &vals[0].val.str_val, &vals[1].val.str_val, keys, 0, vals, columns, (use_domain)?2:1, 16, order, - &res) < 0) { + if(ul_db_layer_query(_d, &vals[0].val.str_val, &vals[1].val.str_val, keys, + 0, vals, columns, (use_domain) ? 2 : 1, 16, order, &res) + < 0) { LM_ERR("db_query failed\n"); return 0; } - if (RES_ROW_N(res) == 0) { - LM_DBG("aor %.*s not found in table %.*s\n",_aor->len, _aor->s, _d->name->len, _d->name->s); + if(RES_ROW_N(res) == 0) { + LM_DBG("aor %.*s not found in table %.*s\n", _aor->len, _aor->s, + _d->name->len, _d->name->s); ul_db_layer_free_result(_d, res); return 0; @@ -429,17 +433,17 @@ urecord_t* db_load_urecord(udomain_t* _d, str *_aor) r = 0; for(i = 0; i < RES_ROW_N(res); i++) { - ci = dbrow2info( ROW_VALUES(RES_ROWS(res) + i), &contact); - if (ci==0) { - LM_ERR("skipping record for %.*s in table %s\n", - _aor->len, _aor->s, _d->name->s); + ci = dbrow2info(ROW_VALUES(RES_ROWS(res) + i), &contact); + if(ci == 0) { + LM_ERR("skipping record for %.*s in table %s\n", _aor->len, _aor->s, + _d->name->s); continue; } - - if ( r==0 ) - get_static_urecord( _d, _aor, &r); - if ( (c=mem_insert_ucontact(r, &contact, ci)) == 0) { + if(r == 0) + get_static_urecord(_d, _aor, &r); + + if((c = mem_insert_ucontact(r, &contact, ci)) == 0) { LM_ERR("mem_insert failed\n"); free_urecord(r); ul_db_layer_free_result(_d, res); @@ -462,22 +466,22 @@ urecord_t* db_load_urecord(udomain_t* _d, str *_aor) * \param _aor address of record * \return pointer to the record on success, 0 on errors or if nothing is found */ -urecord_t* db_load_urecord_by_ruid(udomain_t* _d, str *_ruid) +urecord_t *db_load_urecord_by_ruid(udomain_t *_d, str *_ruid) { ucontact_info_t *ci; db_key_t columns[18]; db_key_t keys[1]; db_key_t order; db_val_t vals[1]; - db1_res_t* res = NULL; + db1_res_t *res = NULL; db_row_t *row; str contact; str aor; char aorbuf[512]; str domain; - urecord_t* r; - ucontact_t* c; + urecord_t *r; + ucontact_t *c; keys[0] = &ruid_col; vals[0].type = DB1_STR; @@ -503,19 +507,20 @@ urecord_t* db_load_urecord_by_ruid(udomain_t* _d, str *_ruid) columns[16] = &user_col; columns[17] = &domain_col; - if (desc_time_order) + if(desc_time_order) order = &last_mod_col; else order = &q_col; - if (ul_db_layer_query(_d, &vals[0].val.str_val, NULL, keys, 0, vals, columns, 1, 18, order, - &res) < 0) { + if(ul_db_layer_query(_d, &vals[0].val.str_val, NULL, keys, 0, vals, columns, + 1, 18, order, &res) + < 0) { LM_ERR("db_query failed\n"); return 0; } - if (RES_ROW_N(res) == 0) { - LM_DBG("aor %.*s not found in table %.*s\n",_ruid->len, _ruid->s, + if(RES_ROW_N(res) == 0) { + LM_DBG("aor %.*s not found in table %.*s\n", _ruid->len, _ruid->s, _d->name->len, _d->name->s); ul_db_layer_free_result(_d, res); return 0; @@ -527,20 +532,21 @@ urecord_t* db_load_urecord_by_ruid(udomain_t* _d, str *_ruid) row = RES_ROWS(res); ci = dbrow2info(ROW_VALUES(RES_ROWS(res)), &contact); - if (ci==0) { - LM_ERR("skipping record for %.*s in table %s\n", - _ruid->len, _ruid->s, _d->name->s); + if(ci == 0) { + LM_ERR("skipping record for %.*s in table %s\n", _ruid->len, _ruid->s, + _d->name->s); goto done; } - aor.s = (char*)VAL_STRING(ROW_VALUES(row) + 15); + aor.s = (char *)VAL_STRING(ROW_VALUES(row) + 15); aor.len = strlen(aor.s); - if (use_domain) { - domain.s = (char*)VAL_STRING(ROW_VALUES(row) + 17); - if (VAL_NULL(ROW_VALUES(row)+17) || domain.s==0 || domain.s[0]==0){ - LM_CRIT("empty domain record for user %.*s...skipping\n", - aor.len, aor.s); + if(use_domain) { + domain.s = (char *)VAL_STRING(ROW_VALUES(row) + 17); + if(VAL_NULL(ROW_VALUES(row) + 17) || domain.s == 0 + || domain.s[0] == 0) { + LM_CRIT("empty domain record for user %.*s...skipping\n", aor.len, + aor.s); goto done; } domain.len = strlen(domain.s); @@ -555,9 +561,9 @@ urecord_t* db_load_urecord_by_ruid(udomain_t* _d, str *_ruid) aor.s = aorbuf; aor.s[aor.len] = '\0'; } - get_static_urecord( _d, &aor, &r); + get_static_urecord(_d, &aor, &r); - if ( (c=mem_insert_ucontact(r, &contact, ci)) == 0) { + if((c = mem_insert_ucontact(r, &contact, ci)) == 0) { LM_ERR("mem_insert failed\n"); free_urecord(r); ul_db_layer_free_result(_d, res); @@ -606,7 +612,6 @@ int db_timer_udomain(udomain_t* _d) */ - /*! * \brief Insert a new record into domain in memory * \param _d domain the record belongs to @@ -614,18 +619,18 @@ int db_timer_udomain(udomain_t* _d) * \param _r new created record * \return 0 on success, -1 on failure */ -int mem_insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r) +int mem_insert_urecord(udomain_t *_d, str *_aor, struct urecord **_r) { int sl; - - if (new_urecord(_d->name, _aor, _r) < 0) { + + if(new_urecord(_d->name, _aor, _r) < 0) { LM_ERR("creating urecord failed\n"); return -1; } - sl = ((*_r)->aorhash)&(_d->size-1); + sl = ((*_r)->aorhash) & (_d->size - 1); slot_add(&_d->table[sl], *_r); - update_stat( _d->users, 1); + update_stat(_d->users, 1); return 0; } @@ -635,11 +640,11 @@ int mem_insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r) * \param _d domain the record belongs to * \param _r deleted record */ -void mem_delete_urecord(udomain_t* _d, struct urecord* _r) +void mem_delete_urecord(udomain_t *_d, struct urecord *_r) { slot_rem(_r->slot, _r); free_urecord(_r); - update_stat( _d->users, -1); + update_stat(_d->users, -1); } @@ -647,13 +652,12 @@ void mem_delete_urecord(udomain_t* _d, struct urecord* _r) * \brief Run timer handler for given domain * \param _d domain */ -void mem_timer_udomain(udomain_t* _d) +void mem_timer_udomain(udomain_t *_d) { - struct urecord* ptr, *t; + struct urecord *ptr, *t; int i; - for(i=0; i<_d->size; i++) - { + for(i = 0; i < _d->size; i++) { lock_ulslot(_d, i); ptr = _d->table[i].first; @@ -661,7 +665,7 @@ void mem_timer_udomain(udomain_t* _d) while(ptr) { timer_urecord(ptr); /* Remove the entire record if it is empty */ - if (ptr->contacts == 0) { + if(ptr->contacts == 0) { t = ptr; ptr = ptr->next; mem_delete_urecord(_d, t); @@ -679,11 +683,10 @@ void mem_timer_udomain(udomain_t* _d) * \param _d domain * \param _aor address of record, used as hash source for the lock slot */ -void lock_udomain(udomain_t* _d, str* _aor) +void lock_udomain(udomain_t *_d, str *_aor) { unsigned int sl; - if (db_mode!=DB_ONLY) - { + if(db_mode != DB_ONLY) { sl = ul_get_aorhash(_aor) & (_d->size - 1); #ifdef GEN_LOCK_T_PREFERED lock_get(_d->table[sl].lock); @@ -699,11 +702,10 @@ void lock_udomain(udomain_t* _d, str* _aor) * \param _d domain * \param _aor address of record, uses as hash source for the lock slot */ -void unlock_udomain(udomain_t* _d, str* _aor) +void unlock_udomain(udomain_t *_d, str *_aor) { unsigned int sl; - if (db_mode!=DB_ONLY) - { + if(db_mode != DB_ONLY) { sl = ul_get_aorhash(_aor) & (_d->size - 1); #ifdef GEN_LOCK_T_PREFERED lock_release(_d->table[sl].lock); @@ -718,9 +720,9 @@ void unlock_udomain(udomain_t* _d, str* _aor) * \param _d domain * \param i slot number */ -void lock_ulslot(udomain_t* _d, int i) +void lock_ulslot(udomain_t *_d, int i) { - if (db_mode!=DB_ONLY) + if(db_mode != DB_ONLY) #ifdef GEN_LOCK_T_PREFERED lock_get(_d->table[i].lock); #else @@ -734,9 +736,9 @@ void lock_ulslot(udomain_t* _d, int i) * \param _d domain * \param i slot number */ -void unlock_ulslot(udomain_t* _d, int i) +void unlock_ulslot(udomain_t *_d, int i) { - if (db_mode!=DB_ONLY) + if(db_mode != DB_ONLY) #ifdef GEN_LOCK_T_PREFERED lock_release(_d->table[i].lock); #else @@ -745,7 +747,6 @@ void unlock_ulslot(udomain_t* _d, int i) } - /*! * \brief Create and insert a new record * \param _d domain to insert the new record @@ -753,15 +754,15 @@ void unlock_ulslot(udomain_t* _d, int i) * \param _r new created record * \return return 0 on success, -1 on failure */ -int insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r) +int insert_urecord(udomain_t *_d, str *_aor, struct urecord **_r) { - if (db_mode!=DB_ONLY) { - if (mem_insert_urecord(_d, _aor, _r) < 0) { + if(db_mode != DB_ONLY) { + if(mem_insert_urecord(_d, _aor, _r) < 0) { LM_ERR("inserting record failed\n"); return -1; } } else { - get_static_urecord( _d, _aor, _r); + get_static_urecord(_d, _aor, _r); } return 0; } @@ -774,19 +775,19 @@ int insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r) * \param _r new created record * \return 0 if a record was found, 1 if nothing could be found */ -int get_urecord(udomain_t* _d, str* _aor, struct urecord** _r) +int get_urecord(udomain_t *_d, str *_aor, struct urecord **_r) { unsigned int sl, i, aorhash; - urecord_t* r; - if (db_mode!=DB_ONLY) { + urecord_t *r; + if(db_mode != DB_ONLY) { /* search in cache */ aorhash = ul_get_aorhash(_aor); - sl = aorhash&(_d->size-1); + sl = aorhash & (_d->size - 1); r = _d->table[sl].first; - for(i = 0; r!=NULL && i < _d->table[sl].n; i++) { - if((r->aorhash==aorhash) && (r->aor.len==_aor->len) - && !memcmp(r->aor.s,_aor->s,_aor->len)){ + for(i = 0; r != NULL && i < _d->table[sl].n; i++) { + if((r->aorhash == aorhash) && (r->aor.len == _aor->len) + && !memcmp(r->aor.s, _aor->s, _aor->len)) { *_r = r; return 0; } @@ -796,13 +797,13 @@ int get_urecord(udomain_t* _d, str* _aor, struct urecord** _r) } else { /* search in DB */ r = db_load_urecord(_d, _aor); - if (r) { + if(r) { *_r = r; return 0; } } - return 1; /* Nothing found */ + return 1; /* Nothing found */ } /*! @@ -814,25 +815,25 @@ int get_urecord(udomain_t* _d, str* _aor, struct urecord** _r) * \param _c store pointer to contact structure * \return 0 if a record was found, 1 if nothing could be found */ -int get_urecord_by_ruid(udomain_t* _d, unsigned int _aorhash, - str *_ruid, struct urecord** _r, struct ucontact** _c) +int get_urecord_by_ruid(udomain_t *_d, unsigned int _aorhash, str *_ruid, + struct urecord **_r, struct ucontact **_c) { unsigned int sl, i; - urecord_t* r; - ucontact_t* c; + urecord_t *r; + ucontact_t *c; - sl = _aorhash&(_d->size-1); + sl = _aorhash & (_d->size - 1); lock_ulslot(_d, sl); - if (db_mode!=DB_ONLY) { + if(db_mode != DB_ONLY) { /* search in cache */ r = _d->table[sl].first; for(i = 0; i < _d->table[sl].n; i++) { - if(r->aorhash==_aorhash) { + if(r->aorhash == _aorhash) { c = r->contacts; while(c) { - if(c->ruid.len==_ruid->len + if(c->ruid.len == _ruid->len && !memcmp(c->ruid.s, _ruid->s, _ruid->len)) { *_r = r; *_c = c; @@ -845,11 +846,11 @@ int get_urecord_by_ruid(udomain_t* _d, unsigned int _aorhash, } else { /* search in DB */ r = db_load_urecord_by_ruid(_d, _ruid); - if (r) { - if(r->aorhash==_aorhash) { + if(r) { + if(r->aorhash == _aorhash) { c = r->contacts; while(c) { - if(c->ruid.len==_ruid->len + if(c->ruid.len == _ruid->len && !memcmp(c->ruid.s, _ruid->s, _ruid->len)) { *_r = r; *_c = c; @@ -861,7 +862,7 @@ int get_urecord_by_ruid(udomain_t* _d, unsigned int _aorhash, } unlock_ulslot(_d, (_aorhash & (_d->size - 1))); - return -1; /* Nothing found */ + return -1; /* Nothing found */ } @@ -872,14 +873,14 @@ int get_urecord_by_ruid(udomain_t* _d, unsigned int _aorhash, * \param _r deleted record * \return 0 on success, -1 if the record could not be deleted */ -int delete_urecord(udomain_t* _d, str* _aor, struct urecord* _r) +int delete_urecord(udomain_t *_d, str *_aor, struct urecord *_r) { - struct ucontact* c, *t; + struct ucontact *c, *t; - if (db_mode==DB_ONLY) { - if (_r==0) - get_static_urecord( _d, _aor, &_r); - if (db_delete_urecord(_d, _r)<0) { + if(db_mode == DB_ONLY) { + if(_r == 0) + get_static_urecord(_d, _aor, &_r); + if(db_delete_urecord(_d, _r) < 0) { LM_ERR("DB delete failed\n"); return -1; } @@ -887,8 +888,8 @@ int delete_urecord(udomain_t* _d, str* _aor, struct urecord* _r) return 0; } - if (_r==0) { - if (get_urecord(_d, _aor, &_r) > 0) { + if(_r == 0) { + if(get_urecord(_d, _aor, &_r) > 0) { return 0; } } @@ -897,7 +898,7 @@ int delete_urecord(udomain_t* _d, str* _aor, struct urecord* _r) while(c) { t = c; c = c->next; - if (delete_ucontact(_r, t) < 0) { + if(delete_ucontact(_r, t) < 0) { LM_ERR("deleting contact failed\n"); return -1; } diff --git a/src/modules/p_usrloc/udomain.h b/src/modules/p_usrloc/udomain.h index d949f0d94f3..9ccb5eb4672 100644 --- a/src/modules/p_usrloc/udomain.h +++ b/src/modules/p_usrloc/udomain.h @@ -37,24 +37,25 @@ #include "hslot.h" -struct hslot; /*!< Hash table slot */ +struct hslot; /*!< Hash table slot */ struct urecord; /*!< Usrloc record */ /*! \brief * The structure represents a usrloc domain */ -struct udomain { - str* name; /*!< Domain name (NULL terminated) */ - int size; /*!< Hash table size */ - struct hslot* table; /*!< Hash table - array of collision slots */ +struct udomain +{ + str *name; /*!< Domain name (NULL terminated) */ + int size; /*!< Hash table size */ + struct hslot *table; /*!< Hash table - array of collision slots */ /* statistics */ - stat_var *users; /*!< no of registered users */ - stat_var *contacts; /*!< no of registered contacts */ - stat_var *expires; /*!< no of expires */ + stat_var *users; /*!< no of registered users */ + stat_var *contacts; /*!< no of registered contacts */ + stat_var *expires; /*!< no of expires */ /* for ul_db_layer */ - int dbt; /* type of the database */ - db1_con_t * dbh; /* database handle */ + int dbt; /* type of the database */ + db1_con_t *dbh; /* database handle */ }; @@ -66,20 +67,20 @@ struct udomain { * \param _d new created domain * \return 0 on success, -1 on failure */ -int new_udomain(str* _n, int _s, udomain_t** _d); +int new_udomain(str *_n, int _s, udomain_t **_d); /*! * \brief Free all memory allocated for the domain * \param _d freed domain */ -void free_udomain(udomain_t* _d); +void free_udomain(udomain_t *_d); /*! * \brief Print udomain, debugging helper function */ -void print_udomain(FILE* _f, udomain_t* _d); +void print_udomain(FILE *_f, udomain_t *_d); /*! @@ -96,7 +97,7 @@ int db_timer_udomain(udomain_t* _d); * \brief Run timer handler for given domain * \param _d domain */ -void mem_timer_udomain(udomain_t* _d); +void mem_timer_udomain(udomain_t *_d); /*! @@ -106,7 +107,7 @@ void mem_timer_udomain(udomain_t* _d); * \param _r new created record * \return 0 on success, -1 on failure */ -int mem_insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r); +int mem_insert_urecord(udomain_t *_d, str *_aor, struct urecord **_r); /*! @@ -114,8 +115,7 @@ int mem_insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r); * \param _d domain the record belongs to * \param _r deleted record */ -void mem_delete_urecord(udomain_t* _d, struct urecord* _r); - +void mem_delete_urecord(udomain_t *_d, struct urecord *_r); /*! @@ -123,21 +123,21 @@ void mem_delete_urecord(udomain_t* _d, struct urecord* _r); * \param _d domain * \param i slot number */ -void lock_ulslot(udomain_t* _d, int i); +void lock_ulslot(udomain_t *_d, int i); /*! * \brief Release lock for a slot * \param _d domain * \param i slot number */ -void unlock_ulslot(udomain_t* _d, int i); +void unlock_ulslot(udomain_t *_d, int i); /* ===== module interface ======= */ /*! \brief * Timer handler for given domain */ -void lock_udomain(udomain_t* _d, str *_aor); +void lock_udomain(udomain_t *_d, str *_aor); /*! @@ -145,8 +145,7 @@ void lock_udomain(udomain_t* _d, str *_aor); * \param _d domain * \param _aor address of record, uses as hash source for the lock slot */ -void unlock_udomain(udomain_t* _d, str *_aor); - +void unlock_udomain(udomain_t *_d, str *_aor); /*! @@ -156,7 +155,7 @@ void unlock_udomain(udomain_t* _d, str *_aor); * \param _r new created record * \return return 0 on success, -1 on failure */ -int insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r); +int insert_urecord(udomain_t *_d, str *_aor, struct urecord **_r); /*! @@ -166,7 +165,7 @@ int insert_urecord(udomain_t* _d, str* _aor, struct urecord** _r); * \param _r new created record * \return 0 if a record was found, 1 if nothing could be found */ -int get_urecord(udomain_t* _d, str* _aor, struct urecord** _r); +int get_urecord(udomain_t *_d, str *_aor, struct urecord **_r); /*! @@ -178,8 +177,8 @@ int get_urecord(udomain_t* _d, str* _aor, struct urecord** _r); * \param _c store pointer to contact structure * \return 0 if a record was found, 1 if nothing could be found */ -int get_urecord_by_ruid(udomain_t* _d, unsigned int _aorhash, - str *_ruid, struct urecord** _r, struct ucontact** _c); +int get_urecord_by_ruid(udomain_t *_d, unsigned int _aorhash, str *_ruid, + struct urecord **_r, struct ucontact **_c); /*! * \brief Delete a urecord from domain @@ -188,7 +187,7 @@ int get_urecord_by_ruid(udomain_t* _d, unsigned int _aorhash, * \param _r deleted record * \return 0 on success, -1 if the record could not be deleted */ -int delete_urecord(udomain_t* _d, str* _aor, struct urecord* _r); +int delete_urecord(udomain_t *_d, str *_aor, struct urecord *_r); #endif diff --git a/src/modules/p_usrloc/ul_callback.c b/src/modules/p_usrloc/ul_callback.c index 54d566d8cfd..2b66cbdbbc4 100644 --- a/src/modules/p_usrloc/ul_callback.c +++ b/src/modules/p_usrloc/ul_callback.c @@ -35,15 +35,14 @@ #include "../usrloc/ul_callback.h" #include "udomain.h" -struct ulcb_head_list* ulcb_list = 0; - +struct ulcb_head_list *ulcb_list = 0; int init_ulcb_list(void) { - ulcb_list = (struct ulcb_head_list*)shm_malloc - ( sizeof(struct ulcb_head_list) ); - if (ulcb_list==0) { + ulcb_list = + (struct ulcb_head_list *)shm_malloc(sizeof(struct ulcb_head_list)); + if(ulcb_list == 0) { LM_CRIT("no more shared mem\n"); return -1; } @@ -57,41 +56,41 @@ void destroy_ulcb_list(void) { struct ul_callback *cbp, *cbp_tmp; - if (!ulcb_list) + if(!ulcb_list) return; - for( cbp=ulcb_list->first; cbp ; ) { + for(cbp = ulcb_list->first; cbp;) { cbp_tmp = cbp; cbp = cbp->next; - if (cbp_tmp->param) shm_free( cbp_tmp->param ); - shm_free( cbp_tmp ); + if(cbp_tmp->param) + shm_free(cbp_tmp->param); + shm_free(cbp_tmp); } shm_free(ulcb_list); } - /*! \brief register a callback function 'f' for 'types' mask of events; */ -int register_ulcb( int types, ul_cb f, void *param ) +int register_ulcb(int types, ul_cb f, void *param) { struct ul_callback *cbp; /* are the callback types valid?... */ - if ( types<0 || types>ULCB_MAX ) { - LM_CRIT("invalid callback types: mask=%d\n",types); + if(types < 0 || types > ULCB_MAX) { + LM_CRIT("invalid callback types: mask=%d\n", types); return E_BUG; } /* we don't register null functions */ - if (f==0) { + if(f == 0) { LM_CRIT("null callback function\n"); return E_BUG; } /* build a new callback structure */ - if (!(cbp=(struct ul_callback*)shm_malloc(sizeof( struct ul_callback)))) { + if(!(cbp = (struct ul_callback *)shm_malloc(sizeof(struct ul_callback)))) { LM_ERR("no more share mem\n"); return E_OUT_OF_MEM; } @@ -104,13 +103,10 @@ int register_ulcb( int types, ul_cb f, void *param ) cbp->callback = f; cbp->param = param; cbp->types = types; - if (cbp->next) - cbp->id = cbp->next->id+1; + if(cbp->next) + cbp->id = cbp->next->id + 1; else cbp->id = 0; return 1; } - - - diff --git a/src/modules/p_usrloc/ul_check.c b/src/modules/p_usrloc/ul_check.c index 0ed32dc99f7..aef59ed0f20 100644 --- a/src/modules/p_usrloc/ul_check.c +++ b/src/modules/p_usrloc/ul_check.c @@ -26,22 +26,25 @@ #include "ul_check.h" #include "time.h" -static struct check_list_head * head = NULL; +static struct check_list_head *head = NULL; -static struct check_list_element * initialise_element(void); +static struct check_list_element *initialise_element(void); -static void destroy_element(struct check_list_element * element); +static void destroy_element(struct check_list_element *element); -int init_list(void) { - if(!head){ - if((head = (struct check_list_head *)shm_malloc(sizeof(struct check_list_head))) == NULL){ +int init_list(void) +{ + if(!head) { + if((head = (struct check_list_head *)shm_malloc( + sizeof(struct check_list_head))) + == NULL) { LM_ERR("couldn't allocate shared memory.\n"); return -1; } } memset(head, 0, sizeof(struct check_list_head)); - - if(lock_init(&head->list_lock) == 0){ + + if(lock_init(&head->list_lock) == 0) { LM_ERR("cannot initialise lock.\n"); shm_free(head); return -1; @@ -49,21 +52,22 @@ int init_list(void) { return 0; } -struct check_data * get_new_element(void) { - struct check_list_element * ret; - if(!head){ +struct check_data *get_new_element(void) +{ + struct check_list_element *ret; + if(!head) { LM_ERR("list not initialised.\n"); return NULL; } LM_DBG("start.\n"); lock_get(&head->list_lock); - - if((ret = initialise_element()) == NULL){ + + if((ret = initialise_element()) == NULL) { lock_release(&head->list_lock); return NULL; } head->element_count++; - if(head->first == NULL){ + if(head->first == NULL) { LM_DBG("new element is the first.\n"); LM_DBG("element_count: %i\n", head->element_count); head->first = ret; @@ -78,7 +82,8 @@ struct check_data * get_new_element(void) { return ret->data; } -int must_refresh(struct check_data * element) { +int must_refresh(struct check_data *element) +{ int ret; lock_get(&element->flag_lock); ret = element->refresh_flag; @@ -88,7 +93,8 @@ int must_refresh(struct check_data * element) { return ret; } -int must_reconnect(struct check_data * element) { +int must_reconnect(struct check_data *element) +{ int ret; lock_get(&element->flag_lock); ret = element->reconnect_flag; @@ -98,12 +104,13 @@ int must_reconnect(struct check_data * element) { return ret; } -int set_must_refresh(void) { - struct check_list_element * tmp; +int set_must_refresh(void) +{ + struct check_list_element *tmp; int i = 0; lock_get(&head->list_lock); tmp = head->first; - while(tmp){ + while(tmp) { lock_get(&tmp->data->flag_lock); tmp->data->refresh_flag = 1; lock_release(&tmp->data->flag_lock); @@ -115,12 +122,13 @@ int set_must_refresh(void) { return i; } -int set_must_reconnect(void) { - struct check_list_element * tmp; +int set_must_reconnect(void) +{ + struct check_list_element *tmp; int i = 0; lock_get(&head->list_lock); tmp = head->first; - while(tmp){ + while(tmp) { lock_get(&tmp->data->flag_lock); tmp->data->reconnect_flag = 1; lock_release(&tmp->data->flag_lock); @@ -133,25 +141,27 @@ int set_must_reconnect(void) { } -int must_retry(time_t *timer, time_t interval){ - if(!timer){ +int must_retry(time_t *timer, time_t interval) +{ + if(!timer) { return -1; } - LM_DBG("must_retry: time is at %" PRIu64 ", retry at %" PRIu64 ".\n", (uint64_t)time(NULL), - (uint64_t)(*timer)); - if(*timer <= time(NULL)){ + LM_DBG("must_retry: time is at %" PRIu64 ", retry at %" PRIu64 ".\n", + (uint64_t)time(NULL), (uint64_t)(*timer)); + if(*timer <= time(NULL)) { *timer = time(NULL) + interval; return 1; } return 0; } -void destroy_list(void) { - struct check_list_element * tmp; - struct check_list_element * del; - if(head){ +void destroy_list(void) +{ + struct check_list_element *tmp; + struct check_list_element *del; + if(head) { tmp = head->first; - while(tmp){ + while(tmp) { del = tmp; tmp = tmp->next; destroy_element(del); @@ -162,22 +172,26 @@ void destroy_list(void) { return; } -static struct check_list_element * initialise_element(void){ - struct check_list_element * ret; - if((ret = (struct check_list_element *)shm_malloc(sizeof(struct check_list_element))) == NULL){ +static struct check_list_element *initialise_element(void) +{ + struct check_list_element *ret; + if((ret = (struct check_list_element *)shm_malloc( + sizeof(struct check_list_element))) + == NULL) { LM_ERR("couldn't allocate shared memory.\n"); return NULL; } memset(ret, 0, sizeof(struct check_list_element)); - - if((ret->data = (struct check_data *)shm_malloc(sizeof(struct check_data))) == NULL){ + + if((ret->data = (struct check_data *)shm_malloc(sizeof(struct check_data))) + == NULL) { LM_ERR("couldn't allocate shared memory.\n"); shm_free(ret); return NULL; } memset(ret->data, 0, sizeof(struct check_data)); - - if(lock_init(&ret->data->flag_lock) == 0){ + + if(lock_init(&ret->data->flag_lock) == 0) { LM_ERR("cannot initialise flag lock.\n"); shm_free(ret->data); shm_free(ret); @@ -186,12 +200,13 @@ static struct check_list_element * initialise_element(void){ return ret; } -static void destroy_element(struct check_list_element * element){ - if(element){ - if(element->data){ -/* if(element->data->flag_lock){ */ - lock_destroy(&element->data->flag_lock); -/* }*/ +static void destroy_element(struct check_list_element *element) +{ + if(element) { + if(element->data) { + /* if(element->data->flag_lock){ */ + lock_destroy(&element->data->flag_lock); + /* }*/ shm_free(element->data); } shm_free(element); diff --git a/src/modules/p_usrloc/ul_check.h b/src/modules/p_usrloc/ul_check.h index 479812c5f69..22b11cc6548 100644 --- a/src/modules/p_usrloc/ul_check.h +++ b/src/modules/p_usrloc/ul_check.h @@ -26,30 +26,33 @@ #include "../../core/lock_ops.h" -struct check_data { +struct check_data +{ int refresh_flag; int reconnect_flag; gen_lock_t flag_lock; }; -struct check_list_element{ - struct check_data * data; - struct check_list_element * next; +struct check_list_element +{ + struct check_data *data; + struct check_list_element *next; }; -struct check_list_head{ +struct check_list_head +{ gen_lock_t list_lock; int element_count; - struct check_list_element * first; + struct check_list_element *first; }; int init_list(void); -struct check_data * get_new_element(void); +struct check_data *get_new_element(void); -int must_refresh(struct check_data * element); +int must_refresh(struct check_data *element); -int must_reconnect(struct check_data * element); +int must_reconnect(struct check_data *element); int set_must_refresh(void); @@ -57,7 +60,7 @@ int set_must_reconnect(void); void destroy_list(void); -int must_retry(time_t * timer, time_t interval); +int must_retry(time_t *timer, time_t interval); #endif diff --git a/src/modules/p_usrloc/ul_db.c b/src/modules/p_usrloc/ul_db.c index b09690c1619..5f8a6a7196e 100644 --- a/src/modules/p_usrloc/ul_db.c +++ b/src/modules/p_usrloc/ul_db.c @@ -37,30 +37,33 @@ ul_db_handle_t dbh_tmp; ul_master_db_set_t mdb; -int required_caps = DB_CAP_QUERY | DB_CAP_RAW_QUERY | DB_CAP_INSERT | DB_CAP_DELETE | DB_CAP_UPDATE | DB_CAP_INSERT_UPDATE; +int required_caps = DB_CAP_QUERY | DB_CAP_RAW_QUERY | DB_CAP_INSERT + | DB_CAP_DELETE | DB_CAP_UPDATE | DB_CAP_INSERT_UPDATE; static char query[UL_DB_QUERY_LEN]; -typedef struct db_dbf_dbres { - db1_res_t * res; - db_func_t * dbf; +typedef struct db_dbf_dbres +{ + db1_res_t *res; + db_func_t *dbf; } db_dbf_dbres_t; #define UL_DB_RES_LIMIT 20 db_dbf_dbres_t results[UL_DB_RES_LIMIT]; -static int add_dbf(db1_res_t * res, db_func_t * dbf); +static int add_dbf(db1_res_t *res, db_func_t *dbf); -static db_func_t * get_and_remove_dbf(db1_res_t * res); +static db_func_t *get_and_remove_dbf(db1_res_t *res); -int ul_db_init(void) { +int ul_db_init(void) +{ mdb.read.url = &read_db_url; mdb.write.url = &write_db_url; - + memset(results, 0, sizeof(results)); - if(db_master_write){ + if(db_master_write) { if(db_bind_mod(mdb.write.url, &mdb.write.dbf) < 0) { LM_ERR("could not bind api for write db.\n"); return -1; @@ -71,7 +74,7 @@ int ul_db_init(void) { } LM_INFO("write db initialized\n"); } - + if(db_bind_mod(mdb.read.url, &mdb.read.dbf) < 0) { LM_ERR("could not bind db api for read db.\n"); return -1; @@ -84,31 +87,32 @@ int ul_db_init(void) { return 0; } -int ul_db_child_init(void) { - if(mdb.read.dbh){ +int ul_db_child_init(void) +{ + if(mdb.read.dbh) { mdb.read.dbf.close(mdb.read.dbh); mdb.read.dbh = NULL; } - if(mdb.write.dbh){ + if(mdb.write.dbh) { mdb.write.dbf.close(mdb.write.dbh); mdb.write.dbh = NULL; } - if((mdb.read.dbh = mdb.read.dbf.init(mdb.read.url)) == NULL) { + if((mdb.read.dbh = mdb.read.dbf.init(mdb.read.url)) == NULL) { LM_ERR("could not connect to sip master db (read).\n"); return -1; } LM_INFO("read db connection for children initialized\n"); - - if(ul_db_child_locnr_init() == -1) return -1; - + + if(ul_db_child_locnr_init() == -1) + return -1; + LM_INFO("location number is %d\n", max_loc_nr); - if(db_master_write){ - if((mdb.write.dbh = mdb.write.dbf.init(mdb.write.url)) == NULL) { - if (mdb_availability_control) { + if(db_master_write) { + if((mdb.write.dbh = mdb.write.dbf.init(mdb.write.url)) == NULL) { + if(mdb_availability_control) { LM_INFO("starting with no connection to sip master db write\n"); return 0; - } - else { + } else { LM_ERR("could not connect to sip master db (write).\n"); return -1; } @@ -118,36 +122,39 @@ int ul_db_child_init(void) { return 0; } -int ul_db_child_locnr_init(void) { - if(!mdb.read.dbh){ +int ul_db_child_locnr_init(void) +{ + if(!mdb.read.dbh) { LM_ERR("Sip master DB connection(read) is down\n"); return -1; } - if(load_location_number(&mdb.read.dbf, mdb.read.dbh, &max_loc_nr) != 0){ + if(load_location_number(&mdb.read.dbf, mdb.read.dbh, &max_loc_nr) != 0) { LM_ERR("could not load location number\n"); return -1; } return 0; } -void ul_db_shutdown(void) { +void ul_db_shutdown(void) +{ destroy_handles(); - if(mdb.read.dbh){ + if(mdb.read.dbh) { mdb.read.dbf.close(mdb.read.dbh); } - if(mdb.write.dbh){ + if(mdb.write.dbh) { mdb.write.dbf.close(mdb.write.dbh); } return; } -int init_w_dbh(ul_master_db_t *write) { - if (mdb_availability_control) { - if (!(*mdb_w_available)) { +int init_w_dbh(ul_master_db_t *write) +{ + if(mdb_availability_control) { + if(!(*mdb_w_available)) { return -1; } - if (write->dbh == NULL) { - if((write->dbh = write->dbf.init(write->url)) == NULL) { + if(write->dbh == NULL) { + if((write->dbh = write->dbf.init(write->url)) == NULL) { LM_ERR("Could not recreate connection to master write db.\n"); return -1; } @@ -157,43 +164,44 @@ int init_w_dbh(ul_master_db_t *write) { return 0; } -int db_handle_error(ul_db_handle_t * handle, int no) { +int db_handle_error(ul_db_handle_t *handle, int no) +{ int query_len; - ul_db_t * db; + ul_db_t *db; int i; str tmp; - - if(!handle){ + + if(!handle) { LM_ERR("NULL pointer in parameter.\n"); return -1; } - if (!db_master_write) { + if(!db_master_write) { return 0; } - query_len = 35 + reg_table.len - + error_col.len * 2 + id_col.len; - - if(query_len > UL_DB_QUERY_LEN){ + query_len = 35 + reg_table.len + error_col.len * 2 + id_col.len; + + if(query_len > UL_DB_QUERY_LEN) { LM_ERR("query too long\n"); return -1; } - - if((db = get_db_by_num(handle, no)) == NULL){ + + if((db = get_db_by_num(handle, no)) == NULL) { LM_ERR("can't get db.\n"); return -1; } - if (db->errors < cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)) { - if (sprintf(query, "UPDATE %.*s " - "SET %.*s=%.*s+1 " - "WHERE %.*s=%i " - "AND %.*s=%i", - reg_table.len, reg_table.s, - error_col.len, error_col.s, error_col.len, error_col.s, - id_col.len, id_col.s, handle->id, - num_col.len, num_col.s, db->no) < 0) { + if(db->errors < cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)) { + if(sprintf(query, + "UPDATE %.*s " + "SET %.*s=%.*s+1 " + "WHERE %.*s=%i " + "AND %.*s=%i", + reg_table.len, reg_table.s, error_col.len, error_col.s, + error_col.len, error_col.s, id_col.len, id_col.s, handle->id, + num_col.len, num_col.s, db->no) + < 0) { LM_ERR("could not print the query\n"); return -1; } @@ -201,39 +209,40 @@ int db_handle_error(ul_db_handle_t * handle, int no) { tmp.s = query; tmp.len = strlen(query); - if (init_w_dbh(&mdb.write) < 0) + if(init_w_dbh(&mdb.write) < 0) return -1; - if (mdb.write.dbf.raw_query(mdb.write.dbh, &tmp, NULL)) { + if(mdb.write.dbf.raw_query(mdb.write.dbh, &tmp, NULL)) { LM_ERR("error in database update.\n"); return -1; } } - for(i=0; idb[i].dbh && handle->db[i].dbf.close){ + for(i = 0; i < DB_NUM; i++) { + if(handle->db[i].dbh && handle->db[i].dbf.close) { handle->db[i].dbf.close(handle->db[i].dbh); handle->db[i].dbh = NULL; } } - if(load_data(&mdb.read.dbf, mdb.read.dbh, &dbh_tmp, handle->id) < 0){ + if(load_data(&mdb.read.dbf, mdb.read.dbh, &dbh_tmp, handle->id) < 0) { LM_ERR("could not load id %i\n", handle->id); return -1; } refresh_handle(handle, &dbh_tmp, 0); LM_ERR("error on id %i, db %i, " - "errors occurred: %i, threshold: %i\n", - handle->id, db->no, db->errors, cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)); + "errors occurred: %i, threshold: %i\n", + handle->id, db->no, db->errors, + cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)); if(db->errors >= cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)) { LM_DBG("db_handle_error: now doing failover\n"); - if (init_w_dbh(&mdb.write) < 0) + if(init_w_dbh(&mdb.write) < 0) return -1; if((db_failover(&mdb.write.dbf, mdb.write.dbh, handle, no)) < 0) { LM_ERR("error in doing failover.\n"); return -1; } - if(load_data(&mdb.read.dbf, mdb.read.dbh, &dbh_tmp, handle->id) < 0){ + if(load_data(&mdb.read.dbf, mdb.read.dbh, &dbh_tmp, handle->id) < 0) { return -1; } refresh_handle(handle, &dbh_tmp, 0); @@ -242,204 +251,235 @@ int db_handle_error(ul_db_handle_t * handle, int no) { return 0; } -int db_check_policy(int pol, int ok, int working) { +int db_check_policy(int pol, int ok, int working) +{ #define DB_POL_N_1 0 #define DB_POL_N_HALF 1 #define DB_POL_N_ALL 2 switch(policy) { - case DB_POL_N_1: + case DB_POL_N_1: switch(pol) { - case DB_POL_OP: if(ok >= (DB_NUM - 1)) { + case DB_POL_OP: + if(ok >= (DB_NUM - 1)) { return 0; } else { return -1; } break; - case DB_POL_QUERY: if(ok >= 1) { + case DB_POL_QUERY: + if(ok >= 1) { return 0; } else { return -1; } - case DB_POL_MOD: if((ok == working) && (working >= (DB_NUM - 1))) { + case DB_POL_MOD: + if((ok == working) && (working >= (DB_NUM - 1))) { return 0; } else { return -1; } - default: LM_ERR("wrong mode given.\n"); - return -1; - } - case DB_POL_N_HALF: + default: + LM_ERR("wrong mode given.\n"); + return -1; + } + case DB_POL_N_HALF: switch(pol) { - case DB_POL_OP: if(ok >= (DB_NUM / 2)) { + case DB_POL_OP: + if(ok >= (DB_NUM / 2)) { return 0; } else { return -1; } break; - case DB_POL_QUERY: if(ok >= 1) { + case DB_POL_QUERY: + if(ok >= 1) { return 0; } else { return -1; } - case DB_POL_MOD: if((ok == working) && (working >= (DB_NUM / 2))) { + case DB_POL_MOD: + if((ok == working) && (working >= (DB_NUM / 2))) { return 0; } else { return -1; } - default: LM_ERR("wrong mode given.\n"); - return -1; - } + default: + LM_ERR("wrong mode given.\n"); + return -1; + } - case DB_POL_N_ALL: + case DB_POL_N_ALL: switch(pol) { - case DB_POL_OP: if(ok == DB_NUM) { + case DB_POL_OP: + if(ok == DB_NUM) { return 0; } else { return -1; } break; - case DB_POL_QUERY: if(ok >= 1) { + case DB_POL_QUERY: + if(ok >= 1) { return 0; } else { return -1; } - case DB_POL_MOD: if(ok == DB_NUM) { + case DB_POL_MOD: + if(ok == DB_NUM) { return 0; } else { return -1; } - default: LM_ERR("wrong mode given.\n"); + default: + LM_ERR("wrong mode given.\n"); return -1; } - default: - return -1; + default: + return -1; } } -int ul_db_insert(str * table, str * first, str * second, - db_key_t* _k, db_val_t* _v, int _n) { - ul_db_handle_t * handle; - if(!db_write){ +int ul_db_insert( + str *table, str *first, str *second, db_key_t *_k, db_val_t *_v, int _n) +{ + ul_db_handle_t *handle; + if(!db_write) { LM_ERR("not allowed in read only mode, abort.\n"); return -1; } - if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) == NULL) { + if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) + == NULL) { LM_ERR("could not retrieve db handle.\n"); return -1; } return db_insert(handle, table, _k, _v, _n); } -int ul_db_replace(str * table, str * first, str * second, - db_key_t* _k, db_val_t* _v, int _n, int _un) { - ul_db_handle_t * handle; - if(!db_write){ +int ul_db_replace(str *table, str *first, str *second, db_key_t *_k, + db_val_t *_v, int _n, int _un) +{ + ul_db_handle_t *handle; + if(!db_write) { LM_ERR("not allowed in read only mode, abort.\n"); return -1; } - if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) == NULL) { + if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) + == NULL) { LM_ERR("could not retrieve db handle.\n"); return -1; } return db_replace(handle, table, _k, _v, _n, _un); } -int ul_db_update(str * table, str * first, str * second, - db_key_t* _k, db_op_t * _op, db_val_t* _v, - db_key_t* _uk, db_val_t* _uv, int _n, int _un) { - ul_db_handle_t * handle; - if(!db_write){ +int ul_db_update(str *table, str *first, str *second, db_key_t *_k, + db_op_t *_op, db_val_t *_v, db_key_t *_uk, db_val_t *_uv, int _n, + int _un) +{ + ul_db_handle_t *handle; + if(!db_write) { LM_ERR("not allowed in read only mode, abort.\n"); return -1; } - if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) == NULL) { + if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) + == NULL) { LM_ERR("could not retrieve db handle.\n"); return -1; } return db_update(handle, table, _k, _op, _v, _uk, _uv, _n, _un); } -int ul_db_insert_update(str * table, str * first, str * second, - db_key_t* _k, db_val_t* _v, int _n) { - ul_db_handle_t * handle; - if(!db_write){ +int ul_db_insert_update( + str *table, str *first, str *second, db_key_t *_k, db_val_t *_v, int _n) +{ + ul_db_handle_t *handle; + if(!db_write) { LM_ERR("not allowed in read only mode, abort.\n"); return -1; } - if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) == NULL) { + if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) + == NULL) { LM_ERR("could not retrieve db handle.\n"); return -1; } return db_insert_update(handle, table, _k, _v, _n); } -int ul_db_delete(str * table, str * first, str * second, - db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { - ul_db_handle_t * handle; - if(!db_write){ +int ul_db_delete(str *table, str *first, str *second, db_key_t *_k, db_op_t *_o, + db_val_t *_v, int _n) +{ + ul_db_handle_t *handle; + if(!db_write) { LM_ERR("not allowed in read only mode, abort.\n"); return -1; } - if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) == NULL) { + if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) + == NULL) { LM_ERR("could not retrieve db handle.\n"); return -1; } return db_delete(handle, table, _k, _o, _v, _n); } -int ul_db_query(str * table, str * first, str * second, db1_con_t *** _r_h, - db_key_t* _k, db_op_t* _op, db_val_t* _v, - db_key_t* _c, int _n, int _nc, db_key_t _o, db1_res_t** _r) { - ul_db_handle_t * handle; - db_func_t * f; +int ul_db_query(str *table, str *first, str *second, db1_con_t ***_r_h, + db_key_t *_k, db_op_t *_op, db_val_t *_v, db_key_t *_c, int _n, int _nc, + db_key_t _o, db1_res_t **_r) +{ + ul_db_handle_t *handle; + db_func_t *f; int ret; - if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) == NULL) { + if((handle = get_handle(&mdb.read.dbf, mdb.read.dbh, first, second)) + == NULL) { LM_ERR("could not retrieve db handle.\n"); return -1; } - if((ret = db_query(handle, _r_h, &f, table, _k, _op, _v, _c, _n, _nc, _o, _r, db_master_write)) < 0){ + if((ret = db_query(handle, _r_h, &f, table, _k, _op, _v, _c, _n, _nc, _o, + _r, db_master_write)) + < 0) { return ret; } add_dbf(*_r, f); return ret; } -int ul_db_free_result(db1_con_t ** dbh, db1_res_t * res){ - db_func_t * f; - if(!dbh){ +int ul_db_free_result(db1_con_t **dbh, db1_res_t *res) +{ + db_func_t *f; + if(!dbh) { LM_ERR("NULL pointer in parameter.\n"); return -1; } - if((f = get_and_remove_dbf(res)) == NULL){ + if((f = get_and_remove_dbf(res)) == NULL) { return -1; } return f->free_result(*dbh, res); } -int db_reactivate(ul_db_handle_t * handle, int no){ - if(!db_master_write){ +int db_reactivate(ul_db_handle_t *handle, int no) +{ + if(!db_master_write) { LM_ERR("running in read only mode, abort.\n"); return -1; } - if (init_w_dbh(&mdb.write) < 0) + if(init_w_dbh(&mdb.write) < 0) return -1; return db_failover_reactivate(&mdb.write.dbf, mdb.write.dbh, handle, no); } -int db_reset_failover_time(ul_db_handle_t * handle, int no){ - if(!db_master_write){ +int db_reset_failover_time(ul_db_handle_t *handle, int no) +{ + if(!db_master_write) { LM_ERR("running in read only mode, abort.\n"); return -1; } - if (init_w_dbh(&mdb.write) < 0) + if(init_w_dbh(&mdb.write) < 0) return -1; return db_failover_reset(&mdb.write.dbf, mdb.write.dbh, handle->id, no); } -int ul_db_check(ul_db_handle_t * handle){ - if(db_master_write){ - if (init_w_dbh(&mdb.write) < 0) +int ul_db_check(ul_db_handle_t *handle) +{ + if(db_master_write) { + if(init_w_dbh(&mdb.write) < 0) return -1; return check_handle(&mdb.write.dbf, mdb.write.dbh, handle); } else { @@ -448,10 +488,11 @@ int ul_db_check(ul_db_handle_t * handle){ } } -static int add_dbf(db1_res_t * res, db_func_t * dbf){ - int i=0; - for(i=0;iinsert = (ul_db_insert_t) ul_db_insert; - if(api->insert == 0){ + + api->insert = (ul_db_insert_t)ul_db_insert; + if(api->insert == 0) { LM_ERR("can't bind ul_db_insert\n"); return -1; } - api->update = (ul_db_update_t) ul_db_update; - if(api->update == 0){ + api->update = (ul_db_update_t)ul_db_update; + if(api->update == 0) { LM_ERR("can't bind ul_db_update\n"); return -1; } - - api->replace = (ul_db_replace_t) ul_db_replace; - if(api->replace == 0){ + + api->replace = (ul_db_replace_t)ul_db_replace; + if(api->replace == 0) { LM_ERR("can't bind ul_db_replace\n"); return -1; } - - api->delete = (ul_db_delete_t) ul_db_delete; - if(api->delete == 0){ + + api->delete = (ul_db_delete_t)ul_db_delete; + if(api->delete == 0) { LM_ERR("can't bind ul_db_delete\n"); return -1; } - - api->query = (ul_db_query_t) ul_db_query; - if(api->query == 0){ + + api->query = (ul_db_query_t)ul_db_query; + if(api->query == 0) { LM_ERR("can't bind ul_db_query\n"); return -1; } - - api->free_result = (ul_db_free_result_t) ul_db_free_result; - if(api->free_result == 0){ + + api->free_result = (ul_db_free_result_t)ul_db_free_result; + if(api->free_result == 0) { LM_ERR("can't bind ul_db_free_result\n"); return -1; } diff --git a/src/modules/p_usrloc/ul_db_api.h b/src/modules/p_usrloc/ul_db_api.h index dc7a2089172..14591e8fa4e 100644 --- a/src/modules/p_usrloc/ul_db_api.h +++ b/src/modules/p_usrloc/ul_db_api.h @@ -25,41 +25,42 @@ #include "ul_db.h" #include "../../core/sr_module.h" -typedef int (* ul_db_insert_t) (str * table, str * first, str * second, - db_key_t* _k, db_val_t* _v, int _n); +typedef int (*ul_db_insert_t)(str *table, str *first, str *second, db_key_t *_k, + db_val_t *_v, int _n); -typedef int (* ul_db_update_t) (str * table, str * first, str * second, - db_key_t* _k, db_op_t * _op, db_val_t* _v, db_key_t* _uk, - db_val_t* _uv, int _n, int _un); +typedef int (*ul_db_update_t)(str *table, str *first, str *second, db_key_t *_k, + db_op_t *_op, db_val_t *_v, db_key_t *_uk, db_val_t *_uv, int _n, + int _un); -typedef int (* ul_db_insert_update_t) (str * table, str * first, str * second, - db_key_t* _k, db_val_t* _v, int _n); +typedef int (*ul_db_insert_update_t)(str *table, str *first, str *second, + db_key_t *_k, db_val_t *_v, int _n); -typedef int (* ul_db_replace_t) (str * table, str * first, str * second, - db_key_t* _k, db_val_t* _v, int _n, int _un); +typedef int (*ul_db_replace_t)(str *table, str *first, str *second, + db_key_t *_k, db_val_t *_v, int _n, int _un); -typedef int (* ul_db_delete_t) (str * table, str * first, str * second, - db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n); +typedef int (*ul_db_delete_t)(str *table, str *first, str *second, db_key_t *_k, + db_op_t *_o, db_val_t *_v, int _n); -typedef int (* ul_db_query_t) (str * table, str * first, str * second, db1_con_t *** _r_h, - db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _c, - int _n, int _nc, db_key_t _o, db1_res_t** _r); +typedef int (*ul_db_query_t)(str *table, str *first, str *second, + db1_con_t ***_r_h, db_key_t *_k, db_op_t *_op, db_val_t *_v, + db_key_t *_c, int _n, int _nc, db_key_t _o, db1_res_t **_r); -typedef int (* ul_db_free_result_t)(db1_con_t ** dbh, db1_res_t * res); +typedef int (*ul_db_free_result_t)(db1_con_t **dbh, db1_res_t *res); -typedef struct ul_db_api{ - ul_db_update_t update; - ul_db_insert_t insert; - ul_db_insert_update_t insert_update; - ul_db_replace_t replace; - ul_db_delete_t delete; - ul_db_query_t query; - ul_db_free_result_t free_result; -}ul_db_api_t; +typedef struct ul_db_api +{ + ul_db_update_t update; + ul_db_insert_t insert; + ul_db_insert_update_t insert_update; + ul_db_replace_t replace; + ul_db_delete_t delete; + ul_db_query_t query; + ul_db_free_result_t free_result; +} ul_db_api_t; -typedef int (*bind_ul_db_t)(ul_db_api_t * api); +typedef int (*bind_ul_db_t)(ul_db_api_t *api); -int bind_ul_db(ul_db_api_t* api); +int bind_ul_db(ul_db_api_t *api); #endif diff --git a/src/modules/p_usrloc/ul_db_del.c b/src/modules/p_usrloc/ul_db_del.c index 36900d354a5..f152c45956b 100644 --- a/src/modules/p_usrloc/ul_db_del.c +++ b/src/modules/p_usrloc/ul_db_del.c @@ -23,7 +23,9 @@ #include "ul_db_del.h" #include "ul_db_form_query.h" -int db_delete(ul_db_handle_t * handle, str * table, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { - return db_submit_query(UL_DB_DEL, handle, table, _k, _o, _v, NULL, NULL, _n, 0); +int db_delete(ul_db_handle_t *handle, str *table, db_key_t *_k, db_op_t *_o, + db_val_t *_v, int _n) +{ + return db_submit_query( + UL_DB_DEL, handle, table, _k, _o, _v, NULL, NULL, _n, 0); } - diff --git a/src/modules/p_usrloc/ul_db_del.h b/src/modules/p_usrloc/ul_db_del.h index d9b94df454e..a5772de86f6 100644 --- a/src/modules/p_usrloc/ul_db_del.h +++ b/src/modules/p_usrloc/ul_db_del.h @@ -25,6 +25,7 @@ #include "../../lib/srdb1/db.h" #include "ul_db_handle.h" -int db_delete(ul_db_handle_t * handle, str * table, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n); +int db_delete(ul_db_handle_t *handle, str *table, db_key_t *_k, db_op_t *_o, + db_val_t *_v, int _n); #endif diff --git a/src/modules/p_usrloc/ul_db_failover.c b/src/modules/p_usrloc/ul_db_failover.c index d2e6f18dac3..ed4ba12b914 100644 --- a/src/modules/p_usrloc/ul_db_failover.c +++ b/src/modules/p_usrloc/ul_db_failover.c @@ -32,71 +32,82 @@ static ul_db_t tmp; static char query[UL_DB_QUERY_LEN]; -static int ul_db_failover_get_spare(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db); +static int ul_db_failover_get_spare( + db_func_t *dbf, db1_con_t *dbh, ul_db_t *db); -static int ul_db_failover_switch(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no); +static int ul_db_failover_switch( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no); -static int ul_db_failover_normal(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no); +static int ul_db_failover_normal( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no); -int db_failover(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no) { - if(cfg_get(p_usrloc, p_usrloc_cfg, failover_level) & FAILOVER_MODE_NORMAL){ - if(ul_db_failover_normal(dbf, dbh, handle, no) < 0){ +int db_failover(db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no) +{ + if(cfg_get(p_usrloc, p_usrloc_cfg, failover_level) & FAILOVER_MODE_NORMAL) { + if(ul_db_failover_normal(dbf, dbh, handle, no) < 0) { LM_ERR("could not switch to spare, try to " - "turn off broken db id %i, db %i.\n", + "turn off broken db id %i, db %i.\n", handle->id, no); } else { return 0; } } - if(cfg_get(p_usrloc, p_usrloc_cfg, failover_level) & (FAILOVER_MODE_NONE | FAILOVER_MODE_NORMAL)){ - if(db_failover_deactivate(dbf, dbh, handle, no) < 0){ + if(cfg_get(p_usrloc, p_usrloc_cfg, failover_level) + & (FAILOVER_MODE_NONE | FAILOVER_MODE_NORMAL)) { + if(db_failover_deactivate(dbf, dbh, handle, no) < 0) { LM_ERR("could not deactivate " - "id %i, db %i.\n", - handle->id, no); + "id %i, db %i.\n", + handle->id, no); return -1; } } return 0; } -static int ul_db_failover_normal(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no){ - ul_db_t * db = NULL; - if(ul_db_failover_prepare(dbf, dbh) < 0){ +static int ul_db_failover_normal( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no) +{ + ul_db_t *db = NULL; + if(ul_db_failover_prepare(dbf, dbh) < 0) { LM_ERR("could not " - "initiate failover transaction, rollback.\n"); + "initiate failover transaction, rollback.\n"); ul_db_failover_rollback(dbf, dbh); return -1; } - if((db = get_db_by_num(handle, no)) == NULL){ + if((db = get_db_by_num(handle, no)) == NULL) { LM_ERR("could not find id %i, " - "db %i.\n", handle->id, no); + "db %i.\n", + handle->id, no); ul_db_failover_rollback(dbf, dbh); return -1; } - if(ul_db_failover_get_spare(dbf, dbh, db) < 0){ + if(ul_db_failover_get_spare(dbf, dbh, db) < 0) { LM_ERR("no spare found. " - "id %i, db %i.\n", handle->id, no); + "id %i, db %i.\n", + handle->id, no); ul_db_failover_rollback(dbf, dbh); return -1; } - if(ul_db_failover_switch(dbf, dbh, handle, no) < 0){ + if(ul_db_failover_switch(dbf, dbh, handle, no) < 0) { LM_ERR("switch to spare on " - "id %i, db %i.\n", handle->id, no); + "id %i, db %i.\n", + handle->id, no); ul_db_failover_rollback(dbf, dbh); return -1; } - if(ul_db_failover_commit(dbf, dbh) < 0){ + if(ul_db_failover_commit(dbf, dbh) < 0) { LM_ERR("could not " - "commit failover transaction, rollback.\n"); + "commit failover transaction, rollback.\n"); ul_db_failover_rollback(dbf, dbh); return -1; } return 0; } -static int ul_db_failover_get_spare(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db) { - db1_res_t * res = NULL; - db_row_t * row; +static int ul_db_failover_get_spare(db_func_t *dbf, db1_con_t *dbh, ul_db_t *db) +{ + db1_res_t *res = NULL; + db_row_t *row; int query_len; str tmp; @@ -107,42 +118,34 @@ static int ul_db_failover_get_spare(db_func_t * dbf, db1_con_t * dbh, ul_db_t * memset(&spare, 0, sizeof(ul_db_handle_t)); memset(query, 0, UL_DB_QUERY_LEN); - - query_len = 100 - + id_col.len - + num_col.len - + url_col.len - + 2 * risk_group_col.len - + reg_table.len - + spare_col.len - + status_col.len; - - if(query_len >= UL_DB_QUERY_LEN){ + + query_len = 100 + id_col.len + num_col.len + url_col.len + + 2 * risk_group_col.len + reg_table.len + spare_col.len + + status_col.len; + + if(query_len >= UL_DB_QUERY_LEN) { LM_ERR("weird: extremely long query.\n"); return -1; } - - if (sprintf(query, - "SELECT " - "%.*s, " - "%.*s, " - "%.*s, " - "%.*s " - "FROM %.*s " - "WHERE " - "%.*s=1 AND " - "%.*s!=%i AND " - "%.*s=%i " - "LIMIT 1 " - "FOR UPDATE", - id_col.len, id_col.s, - num_col.len, num_col.s, - url_col.len, url_col.s, - risk_group_col.len, risk_group_col.s, - reg_table.len, reg_table.s, - spare_col.len, spare_col.s, - risk_group_col.len, risk_group_col.s, db->rg, - status_col.len, status_col.s, DB_ON) < 0) { + + if(sprintf(query, + "SELECT " + "%.*s, " + "%.*s, " + "%.*s, " + "%.*s " + "FROM %.*s " + "WHERE " + "%.*s=1 AND " + "%.*s!=%i AND " + "%.*s=%i " + "LIMIT 1 " + "FOR UPDATE", + id_col.len, id_col.s, num_col.len, num_col.s, url_col.len, + url_col.s, risk_group_col.len, risk_group_col.s, reg_table.len, + reg_table.s, spare_col.len, spare_col.s, risk_group_col.len, + risk_group_col.s, db->rg, status_col.len, status_col.s, DB_ON) + < 0) { LM_ERR("could not print query\n"); return -1; } @@ -159,48 +162,52 @@ static int ul_db_failover_get_spare(db_func_t * dbf, db1_con_t * dbh, ul_db_t * dbf->free_result(dbh, res); return -1; } - + row = RES_ROWS(res); spare.id = VAL_INT(ROW_VALUES(row) + 0); spare.db[0].no = VAL_INT(ROW_VALUES(row) + 1); - if(strlen(VAL_STRING(ROW_VALUES(row) + 2)) >= UL_DB_URL_LEN){ + if(strlen(VAL_STRING(ROW_VALUES(row) + 2)) >= UL_DB_URL_LEN) { LM_ERR("weird: " - "db URL longer than %i.\n", UL_DB_URL_LEN); + "db URL longer than %i.\n", + UL_DB_URL_LEN); dbf->free_result(dbh, res); return -1; } strcpy(spare.db[0].url.s, VAL_STRING(ROW_VALUES(row) + 2)); spare.db[0].url.len = strlen(spare.db[0].url.s); spare.db[0].rg = VAL_INT(ROW_VALUES(row) + 3); - + dbf->free_result(dbh, res); return 0; } -static int ul_db_failover_switch(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no) { - ul_db_t * db; +static int ul_db_failover_switch( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no) +{ + ul_db_t *db; int check; int old_num, new_num; - - if((db = get_db_by_num(handle, no)) == NULL){ + + if((db = get_db_by_num(handle, no)) == NULL) { LM_ERR("could not find id %i, " - "db %i.\n", handle->id, no); + "db %i.\n", + handle->id, no); return -1; } memset(&tmp, 0, sizeof(ul_db_t)); memmove(&tmp, db, sizeof(ul_db_t)); memset(tmp.url.s, 0, UL_DB_URL_LEN); memmove(tmp.url.s, db->url.s, UL_DB_URL_LEN); - + check = check_handle_data(dbf, dbh, db, handle->id); - if(check < 0){ + if(check < 0) { LM_ERR("data check failed.\n"); return -1; - } else if(check > 0){ + } else if(check > 0) { LM_ERR("failover already done.\n"); return 0; } - if((new_num = get_max_no_of_db_id(dbf, dbh, spare.id)) < 0){ + if((new_num = get_max_no_of_db_id(dbf, dbh, spare.id)) < 0) { LM_ERR("getting highest num failed.\n"); return -1; } @@ -210,7 +217,7 @@ static int ul_db_failover_switch(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_ tmp.status = DB_OFF; tmp.failover_time = time(NULL); tmp.spare = 1; - if(store_handle_data(dbf, dbh, &tmp, handle->id, old_num, spare.id) < 0){ + if(store_handle_data(dbf, dbh, &tmp, handle->id, old_num, spare.id) < 0) { LM_ERR("storing data of broken db failed.\n"); return -1; } @@ -218,14 +225,17 @@ static int ul_db_failover_switch(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_ spare.db[0].errors = 0; old_num = spare.db[0].no; spare.db[0].no = db->no; - if(store_handle_data(dbf, dbh, &spare.db[0], spare.id, old_num, handle->id) < 0){ + if(store_handle_data(dbf, dbh, &spare.db[0], spare.id, old_num, handle->id) + < 0) { LM_ERR("storing data of activated spare db failed.\n"); return -1; } return 0; } -int db_failover_deactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no) { +int db_failover_deactivate( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no) +{ db_key_t cols[3]; db_key_t keys[3]; db_val_t vals[3]; @@ -236,18 +246,18 @@ int db_failover_deactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * ha vals[0].type = DB1_INT; vals[0].nul = 0; vals[0].val.int_val = DB_OFF; - + cols[1] = &failover_time_col; - vals[1].type = DB1_DATETIME; + vals[1].type = DB1_DATETIME; vals[1].nul = 0; vals[1].val.time_val = time(NULL); - + keys[0] = &id_col; op[0] = OP_EQ; key_vals[0].type = DB1_INT; key_vals[0].nul = 0; key_vals[0].val.int_val = handle->id; - + keys[1] = &num_col; op[1] = OP_EQ; key_vals[1].type = DB1_INT; @@ -267,8 +277,9 @@ int db_failover_deactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * ha } - -int db_failover_reactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no) { +int db_failover_reactivate( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no) +{ db_key_t cols[3]; db_key_t keys[2]; db_val_t vals[3]; @@ -279,23 +290,23 @@ int db_failover_reactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * ha vals[0].type = DB1_INT; vals[0].nul = 0; vals[0].val.int_val = DB_ON; - + cols[1] = &failover_time_col; - vals[1].type = DB1_DATETIME; + vals[1].type = DB1_DATETIME; vals[1].nul = 0; vals[1].val.time_val = time(NULL); - + cols[2] = &error_col; - vals[2].type = DB1_INT; + vals[2].type = DB1_INT; vals[2].nul = 0; vals[2].val.time_val = 0; - + keys[0] = &id_col; op[0] = OP_EQ; key_vals[0].type = DB1_INT; key_vals[0].nul = 0; key_vals[0].val.int_val = handle->id; - + keys[1] = &num_col; op[1] = OP_EQ; key_vals[1].type = DB1_INT; @@ -314,31 +325,31 @@ int db_failover_reactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * ha return 0; } -int db_failover_reset(db_func_t * dbf, db1_con_t * dbh,int id, int no){ +int db_failover_reset(db_func_t *dbf, db1_con_t *dbh, int id, int no) +{ db_key_t cols[1]; db_key_t keys[2]; db_val_t vals[1]; db_val_t key_vals[2]; db_op_t op[2]; - + cols[0] = &failover_time_col; - vals[0].type = DB1_DATETIME; + vals[0].type = DB1_DATETIME; vals[0].nul = 0; vals[0].val.time_val = UL_DB_ZERO_TIME; - + keys[0] = &id_col; op[0] = OP_EQ; key_vals[0].type = DB1_INT; key_vals[0].nul = 0; key_vals[0].val.int_val = id; - + keys[1] = &num_col; op[1] = OP_EQ; key_vals[1].type = DB1_INT; key_vals[1].nul = 0; key_vals[1].val.int_val = no; - if(dbf->use_table(dbh, ®_table) < 0) { LM_ERR("could not use reg_table.\n"); diff --git a/src/modules/p_usrloc/ul_db_failover.h b/src/modules/p_usrloc/ul_db_failover.h index 9e3ef02de26..5427a904bdd 100644 --- a/src/modules/p_usrloc/ul_db_failover.h +++ b/src/modules/p_usrloc/ul_db_failover.h @@ -26,16 +26,18 @@ #include "../../lib/srdb1/db.h" #define FAILOVER_MODE_NONE 1 -#define FAILOVER_MODE_NORMAL (1<<1) -#define FAILOVER_MODE_LAST (1<<2) +#define FAILOVER_MODE_NORMAL (1 << 1) +#define FAILOVER_MODE_LAST (1 << 2) -int db_failover(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no); +int db_failover(db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no); -int db_failover_deactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no); +int db_failover_deactivate( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no); -int db_failover_reactivate(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no); +int db_failover_reactivate( + db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int no); -int db_failover_reset(db_func_t * dbf, db1_con_t * dbh, int id, int no); +int db_failover_reset(db_func_t *dbf, db1_con_t *dbh, int id, int no); #endif diff --git a/src/modules/p_usrloc/ul_db_failover_func.c b/src/modules/p_usrloc/ul_db_failover_func.c index 663907e0d66..d2df90e6ea8 100644 --- a/src/modules/p_usrloc/ul_db_failover_func.c +++ b/src/modules/p_usrloc/ul_db_failover_func.c @@ -26,93 +26,97 @@ #include "ul_db.h" static str autocommit_off = str_init("SET AUTOCOMMIT=0"); -static str fail_isolation_level = str_init("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"); +static str fail_isolation_level = + str_init("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"); static str start_transaction = str_init("START TRANSACTION"); static str commit = str_init("COMMIT"); static str rollback = str_init("ROLLBACK"); static str autocommit_on = str_init("SET AUTOCOMMIT=1"); static char query[UL_DB_QUERY_LEN]; -int ul_db_failover_prepare(db_func_t * dbf, db1_con_t * dbh) { +int ul_db_failover_prepare(db_func_t *dbf, db1_con_t *dbh) +{ if(dbf->raw_query(dbh, &autocommit_off, NULL) < 0) { LM_ERR("could not " - "set autocommit off!\n"); + "set autocommit off!\n"); return -2; } if(dbf->raw_query(dbh, &fail_isolation_level, NULL) < 0) { LM_ERR("could not " - "set transaction isolation level!\n"); + "set transaction isolation level!\n"); return -2; } if(dbf->raw_query(dbh, &start_transaction, NULL) < 0) { LM_ERR("could not " - "start transaction!\n"); + "start transaction!\n"); return -2; } return 0; } -int ul_db_failover_commit(db_func_t * dbf, db1_con_t * dbh) { +int ul_db_failover_commit(db_func_t *dbf, db1_con_t *dbh) +{ if(dbf->raw_query(dbh, &commit, NULL) < 0) { LM_ERR("transaction commit " - "failed.\n"); + "failed.\n"); return -1; } if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) { LM_ERR("could not turn " - "transaction autocommit on.\n"); + "transaction autocommit on.\n"); return -2; } return 0; } -int ul_db_failover_rollback(db_func_t * dbf, db1_con_t * dbh) { +int ul_db_failover_rollback(db_func_t *dbf, db1_con_t *dbh) +{ LM_ERR("rolling back failover " - "transaction.\n"); + "transaction.\n"); if(dbf->raw_query(dbh, &rollback, NULL) < 0) { LM_ERR("could not rollback " - "transaction.\n"); + "transaction.\n"); return -1; } if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) { LM_ERR("could not set " - "autocommit on.\n"); + "autocommit on.\n"); return -2; } return 0; } -int get_max_no_of_db_id(db_func_t * dbf, db1_con_t * dbh, int id){ - db1_res_t * res; - db_row_t * row; +int get_max_no_of_db_id(db_func_t *dbf, db1_con_t *dbh, int id) +{ + db1_res_t *res; + db_row_t *row; int query_len, max; str tmp; query_len = 50 + reg_table.len + id_col.len + num_col.len; - if(query_len > UL_DB_QUERY_LEN){ + if(query_len > UL_DB_QUERY_LEN) { LM_ERR("weird: query too long.\n"); return -1; } memset(query, 0, UL_DB_QUERY_LEN); - if (sprintf(query, - "SELECT MAX(%.*s) " - "FROM %.*s " - "WHERE %.*s='%i'", - num_col.len, num_col.s, - reg_table.len, reg_table.s, - id_col.len, id_col.s, id) < 0) { + if(sprintf(query, + "SELECT MAX(%.*s) " + "FROM %.*s " + "WHERE %.*s='%i'", + num_col.len, num_col.s, reg_table.len, reg_table.s, id_col.len, + id_col.s, id) + < 0) { LM_ERR("could not print query\n"); return -1; } tmp.s = query; tmp.len = strlen(query); - - if(dbf->raw_query(dbh, &tmp, &res) < 0){ - LM_ERR("weird: could not query %.*s.\n", - reg_table.len, reg_table.s); + + if(dbf->raw_query(dbh, &tmp, &res) < 0) { + LM_ERR("weird: could not query %.*s.\n", reg_table.len, reg_table.s); return -1; } - if(RES_ROW_N(res) == 0){ + if(RES_ROW_N(res) == 0) { LM_ERR("weird: no data found for id %i\n", id); dbf->free_result(dbh, res); return -1; @@ -123,114 +127,117 @@ int get_max_no_of_db_id(db_func_t * dbf, db1_con_t * dbh, int id){ return max; } -int store_handle_data(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db, int id, int old_num, int new_id){ +int store_handle_data(db_func_t *dbf, db1_con_t *dbh, ul_db_t *db, int id, + int old_num, int new_id) +{ db_key_t cols[8]; db_val_t vals[8]; db_key_t keys[2]; db_val_t key_vals[8]; db_op_t op[2]; - + cols[0] = &id_col; vals[0].type = DB1_INT; vals[0].nul = 0; vals[0].val.int_val = new_id; - + cols[1] = &num_col; vals[1].type = DB1_INT; vals[1].nul = 0; vals[1].val.int_val = db->no; - + cols[2] = &url_col; vals[2].type = DB1_STRING; vals[2].nul = 0; vals[2].val.string_val = db->url.s; - + cols[3] = &error_col; vals[3].type = DB1_INT; vals[3].nul = 0; vals[3].val.int_val = db->errors; - + cols[4] = &failover_time_col; vals[4].type = DB1_DATETIME; vals[4].nul = 0; vals[4].val.time_val = db->failover_time; - + cols[5] = &spare_col; vals[5].type = DB1_INT; vals[5].nul = 0; vals[5].val.int_val = db->spare; - + cols[6] = &status_col; vals[6].type = DB1_INT; vals[6].nul = 0; vals[6].val.int_val = db->status; - + cols[7] = &risk_group_col; vals[7].type = DB1_INT; vals[7].nul = 0; vals[7].val.int_val = db->rg; - + keys[0] = &id_col; op[0] = OP_EQ; key_vals[0].type = DB1_INT; key_vals[0].nul = 0; key_vals[0].val.int_val = id; - + keys[1] = &num_col; op[1] = OP_EQ; key_vals[1].type = DB1_INT; key_vals[1].nul = 0; key_vals[1].val.int_val = old_num; - - if(dbf->use_table(dbh, ®_table) < 0){ + + if(dbf->use_table(dbh, ®_table) < 0) { LM_ERR("could not use reg_table.\n"); return -1; } - if(dbf->update(dbh, keys, op, key_vals, cols, vals, 2, 7) < 0){ + if(dbf->update(dbh, keys, op, key_vals, cols, vals, 2, 7) < 0) { LM_ERR("could insert handle data.\n"); return -1; } return 0; } -int check_handle_data(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db, int id){ +int check_handle_data(db_func_t *dbf, db1_con_t *dbh, ul_db_t *db, int id) +{ db_key_t cols[2]; db_key_t keys[4]; db_val_t key_vals[4]; db_op_t op[4]; - db1_res_t * res; - + db1_res_t *res; + cols[0] = &id_col; - + keys[0] = &id_col; op[0] = OP_EQ; key_vals[0].type = DB1_INT; key_vals[0].nul = 0; key_vals[0].val.int_val = id; - + keys[1] = &num_col; op[1] = OP_EQ; key_vals[1].type = DB1_INT; key_vals[1].nul = 0; key_vals[1].val.int_val = db->no; - + keys[2] = &url_col; op[2] = OP_EQ; key_vals[2].type = DB1_STRING; key_vals[2].nul = 0; key_vals[2].val.string_val = db->url.s; - - if(dbf->use_table(dbh, ®_table) < 0){ + + if(dbf->use_table(dbh, ®_table) < 0) { LM_ERR("could not use " - "reg table.\n"); + "reg table.\n"); return -1; } - if(dbf->query(dbh, keys, op, key_vals, cols, 3, 1, NULL, &res) < 0){ + if(dbf->query(dbh, keys, op, key_vals, cols, 3, 1, NULL, &res) < 0) { LM_ERR("could not use " - "query table.\n"); + "query table.\n"); return -1; } - if(RES_ROW_N(res) == 0){ + if(RES_ROW_N(res) == 0) { dbf->free_result(dbh, res); return 1; } diff --git a/src/modules/p_usrloc/ul_db_failover_func.h b/src/modules/p_usrloc/ul_db_failover_func.h index e465f23c137..b3729e3bec5 100644 --- a/src/modules/p_usrloc/ul_db_failover_func.h +++ b/src/modules/p_usrloc/ul_db_failover_func.h @@ -25,16 +25,17 @@ #include "../../lib/srdb1/db.h" #include "ul_db_handle.h" -int ul_db_failover_prepare(db_func_t * dbf, db1_con_t * dbh); +int ul_db_failover_prepare(db_func_t *dbf, db1_con_t *dbh); -int ul_db_failover_commit(db_func_t * dbf, db1_con_t * dbh); +int ul_db_failover_commit(db_func_t *dbf, db1_con_t *dbh); -int ul_db_failover_rollback(db_func_t * dbf, db1_con_t * dbh); +int ul_db_failover_rollback(db_func_t *dbf, db1_con_t *dbh); -int get_max_no_of_db_id(db_func_t * dbf, db1_con_t * dbh, int id); +int get_max_no_of_db_id(db_func_t *dbf, db1_con_t *dbh, int id); -int store_handle_data(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db, int id, int old_num, int new_id); +int store_handle_data(db_func_t *dbf, db1_con_t *dbh, ul_db_t *db, int id, + int old_num, int new_id); -int check_handle_data(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db, int id); +int check_handle_data(db_func_t *dbf, db1_con_t *dbh, ul_db_t *db, int id); #endif diff --git a/src/modules/p_usrloc/ul_db_form_query.c b/src/modules/p_usrloc/ul_db_form_query.c index 02a4bab3285..c0a73f03684 100644 --- a/src/modules/p_usrloc/ul_db_form_query.c +++ b/src/modules/p_usrloc/ul_db_form_query.c @@ -24,11 +24,14 @@ #include "ul_db.h" #include "p_usrloc_mod.h" -static int db_do_query(ul_db_op_t ul_op, db_func_t * dbf, db1_con_t * dbh, str * table, db_key_t* _k, db_op_t* _o, - db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un); +static int db_do_query(ul_db_op_t ul_op, db_func_t *dbf, db1_con_t *dbh, + str *table, db_key_t *_k, db_op_t *_o, db_val_t *_v, db_key_t *_uk, + db_val_t *_uv, int _n, int _un); -int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_key_t* _k, db_op_t* _o, - db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { +int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t *handle, str *table, + db_key_t *_k, db_op_t *_o, db_val_t *_v, db_key_t *_uk, db_val_t *_uv, + int _n, int _un) +{ int i; int working_c[DB_NUM]; int working_r[DB_NUM]; @@ -41,14 +44,15 @@ int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_k } if(db_use_transactions) { - for(i=0; ilen, table->s, handle->id); + " on table %.*s with id %i.\n", + table->len, table->s, handle->id); w = get_working_sum(working_r, DB_NUM); if(db_check_policy(DB_POL_MOD, w, handle->working) < 0) { ul_db_tran_rollback(handle, working_r); @@ -56,16 +60,18 @@ int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_k } } - for(i=0; idb[i].status == DB_ON) && (working_c[i])) { - if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) { + if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, + table, _k, _o, _v, _uk, _uv, _n, _un) + < 0) { LM_ERR("error during querying " - "table %.*s with id %i on db %i.\n", - table->len, table->s, handle->id, i); + "table %.*s with id %i on db %i.\n", + table->len, table->s, handle->id, i); if(db_handle_error(handle, handle->db[i].no) < 0) { LM_CRIT("could not handle error on db %i, handle, %i\n", - handle->id, handle->db[i].no); + handle->id, handle->db[i].no); } errors++; working_c[i] = 0; @@ -85,12 +91,14 @@ int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_k } return ul_db_tran_commit(handle, working_c); } else { - for(i=0; idb[i].status == DB_ON) { - if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) { + if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, + table, _k, _o, _v, _uk, _uv, _n, _un) + < 0) { if(db_handle_error(handle, handle->db[i].no) < 0) { LM_CRIT("could not handle error on db %i, handle, %i\n", - handle->id, handle->db[i].no); + handle->id, handle->db[i].no); } return -1; } @@ -100,48 +108,56 @@ int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_k } } -static int db_do_query(ul_db_op_t ul_op, db_func_t * dbf, db1_con_t * dbh, str * table, db_key_t* _k, db_op_t* _o, - db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { +static int db_do_query(ul_db_op_t ul_op, db_func_t *dbf, db1_con_t *dbh, + str *table, db_key_t *_k, db_op_t *_o, db_val_t *_v, db_key_t *_uk, + db_val_t *_uv, int _n, int _un) +{ if(dbf->use_table(dbh, table) < 0) { LM_ERR("error in use table %.*s.\n", table->len, table->s); return -1; } switch(ul_op) { - case UL_DB_INS: + case UL_DB_INS: if(dbf->insert(dbh, _k, _v, _n) < 0) { LM_ERR("error in inserting into " - "table %.*s.\n", table->len, table->s); + "table %.*s.\n", + table->len, table->s); return -1; } return 0; - case UL_DB_REPL: + case UL_DB_REPL: if(dbf->replace(dbh, _k, _v, _n, _un, 0) < 0) { LM_ERR("error in replacing in " - "table %.*s.\n", table->len, table->s); + "table %.*s.\n", + table->len, table->s); return -1; } return 0; - case UL_DB_INS_UPD: - if(dbf->insert_update(dbh, _k, _v, _n ) < 0) { + case UL_DB_INS_UPD: + if(dbf->insert_update(dbh, _k, _v, _n) < 0) { LM_ERR("error in inserting/updating in " - "table %.*s.\n", table->len, table->s); + "table %.*s.\n", + table->len, table->s); return -1; } return 0; - case UL_DB_UPD: + case UL_DB_UPD: if(dbf->update(dbh, _k, _o, _v, _uk, _uv, _n, _un) < 0) { LM_ERR("error in updating " - "table %.*s.\n", table->len, table->s); + "table %.*s.\n", + table->len, table->s); return -1; } return 0; - case UL_DB_DEL: + case UL_DB_DEL: if(dbf->delete(dbh, _k, _o, _v, _n) < 0) { - LM_ERR("error in deleting from table %.*s.\n", table->len, table->s); + LM_ERR("error in deleting from table %.*s.\n", table->len, + table->s); return -1; } return 0; - default: return -1; + default: + return -1; } return 0; } diff --git a/src/modules/p_usrloc/ul_db_form_query.h b/src/modules/p_usrloc/ul_db_form_query.h index 51c4eb6d234..e3630c70023 100644 --- a/src/modules/p_usrloc/ul_db_form_query.h +++ b/src/modules/p_usrloc/ul_db_form_query.h @@ -25,24 +25,17 @@ #include "../../lib/srdb1/db.h" #include "ul_db_handle.h" -typedef enum { +typedef enum +{ UL_DB_INS, UL_DB_REPL, UL_DB_INS_UPD, UL_DB_UPD, UL_DB_DEL, -}ul_db_op_t; +} ul_db_op_t; -int db_submit_query(ul_db_op_t ul_op, - ul_db_handle_t * handle, - str * table, - db_key_t* _k, - db_op_t* _o, - db_val_t* _v, - db_key_t* _uk, - db_val_t* _uv, - int _n, - int _un - ); +int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t *handle, str *table, + db_key_t *_k, db_op_t *_o, db_val_t *_v, db_key_t *_uk, db_val_t *_uv, + int _n, int _un); #endif diff --git a/src/modules/p_usrloc/ul_db_handle.c b/src/modules/p_usrloc/ul_db_handle.c index 4e4eab0848e..d921e8b6699 100644 --- a/src/modules/p_usrloc/ul_db_handle.c +++ b/src/modules/p_usrloc/ul_db_handle.c @@ -26,59 +26,62 @@ #include "config.h" #include "../../core/crc.h" -static ul_db_handle_list_t * db_handles = NULL; +static ul_db_handle_list_t *db_handles = NULL; static ul_db_handle_t tmp; -static ul_db_handle_t * allocate_handle(void); +static ul_db_handle_t *allocate_handle(void); -static ul_db_handle_list_t * allocate_handle_list(void); +static ul_db_handle_list_t *allocate_handle_list(void); -static void free_handle(ul_db_handle_list_t * element); +static void free_handle(ul_db_handle_list_t *element); -static int check_status(ul_db_handle_t * a, ul_db_handle_t * b); +static int check_status(ul_db_handle_t *a, ul_db_handle_t *b); -static int compute_id(str* first, str* second); +static int compute_id(str *first, str *second); -static int release_handle(ul_db_handle_t * handle); +static int release_handle(ul_db_handle_t *handle); -static int activate_handle(ul_db_handle_t * handle); +static int activate_handle(ul_db_handle_t *handle); -ul_db_handle_t * get_handle(db_func_t * dbf, db1_con_t * dbh, str * first, str * second) { - struct ul_db_handle_list * element; - ul_db_handle_t * ret = NULL; +ul_db_handle_t *get_handle( + db_func_t *dbf, db1_con_t *dbh, str *first, str *second) +{ + struct ul_db_handle_list *element; + ul_db_handle_t *ret = NULL; int db_ok = 0; int id; - switch(alg_location){ - case 1:default:{ - /* atm this is the only matching mode */ - if( max_loc_nr == 0){ - LM_WARN("max_loc_nr is 0 . Try to recompute value\n"); - if( load_location_number(dbf, dbh, &max_loc_nr) != 0 ){ - LM_ERR("Could not get location number\n"); + switch(alg_location) { + case 1: + default: { + /* atm this is the only matching mode */ + if(max_loc_nr == 0) { + LM_WARN("max_loc_nr is 0 . Try to recompute value\n"); + if(load_location_number(dbf, dbh, &max_loc_nr) != 0) { + LM_ERR("Could not get location number\n"); + return NULL; + } + } + if((id = compute_id(first, second)) < 0) { return NULL; } - } - if((id = compute_id(first, second)) < 0){ - return NULL; + break; } - break; - } -/* default:{ + /* default:{ LM_ERR("No suitable selection for location\n"); return NULL; } */ } - - if(load_data(dbf, dbh, &tmp, id) < 0){ + + if(load_data(dbf, dbh, &tmp, id) < 0) { // load_data should explain this return NULL; } - element = db_handles; + element = db_handles; db_ok = 0; while(element && element->handle) { if(element->handle->id == tmp.id) { @@ -89,7 +92,7 @@ ul_db_handle_t * get_handle(db_func_t * dbf, db1_con_t * dbh, str * first, str * } ret = element->handle; } - if((element->handle->expires < time(NULL)) && element->handle->active){ + if((element->handle->expires < time(NULL)) && element->handle->active) { release_handle(element->handle); } element = element->next; @@ -116,30 +119,33 @@ ul_db_handle_t * get_handle(db_func_t * dbf, db1_con_t * dbh, str * first, str * ret = NULL; } ret: - if(ret && !ret->active){ + if(ret && !ret->active) { activate_handle(ret); } return ret; } -int refresh_handle(ul_db_handle_t * handle, ul_db_handle_t * new_data, int error_handling) { +int refresh_handle( + ul_db_handle_t *handle, ul_db_handle_t *new_data, int error_handling) +{ int db_ok = 0; int i, ret; str tmpurl; handle->id = new_data->id; - + handle->working = 0; handle->expires = time(NULL) + connection_expires; - for(i=0; idb[i].status = new_data->db[i].status; handle->db[i].errors = new_data->db[i].errors; handle->db[i].failover_time = new_data->db[i].failover_time; handle->db[i].rg = new_data->db[i].rg; handle->db[i].no = new_data->db[i].no; - - if((handle->db[i].url.len != new_data->db[i].url.len) || (strcmp(handle->db[i].url.s, new_data->db[i].url.s) != 0)) { + + if((handle->db[i].url.len != new_data->db[i].url.len) + || (strcmp(handle->db[i].url.s, new_data->db[i].url.s) != 0)) { memset(handle->db[i].url.s, 0, UL_DB_URL_LEN); strcpy(handle->db[i].url.s, new_data->db[i].url.s); handle->db[i].url.len = new_data->db[i].url.len; @@ -150,7 +156,7 @@ int refresh_handle(ul_db_handle_t * handle, ul_db_handle_t * new_data, int error memset(&handle->db[i].dbf, 0, sizeof(db_func_t)); tmpurl.len = handle->db[i].url.len; tmpurl.s = handle->db[i].url.s; - if(db_bind_mod(&tmpurl, &handle->db[i].dbf) < 0){ + if(db_bind_mod(&tmpurl, &handle->db[i].dbf) < 0) { LM_ERR("could not bind db module.\n"); return -1; } @@ -163,22 +169,26 @@ int refresh_handle(ul_db_handle_t * handle, ul_db_handle_t * new_data, int error LM_DBG("connect id %i db %i.\n", handle->id, handle->db[i].no); tmpurl.len = handle->db[i].url.len; tmpurl.s = handle->db[i].url.s; - if((handle->db[i].dbh = handle->db[i].dbf.init(&tmpurl)) == NULL) { + if((handle->db[i].dbh = handle->db[i].dbf.init(&tmpurl)) + == NULL) { LM_ERR("id: %i could not " - "connect database %i.\n", handle->id, handle->db[i].no); - if(error_handling){ - if(db_handle_error(handle, handle->db[i].no) < 0){ + "connect database %i.\n", + handle->id, handle->db[i].no); + if(error_handling) { + if(db_handle_error(handle, handle->db[i].no) < 0) { LM_ERR("id: %i could not " - "handle error on database %i.\n", handle->id, handle->db[i].no); + "handle error on database %i.\n", + handle->id, handle->db[i].no); } } } else { db_ok++; } } - } else if (handle->db[i].status == DB_INACTIVE) { - if(handle->db[i].dbh){ - LM_DBG("deactivate id %i db %i.\n", handle->id, handle->db[i].no); + } else if(handle->db[i].status == DB_INACTIVE) { + if(handle->db[i].dbh) { + LM_DBG("deactivate id %i db %i.\n", handle->id, + handle->db[i].no); handle->db[i].dbf.close(handle->db[i].dbh); handle->db[i].dbh = NULL; } @@ -190,17 +200,20 @@ int refresh_handle(ul_db_handle_t * handle, ul_db_handle_t * new_data, int error } } } - if((ret = db_check_policy(DB_POL_OP, db_ok, handle->working)) < 0){ + if((ret = db_check_policy(DB_POL_OP, db_ok, handle->working)) < 0) { LM_ERR("id %i: too few dbs working\n", handle->id); } return ret; } -static int release_handle(ul_db_handle_t * handle){ +static int release_handle(ul_db_handle_t *handle) +{ int i; - LM_NOTICE("deactivating handle id %i, db 1: %.*s, db2: %.*s\n", handle->id, handle->db[0].url.len, handle->db[0].url.s, handle->db[1].url.len, handle->db[1].url.s); - for(i=0; idb[i].dbh){ + LM_NOTICE("deactivating handle id %i, db 1: %.*s, db2: %.*s\n", + handle->id, handle->db[0].url.len, handle->db[0].url.s, + handle->db[1].url.len, handle->db[1].url.s); + for(i = 0; i < DB_NUM; i++) { + if(handle->db[i].dbh) { handle->db[i].dbf.close(handle->db[i].dbh); handle->db[i].dbh = NULL; } @@ -209,15 +222,20 @@ static int release_handle(ul_db_handle_t * handle){ return ul_unregister_watch_db(handle->id); } -static int activate_handle(ul_db_handle_t * handle){ - LM_NOTICE("activating handle id %i, db 1: %.*s, db2: %.*s\n", handle->id, handle->db[0].url.len, handle->db[0].url.s, handle->db[1].url.len, handle->db[1].url.s); +static int activate_handle(ul_db_handle_t *handle) +{ + LM_NOTICE("activating handle id %i, db 1: %.*s, db2: %.*s\n", handle->id, + handle->db[0].url.len, handle->db[0].url.s, handle->db[1].url.len, + handle->db[1].url.s); handle->active = 1; return ul_register_watch_db(handle->id); } -static ul_db_handle_list_t * allocate_handle_list(void) { - ul_db_handle_list_t * ret; - if((ret = (ul_db_handle_list_t *)pkg_malloc(sizeof(ul_db_handle_list_t))) == NULL) { +static ul_db_handle_list_t *allocate_handle_list(void) +{ + ul_db_handle_list_t *ret; + if((ret = (ul_db_handle_list_t *)pkg_malloc(sizeof(ul_db_handle_list_t))) + == NULL) { LM_ERR("couldn't allocate private memory.\n"); return NULL; } @@ -228,8 +246,9 @@ static ul_db_handle_list_t * allocate_handle_list(void) { return ret; } -static ul_db_handle_t * allocate_handle(void) { - ul_db_handle_t * ret; +static ul_db_handle_t *allocate_handle(void) +{ + ul_db_handle_t *ret; if((ret = (ul_db_handle_t *)pkg_malloc(sizeof(ul_db_handle_t))) == NULL) { LM_ERR("couldn't allocate pkg mem.\n"); return NULL; @@ -241,14 +260,15 @@ static ul_db_handle_t * allocate_handle(void) { } return ret; } -int load_location_number(db_func_t * dbf, db1_con_t* dbh, int *loc_nr){ +int load_location_number(db_func_t *dbf, db1_con_t *dbh, int *loc_nr) +{ static char query[UL_DB_QUERY_LEN]; - db1_res_t * res; - db_row_t * row; + db1_res_t *res; + db_row_t *row; int query_len; str tmp; - if(!loc_nr || !dbf || !dbh){ + if(!loc_nr || !dbf || !dbh) { LM_ERR("NULL parameter passed \n"); return -1; } @@ -258,62 +278,66 @@ int load_location_number(db_func_t * dbf, db1_con_t* dbh, int *loc_nr){ LM_ERR("weird: query larger than %i bytes.\n", UL_DB_QUERY_LEN); return -1; } - + memset(query, 0, UL_DB_QUERY_LEN); - + if(sprintf(query, - "SELECT MAX(%.*s) " - "FROM " - "%.*s " - "WHERE %.*s = 1;", id_col.len, id_col.s, reg_table.len, reg_table.s, status_col.len, status_col.s) < 0){ - LM_ERR("could not sprintf query\n"); - return -1; + "SELECT MAX(%.*s) " + "FROM " + "%.*s " + "WHERE %.*s = 1;", + id_col.len, id_col.s, reg_table.len, reg_table.s, status_col.len, + status_col.s) + < 0) { + LM_ERR("could not sprintf query\n"); + return -1; } - LM_DBG("%s\n",query); - + LM_DBG("%s\n", query); + tmp.s = query; tmp.len = strlen(query); - if (dbf->raw_query (dbh, &tmp, &res) < 0) { + if(dbf->raw_query(dbh, &tmp, &res) < 0) { LM_ERR("in database query.\n"); return -1; } - if (RES_ROW_N (res) == 0) { - dbf->free_result (dbh, res); - LM_DBG ("no data found\n"); + if(RES_ROW_N(res) == 0) { + dbf->free_result(dbh, res); + LM_DBG("no data found\n"); return 1; } row = RES_ROWS(res) + 0; /* only one row in answer */ - - if (VAL_NULL (ROW_VALUES(row) + 0)) { + + if(VAL_NULL(ROW_VALUES(row) + 0)) { LM_ERR("Weird: Empty Max ID Number\n"); - dbf->free_result (dbh, res); + dbf->free_result(dbh, res); return 1; } - *loc_nr = VAL_INT (ROW_VALUES(row) + 0); - dbf->free_result (dbh, res); - if(*loc_nr == 0){ + *loc_nr = VAL_INT(ROW_VALUES(row) + 0); + dbf->free_result(dbh, res); + if(*loc_nr == 0) { LM_ERR("No location in DB?!\n"); return 1; } return 0; } -int refresh_handles(db_func_t * dbf, db1_con_t * dbh) { - ul_db_handle_list_t * element; +int refresh_handles(db_func_t *dbf, db1_con_t *dbh) +{ + ul_db_handle_list_t *element; int i; element = db_handles; while(element) { - for(i=0; ihandle->db[i].dbh) { dbf->close(element->handle->db[i].dbh); element->handle->db[i].dbh = NULL; } } - if(load_data(dbf, dbh, &tmp, element->handle->id) < 0){ + if(load_data(dbf, dbh, &tmp, element->handle->id) < 0) { LM_ERR("couldn't load handle data.\n"); return -1; } @@ -326,13 +350,14 @@ int refresh_handles(db_func_t * dbf, db1_con_t * dbh) { return 1; } -void destroy_handles(void){ - ul_db_handle_list_t * element, * del; +void destroy_handles(void) +{ + ul_db_handle_list_t *element, *del; int i; element = db_handles; - while(element){ - for(i=0; ihandle->db[i].dbh){ + while(element) { + for(i = 0; i < DB_NUM; i++) { + if(element->handle->db[i].dbh) { element->handle->db[i].dbf.close(element->handle->db[i].dbh); element->handle->db[i].dbh = NULL; } @@ -343,9 +368,10 @@ void destroy_handles(void){ } } -ul_db_t * get_db_by_num(ul_db_handle_t * handle, int no) { +ul_db_t *get_db_by_num(ul_db_handle_t *handle, int no) +{ int i; - for(i=0; idb[i].no == no) { return &handle->db[i]; } @@ -353,25 +379,33 @@ ul_db_t * get_db_by_num(ul_db_handle_t * handle, int no) { return NULL; } -int check_handle(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle){ +int check_handle(db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle) +{ int i; str tmpurl; LM_INFO("checking id %i\n", handle->id); - if(load_data(dbf, dbh, &tmp, handle->id) < 0){ + if(load_data(dbf, dbh, &tmp, handle->id) < 0) { return -1; } refresh_handle(handle, &tmp, 1); - for(i=0; idb[i].url.len > 0){ - LM_INFO("checking id %i no %i, url %.*s, status %s\n", handle->id, handle->db[i].no, - handle->db[i].url.len, handle->db[i].url.s, (handle->db[i].status == DB_ON ? "ON" : (handle->db[i].status == DB_OFF ? "OFF" : "DEACTIVATED"))); + for(i = 0; i < DB_NUM; i++) { + if(handle->db[i].url.len > 0) { + LM_INFO("checking id %i no %i, url %.*s, status %s\n", handle->id, + handle->db[i].no, handle->db[i].url.len, + handle->db[i].url.s, + (handle->db[i].status == DB_ON + ? "ON" + : (handle->db[i].status == DB_OFF + ? "OFF" + : "DEACTIVATED"))); if(handle->db[i].status == DB_OFF) { tmpurl.len = handle->db[i].url.len; tmpurl.s = handle->db[i].url.s; - if((handle->db[i].dbh = handle->db[i].dbf.init(&tmpurl)) != NULL){ + if((handle->db[i].dbh = handle->db[i].dbf.init(&tmpurl)) + != NULL) { if(db_reactivate(handle, handle->db[i].no) < 0) { LM_ERR("could not reactivate id %i, db %i.\n", - handle->id, handle->db[i].no); + handle->id, handle->db[i].no); handle->db[i].dbf.close(handle->db[i].dbh); handle->db[i].dbh = NULL; } else { @@ -379,32 +413,40 @@ int check_handle(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle){ set_must_reconnect(); } } else { - LM_NOTICE("%s: db id %i, no %i url %.*s is still down\n", __FUNCTION__, - handle->id, handle->db[i].no, handle->db[i].url.len, handle->db[i].url.s); + LM_NOTICE("%s: db id %i, no %i url %.*s is still down\n", + __FUNCTION__, handle->id, handle->db[i].no, + handle->db[i].url.len, handle->db[i].url.s); } } else if((handle->db[i].status == DB_ON) && handle->db[i].dbh) { - if ((handle->db[i].failover_time - < (time(NULL) - - cfg_get(p_usrloc, p_usrloc_cfg, expire_time))) + if((handle->db[i].failover_time + < (time(NULL) + - cfg_get(p_usrloc, p_usrloc_cfg, + expire_time))) && (handle->db[i].failover_time != UL_DB_ZERO_TIME)) { - LM_ERR("%s: failover_time: %ld, now: %ld, delta: %ld, now going to reset failover time\n", __FUNCTION__, - (long int)handle->db[i].failover_time, (long int)time(NULL), (long int)(time(NULL) - handle->db[i].failover_time)); + LM_ERR("%s: failover_time: %ld, now: %ld, delta: %ld, now " + "going to reset failover time\n", + __FUNCTION__, (long int)handle->db[i].failover_time, + (long int)time(NULL), + (long int)(time(NULL) + - handle->db[i].failover_time)); if(db_reset_failover_time(handle, handle->db[i].no) < 0) { - LM_ERR("could not reset failover time for id %i, db %i.\n", - handle->id, handle->db[i].no); + LM_ERR("could not reset failover time for id %i, db " + "%i.\n", + handle->id, handle->db[i].no); } } } } else { LM_ERR("id %i, no url to check\n", handle->id); } - } + } return 1; } -static void free_handle(ul_db_handle_list_t * element) { - if(element){ - if(element->handle){ +static void free_handle(ul_db_handle_list_t *element) +{ + if(element) { + if(element->handle) { pkg_free(element->handle); } pkg_free(element); @@ -412,65 +454,72 @@ static void free_handle(ul_db_handle_list_t * element) { return; } -static int check_status(ul_db_handle_t * a, ul_db_handle_t * b){ +static int check_status(ul_db_handle_t *a, ul_db_handle_t *b) +{ int i; - if(!a->active){ + if(!a->active) { LM_NOTICE("id %i is inactive\n", a->id); return -1; } - if(must_refresh(a->check)){ + if(must_refresh(a->check)) { LM_NOTICE("id %i must be refreshed\n", a->id); return -1; } - if(must_reconnect(a->check)){ + if(must_reconnect(a->check)) { LM_NOTICE("id %i must be reconnected\n", a->id); return -1; } - for(i=0; idb[i].url.s, b->db[i].url.s) != 0){ - LM_NOTICE("id %i, db %s has different url\n", a->id, a->db[i].url.s); + for(i = 0; i < DB_NUM; i++) { + if(strcmp(a->db[i].url.s, b->db[i].url.s) != 0) { + LM_NOTICE( + "id %i, db %s has different url\n", a->id, a->db[i].url.s); return -1; } - if(a->db[i].status != b->db[i].status){ - LM_NOTICE("id %i, db %s has different status\n", a->id, a->db[i].url.s); + if(a->db[i].status != b->db[i].status) { + LM_NOTICE("id %i, db %s has different status\n", a->id, + a->db[i].url.s); return -1; } - if(a->db[i].no != b->db[i].no){ + if(a->db[i].no != b->db[i].no) { LM_NOTICE("id %i, db %s has different no\n", a->id, a->db[i].url.s); return -1; } - if((a->db[i].status == DB_ON) && (!a->db[i].dbh)){ - LM_NOTICE("id %i, db %s has inconsistent status (1)\n", a->id, a->db[i].url.s); + if((a->db[i].status == DB_ON) && (!a->db[i].dbh)) { + LM_NOTICE("id %i, db %s has inconsistent status (1)\n", a->id, + a->db[i].url.s); return -1; } - if((a->db[i].status == DB_OFF) && (a->db[i].dbh)){ - LM_NOTICE("id %i, db %s has inconsistent status (2)\n", a->id, a->db[i].url.s); + if((a->db[i].status == DB_OFF) && (a->db[i].dbh)) { + LM_NOTICE("id %i, db %s has inconsistent status (2)\n", a->id, + a->db[i].url.s); return -1; } } return 0; } -static int compute_id(str* first, str* second){ +static int compute_id(str *first, str *second) +{ unsigned int crc32_val; -#define BUF_MAXSIZE 1024 +#define BUF_MAXSIZE 1024 char aux[BUF_MAXSIZE]; str tmp; - if(!first){ + if(!first) { LM_ERR("Null first parameter received\n"); return -1; } - + if(use_domain && use_domain_crc32) { //compute crc32(user@domain) LM_DBG("XDBGX: compute_id HAS second key : %.*s", first->len, first->s); - if(!second){ - LM_ERR("Null second parameter received and use_domain set to true\n"); + if(!second) { + LM_ERR("Null second parameter received and use_domain set to " + "true\n"); return -1; } tmp.len = first->len + second->len + 1; - if( tmp.len > BUF_MAXSIZE - 1 ){ + if(tmp.len > BUF_MAXSIZE - 1) { LM_ERR("Very long user or domain\n"); return -1; } @@ -481,7 +530,7 @@ static int compute_id(str* first, str* second){ crc32_uint(&tmp, &crc32_val); return crc32_val % max_loc_nr + 1; - }else{ + } else { crc32_uint(first, &crc32_val); LM_DBG("crc32 for %.*s is %u\n", first->len, first->s, crc32_val); return crc32_val % max_loc_nr + 1; @@ -489,7 +538,8 @@ static int compute_id(str* first, str* second){ } -int load_data(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int id){ +int load_data(db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int id) +{ db1_res_t *res; db_row_t *row; db_key_t cols[7]; @@ -499,13 +549,13 @@ int load_data(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int id) db_key_t order; int i, ret = 0; - if(!dbf || !dbh || !handle){ + if(!dbf || !dbh || !handle) { LM_ERR("NULL-Pointer in Parameter\n"); return -1; } memset(handle, 0, sizeof(ul_db_handle_t)); - + cols[0] = &num_col; cols[1] = &url_col; cols[2] = &status_col; @@ -513,7 +563,7 @@ int load_data(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int id) cols[4] = &spare_col; cols[5] = &error_col; cols[6] = &risk_group_col; - + order = &num_col; keys[0] = &id_col; @@ -521,12 +571,12 @@ int load_data(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int id) key_vals[0].type = DB1_INT; key_vals[0].nul = 0; key_vals[0].val.int_val = id; - - if(dbf->use_table(dbh, ®_table) < 0){ + + if(dbf->use_table(dbh, ®_table) < 0) { LM_ERR("couldn't use table.\n"); return -1; } - if(dbf->query(dbh, keys, op, key_vals, cols, 1, 7, order, &res) < 0){ + if(dbf->query(dbh, keys, op, key_vals, cols, 1, 7, order, &res) < 0) { LM_ERR("error while doing db query.\n"); return -1; } @@ -537,16 +587,17 @@ int load_data(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int id) } handle->id = id; - - for(i=0; idb[i].no = (int)VAL_INT(ROW_VALUES(row)); - if(VAL_NULL(ROW_VALUES(row) + 1)){ + if(VAL_NULL(ROW_VALUES(row) + 1)) { LM_ERR("Weird: Empty database URL\n"); ret = -1; goto ret; } - if(strlen((char *)VAL_STRING(ROW_VALUES(row) + 1)) >= (UL_DB_URL_LEN - 1)){ + if(strlen((char *)VAL_STRING(ROW_VALUES(row) + 1)) + >= (UL_DB_URL_LEN - 1)) { LM_ERR("weird: very large URL (%d Bytes)\n", (int)(strlen((char *)VAL_STRING(ROW_VALUES(row) + 1)) + 1)); ret = -1; @@ -555,10 +606,10 @@ int load_data(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int id) strcpy(handle->db[i].url.s, (char *)VAL_STRING(ROW_VALUES(row) + 1)); handle->db[i].url.len = strlen(handle->db[i].url.s); handle->db[i].status = (int)VAL_INT(ROW_VALUES(row) + 2); - handle->db[i].failover_time = VAL_TIME (ROW_VALUES(row) + 3); - handle->db[i].spare = VAL_INT (ROW_VALUES(row) + 4); - handle->db[i].errors = VAL_INT (ROW_VALUES(row) + 5); - handle->db[i].rg = VAL_INT (ROW_VALUES(row) + 6); + handle->db[i].failover_time = VAL_TIME(ROW_VALUES(row) + 3); + handle->db[i].spare = VAL_INT(ROW_VALUES(row) + 4); + handle->db[i].errors = VAL_INT(ROW_VALUES(row) + 5); + handle->db[i].rg = VAL_INT(ROW_VALUES(row) + 6); } ret: dbf->free_result(dbh, res); diff --git a/src/modules/p_usrloc/ul_db_handle.h b/src/modules/p_usrloc/ul_db_handle.h index 9916dc56ac6..9484f1b83e7 100644 --- a/src/modules/p_usrloc/ul_db_handle.h +++ b/src/modules/p_usrloc/ul_db_handle.h @@ -34,12 +34,14 @@ #define UL_DB_URL_LEN 260 -typedef struct str2s { +typedef struct str2s +{ char s[UL_DB_URL_LEN]; int len; } str2; -typedef struct ul_db { +typedef struct ul_db +{ str2 url; int no; time_t failover_time; @@ -48,38 +50,42 @@ typedef struct ul_db { int status; int spare; int rg; - db1_con_t * dbh; + db1_con_t *dbh; db_func_t dbf; -}ul_db_t; +} ul_db_t; -typedef struct ul_db_handle { +typedef struct ul_db_handle +{ unsigned int id; - struct check_data * check; + struct check_data *check; int working; time_t expires; int active; ul_db_t db[DB_NUM]; -}ul_db_handle_t; +} ul_db_handle_t; -typedef struct ul_db_handle_list { - ul_db_handle_t * handle; - struct ul_db_handle_list * next; -}ul_db_handle_list_t; +typedef struct ul_db_handle_list +{ + ul_db_handle_t *handle; + struct ul_db_handle_list *next; +} ul_db_handle_list_t; void destroy_handles(void); -int refresh_handles(db_func_t * dbf, db1_con_t * dbh); +int refresh_handles(db_func_t *dbf, db1_con_t *dbh); -int load_location_number(db_func_t * dbf, db1_con_t * dbh, int*); +int load_location_number(db_func_t *dbf, db1_con_t *dbh, int *); -ul_db_handle_t * get_handle(db_func_t * dbf, db1_con_t * dbh, str * first, str * second); +ul_db_handle_t *get_handle( + db_func_t *dbf, db1_con_t *dbh, str *first, str *second); -int load_data(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int id); +int load_data(db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle, int id); -int refresh_handle(ul_db_handle_t * handle, ul_db_handle_t * new_data, int error_handling); +int refresh_handle( + ul_db_handle_t *handle, ul_db_handle_t *new_data, int error_handling); -ul_db_t * get_db_by_num(ul_db_handle_t * handle, int no); +ul_db_t *get_db_by_num(ul_db_handle_t *handle, int no); -int check_handle(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle); +int check_handle(db_func_t *dbf, db1_con_t *dbh, ul_db_handle_t *handle); #endif diff --git a/src/modules/p_usrloc/ul_db_ins.c b/src/modules/p_usrloc/ul_db_ins.c index 2a86653d466..8f769b21a31 100644 --- a/src/modules/p_usrloc/ul_db_ins.c +++ b/src/modules/p_usrloc/ul_db_ins.c @@ -22,7 +22,9 @@ #include "ul_db_ins.h" #include "ul_db_form_query.h" -int db_insert(ul_db_handle_t * handle, str * table, - db_key_t* _k, db_val_t* _v, int _n) { - return db_submit_query(UL_DB_INS, handle, table, _k, NULL, _v, NULL, NULL, _n, 0); +int db_insert( + ul_db_handle_t *handle, str *table, db_key_t *_k, db_val_t *_v, int _n) +{ + return db_submit_query( + UL_DB_INS, handle, table, _k, NULL, _v, NULL, NULL, _n, 0); } diff --git a/src/modules/p_usrloc/ul_db_ins.h b/src/modules/p_usrloc/ul_db_ins.h index 0df52ed9d98..ec658f43d9e 100644 --- a/src/modules/p_usrloc/ul_db_ins.h +++ b/src/modules/p_usrloc/ul_db_ins.h @@ -25,6 +25,7 @@ #include "../../lib/srdb1/db.h" #include "ul_db_handle.h" -int db_insert(ul_db_handle_t * handle, str * table, db_key_t* _k, db_val_t* _v, int _n); +int db_insert( + ul_db_handle_t *handle, str *table, db_key_t *_k, db_val_t *_v, int _n); #endif diff --git a/src/modules/p_usrloc/ul_db_ins_upd.c b/src/modules/p_usrloc/ul_db_ins_upd.c index 985e10827f3..ae81352e80c 100644 --- a/src/modules/p_usrloc/ul_db_ins_upd.c +++ b/src/modules/p_usrloc/ul_db_ins_upd.c @@ -22,7 +22,9 @@ #include "ul_db_ins_upd.h" #include "ul_db_form_query.h" -int db_insert_update(ul_db_handle_t * handle, - str * table, db_key_t* _k, db_val_t* _v, int _n) { - return db_submit_query(UL_DB_INS_UPD, handle, table, _k, NULL, _v, NULL, NULL, _n, 0); +int db_insert_update( + ul_db_handle_t *handle, str *table, db_key_t *_k, db_val_t *_v, int _n) +{ + return db_submit_query( + UL_DB_INS_UPD, handle, table, _k, NULL, _v, NULL, NULL, _n, 0); } diff --git a/src/modules/p_usrloc/ul_db_ins_upd.h b/src/modules/p_usrloc/ul_db_ins_upd.h index 01fc3615fdd..9facca7dbca 100644 --- a/src/modules/p_usrloc/ul_db_ins_upd.h +++ b/src/modules/p_usrloc/ul_db_ins_upd.h @@ -25,6 +25,7 @@ #include "../../lib/srdb1/db.h" #include "ul_db_handle.h" -int db_insert_update(ul_db_handle_t * handle, str * table, db_key_t* _k, db_val_t* _v, int _n); +int db_insert_update( + ul_db_handle_t *handle, str *table, db_key_t *_k, db_val_t *_v, int _n); #endif diff --git a/src/modules/p_usrloc/ul_db_layer.c b/src/modules/p_usrloc/ul_db_layer.c index 036adf23bb4..e17dbcb7b2c 100644 --- a/src/modules/p_usrloc/ul_db_layer.c +++ b/src/modules/p_usrloc/ul_db_layer.c @@ -4,26 +4,27 @@ #include "ul_db_layer.h" #include "ul_db_api.h" -typedef struct res_list { - db1_con_t ** h; - db1_res_t * r; - struct res_list * next; -} -res_list_t; +typedef struct res_list +{ + db1_con_t **h; + db1_res_t *r; + struct res_list *next; +} res_list_t; -static void add_res(db1_res_t * _r, db1_con_t ** _h); -static db1_con_t ** get_handle_by_res(db1_res_t * _r); -static void drop_res(db1_res_t * _r); +static void add_res(db1_res_t *_r, db1_con_t **_h); +static db1_con_t **get_handle_by_res(db1_res_t *_r); +static void drop_res(db1_res_t *_r); static ul_db_api_t p_ul_dbf; static db_func_t dbf; -static res_list_t * used = NULL; -static res_list_t * unused = NULL; +static res_list_t *used = NULL; +static res_list_t *unused = NULL; -static ul_domain_db_list_t * domain_db_list = NULL; +static ul_domain_db_list_t *domain_db_list = NULL; -int ul_db_layer_init(void) { +int ul_db_layer_init(void) +{ if(bind_ul_db(&p_ul_dbf) < 0) { LM_ERR("could not bind ul_db_api.\n"); return -1; @@ -35,8 +36,9 @@ int ul_db_layer_init(void) { return 0; } -void ul_db_layer_destroy(void) { - res_list_t * tmp, * del; +void ul_db_layer_destroy(void) +{ + res_list_t *tmp, *del; tmp = used; while(tmp) { del = tmp; @@ -52,23 +54,27 @@ void ul_db_layer_destroy(void) { return; } -int ul_db_layer_single_connect(udomain_t * domain, str * url) { - if((domain->dbh = dbf.init(url)) == NULL){ +int ul_db_layer_single_connect(udomain_t *domain, str *url) +{ + if((domain->dbh = dbf.init(url)) == NULL) { return -1; } return 1; } -int ul_db_layer_insert(udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_val_t* _v, int _n) { - ul_domain_db_t * d; +int ul_db_layer_insert(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_val_t *_v, int _n) +{ + ul_domain_db_t *d; switch(domain->dbt) { - case DB_TYPE_CLUSTER: return p_ul_dbf.insert(domain->name, user, sipdomain, _k, _v, _n); - case DB_TYPE_SINGLE: if(!domain->dbh){ - if((d = ul_find_domain(domain->name->s)) == 0){ + case DB_TYPE_CLUSTER: + return p_ul_dbf.insert(domain->name, user, sipdomain, _k, _v, _n); + case DB_TYPE_SINGLE: + if(!domain->dbh) { + if((d = ul_find_domain(domain->name->s)) == 0) { return -1; } - if(ul_db_layer_single_connect(domain, &d->url) < 0){ + if(ul_db_layer_single_connect(domain, &d->url) < 0) { return -1; } } @@ -76,20 +82,26 @@ int ul_db_layer_insert(udomain_t * domain, str * user, str * sipdomain, return -1; } return dbf.insert(domain->dbh, _k, _v, _n); - default: return -1; + default: + return -1; } } -int ul_db_layer_update(udomain_t * domain, str * user, str * sipdomain, db_key_t* _k, db_op_t* _o, db_val_t* _v, - db_key_t* _uk, db_val_t* _uv, int _n, int _un) { - ul_domain_db_t * d; +int ul_db_layer_update(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_op_t *_o, db_val_t *_v, db_key_t *_uk, db_val_t *_uv, + int _n, int _un) +{ + ul_domain_db_t *d; switch(domain->dbt) { - case DB_TYPE_CLUSTER: return p_ul_dbf.update(domain->name, user, sipdomain, _k, _o, _v, _uk, _uv, _n, _un); - case DB_TYPE_SINGLE: if(!domain->dbh){ - if((d = ul_find_domain(domain->name->s)) == 0){ + case DB_TYPE_CLUSTER: + return p_ul_dbf.update(domain->name, user, sipdomain, _k, _o, _v, + _uk, _uv, _n, _un); + case DB_TYPE_SINGLE: + if(!domain->dbh) { + if((d = ul_find_domain(domain->name->s)) == 0) { return -1; } - if(ul_db_layer_single_connect(domain, &d->url) < 0){ + if(ul_db_layer_single_connect(domain, &d->url) < 0) { return -1; } } @@ -97,93 +109,114 @@ int ul_db_layer_update(udomain_t * domain, str * user, str * sipdomain, db_key_t return -1; } return dbf.update(domain->dbh, _k, _o, _v, _uk, _uv, _n, _un); - default: return -1; + default: + return -1; } } -int ul_db_layer_replace (udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_val_t* _v, int _n, int _un) { - ul_domain_db_t * d; +int ul_db_layer_replace(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_val_t *_v, int _n, int _un) +{ + ul_domain_db_t *d; switch(domain->dbt) { - case DB_TYPE_CLUSTER: { return p_ul_dbf.replace(domain->name, user, sipdomain, _k, _v, _n, _un);;} - case DB_TYPE_SINGLE: if(!domain->dbh){ - if((d = ul_find_domain(domain->name->s)) == 0){ + case DB_TYPE_CLUSTER: { + return p_ul_dbf.replace( + domain->name, user, sipdomain, _k, _v, _n, _un); + ; + } + case DB_TYPE_SINGLE: + if(!domain->dbh) { + if((d = ul_find_domain(domain->name->s)) == 0) { return -1; } - if(ul_db_layer_single_connect(domain, &d->url) < 0){ + if(ul_db_layer_single_connect(domain, &d->url) < 0) { return -1; } } - if(dbf.use_table(domain->dbh, domain->name) < 0) { + if(dbf.use_table(domain->dbh, domain->name) < 0) { return -1; } return dbf.replace(domain->dbh, _k, _v, _n, _un, 0); - default: return -1; + default: + return -1; } } -int ul_db_layer_delete(udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { - ul_domain_db_t * d; +int ul_db_layer_delete(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_op_t *_o, db_val_t *_v, int _n) +{ + ul_domain_db_t *d; switch(domain->dbt) { - case DB_TYPE_CLUSTER: return p_ul_dbf.delete(domain->name, user, sipdomain, _k, _o, _v, _n); - case DB_TYPE_SINGLE: if(!domain->dbh){ - if((d = ul_find_domain(domain->name->s)) == 0){ + case DB_TYPE_CLUSTER: + return p_ul_dbf.delete( + domain->name, user, sipdomain, _k, _o, _v, _n); + case DB_TYPE_SINGLE: + if(!domain->dbh) { + if((d = ul_find_domain(domain->name->s)) == 0) { return -1; } - if(ul_db_layer_single_connect(domain, &d->url) < 0){ + if(ul_db_layer_single_connect(domain, &d->url) < 0) { return -1; } } - if(dbf.use_table(domain->dbh, domain->name) < 0) { + if(dbf.use_table(domain->dbh, domain->name) < 0) { return -1; } return dbf.delete(domain->dbh, _k, _o, _v, _n); - default: return -1; + default: + return -1; } } -int ul_db_layer_query(udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _c, - int _n, int _nc, db_key_t _o, db1_res_t** _r) { - ul_domain_db_t * d; - db1_con_t ** h; +int ul_db_layer_query(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_op_t *_op, db_val_t *_v, db_key_t *_c, int _n, int _nc, + db_key_t _o, db1_res_t **_r) +{ + ul_domain_db_t *d; + db1_con_t **h; int ret; switch(domain->dbt) { - case DB_TYPE_CLUSTER:{ - if((ret = p_ul_dbf.query(domain->name, user, sipdomain, &h, _k, _op, _v, _c, _n, _nc, _o, _r)) < 0 || (!_r)) { + case DB_TYPE_CLUSTER: { + if((ret = p_ul_dbf.query(domain->name, user, sipdomain, &h, _k, _op, + _v, _c, _n, _nc, _o, _r)) + < 0 + || (!_r)) { return -1; } add_res(*_r, h); return ret; - } - case DB_TYPE_SINGLE: if(!domain->dbh){ - if((d = ul_find_domain(domain->name->s)) == 0){ + } + case DB_TYPE_SINGLE: + if(!domain->dbh) { + if((d = ul_find_domain(domain->name->s)) == 0) { return -1; } - if(ul_db_layer_single_connect(domain, &d->url) < 0){ + if(ul_db_layer_single_connect(domain, &d->url) < 0) { return -1; } } - if(dbf.use_table(domain->dbh, domain->name) < 0) { + if(dbf.use_table(domain->dbh, domain->name) < 0) { return -1; } return dbf.query(domain->dbh, _k, _op, _v, _c, _n, _nc, _o, _r); - default: return -1; + default: + return -1; } } -int ul_db_layer_raw_query(udomain_t * domain, str* _s, db1_res_t** _r) { - ul_domain_db_t * d; +int ul_db_layer_raw_query(udomain_t *domain, str *_s, db1_res_t **_r) +{ + ul_domain_db_t *d; switch(domain->dbt) { - case DB_TYPE_CLUSTER: { - return -1; // not useful in this mode - } - case DB_TYPE_SINGLE: if(!domain->dbh){ - if((d = ul_find_domain(domain->name->s)) == 0){ + case DB_TYPE_CLUSTER: { + return -1; // not useful in this mode + } + case DB_TYPE_SINGLE: + if(!domain->dbh) { + if((d = ul_find_domain(domain->name->s)) == 0) { return -1; } - if(ul_db_layer_single_connect(domain, &d->url) < 0){ + if(ul_db_layer_single_connect(domain, &d->url) < 0) { return -1; } } @@ -191,35 +224,42 @@ int ul_db_layer_raw_query(udomain_t * domain, str* _s, db1_res_t** _r) { return -1; } return dbf.raw_query(domain->dbh, _s, _r); - default: return -1; + default: + return -1; } } -int ul_db_layer_free_result(udomain_t * domain, db1_res_t * res) { - db1_con_t ** h; +int ul_db_layer_free_result(udomain_t *domain, db1_res_t *res) +{ + db1_con_t **h; int ret; switch(domain->dbt) { - case DB_TYPE_CLUSTER: if((h = get_handle_by_res(res)) == NULL) { + case DB_TYPE_CLUSTER: + if((h = get_handle_by_res(res)) == NULL) { return -1; } ret = p_ul_dbf.free_result(h, res); drop_res(res); return ret; - case DB_TYPE_SINGLE: return dbf.free_result(domain->dbh, res); - default: return -1; + case DB_TYPE_SINGLE: + return dbf.free_result(domain->dbh, res); + default: + return -1; } } -int ul_add_domain_db(str * d, int t, str * url) { - ul_domain_db_list_t * new_d = NULL; +int ul_add_domain_db(str *d, int t, str *url) +{ + ul_domain_db_list_t *new_d = NULL; LM_DBG("%.*s, type: %s\n", d->len, d->s, - t==DB_TYPE_SINGLE ? "SINGLE" : "CLUSTER"); + t == DB_TYPE_SINGLE ? "SINGLE" : "CLUSTER"); if((new_d = pkg_malloc(sizeof(ul_domain_db_list_t))) == NULL) { return -1; } memset(new_d, 0, sizeof(ul_domain_db_list_t)); - if(!d || !d->s) goto error; + if(!d || !d->s) + goto error; if((new_d->domain.name.s = pkg_malloc(d->len + 1)) == NULL) { goto error; @@ -233,10 +273,11 @@ int ul_add_domain_db(str * d, int t, str * url) { } strncpy(new_d->domain.url.s, url->s, url->len); - new_d->domain.url.s[url->len] = '\0'; + new_d->domain.url.s[url->len] = '\0'; new_d->domain.url.len = url->len; } else { - if((new_d->domain.url.s = pkg_malloc(default_db_url.len + 1)) == NULL) { + if((new_d->domain.url.s = pkg_malloc(default_db_url.len + 1)) + == NULL) { goto error; } strcpy(new_d->domain.url.s, default_db_url.s); @@ -254,31 +295,35 @@ int ul_add_domain_db(str * d, int t, str * url) { return -1; } -ul_domain_db_t * ul_find_domain(const char * s) { - ul_domain_db_list_t * tmp; +ul_domain_db_t *ul_find_domain(const char *s) +{ + ul_domain_db_list_t *tmp; str d; - if(!domain_db_list){ - if(parse_domain_db(&domain_db) < 0){ + if(!domain_db_list) { + if(parse_domain_db(&domain_db) < 0) { LM_ERR("could not parse domain parameter.\n"); return NULL; } } tmp = domain_db_list; - while(tmp){ - LM_DBG("searched domain: %s, actual domain: %.*s, length: %i, type: %s\n", - s, tmp->domain.name.len, tmp->domain.name.s, tmp->domain.name.len, - tmp->domain.dbt==DB_TYPE_SINGLE ? "SINGLE" : "CLUSTER"); - if((strlen(s) == tmp->domain.name.len) && (memcmp(s, tmp->domain.name.s, tmp->domain.name.len) == 0)){ + while(tmp) { + LM_DBG("searched domain: %s, actual domain: %.*s, length: %i, type: " + "%s\n", + s, tmp->domain.name.len, tmp->domain.name.s, + tmp->domain.name.len, + tmp->domain.dbt == DB_TYPE_SINGLE ? "SINGLE" : "CLUSTER"); + if((strlen(s) == tmp->domain.name.len) + && (memcmp(s, tmp->domain.name.s, tmp->domain.name.len) == 0)) { return &tmp->domain; } tmp = tmp->next; } - if((d.s = pkg_malloc(strlen(s) + 1)) == NULL){ + if((d.s = pkg_malloc(strlen(s) + 1)) == NULL) { return NULL; } strcpy(d.s, s); d.len = strlen(s); - if(ul_add_domain_db(&d, default_dbt, &default_db_url)){ + if(ul_add_domain_db(&d, default_dbt, &default_db_url)) { pkg_free(d.s); return ul_find_domain(s); } @@ -286,14 +331,15 @@ ul_domain_db_t * ul_find_domain(const char * s) { return NULL; } -void free_all_udomains(void) { - ul_domain_db_list_t * tmp, * del; +void free_all_udomains(void) +{ + ul_domain_db_list_t *tmp, *del; tmp = domain_db_list; - while(tmp){ + while(tmp) { del = tmp; tmp = tmp->next; pkg_free(del->domain.name.s); - if(del->domain.dbt == DB_TYPE_SINGLE){ + if(del->domain.dbt == DB_TYPE_SINGLE) { pkg_free(del->domain.url.s); } pkg_free(del); @@ -301,18 +347,19 @@ void free_all_udomains(void) { return; } -int parse_domain_db(str * _d) { +int parse_domain_db(str *_d) +{ #define STATE_START 0 #define STATE_DOMAIN 1 #define STATE_TYPE 2 #define STATE_URL 3 - char * domains; - char * end; + char *domains; + char *end; str d = {NULL, 0}, u = {NULL, 0}, t = {NULL, 0}; int type = 0; int state; - if (_d->len==0) { + if(_d->len == 0) { return -1; } domains = _d->s; @@ -323,25 +370,33 @@ int parse_domain_db(str * _d) { switch(*domains) { case '=': switch(state) { - case STATE_START: return -1; - case STATE_DOMAIN: + case STATE_START: + return -1; + case STATE_DOMAIN: LM_DBG("found domain %.*s\n", d.len, d.s); state = STATE_TYPE; t.s = domains + 1; t.len = 0; break; - case STATE_TYPE: return -1; - case STATE_URL: return -1; - default: return -1; + case STATE_TYPE: + return -1; + case STATE_URL: + return -1; + default: + return -1; } break; case ';': switch(state) { - case STATE_START: return 1; - case STATE_DOMAIN: return -1; - case STATE_TYPE: + case STATE_START: + return 1; + case STATE_DOMAIN: + return -1; + case STATE_TYPE: LM_DBG("found type %.*s\n", t.len, t.s); - if(strncmp(t.s, DB_TYPE_CLUSTER_STR, strlen(DB_TYPE_CLUSTER_STR)) == 0) { + if(strncmp(t.s, DB_TYPE_CLUSTER_STR, + strlen(DB_TYPE_CLUSTER_STR)) + == 0) { type = DB_TYPE_CLUSTER; } else { type = DB_TYPE_SINGLE; @@ -350,18 +405,24 @@ int parse_domain_db(str * _d) { u.s = domains + 1; u.len = 0; break; - case STATE_URL: return -1; + case STATE_URL: + return -1; + break; + default: break; - default: break; } break; case ',': switch(state) { - case STATE_START: return -1; - case STATE_DOMAIN: return -1; - case STATE_TYPE: + case STATE_START: + return -1; + case STATE_DOMAIN: + return -1; + case STATE_TYPE: LM_DBG("found type %.*s\n", t.len, t.s); - if(strncmp(t.s, DB_TYPE_CLUSTER_STR, strlen(DB_TYPE_CLUSTER_STR)) == 0) { + if(strncmp(t.s, DB_TYPE_CLUSTER_STR, + strlen(DB_TYPE_CLUSTER_STR)) + == 0) { type = DB_TYPE_CLUSTER; } else { type = DB_TYPE_SINGLE; @@ -369,12 +430,13 @@ int parse_domain_db(str * _d) { ul_add_domain_db(&d, type, NULL); state = STATE_START; break; - case STATE_URL: + case STATE_URL: LM_DBG("found url %.*s\n", u.len, u.s); state = STATE_START; - ul_add_domain_db(&d, type, &u); + ul_add_domain_db(&d, type, &u); break; - default: return -1; + default: + return -1; } break; case '\0': @@ -385,7 +447,9 @@ int parse_domain_db(str * _d) { return -1; case STATE_TYPE: LM_DBG("found type %.*s\n", t.len, t.s); - if(strncmp(t.s, DB_TYPE_CLUSTER_STR, strlen(DB_TYPE_CLUSTER_STR)) == 0) { + if(strncmp(t.s, DB_TYPE_CLUSTER_STR, + strlen(DB_TYPE_CLUSTER_STR)) + == 0) { type = DB_TYPE_CLUSTER; } else { type = DB_TYPE_SINGLE; @@ -396,7 +460,8 @@ int parse_domain_db(str * _d) { LM_DBG("found url %.*s\n", u.len, u.s); ul_add_domain_db(&d, type, &u); return 1; - default: return -1; + default: + return -1; } break; default: @@ -415,7 +480,8 @@ int parse_domain_db(str * _d) { case STATE_URL: u.len++; break; - default: break; + default: + break; } break; } @@ -424,8 +490,9 @@ int parse_domain_db(str * _d) { return 1; } -static void add_res(db1_res_t * _r, db1_con_t ** _h) { - res_list_t * new_res; +static void add_res(db1_res_t *_r, db1_con_t **_h) +{ + res_list_t *new_res; if(!unused) { if((new_res = pkg_malloc(sizeof(res_list_t))) == NULL) { return; @@ -442,8 +509,9 @@ static void add_res(db1_res_t * _r, db1_con_t ** _h) { return; } -static db1_con_t ** get_handle_by_res(db1_res_t * _r) { - res_list_t * tmp = used; +static db1_con_t **get_handle_by_res(db1_res_t *_r) +{ + res_list_t *tmp = used; while(tmp) { if(_r == tmp->r) { return tmp->h; @@ -453,13 +521,14 @@ static db1_con_t ** get_handle_by_res(db1_res_t * _r) { return NULL; } -static void drop_res(db1_res_t * _r) { - res_list_t * prev = NULL; - res_list_t * tmp; +static void drop_res(db1_res_t *_r) +{ + res_list_t *prev = NULL; + res_list_t *tmp; tmp = used; while(tmp) { if(_r == tmp->r) { - if(prev==NULL) { + if(prev == NULL) { used = tmp->next; } else { prev->next = tmp->next; @@ -473,4 +542,3 @@ static void drop_res(db1_res_t * _r) { } return; } - diff --git a/src/modules/p_usrloc/ul_db_layer.h b/src/modules/p_usrloc/ul_db_layer.h index 47cc361f32b..5289ba4f9c6 100644 --- a/src/modules/p_usrloc/ul_db_layer.h +++ b/src/modules/p_usrloc/ul_db_layer.h @@ -1,4 +1,4 @@ -#ifndef UL_DB_LAYER_H +#ifndef UL_DB_LAYER_H #define UL_DB_LAYER_H #include "../../lib/srdb1/db.h" @@ -11,51 +11,53 @@ #define DB_TYPE_SINGLE 1 #define DB_TYPE_SINGLE_STR "single" -typedef struct ul_domain_db { +typedef struct ul_domain_db +{ str name; str url; int dbt; - struct ul_domain_db_list * next; + struct ul_domain_db_list *next; } ul_domain_db_t; -typedef struct ul_domain_db_list { +typedef struct ul_domain_db_list +{ ul_domain_db_t domain; - struct ul_domain_db_list * next; + struct ul_domain_db_list *next; } ul_domain_db_list_t; int ul_db_layer_init(); void ul_db_layer_destroy(); -int ul_db_layer_single_connect(udomain_t * domain, str * url); +int ul_db_layer_single_connect(udomain_t *domain, str *url); -int ul_db_layer_insert(udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_val_t* _v, int _n); +int ul_db_layer_insert(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_val_t *_v, int _n); -int ul_db_layer_update(udomain_t * domain, str * user, str * sipdomain, db_key_t* _k, db_op_t* _o, db_val_t* _v, - db_key_t* _uk, db_val_t* _uv, int _n, int _un); +int ul_db_layer_update(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_op_t *_o, db_val_t *_v, db_key_t *_uk, db_val_t *_uv, + int _n, int _un); -int ul_db_layer_replace(udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_val_t* _v, int _n, int _un); +int ul_db_layer_replace(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_val_t *_v, int _n, int _un); -int ul_db_layer_delete(udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n); +int ul_db_layer_delete(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_op_t *_o, db_val_t *_v, int _n); -int ul_db_layer_query(udomain_t * domain, str * user, str * sipdomain, - db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _c, - int _n, int _nc, db_key_t _o, db1_res_t** _r); +int ul_db_layer_query(udomain_t *domain, str *user, str *sipdomain, + db_key_t *_k, db_op_t *_op, db_val_t *_v, db_key_t *_c, int _n, int _nc, + db_key_t _o, db1_res_t **_r); -int ul_db_layer_raw_query(udomain_t * domain, str* _s, db1_res_t** _r); +int ul_db_layer_raw_query(udomain_t *domain, str *_s, db1_res_t **_r); -int ul_db_layer_fetch_result(udomain_t * domain, db1_res_t** _r, int _n); +int ul_db_layer_fetch_result(udomain_t *domain, db1_res_t **_r, int _n); -int ul_db_layer_free_result(udomain_t * domain, db1_res_t * res); +int ul_db_layer_free_result(udomain_t *domain, db1_res_t *res); -int ul_add_domain_db(str * d, int t, str * url); +int ul_add_domain_db(str *d, int t, str *url); -ul_domain_db_t * ul_find_domain(const char * s); +ul_domain_db_t *ul_find_domain(const char *s); -int parse_domain_db(str * _d); +int parse_domain_db(str *_d); #endif - diff --git a/src/modules/p_usrloc/ul_db_query.c b/src/modules/p_usrloc/ul_db_query.c index eff45a07e12..79cdb73d115 100644 --- a/src/modules/p_usrloc/ul_db_query.c +++ b/src/modules/p_usrloc/ul_db_query.c @@ -23,16 +23,16 @@ #include "ul_db.h" -static int order_dbs(ul_db_handle_t * handle, int order[]); +static int order_dbs(ul_db_handle_t *handle, int order[]); -static int db_exec_query(db_func_t * dbf, db1_con_t * dbh, str * table, - db_key_t* _k, db_op_t * _op, db_val_t * _v, - db_key_t * _c, int _n, int _nc, db_key_t _o, - db1_res_t ** _r); +static int db_exec_query(db_func_t *dbf, db1_con_t *dbh, str *table, + db_key_t *_k, db_op_t *_op, db_val_t *_v, db_key_t *_c, int _n, int _nc, + db_key_t _o, db1_res_t **_r); -int db_query(ul_db_handle_t * handle, db1_con_t *** _r_h, db_func_t ** _r_f, - str * table, db_key_t* _k, db_op_t * _op, db_val_t * _v, - db_key_t * _c, int _n, int _nc, db_key_t _o, db1_res_t ** _r, int rw) { +int db_query(ul_db_handle_t *handle, db1_con_t ***_r_h, db_func_t **_r_f, + str *table, db_key_t *_k, db_op_t *_op, db_val_t *_v, db_key_t *_c, + int _n, int _nc, db_key_t _o, db1_res_t **_r, int rw) +{ int order[DB_NUM]; int i; int err[DB_NUM]; @@ -44,18 +44,26 @@ int db_query(ul_db_handle_t * handle, db1_con_t *** _r_h, db_func_t ** _r_f, } order_dbs(handle, order); - memset(err, 0 , sizeof(int) * DB_NUM); + memset(err, 0, sizeof(int) * DB_NUM); i = 0; do { - LM_DBG("now trying id %i, db %i.\n", handle->id, handle->db[order[i]].no); + LM_DBG("now trying id %i, db %i.\n", handle->id, + handle->db[order[i]].no); if(handle->db[order[i]].status == DB_ON) { - if((ret = db_exec_query(&handle->db[order[i]].dbf, handle->db[order[i]].dbh, table, _k, _op, _v, _c, _n, _nc, _o, _r)) < 0) { - LM_ERR("could not query table %.*s error on id %i, db %i.\n", table->len, table->s, handle->id, handle->db[order[i]].no); + if((ret = db_exec_query(&handle->db[order[i]].dbf, + handle->db[order[i]].dbh, table, _k, _op, _v, _c, _n, + _nc, _o, _r)) + < 0) { + LM_ERR("could not query table %.*s error on id %i, db %i.\n", + table->len, table->s, handle->id, + handle->db[order[i]].no); if(rw) { if(err[i] == 0 && handle->db[order[i]].status == DB_ON) { - if(db_handle_error(handle, handle->db[order[i]].no) < 0) { - LM_ERR("could not handle error on id %i, db %i.\n", handle->id, handle->db[order[i]].no); + if(db_handle_error(handle, handle->db[order[i]].no) + < 0) { + LM_ERR("could not handle error on id %i, db %i.\n", + handle->id, handle->db[order[i]].no); } else { err[i] = 1; i--; @@ -67,24 +75,29 @@ int db_query(ul_db_handle_t * handle, db1_con_t *** _r_h, db_func_t ** _r_f, i++; } while((ret < 0) && (i < DB_NUM)); i--; - LM_DBG("returned handle is for id %i, db %i\n", handle->id, handle->db[order[i]].no); + LM_DBG("returned handle is for id %i, db %i\n", handle->id, + handle->db[order[i]].no); *_r_h = &handle->db[order[i]].dbh; *_r_f = &handle->db[order[i]].dbf; return ret; } -static int order_dbs(ul_db_handle_t * handle, int order[]) { - int i,j, tmp; - for(i=0; idb[i].status == DB_OFF || handle->db[i].status == DB_INACTIVE) && (handle->db[j].status == DB_ON)) { + for(i = 0; i < DB_NUM; i++) { + for(j = i + 1; j < DB_NUM; j++) { + if((handle->db[i].status == DB_OFF + || handle->db[i].status == DB_INACTIVE) + && (handle->db[j].status == DB_ON)) { tmp = order[i]; order[i] = order[j]; order[j] = tmp; - } else if(handle->db[i].failover_time > handle->db[j].failover_time) { + } else if(handle->db[i].failover_time + > handle->db[j].failover_time) { tmp = order[i]; order[i] = order[j]; order[j] = tmp; @@ -94,10 +107,10 @@ static int order_dbs(ul_db_handle_t * handle, int order[]) { return 0; } -static int db_exec_query(db_func_t * dbf, db1_con_t * dbh, str * table, - db_key_t* _k, db_op_t * _op, db_val_t * _v, - db_key_t * _c, int _n, int _nc, db_key_t _o, - db1_res_t ** _r) { +static int db_exec_query(db_func_t *dbf, db1_con_t *dbh, str *table, + db_key_t *_k, db_op_t *_op, db_val_t *_v, db_key_t *_c, int _n, int _nc, + db_key_t _o, db1_res_t **_r) +{ if(!dbf || !dbh || !table || !table->s) { LM_ERR("NULL pointer in parameter.\n"); return -1; diff --git a/src/modules/p_usrloc/ul_db_query.h b/src/modules/p_usrloc/ul_db_query.h index 789917d6d0a..536fbcb65a7 100644 --- a/src/modules/p_usrloc/ul_db_query.h +++ b/src/modules/p_usrloc/ul_db_query.h @@ -25,7 +25,8 @@ #include "../../lib/srdb1/db.h" #include "ul_db_handle.h" -int db_query(ul_db_handle_t * handle, db1_con_t *** _r_h, db_func_t ** _r_f, str * table, db_key_t* _k, db_op_t * _op, db_val_t * _v, - db_key_t * _c, int _n, int _nc, db_key_t _o, db1_res_t ** _r, int rw); +int db_query(ul_db_handle_t *handle, db1_con_t ***_r_h, db_func_t **_r_f, + str *table, db_key_t *_k, db_op_t *_op, db_val_t *_v, db_key_t *_c, + int _n, int _nc, db_key_t _o, db1_res_t **_r, int rw); #endif diff --git a/src/modules/p_usrloc/ul_db_repl.c b/src/modules/p_usrloc/ul_db_repl.c index 7f1329d5b51..96a25de7532 100644 --- a/src/modules/p_usrloc/ul_db_repl.c +++ b/src/modules/p_usrloc/ul_db_repl.c @@ -22,7 +22,9 @@ #include "ul_db_repl.h" #include "ul_db_form_query.h" -int db_replace(ul_db_handle_t * handle, str * table, - db_key_t* _k, db_val_t* _v, int _n, int _un) { - return db_submit_query(UL_DB_REPL, handle, table, _k, NULL, _v, NULL, NULL, _n, _un); +int db_replace(ul_db_handle_t *handle, str *table, db_key_t *_k, db_val_t *_v, + int _n, int _un) +{ + return db_submit_query( + UL_DB_REPL, handle, table, _k, NULL, _v, NULL, NULL, _n, _un); } diff --git a/src/modules/p_usrloc/ul_db_repl.h b/src/modules/p_usrloc/ul_db_repl.h index 443f3edcc5a..9cb6d906097 100644 --- a/src/modules/p_usrloc/ul_db_repl.h +++ b/src/modules/p_usrloc/ul_db_repl.h @@ -25,6 +25,7 @@ #include "../../lib/srdb1/db.h" #include "ul_db_handle.h" -int db_replace(ul_db_handle_t * handle, str * table, db_key_t* _k, db_val_t* _v, int _n, int _un); +int db_replace(ul_db_handle_t *handle, str *table, db_key_t *_k, db_val_t *_v, + int _n, int _un); #endif diff --git a/src/modules/p_usrloc/ul_db_tran.c b/src/modules/p_usrloc/ul_db_tran.c index 4e3bf99b250..17b5e1e6d47 100644 --- a/src/modules/p_usrloc/ul_db_tran.c +++ b/src/modules/p_usrloc/ul_db_tran.c @@ -29,11 +29,12 @@ static str commit = str_init("COMMIT"); static str autocommit_on = str_init("SET AUTOCOMMIT=1"); static str rollback = str_init("ROLLBACK"); -static int submit_tran_start(db_func_t * dbf, db1_con_t * dbh); -static int submit_tran_commit(db_func_t * dbf, db1_con_t * dbh); -static int submit_tran_rollback(db_func_t * dbf, db1_con_t * dbh); +static int submit_tran_start(db_func_t *dbf, db1_con_t *dbh); +static int submit_tran_commit(db_func_t *dbf, db1_con_t *dbh); +static int submit_tran_rollback(db_func_t *dbf, db1_con_t *dbh); -int ul_db_tran_start(ul_db_handle_t * handle, int working[]) { +int ul_db_tran_start(ul_db_handle_t *handle, int working[]) +{ int i; int errors = 0; int w = 0; @@ -43,19 +44,23 @@ int ul_db_tran_start(ul_db_handle_t * handle, int working[]) { return -1; } - for(i=0; idb[i].status == DB_ON) { if(submit_tran_start(&handle->db[i].dbf, handle->db[i].dbh) < 0) { LM_ERR("error while starting " - "transaction on id %i, db %i.\n", handle->id, handle->db[i].no); + "transaction on id %i, db %i.\n", + handle->id, handle->db[i].no); if(db_handle_error(handle, handle->db[i].no) < 0) { LM_ERR("error during handling error " - "on id %i on db %i, trying again.\n", handle->id, handle->db[i].no); + "on id %i on db %i, trying again.\n", + handle->id, handle->db[i].no); errors++; } else { - if(submit_tran_start(&handle->db[i].dbf, handle->db[i].dbh) < 0) { + if(submit_tran_start(&handle->db[i].dbf, handle->db[i].dbh) + < 0) { LM_ERR("error while starting " - "transaction on id %i, db %i.\n", handle->id, handle->db[i].no); + "transaction on id %i, db %i.\n", + handle->id, handle->db[i].no); errors++; } } @@ -72,7 +77,8 @@ int ul_db_tran_start(ul_db_handle_t * handle, int working[]) { return 0; } -int ul_db_tran_commit(ul_db_handle_t * handle, int working[]) { +int ul_db_tran_commit(ul_db_handle_t *handle, int working[]) +{ int i; int errors = 0; int w = 0; @@ -82,14 +88,16 @@ int ul_db_tran_commit(ul_db_handle_t * handle, int working[]) { return -1; } - for(i=0; idb[i].status == DB_ON) && (working[i])) { if(submit_tran_commit(&handle->db[i].dbf, handle->db[i].dbh) < 0) { LM_ERR("error while committing " - "transaction on id %i, db %i.\n", handle->id, handle->db[i].no); + "transaction on id %i, db %i.\n", + handle->id, handle->db[i].no); if(db_handle_error(handle, handle->db[i].no) < 0) { LM_ERR("error during handling error " - "on id %i on db %i, trying again.\n", handle->id, handle->db[i].no); + "on id %i on db %i, trying again.\n", + handle->id, handle->db[i].no); } errors++; } else { @@ -103,7 +111,8 @@ int ul_db_tran_commit(ul_db_handle_t * handle, int working[]) { return 0; } -int ul_db_tran_rollback(ul_db_handle_t * handle, int working[]) { +int ul_db_tran_rollback(ul_db_handle_t *handle, int working[]) +{ int i; int errors = 0; int w = 0; @@ -113,11 +122,13 @@ int ul_db_tran_rollback(ul_db_handle_t * handle, int working[]) { return -1; } - for(i=0; idb[i].status == DB_ON) && (working[i])) { - if(submit_tran_rollback(&handle->db[i].dbf, handle->db[i].dbh) < 0) { + if(submit_tran_rollback(&handle->db[i].dbf, handle->db[i].dbh) + < 0) { LM_ERR("error while rolling back " - "transaction on id %i, db %i.\n", handle->id, handle->db[i].no); + "transaction on id %i, db %i.\n", + handle->id, handle->db[i].no); errors++; } else { w++; @@ -130,25 +141,26 @@ int ul_db_tran_rollback(ul_db_handle_t * handle, int working[]) { return 0; } -static int submit_tran_start(db_func_t * dbf, db1_con_t * dbh) { +static int submit_tran_start(db_func_t *dbf, db1_con_t *dbh) +{ int errors = 0; str tmp; if(dbh) { if(dbf->raw_query(dbh, &autocommit_off, NULL) < 0) { LM_ERR("error while turning off " - "autocommit.\n"); + "autocommit.\n"); errors++; } - tmp.s = isolation_level; + tmp.s = isolation_level; tmp.len = strlen(isolation_level); if(dbf->raw_query(dbh, &tmp, NULL) < 0) { LM_ERR("error while setting " - "isolation level.\n"); + "isolation level.\n"); errors++; } if(dbf->raw_query(dbh, &start_transaction, NULL) < 0) { LM_ERR("error while starting " - "transaction.\n"); + "transaction.\n"); errors++; } } else { @@ -161,7 +173,8 @@ static int submit_tran_start(db_func_t * dbf, db1_con_t * dbh) { return 0; } -static int submit_tran_commit(db_func_t * dbf, db1_con_t * dbh) { +static int submit_tran_commit(db_func_t *dbf, db1_con_t *dbh) +{ int errors = 0; if(dbh) { @@ -171,7 +184,7 @@ static int submit_tran_commit(db_func_t * dbf, db1_con_t * dbh) { } if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) { LM_ERR("error while turning " - "on autocommit.\n"); + "on autocommit.\n"); errors++; } } else { @@ -185,18 +198,19 @@ static int submit_tran_commit(db_func_t * dbf, db1_con_t * dbh) { return 0; } -static int submit_tran_rollback(db_func_t * dbf, db1_con_t * dbh) { +static int submit_tran_rollback(db_func_t *dbf, db1_con_t *dbh) +{ int errors = 0; if(dbh) { if(dbf->raw_query(dbh, &rollback, NULL) < 0) { LM_ERR("error during " - "rollback.\n"); + "rollback.\n"); errors++; } if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) { LM_ERR("error while " - "turning on autocommit.\n"); + "turning on autocommit.\n"); errors++; } } else { @@ -210,13 +224,14 @@ static int submit_tran_rollback(db_func_t * dbf, db1_con_t * dbh) { return 0; } -int get_working_sum(int working[], int no) { +int get_working_sum(int working[], int no) +{ int i; int sum = 0; if(!working) { return -1; } - for(i=0; inext; shm_free(del); @@ -82,7 +87,7 @@ void ul_db_watch_destroy(void){ shm_free(list); list = NULL; } - while(handles){ + while(handles) { del2 = handles; handles = handles->next; pkg_free(del2); @@ -90,35 +95,37 @@ void ul_db_watch_destroy(void){ return; } -void check_dbs(unsigned int ticks, void *param){ +void check_dbs(unsigned int ticks, void *param) +{ LM_DBG("check availability of databases\n"); - ul_db_watch_list_t * tmp; - ul_db_handle_list_t * tmp2, * new_element; + ul_db_watch_list_t *tmp; + ul_db_handle_list_t *tmp2, *new_element; int found; int i; - if (db_master_write && mdb_availability_control) { + if(db_master_write && mdb_availability_control) { check_master_db(); } - if(!list_lock){ + if(!list_lock) { return; } - lock_get(list_lock); + lock_get(list_lock); tmp = *list; - while(tmp){ + while(tmp) { tmp2 = handles; found = 0; - while(tmp2){ - if(tmp2->handle->id == tmp->id){ + while(tmp2) { + if(tmp2->handle->id == tmp->id) { found = 1; - if(tmp->active){ + if(tmp->active) { LM_INFO("handle %i found, check it\n", tmp->id); tmp2->handle->active = 1; ul_db_check(tmp2->handle); - } else if (tmp2->handle->active) { - for(i=0; ihandle->db[i].dbh){ - tmp2->handle->db[i].dbf.close(tmp2->handle->db[i].dbh); + } else if(tmp2->handle->active) { + for(i = 0; i < DB_NUM; i++) { + if(tmp2->handle->db[i].dbh) { + tmp2->handle->db[i].dbf.close( + tmp2->handle->db[i].dbh); tmp2->handle->db[i].dbh = NULL; } } @@ -127,15 +134,17 @@ void check_dbs(unsigned int ticks, void *param){ } tmp2 = tmp2->next; } - if(!found){ + if(!found) { LM_NOTICE("handle %i not found, create it\n", tmp->id); - if((new_element = pkg_malloc(sizeof(ul_db_handle_list_t))) == NULL){ + if((new_element = pkg_malloc(sizeof(ul_db_handle_list_t))) + == NULL) { LM_ERR("couldn't allocate private memory\n"); lock_release(list_lock); return; } memset(new_element, 0, sizeof(ul_db_handle_list_t)); - if((new_element->handle = pkg_malloc(sizeof(ul_db_handle_t))) == NULL){ + if((new_element->handle = pkg_malloc(sizeof(ul_db_handle_t))) + == NULL) { LM_ERR("couldn't allocate private memory\n"); pkg_free(new_element); lock_release(list_lock); @@ -152,13 +161,14 @@ void check_dbs(unsigned int ticks, void *param){ lock_release(list_lock); } -void check_master_db() { - if(mdb.write.dbh){ +void check_master_db() +{ + if(mdb.write.dbh) { mdb.write.dbf.close(mdb.write.dbh); mdb.write.dbh = NULL; } - if((mdb.write.dbh = mdb.write.dbf.init(mdb.write.url)) == NULL) { + if((mdb.write.dbh = mdb.write.dbf.init(mdb.write.url)) == NULL) { LM_INFO("Master db is unavailable.\n"); *mdb_w_available = 0; } else { @@ -167,24 +177,25 @@ void check_master_db() { } } -int ul_register_watch_db(int id){ - ul_db_watch_list_t * new_id = NULL, * tmp; - if(!list_lock){ - if(init_watch_db_list() < 0){ +int ul_register_watch_db(int id) +{ + ul_db_watch_list_t *new_id = NULL, *tmp; + if(!list_lock) { + if(init_watch_db_list() < 0) { return -1; } } lock_get(list_lock); tmp = *list; - while(tmp){ - if(tmp->id == id){ + while(tmp) { + if(tmp->id == id) { tmp->active = 1; lock_release(list_lock); return 0; } tmp = tmp->next; } - if((new_id = shm_malloc(sizeof(ul_db_watch_list_t))) == NULL){ + if((new_id = shm_malloc(sizeof(ul_db_watch_list_t))) == NULL) { LM_ERR("couldn't allocate shared memory\n"); lock_release(list_lock); return -1; @@ -198,15 +209,16 @@ int ul_register_watch_db(int id){ return 0; } -int ul_unregister_watch_db(int id){ - ul_db_watch_list_t * tmp; - if(!list_lock){ +int ul_unregister_watch_db(int id) +{ + ul_db_watch_list_t *tmp; + if(!list_lock) { return 0; } lock_get(list_lock); tmp = *list; - while(tmp){ - if(tmp->id == id){ + while(tmp) { + if(tmp->id == id) { tmp->active = 0; lock_release(list_lock); return 0; @@ -217,12 +229,13 @@ int ul_unregister_watch_db(int id){ return 0; } -static int init_watch_db_list(void){ - if((list_lock = lock_alloc()) == NULL){ +static int init_watch_db_list(void) +{ + if((list_lock = lock_alloc()) == NULL) { LM_ERR("could not allocate lock\n"); return -1; } - if(lock_init(list_lock) == NULL){ + if(lock_init(list_lock) == NULL) { LM_ERR("could not initialise lock\n"); return -1; } diff --git a/src/modules/p_usrloc/ul_mi.c b/src/modules/p_usrloc/ul_mi.c index 999d6ee416d..6f6d2a98386 100644 --- a/src/modules/p_usrloc/ul_mi.c +++ b/src/modules/p_usrloc/ul_mi.c @@ -28,11 +28,11 @@ #ifdef MI_REMOVED -#define MI_USRLOC_RM "ul_rm" -#define MI_USRLOC_RM_CONTACT "ul_rm_contact" -#define MI_USRLOC_DUMP "ul_dump" -#define MI_USRLOC_FLUSH "ul_flush" -#define MI_USRLOC_ADD "ul_add" +#define MI_USRLOC_RM "ul_rm" +#define MI_USRLOC_RM_CONTACT "ul_rm_contact" +#define MI_USRLOC_DUMP "ul_dump" +#define MI_USRLOC_FLUSH "ul_flush" +#define MI_USRLOC_ADD "ul_add" #define MI_USRLOC_SHOW_CONTACT "ul_show_contact" #include @@ -53,7 +53,7 @@ /*! call-id used for ul_add and ul_rm_contact */ static str mi_ul_cid = str_init("dfjrewr12386fd6-343@Kamailio.mi"); /*! user agent used for ul_add */ -static str mi_ul_ua = str_init("Kamailio MI Server"); +static str mi_ul_ua = str_init("Kamailio MI Server"); static str mi_ul_path = str_init("dummypath"); @@ -64,7 +64,7 @@ static str mi_ul_path = str_init("dummypath"); * \param table domain (table) name * \return pointer to domain if found, 0 if not found */ -static inline udomain_t* mi_find_domain(str* table) +static inline udomain_t *mi_find_domain(str *table) { LM_ERR("not available in p_usrloc mode\n"); @@ -84,12 +84,12 @@ static inline int mi_fix_aor(str *aor) { char *p; - p = memchr( aor->s, '@', aor->len); - if (use_domain) { - if (p==NULL) + p = memchr(aor->s, '@', aor->len); + if(use_domain) { + if(p == NULL) return -1; } else { - if (p) + if(p) aor->len = p - aor->s; } strlower(aor); @@ -106,20 +106,21 @@ static inline int mi_fix_aor(str *aor) * \param short_dump 0 means that all informations will be included, 1 that only the AOR is printed * \return 0 on success, -1 on failure */ -static inline int mi_add_aor_node(struct mi_node *parent, urecord_t* r, time_t t, int short_dump) +static inline int mi_add_aor_node( + struct mi_node *parent, urecord_t *r, time_t t, int short_dump) { struct mi_node *anode, *cnode, *node; struct mi_attr *attr; - ucontact_t* c; + ucontact_t *c; char *p; int len; - anode = add_mi_node_child( parent, MI_DUP_VALUE, "AOR", 3, - r->aor.s, r->aor.len); - if (anode==0) + anode = add_mi_node_child( + parent, MI_DUP_VALUE, "AOR", 3, r->aor.s, r->aor.len); + if(anode == 0) return -1; - if (short_dump) + if(short_dump) return 0; #if 0 @@ -130,128 +131,128 @@ static inline int mi_add_aor_node(struct mi_node *parent, urecord_t* r, time_t t return -1; #endif - for( c=r->contacts ; c ; c=c->next) { + for(c = r->contacts; c; c = c->next) { /* contact */ - cnode = add_mi_node_child( anode, MI_DUP_VALUE, "Contact", 7, - c->c.s, c->c.len); - if (cnode==0) + cnode = add_mi_node_child( + anode, MI_DUP_VALUE, "Contact", 7, c->c.s, c->c.len); + if(cnode == 0) return -1; /* expires */ - if (c->expires == 0) { - node = add_mi_node_child( cnode, 0, "Expires", 7, "permanent", 9); - } else if (c->expires == UL_EXPIRED_TIME) { - node = add_mi_node_child( cnode, 0, "Expires", 7, "deleted", 7); - } else if (t > c->expires) { - node = add_mi_node_child( cnode, 0, "Expires", 7, "expired", 7); + if(c->expires == 0) { + node = add_mi_node_child(cnode, 0, "Expires", 7, "permanent", 9); + } else if(c->expires == UL_EXPIRED_TIME) { + node = add_mi_node_child(cnode, 0, "Expires", 7, "deleted", 7); + } else if(t > c->expires) { + node = add_mi_node_child(cnode, 0, "Expires", 7, "expired", 7); } else { p = int2str((unsigned long)(c->expires - t), &len); - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Expires", 7,p,len); + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Expires", 7, p, len); } - if (node==0) + if(node == 0) return -1; /* q */ - p = q2str(c->q, (unsigned int*)&len); - attr = add_mi_attr( cnode, MI_DUP_VALUE, "Q", 1, p, len); - if (attr==0) + p = q2str(c->q, (unsigned int *)&len); + attr = add_mi_attr(cnode, MI_DUP_VALUE, "Q", 1, p, len); + if(attr == 0) return -1; /* callid */ - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Callid", 6, - c->callid.s, c->callid.len); - if (node==0) + node = add_mi_node_child( + cnode, MI_DUP_VALUE, "Callid", 6, c->callid.s, c->callid.len); + if(node == 0) return -1; /* cseq */ p = int2str((unsigned long)c->cseq, &len); - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Cseq", 4, p, len); - if (node==0) + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Cseq", 4, p, len); + if(node == 0) return -1; /* User-Agent */ - if (c->user_agent.len) { - node = add_mi_node_child( cnode, MI_DUP_VALUE, "User-agent", 10, - c->user_agent.s, c->user_agent.len); - if (node==0) + if(c->user_agent.len) { + node = add_mi_node_child(cnode, MI_DUP_VALUE, "User-agent", 10, + c->user_agent.s, c->user_agent.len); + if(node == 0) return -1; } /* received */ - if (c->received.len) { - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Received", 8, - c->received.s, c->received.len); - if (node==0) + if(c->received.len) { + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Received", 8, + c->received.s, c->received.len); + if(node == 0) return -1; } /* path */ - if (c->path.len) { - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Path", 4, - c->path.s, c->path.len); - if (node==0) + if(c->path.len) { + node = add_mi_node_child( + cnode, MI_DUP_VALUE, "Path", 4, c->path.s, c->path.len); + if(node == 0) return -1; } /* state */ - if (c->state == CS_NEW) { - node = add_mi_node_child( cnode, 0, "State", 5, "CS_NEW", 6); - } else if (c->state == CS_SYNC) { - node = add_mi_node_child( cnode, 0, "State", 5, "CS_SYNC", 7); - } else if (c->state== CS_DIRTY) { - node = add_mi_node_child( cnode, 0, "State", 5, "CS_DIRTY", 8); + if(c->state == CS_NEW) { + node = add_mi_node_child(cnode, 0, "State", 5, "CS_NEW", 6); + } else if(c->state == CS_SYNC) { + node = add_mi_node_child(cnode, 0, "State", 5, "CS_SYNC", 7); + } else if(c->state == CS_DIRTY) { + node = add_mi_node_child(cnode, 0, "State", 5, "CS_DIRTY", 8); } else { - node = add_mi_node_child( cnode, 0, "State", 5, "CS_UNKNOWN", 10); + node = add_mi_node_child(cnode, 0, "State", 5, "CS_UNKNOWN", 10); } - if (node==0) + if(node == 0) return -1; /* flags */ p = int2str((unsigned long)c->flags, &len); - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Flags", 5, p, len); - if (node==0) + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Flags", 5, p, len); + if(node == 0) return -1; /* cflags */ p = int2str((unsigned long)c->cflags, &len); - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Cflags", 5, p, len); - if (node==0) + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Cflags", 5, p, len); + if(node == 0) return -1; /* socket */ - if (c->sock) { - node = add_mi_node_child( cnode, 0, "Socket", 6, - c->sock->sock_str.s, c->sock->sock_str.len); - if (node==0) + if(c->sock) { + node = add_mi_node_child(cnode, 0, "Socket", 6, c->sock->sock_str.s, + c->sock->sock_str.len); + if(node == 0) return -1; } /* methods */ p = int2str((unsigned long)c->methods, &len); - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Methods", 7, p, len); - if (node==0) + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Methods", 7, p, len); + if(node == 0) return -1; /* ruid */ - if (c->ruid.len) { - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Ruid", 4, - c->ruid.s, c->ruid.len); - if (node==0) + if(c->ruid.len) { + node = add_mi_node_child( + cnode, MI_DUP_VALUE, "Ruid", 4, c->ruid.s, c->ruid.len); + if(node == 0) return -1; } /* instance */ - if (c->instance.len) { - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Instance", 8, - c->instance.s, c->instance.len); - if (node==0) + if(c->instance.len) { + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Instance", 8, + c->instance.s, c->instance.len); + if(node == 0) return -1; } /* reg-id */ p = int2str((unsigned long)c->reg_id, &len); - node = add_mi_node_child( cnode, MI_DUP_VALUE, "Reg-Id", 6, p, len); - if (node==0) + node = add_mi_node_child(cnode, MI_DUP_VALUE, "Reg-Id", 6, p, len); + if(node == 0) return -1; } /* for */ @@ -269,34 +270,34 @@ static inline int mi_add_aor_node(struct mi_node *parent, urecord_t* r, time_t t * \note expects 2 nodes: the table name and the AOR * \return mi_root with the result */ -struct mi_root* mi_usrloc_rm_aor(struct mi_root *cmd, void *param) +struct mi_root *mi_usrloc_rm_aor(struct mi_root *cmd, void *param) { struct mi_node *node; udomain_t *dom; str *aor; node = cmd->node.kids; - if (node==NULL || node->next==NULL || node->next->next!=NULL) - return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); + if(node == NULL || node->next == NULL || node->next->next != NULL) + return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); /* look for table */ - dom = mi_find_domain( &node->value ); - if (dom==NULL) - return init_mi_tree( 404, "Table not found", 15); + dom = mi_find_domain(&node->value); + if(dom == NULL) + return init_mi_tree(404, "Table not found", 15); /* process the aor */ aor = &node->next->value; - if ( mi_fix_aor(aor)!=0 ) - return init_mi_tree( 400, "Domain missing in AOR", 21); + if(mi_fix_aor(aor) != 0) + return init_mi_tree(400, "Domain missing in AOR", 21); - lock_udomain( dom, aor); - if (delete_urecord( dom, aor, 0) < 0) { - unlock_udomain( dom, aor); - return init_mi_tree( 500, "Failed to delete AOR", 20); + lock_udomain(dom, aor); + if(delete_urecord(dom, aor, 0) < 0) { + unlock_udomain(dom, aor); + return init_mi_tree(500, "Failed to delete AOR", 20); } - unlock_udomain( dom, aor); - return init_mi_tree( 200, MI_OK_S, MI_OK_LEN); + unlock_udomain(dom, aor); + return init_mi_tree(200, MI_OK_S, MI_OK_LEN); } @@ -307,57 +308,58 @@ struct mi_root* mi_usrloc_rm_aor(struct mi_root *cmd, void *param) * \note expects 3 nodes: the table name, the AOR and contact * \return mi_root with the result or 0 on failure */ -struct mi_root* mi_usrloc_rm_contact(struct mi_root *cmd, void *param) +struct mi_root *mi_usrloc_rm_contact(struct mi_root *cmd, void *param) { struct mi_node *node; udomain_t *dom; urecord_t *rec; - ucontact_t* con; + ucontact_t *con; str *aor, *contact; int ret; node = cmd->node.kids; - if (node==NULL || node->next==NULL || node->next->next==NULL || - node->next->next->next!=NULL) - return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); + if(node == NULL || node->next == NULL || node->next->next == NULL + || node->next->next->next != NULL) + return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); /* look for table */ - dom = mi_find_domain( &node->value ); - if (dom==NULL) - return init_mi_tree( 404, "Table not found", 15); + dom = mi_find_domain(&node->value); + if(dom == NULL) + return init_mi_tree(404, "Table not found", 15); /* process the aor */ aor = &node->next->value; - if ( mi_fix_aor(aor)!=0 ) - return init_mi_tree( 400, "Domain missing in AOR", 21); + if(mi_fix_aor(aor) != 0) + return init_mi_tree(400, "Domain missing in AOR", 21); - lock_udomain( dom, aor); + lock_udomain(dom, aor); - ret = get_urecord( dom, aor, &rec); - if (ret == 1) { - unlock_udomain( dom, aor); - return init_mi_tree( 404, "AOR not found", 13); + ret = get_urecord(dom, aor, &rec); + if(ret == 1) { + unlock_udomain(dom, aor); + return init_mi_tree(404, "AOR not found", 13); } contact = &node->next->next->value; - ret = get_ucontact( rec, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ+1, &con); - if (ret < 0) { - unlock_udomain( dom, aor); + ret = get_ucontact( + rec, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ + 1, &con); + if(ret < 0) { + unlock_udomain(dom, aor); return 0; } - if (ret > 0) { - unlock_udomain( dom, aor); - return init_mi_tree( 404, "Contact not found", 17); + if(ret > 0) { + unlock_udomain(dom, aor); + return init_mi_tree(404, "Contact not found", 17); } - if (delete_ucontact(rec, con) < 0) { - unlock_udomain( dom, aor); + if(delete_ucontact(rec, con) < 0) { + unlock_udomain(dom, aor); return 0; } release_urecord(rec); - unlock_udomain( dom, aor); - return init_mi_tree( 200, MI_OK_S, MI_OK_LEN); + unlock_udomain(dom, aor); + return init_mi_tree(200, MI_OK_S, MI_OK_LEN); } @@ -367,7 +369,7 @@ struct mi_root* mi_usrloc_rm_contact(struct mi_root *cmd, void *param) * \param param not used * \return mi_root with the result or 0 on failure */ -struct mi_root* mi_usrloc_dump(struct mi_root *cmd, void *param) +struct mi_root *mi_usrloc_dump(struct mi_root *cmd, void *param) { LM_ERR("not available in p_usrloc mode\n"); return 0; @@ -380,12 +382,12 @@ struct mi_root* mi_usrloc_dump(struct mi_root *cmd, void *param) * \param param not used * \return mi_root with the result or 0 on failure */ -struct mi_root* mi_usrloc_flush(struct mi_root *cmd, void *param) +struct mi_root *mi_usrloc_flush(struct mi_root *cmd, void *param) { struct mi_root *rpl_tree; - rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN); - if (rpl_tree==NULL) + rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN); + if(rpl_tree == NULL) return 0; synchronize_all_udomains(); @@ -401,49 +403,50 @@ struct mi_root* mi_usrloc_flush(struct mi_root *cmd, void *param) * useless - backward compatible, flags, cflags, methods * \return mi_root with the result */ -struct mi_root* mi_usrloc_add(struct mi_root *cmd, void *param) +struct mi_root *mi_usrloc_add(struct mi_root *cmd, void *param) { ucontact_info_t ci; - urecord_t* r; - ucontact_t* c; + urecord_t *r; + ucontact_t *c; struct mi_node *node; udomain_t *dom; str *aor, *contact; unsigned int ui_val; int n; - for( n=0,node = cmd->node.kids; n<9 && node ; n++,node=node->next ); - if (n!=9 || node!=0) - return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); + for(n = 0, node = cmd->node.kids; n < 9 && node; n++, node = node->next) + ; + if(n != 9 || node != 0) + return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); node = cmd->node.kids; /* look for table (param 1) */ - dom = mi_find_domain( &node->value ); - if (dom==NULL) - return init_mi_tree( 404, "Table not found", 15); + dom = mi_find_domain(&node->value); + if(dom == NULL) + return init_mi_tree(404, "Table not found", 15); /* process the aor (param 2) */ node = node->next; aor = &node->value; - if ( mi_fix_aor(aor)!=0 ) - return init_mi_tree( 400, "Domain missing in AOR", 21); + if(mi_fix_aor(aor) != 0) + return init_mi_tree(400, "Domain missing in AOR", 21); /* contact (param 3) */ node = node->next; contact = &node->value; - memset( &ci, 0, sizeof(ucontact_info_t)); + memset(&ci, 0, sizeof(ucontact_info_t)); /* expire (param 4) */ node = node->next; - if (str2int( &node->value, &ui_val) < 0) + if(str2int(&node->value, &ui_val) < 0) goto bad_syntax; ci.expires = ui_val; /* q value (param 5) */ node = node->next; - if (str2q( &ci.q, node->value.s, node->value.len) < 0) + if(str2q(&ci.q, node->value.s, node->value.len) < 0) goto bad_syntax; /* unused value (param 6) FIXME */ @@ -451,28 +454,29 @@ struct mi_root* mi_usrloc_add(struct mi_root *cmd, void *param) /* flags value (param 7) */ node = node->next; - if (str2int( &node->value, (unsigned int*)&ci.flags) < 0) + if(str2int(&node->value, (unsigned int *)&ci.flags) < 0) goto bad_syntax; /* branch flags value (param 8) */ node = node->next; - if (str2int( &node->value, (unsigned int*)&ci.cflags) < 0) + if(str2int(&node->value, (unsigned int *)&ci.cflags) < 0) goto bad_syntax; /* methods value (param 9) */ node = node->next; - if (str2int( &node->value, (unsigned int*)&ci.methods) < 0) + if(str2int(&node->value, (unsigned int *)&ci.methods) < 0) goto bad_syntax; - lock_udomain( dom, aor); + lock_udomain(dom, aor); - n = get_urecord( dom, aor, &r); - if ( n==1) { - if (insert_urecord( dom, aor, &r) < 0) + n = get_urecord(dom, aor, &r); + if(n == 1) { + if(insert_urecord(dom, aor, &r) < 0) goto lock_error; c = 0; } else { - if (get_ucontact( r, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ+1, &c) < 0) + if(get_ucontact(r, contact, &mi_ul_cid, &mi_ul_path, MI_UL_CSEQ + 1, &c) + < 0) goto lock_error; } @@ -482,29 +486,29 @@ struct mi_root* mi_usrloc_add(struct mi_root *cmd, void *param) ci.user_agent = &mi_ul_ua; ci.cseq = MI_UL_CSEQ; /* 0 expires means permanent contact */ - if (ci.expires!=0) + if(ci.expires != 0) ci.expires += act_time; - if (c) { - if (update_ucontact( r, c, &ci) < 0) + if(c) { + if(update_ucontact(r, c, &ci) < 0) goto release_error; } else { - if ( insert_ucontact( r, contact, &ci, &c) < 0 ) + if(insert_ucontact(r, contact, &ci, &c) < 0) goto release_error; } release_urecord(r); - unlock_udomain( dom, aor); + unlock_udomain(dom, aor); - return init_mi_tree( 200, MI_OK_S, MI_OK_LEN); + return init_mi_tree(200, MI_OK_S, MI_OK_LEN); bad_syntax: - return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN); + return init_mi_tree(400, MI_BAD_PARM_S, MI_BAD_PARM_LEN); release_error: release_urecord(r); lock_error: - unlock_udomain( dom, aor); - return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN); + unlock_udomain(dom, aor); + return init_mi_tree(500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN); } @@ -515,85 +519,84 @@ struct mi_root* mi_usrloc_add(struct mi_root *cmd, void *param) * \note expects 2 nodes: the table name and the AOR * \return mi_root with the result or 0 on failure */ -struct mi_root* mi_usrloc_show_contact(struct mi_root *cmd, void *param) +struct mi_root *mi_usrloc_show_contact(struct mi_root *cmd, void *param) { struct mi_root *rpl_tree; struct mi_node *rpl, *node; udomain_t *dom; urecord_t *rec; - ucontact_t* con; + ucontact_t *con; str *aor; int ret; node = cmd->node.kids; - if (node==NULL || node->next==NULL || node->next->next!=NULL) - return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); + if(node == NULL || node->next == NULL || node->next->next != NULL) + return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); /* look for table */ - dom = mi_find_domain( &node->value ); - if (dom==NULL) - return init_mi_tree( 404, "Table not found", 15); + dom = mi_find_domain(&node->value); + if(dom == NULL) + return init_mi_tree(404, "Table not found", 15); /* process the aor */ aor = &node->next->value; - if ( mi_fix_aor(aor)!=0 ) - return init_mi_tree( 400, "Domain missing in AOR", 21); + if(mi_fix_aor(aor) != 0) + return init_mi_tree(400, "Domain missing in AOR", 21); - lock_udomain( dom, aor); + lock_udomain(dom, aor); - ret = get_urecord( dom, aor, &rec); - if (ret == 1) { - unlock_udomain( dom, aor); - return init_mi_tree( 404, "AOR not found", 13); + ret = get_urecord(dom, aor, &rec); + if(ret == 1) { + unlock_udomain(dom, aor); + return init_mi_tree(404, "AOR not found", 13); } get_act_time(); rpl_tree = 0; rpl = 0; - for( con=rec->contacts ; con ; con=con->next) { - if (VALID_CONTACT( con, act_time)) { - if (rpl_tree==0) { - rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN); - if (rpl_tree==0) + for(con = rec->contacts; con; con = con->next) { + if(VALID_CONTACT(con, act_time)) { + if(rpl_tree == 0) { + rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN); + if(rpl_tree == 0) goto error; rpl = &rpl_tree->node; } - node = addf_mi_node_child( rpl, 0, "Contact", 7, - "<%.*s>;q=%s;expires=%d;flags=0x%X;cflags=0x%X;socket=<%.*s>;" - "methods=0x%X" - "%s%.*s%s" /*received*/ - "%s%.*s%s" /*user-agent*/ - "%s%.*s%s", /*path*/ - con->c.len, ZSW(con->c.s), - q2str(con->q, 0), (int)(con->expires - act_time), - con->flags, con->cflags, - con->sock?con->sock->sock_str.len:3, - con->sock?con->sock->sock_str.s:"NULL", - con->methods, - con->received.len?";received=<":"",con->received.len, - ZSW(con->received.s), con->received.len?">":"", - con->user_agent.len?";user_agent=<":"",con->user_agent.len, - ZSW(con->user_agent.s), con->user_agent.len?">":"", - con->path.len?";path=<":"", con->path.len, - ZSW(con->path.s), con->path.len?">":"" - ); - if (node==0) + node = addf_mi_node_child(rpl, 0, "Contact", 7, + "<%.*s>;q=%s;expires=%d;flags=0x%X;cflags=0x%X;socket=<%.*" + "s>;" + "methods=0x%X" + "%s%.*s%s" /*received*/ + "%s%.*s%s" /*user-agent*/ + "%s%.*s%s", /*path*/ + con->c.len, ZSW(con->c.s), q2str(con->q, 0), + (int)(con->expires - act_time), con->flags, con->cflags, + con->sock ? con->sock->sock_str.len : 3, + con->sock ? con->sock->sock_str.s : "NULL", con->methods, + con->received.len ? ";received=<" : "", con->received.len, + ZSW(con->received.s), con->received.len ? ">" : "", + con->user_agent.len ? ";user_agent=<" : "", + con->user_agent.len, ZSW(con->user_agent.s), + con->user_agent.len ? ">" : "", + con->path.len ? ";path=<" : "", con->path.len, + ZSW(con->path.s), con->path.len ? ">" : ""); + if(node == 0) goto error; } } - unlock_udomain( dom, aor); + unlock_udomain(dom, aor); - if (rpl_tree==0) - return init_mi_tree( 404 , "AOR has no contacts", 18); + if(rpl_tree == 0) + return init_mi_tree(404, "AOR has no contacts", 18); return rpl_tree; error: - if (rpl_tree) - free_mi_tree( rpl_tree ); - unlock_udomain( dom, aor); + if(rpl_tree) + free_mi_tree(rpl_tree); + unlock_udomain(dom, aor); return 0; } #endif diff --git a/src/modules/p_usrloc/urecord.c b/src/modules/p_usrloc/urecord.c index 16b8255d134..43a67bbad73 100644 --- a/src/modules/p_usrloc/urecord.c +++ b/src/modules/p_usrloc/urecord.c @@ -52,17 +52,17 @@ int cseq_delay = 20; * \param _r pointer to the new record * \return 0 on success, negative on failure */ -int new_urecord(str* _dom, str* _aor, urecord_t** _r) +int new_urecord(str *_dom, str *_aor, urecord_t **_r) { - *_r = (urecord_t*)shm_malloc(sizeof(urecord_t)); - if (*_r == 0) { + *_r = (urecord_t *)shm_malloc(sizeof(urecord_t)); + if(*_r == 0) { LM_ERR("no more share memory\n"); return -1; } memset(*_r, 0, sizeof(urecord_t)); - (*_r)->aor.s = (char*)shm_malloc(_aor->len); - if ((*_r)->aor.s == 0) { + (*_r)->aor.s = (char *)shm_malloc(_aor->len); + if((*_r)->aor.s == 0) { LM_ERR("no more share memory\n"); shm_free(*_r); *_r = 0; @@ -84,19 +84,20 @@ int new_urecord(str* _dom, str* _aor, urecord_t** _r) * lists first * \param _r freed record list */ -void free_urecord(urecord_t* _r) +void free_urecord(urecord_t *_r) { - ucontact_t* ptr; + ucontact_t *ptr; while(_r->contacts) { ptr = _r->contacts; _r->contacts = _r->contacts->next; free_ucontact(ptr); } - + /* if mem cache is not used, the urecord struct is static*/ - if (db_mode!=DB_ONLY) { - if (_r->aor.s) shm_free(_r->aor.s); + if(db_mode != DB_ONLY) { + if(_r->aor.s) + shm_free(_r->aor.s); shm_free(_r); } } @@ -107,17 +108,17 @@ void free_urecord(urecord_t* _r) * \param _f print output * \param _r printed record */ -void print_urecord(FILE* _f, urecord_t* _r) +void print_urecord(FILE *_f, urecord_t *_r) { - ucontact_t* ptr; + ucontact_t *ptr; fprintf(_f, "...Record(%p)...\n", _r); fprintf(_f, "domain : '%.*s'\n", _r->domain->len, ZSW(_r->domain->s)); fprintf(_f, "aor : '%.*s'\n", _r->aor.len, ZSW(_r->aor.s)); fprintf(_f, "aorhash: '%u'\n", (unsigned)_r->aorhash); - fprintf(_f, "slot: '%d'\n", _r->aorhash&(_r->slot->d->size-1)); - - if (_r->contacts) { + fprintf(_f, "slot: '%d'\n", _r->aorhash & (_r->slot->d->size - 1)); + + if(_r->contacts) { ptr = _r->contacts; while(ptr) { print_ucontact(_f, ptr); @@ -139,29 +140,30 @@ void print_urecord(FILE* _f, urecord_t* _r) * \param _ci contact information * \return pointer to new created contact on success, 0 on failure */ -ucontact_t* mem_insert_ucontact(urecord_t* _r, str* _c, ucontact_info_t* _ci) +ucontact_t *mem_insert_ucontact(urecord_t *_r, str *_c, ucontact_info_t *_ci) { - ucontact_t* ptr, *prev = 0; - ucontact_t* c; + ucontact_t *ptr, *prev = 0; + ucontact_t *c; - if ( (c=new_ucontact(_r->domain, &_r->aor, _c, _ci)) == 0) { + if((c = new_ucontact(_r->domain, &_r->aor, _c, _ci)) == 0) { LM_ERR("failed to create new contact\n"); return 0; } - if_update_stat( _r->slot, _r->slot->d->contacts, 1); + if_update_stat(_r->slot, _r->slot->d->contacts, 1); ptr = _r->contacts; - if (!desc_time_order) { + if(!desc_time_order) { while(ptr) { - if (ptr->q < c->q) break; + if(ptr->q < c->q) + break; prev = ptr; ptr = ptr->next; } } - if (ptr) { - if (!ptr->prev) { + if(ptr) { + if(!ptr->prev) { ptr->prev = c; c->next = ptr; _r->contacts = c; @@ -171,7 +173,7 @@ ucontact_t* mem_insert_ucontact(urecord_t* _r, str* _c, ucontact_info_t* _ci) ptr->prev->next = c; ptr->prev = c; } - } else if (prev) { + } else if(prev) { prev->next = c; c->prev = prev; } else { @@ -187,20 +189,20 @@ ucontact_t* mem_insert_ucontact(urecord_t* _r, str* _c, ucontact_info_t* _ci) * \param _r record this contact belongs to * \param _c removed contact */ -void mem_remove_ucontact(urecord_t* _r, ucontact_t* _c) +void mem_remove_ucontact(urecord_t *_r, ucontact_t *_c) { - if (_c->prev) { + if(_c->prev) { _c->prev->next = _c->next; - if (_c->next) { + if(_c->next) { _c->next->prev = _c->prev; } } else { _r->contacts = _c->next; - if (_c->next) { + if(_c->next) { _c->next->prev = 0; } } -} +} /*! @@ -208,10 +210,10 @@ void mem_remove_ucontact(urecord_t* _r, ucontact_t* _c) * \param _r record this contact belongs to * \param _c deleted contact */ -void mem_delete_ucontact(urecord_t* _r, ucontact_t* _c) +void mem_delete_ucontact(urecord_t *_r, ucontact_t *_c) { mem_remove_ucontact(_r, _c); - if_update_stat( _r->slot, _r->slot->d->contacts, -1); + if_update_stat(_r->slot, _r->slot->d->contacts, -1); free_ucontact(_c); } @@ -223,27 +225,26 @@ void mem_delete_ucontact(urecord_t* _r, ucontact_t* _c) * the record, delete the expired ones from memory. * \param _r processed record */ -static inline void nodb_timer(urecord_t* _r) +static inline void nodb_timer(urecord_t *_r) { - ucontact_t* ptr, *t; + ucontact_t *ptr, *t; ptr = _r->contacts; while(ptr) { - if (!VALID_CONTACT(ptr, act_time)) { + if(!VALID_CONTACT(ptr, act_time)) { /* run callbacks for EXPIRE event */ - if (exists_ulcb_type(UL_CONTACT_EXPIRE)) - run_ul_callbacks( UL_CONTACT_EXPIRE, ptr); + if(exists_ulcb_type(UL_CONTACT_EXPIRE)) + run_ul_callbacks(UL_CONTACT_EXPIRE, ptr); - LM_DBG("Binding '%.*s','%.*s' has expired\n", - ptr->aor->len, ZSW(ptr->aor->s), - ptr->c.len, ZSW(ptr->c.s)); + LM_DBG("Binding '%.*s','%.*s' has expired\n", ptr->aor->len, + ZSW(ptr->aor->s), ptr->c.len, ZSW(ptr->c.s)); t = ptr; ptr = ptr->next; mem_delete_ucontact(_r, t); - update_stat( _r->slot->d->expires, 1); + update_stat(_r->slot->d->expires, 1); } else { ptr = ptr->next; } @@ -259,31 +260,30 @@ static inline void nodb_timer(urecord_t* _r) * \param _r processed record * \note currently unused, this mode is also handled by the wb_timer */ -static inline void wt_timer(urecord_t* _r) +static inline void wt_timer(urecord_t *_r) { - ucontact_t* ptr, *t; + ucontact_t *ptr, *t; ptr = _r->contacts; while(ptr) { - if (!VALID_CONTACT(ptr, act_time)) { + if(!VALID_CONTACT(ptr, act_time)) { /* run callbacks for EXPIRE event */ - if (exists_ulcb_type(UL_CONTACT_EXPIRE)) { - run_ul_callbacks( UL_CONTACT_EXPIRE, ptr); + if(exists_ulcb_type(UL_CONTACT_EXPIRE)) { + run_ul_callbacks(UL_CONTACT_EXPIRE, ptr); } - LM_DBG("Binding '%.*s','%.*s' has expired\n", - ptr->aor->len, ZSW(ptr->aor->s), - ptr->c.len, ZSW(ptr->c.s)); + LM_DBG("Binding '%.*s','%.*s' has expired\n", ptr->aor->len, + ZSW(ptr->aor->s), ptr->c.len, ZSW(ptr->c.s)); t = ptr; ptr = ptr->next; - if (db_delete_ucontact(t) < 0) { + if(db_delete_ucontact(t) < 0) { LM_ERR("deleting contact from database failed\n"); } mem_delete_ucontact(_r, t); - update_stat( _r->slot->d->expires, 1); + update_stat(_r->slot->d->expires, 1); } else { ptr = ptr->next; } @@ -300,32 +300,31 @@ static inline void wt_timer(urecord_t* _r) * ones in the DB. * \param _r processed record */ -static inline void wb_timer(urecord_t* _r) +static inline void wb_timer(urecord_t *_r) { - ucontact_t* ptr, *t; + ucontact_t *ptr, *t; cstate_t old_state; int op, res; ptr = _r->contacts; while(ptr) { - if (!VALID_CONTACT(ptr, act_time)) { + if(!VALID_CONTACT(ptr, act_time)) { /* run callbacks for EXPIRE event */ - if (exists_ulcb_type(UL_CONTACT_EXPIRE)) { - run_ul_callbacks( UL_CONTACT_EXPIRE, ptr); + if(exists_ulcb_type(UL_CONTACT_EXPIRE)) { + run_ul_callbacks(UL_CONTACT_EXPIRE, ptr); } - LM_DBG("Binding '%.*s','%.*s' has expired\n", - ptr->aor->len, ZSW(ptr->aor->s), - ptr->c.len, ZSW(ptr->c.s)); - update_stat( _r->slot->d->expires, 1); + LM_DBG("Binding '%.*s','%.*s' has expired\n", ptr->aor->len, + ZSW(ptr->aor->s), ptr->c.len, ZSW(ptr->c.s)); + update_stat(_r->slot->d->expires, 1); t = ptr; ptr = ptr->next; /* Should we remove the contact from the database ? */ - if (st_expired_ucontact(t) == 1) { - if (db_delete_ucontact(t) < 0) { + if(st_expired_ucontact(t) == 1) { + if(db_delete_ucontact(t) < 0) { LM_ERR("failed to delete contact from the database\n"); } } @@ -337,26 +336,26 @@ static inline void wb_timer(urecord_t* _r) op = st_flush_ucontact(ptr); switch(op) { - case 0: /* do nothing, contact is synchronized */ - break; - - case 1: /* insert */ - if (db_insert_ucontact(ptr) < 0) { - LM_ERR("inserting contact into database failed\n"); - ptr->state = old_state; - } - break; - - case 2: /* update */ - if (cfg_get(p_usrloc,p_usrloc_cfg,db_update_as_insert)) - res = db_insert_ucontact(ptr); - else - res = db_update_ucontact(ptr); - if (res < 0) { - LM_ERR("updating contact in db failed\n"); - ptr->state = old_state; - } - break; + case 0: /* do nothing, contact is synchronized */ + break; + + case 1: /* insert */ + if(db_insert_ucontact(ptr) < 0) { + LM_ERR("inserting contact into database failed\n"); + ptr->state = old_state; + } + break; + + case 2: /* update */ + if(cfg_get(p_usrloc, p_usrloc_cfg, db_update_as_insert)) + res = db_insert_ucontact(ptr); + else + res = db_update_ucontact(ptr); + if(res < 0) { + LM_ERR("updating contact in db failed\n"); + ptr->state = old_state; + } + break; } ptr = ptr->next; @@ -372,17 +371,20 @@ static inline void wb_timer(urecord_t* _r) * on the db_mode setting. * \param _r processed record */ -void timer_urecord(urecord_t* _r) +void timer_urecord(urecord_t *_r) { switch(db_mode) { - case NO_DB: nodb_timer(_r); - break; - /* use also the write_back timer routine to handle the failed + case NO_DB: + nodb_timer(_r); + break; + /* use also the write_back timer routine to handle the failed * realtime inserts/updates */ - case WRITE_THROUGH: wb_timer(_r); /*wt_timer(_r);*/ - break; - case WRITE_BACK: wb_timer(_r); - break; + case WRITE_THROUGH: + wb_timer(_r); /*wt_timer(_r);*/ + break; + case WRITE_BACK: + wb_timer(_r); + break; } } @@ -392,11 +394,11 @@ void timer_urecord(urecord_t* _r) * \param _r deleted record * \return 0 on success, -1 on failure */ -int db_delete_urecord(udomain_t* _d, urecord_t* _r) +int db_delete_urecord(udomain_t *_d, urecord_t *_r) { db_key_t keys[2]; db_val_t vals[2]; - char* dom; + char *dom; keys[0] = &user_col; keys[1] = &domain_col; @@ -405,7 +407,7 @@ int db_delete_urecord(udomain_t* _d, urecord_t* _r) vals[0].val.str_val.s = _r->aor.s; vals[0].val.str_val.len = _r->aor.len; - if (use_domain) { + if(use_domain) { dom = memchr(_r->aor.s, '@', _r->aor.len); vals[0].val.str_val.len = dom - _r->aor.s; @@ -415,7 +417,9 @@ int db_delete_urecord(udomain_t* _d, urecord_t* _r) vals[1].val.str_val.len = _r->aor.s + _r->aor.len - dom - 1; } - if (ul_db_layer_delete(_d, &vals[0].val.str_val, &vals[1].val.str_val, keys, 0, vals, (use_domain) ? (2) : (1)) < 0) { + if(ul_db_layer_delete(_d, &vals[0].val.str_val, &vals[1].val.str_val, keys, + 0, vals, (use_domain) ? (2) : (1)) + < 0) { return -1; } @@ -431,11 +435,11 @@ int db_delete_urecord(udomain_t* _d, urecord_t* _r) * it is not necessary, as this function already releases the record. * \param _r released record */ -void release_urecord(urecord_t* _r) +void release_urecord(urecord_t *_r) { - if (db_mode==DB_ONLY) { + if(db_mode == DB_ONLY) { free_urecord(_r); - } else if (_r->contacts == 0) { + } else if(_r->contacts == 0) { mem_delete_urecord(_r->slot->d, _r); } } @@ -449,20 +453,20 @@ void release_urecord(urecord_t* _r) * \param _c new created contact * \return 0 on success, -1 on failure */ -int insert_ucontact(urecord_t* _r, str* _contact, ucontact_info_t* _ci, - ucontact_t** _c) +int insert_ucontact( + urecord_t *_r, str *_contact, ucontact_info_t *_ci, ucontact_t **_c) { - if ( ((*_c)=mem_insert_ucontact(_r, _contact, _ci)) == 0) { + if(((*_c) = mem_insert_ucontact(_r, _contact, _ci)) == 0) { LM_ERR("failed to insert contact\n"); return -1; } - if (exists_ulcb_type(UL_CONTACT_INSERT)) { - run_ul_callbacks( UL_CONTACT_INSERT, *_c); + if(exists_ulcb_type(UL_CONTACT_INSERT)) { + run_ul_callbacks(UL_CONTACT_INSERT, *_c); } - if (db_mode == WRITE_THROUGH || db_mode==DB_ONLY) { - if (db_insert_ucontact(*_c) < 0) { + if(db_mode == WRITE_THROUGH || db_mode == DB_ONLY) { + if(db_insert_ucontact(*_c) < 0) { LM_ERR("failed to insert in database\n"); return -1; } else { @@ -480,17 +484,17 @@ int insert_ucontact(urecord_t* _r, str* _contact, ucontact_info_t* _ci, * \param _c deleted contact * \return 0 on success, -1 on failure */ -int delete_ucontact(urecord_t* _r, struct ucontact* _c) +int delete_ucontact(urecord_t *_r, struct ucontact *_c) { int ret = 0; - if (exists_ulcb_type(UL_CONTACT_DELETE)) { - run_ul_callbacks( UL_CONTACT_DELETE, _c); + if(exists_ulcb_type(UL_CONTACT_DELETE)) { + run_ul_callbacks(UL_CONTACT_DELETE, _c); } - if (st_delete_ucontact(_c) > 0) { - if (db_mode == WRITE_THROUGH || db_mode==DB_ONLY) { - if (db_delete_ucontact(_c) < 0) { + if(st_delete_ucontact(_c) > 0) { + if(db_mode == WRITE_THROUGH || db_mode == DB_ONLY) { + if(db_delete_ucontact(_c) < 0) { LM_ERR("failed to remove contact from database\n"); ret = -1; } @@ -509,13 +513,13 @@ int delete_ucontact(urecord_t* _r, struct ucontact* _c) * \param _c contact string * \return ptr on successfull match, 0 when they not match */ -static inline struct ucontact* contact_match( ucontact_t* ptr, str* _c) +static inline struct ucontact *contact_match(ucontact_t *ptr, str *_c) { while(ptr) { - if ((_c->len == ptr->c.len) && !memcmp(_c->s, ptr->c.s, _c->len)) { + if((_c->len == ptr->c.len) && !memcmp(_c->s, ptr->c.s, _c->len)) { return ptr; } - + ptr = ptr->next; } return 0; @@ -529,41 +533,41 @@ static inline struct ucontact* contact_match( ucontact_t* ptr, str* _c) * \param _callid callid * \return ptr on successfull match, 0 when they not match */ -static inline struct ucontact* contact_callid_match( ucontact_t* ptr, - str* _c, str *_callid) +static inline struct ucontact *contact_callid_match( + ucontact_t *ptr, str *_c, str *_callid) { while(ptr) { - if ( (_c->len==ptr->c.len) && (_callid->len==ptr->callid.len) - && !memcmp(_c->s, ptr->c.s, _c->len) - && !memcmp(_callid->s, ptr->callid.s, _callid->len) - ) { + if((_c->len == ptr->c.len) && (_callid->len == ptr->callid.len) + && !memcmp(_c->s, ptr->c.s, _c->len) + && !memcmp(_callid->s, ptr->callid.s, _callid->len)) { return ptr; } - + ptr = ptr->next; } return 0; } - /*! +/*! + * \brief Match a contact record to a contact string and path + * \param ptr contact record + * \param _c contact string + * \param _path path + * \return ptr on successfull match, 0 when they not match + */ -static inline struct ucontact* contact_path_match( ucontact_t* ptr, str* _c, str *_path) +static inline struct ucontact *contact_path_match( + ucontact_t *ptr, str *_c, str *_path) { /* if no path is preset (in REGISTER request) or use_path is not configured in registrar module, default to contact_match() */ - if( _path == NULL) return contact_match(ptr, _c); + if(_path == NULL) + return contact_match(ptr, _c); while(ptr) { - if ( (_c->len==ptr->c.len) && (_path->len==ptr->path.len) - && !memcmp(_c->s, ptr->c.s, _c->len) - && !memcmp(_path->s, ptr->path.s, _path->len) - ) { + if((_c->len == ptr->c.len) && (_path->len == ptr->path.len) + && !memcmp(_c->s, ptr->c.s, _c->len) + && !memcmp(_path->s, ptr->path.s, _path->len)) { return ptr; } @@ -583,10 +587,10 @@ static inline struct ucontact* contact_path_match( ucontact_t* ptr, str* _c, str * \return 0 - found, 1 - not found, -1 - invalid found, * -2 - found, but to be skipped (same cseq) */ -int get_ucontact(urecord_t* _r, str* _c, str* _callid, str* _path, int _cseq, - struct ucontact** _co) +int get_ucontact(urecord_t *_r, str *_c, str *_callid, str *_path, int _cseq, + struct ucontact **_co) { - ucontact_t* ptr; + ucontact_t *ptr; int no_callid; ptr = 0; @@ -595,31 +599,33 @@ int get_ucontact(urecord_t* _r, str* _c, str* _callid, str* _path, int _cseq, int matching_mode_cfg = cfg_get(p_usrloc, p_usrloc_cfg, matching_mode); - switch (matching_mode_cfg) { + switch(matching_mode_cfg) { case CONTACT_ONLY: - ptr = contact_match( _r->contacts, _c); + ptr = contact_match(_r->contacts, _c); break; case CONTACT_CALLID: - ptr = contact_callid_match( _r->contacts, _c, _callid); + ptr = contact_callid_match(_r->contacts, _c, _callid); no_callid = 1; break; case CONTACT_PATH: - ptr = contact_path_match( _r->contacts, _c, _path); + ptr = contact_path_match(_r->contacts, _c, _path); break; default: LM_CRIT("unknown matching_mode %d\n", matching_mode_cfg); return -1; } - if (ptr) { + if(ptr) { /* found -> check callid and cseq */ - if ( no_callid || (ptr->callid.len==_callid->len - && memcmp(_callid->s, ptr->callid.s, _callid->len)==0 ) ) { - if (_cseqcseq) + if(no_callid + || (ptr->callid.len == _callid->len + && memcmp(_callid->s, ptr->callid.s, _callid->len) + == 0)) { + if(_cseq < ptr->cseq) return -1; - if (_cseq==ptr->cseq) { + if(_cseq == ptr->cseq) { get_act_time(); - return (ptr->last_modified+cseq_delay>act_time)?-2:-1; + return (ptr->last_modified + cseq_delay > act_time) ? -2 : -1; } } *_co = ptr; @@ -633,38 +639,37 @@ int get_ucontact(urecord_t* _r, str* _c, str* _callid, str* _path, int _cseq, /* * Get pointer to ucontact with given info (by address or sip.instance) */ -int get_ucontact_by_instance(urecord_t* _r, str* _c, ucontact_info_t* _ci, - ucontact_t** _co) +int get_ucontact_by_instance( + urecord_t *_r, str *_c, ucontact_info_t *_ci, ucontact_t **_co) { - ucontact_t* ptr; + ucontact_t *ptr; str i1; str i2; - - if (_ci->instance.s == NULL || _ci->instance.len <= 0) { + + if(_ci->instance.s == NULL || _ci->instance.len <= 0) { return get_ucontact(_r, _c, _ci->callid, _ci->path, _ci->cseq, _co); } /* find by instance */ ptr = _r->contacts; while(ptr) { - if (ptr->instance.len>0 && _ci->reg_id==ptr->reg_id) - { + if(ptr->instance.len > 0 && _ci->reg_id == ptr->reg_id) { i1 = _ci->instance; i2 = ptr->instance; - if(i1.s[0]=='<' && i1.s[i1.len-1]=='>') { + if(i1.s[0] == '<' && i1.s[i1.len - 1] == '>') { i1.s++; - i1.len-=2; + i1.len -= 2; } - if(i2.s[0]=='<' && i2.s[i2.len-1]=='>') { + if(i2.s[0] == '<' && i2.s[i2.len - 1] == '>') { i2.s++; - i2.len-=2; + i2.len -= 2; } - if(i1.len==i2.len && memcmp(i1.s, i2.s, i2.len)==0) { + if(i1.len == i2.len && memcmp(i1.s, i2.s, i2.len) == 0) { *_co = ptr; return 0; } } - + ptr = ptr->next; } return 1; diff --git a/src/modules/p_usrloc/urecord.h b/src/modules/p_usrloc/urecord.h index 90c65191fe1..235483010e9 100644 --- a/src/modules/p_usrloc/urecord.h +++ b/src/modules/p_usrloc/urecord.h @@ -43,7 +43,7 @@ * \param _r pointer to the new record * \return 0 on success, negative on failure */ -int new_urecord(str* _dom, str* _aor, urecord_t** _r); +int new_urecord(str *_dom, str *_aor, urecord_t **_r); /*! @@ -54,7 +54,7 @@ int new_urecord(str* _dom, str* _aor, urecord_t** _r); * lists first * \param _r freed record list */ -void free_urecord(urecord_t* _r); +void free_urecord(urecord_t *_r); /*! @@ -62,7 +62,7 @@ void free_urecord(urecord_t* _r); * \param _f print output * \param _r printed record */ -void print_urecord(FILE* _f, urecord_t* _r); +void print_urecord(FILE *_f, urecord_t *_r); /*! @@ -75,7 +75,7 @@ void print_urecord(FILE* _f, urecord_t* _r); * \param _ci contact information * \return pointer to new created contact on success, 0 on failure */ -ucontact_t* mem_insert_ucontact(urecord_t* _r, str* _c, ucontact_info_t* _ci); +ucontact_t *mem_insert_ucontact(urecord_t *_r, str *_c, ucontact_info_t *_ci); /*! @@ -83,7 +83,7 @@ ucontact_t* mem_insert_ucontact(urecord_t* _r, str* _c, ucontact_info_t* _ci); * \param _r record this contact belongs to * \param _c removed contact */ -void mem_remove_ucontact(urecord_t* _r, ucontact_t* _c); +void mem_remove_ucontact(urecord_t *_r, ucontact_t *_c); /*! @@ -91,7 +91,7 @@ void mem_remove_ucontact(urecord_t* _r, ucontact_t* _c); * \param _r record this contact belongs to * \param _c deleted contact */ -void mem_delete_ucontact(urecord_t* _r, ucontact_t* _c); +void mem_delete_ucontact(urecord_t *_r, ucontact_t *_c); /*! @@ -101,7 +101,7 @@ void mem_delete_ucontact(urecord_t* _r, ucontact_t* _c); * on the db_mode setting. * \param _r processed record */ -void timer_urecord(urecord_t* _r); +void timer_urecord(urecord_t *_r); /*! @@ -109,7 +109,7 @@ void timer_urecord(urecord_t* _r); * \param _r deleted record * \return 0 on success, -1 on failure */ -int db_delete_urecord(struct udomain* _d, urecord_t* _r); +int db_delete_urecord(struct udomain *_d, urecord_t *_r); /*! * \brief Create and insert new contact into urecord @@ -119,8 +119,8 @@ int db_delete_urecord(struct udomain* _d, urecord_t* _r); * \param _c new created contact * \return 0 on success, -1 on failure */ -int insert_ucontact(urecord_t* _r, str* _contact, - ucontact_info_t* _ci, ucontact_t** _c); +int insert_ucontact( + urecord_t *_r, str *_contact, ucontact_info_t *_ci, ucontact_t **_c); /*! @@ -129,7 +129,7 @@ int insert_ucontact(urecord_t* _r, str* _contact, * \param _c deleted contact * \return 0 on success, -1 on failure */ -int delete_ucontact(urecord_t* _r, struct ucontact* _c); +int delete_ucontact(urecord_t *_r, struct ucontact *_c); /*! @@ -143,9 +143,8 @@ int delete_ucontact(urecord_t* _r, struct ucontact* _c); * \return 0 - found, 1 - not found, -1 - invalid found, * -2 - found, but to be skipped (same cseq) */ -int get_ucontact(urecord_t* _r, str* _c, str* _callid, str* _path, - int _cseq, - struct ucontact** _co); +int get_ucontact(urecord_t *_r, str *_c, str *_callid, str *_path, int _cseq, + struct ucontact **_co); /* ===== Module interface ======== */ @@ -158,7 +157,7 @@ int get_ucontact(urecord_t* _r, str* _c, str* _callid, str* _path, * it is not necessary, as this function already releases the record. * \param _r released record */ -void release_urecord(urecord_t* _r); +void release_urecord(urecord_t *_r); /*! * \brief Get pointer to ucontact with given contact @@ -169,7 +168,7 @@ void release_urecord(urecord_t* _r); * \return 0 - found, 1 - not found, -1 - invalid found, * -2 - found, but to be skipped (same cseq) */ -int get_ucontact_by_instance(urecord_t* _r, str* _c, ucontact_info_t* _ci, - ucontact_t** _co); +int get_ucontact_by_instance( + urecord_t *_r, str *_c, ucontact_info_t *_ci, ucontact_t **_co); #endif diff --git a/src/modules/p_usrloc/usrloc.c b/src/modules/p_usrloc/usrloc.c index acb70f1bd89..27cbdb7f8ce 100644 --- a/src/modules/p_usrloc/usrloc.c +++ b/src/modules/p_usrloc/usrloc.c @@ -45,39 +45,39 @@ extern unsigned int init_flag; * \param api usrloc API * \return 0 on success, -1 on failure */ -int bind_usrloc(usrloc_api_t* api) +int bind_usrloc(usrloc_api_t *api) { - if (!api) { + if(!api) { LM_ERR("invalid parameter value\n"); return -1; } - if (init_flag==0) { + if(init_flag == 0) { LM_ERR("configuration error - trying to bind to usrloc module" - " before being initialized\n"); + " before being initialized\n"); return -1; } - api->register_udomain = register_udomain; - api->get_all_ucontacts = get_all_ucontacts; - api->insert_urecord = insert_urecord; - api->delete_urecord = delete_urecord; - api->get_urecord = get_urecord; - api->lock_udomain = lock_udomain; - api->unlock_udomain = unlock_udomain; - api->release_urecord = release_urecord; - api->insert_ucontact = insert_ucontact; - api->delete_ucontact = delete_ucontact; - api->get_ucontact = get_ucontact; - api->update_ucontact = update_ucontact; - api->register_ulcb = register_ulcb; - api->get_aorhash = ul_get_aorhash; + api->register_udomain = register_udomain; + api->get_all_ucontacts = get_all_ucontacts; + api->insert_urecord = insert_urecord; + api->delete_urecord = delete_urecord; + api->get_urecord = get_urecord; + api->lock_udomain = lock_udomain; + api->unlock_udomain = unlock_udomain; + api->release_urecord = release_urecord; + api->insert_ucontact = insert_ucontact; + api->delete_ucontact = delete_ucontact; + api->get_ucontact = get_ucontact; + api->update_ucontact = update_ucontact; + api->register_ulcb = register_ulcb; + api->get_aorhash = ul_get_aorhash; - api->get_urecord_by_ruid = get_urecord_by_ruid; + api->get_urecord_by_ruid = get_urecord_by_ruid; api->get_ucontact_by_instance = get_ucontact_by_instance; api->use_domain = use_domain; - api->db_mode = db_mode; - api->nat_flag = nat_bflag; + api->db_mode = db_mode; + api->nat_flag = nat_bflag; return 0; }