blob: 0fd4f43c1d9a4abf73c400c488fdd5995b3a1324 [file] [log] [blame]
Harald Welte5bc61dc2010-05-16 22:02:16 +02001/* OpenBSC VTY common helpers */
2/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2009-2010 by Holger Hans Peter Freyther
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Harald Welte5bc61dc2010-05-16 22:02:16 +02009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Harald Welte5bc61dc2010-05-16 22:02:16 +020015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte5bc61dc2010-05-16 22:02:16 +020018 *
19 */
20
21#include <stdlib.h>
22#include <string.h>
23
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010024#include <osmocom/core/talloc.h>
Harald Welte5bc61dc2010-05-16 22:02:16 +020025
26#include <openbsc/vty.h>
Harald Welte5bc61dc2010-05-16 22:02:16 +020027#include <openbsc/gsm_data.h>
28#include <openbsc/debug.h>
Harald Welte4b037e42010-05-19 19:45:32 +020029#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +080030#include <openbsc/bsc_nat.h>
Harald Welte5bc61dc2010-05-16 22:02:16 +020031
Harald Welte4b037e42010-05-19 19:45:32 +020032#include <osmocom/vty/telnet_interface.h>
33#include <osmocom/vty/command.h>
34#include <osmocom/vty/buffer.h>
35#include <osmocom/vty/vty.h>
Harald Welte5bc61dc2010-05-16 22:02:16 +020036
37
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +080038enum node_type bsc_vty_go_parent(struct vty *vty)
Harald Welte4b037e42010-05-19 19:45:32 +020039{
40 switch (vty->node) {
41 case GSMNET_NODE:
42 vty->node = CONFIG_NODE;
43 vty->index = NULL;
44 break;
45 case BTS_NODE:
46 vty->node = GSMNET_NODE;
47 {
48 /* set vty->index correctly ! */
49 struct gsm_bts *bts = vty->index;
50 vty->index = bts->network;
51 }
52 break;
53 case TRX_NODE:
54 vty->node = BTS_NODE;
55 {
56 /* set vty->index correctly ! */
57 struct gsm_bts_trx *trx = vty->index;
58 vty->index = trx->bts;
59 }
60 break;
61 case TS_NODE:
62 vty->node = TRX_NODE;
63 {
64 /* set vty->index correctly ! */
65 struct gsm_bts_trx_ts *ts = vty->index;
66 vty->index = ts->trx;
67 }
68 break;
Harald Welte64632322010-05-31 17:17:29 +020069 case OML_NODE:
Harald Welte8d4a4722011-02-12 15:02:29 +010070 case OM2K_NODE:
Harald Welte64632322010-05-31 17:17:29 +020071 vty->node = ENABLE_NODE;
72 talloc_free(vty->index);
73 vty->index = NULL;
74 break;
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +080075 case NAT_NODE:
76 vty->node = CONFIG_NODE;
77 vty->index = NULL;
78 break;
Holger Hans Peter Freyther7b4a53d2010-06-17 14:42:20 +080079 case NAT_BSC_NODE:
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +080080 vty->node = NAT_NODE;
81 {
82 struct bsc_config *bsc_config = vty->index;
83 vty->index = bsc_config->nat;
84 }
85 break;
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +020086 case PGROUP_NODE:
87 vty->node = NAT_NODE;
88 break;
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +010089 case TRUNK_NODE:
90 vty->node = MGCP_NODE;
91 break;
Harald Welteab386e62011-09-01 18:18:43 +020092 case MSC_NODE:
93 case MNCC_INT_NODE:
Harald Welte4b037e42010-05-19 19:45:32 +020094 default:
95 vty->node = CONFIG_NODE;
96 }
97
98 return vty->node;
99}
100
Harald Welte5bc61dc2010-05-16 22:02:16 +0200101/* Down vty node level. */
102gDEFUN(ournode_exit,
103 ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
104{
105 switch (vty->node) {
106 case GSMNET_NODE:
107 vty->node = CONFIG_NODE;
108 vty->index = NULL;
109 break;
110 case BTS_NODE:
111 vty->node = GSMNET_NODE;
112 {
113 /* set vty->index correctly ! */
114 struct gsm_bts *bts = vty->index;
115 vty->index = bts->network;
116 vty->index_sub = NULL;
117 }
118 break;
119 case TRX_NODE:
120 vty->node = BTS_NODE;
121 {
122 /* set vty->index correctly ! */
123 struct gsm_bts_trx *trx = vty->index;
124 vty->index = trx->bts;
125 vty->index_sub = &trx->bts->description;
126 }
127 break;
128 case TS_NODE:
129 vty->node = TRX_NODE;
130 {
131 /* set vty->index correctly ! */
132 struct gsm_bts_trx_ts *ts = vty->index;
133 vty->index = ts->trx;
134 vty->index_sub = &ts->trx->description;
135 }
136 break;
Holger Hans Peter Freyther7b4a53d2010-06-17 14:42:20 +0800137 case NAT_BSC_NODE:
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +0800138 vty->node = NAT_NODE;
139 {
140 struct bsc_config *bsc_config = vty->index;
141 vty->index = bsc_config->nat;
142 }
143 break;
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200144 case PGROUP_NODE:
145 vty->node = NAT_NODE;
146 break;
Harald Welte5bc61dc2010-05-16 22:02:16 +0200147 case MGCP_NODE:
148 case GBPROXY_NODE:
149 case SGSN_NODE:
Holger Hans Peter Freytherab52c842010-06-15 20:11:16 +0800150 case NAT_NODE:
Holger Hans Peter Freyther8b902d72011-06-04 20:22:49 +0200151 case BSC_NODE:
Harald Welte5bc61dc2010-05-16 22:02:16 +0200152 vty->node = CONFIG_NODE;
153 vty->index = NULL;
154 break;
Harald Welte64632322010-05-31 17:17:29 +0200155 case OML_NODE:
Harald Welte810e3232011-02-12 15:00:35 +0100156 case OM2K_NODE:
Harald Welte64632322010-05-31 17:17:29 +0200157 vty->node = ENABLE_NODE;
158 talloc_free(vty->index);
159 vty->index = NULL;
160 break;
Holger Hans Peter Freyther47b26012010-09-15 23:28:49 +0800161 case MSC_NODE:
Harald Welteab386e62011-09-01 18:18:43 +0200162 case MNCC_INT_NODE:
Holger Hans Peter Freyther06f4fc72011-03-29 17:46:14 +0200163 vty->node = CONFIG_NODE;
Holger Hans Peter Freyther47b26012010-09-15 23:28:49 +0800164 break;
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100165 case TRUNK_NODE:
166 vty->node = MGCP_NODE;
167 vty->index = NULL;
168 break;
Harald Welte5bc61dc2010-05-16 22:02:16 +0200169 default:
170 break;
171 }
172 return CMD_SUCCESS;
173}
174
175/* End of configuration. */
176gDEFUN(ournode_end,
177 ournode_end_cmd, "end", "End current mode and change to enable mode.")
178{
179 switch (vty->node) {
180 case VIEW_NODE:
181 case ENABLE_NODE:
182 /* Nothing to do. */
183 break;
184 case CONFIG_NODE:
185 case GSMNET_NODE:
186 case BTS_NODE:
187 case TRX_NODE:
188 case TS_NODE:
189 case MGCP_NODE:
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100190 case TRUNK_NODE:
Harald Welte5bc61dc2010-05-16 22:02:16 +0200191 case GBPROXY_NODE:
192 case SGSN_NODE:
Harald Welte5bc61dc2010-05-16 22:02:16 +0200193 case VTY_NODE:
Holger Hans Peter Freyther5654c5b2010-07-31 04:50:21 +0800194 case NAT_NODE:
195 case NAT_BSC_NODE:
Holger Hans Peter Freyther6860c442011-05-02 19:16:13 +0200196 case PGROUP_NODE:
Holger Hans Peter Freyther47b26012010-09-15 23:28:49 +0800197 case MSC_NODE:
Harald Welteab386e62011-09-01 18:18:43 +0200198 case MNCC_INT_NODE:
Holger Hans Peter Freyther8b902d72011-06-04 20:22:49 +0200199 case BSC_NODE:
Harald Welte5bc61dc2010-05-16 22:02:16 +0200200 vty_config_unlock(vty);
201 vty->node = ENABLE_NODE;
202 vty->index = NULL;
203 vty->index_sub = NULL;
204 break;
205 default:
206 break;
207 }
208 return CMD_SUCCESS;
209}
210
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800211int bsc_vty_is_config_node(struct vty *vty, int node)
212{
213 switch (node) {
214 /* add items that are not config */
215 case OML_NODE:
Harald Welte810e3232011-02-12 15:00:35 +0100216 case OM2K_NODE:
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800217 case SUBSCR_NODE:
Holger Hans Peter Freyther6ffca132010-09-04 11:15:27 +0800218 case CONFIG_NODE:
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800219 return 0;
220
221 default:
222 return 1;
223 }
224}
Holger Hans Peter Freyther3e9a7f82010-10-12 23:21:54 +0200225
226/* a talloc string replace routine */
227void bsc_replace_string(void *ctx, char **dst, const char *newstr)
228{
229 if (*dst)
230 talloc_free(*dst);
231 *dst = talloc_strdup(ctx, newstr);
232}