2015-12-23 20:25:28 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 12:40:32 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
|
|
|
* Copyright (C) 2016-2017 - Gregor Richards
|
2015-12-23 20:25:28 +00:00
|
|
|
*
|
|
|
|
* RetroArch 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 Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch 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 RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-12-14 02:01:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2016-09-05 22:56:00 +00:00
|
|
|
|
2016-12-14 02:01:31 +00:00
|
|
|
#include <boolean.h>
|
2016-12-03 14:58:16 +00:00
|
|
|
#include <compat/strl.h>
|
2016-12-14 02:01:31 +00:00
|
|
|
#include <rhash.h>
|
2016-05-19 09:46:54 +00:00
|
|
|
|
2016-12-14 02:01:31 +00:00
|
|
|
#include "netplay_private.h"
|
2016-09-15 03:19:47 +00:00
|
|
|
|
2017-02-04 08:20:41 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../../config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-15 23:10:24 +00:00
|
|
|
#include "../../autosave.h"
|
2016-12-13 04:07:46 +00:00
|
|
|
#include "../../configuration.h"
|
2016-05-19 09:46:54 +00:00
|
|
|
#include "../../content.h"
|
2016-12-13 20:26:20 +00:00
|
|
|
#include "../../retroarch.h"
|
2016-09-03 05:51:11 +00:00
|
|
|
#include "../../runloop.h"
|
2016-09-03 05:45:51 +00:00
|
|
|
#include "../../version.h"
|
2017-02-04 08:20:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MENU
|
2016-12-13 20:26:20 +00:00
|
|
|
#include "../../menu/widgets/menu_input_dialog.h"
|
2017-02-04 08:20:41 +00:00
|
|
|
#endif
|
2016-01-22 15:09:48 +00:00
|
|
|
|
2016-12-14 02:01:31 +00:00
|
|
|
#ifndef HAVE_SOCKET_LEGACY
|
|
|
|
/* Custom inet_ntop. Win32 doesn't seem to support this ... */
|
|
|
|
void netplay_log_connection(const struct sockaddr_storage *their_addr,
|
|
|
|
unsigned slot, const char *nick)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
const struct sockaddr_storage *storage;
|
|
|
|
const struct sockaddr_in *v4;
|
|
|
|
const struct sockaddr_in6 *v6;
|
|
|
|
} u;
|
|
|
|
const char *str = NULL;
|
|
|
|
char buf_v4[INET_ADDRSTRLEN] = {0};
|
|
|
|
char buf_v6[INET6_ADDRSTRLEN] = {0};
|
|
|
|
char msg[512];
|
|
|
|
|
|
|
|
msg[0] = '\0';
|
|
|
|
|
|
|
|
u.storage = their_addr;
|
|
|
|
|
|
|
|
switch (their_addr->ss_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in in;
|
|
|
|
|
|
|
|
memset(&in, 0, sizeof(in));
|
|
|
|
|
|
|
|
str = buf_v4;
|
|
|
|
in.sin_family = AF_INET;
|
|
|
|
memcpy(&in.sin_addr, &u.v4->sin_addr, sizeof(struct in_addr));
|
|
|
|
|
|
|
|
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in),
|
|
|
|
buf_v4, sizeof(buf_v4),
|
|
|
|
NULL, 0, NI_NUMERICHOST);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 in;
|
|
|
|
memset(&in, 0, sizeof(in));
|
|
|
|
|
|
|
|
str = buf_v6;
|
|
|
|
in.sin6_family = AF_INET6;
|
|
|
|
memcpy(&in.sin6_addr, &u.v6->sin6_addr, sizeof(struct in6_addr));
|
|
|
|
|
|
|
|
getnameinfo((struct sockaddr*)&in, sizeof(struct sockaddr_in6),
|
|
|
|
buf_v6, sizeof(buf_v6), NULL, 0, NI_NUMERICHOST);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM_NAME),
|
|
|
|
nick, str);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-14 02:01:31 +00:00
|
|
|
RARCH_LOG("%s\n", msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM),
|
|
|
|
nick);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-14 02:01:31 +00:00
|
|
|
RARCH_LOG("%s\n", msg);
|
|
|
|
}
|
|
|
|
RARCH_LOG("%s %u\n", msg_hash_to_str(MSG_CONNECTION_SLOT),
|
|
|
|
slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
void netplay_log_connection(const struct sockaddr_storage *their_addr,
|
|
|
|
unsigned slot, const char *nick)
|
|
|
|
{
|
|
|
|
char msg[512];
|
|
|
|
|
|
|
|
msg[0] = '\0';
|
|
|
|
|
|
|
|
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_GOT_CONNECTION_FROM),
|
|
|
|
nick);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-14 02:01:31 +00:00
|
|
|
RARCH_LOG("%s\n", msg);
|
|
|
|
RARCH_LOG("%s %u\n",
|
|
|
|
msg_hash_to_str(MSG_CONNECTION_SLOT), slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-12-23 20:25:28 +00:00
|
|
|
/**
|
2016-05-12 10:03:43 +00:00
|
|
|
* netplay_impl_magic:
|
2015-12-23 20:25:28 +00:00
|
|
|
*
|
2016-12-17 19:48:07 +00:00
|
|
|
* A pseudo-hash of the RetroArch and Netplay version, so only compatible
|
|
|
|
* versions play together.
|
|
|
|
*/
|
2016-05-12 10:03:43 +00:00
|
|
|
uint32_t netplay_impl_magic(void)
|
2015-12-23 20:25:28 +00:00
|
|
|
{
|
|
|
|
size_t i, len;
|
|
|
|
uint32_t res = 0;
|
|
|
|
const char *ver = PACKAGE_VERSION;
|
2016-01-28 03:26:17 +00:00
|
|
|
|
2015-12-23 20:25:28 +00:00
|
|
|
len = strlen(ver);
|
|
|
|
for (i = 0; i < len; i++)
|
2016-12-17 19:48:07 +00:00
|
|
|
res ^= ver[i] << (i & 0xf);
|
2015-12-23 20:25:28 +00:00
|
|
|
|
2016-12-17 19:48:07 +00:00
|
|
|
res ^= NETPLAY_PROTOCOL_VERSION << (i & 0xf);
|
Multitudinous fixes and updates to Netplay. Had to be one commit since
they're mostly related:
(1) Renamed frame_count to self_frame_count to be consistent with all
other names.
(2) Previously, it was possible to overwrite data in the ring buffer
that hadn't yet been used. Now that's not possible, but that just
changes one breakage for another: It's now possible to miss the NEW
data. The final resolution for this will probably be requesting stalls.
This is accomplished simply by storing frame numbers in the ring buffer
and checking them against the 'other' head.
(3) In TCP packets, separated cmd_size from cmd. It was beyond pointless
for these to be combined, and restricted cmd_size to 16 bits, which
will probably fail when/if state loading is supported.
(4) Readahead is now allowed. In the past, if the peer got ahead of us,
we would simply ignore their data. Thus, if they got too far ahead of
us, we'd stop reading their data altogether. Fabulous. Now, we're happy
to read future input.
(5) If the peer gets too far ahead of us (currently an unconfigurable 10
frames), fast forward to catch up. This should prevent desync due to
clock drift or stutter.
(6) Used frame_count in a few places where ptr was used. Doing a
comparison of pointers on a ring buffer is a far more dangerous way to
assure we're done with a task than simply using the count, since the
ring buffer is... well, a ring.
(7) Renamed tmp_{ptr,frame_count} to replay_{ptr,frame_count} for
clarity.
(8) Slightly changed the protocol version hash, just to assure that
other clients wouldn't think they were compatible with this one.
(9) There was an off-by-one error which, under some circumstances, could
allow the replay engine to run a complete round through the ring buffer,
replaying stale data. Fixed.
2016-09-12 02:01:47 +00:00
|
|
|
|
2015-12-23 20:25:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-06 01:12:42 +00:00
|
|
|
/**
|
|
|
|
* netplay_platform_magic
|
|
|
|
*
|
|
|
|
* Just enough info to tell us if our platforms mismatch: Endianness and a
|
|
|
|
* couple of type sizes.
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
* bit 31: Reserved
|
|
|
|
* bit 30: 1 for big endian
|
|
|
|
* bits 29-15: sizeof(size_t)
|
|
|
|
* bits 14-0: sizeof(long)
|
|
|
|
*/
|
|
|
|
static uint32_t netplay_platform_magic(void)
|
|
|
|
{
|
|
|
|
uint32_t ret =
|
|
|
|
((1 == htonl(1)) << 30)
|
|
|
|
|(sizeof(size_t) << 15)
|
|
|
|
|(sizeof(long));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* netplay_endian_mismatch
|
|
|
|
*
|
|
|
|
* Do the platform magics mismatch on endianness?
|
|
|
|
*/
|
|
|
|
static bool netplay_endian_mismatch(uint32_t pma, uint32_t pmb)
|
|
|
|
{
|
|
|
|
uint32_t ebit = (1<<30);
|
|
|
|
return (pma & ebit) != (pmb & ebit);
|
|
|
|
}
|
|
|
|
|
2016-12-14 00:10:44 +00:00
|
|
|
static unsigned long simple_rand_next = 1;
|
|
|
|
|
|
|
|
static int simple_rand()
|
|
|
|
{
|
|
|
|
simple_rand_next = simple_rand_next * 1103515245 + 12345;
|
|
|
|
return((unsigned)(simple_rand_next/65536) % 32768);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void simple_srand(unsigned int seed) {
|
|
|
|
simple_rand_next = seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t simple_rand_uint32()
|
|
|
|
{
|
|
|
|
uint32_t parts[3];
|
|
|
|
parts[0] = simple_rand();
|
|
|
|
parts[1] = simple_rand();
|
|
|
|
parts[2] = simple_rand();
|
|
|
|
return ((parts[0] << 30) +
|
|
|
|
(parts[1] << 15) +
|
|
|
|
parts[2]);
|
|
|
|
}
|
|
|
|
|
2016-12-15 13:42:03 +00:00
|
|
|
/**
|
|
|
|
* netplay_handshake_init_send
|
|
|
|
*
|
|
|
|
* Initialize our handshake and send the first part of the handshake protocol.
|
|
|
|
*/
|
|
|
|
bool netplay_handshake_init_send(netplay_t *netplay,
|
|
|
|
struct netplay_connection *connection)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
2016-12-17 19:48:07 +00:00
|
|
|
uint32_t header[4] = {0};
|
2016-12-21 14:51:50 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
header[0] = htonl(netplay_impl_magic());
|
2016-12-17 19:48:07 +00:00
|
|
|
header[1] = htonl(netplay_platform_magic());
|
|
|
|
header[2] = htonl(NETPLAY_COMPRESSION_SUPPORTED);
|
2016-12-21 14:51:50 +00:00
|
|
|
if (netplay->is_server &&
|
|
|
|
(settings->netplay.password[0] || settings->netplay.spectate_password[0]))
|
2016-12-13 22:05:07 +00:00
|
|
|
{
|
|
|
|
/* Demand a password */
|
2016-12-14 00:10:44 +00:00
|
|
|
if (simple_rand_next == 1)
|
2016-12-21 15:13:31 +00:00
|
|
|
simple_srand((unsigned int) time(NULL));
|
2016-12-14 00:10:44 +00:00
|
|
|
connection->salt = simple_rand_uint32();
|
2016-12-13 22:05:07 +00:00
|
|
|
if (connection->salt == 0) connection->salt = 1;
|
2016-12-17 19:48:07 +00:00
|
|
|
header[3] = htonl(connection->salt);
|
2016-12-13 22:05:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-17 19:48:07 +00:00
|
|
|
header[3] = htonl(0);
|
2016-12-13 22:05:07 +00:00
|
|
|
}
|
2016-12-03 20:17:38 +00:00
|
|
|
|
2016-12-09 19:14:54 +00:00
|
|
|
if (!netplay_send(&connection->send_packet_buffer, connection->fd, header,
|
2016-12-03 20:17:38 +00:00
|
|
|
sizeof(header)) ||
|
2016-12-09 19:14:54 +00:00
|
|
|
!netplay_send_flush(&connection->send_packet_buffer, connection->fd, false))
|
2016-12-03 20:17:38 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nick_buf_s
|
|
|
|
{
|
|
|
|
uint32_t cmd[2];
|
2016-12-13 22:07:49 +00:00
|
|
|
char nick[NETPLAY_NICK_LEN];
|
2016-12-03 20:17:38 +00:00
|
|
|
};
|
|
|
|
|
2016-12-13 20:26:20 +00:00
|
|
|
struct password_buf_s
|
|
|
|
{
|
|
|
|
uint32_t cmd[2];
|
2016-12-13 22:07:49 +00:00
|
|
|
char password[NETPLAY_PASS_HASH_LEN];
|
2016-12-13 20:26:20 +00:00
|
|
|
};
|
|
|
|
|
2016-12-17 19:48:07 +00:00
|
|
|
struct info_buf_s
|
|
|
|
{
|
|
|
|
uint32_t cmd[2];
|
|
|
|
char core_name[NETPLAY_NICK_LEN];
|
|
|
|
char core_version[NETPLAY_NICK_LEN];
|
|
|
|
uint32_t content_crc;
|
|
|
|
};
|
|
|
|
|
2016-12-03 20:17:38 +00:00
|
|
|
#define RECV(buf, sz) \
|
2016-12-09 19:14:54 +00:00
|
|
|
recvd = netplay_recv(&connection->recv_packet_buffer, connection->fd, (buf), (sz), false); \
|
2016-12-24 20:25:03 +00:00
|
|
|
if (recvd >= 0 && recvd < (ssize_t) (sz)) \
|
2016-12-03 20:17:38 +00:00
|
|
|
{ \
|
2016-12-09 19:14:54 +00:00
|
|
|
netplay_recv_reset(&connection->recv_packet_buffer); \
|
2016-12-03 20:17:38 +00:00
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
else if (recvd < 0)
|
|
|
|
|
2017-02-04 08:20:41 +00:00
|
|
|
static netplay_t *handshake_password_netplay = NULL;
|
2016-12-13 20:26:20 +00:00
|
|
|
|
2017-02-04 08:20:41 +00:00
|
|
|
#ifdef HAVE_MENU
|
2016-12-13 20:26:20 +00:00
|
|
|
static void handshake_password(void *ignore, const char *line)
|
|
|
|
{
|
2016-12-13 22:05:07 +00:00
|
|
|
struct password_buf_s password_buf;
|
2016-12-13 22:07:49 +00:00
|
|
|
char password[8+NETPLAY_PASS_LEN]; /* 8 for salt, 128 for password */
|
2016-12-13 20:26:20 +00:00
|
|
|
netplay_t *netplay = handshake_password_netplay;
|
|
|
|
struct netplay_connection *connection = &netplay->connections[0];
|
|
|
|
|
2016-12-13 22:05:07 +00:00
|
|
|
snprintf(password, sizeof(password), "%08X", connection->salt);
|
|
|
|
strlcpy(password + 8, line, sizeof(password)-8);
|
|
|
|
|
|
|
|
password_buf.cmd[0] = htonl(NETPLAY_CMD_PASSWORD);
|
|
|
|
password_buf.cmd[1] = htonl(sizeof(password_buf.password));
|
|
|
|
sha256_hash(password_buf.password, (uint8_t *) password, strlen(password));
|
2016-12-13 20:26:20 +00:00
|
|
|
|
2016-12-21 15:13:31 +00:00
|
|
|
/* We have no way to handle an error here, so we'll let the next function error out */
|
|
|
|
if (netplay_send(&connection->send_packet_buffer, connection->fd, &password_buf, sizeof(password_buf)))
|
|
|
|
netplay_send_flush(&connection->send_packet_buffer, connection->fd, false);
|
2016-12-13 20:26:20 +00:00
|
|
|
|
2017-02-04 08:20:41 +00:00
|
|
|
#ifdef HAVE_MENU
|
2016-12-13 20:26:20 +00:00
|
|
|
menu_input_dialog_end();
|
|
|
|
rarch_ctl(RARCH_CTL_MENU_RUNNING_FINISHED, NULL);
|
2017-02-04 08:20:41 +00:00
|
|
|
#endif
|
2016-12-13 20:26:20 +00:00
|
|
|
}
|
2017-02-04 08:20:41 +00:00
|
|
|
#endif
|
2016-12-13 20:26:20 +00:00
|
|
|
|
2016-12-15 13:42:03 +00:00
|
|
|
/**
|
|
|
|
* netplay_handshake_init
|
|
|
|
*
|
|
|
|
* Data receiver for the initial part of the handshake, i.e., waiting for the
|
|
|
|
* netplay header.
|
|
|
|
*/
|
|
|
|
bool netplay_handshake_init(netplay_t *netplay,
|
|
|
|
struct netplay_connection *connection, bool *had_input)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
2016-12-17 19:48:07 +00:00
|
|
|
uint32_t header[4] = {0};
|
2016-12-03 20:17:38 +00:00
|
|
|
ssize_t recvd;
|
2016-12-15 22:20:04 +00:00
|
|
|
const char *dmsg;
|
2016-12-03 20:17:38 +00:00
|
|
|
struct nick_buf_s nick_buf;
|
|
|
|
uint32_t local_pmagic, remote_pmagic;
|
|
|
|
uint32_t compression;
|
2016-12-18 21:52:21 +00:00
|
|
|
struct compression_transcoder *ctrans;
|
2016-12-03 20:17:38 +00:00
|
|
|
|
2016-12-15 22:20:04 +00:00
|
|
|
dmsg = NULL;
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
RECV(header, sizeof(header))
|
|
|
|
{
|
2016-12-15 22:20:04 +00:00
|
|
|
dmsg = msg_hash_to_str(MSG_FAILED_TO_RECEIVE_HEADER_FROM_CLIENT);
|
2016-12-03 20:17:38 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (netplay_impl_magic() != ntohl(header[0]))
|
|
|
|
{
|
2016-12-15 22:20:04 +00:00
|
|
|
dmsg = msg_hash_to_str(MSG_NETPLAY_IMPLEMENTATIONS_DIFFER);
|
2016-12-03 20:17:38 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We only care about platform magic if our core is quirky */
|
|
|
|
local_pmagic = netplay_platform_magic();
|
2016-12-17 19:48:07 +00:00
|
|
|
remote_pmagic = ntohl(header[1]);
|
2016-12-03 20:17:38 +00:00
|
|
|
if ((netplay->quirks & NETPLAY_QUIRK_ENDIAN_DEPENDENT) &&
|
|
|
|
netplay_endian_mismatch(local_pmagic, remote_pmagic))
|
|
|
|
{
|
|
|
|
RARCH_ERR("Endianness mismatch with an endian-sensitive core.\n");
|
2016-12-15 22:20:04 +00:00
|
|
|
dmsg = msg_hash_to_str(MSG_NETPLAY_ENDIAN_DEPENDENT);
|
2016-12-03 20:17:38 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if ((netplay->quirks & NETPLAY_QUIRK_PLATFORM_DEPENDENT) &&
|
|
|
|
(local_pmagic != remote_pmagic))
|
|
|
|
{
|
|
|
|
RARCH_ERR("Platform mismatch with a platform-sensitive core.\n");
|
2016-12-15 22:20:04 +00:00
|
|
|
dmsg = msg_hash_to_str(MSG_NETPLAY_PLATFORM_DEPENDENT);
|
2016-12-03 20:17:38 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check what compression is supported */
|
2016-12-17 19:48:07 +00:00
|
|
|
compression = ntohl(header[2]);
|
2016-12-03 20:17:38 +00:00
|
|
|
compression &= NETPLAY_COMPRESSION_SUPPORTED;
|
|
|
|
if (compression & NETPLAY_COMPRESSION_ZLIB)
|
|
|
|
{
|
2016-12-18 21:52:21 +00:00
|
|
|
ctrans = &netplay->compress_zlib;
|
|
|
|
if (!ctrans->compression_backend)
|
|
|
|
{
|
|
|
|
ctrans->compression_backend =
|
|
|
|
trans_stream_get_zlib_deflate_backend();
|
|
|
|
if (!ctrans->compression_backend)
|
|
|
|
ctrans->compression_backend = trans_stream_get_pipe_backend();
|
|
|
|
}
|
|
|
|
connection->compression_supported = NETPLAY_COMPRESSION_ZLIB;
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-18 21:52:21 +00:00
|
|
|
ctrans = &netplay->compress_nil;
|
|
|
|
if (!ctrans->compression_backend)
|
|
|
|
{
|
|
|
|
ctrans->compression_backend =
|
|
|
|
trans_stream_get_pipe_backend();
|
|
|
|
}
|
|
|
|
connection->compression_supported = 0;
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
2016-12-18 21:52:21 +00:00
|
|
|
if (!ctrans->decompression_backend)
|
|
|
|
ctrans->decompression_backend = ctrans->compression_backend->reverse;
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
/* Allocate our compression stream */
|
2016-12-18 21:52:21 +00:00
|
|
|
if (!ctrans->compression_stream)
|
|
|
|
{
|
|
|
|
ctrans->compression_stream = ctrans->compression_backend->stream_new();
|
|
|
|
ctrans->decompression_stream = ctrans->decompression_backend->stream_new();
|
|
|
|
}
|
|
|
|
if (!ctrans->compression_stream || !ctrans->decompression_stream)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to allocate compression transcoder!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:26:20 +00:00
|
|
|
/* If a password is demanded, ask for it */
|
2016-12-17 19:48:07 +00:00
|
|
|
if (!netplay->is_server && (connection->salt = ntohl(header[3])))
|
2016-12-13 20:26:20 +00:00
|
|
|
{
|
2017-02-04 08:20:41 +00:00
|
|
|
#ifdef HAVE_MENU
|
2016-12-13 20:26:20 +00:00
|
|
|
menu_input_ctx_line_t line;
|
|
|
|
rarch_ctl(RARCH_CTL_MENU_RUNNING, NULL);
|
2017-02-04 08:20:41 +00:00
|
|
|
#endif
|
|
|
|
|
2016-12-13 20:26:20 +00:00
|
|
|
handshake_password_netplay = netplay;
|
2017-02-04 08:20:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MENU
|
|
|
|
memset(&line, 0, sizeof(line));
|
|
|
|
line.label = msg_hash_to_str(MSG_NETPLAY_ENTER_PASSWORD);
|
2016-12-13 20:26:20 +00:00
|
|
|
line.label_setting = "no_setting";
|
2017-02-04 08:20:41 +00:00
|
|
|
line.cb = handshake_password;
|
2016-12-21 00:16:31 +00:00
|
|
|
if (!menu_input_dialog_start(&line))
|
|
|
|
return false;
|
2017-02-04 08:20:41 +00:00
|
|
|
#endif
|
2016-12-13 20:26:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 20:17:38 +00:00
|
|
|
/* Send our nick */
|
|
|
|
nick_buf.cmd[0] = htonl(NETPLAY_CMD_NICK);
|
|
|
|
nick_buf.cmd[1] = htonl(sizeof(nick_buf.nick));
|
|
|
|
memset(nick_buf.nick, 0, sizeof(nick_buf.nick));
|
|
|
|
strlcpy(nick_buf.nick, netplay->nick, sizeof(nick_buf.nick));
|
2016-12-09 19:14:54 +00:00
|
|
|
if (!netplay_send(&connection->send_packet_buffer, connection->fd, &nick_buf,
|
2016-12-03 20:17:38 +00:00
|
|
|
sizeof(nick_buf)) ||
|
2016-12-09 19:14:54 +00:00
|
|
|
!netplay_send_flush(&connection->send_packet_buffer, connection->fd, false))
|
2016-12-03 20:17:38 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Move on to the next mode */
|
2016-12-05 05:04:01 +00:00
|
|
|
connection->mode = NETPLAY_CONNECTION_PRE_NICK;
|
2016-12-03 20:17:38 +00:00
|
|
|
*had_input = true;
|
2016-12-09 19:14:54 +00:00
|
|
|
netplay_recv_flush(&connection->recv_packet_buffer);
|
2016-12-03 20:17:38 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2016-12-15 22:20:04 +00:00
|
|
|
if (dmsg)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
2016-12-15 22:20:04 +00:00
|
|
|
RARCH_ERR("%s\n", dmsg);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(dmsg, 1, 180, false);
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:04:01 +00:00
|
|
|
static void netplay_handshake_ready(netplay_t *netplay, struct netplay_connection *connection)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
|
|
|
char msg[512];
|
|
|
|
|
|
|
|
if (netplay->is_server)
|
|
|
|
{
|
2017-02-26 09:33:03 +00:00
|
|
|
netplay_log_connection(&connection->addr, (unsigned)(connection - netplay->connections), connection->nick);
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
/* Send them the savestate */
|
|
|
|
if (!(netplay->quirks & (NETPLAY_QUIRK_NO_SAVESTATES|NETPLAY_QUIRK_NO_TRANSMISSION)))
|
|
|
|
{
|
2016-12-13 00:34:50 +00:00
|
|
|
netplay->force_send_savestate = true;
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(msg, sizeof(msg), "%s: \"%s\"",
|
|
|
|
msg_hash_to_str(MSG_CONNECTED_TO),
|
2016-12-10 03:02:31 +00:00
|
|
|
connection->nick);
|
2016-12-03 20:17:38 +00:00
|
|
|
RARCH_LOG("%s\n", msg);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 20:25:37 +00:00
|
|
|
/* Unstall if we were waiting for this */
|
2016-12-09 18:32:04 +00:00
|
|
|
if (netplay->stall == NETPLAY_STALL_NO_CONNECTION)
|
2016-12-15 02:28:20 +00:00
|
|
|
netplay->stall = NETPLAY_STALL_NONE;
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 19:48:07 +00:00
|
|
|
/**
|
|
|
|
* netplay_handshake_info
|
|
|
|
*
|
|
|
|
* Send an INFO command.
|
|
|
|
*/
|
|
|
|
bool netplay_handshake_info(netplay_t *netplay, struct netplay_connection *connection)
|
|
|
|
{
|
|
|
|
struct info_buf_s info_buf;
|
|
|
|
rarch_system_info_t *core_info;
|
|
|
|
uint32_t *content_crc_ptr;
|
|
|
|
|
|
|
|
memset(&info_buf, 0, sizeof(info_buf));
|
|
|
|
info_buf.cmd[0] = htonl(NETPLAY_CMD_INFO);
|
|
|
|
info_buf.cmd[1] = htonl(sizeof(info_buf) - 2*sizeof(uint32_t));
|
|
|
|
|
|
|
|
/* Get our core info */
|
|
|
|
core_info = NULL;
|
|
|
|
runloop_ctl(RUNLOOP_CTL_SYSTEM_INFO_GET, &core_info);
|
|
|
|
if (core_info)
|
|
|
|
{
|
|
|
|
strlcpy(info_buf.core_name, core_info->info.library_name, sizeof(info_buf.core_name));
|
|
|
|
strlcpy(info_buf.core_version, core_info->info.library_version, sizeof(info_buf.core_version));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strlcpy(info_buf.core_name, "UNKNOWN", sizeof(info_buf.core_name));
|
|
|
|
strlcpy(info_buf.core_version, "UNKNOWN", sizeof(info_buf.core_version));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get our content CRC */
|
|
|
|
content_crc_ptr = NULL;
|
|
|
|
content_get_crc(&content_crc_ptr);
|
|
|
|
if (content_crc_ptr)
|
|
|
|
info_buf.content_crc = htonl(*content_crc_ptr);
|
|
|
|
|
|
|
|
/* Send it off and wait for info back */
|
|
|
|
if (!netplay_send(&connection->send_packet_buffer, connection->fd,
|
|
|
|
&info_buf, sizeof(info_buf)) ||
|
|
|
|
!netplay_send_flush(&connection->send_packet_buffer, connection->fd,
|
|
|
|
false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
connection->mode = NETPLAY_CONNECTION_PRE_INFO;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* netplay_handshake_sync
|
|
|
|
*
|
|
|
|
* Send a SYNC command.
|
|
|
|
*/
|
2016-12-13 20:26:20 +00:00
|
|
|
bool netplay_handshake_sync(netplay_t *netplay, struct netplay_connection *connection)
|
|
|
|
{
|
|
|
|
/* If we're the server, now we send sync info */
|
|
|
|
uint32_t cmd[5];
|
|
|
|
uint32_t connected_players;
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
size_t i;
|
|
|
|
uint32_t device;
|
|
|
|
retro_ctx_memory_info_t mem_info;
|
2016-12-17 23:59:42 +00:00
|
|
|
size_t nicklen, nickmangle;
|
|
|
|
int matchct;
|
|
|
|
bool nick_matched;
|
2016-12-13 20:26:20 +00:00
|
|
|
|
2017-01-15 23:10:24 +00:00
|
|
|
autosave_lock();
|
2016-12-13 20:26:20 +00:00
|
|
|
mem_info.id = RETRO_MEMORY_SAVE_RAM;
|
|
|
|
core_get_memory(&mem_info);
|
2017-01-15 23:10:24 +00:00
|
|
|
autosave_unlock();
|
2016-12-13 20:26:20 +00:00
|
|
|
|
|
|
|
/* Send basic sync info */
|
|
|
|
cmd[0] = htonl(NETPLAY_CMD_SYNC);
|
2016-12-17 23:59:42 +00:00
|
|
|
cmd[1] = htonl(3*sizeof(uint32_t) + MAX_USERS*sizeof(uint32_t) +
|
|
|
|
NETPLAY_NICK_LEN + mem_info.size);
|
2017-01-20 19:28:18 +00:00
|
|
|
cmd[2] = htonl(netplay->self_frame_count);
|
2016-12-13 20:26:20 +00:00
|
|
|
connected_players = netplay->connected_players;
|
|
|
|
if (netplay->self_mode == NETPLAY_CONNECTION_PLAYING)
|
|
|
|
connected_players |= 1<<netplay->self_player;
|
2016-12-18 03:41:34 +00:00
|
|
|
if (netplay->local_paused || netplay->remote_paused)
|
|
|
|
connected_players |= NETPLAY_CMD_SYNC_BIT_PAUSED;
|
2016-12-13 20:26:20 +00:00
|
|
|
cmd[3] = htonl(connected_players);
|
|
|
|
if (netplay->flip)
|
|
|
|
cmd[4] = htonl(netplay->flip_frame);
|
|
|
|
else
|
|
|
|
cmd[4] = htonl(0);
|
|
|
|
|
|
|
|
if (!netplay_send(&connection->send_packet_buffer, connection->fd, cmd,
|
|
|
|
sizeof(cmd)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Now send the device info */
|
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
|
|
|
{
|
|
|
|
device = htonl(settings->input.libretro_device[i]);
|
|
|
|
if (!netplay_send(&connection->send_packet_buffer, connection->fd,
|
|
|
|
&device, sizeof(device)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-17 23:59:42 +00:00
|
|
|
/* Now see if we need to mangle their nick */
|
|
|
|
nicklen = strlen(connection->nick);
|
|
|
|
if (nicklen > NETPLAY_NICK_LEN - 5)
|
|
|
|
nickmangle = NETPLAY_NICK_LEN - 5;
|
|
|
|
else
|
|
|
|
nickmangle = nicklen;
|
|
|
|
matchct = 1;
|
|
|
|
do {
|
|
|
|
nick_matched = false;
|
|
|
|
for (i = 0; i < netplay->connections_size; i++)
|
|
|
|
{
|
|
|
|
struct netplay_connection *sc = &netplay->connections[i];
|
|
|
|
if (sc == connection) continue;
|
|
|
|
if (sc->active &&
|
|
|
|
sc->mode >= NETPLAY_CONNECTION_CONNECTED &&
|
|
|
|
!strncmp(connection->nick, sc->nick, NETPLAY_NICK_LEN))
|
|
|
|
{
|
|
|
|
nick_matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strncmp(connection->nick, netplay->nick, NETPLAY_NICK_LEN))
|
|
|
|
nick_matched = true;
|
|
|
|
|
|
|
|
if (nick_matched)
|
|
|
|
{
|
|
|
|
/* Somebody has this nick, make a new one! */
|
|
|
|
snprintf(connection->nick + nickmangle, NETPLAY_NICK_LEN - nickmangle, " (%d)", ++matchct);
|
|
|
|
connection->nick[NETPLAY_NICK_LEN - 1] = '\0';
|
|
|
|
}
|
|
|
|
} while (nick_matched);
|
|
|
|
|
|
|
|
/* Send the nick */
|
|
|
|
if (!netplay_send(&connection->send_packet_buffer, connection->fd,
|
|
|
|
connection->nick, NETPLAY_NICK_LEN))
|
|
|
|
return false;
|
|
|
|
|
2016-12-13 20:26:20 +00:00
|
|
|
/* And finally, the SRAM */
|
2017-01-15 23:10:24 +00:00
|
|
|
autosave_lock();
|
2016-12-13 20:26:20 +00:00
|
|
|
if (!netplay_send(&connection->send_packet_buffer, connection->fd,
|
|
|
|
mem_info.data, mem_info.size) ||
|
|
|
|
!netplay_send_flush(&connection->send_packet_buffer, connection->fd,
|
|
|
|
false))
|
2017-01-15 23:10:24 +00:00
|
|
|
{
|
|
|
|
autosave_unlock();
|
2016-12-13 20:26:20 +00:00
|
|
|
return false;
|
2017-01-15 23:10:24 +00:00
|
|
|
}
|
|
|
|
autosave_unlock();
|
2016-12-13 20:26:20 +00:00
|
|
|
|
|
|
|
/* Now we're ready! */
|
|
|
|
connection->mode = NETPLAY_CONNECTION_SPECTATING;
|
|
|
|
netplay_handshake_ready(netplay, connection);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-15 13:42:03 +00:00
|
|
|
/**
|
|
|
|
* netplay_handshake_pre_nick
|
|
|
|
*
|
|
|
|
* Data receiver for the second stage of handshake, receiving the other side's
|
|
|
|
* nickname.
|
|
|
|
*/
|
|
|
|
bool netplay_handshake_pre_nick(netplay_t *netplay,
|
|
|
|
struct netplay_connection *connection, bool *had_input)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
|
|
|
struct nick_buf_s nick_buf;
|
|
|
|
ssize_t recvd;
|
|
|
|
char msg[512];
|
2016-12-21 14:51:50 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
msg[0] = '\0';
|
|
|
|
|
2016-12-19 22:31:24 +00:00
|
|
|
RECV(&nick_buf, sizeof(nick_buf)) {}
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
/* Expecting only a nick command */
|
|
|
|
if (recvd < 0 ||
|
|
|
|
ntohl(nick_buf.cmd[0]) != NETPLAY_CMD_NICK ||
|
|
|
|
ntohl(nick_buf.cmd[1]) != sizeof(nick_buf.nick))
|
|
|
|
{
|
|
|
|
if (netplay->is_server)
|
|
|
|
strlcpy(msg, msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT),
|
|
|
|
sizeof(msg));
|
|
|
|
else
|
|
|
|
strlcpy(msg,
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_RECEIVE_NICKNAME_FROM_HOST),
|
|
|
|
sizeof(msg));
|
|
|
|
RARCH_ERR("%s\n", msg);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-03 20:17:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:02:31 +00:00
|
|
|
strlcpy(connection->nick, nick_buf.nick,
|
|
|
|
(sizeof(connection->nick) < sizeof(nick_buf.nick)) ?
|
|
|
|
sizeof(connection->nick) : sizeof(nick_buf.nick));
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
if (netplay->is_server)
|
|
|
|
{
|
2016-12-21 14:51:50 +00:00
|
|
|
if (settings->netplay.password[0] || settings->netplay.spectate_password[0])
|
2016-12-13 20:26:20 +00:00
|
|
|
{
|
|
|
|
/* There's a password, so just put them in PRE_PASSWORD mode */
|
|
|
|
connection->mode = NETPLAY_CONNECTION_PRE_PASSWORD;
|
|
|
|
}
|
2016-12-12 02:34:17 +00:00
|
|
|
else
|
2016-12-13 04:07:46 +00:00
|
|
|
{
|
2016-12-15 03:18:24 +00:00
|
|
|
connection->can_play = true;
|
2016-12-17 19:48:07 +00:00
|
|
|
if (!netplay_handshake_info(netplay, connection))
|
2016-12-13 04:07:46 +00:00
|
|
|
return false;
|
2016-12-17 19:48:07 +00:00
|
|
|
connection->mode = NETPLAY_CONNECTION_PRE_INFO;
|
2016-12-13 04:07:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-17 19:48:07 +00:00
|
|
|
/* Client needs to wait for INFO */
|
|
|
|
connection->mode = NETPLAY_CONNECTION_PRE_INFO;
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*had_input = true;
|
2016-12-09 19:14:54 +00:00
|
|
|
netplay_recv_flush(&connection->recv_packet_buffer);
|
2016-12-03 20:17:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-15 13:42:03 +00:00
|
|
|
/**
|
|
|
|
* netplay_handshake_pre_password
|
|
|
|
*
|
|
|
|
* Data receiver for the third, optional stage of server handshake, receiving
|
2016-12-17 19:48:07 +00:00
|
|
|
* the password and sending core/content info.
|
2016-12-15 13:42:03 +00:00
|
|
|
*/
|
|
|
|
bool netplay_handshake_pre_password(netplay_t *netplay,
|
|
|
|
struct netplay_connection *connection, bool *had_input)
|
2016-12-13 20:26:20 +00:00
|
|
|
{
|
2016-12-13 22:05:07 +00:00
|
|
|
struct password_buf_s password_buf, corr_password_buf;
|
2016-12-13 22:07:49 +00:00
|
|
|
char password[8+NETPLAY_PASS_LEN]; /* 8 for salt */
|
2016-12-13 20:26:20 +00:00
|
|
|
ssize_t recvd;
|
|
|
|
char msg[512];
|
2016-12-15 03:18:24 +00:00
|
|
|
bool correct;
|
2016-12-21 14:51:50 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-12-13 20:26:20 +00:00
|
|
|
|
|
|
|
msg[0] = '\0';
|
|
|
|
|
2016-12-19 22:31:24 +00:00
|
|
|
RECV(&password_buf, sizeof(password_buf)) {}
|
2016-12-13 20:26:20 +00:00
|
|
|
|
2016-12-13 22:05:07 +00:00
|
|
|
/* Expecting only a password command */
|
2016-12-13 20:26:20 +00:00
|
|
|
if (recvd < 0 ||
|
|
|
|
ntohl(password_buf.cmd[0]) != NETPLAY_CMD_PASSWORD ||
|
|
|
|
ntohl(password_buf.cmd[1]) != sizeof(password_buf.password))
|
|
|
|
{
|
|
|
|
if (netplay->is_server)
|
|
|
|
strlcpy(msg, msg_hash_to_str(MSG_FAILED_TO_GET_NICKNAME_FROM_CLIENT),
|
|
|
|
sizeof(msg));
|
|
|
|
else
|
|
|
|
strlcpy(msg,
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_RECEIVE_NICKNAME_FROM_HOST),
|
|
|
|
sizeof(msg));
|
|
|
|
RARCH_ERR("%s\n", msg);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-13 20:26:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:18:24 +00:00
|
|
|
/* Calculate the correct password hash(es) and compare */
|
|
|
|
correct = false;
|
2016-12-13 22:05:07 +00:00
|
|
|
snprintf(password, sizeof(password), "%08X", connection->salt);
|
2016-12-21 14:51:50 +00:00
|
|
|
if (settings->netplay.password[0])
|
2016-12-15 03:18:24 +00:00
|
|
|
{
|
2016-12-21 14:51:50 +00:00
|
|
|
strlcpy(password + 8, settings->netplay.password, sizeof(password)-8);
|
2016-12-15 03:18:24 +00:00
|
|
|
sha256_hash(corr_password_buf.password, (uint8_t *) password, strlen(password));
|
|
|
|
if (!memcmp(password_buf.password, corr_password_buf.password, sizeof(password_buf.password)))
|
|
|
|
{
|
|
|
|
correct = true;
|
|
|
|
connection->can_play = true;
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 14:51:50 +00:00
|
|
|
if (settings->netplay.spectate_password[0])
|
2016-12-15 03:18:24 +00:00
|
|
|
{
|
2016-12-21 14:51:50 +00:00
|
|
|
strlcpy(password + 8, settings->netplay.spectate_password, sizeof(password)-8);
|
2016-12-15 03:18:24 +00:00
|
|
|
sha256_hash(corr_password_buf.password, (uint8_t *) password, strlen(password));
|
|
|
|
if (!memcmp(password_buf.password, corr_password_buf.password, sizeof(password_buf.password)))
|
|
|
|
correct = true;
|
|
|
|
}
|
2016-12-13 22:05:07 +00:00
|
|
|
|
2016-12-15 03:18:24 +00:00
|
|
|
/* Just disconnect if it was wrong */
|
|
|
|
if (!correct)
|
2016-12-13 20:26:20 +00:00
|
|
|
return false;
|
|
|
|
|
2016-12-17 19:48:07 +00:00
|
|
|
/* Otherwise, exchange info */
|
|
|
|
if (!netplay_handshake_info(netplay, connection))
|
2016-12-13 20:26:20 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*had_input = true;
|
2016-12-17 19:48:07 +00:00
|
|
|
connection->mode = NETPLAY_CONNECTION_PRE_INFO;
|
2016-12-13 20:26:20 +00:00
|
|
|
netplay_recv_flush(&connection->recv_packet_buffer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-17 19:48:07 +00:00
|
|
|
/**
|
|
|
|
* netplay_handshake_pre_info
|
|
|
|
*
|
|
|
|
* Data receiver for the third stage of server handshake, receiving
|
|
|
|
* the password.
|
|
|
|
*/
|
|
|
|
bool netplay_handshake_pre_info(netplay_t *netplay,
|
|
|
|
struct netplay_connection *connection, bool *had_input)
|
|
|
|
{
|
|
|
|
struct info_buf_s info_buf;
|
2016-12-18 01:33:02 +00:00
|
|
|
uint32_t cmd_size;
|
2016-12-17 19:48:07 +00:00
|
|
|
ssize_t recvd;
|
|
|
|
rarch_system_info_t *core_info;
|
|
|
|
uint32_t *content_crc_ptr;
|
|
|
|
const char *dmsg = NULL;
|
|
|
|
|
2017-02-28 03:06:20 +00:00
|
|
|
RECV(&info_buf, sizeof(info_buf.cmd)) {}
|
2016-12-17 19:48:07 +00:00
|
|
|
|
|
|
|
if (recvd < 0 ||
|
2016-12-18 01:33:02 +00:00
|
|
|
ntohl(info_buf.cmd[0]) != NETPLAY_CMD_INFO)
|
2016-12-17 19:48:07 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to receive netplay info.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-18 01:33:02 +00:00
|
|
|
cmd_size = ntohl(info_buf.cmd[1]);
|
|
|
|
if (cmd_size != sizeof(info_buf) - 2*sizeof(uint32_t))
|
|
|
|
{
|
|
|
|
/* Either the host doesn't have anything loaded, or this is just screwy */
|
|
|
|
if (cmd_size != 0)
|
|
|
|
{
|
|
|
|
/* Huh? */
|
|
|
|
RARCH_ERR("Invalid NETPLAY_CMD_INFO payload size.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send our info and hope they load it! */
|
|
|
|
if (!netplay_handshake_info(netplay, connection))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*had_input = true;
|
|
|
|
netplay_recv_flush(&connection->recv_packet_buffer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-28 03:06:20 +00:00
|
|
|
RECV(&info_buf.core_name, cmd_size)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to receive netplay info payload.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-17 19:48:07 +00:00
|
|
|
/* Check the core info */
|
|
|
|
core_info = NULL;
|
|
|
|
runloop_ctl(RUNLOOP_CTL_SYSTEM_INFO_GET, &core_info);
|
|
|
|
if (core_info)
|
|
|
|
{
|
|
|
|
if (strncmp(info_buf.core_name, core_info->info.library_name, sizeof(info_buf.core_name)) ||
|
|
|
|
strncmp(info_buf.core_version, core_info->info.library_version, sizeof(info_buf.core_version)))
|
|
|
|
{
|
|
|
|
dmsg = msg_hash_to_str(MSG_NETPLAY_IMPLEMENTATIONS_DIFFER);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the content CRC */
|
|
|
|
content_crc_ptr = NULL;
|
|
|
|
content_get_crc(&content_crc_ptr);
|
|
|
|
if (content_crc_ptr)
|
|
|
|
{
|
|
|
|
if (ntohl(info_buf.content_crc) != *content_crc_ptr)
|
|
|
|
{
|
|
|
|
dmsg = msg_hash_to_str(MSG_CONTENT_CRC32S_DIFFER);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now switch to the right mode */
|
|
|
|
if (netplay->is_server)
|
|
|
|
{
|
|
|
|
if (!netplay_handshake_sync(netplay, connection))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!netplay_handshake_info(netplay, connection))
|
|
|
|
return false;
|
|
|
|
connection->mode = NETPLAY_CONNECTION_PRE_SYNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
*had_input = true;
|
|
|
|
netplay_recv_flush(&connection->recv_packet_buffer);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (dmsg)
|
|
|
|
{
|
|
|
|
RARCH_ERR("%s\n", dmsg);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(dmsg, 1, 180, false);
|
2016-12-17 19:48:07 +00:00
|
|
|
}
|
2017-01-21 18:18:31 +00:00
|
|
|
|
|
|
|
if (!netplay->is_server)
|
|
|
|
{
|
|
|
|
/* Counter-intuitively, we still want to send our info. This is simply so
|
|
|
|
* that the server knows why we disconnected. */
|
|
|
|
if (!netplay_handshake_info(netplay, connection))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-17 19:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-15 13:42:03 +00:00
|
|
|
/**
|
|
|
|
* netplay_handshake_pre_sync
|
|
|
|
*
|
|
|
|
* Data receiver for the client's third handshake stage, receiving the
|
|
|
|
* synchronization information.
|
|
|
|
*/
|
|
|
|
bool netplay_handshake_pre_sync(netplay_t *netplay,
|
|
|
|
struct netplay_connection *connection, bool *had_input)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
|
|
|
uint32_t cmd[2];
|
2016-12-12 02:34:17 +00:00
|
|
|
uint32_t new_frame_count, connected_players, flip_frame;
|
2016-12-13 04:07:46 +00:00
|
|
|
uint32_t device;
|
2016-12-03 20:17:38 +00:00
|
|
|
uint32_t local_sram_size, remote_sram_size;
|
2016-12-03 23:53:57 +00:00
|
|
|
size_t i;
|
2016-12-03 20:17:38 +00:00
|
|
|
ssize_t recvd;
|
2016-12-13 04:07:46 +00:00
|
|
|
retro_ctx_controller_info_t pad;
|
2016-12-17 23:59:42 +00:00
|
|
|
char new_nick[NETPLAY_NICK_LEN];
|
2016-12-03 20:17:38 +00:00
|
|
|
retro_ctx_memory_info_t mem_info;
|
2017-02-17 00:17:06 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
RECV(cmd, sizeof(cmd))
|
2016-12-15 17:15:02 +00:00
|
|
|
{
|
2016-12-15 22:20:04 +00:00
|
|
|
const char *msg = msg_hash_to_str(MSG_NETPLAY_INCORRECT_PASSWORD);
|
2016-12-15 17:15:02 +00:00
|
|
|
RARCH_ERR("%s\n", msg);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-03 20:17:38 +00:00
|
|
|
return false;
|
2016-12-15 17:15:02 +00:00
|
|
|
}
|
2016-12-03 20:17:38 +00:00
|
|
|
|
2016-12-03 23:53:57 +00:00
|
|
|
/* Only expecting a sync command */
|
|
|
|
if (ntohl(cmd[0]) != NETPLAY_CMD_SYNC ||
|
2016-12-17 23:59:42 +00:00
|
|
|
ntohl(cmd[1]) < 3*sizeof(uint32_t) + MAX_USERS*sizeof(uint32_t) +
|
|
|
|
NETPLAY_NICK_LEN)
|
2016-12-03 20:17:38 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("%s\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_RECEIVE_SRAM_DATA_FROM_HOST));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:53:57 +00:00
|
|
|
/* Get the frame count */
|
|
|
|
RECV(&new_frame_count, sizeof(new_frame_count))
|
|
|
|
return false;
|
|
|
|
new_frame_count = ntohl(new_frame_count);
|
|
|
|
|
2016-12-18 03:41:34 +00:00
|
|
|
/* Get the connected players and pause mode */
|
2016-12-12 02:34:17 +00:00
|
|
|
RECV(&connected_players, sizeof(connected_players))
|
|
|
|
return false;
|
|
|
|
connected_players = ntohl(connected_players);
|
2016-12-18 03:41:34 +00:00
|
|
|
if (connected_players & NETPLAY_CMD_SYNC_BIT_PAUSED)
|
|
|
|
{
|
|
|
|
netplay->remote_paused = true;
|
|
|
|
connected_players ^= NETPLAY_CMD_SYNC_BIT_PAUSED;
|
|
|
|
}
|
2016-12-12 02:34:17 +00:00
|
|
|
netplay->connected_players = connected_players;
|
|
|
|
|
|
|
|
/* And the flip state */
|
|
|
|
RECV(&flip_frame, sizeof(flip_frame))
|
|
|
|
return false;
|
|
|
|
flip_frame = ntohl(flip_frame);
|
|
|
|
netplay->flip = !!flip_frame;
|
|
|
|
netplay->flip_frame = flip_frame;
|
|
|
|
|
|
|
|
/* Set our frame counters as requested */
|
2017-01-20 19:28:18 +00:00
|
|
|
netplay->self_frame_count = netplay->run_frame_count =
|
2017-01-18 21:07:17 +00:00
|
|
|
netplay->other_frame_count = netplay->unread_frame_count =
|
|
|
|
netplay->server_frame_count = new_frame_count;
|
2016-12-03 23:53:57 +00:00
|
|
|
for (i = 0; i < netplay->buffer_size; i++)
|
|
|
|
{
|
2016-12-04 01:20:16 +00:00
|
|
|
struct delta_frame *ptr = &netplay->buffer[i];
|
|
|
|
ptr->used = false;
|
|
|
|
|
2017-01-20 19:28:18 +00:00
|
|
|
if (i == netplay->self_ptr)
|
2016-12-03 23:53:57 +00:00
|
|
|
{
|
2016-12-04 01:20:16 +00:00
|
|
|
/* Clear out any current data but still use this frame */
|
2016-12-21 00:16:31 +00:00
|
|
|
if (!netplay_delta_frame_ready(netplay, ptr, 0))
|
|
|
|
return false;
|
2016-12-03 23:53:57 +00:00
|
|
|
ptr->frame = new_frame_count;
|
2016-12-04 01:20:16 +00:00
|
|
|
ptr->have_local = true;
|
2017-01-18 21:07:17 +00:00
|
|
|
netplay->run_ptr = netplay->other_ptr = netplay->unread_ptr =
|
|
|
|
netplay->server_ptr = i;
|
2016-12-04 01:20:16 +00:00
|
|
|
|
2016-12-03 23:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-12 02:34:17 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
|
|
|
{
|
|
|
|
if (connected_players & (1<<i))
|
|
|
|
{
|
2017-01-20 19:28:18 +00:00
|
|
|
netplay->read_ptr[i] = netplay->self_ptr;
|
|
|
|
netplay->read_frame_count[i] = netplay->self_frame_count;
|
2016-12-12 02:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-03 23:53:57 +00:00
|
|
|
|
2016-12-13 04:07:46 +00:00
|
|
|
/* Get and set each pad */
|
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
|
|
|
{
|
|
|
|
RECV(&device, sizeof(device))
|
|
|
|
return false;
|
2017-02-26 09:33:03 +00:00
|
|
|
pad.port = (unsigned)i;
|
2016-12-13 04:07:46 +00:00
|
|
|
pad.device = ntohl(device);
|
|
|
|
core_set_controller_port_device(&pad);
|
|
|
|
}
|
|
|
|
|
2016-12-17 23:59:42 +00:00
|
|
|
/* Get our nick */
|
|
|
|
RECV(new_nick, NETPLAY_NICK_LEN)
|
|
|
|
return false;
|
|
|
|
if (strncmp(netplay->nick, new_nick, NETPLAY_NICK_LEN))
|
|
|
|
{
|
|
|
|
char msg[512];
|
|
|
|
strlcpy(netplay->nick, new_nick, NETPLAY_NICK_LEN);
|
|
|
|
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_NETPLAY_CHANGED_NICK), netplay->nick);
|
|
|
|
RARCH_LOG("%s\n", msg);
|
2016-12-22 22:36:11 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, 180, false);
|
2016-12-17 23:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:53:57 +00:00
|
|
|
/* Now check the SRAM */
|
2017-01-15 23:10:24 +00:00
|
|
|
autosave_lock();
|
2016-12-03 20:17:38 +00:00
|
|
|
mem_info.id = RETRO_MEMORY_SAVE_RAM;
|
|
|
|
core_get_memory(&mem_info);
|
|
|
|
|
2017-02-26 09:33:03 +00:00
|
|
|
local_sram_size = (unsigned)mem_info.size;
|
2016-12-17 23:59:42 +00:00
|
|
|
remote_sram_size = ntohl(cmd[1]) - 3*sizeof(uint32_t) -
|
|
|
|
MAX_USERS*sizeof(uint32_t) - NETPLAY_NICK_LEN;
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
if (local_sram_size != 0 && local_sram_size == remote_sram_size)
|
|
|
|
{
|
|
|
|
RECV(mem_info.data, local_sram_size)
|
|
|
|
{
|
|
|
|
RARCH_ERR("%s\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_RECEIVE_SRAM_DATA_FROM_HOST));
|
2017-01-15 23:10:24 +00:00
|
|
|
autosave_unlock();
|
2016-12-03 20:17:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (remote_sram_size != 0)
|
|
|
|
{
|
|
|
|
/* We can't load this, but we still need to get rid of the data */
|
|
|
|
uint32_t quickbuf;
|
|
|
|
while (remote_sram_size > 0)
|
|
|
|
{
|
|
|
|
RECV(&quickbuf, (remote_sram_size > sizeof(uint32_t)) ? sizeof(uint32_t) : remote_sram_size)
|
|
|
|
{
|
|
|
|
RARCH_ERR("%s\n",
|
|
|
|
msg_hash_to_str(MSG_FAILED_TO_RECEIVE_SRAM_DATA_FROM_HOST));
|
2017-01-15 23:10:24 +00:00
|
|
|
autosave_unlock();
|
2016-12-03 20:17:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (remote_sram_size > sizeof(uint32_t))
|
|
|
|
remote_sram_size -= sizeof(uint32_t);
|
|
|
|
else
|
|
|
|
remote_sram_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-01-15 23:10:24 +00:00
|
|
|
autosave_unlock();
|
2016-12-03 20:17:38 +00:00
|
|
|
|
|
|
|
/* We're ready! */
|
2016-12-17 19:48:07 +00:00
|
|
|
*had_input = true;
|
2016-12-11 01:36:57 +00:00
|
|
|
netplay->self_mode = NETPLAY_CONNECTION_SPECTATING;
|
|
|
|
connection->mode = NETPLAY_CONNECTION_PLAYING;
|
2016-12-05 05:04:01 +00:00
|
|
|
netplay_handshake_ready(netplay, connection);
|
2016-12-09 19:14:54 +00:00
|
|
|
netplay_recv_flush(&connection->recv_packet_buffer);
|
2016-12-11 01:36:57 +00:00
|
|
|
|
2017-02-17 00:17:06 +00:00
|
|
|
/* Ask to switch to playing mode if we should */
|
|
|
|
if (!settings->netplay.start_as_spectator)
|
|
|
|
return netplay_cmd_mode(netplay, connection, NETPLAY_CONNECTION_PLAYING);
|
|
|
|
else
|
|
|
|
return true;
|
2016-12-03 20:17:38 +00:00
|
|
|
}
|
2016-12-17 19:48:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* netplay_handshake
|
|
|
|
*
|
|
|
|
* Data receiver for all handshake states.
|
|
|
|
*/
|
|
|
|
bool netplay_handshake(netplay_t *netplay,
|
|
|
|
struct netplay_connection *connection, bool *had_input)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
switch (connection->mode)
|
|
|
|
{
|
|
|
|
case NETPLAY_CONNECTION_NONE:
|
|
|
|
/* Huh?! */
|
|
|
|
return false;
|
|
|
|
case NETPLAY_CONNECTION_INIT:
|
|
|
|
ret = netplay_handshake_init(netplay, connection, had_input);
|
|
|
|
break;
|
|
|
|
case NETPLAY_CONNECTION_PRE_NICK:
|
|
|
|
ret = netplay_handshake_pre_nick(netplay, connection, had_input);
|
|
|
|
break;
|
|
|
|
case NETPLAY_CONNECTION_PRE_PASSWORD:
|
|
|
|
ret = netplay_handshake_pre_password(netplay, connection, had_input);
|
|
|
|
break;
|
|
|
|
case NETPLAY_CONNECTION_PRE_INFO:
|
|
|
|
ret = netplay_handshake_pre_info(netplay, connection, had_input);
|
|
|
|
break;
|
|
|
|
case NETPLAY_CONNECTION_PRE_SYNC:
|
|
|
|
ret = netplay_handshake_pre_sync(netplay, connection, had_input);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection->mode >= NETPLAY_CONNECTION_CONNECTED &&
|
|
|
|
!netplay_send_cur_input(netplay, connection))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|