From fd71da8adeff263221db9010e2152ecf6c074fee Mon Sep 17 00:00:00 2001 From: jsteube Date: Sat, 1 Sep 2018 13:19:29 +0200 Subject: [PATCH] Add timeout to fgets() for later use --- include/shared.h | 3 +++ src/dispatch.c | 32 ++++++++++++++++++++++++-------- src/shared.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/include/shared.h b/include/shared.h index 541a50522..5d9a497ee 100644 --- a/include/shared.h +++ b/include/shared.h @@ -74,4 +74,7 @@ void hc_strncat (u8 *dst, u8 *src, const size_t n); int count_char (const u8 *buf, const int len, const u8 c); float get_entropy (const u8 *buf, const int len); +int select_read_timeout (int sockfd, const int sec); +int select_write_timeout (int sockfd, const int sec); + #endif // _SHARED_H diff --git a/src/dispatch.c b/src/dispatch.c index 10d624f0e..641f278e0 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -171,6 +171,17 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par while (device_param->pws_pre_cnt < device_param->kernel_power) { + const int rc_select = select_read_timeout (fileno (stdin), 1); + + if (rc_select == -1) break; + + if (rc_select == 0) + { + if (status_ctx->run_thread_level1 == false) break; + + continue; + } + char *line_buf = fgets (buf, HCBUFSIZ_LARGE - 1, stdin); if (line_buf == NULL) break; @@ -230,7 +241,7 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par pw_pre_add (device_param, (u8 *) line_buf, (int) line_len, NULL, 0, 0); - while (status_ctx->run_thread_level1 == false) break; + if (status_ctx->run_thread_level1 == false) break; } hc_thread_mutex_unlock (status_ctx->mux_dispatcher); @@ -324,6 +335,17 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par while (device_param->pws_cnt < device_param->kernel_power) { + const int rc_select = select_read_timeout (fileno (stdin), 1); + + if (rc_select == -1) break; + + if (rc_select == 0) + { + if (status_ctx->run_thread_level1 == false) break; + + continue; + } + char *line_buf = fgets (buf, HCBUFSIZ_LARGE - 1, stdin); if (line_buf == NULL) break; @@ -383,7 +405,7 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par pw_add (device_param, (const u8 *) line_buf, (const int) line_len); - while (status_ctx->run_thread_level1 == false) break; + if (status_ctx->run_thread_level1 == false) break; } hc_thread_mutex_unlock (status_ctx->mux_dispatcher); @@ -432,12 +454,6 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par if (device_param->speed_only_finish == true) break; } - - device_param->kernel_accel_prev = device_param->kernel_accel; - device_param->kernel_loops_prev = device_param->kernel_loops; - - device_param->kernel_accel = 0; - device_param->kernel_loops = 0; } device_param->kernel_accel_prev = device_param->kernel_accel; diff --git a/src/shared.c b/src/shared.c index 16e5e68e1..38e38e6bd 100644 --- a/src/shared.c +++ b/src/shared.c @@ -709,3 +709,33 @@ float get_entropy (const u8 *buf, const int len) return entropy; } + +int select_read_timeout (int sockfd, const int sec) +{ + struct timeval tv; + + tv.tv_sec = sec; + tv.tv_usec = 0; + + fd_set fds; + + FD_ZERO (&fds); + FD_SET (sockfd, &fds); + + return select (sockfd + 1, &fds, NULL, NULL, &tv); +} + +int select_write_timeout (int sockfd, const int sec) +{ + struct timeval tv; + + tv.tv_sec = sec; + tv.tv_usec = 0; + + fd_set fds; + + FD_ZERO (&fds); + FD_SET (sockfd, &fds); + + return select (sockfd + 1, NULL, &fds, NULL, &tv); +}