232 #include <inttypes.h> 277 #include "sip/include/dialplan_functions.h" 684 .resync_threshold = 1000,
720 {
NONE,
"-",
"unknown",
"unknown" },
722 {
DIALOG_INFO_XML,
"dialog",
"application/dialog-info+xml",
"dialog-info+xml" },
723 {
CPIM_PIDF_XML,
"presence",
"application/cpim-pidf+xml",
"cpim-pidf+xml" },
724 {
PIDF_XML,
"presence",
"application/pidf+xml",
"pidf+xml" },
725 {
XPIDF_XML,
"presence",
"application/xpidf+xml",
"xpidf+xml" },
726 {
MWI_NOTIFICATION,
"message-summary",
"application/simple-message-summary",
"mwi" }
813 #define SIP_PEDANTIC_DECODE(str) \ 814 if (sip_cfg.pedanticsipchecking && !ast_strlen_zero(str)) { \ 815 ast_uri_decode(str, ast_uri_sip_user); \ 948 static const struct {
958 static const struct {
1044 #define sip_pvt_lock(x) ao2_lock(x) 1045 #define sip_pvt_trylock(x) ao2_trylock(x) 1046 #define sip_pvt_unlock(x) ao2_unlock(x) 1058 #define BOGUS_PEER_MD5SECRET "intentionally_invalid_md5_string" 1076 #define SIP_TRANSPORT_STR_BUFSIZE 128 1157 #define UNLINK(element, head, prev) do { \ 1159 (prev)->next = (element)->next; \ 1161 (head) = (element)->next; \ 1239 const char *
file,
int line,
const char *func);
1240 #define find_call(req, addr, intended_method) \ 1241 __find_call(req, addr, intended_method, __FILE__, __LINE__, __PRETTY_FUNCTION__) 1279 int debug,
int *min_packet_size,
int *max_packet_size);
1291 const char *secret,
const char *md5secret,
int sipmethod,
1337 #define peer_in_destruction(peer) (ao2_ref(peer, 0) == 0) 1389 const char *
name,
int flag,
unsigned int transport);
1448 static const char *
find_alias(
const char *
name,
const char *_default);
1551 static int parse_minse(
const char *p_hdrval,
int *
const p_interval);
1575 .description =
"Session Initiation Protocol (SIP)",
1673 .name =
"call-completion",
1701 if (!(epa_entry =
ao2_t_alloc(
sizeof(*epa_entry), static_data->
destructor,
"Allocate new EPA entry"))) {
1801 expires_ms = expires * 1000;
1843 const char *uri = arg;
1858 const char *uri = arg;
1873 struct sip_pvt *call_pvt = arg;
2025 return monitor_instance->
core_id;
2049 ao2_t_ref(monitor_instance->
suspension_entry, -1,
"Decrementing suspension entry refcount in sip_monitor_instance_destructor");
2058 if (!monitor_instance) {
2063 ao2_ref(monitor_instance, -1);
2071 ao2_link(sip_monitor_instances, monitor_instance);
2072 return monitor_instance;
2108 if (!monitor_instance) {
2129 ao2_t_ref(monitor, +1,
"Adding a ref to the monitor for the scheduler");
2144 ast_str_append(&body, 0,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
2151 ast_str_append(&body, 0,
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"%s\">\n", presentity);
2153 ast_str_append(&body, 0,
"<status><basic>%s</basic></status>\n", state ==
CC_OPEN ?
"open" :
"closed");
2166 if (!monitor_instance) {
2174 ao2_ref(monitor_instance, -1);
2177 if (!(cc_entry =
ast_calloc(1,
sizeof(*cc_entry)))) {
2178 ast_log(
LOG_WARNING,
"Unable to allocate space for instance data of EPA entry for call-completion\n");
2179 ao2_ref(monitor_instance, -1);
2216 if (!monitor_instance) {
2237 if (*sched_id != -1) {
2239 ao2_t_ref(monitor, -1,
"Removing scheduler's reference to the monitor");
2247 ao2_unlink(sip_monitor_instances, monitor_instance);
2257 static const char cc_purpose[] =
"purpose=call-completion";
2258 static const int cc_purpose_len =
sizeof(cc_purpose) - 1;
2265 uri =
strsep(&call_info,
";");
2267 while ((purpose =
strsep(&call_info,
";"))) {
2268 if (!strncmp(purpose, cc_purpose, cc_purpose_len)) {
2278 while ((service_str =
strsep(&call_info,
";"))) {
2279 if (!strncmp(service_str,
"m=", 2)) {
2292 strsep(&service_str,
"=");
2363 ao2_ref(monitor_instance, -1);
2388 .name =
"SIP TCP server",
2399 .name =
"SIP TLS server",
2406 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args) 2415 for (cur = table; cur->
s; cur++) {
2430 for (cur = table; cur->
s; cur++) {
2431 if (!strcasecmp(cur->
s, s)) {
2461 && !strchr(
"-.!%*_+`'~", *str)) {
2481 code = reason->
code;
2498 #define check_request_transport(peer, tmpl) ({ \ 2500 if (peer->socket.type == tmpl->socket.type) \ 2502 else if (!(peer->transports & tmpl->socket.type)) {\ 2503 ast_log(LOG_ERROR, \ 2504 "'%s' is not a valid transport for '%s'. we only use '%s'! ending call.\n", \ 2505 sip_get_transport(tmpl->socket.type), peer->name, get_transport_list(peer->transports) \ 2508 } else if (peer->socket.type & AST_TRANSPORT_TLS) { \ 2509 ast_log(LOG_WARNING, \ 2510 "peer '%s' HAS NOT USED (OR SWITCHED TO) TLS in favor of '%s' (but this was allowed in sip.conf)!\n", \ 2511 peer->name, sip_get_transport(tmpl->socket.type) \ 2515 "peer '%s' has contacted us over %s even though we prefer %s.\n", \ 2516 peer->name, sip_get_transport(tmpl->socket.type), sip_get_transport(peer->socket.type) \ 2529 for (v = src ; v ; v = v->
next) {
2575 ao2_t_ref(packet, -1,
"thread destruction, removing packet from frame queue");
2595 ao2_t_ref(th, -1,
"Failed to open alert pipe on sip_threadinfo");
2596 ast_log(
LOG_ERROR,
"Could not create sip alert pipe in tcptls thread, error %s\n", strerror(
errno));
2599 ao2_t_ref(tcptls_session, +1,
"tcptls_session ref for sip_threadinfo object");
2602 ao2_t_link(threadt, th,
"Adding new tcptls helper thread");
2603 ao2_t_ref(th, -1,
"Decrementing threadinfo ref from alloc, only table ref remains");
2614 .tcptls_session = tcptls_session,
2618 if (!tcptls_session) {
2624 if (!tcptls_session->
stream ||
2627 goto tcptls_write_setup_error;
2630 if (!(th =
ao2_t_find(threadt, &tmp,
OBJ_POINTER,
"ao2_find, getting sip_threadinfo in tcp helper thread"))) {
2632 goto tcptls_write_setup_error;
2643 if (write(th->
alert_pipe[1], &alert,
sizeof(alert)) == -1) {
2645 ao2_t_ref(packet, -1,
"could not write to alert pipe, remove packet");
2654 ao2_t_ref(th, -1,
"In sip_tcptls_write, unref threadinfo object after finding it");
2657 tcptls_write_setup_error:
2659 ao2_t_ref(th, -1,
"In sip_tcptls_write, unref threadinfo obj, could not create packet");
2662 ao2_t_ref(packet, -1,
"could not allocate packet's data");
2692 uint64_t payload_len;
2703 char data[payload_len + 1];
2709 strncpy(data, payload, payload_len);
2710 data[payload_len] =
'\0';
2744 if(time(&now) == -1) {
2791 char *content_length_str;
2792 int content_length = -1;
2810 if ((content_length_str =
strcasestr(msg,
"\nContent-Length:"))) {
2811 content_length_str +=
sizeof(
"\nContent-Length:") - 1;
2812 }
else if ((content_length_str =
strcasestr(msg,
"\nl:"))) {
2813 content_length_str +=
sizeof(
"\nl:") - 1;
2824 if (!strchr(content_length_str,
'\n')) {
2828 if (sscanf(content_length_str,
"%30d", &content_length) != 1) {
2829 content_length = -1;
2834 return content_length;
2862 body = strstr(message,
"\r\n\r\n");
2869 body +=
sizeof(
"\r\n\r\n") - 1;
2870 body_len = message_len - (body - message);
2876 if (content_length < 0) {
2878 }
else if (content_length == 0) {
2883 if (body_len == 0) {
2894 if (body_len < content_length) {
2897 }
else if (body_len > content_length) {
2919 int authenticated, time_t start)
2930 if (!tcptls_session->
client && !authenticated) {
2936 ast_debug(2,
"SIP TCP/TLS server timed out\n");
2944 ast_debug(2,
"SIP TCP/TLS server :: ast_wait_for_input returned %d\n", res);
2946 }
else if (res == 0) {
2947 ast_debug(2,
"SIP TCP/TLS server timed out\n");
2956 ast_debug(2,
"SIP TCP/TLS server error when receiving data\n");
2958 }
else if (res == 0) {
2959 ast_debug(2,
"SIP TCP/TLS server has shut down\n");
2962 readbuf[res] =
'\0';
2971 ast_log(
LOG_WARNING,
"Rejecting TCP/TLS packet from '%s' because way too large: %zu\n",
2987 int res,
timeout = -1, authenticated = 0, flags;
2989 struct sip_request req = { 0, } , reqcpy = { 0, };
2991 char buf[1024] =
"";
2992 struct pollfd fds[2] = { { 0 }, { 0 }, };
3006 if (!tcptls_session->
client) {
3017 ao2_t_ref(me, +1,
"Adding threadinfo ref for tcp_helper_thread");
3020 .tcptls_session = tcptls_session,
3023 if ((!(ca = tcptls_session->
parent)) ||
3024 (!(me =
ao2_t_find(threadt, &tmp,
OBJ_POINTER,
"ao2_find, getting sip_threadinfo in tcp helper thread")))) {
3046 fds[0].events = fds[1].events = POLLIN | POLLPRI;
3055 if(time(&start) == -1) {
3079 if (!tcptls_session->
client && !authenticated ) {
3097 }
else if (res == 0) {
3112 str_save = req.
data;
3113 memset(&req, 0,
sizeof(req));
3114 req.
data = str_save;
3117 str_save = reqcpy.data;
3118 memset(&reqcpy, 0,
sizeof(reqcpy));
3119 reqcpy.data = str_save;
3122 memset(buf, 0,
sizeof(buf));
3141 if (fds[1].revents) {
3147 if (read(me->
alert_pipe[0], &alert,
sizeof(alert)) == -1) {
3158 ast_log(
LOG_WARNING,
"TCPTLS thread alert_pipe indicated packet should be sent, but frame_q is empty\n");
3166 ao2_t_ref(packet, -1,
"tcptls packet sent, this is no longer needed");
3181 if (tcptls_session && !tcptls_session->
client && !authenticated) {
3186 ao2_t_unlink(threadt, me,
"Removing tcptls helper thread, thread is closing");
3187 ao2_t_ref(me, -1,
"Removing tcp_helper_threads threadinfo ref");
3194 ao2_t_ref(ca, -1,
"closing tcptls thread, getting rid of client tcptls_session arguments");
3197 if (tcptls_session) {
3204 tcptls_session =
NULL;
3215 if (peer->
expire != -1) {
3314 while ((cp = dialog->
packets)) {
3318 ao2_t_ref(cp, -1,
"Stop scheduled packet retransmission"));
3319 ao2_t_ref(cp, -1,
"Packet retransmission list");
3336 dialog_unref(dialog,
"Stop scheduled request_queue_sched_id"));
3339 dialog_unref(dialog,
"Stop scheduled provisional keepalive"));
3353 struct sip_pvt *dialog = (
void *) data;
3356 dialog_unref(dialog,
"Stop scheduled items for unlink action");
3371 dialog_ref(dialog,
"Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
3373 ao2_t_unlink(dialogs, dialog,
"unlinking dialog via ao2_unlink");
3374 ao2_t_unlink(dialogs_needdestroy, dialog,
"unlinking dialog_needdestroy via ao2_unlink");
3375 ao2_t_unlink(dialogs_rtpcheck, dialog,
"unlinking dialog_rtpcheck via ao2_unlink");
3406 dialog_ref(dialog,
"Stop scheduled items for unlink action");
3412 dialog_unref(dialog,
"Failed to schedule stop scheduled items for unlink action");
3416 dialog_unref(dialog,
"Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
3420 __attribute__((
format(printf, 2, 3)));
3426 return map_x_s(referstatusstrings, rstatus,
"");
3434 append_history(pvt,
"NeedDestroy",
"Setting needdestroy because %s", reason);
3437 ao2_t_link(dialogs_needdestroy, pvt,
"link pvt into dialogs_needdestroy container");
3447 ast_debug(1,
"Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->
callid);
3462 ast_debug(3,
"Setting SIP_ALREADYGONE on dialog %s\n", dialog->
callid);
3493 char *mutable_proxy, *sep, *
name;
3507 sep = strchr(mutable_proxy,
',');
3522 dest->
name[0] =
'\0';
3540 int port = standard;
3541 if (
ast_strlen_zero(pt) || (sscanf(pt,
"%30d", &port) != 1) || (port < 1) || (port > 65535)) {
3553 ast_debug(1,
"OBPROXY: Applying dialplan set OBproxy to this call\n");
3560 ast_debug(1,
"OBPROXY: Applying peer OBproxy to this call\n");
3567 ast_debug(1,
"OBPROXY: Applying global OBproxy to this call\n");
3573 ast_debug(1,
"OBPROXY: Not applying OBproxy to this call\n");
3587 int l_name = name ? strlen(name) : 0;
3589 return (l_name >= len && name && name[len] < 33 &&
3666 if (!strcasecmp(transport,
"udp")) {
3669 if (!strcasecmp(transport,
"tcp")) {
3672 if (!strcasecmp(transport,
"tls")) {
3675 if (!strcasecmp(transport,
"ws")) {
3678 if (!strcasecmp(transport,
"wss")) {
3718 buf[strlen(buf) - 1] = 0;
3825 ast_debug(2,
"Socket type is TCP but no tcptls_session is present to write to\n");
3853 snprintf(p->
via,
sizeof(p->
via),
"SIP/2.0/%s %s;branch=z9hG4bK%08x%s",
3856 (
unsigned)p->
branch, rport);
3892 "but we're using IPv6, which doesn't need it. Please " 3893 "remove \"localnet\" and/or \"externaddr\" settings.\n");
3896 want_remap = localaddr &&
3944 ast_debug(1,
"Target address %s is not local, substituting externaddr\n",
3992 vsnprintf(buf,
sizeof(buf), fmt, ap);
3994 l = strlen(buf) + 1;
3995 if (!(hist =
ast_calloc(1,
sizeof(*hist) + l))) {
3998 if (!p->history && !(p->history =
ast_calloc(1,
sizeof(*p->history)))) {
4002 memcpy(hist->
event, buf, l);
4006 p->history_entries--;
4010 p->history_entries++;
4066 ast_debug(4,
"SIP TIMER: Not rescheduling id #%d:%s (Method %d) (No timer T1)\n",
4075 ast_debug(4,
"SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n",
4101 reschedule = siptimer_a;
4102 ast_debug(4,
"** SIP timers: Rescheduling retransmission %d to %d ms (t1 %d ms (Retrans id #%d)) \n",
4112 ast_verbose(
"Retransmitting #%d (%s) to %s:\n%s\n---\n",
4127 if (reschedule >= diff) {
4143 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" 4144 "Packet timed out after %dms with no response\n",
4147 pkt->
is_fatal ?
"Critical" :
"Non-critical",
4148 pkt->
is_resp ?
"Response" :
"Request",
4152 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);
4167 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);
4216 append_history(pkt->
owner,
"ByeFailure",
"Remote peer doesn't respond to bye. Destroying call anyway.");
4225 ao2_t_ref(pkt, -1,
"Packet retransmission list (retransmission complete)");
4236 ao2_t_ref(pkt, -1,
"Scheduled packet retransmission complete");
4243 struct sip_pkt *pkt = (
void *) data;
4246 ao2_t_ref(pkt, -1,
"Stop scheduled packet retransmission"));
4247 ao2_t_ref(pkt, -1,
"Stop packet retransmission action");
4253 ao2_t_ref(pkt, +1,
"Stop packet retransmission action");
4256 ao2_t_ref(pkt, -1,
"Failed to schedule stop packet retransmission action");
4262 struct sip_pkt *pkt = (
void *) vdoomed;
4294 ao2_t_ref(pkt, -1,
"Failed to initialize");
4303 pkt->
owner =
dialog_ref(p,
"__sip_reliable_xmit: setting pkt->owner");
4331 ao2_t_ref(pkt, +1,
"Schedule packet retransmission");
4334 ao2_t_ref(pkt, -1,
"Failed to schedule packet retransmission");
4338 ast_debug(4,
"*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->
retransid);
4345 ast_log(
LOG_ERROR,
"Serious Network Trouble; __sip_xmit returns error for pkt data\n");
4350 ao2_t_ref(pkt, -1,
"Packet retransmission list");
4383 ast_debug(3,
"Re-scheduled destruction of SIP subscription %s\n", p->
callid ? p->
callid :
"<unknown>");
4390 char method_str[31];
4394 if (sscanf(p->
lastmsg,
"Tx: %30s", method_str) == 1 || sscanf(p->
lastmsg,
"Rx: %30s", method_str) == 1) {
4412 "Autodestruct on dialog '%s' with owner %s in place (Method: %s). Rescheduling destruction for 10000 ms\n",