mgcp_conn_get(): match conn Id ('I:') despite leading zeros

The Connection Identifier is defined as a hex string, so clients may send the
ID back with or without leading zeros. Ignore all leading zeros when comparing.

A specific SCCPlite MSC is observed to DLCX with Connection Identifier with
leading zeros removed, which would mismatch pefore this patch.

Extend test_conn_id_matching() in mgcp_test.c to include leading zero tests.

Now, mgcp_conn_get() would match a valid id with *any* amount of leading zeros,
even if that far surpasses the permitted conn id length. Valid lengths of
incoming conn ids should be and is checked elsewhere.

Related: OS#3509
Change-Id: If55a64a2da47b6eff035711c08e4114d70dbec91
diff --git a/src/libosmo-mgcp/mgcp_conn.c b/src/libosmo-mgcp/mgcp_conn.c
index 0918b8b..820c63a 100644
--- a/src/libosmo-mgcp/mgcp_conn.c
+++ b/src/libosmo-mgcp/mgcp_conn.c
@@ -190,17 +190,25 @@
 {
 	struct mgcp_conn *conn;
 	const char *id_upper;
+	const char *conn_id;
 
 	if (!id || !*id)
 		return NULL;
 
+	/* Ignore leading zeros in needle */
+	while (*id == '0')
+		id++;
+
 	/* Use uppercase to compare identifiers, to avoid mismatches: RFC3435 2.1.3.2 "Names of
 	 * Connections" defines the id as a hex string, so clients may return lower case hex even though
 	 * we sent upper case hex in the CRCX response. */
 	id_upper = osmo_str_toupper(id);
 
 	llist_for_each_entry(conn, &endp->conns, entry) {
-		if (strcmp(conn->id, id_upper) == 0)
+		/* Ignore leading zeros in haystack */
+		for (conn_id=conn->id; *conn_id == '0'; conn_id++);
+
+		if (strcmp(conn_id, id_upper) == 0)
 			return conn;
 	}
 
diff --git a/tests/mgcp/mgcp_test.c b/tests/mgcp/mgcp_test.c
index 99ddd71..b9f7253 100644
--- a/tests/mgcp/mgcp_test.c
+++ b/tests/mgcp/mgcp_test.c
@@ -1759,8 +1759,14 @@
 	int i;
 	const char *conn_id_generated = "000023AB";
 	const char *conn_id_request[] = {
+		"23AB",
+		"0023AB",
 		"000023AB",
+		"00000023AB",
+		"23ab",
+		"0023ab",
 		"000023ab",
+		"00000023ab",
 	};
 
 	printf("\nTesting %s\n", __func__);
diff --git a/tests/mgcp/mgcp_test.ok b/tests/mgcp/mgcp_test.ok
index f50f487..28e9aad 100644
--- a/tests/mgcp/mgcp_test.ok
+++ b/tests/mgcp/mgcp_test.ok
Binary files differ