Replace SDL_net by custom implementation

SDL_net is not very suitable for scrcpy.

For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.

But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.

This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)

Provide our own simplified API (net.h) instead, implemented for the
different platforms.
This commit is contained in:
Romain Vimont
2018-02-15 22:59:21 +01:00
parent bf41e5479b
commit 9b056f5091
18 changed files with 178 additions and 95 deletions

View File

@@ -1,20 +1,20 @@
#ifndef SERVER_H
#define SERVER_H
#include <SDL2/SDL_net.h>
#include "command.h"
#include "net.h"
struct server {
process_t process;
TCPsocket server_socket;
TCPsocket device_socket;
socket_t server_socket;
socket_t device_socket;
SDL_bool adb_reverse_enabled;
};
#define SERVER_INITIALIZER { \
.process = PROCESS_NONE, \
.server_socket = NULL, \
.device_socket = NULL, \
.server_socket = INVALID_SOCKET, \
.device_socket = INVALID_SOCKET, \
.adb_reverse_enabled = SDL_FALSE, \
}
@@ -26,7 +26,7 @@ SDL_bool server_start(struct server *server, const char *serial, Uint16 local_po
Uint16 max_size, Uint32 bit_rate);
// block until the communication with the server is established
TCPsocket server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms);
socket_t server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms);
// disconnect and kill the server process
void server_stop(struct server *server, const char *serial);