void * status_thread() { int pwds; const short status_sleep = 3; while(1) { sleep(status_sleep); xmlMutexLock(finishedMutex); pwds = counter / status_sleep; counter = 0; if (finished != 0) break; xmlMutexUnlock(finishedMutex); xmlMutexLock(pwdMutex); printf("Probing: '%s' [%d pwds/sec]\n", password, pwds); xmlMutexUnlock(pwdMutex); savestatus(); //FIXME: this is wrong, when probing current password(s) is(are) not finished yet, and the program is exiting } }
int status(struct JOB_PARM *My_Job) { static int Counter = MAX_SAVE_COUNT; static int ThreadCounter[MAX_THREADS]; static unsigned long LastLength[MAX_THREADS]; int i, ReturnCode = 0; /* Do nothing for default */ int MyID = My_Job->ThreadID; int MaxThreads = My_Job->MaxThreads; if( ThreadCounter[MyID] == -1 ) return ReturnCode; /* This thread has finished */ if( My_Job->ti[MyID]->DestLength ) /* Current thread have something to do */ { if( Counter++ >= MAX_SAVE_COUNT ) /* Save status */ { Counter = 0; /* Reset counter */ savestatus(My_Job); } if( (ThreadCounter[MyID] > MAX_RESPAWN_COUNT) || (ThreadCounter[MyID] == 0) ) { if(ThreadCounter[MyID] == 0) /* This is the initial loop, the thread has not started yet */ ReturnCode = 1; /* Start it */ ThreadCounter[MyID] = 1; /* Reset counter */ if( (LastLength[MyID] == 0) || (LastLength[MyID] > My_Job->ti[MyID]->DestLength) ) /* Update? */ { LastLength[MyID] = My_Job->ti[MyID]->DestLength; } else /* This thread seems to be frozen, restart it */ { printf("Thread frozen, respawn......\n"); return 3; /* Kill me & Start a new thread */ } } ThreadCounter[MyID]++; //printf("MyID:%d ThreadCounter[MyID]:%d LastLength[MyID]:%d SourOffset:%d DestLength:%d\n", MyID, ThreadCounter[MyID], LastLength[MyID], //My_Job->ti[MyID]->SourOffset, My_Job->ti[MyID]->DestLength); if( My_Job->ti[MyID]->Message == MESSAGE_EXIT ) /* The main loop called for a exit */ { //printf("Someone send a MESSAGE_EXIT! ID:%d\n", MyID); savestatus(My_Job); My_Job->ti[MyID]->Message = 0; ThreadCounter[MyID] = -1; ReturnCode = 2; /* Mark this thread has finished */ } } else { int ToInit = 0; int ToGive = 0; int MaxID = 0; unsigned int MaxLength = 0; for(i=0 ; i<MaxThreads ; i++) /* Count empty threads */ { if( !My_Job->ti[i]->DestLength ) { ToGive++; if( !My_Job->ti[i]->SourOffset ) ToInit++; /* Both zero mean this job've not been initialized */ } if( My_Job->ti[i]->DestLength > MaxLength ) /* Find the thread that have most heavy duty */ { MaxID = i; MaxLength = My_Job->ti[i]->DestLength; } } if(ToInit == MaxThreads) /* All threads uninitialized. give the whole file to this thread */ { //printf("%d %d\n", My_Job->SpecOffset, My_Job->SpecLength); My_Job->ti[MyID]->SourOffset = My_Job->SpecOffset; My_Job->ti[MyID]->DestLength = My_Job->SpecLength; ReturnCode = 255; /* Ask for execute on this thread again (to really start it) */ } else /* Give current thread something to do */ { unsigned int Length = My_Job->ti[MaxID]->DestLength / (ToGive+1); /* +1 is MaxID's owner itself */ if(Length > MIN_INSERT_SIZE) /* No need to spawn thread if no much work */ { MUTEX /* Will change other threads's working parameters */ My_Job->ti[MyID]->DestLength = My_Job->ti[MaxID]->DestLength - Length; My_Job->ti[MyID]->SourOffset = My_Job->ti[MaxID]->SourOffset + Length; My_Job->ti[MaxID]->DestLength = Length; DEMUTEX ReturnCode = 255; /* Ask for execute on this thread again (to really start it) */ } else { //printf("I go here!\nLength:%d ToGive:%d ToInit:%d\n", Length, ToGive, ToInit); ThreadCounter[MyID] = -1; ReturnCode = 2; /* Nothing to do, current thread finished */ } } savestatus(My_Job); } return ReturnCode; }
int loadstatus() { xmlNodePtr root = NULL; xmlNodePtr node = NULL; xmlParserCtxtPtr parserctxt; int ret = 0; char* tmp; FILE* totest; totest = fopen(statname, "r"); if (totest != NULL) { fclose(totest); status = xmlParseFile(statname); } if (status != NULL) root = xmlDocGetRootElement(status); else status = xmlNewDoc(NULL); if (root != NULL) { parserctxt = xmlNewParserCtxt(); for (node = root->children; node; node = node->next) { if (xmlStrcmp(node->name, "abc") == 0) { if (node->children && (strlen(node->children->content) > 0)) { ABC = xmlStringDecodeEntities(parserctxt, node->children->content,XML_SUBSTITUTE_BOTH,0,0,0); } else ret = 1; } else if (xmlStrcmp(node->name, "current") == 0) { if (node->children && (strlen(node->children->content) > 0)) { tmp = xmlStringDecodeEntities(parserctxt, node->children->content,XML_SUBSTITUTE_BOTH,0,0,0); strcpy(password,tmp); curr_len = strlen(password); printf("INFO: Resuming cracking from password: '******'\n",password); xmlFree(tmp); } else ret = 1; } else if (xmlStrcmp(node->name, "good_password") == 0) { if (node->children && (strlen(node->children->content) > 0)) { tmp = xmlStringDecodeEntities(parserctxt, node->children->content,XML_SUBSTITUTE_BOTH,0,0,0); strcpy(password,tmp); curr_len = strlen(password); xmlMutexLock(finishedMutex); finished = 1; xmlMutexUnlock(finishedMutex); strcpy((char*) &password_good, (char*) &password); printf("GOOD: This archive was succesfully cracked\n"); printf(" The good password is: '%s'\n", password); xmlFree(tmp); ret = 1; } } } xmlFreeParserCtxt(parserctxt); } else { root = xmlNewNode(NULL, "rarcrack"); xmlDocSetRootElement(status, root); node = xmlNewTextChild(root, NULL, "abc", ABC); node = xmlNewTextChild(root, NULL, "current", getfirstpassword()); node = xmlNewTextChild(root, NULL, "good_password", ""); savestatus(); } return ret; }