osmo-mgw: Use libosmocore socket abstraction

There's no need for us to use the sockets API directly:  We have
pretty nice socket helper functions in libosmocore, let's make
use of them.

Change-Id: I39d47b8a27f683060a2facf2dbecff8d00c19ce9
This commit is contained in:
Harald Welte 2017-11-17 15:09:30 +01:00
parent 5dbfc78aff
commit 15f64b5199
3 changed files with 36 additions and 118 deletions

View File

@ -24,6 +24,7 @@
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/byteswap.h>
#include <osmocom/core/socket.h>
#include <osmocom/mgcp_client/mgcp_client.h>
#include <osmocom/mgcp_client/mgcp_client_internal.h>
@ -445,7 +446,6 @@ struct mgcp_client *mgcp_client_init(void *ctx,
int mgcp_client_connect(struct mgcp_client *mgcp)
{
int on;
struct sockaddr_in addr;
struct osmo_wqueue *wq;
int rc;
@ -457,46 +457,19 @@ int mgcp_client_connect(struct mgcp_client *mgcp)
wq = &mgcp->wq;
wq->bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
if (wq->bfd.fd < 0) {
LOGP(DLMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
return -errno;
}
on = 1;
if (setsockopt(wq->bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
mgcp->actual.local_addr, mgcp->actual.local_port,
mgcp->actual.remote_addr, mgcp->actual.remote_port,
OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
if (rc < 0) {
LOGP(DLMGCP, LOGL_FATAL,
"Failed to initialize socket for MGCP GW: %s\n",
strerror(errno));
rc = -errno;
"Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
mgcp->actual.local_addr, mgcp->actual.local_port,
mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
goto error_close_fd;
}
/* bind socket */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
inet_aton(mgcp->actual.local_addr, &addr.sin_addr);
addr.sin_port = htons(mgcp->actual.local_port);
if (bind(wq->bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
LOGP(DLMGCP, LOGL_FATAL,
"Failed to bind for MGCP GW to %s %u\n",
mgcp->actual.local_addr, mgcp->actual.local_port);
rc = -errno;
goto error_close_fd;
}
/* connect to the remote */
inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
addr.sin_port = htons(mgcp->actual.remote_port);
if (connect(wq->bfd.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
LOGP(DLMGCP, LOGL_FATAL,
"Failed to connect to MGCP GW at %s %u: %s\n",
mgcp->actual.remote_addr, mgcp->actual.remote_port,
strerror(errno));
rc = -errno;
goto error_close_fd;
}
mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
osmo_wqueue_init(wq, 10);
@ -505,11 +478,6 @@ int mgcp_client_connect(struct mgcp_client *mgcp)
wq->read_cb = mgcp_do_read;
wq->write_cb = mgcp_do_write;
if (osmo_fd_register(&wq->bfd) != 0) {
LOGP(DLMGCP, LOGL_FATAL, "Failed to register BFD\n");
rc = -EIO;
goto error_close_fd;
}
LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
mgcp->actual.local_addr, mgcp->actual.local_port,
mgcp->actual.remote_addr, mgcp->actual.remote_port);

View File

@ -27,7 +27,6 @@
#include <errno.h>
#include <time.h>
#include <limits.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <osmocom/core/msgb.h>
@ -1118,41 +1117,17 @@ int mgcp_set_ip_tos(int fd, int tos)
* \returns 0 on success, -1 on ERROR */
int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
{
struct sockaddr_in addr;
int on = 1;
int rc;
fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd->fd < 0) {
LOGP(DRTP, LOGL_ERROR, "failed to create UDP port (%s:%i).\n",
source_addr, port);
return -1;
} else {
LOGP(DRTP, LOGL_DEBUG,
"created UDP port (%s:%i).\n", source_addr, port);
}
if (setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
LOGP(DRTP, LOGL_ERROR,
"failed to set socket options (%s:%i).\n", source_addr,
port);
return -1;
}
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;
rc = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, source_addr, port,
NULL, 0, OSMO_SOCK_F_BIND);
if (rc < 0) {
LOGP(DRTP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
source_addr, port);
return -1;
} else {
LOGP(DRTP, LOGL_DEBUG,
"bound UDP port (%s:%i).\n", source_addr, port);
}
fd->fd = rc;
LOGP(DRTP, LOGL_DEBUG, "created socket + bound UDP port (%s:%i).\n", source_addr, port);
return 0;
}

View File

@ -45,6 +45,7 @@
#include <osmocom/core/stats.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/socket.h>
#include <osmocom/vty/telnet_interface.h>
#include <osmocom/vty/logging.h>
@ -250,8 +251,8 @@ const struct log_info log_info = {
int main(int argc, char **argv)
{
struct sockaddr_in addr;
int on = 1, rc;
unsigned int flags;
int rc;
tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent");
msgb_talloc_ctx_init(tall_bsc_ctx, 0);
@ -289,53 +290,27 @@ int main(int argc, char **argv)
cfg->reset_cb = mgcp_rsip_cb;
/* we need to bind a socket */
if (rc == 0) {
cfg->gw_fd.bfd.when = BSC_FD_READ;
cfg->gw_fd.bfd.cb = read_call_agent;
cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
if (cfg->gw_fd.bfd.fd < 0) {
perror("Gateway failed to listen");
return -1;
}
flags = OSMO_SOCK_F_BIND;
if (cfg->call_agent_addr)
flags |= OSMO_SOCK_F_CONNECT;
setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(cfg->source_port);
inet_aton(cfg->source_addr, &addr.sin_addr);
if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("Gateway failed to bind");
return -1;
}
cfg->gw_fd.bfd.data = msgb_alloc(4096, "mgcp-msg");
if (!cfg->gw_fd.bfd.data) {
fprintf(stderr, "Gateway memory error.\n");
return -1;
}
if (cfg->call_agent_addr) {
addr.sin_port = htons(2727);
inet_aton(cfg->call_agent_addr, &addr.sin_addr);
if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
LOGP(DLMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
cfg->call_agent_addr, errno);
close(cfg->gw_fd.bfd.fd);
cfg->gw_fd.bfd.fd = -1;
return -1;
}
}
if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) {
LOGP(DLMGCP, LOGL_FATAL, "Failed to register the fd\n");
return -1;
}
LOGP(DLMGCP, LOGL_NOTICE, "Configured for MGCP.\n");
rc = osmo_sock_init2_ofd(&cfg->gw_fd.bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
cfg->source_addr, cfg->source_port,
cfg->call_agent_addr, cfg->call_agent_addr ? 2727 : 0, flags);
if (rc < 0) {
perror("Gateway failed to bind");
return -1;
}
cfg->gw_fd.bfd.cb = read_call_agent;
cfg->gw_fd.bfd.data = msgb_alloc(4096, "mgcp-msg");
if (!cfg->gw_fd.bfd.data) {
fprintf(stderr, "Gateway memory error.\n");
return -1;
}
LOGP(DLMGCP, LOGL_NOTICE, "Configured for MGCP.\n");
/* initialisation */
srand(time(NULL));