blob: 0c2dd7c52528c63943984f64769f469a77e2c80a [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
Holger Freyther9c564b82009-02-09 23:39:20 +000081struct gsm_subscriber *subscr_get_by_extension(const char *ext)
82{
83 struct gsm_subscriber *subscr;
84
85 llist_for_each_entry(subscr, &active_subscribers, entry) {
86 if (strcmp(subscr->extension, ext) == 0)
87 return subscr_get(subscr);
88 }
89
90 return db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, ext);
91}
92
Holger Freyther4a49e772009-04-12 05:37:29 +000093int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +000094{
Holger Freyther4a49e772009-04-12 05:37:29 +000095 /* FIXME: Migrate pending requests from one BSC to another */
96 switch (reason) {
97 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
98 s->current_bts = bts;
99 break;
100 case GSM_SUBSCRIBER_UPDATE_DETACHED:
101 /* Only detach if we are currently attached to this bts */
102 if (bts == s->current_bts)
103 s->current_bts = NULL;
104 break;
105 default:
106 fprintf(stderr, "subscr_update with unknown reason: %d\n",
107 reason);
108 break;
109 };
Holger Freyther12aa50d2009-01-01 18:02:05 +0000110 return db_sync_subscriber(s);
111}
112
113struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
114{
115 subscr->use_count++;
116 return subscr;
117}
118
119struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
120{
121 if (--subscr->use_count <= 0)
122 subscr_free(subscr);
123 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000124}
Holger Freythera1f92f02009-04-12 05:37:52 +0000125
126void subscr_put_channel(struct gsm_lchan *lchan)
127{
128 /*
129 * FIXME: Continue with other requests now... by checking
130 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
131 * of the lchan after having asked the next requestee to handle
132 * the channel.
133 */
134 put_lchan(lchan);
135}