Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame^] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Authors: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * and Thomas Klausner <tk@giga.or.at> * |
| 5 | * * |
| 6 | * Copyright (c) 2011, 2014, WIDE Project and NICT * |
| 7 | * All rights reserved. * |
| 8 | * * |
| 9 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 10 | * permitted provided that the following conditions are met: * |
| 11 | * * |
| 12 | * * Redistributions of source code must retain the above * |
| 13 | * copyright notice, this list of conditions and the * |
| 14 | * following disclaimer. * |
| 15 | * * |
| 16 | * * Redistributions in binary form must reproduce the above * |
| 17 | * copyright notice, this list of conditions and the * |
| 18 | * following disclaimer in the documentation and/or other * |
| 19 | * materials provided with the distribution. * |
| 20 | * * |
| 21 | * * Neither the name of the WIDE Project or NICT nor the * |
| 22 | * names of its contributors may be used to endorse or * |
| 23 | * promote products derived from this software without * |
| 24 | * specific prior written permission of WIDE Project and * |
| 25 | * NICT. * |
| 26 | * * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 28 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 29 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 30 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 31 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 33 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 35 | *********************************************************************************************************/ |
| 36 | |
| 37 | #include "rt_redir.h" |
| 38 | #include "uthash.h" |
| 39 | |
| 40 | /* The array with all entries ordered by their data */ |
| 41 | struct redir_line redirects_usages[H_U_MAX + 1]; |
| 42 | |
| 43 | /* for symmetry reasons, hash tables for all types exist, but only ALL_SESSION and ALL_USER will be used */ |
| 44 | struct redir_entry *redirect_hash_table[H_U_MAX+1]; |
| 45 | |
| 46 | /* Initialize the array */ |
| 47 | int redir_entry_init() |
| 48 | { |
| 49 | int i; |
| 50 | |
| 51 | TRACE_ENTRY(""); |
| 52 | |
| 53 | /* redirects_usages */ |
| 54 | memset(&redirects_usages, 0, sizeof(redirects_usages)); |
| 55 | |
| 56 | for (i = 0; i <= H_U_MAX; i++) { |
| 57 | /* only one of the two will be used for each type, but initialize both to be on the safe side */ |
| 58 | |
| 59 | /* initialize list */ |
| 60 | CHECK_POSIX( pthread_rwlock_init( &redirects_usages[i].lock, NULL) ); |
| 61 | fd_list_init( &redirects_usages[i].sentinel, &redirects_usages[i] ); |
| 62 | |
| 63 | /* initialize hash table */ |
| 64 | redirect_hash_table[i] = NULL; |
| 65 | } |
| 66 | |
| 67 | /* initialize the scores */ |
| 68 | redirects_usages[ DONT_CACHE ].score = FD_SCORE_REDIR_ONCE; |
| 69 | redirects_usages[ ALL_SESSION ].score = FD_SCORE_REDIR_SESSION; |
| 70 | redirects_usages[ ALL_REALM ].score = FD_SCORE_REDIR_REALM; |
| 71 | redirects_usages[ REALM_AND_APPLICATION ].score = FD_SCORE_REDIR_REALM_APP; |
| 72 | redirects_usages[ ALL_APPLICATION ].score = FD_SCORE_REDIR_APP; |
| 73 | redirects_usages[ ALL_HOST ].score = FD_SCORE_REDIR_HOST; |
| 74 | redirects_usages[ ALL_USER ].score = FD_SCORE_REDIR_USER; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int redir_entry_fini() |
| 80 | { |
| 81 | int i; |
| 82 | struct redir_entry *current_entry, *tmp; |
| 83 | |
| 84 | /* Empty all entries */ |
| 85 | CHECK_POSIX_DO( pthread_mutex_lock(&redir_exp_peer_lock), ); |
| 86 | for (i = 0; i <= H_U_MAX; i++) { |
| 87 | CHECK_POSIX_DO( pthread_rwlock_wrlock( &redirects_usages[i].lock), ); |
| 88 | switch(i) { |
| 89 | case ALL_SESSION: |
| 90 | case ALL_USER: |
| 91 | HASH_ITER(hh, redirect_hash_table[i], current_entry, tmp) { |
| 92 | HASH_DEL(redirect_hash_table[i], current_entry); |
| 93 | CHECK_FCT_DO( redir_entry_destroy(current_entry), ); |
| 94 | } |
| 95 | break; |
| 96 | default: |
| 97 | while (!FD_IS_LIST_EMPTY(&redirects_usages[i].sentinel)) { |
| 98 | struct redir_entry * e = redirects_usages[i].sentinel.next->o; |
| 99 | fd_list_unlink(&e->redir_list); |
| 100 | CHECK_FCT_DO( redir_entry_destroy(e), ); |
| 101 | } |
| 102 | } |
| 103 | CHECK_POSIX_DO( pthread_rwlock_unlock( &redirects_usages[i].lock), ); |
| 104 | CHECK_POSIX_DO( pthread_rwlock_destroy( &redirects_usages[i].lock), ); |
| 105 | } |
| 106 | CHECK_POSIX_DO( pthread_mutex_unlock(&redir_exp_peer_lock), ); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* Create a new redir_entry and add the correct data */ |
| 112 | int redir_entry_new(struct redir_entry ** e, struct fd_list * targets, uint32_t rhu, struct msg * qry, DiamId_t nh, size_t nhlen, os0_t oh, size_t ohlen) |
| 113 | { |
| 114 | struct redir_entry * entry = NULL; |
| 115 | os0_t s; |
| 116 | size_t l; |
| 117 | |
| 118 | TRACE_ENTRY("%p %p %d %p %p %zd %p %zd", e, targets, rhu, qry, nh, nhlen, oh, ohlen) |
| 119 | ASSERT(e && targets && (rhu <= H_U_MAX) && qry && nh && nhlen && oh && ohlen); |
| 120 | |
| 121 | CHECK_MALLOC( entry = malloc(sizeof(struct redir_entry)) ); |
| 122 | memset(entry, 0, sizeof(struct redir_entry)); |
| 123 | |
| 124 | entry->eyec = REDIR_ENTRY_EYEC; |
| 125 | |
| 126 | CHECK_MALLOC( entry->from.s = os0dup(nh, nhlen) ); |
| 127 | entry->from.l = nhlen; |
| 128 | |
| 129 | fd_list_init(&entry->target_peers_list, entry); |
| 130 | fd_list_move_end(&entry->target_peers_list, targets); |
| 131 | |
| 132 | fd_list_init(&entry->exp_list, entry); |
| 133 | |
| 134 | entry->type = rhu; |
| 135 | /* list entry for putting into redirects_usage; also doubles as pointer into that list so it can be removed easily */ |
| 136 | fd_list_init(&entry->redir_list, entry); |
| 137 | /* finally initialize the data */ |
| 138 | switch (rhu) { |
| 139 | case DONT_CACHE: |
| 140 | entry->data.message.msg = qry; |
| 141 | break; |
| 142 | |
| 143 | case ALL_SESSION: |
| 144 | { |
| 145 | /* There is a good chance that the session is already cached in the message, so retrieve it */ |
| 146 | struct session * sess; |
| 147 | CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, qry, &sess, NULL) ); |
| 148 | if (!sess) { |
| 149 | TRACE_DEBUG(INFO, "Received a Redirect indication with usage ALL_SESSION but no Session-Id AVP in the message, defaulting to DONT_CACHE"); |
| 150 | entry->type = DONT_CACHE; |
| 151 | entry->data.message.msg = qry; |
| 152 | break; |
| 153 | } |
| 154 | CHECK_FCT( fd_sess_getsid(sess, &s, &l) ); |
| 155 | CHECK_MALLOC( entry->data.session.s = os0dup(s, l) ); |
| 156 | entry->data.session.l = l; |
| 157 | } |
| 158 | break; |
| 159 | |
| 160 | case ALL_REALM: |
| 161 | { |
| 162 | /* Search the Destination-Realm of the message */ |
| 163 | struct avp * dr; |
| 164 | struct avp_hdr * ahdr; |
| 165 | CHECK_FCT( fd_msg_search_avp(qry, redir_dict_dr, &dr) ); |
| 166 | if (!dr) { |
| 167 | TRACE_DEBUG(INFO, "Received a Redirect indication with usage ALL_REALM but no Destination-Realm AVP in the message, defaulting to DONT_CACHE"); |
| 168 | entry->type = DONT_CACHE; |
| 169 | entry->data.message.msg = qry; |
| 170 | break; |
| 171 | } |
| 172 | CHECK_FCT( fd_msg_avp_hdr( dr, &ahdr ) ); |
| 173 | CHECK_MALLOC( entry->data.realm.s = os0dup(ahdr->avp_value->os.data, ahdr->avp_value->os.len) ); |
| 174 | entry->data.realm.l = ahdr->avp_value->os.len; |
| 175 | } |
| 176 | break; |
| 177 | |
| 178 | case REALM_AND_APPLICATION: |
| 179 | { |
| 180 | /* Search the Destination-Realm of the message */ |
| 181 | struct avp * dr; |
| 182 | struct avp_hdr * ahdr; |
| 183 | CHECK_FCT( fd_msg_search_avp(qry, redir_dict_dr, &dr) ); |
| 184 | if (!dr) { |
| 185 | TRACE_DEBUG(INFO, "Received a Redirect indication with usage REALM_AND_APPLICATION but no Destination-Realm AVP in the message, defaulting to DONT_CACHE"); |
| 186 | entry->type = DONT_CACHE; |
| 187 | entry->data.message.msg = qry; |
| 188 | break; |
| 189 | } |
| 190 | CHECK_FCT( fd_msg_avp_hdr( dr, &ahdr ) ); |
| 191 | CHECK_MALLOC( entry->data.realm_app.s = os0dup(ahdr->avp_value->os.data, ahdr->avp_value->os.len) ); |
| 192 | entry->data.realm_app.l = ahdr->avp_value->os.len; |
| 193 | } |
| 194 | /* and then the application */ |
| 195 | { |
| 196 | struct msg_hdr * hdr; |
| 197 | CHECK_FCT( fd_msg_hdr(qry, &hdr) ); |
| 198 | entry->data.realm_app.a = hdr->msg_appl; |
| 199 | } |
| 200 | break; |
| 201 | |
| 202 | case ALL_APPLICATION: |
| 203 | { |
| 204 | struct msg_hdr * hdr; |
| 205 | CHECK_FCT( fd_msg_hdr(qry, &hdr) ); |
| 206 | entry->data.app.a = hdr->msg_appl; |
| 207 | } |
| 208 | break; |
| 209 | |
| 210 | case ALL_HOST: |
| 211 | CHECK_MALLOC( entry->data.host.s = os0dup(oh, ohlen) ); |
| 212 | entry->data.host.l = ohlen; |
| 213 | break; |
| 214 | |
| 215 | case ALL_USER: |
| 216 | { |
| 217 | /* Search the User-Name of the message */ |
| 218 | struct avp * un; |
| 219 | struct avp_hdr * ahdr; |
| 220 | CHECK_FCT( fd_msg_search_avp(qry, redir_dict_un, &un) ); |
| 221 | if (!un) { |
| 222 | TRACE_DEBUG(INFO, "Received a Redirect indication with usage ALL_USER but no User-Name AVP in the message, defaulting to DONT_CACHE"); |
| 223 | entry->type = DONT_CACHE; |
| 224 | entry->data.message.msg = qry; |
| 225 | break; |
| 226 | } |
| 227 | CHECK_FCT( fd_msg_avp_hdr( un, &ahdr ) ); |
| 228 | CHECK_MALLOC( entry->data.user.s = os0dup(ahdr->avp_value->os.data, ahdr->avp_value->os.len) ); |
| 229 | entry->data.user.l = ahdr->avp_value->os.len; |
| 230 | } |
| 231 | break; |
| 232 | |
| 233 | default: |
| 234 | ASSERT(0); |
| 235 | return EINVAL; |
| 236 | } |
| 237 | |
| 238 | /* We're done */ |
| 239 | *e = entry; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* Compares two pointers (DONT_CACHE) */ |
| 245 | static int compare_entries_ptr(union matchdata * d1, union matchdata * d2) { |
| 246 | unsigned long v1 = (unsigned long) d1->message.msg; |
| 247 | unsigned long v2 = (unsigned long) d2->message.msg; |
| 248 | if (v1 > v2) |
| 249 | return 1; |
| 250 | if (v1 < v2) |
| 251 | return -1; |
| 252 | return 0; |
| 253 | } |
| 254 | /* Compare two applications (REALM_AND_APPLICATION and ALL_APPLICATION) */ |
| 255 | static int compare_entries_appl(union matchdata * d1, union matchdata * d2) { |
| 256 | if (d1->app.a > d2->app.a) |
| 257 | return 1; |
| 258 | if (d1->app.a < d2->app.a) |
| 259 | return -1; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /* Compare two strings (ALL_SESSION, ALL_REALM, ALL_HOST, ALL_USER) */ |
| 264 | static int compare_entries_ostr(union matchdata * d1, union matchdata * d2) { |
| 265 | return fd_os_cmp(d1->session.s, d1->session.l, d2->session.s, d2->session.l); |
| 266 | } |
| 267 | |
| 268 | /* The array of callbacks */ |
| 269 | int (*redir_entry_cmp_key[H_U_MAX +1])(union matchdata * , union matchdata * ) = { |
| 270 | compare_entries_ptr, /* DONT_CACHE */ |
| 271 | compare_entries_ostr, /* ALL_SESSION */ |
| 272 | compare_entries_ostr, /* ALL_REALM */ |
| 273 | compare_entries_appl, /* REALM_AND_APPLICATION */ |
| 274 | compare_entries_appl, /* ALL_APPLICATION */ |
| 275 | compare_entries_ostr, /* ALL_HOST */ |
| 276 | compare_entries_ostr /* ALL_USER */ |
| 277 | }; |
| 278 | |
| 279 | /* Link the newly created entry into the correct redirects_usages list. The mutex must be held */ |
| 280 | int redir_entry_insert(struct redir_entry * e) |
| 281 | { |
| 282 | struct fd_list * li; |
| 283 | struct redir_entry * r = NULL; |
| 284 | |
| 285 | TRACE_ENTRY("%p", e); |
| 286 | CHECK_PARAMS(e && (e->eyec == REDIR_ENTRY_EYEC)); |
| 287 | |
| 288 | /* Write-Lock the line */ |
| 289 | CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); |
| 290 | |
| 291 | switch (e->type) { |
| 292 | case ALL_SESSION: |
| 293 | HASH_FIND(hh, redirect_hash_table[e->type], e->data.session.s, e->data.session.l, r); |
| 294 | if (r) { |
| 295 | /* previously existing entry, delete it from hash and free it */ |
| 296 | HASH_DELETE(hh, redirect_hash_table[e->type], r); |
| 297 | CHECK_FCT_DO( redir_entry_destroy(r), ); |
| 298 | } |
| 299 | HASH_ADD_KEYPTR(hh, redirect_hash_table[e->type], e->data.session.s, e->data.session.l, e); |
| 300 | break; |
| 301 | case ALL_USER: |
| 302 | HASH_FIND(hh, redirect_hash_table[e->type], e->data.user.s, e->data.user.l, r); |
| 303 | if (r) { |
| 304 | /* previously existing entry, delete it from hash and free it */ |
| 305 | HASH_DELETE(hh, redirect_hash_table[e->type], r); |
| 306 | CHECK_FCT_DO( redir_entry_destroy(r), ); |
| 307 | } |
| 308 | HASH_ADD_KEYPTR(hh, redirect_hash_table[e->type], e->data.user.s, e->data.user.l, e); |
| 309 | break; |
| 310 | default: |
| 311 | for (li = redirects_usages[e->type].sentinel.next; li != &redirects_usages[e->type].sentinel; li = li->next) { |
| 312 | struct redir_entry * n = li->o; |
| 313 | int cmp = redir_entry_cmp_key[e->type](&e->data, &n->data); |
| 314 | if (cmp <= 0) |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | fd_list_insert_before(li, &e->redir_list); |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | /* unLock the line */ |
| 323 | CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* Destroy -- the exp_peer_lock must be held when this function is called */ |
| 329 | int redir_entry_destroy(struct redir_entry * e) |
| 330 | { |
| 331 | struct redir_entry *match; |
| 332 | TRACE_ENTRY("%p", e); |
| 333 | CHECK_PARAMS(e && (e->eyec == REDIR_ENTRY_EYEC)); |
| 334 | |
| 335 | switch (e->type) { |
| 336 | case ALL_SESSION: |
| 337 | /* If the entry is in the hash table, lock the rwlock also */ |
| 338 | HASH_FIND(hh, redirect_hash_table[e->type], e->data.session.s, e->data.session.l, match); |
| 339 | if (match) { |
| 340 | /* TODO: check if e == match? */ |
| 341 | CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); |
| 342 | HASH_DELETE(hh, redirect_hash_table[e->type], match); |
| 343 | CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); |
| 344 | } |
| 345 | break; |
| 346 | case ALL_USER: |
| 347 | /* If the entry is in the hash table, lock the rwlock also */ |
| 348 | HASH_FIND(hh, redirect_hash_table[e->type], e->data.user.s, e->data.user.l, match); |
| 349 | if (match) { |
| 350 | /* TODO: check if e == match? */ |
| 351 | CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); |
| 352 | HASH_DELETE(hh, redirect_hash_table[e->type], match); |
| 353 | CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); |
| 354 | } |
| 355 | break; |
| 356 | default: |
| 357 | /* If the entry is linked, lock the rwlock also */ |
| 358 | if (!FD_IS_LIST_EMPTY(&e->redir_list)) { |
| 359 | CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); |
| 360 | fd_list_unlink(&e->redir_list); |
| 361 | CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); |
| 362 | } |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | /* Now unlink from other list */ |
| 367 | fd_list_unlink(&e->exp_list); |
| 368 | |
| 369 | /* Empty the targets list */ |
| 370 | while (!FD_IS_LIST_EMPTY(&e->target_peers_list)) { |
| 371 | struct redir_host * h = (struct redir_host *)e->target_peers_list.next->o; |
| 372 | |
| 373 | fd_list_unlink(&h->chain); |
| 374 | free(h->id); |
| 375 | free(h); |
| 376 | } |
| 377 | |
| 378 | /* Now we can destroy the data safely */ |
| 379 | switch (e->type) { |
| 380 | case DONT_CACHE: |
| 381 | /* nothing special */ |
| 382 | break; |
| 383 | case ALL_SESSION: |
| 384 | free(e->data.session.s); |
| 385 | break; |
| 386 | case ALL_REALM: |
| 387 | free(e->data.realm.s); |
| 388 | break; |
| 389 | case REALM_AND_APPLICATION: |
| 390 | free(e->data.realm_app.s); |
| 391 | break; |
| 392 | case ALL_APPLICATION: |
| 393 | break; |
| 394 | case ALL_HOST: |
| 395 | free(e->data.host.s); |
| 396 | break; |
| 397 | case ALL_USER: |
| 398 | free(e->data.user.s); |
| 399 | break; |
| 400 | default: |
| 401 | TRACE_DEBUG(INFO, "Invalid redirect type was saved"); |
| 402 | ASSERT(0); |
| 403 | return EINVAL; |
| 404 | } |
| 405 | |
| 406 | free(e->from.s); |
| 407 | |
| 408 | free(e); |
| 409 | return 0; |
| 410 | } |