bool TorrentFile::check_piece_hash(uint32_t piece_index) { /*uint64_t offset = m_piece_info[piece_index].offset; int file_index = m_piece_info[piece_index].file_index; uint32_t piece_length = m_piece_info[piece_index].length; uint32_t to_read = piece_length; uint32_t pos = 0; std::list<int> files_list; while(pos < piece_length) { files_list.push_back(m_files[file_index].fm_id); uint64_t r = m_files[file_index].length - offset; r = r > to_read ? to_read : r; file_index++; pos += r; to_read -= r; offset = 0; } int * files = new int[files_list.size()]; int i = 0; for(std::list<int>::iterator iter = files_list.begin(); iter != files_list.end(); ++iter) { files[i] = *iter; i++; } m_torrent->m_hc->push(files, files_list.size(), m_pieces[piece_index], m_piece_info[piece_index].offset, piece_index, piece_length, m_torrent); delete[] files; return true;*/ if (read_piece(piece_index) != ERR_NO_ERROR) { m_torrent->event_piece_hash(piece_index, false, true); return ERR_INTERNAL; } unsigned char sha1[20]; memset(sha1, 0, 20); m_csha1.Update((unsigned char*)m_piece_for_check_hash, m_piece_info[piece_index].length); m_csha1.Final(); m_csha1.GetHash(sha1); m_csha1.Reset(); bool ret = memcmp(sha1,m_pieces[piece_index], 20) == 0; m_torrent->event_piece_hash(piece_index, ret, false); return true; }
/* * If given a '-p' flag, read encoded delta pieces from stdin or file * arguments, decode them and assemble any completed deltas. If given * a '-b' flag, pass any completed deltas to 'ctm' for application to * the source tree. The '-d' flag is mandatory, but either of '-p' or * '-b' can be omitted. If given the '-l' flag, notes and errors will * be timestamped and written to the given file. * * Exit status is 0 for success or 1 for indigestible input. That is, * 0 means the encode input pieces were decoded and stored, and 1 means * some input was discarded. If a delta fails to apply, this won't be * reflected in the exit status. In this case, the delta is left in * 'deltadir'. */ int main(int argc, char **argv) { char *log_file = NULL; int status = 0; int fork_ctm = 0; mask = umask(0); umask(mask); err_prog_name(argv[0]); OPTIONS("[-Dfuv] [-p piecedir] [-d deltadir] [-b basedir] [-l log] [file ...]") FLAG('D', delete_after) FLAG('f', fork_ctm) FLAG('u', set_time) FLAG('v', apply_verbose) STRING('p', piece_dir) STRING('d', delta_dir) STRING('b', base_dir) STRING('l', log_file) ENDOPTS if (delta_dir == NULL) usage(); if (piece_dir == NULL && (base_dir == NULL || argc > 1)) usage(); if (log_file != NULL) err_set_log(log_file); /* * Digest each file in turn, or just stdin if no files were given. */ if (argc <= 1) { if (piece_dir != NULL) status = read_piece(NULL); } else { while (*++argv != NULL) status |= read_piece(*argv); } /* * Maybe it's time to look for and apply completed deltas with ctm. * * Shall we report back to sendmail immediately, and let a child do * the work? Sendmail will be waiting for us to complete, delaying * other mail, and possibly some intermediate process (like MH slocal) * will terminate us if we take too long! * * If fork() fails, it's unlikely we'll be able to run ctm, so give up. * Also, the child exit status is unimportant. */ if (base_dir != NULL) if (!fork_ctm || fork() == 0) apply_complete(); return status; }
void torrent_handle::read_piece(int piece) const { INVARIANT_CHECK; TORRENT_FORWARD(read_piece(piece)); }
/* Fonction exécutée par un thread. */ void *treat_sockets(void* ptr) { int thread_id = *(int*)ptr; int sockfd, message_length, file_hash, peer_id ; int i,tmp; char message_id ; u_int s_name, file_name ; /* Buffer d'entrées sortie alloué une seule fois. */ char *io_buff = (char*) malloc(max_piecelength*sizeof(char)) ; while(1) { sockfd = pop(request) ; if(quit_program) break ; s_name = get_name(socket_map,(u_int)sockfd) ; file_hash = (int)socket_to_file[s_name] ; peer_id = (int)socket_to_peer[s_name] ; file_name = get_name(file_map,(u_int)file_hash) ; if(-1==read_socket(sockfd,(char*)&message_length,sizeof(int))) { /* pair déconnecté, on le supprime */ pthread_mutex_lock(&torrent_list[file_name]->peerlist->lock); for(i = 0 ; i < torrent_list[file_name]->peerlist->nbPeers ; i++) if(torrent_list[file_name]->peerlist->pentry[i].peerId == (u_int)peer_id) break ; assert(i < torrent_list[file_name]->peerlist->nbPeers); tmp=i; deletepeer(&torrent_list[file_name]->peerlist->pentry[i]); printf("Peer %d does not respond anymore. Deleted from list.\n",peer_id); for(i=tmp;i< torrent_list[file_name]->peerlist->nbPeers-1 ; i++) { torrent_list[file_name]->peerlist->pentry[i] = torrent_list[file_name]->peerlist->pentry[i+1] ; } torrent_list[file_name]->peerlist->nbPeers--; pthread_mutex_unlock(&torrent_list[file_name]->peerlist->lock); continue ; } assert_read_socket(sockfd,&message_id,sizeof(char)) ; switch(message_id) { case KEEP_ALIVE : pthread_mutex_lock(&print_lock) ; blue(); printf("Received KEEP_ALIVE from peer %d (file %s)\n",peer_id,torrent_list[file_name]->torrent->filename); normal() ; printf("\t\t\t\t\t\tNOT YET IMPLEMENTED.\n"); pthread_mutex_unlock(&print_lock) ; break ; case HAVE: read_have(peers[s_name],torrent_list[file_name]->torrent,thread_id); break ; case BIT_FIELD: read_bitfield(peers[s_name],torrent_list[file_name]->torrent,message_length,thread_id); break ; case REQUEST: read_request(peers[s_name],torrent_list[file_name]->torrent,io_buff,thread_id); break ; case PIECE: read_piece(peers[s_name],torrent_list[file_name]->torrent,torrent_list[file_name]->peerlist,message_length,io_buff,thread_id) ; break ; case CANCEL: pthread_mutex_lock(&print_lock) ; blue(); printf("Received CANCEL from peer %d (file %s)\n",peer_id,torrent_list[file_name]->torrent->filename); normal() ; printf("\t\t\t\t\t\tNOT YET IMPLEMENTED.\n"); assert_read_socket(sockfd,&tmp,sizeof(u_int)) ; assert_read_socket(sockfd,&tmp,sizeof(u_int)) ; assert_read_socket(sockfd,&tmp,sizeof(u_int)) ; pthread_mutex_unlock(&print_lock) ; break ; default : blue(); printf("Error, unknown message (id %d).\n",(int)message_id) ; normal() ; printf("\n"); exit(EXIT_FAILURE) ; break; } pthread_mutex_lock(&handled_request->lock) ; handled_request->queue[handled_request->last] = sockfd ; handled_request->last = (handled_request->last+1)%N_SOCK ; pthread_mutex_unlock(&handled_request->lock) ; } free(io_buff); pthread_mutex_lock(&print_lock) ; green(); printf("[#%d thread]\t\t",thread_id); normal(); printf("will quit.\n") ; pthread_mutex_unlock(&print_lock) ; pthread_exit(ptr); }
int main(void) { int r; unsigned long grid_cells_n2, pieces_n, pieces_max, column_nodes_n1, column_nodes_n2, row_nodes_n, pieces_r, piece_f, piece_l, nodes_n, i, j, k; scanf("%lu", &grid_rows); if (!grid_rows) { return EXIT_FAILURE; } scanf("%lu", &grid_columns1); if (!grid_columns1) { return EXIT_FAILURE; } grid_cells_n1 = grid_rows*grid_columns1; grid_columns2 = grid_columns1+1; grid_cells_n2 = grid_rows*grid_columns2; grid_cells = malloc(grid_cells_n2+1); if (!grid_cells) { free_data(0UL); return EXIT_FAILURE; } for (i = grid_columns1; i < grid_cells_n2; i += grid_columns2) { grid_cells[i] = '\n'; } grid_cells[grid_cells_n2] = 0; scanf("%lu", &pieces_n); if (!pieces_n) { return EXIT_FAILURE; } pieces_max = pieces_n*8; pieces = malloc(sizeof(piece_t)*pieces_max); if (!pieces) { return EXIT_FAILURE; } column_nodes_n1 = grid_cells_n1+pieces_n; column_nodes_n2 = column_nodes_n1+1; row_nodes_n = 0; pieces_r = 0; for (i = 0; i < pieces_n; i++) { if (!read_piece(&pieces[pieces_r], i)) { free_data(pieces_r); return EXIT_FAILURE; } piece_f = pieces_r; row_nodes_n += pieces[pieces_r].row_nodes_n; pieces_r++; j = 1; do { if (!rotate_piece(&pieces[pieces_r-1], &pieces[pieces_r])) { free_data(pieces_r); return EXIT_FAILURE; } r = compare_pieces(&pieces[piece_f], &pieces[pieces_r]); for (k = piece_f+1; k < pieces_r && !r; k++) { r = compare_pieces(&pieces[k], &pieces[pieces_r]); } if (!r) { row_nodes_n += pieces[pieces_r].row_nodes_n; pieces_r++; j++; } else { free_piece(&pieces[pieces_r]); } } while (j < 4 && !r); piece_l = pieces_r; j = piece_f; do { if (!flip_piece(&pieces[j], &pieces[pieces_r])) { free_data(pieces_r); return EXIT_FAILURE; } r = compare_pieces(&pieces[piece_f], &pieces[pieces_r]); for (k = piece_f+1; k < piece_l && !r; k++) { r = compare_pieces(&pieces[k], &pieces[pieces_r]); } if (!r) { row_nodes_n += pieces[pieces_r].row_nodes_n; pieces_r++; j++; } else { free_piece(&pieces[pieces_r]); } } while (j < piece_l && !r); } row_nodes = malloc(sizeof(row_node_t)*row_nodes_n); if (!row_nodes) { free_data(pieces_r); return EXIT_FAILURE; } nodes_n = column_nodes_n2+row_nodes_n; nodes = malloc(sizeof(node_t)*nodes_n); if (!nodes) { free_data(pieces_r); return EXIT_FAILURE; } for (i = column_nodes_n2; i < nodes_n; i++) { nodes[i].row_node = &row_nodes[i-column_nodes_n2]; } tops = malloc(sizeof(node_t *)*column_nodes_n1); if (!tops) { free_data(pieces_r); return EXIT_FAILURE; } header = &nodes[column_nodes_n1]; set_column_node(nodes, header); for (i = 0; i < column_nodes_n1; i++) { set_column_node(&nodes[i+1], &nodes[i]); tops[i] = &nodes[i]; } row_node = header+1; for (i = 0; i < pieces_r; i++) { print_piece(&pieces[i]); set_piece_row_nodes(&pieces[i]); } for (i = 0; i < column_nodes_n1; i++) { link_top(&nodes[i], tops[i]); } dlx_search(); printf("\nCost %lu\nSolutions %lu\n", cost, solutions); free_data(pieces_r); return EXIT_SUCCESS; }