mgcp_network: refactor MGCP_DUMMY_LOAD
The constant MGCP_DUMMY_LOAD is used ambigously. Sometimes it is used as
initalizer for an array, sometimes it is used as a single byte. Also the
name is not very expressive. Lets refactor this.
Change-Id: I21d96cefeeb647958bfa1e22a0ea030884746fad
Related: OS#4005
diff --git a/include/osmocom/mgcp/mgcp_network.h b/include/osmocom/mgcp/mgcp_network.h
index 6e1e52c..1766f4a 100644
--- a/include/osmocom/mgcp/mgcp_network.h
+++ b/include/osmocom/mgcp/mgcp_network.h
@@ -7,7 +7,16 @@
#include <osmocom/mgcp/mgcp.h>
-#define MGCP_DUMMY_LOAD 0x23
+/* The following constant defines an RTP dummy payload that is used for
+ * "UDP Hole Punching" (NAT) */
+static const char rtp_dummy_payload[] = { 0x23 };
+
+/* Check if the data in a given message buffer matches the rtp dummy payload
+ * defined above */
+#define mgcp_is_rtp_dummy_payload(msg) \
+ (msgb_length(msg) == sizeof(rtp_dummy_payload) && \
+ memcmp(msgb_data(msg), rtp_dummy_payload, sizeof(rtp_dummy_payload)) == 0)
+
#define RTP_BUF_SIZE 4096
struct mgcp_rtp_stream_state {
diff --git a/src/libosmo-mgcp/mgcp_network.c b/src/libosmo-mgcp/mgcp_network.c
index 79ddf8a..1b7c3bd 100644
--- a/src/libosmo-mgcp/mgcp_network.c
+++ b/src/libosmo-mgcp/mgcp_network.c
@@ -205,7 +205,6 @@
* \returns bytes sent, -1 on error. */
int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
{
- static char buf[] = { MGCP_DUMMY_LOAD };
int rc;
int was_rtcp = 0;
@@ -216,7 +215,7 @@
mgcp_conn_dump(conn->conn));
rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
- conn->end.rtp_port, buf, 1);
+ conn->end.rtp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
if (rc == -1)
goto failed;
@@ -226,7 +225,7 @@
was_rtcp = 1;
rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
- conn->end.rtcp_port, buf, 1);
+ conn->end.rtcp_port, rtp_dummy_payload, sizeof(rtp_dummy_payload));
if (rc >= 0)
return rc;
@@ -1398,11 +1397,6 @@
mgcp_cleanup_rtp_bridge_cb(endp, conn);
}
-static bool is_dummy_msg(enum rtp_proto proto, struct msgb *msg)
-{
- return msgb_length(msg) == 1 && msgb_data(msg)[0] == MGCP_DUMMY_LOAD;
-}
-
/* Handle incoming RTP data from NET */
static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
{
@@ -1453,7 +1447,7 @@
goto out;
}
- if (is_dummy_msg(proto, msg)) {
+ if (mgcp_is_rtp_dummy_payload(msg)) {
LOG_CONN_RTP(conn_src, LOGL_DEBUG, "rx dummy packet (dropped)\n");
rc = 0;
goto out;
diff --git a/src/libosmo-mgcp/mgcp_osmux.c b/src/libosmo-mgcp/mgcp_osmux.c
index bb85735..8f0a906 100644
--- a/src/libosmo-mgcp/mgcp_osmux.c
+++ b/src/libosmo-mgcp/mgcp_osmux.c
@@ -375,7 +375,7 @@
}
/* not any further processing dummy messages */
- if (msg->data[0] == MGCP_DUMMY_LOAD)
+ if (mgcp_is_rtp_dummy_payload(msg))
return osmux_handle_dummy(cfg, &addr, msg);
rem = msg->len;
diff --git a/tests/mgcp/mgcp_test.c b/tests/mgcp/mgcp_test.c
index 74ecfe8..2ca0b4c 100644
--- a/tests/mgcp/mgcp_test.c
+++ b/tests/mgcp/mgcp_test.c
@@ -30,6 +30,7 @@
#include <osmocom/mgcp/mgcp_trunk.h>
#include <osmocom/mgcp/mgcp_sdp.h>
#include <osmocom/mgcp/mgcp_codec.h>
+#include <osmocom/mgcp/mgcp_network.h>
#include <osmocom/core/application.h>
#include <osmocom/core/talloc.h>
@@ -616,7 +617,6 @@
return MGCP_POLICY_CONT;
}
-#define MGCP_DUMMY_LOAD 0x23
static int dummy_packets = 0;
/* override and forward */
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
@@ -626,7 +626,8 @@
htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
- if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
+ if (len == sizeof(rtp_dummy_payload)
+ && memcmp(buf, rtp_dummy_payload, sizeof(rtp_dummy_payload)) == 0) {
fprintf(stderr,
"Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
dest_host, dest_port, len, osmo_hexdump(buf, len));
@@ -2161,6 +2162,47 @@
return;
}
+void test_mgcp_is_rtp_dummy_payload()
+{
+ /* realistic rtp packet */
+ static const char rtp_payload[] =
+ { 0x80, 0x03, 0xca, 0xd7, 0x62, 0x12, 0x75, 0xc4, 0x43, 0x4b, 0x3e,
+ 0x72, 0xd2, 0x57, 0x7a, 0x1c, 0xda, 0x50, 0x00, 0x49, 0x24, 0x92,
+ 0x49, 0x24, 0x50, 0x00, 0x49, 0x24, 0x92, 0x49, 0x24, 0x50, 0x00,
+ 0x49, 0x24, 0x92, 0x49, 0x24, 0x50, 0x00, 0x49, 0x23, 0x92, 0x49,
+ 0x24 };
+
+ struct msgb *msg_dummy = msgb_alloc(RTP_BUF_SIZE, "RTP-msg_dummy");
+ struct msgb *msg_rtp = msgb_alloc(RTP_BUF_SIZE, "RTP-msg_rtp");
+ struct msgb *msg_dummy_rtp =
+ msgb_alloc(RTP_BUF_SIZE, "RTP-msg_dummy_rtp");
+
+ uint8_t *buf;
+
+ /* Dummy RTP packet */
+ buf = msgb_put(msg_dummy, ARRAY_SIZE(rtp_dummy_payload));
+ memcpy(buf, rtp_dummy_payload, ARRAY_SIZE(rtp_dummy_payload));
+
+ /* Normal RTP packet */
+ buf = msgb_put(msg_rtp, ARRAY_SIZE(rtp_payload));
+ memcpy(buf, rtp_payload, ARRAY_SIZE(rtp_payload));
+
+ /* Dummy RTP packet with normal RTP packet attached, this must not be
+ * recognized as Dummy RTP packet */
+ buf = msgb_put(msg_dummy_rtp, ARRAY_SIZE(rtp_dummy_payload));
+ memcpy(buf, rtp_dummy_payload, ARRAY_SIZE(rtp_dummy_payload));
+ buf = msgb_put(msg_dummy_rtp, ARRAY_SIZE(rtp_payload));
+ memcpy(buf, rtp_payload, ARRAY_SIZE(rtp_payload));
+
+ OSMO_ASSERT(mgcp_is_rtp_dummy_payload(msg_dummy) == true);
+ OSMO_ASSERT(mgcp_is_rtp_dummy_payload(msg_rtp) == false);
+ OSMO_ASSERT(mgcp_is_rtp_dummy_payload(msg_dummy_rtp) == false);
+
+ msgb_free(msg_dummy);
+ msgb_free(msg_rtp);
+ msgb_free(msg_dummy_rtp);
+}
+
int main(int argc, char **argv)
{
void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
@@ -2187,6 +2229,7 @@
test_mgcp_codec_pt_translate();
test_conn_id_matching();
test_e1_trunk_nr_from_epname();
+ test_mgcp_is_rtp_dummy_payload();
OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);