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> |
| 41 | #include <osmocom/mgcp/mgcp_ep.h> |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 42 | #include <osmocom/mgcp/mgcp_sdp.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 43 | |
| 44 | struct mgcp_request { |
| 45 | char *name; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 46 | struct msgb *(*handle_request) (struct mgcp_parse_data * data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 47 | char *debug_name; |
| 48 | }; |
| 49 | |
| 50 | #define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \ |
| 51 | { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME }, |
| 52 | |
| 53 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data); |
| 54 | static struct msgb *handle_create_con(struct mgcp_parse_data *data); |
| 55 | static struct msgb *handle_delete_con(struct mgcp_parse_data *data); |
| 56 | static struct msgb *handle_modify_con(struct mgcp_parse_data *data); |
| 57 | static struct msgb *handle_rsip(struct mgcp_parse_data *data); |
| 58 | static struct msgb *handle_noti_req(struct mgcp_parse_data *data); |
| 59 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 60 | /* Initalize transcoder */ |
| 61 | static int setup_rtp_processing(struct mgcp_endpoint *endp, |
| 62 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 63 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 64 | struct mgcp_config *cfg = endp->cfg; |
| 65 | struct mgcp_conn_rtp *conn_src = NULL; |
| 66 | struct mgcp_conn_rtp *conn_dst = conn; |
| 67 | struct mgcp_conn *_conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 68 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 69 | if (conn->type != MGCP_RTP_DEFAULT) { |
| 70 | LOGP(DLMGCP, LOGL_NOTICE, |
| 71 | "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n", |
| 72 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 76 | if (conn->conn->mode == MGCP_CONN_LOOPBACK) { |
| 77 | LOGP(DLMGCP, LOGL_NOTICE, |
| 78 | "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n", |
| 79 | ENDPOINT_NUMBER(endp)); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /* Find the "sister" connection */ |
| 84 | llist_for_each_entry(_conn, &endp->conns, entry) { |
| 85 | if (_conn->id != conn->conn->id) { |
| 86 | conn_src = &_conn->u.rtp; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return cfg->setup_rtp_processing_cb(endp, &conn_dst->end, |
| 92 | &conn_src->end); |
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 | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 195 | /* Format MGCP response string (with SDP attached) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 196 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 197 | struct mgcp_conn_rtp *conn, |
| 198 | const char *msg, |
| 199 | const char *trans_id) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 200 | { |
| 201 | const char *addr = endp->cfg->local_ip; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 202 | struct msgb *sdp; |
| 203 | int rc; |
| 204 | struct msgb *result; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | char osmux_extension[strlen("\nX-Osmux: 255") + 1]; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 206 | char local_ip_addr[INET_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 207 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 208 | sdp = msgb_alloc_headroom(4096, 128, "sdp record"); |
| 209 | if (!sdp) |
| 210 | return NULL; |
| 211 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 212 | if (!addr) { |
| 213 | mgcp_get_local_addr(local_ip_addr, conn); |
| 214 | addr = local_ip_addr; |
| 215 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 216 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 217 | if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) { |
| 218 | sprintf(osmux_extension, "\nX-Osmux: %u", conn->osmux.cid); |
| 219 | conn->osmux.state = OSMUX_STATE_ACTIVATING; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 220 | } else { |
| 221 | osmux_extension[0] = '\0'; |
| 222 | } |
| 223 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 224 | rc = msgb_printf(sdp, "I: %s%s\n\n", conn->conn->id, osmux_extension); |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 225 | if (rc < 0) |
| 226 | goto error; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 227 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 228 | rc = mgcp_write_response_sdp(endp, conn, sdp, addr); |
| 229 | if (rc < 0) |
| 230 | goto error; |
| 231 | result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data); |
| 232 | msgb_free(sdp); |
| 233 | return result; |
| 234 | error: |
| 235 | msgb_free(sdp); |
| 236 | return NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 239 | /* Send out dummy packet to keep the connection open, if the connection is an |
| 240 | * osmux connection, send the dummy packet via OSMUX */ |
| 241 | 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] | 242 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 243 | if (conn->osmux.state != OSMUX_STATE_DISABLED) |
| 244 | osmux_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 245 | else |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 246 | mgcp_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 249 | /* handle incoming messages: |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 250 | * - this can be a command (four letters, space, transaction id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 251 | * - or a response (three numbers, space, transaction id) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 252 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
| 253 | { |
| 254 | struct mgcp_parse_data pdata; |
| 255 | int i, code, handled = 0; |
| 256 | struct msgb *resp = NULL; |
| 257 | char *data; |
| 258 | |
| 259 | if (msgb_l2len(msg) < 4) { |
| 260 | LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | if (mgcp_msg_terminate_nul(msg)) |
| 265 | return NULL; |
| 266 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 267 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 268 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 269 | /* attempt to treat it as a response */ |
| 270 | if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 271 | LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | msg->l3h = &msg->l2h[4]; |
| 276 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 277 | /* |
| 278 | * Check for a duplicate message and respond. |
| 279 | */ |
| 280 | memset(&pdata, 0, sizeof(pdata)); |
| 281 | pdata.cfg = cfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 282 | data = mgcp_strline((char *)msg->l3h, &pdata.save); |
| 283 | pdata.found = mgcp_parse_header(&pdata, data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 284 | if (pdata.endp && pdata.trans |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 285 | && pdata.endp->last_trans |
| 286 | && strcmp(pdata.endp->last_trans, pdata.trans) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 287 | return do_retransmission(pdata.endp); |
| 288 | } |
| 289 | |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame^] | 290 | /* check for general parser failure */ |
| 291 | if (pdata.found < 0) { |
| 292 | LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h); |
| 293 | return create_err_response(NULL, -pdata.found, (const char *) msg->l2h, pdata.trans); |
| 294 | } |
| 295 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 296 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 297 | if (strncmp |
| 298 | (mgcp_requests[i].name, (const char *)&msg->l2h[0], |
| 299 | 4) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 300 | handled = 1; |
| 301 | resp = mgcp_requests[i].handle_request(&pdata); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (!handled) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 307 | LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", |
| 308 | &msg->l2h[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 309 | |
| 310 | return resp; |
| 311 | } |
| 312 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 313 | /* AUEP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 314 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p) |
| 315 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 316 | LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n"); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame^] | 317 | return create_ok_response(p->endp, 200, "AUEP", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 320 | /* 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] | 321 | * counter that points on the next free port. Since we have a pointer |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 322 | * to the next free port, binding should work on the first attempt, |
| 323 | * nevertheless, try at least the next 200 ports before giving up */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 324 | 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] | 325 | { |
| 326 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 327 | struct mgcp_rtp_end *end; |
| 328 | struct mgcp_port_range *range; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 329 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 330 | OSMO_ASSERT(conn); |
| 331 | end = &conn->end; |
| 332 | OSMO_ASSERT(end); |
| 333 | |
| 334 | range = &endp->cfg->net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 335 | |
| 336 | /* attempt to find a port */ |
| 337 | for (i = 0; i < 200; ++i) { |
| 338 | int rc; |
| 339 | |
| 340 | if (range->last_port >= range->range_end) |
| 341 | range->last_port = range->range_start; |
| 342 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 343 | rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 344 | |
| 345 | range->last_port += 2; |
| 346 | if (rc == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | } |
| 351 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 352 | LOGP(DLMGCP, LOGL_ERROR, |
| 353 | "Allocating a RTP/RTCP port failed 200 times 0x%x.\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 354 | ENDPOINT_NUMBER(endp)); |
| 355 | return -1; |
| 356 | } |
| 357 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 358 | /* Set the LCO from a string (see RFC 3435). |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 359 | * 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] | 360 | * like an empty string, the 'string' field is never NULL after this function |
| 361 | * has been called. */ |
| 362 | static void set_local_cx_options(void *ctx, struct mgcp_lco *lco, |
| 363 | const char *options) |
| 364 | { |
| 365 | char *p_opt, *a_opt; |
| 366 | char codec[9]; |
| 367 | |
| 368 | talloc_free(lco->string); |
| 369 | talloc_free(lco->codec); |
| 370 | lco->codec = NULL; |
| 371 | lco->pkt_period_min = lco->pkt_period_max = 0; |
| 372 | lco->string = talloc_strdup(ctx, options ? options : ""); |
| 373 | |
| 374 | p_opt = strstr(lco->string, "p:"); |
| 375 | if (p_opt && sscanf(p_opt, "p:%d-%d", |
| 376 | &lco->pkt_period_min, &lco->pkt_period_max) == 1) |
| 377 | lco->pkt_period_max = lco->pkt_period_min; |
| 378 | |
| 379 | a_opt = strstr(lco->string, "a:"); |
| 380 | if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1) |
| 381 | lco->codec = talloc_strdup(ctx, codec); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 382 | |
| 383 | LOGP(DLMGCP, LOGL_DEBUG, |
| 384 | "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n", |
| 385 | lco->pkt_period_max, lco->codec); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 389 | struct mgcp_rtp_end *rtp) |
| 390 | { |
| 391 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 392 | |
| 393 | int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc; |
| 394 | |
| 395 | rtp->force_aligned_timing = tcfg->force_aligned_timing; |
| 396 | rtp->force_constant_ssrc = patch_ssrc ? 1 : 0; |
| 397 | |
| 398 | LOGP(DLMGCP, LOGL_DEBUG, |
| 399 | "Configuring RTP endpoint: local port %d%s%s\n", |
| 400 | ntohs(rtp->rtp_port), |
| 401 | rtp->force_aligned_timing ? ", force constant timing" : "", |
| 402 | rtp->force_constant_ssrc ? ", force constant ssrc" : ""); |
| 403 | } |
| 404 | |
| 405 | uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp, |
| 406 | struct mgcp_rtp_end *rtp) |
| 407 | { |
| 408 | int f = 0; |
| 409 | |
| 410 | /* Get the number of frames per channel and packet */ |
| 411 | if (rtp->frames_per_packet) |
| 412 | f = rtp->frames_per_packet; |
| 413 | else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) { |
| 414 | int den = 1000 * rtp->codec.frame_duration_num; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 415 | f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den + |
| 416 | den / 2) |
| 417 | / den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 420 | return rtp->codec.rate * f * rtp->codec.frame_duration_num / |
| 421 | rtp->codec.frame_duration_den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line) |
| 425 | { |
| 426 | if (!endp->cfg->osmux_init) { |
| 427 | if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) { |
| 428 | LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n"); |
| 429 | return -1; |
| 430 | } |
| 431 | LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n"); |
| 432 | } |
| 433 | |
| 434 | return mgcp_parse_osmux_cid(line); |
| 435 | } |
| 436 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 437 | /* CRCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 438 | static struct msgb *handle_create_con(struct mgcp_parse_data *p) |
| 439 | { |
| 440 | struct mgcp_trunk_config *tcfg; |
| 441 | struct mgcp_endpoint *endp = p->endp; |
| 442 | int error_code = 400; |
| 443 | |
| 444 | const char *local_options = NULL; |
| 445 | const char *callid = NULL; |
| 446 | const char *mode = NULL; |
| 447 | char *line; |
| 448 | int have_sdp = 0, osmux_cid = -1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 449 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 450 | struct mgcp_conn *_conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 451 | char conn_name[512]; |
| 452 | |
| 453 | LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 454 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 455 | /* parse CallID C: and LocalParameters L: */ |
| 456 | for_each_line(line, p->save) { |
| 457 | if (!mgcp_check_param(endp, line)) |
| 458 | continue; |
| 459 | |
| 460 | switch (line[0]) { |
| 461 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 462 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 463 | break; |
| 464 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 465 | callid = (const char *)line + 3; |
| 466 | break; |
| 467 | case 'I': |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 468 | /* It is illegal to send a connection identifier |
| 469 | * together with a CRCX, the MGW will assign the |
| 470 | * connection identifier by itself on CRCX */ |
| 471 | return create_err_response(NULL, 523, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 472 | break; |
| 473 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 474 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 475 | break; |
| 476 | case 'X': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 477 | /* If osmoux is disabled, just skip setting it up */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 478 | if (!p->endp->cfg->osmux) |
| 479 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 480 | if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == |
| 481 | 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 482 | osmux_cid = mgcp_osmux_setup(endp, line); |
| 483 | break; |
| 484 | case '\0': |
| 485 | have_sdp = 1; |
| 486 | goto mgcp_header_done; |
| 487 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 488 | LOGP(DLMGCP, LOGL_NOTICE, |
| 489 | "CRCX: endpoint:%x unhandled option: '%c'/%d\n", |
| 490 | ENDPOINT_NUMBER(endp), *line, *line); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | mgcp_header_done: |
| 496 | tcfg = p->endp->tcfg; |
| 497 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 498 | /* Check parameters */ |
| 499 | if (!callid) { |
| 500 | LOGP(DLMGCP, LOGL_ERROR, |
| 501 | "CRCX: endpoint:%x insufficient parameters, missing callid\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 502 | ENDPOINT_NUMBER(endp)); |
| 503 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 504 | } |
| 505 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 506 | if (!mode) { |
| 507 | LOGP(DLMGCP, LOGL_ERROR, |
| 508 | "CRCX: endpoint:%x insufficient parameters, missing mode\n", |
| 509 | ENDPOINT_NUMBER(endp)); |
| 510 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 511 | } |
| 512 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 513 | /* Check if we are able to accept the creation of another connection */ |
| 514 | if (llist_count(&endp->conns) >= endp->type->max_conns) { |
| 515 | LOGP(DLMGCP, LOGL_ERROR, |
| 516 | "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n", |
| 517 | endp->type->max_conns, ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 518 | if (tcfg->force_realloc) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 519 | /* There is no more room for a connection, make some |
| 520 | * room by blindly tossing the oldest of the two two |
| 521 | * connections */ |
| 522 | mgcp_conn_free_oldest(endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 523 | } else { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 524 | /* There is no more room for a connection, leave |
| 525 | * everything as it is and return with an error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 526 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 527 | } |
| 528 | } |
| 529 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 530 | /* Check if this endpoint already serves a call, if so, check if the |
| 531 | * callids match up so that we are sure that this is our call */ |
| 532 | if (endp->callid && mgcp_verify_call_id(endp, callid)) { |
| 533 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 534 | "CRCX: endpoint:0x%x allready seized by other call (%s)\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 535 | ENDPOINT_NUMBER(endp), endp->callid); |
| 536 | if (tcfg->force_realloc) |
| 537 | /* This is not our call, toss everything by releasing |
| 538 | * the entire endpoint. (rude!) */ |
| 539 | mgcp_release_endp(endp); |
| 540 | else { |
| 541 | /* This is not our call, leave everything as it is and |
| 542 | * return with an error. */ |
| 543 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* Set the callid, creation of another connection will only be possible |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 548 | * when the callid matches up. (Connections are distinguished by their |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 549 | * connection ids) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 550 | endp->callid = talloc_strdup(tcfg->endpoints, callid); |
| 551 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 552 | /* Extract audio codec information */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 553 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 554 | local_options); |
| 555 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 556 | snprintf(conn_name, sizeof(conn_name), "%s", callid); |
| 557 | _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name); |
| 558 | if (!_conn) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 559 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 560 | "CRCX: endpoint:0x%x unable to allocate RTP connection\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 561 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 562 | goto error2; |
| 563 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 564 | } |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 565 | conn = mgcp_conn_get_rtp(endp, _conn->id); |
| 566 | OSMO_ASSERT(conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 567 | |
| 568 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 569 | error_code = 517; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 570 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 573 | /* Annotate Osmux circuit ID and set it to negotiating state until this |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 574 | * is fully set up from the dummy load. */ |
| 575 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 576 | if (osmux_cid >= 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 577 | conn->osmux.cid = osmux_cid; |
| 578 | conn->osmux.state = OSMUX_STATE_NEGOTIATING; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 579 | } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) { |
| 580 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 581 | "CRCX: endpoint:0x%x osmux only and no osmux offered\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 582 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 583 | goto error2; |
| 584 | } |
| 585 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 586 | /* set up RTP media parameters */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 587 | if (have_sdp) |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 588 | mgcp_parse_sdp_data(endp, conn, p); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 589 | else if (endp->local_options.codec) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 590 | mgcp_set_audio_info(p->cfg, &conn->end.codec, |
| 591 | PTYPE_UNDEFINED, endp->local_options.codec); |
| 592 | conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints, |
| 593 | tcfg->audio_fmtp_extra); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 594 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 595 | if (p->cfg->force_ptime) { |
| 596 | conn->end.packet_duration_ms = p->cfg->force_ptime; |
| 597 | conn->end.force_output_ptime = 1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 600 | mgcp_rtp_end_config(endp, 0, &conn->end); |
| 601 | |
| 602 | if (allocate_port(endp, conn) != 0) { |
| 603 | goto error2; |
| 604 | } |
| 605 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 606 | if (setup_rtp_processing(endp, conn) != 0) { |
| 607 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 608 | "CRCX: endpoint:0x%x could not start RTP processing!\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 609 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 610 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 611 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 612 | |
| 613 | /* policy CB */ |
| 614 | if (p->cfg->policy_cb) { |
| 615 | int rc; |
| 616 | rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 617 | MGCP_ENDP_CRCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 618 | switch (rc) { |
| 619 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 620 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 621 | "CRCX: endpoint:0x%x CRCX rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 622 | ENDPOINT_NUMBER(endp)); |
| 623 | mgcp_release_endp(endp); |
| 624 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 625 | break; |
| 626 | case MGCP_POLICY_DEFER: |
| 627 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 628 | return NULL; |
| 629 | break; |
| 630 | case MGCP_POLICY_CONT: |
| 631 | /* just continue */ |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 636 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 637 | "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 638 | ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 639 | if (p->cfg->change_cb) |
| 640 | p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); |
| 641 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 642 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 643 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 644 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 645 | && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 646 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 647 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 648 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 649 | "CRCX: endpoint:0x%x connection successfully created\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 650 | ENDPOINT_NUMBER(endp)); |
| 651 | return create_response_with_sdp(endp, conn, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 652 | error2: |
| 653 | mgcp_release_endp(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 654 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 655 | "CRCX: endpoint:0x%x unable to create connection resource error\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 656 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 657 | return create_err_response(endp, error_code, "CRCX", p->trans); |
| 658 | } |
| 659 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 660 | /* MDCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 661 | static struct msgb *handle_modify_con(struct mgcp_parse_data *p) |
| 662 | { |
| 663 | struct mgcp_endpoint *endp = p->endp; |
| 664 | int error_code = 500; |
| 665 | int silent = 0; |
| 666 | int have_sdp = 0; |
| 667 | char *line; |
| 668 | const char *local_options = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 669 | const char *mode = NULL; |
| 670 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 671 | const char *conn_id = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 672 | |
| 673 | LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 674 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 675 | if (llist_count(&endp->conns) <= 0) { |
| 676 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 677 | "MDCX: endpoint:0x%x endpoint is not holding a connection.\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 678 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 679 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 680 | } |
| 681 | |
| 682 | for_each_line(line, p->save) { |
| 683 | if (!mgcp_check_param(endp, line)) |
| 684 | continue; |
| 685 | |
| 686 | switch (line[0]) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 687 | case 'C': |
| 688 | if (mgcp_verify_call_id(endp, line + 3) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 689 | goto error3; |
| 690 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 691 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 692 | conn_id = (const char *)line + 3; |
| 693 | if (mgcp_verify_ci(endp, conn_id) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 694 | goto error3; |
| 695 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 696 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 697 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 698 | break; |
| 699 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 700 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 701 | break; |
| 702 | case 'Z': |
| 703 | silent = strcmp("noanswer", line + 3) == 0; |
| 704 | break; |
| 705 | case '\0': |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 706 | have_sdp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 707 | goto mgcp_header_done; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 708 | break; |
| 709 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 710 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 711 | "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 712 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | } |
| 716 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 717 | mgcp_header_done: |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 718 | if (!conn_id) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 719 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 720 | "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 721 | ENDPOINT_NUMBER(endp)); |
| 722 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 723 | } |
| 724 | |
| 725 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 726 | if (!conn) |
| 727 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 728 | |
| 729 | if (mode) { |
| 730 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 731 | error_code = 517; |
| 732 | goto error3; |
| 733 | } |
| 734 | } else |
| 735 | conn->conn->mode = conn->conn->mode_orig; |
| 736 | |
| 737 | if (have_sdp) |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 738 | mgcp_parse_sdp_data(endp, conn, p); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 739 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 740 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 741 | local_options); |
| 742 | |
| 743 | if (!have_sdp && endp->local_options.codec) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 744 | mgcp_set_audio_info(p->cfg, &conn->end.codec, |
| 745 | PTYPE_UNDEFINED, endp->local_options.codec); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 746 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 747 | if (setup_rtp_processing(endp, conn) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 748 | goto error3; |
| 749 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 750 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 751 | /* policy CB */ |
| 752 | if (p->cfg->policy_cb) { |
| 753 | int rc; |
| 754 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 755 | MGCP_ENDP_MDCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 756 | switch (rc) { |
| 757 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 758 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 759 | "MDCX: endpoint:0x%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 760 | ENDPOINT_NUMBER(endp)); |
| 761 | if (silent) |
| 762 | goto out_silent; |
| 763 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 764 | break; |
| 765 | case MGCP_POLICY_DEFER: |
| 766 | /* stop processing */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 767 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 768 | "MDCX: endpoint:0x%x defered by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 769 | ENDPOINT_NUMBER(endp)); |
| 770 | return NULL; |
| 771 | break; |
| 772 | case MGCP_POLICY_CONT: |
| 773 | /* just continue */ |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 778 | mgcp_rtp_end_config(endp, 1, &conn->end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 779 | |
| 780 | /* modify */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 781 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 782 | "MDCX: endpoint:0x%x modified conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 783 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 784 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 785 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 786 | MGCP_ENDP_MDCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 787 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 788 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 789 | OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 790 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
| 791 | && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 792 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 793 | |
| 794 | if (silent) |
| 795 | goto out_silent; |
| 796 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 797 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 798 | "MDCX: endpoint:0x%x connection successfully modified\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 799 | ENDPOINT_NUMBER(endp)); |
| 800 | return create_response_with_sdp(endp, conn, "MDCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 801 | error3: |
| 802 | return create_err_response(endp, error_code, "MDCX", p->trans); |
| 803 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 804 | out_silent: |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 805 | LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 806 | ENDPOINT_NUMBER(endp)); |
| 807 | return NULL; |
| 808 | } |
| 809 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 810 | /* DLCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 811 | static struct msgb *handle_delete_con(struct mgcp_parse_data *p) |
| 812 | { |
| 813 | struct mgcp_endpoint *endp = p->endp; |
| 814 | int error_code = 400; |
| 815 | int silent = 0; |
| 816 | char *line; |
| 817 | char stats[1048]; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 818 | const char *conn_id = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 819 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 820 | |
| 821 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 822 | "DLCX: endpoint:0x%x deleting connection ...\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 823 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 824 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 825 | if (llist_count(&endp->conns) <= 0) { |
| 826 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 827 | "DLCX: endpoint:0x%x endpoint is not holding a connection.\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 828 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 829 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 830 | } |
| 831 | |
| 832 | for_each_line(line, p->save) { |
| 833 | if (!mgcp_check_param(endp, line)) |
| 834 | continue; |
| 835 | |
| 836 | switch (line[0]) { |
| 837 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 838 | if (mgcp_verify_call_id(endp, line + 3) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 839 | goto error3; |
| 840 | break; |
| 841 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 842 | conn_id = (const char *)line + 3; |
| 843 | if (mgcp_verify_ci(endp, conn_id) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 844 | goto error3; |
| 845 | break; |
| 846 | case 'Z': |
| 847 | silent = strcmp("noanswer", line + 3) == 0; |
| 848 | break; |
| 849 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 850 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 851 | "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 852 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /* policy CB */ |
| 858 | if (p->cfg->policy_cb) { |
| 859 | int rc; |
| 860 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 861 | MGCP_ENDP_DLCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 862 | switch (rc) { |
| 863 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 864 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 865 | "DLCX: endpoint:0x%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 866 | ENDPOINT_NUMBER(endp)); |
| 867 | if (silent) |
| 868 | goto out_silent; |
| 869 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 870 | break; |
| 871 | case MGCP_POLICY_DEFER: |
| 872 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 873 | return NULL; |
| 874 | break; |
| 875 | case MGCP_POLICY_CONT: |
| 876 | /* just continue */ |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 881 | /* When no connection id is supplied, we will interpret this as a |
| 882 | * wildcarded DLCX and drop all connections at once. (See also |
| 883 | * RFC3435 Section F.7) */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 884 | if (!conn_id) { |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 885 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 886 | "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] | 887 | ENDPOINT_NUMBER(endp)); |
| 888 | |
| 889 | mgcp_release_endp(endp); |
| 890 | |
| 891 | /* Note: In this case we do not return any statistics, |
| 892 | * as we assume that the client is not interested in |
| 893 | * this case. */ |
| 894 | return create_ok_response(endp, 200, "DLCX", p->trans); |
| 895 | } |
| 896 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 897 | /* Find the connection */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 898 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 899 | if (!conn) |
| 900 | goto error3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 901 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 902 | /* save the statistics of the current connection */ |
| 903 | mgcp_format_stats(stats, sizeof(stats), conn->conn); |
| 904 | |
| 905 | /* delete connection */ |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 906 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 907 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 908 | mgcp_conn_free(endp, conn_id); |
| 909 | LOGP(DLMGCP, LOGL_NOTICE, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 910 | "DLCX: endpoint:0x%x connection successfully deleted\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 911 | ENDPOINT_NUMBER(endp)); |
| 912 | |
| 913 | /* When all connections are closed, the endpoint will be released |
| 914 | * in order to be ready to be used by another call. */ |
| 915 | if (llist_count(&endp->conns) <= 0) { |
| 916 | mgcp_release_endp(endp); |
| 917 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 918 | "DLCX: endpoint:0x%x endpoint released\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 919 | ENDPOINT_NUMBER(endp)); |
| 920 | } |
| 921 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 922 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 923 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 924 | MGCP_ENDP_DLCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 925 | |
| 926 | if (silent) |
| 927 | goto out_silent; |
| 928 | return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats); |
| 929 | |
| 930 | error3: |
| 931 | return create_err_response(endp, error_code, "DLCX", p->trans); |
| 932 | |
| 933 | out_silent: |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 934 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 935 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 936 | return NULL; |
| 937 | } |
| 938 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 939 | /* RSIP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 940 | static struct msgb *handle_rsip(struct mgcp_parse_data *p) |
| 941 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 942 | /* TODO: Also implement the resetting of a specific endpoint |
| 943 | * to make mgcp_send_reset_ep() work. Currently this will call |
| 944 | * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1 |
| 945 | * to make read_call_agent() reset all endpoints when called |
| 946 | * next time. In order to selectively reset endpoints some |
| 947 | * mechanism to distinguish which endpoint shall be resetted |
| 948 | * is needed */ |
| 949 | |
| 950 | LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n"); |
| 951 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 952 | if (p->cfg->reset_cb) |
| 953 | p->cfg->reset_cb(p->endp->tcfg); |
| 954 | return NULL; |
| 955 | } |
| 956 | |
| 957 | static char extract_tone(const char *line) |
| 958 | { |
| 959 | const char *str = strstr(line, "D/"); |
| 960 | if (!str) |
| 961 | return CHAR_MAX; |
| 962 | |
| 963 | return str[2]; |
| 964 | } |
| 965 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 966 | /* This can request like DTMF detection and forward, fax detection... it |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 967 | * 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] | 968 | * do this right now. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 969 | static struct msgb *handle_noti_req(struct mgcp_parse_data *p) |
| 970 | { |
| 971 | int res = 0; |
| 972 | char *line; |
| 973 | char tone = CHAR_MAX; |
| 974 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 975 | LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n"); |
| 976 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 977 | for_each_line(line, p->save) { |
| 978 | switch (line[0]) { |
| 979 | case 'S': |
| 980 | tone = extract_tone(line); |
| 981 | break; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /* we didn't see a signal request with a tone */ |
| 986 | if (tone == CHAR_MAX) |
| 987 | return create_ok_response(p->endp, 200, "RQNT", p->trans); |
| 988 | |
| 989 | if (p->cfg->rqnt_cb) |
| 990 | res = p->cfg->rqnt_cb(p->endp, tone); |
| 991 | |
| 992 | return res == 0 ? |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 993 | create_ok_response(p->endp, 200, "RQNT", p->trans) : |
| 994 | create_err_response(p->endp, res, "RQNT", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 997 | /* Connection keepalive timer, will take care that dummy packets are send |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 998 | * regularly, so that NAT connections stay open */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 999 | static void mgcp_keepalive_timer_cb(void *_tcfg) |
| 1000 | { |
| 1001 | struct mgcp_trunk_config *tcfg = _tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1002 | struct mgcp_conn *conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1003 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1004 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1005 | LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1006 | tcfg->trunk_nr); |
| 1007 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1008 | /* Do not accept invalid configuration values |
| 1009 | * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and |
| 1010 | * values greater 0 */ |
| 1011 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 1012 | |
| 1013 | /* The dummy packet functionality has been disabled, we will exit |
| 1014 | * immediately, no further timer is scheduled, which means we will no |
| 1015 | * longer send dummy packets even when we did before */ |
| 1016 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER) |
| 1017 | return; |
| 1018 | |
| 1019 | /* In cases where only one dummy packet is sent, we do not need |
| 1020 | * the timer since the functions that handle the CRCX and MDCX are |
| 1021 | * triggering the sending of the dummy packet. So we behave like in |
| 1022 | * the MGCP_KEEPALIVE_NEVER case */ |
| 1023 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1024 | return; |
| 1025 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1026 | /* Send walk over all endpoints and send out dummy packets through |
| 1027 | * every connection present on each endpoint */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1028 | for (i = 1; i < tcfg->number_endpoints; ++i) { |
| 1029 | struct mgcp_endpoint *endp = &tcfg->endpoints[i]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1030 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 1031 | if (conn->mode == MGCP_CONN_RECV_ONLY) |
| 1032 | send_dummy(endp, &conn->u.rtp); |
| 1033 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1036 | /* Schedule the keepalive timer for the next round */ |
| 1037 | LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1038 | tcfg->trunk_nr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1039 | osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, |
| 1040 | 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval) |
| 1044 | { |
| 1045 | tcfg->keepalive_interval = interval; |
| 1046 | osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg); |
| 1047 | |
| 1048 | if (interval <= 0) |
| 1049 | osmo_timer_del(&tcfg->keepalive_timer); |
| 1050 | else |
| 1051 | osmo_timer_schedule(&tcfg->keepalive_timer, |
| 1052 | tcfg->keepalive_interval, 0); |
| 1053 | } |
| 1054 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1055 | /*! allocate configuration with default values. |
| 1056 | * (called once at startup by main function) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1057 | struct mgcp_config *mgcp_config_alloc(void) |
| 1058 | { |
| 1059 | struct mgcp_config *cfg; |
| 1060 | |
| 1061 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 1062 | if (!cfg) { |
| 1063 | LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 1064 | return NULL; |
| 1065 | } |
| 1066 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1067 | cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START; |
| 1068 | cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END; |
| 1069 | cfg->net_ports.last_port = cfg->net_ports.range_start; |
| 1070 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1071 | cfg->source_port = 2427; |
| 1072 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1073 | cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1074 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1075 | cfg->rtp_processing_cb = &mgcp_rtp_processing_default; |
| 1076 | cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default; |
| 1077 | |
| 1078 | cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default; |
| 1079 | |
| 1080 | /* default trunk handling */ |
| 1081 | cfg->trunk.cfg = cfg; |
| 1082 | cfg->trunk.trunk_nr = 0; |
| 1083 | cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL; |
| 1084 | cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1085 | cfg->trunk.audio_payload = 126; |
| 1086 | cfg->trunk.audio_send_ptime = 1; |
| 1087 | cfg->trunk.audio_send_name = 1; |
| 1088 | cfg->trunk.omit_rtcp = 0; |
| 1089 | mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE); |
| 1090 | |
| 1091 | INIT_LLIST_HEAD(&cfg->trunks); |
| 1092 | |
| 1093 | return cfg; |
| 1094 | } |
| 1095 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1096 | /*! allocate configuration with default values. |
| 1097 | * (called once at startup by VTY) |
| 1098 | * \param[in] cfg mgcp configuration |
| 1099 | * \param[in] nr trunk number |
| 1100 | * \returns pointer to allocated trunk configuration */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1101 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr) |
| 1102 | { |
| 1103 | struct mgcp_trunk_config *trunk; |
| 1104 | |
| 1105 | trunk = talloc_zero(cfg, struct mgcp_trunk_config); |
| 1106 | if (!trunk) { |
| 1107 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n"); |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | |
| 1111 | trunk->cfg = cfg; |
| 1112 | trunk->trunk_type = MGCP_TRUNK_E1; |
| 1113 | trunk->trunk_nr = nr; |
| 1114 | trunk->audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1115 | trunk->audio_payload = 126; |
| 1116 | trunk->audio_send_ptime = 1; |
| 1117 | trunk->audio_send_name = 1; |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1118 | trunk->vty_number_endpoints = 33; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1119 | trunk->omit_rtcp = 0; |
| 1120 | mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE); |
| 1121 | llist_add_tail(&trunk->entry, &cfg->trunks); |
| 1122 | return trunk; |
| 1123 | } |
| 1124 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1125 | /*! get trunk configuration by trunk number (index). |
| 1126 | * \param[in] cfg mgcp configuration |
| 1127 | * \param[in] index trunk number |
| 1128 | * \returns pointer to trunk configuration, NULL on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1129 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index) |
| 1130 | { |
| 1131 | struct mgcp_trunk_config *trunk; |
| 1132 | |
| 1133 | llist_for_each_entry(trunk, &cfg->trunks, entry) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1134 | if (trunk->trunk_nr == index) |
| 1135 | return trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1136 | |
| 1137 | return NULL; |
| 1138 | } |
| 1139 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1140 | /*! allocate endpoints and set default values. |
| 1141 | * (called once at startup by VTY) |
| 1142 | * \param[in] tcfg trunk configuration |
| 1143 | * \returns 0 on success, -1 on failure */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1144 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg) |
| 1145 | { |
| 1146 | int i; |
| 1147 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1148 | tcfg->endpoints = _talloc_zero_array(tcfg->cfg, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1149 | sizeof(struct mgcp_endpoint), |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1150 | tcfg->vty_number_endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1151 | "endpoints"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1152 | if (!tcfg->endpoints) |
| 1153 | return -1; |
| 1154 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1155 | for (i = 0; i < tcfg->vty_number_endpoints; ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1156 | INIT_LLIST_HEAD(&tcfg->endpoints[i].conns); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1157 | tcfg->endpoints[i].cfg = tcfg->cfg; |
| 1158 | tcfg->endpoints[i].tcfg = tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1159 | |
| 1160 | /* NOTE: Currently all endpoints are of type RTP, this will |
| 1161 | * change when new variations are implemented */ |
| 1162 | tcfg->endpoints[i].type = &ep_typeset.rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1165 | tcfg->number_endpoints = tcfg->vty_number_endpoints; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 1169 | /*! release endpoint, all open connections are closed. |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1170 | * \param[in] endp endpoint to release */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1171 | void mgcp_release_endp(struct mgcp_endpoint *endp) |
| 1172 | { |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 1173 | LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:0x%x\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1174 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1175 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 1176 | /* Normally this function should only be called when |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1177 | * all connections have been removed already. In case |
| 1178 | * that there are still connections open (e.g. when |
| 1179 | * RSIP is executed), free them all at once. */ |
| 1180 | mgcp_conn_free_all(endp); |
| 1181 | |
| 1182 | /* Reset endpoint parameters and states */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1183 | talloc_free(endp->callid); |
| 1184 | endp->callid = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1185 | talloc_free(endp->local_options.string); |
| 1186 | endp->local_options.string = NULL; |
| 1187 | talloc_free(endp->local_options.codec); |
| 1188 | endp->local_options.codec = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | static int send_agent(struct mgcp_config *cfg, const char *buf, int len) |
| 1192 | { |
| 1193 | return write(cfg->gw_fd.bfd.fd, buf, len); |
| 1194 | } |
| 1195 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1196 | /*! Reset all endpoints by sending RSIP message to self. |
| 1197 | * (called by VTY) |
| 1198 | * \param[in] endp trunk endpoint |
| 1199 | * \param[in] endpoint number |
| 1200 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1201 | int mgcp_send_reset_all(struct mgcp_config *cfg) |
| 1202 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1203 | int rc; |
| 1204 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1205 | static const char mgcp_reset[] = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1206 | "RSIP 1 *@mgw MGCP 1.0\r\n" |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1207 | }; |
| 1208 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1209 | rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1); |
| 1210 | if (rc <= 0) |
| 1211 | return -1; |
| 1212 | |
| 1213 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1216 | /*! Reset a single endpoint by sending RSIP message to self. |
| 1217 | * (called by VTY) |
| 1218 | * \param[in] endp trunk endpoint |
| 1219 | * \param[in] endpoint number |
| 1220 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1221 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint) |
| 1222 | { |
| 1223 | char buf[128]; |
| 1224 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1225 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1226 | |
| 1227 | len = snprintf(buf, sizeof(buf), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1228 | "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1229 | if (len < 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1230 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1231 | |
| 1232 | buf[sizeof(buf) - 1] = '\0'; |
| 1233 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1234 | rc = send_agent(endp->cfg, buf, len); |
| 1235 | if (rc <= 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1236 | return -1; |
| 1237 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1238 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1239 | } |