From 8598f4e099a30e94af1fb526afc39170af53ee0d Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 17 May 2023 16:36:10 +0200 Subject: [PATCH] ims_usrloc_pcscf: clang-format for coherent indentation and coding style --- src/modules/ims_usrloc_pcscf/dlist.c | 253 +-- src/modules/ims_usrloc_pcscf/dlist.h | 19 +- src/modules/ims_usrloc_pcscf/hslot.c | 53 +- src/modules/ims_usrloc_pcscf/hslot.h | 23 +- .../ims_usrloc_pcscf/ims_usrloc_pcscf_mod.c | 219 +-- src/modules/ims_usrloc_pcscf/pcontact.c | 463 +++--- src/modules/ims_usrloc_pcscf/pcontact.h | 36 +- src/modules/ims_usrloc_pcscf/reginfo.c | 4 - src/modules/ims_usrloc_pcscf/reginfo.h | 21 +- src/modules/ims_usrloc_pcscf/udomain.c | 1385 +++++++++-------- src/modules/ims_usrloc_pcscf/udomain.h | 59 +- src/modules/ims_usrloc_pcscf/ul_callback.c | 169 +- src/modules/ims_usrloc_pcscf/ul_callback.h | 52 +- src/modules/ims_usrloc_pcscf/ul_rpc.c | 123 +- src/modules/ims_usrloc_pcscf/usrloc.c | 129 +- src/modules/ims_usrloc_pcscf/usrloc.h | 390 ++--- src/modules/ims_usrloc_pcscf/usrloc_db.c | 688 ++++---- src/modules/ims_usrloc_pcscf/usrloc_db.h | 181 +-- 18 files changed, 2278 insertions(+), 1989 deletions(-) diff --git a/src/modules/ims_usrloc_pcscf/dlist.c b/src/modules/ims_usrloc_pcscf/dlist.c index e6834c1c6a8..f2e495e49d3 100644 --- a/src/modules/ims_usrloc_pcscf/dlist.c +++ b/src/modules/ims_usrloc_pcscf/dlist.c @@ -44,38 +44,37 @@ */ #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_ut.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 "usrloc.h" #include "utime.h" #include "ims_usrloc_pcscf_mod.h" #include "pcontact.h" -dlist_t* root = 0; +dlist_t *root = 0; -static inline int find_dlist(str* _n, dlist_t** _d) +static inline int find_dlist(str *_n, dlist_t **_d) { - dlist_t* ptr; + dlist_t *ptr; ptr = root; while(ptr) { - if ((_n->len == ptr->name.len) && - !memcmp(_n->s, ptr->name.s, _n->len)) { + if((_n->len == ptr->name.len) && !memcmp(_n->s, ptr->name.s, _n->len)) { *_d = ptr; return 0; } - + ptr = ptr->next; } - + return 1; } @@ -110,7 +109,7 @@ static inline int find_dlist(str* _n, dlist_t** _d) * \return 0 on success, positive if buffer size was not sufficient, negative on failure */ static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags, - unsigned int part_idx, unsigned int part_max) + unsigned int part_idx, unsigned int part_max) { dlist_t *p; pcontact_t *c; @@ -124,59 +123,86 @@ static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags, /* Reserve space for terminating 0000 */ len -= sizeof(int); - for (p = root; p != NULL; p = p->next) { - for(i=0; id->size; i++) { - if ((i % part_max) != part_idx) { + for(p = root; p != NULL; p = p->next) { + for(i = 0; i < p->d->size; i++) { + if((i % part_max) != part_idx) { continue; } lock_ulslot(p->d, i); - if(p->d->table[i].n<=0) { + if(p->d->table[i].n <= 0) { unlock_ulslot(p->d, i); continue; } - for (c = p->d->table[i].first; c != NULL; c = c->next) { - if (c->received_host.s && c->security_temp && c->security_temp->type == SECURITY_IPSEC) { - needed = (int)(sizeof(c->received_host.len) + c->received_host.len + - sizeof(c->security_temp->data.ipsec->spi_uc) + - sizeof(c->security_temp->data.ipsec->spi_us) + - sizeof(c->security_temp->data.ipsec->spi_pc) + - sizeof(c->security_temp->data.ipsec->spi_ps) + - sizeof(c->security_temp->data.ipsec->port_uc) + - sizeof(c->security_temp->data.ipsec->port_us) + - sizeof(c->security_temp->data.ipsec->port_pc) + - sizeof(c->security_temp->data.ipsec->port_ps)); - - if (len >= needed) { - memcpy(cp, &c->received_host.len, sizeof(c->received_host.len)); - cp = (char*)cp + sizeof(c->received_host.len); + for(c = p->d->table[i].first; c != NULL; c = c->next) { + if(c->received_host.s && c->security_temp + && c->security_temp->type == SECURITY_IPSEC) { + needed = (int)(sizeof(c->received_host.len) + + c->received_host.len + + sizeof( + c->security_temp->data.ipsec->spi_uc) + + sizeof( + c->security_temp->data.ipsec->spi_us) + + sizeof( + c->security_temp->data.ipsec->spi_pc) + + sizeof( + c->security_temp->data.ipsec->spi_ps) + + sizeof(c->security_temp->data.ipsec + ->port_uc) + + sizeof(c->security_temp->data.ipsec + ->port_us) + + sizeof(c->security_temp->data.ipsec + ->port_pc) + + sizeof(c->security_temp->data.ipsec + ->port_ps)); + + if(len >= needed) { + memcpy(cp, &c->received_host.len, + sizeof(c->received_host.len)); + cp = (char *)cp + sizeof(c->received_host.len); memcpy(cp, c->received_host.s, c->received_host.len); - cp = (char*)cp + c->received_host.len; - - memcpy(cp, &c->security_temp->data.ipsec->spi_uc, sizeof(c->security_temp->data.ipsec->spi_uc)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->spi_uc); - - memcpy(cp, &c->security_temp->data.ipsec->spi_us, sizeof(c->security_temp->data.ipsec->spi_us)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->spi_us); - - memcpy(cp, &c->security_temp->data.ipsec->spi_pc, sizeof(c->security_temp->data.ipsec->spi_pc)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->spi_pc); - - memcpy(cp, &c->security_temp->data.ipsec->spi_ps, sizeof(c->security_temp->data.ipsec->spi_ps)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->spi_ps); - - memcpy(cp, &c->security_temp->data.ipsec->port_uc, sizeof(c->security_temp->data.ipsec->port_uc)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->port_uc); - - memcpy(cp, &c->security_temp->data.ipsec->port_us, sizeof(c->security_temp->data.ipsec->port_us)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->port_us); - - memcpy(cp, &c->security_temp->data.ipsec->port_pc, sizeof(c->security_temp->data.ipsec->port_pc)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->port_pc); - - memcpy(cp, &c->security_temp->data.ipsec->port_ps, sizeof(c->security_temp->data.ipsec->port_ps)); - cp = (char*)cp + sizeof(c->security_temp->data.ipsec->port_ps); + cp = (char *)cp + c->received_host.len; + + memcpy(cp, &c->security_temp->data.ipsec->spi_uc, + sizeof(c->security_temp->data.ipsec->spi_uc)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->spi_uc); + + memcpy(cp, &c->security_temp->data.ipsec->spi_us, + sizeof(c->security_temp->data.ipsec->spi_us)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->spi_us); + + memcpy(cp, &c->security_temp->data.ipsec->spi_pc, + sizeof(c->security_temp->data.ipsec->spi_pc)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->spi_pc); + + memcpy(cp, &c->security_temp->data.ipsec->spi_ps, + sizeof(c->security_temp->data.ipsec->spi_ps)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->spi_ps); + + memcpy(cp, &c->security_temp->data.ipsec->port_uc, + sizeof(c->security_temp->data.ipsec->port_uc)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->port_uc); + + memcpy(cp, &c->security_temp->data.ipsec->port_us, + sizeof(c->security_temp->data.ipsec->port_us)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->port_us); + + memcpy(cp, &c->security_temp->data.ipsec->port_pc, + sizeof(c->security_temp->data.ipsec->port_pc)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->port_pc); + + memcpy(cp, &c->security_temp->data.ipsec->port_ps, + sizeof(c->security_temp->data.ipsec->port_ps)); + cp = (char *)cp + + sizeof(c->security_temp->data.ipsec->port_ps); len -= needed; } else { @@ -188,11 +214,11 @@ static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags, } } /* len < 0 is possible, if size of the buffer < sizeof(c->c.len) */ - if (len >= 0) + if(len >= 0) memset(cp, 0, sizeof(int)); /* Shouldn't happen */ - if (shortage > 0 && len > shortage) { + if(shortage > 0 && len > shortage) { abort(); } @@ -202,28 +228,28 @@ static inline int get_all_mem_ucontacts(void *buf, int len, unsigned int flags, } int get_all_ucontacts(void *buf, int len, unsigned int flags, - unsigned int part_idx, unsigned int part_max) + unsigned int part_idx, unsigned int part_max) { - return get_all_mem_ucontacts( buf, len, flags, part_idx, part_max); + return get_all_mem_ucontacts(buf, len, flags, part_idx, part_max); } -static inline int new_dlist(str* _n, dlist_t** _d) +static inline int new_dlist(str *_n, dlist_t **_d) { - dlist_t* ptr; + dlist_t *ptr; /* Domains are created before ser forks, * so we can create them using pkg_malloc */ - ptr = (dlist_t*)shm_malloc(sizeof(dlist_t)); - if (ptr == 0) { + ptr = (dlist_t *)shm_malloc(sizeof(dlist_t)); + if(ptr == 0) { LM_ERR("no more share memory\n"); return -1; } memset(ptr, 0, sizeof(dlist_t)); /* copy domain name as null terminated string */ - ptr->name.s = (char*)shm_malloc(_n->len+1); - if (ptr->name.s == 0) { + ptr->name.s = (char *)shm_malloc(_n->len + 1); + if(ptr->name.s == 0) { LM_ERR("no more memory left\n"); shm_free(ptr); return -2; @@ -233,7 +259,7 @@ static inline int new_dlist(str* _n, dlist_t** _d) ptr->name.len = _n->len; ptr->name.s[ptr->name.len] = 0; - if (new_udomain(&(ptr->name), ul_hash_size, &(ptr->d)) < 0) { + if(new_udomain(&(ptr->name), ul_hash_size, &(ptr->d)) < 0) { LM_ERR("creating domain structure failed\n"); shm_free(ptr->name.s); shm_free(ptr); @@ -244,15 +270,15 @@ static inline int new_dlist(str* _n, dlist_t** _d) return 0; } -int get_udomain(const char* _n, udomain_t** _d) +int get_udomain(const char *_n, udomain_t **_d) { - dlist_t* d; + dlist_t *d; str s; - s.s = (char*)_n; + s.s = (char *)_n; s.len = strlen(_n); - if (find_dlist(&s, &d) == 0) { + if(find_dlist(&s, &d) == 0) { *_d = d->d; return 0; } @@ -260,34 +286,34 @@ int get_udomain(const char* _n, udomain_t** _d) return -1; } -int register_udomain(const char* _n, udomain_t** _d) +int register_udomain(const char *_n, udomain_t **_d) { - dlist_t* d; + dlist_t *d; str s; - s.s = (char*)_n; + s.s = (char *)_n; s.len = strlen(_n); - if (find_dlist(&s, &d) == 0) { + if(find_dlist(&s, &d) == 0) { *_d = d->d; return 0; } - - if (new_dlist(&s, &d) < 0) { + + if(new_dlist(&s, &d) < 0) { LM_ERR("failed to create new domain\n"); return -1; } d->next = root; root = d; - + *_d = d->d; return 0; } void free_all_udomains(void) { - dlist_t* ptr; + dlist_t *ptr; while(root) { ptr = root; @@ -299,10 +325,10 @@ void free_all_udomains(void) } } -void print_all_udomains(FILE* _f) +void print_all_udomains(FILE *_f) { - dlist_t* ptr; - + dlist_t *ptr; + ptr = root; fprintf(_f, "===Domain list===\n"); @@ -316,22 +342,22 @@ void print_all_udomains(FILE* _f) int synchronize_all_udomains(void) { int res = 0; - dlist_t* ptr; + dlist_t *ptr; get_act_time(); /* Get and save actual time */ - for( ptr=root ; ptr ; ptr=ptr->next) + for(ptr = root; ptr; ptr = ptr->next) mem_timer_udomain(ptr->d); return res; } -int find_domain(str* _d, udomain_t** _p) +int find_domain(str *_d, udomain_t **_p) { - dlist_t* d; + dlist_t *d; - if (find_dlist(_d, &d) == 0) { - *_p = d->d; + if(find_dlist(_d, &d) == 0) { + *_p = d->d; return 0; } @@ -340,51 +366,48 @@ int find_domain(str* _d, udomain_t** _p) unsigned long get_number_of_contacts(void) { - long numberOfUsers = 0; + long numberOfUsers = 0; - dlist_t* current_dlist; + dlist_t *current_dlist; - current_dlist = root; + current_dlist = root; - while (current_dlist) - { - numberOfUsers += get_stat_val(current_dlist->d->contacts); - current_dlist = current_dlist->next; - } + while(current_dlist) { + numberOfUsers += get_stat_val(current_dlist->d->contacts); + current_dlist = current_dlist->next; + } - return numberOfUsers; + return numberOfUsers; } unsigned long get_number_of_expired(void) { - long numberOfExpired = 0; + long numberOfExpired = 0; - dlist_t* current_dlist; + dlist_t *current_dlist; - current_dlist = root; + current_dlist = root; - while (current_dlist) - { - numberOfExpired += get_stat_val(current_dlist->d->expired); - current_dlist = current_dlist->next; - } + while(current_dlist) { + numberOfExpired += get_stat_val(current_dlist->d->expired); + current_dlist = current_dlist->next; + } - return numberOfExpired; + return numberOfExpired; } unsigned long get_number_of_impu(void) { - long numberOfExpired = 0; + long numberOfExpired = 0; - dlist_t* current_dlist; + dlist_t *current_dlist; - current_dlist = root; + current_dlist = root; - while (current_dlist) - { - numberOfExpired += get_stat_val(current_dlist->d->expired); - current_dlist = current_dlist->next; - } + while(current_dlist) { + numberOfExpired += get_stat_val(current_dlist->d->expired); + current_dlist = current_dlist->next; + } - return numberOfExpired; + return numberOfExpired; } diff --git a/src/modules/ims_usrloc_pcscf/dlist.h b/src/modules/ims_usrloc_pcscf/dlist.h index 5d00b7e4858..130621a5352 100644 --- a/src/modules/ims_usrloc_pcscf/dlist.h +++ b/src/modules/ims_usrloc_pcscf/dlist.h @@ -54,24 +54,25 @@ /*! * List of all domains registered with usrloc */ -typedef struct dlist { - str name; /*!< Name of the domain (null terminated) */ - udomain_t* d; /*!< Payload */ - struct dlist* next; /*!< Next element in the list */ +typedef struct dlist +{ + str name; /*!< Name of the domain (null terminated) */ + udomain_t *d; /*!< Payload */ + struct dlist *next; /*!< Next element in the list */ } dlist_t; /*! \brief Global list of all registered domains */ -extern dlist_t* root; +extern dlist_t *root; -int register_udomain(const char* _n, udomain_t** _d); +int register_udomain(const char *_n, udomain_t **_d); void free_all_udomains(void); -void print_all_udomains(FILE* _f); +void print_all_udomains(FILE *_f); int synchronize_all_udomains(void); int get_all_ucontacts(void *buf, int len, unsigned int flags, unsigned int part_idx, unsigned int part_max); -int get_udomain(const char* _n, udomain_t** _d); +int get_udomain(const char *_n, udomain_t **_d); unsigned long get_number_of_users(void); -int find_domain(str* _d, udomain_t** _p); +int find_domain(str *_d, udomain_t **_p); /* stat retrieval functions */ unsigned long get_number_of_contacts(void); diff --git a/src/modules/ims_usrloc_pcscf/hslot.c b/src/modules/ims_usrloc_pcscf/hslot.c index d78502180d8..0d7ee2d8dc6 100644 --- a/src/modules/ims_usrloc_pcscf/hslot.c +++ b/src/modules/ims_usrloc_pcscf/hslot.c @@ -46,34 +46,31 @@ #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; 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); } @@ -81,10 +78,10 @@ void ul_unlock_locks(void) { unsigned int i; - if (ul_locks==0) + if(ul_locks == 0) return; - for (i=0;ilocks[i]); #else @@ -95,7 +92,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); }; @@ -114,7 +111,7 @@ void ul_release_idx(int idx) } #endif -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; @@ -122,31 +119,31 @@ 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 } -void deinit_slot(hslot_t* _s) +void deinit_slot(hslot_t *_s) { - struct pcontact* ptr; - - /* Remove all elements */ + struct pcontact *ptr; + + /* Remove all elements */ while(_s->first) { ptr = _s->first; _s->first = _s->first->next; free_pcontact(ptr); } - + _s->n = 0; _s->last = 0; - _s->d = 0; + _s->d = 0; } -void slot_add(hslot_t* _s, struct pcontact* _r) +void slot_add(hslot_t *_s, struct pcontact *_r) { - if (_s->n == 0) { + if(_s->n == 0) { _s->first = _s->last = _r; } else { _r->prev = _s->last; @@ -157,15 +154,15 @@ void slot_add(hslot_t* _s, struct pcontact* _r) _r->slot = _s; } -void slot_rem(hslot_t* _s, struct pcontact* _r) +void slot_rem(hslot_t *_s, struct pcontact *_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/ims_usrloc_pcscf/hslot.h b/src/modules/ims_usrloc_pcscf/hslot.h index 522bee383a6..3d67073a433 100644 --- a/src/modules/ims_usrloc_pcscf/hslot.h +++ b/src/modules/ims_usrloc_pcscf/hslot.h @@ -56,22 +56,23 @@ struct udomain; struct pcontact; -typedef struct hslot { - int n; /*!< Number of elements in the collision slot */ - struct pcontact* first; /*!< First element in the list */ - struct pcontact* 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 pcontact *first; /*!< First element in the list */ + struct pcontact *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; -void init_slot(struct udomain* _d, hslot_t* _s, int n); -void deinit_slot(hslot_t* _s); -void slot_add(hslot_t* _s, struct pcontact* _r); -void slot_rem(hslot_t* _s, struct pcontact* _r); +void init_slot(struct udomain *_d, hslot_t *_s, int n); +void deinit_slot(hslot_t *_s); +void slot_add(hslot_t *_s, struct pcontact *_r); +void slot_rem(hslot_t *_s, struct pcontact *_r); int ul_init_locks(void); void ul_unlock_locks(void); void ul_destroy_locks(void); diff --git a/src/modules/ims_usrloc_pcscf/ims_usrloc_pcscf_mod.c b/src/modules/ims_usrloc_pcscf/ims_usrloc_pcscf_mod.c index 8e7c74ce632..d90cb05b5d2 100644 --- a/src/modules/ims_usrloc_pcscf/ims_usrloc_pcscf_mod.c +++ b/src/modules/ims_usrloc_pcscf/ims_usrloc_pcscf_mod.c @@ -40,19 +40,19 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ - + #include #include "ims_usrloc_pcscf_mod.h" #include "../../core/sr_module.h" #include "../../core/dprint.h" #include "../../core/rpc_lookup.h" -#include "../../core/timer.h" /* register_timer */ +#include "../../core/timer.h" /* register_timer */ #include "../../core/globals.h" -#include "../../core/ut.h" /* str_init */ -#include "dlist.h" /* register_udomain */ -#include "udomain.h" /* {insert,delete,get,release}_urecord */ -#include "pcontact.h" /* {insert,delete,get}_ucontact */ +#include "../../core/ut.h" /* str_init */ +#include "dlist.h" /* register_udomain */ +#include "udomain.h" /* {insert,delete,get,release}_urecord */ +#include "pcontact.h" /* {insert,delete,get}_ucontact */ #include "ul_rpc.h" #include "ul_callback.h" #include "usrloc.h" @@ -66,92 +66,95 @@ static FILE *debug_file; int audit_expired_pcontacts_timeout = 40; int audit_expired_pcontacts_interval = 60; -static void audit_usrloc_expired_pcontacts_timer(unsigned int ticks, void* param); +static void audit_usrloc_expired_pcontacts_timer( + unsigned int ticks, void *param); -static int mod_init(void); /*!< Module initialization function */ -static void destroy(void); /*!< Module destroy function */ -static void timer(unsigned int ticks, void* param); /*!< Timer handler */ -static int child_init(int rank); /*!< Per-child init function */ +static int mod_init(void); /*!< Module initialization function */ +static void destroy(void); /*!< Module destroy function */ +static void timer(unsigned int ticks, void *param); /*!< Timer handler */ +static int child_init(int rank); /*!< Per-child init function */ -extern int bind_usrloc(usrloc_api_t* api); +extern int bind_usrloc(usrloc_api_t *api); extern int ul_locks_no; -int expires_grace = 3600; //default is a grace period of 1 hour - after this contact is removed from P +int expires_grace = + 3600; //default is a grace period of 1 hour - after this contact is removed from P /* * Module parameters and their default values */ str usrloc_debug_file = str_init(DEFAULT_DBG_FILE); -int usrloc_debug = 0; -int ul_hash_size = 9; +int usrloc_debug = 0; +int ul_hash_size = 9; int ims_ulp_init_flag = 0; -str db_url = str_init(DEFAULT_DB_URL); /*!< Database URL */ -int timer_interval = 60; /*!< Timer interval in seconds */ -int db_mode = 0; /*!< Database sync scheme: 0-no db, 1-write through, 2-write back, 3-only db */ -int ul_fetch_rows = 2000; -int match_contact_host_port = 1; /*!< Should we match contact just based on rui host and port*/ - -db1_con_t* ul_dbh = 0; +str db_url = str_init(DEFAULT_DB_URL); /*!< Database URL */ +int timer_interval = 60; /*!< Timer interval in seconds */ +int db_mode = + 0; /*!< Database sync scheme: 0-no db, 1-write through, 2-write back, 3-only db */ +int ul_fetch_rows = 2000; +int match_contact_host_port = + 1; /*!< Should we match contact just based on rui host and port*/ + +db1_con_t *ul_dbh = 0; db_func_t ul_dbf; /*! \brief * Exported functions */ static cmd_export_t cmds[] = { - {"ul_bind_ims_usrloc_pcscf", (cmd_function)bind_usrloc, 1, 0, 0, 0}, - {0, 0, 0, 0, 0, 0} -}; + {"ul_bind_ims_usrloc_pcscf", (cmd_function)bind_usrloc, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}}; /*! \brief * Exported parameters */ -static param_export_t params[] = { - {"hash_size", INT_PARAM, &ul_hash_size }, - {"timer_interval", INT_PARAM, &timer_interval }, - {"usrloc_debug_file", PARAM_STR, &usrloc_debug_file }, - {"enable_debug_file", INT_PARAM, &usrloc_debug}, - - {"db_url", PARAM_STR, &db_url }, - {"timer_interval", INT_PARAM, &timer_interval }, - {"db_mode", INT_PARAM, &db_mode }, - - {"match_contact_host_port", INT_PARAM, &match_contact_host_port }, - {"audit_expired_pcontacts_timeout", INT_PARAM, &audit_expired_pcontacts_timeout }, - {"audit_expired_pcontacts_interval", INT_PARAM, &audit_expired_pcontacts_interval }, - {"expires_grace", INT_PARAM, &expires_grace }, - - {0, 0, 0} -}; - -stat_export_t mod_stats[] = { - {"registered_contacts" , STAT_IS_FUNC, (stat_var**)get_number_of_contacts }, - {"registered_impus" , STAT_IS_FUNC, (stat_var**)get_number_of_impu }, - {"expired_contacts" , STAT_IS_FUNC, (stat_var**)get_number_of_expired }, - {0,0,0} -}; +static param_export_t params[] = {{"hash_size", INT_PARAM, &ul_hash_size}, + {"timer_interval", INT_PARAM, &timer_interval}, + {"usrloc_debug_file", PARAM_STR, &usrloc_debug_file}, + {"enable_debug_file", INT_PARAM, &usrloc_debug}, + + {"db_url", PARAM_STR, &db_url}, + {"timer_interval", INT_PARAM, &timer_interval}, + {"db_mode", INT_PARAM, &db_mode}, + + {"match_contact_host_port", INT_PARAM, &match_contact_host_port}, + {"audit_expired_pcontacts_timeout", INT_PARAM, + &audit_expired_pcontacts_timeout}, + {"audit_expired_pcontacts_interval", INT_PARAM, + &audit_expired_pcontacts_interval}, + {"expires_grace", INT_PARAM, &expires_grace}, + + {0, 0, 0}}; + +stat_export_t mod_stats[] = {{"registered_contacts", STAT_IS_FUNC, + (stat_var **)get_number_of_contacts}, + {"registered_impus", STAT_IS_FUNC, (stat_var **)get_number_of_impu}, + {"expired_contacts", STAT_IS_FUNC, (stat_var **)get_number_of_expired}, + {0, 0, 0}}; struct module_exports exports = { - "ims_usrloc_pcscf", - DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, /* exported functions */ - params, /* export parameters */ - 0, /* exported RPC functions */ - 0, /* exported pseudo-variables */ - 0, /* response·function */ - mod_init, /* module initialization function */ - child_init, /* per-child·init·function*/ - destroy /* destroy function */ + "ims_usrloc_pcscf", DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, /* exported functions */ + params, /* export parameters */ + 0, /* exported RPC functions */ + 0, /* exported pseudo-variables */ + 0, /* response·function */ + mod_init, /* module initialization function */ + child_init, /* per-child·init·function*/ + destroy /* destroy function */ }; /*! \brief * Module initialization function */ -static int mod_init(void) { +static int mod_init(void) +{ - if (usrloc_debug){ - LM_INFO("Logging usrloc records to %.*s\n", usrloc_debug_file.len, usrloc_debug_file.s); + if(usrloc_debug) { + LM_INFO("Logging usrloc records to %.*s\n", usrloc_debug_file.len, + usrloc_debug_file.s); debug_file = fopen(usrloc_debug_file.s, "a"); fprintf(debug_file, "starting\n"); fflush(debug_file); @@ -159,25 +162,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 << ul_hash_size; ul_locks_no = ul_hash_size; - if (ul_init_locks() != 0) { + if(ul_init_locks() != 0) { LM_ERR("locks array initialization failed\n"); return -1; } /* Regsiter RPC */ - if (rpc_register_array(ul_rpc) != 0) { + if(rpc_register_array(ul_rpc) != 0) { LM_ERR("failed to register RPC commands\n"); return -1; } @@ -186,33 +189,34 @@ static int mod_init(void) { LM_DBG("Registering cache timer"); register_timer(timer, 0, timer_interval); - /* Register audit timer */ - if (db_mode == DB_ONLY) - register_timer(audit_usrloc_expired_pcontacts_timer, 0, audit_expired_pcontacts_interval); + /* Register audit timer */ + if(db_mode == DB_ONLY) + register_timer(audit_usrloc_expired_pcontacts_timer, 0, + audit_expired_pcontacts_interval); /* init the callbacks list */ - if (init_ulcb_list() < 0) { + if(init_ulcb_list() < 0) { LM_ERR("usrloc/callbacks initialization failed\n"); return -1; } /* Shall we use database ? */ - if (db_mode != NO_DB) { /* Yes */ - if(ul_fetch_rows<=0) { + if(db_mode != NO_DB) { /* Yes */ + if(ul_fetch_rows <= 0) { LM_ERR("invalid fetch_rows number '%d'\n", ul_fetch_rows); return -1; } - if (db_bind_mod(&db_url, &ul_dbf) < 0) { /* Find database module */ - LM_ERR("failed to bind database module\n"); - return -1; - } - if (!DB_CAPABILITY(ul_dbf, DB_CAP_ALL)) { - LM_ERR("database module does not implement all functions" - " needed by the module\n"); - return -1; - } + if(db_bind_mod(&db_url, &ul_dbf) < 0) { /* Find database module */ + LM_ERR("failed to bind database module\n"); + return -1; + } + if(!DB_CAPABILITY(ul_dbf, DB_CAP_ALL)) { + LM_ERR("database module does not implement all functions" + " needed by the module\n"); + return -1; + } LM_DBG("Running in DB mode %i\n", db_mode); } @@ -223,40 +227,41 @@ static int mod_init(void) { static int child_init(int _rank) { - dlist_t* ptr; + dlist_t *ptr; /* connecting to DB ? */ - switch (db_mode) { + switch(db_mode) { case NO_DB: return 0; case WRITE_THROUGH: /* connect to db only from SIP workers, TIMER and MAIN processes */ - if (_rank<=0 && _rank!=PROC_TIMER && _rank!=PROC_MAIN) + if(_rank <= 0 && _rank != PROC_TIMER && _rank != PROC_MAIN) return 0; break; case WRITE_BACK: /* connect to db only from TIMER (for flush), from MAIN (for * final flush() and from child 1 for preload */ - if (_rank!=PROC_TIMER && _rank!=PROC_MAIN && _rank!=PROC_SIPINIT) + if(_rank != PROC_TIMER && _rank != PROC_MAIN + && _rank != PROC_SIPINIT) return 0; break; } LM_DBG("Connecting to usrloc_pcscf DB for rank %d\n", _rank); - ul_dbh = ul_dbf.init(&db_url); /* Get a database connection per child */ + ul_dbh = ul_dbf.init(&db_url); /* Get a database connection per child */ - if (!ul_dbh) { - LM_ERR("child(%d): failed to connect to database\n", _rank); - return -1; - } + if(!ul_dbh) { + LM_ERR("child(%d): failed to connect to database\n", _rank); + return -1; + } /* _rank==PROC_SIPINIT is used even when fork is disabled */ - if (_rank==PROC_SIPINIT && db_mode!=DB_ONLY) { + if(_rank == PROC_SIPINIT && db_mode != DB_ONLY) { // if cache is used, populate domains from DB - for( ptr=root ; ptr ; ptr=ptr->next) { + for(ptr = root; ptr; ptr = ptr->next) { LM_DBG("Preloading domain %.*s\n", ptr->name.len, ptr->name.s); - if (preload_udomain(ul_dbh, ptr->d) < 0) { - LM_ERR("child(%d): failed to preload domain '%.*s'\n", - _rank, ptr->name.len, ZSW(ptr->name.s)); + if(preload_udomain(ul_dbh, ptr->d) < 0) { + LM_ERR("child(%d): failed to preload domain '%.*s'\n", _rank, + ptr->name.len, ZSW(ptr->name.s)); return -1; } } @@ -279,34 +284,36 @@ static void destroy(void) free_service_route_buf(); free_impu_buf(); - if (db_mode) + if(db_mode) destroy_db(); - if (cbp_qos) - shm_free(cbp_qos); + if(cbp_qos) + shm_free(cbp_qos); - if (cbp_registrar) - shm_free(cbp_registrar); + if(cbp_registrar) + shm_free(cbp_registrar); } -static void audit_usrloc_expired_pcontacts_timer(unsigned int ticks, void* param) { +static void audit_usrloc_expired_pcontacts_timer( + unsigned int ticks, void *param) +{ - audit_usrloc_expired_pcontacts(root->d); + audit_usrloc_expired_pcontacts(root->d); } /*! \brief * Timer handler */ -static void timer(unsigned int ticks, void* param) { +static void timer(unsigned int ticks, void *param) +{ LM_DBG("Syncing cache\n"); - if (usrloc_debug) { + if(usrloc_debug) { print_all_udomains(debug_file); fflush(debug_file); } - if (synchronize_all_udomains() != 0) { + if(synchronize_all_udomains() != 0) { LM_ERR("synchronizing cache failed\n"); } } - diff --git a/src/modules/ims_usrloc_pcscf/pcontact.c b/src/modules/ims_usrloc_pcscf/pcontact.c index 550a9b441ad..3c669c0b4af 100644 --- a/src/modules/ims_usrloc_pcscf/pcontact.c +++ b/src/modules/ims_usrloc_pcscf/pcontact.c @@ -64,13 +64,12 @@ extern int expires_grace; /*! retransmission detection interval in seconds */ int cseq_delay = 20; -void insert_ppublic(struct pcontact* _c, ppublic_t* _p) +void insert_ppublic(struct pcontact *_c, ppublic_t *_p) { - LM_DBG("linking IMPU <%.*s> to contact <%.*s>\n", - _p->public_identity.len,_p->public_identity.s, - _c->aor.len, _c->aor.s); + LM_DBG("linking IMPU <%.*s> to contact <%.*s>\n", _p->public_identity.len, + _p->public_identity.s, _c->aor.len, _c->aor.s); - if (_c->head == 0) {//first entry + if(_c->head == 0) { //first entry _c->head = _c->tail = _p; } else { _p->prev = _c->tail; @@ -78,18 +77,18 @@ void insert_ppublic(struct pcontact* _c, ppublic_t* _p) _c->tail = _p; } } -int new_ppublic(str* public_identity, int is_default, ppublic_t** _p) +int new_ppublic(str *public_identity, int is_default, ppublic_t **_p) { - *_p = (ppublic_t*)shm_malloc(sizeof(ppublic_t)); - if (!*_p) { + *_p = (ppublic_t *)shm_malloc(sizeof(ppublic_t)); + if(!*_p) { LM_ERR("no more shm memory\n"); return -1; } - (*_p)->next=(*_p)->prev=0; - (*_p)->public_identity.s = (char*)shm_malloc(public_identity->len); - if (!(*_p)->public_identity.s) { + (*_p)->next = (*_p)->prev = 0; + (*_p)->public_identity.s = (char *)shm_malloc(public_identity->len); + if(!(*_p)->public_identity.s) { LM_ERR("no more shm memory\n"); - if (*_p) { + if(*_p) { shm_free(*_p); } return -1; @@ -101,11 +100,11 @@ int new_ppublic(str* public_identity, int is_default, ppublic_t** _p) return 0; } -void free_ppublic(ppublic_t* _p) +void free_ppublic(ppublic_t *_p) { - if (!_p) + if(!_p) return; - if (_p->public_identity.s) { + if(_p->public_identity.s) { shm_free(_p->public_identity.s); } shm_free(_p); @@ -116,117 +115,126 @@ void free_ppublic(ppublic_t* _p) // Function - parse_sec_agree() // goto label - cleanup // Keep them in sync! -void free_security(security_t* _p) +void free_security(security_t *_p) { - if (!_p) { - return; - } + if(!_p) { + return; + } - if(_p->sec_header.s) { - shm_free(_p->sec_header.s); + if(_p->sec_header.s) { + shm_free(_p->sec_header.s); } - switch (_p->type) - { - case SECURITY_IPSEC: - if(_p->data.ipsec){ - if(_p->data.ipsec->ealg.s) shm_free(_p->data.ipsec->ealg.s); - if(_p->data.ipsec->r_ealg.s) shm_free(_p->data.ipsec->r_ealg.s); - if(_p->data.ipsec->ck.s) shm_free(_p->data.ipsec->ck.s); - if(_p->data.ipsec->alg.s) shm_free(_p->data.ipsec->alg.s); - if(_p->data.ipsec->r_alg.s) shm_free(_p->data.ipsec->r_alg.s); - if(_p->data.ipsec->ik.s) shm_free(_p->data.ipsec->ik.s); - if(_p->data.ipsec->prot.s) shm_free(_p->data.ipsec->prot.s); - if(_p->data.ipsec->mod.s) shm_free(_p->data.ipsec->mod.s); + switch(_p->type) { + case SECURITY_IPSEC: + if(_p->data.ipsec) { + if(_p->data.ipsec->ealg.s) + shm_free(_p->data.ipsec->ealg.s); + if(_p->data.ipsec->r_ealg.s) + shm_free(_p->data.ipsec->r_ealg.s); + if(_p->data.ipsec->ck.s) + shm_free(_p->data.ipsec->ck.s); + if(_p->data.ipsec->alg.s) + shm_free(_p->data.ipsec->alg.s); + if(_p->data.ipsec->r_alg.s) + shm_free(_p->data.ipsec->r_alg.s); + if(_p->data.ipsec->ik.s) + shm_free(_p->data.ipsec->ik.s); + if(_p->data.ipsec->prot.s) + shm_free(_p->data.ipsec->prot.s); + if(_p->data.ipsec->mod.s) + shm_free(_p->data.ipsec->mod.s); shm_free(_p->data.ipsec); } - break; + break; - case SECURITY_TLS: + case SECURITY_TLS: if(_p->data.tls) { shm_free(_p->data.tls); } - break; - - case SECURITY_NONE: - //Nothing to deallocate - break; - } - - shm_free(_p); + break; + + case SECURITY_NONE: + //Nothing to deallocate + break; + } + + shm_free(_p); } -int new_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci, struct pcontact** _c) +int new_pcontact(struct udomain *_d, str *_contact, struct pcontact_info *_ci, + struct pcontact **_c) { - int i, has_rinstance=0; - ppublic_t* ppublic_ptr; + int i, has_rinstance = 0; + ppublic_t *ppublic_ptr; int is_default = 1, params_len; struct sip_uri sip_uri; - char* p, *params, *sep; - str rinstance = {0,0}; - - *_c = (pcontact_t*)shm_malloc(sizeof(pcontact_t) + _contact->len + _ci->received_host.len + _ci->via_host.len); - if (*_c == 0) { + char *p, *params, *sep; + str rinstance = {0, 0}; + + *_c = (pcontact_t *)shm_malloc(sizeof(pcontact_t) + _contact->len + + _ci->received_host.len + + _ci->via_host.len); + if(*_c == 0) { LM_ERR("no more shared memory\n"); return -1; } memset(*_c, 0, sizeof(pcontact_t)); LM_DBG("New contact [<%.*s>] with %d associated IMPUs in state: [%s]\n", - _contact->len, _contact->s, - _ci->num_public_ids, + _contact->len, _contact->s, _ci->num_public_ids, reg_state_to_string(_ci->reg_state)); - - - p = (char*)((struct pcontact*)(*_c) + 1); - (*_c)->aor.s = p; + + p = (char *)((struct pcontact *)(*_c) + 1); + (*_c)->aor.s = p; memcpy(p, _contact->s, _contact->len); - p += _contact->len; + p += _contact->len; (*_c)->aor.len = _contact->len; - (*_c)->domain = (str*)_d; + (*_c)->domain = (str *)_d; - if (parse_uri((*_c)->aor.s, (*_c)->aor.len, &sip_uri) != 0) { - LM_ERR("unable to determine contact host from uri [%.*s\n", (*_c)->aor.len, (*_c)->aor.s); + if(parse_uri((*_c)->aor.s, (*_c)->aor.len, &sip_uri) != 0) { + LM_ERR("unable to determine contact host from uri [%.*s\n", + (*_c)->aor.len, (*_c)->aor.s); shm_free((*_c)->aor.s); shm_free(*_c); *_c = 0; return -2; } - - /* is there an rinstance param */ - LM_DBG("checking for rinstance"); - /*check for alias - NAT */ - params = sip_uri.sip_params.s; - params_len = sip_uri.sip_params.len; - while (params_len >= RINSTANCE_LEN) { - if (strncmp(params, RINSTANCE, RINSTANCE_LEN) == 0) { - has_rinstance = 1; - break; - } - sep = memchr(params, 59 /* ; */, params_len); - if (sep == NULL) { - LM_DBG("no rinstance param\n"); - break; - } else { - params_len = params_len - (sep - params + 1); - params = sep + 1; - } - } - if (has_rinstance) { - rinstance.s = params + RINSTANCE_LEN; - rinstance.len = params_len - RINSTANCE_LEN; - sep = (char*)memchr(rinstance.s, 59 /* ; */, rinstance.len); - if (sep != NULL){ - rinstance.len = (sep-rinstance.s); - } - } - (*_c)->rinstance.s = rinstance.s; - (*_c)->rinstance.len = rinstance.len; + + /* is there an rinstance param */ + LM_DBG("checking for rinstance"); + /*check for alias - NAT */ + params = sip_uri.sip_params.s; + params_len = sip_uri.sip_params.len; + while(params_len >= RINSTANCE_LEN) { + if(strncmp(params, RINSTANCE, RINSTANCE_LEN) == 0) { + has_rinstance = 1; + break; + } + sep = memchr(params, 59 /* ; */, params_len); + if(sep == NULL) { + LM_DBG("no rinstance param\n"); + break; + } else { + params_len = params_len - (sep - params + 1); + params = sep + 1; + } + } + if(has_rinstance) { + rinstance.s = params + RINSTANCE_LEN; + rinstance.len = params_len - RINSTANCE_LEN; + sep = (char *)memchr(rinstance.s, 59 /* ; */, rinstance.len); + if(sep != NULL) { + rinstance.len = (sep - rinstance.s); + } + } + (*_c)->rinstance.s = rinstance.s; + (*_c)->rinstance.len = rinstance.len; (*_c)->contact_host.s = sip_uri.host.s; (*_c)->contact_host.len = sip_uri.host.len; - (*_c)->contact_port = sip_uri.port_no?sip_uri.port_no:5060; + (*_c)->contact_port = sip_uri.port_no ? sip_uri.port_no : 5060; (*_c)->contact_user.s = sip_uri.user.s; (*_c)->contact_user.len = sip_uri.user.len; @@ -234,95 +242,109 @@ int new_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci, s (*_c)->reg_state = _ci->reg_state; // Add received Info: - if (_ci->received_host.len > 0 && _ci->received_host.s) { - (*_c)->received_host.s = p; + if(_ci->received_host.len > 0 && _ci->received_host.s) { + (*_c)->received_host.s = p; memcpy(p, _ci->received_host.s, _ci->received_host.len); - p += _ci->received_host.len; + p += _ci->received_host.len; (*_c)->received_host.len = _ci->received_host.len; (*_c)->received_port = _ci->received_port; (*_c)->received_proto = _ci->received_proto; } - - if (_ci->via_host.len > 0 && _ci->via_host.s) { - (*_c)->via_host.s = p; - memcpy(p, _ci->via_host.s, _ci->via_host.len); - p += _ci->via_host.len; - (*_c)->via_host.len = _ci->via_host.len; - (*_c)->via_port = _ci->via_port; - (*_c)->via_proto = _ci->via_prot; - } - - (*_c)->aorhash = get_aor_hash(_d, &_ci->via_host, _ci->via_port, _ci->via_prot); + + if(_ci->via_host.len > 0 && _ci->via_host.s) { + (*_c)->via_host.s = p; + memcpy(p, _ci->via_host.s, _ci->via_host.len); + p += _ci->via_host.len; + (*_c)->via_host.len = _ci->via_host.len; + (*_c)->via_port = _ci->via_port; + (*_c)->via_proto = _ci->via_prot; + } + + (*_c)->aorhash = + get_aor_hash(_d, &_ci->via_host, _ci->via_port, _ci->via_prot); //setup public ids - for (i=0; i<_ci->num_public_ids; i++) { - if (i>0) is_default = 0; //only the first one is default - P-Associated-uri (first one is default) - if (new_ppublic(&_ci->public_ids[i], is_default, &ppublic_ptr)!=0) { + for(i = 0; i < _ci->num_public_ids; i++) { + if(i > 0) + is_default = + 0; //only the first one is default - P-Associated-uri (first one is default) + if(new_ppublic(&_ci->public_ids[i], is_default, &ppublic_ptr) != 0) { LM_ERR("unable to create new ppublic\n"); } else { insert_ppublic(*_c, ppublic_ptr); } } //add the service routes - if (_ci->num_service_routes > 0) { - (*_c)->service_routes = shm_malloc(_ci->num_service_routes * sizeof(str)); - if (!(*_c)->service_routes) { + if(_ci->num_service_routes > 0) { + (*_c)->service_routes = + shm_malloc(_ci->num_service_routes * sizeof(str)); + if(!(*_c)->service_routes) { LM_ERR("no more shm mem\n"); goto out_of_memory; } else { - for (i = 0; i < _ci->num_service_routes; i++) { - STR_SHM_DUP((*_c)->service_routes[i], _ci->service_routes[i], "new_pcontact"); + for(i = 0; i < _ci->num_service_routes; i++) { + STR_SHM_DUP((*_c)->service_routes[i], _ci->service_routes[i], + "new_pcontact"); } (*_c)->num_service_routes = _ci->num_service_routes; } } // add the rx session id - if ((_ci->rx_regsession_id) && (_ci->rx_regsession_id->len > 0) && (_ci->rx_regsession_id->s)) { + if((_ci->rx_regsession_id) && (_ci->rx_regsession_id->len > 0) + && (_ci->rx_regsession_id->s)) { (*_c)->rx_session_id.s = shm_malloc(_ci->rx_regsession_id->len); - if (!((*_c)->rx_session_id.s)) { + if(!((*_c)->rx_session_id.s)) { LM_ERR("no more shm mem\n"); goto out_of_memory; } - memcpy((*_c)->rx_session_id.s, _ci->rx_regsession_id->s, _ci->rx_regsession_id->len); + memcpy((*_c)->rx_session_id.s, _ci->rx_regsession_id->s, + _ci->rx_regsession_id->len); (*_c)->rx_session_id.len = _ci->rx_regsession_id->len; } - - LM_DBG("New contact host:port [%.*s:%d]\n", (*_c)->contact_host.len, (*_c)->contact_host.s, (*_c)->contact_port); - LM_DBG("New contact via host:port:proto: [%.*s:%d:%d]\n", (*_c)->via_host.len, (*_c)->via_host.s, (*_c)->via_port, (*_c)->via_proto); - LM_DBG("New contact received host:port:proto: [%.*s:%d:%d]\n", (*_c)->received_host.len, (*_c)->received_host.s, (*_c)->received_port, (*_c)->received_proto); - LM_DBG("New contact aorhash [%u]\n", (*_c)->aorhash); - + + LM_DBG("New contact host:port [%.*s:%d]\n", (*_c)->contact_host.len, + (*_c)->contact_host.s, (*_c)->contact_port); + LM_DBG("New contact via host:port:proto: [%.*s:%d:%d]\n", + (*_c)->via_host.len, (*_c)->via_host.s, (*_c)->via_port, + (*_c)->via_proto); + LM_DBG("New contact received host:port:proto: [%.*s:%d:%d]\n", + (*_c)->received_host.len, (*_c)->received_host.s, + (*_c)->received_port, (*_c)->received_proto); + LM_DBG("New contact aorhash [%u]\n", (*_c)->aorhash); + return 0; out_of_memory: return -1; } -void free_pcontact(pcontact_t* _c) { - ppublic_t* p, *tmp; +void free_pcontact(pcontact_t *_c) +{ + ppublic_t *p, *tmp; int i; - if (!_c) + if(!_c) return; /*free callbacks*/ - if (_c->cbs.first) { + if(_c->cbs.first) { destroy_ul_callbacks_list(_c->cbs.first); } LM_DBG("freeing pcontact: <%.*s>\n", _c->aor.len, _c->aor.s); //free ppublics p = _c->head; - while (p) { - LM_DBG("freeing linked IMPI: <%.*s>\n", p->public_identity.len, p->public_identity.s); + while(p) { + LM_DBG("freeing linked IMPI: <%.*s>\n", p->public_identity.len, + p->public_identity.s); tmp = p->next; free_ppublic(p); p = tmp; } //free service_routes - if (_c->service_routes) { //remove old service routes - for (i = 0; i < _c->num_service_routes; i++) { - if (_c->service_routes[i].s) + if(_c->service_routes) { //remove old service routes + for(i = 0; i < _c->num_service_routes; i++) { + if(_c->service_routes[i].s) shm_free(_c->service_routes[i].s); } shm_free(_c->service_routes); @@ -330,115 +352,110 @@ void free_pcontact(pcontact_t* _c) { _c->num_service_routes = 0; } - // free_security() checks for NULL ptr - free_security(_c->security_temp); - free_security(_c->security); + // free_security() checks for NULL ptr + free_security(_c->security_temp); + free_security(_c->security); - if (_c->rx_session_id.len > 0 && _c->rx_session_id.s) + if(_c->rx_session_id.len > 0 && _c->rx_session_id.s) shm_free(_c->rx_session_id.s); shm_free(_c); } -static inline void nodb_timer(pcontact_t* _c) +static inline void nodb_timer(pcontact_t *_c) { LM_DBG("Running nodb timer on <%.*s>, " - "Reg state: %s, " - "Expires: %d, " - "Expires in: %d seconds, " - "Received: %.*s:%d, " - "Path: %.*s, " - "Proto: %d, " - "Hash: %u, " - "Slot: %u\n", - _c->aor.len, _c->aor.s, - reg_state_to_string(_c->reg_state), - (int)_c->expires, - (int)(_c->expires - time(NULL)), - _c->received_host.len, _c->received_host.s, - _c->received_port, - _c->path.len, _c->path.s, - _c->received_proto, - _c->aorhash, - _c->sl); + "Reg state: %s, " + "Expires: %d, " + "Expires in: %d seconds, " + "Received: %.*s:%d, " + "Path: %.*s, " + "Proto: %d, " + "Hash: %u, " + "Slot: %u\n", + _c->aor.len, _c->aor.s, reg_state_to_string(_c->reg_state), + (int)_c->expires, (int)(_c->expires - time(NULL)), + _c->received_host.len, _c->received_host.s, _c->received_port, + _c->path.len, _c->path.s, _c->received_proto, _c->aorhash, _c->sl); get_act_time(); - - - if ((_c->expires - act_time) + expires_grace <= 0) {//we've allowed some grace time TODO: add as parameter - //if ((_c->expires - act_time) <= -10) {//we've allowed some grace time TODO: add as parameter - LM_DBG("pcscf contact <%.*s> has expired and will be removed\n", _c->aor.len, _c->aor.s); - - if (db_mode == DB_ONLY){ - if (db_load_pcontact(_c->slot->d, &_c->aor, 0/*insert_cache*/, &_c, NULL)){ - if ((_c->reg_state == PCONTACT_REG_PENDING_AAR) || (_c->reg_state == PCONTACT_REG_PENDING)){ - //we do not need expires_grace here as contact is not registered in scscf - if ((_c->expires - act_time) <= 0) { - _c->reg_state = PCONTACT_DEREG_PENDING_PUBLISH; - delete_pcontact(_c->slot->d, _c); - return; - } - } - else{ - if ((_c->expires - act_time) + expires_grace >= 0) { - return; - } - } - } - else{ - // not found in DB: better delete in cache also - update_stat(_c->slot->d->expired, 1); - mem_delete_pcontact(_c->slot->d, _c); - return; - } - - _c->reg_state = PCONTACT_DEREG_PENDING_PUBLISH; - if(db_delete_pcontact(_c) !=0) { - LM_ERR("Error deleting ims_usrloc_pcscf record in DB"); - } //delete contact in DB to not process this contact in several units - if (exists_ulcb_type(PCSCF_CONTACT_UPDATE)) { - run_ul_callbacks(PCSCF_CONTACT_UPDATE, _c); - LM_INFO("pcscf contact <%.*s> has expired - sending PUBLISH\n", _c->aor.len, _c->aor.s); - return; - } - } - else{ - if (exists_ulcb_type(PCSCF_CONTACT_EXPIRE)) { - run_ul_callbacks(PCSCF_CONTACT_EXPIRE, _c); - } - - if (db_mode == WRITE_THROUGH && db_delete_pcontact(_c) != 0) { - LM_ERR("Error deleting ims_usrloc_pcscf record in DB"); - } - - update_stat(_c->slot->d->expired, 1); - mem_delete_pcontact(_c->slot->d, _c); - return; + + + if((_c->expires - act_time) + expires_grace + <= 0) { //we've allowed some grace time TODO: add as parameter + //if ((_c->expires - act_time) <= -10) {//we've allowed some grace time TODO: add as parameter + LM_DBG("pcscf contact <%.*s> has expired and will be removed\n", + _c->aor.len, _c->aor.s); + + if(db_mode == DB_ONLY) { + if(db_load_pcontact( + _c->slot->d, &_c->aor, 0 /*insert_cache*/, &_c, NULL)) { + if((_c->reg_state == PCONTACT_REG_PENDING_AAR) + || (_c->reg_state == PCONTACT_REG_PENDING)) { + //we do not need expires_grace here as contact is not registered in scscf + if((_c->expires - act_time) <= 0) { + _c->reg_state = PCONTACT_DEREG_PENDING_PUBLISH; + delete_pcontact(_c->slot->d, _c); + return; + } + } else { + if((_c->expires - act_time) + expires_grace >= 0) { + return; + } + } + } else { + // not found in DB: better delete in cache also + update_stat(_c->slot->d->expired, 1); + mem_delete_pcontact(_c->slot->d, _c); + return; + } + + _c->reg_state = PCONTACT_DEREG_PENDING_PUBLISH; + if(db_delete_pcontact(_c) != 0) { + LM_ERR("Error deleting ims_usrloc_pcscf record in DB"); + } //delete contact in DB to not process this contact in several units + if(exists_ulcb_type(PCSCF_CONTACT_UPDATE)) { + run_ul_callbacks(PCSCF_CONTACT_UPDATE, _c); + LM_INFO("pcscf contact <%.*s> has expired - sending PUBLISH\n", + _c->aor.len, _c->aor.s); + return; + } + } else { + if(exists_ulcb_type(PCSCF_CONTACT_EXPIRE)) { + run_ul_callbacks(PCSCF_CONTACT_EXPIRE, _c); + } + + if(db_mode == WRITE_THROUGH && db_delete_pcontact(_c) != 0) { + LM_ERR("Error deleting ims_usrloc_pcscf record in DB"); + } + + update_stat(_c->slot->d->expired, 1); + mem_delete_pcontact(_c->slot->d, _c); + return; } } //TODO: this is just for tmp debugging -// p = _c->head; -// while (p) { -// if (p->is_default) -// LM_DBG("public identity %i (default): <%.*s>\n", i, p->public_identity.len, p->public_identity.s); -// else -// LM_DBG("public identity %i: <%.*s>\n", i, p->public_identity.len, p->public_identity.s); -// i++; -// p=p->next; -// } -// -// LM_DBG("There are %i service routes as follows:\n", _c->num_service_routes); -// for (i=0; i<_c->num_service_routes; i++) { -// LM_DBG("service route %i: <%.*s>\n", i+1, _c->service_routes[i].len, _c->service_routes[i].s); -// } + // p = _c->head; + // while (p) { + // if (p->is_default) + // LM_DBG("public identity %i (default): <%.*s>\n", i, p->public_identity.len, p->public_identity.s); + // else + // LM_DBG("public identity %i: <%.*s>\n", i, p->public_identity.len, p->public_identity.s); + // i++; + // p=p->next; + // } + // + // LM_DBG("There are %i service routes as follows:\n", _c->num_service_routes); + // for (i=0; i<_c->num_service_routes; i++) { + // LM_DBG("service route %i: <%.*s>\n", i+1, _c->service_routes[i].len, _c->service_routes[i].s); + // } } -void timer_pcontact(pcontact_t* _r) +void timer_pcontact(pcontact_t *_r) { nodb_timer(_r); } -void print_pcontact(FILE* _f, pcontact_t* _r) +void print_pcontact(FILE *_f, pcontact_t *_r) { - } diff --git a/src/modules/ims_usrloc_pcscf/pcontact.h b/src/modules/ims_usrloc_pcscf/pcontact.h index ab652addaf7..d0d62d08ba6 100644 --- a/src/modules/ims_usrloc_pcscf/pcontact.h +++ b/src/modules/ims_usrloc_pcscf/pcontact.h @@ -53,25 +53,29 @@ #include "../../core/qvalue.h" #include "usrloc.h" -struct hslot; /*!< Hash table slot */ +struct hslot; /*!< Hash table slot */ -void insert_ppublic(struct pcontact* _c, ppublic_t* _p); -int new_ppublic(str* public_identity, int is_default, ppublic_t** _p); -void free_ppublic(ppublic_t* _p); +void insert_ppublic(struct pcontact *_c, ppublic_t *_p); +int new_ppublic(str *public_identity, int is_default, ppublic_t **_p); +void free_ppublic(ppublic_t *_p); -int new_pcontact(/*str* _dom, str* public_identity, int reg_state, int barring, ims_subscription** s, str* ccf1, str* ccf2, str* ecf1, str* ecf2, impurecord_t** _r*/); -void free_pcontact(pcontact_t* _c); -void print_pcontact(FILE* _f, pcontact_t* _r); -ppublic_t* mem_insert_ppublic(pcontact_t* _r/*, str* _c, ucontact_info_t* _ci*/); -void mem_remove_ppublic(pcontact_t* _r/*, ucontact_t* _c*/); -void mem_delete_ppublic(pcontact_t* _r/*, ucontact_t* _c*/); -void timer_pcontact(pcontact_t* _r); -int delete_ppublic(pcontact_t* _r/*, struct ucontact* _c*/); -int get_ppublic(pcontact_t* _r); -int aor_to_contact(str* aor, str* contact); -unsigned int get_hash_slot(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto); -unsigned int get_aor_hash(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto); +int new_pcontact( + /*str* _dom, str* public_identity, int reg_state, int barring, ims_subscription** s, str* ccf1, str* ccf2, str* ecf1, str* ecf2, impurecord_t** _r*/); +void free_pcontact(pcontact_t *_c); +void print_pcontact(FILE *_f, pcontact_t *_r); +ppublic_t *mem_insert_ppublic( + pcontact_t *_r /*, str* _c, ucontact_info_t* _ci*/); +void mem_remove_ppublic(pcontact_t *_r /*, ucontact_t* _c*/); +void mem_delete_ppublic(pcontact_t *_r /*, ucontact_t* _c*/); +void timer_pcontact(pcontact_t *_r); +int delete_ppublic(pcontact_t *_r /*, struct ucontact* _c*/); +int get_ppublic(pcontact_t *_r); +int aor_to_contact(str *aor, str *contact); +unsigned int get_hash_slot(udomain_t *_d, str *via_host, + unsigned short via_port, unsigned short via_proto); +unsigned int get_aor_hash(udomain_t *_d, str *via_host, unsigned short via_port, + unsigned short via_proto); #endif diff --git a/src/modules/ims_usrloc_pcscf/reginfo.c b/src/modules/ims_usrloc_pcscf/reginfo.c index 38f36dbb591..e95ce208e71 100644 --- a/src/modules/ims_usrloc_pcscf/reginfo.c +++ b/src/modules/ims_usrloc_pcscf/reginfo.c @@ -44,7 +44,3 @@ */ #include "reginfo.h" - - - - diff --git a/src/modules/ims_usrloc_pcscf/reginfo.h b/src/modules/ims_usrloc_pcscf/reginfo.h index f0a3f84742e..5fe28da9b6c 100644 --- a/src/modules/ims_usrloc_pcscf/reginfo.h +++ b/src/modules/ims_usrloc_pcscf/reginfo.h @@ -49,23 +49,24 @@ #include "../../core/locking.h" /** reg Subscription Structure */ -typedef struct _reginfo_subscription { +typedef struct _reginfo_subscription +{ unsigned int hash; - str req_uri; /**< public id of the user, same thing for To: */ - int duration; /**< duration of subscription */ - time_t expires; /**< time of expiration */ - char attempts_left; /**< number of unsuccessful attempts to subscribe*/ + str req_uri; /**< public id of the user, same thing for To: */ + int duration; /**< duration of subscription */ + time_t expires; /**< time of expiration */ + char attempts_left; /**< number of unsuccessful attempts to subscribe*/ struct _reginfo_subscription *next, *prev; } reginfo_subscription; /** Subscription list */ -typedef struct { - gen_lock_t *lock; /**< lock for subscription list operations */ - reginfo_subscription *head; /**< first subscription in the list */ - reginfo_subscription *tail; /**< last subscription in the list */ +typedef struct +{ + gen_lock_t *lock; /**< lock for subscription list operations */ + reginfo_subscription *head; /**< first subscription in the list */ + reginfo_subscription *tail; /**< last subscription in the list */ } reginfo_subscription_hash_slot; - #endif /* REGINFO_H_ */ diff --git a/src/modules/ims_usrloc_pcscf/udomain.c b/src/modules/ims_usrloc_pcscf/udomain.c index 97fc3fbe97c..19ad91fe3b0 100644 --- a/src/modules/ims_usrloc_pcscf/udomain.c +++ b/src/modules/ims_usrloc_pcscf/udomain.c @@ -53,7 +53,7 @@ #include "../../lib/srdb1/db.h" #include "../../core/socket_info.h" #include "../../core/ut.h" -#include "ims_usrloc_pcscf_mod.h" /* usrloc module parameters */ +#include "ims_usrloc_pcscf_mod.h" /* usrloc module parameters */ #include "usrloc.h" #include "utime.h" #include "ul_callback.h" @@ -69,29 +69,29 @@ extern int db_mode_ext; extern int match_contact_host_port; #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; } #endif -int new_udomain(str* _n, int _s, udomain_t** _d) +int new_udomain(str *_n, int _s, udomain_t **_d) { int i; #ifdef STATISTICS @@ -102,15 +102,15 @@ int new_udomain(str* _n, int _s, udomain_t** _d) * 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; } @@ -125,13 +125,16 @@ int new_udomain(str* _n, int _s, udomain_t** _d) #ifdef STATISTICS /* register the statistics */ - 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)->expired, STAT_SHM_NAME)!=0 ) { + if((name = build_stat_name(_n, "expires")) == 0 + || register_stat("usrloc", name, &(*_d)->expired, STAT_SHM_NAME) + != 0) { LM_ERR("failed to add stat variable\n"); goto error2; } @@ -148,11 +151,11 @@ int new_udomain(str* _n, int _s, udomain_t** _d) return -1; } -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); @@ -163,23 +166,22 @@ void free_udomain(udomain_t* _d) shm_free(_d); } -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 pcontact* r; + int max = 0, slot = 0, n = 0; + struct pcontact *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) { @@ -192,12 +194,12 @@ void print_udomain(FILE* _f, udomain_t* _d) } -inline int time2str(time_t _v, char* _s, int* _l) +inline int time2str(time_t _v, char *_s, int *_l) { - struct tm* t; + struct tm *t; int l; - if ((!_s) || (!_l) || (*_l < 2)) { + if((!_s) || (!_l) || (*_l < 2)) { LM_ERR("Invalid parameter value\n"); return -1; } @@ -206,9 +208,9 @@ inline int time2str(time_t _v, char* _s, int* _l) /* Convert time_t structure to format accepted by the database */ t = localtime(&_v); - l = strftime(_s, *_l -1, "%Y-%m-%d %H:%M:%S", t); + l = strftime(_s, *_l - 1, "%Y-%m-%d %H:%M:%S", t); - if (l == 0) { + if(l == 0) { LM_ERR("Error during time conversion\n"); /* the value of _s is now unspecified */ _s = NULL; @@ -222,54 +224,56 @@ inline int time2str(time_t _v, char* _s, int* _l) return 0; } -int mem_insert_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci, struct pcontact** _c){ +int mem_insert_pcontact(struct udomain *_d, str *_contact, + struct pcontact_info *_ci, struct pcontact **_c) +{ int sl; - if (new_pcontact(_d->name, _contact, _ci, _c) < 0) { + if(new_pcontact(_d->name, _contact, _ci, _c) < 0) { LM_ERR("creating pcontact failed\n"); return -1; } sl = ((*_c)->aorhash) & (_d->size - 1); (*_c)->sl = sl; - LM_DBG("Putting contact into slot [%d]\n", sl); + LM_DBG("Putting contact into slot [%d]\n", sl); slot_add(&_d->table[sl], *_c); update_stat(_d->contacts, 1); return 0; } -void mem_delete_pcontact(udomain_t* _d, struct pcontact* _c) +void mem_delete_pcontact(udomain_t *_d, struct pcontact *_c) { slot_rem(_c->slot, _c); free_pcontact(_c); - update_stat( _d->contacts, -1); + update_stat(_d->contacts, -1); } -void mem_timer_udomain(udomain_t* _d) +void mem_timer_udomain(udomain_t *_d) { - struct pcontact* ptr, *tmp; + struct pcontact *ptr, *tmp; 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; while(ptr) { - tmp = ptr; + tmp = ptr; ptr = ptr->next; - timer_pcontact(tmp); + timer_pcontact(tmp); } - + unlock_ulslot(_d, i); } } -void lock_udomain(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto) +void lock_udomain(udomain_t *_d, str *via_host, unsigned short via_port, + unsigned short via_proto) { unsigned int sl; - + sl = get_hash_slot(_d, via_host, via_port, via_proto); #ifdef GEN_LOCK_T_PREFERED @@ -279,7 +283,8 @@ void lock_udomain(udomain_t* _d, str* via_host, unsigned short via_port, unsigne #endif } -void unlock_udomain(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto) +void unlock_udomain(udomain_t *_d, str *via_host, unsigned short via_port, + unsigned short via_proto) { unsigned int sl; sl = get_hash_slot(_d, via_host, via_port, via_proto); @@ -290,7 +295,7 @@ void unlock_udomain(udomain_t* _d, str* via_host, unsigned short via_port, unsig #endif } -void lock_ulslot(udomain_t* _d, int i) +void lock_ulslot(udomain_t *_d, int i) { #ifdef GEN_LOCK_T_PREFERED lock_get(_d->table[i].lock); @@ -300,7 +305,7 @@ void lock_ulslot(udomain_t* _d, int i) } -void unlock_ulslot(udomain_t* _d, int i) +void unlock_ulslot(udomain_t *_d, int i) { #ifdef GEN_LOCK_T_PREFERED lock_release(_d->table[i].lock); @@ -310,14 +315,16 @@ void unlock_ulslot(udomain_t* _d, int i) } //TODO: this should be removed... -int update_rx_regsession(struct udomain* _d, str* session_id, struct pcontact* _c) { - if (session_id->len > 0 && session_id->s) { - if (_c->rx_session_id.len > 0 && _c->rx_session_id.s) { +int update_rx_regsession( + struct udomain *_d, str *session_id, struct pcontact *_c) +{ + if(session_id->len > 0 && session_id->s) { + if(_c->rx_session_id.len > 0 && _c->rx_session_id.s) { _c->rx_session_id.len = 0; shm_free(_c->rx_session_id.s); } _c->rx_session_id.s = shm_malloc(session_id->len); - if (!_c->rx_session_id.s) { + if(!_c->rx_session_id.s) { LM_ERR("no more shm_mem\n"); return -1; } @@ -336,52 +343,58 @@ int update_rx_regsession(struct udomain* _d, str* session_id, struct pcontact* _ * @param _c * @return */ -int update_pcontact(struct udomain* _d, struct pcontact_info* _ci, struct pcontact* _c) //TODO: should prob move this to pcontact +int update_pcontact(struct udomain *_d, struct pcontact_info *_ci, + struct pcontact *_c) //TODO: should prob move this to pcontact { int is_default = 1; - ppublic_t* ppublic_ptr; + ppublic_t *ppublic_ptr; int i; _c->reg_state = _ci->reg_state; - if (_ci->expires > 0) { + if(_ci->expires > 0) { _c->expires = _ci->expires; } - if (_ci->num_service_routes > 0 && _ci->service_routes) { + if(_ci->num_service_routes > 0 && _ci->service_routes) { //replace all existing service routes - if (_c->service_routes) { //remove old service routes - for (i=0; i<_c->num_service_routes; i++) { - if (_c->service_routes[i].s) + if(_c->service_routes) { //remove old service routes + for(i = 0; i < _c->num_service_routes; i++) { + if(_c->service_routes[i].s) shm_free(_c->service_routes[i].s); } shm_free(_c->service_routes); - _c->service_routes=0; - _c->num_service_routes=0; + _c->service_routes = 0; + _c->num_service_routes = 0; } //now add the new service routes - if (_ci->num_service_routes > 0) { - _c->service_routes = shm_malloc(_ci->num_service_routes*sizeof(str)); - if (!_c->service_routes) { - LM_ERR("no more shm mem trying to allocate [%ld bytes]\n", _ci->num_service_routes*sizeof(str)); + if(_ci->num_service_routes > 0) { + _c->service_routes = + shm_malloc(_ci->num_service_routes * sizeof(str)); + if(!_c->service_routes) { + LM_ERR("no more shm mem trying to allocate [%ld bytes]\n", + _ci->num_service_routes * sizeof(str)); goto out_of_memory; } else { - for (i=0; i<_ci->num_service_routes; i++) { - STR_SHM_DUP(_c->service_routes[i], _ci->service_routes[i], "update_pcontact"); + for(i = 0; i < _ci->num_service_routes; i++) { + STR_SHM_DUP(_c->service_routes[i], _ci->service_routes[i], + "update_pcontact"); } _c->num_service_routes = _ci->num_service_routes; } } } - if (_ci->num_public_ids > 0 && _ci->public_ids) { - if (_c->head) { + if(_ci->num_public_ids > 0 && _ci->public_ids) { + if(_c->head) { LM_DBG("ppublic's already exist.... .not updating\n"); } else { - for (i = 0; i < _ci->num_public_ids; i++) { - if (i > 0) - is_default = 0; //only the first one is default - P-Associated-uri (first one is default) - if (new_ppublic(&_ci->public_ids[i], is_default, &ppublic_ptr) != 0) { + for(i = 0; i < _ci->num_public_ids; i++) { + if(i > 0) + is_default = + 0; //only the first one is default - P-Associated-uri (first one is default) + if(new_ppublic(&_ci->public_ids[i], is_default, &ppublic_ptr) + != 0) { LM_ERR("unable to create new ppublic\n"); } else { insert_ppublic(_c, ppublic_ptr); @@ -391,25 +404,28 @@ int update_pcontact(struct udomain* _d, struct pcontact_info* _ci, struct pconta } //update Rx reg session information - if (_ci->rx_regsession_id && _ci->rx_regsession_id->len>0 && _ci->rx_regsession_id->s) { - if (_c->rx_session_id.len > 0 && _c->rx_session_id.s) { + if(_ci->rx_regsession_id && _ci->rx_regsession_id->len > 0 + && _ci->rx_regsession_id->s) { + if(_c->rx_session_id.len > 0 && _c->rx_session_id.s) { _c->rx_session_id.len = 0; shm_free(_c->rx_session_id.s); } _c->rx_session_id.s = shm_malloc(_ci->rx_regsession_id->len); - if (!_c->rx_session_id.s) { + if(!_c->rx_session_id.s) { LM_ERR("no more shm_mem\n"); return -1; } - memcpy(_c->rx_session_id.s, _ci->rx_regsession_id->s, _ci->rx_regsession_id->len); + memcpy(_c->rx_session_id.s, _ci->rx_regsession_id->s, + _ci->rx_regsession_id->len); _c->rx_session_id.len = _ci->rx_regsession_id->len; } //TODO: update path, etc - if (((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) && (db_update_pcontact(_c) != 0)){ - LM_ERR("Error updating record in DB"); - return -1; + if(((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) + && (db_update_pcontact(_c) != 0)) { + LM_ERR("Error updating record in DB"); + return -1; } run_ul_callbacks(PCSCF_CONTACT_UPDATE, _c); @@ -419,25 +435,28 @@ int update_pcontact(struct udomain* _d, struct pcontact_info* _ci, struct pconta return -1; } -int insert_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci, struct pcontact** _c) { +int insert_pcontact(struct udomain *_d, str *_contact, + struct pcontact_info *_ci, struct pcontact **_c) +{ - if (mem_insert_pcontact(_d, _contact, _ci, _c)){ - LM_ERR("inserting pcontact failed\n"); - goto error; - } - if (exists_ulcb_type(PCSCF_CONTACT_INSERT)) { + if(mem_insert_pcontact(_d, _contact, _ci, _c)) { + LM_ERR("inserting pcontact failed\n"); + goto error; + } + if(exists_ulcb_type(PCSCF_CONTACT_INSERT)) { run_ul_create_callbacks(*_c); } - if (((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) && db_insert_pcontact(*_c) != 0) { + if(((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) + && db_insert_pcontact(*_c) != 0) { LM_ERR("error inserting contact into db"); goto error; } - return 0; + return 0; error: - return -1; + return -1; } /* @@ -448,27 +467,33 @@ int insert_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci * @int reverse_search - reverse search for a contact in the memory * @return 0 if found <>0 if not */ -int get_pcontact_from_cache(udomain_t* _d, pcontact_info_t* contact_info, struct pcontact** _c, int reverse_search) { - unsigned int sl, i, j, aorhash, params_len, has_rinstance=0; - struct pcontact* c; +int get_pcontact_from_cache(udomain_t *_d, pcontact_info_t *contact_info, + struct pcontact **_c, int reverse_search) +{ + unsigned int sl, i, j, aorhash, params_len, has_rinstance = 0; + struct pcontact *c; struct sip_uri needle_uri; int serviceroutematch; char *params, *sep; str rinstance = {0, 0}; - LM_DBG("Searching for contact with AOR [%.*s] in P-CSCF usrloc based on VIA [%d://%.*s:%d]" - " Received [%d://%.*s:%d], Search flag is %d, reverse_search %d\n", - contact_info->aor.len, contact_info->aor.s, contact_info->via_prot, contact_info->via_host.len, - contact_info->via_host.s, contact_info->via_port, - contact_info->received_proto, contact_info->received_host.len, contact_info->received_host.s, - contact_info->received_port, contact_info->searchflag, - reverse_search); + LM_DBG("Searching for contact with AOR [%.*s] in P-CSCF usrloc based on " + "VIA [%d://%.*s:%d]" + " Received [%d://%.*s:%d], Search flag is %d, reverse_search %d\n", + contact_info->aor.len, contact_info->aor.s, contact_info->via_prot, + contact_info->via_host.len, contact_info->via_host.s, + contact_info->via_port, contact_info->received_proto, + contact_info->received_host.len, contact_info->received_host.s, + contact_info->received_port, contact_info->searchflag, + reverse_search); /* parse the uri in the NOTIFY */ - if (contact_info->aor.len>0 && contact_info->aor.s){ + if(contact_info->aor.len > 0 && contact_info->aor.s) { LM_DBG("Have an AOR to search for\n"); - if (parse_uri(contact_info->aor.s, contact_info->aor.len, &needle_uri) != 0) { - LM_ERR("Unable to parse contact aor in get_pcontact [%.*s]\n", contact_info->aor.len, contact_info->aor.s); + if(parse_uri(contact_info->aor.s, contact_info->aor.len, &needle_uri) + != 0) { + LM_ERR("Unable to parse contact aor in get_pcontact [%.*s]\n", + contact_info->aor.len, contact_info->aor.s); return 1; } LM_DBG("checking for rinstance"); @@ -476,13 +501,13 @@ int get_pcontact_from_cache(udomain_t* _d, pcontact_info_t* contact_info, struct params = needle_uri.sip_params.s; params_len = needle_uri.sip_params.len; - while (params_len >= RINSTANCE_LEN) { - if (strncmp(params, RINSTANCE, RINSTANCE_LEN) == 0) { + while(params_len >= RINSTANCE_LEN) { + if(strncmp(params, RINSTANCE, RINSTANCE_LEN) == 0) { has_rinstance = 1; break; } sep = memchr(params, 59 /* ; */, params_len); - if (sep == NULL) { + if(sep == NULL) { LM_DBG("no rinstance param\n"); break; } else { @@ -490,31 +515,35 @@ int get_pcontact_from_cache(udomain_t* _d, pcontact_info_t* contact_info, struct params = sep + 1; } } - if (has_rinstance) { + if(has_rinstance) { rinstance.s = params + RINSTANCE_LEN; rinstance.len = params_len - RINSTANCE_LEN; - sep = (char*)memchr(rinstance.s, 59 /* ; */, rinstance.len); - if (sep != NULL){ - rinstance.len = (sep-rinstance.s); + sep = (char *)memchr(rinstance.s, 59 /* ; */, rinstance.len); + if(sep != NULL) { + rinstance.len = (sep - rinstance.s); } LM_DBG("rinstance found [%.*s]\n", rinstance.len, rinstance.s); } } - - + + /* search in cache */ - aorhash = get_aor_hash(_d, &contact_info->via_host, contact_info->via_port, contact_info->via_prot); + aorhash = get_aor_hash(_d, &contact_info->via_host, contact_info->via_port, + contact_info->via_prot); sl = aorhash & (_d->size - 1); - + LM_DBG("get_pcontact slot is [%d]\n", sl); c = reverse_search ? _d->table[sl].last : _d->table[sl].first; - for (i = 0; i < _d->table[sl].n; i++) { - LM_DBG("comparing contact with aorhash [%u], aor [%.*s]\n", c->aorhash, c->aor.len, c->aor.s); - LM_DBG(" contact host [%.*s:%d]\n", c->contact_host.len, c->contact_host.s, c->contact_port); - LM_DBG("contact received [%d:%.*s:%d]\n", c->received_proto, c->received_host.len, c->received_host.s, c->received_port); + for(i = 0; i < _d->table[sl].n; i++) { + LM_DBG("comparing contact with aorhash [%u], aor [%.*s]\n", c->aorhash, + c->aor.len, c->aor.s); + LM_DBG(" contact host [%.*s:%d]\n", c->contact_host.len, + c->contact_host.s, c->contact_port); + LM_DBG("contact received [%d:%.*s:%d]\n", c->received_proto, + c->received_host.len, c->received_host.s, c->received_port); - if(c->aorhash == aorhash){ + if(c->aorhash == aorhash) { int check1_passed = 0; int check2_passed = 0; ip_addr_t c_ip_addr; @@ -522,113 +551,144 @@ int get_pcontact_from_cache(udomain_t* _d, pcontact_info_t* contact_info, struct LM_DBG("mached a record by aorhash: %u\n", aorhash); // convert 'contact->contact host' ip string to ip_addr_t - if (str2ipxbuf(&c->contact_host, &c_ip_addr) < 0){ - LM_ERR("Unable to convert c->contact_host [%.*s]\n", c->contact_host.len, c->contact_host.s); + if(str2ipxbuf(&c->contact_host, &c_ip_addr) < 0) { + LM_ERR("Unable to convert c->contact_host [%.*s]\n", + c->contact_host.len, c->contact_host.s); return 1; } // convert 'contact info->via host' ip string to ip_addr_t - if(str2ipxbuf(&contact_info->via_host, &ci_ip_addr) < 0){ - LM_ERR("Unable to convert contact_info->via_host [%.*s]\n", contact_info->via_host.len, contact_info->via_host.s); + if(str2ipxbuf(&contact_info->via_host, &ci_ip_addr) < 0) { + LM_ERR("Unable to convert contact_info->via_host [%.*s]\n", + contact_info->via_host.len, contact_info->via_host.s); return 1; } // compare 'contact->contact host' and 'contact info->via host' - if(ip_addr_cmp(&c_ip_addr, &ci_ip_addr) && - (c->contact_port == contact_info->via_port) && - !(contact_info->searchflag & SEARCH_RECEIVED)) - { + if(ip_addr_cmp(&c_ip_addr, &ci_ip_addr) + && (c->contact_port == contact_info->via_port) + && !(contact_info->searchflag & SEARCH_RECEIVED)) { LM_DBG("matched contact ip address and port\n"); check1_passed = 1; } - if(contact_info->searchflag & SEARCH_RECEIVED){ + if(contact_info->searchflag & SEARCH_RECEIVED) { LM_DBG("continuing to match on received details\n"); // convert 'contact->received host' ip string to ip_addr_t - if (str2ipxbuf(&c->received_host, &c_ip_addr) < 0){ - LM_ERR("Unable to convert c->received_host [%.*s]\n", c->received_host.len, c->received_host.s); + if(str2ipxbuf(&c->received_host, &c_ip_addr) < 0) { + LM_ERR("Unable to convert c->received_host [%.*s]\n", + c->received_host.len, c->received_host.s); return 1; } // convert 'contact info->received host' ip string to ip_addr_t - if(str2ipxbuf(&contact_info->received_host, &ci_ip_addr) < 0){ - LM_ERR("Unable to convert contact_info->received_host [%.*s]\n", - contact_info->received_host.len, contact_info->received_host.s); + if(str2ipxbuf(&contact_info->received_host, &ci_ip_addr) < 0) { + LM_ERR("Unable to convert contact_info->received_host " + "[%.*s]\n", + contact_info->received_host.len, + contact_info->received_host.s); return 1; } // compare 'contact->received host' and 'contact info->received host' - if(ip_addr_cmp(&c_ip_addr, &ci_ip_addr) && - ((c->received_port == contact_info->received_port) || - (c->contact_port == contact_info->received_port))){ /*volte comes from a different port.... typically uses 4060*/ + if(ip_addr_cmp(&c_ip_addr, &ci_ip_addr) + && ((c->received_port == contact_info->received_port) + || (c->contact_port + == contact_info + ->received_port))) { /*volte comes from a different port.... typically uses 4060*/ check2_passed = 1; } } - if(check1_passed || check2_passed){ + if(check1_passed || check2_passed) { LM_DBG("found contact with URI [%.*s]\n", c->aor.len, c->aor.s); - if (has_rinstance) { - LM_DBG("confirming rinstance is the same - search has [%.*s] and proposed found contact has [%.*s]", - rinstance.len, rinstance.s, - c->rinstance.len, c->rinstance.s); - if ((rinstance.len != c->rinstance.len) || (memcmp(rinstance.s, c->rinstance.s, rinstance.len) != 0) ) { + if(has_rinstance) { + LM_DBG("confirming rinstance is the same - search has " + "[%.*s] and proposed found contact has [%.*s]", + rinstance.len, rinstance.s, c->rinstance.len, + c->rinstance.s); + if((rinstance.len != c->rinstance.len) + || (memcmp(rinstance.s, c->rinstance.s, + rinstance.len) + != 0)) { LM_DBG("rinstance does not match - no match here...\n"); c = reverse_search ? c->prev : c->next; continue; } } - if ((contact_info->aor.len>0) && (needle_uri.user.len != 0)){ - if ((needle_uri.user.len != c->contact_user.len) || - (memcmp(needle_uri.user.s, c->contact_user.s, needle_uri.user.len) != 0)) { - LM_ERR("user name does not match - no match here...\n"); - LM_DBG("found pcontact username [%d]: [%.*s]\n", i, c->contact_user.len, c->contact_user.s); - LM_DBG("incoming contact username: [%.*s]\n", needle_uri.user.len, needle_uri.user.s); - c = c->next; - continue; - } - if ((contact_info->aor.len >= 4) && (memcmp(contact_info->aor.s, c->aor.s, 4) != 0)) { // do not mix up sip- and tel-URIs. - LM_ERR("scheme does not match - no match here...\n"); - LM_DBG("found pcontact scheme [%d]: [%.*s]\n", i, 4, c->aor.s); - LM_DBG("incoming contact scheme: [%.*s]\n", 4, contact_info->aor.s); - c = c->next; - continue; - } - } - else{ - LM_DBG("No user name present - abort user name check\n"); - } - - - if ((contact_info->extra_search_criteria & SEARCH_SERVICE_ROUTES) && contact_info->num_service_routes > 0) { - LM_DBG("have %d service routes to search for\n", contact_info->num_service_routes); - if (contact_info->num_service_routes != c->num_service_routes) { + if((contact_info->aor.len > 0) && (needle_uri.user.len != 0)) { + if((needle_uri.user.len != c->contact_user.len) + || (memcmp(needle_uri.user.s, c->contact_user.s, + needle_uri.user.len) + != 0)) { + LM_ERR("user name does not match - no match here...\n"); + LM_DBG("found pcontact username [%d]: [%.*s]\n", i, + c->contact_user.len, c->contact_user.s); + LM_DBG("incoming contact username: [%.*s]\n", + needle_uri.user.len, needle_uri.user.s); + c = c->next; + continue; + } + if((contact_info->aor.len >= 4) + && (memcmp(contact_info->aor.s, c->aor.s, 4) + != 0)) { // do not mix up sip- and tel-URIs. + LM_ERR("scheme does not match - no match here...\n"); + LM_DBG("found pcontact scheme [%d]: [%.*s]\n", i, 4, + c->aor.s); + LM_DBG("incoming contact scheme: [%.*s]\n", 4, + contact_info->aor.s); + c = c->next; + continue; + } + } else { + LM_DBG("No user name present - abort user name check\n"); + } + + + if((contact_info->extra_search_criteria & SEARCH_SERVICE_ROUTES) + && contact_info->num_service_routes > 0) { + LM_DBG("have %d service routes to search for\n", + contact_info->num_service_routes); + if(contact_info->num_service_routes + != c->num_service_routes) { c = reverse_search ? c->prev : c->next; - LM_DBG("number of service routes do not match - failing\n"); + LM_DBG("number of service routes do not match - " + "failing\n"); continue; - } - + } + serviceroutematch = 1; - for (j=0; jnum_service_routes; j++) { - if (contact_info->service_routes[j].len != c->service_routes[j].len - || memcmp(contact_info->service_routes[j].s, c->service_routes[j].s, c->service_routes[j].len) != 0) { - LM_DBG("service route at position %d does not match - looking for [%.*s] and contact has [%.*s]..." - " continuing to next contact check\n", - j, - contact_info->service_routes[j].len, contact_info->service_routes[j].s, - c->service_routes[j].len, c->service_routes[j].s); + for(j = 0; j < contact_info->num_service_routes; j++) { + if(contact_info->service_routes[j].len + != c->service_routes[j].len + || memcmp(contact_info->service_routes[j].s, + c->service_routes[j].s, + c->service_routes[j].len) + != 0) { + LM_DBG("service route at position %d does not " + "match - looking for [%.*s] and contact has " + "[%.*s]..." + " continuing to next contact check\n", + j, contact_info->service_routes[j].len, + contact_info->service_routes[j].s, + c->service_routes[j].len, + c->service_routes[j].s); serviceroutematch = 0; break; } } - if (serviceroutematch == 0) { + if(serviceroutematch == 0) { c = reverse_search ? c->prev : c->next; continue; } } - + //finally check state being searched for - if ( (contact_info->reg_state != PCONTACT_ANY) && ((contact_info->reg_state & c->reg_state) == 0)) { - LM_DBG("can't find contact for requested reg state [%d] - (have [%d])\n", contact_info->reg_state, c->reg_state); + if((contact_info->reg_state != PCONTACT_ANY) + && ((contact_info->reg_state & c->reg_state) == 0)) { + LM_DBG("can't find contact for requested reg state [%d] - " + "(have [%d])\n", + contact_info->reg_state, c->reg_state); c = reverse_search ? c->prev : c->next; continue; } @@ -639,16 +699,19 @@ int get_pcontact_from_cache(udomain_t* _d, pcontact_info_t* contact_info, struct } c = reverse_search ? c->prev : c->next; } - + LM_DBG("contact not found in memory\n"); // Default: Not found. *_c = NULL; - + return 1; /* Nothing found */ } -int update_security(udomain_t* _d, security_type _t, security_t* _s, struct pcontact* _c) { - if (((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) && db_update_pcontact_security(_c, _t, _s) != 0) { +int update_security( + udomain_t *_d, security_type _t, security_t *_s, struct pcontact *_c) +{ + if(((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) + && db_update_pcontact_security(_c, _t, _s) != 0) { LM_ERR("Error updating security for contact in DB\n"); return -1; } @@ -656,8 +719,11 @@ int update_security(udomain_t* _d, security_type _t, security_t* _s, struct pcon return 0; } -int update_temp_security(udomain_t* _d, security_type _t, security_t* _s, struct pcontact* _c) { - if (((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) && db_update_pcontact_security_temp(_c, _t, _s) != 0) { +int update_temp_security( + udomain_t *_d, security_type _t, security_t *_s, struct pcontact *_c) +{ + if(((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) + && db_update_pcontact_security_temp(_c, _t, _s) != 0) { LM_ERR("Error updating temp security for contact in DB\n"); return -1; } @@ -666,41 +732,51 @@ int update_temp_security(udomain_t* _d, security_type _t, security_t* _s, struct } -int assert_identity(udomain_t* _d, str * _host, unsigned short _port, unsigned short _proto, str * _identity) { +int assert_identity(udomain_t *_d, str *_host, unsigned short _port, + unsigned short _proto, str *_identity) +{ int i; - struct pcontact* c; + struct pcontact *c; // Public identities of this contact - struct ppublic * p; + struct ppublic *p; - for(i=0; i<_d->size; i++) - { + for(i = 0; i < _d->size; i++) { c = _d->table[i].first; while(c) { - LM_DBG("Port %d (search %d), Proto %d (search %d), reg_state %s (search %s)\n", - c->received_port, _port, c->received_proto, _proto, - reg_state_to_string(c->reg_state), reg_state_to_string(PCONTACT_REGISTERED) - ); + LM_DBG("Port %d (search %d), Proto %d (search %d), reg_state %s " + "(search %s)\n", + c->received_port, _port, c->received_proto, _proto, + reg_state_to_string(c->reg_state), + reg_state_to_string(PCONTACT_REGISTERED)); // First check, if Proto and Port matches: - if ((c->reg_state == PCONTACT_REGISTERED) && (c->received_port == _port) && (c->received_proto == _proto)) { - LM_DBG("Received host len %d (search %d)\n", c->received_host.len, _host->len); + if((c->reg_state == PCONTACT_REGISTERED) + && (c->received_port == _port) + && (c->received_proto == _proto)) { + LM_DBG("Received host len %d (search %d)\n", + c->received_host.len, _host->len); // Then check the length: - if (c->received_host.len == _host->len) { + if(c->received_host.len == _host->len) { LM_DBG("Received host %.*s (search %.*s)\n", - c->received_host.len, c->received_host.s, - _host->len, _host->s); + c->received_host.len, c->received_host.s, + _host->len, _host->s); // Finally really compare the "received_host" - if (!memcmp(c->received_host.s, _host->s, _host->len)) { - for (p = c->head; p; p = p->next) { - LM_DBG("Public identity: %.*s\n", p->public_identity.len, p->public_identity.s); + if(!memcmp(c->received_host.s, _host->s, _host->len)) { + for(p = c->head; p; p = p->next) { + LM_DBG("Public identity: %.*s\n", + p->public_identity.len, + p->public_identity.s); /* Check length: */ - if (_identity->len == p->public_identity.len) { + if(_identity->len == p->public_identity.len) { /* Check contents: */ - if (strncasecmp(_identity->s, p->public_identity.s, _identity->len) == 0) { + if(strncasecmp(_identity->s, + p->public_identity.s, _identity->len) + == 0) { LM_DBG("Match!\n"); return 1; } - } else LM_DBG("Length does not match.\n"); + } else + LM_DBG("Length does not match.\n"); } } } @@ -711,16 +787,19 @@ int assert_identity(udomain_t* _d, str * _host, unsigned short _port, unsigned s return 0; /* Nothing found */ } -int delete_pcontact(udomain_t* _d, /*str* _aor, str* _received_host, int _received_port,*/ struct pcontact* _c) +int delete_pcontact(udomain_t *_d, + /*str* _aor, str* _received_host, int _received_port,*/ struct pcontact + *_c) { - if (_c==0) { + if(_c == 0) { return 0; } - if (exists_ulcb_type(PCSCF_CONTACT_DELETE)) { + if(exists_ulcb_type(PCSCF_CONTACT_DELETE)) { run_ul_callbacks(PCSCF_CONTACT_DELETE, _c); } - if (((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) && db_delete_pcontact(_c) != 0) { + if(((db_mode == WRITE_THROUGH) || (db_mode == DB_ONLY)) + && db_delete_pcontact(_c) != 0) { LM_ERR("Error deleting contact from DB"); return -1; } @@ -730,95 +809,124 @@ int delete_pcontact(udomain_t* _d, /*str* _aor, str* _received_host, int _receiv return 0; } -int unreg_pending_contacts_cb(udomain_t* _d, pcontact_t* _c, int type) +int unreg_pending_contacts_cb(udomain_t *_d, pcontact_t *_c, int type) { - pcontact_t* c; - pcontact_info_t contact_info; - unsigned int aorhash, sl, i; + pcontact_t *c; + pcontact_info_t contact_info; + unsigned int aorhash, sl, i; contact_info.via_host = _c->via_host; contact_info.via_port = SIP_PORT; contact_info.via_prot = _c->via_proto; contact_info.reg_state = PCONTACT_ANY; - LM_DBG("Searching for contact in P-CSCF usrloc based on VIA [%d://%.*s:%d], reg state 0x%02X\n", - contact_info.via_prot, contact_info.via_host.len, contact_info.via_host.s, contact_info.via_port, contact_info.reg_state); - - aorhash = get_aor_hash(_d, &contact_info.via_host, contact_info.via_port, contact_info.via_prot); + LM_DBG("Searching for contact in P-CSCF usrloc based on VIA " + "[%d://%.*s:%d], reg state 0x%02X\n", + contact_info.via_prot, contact_info.via_host.len, + contact_info.via_host.s, contact_info.via_port, + contact_info.reg_state); + + aorhash = get_aor_hash(_d, &contact_info.via_host, contact_info.via_port, + contact_info.via_prot); sl = aorhash & (_d->size - 1); - - LM_DBG("get_pcontact slot is [%d]\n", sl); + + LM_DBG("get_pcontact slot is [%d]\n", sl); c = _d->table[sl].first; - for(i = 0; i < _d->table[sl].n; i++){ - LM_DBG("comparing contact with aorhash [%u], aor [%.*s]\n", c->aorhash, c->aor.len, c->aor.s); - LM_DBG("contact host [%.*s:%d]\n", c->contact_host.len, c->contact_host.s, c->contact_port); + for(i = 0; i < _d->table[sl].n; i++) { + LM_DBG("comparing contact with aorhash [%u], aor [%.*s]\n", c->aorhash, + c->aor.len, c->aor.s); + LM_DBG("contact host [%.*s:%d]\n", c->contact_host.len, + c->contact_host.s, c->contact_port); - if(c->aorhash == aorhash){ + if(c->aorhash == aorhash) { ip_addr_t c_ip_addr; ip_addr_t ci_ip_addr; // convert 'contact->contact host' ip string to ip_addr_t - if (str2ipxbuf(&c->contact_host, &c_ip_addr) < 0){ - LM_ERR("Unable to convert c->contact_host [%.*s]\n", c->contact_host.len, c->contact_host.s); + if(str2ipxbuf(&c->contact_host, &c_ip_addr) < 0) { + LM_ERR("Unable to convert c->contact_host [%.*s]\n", + c->contact_host.len, c->contact_host.s); return 1; } // convert 'contact info->via host' ip string to ip_addr_t - if(str2ipxbuf(&contact_info.via_host, &ci_ip_addr) < 0){ - LM_ERR("Unable to convert contact_info.via_host [%.*s]\n", contact_info.via_host.len, contact_info.via_host.s); + if(str2ipxbuf(&contact_info.via_host, &ci_ip_addr) < 0) { + LM_ERR("Unable to convert contact_info.via_host [%.*s]\n", + contact_info.via_host.len, contact_info.via_host.s); return 1; } // compare 'contact->contact host' and 'contact info->via host' - if(ip_addr_cmp(&c_ip_addr, &ci_ip_addr) && (c->contact_port == contact_info.via_port)){ + if(ip_addr_cmp(&c_ip_addr, &ci_ip_addr) + && (c->contact_port == contact_info.via_port)) { LM_DBG("found contact with URI [%.*s]\n", c->aor.len, c->aor.s); // finally check state being searched for - if((contact_info.reg_state != PCONTACT_ANY) && ((contact_info.reg_state & c->reg_state) == 0)){ - LM_DBG("can't find contact for requested reg state [%d] - (have [%d])\n", contact_info.reg_state, c->reg_state); + if((contact_info.reg_state != PCONTACT_ANY) + && ((contact_info.reg_state & c->reg_state) == 0)) { + LM_DBG("can't find contact for requested reg state [%d] - " + "(have [%d])\n", + contact_info.reg_state, c->reg_state); c = c->next; continue; } // check for equal ipsec parameters - if(c->security_temp == NULL || _c->security_temp == NULL){ + if(c->security_temp == NULL || _c->security_temp == NULL) { LM_DBG("Invalid temp security\n"); c = c->next; continue; } - if(c->security_temp->type != SECURITY_IPSEC){ + if(c->security_temp->type != SECURITY_IPSEC) { LM_DBG("Invalid temp security type\n"); c = c->next; continue; } - if(c->security_temp->data.ipsec == NULL || _c->security_temp->data.ipsec == NULL){ + if(c->security_temp->data.ipsec == NULL + || _c->security_temp->data.ipsec == NULL) { LM_DBG("Invalid ipsec\n"); c = c->next; continue; } - LM_DBG("=========== c->reg_state 0x%02X, %u-%u | %u-%u | %u-%u | %u-%u | %u-%u | %u-%u | %u-%u | %u-%u |", - c->reg_state, - c->security_temp->data.ipsec->port_pc, _c->security_temp->data.ipsec->port_pc, - c->security_temp->data.ipsec->port_ps, _c->security_temp->data.ipsec->port_ps, - c->security_temp->data.ipsec->port_uc, _c->security_temp->data.ipsec->port_uc, - c->security_temp->data.ipsec->port_us, _c->security_temp->data.ipsec->port_us, - c->security_temp->data.ipsec->spi_pc, _c->security_temp->data.ipsec->spi_pc, - c->security_temp->data.ipsec->spi_ps, _c->security_temp->data.ipsec->spi_ps, - c->security_temp->data.ipsec->spi_uc, _c->security_temp->data.ipsec->spi_uc, - c->security_temp->data.ipsec->spi_us, _c->security_temp->data.ipsec->spi_us); - - if(c->security_temp->data.ipsec->port_pc == _c->security_temp->data.ipsec->port_pc && - c->security_temp->data.ipsec->port_ps == _c->security_temp->data.ipsec->port_ps && - c->security_temp->data.ipsec->port_uc == _c->security_temp->data.ipsec->port_uc && - c->security_temp->data.ipsec->port_us == _c->security_temp->data.ipsec->port_us && - c->security_temp->data.ipsec->spi_pc == _c->security_temp->data.ipsec->spi_pc && - c->security_temp->data.ipsec->spi_ps == _c->security_temp->data.ipsec->spi_ps && - c->security_temp->data.ipsec->spi_uc == _c->security_temp->data.ipsec->spi_uc && - c->security_temp->data.ipsec->spi_us == _c->security_temp->data.ipsec->spi_us){ + LM_DBG("=========== c->reg_state 0x%02X, %u-%u | %u-%u | %u-%u " + "| %u-%u | %u-%u | %u-%u | %u-%u | %u-%u |", + c->reg_state, c->security_temp->data.ipsec->port_pc, + _c->security_temp->data.ipsec->port_pc, + c->security_temp->data.ipsec->port_ps, + _c->security_temp->data.ipsec->port_ps, + c->security_temp->data.ipsec->port_uc, + _c->security_temp->data.ipsec->port_uc, + c->security_temp->data.ipsec->port_us, + _c->security_temp->data.ipsec->port_us, + c->security_temp->data.ipsec->spi_pc, + _c->security_temp->data.ipsec->spi_pc, + c->security_temp->data.ipsec->spi_ps, + _c->security_temp->data.ipsec->spi_ps, + c->security_temp->data.ipsec->spi_uc, + _c->security_temp->data.ipsec->spi_uc, + c->security_temp->data.ipsec->spi_us, + _c->security_temp->data.ipsec->spi_us); + + if(c->security_temp->data.ipsec->port_pc + == _c->security_temp->data.ipsec->port_pc + && c->security_temp->data.ipsec->port_ps + == _c->security_temp->data.ipsec->port_ps + && c->security_temp->data.ipsec->port_uc + == _c->security_temp->data.ipsec->port_uc + && c->security_temp->data.ipsec->port_us + == _c->security_temp->data.ipsec->port_us + && c->security_temp->data.ipsec->spi_pc + == _c->security_temp->data.ipsec->spi_pc + && c->security_temp->data.ipsec->spi_ps + == _c->security_temp->data.ipsec->spi_ps + && c->security_temp->data.ipsec->spi_uc + == _c->security_temp->data.ipsec->spi_uc + && c->security_temp->data.ipsec->spi_us + == _c->security_temp->data.ipsec->spi_us) { // deregister user callback only for contacts with exact sec parameters like registerd contact delete_ulcb(c, type); } @@ -840,18 +948,19 @@ int unreg_pending_contacts_cb(udomain_t* _d, pcontact_t* _c, int type) * \param contact contact * \return pointer to the ucontact_info on success, 0 on failure */ -static inline pcontact_info_t* dbrow2info( db_val_t *vals, str *contact) +static inline pcontact_info_t *dbrow2info(db_val_t *vals, str *contact) { static pcontact_info_t ci; - static str host, received, path, rx_session_id, implicit_impus, tmpstr, service_routes; + static str host, received, path, rx_session_id, implicit_impus, tmpstr, + service_routes; static str *impu_list, *service_route_list; - int flag=0, n; - char *p, *q=0; + int flag = 0, n; + char *p, *q = 0; - memset( &ci, 0, sizeof(pcontact_info_t)); + memset(&ci, 0, sizeof(pcontact_info_t)); - host.s = (char*) VAL_STRING(vals + 1); - if (VAL_NULL(vals+1) || !host.s || !host.s[0]) { + host.s = (char *)VAL_STRING(vals + 1); + if(VAL_NULL(vals + 1) || !host.s || !host.s[0]) { host.len = 0; host.s = 0; } else { @@ -860,9 +969,10 @@ static inline pcontact_info_t* dbrow2info( db_val_t *vals, str *contact) ci.via_host = host; ci.via_port = VAL_INT(vals + 2); ci.via_prot = VAL_INT(vals + 3); - received.s = (char*) VAL_STRING(vals + 4); - if (VAL_NULL(vals+4) || !received.s || !received.s[0]) { - LM_DBG("Empty received for contact [%.*s]....\n", contact->len, contact->s); /*this could happen if you have been notified about a contact from S-CSCF*/ + received.s = (char *)VAL_STRING(vals + 4); + if(VAL_NULL(vals + 4) || !received.s || !received.s[0]) { + LM_DBG("Empty received for contact [%.*s]....\n", contact->len, + contact->s); /*this could happen if you have been notified about a contact from S-CSCF*/ received.len = 0; received.s = 0; } else { @@ -872,57 +982,57 @@ static inline pcontact_info_t* dbrow2info( db_val_t *vals, str *contact) ci.received_port = VAL_INT(vals + 5); ci.received_proto = VAL_INT(vals + 6); - rx_session_id.s = (char*) VAL_STRING(vals + 7); - if (VAL_NULL(vals+7) || !rx_session_id.s || !rx_session_id.s[0]) { + rx_session_id.s = (char *)VAL_STRING(vals + 7); + if(VAL_NULL(vals + 7) || !rx_session_id.s || !rx_session_id.s[0]) { rx_session_id.len = 0; rx_session_id.s = 0; } else { rx_session_id.len = strlen(rx_session_id.s); } ci.rx_regsession_id = &rx_session_id; - if (VAL_NULL(vals + 8)) { + if(VAL_NULL(vals + 8)) { LM_ERR("empty registration state in DB\n"); return 0; } ci.reg_state = VAL_INT(vals + 8); - - if (VAL_NULL(vals + 9)) { + + if(VAL_NULL(vals + 9)) { LM_ERR("empty expire\n"); return 0; } ci.expires = VAL_TIME(vals + 9); - path.s = (char*)VAL_STRING(vals+13); - if (VAL_NULL(vals+13) || !path.s || !path.s[0]) { - path.len = 0; - path.s = 0; - } else { - path.len = strlen(path.s); - } + path.s = (char *)VAL_STRING(vals + 13); + if(VAL_NULL(vals + 13) || !path.s || !path.s[0]) { + path.len = 0; + path.s = 0; + } else { + path.len = strlen(path.s); + } ci.path = &path; //public IDs - implicit set - implicit_impus.s = (char*) VAL_STRING(vals + 12); - if (!VAL_NULL(vals + 12) && implicit_impus.s && implicit_impus.s[0]) { + implicit_impus.s = (char *)VAL_STRING(vals + 12); + if(!VAL_NULL(vals + 12) && implicit_impus.s && implicit_impus.s[0]) { //how many - n=0; + n = 0; p = implicit_impus.s; - while (*p) { - if ((*p) == '<') { + while(*p) { + if((*p) == '<') { n++; } p++; } impu_list = pkg_malloc(sizeof(str) * n); - n=0; + n = 0; p = implicit_impus.s; - while (*p) { - if (*p == '<') { + while(*p) { + if(*p == '<') { q = p + 1; flag = 1; } - if (*p == '>') { - if (flag) { + if(*p == '>') { + if(flag) { tmpstr.s = q; tmpstr.len = p - q; impu_list[n++] = tmpstr; @@ -936,13 +1046,13 @@ static inline pcontact_info_t* dbrow2info( db_val_t *vals, str *contact) } //service routes - service_routes.s = (char*) VAL_STRING(vals + 11); - if (!VAL_NULL(vals + 11) && service_routes.s && service_routes.s[0]) { + service_routes.s = (char *)VAL_STRING(vals + 11); + if(!VAL_NULL(vals + 11) && service_routes.s && service_routes.s[0]) { //how many n = 0; p = service_routes.s; - while (*p) { - if ((*p) == '<') { + while(*p) { + if((*p) == '<') { n++; } p++; @@ -951,13 +1061,13 @@ static inline pcontact_info_t* dbrow2info( db_val_t *vals, str *contact) n = 0; p = service_routes.s; - while (*p) { - if (*p == '<') { + while(*p) { + if(*p == '<') { q = p + 1; flag = 1; } - if (*p == '>') { - if (flag) { + if(*p == '>') { + if(flag) { tmpstr.s = q; tmpstr.len = p - q; service_route_list[n++] = tmpstr; @@ -982,16 +1092,16 @@ static inline pcontact_info_t* dbrow2info( db_val_t *vals, str *contact) * \param _d loaded domain * \return 0 on success, -1 on failure */ -int preload_udomain(db1_con_t* _c, udomain_t* _d) +int preload_udomain(db1_con_t *_c, udomain_t *_d) { pcontact_info_t *ci; db_row_t *row; db_key_t columns[15]; - db1_res_t* res = NULL; + db1_res_t *res = NULL; str aor; int i, n; - pcontact_t* c; + pcontact_t *c; LM_DBG("pre-loading domain from DB\n"); @@ -1011,7 +1121,7 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d) columns[13] = &public_ids_col; columns[14] = &path_col; - if (ul_dbf.use_table(_c, _d->name) < 0) { + if(ul_dbf.use_table(_c, _d->name) < 0) { LM_ERR("sql use_table failed\n"); return -1; } @@ -1020,23 +1130,23 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d) LM_NOTICE("load start time [%d]\n", (int)time(NULL)); #endif - if (DB_CAPABILITY(ul_dbf, DB_CAP_FETCH)) { - if (ul_dbf.query(_c, 0, 0, 0, columns, 0, 15, 0, 0) < 0) { + if(DB_CAPABILITY(ul_dbf, DB_CAP_FETCH)) { + if(ul_dbf.query(_c, 0, 0, 0, columns, 0, 15, 0, 0) < 0) { LM_ERR("db_query (1) failed\n"); return -1; } - if(ul_dbf.fetch_result(_c, &res, ul_fetch_rows)<0) { + if(ul_dbf.fetch_result(_c, &res, ul_fetch_rows) < 0) { LM_ERR("fetching rows failed\n"); return -1; } } else { - if (ul_dbf.query(_c, 0, 0, 0, columns, 0, 15, 0, &res) < 0) { + if(ul_dbf.query(_c, 0, 0, 0, columns, 0, 15, 0, &res) < 0) { LM_ERR("db_query failed\n"); return -1; } } - if (RES_ROW_N(res) == 0) { + if(RES_ROW_N(res) == 0) { LM_DBG("table is empty\n"); ul_dbf.free_result(_c, res); return 0; @@ -1050,43 +1160,45 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d) for(i = 0; i < RES_ROW_N(res); i++) { row = RES_ROWS(res) + i; - aor.s = (char*) VAL_STRING(ROW_VALUES(row) + 1); - if (VAL_NULL(ROW_VALUES(row) + 1) || aor.s == 0 || aor.s[0] == 0) { - LM_CRIT("empty aor record in table %s...skipping\n", _d->name->s); + aor.s = (char *)VAL_STRING(ROW_VALUES(row) + 1); + if(VAL_NULL(ROW_VALUES(row) + 1) || aor.s == 0 || aor.s[0] == 0) { + LM_CRIT("empty aor record in table %s...skipping\n", + _d->name->s); continue; } aor.len = strlen(aor.s); ci = dbrow2info(ROW_VALUES(row) + 1, &aor); - if (!ci) { - LM_WARN("Failed to get contact info from DB.... continuing...\n"); + if(!ci) { + LM_WARN("Failed to get contact info from DB.... " + "continuing...\n"); continue; } lock_udomain(_d, &ci->via_host, ci->via_port, ci->via_prot); - if ( (mem_insert_pcontact(_d, &aor, ci, &c)) != 0) { + if((mem_insert_pcontact(_d, &aor, ci, &c)) != 0) { LM_ERR("inserting contact failed\n"); unlock_udomain(_d, &ci->via_host, ci->via_port, ci->via_prot); - if(ci->public_ids){ + if(ci->public_ids) { pkg_free(ci->public_ids); } - if(ci->service_routes){ - pkg_free(ci->service_routes); - } + if(ci->service_routes) { + pkg_free(ci->service_routes); + } goto error1; } //c->flags = c->flags|(1<via_host, ci->via_port, ci->via_prot); - if(ci->public_ids){ + if(ci->public_ids) { pkg_free(ci->public_ids); } - if(ci->service_routes){ - pkg_free(ci->service_routes); - } + if(ci->service_routes) { + pkg_free(ci->service_routes); + } } - if (DB_CAPABILITY(ul_dbf, DB_CAP_FETCH)) { - if(ul_dbf.fetch_result(_c, &res, ul_fetch_rows)<0) { + if(DB_CAPABILITY(ul_dbf, DB_CAP_FETCH)) { + if(ul_dbf.fetch_result(_c, &res, ul_fetch_rows) < 0) { LM_ERR("fetching rows (1) failed\n"); ul_dbf.free_result(_c, res); return -1; @@ -1094,7 +1206,7 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d) } else { break; } - } while(RES_ROW_N(res)>0); + } while(RES_ROW_N(res) > 0); ul_dbf.free_result(_c, res); @@ -1110,328 +1222,365 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d) return -1; } -int db_load_pcontact(udomain_t* _d, str *_aor, int insert_cache, struct pcontact** _c, pcontact_info_t* contact_info) +int db_load_pcontact(udomain_t *_d, str *_aor, int insert_cache, + struct pcontact **_c, pcontact_info_t *contact_info) { pcontact_info_t *ci; - db_key_t columns[15]; - db_key_t keys[2]; - db_val_t vals[2]; - db_op_t op[2]; - db1_res_t* res = NULL; - db_row_t *row; - int i; - str aor, port={0,0}; - pcontact_t* c = NULL; - - keys[0] = &aor_col; - vals[0].type = DB1_STR; - vals[0].nul = 0; - vals[0].val.str_val = *_aor; - op[0] = OP_EQ; - op[1] = OP_EQ; - - - columns[0] = &domain_col; - columns[1] = &aor_col; - columns[2] = &host_col; - columns[3] = &port_col; - columns[4] = &protocol_col; - columns[5] = &received_col; - columns[6] = &received_port_col; - columns[7] = &received_proto_col; - columns[8] = &rx_session_id_col; - columns[9] = ®_state_col; - columns[10] = &expires_col; - columns[11] = &socket_col; - columns[12] = &service_routes_col; - columns[13] = &public_ids_col; - columns[14] = &path_col; - - - - if (_aor->len>0 && _aor->s){ - LM_DBG("Querying database for P-CSCF contact [%.*s]\n", _aor->len, _aor->s); - } else { - LM_DBG("Querying database for P-CSCF received_host [%.*s] and received_port [%d]\n", contact_info->received_host.len, contact_info->received_host.s, contact_info->received_port); - keys[0] = &received_col; - vals[0].type = DB1_STR; - vals[0].nul = 0; - vals[0].val.str_val = contact_info->received_host; - keys[1] = &received_port_col; - vals[1].type = DB1_INT; - vals[1].nul = 0; - port.s = int2str(contact_info->received_port, &port.len); - vals[1].val.int_val = contact_info->received_port; - - } - - if (ul_dbf.use_table(ul_dbh, _d->name) < 0) { - LM_ERR("sql use_table failed\n"); - return -1; - } - - - if (ul_dbf.query(ul_dbh, keys, op, vals, columns, port.s ? 2 : 1, 15, 0, &res) < 0) { - if (!port.s) { - LM_ERR("Unable to query DB for location associated with aor [%.*s]\n", _aor->len, _aor->s); - } - else { - LM_ERR("Unable to query DB for location associated with host [%.*s] and port [%.*s]\n", contact_info->received_host.len, contact_info->received_host.s, port.len, port.s); - } - ul_dbf.free_result(ul_dbh, res); - return 0; - } - - if (RES_ROW_N(res) == 0) { - if (!port.s) { - LM_DBG("aor [%.*s] not found in table %.*s\n",_aor->len, _aor->s, _d->name->len, _d->name->s); - } - else { - LM_DBG("host [%.*s] and port [%.*s] not found in table %.*s\n", contact_info->received_host.len, contact_info->received_host.s, port.len, port.s, _d->name->len, _d->name->s); - } + db_key_t columns[15]; + db_key_t keys[2]; + db_val_t vals[2]; + db_op_t op[2]; + db1_res_t *res = NULL; + db_row_t *row; + int i; + str aor, port = {0, 0}; + pcontact_t *c = NULL; + + keys[0] = &aor_col; + vals[0].type = DB1_STR; + vals[0].nul = 0; + vals[0].val.str_val = *_aor; + op[0] = OP_EQ; + op[1] = OP_EQ; + + columns[0] = &domain_col; + columns[1] = &aor_col; + columns[2] = &host_col; + columns[3] = &port_col; + columns[4] = &protocol_col; + columns[5] = &received_col; + columns[6] = &received_port_col; + columns[7] = &received_proto_col; + columns[8] = &rx_session_id_col; + columns[9] = ®_state_col; + columns[10] = &expires_col; + columns[11] = &socket_col; + columns[12] = &service_routes_col; + columns[13] = &public_ids_col; + columns[14] = &path_col; + + + if(_aor->len > 0 && _aor->s) { + LM_DBG("Querying database for P-CSCF contact [%.*s]\n", _aor->len, + _aor->s); + } else { + LM_DBG("Querying database for P-CSCF received_host [%.*s] and " + "received_port [%d]\n", + contact_info->received_host.len, contact_info->received_host.s, + contact_info->received_port); + keys[0] = &received_col; + vals[0].type = DB1_STR; + vals[0].nul = 0; + vals[0].val.str_val = contact_info->received_host; + keys[1] = &received_port_col; + vals[1].type = DB1_INT; + vals[1].nul = 0; + port.s = int2str(contact_info->received_port, &port.len); + vals[1].val.int_val = contact_info->received_port; + } + + if(ul_dbf.use_table(ul_dbh, _d->name) < 0) { + LM_ERR("sql use_table failed\n"); + return -1; + } + + + if(ul_dbf.query( + ul_dbh, keys, op, vals, columns, port.s ? 2 : 1, 15, 0, &res) + < 0) { + if(!port.s) { + LM_ERR("Unable to query DB for location associated with aor " + "[%.*s]\n", + _aor->len, _aor->s); + } else { + LM_ERR("Unable to query DB for location associated with host " + "[%.*s] and port [%.*s]\n", + contact_info->received_host.len, + contact_info->received_host.s, port.len, port.s); + } ul_dbf.free_result(ul_dbh, res); - return 0; - } - LM_DBG("Handling Result for query received\n"); - - for(i = 0; i < RES_ROW_N(res); i++) { - row = RES_ROWS(res) + i; - - aor.s = (char*) VAL_STRING(ROW_VALUES(row) + 1); - if (VAL_NULL(ROW_VALUES(row) + 1) || aor.s == 0 || aor.s[0] == 0) { - LM_ERR("empty aor record in table %s...skipping\n", _d->name->s); - continue; - } - aor.len = strlen(aor.s); - - if ((_aor->len==0 && !_aor->s) && (VAL_NULL(ROW_VALUES(row) + 5) || VAL_NULL(ROW_VALUES(row) + 6))){ - LM_ERR("empty received_host or received_port record in table %s...skipping\n", _d->name->s); - continue; - } - LM_DBG("Convert database values extracted with AOR."); - ci = dbrow2info(ROW_VALUES(row) + 1, &aor); - if (!ci) { - LM_WARN("Failed to get contact info from DB.... continuing...\n"); - continue; - } - - if(!(insert_cache)){ - (*_c)->expires = ci->expires; - ul_dbf.free_result(ul_dbh, res); - if(ci->public_ids){ - pkg_free(ci->public_ids); - } - if(ci->service_routes){ - pkg_free(ci->service_routes); - } - LM_DBG("Freed memory in db_load_pcontact"); - return 1; - } - if(ci->reg_state==PCONTACT_REGISTERED){ - if ( (mem_insert_pcontact(_d, &aor, ci, &c)) != 0) { - if(ci->public_ids){ - pkg_free(ci->public_ids); - } - if(ci->service_routes){ - pkg_free(ci->service_routes); - } - LM_ERR("inserting contact failed\n"); - goto error; - } - }else { - if(ci->public_ids){ - pkg_free(ci->public_ids); - } - if(ci->service_routes){ - pkg_free(ci->service_routes); - } - LM_ERR("inserting contact failed\n"); - goto error1; + return 0; + } + + if(RES_ROW_N(res) == 0) { + if(!port.s) { + LM_DBG("aor [%.*s] not found in table %.*s\n", _aor->len, _aor->s, + _d->name->len, _d->name->s); + } else { + LM_DBG("host [%.*s] and port [%.*s] not found in table %.*s\n", + contact_info->received_host.len, + contact_info->received_host.s, port.len, port.s, + _d->name->len, _d->name->s); + } + + ul_dbf.free_result(ul_dbh, res); + return 0; + } + LM_DBG("Handling Result for query received\n"); + + for(i = 0; i < RES_ROW_N(res); i++) { + row = RES_ROWS(res) + i; + + aor.s = (char *)VAL_STRING(ROW_VALUES(row) + 1); + if(VAL_NULL(ROW_VALUES(row) + 1) || aor.s == 0 || aor.s[0] == 0) { + LM_ERR("empty aor record in table %s...skipping\n", _d->name->s); + continue; + } + aor.len = strlen(aor.s); + + if((_aor->len == 0 && !_aor->s) + && (VAL_NULL(ROW_VALUES(row) + 5) + || VAL_NULL(ROW_VALUES(row) + 6))) { + LM_ERR("empty received_host or received_port record in table " + "%s...skipping\n", + _d->name->s); + continue; + } + LM_DBG("Convert database values extracted with AOR."); + ci = dbrow2info(ROW_VALUES(row) + 1, &aor); + if(!ci) { + LM_WARN("Failed to get contact info from DB.... continuing...\n"); + continue; + } + + if(!(insert_cache)) { + (*_c)->expires = ci->expires; + ul_dbf.free_result(ul_dbh, res); + if(ci->public_ids) { + pkg_free(ci->public_ids); + } + if(ci->service_routes) { + pkg_free(ci->service_routes); + } + LM_DBG("Freed memory in db_load_pcontact"); + return 1; + } + if(ci->reg_state == PCONTACT_REGISTERED) { + if((mem_insert_pcontact(_d, &aor, ci, &c)) != 0) { + if(ci->public_ids) { + pkg_free(ci->public_ids); + } + if(ci->service_routes) { + pkg_free(ci->service_routes); } - if (exists_ulcb_type(PCSCF_CONTACT_INSERT)) { - run_ul_create_callbacks(c); - } + LM_ERR("inserting contact failed\n"); + goto error; + } + } else { + if(ci->public_ids) { + pkg_free(ci->public_ids); + } + if(ci->service_routes) { + pkg_free(ci->service_routes); + } + LM_ERR("inserting contact failed\n"); + goto error1; + } + if(exists_ulcb_type(PCSCF_CONTACT_INSERT)) { + run_ul_create_callbacks(c); + } - register_ulcb(c, PCSCF_CONTACT_DELETE | PCSCF_CONTACT_EXPIRE | PCSCF_CONTACT_UPDATE, cbp_registrar->callback, NULL); + register_ulcb(c, + PCSCF_CONTACT_DELETE | PCSCF_CONTACT_EXPIRE + | PCSCF_CONTACT_UPDATE, + cbp_registrar->callback, NULL); + + if(c->rx_session_id.len > 0) { + register_ulcb(c, PCSCF_CONTACT_DELETE | PCSCF_CONTACT_EXPIRE, + cbp_qos->callback, NULL); + } - if (c->rx_session_id.len > 0){ - register_ulcb(c, PCSCF_CONTACT_DELETE | PCSCF_CONTACT_EXPIRE, cbp_qos->callback, NULL); - } - //c->flags = c->flags|(1<public_ids){ - pkg_free(ci->public_ids); - } - if(ci->service_routes){ - pkg_free(ci->service_routes); - } + LM_DBG("inserting contact done\n"); + *_c = c; + if(ci->public_ids) { + pkg_free(ci->public_ids); + } + if(ci->service_routes) { + pkg_free(ci->service_routes); + } } - ul_dbf.free_result(ul_dbh, res); + ul_dbf.free_result(ul_dbh, res); return 1; error: - free_pcontact(c); + free_pcontact(c); error1: - ul_dbf.free_result(ul_dbh, res); - return 0; + ul_dbf.free_result(ul_dbh, res); + return 0; } -int get_pcontact(udomain_t* _d, pcontact_info_t* contact_info, struct pcontact** _c, int reverse_search) { - - int ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); - - if (ret && (db_mode == DB_ONLY)){ - LM_DBG("contact not found in cache for contact_info->received_port [%d]\n", contact_info->received_port); - if (contact_info->searchflag == SEARCH_RECEIVED){ - LM_DBG("Trying contact_info.extra_search_criteria = 0\n"); - contact_info->extra_search_criteria = 0; - ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); - if (ret == 0){ - return ret; - } - LM_DBG("contact not found in cache for contact_info->via_port [%d]\n", contact_info->via_port); - contact_info->extra_search_criteria = SEARCH_SERVICE_ROUTES; - - contact_info->searchflag = SEARCH_NORMAL; - LM_DBG("Trying contact_info.searchflag = SEARCH_NORMAL\n"); - ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); - if (ret == 0){ - return ret; - } - else { - LM_DBG("Trying contact_info.extra_search_criteria = 0\n"); - contact_info->extra_search_criteria = 0; - ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); - if (ret == 0){ - return ret; - } - LM_DBG("contact not found in cache for contact_info->via_port [%d]\n", contact_info->via_port); - contact_info->extra_search_criteria = SEARCH_SERVICE_ROUTES; - contact_info->searchflag = SEARCH_RECEIVED; - } - } - else { - LM_DBG("Trying contact_info.extra_search_criteria = 0\n"); - contact_info->extra_search_criteria = 0; - ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); - if (ret == 0){ - return ret; - } - LM_DBG("contact not found in cache for contact_info->via_port [%d]\n", contact_info->via_port); - contact_info->extra_search_criteria = SEARCH_SERVICE_ROUTES; - } - if (db_load_pcontact(_d, &contact_info->aor, 1/*insert_cache*/, _c, contact_info)){ - LM_DBG("loaded location from db for AOR [%.*s]\n", contact_info->aor.len, contact_info->aor.s); - return 0; - } else { - LM_DBG("download location DB failed for AOR [%.*s]\n", contact_info->aor.len, contact_info->aor.s); - return 1; - } - } - - return ret; +int get_pcontact(udomain_t *_d, pcontact_info_t *contact_info, + struct pcontact **_c, int reverse_search) +{ + int ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); + + if(ret && (db_mode == DB_ONLY)) { + LM_DBG("contact not found in cache for contact_info->received_port " + "[%d]\n", + contact_info->received_port); + if(contact_info->searchflag == SEARCH_RECEIVED) { + LM_DBG("Trying contact_info.extra_search_criteria = 0\n"); + contact_info->extra_search_criteria = 0; + ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); + if(ret == 0) { + return ret; + } + LM_DBG("contact not found in cache for contact_info->via_port " + "[%d]\n", + contact_info->via_port); + contact_info->extra_search_criteria = SEARCH_SERVICE_ROUTES; + + contact_info->searchflag = SEARCH_NORMAL; + LM_DBG("Trying contact_info.searchflag = SEARCH_NORMAL\n"); + ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); + if(ret == 0) { + return ret; + } else { + LM_DBG("Trying contact_info.extra_search_criteria = 0\n"); + contact_info->extra_search_criteria = 0; + ret = get_pcontact_from_cache( + _d, contact_info, _c, reverse_search); + if(ret == 0) { + return ret; + } + LM_DBG("contact not found in cache for contact_info->via_port " + "[%d]\n", + contact_info->via_port); + contact_info->extra_search_criteria = SEARCH_SERVICE_ROUTES; + contact_info->searchflag = SEARCH_RECEIVED; + } + } else { + LM_DBG("Trying contact_info.extra_search_criteria = 0\n"); + contact_info->extra_search_criteria = 0; + ret = get_pcontact_from_cache(_d, contact_info, _c, reverse_search); + if(ret == 0) { + return ret; + } + LM_DBG("contact not found in cache for contact_info->via_port " + "[%d]\n", + contact_info->via_port); + contact_info->extra_search_criteria = SEARCH_SERVICE_ROUTES; + } + if(db_load_pcontact(_d, &contact_info->aor, 1 /*insert_cache*/, _c, + contact_info)) { + LM_DBG("loaded location from db for AOR [%.*s]\n", + contact_info->aor.len, contact_info->aor.s); + return 0; + } else { + LM_DBG("download location DB failed for AOR [%.*s]\n", + contact_info->aor.len, contact_info->aor.s); + return 1; + } + } + + return ret; } -int audit_usrloc_expired_pcontacts(udomain_t* _d) { - - db1_res_t* location_rs = NULL; - pcontact_info_t *ci; - db_key_t columns[15]; - db_key_t keys[1]; - db_val_t vals[1]; - db_op_t op[1]; - db_row_t *row; - int i; - str aor; - pcontact_t* c = NULL; - - keys[0] = &expires_col; - vals[0].type = DB1_DATETIME; - vals[0].nul = 0; - vals[0].val.time_val = time(0) - expires_grace - audit_expired_pcontacts_timeout;; - op[0] = OP_LT; - - - columns[0] = &domain_col; - columns[1] = &aor_col; - columns[2] = &host_col; - columns[3] = &port_col; - columns[4] = &protocol_col; - columns[5] = &received_col; - columns[6] = &received_port_col; - columns[7] = &received_proto_col; - columns[8] = &rx_session_id_col; - columns[9] = ®_state_col; - columns[10] = &expires_col; - columns[11] = &socket_col; - columns[12] = &service_routes_col; - columns[13] = &public_ids_col; - columns[14] = &path_col; - - if (ul_dbf.use_table(ul_dbh, _d->name) < 0) { - LM_ERR("sql use_table failed\n"); - return -1; - } - - if (ul_dbf.query(ul_dbh, keys, op, vals, columns, 1, 15, 0, &location_rs) < 0) { - LM_ERR("Unable to query DB for expired pcontacts\n"); - ul_dbf.free_result(ul_dbh, location_rs); - } else { - if (RES_ROW_N(location_rs) == 0) { - LM_DBG("no expired pcontacts found in DB\n"); - ul_dbf.free_result(ul_dbh, location_rs); - goto done; - } - - for(i = 0; i < RES_ROW_N(location_rs); i++) { - row = RES_ROWS(location_rs) + i; - - aor.s = (char*) VAL_STRING(ROW_VALUES(row) + 1); - - if (VAL_NULL(ROW_VALUES(row) + 1) || aor.s == 0 || aor.s[0] == 0) { - LM_ERR("empty aor record in table %s...skipping\n", _d->name->s); - continue; - } - aor.len = strlen(aor.s); - ci = dbrow2info(ROW_VALUES(row) + 1, &aor); - if (!ci) { - LM_ERR("Failed to get contact info from DB.... continuing...\n"); - continue; - } - ci->aor = aor; - ci->searchflag = SEARCH_NORMAL; - ci->reg_state = PCONTACT_ANY; - lock_udomain(_d, &ci->via_host, ci->via_port, ci->via_prot); - if (get_pcontact_from_cache(_d, ci, &c, 0) == 0){ - LM_DBG("found pcontact [%.*s] in cache.....should have been cleared by expiry handler\n", aor.len, aor.s); - } - else{ - // insert pcontact - if (!(db_load_pcontact(_d, &aor, 1/*insert_cache*/, &c, ci))){ - LM_ERR("could not insert pcontact [%.*s] into cache\n", aor.len, aor.s); - } - } - unlock_udomain(_d, &ci->via_host, ci->via_port, ci->via_prot); - if(ci->public_ids){ - pkg_free(ci->public_ids); - } - if(ci->service_routes){ - pkg_free(ci->service_routes); - } - } - ul_dbf.free_result(ul_dbh, location_rs); - } +int audit_usrloc_expired_pcontacts(udomain_t *_d) +{ + + db1_res_t *location_rs = NULL; + pcontact_info_t *ci; + db_key_t columns[15]; + db_key_t keys[1]; + db_val_t vals[1]; + db_op_t op[1]; + db_row_t *row; + int i; + str aor; + pcontact_t *c = NULL; + + keys[0] = &expires_col; + vals[0].type = DB1_DATETIME; + vals[0].nul = 0; + vals[0].val.time_val = + time(0) - expires_grace - audit_expired_pcontacts_timeout; + ; + op[0] = OP_LT; + + + columns[0] = &domain_col; + columns[1] = &aor_col; + columns[2] = &host_col; + columns[3] = &port_col; + columns[4] = &protocol_col; + columns[5] = &received_col; + columns[6] = &received_port_col; + columns[7] = &received_proto_col; + columns[8] = &rx_session_id_col; + columns[9] = ®_state_col; + columns[10] = &expires_col; + columns[11] = &socket_col; + columns[12] = &service_routes_col; + columns[13] = &public_ids_col; + columns[14] = &path_col; + + if(ul_dbf.use_table(ul_dbh, _d->name) < 0) { + LM_ERR("sql use_table failed\n"); + return -1; + } + + if(ul_dbf.query(ul_dbh, keys, op, vals, columns, 1, 15, 0, &location_rs) + < 0) { + LM_ERR("Unable to query DB for expired pcontacts\n"); + ul_dbf.free_result(ul_dbh, location_rs); + } else { + if(RES_ROW_N(location_rs) == 0) { + LM_DBG("no expired pcontacts found in DB\n"); + ul_dbf.free_result(ul_dbh, location_rs); + goto done; + } + + for(i = 0; i < RES_ROW_N(location_rs); i++) { + row = RES_ROWS(location_rs) + i; + + aor.s = (char *)VAL_STRING(ROW_VALUES(row) + 1); + + if(VAL_NULL(ROW_VALUES(row) + 1) || aor.s == 0 || aor.s[0] == 0) { + LM_ERR("empty aor record in table %s...skipping\n", + _d->name->s); + continue; + } + aor.len = strlen(aor.s); + ci = dbrow2info(ROW_VALUES(row) + 1, &aor); + if(!ci) { + LM_ERR("Failed to get contact info from DB.... " + "continuing...\n"); + continue; + } + ci->aor = aor; + ci->searchflag = SEARCH_NORMAL; + ci->reg_state = PCONTACT_ANY; + lock_udomain(_d, &ci->via_host, ci->via_port, ci->via_prot); + if(get_pcontact_from_cache(_d, ci, &c, 0) == 0) { + LM_DBG("found pcontact [%.*s] in cache.....should have been " + "cleared by expiry handler\n", + aor.len, aor.s); + } else { + // insert pcontact + if(!(db_load_pcontact(_d, &aor, 1 /*insert_cache*/, &c, ci))) { + LM_ERR("could not insert pcontact [%.*s] into cache\n", + aor.len, aor.s); + } + } + unlock_udomain(_d, &ci->via_host, ci->via_port, ci->via_prot); + if(ci->public_ids) { + pkg_free(ci->public_ids); + } + if(ci->service_routes) { + pkg_free(ci->service_routes); + } + } + ul_dbf.free_result(ul_dbh, location_rs); + } done: - return 0; + return 0; } - diff --git a/src/modules/ims_usrloc_pcscf/udomain.h b/src/modules/ims_usrloc_pcscf/udomain.h index 5e190af2031..71fc145a577 100644 --- a/src/modules/ims_usrloc_pcscf/udomain.h +++ b/src/modules/ims_usrloc_pcscf/udomain.h @@ -40,7 +40,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ - + #ifndef UDOMAIN_H #define UDOMAIN_H @@ -55,34 +55,45 @@ #include "hslot.h" #include "usrloc.h" -struct hslot; /*!< Hash table slot */ +struct hslot; /*!< Hash table slot */ struct pcontact; /*!< contact record */ -int new_udomain(str* _n, int _s, udomain_t** _d); -void free_udomain(udomain_t* _d); -void print_udomain(FILE* _f, udomain_t* _d); +int new_udomain(str *_n, int _s, udomain_t **_d); +void free_udomain(udomain_t *_d); +void print_udomain(FILE *_f, udomain_t *_d); -void mem_timer_udomain(udomain_t* _d); -int mem_insert_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci, struct pcontact** _c); -void mem_delete_pcontact(udomain_t* _d, struct pcontact* _r); +void mem_timer_udomain(udomain_t *_d); +int mem_insert_pcontact(struct udomain *_d, str *_contact, + struct pcontact_info *_ci, struct pcontact **_c); +void mem_delete_pcontact(udomain_t *_d, struct pcontact *_r); -void lock_udomain(udomain_t* _d, str* _via_host, unsigned short via_port, unsigned short via_protot); -void unlock_udomain(udomain_t* _d, str* _via_host, unsigned short via_port, unsigned short via_proto); +void lock_udomain(udomain_t *_d, str *_via_host, unsigned short via_port, + unsigned short via_protot); +void unlock_udomain(udomain_t *_d, str *_via_host, unsigned short via_port, + unsigned short via_proto); -void lock_ulslot(udomain_t* _d, int i); -void unlock_ulslot(udomain_t* _d, int i); +void lock_ulslot(udomain_t *_d, int i); +void unlock_ulslot(udomain_t *_d, int i); -int update_rx_regsession(struct udomain* _d, str* session_id, struct pcontact* _c); -int update_pcontact(struct udomain* _d, struct pcontact_info* _ci, struct pcontact* _c); -int insert_pcontact(struct udomain* _d, str* _contact, struct pcontact_info* _ci, struct pcontact** _r); -int get_pcontact(udomain_t* _d, pcontact_info_t* contact_info, struct pcontact** _c, int reverse_search); -int assert_identity(udomain_t* _d, str * _host, unsigned short _port, unsigned short _proto, str * _identity); -int delete_pcontact(udomain_t* _d, struct pcontact* _r); -int unreg_pending_contacts_cb(udomain_t* _d, pcontact_t* _c, int type); -int update_security(udomain_t* _d, security_type _t, security_t* _s, struct pcontact* _c); -int update_temp_security(udomain_t* _d, security_type _t, security_t* _s, struct pcontact* _c); +int update_rx_regsession( + struct udomain *_d, str *session_id, struct pcontact *_c); +int update_pcontact( + struct udomain *_d, struct pcontact_info *_ci, struct pcontact *_c); +int insert_pcontact(struct udomain *_d, str *_contact, + struct pcontact_info *_ci, struct pcontact **_r); +int get_pcontact(udomain_t *_d, pcontact_info_t *contact_info, + struct pcontact **_c, int reverse_search); +int assert_identity(udomain_t *_d, str *_host, unsigned short _port, + unsigned short _proto, str *_identity); +int delete_pcontact(udomain_t *_d, struct pcontact *_r); +int unreg_pending_contacts_cb(udomain_t *_d, pcontact_t *_c, int type); +int update_security( + udomain_t *_d, security_type _t, security_t *_s, struct pcontact *_c); +int update_temp_security( + udomain_t *_d, security_type _t, security_t *_s, struct pcontact *_c); -int preload_udomain(db1_con_t* _c, udomain_t* _d); -int audit_usrloc_expired_pcontacts(udomain_t* _d); -int db_load_pcontact(udomain_t* _d, str *_aor, int insert_cache, struct pcontact** _c, pcontact_info_t* contact_info); +int preload_udomain(db1_con_t *_c, udomain_t *_d); +int audit_usrloc_expired_pcontacts(udomain_t *_d); +int db_load_pcontact(udomain_t *_d, str *_aor, int insert_cache, + struct pcontact **_c, pcontact_info_t *contact_info); #endif diff --git a/src/modules/ims_usrloc_pcscf/ul_callback.c b/src/modules/ims_usrloc_pcscf/ul_callback.c index 957c4b9a2ef..090b908b4e1 100644 --- a/src/modules/ims_usrloc_pcscf/ul_callback.c +++ b/src/modules/ims_usrloc_pcscf/ul_callback.c @@ -52,12 +52,13 @@ #include "../../core/mem/shm_mem.h" //#include "ul_callback.h" #include "../ims_usrloc_pcscf/usrloc.h" -struct ulcb_head_list* ulcb_list = 0; /*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); } -void destroy_ul_callbacks_list(struct ul_callback* cb) { +void destroy_ul_callbacks_list(struct ul_callback *cb) +{ struct ul_callback *cb_t; - while (cb) { + while(cb) { cb_t = cb; cb = cb->next; -// if (cb_t->callback_param_free && cb_t->param) { -// cb_t->callback_param_free(cb_t->param); -// cb_t->param = NULL; -// } //TODO: this is if we need/require a freeparam function + // if (cb_t->callback_param_free && cb_t->param) { + // cb_t->callback_param_free(cb_t->param); + // cb_t->param = NULL; + // } //TODO: this is if we need/require a freeparam function shm_free(cb_t); } } -int register_ulcb_method( struct pcontact *c, int types, ul_cb f, void *param ) +int register_ulcb_method(struct pcontact *c, int types, ul_cb f, void *param) { - //struct ul_callback *cbp; - - /* are the callback types valid?... */ - if ( types<0 || types>PCSCF_MAX ) { - LM_CRIT("invalid callback types: mask=%d\n",types); - return E_BUG; - } - /* we don't register null functions */ - if (f==0) { - LM_CRIT("null callback function\n"); - return E_BUG; - } - - /* build a new callback structure */ - if ( types & PCSCF_CONTACT_UPDATE){ - if (!(cbp_registrar=(struct ul_callback*)shm_malloc(sizeof( struct ul_callback)))) { - LM_ERR("no more share mem\n"); - return E_OUT_OF_MEM; - } - cbp_registrar->callback = f; - } - else{ - if (!(cbp_qos=(struct ul_callback*)shm_malloc(sizeof( struct ul_callback)))) { - LM_ERR("no more share mem\n"); - return E_OUT_OF_MEM; - } - cbp_qos->callback = f; - } - return 1; + //struct ul_callback *cbp; + + /* are the callback types valid?... */ + if(types < 0 || types > PCSCF_MAX) { + LM_CRIT("invalid callback types: mask=%d\n", types); + return E_BUG; + } + /* we don't register null functions */ + if(f == 0) { + LM_CRIT("null callback function\n"); + return E_BUG; + } + + /* build a new callback structure */ + if(types & PCSCF_CONTACT_UPDATE) { + if(!(cbp_registrar = (struct ul_callback *)shm_malloc( + sizeof(struct ul_callback)))) { + LM_ERR("no more share mem\n"); + return E_OUT_OF_MEM; + } + cbp_registrar->callback = f; + } else { + if(!(cbp_qos = (struct ul_callback *)shm_malloc( + sizeof(struct ul_callback)))) { + LM_ERR("no more share mem\n"); + return E_OUT_OF_MEM; + } + cbp_qos->callback = f; + } + return 1; } -int register_ulcb( struct pcontact *c, int types, ul_cb f, void *param ) +int register_ulcb(struct pcontact *c, int types, ul_cb f, void *param) { struct ul_callback *cbp; /* are the callback types valid?... */ - if ( types<0 || types>PCSCF_MAX ) { - LM_CRIT("invalid callback types: mask=%d\n",types); + if(types < 0 || types > PCSCF_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; } @@ -156,7 +160,7 @@ int register_ulcb( struct pcontact *c, int types, ul_cb f, void *param ) cbp->param = param; cbp->types = types; - if ( types==PCSCF_CONTACT_INSERT ) { + if(types == PCSCF_CONTACT_INSERT) { LM_DBG("TODO: check for registering callback before/after init\n"); /* link it into the proper place... */ cbp->next = ulcb_list->first; @@ -172,21 +176,24 @@ int register_ulcb( struct pcontact *c, int types, ul_cb f, void *param ) return 1; } -void delete_ulcb(struct pcontact* c, int type) +void delete_ulcb(struct pcontact *c, int type) { - struct ul_callback* cur; - struct ul_callback* prev; + struct ul_callback *cur; + struct ul_callback *prev; - if(c->cbs.first == 0 || ((c->cbs.reg_types) & type) == 0){ + if(c->cbs.first == 0 || ((c->cbs.reg_types) & type) == 0) { return; } // if the target is the first callback cur = c->cbs.first; - if(cur->types & type){ - if(cur->param){ - if(*((unsigned short*)cur->param) == c->received_port){ - LM_DBG("Removed ulcb from the head for contact: aor[%.*s], via port %u, received port %u, types 0x%02X\n", c->aor.len, c->aor.s, c->via_port, c->received_port, cur->types); + if(cur->types & type) { + if(cur->param) { + if(*((unsigned short *)cur->param) == c->received_port) { + LM_DBG("Removed ulcb from the head for contact: aor[%.*s], via " + "port %u, received port %u, types 0x%02X\n", + c->aor.len, c->aor.s, c->via_port, c->received_port, + cur->types); c->cbs.first = cur->next; shm_free(cur); return; @@ -196,12 +203,15 @@ void delete_ulcb(struct pcontact* c, int type) prev = c->cbs.first; cur = c->cbs.first->next; - while(cur){ - if(cur->types & type){ - if(cur->param){ - if(*((unsigned short*)cur->param) == c->received_port){ + while(cur) { + if(cur->types & type) { + if(cur->param) { + if(*((unsigned short *)cur->param) == c->received_port) { prev->next = cur->next; - LM_DBG("Removed ulcb for contact: aor[%.*s], via port %u, received port %u, types 0x%02X\n", c->aor.len, c->aor.s, c->via_port, c->received_port, cur->types); + LM_DBG("Removed ulcb for contact: aor[%.*s], via port %u, " + "received port %u, types 0x%02X\n", + c->aor.len, c->aor.s, c->via_port, c->received_port, + cur->types); shm_free(cur); return; } @@ -212,34 +222,36 @@ void delete_ulcb(struct pcontact* c, int type) cur = cur->next; } - LM_DBG("No ulcb has been deleted for contact: aor[%.*s], via port %u, received port %u\n", c->aor.len, c->aor.s, c->via_port, c->received_port); + LM_DBG("No ulcb has been deleted for contact: aor[%.*s], via port %u, " + "received port %u\n", + c->aor.len, c->aor.s, c->via_port, c->received_port); } -int is_ulcb_registered( struct pcontact *c, ul_cb f) +int is_ulcb_registered(struct pcontact *c, ul_cb f) { struct ul_callback *cbp; - for (cbp=c->cbs.first; cbp; cbp=cbp->next) { - if (cbp->callback == f) - return 1; - } - return 0; - + for(cbp = c->cbs.first; cbp; cbp = cbp->next) { + if(cbp->callback == f) + return 1; + } + return 0; }; /*! \brief run all transaction callbacks for an event type */ -void run_ul_callbacks( int type , struct pcontact *c) +void run_ul_callbacks(int type, struct pcontact *c) { struct ul_callback *cbp; - if (c->cbs.first == 0 || ((c->cbs.reg_types) & type) == 0) + if(c->cbs.first == 0 || ((c->cbs.reg_types) & type) == 0) return; - for (cbp=c->cbs.first; cbp; cbp=cbp->next) { - if ((cbp->types) & type) { - LM_DBG("contact=%p, callback type %d/%d entered\n", c, type, cbp->types); - cbp->callback( c, type, cbp->param ); + for(cbp = c->cbs.first; cbp; cbp = cbp->next) { + if((cbp->types) & type) { + LM_DBG("contact=%p, callback type %d/%d entered\n", c, type, + cbp->types); + cbp->callback(c, type, cbp->param); } } return; @@ -249,9 +261,8 @@ void run_ul_create_callbacks(struct pcontact *c) { struct ul_callback *cbp; - for (cbp = ulcb_list->first; cbp; cbp = cbp->next) { - LM_DBG("contact=%p, callback type PCSCF_CONTACT_INSERT entered\n", c); + for(cbp = ulcb_list->first; cbp; cbp = cbp->next) { + LM_DBG("contact=%p, callback type PCSCF_CONTACT_INSERT entered\n", c); cbp->callback(c, PCSCF_CONTACT_INSERT, cbp->param); } } - diff --git a/src/modules/ims_usrloc_pcscf/ul_callback.h b/src/modules/ims_usrloc_pcscf/ul_callback.h index a2c36eec8cf..b659c069a92 100644 --- a/src/modules/ims_usrloc_pcscf/ul_callback.h +++ b/src/modules/ims_usrloc_pcscf/ul_callback.h @@ -42,55 +42,59 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ - + #ifndef _UL_CALLBACKS_H #define _UL_CALLBACKS_H #include "../../core/dprint.h" #include "../../core/parser/msg_parser.h" -#include "../../core/parser/contact/parse_contact.h" +#include "../../core/parser/contact/parse_contact.h" #include "../../core/ut.h" -#include "../ims_usrloc_pcscf/usrloc.h" +#include "../ims_usrloc_pcscf/usrloc.h" #include "../../lib/ims/ims_getters.h" struct pcontact; -#define PCSCF_CONTACT_INSERT (1<<0) -#define PCSCF_CONTACT_UPDATE (1<<1) -#define PCSCF_CONTACT_DELETE (1<<2) -#define PCSCF_CONTACT_EXPIRE (1<<3) -#define PCSCF_MAX ((1<<4)-1) +#define PCSCF_CONTACT_INSERT (1 << 0) +#define PCSCF_CONTACT_UPDATE (1 << 1) +#define PCSCF_CONTACT_DELETE (1 << 2) +#define PCSCF_CONTACT_EXPIRE (1 << 3) +#define PCSCF_MAX ((1 << 4) - 1) -typedef void (ul_cb) (struct pcontact *c, int type, void *param); /*! \brief callback function prototype */ -typedef int (*register_ulcb_t)(struct pcontact *c, int cb_types, ul_cb f, void *param); /*! \brief register callback function prototype */ +typedef void(ul_cb)(struct pcontact *c, int type, + void *param); /*! \brief callback function prototype */ +typedef int (*register_ulcb_t)(struct pcontact *c, int cb_types, ul_cb f, + void *param); /*! \brief register callback function prototype */ typedef int (*is_ulcb_registered_t)(struct pcontact *c, ul_cb f); -struct ul_callback { - int types; /*!< types of events that trigger the callback*/ - ul_cb* callback; /*!< callback function */ - void *param; /*!< param to be passed to callback function */ +struct ul_callback +{ + int types; /*!< types of events that trigger the callback*/ + ul_cb *callback; /*!< callback function */ + void *param; /*!< param to be passed to callback function */ //param_free_cb* callback_param_free; - struct ul_callback* next; + struct ul_callback *next; }; -struct ulcb_head_list { +struct ulcb_head_list +{ struct ul_callback *first; int reg_types; }; -extern struct ulcb_head_list* ulcb_list; /*!< this is the list for the INSERT callbacks*/ +extern struct ulcb_head_list + *ulcb_list; /*!< this is the list for the INSERT callbacks*/ -#define exists_ulcb_type(_types_) \ - ( (ulcb_list->reg_types)|(_types_) ) +#define exists_ulcb_type(_types_) ((ulcb_list->reg_types) | (_types_)) int init_ulcb_list(void); void destroy_ulcb_list(void); -void destroy_ul_callbacks_list(struct ul_callback* cb); -int register_ulcb( struct pcontact *c, int types, ul_cb f, void *param); -void delete_ulcb(struct pcontact* c, int type); -int register_ulcb_method( struct pcontact *c, int types, ul_cb f, void *param); -void run_ul_callbacks( int type , struct pcontact *c); +void destroy_ul_callbacks_list(struct ul_callback *cb); +int register_ulcb(struct pcontact *c, int types, ul_cb f, void *param); +void delete_ulcb(struct pcontact *c, int type); +int register_ulcb_method(struct pcontact *c, int types, ul_cb f, void *param); +void run_ul_callbacks(int type, struct pcontact *c); void run_ul_create_callbacks(struct pcontact *c); int is_ulcb_registered(struct pcontact *c, ul_cb f); diff --git a/src/modules/ims_usrloc_pcscf/ul_rpc.c b/src/modules/ims_usrloc_pcscf/ul_rpc.c index 91d95dd59f9..96dd0f14c54 100644 --- a/src/modules/ims_usrloc_pcscf/ul_rpc.c +++ b/src/modules/ims_usrloc_pcscf/ul_rpc.c @@ -42,7 +42,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ - + #include "../../core/ip_addr.h" #include "../../core/dprint.h" @@ -52,125 +52,126 @@ #include "pcontact.h" #include "utime.h" -static const char* ul_rpc_dump_doc[2] = { - "Dump PCSCF contacts and associated identitites", - 0 -}; +static const char *ul_rpc_dump_doc[2] = { + "Dump PCSCF contacts and associated identitites", 0}; -static void ul_rpc_dump(rpc_t* rpc, void* ctx) { - dlist_t* dl; - udomain_t* dom; +static void ul_rpc_dump(rpc_t *rpc, void *ctx) +{ + dlist_t *dl; + udomain_t *dom; time_t t; - void* th; - void* ah; - void* sh; + void *th; + void *ah; + void *sh; int max, n, i; - pcontact_t* c; + pcontact_t *c; t = time(0); - for (dl = root; dl; dl = dl->next) { + for(dl = root; dl; dl = dl->next) { dom = dl->d; - if (rpc->add(ctx, "{", &th) < 0) { + if(rpc->add(ctx, "{", &th) < 0) { rpc->fault(ctx, 500, "Internal error creating top rpc"); return; } - if (rpc->struct_add(th, "Sd{", "Domain", &dl->name, "Size", - (int) dom->size, "AoRs", &ah) < 0) { + if(rpc->struct_add(th, "Sd{", "Domain", &dl->name, "Size", + (int)dom->size, "AoRs", &ah) + < 0) { rpc->fault(ctx, 500, "Internal error creating inner struct"); return; } - for (i = 0, n = 0, max = 0; i < dom->size; i++) { + for(i = 0, n = 0, max = 0; i < dom->size; i++) { lock_ulslot(dom, i); n += dom->table[i].n; - if (max < dom->table[i].n) + if(max < dom->table[i].n) max = dom->table[i].n; - for (c = dom->table[i].first; c; c = c->next) { - if (rpc->struct_add(ah, "S", "AoR", &c->aor) < 0) { + for(c = dom->table[i].first; c; c = c->next) { + if(rpc->struct_add(ah, "S", "AoR", &c->aor) < 0) { unlock_ulslot(dom, i); rpc->fault(ctx, 500, "Internal error creating aor struct"); return; } - if (rpc->struct_add(ah, "s", "State", reg_state_to_string(c->reg_state)) < 0) { + if(rpc->struct_add( + ah, "s", "State", reg_state_to_string(c->reg_state)) + < 0) { unlock_ulslot(dom, i); - rpc->fault(ctx, 500, "Internal error creating reg state struct"); + rpc->fault(ctx, 500, + "Internal error creating reg state struct"); return; } - if (c->expires == 0) { - if (rpc->struct_add(ah, "s", "Expires", "permanent") < 0) { + if(c->expires == 0) { + if(rpc->struct_add(ah, "s", "Expires", "permanent") < 0) { unlock_ulslot(dom, i); rpc->fault(ctx, 500, "Internal error adding expire"); return; } - } else if (c->expires == -1/*UL_EXPIRED_TIME*/) { - if (rpc->struct_add(ah, "s", "Expires", "deleted") < 0) { + } else if(c->expires == -1 /*UL_EXPIRED_TIME*/) { + if(rpc->struct_add(ah, "s", "Expires", "deleted") < 0) { unlock_ulslot(dom, i); rpc->fault(ctx, 500, "Internal error adding expire"); return; } - } else if (t > c->expires) { - if (rpc->struct_add(ah, "s", "Expires", "expired") < 0) { + } else if(t > c->expires) { + if(rpc->struct_add(ah, "s", "Expires", "expired") < 0) { unlock_ulslot(dom, i); rpc->fault(ctx, 500, "Internal error adding expire"); return; } } else { - if (rpc->struct_add(ah, "d", "Expires", (int) (c->expires - t)) < 0) { + if(rpc->struct_add( + ah, "d", "Expires", (int)(c->expires - t)) + < 0) { unlock_ulslot(dom, i); rpc->fault(ctx, 500, "Internal error adding expire"); return; } } - if (rpc->struct_add(ah, "S", "Path", &c->path) < 0) { + if(rpc->struct_add(ah, "S", "Path", &c->path) < 0) { unlock_ulslot(dom, i); rpc->fault(ctx, 500, "Internal error creating path struct"); return; } - // if (rpc->struct_add(ah, "{", "Service Routes", &sr) < 0) { - // unlock_ulslot(dom, i); - // rpc->fault(ctx, 500, "Internal error creating Service Routes"); - // return; - // } + // if (rpc->struct_add(ah, "{", "Service Routes", &sr) < 0) { + // unlock_ulslot(dom, i); + // rpc->fault(ctx, 500, "Internal error creating Service Routes"); + // return; + // } - // for (j = 0; j < c->num_service_routes; j++) { - // if (rpc->struct_add(sr, "S", "Route", &c->service_routes[j]) < 0) { - // unlock_ulslot(dom, i); - // rpc->fault(ctx, 500, "Internal error creating Service Route struct"); - // return; - // } - // } + // for (j = 0; j < c->num_service_routes; j++) { + // if (rpc->struct_add(sr, "S", "Route", &c->service_routes[j]) < 0) { + // unlock_ulslot(dom, i); + // rpc->fault(ctx, 500, "Internal error creating Service Route struct"); + // return; + // } + // } - // if (rpc->struct_add(ah, "{", "Public Identities", &ih) < 0) { - // unlock_ulslot(dom, i); - // rpc->fault(ctx, 500, "Internal error creating IMPU struct"); - // return; - // } + // if (rpc->struct_add(ah, "{", "Public Identities", &ih) < 0) { + // unlock_ulslot(dom, i); + // rpc->fault(ctx, 500, "Internal error creating IMPU struct"); + // return; + // } - // for (p = c->head; p; p = p->next) { - // if (rpc->struct_add(ih, "S", "IMPU", &p->public_identity) < 0) { - // unlock_ulslot(dom, i); - // rpc->fault(ctx, 500, "Internal error creating IMPU struct"); - // return; - // } - // } + // for (p = c->head; p; p = p->next) { + // if (rpc->struct_add(ih, "S", "IMPU", &p->public_identity) < 0) { + // unlock_ulslot(dom, i); + // rpc->fault(ctx, 500, "Internal error creating IMPU struct"); + // return; + // } + // } } unlock_ulslot(dom, i); } - if (rpc->struct_add(ah, "{", "Stats", &sh) > 0) { + if(rpc->struct_add(ah, "{", "Stats", &sh) > 0) { rpc->fault(ctx, 500, "Internal error creating stats"); } - if (rpc->struct_add(sh, "dd", "Records", n, "Max-Slots", max) < 0) { + if(rpc->struct_add(sh, "dd", "Records", n, "Max-Slots", max) < 0) { rpc->fault(ctx, 500, "Internal error creating stats struct"); } } } rpc_export_t ul_rpc[] = { - {"ulpcscf.status", ul_rpc_dump, ul_rpc_dump_doc, 0}, - {0, 0, 0, 0} -}; - - + {"ulpcscf.status", ul_rpc_dump, ul_rpc_dump_doc, 0}, {0, 0, 0, 0}}; diff --git a/src/modules/ims_usrloc_pcscf/usrloc.c b/src/modules/ims_usrloc_pcscf/usrloc.c index f3aa7be7ca5..3bf30adbfd2 100644 --- a/src/modules/ims_usrloc_pcscf/usrloc.c +++ b/src/modules/ims_usrloc_pcscf/usrloc.c @@ -42,7 +42,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ - + #include "usrloc.h" #include "dlist.h" #include "pcontact.h" @@ -57,14 +57,15 @@ struct ul_callback *cbp_registrar = 0; struct ul_callback *cbp_qos = 0; -int bind_usrloc(usrloc_api_t* api) { - if (!api) { +int bind_usrloc(usrloc_api_t *api) +{ + if(!api) { LM_ERR("invalid parameter value\n"); return -1; } - if (ims_ulp_init_flag == 0) { + if(ims_ulp_init_flag == 0) { LM_ERR("configuration error - trying to bind to usrloc module" - " before being initialized\n"); + " before being initialized\n"); return -1; } @@ -74,7 +75,7 @@ int bind_usrloc(usrloc_api_t* api) { api->unlock_udomain = unlock_udomain; api->insert_pcontact = insert_pcontact; api->delete_pcontact = delete_pcontact; - api->unreg_pending_contacts_cb = unreg_pending_contacts_cb; + api->unreg_pending_contacts_cb = unreg_pending_contacts_cb; api->get_pcontact = get_pcontact; api->assert_identity = assert_identity; api->update_pcontact = update_pcontact; @@ -87,74 +88,80 @@ int bind_usrloc(usrloc_api_t* api) { api->is_ulcb_registered = is_ulcb_registered; api->register_ulcb_method = register_ulcb_method; - api->db_mode = db_mode; + api->db_mode = db_mode; return 0; } -#define ALIAS "alias=" +#define ALIAS "alias=" #define ALIAS_LEN (sizeof(ALIAS) - 1) -int get_alias_host_from_contact(str *contact_uri_params, str *alias_host) { - char *rest, *sep; - unsigned int rest_len; - - rest = contact_uri_params->s; - rest_len = contact_uri_params->len; - if (rest_len == 0) { - LM_DBG("no params\n"); - return -1; - } - - /*Get full alias parameter*/ - while (rest_len >= ALIAS_LEN) { - if (strncmp(rest, ALIAS, ALIAS_LEN) == 0) break; - sep = memchr(rest, 59 /* ; */, rest_len); - if (sep == NULL) { - LM_DBG("no alias param\n"); - return -1; - } else { - rest_len = rest_len - (sep - rest + 1); - rest = sep + 1; - } - } - - if (rest_len < ALIAS_LEN) { - LM_DBG("no alias param\n"); - return -1; - } - - alias_host->s = rest + ALIAS_LEN; - alias_host->len = rest_len - ALIAS_LEN; - - /*Get host from alias*/ - rest = memchr(alias_host->s, 126 /* ~ */, alias_host->len); - if (rest == NULL) { - LM_ERR("no '~' in alias param value\n"); - return -1; - } - alias_host->len = rest - alias_host->s; - LM_DBG("Alias host to return [%.*s]\n", alias_host->len, alias_host->s); - return 0; +int get_alias_host_from_contact(str *contact_uri_params, str *alias_host) +{ + char *rest, *sep; + unsigned int rest_len; + + rest = contact_uri_params->s; + rest_len = contact_uri_params->len; + if(rest_len == 0) { + LM_DBG("no params\n"); + return -1; + } + + /*Get full alias parameter*/ + while(rest_len >= ALIAS_LEN) { + if(strncmp(rest, ALIAS, ALIAS_LEN) == 0) + break; + sep = memchr(rest, 59 /* ; */, rest_len); + if(sep == NULL) { + LM_DBG("no alias param\n"); + return -1; + } else { + rest_len = rest_len - (sep - rest + 1); + rest = sep + 1; + } + } + + if(rest_len < ALIAS_LEN) { + LM_DBG("no alias param\n"); + return -1; + } + + alias_host->s = rest + ALIAS_LEN; + alias_host->len = rest_len - ALIAS_LEN; + + /*Get host from alias*/ + rest = memchr(alias_host->s, 126 /* ~ */, alias_host->len); + if(rest == NULL) { + LM_ERR("no '~' in alias param value\n"); + return -1; + } + alias_host->len = rest - alias_host->s; + LM_DBG("Alias host to return [%.*s]\n", alias_host->len, alias_host->s); + return 0; } /* return the slot id for inserting contacts in the hash */ -unsigned int get_hash_slot(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto) { - unsigned int sl; +unsigned int get_hash_slot(udomain_t *_d, str *via_host, + unsigned short via_port, unsigned short via_proto) +{ + unsigned int sl; - sl = get_aor_hash(_d, via_host, via_port, via_proto); - sl = sl & (_d->size - 1) ; - LM_DBG("Returning hash slot: [%d]\n", sl); + sl = get_aor_hash(_d, via_host, via_port, via_proto); + sl = sl & (_d->size - 1); + LM_DBG("Returning hash slot: [%d]\n", sl); - return sl; + return sl; } -unsigned int get_aor_hash(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto) { - unsigned int aorhash; - - aorhash = core_hash(via_host, 0, 0); - LM_DBG("Returning hash: [%u]\n", aorhash); +unsigned int get_aor_hash(udomain_t *_d, str *via_host, unsigned short via_port, + unsigned short via_proto) +{ + unsigned int aorhash; + + aorhash = core_hash(via_host, 0, 0); + LM_DBG("Returning hash: [%u]\n", aorhash); - return aorhash; + return aorhash; } diff --git a/src/modules/ims_usrloc_pcscf/usrloc.h b/src/modules/ims_usrloc_pcscf/usrloc.h index 1596666b267..8adfdfdcfdb 100644 --- a/src/modules/ims_usrloc_pcscf/usrloc.h +++ b/src/modules/ims_usrloc_pcscf/usrloc.h @@ -41,7 +41,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ - + #ifndef USRLOC_H #define USRLOC_H @@ -54,249 +54,279 @@ #include "../../modules/tm/dlg.h" #include "../cdp/diameter_ims_code_avp.h" -#define NO_DB 0 +#define NO_DB 0 #define WRITE_THROUGH 1 -#define WRITE_BACK 2 //not implemented yet -#define DB_ONLY 3 +#define WRITE_BACK 2 //not implemented yet +#define DB_ONLY 3 #define SEARCH_NORMAL 0 #define SEARCH_RECEIVED 1 -#define ALIAS "alias=" +#define ALIAS "alias=" #define ALIAS_LEN (sizeof(ALIAS) - 1) -#define RINSTANCE "rinstance=" +#define RINSTANCE "rinstance=" #define RINSTANCE_LEN (sizeof(RINSTANCE) - 1) -#define VALID_CONTACT(c, t) ((c->expires>t) || (c->expires==0)) +#define VALID_CONTACT(c, t) ((c->expires > t) || (c->expires == 0)) -#define SEARCH_SERVICE_ROUTES 1<<0 /* bitmap of extra search functionality when searching for contacts */ +#define SEARCH_SERVICE_ROUTES \ + 1 << 0 /* bitmap of extra search functionality when searching for contacts */ struct hslot; /*!< Hash table slot */ struct socket_info; int get_alias_host_from_contact(str *contact_uri_params, str *alias_host); -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 *contacts; /*!< no of registered contacts */ - stat_var *expired; /*!< no of expires */ +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 *contacts; /*!< no of registered contacts */ + stat_var *expired; /*!< no of expires */ }; typedef struct udomain udomain_t; /** Public Identity Structure */ -typedef struct { - char barring; /**< Barring state */ - str public_identity; /**< Public Identity string */ - str wildcarded_psi; /** if exists is the wildcarded psi */ +typedef struct +{ + char barring; /**< Barring state */ + str public_identity; /**< Public Identity string */ + str wildcarded_psi; /** if exists is the wildcarded psi */ } ims_public_identity; /** TLS SA Information */ -typedef struct tls { - unsigned short port_tls; /**< Port UE TLS */ - unsigned long session_hash; +typedef struct tls +{ + unsigned short port_tls; /**< Port UE TLS */ + unsigned long session_hash; } tls_t; /** IPSec SA Information */ -typedef struct ipsec { - unsigned int spi_uc; /**< SPI Client to use */ - unsigned int spi_us; /**< SPI Server to use */ - unsigned int spi_pc; /**< SPI Client to use */ - unsigned int spi_ps; /**< SPI Server to use */ - unsigned short port_uc; /**< Port UE Client */ - unsigned short port_us; /**< Port UE Server */ - unsigned short port_pc; /**< Port Proxy Client */ - unsigned short port_ps; /**< Port Proxy Server */ - - str ealg; /**< Cypher Algorithm - ESP */ - str r_ealg; /**< received Cypher Algorithm - ESP */ - str ck; /**< Cypher Key */ - str alg; /**< Integrity Algorithm - AH */ - str r_alg; /**aor.len, _c->aor.s, _c->received_port); + + LM_DBG("updating pcontact: aor[%.*s], received port %u\n", _c->aor.len, + _c->aor.s, _c->received_port); VAL_TYPE(match_values) = DB1_STR; VAL_NULL(match_values) = 0; VAL_STR(match_values) = _c->aor; - VAL_TYPE(match_values + 1) = DB1_INT; - VAL_NULL(match_values + 1) = 0; - VAL_INT(match_values + 1) = _c->received_port; - - op[0]=OP_EQ; - op[1]=OP_EQ; + VAL_TYPE(match_values + 1) = DB1_INT; + VAL_NULL(match_values + 1) = 0; + VAL_INT(match_values + 1) = _c->received_port; + + op[0] = OP_EQ; + op[1] = OP_EQ; - if (use_location_pcscf_table(_c->domain) < 0) { - LM_ERR("Error trying to use table %.*s\n", _c->domain->len, _c->domain->s); + if(use_location_pcscf_table(_c->domain) < 0) { + LM_ERR("Error trying to use table %.*s\n", _c->domain->len, + _c->domain->s); return -1; } - VAL_TYPE(values) = DB1_DATETIME; - VAL_TIME(values) = _c->expires; - VAL_NULL(values) = 0; + VAL_TYPE(values) = DB1_DATETIME; + VAL_TIME(values) = _c->expires; + VAL_NULL(values) = 0; - VAL_TYPE(values + 1)= DB1_INT; - VAL_NULL(values + 1)= 0; - VAL_INT(values + 1) = _c->reg_state; + VAL_TYPE(values + 1) = DB1_INT; + VAL_NULL(values + 1) = 0; + VAL_INT(values + 1) = _c->reg_state; str empty_str = str_init(""); - if (_c->service_routes) { - service_routes.len = service_routes_as_string(_c, &service_route_buffer); + if(_c->service_routes) { + service_routes.len = + service_routes_as_string(_c, &service_route_buffer); service_routes.s = service_route_buffer.buf; } - SET_STR_VALUE(values + 2, (_c->service_routes)?service_routes:empty_str); + SET_STR_VALUE( + values + 2, (_c->service_routes) ? service_routes : empty_str); VAL_TYPE(values + 2) = DB1_STR; VAL_NULL(values + 2) = 0; @@ -180,17 +184,18 @@ int db_update_pcontact(pcontact_t* _c) VAL_TYPE(values + 3) = DB1_STR; VAL_NULL(values + 3) = 0; - VAL_TYPE(values + 4)= DB1_INT; - VAL_NULL(values + 4)= 0; - VAL_INT(values + 4) = _c->received_port; + VAL_TYPE(values + 4) = DB1_INT; + VAL_NULL(values + 4) = 0; + VAL_INT(values + 4) = _c->received_port; - VAL_TYPE(values + 5)= DB1_INT; - VAL_NULL(values + 5)= 0; - VAL_INT(values + 5) = _c->received_proto; + VAL_TYPE(values + 5) = DB1_INT; + VAL_NULL(values + 5) = 0; + VAL_INT(values + 5) = _c->received_proto; VAL_TYPE(values + 6) = DB1_STR; SET_PROPER_NULL_FLAG(_c->rx_session_id, values, 6); - LM_DBG("Trying to set rx session id: %.*s\n", _c->rx_session_id.len, _c->rx_session_id.s); + LM_DBG("Trying to set rx session id: %.*s\n", _c->rx_session_id.len, + _c->rx_session_id.s); SET_STR_VALUE(values + 6, _c->rx_session_id); /* add the public identities */ @@ -200,15 +205,17 @@ int db_update_pcontact(pcontact_t* _c) SET_PROPER_NULL_FLAG(impus, values, 7); SET_STR_VALUE(values + 7, impus); - if((ul_dbf.update(ul_dbh, match_keys, op, match_values, update_keys,values, 2, 8)) !=0){ + if((ul_dbf.update( + ul_dbh, match_keys, op, match_values, update_keys, values, 2, 8)) + != 0) { LM_ERR("could not update database info\n"); - return -1; + return -1; } - if (ul_dbf.affected_rows && ul_dbf.affected_rows(ul_dbh) == 0) { + if(ul_dbf.affected_rows && ul_dbf.affected_rows(ul_dbh) == 0) { LM_DBG("no existing rows for an update... doing insert\n"); - if (db_insert_pcontact(_c) != 0) { + if(db_insert_pcontact(_c) != 0) { LM_ERR("Failed to insert a pcontact on update\n"); } } @@ -216,11 +223,12 @@ int db_update_pcontact(pcontact_t* _c) return 0; } -int db_delete_pcontact(pcontact_t* _c) +int db_delete_pcontact(pcontact_t *_c) { - LM_DBG("Trying to delete contact: aor[%.*s], received port %u\n", _c->aor.len, _c->aor.s, _c->received_port); + LM_DBG("Trying to delete contact: aor[%.*s], received port %u\n", + _c->aor.len, _c->aor.s, _c->received_port); db_val_t values[2]; - db_key_t match_keys[2] = { &aor_col, &received_port_col }; + db_key_t match_keys[2] = {&aor_col, &received_port_col}; VAL_TYPE(values) = DB1_STR; VAL_NULL(values) = 0; @@ -228,39 +236,35 @@ int db_delete_pcontact(pcontact_t* _c) VAL_TYPE(values + 1) = DB1_INT; VAL_NULL(values + 1) = 0; - VAL_INT(values + 1) = _c->received_port; + VAL_INT(values + 1) = _c->received_port; - if (use_location_pcscf_table(_c->domain) < 0) { - LM_ERR("Error trying to use table %.*s\n", _c->domain->len, _c->domain->s); + if(use_location_pcscf_table(_c->domain) < 0) { + LM_ERR("Error trying to use table %.*s\n", _c->domain->len, + _c->domain->s); return -1; } if(ul_dbf.delete(ul_dbh, match_keys, 0, values, 2) < 0) { - LM_ERR("Failed to delete database information: aor[%.*s], received port %u, rx_session_id=[%.*s]\n", - _c->aor.len, _c->aor.s, - _c->received_port, - _c->rx_session_id.len, _c->rx_session_id.s); + LM_ERR("Failed to delete database information: aor[%.*s], received " + "port %u, rx_session_id=[%.*s]\n", + _c->aor.len, _c->aor.s, _c->received_port, + _c->rx_session_id.len, _c->rx_session_id.s); return -1; } return 0; } -int db_insert_pcontact(struct pcontact* _c) +int db_insert_pcontact(struct pcontact *_c) { str empty_str = str_init(""); str impus, service_routes; - db_key_t keys[16] = { - &domain_col, - &aor_col, - &received_col, - &received_port_col, &received_proto_col, - &path_col, &rinstance_col, - &rx_session_id_col, ®_state_col, - &expires_col, &service_routes_col, - &socket_col, &public_ids_col, &host_col, &port_col, &protocol_col - }; + db_key_t keys[16] = {&domain_col, &aor_col, &received_col, + &received_port_col, &received_proto_col, &path_col, &rinstance_col, + &rx_session_id_col, ®_state_col, &expires_col, + &service_routes_col, &socket_col, &public_ids_col, &host_col, + &port_col, &protocol_col}; db_val_t values[16]; VAL_TYPE(GET_FIELD_IDX(values, LP_DOMAIN_IDX)) = DB1_STR; @@ -279,10 +283,11 @@ int db_insert_pcontact(struct pcontact* _c) VAL_TYPE(GET_FIELD_IDX(values, LP_HOST_IDX)) = DB1_STR; VAL_TYPE(GET_FIELD_IDX(values, LP_PORT_IDX)) = DB1_INT; VAL_TYPE(GET_FIELD_IDX(values, LP_PROTOCOL_IDX)) = DB1_INT; - + SET_STR_VALUE(GET_FIELD_IDX(values, LP_DOMAIN_IDX), (*_c->domain)); - SET_STR_VALUE(GET_FIELD_IDX(values, LP_AOR_IDX), _c->aor); //TODO: need to clean AOR + SET_STR_VALUE(GET_FIELD_IDX(values, LP_AOR_IDX), + _c->aor); //TODO: need to clean AOR SET_STR_VALUE(GET_FIELD_IDX(values, LP_RECEIVED_IDX), _c->received_host); SET_STR_VALUE(GET_FIELD_IDX(values, LP_HOST_IDX), _c->via_host); @@ -302,7 +307,8 @@ int db_insert_pcontact(struct pcontact* _c) SET_STR_VALUE(GET_FIELD_IDX(values, LP_PATH_IDX), _c->path); SET_STR_VALUE(GET_FIELD_IDX(values, LP_RINSTANCE_IDX), _c->rinstance); - SET_STR_VALUE(GET_FIELD_IDX(values, LP_RX_SESSION_ID_IDX), _c->rx_session_id); + SET_STR_VALUE( + GET_FIELD_IDX(values, LP_RX_SESSION_ID_IDX), _c->rx_session_id); SET_PROPER_NULL_FLAG(_c->path, values, LP_PATH_IDX); SET_PROPER_NULL_FLAG(_c->rinstance, values, LP_RINSTANCE_IDX); SET_PROPER_NULL_FLAG(_c->rx_session_id, values, LP_RX_SESSION_ID_IDX); @@ -312,19 +318,21 @@ int db_insert_pcontact(struct pcontact* _c) VAL_NULL(GET_FIELD_IDX(values, LP_REG_STATE_IDX)) = 0; VAL_NULL(GET_FIELD_IDX(values, LP_EXPIRES_IDX)) = 0; - SET_STR_VALUE(GET_FIELD_IDX(values, LP_SERVICE_ROUTES_IDX), _c->service_routes?(*_c->service_routes):empty_str); + SET_STR_VALUE(GET_FIELD_IDX(values, LP_SERVICE_ROUTES_IDX), + _c->service_routes ? (*_c->service_routes) : empty_str); VAL_NULL(GET_FIELD_IDX(values, LP_SERVICE_ROUTES_IDX)) = 1; - SET_STR_VALUE(GET_FIELD_IDX(values, LP_SOCKET_IDX), _c->sock?_c->sock->sock_str:empty_str); + SET_STR_VALUE(GET_FIELD_IDX(values, LP_SOCKET_IDX), + _c->sock ? _c->sock->sock_str : empty_str); VAL_NULL(GET_FIELD_IDX(values, LP_SOCKET_IDX)) = 1; - if (_c->service_routes) { - SET_PROPER_NULL_FLAG((*_c->service_routes), values, LP_SERVICE_ROUTES_IDX); - } - else { + if(_c->service_routes) { + SET_PROPER_NULL_FLAG( + (*_c->service_routes), values, LP_SERVICE_ROUTES_IDX); + } else { VAL_NULL(GET_FIELD_IDX(values, LP_SERVICE_ROUTES_IDX)) = 1; } - if (_c->sock) { + if(_c->sock) { SET_PROPER_NULL_FLAG(_c->sock->sock_str, values, LP_SOCKET_IDX); } else { VAL_NULL(GET_FIELD_IDX(values, LP_SOCKET_IDX)) = 1; @@ -342,12 +350,13 @@ int db_insert_pcontact(struct pcontact* _c) SET_PROPER_NULL_FLAG(service_routes, values, LP_SERVICE_ROUTES_IDX); SET_STR_VALUE(GET_FIELD_IDX(values, LP_SERVICE_ROUTES_IDX), service_routes); - if (use_location_pcscf_table(_c->domain) < 0) { - LM_ERR("Error trying to use table %.*s\n", _c->domain->len, _c->domain->s); + if(use_location_pcscf_table(_c->domain) < 0) { + LM_ERR("Error trying to use table %.*s\n", _c->domain->len, + _c->domain->s); return -1; } - if (ul_dbf.insert(ul_dbh, keys, values, 16) < 0) { + if(ul_dbf.insert(ul_dbh, keys, values, 16) < 0) { LM_ERR("inserting contact in db failed\n"); return -1; } @@ -355,229 +364,244 @@ int db_insert_pcontact(struct pcontact* _c) return 0; } -int db_update_pcontact_security_temp(struct pcontact* _c, security_type _t, security_t* _s) { +int db_update_pcontact_security_temp( + struct pcontact *_c, security_type _t, security_t *_s) +{ db_val_t match_values[2]; - db_key_t match_keys[2] = { &aor_col, &received_port_col }; + db_key_t match_keys[2] = {&aor_col, &received_port_col}; db_op_t op[2]; - db_key_t update_keys[15] = { &t_security_type_col, &t_protocol_col, - &t_mode_col, &t_ck_col, &t_ik_col, &t_ealg_col, &t_ialg_col, &t_port_pc_col, &t_port_ps_col, &t_port_uc_col, - &t_port_us_col, &t_spi_pc_col, &t_spi_ps_col, &t_spi_uc_col, &t_spi_us_col }; + db_key_t update_keys[15] = {&t_security_type_col, &t_protocol_col, + &t_mode_col, &t_ck_col, &t_ik_col, &t_ealg_col, &t_ialg_col, + &t_port_pc_col, &t_port_ps_col, &t_port_uc_col, &t_port_us_col, + &t_spi_pc_col, &t_spi_ps_col, &t_spi_uc_col, &t_spi_us_col}; db_val_t values[15]; - LM_CRIT("updating temp security for pcontact: aor[%.*s], received port %u\n", _c->aor.len, _c->aor.s, _c->received_port); + LM_CRIT("updating temp security for pcontact: aor[%.*s], received port " + "%u\n", + _c->aor.len, _c->aor.s, _c->received_port); VAL_TYPE(match_values) = DB1_STR; VAL_NULL(match_values) = 0; VAL_STR(match_values) = _c->aor; - VAL_TYPE(match_values + 1) = DB1_INT; - VAL_NULL(match_values + 1) = 0; - VAL_INT(match_values + 1) = _c->received_port; + VAL_TYPE(match_values + 1) = DB1_INT; + VAL_NULL(match_values + 1) = 0; + VAL_INT(match_values + 1) = _c->received_port; - op[0]=OP_EQ; - op[1]=OP_EQ; + op[0] = OP_EQ; + op[1] = OP_EQ; - if (use_location_pcscf_table(_c->domain) < 0) { - LM_ERR("Error trying to use table %.*s\n", _c->domain->len, _c->domain->s); + if(use_location_pcscf_table(_c->domain) < 0) { + LM_ERR("Error trying to use table %.*s\n", _c->domain->len, + _c->domain->s); return -1; } VAL_TYPE(values) = DB1_INT; - VAL_TIME(values) = _s?_s->type:0; + VAL_TIME(values) = _s ? _s->type : 0; VAL_NULL(values) = 0; - switch (_t) { - case SECURITY_IPSEC: { - ipsec_t* ipsec = _s?_s->data.ipsec:0; - str s_empty = {0,0}; - int i = 1; - VAL_TYPE(values + i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->prot:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->mod:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->ck:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->ik:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->ealg:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->alg:s_empty; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_pc:0; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_ps:0; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_uc:0; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_us:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_pc:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_ps:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_uc:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_us:0; - - if ((ul_dbf.update(ul_dbh, match_keys, op, match_values, update_keys, values, 2, 15)) != 0) { - LM_ERR("could not update database info\n"); - return -1; - } + switch(_t) { + case SECURITY_IPSEC: { + ipsec_t *ipsec = _s ? _s->data.ipsec : 0; + str s_empty = {0, 0}; + int i = 1; + VAL_TYPE(values + i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->prot : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->mod : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->ck : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->ik : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->ealg : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->alg : s_empty; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_pc : 0; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_ps : 0; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_uc : 0; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_us : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_pc : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_ps : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_uc : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_us : 0; + + if((ul_dbf.update(ul_dbh, match_keys, op, match_values, update_keys, + values, 2, 15)) + != 0) { + LM_ERR("could not update database info\n"); + return -1; + } - if (ul_dbf.affected_rows && ul_dbf.affected_rows(ul_dbh) == 0) { - LM_CRIT("no existing rows for an update... doing insert\n"); - if (db_insert_pcontact(_c) != 0) { - LM_ERR("Failed to insert a pcontact on update\n"); + if(ul_dbf.affected_rows && ul_dbf.affected_rows(ul_dbh) == 0) { + LM_CRIT("no existing rows for an update... doing insert\n"); + if(db_insert_pcontact(_c) != 0) { + LM_ERR("Failed to insert a pcontact on update\n"); + } } + break; } - break; - } - default: - LM_WARN("not yet implemented or unknown security type\n"); - return -1; + default: + LM_WARN("not yet implemented or unknown security type\n"); + return -1; } return 0; } -int db_update_pcontact_security(struct pcontact* _c, security_type _t, security_t* _s) { +int db_update_pcontact_security( + struct pcontact *_c, security_type _t, security_t *_s) +{ db_val_t match_values[2]; - db_key_t match_keys[2] = { &aor_col, &received_port_col }; + db_key_t match_keys[2] = {&aor_col, &received_port_col}; db_op_t op[2]; - db_key_t update_keys[15] = { &security_type_col, &protocol_col, - &mode_col, &ck_col, &ik_col, &ealg_col, &ialg_col, &port_pc_col, &port_ps_col, &port_uc_col, - &port_us_col, &spi_pc_col, &spi_ps_col, &spi_uc_col, &spi_us_col }; + db_key_t update_keys[15] = {&security_type_col, &protocol_col, &mode_col, + &ck_col, &ik_col, &ealg_col, &ialg_col, &port_pc_col, &port_ps_col, + &port_uc_col, &port_us_col, &spi_pc_col, &spi_ps_col, &spi_uc_col, + &spi_us_col}; db_val_t values[15]; - LM_DBG("updating security for pcontact: aor[%.*s], received port %u\n", _c->aor.len, _c->aor.s, _c->received_port); + LM_DBG("updating security for pcontact: aor[%.*s], received port %u\n", + _c->aor.len, _c->aor.s, _c->received_port); VAL_TYPE(match_values) = DB1_STR; VAL_NULL(match_values) = 0; VAL_STR(match_values) = _c->aor; - VAL_TYPE(match_values + 1) = DB1_INT; - VAL_NULL(match_values + 1) = 0; - VAL_INT(match_values + 1) = _c->received_port; - - op[0]=OP_EQ; - op[1]=OP_EQ; + VAL_TYPE(match_values + 1) = DB1_INT; + VAL_NULL(match_values + 1) = 0; + VAL_INT(match_values + 1) = _c->received_port; - if (use_location_pcscf_table(_c->domain) < 0) { - LM_ERR("Error trying to use table %.*s\n", _c->domain->len, _c->domain->s); + op[0] = OP_EQ; + op[1] = OP_EQ; + + if(use_location_pcscf_table(_c->domain) < 0) { + LM_ERR("Error trying to use table %.*s\n", _c->domain->len, + _c->domain->s); return -1; } VAL_TYPE(values) = DB1_INT; - VAL_TIME(values) = _s?_s->type:0; + VAL_TIME(values) = _s ? _s->type : 0; VAL_NULL(values) = 0; - switch (_t) { - case SECURITY_IPSEC: { - ipsec_t* ipsec = _s?_s->data.ipsec:0; - int i = 1; - str s_empty = {0,0}; - VAL_TYPE(values + i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->prot:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->mod:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->ck:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->ik:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->ealg:s_empty; - - VAL_TYPE(values + ++i) = DB1_STR; - VAL_NULL(values + i) = ipsec?0:1; - VAL_STR(values + i) = ipsec?ipsec->alg:s_empty; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_pc:0; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_ps:0; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_uc:0; - - VAL_TYPE(values + ++i) = DB1_INT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_INT(values + i) = ipsec?ipsec->port_us:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_pc:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_ps:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_uc:0; - - VAL_TYPE(values + ++i) = DB1_BIGINT; - VAL_NULL(values + i) = ipsec?0:1; - VAL_BIGINT(values + i) = ipsec?ipsec->spi_us:0; - - if ((ul_dbf.update(ul_dbh, match_keys, op, match_values, update_keys, values, 2, 15)) != 0) { - LM_ERR("could not update database info\n"); - return -1; - } + switch(_t) { + case SECURITY_IPSEC: { + ipsec_t *ipsec = _s ? _s->data.ipsec : 0; + int i = 1; + str s_empty = {0, 0}; + VAL_TYPE(values + i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->prot : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->mod : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->ck : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->ik : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->ealg : s_empty; + + VAL_TYPE(values + ++i) = DB1_STR; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_STR(values + i) = ipsec ? ipsec->alg : s_empty; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_pc : 0; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_ps : 0; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_uc : 0; + + VAL_TYPE(values + ++i) = DB1_INT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_INT(values + i) = ipsec ? ipsec->port_us : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_pc : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_ps : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_uc : 0; + + VAL_TYPE(values + ++i) = DB1_BIGINT; + VAL_NULL(values + i) = ipsec ? 0 : 1; + VAL_BIGINT(values + i) = ipsec ? ipsec->spi_us : 0; + + if((ul_dbf.update(ul_dbh, match_keys, op, match_values, update_keys, + values, 2, 15)) + != 0) { + LM_ERR("could not update database info\n"); + return -1; + } - if (ul_dbf.affected_rows && ul_dbf.affected_rows(ul_dbh) == 0) { - LM_DBG("no existing rows for an update... doing insert\n"); - if (db_insert_pcontact(_c) != 0) { - LM_ERR("Failed to insert a pcontact on update\n"); + if(ul_dbf.affected_rows && ul_dbf.affected_rows(ul_dbh) == 0) { + LM_DBG("no existing rows for an update... doing insert\n"); + if(db_insert_pcontact(_c) != 0) { + LM_ERR("Failed to insert a pcontact on update\n"); + } } + break; } - break; - } - default: - LM_WARN("not yet implemented or unknown security type\n"); - return -1; + default: + LM_WARN("not yet implemented or unknown security type\n"); + return -1; } return 0; @@ -589,23 +613,24 @@ int db_update_pcontact_security(struct pcontact* _c, security_type _t, security_ * returns the length of the string (list) * the string list itself will be available in p */ -int impus_as_string(struct pcontact* _c, t_reusable_buffer* buffer) { - ppublic_t* impu; +int impus_as_string(struct pcontact *_c, t_reusable_buffer *buffer) +{ + ppublic_t *impu; int len = 0; char *p; impu = _c->head; - while (impu) { + while(impu) { len += 2 + impu->public_identity.len; impu = impu->next; } - if (!buffer->buf || buffer->buf_len == 0 || len > buffer->buf_len) { - if (buffer->buf) { + if(!buffer->buf || buffer->buf_len == 0 || len > buffer->buf_len) { + if(buffer->buf) { pkg_free(buffer->buf); } - buffer->buf = (char*) pkg_malloc(len); - if (!buffer->buf) { + buffer->buf = (char *)pkg_malloc(len); + if(!buffer->buf) { LM_CRIT("unable to allocate pkg memory\n"); return 0; } @@ -614,7 +639,7 @@ int impus_as_string(struct pcontact* _c, t_reusable_buffer* buffer) { impu = _c->head; p = buffer->buf; - while (impu) { + while(impu) { *p++ = '<'; memcpy(p, impu->public_identity.s, impu->public_identity.len); p += impu->public_identity.len; @@ -631,20 +656,21 @@ int impus_as_string(struct pcontact* _c, t_reusable_buffer* buffer) { * returns the length of the string (list) * the string list itself will be available in p */ -int service_routes_as_string(struct pcontact* _c, t_reusable_buffer *buffer) { +int service_routes_as_string(struct pcontact *_c, t_reusable_buffer *buffer) +{ int i; int len = 0; char *p; - for (i=0; i<_c->num_service_routes; i++) { + for(i = 0; i < _c->num_service_routes; i++) { len += 2 + _c->service_routes[i].len; } - if (!buffer->buf || buffer->buf_len==0 || len > buffer->buf_len) { - if (buffer->buf) { + if(!buffer->buf || buffer->buf_len == 0 || len > buffer->buf_len) { + if(buffer->buf) { pkg_free(buffer->buf); } - buffer->buf = (char*)pkg_malloc(len); - if (!buffer->buf) { + buffer->buf = (char *)pkg_malloc(len); + if(!buffer->buf) { LM_CRIT("unable to allocate pkg memory\n"); return 0; } @@ -652,11 +678,11 @@ int service_routes_as_string(struct pcontact* _c, t_reusable_buffer *buffer) { } p = buffer->buf; - for (i=0; i<_c->num_service_routes; i++) { + for(i = 0; i < _c->num_service_routes; i++) { *p = '<'; p++; memcpy(p, _c->service_routes[i].s, _c->service_routes[i].len); - p+=_c->service_routes[i].len; + p += _c->service_routes[i].len; *p = '>'; p++; } @@ -666,7 +692,7 @@ int service_routes_as_string(struct pcontact* _c, t_reusable_buffer *buffer) { void free_service_route_buf(void) { - if (service_route_buffer.buf) { + if(service_route_buffer.buf) { pkg_free(service_route_buffer.buf); service_route_buffer.data_len = 0; service_route_buffer.buf_len = 0; @@ -674,12 +700,12 @@ void free_service_route_buf(void) } } -void free_impu_buf(void) { - if (impu_buffer.buf) { +void free_impu_buf(void) +{ + if(impu_buffer.buf) { pkg_free(impu_buffer.buf); impu_buffer.data_len = 0; impu_buffer.buf_len = 0; impu_buffer.buf = 0; } } - diff --git a/src/modules/ims_usrloc_pcscf/usrloc_db.h b/src/modules/ims_usrloc_pcscf/usrloc_db.h index 3deeea813c7..3c50c153b4e 100644 --- a/src/modules/ims_usrloc_pcscf/usrloc_db.h +++ b/src/modules/ims_usrloc_pcscf/usrloc_db.h @@ -8,100 +8,100 @@ #ifndef USRLOC_DB_H_ #define USRLOC_DB_H_ -typedef enum location_pcscf_fields_idx { -// LP_ID_IDX = 0, +typedef enum location_pcscf_fields_idx +{ + // LP_ID_IDX = 0, LP_DOMAIN_IDX = 0, LP_AOR_IDX, LP_RECEIVED_IDX, LP_RECEIVED_PORT_IDX, LP_RECEIVED_PROTO_IDX, LP_PATH_IDX, - LP_RINSTANCE_IDX, + LP_RINSTANCE_IDX, LP_RX_SESSION_ID_IDX, LP_REG_STATE_IDX, LP_EXPIRES_IDX, LP_SERVICE_ROUTES_IDX, LP_SOCKET_IDX, LP_PUBLIC_IPS_IDX, - LP_HOST_IDX, - LP_PORT_IDX, - LP_PROTOCOL_IDX + LP_HOST_IDX, + LP_PORT_IDX, + LP_PROTOCOL_IDX } location_pcscf_fields_idx_t; -#define GET_FIELD_IDX(_val, _idx)\ - (_val + _idx) - -#define SET_PROPER_NULL_FLAG(_str, _vals, _index)\ - do{\ - if( (_str).len == 0)\ - VAL_NULL( (_vals)+(_index) ) = 1;\ - else\ - VAL_NULL( (_vals)+(_index) ) = 0;\ - }while(0); - -#define SET_STR_VALUE(_val, _str)\ - do{\ - VAL_STR((_val)).s = (_str).s;\ - VAL_STR((_val)).len = (_str).len;\ - }while(0); - -#define SET_NULL_FLAG(_vals, _i, _max, _flag)\ - do{\ - for((_i) = 0;(_i)<(_max); (_i)++)\ - VAL_NULL((_vals)+(_i)) = (_flag);\ - }while(0); - - -#define ID_COL "id" -#define DOMAIN_COL "domain" -#define HOST_COL "host" -#define PORT_COL "port" -#define PROTOCOL_COL "protocol" -#define AOR_COL "aor" -#define RECEIVED_COL "received" -#define RECEIVED_PORT_COL "received_port" -#define RECEIVED_PROTO_COL "received_proto" -#define PATH_COL "path" -#define RINSTANCE_COL "rinstance" -#define RX_SESSION_ID_COL "rx_session_id" -#define REG_STATE_COL "reg_state" -#define EXPIRES_COL "expires" -#define SERVICE_ROUTES_COL "service_routes" -#define SOCKET_COL "socket" -#define PUBLIC_IDS_COL "public_ids" -#define SECURITY_TYPE_COL "security_type" - -#define MODE_COL "mode" -#define CK_COL "ck" -#define IK_COL "ik" -#define EALG_COL "ealg" -#define IALG_COL "ialg" -#define PORTPC_COL "port_pc" -#define PORTPS_COL "port_ps" -#define PORTUC_COL "port_uc" -#define PORTUS_COL "port_us" -#define SPIPC_COL "spi_pc" -#define SPIPS_COL "spi_ps" -#define SPIUC_COL "spi_uc" -#define SPIUS_COL "spi_us" -#define T_SECURITY_TYPE_COL "t_security_type" -#define T_PROTOCOL_COL "t_protocol" -#define T_MODE_COL "t_mode" -#define T_CK_COL "t_ck" -#define T_IK_COL "t_ik" -#define T_EALG_COL "t_ealg" -#define T_IALG_COL "t_ialg" -#define T_PORTPC_COL "t_port_pc" -#define T_PORTPS_COL "t_port_ps" -#define T_PORTUC_COL "t_port_uc" -#define T_PORTUS_COL "t_port_us" -#define T_SPIPC_COL "t_spi_pc" -#define T_SPIPS_COL "t_spi_ps" -#define T_SPIUC_COL "t_spi_uc" -#define T_SPIUS_COL "t_spi_us" - -extern db1_con_t* ul_dbh; +#define GET_FIELD_IDX(_val, _idx) (_val + _idx) + +#define SET_PROPER_NULL_FLAG(_str, _vals, _index) \ + do { \ + if((_str).len == 0) \ + VAL_NULL((_vals) + (_index)) = 1; \ + else \ + VAL_NULL((_vals) + (_index)) = 0; \ + } while(0); + +#define SET_STR_VALUE(_val, _str) \ + do { \ + VAL_STR((_val)).s = (_str).s; \ + VAL_STR((_val)).len = (_str).len; \ + } while(0); + +#define SET_NULL_FLAG(_vals, _i, _max, _flag) \ + do { \ + for((_i) = 0; (_i) < (_max); (_i)++) \ + VAL_NULL((_vals) + (_i)) = (_flag); \ + } while(0); + + +#define ID_COL "id" +#define DOMAIN_COL "domain" +#define HOST_COL "host" +#define PORT_COL "port" +#define PROTOCOL_COL "protocol" +#define AOR_COL "aor" +#define RECEIVED_COL "received" +#define RECEIVED_PORT_COL "received_port" +#define RECEIVED_PROTO_COL "received_proto" +#define PATH_COL "path" +#define RINSTANCE_COL "rinstance" +#define RX_SESSION_ID_COL "rx_session_id" +#define REG_STATE_COL "reg_state" +#define EXPIRES_COL "expires" +#define SERVICE_ROUTES_COL "service_routes" +#define SOCKET_COL "socket" +#define PUBLIC_IDS_COL "public_ids" +#define SECURITY_TYPE_COL "security_type" + +#define MODE_COL "mode" +#define CK_COL "ck" +#define IK_COL "ik" +#define EALG_COL "ealg" +#define IALG_COL "ialg" +#define PORTPC_COL "port_pc" +#define PORTPS_COL "port_ps" +#define PORTUC_COL "port_uc" +#define PORTUS_COL "port_us" +#define SPIPC_COL "spi_pc" +#define SPIPS_COL "spi_ps" +#define SPIUC_COL "spi_uc" +#define SPIUS_COL "spi_us" +#define T_SECURITY_TYPE_COL "t_security_type" +#define T_PROTOCOL_COL "t_protocol" +#define T_MODE_COL "t_mode" +#define T_CK_COL "t_ck" +#define T_IK_COL "t_ik" +#define T_EALG_COL "t_ealg" +#define T_IALG_COL "t_ialg" +#define T_PORTPC_COL "t_port_pc" +#define T_PORTPS_COL "t_port_ps" +#define T_PORTUC_COL "t_port_uc" +#define T_PORTUS_COL "t_port_us" +#define T_SPIPC_COL "t_spi_pc" +#define T_SPIPS_COL "t_spi_ps" +#define T_SPIUC_COL "t_spi_uc" +#define T_SPIUS_COL "t_spi_us" + +extern db1_con_t *ul_dbh; extern db_func_t ul_dbf; extern str id_col; @@ -121,8 +121,9 @@ extern str service_routes_col; extern str socket_col; extern str public_ids_col; -typedef struct reusable_buffer{ - char* buf; +typedef struct reusable_buffer +{ + char *buf; int buf_len; int data_len; } t_reusable_buffer; @@ -132,15 +133,17 @@ void destroy_db(); int init_db(const str *db_url, int db_update_period, int fetch_num_rows); int connect_db(const str *db_url); -int impus_as_string(struct pcontact* _c, t_reusable_buffer* buffer); -int service_routes_as_string(struct pcontact* _c, t_reusable_buffer *buffer); +int impus_as_string(struct pcontact *_c, t_reusable_buffer *buffer); +int service_routes_as_string(struct pcontact *_c, t_reusable_buffer *buffer); void free_service_route_buf(void); void free_impu_buf(void); -int db_insert_pcontact(pcontact_t* _c); -int db_delete_pcontact(pcontact_t* _c); -int db_update_pcontact(pcontact_t* _c); -int db_update_pcontact_security_temp(struct pcontact* _c, security_type _t, security_t* _s); -int db_update_pcontact_security(struct pcontact* _c, security_type _t, security_t* _s); -db1_con_t* get_db_handle (); +int db_insert_pcontact(pcontact_t *_c); +int db_delete_pcontact(pcontact_t *_c); +int db_update_pcontact(pcontact_t *_c); +int db_update_pcontact_security_temp( + struct pcontact *_c, security_type _t, security_t *_s); +int db_update_pcontact_security( + struct pcontact *_c, security_type _t, security_t *_s); +db1_con_t *get_db_handle(); #endif /* USRLOC_DB_H_ */