Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 1 | /* 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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * 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 Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 9 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * 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 Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include <osmocore/talloc.h> |
| 25 | |
| 26 | #include <openbsc/vty.h> |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
| 28 | #include <openbsc/debug.h> |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | ab52c84 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 30 | #include <openbsc/bsc_nat.h> |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 31 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 32 | #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 Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 36 | |
| 37 | |
Holger Hans Peter Freyther | 57da447 | 2010-06-08 16:11:06 +0800 | [diff] [blame] | 38 | enum node_type bsc_vty_go_parent(struct vty *vty) |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 39 | { |
| 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 Welte | 6463232 | 2010-05-31 17:17:29 +0200 | [diff] [blame] | 69 | case OML_NODE: |
Harald Welte | 8d4a472 | 2011-02-12 15:02:29 +0100 | [diff] [blame] | 70 | case OM2K_NODE: |
Harald Welte | 6463232 | 2010-05-31 17:17:29 +0200 | [diff] [blame] | 71 | vty->node = ENABLE_NODE; |
| 72 | talloc_free(vty->index); |
| 73 | vty->index = NULL; |
| 74 | break; |
Holger Hans Peter Freyther | ab52c84 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 75 | case NAT_NODE: |
| 76 | vty->node = CONFIG_NODE; |
| 77 | vty->index = NULL; |
| 78 | break; |
Holger Hans Peter Freyther | 7b4a53d | 2010-06-17 14:42:20 +0800 | [diff] [blame] | 79 | case NAT_BSC_NODE: |
Holger Hans Peter Freyther | ab52c84 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 80 | 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 Freyther | 47b2601 | 2010-09-15 23:28:49 +0800 | [diff] [blame] | 86 | case MSC_NODE: |
| 87 | vty->node = GSMNET_NODE; |
| 88 | break; |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 89 | default: |
| 90 | vty->node = CONFIG_NODE; |
| 91 | } |
| 92 | |
| 93 | return vty->node; |
| 94 | } |
| 95 | |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 96 | /* Down vty node level. */ |
| 97 | gDEFUN(ournode_exit, |
| 98 | ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n") |
| 99 | { |
| 100 | switch (vty->node) { |
| 101 | case GSMNET_NODE: |
| 102 | vty->node = CONFIG_NODE; |
| 103 | vty->index = NULL; |
| 104 | break; |
| 105 | case BTS_NODE: |
| 106 | vty->node = GSMNET_NODE; |
| 107 | { |
| 108 | /* set vty->index correctly ! */ |
| 109 | struct gsm_bts *bts = vty->index; |
| 110 | vty->index = bts->network; |
| 111 | vty->index_sub = NULL; |
| 112 | } |
| 113 | break; |
| 114 | case TRX_NODE: |
| 115 | vty->node = BTS_NODE; |
| 116 | { |
| 117 | /* set vty->index correctly ! */ |
| 118 | struct gsm_bts_trx *trx = vty->index; |
| 119 | vty->index = trx->bts; |
| 120 | vty->index_sub = &trx->bts->description; |
| 121 | } |
| 122 | break; |
| 123 | case TS_NODE: |
| 124 | vty->node = TRX_NODE; |
| 125 | { |
| 126 | /* set vty->index correctly ! */ |
| 127 | struct gsm_bts_trx_ts *ts = vty->index; |
| 128 | vty->index = ts->trx; |
| 129 | vty->index_sub = &ts->trx->description; |
| 130 | } |
| 131 | break; |
Holger Hans Peter Freyther | 7b4a53d | 2010-06-17 14:42:20 +0800 | [diff] [blame] | 132 | case NAT_BSC_NODE: |
Holger Hans Peter Freyther | ab52c84 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 133 | vty->node = NAT_NODE; |
| 134 | { |
| 135 | struct bsc_config *bsc_config = vty->index; |
| 136 | vty->index = bsc_config->nat; |
| 137 | } |
| 138 | break; |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 139 | case MGCP_NODE: |
| 140 | case GBPROXY_NODE: |
| 141 | case SGSN_NODE: |
| 142 | case NS_NODE: |
Harald Welte | 5fa2f44 | 2010-05-17 23:20:56 +0200 | [diff] [blame] | 143 | case BSSGP_NODE: |
Holger Hans Peter Freyther | ab52c84 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 144 | case NAT_NODE: |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 145 | vty->node = CONFIG_NODE; |
| 146 | vty->index = NULL; |
| 147 | break; |
Harald Welte | 6463232 | 2010-05-31 17:17:29 +0200 | [diff] [blame] | 148 | case OML_NODE: |
Harald Welte | 810e323 | 2011-02-12 15:00:35 +0100 | [diff] [blame] | 149 | case OM2K_NODE: |
Harald Welte | 6463232 | 2010-05-31 17:17:29 +0200 | [diff] [blame] | 150 | vty->node = ENABLE_NODE; |
| 151 | talloc_free(vty->index); |
| 152 | vty->index = NULL; |
| 153 | break; |
Holger Hans Peter Freyther | 47b2601 | 2010-09-15 23:28:49 +0800 | [diff] [blame] | 154 | case MSC_NODE: |
| 155 | vty->node = GSMNET_NODE; |
| 156 | break; |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 157 | default: |
| 158 | break; |
| 159 | } |
| 160 | return CMD_SUCCESS; |
| 161 | } |
| 162 | |
| 163 | /* End of configuration. */ |
| 164 | gDEFUN(ournode_end, |
| 165 | ournode_end_cmd, "end", "End current mode and change to enable mode.") |
| 166 | { |
| 167 | switch (vty->node) { |
| 168 | case VIEW_NODE: |
| 169 | case ENABLE_NODE: |
| 170 | /* Nothing to do. */ |
| 171 | break; |
| 172 | case CONFIG_NODE: |
| 173 | case GSMNET_NODE: |
| 174 | case BTS_NODE: |
| 175 | case TRX_NODE: |
| 176 | case TS_NODE: |
| 177 | case MGCP_NODE: |
| 178 | case GBPROXY_NODE: |
| 179 | case SGSN_NODE: |
| 180 | case NS_NODE: |
| 181 | case VTY_NODE: |
Holger Hans Peter Freyther | 5654c5b | 2010-07-31 04:50:21 +0800 | [diff] [blame] | 182 | case NAT_NODE: |
| 183 | case NAT_BSC_NODE: |
Holger Hans Peter Freyther | 47b2601 | 2010-09-15 23:28:49 +0800 | [diff] [blame] | 184 | case MSC_NODE: |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 185 | vty_config_unlock(vty); |
| 186 | vty->node = ENABLE_NODE; |
| 187 | vty->index = NULL; |
| 188 | vty->index_sub = NULL; |
| 189 | break; |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | return CMD_SUCCESS; |
| 194 | } |
| 195 | |
Holger Hans Peter Freyther | 7a2c86b | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 196 | int bsc_vty_is_config_node(struct vty *vty, int node) |
| 197 | { |
| 198 | switch (node) { |
| 199 | /* add items that are not config */ |
| 200 | case OML_NODE: |
Harald Welte | 810e323 | 2011-02-12 15:00:35 +0100 | [diff] [blame] | 201 | case OM2K_NODE: |
Holger Hans Peter Freyther | 7a2c86b | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 202 | case SUBSCR_NODE: |
Holger Hans Peter Freyther | 6ffca13 | 2010-09-04 11:15:27 +0800 | [diff] [blame] | 203 | case CONFIG_NODE: |
Holger Hans Peter Freyther | 7a2c86b | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 204 | return 0; |
| 205 | |
| 206 | default: |
| 207 | return 1; |
| 208 | } |
| 209 | } |
Holger Hans Peter Freyther | 3e9a7f8 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 210 | |
| 211 | /* a talloc string replace routine */ |
| 212 | void bsc_replace_string(void *ctx, char **dst, const char *newstr) |
| 213 | { |
| 214 | if (*dst) |
| 215 | talloc_free(*dst); |
| 216 | *dst = talloc_strdup(ctx, newstr); |
| 217 | } |