diff --git a/src/mod/applications/mod_enum/mod_enum.c b/src/mod/applications/mod_enum/mod_enum.c index 08c0d709f7..e7fbfe236e 100644 --- a/src/mod/applications/mod_enum/mod_enum.c +++ b/src/mod/applications/mod_enum/mod_enum.c @@ -674,6 +674,57 @@ SWITCH_STANDARD_DIALPLAN(enum_dialplan_hunt) return extension; } +/* Pack the supported routes into buf as a dial string, NUL-terminated. Routes of equal + NAPTR order and preference are joined with ',', the rest with '|'. Every element of + that string is dialed as a peer, so routes go in whole; one that does not fit is + skipped and the others are packed. Returns how many were packed. */ +static switch_size_t pack_routes(char *buf, switch_size_t buflen, enum_record_t *results) +{ + enum_record_t *rp; + char *bp = buf; + switch_size_t bl = buflen; + switch_size_t packed = 0; + int last_order = 0, last_pref = 0; + + if (!buflen) { + return 0; + } + + *buf = '\0'; + + for (rp = results; rp; rp = rp->next) { + const char *sep = ""; + int written; + + /* An empty route would contribute a separator and no peer. */ + if (!rp->supported || zstr(rp->route)) { + continue; + } + + /* Compared with the last route packed rather than the previous list entry, + so one left out for space does not split two alternatives. */ + if (packed) { + sep = (rp->order == last_order && rp->preference == last_pref) ? "," : "|"; + } + + if (strlen(rp->route) + strlen(sep) + 1 > bl) { + continue; + } + + /* switch_snprintf() returns the bytes it wrote, clamped to bl - 1, where + C99 snprintf would return the untruncated length. The check above leaves + room for both parts and the terminator, so nothing is clipped. */ + written = switch_snprintf(bp, bl, "%s%s", sep, rp->route); + bp += written; + bl -= written; + last_order = rp->order; + last_pref = rp->preference; + packed++; + } + + return packed; +} + SWITCH_STANDARD_APP(enum_app_function) { char *argv[4] = { 0 }; @@ -682,12 +733,8 @@ SWITCH_STANDARD_APP(enum_app_function) enum_record_t *results, *rp; char rbuf[1024] = ""; char vbuf[1024] = ""; - char *rbp = rbuf; - switch_size_t l = 0, rbl = sizeof(rbuf); uint32_t cnt = 1; switch_channel_t *channel = switch_core_session_get_channel(session); - int last_order = -1, last_pref = -2; - char *last_delim = "|"; if (!(mydata = switch_core_session_strdup(session, data))) { return; @@ -698,6 +745,7 @@ SWITCH_STANDARD_APP(enum_app_function) root = argv[1]; if (enum_lookup(root, dest, &results, channel, session) == SWITCH_STATUS_SUCCESS) { switch_event_t *vars; + switch_size_t packed; if (switch_channel_get_variables(channel, &vars) == SWITCH_STATUS_SUCCESS) { switch_event_header_t *hi; @@ -710,28 +758,32 @@ SWITCH_STANDARD_APP(enum_app_function) switch_event_destroy(&vars); } + /* Same test as pack_routes() applies, so enum_route_count and the packed + count describe the same set of routes. An empty value would delete the + variable rather than set it, leaving a gap in the numbering. */ for (rp = results; rp; rp = rp->next) { - if (!rp->supported) { + if (!rp->supported || zstr(rp->route)) { continue; } - switch_snprintf(vbuf, sizeof(vbuf), "enum_route_%d", cnt++); + switch_snprintf(vbuf, sizeof(vbuf), "enum_route_%u", cnt++); switch_channel_set_variable_var_check(channel, vbuf, rp->route, SWITCH_FALSE); - if (rp->preference == last_pref && rp->order == last_order) { - *last_delim = ','; - } - switch_snprintf(rbp, rbl, "%s|", rp->route); - last_delim = end_of_p(rbp); - last_order = rp->order; - last_pref = rp->preference; - l = strlen(rp->route) + 1; - rbp += l; - rbl -= l; } - switch_snprintf(vbuf, sizeof(vbuf), "%d", cnt - 1); + packed = pack_routes(rbuf, sizeof(rbuf), results); + + switch_snprintf(vbuf, sizeof(vbuf), "%u", cnt - 1); switch_channel_set_variable_var_check(channel, "enum_route_count", vbuf, SWITCH_FALSE); - *(rbuf + strlen(rbuf) - 1) = '\0'; switch_channel_set_variable_var_check(channel, "enum_auto_route", rbuf, SWITCH_FALSE); + + if (!packed && cnt > 1) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, + "No ENUM route fits the dial string, so enum_auto_route is unset; all %u are in enum_route_N\n", + cnt - 1); + } else if (packed < cnt - 1) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, + "ENUM dial string holds %u of %u routes; the rest are in enum_route_N\n", + (unsigned) packed, cnt - 1); + } free_results(&results); } } @@ -742,12 +794,8 @@ SWITCH_STANDARD_API(enum_api) char *argv[4] = { 0 }; char *mydata = NULL; char *dest = NULL, *root = NULL; - enum_record_t *results, *rp; + enum_record_t *results; char rbuf[1024] = ""; - char *rbp = rbuf; - switch_size_t l = 0, rbl = sizeof(rbuf); - int last_order = -1, last_pref = -2; - char *last_delim = "|"; int ok = 0; if (zstr(cmd)) { @@ -764,23 +812,7 @@ SWITCH_STANDARD_API(enum_api) root = argv[1]; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Looking up %s@%s\n", dest, root); if (enum_lookup(root, dest, &results, NULL, session) == SWITCH_STATUS_SUCCESS) { - for (rp = results; rp; rp = rp->next) { - if (!rp->supported) { - continue; - } - if (rp->preference == last_pref && rp->order == last_order) { - *last_delim = ','; - } - switch_snprintf(rbp, rbl, "%s|", rp->route); - last_delim = end_of_p(rbp); - last_order = rp->order; - last_pref = rp->preference; - l = strlen(rp->route) + 1; - rbp += l; - rbl -= l; - - } - *(rbuf + strlen(rbuf) - 1) = '\0'; + pack_routes(rbuf, sizeof(rbuf), results); stream->write_function(stream, "%s", rbuf); free_results(&results); ok++;