endp: move endpoint name generation into mgcp_endp.c

When the trunk allocates its endpoints by using mgcp_endp_alloc()
ist passes the name for each endpoint as a parameter. In order to
generate the name endpoint specific knowlege is required.

This process can be simplified, since all what
mgcp_trunk_alloc_endpts() does is calling mgcp_endp_alloc() in a loop in
order to generate a consecuitve series of endpoints. The endpoint names
are generated from the index of the for loop.

When we just pass the index instead of the endpoint name to
mgcp_endp_alloc(), then we can greatly simplify the code since all the
knowledge about the name generation can go into mgcp_endp.c. The
endpoint will name itsself by the trunk properties and the index number
we pass with the allocator function.

Change-Id: I8dee07f1c63037d1f73113f69c612d1f2703cee5
Related: OS#2659
diff --git a/src/libosmo-mgcp/mgcp_endp.c b/src/libosmo-mgcp/mgcp_endp.c
index 6c78de2..3f1213b 100644
--- a/src/libosmo-mgcp/mgcp_endp.c
+++ b/src/libosmo-mgcp/mgcp_endp.c
@@ -33,11 +33,20 @@
 	.rtp.cleanup_cb = mgcp_cleanup_rtp_bridge_cb
 };
 
+/* Generate virtual endpoint name from given parameters */
+static char *gen_virtual_epname(void *ctx, const char *domain,
+			       unsigned int index)
+{
+	return talloc_asprintf(ctx, "%s%x@%s",
+		 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, index, domain);
+}
+
 /*! allocate an endpoint and set default values.
  *  \param[in] trunk configuration.
- *  \param[in] name endpoint name.
+ *  \param[in] name endpoint index.
  *  \returns endpoint on success, NULL on failure. */
-struct mgcp_endpoint *mgcp_endp_alloc(struct mgcp_trunk *trunk, char *name)
+struct mgcp_endpoint *mgcp_endp_alloc(struct mgcp_trunk *trunk,
+				      unsigned int index)
 {
 	struct mgcp_endpoint *endp;
 
@@ -48,15 +57,18 @@
 	INIT_LLIST_HEAD(&endp->conns);
 	endp->cfg = trunk->cfg;
 	endp->trunk = trunk;
-	endp->name = talloc_strdup(endp, name);
 
 	switch (trunk->trunk_type) {
 	case MGCP_TRUNK_VIRTUAL:
 		endp->type = &ep_typeset.rtp;
+		endp->name = gen_virtual_epname(endp, trunk->cfg->domain, index);
 		break;
 	case MGCP_TRUNK_E1:
-		/* FIXME: Implement E1 allocation */
+		/* FIXME: E1 trunk implementation is work in progress, this endpoint
+		 * name is incomplete (subslots) */
+		endp->name = talloc_asprintf(endp, "%s-1/%x", MGCP_ENDPOINT_PREFIX_E1_TRUNK, index);
 		LOGP(DLMGCP, LOGL_FATAL, "E1 trunks not implemented!\n");
+		endp->type = &ep_typeset.rtp;
 		break;
 	default:
 		osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",