1 /*
2  * $Id$
3  *
4  * Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
5  * Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
6  *
7  * The initial version of this code was written by Dragos Vingarzan
8  * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
9  * Fruanhofer Institute. It was and still is maintained in a separate
10  * branch of the original SER. We are therefore migrating it to
11  * Kamailio/SR and look forward to maintaining it from here on out.
12  * 2011/2012 Smile Communications, Pty. Ltd.
13  * ported/maintained/improved by
14  * Jason Penton (jason(dot)penton(at)smilecoms.com and
15  * Richard Good (richard(dot)good(at)smilecoms.com) as part of an
16  * effort to add full IMS support to Kamailio/SR using a new and
17  * improved architecture
18  *
19  * NB: Alot of this code was originally part of OpenIMSCore,
20  * FhG Fokus.
21  * Copyright (C) 2004-2006 FhG Fokus
22  * Thanks for great work! This is an effort to
23  * break apart the various CSCF functions into logically separate
24  * components. We hope this will drive wider use. We also feel
25  * that in this way the architecture is more complete and thereby easier
26  * to manage in the Kamailio/SR environment
27  *
28  * This file is part of Kamailio, a free SIP server.
29  *
30  * Kamailio is free software; you can redistribute it and/or modify
31  * it under the terms of the GNU General Public License as published by
32  * the Free Software Foundation; either version 2 of the License, or
33  * (at your option) any later version
34  *
35  * Kamailio is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  * GNU General Public License for more details.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
43  *
44  */
45 
46 #include "usrloc.h"
47 #include "dlist.h"
48 #include "pcontact.h"
49 #include "udomain.h"
50 #include "../../core/sr_module.h"
51 #include "ims_usrloc_pcscf_mod.h"
52 #include "../../core/parser/parse_uri.h"
53 
54 extern unsigned int init_flag;
55 
bind_usrloc(usrloc_api_t * api)56 int bind_usrloc(usrloc_api_t* api) {
57 	if (!api) {
58 		LM_ERR("invalid parameter value\n");
59 		return -1;
60 	}
61 	if (init_flag == 0) {
62 		LM_ERR("configuration error - trying to bind to usrloc module"
63 				" before being initialized\n");
64 		return -1;
65 	}
66 
67 	api->register_udomain = register_udomain;
68 	api->get_udomain = get_udomain;
69 	api->lock_udomain = lock_udomain;
70 	api->unlock_udomain = unlock_udomain;
71 	api->insert_pcontact = insert_pcontact;
72 	api->delete_pcontact = delete_pcontact;
73     api->unreg_pending_contacts_cb = unreg_pending_contacts_cb;
74 	api->get_pcontact = get_pcontact;
75 	api->assert_identity = assert_identity;
76 	api->update_pcontact = update_pcontact;
77 	api->update_rx_regsession = update_rx_regsession;
78 	api->get_all_ucontacts = get_all_ucontacts;
79 	api->update_security = update_security;
80 	api->update_temp_security = update_temp_security;
81 	api->register_ulcb = register_ulcb;
82 	api->get_number_of_contacts = get_number_of_contacts;
83 
84 	return 0;
85 }
86 
87 #define ALIAS        "alias="
88 #define ALIAS_LEN (sizeof(ALIAS) - 1)
89 
get_alias_host_from_contact(str * contact_uri_params,str * alias_host)90 int get_alias_host_from_contact(str *contact_uri_params, str *alias_host) {
91     char *rest, *sep;
92     unsigned int rest_len;
93 
94     rest = contact_uri_params->s;
95     rest_len = contact_uri_params->len;
96     if (rest_len == 0) {
97         LM_DBG("no params\n");
98         return -1;
99     }
100 
101     /*Get full alias parameter*/
102     while (rest_len >= ALIAS_LEN) {
103         if (strncmp(rest, ALIAS, ALIAS_LEN) == 0) break;
104         sep = memchr(rest, 59 /* ; */, rest_len);
105         if (sep == NULL) {
106             LM_DBG("no alias param\n");
107             return -1;
108         } else {
109             rest_len = rest_len - (sep - rest + 1);
110             rest = sep + 1;
111         }
112     }
113 
114     if (rest_len < ALIAS_LEN) {
115         LM_DBG("no alias param\n");
116         return -1;
117     }
118 
119     alias_host->s = rest + ALIAS_LEN;
120     alias_host->len = rest_len - ALIAS_LEN;
121 
122     /*Get host from alias*/
123     rest = memchr(alias_host->s, 126 /* ~ */, alias_host->len);
124     if (rest == NULL) {
125         LM_ERR("no '~' in alias param value\n");
126         return -1;
127     }
128     alias_host->len = rest - alias_host->s;
129     LM_DBG("Alias host to return [%.*s]\n", alias_host->len, alias_host->s);
130     return 0;
131 }
132 
133 
134 /* return the slot id for inserting contacts in the hash */
get_hash_slot(udomain_t * _d,str * via_host,unsigned short via_port,unsigned short via_proto)135 unsigned int get_hash_slot(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto) {
136     unsigned int sl;
137 
138     sl = get_aor_hash(_d, via_host, via_port, via_proto);
139     sl = sl & (_d->size - 1) ;
140     LM_DBG("Returning hash slot: [%d]\n", sl);
141 
142     return sl;
143 }
144 
get_aor_hash(udomain_t * _d,str * via_host,unsigned short via_port,unsigned short via_proto)145 unsigned int get_aor_hash(udomain_t* _d, str* via_host, unsigned short via_port, unsigned short via_proto) {
146     unsigned int aorhash;
147 
148     aorhash = core_hash(via_host, 0, 0);
149     LM_DBG("Returning hash: [%u]\n", aorhash);
150 
151     return aorhash;
152 }
153