2014-07-21 07:10:57 +08:00
|
|
|
/** msi.c
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2015-02-18 06:34:40 +08:00
|
|
|
* Copyright (C) 2013-2015 Tox project All Rights Reserved.
|
2014-01-25 08:32:33 +08:00
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox 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
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2014-05-31 23:27:22 +08:00
|
|
|
#include "../toxcore/util.h"
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
#include "msi.h"
|
2014-03-23 02:33:56 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-23 06:22:01 +08:00
|
|
|
#include <stdbool.h>
|
2015-02-19 06:23:46 +08:00
|
|
|
#include <assert.h>
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
#define MSI_MAXMSG_SIZE 256
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
/* TODO send error on any calloc or etc */
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Protocol:
|
|
|
|
*
|
2014-07-21 07:10:57 +08:00
|
|
|
* |id [1 byte]| |size [1 byte]| |data [$size bytes]| |...{repeat}| |0 {end byte}|
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
typedef enum {
|
|
|
|
IDRequest = 1,
|
|
|
|
IDResponse,
|
2015-02-17 06:30:20 +08:00
|
|
|
IDError,
|
2015-02-16 05:41:10 +08:00
|
|
|
IDCapabilities,
|
2015-02-18 06:34:40 +08:00
|
|
|
IDMVFSZ,
|
|
|
|
IDMVFPSZ,
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
} MSIHeaderID;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
typedef enum {
|
2015-02-16 05:41:10 +08:00
|
|
|
requ_invite,
|
|
|
|
requ_start,
|
|
|
|
requ_reject,
|
|
|
|
requ_end,
|
2014-01-25 08:32:33 +08:00
|
|
|
} MSIRequest;
|
|
|
|
|
|
|
|
typedef enum {
|
2015-02-16 05:41:10 +08:00
|
|
|
resp_ringing,
|
|
|
|
resp_starting,
|
2014-01-25 08:32:33 +08:00
|
|
|
} MSIResponse;
|
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
#define GENERIC_HEADER(header, val_type) \
|
2015-02-16 05:41:10 +08:00
|
|
|
typedef struct { \
|
2015-02-19 06:23:46 +08:00
|
|
|
val_type value; \
|
|
|
|
bool exists; \
|
2015-02-18 06:34:40 +08:00
|
|
|
} MSIHeader##header
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
GENERIC_HEADER ( Request, MSIRequest );
|
|
|
|
GENERIC_HEADER ( Response, MSIResponse );
|
|
|
|
GENERIC_HEADER ( Error, MSIError );
|
|
|
|
GENERIC_HEADER ( Capabilities, uint8_t );
|
|
|
|
GENERIC_HEADER ( MVFSZ, uint16_t );
|
|
|
|
GENERIC_HEADER ( MVFPSZ, uint16_t );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
2015-02-16 05:41:10 +08:00
|
|
|
typedef struct {
|
|
|
|
MSIHeaderRequest request;
|
|
|
|
MSIHeaderResponse response;
|
2015-02-17 06:30:20 +08:00
|
|
|
MSIHeaderError error;
|
2015-02-16 05:41:10 +08:00
|
|
|
MSIHeaderCapabilities capabilities;
|
2015-02-18 06:34:40 +08:00
|
|
|
MSIHeaderMVFSZ mvfsz; /* Max video frame size. NOTE: Value must be in network b-order */
|
|
|
|
MSIHeaderMVFPSZ mvfpsz; /* Max video frame piece size. NOTE: Value must be in network b-order */
|
2014-01-25 08:32:33 +08:00
|
|
|
} MSIMessage;
|
|
|
|
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
int msg_parse_in ( MSIMessage *dest, const uint8_t *data, uint16_t length );
|
|
|
|
uint8_t *msg_parse_header_out ( MSIHeaderID id, uint8_t *dest, const void *value, uint8_t value_len, uint16_t *length );
|
|
|
|
int send_message ( Messenger* m, uint32_t friend_id, const MSIMessage *msg );
|
|
|
|
int send_error ( Messenger* m, uint32_t friend_id, MSIError error );
|
|
|
|
static void invoke_callback(MSICall* call, MSICallbackID cb);
|
|
|
|
static MSICall *get_call ( MSISession *session, uint32_t friend_id );
|
|
|
|
MSICall *new_call ( MSISession *session, uint32_t friend_id );
|
|
|
|
void kill_call ( MSICall *call );
|
|
|
|
void on_peer_status(Messenger *m, int friend_id, uint8_t status, void *data);
|
|
|
|
int handle_recv_invite ( MSICall *call, const MSIMessage *msg );
|
|
|
|
int handle_recv_start ( MSICall *call, const MSIMessage *msg );
|
|
|
|
int handle_recv_reject ( MSICall *call, const MSIMessage *msg );
|
|
|
|
int handle_recv_end ( MSICall *call, const MSIMessage *msg );
|
|
|
|
int handle_recv_ringing ( MSICall *call, const MSIMessage *msg );
|
|
|
|
int handle_recv_starting ( MSICall *call, const MSIMessage *msg );
|
|
|
|
int handle_recv_error ( MSICall *call, const MSIMessage *msg );
|
|
|
|
void handle_msi_packet ( Messenger *m, int friend_id, const uint8_t *data, uint16_t length, void *object );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public functions
|
|
|
|
*/
|
|
|
|
void msi_register_callback ( MSISession *session, MSICallbackType callback, MSICallbackID id)
|
|
|
|
{
|
|
|
|
session->callbacks[id] = callback;
|
|
|
|
}
|
|
|
|
MSISession *msi_new ( Messenger *messenger )
|
|
|
|
{
|
|
|
|
if (messenger == NULL) {
|
|
|
|
LOGGER_ERROR("Could not init session on empty messenger!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSISession *retu = calloc ( sizeof ( MSISession ), 1 );
|
|
|
|
|
|
|
|
if (retu == NULL) {
|
|
|
|
LOGGER_ERROR("Allocation failed! Program might misbehave!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create_recursive_mutex(retu->mutex) != 0) {
|
|
|
|
LOGGER_ERROR("Failed to init mutex! Program might misbehave");
|
|
|
|
free(retu);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
retu->messenger = messenger;
|
|
|
|
|
|
|
|
m_callback_msi_packet(messenger, handle_msi_packet, retu );
|
|
|
|
|
|
|
|
/* This is called when remote terminates session */
|
|
|
|
m_callback_connectionstatus_internal_av(messenger, on_peer_status, retu);
|
|
|
|
|
|
|
|
LOGGER_DEBUG("New msi session: %p ", retu);
|
|
|
|
return retu;
|
|
|
|
}
|
|
|
|
int msi_kill ( MSISession *session )
|
|
|
|
{
|
|
|
|
if (session == NULL) {
|
|
|
|
LOGGER_ERROR("Tried to terminate non-existing session");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_callback_msi_packet((struct Messenger *) session->messenger, NULL, NULL);
|
|
|
|
pthread_mutex_lock(session->mutex);
|
|
|
|
|
|
|
|
if (session->calls) {
|
|
|
|
MSIMessage msg_end;
|
|
|
|
msg_end.request.exists = true;
|
|
|
|
msg_end.request.value = requ_end;
|
|
|
|
|
|
|
|
MSICall* it = get_call(session, session->calls_head);
|
|
|
|
for (; it; it = it->next) {
|
|
|
|
send_message(session->messenger, it->friend_id, &msg_end);
|
|
|
|
kill_call(it); /* This will eventually free session->calls */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(session->mutex);
|
|
|
|
pthread_mutex_destroy(session->mutex);
|
|
|
|
|
|
|
|
LOGGER_DEBUG("Terminated session: %p", session);
|
|
|
|
free ( session );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int msi_invite ( MSISession *session, MSICall **call, uint32_t friend_id, uint8_t capabilities )
|
|
|
|
{
|
|
|
|
LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
|
|
|
|
|
|
|
|
if (get_call(session, friend_id) != NULL) {
|
|
|
|
LOGGER_ERROR("Already in a call");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*call) = new_call ( session, friend_id );
|
|
|
|
|
|
|
|
if ( *call == NULL )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
(*call)->self_capabilities = capabilities;
|
|
|
|
|
|
|
|
MSIMessage msg_invite;
|
|
|
|
msg_invite.request.exists = true;
|
|
|
|
msg_invite.request.value = requ_invite;
|
|
|
|
|
|
|
|
msg_invite.capabilities.exists = true;
|
|
|
|
msg_invite.capabilities.value = capabilities;
|
|
|
|
|
|
|
|
msg_invite.mvfsz.exists = true;
|
|
|
|
msg_invite.mvfsz.value = htons(D_MVFSZ);
|
|
|
|
|
|
|
|
msg_invite.mvfpsz.exists = true;
|
|
|
|
msg_invite.mvfpsz.value = htons(D_MVFPSZ);
|
|
|
|
|
|
|
|
send_message ( (*call)->session->messenger, (*call)->friend_id, &msg_invite );
|
|
|
|
|
|
|
|
(*call)->state = msi_CallRequesting;
|
|
|
|
|
|
|
|
LOGGER_DEBUG("Invite sent");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int msi_hangup ( MSICall* call )
|
|
|
|
{
|
|
|
|
LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index);
|
|
|
|
|
|
|
|
MSIMessage msg_end;
|
|
|
|
msg_end.request.exists = true;
|
|
|
|
msg_end.request.value = requ_end;
|
|
|
|
send_message ( call->session->messenger, call->friend_id, &msg_end );
|
|
|
|
|
|
|
|
kill_call(call);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int msi_answer ( MSICall* call, uint8_t capabilities )
|
|
|
|
{
|
|
|
|
LOGGER_DEBUG("Session: %p Answering call from: %u", call->session, call->friend_id);
|
|
|
|
|
|
|
|
if ( call->state != msi_CallRequested ) {
|
|
|
|
LOGGER_ERROR("Call is in invalid state!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->self_capabilities = capabilities;
|
|
|
|
|
|
|
|
MSIMessage msg_starting;
|
|
|
|
msg_starting.response.exists = true;
|
|
|
|
msg_starting.response.value = resp_starting;
|
|
|
|
|
|
|
|
msg_starting.capabilities.exists = true;
|
|
|
|
msg_starting.capabilities.value = capabilities;
|
|
|
|
|
|
|
|
msg_starting.mvfsz.exists = true;
|
|
|
|
msg_starting.mvfsz.value = htons(D_MVFSZ);
|
|
|
|
|
|
|
|
msg_starting.mvfpsz.exists = true;
|
|
|
|
msg_starting.mvfpsz.value = htons(D_MVFPSZ);
|
|
|
|
|
|
|
|
send_message ( call->session->messenger, call->friend_id, &msg_starting );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int msi_reject ( MSICall* call )
|
|
|
|
{
|
|
|
|
LOGGER_DEBUG("Session: %p Rejecting call: %u; reason: %s", session, call_index, reason ? reason : "Unknown");
|
|
|
|
|
|
|
|
if ( call->state != msi_CallRequested ) {
|
|
|
|
LOGGER_ERROR("Call is in invalid state!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSIMessage msg_reject;
|
|
|
|
msg_reject.request.exists = true;
|
|
|
|
msg_reject.request.value = requ_reject;
|
|
|
|
|
|
|
|
send_message ( call->session->messenger, call->friend_id, &msg_reject );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int msi_change_csettings( MSICall* call, uint8_t capabilities )
|
|
|
|
{
|
|
|
|
call->self_capabilities = capabilities;
|
|
|
|
|
|
|
|
MSIMessage msg_invite;
|
|
|
|
msg_invite.request.exists = true;
|
|
|
|
msg_invite.request.value = requ_invite;
|
|
|
|
|
|
|
|
msg_invite.capabilities.exists = true;
|
|
|
|
msg_invite.capabilities.value = capabilities;
|
|
|
|
|
|
|
|
send_message ( call->session->messenger, call->friend_id, &msg_invite );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
int msg_parse_in ( MSIMessage *dest, const uint8_t *data, uint16_t length )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-18 06:34:40 +08:00
|
|
|
/* Parse raw data received from socket into MSIMessage struct */
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
#define CHECK_SIZE(bytes, constraint, size) \
|
2015-02-18 06:34:40 +08:00
|
|
|
if ((constraint -= (2 + size)) < 1) { LOGGER_ERROR("Read over length!"); return -1; } \
|
2015-02-17 06:30:20 +08:00
|
|
|
if ( bytes[1] != size ) { LOGGER_ERROR("Invalid data size!"); return -1; }
|
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
#define CHECK_ENUM_HIGH(bytes, enum_high) /* Assumes size == 1 */ \
|
2015-02-17 06:30:20 +08:00
|
|
|
if ( bytes[2] > enum_high ) { LOGGER_ERROR("Failed enum high limit!"); return -1; }
|
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
#define SET_UINT8(bytes, header) do { \
|
2015-02-16 05:41:10 +08:00
|
|
|
header.value = bytes[2]; \
|
2015-02-18 06:34:40 +08:00
|
|
|
header.exists = true; \
|
2015-02-16 05:41:10 +08:00
|
|
|
bytes += 3; \
|
|
|
|
} while(0)
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
#define SET_UINT16(bytes, header) do { \
|
|
|
|
memcpy(&header.value, bytes + 2, 2);\
|
|
|
|
header.exists = true; \
|
|
|
|
bytes += 4; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(dest);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
if ( length == 0 || data[length - 1] ) { /* End byte must have value 0 */
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_ERROR("Invalid end byte");
|
2014-02-07 07:10:55 +08:00
|
|
|
return -1;
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
const uint8_t *it = data;
|
|
|
|
int size_constraint = length;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
while ( *it ) {/* until end byte is hit */
|
|
|
|
switch (*it) {
|
2014-07-22 23:20:55 +08:00
|
|
|
case IDRequest:
|
2015-02-17 06:30:20 +08:00
|
|
|
CHECK_SIZE(it, size_constraint, 1);
|
|
|
|
CHECK_ENUM_HIGH(it, requ_end);
|
2015-02-18 06:34:40 +08:00
|
|
|
SET_UINT8(it, dest->request);
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case IDResponse:
|
2015-02-17 06:30:20 +08:00
|
|
|
CHECK_SIZE(it, size_constraint, 1);
|
2015-02-19 06:23:46 +08:00
|
|
|
CHECK_ENUM_HIGH(it, resp_starting);
|
2015-02-18 06:34:40 +08:00
|
|
|
SET_UINT8(it, dest->response);
|
2014-07-22 23:20:55 +08:00
|
|
|
it += 3;
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
case IDError:
|
|
|
|
CHECK_SIZE(it, size_constraint, 1);
|
|
|
|
CHECK_ENUM_HIGH(it, msi_ErrUndisclosed);
|
2015-02-18 06:34:40 +08:00
|
|
|
SET_UINT8(it, dest->error);
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
2015-02-16 05:41:10 +08:00
|
|
|
|
|
|
|
case IDCapabilities:
|
2015-02-17 06:30:20 +08:00
|
|
|
CHECK_SIZE(it, size_constraint, 1);
|
2015-02-18 06:34:40 +08:00
|
|
|
SET_UINT8(it, dest->capabilities);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IDMVFSZ:
|
|
|
|
CHECK_SIZE(it, size_constraint, 2);
|
|
|
|
SET_UINT16(it, dest->mvfsz);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IDMVFPSZ:
|
|
|
|
CHECK_SIZE(it, size_constraint, 2);
|
|
|
|
SET_UINT16(it, dest->mvfpsz);
|
2014-07-27 01:29:49 +08:00
|
|
|
break;
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
default:
|
|
|
|
LOGGER_ERROR("Invalid id byte");
|
|
|
|
return -1;
|
|
|
|
break;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
#undef CHECK_SIZE
|
|
|
|
#undef CHECK_ENUM_HIGH
|
2015-02-18 06:34:40 +08:00
|
|
|
#undef SET_UINT8
|
|
|
|
#undef SET_UINT16
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
uint8_t *msg_parse_header_out ( MSIHeaderID id, uint8_t *dest, const void *value, uint8_t value_len, uint16_t *length )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2015-02-18 06:34:40 +08:00
|
|
|
/* Parse a single header for sending */
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(dest);
|
|
|
|
assert(value);
|
|
|
|
assert(value_len);
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
*dest = id;
|
|
|
|
dest ++;
|
|
|
|
*dest = value_len;
|
|
|
|
dest ++;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
memcpy(dest, value, value_len);
|
2014-06-15 22:36:57 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
*length += (2 + value_len);
|
2014-06-15 22:36:57 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
return dest + value_len; /* Set to next position ready to be written */
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int send_message ( Messenger* m, uint32_t friend_id, const MSIMessage *msg )
|
2015-02-18 06:34:40 +08:00
|
|
|
{
|
|
|
|
/* Parse and send message */
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(m);
|
2015-02-18 06:34:40 +08:00
|
|
|
|
|
|
|
uint8_t parsed [MSI_MAXMSG_SIZE];
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
uint8_t *it = parsed;
|
2014-07-21 07:10:57 +08:00
|
|
|
uint16_t size = 0;
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (msg->request.exists) {
|
|
|
|
uint8_t cast = msg->request.value;
|
2015-02-19 06:23:46 +08:00
|
|
|
it = msg_parse_header_out(IDRequest, it, &cast,
|
2015-02-18 06:34:40 +08:00
|
|
|
sizeof(cast), &size);
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (msg->response.exists) {
|
|
|
|
uint8_t cast = msg->response.value;
|
2015-02-19 06:23:46 +08:00
|
|
|
it = msg_parse_header_out(IDResponse, it, &cast,
|
2015-02-18 06:34:40 +08:00
|
|
|
sizeof(cast), &size);
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
if (msg->error.exists) {
|
2015-02-19 06:23:46 +08:00
|
|
|
it = msg_parse_header_out(IDError, it, &msg->error.value,
|
2015-02-18 06:34:40 +08:00
|
|
|
sizeof(msg->error.value), &size);
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2015-02-16 05:41:10 +08:00
|
|
|
if (msg->capabilities.exists) {
|
2015-02-19 06:23:46 +08:00
|
|
|
it = msg_parse_header_out(IDCapabilities, it, &msg->capabilities.value,
|
2015-02-18 06:34:40 +08:00
|
|
|
sizeof(msg->capabilities.value), &size);
|
2014-07-27 01:29:49 +08:00
|
|
|
}
|
2015-02-18 06:34:40 +08:00
|
|
|
|
|
|
|
if (msg->mvfsz.exists) {
|
2015-02-19 06:23:46 +08:00
|
|
|
it = msg_parse_header_out(IDMVFSZ, it, &msg->mvfsz.value,
|
2015-02-18 06:34:40 +08:00
|
|
|
sizeof(msg->mvfsz.value), &size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->mvfpsz.exists) {
|
2015-02-19 06:23:46 +08:00
|
|
|
it = msg_parse_header_out(IDMVFPSZ, it, &msg->mvfpsz.value,
|
2015-02-18 06:34:40 +08:00
|
|
|
sizeof(msg->mvfpsz.value), &size);
|
|
|
|
}
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
*it = 0;
|
|
|
|
size ++;
|
2015-02-18 06:34:40 +08:00
|
|
|
|
|
|
|
if ( it == parsed ) {
|
|
|
|
LOGGER_WARNING("Parsing message failed; empty message");
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
if ( m_msi_packet(m, friend_id, parsed, size) ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Sent message");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int send_error ( Messenger* m, uint32_t friend_id, MSIError error )
|
2014-11-18 07:46:46 +08:00
|
|
|
{
|
2015-02-18 06:34:40 +08:00
|
|
|
/* Send error message */
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(m);
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
LOGGER_DEBUG("Sending error: %d to friend: %d", error, friend_id);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
MSIMessage msg_error;
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
msg_error.error.exists = true;
|
|
|
|
msg_error.error.value = error;
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
send_message ( m, friend_id, &msg_error );
|
2014-11-18 07:46:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
static void invoke_callback(MSICall* call, MSICallbackID cb)
|
|
|
|
{
|
|
|
|
assert(call);
|
|
|
|
|
|
|
|
if ( call->session->callbacks[cb] ) {
|
|
|
|
LOGGER_DEBUG("Invoking callback function: %d", cb);
|
|
|
|
call->session->callbacks[cb] ( call->session->av, call );
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
static MSICall *get_call ( MSISession *session, uint32_t friend_id )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(session);
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
if (session->calls == NULL || session->calls_tail < friend_id)
|
|
|
|
return NULL;
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
return session->calls[friend_id];
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
MSICall *new_call ( MSISession *session, uint32_t friend_id )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(session);
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
MSICall *rc = calloc(sizeof(MSICall), 1);
|
|
|
|
|
|
|
|
if (rc == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rc->friend_id = friend_id;
|
|
|
|
|
|
|
|
if (session->calls == NULL) { /* Creating */
|
|
|
|
session->calls = calloc (sizeof(MSICall*), friend_id + 1);
|
|
|
|
|
|
|
|
if (session->calls == NULL) {
|
|
|
|
free(rc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->calls_tail = session->calls_head = friend_id;
|
|
|
|
|
|
|
|
} else if (session->calls_tail < friend_id) { /* Appending */
|
|
|
|
void* tmp = realloc(session->calls, sizeof(MSICall*) * friend_id + 1);
|
|
|
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
free(rc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->calls = tmp;
|
|
|
|
|
|
|
|
/* Set fields in between to null */
|
|
|
|
int32_t i = session->calls_tail;
|
|
|
|
for (; i < friend_id; i ++)
|
|
|
|
session->calls[i] = NULL;
|
|
|
|
|
|
|
|
rc->prev = session->calls[session->calls_tail];
|
|
|
|
session->calls[session->calls_tail]->next = rc;
|
|
|
|
|
|
|
|
session->calls_tail = friend_id;
|
|
|
|
|
|
|
|
} else if (session->calls_head > friend_id) { /* Inserting at front */
|
|
|
|
rc->next = session->calls[session->calls_head];
|
|
|
|
session->calls[session->calls_head]->prev = rc;
|
|
|
|
session->calls_head = friend_id;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
session->calls[friend_id] = rc;
|
|
|
|
return rc;
|
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
void kill_call ( MSICall *call )
|
2015-02-17 06:30:20 +08:00
|
|
|
{
|
|
|
|
if ( call == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
MSISession* session = call->session;
|
|
|
|
|
|
|
|
MSICall* prev = call->prev;
|
|
|
|
MSICall* next = call->next;
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
else if (next)
|
|
|
|
session->calls_head = next->friend_id;
|
|
|
|
else goto CLEAR;
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
next->prev = prev;
|
|
|
|
else if (prev)
|
|
|
|
session->calls_tail = prev->friend_id;
|
|
|
|
else goto CLEAR;
|
|
|
|
|
|
|
|
session->calls[call->friend_id] = NULL;
|
|
|
|
free(call);
|
|
|
|
return;
|
|
|
|
|
|
|
|
CLEAR:
|
|
|
|
session->calls_head = session->calls_tail = 0;
|
|
|
|
free(session->calls);
|
|
|
|
session->calls = NULL;
|
|
|
|
free(call);
|
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
void on_peer_status(Messenger *m, int friend_id, uint8_t status, void *data)
|
2014-11-18 07:46:46 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
(void)m;
|
|
|
|
MSISession *session = data;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
switch ( status ) {
|
2015-02-19 06:23:46 +08:00
|
|
|
case 0: { /* Friend is now offline */
|
|
|
|
LOGGER_DEBUG("Friend %d is now offline", friend_id);
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
MSICall* call = get_call(session, friend_id);
|
|
|
|
|
|
|
|
if (call == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
invoke_callback(call, msi_OnPeerTimeout);
|
2015-02-17 06:30:20 +08:00
|
|
|
kill_call(call);
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int handle_recv_invite ( MSICall *call, const MSIMessage *msg )
|
2015-02-17 06:30:20 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(call);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
MSISession* session = call->session;
|
|
|
|
|
|
|
|
LOGGER_DEBUG("Session: %p Handling 'invite' friend: %d", call->session, call->friend_id);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
if (!msg->capabilities.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid capabilities on 'invite'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidMessage;
|
2015-02-18 06:34:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSIMessage response;
|
|
|
|
response.response.exists = true;
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
if ( call->state == msi_CallRequesting ) {
|
|
|
|
/* The rare glare case.
|
|
|
|
* Send starting and wait for starting by the other side.
|
|
|
|
* The peer will do the same.
|
|
|
|
* When you receive starting from peer send started.
|
|
|
|
* Don't notice the app until the start is received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
LOGGER_DEBUG("Glare detected!");
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
if (!msg->mvfsz.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid mvfsz on 'invite'");
|
|
|
|
call->error = msi_InvalidMessage;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg->mvfpsz.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid mvfpsz on 'invite'");
|
|
|
|
call->error = msi_InvalidMessage;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->peer_capabilities = msg->capabilities.value;
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
call->peer_mvfsz = ntohs(msg->mvfsz.value);
|
|
|
|
call->peer_mvfpsz = ntohs(msg->mvfpsz.value);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
if (call->peer_mvfsz > call->peer_mvfpsz) {
|
|
|
|
LOGGER_WARNING("Session: %p mvfsz param greater than mvfpsz on 'invite'");
|
|
|
|
call->error = msi_InvalidParam;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
/* Send response */
|
|
|
|
response.response.value = resp_starting;
|
2015-02-19 06:23:46 +08:00
|
|
|
send_message ( call->session->messenger, call->friend_id, &response );
|
2015-02-18 06:34:40 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else if ( call->state == msi_CallActive ) {
|
|
|
|
/* Changing capabilities.
|
|
|
|
* We send starting but no response is expected.
|
|
|
|
* WARNING: if start is sent call is terminated with an error
|
|
|
|
*/
|
|
|
|
LOGGER_DEBUG("Peer is changing capabilities");
|
|
|
|
|
|
|
|
/* Send response */
|
|
|
|
response.response.value = resp_starting;
|
2015-02-19 06:23:46 +08:00
|
|
|
send_message ( call->session->messenger, call->friend_id, &response );
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
if ( call->peer_capabilities != msg->capabilities.value) {
|
|
|
|
/* Only invoke callback if capabilities changed */
|
|
|
|
call->peer_capabilities = msg->capabilities.value;
|
|
|
|
invoke_callback(call, msi_OnCapabilities);
|
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
|
|
|
|
if (!msg->mvfsz.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid mvfsz on 'invite'");
|
|
|
|
call->error = msi_InvalidMessage;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg->mvfpsz.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid mvfpsz on 'invite'");
|
|
|
|
call->error = msi_InvalidMessage;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->peer_capabilities = msg->capabilities.value;
|
2015-02-18 06:34:40 +08:00
|
|
|
|
|
|
|
call->peer_mvfsz = ntohs(msg->mvfsz.value);
|
|
|
|
call->peer_mvfpsz = ntohs(msg->mvfpsz.value);
|
|
|
|
|
2015-01-25 06:29:54 +08:00
|
|
|
call->state = msi_CallRequested;
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
/* Send response */
|
|
|
|
response.response.value = resp_ringing;
|
2015-02-19 06:23:46 +08:00
|
|
|
send_message ( call->session->messenger, call->friend_id, &response );
|
2015-02-18 06:34:40 +08:00
|
|
|
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
invoke_callback(call, msi_OnInvite);
|
2015-02-17 06:30:20 +08:00
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int handle_recv_start ( MSICall *call, const MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(call);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
if ( call->state != msi_CallRequested || call->state != msi_CallRequesting ) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid call state on 'start'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidState;
|
2015-02-17 06:30:20 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-08-04 06:15:00 +08:00
|
|
|
(void)msg;
|
2014-08-05 01:49:53 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'start', friend id: %d", call->session, call->friend_id );
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
call->state = msi_CallActive;
|
2015-02-19 06:23:46 +08:00
|
|
|
invoke_callback(call, msi_OnStart);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int handle_recv_reject ( MSICall *call, const MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(call);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2014-08-04 06:15:00 +08:00
|
|
|
(void)msg;
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
if ( call->state != msi_CallRequesting ) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid call state on 'reject'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidState;
|
2015-02-17 06:30:20 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGGER_DEBUG("Session: %p Handling 'reject', friend id: %u", call->session, call->friend_id);
|
2014-08-05 01:49:53 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
invoke_callback(call, msi_OnReject);
|
2015-02-17 06:30:20 +08:00
|
|
|
kill_call(call);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int handle_recv_end ( MSICall *call, const MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(call);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
(void)msg;
|
2014-06-23 07:54:40 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'end', friend id: %d", call->session, call->friend_id);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
invoke_callback(call, msi_OnEnd);
|
2015-02-17 06:30:20 +08:00
|
|
|
kill_call ( call );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int handle_recv_ringing ( MSICall *call, const MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(call);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-08-04 06:15:00 +08:00
|
|
|
(void)msg;
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
if ( call->state != msi_CallRequesting ) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid call state on 'ringing'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidState;
|
2015-02-17 06:30:20 +08:00
|
|
|
return -1;
|
2014-06-27 05:42:37 +08:00
|
|
|
}
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'ringing' friend id: %d", call->session, call->friend_id );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
invoke_callback(call, msi_OnRinging);
|
2015-02-17 06:30:20 +08:00
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int handle_recv_starting ( MSICall *call, const MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(call);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
if ( call->state == msi_CallActive ) {
|
|
|
|
LOGGER_DEBUG("Capabilities change confirmed");
|
|
|
|
return 0;
|
|
|
|
} else if ( call->state != msi_CallRequested || call->state != msi_CallRequesting ) {
|
2015-02-17 06:30:20 +08:00
|
|
|
LOGGER_WARNING("Session: %p Invalid call state on 'starting'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidState;
|
2015-02-17 06:30:20 +08:00
|
|
|
return -1;
|
2014-05-30 05:57:58 +08:00
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
if (call->state == msi_CallRequesting) {
|
2015-02-18 06:34:40 +08:00
|
|
|
if (!msg->capabilities.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid capabilities on 'starting'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidParam;
|
2015-02-18 06:34:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg->mvfsz.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid mvfsz on 'invite'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidParam;
|
2015-02-18 06:34:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msg->mvfpsz.exists) {
|
|
|
|
LOGGER_WARNING("Session: %p Invalid mvfpsz on 'invite'");
|
2015-02-19 06:23:46 +08:00
|
|
|
call->error = msi_InvalidParam;
|
2015-02-18 06:34:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->peer_capabilities = msg->capabilities.value;
|
|
|
|
|
|
|
|
call->peer_mvfsz = ntohs(msg->mvfsz.value);
|
|
|
|
call->peer_mvfpsz = ntohs(msg->mvfpsz.value);
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
|
|
|
|
if (call->peer_mvfsz > call->peer_mvfpsz) {
|
|
|
|
LOGGER_WARNING("Session: %p mvfsz param greater than mvfpsz on 'invite'");
|
|
|
|
call->error = msi_InvalidParam;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
call->state = msi_CallActive;
|
2015-02-19 06:23:46 +08:00
|
|
|
invoke_callback(call, msi_OnStart);
|
2015-02-18 06:34:40 +08:00
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
/* Otherwise it's a glare case so don't start until 'start' is recved */
|
|
|
|
|
2015-02-18 06:34:40 +08:00
|
|
|
/* Send start in either case (glare or normal) */
|
|
|
|
MSIMessage msg_start;
|
|
|
|
msg_start.request.exists = true;
|
|
|
|
msg_start.request.value = requ_start;
|
2015-02-19 06:23:46 +08:00
|
|
|
send_message ( call->session->messenger, call->friend_id, &msg_start );
|
2015-02-18 06:34:40 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
int handle_recv_error ( MSICall *call, const MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-19 06:23:46 +08:00
|
|
|
assert(call);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'error' friend id: %d", call->session, call->friend_id );
|
2015-02-19 06:23:46 +08:00
|
|
|
|
|
|
|
call->error = msg->error.value;
|
|
|
|
invoke_callback(call, msi_OnError);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
void handle_msi_packet ( Messenger *m, int friend_id, const uint8_t *data, uint16_t length, void *object )
|
2014-02-01 19:52:48 +08:00
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Got msi message");
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
MSISession *session = object;
|
2015-02-18 06:34:40 +08:00
|
|
|
MSIMessage msg;
|
2015-02-19 06:23:46 +08:00
|
|
|
|
|
|
|
int rc = 0;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
if ( msg_parse_in ( &msg, data, length ) == -1 ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_WARNING("Error parsing message");
|
2015-02-19 06:23:46 +08:00
|
|
|
send_error(m, friend_id, msi_InvalidMessage);
|
2014-04-28 01:21:26 +08:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
LOGGER_DEBUG("Successfully parsed message");
|
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
MSICall *call = get_call(session, friend_id);
|
|
|
|
|
|
|
|
if (call == NULL) {
|
2015-02-19 06:23:46 +08:00
|
|
|
if (msg.request.value != requ_invite) {
|
|
|
|
send_error(m, friend_id, msi_StrayMessage);
|
2015-02-17 06:30:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
call = new_call(session, friend_id);
|
|
|
|
if (call == NULL) {
|
2015-02-19 06:23:46 +08:00
|
|
|
send_error(m, friend_id, msi_SystemError);
|
2015-02-17 06:30:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 05:41:10 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
/* Now handle message */
|
2015-02-18 06:34:40 +08:00
|
|
|
if ( msg.request.exists ) { /* Handle request */
|
|
|
|
switch (msg.request.value) {
|
2015-02-16 05:41:10 +08:00
|
|
|
case requ_invite:
|
2015-02-18 06:34:40 +08:00
|
|
|
rc = handle_recv_invite ( call, &msg );
|
2014-07-22 23:20:55 +08:00
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-16 05:41:10 +08:00
|
|
|
case requ_start:
|
2015-02-18 06:34:40 +08:00
|
|
|
rc = handle_recv_start ( call, &msg );
|
2014-07-22 23:20:55 +08:00
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-16 05:41:10 +08:00
|
|
|
case requ_reject:
|
2015-02-18 06:34:40 +08:00
|
|
|
rc = handle_recv_reject ( call, &msg );
|
2014-07-22 23:20:55 +08:00
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-16 05:41:10 +08:00
|
|
|
case requ_end:
|
2015-02-18 06:34:40 +08:00
|
|
|
rc = handle_recv_end ( call, &msg );
|
2014-07-22 23:20:55 +08:00
|
|
|
break;
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
2015-02-18 06:34:40 +08:00
|
|
|
} else if ( msg.response.exists ) { /* Handle response */
|
|
|
|
switch (msg.response.value) {
|
2015-02-16 05:41:10 +08:00
|
|
|
case resp_ringing:
|
2015-02-18 06:34:40 +08:00
|
|
|
rc = handle_recv_ringing ( call, &msg );
|
2014-07-22 23:20:55 +08:00
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-16 05:41:10 +08:00
|
|
|
case resp_starting:
|
2015-02-18 06:34:40 +08:00
|
|
|
rc = handle_recv_starting ( call, &msg );
|
2014-07-22 23:20:55 +08:00
|
|
|
break;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2015-02-19 06:23:46 +08:00
|
|
|
} else if (msg.error.exists) {
|
|
|
|
handle_recv_error ( call, &msg );
|
|
|
|
rc = -1;
|
2014-04-28 01:21:26 +08:00
|
|
|
} else {
|
2015-02-19 06:23:46 +08:00
|
|
|
LOGGER_WARNING("Invalid message; none of the request, response or error!");
|
|
|
|
call->error = msi_InvalidMessage;
|
2015-02-17 06:30:20 +08:00
|
|
|
rc = -1;
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
2015-02-17 06:30:20 +08:00
|
|
|
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
if (rc == -1) {
|
|
|
|
if (call->error != msi_ErrorNone)
|
|
|
|
send_error(m, friend_id, call->error);
|
2015-02-17 06:30:20 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
kill_call(call);
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|