blob: 390979b9309ce588804e6d4c690a717da2a33b68 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */
2
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>
Harald Welte52b1f982008-12-23 20:25:15 +000028
Harald Welte8470bf22008-12-25 23:28:35 +000029#include <openbsc/gsm_subscriber.h>
Harald Welte75a983f2008-12-27 21:34:06 +000030#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Holger Freyther12aa50d2009-01-01 18:02:05 +000032
33LLIST_HEAD(active_subscribers);
34
Harald Welte75a983f2008-12-27 21:34:06 +000035struct gsm_subscriber *subscr_alloc(void)
Harald Welte52b1f982008-12-23 20:25:15 +000036{
Harald Welte75a983f2008-12-27 21:34:06 +000037 struct gsm_subscriber *s;
38
39 s = malloc(sizeof(struct gsm_subscriber));
40 if (!s)
41 return NULL;
42
43 memset(s, 0, sizeof(*s));
Holger Freyther12aa50d2009-01-01 18:02:05 +000044 llist_add_tail(&s->entry, &active_subscribers);
45 s->use_count = 1;
Harald Welte75a983f2008-12-27 21:34:06 +000046
47 return s;
Harald Welte52b1f982008-12-23 20:25:15 +000048}
Harald Welte75a983f2008-12-27 21:34:06 +000049
Holger Freyther12aa50d2009-01-01 18:02:05 +000050static void subscr_free(struct gsm_subscriber *subscr)
Harald Welte52b1f982008-12-23 20:25:15 +000051{
Holger Freyther12aa50d2009-01-01 18:02:05 +000052 llist_del(&subscr->entry);
Harald Welte75a983f2008-12-27 21:34:06 +000053 free(subscr);
54}
55
Holger Freythera471a412009-01-04 03:47:05 +000056struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +000057{
Holger Freyther12aa50d2009-01-01 18:02:05 +000058 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +000059
Holger Freyther12aa50d2009-01-01 18:02:05 +000060 /* we might have a record in memory already */
61 llist_for_each_entry(subscr, &active_subscribers, entry) {
62 if (strcmp(subscr->tmsi, tmsi) == 0)
63 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +000064 }
65
Holger Freyther12aa50d2009-01-01 18:02:05 +000066 return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte75a983f2008-12-27 21:34:06 +000067}
68
Holger Freythera471a412009-01-04 03:47:05 +000069struct gsm_subscriber *subscr_get_by_imsi(const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +000070{
Holger Freyther12aa50d2009-01-01 18:02:05 +000071 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +000072
Holger Freyther12aa50d2009-01-01 18:02:05 +000073 llist_for_each_entry(subscr, &active_subscribers, entry) {
74 if (strcmp(subscr->imsi, imsi) == 0)
75 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +000076 }
77
Holger Freyther12aa50d2009-01-01 18:02:05 +000078 return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +000079}
80
81int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts)
82{
Holger Freyther12aa50d2009-01-01 18:02:05 +000083 return db_sync_subscriber(s);
84}
85
86struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
87{
88 subscr->use_count++;
89 return subscr;
90}
91
92struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
93{
94 if (--subscr->use_count <= 0)
95 subscr_free(subscr);
96 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +000097}