2010-07-27 19:32:52 +00:00
|
|
|
/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
|
|
|
|
/* The protocol implementation */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* (C) 2009-2010 by On-Waves
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-07-30 21:25:35 +00:00
|
|
|
#include <cellmgr_debug.h>
|
|
|
|
|
2010-07-27 19:32:52 +00:00
|
|
|
#include <mgcp/mgcp.h>
|
|
|
|
#include <mgcp/mgcp_internal.h>
|
|
|
|
|
2010-07-30 21:25:35 +00:00
|
|
|
#include <osmocore/msgb.h>
|
|
|
|
#include <osmocore/talloc.h>
|
|
|
|
#include <osmocore/select.h>
|
|
|
|
#include <osmocore/utils.h>
|
|
|
|
|
|
|
|
|
2010-07-27 19:32:52 +00:00
|
|
|
/**
|
|
|
|
* Macro for tokenizing MGCP messages and SDP in one go.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define MSG_TOKENIZE_START \
|
|
|
|
line_start = 0; \
|
|
|
|
for (i = 0; i < msgb_l3len(msg); ++i) { \
|
|
|
|
/* we have a line end */ \
|
|
|
|
if (msg->l3h[i] == '\n') { \
|
|
|
|
/* skip the first line */ \
|
|
|
|
if (line_start == 0) { \
|
|
|
|
line_start = i + 1; \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* check if we have a proper param */ \
|
|
|
|
if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
|
|
|
|
} else if (i - line_start > 2 \
|
|
|
|
&& islower(msg->l3h[line_start]) \
|
|
|
|
&& msg->l3h[line_start + 1] == '=') { \
|
|
|
|
} else if (i - line_start < 3 \
|
|
|
|
|| msg->l3h[line_start + 1] != ':' \
|
|
|
|
|| msg->l3h[line_start + 2] != ' ') \
|
|
|
|
goto error; \
|
|
|
|
\
|
|
|
|
msg->l3h[i] = '\0'; \
|
|
|
|
if (msg->l3h[i-1] == '\r') \
|
|
|
|
msg->l3h[i-1] = '\0';
|
|
|
|
|
|
|
|
#define MSG_TOKENIZE_END \
|
|
|
|
line_start = i + 1; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct mgcp_request {
|
|
|
|
char *name;
|
|
|
|
struct msgb *(*handle_request) (struct mgcp_config *cfg, struct msgb *msg);
|
|
|
|
char *debug_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
|
|
|
|
{ .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
|
|
|
|
|
|
|
|
static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg);
|
|
|
|
static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg);
|
|
|
|
static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg);
|
|
|
|
static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg);
|
|
|
|
static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg);
|
|
|
|
|
2010-08-07 23:51:51 +00:00
|
|
|
static uint32_t generate_call_id(struct mgcp_config *cfg)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* use the call id */
|
|
|
|
++cfg->last_call_id;
|
|
|
|
|
|
|
|
/* handle wrap around */
|
|
|
|
if (cfg->last_call_id == CI_UNUSED)
|
|
|
|
++cfg->last_call_id;
|
|
|
|
|
|
|
|
/* callstack can only be of size number_of_endpoints */
|
|
|
|
/* verify that the call id is free, e.g. in case of overrun */
|
|
|
|
for (i = 1; i < cfg->number_endpoints; ++i)
|
|
|
|
if (cfg->endpoints[i].ci == cfg->last_call_id)
|
|
|
|
return generate_call_id(cfg);
|
|
|
|
|
|
|
|
return cfg->last_call_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* array of function pointers for handling various
|
|
|
|
* messages. In the future this might be binary sorted
|
|
|
|
* for performance reasons.
|
|
|
|
*/
|
|
|
|
static const struct mgcp_request mgcp_requests [] = {
|
|
|
|
MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
|
|
|
|
MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
|
|
|
|
MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
|
|
|
|
MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
|
|
|
|
|
|
|
|
/* SPEC extension */
|
|
|
|
MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct msgb *mgcp_msgb_alloc(void)
|
|
|
|
{
|
|
|
|
struct msgb *msg;
|
|
|
|
msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
|
|
|
|
if (!msg)
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2011-01-06 18:48:55 +00:00
|
|
|
struct msgb *mgcp_create_response_with_data(int code, const char *txt,
|
|
|
|
const char *msg, const char *trans,
|
|
|
|
const char *data)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
struct msgb *res;
|
|
|
|
|
|
|
|
res = mgcp_msgb_alloc();
|
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (data) {
|
2011-01-06 18:48:55 +00:00
|
|
|
len = snprintf((char *) res->data, 2048, "%d %s%s\r\n%s", code, trans, txt, data);
|
2010-07-27 19:32:52 +00:00
|
|
|
} else {
|
2011-01-06 18:48:55 +00:00
|
|
|
len = snprintf((char *) res->data, 2048, "%d %s%s\r\n", code, trans, txt);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res->l2h = msgb_put(res, len);
|
|
|
|
LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-01-06 18:48:55 +00:00
|
|
|
static struct msgb *create_ok_response(int code, const char *msg, const char *trans)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2011-01-06 18:48:55 +00:00
|
|
|
return mgcp_create_response_with_data(code, " OK", msg, trans, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct msgb *create_err_response(int code, const char *msg, const char *trans)
|
|
|
|
{
|
|
|
|
return mgcp_create_response_with_data(code, " FAIL", msg, trans, NULL);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
|
|
|
|
const char *msg, const char *trans_id)
|
|
|
|
{
|
|
|
|
const char *addr = endp->cfg->local_ip;
|
|
|
|
char sdp_record[4096];
|
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
addr = endp->cfg->source_addr;
|
|
|
|
|
|
|
|
snprintf(sdp_record, sizeof(sdp_record) - 1,
|
2010-08-07 23:51:51 +00:00
|
|
|
"I: %u\n\n"
|
2010-07-27 19:32:52 +00:00
|
|
|
"v=0\r\n"
|
|
|
|
"c=IN IP4 %s\r\n"
|
|
|
|
"m=audio %d RTP/AVP %d\r\n"
|
|
|
|
"a=rtpmap:%d %s\r\n",
|
|
|
|
endp->ci, addr, endp->rtp_port,
|
|
|
|
endp->bts_payload_type, endp->bts_payload_type,
|
|
|
|
endp->cfg->audio_name);
|
2011-01-06 18:48:55 +00:00
|
|
|
return mgcp_create_response_with_data(200, " OK", msg, trans_id, sdp_record);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle incoming messages:
|
|
|
|
* - this can be a command (four letters, space, transaction id)
|
|
|
|
* - or a response (three numbers, space, transaction id)
|
|
|
|
*/
|
|
|
|
struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
struct msgb *resp = NULL;
|
|
|
|
|
|
|
|
if (msgb_l2len(msg) < 4) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* attempt to treat it as a response */
|
|
|
|
if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
|
|
|
|
LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
|
|
|
|
} else {
|
|
|
|
int i, handled = 0;
|
|
|
|
msg->l3h = &msg->l2h[4];
|
|
|
|
for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
|
|
|
|
if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) {
|
|
|
|
handled = 1;
|
|
|
|
resp = mgcp_requests[i].handle_request(cfg, msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!handled) {
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* string tokenizer for the poor */
|
|
|
|
static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
|
|
|
|
{
|
|
|
|
int i, found = 0;
|
|
|
|
|
|
|
|
int whitespace = 1;
|
|
|
|
for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
|
|
|
|
/* if we have a space we found an end */
|
|
|
|
if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
|
|
|
|
if (!whitespace) {
|
|
|
|
++found;
|
|
|
|
whitespace = 1;
|
|
|
|
ptrs->length = i - ptrs->start - 1;
|
|
|
|
++ptrs;
|
|
|
|
--ptrs_length;
|
|
|
|
} else {
|
|
|
|
/* skip any number of whitespace */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* line end... stop */
|
|
|
|
if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
|
|
|
|
break;
|
|
|
|
} else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
|
|
|
|
/* line end, be done */
|
|
|
|
break;
|
|
|
|
} else if (whitespace) {
|
|
|
|
whitespace = 0;
|
|
|
|
ptrs->start = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptrs_length == 0)
|
|
|
|
return -1;
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2011-01-06 18:48:55 +00:00
|
|
|
/**
|
|
|
|
* We have a null terminated string with the endpoint name here. We only
|
|
|
|
* support two kinds. Simple ones as seen on the BSC level and the ones
|
|
|
|
* seen on the trunk side.
|
|
|
|
*/
|
|
|
|
static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
|
|
|
|
const char *mgcp)
|
|
|
|
{
|
|
|
|
char *rest = NULL;
|
|
|
|
int trunk, endp, mgcp_endp;
|
|
|
|
|
|
|
|
trunk = strtoul(mgcp + 6, &rest, 10);
|
|
|
|
if (rest == NULL || rest[0] != '/') {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
endp = strtoul(rest + 1, &rest, 10);
|
|
|
|
if (rest == NULL || rest[0] != '@') {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* signalling is on timeslot 1 */
|
|
|
|
if (endp == 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mgcp_endp = mgcp_timeslot_to_endpoint(trunk, endp);
|
|
|
|
if (mgcp_endp < 1 || mgcp_endp >= cfg->number_endpoints) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cfg->endpoints[mgcp_endp];
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:32:52 +00:00
|
|
|
static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp)
|
|
|
|
{
|
|
|
|
char *endptr = NULL;
|
|
|
|
unsigned int gw = INT_MAX;
|
|
|
|
|
2011-01-06 18:48:55 +00:00
|
|
|
if (strncmp(mgcp, "ds/e1", 5) == 0) {
|
|
|
|
return find_e1_endpoint(cfg, mgcp);
|
|
|
|
} else {
|
|
|
|
gw = strtoul(mgcp, &endptr, 16);
|
|
|
|
if (gw > 0 && gw < cfg->number_endpoints && strcmp(endptr, "@mgw") == 0)
|
|
|
|
return &cfg->endpoints[gw];
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 18:48:55 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
|
|
|
|
return NULL;
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int mgcp_analyze_header(struct mgcp_config *cfg, struct msgb *msg,
|
|
|
|
struct mgcp_msg_ptr *ptr, int size,
|
|
|
|
const char **transaction_id, struct mgcp_endpoint **endp)
|
|
|
|
{
|
|
|
|
int found;
|
|
|
|
|
|
|
|
*transaction_id = "000000";
|
|
|
|
|
|
|
|
if (size < 3) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
found = find_msg_pointers(msg, ptr, size);
|
|
|
|
|
|
|
|
if (found <= 3) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* replace the space with \0. the main method gurantess that
|
|
|
|
* we still have + 1 for null termination
|
|
|
|
*/
|
|
|
|
msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
|
|
|
|
msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
|
|
|
|
msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
|
|
|
|
msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
|
|
|
|
|
|
|
|
if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
|
|
|
|
|| strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n",
|
|
|
|
(const char *)&msg->l3h[ptr[3].start],
|
|
|
|
(const char *)&msg->l3h[ptr[2].start]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*transaction_id = (const char *)&msg->l3h[ptr[0].start];
|
|
|
|
if (endp) {
|
|
|
|
*endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]);
|
|
|
|
return *endp == NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int verify_call_id(const struct mgcp_endpoint *endp,
|
|
|
|
const char *callid)
|
|
|
|
{
|
|
|
|
if (strcmp(endp->callid, callid) != 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
|
|
|
|
ENDPOINT_NUMBER(endp), endp->callid, callid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int verify_ci(const struct mgcp_endpoint *endp,
|
2010-08-07 23:51:51 +00:00
|
|
|
const char *_ci)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2010-08-07 23:51:51 +00:00
|
|
|
uint32_t ci = strtoul(_ci, NULL, 10);
|
|
|
|
|
|
|
|
if (ci != endp->ci) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %u != %s\n",
|
|
|
|
ENDPOINT_NUMBER(endp), endp->ci, _ci);
|
2010-07-27 19:32:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg)
|
|
|
|
{
|
|
|
|
struct mgcp_msg_ptr data_ptrs[6];
|
2011-01-06 18:48:55 +00:00
|
|
|
int found;
|
2010-07-27 19:32:52 +00:00
|
|
|
const char *trans_id;
|
|
|
|
struct mgcp_endpoint *endp;
|
|
|
|
|
|
|
|
found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
|
|
|
|
if (found != 0)
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(500, "AUEP", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
else
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_ok_response(200, "AUEP", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_conn_mode(const char* msg, int *conn_mode)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if (strcmp(msg, "recvonly") == 0)
|
|
|
|
*conn_mode = MGCP_CONN_RECV_ONLY;
|
|
|
|
else if (strcmp(msg, "sendrecv") == 0)
|
|
|
|
*conn_mode = MGCP_CONN_RECV_SEND;
|
2010-09-18 12:23:05 +00:00
|
|
|
else if (strcmp(msg, "sendonly") == 0)
|
|
|
|
*conn_mode = MGCP_CONN_SEND_ONLY;
|
2010-08-03 22:11:27 +00:00
|
|
|
else if (strcmp(msg, "loopback") == 0)
|
|
|
|
*conn_mode = MGCP_CONN_LOOPBACK;
|
2010-07-27 19:32:52 +00:00
|
|
|
else {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg)
|
|
|
|
{
|
|
|
|
struct mgcp_msg_ptr data_ptrs[6];
|
|
|
|
int found, i, line_start;
|
|
|
|
const char *trans_id;
|
|
|
|
struct mgcp_endpoint *endp;
|
|
|
|
int error_code = 500;
|
|
|
|
int port;
|
|
|
|
|
|
|
|
found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
|
|
|
|
if (found != 0)
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(510, "CRCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
if (endp->ci != CI_UNUSED) {
|
|
|
|
if (cfg->force_realloc) {
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n",
|
|
|
|
ENDPOINT_NUMBER(endp));
|
2010-08-03 22:11:27 +00:00
|
|
|
mgcp_free_endp(endp);
|
2010-09-17 19:11:00 +00:00
|
|
|
if (cfg->realloc_cb)
|
|
|
|
cfg->realloc_cb(cfg, ENDPOINT_NUMBER(endp));
|
2010-07-27 19:32:52 +00:00
|
|
|
} else {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n",
|
|
|
|
ENDPOINT_NUMBER(endp));
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(400, "CRCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse CallID C: and LocalParameters L: */
|
|
|
|
MSG_TOKENIZE_START
|
|
|
|
switch (msg->l3h[line_start]) {
|
|
|
|
case 'L':
|
|
|
|
endp->local_options = talloc_strdup(cfg->endpoints,
|
|
|
|
(const char *)&msg->l3h[line_start + 3]);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
endp->callid = talloc_strdup(cfg->endpoints,
|
|
|
|
(const char *)&msg->l3h[line_start + 3]);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
|
|
|
|
&endp->conn_mode) != 0) {
|
|
|
|
error_code = 517;
|
|
|
|
goto error2;
|
|
|
|
}
|
2010-08-03 22:11:27 +00:00
|
|
|
|
|
|
|
endp->orig_mode = endp->conn_mode;
|
2010-07-27 19:32:52 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
|
|
|
|
msg->l3h[line_start], msg->l3h[line_start],
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MSG_TOKENIZE_END
|
|
|
|
|
|
|
|
/* initialize */
|
|
|
|
endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
|
|
|
|
|
|
|
|
/* set to zero until we get the info */
|
|
|
|
memset(&endp->remote, 0, sizeof(endp->remote));
|
|
|
|
|
|
|
|
/* bind to the port now */
|
|
|
|
port = rtp_calculate_port(ENDPOINT_NUMBER(endp), cfg->rtp_base_port);
|
|
|
|
if (cfg->early_bind)
|
|
|
|
endp->rtp_port = port;
|
|
|
|
else if (mgcp_bind_rtp_port(endp, port) != 0)
|
|
|
|
goto error2;
|
|
|
|
|
|
|
|
/* assign a local call identifier or fail */
|
|
|
|
endp->ci = generate_call_id(cfg);
|
|
|
|
if (endp->ci == CI_UNUSED)
|
|
|
|
goto error2;
|
|
|
|
|
|
|
|
endp->bts_payload_type = cfg->audio_payload;
|
|
|
|
|
|
|
|
/* policy CB */
|
|
|
|
if (cfg->policy_cb) {
|
|
|
|
switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, trans_id)) {
|
|
|
|
case MGCP_POLICY_REJECT:
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n",
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
mgcp_free_endp(endp);
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(400, "CRCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
break;
|
|
|
|
case MGCP_POLICY_DEFER:
|
|
|
|
/* stop processing */
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case MGCP_POLICY_CONT:
|
|
|
|
/* just continue */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n",
|
|
|
|
ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
|
|
|
|
if (cfg->change_cb)
|
|
|
|
cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, endp->rtp_port);
|
|
|
|
|
|
|
|
return create_response_with_sdp(endp, "CRCX", trans_id);
|
|
|
|
error:
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
|
|
|
|
hexdump(msg->l3h, msgb_l3len(msg)),
|
|
|
|
ENDPOINT_NUMBER(endp), line_start, i);
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(error_code, "CRCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
error2:
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(error_code, "CRCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg)
|
|
|
|
{
|
|
|
|
struct mgcp_msg_ptr data_ptrs[6];
|
|
|
|
int found, i, line_start;
|
|
|
|
const char *trans_id;
|
|
|
|
struct mgcp_endpoint *endp;
|
|
|
|
int error_code = 500;
|
|
|
|
int silent = 0;
|
|
|
|
|
|
|
|
found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
|
|
|
|
if (found != 0)
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(510, "MDCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
if (endp->ci == CI_UNUSED) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(400, "MDCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MSG_TOKENIZE_START
|
|
|
|
switch (msg->l3h[line_start]) {
|
|
|
|
case 'C': {
|
|
|
|
if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
|
|
|
|
goto error3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'I': {
|
|
|
|
if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
|
|
|
|
goto error3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'L':
|
|
|
|
/* skip */
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
|
|
|
|
&endp->conn_mode) != 0) {
|
|
|
|
error_code = 517;
|
|
|
|
goto error3;
|
|
|
|
}
|
2010-08-03 22:11:27 +00:00
|
|
|
endp->orig_mode = endp->conn_mode;
|
2010-07-27 19:32:52 +00:00
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0;
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
/* SDP file begins */
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
case 'o':
|
|
|
|
case 's':
|
|
|
|
case 't':
|
|
|
|
case 'v':
|
|
|
|
/* skip these SDP attributes */
|
|
|
|
break;
|
|
|
|
case 'm': {
|
|
|
|
int port;
|
|
|
|
int payload;
|
|
|
|
const char *param = (const char *)&msg->l3h[line_start];
|
|
|
|
|
|
|
|
if (sscanf(param, "m=audio %d RTP/AVP %d", &port, &payload) == 2) {
|
|
|
|
endp->net_rtp = htons(port);
|
|
|
|
endp->net_rtcp = htons(port + 1);
|
|
|
|
endp->net_payload_type = payload;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'c': {
|
|
|
|
char ipv4[16];
|
|
|
|
const char *param = (const char *)&msg->l3h[line_start];
|
|
|
|
|
|
|
|
if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
|
|
|
|
inet_aton(ipv4, &endp->remote);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
|
|
|
|
msg->l3h[line_start], msg->l3h[line_start],
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MSG_TOKENIZE_END
|
|
|
|
|
|
|
|
/* policy CB */
|
|
|
|
if (cfg->policy_cb) {
|
|
|
|
switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, trans_id)) {
|
|
|
|
case MGCP_POLICY_REJECT:
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n",
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
if (silent)
|
|
|
|
goto out_silent;
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(400, "MDCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
break;
|
|
|
|
case MGCP_POLICY_DEFER:
|
|
|
|
/* stop processing */
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case MGCP_POLICY_CONT:
|
|
|
|
/* just continue */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* modify */
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n",
|
|
|
|
ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp));
|
|
|
|
if (cfg->change_cb)
|
|
|
|
cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, endp->rtp_port);
|
|
|
|
if (silent)
|
|
|
|
goto out_silent;
|
|
|
|
|
|
|
|
return create_response_with_sdp(endp, "MDCX", trans_id);
|
|
|
|
|
|
|
|
error:
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
|
|
|
|
hexdump(msg->l3h, msgb_l3len(msg)),
|
|
|
|
ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(error_code, "MDCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
error3:
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(error_code, "MDCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
out_silent:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg)
|
|
|
|
{
|
|
|
|
struct mgcp_msg_ptr data_ptrs[6];
|
|
|
|
int found, i, line_start;
|
|
|
|
const char *trans_id;
|
|
|
|
struct mgcp_endpoint *endp;
|
|
|
|
int error_code = 500;
|
|
|
|
int silent = 0;
|
|
|
|
|
|
|
|
found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
|
|
|
|
if (found != 0)
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(error_code, "DLCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
if (endp->ci == CI_UNUSED) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(400, "DLCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MSG_TOKENIZE_START
|
|
|
|
switch (msg->l3h[line_start]) {
|
|
|
|
case 'C': {
|
|
|
|
if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
|
|
|
|
goto error3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'I': {
|
|
|
|
if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
|
|
|
|
goto error3;
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
|
|
|
|
msg->l3h[line_start], msg->l3h[line_start],
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MSG_TOKENIZE_END
|
|
|
|
|
|
|
|
/* policy CB */
|
|
|
|
if (cfg->policy_cb) {
|
|
|
|
switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, trans_id)) {
|
|
|
|
case MGCP_POLICY_REJECT:
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n",
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
if (silent)
|
|
|
|
goto out_silent;
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(400, "DLCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
break;
|
|
|
|
case MGCP_POLICY_DEFER:
|
|
|
|
/* stop processing */
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case MGCP_POLICY_CONT:
|
|
|
|
/* just continue */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free the connection */
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Deleted endpoint on: 0x%x Server: %s:%u\n",
|
|
|
|
ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp));
|
|
|
|
mgcp_free_endp(endp);
|
|
|
|
if (cfg->change_cb)
|
|
|
|
cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, endp->rtp_port);
|
|
|
|
|
|
|
|
if (silent)
|
|
|
|
goto out_silent;
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_ok_response(250, "DLCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
|
|
|
|
hexdump(msg->l3h, msgb_l3len(msg)),
|
|
|
|
ENDPOINT_NUMBER(endp), line_start, i);
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(error_code, "DLCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
error3:
|
2011-01-06 18:48:55 +00:00
|
|
|
return create_err_response(error_code, "DLCX", trans_id);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
out_silent:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg)
|
|
|
|
{
|
|
|
|
if (cfg->reset_cb)
|
|
|
|
cfg->reset_cb(cfg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mgcp_config *mgcp_config_alloc(void)
|
|
|
|
{
|
|
|
|
struct mgcp_config *cfg;
|
|
|
|
|
|
|
|
cfg = talloc_zero(NULL, struct mgcp_config);
|
|
|
|
if (!cfg) {
|
|
|
|
LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg->source_port = 2427;
|
|
|
|
cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
|
|
|
|
cfg->audio_name = talloc_strdup(cfg, "GSM-EFR/8000");
|
|
|
|
cfg->audio_payload = 97;
|
|
|
|
cfg->rtp_base_port = RTP_PORT_DEFAULT;
|
|
|
|
|
|
|
|
return cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mgcp_endpoints_allocate(struct mgcp_config *cfg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize all endpoints */
|
|
|
|
cfg->endpoints = _talloc_zero_array(cfg,
|
|
|
|
sizeof(struct mgcp_endpoint),
|
|
|
|
cfg->number_endpoints, "endpoints");
|
|
|
|
if (!cfg->endpoints)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < cfg->number_endpoints; ++i) {
|
|
|
|
cfg->endpoints[i].local_rtp.fd = -1;
|
|
|
|
cfg->endpoints[i].local_rtcp.fd = -1;
|
|
|
|
cfg->endpoints[i].ci = CI_UNUSED;
|
|
|
|
cfg->endpoints[i].cfg = cfg;
|
|
|
|
cfg->endpoints[i].net_payload_type = -1;
|
|
|
|
cfg->endpoints[i].bts_payload_type = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mgcp_free_endp(struct mgcp_endpoint *endp)
|
|
|
|
{
|
|
|
|
LOGP(DMGCP, LOGL_DEBUG, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
|
2010-08-03 22:11:27 +00:00
|
|
|
endp->ci = CI_UNUSED;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
if (endp->callid) {
|
|
|
|
talloc_free(endp->callid);
|
|
|
|
endp->callid = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endp->local_options) {
|
|
|
|
talloc_free(endp->local_options);
|
|
|
|
endp->local_options = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!endp->cfg->early_bind) {
|
|
|
|
bsc_unregister_fd(&endp->local_rtp);
|
|
|
|
bsc_unregister_fd(&endp->local_rtcp);
|
|
|
|
}
|
|
|
|
|
|
|
|
endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
|
|
|
|
endp->net_payload_type = endp->bts_payload_type = -1;
|
|
|
|
endp->in_bts = endp->in_remote = 0;
|
|
|
|
memset(&endp->remote, 0, sizeof(endp->remote));
|
|
|
|
memset(&endp->bts, 0, sizeof(endp->bts));
|
2010-08-03 22:11:27 +00:00
|
|
|
|
|
|
|
memset(&endp->net_state, 0, sizeof(endp->net_state));
|
|
|
|
memset(&endp->bts_state, 0, sizeof(endp->bts_state));
|
|
|
|
|
|
|
|
endp->conn_mode = endp->orig_mode = MGCP_CONN_NONE;
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|