| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
#include <assert.h> |
| 34 |
|
| 35 |
#include "fsm.h" |
| 36 |
|
| 37 |
fsm_t *fsm_alloc(ophcrack_t *crack) { |
| 38 |
fsm_t *fsm = (fsm_t*)malloc(sizeof(fsm_t)); |
| 39 |
|
| 40 |
fsm->oldstate = st_wait; |
| 41 |
fsm->state = st_wait; |
| 42 |
|
| 43 |
fsm->crack = crack; |
| 44 |
fsm->arg = crack->arg; |
| 45 |
|
| 46 |
fsm->preload = 1; |
| 47 |
fsm->bforce = fsm->arg->bforce; |
| 48 |
fsm->pwait = 0; |
| 49 |
fsm->ssave = 0; |
| 50 |
|
| 51 |
fsm->pending_msg = list_alloc(); |
| 52 |
fsm->htoremove = list_alloc(); |
| 53 |
|
| 54 |
fsm_reset_preload(fsm); |
| 55 |
fsm_reset_bforce(fsm); |
| 56 |
|
| 57 |
return fsm; |
| 58 |
} |
| 59 |
|
| 60 |
message_t *fsm_next(fsm_t *fsm, message_t *msg) { |
| 61 |
ophcrack_t *crack = fsm->crack; |
| 62 |
scheduler_t *sched = crack->sched; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if (msg->kind == msg_preload) { |
| 67 |
msg_load_t *load = (msg_load_t*)msg->data; |
| 68 |
table_t *tbl = load->tbl; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if (tbl == 0 && !load->done) { |
| 73 |
fsm->psize_curr = 0; |
| 74 |
fsm->psize_total = load->size; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
else if (tbl && load->done) |
| 80 |
fsm->psize_curr += load->size; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
else if (msg->kind == msg_bforce) { |
| 87 |
msg_bforce_t *force = (msg_bforce_t*)msg->data; |
| 88 |
|
| 89 |
if (!force->done) { |
| 90 |
fsm->bforce_curr = 0; |
| 91 |
fsm->bforce_total = force->count; |
| 92 |
} |
| 93 |
|
| 94 |
else { |
| 95 |
fsm->bforce_curr += force->count; |
| 96 |
assert(fsm->bforce_curr <= fsm->bforce_total); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
else if (msg->kind == msg_done && ((msg_done_t*)msg->data)->kind == check) { |
| 103 |
|
| 104 |
if (fsm->state != st_pause1 && fsm->state != st_pause2) { |
| 105 |
scheduler_pause(sched); |
| 106 |
fsm->pwait = sched->nthreads; |
| 107 |
fsm->state = st_pause2; |
| 108 |
} |
| 109 |
|
| 110 |
fsm_reset_preload(fsm); |
| 111 |
fsm->oldstate = st_start; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
else { |
| 117 |
switch (fsm->state) { |
| 118 |
case st_wait: |
| 119 |
break; |
| 120 |
|
| 121 |
case st_start: |
| 122 |
break; |
| 123 |
|
| 124 |
case st_pause1: |
| 125 |
msg = fsm_handle_pause1(fsm, msg); |
| 126 |
break; |
| 127 |
|
| 128 |
case st_pause2: |
| 129 |
msg = fsm_handle_pause2(fsm, msg); |
| 130 |
break; |
| 131 |
|
| 132 |
case st_preload: |
| 133 |
msg = fsm_handle_preload(fsm, msg); |
| 134 |
break; |
| 135 |
|
| 136 |
case st_work1: |
| 137 |
msg = fsm_handle_work1(fsm, msg); |
| 138 |
break; |
| 139 |
|
| 140 |
case st_work2: |
| 141 |
msg = fsm_handle_work2(fsm, msg); |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return msg; |
| 147 |
} |
| 148 |
|
| 149 |
void fsm_handle_start(fsm_t *fsm) { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
if (fsm->bforce || fsm->arg->bforce == 0) { |
| 155 |
list_t *htoremove = fsm->htoremove; |
| 156 |
|
| 157 |
while (htoremove->size > 0) { |
| 158 |
hash_t *hsh = (hash_t*)list_rem_head(htoremove); |
| 159 |
hash_free(hsh); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
fsm_launch_preload(fsm); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if (fsm->bforce) fsm_launch_bforce(fsm); |
| 170 |
|
| 171 |
fsm->state = st_preload; |
| 172 |
} |
| 173 |
|
| 174 |
message_t *fsm_handle_preload(fsm_t *fsm, message_t *msg) { |
| 175 |
ophcrack_t *crack = fsm->crack; |
| 176 |
scheduler_t *sched = crack->sched; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
list_t *tasks = list_alloc(); |
| 182 |
|
| 183 |
if (msg->kind == msg_done) { |
| 184 |
msg_done_t *done = (msg_done_t*)msg->data; |
| 185 |
|
| 186 |
if (done->kind == preload_all) { |
| 187 |
assert(fsm->psize_curr <= fsm->psize_total); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
fsm->psize_curr = 1; |
| 198 |
fsm->psize_total = 1; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
pthread_mutex_lock(sched->mutex); |
| 203 |
|
| 204 |
int nthreads = sched->nthreads; |
| 205 |
int ntasks = MY_MAX(1, nthreads-1); |
| 206 |
|
| 207 |
for (int n=0; n<ntasks; ++n) { |
| 208 |
ophtask_t *task = ophcrack_next(crack); |
| 209 |
|
| 210 |
if (task) |
| 211 |
list_add_tail(tasks, task); |
| 212 |
else |
| 213 |
break; |
| 214 |
} |
| 215 |
|
| 216 |
pthread_mutex_unlock(sched->mutex); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
ntasks = tasks->size; |
| 221 |
|
| 222 |
for (int i=0; i<ntasks; ++i) { |
| 223 |
ophtask_t *task = (ophtask_t*)list_rem_head(tasks); |
| 224 |
scheduler_add(sched, task, low); |
| 225 |
} |
| 226 |
|
| 227 |
assert(tasks->size == 0); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
if (ntasks == 0) { |
| 237 |
ophtask_t *task = ophtask_alloc(find); |
| 238 |
scheduler_add(sched, task, low); |
| 239 |
message_done(find); |
| 240 |
} |
| 241 |
|
| 242 |
fsm->state = st_work1; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
list_free(tasks); |
| 247 |
|
| 248 |
return msg; |
| 249 |
} |
| 250 |
|
| 251 |
message_t *fsm_handle_pause1(fsm_t *fsm, message_t *msg) { |
| 252 |
list_t *pending_msg = fsm->pending_msg; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
if (msg->kind == msg_pause) { |
| 257 |
--fsm->pwait; |
| 258 |
|
| 259 |
#ifndef NDEBUG |
| 260 |
int pwait = fsm->pwait; |
| 261 |
int nthreads = fsm->crack->sched->nthreads; |
| 262 |
|
| 263 |
assert(pwait >= 0 && pwait <= nthreads); |
| 264 |
#endif |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
else |
| 270 |
list_add_tail(pending_msg, msg); |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
message_t *fsm_handle_pause2(fsm_t *fsm, message_t *msg) { |
| 276 |
ophcrack_t *crack = fsm->crack; |
| 277 |
scheduler_t *sched = crack->sched; |
| 278 |
list_t *pending_msg = fsm->pending_msg; |
| 279 |
|
| 280 |
if (msg) { |
| 281 |
|
| 282 |
|
| 283 |
if (msg->kind == msg_pause) { |
| 284 |
--fsm->pwait; |
| 285 |
|
| 286 |
#ifndef NDEBUG |
| 287 |
int pwait = fsm->pwait; |
| 288 |
int nthreads = crack->sched->nthreads; |
| 289 |
|
| 290 |
assert(pwait >= 0 && pwait <= nthreads); |
| 291 |
#endif |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
else { |
| 297 |
list_add_tail(pending_msg, msg); |
| 298 |
msg = 0; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
if (fsm->pwait == 0) { |
| 305 |
fsm_reset(fsm); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if (fsm->bforce && fsm->preload == 0) fsm_launch_bforce(fsm); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
scheduler_continue(sched); |
| 319 |
message_insert_first(fsm->pending_msg); |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
fsm->state = fsm->preload ? st_start : fsm->oldstate; |
| 325 |
|
| 326 |
if (fsm->state == st_start) { |
| 327 |
assert(fsm->preload != 0); |
| 328 |
fsm_handle_start(fsm); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return msg; |
| 333 |
} |
| 334 |
|
| 335 |
message_t *fsm_handle_work1(fsm_t *fsm, message_t *msg) { |
| 336 |
if (msg->kind != msg_done) return msg; |
| 337 |
|
| 338 |
msg_done_t *done = (msg_done_t*)msg->data; |
| 339 |
|
| 340 |
if (done->kind == find) |
| 341 |
fsm->state = st_work2; |
| 342 |
|
| 343 |
return msg; |
| 344 |
} |
| 345 |
|
| 346 |
message_t *fsm_handle_work2(fsm_t *fsm, message_t *msg) { |
| 347 |
ophcrack_t *crack = fsm->crack; |
| 348 |
scheduler_t *sched = crack->sched; |
| 349 |
arg_t *arg = fsm->arg; |
| 350 |
|
| 351 |
if (msg->kind != msg_done) return msg; |
| 352 |
|
| 353 |
msg_done_t *done = (msg_done_t*)msg->data; |
| 354 |
|
| 355 |
if (done->kind == all) { |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
int npwds_total = crack->npwds_total; |
| 360 |
int npwds_found = crack->npwds_found; |
| 361 |
|
| 362 |
if (crack->remaining->size > 0 && npwds_found < npwds_total) { |
| 363 |
ophtask_t *task = ophtask_alloc(preload_all); |
| 364 |
scheduler_add(sched, task, disk); |
| 365 |
|
| 366 |
fsm->state = st_preload; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
else { |
| 372 |
if (arg->bforce) { |
| 373 |
fsm->bforce_curr = 1; |
| 374 |
fsm->bforce_total = 1; |
| 375 |
} |
| 376 |
|
| 377 |
fsm->state = st_wait; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
return msg; |
| 382 |
} |
| 383 |
|
| 384 |
void fsm_ssave(fsm_t *fsm) { |
| 385 |
fsm_pause(fsm); |
| 386 |
|
| 387 |
fsm->ssave = 1; |
| 388 |
fsm->state = st_pause2; |
| 389 |
} |
| 390 |
|
| 391 |
void fsm_pause(fsm_t *fsm) { |
| 392 |
ophcrack_t *crack = fsm->crack; |
| 393 |
scheduler_t *sched = crack->sched; |
| 394 |
|
| 395 |
scheduler_pause(sched); |
| 396 |
|
| 397 |
fsm->pwait = sched->nthreads; |
| 398 |
fsm->oldstate = fsm->state; |
| 399 |
fsm->state = st_pause1; |
| 400 |
} |
| 401 |
|
| 402 |
void fsm_reset_preload(fsm_t *fsm) { |
| 403 |
fsm->preload = 1; |
| 404 |
fsm->psize_curr = 1; |
| 405 |
fsm->psize_total = 0; |
| 406 |
} |
| 407 |
|
| 408 |
void fsm_reset_bforce(fsm_t *fsm) { |
| 409 |
fsm->bforce = fsm->arg->bforce; |
| 410 |
fsm->bforce_curr = 1; |
| 411 |
fsm->bforce_total = 0; |
| 412 |
} |
| 413 |
|
| 414 |
void fsm_launch_preload(fsm_t *fsm) { |
| 415 |
ophcrack_t *crack = fsm->crack; |
| 416 |
scheduler_t *sched = crack->sched; |
| 417 |
|
| 418 |
ophtask_t *task = ophtask_alloc(preload_all); |
| 419 |
ophload_t *load = (ophload_t*)task->data; |
| 420 |
|
| 421 |
list_t *enabled = crack->enabled; |
| 422 |
list_nd_t *tnd = 0; |
| 423 |
|
| 424 |
for (tnd = enabled->head; tnd != 0; tnd = tnd->next) |
| 425 |
list_add_tail(load->tables, tnd->data); |
| 426 |
|
| 427 |
scheduler_add(sched, task, disk); |
| 428 |
fsm->preload = 0; |
| 429 |
} |
| 430 |
|
| 431 |
void fsm_launch_bforce(fsm_t *fsm) { |
| 432 |
ophcrack_t *crack = fsm->crack; |
| 433 |
scheduler_t *sched = crack->sched; |
| 434 |
|
| 435 |
ophtask_t *task = ophtask_alloc(bforce_all); |
| 436 |
ophbforce_t *force = (ophbforce_t*)task->data; |
| 437 |
|
| 438 |
list_t *hashes = crack->hashes; |
| 439 |
list_nd_t *nd; |
| 440 |
|
| 441 |
int nhashes = hashes->size; |
| 442 |
int n; |
| 443 |
|
| 444 |
force->nhashes = nhashes; |
| 445 |
force->hashes = (hash_t**)malloc(nhashes*sizeof(hash_t*)); |
| 446 |
|
| 447 |
for (nd=hashes->head, n=0; nd!=0; nd=nd->next, ++n) |
| 448 |
force->hashes[n] = (hash_t*)nd->data; |
| 449 |
|
| 450 |
scheduler_add(sched, task, low); |
| 451 |
fsm->bforce = 0; |
| 452 |
} |
| 453 |
|
| 454 |
void fsm_reset(fsm_t *fsm) { |
| 455 |
assert(fsm->state == st_wait || fsm->state == st_pause2); |
| 456 |
|
| 457 |
static const int bforce_mask = bforce_all + bforce_nt + bforce_lm; |
| 458 |
|
| 459 |
ophcrack_t *crack = fsm->crack; |
| 460 |
arg_t *arg = crack->arg; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
if (arg->ssave && fsm->ssave) { |
| 465 |
FILE *file = fopen(arg->sfname, "w"); |
| 466 |
|
| 467 |
if (file) { |
| 468 |
ophcrack_reset(crack, all - bforce_mask, 0); |
| 469 |
ophcrack_save(crack, file, 0, 1); |
| 470 |
fclose(file); |
| 471 |
} |
| 472 |
|
| 473 |
fsm->ssave = 0; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
if (fsm->preload) |
| 480 |
ophcrack_reset(crack, all - bforce_mask, 1); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
if (fsm->bforce || arg->bforce == 0) |
| 486 |
ophcrack_reset(crack, bforce_mask, 1); |
| 487 |
} |