mirror of https://github.com/Genymobile/scrcpy
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.hidpi
parent
bf41e5479b
commit
9b056f5091
@ -1,14 +1,14 @@
|
||||
#ifndef DEVICE_H
|
||||
#define DEVICE_H
|
||||
|
||||
#include <SDL2/SDL_net.h>
|
||||
#include <SDL2/SDL_stdinc.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "net.h"
|
||||
|
||||
#define DEVICE_NAME_FIELD_LENGTH 64
|
||||
|
||||
// name must be at least DEVICE_NAME_FIELD_LENGTH bytes
|
||||
SDL_bool device_read_info(TCPsocket device_socket, char *name, struct size *frame_size);
|
||||
SDL_bool device_read_info(socket_t device_socket, char *name, struct size *frame_size);
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,56 @@
|
||||
#include "net.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
typedef int socklen_t;
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
# include <sys/socket.h>
|
||||
# include <netinet/in.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <unistd.h>
|
||||
# define SOCKET_ERROR -1
|
||||
typedef struct sockaddr_in SOCKADDR_IN;
|
||||
typedef struct sockaddr SOCKADDR;
|
||||
typedef struct in_addr IN_ADDR;
|
||||
#endif
|
||||
|
||||
socket_t net_listen(Uint32 addr, Uint16 port, int backlog) {
|
||||
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
LOGE("Cannot create socket");
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = htonl(addr); // htonl() harmless on INADDR_ANY
|
||||
sin.sin_port = htons(port);
|
||||
|
||||
if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
LOGE("Cannot bind");
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (listen(sock, backlog) == SOCKET_ERROR) {
|
||||
LOGE("Cannot listen on port %" PRIu16, port);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
socket_t net_accept(socket_t server_socket) {
|
||||
SOCKADDR_IN csin;
|
||||
socklen_t sinsize = sizeof(csin);
|
||||
return accept(server_socket, (SOCKADDR *) &csin, &sinsize);
|
||||
}
|
||||
|
||||
ssize_t net_recv(socket_t socket, void *buf, size_t len) {
|
||||
return recv(socket, buf, len, 0);
|
||||
}
|
||||
|
||||
ssize_t net_send(socket_t socket, void *buf, size_t len) {
|
||||
return send(socket, buf, len, 0);
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#ifndef NET_H
|
||||
#define NET_H
|
||||
|
||||
#include <SDL2/SDL_platform.h>
|
||||
#include <SDL2/SDL_stdinc.h>
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
# include <winsock2.h>
|
||||
typedef SIZE_T size_t;
|
||||
typedef SSIZE_T ssize_t;
|
||||
typedef SOCKET socket_t;
|
||||
#else
|
||||
# define INVALID_SOCKET -1
|
||||
typedef int socket_t;
|
||||
#endif
|
||||
|
||||
SDL_bool net_init(void);
|
||||
void net_cleanup(void);
|
||||
|
||||
socket_t net_listen(Uint32 addr, Uint16 port, int backlog);
|
||||
socket_t net_accept(socket_t server_socket);
|
||||
ssize_t net_recv(socket_t socket, void *buf, size_t len);
|
||||
ssize_t net_send(socket_t socket, void *buf, size_t len);
|
||||
void net_close(socket_t socket);
|
||||
|
||||
#endif
|
@ -1,31 +0,0 @@
|
||||
#include "netutil.h"
|
||||
|
||||
#include <SDL2/SDL_net.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
// contrary to SDLNet_TCP_Send and SDLNet_TCP_Recv, SDLNet_TCP_Accept is non-blocking
|
||||
// so we need to block before calling it
|
||||
TCPsocket server_socket_accept(TCPsocket server_socket, Uint32 timeout_ms) {
|
||||
SDLNet_SocketSet set = SDLNet_AllocSocketSet(1);
|
||||
if (!set) {
|
||||
LOGC("Could not allocate socket set");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (SDLNet_TCP_AddSocket(set, server_socket) == -1) {
|
||||
LOGC("Could not add socket to set");
|
||||
SDLNet_FreeSocketSet(set);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (SDLNet_CheckSockets(set, timeout_ms) != 1) {
|
||||
LOGE("No connection to accept");
|
||||
SDLNet_FreeSocketSet(set);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDLNet_FreeSocketSet(set);
|
||||
|
||||
return SDLNet_TCP_Accept(server_socket);
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
#ifndef NETUTIL_H
|
||||
#define NETUTIL_H
|
||||
|
||||
#include <SDL2/SDL_net.h>
|
||||
|
||||
// blocking accept on the server socket
|
||||
TCPsocket server_socket_accept(TCPsocket server_socket, Uint32 timeout_ms);
|
||||
|
||||
#endif
|
@ -0,0 +1,16 @@
|
||||
#include "../../net.h"
|
||||
|
||||
# include <unistd.h>
|
||||
|
||||
SDL_bool net_init(void) {
|
||||
// do nothing
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
void net_cleanup(void) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
void net_close(socket_t socket) {
|
||||
close(socket);
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
#include "../../net.h"
|
||||
|
||||
#include "../../log.h"
|
||||
|
||||
SDL_bool net_init(void) {
|
||||
WSADATA wsa;
|
||||
int res = WSAStartup(MAKEWORD(2, 2), &wsa) < 0;
|
||||
if (res < 0) {
|
||||
LOGC("WSAStartup failed with error %d", res);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
void net_cleanup(void) {
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
void net_close(socket_t socket) {
|
||||
closesocket(socket);
|
||||
}
|
Loading…
Reference in New Issue