2 * @file sip-transport.c
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This module incapsulates SIP (RFC3261) protocol and provides
25 * higher level API (a layer) to XML-based SIPE (SIP with Extensions).
26 * Underlying leyer for this is TCP/SSL layer.
28 * A diagram in pseudographics:
30 * === SIPE (XML-based) layer ======================
31 * === SIP RFC3261 transport layer (This module) ===
32 * === TCP/SSL layer ===============================
34 * Authentication (Kerberos and NTLM) is applicable to this layer only.
35 * The same with message integtity (signing). No sip-sec* code should
36 * be used ourside of this module.
38 * SIP errors as codes(both as a return codes and network conditions) should be
39 * escalated to higher leyer (SIPE). Network conditions include no response
40 * within timeout interval.
42 * This module should support redirect internally. No escalations to higher
45 * NO SIP-messages (headers) composing and processing should be outside of
46 * this module (!) Like headers: Via, Route, Contact, Authorization, etc.
47 * It's all irrelevant to higher layer responsibilities.
61 #include "sipe-common.h"
64 #include "sip-transport.h"
65 #include "sipe-backend.h"
66 #include "sipe-core.h"
67 #include "sipe-core-private.h"
68 #include "sipe-certificate.h"
69 #include "sipe-dialog.h"
70 #include "sipe-incoming.h"
72 #include "sipe-notify.h"
73 #include "sipe-schedule.h"
74 #include "sipe-sign.h"
75 #include "sipe-subscriptions.h"
76 #include "sipe-utils.h"
80 struct sip_sec_context
*gssapi_context
;
83 const gchar
*protocol
;
93 /* sip-transport.c private data */
94 struct sip_transport
{
95 struct sipe_transport_connection
*connection
;
99 gchar
*server_version
;
103 GSList
*transactions
;
105 struct sip_auth registrar
;
106 struct sip_auth proxy
;
109 guint register_attempt
;
111 gboolean processing_input
; /* whether full header received */
112 gboolean auth_incomplete
; /* whether authentication not completed */
113 gboolean reregister_set
; /* whether reregister timer set */
114 gboolean reauthenticate_set
; /* whether reauthenticate timer set */
115 gboolean subscribed
; /* whether subscribed to events, except buddies presence */
116 gboolean deregister
; /* whether in deregistration */
119 /* Keep in sync with sipe_transport_type! */
120 static const char *transport_descriptor
[] = { "", "tls", "tcp"};
121 #define TRANSPORT_DESCRIPTOR (transport_descriptor[transport->connection->type])
123 static char *genbranch()
125 return g_strdup_printf("z9hG4bK%04X%04X%04X%04X%04X",
126 rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF,
127 rand() & 0xFFFF, rand() & 0xFFFF);
130 static void sipe_auth_free(struct sip_auth
*auth
)
132 g_free(auth
->opaque
);
134 auth
->protocol
= NULL
;
137 g_free(auth
->sts_uri
);
138 auth
->sts_uri
= NULL
;
139 g_free(auth
->target
);
142 auth
->type
= SIPE_AUTHENTICATION_TYPE_UNSET
;
145 g_free(auth
->gssapi_data
);
146 auth
->gssapi_data
= NULL
;
147 sip_sec_destroy_context(auth
->gssapi_context
);
148 auth
->gssapi_context
= NULL
;
151 static void sipe_make_signature(struct sipe_core_private
*sipe_private
,
154 struct sip_transport
*transport
= sipe_private
->transport
;
155 if (sip_sec_context_is_ready(transport
->registrar
.gssapi_context
)) {
156 struct sipmsg_breakdown msgbd
;
157 gchar
*signature_input_str
;
159 sipmsg_breakdown_parse(&msgbd
, transport
->registrar
.realm
, transport
->registrar
.target
,
160 transport
->registrar
.protocol
);
161 msgbd
.rand
= g_strdup_printf("%08x", g_random_int());
162 transport
->registrar
.ntlm_num
++;
163 msgbd
.num
= g_strdup_printf("%d", transport
->registrar
.ntlm_num
);
164 signature_input_str
= sipmsg_breakdown_get_string(transport
->registrar
.version
, &msgbd
);
165 if (signature_input_str
!= NULL
) {
166 char *signature_hex
= sip_sec_make_signature(transport
->registrar
.gssapi_context
, signature_input_str
);
167 msg
->signature
= signature_hex
;
168 msg
->rand
= g_strdup(msgbd
.rand
);
169 msg
->num
= g_strdup(msgbd
.num
);
170 g_free(signature_input_str
);
172 sipmsg_breakdown_free(&msgbd
);
176 static const gchar
*const auth_type_to_protocol
[] = {
177 NULL
, /* SIPE_AUTHENTICATION_TYPE_UNSET */
178 "NTLM", /* SIPE_AUTHENTICATION_TYPE_NTLM */
179 "Kerberos", /* SIPE_AUTHENTICATION_TYPE_KERBEROS */
180 NULL
, /* SIPE_AUTHENTICATION_TYPE_NEGOTIATE */
181 "TLS-DSK", /* SIPE_AUTHENTICATION_TYPE_TLS_DSK */
183 #define AUTH_PROTOCOLS (sizeof(auth_type_to_protocol)/sizeof(gchar *))
185 static gchar
*msg_signature_to_auth(struct sip_auth
*auth
,
188 return(g_strdup_printf("%s qop=\"auth\", opaque=\"%s\", realm=\"%s\", targetname=\"%s\", crand=\"%s\", cnum=\"%s\", response=\"%s\"",
190 auth
->opaque
, auth
->realm
, auth
->target
,
191 msg
->rand
, msg
->num
, msg
->signature
));
194 static gchar
*initialize_auth_context(struct sipe_core_private
*sipe_private
,
195 struct sip_auth
*auth
,
198 struct sip_transport
*transport
= sipe_private
->transport
;
200 gchar
*gssapi_data
= NULL
;
207 * If transport is de-registering when we reach this point then we
208 * are in the middle of the previous authentication context setup
209 * attempt. So we shouldn't try another attempt.
211 if (transport
->deregister
)
214 /* Create security context or handshake continuation? */
215 if (auth
->gssapi_context
) {
216 /* Perform next step in authentication handshake */
217 gboolean status
= sip_sec_init_context_step(auth
->gssapi_context
,
223 /* If authentication is completed gssapi_data can be NULL */
225 (sip_sec_context_is_ready(auth
->gssapi_context
) || gssapi_data
))) {
226 SIPE_DEBUG_ERROR_NOFORMAT("initialize_auth_context: security context continuation failed");
228 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
229 SIPE_CONNECTION_ERROR_AUTHENTICATION_FAILED
,
230 _("Failed to authenticate to server"));
235 /* Create security context */
236 gpointer password
= sipe_private
->password
;
238 /* For TLS-DSK the "password" is a certificate */
239 if (auth
->type
== SIPE_AUTHENTICATION_TYPE_TLS_DSK
) {
240 password
= sipe_certificate_tls_dsk_find(sipe_private
,
245 SIPE_DEBUG_INFO("initialize_auth_context: TLS-DSK Certificate Provisioning URI %s",
247 if (!sipe_certificate_tls_dsk_generate(sipe_private
,
250 gchar
*tmp
= g_strdup_printf(_("Can't request certificate from %s"),
252 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
253 SIPE_CONNECTION_ERROR_AUTHENTICATION_FAILED
,
258 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
259 SIPE_CONNECTION_ERROR_AUTHENTICATION_FAILED
,
260 _("No URI for certificate provisioning service provided"));
263 /* we can't authenticate the message yet */
264 transport
->auth_incomplete
= TRUE
;
268 SIPE_DEBUG_INFO("initialize_auth_context: TLS-DSK certificate for target '%s' found.",
273 auth
->gssapi_context
= sip_sec_create_context(auth
->type
,
274 SIPE_CORE_PRIVATE_FLAG_IS(SSO
),
275 FALSE
, /* connection-less for SIP */
276 sipe_private
->authdomain
? sipe_private
->authdomain
: "",
277 sipe_private
->authuser
,
280 if (auth
->gssapi_context
) {
281 sip_sec_init_context_step(auth
->gssapi_context
,
288 if (!gssapi_data
|| !auth
->gssapi_context
) {
290 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
291 SIPE_CONNECTION_ERROR_AUTHENTICATION_FAILED
,
292 _("Failed to authenticate to server"));
297 if ((auth
->version
> 3) &&
298 sip_sec_context_is_ready(auth
->gssapi_context
)) {
299 sipe_make_signature(sipe_private
, msg
);
300 sign_str
= g_strdup_printf(", crand=\"%s\", cnum=\"%s\", response=\"%s\"",
301 msg
->rand
, msg
->num
, msg
->signature
);
303 sign_str
= g_strdup("");
307 gssapi_str
= g_strdup_printf(", gssapi-data=\"%s\"",
311 gssapi_str
= g_strdup("");
314 opaque_str
= auth
->opaque
? g_strdup_printf(", opaque=\"%s\"", auth
->opaque
) : g_strdup("");
316 if (auth
->version
> 2) {
317 version_str
= g_strdup_printf(", version=%d", auth
->version
);
319 version_str
= g_strdup("");
322 ret
= g_strdup_printf("%s qop=\"auth\"%s, realm=\"%s\", targetname=\"%s\"%s%s%s",
323 auth
->protocol
, opaque_str
,
324 auth
->realm
, auth
->target
,
325 gssapi_str
, version_str
, sign_str
);
334 static gchar
*auth_header(struct sipe_core_private
*sipe_private
,
335 struct sip_auth
*auth
,
341 * If the message is already signed then we have an authentication
342 * context, i.e. the authentication handshake is complete. Generate
343 * authentication header from message signature.
345 if (msg
->signature
) {
346 ret
= msg_signature_to_auth(auth
, msg
);
349 * We should reach this point only when the authentication context
350 * needs to be initialized.
353 ret
= initialize_auth_context(sipe_private
, auth
, msg
);
359 static void fill_auth(const gchar
*hdr
, struct sip_auth
*auth
)
363 /* skip authentication identifier */
364 hdr
= strchr(hdr
, ' ');
366 SIPE_DEBUG_ERROR_NOFORMAT("fill_auth: corrupted authentication header");
372 /* start of next parameter value */
373 while ((param
= strchr(hdr
, '=')) != NULL
) {
376 /* parameter value type */
379 /* string: xyz="..."(,) */
380 end
= strchr(++param
, '"');
382 SIPE_DEBUG_ERROR("fill_auth: corrupted string parameter near '%s'", hdr
);
386 /* number: xyz=12345(,) */
387 end
= strchr(param
, ',');
390 end
= param
+ strlen(param
);
395 SIPE_DEBUG_INFO("fill_auth: hdr '%s'", hdr
);
396 SIPE_DEBUG_INFO("fill_auth: param '%s'", param
);
397 SIPE_DEBUG_INFO("fill_auth: end '%s'", end
);
401 if (g_str_has_prefix(hdr
, "gssapi-data=\"")) {
402 g_free(auth
->gssapi_data
);
403 auth
->gssapi_data
= g_strndup(param
, end
- param
);
404 } else if (g_str_has_prefix(hdr
, "opaque=\"")) {
405 g_free(auth
->opaque
);
406 auth
->opaque
= g_strndup(param
, end
- param
);
407 } else if (g_str_has_prefix(hdr
, "realm=\"")) {
409 auth
->realm
= g_strndup(param
, end
- param
);
410 } else if (g_str_has_prefix(hdr
, "sts-uri=\"")) {
411 /* Only used with SIPE_AUTHENTICATION_TYPE_TLS_DSK */
412 g_free(auth
->sts_uri
);
413 auth
->sts_uri
= g_strndup(param
, end
- param
);
414 } else if (g_str_has_prefix(hdr
, "targetname=\"")) {
415 g_free(auth
->target
);
416 auth
->target
= g_strndup(param
, end
- param
);
417 } else if (g_str_has_prefix(hdr
, "version=")) {
418 auth
->version
= atoi(param
);
421 /* skip to next parameter */
422 while ((*end
== '"') || (*end
== ',') || (*end
== ' '))
430 static void sign_outgoing_message(struct sipe_core_private
*sipe_private
,
433 struct sip_transport
*transport
= sipe_private
->transport
;
436 if (transport
->registrar
.type
== SIPE_AUTHENTICATION_TYPE_UNSET
) {
440 sipe_make_signature(sipe_private
, msg
);
442 buf
= auth_header(sipe_private
, &transport
->registrar
, msg
);
444 sipmsg_add_header_now_pos(msg
, "Authorization", buf
, 5);
449 static const gchar
*sip_transport_user_agent(struct sipe_core_private
*sipe_private
)
451 struct sip_transport
*transport
= sipe_private
->transport
;
453 if (!transport
->user_agent
) {
454 const gchar
*useragent
= sipe_backend_setting(SIPE_CORE_PUBLIC
,
455 SIPE_SETTING_USER_AGENT
);
456 if (is_empty(useragent
)) {
457 /*@TODO: better approach to define _user_ OS, it's version and host architecture */
459 #if defined(__linux__) || defined(__linux) || defined(__LINUX__)
460 #define SIPE_TARGET_PLATFORM "linux"
461 #elif defined(__NetBSD__) ||defined( __OpenBSD__) || defined(__FreeBSD__)
462 #define SIPE_TARGET_PLATFORM "bsd"
463 #elif defined(__APPLE__) || defined(__MACOS__)
464 #define SIPE_TARGET_PLATFORM "macosx"
465 #elif defined(_AIX) || defined(__AIX__) || defined(__aix__)
466 #define SIPE_TARGET_PLATFORM "aix"
467 #elif defined(__solaris__) || defined(__sun)
468 #define SIPE_TARGET_PLATFORM "sun"
469 #elif defined(_WIN32)
470 #define SIPE_TARGET_PLATFORM "win"
471 #elif defined(__CYGWIN__)
472 #define SIPE_TARGET_PLATFORM "cygwin"
473 #elif defined(__hpux__)
474 #define SIPE_TARGET_PLATFORM "hpux"
475 #elif defined(__sgi__)
476 #define SIPE_TARGET_PLATFORM "irix"
478 #define SIPE_TARGET_PLATFORM "unknown"
481 #if defined(__amd64__) || defined(__x86_64__) || defined(_M_AMD64)
482 #define SIPE_TARGET_ARCH "x86_64"
483 #elif defined(__386__) || defined(__i386__) || defined(__i386) || defined(_M_IX86) || defined(_M_I386)
484 #define SIPE_TARGET_ARCH "i386"
485 #elif defined(__ppc64__)
486 #define SIPE_TARGET_ARCH "ppc64"
487 #elif defined(__powerpc__) || defined(__powerpc) || defined(__ppc__) || defined(__PPC__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PWR)
488 #define SIPE_TARGET_ARCH "ppc"
489 #elif defined(__hppa__) || defined(__hppa)
490 #define SIPE_TARGET_ARCH "hppa"
491 #elif defined(__mips__) || defined(__mips) || defined(_MIPS_ARCH) || defined(_M_MRX000)
492 #define SIPE_TARGET_ARCH "mips"
493 #elif defined(__s390__) || defined(__s390) || defined(__s390x__) || defined(__s390x)
494 #define SIPE_TARGET_ARCH "s390"
495 #elif defined(__sparc__) || defined(__sparc) || defined(__sparcv8)
496 #define SIPE_TARGET_ARCH "sparc"
497 #elif defined(__arm__)
498 #define SIPE_TARGET_ARCH "arm"
500 #define SIPE_TARGET_ARCH "other"
502 gchar
*backend
= sipe_backend_version();
503 transport
->user_agent
= g_strdup_printf("%s Sipe/" PACKAGE_VERSION
" (" SIPE_TARGET_PLATFORM
"-" SIPE_TARGET_ARCH
"; %s)",
505 transport
->server_version
? transport
->server_version
: "");
508 transport
->user_agent
= g_strdup(useragent
);
511 return(transport
->user_agent
);
514 void sip_transport_response(struct sipe_core_private
*sipe_private
,
522 GString
*outstr
= g_string_new("");
525 const gchar
*keepers
[] = { "To", "From", "Call-ID", "CSeq", "Via", "Record-Route", NULL
};
527 /* Can return NULL! */
528 contact
= get_contact(sipe_private
);
530 sipmsg_add_header(msg
, "Contact", contact
);
535 gchar
*len
= g_strdup_printf("%" G_GSIZE_FORMAT
, (gsize
) strlen(body
));
536 sipmsg_add_header(msg
, "Content-Length", len
);
539 sipmsg_add_header(msg
, "Content-Length", "0");
542 sipmsg_add_header(msg
, "User-Agent", sip_transport_user_agent(sipe_private
));
544 msg
->response
= code
;
546 sipmsg_strip_headers(msg
, keepers
);
547 sipmsg_merge_new_headers(msg
);
548 sign_outgoing_message(sipe_private
, msg
);
550 g_string_append_printf(outstr
, "SIP/2.0 %d %s\r\n", code
, text
);
553 name
= ((struct sipnameval
*) (tmp
->data
))->name
;
554 value
= ((struct sipnameval
*) (tmp
->data
))->value
;
556 g_string_append_printf(outstr
, "%s: %s\r\n", name
, value
);
557 tmp
= g_slist_next(tmp
);
559 g_string_append_printf(outstr
, "\r\n%s", body
? body
: "");
560 sipe_utils_message_debug("SIP", outstr
->str
, NULL
, TRUE
);
561 sipe_backend_transport_message(sipe_private
->transport
->connection
, outstr
->str
);
562 g_string_free(outstr
, TRUE
);
565 static void transactions_remove(struct sipe_core_private
*sipe_private
,
566 struct transaction
*trans
)
568 struct sip_transport
*transport
= sipe_private
->transport
;
569 if (transport
->transactions
) {
570 transport
->transactions
= g_slist_remove(transport
->transactions
,
572 SIPE_DEBUG_INFO("SIP transactions count:%d after removal", g_slist_length(transport
->transactions
));
574 if (trans
->msg
) sipmsg_free(trans
->msg
);
575 if (trans
->payload
) {
576 if (trans
->payload
->destroy
)
577 (*trans
->payload
->destroy
)(trans
->payload
->data
);
578 g_free(trans
->payload
);
581 if (trans
->timeout_key
) {
582 sipe_schedule_cancel(sipe_private
, trans
->timeout_key
);
583 g_free(trans
->timeout_key
);
589 static struct transaction
*transactions_find(struct sip_transport
*transport
,
592 GSList
*transactions
= transport
->transactions
;
593 const gchar
*call_id
= sipmsg_find_header(msg
, "Call-ID");
594 const gchar
*cseq
= sipmsg_find_header(msg
, "CSeq");
597 if (!call_id
|| !cseq
) {
598 SIPE_DEBUG_ERROR_NOFORMAT("transaction_find: no Call-ID or CSeq!");
602 key
= g_strdup_printf("<%s><%s>", call_id
, cseq
);
603 while (transactions
) {
604 struct transaction
*trans
= transactions
->data
;
605 if (!g_ascii_strcasecmp(trans
->key
, key
)) {
609 transactions
= transactions
->next
;
616 static void transaction_timeout_cb(struct sipe_core_private
*sipe_private
,
619 struct transaction
*trans
= data
;
620 (trans
->timeout_callback
)(sipe_private
, trans
->msg
, trans
);
621 transactions_remove(sipe_private
, trans
);
624 struct transaction
*sip_transport_request_timeout(struct sipe_core_private
*sipe_private
,
628 const gchar
*addheaders
,
630 struct sip_dialog
*dialog
,
631 TransCallback callback
,
633 TransCallback timeout_callback
)
635 struct sip_transport
*transport
= sipe_private
->transport
;
638 gchar
*ourtag
= dialog
&& dialog
->ourtag
? g_strdup(dialog
->ourtag
) : NULL
;
639 gchar
*theirtag
= dialog
&& dialog
->theirtag
? g_strdup(dialog
->theirtag
) : NULL
;
640 gchar
*theirepid
= dialog
&& dialog
->theirepid
? g_strdup(dialog
->theirepid
) : NULL
;
641 gchar
*callid
= dialog
&& dialog
->callid
? g_strdup(dialog
->callid
) : gencallid();
642 gchar
*branch
= dialog
&& dialog
->callid
? NULL
: genbranch();
643 gchar
*route
= g_strdup("");
644 gchar
*epid
= get_epid(sipe_private
);
645 int cseq
= dialog
? ++dialog
->cseq
: 1 /* as Call-Id is new in this case */;
646 struct transaction
*trans
= NULL
;
648 if (dialog
&& dialog
->routes
)
650 GSList
*iter
= dialog
->routes
;
655 route
= g_strdup_printf("%sRoute: %s\r\n", route
, (char *)iter
->data
);
657 iter
= g_slist_next(iter
);
661 if (!ourtag
&& !dialog
) {
665 if (sipe_strequal(method
, "REGISTER")) {
666 if (sipe_private
->register_callid
) {
668 callid
= g_strdup(sipe_private
->register_callid
);
670 sipe_private
->register_callid
= g_strdup(callid
);
672 cseq
= ++transport
->cseq
;
675 buf
= g_strdup_printf("%s %s SIP/2.0\r\n"
676 "Via: SIP/2.0/%s %s:%d%s%s\r\n"
677 "From: <sip:%s>%s%s;epid=%s\r\n"
678 "To: <%s>%s%s%s%s\r\n"
679 "Max-Forwards: 70\r\n"
684 "Content-Length: %" G_GSIZE_FORMAT
"\r\n\r\n%s",
686 dialog
&& dialog
->request
? dialog
->request
: url
,
687 TRANSPORT_DESCRIPTOR
,
688 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC
),
689 transport
->connection
->client_port
,
690 branch
? ";branch=" : "",
691 branch
? branch
: "",
692 sipe_private
->username
,
693 ourtag
? ";tag=" : "",
694 ourtag
? ourtag
: "",
697 theirtag
? ";tag=" : "",
698 theirtag
? theirtag
: "",
699 theirepid
? ";epid=" : "",
700 theirepid
? theirepid
: "",
703 sip_transport_user_agent(sipe_private
),
706 addheaders
? addheaders
: "",
707 body
? (gsize
) strlen(body
) : 0,
711 //printf ("parsing msg buf:\n%s\n\n", buf);
712 msg
= sipmsg_parse_msg(buf
);
722 sign_outgoing_message(sipe_private
, msg
);
724 /* The authentication scheme is not ready so we can't send the message.
725 This should only happen for REGISTER messages. */
726 if (!transport
->auth_incomplete
) {
727 buf
= sipmsg_to_string(msg
);
729 /* add to ongoing transactions */
730 /* ACK isn't supposed to be answered ever. So we do not keep transaction for it. */
731 if (!sipe_strequal(method
, "ACK")) {
732 trans
= g_new0(struct transaction
, 1);
733 trans
->callback
= callback
;
735 trans
->key
= g_strdup_printf("<%s><%d %s>", callid
, cseq
, method
);
736 if (timeout_callback
) {
737 trans
->timeout_callback
= timeout_callback
;
738 trans
->timeout_key
= g_strdup_printf("<transaction timeout>%s", trans
->key
);
739 sipe_schedule_seconds(sipe_private
,
743 transaction_timeout_cb
,
746 transport
->transactions
= g_slist_append(transport
->transactions
,
748 SIPE_DEBUG_INFO("SIP transactions count:%d after addition", g_slist_length(transport
->transactions
));
751 sipe_utils_message_debug("SIP", buf
, NULL
, TRUE
);
752 sipe_backend_transport_message(transport
->connection
, buf
);
756 if (!trans
) sipmsg_free(msg
);
761 struct transaction
*sip_transport_request(struct sipe_core_private
*sipe_private
,
765 const gchar
*addheaders
,
767 struct sip_dialog
*dialog
,
768 TransCallback callback
)
770 return sip_transport_request_timeout(sipe_private
,
782 static void sip_transport_simple_request(struct sipe_core_private
*sipe_private
,
784 struct sip_dialog
*dialog
)
786 sip_transport_request(sipe_private
,
796 void sip_transport_ack(struct sipe_core_private
*sipe_private
,
797 struct sip_dialog
*dialog
)
799 sip_transport_simple_request(sipe_private
, "ACK", dialog
);
802 void sip_transport_bye(struct sipe_core_private
*sipe_private
,
803 struct sip_dialog
*dialog
)
805 sip_transport_simple_request(sipe_private
, "BYE", dialog
);
808 struct transaction
*sip_transport_info(struct sipe_core_private
*sipe_private
,
809 const gchar
*addheaders
,
811 struct sip_dialog
*dialog
,
812 TransCallback callback
)
814 return sip_transport_request(sipe_private
,
824 struct transaction
*sip_transport_invite(struct sipe_core_private
*sipe_private
,
825 const gchar
*addheaders
,
827 struct sip_dialog
*dialog
,
828 TransCallback callback
)
830 return sip_transport_request(sipe_private
,
840 struct transaction
*sip_transport_service(struct sipe_core_private
*sipe_private
,
842 const gchar
*addheaders
,
844 TransCallback callback
)
846 return sip_transport_request(sipe_private
,
856 void sip_transport_subscribe(struct sipe_core_private
*sipe_private
,
858 const gchar
*addheaders
,
860 struct sip_dialog
*dialog
,
861 TransCallback callback
)
863 sip_transport_request(sipe_private
,
873 static const gchar
*get_auth_header(struct sipe_core_private
*sipe_private
,
874 struct sip_auth
*auth
,
877 auth
->type
= sipe_private
->authentication_type
;
878 auth
->protocol
= auth_type_to_protocol
[auth
->type
];
880 return(sipmsg_find_auth_header(msg
, auth
->protocol
));
883 static void do_register(struct sipe_core_private
*sipe_private
,
884 gboolean deregister
);
886 static void do_reauthenticate_cb(struct sipe_core_private
*sipe_private
,
887 SIPE_UNUSED_PARAMETER gpointer unused
)
889 struct sip_transport
*transport
= sipe_private
->transport
;
891 /* register again when security token expires */
892 /* we have to start a new authentication as the security token
893 * is almost expired by sending a not signed REGISTER message */
894 SIPE_DEBUG_INFO_NOFORMAT("do a full reauthentication");
895 sipe_auth_free(&transport
->registrar
);
896 sipe_auth_free(&transport
->proxy
);
897 sipe_schedule_cancel(sipe_private
, "<registration>");
898 transport
->reregister_set
= FALSE
;
899 transport
->register_attempt
= 0;
900 do_register(sipe_private
, FALSE
);
901 transport
->reauthenticate_set
= FALSE
;
904 static void sip_transport_default_contact(struct sipe_core_private
*sipe_private
)
906 struct sip_transport
*transport
= sipe_private
->transport
;
907 sipe_private
->contact
= g_strdup_printf("<sip:%s:%d;maddr=%s;transport=%s>;proxy=replace",
908 sipe_private
->username
,
909 transport
->connection
->client_port
,
910 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC
),
911 TRANSPORT_DESCRIPTOR
);
914 static void do_register_cb(struct sipe_core_private
*sipe_private
,
915 SIPE_UNUSED_PARAMETER
void *unused
)
917 do_register(sipe_private
, FALSE
);
920 static void sip_transport_set_reregister(struct sipe_core_private
*sipe_private
,
923 sipe_schedule_seconds(sipe_private
,
931 static void sipe_server_register(struct sipe_core_private
*sipe_private
,
936 static gboolean
process_register_response(struct sipe_core_private
*sipe_private
,
938 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
940 struct sip_transport
*transport
= sipe_private
->transport
;
941 const gchar
*expires_header
;
943 GSList
*hdr
= msg
->headers
;
944 struct sipnameval
*elem
;
946 expires_header
= sipmsg_find_header(msg
, "Expires");
947 expires
= expires_header
!= NULL
? strtol(expires_header
, NULL
, 10) : 0;
948 SIPE_DEBUG_INFO("process_register_response: got response to REGISTER; expires = %d", expires
);
950 switch (msg
->response
) {
953 const gchar
*contact_hdr
;
954 const gchar
*auth_hdr
;
958 const gchar
*server_hdr
= sipmsg_find_header(msg
, "Server");
960 if (!transport
->reregister_set
) {
961 sip_transport_set_reregister(sipe_private
,
963 transport
->reregister_set
= TRUE
;
966 if (server_hdr
&& !transport
->server_version
) {
967 transport
->server_version
= g_strdup(server_hdr
);
968 g_free(transport
->user_agent
);
969 transport
->user_agent
= NULL
;
972 auth_hdr
= get_auth_header(sipe_private
, &transport
->registrar
, msg
);
974 SIPE_DEBUG_INFO("process_register_response: Auth header: %s", auth_hdr
);
975 fill_auth(auth_hdr
, &transport
->registrar
);
978 if (!transport
->reauthenticate_set
) {
979 gchar
*action_name
= g_strdup_printf("<%s>", "+reauthentication");
980 guint reauth_timeout
= transport
->registrar
.expires
;
982 SIPE_DEBUG_INFO_NOFORMAT("process_register_response: authentication handshake completed successfully");
984 /* Does authentication scheme provide valid expiration time? */
985 if (reauth_timeout
<= (5 * 60)) {
986 SIPE_DEBUG_INFO_NOFORMAT("process_register_response: no expiration time - using default of 8 hours");
987 reauth_timeout
= 8 * 60 * 60;
990 /* schedule reauthentication 5 minutes before expiration */
991 sipe_schedule_seconds(sipe_private
,
994 reauth_timeout
- 5 * 60,
995 do_reauthenticate_cb
,
998 transport
->reauthenticate_set
= TRUE
;
1001 sipe_backend_connection_completed(SIPE_CORE_PUBLIC
);
1003 uuid
= get_uuid(sipe_private
);
1005 // There can be multiple Contact headers (one per location where the user is logged in) so
1006 // make sure to only get the one for this uuid
1007 for (i
= 0; (contact_hdr
= sipmsg_find_header_instance (msg
, "Contact", i
)); i
++) {
1008 gchar
* valid_contact
= sipmsg_find_part_of_header (contact_hdr
, uuid
, NULL
, NULL
);
1009 if (valid_contact
) {
1010 gruu
= sipmsg_find_part_of_header(contact_hdr
, "gruu=\"", "\"", NULL
);
1011 //SIPE_DEBUG_INFO("process_register_response: got gruu %s from contact hdr w/ right uuid: %s", gruu, contact_hdr);
1012 g_free(valid_contact
);
1015 //SIPE_DEBUG_INFO("process_register_response: ignoring contact hdr b/c not right uuid: %s", contact_hdr);
1020 g_free(sipe_private
->contact
);
1022 sipe_private
->contact
= g_strdup_printf("<%s>", gruu
);
1025 //SIPE_DEBUG_INFO_NOFORMAT("process_register_response: didn't find gruu in a Contact hdr");
1026 sip_transport_default_contact(sipe_private
);
1028 SIPE_CORE_PRIVATE_FLAG_UNSET(OCS2007
);
1029 SIPE_CORE_PRIVATE_FLAG_UNSET(REMOTE_USER
);
1030 SIPE_CORE_PRIVATE_FLAG_UNSET(BATCHED_SUPPORT
);
1035 if (sipe_strcase_equal(elem
->name
, "Supported")) {
1036 if (sipe_strcase_equal(elem
->value
, "msrtc-event-categories")) {
1037 /* We interpret this as OCS2007+ indicator */
1038 SIPE_CORE_PRIVATE_FLAG_SET(OCS2007
);
1039 SIPE_DEBUG_INFO("Supported: %s (indicates OCS2007+)", elem
->value
);
1041 if (sipe_strcase_equal(elem
->value
, "adhoclist")) {
1042 SIPE_CORE_PRIVATE_FLAG_SET(BATCHED_SUPPORT
);
1043 SIPE_DEBUG_INFO("Supported: %s", elem
->value
);
1046 if (sipe_strcase_equal(elem
->name
, "Allow-Events")){
1047 gchar
**caps
= g_strsplit(elem
->value
,",",0);
1050 sipe_private
->allowed_events
= g_slist_append(sipe_private
->allowed_events
, g_strdup(caps
[i
]));
1051 SIPE_DEBUG_INFO("Allow-Events: %s", caps
[i
]);
1056 if (sipe_strcase_equal(elem
->name
, "ms-user-logon-data")) {
1057 if (sipe_strcase_equal(elem
->value
, "RemoteUser")) {
1058 SIPE_CORE_PRIVATE_FLAG_SET(REMOTE_USER
);
1059 SIPE_DEBUG_INFO_NOFORMAT("ms-user-logon-data: RemoteUser (connected "
1060 "via Edge Server)");
1063 hdr
= g_slist_next(hdr
);
1066 /* rejoin open chats to be able to use them by continue to send messages */
1067 sipe_backend_chat_rejoin_all(SIPE_CORE_PUBLIC
);
1069 /* subscriptions, done only once */
1070 if (!transport
->subscribed
) {
1071 sipe_subscription_self_events(sipe_private
);
1072 transport
->subscribed
= TRUE
;
1075 timeout
= sipmsg_find_part_of_header(sipmsg_find_header(msg
, "ms-keep-alive"),
1076 "timeout=", ";", NULL
);
1077 if (timeout
!= NULL
) {
1078 sscanf(timeout
, "%u", &sipe_private
->public.keepalive_timeout
);
1079 SIPE_DEBUG_INFO("process_register_response: server determined keep alive timeout is %u seconds",
1080 sipe_private
->public.keepalive_timeout
);
1084 SIPE_DEBUG_INFO("process_register_response: got 200, removing CSeq: %d", transport
->cseq
);
1089 gchar
*redirect
= parse_from(sipmsg_find_header(msg
, "Contact"));
1091 SIPE_DEBUG_INFO_NOFORMAT("process_register_response: authentication handshake completed successfully (with redirect)");
1093 if (redirect
&& (g_ascii_strncasecmp("sip:", redirect
, 4) == 0)) {
1094 gchar
**parts
= g_strsplit(redirect
+ 4, ";", 0);
1098 guint transport_type
= SIPE_TRANSPORT_TLS
;
1101 tmp
= g_strsplit(parts
[0], ":", 0);
1102 hostname
= g_strdup(tmp
[0]);
1103 if (tmp
[1]) port
= strtoul(tmp
[1], NULL
, 10);
1107 tmp
= g_strsplit(parts
[i
], "=", 0);
1109 if (g_ascii_strcasecmp("transport", tmp
[0]) == 0) {
1110 if (g_ascii_strcasecmp("tcp", tmp
[1]) == 0) {
1111 transport_type
= SIPE_TRANSPORT_TCP
;
1120 /* Close old connection */
1121 sipe_core_connection_cleanup(sipe_private
);
1122 /* transport and sipe_private->transport are invalid after this */
1124 /* Create new connection */
1125 sipe_server_register(sipe_private
, transport_type
, hostname
, port
);
1126 /* sipe_private->transport has a new value */
1127 SIPE_DEBUG_INFO("process_register_response: redirected to host %s port %d transport %d",
1128 hostname
, port
, transport_type
);
1135 const char *auth_hdr
;
1137 SIPE_DEBUG_INFO("process_register_response: REGISTER retries %d", transport
->registrar
.retries
);
1139 if (transport
->reauthenticate_set
) {
1140 SIPE_DEBUG_ERROR_NOFORMAT("process_register_response: RE-REGISTER rejected, triggering re-authentication");
1141 do_reauthenticate_cb(sipe_private
, NULL
);
1145 if (sip_sec_context_is_ready(transport
->registrar
.gssapi_context
)) {
1146 SIPE_DEBUG_INFO_NOFORMAT("process_register_response: authentication handshake failed - giving up.");
1147 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1148 SIPE_CONNECTION_ERROR_AUTHENTICATION_FAILED
,
1149 _("Authentication failed"));
1153 auth_hdr
= get_auth_header(sipe_private
, &transport
->registrar
, msg
);
1155 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1156 SIPE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE
,
1157 _("Incompatible authentication scheme chosen"));
1160 SIPE_DEBUG_INFO("process_register_response: Auth header: %s", auth_hdr
);
1161 fill_auth(auth_hdr
, &transport
->registrar
);
1162 transport
->reregister_set
= FALSE
;
1163 transport
->register_attempt
= 0;
1164 do_register(sipe_private
,
1165 sipe_backend_connection_is_disconnecting(SIPE_CORE_PUBLIC
));
1172 sipmsg_parse_warning(msg
, &reason
);
1173 reason
= reason
? reason
: sipmsg_get_ms_diagnostics_public_reason(msg
);
1174 warning
= g_strdup_printf(_("You have been rejected by the server: %s"),
1175 reason
? reason
: _("no reason given"));
1178 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1179 SIPE_CONNECTION_ERROR_INVALID_SETTINGS
,
1187 const gchar
*diagnostics
= sipmsg_find_header(msg
, "ms-diagnostics");
1188 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1190 warning
= g_strdup_printf(_("Not found: %s. Please contact your Administrator"),
1191 diagnostics
? (reason
? reason
: _("no reason given")) :
1192 _("SIP is either not enabled for the destination URI or it does not exist"));
1195 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1196 SIPE_CONNECTION_ERROR_INVALID_USERNAME
,
1202 case 504: /* Server time-out */
1203 /* first attempt + 5 retries */
1204 if (transport
->register_attempt
< 6) {
1205 SIPE_DEBUG_INFO("process_register_response: RE-REGISTER timeout on attempt %d, retrying later",
1206 transport
->register_attempt
);
1207 sip_transport_set_reregister(sipe_private
, 60);
1213 gchar
*reason
= sipmsg_get_ms_diagnostics_reason(msg
);
1215 warning
= g_strdup_printf(_("Service unavailable: %s"), reason
? reason
: _("no reason given"));
1218 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1219 SIPE_CONNECTION_ERROR_NETWORK
,
1229 static gboolean
register_response_timeout(struct sipe_core_private
*sipe_private
,
1230 SIPE_UNUSED_PARAMETER
struct sipmsg
*msg
,
1231 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
1233 struct sip_transport
*transport
= sipe_private
->transport
;
1234 if (transport
->register_attempt
< 6) {
1235 SIPE_DEBUG_INFO("register_response_timeout: no answer to attempt %d, retrying",
1236 transport
->register_attempt
);
1237 do_register(sipe_private
, FALSE
);
1239 gchar
*warning
= g_strdup_printf(_("Service unavailable: %s"), _("no reason given"));
1240 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1241 SIPE_CONNECTION_ERROR_NETWORK
,
1248 static void do_register(struct sipe_core_private
*sipe_private
,
1249 gboolean deregister
)
1251 struct sip_transport
*transport
= sipe_private
->transport
;
1257 if (!sipe_private
->public.sip_domain
) return;
1260 if (transport
->reregister_set
) {
1261 transport
->reregister_set
= FALSE
;
1262 transport
->register_attempt
= 1;
1264 transport
->register_attempt
++;
1268 transport
->deregister
= deregister
;
1269 transport
->auth_incomplete
= FALSE
;
1271 uuid
= get_uuid(sipe_private
);
1272 hdr
= g_strdup_printf("Contact: <sip:%s:%d;transport=%s;ms-opaque=d3470f2e1d>;methods=\"INVITE, MESSAGE, INFO, SUBSCRIBE, OPTIONS, BYE, CANCEL, NOTIFY, ACK, REFER, BENOTIFY\";proxy=replace;+sip.instance=\"<urn:uuid:%s>\"\r\n"
1273 "Supported: gruu-10, adhoclist, msrtc-event-categories, com.microsoft.msrtc.presence\r\n"
1274 "Event: registration\r\n"
1275 "Allow-Events: presence\r\n"
1276 "ms-keep-alive: UAC;hop-hop=yes\r\n"
1278 sipe_backend_network_ip_address(SIPE_CORE_PUBLIC
),
1279 transport
->connection
->client_port
,
1280 TRANSPORT_DESCRIPTOR
,
1282 deregister
? "Expires: 0\r\n" : "");
1285 uri
= sip_uri_from_name(sipe_private
->public.sip_domain
);
1286 to
= sip_uri_self(sipe_private
);
1287 sip_transport_request_timeout(sipe_private
,
1294 process_register_response
,
1296 deregister
? NULL
: register_response_timeout
);
1302 /* Make sure that all messages are pushed to the server
1303 before the connection gets shut down */
1304 SIPE_DEBUG_INFO_NOFORMAT("De-register from server. Flushing outstanding messages.");
1305 sipe_backend_transport_flush(transport
->connection
);
1309 void sip_transport_deregister(struct sipe_core_private
*sipe_private
)
1311 do_register(sipe_private
, TRUE
);
1314 void sip_transport_disconnect(struct sipe_core_private
*sipe_private
)
1316 struct sip_transport
*transport
= sipe_private
->transport
;
1318 /* transport can be NULL during connection setup */
1320 sipe_backend_transport_disconnect(transport
->connection
);
1322 sipe_auth_free(&transport
->registrar
);
1323 sipe_auth_free(&transport
->proxy
);
1325 g_free(transport
->server_name
);
1326 g_free(transport
->server_version
);
1327 g_free(transport
->user_agent
);
1329 while (transport
->transactions
)
1330 transactions_remove(sipe_private
,
1331 transport
->transactions
->data
);
1336 sipe_private
->transport
= NULL
;
1337 sipe_private
->service_data
= NULL
;
1338 sipe_private
->address_data
= NULL
;
1340 if (sipe_private
->dns_query
)
1341 sipe_backend_dns_query_cancel(sipe_private
->dns_query
);
1345 void sip_transport_authentication_completed(struct sipe_core_private
*sipe_private
)
1347 do_reauthenticate_cb(sipe_private
, NULL
);
1350 guint
sip_transport_port(struct sipe_core_private
*sipe_private
)
1352 return sipe_private
->transport
->server_port
;
1355 static void process_input_message(struct sipe_core_private
*sipe_private
,
1358 struct sip_transport
*transport
= sipe_private
->transport
;
1359 gboolean notfound
= FALSE
;
1360 const char *method
= msg
->method
? msg
->method
: "NOT FOUND";
1362 SIPE_DEBUG_INFO("process_input_message: msg->response(%d),msg->method(%s)",
1363 msg
->response
, method
);
1365 if (msg
->response
== 0) { /* request */
1366 if (sipe_strequal(method
, "MESSAGE")) {
1367 process_incoming_message(sipe_private
, msg
);
1368 } else if (sipe_strequal(method
, "NOTIFY")) {
1369 SIPE_DEBUG_INFO_NOFORMAT("send->process_incoming_notify");
1370 process_incoming_notify(sipe_private
, msg
);
1371 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1372 } else if (sipe_strequal(method
, "BENOTIFY")) {
1373 SIPE_DEBUG_INFO_NOFORMAT("send->process_incoming_benotify");
1374 process_incoming_notify(sipe_private
, msg
);
1375 } else if (sipe_strequal(method
, "INVITE")) {
1376 process_incoming_invite(sipe_private
, msg
);
1377 } else if (sipe_strequal(method
, "REFER")) {
1378 process_incoming_refer(sipe_private
, msg
);
1379 } else if (sipe_strequal(method
, "OPTIONS")) {
1380 process_incoming_options(sipe_private
, msg
);
1381 } else if (sipe_strequal(method
, "INFO")) {
1382 process_incoming_info(sipe_private
, msg
);
1383 } else if (sipe_strequal(method
, "ACK")) {
1384 /* ACK's don't need any response */
1385 } else if (sipe_strequal(method
, "PRACK")) {
1386 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1387 } else if (sipe_strequal(method
, "SUBSCRIBE")) {
1388 /* LCS 2005 sends us these - just respond 200 OK */
1389 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1390 } else if (sipe_strequal(method
, "CANCEL")) {
1391 process_incoming_cancel(sipe_private
, msg
);
1392 } else if (sipe_strequal(method
, "BYE")) {
1393 process_incoming_bye(sipe_private
, msg
);
1395 sip_transport_response(sipe_private
, msg
, 501, "Not implemented", NULL
);
1399 } else { /* response */
1400 struct transaction
*trans
= transactions_find(transport
, msg
);
1402 if (msg
->response
< 200) {
1403 /* ignore provisional response */
1404 SIPE_DEBUG_INFO("process_input_message: got provisional (%d) response, ignoring", msg
->response
);
1406 /* Transaction not yet completed */
1409 } else if (msg
->response
== 401) { /* Unauthorized */
1411 if (sipe_strequal(trans
->msg
->method
, "REGISTER")) {
1412 /* Expected response during authentication handshake */
1413 transport
->registrar
.retries
++;
1414 SIPE_DEBUG_INFO("process_input_message: RE-REGISTER CSeq: %d", transport
->cseq
);
1418 /* Are we registered? */
1419 if (transport
->reregister_set
) {
1420 SIPE_DEBUG_INFO_NOFORMAT("process_input_message: 401 response to non-REGISTER message. Retrying with new authentication.");
1421 sign_outgoing_message(sipe_private
,
1425 * We don't have a valid authentication at the moment.
1426 * Resend message unchanged. It will be rejected again
1427 * and hopefully by then we have a valid authentication.
1429 SIPE_DEBUG_INFO_NOFORMAT("process_input_message: 401 response to non-REGISTER message. Bouncing...");
1432 /* Resend request */
1433 resend
= sipmsg_to_string(trans
->msg
);
1434 sipe_utils_message_debug("SIP", resend
, NULL
, TRUE
);
1435 sipe_backend_transport_message(sipe_private
->transport
->connection
, resend
);
1438 /* Transaction not yet completed */
1442 } else if (msg
->response
== 407) { /* Proxy Authentication Required */
1444 if (transport
->proxy
.retries
> 30) {
1445 SIPE_DEBUG_ERROR_NOFORMAT("process_input_message: too many proxy authentication retries. Giving up.");
1447 gchar
*resend
, *auth
;
1448 const gchar
*auth_hdr
;
1450 transport
->proxy
.retries
++;
1452 /* do proxy authentication */
1453 auth_hdr
= sipmsg_find_header(msg
, "Proxy-Authenticate");
1456 transport
->proxy
.type
= SIPE_AUTHENTICATION_TYPE_UNSET
;
1457 for (i
= 0; i
< AUTH_PROTOCOLS
; i
++) {
1458 const gchar
*protocol
= auth_type_to_protocol
[i
];
1460 !g_ascii_strncasecmp(auth_hdr
, protocol
, strlen(protocol
))) {
1461 SIPE_DEBUG_INFO("proxy auth: type %s", protocol
);
1462 transport
->proxy
.type
= i
;
1463 transport
->proxy
.protocol
= protocol
;
1467 if (transport
->proxy
.type
== SIPE_AUTHENTICATION_TYPE_UNSET
)
1468 SIPE_DEBUG_ERROR("Unknown proxy authentication: %s", auth_hdr
);
1469 fill_auth(auth_hdr
, &transport
->proxy
);
1471 auth
= auth_header(sipe_private
, &transport
->proxy
, trans
->msg
);
1473 sipmsg_remove_header_now(trans
->msg
, "Proxy-Authorization");
1474 sipmsg_add_header_now_pos(trans
->msg
, "Proxy-Authorization", auth
, 5);
1478 /* resend request */
1479 resend
= sipmsg_to_string(trans
->msg
);
1480 sipe_utils_message_debug("SIP", resend
, NULL
, TRUE
);
1481 sipe_backend_transport_message(sipe_private
->transport
->connection
, resend
);
1484 /* Transaction not yet completed */
1489 transport
->registrar
.retries
= 0;
1490 transport
->proxy
.retries
= 0;
1493 /* Is transaction completed? */
1495 if (trans
->callback
) {
1496 SIPE_DEBUG_INFO_NOFORMAT("process_input_message: we have a transaction callback");
1497 /* call the callback to process response */
1498 (trans
->callback
)(sipe_private
, msg
, trans
);
1499 /* transport && trans no longer valid after redirect */
1503 * Redirect case: sipe_private->transport is
1504 * the new transport with empty queue
1506 if (sipe_private
->transport
->transactions
) {
1507 SIPE_DEBUG_INFO("process_input_message: removing CSeq %d", transport
->cseq
);
1508 transactions_remove(sipe_private
, trans
);
1512 SIPE_DEBUG_INFO_NOFORMAT("process_input_message: received response to unknown transaction");
1518 SIPE_DEBUG_INFO("received a unknown sip message with method %s and response %d", method
, msg
->response
);
1522 static void sip_transport_input(struct sipe_transport_connection
*conn
)
1524 struct sipe_core_private
*sipe_private
= conn
->user_data
;
1525 struct sip_transport
*transport
= sipe_private
->transport
;
1526 gchar
*cur
= conn
->buffer
;
1528 /* according to the RFC remove CRLF at the beginning */
1529 while (*cur
== '\r' || *cur
== '\n') {
1532 if (cur
!= conn
->buffer
)
1533 sipe_utils_shrink_buffer(conn
, cur
);
1535 /* Received a full Header? */
1536 transport
->processing_input
= TRUE
;
1537 while (transport
->processing_input
&&
1538 ((cur
= strstr(conn
->buffer
, "\r\n\r\n")) != NULL
)) {
1544 msg
= sipmsg_parse_header(conn
->buffer
);
1547 remainder
= conn
->buffer_used
- (cur
- conn
->buffer
);
1548 if (msg
&& remainder
>= (guint
) msg
->bodylen
) {
1549 char *dummy
= g_malloc(msg
->bodylen
+ 1);
1550 memcpy(dummy
, cur
, msg
->bodylen
);
1551 dummy
[msg
->bodylen
] = '\0';
1553 cur
+= msg
->bodylen
;
1554 sipe_utils_message_debug("SIP",
1558 sipe_utils_shrink_buffer(conn
, cur
);
1561 SIPE_DEBUG_INFO("sipe_transport_input: body too short (%d < %d, strlen %d) - ignoring message", remainder
, msg
->bodylen
, (int)strlen(conn
->buffer
));
1565 /* restore header for next try */
1570 // Verify the signature before processing it
1571 if (sip_sec_context_is_ready(transport
->registrar
.gssapi_context
)) {
1572 struct sipmsg_breakdown msgbd
;
1573 gchar
*signature_input_str
;
1576 sipmsg_breakdown_parse(&msgbd
, transport
->registrar
.realm
, transport
->registrar
.target
,
1577 transport
->registrar
.protocol
);
1578 signature_input_str
= sipmsg_breakdown_get_string(transport
->registrar
.version
, &msgbd
);
1580 rspauth
= sipmsg_find_part_of_header(sipmsg_find_header(msg
, "Authentication-Info"), "rspauth=\"", "\"", NULL
);
1582 if (rspauth
!= NULL
) {
1583 if (sip_sec_verify_signature(transport
->registrar
.gssapi_context
, signature_input_str
, rspauth
)) {
1584 SIPE_DEBUG_INFO_NOFORMAT("sip_transport_input: signature of incoming message validated");
1585 process_input_message(sipe_private
, msg
);
1586 /* transport is invalid after redirect */
1588 SIPE_DEBUG_INFO_NOFORMAT("sip_transport_input: signature of incoming message is invalid.");
1589 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1590 SIPE_CONNECTION_ERROR_NETWORK
,
1591 _("Invalid message signature received"));
1593 } else if ((msg
->response
== 401) ||
1594 sipe_strequal(msg
->method
, "REGISTER")) {
1595 /* a) Retry non-REGISTER requests with updated authentication */
1596 /* b) We must always process REGISTER responses */
1597 process_input_message(sipe_private
, msg
);
1599 /* OCS sends provisional messages that are *not* signed */
1600 if (msg
->response
>= 200) {
1601 /* We are not calling process_input_message(),
1602 so we need to drop the transaction here. */
1603 struct transaction
*trans
= transactions_find(transport
, msg
);
1604 if (trans
) transactions_remove(sipe_private
, trans
);
1606 SIPE_DEBUG_INFO_NOFORMAT("sip_transport_input: message without authentication data - ignoring");
1608 g_free(signature_input_str
);
1611 sipmsg_breakdown_free(&msgbd
);
1613 process_input_message(sipe_private
, msg
);
1618 /* Redirect: old content of "transport" & "conn" is no longer valid */
1619 transport
= sipe_private
->transport
;
1620 conn
= transport
->connection
;
1624 static void sip_transport_connected(struct sipe_transport_connection
*conn
)
1626 struct sipe_core_private
*sipe_private
= conn
->user_data
;
1627 sipe_private
->service_data
= NULL
;
1628 sipe_private
->address_data
= NULL
;
1629 do_register(sipe_private
, FALSE
);
1632 static void resolve_next_service(struct sipe_core_private
*sipe_private
,
1633 const struct sip_service_data
*start
);
1634 static void resolve_next_address(struct sipe_core_private
*sipe_private
,
1636 static void sip_transport_error(struct sipe_transport_connection
*conn
,
1639 struct sipe_core_private
*sipe_private
= conn
->user_data
;
1641 /* This failed attempt was based on a DNS SRV record */
1642 if (sipe_private
->service_data
) {
1643 resolve_next_service(sipe_private
, NULL
);
1644 /* This failed attempt was based on a DNS A record */
1645 } else if (sipe_private
->address_data
) {
1646 resolve_next_address(sipe_private
, FALSE
);
1648 sipe_backend_connection_error(SIPE_CORE_PUBLIC
,
1649 SIPE_CONNECTION_ERROR_NETWORK
,
1654 /* server_name must be g_alloc()'ed */
1655 static void sipe_server_register(struct sipe_core_private
*sipe_private
,
1660 sipe_connect_setup setup
= {
1663 (server_port
!= 0) ? server_port
:
1664 (type
== SIPE_TRANSPORT_TLS
) ? 5061 : 5060,
1666 sip_transport_connected
,
1667 sip_transport_input
,
1670 struct sip_transport
*transport
= g_new0(struct sip_transport
, 1);
1672 transport
->server_name
= server_name
;
1673 transport
->server_port
= setup
.server_port
;
1674 transport
->connection
= sipe_backend_transport_connect(SIPE_CORE_PUBLIC
,
1676 sipe_private
->transport
= transport
;
1679 struct sip_service_data
{
1680 const char *protocol
;
1681 const char *transport
;
1686 * Autodiscover using DNS SRV records. See RFC2782/3263
1688 * Service list for AUTO
1690 static const struct sip_service_data service_autodetect
[] = {
1691 { "sipinternaltls", "tcp", SIPE_TRANSPORT_TLS
}, /* for internal TLS connections */
1692 { "sipinternal", "tcp", SIPE_TRANSPORT_TCP
}, /* for internal TCP connections */
1693 { "sip", "tls", SIPE_TRANSPORT_TLS
}, /* for external TLS connections */
1694 { "sip", "tcp", SIPE_TRANSPORT_TCP
}, /*.for external TCP connections */
1698 /* Service list for SSL/TLS */
1699 static const struct sip_service_data service_tls
[] = {
1700 { "sipinternaltls", "tcp", SIPE_TRANSPORT_TLS
}, /* for internal TLS connections */
1701 { "sip", "tls", SIPE_TRANSPORT_TLS
}, /* for external TLS connections */
1705 /* Service list for TCP */
1706 static const struct sip_service_data service_tcp
[] = {
1707 { "sipinternal", "tcp", SIPE_TRANSPORT_TCP
}, /* for internal TCP connections */
1708 { "sip", "tcp", SIPE_TRANSPORT_TCP
}, /*.for external TCP connections */
1712 static const struct sip_service_data
*services
[] = {
1713 service_autodetect
, /* SIPE_TRANSPORT_AUTO */
1714 service_tls
, /* SIPE_TRANSPORT_TLS */
1715 service_tcp
/* SIPE_TRANSPORT_TCP */
1718 struct sip_address_data
{
1724 * Autodiscover using DNS A records. This is an extension addded
1725 * by Microsoft. See http://support.microsoft.com/kb/2619522
1727 static const struct sip_address_data addresses
[] = {
1728 { "sipinternal", 5061 },
1729 { "sipexternal", 443 },
1731 * Our implementation supports only one port per host name. If the host name
1732 * resolves OK, we abort the search and try to connect. If we would know if we
1733 * are trying to connect from "Intranet" or "Internet" then we could choose
1734 * between those two ports.
1736 * We drop port 5061 in order to cover the "Internet" case.
1744 static void sipe_core_dns_resolved(struct sipe_core_public
*sipe_public
,
1745 const gchar
*hostname
, guint port
)
1747 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1748 gboolean service
= sipe_private
->service_data
!= NULL
;
1750 sipe_private
->dns_query
= NULL
;
1757 host
= g_strdup(hostname
);
1758 type
= sipe_private
->service_data
->type
;
1760 /* DNS A resolver returns an IP address */
1761 host
= g_strdup_printf("%s.%s",
1762 sipe_private
->address_data
->prefix
,
1763 sipe_private
->public.sip_domain
);
1764 port
= sipe_private
->address_data
->port
;
1765 type
= sipe_private
->transport_type
;
1766 if (type
== SIPE_TRANSPORT_AUTO
)
1767 type
= SIPE_TRANSPORT_TLS
;
1770 SIPE_DEBUG_INFO("sipe_core_dns_resolved - %s hostname: %s port: %d",
1771 service
? "SRV" : "A", hostname
, port
);
1772 sipe_server_register(sipe_private
, type
, host
, port
);
1775 resolve_next_service(SIPE_CORE_PRIVATE
, NULL
);
1777 resolve_next_address(SIPE_CORE_PRIVATE
, FALSE
);
1781 static void resolve_next_service(struct sipe_core_private
*sipe_private
,
1782 const struct sip_service_data
*start
)
1785 sipe_private
->service_data
= start
;
1787 sipe_private
->service_data
++;
1788 if (sipe_private
->service_data
->protocol
== NULL
) {
1790 /* We tried all services */
1791 sipe_private
->service_data
= NULL
;
1793 /* Try A records list next */
1794 SIPE_DEBUG_INFO_NOFORMAT("no SRV records found; trying A records next");
1795 resolve_next_address(sipe_private
, TRUE
);
1800 /* Try to resolve next service */
1801 sipe_private
->dns_query
= sipe_backend_dns_query_srv(
1803 sipe_private
->service_data
->protocol
,
1804 sipe_private
->service_data
->transport
,
1805 sipe_private
->public.sip_domain
,
1806 (sipe_dns_resolved_cb
) sipe_core_dns_resolved
,
1810 static void resolve_next_address(struct sipe_core_private
*sipe_private
,
1816 sipe_private
->address_data
= addresses
;
1818 sipe_private
->address_data
++;
1819 if (sipe_private
->address_data
->prefix
== NULL
) {
1820 guint type
= sipe_private
->transport_type
;
1822 /* We tried all addresss */
1823 sipe_private
->address_data
= NULL
;
1825 /* Try connecting to the SIP hostname directly */
1826 SIPE_DEBUG_INFO_NOFORMAT("no SRV or A records found; using SIP domain as fallback");
1827 if (type
== SIPE_TRANSPORT_AUTO
)
1828 type
= SIPE_TRANSPORT_TLS
;
1830 sipe_server_register(sipe_private
, type
,
1831 g_strdup(sipe_private
->public.sip_domain
),
1837 /* Try to resolve next address */
1838 hostname
= g_strdup_printf("%s.%s",
1839 sipe_private
->address_data
->prefix
,
1840 sipe_private
->public.sip_domain
);
1841 sipe_private
->dns_query
= sipe_backend_dns_query_a(
1844 sipe_private
->address_data
->port
,
1845 (sipe_dns_resolved_cb
) sipe_core_dns_resolved
,
1851 * NOTE: this function can be called before sipe_core_allocate()!
1853 gboolean
sipe_core_transport_sip_requires_password(guint authentication
,
1856 return(sip_sec_requires_password(authentication
, sso
));
1859 void sipe_core_transport_sip_connect(struct sipe_core_public
*sipe_public
,
1861 guint authentication
,
1862 const gchar
*server
,
1865 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1867 sipe_private
->authentication_type
= authentication
;
1870 * Initializing the certificate sub-system will trigger the generation
1871 * of a cryptographic key pair which takes time. If we do this after we
1872 * have connected to the server then there is a risk that we run into a
1873 * SIP connection timeout. So let's get this out of the way now...
1875 * This is currently only needed if the user has selected TLS-DSK.
1877 if (sipe_private
->authentication_type
== SIPE_AUTHENTICATION_TYPE_TLS_DSK
)
1878 sipe_certificate_init(sipe_private
);
1881 /* Use user specified server[:port] */
1882 int port_number
= 0;
1885 port_number
= atoi(port
);
1887 SIPE_DEBUG_INFO("sipe_core_connect: user specified SIP server %s:%d",
1888 server
, port_number
);
1890 sipe_server_register(sipe_private
, transport
,
1891 g_strdup(server
), port_number
);
1893 /* Server auto-discovery */
1895 /* Remember user specified transport type */
1896 sipe_private
->transport_type
= transport
;
1897 resolve_next_service(sipe_private
, services
[transport
]);
1901 void sipe_core_transport_sip_keepalive(struct sipe_core_public
*sipe_public
)
1903 SIPE_DEBUG_INFO("sending keep alive %d",
1904 sipe_public
->keepalive_timeout
);
1905 sipe_utils_message_debug("SIP", "", NULL
, TRUE
);
1906 sipe_backend_transport_message(SIPE_CORE_PRIVATE
->transport
->connection
,
1910 const gchar
*sipe_core_transport_sip_server_name(struct sipe_core_public
*sipe_public
)
1912 struct sip_transport
*transport
= SIPE_CORE_PRIVATE
->transport
;
1913 return(transport
? transport
->server_name
: NULL
);
1916 int sip_transaction_cseq(struct transaction
*trans
)
1920 g_return_val_if_fail(trans
&& trans
->key
, 0);
1922 sscanf(trans
->key
, "<%*[a-zA-Z0-9]><%d INVITE>", &cseq
);