From 06da85ed3abb20e28d72db6202a3396fb739ea11 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Thu, 5 Oct 2017 18:49:24 +0200 Subject: [PATCH] client: add ip address parsing to the client Some MGCP messages (CRCX, MDCX) return IP-Addresses. Make use of this information. Change-Id: I44b338b09de45e1675cedf9737fa72dde72e979a --- include/osmocom/mgcp_client/mgcp_client.h | 1 + src/libosmo-mgcp-client/mgcp_client.c | 39 +++++++++++++++++++++-- tests/mgcp_client/mgcp_client_test.c | 6 ++-- tests/mgcp_client/mgcp_client_test.ok | 1 + 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/include/osmocom/mgcp_client/mgcp_client.h b/include/osmocom/mgcp_client/mgcp_client.h index 21717e3b3..e91b1906b 100644 --- a/include/osmocom/mgcp_client/mgcp_client.h +++ b/include/osmocom/mgcp_client/mgcp_client.h @@ -36,6 +36,7 @@ struct mgcp_response { char *body; struct mgcp_response_head head; uint16_t audio_port; + char audio_ip[INET_ADDRSTRLEN]; }; enum mgcp_verb { diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c index c7cc98934..f8c55ac58 100644 --- a/src/libosmo-mgcp-client/mgcp_client.c +++ b/src/libosmo-mgcp-client/mgcp_client.c @@ -174,7 +174,7 @@ static bool mgcp_line_is_valid(const char *line) } /* Parse a line like "m=audio 16002 RTP/AVP 98" */ -static int mgcp_parse_audio(struct mgcp_response *r, const char *line) +static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line) { if (sscanf(line, "m=audio %hu", &r->audio_port) != 1) @@ -184,7 +184,35 @@ static int mgcp_parse_audio(struct mgcp_response *r, const char *line) response_parse_failure: LOGP(DLMGCP, LOGL_ERROR, - "Failed to parse MGCP response header\n"); + "Failed to parse MGCP response header (audio port)\n"); + return -EINVAL; +} + +/* Parse a line like "c=IN IP4 10.11.12.13" */ +static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line) +{ + struct in_addr ip_test; + + if (strlen(line) < 16) + goto response_parse_failure; + + /* The current implementation strictly supports IPV4 only ! */ + if (memcmp("c=IN IP4 ", line, 9) != 0) + goto response_parse_failure; + + /* Extract IP-Address */ + strncpy(r->audio_ip, line + 9, sizeof(r->audio_ip)); + r->audio_ip[sizeof(r->audio_ip) - 1] = '\0'; + + /* Check IP-Address */ + if (inet_aton(r->audio_ip, &ip_test) == 0) + goto response_parse_failure; + + return 0; + +response_parse_failure: + LOGP(DLMGCP, LOGL_ERROR, + "Failed to parse MGCP response header (audio ip)\n"); return -EINVAL; } @@ -213,7 +241,12 @@ int mgcp_response_parse_params(struct mgcp_response *r) switch (line[0]) { case 'm': - rc = mgcp_parse_audio(r, line); + rc = mgcp_parse_audio_port(r, line); + if (rc) + return rc; + break; + case 'c': + rc = mgcp_parse_audio_ip(r, line); if (rc) return rc; break; diff --git a/tests/mgcp_client/mgcp_client_test.c b/tests/mgcp_client/mgcp_client_test.c index 7977a6abc..513f3454c 100644 --- a/tests/mgcp_client/mgcp_client_test.c +++ b/tests/mgcp_client/mgcp_client_test.c @@ -99,11 +99,13 @@ void test_response_cb(struct mgcp_response *response, void *priv) " head.response_code = %d\n" " head.trans_id = %u\n" " head.comment = %s\n" - " audio_port = %u\n", + " audio_port = %u\n" + " audio_ip = %s\n", response->head.response_code, response->head.trans_id, response->head.comment, - response->audio_port + response->audio_port, + response->audio_ip ); } diff --git a/tests/mgcp_client/mgcp_client_test.ok b/tests/mgcp_client/mgcp_client_test.ok index e3b611314..d4efee4cc 100644 --- a/tests/mgcp_client/mgcp_client_test.ok +++ b/tests/mgcp_client/mgcp_client_test.ok @@ -28,6 +28,7 @@ response cb received: head.trans_id = 1 head.comment = OK audio_port = 16002 + audio_ip = 10.9.1.120 Generated CRCX message: CRCX 1 23@mgw MGCP 1.0