mirror of
https://gitea.osmocom.org/cellular-infrastructure/osmo-mgw.git
synced 2025-11-02 13:03:33 +00:00
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.
This commit is contained in:
@@ -277,21 +277,24 @@ struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
|
||||
|
||||
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 @@ struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
|
||||
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));
|
||||
|
||||
@@ -109,6 +109,10 @@ static const char mdcx_patched[] = " MDCX 23330829 8@mgw MGCP 1.0\r\nC: 394b0439
|
||||
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 @@ static const struct mgcp_patch_test mgcp_messages[] = {
|
||||
.ip = "10.0.0.23",
|
||||
.port = 5555,
|
||||
},
|
||||
{
|
||||
.orig = mdcx_resp2,
|
||||
.patch = mdcx_resp_patched2,
|
||||
.ip = "10.0.0.23",
|
||||
.port = 5555,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user