From d04011abc17f298714213b970cbdf034a1ac2d0d Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Tue, 11 Dec 2012 16:35:01 +0100 Subject: stp: Extract the MGCP forwarding code out of the msc_connection For the ISUP/MGCP handling we will need the same code, extract it from the msc_connection. For the reading code callback is introduced that will pass the MGCP message to the higher layer. --- include/Makefile.am | 2 +- include/mgcp_callagent.h | 35 +++++++++++ include/msc_connection.h | 5 +- src/Makefile.am | 6 +- src/mgcp_callagent.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++ src/msc_conn.c | 127 +++----------------------------------- 6 files changed, 206 insertions(+), 123 deletions(-) create mode 100644 include/mgcp_callagent.h create mode 100644 src/mgcp_callagent.c diff --git a/include/Makefile.am b/include/Makefile.am index c923515..c46e698 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -2,6 +2,6 @@ noinst_HEADERS = mtp_level3.h mtp_data.h ipaccess.h thread.h mtp_pcap.h \ mgcp_ss7.h bss_patch.h bssap_sccp.h bsc_data.h udp_input.h \ snmp_mtp.h cellmgr_debug.h bsc_sccp.h bsc_ussd.h sctp_m2ua.h \ isup_types.h counter.h msc_connection.h ss7_application.h \ - mgcp_patch.h ss7_vty.h dtmf_scheduler.h + mgcp_patch.h ss7_vty.h dtmf_scheduler.h mgcp_callagent.h SUBDIRS = mgcp diff --git a/include/mgcp_callagent.h b/include/mgcp_callagent.h new file mode 100644 index 0000000..8260daa --- /dev/null +++ b/include/mgcp_callagent.h @@ -0,0 +1,35 @@ +/* + * (C) 2012 by Holger Hans Peter Freyther + * (C) 2012 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 . + * + */ + +#ifndef mgcp_callagent_h +#define mgcp_callagent_h + +#include + +struct mgcp_callagent { + struct osmo_wqueue queue; + void (*read_cb)(struct mgcp_callagent *, struct msgb *msg); +}; + +int mgcp_create_port(struct mgcp_callagent *agent); +void mgcp_forward(struct mgcp_callagent *agent, const uint8_t *data, + unsigned int length); + +#endif diff --git a/include/msc_connection.h b/include/msc_connection.h index 94754d7..74319b3 100644 --- a/include/msc_connection.h +++ b/include/msc_connection.h @@ -20,8 +20,9 @@ #ifndef MSC_CONNECTION_H #define MSC_CONNECTION_H +#include "mgcp_callagent.h" + #include -#include #include #include @@ -57,7 +58,7 @@ struct msc_connection { struct osmo_timer_list pong_timeout; /* mgcp messgaes */ - struct osmo_wqueue mgcp_agent; + struct mgcp_callagent mgcp_agent; /* application pointer */ struct ss7_application *app; diff --git a/src/Makefile.am b/src/Makefile.am index b8cacc1..4d6061c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,7 +15,8 @@ cellmgr_ng_SOURCES = main.c mtp_layer3.c thread.c input/ipaccess.c pcap.c \ bss_patch.c bssap_sccp.c bsc_sccp.c bsc_ussd.c links.c \ msc_conn.c link_udp.c snmp_mtp.c debug.c isup.c \ mtp_link.c counter.c sccp_state.c bsc.c ss7_application.c \ - vty_interface_legacy.c vty_interface_cmds.c mgcp_patch.c + vty_interface_legacy.c vty_interface_cmds.c mgcp_patch.c \ + mgcp_callagent.c cellmgr_ng_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(LIBOSMOVTY_LIBS) \ $(LIBOSMOSCCP_LIBS) $(NEXUSWARE_C7_LIBS) \ -lpthread -lnetsnmp -lcrypto @@ -24,7 +25,8 @@ osmo_stp_SOURCES = main_stp.c mtp_layer3.c thread.c pcap.c link_udp.c snmp_mtp.c debug.c links.c isup.c sctp_m2ua.c msc_conn.c sccp_state.c \ bss_patch.c bssap_sccp.c bsc_sccp.c bsc_ussd.c input/ipaccess.c \ mtp_link.c counter.c bsc.c ss7_application.c \ - vty_interface.c vty_interface_cmds.c mgcp_patch.c + vty_interface.c vty_interface_cmds.c mgcp_patch.c \ + mgcp_callagent.c osmo_stp_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(LIBOSMOVTY_LIBS) \ $(LIBOSMOSCCP_LIBS) $(NEXUSWARE_C7_LIBS) \ -lpthread -lnetsnmp -lcrypto -lm2ua -lsctp diff --git a/src/mgcp_callagent.c b/src/mgcp_callagent.c new file mode 100644 index 0000000..06efb5a --- /dev/null +++ b/src/mgcp_callagent.c @@ -0,0 +1,154 @@ +/* + * (C) 2010-2012 by Holger Hans Peter Freyther + * (C) 2010-2012 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 int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) +{ + int ret; + + LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); + + ret = write(fd->fd, msg->data, msg->len); + if (ret != msg->len) { + LOGP(DMGCP, LOGL_ERROR, + "Failed to forward message to MGCP GW (%s).\n", + strerror(errno)); + } + + return ret; +} + +static int mgcp_do_read(struct osmo_fd *fd) +{ + struct mgcp_callagent *agent; + struct msgb *mgcp; + int ret; + + agent = fd->data; + + mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); + if (!mgcp) { + LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); + return -1; + } + + ret = read(fd->fd, mgcp->data, 4096 - 128); + if (ret <= 0) { + LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); + msgb_free(mgcp); + return -1; + } else if (ret > 4096 - 128) { + LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret); + msgb_free(mgcp); + return -1; + } + + mgcp->l2h = msgb_put(mgcp, ret); + agent->read_cb(agent, mgcp); + return 0; +} + +int mgcp_create_port(struct mgcp_callagent *agent) +{ + int on; + struct sockaddr_in addr; + + agent->queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); + if (agent->queue.bfd.fd < 0) { + LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno); + return -1; + } + + on = 1; + setsockopt(agent->queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + + /* try to bind the socket */ + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = 0; + + if (bind(agent->queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n"); + close(agent->queue.bfd.fd); + agent->queue.bfd.fd = -1; + return -1; + } + + /* connect to the remote */ + addr.sin_port = htons(2427); + if (connect(agent->queue.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) { + LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno)); + close(agent->queue.bfd.fd); + agent->queue.bfd.fd = -1; + return -1; + } + + osmo_wqueue_init(&agent->queue, 10); + agent->queue.bfd.data = agent; + agent->queue.bfd.when = BSC_FD_READ; + agent->queue.read_cb = mgcp_do_read; + agent->queue.write_cb = mgcp_do_write; + + if (osmo_fd_register(&agent->queue.bfd) != 0) { + LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n"); + close(agent->queue.bfd.fd); + agent->queue.bfd.fd = -1; + return -1; + } + + return 0; +} + +void mgcp_forward(struct mgcp_callagent *agent, const uint8_t *data, + unsigned int length) +{ + struct msgb *mgcp; + + if (length > 4096) { + LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n"); + return; + } + + mgcp = msgb_alloc(4096, "mgcp_to_gw"); + if (!mgcp) { + LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n"); + return; + } + + msgb_put(mgcp, length); + memcpy(mgcp->data, data, mgcp->len); + if (osmo_wqueue_enqueue(&agent->queue, mgcp) != 0) { + LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n"); + msgb_free(mgcp); + } +} + diff --git a/src/msc_conn.c b/src/msc_conn.c index edfd521..b52b901 100644 --- a/src/msc_conn.c +++ b/src/msc_conn.c @@ -1,7 +1,7 @@ /* MSC related stuff... */ /* - * (C) 2010-2011 by Holger Hans Peter Freyther - * (C) 2010-2011 by On-Waves + * (C) 2010-2012 by Holger Hans Peter Freyther + * (C) 2010-2012 by On-Waves * All Rights Reserved * * This program is free software: you can redistribute it and/or modify @@ -49,7 +49,6 @@ static void msc_send_id_response(struct msc_connection *bsc); static void msc_send(struct msc_connection *bsc, struct msgb *msg, int proto); static void msc_schedule_reconnect(struct msc_connection *bsc); -static void mgcp_forward(struct msc_connection *fw, const uint8_t *data, unsigned int length); void msc_close_connection(struct msc_connection *fw) { @@ -163,7 +162,7 @@ static int ipaccess_a_fd_cb(struct osmo_fd *bfd) msc_dispatch_sccp(fw, msg); } else if (hh->proto == NAT_MUX) { msg = mgcp_patch(fw->app, msg); - mgcp_forward(fw, msg->l2h, msgb_l2len(msg)); + mgcp_forward(&fw->mgcp_agent, msg->l2h, msgb_l2len(msg)); } else { LOGP(DMSC, LOGL_ERROR, "Unknown IPA proto 0x%x\n", hh->proto); } @@ -348,122 +347,13 @@ void msc_mgcp_reset(struct msc_connection *msc) snprintf(buf, sizeof(buf) - 1, "RSIP 1 13@%s MGCP 1.0\r\n", dest); buf[sizeof(buf) - 1] = '\0'; - mgcp_forward(msc, (const uint8_t *) buf, strlen(buf)); + mgcp_forward(&msc->mgcp_agent, (const uint8_t *) buf, strlen(buf)); } -static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) +static void msc_mgcp_read_cb(struct mgcp_callagent *agent, struct msgb *msg) { - int ret; - - LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); - - ret = write(fd->fd, msg->data, msg->len); - if (ret != msg->len) - LOGP(DMGCP, LOGL_ERROR, "Failed to forward message to MGCP GW (%s).\n", strerror(errno)); - - return ret; -} - -static int mgcp_do_read(struct osmo_fd *fd) -{ - struct msgb *mgcp; - int ret; - - mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); - if (!mgcp) { - LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); - return -1; - } - - ret = read(fd->fd, mgcp->data, 4096 - 128); - if (ret <= 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); - msgb_free(mgcp); - return -1; - } else if (ret > 4096 - 128) { - LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret); - msgb_free(mgcp); - return -1; - } - - mgcp->l2h = msgb_put(mgcp, ret); - msc_send(fd->data, mgcp, NAT_MUX); - return 0; -} - -static void mgcp_forward(struct msc_connection *fw, const uint8_t *data, unsigned int length) -{ - struct msgb *mgcp; - - if (length > 4096) { - LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n"); - return; - } - - mgcp = msgb_alloc(4096, "mgcp_to_gw"); - if (!mgcp) { - LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n"); - return; - } - - msgb_put(mgcp, length); - memcpy(mgcp->data, data, mgcp->len); - if (osmo_wqueue_enqueue(&fw->mgcp_agent, mgcp) != 0) { - LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n"); - msgb_free(mgcp); - } -} - -static int mgcp_create_port(struct msc_connection *fw) -{ - int on; - struct sockaddr_in addr; - - fw->mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); - if (fw->mgcp_agent.bfd.fd < 0) { - LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno); - return -1; - } - - on = 1; - setsockopt(fw->mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - - /* try to bind the socket */ - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - addr.sin_port = 0; - - if (bind(fw->mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n"); - close(fw->mgcp_agent.bfd.fd); - fw->mgcp_agent.bfd.fd = -1; - return -1; - } - - /* connect to the remote */ - addr.sin_port = htons(2427); - if (connect(fw->mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) { - LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno)); - close(fw->mgcp_agent.bfd.fd); - fw->mgcp_agent.bfd.fd = -1; - return -1; - } - - osmo_wqueue_init(&fw->mgcp_agent, 10); - fw->mgcp_agent.bfd.data = fw; - fw->mgcp_agent.bfd.when = BSC_FD_READ; - fw->mgcp_agent.read_cb = mgcp_do_read; - fw->mgcp_agent.write_cb = mgcp_do_write; - - if (osmo_fd_register(&fw->mgcp_agent.bfd) != 0) { - LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n"); - close(fw->mgcp_agent.bfd.fd); - fw->mgcp_agent.bfd.fd = -1; - return -1; - } - - return 0; + struct msc_connection *fw = container_of(agent, struct msc_connection, mgcp_agent); + msc_send(fw, msg, NAT_MUX); } static void msc_send(struct msc_connection *fw, struct msgb *msg, int proto) @@ -561,7 +451,7 @@ struct msc_connection *msc_connection_create(struct bsc_data *bsc, int mgcp) msc->pong_timeout.data = msc; /* create MGCP port */ - if (mgcp && mgcp_create_port(msc) != 0) { + if (mgcp && mgcp_create_port(&msc->mgcp_agent) != 0) { LOGP(DMSC, LOGL_ERROR, "Failed to bind for the MGCP port.\n"); talloc_free(msc); return NULL; @@ -569,6 +459,7 @@ struct msc_connection *msc_connection_create(struct bsc_data *bsc, int mgcp) llist_add_tail(&msc->entry, &bsc->mscs); msc->nr = bsc->num_mscs++; + msc->mgcp_agent.read_cb = msc_mgcp_read_cb; return msc; } -- cgit v1.2.3