28 #include <netinet/in.h> 63 } __attribute__((__packed__));
90 struct in6_addr ip6_src;
91 struct in6_addr ip6_dst;
181 pj_sockaddr *source, pj_sockaddr *destination)
186 .ts_usec = now.tv_usec,
198 void *pcap_ip_header;
199 size_t pcap_ip_header_len;
204 pcap_udp_header.
src = ntohs(pj_sockaddr_get_port(source));
206 pcap_udp_header.
src = ntohs(0);
209 pcap_udp_header.
dst = ntohs(pj_sockaddr_get_port(destination));
211 pcap_udp_header.
dst = ntohs(0);
213 pcap_udp_header.
length = ntohs(
sizeof(
struct pcap_udp_header) + msg_len);
216 if ((source && source->addr.sa_family == pj_AF_INET()) ||
217 (destination && destination->addr.sa_family == pj_AF_INET())) {
218 pcap_ethernet_header.
type = htons(0x0800);
219 pcap_ip_header = &pcap_ipv4_header;
220 pcap_ip_header_len =
sizeof(
struct pcap_ipv4_header);
222 memcpy(&pcap_ipv4_header.
ip_src, pj_sockaddr_get_addr(source), pj_sockaddr_get_addr_len(source));
225 memcpy(&pcap_ipv4_header.
ip_dst, pj_sockaddr_get_addr(destination), pj_sockaddr_get_addr_len(destination));
227 pcap_ipv4_header.
ip_len = htons(
sizeof(
struct pcap_udp_header) +
sizeof(
struct pcap_ipv4_header) + msg_len);
230 pcap_ethernet_header.
type = htons(0x86DD);
231 pcap_ip_header = &pcap_ipv6_header;
232 pcap_ip_header_len =
sizeof(
struct pcap_ipv6_header);
234 memcpy(&pcap_ipv6_header.
ip6_src, pj_sockaddr_get_addr(source), pj_sockaddr_get_addr_len(source));
237 memcpy(&pcap_ipv6_header.
ip6_dst, pj_sockaddr_get_addr(destination), pj_sockaddr_get_addr_len(destination));
239 pcap_ipv6_header.
ip6_ctlun.
ip6_un1.ip6_un1_plen = htons(
sizeof(
struct pcap_udp_header) + msg_len);
244 pcap_record_header.
incl_len = pcap_record_header.
orig_len =
sizeof(pcap_ethernet_header) + pcap_ip_header_len +
sizeof(pcap_udp_header) + msg_len;
249 if (fwrite(&pcap_record_header,
sizeof(
struct pcap_record_header), 1, session->
pcap_file) != 1) {
252 if (fwrite(&pcap_ethernet_header,
sizeof(
struct pcap_ethernet_header), 1, session->
pcap_file) != 1) {
255 if (fwrite(pcap_ip_header, pcap_ip_header_len, 1, session->
pcap_file) != 1) {
258 if (fwrite(&pcap_udp_header,
sizeof(
struct pcap_udp_header), 1, session->
pcap_file) != 1) {
261 if (fwrite(msg, msg_len, 1, session->
pcap_file) != 1) {
273 if (!
pjsip_log_test_addr(default_logger, tdata->tp_info.dst_name, tdata->tp_info.dst_port)) {
280 ast_verbose(
"<--- Transmitting SIP %s (%d bytes) to %s:%s --->\n%.*s\n",
281 tdata->msg->type == PJSIP_REQUEST_MSG ?
"request" :
"response",
282 (
int) (tdata->buf.cur - tdata->buf.start),
283 tdata->tp_info.transport->type_name,
284 pj_sockaddr_print(&tdata->tp_info.dst_addr, buffer,
sizeof(buffer), 3),
285 (
int) (tdata->buf.end - tdata->buf.start), tdata->buf.start);
290 NULL, &tdata->tp_info.dst_addr);
300 if (!rdata->msg_info.msg) {
305 if (!
pjsip_log_test_addr(default_logger, rdata->pkt_info.src_name, rdata->pkt_info.src_port)) {
312 ast_verbose(
"<--- Received SIP %s (%d bytes) from %s:%s --->\n%s\n",
313 rdata->msg_info.msg->type == PJSIP_REQUEST_MSG ?
"request" :
"response",
315 rdata->tp_info.transport->type_name,
316 pj_sockaddr_print(&rdata->pkt_info.src_addr, buffer,
sizeof(buffer), 3),
317 rdata->pkt_info.packet);
322 &rdata->pkt_info.src_addr,
NULL);
329 .name = {
"Logging Module", 14 },
345 ast_cli(fd,
"PJSIP Logging enabled\n");
353 const char *
host = arg;
369 mask = strrchr(host,
'/');
379 if (!default_logger->
matches || error) {
381 ast_cli(fd,
"Failed to add address '%s' for logging\n", host);
419 ast_cli(fd,
"PJSIP Logging disabled\n");
431 ast_cli(fd,
"PJSIP Logging to verbose has been %s\n",
ast_true(arg) ?
"enabled" :
"disabled");
454 default_logger->
pcap_file = fopen(arg,
"wb");
457 ast_cli(fd,
"Failed to open file '%s' for pcap writing\n", arg);
460 fwrite(&pcap_header, 1,
sizeof(
struct pcap_header), default_logger->
pcap_file);
465 ast_cli(fd,
"PJSIP logging to pcap file '%s'\n", arg);
475 e->
command =
"pjsip set logger {on|off|host|add|verbose|pcap}";
477 "Usage: pjsip set logger {on|off|host <name/subnet>|add <name/subnet>|verbose <on/off>|pcap <filename>}\n" 478 " Enables or disabling logging of SIP packets\n" 479 " read on ports bound to PJSIP transports either\n" 480 " globally or enables logging for an individual\n" 490 if (!strcasecmp(what,
"on")) {
492 }
else if (!strcasecmp(what,
"off")) {
495 }
else if (a->
argc == e->
args + 1) {
496 if (!strcasecmp(what,
"host")) {
498 }
else if (!strcasecmp(what,
"add")) {
500 }
else if (!strcasecmp(what,
"verbose")) {
502 }
else if (!strcasecmp(what,
"pcap")) {
551 if (!default_logger) {
575 default_logger =
NULL;
585 .requires =
"res_pjsip",
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
#define AST_SOCKADDR_BUFLEN
#define AST_CLI_DEFINE(fn, txt,...)
Asterisk main include file. File version handling, generic pbx functions.
static void check_debug(void)
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
char pcap_filename[PATH_MAX]
Filename used for the pcap file.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
unsigned int log_to_verbose
Whether to log to verbose or not.
descriptor for a cli entry.
static int debug
Global debug status.
static char * pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
char * ast_sip_get_debug(void)
Retrieve the system debug setting (yes|no|host).
enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr)
Apply a set of rules to a given IP address.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
#define ao2_alloc_options(data_size, destructor_fn, options)
static const struct ast_sorcery_observer global_observer
void ast_verbose(const char *fmt,...)
void ast_cli(int fd, const char *fmt,...)
static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
Socket address structure.
static void pjsip_logger_session_destroy(void *obj)
Destructor for logger session.
int args
This gets set in ast_cli_register()
#define ast_strlen_zero(foo)
static int load_module(void)
static char * pjsip_disable_logger(int fd)
unsigned int log_to_pcap
Whether to log to pcap or not.
internal representation of ACL entries In principle user applications would have no need for this...
static pjsip_module logging_module
static char * pjsip_set_logger_pcap(int fd, const char *arg)
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
static void global_reloaded(const char *object_type)
static struct ast_mansession session
Access Control of various sorts.
int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr, const char *name, int flag, int family)
Return the first entry from ast_sockaddr_resolve filtered by address family.
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Add an observer to a specific object type.
static struct pjsip_logger_session * pjsip_logger_session_alloc(void)
Allocator for logger session.
static int unload_module(void)
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
static char * pjsip_enable_logger_all(int fd)
Interface for a sorcery object type observer.
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
static struct ast_cli_entry cli_pjsip[]
Module has failed to load, may be in an inconsistent state.
void(* loaded)(const char *object_type)
Callback for when an object type is loaded/reloaded.
Support for logging to various files, console and syslog Configuration in file logger.conf.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static char * pjsip_enable_logger_host(int fd, const char *arg, unsigned int add_host)
void ast_free_ha(struct ast_ha *ha)
Free a list of HAs.
static char * pjsip_set_logger_verbose(int fd, const char *arg)
static struct pjsip_logger_session * default_logger
The default logger session.
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Remove an observer from a specific object type.
struct ast_ha * ast_append_ha_with_port(const char *sense, const char *stuff, struct ast_ha *path, int *error)
Add a new rule with optional port to a list of HAs.
FILE * pcap_file
The pcap file itself.
unsigned int enabled
Whether the session is enabled or not.
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
static int pjsip_log_test_addr(const struct pjsip_logger_session *session, const char *address, int port)
See if we pass debug IP filter.
static void pjsip_logger_write_to_pcap(struct pjsip_logger_session *session, const char *msg, size_t msg_len, pj_sockaddr *source, pj_sockaddr *destination)
unsigned int log_all_traffic
Whether the session is logging all traffic or not.
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"...
void ast_sip_unregister_service(pjsip_module *module)
struct ast_ha * matches
Explicit addresses or ranges being logged.
int error(const char *format,...)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)