Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <unistd.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include <openbsc/gsm_subscriber.h> |
| 30 | #include <openbsc/debug.h> |
| 31 | #include <openbsc/db.h> |
| 32 | |
| 33 | |
| 34 | LLIST_HEAD(active_subscribers); |
| 35 | |
| 36 | struct gsm_subscriber *subscr_alloc(void) |
| 37 | { |
| 38 | struct gsm_subscriber *s; |
| 39 | |
| 40 | s = malloc(sizeof(struct gsm_subscriber)); |
| 41 | if (!s) |
| 42 | return NULL; |
| 43 | |
| 44 | memset(s, 0, sizeof(*s)); |
| 45 | llist_add_tail(&s->entry, &active_subscribers); |
| 46 | s->use_count = 1; |
| 47 | |
| 48 | return s; |
| 49 | } |
| 50 | |
| 51 | static void subscr_free(struct gsm_subscriber *subscr) |
| 52 | { |
| 53 | llist_del(&subscr->entry); |
| 54 | free(subscr); |
| 55 | } |
| 56 | |
| 57 | struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi) |
| 58 | { |
| 59 | struct gsm_subscriber *subscr; |
| 60 | |
| 61 | /* we might have a record in memory already */ |
| 62 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 63 | if (strcmp(subscr->tmsi, tmsi) == 0) |
| 64 | return subscr_get(subscr); |
| 65 | } |
| 66 | |
| 67 | return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi); |
| 68 | } |
| 69 | |
| 70 | struct gsm_subscriber *subscr_get_by_imsi(const char *imsi) |
| 71 | { |
| 72 | struct gsm_subscriber *subscr; |
| 73 | |
| 74 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 75 | if (strcmp(subscr->imsi, imsi) == 0) |
| 76 | return subscr_get(subscr); |
| 77 | } |
| 78 | |
| 79 | return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi); |
| 80 | } |
| 81 | |
| 82 | struct gsm_subscriber *subscr_get_by_extension(const char *ext) |
| 83 | { |
| 84 | struct gsm_subscriber *subscr; |
| 85 | |
| 86 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 87 | if (strcmp(subscr->extension, ext) == 0) |
| 88 | return subscr_get(subscr); |
| 89 | } |
| 90 | |
| 91 | return db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, ext); |
| 92 | } |
| 93 | |
| 94 | int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason) |
| 95 | { |
| 96 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 97 | switch (reason) { |
| 98 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
| 99 | /* Indicate "attached to LAC" */ |
| 100 | s->lac = bts->location_area_code; |
| 101 | break; |
| 102 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
| 103 | /* Only detach if we are currently in this area */ |
| 104 | if (bts->location_area_code == s->lac) |
| 105 | s->lac = 0; |
| 106 | |
| 107 | break; |
| 108 | default: |
| 109 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 110 | reason); |
| 111 | break; |
| 112 | }; |
| 113 | return db_sync_subscriber(s); |
| 114 | } |
| 115 | |
| 116 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 117 | { |
| 118 | subscr->use_count++; |
| 119 | DEBUGP(DCC, "subscr %s usage increases usage to: %d\n", |
| 120 | subscr->extension, subscr->use_count); |
| 121 | return subscr; |
| 122 | } |
| 123 | |
| 124 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 125 | { |
| 126 | subscr->use_count--; |
| 127 | DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n", |
| 128 | subscr->extension, subscr->use_count); |
| 129 | if (subscr->use_count <= 0) |
| 130 | subscr_free(subscr); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | void subscr_put_channel(struct gsm_lchan *lchan) |
| 135 | { |
| 136 | /* |
| 137 | * FIXME: Continue with other requests now... by checking |
| 138 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 139 | * of the lchan after having asked the next requestee to handle |
| 140 | * the channel. |
| 141 | */ |
| 142 | put_lchan(lchan); |
| 143 | } |