pespin has uploaded this change for review.

View Change

mgcp-cli: Fix filling in wrong local IP address of SDP Origin o=

If user (VTY) configured the local IP address to use for MGCP, and that
IP address was not the one selected by kernel routing table lookup, the
IP address filled in the MGCP message would be wrong, not matching the
one sending the MGCP message.

Change-Id: I35624db853dc1f0fee85503105960613f70473c6
---
M src/libosmo-mgcp-client/mgcp_client.c
1 file changed, 34 insertions(+), 7 deletions(-)

git pull ssh://gerrit.osmocom.org:29418/osmo-mgw refs/changes/80/39180/1
diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c
index 1c89e69..8a7c1f6 100644
--- a/src/libosmo-mgcp-client/mgcp_client.c
+++ b/src/libosmo-mgcp-client/mgcp_client.c
@@ -1320,6 +1320,36 @@
#undef MSGB_PRINTF_OR_RET
}

+/* Helper function to obtain local IP address used in MGCP towards MGW,
+ * in string format, to fill the SDP "Origin" ("o=") field.
+ * return 0 on success, negative on error.
+ */
+static int get_mgcp_local_addr(const struct mgcp_client *mgcp, char *local_ip, size_t local_ip_len)
+{
+ int fd;
+
+ /* Try to get the socket local IP address if available: */
+ if (mgcp->iofd && ((fd = osmo_iofd_get_fd(mgcp->iofd)) >= 0)) {
+ if (osmo_sock_get_local_ip(fd, local_ip, local_ip_len) == 0)
+ return 0;
+ /* else: continue below */
+ }
+
+ /* If MGCP local address was explicitly specified in config, use it: */
+ if (mgcp->actual.local_addr) {
+ osmo_strlcpy(local_ip, mgcp->actual.local_addr, local_ip_len);
+ return 0;
+ }
+
+ /* Guess our local address based on system routing towards MGW: */
+ OSMO_ASSERT(local_ip_len >= INET6_ADDRSTRLEN);
+ if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) == 0)
+ return 0;
+
+ LOGPMGW(mgcp, LOGL_ERROR, "Could not determine local IP-Address!\n");
+ return -EINVAL;
+}
+
/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
{
@@ -1329,6 +1359,7 @@
const char *codec;
unsigned int pt;
uint16_t audio_port;
+ int rc;

#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
@@ -1344,13 +1375,9 @@
MSGB_PRINTF_OR_RET("v=0\r\n");

/* Determine local IP-Address */
- if (mgcp->actual.local_addr) {
- OSMO_STRLCPY_ARRAY(local_ip, mgcp->actual.local_addr);
- } else if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
- LOGPMGW(mgcp, LOGL_ERROR,
- "Could not determine local IP-Address!\n");
- return -EINVAL;
- }
+ rc = get_mgcp_local_addr(mgcp, local_ip, sizeof(local_ip));
+ if (rc < 0)
+ return rc;
local_ip_family = osmo_ip_str_type(local_ip);
if (local_ip_family == AF_UNSPEC)
return -EINVAL;

To view, visit change 39180. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: osmo-mgw
Gerrit-Branch: master
Gerrit-Change-Id: I35624db853dc1f0fee85503105960613f70473c6
Gerrit-Change-Number: 39180
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin@sysmocom.de>