blob: 84e14a0ea5506e5ff24496146f361d0a52fbca1a [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,
38 const char *tmsi)
Harald Welte59b04682009-06-10 05:40:52 +080039{
40 struct gsm_subscriber *subscr;
41
42 /* we might have a record in memory already */
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020043 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +080044 if (strcmp(subscr->tmsi, tmsi) == 0)
45 return subscr_get(subscr);
46 }
47
Harald Welte75350412009-07-23 18:46:00 +020048 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +080049}
50
Harald Welte75350412009-07-23 18:46:00 +020051struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
52 const char *imsi)
Harald Welte59b04682009-06-10 05:40:52 +080053{
54 struct gsm_subscriber *subscr;
55
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020056 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +080057 if (strcmp(subscr->imsi, imsi) == 0)
58 return subscr_get(subscr);
59 }
60
Harald Welte75350412009-07-23 18:46:00 +020061 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte59b04682009-06-10 05:40:52 +080062}
63
Harald Welte75350412009-07-23 18:46:00 +020064struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
65 const char *ext)
Harald Welte59b04682009-06-10 05:40:52 +080066{
67 struct gsm_subscriber *subscr;
68
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020069 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +080070 if (strcmp(subscr->extension, ext) == 0)
71 return subscr_get(subscr);
72 }
73
Harald Welte75350412009-07-23 18:46:00 +020074 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Harald Welte59b04682009-06-10 05:40:52 +080075}
76
Harald Welte68b7df22009-08-08 16:03:15 +020077struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
78 unsigned long long id)
79{
80 struct gsm_subscriber *subscr;
81 char buf[32];
82 sprintf(buf, "%llu", id);
83
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020084 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte68b7df22009-08-08 16:03:15 +020085 if (subscr->id == id)
86 return subscr_get(subscr);
87 }
88
89 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
90}
91
92
Harald Welte59b04682009-06-10 05:40:52 +080093int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
94{
95 /* FIXME: Migrate pending requests from one BSC to another */
96 switch (reason) {
97 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltec2189a62009-07-23 18:56:43 +020098 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +080099 /* Indicate "attached to LAC" */
100 s->lac = bts->location_area_code;
Harald Welteb7799832009-07-29 23:14:15 +0200101 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800102 break;
103 case GSM_SUBSCRIBER_UPDATE_DETACHED:
104 /* Only detach if we are currently in this area */
105 if (bts->location_area_code == s->lac)
106 s->lac = 0;
Harald Welteb7799832009-07-29 23:14:15 +0200107 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800108 break;
109 default:
110 fprintf(stderr, "subscr_update with unknown reason: %d\n",
111 reason);
112 break;
113 };
114 return db_sync_subscriber(s);
115}
116
Harald Welte59b04682009-06-10 05:40:52 +0800117