From 89579b4317a7f7ab6ee706399bee4b8f25a12c3a Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 4 Mar 2011 13:18:30 +0100 Subject: prefix sub-directories containing libraries with 'lib' ... and make sure tests work again after restructuring --- openbsc/src/mgcp/Makefile.am | 7 - openbsc/src/mgcp/mgcp_network.c | 579 -------------------- openbsc/src/mgcp/mgcp_protocol.c | 1102 -------------------------------------- openbsc/src/mgcp/mgcp_vty.c | 742 ------------------------- 4 files changed, 2430 deletions(-) delete mode 100644 openbsc/src/mgcp/Makefile.am delete mode 100644 openbsc/src/mgcp/mgcp_network.c delete mode 100644 openbsc/src/mgcp/mgcp_protocol.c delete mode 100644 openbsc/src/mgcp/mgcp_vty.c (limited to 'openbsc/src/mgcp') diff --git a/openbsc/src/mgcp/Makefile.am b/openbsc/src/mgcp/Makefile.am deleted file mode 100644 index b1d1d158a..000000000 --- a/openbsc/src/mgcp/Makefile.am +++ /dev/null @@ -1,7 +0,0 @@ -INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir) -AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(COVERAGE_CFLAGS) -AM_LDFLAGS = $(LIBOSMOCORE_LIBS) $(COVERAGE_LDFLAGS) - -noinst_LIBRARIES = libmgcp.a - -libmgcp_a_SOURCES = mgcp_protocol.c mgcp_network.c mgcp_vty.c diff --git a/openbsc/src/mgcp/mgcp_network.c b/openbsc/src/mgcp/mgcp_network.c deleted file mode 100644 index 51c08d151..000000000 --- a/openbsc/src/mgcp/mgcp_network.c +++ /dev/null @@ -1,579 +0,0 @@ -/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ -/* The protocol implementation */ - -/* - * (C) 2009-2011 by Holger Hans Peter Freyther - * (C) 2009-2011 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 Affero General Public License as published by - * the Free Software Foundation; either version 3 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include -#include -#include - -#warning "Make use of the rtp proxy code" - -/* attempt to determine byte order */ -#include -#include -#include - -#ifndef __BYTE_ORDER -#error "__BYTE_ORDER should be defined by someone" -#endif - -/* according to rtp_proxy.c RFC 3550 */ -struct rtp_hdr { -#if __BYTE_ORDER == __LITTLE_ENDIAN - uint8_t csrc_count:4, - extension:1, - padding:1, - version:2; - uint8_t payload_type:7, - marker:1; -#elif __BYTE_ORDER == __BIG_ENDIAN - uint8_t version:2, - padding:1, - extension:1, - csrc_count:4; - uint8_t marker:1, - payload_type:7; -#endif - uint16_t sequence; - uint32_t timestamp; - uint32_t ssrc; -} __attribute__((packed)); - - -enum { - DEST_NETWORK = 0, - DEST_BTS = 1, -}; - -enum { - PROTO_RTP, - PROTO_RTCP, -}; - -#define DUMMY_LOAD 0x23 - - -static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) -{ - struct sockaddr_in out; - out.sin_family = AF_INET; - out.sin_port = port; - memcpy(&out.sin_addr, addr, sizeof(*addr)); - - return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); -} - -int mgcp_send_dummy(struct mgcp_endpoint *endp) -{ - static char buf[] = { DUMMY_LOAD }; - - return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, - endp->net_end.rtp_port, buf, 1); -} - -static void patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, - int payload, struct sockaddr_in *addr, char *data, int len) -{ - uint16_t seq; - uint32_t timestamp; - struct rtp_hdr *rtp_hdr; - - if (len < sizeof(*rtp_hdr)) - return; - - rtp_hdr = (struct rtp_hdr *) data; - seq = ntohs(rtp_hdr->sequence); - timestamp = ntohl(rtp_hdr->timestamp); - - if (!state->initialized) { - state->seq_no = seq - 1; - state->ssrc = state->orig_ssrc = rtp_hdr->ssrc; - state->initialized = 1; - state->last_timestamp = timestamp; - } else if (state->ssrc != rtp_hdr->ssrc) { - state->ssrc = rtp_hdr->ssrc; - state->seq_offset = (state->seq_no + 1) - seq; - state->timestamp_offset = state->last_timestamp - timestamp; - state->patch = endp->allow_patch; - LOGP(DMGCP, LOGL_NOTICE, - "The SSRC changed on 0x%x SSRC: %u offset: %d from %s:%d in %d\n", - ENDPOINT_NUMBER(endp), state->ssrc, state->seq_offset, - inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), endp->conn_mode); - } - - /* apply the offset and store it back to the packet */ - if (state->patch) { - seq += state->seq_offset; - rtp_hdr->sequence = htons(seq); - rtp_hdr->ssrc = state->orig_ssrc; - - timestamp += state->timestamp_offset; - rtp_hdr->timestamp = htonl(timestamp); - } - - /* seq changed, now compare if we have lost something */ - if (state->seq_no + 1u != seq) - state->lost_no = abs(seq - (state->seq_no + 1)); - state->seq_no = seq; - - state->last_timestamp = timestamp; - - if (payload < 0) - return; - - rtp_hdr->payload_type = payload; -} - -/* - * The below code is for dispatching. We have a dedicated port for - * the data coming from the net and one to discover the BTS. - */ -static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len) -{ - if (!tap->enabled) - return 0; - - return sendto(fd, buf, len, 0, - (struct sockaddr *)&tap->forward, sizeof(tap->forward)); -} - -static int send_transcoder(struct mgcp_rtp_end *end, struct mgcp_config *cfg, - int is_rtp, const char *buf, int len) -{ - int rc; - int port; - struct sockaddr_in addr; - - port = is_rtp ? end->rtp_port : end->rtcp_port; - - addr.sin_family = AF_INET; - addr.sin_addr = cfg->transcoder_in; - addr.sin_port = port; - - rc = sendto(is_rtp ? - end->rtp.fd : - end->rtcp.fd, buf, len, 0, - (struct sockaddr *) &addr, sizeof(addr)); - - if (rc != len) - LOGP(DMGCP, LOGL_ERROR, - "Failed to send data to the transcoder: %s\n", - strerror(errno)); - - return rc; -} - -static int send_to(struct mgcp_endpoint *endp, int dest, int is_rtp, - struct sockaddr_in *addr, char *buf, int rc) -{ - struct mgcp_trunk_config *tcfg = endp->tcfg; - /* For loop toggle the destination and then dispatch. */ - if (tcfg->audio_loop) - dest = !dest; - - /* Loop based on the conn_mode, maybe undoing the above */ - if (endp->conn_mode == MGCP_CONN_LOOPBACK) - dest = !dest; - - if (dest == DEST_NETWORK) { - if (is_rtp) { - patch_and_count(endp, &endp->bts_state, - endp->net_end.payload_type, - addr, buf, rc); - forward_data(endp->net_end.rtp.fd, - &endp->taps[MGCP_TAP_NET_OUT], buf, rc); - return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, - endp->net_end.rtp_port, buf, rc); - } else { - return udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr, - endp->net_end.rtcp_port, buf, rc); - } - } else { - if (is_rtp) { - patch_and_count(endp, &endp->net_state, - endp->bts_end.payload_type, - addr, buf, rc); - forward_data(endp->bts_end.rtp.fd, - &endp->taps[MGCP_TAP_BTS_OUT], buf, rc); - return udp_send(endp->bts_end.rtp.fd, &endp->bts_end.addr, - endp->bts_end.rtp_port, buf, rc); - } else { - return udp_send(endp->bts_end.rtcp.fd, &endp->bts_end.addr, - endp->bts_end.rtcp_port, buf, rc); - } - } -} - -static int recevice_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, - char *buf, int bufsize) -{ - int rc; - socklen_t slen = sizeof(*addr); - - rc = recvfrom(fd, buf, bufsize, 0, - (struct sockaddr *) addr, &slen); - if (rc < 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", - ENDPOINT_NUMBER(endp), errno, strerror(errno)); - return -1; - } - - /* do not forward aynthing... maybe there is a packet from the bts */ - if (!endp->allocated) - return -1; - - #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." - - return rc; -} - -static int rtp_data_net(struct bsc_fd *fd, unsigned int what) -{ - char buf[4096]; - struct sockaddr_in addr; - struct mgcp_endpoint *endp; - int rc, proto; - - endp = (struct mgcp_endpoint *) fd->data; - - rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); - if (rc <= 0) - return -1; - - if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { - LOGP(DMGCP, LOGL_ERROR, - "Data from wrong address %s on 0x%x\n", - inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); - return -1; - } - - if (endp->net_end.rtp_port != addr.sin_port && - endp->net_end.rtcp_port != addr.sin_port) { - LOGP(DMGCP, LOGL_ERROR, - "Data from wrong source port %d on 0x%x\n", - ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); - return -1; - } - - /* throw away the dummy message */ - if (rc == 1 && buf[0] == DUMMY_LOAD) { - LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", - ENDPOINT_NUMBER(endp)); - return 0; - } - - proto = fd == &endp->net_end.rtp ? PROTO_RTP : PROTO_RTCP; - endp->net_end.packets += 1; - - forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc); - if (endp->is_transcoded) - return send_transcoder(&endp->trans_net, endp->cfg, proto == PROTO_RTP, &buf[0], rc); - else - return send_to(endp, DEST_BTS, proto == PROTO_RTP, &addr, &buf[0], rc); -} - -static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr) -{ - struct mgcp_config *cfg = endp->cfg; - - if (proto == PROTO_RTP && endp->bts_end.rtp_port == 0) { - if (!cfg->bts_ip || - memcmp(&addr->sin_addr, - &cfg->bts_in, sizeof(cfg->bts_in)) == 0 || - memcmp(&addr->sin_addr, - &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) { - - endp->bts_end.rtp_port = addr->sin_port; - endp->bts_end.addr = addr->sin_addr; - - LOGP(DMGCP, LOGL_NOTICE, - "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", - ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), - ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr)); - } - } else if (proto == PROTO_RTCP && endp->bts_end.rtcp_port == 0) { - if (memcmp(&endp->bts_end.addr, &addr->sin_addr, - sizeof(endp->bts_end.addr)) == 0) { - endp->bts_end.rtcp_port = addr->sin_port; - } - } -} - -static int rtp_data_bts(struct bsc_fd *fd, unsigned int what) -{ - char buf[4096]; - struct sockaddr_in addr; - struct mgcp_endpoint *endp; - struct mgcp_config *cfg; - int rc, proto; - - endp = (struct mgcp_endpoint *) fd->data; - cfg = endp->cfg; - - rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); - if (rc <= 0) - return -1; - - proto = fd == &endp->bts_end.rtp ? PROTO_RTP : PROTO_RTCP; - - /* We have no idea who called us, maybe it is the BTS. */ - /* it was the BTS... */ - discover_bts(endp, proto, &addr); - - if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { - LOGP(DMGCP, LOGL_ERROR, - "Data from wrong bts %s on 0x%x\n", - inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); - return -1; - } - - if (endp->bts_end.rtp_port != addr.sin_port && - endp->bts_end.rtcp_port != addr.sin_port) { - LOGP(DMGCP, LOGL_ERROR, - "Data from wrong bts source port %d on 0x%x\n", - ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); - return -1; - } - - /* throw away the dummy message */ - if (rc == 1 && buf[0] == DUMMY_LOAD) { - LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n", - ENDPOINT_NUMBER(endp)); - return 0; - } - - /* do this before the loop handling */ - endp->bts_end.packets += 1; - - forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc); - if (endp->is_transcoded) - return send_transcoder(&endp->trans_bts, endp->cfg, proto == PROTO_RTP, &buf[0], rc); - else - return send_to(endp, DEST_NETWORK, proto == PROTO_RTP, &addr, &buf[0], rc); -} - -static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp, - int dest, struct bsc_fd *fd) -{ - char buf[4096]; - struct sockaddr_in addr; - struct mgcp_config *cfg; - int rc, proto; - - cfg = _endp->cfg; - rc = recevice_from(_endp, fd->fd, &addr, buf, sizeof(buf)); - if (rc <= 0) - return -1; - - proto = fd == &end->rtp ? PROTO_RTP : PROTO_RTCP; - - if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) { - LOGP(DMGCP, LOGL_ERROR, - "Data not coming from transcoder dest: %d %s on 0x%x\n", - dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp)); - return -1; - } - - if (end->rtp_port != addr.sin_port && - end->rtcp_port != addr.sin_port) { - LOGP(DMGCP, LOGL_ERROR, - "Data from wrong transcoder dest %d source port %d on 0x%x\n", - dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp)); - return -1; - } - - /* throw away the dummy message */ - if (rc == 1 && buf[0] == DUMMY_LOAD) { - LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n", - dest, ENDPOINT_NUMBER(_endp)); - return 0; - } - - end->packets += 1; - return send_to(_endp, dest, proto == PROTO_RTP, &addr, &buf[0], rc); -} - -static int rtp_data_trans_net(struct bsc_fd *fd, unsigned int what) -{ - struct mgcp_endpoint *endp; - endp = (struct mgcp_endpoint *) fd->data; - - return rtp_data_transcoder(&endp->trans_net, endp, DEST_NETWORK, fd); -} - -static int rtp_data_trans_bts(struct bsc_fd *fd, unsigned int what) -{ - struct mgcp_endpoint *endp; - endp = (struct mgcp_endpoint *) fd->data; - - return rtp_data_transcoder(&endp->trans_bts, endp, DEST_BTS, fd); -} - -static int create_bind(const char *source_addr, struct bsc_fd *fd, int port) -{ - struct sockaddr_in addr; - int on = 1; - - fd->fd = socket(AF_INET, SOCK_DGRAM, 0); - if (fd->fd < 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); - return -1; - } - - setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - inet_aton(source_addr, &addr.sin_addr); - - if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - close(fd->fd); - fd->fd = -1; - return -1; - } - - return 0; -} - -static int set_ip_tos(int fd, int tos) -{ - int ret; - ret = setsockopt(fd, IPPROTO_IP, IP_TOS, - &tos, sizeof(tos)); - return ret != 0; -} - -static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno) -{ - if (create_bind(cfg->source_addr, &rtp_end->rtp, rtp_end->local_port) != 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", - cfg->source_addr, rtp_end->local_port, endpno); - goto cleanup0; - } - - if (create_bind(cfg->source_addr, &rtp_end->rtcp, rtp_end->local_port + 1) != 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", - cfg->source_addr, rtp_end->local_port + 1, endpno); - goto cleanup1; - } - - set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); - set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); - - rtp_end->rtp.when = BSC_FD_READ; - if (bsc_register_fd(&rtp_end->rtp) != 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", - rtp_end->local_port, endpno); - goto cleanup2; - } - - rtp_end->rtcp.when = BSC_FD_READ; - if (bsc_register_fd(&rtp_end->rtcp) != 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", - rtp_end->local_port + 1, endpno); - goto cleanup3; - } - - return 0; - -cleanup3: - bsc_unregister_fd(&rtp_end->rtp); -cleanup2: - close(rtp_end->rtcp.fd); - rtp_end->rtcp.fd = -1; -cleanup1: - close(rtp_end->rtp.fd); - rtp_end->rtp.fd = -1; -cleanup0: - return -1; -} - -static int int_bind(const char *port, - struct mgcp_rtp_end *end, int (*cb)(struct bsc_fd *, unsigned), - struct mgcp_endpoint *_endp, int rtp_port) -{ - if (end->rtp.fd != -1 || end->rtcp.fd != -1) { - LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n", - port, ENDPOINT_NUMBER(_endp)); - mgcp_free_rtp_port(end); - } - - end->local_port = rtp_port; - end->rtp.cb = cb; - end->rtp.data = _endp; - end->rtcp.data = _endp; - end->rtcp.cb = cb; - return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp)); -} - - -int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) -{ - return int_bind("bts-port", &endp->bts_end, - rtp_data_bts, endp, rtp_port); -} - -int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) -{ - return int_bind("net-port", &endp->net_end, - rtp_data_net, endp, rtp_port); -} - -int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) -{ - return int_bind("trans-net", &endp->trans_net, - rtp_data_trans_net, endp, rtp_port); -} - -int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) -{ - return int_bind("trans-bts", &endp->trans_bts, - rtp_data_trans_bts, endp, rtp_port); -} - -int mgcp_free_rtp_port(struct mgcp_rtp_end *end) -{ - if (end->rtp.fd != -1) { - close(end->rtp.fd); - end->rtp.fd = -1; - bsc_unregister_fd(&end->rtp); - } - - if (end->rtcp.fd != -1) { - close(end->rtcp.fd); - end->rtcp.fd = -1; - bsc_unregister_fd(&end->rtcp); - } - - return 0; -} diff --git a/openbsc/src/mgcp/mgcp_protocol.c b/openbsc/src/mgcp/mgcp_protocol.c deleted file mode 100644 index ba290dd90..000000000 --- a/openbsc/src/mgcp/mgcp_protocol.c +++ /dev/null @@ -1,1102 +0,0 @@ -/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ -/* The protocol implementation */ - -/* - * (C) 2009-2011 by Holger Hans Peter Freyther - * (C) 2009-2011 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 Affero General Public License as published by - * the Free Software Foundation; either version 3 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -/** - * 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; \ - } \ - } - -static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end); - -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); -static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg); - -static void create_transcoder(struct mgcp_endpoint *endp); -static void delete_transcoder(struct mgcp_endpoint *endp); - -static uint32_t generate_call_id(struct mgcp_config *cfg) -{ - 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->trunk.number_endpoints; ++i) - if (cfg->trunk.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") - MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest") - - /* 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; -} - -struct msgb *mgcp_create_response_with_data(int code, const char *txt, - const char *msg, const char *trans, - const char *data) -{ - int len; - struct msgb *res; - - res = mgcp_msgb_alloc(); - if (!res) - return NULL; - - if (data) { - len = snprintf((char *) res->data, 2048, "%d %s%s\r\n%s", code, trans, txt, data); - } else { - len = snprintf((char *) res->data, 2048, "%d %s%s\r\n", code, trans, txt); - } - - res->l2h = msgb_put(res, len); - LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h); - return res; -} - -static struct msgb *create_ok_response(int code, const char *msg, const char *trans) -{ - 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); -} - -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, - "I: %u\n\n" - "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->net_end.local_port, - endp->bts_end.payload_type, endp->bts_end.payload_type, - endp->tcfg->audio_name); - return mgcp_create_response_with_data(200, " OK", msg, trans_id, sdp_record); -} - -/* - * 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; -} - -/** - * 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; - struct mgcp_trunk_config *tcfg; - int trunk, endp; - - trunk = strtoul(mgcp + 6, &rest, 10); - if (rest == NULL || rest[0] != '/' || trunk < 1) { - 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 endpoint name '%s'\n", mgcp); - return NULL; - } - - /* signalling is on timeslot 1 */ - if (endp == 1) - return NULL; - - tcfg = mgcp_trunk_num(cfg, trunk); - if (!tcfg) { - LOGP(DMGCP, LOGL_ERROR, "The trunk %d is not declared.\n", trunk); - return NULL; - } - - if (!tcfg->endpoints) { - LOGP(DMGCP, LOGL_ERROR, "Endpoints of trunk %d not allocated.\n", trunk); - return NULL; - } - - if (endp < 1 || endp >= tcfg->number_endpoints) { - LOGP(DMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp); - return NULL; - } - - return &tcfg->endpoints[endp]; -} - -static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp) -{ - char *endptr = NULL; - unsigned int gw = INT_MAX; - - if (strncmp(mgcp, "ds/e1", 5) == 0) { - return find_e1_endpoint(cfg, mgcp); - } else { - gw = strtoul(mgcp, &endptr, 16); - if (gw > 0 && gw < cfg->trunk.number_endpoints && strcmp(endptr, "@mgw") == 0) - return &cfg->trunk.endpoints[gw]; - } - - LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp); - return NULL; -} - -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, - const char *_ci) -{ - 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); - return -1; - } - - return 0; -} - -static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg) -{ - struct mgcp_msg_ptr data_ptrs[6]; - int found; - 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) - return create_err_response(500, "AUEP", trans_id); - else - return create_ok_response(200, "AUEP", trans_id); -} - -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; - else if (strcmp(msg, "sendonly") == 0) - *conn_mode = MGCP_CONN_SEND_ONLY; - else if (strcmp(msg, "loopback") == 0) - *conn_mode = MGCP_CONN_LOOPBACK; - else { - LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg); - ret = -1; - } - - return ret; -} - -static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_rtp_end *end, - struct mgcp_port_range *range, - int (*alloc)(struct mgcp_endpoint *endp, int port)) -{ - int i; - - if (range->mode == PORT_ALLOC_STATIC) { - end->local_alloc = PORT_ALLOC_STATIC; - return 0; - } - - /* attempt to find a port */ - for (i = 0; i < 200; ++i) { - int rc; - - if (range->last_port >= range->range_end) - range->last_port = range->range_start; - - rc = alloc(endp, range->last_port); - - range->last_port += 2; - if (rc == 0) { - end->local_alloc = PORT_ALLOC_DYNAMIC; - return 0; - } - - } - - LOGP(DMGCP, LOGL_ERROR, "Allocating a RTP/RTCP port failed 200 times 0x%x.\n", - ENDPOINT_NUMBER(endp)); - return -1; -} - -static int allocate_ports(struct mgcp_endpoint *endp) -{ - if (allocate_port(endp, &endp->net_end, &endp->cfg->net_ports, - mgcp_bind_net_rtp_port) != 0) - return -1; - - if (allocate_port(endp, &endp->bts_end, &endp->cfg->bts_ports, - mgcp_bind_bts_rtp_port) != 0) { - mgcp_rtp_end_reset(&endp->net_end); - return -1; - } - - if (endp->cfg->transcoder_ip && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) { - if (allocate_port(endp, &endp->trans_net, - &endp->cfg->transcoder_ports, - mgcp_bind_trans_net_rtp_port) != 0) { - mgcp_rtp_end_reset(&endp->net_end); - mgcp_rtp_end_reset(&endp->bts_end); - return -1; - } - - if (allocate_port(endp, &endp->trans_bts, - &endp->cfg->transcoder_ports, - mgcp_bind_trans_bts_rtp_port) != 0) { - mgcp_rtp_end_reset(&endp->net_end); - mgcp_rtp_end_reset(&endp->bts_end); - mgcp_rtp_end_reset(&endp->trans_net); - return -1; - } - - /* remember that we have set up transcoding */ - endp->is_transcoded = 1; - } - - return 0; -} - -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_trunk_config *tcfg; - struct mgcp_endpoint *endp; - int error_code = 400; - - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(510, "CRCX", trans_id); - - tcfg = endp->tcfg; - - if (endp->allocated) { - if (tcfg->force_realloc) { - LOGP(DMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n", - ENDPOINT_NUMBER(endp)); - mgcp_free_endp(endp); - if (cfg->realloc_cb) - cfg->realloc_cb(tcfg, ENDPOINT_NUMBER(endp)); - } else { - LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", - ENDPOINT_NUMBER(endp)); - return create_err_response(400, "CRCX", trans_id); - } - } - - /* parse CallID C: and LocalParameters L: */ - MSG_TOKENIZE_START - switch (msg->l3h[line_start]) { - case 'L': - endp->local_options = talloc_strdup(tcfg->endpoints, - (const char *)&msg->l3h[line_start + 3]); - break; - case 'C': - endp->callid = talloc_strdup(tcfg->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; - } - - endp->orig_mode = endp->conn_mode; - 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_end.rtp_port = endp->net_end.rtcp_port = endp->bts_end.rtp_port = endp->bts_end.rtcp_port = 0; - - /* set to zero until we get the info */ - memset(&endp->net_end.addr, 0, sizeof(endp->net_end.addr)); - - /* bind to the port now */ - if (allocate_ports(endp) != 0) - goto error2; - - /* assign a local call identifier or fail */ - endp->ci = generate_call_id(cfg); - if (endp->ci == CI_UNUSED) - goto error2; - - endp->allocated = 1; - endp->bts_end.payload_type = tcfg->audio_payload; - - /* policy CB */ - if (cfg->policy_cb) { - switch (cfg->policy_cb(tcfg, 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); - return create_err_response(400, "CRCX", trans_id); - break; - case MGCP_POLICY_DEFER: - /* stop processing */ - create_transcoder(endp); - return NULL; - break; - case MGCP_POLICY_CONT: - /* just continue */ - break; - } - } - - LOGP(DMGCP, LOGL_DEBUG, "Creating endpoint on: 0x%x CI: %u port: %u/%u\n", - ENDPOINT_NUMBER(endp), endp->ci, - endp->net_end.local_port, endp->bts_end.local_port); - if (cfg->change_cb) - cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); - - create_transcoder(endp); - 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); - return create_err_response(error_code, "CRCX", trans_id); - -error2: - mgcp_free_endp(endp); - LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(error_code, "CRCX", trans_id); -} - -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) - return create_err_response(510, "MDCX", trans_id); - - if (endp->ci == CI_UNUSED) { - LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(400, "MDCX", trans_id); - } - - 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; - } - endp->orig_mode = endp->conn_mode; - 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_end.rtp_port = htons(port); - endp->net_end.rtcp_port = htons(port + 1); - endp->net_end.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->net_end.addr); - } - 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(endp->tcfg, 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; - return create_err_response(400, "MDCX", trans_id); - break; - case MGCP_POLICY_DEFER: - /* stop processing */ - return NULL; - break; - case MGCP_POLICY_CONT: - /* just continue */ - break; - } - } - - /* modify */ - LOGP(DMGCP, LOGL_DEBUG, "Modified endpoint on: 0x%x Server: %s:%u\n", - ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port)); - if (cfg->change_cb) - cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX); - 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]); - return create_err_response(error_code, "MDCX", trans_id); - -error3: - return create_err_response(error_code, "MDCX", trans_id); - - -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 = 400; - int silent = 0; - - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(error_code, "DLCX", trans_id); - - if (!endp->allocated) { - LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(400, "DLCX", trans_id); - } - - 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(endp->tcfg, 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; - return create_err_response(400, "DLCX", trans_id); - break; - case MGCP_POLICY_DEFER: - /* stop processing */ - delete_transcoder(endp); - return NULL; - break; - case MGCP_POLICY_CONT: - /* just continue */ - break; - } - } - - /* free the connection */ - LOGP(DMGCP, LOGL_DEBUG, "Deleted endpoint on: 0x%x Server: %s:%u\n", - ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port)); - - delete_transcoder(endp); - mgcp_free_endp(endp); - if (cfg->change_cb) - cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX); - - if (silent) - goto out_silent; - return create_ok_response(250, "DLCX", 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); - return create_err_response(error_code, "DLCX", trans_id); - -error3: - return create_err_response(error_code, "DLCX", trans_id); - -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; -} - -/* - * This can request like DTMF detection and forward, fax detection... it - * can also request when the notification should be send and such. We don't - * do this right now. - */ -static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg) -{ - struct mgcp_msg_ptr data_ptrs[6]; - const char *trans_id; - struct mgcp_endpoint *endp; - int found; - - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(400, "RQNT", trans_id); - - if (!endp->allocated) { - LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(400, "RQNT", trans_id); - } - return create_ok_response(200, "RQNT", trans_id); -} - -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->transcoder_remote_base = 4000; - - cfg->bts_ports.base_port = RTP_PORT_DEFAULT; - cfg->net_ports.base_port = RTP_PORT_NET_DEFAULT; - - /* default trunk handling */ - cfg->trunk.cfg = cfg; - cfg->trunk.trunk_nr = 0; - cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL; - cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000"); - cfg->trunk.audio_payload = 126; - - INIT_LLIST_HEAD(&cfg->trunks); - - return cfg; -} - -struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr) -{ - struct mgcp_trunk_config *trunk; - - trunk = talloc_zero(cfg, struct mgcp_trunk_config); - if (!trunk) { - LOGP(DMGCP, LOGL_ERROR, "Failed to allocate.\n"); - return NULL; - } - - trunk->cfg = cfg; - trunk->trunk_type = MGCP_TRUNK_E1; - trunk->trunk_nr = nr; - trunk->audio_name = talloc_strdup(cfg, "AMR/8000"); - trunk->audio_payload = 126; - trunk->number_endpoints = 33; - llist_add_tail(&trunk->entry, &cfg->trunks); - return trunk; -} - -struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index) -{ - struct mgcp_trunk_config *trunk; - - llist_for_each_entry(trunk, &cfg->trunks, entry) - if (trunk->trunk_nr == index) - return trunk; - - return NULL; -} - -static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end) -{ - if (end->local_alloc == PORT_ALLOC_DYNAMIC) { - mgcp_free_rtp_port(end); - end->local_port = 0; - } - - end->packets = 0; - memset(&end->addr, 0, sizeof(end->addr)); - end->rtp_port = end->rtcp_port = 0; - end->payload_type = -1; - end->local_alloc = -1; -} - -static void mgcp_rtp_end_init(struct mgcp_rtp_end *end) -{ - mgcp_rtp_end_reset(end); - end->rtp.fd = -1; - end->rtcp.fd = -1; -} - -int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg) -{ - int i; - - /* Initialize all endpoints */ - tcfg->endpoints = _talloc_zero_array(tcfg->cfg, - sizeof(struct mgcp_endpoint), - tcfg->number_endpoints, "endpoints"); - if (!tcfg->endpoints) - return -1; - - for (i = 0; i < tcfg->number_endpoints; ++i) { - tcfg->endpoints[i].ci = CI_UNUSED; - tcfg->endpoints[i].cfg = tcfg->cfg; - tcfg->endpoints[i].tcfg = tcfg; - mgcp_rtp_end_init(&tcfg->endpoints[i].net_end); - mgcp_rtp_end_init(&tcfg->endpoints[i].bts_end); - mgcp_rtp_end_init(&tcfg->endpoints[i].trans_net); - mgcp_rtp_end_init(&tcfg->endpoints[i].trans_bts); - } - - return 0; -} - -void mgcp_free_endp(struct mgcp_endpoint *endp) -{ - LOGP(DMGCP, LOGL_DEBUG, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp)); - endp->ci = CI_UNUSED; - endp->allocated = 0; - - if (endp->callid) { - talloc_free(endp->callid); - endp->callid = NULL; - } - - if (endp->local_options) { - talloc_free(endp->local_options); - endp->local_options = NULL; - } - - mgcp_rtp_end_reset(&endp->bts_end); - mgcp_rtp_end_reset(&endp->net_end); - mgcp_rtp_end_reset(&endp->trans_net); - mgcp_rtp_end_reset(&endp->trans_bts); - endp->is_transcoded = 0; - - 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; - endp->allow_patch = 0; - - memset(&endp->taps, 0, sizeof(endp->taps)); -} - -static int send_trans(struct mgcp_config *cfg, const char *buf, int len) -{ - struct sockaddr_in addr; - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr = cfg->transcoder_in; - addr.sin_port = htons(2427); - return sendto(cfg->gw_fd.bfd.fd, buf, len, 0, - (struct sockaddr *) &addr, sizeof(addr)); -} - -static void send_msg(struct mgcp_endpoint *endp, int endpoint, int port, - const char *msg, const char *mode) -{ - char buf[2096]; - int len; - - /* hardcoded to AMR right now, we do not know the real type at this point */ - len = snprintf(buf, sizeof(buf), - "%s 42 %x@mgw MGCP 1.0\r\n" - "C: 4256\r\n" - "M: %s\r\n" - "\r\n" - "c=IN IP4 %s\r\n" - "m=audio %d RTP/AVP %d\r\n" - "a=rtpmap:%d %s\r\n", - msg, endpoint, mode, endp->cfg->source_addr, - port, endp->tcfg->audio_payload, - endp->tcfg->audio_payload, endp->tcfg->audio_name); - - if (len < 0) - return; - - buf[sizeof(buf) - 1] = '\0'; - - send_trans(endp->cfg, buf, len); -} - -static void send_dlcx(struct mgcp_endpoint *endp, int endpoint) -{ - char buf[2096]; - int len; - - len = snprintf(buf, sizeof(buf), - "DLCX 43 %x@mgw MGCP 1.0\r\n" - "C: 4256\r\n" - , endpoint); - - if (len < 0) - return; - - buf[sizeof(buf) - 1] = '\0'; - - send_trans(endp->cfg, buf, len); -} - -static void create_transcoder(struct mgcp_endpoint *endp) -{ - int port; - int in_endp = ENDPOINT_NUMBER(endp); - int out_endp = endp_back_channel(in_endp); - - if (!endp->is_transcoded) - return; - - send_msg(endp, in_endp, endp->trans_bts.local_port, "CRCX", "sendrecv"); - send_msg(endp, in_endp, endp->trans_bts.local_port, "MDCX", "sendrecv"); - send_msg(endp, out_endp, endp->trans_net.local_port, "CRCX", "sendrecv"); - send_msg(endp, out_endp, endp->trans_net.local_port, "MDCX", "sendrecv"); - - port = rtp_calculate_port(in_endp, endp->cfg->transcoder_remote_base); - endp->trans_bts.rtp_port = htons(port); - endp->trans_bts.rtcp_port = htons(port + 1); - - port = rtp_calculate_port(out_endp, endp->cfg->transcoder_remote_base); - endp->trans_net.rtp_port = htons(port); - endp->trans_net.rtcp_port = htons(port + 1); -} - -static void delete_transcoder(struct mgcp_endpoint *endp) -{ - int in_endp = ENDPOINT_NUMBER(endp); - int out_endp = endp_back_channel(in_endp); - - if (!endp->is_transcoded) - return; - - send_dlcx(endp, in_endp); - send_dlcx(endp, out_endp); -} - -int mgcp_reset_transcoder(struct mgcp_config *cfg) -{ - if (!cfg->transcoder_ip) - return 0; - - static const char mgcp_reset[] = { - "RSIP 1 13@mgw MGCP 1.0\r\n" - }; - - return send_trans(cfg, mgcp_reset, sizeof mgcp_reset -1); -} diff --git a/openbsc/src/mgcp/mgcp_vty.c b/openbsc/src/mgcp/mgcp_vty.c deleted file mode 100644 index c299a98cc..000000000 --- a/openbsc/src/mgcp/mgcp_vty.c +++ /dev/null @@ -1,742 +0,0 @@ -/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ -/* The protocol implementation */ - -/* - * (C) 2009-2011 by Holger Hans Peter Freyther - * (C) 2009-2011 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 Affero General Public License as published by - * the Free Software Foundation; either version 3 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include - -static struct mgcp_config *g_cfg = NULL; - -static struct mgcp_trunk_config *find_trunk(struct mgcp_config *cfg, int nr) -{ - struct mgcp_trunk_config *trunk; - - if (nr == 0) - trunk = &cfg->trunk; - else - trunk = mgcp_trunk_num(cfg, nr); - - return trunk; -} - -/* - * vty code for mgcp below - */ -struct cmd_node mgcp_node = { - MGCP_NODE, - "%s(mgcp)#", - 1, -}; - -struct cmd_node trunk_node = { - TRUNK_NODE, - "%s(trunk)#", - 1, -}; - -static int config_write_mgcp(struct vty *vty) -{ - vty_out(vty, "mgcp%s", VTY_NEWLINE); - if (g_cfg->local_ip) - vty_out(vty, " local ip %s%s", g_cfg->local_ip, VTY_NEWLINE); - if (g_cfg->bts_ip && strlen(g_cfg->bts_ip) != 0) - vty_out(vty, " bts ip %s%s", g_cfg->bts_ip, VTY_NEWLINE); - vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE); - vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE); - - if (g_cfg->bts_ports.mode == PORT_ALLOC_STATIC) - vty_out(vty, " rtp bts-base %u%s", g_cfg->bts_ports.base_port, VTY_NEWLINE); - else - vty_out(vty, " rtp bts-range %u %u%s", - g_cfg->bts_ports.range_start, g_cfg->bts_ports.range_end, VTY_NEWLINE); - - if (g_cfg->net_ports.mode == PORT_ALLOC_STATIC) - vty_out(vty, " rtp net-base %u%s", g_cfg->net_ports.base_port, VTY_NEWLINE); - else - vty_out(vty, " rtp net-range %u %u%s", - g_cfg->net_ports.range_start, g_cfg->net_ports.range_end, VTY_NEWLINE); - - vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE); - if (g_cfg->trunk.audio_payload != -1) - vty_out(vty, " sdp audio payload number %d%s", - g_cfg->trunk.audio_payload, VTY_NEWLINE); - if (g_cfg->trunk.audio_name) - vty_out(vty, " sdp audio payload name %s%s", - g_cfg->trunk.audio_name, VTY_NEWLINE); - vty_out(vty, " loop %u%s", !!g_cfg->trunk.audio_loop, VTY_NEWLINE); - vty_out(vty, " number endpoints %u%s", g_cfg->trunk.number_endpoints - 1, VTY_NEWLINE); - if (g_cfg->call_agent_addr) - vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE); - if (g_cfg->transcoder_ip) - vty_out(vty, " transcoder-mgw %s%s", g_cfg->transcoder_ip, VTY_NEWLINE); - - if (g_cfg->transcoder_ports.mode == PORT_ALLOC_STATIC) - vty_out(vty, " rtp transcoder-base %u%s", g_cfg->transcoder_ports.base_port, VTY_NEWLINE); - else - vty_out(vty, " rtp transcoder-range %u %u%s", - g_cfg->transcoder_ports.range_start, g_cfg->transcoder_ports.range_end, VTY_NEWLINE); - vty_out(vty, " transcoder-remote-base %u%s", g_cfg->transcoder_remote_base, VTY_NEWLINE); - - return CMD_SUCCESS; -} - -static void dump_trunk(struct vty *vty, struct mgcp_trunk_config *cfg) -{ - int i; - - vty_out(vty, "%s trunk nr %d with %d endpoints:%s", - cfg->trunk_type == MGCP_TRUNK_VIRTUAL ? "Virtual" : "E1", - cfg->trunk_nr, cfg->number_endpoints - 1, VTY_NEWLINE); - - if (!cfg->endpoints) { - vty_out(vty, "No endpoints allocated yet.%s", VTY_NEWLINE); - return; - } - - for (i = 1; i < cfg->number_endpoints; ++i) { - struct mgcp_endpoint *endp = &cfg->endpoints[i]; - vty_out(vty, - " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s " - "traffic received bts: %u/%u remote: %u/%u transcoder: %u/%u%s", - i, endp->ci, - ntohs(endp->net_end.rtp_port), ntohs(endp->net_end.rtcp_port), - ntohs(endp->bts_end.rtp_port), ntohs(endp->bts_end.rtcp_port), - inet_ntoa(endp->bts_end.addr), - endp->bts_end.packets, endp->bts_state.lost_no, - endp->net_end.packets, endp->net_state.lost_no, - endp->trans_net.packets, endp->trans_bts.packets, - VTY_NEWLINE); - } -} - -DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp", - SHOW_STR "Display information about the MGCP Media Gateway") -{ - struct mgcp_trunk_config *trunk; - - dump_trunk(vty, &g_cfg->trunk); - - llist_for_each_entry(trunk, &g_cfg->trunks, entry) - dump_trunk(vty, trunk); - - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp, - cfg_mgcp_cmd, - "mgcp", - "Configure the MGCP") -{ - vty->node = MGCP_NODE; - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_local_ip, - cfg_mgcp_local_ip_cmd, - "local ip A.B.C.D", - "Set the IP to be used in SDP records") -{ - bsc_replace_string(g_cfg, &g_cfg->local_ip, argv[0]); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_bts_ip, - cfg_mgcp_bts_ip_cmd, - "bts ip A.B.C.D", - "Set the IP of the BTS for RTP forwarding") -{ - bsc_replace_string(g_cfg, &g_cfg->bts_ip, argv[0]); - inet_aton(g_cfg->bts_ip, &g_cfg->bts_in); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_bind_ip, - cfg_mgcp_bind_ip_cmd, - "bind ip A.B.C.D", - "Bind the MGCP to this local addr") -{ - bsc_replace_string(g_cfg, &g_cfg->source_addr, argv[0]); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_bind_port, - cfg_mgcp_bind_port_cmd, - "bind port <0-65534>", - "Bind the MGCP to this port") -{ - unsigned int port = atoi(argv[0]); - g_cfg->source_port = port; - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_bind_early, - cfg_mgcp_bind_early_cmd, - "bind early (0|1)", - "Bind all RTP ports early") -{ - vty_out(vty, "bind early is deprecated, remove it from the config.\n"); - return CMD_WARNING; -} - -static void parse_base(struct mgcp_port_range *range, const char **argv) -{ - unsigned int port = atoi(argv[0]); - range->mode = PORT_ALLOC_STATIC; - range->base_port = port; -} - -static void parse_range(struct mgcp_port_range *range, const char **argv) -{ - range->mode = PORT_ALLOC_DYNAMIC; - range->range_start = atoi(argv[0]); - range->range_end = atoi(argv[1]); - range->last_port = g_cfg->bts_ports.range_start; -} - - -DEFUN(cfg_mgcp_rtp_bts_base_port, - cfg_mgcp_rtp_bts_base_port_cmd, - "rtp bts-base <0-65534>", - "Base port to use") -{ - parse_base(&g_cfg->bts_ports, argv); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_rtp_bts_range, - cfg_mgcp_rtp_bts_range_cmd, - "rtp bts-range <0-65534> <0-65534>", - "Range of ports to allocate for endpoints\n" - "Start of the range of ports\n" "End of the range of ports\n") -{ - parse_range(&g_cfg->bts_ports, argv); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_rtp_net_range, - cfg_mgcp_rtp_net_range_cmd, - "rtp net-range <0-65534> <0-65534>", - "Range of ports to allocate for endpoints\n" - "Start of the range of ports\n" "End of the range of ports\n") -{ - parse_range(&g_cfg->net_ports, argv); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_rtp_net_base_port, - cfg_mgcp_rtp_net_base_port_cmd, - "rtp net-base <0-65534>", - "Base port to use for network port\n" "Port\n") -{ - parse_base(&g_cfg->net_ports, argv); - return CMD_SUCCESS; -} - -ALIAS_DEPRECATED(cfg_mgcp_rtp_bts_base_port, cfg_mgcp_rtp_base_port_cmd, - "rtp base <0-65534>", "Base port to use") - -DEFUN(cfg_mgcp_rtp_transcoder_range, - cfg_mgcp_rtp_transcoder_range_cmd, - "rtp transcoder-range <0-65534> <0-65534>", - "Range of ports to allocate for the transcoder\n" - "Start of the range of ports\n" "End of the range of ports\n") -{ - parse_range(&g_cfg->transcoder_ports, argv); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_rtp_transcoder_base, - cfg_mgcp_rtp_transcoder_base_cmd, - "rtp transcoder-base <0-65534>", - "Base port for the transcoder range\n" "Port\n") -{ - parse_base(&g_cfg->transcoder_ports, argv); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_rtp_ip_dscp, - cfg_mgcp_rtp_ip_dscp_cmd, - "rtp ip-dscp <0-255>", - "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.") -{ - int dscp = atoi(argv[0]); - g_cfg->endp_dscp = dscp; - return CMD_SUCCESS; -} - -ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd, - "rtp ip-tos <0-255>", - "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.") - - -DEFUN(cfg_mgcp_sdp_payload_number, - cfg_mgcp_sdp_payload_number_cmd, - "sdp audio payload number <1-255>", - "Set the audio codec to use") -{ - unsigned int payload = atoi(argv[0]); - g_cfg->trunk.audio_payload = payload; - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_sdp_payload_name, - cfg_mgcp_sdp_payload_name_cmd, - "sdp audio payload name NAME", - "Set the audio name to use") -{ - bsc_replace_string(g_cfg, &g_cfg->trunk.audio_name, argv[0]); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_loop, - cfg_mgcp_loop_cmd, - "loop (0|1)", - "Loop the audio") -{ - g_cfg->trunk.audio_loop = atoi(argv[0]); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_number_endp, - cfg_mgcp_number_endp_cmd, - "number endpoints <0-65534>", - "The number of endpoints to allocate. This is not dynamic.") -{ - /* + 1 as we start counting at one */ - g_cfg->trunk.number_endpoints = atoi(argv[0]) + 1; - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_agent_addr, - cfg_mgcp_agent_addr_cmd, - "call agent ip IP", - "Set the address of the call agent.") -{ - bsc_replace_string(g_cfg, &g_cfg->call_agent_addr, argv[0]); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_transcoder, - cfg_mgcp_transcoder_cmd, - "transcoder-mgw A.B.C.D", - "Use a MGW to detranscoder RTP\n" - "The IP address of the MGW") -{ - bsc_replace_string(g_cfg, &g_cfg->transcoder_ip, argv[0]); - inet_aton(g_cfg->transcoder_ip, &g_cfg->transcoder_in); - - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_no_transcoder, - cfg_mgcp_no_transcoder_cmd, - NO_STR "transcoder-mgw", - "Disable the transcoding\n") -{ - if (g_cfg->transcoder_ip) { - LOGP(DMGCP, LOGL_NOTICE, "Disabling transcoding on future calls.\n"); - talloc_free(g_cfg->transcoder_ip); - g_cfg->transcoder_ip = NULL; - } - - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_transcoder_remote_base, - cfg_mgcp_transcoder_remote_base_cmd, - "transcoder-remote-base <0-65534>", - "Set the base port for the transcoder\n" "The RTP base port on the transcoder") -{ - g_cfg->transcoder_remote_base = atoi(argv[0]); - return CMD_SUCCESS; -} - -DEFUN(cfg_mgcp_trunk, cfg_mgcp_trunk_cmd, - "trunk <1-64>", - "Configure a SS7 trunk\n" "Trunk Nr\n") -{ - struct mgcp_trunk_config *trunk; - int index = atoi(argv[0]); - - trunk = mgcp_trunk_num(g_cfg, index); - if (!trunk) - trunk = mgcp_trunk_alloc(g_cfg, index); - - if (!trunk) { - vty_out(vty, "%%Unable to allocate trunk %u.%s", - index, VTY_NEWLINE); - return CMD_WARNING; - } - - vty->node = TRUNK_NODE; - vty->index = trunk; - return CMD_SUCCESS; -} - -static int config_write_trunk(struct vty *vty) -{ - struct mgcp_trunk_config *trunk; - - llist_for_each_entry(trunk, &g_cfg->trunks, entry) { - vty_out(vty, " trunk %d%s", trunk->trunk_nr, VTY_NEWLINE); - vty_out(vty, " sdp audio payload number %d%s", - trunk->audio_payload, VTY_NEWLINE); - vty_out(vty, " sdp audio payload name %s%s", - trunk->audio_name, VTY_NEWLINE); - vty_out(vty, " loop %d%s", - trunk->audio_loop, VTY_NEWLINE); - } - - return CMD_SUCCESS; -} - -DEFUN(cfg_trunk_payload_number, - cfg_trunk_payload_number_cmd, - "sdp audio payload number <1-255>", - "SDP related\n" "Audio\n" "Payload\n" "Payload Number\n") -{ - struct mgcp_trunk_config *trunk = vty->index; - unsigned int payload = atoi(argv[0]); - - trunk->audio_payload = payload; - return CMD_SUCCESS; -} - -DEFUN(cfg_trunk_payload_name, - cfg_trunk_payload_name_cmd, - "sdp audio payload name NAME", - "SDP related\n" "Audio\n" "Payload\n" "Payload Name\n") -{ - struct mgcp_trunk_config *trunk = vty->index; - - bsc_replace_string(g_cfg, &trunk->audio_name, argv[0]); - return CMD_SUCCESS; -} - -DEFUN(cfg_trunk_loop, - cfg_trunk_loop_cmd, - "loop (0|1)", - "Loop the audio") -{ - struct mgcp_trunk_config *trunk = vty->index; - - trunk->audio_loop = atoi(argv[0]); - return CMD_SUCCESS; -} - -DEFUN(loop_endp, - loop_endp_cmd, - "loop-endpoint <0-64> NAME (0|1)", - "Loop a given endpoint\n" "Trunk number\n" - "The name in hex of the endpoint\n" "Disable the loop\n" "Enable the loop\n") -{ - struct mgcp_trunk_config *trunk; - struct mgcp_endpoint *endp; - - trunk = find_trunk(g_cfg, atoi(argv[0])); - if (!trunk) { - vty_out(vty, "%%Trunk %d not found in the config.%s", - atoi(argv[0]), VTY_NEWLINE); - return CMD_WARNING; - } - - if (!trunk->endpoints) { - vty_out(vty, "%%Trunk %d has no endpoints allocated.%s", - trunk->trunk_nr, VTY_NEWLINE); - return CMD_WARNING; - } - - int endp_no = strtoul(argv[1], NULL, 16); - if (endp_no < 1 || endp_no >= trunk->number_endpoints) { - vty_out(vty, "Loopback number %s/%d is invalid.%s", - argv[1], endp_no, VTY_NEWLINE); - return CMD_WARNING; - } - - - endp = &trunk->endpoints[endp_no]; - int loop = atoi(argv[2]); - - if (loop) - endp->conn_mode = MGCP_CONN_LOOPBACK; - else - endp->conn_mode = endp->orig_mode; - endp->allow_patch = 1; - - return CMD_SUCCESS; -} - -DEFUN(tap_call, - tap_call_cmd, - "tap-call <0-64> ENDPOINT (bts-in|bts-out|net-in|net-out) A.B.C.D <0-65534>", - "Forward data on endpoint to a different system\n" "Trunk number\n" - "The endpoint in hex\n" - "Forward the data coming from the bts\n" - "Forward the data coming from the bts leaving to the network\n" - "Forward the data coming from the net\n" - "Forward the data coming from the net leaving to the bts\n" - "destination IP of the data\n" "destination port\n") -{ - struct mgcp_rtp_tap *tap; - struct mgcp_trunk_config *trunk; - struct mgcp_endpoint *endp; - int port = 0; - - trunk = find_trunk(g_cfg, atoi(argv[0])); - if (!trunk) { - vty_out(vty, "%%Trunk %d not found in the config.%s", - atoi(argv[0]), VTY_NEWLINE); - return CMD_WARNING; - } - - if (!trunk->endpoints) { - vty_out(vty, "%%Trunk %d has no endpoints allocated.%s", - trunk->trunk_nr, VTY_NEWLINE); - return CMD_WARNING; - } - - int endp_no = strtoul(argv[1], NULL, 16); - if (endp_no < 1 || endp_no >= trunk->number_endpoints) { - vty_out(vty, "Endpoint number %s/%d is invalid.%s", - argv[1], endp_no, VTY_NEWLINE); - return CMD_WARNING; - } - - endp = &trunk->endpoints[endp_no]; - - if (strcmp(argv[2], "bts-in") == 0) { - port = MGCP_TAP_BTS_IN; - } else if (strcmp(argv[2], "bts-out") == 0) { - port = MGCP_TAP_BTS_OUT; - } else if (strcmp(argv[2], "net-in") == 0) { - port = MGCP_TAP_NET_IN; - } else if (strcmp(argv[2], "net-out") == 0) { - port = MGCP_TAP_NET_OUT; - } else { - vty_out(vty, "Unknown mode... tricked vty?%s", VTY_NEWLINE); - return CMD_WARNING; - } - - tap = &endp->taps[port]; - memset(&tap->forward, 0, sizeof(tap->forward)); - inet_aton(argv[3], &tap->forward.sin_addr); - tap->forward.sin_port = htons(atoi(argv[4])); - tap->enabled = 1; - return CMD_SUCCESS; -} - -DEFUN(free_endp, free_endp_cmd, - "free-endpoint <0-64> NUMBER", - "Free the given endpoint\n" "Trunk number\n" - "Endpoint number in hex.\n") -{ - struct mgcp_trunk_config *trunk; - struct mgcp_endpoint *endp; - - trunk = find_trunk(g_cfg, atoi(argv[0])); - if (!trunk) { - vty_out(vty, "%%Trunk %d not found in the config.%s", - atoi(argv[0]), VTY_NEWLINE); - return CMD_WARNING; - } - - if (!trunk->endpoints) { - vty_out(vty, "%%Trunk %d has no endpoints allocated.%s", - trunk->trunk_nr, VTY_NEWLINE); - return CMD_WARNING; - } - - int endp_no = strtoul(argv[1], NULL, 16); - if (endp_no < 1 || endp_no >= trunk->number_endpoints) { - vty_out(vty, "Endpoint number %s/%d is invalid.%s", - argv[1], endp_no, VTY_NEWLINE); - return CMD_WARNING; - } - - endp = &trunk->endpoints[endp_no]; - mgcp_free_endp(endp); - return CMD_SUCCESS; -} - -int mgcp_vty_init(void) -{ - install_element_ve(&show_mgcp_cmd); - install_element(ENABLE_NODE, &loop_endp_cmd); - install_element(ENABLE_NODE, &tap_call_cmd); - install_element(ENABLE_NODE, &free_endp_cmd); - - install_element(CONFIG_NODE, &cfg_mgcp_cmd); - install_node(&mgcp_node, config_write_mgcp); - - install_default(MGCP_NODE); - install_element(MGCP_NODE, &ournode_exit_cmd); - install_element(MGCP_NODE, &ournode_end_cmd); - install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd); - install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd); - install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd); - install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd); - install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_bts_base_port_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_net_base_port_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_bts_range_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_net_range_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_transcoder_range_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_transcoder_base_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd); - install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd); - install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd); - install_element(MGCP_NODE, &cfg_mgcp_transcoder_cmd); - install_element(MGCP_NODE, &cfg_mgcp_no_transcoder_cmd); - install_element(MGCP_NODE, &cfg_mgcp_transcoder_remote_base_cmd); - install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd); - install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd); - install_element(MGCP_NODE, &cfg_mgcp_loop_cmd); - install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd); - - install_element(MGCP_NODE, &cfg_mgcp_trunk_cmd); - install_node(&trunk_node, config_write_trunk); - install_default(TRUNK_NODE); - install_element(TRUNK_NODE, &ournode_exit_cmd); - install_element(TRUNK_NODE, &ournode_end_cmd); - install_element(TRUNK_NODE, &cfg_trunk_payload_number_cmd); - install_element(TRUNK_NODE, &cfg_trunk_payload_name_cmd); - install_element(TRUNK_NODE, &cfg_trunk_loop_cmd); - - return 0; -} - -static int allocate_trunk(struct mgcp_trunk_config *trunk) -{ - int i; - struct mgcp_config *cfg = trunk->cfg; - - if (mgcp_endpoints_allocate(trunk) != 0) { - LOGP(DMGCP, LOGL_ERROR, - "Failed to allocate %d endpoints on trunk %d.\n", - trunk->number_endpoints, trunk->trunk_nr); - return -1; - } - - /* early bind */ - for (i = 1; i < trunk->number_endpoints; ++i) { - struct mgcp_endpoint *endp = &trunk->endpoints[i]; - - if (cfg->bts_ports.mode == PORT_ALLOC_STATIC) { - cfg->last_bts_port += 2; - if (mgcp_bind_bts_rtp_port(endp, cfg->last_bts_port) != 0) { - LOGP(DMGCP, LOGL_FATAL, - "Failed to bind: %d\n", cfg->last_bts_port); - return -1; - } - endp->bts_end.local_alloc = PORT_ALLOC_STATIC; - } - - if (cfg->net_ports.mode == PORT_ALLOC_STATIC) { - cfg->last_net_port += 2; - if (mgcp_bind_net_rtp_port(endp, cfg->last_net_port) != 0) { - LOGP(DMGCP, LOGL_FATAL, - "Failed to bind: %d\n", cfg->last_net_port); - return -1; - } - endp->net_end.local_alloc = PORT_ALLOC_STATIC; - } - - if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL && - cfg->transcoder_ip && cfg->transcoder_ports.mode == PORT_ALLOC_STATIC) { - int rtp_port; - - /* network side */ - rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), - cfg->transcoder_ports.base_port); - if (mgcp_bind_trans_net_rtp_port(endp, rtp_port) != 0) { - LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port); - return -1; - } - endp->trans_net.local_alloc = PORT_ALLOC_STATIC; - - /* bts side */ - rtp_port = rtp_calculate_port(endp_back_channel(ENDPOINT_NUMBER(endp)), - cfg->transcoder_ports.base_port); - if (mgcp_bind_trans_bts_rtp_port(endp, rtp_port) != 0) { - LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port); - return -1; - } - endp->trans_bts.local_alloc = PORT_ALLOC_STATIC; - } - } - - return 0; -} - -int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg) -{ - int rc; - struct mgcp_trunk_config *trunk; - - g_cfg = cfg; - rc = vty_read_config_file(config_file, NULL); - if (rc < 0) { - fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); - return rc; - } - - - if (!g_cfg->bts_ip) - fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n"); - - if (!g_cfg->source_addr) { - fprintf(stderr, "You need to specify a bind address.\n"); - return -1; - } - - /* initialize the last ports */ - g_cfg->last_bts_port = rtp_calculate_port(0, g_cfg->bts_ports.base_port); - g_cfg->last_net_port = rtp_calculate_port(0, g_cfg->net_ports.base_port); - - if (allocate_trunk(&g_cfg->trunk) != 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to initialize the virtual trunk.\n"); - return -1; - } - - llist_for_each_entry(trunk, &g_cfg->trunks, entry) { - if (allocate_trunk(trunk) != 0) { - LOGP(DMGCP, LOGL_ERROR, - "Failed to initialize E1 trunk %d.\n", trunk->trunk_nr); - return -1; - } - } - - return 0; -} - -- cgit v1.2.3