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 | 0745c4f | 2016-11-11 19:41:59 +0100 | [diff] [blame] | 31 | #include <openbsc/abis_om2000.h> |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 32 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 33 | #include <osmocom/vty/telnet_interface.h> |
| 34 | #include <osmocom/vty/command.h> |
| 35 | #include <osmocom/vty/buffer.h> |
| 36 | #include <osmocom/vty/vty.h> |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 37 | |
| 38 | |
Holger Hans Peter Freyther | 4ac8cd1 | 2015-09-24 15:38:38 +0200 | [diff] [blame] | 39 | int bsc_vty_go_parent(struct vty *vty) |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 40 | { |
| 41 | switch (vty->node) { |
| 42 | case GSMNET_NODE: |
| 43 | vty->node = CONFIG_NODE; |
| 44 | vty->index = NULL; |
| 45 | break; |
| 46 | case BTS_NODE: |
| 47 | vty->node = GSMNET_NODE; |
| 48 | { |
| 49 | /* set vty->index correctly ! */ |
| 50 | struct gsm_bts *bts = vty->index; |
| 51 | vty->index = bts->network; |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 52 | vty->index_sub = NULL; |
| 53 | } |
| 54 | break; |
| 55 | case TRX_NODE: |
| 56 | vty->node = BTS_NODE; |
| 57 | { |
| 58 | /* set vty->index correctly ! */ |
| 59 | struct gsm_bts_trx *trx = vty->index; |
| 60 | vty->index = trx->bts; |
| 61 | vty->index_sub = &trx->bts->description; |
| 62 | } |
| 63 | break; |
| 64 | case TS_NODE: |
| 65 | vty->node = TRX_NODE; |
| 66 | { |
| 67 | /* set vty->index correctly ! */ |
| 68 | struct gsm_bts_trx_ts *ts = vty->index; |
| 69 | vty->index = ts->trx; |
| 70 | vty->index_sub = &ts->trx->description; |
| 71 | } |
| 72 | break; |
Jacob Erlbeck | e86c597 | 2013-09-02 13:17:15 +0200 | [diff] [blame] | 73 | case OML_NODE: |
| 74 | case OM2K_NODE: |
| 75 | vty->node = ENABLE_NODE; |
| 76 | /* NOTE: this only works because it's not part of the config |
| 77 | * tree, where outer commands are searched via vty_go_parent() |
| 78 | * and only (!) executed when a matching one is found. |
| 79 | */ |
| 80 | talloc_free(vty->index); |
| 81 | vty->index = NULL; |
| 82 | break; |
Harald Welte | 0745c4f | 2016-11-11 19:41:59 +0100 | [diff] [blame] | 83 | case OM2K_CON_GROUP_NODE: |
| 84 | vty->node = BTS_NODE; |
| 85 | { |
| 86 | struct con_group *cg = vty->index; |
| 87 | struct gsm_bts *bts = cg->bts; |
| 88 | vty->index = bts; |
| 89 | vty->index_sub = &bts->description; |
| 90 | } |
| 91 | break; |
Holger Hans Peter Freyther | a34bb3d | 2010-06-17 14:42:20 +0800 | [diff] [blame] | 92 | case NAT_BSC_NODE: |
Holger Hans Peter Freyther | 256a040 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 93 | vty->node = NAT_NODE; |
| 94 | { |
| 95 | struct bsc_config *bsc_config = vty->index; |
| 96 | vty->index = bsc_config->nat; |
| 97 | } |
| 98 | break; |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 99 | case PGROUP_NODE: |
| 100 | vty->node = NAT_NODE; |
Jacob Erlbeck | e86c597 | 2013-09-02 13:17:15 +0200 | [diff] [blame] | 101 | vty->index = NULL; |
| 102 | break; |
| 103 | case TRUNK_NODE: |
| 104 | vty->node = MGCP_NODE; |
| 105 | vty->index = NULL; |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 106 | break; |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 107 | case SMPP_ESME_NODE: |
| 108 | vty->node = SMPP_NODE; |
| 109 | vty->index = NULL; |
| 110 | break; |
| 111 | case SMPP_NODE: |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 112 | case MGCP_NODE: |
| 113 | case GBPROXY_NODE: |
| 114 | case SGSN_NODE: |
Holger Hans Peter Freyther | 256a040 | 2010-06-15 20:11:16 +0800 | [diff] [blame] | 115 | case NAT_NODE: |
Holger Hans Peter Freyther | 387a85c | 2011-06-04 20:22:49 +0200 | [diff] [blame] | 116 | case BSC_NODE: |
Holger Hans Peter Freyther | 8c4dca2 | 2010-09-15 23:28:49 +0800 | [diff] [blame] | 117 | case MSC_NODE: |
Harald Welte | 1ed89d4 | 2011-09-01 18:18:43 +0200 | [diff] [blame] | 118 | case MNCC_INT_NODE: |
Holger Hans Peter Freyther | e144211 | 2015-01-27 10:58:29 +0100 | [diff] [blame] | 119 | case NITB_NODE: |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 120 | default: |
Jacob Erlbeck | e86c597 | 2013-09-02 13:17:15 +0200 | [diff] [blame] | 121 | if (bsc_vty_is_config_node(vty, vty->node)) |
| 122 | vty->node = CONFIG_NODE; |
| 123 | else |
| 124 | vty->node = ENABLE_NODE; |
| 125 | |
| 126 | vty->index = NULL; |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 127 | } |
Jacob Erlbeck | e86c597 | 2013-09-02 13:17:15 +0200 | [diff] [blame] | 128 | |
| 129 | return vty->node; |
| 130 | } |
| 131 | |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 132 | int bsc_vty_is_config_node(struct vty *vty, int node) |
| 133 | { |
| 134 | switch (node) { |
| 135 | /* add items that are not config */ |
| 136 | case OML_NODE: |
Harald Welte | 422a781 | 2011-02-12 15:00:35 +0100 | [diff] [blame] | 137 | case OM2K_NODE: |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 138 | case SUBSCR_NODE: |
Holger Hans Peter Freyther | 7653d35 | 2010-09-04 11:15:27 +0800 | [diff] [blame] | 139 | case CONFIG_NODE: |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 140 | return 0; |
| 141 | |
| 142 | default: |
| 143 | return 1; |
| 144 | } |
| 145 | } |
Holger Hans Peter Freyther | 7c831f6 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 146 | |
| 147 | /* a talloc string replace routine */ |
| 148 | void bsc_replace_string(void *ctx, char **dst, const char *newstr) |
| 149 | { |
| 150 | if (*dst) |
| 151 | talloc_free(*dst); |
| 152 | *dst = talloc_strdup(ctx, newstr); |
| 153 | } |