mgcp-client: Introduce API osmo_mgcpc_ep_local_name()

Change-Id: I18d7bdf650c0ec87ae16ed4944aed9f495400137
diff --git a/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h b/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h
index f21f40e..3daa09d 100644
--- a/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h
+++ b/include/osmocom/mgcp_client/mgcp_client_endpoint_fsm.h
@@ -45,6 +45,7 @@
 void osmo_mgcpc_ep_clear(struct osmo_mgcpc_ep *ep);
 
 const char *osmo_mgcpc_ep_name(const struct osmo_mgcpc_ep *ep);
+const char *osmo_mgcpc_ep_local_name(const struct osmo_mgcpc_ep *ep);
 const char *osmo_mgcpc_ep_ci_name(const struct osmo_mgcpc_ep_ci *ci);
 const char *osmo_mgcpc_ep_ci_id(const struct osmo_mgcpc_ep_ci *ci);
 struct mgcp_client *osmo_mgcpc_ep_client(const struct osmo_mgcpc_ep *ep);
diff --git a/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c b/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c
index a01bbdc..105e54b 100644
--- a/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c
+++ b/src/libosmo-mgcp-client/mgcp_client_endpoint_fsm.c
@@ -173,6 +173,30 @@
 	return osmo_fsm_inst_name(ep->fi);
 }
 
+/*! Get "local endpoint name" part of the endpoint name: (local-endpoint-name@domain-name)
+ *
+ * \param ep  The MGCP Endpoint
+ * \returns the local endpoint name if found, NULL on error.
+ */
+const char *osmo_mgcpc_ep_local_name(const struct osmo_mgcpc_ep *ep)
+{
+	static char buf[1024];
+	const char *sep;
+
+	OSMO_ASSERT(ep);
+	sep = strchr(ep->endpoint, '@');
+	if (!sep) {
+		OSMO_STRLCPY_ARRAY(buf, ep->endpoint);
+		return buf;
+	}
+	if (sep - ep->endpoint >= sizeof(buf))
+		return NULL;
+
+	memcpy(buf, ep->endpoint, sep - ep->endpoint);
+	buf[sep - ep->endpoint] = '\0';
+	return buf;
+}
+
 const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info)
 {
 	/* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if