Timeout socket accept using select (#10415)

This commit is contained in:
Florian Märkl 2018-06-20 11:20:55 +02:00 committed by radare
parent 65deb59f5f
commit 2202d14067
5 changed files with 35 additions and 7 deletions

View File

@ -101,7 +101,7 @@ int main(int argc, char **argv) {
char *result_heap = NULL;
const char *result = page_index;
rs = r_socket_http_accept (s, timeout);
rs = r_socket_http_accept (s, 0, timeout);
if (!rs) continue;
if (!strcmp (rs->method, "GET")) {
if (!strncmp (rs->path, "/proc/kill/", 11)) {

View File

@ -467,8 +467,6 @@ static int r_core_rtr_http_run(RCore *core, int launch, const char *path) {
return 1;
}
r_socket_block_time (s, 1, 1);
if (launch=='H') {
const char *browser = r_config_get (core->config, "http.browser");
r_sys_cmdf ("%s http://%s:%d/%s &",
@ -528,7 +526,8 @@ static int r_core_rtr_http_run(RCore *core, int launch, const char *path) {
/* this is blocking */
activateDieTime (core);
rs = r_socket_http_accept (s, timeout);
rs = r_socket_http_accept (s, 1, timeout);
origoff = core->offset;
origblk = core->block;

View File

@ -92,6 +92,7 @@ R_API int r_socket_close(RSocket *s);
R_API int r_socket_free(RSocket *s);
R_API bool r_socket_listen(RSocket *s, const char *port, const char *certfile);
R_API RSocket *r_socket_accept(RSocket *s);
R_API RSocket *r_socket_accept_timeout(RSocket *s, unsigned int timeout);
R_API int r_socket_block_time(RSocket *s, int block, int sec);
R_API int r_socket_flush(RSocket *s);
R_API int r_socket_ready(RSocket *s, int secs, int usecs);
@ -136,7 +137,7 @@ typedef struct r_socket_http_request {
int data_length;
} RSocketHTTPRequest;
R_API RSocketHTTPRequest *r_socket_http_accept(RSocket *s, int timeout);
R_API RSocketHTTPRequest *r_socket_http_accept(RSocket *s, int accept_timeout, int timeout);
R_API void r_socket_http_response(RSocketHTTPRequest *rs, int code, const char *out, int x, const char *headers);
R_API void r_socket_http_close(RSocketHTTPRequest *rs);
R_API ut8 *r_socket_http_handle_upload(const ut8 *str, int len, int *olen);

View File

@ -8,13 +8,17 @@ R_API void r_socket_http_server_set_breaked(bool *b) {
breaked = b;
}
R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s, int timeout) {
R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s, int accept_timeout, int timeout) {
int content_length = 0, xx, yy;
int pxx = 1, first = 0;
char buf[1500], *p, *q;
RSocketHTTPRequest *hr = R_NEW0 (RSocketHTTPRequest);
if (!hr) return NULL;
hr->s = r_socket_accept (s);
if (accept_timeout > 0) {
hr->s = r_socket_accept_timeout (s, 1);
} else {
hr->s = r_socket_accept (s);
}
if (!hr->s) {
free (hr);
return NULL;

View File

@ -590,6 +590,30 @@ R_API RSocket *r_socket_accept(RSocket *s) {
return sock;
}
R_API RSocket *r_socket_accept_timeout(RSocket *s, unsigned int timeout) {
fd_set read_fds;
fd_set except_fds;
FD_ZERO (&read_fds);
FD_SET (s->fd, &read_fds);
FD_ZERO (&except_fds);
FD_SET (s->fd, &except_fds);
struct timeval t;
t.tv_sec = timeout;
t.tv_usec = 0;
int r = select (s->fd + 1, &read_fds, NULL, &except_fds, &t);
if(r < 0) {
perror ("select");
} else if (r > 0 && FD_ISSET (s->fd, &read_fds)) {
return r_socket_accept (s);
}
return NULL;
}
R_API int r_socket_block_time (RSocket *s, int block, int sec) {
#if __UNIX__ || defined(__CYGWIN__)
int ret, flags;