Harald Welte | 682ee5f | 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 | 0e3e88e | 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 | 682ee5f | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 0e3e88e | 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 | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 24 | #include <osmocom/core/talloc.h> |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 25 | |
| 26 | #include <openbsc/vty.h> |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
| 28 | #include <openbsc/debug.h> |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | 256a040 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 30 | #include <openbsc/bsc_nat.h> |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 31 | |
Harald Welte | bd9591f | 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 | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 36 | |
| 37 | |
Holger Hans Peter Freyther | 3538c77 | 2010-06-08 16:11:06 +0800 | [diff] [blame] | 38 | enum node_type bsc_vty_go_parent(struct vty *vty) |
Harald Welte | bd9591f | 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 | c29ae4b | 2010-05-31 17:17:29 +0200 | [diff] [blame] | 69 | case OML_NODE: |
Harald Welte | fe59e07 | 2011-02-12 15:02:29 +0100 | [diff] [blame] | 70 | case OM2K_NODE: |
Harald Welte | c29ae4b | 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 | 256a040 | 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 | a34bb3d | 2010-06-17 14:42:20 +0800 | [diff] [blame] | 79 | case NAT_BSC_NODE: |
Holger Hans Peter Freyther | 256a040 | 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 | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 86 | case PGROUP_NODE: |
| 87 | vty->node = NAT_NODE; |
| 88 | break; |
Holger Hans Peter Freyther | 06bfe79 | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 89 | case TRUNK_NODE: |
| 90 | vty->node = MGCP_NODE; |
| 91 | break; |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 92 | case SMPP_ESME_NODE: |
| 93 | vty->node = SMPP_NODE; |
| 94 | vty->index = NULL; |
| 95 | break; |
| 96 | case SMPP_NODE: |
Harald Welte | 1ed89d4 | 2011-09-01 18:18:43 +0200 | [diff] [blame] | 97 | case MSC_NODE: |
| 98 | case MNCC_INT_NODE: |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 99 | default: |
| 100 | vty->node = CONFIG_NODE; |
| 101 | } |
| 102 | |
| 103 | return vty->node; |
| 104 | } |
| 105 | |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 106 | /* Down vty node level. */ |
| 107 | gDEFUN(ournode_exit, |
| 108 | ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n") |
| 109 | { |
| 110 | switch (vty->node) { |
| 111 | case GSMNET_NODE: |
| 112 | vty->node = CONFIG_NODE; |
| 113 | vty->index = NULL; |
| 114 | break; |
| 115 | case BTS_NODE: |
| 116 | vty->node = GSMNET_NODE; |
| 117 | { |
| 118 | /* set vty->index correctly ! */ |
| 119 | struct gsm_bts *bts = vty->index; |
| 120 | vty->index = bts->network; |
| 121 | vty->index_sub = NULL; |
| 122 | } |
| 123 | break; |
| 124 | case TRX_NODE: |
| 125 | vty->node = BTS_NODE; |
| 126 | { |
| 127 | /* set vty->index correctly ! */ |
| 128 | struct gsm_bts_trx *trx = vty->index; |
| 129 | vty->index = trx->bts; |
| 130 | vty->index_sub = &trx->bts->description; |
| 131 | } |
| 132 | break; |
| 133 | case TS_NODE: |
| 134 | vty->node = TRX_NODE; |
| 135 | { |
| 136 | /* set vty->index correctly ! */ |
| 137 | struct gsm_bts_trx_ts *ts = vty->index; |
| 138 | vty->index = ts->trx; |
| 139 | vty->index_sub = &ts->trx->description; |
| 140 | } |
| 141 | break; |
Holger Hans Peter Freyther | a34bb3d | 2010-06-17 14:42:20 +0800 | [diff] [blame] | 142 | case NAT_BSC_NODE: |
Holger Hans Peter Freyther | 256a040 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 143 | vty->node = NAT_NODE; |
| 144 | { |
| 145 | struct bsc_config *bsc_config = vty->index; |
| 146 | vty->index = bsc_config->nat; |
| 147 | } |
| 148 | break; |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 149 | case PGROUP_NODE: |
| 150 | vty->node = NAT_NODE; |
| 151 | break; |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 152 | case SMPP_ESME_NODE: |
| 153 | vty->node = SMPP_NODE; |
| 154 | vty->index = NULL; |
| 155 | break; |
| 156 | case SMPP_NODE: |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 157 | case MGCP_NODE: |
| 158 | case GBPROXY_NODE: |
| 159 | case SGSN_NODE: |
Holger Hans Peter Freyther | 256a040 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 160 | case NAT_NODE: |
Holger Hans Peter Freyther | 387a85c | 2011-06-04 20:22:49 +0200 | [diff] [blame] | 161 | case BSC_NODE: |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 162 | vty->node = CONFIG_NODE; |
| 163 | vty->index = NULL; |
| 164 | break; |
Harald Welte | c29ae4b | 2010-05-31 17:17:29 +0200 | [diff] [blame] | 165 | case OML_NODE: |
Harald Welte | 422a781 | 2011-02-12 15:00:35 +0100 | [diff] [blame] | 166 | case OM2K_NODE: |
Harald Welte | c29ae4b | 2010-05-31 17:17:29 +0200 | [diff] [blame] | 167 | vty->node = ENABLE_NODE; |
| 168 | talloc_free(vty->index); |
| 169 | vty->index = NULL; |
| 170 | break; |
Holger Hans Peter Freyther | 8c4dca2 | 2010-09-15 23:28:49 +0800 | [diff] [blame] | 171 | case MSC_NODE: |
Harald Welte | 1ed89d4 | 2011-09-01 18:18:43 +0200 | [diff] [blame] | 172 | case MNCC_INT_NODE: |
Holger Hans Peter Freyther | 8cbef57 | 2011-03-29 17:46:14 +0200 | [diff] [blame] | 173 | vty->node = CONFIG_NODE; |
Holger Hans Peter Freyther | 8c4dca2 | 2010-09-15 23:28:49 +0800 | [diff] [blame] | 174 | break; |
Holger Hans Peter Freyther | 06bfe79 | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 175 | case TRUNK_NODE: |
| 176 | vty->node = MGCP_NODE; |
| 177 | vty->index = NULL; |
| 178 | break; |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 179 | default: |
| 180 | break; |
| 181 | } |
| 182 | return CMD_SUCCESS; |
| 183 | } |
| 184 | |
| 185 | /* End of configuration. */ |
| 186 | gDEFUN(ournode_end, |
| 187 | ournode_end_cmd, "end", "End current mode and change to enable mode.") |
| 188 | { |
| 189 | switch (vty->node) { |
| 190 | case VIEW_NODE: |
| 191 | case ENABLE_NODE: |
| 192 | /* Nothing to do. */ |
| 193 | break; |
| 194 | case CONFIG_NODE: |
| 195 | case GSMNET_NODE: |
| 196 | case BTS_NODE: |
| 197 | case TRX_NODE: |
| 198 | case TS_NODE: |
| 199 | case MGCP_NODE: |
Holger Hans Peter Freyther | 06bfe79 | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 200 | case TRUNK_NODE: |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 201 | case GBPROXY_NODE: |
| 202 | case SGSN_NODE: |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 203 | case VTY_NODE: |
Holger Hans Peter Freyther | a02eaf7 | 2010-07-31 04:50:21 +0800 | [diff] [blame] | 204 | case NAT_NODE: |
| 205 | case NAT_BSC_NODE: |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 206 | case PGROUP_NODE: |
Holger Hans Peter Freyther | 8c4dca2 | 2010-09-15 23:28:49 +0800 | [diff] [blame] | 207 | case MSC_NODE: |
Harald Welte | 1ed89d4 | 2011-09-01 18:18:43 +0200 | [diff] [blame] | 208 | case MNCC_INT_NODE: |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 209 | case SMPP_NODE: |
| 210 | case SMPP_ESME_NODE: |
Holger Hans Peter Freyther | 387a85c | 2011-06-04 20:22:49 +0200 | [diff] [blame] | 211 | case BSC_NODE: |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 212 | vty_config_unlock(vty); |
| 213 | vty->node = ENABLE_NODE; |
| 214 | vty->index = NULL; |
| 215 | vty->index_sub = NULL; |
| 216 | break; |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | return CMD_SUCCESS; |
| 221 | } |
| 222 | |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 223 | int bsc_vty_is_config_node(struct vty *vty, int node) |
| 224 | { |
| 225 | switch (node) { |
| 226 | /* add items that are not config */ |
| 227 | case OML_NODE: |
Harald Welte | 422a781 | 2011-02-12 15:00:35 +0100 | [diff] [blame] | 228 | case OM2K_NODE: |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 229 | case SUBSCR_NODE: |
Holger Hans Peter Freyther | 7653d35 | 2010-09-04 11:15:27 +0800 | [diff] [blame] | 230 | case CONFIG_NODE: |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 231 | return 0; |
| 232 | |
| 233 | default: |
| 234 | return 1; |
| 235 | } |
| 236 | } |
Holger Hans Peter Freyther | 7c831f6 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 237 | |
| 238 | /* a talloc string replace routine */ |
| 239 | void bsc_replace_string(void *ctx, char **dst, const char *newstr) |
| 240 | { |
| 241 | if (*dst) |
| 242 | talloc_free(*dst); |
| 243 | *dst = talloc_strdup(ctx, newstr); |
| 244 | } |