gbproxy: Add gprs_gb_message_name function

This function tries to get an accurate name for the message even if
the parsing has been aborted due to message errors.

The patch also moves the settings of the BSSGP related fields in
parse_ctx from behind to the front of bssgp_tlv_parse, to get more
information in the case of failure. This is now consistent with the
handling of the llc and g48_hdr fields.

Id addition, gprs_gb_log_parse_context now uses the new function to
derive a more accurate message name.

Ticket: OW#1307
Sponsored-by: On-Waves ehf
diff --git a/openbsc/src/gprs/gprs_gb_parse.c b/openbsc/src/gprs/gprs_gb_parse.c
index 1978bd4..17d4b9c 100644
--- a/openbsc/src/gprs/gprs_gb_parse.c
+++ b/openbsc/src/gprs/gprs_gb_parse.c
@@ -564,15 +564,15 @@
 		data_len = bssgp_len - sizeof(*bgph);
 	}
 
-	if (bssgp_tlv_parse(tp, data, data_len) < 0)
-		return 0;
-
 	parse_ctx->pdu_type = pdu_type;
 	parse_ctx->bud_hdr = budh;
 	parse_ctx->bgp_hdr = bgph;
 	parse_ctx->bssgp_data = data;
 	parse_ctx->bssgp_data_len = data_len;
 
+	if (bssgp_tlv_parse(tp, data, data_len) < 0)
+		return 0;
+
 	if (budh)
 		parse_ctx->tlli_enc = (uint8_t *)&budh->tlli;
 
@@ -626,7 +626,7 @@
 void gprs_gb_log_parse_context(struct gprs_gb_parse_context *parse_ctx,
 			       const char *default_msg_name)
 {
-	const char *msg_name = default_msg_name;
+	const char *msg_name;
 	const char *sep = "";
 
 	if (!parse_ctx->tlli_enc &&
@@ -635,6 +635,8 @@
 	    !parse_ctx->imsi)
 		return;
 
+	msg_name = gprs_gb_message_name(parse_ctx, default_msg_name);
+
 	if (parse_ctx->llc_msg_name)
 		msg_name = parse_ctx->llc_msg_name;
 
@@ -713,3 +715,23 @@
 	LOGPC(DGPRS, LOGL_DEBUG, "\n");
 }
 
+const char *gprs_gb_message_name(const struct gprs_gb_parse_context *parse_ctx,
+				 const char *default_msg_name)
+{
+	if (parse_ctx->llc_msg_name)
+		return parse_ctx->llc_msg_name;
+
+	if (parse_ctx->g48_hdr)
+		return "GMM";
+
+	if (parse_ctx->llc)
+		return "LLC";
+
+	if (parse_ctx->bud_hdr)
+		return "BSSGP-UNITDATA";
+
+	if (parse_ctx->bgp_hdr)
+		return "BSSGP";
+
+	return "unknown";
+}