sal.cpp 37 KB
Newer Older
1 2
/*
 * sal.cpp
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19
 */

20 21
#include <algorithm>

22 23 24
#include "sal/sal.h"
#include "sal/call-op.h"
#include "sal/presence-op.h"
25
#include "sal/refer-op.h"
26 27
#include "sal/event-op.h"
#include "sal/message-op.h"
28 29 30 31
#include "bellesip_sal/sal_impl.h"
#include "tester_utils.h"
#include "private.h"

32 33
#include "c-wrapper/internal/c-tools.h"

34 35 36 37
using namespace std;

LINPHONE_BEGIN_NAMESPACE

Ghislain MARY's avatar
Ghislain MARY committed
38 39 40 41 42 43 44
void Sal::processDialogTerminatedCb (void *sal, const belle_sip_dialog_terminated_event_t *event) {
	auto dialog = belle_sip_dialog_terminated_event_get_dialog(event);
	auto op = static_cast<SalOp *>(belle_sip_dialog_get_application_data(dialog));
	if (op && op->mCallbacks && op->mCallbacks->process_dialog_terminated)
		op->mCallbacks->process_dialog_terminated(op, event);
	else
		lError() << "Sal::processDialogTerminatedCb(): no op found for this dialog [" << dialog << "], ignoring";
45 46
}

Ghislain MARY's avatar
Ghislain MARY committed
47 48 49 50 51 52 53 54
void Sal::processIoErrorCb (void *userCtx, const belle_sip_io_error_event_t *event) {
	if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(belle_sip_io_error_event_get_source(event), belle_sip_client_transaction_t)) {
		auto client_transaction = BELLE_SIP_CLIENT_TRANSACTION(belle_sip_io_error_event_get_source(event));
		auto op = static_cast<SalOp *>(belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(client_transaction)));
		// Also reset auth count on IO error
		op->mAuthRequests = 0;
		if (op->mCallbacks && op->mCallbacks->process_io_error)
			op->mCallbacks->process_io_error(op, event);
55
	} else {
Ghislain MARY's avatar
Ghislain MARY committed
56
		// Nop, because already handled at transaction layer
57 58 59
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
60 61 62 63 64 65
void Sal::processRequestEventCb (void *userCtx, const belle_sip_request_event_t *event) {
	auto sal = static_cast<Sal *>(userCtx);
	SalOp *op = nullptr;
	belle_sip_header_t *evh = nullptr;
	auto request = belle_sip_request_event_get_request(event);
	string method = belle_sip_request_get_method(request);
66

Ghislain MARY's avatar
Ghislain MARY committed
67
	auto dialog = belle_sip_request_event_get_dialog(event);
68
	if (dialog) {
Ghislain MARY's avatar
Ghislain MARY committed
69 70 71 72 73
		op = static_cast<SalOp *>(belle_sip_dialog_get_application_data(dialog));
		if (!op && (method == "NOTIFY")) {
			// Special case for dialog created by notify matching subscribe
			auto subscribeTransaction = belle_sip_dialog_get_last_transaction(dialog);
			op = static_cast<SalOp *>(belle_sip_transaction_get_application_data(subscribeTransaction));
74
		}
Ghislain MARY's avatar
Ghislain MARY committed
75 76
		if (!op || (op->mState == SalOp::State::Terminated)) {
			lWarning() << "Receiving request for null or terminated op [" << op << "], ignored";
77 78
			return;
		}
Benjamin REIS's avatar
Benjamin REIS committed
79
	} else {
Ghislain MARY's avatar
Ghislain MARY committed
80 81 82 83 84 85
		// Handle the case where we are receiving a request with to tag but it is not belonging to any dialog
		auto toHeader = belle_sip_message_get_header_by_type(request, belle_sip_header_to_t);
		if (((method == "INVITE") || (method == "NOTIFY")) && belle_sip_header_to_get_tag(toHeader)) {
			lWarning() << "Receiving " << method << " with to-tag but no know dialog here, rejecting";
			auto response = belle_sip_response_create_from_request(request, 481);
			belle_sip_provider_send_response(sal->mProvider, response);
86
			return;
Ghislain MARY's avatar
Ghislain MARY committed
87 88 89 90
		// By default (eg. when a to-tag is present), out of dialog ACK are automatically
		// handled in lower layers (belle-sip) but in case it misses, it will be forwarded to us
		} else if ((method == "ACK") && !belle_sip_header_to_get_tag(toHeader)) {
			lWarning() << "Receiving ACK without to-tag but no know dialog here, ignoring";
91 92 93
			return;
		}

Ghislain MARY's avatar
Ghislain MARY committed
94 95
		if (method == "INVITE") {
			op = new SalCallOp(sal);
96
			op->fillCallbacks();
Ghislain MARY's avatar
Ghislain MARY committed
97 98 99 100 101 102
		} else if (((method == "SUBSCRIBE") || (method == "NOTIFY")) && (evh = belle_sip_message_get_header(BELLE_SIP_MESSAGE(request), "Event")) != nullptr) {
			string eventHeaderValue = belle_sip_header_get_unparsed_value(evh);
			if (eventHeaderValue == "presence")
				op = new SalPresenceOp(sal);
			else
				op = new SalSubscribeOp(sal);
103
			op->fillCallbacks();
Ghislain MARY's avatar
Ghislain MARY committed
104 105
		} else if (method == "MESSAGE") {
			op = new SalMessageOp(sal);
106
			op->fillCallbacks();
Ghislain MARY's avatar
Ghislain MARY committed
107 108 109 110 111
		} else if (method == "REFER") {
			op = new SalReferOp(sal);
		} else if (method == "OPTIONS") {
			auto response = belle_sip_response_create_from_request(request, 200);
			belle_sip_provider_send_response(sal->mProvider, response);
112
			return;
Ghislain MARY's avatar
Ghislain MARY committed
113 114 115
		} else if (method == "INFO") { // INFO out of call dialogs are not allowed
			auto response = belle_sip_response_create_from_request(request, 481);
			belle_sip_provider_send_response(sal->mProvider, response);
116
			return;
Ghislain MARY's avatar
Ghislain MARY committed
117 118 119
		} else if (method == "BYE") { // Out of dialog BYE
			auto response = belle_sip_response_create_from_request(request, 481);
			belle_sip_provider_send_response(sal->mProvider, response);
120
			return;
Ghislain MARY's avatar
Ghislain MARY committed
121 122 123
		} else if (method == "CANCEL") { // Out of dialog CANCEL
			auto response = belle_sip_response_create_from_request(request, 481);
			belle_sip_provider_send_response(sal->mProvider, response);
124
			return;
Ghislain MARY's avatar
Ghislain MARY committed
125 126 127 128
		} else if (sal->mEnableTestFeatures && (method == "PUBLISH")) { // Out of dialog PUBLISH
			auto response = belle_sip_response_create_from_request(request, 200);
			belle_sip_message_add_header(BELLE_SIP_MESSAGE(response), belle_sip_header_create("SIP-Etag", "4441929FFFZQOA"));
			belle_sip_provider_send_response(sal->mProvider, response);
129
			return;
Ghislain MARY's avatar
Ghislain MARY committed
130 131 132 133 134 135 136 137
		} else {
			lError() << "Sal::processRequestEventCb(): not implemented yet for method [" << method << "]";
			auto response = belle_sip_response_create_from_request(request, 405);
			belle_sip_message_add_header(
				BELLE_SIP_MESSAGE(response),
				BELLE_SIP_HEADER(belle_sip_header_allow_create("INVITE, CANCEL, ACK, BYE, SUBSCRIBE, NOTIFY, MESSAGE, OPTIONS, INFO"))
			);
			belle_sip_provider_send_response(sal->mProvider, response);
138 139
			return;
		}
Ghislain MARY's avatar
Ghislain MARY committed
140
		op->mDir = SalOp::Dir::Incoming;
141 142
	}

Ghislain MARY's avatar
Ghislain MARY committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	if (!op->mFromAddress) {
		auto fromHeader = belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(request), belle_sip_header_from_t);
		belle_sip_header_address_t *addressHeader = nullptr;
		auto uri = belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(fromHeader));
		auto absoluteUri = belle_sip_header_address_get_absolute_uri(BELLE_SIP_HEADER_ADDRESS(fromHeader));
		if (uri) {
			addressHeader = belle_sip_header_address_create(
				belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(fromHeader)),
				uri
			);
		} else if (absoluteUri) {
			addressHeader = belle_sip_header_address_create2(
				belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(fromHeader)),
				absoluteUri
			);
		} else {
			lError() << "Cannot find from uri from request [" << request << "]";
		}
		if (addressHeader) {
			op->setFromAddress(reinterpret_cast<SalAddress *>(addressHeader));
			belle_sip_object_unref(addressHeader);
		}
165 166
	}

Ghislain MARY's avatar
Ghislain MARY committed
167 168 169
	auto remoteContactHeader = belle_sip_message_get_header_by_type(request, belle_sip_header_contact_t);
	if (remoteContactHeader)
		op->setRemoteContact(belle_sip_header_get_unparsed_value(BELLE_SIP_HEADER(remoteContactHeader)));
170

171
	if (!op->mToAddress) {
Ghislain MARY's avatar
Ghislain MARY committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		auto toHeader = belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(request), belle_sip_header_to_t);
		belle_sip_header_address_t *addressHeader = nullptr;
		auto uri = belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(toHeader));
		auto absoluteUri = belle_sip_header_address_get_absolute_uri(BELLE_SIP_HEADER_ADDRESS(toHeader));
		if (uri) {
			addressHeader = belle_sip_header_address_create(
				belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(toHeader)),
				uri
			);
		} else if (absoluteUri) {
			addressHeader = belle_sip_header_address_create2(
				belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(toHeader)),
				absoluteUri
			);
		} else {
			lError() << "Cannot find to uri from request [" << request << "]";
		}
		if (addressHeader) {
			op->setToAddress(reinterpret_cast<SalAddress *>(addressHeader));
			belle_sip_object_unref(addressHeader);
		}
193 194
	}

Ghislain MARY's avatar
Ghislain MARY committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	auto subjectHeader = belle_sip_message_get_header(BELLE_SIP_MESSAGE(request), "Subject");
	if (subjectHeader)
		op->setSubject(belle_sip_header_get_unparsed_value(subjectHeader));

	if(!op->mDiversionAddress) {
		auto diversionHeader = belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(request), belle_sip_header_diversion_t);
		if (diversionHeader) {
			belle_sip_header_address_t *addressHeader = nullptr;
			auto uri = belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(diversionHeader));
			auto absoluteUri = belle_sip_header_address_get_absolute_uri(BELLE_SIP_HEADER_ADDRESS(diversionHeader));
			if (uri) {
				addressHeader = belle_sip_header_address_create(
					belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(diversionHeader)),
					uri
				);
			} else if (absoluteUri) {
				addressHeader = belle_sip_header_address_create2(
					belle_sip_header_address_get_displayname(BELLE_SIP_HEADER_ADDRESS(diversionHeader)),
					absoluteUri
				);
			} else {
				lWarning() << "Cannot not find diversion header from request [" << request << "]";
			}
			if (addressHeader) {
				op->setDiversionAddress(reinterpret_cast<SalAddress *>(addressHeader));
				belle_sip_object_unref(addressHeader);
221 222 223 224
			}
		}
	}

225
	if (op->mOrigin.empty()) {
Ghislain MARY's avatar
Ghislain MARY committed
226 227 228 229
		// Set origin uri
		auto originAddressHeader = belle_sip_header_address_create(nullptr, belle_sip_request_extract_origin(request));
		op->setNetworkOriginAddress(reinterpret_cast<SalAddress *>(originAddressHeader));
		belle_sip_object_unref(originAddressHeader);
230
	}
231
	if (op->mRemoteUserAgent.empty())
Ghislain MARY's avatar
Ghislain MARY committed
232
		op->setRemoteUserAgent(BELLE_SIP_MESSAGE(request));
233

234 235
	if (op->mCallId.empty()) {
		op->mCallId = belle_sip_header_call_id_get_call_id(
Ghislain MARY's avatar
Ghislain MARY committed
236
			BELLE_SIP_HEADER_CALL_ID(belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(request), belle_sip_header_call_id_t))
237
		);
238
	}
239

Ghislain MARY's avatar
Ghislain MARY committed
240 241
	// It is worth noting that proxies can (and will) remove this header field
	op->setPrivacyFromMessage(BELLE_SIP_MESSAGE(request));
242

Ghislain MARY's avatar
Ghislain MARY committed
243 244
	if (method != "ACK") // The ACK custom header is processed specifically later on
		op->assignRecvHeaders(BELLE_SIP_MESSAGE(request));
245

Ghislain MARY's avatar
Ghislain MARY committed
246 247 248 249
	if (op->mCallbacks && op->mCallbacks->process_request_event)
		op->mCallbacks->process_request_event(op, event);
	else
		lError() << "Sal::processRequestEventCb(): not implemented yet";
250 251
}

Ghislain MARY's avatar
Ghislain MARY committed
252 253 254
void Sal::processResponseEventCb (void *userCtx, const belle_sip_response_event_t *event) {
	auto response = belle_sip_response_event_get_response(event);
	int responseCode = belle_sip_response_get_status_code(response);
255

Ghislain MARY's avatar
Ghislain MARY committed
256 257 258
	auto clientTransaction = belle_sip_response_event_get_client_transaction(event);
	if (!clientTransaction) {
		lWarning() << "Discarding stateless response [" << responseCode << "]";
259
		return;
Ghislain MARY's avatar
Ghislain MARY committed
260
	}
261

Ghislain MARY's avatar
Ghislain MARY committed
262 263 264 265 266
	auto op = static_cast<SalOp *>(belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(clientTransaction)));
	if (op->mState == SalOp::State::Terminated) {
		lInfo() << "Op [" << op << "] is terminated, nothing to do with this [" << responseCode << "]";
		return;
	}
267

Ghislain MARY's avatar
Ghislain MARY committed
268 269
	// Do it all the time, since we can receive provisional responses from a different instance than the final one
	op->setRemoteUserAgent(BELLE_SIP_MESSAGE(response));
270

Ghislain MARY's avatar
Ghislain MARY committed
271 272 273
	auto remoteContactHeader = belle_sip_message_get_header_by_type(response, belle_sip_header_contact_t);
	if (remoteContactHeader)
		op->setRemoteContact(belle_sip_header_get_unparsed_value(BELLE_SIP_HEADER(remoteContactHeader)));
274

Ghislain MARY's avatar
Ghislain MARY committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if (op->mCallId.empty()) {
		op->mCallId = belle_sip_header_call_id_get_call_id(
			BELLE_SIP_HEADER_CALL_ID(belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(response), belle_sip_header_call_id_t))
		);
	}

	op->assignRecvHeaders(BELLE_SIP_MESSAGE(response));

	if (op->mCallbacks && op->mCallbacks->process_response_event) {
		// Handle authorization
		auto request = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(clientTransaction));
		string method = belle_sip_request_get_method(request);
		switch (responseCode) {
			case 200:
				break;
			case 401:
			case 407:
				if ((op->mState == SalOp::State::Terminating) && (method != "BYE")) {
					// Only bye are completed
					lInfo() << "Op is in state terminating, nothing else to do";
				} else {
					if (op->mPendingAuthTransaction) {
						belle_sip_object_unref(op->mPendingAuthTransaction);
						op->mPendingAuthTransaction = nullptr;
299
					}
Ghislain MARY's avatar
Ghislain MARY committed
300 301 302 303 304 305 306
					if (++op->mAuthRequests > 2) {
						lWarning() << "Auth info cannot be found for op [" << op->getFrom() << "/" << op->getTo() << "] after 2 attempts, giving up";
						op->mRoot->mCallbacks.auth_failure(op, op->mAuthInfo);
						op->mRoot->removePendingAuth(op);
					} else {
						op->mPendingAuthTransaction = BELLE_SIP_CLIENT_TRANSACTION(belle_sip_object_ref(clientTransaction));
						op->processAuthentication();
307
						return;
Ghislain MARY's avatar
Ghislain MARY committed
308 309 310 311 312 313 314 315 316 317 318 319
					}
				}
				break;
			case 403:
				if (op->mAuthInfo)
					op->mRoot->mCallbacks.auth_failure(op, op->mAuthInfo);
				break;
			case 302:
			case 301:
				if (op->processRedirect() == 0)
					return;
				break;
320
		}
Ghislain MARY's avatar
Ghislain MARY committed
321 322 323 324 325
		if ((responseCode >= 180) && (responseCode != 401) && (responseCode != 407) && (responseCode != 403)) // Not an auth request
			op->mAuthRequests = 0;
		op->mCallbacks->process_response_event(op, event);
	} else {
		lError() << "Unhandled event response [" << event << "]";
326 327 328
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
329 330 331 332 333 334 335
void Sal::processTimeoutCb (void *userCtx, const belle_sip_timeout_event_t *event) {
	auto clientTransaction = belle_sip_timeout_event_get_client_transaction(event);
	auto op = static_cast<SalOp *>(belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(clientTransaction)));
	if (op && op->mCallbacks && op->mCallbacks->process_timeout)
		op->mCallbacks->process_timeout(op, event);
	else
		lError() << "Unhandled event timeout [" << event << "]";
336 337
}

Ghislain MARY's avatar
Ghislain MARY committed
338 339 340 341 342
void Sal::processTransactionTerminatedCb (void *userCtx, const belle_sip_transaction_terminated_event_t *event) {
	auto clientTransaction = belle_sip_transaction_terminated_event_get_client_transaction(event);
	auto serverTransaction = belle_sip_transaction_terminated_event_get_server_transaction(event);
	auto transaction = clientTransaction ? BELLE_SIP_TRANSACTION(clientTransaction) : BELLE_SIP_TRANSACTION(serverTransaction);
	auto op = static_cast<SalOp *>(belle_sip_transaction_get_application_data(transaction));
343

Ghislain MARY's avatar
Ghislain MARY committed
344 345 346 347
	if (op && op->mCallbacks && op->mCallbacks->process_transaction_terminated)
		op->mCallbacks->process_transaction_terminated(op, event);
	else
		lInfo() << "Unhandled transaction terminated [" << transaction << "]";
348 349

	if (op) {
Ghislain MARY's avatar
Ghislain MARY committed
350 351
		op->unref(); // Because every transaction ref op
		belle_sip_transaction_set_application_data(transaction, nullptr); // No longer reference something we do not ref to avoid future access of a released op
352 353 354
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
355 356 357 358 359 360 361 362 363 364
void Sal::processAuthRequestedCb (void *userCtx, belle_sip_auth_event_t *event) {
	auto sal = static_cast<Sal *>(userCtx);
	SalAuthInfo *authInfo = sal_auth_info_create(event);
	sal->mCallbacks.auth_requested(sal, authInfo);
	belle_sip_auth_event_set_passwd(event, (const char *)authInfo->password);
	belle_sip_auth_event_set_ha1(event, (const char *)authInfo->ha1);
	belle_sip_auth_event_set_userid(event, (const char *)authInfo->userid);
	belle_sip_auth_event_set_signing_key(event, (belle_sip_signing_key_t *)authInfo->key);
	belle_sip_auth_event_set_client_certificates_chain(event, (belle_sip_certificates_chain_t *)authInfo->certificates);
	sal_auth_info_delete(authInfo);
365 366
}

Ghislain MARY's avatar
Ghislain MARY committed
367 368 369
Sal::Sal (MSFactory *factory) : mFactory(factory) {
	// First create the stack, which initializes the belle-sip object's pool for this thread
	mStack = belle_sip_stack_new(nullptr);
370

Ghislain MARY's avatar
Ghislain MARY committed
371
	mUserAgentHeader = belle_sip_header_user_agent_new();
372
#if defined(PACKAGE_NAME) && defined(LIBLINPHONE_VERSION)
Ghislain MARY's avatar
Ghislain MARY committed
373
	belle_sip_header_user_agent_add_product(mUserAgentHeader, PACKAGE_NAME "/" LIBLINPHONE_VERSION);
374
#else
375
	belle_sip_header_user_agent_add_product(mUserAgentHeader, "Unknown");
376
#endif
Ghislain MARY's avatar
Ghislain MARY committed
377
	appendStackStringToUserAgent();
378
	belle_sip_object_ref(mUserAgentHeader);
379

Ghislain MARY's avatar
Ghislain MARY committed
380 381 382 383 384 385 386 387 388 389 390 391
	mProvider = belle_sip_stack_create_provider(mStack, nullptr);
	enableNatHelper(true);

	belle_sip_listener_callbacks_t listenerCallbacks = { 0 };
	listenerCallbacks.process_dialog_terminated = processDialogTerminatedCb;
	listenerCallbacks.process_io_error = processIoErrorCb;
	listenerCallbacks.process_request_event = processRequestEventCb;
	listenerCallbacks.process_response_event = processResponseEventCb;
	listenerCallbacks.process_timeout = processTimeoutCb;
	listenerCallbacks.process_transaction_terminated = processTransactionTerminatedCb;
	listenerCallbacks.process_auth_requested = processAuthRequestedCb;
	mListener = belle_sip_listener_create_from_callbacks(&listenerCallbacks, this);
392
	belle_sip_provider_add_sip_listener(mProvider, mListener);
393 394
}

Ghislain MARY's avatar
Ghislain MARY committed
395
Sal::~Sal () {
396
	belle_sip_object_unref(mUserAgentHeader);
397 398 399
	belle_sip_object_unref(mProvider);
	belle_sip_object_unref(mStack);
	belle_sip_object_unref(mListener);
400 401
	if (mSupportedHeader)
		belle_sip_object_unref(mSupportedHeader);
402 403
}

Ghislain MARY's avatar
Ghislain MARY committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
void Sal::setCallbacks (const Callbacks *cbs) {
	memcpy(&mCallbacks, cbs, sizeof(*cbs));
	if (!mCallbacks.call_received)
		mCallbacks.call_received = (OnCallReceivedCb)unimplementedStub;
	if (!mCallbacks.call_ringing)
		mCallbacks.call_ringing = (OnCallRingingCb)unimplementedStub;
	if (!mCallbacks.call_accepted)
		mCallbacks.call_accepted = (OnCallAcceptedCb)unimplementedStub;
	if (!mCallbacks.call_failure)
		mCallbacks.call_failure = (OnCallFailureCb)unimplementedStub;
	if (!mCallbacks.call_terminated)
		mCallbacks.call_terminated = (OnCallTerminatedCb)unimplementedStub;
	if (!mCallbacks.call_released)
		mCallbacks.call_released = (OnCallReleasedCb)unimplementedStub;
	if (!mCallbacks.call_updating)
		mCallbacks.call_updating = (OnCallUpdatingCb)unimplementedStub;
	if (!mCallbacks.auth_failure)
		mCallbacks.auth_failure = (OnAuthFailureCb)unimplementedStub;
	if (!mCallbacks.register_success)
		mCallbacks.register_success = (OnRegisterSuccessCb)unimplementedStub;
	if (!mCallbacks.register_failure)
		mCallbacks.register_failure = (OnRegisterFailureCb)unimplementedStub;
	if (!mCallbacks.dtmf_received)
		mCallbacks.dtmf_received = (OnDtmfReceivedCb)unimplementedStub;
	if (!mCallbacks.notify)
		mCallbacks.notify = (OnNotifyCb)unimplementedStub;
	if (!mCallbacks.subscribe_received)
		mCallbacks.subscribe_received = (OnSubscribeReceivedCb)unimplementedStub;
	if (!mCallbacks.incoming_subscribe_closed)
		mCallbacks.incoming_subscribe_closed = (OnIncomingSubscribeClosedCb)unimplementedStub;
	if (!mCallbacks.parse_presence_requested)
		mCallbacks.parse_presence_requested = (OnParsePresenceRequestedCb)unimplementedStub;
	if (!mCallbacks.convert_presence_to_xml_requested)
		mCallbacks.convert_presence_to_xml_requested = (OnConvertPresenceToXMLRequestedCb)unimplementedStub;
	if (!mCallbacks.notify_presence)
		mCallbacks.notify_presence = (OnNotifyPresenceCb)unimplementedStub;
	if (!mCallbacks.subscribe_presence_received)
		mCallbacks.subscribe_presence_received = (OnSubscribePresenceReceivedCb)unimplementedStub;
	if (!mCallbacks.message_received)
		mCallbacks.message_received = (OnMessageReceivedCb)unimplementedStub;
	if (!mCallbacks.ping_reply)
		mCallbacks.ping_reply = (OnPingReplyCb)unimplementedStub;
	if (!mCallbacks.auth_requested)
		mCallbacks.auth_requested = (OnAuthRequestedCb)unimplementedStub;
	if (!mCallbacks.info_received)
		mCallbacks.info_received = (OnInfoReceivedCb)unimplementedStub;
	if (!mCallbacks.on_publish_response)
		mCallbacks.on_publish_response = (OnPublishResponseCb)unimplementedStub;
	if (!mCallbacks.on_expire)
		mCallbacks.on_expire = (OnExpireCb)unimplementedStub;
}

void Sal::setTlsProperties () {
	auto listeningPoint = belle_sip_provider_get_listening_point(mProvider, "TLS");
	if (!listeningPoint)
		return;
460

Ghislain MARY's avatar
Ghislain MARY committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	auto cryptoConfig = belle_tls_crypto_config_new();
	int verifyExceptions = BELLE_TLS_VERIFY_NONE;
	if (!mTlsVerify)
		verifyExceptions = BELLE_TLS_VERIFY_ANY_REASON;
	else if (!mTlsVerifyCn)
		verifyExceptions = BELLE_TLS_VERIFY_CN_MISMATCH;
	belle_tls_crypto_config_set_verify_exceptions(cryptoConfig, verifyExceptions);

	if (!mRootCa.empty())
		belle_tls_crypto_config_set_root_ca(cryptoConfig, mRootCa.c_str());
	if (!mRootCaData.empty())
		belle_tls_crypto_config_set_root_ca_data(cryptoConfig, mRootCaData.c_str());
	if (mSslConfig)
		belle_tls_crypto_config_set_ssl_config(cryptoConfig, mSslConfig);
	if (mTlsPostcheckCb)
		belle_tls_crypto_config_set_postcheck_callback(cryptoConfig, mTlsPostcheckCb, mTlsPostcheckCbData);
	auto tlsListeningPoint = BELLE_SIP_TLS_LISTENING_POINT(listeningPoint);
	belle_sip_tls_listening_point_set_crypto_config(tlsListeningPoint, cryptoConfig);
	belle_sip_object_unref(cryptoConfig);
}

int Sal::addListenPort (SalAddress *addr, bool isTunneled) {
	belle_sip_listening_point_t *listeningPoint;

	if (isTunneled) {
486
#ifdef TUNNEL_ENABLED
Ghislain MARY's avatar
Ghislain MARY committed
487 488
		if (sal_address_get_transport(addr) != SalTransportUDP) {
			lError() << "Tunneled mode is only available for UDP kind of transports";
489 490
			return -1;
		}
Ghislain MARY's avatar
Ghislain MARY committed
491 492 493
		listeningPoint = belle_sip_tunnel_listening_point_new(mStack, mTunnelClient);
		if (!listeningPoint) {
			lError() << "Could not create tunnel listening point";
494 495 496
			return -1;
		}
#else
Ghislain MARY's avatar
Ghislain MARY committed
497
		lError() << "No tunnel support in library";
498 499 500
		return -1;
#endif
	} else {
Ghislain MARY's avatar
Ghislain MARY committed
501 502 503 504 505 506
		listeningPoint = belle_sip_stack_create_listening_point(
			mStack,
			sal_address_get_domain(addr),
			sal_address_get_port(addr),
			sal_transport_to_string(sal_address_get_transport(addr))
		);
507
	}
Ghislain MARY's avatar
Ghislain MARY committed
508 509 510 511 512 513 514 515

	if (!listeningPoint)
		return -1;

	belle_sip_listening_point_set_keep_alive(listeningPoint, (int)mKeepAlive);
	int result = belle_sip_provider_add_listening_point(mProvider, listeningPoint);
	if (sal_address_get_transport(addr) == SalTransportTLS)
		setTlsProperties();
516 517 518
	return result;
}

519 520 521 522 523 524
int Sal::setListenPort (const string &addr, int port, SalTransport tr, bool isTunneled) {
	SalAddress *salAddr = sal_address_new(nullptr);
	sal_address_set_domain(salAddr, L_STRING_TO_C(addr));
	sal_address_set_port(salAddr, port);
	sal_address_set_transport(salAddr, tr);
	int result = addListenPort(salAddr, isTunneled);
Ghislain MARY's avatar
Ghislain MARY committed
525
	sal_address_unref(salAddr);
526 527 528
	return result;
}

Ghislain MARY's avatar
Ghislain MARY committed
529 530 531 532 533
int Sal::getListeningPort (SalTransport tr) {
	const char *tpn = sal_transport_to_string(tr);
	auto listeningPoint = belle_sip_provider_get_listening_point(mProvider, tpn);
	if (listeningPoint)
		return belle_sip_listening_point_get_port(listeningPoint);
534 535 536
	return 0;
}

Ghislain MARY's avatar
Ghislain MARY committed
537 538 539 540 541 542
void Sal::unlistenPorts () {
	auto listeningPoints = belle_sip_provider_get_listening_points(mProvider);
	auto listeningPointsCopy = belle_sip_list_copy(listeningPoints);
	belle_sip_list_for_each2(listeningPointsCopy, (bctbx_list_iterate2_func)removeListeningPoint, mProvider);
	belle_sip_list_free(listeningPointsCopy);
	lInfo() << "Sal::unlistenPorts(): done";
543 544
}

Ghislain MARY's avatar
Ghislain MARY committed
545 546
bool Sal::isTransportAvailable (SalTransport tr) {
	switch (tr) {
547 548
		case SalTransportUDP:
		case SalTransportTCP:
Ghislain MARY's avatar
Ghislain MARY committed
549
			return true;
550
		case SalTransportTLS:
551
			return !!belle_sip_stack_tls_available(mStack);
552
		case SalTransportDTLS:
Ghislain MARY's avatar
Ghislain MARY committed
553 554
		default:
			return false;
555 556 557
	}
}

558 559 560 561
void Sal::makeSupportedHeader () {
	if (mSupportedHeader) {
		belle_sip_object_unref(mSupportedHeader);
		mSupportedHeader = nullptr;
562
	}
563 564 565 566 567 568
	string tags = Utils::join(mSupportedTags, ", ");
	if (tags.empty())
		return;
	mSupportedHeader = belle_sip_header_create("Supported", tags.c_str());
	if (mSupportedHeader)
		belle_sip_object_ref(mSupportedHeader);
569 570
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584
void Sal::setSupportedTags (const string &tags) {
	vector<string> splittedTags = Utils::split(tags, ",");
	mSupportedTags.clear();
	for (const auto &tag : splittedTags)
		mSupportedTags.push_back(Utils::trim(tag));
	makeSupportedHeader();
}

const string &Sal::getSupportedTags () const {
	if (mSupportedHeader)
		mSupported = belle_sip_header_get_unparsed_value(mSupportedHeader);
	else
		mSupported.clear();
	return mSupported;
585 586
}

587 588 589 590 591
void Sal::addSupportedTag (const string &tag) {
	auto it = find(mSupportedTags.cbegin(), mSupportedTags.cend(), tag);
	if (it == mSupportedTags.cend()) {
		mSupportedTags.push_back(tag);
		makeSupportedHeader();
592 593 594
	}
}

595
void Sal::removeSupportedTag (const string &tag) {
Ghislain MARY's avatar
Ghislain MARY committed
596 597
	auto it = find(mSupportedTags.begin(), mSupportedTags.end(), tag);
	if (it != mSupportedTags.end()) {
598 599
		mSupportedTags.erase(it);
		makeSupportedHeader();
600 601 602
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
603 604
void Sal::resetTransports () {
	lInfo() << "Reseting transports";
605
	belle_sip_provider_clean_channels(mProvider);
606 607
}

608 609 610
void Sal::setUserAgent (const string &value) {
	belle_sip_header_user_agent_set_products(mUserAgentHeader, nullptr);
	belle_sip_header_user_agent_add_product(mUserAgentHeader, L_STRING_TO_C(value));
611 612
}

613 614
const string &Sal::getUserAgent () const {
	char userAgent[256];
Ghislain MARY's avatar
Ghislain MARY committed
615
	belle_sip_header_user_agent_get_products_as_string(mUserAgentHeader, userAgent, (unsigned int)sizeof(userAgent) - 1);
616 617
	mUserAgent = userAgent;
	return mUserAgent;
618 619
}

620 621 622 623 624
void Sal::appendStackStringToUserAgent () {
	stringstream ss;
	ss << "(belle-sip/" << belle_sip_version_to_string() << ")";
	string stackStr = ss.str();
	belle_sip_header_user_agent_add_product(mUserAgentHeader, stackStr.c_str());
625 626
}

Ghislain MARY's avatar
Ghislain MARY committed
627 628 629 630 631 632 633 634
void Sal::setKeepAlivePeriod (unsigned int value) {
	mKeepAlive = value;
	for (auto it = belle_sip_provider_get_listening_points(mProvider); it; it = bctbx_list_next(it)) {
		auto listeningPoint = static_cast<belle_sip_listening_point_t *>(bctbx_list_get_data(it));
		if (mUseTcpTlsKeepAlive
			|| (strcasecmp(belle_sip_listening_point_get_transport(listeningPoint), "udp") == 0)
		) {
			belle_sip_listening_point_set_keep_alive(listeningPoint, (int)mKeepAlive);
635 636 637 638
		}
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
639
int Sal::setTunnel (void *tunnelclient) {
640
#ifdef TUNNEL_ENABLED
Ghislain MARY's avatar
Ghislain MARY committed
641
	mTunnelClient = tunnelclient;
642 643 644 645 646 647
	return 0;
#else
	return -1;
#endif
}

648 649
void Sal::setHttpProxyHost (const string &value) {
	belle_sip_stack_set_http_proxy_host(mStack, L_STRING_TO_C(value));
650 651
}

652 653 654
const string &Sal::getHttpProxyHost () const {
	mHttpProxyHost = belle_sip_stack_get_http_proxy_host(mStack);
	return mHttpProxyHost;
655 656
}

657
bool Sal::isContentEncodingAvailable (const string &contentEncoding) const {
658
	return !!belle_sip_stack_content_encoding_available(mStack, L_STRING_TO_C(contentEncoding));
659 660 661 662
}

bool Sal::isContentTypeSupported (const string &contentType) const {
	auto it = find_if(mSupportedContentTypes.cbegin(), mSupportedContentTypes.cend(),
Ghislain MARY's avatar
Ghislain MARY committed
663 664 665 666
		[contentType](string supportedContentType) {
			return contentType == supportedContentType;
		}
	);
667 668 669 670 671 672 673 674 675 676 677 678
	return it != mSupportedContentTypes.cend();
}

void Sal::addContentTypeSupport (const string &contentType) {
	if (!contentType.empty() && !isContentTypeSupported(contentType))
		mSupportedContentTypes.push_back(contentType);
}

void Sal::removeContentTypeSupport (const string &contentType) {
	auto it = find(mSupportedContentTypes.begin(), mSupportedContentTypes.end(), contentType);
	if (it != mSupportedContentTypes.end())
		mSupportedContentTypes.erase(it);
Sylvain Berfini's avatar
Sylvain Berfini committed
679 680
}

Ghislain MARY's avatar
Ghislain MARY committed
681 682 683
void Sal::useRport (bool useRports) {
	belle_sip_provider_enable_rport(mProvider, useRports);
	lInfo() << "Sal use rports [" << (useRports ? "enabled" : "disabled") << "]";
684 685
}

686 687
void Sal::setRootCa (const string &value) {
	mRootCa = value;
688
	setTlsProperties();
689 690
}

691 692
void Sal::setRootCaData (const string &value) {
	mRootCaData = value;
693
	setTlsProperties();
694 695
}

Ghislain MARY's avatar
Ghislain MARY committed
696 697
void Sal::verifyServerCertificates (bool verify) {
	mTlsVerify = verify;
698
	setTlsProperties();
699 700
}

Ghislain MARY's avatar
Ghislain MARY committed
701
void Sal::verifyServerCn (bool verify) {
702 703
	mTlsVerifyCn = verify;
	setTlsProperties();
704 705
}

Ghislain MARY's avatar
Ghislain MARY committed
706 707
void Sal::setSslConfig (void *sslConfig) {
	mSslConfig = sslConfig;
708
	setTlsProperties();
709 710
}

Ghislain MARY's avatar
Ghislain MARY committed
711
void Sal::setTlsPostcheckCallback (int (*cb)(void *, const bctbx_x509_certificate_t *), void *data) {
712 713 714 715
	mTlsPostcheckCb = cb;
	mTlsPostcheckCbData = data;
}

Ghislain MARY's avatar
Ghislain MARY committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
string Sal::createUuid () {
	setUuid(generateUuid());
	return mUuid;
}

string Sal::generateUuid () {
	// Create an UUID as described in RFC4122, 4.4
	SalUuid uuidStruct;
	belle_sip_random_bytes((unsigned char *)&uuidStruct, sizeof(uuidStruct));
	uuidStruct.clockSeqHiAndReserved &= (unsigned char)~(1 << 6);
	uuidStruct.clockSeqHiAndReserved |= (unsigned char)(1 << 7);
	uuidStruct.timeHiAndVersion &= (unsigned char)~(0xf << 12);
	uuidStruct.timeHiAndVersion |= (unsigned char)(4 << 12);

	char generatedUuid[128] = { 0 };
	int written = snprintf(
		generatedUuid,
		sizeof(generatedUuid) - 1,
		"%8.8x-%4.4x-%4.4x-%2.2x%2.2x-",
		uuidStruct.timeLow,
		uuidStruct.timeMid,
		uuidStruct.timeHiAndVersion,
		uuidStruct.clockSeqHiAndReserved,
		uuidStruct.clockSeqLow
	);
	for (int i = 0; i < 6; i++)
		written += snprintf(generatedUuid + written, sizeof(generatedUuid) - (unsigned long)written, "%2.2x", uuidStruct.node[i]);
	return generatedUuid;
744 745
}

746 747 748 749 750
void Sal::addPendingAuth (SalOp *op) {
	auto it = find(mPendingAuths.cbegin(), mPendingAuths.cend(), op);
	if (it == mPendingAuths.cend()) {
		mPendingAuths.push_back(op);
		op->mHasAuthPending = true;
751 752 753
	}
}

754 755 756 757
void Sal::removePendingAuth (SalOp *op) {
	if (op->mHasAuthPending) {
		op->mHasAuthPending = false;
		mPendingAuths.remove(op);
758 759 760
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
761 762 763 764
void Sal::setDefaultSdpHandling (SalOpSDPHandling sdpHandlingMethod) {
	if (sdpHandlingMethod != SalOpSDPNormal)
		lInfo() << "Enabling special SDP handling for all new SalOp in Sal[" << this << "]!";
	mDefaultSdpHandling = sdpHandlingMethod;
765 766
}

Ghislain MARY's avatar
Ghislain MARY committed
767 768 769 770
void Sal::enableNatHelper (bool enable) {
	mNatHelperEnabled = enable;
	belle_sip_provider_enable_nat_helper(mProvider, enable);
	lInfo() << "Sal nat helper [" << (enable ? "enabled" : "disabled") << "]";
771 772
}

Ghislain MARY's avatar
Ghislain MARY committed
773 774
void Sal::setDnsServers (const bctbx_list_t *servers) {
	belle_sip_stack_set_dns_servers(mStack, servers);
775 776
}

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
void Sal::setDnsUserHostsFile (const string &value) {
	belle_sip_stack_set_dns_user_hosts_file(mStack, value.c_str());
}

const string &Sal::getDnsUserHostsFile () const {
	mDnsUserHostsFile = belle_sip_stack_get_dns_user_hosts_file(mStack);
	return mDnsUserHostsFile;
}

belle_sip_resolver_context_t *Sal::resolveA (const string &name, int port, int family, belle_sip_resolver_callback_t cb, void *data) {
	return belle_sip_stack_resolve_a(mStack, L_STRING_TO_C(name), port, family, cb, data);
}

belle_sip_resolver_context_t *Sal::resolve (const string &service, const string &transport, const string &name, int port, int family, belle_sip_resolver_callback_t cb, void *data) {
	return belle_sip_stack_resolve(mStack, L_STRING_TO_C(service), L_STRING_TO_C(transport), L_STRING_TO_C(name), port, family, cb, data);
}

belle_sip_source_t *Sal::createTimer (belle_sip_source_func_t func, void *data, unsigned int timeoutValueMs, const string &timerName) {
795
	belle_sip_main_loop_t *ml = belle_sip_stack_get_main_loop(mStack);
796
	return belle_sip_main_loop_create_timeout(ml, func, data, timeoutValueMs, L_STRING_TO_C(timerName));
797 798
}

799 800
void Sal::cancelTimer(belle_sip_source_t *timer) {
	belle_sip_main_loop_t *ml = belle_sip_stack_get_main_loop(mStack);
801 802 803
	belle_sip_main_loop_remove_source(ml, timer);
}

Ghislain MARY's avatar
Ghislain MARY committed
804 805 806 807 808
belle_sip_response_t *Sal::createResponseFromRequest (belle_sip_request_t *request, int code) {
	auto response = belle_sip_response_create_from_request(request, code);
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(response), BELLE_SIP_HEADER(mUserAgentHeader));
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(response), mSupportedHeader);
	return response;
809 810
}

811 812 813 814 815 816 817
int Sal::findCryptoIndexFromTag (const SalSrtpCryptoAlgo crypto[], unsigned char tag) {
	for (int i = 0; i < SAL_CRYPTO_ALGO_MAX; i++) {
		if (crypto[i].tag == tag)
			return i;
	}
	return -1;
}
818 819


Ghislain MARY's avatar
Ghislain MARY committed
820 821 822
//***********************************
// Global functions implementation
//***********************************
823

Ghislain MARY's avatar
Ghislain MARY committed
824 825
int toSipCode (SalReason reason) {
	switch (reason) {
826
		case SalReasonNone:
Ghislain MARY's avatar
Ghislain MARY committed
827
			return 200;
828
		case SalReasonIOError:
Ghislain MARY's avatar
Ghislain MARY committed
829
			return 503;
830
		case SalReasonUnknown:
Ghislain MARY's avatar
Ghislain MARY committed
831
			return 400;
832
		case SalReasonBusy:
Ghislain MARY's avatar
Ghislain MARY committed
833
			return 486;
834
		case SalReasonDeclined:
Ghislain MARY's avatar
Ghislain MARY committed
835
			return 603;
836
		case SalReasonDoNotDisturb:
Ghislain MARY's avatar
Ghislain MARY committed
837
			return 600;
838
		case SalReasonForbidden:
Ghislain MARY's avatar
Ghislain MARY committed
839
			return 403;
840
		case SalReasonUnsupportedContent:
Ghislain MARY's avatar
Ghislain MARY committed
841
			return 415;
842
		case SalReasonNotFound:
Ghislain MARY's avatar
Ghislain MARY committed
843
			return 404;
844
		case SalReasonRedirect:
Ghislain MARY's avatar
Ghislain MARY committed
845
			return 302;
846
		case SalReasonTemporarilyUnavailable:
Ghislain MARY's avatar
Ghislain MARY committed
847
			return 480;
848
		case SalReasonServiceUnavailable:
Ghislain MARY's avatar
Ghislain MARY committed
849
			return 503;
850
		case SalReasonRequestPending:
Ghislain MARY's avatar
Ghislain MARY committed
851
			return 491;
852
		case SalReasonUnauthorized:
Ghislain MARY's avatar
Ghislain MARY committed
853
			return 401;
854
		case SalReasonNotAcceptable:
Ghislain MARY's avatar
Ghislain MARY committed
855
			return 488; // Or maybe 606 Not Acceptable?
856
		case SalReasonNoMatch:
Ghislain MARY's avatar
Ghislain MARY committed
857
			return 481;
858
		case SalReasonRequestTimeout:
Ghislain MARY's avatar
Ghislain MARY committed
859
			return 408;
860
		case SalReasonMovedPermanently:
Ghislain MARY's avatar
Ghislain MARY committed
861
			return 301;
862
		case SalReasonGone:
Ghislain MARY's avatar
Ghislain MARY committed
863
			return 410;
864
		case SalReasonAddressIncomplete:
Ghislain MARY's avatar
Ghislain MARY committed
865
			return 484;
866
		case SalReasonNotImplemented:
Ghislain MARY's avatar
Ghislain MARY committed
867
			return 501;
868
		case SalReasonServerTimeout:
Ghislain MARY's avatar
Ghislain MARY committed
869
			return 504;
870
		case SalReasonBadGateway:
Ghislain MARY's avatar
Ghislain MARY committed
871
			return 502;
872
		case SalReasonInternalError:
Ghislain MARY's avatar
Ghislain MARY committed
873 874
		default:
			return 500;
875 876 877 878 879
	}
}

LINPHONE_END_NAMESPACE

Ghislain MARY's avatar
Ghislain MARY committed
880 881 882
//*******************************
// C++ to C wrapping functions
//*******************************
883

884
using namespace LinphonePrivate;
885

886 887 888 889
// NOTE: This is ugly but it's not possible to export simply this set of functions in tester_utils...
// Because tester_utils and private files are ill-thought.
// A workaround is to use LINPHONE_PUBLIC here.

890 891
extern "C" {

Ghislain MARY's avatar
Ghislain MARY committed
892
LINPHONE_PUBLIC Sal *linphone_core_get_sal (const LinphoneCore *lc) {
893 894 895
	return lc->sal;
}

Ghislain MARY's avatar
Ghislain MARY committed
896
LINPHONE_PUBLIC SalOp *linphone_proxy_config_get_sal_op (const LinphoneProxyConfig *cfg) {
897 898 899
	return cfg->op;
}

Ghislain MARY's avatar
Ghislain MARY committed
900
LINPHONE_PUBLIC SalOp *linphone_call_get_op_as_sal_op (const LinphoneCall *call) {
901 902 903
	return linphone_call_get_op(call);
}

Ghislain MARY's avatar
Ghislain MARY committed
904
LINPHONE_PUBLIC Sal *sal_init (MSFactory *factory) {
905 906 907
	return new Sal(factory);
}

Ghislain MARY's avatar
Ghislain MARY committed
908
LINPHONE_PUBLIC void sal_uninit (Sal* sal) {
909 910 911
	delete sal;
}

Ghislain MARY's avatar
Ghislain MARY committed
912 913 914 915 916 917
LINPHONE_PUBLIC int sal_create_uuid (Sal *ctx, char *uuid, size_t len) {
	string uuidStr = ctx->createUuid();
	if (len < uuidStr.size())
		return -1;
	snprintf(uuid, len, "%s", uuidStr.c_str());
	return 0;
918 919
}

Ghislain MARY's avatar
Ghislain MARY committed
920
LINPHONE_PUBLIC void sal_set_uuid (Sal *ctx, const char *uuid) {
921
	ctx->setUuid(L_C_TO_STRING(uuid));
922 923
}

Ghislain MARY's avatar
Ghislain MARY committed
924 925
LINPHONE_PUBLIC void sal_default_set_sdp_handling (Sal* h, SalOpSDPHandling sdpHandlingMethod) {
	h->setDefaultSdpHandling(sdpHandlingMethod);
926 927
}

Ghislain MARY's avatar
Ghislain MARY committed
928
LINPHONE_PUBLIC void sal_set_send_error (Sal *sal,int value) {
929
	sal->setSendError(value);
930 931
}

Ghislain MARY's avatar
Ghislain MARY committed
932
LINPHONE_PUBLIC void sal_set_recv_error (Sal *sal,int value) {
933
	sal->setRecvError(value);
934 935
}

Ghislain MARY's avatar
Ghislain MARY committed
936
LINPHONE_PUBLIC void sal_enable_pending_trans_checking (Sal *sal, bool_t value) {
937
	sal->enablePendingTransactionChecking(!!value);
938 939
}

Ghislain MARY's avatar
Ghislain MARY committed
940
LINPHONE_PUBLIC void sal_enable_unconditional_answer (Sal *sal, bool_t value) {
941
	sal->enableUnconditionalAnswer(value);
942 943
}

Ghislain MARY's avatar
Ghislain MARY committed
944
LINPHONE_PUBLIC void sal_set_dns_timeout (Sal* sal, int timeout) {
945
	sal->setDnsTimeout(timeout);
946 947
}

Ghislain MARY's avatar
Ghislain MARY committed
948 949
LINPHONE_PUBLIC void sal_set_dns_user_hosts_file (Sal *sal, const char *hostsFile) {
	sal->setDnsUserHostsFile(hostsFile);
950 951
}

Ghislain MARY's avatar
Ghislain MARY committed
952
LINPHONE_PUBLIC void *sal_get_stack_impl (Sal *sal) {
953
	return sal->getStackImpl();
954 955
}

Ghislain MARY's avatar
Ghislain MARY committed
956
LINPHONE_PUBLIC void sal_set_refresher_retry_after (Sal *sal, int value) {
957
	sal->setRefresherRetryAfter(value);
958 959
}

Ghislain MARY's avatar
Ghislain MARY committed
960
LINPHONE_PUBLIC int sal_get_refresher_retry_after (const Sal *sal) {
961
	return sal->getRefresherRetryAfter();
962 963
}

Ghislain MARY's avatar
Ghislain MARY committed
964
LINPHONE_PUBLIC void sal_set_transport_timeout (Sal* sal, int timeout) {
965
	sal->setTransportTimeout(timeout);
966 967
}

Ghislain MARY's avatar
Ghislain MARY committed
968
LINPHONE_PUBLIC void sal_enable_test_features (Sal*ctx, bool_t value) {
969
	ctx->enableTestFeatures(!!value);
970 971
}

Ghislain MARY's avatar
Ghislain MARY committed
972 973
LINPHONE_PUBLIC bool_t sal_transport_available (Sal *ctx, SalTransport tr) {
	return (bool_t)ctx->isTransportAvailable(tr);
974 975
}

Ghislain MARY's avatar
Ghislain MARY committed
976
LINPHONE_PUBLIC const SalErrorInfo *sal_op_get_error_info (const SalOp *op) {
977
	return op->getErrorInfo();
978 979
}

Ghislain MARY's avatar
Ghislain MARY committed
980
LINPHONE_PUBLIC bool_t sal_call_dialog_request_pending (const SalOp *op) {
981
	auto callOp = dynamic_cast<const SalCallOp *>(op);
Ghislain MARY's avatar
Ghislain MARY committed
982 983
	if (!callOp)
		return FALSE;
984
	return callOp->dialogRequestPending();
985 986
}

Ghislain MARY's avatar
Ghislain MARY committed
987 988 989 990
LINPHONE_PUBLIC void sal_call_set_sdp_handling (SalOp *op, SalOpSDPHandling handling) {
	auto callOp = dynamic_cast<SalCallOp *>(op);
	if (callOp)
		callOp->setSdpHandling(handling);
991 992
}

Ghislain MARY's avatar
Ghislain MARY committed
993 994 995 996
LINPHONE_PUBLIC SalMediaDescription *sal_call_get_final_media_description (SalOp *op) {
	auto callOp = dynamic_cast<SalCallOp *>(op);
	if (!callOp)
		return nullptr;
997
	return callOp->getFinalMediaDescription();
998 999
}

Ghislain MARY's avatar
Ghislain MARY committed
1000
LINPHONE_PUBLIC belle_sip_resolver_context_t *sal_resolve_a (Sal *sal, const char *name, int port, int family, belle_sip_resolver_callback_t cb, void *data) {
1001
	return sal->resolveA(name, port, family, cb, data);
1002 1003
}

Ghislain MARY's avatar
Ghislain MARY committed
1004
LINPHONE_PUBLIC Sal *sal_op_get_sal (SalOp *op) {
1005
	return op->getSal();
Ghislain MARY's avatar
Ghislain MARY committed
1006 1007
}

Ghislain MARY's avatar
Ghislain MARY committed
1008
LINPHONE_PUBLIC SalOp *sal_create_refer_op (Sal *sal) {
Ghislain MARY's avatar
Ghislain MARY committed
1009 1010 1011
	return new SalReferOp(sal);
}

Ghislain MARY's avatar
Ghislain MARY committed
1012
LINPHONE_PUBLIC void sal_release_op (SalOp *op) {
Ghislain MARY's avatar
Ghislain MARY committed
1013 1014 1015
	op->release();
}

Ghislain MARY's avatar
Ghislain MARY committed
1016 1017 1018 1019
LINPHONE_PUBLIC void sal_op_set_from (SalOp *salReferOp, const char *from) {
	auto referOp = dynamic_cast<SalReferOp *>(salReferOp);
	if (referOp)
		referOp->setFrom(from);
Ghislain MARY's avatar
Ghislain MARY committed
1020 1021
}

Ghislain MARY's avatar
Ghislain MARY committed
1022 1023 1024 1025
LINPHONE_PUBLIC void sal_op_set_to (SalOp *salReferOp, const char *to) {
	auto referOp = dynamic_cast<SalReferOp *>(salReferOp);
	if (referOp)
		referOp->setTo(to);
Ghislain MARY's avatar
Ghislain MARY committed
1026 1027
}

Ghislain MARY's avatar
Ghislain MARY committed
1028 1029 1030 1031
LINPHONE_PUBLIC void sal_op_send_refer (SalOp *salReferOp, SalAddress *referTo) {
	auto referOp = dynamic_cast<SalReferOp *>(salReferOp);
	if (referOp)
		referOp->sendRefer(referTo);
Ghislain MARY's avatar
Ghislain MARY committed
1032 1033
}

Ghislain MARY's avatar
Ghislain MARY committed
1034 1035
LINPHONE_PUBLIC void sal_set_user_pointer (Sal *sal, void *userPointer) {
	sal->setUserPointer(userPointer);
Ghislain MARY's avatar
Ghislain MARY committed
1036 1037
}

Ghislain MARY's avatar
Ghislain MARY committed
1038
LINPHONE_PUBLIC void *sal_get_user_pointer (Sal *sal) {
1039
	return sal->getUserPointer();
Ghislain MARY's avatar
Ghislain MARY committed
1040 1041
}

Ghislain MARY's avatar
Ghislain MARY committed
1042 1043
LINPHONE_PUBLIC void sal_set_call_refer_callback (Sal *sal, void (*OnReferCb)(SalOp *op, const SalAddress *referTo)) {
	struct Sal::Callbacks cbs = { nullptr };
Ghislain MARY's avatar
Ghislain MARY committed
1044
	cbs.refer_received = OnReferCb;
1045
	sal->setCallbacks(&cbs);
Ghislain MARY's avatar
Ghislain MARY committed
1046 1047
}

Ghislain MARY's avatar
Ghislain MARY committed
1048 1049 1050 1051 1052
char *sal_generate_uuid (void) {
	string uuid = Sal::generateUuid();
	return bctbx_strdup(uuid.c_str());
}

1053
}