mirror of http://gerrit.asterisk.org/asterisk
New files - channels/sip/sip.h – A new header for shared #define, enum, and struct definitions. - channels/sip/include/sip_utils.h – sip util functions shared among the all the sip APIs - channels/sip/include/config_parser.h – sip config-parser API - channels/sip/config_parser.c – Contains sip.conf parsing helper functions with unit tests. - channels/sip/include/reqresp_parser.h – sip request response parser API - channels/sip/reqresp_parser.c – Contains sip request and response parsing helper functions with unit tests. New Unit Tests - sip_parse_uri_test - sip_parse_host_test - sip_parse_register_line_test Code Refactoring - All reusable #define, enum, and struct definitions were moved out of chan_sip.c into sip.h. During this process formatting changes were made to comments in both sip.h and chan_sip.c in order to better adhere to the coding guidelines. - The beginnings of three new sip APIs, sip-utils.h, config-parser.h, reqresp-parser.h using existing chan_sip.c functions. - parse_uri() and get_calleridname() were moved from chan_sip.c to request-parser.c along with unit tests for both functions. - sip_parse_host() and sip_parse_register_line() were moved from chan_sip.c to config-parser.c along with unit tests for both functions. Changes to parse_uri() -removal of the options parameter. It was never used and did not behave correctly. -additional check for [?header] field. When this field was present, the transport type was not being set correctly. ----- Overview ----- This patch is introduced with the hope that unit tests for all our sip parsing functions will be written soon. chan_sip is a huge file, and with the addition of each unit test chan_sip is going to grow larger and harder to maintain. I'm proposing we begin refactoring chan_sip, starting with the parsing functions. With each parsing function we move into a separate helper file, a unit test should accompany it. I've attempted to lay down the ground work for this change by creating two new parser helper files (config-parser.c and reqresp-parser.c) and moving all shared structs, enums, and defines from chan_sip.c into a shared sip.h file. We can't verify everything in Asterisk using unit tests, but string parsing is one area where unit tests make the most sense. By beginning to restructure the code in this way, chan_sip not only becomes less bloated, but Asterisk as a whole will become more stable. Review: https://reviewboard.asterisk.org/r/477/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@244597 65c4cc65-6c06-0410-ace0-fbb531ad65f3certified/1.8.6
parent
dbc3111652
commit
a9932363a9
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,657 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2010, Digium, Inc.
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief sip config parsing functions and unit tests
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "include/sip.h"
|
||||
#include "include/config_parser.h"
|
||||
#include "include/sip_utils.h"
|
||||
|
||||
/*! \brief Parse register=> line in sip.conf
|
||||
*
|
||||
* \retval 0 on success
|
||||
* \retval -1 on failure
|
||||
*/
|
||||
int sip_parse_register_line(struct sip_registry *reg, const char *value, int lineno)
|
||||
{
|
||||
int portnum = 0;
|
||||
enum sip_transport transport = SIP_TRANSPORT_UDP;
|
||||
char buf[256] = "";
|
||||
char *userpart = NULL, *hostpart = NULL;
|
||||
/* register => [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry] */
|
||||
AST_DECLARE_APP_ARGS(pre1,
|
||||
AST_APP_ARG(peer);
|
||||
AST_APP_ARG(userpart);
|
||||
);
|
||||
AST_DECLARE_APP_ARGS(pre2,
|
||||
AST_APP_ARG(transport);
|
||||
AST_APP_ARG(blank);
|
||||
AST_APP_ARG(userpart);
|
||||
);
|
||||
AST_DECLARE_APP_ARGS(user1,
|
||||
AST_APP_ARG(userpart);
|
||||
AST_APP_ARG(secret);
|
||||
AST_APP_ARG(authuser);
|
||||
);
|
||||
AST_DECLARE_APP_ARGS(host1,
|
||||
AST_APP_ARG(hostpart);
|
||||
AST_APP_ARG(expiry);
|
||||
);
|
||||
AST_DECLARE_APP_ARGS(host2,
|
||||
AST_APP_ARG(hostpart);
|
||||
AST_APP_ARG(extension);
|
||||
);
|
||||
AST_DECLARE_APP_ARGS(host3,
|
||||
AST_APP_ARG(host);
|
||||
AST_APP_ARG(port);
|
||||
);
|
||||
|
||||
if (!value) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!reg) {
|
||||
return -1;
|
||||
}
|
||||
ast_copy_string(buf, value, sizeof(buf));
|
||||
|
||||
/*! register => [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry]
|
||||
* becomes
|
||||
* userpart => [peer?][transport://]user[@domain][:secret[:authuser]]
|
||||
* hostpart => host[:port][/extension][~expiry]
|
||||
*/
|
||||
if ((hostpart = strrchr(buf, '@'))) {
|
||||
*hostpart++ = '\0';
|
||||
userpart = buf;
|
||||
}
|
||||
|
||||
if (ast_strlen_zero(userpart) || ast_strlen_zero(hostpart)) {
|
||||
ast_log(LOG_WARNING, "Format for registration is [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry] at line %d\n", lineno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*!
|
||||
* pre1.peer => peer
|
||||
* pre1.userpart => [transport://]user[@domain][:secret[:authuser]]
|
||||
* hostpart => host[:port][/extension][~expiry]
|
||||
*/
|
||||
AST_NONSTANDARD_RAW_ARGS(pre1, userpart, '?');
|
||||
if (ast_strlen_zero(pre1.userpart)) {
|
||||
pre1.userpart = pre1.peer;
|
||||
pre1.peer = NULL;
|
||||
}
|
||||
|
||||
/*!
|
||||
* pre1.peer => peer
|
||||
* pre2.transport = transport
|
||||
* pre2.userpart => user[@domain][:secret[:authuser]]
|
||||
* hostpart => host[:port][/extension][~expiry]
|
||||
*/
|
||||
AST_NONSTANDARD_RAW_ARGS(pre2, pre1.userpart, '/');
|
||||
if (ast_strlen_zero(pre2.userpart)) {
|
||||
pre2.userpart = pre2.transport;
|
||||
pre2.transport = NULL;
|
||||
} else {
|
||||
pre2.transport[strlen(pre2.transport) - 1] = '\0'; /* Remove trailing : */
|
||||
}
|
||||
|
||||
if (!ast_strlen_zero(pre2.blank)) {
|
||||
ast_log(LOG_WARNING, "Format for registration is [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry] at line %d\n", lineno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*!
|
||||
* pre1.peer => peer
|
||||
* pre2.transport = transport
|
||||
* user1.userpart => user[@domain]
|
||||
* user1.secret => secret
|
||||
* user1.authuser => authuser
|
||||
* hostpart => host[:port][/extension][~expiry]
|
||||
*/
|
||||
AST_NONSTANDARD_RAW_ARGS(user1, pre2.userpart, ':');
|
||||
|
||||
/*!
|
||||
* pre1.peer => peer
|
||||
* pre2.transport = transport
|
||||
* user1.userpart => user[@domain]
|
||||
* user1.secret => secret
|
||||
* user1.authuser => authuser
|
||||
* host1.hostpart => host[:port][/extension]
|
||||
* host1.expiry => [expiry]
|
||||
*/
|
||||
AST_NONSTANDARD_RAW_ARGS(host1, hostpart, '~');
|
||||
|
||||
/*!
|
||||
* pre1.peer => peer
|
||||
* pre2.transport = transport
|
||||
* user1.userpart => user[@domain]
|
||||
* user1.secret => secret
|
||||
* user1.authuser => authuser
|
||||
* host2.hostpart => host[:port]
|
||||
* host2.extension => [extension]
|
||||
* host1.expiry => [expiry]
|
||||
*/
|
||||
AST_NONSTANDARD_RAW_ARGS(host2, host1.hostpart, '/');
|
||||
|
||||
/*!
|
||||
* pre1.peer => peer
|
||||
* pre2.transport = transport
|
||||
* user1.userpart => user[@domain]
|
||||
* user1.secret => secret
|
||||
* user1.authuser => authuser
|
||||
* host3.host => host
|
||||
* host3.port => port
|
||||
* host2.extension => extension
|
||||
* host1.expiry => expiry
|
||||
*/
|
||||
AST_NONSTANDARD_RAW_ARGS(host3, host2.hostpart, ':');
|
||||
|
||||
if (host3.port) {
|
||||
if (!(portnum = port_str2int(host3.port, 0))) {
|
||||
ast_log(LOG_NOTICE, "'%s' is not a valid port number on line %d of sip.conf. using default.\n", host3.port, lineno);
|
||||
}
|
||||
}
|
||||
|
||||
/* set transport type */
|
||||
if (!pre2.transport) {
|
||||
transport = SIP_TRANSPORT_UDP;
|
||||
} else if (!strncasecmp(pre2.transport, "tcp", 3)) {
|
||||
transport = SIP_TRANSPORT_TCP;
|
||||
} else if (!strncasecmp(pre2.transport, "tls", 3)) {
|
||||
transport = SIP_TRANSPORT_TLS;
|
||||
} else if (!strncasecmp(pre2.transport, "udp", 3)) {
|
||||
transport = SIP_TRANSPORT_UDP;
|
||||
} else {
|
||||
transport = SIP_TRANSPORT_UDP;
|
||||
ast_log(LOG_NOTICE, "'%.3s' is not a valid transport type on line %d of sip.conf. defaulting to udp.\n", pre2.transport, lineno);
|
||||
}
|
||||
|
||||
/* if no portnum specified, set default for transport */
|
||||
if (!portnum) {
|
||||
if (transport == SIP_TRANSPORT_TLS) {
|
||||
portnum = STANDARD_TLS_PORT;
|
||||
} else {
|
||||
portnum = STANDARD_SIP_PORT;
|
||||
}
|
||||
}
|
||||
|
||||
/* copy into sip_registry object */
|
||||
ast_string_field_set(reg, callback, ast_strip_quoted(S_OR(host2.extension, "s"), "\"", "\""));
|
||||
ast_string_field_set(reg, username, ast_strip_quoted(S_OR(user1.userpart, ""), "\"", "\""));
|
||||
ast_string_field_set(reg, hostname, ast_strip_quoted(S_OR(host3.host, ""), "\"", "\""));
|
||||
ast_string_field_set(reg, authuser, ast_strip_quoted(S_OR(user1.authuser, ""), "\"", "\""));
|
||||
ast_string_field_set(reg, secret, ast_strip_quoted(S_OR(user1.secret, ""), "\"", "\""));
|
||||
ast_string_field_set(reg, peername, ast_strip_quoted(S_OR(pre1.peer, ""), "\"", "\""));
|
||||
|
||||
reg->transport = transport;
|
||||
reg->timeout = reg->expire = -1;
|
||||
reg->portno = portnum;
|
||||
reg->callid_valid = FALSE;
|
||||
reg->ocseq = INITIAL_CSEQ;
|
||||
if (!ast_strlen_zero(host1.expiry)) {
|
||||
reg->refresh = reg->expiry = reg->configured_expiry = atoi(ast_strip_quoted(host1.expiry, "\"", "\""));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(sip_parse_register_line_test)
|
||||
{
|
||||
int res = AST_TEST_PASS;
|
||||
struct sip_registry *reg;
|
||||
const char *reg1 = "name@domain";
|
||||
const char *reg2 = "name:pass@domain";
|
||||
const char *reg3 = "name@namedomain:pass:authuser@domain";
|
||||
const char *reg4 = "name@namedomain:pass:authuser@domain/extension";
|
||||
const char *reg5 = "tcp://name@namedomain:pass:authuser@domain/extension";
|
||||
const char *reg6 = "tls://name@namedomain:pass:authuser@domain/extension~111";
|
||||
const char *reg7 = "peer?tcp://name@namedomain:pass:authuser@domain:1234/extension~111";
|
||||
const char *reg8 = "peer?name@namedomain:pass:authuser@domain:1234/extension~111";
|
||||
const char *reg9 = "peer?name:pass:authuser:1234/extension~111";
|
||||
const char *reg10 = "@domin:1234";
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = "sip_parse_register_line_test";
|
||||
info->category = "channels/chan_sip/";
|
||||
info->summary = "tests sip register line parsing";
|
||||
info->description =
|
||||
" Tests parsing of various register line configurations."
|
||||
" Verifies output matches expected behavior.";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
/* ---Test reg 1, simple config --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg1, 1) ||
|
||||
strcmp(reg->callback, "s") ||
|
||||
strcmp(reg->username, "name") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "") ||
|
||||
strcmp(reg->secret, "") ||
|
||||
strcmp(reg->peername, "") ||
|
||||
reg->transport != SIP_TRANSPORT_UDP ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh ||
|
||||
reg->expiry ||
|
||||
reg->configured_expiry ||
|
||||
reg->portno != STANDARD_SIP_PORT ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple config failed\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 2, add secret --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg2, 1) ||
|
||||
strcmp(reg->callback, "s") ||
|
||||
strcmp(reg->username, "name") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "") ||
|
||||
strcmp(reg->secret, "pass") ||
|
||||
strcmp(reg->peername, "") ||
|
||||
reg->transport != SIP_TRANSPORT_UDP ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh ||
|
||||
reg->expiry ||
|
||||
reg->configured_expiry ||
|
||||
reg->portno != STANDARD_SIP_PORT ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 2: add secret failed\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 3, add userdomain and authuser --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg3, 1) ||
|
||||
strcmp(reg->callback, "s") ||
|
||||
strcmp(reg->username, "name@namedomain") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "authuser") ||
|
||||
strcmp(reg->secret, "pass") ||
|
||||
strcmp(reg->peername, "") ||
|
||||
reg->transport != SIP_TRANSPORT_UDP ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh ||
|
||||
reg->expiry ||
|
||||
reg->configured_expiry ||
|
||||
reg->portno != STANDARD_SIP_PORT ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 3: add userdomain and authuser failed\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 4, add callback extension --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg4, 1) ||
|
||||
strcmp(reg->callback, "extension") ||
|
||||
strcmp(reg->username, "name@namedomain") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "authuser") ||
|
||||
strcmp(reg->secret, "pass") ||
|
||||
strcmp(reg->peername, "") ||
|
||||
reg->transport != SIP_TRANSPORT_UDP ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh ||
|
||||
reg->expiry ||
|
||||
reg->configured_expiry ||
|
||||
reg->portno != STANDARD_SIP_PORT ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 4: add callback extension failed\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 5, add transport --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg5, 1) ||
|
||||
strcmp(reg->callback, "extension") ||
|
||||
strcmp(reg->username, "name@namedomain") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "authuser") ||
|
||||
strcmp(reg->secret, "pass") ||
|
||||
strcmp(reg->peername, "") ||
|
||||
reg->transport != SIP_TRANSPORT_TCP ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh ||
|
||||
reg->expiry ||
|
||||
reg->configured_expiry ||
|
||||
reg->portno != STANDARD_SIP_PORT ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 5: add transport failed\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 6, change to tls transport, add expiry --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg6, 1) ||
|
||||
strcmp(reg->callback, "extension") ||
|
||||
strcmp(reg->username, "name@namedomain") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "authuser") ||
|
||||
strcmp(reg->secret, "pass") ||
|
||||
strcmp(reg->peername, "") ||
|
||||
reg->transport != SIP_TRANSPORT_TLS ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh != 111 ||
|
||||
reg->expiry != 111 ||
|
||||
reg->configured_expiry != 111 ||
|
||||
reg->portno != STANDARD_TLS_PORT ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 6: change to tls transport and add expiry failed\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 7, change transport to tcp, add custom port, and add peer --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg7, 1) ||
|
||||
strcmp(reg->callback, "extension") ||
|
||||
strcmp(reg->username, "name@namedomain") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "authuser") ||
|
||||
strcmp(reg->secret, "pass") ||
|
||||
strcmp(reg->peername, "peer") ||
|
||||
reg->transport != SIP_TRANSPORT_TCP ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh != 111 ||
|
||||
reg->expiry != 111 ||
|
||||
reg->configured_expiry != 111 ||
|
||||
reg->portno != 1234 ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 7, change transport to tcp, add custom port, and add peer failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 8, remove transport --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (
|
||||
sip_parse_register_line(reg, reg8, 1) ||
|
||||
strcmp(reg->callback, "extension") ||
|
||||
strcmp(reg->username, "name@namedomain") ||
|
||||
strcmp(reg->hostname, "domain") ||
|
||||
strcmp(reg->authuser, "authuser") ||
|
||||
strcmp(reg->secret, "pass") ||
|
||||
strcmp(reg->peername, "peer") ||
|
||||
reg->transport != SIP_TRANSPORT_UDP ||
|
||||
reg->timeout != -1 ||
|
||||
reg->expire != -1 ||
|
||||
reg->refresh != 111 ||
|
||||
reg->expiry != 111 ||
|
||||
reg->configured_expiry != 111 ||
|
||||
reg->portno != 1234 ||
|
||||
reg->callid_valid != FALSE ||
|
||||
reg->ocseq != INITIAL_CSEQ) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 8, remove transport failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 9, missing domain, expected to fail --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (!sip_parse_register_line(reg, reg9, 1)) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 9, missing domain, expected to fail but did not.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 10, missing user, expected to fail --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (!sip_parse_register_line(reg, reg10, 1)) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 10, missing user expected to fail but did not\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
/* ---Test reg 11, no registry object, expected to fail--- */
|
||||
if (!sip_parse_register_line(NULL, reg1, 1)) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 11, no registery object, expected to fail but did not.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* ---Test reg 11, no registry line, expected to fail --- */
|
||||
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
|
||||
goto alloc_fail;
|
||||
} else if (!sip_parse_register_line(reg, NULL, 1)) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 11, NULL register line expected to fail but did not.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
ast_string_field_free_memory(reg);
|
||||
ast_free(reg);
|
||||
|
||||
|
||||
return res;
|
||||
|
||||
alloc_fail:
|
||||
ast_str_set(&args->ast_test_error_str, 0, "Out of memory. \n");
|
||||
return res;
|
||||
}
|
||||
|
||||
int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport)
|
||||
{
|
||||
char *port;
|
||||
|
||||
if (ast_strlen_zero(line)) {
|
||||
return -1;
|
||||
}
|
||||
if ((*hostname = strstr(line, "://"))) {
|
||||
*hostname += 3;
|
||||
|
||||
if (!strncasecmp(line, "tcp", 3))
|
||||
*transport = SIP_TRANSPORT_TCP;
|
||||
else if (!strncasecmp(line, "tls", 3))
|
||||
*transport = SIP_TRANSPORT_TLS;
|
||||
else if (!strncasecmp(line, "udp", 3))
|
||||
*transport = SIP_TRANSPORT_UDP;
|
||||
else
|
||||
ast_log(LOG_NOTICE, "'%.3s' is not a valid transport type on line %d of sip.conf. defaulting to udp.\n", line, lineno);
|
||||
} else {
|
||||
*hostname = line;
|
||||
*transport = SIP_TRANSPORT_UDP;
|
||||
}
|
||||
|
||||
if ((line = strrchr(*hostname, '@')))
|
||||
line++;
|
||||
else
|
||||
line = *hostname;
|
||||
|
||||
if ((port = strrchr(line, ':'))) {
|
||||
*port++ = '\0';
|
||||
|
||||
if (!sscanf(port, "%5u", portnum)) {
|
||||
ast_log(LOG_NOTICE, "'%s' is not a valid port number on line %d of sip.conf. using default.\n", port, lineno);
|
||||
port = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!port) {
|
||||
if (*transport & SIP_TRANSPORT_TLS) {
|
||||
*portnum = STANDARD_TLS_PORT;
|
||||
} else {
|
||||
*portnum = STANDARD_SIP_PORT;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(sip_parse_host_line_test)
|
||||
{
|
||||
int res = AST_TEST_PASS;
|
||||
char *host;
|
||||
int port;
|
||||
enum sip_transport transport;
|
||||
char host1[] = "www.blah.com";
|
||||
char host2[] = "tcp://www.blah.com";
|
||||
char host3[] = "tls://10.10.10.10";
|
||||
char host4[] = "tls://10.10.10.10:1234";
|
||||
char host5[] = "10.10.10.10:1234";
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = "sip_parse_host_line_test";
|
||||
info->category = "channels/chan_sip/";
|
||||
info->summary = "tests sip.conf host line parsing";
|
||||
info->description =
|
||||
" Tests parsing of various host line configurations."
|
||||
" Verifies output matches expected behavior.";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
/* test 1, simple host */
|
||||
sip_parse_host(host1, 1, &host, &port, &transport);
|
||||
if (port != STANDARD_SIP_PORT ||
|
||||
ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
|
||||
transport != SIP_TRANSPORT_UDP) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple host failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* test 2, add tcp transport */
|
||||
sip_parse_host(host2, 1, &host, &port, &transport);
|
||||
if (port != STANDARD_SIP_PORT ||
|
||||
ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
|
||||
transport != SIP_TRANSPORT_TCP) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 2: tcp host failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* test 3, add tls transport */
|
||||
sip_parse_host(host3, 1, &host, &port, &transport);
|
||||
if (port != STANDARD_TLS_PORT ||
|
||||
ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
|
||||
transport != SIP_TRANSPORT_TLS) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 3: tls host failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* test 4, add custom port with tls */
|
||||
sip_parse_host(host4, 1, &host, &port, &transport);
|
||||
if (port != 1234 ||
|
||||
ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
|
||||
transport != SIP_TRANSPORT_TLS) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 4: tls host with custom port failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* test 5, simple host with custom port */
|
||||
sip_parse_host(host5, 1, &host, &port, &transport);
|
||||
if (port != 1234 ||
|
||||
ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
|
||||
transport != SIP_TRANSPORT_UDP) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 5: simple host with custom port failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
return res;
|
||||
|
||||
/* test 6, expected failure with NULL input */
|
||||
if (sip_parse_host(NULL, 1, &host, &port, &transport)) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 6: expected error on NULL input did not occur.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
/*! \brief SIP test registration */
|
||||
void sip_config_parser_register_tests(void)
|
||||
{
|
||||
AST_TEST_REGISTER(sip_parse_register_line_test);
|
||||
AST_TEST_REGISTER(sip_parse_host_line_test);
|
||||
}
|
||||
|
||||
/*! \brief SIP test registration */
|
||||
void sip_config_parser_unregister_tests(void)
|
||||
{
|
||||
AST_TEST_UNREGISTER(sip_parse_register_line_test);
|
||||
AST_TEST_UNREGISTER(sip_parse_host_line_test);
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2010, Digium, Inc.
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief sip.conf parser header file
|
||||
*/
|
||||
|
||||
#include "sip.h"
|
||||
|
||||
#ifndef _SIP_CONF_PARSE_H
|
||||
#define _SIP_CONF_PARSE_H
|
||||
|
||||
|
||||
/*! \brief Parse register=> line in sip.conf
|
||||
*
|
||||
* \retval 0 on success
|
||||
* \retval -1 on failure
|
||||
*/
|
||||
int sip_parse_register_line(struct sip_registry *reg, const char *value, int lineno);
|
||||
|
||||
/*!
|
||||
* \brief parses a config line for a host with a transport
|
||||
* i.e. tls://www.google.com:8056
|
||||
*
|
||||
* \retval 0 on success
|
||||
* \retval -1 on failure
|
||||
*/
|
||||
int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport);
|
||||
|
||||
/*! \brief register config parsing tests */
|
||||
void sip_config_parser_register_tests(void);
|
||||
|
||||
/*! \brief unregister config parsing tests */
|
||||
void sip_config_parser_unregister_tests(void);
|
||||
|
||||
#endif
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2010, Digium, Inc.
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief sip request response parser header file
|
||||
*/
|
||||
|
||||
#ifndef _SIP_REQRESP_H
|
||||
#define _SIP_REQRESP_H
|
||||
|
||||
/*! \brief parses a URI in its components.
|
||||
*
|
||||
* \note
|
||||
* - Multiple scheme's can be specified ',' delimited. ex: "sip:,sips:"
|
||||
* - If a component is not requested, do not split around it. This means
|
||||
* that if we don't have domain, we cannot split name:pass and domain:port.
|
||||
* - It is safe to call with ret_name, pass, domain, port pointing all to
|
||||
* the same place.
|
||||
* - This function overwrites the the uri string.
|
||||
*
|
||||
* \retval 0 on success
|
||||
* \retval -1 on error.
|
||||
*
|
||||
* \verbatim
|
||||
* general form we are expecting is sip:user:password;user-parameters@host:port;uri-parameters?headers
|
||||
* \endverbatim
|
||||
*
|
||||
*/
|
||||
int parse_uri(char *uri, const char *scheme, char **ret_name, char **pass, char **domain, char **port, char **transport);
|
||||
|
||||
/*! \brief Get caller id name from SIP headers, copy into output buffer
|
||||
*
|
||||
* \retval input string pointer placed after display-name field if possible
|
||||
*/
|
||||
const char *get_calleridname(const char *input, char *output, size_t outputsize);
|
||||
|
||||
/*! \brief register request parsing tests */
|
||||
void sip_request_parser_register_tests(void);
|
||||
|
||||
/*! \brief unregister request parsing tests */
|
||||
void sip_request_parser_unregister_tests(void);
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2010, Digium, Inc.
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief sip utils header file
|
||||
*/
|
||||
|
||||
#ifndef _SIP_UTILS_H
|
||||
#define _SIP_UTILS_H
|
||||
|
||||
|
||||
/*! \brief converts ascii port to int representation. If no
|
||||
* pt buffer is provided or the pt has errors when being converted
|
||||
* to an int value, the port provided as the standard is used.
|
||||
*
|
||||
* \retval positive numeric port
|
||||
*/
|
||||
unsigned int port_str2int(const char *pt, unsigned int standard);
|
||||
|
||||
#endif
|
@ -0,0 +1,398 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2010, Digium, Inc.
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief sip request parsing functions and unit tests
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "include/sip.h"
|
||||
#include "include/reqresp_parser.h"
|
||||
|
||||
/*! \brief * parses a URI in its components.*/
|
||||
int parse_uri(char *uri, const char *scheme, char **ret_name, char **pass, char **domain, char **port, char **transport)
|
||||
{
|
||||
char *name = NULL;
|
||||
char *tmp; /* used as temporary place holder */
|
||||
int error = 0;
|
||||
|
||||
/* check for valid input */
|
||||
if (ast_strlen_zero(uri)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* strip [?headers] from end of uri */
|
||||
if ((tmp = strrchr(uri, '?'))) {
|
||||
*tmp = '\0';
|
||||
}
|
||||
|
||||
/* init field as required */
|
||||
if (pass)
|
||||
*pass = "";
|
||||
if (port)
|
||||
*port = "";
|
||||
if (scheme) {
|
||||
int l;
|
||||
char *scheme2 = ast_strdupa(scheme);
|
||||
char *cur = strsep(&scheme2, ",");
|
||||
for (; !ast_strlen_zero(cur); cur = strsep(&scheme2, ",")) {
|
||||
l = strlen(cur);
|
||||
if (!strncasecmp(uri, cur, l)) {
|
||||
uri += l;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ast_strlen_zero(cur)) {
|
||||
ast_debug(1, "No supported scheme found in '%s' using the scheme[s] %s\n", uri, scheme);
|
||||
error = -1;
|
||||
}
|
||||
}
|
||||
if (transport) {
|
||||
char *t, *type = "";
|
||||
*transport = "";
|
||||
if ((t = strstr(uri, "transport="))) {
|
||||
strsep(&t, "=");
|
||||
if ((type = strsep(&t, ";"))) {
|
||||
*transport = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!domain) {
|
||||
/* if we don't want to split around domain, keep everything as a name,
|
||||
* so we need to do nothing here, except remember why.
|
||||
*/
|
||||
} else {
|
||||
/* store the result in a temp. variable to avoid it being
|
||||
* overwritten if arguments point to the same place.
|
||||
*/
|
||||
char *c, *dom = "";
|
||||
|
||||
if ((c = strchr(uri, '@')) == NULL) {
|
||||
/* domain-only URI, according to the SIP RFC. */
|
||||
dom = uri;
|
||||
name = "";
|
||||
} else {
|
||||
*c++ = '\0';
|
||||
dom = c;
|
||||
name = uri;
|
||||
}
|
||||
|
||||
/* Remove parameters in domain and name */
|
||||
dom = strsep(&dom, ";");
|
||||
name = strsep(&name, ";");
|
||||
|
||||
if (port && (c = strchr(dom, ':'))) { /* Remove :port */
|
||||
*c++ = '\0';
|
||||
*port = c;
|
||||
}
|
||||
if (pass && (c = strchr(name, ':'))) { /* user:password */
|
||||
*c++ = '\0';
|
||||
*pass = c;
|
||||
}
|
||||
*domain = dom;
|
||||
}
|
||||
if (ret_name) /* same as for domain, store the result only at the end */
|
||||
*ret_name = name;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(sip_parse_uri_test)
|
||||
{
|
||||
int res = AST_TEST_PASS;
|
||||
char *name, *pass, *domain, *port, *transport;
|
||||
char uri1[] = "sip:name@host";
|
||||
char uri2[] = "sip:name@host;transport=tcp";
|
||||
char uri3[] = "sip:name:secret@host;transport=tcp";
|
||||
char uri4[] = "sip:name:secret@host:port;transport=tcp?headers=%40%40testblah&headers2=blah%20blah";
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = "sip_uri_parse_test";
|
||||
info->category = "channels/chan_sip/";
|
||||
info->summary = "tests sip uri parsing";
|
||||
info->description =
|
||||
" Tests parsing of various URIs"
|
||||
" Verifies output matches expected behavior.";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Test 1, simple URI */
|
||||
name = pass = domain = port = transport = NULL;
|
||||
if (parse_uri(uri1, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
|
||||
strcmp(name, "name") ||
|
||||
!ast_strlen_zero(pass) ||
|
||||
strcmp(domain, "host") ||
|
||||
!ast_strlen_zero(port) ||
|
||||
!ast_strlen_zero(transport)) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple uri failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* Test 2, add tcp transport */
|
||||
name = pass = domain = port = transport = NULL;
|
||||
if (parse_uri(uri2, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
|
||||
strcmp(name, "name") ||
|
||||
!ast_strlen_zero(pass) ||
|
||||
strcmp(domain, "host") ||
|
||||
!ast_strlen_zero(port) ||
|
||||
strcmp(transport, "tcp")) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 2: uri with addtion of tcp transport failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* Test 3, add secret */
|
||||
name = pass = domain = port = transport = NULL;
|
||||
if (parse_uri(uri3, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
|
||||
strcmp(name, "name") ||
|
||||
strcmp(pass, "secret") ||
|
||||
strcmp(domain, "host") ||
|
||||
!ast_strlen_zero(port) ||
|
||||
strcmp(transport, "tcp")) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 3: uri with addition of secret failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* Test 4, add port and unparsed header field*/
|
||||
name = pass = domain = port = transport = NULL;
|
||||
if (parse_uri(uri4, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
|
||||
strcmp(name, "name") ||
|
||||
strcmp(pass, "secret") ||
|
||||
strcmp(domain, "host") ||
|
||||
strcmp(port, "port") ||
|
||||
strcmp(transport, "tcp")) {
|
||||
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 4: add port and unparsed header field failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* Test 5, verify parse_uri does not crash when given a NULL uri */
|
||||
name = pass = domain = port = transport = NULL;
|
||||
if (!parse_uri(NULL, "sip:,sips:", &name, &pass, &domain, &port, &transport)) {
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 5: passing a NULL uri failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* Test 6, verify parse_uri does not crash when given a NULL output parameters */
|
||||
name = pass = domain = port = transport = NULL;
|
||||
if (parse_uri(uri4, "sip:,sips:", NULL, NULL, NULL, NULL, NULL)) {
|
||||
ast_str_append(&args->ast_test_error_str, 0, "Test 6: passing NULL output parameters failed.\n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*! \brief Get caller id name from SIP headers, copy into output buffer
|
||||
*
|
||||
* \retval input string pointer placed after display-name field if possible
|
||||
*/
|
||||
const char *get_calleridname(const char *input, char *output, size_t outputsize)
|
||||
{
|
||||
/* From RFC3261:
|
||||
*
|
||||
* From = ( "From" / "f" ) HCOLON from-spec
|
||||
* from-spec = ( name-addr / addr-spec ) *( SEMI from-param )
|
||||
* name-addr = [ display-name ] LAQUOT addr-spec RAQUOT
|
||||
* display-name = *(token LWS)/ quoted-string
|
||||
* token = 1*(alphanum / "-" / "." / "!" / "%" / "*"
|
||||
* / "_" / "+" / "`" / "'" / "~" )
|
||||
* quoted-string = SWS DQUOTE *(qdtext / quoted-pair ) DQUOTE
|
||||
* qdtext = LWS / %x21 / %x23-5B / %x5D-7E
|
||||
* / UTF8-NONASCII
|
||||
* quoted-pair = "\" (%x00-09 / %x0B-0C / %x0E-7F)
|
||||
*
|
||||
* HCOLON = *WSP ":" SWS
|
||||
* SWS = [LWS]
|
||||
* LWS = *[*WSP CRLF] 1*WSP
|
||||
* WSP = (SP / HTAB)
|
||||
*
|
||||
* Deviations from it:
|
||||
* - following CRLF's in LWS is not done (here at least)
|
||||
* - ascii NUL is never legal as it terminates the C-string
|
||||
* - utf8-nonascii is not checked for validity
|
||||
*/
|
||||
char *orig_output = output;
|
||||
const char *orig_input = input;
|
||||
|
||||
/* clear any empty characters in the beginning */
|
||||
input = ast_skip_blanks(input);
|
||||
|
||||
/* no data at all or no storage room? */
|
||||
if (!input || *input == '<' || !outputsize || !output) {
|
||||
return orig_input;
|
||||
}
|
||||
|
||||
/* make sure the output buffer is initilized */
|
||||
*orig_output = '\0';
|
||||
|
||||
/* make room for '\0' at the end of the output buffer */
|
||||
outputsize--;
|
||||
|
||||
/* quoted-string rules */
|
||||
if (input[0] == '"') {
|
||||
input++; /* skip the first " */
|
||||
|
||||
for (;((outputsize > 0) && *input); input++) {
|
||||
if (*input == '"') { /* end of quoted-string */
|
||||
break;
|
||||
} else if (*input == 0x5c) { /* quoted-pair = "\" (%x00-09 / %x0B-0C / %x0E-7F) */
|
||||
input++;
|
||||
if (!*input || (unsigned char)*input > 0x7f || *input == 0xa || *input == 0xd) {
|
||||
continue; /* not a valid quoted-pair, so skip it */
|
||||
}
|
||||
} else if (((*input != 0x9) && ((unsigned char) *input < 0x20)) ||
|
||||
(*input == 0x7f)) {
|
||||
continue; /* skip this invalid character. */
|
||||
}
|
||||
|
||||
*output++ = *input;
|
||||
outputsize--;
|
||||
}
|
||||
|
||||
/* if this is successful, input should be at the ending quote */
|
||||
if (!input || *input != '"') {
|
||||
ast_log(LOG_WARNING, "No ending quote for display-name was found\n");
|
||||
*orig_output = '\0';
|
||||
return orig_input;
|
||||
}
|
||||
|
||||
/* make sure input is past the last quote */
|
||||
input++;
|
||||
|
||||
/* terminate outbuf */
|
||||
*output = '\0';
|
||||
} else { /* either an addr-spec or tokenLWS-combo */
|
||||
for (;((outputsize > 0) && *input); input++) {
|
||||
/* token or WSP (without LWS) */
|
||||
if ((*input >= '0' && *input <= '9') || (*input >= 'A' && *input <= 'Z')
|
||||
|| (*input >= 'a' && *input <= 'z') || *input == '-' || *input == '.'
|
||||
|| *input == '!' || *input == '%' || *input == '*' || *input == '_'
|
||||
|| *input == '+' || *input == '`' || *input == '\'' || *input == '~'
|
||||
|| *input == 0x9 || *input == ' ') {
|
||||
*output++ = *input;
|
||||
outputsize -= 1;
|
||||
} else if (*input == '<') { /* end of tokenLWS-combo */
|
||||
/* we could assert that the previous char is LWS, but we don't care */
|
||||
break;
|
||||
} else if (*input == ':') {
|
||||
/* This invalid character which indicates this is addr-spec rather than display-name. */
|
||||
*orig_output = '\0';
|
||||
return orig_input;
|
||||
} else { /* else, invalid character we can skip. */
|
||||
continue; /* skip this character */
|
||||
}
|
||||
}
|
||||
|
||||
/* set NULL while trimming trailing whitespace */
|
||||
do {
|
||||
*output-- = '\0';
|
||||
} while (*output == 0x9 || *output == ' '); /* we won't go past orig_output as first was a non-space */
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(get_calleridname_test)
|
||||
{
|
||||
int res = AST_TEST_PASS;
|
||||
const char *in1 = "\" quoted-text internal \\\" quote \"<stuff>";
|
||||
const char *in2 = " token text with no quotes <stuff>";
|
||||
const char *overflow1 = " \"quoted-text overflow 1234567890123456789012345678901234567890\" <stuff>";
|
||||
const char *noendquote = " \"quoted-text no end <stuff>";
|
||||
const char *addrspec = " \"sip:blah@blah <stuff>";
|
||||
const char *after_dname;
|
||||
char dname[40];
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = "sip_get_calleridname_test";
|
||||
info->category = "channels/chan_sip/";
|
||||
info->summary = "decodes callerid name from sip header";
|
||||
info->description = "Decodes display-name field of sip header. Checks for valid output and expected failure cases.";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
/* quoted-text with backslash escaped quote */
|
||||
after_dname = get_calleridname(in1, dname, sizeof(dname));
|
||||
ast_test_status_update(&args->status_update, "display-name1: %s\nafter: %s\n", dname, after_dname);
|
||||
if (strcmp(dname, " quoted-text internal \" quote ")) {
|
||||
ast_test_status_update(&args->status_update, "display-name1 test failed\n");
|
||||
ast_str_append(&args->ast_test_error_str, 0, "quoted-text with internal backslash decode failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* token text */
|
||||
after_dname = get_calleridname(in2, dname, sizeof(dname));
|
||||
ast_test_status_update(&args->status_update, "display-name2: %s\nafter: %s\n", dname, after_dname);
|
||||
if (strcmp(dname, "token text with no quotes")) {
|
||||
ast_test_status_update(&args->status_update, "display-name2 test failed\n");
|
||||
ast_str_append(&args->ast_test_error_str, 0, "token text with decode failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* quoted-text buffer overflow */
|
||||
after_dname = get_calleridname(overflow1, dname, sizeof(dname));
|
||||
ast_test_status_update(&args->status_update, "overflow display-name1: %s\nafter: %s\n", dname, after_dname);
|
||||
if (*dname != '\0' && after_dname != overflow1) {
|
||||
ast_test_status_update(&args->status_update, "overflow display-name1 test failed\n");
|
||||
ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer overflow check failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* quoted-text buffer with no terminating end quote */
|
||||
after_dname = get_calleridname(noendquote, dname, sizeof(dname));
|
||||
ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
|
||||
if (*dname != '\0' && after_dname != noendquote) {
|
||||
ast_test_status_update(&args->status_update, "no end quote for quoted-text display-name failed\n");
|
||||
ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer check no terminating end quote failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* addr-spec rather than display-name. */
|
||||
after_dname = get_calleridname(addrspec, dname, sizeof(dname));
|
||||
ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
|
||||
if (*dname != '\0' && after_dname != addrspec) {
|
||||
ast_test_status_update(&args->status_update, "detection of addr-spec failed\n");
|
||||
ast_str_append(&args->ast_test_error_str, 0, "detection of addr-spec failed. \n");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void sip_request_parser_register_tests(void)
|
||||
{
|
||||
AST_TEST_REGISTER(get_calleridname_test);
|
||||
AST_TEST_REGISTER(sip_parse_uri_test);
|
||||
}
|
||||
void sip_request_parser_unregister_tests(void)
|
||||
{
|
||||
AST_TEST_UNREGISTER(sip_parse_uri_test);
|
||||
AST_TEST_UNREGISTER(get_calleridname_test);
|
||||
}
|
Loading…
Reference in new issue