From e48231c53972a41918dc50aadced4bcae692574c Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Tue, 21 May 2013 17:37:49 +0200 Subject: [PATCH] dtmf: Check the tones that are put into the DTMF scheduler and reject a 0 Playing tones should be printable ascii characters, begin by checking for NULL (as it happened before) as the first illegal tone. This is an attempt to make the API more robust and detect errors more early. --- src/dtmf_scheduler.c | 3 +++ src/mgcp_ss7.c | 16 +++++++++++++++- tests/dtmf/dtmf_test.c | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/dtmf_scheduler.c b/src/dtmf_scheduler.c index 4f64eb0..5a99a5b 100644 --- a/src/dtmf_scheduler.c +++ b/src/dtmf_scheduler.c @@ -32,6 +32,9 @@ int dtmf_state_add(struct dtmf_state *state, char tone) /* we would override the head */ if (state->size == sizeof(state->tones)) return -1; + /* avoid someone adding a NULL byte */ + if (tone == 0) + return -2; state->tones[state->size++] = tone; return 0; diff --git a/src/mgcp_ss7.c b/src/mgcp_ss7.c index 3490a9a..017bb8e 100644 --- a/src/mgcp_ss7.c +++ b/src/mgcp_ss7.c @@ -97,7 +97,7 @@ static void send_dtmf(struct mgcp_endpoint *mgw_endp, int ascii_tone) { int rc; rc = dtmf_state_add(&mgw_endp->dtmf_state, ascii_tone); - if (rc != 0) { + if (rc == -1) { fprintf(stderr, "DTMF queue too long on 0x%x with %u tones\n", ENDPOINT_NUMBER(mgw_endp), dtmf_tones_queued(&mgw_endp->dtmf_state)); @@ -106,6 +106,20 @@ static void send_dtmf(struct mgcp_endpoint *mgw_endp, int ascii_tone) dtmf_tones_queued(&mgw_endp->dtmf_state)); return; } + if (rc == -2) { + fprintf(stderr, "DTMF illegal tone %d on 0x%x\n", + ascii_tone, ENDPOINT_NUMBER(mgw_endp)); + syslog(LOG_ERR, "DTMF illegal tone %d on 0x%x\n", + ascii_tone, ENDPOINT_NUMBER(mgw_endp)); + return; + } + if (rc < 0) { + fprintf(stderr, "DTMF unknown error %d on 0x%x\n", + rc, ENDPOINT_NUMBER(mgw_endp)); + syslog(LOG_ERR, "DTMF unknown error %d on 0x%x\n", + rc, ENDPOINT_NUMBER(mgw_endp)); + return; + } if (!mgw_endp->dtmf_state.playing) play_pending_tones(mgw_endp); diff --git a/tests/dtmf/dtmf_test.c b/tests/dtmf/dtmf_test.c index 6fab052..97f4f14 100644 --- a/tests/dtmf/dtmf_test.c +++ b/tests/dtmf/dtmf_test.c @@ -99,11 +99,20 @@ static void test_queue_over_flow(void) ASSERT(state.playing, 0); } +static void test_queue_null_byte(void) +{ + struct dtmf_state state; + dtmf_state_init(&state); + + ASSERT(dtmf_state_add(&state, 0), -2); +} + int main(int argc, char **argv) { test_queue_while_play(); test_queue_over_flow(); + test_queue_null_byte(); printf("All tests passed.\n"); return 0; }