forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
netplay.c
1762 lines (1445 loc) · 43.5 KB
/
netplay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
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
460
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* 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/>.
*/
#if defined(_MSC_VER) && !defined(_XBOX)
#pragma comment(lib, "ws2_32")
#endif
#include <stdlib.h>
#include <string.h>
#include <net/net_compat.h>
#include <retro_endianness.h>
#include "netplay.h"
#include "general.h"
#include "autosave.h"
#include "dynamic.h"
#include "msg_hash.h"
#include "system.h"
struct delta_frame
{
void *state;
uint16_t real_input_state;
uint16_t simulated_input_state;
uint16_t self_state;
bool is_simulated;
bool used_real;
};
#define UDP_FRAME_PACKETS 16
#define MAX_SPECTATORS 16
#define NETPLAY_CMD_ACK 0
#define NETPLAY_CMD_NAK 1
#define NETPLAY_CMD_FLIP_PLAYERS 2
#define PREV_PTR(x) ((x) == 0 ? netplay->buffer_size - 1 : (x) - 1)
#define NEXT_PTR(x) ((x + 1) % netplay->buffer_size)
struct netplay
{
char nick[32];
char other_nick[32];
struct sockaddr_storage other_addr;
struct retro_callbacks cbs;
/* TCP connection for state sending, etc. Also used for commands */
int fd;
/* UDP connection for game state updates. */
int udp_fd;
/* Which port is governed by netplay (other user)? */
unsigned port;
bool has_connection;
struct delta_frame *buffer;
size_t buffer_size;
/* Pointer where we are now. */
size_t self_ptr;
/* Points to the last reliable state that self ever had. */
size_t other_ptr;
/* Pointer to where we are reading.
* Generally, other_ptr <= read_ptr <= self_ptr. */
size_t read_ptr;
/* A temporary pointer used on replay. */
size_t tmp_ptr;
size_t state_size;
/* Are we replaying old frames? */
bool is_replay;
/* We don't want to poll several times on a frame. */
bool can_poll;
/* To compat UDP packet loss we also send
* old data along with the packets. */
uint32_t packet_buffer[UDP_FRAME_PACKETS * 2];
uint32_t frame_count;
uint32_t read_frame_count;
uint32_t other_frame_count;
uint32_t tmp_frame_count;
struct addrinfo *addr;
struct sockaddr_storage their_addr;
bool has_client_addr;
unsigned timeout_cnt;
/* Spectating. */
bool spectate;
bool spectate_client;
int spectate_fds[MAX_SPECTATORS];
uint16_t *spectate_input;
size_t spectate_input_ptr;
size_t spectate_input_size;
/* User flipping
* Flipping state. If ptr >= flip_frame, we apply the flip.
* If not, we apply the opposite, effectively creating a trigger point.
* To avoid collition we need to make sure our client/host is synced up
* well after flip_frame before allowing another flip. */
bool flip;
uint32_t flip_frame;
};
/**
* warn_hangup:
*
* Warns that netplay has disconnected.
**/
static void warn_hangup(void)
{
RARCH_WARN("Netplay has disconnected. Will continue without connection ...\n");
rarch_main_msg_queue_push("Netplay has disconnected. Will continue without connection.", 0, 480, false);
}
/**
* netplay_should_skip:
* @netplay : pointer to netplay object
*
* If we're fast-forward replaying to resync, check if we
* should actually show frame.
*
* Returns: bool (1) if we should skip this frame, otherwise
* false (0).
**/
static bool netplay_should_skip(netplay_t *netplay)
{
if (!netplay)
return false;
return netplay->is_replay && netplay->has_connection;
}
static bool netplay_can_poll(netplay_t *netplay)
{
if (!netplay)
return false;
return netplay->can_poll;
}
static bool send_chunk(netplay_t *netplay)
{
const struct sockaddr *addr = NULL;
if (netplay->addr)
addr = netplay->addr->ai_addr;
else if (netplay->has_client_addr)
addr = (const struct sockaddr*)&netplay->their_addr;
if (addr)
{
if (sendto(netplay->udp_fd, (const char*)netplay->packet_buffer,
sizeof(netplay->packet_buffer), 0, addr,
#ifdef ANDROID
sizeof(struct sockaddr_in6)
#else
sizeof(struct sockaddr_in)
#endif
)
!= sizeof(netplay->packet_buffer))
{
warn_hangup();
netplay->has_connection = false;
return false;
}
}
return true;
}
/**
* get_self_input_state:
* @netplay : pointer to netplay object
*
* Grab our own input state and send this over the network.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool get_self_input_state(netplay_t *netplay)
{
unsigned i;
uint32_t state = 0;
struct delta_frame *ptr = &netplay->buffer[netplay->self_ptr];
driver_t *driver = driver_get_ptr();
settings_t *settings = config_get_ptr();
if (!driver->block_libretro_input && netplay->frame_count > 0)
{
/* First frame we always give zero input since relying on
* input from first frame screws up when we use -F 0. */
retro_input_state_t cb = netplay->cbs.state_cb;
for (i = 0; i < RARCH_FIRST_META_KEY; i++)
{
int16_t tmp = cb(settings->input.netplay_client_swap_input ?
0 : !netplay->port,
RETRO_DEVICE_JOYPAD, 0, i);
state |= tmp ? 1 << i : 0;
}
}
memmove(netplay->packet_buffer, netplay->packet_buffer + 2,
sizeof (netplay->packet_buffer) - 2 * sizeof(uint32_t));
netplay->packet_buffer[(UDP_FRAME_PACKETS - 1) * 2] = htonl(netplay->frame_count);
netplay->packet_buffer[(UDP_FRAME_PACKETS - 1) * 2 + 1] = htonl(state);
if (!send_chunk(netplay))
{
warn_hangup();
netplay->has_connection = false;
return false;
}
ptr->self_state = state;
netplay->self_ptr = NEXT_PTR(netplay->self_ptr);
return true;
}
static bool netplay_cmd_ack(netplay_t *netplay)
{
uint32_t cmd = htonl(NETPLAY_CMD_ACK);
return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd));
}
static bool netplay_cmd_nak(netplay_t *netplay)
{
uint32_t cmd = htonl(NETPLAY_CMD_NAK);
return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd));
}
static bool netplay_get_response(netplay_t *netplay)
{
uint32_t response;
if (!socket_receive_all_blocking(netplay->fd, &response, sizeof(response)))
return false;
return ntohl(response) == NETPLAY_CMD_ACK;
}
static bool netplay_get_cmd(netplay_t *netplay)
{
uint32_t cmd, flip_frame;
size_t cmd_size;
if (!socket_receive_all_blocking(netplay->fd, &cmd, sizeof(cmd)))
return false;
cmd = ntohl(cmd);
cmd_size = cmd & 0xffff;
cmd = cmd >> 16;
switch (cmd)
{
case NETPLAY_CMD_FLIP_PLAYERS:
if (cmd_size != sizeof(uint32_t))
{
RARCH_ERR("CMD_FLIP_PLAYERS has unexpected command size.\n");
return netplay_cmd_nak(netplay);
}
if (!socket_receive_all_blocking(netplay->fd, &flip_frame, sizeof(flip_frame)))
{
RARCH_ERR("Failed to receive CMD_FLIP_PLAYERS argument.\n");
return netplay_cmd_nak(netplay);
}
flip_frame = ntohl(flip_frame);
if (flip_frame < netplay->flip_frame)
{
RARCH_ERR("Host asked us to flip users in the past. Not possible ...\n");
return netplay_cmd_nak(netplay);
}
netplay->flip ^= true;
netplay->flip_frame = flip_frame;
RARCH_LOG("Netplay users are flipped.\n");
rarch_main_msg_queue_push("Netplay users are flipped.", 1, 180, false);
return netplay_cmd_ack(netplay);
default:
break;
}
RARCH_ERR("Unknown netplay command received.\n");
return netplay_cmd_nak(netplay);
}
#define MAX_RETRIES 16
#define RETRY_MS 500
static int poll_input(netplay_t *netplay, bool block)
{
int max_fd = (netplay->fd > netplay->udp_fd ? netplay->fd : netplay->udp_fd) + 1;
struct timeval tv = {0};
tv.tv_sec = 0;
tv.tv_usec = block ? (RETRY_MS * 1000) : 0;
do
{
fd_set fds;
/* select() does not take pointer to const struct timeval.
* Technically possible for select() to modify tmp_tv, so
* we go paranoia mode. */
struct timeval tmp_tv = tv;
netplay->timeout_cnt++;
FD_ZERO(&fds);
FD_SET(netplay->udp_fd, &fds);
FD_SET(netplay->fd, &fds);
if (socket_select(max_fd, &fds, NULL, NULL, &tmp_tv) < 0)
return -1;
/* Somewhat hacky,
* but we aren't using the TCP connection for anything useful atm. */
if (FD_ISSET(netplay->fd, &fds) && !netplay_get_cmd(netplay))
return -1;
if (FD_ISSET(netplay->udp_fd, &fds))
return 1;
if (!block)
continue;
if (!send_chunk(netplay))
{
warn_hangup();
netplay->has_connection = false;
return -1;
}
RARCH_LOG("Network is stalling, resending packet... Count %u of %d ...\n",
netplay->timeout_cnt, MAX_RETRIES);
} while ((netplay->timeout_cnt < MAX_RETRIES) && block);
if (block)
return -1;
return 0;
}
static bool receive_data(netplay_t *netplay, uint32_t *buffer, size_t size)
{
socklen_t addrlen = sizeof(netplay->their_addr);
if (recvfrom(netplay->udp_fd, (char*)buffer, size, 0,
(struct sockaddr*)&netplay->their_addr, &addrlen) != (ssize_t)size)
return false;
netplay->has_client_addr = true;
return true;
}
static void parse_packet(netplay_t *netplay, uint32_t *buffer, unsigned size)
{
unsigned i;
for (i = 0; i < size * 2; i++)
buffer[i] = ntohl(buffer[i]);
for (i = 0; i < size && netplay->read_frame_count <= netplay->frame_count; i++)
{
uint32_t frame = buffer[2 * i + 0];
uint32_t state = buffer[2 * i + 1];
if (frame != netplay->read_frame_count)
continue;
netplay->buffer[netplay->read_ptr].is_simulated = false;
netplay->buffer[netplay->read_ptr].real_input_state = state;
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
netplay->read_frame_count++;
netplay->timeout_cnt = 0;
}
}
/* TODO: Somewhat better prediction. :P */
static void simulate_input(netplay_t *netplay)
{
size_t ptr = PREV_PTR(netplay->self_ptr);
size_t prev = PREV_PTR(netplay->read_ptr);
netplay->buffer[ptr].simulated_input_state =
netplay->buffer[prev].real_input_state;
netplay->buffer[ptr].is_simulated = true;
netplay->buffer[ptr].used_real = false;
}
/**
* netplay_poll:
* @netplay : pointer to netplay object
*
* Polls network to see if we have anything new. If our
* network buffer is full, we simply have to block
* for new input data.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool netplay_poll(netplay_t *netplay)
{
int res;
if (!netplay->has_connection)
return false;
netplay->can_poll = false;
if (!get_self_input_state(netplay))
return false;
/* We skip reading the first frame so the host has a chance to grab
* our host info so we don't block forever :') */
if (netplay->frame_count == 0)
{
netplay->buffer[0].used_real = true;
netplay->buffer[0].is_simulated = false;
netplay->buffer[0].real_input_state = 0;
netplay->read_ptr = NEXT_PTR(netplay->read_ptr);
netplay->read_frame_count++;
return true;
}
/* We might have reached the end of the buffer, where we
* simply have to block. */
res = poll_input(netplay, netplay->other_ptr == netplay->self_ptr);
if (res == -1)
{
netplay->has_connection = false;
warn_hangup();
return false;
}
if (res == 1)
{
uint32_t first_read = netplay->read_frame_count;
do
{
uint32_t buffer[UDP_FRAME_PACKETS * 2];
if (!receive_data(netplay, buffer, sizeof(buffer)))
{
warn_hangup();
netplay->has_connection = false;
return false;
}
parse_packet(netplay, buffer, UDP_FRAME_PACKETS);
} while ((netplay->read_frame_count <= netplay->frame_count) &&
poll_input(netplay, (netplay->other_ptr == netplay->self_ptr) &&
(first_read == netplay->read_frame_count)) == 1);
}
else
{
/* Cannot allow this. Should not happen though. */
if (netplay->self_ptr == netplay->other_ptr)
{
warn_hangup();
return false;
}
}
if (netplay->read_ptr != netplay->self_ptr)
simulate_input(netplay);
else
netplay->buffer[PREV_PTR(netplay->self_ptr)].used_real = true;
return true;
}
void input_poll_net(void)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay) && netplay_can_poll(netplay))
netplay_poll(netplay);
}
void video_frame_net(const void *data, unsigned width,
unsigned height, size_t pitch)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay))
netplay->cbs.frame_cb(data, width, height, pitch);
}
void audio_sample_net(int16_t left, int16_t right)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay))
netplay->cbs.sample_cb(left, right);
}
size_t audio_sample_batch_net(const int16_t *data, size_t frames)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (!netplay_should_skip(netplay))
return netplay->cbs.sample_batch_cb(data, frames);
return frames;
}
/**
* netplay_is_alive:
* @netplay : pointer to netplay object
*
* Checks if input port/index is controlled by netplay or not.
*
* Returns: true (1) if alive, otherwise false (0).
**/
static bool netplay_is_alive(netplay_t *netplay)
{
if (!netplay)
return false;
return netplay->has_connection;
}
static bool netplay_flip_port(netplay_t *netplay, bool port)
{
size_t frame = netplay->frame_count;
if (netplay->flip_frame == 0)
return port;
if (netplay->is_replay)
frame = netplay->tmp_frame_count;
return port ^ netplay->flip ^ (frame < netplay->flip_frame);
}
static int16_t netplay_input_state(netplay_t *netplay, bool port, unsigned device,
unsigned idx, unsigned id)
{
size_t ptr = netplay->is_replay ?
netplay->tmp_ptr : PREV_PTR(netplay->self_ptr);
uint16_t curr_input_state = netplay->buffer[ptr].self_state;
if (netplay->port == (netplay_flip_port(netplay, port) ? 1 : 0))
{
if (netplay->buffer[ptr].is_simulated)
curr_input_state = netplay->buffer[ptr].simulated_input_state;
else
curr_input_state = netplay->buffer[ptr].real_input_state;
}
return ((1 << id) & curr_input_state) ? 1 : 0;
}
int16_t input_state_net(unsigned port, unsigned device,
unsigned idx, unsigned id)
{
driver_t *driver = driver_get_ptr();
netplay_t *netplay = (netplay_t*)driver->netplay_data;
if (netplay_is_alive(netplay))
return netplay_input_state(netplay, port, device, idx, id);
return netplay->cbs.state_cb(port, device, idx, id);
}
#ifndef HAVE_SOCKET_LEGACY
/* Custom inet_ntop. Win32 doesn't seem to support this ... */
static void 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};
u.storage = their_addr;
if (their_addr->ss_family == AF_INET)
{
struct sockaddr_in in;
str = buf_v4;
memset(&in, 0, sizeof(in));
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);
}
else if (their_addr->ss_family == AF_INET6)
{
struct sockaddr_in6 in;
str = buf_v6;
memset(&in, 0, sizeof(in));
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);
}
if (str)
{
char msg[512] = {0};
snprintf(msg, sizeof(msg), "Got connection from: \"%s (%s)\" (#%u)",
nick, str, slot);
rarch_main_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
}
}
#endif
static int init_tcp_connection(const struct addrinfo *res,
bool server, bool spectate,
struct sockaddr *other_addr, socklen_t addr_size)
{
bool ret = true;
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd < 0)
{
ret = false;
goto end;
}
if (server)
{
if (connect(fd, res->ai_addr, res->ai_addrlen) < 0)
{
ret = false;
goto end;
}
}
else
{
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(int));
if (bind(fd, res->ai_addr, res->ai_addrlen) < 0 ||
listen(fd, spectate ? MAX_SPECTATORS : 1) < 0)
{
ret = false;
goto end;
}
if (!spectate)
{
int new_fd = accept(fd, other_addr, &addr_size);
if (new_fd < 0)
{
ret = false;
goto end;
}
socket_close(fd);
fd = new_fd;
}
}
end:
if (!ret && fd >= 0)
{
socket_close(fd);
fd = -1;
}
return fd;
}
static bool init_tcp_socket(netplay_t *netplay, const char *server,
uint16_t port, bool spectate)
{
char port_buf[16] = {0};
bool ret = false;
const struct addrinfo *tmp_info = NULL;
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof(hints));
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_STREAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo_rarch(server, port_buf, &hints, &res) < 0)
return false;
if (!res)
return false;
/* If "localhost" is used, it is important to check every possible
* address for IPv4/IPv6. */
tmp_info = res;
while (tmp_info)
{
int fd;
if ((fd = init_tcp_connection(tmp_info, server, netplay->spectate,
(struct sockaddr*)&netplay->other_addr,
sizeof(netplay->other_addr))) >= 0)
{
ret = true;
netplay->fd = fd;
break;
}
tmp_info = tmp_info->ai_next;
}
if (res)
freeaddrinfo_rarch(res);
if (!ret)
RARCH_ERR("Failed to set up netplay sockets.\n");
return ret;
}
static bool init_udp_socket(netplay_t *netplay, const char *server,
uint16_t port)
{
char port_buf[16] = {0};
struct addrinfo hints = {0};
memset(&hints, 0, sizeof(hints));
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_DGRAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo_rarch(server, port_buf, &hints, &netplay->addr) < 0)
return false;
if (!netplay->addr)
return false;
netplay->udp_fd = socket(netplay->addr->ai_family,
netplay->addr->ai_socktype, netplay->addr->ai_protocol);
if (netplay->udp_fd < 0)
{
RARCH_ERR("Failed to initialize socket.\n");
return false;
}
if (!server)
{
/* Not sure if we have to do this for UDP, but hey :) */
int yes = 1;
setsockopt(netplay->udp_fd, SOL_SOCKET, SO_REUSEADDR,
(const char*)&yes, sizeof(int));
if (bind(netplay->udp_fd, netplay->addr->ai_addr,
netplay->addr->ai_addrlen) < 0)
{
RARCH_ERR("Failed to bind socket.\n");
socket_close(netplay->udp_fd);
netplay->udp_fd = -1;
}
freeaddrinfo_rarch(netplay->addr);
netplay->addr = NULL;
}
return true;
}
static bool init_socket(netplay_t *netplay, const char *server, uint16_t port)
{
if (!network_init())
return false;
if (!init_tcp_socket(netplay, server, port, netplay->spectate))
return false;
if (!netplay->spectate && !init_udp_socket(netplay, server, port))
return false;
return true;
}
/**
* implementation_magic_value:
*
* Not really a hash, but should be enough to differentiate
* implementations from each other.
*
* Subtle differences in the implementation will not be possible to spot.
* The alternative would have been checking serialization sizes, but it
* was troublesome for cross platform compat.
**/
static uint32_t implementation_magic_value(void)
{
size_t i, len;
uint32_t res = 0;
const char *ver = PACKAGE_VERSION;
unsigned api = pretro_api_version();
rarch_system_info_t *info = rarch_system_info_get_ptr();
const char *lib = info ? info->info.library_name : NULL;
res |= api;
len = strlen(lib);
for (i = 0; i < len; i++)
res ^= lib[i] << (i & 0xf);
lib = info->info.library_version;
len = strlen(lib);
for (i = 0; i < len; i++)
res ^= lib[i] << (i & 0xf);
len = strlen(ver);
for (i = 0; i < len; i++)
res ^= ver[i] << ((i & 0xf) + 16);
return res;
}
static bool send_nickname(netplay_t *netplay, int fd)
{
uint8_t nick_size = strlen(netplay->nick);
if (!socket_send_all_blocking(fd, &nick_size, sizeof(nick_size)))
{
RARCH_ERR("Failed to send nick size.\n");
return false;
}
if (!socket_send_all_blocking(fd, netplay->nick, nick_size))
{
RARCH_ERR("Failed to send nick.\n");
return false;
}
return true;
}
static bool get_nickname(netplay_t *netplay, int fd)
{
uint8_t nick_size;
if (!socket_receive_all_blocking(fd, &nick_size, sizeof(nick_size)))
{
RARCH_ERR("Failed to receive nick size from host.\n");
return false;
}
if (nick_size >= sizeof(netplay->other_nick))
{
RARCH_ERR("Invalid nick size.\n");
return false;
}
if (!socket_receive_all_blocking(fd, netplay->other_nick, nick_size))
{
RARCH_ERR("Failed to receive nick.\n");
return false;
}
return true;
}
static bool send_info(netplay_t *netplay)
{
unsigned sram_size;
char msg[512] = {0};
void *sram = NULL;
uint32_t header[3] = {0};
global_t *global = global_get_ptr();
header[0] = htonl(global->content_crc);
header[1] = htonl(implementation_magic_value());
header[2] = htonl(pretro_get_memory_size(RETRO_MEMORY_SAVE_RAM));
if (!socket_send_all_blocking(netplay->fd, header, sizeof(header)))
return false;
if (!send_nickname(netplay, netplay->fd))
{
RARCH_ERR("Failed to send nick to host.\n");
return false;
}
/* Get SRAM data from User 1. */
sram = pretro_get_memory_data(RETRO_MEMORY_SAVE_RAM);
sram_size = pretro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
if (!socket_receive_all_blocking(netplay->fd, sram, sram_size))
{
RARCH_ERR("Failed to receive SRAM data from host.\n");
return false;
}
if (!get_nickname(netplay, netplay->fd))
{
RARCH_ERR("Failed to receive nick from host.\n");
return false;
}
snprintf(msg, sizeof(msg), "Connected to: \"%s\"", netplay->other_nick);
RARCH_LOG("%s\n", msg);
rarch_main_msg_queue_push(msg, 1, 180, false);
return true;
}
static bool get_info(netplay_t *netplay)
{
unsigned sram_size;
uint32_t header[3];
const void *sram = NULL;
global_t *global = global_get_ptr();
if (!socket_receive_all_blocking(netplay->fd, header, sizeof(header)))
{
RARCH_ERR("Failed to receive header from client.\n");
return false;
}
if (global->content_crc != ntohl(header[0]))
{
RARCH_ERR("Content CRC32s differ. Cannot use different games.\n");
return false;
}
if (implementation_magic_value() != ntohl(header[1]))
{
RARCH_ERR("Implementations differ, make sure you're using exact same libretro implementations and RetroArch version.\n");
return false;
}
if (pretro_get_memory_size(RETRO_MEMORY_SAVE_RAM) != ntohl(header[2]))
{
RARCH_ERR("Content SRAM sizes do not correspond.\n");
return false;
}
if (!get_nickname(netplay, netplay->fd))
{
RARCH_ERR("Failed to get nickname from client.\n");
return false;
}
/* Send SRAM data to our User 2. */
sram = pretro_get_memory_data(RETRO_MEMORY_SAVE_RAM);
sram_size = pretro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
if (!socket_send_all_blocking(netplay->fd, sram, sram_size))
{
RARCH_ERR("Failed to send SRAM data to client.\n");
return false;
}
if (!send_nickname(netplay, netplay->fd))
{
RARCH_ERR("Failed to send nickname to client.\n");
return false;
}
#ifndef HAVE_SOCKET_LEGACY
log_connection(&netplay->other_addr, 0, netplay->other_nick);
#endif
return true;
}