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.
2018-02-16 05:59:21 +08:00
|
|
|
#include "net.h"
|
|
|
|
|
2018-02-16 21:55:33 +08:00
|
|
|
#include <stdio.h>
|
2019-12-16 04:55:43 +08:00
|
|
|
#include <SDL2/SDL_platform.h>
|
2018-02-16 21:55:33 +08:00
|
|
|
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
typedef int socklen_t;
|
2021-10-27 04:49:45 +08:00
|
|
|
typedef SOCKET sc_raw_socket;
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
#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;
|
2021-10-27 04:49:45 +08:00
|
|
|
typedef int sc_raw_socket;
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
#endif
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
bool
|
|
|
|
net_init(void) {
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
WSADATA wsa;
|
|
|
|
int res = WSAStartup(MAKEWORD(2, 2), &wsa) < 0;
|
|
|
|
if (res < 0) {
|
|
|
|
LOGC("WSAStartup failed with error %d", res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
net_cleanup(void) {
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
static inline sc_socket
|
|
|
|
wrap(sc_raw_socket sock) {
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
if (sock == INVALID_SOCKET) {
|
|
|
|
return SC_INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sc_socket_windows *socket = malloc(sizeof(*socket));
|
|
|
|
if (!socket) {
|
|
|
|
closesocket(sock);
|
|
|
|
return SC_INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket->socket = sock;
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
#else
|
|
|
|
return sock;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline sc_raw_socket
|
|
|
|
unwrap(sc_socket socket) {
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
if (socket == SC_INVALID_SOCKET) {
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
return socket->socket;
|
|
|
|
#else
|
|
|
|
return socket;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-10 00:07:38 +08:00
|
|
|
static void
|
|
|
|
net_perror(const char *s) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
int error = WSAGetLastError();
|
|
|
|
char *wsa_message;
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(char *) &wsa_message, 0, NULL);
|
|
|
|
// no explicit '\n', wsa_message already contains a trailing '\n'
|
|
|
|
fprintf(stderr, "%s: [%d] %s", s, error, wsa_message);
|
|
|
|
LocalFree(wsa_message);
|
|
|
|
#else
|
|
|
|
perror(s);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_socket
|
2019-03-03 06:52:22 +08:00
|
|
|
net_connect(uint32_t addr, uint16_t port) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
sc_socket sock = wrap(raw_sock);
|
2021-10-27 04:49:45 +08:00
|
|
|
if (sock == SC_INVALID_SOCKET) {
|
2021-09-10 00:07:38 +08:00
|
|
|
net_perror("socket");
|
2021-10-27 04:49:45 +08:00
|
|
|
return SC_INVALID_SOCKET;
|
2018-03-12 15:35:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SOCKADDR_IN sin;
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = htonl(addr);
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
if (connect(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
2021-09-10 00:07:38 +08:00
|
|
|
net_perror("connect");
|
2019-06-01 05:32:53 +08:00
|
|
|
net_close(sock);
|
2021-10-27 04:49:45 +08:00
|
|
|
return SC_INVALID_SOCKET;
|
2018-03-12 15:35:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_socket
|
2019-03-03 06:52:22 +08:00
|
|
|
net_listen(uint32_t addr, uint16_t port, int backlog) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
sc_socket sock = wrap(raw_sock);
|
2021-10-27 04:49:45 +08:00
|
|
|
if (sock == SC_INVALID_SOCKET) {
|
2021-09-10 00:07:38 +08:00
|
|
|
net_perror("socket");
|
2021-10-27 04:49:45 +08:00
|
|
|
return SC_INVALID_SOCKET;
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 22:05:43 +08:00
|
|
|
int reuse = 1;
|
2021-10-27 04:49:45 +08:00
|
|
|
if (setsockopt(raw_sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
|
2019-03-03 03:09:56 +08:00
|
|
|
sizeof(reuse)) == -1) {
|
2021-09-10 00:07:38 +08:00
|
|
|
net_perror("setsockopt(SO_REUSEADDR)");
|
2018-02-16 22:05:43 +08:00
|
|
|
}
|
|
|
|
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
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);
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
if (bind(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
2021-09-10 00:07:38 +08:00
|
|
|
net_perror("bind");
|
2019-06-01 05:32:53 +08:00
|
|
|
net_close(sock);
|
2021-10-27 04:49:45 +08:00
|
|
|
return SC_INVALID_SOCKET;
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
if (listen(raw_sock, backlog) == SOCKET_ERROR) {
|
2021-09-10 00:07:38 +08:00
|
|
|
net_perror("listen");
|
2019-06-01 05:32:53 +08:00
|
|
|
net_close(sock);
|
2021-10-27 04:49:45 +08:00
|
|
|
return SC_INVALID_SOCKET;
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_socket
|
|
|
|
net_accept(sc_socket server_socket) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_server_socket = unwrap(server_socket);
|
|
|
|
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
SOCKADDR_IN csin;
|
|
|
|
socklen_t sinsize = sizeof(csin);
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock =
|
|
|
|
accept(raw_server_socket, (SOCKADDR *) &csin, &sinsize);
|
|
|
|
|
|
|
|
return wrap(raw_sock);
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
ssize_t
|
2021-10-27 04:49:45 +08:00
|
|
|
net_recv(sc_socket socket, void *buf, size_t len) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock = unwrap(socket);
|
|
|
|
return recv(raw_sock, buf, len, 0);
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
ssize_t
|
2021-10-27 04:49:45 +08:00
|
|
|
net_recv_all(sc_socket socket, void *buf, size_t len) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock = unwrap(socket);
|
|
|
|
return recv(raw_sock, buf, len, MSG_WAITALL);
|
2018-02-16 06:55:52 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
ssize_t
|
2021-10-27 04:49:45 +08:00
|
|
|
net_send(sc_socket socket, const void *buf, size_t len) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock = unwrap(socket);
|
|
|
|
return send(raw_sock, buf, len, 0);
|
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.
2018-02-16 05:59:21 +08:00
|
|
|
}
|
2018-02-16 06:55:52 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
ssize_t
|
2021-10-27 04:49:45 +08:00
|
|
|
net_send_all(sc_socket socket, const void *buf, size_t len) {
|
2021-07-16 00:16:26 +08:00
|
|
|
size_t copied = 0;
|
2018-02-16 06:55:52 +08:00
|
|
|
while (len > 0) {
|
2021-10-27 04:49:45 +08:00
|
|
|
ssize_t w = net_send(socket, buf, len);
|
2018-02-16 06:55:52 +08:00
|
|
|
if (w == -1) {
|
2021-07-16 00:16:26 +08:00
|
|
|
return copied ? (ssize_t) copied : -1;
|
2018-02-16 06:55:52 +08:00
|
|
|
}
|
|
|
|
len -= w;
|
2018-04-04 16:48:14 +08:00
|
|
|
buf = (char *) buf + w;
|
2021-07-16 00:16:26 +08:00
|
|
|
copied += w;
|
2018-02-16 06:55:52 +08:00
|
|
|
}
|
2021-07-16 00:16:26 +08:00
|
|
|
return copied;
|
2018-02-16 06:55:52 +08:00
|
|
|
}
|
2018-02-16 18:11:07 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2021-10-27 04:49:45 +08:00
|
|
|
net_shutdown(sc_socket socket, int how) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock = unwrap(socket);
|
|
|
|
return !shutdown(raw_sock, how);
|
2018-02-16 18:11:07 +08:00
|
|
|
}
|
2019-12-16 04:55:43 +08:00
|
|
|
|
|
|
|
bool
|
2021-10-27 04:49:45 +08:00
|
|
|
net_close(sc_socket socket) {
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_raw_socket raw_sock = unwrap(socket);
|
|
|
|
|
2019-12-16 04:55:43 +08:00
|
|
|
#ifdef __WINDOWS__
|
2021-10-27 04:49:45 +08:00
|
|
|
free(socket);
|
|
|
|
return !closesocket(raw_sock);
|
2019-12-16 04:55:43 +08:00
|
|
|
#else
|
2021-10-27 04:49:45 +08:00
|
|
|
return !close(raw_sock);
|
2019-12-16 04:55:43 +08:00
|
|
|
#endif
|
|
|
|
}
|