Holger Hans Peter Freyther | e8e5ef2 | 2014-03-23 18:08:26 +0100 | [diff] [blame] | 1 | /* Control Interface Implementation for the SGSN */ |
| 2 | /* |
| 3 | * (C) 2014 by Holger Hans Peter Freyther |
| 4 | * (C) 2014 by sysmocom s.f.m.c. GmbH |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/control_if.h> |
| 23 | #include <openbsc/control_cmd.h> |
| 24 | #include <openbsc/gprs_sgsn.h> |
| 25 | #include <openbsc/sgsn.h> |
| 26 | #include <openbsc/debug.h> |
| 27 | |
| 28 | #include <pdp.h> |
| 29 | |
| 30 | extern vector ctrl_node_vec; |
| 31 | |
| 32 | static int verify_subscriber_list(struct ctrl_cmd *cmd, const char *v, void *d) |
| 33 | { |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | static int set_subscriber_list(struct ctrl_cmd *cmd, void *d) |
| 38 | { |
| 39 | cmd->reply = "Get only attribute"; |
| 40 | return CTRL_CMD_ERROR; |
| 41 | } |
| 42 | |
| 43 | static int get_subscriber_list(struct ctrl_cmd *cmd, void *d) |
| 44 | { |
| 45 | struct sgsn_mm_ctx *mm; |
| 46 | |
| 47 | cmd->reply = talloc_strdup(cmd, ""); |
| 48 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 49 | char *addr = NULL; |
| 50 | struct sgsn_pdp_ctx *pdp; |
| 51 | |
| 52 | if (strlen(mm->imsi) == 0) |
| 53 | continue; |
| 54 | |
| 55 | llist_for_each_entry(pdp, &mm->pdp_list, list) |
| 56 | addr = gprs_pdpaddr2str(pdp->lib->eua.v, |
| 57 | pdp->lib->eua.l); |
| 58 | |
| 59 | cmd->reply = talloc_asprintf_append( |
| 60 | cmd->reply, |
| 61 | "%s,%s\n", mm->imsi, addr ? addr : ""); |
| 62 | } |
| 63 | |
| 64 | return CTRL_CMD_REPLY; |
| 65 | } |
| 66 | CTRL_CMD_DEFINE(subscriber_list, "subscriber-list-active-v1"); |
| 67 | |
| 68 | int sgsn_ctrl_cmds_install(void) |
| 69 | { |
| 70 | int rc = 0; |
| 71 | rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_subscriber_list); |
| 72 | return rc; |
| 73 | } |
| 74 | |
| 75 | static int sgsn_cmd_handle(struct ctrl_cmd *cmd, void *data) |
| 76 | { |
| 77 | char *request; |
| 78 | cmd->reply = NULL; |
| 79 | cmd->node = NULL; |
| 80 | vector vline, cmdvec, cmds_vec; |
| 81 | int i, ret; |
| 82 | |
| 83 | ret = CTRL_CMD_ERROR; |
| 84 | |
| 85 | request = talloc_strdup(tall_bsc_ctx, cmd->variable); |
| 86 | if (!request) |
| 87 | goto err; |
| 88 | |
| 89 | for (i = 0; i < strlen(request); ++i) { |
| 90 | if (request[i] == '.') |
| 91 | request[i] = ' '; |
| 92 | } |
| 93 | |
| 94 | vline = cmd_make_strvec(request); |
| 95 | talloc_free(request); |
| 96 | if (!vline) { |
| 97 | cmd->reply = "cmd_make_strvec failed."; |
| 98 | goto err; |
| 99 | } |
| 100 | |
| 101 | /* If we're here the rest must be the command */ |
| 102 | cmdvec = vector_init(vector_active(vline)); |
| 103 | for (i = 0 ; i < vector_active(vline); ++i) { |
| 104 | vector_set(cmdvec, vector_slot(vline, i)); |
| 105 | } |
| 106 | |
| 107 | /* Get the command vector of the right node */ |
| 108 | cmds_vec = vector_lookup(ctrl_node_vec, CTRL_NODE_ROOT); |
| 109 | |
| 110 | if (!cmds_vec) { |
| 111 | cmd->reply = "Command not found."; |
| 112 | vector_free(cmdvec); |
| 113 | goto err; |
| 114 | } |
| 115 | |
| 116 | ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data); |
| 117 | |
| 118 | vector_free(cmdvec); |
| 119 | cmd_free_strvec(vline); |
| 120 | |
| 121 | err: |
| 122 | if (!cmd->reply) { |
| 123 | LOGP(DCTRL, LOGL_ERROR, "cmd->reply has not been set.\n"); |
| 124 | if (ret == CTRL_CMD_ERROR) |
| 125 | cmd->reply = "An error has occured."; |
| 126 | else |
| 127 | cmd->reply = "Command has been handled."; |
| 128 | } |
| 129 | |
| 130 | if (ret == CTRL_CMD_ERROR) |
| 131 | cmd->type = CTRL_TYPE_ERROR; |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | struct ctrl_handle *sgsn_controlif_setup(struct gsm_network *net, uint16_t port) |
| 136 | { |
| 137 | return controlif_setup(net, port, sgsn_cmd_handle); |
| 138 | } |