diff options
Diffstat (limited to 'openbsc/src/bsc_msc_ip.c')
-rw-r--r-- | openbsc/src/bsc_msc_ip.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/openbsc/src/bsc_msc_ip.c b/openbsc/src/bsc_msc_ip.c index 535445a4a..0bdb8ba54 100644 --- a/openbsc/src/bsc_msc_ip.c +++ b/openbsc/src/bsc_msc_ip.c @@ -642,6 +642,7 @@ static void mgcp_forward(struct msgb *msg) static int mgcp_create_port(void) { int on; + int port; struct sockaddr_in addr; mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); @@ -653,10 +654,26 @@ static int mgcp_create_port(void) on = 1; setsockopt(mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - /* connect to the remote by using autobind */ + /* try to bind the socket */ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + for (port = 2727; port < 3000; ++port) { + addr.sin_port = htons(port); + if (bind(mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) == 0) + break; + perror("foo"); + } + + if (port >= 3000) { + LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n"); + close(mgcp_agent.bfd.fd); + mgcp_agent.bfd.fd = -1; + return -1; + } + + /* connect to the remote */ addr.sin_port = htons(2427); if (connect(mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) { LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %d\n", errno); |