2018-02-08 22:42:49 +08:00
|
|
|
#include "device.h"
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
2018-02-08 22:42:49 +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
|
|
|
SDL_bool device_read_info(socket_t device_socket, char *device_name, struct size *size) {
|
2018-02-08 22:42:49 +08:00
|
|
|
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4];
|
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
|
|
|
if (net_recv(device_socket, buf, sizeof(buf)) <= 0) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGE("Could not retrieve device information");
|
2018-02-08 22:42:49 +08:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0'; // in case the client sends garbage
|
|
|
|
// strcpy is safe here, since name contains at least DEVICE_NAME_FIELD_LENGTH bytes
|
|
|
|
// and strlen(buf) < DEVICE_NAME_FIELD_LENGTH
|
|
|
|
strcpy(device_name, (char *) buf);
|
|
|
|
size->width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8) | buf[DEVICE_NAME_FIELD_LENGTH + 1];
|
|
|
|
size->height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8) | buf[DEVICE_NAME_FIELD_LENGTH + 3];
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|