Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1 | /* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 5 | * |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 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 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 28 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 31 | #include <openbsc/db.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 32 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 33 | |
| 34 | LLIST_HEAD(active_subscribers); |
| 35 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 36 | struct gsm_subscriber *subscr_alloc(void) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 37 | { |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 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)); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 45 | llist_add_tail(&s->entry, &active_subscribers); |
| 46 | s->use_count = 1; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 47 | |
| 48 | return s; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 49 | } |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 50 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 51 | static void subscr_free(struct gsm_subscriber *subscr) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 52 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 53 | llist_del(&subscr->entry); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 54 | free(subscr); |
| 55 | } |
| 56 | |
Holger Freyther | a471a41 | 2009-01-04 03:47:05 +0000 | [diff] [blame] | 57 | struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 58 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 59 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 60 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 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); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 67 | return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Holger Freyther | a471a41 | 2009-01-04 03:47:05 +0000 | [diff] [blame] | 70 | struct gsm_subscriber *subscr_get_by_imsi(const char *imsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 71 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 72 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 73 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 74 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 75 | if (strcmp(subscr->imsi, imsi) == 0) |
| 76 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 79 | return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 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 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 94 | int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 95 | { |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 96 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 97 | switch (reason) { |
| 98 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
| 99 | s->current_bts = bts; |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 100 | /* Indicate "attached to LAC" */ |
| 101 | s->lac = bts->location_area_code; |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 102 | break; |
| 103 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
| 104 | /* Only detach if we are currently attached to this bts */ |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 105 | if (bts == s->current_bts) { |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 106 | s->current_bts = NULL; |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 107 | /* Indicate "detached" */ |
| 108 | s->lac = 0; |
| 109 | } |
| 110 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 111 | break; |
| 112 | default: |
| 113 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 114 | reason); |
| 115 | break; |
| 116 | }; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 117 | return db_sync_subscriber(s); |
| 118 | } |
| 119 | |
| 120 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 121 | { |
| 122 | subscr->use_count++; |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 123 | DEBUGP(DCC, "subscr %s usage increases usage to: %d\n", |
| 124 | subscr->extension, subscr->use_count); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 125 | return subscr; |
| 126 | } |
| 127 | |
| 128 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 129 | { |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 130 | subscr->use_count--; |
| 131 | DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n", |
| 132 | subscr->extension, subscr->use_count); |
| 133 | if (subscr->use_count <= 0) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 134 | subscr_free(subscr); |
| 135 | return NULL; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 136 | } |
Holger Freyther | a1f92f0 | 2009-04-12 05:37:52 +0000 | [diff] [blame] | 137 | |
| 138 | void subscr_put_channel(struct gsm_lchan *lchan) |
| 139 | { |
| 140 | /* |
| 141 | * FIXME: Continue with other requests now... by checking |
| 142 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 143 | * of the lchan after having asked the next requestee to handle |
| 144 | * the channel. |
| 145 | */ |
| 146 | put_lchan(lchan); |
| 147 | } |