nat: Remove the broken empty line check, follow \n vs \r\n of input

Instead of checking the token for NULL we need to check if running
was set to null. Look at the data of the token and check if the line
was ending with a \r\n or \n and then when rewriting a line use that
line ending as well. Add a new test for that.
diff --git a/openbsc/src/nat/bsc_mgcp_utils.c b/openbsc/src/nat/bsc_mgcp_utils.c
index 164b6f6..2b28305 100644
--- a/openbsc/src/nat/bsc_mgcp_utils.c
+++ b/openbsc/src/nat/bsc_mgcp_utils.c
@@ -277,21 +277,24 @@
 
 	running = input;
 	output->l2h = output->data;
-	for (token = strsep(&running, "\n"); token; token = strsep(&running, "\n")) {
+	for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
 		int len = strlen(token);
-
-		/* ignore completely empty lines for now */
-		if (len == 0)
-			continue;
+		int cr = len > 0 && token[len - 1] == '\r';
 
 		if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
 			output->l3h = msgb_put(output, strlen(ip_str));
 			memcpy(output->l3h, ip_str, strlen(ip_str));
 			output->l3h = msgb_put(output, strlen(ip));
 			memcpy(output->l3h, ip, strlen(ip));
-			output->l3h = msgb_put(output, 2);
-			output->l3h[0] = '\r';
-			output->l3h[1] = '\n';
+
+			if (cr) {
+				output->l3h = msgb_put(output, 2);
+				output->l3h[0] = '\r';
+				output->l3h[1] = '\n';
+			} else {
+				output->l3h = msgb_put(output, 1);
+				output->l3h[0] = '\n';
+			}
 		} else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
 			int payload;
 			if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
@@ -300,7 +303,8 @@
 				return NULL;
 			}
 
-			snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d\r\n", port, payload);
+			snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
+				 port, payload, cr ? "\r\n" : "\n");
 			buf[sizeof(buf)-1] = '\0';
 
 			output->l3h = msgb_put(output, strlen(buf));
diff --git a/openbsc/tests/bsc-nat/bsc_data.c b/openbsc/tests/bsc-nat/bsc_data.c
index 28d5064..34242db 100644
--- a/openbsc/tests/bsc-nat/bsc_data.c
+++ b/openbsc/tests/bsc-nat/bsc_data.c
@@ -109,6 +109,10 @@
 static const char mdcx_resp[] = "200 23330829\r\n\r\nv=0\r\nc=IN IP4 172.16.18.2\r\nm=audio 4002 RTP/AVP 98\r\na=rtpmap:98 AMR/8000\r\n";
 static const char mdcx_resp_patched[] = "200 23330829\r\n\r\nv=0\r\nc=IN IP4 10.0.0.23\r\nm=audio 5555 RTP/AVP 98\r\na=rtpmap:98 AMR/8000\r\n";
 
+/* different line ending */
+static const char mdcx_resp2[] = "200 33330829\n\nv=0\nc=IN IP4 172.16.18.2\nm=audio 4002 RTP/AVP 98\na=rtpmap:98 AMR/8000\n";
+static const char mdcx_resp_patched2[] = "200 33330829\n\nv=0\nc=IN IP4 10.0.0.23\nm=audio 5555 RTP/AVP 98\na=rtpmap:98 AMR/8000\n";
+
 struct mgcp_patch_test {
 	const char *orig;
 	const char *patch;
@@ -141,4 +145,10 @@
 		.ip = "10.0.0.23",
 		.port = 5555,
 	},
+	{
+		.orig = mdcx_resp2,
+		.patch = mdcx_resp_patched2,
+		.ip = "10.0.0.23",
+		.port = 5555,
+	},
 };