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
|
|
|
#ifndef NET_H
|
|
|
|
#define NET_H
|
|
|
|
|
2021-01-09 02:24:51 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
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 <SDL2/SDL_platform.h>
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2021-10-27 04:49:45 +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 <winsock2.h>
|
2021-10-27 04:49:45 +08:00
|
|
|
# include <stdatomic.h>
|
2021-10-27 04:49:45 +08:00
|
|
|
# define SC_INVALID_SOCKET NULL
|
|
|
|
typedef struct sc_socket_windows {
|
|
|
|
SOCKET socket;
|
2021-10-27 04:49:45 +08:00
|
|
|
atomic_flag closed;
|
2021-10-27 04:49:45 +08:00
|
|
|
} *sc_socket;
|
2021-10-27 04:49:45 +08:00
|
|
|
|
|
|
|
#else // not __WINDOWS__
|
|
|
|
|
2018-02-16 18:11:07 +08:00
|
|
|
# include <sys/socket.h>
|
2021-10-27 04:49:45 +08:00
|
|
|
# define SC_INVALID_SOCKET -1
|
|
|
|
typedef int sc_socket;
|
2021-11-13 01:59:56 +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
|
|
|
#endif
|
|
|
|
|
2021-11-13 01:59:56 +08:00
|
|
|
#define IPV4_LOCALHOST 0x7F000001
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
net_init(void);
|
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
|
|
|
void
|
|
|
|
net_cleanup(void);
|
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_socket
|
2021-11-01 05:01:57 +08:00
|
|
|
net_socket(void);
|
2019-03-03 03:09:56 +08:00
|
|
|
|
2021-11-01 05:01:57 +08:00
|
|
|
bool
|
|
|
|
net_connect(sc_socket socket, uint32_t addr, uint16_t port);
|
|
|
|
|
|
|
|
bool
|
|
|
|
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog);
|
2019-03-03 03:09:56 +08:00
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
sc_socket
|
|
|
|
net_accept(sc_socket server_socket);
|
2018-02-16 06:55:52 +08:00
|
|
|
|
|
|
|
// the _all versions wait/retry until len bytes have been written/read
|
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);
|
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);
|
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);
|
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);
|
2019-03-03 03:09:56 +08:00
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
// Shutdown the socket (or close on Windows) so that any blocking send() or
|
|
|
|
// recv() are interrupted.
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2021-10-27 04:49:45 +08:00
|
|
|
net_interrupt(sc_socket socket);
|
2019-03-03 03:09:56 +08:00
|
|
|
|
2021-10-27 04:49:45 +08:00
|
|
|
// Close the socket.
|
|
|
|
// A socket must always be closed, even if net_interrupt() has been called.
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2021-10-27 04:49:45 +08:00
|
|
|
net_close(sc_socket 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
|