Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <time.h> |
| 28 | #include <limits.h> |
| 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
| 31 | |
| 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
| 35 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 36 | #include <osmocom/mgcp/mgcp.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 37 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 38 | #include <osmocom/mgcp/mgcp_internal.h> |
| 39 | #include <osmocom/mgcp/mgcp_stat.h> |
| 40 | #include <osmocom/mgcp/mgcp_msg.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 41 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 42 | #include <osmocom/mgcp/mgcp_sdp.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 43 | #include <osmocom/mgcp/mgcp_codec.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 44 | |
| 45 | struct mgcp_request { |
| 46 | char *name; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 47 | struct msgb *(*handle_request) (struct mgcp_parse_data * data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 48 | char *debug_name; |
| 49 | }; |
| 50 | |
| 51 | #define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \ |
| 52 | { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME }, |
| 53 | |
| 54 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data); |
| 55 | static struct msgb *handle_create_con(struct mgcp_parse_data *data); |
| 56 | static struct msgb *handle_delete_con(struct mgcp_parse_data *data); |
| 57 | static struct msgb *handle_modify_con(struct mgcp_parse_data *data); |
| 58 | static struct msgb *handle_rsip(struct mgcp_parse_data *data); |
| 59 | static struct msgb *handle_noti_req(struct mgcp_parse_data *data); |
| 60 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 61 | /* Initalize transcoder */ |
| 62 | static int setup_rtp_processing(struct mgcp_endpoint *endp, |
| 63 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 64 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 65 | struct mgcp_config *cfg = endp->cfg; |
| 66 | struct mgcp_conn_rtp *conn_src = NULL; |
| 67 | struct mgcp_conn_rtp *conn_dst = conn; |
| 68 | struct mgcp_conn *_conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 69 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 70 | if (conn->type != MGCP_RTP_DEFAULT) { |
| 71 | LOGP(DLMGCP, LOGL_NOTICE, |
| 72 | "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n", |
| 73 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 77 | if (conn->conn->mode == MGCP_CONN_LOOPBACK) { |
| 78 | LOGP(DLMGCP, LOGL_NOTICE, |
| 79 | "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n", |
| 80 | ENDPOINT_NUMBER(endp)); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* Find the "sister" connection */ |
| 85 | llist_for_each_entry(_conn, &endp->conns, entry) { |
| 86 | if (_conn->id != conn->conn->id) { |
| 87 | conn_src = &_conn->u.rtp; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 92 | return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 95 | /* array of function pointers for handling various |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 96 | * messages. In the future this might be binary sorted |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 97 | * for performance reasons. */ |
| 98 | static const struct mgcp_request mgcp_requests[] = { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 99 | MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint") |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 100 | MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection") |
| 101 | MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection") |
| 102 | MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection") |
| 103 | MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest") |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 104 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 105 | /* SPEC extension */ |
| 106 | MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress") |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 109 | /* Helper function to allocate some memory for responses and retransmissions */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 110 | static struct msgb *mgcp_msgb_alloc(void) |
| 111 | { |
| 112 | struct msgb *msg; |
| 113 | msg = msgb_alloc_headroom(4096, 128, "MGCP msg"); |
| 114 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 115 | LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 116 | |
| 117 | return msg; |
| 118 | } |
| 119 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 120 | /* Helper function for do_retransmission() and create_resp() */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 121 | static struct msgb *do_retransmission(const struct mgcp_endpoint *endp) |
| 122 | { |
| 123 | struct msgb *msg = mgcp_msgb_alloc(); |
| 124 | if (!msg) |
| 125 | return NULL; |
| 126 | |
| 127 | msg->l2h = msgb_put(msg, strlen(endp->last_response)); |
| 128 | memcpy(msg->l2h, endp->last_response, msgb_l2len(msg)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 129 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 130 | return msg; |
| 131 | } |
| 132 | |
| 133 | static struct msgb *create_resp(struct mgcp_endpoint *endp, int code, |
| 134 | const char *txt, const char *msg, |
| 135 | const char *trans, const char *param, |
| 136 | const char *sdp) |
| 137 | { |
| 138 | int len; |
| 139 | struct msgb *res; |
| 140 | |
| 141 | res = mgcp_msgb_alloc(); |
| 142 | if (!res) |
| 143 | return NULL; |
| 144 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 145 | len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s", |
| 146 | code, trans, txt, param ? param : "", sdp ? sdp : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 147 | if (len < 0) { |
| 148 | LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n"); |
| 149 | msgb_free(res); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | res->l2h = msgb_put(res, len); |
| 154 | LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 155 | mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Remember the last transmission per endpoint. |
| 159 | */ |
| 160 | if (endp) { |
| 161 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 162 | talloc_free(endp->last_response); |
| 163 | talloc_free(endp->last_trans); |
| 164 | endp->last_trans = talloc_strdup(tcfg->endpoints, trans); |
| 165 | endp->last_response = talloc_strndup(tcfg->endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 166 | (const char *)res->l2h, |
| 167 | msgb_l2len(res)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return res; |
| 171 | } |
| 172 | |
| 173 | static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 174 | int code, const char *msg, |
| 175 | const char *trans, |
| 176 | const char *param) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 177 | { |
| 178 | return create_resp(endp, code, " OK", msg, trans, param, NULL); |
| 179 | } |
| 180 | |
| 181 | static struct msgb *create_ok_response(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 182 | int code, const char *msg, |
| 183 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 184 | { |
| 185 | return create_ok_resp_with_param(endp, code, msg, trans, NULL); |
| 186 | } |
| 187 | |
| 188 | static struct msgb *create_err_response(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 189 | int code, const char *msg, |
| 190 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 191 | { |
| 192 | return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL); |
| 193 | } |
| 194 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 195 | /* Add MGCP parameters to a message buffer */ |
| 196 | static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp, |
| 197 | const struct mgcp_conn_rtp *conn) |
| 198 | { |
| 199 | int rc; |
| 200 | |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 201 | /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */ |
Philipp Maier | 207ab51 | 2018-02-02 14:19:26 +0100 | [diff] [blame] | 202 | if (endp->wildcarded_req |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 203 | && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) { |
Philipp Maier | dd4ede3 | 2018-02-01 17:52:52 +0100 | [diff] [blame] | 204 | rc = msgb_printf(msg, "Z: %s%x@%s\r\n", |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 205 | MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, |
| 206 | ENDPOINT_NUMBER(endp), endp->cfg->domain); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 207 | if (rc < 0) |
| 208 | return -EINVAL; |
| 209 | } |
| 210 | |
Philipp Maier | c3cfae2 | 2018-01-22 12:03:03 +0100 | [diff] [blame] | 211 | rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 212 | if (rc < 0) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 218 | /* Format MGCP response string (with SDP attached) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 219 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 220 | struct mgcp_conn_rtp *conn, |
| 221 | const char *msg, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 222 | const char *trans_id, |
| 223 | bool add_conn_params) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 224 | { |
| 225 | const char *addr = endp->cfg->local_ip; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 226 | struct msgb *sdp; |
| 227 | int rc; |
| 228 | struct msgb *result; |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 229 | char osmux_extension[strlen("X-Osmux: 255") + 1]; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 230 | char local_ip_addr[INET_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 231 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 232 | sdp = msgb_alloc_headroom(4096, 128, "sdp record"); |
| 233 | if (!sdp) |
| 234 | return NULL; |
| 235 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 236 | if (!addr) { |
| 237 | mgcp_get_local_addr(local_ip_addr, conn); |
| 238 | addr = local_ip_addr; |
| 239 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 240 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 241 | if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) { |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 242 | sprintf(osmux_extension, "X-Osmux: %u", conn->osmux.cid); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 243 | conn->osmux.state = OSMUX_STATE_ACTIVATING; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 244 | } else { |
| 245 | osmux_extension[0] = '\0'; |
| 246 | } |
| 247 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 248 | /* Attach optional connection parameters */ |
| 249 | if (add_conn_params) { |
| 250 | rc = add_params(sdp, endp, conn); |
| 251 | if (rc < 0) |
| 252 | goto error; |
| 253 | } |
| 254 | |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 255 | /* Attach optional OSMUX parameters */ |
| 256 | if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) { |
Philipp Maier | c3cfae2 | 2018-01-22 12:03:03 +0100 | [diff] [blame] | 257 | rc = msgb_printf(sdp, "%s\r\n", osmux_extension); |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 258 | if (rc < 0) |
| 259 | goto error; |
| 260 | } |
| 261 | |
| 262 | /* Attach line break to separate the parameters from the SDP block */ |
Philipp Maier | c3cfae2 | 2018-01-22 12:03:03 +0100 | [diff] [blame] | 263 | rc = msgb_printf(sdp, "\r\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 264 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 265 | rc = mgcp_write_response_sdp(endp, conn, sdp, addr); |
| 266 | if (rc < 0) |
| 267 | goto error; |
| 268 | result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data); |
| 269 | msgb_free(sdp); |
| 270 | return result; |
| 271 | error: |
| 272 | msgb_free(sdp); |
| 273 | return NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 276 | /* Send out dummy packet to keep the connection open, if the connection is an |
| 277 | * osmux connection, send the dummy packet via OSMUX */ |
| 278 | static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 279 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 280 | if (conn->osmux.state != OSMUX_STATE_DISABLED) |
| 281 | osmux_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 282 | else |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 283 | mgcp_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 286 | /* handle incoming messages: |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 287 | * - this can be a command (four letters, space, transaction id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 288 | * - or a response (three numbers, space, transaction id) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 289 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
| 290 | { |
| 291 | struct mgcp_parse_data pdata; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 292 | int rc, i, code, handled = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 293 | struct msgb *resp = NULL; |
| 294 | char *data; |
| 295 | |
| 296 | if (msgb_l2len(msg) < 4) { |
| 297 | LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | if (mgcp_msg_terminate_nul(msg)) |
| 302 | return NULL; |
| 303 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 304 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 305 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 306 | /* attempt to treat it as a response */ |
| 307 | if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 308 | LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | msg->l3h = &msg->l2h[4]; |
| 313 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 314 | /* |
| 315 | * Check for a duplicate message and respond. |
| 316 | */ |
| 317 | memset(&pdata, 0, sizeof(pdata)); |
| 318 | pdata.cfg = cfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 319 | data = mgcp_strline((char *)msg->l3h, &pdata.save); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 320 | rc = mgcp_parse_header(&pdata, data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 321 | if (pdata.endp && pdata.trans |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 322 | && pdata.endp->last_trans |
| 323 | && strcmp(pdata.endp->last_trans, pdata.trans) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 324 | return do_retransmission(pdata.endp); |
| 325 | } |
| 326 | |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 327 | /* check for general parser failure */ |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 328 | if (rc < 0) { |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 329 | LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 330 | return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 333 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 334 | if (strncmp |
| 335 | (mgcp_requests[i].name, (const char *)&msg->l2h[0], |
| 336 | 4) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 337 | handled = 1; |
| 338 | resp = mgcp_requests[i].handle_request(&pdata); |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (!handled) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 344 | LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", |
| 345 | &msg->l2h[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 346 | |
| 347 | return resp; |
| 348 | } |
| 349 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 350 | /* AUEP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 351 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p) |
| 352 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 353 | LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n"); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 354 | return create_ok_response(p->endp, 200, "AUEP", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 357 | /* Try to find a free port by attempting to bind on it. Also handle the |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 358 | * counter that points on the next free port. Since we have a pointer |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 359 | * to the next free port, binding should in work on the first attempt in |
| 360 | * general. In case of failure the next port is tryed until the whole port |
| 361 | * range is tryed once. */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 362 | static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 363 | { |
| 364 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 365 | struct mgcp_rtp_end *end; |
| 366 | struct mgcp_port_range *range; |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 367 | unsigned int tries; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 368 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 369 | OSMO_ASSERT(conn); |
| 370 | end = &conn->end; |
| 371 | OSMO_ASSERT(end); |
| 372 | |
| 373 | range = &endp->cfg->net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 374 | |
| 375 | /* attempt to find a port */ |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 376 | tries = (range->range_end - range->range_start) / 2; |
| 377 | for (i = 0; i < tries; ++i) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 378 | int rc; |
| 379 | |
| 380 | if (range->last_port >= range->range_end) |
| 381 | range->last_port = range->range_start; |
| 382 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 383 | rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 384 | |
| 385 | range->last_port += 2; |
| 386 | if (rc == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | } |
| 391 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 392 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 393 | "Allocating a RTP/RTCP port failed %u times 0x%x.\n", |
| 394 | tries, ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 395 | return -1; |
| 396 | } |
| 397 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 398 | /*! Helper function for check_local_cx_options() to get a pointer of the next |
| 399 | * lco option identifier |
| 400 | * \param[in] lco string |
| 401 | * \returns pointer to the beginning of the LCO identifier, NULL on failure */ |
| 402 | char *get_lco_identifier(const char *options) |
| 403 | { |
| 404 | char *ptr; |
| 405 | unsigned int count = 0; |
| 406 | |
| 407 | /* Jump to the end of the lco identifier */ |
| 408 | ptr = strstr(options, ":"); |
| 409 | if (!ptr) |
| 410 | return NULL; |
| 411 | |
| 412 | /* Walk backwards until the pointer points to the beginning of the |
| 413 | * lco identifier. We know that we stand at the beginning when we |
| 414 | * are either at the beginning of the memory or see a space or |
| 415 | * comma. (this is tolerant, it will accept a:10, b:11 as well as |
| 416 | * a:10,b:11) */ |
| 417 | while (1) { |
| 418 | /* Endless loop protection */ |
| 419 | if (count > 10000) |
| 420 | return NULL; |
| 421 | else if (ptr < options || *ptr == ' ' || *ptr == ',') { |
| 422 | ptr++; |
| 423 | break; |
| 424 | } |
| 425 | ptr--; |
| 426 | count++; |
| 427 | } |
| 428 | |
| 429 | /* Check if we got any result */ |
| 430 | if (*ptr == ':') |
| 431 | return NULL; |
| 432 | |
| 433 | return ptr; |
| 434 | } |
| 435 | |
| 436 | /*! Check the LCO option. This function checks for multiple appearence of LCO |
| 437 | * options, which is illegal |
| 438 | * \param[in] ctx talloc context |
| 439 | * \param[in] lco string |
| 440 | * \returns 0 on success, -1 on failure */ |
| 441 | int check_local_cx_options(void *ctx, const char *options) |
| 442 | { |
| 443 | int i; |
| 444 | char *options_copy; |
| 445 | char *lco_identifier; |
| 446 | char *lco_identifier_end; |
| 447 | char *next_lco_identifier; |
| 448 | |
| 449 | char **lco_seen; |
| 450 | unsigned int lco_seen_n = 0; |
| 451 | |
| 452 | if (!options) |
| 453 | return -1; |
| 454 | |
| 455 | lco_seen = |
| 456 | (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *)); |
| 457 | options_copy = talloc_strdup(ctx, options); |
| 458 | lco_identifier = options_copy; |
| 459 | |
| 460 | do { |
| 461 | /* Move the lco_identifier pointer to the beginning of the |
| 462 | * current lco option identifier */ |
| 463 | lco_identifier = get_lco_identifier(lco_identifier); |
| 464 | if (!lco_identifier) |
| 465 | goto error; |
| 466 | |
| 467 | /* Look ahead to the next LCO option early, since we |
| 468 | * will parse destructively */ |
| 469 | next_lco_identifier = strstr(lco_identifier + 1, ","); |
| 470 | |
| 471 | /* Pinch off the end of the lco field identifier name |
| 472 | * and see if we still got something, also check if |
| 473 | * there is some value after the colon. */ |
| 474 | lco_identifier_end = strstr(lco_identifier, ":"); |
| 475 | if (!lco_identifier_end) |
| 476 | goto error; |
| 477 | if (*(lco_identifier_end + 1) == ' ' |
| 478 | || *(lco_identifier_end + 1) == ',' |
| 479 | || *(lco_identifier_end + 1) == '\0') |
| 480 | goto error; |
| 481 | *lco_identifier_end = '\0'; |
| 482 | if (strlen(lco_identifier) == 0) |
| 483 | goto error; |
| 484 | |
| 485 | /* Check if we have already seen the current field identifier |
| 486 | * before. If yes, we must bail, an LCO must only appear once |
| 487 | * in the LCO string */ |
| 488 | for (i = 0; i < lco_seen_n; i++) { |
| 489 | if (strcmp(lco_seen[i], lco_identifier) == 0) |
| 490 | goto error; |
| 491 | } |
| 492 | lco_seen[lco_seen_n] = lco_identifier; |
| 493 | lco_seen_n++; |
| 494 | |
| 495 | /* The first identifier must always be found at the beginnning |
| 496 | * of the LCO string */ |
| 497 | if (lco_seen[0] != options_copy) |
| 498 | goto error; |
| 499 | |
| 500 | /* Go to the next lco option */ |
| 501 | lco_identifier = next_lco_identifier; |
| 502 | } while (lco_identifier); |
| 503 | |
| 504 | talloc_free(lco_seen); |
| 505 | talloc_free(options_copy); |
| 506 | return 0; |
| 507 | error: |
| 508 | talloc_free(lco_seen); |
| 509 | talloc_free(options_copy); |
| 510 | return -1; |
| 511 | } |
| 512 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 513 | /* Set the LCO from a string (see RFC 3435). |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 514 | * The string is stored in the 'string' field. A NULL string is handled exactly |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 515 | * like an empty string, the 'string' field is never NULL after this function |
| 516 | * has been called. */ |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 517 | static int set_local_cx_options(void *ctx, struct mgcp_lco *lco, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 518 | const char *options) |
| 519 | { |
| 520 | char *p_opt, *a_opt; |
| 521 | char codec[9]; |
| 522 | |
Philipp Maier | 604410c | 2018-06-06 10:02:16 +0200 | [diff] [blame] | 523 | if (!options) |
| 524 | return 0; |
| 525 | if (strlen(options) == 0) |
| 526 | return 0; |
| 527 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 528 | /* Make sure the encoding of the LCO is consistant before we proceed */ |
| 529 | if (check_local_cx_options(ctx, options) != 0) { |
| 530 | LOGP(DLMGCP, LOGL_ERROR, |
| 531 | "local CX options: Internal inconsistency in Local Connection Options!\n"); |
| 532 | return 524; |
| 533 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 534 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 535 | talloc_free(lco->string); |
| 536 | lco->string = talloc_strdup(ctx, options); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 537 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 538 | p_opt = strstr(lco->string, "p:"); |
| 539 | if (p_opt && sscanf(p_opt, "p:%d-%d", |
| 540 | &lco->pkt_period_min, &lco->pkt_period_max) == 1) |
| 541 | lco->pkt_period_max = lco->pkt_period_min; |
| 542 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 543 | /* FIXME: LCO also supports the negotiation of more then one codec. |
| 544 | * (e.g. a:PCMU;G726-32) But this implementation only supports a single |
| 545 | * codec only. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 546 | a_opt = strstr(lco->string, "a:"); |
Philipp Maier | 604410c | 2018-06-06 10:02:16 +0200 | [diff] [blame] | 547 | if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1) { |
| 548 | talloc_free(lco->codec); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 549 | lco->codec = talloc_strdup(ctx, codec); |
Philipp Maier | 604410c | 2018-06-06 10:02:16 +0200 | [diff] [blame] | 550 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 551 | |
| 552 | LOGP(DLMGCP, LOGL_DEBUG, |
| 553 | "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n", |
| 554 | lco->pkt_period_max, lco->codec); |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 555 | |
| 556 | /* Check if the packetization fits the 20ms raster */ |
| 557 | if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) { |
| 558 | LOGP(DLMGCP, LOGL_ERROR, |
| 559 | "local CX options: packetization interval is not a multiple of 20ms!\n"); |
| 560 | return 535; |
| 561 | } |
| 562 | |
| 563 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 567 | struct mgcp_rtp_end *rtp) |
| 568 | { |
| 569 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 570 | |
| 571 | int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc; |
| 572 | |
| 573 | rtp->force_aligned_timing = tcfg->force_aligned_timing; |
| 574 | rtp->force_constant_ssrc = patch_ssrc ? 1 : 0; |
| 575 | |
| 576 | LOGP(DLMGCP, LOGL_DEBUG, |
| 577 | "Configuring RTP endpoint: local port %d%s%s\n", |
| 578 | ntohs(rtp->rtp_port), |
| 579 | rtp->force_aligned_timing ? ", force constant timing" : "", |
| 580 | rtp->force_constant_ssrc ? ", force constant ssrc" : ""); |
| 581 | } |
| 582 | |
| 583 | uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp, |
| 584 | struct mgcp_rtp_end *rtp) |
| 585 | { |
| 586 | int f = 0; |
| 587 | |
| 588 | /* Get the number of frames per channel and packet */ |
| 589 | if (rtp->frames_per_packet) |
| 590 | f = rtp->frames_per_packet; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 591 | else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) { |
| 592 | int den = 1000 * rtp->codec->frame_duration_num; |
| 593 | f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den + |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 594 | den / 2) |
| 595 | / den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 598 | return rtp->codec->rate * f * rtp->codec->frame_duration_num / |
| 599 | rtp->codec->frame_duration_den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line) |
| 603 | { |
| 604 | if (!endp->cfg->osmux_init) { |
| 605 | if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) { |
| 606 | LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n"); |
| 607 | return -1; |
| 608 | } |
| 609 | LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n"); |
| 610 | } |
| 611 | |
| 612 | return mgcp_parse_osmux_cid(line); |
| 613 | } |
| 614 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 615 | /* Process codec information contained in CRCX/MDCX */ |
| 616 | static int handle_codec_info(struct mgcp_conn_rtp *conn, |
| 617 | struct mgcp_parse_data *p, int have_sdp, bool crcx) |
| 618 | { |
| 619 | struct mgcp_endpoint *endp = p->endp; |
| 620 | int rc; |
| 621 | char *cmd; |
| 622 | |
| 623 | if (crcx) |
| 624 | cmd = "CRCX"; |
| 625 | else |
| 626 | cmd = "MDCX"; |
| 627 | |
| 628 | /* Collect codec information */ |
| 629 | if (have_sdp) { |
| 630 | /* If we have SDP, we ignore the local connection options and |
| 631 | * use only the SDP information. */ |
| 632 | mgcp_codec_reset_all(conn); |
| 633 | rc = mgcp_parse_sdp_data(endp, conn, p); |
| 634 | if (rc != 0) { |
| 635 | LOGP(DLMGCP, LOGL_ERROR, |
| 636 | "%s: endpoint:%x sdp not parseable\n", cmd, |
| 637 | ENDPOINT_NUMBER(endp)); |
| 638 | |
| 639 | /* See also RFC 3661: Protocol error */ |
| 640 | return 510; |
| 641 | } |
| 642 | } else if (endp->local_options.codec) { |
| 643 | /* When no SDP is available, we use the codec information from |
| 644 | * the local connection options (if present) */ |
| 645 | mgcp_codec_reset_all(conn); |
| 646 | rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec); |
| 647 | if (rc != 0) |
| 648 | goto error; |
| 649 | } |
| 650 | |
| 651 | /* Make sure we always set a sane default codec */ |
| 652 | if (conn->end.codecs_assigned == 0) { |
| 653 | /* When SDP and/or LCO did not supply any codec information, |
| 654 | * than it makes sense to pick a sane default: (payload-type 0, |
| 655 | * PCMU), see also: OS#2658 */ |
| 656 | mgcp_codec_reset_all(conn); |
| 657 | rc = mgcp_codec_add(conn, 0, NULL); |
| 658 | if (rc != 0) |
| 659 | goto error; |
| 660 | } |
| 661 | |
| 662 | /* Make codec decision */ |
| 663 | if (mgcp_codec_decide(conn) != 0) |
| 664 | goto error; |
| 665 | |
| 666 | return 0; |
| 667 | |
| 668 | error: |
| 669 | LOGP(DLMGCP, LOGL_ERROR, |
| 670 | "%s: endpoint:0x%x codec negotiation failure\n", cmd, |
| 671 | ENDPOINT_NUMBER(endp)); |
| 672 | |
| 673 | /* See also RFC 3661: Codec negotiation failure */ |
| 674 | return 534; |
| 675 | } |
| 676 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 677 | /* CRCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 678 | static struct msgb *handle_create_con(struct mgcp_parse_data *p) |
| 679 | { |
| 680 | struct mgcp_trunk_config *tcfg; |
| 681 | struct mgcp_endpoint *endp = p->endp; |
| 682 | int error_code = 400; |
| 683 | |
| 684 | const char *local_options = NULL; |
| 685 | const char *callid = NULL; |
| 686 | const char *mode = NULL; |
| 687 | char *line; |
| 688 | int have_sdp = 0, osmux_cid = -1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 689 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 690 | struct mgcp_conn *_conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 691 | char conn_name[512]; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 692 | int rc; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 693 | |
| 694 | LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 695 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 696 | /* parse CallID C: and LocalParameters L: */ |
| 697 | for_each_line(line, p->save) { |
| 698 | if (!mgcp_check_param(endp, line)) |
| 699 | continue; |
| 700 | |
| 701 | switch (line[0]) { |
| 702 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 703 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 704 | break; |
| 705 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 706 | callid = (const char *)line + 3; |
| 707 | break; |
| 708 | case 'I': |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 709 | /* It is illegal to send a connection identifier |
| 710 | * together with a CRCX, the MGW will assign the |
| 711 | * connection identifier by itself on CRCX */ |
| 712 | return create_err_response(NULL, 523, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 713 | break; |
| 714 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 715 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 716 | break; |
| 717 | case 'X': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 718 | /* If osmoux is disabled, just skip setting it up */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 719 | if (!p->endp->cfg->osmux) |
| 720 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 721 | if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == |
| 722 | 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 723 | osmux_cid = mgcp_osmux_setup(endp, line); |
| 724 | break; |
| 725 | case '\0': |
| 726 | have_sdp = 1; |
| 727 | goto mgcp_header_done; |
| 728 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 729 | LOGP(DLMGCP, LOGL_NOTICE, |
| 730 | "CRCX: endpoint:%x unhandled option: '%c'/%d\n", |
| 731 | ENDPOINT_NUMBER(endp), *line, *line); |
Philipp Maier | dd0c522 | 2018-02-02 11:08:48 +0100 | [diff] [blame] | 732 | return create_err_response(NULL, 539, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 733 | break; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | mgcp_header_done: |
| 738 | tcfg = p->endp->tcfg; |
| 739 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 740 | /* Check parameters */ |
| 741 | if (!callid) { |
| 742 | LOGP(DLMGCP, LOGL_ERROR, |
| 743 | "CRCX: endpoint:%x insufficient parameters, missing callid\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 744 | ENDPOINT_NUMBER(endp)); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 745 | return create_err_response(endp, 516, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 748 | if (!mode) { |
| 749 | LOGP(DLMGCP, LOGL_ERROR, |
| 750 | "CRCX: endpoint:%x insufficient parameters, missing mode\n", |
| 751 | ENDPOINT_NUMBER(endp)); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 752 | return create_err_response(endp, 517, "CRCX", p->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 755 | /* Check if we are able to accept the creation of another connection */ |
| 756 | if (llist_count(&endp->conns) >= endp->type->max_conns) { |
| 757 | LOGP(DLMGCP, LOGL_ERROR, |
| 758 | "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n", |
| 759 | endp->type->max_conns, ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 760 | if (tcfg->force_realloc) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 761 | /* There is no more room for a connection, make some |
| 762 | * room by blindly tossing the oldest of the two two |
| 763 | * connections */ |
| 764 | mgcp_conn_free_oldest(endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 765 | } else { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 766 | /* There is no more room for a connection, leave |
| 767 | * everything as it is and return with an error */ |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 768 | return create_err_response(endp, 540, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 772 | /* Check if this endpoint already serves a call, if so, check if the |
| 773 | * callids match up so that we are sure that this is our call */ |
| 774 | if (endp->callid && mgcp_verify_call_id(endp, callid)) { |
| 775 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 776 | "CRCX: endpoint:0x%x allready seized by other call (%s)\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 777 | ENDPOINT_NUMBER(endp), endp->callid); |
| 778 | if (tcfg->force_realloc) |
| 779 | /* This is not our call, toss everything by releasing |
| 780 | * the entire endpoint. (rude!) */ |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 781 | mgcp_endp_release(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 782 | else { |
| 783 | /* This is not our call, leave everything as it is and |
| 784 | * return with an error. */ |
| 785 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* Set the callid, creation of another connection will only be possible |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 790 | * when the callid matches up. (Connections are distinguished by their |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 791 | * connection ids) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 792 | endp->callid = talloc_strdup(tcfg->endpoints, callid); |
| 793 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 794 | snprintf(conn_name, sizeof(conn_name), "%s", callid); |
| 795 | _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name); |
| 796 | if (!_conn) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 797 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 798 | "CRCX: endpoint:0x%x unable to allocate RTP connection\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 799 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 800 | goto error2; |
| 801 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 802 | } |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 803 | conn = mgcp_conn_get_rtp(endp, _conn->id); |
| 804 | OSMO_ASSERT(conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 805 | |
| 806 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 807 | error_code = 517; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 808 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 811 | /* Annotate Osmux circuit ID and set it to negotiating state until this |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 812 | * is fully set up from the dummy load. */ |
| 813 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 814 | if (osmux_cid >= 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 815 | conn->osmux.cid = osmux_cid; |
| 816 | conn->osmux.state = OSMUX_STATE_NEGOTIATING; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 817 | } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) { |
| 818 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 819 | "CRCX: endpoint:0x%x osmux only and no osmux offered\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 820 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 821 | goto error2; |
| 822 | } |
| 823 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 824 | /* Set local connection options, if present */ |
| 825 | if (local_options) { |
| 826 | rc = set_local_cx_options(endp->tcfg->endpoints, |
| 827 | &endp->local_options, local_options); |
| 828 | if (rc != 0) { |
| 829 | LOGP(DLMGCP, LOGL_ERROR, |
| 830 | "CRCX: endpoint:%x inavlid local connection options!\n", |
| 831 | ENDPOINT_NUMBER(endp)); |
| 832 | error_code = rc; |
| 833 | goto error2; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* Handle codec information and decide for a suitable codec */ |
| 838 | rc = handle_codec_info(conn, p, have_sdp, true); |
| 839 | mgcp_codec_summary(conn); |
| 840 | if (rc) { |
| 841 | error_code = rc; |
| 842 | goto error2; |
| 843 | } |
| 844 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 845 | conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints, |
| 846 | tcfg->audio_fmtp_extra); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 847 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 848 | if (p->cfg->force_ptime) { |
| 849 | conn->end.packet_duration_ms = p->cfg->force_ptime; |
| 850 | conn->end.force_output_ptime = 1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 853 | mgcp_rtp_end_config(endp, 0, &conn->end); |
| 854 | |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 855 | /* check connection mode setting */ |
| 856 | if (conn->conn->mode != MGCP_CONN_LOOPBACK |
| 857 | && conn->conn->mode != MGCP_CONN_RECV_ONLY |
| 858 | && conn->end.rtp_port == 0) { |
| 859 | LOGP(DLMGCP, LOGL_ERROR, |
| 860 | "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n", |
| 861 | ENDPOINT_NUMBER(endp)); |
| 862 | error_code = 527; |
| 863 | goto error2; |
| 864 | } |
| 865 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 866 | if (allocate_port(endp, conn) != 0) { |
| 867 | goto error2; |
| 868 | } |
| 869 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 870 | if (setup_rtp_processing(endp, conn) != 0) { |
| 871 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 872 | "CRCX: endpoint:0x%x could not start RTP processing!\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 873 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 874 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 875 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 876 | |
| 877 | /* policy CB */ |
| 878 | if (p->cfg->policy_cb) { |
| 879 | int rc; |
| 880 | rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 881 | MGCP_ENDP_CRCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 882 | switch (rc) { |
| 883 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 884 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 885 | "CRCX: endpoint:0x%x CRCX rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 886 | ENDPOINT_NUMBER(endp)); |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 887 | mgcp_endp_release(endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 888 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 889 | break; |
| 890 | case MGCP_POLICY_DEFER: |
| 891 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 892 | return NULL; |
| 893 | break; |
| 894 | case MGCP_POLICY_CONT: |
| 895 | /* just continue */ |
| 896 | break; |
| 897 | } |
| 898 | } |
| 899 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 900 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 901 | "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 902 | ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 903 | if (p->cfg->change_cb) |
| 904 | p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); |
| 905 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 906 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 907 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 908 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 909 | && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 910 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 911 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 912 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 913 | "CRCX: endpoint:0x%x connection successfully created\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 914 | ENDPOINT_NUMBER(endp)); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 915 | return create_response_with_sdp(endp, conn, "CRCX", p->trans, true); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 916 | error2: |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 917 | mgcp_endp_release(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 918 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 3c8ccb6 | 2018-06-04 09:59:24 +0200 | [diff] [blame] | 919 | "CRCX: endpoint:0x%x unable to create connection\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 920 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 921 | return create_err_response(endp, error_code, "CRCX", p->trans); |
| 922 | } |
| 923 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 924 | |
| 925 | |
| 926 | |
| 927 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 928 | /* MDCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 929 | static struct msgb *handle_modify_con(struct mgcp_parse_data *p) |
| 930 | { |
| 931 | struct mgcp_endpoint *endp = p->endp; |
| 932 | int error_code = 500; |
| 933 | int silent = 0; |
| 934 | int have_sdp = 0; |
| 935 | char *line; |
| 936 | const char *local_options = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 937 | const char *mode = NULL; |
| 938 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 939 | const char *conn_id = NULL; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 940 | int rc; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 941 | |
| 942 | LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 943 | |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 944 | /* Prohibit wildcarded requests */ |
| 945 | if (endp->wildcarded_req) { |
| 946 | LOGP(DLMGCP, LOGL_ERROR, |
| 947 | "MDCX: endpoint:0x%x wildcarded endpoint names not supported.\n", |
| 948 | ENDPOINT_NUMBER(endp)); |
| 949 | return create_err_response(endp, 507, "MDCX", p->trans); |
| 950 | } |
| 951 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 952 | if (llist_count(&endp->conns) <= 0) { |
| 953 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 954 | "MDCX: endpoint:0x%x endpoint is not holding a connection.\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 955 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 956 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 957 | } |
| 958 | |
| 959 | for_each_line(line, p->save) { |
| 960 | if (!mgcp_check_param(endp, line)) |
| 961 | continue; |
| 962 | |
| 963 | switch (line[0]) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 964 | case 'C': |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 965 | if (mgcp_verify_call_id(endp, line + 3) != 0) { |
| 966 | error_code = 516; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 967 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 968 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 969 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 970 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 971 | conn_id = (const char *)line + 3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 972 | if (mgcp_verify_ci(endp, conn_id) != 0) { |
| 973 | error_code = 515; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 974 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 975 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 976 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 977 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 978 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 979 | break; |
| 980 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 981 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 982 | break; |
| 983 | case 'Z': |
| 984 | silent = strcmp("noanswer", line + 3) == 0; |
| 985 | break; |
| 986 | case '\0': |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 987 | have_sdp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 988 | goto mgcp_header_done; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 989 | break; |
| 990 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 991 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 992 | "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 993 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Philipp Maier | dd0c522 | 2018-02-02 11:08:48 +0100 | [diff] [blame] | 994 | return create_err_response(NULL, 539, "MDCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 995 | break; |
| 996 | } |
| 997 | } |
| 998 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 999 | mgcp_header_done: |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1000 | if (!conn_id) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1001 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1002 | "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1003 | ENDPOINT_NUMBER(endp)); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1004 | return create_err_response(endp, 515, "MDCX", p->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 1008 | if (!conn) |
| 1009 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 1010 | |
| 1011 | if (mode) { |
| 1012 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 1013 | error_code = 517; |
| 1014 | goto error3; |
| 1015 | } |
| 1016 | } else |
| 1017 | conn->conn->mode = conn->conn->mode_orig; |
| 1018 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1019 | /* Set local connection options, if present */ |
| 1020 | if (local_options) { |
| 1021 | rc = set_local_cx_options(endp->tcfg->endpoints, |
| 1022 | &endp->local_options, local_options); |
| 1023 | if (rc != 0) { |
| 1024 | LOGP(DLMGCP, LOGL_ERROR, |
| 1025 | "MDCX: endpoint:%x inavlid local connection options!\n", |
| 1026 | ENDPOINT_NUMBER(endp)); |
| 1027 | error_code = rc; |
| 1028 | goto error3; |
| 1029 | } |
| 1030 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1031 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1032 | /* Handle codec information and decide for a suitable codec */ |
| 1033 | rc = handle_codec_info(conn, p, have_sdp, false); |
| 1034 | mgcp_codec_summary(conn); |
| 1035 | if (rc) { |
Philipp Maier | af07f66 | 2018-02-02 11:34:02 +0100 | [diff] [blame] | 1036 | error_code = rc; |
| 1037 | goto error3; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 1038 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1039 | |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1040 | /* check connection mode setting */ |
| 1041 | if (conn->conn->mode != MGCP_CONN_LOOPBACK |
| 1042 | && conn->conn->mode != MGCP_CONN_RECV_ONLY |
| 1043 | && conn->end.rtp_port == 0) { |
| 1044 | LOGP(DLMGCP, LOGL_ERROR, |
| 1045 | "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n", |
| 1046 | ENDPOINT_NUMBER(endp)); |
| 1047 | error_code = 527; |
| 1048 | goto error3; |
| 1049 | } |
| 1050 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1051 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1052 | if (setup_rtp_processing(endp, conn) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1053 | goto error3; |
| 1054 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1055 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1056 | /* policy CB */ |
| 1057 | if (p->cfg->policy_cb) { |
| 1058 | int rc; |
| 1059 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1060 | MGCP_ENDP_MDCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1061 | switch (rc) { |
| 1062 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1063 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1064 | "MDCX: endpoint:0x%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1065 | ENDPOINT_NUMBER(endp)); |
| 1066 | if (silent) |
| 1067 | goto out_silent; |
| 1068 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 1069 | break; |
| 1070 | case MGCP_POLICY_DEFER: |
| 1071 | /* stop processing */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1072 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1073 | "MDCX: endpoint:0x%x defered by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1074 | ENDPOINT_NUMBER(endp)); |
| 1075 | return NULL; |
| 1076 | break; |
| 1077 | case MGCP_POLICY_CONT: |
| 1078 | /* just continue */ |
| 1079 | break; |
| 1080 | } |
| 1081 | } |
| 1082 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1083 | mgcp_rtp_end_config(endp, 1, &conn->end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* modify */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1086 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1087 | "MDCX: endpoint:0x%x modified conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1088 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1089 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1090 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 1091 | MGCP_ENDP_MDCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1092 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1093 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 1094 | OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 1095 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
| 1096 | && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1097 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1098 | |
| 1099 | if (silent) |
| 1100 | goto out_silent; |
| 1101 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1102 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1103 | "MDCX: endpoint:0x%x connection successfully modified\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1104 | ENDPOINT_NUMBER(endp)); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 1105 | return create_response_with_sdp(endp, conn, "MDCX", p->trans, false); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1106 | error3: |
| 1107 | return create_err_response(endp, error_code, "MDCX", p->trans); |
| 1108 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1109 | out_silent: |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1110 | LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1111 | ENDPOINT_NUMBER(endp)); |
| 1112 | return NULL; |
| 1113 | } |
| 1114 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1115 | /* DLCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1116 | static struct msgb *handle_delete_con(struct mgcp_parse_data *p) |
| 1117 | { |
| 1118 | struct mgcp_endpoint *endp = p->endp; |
| 1119 | int error_code = 400; |
| 1120 | int silent = 0; |
| 1121 | char *line; |
| 1122 | char stats[1048]; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1123 | const char *conn_id = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1124 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1125 | |
| 1126 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1127 | "DLCX: endpoint:0x%x deleting connection ...\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1128 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1129 | |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 1130 | /* Prohibit wildcarded requests */ |
| 1131 | if (endp->wildcarded_req) { |
| 1132 | LOGP(DLMGCP, LOGL_ERROR, |
| 1133 | "DLCX: endpoint:0x%x wildcarded endpoint names not supported.\n", |
| 1134 | ENDPOINT_NUMBER(endp)); |
| 1135 | return create_err_response(endp, 507, "DLCX", p->trans); |
| 1136 | } |
| 1137 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1138 | if (llist_count(&endp->conns) <= 0) { |
| 1139 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1140 | "DLCX: endpoint:0x%x endpoint is not holding a connection.\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1141 | ENDPOINT_NUMBER(endp)); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1142 | return create_err_response(endp, 515, "DLCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | for_each_line(line, p->save) { |
| 1146 | if (!mgcp_check_param(endp, line)) |
| 1147 | continue; |
| 1148 | |
| 1149 | switch (line[0]) { |
| 1150 | case 'C': |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1151 | if (mgcp_verify_call_id(endp, line + 3) != 0) { |
| 1152 | error_code = 516; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1153 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1154 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1155 | break; |
| 1156 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1157 | conn_id = (const char *)line + 3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1158 | if (mgcp_verify_ci(endp, conn_id) != 0) { |
| 1159 | error_code = 515; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1160 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1161 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1162 | break; |
| 1163 | case 'Z': |
| 1164 | silent = strcmp("noanswer", line + 3) == 0; |
| 1165 | break; |
| 1166 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1167 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1168 | "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1169 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Philipp Maier | dd0c522 | 2018-02-02 11:08:48 +0100 | [diff] [blame] | 1170 | return create_err_response(NULL, 539, "DLCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /* policy CB */ |
| 1176 | if (p->cfg->policy_cb) { |
| 1177 | int rc; |
| 1178 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1179 | MGCP_ENDP_DLCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1180 | switch (rc) { |
| 1181 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1182 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1183 | "DLCX: endpoint:0x%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1184 | ENDPOINT_NUMBER(endp)); |
| 1185 | if (silent) |
| 1186 | goto out_silent; |
| 1187 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 1188 | break; |
| 1189 | case MGCP_POLICY_DEFER: |
| 1190 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1191 | return NULL; |
| 1192 | break; |
| 1193 | case MGCP_POLICY_CONT: |
| 1194 | /* just continue */ |
| 1195 | break; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1199 | /* When no connection id is supplied, we will interpret this as a |
| 1200 | * wildcarded DLCX and drop all connections at once. (See also |
| 1201 | * RFC3435 Section F.7) */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1202 | if (!conn_id) { |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1203 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1204 | "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n", |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1205 | ENDPOINT_NUMBER(endp)); |
| 1206 | |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1207 | mgcp_endp_release(endp); |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1208 | |
| 1209 | /* Note: In this case we do not return any statistics, |
| 1210 | * as we assume that the client is not interested in |
| 1211 | * this case. */ |
| 1212 | return create_ok_response(endp, 200, "DLCX", p->trans); |
| 1213 | } |
| 1214 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1215 | /* Find the connection */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1216 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 1217 | if (!conn) |
| 1218 | goto error3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1219 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1220 | /* save the statistics of the current connection */ |
| 1221 | mgcp_format_stats(stats, sizeof(stats), conn->conn); |
| 1222 | |
| 1223 | /* delete connection */ |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1224 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1225 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 1226 | mgcp_conn_free(endp, conn_id); |
| 1227 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1228 | "DLCX: endpoint:0x%x connection successfully deleted\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1229 | ENDPOINT_NUMBER(endp)); |
| 1230 | |
| 1231 | /* When all connections are closed, the endpoint will be released |
| 1232 | * in order to be ready to be used by another call. */ |
| 1233 | if (llist_count(&endp->conns) <= 0) { |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1234 | mgcp_endp_release(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1235 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1236 | "DLCX: endpoint:0x%x endpoint released\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1237 | ENDPOINT_NUMBER(endp)); |
| 1238 | } |
| 1239 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1240 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1241 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 1242 | MGCP_ENDP_DLCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1243 | |
| 1244 | if (silent) |
| 1245 | goto out_silent; |
| 1246 | return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats); |
| 1247 | |
| 1248 | error3: |
| 1249 | return create_err_response(endp, error_code, "DLCX", p->trans); |
| 1250 | |
| 1251 | out_silent: |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1252 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1253 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1254 | return NULL; |
| 1255 | } |
| 1256 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1257 | /* RSIP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1258 | static struct msgb *handle_rsip(struct mgcp_parse_data *p) |
| 1259 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1260 | /* TODO: Also implement the resetting of a specific endpoint |
| 1261 | * to make mgcp_send_reset_ep() work. Currently this will call |
| 1262 | * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1 |
| 1263 | * to make read_call_agent() reset all endpoints when called |
| 1264 | * next time. In order to selectively reset endpoints some |
| 1265 | * mechanism to distinguish which endpoint shall be resetted |
| 1266 | * is needed */ |
| 1267 | |
| 1268 | LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n"); |
| 1269 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1270 | if (p->cfg->reset_cb) |
| 1271 | p->cfg->reset_cb(p->endp->tcfg); |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | |
| 1275 | static char extract_tone(const char *line) |
| 1276 | { |
| 1277 | const char *str = strstr(line, "D/"); |
| 1278 | if (!str) |
| 1279 | return CHAR_MAX; |
| 1280 | |
| 1281 | return str[2]; |
| 1282 | } |
| 1283 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1284 | /* This can request like DTMF detection and forward, fax detection... it |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1285 | * can also request when the notification should be send and such. We don't |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1286 | * do this right now. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1287 | static struct msgb *handle_noti_req(struct mgcp_parse_data *p) |
| 1288 | { |
| 1289 | int res = 0; |
| 1290 | char *line; |
| 1291 | char tone = CHAR_MAX; |
| 1292 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1293 | LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n"); |
| 1294 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1295 | for_each_line(line, p->save) { |
| 1296 | switch (line[0]) { |
| 1297 | case 'S': |
| 1298 | tone = extract_tone(line); |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | /* we didn't see a signal request with a tone */ |
| 1304 | if (tone == CHAR_MAX) |
| 1305 | return create_ok_response(p->endp, 200, "RQNT", p->trans); |
| 1306 | |
| 1307 | if (p->cfg->rqnt_cb) |
| 1308 | res = p->cfg->rqnt_cb(p->endp, tone); |
| 1309 | |
| 1310 | return res == 0 ? |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1311 | create_ok_response(p->endp, 200, "RQNT", p->trans) : |
| 1312 | create_err_response(p->endp, res, "RQNT", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1315 | /* Connection keepalive timer, will take care that dummy packets are send |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 1316 | * regularly, so that NAT connections stay open */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1317 | static void mgcp_keepalive_timer_cb(void *_tcfg) |
| 1318 | { |
| 1319 | struct mgcp_trunk_config *tcfg = _tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1320 | struct mgcp_conn *conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1321 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1322 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1323 | LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1324 | tcfg->trunk_nr); |
| 1325 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1326 | /* Do not accept invalid configuration values |
| 1327 | * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and |
| 1328 | * values greater 0 */ |
| 1329 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 1330 | |
| 1331 | /* The dummy packet functionality has been disabled, we will exit |
| 1332 | * immediately, no further timer is scheduled, which means we will no |
| 1333 | * longer send dummy packets even when we did before */ |
| 1334 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER) |
| 1335 | return; |
| 1336 | |
| 1337 | /* In cases where only one dummy packet is sent, we do not need |
| 1338 | * the timer since the functions that handle the CRCX and MDCX are |
| 1339 | * triggering the sending of the dummy packet. So we behave like in |
| 1340 | * the MGCP_KEEPALIVE_NEVER case */ |
| 1341 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1342 | return; |
| 1343 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1344 | /* Send walk over all endpoints and send out dummy packets through |
| 1345 | * every connection present on each endpoint */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1346 | for (i = 1; i < tcfg->number_endpoints; ++i) { |
| 1347 | struct mgcp_endpoint *endp = &tcfg->endpoints[i]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1348 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 1349 | if (conn->mode == MGCP_CONN_RECV_ONLY) |
| 1350 | send_dummy(endp, &conn->u.rtp); |
| 1351 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1354 | /* Schedule the keepalive timer for the next round */ |
| 1355 | LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1356 | tcfg->trunk_nr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1357 | osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, |
| 1358 | 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval) |
| 1362 | { |
| 1363 | tcfg->keepalive_interval = interval; |
| 1364 | osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg); |
| 1365 | |
| 1366 | if (interval <= 0) |
| 1367 | osmo_timer_del(&tcfg->keepalive_timer); |
| 1368 | else |
| 1369 | osmo_timer_schedule(&tcfg->keepalive_timer, |
| 1370 | tcfg->keepalive_interval, 0); |
| 1371 | } |
| 1372 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1373 | /*! allocate configuration with default values. |
| 1374 | * (called once at startup by main function) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1375 | struct mgcp_config *mgcp_config_alloc(void) |
| 1376 | { |
| 1377 | struct mgcp_config *cfg; |
| 1378 | |
| 1379 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 1380 | if (!cfg) { |
| 1381 | LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 1382 | return NULL; |
| 1383 | } |
| 1384 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1385 | osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain)); |
| 1386 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1387 | cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START; |
| 1388 | cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END; |
| 1389 | cfg->net_ports.last_port = cfg->net_ports.range_start; |
| 1390 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1391 | cfg->source_port = 2427; |
| 1392 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1393 | cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1394 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1395 | cfg->rtp_processing_cb = &mgcp_rtp_processing_default; |
| 1396 | cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default; |
| 1397 | |
| 1398 | cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default; |
| 1399 | |
| 1400 | /* default trunk handling */ |
| 1401 | cfg->trunk.cfg = cfg; |
| 1402 | cfg->trunk.trunk_nr = 0; |
| 1403 | cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL; |
| 1404 | cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1405 | cfg->trunk.audio_payload = 126; |
| 1406 | cfg->trunk.audio_send_ptime = 1; |
| 1407 | cfg->trunk.audio_send_name = 1; |
| 1408 | cfg->trunk.omit_rtcp = 0; |
| 1409 | mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE); |
| 1410 | |
| 1411 | INIT_LLIST_HEAD(&cfg->trunks); |
| 1412 | |
| 1413 | return cfg; |
| 1414 | } |
| 1415 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1416 | /*! allocate configuration with default values. |
| 1417 | * (called once at startup by VTY) |
| 1418 | * \param[in] cfg mgcp configuration |
| 1419 | * \param[in] nr trunk number |
| 1420 | * \returns pointer to allocated trunk configuration */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1421 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr) |
| 1422 | { |
| 1423 | struct mgcp_trunk_config *trunk; |
| 1424 | |
| 1425 | trunk = talloc_zero(cfg, struct mgcp_trunk_config); |
| 1426 | if (!trunk) { |
| 1427 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n"); |
| 1428 | return NULL; |
| 1429 | } |
| 1430 | |
| 1431 | trunk->cfg = cfg; |
| 1432 | trunk->trunk_type = MGCP_TRUNK_E1; |
| 1433 | trunk->trunk_nr = nr; |
| 1434 | trunk->audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1435 | trunk->audio_payload = 126; |
| 1436 | trunk->audio_send_ptime = 1; |
| 1437 | trunk->audio_send_name = 1; |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1438 | trunk->vty_number_endpoints = 33; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1439 | trunk->omit_rtcp = 0; |
| 1440 | mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE); |
| 1441 | llist_add_tail(&trunk->entry, &cfg->trunks); |
| 1442 | return trunk; |
| 1443 | } |
| 1444 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1445 | /*! get trunk configuration by trunk number (index). |
| 1446 | * \param[in] cfg mgcp configuration |
| 1447 | * \param[in] index trunk number |
| 1448 | * \returns pointer to trunk configuration, NULL on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1449 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index) |
| 1450 | { |
| 1451 | struct mgcp_trunk_config *trunk; |
| 1452 | |
| 1453 | llist_for_each_entry(trunk, &cfg->trunks, entry) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1454 | if (trunk->trunk_nr == index) |
| 1455 | return trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1456 | |
| 1457 | return NULL; |
| 1458 | } |
| 1459 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1460 | /*! allocate endpoints and set default values. |
| 1461 | * (called once at startup by VTY) |
| 1462 | * \param[in] tcfg trunk configuration |
| 1463 | * \returns 0 on success, -1 on failure */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1464 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg) |
| 1465 | { |
| 1466 | int i; |
| 1467 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1468 | tcfg->endpoints = _talloc_zero_array(tcfg->cfg, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1469 | sizeof(struct mgcp_endpoint), |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1470 | tcfg->vty_number_endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1471 | "endpoints"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1472 | if (!tcfg->endpoints) |
| 1473 | return -1; |
| 1474 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1475 | for (i = 0; i < tcfg->vty_number_endpoints; ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1476 | INIT_LLIST_HEAD(&tcfg->endpoints[i].conns); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1477 | tcfg->endpoints[i].cfg = tcfg->cfg; |
| 1478 | tcfg->endpoints[i].tcfg = tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1479 | |
| 1480 | /* NOTE: Currently all endpoints are of type RTP, this will |
| 1481 | * change when new variations are implemented */ |
| 1482 | tcfg->endpoints[i].type = &ep_typeset.rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1485 | tcfg->number_endpoints = tcfg->vty_number_endpoints; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1486 | return 0; |
| 1487 | } |
| 1488 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1489 | static int send_agent(struct mgcp_config *cfg, const char *buf, int len) |
| 1490 | { |
| 1491 | return write(cfg->gw_fd.bfd.fd, buf, len); |
| 1492 | } |
| 1493 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1494 | /*! Reset all endpoints by sending RSIP message to self. |
| 1495 | * (called by VTY) |
| 1496 | * \param[in] endp trunk endpoint |
| 1497 | * \param[in] endpoint number |
| 1498 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1499 | int mgcp_send_reset_all(struct mgcp_config *cfg) |
| 1500 | { |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1501 | char buf[MGCP_ENDPOINT_MAXLEN + 128]; |
| 1502 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1503 | int rc; |
| 1504 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1505 | len = snprintf(buf, sizeof(buf), |
| 1506 | "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain); |
| 1507 | if (len < 0) |
| 1508 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1509 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1510 | rc = send_agent(cfg, buf, len); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1511 | if (rc <= 0) |
| 1512 | return -1; |
| 1513 | |
| 1514 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1517 | /*! Reset a single endpoint by sending RSIP message to self. |
| 1518 | * (called by VTY) |
| 1519 | * \param[in] endp trunk endpoint |
| 1520 | * \param[in] endpoint number |
| 1521 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1522 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint) |
| 1523 | { |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1524 | char buf[MGCP_ENDPOINT_MAXLEN + 128]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1525 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1526 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1527 | |
| 1528 | len = snprintf(buf, sizeof(buf), |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1529 | "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1530 | if (len < 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1531 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1532 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1533 | rc = send_agent(endp->cfg, buf, len); |
| 1534 | if (rc <= 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1535 | return -1; |
| 1536 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1537 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1538 | } |