Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 1 | /* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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> |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 28 | #include <assert.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 29 | |
| 30 | #include <openbsc/gsm_subscriber.h> |
| 31 | #include <openbsc/debug.h> |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 32 | #include <openbsc/signal.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 33 | #include <openbsc/db.h> |
| 34 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 35 | extern struct llist_head *subscr_bsc_active_subscriber(void); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | |
Harald Welte | 0747c6a | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 37 | char *subscr_name(struct gsm_subscriber *subscr) |
| 38 | { |
| 39 | if (strlen(subscr->name)) |
| 40 | return subscr->name; |
| 41 | |
| 42 | return subscr->imsi; |
| 43 | } |
| 44 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 45 | struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net, |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 46 | u_int32_t tmsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 47 | { |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 48 | char tmsi_string[14]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 49 | struct gsm_subscriber *subscr; |
| 50 | |
| 51 | /* we might have a record in memory already */ |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 52 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 53 | if (tmsi == subscr->tmsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 54 | return subscr_get(subscr); |
| 55 | } |
| 56 | |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 57 | sprintf(tmsi_string, "%u", tmsi); |
| 58 | return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 61 | struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net, |
| 62 | const char *imsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 63 | { |
| 64 | struct gsm_subscriber *subscr; |
| 65 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 66 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 67 | if (strcmp(subscr->imsi, imsi) == 0) |
| 68 | return subscr_get(subscr); |
| 69 | } |
| 70 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 71 | return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 72 | } |
| 73 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 74 | struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net, |
| 75 | const char *ext) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | { |
| 77 | struct gsm_subscriber *subscr; |
| 78 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 79 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 80 | if (strcmp(subscr->extension, ext) == 0) |
| 81 | return subscr_get(subscr); |
| 82 | } |
| 83 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 84 | return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 87 | struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net, |
| 88 | unsigned long long id) |
| 89 | { |
| 90 | struct gsm_subscriber *subscr; |
| 91 | char buf[32]; |
| 92 | sprintf(buf, "%llu", id); |
| 93 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 94 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 95 | if (subscr->id == id) |
| 96 | return subscr_get(subscr); |
| 97 | } |
| 98 | |
| 99 | return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf); |
| 100 | } |
| 101 | |
| 102 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 103 | int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason) |
| 104 | { |
| 105 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 106 | switch (reason) { |
| 107 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Harald Welte | c2189a6 | 2009-07-23 18:56:43 +0200 | [diff] [blame] | 108 | s->net = bts->network; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 109 | /* Indicate "attached to LAC" */ |
| 110 | s->lac = bts->location_area_code; |
Harald Welte | f00c2ac | 2009-12-24 13:48:14 +0100 | [diff] [blame] | 111 | LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n", |
Harald Welte | 0747c6a | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 112 | subscr_name(s), s->lac); |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 113 | dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 114 | break; |
| 115 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
| 116 | /* Only detach if we are currently in this area */ |
| 117 | if (bts->location_area_code == s->lac) |
Holger Hans Peter Freyther | 6c6ab86 | 2009-10-01 04:07:15 +0200 | [diff] [blame] | 118 | s->lac = GSM_LAC_RESERVED_DETACHED; |
Harald Welte | 0747c6a | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 119 | LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s)); |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 120 | dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 121 | break; |
| 122 | default: |
| 123 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 124 | reason); |
| 125 | break; |
| 126 | }; |
| 127 | return db_sync_subscriber(s); |
| 128 | } |
| 129 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 130 | |