gbproxy: Add config option to name an SGSN

This is useful for logging and configuration to identify an SGSN by name

Change-Id: I2a3410dd9bebb242957e13a63ed70e447204203c
Related: SYS#5115, OS#4472
diff --git a/src/gbproxy/gb_proxy_peer.c b/src/gbproxy/gb_proxy_peer.c
index a966c6c..88b71be 100644
--- a/src/gbproxy/gb_proxy_peer.c
+++ b/src/gbproxy/gb_proxy_peer.c
@@ -350,9 +350,10 @@
 /*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well
  *  \param[in] cfg The gbproxy configuration
  *  \param[in] nsei The nsei where the SGSN can be reached
+ *  \param[in] name A name to give the SGSN
  *  \return The SGSN, NULL if it couldn't be allocated
  */
-struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei)
+struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name)
 {
 	struct gbproxy_sgsn *sgsn;
 	OSMO_ASSERT(cfg);
@@ -364,16 +365,26 @@
 	sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true);
 	if (!sgsn->nse) {
 		LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "Could not allocate NSE(%05u) for SGSN\n", nsei);
-		talloc_free(sgsn);
-		return NULL;
+		goto free_sgsn;
 	}
 
+	if (name)
+		sgsn->name = talloc_strdup(sgsn, name);
+	else
+		sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei);
+	if (!sgsn->name)
+		goto free_sgsn;
+
 	sgsn->pool.allow_attach = true;
 	sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn);
 
 	llist_add_tail(&sgsn->list, &cfg->sgsns);
 	LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n");
 	return sgsn;
+
+free_sgsn:
+	talloc_free(sgsn);
+	return NULL;
 }
 
 /* Only free gbproxy_sgsn, sgsn can't be NULL */
@@ -386,6 +397,7 @@
 
 	LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n");
 	llist_del(&sgsn->list);
+	// talloc will free ->name and ->pool.nri_ranges
 	talloc_free(sgsn);
 }
 
@@ -408,6 +420,24 @@
  *  \param[in] nsei The nsei where the SGSN can be reached
  *  \return Returns the matching SGSN or NULL if it couldn't be found
  */
+struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name)
+{
+	struct gbproxy_sgsn *sgsn;
+	OSMO_ASSERT(cfg);
+
+	llist_for_each_entry(sgsn, &cfg->sgsns, list) {
+		if (!strcmp(sgsn->name, name))
+			return sgsn;
+	}
+
+	return NULL;
+}
+
+/*! Return the SGSN for a given NSEI
+ *  \param[in] cfg The gbproxy configuration
+ *  \param[in] nsei The nsei where the SGSN can be reached
+ *  \return Returns the matching SGSN or NULL if it couldn't be found
+ */
 struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)
 {
 	struct gbproxy_sgsn *sgsn;
@@ -433,7 +463,7 @@
 
 	sgsn = gbproxy_sgsn_by_nsei(cfg, nsei);
 	if (!sgsn)
-		sgsn = gbproxy_sgsn_alloc(cfg, nsei);
+		sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL);
 
 	return sgsn;
 }