blob: 22f08e32013d70c1898eb547c483a852badacc68 [file] [log] [blame]
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +02001/* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */
Harald Welte52b1f982008-12-23 20:25:15 +00002
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * 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 Welte75a983f2008-12-27 21:34:06 +000024#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Harald Welte8470bf22008-12-25 23:28:35 +000030#include <openbsc/gsm_subscriber.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000031#include <openbsc/debug.h>
Harald Welteea5cf302009-07-29 23:14:15 +020032#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020035extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte75a983f2008-12-27 21:34:06 +000036
Harald Welte9176bd42009-07-23 18:46:00 +020037struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020038 u_int32_t tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +000039{
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020040 char tmsi_string[14];
Holger Freyther12aa50d2009-01-01 18:02:05 +000041 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +000042
Holger Freyther12aa50d2009-01-01 18:02:05 +000043 /* we might have a record in memory already */
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020044 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020045 if (tmsi == subscr->tmsi)
Holger Freyther12aa50d2009-01-01 18:02:05 +000046 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +000047 }
48
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020049 sprintf(tmsi_string, "%u", tmsi);
50 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte75a983f2008-12-27 21:34:06 +000051}
52
Harald Welte9176bd42009-07-23 18:46:00 +020053struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
54 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +000055{
Holger Freyther12aa50d2009-01-01 18:02:05 +000056 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +000057
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020058 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther12aa50d2009-01-01 18:02:05 +000059 if (strcmp(subscr->imsi, imsi) == 0)
60 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +000061 }
62
Harald Welte9176bd42009-07-23 18:46:00 +020063 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +000064}
65
Harald Welte9176bd42009-07-23 18:46:00 +020066struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
67 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +000068{
69 struct gsm_subscriber *subscr;
70
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020071 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther9c564b82009-02-09 23:39:20 +000072 if (strcmp(subscr->extension, ext) == 0)
73 return subscr_get(subscr);
74 }
75
Harald Welte9176bd42009-07-23 18:46:00 +020076 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +000077}
78
Harald Welte76042182009-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 Freyther857e5e62009-06-12 07:08:13 +020086 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte76042182009-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
Holger Freyther4a49e772009-04-12 05:37:29 +000095int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +000096{
Holger Freyther4a49e772009-04-12 05:37:29 +000097 /* FIXME: Migrate pending requests from one BSC to another */
98 switch (reason) {
99 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200100 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000101 /* Indicate "attached to LAC" */
102 s->lac = bts->location_area_code;
Harald Welte77563da2009-12-24 13:48:14 +0100103 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte2e6d4682009-12-24 14:50:24 +0100104 subscr_name(s), s->lac);
Harald Welteea5cf302009-07-29 23:14:15 +0200105 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000106 break;
107 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000108 /* Only detach if we are currently in this area */
109 if (bts->location_area_code == s->lac)
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200110 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte2e6d4682009-12-24 14:50:24 +0100111 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Harald Welteea5cf302009-07-29 23:14:15 +0200112 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000113 break;
114 default:
115 fprintf(stderr, "subscr_update with unknown reason: %d\n",
116 reason);
117 break;
118 };
Holger Freyther12aa50d2009-01-01 18:02:05 +0000119 return db_sync_subscriber(s);
120}
121
Holger Freyther12aa50d2009-01-01 18:02:05 +0000122