sgsn: Allow to resolve the IPv4 address of a GGSN through DNS

For real networks we need to check if the requested APN string
is allowed and then resolve the GGSN address through DNS. There
are countries with two or three digit MNCs and one could either
try to keep a list of countries that have two/three digits or
just try both of them. I have opted for the later for the ease
of the implementation.

C-Ares doesn't allow to cancel a request so we will need to
have the MMCTX and the Lookup have different lifetimes. We simply
set ->mmctx to NULL in case the MMCTX dies more early.

The selected and verified apn_str will be copied into the out
parameter. In case no static APN/GGSN config is present and the
dynamic mode is enabled a request will be made.
diff --git a/openbsc/include/openbsc/gprs_sgsn.h b/openbsc/include/openbsc/gprs_sgsn.h
index 8abe97c..61120b1 100644
--- a/openbsc/include/openbsc/gprs_sgsn.h
+++ b/openbsc/include/openbsc/gprs_sgsn.h
@@ -70,6 +70,29 @@
 
 #define MS_RADIO_ACCESS_CAPA
 
+enum sgsn_ggsn_lookup_state {
+	SGSN_GGSN_2DIGIT,
+	SGSN_GGSN_3DIGIT,
+};
+
+struct sgsn_ggsn_lookup {
+	int state;
+
+	struct sgsn_mm_ctx *mmctx;
+
+	/* APN string */
+	char apn_str[GSM_APN_LENGTH];
+
+	/* the original data */
+	struct msgb *orig_msg;
+	struct tlv_parsed tp;
+
+	/* for dealing with re-transmissions */
+	uint8_t nsapi;
+	uint8_t sapi;
+	uint8_t ti;
+};
+
 /* According to TS 03.60, Table 5: SGSN MM and PDP Contexts */
 /* Extended by 3GPP TS 23.060, Table 6: SGSN MM and PDP Contexts */
 struct sgsn_mm_ctx {
@@ -139,6 +162,9 @@
 	/* the string representation of the current hlr */
 	char 			hlr[GSM_EXTENSION_LENGTH];
 
+	/* the current GGSN look-up operation */
+	struct sgsn_ggsn_lookup *ggsn_lookup;
+
 	struct gsm_subscriber   *subscr;
 };
 
@@ -159,7 +185,8 @@
 
 struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
 						struct tlv_parsed *tp,
-						enum gsm48_gsm_cause *gsm_cause);
+						enum gsm48_gsm_cause *gsm_cause,
+						char *apn_str);
 
 enum pdp_ctx_state {
 	PDP_STATE_NONE,
@@ -182,6 +209,7 @@
 	struct llist_head	list;	/* list_head for mmctx->pdp_list */
 	struct llist_head	g_list;	/* list_head for global list */
 	struct sgsn_mm_ctx	*mm;	/* back pointer to MM CTX */
+	int			destroy_ggsn; /* destroy it on destruction */
 	struct sgsn_ggsn_ctx	*ggsn;	/* which GGSN serves this PDP */
 	struct rate_ctr_group	*ctrg;
 
diff --git a/openbsc/include/openbsc/sgsn.h b/openbsc/include/openbsc/sgsn.h
index 8e4d532..a92bfc6 100644
--- a/openbsc/include/openbsc/sgsn.h
+++ b/openbsc/include/openbsc/sgsn.h
@@ -10,6 +10,7 @@
 #include <ares.h>
 
 struct gprs_gsup_client;
+struct hostent;
 
 enum sgsn_auth_policy {
 	SGSN_AUTH_POLICY_OPEN,
@@ -43,6 +44,8 @@
 
 	/* CDR configuration */
 	struct sgsn_cdr cdr;
+
+	int dynamic_lookup;
 };
 
 struct sgsn_instance {