Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 1 | /* The concept of a subscriber as seen by the BSC */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 108d58a | 2010-06-30 09:22:31 +0800 | [diff] [blame] | 4 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | 5180844 | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <assert.h> |
| 30 | |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 31 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 32 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 34 | |
| 35 | LLIST_HEAD(active_subscribers); |
| 36 | void *tall_subscr_ctx; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 37 | |
| 38 | /* for the gsm_subscriber.c */ |
| 39 | struct llist_head *subscr_bsc_active_subscriber(void) |
| 40 | { |
| 41 | return &active_subscribers; |
| 42 | } |
| 43 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 44 | |
Holger Hans Peter Freyther | 3ea77a5 | 2010-06-30 12:56:41 +0800 | [diff] [blame] | 45 | char *subscr_name(struct gsm_subscriber *subscr) |
| 46 | { |
| 47 | if (strlen(subscr->name)) |
| 48 | return subscr->name; |
| 49 | |
| 50 | return subscr->imsi; |
| 51 | } |
| 52 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 53 | struct gsm_subscriber *subscr_alloc(void) |
| 54 | { |
| 55 | struct gsm_subscriber *s; |
| 56 | |
Holger Hans Peter Freyther | e453348 | 2009-10-29 02:29:45 +0100 | [diff] [blame] | 57 | s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 58 | if (!s) |
| 59 | return NULL; |
| 60 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 61 | llist_add_tail(&s->entry, &active_subscribers); |
| 62 | s->use_count = 1; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 63 | s->tmsi = GSM_RESERVED_TMSI; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 64 | |
| 65 | INIT_LLIST_HEAD(&s->requests); |
| 66 | |
| 67 | return s; |
| 68 | } |
| 69 | |
| 70 | static void subscr_free(struct gsm_subscriber *subscr) |
| 71 | { |
| 72 | llist_del(&subscr->entry); |
| 73 | talloc_free(subscr); |
| 74 | } |
| 75 | |
| 76 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 77 | { |
| 78 | subscr->use_count++; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 79 | DEBUGP(DREF, "subscr %s usage increases usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 80 | subscr->extension, subscr->use_count); |
| 81 | return subscr; |
| 82 | } |
| 83 | |
| 84 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 85 | { |
| 86 | subscr->use_count--; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 87 | DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 88 | subscr->extension, subscr->use_count); |
| 89 | if (subscr->use_count <= 0) |
| 90 | subscr_free(subscr); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Holger Hans Peter Freyther | dcec9aa | 2010-11-15 09:15:01 +0100 | [diff] [blame] | 94 | struct gsm_subscriber *subscr_get_or_create(struct gsm_network *net, |
| 95 | const char *imsi) |
| 96 | { |
| 97 | struct gsm_subscriber *subscr; |
| 98 | |
| 99 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
| 100 | if (strcmp(subscr->imsi, imsi) == 0 && subscr->net == net) |
| 101 | return subscr_get(subscr); |
| 102 | } |
| 103 | |
| 104 | subscr = subscr_alloc(); |
| 105 | if (!subscr) |
| 106 | return NULL; |
| 107 | |
| 108 | strcpy(subscr->imsi, imsi); |
| 109 | subscr->net = net; |
| 110 | return subscr; |
| 111 | } |
Holger Hans Peter Freyther | b0065af | 2010-11-15 13:32:33 +0100 | [diff] [blame] | 112 | |
| 113 | struct gsm_subscriber *subscr_active_by_tmsi(struct gsm_network *net, uint32_t tmsi) |
| 114 | { |
| 115 | struct gsm_subscriber *subscr; |
| 116 | |
| 117 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
| 118 | if (subscr->tmsi == tmsi && subscr->net == net) |
| 119 | return subscr_get(subscr); |
| 120 | } |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | struct gsm_subscriber *subscr_active_by_imsi(struct gsm_network *net, const char *imsi) |
| 126 | { |
| 127 | struct gsm_subscriber *subscr; |
| 128 | |
| 129 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
| 130 | if (strcmp(subscr->imsi, imsi) == 0 && subscr->net == net) |
| 131 | return subscr_get(subscr); |
| 132 | } |
| 133 | |
| 134 | return NULL; |
| 135 | } |