static int sqlcipher_ltc_activate(void *ctx) { unsigned char random_buffer[FORTUNA_MAX_SZ]; #ifndef SQLCIPHER_LTC_NO_MUTEX_RAND if(ltc_rand_mutex == NULL){ ltc_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); } sqlite3_mutex_enter(ltc_rand_mutex); #endif sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); if(ltc_init == 0) { if(register_prng(&fortuna_desc) < 0) return SQLITE_ERROR; if(register_cipher(&rijndael_desc) < 0) return SQLITE_ERROR; if(register_hash(&sha512_desc) < 0) return SQLITE_ERROR; if(register_hash(&sha256_desc) < 0) return SQLITE_ERROR; if(register_hash(&sha1_desc) < 0) return SQLITE_ERROR; if(fortuna_start(&prng) != CRYPT_OK) { return SQLITE_ERROR; } ltc_init = 1; } ltc_ref_count++; #ifndef SQLCIPHER_TEST sqlite3_randomness(FORTUNA_MAX_SZ, random_buffer); #endif #ifndef SQLCIPHER_LTC_NO_MUTEX_RAND sqlite3_mutex_leave(ltc_rand_mutex); #endif if(sqlcipher_ltc_add_random(ctx, random_buffer, FORTUNA_MAX_SZ) != SQLITE_OK) { return SQLITE_ERROR; } sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); return SQLITE_OK; }
bool CryptManager::Verify( RageFileBasic &file, RString sSignature, RString sPublicKey ) { RSAKeyWrapper key; RString sError; if( !key.Load(sPublicKey, sError) ) { LOG->Warn( "Error loading RSA key: %s", sError.c_str() ); return false; } int iHash = register_hash( &sha1_desc ); ASSERT( iHash >= 0 ); unsigned char buf_hash[20]; HashFile( file, buf_hash, iHash ); int iMatch; int iRet = rsa_verify_hash_ex( (const unsigned char *) sSignature.data(), sSignature.size(), buf_hash, sizeof(buf_hash), LTC_PKCS_1_EMSA, iHash, 0, &iMatch, &key.m_Key ); if( iRet != CRYPT_OK ) { LOG->Warn( "Verify(%s) failed: %s", file.GetDisplayPath().c_str(), error_to_string(iRet) ); return false; } if( !iMatch ) { LOG->Warn( "Verify(%s) failed: signature mismatch", file.GetDisplayPath().c_str() ); return false; } return true; }
/* format is ltc_mp, cipher_desc, [cipher_desc], NULL, hash_desc, [hash_desc], NULL, prng_desc, [prng_desc], NULL */ int crypt_fsa(void *mp, ...) { int err; va_list args; void *p; va_start(args, mp); if (mp != NULL) { XMEMCPY(<c_mp, mp, sizeof(ltc_mp)); } while ((p = va_arg(args, void*)) != NULL) { if ((err = register_cipher(p)) != CRYPT_OK) { va_end(args); return err; } } while ((p = va_arg(args, void*)) != NULL) { if ((err = register_hash(p)) != CRYPT_OK) { va_end(args); return err; } } while ((p = va_arg(args, void*)) != NULL) { if ((err = register_prng(p)) != CRYPT_OK) { va_end(args); return err; } } va_end(args); return CRYPT_OK; }
CTomcryptInternals(void) { int ErrorCode = register_cipher(&rijndael_desc); if (ErrorCode == -1) { throw ExInternalError(std::string("Cannot register AES cipher.")); } ErrorCode = register_prng(&fortuna_desc); if (ErrorCode == -1) { throw ExInternalError(std::string("Cannot register fortuna pseudo random generator.")); } int const GeneratorID = find_prng("fortuna"); ErrorCode = rng_make_prng(128, GeneratorID, &mRandomGenerator, NULL); if (ErrorCode != CRYPT_OK) { throw ExInternalError(std::string("Error while starting random generator: ")+std::string(error_to_string(ErrorCode))); } ErrorCode = register_hash(&sha256_desc); if (ErrorCode == -1) { throw ExInternalError(std::string("Cannot register SHA2 hash.")); } return; }
cf_rv_t tomcrypt_digest_setup() { const struct ltc_hash_descriptor **ptr = descriptors; for (ptr; *ptr; *ptr++) { register_hash(*ptr); } return CF_S_OK; }
int main(void) { register_cipher(&rijndael_enc_desc); register_prng(&yarrow_desc); register_hash(&sha256_desc); return 0; }
/* format is ltc_mp, cipher_desc, [cipher_desc], NULL, hash_desc, [hash_desc], NULL, prng_desc, [prng_desc], NULL */ int crypt_fsa(void *mp, ...) { va_list args; void *p; va_start(args, mp); if (mp != NULL) { XMEMCPY(<c_mp, mp, sizeof(ltc_mp)); } while ((p = va_arg(args, void*)) != NULL) { if (register_cipher(p) == -1) { va_end(args); return CRYPT_INVALID_CIPHER; } } while ((p = va_arg(args, void*)) != NULL) { if (register_hash(p) == -1) { va_end(args); return CRYPT_INVALID_HASH; } } while ((p = va_arg(args, void*)) != NULL) { if (register_prng(p) == -1) { va_end(args); return CRYPT_INVALID_PRNG; } } va_end(args); return CRYPT_OK; }
int main(int argc, char **argv) { int rc = 0; prng_state prng; int prng_index, hash_index; rsa_key key; int i; ltc_mp = tfm_desc; prng_index = register_prng(&sprng_desc); /* (fortuna_desc is a good choice if your platform's PRNG sucks.) */ if (prng_index == -1) { fail("Failed to register a RNG"); } hash_index = register_hash(&sha256_desc); if (hash_index == -1) { fail("Failed to register sha256 hasher"); } if ((rc = rng_make_prng(128, prng_index, &prng, NULL)) != CRYPT_OK) { fail("rng_make_prng failed: %s", error_to_string(rc)); } read_rsakey(&key, "privatekey.bin"); for (i = 1; i < argc; i++) { sign_file(argv[i], &key, &prng, prng_index, hash_index); } rsa_free(&key); return 0; }
static int sqlcipher_ltc_activate(void *ctx) { ltc_ctx *ltc = (ltc_ctx*)ctx; int random_buffer_sz = 32; unsigned char random_buffer[random_buffer_sz]; if(ltc_init == 0) { if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR; if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR; if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR; ltc_init = 1; } if(fortuna_start(&(ltc->prng)) != CRYPT_OK) { return SQLITE_ERROR; } sqlite3_randomness(random_buffer_sz, &random_buffer); if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) { return SQLITE_ERROR; } if(sqlcipher_ltc_add_random(ctx, <c, sizeof(ltc_ctx*)) != SQLITE_OK) { return SQLITE_ERROR; } if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) { return SQLITE_ERROR; } return SQLITE_OK; }
int initEncrypt(){ prng_index = register_prng(&fortuna_desc); hash_index = register_hash(&sha256_desc); int err; if ((err = rng_make_prng(128, find_prng("fortuna"), &prng, NULL)) != CRYPT_OK) { printf("Error setting up PRNG, %s\n", error_to_string(err)); exit(EXIT_FAILURE); } return 0; }
RString CryptManager::GetSHA1ForString( RString sData ) { unsigned char digest[20]; int iHash = register_hash( &sha1_desc ); hash_state hash; int iRet = hash_descriptor[iHash].init( &hash ); iRet = hash_descriptor[iHash].process( &hash, (const unsigned char *) sData.data(), sData.size() ); iRet = hash_descriptor[iHash].done( &hash, digest ); return RString( (const char *) digest, sizeof(digest) ); }
C4Err C4_Init() { C4Err err = kC4Err_NoErr; ltc_mp = ltm_desc; register_prng (&sprng_desc); register_hash (&md5_desc); register_hash (&sha1_desc); register_hash (&sha256_desc); register_hash (&sha384_desc); register_hash (&sha512_desc); register_hash (&sha224_desc); register_hash (&skein256_desc); register_hash (&skein512_desc); register_hash (&skein1024_desc); register_hash (&sha512_256_desc); register_cipher (&aes_desc); register_cipher (&twofish_desc); return err; }
void InitializeMpqCryptography() { DWORD dwSeed = 0x00100001; DWORD index1 = 0; DWORD index2 = 0; int i; // Initialize the decryption buffer. // Do nothing if already done. if(bMpqCryptographyInitialized == false) { for(index1 = 0; index1 < 0x100; index1++) { for(index2 = index1, i = 0; i < 5; i++, index2 += 0x100) { DWORD temp1, temp2; dwSeed = (dwSeed * 125 + 3) % 0x2AAAAB; temp1 = (dwSeed & 0xFFFF) << 0x10; dwSeed = (dwSeed * 125 + 3) % 0x2AAAAB; temp2 = (dwSeed & 0xFFFF); StormBuffer[index2] = (temp1 | temp2); } } // Also register both MD5 and SHA1 hash algorithms register_hash(&md5_desc); register_hash(&sha1_desc); // Use LibTomMath as support math library for LibTomCrypt ltc_mp = ltm_desc; // Don't do that again bMpqCryptographyInitialized = true; } }
void CryptHelpers::Init() { ltc_mp = ltm_desc; g_PRNGDescId = register_prng( &yarrow_desc ); if ( g_PRNGDescId == -1 ) RageException::Throw( "Could not register PRNG Descriptor" ); g_PRNGDesc = &yarrow_desc; rng_make_prng(1024, g_PRNGDescId, &g_PRNGState, NULL); g_SHA1DescId = register_hash( &sha1_desc ); if ( g_SHA1DescId == -1 ) RageException::Throw( "Could not register SHA1 Descriptor" ); g_SHA1Desc = &sha1_desc; }
RString CryptManager::GetMD5ForFile( RString fn ) { RageFile file; if( !file.Open( fn, RageFile::READ ) ) { LOG->Warn( "GetMD5: Failed to open file '%s'", fn.c_str() ); return RString(); } int iHash = register_hash( &md5_desc ); ASSERT( iHash >= 0 ); unsigned char digest[16]; HashFile( file, digest, iHash ); return RString( (const char *) digest, sizeof(digest) ); }
bool CryptManager::Sign( RString sPath, RString &sSignatureOut, RString sPrivKey ) { if( !IsAFile(sPath) ) { LOG->Trace( "SignFileToFile: \"%s\" doesn't exist", sPath.c_str() ); return false; } RageFile file; if( !file.Open(sPath) ) { LOG->Warn( "SignFileToFile: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() ); return false; } RSAKeyWrapper key; RString sError; if( !key.Load(sPrivKey, sError) ) { LOG->Warn( "Error loading RSA key: %s", sError.c_str() ); return false; } int iHash = register_hash( &sha1_desc ); ASSERT( iHash >= 0 ); unsigned char buf_hash[20]; if( !HashFile(file, buf_hash, iHash) ) return false; unsigned char signature[256]; unsigned long signature_len = sizeof(signature); int iRet = rsa_sign_hash_ex( buf_hash, sizeof(buf_hash), signature, &signature_len, LTC_PKCS_1_V1_5, &g_pPRNG->m_PRNG, g_pPRNG->m_iPRNG, iHash, 0, &key.m_Key); if( iRet != CRYPT_OK ) { LOG->Warn( "SignFileToFile error: %s", error_to_string(iRet) ); return false; } sSignatureOut.assign( (const char *) signature, signature_len ); return true; }
/* Register the compiled in ciphers. * This should be run before using any of the ciphers/hashes */ void crypto_init() { const struct ltc_cipher_descriptor *regciphers[] = { #ifdef DROPBEAR_AES &aes_desc, #endif #ifdef DROPBEAR_BLOWFISH &blowfish_desc, #endif #ifdef DROPBEAR_TWOFISH &twofish_desc, #endif #ifdef DROPBEAR_3DES &des3_desc, #endif NULL }; const struct ltc_hash_descriptor *reghashes[] = { /* we need sha1 for hostkey stuff regardless */ &sha1_desc, #ifdef DROPBEAR_MD5_HMAC &md5_desc, #endif #ifdef DROPBEAR_SHA2_256_HMAC &sha256_desc, #endif #ifdef DROPBEAR_SHA2_512_HMAC &sha512_desc, #endif NULL }; int i; for (i = 0; regciphers[i] != NULL; i++) { if (register_cipher(regciphers[i]) == -1) { dropbear_exit("Error registering crypto"); } } for (i = 0; reghashes[i] != NULL; i++) { if (register_hash(reghashes[i]) == -1) { dropbear_exit("Error registering crypto"); } } }
BOOL CRSADlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here // 添加list数据 for(int i = 0; i < MAX_LIST; i++) { if(!g_AsyAlgList[i].strName.IsEmpty()) { m_ListBox.InsertString(i, g_AsyAlgList[i].strName); } } m_ListBox.SetCurSel(0); g_AsyAlgList_Index = g_AsyAlgList[0].index; /* register prng/hash */ if (register_prng(&sprng_desc) == -1) { MessageBox("Error registering sprng"); } /* register a math library (in this case TomsFastMath)*/ ltc_mp = ltm_desc; if (register_hash(&sha1_desc) == -1) { MessageBox("Error registering sha1"); } hash_idx = find_hash("sha1"); prng_idx = find_prng("sprng"); UpdateData(TRUE); m_e = _T("010001"); m_e_n = _T("3"); UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
/* compatibility wrapper */ static void HMAC_SHA1( uint8_t* secret, size_t secret_len, uint8_t* in, size_t in_len, uint8_t* out) { int rv; unsigned long out_len; if (sha1_id < 0) { sha1_id = register_hash( &sha1_desc); } out_len = hash_descriptor[sha1_id].hashsize; rv = hmac_memory( sha1_id, secret, secret_len, in, in_len, out, &out_len); if (rv) { abort(); } }
void DB_AuthLoad_InitCrypto() { if (ffVersion < 319) { return; } register_hash(&sha256_desc); register_cipher(&aes_desc); unsigned char encKey[256]; DB_ReadXFileRawData(encKey, 256); ZoneKey key; DB_AuthLoad_DecryptKey(encKey, &key); int aes = find_cipher("aes"); ctr_start(aes, key.iv, key.key, sizeof(key.key), 0, 0, &ffCTR); memcpy(ffIV, key.iv, sizeof(ffIV)); }
// compute the MAC of a given file int hmac(char* filename,unsigned char* dst,unsigned char* key) { int idx, err; hmac_state hmac; unsigned long dstlen; if (register_hash(&sha1_desc)==-1) { printf("error registering SHA1\n"); return -1; } idx = find_hash("sha1"); // use sha1 for HMAC dstlen = 16; if ((err = hmac_file(idx,filename,key,16,dst,&dstlen))!=CRYPT_OK) { printf("Error hmac: %s\n",error_to_string(err)); return -1; } printf("hmac complete\n"); return 0; }
/* Register the compiled in ciphers. * This should be run before using any of the ciphers/hashes */ void crypto_init() { const struct _cipher_descriptor *regciphers[] = { #ifdef DROPBEAR_AES128_CBC &rijndael_desc, #endif #ifdef DROPBEAR_BLOWFISH_CBC &blowfish_desc, #endif #ifdef DROPBEAR_TWOFISH128_CBC &twofish_desc, #endif #ifdef DROPBEAR_3DES_CBC &des3_desc, #endif NULL }; const struct _hash_descriptor *reghashes[] = { /* we need sha1 for hostkey stuff regardless */ &sha1_desc, #ifdef DROPBEAR_MD5_HMAC &md5_desc, #endif NULL }; int i; for (i = 0; regciphers[i] != NULL; i++) { if (register_cipher(regciphers[i]) == -1) { dropbear_exit("error registering crypto"); } } for (i = 0; reghashes[i] != NULL; i++) { if (register_hash(reghashes[i]) == -1) { dropbear_exit("error registering crypto"); } } }
int main(void) { unsigned char buffer[100], hash[MAXBLOCKSIZE]; int idx, x; hash_state md; /* register hashes .... */ if (register_hash(&md5_desc) == -1) { printf("Error registering MD5.\n"); return -1; } /* register other hashes ... */ /* prompt for name and strip newline */ printf("Enter hash name: \n"); fgets(buffer, sizeof(buffer), stdin); buffer[strlen(buffer) - 1] = 0; /* get hash index */ idx = find_hash(buffer); if (idx == -1) { printf("Invalid hash name!\n"); return -1; } /* hash input until blank line */ hash_descriptor[idx].init(&md); while (fgets(buffer, sizeof(buffer), stdin) != NULL) hash_descriptor[idx].process(&md, buffer, strlen(buffer)); hash_descriptor[idx].done(&md, hash); /* dump to screen */ for (x = 0; x < hash_descriptor[idx].hashsize; x++) printf("%02x ", hash[x]); printf("\n"); return 0; }
void register_algs(void) { int x; register_cipher (&rc6_desc); register_cipher (&des3_desc); if (register_hash(&sha256_desc) == -1) { fprintf(stderr, "Error registering SHA256\n"); exit(-1); } if (register_prng(&yarrow_desc) == -1) { fprintf(stderr, "Error registering yarrow PRNG\n"); exit(-1); } if (register_prng(&sprng_desc) == -1) { fprintf(stderr, "Error registering sprng PRNG\n"); exit(-1); } }
int HSH_GetHashId(const char *name) { int i, h; for (i = 0; hashes[i].name; i++) { if (!strcmp(name, hashes[i].name)) break; } if (!hashes[i].name) return -1; /* not found */ h = find_hash(hashes[i].int_name); if (h >= 0) return h; /* already registered */ /* register and try again */ register_hash(hashes[i].desc); return find_hash(hashes[i].int_name); }
/* * Computes a HMAC SHA-1 keyed hash of 'input' using the key 'key' */ bool hmacSha1(const unsigned char* key, const size_t keyLen, const unsigned char* input, const size_t inputLen, unsigned char* output, unsigned int* outputLen) { if (!key || !input || !output) { return false; } static int hashId = -1; if (hashId == -1) { register_hash(&sha1_desc); hashId = find_hash("sha1"); } unsigned long sha1HashLen = 20; if (hmac_memory(hashId, key, keyLen, input, inputLen, output, &sha1HashLen) != CRYPT_OK) { return false; } *outputLen = sha1HashLen; return true; }
int main() { ltc_mp = ltm_desc; rsa_key priv_key, pub_key; int hash_idx, prng_idx; int ret = rsa_import(openssl_private_rsa, sizeof(openssl_private_rsa), &priv_key); ret = rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &pub_key); if (register_hash(&sha1_desc) == -1) { printf("Error registering SHA1\n"); return -1; } hash_idx = find_hash("sha1"); register_prng(&sprng_desc); prng_idx = find_prng("sprng"); prng_state prng; int err; if ((err = yarrow_start(&prng)) != CRYPT_OK) { printf("Start error: %s\n", error_to_string(err)); } /* add entropy */ if ((err = yarrow_add_entropy("hello world", 11, &prng)) != CRYPT_OK) { printf("Add_entropy error: %s\n", error_to_string(err)); } int stat; unsigned char buf[1024]; long size = 1024; //ret = rsa_decrypt_key(sig, strlen(sig), buf, &size, 0, 0, hash_idx, &stat, &key); ret = rsa_sign_hash(hash, strlen(hash), buf, &size, &prng, prng_idx, hash_idx, 0, &priv_key); ret = rsa_verify_hash(sig, strlen(sig), hash, strlen(hash), hash_idx, 0, &stat, &pub_key); printf("status is : %d\n", ret); //load stuff! return 0; }
/** Start the PRNG @param prng [out] The PRNG state to initialize @return CRYPT_OK if successful */ int yarrow_start(prng_state *prng) { int err; LTC_ARGCHK(prng != NULL); /* these are the default hash/cipher combo used */ #ifdef RIJNDAEL #if YARROW_AES==0 prng->yarrow.cipher = register_cipher(&rijndael_enc_desc); #elif YARROW_AES==1 prng->yarrow.cipher = register_cipher(&aes_enc_desc); #elif YARROW_AES==2 prng->yarrow.cipher = register_cipher(&rijndael_desc); #elif YARROW_AES==3 prng->yarrow.cipher = register_cipher(&aes_desc); #endif #elif defined(BLOWFISH) prng->yarrow.cipher = register_cipher(&blowfish_desc); #elif defined(TWOFISH) prng->yarrow.cipher = register_cipher(&twofish_desc); #elif defined(RC6) prng->yarrow.cipher = register_cipher(&rc6_desc); #elif defined(RC5) prng->yarrow.cipher = register_cipher(&rc5_desc); #elif defined(SAFERP) prng->yarrow.cipher = register_cipher(&saferp_desc); #elif defined(RC2) prng->yarrow.cipher = register_cipher(&rc2_desc); #elif defined(NOEKEON) prng->yarrow.cipher = register_cipher(&noekeon_desc); #elif defined(CAST5) prng->yarrow.cipher = register_cipher(&cast5_desc); #elif defined(XTEA) prng->yarrow.cipher = register_cipher(&xtea_desc); #elif defined(SAFER) prng->yarrow.cipher = register_cipher(&safer_sk128_desc); #elif defined(DES) prng->yarrow.cipher = register_cipher(&des3_desc); #else #error YARROW needs at least one CIPHER #endif if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) { return err; } #ifdef SHA256 prng->yarrow.hash = register_hash(&sha256_desc); #elif defined(SHA512) prng->yarrow.hash = register_hash(&sha512_desc); #elif defined(TIGER) prng->yarrow.hash = register_hash(&tiger_desc); #elif defined(SHA1) prng->yarrow.hash = register_hash(&sha1_desc); #elif defined(RIPEMD160) prng->yarrow.hash = register_hash(&rmd160_desc); #elif defined(RIPEMD128) prng->yarrow.hash = register_hash(&rmd128_desc); #elif defined(MD5) prng->yarrow.hash = register_hash(&md5_desc); #elif defined(MD4) prng->yarrow.hash = register_hash(&md4_desc); #elif defined(MD2) prng->yarrow.hash = register_hash(&md2_desc); #elif defined(WHIRLPOOL) prng->yarrow.hash = register_hash(&whirlpool_desc); #else #error YARROW needs at least one HASH #endif if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { return err; } /* zero the memory used */ zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool)); return CRYPT_OK; }
void register_algs(void) { int err; #ifdef TIGER register_hash (&tiger_desc); #endif #ifdef MD2 register_hash (&md2_desc); #endif #ifdef MD4 register_hash (&md4_desc); #endif #ifdef MD5 register_hash (&md5_desc); #endif #ifdef SHA1 register_hash (&sha1_desc); #endif #ifdef SHA224 register_hash (&sha224_desc); #endif #ifdef SHA256 register_hash (&sha256_desc); #endif #ifdef SHA384 register_hash (&sha384_desc); #endif #ifdef SHA512 register_hash (&sha512_desc); #endif #ifdef RIPEMD128 register_hash (&rmd128_desc); #endif #ifdef RIPEMD160 register_hash (&rmd160_desc); #endif #ifdef WHIRLPOOL register_hash (&whirlpool_desc); #endif #ifdef CHC_HASH register_hash(&chc_desc); if ((err = chc_register(register_cipher(&aes_enc_desc))) != CRYPT_OK) { printf("chc_register error: %s\n", error_to_string(err)); exit(EXIT_FAILURE); } #endif }
int trackfile(int sockfd, char *filename, char *range, uint16_t num_trackers, in_addr_t *trackers, uint16_t maxpeers, uint16_t num_pkg_servers, in_addr_t *pkg_servers, CURL *curlhandle) { #ifdef TIMEIT struct timeval start_time, end_time; unsigned long long s, e; #endif CURLcode curlcode; uint64_t hash; uint16_t i; tracker_info_t *tracker_info, *infoptr; int info_count; char success; #ifdef TIMEIT gettimeofday(&start_time, NULL); #endif hash = hashit(filename); #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time1: %lld usec file (%s)\n", (e - s), filename); #endif #ifdef DEBUG { int j; struct in_addr in; for (j = 0 ; j < num_trackers ; ++j) { in.s_addr = trackers[j]; logmsg("trackfile:trackers[%d] = (%s)\n", j, inet_ntoa(in)); } for (j = 0 ; j < num_pkg_servers ; ++j) { in.s_addr = pkg_servers[j]; logmsg("trackfile:pkg_servers[%d] = (%s)\n", j, inet_ntoa(in)); } } #endif #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time2: %lld usec file (%s)\n", (e - s), filename); #endif /* * see if there is a prediction for this file */ tracker_info = NULL; info_count = getprediction(hash, &tracker_info); #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time3: %lld usec file (%s)\n", (e - s), filename); #endif #ifdef DEBUG if (info_count == 0) { logmsg("trackfile:pred miss (0x%016llx)\n", hash); } else { logmsg("trackfile:pred hit (0x%016llx)\n", hash); } #endif if (info_count == 0) { /* * no prediction. need to ask a tracker for peer info for * this file. */ for (i = 0 ; i < num_trackers; ++i) { #ifdef DEBUG struct in_addr in; in.s_addr = trackers[i]; logmsg("trackfile:sending lookup to tracker (%s)\n", inet_ntoa(in)); #endif info_count = lookup(sockfd, &trackers[i], hash, &tracker_info); if (info_count > 0) { break; } /* * lookup() mallocs space for 'tracker_info', so need to * free it here since we'll call lookup() again in the * next iteration */ if (tracker_info != NULL) { free(tracker_info); tracker_info = NULL; } } } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time4: %lld usec file (%s)\n", (e - s), filename); #endif #ifdef DEBUG logmsg("trackfile:info_count (%d)\n", info_count); #endif success = 0; infoptr = tracker_info; if ((info_count > 0) && (infoptr->hash == hash)) { /* * write the prediction info to a file */ save_prediction_info(tracker_info, info_count); #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time5: %lld usec file (%s)\n", (e - s), filename); #endif #ifdef DEBUG logmsg("trackfile:hash (0x%llx) : numpeers (%d)\n", infoptr->hash, infoptr->numpeers); logmsg("trackfile:peers:\n"); for (i = 0 ; i < infoptr->numpeers; ++i) { struct in_addr in; in.s_addr = infoptr->peers[i].ip; logmsg("\t%s : %c %d\n", inet_ntoa(in), (infoptr->peers[i].state == DOWNLOADING ? 'd' : 'r'), infoptr->peers[i].state); } #endif #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time6: %lld usec file (%s)\n", (e - s), filename); #endif for (i = 0 ; i < infoptr->numpeers; ++i) { #ifdef DEBUG { struct in_addr in; int k; in.s_addr = infoptr->peers[i].ip; if (strncmp(inet_ntoa(in), "10.", 3)) { for (k = 0 ; i < num_trackers; ++k) { send_msg(sockfd, &trackers[k], STOP_SERVER); } logmsg("trackfile:bogus IP (%s)\n", inet_ntoa(in)); exit(-1); } } #endif if (getremote(filename, &infoptr->peers[i], range, curlhandle) == 0) { /* * successful download, exit this loop */ success = 1; break; } else { /* * mark the peer as 'bad'. we do this by * telling the tracker server to 'unregister' * this hash. */ tracker_info_t *info; int len; int j; len = sizeof(*info) + sizeof(peer_t); if ((info = (tracker_info_t *)malloc(len)) != NULL) { bzero(info, len); info->hash = hash; info->numpeers = 1; info->peers[0].ip = infoptr->peers[i].ip; for (j = 0 ; j < num_trackers; ++j) { unregister_hash(sockfd, &trackers[j], 1, info); } free(info); } } } } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time7: %lld usec file (%s)\n", (e - s), filename); #endif if (!success) { /* * unable to download the file from a peer, need to * get it from one of the package servers */ for (i = 0 ; i < num_pkg_servers ; ++i) { peer_t pkgpeer; pkgpeer.ip = pkg_servers[i]; pkgpeer.state = READY; /* * if this is the last package server, then * disable the connection timeout -- this is our * last hope of getting the package. */ if (i == (num_pkg_servers - 1)) { if ((curlcode = curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, 0)) != CURLE_OK) { logmsg("getremote:curl_easy_setopt():failed:(%d)\n", curlcode); } } if (getremote(filename, &pkgpeer, range, curlhandle) == 0) { success = 1; } /* * reset the connection timeout */ if (i == (num_pkg_servers - 1)) { if ((curlcode = curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, 2)) != CURLE_OK) { logmsg("getremote:curl_easy_setopt():failed:(%d)\n", curlcode); } } if (success) { break; } } } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time8: %lld usec file (%s)\n", (e - s), filename); #endif if (success) { tracker_info_t info[1]; bzero(info, sizeof(info)); info[0].hash = hash; info[0].numpeers = 0; for (i = 0 ; i < num_trackers; ++i) { register_hash(sockfd, &trackers[i], 1, info); } } /* * lookup() and getprediction() mallocs tracker_info */ if (tracker_info != NULL) { free(tracker_info); } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("trackfile:svc time: %lld usec file (%s)\n", (e - s), filename); #endif if (success) { return(0); } return(-1); }