2012-10-24 19:52:16 +00:00
|
|
|
/**
|
|
|
|
* This file contains helper routines for MGCP Gateway handling.
|
|
|
|
*
|
|
|
|
* The first thing to remember is that each BSC has its own namespace/range
|
|
|
|
* of endpoints. Whenever a BSSMAP ASSIGNMENT REQUEST is received this code
|
|
|
|
* will be called to select an endpoint on the BSC. The mapping from original
|
|
|
|
* multiplex/timeslot to BSC multiplex'/timeslot' will be stored.
|
|
|
|
*
|
|
|
|
* The second part is to take messages on the public MGCP GW interface
|
|
|
|
* and forward them to the right BSC. This requires the MSC to first
|
|
|
|
* assign the timeslot. This assumption has been true so far. We are using
|
|
|
|
* the policy_cb of the MGCP protocol code to decide if the request should
|
|
|
|
* be immediately answered or delayed. An extension "Z: noanswer" is used
|
|
|
|
* to request the BSC to not respond. This is saving some bytes of bandwidth
|
|
|
|
* and as we are using TCP to forward the message we know it will arrive.
|
|
|
|
* The mgcp_do_read method reads these messages and hands them to the protocol
|
|
|
|
* parsing code which will call the mentioned policy_cb. The bsc_mgcp_forward
|
|
|
|
* method is used on the way back from the BSC to the network.
|
|
|
|
*
|
|
|
|
* The third part is to patch messages forwarded to the BSC. This includes
|
|
|
|
* the endpoint number, the ports to be used inside the SDP file and maybe
|
|
|
|
* some other bits.
|
|
|
|
*
|
|
|
|
*/
|
2010-06-15 10:49:53 +00:00
|
|
|
/*
|
2012-10-24 19:52:16 +00:00
|
|
|
* (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* (C) 2010-2012 by On-Waves
|
2010-06-15 10:49:53 +00:00
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2011-01-01 14:25:50 +00:00
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-06-15 10:49:53 +00:00
|
|
|
* (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
|
2011-01-01 14:25:50 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
2010-06-15 10:49:53 +00:00
|
|
|
*
|
2011-01-01 14:25:50 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-06-15 10:49:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openbsc/bsc_nat.h>
|
2010-07-30 21:17:17 +00:00
|
|
|
#include <openbsc/bsc_nat_sccp.h>
|
2010-06-15 10:49:53 +00:00
|
|
|
#include <openbsc/gsm_data.h>
|
|
|
|
#include <openbsc/debug.h>
|
2010-10-13 21:52:01 +00:00
|
|
|
#include <openbsc/ipaccess.h>
|
2010-03-31 07:16:56 +00:00
|
|
|
#include <openbsc/mgcp.h>
|
|
|
|
#include <openbsc/mgcp_internal.h>
|
2010-06-15 10:49:53 +00:00
|
|
|
|
2010-08-03 13:11:51 +00:00
|
|
|
#include <osmocom/sccp/sccp.h>
|
2010-04-22 04:08:17 +00:00
|
|
|
|
2011-03-22 15:47:59 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
2011-03-23 17:26:56 +00:00
|
|
|
#include <osmocom/gsm/gsm0808.h>
|
|
|
|
#include <osmocom/gsm/protocol/gsm_08_08.h>
|
2010-04-01 06:21:33 +00:00
|
|
|
|
2010-06-15 10:49:53 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2010-03-31 11:02:22 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-02-27 10:04:27 +00:00
|
|
|
int bsc_mgcp_nr_multiplexes(int max_endpoints)
|
|
|
|
{
|
|
|
|
int div = max_endpoints / 32;
|
|
|
|
|
|
|
|
if ((max_endpoints % 32) != 0)
|
|
|
|
div += 1;
|
|
|
|
|
|
|
|
return div;
|
|
|
|
}
|
|
|
|
|
2011-02-25 16:09:07 +00:00
|
|
|
static int bsc_init_endps_if_needed(struct bsc_connection *con)
|
|
|
|
{
|
2011-02-27 10:04:27 +00:00
|
|
|
int multiplexes;
|
|
|
|
|
2011-02-25 16:09:07 +00:00
|
|
|
/* we have done that */
|
|
|
|
if (con->_endpoint_status)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* we have no config... */
|
|
|
|
if (!con->cfg)
|
|
|
|
return -1;
|
|
|
|
|
2011-02-27 10:04:27 +00:00
|
|
|
multiplexes = bsc_mgcp_nr_multiplexes(con->cfg->max_endpoints);
|
|
|
|
con->number_multiplexes = multiplexes;
|
|
|
|
con->max_endpoints = con->cfg->max_endpoints;
|
|
|
|
con->_endpoint_status = talloc_zero_array(con, char, 32 * multiplexes + 1);
|
2011-02-25 16:09:07 +00:00
|
|
|
return con->_endpoint_status == NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-28 10:22:14 +00:00
|
|
|
static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
|
|
|
|
{
|
2011-02-26 10:38:00 +00:00
|
|
|
int multiplex;
|
|
|
|
int timeslot;
|
2011-02-27 10:04:27 +00:00
|
|
|
const int number_endpoints = bsc->max_endpoints;
|
2010-08-28 10:22:14 +00:00
|
|
|
int i;
|
|
|
|
|
2011-02-26 10:38:00 +00:00
|
|
|
mgcp_endpoint_to_timeslot(bsc->last_endpoint, &multiplex, ×lot);
|
|
|
|
timeslot += 1;
|
|
|
|
|
|
|
|
for (i = 0; i < number_endpoints; ++i) {
|
|
|
|
int endpoint;
|
|
|
|
|
|
|
|
/* Wrap around timeslots */
|
|
|
|
if (timeslot == 0)
|
|
|
|
timeslot = 1;
|
|
|
|
|
|
|
|
if (timeslot == 0x1f) {
|
|
|
|
timeslot = 1;
|
|
|
|
multiplex += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrap around the multiplex */
|
|
|
|
if (multiplex >= bsc->number_multiplexes)
|
|
|
|
multiplex = 0;
|
|
|
|
|
|
|
|
endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot);
|
2010-08-28 10:22:14 +00:00
|
|
|
|
2011-02-27 10:04:27 +00:00
|
|
|
/* Now check if we are allowed to assign this one */
|
|
|
|
if (endpoint >= bsc->max_endpoints) {
|
|
|
|
multiplex = 0;
|
|
|
|
timeslot = 1;
|
|
|
|
endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-25 16:09:07 +00:00
|
|
|
if (bsc->_endpoint_status[endpoint] == 0) {
|
|
|
|
bsc->_endpoint_status[endpoint] = 1;
|
2010-08-28 10:22:14 +00:00
|
|
|
con->bsc_endp = endpoint;
|
|
|
|
bsc->last_endpoint = endpoint;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-26 10:38:00 +00:00
|
|
|
|
|
|
|
timeslot += 1;
|
2010-08-28 10:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t create_cic(int endpoint)
|
|
|
|
{
|
|
|
|
int timeslot, multiplex;
|
|
|
|
|
|
|
|
mgcp_endpoint_to_timeslot(endpoint, &multiplex, ×lot);
|
|
|
|
return (multiplex << 5) | (timeslot & 0x1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
|
2010-06-15 10:49:53 +00:00
|
|
|
{
|
2010-04-22 04:08:17 +00:00
|
|
|
struct sccp_connections *mcon;
|
2010-06-15 10:49:53 +00:00
|
|
|
struct tlv_parsed tp;
|
2010-07-23 11:09:21 +00:00
|
|
|
uint16_t cic;
|
2010-07-23 11:08:55 +00:00
|
|
|
uint8_t timeslot;
|
|
|
|
uint8_t multiplex;
|
2010-11-04 16:14:41 +00:00
|
|
|
unsigned int endp;
|
2010-06-15 10:49:53 +00:00
|
|
|
|
|
|
|
if (!msg->l3h) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msgb_l3len(msg) < 3) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
|
|
|
|
if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-23 11:09:21 +00:00
|
|
|
cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
|
2010-06-15 10:49:53 +00:00
|
|
|
timeslot = cic & 0x1f;
|
|
|
|
multiplex = (cic & ~0x1f) >> 5;
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
|
2010-08-28 08:08:39 +00:00
|
|
|
endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
|
2010-04-22 04:08:17 +00:00
|
|
|
|
2011-02-27 23:56:17 +00:00
|
|
|
if (endp >= con->bsc->nat->mgcp_cfg->trunk.number_endpoints) {
|
2010-11-04 16:14:41 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR,
|
|
|
|
"MSC attempted to assign bad endpoint 0x%x\n",
|
|
|
|
endp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
/* find stale connections using that endpoint */
|
|
|
|
llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
|
2010-08-28 08:08:39 +00:00
|
|
|
if (mcon->msc_endp == endp) {
|
2010-04-22 04:08:17 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR,
|
2010-08-28 08:08:39 +00:00
|
|
|
"Endpoint %d was assigned to 0x%x and now 0x%x\n",
|
|
|
|
endp,
|
2010-04-22 04:08:17 +00:00
|
|
|
sccp_src_ref_to_int(&mcon->patched_ref),
|
|
|
|
sccp_src_ref_to_int(&con->patched_ref));
|
|
|
|
bsc_mgcp_dlcx(mcon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 08:08:39 +00:00
|
|
|
con->msc_endp = endp;
|
2011-02-25 16:09:07 +00:00
|
|
|
if (bsc_init_endps_if_needed(con->bsc) != 0)
|
|
|
|
return -1;
|
2010-08-28 10:22:14 +00:00
|
|
|
if (bsc_assign_endpoint(con->bsc, con) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now patch the message for the new CIC...
|
|
|
|
* still assumed to be one multiplex only
|
|
|
|
*/
|
|
|
|
cic = htons(create_cic(con->bsc_endp));
|
|
|
|
memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE),
|
|
|
|
&cic, sizeof(cic));
|
|
|
|
|
2010-06-15 10:49:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
|
2010-04-01 06:21:33 +00:00
|
|
|
{
|
|
|
|
if (nat->bsc_endpoints[i].transaction_id) {
|
|
|
|
talloc_free(nat->bsc_endpoints[i].transaction_id);
|
|
|
|
nat->bsc_endpoints[i].transaction_id = NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-06 01:05:05 +00:00
|
|
|
nat->bsc_endpoints[i].transaction_state = 0;
|
2010-04-01 06:21:33 +00:00
|
|
|
nat->bsc_endpoints[i].bsc = NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-31 07:16:56 +00:00
|
|
|
void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-02-27 23:56:17 +00:00
|
|
|
for (i = 1; i < nat->mgcp_cfg->trunk.number_endpoints; ++i){
|
2010-04-01 06:21:33 +00:00
|
|
|
bsc_mgcp_free_endpoint(nat, i);
|
2011-02-27 23:56:17 +00:00
|
|
|
mgcp_free_endp(&nat->mgcp_cfg->trunk.endpoints[i]);
|
2010-04-22 04:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send a MDCX where we do not want a response */
|
2010-08-29 15:40:33 +00:00
|
|
|
static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
|
2010-04-22 04:08:17 +00:00
|
|
|
{
|
|
|
|
char buf[2096];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = snprintf(buf, sizeof(buf),
|
2010-04-22 05:06:24 +00:00
|
|
|
"MDCX 23 %x@mgw MGCP 1.0\r\n"
|
2010-04-22 04:08:17 +00:00
|
|
|
"Z: noanswer\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"c=IN IP4 %s\r\n"
|
|
|
|
"m=audio %d RTP/AVP 255\r\n",
|
2010-08-29 15:40:33 +00:00
|
|
|
port,
|
2010-04-22 04:08:17 +00:00
|
|
|
bsc->nat->mgcp_cfg->source_addr,
|
2010-08-04 19:08:35 +00:00
|
|
|
endp->bts_end.local_port);
|
2010-04-22 04:08:17 +00:00
|
|
|
if (len < 0) {
|
2012-09-14 13:51:43 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "snprintf for MDCX failed.\n");
|
2010-04-22 04:08:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-08-30 03:58:49 +00:00
|
|
|
|
|
|
|
#warning "The MDCX is not send to the BSC. It should"
|
2010-04-22 04:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
|
|
|
|
{
|
|
|
|
char buf[2096];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = snprintf(buf, sizeof(buf),
|
2010-08-30 04:01:36 +00:00
|
|
|
"DLCX 26 %x@mgw MGCP 1.0\r\n"
|
2010-04-22 04:08:17 +00:00
|
|
|
"Z: noanswer\r\n", endpoint);
|
|
|
|
if (len < 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-23 11:08:55 +00:00
|
|
|
bsc_write_mgcp(bsc, (uint8_t *) buf, len);
|
2010-04-22 04:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bsc_mgcp_init(struct sccp_connections *con)
|
|
|
|
{
|
2010-08-28 08:08:39 +00:00
|
|
|
con->msc_endp = -1;
|
|
|
|
con->bsc_endp = -1;
|
2010-04-22 04:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bsc_mgcp_dlcx(struct sccp_connections *con)
|
|
|
|
{
|
|
|
|
/* send a DLCX down the stream */
|
2011-02-25 16:09:07 +00:00
|
|
|
if (con->bsc_endp != -1 && con->bsc->_endpoint_status) {
|
|
|
|
if (con->bsc->_endpoint_status[con->bsc_endp] != 1)
|
2010-08-30 05:44:32 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp);
|
2011-02-25 16:09:07 +00:00
|
|
|
con->bsc->_endpoint_status[con->bsc_endp] = 0;
|
2010-08-28 08:08:39 +00:00
|
|
|
bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
|
|
|
|
bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
|
2010-04-22 04:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bsc_mgcp_init(con);
|
2010-03-31 07:16:56 +00:00
|
|
|
}
|
2010-03-31 11:02:22 +00:00
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
|
2010-04-17 18:26:16 +00:00
|
|
|
struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
|
2010-04-01 01:55:27 +00:00
|
|
|
{
|
2010-04-17 18:26:16 +00:00
|
|
|
struct sccp_connections *con = NULL;
|
2010-04-01 01:55:27 +00:00
|
|
|
struct sccp_connections *sccp;
|
|
|
|
|
|
|
|
llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
|
2010-08-28 08:08:39 +00:00
|
|
|
if (sccp->msc_endp == -1)
|
2010-04-01 01:55:27 +00:00
|
|
|
continue;
|
2010-08-28 08:08:39 +00:00
|
|
|
if (sccp->msc_endp != endpoint)
|
2010-04-01 01:55:27 +00:00
|
|
|
continue;
|
|
|
|
|
2010-04-17 18:26:16 +00:00
|
|
|
con = sccp;
|
2010-04-01 01:55:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-17 18:26:16 +00:00
|
|
|
if (con)
|
|
|
|
return con;
|
2010-04-17 18:14:45 +00:00
|
|
|
|
2010-04-01 01:55:27 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-24 19:53:07 +00:00
|
|
|
static int bsc_mgcp_policy_cb(struct mgcp_trunk_config *tcfg, int endpoint, int state, const char *transaction_id)
|
2010-04-01 06:21:33 +00:00
|
|
|
{
|
|
|
|
struct bsc_nat *nat;
|
|
|
|
struct bsc_endpoint *bsc_endp;
|
2010-04-17 18:26:16 +00:00
|
|
|
struct sccp_connections *sccp;
|
2010-04-01 06:21:33 +00:00
|
|
|
struct mgcp_endpoint *mgcp_endp;
|
|
|
|
struct msgb *bsc_msg;
|
|
|
|
|
2011-02-27 23:56:17 +00:00
|
|
|
nat = tcfg->cfg->data;
|
2010-04-01 06:21:33 +00:00
|
|
|
bsc_endp = &nat->bsc_endpoints[endpoint];
|
2011-02-27 23:56:17 +00:00
|
|
|
mgcp_endp = &nat->mgcp_cfg->trunk.endpoints[endpoint];
|
2010-04-01 06:21:33 +00:00
|
|
|
|
2010-04-24 13:05:18 +00:00
|
|
|
if (bsc_endp->transaction_id) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
|
|
|
|
endpoint, bsc_endp->transaction_id);
|
|
|
|
talloc_free(bsc_endp->transaction_id);
|
|
|
|
bsc_endp->transaction_id = NULL;
|
2010-08-06 01:05:05 +00:00
|
|
|
bsc_endp->transaction_state = 0;
|
2010-04-24 13:05:18 +00:00
|
|
|
}
|
|
|
|
bsc_endp->bsc = NULL;
|
|
|
|
|
2010-04-17 18:26:16 +00:00
|
|
|
sccp = bsc_mgcp_find_con(nat, endpoint);
|
2010-04-01 06:21:33 +00:00
|
|
|
|
2010-04-17 18:26:16 +00:00
|
|
|
if (!sccp) {
|
2010-04-24 13:02:01 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for change on endpoint: 0x%x state: %d\n", endpoint, state);
|
2010-04-01 06:21:33 +00:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case MGCP_ENDP_CRCX:
|
|
|
|
return MGCP_POLICY_REJECT;
|
|
|
|
break;
|
|
|
|
case MGCP_ENDP_DLCX:
|
|
|
|
return MGCP_POLICY_CONT;
|
|
|
|
break;
|
|
|
|
case MGCP_ENDP_MDCX:
|
|
|
|
return MGCP_POLICY_CONT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
|
|
|
|
return MGCP_POLICY_CONT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-07 07:53:54 +00:00
|
|
|
/* we need to generate a new and patched message */
|
2010-08-30 05:44:32 +00:00
|
|
|
bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp,
|
2010-08-04 19:08:35 +00:00
|
|
|
nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
|
2010-04-07 07:53:54 +00:00
|
|
|
if (!bsc_msg) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
|
|
|
|
return MGCP_POLICY_CONT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-04 16:11:49 +00:00
|
|
|
bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
|
2010-08-06 01:05:05 +00:00
|
|
|
bsc_endp->transaction_state = state;
|
2010-04-17 18:26:16 +00:00
|
|
|
bsc_endp->bsc = sccp->bsc;
|
2010-04-01 06:21:33 +00:00
|
|
|
|
|
|
|
/* we need to update some bits */
|
|
|
|
if (state == MGCP_ENDP_CRCX) {
|
|
|
|
struct sockaddr_in sock;
|
|
|
|
socklen_t len = sizeof(sock);
|
2010-04-17 18:26:16 +00:00
|
|
|
if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
|
2010-04-06 09:17:07 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
|
|
|
|
errno, strerror(errno));
|
2010-04-01 06:21:33 +00:00
|
|
|
} else {
|
2010-08-04 17:34:51 +00:00
|
|
|
mgcp_endp->bts_end.addr = sock.sin_addr;
|
2010-04-01 06:21:33 +00:00
|
|
|
}
|
2010-04-22 04:08:17 +00:00
|
|
|
|
2010-08-04 17:50:44 +00:00
|
|
|
/* send the message and a fake MDCX to force sending of a dummy packet */
|
2011-01-07 15:54:46 +00:00
|
|
|
bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
|
2010-08-29 15:40:33 +00:00
|
|
|
bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
|
2010-04-22 04:08:17 +00:00
|
|
|
return MGCP_POLICY_DEFER;
|
2010-04-07 07:53:54 +00:00
|
|
|
} else if (state == MGCP_ENDP_DLCX) {
|
2010-04-22 04:08:17 +00:00
|
|
|
/* we will free the endpoint now and send a DLCX to the BSC */
|
2010-05-01 02:31:53 +00:00
|
|
|
msgb_free(bsc_msg);
|
2010-04-22 04:08:17 +00:00
|
|
|
bsc_mgcp_dlcx(sccp);
|
|
|
|
return MGCP_POLICY_CONT;
|
|
|
|
} else {
|
2011-01-07 15:54:46 +00:00
|
|
|
bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
|
2010-04-22 04:08:17 +00:00
|
|
|
return MGCP_POLICY_DEFER;
|
2010-04-01 06:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-06 11:16:34 +00:00
|
|
|
/*
|
|
|
|
* We do have a failure, free data downstream..
|
|
|
|
*/
|
|
|
|
static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
|
|
|
|
struct bsc_connection *bsc)
|
|
|
|
{
|
2010-08-28 08:46:27 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
|
|
|
|
ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
|
|
|
|
|
|
|
|
/* if a CRCX failed... send a DLCX down the stream */
|
|
|
|
if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
|
|
|
|
struct sccp_connections *con;
|
|
|
|
con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
|
|
|
|
if (!con) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR,
|
|
|
|
"No SCCP connection for endp 0x%x\n",
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
} else {
|
|
|
|
if (con->bsc == bsc) {
|
2010-08-29 14:39:07 +00:00
|
|
|
bsc_mgcp_send_dlcx(bsc, con->bsc_endp);
|
2010-08-06 11:16:34 +00:00
|
|
|
} else {
|
2010-08-28 08:46:27 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR,
|
|
|
|
"Endpoint belongs to a different BSC\n");
|
2010-08-06 11:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-28 08:46:27 +00:00
|
|
|
}
|
2010-08-06 11:16:34 +00:00
|
|
|
|
2010-08-28 08:46:27 +00:00
|
|
|
bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
|
|
|
|
mgcp_free_endp(endp);
|
2010-08-06 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
2010-04-01 08:16:28 +00:00
|
|
|
/*
|
|
|
|
* We have received a msg from the BSC. We will see if we know
|
|
|
|
* this transaction and if it belongs to the BSC. Then we will
|
|
|
|
* need to patch the content to point to the local network and we
|
|
|
|
* need to update the I: that was assigned by the BSS.
|
|
|
|
*/
|
|
|
|
void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
|
|
|
|
{
|
|
|
|
struct msgb *output;
|
|
|
|
struct bsc_endpoint *bsc_endp = NULL;
|
|
|
|
struct mgcp_endpoint *endp = NULL;
|
2010-04-06 09:06:11 +00:00
|
|
|
int i, code;
|
2010-04-01 08:16:28 +00:00
|
|
|
char transaction_id[60];
|
|
|
|
|
|
|
|
/* Some assumption that our buffer is big enough.. and null terminate */
|
|
|
|
if (msgb_l2len(msg) > 2000) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h[msgb_l2len(msg)] = '\0';
|
|
|
|
|
|
|
|
if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-27 23:56:17 +00:00
|
|
|
for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
|
2010-04-01 08:16:28 +00:00
|
|
|
if (bsc->nat->bsc_endpoints[i].bsc != bsc)
|
|
|
|
continue;
|
2010-04-05 16:00:05 +00:00
|
|
|
/* no one listening? a bug? */
|
|
|
|
if (!bsc->nat->bsc_endpoints[i].transaction_id)
|
|
|
|
continue;
|
2010-04-01 08:16:28 +00:00
|
|
|
if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
|
|
|
|
continue;
|
|
|
|
|
2011-02-27 23:56:17 +00:00
|
|
|
endp = &bsc->nat->mgcp_cfg->trunk.endpoints[i];
|
2010-04-01 08:16:28 +00:00
|
|
|
bsc_endp = &bsc->nat->bsc_endpoints[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bsc_endp) {
|
2010-04-05 15:58:52 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
|
|
|
|
transaction_id, (const char *) msg->l2h);
|
2010-04-01 08:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
|
2010-08-06 00:34:46 +00:00
|
|
|
if (endp->ci == CI_UNUSED) {
|
2010-08-06 11:16:34 +00:00
|
|
|
free_chan_downstream(endp, bsc_endp, bsc);
|
2010-08-06 00:34:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-04-07 07:53:54 +00:00
|
|
|
|
2010-04-01 08:16:28 +00:00
|
|
|
/* free some stuff */
|
|
|
|
talloc_free(bsc_endp->transaction_id);
|
|
|
|
bsc_endp->transaction_id = NULL;
|
2010-08-06 01:05:05 +00:00
|
|
|
bsc_endp->transaction_state = 0;
|
2010-04-01 08:16:28 +00:00
|
|
|
|
2010-04-07 07:53:54 +00:00
|
|
|
/*
|
|
|
|
* rewrite the information. In case the endpoint was deleted
|
|
|
|
* there should be nothing for us to rewrite so putting endp->rtp_port
|
|
|
|
* with the value of 0 should be no problem.
|
|
|
|
*/
|
2010-08-30 05:44:32 +00:00
|
|
|
output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1,
|
2010-08-04 19:08:35 +00:00
|
|
|
bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
|
2010-04-05 20:56:49 +00:00
|
|
|
|
2010-04-01 08:16:28 +00:00
|
|
|
if (!output) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-06 10:09:47 +00:00
|
|
|
if (osmo_wqueue_enqueue(&bsc->nat->mgcp_cfg->gw_fd, output) != 0) {
|
2010-04-01 08:16:28 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
|
|
|
|
msgb_free(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
|
|
|
|
{
|
|
|
|
/* we want to parse two strings */
|
|
|
|
return sscanf(str, "%3d %59s\n", code, transaction) != 2;
|
|
|
|
}
|
|
|
|
|
2010-08-06 00:26:54 +00:00
|
|
|
uint32_t bsc_mgcp_extract_ci(const char *str)
|
2010-04-01 08:16:28 +00:00
|
|
|
{
|
2010-08-06 00:26:54 +00:00
|
|
|
unsigned int ci;
|
2010-04-01 08:16:28 +00:00
|
|
|
char *res = strstr(str, "I: ");
|
2010-08-06 00:19:05 +00:00
|
|
|
if (!res) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str);
|
2010-04-01 08:16:28 +00:00
|
|
|
return CI_UNUSED;
|
2010-08-06 00:19:05 +00:00
|
|
|
}
|
2010-04-01 08:16:28 +00:00
|
|
|
|
2010-08-06 00:26:54 +00:00
|
|
|
if (sscanf(res, "I: %u", &ci) != 1) {
|
2010-08-06 00:19:05 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str);
|
2010-04-01 08:16:28 +00:00
|
|
|
return CI_UNUSED;
|
2010-08-06 00:19:05 +00:00
|
|
|
}
|
|
|
|
|
2010-04-01 08:16:28 +00:00
|
|
|
return ci;
|
|
|
|
}
|
|
|
|
|
2012-01-17 14:58:48 +00:00
|
|
|
/**
|
|
|
|
* Create a new MGCPCommand based on the input and endpoint from a message
|
|
|
|
*/
|
2010-08-30 05:44:32 +00:00
|
|
|
static void patch_mgcp(struct msgb *output, const char *op, const char *tok,
|
|
|
|
int endp, int len, int cr)
|
|
|
|
{
|
|
|
|
int slen;
|
|
|
|
int ret;
|
|
|
|
char buf[40];
|
|
|
|
|
|
|
|
buf[0] = buf[39] = '\0';
|
|
|
|
ret = sscanf(tok, "%*s %s", buf);
|
2012-01-17 14:58:48 +00:00
|
|
|
if (ret != 1) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR,
|
|
|
|
"Failed to find Endpoint in: %s\n", tok);
|
|
|
|
return;
|
|
|
|
}
|
2010-08-30 05:44:32 +00:00
|
|
|
|
|
|
|
slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s",
|
|
|
|
op, buf, endp, cr ? "\r\n" : "\n");
|
|
|
|
output->l3h = msgb_put(output, slen);
|
|
|
|
}
|
|
|
|
|
2010-04-01 04:48:52 +00:00
|
|
|
/* we need to replace some strings... */
|
2010-08-30 05:44:32 +00:00
|
|
|
struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port)
|
2010-04-01 04:48:52 +00:00
|
|
|
{
|
2012-11-07 10:36:58 +00:00
|
|
|
static const char crcx_str[] = "CRCX ";
|
|
|
|
static const char dlcx_str[] = "DLCX ";
|
|
|
|
static const char mdcx_str[] = "MDCX ";
|
2010-08-30 05:44:32 +00:00
|
|
|
|
2012-11-07 10:36:58 +00:00
|
|
|
static const char ip_str[] = "c=IN IP4 ";
|
|
|
|
static const char aud_str[] = "m=audio ";
|
|
|
|
static const char fmt_str[] = "a=fmtp:";
|
2010-04-01 04:48:52 +00:00
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
char *running, *token;
|
|
|
|
struct msgb *output;
|
|
|
|
|
2012-11-06 12:16:26 +00:00
|
|
|
/* keep state to add the a=fmtp line */
|
|
|
|
int found_fmtp = 0;
|
|
|
|
int payload = -1;
|
|
|
|
int cr = 1;
|
|
|
|
|
|
|
|
if (length > 4096 - 256) {
|
2010-04-01 04:48:52 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
|
|
|
|
if (!output) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-04 16:09:10 +00:00
|
|
|
running = input;
|
2010-04-01 04:48:52 +00:00
|
|
|
output->l2h = output->data;
|
2010-08-30 05:44:32 +00:00
|
|
|
output->l3h = output->l2h;
|
2010-04-04 17:34:44 +00:00
|
|
|
for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
|
2010-04-01 04:48:52 +00:00
|
|
|
int len = strlen(token);
|
2012-11-06 12:16:26 +00:00
|
|
|
cr = len > 0 && token[len - 1] == '\r';
|
2010-04-01 04:48:52 +00:00
|
|
|
|
2010-08-30 05:44:32 +00:00
|
|
|
if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) {
|
|
|
|
patch_mgcp(output, "CRCX", token, endpoint, len, cr);
|
|
|
|
} else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) {
|
|
|
|
patch_mgcp(output, "DLCX", token, endpoint, len, cr);
|
|
|
|
} else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) {
|
|
|
|
patch_mgcp(output, "MDCX", token, endpoint, len, cr);
|
|
|
|
} else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
|
2010-04-01 04:48:52 +00:00
|
|
|
output->l3h = msgb_put(output, strlen(ip_str));
|
|
|
|
memcpy(output->l3h, ip_str, strlen(ip_str));
|
|
|
|
output->l3h = msgb_put(output, strlen(ip));
|
|
|
|
memcpy(output->l3h, ip, strlen(ip));
|
2010-04-04 17:34:44 +00:00
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
output->l3h = msgb_put(output, 2);
|
|
|
|
output->l3h[0] = '\r';
|
|
|
|
output->l3h[1] = '\n';
|
|
|
|
} else {
|
|
|
|
output->l3h = msgb_put(output, 1);
|
|
|
|
output->l3h[0] = '\n';
|
|
|
|
}
|
2010-04-01 04:48:52 +00:00
|
|
|
} else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
|
|
|
|
if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
|
|
|
|
msgb_free(output);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-04 17:34:44 +00:00
|
|
|
snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
|
|
|
|
port, payload, cr ? "\r\n" : "\n");
|
2010-04-01 04:48:52 +00:00
|
|
|
buf[sizeof(buf)-1] = '\0';
|
|
|
|
|
|
|
|
output->l3h = msgb_put(output, strlen(buf));
|
|
|
|
memcpy(output->l3h, buf, strlen(buf));
|
2012-11-06 12:16:26 +00:00
|
|
|
} else if (strncmp(fmt_str, token, (sizeof fmt_str) - 1) == 0) {
|
|
|
|
found_fmtp = 1;
|
|
|
|
goto copy;
|
2010-04-01 04:48:52 +00:00
|
|
|
} else {
|
2012-11-06 12:16:26 +00:00
|
|
|
copy:
|
2010-04-01 04:48:52 +00:00
|
|
|
output->l3h = msgb_put(output, len + 1);
|
|
|
|
memcpy(output->l3h, token, len);
|
|
|
|
output->l3h[len] = '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 12:16:26 +00:00
|
|
|
/*
|
|
|
|
* the above code made sure that we have 128 bytes lefts. So we can
|
|
|
|
* safely append another line.
|
|
|
|
*/
|
|
|
|
if (!found_fmtp && payload != -1) {
|
|
|
|
snprintf(buf, sizeof(buf) - 1, "a=fmtp:%d mode-set=2%s",
|
|
|
|
payload, cr ? "\r\n" : "\n");
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
output->l3h = msgb_put(output, strlen(buf));
|
|
|
|
memcpy(output->l3h, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
2010-04-01 04:48:52 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2011-05-06 10:11:23 +00:00
|
|
|
static int mgcp_do_read(struct osmo_fd *fd)
|
2010-03-31 11:02:22 +00:00
|
|
|
{
|
|
|
|
struct bsc_nat *nat;
|
|
|
|
struct msgb *msg, *resp;
|
|
|
|
int rc;
|
|
|
|
|
2010-04-04 16:09:10 +00:00
|
|
|
nat = fd->data;
|
|
|
|
|
|
|
|
rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
|
|
|
|
if (rc <= 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
|
2010-03-31 11:02:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-04 16:09:10 +00:00
|
|
|
nat->mgcp_msg[rc] = '\0';
|
|
|
|
nat->mgcp_length = rc;
|
2010-03-31 11:02:22 +00:00
|
|
|
|
2010-04-04 16:09:10 +00:00
|
|
|
msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
|
|
|
|
if (!msg) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
|
2010-03-31 11:02:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h = msgb_put(msg, rc);
|
2010-04-04 16:09:10 +00:00
|
|
|
memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
|
2010-03-31 11:02:22 +00:00
|
|
|
resp = mgcp_handle_message(nat->mgcp_cfg, msg);
|
|
|
|
msgb_free(msg);
|
|
|
|
|
|
|
|
/* we do have a direct answer... e.g. AUEP */
|
|
|
|
if (resp) {
|
2011-05-06 10:09:47 +00:00
|
|
|
if (osmo_wqueue_enqueue(&nat->mgcp_cfg->gw_fd, resp) != 0) {
|
2010-03-31 11:02:22 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
|
|
|
|
msgb_free(resp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-06 10:11:23 +00:00
|
|
|
static int mgcp_do_write(struct osmo_fd *bfd, struct msgb *msg)
|
2010-03-31 11:02:22 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = write(bfd->fd, msg->data, msg->len);
|
|
|
|
|
|
|
|
if (rc != msg->len) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
int bsc_mgcp_nat_init(struct bsc_nat *nat)
|
2010-03-31 11:02:22 +00:00
|
|
|
{
|
|
|
|
int on;
|
|
|
|
struct sockaddr_in addr;
|
2010-09-18 13:13:54 +00:00
|
|
|
struct mgcp_config *cfg = nat->mgcp_cfg;
|
2010-03-31 11:02:22 +00:00
|
|
|
|
2010-09-18 13:13:54 +00:00
|
|
|
if (!cfg->call_agent_addr) {
|
2010-03-31 11:02:22 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-18 13:13:54 +00:00
|
|
|
if (cfg->bts_ip) {
|
2010-04-01 06:21:33 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-18 13:13:54 +00:00
|
|
|
cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (cfg->gw_fd.bfd.fd < 0) {
|
2010-03-31 11:02:22 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
on = 1;
|
2010-09-18 13:13:54 +00:00
|
|
|
setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
2010-03-31 11:02:22 +00:00
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
2010-09-18 13:13:54 +00:00
|
|
|
addr.sin_port = htons(cfg->source_port);
|
|
|
|
inet_aton(cfg->source_addr, &addr.sin_addr);
|
2010-03-31 11:02:22 +00:00
|
|
|
|
2010-09-18 13:13:54 +00:00
|
|
|
if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
2011-03-29 15:12:07 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to bind on %s:%d errno: %d\n",
|
|
|
|
cfg->source_addr, cfg->source_port, errno);
|
2010-09-18 13:13:54 +00:00
|
|
|
close(cfg->gw_fd.bfd.fd);
|
|
|
|
cfg->gw_fd.bfd.fd = -1;
|
2010-03-31 11:02:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.sin_port = htons(2727);
|
2010-09-18 13:13:54 +00:00
|
|
|
inet_aton(cfg->call_agent_addr, &addr.sin_addr);
|
|
|
|
if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
2010-03-31 11:02:22 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
|
2010-09-18 13:13:54 +00:00
|
|
|
cfg->call_agent_addr, errno);
|
|
|
|
close(cfg->gw_fd.bfd.fd);
|
|
|
|
cfg->gw_fd.bfd.fd = -1;
|
2010-03-31 11:02:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-06 10:09:47 +00:00
|
|
|
osmo_wqueue_init(&cfg->gw_fd, 10);
|
2010-09-18 13:13:54 +00:00
|
|
|
cfg->gw_fd.bfd.when = BSC_FD_READ;
|
|
|
|
cfg->gw_fd.bfd.data = nat;
|
|
|
|
cfg->gw_fd.read_cb = mgcp_do_read;
|
|
|
|
cfg->gw_fd.write_cb = mgcp_do_write;
|
2010-03-31 11:02:22 +00:00
|
|
|
|
2011-05-06 10:11:23 +00:00
|
|
|
if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) {
|
2010-03-31 11:02:22 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
|
2010-09-18 13:13:54 +00:00
|
|
|
close(cfg->gw_fd.bfd.fd);
|
|
|
|
cfg->gw_fd.bfd.fd = -1;
|
2010-03-31 11:02:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-01 06:21:33 +00:00
|
|
|
/* some more MGCP config handling */
|
2010-09-18 13:13:54 +00:00
|
|
|
cfg->data = nat;
|
|
|
|
cfg->policy_cb = bsc_mgcp_policy_cb;
|
2011-02-27 23:56:17 +00:00
|
|
|
cfg->trunk.force_realloc = 1;
|
2010-08-05 10:08:36 +00:00
|
|
|
|
2010-09-18 13:13:54 +00:00
|
|
|
if (cfg->bts_ip)
|
|
|
|
talloc_free(cfg->bts_ip);
|
|
|
|
cfg->bts_ip = "";
|
2010-08-05 10:08:36 +00:00
|
|
|
|
2010-04-01 06:21:33 +00:00
|
|
|
nat->bsc_endpoints = talloc_zero_array(nat,
|
|
|
|
struct bsc_endpoint,
|
2011-02-27 23:56:17 +00:00
|
|
|
cfg->trunk.number_endpoints + 1);
|
2010-09-18 20:34:04 +00:00
|
|
|
if (!nat->bsc_endpoints) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n");
|
2010-09-18 13:13:54 +00:00
|
|
|
close(cfg->gw_fd.bfd.fd);
|
|
|
|
cfg->gw_fd.bfd.fd = -1;
|
2010-09-18 20:34:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-04-01 06:21:33 +00:00
|
|
|
|
2010-09-18 20:36:07 +00:00
|
|
|
if (mgcp_reset_transcoder(cfg) < 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n");
|
|
|
|
talloc_free(nat->bsc_endpoints);
|
|
|
|
nat->bsc_endpoints = NULL;
|
|
|
|
close(cfg->gw_fd.bfd.fd);
|
|
|
|
cfg->gw_fd.bfd.fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-31 11:02:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-04-05 21:09:27 +00:00
|
|
|
|
|
|
|
void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
|
|
|
|
{
|
2010-06-17 10:29:42 +00:00
|
|
|
struct rate_ctr *ctr = NULL;
|
2010-04-05 21:09:27 +00:00
|
|
|
int i;
|
2010-06-17 10:29:42 +00:00
|
|
|
|
|
|
|
if (bsc->cfg)
|
|
|
|
ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS];
|
|
|
|
|
2011-02-27 23:56:17 +00:00
|
|
|
for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
|
2010-04-05 21:09:27 +00:00
|
|
|
struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
|
|
|
|
|
|
|
|
if (bsc_endp->bsc != bsc)
|
|
|
|
continue;
|
|
|
|
|
2010-06-17 10:29:42 +00:00
|
|
|
if (ctr)
|
|
|
|
rate_ctr_inc(ctr);
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
bsc_mgcp_free_endpoint(bsc->nat, i);
|
2011-02-27 23:56:17 +00:00
|
|
|
mgcp_free_endp(&bsc->nat->mgcp_cfg->trunk.endpoints[i]);
|
2010-04-05 21:09:27 +00:00
|
|
|
}
|
|
|
|
}
|