Compare commits
7 Commits
server_thr
...
server_thr
Author | SHA1 | Date | |
---|---|---|---|
2f816fdfc2 | |||
91e5e68582 | |||
0bc1882179 | |||
52eb960dd0 | |||
34b92440e2 | |||
e23daebe06 | |||
0b616634c3 |
@ -148,10 +148,10 @@ disable_tunnel(struct server *server) {
|
|||||||
return disable_tunnel_reverse(serial);
|
return disable_tunnel_reverse(serial);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static sc_socket
|
||||||
listen_on_port(sc_socket socket, uint16_t port) {
|
listen_on_port(uint16_t port) {
|
||||||
#define IPV4_LOCALHOST 0x7F000001
|
#define IPV4_LOCALHOST 0x7F000001
|
||||||
return net_listen(socket, IPV4_LOCALHOST, port, 1);
|
return net_listen(IPV4_LOCALHOST, port, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@ -171,19 +171,13 @@ enable_tunnel_reverse_any_port(struct server *server,
|
|||||||
// client can listen before starting the server app, so there is no
|
// client can listen before starting the server app, so there is no
|
||||||
// need to try to connect until the server socket is listening on the
|
// need to try to connect until the server socket is listening on the
|
||||||
// device.
|
// device.
|
||||||
sc_socket server_socket = net_socket();
|
server->server_socket = listen_on_port(port);
|
||||||
if (server_socket != SC_INVALID_SOCKET) {
|
if (server->server_socket != SC_INVALID_SOCKET) {
|
||||||
bool ok = listen_on_port(server_socket, port);
|
|
||||||
if (ok) {
|
|
||||||
// success
|
// success
|
||||||
server->server_socket = server_socket;
|
|
||||||
server->local_port = port;
|
server->local_port = port;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
net_close(server_socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
// failure, disable tunnel and try another port
|
// failure, disable tunnel and try another port
|
||||||
if (!disable_tunnel_reverse(serial)) {
|
if (!disable_tunnel_reverse(serial)) {
|
||||||
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
||||||
@ -341,17 +335,11 @@ execute_server(struct server *server, const struct server_params *params) {
|
|||||||
|
|
||||||
static sc_socket
|
static sc_socket
|
||||||
connect_and_read_byte(uint16_t port) {
|
connect_and_read_byte(uint16_t port) {
|
||||||
sc_socket socket = net_socket();
|
sc_socket socket = net_connect(IPV4_LOCALHOST, port);
|
||||||
if (socket == SC_INVALID_SOCKET) {
|
if (socket == SC_INVALID_SOCKET) {
|
||||||
return SC_INVALID_SOCKET;
|
return SC_INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok = net_connect(socket, IPV4_LOCALHOST, port);
|
|
||||||
if (!ok) {
|
|
||||||
net_close(socket);
|
|
||||||
return SC_INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
char byte;
|
char byte;
|
||||||
// the connection may succeed even if the server behind the "adb tunnel"
|
// the connection may succeed even if the server behind the "adb tunnel"
|
||||||
// is not listening, so read one byte to detect a working connection
|
// is not listening, so read one byte to detect a working connection
|
||||||
@ -373,6 +361,7 @@ connect_to_server(struct server *server, uint32_t attempts, sc_tick delay) {
|
|||||||
// it worked!
|
// it worked!
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
// TODO use mutex + condvar + bool stopped
|
||||||
if (attempts) {
|
if (attempts) {
|
||||||
sc_mutex_lock(&server->mutex);
|
sc_mutex_lock(&server->mutex);
|
||||||
// Ignore timedwait return (spurious wake ups are harmless)
|
// Ignore timedwait return (spurious wake ups are harmless)
|
||||||
@ -576,16 +565,11 @@ server_connect_to(struct server *server, struct server_info *info) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we know that the device is listening, we don't need several attempts
|
// we know that the device is listening, we don't need several attempts
|
||||||
server->control_socket = net_socket();
|
server->control_socket =
|
||||||
|
net_connect(IPV4_LOCALHOST, server->local_port);
|
||||||
if (server->control_socket == SC_INVALID_SOCKET) {
|
if (server->control_socket == SC_INVALID_SOCKET) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool ok = net_connect(server->control_socket, IPV4_LOCALHOST,
|
|
||||||
server->local_port);
|
|
||||||
if (!ok) {
|
|
||||||
net_close(server->control_socket);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we don't need the adb tunnel anymore
|
// we don't need the adb tunnel anymore
|
||||||
|
@ -94,18 +94,13 @@ net_perror(const char *s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sc_socket
|
sc_socket
|
||||||
net_socket(void) {
|
net_connect(uint32_t addr, uint16_t port) {
|
||||||
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
sc_socket sock = wrap(raw_sock);
|
sc_socket sock = wrap(raw_sock);
|
||||||
if (sock == SC_INVALID_SOCKET) {
|
if (sock == SC_INVALID_SOCKET) {
|
||||||
net_perror("socket");
|
net_perror("socket");
|
||||||
|
return SC_INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
return sock;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
net_connect(sc_socket socket, uint32_t addr, uint16_t port) {
|
|
||||||
sc_raw_socket raw_sock = unwrap(socket);
|
|
||||||
|
|
||||||
SOCKADDR_IN sin;
|
SOCKADDR_IN sin;
|
||||||
sin.sin_family = AF_INET;
|
sin.sin_family = AF_INET;
|
||||||
@ -114,15 +109,21 @@ net_connect(sc_socket socket, uint32_t addr, uint16_t port) {
|
|||||||
|
|
||||||
if (connect(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
if (connect(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||||
net_perror("connect");
|
net_perror("connect");
|
||||||
return false;
|
net_close(sock);
|
||||||
|
return SC_INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
sc_socket
|
||||||
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog) {
|
net_listen(uint32_t addr, uint16_t port, int backlog) {
|
||||||
sc_raw_socket raw_sock = unwrap(socket);
|
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
sc_socket sock = wrap(raw_sock);
|
||||||
|
if (sock == SC_INVALID_SOCKET) {
|
||||||
|
net_perror("socket");
|
||||||
|
return SC_INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
int reuse = 1;
|
int reuse = 1;
|
||||||
if (setsockopt(raw_sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
|
if (setsockopt(raw_sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
|
||||||
@ -137,15 +138,17 @@ net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog) {
|
|||||||
|
|
||||||
if (bind(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
if (bind(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||||
net_perror("bind");
|
net_perror("bind");
|
||||||
return false;
|
net_close(sock);
|
||||||
|
return SC_INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(raw_sock, backlog) == SOCKET_ERROR) {
|
if (listen(raw_sock, backlog) == SOCKET_ERROR) {
|
||||||
net_perror("listen");
|
net_perror("listen");
|
||||||
return false;
|
net_close(sock);
|
||||||
|
return SC_INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_socket
|
sc_socket
|
||||||
|
@ -31,13 +31,10 @@ void
|
|||||||
net_cleanup(void);
|
net_cleanup(void);
|
||||||
|
|
||||||
sc_socket
|
sc_socket
|
||||||
net_socket(void);
|
net_connect(uint32_t addr, uint16_t port);
|
||||||
|
|
||||||
bool
|
sc_socket
|
||||||
net_connect(sc_socket socket, uint32_t addr, uint16_t port);
|
net_listen(uint32_t addr, uint16_t port, int backlog);
|
||||||
|
|
||||||
bool
|
|
||||||
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog);
|
|
||||||
|
|
||||||
sc_socket
|
sc_socket
|
||||||
net_accept(sc_socket server_socket);
|
net_accept(sc_socket server_socket);
|
||||||
|
Reference in New Issue
Block a user