233 #include <inttypes.h> 278 #include "sip/include/dialplan_functions.h" 682 .resync_threshold = 1000,
718 {
NONE,
"-",
"unknown",
"unknown" },
720 {
DIALOG_INFO_XML,
"dialog",
"application/dialog-info+xml",
"dialog-info+xml" },
721 {
CPIM_PIDF_XML,
"presence",
"application/cpim-pidf+xml",
"cpim-pidf+xml" },
722 {
PIDF_XML,
"presence",
"application/pidf+xml",
"pidf+xml" },
723 {
XPIDF_XML,
"presence",
"application/xpidf+xml",
"xpidf+xml" },
724 {
MWI_NOTIFICATION,
"message-summary",
"application/simple-message-summary",
"mwi" }
811 #define SIP_PEDANTIC_DECODE(str) \ 812 if (sip_cfg.pedanticsipchecking && !ast_strlen_zero(str)) { \ 813 ast_uri_decode(str, ast_uri_sip_user); \ 946 static const struct {
956 static const struct {
1042 #define sip_pvt_lock(x) ao2_lock(x) 1043 #define sip_pvt_trylock(x) ao2_trylock(x) 1044 #define sip_pvt_unlock(x) ao2_unlock(x) 1056 #define BOGUS_PEER_MD5SECRET "intentionally_invalid_md5_string" 1074 #define SIP_TRANSPORT_STR_BUFSIZE 128 1155 #define UNLINK(element, head, prev) do { \ 1157 (prev)->next = (element)->next; \ 1159 (head) = (element)->next; \ 1237 const char *
file,
int line,
const char *func);
1238 #define find_call(req, addr, intended_method) \ 1239 __find_call(req, addr, intended_method, __FILE__, __LINE__, __PRETTY_FUNCTION__) 1277 int debug,
int *min_packet_size,
int *max_packet_size);
1289 const char *secret,
const char *md5secret,
int sipmethod,
1335 #define peer_in_destruction(peer) (ao2_ref(peer, 0) == 0) 1387 const char *
name,
int flag,
unsigned int transport);
1446 static const char *
find_alias(
const char *
name,
const char *_default);
1549 static int parse_minse(
const char *p_hdrval,
int *
const p_interval);
1573 .description =
"Session Initiation Protocol (SIP)",
1671 .name =
"call-completion",
1699 if (!(epa_entry =
ao2_t_alloc(
sizeof(*epa_entry), static_data->
destructor,
"Allocate new EPA entry"))) {
1799 expires_ms = expires * 1000;
1841 const char *uri = arg;
1856 const char *uri = arg;
1871 struct sip_pvt *call_pvt = arg;
2023 return monitor_instance->
core_id;
2047 ao2_t_ref(monitor_instance->
suspension_entry, -1,
"Decrementing suspension entry refcount in sip_monitor_instance_destructor");
2056 if (!monitor_instance) {
2061 ao2_ref(monitor_instance, -1);
2069 ao2_link(sip_monitor_instances, monitor_instance);
2070 return monitor_instance;
2106 if (!monitor_instance) {
2127 ao2_t_ref(monitor, +1,
"Adding a ref to the monitor for the scheduler");
2142 ast_str_append(&body, 0,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
2149 ast_str_append(&body, 0,
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"%s\">\n", presentity);
2151 ast_str_append(&body, 0,
"<status><basic>%s</basic></status>\n", state ==
CC_OPEN ?
"open" :
"closed");
2164 if (!monitor_instance) {
2172 ao2_ref(monitor_instance, -1);
2175 if (!(cc_entry =
ast_calloc(1,
sizeof(*cc_entry)))) {
2176 ast_log(
LOG_WARNING,
"Unable to allocate space for instance data of EPA entry for call-completion\n");
2177 ao2_ref(monitor_instance, -1);
2214 if (!monitor_instance) {
2235 if (*sched_id != -1) {
2237 ao2_t_ref(monitor, -1,
"Removing scheduler's reference to the monitor");
2245 ao2_unlink(sip_monitor_instances, monitor_instance);
2255 static const char cc_purpose[] =
"purpose=call-completion";
2256 static const int cc_purpose_len =
sizeof(cc_purpose) - 1;
2263 uri =
strsep(&call_info,
";");
2265 while ((purpose =
strsep(&call_info,
";"))) {
2266 if (!strncmp(purpose, cc_purpose, cc_purpose_len)) {
2276 while ((service_str =
strsep(&call_info,
";"))) {
2277 if (!strncmp(service_str,
"m=", 2)) {
2290 strsep(&service_str,
"=");
2361 ao2_ref(monitor_instance, -1);
2386 .name =
"SIP TCP server",
2397 .name =
"SIP TLS server",
2404 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args) 2413 for (cur = table; cur->
s; cur++) {
2428 for (cur = table; cur->
s; cur++) {
2429 if (!strcasecmp(cur->
s, s)) {
2459 && !strchr(
"-.!%*_+`'~", *str)) {
2479 code = reason->
code;
2496 #define check_request_transport(peer, tmpl) ({ \ 2498 if (peer->socket.type == tmpl->socket.type) \ 2500 else if (!(peer->transports & tmpl->socket.type)) {\ 2501 ast_log(LOG_ERROR, \ 2502 "'%s' is not a valid transport for '%s'. we only use '%s'! ending call.\n", \ 2503 sip_get_transport(tmpl->socket.type), peer->name, get_transport_list(peer->transports) \ 2506 } else if (peer->socket.type & AST_TRANSPORT_TLS) { \ 2507 ast_log(LOG_WARNING, \ 2508 "peer '%s' HAS NOT USED (OR SWITCHED TO) TLS in favor of '%s' (but this was allowed in sip.conf)!\n", \ 2509 peer->name, sip_get_transport(tmpl->socket.type) \ 2513 "peer '%s' has contacted us over %s even though we prefer %s.\n", \ 2514 peer->name, sip_get_transport(tmpl->socket.type), sip_get_transport(peer->socket.type) \ 2527 for (v = src ; v ; v = v->
next) {
2573 ao2_t_ref(packet, -1,
"thread destruction, removing packet from frame queue");
2593 ao2_t_ref(th, -1,
"Failed to open alert pipe on sip_threadinfo");
2594 ast_log(
LOG_ERROR,
"Could not create sip alert pipe in tcptls thread, error %s\n", strerror(
errno));
2597 ao2_t_ref(tcptls_session, +1,
"tcptls_session ref for sip_threadinfo object");
2600 ao2_t_link(threadt, th,
"Adding new tcptls helper thread");
2601 ao2_t_ref(th, -1,
"Decrementing threadinfo ref from alloc, only table ref remains");
2612 .tcptls_session = tcptls_session,
2616 if (!tcptls_session) {
2622 if (!tcptls_session->
stream ||
2625 goto tcptls_write_setup_error;
2628 if (!(th =
ao2_t_find(threadt, &tmp,
OBJ_POINTER,
"ao2_find, getting sip_threadinfo in tcp helper thread"))) {
2630 goto tcptls_write_setup_error;
2641 if (write(th->
alert_pipe[1], &alert,
sizeof(alert)) == -1) {
2643 ao2_t_ref(packet, -1,
"could not write to alert pipe, remove packet");
2652 ao2_t_ref(th, -1,
"In sip_tcptls_write, unref threadinfo object after finding it");
2655 tcptls_write_setup_error:
2657 ao2_t_ref(th, -1,
"In sip_tcptls_write, unref threadinfo obj, could not create packet");
2660 ao2_t_ref(packet, -1,
"could not allocate packet's data");
2690 uint64_t payload_len;
2701 char data[payload_len + 1];
2707 strncpy(data, payload, payload_len);
2708 data[payload_len] =
'\0';
2742 if(time(&now) == -1) {
2789 char *content_length_str;
2790 int content_length = -1;
2808 if ((content_length_str =
strcasestr(msg,
"\nContent-Length:"))) {
2809 content_length_str +=
sizeof(
"\nContent-Length:") - 1;
2810 }
else if ((content_length_str =
strcasestr(msg,
"\nl:"))) {
2811 content_length_str +=
sizeof(
"\nl:") - 1;
2822 if (!strchr(content_length_str,
'\n')) {
2826 if (sscanf(content_length_str,
"%30d", &content_length) != 1) {
2827 content_length = -1;
2832 return content_length;
2860 body = strstr(message,
"\r\n\r\n");
2867 body +=
sizeof(
"\r\n\r\n") - 1;
2868 body_len = message_len - (body - message);
2874 if (content_length < 0) {
2876 }
else if (content_length == 0) {
2881 if (body_len == 0) {
2892 if (body_len < content_length) {
2895 }
else if (body_len > content_length) {
2917 int authenticated, time_t start)
2928 if (!tcptls_session->
client && !authenticated) {
2934 ast_debug(2,
"SIP TCP/TLS server timed out\n");
2942 ast_debug(2,
"SIP TCP/TLS server :: ast_wait_for_input returned %d\n", res);
2944 }
else if (res == 0) {
2945 ast_debug(2,
"SIP TCP/TLS server timed out\n");
2954 ast_debug(2,
"SIP TCP/TLS server error when receiving data\n");
2956 }
else if (res == 0) {
2957 ast_debug(2,
"SIP TCP/TLS server has shut down\n");
2960 readbuf[res] =
'\0';
2969 ast_log(
LOG_WARNING,
"Rejecting TCP/TLS packet from '%s' because way too large: %zu\n",
2985 int res,
timeout = -1, authenticated = 0, flags;
2987 struct sip_request req = { 0, } , reqcpy = { 0, };
2989 char buf[1024] =
"";
2990 struct pollfd fds[2] = { { 0 }, { 0 }, };
3004 if (!tcptls_session->
client) {
3015 ao2_t_ref(me, +1,
"Adding threadinfo ref for tcp_helper_thread");
3018 .tcptls_session = tcptls_session,
3021 if ((!(ca = tcptls_session->
parent)) ||
3022 (!(me =
ao2_t_find(threadt, &tmp,
OBJ_POINTER,
"ao2_find, getting sip_threadinfo in tcp helper thread")))) {
3044 fds[0].events = fds[1].events = POLLIN | POLLPRI;
3053 if(time(&start) == -1) {
3077 if (!tcptls_session->
client && !authenticated ) {
3095 }
else if (res == 0) {
3110 str_save = req.
data;
3111 memset(&req, 0,
sizeof(req));
3112 req.
data = str_save;
3115 str_save = reqcpy.data;
3116 memset(&reqcpy, 0,
sizeof(reqcpy));
3117 reqcpy.data = str_save;
3120 memset(buf, 0,
sizeof(buf));
3139 if (fds[1].revents) {
3145 if (read(me->
alert_pipe[0], &alert,
sizeof(alert)) == -1) {
3156 ast_log(
LOG_WARNING,
"TCPTLS thread alert_pipe indicated packet should be sent, but frame_q is empty\n");
3164 ao2_t_ref(packet, -1,
"tcptls packet sent, this is no longer needed");
3179 if (tcptls_session && !tcptls_session->
client && !authenticated) {
3184 ao2_t_unlink(threadt, me,
"Removing tcptls helper thread, thread is closing");
3185 ao2_t_ref(me, -1,
"Removing tcp_helper_threads threadinfo ref");
3192 ao2_t_ref(ca, -1,
"closing tcptls thread, getting rid of client tcptls_session arguments");
3195 if (tcptls_session) {
3202 tcptls_session =
NULL;
3213 if (peer->
expire != -1) {
3312 while ((cp = dialog->
packets)) {
3316 ao2_t_ref(cp, -1,
"Stop scheduled packet retransmission"));
3317 ao2_t_ref(cp, -1,
"Packet retransmission list");
3334 dialog_unref(dialog,
"Stop scheduled request_queue_sched_id"));
3337 dialog_unref(dialog,
"Stop scheduled provisional keepalive"));
3351 struct sip_pvt *dialog = (
void *) data;
3354 dialog_unref(dialog,
"Stop scheduled items for unlink action");
3369 dialog_ref(dialog,
"Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
3371 ao2_t_unlink(dialogs, dialog,
"unlinking dialog via ao2_unlink");
3372 ao2_t_unlink(dialogs_needdestroy, dialog,
"unlinking dialog_needdestroy via ao2_unlink");
3373 ao2_t_unlink(dialogs_rtpcheck, dialog,
"unlinking dialog_rtpcheck via ao2_unlink");
3404 dialog_ref(dialog,
"Stop scheduled items for unlink action");
3410 dialog_unref(dialog,
"Failed to schedule stop scheduled items for unlink action");
3414 dialog_unref(dialog,
"Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
3418 __attribute__((
format(printf, 2, 3)));
3424 return map_x_s(referstatusstrings, rstatus,
"");
3432 append_history(pvt,
"NeedDestroy",
"Setting needdestroy because %s", reason);
3435 ao2_t_link(dialogs_needdestroy, pvt,
"link pvt into dialogs_needdestroy container");
3445 ast_debug(1,
"Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->
callid);
3460 ast_debug(3,
"Setting SIP_ALREADYGONE on dialog %s\n", dialog->
callid);
3491 char *mutable_proxy, *sep, *
name;
3505 sep = strchr(mutable_proxy,
',');
3520 dest->
name[0] =
'\0';
3538 int port = standard;
3539 if (
ast_strlen_zero(pt) || (sscanf(pt,
"%30d", &port) != 1) || (port < 1) || (port > 65535)) {
3551 ast_debug(1,
"OBPROXY: Applying dialplan set OBproxy to this call\n");
3558 ast_debug(1,
"OBPROXY: Applying peer OBproxy to this call\n");
3565 ast_debug(1,
"OBPROXY: Applying global OBproxy to this call\n");
3571 ast_debug(1,
"OBPROXY: Not applying OBproxy to this call\n");
3585 int l_name = name ? strlen(name) : 0;
3587 return (l_name >= len && name && name[len] < 33 &&
3664 if (!strcasecmp(transport,
"udp")) {
3667 if (!strcasecmp(transport,
"tcp")) {
3670 if (!strcasecmp(transport,
"tls")) {
3673 if (!strcasecmp(transport,
"ws")) {
3676 if (!strcasecmp(transport,
"wss")) {
3716 buf[strlen(buf) - 1] = 0;
3823 ast_debug(2,
"Socket type is TCP but no tcptls_session is present to write to\n");
3851 snprintf(p->
via,
sizeof(p->
via),
"SIP/2.0/%s %s;branch=z9hG4bK%08x%s",
3854 (
unsigned)p->
branch, rport);
3890 "but we're using IPv6, which doesn't need it. Please " 3891 "remove \"localnet\" and/or \"externaddr\" settings.\n");
3894 want_remap = localaddr &&
3942 ast_debug(1,
"Target address %s is not local, substituting externaddr\n",
3990 vsnprintf(buf,
sizeof(buf), fmt, ap);
3992 l = strlen(buf) + 1;
3993 if (!(hist =
ast_calloc(1,
sizeof(*hist) + l))) {
3996 if (!p->history && !(p->history =
ast_calloc(1,
sizeof(*p->history)))) {
4000 memcpy(hist->
event, buf, l);
4004 p->history_entries--;
4008 p->history_entries++;
4064 ast_debug(4,
"SIP TIMER: Not rescheduling id #%d:%s (Method %d) (No timer T1)\n",
4073 ast_debug(4,
"SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n",
4099 reschedule = siptimer_a;
4100 ast_debug(4,
"** SIP timers: Rescheduling retransmission %d to %d ms (t1 %d ms (Retrans id #%d)) \n",
4110 ast_verbose(
"Retransmitting #%d (%s) to %s:\n%s\n---\n",
4125 if (reschedule >= diff) {
4141 ast_log(
LOG_WARNING,
"Retransmission timeout reached on transmission %s for seqno %u (%s %s) -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions\n" 4142 "Packet timed out after %dms with no response\n",
4145 pkt->
is_fatal ?
"Critical" :
"Non-critical",
4146 pkt->
is_resp ?
"Response" :
"Request",
4150 ast_log(
LOG_WARNING,
"Cancelling retransmit of OPTIONs (call id %s) -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions\n", pkt->
owner->
callid);
4165 ast_log(
LOG_WARNING,
"Hanging up call %s - no reply to our critical packet (see https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions).\n", pkt->
owner->
callid);
4214 append_history(pkt->
owner,
"ByeFailure",
"Remote peer doesn't respond to bye. Destroying call anyway.");
4223 ao2_t_ref(pkt, -1,
"Packet retransmission list (retransmission complete)");
4234 ao2_t_ref(pkt, -1,
"Scheduled packet retransmission complete");
4241 struct sip_pkt *pkt = (
void *) data;
4244 ao2_t_ref(pkt, -1,
"Stop scheduled packet retransmission"));
4245 ao2_t_ref(pkt, -1,
"Stop packet retransmission action");
4251 ao2_t_ref(pkt, +1,
"Stop packet retransmission action");
4254 ao2_t_ref(pkt, -1,
"Failed to schedule stop packet retransmission action");
4260 struct sip_pkt *pkt = (
void *) vdoomed;
4292 ao2_t_ref(pkt, -1,
"Failed to initialize");
4301 pkt->
owner =
dialog_ref(p,
"__sip_reliable_xmit: setting pkt->owner");
4329 ao2_t_ref(pkt, +1,
"Schedule packet retransmission");
4332 ao2_t_ref(pkt, -1,
"Failed to schedule packet retransmission");
4336 ast_debug(4,
"*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->
retransid);
4343 ast_log(
LOG_ERROR,
"Serious Network Trouble; __sip_xmit returns error for pkt data\n");
4348 ao2_t_ref(pkt, -1,
"Packet retransmission list");