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 | |
| 290 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 291 | if (strncmp |
| 292 | (mgcp_requests[i].name, (const char *)&msg->l2h[0], |
| 293 | 4) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 294 | handled = 1; |
| 295 | resp = mgcp_requests[i].handle_request(&pdata); |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (!handled) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 301 | LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", |
| 302 | &msg->l2h[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 303 | |
| 304 | return resp; |
| 305 | } |
| 306 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 307 | /* AUEP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 308 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p) |
| 309 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 310 | LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n"); |
| 311 | |
| 312 | if (p->found != 0) { |
| 313 | LOGP(DLMGCP, LOGL_ERROR, |
| 314 | "AUEP: failed to find the endpoint.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 315 | return create_err_response(NULL, 500, "AUEP", p->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 316 | } else |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 317 | return create_ok_response(p->endp, 200, "AUEP", p->trans); |
| 318 | } |
| 319 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 320 | /* Try to find a free port by attemting to bind on it. Also handle the |
| 321 | * counter that points on the next free port. Since we have a pointer |
| 322 | * to the next free port, binding should work on the first attemt, |
| 323 | * neverless, try at least the next 200 ports before giving up */ |
| 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). |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 359 | * The string is stored in the 'string' field. A NULL string is handled excatlyy |
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 | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 450 | const char *conn_id = 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 | |
| 455 | if (p->found != 0) |
| 456 | return create_err_response(NULL, 510, "CRCX", p->trans); |
| 457 | |
| 458 | /* parse CallID C: and LocalParameters L: */ |
| 459 | for_each_line(line, p->save) { |
| 460 | if (!mgcp_check_param(endp, line)) |
| 461 | continue; |
| 462 | |
| 463 | switch (line[0]) { |
| 464 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 465 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 466 | break; |
| 467 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 468 | callid = (const char *)line + 3; |
| 469 | break; |
| 470 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 471 | conn_id = (const char *)line + 3; |
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 | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 513 | if (!conn_id) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 514 | LOGP(DLMGCP, LOGL_ERROR, |
| 515 | "CRCX: endpoint:%x insufficient parameters, missing connection id\n", |
| 516 | ENDPOINT_NUMBER(endp)); |
| 517 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 518 | } |
| 519 | |
| 520 | /* Check if we are able to accept the creation of another connection */ |
| 521 | if (llist_count(&endp->conns) >= endp->type->max_conns) { |
| 522 | LOGP(DLMGCP, LOGL_ERROR, |
| 523 | "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n", |
| 524 | endp->type->max_conns, ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 525 | if (tcfg->force_realloc) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 526 | /* There is no more room for a connection, make some |
| 527 | * room by blindly tossing the oldest of the two two |
| 528 | * connections */ |
| 529 | mgcp_conn_free_oldest(endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 530 | } else { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 531 | /* There is no more room for a connection, leave |
| 532 | * everything as it is and return with an error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 533 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 534 | } |
| 535 | } |
| 536 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 537 | /* Check if this endpoint already serves a call, if so, check if the |
| 538 | * callids match up so that we are sure that this is our call */ |
| 539 | if (endp->callid && mgcp_verify_call_id(endp, callid)) { |
| 540 | LOGP(DLMGCP, LOGL_ERROR, |
| 541 | "CRCX: endpoint:%x allready seized by other call (%s)\n", |
| 542 | ENDPOINT_NUMBER(endp), endp->callid); |
| 543 | if (tcfg->force_realloc) |
| 544 | /* This is not our call, toss everything by releasing |
| 545 | * the entire endpoint. (rude!) */ |
| 546 | mgcp_release_endp(endp); |
| 547 | else { |
| 548 | /* This is not our call, leave everything as it is and |
| 549 | * return with an error. */ |
| 550 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* Set the callid, creation of another connection will only be possible |
| 555 | * when the callid matches up. (Connections are distinuished by their |
| 556 | * connection ids) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 557 | endp->callid = talloc_strdup(tcfg->endpoints, callid); |
| 558 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 559 | /* Extract audio codec information */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 560 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 561 | local_options); |
| 562 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 563 | /* Only accept another connection when the connection ID is different. */ |
| 564 | if (mgcp_conn_get_rtp(endp, conn_id)) { |
| 565 | LOGP(DLMGCP, LOGL_ERROR, |
| 566 | "CRCX: endpoint:%x there is already a connection with id %u present!\n", |
| 567 | conn_id, ENDPOINT_NUMBER(endp)); |
| 568 | if (tcfg->force_realloc) { |
| 569 | /* Ignore the existing connection by just freeing it */ |
| 570 | mgcp_conn_free(endp, conn_id); |
| 571 | } else { |
| 572 | /* There is already a connection with that ID present, |
| 573 | * leave everything as it is and return with an error. */ |
| 574 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 575 | } |
| 576 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 577 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 578 | snprintf(conn_name, sizeof(conn_name), "%s-%s", callid, conn_id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 579 | mgcp_conn_alloc(NULL, endp, conn_id, MGCP_CONN_TYPE_RTP, |
| 580 | conn_name); |
| 581 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 582 | if (!conn) { |
| 583 | LOGP(DLMGCP, LOGL_ERROR, |
| 584 | "CRCX: endpoint:%x unable to allocate RTP connection\n", |
| 585 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 586 | goto error2; |
| 587 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 591 | error_code = 517; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 592 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 595 | /* Annotate Osmux circuit ID and set it to negotiating state until this |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 596 | * is fully set up from the dummy load. */ |
| 597 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 598 | if (osmux_cid >= 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 599 | conn->osmux.cid = osmux_cid; |
| 600 | conn->osmux.state = OSMUX_STATE_NEGOTIATING; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 601 | } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) { |
| 602 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 603 | "CRCX: endpoint:%x osmux only and no osmux offered\n", |
| 604 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 605 | goto error2; |
| 606 | } |
| 607 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 608 | /* set up RTP media parameters */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 609 | if (have_sdp) |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 610 | mgcp_parse_sdp_data(endp, conn, p); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 611 | else if (endp->local_options.codec) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 612 | mgcp_set_audio_info(p->cfg, &conn->end.codec, |
| 613 | PTYPE_UNDEFINED, endp->local_options.codec); |
| 614 | conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints, |
| 615 | tcfg->audio_fmtp_extra); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 616 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 617 | if (p->cfg->force_ptime) { |
| 618 | conn->end.packet_duration_ms = p->cfg->force_ptime; |
| 619 | conn->end.force_output_ptime = 1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 622 | mgcp_rtp_end_config(endp, 0, &conn->end); |
| 623 | |
| 624 | if (allocate_port(endp, conn) != 0) { |
| 625 | goto error2; |
| 626 | } |
| 627 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 628 | if (setup_rtp_processing(endp, conn) != 0) { |
| 629 | LOGP(DLMGCP, LOGL_ERROR, |
| 630 | "CRCX: endpoint:%x could not start RTP processing!\n", |
| 631 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 632 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 633 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 634 | |
| 635 | /* policy CB */ |
| 636 | if (p->cfg->policy_cb) { |
| 637 | int rc; |
| 638 | rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 639 | MGCP_ENDP_CRCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 640 | switch (rc) { |
| 641 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 642 | LOGP(DLMGCP, LOGL_NOTICE, |
| 643 | "CRCX: endpoint:%x CRCX rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 644 | ENDPOINT_NUMBER(endp)); |
| 645 | mgcp_release_endp(endp); |
| 646 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 647 | break; |
| 648 | case MGCP_POLICY_DEFER: |
| 649 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 650 | return NULL; |
| 651 | break; |
| 652 | case MGCP_POLICY_CONT: |
| 653 | /* just continue */ |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 658 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 659 | "CRCX: endpoint:%x Creating connection: CI: %s port: %u\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 660 | ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 661 | if (p->cfg->change_cb) |
| 662 | p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); |
| 663 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 664 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 665 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 666 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 667 | && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 668 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 669 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 670 | LOGP(DLMGCP, LOGL_NOTICE, |
| 671 | "CRCX: endpoint:%x connection successfully created\n", |
| 672 | ENDPOINT_NUMBER(endp)); |
| 673 | return create_response_with_sdp(endp, conn, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 674 | error2: |
| 675 | mgcp_release_endp(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 676 | LOGP(DLMGCP, LOGL_NOTICE, |
| 677 | "CRCX: endpoint:%x unable to create connection resource error\n", |
| 678 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 679 | return create_err_response(endp, error_code, "CRCX", p->trans); |
| 680 | } |
| 681 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 682 | /* MDCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 683 | static struct msgb *handle_modify_con(struct mgcp_parse_data *p) |
| 684 | { |
| 685 | struct mgcp_endpoint *endp = p->endp; |
| 686 | int error_code = 500; |
| 687 | int silent = 0; |
| 688 | int have_sdp = 0; |
| 689 | char *line; |
| 690 | const char *local_options = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 691 | const char *mode = NULL; |
| 692 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 693 | const char *conn_id = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 694 | |
| 695 | LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 696 | |
| 697 | if (p->found != 0) |
| 698 | return create_err_response(NULL, 510, "MDCX", p->trans); |
| 699 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 700 | if (llist_count(&endp->conns) <= 0) { |
| 701 | LOGP(DLMGCP, LOGL_ERROR, |
| 702 | "MDCX: endpoint:%x endpoint is not holding a connection.\n", |
| 703 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 704 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 705 | } |
| 706 | |
| 707 | for_each_line(line, p->save) { |
| 708 | if (!mgcp_check_param(endp, line)) |
| 709 | continue; |
| 710 | |
| 711 | switch (line[0]) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 712 | case 'C': |
| 713 | if (mgcp_verify_call_id(endp, line + 3) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 714 | goto error3; |
| 715 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 716 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 717 | conn_id = (const char *)line + 3; |
| 718 | if (mgcp_verify_ci(endp, conn_id) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 719 | goto error3; |
| 720 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 721 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 722 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 723 | break; |
| 724 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 725 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 726 | break; |
| 727 | case 'Z': |
| 728 | silent = strcmp("noanswer", line + 3) == 0; |
| 729 | break; |
| 730 | case '\0': |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 731 | have_sdp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 732 | goto mgcp_header_done; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 733 | break; |
| 734 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 735 | LOGP(DLMGCP, LOGL_NOTICE, |
| 736 | "MDCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n", |
| 737 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 738 | break; |
| 739 | } |
| 740 | } |
| 741 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 742 | mgcp_header_done: |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 743 | if (!conn_id) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 744 | LOGP(DLMGCP, LOGL_ERROR, |
| 745 | "MDCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n", |
| 746 | ENDPOINT_NUMBER(endp)); |
| 747 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 748 | } |
| 749 | |
| 750 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 751 | if (!conn) |
| 752 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 753 | |
| 754 | if (mode) { |
| 755 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 756 | error_code = 517; |
| 757 | goto error3; |
| 758 | } |
| 759 | } else |
| 760 | conn->conn->mode = conn->conn->mode_orig; |
| 761 | |
| 762 | if (have_sdp) |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 763 | mgcp_parse_sdp_data(endp, conn, p); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 764 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 765 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 766 | local_options); |
| 767 | |
| 768 | if (!have_sdp && endp->local_options.codec) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 769 | mgcp_set_audio_info(p->cfg, &conn->end.codec, |
| 770 | PTYPE_UNDEFINED, endp->local_options.codec); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 771 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 772 | if (setup_rtp_processing(endp, conn) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 773 | goto error3; |
| 774 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 775 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 776 | /* policy CB */ |
| 777 | if (p->cfg->policy_cb) { |
| 778 | int rc; |
| 779 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 780 | MGCP_ENDP_MDCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 781 | switch (rc) { |
| 782 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 783 | LOGP(DLMGCP, LOGL_NOTICE, |
| 784 | "MDCX: endpoint:%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 785 | ENDPOINT_NUMBER(endp)); |
| 786 | if (silent) |
| 787 | goto out_silent; |
| 788 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 789 | break; |
| 790 | case MGCP_POLICY_DEFER: |
| 791 | /* stop processing */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 792 | LOGP(DLMGCP, LOGL_DEBUG, |
| 793 | "MDCX: endpoint:%x defered by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 794 | ENDPOINT_NUMBER(endp)); |
| 795 | return NULL; |
| 796 | break; |
| 797 | case MGCP_POLICY_CONT: |
| 798 | /* just continue */ |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 803 | mgcp_rtp_end_config(endp, 1, &conn->end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 804 | |
| 805 | /* modify */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 806 | LOGP(DLMGCP, LOGL_DEBUG, |
| 807 | "MDCX: endpoint:%x modified conn:%s\n", |
| 808 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 809 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 810 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 811 | MGCP_ENDP_MDCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 812 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 813 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 814 | OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 815 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
| 816 | && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 817 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 818 | |
| 819 | if (silent) |
| 820 | goto out_silent; |
| 821 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 822 | LOGP(DLMGCP, LOGL_NOTICE, |
| 823 | "MDCX: endpoint:%x connection successfully modified\n", |
| 824 | ENDPOINT_NUMBER(endp)); |
| 825 | return create_response_with_sdp(endp, conn, "MDCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 826 | error3: |
| 827 | return create_err_response(endp, error_code, "MDCX", p->trans); |
| 828 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 829 | out_silent: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 830 | LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:%x silent exit\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 831 | ENDPOINT_NUMBER(endp)); |
| 832 | return NULL; |
| 833 | } |
| 834 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 835 | /* DLCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 836 | static struct msgb *handle_delete_con(struct mgcp_parse_data *p) |
| 837 | { |
| 838 | struct mgcp_endpoint *endp = p->endp; |
| 839 | int error_code = 400; |
| 840 | int silent = 0; |
| 841 | char *line; |
| 842 | char stats[1048]; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 843 | const char *conn_id = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 844 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 845 | |
Neels Hofmeyr | 653c4ff | 2017-11-10 01:41:45 +0100 | [diff] [blame] | 846 | if (p->found != 0) |
| 847 | return create_err_response(NULL, error_code, "DLCX", p->trans); |
| 848 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 849 | LOGP(DLMGCP, LOGL_NOTICE, |
| 850 | "DLCX: endpoint:%x deleting connection ...\n", |
| 851 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 852 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 853 | if (llist_count(&endp->conns) <= 0) { |
| 854 | LOGP(DLMGCP, LOGL_ERROR, |
| 855 | "DLCX: endpoint:%x endpoint is not holding a connection.\n", |
| 856 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 857 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 858 | } |
| 859 | |
| 860 | for_each_line(line, p->save) { |
| 861 | if (!mgcp_check_param(endp, line)) |
| 862 | continue; |
| 863 | |
| 864 | switch (line[0]) { |
| 865 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 866 | if (mgcp_verify_call_id(endp, line + 3) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 867 | goto error3; |
| 868 | break; |
| 869 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 870 | conn_id = (const char *)line + 3; |
| 871 | if (mgcp_verify_ci(endp, conn_id) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 872 | goto error3; |
| 873 | break; |
| 874 | case 'Z': |
| 875 | silent = strcmp("noanswer", line + 3) == 0; |
| 876 | break; |
| 877 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 878 | LOGP(DLMGCP, LOGL_NOTICE, |
| 879 | "DLCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n", |
| 880 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 881 | break; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /* policy CB */ |
| 886 | if (p->cfg->policy_cb) { |
| 887 | int rc; |
| 888 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 889 | MGCP_ENDP_DLCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 890 | switch (rc) { |
| 891 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 892 | LOGP(DLMGCP, LOGL_NOTICE, |
| 893 | "DLCX: endpoint:%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 894 | ENDPOINT_NUMBER(endp)); |
| 895 | if (silent) |
| 896 | goto out_silent; |
| 897 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 898 | break; |
| 899 | case MGCP_POLICY_DEFER: |
| 900 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 901 | return NULL; |
| 902 | break; |
| 903 | case MGCP_POLICY_CONT: |
| 904 | /* just continue */ |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 909 | /* When no connection id is supplied, we will interpret this as a |
| 910 | * wildcarded DLCX and drop all connections at once. (See also |
| 911 | * RFC3435 Section F.7) */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 912 | if (!conn_id) { |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 913 | LOGP(DLMGCP, LOGL_NOTICE, |
| 914 | "DLCX: endpoint:%x missing ci (connectionIdentifier), will remove all connections at once\n", |
| 915 | ENDPOINT_NUMBER(endp)); |
| 916 | |
| 917 | mgcp_release_endp(endp); |
| 918 | |
| 919 | /* Note: In this case we do not return any statistics, |
| 920 | * as we assume that the client is not interested in |
| 921 | * this case. */ |
| 922 | return create_ok_response(endp, 200, "DLCX", p->trans); |
| 923 | } |
| 924 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 925 | /* Find the connection */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 926 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 927 | if (!conn) |
| 928 | goto error3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 929 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 930 | /* save the statistics of the current connection */ |
| 931 | mgcp_format_stats(stats, sizeof(stats), conn->conn); |
| 932 | |
| 933 | /* delete connection */ |
| 934 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x deleting conn:%s\n", |
| 935 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 936 | mgcp_conn_free(endp, conn_id); |
| 937 | LOGP(DLMGCP, LOGL_NOTICE, |
| 938 | "DLCX: endpoint:%x connection successfully deleted\n", |
| 939 | ENDPOINT_NUMBER(endp)); |
| 940 | |
| 941 | /* When all connections are closed, the endpoint will be released |
| 942 | * in order to be ready to be used by another call. */ |
| 943 | if (llist_count(&endp->conns) <= 0) { |
| 944 | mgcp_release_endp(endp); |
| 945 | LOGP(DLMGCP, LOGL_DEBUG, |
| 946 | "DLCX: endpoint:%x endpoint released\n", |
| 947 | ENDPOINT_NUMBER(endp)); |
| 948 | } |
| 949 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 950 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 951 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 952 | MGCP_ENDP_DLCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 953 | |
| 954 | if (silent) |
| 955 | goto out_silent; |
| 956 | return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats); |
| 957 | |
| 958 | error3: |
| 959 | return create_err_response(endp, error_code, "DLCX", p->trans); |
| 960 | |
| 961 | out_silent: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 962 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x silent exit\n", |
| 963 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 964 | return NULL; |
| 965 | } |
| 966 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 967 | /* RSIP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 968 | static struct msgb *handle_rsip(struct mgcp_parse_data *p) |
| 969 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 970 | /* TODO: Also implement the resetting of a specific endpoint |
| 971 | * to make mgcp_send_reset_ep() work. Currently this will call |
| 972 | * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1 |
| 973 | * to make read_call_agent() reset all endpoints when called |
| 974 | * next time. In order to selectively reset endpoints some |
| 975 | * mechanism to distinguish which endpoint shall be resetted |
| 976 | * is needed */ |
| 977 | |
| 978 | LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n"); |
| 979 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 980 | if (p->found != 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 981 | LOGP(DLMGCP, LOGL_ERROR, |
| 982 | "RSIP: failed to find the endpoint.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 983 | return NULL; |
| 984 | } |
| 985 | |
| 986 | if (p->cfg->reset_cb) |
| 987 | p->cfg->reset_cb(p->endp->tcfg); |
| 988 | return NULL; |
| 989 | } |
| 990 | |
| 991 | static char extract_tone(const char *line) |
| 992 | { |
| 993 | const char *str = strstr(line, "D/"); |
| 994 | if (!str) |
| 995 | return CHAR_MAX; |
| 996 | |
| 997 | return str[2]; |
| 998 | } |
| 999 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1000 | /* This can request like DTMF detection and forward, fax detection... it |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1001 | * 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] | 1002 | * do this right now. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1003 | static struct msgb *handle_noti_req(struct mgcp_parse_data *p) |
| 1004 | { |
| 1005 | int res = 0; |
| 1006 | char *line; |
| 1007 | char tone = CHAR_MAX; |
| 1008 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1009 | LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n"); |
| 1010 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1011 | if (p->found != 0) |
| 1012 | return create_err_response(NULL, 400, "RQNT", p->trans); |
| 1013 | |
| 1014 | for_each_line(line, p->save) { |
| 1015 | switch (line[0]) { |
| 1016 | case 'S': |
| 1017 | tone = extract_tone(line); |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /* we didn't see a signal request with a tone */ |
| 1023 | if (tone == CHAR_MAX) |
| 1024 | return create_ok_response(p->endp, 200, "RQNT", p->trans); |
| 1025 | |
| 1026 | if (p->cfg->rqnt_cb) |
| 1027 | res = p->cfg->rqnt_cb(p->endp, tone); |
| 1028 | |
| 1029 | return res == 0 ? |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1030 | create_ok_response(p->endp, 200, "RQNT", p->trans) : |
| 1031 | create_err_response(p->endp, res, "RQNT", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1034 | /* Connection keepalive timer, will take care that dummy packets are send |
| 1035 | * regulary, so that NAT connections stay open */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1036 | static void mgcp_keepalive_timer_cb(void *_tcfg) |
| 1037 | { |
| 1038 | struct mgcp_trunk_config *tcfg = _tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1039 | struct mgcp_conn *conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1040 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1041 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1042 | LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1043 | tcfg->trunk_nr); |
| 1044 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1045 | /* Do not accept invalid configuration values |
| 1046 | * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and |
| 1047 | * values greater 0 */ |
| 1048 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 1049 | |
| 1050 | /* The dummy packet functionality has been disabled, we will exit |
| 1051 | * immediately, no further timer is scheduled, which means we will no |
| 1052 | * longer send dummy packets even when we did before */ |
| 1053 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER) |
| 1054 | return; |
| 1055 | |
| 1056 | /* In cases where only one dummy packet is sent, we do not need |
| 1057 | * the timer since the functions that handle the CRCX and MDCX are |
| 1058 | * triggering the sending of the dummy packet. So we behave like in |
| 1059 | * the MGCP_KEEPALIVE_NEVER case */ |
| 1060 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1061 | return; |
| 1062 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1063 | /* Send walk over all endpoints and send out dummy packets through |
| 1064 | * every connection present on each endpoint */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1065 | for (i = 1; i < tcfg->number_endpoints; ++i) { |
| 1066 | struct mgcp_endpoint *endp = &tcfg->endpoints[i]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1067 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 1068 | if (conn->mode == MGCP_CONN_RECV_ONLY) |
| 1069 | send_dummy(endp, &conn->u.rtp); |
| 1070 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1073 | /* Schedule the keepalive timer for the next round */ |
| 1074 | LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1075 | tcfg->trunk_nr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1076 | osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, |
| 1077 | 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval) |
| 1081 | { |
| 1082 | tcfg->keepalive_interval = interval; |
| 1083 | osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg); |
| 1084 | |
| 1085 | if (interval <= 0) |
| 1086 | osmo_timer_del(&tcfg->keepalive_timer); |
| 1087 | else |
| 1088 | osmo_timer_schedule(&tcfg->keepalive_timer, |
| 1089 | tcfg->keepalive_interval, 0); |
| 1090 | } |
| 1091 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1092 | /*! allocate configuration with default values. |
| 1093 | * (called once at startup by main function) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1094 | struct mgcp_config *mgcp_config_alloc(void) |
| 1095 | { |
| 1096 | struct mgcp_config *cfg; |
| 1097 | |
| 1098 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 1099 | if (!cfg) { |
| 1100 | LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1104 | cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START; |
| 1105 | cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END; |
| 1106 | cfg->net_ports.last_port = cfg->net_ports.range_start; |
| 1107 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1108 | cfg->source_port = 2427; |
| 1109 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1110 | cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1111 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1112 | cfg->rtp_processing_cb = &mgcp_rtp_processing_default; |
| 1113 | cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default; |
| 1114 | |
| 1115 | cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default; |
| 1116 | |
| 1117 | /* default trunk handling */ |
| 1118 | cfg->trunk.cfg = cfg; |
| 1119 | cfg->trunk.trunk_nr = 0; |
| 1120 | cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL; |
| 1121 | cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1122 | cfg->trunk.audio_payload = 126; |
| 1123 | cfg->trunk.audio_send_ptime = 1; |
| 1124 | cfg->trunk.audio_send_name = 1; |
| 1125 | cfg->trunk.omit_rtcp = 0; |
| 1126 | mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE); |
| 1127 | |
| 1128 | INIT_LLIST_HEAD(&cfg->trunks); |
| 1129 | |
| 1130 | return cfg; |
| 1131 | } |
| 1132 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1133 | /*! allocate configuration with default values. |
| 1134 | * (called once at startup by VTY) |
| 1135 | * \param[in] cfg mgcp configuration |
| 1136 | * \param[in] nr trunk number |
| 1137 | * \returns pointer to allocated trunk configuration */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1138 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr) |
| 1139 | { |
| 1140 | struct mgcp_trunk_config *trunk; |
| 1141 | |
| 1142 | trunk = talloc_zero(cfg, struct mgcp_trunk_config); |
| 1143 | if (!trunk) { |
| 1144 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n"); |
| 1145 | return NULL; |
| 1146 | } |
| 1147 | |
| 1148 | trunk->cfg = cfg; |
| 1149 | trunk->trunk_type = MGCP_TRUNK_E1; |
| 1150 | trunk->trunk_nr = nr; |
| 1151 | trunk->audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1152 | trunk->audio_payload = 126; |
| 1153 | trunk->audio_send_ptime = 1; |
| 1154 | trunk->audio_send_name = 1; |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1155 | trunk->vty_number_endpoints = 33; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1156 | trunk->omit_rtcp = 0; |
| 1157 | mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE); |
| 1158 | llist_add_tail(&trunk->entry, &cfg->trunks); |
| 1159 | return trunk; |
| 1160 | } |
| 1161 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1162 | /*! get trunk configuration by trunk number (index). |
| 1163 | * \param[in] cfg mgcp configuration |
| 1164 | * \param[in] index trunk number |
| 1165 | * \returns pointer to trunk configuration, NULL on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1166 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index) |
| 1167 | { |
| 1168 | struct mgcp_trunk_config *trunk; |
| 1169 | |
| 1170 | llist_for_each_entry(trunk, &cfg->trunks, entry) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1171 | if (trunk->trunk_nr == index) |
| 1172 | return trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1173 | |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1177 | /*! allocate endpoints and set default values. |
| 1178 | * (called once at startup by VTY) |
| 1179 | * \param[in] tcfg trunk configuration |
| 1180 | * \returns 0 on success, -1 on failure */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1181 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg) |
| 1182 | { |
| 1183 | int i; |
| 1184 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1185 | tcfg->endpoints = _talloc_zero_array(tcfg->cfg, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1186 | sizeof(struct mgcp_endpoint), |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1187 | tcfg->vty_number_endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1188 | "endpoints"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1189 | if (!tcfg->endpoints) |
| 1190 | return -1; |
| 1191 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1192 | for (i = 0; i < tcfg->vty_number_endpoints; ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1193 | INIT_LLIST_HEAD(&tcfg->endpoints[i].conns); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1194 | tcfg->endpoints[i].cfg = tcfg->cfg; |
| 1195 | tcfg->endpoints[i].tcfg = tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1196 | |
| 1197 | /* NOTE: Currently all endpoints are of type RTP, this will |
| 1198 | * change when new variations are implemented */ |
| 1199 | tcfg->endpoints[i].type = &ep_typeset.rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1202 | tcfg->number_endpoints = tcfg->vty_number_endpoints; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1203 | return 0; |
| 1204 | } |
| 1205 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1206 | /*! relase endpoint, all open connections are closed. |
| 1207 | * \param[in] endp endpoint to release */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1208 | void mgcp_release_endp(struct mgcp_endpoint *endp) |
| 1209 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1210 | LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:%x\n", |
| 1211 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1212 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1213 | /* Normally this function should only be called wehen |
| 1214 | * all connections have been removed already. In case |
| 1215 | * that there are still connections open (e.g. when |
| 1216 | * RSIP is executed), free them all at once. */ |
| 1217 | mgcp_conn_free_all(endp); |
| 1218 | |
| 1219 | /* Reset endpoint parameters and states */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1220 | talloc_free(endp->callid); |
| 1221 | endp->callid = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1222 | talloc_free(endp->local_options.string); |
| 1223 | endp->local_options.string = NULL; |
| 1224 | talloc_free(endp->local_options.codec); |
| 1225 | endp->local_options.codec = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static int send_agent(struct mgcp_config *cfg, const char *buf, int len) |
| 1229 | { |
| 1230 | return write(cfg->gw_fd.bfd.fd, buf, len); |
| 1231 | } |
| 1232 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1233 | /*! Reset all endpoints by sending RSIP message to self. |
| 1234 | * (called by VTY) |
| 1235 | * \param[in] endp trunk endpoint |
| 1236 | * \param[in] endpoint number |
| 1237 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1238 | int mgcp_send_reset_all(struct mgcp_config *cfg) |
| 1239 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1240 | int rc; |
| 1241 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1242 | static const char mgcp_reset[] = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1243 | "RSIP 1 *@mgw MGCP 1.0\r\n" |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1244 | }; |
| 1245 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1246 | rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1); |
| 1247 | if (rc <= 0) |
| 1248 | return -1; |
| 1249 | |
| 1250 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1253 | /*! Reset a single endpoint by sending RSIP message to self. |
| 1254 | * (called by VTY) |
| 1255 | * \param[in] endp trunk endpoint |
| 1256 | * \param[in] endpoint number |
| 1257 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1258 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint) |
| 1259 | { |
| 1260 | char buf[128]; |
| 1261 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1262 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1263 | |
| 1264 | len = snprintf(buf, sizeof(buf), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1265 | "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1266 | if (len < 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1267 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1268 | |
| 1269 | buf[sizeof(buf) - 1] = '\0'; |
| 1270 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1271 | rc = send_agent(endp->cfg, buf, len); |
| 1272 | if (rc <= 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1273 | return -1; |
| 1274 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1275 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1276 | } |