blob: c81b522d4b8d57ba2c46ab92602fd57343e4ac13 [file] [log] [blame]
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +02001/* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */
Harald Welte59b04682009-06-10 05:40:52 +08002
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 Freyther8bdf7622009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte59b04682009-06-10 05:40:52 +080029
30#include <openbsc/gsm_subscriber.h>
31#include <openbsc/debug.h>
Harald Welteb7799832009-07-29 23:14:15 +020032#include <openbsc/signal.h>
Harald Welte59b04682009-06-10 05:40:52 +080033#include <openbsc/db.h>
34
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020035extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte59b04682009-06-10 05:40:52 +080036
Harald Welte75350412009-07-23 18:46:00 +020037struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +020038 u_int32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +080039{
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +020040 char tmsi_string[14];
Harald Welte59b04682009-06-10 05:40:52 +080041 struct gsm_subscriber *subscr;
42
43 /* we might have a record in memory already */
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020044 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +020045 if (tmsi == subscr->tmsi)
Harald Welte59b04682009-06-10 05:40:52 +080046 return subscr_get(subscr);
47 }
48
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +020049 sprintf(tmsi_string, "%u", tmsi);
50 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte59b04682009-06-10 05:40:52 +080051}
52
Harald Welte75350412009-07-23 18:46:00 +020053struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
54 const char *imsi)
Harald Welte59b04682009-06-10 05:40:52 +080055{
56 struct gsm_subscriber *subscr;
57
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020058 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +080059 if (strcmp(subscr->imsi, imsi) == 0)
60 return subscr_get(subscr);
61 }
62
Harald Welte75350412009-07-23 18:46:00 +020063 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte59b04682009-06-10 05:40:52 +080064}
65
Harald Welte75350412009-07-23 18:46:00 +020066struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
67 const char *ext)
Harald Welte59b04682009-06-10 05:40:52 +080068{
69 struct gsm_subscriber *subscr;
70
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020071 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +080072 if (strcmp(subscr->extension, ext) == 0)
73 return subscr_get(subscr);
74 }
75
Harald Welte75350412009-07-23 18:46:00 +020076 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Harald Welte59b04682009-06-10 05:40:52 +080077}
78
Harald Welte68b7df22009-08-08 16:03:15 +020079struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
80 unsigned long long id)
81{
82 struct gsm_subscriber *subscr;
83 char buf[32];
84 sprintf(buf, "%llu", id);
85
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020086 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte68b7df22009-08-08 16:03:15 +020087 if (subscr->id == id)
88 return subscr_get(subscr);
89 }
90
91 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
92}
93
94
Harald Welte59b04682009-06-10 05:40:52 +080095int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
96{
97 /* FIXME: Migrate pending requests from one BSC to another */
98 switch (reason) {
99 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltec2189a62009-07-23 18:56:43 +0200100 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800101 /* Indicate "attached to LAC" */
102 s->lac = bts->location_area_code;
Harald Welteb7799832009-07-29 23:14:15 +0200103 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800104 break;
105 case GSM_SUBSCRIBER_UPDATE_DETACHED:
106 /* Only detach if we are currently in this area */
107 if (bts->location_area_code == s->lac)
Holger Hans Peter Freyther6c6ab862009-10-01 04:07:15 +0200108 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welteb7799832009-07-29 23:14:15 +0200109 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800110 break;
111 default:
112 fprintf(stderr, "subscr_update with unknown reason: %d\n",
113 reason);
114 break;
115 };
116 return db_sync_subscriber(s);
117}
118
Harald Welte59b04682009-06-10 05:40:52 +0800119