Logging: Add debug logging categories

Added debug logging categories that allow a user to output debug
information based on a specified category. This lets the user limit,
and filter debug output to data relevant to a particular context,
or topic. For instance the following categories are now available for
debug logging purposes:

  dtls, dtls_packet, ice, rtcp, rtcp_packet, rtp, rtp_packet,
  stun, stun_packet

These debug categories can be enable/disable via an Asterisk CLI command.

While this overrides, and outputs debug data, core system debugging is
not affected by this patch. Statements still output at their appropriate
debug level. As well backwards compatibility has been maintained with
past debug groups that could be enabled using the CLI (e.g. rtpdebug,
stundebug, etc.).

ASTERISK-29054 #close

Change-Id: I6e6cb247bb1f01dbf34750b2cd98e5b5b41a1849
(cherry picked from commit 56028426de)
This commit is contained in:
Kevin Harwell
2020-08-28 16:32:37 -05:00
parent a6faa53af0
commit 6255e7976c
11 changed files with 1035 additions and 200 deletions

View File

@@ -32,14 +32,14 @@
#include "asterisk.h"
#include "asterisk/logger.h"
#include "asterisk/logger_category.h"
#include "asterisk/_private.h"
#include "asterisk/stun.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/channel.h"
static int stundebug; /*!< Are we debugging stun? */
/*!
* \brief STUN support code
*
@@ -178,9 +178,10 @@ struct stun_state {
static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
{
if (stundebug)
if (ast_debug_stun_packet_is_allowed) {
ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr), ntohs(attr->len));
stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr), ntohs(attr->len));
}
switch (ntohs(attr->attr)) {
case STUN_USERNAME:
state->username = (const char *) (attr->value);
@@ -189,10 +190,12 @@ static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
state->password = (const char *) (attr->value);
break;
default:
if (stundebug)
if (ast_debug_stun_packet_is_allowed) {
ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr), ntohs(attr->len));
stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr), ntohs(attr->len));
}
}
return 0;
}
@@ -275,35 +278,35 @@ int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data,
* while 'data' is advanced accordingly.
*/
if (len < sizeof(struct stun_header)) {
ast_debug(1, "Runt STUN packet (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
ast_debug_stun(1, "Runt STUN packet (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
return -1;
}
len -= sizeof(struct stun_header);
data += sizeof(struct stun_header);
x = ntohs(hdr->msglen); /* len as advertised in the message */
if (stundebug)
if (ast_debug_stun_packet_is_allowed)
ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), (unsigned)ntohs(hdr->msgtype), x);
if (x > len) {
ast_debug(1, "Scrambled STUN packet length (got %d, expecting %d)\n", x, (int)len);
ast_debug_stun(1, "Scrambled STUN packet length (got %d, expecting %d)\n", x, (int)len);
} else
len = x;
memset(&st, 0, sizeof(st));
while (len) {
if (len < sizeof(struct stun_attr)) {
ast_debug(1, "Runt Attribute (got %d, expecting %d)\n", (int)len, (int) sizeof(struct stun_attr));
ast_debug_stun(1, "Runt Attribute (got %d, expecting %d)\n", (int)len, (int) sizeof(struct stun_attr));
break;
}
attr = (struct stun_attr *)data;
/* compute total attribute length */
x = ntohs(attr->len) + sizeof(struct stun_attr);
if (x > len) {
ast_debug(1, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", x, (int)len);
ast_debug_stun(1, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", x, (int)len);
break;
}
if (stun_cb)
stun_cb(attr, arg);
if (stun_process_attr(&st, attr)) {
ast_debug(1, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr));
ast_debug_stun(1, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), (unsigned)ntohs(attr->attr));
break;
}
/* Clear attribute id: in case previous entry was a string,
@@ -337,7 +340,7 @@ int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data,
attr = (struct stun_attr *)resp->ies;
switch (ntohs(hdr->msgtype)) {
case STUN_BINDREQ:
if (stundebug)
if (ast_debug_stun_packet_is_allowed)
ast_verbose("STUN Bind Request, username: %s\n",
st.username ? st.username : "<none>");
if (st.username) {
@@ -355,7 +358,7 @@ int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data,
ret = AST_STUN_ACCEPT;
break;
default:
if (stundebug)
if (ast_debug_stun_packet_is_allowed)
ast_verbose("Dunno what to do with STUN message %04x (%s)\n", (unsigned)ntohs(hdr->msgtype), stun_msg2str(ntohs(hdr->msgtype)));
}
}
@@ -416,7 +419,7 @@ int ast_stun_request(int s, struct sockaddr_in *dst,
/* Send STUN message. */
res = stun_send(s, dst, req);
if (res < 0) {
ast_debug(1, "stun_send try %d failed: %s\n", retry, strerror(errno));
ast_debug_stun(1, "stun_send try %d failed: %s\n", retry, strerror(errno));
break;
}
if (!answer) {
@@ -459,7 +462,7 @@ try_again:
res = recvfrom(s, rsp_buf, sizeof(rsp_buf) - 1,
0, (struct sockaddr *) &src, &srclen);
if (res < 0) {
ast_debug(1, "recvfrom try %d failed: %s\n", retry, strerror(errno));
ast_debug_stun(1, "recvfrom try %d failed: %s\n", retry, strerror(errno));
break;
}
@@ -500,13 +503,13 @@ static char *handle_cli_stun_set_debug(struct ast_cli_entry *e, int cmd, struct
return CLI_SHOWUSAGE;
if (!strncasecmp(a->argv[e->args-1], "on", 2))
stundebug = 1;
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_STUN_PACKET, AST_LOG_CATEGORY_ENABLED);
else if (!strncasecmp(a->argv[e->args-1], "off", 3))
stundebug = 0;
ast_debug_category_set_sublevel(AST_LOG_CATEGORY_STUN_PACKET, AST_LOG_CATEGORY_DISABLED);
else
return CLI_SHOWUSAGE;
ast_cli(a->fd, "STUN Debugging %s\n", stundebug ? "Enabled" : "Disabled");
ast_cli(a->fd, "STUN Debugging %s\n", ast_debug_stun_packet_is_allowed ? "Enabled" : "Disabled");
return CLI_SUCCESS;
}
@@ -514,9 +517,26 @@ static struct ast_cli_entry cli_stun[] = {
AST_CLI_DEFINE(handle_cli_stun_set_debug, "Enable/Disable STUN debugging"),
};
static uintmax_t debug_category_stun_id;
uintmax_t ast_debug_category_stun_id(void)
{
return debug_category_stun_id;
}
static uintmax_t debug_category_stun_packet_id;
uintmax_t ast_debug_category_stun_packet_id(void)
{
return debug_category_stun_packet_id;
}
static void stun_shutdown(void)
{
ast_cli_unregister_multiple(cli_stun, sizeof(cli_stun) / sizeof(struct ast_cli_entry));
ast_debug_category_unregister(AST_LOG_CATEGORY_STUN_PACKET);
ast_debug_category_unregister(AST_LOG_CATEGORY_STUN);
}
/*! \brief Initialize the STUN system in Asterisk */
@@ -524,4 +544,7 @@ void ast_stun_init(void)
{
ast_cli_register_multiple(cli_stun, sizeof(cli_stun) / sizeof(struct ast_cli_entry));
ast_register_cleanup(stun_shutdown);
debug_category_stun_id = ast_debug_category_register(AST_LOG_CATEGORY_STUN);
debug_category_stun_packet_id = ast_debug_category_register(AST_LOG_CATEGORY_STUN_PACKET);
}