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